2017-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / cp / typeck.c
blobd1111248dc70ab7d08ccebc280f4c3f342f03d27
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"
41 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
42 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
43 static tree pfn_from_ptrmemfunc (tree);
44 static tree delta_from_ptrmemfunc (tree);
45 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
46 tsubst_flags_t, int);
47 static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
48 tsubst_flags_t);
49 static tree rationalize_conditional_expr (enum tree_code, tree,
50 tsubst_flags_t);
51 static int comp_ptr_ttypes_real (tree, tree, int);
52 static bool comp_except_types (tree, tree, bool);
53 static bool comp_array_types (const_tree, const_tree, bool);
54 static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t);
55 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
56 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
57 static bool casts_away_constness (tree, tree, tsubst_flags_t);
58 static bool maybe_warn_about_returning_address_of_local (tree);
59 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
60 static void error_args_num (location_t, tree, bool);
61 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
62 tsubst_flags_t);
64 /* Do `exp = require_complete_type (exp);' to make sure exp
65 does not have an incomplete type. (That includes void types.)
66 Returns error_mark_node if the VALUE does not have
67 complete type when this function returns. */
69 tree
70 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
72 tree type;
74 if (processing_template_decl || value == error_mark_node)
75 return value;
77 if (TREE_CODE (value) == OVERLOAD)
78 type = unknown_type_node;
79 else
80 type = TREE_TYPE (value);
82 if (type == error_mark_node)
83 return error_mark_node;
85 /* First, detect a valid value with a complete type. */
86 if (COMPLETE_TYPE_P (type))
87 return value;
89 if (complete_type_or_maybe_complain (type, value, complain))
90 return value;
91 else
92 return error_mark_node;
95 tree
96 require_complete_type (tree value)
98 return require_complete_type_sfinae (value, tf_warning_or_error);
101 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
102 a template instantiation, do the instantiation. Returns TYPE,
103 whether or not it could be completed, unless something goes
104 horribly wrong, in which case the error_mark_node is returned. */
106 tree
107 complete_type (tree type)
109 if (type == NULL_TREE)
110 /* Rather than crash, we return something sure to cause an error
111 at some point. */
112 return error_mark_node;
114 if (type == error_mark_node || COMPLETE_TYPE_P (type))
116 else if (TREE_CODE (type) == ARRAY_TYPE)
118 tree t = complete_type (TREE_TYPE (type));
119 unsigned int needs_constructing, has_nontrivial_dtor;
120 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
121 layout_type (type);
122 needs_constructing
123 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
124 has_nontrivial_dtor
125 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
126 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
128 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
129 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
132 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
133 instantiate_class_template (TYPE_MAIN_VARIANT (type));
135 return type;
138 /* Like complete_type, but issue an error if the TYPE cannot be completed.
139 VALUE is used for informative diagnostics.
140 Returns NULL_TREE if the type cannot be made complete. */
142 tree
143 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
145 type = complete_type (type);
146 if (type == error_mark_node)
147 /* We already issued an error. */
148 return NULL_TREE;
149 else if (!COMPLETE_TYPE_P (type))
151 if (complain & tf_error)
152 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
153 return NULL_TREE;
155 else
156 return type;
159 tree
160 complete_type_or_else (tree type, tree value)
162 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
165 /* Return truthvalue of whether type of EXP is instantiated. */
168 type_unknown_p (const_tree exp)
170 return (TREE_CODE (exp) == TREE_LIST
171 || TREE_TYPE (exp) == unknown_type_node);
175 /* Return the common type of two parameter lists.
176 We assume that comptypes has already been done and returned 1;
177 if that isn't so, this may crash.
179 As an optimization, free the space we allocate if the parameter
180 lists are already common. */
182 static tree
183 commonparms (tree p1, tree p2)
185 tree oldargs = p1, newargs, n;
186 int i, len;
187 int any_change = 0;
189 len = list_length (p1);
190 newargs = tree_last (p1);
192 if (newargs == void_list_node)
193 i = 1;
194 else
196 i = 0;
197 newargs = 0;
200 for (; i < len; i++)
201 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
203 n = newargs;
205 for (i = 0; p1;
206 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
208 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
210 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
211 any_change = 1;
213 else if (! TREE_PURPOSE (p1))
215 if (TREE_PURPOSE (p2))
217 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
218 any_change = 1;
221 else
223 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
224 any_change = 1;
225 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
227 if (TREE_VALUE (p1) != TREE_VALUE (p2))
229 any_change = 1;
230 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
232 else
233 TREE_VALUE (n) = TREE_VALUE (p1);
235 if (! any_change)
236 return oldargs;
238 return newargs;
241 /* Given a type, perhaps copied for a typedef,
242 find the "original" version of it. */
243 static tree
244 original_type (tree t)
246 int quals = cp_type_quals (t);
247 while (t != error_mark_node
248 && TYPE_NAME (t) != NULL_TREE)
250 tree x = TYPE_NAME (t);
251 if (TREE_CODE (x) != TYPE_DECL)
252 break;
253 x = DECL_ORIGINAL_TYPE (x);
254 if (x == NULL_TREE)
255 break;
256 t = x;
258 return cp_build_qualified_type (t, quals);
261 /* Return the common type for two arithmetic types T1 and T2 under the
262 usual arithmetic conversions. The default conversions have already
263 been applied, and enumerated types converted to their compatible
264 integer types. */
266 static tree
267 cp_common_type (tree t1, tree t2)
269 enum tree_code code1 = TREE_CODE (t1);
270 enum tree_code code2 = TREE_CODE (t2);
271 tree attributes;
272 int i;
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
277 attributes. */
278 attributes = (*targetm.merge_type_attributes) (t1, t2);
280 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
282 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283 return build_type_attribute_variant (t1, attributes);
284 else
285 return NULL_TREE;
288 /* FIXME: Attributes. */
289 gcc_assert (ARITHMETIC_TYPE_P (t1)
290 || VECTOR_TYPE_P (t1)
291 || UNSCOPED_ENUM_P (t1));
292 gcc_assert (ARITHMETIC_TYPE_P (t2)
293 || VECTOR_TYPE_P (t2)
294 || UNSCOPED_ENUM_P (t2));
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
298 required type. */
299 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
301 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
303 tree subtype
304 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
306 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307 return build_type_attribute_variant (t1, attributes);
308 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309 return build_type_attribute_variant (t2, attributes);
310 else
311 return build_type_attribute_variant (build_complex_type (subtype),
312 attributes);
315 if (code1 == VECTOR_TYPE)
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1))
320 return build_type_attribute_variant (t1, attributes);
321 else
322 return build_type_attribute_variant (t2, attributes);
325 /* If only one is real, use it as the result. */
326 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327 return build_type_attribute_variant (t1, attributes);
328 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329 return build_type_attribute_variant (t2, attributes);
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333 return build_type_attribute_variant (t1, attributes);
334 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335 return build_type_attribute_variant (t2, attributes);
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339 return build_type_attribute_variant (t1, attributes);
341 if (code1 != REAL_TYPE)
343 /* If one is unsigned long long, then convert the other to unsigned
344 long long. */
345 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347 return build_type_attribute_variant (long_long_unsigned_type_node,
348 attributes);
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
353 other to long long.
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
358 case. */
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
362 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363 ? long_long_unsigned_type_node
364 : long_long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
368 /* Go through the same procedure, but for longs. */
369 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
371 return build_type_attribute_variant (long_unsigned_type_node,
372 attributes);
373 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
374 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
376 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
377 ? long_unsigned_type_node : long_integer_type_node);
378 return build_type_attribute_variant (t, attributes);
381 /* For __intN types, either the type is __int128 (and is lower
382 priority than the types checked above, but higher than other
383 128-bit types) or it's known to not be the same size as other
384 types (enforced in toplev.c). Prefer the unsigned type. */
385 for (i = 0; i < NUM_INT_N_ENTS; i ++)
387 if (int_n_enabled_p [i]
388 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
389 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
390 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
391 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
393 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
394 ? int_n_trees[i].unsigned_type
395 : int_n_trees[i].signed_type);
396 return build_type_attribute_variant (t, attributes);
400 /* Otherwise prefer the unsigned one. */
401 if (TYPE_UNSIGNED (t1))
402 return build_type_attribute_variant (t1, attributes);
403 else
404 return build_type_attribute_variant (t2, attributes);
406 else
408 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
409 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
410 return build_type_attribute_variant (long_double_type_node,
411 attributes);
412 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
413 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
414 return build_type_attribute_variant (double_type_node,
415 attributes);
416 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
417 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
418 return build_type_attribute_variant (float_type_node,
419 attributes);
421 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
422 the standard C++ floating-point types. Logic earlier in this
423 function has already eliminated the possibility that
424 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
425 compelling reason to choose one or the other. */
426 return build_type_attribute_variant (t1, attributes);
430 /* T1 and T2 are arithmetic or enumeration types. Return the type
431 that will result from the "usual arithmetic conversions" on T1 and
432 T2 as described in [expr]. */
434 tree
435 type_after_usual_arithmetic_conversions (tree t1, tree t2)
437 gcc_assert (ARITHMETIC_TYPE_P (t1)
438 || VECTOR_TYPE_P (t1)
439 || UNSCOPED_ENUM_P (t1));
440 gcc_assert (ARITHMETIC_TYPE_P (t2)
441 || VECTOR_TYPE_P (t2)
442 || UNSCOPED_ENUM_P (t2));
444 /* Perform the integral promotions. We do not promote real types here. */
445 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
446 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
448 t1 = type_promotes_to (t1);
449 t2 = type_promotes_to (t2);
452 return cp_common_type (t1, t2);
455 static void
456 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
457 composite_pointer_operation operation)
459 switch (operation)
461 case CPO_COMPARISON:
462 emit_diagnostic (kind, input_location, 0,
463 "comparison between "
464 "distinct pointer types %qT and %qT lacks a cast",
465 t1, t2);
466 break;
467 case CPO_CONVERSION:
468 emit_diagnostic (kind, input_location, 0,
469 "conversion between "
470 "distinct pointer types %qT and %qT lacks a cast",
471 t1, t2);
472 break;
473 case CPO_CONDITIONAL_EXPR:
474 emit_diagnostic (kind, input_location, 0,
475 "conditional expression between "
476 "distinct pointer types %qT and %qT lacks a cast",
477 t1, t2);
478 break;
479 default:
480 gcc_unreachable ();
484 /* Subroutine of composite_pointer_type to implement the recursive
485 case. See that function for documentation of the parameters. */
487 static tree
488 composite_pointer_type_r (tree t1, tree t2,
489 composite_pointer_operation operation,
490 tsubst_flags_t complain)
492 tree pointee1;
493 tree pointee2;
494 tree result_type;
495 tree attributes;
497 /* Determine the types pointed to by T1 and T2. */
498 if (TYPE_PTR_P (t1))
500 pointee1 = TREE_TYPE (t1);
501 pointee2 = TREE_TYPE (t2);
503 else
505 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
506 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
509 /* [expr.rel]
511 Otherwise, the composite pointer type is a pointer type
512 similar (_conv.qual_) to the type of one of the operands,
513 with a cv-qualification signature (_conv.qual_) that is the
514 union of the cv-qualification signatures of the operand
515 types. */
516 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
517 result_type = pointee1;
518 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
519 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
521 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
522 complain);
523 if (result_type == error_mark_node)
524 return error_mark_node;
526 else
528 if (complain & tf_error)
529 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
530 else
531 return error_mark_node;
532 result_type = void_type_node;
534 result_type = cp_build_qualified_type (result_type,
535 (cp_type_quals (pointee1)
536 | cp_type_quals (pointee2)));
537 /* If the original types were pointers to members, so is the
538 result. */
539 if (TYPE_PTRMEM_P (t1))
541 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
542 TYPE_PTRMEM_CLASS_TYPE (t2)))
544 if (complain & tf_error)
545 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
546 else
547 return error_mark_node;
549 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
550 result_type);
552 else
553 result_type = build_pointer_type (result_type);
555 /* Merge the attributes. */
556 attributes = (*targetm.merge_type_attributes) (t1, t2);
557 return build_type_attribute_variant (result_type, attributes);
560 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
561 ARG1 and ARG2 are the values with those types. The OPERATION is to
562 describe the operation between the pointer types,
563 in case an error occurs.
565 This routine also implements the computation of a common type for
566 pointers-to-members as per [expr.eq]. */
568 tree
569 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
570 composite_pointer_operation operation,
571 tsubst_flags_t complain)
573 tree class1;
574 tree class2;
576 /* [expr.rel]
578 If one operand is a null pointer constant, the composite pointer
579 type is the type of the other operand. */
580 if (null_ptr_cst_p (arg1))
581 return t2;
582 if (null_ptr_cst_p (arg2))
583 return t1;
585 /* We have:
587 [expr.rel]
589 If one of the operands has type "pointer to cv1 void*", then
590 the other has type "pointer to cv2T", and the composite pointer
591 type is "pointer to cv12 void", where cv12 is the union of cv1
592 and cv2.
594 If either type is a pointer to void, make sure it is T1. */
595 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
596 std::swap (t1, t2);
598 /* Now, if T1 is a pointer to void, merge the qualifiers. */
599 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
601 tree attributes;
602 tree result_type;
604 if (TYPE_PTRFN_P (t2))
606 if (complain & tf_error)
608 switch (operation)
610 case CPO_COMPARISON:
611 pedwarn (input_location, OPT_Wpedantic,
612 "ISO C++ forbids comparison between pointer "
613 "of type %<void *%> and pointer-to-function");
614 break;
615 case CPO_CONVERSION:
616 pedwarn (input_location, OPT_Wpedantic,
617 "ISO C++ forbids conversion between pointer "
618 "of type %<void *%> and pointer-to-function");
619 break;
620 case CPO_CONDITIONAL_EXPR:
621 pedwarn (input_location, OPT_Wpedantic,
622 "ISO C++ forbids conditional expression between "
623 "pointer of type %<void *%> and "
624 "pointer-to-function");
625 break;
626 default:
627 gcc_unreachable ();
630 else
631 return error_mark_node;
633 result_type
634 = cp_build_qualified_type (void_type_node,
635 (cp_type_quals (TREE_TYPE (t1))
636 | cp_type_quals (TREE_TYPE (t2))));
637 result_type = build_pointer_type (result_type);
638 /* Merge the attributes. */
639 attributes = (*targetm.merge_type_attributes) (t1, t2);
640 return build_type_attribute_variant (result_type, attributes);
643 if (c_dialect_objc () && TYPE_PTR_P (t1)
644 && TYPE_PTR_P (t2))
646 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
647 return objc_common_type (t1, t2);
650 /* if T1 or T2 is "pointer to noexcept function" and the other type is
651 "pointer to function", where the function types are otherwise the same,
652 "pointer to function" */
653 if (fnptr_conv_p (t1, t2))
654 return t1;
655 if (fnptr_conv_p (t2, t1))
656 return t2;
658 /* [expr.eq] permits the application of a pointer conversion to
659 bring the pointers to a common type. */
660 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
661 && CLASS_TYPE_P (TREE_TYPE (t1))
662 && CLASS_TYPE_P (TREE_TYPE (t2))
663 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
664 TREE_TYPE (t2)))
666 class1 = TREE_TYPE (t1);
667 class2 = TREE_TYPE (t2);
669 if (DERIVED_FROM_P (class1, class2))
670 t2 = (build_pointer_type
671 (cp_build_qualified_type (class1, cp_type_quals (class2))));
672 else if (DERIVED_FROM_P (class2, class1))
673 t1 = (build_pointer_type
674 (cp_build_qualified_type (class2, cp_type_quals (class1))));
675 else
677 if (complain & tf_error)
678 composite_pointer_error (DK_ERROR, t1, t2, operation);
679 return error_mark_node;
682 /* [expr.eq] permits the application of a pointer-to-member
683 conversion to change the class type of one of the types. */
684 else if (TYPE_PTRMEM_P (t1)
685 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
686 TYPE_PTRMEM_CLASS_TYPE (t2)))
688 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
689 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
691 if (DERIVED_FROM_P (class1, class2))
692 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
693 else if (DERIVED_FROM_P (class2, class1))
694 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
695 else
697 if (complain & tf_error)
698 switch (operation)
700 case CPO_COMPARISON:
701 error ("comparison between distinct "
702 "pointer-to-member types %qT and %qT lacks a cast",
703 t1, t2);
704 break;
705 case CPO_CONVERSION:
706 error ("conversion between distinct "
707 "pointer-to-member types %qT and %qT lacks a cast",
708 t1, t2);
709 break;
710 case CPO_CONDITIONAL_EXPR:
711 error ("conditional expression between distinct "
712 "pointer-to-member types %qT and %qT lacks a cast",
713 t1, t2);
714 break;
715 default:
716 gcc_unreachable ();
718 return error_mark_node;
722 return composite_pointer_type_r (t1, t2, operation, complain);
725 /* Return the merged type of two types.
726 We assume that comptypes has already been done and returned 1;
727 if that isn't so, this may crash.
729 This just combines attributes and default arguments; any other
730 differences would cause the two types to compare unalike. */
732 tree
733 merge_types (tree t1, tree t2)
735 enum tree_code code1;
736 enum tree_code code2;
737 tree attributes;
739 /* Save time if the two types are the same. */
740 if (t1 == t2)
741 return t1;
742 if (original_type (t1) == original_type (t2))
743 return t1;
745 /* If one type is nonsense, use the other. */
746 if (t1 == error_mark_node)
747 return t2;
748 if (t2 == error_mark_node)
749 return t1;
751 /* Handle merging an auto redeclaration with a previous deduced
752 return type. */
753 if (is_auto (t1))
754 return t2;
756 /* Merge the attributes. */
757 attributes = (*targetm.merge_type_attributes) (t1, t2);
759 if (TYPE_PTRMEMFUNC_P (t1))
760 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
761 if (TYPE_PTRMEMFUNC_P (t2))
762 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
764 code1 = TREE_CODE (t1);
765 code2 = TREE_CODE (t2);
766 if (code1 != code2)
768 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
769 if (code1 == TYPENAME_TYPE)
771 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
772 code1 = TREE_CODE (t1);
774 else
776 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
777 code2 = TREE_CODE (t2);
781 switch (code1)
783 case POINTER_TYPE:
784 case REFERENCE_TYPE:
785 /* For two pointers, do this recursively on the target type. */
787 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
788 int quals = cp_type_quals (t1);
790 if (code1 == POINTER_TYPE)
792 t1 = build_pointer_type (target);
793 if (TREE_CODE (target) == METHOD_TYPE)
794 t1 = build_ptrmemfunc_type (t1);
796 else
797 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
798 t1 = build_type_attribute_variant (t1, attributes);
799 t1 = cp_build_qualified_type (t1, quals);
801 return t1;
804 case OFFSET_TYPE:
806 int quals;
807 tree pointee;
808 quals = cp_type_quals (t1);
809 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
810 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
811 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
812 pointee);
813 t1 = cp_build_qualified_type (t1, quals);
814 break;
817 case ARRAY_TYPE:
819 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
820 /* Save space: see if the result is identical to one of the args. */
821 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
822 return build_type_attribute_variant (t1, attributes);
823 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
824 return build_type_attribute_variant (t2, attributes);
825 /* Merge the element types, and have a size if either arg has one. */
826 t1 = build_cplus_array_type
827 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
828 break;
831 case FUNCTION_TYPE:
832 /* Function types: prefer the one that specified arg types.
833 If both do, merge the arg types. Also merge the return types. */
835 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
836 tree p1 = TYPE_ARG_TYPES (t1);
837 tree p2 = TYPE_ARG_TYPES (t2);
838 tree parms;
839 tree rval, raises;
840 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
842 /* Save space: see if the result is identical to one of the args. */
843 if (valtype == TREE_TYPE (t1) && ! p2)
844 return cp_build_type_attribute_variant (t1, attributes);
845 if (valtype == TREE_TYPE (t2) && ! p1)
846 return cp_build_type_attribute_variant (t2, attributes);
848 /* Simple way if one arg fails to specify argument types. */
849 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
850 parms = p2;
851 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
852 parms = p1;
853 else
854 parms = commonparms (p1, p2);
856 rval = build_function_type (valtype, parms);
857 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
858 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
859 rval = apply_memfn_quals (rval,
860 type_memfn_quals (t1),
861 type_memfn_rqual (t1));
862 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
863 TYPE_RAISES_EXCEPTIONS (t2));
864 t1 = build_exception_variant (rval, raises);
865 if (late_return_type_p)
866 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
867 break;
870 case METHOD_TYPE:
872 /* Get this value the long way, since TYPE_METHOD_BASETYPE
873 is just the main variant of this. */
874 tree basetype = class_of_this_parm (t2);
875 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
876 TYPE_RAISES_EXCEPTIONS (t2));
877 cp_ref_qualifier rqual = type_memfn_rqual (t1);
878 tree t3;
879 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
880 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
882 /* If this was a member function type, get back to the
883 original type of type member function (i.e., without
884 the class instance variable up front. */
885 t1 = build_function_type (TREE_TYPE (t1),
886 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
887 t2 = build_function_type (TREE_TYPE (t2),
888 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
889 t3 = merge_types (t1, t2);
890 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
891 TYPE_ARG_TYPES (t3));
892 t1 = build_exception_variant (t3, raises);
893 t1 = build_ref_qualified_type (t1, rqual);
894 if (late_return_type_1_p)
895 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
896 if (late_return_type_2_p)
897 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
898 break;
901 case TYPENAME_TYPE:
902 /* There is no need to merge attributes into a TYPENAME_TYPE.
903 When the type is instantiated it will have whatever
904 attributes result from the instantiation. */
905 return t1;
907 default:;
910 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
911 return t1;
912 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
913 return t2;
914 else
915 return cp_build_type_attribute_variant (t1, attributes);
918 /* Return the ARRAY_TYPE type without its domain. */
920 tree
921 strip_array_domain (tree type)
923 tree t2;
924 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
925 if (TYPE_DOMAIN (type) == NULL_TREE)
926 return type;
927 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
928 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
931 /* Wrapper around cp_common_type that is used by c-common.c and other
932 front end optimizations that remove promotions.
934 Return the common type for two arithmetic types T1 and T2 under the
935 usual arithmetic conversions. The default conversions have already
936 been applied, and enumerated types converted to their compatible
937 integer types. */
939 tree
940 common_type (tree t1, tree t2)
942 /* If one type is nonsense, use the other */
943 if (t1 == error_mark_node)
944 return t2;
945 if (t2 == error_mark_node)
946 return t1;
948 return cp_common_type (t1, t2);
951 /* Return the common type of two pointer types T1 and T2. This is the
952 type for the result of most arithmetic operations if the operands
953 have the given two types.
955 We assume that comp_target_types has already been done and returned
956 nonzero; if that isn't so, this may crash. */
958 tree
959 common_pointer_type (tree t1, tree t2)
961 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
962 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
963 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
965 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
966 CPO_CONVERSION, tf_warning_or_error);
969 /* Compare two exception specifier types for exactness or subsetness, if
970 allowed. Returns false for mismatch, true for match (same, or
971 derived and !exact).
973 [except.spec] "If a class X ... objects of class X or any class publicly
974 and unambiguously derived from X. Similarly, if a pointer type Y * ...
975 exceptions of type Y * or that are pointers to any type publicly and
976 unambiguously derived from Y. Otherwise a function only allows exceptions
977 that have the same type ..."
978 This does not mention cv qualifiers and is different to what throw
979 [except.throw] and catch [except.catch] will do. They will ignore the
980 top level cv qualifiers, and allow qualifiers in the pointer to class
981 example.
983 We implement the letter of the standard. */
985 static bool
986 comp_except_types (tree a, tree b, bool exact)
988 if (same_type_p (a, b))
989 return true;
990 else if (!exact)
992 if (cp_type_quals (a) || cp_type_quals (b))
993 return false;
995 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
997 a = TREE_TYPE (a);
998 b = TREE_TYPE (b);
999 if (cp_type_quals (a) || cp_type_quals (b))
1000 return false;
1003 if (TREE_CODE (a) != RECORD_TYPE
1004 || TREE_CODE (b) != RECORD_TYPE)
1005 return false;
1007 if (publicly_uniquely_derived_p (a, b))
1008 return true;
1010 return false;
1013 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1014 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1015 If EXACT is ce_type, the C++17 type compatibility rules apply.
1016 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1017 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1018 are unordered, but we've already filtered out duplicates. Most lists will
1019 be in order, we should try to make use of that. */
1021 bool
1022 comp_except_specs (const_tree t1, const_tree t2, int exact)
1024 const_tree probe;
1025 const_tree base;
1026 int length = 0;
1028 if (t1 == t2)
1029 return true;
1031 /* First handle noexcept. */
1032 if (exact < ce_exact)
1034 if (exact == ce_type
1035 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1036 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1037 return true;
1039 /* noexcept(false) is compatible with no exception-specification,
1040 and less strict than any spec. */
1041 if (t1 == noexcept_false_spec)
1042 return t2 == NULL_TREE || exact == ce_derived;
1043 /* Even a derived noexcept(false) is compatible with no
1044 exception-specification. */
1045 if (t2 == noexcept_false_spec)
1046 return t1 == NULL_TREE;
1048 /* Otherwise, if we aren't looking for an exact match, noexcept is
1049 equivalent to throw(). */
1050 if (t1 == noexcept_true_spec)
1051 t1 = empty_except_spec;
1052 if (t2 == noexcept_true_spec)
1053 t2 = empty_except_spec;
1056 /* If any noexcept is left, it is only comparable to itself;
1057 either we're looking for an exact match or we're redeclaring a
1058 template with dependent noexcept. */
1059 if ((t1 && TREE_PURPOSE (t1))
1060 || (t2 && TREE_PURPOSE (t2)))
1061 return (t1 && t2
1062 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1064 if (t1 == NULL_TREE) /* T1 is ... */
1065 return t2 == NULL_TREE || exact == ce_derived;
1066 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1067 return t2 != NULL_TREE && !TREE_VALUE (t2);
1068 if (t2 == NULL_TREE) /* T2 is ... */
1069 return false;
1070 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1071 return exact == ce_derived;
1073 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1074 Count how many we find, to determine exactness. For exact matching and
1075 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1076 O(nm). */
1077 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1079 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1081 tree a = TREE_VALUE (probe);
1082 tree b = TREE_VALUE (t2);
1084 if (comp_except_types (a, b, exact))
1086 if (probe == base && exact > ce_derived)
1087 base = TREE_CHAIN (probe);
1088 length++;
1089 break;
1092 if (probe == NULL_TREE)
1093 return false;
1095 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1098 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1099 [] can match [size]. */
1101 static bool
1102 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1104 tree d1;
1105 tree d2;
1106 tree max1, max2;
1108 if (t1 == t2)
1109 return true;
1111 /* The type of the array elements must be the same. */
1112 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1113 return false;
1115 d1 = TYPE_DOMAIN (t1);
1116 d2 = TYPE_DOMAIN (t2);
1118 if (d1 == d2)
1119 return true;
1121 /* If one of the arrays is dimensionless, and the other has a
1122 dimension, they are of different types. However, it is valid to
1123 write:
1125 extern int a[];
1126 int a[3];
1128 by [basic.link]:
1130 declarations for an array object can specify
1131 array types that differ by the presence or absence of a major
1132 array bound (_dcl.array_). */
1133 if (!d1 || !d2)
1134 return allow_redeclaration;
1136 /* Check that the dimensions are the same. */
1138 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1139 return false;
1140 max1 = TYPE_MAX_VALUE (d1);
1141 max2 = TYPE_MAX_VALUE (d2);
1143 if (!cp_tree_equal (max1, max2))
1144 return false;
1146 return true;
1149 /* Compare the relative position of T1 and T2 into their respective
1150 template parameter list.
1151 T1 and T2 must be template parameter types.
1152 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1154 static bool
1155 comp_template_parms_position (tree t1, tree t2)
1157 tree index1, index2;
1158 gcc_assert (t1 && t2
1159 && TREE_CODE (t1) == TREE_CODE (t2)
1160 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1161 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1162 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1164 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1165 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1167 /* Then compare their relative position. */
1168 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1169 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1170 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1171 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1172 return false;
1174 /* In C++14 we can end up comparing 'auto' to a normal template
1175 parameter. Don't confuse them. */
1176 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1177 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1179 return true;
1182 /* Subroutine in comptypes. */
1184 static bool
1185 structural_comptypes (tree t1, tree t2, int strict)
1187 if (t1 == t2)
1188 return true;
1190 /* Suppress errors caused by previously reported errors. */
1191 if (t1 == error_mark_node || t2 == error_mark_node)
1192 return false;
1194 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1196 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1197 current instantiation. */
1198 if (TREE_CODE (t1) == TYPENAME_TYPE)
1199 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1201 if (TREE_CODE (t2) == TYPENAME_TYPE)
1202 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1204 if (TYPE_PTRMEMFUNC_P (t1))
1205 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1206 if (TYPE_PTRMEMFUNC_P (t2))
1207 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1209 /* Different classes of types can't be compatible. */
1210 if (TREE_CODE (t1) != TREE_CODE (t2))
1211 return false;
1213 /* Qualifiers must match. For array types, we will check when we
1214 recur on the array element types. */
1215 if (TREE_CODE (t1) != ARRAY_TYPE
1216 && cp_type_quals (t1) != cp_type_quals (t2))
1217 return false;
1218 if (TREE_CODE (t1) == FUNCTION_TYPE
1219 && type_memfn_quals (t1) != type_memfn_quals (t2))
1220 return false;
1221 /* Need to check this before TYPE_MAIN_VARIANT.
1222 FIXME function qualifiers should really change the main variant. */
1223 if (TREE_CODE (t1) == FUNCTION_TYPE
1224 || TREE_CODE (t1) == METHOD_TYPE)
1226 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1227 return false;
1228 if (flag_noexcept_type
1229 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1230 TYPE_RAISES_EXCEPTIONS (t2),
1231 ce_type))
1232 return false;
1235 /* Allow for two different type nodes which have essentially the same
1236 definition. Note that we already checked for equality of the type
1237 qualifiers (just above). */
1239 if (TREE_CODE (t1) != ARRAY_TYPE
1240 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1241 return true;
1244 /* Compare the types. Break out if they could be the same. */
1245 switch (TREE_CODE (t1))
1247 case VOID_TYPE:
1248 case BOOLEAN_TYPE:
1249 /* All void and bool types are the same. */
1250 break;
1252 case INTEGER_TYPE:
1253 case FIXED_POINT_TYPE:
1254 case REAL_TYPE:
1255 /* With these nodes, we can't determine type equivalence by
1256 looking at what is stored in the nodes themselves, because
1257 two nodes might have different TYPE_MAIN_VARIANTs but still
1258 represent the same type. For example, wchar_t and int could
1259 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1260 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1261 and are distinct types. On the other hand, int and the
1262 following typedef
1264 typedef int INT __attribute((may_alias));
1266 have identical properties, different TYPE_MAIN_VARIANTs, but
1267 represent the same type. The canonical type system keeps
1268 track of equivalence in this case, so we fall back on it. */
1269 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1271 case TEMPLATE_TEMPLATE_PARM:
1272 case BOUND_TEMPLATE_TEMPLATE_PARM:
1273 if (!comp_template_parms_position (t1, t2))
1274 return false;
1275 if (!comp_template_parms
1276 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1277 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1278 return false;
1279 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1280 break;
1281 /* Don't check inheritance. */
1282 strict = COMPARE_STRICT;
1283 /* Fall through. */
1285 case RECORD_TYPE:
1286 case UNION_TYPE:
1287 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1288 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1289 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1290 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1291 break;
1293 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1294 break;
1295 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1296 break;
1298 return false;
1300 case OFFSET_TYPE:
1301 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1302 strict & ~COMPARE_REDECLARATION))
1303 return false;
1304 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1305 return false;
1306 break;
1308 case REFERENCE_TYPE:
1309 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1310 return false;
1311 /* fall through to checks for pointer types */
1312 gcc_fallthrough ();
1314 case POINTER_TYPE:
1315 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1316 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1317 return false;
1318 break;
1320 case METHOD_TYPE:
1321 case FUNCTION_TYPE:
1322 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1323 return false;
1324 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1325 return false;
1326 break;
1328 case ARRAY_TYPE:
1329 /* Target types must match incl. qualifiers. */
1330 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1331 return false;
1332 break;
1334 case TEMPLATE_TYPE_PARM:
1335 /* If T1 and T2 don't have the same relative position in their
1336 template parameters set, they can't be equal. */
1337 if (!comp_template_parms_position (t1, t2))
1338 return false;
1339 /* Constrained 'auto's are distinct from parms that don't have the same
1340 constraints. */
1341 if (!equivalent_placeholder_constraints (t1, t2))
1342 return false;
1343 break;
1345 case TYPENAME_TYPE:
1346 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1347 TYPENAME_TYPE_FULLNAME (t2)))
1348 return false;
1349 /* Qualifiers don't matter on scopes. */
1350 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1351 TYPE_CONTEXT (t2)))
1352 return false;
1353 break;
1355 case UNBOUND_CLASS_TEMPLATE:
1356 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1357 return false;
1358 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1359 return false;
1360 break;
1362 case COMPLEX_TYPE:
1363 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1364 return false;
1365 break;
1367 case VECTOR_TYPE:
1368 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1369 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1370 return false;
1371 break;
1373 case TYPE_PACK_EXPANSION:
1374 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1375 PACK_EXPANSION_PATTERN (t2))
1376 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1377 PACK_EXPANSION_EXTRA_ARGS (t2)));
1379 case DECLTYPE_TYPE:
1380 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1381 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1382 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1383 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1384 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1385 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1386 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1387 DECLTYPE_TYPE_EXPR (t2)))
1388 return false;
1389 break;
1391 case UNDERLYING_TYPE:
1392 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1393 UNDERLYING_TYPE_TYPE (t2));
1395 default:
1396 return false;
1399 /* If we get here, we know that from a target independent POV the
1400 types are the same. Make sure the target attributes are also
1401 the same. */
1402 return comp_type_attributes (t1, t2);
1405 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1406 is a bitwise-or of the COMPARE_* flags. */
1408 bool
1409 comptypes (tree t1, tree t2, int strict)
1411 if (strict == COMPARE_STRICT)
1413 if (t1 == t2)
1414 return true;
1416 if (t1 == error_mark_node || t2 == error_mark_node)
1417 return false;
1419 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1420 /* At least one of the types requires structural equality, so
1421 perform a deep check. */
1422 return structural_comptypes (t1, t2, strict);
1424 if (flag_checking && USE_CANONICAL_TYPES)
1426 bool result = structural_comptypes (t1, t2, strict);
1428 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1429 /* The two types are structurally equivalent, but their
1430 canonical types were different. This is a failure of the
1431 canonical type propagation code.*/
1432 internal_error
1433 ("canonical types differ for identical types %T and %T",
1434 t1, t2);
1435 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1436 /* Two types are structurally different, but the canonical
1437 types are the same. This means we were over-eager in
1438 assigning canonical types. */
1439 internal_error
1440 ("same canonical type node for different types %T and %T",
1441 t1, t2);
1443 return result;
1445 if (!flag_checking && USE_CANONICAL_TYPES)
1446 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1447 else
1448 return structural_comptypes (t1, t2, strict);
1450 else if (strict == COMPARE_STRUCTURAL)
1451 return structural_comptypes (t1, t2, COMPARE_STRICT);
1452 else
1453 return structural_comptypes (t1, t2, strict);
1456 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1457 top-level qualifiers. */
1459 bool
1460 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1462 if (type1 == error_mark_node || type2 == error_mark_node)
1463 return false;
1465 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1466 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1467 return same_type_p (type1, type2);
1470 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1472 bool
1473 at_least_as_qualified_p (const_tree type1, const_tree type2)
1475 int q1 = cp_type_quals (type1);
1476 int q2 = cp_type_quals (type2);
1478 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1479 return (q1 & q2) == q2;
1482 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1483 more cv-qualified that TYPE1, and 0 otherwise. */
1486 comp_cv_qualification (int q1, int q2)
1488 if (q1 == q2)
1489 return 0;
1491 if ((q1 & q2) == q2)
1492 return 1;
1493 else if ((q1 & q2) == q1)
1494 return -1;
1496 return 0;
1500 comp_cv_qualification (const_tree type1, const_tree type2)
1502 int q1 = cp_type_quals (type1);
1503 int q2 = cp_type_quals (type2);
1504 return comp_cv_qualification (q1, q2);
1507 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1508 subset of the cv-qualification signature of TYPE2, and the types
1509 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1512 comp_cv_qual_signature (tree type1, tree type2)
1514 if (comp_ptr_ttypes_real (type2, type1, -1))
1515 return 1;
1516 else if (comp_ptr_ttypes_real (type1, type2, -1))
1517 return -1;
1518 else
1519 return 0;
1522 /* Subroutines of `comptypes'. */
1524 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1525 equivalent in the sense that functions with those parameter types
1526 can have equivalent types. The two lists must be equivalent,
1527 element by element. */
1529 bool
1530 compparms (const_tree parms1, const_tree parms2)
1532 const_tree t1, t2;
1534 /* An unspecified parmlist matches any specified parmlist
1535 whose argument types don't need default promotions. */
1537 for (t1 = parms1, t2 = parms2;
1538 t1 || t2;
1539 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1541 /* If one parmlist is shorter than the other,
1542 they fail to match. */
1543 if (!t1 || !t2)
1544 return false;
1545 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1546 return false;
1548 return true;
1552 /* Process a sizeof or alignof expression where the operand is a
1553 type. */
1555 tree
1556 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1558 tree value;
1559 bool dependent_p;
1561 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1562 if (type == error_mark_node)
1563 return error_mark_node;
1565 type = non_reference (type);
1566 if (TREE_CODE (type) == METHOD_TYPE)
1568 if (complain)
1569 pedwarn (input_location, OPT_Wpointer_arith,
1570 "invalid application of %qs to a member function",
1571 operator_name_info[(int) op].name);
1572 else
1573 return error_mark_node;
1574 value = size_one_node;
1577 dependent_p = dependent_type_p (type);
1578 if (!dependent_p)
1579 complete_type (type);
1580 if (dependent_p
1581 /* VLA types will have a non-constant size. In the body of an
1582 uninstantiated template, we don't need to try to compute the
1583 value, because the sizeof expression is not an integral
1584 constant expression in that case. And, if we do try to
1585 compute the value, we'll likely end up with SAVE_EXPRs, which
1586 the template substitution machinery does not expect to see. */
1587 || (processing_template_decl
1588 && COMPLETE_TYPE_P (type)
1589 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1591 value = build_min (op, size_type_node, type);
1592 TREE_READONLY (value) = 1;
1593 return value;
1596 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1597 op == SIZEOF_EXPR, false,
1598 complain);
1601 /* Return the size of the type, without producing any warnings for
1602 types whose size cannot be taken. This routine should be used only
1603 in some other routine that has already produced a diagnostic about
1604 using the size of such a type. */
1605 tree
1606 cxx_sizeof_nowarn (tree type)
1608 if (TREE_CODE (type) == FUNCTION_TYPE
1609 || VOID_TYPE_P (type)
1610 || TREE_CODE (type) == ERROR_MARK)
1611 return size_one_node;
1612 else if (!COMPLETE_TYPE_P (type))
1613 return size_zero_node;
1614 else
1615 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1618 /* Process a sizeof expression where the operand is an expression. */
1620 static tree
1621 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1623 if (e == error_mark_node)
1624 return error_mark_node;
1626 if (processing_template_decl)
1628 e = build_min (SIZEOF_EXPR, size_type_node, e);
1629 TREE_SIDE_EFFECTS (e) = 0;
1630 TREE_READONLY (e) = 1;
1632 return e;
1635 /* To get the size of a static data member declared as an array of
1636 unknown bound, we need to instantiate it. */
1637 if (VAR_P (e)
1638 && VAR_HAD_UNKNOWN_BOUND (e)
1639 && DECL_TEMPLATE_INSTANTIATION (e))
1640 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1642 if (TREE_CODE (e) == PARM_DECL
1643 && DECL_ARRAY_PARAMETER_P (e)
1644 && (complain & tf_warning))
1646 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1647 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1648 inform (DECL_SOURCE_LOCATION (e), "declared here");
1651 e = mark_type_use (e);
1653 if (bitfield_p (e))
1655 if (complain & tf_error)
1656 error ("invalid application of %<sizeof%> to a bit-field");
1657 else
1658 return error_mark_node;
1659 e = char_type_node;
1661 else if (is_overloaded_fn (e))
1663 if (complain & tf_error)
1664 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1665 "function type");
1666 else
1667 return error_mark_node;
1668 e = char_type_node;
1670 else if (type_unknown_p (e))
1672 if (complain & tf_error)
1673 cxx_incomplete_type_error (e, TREE_TYPE (e));
1674 else
1675 return error_mark_node;
1676 e = char_type_node;
1678 else
1679 e = TREE_TYPE (e);
1681 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1684 /* Implement the __alignof keyword: Return the minimum required
1685 alignment of E, measured in bytes. For VAR_DECL's and
1686 FIELD_DECL's return DECL_ALIGN (which can be set from an
1687 "aligned" __attribute__ specification). */
1689 static tree
1690 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1692 tree t;
1694 if (e == error_mark_node)
1695 return error_mark_node;
1697 if (processing_template_decl)
1699 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1700 TREE_SIDE_EFFECTS (e) = 0;
1701 TREE_READONLY (e) = 1;
1703 return e;
1706 e = mark_type_use (e);
1708 if (VAR_P (e))
1709 t = size_int (DECL_ALIGN_UNIT (e));
1710 else if (bitfield_p (e))
1712 if (complain & tf_error)
1713 error ("invalid application of %<__alignof%> to a bit-field");
1714 else
1715 return error_mark_node;
1716 t = size_one_node;
1718 else if (TREE_CODE (e) == COMPONENT_REF
1719 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1720 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1721 else if (is_overloaded_fn (e))
1723 if (complain & tf_error)
1724 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1725 "function type");
1726 else
1727 return error_mark_node;
1728 if (TREE_CODE (e) == FUNCTION_DECL)
1729 t = size_int (DECL_ALIGN_UNIT (e));
1730 else
1731 t = size_one_node;
1733 else if (type_unknown_p (e))
1735 if (complain & tf_error)
1736 cxx_incomplete_type_error (e, TREE_TYPE (e));
1737 else
1738 return error_mark_node;
1739 t = size_one_node;
1741 else
1742 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1743 complain & tf_error);
1745 return fold_convert (size_type_node, t);
1748 /* Process a sizeof or alignof expression E with code OP where the operand
1749 is an expression. */
1751 tree
1752 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1754 if (op == SIZEOF_EXPR)
1755 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1756 else
1757 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1760 /* Build a representation of an expression 'alignas(E).' Return the
1761 folded integer value of E if it is an integral constant expression
1762 that resolves to a valid alignment. If E depends on a template
1763 parameter, return a syntactic representation tree of kind
1764 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1765 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1767 tree
1768 cxx_alignas_expr (tree e)
1770 if (e == NULL_TREE || e == error_mark_node
1771 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1772 return e;
1774 if (TYPE_P (e))
1775 /* [dcl.align]/3:
1777 When the alignment-specifier is of the form
1778 alignas(type-id ), it shall have the same effect as
1779 alignas(alignof(type-id )). */
1781 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1783 /* If we reach this point, it means the alignas expression if of
1784 the form "alignas(assignment-expression)", so we should follow
1785 what is stated by [dcl.align]/2. */
1787 if (value_dependent_expression_p (e))
1788 /* Leave value-dependent expression alone for now. */
1789 return e;
1791 e = instantiate_non_dependent_expr (e);
1792 e = mark_rvalue_use (e);
1794 /* [dcl.align]/2 says:
1796 the assignment-expression shall be an integral constant
1797 expression. */
1799 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1801 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1802 return error_mark_node;
1805 return cxx_constant_value (e);
1809 /* EXPR is being used in a context that is not a function call.
1810 Enforce:
1812 [expr.ref]
1814 The expression can be used only as the left-hand operand of a
1815 member function call.
1817 [expr.mptr.operator]
1819 If the result of .* or ->* is a function, then that result can be
1820 used only as the operand for the function call operator ().
1822 by issuing an error message if appropriate. Returns true iff EXPR
1823 violates these rules. */
1825 bool
1826 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1828 if (expr == NULL_TREE)
1829 return false;
1830 /* Don't enforce this in MS mode. */
1831 if (flag_ms_extensions)
1832 return false;
1833 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1834 expr = get_first_fn (expr);
1835 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1837 if (complain & tf_error)
1839 if (DECL_P (expr))
1841 error_at (loc, "invalid use of non-static member function %qD",
1842 expr);
1843 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1845 else
1846 error_at (loc, "invalid use of non-static member function of "
1847 "type %qT", TREE_TYPE (expr));
1849 return true;
1851 return false;
1854 /* If EXP is a reference to a bitfield, and the type of EXP does not
1855 match the declared type of the bitfield, return the declared type
1856 of the bitfield. Otherwise, return NULL_TREE. */
1858 tree
1859 is_bitfield_expr_with_lowered_type (const_tree exp)
1861 switch (TREE_CODE (exp))
1863 case COND_EXPR:
1864 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1865 ? TREE_OPERAND (exp, 1)
1866 : TREE_OPERAND (exp, 0)))
1867 return NULL_TREE;
1868 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1870 case COMPOUND_EXPR:
1871 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1873 case MODIFY_EXPR:
1874 case SAVE_EXPR:
1875 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1877 case COMPONENT_REF:
1879 tree field;
1881 field = TREE_OPERAND (exp, 1);
1882 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1883 return NULL_TREE;
1884 if (same_type_ignoring_top_level_qualifiers_p
1885 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1886 return NULL_TREE;
1887 return DECL_BIT_FIELD_TYPE (field);
1890 case VAR_DECL:
1891 if (DECL_HAS_VALUE_EXPR_P (exp))
1892 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1893 (CONST_CAST_TREE (exp)));
1894 return NULL_TREE;
1896 CASE_CONVERT:
1897 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1898 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1899 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1900 /* Fallthrough. */
1902 default:
1903 return NULL_TREE;
1907 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1908 bitfield with a lowered type, the type of EXP is returned, rather
1909 than NULL_TREE. */
1911 tree
1912 unlowered_expr_type (const_tree exp)
1914 tree type;
1915 tree etype = TREE_TYPE (exp);
1917 type = is_bitfield_expr_with_lowered_type (exp);
1918 if (type)
1919 type = cp_build_qualified_type (type, cp_type_quals (etype));
1920 else
1921 type = etype;
1923 return type;
1926 /* Perform the conversions in [expr] that apply when an lvalue appears
1927 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1928 function-to-pointer conversions. In addition, bitfield references are
1929 converted to their declared types. Note that this function does not perform
1930 the lvalue-to-rvalue conversion for class types. If you need that conversion
1931 for class types, then you probably need to use force_rvalue.
1933 Although the returned value is being used as an rvalue, this
1934 function does not wrap the returned expression in a
1935 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1936 that the return value is no longer an lvalue. */
1938 tree
1939 decay_conversion (tree exp,
1940 tsubst_flags_t complain,
1941 bool reject_builtin /* = true */)
1943 tree type;
1944 enum tree_code code;
1945 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1947 type = TREE_TYPE (exp);
1948 if (type == error_mark_node)
1949 return error_mark_node;
1951 exp = resolve_nondeduced_context (exp, complain);
1952 if (type_unknown_p (exp))
1954 if (complain & tf_error)
1955 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1956 return error_mark_node;
1959 code = TREE_CODE (type);
1961 if (error_operand_p (exp))
1962 return error_mark_node;
1964 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1965 return nullptr_node;
1967 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1968 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1969 if (code == VOID_TYPE)
1971 if (complain & tf_error)
1972 error_at (loc, "void value not ignored as it ought to be");
1973 return error_mark_node;
1975 if (invalid_nonstatic_memfn_p (loc, exp, complain))
1976 return error_mark_node;
1977 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1979 exp = mark_lvalue_use (exp);
1980 if (reject_builtin && reject_gcc_builtin (exp, loc))
1981 return error_mark_node;
1982 return cp_build_addr_expr (exp, complain);
1984 if (code == ARRAY_TYPE)
1986 tree adr;
1987 tree ptrtype;
1989 exp = mark_lvalue_use (exp);
1991 if (INDIRECT_REF_P (exp))
1992 return build_nop (build_pointer_type (TREE_TYPE (type)),
1993 TREE_OPERAND (exp, 0));
1995 if (TREE_CODE (exp) == COMPOUND_EXPR)
1997 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1998 if (op1 == error_mark_node)
1999 return error_mark_node;
2000 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2001 TREE_OPERAND (exp, 0), op1);
2004 if (!obvalue_p (exp)
2005 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2007 if (complain & tf_error)
2008 error_at (loc, "invalid use of non-lvalue array");
2009 return error_mark_node;
2012 /* Don't let an array compound literal decay to a pointer. It can
2013 still be used to initialize an array or bind to a reference. */
2014 if (TREE_CODE (exp) == TARGET_EXPR)
2016 if (complain & tf_error)
2017 error_at (loc, "taking address of temporary array");
2018 return error_mark_node;
2021 ptrtype = build_pointer_type (TREE_TYPE (type));
2023 if (VAR_P (exp))
2025 if (!cxx_mark_addressable (exp))
2026 return error_mark_node;
2027 adr = build_nop (ptrtype, build_address (exp));
2028 return adr;
2030 /* This way is better for a COMPONENT_REF since it can
2031 simplify the offset for a component. */
2032 adr = cp_build_addr_expr (exp, complain);
2033 return cp_convert (ptrtype, adr, complain);
2036 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2037 exp = mark_rvalue_use (exp, loc, reject_builtin);
2039 /* If a bitfield is used in a context where integral promotion
2040 applies, then the caller is expected to have used
2041 default_conversion. That function promotes bitfields correctly
2042 before calling this function. At this point, if we have a
2043 bitfield referenced, we may assume that is not subject to
2044 promotion, and that, therefore, the type of the resulting rvalue
2045 is the declared type of the bitfield. */
2046 exp = convert_bitfield_to_declared_type (exp);
2048 /* We do not call rvalue() here because we do not want to wrap EXP
2049 in a NON_LVALUE_EXPR. */
2051 /* [basic.lval]
2053 Non-class rvalues always have cv-unqualified types. */
2054 type = TREE_TYPE (exp);
2055 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2056 exp = build_nop (cv_unqualified (type), exp);
2058 if (!complete_type_or_maybe_complain (type, exp, complain))
2059 return error_mark_node;
2061 return exp;
2064 /* Perform preparatory conversions, as part of the "usual arithmetic
2065 conversions". In particular, as per [expr]:
2067 Whenever an lvalue expression appears as an operand of an
2068 operator that expects the rvalue for that operand, the
2069 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2070 standard conversions are applied to convert the expression to an
2071 rvalue.
2073 In addition, we perform integral promotions here, as those are
2074 applied to both operands to a binary operator before determining
2075 what additional conversions should apply. */
2077 static tree
2078 cp_default_conversion (tree exp, tsubst_flags_t complain)
2080 /* Check for target-specific promotions. */
2081 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2082 if (promoted_type)
2083 exp = cp_convert (promoted_type, exp, complain);
2084 /* Perform the integral promotions first so that bitfield
2085 expressions (which may promote to "int", even if the bitfield is
2086 declared "unsigned") are promoted correctly. */
2087 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2088 exp = cp_perform_integral_promotions (exp, complain);
2089 /* Perform the other conversions. */
2090 exp = decay_conversion (exp, complain);
2092 return exp;
2095 /* C version. */
2097 tree
2098 default_conversion (tree exp)
2100 return cp_default_conversion (exp, tf_warning_or_error);
2103 /* EXPR is an expression with an integral or enumeration type.
2104 Perform the integral promotions in [conv.prom], and return the
2105 converted value. */
2107 tree
2108 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2110 tree type;
2111 tree promoted_type;
2113 expr = mark_rvalue_use (expr);
2115 /* [conv.prom]
2117 If the bitfield has an enumerated type, it is treated as any
2118 other value of that type for promotion purposes. */
2119 type = is_bitfield_expr_with_lowered_type (expr);
2120 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2121 type = TREE_TYPE (expr);
2122 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2123 /* Scoped enums don't promote. */
2124 if (SCOPED_ENUM_P (type))
2125 return expr;
2126 promoted_type = type_promotes_to (type);
2127 if (type != promoted_type)
2128 expr = cp_convert (promoted_type, expr, complain);
2129 return expr;
2132 /* C version. */
2134 tree
2135 perform_integral_promotions (tree expr)
2137 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2140 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2141 decay_conversion to one. */
2144 string_conv_p (const_tree totype, const_tree exp, int warn)
2146 tree t;
2148 if (!TYPE_PTR_P (totype))
2149 return 0;
2151 t = TREE_TYPE (totype);
2152 if (!same_type_p (t, char_type_node)
2153 && !same_type_p (t, char16_type_node)
2154 && !same_type_p (t, char32_type_node)
2155 && !same_type_p (t, wchar_type_node))
2156 return 0;
2158 if (TREE_CODE (exp) == STRING_CST)
2160 /* Make sure that we don't try to convert between char and wide chars. */
2161 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2162 return 0;
2164 else
2166 /* Is this a string constant which has decayed to 'const char *'? */
2167 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2168 if (!same_type_p (TREE_TYPE (exp), t))
2169 return 0;
2170 STRIP_NOPS (exp);
2171 if (TREE_CODE (exp) != ADDR_EXPR
2172 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2173 return 0;
2175 if (warn)
2177 if (cxx_dialect >= cxx11)
2178 pedwarn (input_location, OPT_Wwrite_strings,
2179 "ISO C++ forbids converting a string constant to %qT",
2180 totype);
2181 else
2182 warning (OPT_Wwrite_strings,
2183 "deprecated conversion from string constant to %qT",
2184 totype);
2187 return 1;
2190 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2191 can, for example, use as an lvalue. This code used to be in
2192 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2193 expressions, where we're dealing with aggregates. But now it's again only
2194 called from unary_complex_lvalue. The case (in particular) that led to
2195 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2196 get it there. */
2198 static tree
2199 rationalize_conditional_expr (enum tree_code code, tree t,
2200 tsubst_flags_t complain)
2202 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2204 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2205 the first operand is always the one to be used if both operands
2206 are equal, so we know what conditional expression this used to be. */
2207 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2209 tree op0 = TREE_OPERAND (t, 0);
2210 tree op1 = TREE_OPERAND (t, 1);
2212 /* The following code is incorrect if either operand side-effects. */
2213 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2214 && !TREE_SIDE_EFFECTS (op1));
2215 return
2216 build_conditional_expr (loc,
2217 build_x_binary_op (loc,
2218 (TREE_CODE (t) == MIN_EXPR
2219 ? LE_EXPR : GE_EXPR),
2220 op0, TREE_CODE (op0),
2221 op1, TREE_CODE (op1),
2222 /*overload=*/NULL,
2223 complain),
2224 cp_build_unary_op (code, op0, false, complain),
2225 cp_build_unary_op (code, op1, false, complain),
2226 complain);
2229 return
2230 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2231 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2232 complain),
2233 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2234 complain),
2235 complain);
2238 /* Given the TYPE of an anonymous union field inside T, return the
2239 FIELD_DECL for the field. If not found return NULL_TREE. Because
2240 anonymous unions can nest, we must also search all anonymous unions
2241 that are directly reachable. */
2243 tree
2244 lookup_anon_field (tree t, tree type)
2246 tree field;
2248 t = TYPE_MAIN_VARIANT (t);
2250 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2252 if (TREE_STATIC (field))
2253 continue;
2254 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2255 continue;
2257 /* If we find it directly, return the field. */
2258 if (DECL_NAME (field) == NULL_TREE
2259 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2261 return field;
2264 /* Otherwise, it could be nested, search harder. */
2265 if (DECL_NAME (field) == NULL_TREE
2266 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2268 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2269 if (subfield)
2270 return subfield;
2273 return NULL_TREE;
2276 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2277 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2278 non-NULL, it indicates the path to the base used to name MEMBER.
2279 If PRESERVE_REFERENCE is true, the expression returned will have
2280 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2281 returned will have the type referred to by the reference.
2283 This function does not perform access control; that is either done
2284 earlier by the parser when the name of MEMBER is resolved to MEMBER
2285 itself, or later when overload resolution selects one of the
2286 functions indicated by MEMBER. */
2288 tree
2289 build_class_member_access_expr (cp_expr object, tree member,
2290 tree access_path, bool preserve_reference,
2291 tsubst_flags_t complain)
2293 tree object_type;
2294 tree member_scope;
2295 tree result = NULL_TREE;
2296 tree using_decl = NULL_TREE;
2298 if (error_operand_p (object) || error_operand_p (member))
2299 return error_mark_node;
2301 gcc_assert (DECL_P (member) || BASELINK_P (member));
2303 /* [expr.ref]
2305 The type of the first expression shall be "class object" (of a
2306 complete type). */
2307 object_type = TREE_TYPE (object);
2308 if (!currently_open_class (object_type)
2309 && !complete_type_or_maybe_complain (object_type, object, complain))
2310 return error_mark_node;
2311 if (!CLASS_TYPE_P (object_type))
2313 if (complain & tf_error)
2315 if (POINTER_TYPE_P (object_type)
2316 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2317 error ("request for member %qD in %qE, which is of pointer "
2318 "type %qT (maybe you meant to use %<->%> ?)",
2319 member, object.get_value (), object_type);
2320 else
2321 error ("request for member %qD in %qE, which is of non-class "
2322 "type %qT", member, object.get_value (), object_type);
2324 return error_mark_node;
2327 /* The standard does not seem to actually say that MEMBER must be a
2328 member of OBJECT_TYPE. However, that is clearly what is
2329 intended. */
2330 if (DECL_P (member))
2332 member_scope = DECL_CLASS_CONTEXT (member);
2333 if (!mark_used (member, complain) && !(complain & tf_error))
2334 return error_mark_node;
2335 if (TREE_DEPRECATED (member))
2336 warn_deprecated_use (member, NULL_TREE);
2338 else
2339 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2340 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2341 presently be the anonymous union. Go outwards until we find a
2342 type related to OBJECT_TYPE. */
2343 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2344 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2345 object_type))
2346 member_scope = TYPE_CONTEXT (member_scope);
2347 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2349 if (complain & tf_error)
2351 if (TREE_CODE (member) == FIELD_DECL)
2352 error ("invalid use of nonstatic data member %qE", member);
2353 else
2354 error ("%qD is not a member of %qT", member, object_type);
2356 return error_mark_node;
2359 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2360 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2361 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2363 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2364 if (temp)
2365 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2368 /* In [expr.ref], there is an explicit list of the valid choices for
2369 MEMBER. We check for each of those cases here. */
2370 if (VAR_P (member))
2372 /* A static data member. */
2373 result = member;
2374 mark_exp_read (object);
2375 /* If OBJECT has side-effects, they are supposed to occur. */
2376 if (TREE_SIDE_EFFECTS (object))
2377 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2379 else if (TREE_CODE (member) == FIELD_DECL)
2381 /* A non-static data member. */
2382 bool null_object_p;
2383 int type_quals;
2384 tree member_type;
2386 if (INDIRECT_REF_P (object))
2387 null_object_p =
2388 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2389 else
2390 null_object_p = false;
2392 /* Convert OBJECT to the type of MEMBER. */
2393 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2394 TYPE_MAIN_VARIANT (member_scope)))
2396 tree binfo;
2397 base_kind kind;
2399 binfo = lookup_base (access_path ? access_path : object_type,
2400 member_scope, ba_unique, &kind, complain);
2401 if (binfo == error_mark_node)
2402 return error_mark_node;
2404 /* It is invalid to try to get to a virtual base of a
2405 NULL object. The most common cause is invalid use of
2406 offsetof macro. */
2407 if (null_object_p && kind == bk_via_virtual)
2409 if (complain & tf_error)
2411 error ("invalid access to non-static data member %qD in "
2412 "virtual base of NULL object", member);
2414 return error_mark_node;
2417 /* Convert to the base. */
2418 object = build_base_path (PLUS_EXPR, object, binfo,
2419 /*nonnull=*/1, complain);
2420 /* If we found the base successfully then we should be able
2421 to convert to it successfully. */
2422 gcc_assert (object != error_mark_node);
2425 /* If MEMBER is from an anonymous aggregate, we have converted
2426 OBJECT so that it refers to the class containing the
2427 anonymous union. Generate a reference to the anonymous union
2428 itself, and recur to find MEMBER. */
2429 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2430 /* When this code is called from build_field_call, the
2431 object already has the type of the anonymous union.
2432 That is because the COMPONENT_REF was already
2433 constructed, and was then disassembled before calling
2434 build_field_call. After the function-call code is
2435 cleaned up, this waste can be eliminated. */
2436 && (!same_type_ignoring_top_level_qualifiers_p
2437 (TREE_TYPE (object), DECL_CONTEXT (member))))
2439 tree anonymous_union;
2441 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2442 DECL_CONTEXT (member));
2443 object = build_class_member_access_expr (object,
2444 anonymous_union,
2445 /*access_path=*/NULL_TREE,
2446 preserve_reference,
2447 complain);
2450 /* Compute the type of the field, as described in [expr.ref]. */
2451 type_quals = TYPE_UNQUALIFIED;
2452 member_type = TREE_TYPE (member);
2453 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2455 type_quals = (cp_type_quals (member_type)
2456 | cp_type_quals (object_type));
2458 /* A field is const (volatile) if the enclosing object, or the
2459 field itself, is const (volatile). But, a mutable field is
2460 not const, even within a const object. */
2461 if (DECL_MUTABLE_P (member))
2462 type_quals &= ~TYPE_QUAL_CONST;
2463 member_type = cp_build_qualified_type (member_type, type_quals);
2466 result = build3_loc (input_location, COMPONENT_REF, member_type,
2467 object, member, NULL_TREE);
2469 /* Mark the expression const or volatile, as appropriate. Even
2470 though we've dealt with the type above, we still have to mark the
2471 expression itself. */
2472 if (type_quals & TYPE_QUAL_CONST)
2473 TREE_READONLY (result) = 1;
2474 if (type_quals & TYPE_QUAL_VOLATILE)
2475 TREE_THIS_VOLATILE (result) = 1;
2477 else if (BASELINK_P (member))
2479 /* The member is a (possibly overloaded) member function. */
2480 tree functions;
2481 tree type;
2483 /* If the MEMBER is exactly one static member function, then we
2484 know the type of the expression. Otherwise, we must wait
2485 until overload resolution has been performed. */
2486 functions = BASELINK_FUNCTIONS (member);
2487 if (TREE_CODE (functions) == FUNCTION_DECL
2488 && DECL_STATIC_FUNCTION_P (functions))
2489 type = TREE_TYPE (functions);
2490 else
2491 type = unknown_type_node;
2492 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2493 base. That will happen when the function is called. */
2494 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2496 else if (TREE_CODE (member) == CONST_DECL)
2498 /* The member is an enumerator. */
2499 result = member;
2500 /* If OBJECT has side-effects, they are supposed to occur. */
2501 if (TREE_SIDE_EFFECTS (object))
2502 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2503 object, result);
2505 else if ((using_decl = strip_using_decl (member)) != member)
2506 result = build_class_member_access_expr (object,
2507 using_decl,
2508 access_path, preserve_reference,
2509 complain);
2510 else
2512 if (complain & tf_error)
2513 error ("invalid use of %qD", member);
2514 return error_mark_node;
2517 if (!preserve_reference)
2518 /* [expr.ref]
2520 If E2 is declared to have type "reference to T", then ... the
2521 type of E1.E2 is T. */
2522 result = convert_from_reference (result);
2524 return result;
2527 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2528 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2530 static tree
2531 lookup_destructor (tree object, tree scope, tree dtor_name,
2532 tsubst_flags_t complain)
2534 tree object_type = TREE_TYPE (object);
2535 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2536 tree expr;
2538 /* We've already complained about this destructor. */
2539 if (dtor_type == error_mark_node)
2540 return error_mark_node;
2542 if (scope && !check_dtor_name (scope, dtor_type))
2544 if (complain & tf_error)
2545 error ("qualified type %qT does not match destructor name ~%qT",
2546 scope, dtor_type);
2547 return error_mark_node;
2549 if (is_auto (dtor_type))
2550 dtor_type = object_type;
2551 else if (identifier_p (dtor_type))
2553 /* In a template, names we can't find a match for are still accepted
2554 destructor names, and we check them here. */
2555 if (check_dtor_name (object_type, dtor_type))
2556 dtor_type = object_type;
2557 else
2559 if (complain & tf_error)
2560 error ("object type %qT does not match destructor name ~%qT",
2561 object_type, dtor_type);
2562 return error_mark_node;
2566 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2568 if (complain & tf_error)
2569 error ("the type being destroyed is %qT, but the destructor "
2570 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2571 return error_mark_node;
2573 expr = lookup_member (dtor_type, complete_dtor_identifier,
2574 /*protect=*/1, /*want_type=*/false,
2575 tf_warning_or_error);
2576 if (!expr)
2578 if (complain & tf_error)
2579 cxx_incomplete_type_error (dtor_name, dtor_type);
2580 return error_mark_node;
2582 expr = (adjust_result_of_qualified_name_lookup
2583 (expr, dtor_type, object_type));
2584 if (scope == NULL_TREE)
2585 /* We need to call adjust_result_of_qualified_name_lookup in case the
2586 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2587 that we still get virtual function binding. */
2588 BASELINK_QUALIFIED_P (expr) = false;
2589 return expr;
2592 /* An expression of the form "A::template B" has been resolved to
2593 DECL. Issue a diagnostic if B is not a template or template
2594 specialization. */
2596 void
2597 check_template_keyword (tree decl)
2599 /* The standard says:
2601 [temp.names]
2603 If a name prefixed by the keyword template is not a member
2604 template, the program is ill-formed.
2606 DR 228 removed the restriction that the template be a member
2607 template.
2609 DR 96, if accepted would add the further restriction that explicit
2610 template arguments must be provided if the template keyword is
2611 used, but, as of 2005-10-16, that DR is still in "drafting". If
2612 this DR is accepted, then the semantic checks here can be
2613 simplified, as the entity named must in fact be a template
2614 specialization, rather than, as at present, a set of overloaded
2615 functions containing at least one template function. */
2616 if (TREE_CODE (decl) != TEMPLATE_DECL
2617 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2619 if (VAR_P (decl))
2621 if (DECL_USE_TEMPLATE (decl)
2622 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2624 else
2625 permerror (input_location, "%qD is not a template", decl);
2627 else if (!is_overloaded_fn (decl))
2628 permerror (input_location, "%qD is not a template", decl);
2629 else
2631 tree fns;
2632 fns = decl;
2633 if (BASELINK_P (fns))
2634 fns = BASELINK_FUNCTIONS (fns);
2635 while (fns)
2637 tree fn = OVL_CURRENT (fns);
2638 if (TREE_CODE (fn) == TEMPLATE_DECL
2639 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2640 break;
2641 if (TREE_CODE (fn) == FUNCTION_DECL
2642 && DECL_USE_TEMPLATE (fn)
2643 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2644 break;
2645 fns = OVL_NEXT (fns);
2647 if (!fns)
2648 permerror (input_location, "%qD is not a template", decl);
2653 /* This function is called by the parser to process a class member
2654 access expression of the form OBJECT.NAME. NAME is a node used by
2655 the parser to represent a name; it is not yet a DECL. It may,
2656 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2657 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2658 there is no reason to do the lookup twice, so the parser keeps the
2659 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2660 be a template via the use of the "A::template B" syntax. */
2662 tree
2663 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2664 tsubst_flags_t complain)
2666 tree expr;
2667 tree object_type;
2668 tree member;
2669 tree access_path = NULL_TREE;
2670 tree orig_object = object;
2671 tree orig_name = name;
2673 if (object == error_mark_node || name == error_mark_node)
2674 return error_mark_node;
2676 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2677 if (!objc_is_public (object, name))
2678 return error_mark_node;
2680 object_type = TREE_TYPE (object);
2682 if (processing_template_decl)
2684 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2685 type_dependent_object_expression_p (object)
2686 /* If NAME is "f<args>", where either 'f' or 'args' is
2687 dependent, then the expression is dependent. */
2688 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2689 && dependent_template_id_p (TREE_OPERAND (name, 0),
2690 TREE_OPERAND (name, 1)))
2691 /* If NAME is "T::X" where "T" is dependent, then the
2692 expression is dependent. */
2693 || (TREE_CODE (name) == SCOPE_REF
2694 && TYPE_P (TREE_OPERAND (name, 0))
2695 && dependent_scope_p (TREE_OPERAND (name, 0))))
2697 dependent:
2698 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2699 orig_object, orig_name, NULL_TREE);
2701 object = build_non_dependent_expr (object);
2703 else if (c_dialect_objc ()
2704 && identifier_p (name)
2705 && (expr = objc_maybe_build_component_ref (object, name)))
2706 return expr;
2708 /* [expr.ref]
2710 The type of the first expression shall be "class object" (of a
2711 complete type). */
2712 if (!currently_open_class (object_type)
2713 && !complete_type_or_maybe_complain (object_type, object, complain))
2714 return error_mark_node;
2715 if (!CLASS_TYPE_P (object_type))
2717 if (complain & tf_error)
2719 if (POINTER_TYPE_P (object_type)
2720 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2721 error ("request for member %qD in %qE, which is of pointer "
2722 "type %qT (maybe you meant to use %<->%> ?)",
2723 name, object.get_value (), object_type);
2724 else
2725 error ("request for member %qD in %qE, which is of non-class "
2726 "type %qT", name, object.get_value (), object_type);
2728 return error_mark_node;
2731 if (BASELINK_P (name))
2732 /* A member function that has already been looked up. */
2733 member = name;
2734 else
2736 bool is_template_id = false;
2737 tree template_args = NULL_TREE;
2738 tree scope = NULL_TREE;
2740 access_path = object_type;
2742 if (TREE_CODE (name) == SCOPE_REF)
2744 /* A qualified name. The qualifying class or namespace `S'
2745 has already been looked up; it is either a TYPE or a
2746 NAMESPACE_DECL. */
2747 scope = TREE_OPERAND (name, 0);
2748 name = TREE_OPERAND (name, 1);
2750 /* If SCOPE is a namespace, then the qualified name does not
2751 name a member of OBJECT_TYPE. */
2752 if (TREE_CODE (scope) == NAMESPACE_DECL)
2754 if (complain & tf_error)
2755 error ("%<%D::%D%> is not a member of %qT",
2756 scope, name, object_type);
2757 return error_mark_node;
2761 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2763 is_template_id = true;
2764 template_args = TREE_OPERAND (name, 1);
2765 name = TREE_OPERAND (name, 0);
2767 if (TREE_CODE (name) == OVERLOAD)
2768 name = DECL_NAME (get_first_fn (name));
2769 else if (DECL_P (name))
2770 name = DECL_NAME (name);
2773 if (scope)
2775 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2777 gcc_assert (!is_template_id);
2778 /* Looking up a member enumerator (c++/56793). */
2779 if (!TYPE_CLASS_SCOPE_P (scope)
2780 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2782 if (complain & tf_error)
2783 error ("%<%D::%D%> is not a member of %qT",
2784 scope, name, object_type);
2785 return error_mark_node;
2787 tree val = lookup_enumerator (scope, name);
2788 if (!val)
2790 if (complain & tf_error)
2791 error ("%qD is not a member of %qD",
2792 name, scope);
2793 return error_mark_node;
2796 if (TREE_SIDE_EFFECTS (object))
2797 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2798 return val;
2801 gcc_assert (CLASS_TYPE_P (scope));
2802 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2804 if (constructor_name_p (name, scope))
2806 if (complain & tf_error)
2807 error ("cannot call constructor %<%T::%D%> directly",
2808 scope, name);
2809 return error_mark_node;
2812 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2813 access_path = lookup_base (object_type, scope, ba_check,
2814 NULL, complain);
2815 if (access_path == error_mark_node)
2816 return error_mark_node;
2817 if (!access_path)
2819 if (any_dependent_bases_p (object_type))
2820 goto dependent;
2821 if (complain & tf_error)
2822 error ("%qT is not a base of %qT", scope, object_type);
2823 return error_mark_node;
2827 if (TREE_CODE (name) == BIT_NOT_EXPR)
2829 if (dependent_type_p (object_type))
2830 /* The destructor isn't declared yet. */
2831 goto dependent;
2832 member = lookup_destructor (object, scope, name, complain);
2834 else
2836 /* Look up the member. */
2837 member = lookup_member (access_path, name, /*protect=*/1,
2838 /*want_type=*/false, complain);
2839 if (member == NULL_TREE)
2841 if (dependent_type_p (object_type))
2842 /* Try again at instantiation time. */
2843 goto dependent;
2844 if (complain & tf_error)
2846 tree guessed_id = lookup_member_fuzzy (access_path, name,
2847 /*want_type=*/false);
2848 if (guessed_id)
2850 location_t bogus_component_loc = input_location;
2851 gcc_rich_location rich_loc (bogus_component_loc);
2852 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2853 guessed_id);
2854 error_at_rich_loc
2855 (&rich_loc,
2856 "%q#T has no member named %qE; did you mean %qE?",
2857 TREE_CODE (access_path) == TREE_BINFO
2858 ? TREE_TYPE (access_path) : object_type, name,
2859 guessed_id);
2861 else
2862 error ("%q#T has no member named %qE",
2863 TREE_CODE (access_path) == TREE_BINFO
2864 ? TREE_TYPE (access_path) : object_type, name);
2866 return error_mark_node;
2868 if (member == error_mark_node)
2869 return error_mark_node;
2870 if (DECL_P (member)
2871 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2872 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2873 wrong, so don't use it. */
2874 goto dependent;
2875 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2876 goto dependent;
2879 if (is_template_id)
2881 tree templ = member;
2883 if (BASELINK_P (templ))
2884 member = lookup_template_function (templ, template_args);
2885 else if (variable_template_p (templ))
2886 member = (lookup_and_finish_template_variable
2887 (templ, template_args, complain));
2888 else
2890 if (complain & tf_error)
2891 error ("%qD is not a member template function", name);
2892 return error_mark_node;
2897 if (TREE_DEPRECATED (member))
2898 warn_deprecated_use (member, NULL_TREE);
2900 if (template_p)
2901 check_template_keyword (member);
2903 expr = build_class_member_access_expr (object, member, access_path,
2904 /*preserve_reference=*/false,
2905 complain);
2906 if (processing_template_decl && expr != error_mark_node)
2908 if (BASELINK_P (member))
2910 if (TREE_CODE (orig_name) == SCOPE_REF)
2911 BASELINK_QUALIFIED_P (member) = 1;
2912 orig_name = member;
2914 return build_min_non_dep (COMPONENT_REF, expr,
2915 orig_object, orig_name,
2916 NULL_TREE);
2919 return expr;
2922 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2923 type. */
2925 tree
2926 build_simple_component_ref (tree object, tree member)
2928 tree type = cp_build_qualified_type (TREE_TYPE (member),
2929 cp_type_quals (TREE_TYPE (object)));
2930 return build3_loc (input_location,
2931 COMPONENT_REF, type,
2932 object, member, NULL_TREE);
2935 /* Return an expression for the MEMBER_NAME field in the internal
2936 representation of PTRMEM, a pointer-to-member function. (Each
2937 pointer-to-member function type gets its own RECORD_TYPE so it is
2938 more convenient to access the fields by name than by FIELD_DECL.)
2939 This routine converts the NAME to a FIELD_DECL and then creates the
2940 node for the complete expression. */
2942 tree
2943 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2945 tree ptrmem_type;
2946 tree member;
2948 /* This code is a stripped down version of
2949 build_class_member_access_expr. It does not work to use that
2950 routine directly because it expects the object to be of class
2951 type. */
2952 ptrmem_type = TREE_TYPE (ptrmem);
2953 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2954 for (member = TYPE_FIELDS (ptrmem_type); member;
2955 member = DECL_CHAIN (member))
2956 if (DECL_NAME (member) == member_name)
2957 break;
2958 tree res = build_simple_component_ref (ptrmem, member);
2960 TREE_NO_WARNING (res) = 1;
2961 return res;
2964 /* Given an expression PTR for a pointer, return an expression
2965 for the value pointed to.
2966 ERRORSTRING is the name of the operator to appear in error messages.
2968 This function may need to overload OPERATOR_FNNAME.
2969 Must also handle REFERENCE_TYPEs for C++. */
2971 tree
2972 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2973 tsubst_flags_t complain)
2975 tree orig_expr = expr;
2976 tree rval;
2977 tree overload = NULL_TREE;
2979 if (processing_template_decl)
2981 /* Retain the type if we know the operand is a pointer. */
2982 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2983 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2984 if (type_dependent_expression_p (expr))
2985 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2986 expr = build_non_dependent_expr (expr);
2989 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2990 NULL_TREE, NULL_TREE, &overload, complain);
2991 if (!rval)
2992 rval = cp_build_indirect_ref (expr, errorstring, complain);
2994 if (processing_template_decl && rval != error_mark_node)
2996 if (overload != NULL_TREE)
2997 return (build_min_non_dep_op_overload
2998 (INDIRECT_REF, rval, overload, orig_expr));
3000 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3002 else
3003 return rval;
3006 /* Helper function called from c-common. */
3007 tree
3008 build_indirect_ref (location_t /*loc*/,
3009 tree ptr, ref_operator errorstring)
3011 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
3014 tree
3015 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3016 tsubst_flags_t complain)
3018 tree pointer, type;
3020 if (ptr == current_class_ptr
3021 || (TREE_CODE (ptr) == NOP_EXPR
3022 && TREE_OPERAND (ptr, 0) == current_class_ptr
3023 && (same_type_ignoring_top_level_qualifiers_p
3024 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3025 return current_class_ref;
3027 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
3028 ? ptr : decay_conversion (ptr, complain));
3029 if (pointer == error_mark_node)
3030 return error_mark_node;
3032 type = TREE_TYPE (pointer);
3034 if (POINTER_TYPE_P (type))
3036 /* [expr.unary.op]
3038 If the type of the expression is "pointer to T," the type
3039 of the result is "T." */
3040 tree t = TREE_TYPE (type);
3042 if ((CONVERT_EXPR_P (ptr)
3043 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3044 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3046 /* If a warning is issued, mark it to avoid duplicates from
3047 the backend. This only needs to be done at
3048 warn_strict_aliasing > 2. */
3049 if (warn_strict_aliasing > 2)
3050 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
3051 type, TREE_OPERAND (ptr, 0)))
3052 TREE_NO_WARNING (ptr) = 1;
3055 if (VOID_TYPE_P (t))
3057 /* A pointer to incomplete type (other than cv void) can be
3058 dereferenced [expr.unary.op]/1 */
3059 if (complain & tf_error)
3060 error ("%qT is not a pointer-to-object type", type);
3061 return error_mark_node;
3063 else if (TREE_CODE (pointer) == ADDR_EXPR
3064 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3065 /* The POINTER was something like `&x'. We simplify `*&x' to
3066 `x'. */
3067 return TREE_OPERAND (pointer, 0);
3068 else
3070 tree ref = build1 (INDIRECT_REF, t, pointer);
3072 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3073 so that we get the proper error message if the result is used
3074 to assign to. Also, &* is supposed to be a no-op. */
3075 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3076 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3077 TREE_SIDE_EFFECTS (ref)
3078 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3079 return ref;
3082 else if (!(complain & tf_error))
3083 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3085 /* `pointer' won't be an error_mark_node if we were given a
3086 pointer to member, so it's cool to check for this here. */
3087 else if (TYPE_PTRMEM_P (type))
3088 switch (errorstring)
3090 case RO_ARRAY_INDEXING:
3091 error ("invalid use of array indexing on pointer to member");
3092 break;
3093 case RO_UNARY_STAR:
3094 error ("invalid use of unary %<*%> on pointer to member");
3095 break;
3096 case RO_IMPLICIT_CONVERSION:
3097 error ("invalid use of implicit conversion on pointer to member");
3098 break;
3099 case RO_ARROW_STAR:
3100 error ("left hand operand of %<->*%> must be a pointer to class, "
3101 "but is a pointer to member of type %qT", type);
3102 break;
3103 default:
3104 gcc_unreachable ();
3106 else if (pointer != error_mark_node)
3107 invalid_indirection_error (input_location, type, errorstring);
3109 return error_mark_node;
3112 /* This handles expressions of the form "a[i]", which denotes
3113 an array reference.
3115 This is logically equivalent in C to *(a+i), but we may do it differently.
3116 If A is a variable or a member, we generate a primitive ARRAY_REF.
3117 This avoids forcing the array out of registers, and can work on
3118 arrays that are not lvalues (for example, members of structures returned
3119 by functions).
3121 If INDEX is of some user-defined type, it must be converted to
3122 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3123 will inherit the type of the array, which will be some pointer type.
3125 LOC is the location to use in building the array reference. */
3127 tree
3128 cp_build_array_ref (location_t loc, tree array, tree idx,
3129 tsubst_flags_t complain)
3131 tree ret;
3133 if (idx == 0)
3135 if (complain & tf_error)
3136 error_at (loc, "subscript missing in array reference");
3137 return error_mark_node;
3140 /* If an array's index is an array notation, then its rank cannot be
3141 greater than one. */
3142 if (flag_cilkplus && contains_array_notation_expr (idx))
3144 size_t rank = 0;
3146 /* If find_rank returns false, then it should have reported an error,
3147 thus it is unnecessary for repetition. */
3148 if (!find_rank (loc, idx, idx, true, &rank))
3149 return error_mark_node;
3150 if (rank > 1)
3152 error_at (loc, "rank of the array%'s index is greater than 1");
3153 return error_mark_node;
3156 if (TREE_TYPE (array) == error_mark_node
3157 || TREE_TYPE (idx) == error_mark_node)
3158 return error_mark_node;
3160 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3161 inside it. */
3162 switch (TREE_CODE (array))
3164 case COMPOUND_EXPR:
3166 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3167 complain);
3168 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3169 TREE_OPERAND (array, 0), value);
3170 SET_EXPR_LOCATION (ret, loc);
3171 return ret;
3174 case COND_EXPR:
3175 ret = build_conditional_expr
3176 (loc, TREE_OPERAND (array, 0),
3177 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3178 complain),
3179 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3180 complain),
3181 complain);
3182 protected_set_expr_location (ret, loc);
3183 return ret;
3185 default:
3186 break;
3189 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3191 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3193 tree rval, type;
3195 warn_array_subscript_with_type_char (loc, idx);
3197 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3199 if (complain & tf_error)
3200 error_at (loc, "array subscript is not an integer");
3201 return error_mark_node;
3204 /* Apply integral promotions *after* noticing character types.
3205 (It is unclear why we do these promotions -- the standard
3206 does not say that we should. In fact, the natural thing would
3207 seem to be to convert IDX to ptrdiff_t; we're performing
3208 pointer arithmetic.) */
3209 idx = cp_perform_integral_promotions (idx, complain);
3211 /* An array that is indexed by a non-constant
3212 cannot be stored in a register; we must be able to do
3213 address arithmetic on its address.
3214 Likewise an array of elements of variable size. */
3215 if (TREE_CODE (idx) != INTEGER_CST
3216 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3217 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3218 != INTEGER_CST)))
3220 if (!cxx_mark_addressable (array))
3221 return error_mark_node;
3224 /* An array that is indexed by a constant value which is not within
3225 the array bounds cannot be stored in a register either; because we
3226 would get a crash in store_bit_field/extract_bit_field when trying
3227 to access a non-existent part of the register. */
3228 if (TREE_CODE (idx) == INTEGER_CST
3229 && TYPE_DOMAIN (TREE_TYPE (array))
3230 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3232 if (!cxx_mark_addressable (array))
3233 return error_mark_node;
3236 /* Note in C++ it is valid to subscript a `register' array, since
3237 it is valid to take the address of something with that
3238 storage specification. */
3239 if (extra_warnings)
3241 tree foo = array;
3242 while (TREE_CODE (foo) == COMPONENT_REF)
3243 foo = TREE_OPERAND (foo, 0);
3244 if (VAR_P (foo) && DECL_REGISTER (foo)
3245 && (complain & tf_warning))
3246 warning_at (loc, OPT_Wextra,
3247 "subscripting array declared %<register%>");
3250 type = TREE_TYPE (TREE_TYPE (array));
3251 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3252 /* Array ref is const/volatile if the array elements are
3253 or if the array is.. */
3254 TREE_READONLY (rval)
3255 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3256 TREE_SIDE_EFFECTS (rval)
3257 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3258 TREE_THIS_VOLATILE (rval)
3259 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3260 ret = require_complete_type_sfinae (rval, complain);
3261 protected_set_expr_location (ret, loc);
3262 if (non_lvalue)
3263 ret = non_lvalue_loc (loc, ret);
3264 return ret;
3268 tree ar = cp_default_conversion (array, complain);
3269 tree ind = cp_default_conversion (idx, complain);
3271 /* Put the integer in IND to simplify error checking. */
3272 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3273 std::swap (ar, ind);
3275 if (ar == error_mark_node || ind == error_mark_node)
3276 return error_mark_node;
3278 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3280 if (complain & tf_error)
3281 error_at (loc, "subscripted value is neither array nor pointer");
3282 return error_mark_node;
3284 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3286 if (complain & tf_error)
3287 error_at (loc, "array subscript is not an integer");
3288 return error_mark_node;
3291 warn_array_subscript_with_type_char (loc, idx);
3293 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3294 PLUS_EXPR, ar, ind,
3295 complain),
3296 RO_ARRAY_INDEXING,
3297 complain);
3298 protected_set_expr_location (ret, loc);
3299 if (non_lvalue)
3300 ret = non_lvalue_loc (loc, ret);
3301 return ret;
3305 /* Entry point for Obj-C++. */
3307 tree
3308 build_array_ref (location_t loc, tree array, tree idx)
3310 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3313 /* Resolve a pointer to member function. INSTANCE is the object
3314 instance to use, if the member points to a virtual member.
3316 This used to avoid checking for virtual functions if basetype
3317 has no virtual functions, according to an earlier ANSI draft.
3318 With the final ISO C++ rules, such an optimization is
3319 incorrect: A pointer to a derived member can be static_cast
3320 to pointer-to-base-member, as long as the dynamic object
3321 later has the right member. So now we only do this optimization
3322 when we know the dynamic type of the object. */
3324 tree
3325 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3326 tsubst_flags_t complain)
3328 if (TREE_CODE (function) == OFFSET_REF)
3329 function = TREE_OPERAND (function, 1);
3331 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3333 tree idx, delta, e1, e2, e3, vtbl;
3334 bool nonvirtual;
3335 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3336 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3338 tree instance_ptr = *instance_ptrptr;
3339 tree instance_save_expr = 0;
3340 if (instance_ptr == error_mark_node)
3342 if (TREE_CODE (function) == PTRMEM_CST)
3344 /* Extracting the function address from a pmf is only
3345 allowed with -Wno-pmf-conversions. It only works for
3346 pmf constants. */
3347 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3348 e1 = convert (fntype, e1);
3349 return e1;
3351 else
3353 if (complain & tf_error)
3354 error ("object missing in use of %qE", function);
3355 return error_mark_node;
3359 /* True if we know that the dynamic type of the object doesn't have
3360 virtual functions, so we can assume the PFN field is a pointer. */
3361 nonvirtual = (COMPLETE_TYPE_P (basetype)
3362 && !TYPE_POLYMORPHIC_P (basetype)
3363 && resolves_to_fixed_type_p (instance_ptr, 0));
3365 /* If we don't really have an object (i.e. in an ill-formed
3366 conversion from PMF to pointer), we can't resolve virtual
3367 functions anyway. */
3368 if (!nonvirtual && is_dummy_object (instance_ptr))
3369 nonvirtual = true;
3371 if (TREE_SIDE_EFFECTS (instance_ptr))
3372 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3374 if (TREE_SIDE_EFFECTS (function))
3375 function = save_expr (function);
3377 /* Start by extracting all the information from the PMF itself. */
3378 e3 = pfn_from_ptrmemfunc (function);
3379 delta = delta_from_ptrmemfunc (function);
3380 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3381 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3383 int flag_sanitize_save;
3384 case ptrmemfunc_vbit_in_pfn:
3385 e1 = cp_build_binary_op (input_location,
3386 BIT_AND_EXPR, idx, integer_one_node,
3387 complain);
3388 idx = cp_build_binary_op (input_location,
3389 MINUS_EXPR, idx, integer_one_node,
3390 complain);
3391 if (idx == error_mark_node)
3392 return error_mark_node;
3393 break;
3395 case ptrmemfunc_vbit_in_delta:
3396 e1 = cp_build_binary_op (input_location,
3397 BIT_AND_EXPR, delta, integer_one_node,
3398 complain);
3399 /* Don't instrument the RSHIFT_EXPR we're about to create because
3400 we're going to use DELTA number of times, and that wouldn't play
3401 well with SAVE_EXPRs therein. */
3402 flag_sanitize_save = flag_sanitize;
3403 flag_sanitize = 0;
3404 delta = cp_build_binary_op (input_location,
3405 RSHIFT_EXPR, delta, integer_one_node,
3406 complain);
3407 flag_sanitize = flag_sanitize_save;
3408 if (delta == error_mark_node)
3409 return error_mark_node;
3410 break;
3412 default:
3413 gcc_unreachable ();
3416 if (e1 == error_mark_node)
3417 return error_mark_node;
3419 /* Convert down to the right base before using the instance. A
3420 special case is that in a pointer to member of class C, C may
3421 be incomplete. In that case, the function will of course be
3422 a member of C, and no conversion is required. In fact,
3423 lookup_base will fail in that case, because incomplete
3424 classes do not have BINFOs. */
3425 if (!same_type_ignoring_top_level_qualifiers_p
3426 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3428 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3429 basetype, ba_check, NULL, complain);
3430 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3431 1, complain);
3432 if (instance_ptr == error_mark_node)
3433 return error_mark_node;
3435 /* ...and then the delta in the PMF. */
3436 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3438 /* Hand back the adjusted 'this' argument to our caller. */
3439 *instance_ptrptr = instance_ptr;
3441 if (nonvirtual)
3442 /* Now just return the pointer. */
3443 return e3;
3445 /* Next extract the vtable pointer from the object. */
3446 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3447 instance_ptr);
3448 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3449 if (vtbl == error_mark_node)
3450 return error_mark_node;
3452 /* Finally, extract the function pointer from the vtable. */
3453 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3454 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3455 if (e2 == error_mark_node)
3456 return error_mark_node;
3457 TREE_CONSTANT (e2) = 1;
3459 /* When using function descriptors, the address of the
3460 vtable entry is treated as a function pointer. */
3461 if (TARGET_VTABLE_USES_DESCRIPTORS)
3462 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3463 cp_build_addr_expr (e2, complain));
3465 e2 = fold_convert (TREE_TYPE (e3), e2);
3466 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3467 if (e1 == error_mark_node)
3468 return error_mark_node;
3470 /* Make sure this doesn't get evaluated first inside one of the
3471 branches of the COND_EXPR. */
3472 if (instance_save_expr)
3473 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3474 instance_save_expr, e1);
3476 function = e1;
3478 return function;
3481 /* Used by the C-common bits. */
3482 tree
3483 build_function_call (location_t /*loc*/,
3484 tree function, tree params)
3486 return cp_build_function_call (function, params, tf_warning_or_error);
3489 /* Used by the C-common bits. */
3490 tree
3491 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3492 tree function, vec<tree, va_gc> *params,
3493 vec<tree, va_gc> * /*origtypes*/)
3495 vec<tree, va_gc> *orig_params = params;
3496 tree ret = cp_build_function_call_vec (function, &params,
3497 tf_warning_or_error);
3499 /* cp_build_function_call_vec can reallocate PARAMS by adding
3500 default arguments. That should never happen here. Verify
3501 that. */
3502 gcc_assert (params == orig_params);
3504 return ret;
3507 /* Build a function call using a tree list of arguments. */
3509 static tree
3510 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3512 vec<tree, va_gc> *vec;
3513 tree ret;
3515 vec = make_tree_vector ();
3516 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3517 vec_safe_push (vec, TREE_VALUE (params));
3518 ret = cp_build_function_call_vec (function, &vec, complain);
3519 release_tree_vector (vec);
3520 return ret;
3523 /* Build a function call using varargs. */
3525 tree
3526 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3528 vec<tree, va_gc> *vec;
3529 va_list args;
3530 tree ret, t;
3532 vec = make_tree_vector ();
3533 va_start (args, complain);
3534 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3535 vec_safe_push (vec, t);
3536 va_end (args);
3537 ret = cp_build_function_call_vec (function, &vec, complain);
3538 release_tree_vector (vec);
3539 return ret;
3542 /* Build a function call using a vector of arguments. PARAMS may be
3543 NULL if there are no parameters. This changes the contents of
3544 PARAMS. */
3546 tree
3547 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3548 tsubst_flags_t complain)
3550 tree fntype, fndecl;
3551 int is_method;
3552 tree original = function;
3553 int nargs;
3554 tree *argarray;
3555 tree parm_types;
3556 vec<tree, va_gc> *allocated = NULL;
3557 tree ret;
3559 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3560 expressions, like those used for ObjC messenger dispatches. */
3561 if (params != NULL && !vec_safe_is_empty (*params))
3562 function = objc_rewrite_function_call (function, (**params)[0]);
3564 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3565 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3566 if (TREE_CODE (function) == NOP_EXPR
3567 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3568 function = TREE_OPERAND (function, 0);
3570 if (TREE_CODE (function) == FUNCTION_DECL)
3572 /* If the function is a non-template member function
3573 or a non-template friend, then we need to check the
3574 constraints.
3576 Note that if overload resolution failed with a single
3577 candidate this function will be used to explicitly diagnose
3578 the failure for the single call expression. The check is
3579 technically redundant since we also would have failed in
3580 add_function_candidate. */
3581 if (flag_concepts
3582 && (complain & tf_error)
3583 && !constraints_satisfied_p (function))
3585 error ("cannot call function %qD", function);
3586 location_t loc = DECL_SOURCE_LOCATION (function);
3587 diagnose_constraints (loc, function, NULL_TREE);
3588 return error_mark_node;
3591 if (!mark_used (function, complain) && !(complain & tf_error))
3592 return error_mark_node;
3593 fndecl = function;
3595 /* Convert anything with function type to a pointer-to-function. */
3596 if (DECL_MAIN_P (function))
3598 if (complain & tf_error)
3599 pedwarn (input_location, OPT_Wpedantic,
3600 "ISO C++ forbids calling %<::main%> from within program");
3601 else
3602 return error_mark_node;
3604 function = build_addr_func (function, complain);
3606 else
3608 fndecl = NULL_TREE;
3610 function = build_addr_func (function, complain);
3613 if (function == error_mark_node)
3614 return error_mark_node;
3616 fntype = TREE_TYPE (function);
3618 if (TYPE_PTRMEMFUNC_P (fntype))
3620 if (complain & tf_error)
3621 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3622 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3623 original, original);
3624 return error_mark_node;
3627 is_method = (TYPE_PTR_P (fntype)
3628 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3630 if (!(TYPE_PTRFN_P (fntype)
3631 || is_method
3632 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3634 if (complain & tf_error)
3636 if (!flag_diagnostics_show_caret)
3637 error_at (input_location,
3638 "%qE cannot be used as a function", original);
3639 else if (DECL_P (original))
3640 error_at (input_location,
3641 "%qD cannot be used as a function", original);
3642 else
3643 error_at (input_location,
3644 "expression cannot be used as a function");
3647 return error_mark_node;
3650 /* fntype now gets the type of function pointed to. */
3651 fntype = TREE_TYPE (fntype);
3652 parm_types = TYPE_ARG_TYPES (fntype);
3654 if (params == NULL)
3656 allocated = make_tree_vector ();
3657 params = &allocated;
3660 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3661 complain);
3662 if (nargs < 0)
3663 return error_mark_node;
3665 argarray = (*params)->address ();
3667 /* Check for errors in format strings and inappropriately
3668 null parameters. */
3669 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3670 nargs, argarray);
3672 ret = build_cxx_call (function, nargs, argarray, complain);
3674 if (warned_p)
3676 tree c = extract_call_expr (ret);
3677 if (TREE_CODE (c) == CALL_EXPR)
3678 TREE_NO_WARNING (c) = 1;
3681 if (allocated != NULL)
3682 release_tree_vector (allocated);
3684 return ret;
3687 /* Subroutine of convert_arguments.
3688 Print an error message about a wrong number of arguments. */
3690 static void
3691 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3693 if (fndecl)
3695 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3697 if (DECL_NAME (fndecl) == NULL_TREE
3698 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3699 error_at (loc,
3700 too_many_p
3701 ? G_("too many arguments to constructor %q#D")
3702 : G_("too few arguments to constructor %q#D"),
3703 fndecl);
3704 else
3705 error_at (loc,
3706 too_many_p
3707 ? G_("too many arguments to member function %q#D")
3708 : G_("too few arguments to member function %q#D"),
3709 fndecl);
3711 else
3712 error_at (loc,
3713 too_many_p
3714 ? G_("too many arguments to function %q#D")
3715 : G_("too few arguments to function %q#D"),
3716 fndecl);
3717 if (!DECL_IS_BUILTIN (fndecl))
3718 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3720 else
3722 if (c_dialect_objc () && objc_message_selector ())
3723 error_at (loc,
3724 too_many_p
3725 ? G_("too many arguments to method %q#D")
3726 : G_("too few arguments to method %q#D"),
3727 objc_message_selector ());
3728 else
3729 error_at (loc, too_many_p ? G_("too many arguments to function")
3730 : G_("too few arguments to function"));
3734 /* Convert the actual parameter expressions in the list VALUES to the
3735 types in the list TYPELIST. The converted expressions are stored
3736 back in the VALUES vector.
3737 If parmdecls is exhausted, or when an element has NULL as its type,
3738 perform the default conversions.
3740 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3742 This is also where warnings about wrong number of args are generated.
3744 Returns the actual number of arguments processed (which might be less
3745 than the length of the vector), or -1 on error.
3747 In C++, unspecified trailing parameters can be filled in with their
3748 default arguments, if such were specified. Do so here. */
3750 static int
3751 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3752 int flags, tsubst_flags_t complain)
3754 tree typetail;
3755 unsigned int i;
3757 /* Argument passing is always copy-initialization. */
3758 flags |= LOOKUP_ONLYCONVERTING;
3760 for (i = 0, typetail = typelist;
3761 i < vec_safe_length (*values);
3762 i++)
3764 tree type = typetail ? TREE_VALUE (typetail) : 0;
3765 tree val = (**values)[i];
3767 if (val == error_mark_node || type == error_mark_node)
3768 return -1;
3770 if (type == void_type_node)
3772 if (complain & tf_error)
3774 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3775 return i;
3777 else
3778 return -1;
3781 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3782 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3783 if (TREE_CODE (val) == NOP_EXPR
3784 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3785 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3786 val = TREE_OPERAND (val, 0);
3788 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3790 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3791 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3792 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3793 val = decay_conversion (val, complain);
3796 if (val == error_mark_node)
3797 return -1;
3799 if (type != 0)
3801 /* Formal parm type is specified by a function prototype. */
3802 tree parmval;
3804 if (!COMPLETE_TYPE_P (complete_type (type)))
3806 if (complain & tf_error)
3808 if (fndecl)
3809 error ("parameter %P of %qD has incomplete type %qT",
3810 i, fndecl, type);
3811 else
3812 error ("parameter %P has incomplete type %qT", i, type);
3814 parmval = error_mark_node;
3816 else
3818 parmval = convert_for_initialization
3819 (NULL_TREE, type, val, flags,
3820 ICR_ARGPASS, fndecl, i, complain);
3821 parmval = convert_for_arg_passing (type, parmval, complain);
3824 if (parmval == error_mark_node)
3825 return -1;
3827 (**values)[i] = parmval;
3829 else
3831 if (fndecl && magic_varargs_p (fndecl))
3832 /* Don't do ellipsis conversion for __built_in_constant_p
3833 as this will result in spurious errors for non-trivial
3834 types. */
3835 val = require_complete_type_sfinae (val, complain);
3836 else
3837 val = convert_arg_to_ellipsis (val, complain);
3839 (**values)[i] = val;
3842 if (typetail)
3843 typetail = TREE_CHAIN (typetail);
3846 if (typetail != 0 && typetail != void_list_node)
3848 /* See if there are default arguments that can be used. Because
3849 we hold default arguments in the FUNCTION_TYPE (which is so
3850 wrong), we can see default parameters here from deduced
3851 contexts (and via typeof) for indirect function calls.
3852 Fortunately we know whether we have a function decl to
3853 provide default arguments in a language conformant
3854 manner. */
3855 if (fndecl && TREE_PURPOSE (typetail)
3856 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3858 for (; typetail != void_list_node; ++i)
3860 /* After DR777, with explicit template args we can end up with a
3861 default argument followed by no default argument. */
3862 if (!TREE_PURPOSE (typetail))
3863 break;
3864 tree parmval
3865 = convert_default_arg (TREE_VALUE (typetail),
3866 TREE_PURPOSE (typetail),
3867 fndecl, i, complain);
3869 if (parmval == error_mark_node)
3870 return -1;
3872 vec_safe_push (*values, parmval);
3873 typetail = TREE_CHAIN (typetail);
3874 /* ends with `...'. */
3875 if (typetail == NULL_TREE)
3876 break;
3880 if (typetail && typetail != void_list_node)
3882 if (complain & tf_error)
3883 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3884 return -1;
3888 return (int) i;
3891 /* Build a binary-operation expression, after performing default
3892 conversions on the operands. CODE is the kind of expression to
3893 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3894 are the tree codes which correspond to ARG1 and ARG2 when issuing
3895 warnings about possibly misplaced parentheses. They may differ
3896 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3897 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3898 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3899 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3900 ARG2_CODE as ERROR_MARK. */
3902 tree
3903 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3904 enum tree_code arg1_code, tree arg2,
3905 enum tree_code arg2_code, tree *overload_p,
3906 tsubst_flags_t complain)
3908 tree orig_arg1;
3909 tree orig_arg2;
3910 tree expr;
3911 tree overload = NULL_TREE;
3913 orig_arg1 = arg1;
3914 orig_arg2 = arg2;
3916 if (processing_template_decl)
3918 if (type_dependent_expression_p (arg1)
3919 || type_dependent_expression_p (arg2))
3920 return build_min_nt_loc (loc, code, arg1, arg2);
3921 arg1 = build_non_dependent_expr (arg1);
3922 arg2 = build_non_dependent_expr (arg2);
3925 if (code == DOTSTAR_EXPR)
3926 expr = build_m_component_ref (arg1, arg2, complain);
3927 else
3928 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3929 &overload, complain);
3931 if (overload_p != NULL)
3932 *overload_p = overload;
3934 /* Check for cases such as x+y<<z which users are likely to
3935 misinterpret. But don't warn about obj << x + y, since that is a
3936 common idiom for I/O. */
3937 if (warn_parentheses
3938 && (complain & tf_warning)
3939 && !processing_template_decl
3940 && !error_operand_p (arg1)
3941 && !error_operand_p (arg2)
3942 && (code != LSHIFT_EXPR
3943 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3944 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3945 arg2_code, orig_arg2);
3947 if (processing_template_decl && expr != error_mark_node)
3949 if (overload != NULL_TREE)
3950 return (build_min_non_dep_op_overload
3951 (code, expr, overload, orig_arg1, orig_arg2));
3953 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3956 return expr;
3959 /* Build and return an ARRAY_REF expression. */
3961 tree
3962 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3963 tsubst_flags_t complain)
3965 tree orig_arg1 = arg1;
3966 tree orig_arg2 = arg2;
3967 tree expr;
3968 tree overload = NULL_TREE;
3970 if (processing_template_decl)
3972 if (type_dependent_expression_p (arg1)
3973 || type_dependent_expression_p (arg2))
3974 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3975 NULL_TREE, NULL_TREE);
3976 arg1 = build_non_dependent_expr (arg1);
3977 arg2 = build_non_dependent_expr (arg2);
3980 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3981 NULL_TREE, &overload, complain);
3983 if (processing_template_decl && expr != error_mark_node)
3985 if (overload != NULL_TREE)
3986 return (build_min_non_dep_op_overload
3987 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
3989 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3990 NULL_TREE, NULL_TREE);
3992 return expr;
3995 /* Return whether OP is an expression of enum type cast to integer
3996 type. In C++ even unsigned enum types are cast to signed integer
3997 types. We do not want to issue warnings about comparisons between
3998 signed and unsigned types when one of the types is an enum type.
3999 Those warnings are always false positives in practice. */
4001 static bool
4002 enum_cast_to_int (tree op)
4004 if (CONVERT_EXPR_P (op)
4005 && TREE_TYPE (op) == integer_type_node
4006 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4007 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4008 return true;
4010 /* The cast may have been pushed into a COND_EXPR. */
4011 if (TREE_CODE (op) == COND_EXPR)
4012 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4013 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4015 return false;
4018 /* For the c-common bits. */
4019 tree
4020 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4021 int /*convert_p*/)
4023 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4026 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4027 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4029 static tree
4030 build_vec_cmp (tree_code code, tree type,
4031 tree arg0, tree arg1)
4033 tree zero_vec = build_zero_cst (type);
4034 tree minus_one_vec = build_minus_one_cst (type);
4035 tree cmp_type = build_same_sized_truth_vector_type(type);
4036 tree cmp = build2 (code, cmp_type, arg0, arg1);
4037 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4040 /* Possibly warn about an address never being NULL. */
4042 static void
4043 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4045 if (!warn_address
4046 || (complain & tf_warning) == 0
4047 || c_inhibit_evaluation_warnings != 0
4048 || TREE_NO_WARNING (op))
4049 return;
4051 tree cop = fold_non_dependent_expr (op);
4053 if (TREE_CODE (cop) == ADDR_EXPR
4054 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4055 && !TREE_NO_WARNING (cop))
4056 warning_at (location, OPT_Waddress, "the address of %qD will never "
4057 "be NULL", TREE_OPERAND (cop, 0));
4059 if (CONVERT_EXPR_P (op)
4060 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4062 tree inner_op = op;
4063 STRIP_NOPS (inner_op);
4065 if (DECL_P (inner_op))
4066 warning_at (location, OPT_Waddress,
4067 "the compiler can assume that the address of "
4068 "%qD will never be NULL", inner_op);
4072 /* Build a binary-operation expression without default conversions.
4073 CODE is the kind of expression to build.
4074 LOCATION is the location_t of the operator in the source code.
4075 This function differs from `build' in several ways:
4076 the data type of the result is computed and recorded in it,
4077 warnings are generated if arg data types are invalid,
4078 special handling for addition and subtraction of pointers is known,
4079 and some optimization is done (operations on narrow ints
4080 are done in the narrower type when that gives the same result).
4081 Constant folding is also done before the result is returned.
4083 Note that the operands will never have enumeral types
4084 because either they have just had the default conversions performed
4085 or they have both just been converted to some other type in which
4086 the arithmetic is to be done.
4088 C++: must do special pointer arithmetic when implementing
4089 multiple inheritance, and deal with pointer to member functions. */
4091 tree
4092 cp_build_binary_op (location_t location,
4093 enum tree_code code, tree orig_op0, tree orig_op1,
4094 tsubst_flags_t complain)
4096 tree op0, op1;
4097 enum tree_code code0, code1;
4098 tree type0, type1;
4099 const char *invalid_op_diag;
4101 /* Expression code to give to the expression when it is built.
4102 Normally this is CODE, which is what the caller asked for,
4103 but in some special cases we change it. */
4104 enum tree_code resultcode = code;
4106 /* Data type in which the computation is to be performed.
4107 In the simplest cases this is the common type of the arguments. */
4108 tree result_type = NULL;
4110 /* Nonzero means operands have already been type-converted
4111 in whatever way is necessary.
4112 Zero means they need to be converted to RESULT_TYPE. */
4113 int converted = 0;
4115 /* Nonzero means create the expression with this type, rather than
4116 RESULT_TYPE. */
4117 tree build_type = 0;
4119 /* Nonzero means after finally constructing the expression
4120 convert it to this type. */
4121 tree final_type = 0;
4123 tree result, result_ovl;
4124 tree orig_type = NULL;
4126 /* Nonzero if this is an operation like MIN or MAX which can
4127 safely be computed in short if both args are promoted shorts.
4128 Also implies COMMON.
4129 -1 indicates a bitwise operation; this makes a difference
4130 in the exact conditions for when it is safe to do the operation
4131 in a narrower mode. */
4132 int shorten = 0;
4134 /* Nonzero if this is a comparison operation;
4135 if both args are promoted shorts, compare the original shorts.
4136 Also implies COMMON. */
4137 int short_compare = 0;
4139 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4140 int common = 0;
4142 /* True if both operands have arithmetic type. */
4143 bool arithmetic_types_p;
4145 /* Apply default conversions. */
4146 op0 = orig_op0;
4147 op1 = orig_op1;
4149 /* Remember whether we're doing / or %. */
4150 bool doing_div_or_mod = false;
4152 /* Remember whether we're doing << or >>. */
4153 bool doing_shift = false;
4155 /* Tree holding instrumentation expression. */
4156 tree instrument_expr = NULL;
4158 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4159 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4160 || code == TRUTH_XOR_EXPR)
4162 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4163 op0 = decay_conversion (op0, complain);
4164 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4165 op1 = decay_conversion (op1, complain);
4167 else
4169 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4170 op0 = cp_default_conversion (op0, complain);
4171 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4172 op1 = cp_default_conversion (op1, complain);
4175 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4176 STRIP_TYPE_NOPS (op0);
4177 STRIP_TYPE_NOPS (op1);
4179 /* DTRT if one side is an overloaded function, but complain about it. */
4180 if (type_unknown_p (op0))
4182 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4183 if (t != error_mark_node)
4185 if (complain & tf_error)
4186 permerror (input_location, "assuming cast to type %qT from overloaded function",
4187 TREE_TYPE (t));
4188 op0 = t;
4191 if (type_unknown_p (op1))
4193 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4194 if (t != error_mark_node)
4196 if (complain & tf_error)
4197 permerror (input_location, "assuming cast to type %qT from overloaded function",
4198 TREE_TYPE (t));
4199 op1 = t;
4203 type0 = TREE_TYPE (op0);
4204 type1 = TREE_TYPE (op1);
4206 /* The expression codes of the data types of the arguments tell us
4207 whether the arguments are integers, floating, pointers, etc. */
4208 code0 = TREE_CODE (type0);
4209 code1 = TREE_CODE (type1);
4211 /* If an error was already reported for one of the arguments,
4212 avoid reporting another error. */
4213 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4214 return error_mark_node;
4216 if ((invalid_op_diag
4217 = targetm.invalid_binary_op (code, type0, type1)))
4219 if (complain & tf_error)
4220 error (invalid_op_diag);
4221 return error_mark_node;
4224 /* Issue warnings about peculiar, but valid, uses of NULL. */
4225 if ((orig_op0 == null_node || orig_op1 == null_node)
4226 /* It's reasonable to use pointer values as operands of &&
4227 and ||, so NULL is no exception. */
4228 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4229 && ( /* Both are NULL (or 0) and the operation was not a
4230 comparison or a pointer subtraction. */
4231 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4232 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4233 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4234 || (!null_ptr_cst_p (orig_op0)
4235 && !TYPE_PTR_OR_PTRMEM_P (type0))
4236 || (!null_ptr_cst_p (orig_op1)
4237 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4238 && (complain & tf_warning))
4240 source_location loc =
4241 expansion_point_location_if_in_system_header (input_location);
4243 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4246 /* In case when one of the operands of the binary operation is
4247 a vector and another is a scalar -- convert scalar to vector. */
4248 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4250 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4251 complain & tf_error);
4253 switch (convert_flag)
4255 case stv_error:
4256 return error_mark_node;
4257 case stv_firstarg:
4259 op0 = convert (TREE_TYPE (type1), op0);
4260 op0 = save_expr (op0);
4261 op0 = build_vector_from_val (type1, op0);
4262 type0 = TREE_TYPE (op0);
4263 code0 = TREE_CODE (type0);
4264 converted = 1;
4265 break;
4267 case stv_secondarg:
4269 op1 = convert (TREE_TYPE (type0), op1);
4270 op1 = save_expr (op1);
4271 op1 = build_vector_from_val (type0, op1);
4272 type1 = TREE_TYPE (op1);
4273 code1 = TREE_CODE (type1);
4274 converted = 1;
4275 break;
4277 default:
4278 break;
4282 switch (code)
4284 case MINUS_EXPR:
4285 /* Subtraction of two similar pointers.
4286 We must subtract them as integers, then divide by object size. */
4287 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4288 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4289 TREE_TYPE (type1)))
4290 return pointer_diff (location, op0, op1,
4291 common_pointer_type (type0, type1), complain);
4292 /* In all other cases except pointer - int, the usual arithmetic
4293 rules apply. */
4294 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4296 common = 1;
4297 break;
4299 /* The pointer - int case is just like pointer + int; fall
4300 through. */
4301 gcc_fallthrough ();
4302 case PLUS_EXPR:
4303 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4304 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4306 tree ptr_operand;
4307 tree int_operand;
4308 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4309 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4310 if (processing_template_decl)
4312 result_type = TREE_TYPE (ptr_operand);
4313 break;
4315 return cp_pointer_int_sum (location, code,
4316 ptr_operand,
4317 int_operand,
4318 complain);
4320 common = 1;
4321 break;
4323 case MULT_EXPR:
4324 common = 1;
4325 break;
4327 case TRUNC_DIV_EXPR:
4328 case CEIL_DIV_EXPR:
4329 case FLOOR_DIV_EXPR:
4330 case ROUND_DIV_EXPR:
4331 case EXACT_DIV_EXPR:
4332 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4333 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4334 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4335 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4337 enum tree_code tcode0 = code0, tcode1 = code1;
4338 tree cop1 = fold_non_dependent_expr (op1);
4339 doing_div_or_mod = true;
4340 warn_for_div_by_zero (location, cop1);
4342 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4343 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4344 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4345 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4347 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4348 resultcode = RDIV_EXPR;
4349 else
4350 /* When dividing two signed integers, we have to promote to int.
4351 unless we divide by a constant != -1. Note that default
4352 conversion will have been performed on the operands at this
4353 point, so we have to dig out the original type to find out if
4354 it was unsigned. */
4355 shorten = ((TREE_CODE (op0) == NOP_EXPR
4356 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4357 || (TREE_CODE (op1) == INTEGER_CST
4358 && ! integer_all_onesp (op1)));
4360 common = 1;
4362 break;
4364 case BIT_AND_EXPR:
4365 case BIT_IOR_EXPR:
4366 case BIT_XOR_EXPR:
4367 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4368 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4369 && !VECTOR_FLOAT_TYPE_P (type0)
4370 && !VECTOR_FLOAT_TYPE_P (type1)))
4371 shorten = -1;
4372 break;
4374 case TRUNC_MOD_EXPR:
4375 case FLOOR_MOD_EXPR:
4377 tree cop1 = fold_non_dependent_expr (op1);
4378 doing_div_or_mod = true;
4379 warn_for_div_by_zero (location, cop1);
4382 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4383 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4384 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4385 common = 1;
4386 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4388 /* Although it would be tempting to shorten always here, that loses
4389 on some targets, since the modulo instruction is undefined if the
4390 quotient can't be represented in the computation mode. We shorten
4391 only if unsigned or if dividing by something we know != -1. */
4392 shorten = ((TREE_CODE (op0) == NOP_EXPR
4393 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4394 || (TREE_CODE (op1) == INTEGER_CST
4395 && ! integer_all_onesp (op1)));
4396 common = 1;
4398 break;
4400 case TRUTH_ANDIF_EXPR:
4401 case TRUTH_ORIF_EXPR:
4402 case TRUTH_AND_EXPR:
4403 case TRUTH_OR_EXPR:
4404 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4406 if (!COMPARISON_CLASS_P (op1))
4407 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4408 build_zero_cst (type1), complain);
4409 if (code == TRUTH_ANDIF_EXPR)
4411 tree z = build_zero_cst (TREE_TYPE (op1));
4412 return build_conditional_expr (location, op0, op1, z, complain);
4414 else if (code == TRUTH_ORIF_EXPR)
4416 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4417 return build_conditional_expr (location, op0, m1, op1, complain);
4419 else
4420 gcc_unreachable ();
4422 if (VECTOR_TYPE_P (type0))
4424 if (!COMPARISON_CLASS_P (op0))
4425 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4426 build_zero_cst (type0), complain);
4427 if (!VECTOR_TYPE_P (type1))
4429 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4430 tree z = build_zero_cst (TREE_TYPE (op0));
4431 op1 = build_conditional_expr (location, op1, m1, z, complain);
4433 else if (!COMPARISON_CLASS_P (op1))
4434 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4435 build_zero_cst (type1), complain);
4437 if (code == TRUTH_ANDIF_EXPR)
4438 code = BIT_AND_EXPR;
4439 else if (code == TRUTH_ORIF_EXPR)
4440 code = BIT_IOR_EXPR;
4441 else
4442 gcc_unreachable ();
4444 return cp_build_binary_op (location, code, op0, op1, complain);
4447 result_type = boolean_type_node;
4448 break;
4450 /* Shift operations: result has same type as first operand;
4451 always convert second operand to int.
4452 Also set SHORT_SHIFT if shifting rightward. */
4454 case RSHIFT_EXPR:
4455 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4456 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4458 result_type = type0;
4459 converted = 1;
4461 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4462 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4463 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4464 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4466 result_type = type0;
4467 converted = 1;
4469 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4471 tree const_op1 = fold_non_dependent_expr (op1);
4472 if (TREE_CODE (const_op1) != INTEGER_CST)
4473 const_op1 = op1;
4474 result_type = type0;
4475 doing_shift = true;
4476 if (TREE_CODE (const_op1) == INTEGER_CST)
4478 if (tree_int_cst_lt (const_op1, integer_zero_node))
4480 if ((complain & tf_warning)
4481 && c_inhibit_evaluation_warnings == 0)
4482 warning (OPT_Wshift_count_negative,
4483 "right shift count is negative");
4485 else
4487 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4488 && (complain & tf_warning)
4489 && c_inhibit_evaluation_warnings == 0)
4490 warning (OPT_Wshift_count_overflow,
4491 "right shift count >= width of type");
4494 /* Avoid converting op1 to result_type later. */
4495 converted = 1;
4497 break;
4499 case LSHIFT_EXPR:
4500 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4501 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4503 result_type = type0;
4504 converted = 1;
4506 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4507 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4508 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4509 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4511 result_type = type0;
4512 converted = 1;
4514 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4516 tree const_op0 = fold_non_dependent_expr (op0);
4517 if (TREE_CODE (const_op0) != INTEGER_CST)
4518 const_op0 = op0;
4519 tree const_op1 = fold_non_dependent_expr (op1);
4520 if (TREE_CODE (const_op1) != INTEGER_CST)
4521 const_op1 = op1;
4522 result_type = type0;
4523 doing_shift = true;
4524 if (TREE_CODE (const_op0) == INTEGER_CST
4525 && tree_int_cst_sgn (const_op0) < 0
4526 && (complain & tf_warning)
4527 && c_inhibit_evaluation_warnings == 0)
4528 warning (OPT_Wshift_negative_value,
4529 "left shift of negative value");
4530 if (TREE_CODE (const_op1) == INTEGER_CST)
4532 if (tree_int_cst_lt (const_op1, integer_zero_node))
4534 if ((complain & tf_warning)
4535 && c_inhibit_evaluation_warnings == 0)
4536 warning (OPT_Wshift_count_negative,
4537 "left shift count is negative");
4539 else if (compare_tree_int (const_op1,
4540 TYPE_PRECISION (type0)) >= 0)
4542 if ((complain & tf_warning)
4543 && c_inhibit_evaluation_warnings == 0)
4544 warning (OPT_Wshift_count_overflow,
4545 "left shift count >= width of type");
4547 else if (TREE_CODE (const_op0) == INTEGER_CST
4548 && (complain & tf_warning))
4549 maybe_warn_shift_overflow (location, const_op0, const_op1);
4551 /* Avoid converting op1 to result_type later. */
4552 converted = 1;
4554 break;
4556 case RROTATE_EXPR:
4557 case LROTATE_EXPR:
4558 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4560 result_type = type0;
4561 if (TREE_CODE (op1) == INTEGER_CST)
4563 if (tree_int_cst_lt (op1, integer_zero_node))
4565 if (complain & tf_warning)
4566 warning (0, (code == LROTATE_EXPR)
4567 ? G_("left rotate count is negative")
4568 : G_("right rotate count is negative"));
4570 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4572 if (complain & tf_warning)
4573 warning (0, (code == LROTATE_EXPR)
4574 ? G_("left rotate count >= width of type")
4575 : G_("right rotate count >= width of type"));
4578 /* Convert the shift-count to an integer, regardless of
4579 size of value being shifted. */
4580 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4581 op1 = cp_convert (integer_type_node, op1, complain);
4583 break;
4585 case EQ_EXPR:
4586 case NE_EXPR:
4587 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4588 goto vector_compare;
4589 if ((complain & tf_warning)
4590 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4591 warning (OPT_Wfloat_equal,
4592 "comparing floating point with == or != is unsafe");
4593 if ((complain & tf_warning)
4594 && ((TREE_CODE (orig_op0) == STRING_CST
4595 && !integer_zerop (cp_fully_fold (op1)))
4596 || (TREE_CODE (orig_op1) == STRING_CST
4597 && !integer_zerop (cp_fully_fold (op0)))))
4598 warning (OPT_Waddress, "comparison with string literal results "
4599 "in unspecified behavior");
4601 build_type = boolean_type_node;
4602 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4603 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4604 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4605 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4606 short_compare = 1;
4607 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4608 && null_ptr_cst_p (orig_op1))
4609 /* Handle, eg, (void*)0 (c++/43906), and more. */
4610 || (code0 == POINTER_TYPE
4611 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4613 if (TYPE_PTR_P (type1))
4614 result_type = composite_pointer_type (type0, type1, op0, op1,
4615 CPO_COMPARISON, complain);
4616 else
4617 result_type = type0;
4619 if (char_type_p (TREE_TYPE (orig_op1))
4620 && warning (OPT_Wpointer_compare,
4621 "comparison between pointer and zero character "
4622 "constant"))
4623 inform (input_location,
4624 "did you mean to dereference the pointer?");
4625 warn_for_null_address (location, op0, complain);
4627 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4628 && null_ptr_cst_p (orig_op0))
4629 /* Handle, eg, (void*)0 (c++/43906), and more. */
4630 || (code1 == POINTER_TYPE
4631 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4633 if (TYPE_PTR_P (type0))
4634 result_type = composite_pointer_type (type0, type1, op0, op1,
4635 CPO_COMPARISON, complain);
4636 else
4637 result_type = type1;
4639 if (char_type_p (TREE_TYPE (orig_op0))
4640 && warning (OPT_Wpointer_compare,
4641 "comparison between pointer and zero character "
4642 "constant"))
4643 inform (input_location,
4644 "did you mean to dereference the pointer?");
4645 warn_for_null_address (location, op1, complain);
4647 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4648 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4649 result_type = composite_pointer_type (type0, type1, op0, op1,
4650 CPO_COMPARISON, complain);
4651 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4652 /* One of the operands must be of nullptr_t type. */
4653 result_type = TREE_TYPE (nullptr_node);
4654 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4656 result_type = type0;
4657 if (complain & tf_error)
4658 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4659 else
4660 return error_mark_node;
4662 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4664 result_type = type1;
4665 if (complain & tf_error)
4666 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4667 else
4668 return error_mark_node;
4670 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4672 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4673 == ptrmemfunc_vbit_in_delta)
4675 tree pfn0, delta0, e1, e2;
4677 if (TREE_SIDE_EFFECTS (op0))
4678 op0 = save_expr (op0);
4680 pfn0 = pfn_from_ptrmemfunc (op0);
4681 delta0 = delta_from_ptrmemfunc (op0);
4682 e1 = cp_build_binary_op (location,
4683 EQ_EXPR,
4684 pfn0,
4685 build_zero_cst (TREE_TYPE (pfn0)),
4686 complain);
4687 e2 = cp_build_binary_op (location,
4688 BIT_AND_EXPR,
4689 delta0,
4690 integer_one_node,
4691 complain);
4693 if (complain & tf_warning)
4694 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4696 e2 = cp_build_binary_op (location,
4697 EQ_EXPR, e2, integer_zero_node,
4698 complain);
4699 op0 = cp_build_binary_op (location,
4700 TRUTH_ANDIF_EXPR, e1, e2,
4701 complain);
4702 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4704 else
4706 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4707 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4709 result_type = TREE_TYPE (op0);
4711 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4712 return cp_build_binary_op (location, code, op1, op0, complain);
4713 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4715 tree type;
4716 /* E will be the final comparison. */
4717 tree e;
4718 /* E1 and E2 are for scratch. */
4719 tree e1;
4720 tree e2;
4721 tree pfn0;
4722 tree pfn1;
4723 tree delta0;
4724 tree delta1;
4726 type = composite_pointer_type (type0, type1, op0, op1,
4727 CPO_COMPARISON, complain);
4729 if (!same_type_p (TREE_TYPE (op0), type))
4730 op0 = cp_convert_and_check (type, op0, complain);
4731 if (!same_type_p (TREE_TYPE (op1), type))
4732 op1 = cp_convert_and_check (type, op1, complain);
4734 if (op0 == error_mark_node || op1 == error_mark_node)
4735 return error_mark_node;
4737 if (TREE_SIDE_EFFECTS (op0))
4738 op0 = save_expr (op0);
4739 if (TREE_SIDE_EFFECTS (op1))
4740 op1 = save_expr (op1);
4742 pfn0 = pfn_from_ptrmemfunc (op0);
4743 pfn0 = cp_fully_fold (pfn0);
4744 /* Avoid -Waddress warnings (c++/64877). */
4745 if (TREE_CODE (pfn0) == ADDR_EXPR)
4746 TREE_NO_WARNING (pfn0) = 1;
4747 pfn1 = pfn_from_ptrmemfunc (op1);
4748 pfn1 = cp_fully_fold (pfn1);
4749 delta0 = delta_from_ptrmemfunc (op0);
4750 delta1 = delta_from_ptrmemfunc (op1);
4751 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4752 == ptrmemfunc_vbit_in_delta)
4754 /* We generate:
4756 (op0.pfn == op1.pfn
4757 && ((op0.delta == op1.delta)
4758 || (!op0.pfn && op0.delta & 1 == 0
4759 && op1.delta & 1 == 0))
4761 The reason for the `!op0.pfn' bit is that a NULL
4762 pointer-to-member is any member with a zero PFN and
4763 LSB of the DELTA field is 0. */
4765 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4766 delta0,
4767 integer_one_node,
4768 complain);
4769 e1 = cp_build_binary_op (location,
4770 EQ_EXPR, e1, integer_zero_node,
4771 complain);
4772 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4773 delta1,
4774 integer_one_node,
4775 complain);
4776 e2 = cp_build_binary_op (location,
4777 EQ_EXPR, e2, integer_zero_node,
4778 complain);
4779 e1 = cp_build_binary_op (location,
4780 TRUTH_ANDIF_EXPR, e2, e1,
4781 complain);
4782 e2 = cp_build_binary_op (location, EQ_EXPR,
4783 pfn0,
4784 build_zero_cst (TREE_TYPE (pfn0)),
4785 complain);
4786 e2 = cp_build_binary_op (location,
4787 TRUTH_ANDIF_EXPR, e2, e1, complain);
4788 e1 = cp_build_binary_op (location,
4789 EQ_EXPR, delta0, delta1, complain);
4790 e1 = cp_build_binary_op (location,
4791 TRUTH_ORIF_EXPR, e1, e2, complain);
4793 else
4795 /* We generate:
4797 (op0.pfn == op1.pfn
4798 && (!op0.pfn || op0.delta == op1.delta))
4800 The reason for the `!op0.pfn' bit is that a NULL
4801 pointer-to-member is any member with a zero PFN; the
4802 DELTA field is unspecified. */
4804 e1 = cp_build_binary_op (location,
4805 EQ_EXPR, delta0, delta1, complain);
4806 e2 = cp_build_binary_op (location,
4807 EQ_EXPR,
4808 pfn0,
4809 build_zero_cst (TREE_TYPE (pfn0)),
4810 complain);
4811 e1 = cp_build_binary_op (location,
4812 TRUTH_ORIF_EXPR, e1, e2, complain);
4814 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4815 e = cp_build_binary_op (location,
4816 TRUTH_ANDIF_EXPR, e2, e1, complain);
4817 if (code == EQ_EXPR)
4818 return e;
4819 return cp_build_binary_op (location,
4820 EQ_EXPR, e, integer_zero_node, complain);
4822 else
4824 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4825 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4826 type1));
4827 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4828 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4829 type0));
4832 break;
4834 case MAX_EXPR:
4835 case MIN_EXPR:
4836 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4837 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4838 shorten = 1;
4839 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4840 result_type = composite_pointer_type (type0, type1, op0, op1,
4841 CPO_COMPARISON, complain);
4842 break;
4844 case LE_EXPR:
4845 case GE_EXPR:
4846 case LT_EXPR:
4847 case GT_EXPR:
4848 if (TREE_CODE (orig_op0) == STRING_CST
4849 || TREE_CODE (orig_op1) == STRING_CST)
4851 if (complain & tf_warning)
4852 warning (OPT_Waddress, "comparison with string literal results "
4853 "in unspecified behavior");
4856 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4858 vector_compare:
4859 tree intt;
4860 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4861 TREE_TYPE (type1))
4862 && !vector_types_compatible_elements_p (type0, type1))
4864 if (complain & tf_error)
4866 error_at (location, "comparing vectors with different "
4867 "element types");
4868 inform (location, "operand types are %qT and %qT",
4869 type0, type1);
4871 return error_mark_node;
4874 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4876 if (complain & tf_error)
4878 error_at (location, "comparing vectors with different "
4879 "number of elements");
4880 inform (location, "operand types are %qT and %qT",
4881 type0, type1);
4883 return error_mark_node;
4886 /* It's not precisely specified how the usual arithmetic
4887 conversions apply to the vector types. Here, we use
4888 the unsigned type if one of the operands is signed and
4889 the other one is unsigned. */
4890 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
4892 if (!TYPE_UNSIGNED (type0))
4893 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
4894 else
4895 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
4896 warning_at (location, OPT_Wsign_compare, "comparison between "
4897 "types %qT and %qT", type0, type1);
4900 /* Always construct signed integer vector type. */
4901 intt = c_common_type_for_size (GET_MODE_BITSIZE
4902 (TYPE_MODE (TREE_TYPE (type0))), 0);
4903 if (!intt)
4905 if (complain & tf_error)
4906 error_at (location, "could not find an integer type "
4907 "of the same size as %qT", TREE_TYPE (type0));
4908 return error_mark_node;
4910 result_type = build_opaque_vector_type (intt,
4911 TYPE_VECTOR_SUBPARTS (type0));
4912 converted = 1;
4913 return build_vec_cmp (resultcode, result_type, op0, op1);
4915 build_type = boolean_type_node;
4916 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4917 || code0 == ENUMERAL_TYPE)
4918 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4919 || code1 == ENUMERAL_TYPE))
4920 short_compare = 1;
4921 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4922 result_type = composite_pointer_type (type0, type1, op0, op1,
4923 CPO_COMPARISON, complain);
4924 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
4926 result_type = type0;
4927 if (extra_warnings && (complain & tf_warning))
4928 warning (OPT_Wextra,
4929 "ordered comparison of pointer with integer zero");
4931 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
4933 result_type = type1;
4934 if (extra_warnings && (complain & tf_warning))
4935 warning (OPT_Wextra,
4936 "ordered comparison of pointer with integer zero");
4938 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4939 /* One of the operands must be of nullptr_t type. */
4940 result_type = TREE_TYPE (nullptr_node);
4941 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4943 result_type = type0;
4944 if (complain & tf_error)
4945 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4946 else
4947 return error_mark_node;
4949 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4951 result_type = type1;
4952 if (complain & tf_error)
4953 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4954 else
4955 return error_mark_node;
4957 break;
4959 case UNORDERED_EXPR:
4960 case ORDERED_EXPR:
4961 case UNLT_EXPR:
4962 case UNLE_EXPR:
4963 case UNGT_EXPR:
4964 case UNGE_EXPR:
4965 case UNEQ_EXPR:
4966 build_type = integer_type_node;
4967 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4969 if (complain & tf_error)
4970 error ("unordered comparison on non-floating point argument");
4971 return error_mark_node;
4973 common = 1;
4974 break;
4976 default:
4977 break;
4980 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4981 || code0 == ENUMERAL_TYPE)
4982 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4983 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4984 arithmetic_types_p = 1;
4985 else
4987 arithmetic_types_p = 0;
4988 /* Vector arithmetic is only allowed when both sides are vectors. */
4989 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4991 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4992 || !vector_types_compatible_elements_p (type0, type1))
4994 if (complain & tf_error)
4996 /* "location" already embeds the locations of the
4997 operands, so we don't need to add them separately
4998 to richloc. */
4999 rich_location richloc (line_table, location);
5000 binary_op_error (&richloc, code, type0, type1);
5002 return error_mark_node;
5004 arithmetic_types_p = 1;
5007 /* Determine the RESULT_TYPE, if it is not already known. */
5008 if (!result_type
5009 && arithmetic_types_p
5010 && (shorten || common || short_compare))
5012 result_type = cp_common_type (type0, type1);
5013 if (complain & tf_warning)
5014 do_warn_double_promotion (result_type, type0, type1,
5015 "implicit conversion from %qT to %qT "
5016 "to match other operand of binary "
5017 "expression",
5018 location);
5021 if (!result_type)
5023 if (complain & tf_error)
5024 error_at (location,
5025 "invalid operands of types %qT and %qT to binary %qO",
5026 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5027 return error_mark_node;
5030 /* If we're in a template, the only thing we need to know is the
5031 RESULT_TYPE. */
5032 if (processing_template_decl)
5034 /* Since the middle-end checks the type when doing a build2, we
5035 need to build the tree in pieces. This built tree will never
5036 get out of the front-end as we replace it when instantiating
5037 the template. */
5038 tree tmp = build2 (resultcode,
5039 build_type ? build_type : result_type,
5040 NULL_TREE, op1);
5041 TREE_OPERAND (tmp, 0) = op0;
5042 return tmp;
5045 if (arithmetic_types_p)
5047 bool first_complex = (code0 == COMPLEX_TYPE);
5048 bool second_complex = (code1 == COMPLEX_TYPE);
5049 int none_complex = (!first_complex && !second_complex);
5051 /* Adapted from patch for c/24581. */
5052 if (first_complex != second_complex
5053 && (code == PLUS_EXPR
5054 || code == MINUS_EXPR
5055 || code == MULT_EXPR
5056 || (code == TRUNC_DIV_EXPR && first_complex))
5057 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5058 && flag_signed_zeros)
5060 /* An operation on mixed real/complex operands must be
5061 handled specially, but the language-independent code can
5062 more easily optimize the plain complex arithmetic if
5063 -fno-signed-zeros. */
5064 tree real_type = TREE_TYPE (result_type);
5065 tree real, imag;
5066 if (first_complex)
5068 if (TREE_TYPE (op0) != result_type)
5069 op0 = cp_convert_and_check (result_type, op0, complain);
5070 if (TREE_TYPE (op1) != real_type)
5071 op1 = cp_convert_and_check (real_type, op1, complain);
5073 else
5075 if (TREE_TYPE (op0) != real_type)
5076 op0 = cp_convert_and_check (real_type, op0, complain);
5077 if (TREE_TYPE (op1) != result_type)
5078 op1 = cp_convert_and_check (result_type, op1, complain);
5080 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5081 return error_mark_node;
5082 if (first_complex)
5084 op0 = save_expr (op0);
5085 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5086 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5087 switch (code)
5089 case MULT_EXPR:
5090 case TRUNC_DIV_EXPR:
5091 op1 = save_expr (op1);
5092 imag = build2 (resultcode, real_type, imag, op1);
5093 /* Fall through. */
5094 case PLUS_EXPR:
5095 case MINUS_EXPR:
5096 real = build2 (resultcode, real_type, real, op1);
5097 break;
5098 default:
5099 gcc_unreachable();
5102 else
5104 op1 = save_expr (op1);
5105 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5106 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5107 switch (code)
5109 case MULT_EXPR:
5110 op0 = save_expr (op0);
5111 imag = build2 (resultcode, real_type, op0, imag);
5112 /* Fall through. */
5113 case PLUS_EXPR:
5114 real = build2 (resultcode, real_type, op0, real);
5115 break;
5116 case MINUS_EXPR:
5117 real = build2 (resultcode, real_type, op0, real);
5118 imag = build1 (NEGATE_EXPR, real_type, imag);
5119 break;
5120 default:
5121 gcc_unreachable();
5124 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5125 return result;
5128 /* For certain operations (which identify themselves by shorten != 0)
5129 if both args were extended from the same smaller type,
5130 do the arithmetic in that type and then extend.
5132 shorten !=0 and !=1 indicates a bitwise operation.
5133 For them, this optimization is safe only if
5134 both args are zero-extended or both are sign-extended.
5135 Otherwise, we might change the result.
5136 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5137 but calculated in (unsigned short) it would be (unsigned short)-1. */
5139 if (shorten && none_complex)
5141 orig_type = result_type;
5142 final_type = result_type;
5143 result_type = shorten_binary_op (result_type, op0, op1,
5144 shorten == -1);
5147 /* Comparison operations are shortened too but differently.
5148 They identify themselves by setting short_compare = 1. */
5150 if (short_compare)
5152 /* We call shorten_compare only for diagnostic-reason. */
5153 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5154 xresult_type = result_type;
5155 enum tree_code xresultcode = resultcode;
5156 shorten_compare (location, &xop0, &xop1, &xresult_type,
5157 &xresultcode);
5160 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5161 && warn_sign_compare
5162 /* Do not warn until the template is instantiated; we cannot
5163 bound the ranges of the arguments until that point. */
5164 && !processing_template_decl
5165 && (complain & tf_warning)
5166 && c_inhibit_evaluation_warnings == 0
5167 /* Even unsigned enum types promote to signed int. We don't
5168 want to issue -Wsign-compare warnings for this case. */
5169 && !enum_cast_to_int (orig_op0)
5170 && !enum_cast_to_int (orig_op1))
5172 tree oop0 = maybe_constant_value (orig_op0);
5173 tree oop1 = maybe_constant_value (orig_op1);
5175 if (TREE_CODE (oop0) != INTEGER_CST)
5176 oop0 = cp_fully_fold (orig_op0);
5177 if (TREE_CODE (oop1) != INTEGER_CST)
5178 oop1 = cp_fully_fold (orig_op1);
5179 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5180 result_type, resultcode);
5184 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5185 Then the expression will be built.
5186 It will be given type FINAL_TYPE if that is nonzero;
5187 otherwise, it will be given type RESULT_TYPE. */
5188 if (! converted)
5190 if (TREE_TYPE (op0) != result_type)
5191 op0 = cp_convert_and_check (result_type, op0, complain);
5192 if (TREE_TYPE (op1) != result_type)
5193 op1 = cp_convert_and_check (result_type, op1, complain);
5195 if (op0 == error_mark_node || op1 == error_mark_node)
5196 return error_mark_node;
5199 if (build_type == NULL_TREE)
5200 build_type = result_type;
5202 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
5203 | SANITIZE_FLOAT_DIVIDE))
5204 && !processing_template_decl
5205 && do_ubsan_in_current_function ()
5206 && (doing_div_or_mod || doing_shift))
5208 /* OP0 and/or OP1 might have side-effects. */
5209 op0 = cp_save_expr (op0);
5210 op1 = cp_save_expr (op1);
5211 op0 = fold_non_dependent_expr (op0);
5212 op1 = fold_non_dependent_expr (op1);
5213 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
5214 | SANITIZE_FLOAT_DIVIDE)))
5216 /* For diagnostics we want to use the promoted types without
5217 shorten_binary_op. So convert the arguments to the
5218 original result_type. */
5219 tree cop0 = op0;
5220 tree cop1 = op1;
5221 if (orig_type != NULL && result_type != orig_type)
5223 cop0 = cp_convert (orig_type, op0, complain);
5224 cop1 = cp_convert (orig_type, op1, complain);
5226 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5228 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5229 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5232 result = build2_loc (location, resultcode, build_type, op0, op1);
5233 if (final_type != 0)
5234 result = cp_convert (final_type, result, complain);
5236 if (instrument_expr != NULL)
5237 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5238 instrument_expr, result);
5240 if (!processing_template_decl)
5242 op0 = cp_fully_fold (op0);
5243 /* Only consider the second argument if the first isn't overflowed. */
5244 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5245 return result;
5246 op1 = cp_fully_fold (op1);
5247 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5248 return result;
5250 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5251 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5252 return result;
5254 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5255 if (TREE_OVERFLOW_P (result_ovl))
5256 overflow_warning (location, result_ovl);
5258 return result;
5261 /* Build a VEC_PERM_EXPR.
5262 This is a simple wrapper for c_build_vec_perm_expr. */
5263 tree
5264 build_x_vec_perm_expr (location_t loc,
5265 tree arg0, tree arg1, tree arg2,
5266 tsubst_flags_t complain)
5268 tree orig_arg0 = arg0;
5269 tree orig_arg1 = arg1;
5270 tree orig_arg2 = arg2;
5271 if (processing_template_decl)
5273 if (type_dependent_expression_p (arg0)
5274 || type_dependent_expression_p (arg1)
5275 || type_dependent_expression_p (arg2))
5276 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5277 arg0 = build_non_dependent_expr (arg0);
5278 if (arg1)
5279 arg1 = build_non_dependent_expr (arg1);
5280 arg2 = build_non_dependent_expr (arg2);
5282 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5283 if (processing_template_decl && exp != error_mark_node)
5284 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5285 orig_arg1, orig_arg2);
5286 return exp;
5289 /* Return a tree for the sum or difference (RESULTCODE says which)
5290 of pointer PTROP and integer INTOP. */
5292 static tree
5293 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5294 tree intop, tsubst_flags_t complain)
5296 tree res_type = TREE_TYPE (ptrop);
5298 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5299 in certain circumstance (when it's valid to do so). So we need
5300 to make sure it's complete. We don't need to check here, if we
5301 can actually complete it at all, as those checks will be done in
5302 pointer_int_sum() anyway. */
5303 complete_type (TREE_TYPE (res_type));
5305 return pointer_int_sum (loc, resultcode, ptrop,
5306 intop, complain & tf_warning_or_error);
5309 /* Return a tree for the difference of pointers OP0 and OP1.
5310 The resulting tree has type int. */
5312 static tree
5313 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5314 tsubst_flags_t complain)
5316 tree result;
5317 tree restype = ptrdiff_type_node;
5318 tree target_type = TREE_TYPE (ptrtype);
5320 if (!complete_type_or_else (target_type, NULL_TREE))
5321 return error_mark_node;
5323 if (VOID_TYPE_P (target_type))
5325 if (complain & tf_error)
5326 permerror (loc, "ISO C++ forbids using pointer of "
5327 "type %<void *%> in subtraction");
5328 else
5329 return error_mark_node;
5331 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5333 if (complain & tf_error)
5334 permerror (loc, "ISO C++ forbids using pointer to "
5335 "a function in subtraction");
5336 else
5337 return error_mark_node;
5339 if (TREE_CODE (target_type) == METHOD_TYPE)
5341 if (complain & tf_error)
5342 permerror (loc, "ISO C++ forbids using pointer to "
5343 "a method in subtraction");
5344 else
5345 return error_mark_node;
5348 /* First do the subtraction as integers;
5349 then drop through to build the divide operator. */
5351 op0 = cp_build_binary_op (loc,
5352 MINUS_EXPR,
5353 cp_convert (restype, op0, complain),
5354 cp_convert (restype, op1, complain),
5355 complain);
5357 /* This generates an error if op1 is a pointer to an incomplete type. */
5358 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5360 if (complain & tf_error)
5361 error_at (loc, "invalid use of a pointer to an incomplete type in "
5362 "pointer arithmetic");
5363 else
5364 return error_mark_node;
5367 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5369 if (complain & tf_error)
5370 error_at (loc, "arithmetic on pointer to an empty aggregate");
5371 else
5372 return error_mark_node;
5375 op1 = (TYPE_PTROB_P (ptrtype)
5376 ? size_in_bytes_loc (loc, target_type)
5377 : integer_one_node);
5379 /* Do the division. */
5381 result = build2_loc (loc, EXACT_DIV_EXPR, restype, op0,
5382 cp_convert (restype, op1, complain));
5383 return result;
5386 /* Construct and perhaps optimize a tree representation
5387 for a unary operation. CODE, a tree_code, specifies the operation
5388 and XARG is the operand. */
5390 tree
5391 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5392 tsubst_flags_t complain)
5394 tree orig_expr = xarg;
5395 tree exp;
5396 int ptrmem = 0;
5397 tree overload = NULL_TREE;
5399 if (processing_template_decl)
5401 if (type_dependent_expression_p (xarg))
5402 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5404 xarg = build_non_dependent_expr (xarg);
5407 exp = NULL_TREE;
5409 /* [expr.unary.op] says:
5411 The address of an object of incomplete type can be taken.
5413 (And is just the ordinary address operator, not an overloaded
5414 "operator &".) However, if the type is a template
5415 specialization, we must complete the type at this point so that
5416 an overloaded "operator &" will be available if required. */
5417 if (code == ADDR_EXPR
5418 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5419 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5420 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5421 || (TREE_CODE (xarg) == OFFSET_REF)))
5422 /* Don't look for a function. */;
5423 else
5424 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5425 NULL_TREE, &overload, complain);
5427 if (!exp && code == ADDR_EXPR)
5429 if (is_overloaded_fn (xarg))
5431 tree fn = get_first_fn (xarg);
5432 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5434 if (complain & tf_error)
5435 error (DECL_CONSTRUCTOR_P (fn)
5436 ? G_("taking address of constructor %qE")
5437 : G_("taking address of destructor %qE"),
5438 xarg.get_value ());
5439 return error_mark_node;
5443 /* A pointer to member-function can be formed only by saying
5444 &X::mf. */
5445 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5446 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5448 if (TREE_CODE (xarg) != OFFSET_REF
5449 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5451 if (complain & tf_error)
5453 error ("invalid use of %qE to form a "
5454 "pointer-to-member-function", xarg.get_value ());
5455 if (TREE_CODE (xarg) != OFFSET_REF)
5456 inform (input_location, " a qualified-id is required");
5458 return error_mark_node;
5460 else
5462 if (complain & tf_error)
5463 error ("parentheses around %qE cannot be used to form a"
5464 " pointer-to-member-function",
5465 xarg.get_value ());
5466 else
5467 return error_mark_node;
5468 PTRMEM_OK_P (xarg) = 1;
5472 if (TREE_CODE (xarg) == OFFSET_REF)
5474 ptrmem = PTRMEM_OK_P (xarg);
5476 if (!ptrmem && !flag_ms_extensions
5477 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5479 /* A single non-static member, make sure we don't allow a
5480 pointer-to-member. */
5481 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5482 TREE_OPERAND (xarg, 0),
5483 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5484 PTRMEM_OK_P (xarg) = ptrmem;
5488 exp = cp_build_addr_expr_strict (xarg, complain);
5491 if (processing_template_decl && exp != error_mark_node)
5493 if (overload != NULL_TREE)
5494 return (build_min_non_dep_op_overload
5495 (code, exp, overload, orig_expr, integer_zero_node));
5497 exp = build_min_non_dep (code, exp, orig_expr,
5498 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5500 if (TREE_CODE (exp) == ADDR_EXPR)
5501 PTRMEM_OK_P (exp) = ptrmem;
5502 return exp;
5505 /* Construct and perhaps optimize a tree representation
5506 for __builtin_addressof operation. ARG specifies the operand. */
5508 tree
5509 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5511 tree orig_expr = arg;
5513 if (processing_template_decl)
5515 if (type_dependent_expression_p (arg))
5516 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5518 arg = build_non_dependent_expr (arg);
5521 tree exp = cp_build_addr_expr_strict (arg, complain);
5523 if (processing_template_decl && exp != error_mark_node)
5524 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5525 return exp;
5528 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5529 constants, where a null value is represented by an INTEGER_CST of
5530 -1. */
5532 tree
5533 cp_truthvalue_conversion (tree expr)
5535 tree type = TREE_TYPE (expr);
5536 if (TYPE_PTR_OR_PTRMEM_P (type)
5537 /* Avoid ICE on invalid use of non-static member function. */
5538 || TREE_CODE (expr) == FUNCTION_DECL)
5539 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, 1);
5540 else
5541 return c_common_truthvalue_conversion (input_location, expr);
5544 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5546 tree
5547 condition_conversion (tree expr)
5549 tree t;
5550 if (processing_template_decl)
5551 return expr;
5552 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5553 tf_warning_or_error, LOOKUP_NORMAL);
5554 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5555 return t;
5558 /* Returns the address of T. This function will fold away
5559 ADDR_EXPR of INDIRECT_REF. */
5561 tree
5562 build_address (tree t)
5564 if (error_operand_p (t) || !cxx_mark_addressable (t))
5565 return error_mark_node;
5566 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5567 t = build_fold_addr_expr (t);
5568 if (TREE_CODE (t) != ADDR_EXPR)
5569 t = rvalue (t);
5570 return t;
5573 /* Return a NOP_EXPR converting EXPR to TYPE. */
5575 tree
5576 build_nop (tree type, tree expr)
5578 if (type == error_mark_node || error_operand_p (expr))
5579 return expr;
5580 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5583 /* Take the address of ARG, whatever that means under C++ semantics.
5584 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5585 and class rvalues as well.
5587 Nothing should call this function directly; instead, callers should use
5588 cp_build_addr_expr or cp_build_addr_expr_strict. */
5590 static tree
5591 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5593 tree argtype;
5594 tree val;
5596 if (!arg || error_operand_p (arg))
5597 return error_mark_node;
5599 arg = mark_lvalue_use (arg);
5600 argtype = lvalue_type (arg);
5602 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5604 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5605 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5607 /* They're trying to take the address of a unique non-static
5608 member function. This is ill-formed (except in MS-land),
5609 but let's try to DTRT.
5610 Note: We only handle unique functions here because we don't
5611 want to complain if there's a static overload; non-unique
5612 cases will be handled by instantiate_type. But we need to
5613 handle this case here to allow casts on the resulting PMF.
5614 We could defer this in non-MS mode, but it's easier to give
5615 a useful error here. */
5617 /* Inside constant member functions, the `this' pointer
5618 contains an extra const qualifier. TYPE_MAIN_VARIANT
5619 is used here to remove this const from the diagnostics
5620 and the created OFFSET_REF. */
5621 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5622 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5623 if (!mark_used (fn, complain) && !(complain & tf_error))
5624 return error_mark_node;
5626 if (! flag_ms_extensions)
5628 tree name = DECL_NAME (fn);
5629 if (!(complain & tf_error))
5630 return error_mark_node;
5631 else if (current_class_type
5632 && TREE_OPERAND (arg, 0) == current_class_ref)
5633 /* An expression like &memfn. */
5634 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5635 " or parenthesized non-static member function to form"
5636 " a pointer to member function. Say %<&%T::%D%>",
5637 base, name);
5638 else
5639 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5640 " function to form a pointer to member function."
5641 " Say %<&%T::%D%>",
5642 base, name);
5644 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5647 /* Uninstantiated types are all functions. Taking the
5648 address of a function is a no-op, so just return the
5649 argument. */
5650 if (type_unknown_p (arg))
5651 return build1 (ADDR_EXPR, unknown_type_node, arg);
5653 if (TREE_CODE (arg) == OFFSET_REF)
5654 /* We want a pointer to member; bypass all the code for actually taking
5655 the address of something. */
5656 goto offset_ref;
5658 /* Anything not already handled and not a true memory reference
5659 is an error. */
5660 if (TREE_CODE (argtype) != FUNCTION_TYPE
5661 && TREE_CODE (argtype) != METHOD_TYPE)
5663 cp_lvalue_kind kind = lvalue_kind (arg);
5664 if (kind == clk_none)
5666 if (complain & tf_error)
5667 lvalue_error (input_location, lv_addressof);
5668 return error_mark_node;
5670 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5672 if (!(complain & tf_error))
5673 return error_mark_node;
5674 if (kind & clk_class)
5675 /* Make this a permerror because we used to accept it. */
5676 permerror (input_location, "taking address of temporary");
5677 else
5678 error ("taking address of xvalue (rvalue reference)");
5682 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5684 tree type = build_pointer_type (TREE_TYPE (argtype));
5685 arg = build1 (CONVERT_EXPR, type, arg);
5686 return arg;
5688 else if (pedantic && DECL_MAIN_P (arg))
5690 /* ARM $3.4 */
5691 /* Apparently a lot of autoconf scripts for C++ packages do this,
5692 so only complain if -Wpedantic. */
5693 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5694 pedwarn (input_location, OPT_Wpedantic,
5695 "ISO C++ forbids taking address of function %<::main%>");
5696 else if (flag_pedantic_errors)
5697 return error_mark_node;
5700 /* Let &* cancel out to simplify resulting code. */
5701 if (INDIRECT_REF_P (arg))
5703 arg = TREE_OPERAND (arg, 0);
5704 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5706 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5707 arg = build1 (CONVERT_EXPR, type, arg);
5709 else
5710 /* Don't let this be an lvalue. */
5711 arg = rvalue (arg);
5712 return arg;
5715 /* ??? Cope with user tricks that amount to offsetof. */
5716 if (TREE_CODE (argtype) != FUNCTION_TYPE
5717 && TREE_CODE (argtype) != METHOD_TYPE
5718 && argtype != unknown_type_node
5719 && (val = get_base_address (arg))
5720 && COMPLETE_TYPE_P (TREE_TYPE (val))
5721 && INDIRECT_REF_P (val)
5722 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5724 tree type = build_pointer_type (argtype);
5725 return fold_convert (type, fold_offsetof_1 (arg));
5728 /* Handle complex lvalues (when permitted)
5729 by reduction to simpler cases. */
5730 val = unary_complex_lvalue (ADDR_EXPR, arg);
5731 if (val != 0)
5732 return val;
5734 switch (TREE_CODE (arg))
5736 CASE_CONVERT:
5737 case FLOAT_EXPR:
5738 case FIX_TRUNC_EXPR:
5739 /* We should have handled this above in the lvalue_kind check. */
5740 gcc_unreachable ();
5741 break;
5743 case BASELINK:
5744 arg = BASELINK_FUNCTIONS (arg);
5745 /* Fall through. */
5747 case OVERLOAD:
5748 arg = OVL_CURRENT (arg);
5749 break;
5751 case OFFSET_REF:
5752 offset_ref:
5753 /* Turn a reference to a non-static data member into a
5754 pointer-to-member. */
5756 tree type;
5757 tree t;
5759 gcc_assert (PTRMEM_OK_P (arg));
5761 t = TREE_OPERAND (arg, 1);
5762 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5764 if (complain & tf_error)
5765 error ("cannot create pointer to reference member %qD", t);
5766 return error_mark_node;
5769 type = build_ptrmem_type (context_for_name_lookup (t),
5770 TREE_TYPE (t));
5771 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5772 return t;
5775 default:
5776 break;
5779 if (argtype != error_mark_node)
5780 argtype = build_pointer_type (argtype);
5782 if (bitfield_p (arg))
5784 if (complain & tf_error)
5785 error ("attempt to take address of bit-field");
5786 return error_mark_node;
5789 /* In a template, we are processing a non-dependent expression
5790 so we can just form an ADDR_EXPR with the correct type. */
5791 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5793 val = build_address (arg);
5794 if (TREE_CODE (arg) == OFFSET_REF)
5795 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5797 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5799 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5801 /* We can only get here with a single static member
5802 function. */
5803 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5804 && DECL_STATIC_FUNCTION_P (fn));
5805 if (!mark_used (fn, complain) && !(complain & tf_error))
5806 return error_mark_node;
5807 val = build_address (fn);
5808 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5809 /* Do not lose object's side effects. */
5810 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5811 TREE_OPERAND (arg, 0), val);
5813 else
5815 tree object = TREE_OPERAND (arg, 0);
5816 tree field = TREE_OPERAND (arg, 1);
5817 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5818 (TREE_TYPE (object), decl_type_context (field)));
5819 val = build_address (arg);
5822 if (TYPE_PTR_P (argtype)
5823 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5825 build_ptrmemfunc_type (argtype);
5826 val = build_ptrmemfunc (argtype, val, 0,
5827 /*c_cast_p=*/false,
5828 complain);
5831 return val;
5834 /* Take the address of ARG if it has one, even if it's an rvalue. */
5836 tree
5837 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5839 return cp_build_addr_expr_1 (arg, 0, complain);
5842 /* Take the address of ARG, but only if it's an lvalue. */
5844 static tree
5845 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5847 return cp_build_addr_expr_1 (arg, 1, complain);
5850 /* C++: Must handle pointers to members.
5852 Perhaps type instantiation should be extended to handle conversion
5853 from aggregates to types we don't yet know we want? (Or are those
5854 cases typically errors which should be reported?)
5856 NOCONVERT suppresses the default promotions (such as from short to int). */
5858 tree
5859 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
5860 tsubst_flags_t complain)
5862 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5863 tree arg = xarg;
5864 location_t location = EXPR_LOC_OR_LOC (arg, input_location);
5865 tree argtype = 0;
5866 const char *errstring = NULL;
5867 tree val;
5868 const char *invalid_op_diag;
5870 if (!arg || error_operand_p (arg))
5871 return error_mark_node;
5873 if ((invalid_op_diag
5874 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5875 ? CONVERT_EXPR
5876 : code),
5877 TREE_TYPE (xarg))))
5879 if (complain & tf_error)
5880 error (invalid_op_diag);
5881 return error_mark_node;
5884 switch (code)
5886 case UNARY_PLUS_EXPR:
5887 case NEGATE_EXPR:
5889 int flags = WANT_ARITH | WANT_ENUM;
5890 /* Unary plus (but not unary minus) is allowed on pointers. */
5891 if (code == UNARY_PLUS_EXPR)
5892 flags |= WANT_POINTER;
5893 arg = build_expr_type_conversion (flags, arg, true);
5894 if (!arg)
5895 errstring = (code == NEGATE_EXPR
5896 ? _("wrong type argument to unary minus")
5897 : _("wrong type argument to unary plus"));
5898 else
5900 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5901 arg = cp_perform_integral_promotions (arg, complain);
5903 /* Make sure the result is not an lvalue: a unary plus or minus
5904 expression is always a rvalue. */
5905 arg = rvalue (arg);
5908 break;
5910 case BIT_NOT_EXPR:
5911 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5913 code = CONJ_EXPR;
5914 if (!noconvert)
5916 arg = cp_default_conversion (arg, complain);
5917 if (arg == error_mark_node)
5918 return error_mark_node;
5921 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5922 | WANT_VECTOR_OR_COMPLEX,
5923 arg, true)))
5924 errstring = _("wrong type argument to bit-complement");
5925 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5927 /* Warn if the expression has boolean value. */
5928 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
5929 && warning_at (location, OPT_Wbool_operation,
5930 "%<~%> on an expression of type bool"))
5931 inform (location, "did you mean to use logical not (%<!%>)?");
5932 arg = cp_perform_integral_promotions (arg, complain);
5934 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
5935 arg = mark_rvalue_use (arg);
5936 break;
5938 case ABS_EXPR:
5939 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5940 errstring = _("wrong type argument to abs");
5941 else if (!noconvert)
5943 arg = cp_default_conversion (arg, complain);
5944 if (arg == error_mark_node)
5945 return error_mark_node;
5947 break;
5949 case CONJ_EXPR:
5950 /* Conjugating a real value is a no-op, but allow it anyway. */
5951 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5952 errstring = _("wrong type argument to conjugation");
5953 else if (!noconvert)
5955 arg = cp_default_conversion (arg, complain);
5956 if (arg == error_mark_node)
5957 return error_mark_node;
5959 break;
5961 case TRUTH_NOT_EXPR:
5962 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5963 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5964 build_zero_cst (TREE_TYPE (arg)), complain);
5965 arg = perform_implicit_conversion (boolean_type_node, arg,
5966 complain);
5967 val = invert_truthvalue_loc (input_location, arg);
5968 if (arg != error_mark_node)
5969 return val;
5970 errstring = _("in argument to unary !");
5971 break;
5973 case NOP_EXPR:
5974 break;
5976 case REALPART_EXPR:
5977 case IMAGPART_EXPR:
5978 arg = build_real_imag_expr (input_location, code, arg);
5979 return arg;
5981 case PREINCREMENT_EXPR:
5982 case POSTINCREMENT_EXPR:
5983 case PREDECREMENT_EXPR:
5984 case POSTDECREMENT_EXPR:
5985 /* Handle complex lvalues (when permitted)
5986 by reduction to simpler cases. */
5988 val = unary_complex_lvalue (code, arg);
5989 if (val != 0)
5990 return val;
5992 arg = mark_lvalue_use (arg);
5994 /* Increment or decrement the real part of the value,
5995 and don't change the imaginary part. */
5996 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5998 tree real, imag;
6000 arg = cp_stabilize_reference (arg);
6001 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6002 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6003 real = cp_build_unary_op (code, real, true, complain);
6004 if (real == error_mark_node || imag == error_mark_node)
6005 return error_mark_node;
6006 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6007 real, imag);
6010 /* Report invalid types. */
6012 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6013 arg, true)))
6015 if (code == PREINCREMENT_EXPR)
6016 errstring = _("no pre-increment operator for type");
6017 else if (code == POSTINCREMENT_EXPR)
6018 errstring = _("no post-increment operator for type");
6019 else if (code == PREDECREMENT_EXPR)
6020 errstring = _("no pre-decrement operator for type");
6021 else
6022 errstring = _("no post-decrement operator for type");
6023 break;
6025 else if (arg == error_mark_node)
6026 return error_mark_node;
6028 /* Report something read-only. */
6030 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6031 || TREE_READONLY (arg))
6033 if (complain & tf_error)
6034 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6035 || code == POSTINCREMENT_EXPR)
6036 ? lv_increment : lv_decrement));
6037 else
6038 return error_mark_node;
6042 tree inc;
6043 tree declared_type = unlowered_expr_type (arg);
6045 argtype = TREE_TYPE (arg);
6047 /* ARM $5.2.5 last annotation says this should be forbidden. */
6048 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6050 if (complain & tf_error)
6051 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6052 ? G_("ISO C++ forbids incrementing an enum")
6053 : G_("ISO C++ forbids decrementing an enum"));
6054 else
6055 return error_mark_node;
6058 /* Compute the increment. */
6060 if (TYPE_PTR_P (argtype))
6062 tree type = complete_type (TREE_TYPE (argtype));
6064 if (!COMPLETE_OR_VOID_TYPE_P (type))
6066 if (complain & tf_error)
6067 error (((code == PREINCREMENT_EXPR
6068 || code == POSTINCREMENT_EXPR))
6069 ? G_("cannot increment a pointer to incomplete type %qT")
6070 : G_("cannot decrement a pointer to incomplete type %qT"),
6071 TREE_TYPE (argtype));
6072 else
6073 return error_mark_node;
6075 else if (!TYPE_PTROB_P (argtype))
6077 if (complain & tf_error)
6078 pedwarn (input_location, OPT_Wpointer_arith,
6079 (code == PREINCREMENT_EXPR
6080 || code == POSTINCREMENT_EXPR)
6081 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6082 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6083 argtype);
6084 else
6085 return error_mark_node;
6088 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6090 else
6091 inc = VECTOR_TYPE_P (argtype)
6092 ? build_one_cst (argtype)
6093 : integer_one_node;
6095 inc = cp_convert (argtype, inc, complain);
6097 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6098 need to ask Objective-C to build the increment or decrement
6099 expression for it. */
6100 if (objc_is_property_ref (arg))
6101 return objc_build_incr_expr_for_property_ref (input_location, code,
6102 arg, inc);
6104 /* Complain about anything else that is not a true lvalue. */
6105 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6106 || code == POSTINCREMENT_EXPR)
6107 ? lv_increment : lv_decrement),
6108 complain))
6109 return error_mark_node;
6111 /* Forbid using -- or ++ in C++17 on `bool'. */
6112 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6114 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6116 if (complain & tf_error)
6117 error ("use of an operand of type %qT in %<operator--%> "
6118 "is forbidden", boolean_type_node);
6119 return error_mark_node;
6121 else
6123 if (cxx_dialect >= cxx1z)
6125 if (complain & tf_error)
6126 error ("use of an operand of type %qT in "
6127 "%<operator++%> is forbidden in C++1z",
6128 boolean_type_node);
6129 return error_mark_node;
6131 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6132 else if (!in_system_header_at (input_location))
6133 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6134 "in %<operator++%> is deprecated",
6135 boolean_type_node);
6137 val = boolean_increment (code, arg);
6139 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6140 /* An rvalue has no cv-qualifiers. */
6141 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6142 else
6143 val = build2 (code, TREE_TYPE (arg), arg, inc);
6145 TREE_SIDE_EFFECTS (val) = 1;
6146 return val;
6149 case ADDR_EXPR:
6150 /* Note that this operation never does default_conversion
6151 regardless of NOCONVERT. */
6152 return cp_build_addr_expr (arg, complain);
6154 default:
6155 break;
6158 if (!errstring)
6160 if (argtype == 0)
6161 argtype = TREE_TYPE (arg);
6162 return build1 (code, argtype, arg);
6165 if (complain & tf_error)
6166 error ("%s", errstring);
6167 return error_mark_node;
6170 /* Hook for the c-common bits that build a unary op. */
6171 tree
6172 build_unary_op (location_t /*location*/,
6173 enum tree_code code, tree xarg, bool noconvert)
6175 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6178 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6179 for certain kinds of expressions which are not really lvalues
6180 but which we can accept as lvalues.
6182 If ARG is not a kind of expression we can handle, return
6183 NULL_TREE. */
6185 tree
6186 unary_complex_lvalue (enum tree_code code, tree arg)
6188 /* Inside a template, making these kinds of adjustments is
6189 pointless; we are only concerned with the type of the
6190 expression. */
6191 if (processing_template_decl)
6192 return NULL_TREE;
6194 /* Handle (a, b) used as an "lvalue". */
6195 if (TREE_CODE (arg) == COMPOUND_EXPR)
6197 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6198 tf_warning_or_error);
6199 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6200 TREE_OPERAND (arg, 0), real_result);
6203 /* Handle (a ? b : c) used as an "lvalue". */
6204 if (TREE_CODE (arg) == COND_EXPR
6205 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6206 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6208 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6209 if (TREE_CODE (arg) == MODIFY_EXPR
6210 || TREE_CODE (arg) == PREINCREMENT_EXPR
6211 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6213 tree lvalue = TREE_OPERAND (arg, 0);
6214 if (TREE_SIDE_EFFECTS (lvalue))
6216 lvalue = cp_stabilize_reference (lvalue);
6217 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6218 lvalue, TREE_OPERAND (arg, 1));
6220 return unary_complex_lvalue
6221 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
6224 if (code != ADDR_EXPR)
6225 return NULL_TREE;
6227 /* Handle (a = b) used as an "lvalue" for `&'. */
6228 if (TREE_CODE (arg) == MODIFY_EXPR
6229 || TREE_CODE (arg) == INIT_EXPR)
6231 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6232 tf_warning_or_error);
6233 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6234 arg, real_result);
6235 TREE_NO_WARNING (arg) = 1;
6236 return arg;
6239 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6240 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6241 || TREE_CODE (arg) == OFFSET_REF)
6242 return NULL_TREE;
6244 /* We permit compiler to make function calls returning
6245 objects of aggregate type look like lvalues. */
6247 tree targ = arg;
6249 if (TREE_CODE (targ) == SAVE_EXPR)
6250 targ = TREE_OPERAND (targ, 0);
6252 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6254 if (TREE_CODE (arg) == SAVE_EXPR)
6255 targ = arg;
6256 else
6257 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6258 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6261 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6262 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6263 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6266 /* Don't let anything else be handled specially. */
6267 return NULL_TREE;
6270 /* Mark EXP saying that we need to be able to take the
6271 address of it; it should not be allocated in a register.
6272 Value is true if successful.
6274 C++: we do not allow `current_class_ptr' to be addressable. */
6276 bool
6277 cxx_mark_addressable (tree exp)
6279 tree x = exp;
6281 while (1)
6282 switch (TREE_CODE (x))
6284 case ADDR_EXPR:
6285 case COMPONENT_REF:
6286 case ARRAY_REF:
6287 case REALPART_EXPR:
6288 case IMAGPART_EXPR:
6289 x = TREE_OPERAND (x, 0);
6290 break;
6292 case PARM_DECL:
6293 if (x == current_class_ptr)
6295 error ("cannot take the address of %<this%>, which is an rvalue expression");
6296 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6297 return true;
6299 /* Fall through. */
6301 case VAR_DECL:
6302 /* Caller should not be trying to mark initialized
6303 constant fields addressable. */
6304 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6305 || DECL_IN_AGGR_P (x) == 0
6306 || TREE_STATIC (x)
6307 || DECL_EXTERNAL (x));
6308 /* Fall through. */
6310 case RESULT_DECL:
6311 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6312 && !DECL_ARTIFICIAL (x))
6314 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6316 error
6317 ("address of explicit register variable %qD requested", x);
6318 return false;
6320 else if (extra_warnings)
6321 warning
6322 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6324 TREE_ADDRESSABLE (x) = 1;
6325 return true;
6327 case CONST_DECL:
6328 case FUNCTION_DECL:
6329 TREE_ADDRESSABLE (x) = 1;
6330 return true;
6332 case CONSTRUCTOR:
6333 TREE_ADDRESSABLE (x) = 1;
6334 return true;
6336 case TARGET_EXPR:
6337 TREE_ADDRESSABLE (x) = 1;
6338 cxx_mark_addressable (TREE_OPERAND (x, 0));
6339 return true;
6341 default:
6342 return true;
6346 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6348 tree
6349 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6350 tsubst_flags_t complain)
6352 tree orig_ifexp = ifexp;
6353 tree orig_op1 = op1;
6354 tree orig_op2 = op2;
6355 tree expr;
6357 if (processing_template_decl)
6359 /* The standard says that the expression is type-dependent if
6360 IFEXP is type-dependent, even though the eventual type of the
6361 expression doesn't dependent on IFEXP. */
6362 if (type_dependent_expression_p (ifexp)
6363 /* As a GNU extension, the middle operand may be omitted. */
6364 || (op1 && type_dependent_expression_p (op1))
6365 || type_dependent_expression_p (op2))
6366 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6367 ifexp = build_non_dependent_expr (ifexp);
6368 if (op1)
6369 op1 = build_non_dependent_expr (op1);
6370 op2 = build_non_dependent_expr (op2);
6373 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6374 if (processing_template_decl && expr != error_mark_node)
6376 tree min = build_min_non_dep (COND_EXPR, expr,
6377 orig_ifexp, orig_op1, orig_op2);
6378 /* Remember that the result is an lvalue or xvalue. */
6379 if (glvalue_p (expr) && !glvalue_p (min))
6380 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6381 !lvalue_p (expr));
6382 expr = convert_from_reference (min);
6384 return expr;
6387 /* Given a list of expressions, return a compound expression
6388 that performs them all and returns the value of the last of them. */
6390 tree
6391 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6392 tsubst_flags_t complain)
6394 tree expr = TREE_VALUE (list);
6396 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6397 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6399 if (complain & tf_error)
6400 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6401 "list-initializer for non-class type must not "
6402 "be parenthesized");
6403 else
6404 return error_mark_node;
6407 if (TREE_CHAIN (list))
6409 if (complain & tf_error)
6410 switch (exp)
6412 case ELK_INIT:
6413 permerror (input_location, "expression list treated as compound "
6414 "expression in initializer");
6415 break;
6416 case ELK_MEM_INIT:
6417 permerror (input_location, "expression list treated as compound "
6418 "expression in mem-initializer");
6419 break;
6420 case ELK_FUNC_CAST:
6421 permerror (input_location, "expression list treated as compound "
6422 "expression in functional cast");
6423 break;
6424 default:
6425 gcc_unreachable ();
6427 else
6428 return error_mark_node;
6430 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6431 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6432 expr, TREE_VALUE (list), complain);
6435 return expr;
6438 /* Like build_x_compound_expr_from_list, but using a VEC. */
6440 tree
6441 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6442 tsubst_flags_t complain)
6444 if (vec_safe_is_empty (vec))
6445 return NULL_TREE;
6446 else if (vec->length () == 1)
6447 return (*vec)[0];
6448 else
6450 tree expr;
6451 unsigned int ix;
6452 tree t;
6454 if (msg != NULL)
6456 if (complain & tf_error)
6457 permerror (input_location,
6458 "%s expression list treated as compound expression",
6459 msg);
6460 else
6461 return error_mark_node;
6464 expr = (*vec)[0];
6465 for (ix = 1; vec->iterate (ix, &t); ++ix)
6466 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6467 t, complain);
6469 return expr;
6473 /* Handle overloading of the ',' operator when needed. */
6475 tree
6476 build_x_compound_expr (location_t loc, tree op1, tree op2,
6477 tsubst_flags_t complain)
6479 tree result;
6480 tree orig_op1 = op1;
6481 tree orig_op2 = op2;
6482 tree overload = NULL_TREE;
6484 if (processing_template_decl)
6486 if (type_dependent_expression_p (op1)
6487 || type_dependent_expression_p (op2))
6488 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6489 op1 = build_non_dependent_expr (op1);
6490 op2 = build_non_dependent_expr (op2);
6493 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6494 NULL_TREE, &overload, complain);
6495 if (!result)
6496 result = cp_build_compound_expr (op1, op2, complain);
6498 if (processing_template_decl && result != error_mark_node)
6500 if (overload != NULL_TREE)
6501 return (build_min_non_dep_op_overload
6502 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6504 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6507 return result;
6510 /* Like cp_build_compound_expr, but for the c-common bits. */
6512 tree
6513 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6515 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6518 /* Build a compound expression. */
6520 tree
6521 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6523 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6525 if (lhs == error_mark_node || rhs == error_mark_node)
6526 return error_mark_node;
6528 if (flag_cilkplus
6529 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6530 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6532 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6533 : EXPR_LOCATION (rhs));
6534 error_at (loc,
6535 "spawned function call cannot be part of a comma expression");
6536 return error_mark_node;
6539 if (TREE_CODE (rhs) == TARGET_EXPR)
6541 /* If the rhs is a TARGET_EXPR, then build the compound
6542 expression inside the target_expr's initializer. This
6543 helps the compiler to eliminate unnecessary temporaries. */
6544 tree init = TREE_OPERAND (rhs, 1);
6546 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6547 TREE_OPERAND (rhs, 1) = init;
6549 return rhs;
6552 if (type_unknown_p (rhs))
6554 if (complain & tf_error)
6555 error ("no context to resolve type of %qE", rhs);
6556 return error_mark_node;
6559 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6562 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6563 casts away constness. CAST gives the type of cast. Returns true
6564 if the cast is ill-formed, false if it is well-formed.
6566 ??? This function warns for casting away any qualifier not just
6567 const. We would like to specify exactly what qualifiers are casted
6568 away.
6571 static bool
6572 check_for_casting_away_constness (tree src_type, tree dest_type,
6573 enum tree_code cast, tsubst_flags_t complain)
6575 /* C-style casts are allowed to cast away constness. With
6576 WARN_CAST_QUAL, we still want to issue a warning. */
6577 if (cast == CAST_EXPR && !warn_cast_qual)
6578 return false;
6580 if (!casts_away_constness (src_type, dest_type, complain))
6581 return false;
6583 switch (cast)
6585 case CAST_EXPR:
6586 if (complain & tf_warning)
6587 warning (OPT_Wcast_qual,
6588 "cast from type %qT to type %qT casts away qualifiers",
6589 src_type, dest_type);
6590 return false;
6592 case STATIC_CAST_EXPR:
6593 if (complain & tf_error)
6594 error ("static_cast from type %qT to type %qT casts away qualifiers",
6595 src_type, dest_type);
6596 return true;
6598 case REINTERPRET_CAST_EXPR:
6599 if (complain & tf_error)
6600 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6601 src_type, dest_type);
6602 return true;
6604 default:
6605 gcc_unreachable();
6610 Warns if the cast from expression EXPR to type TYPE is useless.
6612 void
6613 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6615 if (warn_useless_cast
6616 && complain & tf_warning)
6618 if ((TREE_CODE (type) == REFERENCE_TYPE
6619 && (TYPE_REF_IS_RVALUE (type)
6620 ? xvalue_p (expr) : lvalue_p (expr))
6621 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6622 || same_type_p (TREE_TYPE (expr), type))
6623 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6627 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6628 (another pointer-to-member type in the same hierarchy) and return
6629 the converted expression. If ALLOW_INVERSE_P is permitted, a
6630 pointer-to-derived may be converted to pointer-to-base; otherwise,
6631 only the other direction is permitted. If C_CAST_P is true, this
6632 conversion is taking place as part of a C-style cast. */
6634 tree
6635 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6636 bool c_cast_p, tsubst_flags_t complain)
6638 if (TYPE_PTRDATAMEM_P (type))
6640 tree delta;
6642 if (TREE_CODE (expr) == PTRMEM_CST)
6643 expr = cplus_expand_constant (expr);
6644 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6645 TYPE_PTRMEM_CLASS_TYPE (type),
6646 allow_inverse_p,
6647 c_cast_p, complain);
6648 if (delta == error_mark_node)
6649 return error_mark_node;
6651 if (!integer_zerop (delta))
6653 tree cond, op1, op2;
6655 cond = cp_build_binary_op (input_location,
6656 EQ_EXPR,
6657 expr,
6658 build_int_cst (TREE_TYPE (expr), -1),
6659 complain);
6660 op1 = build_nop (ptrdiff_type_node, expr);
6661 op2 = cp_build_binary_op (input_location,
6662 PLUS_EXPR, op1, delta,
6663 complain);
6665 expr = fold_build3_loc (input_location,
6666 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6670 return build_nop (type, expr);
6672 else
6673 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6674 allow_inverse_p, c_cast_p, complain);
6677 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6678 this static_cast is being attempted as one of the possible casts
6679 allowed by a C-style cast. (In that case, accessibility of base
6680 classes is not considered, and it is OK to cast away
6681 constness.) Return the result of the cast. *VALID_P is set to
6682 indicate whether or not the cast was valid. */
6684 static tree
6685 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6686 bool *valid_p, tsubst_flags_t complain)
6688 tree intype;
6689 tree result;
6690 cp_lvalue_kind clk;
6692 /* Assume the cast is valid. */
6693 *valid_p = true;
6695 intype = unlowered_expr_type (expr);
6697 /* Save casted types in the function's used types hash table. */
6698 used_types_insert (type);
6700 /* [expr.static.cast]
6702 An lvalue of type "cv1 B", where B is a class type, can be cast
6703 to type "reference to cv2 D", where D is a class derived (clause
6704 _class.derived_) from B, if a valid standard conversion from
6705 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6706 same cv-qualification as, or greater cv-qualification than, cv1,
6707 and B is not a virtual base class of D. */
6708 /* We check this case before checking the validity of "TYPE t =
6709 EXPR;" below because for this case:
6711 struct B {};
6712 struct D : public B { D(const B&); };
6713 extern B& b;
6714 void f() { static_cast<const D&>(b); }
6716 we want to avoid constructing a new D. The standard is not
6717 completely clear about this issue, but our interpretation is
6718 consistent with other compilers. */
6719 if (TREE_CODE (type) == REFERENCE_TYPE
6720 && CLASS_TYPE_P (TREE_TYPE (type))
6721 && CLASS_TYPE_P (intype)
6722 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6723 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6724 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6725 build_pointer_type (TYPE_MAIN_VARIANT
6726 (TREE_TYPE (type))),
6727 complain)
6728 && (c_cast_p
6729 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6731 tree base;
6733 /* There is a standard conversion from "D*" to "B*" even if "B"
6734 is ambiguous or inaccessible. If this is really a
6735 static_cast, then we check both for inaccessibility and
6736 ambiguity. However, if this is a static_cast being performed
6737 because the user wrote a C-style cast, then accessibility is
6738 not considered. */
6739 base = lookup_base (TREE_TYPE (type), intype,
6740 c_cast_p ? ba_unique : ba_check,
6741 NULL, complain);
6742 expr = build_address (expr);
6744 if (flag_sanitize & SANITIZE_VPTR)
6746 tree ubsan_check
6747 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6748 intype, expr);
6749 if (ubsan_check)
6750 expr = ubsan_check;
6753 /* Convert from "B*" to "D*". This function will check that "B"
6754 is not a virtual base of "D". */
6755 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6756 complain);
6758 /* Convert the pointer to a reference -- but then remember that
6759 there are no expressions with reference type in C++.
6761 We call rvalue so that there's an actual tree code
6762 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6763 is a variable with the same type, the conversion would get folded
6764 away, leaving just the variable and causing lvalue_kind to give
6765 the wrong answer. */
6766 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6769 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6770 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6771 if (TREE_CODE (type) == REFERENCE_TYPE
6772 && TYPE_REF_IS_RVALUE (type)
6773 && (clk = real_lvalue_p (expr))
6774 && reference_related_p (TREE_TYPE (type), intype)
6775 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6777 if (clk == clk_ordinary)
6779 /* Handle the (non-bit-field) lvalue case here by casting to
6780 lvalue reference and then changing it to an rvalue reference.
6781 Casting an xvalue to rvalue reference will be handled by the
6782 main code path. */
6783 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6784 result = (perform_direct_initialization_if_possible
6785 (lref, expr, c_cast_p, complain));
6786 result = build1 (NON_LVALUE_EXPR, type, result);
6787 return convert_from_reference (result);
6789 else
6790 /* For a bit-field or packed field, bind to a temporary. */
6791 expr = rvalue (expr);
6794 /* Resolve overloaded address here rather than once in
6795 implicit_conversion and again in the inverse code below. */
6796 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6798 expr = instantiate_type (type, expr, complain);
6799 intype = TREE_TYPE (expr);
6802 /* [expr.static.cast]
6804 Any expression can be explicitly converted to type cv void. */
6805 if (VOID_TYPE_P (type))
6806 return convert_to_void (expr, ICV_CAST, complain);
6808 /* [class.abstract]
6809 An abstract class shall not be used ... as the type of an explicit
6810 conversion. */
6811 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6812 return error_mark_node;
6814 /* [expr.static.cast]
6816 An expression e can be explicitly converted to a type T using a
6817 static_cast of the form static_cast<T>(e) if the declaration T
6818 t(e);" is well-formed, for some invented temporary variable
6819 t. */
6820 result = perform_direct_initialization_if_possible (type, expr,
6821 c_cast_p, complain);
6822 if (result)
6824 result = convert_from_reference (result);
6826 /* [expr.static.cast]
6828 If T is a reference type, the result is an lvalue; otherwise,
6829 the result is an rvalue. */
6830 if (TREE_CODE (type) != REFERENCE_TYPE)
6832 result = rvalue (result);
6834 if (result == expr && SCALAR_TYPE_P (type))
6835 /* Leave some record of the cast. */
6836 result = build_nop (type, expr);
6838 return result;
6841 /* [expr.static.cast]
6843 The inverse of any standard conversion sequence (clause _conv_),
6844 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6845 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6846 (_conv.bool_) conversions, can be performed explicitly using
6847 static_cast subject to the restriction that the explicit
6848 conversion does not cast away constness (_expr.const.cast_), and
6849 the following additional rules for specific cases: */
6850 /* For reference, the conversions not excluded are: integral
6851 promotions, floating point promotion, integral conversions,
6852 floating point conversions, floating-integral conversions,
6853 pointer conversions, and pointer to member conversions. */
6854 /* DR 128
6856 A value of integral _or enumeration_ type can be explicitly
6857 converted to an enumeration type. */
6858 /* The effect of all that is that any conversion between any two
6859 types which are integral, floating, or enumeration types can be
6860 performed. */
6861 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6862 || SCALAR_FLOAT_TYPE_P (type))
6863 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6864 || SCALAR_FLOAT_TYPE_P (intype)))
6865 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6867 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6868 && CLASS_TYPE_P (TREE_TYPE (type))
6869 && CLASS_TYPE_P (TREE_TYPE (intype))
6870 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6871 (TREE_TYPE (intype))),
6872 build_pointer_type (TYPE_MAIN_VARIANT
6873 (TREE_TYPE (type))),
6874 complain))
6876 tree base;
6878 if (!c_cast_p
6879 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6880 complain))
6881 return error_mark_node;
6882 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6883 c_cast_p ? ba_unique : ba_check,
6884 NULL, complain);
6885 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6886 complain);
6888 if (flag_sanitize & SANITIZE_VPTR)
6890 tree ubsan_check
6891 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6892 intype, expr);
6893 if (ubsan_check)
6894 expr = ubsan_check;
6897 return cp_fold_convert (type, expr);
6900 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6901 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6903 tree c1;
6904 tree c2;
6905 tree t1;
6906 tree t2;
6908 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6909 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6911 if (TYPE_PTRDATAMEM_P (type))
6913 t1 = (build_ptrmem_type
6914 (c1,
6915 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6916 t2 = (build_ptrmem_type
6917 (c2,
6918 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6920 else
6922 t1 = intype;
6923 t2 = type;
6925 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6927 if (!c_cast_p
6928 && check_for_casting_away_constness (intype, type,
6929 STATIC_CAST_EXPR,
6930 complain))
6931 return error_mark_node;
6932 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6933 c_cast_p, complain);
6937 /* [expr.static.cast]
6939 An rvalue of type "pointer to cv void" can be explicitly
6940 converted to a pointer to object type. A value of type pointer
6941 to object converted to "pointer to cv void" and back to the
6942 original pointer type will have its original value. */
6943 if (TYPE_PTR_P (intype)
6944 && VOID_TYPE_P (TREE_TYPE (intype))
6945 && TYPE_PTROB_P (type))
6947 if (!c_cast_p
6948 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6949 complain))
6950 return error_mark_node;
6951 return build_nop (type, expr);
6954 *valid_p = false;
6955 return error_mark_node;
6958 /* Return an expression representing static_cast<TYPE>(EXPR). */
6960 tree
6961 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6963 tree result;
6964 bool valid_p;
6966 if (type == error_mark_node || expr == error_mark_node)
6967 return error_mark_node;
6969 if (processing_template_decl)
6971 expr = build_min (STATIC_CAST_EXPR, type, expr);
6972 /* We don't know if it will or will not have side effects. */
6973 TREE_SIDE_EFFECTS (expr) = 1;
6974 return convert_from_reference (expr);
6977 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6978 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6979 if (TREE_CODE (type) != REFERENCE_TYPE
6980 && TREE_CODE (expr) == NOP_EXPR
6981 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6982 expr = TREE_OPERAND (expr, 0);
6984 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6985 complain);
6986 if (valid_p)
6988 if (result != error_mark_node)
6989 maybe_warn_about_useless_cast (type, expr, complain);
6990 return result;
6993 if (complain & tf_error)
6994 error ("invalid static_cast from type %qT to type %qT",
6995 TREE_TYPE (expr), type);
6996 return error_mark_node;
6999 /* EXPR is an expression with member function or pointer-to-member
7000 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7001 not permitted by ISO C++, but we accept it in some modes. If we
7002 are not in one of those modes, issue a diagnostic. Return the
7003 converted expression. */
7005 tree
7006 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7008 tree intype;
7009 tree decl;
7011 intype = TREE_TYPE (expr);
7012 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7013 || TREE_CODE (intype) == METHOD_TYPE);
7015 if (!(complain & tf_warning_or_error))
7016 return error_mark_node;
7018 if (pedantic || warn_pmf2ptr)
7019 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7020 "converting from %qT to %qT", intype, type);
7022 if (TREE_CODE (intype) == METHOD_TYPE)
7023 expr = build_addr_func (expr, complain);
7024 else if (TREE_CODE (expr) == PTRMEM_CST)
7025 expr = build_address (PTRMEM_CST_MEMBER (expr));
7026 else
7028 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7029 decl = build_address (decl);
7030 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7033 if (expr == error_mark_node)
7034 return error_mark_node;
7036 return build_nop (type, expr);
7039 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7040 If C_CAST_P is true, this reinterpret cast is being done as part of
7041 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7042 indicate whether or not reinterpret_cast was valid. */
7044 static tree
7045 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7046 bool *valid_p, tsubst_flags_t complain)
7048 tree intype;
7050 /* Assume the cast is invalid. */
7051 if (valid_p)
7052 *valid_p = true;
7054 if (type == error_mark_node || error_operand_p (expr))
7055 return error_mark_node;
7057 intype = TREE_TYPE (expr);
7059 /* Save casted types in the function's used types hash table. */
7060 used_types_insert (type);
7062 /* [expr.reinterpret.cast]
7063 An lvalue expression of type T1 can be cast to the type
7064 "reference to T2" if an expression of type "pointer to T1" can be
7065 explicitly converted to the type "pointer to T2" using a
7066 reinterpret_cast. */
7067 if (TREE_CODE (type) == REFERENCE_TYPE)
7069 if (! lvalue_p (expr))
7071 if (complain & tf_error)
7072 error ("invalid cast of an rvalue expression of type "
7073 "%qT to type %qT",
7074 intype, type);
7075 return error_mark_node;
7078 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7079 "B" are related class types; the reinterpret_cast does not
7080 adjust the pointer. */
7081 if (TYPE_PTR_P (intype)
7082 && (complain & tf_warning)
7083 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7084 COMPARE_BASE | COMPARE_DERIVED)))
7085 warning (0, "casting %qT to %qT does not dereference pointer",
7086 intype, type);
7088 expr = cp_build_addr_expr (expr, complain);
7090 if (warn_strict_aliasing > 2)
7091 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
7093 if (expr != error_mark_node)
7094 expr = build_reinterpret_cast_1
7095 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7096 valid_p, complain);
7097 if (expr != error_mark_node)
7098 /* cp_build_indirect_ref isn't right for rvalue refs. */
7099 expr = convert_from_reference (fold_convert (type, expr));
7100 return expr;
7103 /* As a G++ extension, we consider conversions from member
7104 functions, and pointers to member functions to
7105 pointer-to-function and pointer-to-void types. If
7106 -Wno-pmf-conversions has not been specified,
7107 convert_member_func_to_ptr will issue an error message. */
7108 if ((TYPE_PTRMEMFUNC_P (intype)
7109 || TREE_CODE (intype) == METHOD_TYPE)
7110 && TYPE_PTR_P (type)
7111 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7112 || VOID_TYPE_P (TREE_TYPE (type))))
7113 return convert_member_func_to_ptr (type, expr, complain);
7115 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7116 array-to-pointer, and function-to-pointer conversions are
7117 performed. */
7118 expr = decay_conversion (expr, complain);
7120 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7121 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7122 if (TREE_CODE (expr) == NOP_EXPR
7123 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7124 expr = TREE_OPERAND (expr, 0);
7126 if (error_operand_p (expr))
7127 return error_mark_node;
7129 intype = TREE_TYPE (expr);
7131 /* [expr.reinterpret.cast]
7132 A pointer can be converted to any integral type large enough to
7133 hold it. ... A value of type std::nullptr_t can be converted to
7134 an integral type; the conversion has the same meaning and
7135 validity as a conversion of (void*)0 to the integral type. */
7136 if (CP_INTEGRAL_TYPE_P (type)
7137 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7139 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7141 if (complain & tf_error)
7142 permerror (input_location, "cast from %qT to %qT loses precision",
7143 intype, type);
7144 else
7145 return error_mark_node;
7147 if (NULLPTR_TYPE_P (intype))
7148 return build_int_cst (type, 0);
7150 /* [expr.reinterpret.cast]
7151 A value of integral or enumeration type can be explicitly
7152 converted to a pointer. */
7153 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7154 /* OK */
7156 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7157 || TYPE_PTR_OR_PTRMEM_P (type))
7158 && same_type_p (type, intype))
7159 /* DR 799 */
7160 return rvalue (expr);
7161 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7162 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7163 return build_nop (type, expr);
7164 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7165 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7167 tree sexpr = expr;
7169 if (!c_cast_p
7170 && check_for_casting_away_constness (intype, type,
7171 REINTERPRET_CAST_EXPR,
7172 complain))
7173 return error_mark_node;
7174 /* Warn about possible alignment problems. */
7175 if (STRICT_ALIGNMENT && warn_cast_align
7176 && (complain & tf_warning)
7177 && !VOID_TYPE_P (type)
7178 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7179 && COMPLETE_TYPE_P (TREE_TYPE (type))
7180 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7181 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
7182 warning (OPT_Wcast_align, "cast from %qT to %qT "
7183 "increases required alignment of target type", intype, type);
7185 /* We need to strip nops here, because the front end likes to
7186 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7187 STRIP_NOPS (sexpr);
7188 if (warn_strict_aliasing <= 2)
7189 strict_aliasing_warning (intype, type, sexpr);
7191 return build_nop (type, expr);
7193 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7194 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7196 if (complain & tf_warning)
7197 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7198 object pointer type or vice versa is conditionally-supported." */
7199 warning (OPT_Wconditionally_supported,
7200 "casting between pointer-to-function and pointer-to-object "
7201 "is conditionally-supported");
7202 return build_nop (type, expr);
7204 else if (VECTOR_TYPE_P (type))
7205 return convert_to_vector (type, expr);
7206 else if (VECTOR_TYPE_P (intype)
7207 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7208 return convert_to_integer_nofold (type, expr);
7209 else
7211 if (valid_p)
7212 *valid_p = false;
7213 if (complain & tf_error)
7214 error ("invalid cast from type %qT to type %qT", intype, type);
7215 return error_mark_node;
7218 return cp_convert (type, expr, complain);
7221 tree
7222 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7224 tree r;
7226 if (type == error_mark_node || expr == error_mark_node)
7227 return error_mark_node;
7229 if (processing_template_decl)
7231 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7233 if (!TREE_SIDE_EFFECTS (t)
7234 && type_dependent_expression_p (expr))
7235 /* There might turn out to be side effects inside expr. */
7236 TREE_SIDE_EFFECTS (t) = 1;
7237 return convert_from_reference (t);
7240 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7241 /*valid_p=*/NULL, complain);
7242 if (r != error_mark_node)
7243 maybe_warn_about_useless_cast (type, expr, complain);
7244 return r;
7247 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7248 return an appropriate expression. Otherwise, return
7249 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7250 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7251 performing a C-style cast, its value upon return will indicate
7252 whether or not the conversion succeeded. */
7254 static tree
7255 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7256 bool *valid_p)
7258 tree src_type;
7259 tree reference_type;
7261 /* Callers are responsible for handling error_mark_node as a
7262 destination type. */
7263 gcc_assert (dst_type != error_mark_node);
7264 /* In a template, callers should be building syntactic
7265 representations of casts, not using this machinery. */
7266 gcc_assert (!processing_template_decl);
7268 /* Assume the conversion is invalid. */
7269 if (valid_p)
7270 *valid_p = false;
7272 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7274 if (complain & tf_error)
7275 error ("invalid use of const_cast with type %qT, "
7276 "which is not a pointer, "
7277 "reference, nor a pointer-to-data-member type", dst_type);
7278 return error_mark_node;
7281 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7283 if (complain & tf_error)
7284 error ("invalid use of const_cast with type %qT, which is a pointer "
7285 "or reference to a function type", dst_type);
7286 return error_mark_node;
7289 /* Save casted types in the function's used types hash table. */
7290 used_types_insert (dst_type);
7292 src_type = TREE_TYPE (expr);
7293 /* Expressions do not really have reference types. */
7294 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7295 src_type = TREE_TYPE (src_type);
7297 /* [expr.const.cast]
7299 For two object types T1 and T2, if a pointer to T1 can be explicitly
7300 converted to the type "pointer to T2" using a const_cast, then the
7301 following conversions can also be made:
7303 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7304 type T2 using the cast const_cast<T2&>;
7306 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7307 type T2 using the cast const_cast<T2&&>; and
7309 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7310 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7312 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7314 reference_type = dst_type;
7315 if (!TYPE_REF_IS_RVALUE (dst_type)
7316 ? lvalue_p (expr)
7317 : obvalue_p (expr))
7318 /* OK. */;
7319 else
7321 if (complain & tf_error)
7322 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7323 src_type, dst_type);
7324 return error_mark_node;
7326 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7327 src_type = build_pointer_type (src_type);
7329 else
7331 reference_type = NULL_TREE;
7332 /* If the destination type is not a reference type, the
7333 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7334 conversions are performed. */
7335 src_type = type_decays_to (src_type);
7336 if (src_type == error_mark_node)
7337 return error_mark_node;
7340 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7342 if (comp_ptr_ttypes_const (dst_type, src_type))
7344 if (valid_p)
7346 *valid_p = true;
7347 /* This cast is actually a C-style cast. Issue a warning if
7348 the user is making a potentially unsafe cast. */
7349 check_for_casting_away_constness (src_type, dst_type,
7350 CAST_EXPR, complain);
7352 if (reference_type)
7354 expr = cp_build_addr_expr (expr, complain);
7355 if (expr == error_mark_node)
7356 return error_mark_node;
7357 expr = build_nop (reference_type, expr);
7358 return convert_from_reference (expr);
7360 else
7362 expr = decay_conversion (expr, complain);
7363 if (expr == error_mark_node)
7364 return error_mark_node;
7366 /* build_c_cast puts on a NOP_EXPR to make the result not an
7367 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7368 non-lvalue context. */
7369 if (TREE_CODE (expr) == NOP_EXPR
7370 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7371 expr = TREE_OPERAND (expr, 0);
7372 return build_nop (dst_type, expr);
7375 else if (valid_p
7376 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7377 TREE_TYPE (src_type)))
7378 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7379 complain);
7382 if (complain & tf_error)
7383 error ("invalid const_cast from type %qT to type %qT",
7384 src_type, dst_type);
7385 return error_mark_node;
7388 tree
7389 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7391 tree r;
7393 if (type == error_mark_node || error_operand_p (expr))
7394 return error_mark_node;
7396 if (processing_template_decl)
7398 tree t = build_min (CONST_CAST_EXPR, type, expr);
7400 if (!TREE_SIDE_EFFECTS (t)
7401 && type_dependent_expression_p (expr))
7402 /* There might turn out to be side effects inside expr. */
7403 TREE_SIDE_EFFECTS (t) = 1;
7404 return convert_from_reference (t);
7407 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7408 if (r != error_mark_node)
7409 maybe_warn_about_useless_cast (type, expr, complain);
7410 return r;
7413 /* Like cp_build_c_cast, but for the c-common bits. */
7415 tree
7416 build_c_cast (location_t /*loc*/, tree type, tree expr)
7418 return cp_build_c_cast (type, expr, tf_warning_or_error);
7421 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7422 preserve location information even for tree nodes that don't
7423 support it. */
7425 cp_expr
7426 build_c_cast (location_t loc, tree type, cp_expr expr)
7428 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7429 result.set_location (loc);
7430 return result;
7433 /* Build an expression representing an explicit C-style cast to type
7434 TYPE of expression EXPR. */
7436 tree
7437 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7439 tree value = expr;
7440 tree result;
7441 bool valid_p;
7443 if (type == error_mark_node || error_operand_p (expr))
7444 return error_mark_node;
7446 if (processing_template_decl)
7448 tree t = build_min (CAST_EXPR, type,
7449 tree_cons (NULL_TREE, value, NULL_TREE));
7450 /* We don't know if it will or will not have side effects. */
7451 TREE_SIDE_EFFECTS (t) = 1;
7452 return convert_from_reference (t);
7455 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7456 'Class') should always be retained, because this information aids
7457 in method lookup. */
7458 if (objc_is_object_ptr (type)
7459 && objc_is_object_ptr (TREE_TYPE (expr)))
7460 return build_nop (type, expr);
7462 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7463 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7464 if (TREE_CODE (type) != REFERENCE_TYPE
7465 && TREE_CODE (value) == NOP_EXPR
7466 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7467 value = TREE_OPERAND (value, 0);
7469 if (TREE_CODE (type) == ARRAY_TYPE)
7471 /* Allow casting from T1* to T2[] because Cfront allows it.
7472 NIHCL uses it. It is not valid ISO C++ however. */
7473 if (TYPE_PTR_P (TREE_TYPE (expr)))
7475 if (complain & tf_error)
7476 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7477 else
7478 return error_mark_node;
7479 type = build_pointer_type (TREE_TYPE (type));
7481 else
7483 if (complain & tf_error)
7484 error ("ISO C++ forbids casting to an array type %qT", type);
7485 return error_mark_node;
7489 if (TREE_CODE (type) == FUNCTION_TYPE
7490 || TREE_CODE (type) == METHOD_TYPE)
7492 if (complain & tf_error)
7493 error ("invalid cast to function type %qT", type);
7494 return error_mark_node;
7497 if (TYPE_PTR_P (type)
7498 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7499 /* Casting to an integer of smaller size is an error detected elsewhere. */
7500 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7501 /* Don't warn about converting any constant. */
7502 && !TREE_CONSTANT (value))
7503 warning_at (input_location, OPT_Wint_to_pointer_cast,
7504 "cast to pointer from integer of different size");
7506 /* A C-style cast can be a const_cast. */
7507 result = build_const_cast_1 (type, value, complain & tf_warning,
7508 &valid_p);
7509 if (valid_p)
7511 if (result != error_mark_node)
7512 maybe_warn_about_useless_cast (type, value, complain);
7513 return result;
7516 /* Or a static cast. */
7517 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7518 &valid_p, complain);
7519 /* Or a reinterpret_cast. */
7520 if (!valid_p)
7521 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7522 &valid_p, complain);
7523 /* The static_cast or reinterpret_cast may be followed by a
7524 const_cast. */
7525 if (valid_p
7526 /* A valid cast may result in errors if, for example, a
7527 conversion to an ambiguous base class is required. */
7528 && !error_operand_p (result))
7530 tree result_type;
7532 maybe_warn_about_useless_cast (type, value, complain);
7534 /* Non-class rvalues always have cv-unqualified type. */
7535 if (!CLASS_TYPE_P (type))
7536 type = TYPE_MAIN_VARIANT (type);
7537 result_type = TREE_TYPE (result);
7538 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7539 result_type = TYPE_MAIN_VARIANT (result_type);
7540 /* If the type of RESULT does not match TYPE, perform a
7541 const_cast to make it match. If the static_cast or
7542 reinterpret_cast succeeded, we will differ by at most
7543 cv-qualification, so the follow-on const_cast is guaranteed
7544 to succeed. */
7545 if (!same_type_p (non_reference (type), non_reference (result_type)))
7547 result = build_const_cast_1 (type, result, false, &valid_p);
7548 gcc_assert (valid_p);
7550 return result;
7553 return error_mark_node;
7556 /* For use from the C common bits. */
7557 tree
7558 build_modify_expr (location_t location,
7559 tree lhs, tree /*lhs_origtype*/,
7560 enum tree_code modifycode,
7561 location_t /*rhs_location*/, tree rhs,
7562 tree /*rhs_origtype*/)
7564 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7565 tf_warning_or_error);
7568 /* Build an assignment expression of lvalue LHS from value RHS.
7569 MODIFYCODE is the code for a binary operator that we use
7570 to combine the old value of LHS with RHS to get the new value.
7571 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7573 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7575 tree
7576 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7577 tree rhs, tsubst_flags_t complain)
7579 tree result = NULL_TREE;
7580 tree newrhs = rhs;
7581 tree lhstype = TREE_TYPE (lhs);
7582 tree olhs = lhs;
7583 tree olhstype = lhstype;
7584 bool plain_assign = (modifycode == NOP_EXPR);
7585 bool compound_side_effects_p = false;
7586 tree preeval = NULL_TREE;
7588 /* Avoid duplicate error messages from operands that had errors. */
7589 if (error_operand_p (lhs) || error_operand_p (rhs))
7590 return error_mark_node;
7592 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7594 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7595 compound_side_effects_p = true;
7596 lhs = TREE_OPERAND (lhs, 1);
7599 /* Handle control structure constructs used as "lvalues". Note that we
7600 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7601 switch (TREE_CODE (lhs))
7603 /* Handle --foo = 5; as these are valid constructs in C++. */
7604 case PREDECREMENT_EXPR:
7605 case PREINCREMENT_EXPR:
7606 if (compound_side_effects_p)
7607 newrhs = rhs = stabilize_expr (rhs, &preeval);
7608 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7609 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7610 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7611 TREE_OPERAND (lhs, 1));
7612 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7613 maybe_add_compound:
7614 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7615 and looked through the COMPOUND_EXPRs, readd them now around
7616 the resulting lhs. */
7617 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7619 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7620 tree *ptr = &TREE_OPERAND (lhs, 1);
7621 for (olhs = TREE_OPERAND (olhs, 1);
7622 TREE_CODE (olhs) == COMPOUND_EXPR;
7623 olhs = TREE_OPERAND (olhs, 1))
7625 *ptr = build2 (COMPOUND_EXPR, lhstype,
7626 TREE_OPERAND (olhs, 0), *ptr);
7627 ptr = &TREE_OPERAND (*ptr, 1);
7630 break;
7632 case MODIFY_EXPR:
7633 if (compound_side_effects_p)
7634 newrhs = rhs = stabilize_expr (rhs, &preeval);
7635 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7636 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7637 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7638 TREE_OPERAND (lhs, 1));
7639 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7640 goto maybe_add_compound;
7642 case MIN_EXPR:
7643 case MAX_EXPR:
7644 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7645 when neither operand has side-effects. */
7646 if (!lvalue_or_else (lhs, lv_assign, complain))
7647 return error_mark_node;
7649 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7650 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7652 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7653 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7654 boolean_type_node,
7655 TREE_OPERAND (lhs, 0),
7656 TREE_OPERAND (lhs, 1)),
7657 TREE_OPERAND (lhs, 0),
7658 TREE_OPERAND (lhs, 1));
7659 gcc_fallthrough ();
7661 /* Handle (a ? b : c) used as an "lvalue". */
7662 case COND_EXPR:
7664 /* Produce (a ? (b = rhs) : (c = rhs))
7665 except that the RHS goes through a save-expr
7666 so the code to compute it is only emitted once. */
7667 tree cond;
7669 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7671 if (complain & tf_error)
7672 error ("void value not ignored as it ought to be");
7673 return error_mark_node;
7676 rhs = stabilize_expr (rhs, &preeval);
7678 /* Check this here to avoid odd errors when trying to convert
7679 a throw to the type of the COND_EXPR. */
7680 if (!lvalue_or_else (lhs, lv_assign, complain))
7681 return error_mark_node;
7683 cond = build_conditional_expr
7684 (input_location, TREE_OPERAND (lhs, 0),
7685 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7686 modifycode, rhs, complain),
7687 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7688 modifycode, rhs, complain),
7689 complain);
7691 if (cond == error_mark_node)
7692 return cond;
7693 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7694 and looked through the COMPOUND_EXPRs, readd them now around
7695 the resulting cond before adding the preevaluated rhs. */
7696 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7698 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7699 TREE_OPERAND (olhs, 0), cond);
7700 tree *ptr = &TREE_OPERAND (cond, 1);
7701 for (olhs = TREE_OPERAND (olhs, 1);
7702 TREE_CODE (olhs) == COMPOUND_EXPR;
7703 olhs = TREE_OPERAND (olhs, 1))
7705 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7706 TREE_OPERAND (olhs, 0), *ptr);
7707 ptr = &TREE_OPERAND (*ptr, 1);
7710 /* Make sure the code to compute the rhs comes out
7711 before the split. */
7712 result = cond;
7713 goto ret;
7716 default:
7717 lhs = olhs;
7718 break;
7721 if (modifycode == INIT_EXPR)
7723 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7724 /* Do the default thing. */;
7725 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7727 /* Compound literal. */
7728 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7729 /* Call convert to generate an error; see PR 11063. */
7730 rhs = convert (lhstype, rhs);
7731 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7732 TREE_SIDE_EFFECTS (result) = 1;
7733 goto ret;
7735 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7736 /* Do the default thing. */;
7737 else
7739 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7740 result = build_special_member_call (lhs, complete_ctor_identifier,
7741 &rhs_vec, lhstype, LOOKUP_NORMAL,
7742 complain);
7743 release_tree_vector (rhs_vec);
7744 if (result == NULL_TREE)
7745 return error_mark_node;
7746 goto ret;
7749 else
7751 lhs = require_complete_type_sfinae (lhs, complain);
7752 if (lhs == error_mark_node)
7753 return error_mark_node;
7755 if (modifycode == NOP_EXPR)
7757 if (c_dialect_objc ())
7759 result = objc_maybe_build_modify_expr (lhs, rhs);
7760 if (result)
7761 goto ret;
7764 /* `operator=' is not an inheritable operator. */
7765 if (! MAYBE_CLASS_TYPE_P (lhstype))
7766 /* Do the default thing. */;
7767 else
7769 result = build_new_op (input_location, MODIFY_EXPR,
7770 LOOKUP_NORMAL, lhs, rhs,
7771 make_node (NOP_EXPR), /*overload=*/NULL,
7772 complain);
7773 if (result == NULL_TREE)
7774 return error_mark_node;
7775 goto ret;
7777 lhstype = olhstype;
7779 else
7781 tree init = NULL_TREE;
7783 /* A binary op has been requested. Combine the old LHS
7784 value with the RHS producing the value we should actually
7785 store into the LHS. */
7786 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7787 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7788 || MAYBE_CLASS_TYPE_P (lhstype)));
7790 /* Preevaluate the RHS to make sure its evaluation is complete
7791 before the lvalue-to-rvalue conversion of the LHS:
7793 [expr.ass] With respect to an indeterminately-sequenced
7794 function call, the operation of a compound assignment is a
7795 single evaluation. [ Note: Therefore, a function call shall
7796 not intervene between the lvalue-to-rvalue conversion and the
7797 side effect associated with any single compound assignment
7798 operator. -- end note ] */
7799 lhs = cp_stabilize_reference (lhs);
7800 rhs = rvalue (rhs);
7801 rhs = stabilize_expr (rhs, &init);
7802 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
7803 if (newrhs == error_mark_node)
7805 if (complain & tf_error)
7806 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7807 TREE_TYPE (lhs), TREE_TYPE (rhs));
7808 return error_mark_node;
7811 if (init)
7812 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7814 /* Now it looks like a plain assignment. */
7815 modifycode = NOP_EXPR;
7816 if (c_dialect_objc ())
7818 result = objc_maybe_build_modify_expr (lhs, newrhs);
7819 if (result)
7820 goto ret;
7823 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7824 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7827 /* The left-hand side must be an lvalue. */
7828 if (!lvalue_or_else (lhs, lv_assign, complain))
7829 return error_mark_node;
7831 /* Warn about modifying something that is `const'. Don't warn if
7832 this is initialization. */
7833 if (modifycode != INIT_EXPR
7834 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7835 /* Functions are not modifiable, even though they are
7836 lvalues. */
7837 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7838 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7839 /* If it's an aggregate and any field is const, then it is
7840 effectively const. */
7841 || (CLASS_TYPE_P (lhstype)
7842 && C_TYPE_FIELDS_READONLY (lhstype))))
7844 if (complain & tf_error)
7845 cxx_readonly_error (lhs, lv_assign);
7846 else
7847 return error_mark_node;
7850 /* If storing into a structure or union member, it may have been given a
7851 lowered bitfield type. We need to convert to the declared type first,
7852 so retrieve it now. */
7854 olhstype = unlowered_expr_type (lhs);
7856 /* Convert new value to destination type. */
7858 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7860 int from_array;
7862 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7864 if (modifycode != INIT_EXPR)
7866 if (complain & tf_error)
7867 error ("assigning to an array from an initializer list");
7868 return error_mark_node;
7870 if (check_array_initializer (lhs, lhstype, newrhs))
7871 return error_mark_node;
7872 newrhs = digest_init (lhstype, newrhs, complain);
7873 if (newrhs == error_mark_node)
7874 return error_mark_node;
7877 /* C++11 8.5/17: "If the destination type is an array of characters,
7878 an array of char16_t, an array of char32_t, or an array of wchar_t,
7879 and the initializer is a string literal...". */
7880 else if (TREE_CODE (newrhs) == STRING_CST
7881 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7882 && modifycode == INIT_EXPR)
7884 newrhs = digest_init (lhstype, newrhs, complain);
7885 if (newrhs == error_mark_node)
7886 return error_mark_node;
7889 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7890 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7892 if (complain & tf_error)
7893 error ("incompatible types in assignment of %qT to %qT",
7894 TREE_TYPE (rhs), lhstype);
7895 return error_mark_node;
7898 /* Allow array assignment in compiler-generated code. */
7899 else if (!current_function_decl
7900 || !DECL_DEFAULTED_FN (current_function_decl))
7902 /* This routine is used for both initialization and assignment.
7903 Make sure the diagnostic message differentiates the context. */
7904 if (complain & tf_error)
7906 if (modifycode == INIT_EXPR)
7907 error ("array used as initializer");
7908 else
7909 error ("invalid array assignment");
7911 return error_mark_node;
7914 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7915 ? 1 + (modifycode != INIT_EXPR): 0;
7916 result = build_vec_init (lhs, NULL_TREE, newrhs,
7917 /*explicit_value_init_p=*/false,
7918 from_array, complain);
7919 goto ret;
7922 if (modifycode == INIT_EXPR)
7923 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7924 LOOKUP_ONLYCONVERTING. */
7925 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7926 ICR_INIT, NULL_TREE, 0,
7927 complain);
7928 else
7929 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7930 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7932 if (!same_type_p (lhstype, olhstype))
7933 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7935 if (modifycode != INIT_EXPR)
7937 if (TREE_CODE (newrhs) == CALL_EXPR
7938 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7939 newrhs = build_cplus_new (lhstype, newrhs, complain);
7941 /* Can't initialize directly from a TARGET_EXPR, since that would
7942 cause the lhs to be constructed twice, and possibly result in
7943 accidental self-initialization. So we force the TARGET_EXPR to be
7944 expanded without a target. */
7945 if (TREE_CODE (newrhs) == TARGET_EXPR)
7946 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7947 TREE_OPERAND (newrhs, 0));
7950 if (newrhs == error_mark_node)
7951 return error_mark_node;
7953 if (c_dialect_objc () && flag_objc_gc)
7955 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7957 if (result)
7958 goto ret;
7961 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7962 lhstype, lhs, newrhs);
7964 TREE_SIDE_EFFECTS (result) = 1;
7965 if (!plain_assign)
7966 TREE_NO_WARNING (result) = 1;
7968 ret:
7969 if (preeval)
7970 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
7971 return result;
7974 cp_expr
7975 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7976 tree rhs, tsubst_flags_t complain)
7978 tree orig_lhs = lhs;
7979 tree orig_rhs = rhs;
7980 tree overload = NULL_TREE;
7981 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
7983 if (processing_template_decl)
7985 if (modifycode == NOP_EXPR
7986 || type_dependent_expression_p (lhs)
7987 || type_dependent_expression_p (rhs))
7988 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7989 build_min_nt_loc (loc, modifycode, NULL_TREE,
7990 NULL_TREE), rhs);
7992 lhs = build_non_dependent_expr (lhs);
7993 rhs = build_non_dependent_expr (rhs);
7996 if (modifycode != NOP_EXPR)
7998 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
7999 lhs, rhs, op, &overload, complain);
8000 if (rval)
8002 if (rval == error_mark_node)
8003 return rval;
8004 TREE_NO_WARNING (rval) = 1;
8005 if (processing_template_decl)
8007 if (overload != NULL_TREE)
8008 return (build_min_non_dep_op_overload
8009 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8011 return (build_min_non_dep
8012 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8014 return rval;
8017 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8020 /* Helper function for get_delta_difference which assumes FROM is a base
8021 class of TO. Returns a delta for the conversion of pointer-to-member
8022 of FROM to pointer-to-member of TO. If the conversion is invalid and
8023 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8024 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8025 If C_CAST_P is true, this conversion is taking place as part of a
8026 C-style cast. */
8028 static tree
8029 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8030 tsubst_flags_t complain)
8032 tree binfo;
8033 base_kind kind;
8035 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8036 &kind, complain);
8038 if (binfo == error_mark_node)
8040 if (!(complain & tf_error))
8041 return error_mark_node;
8043 error (" in pointer to member function conversion");
8044 return size_zero_node;
8046 else if (binfo)
8048 if (kind != bk_via_virtual)
8049 return BINFO_OFFSET (binfo);
8050 else
8051 /* FROM is a virtual base class of TO. Issue an error or warning
8052 depending on whether or not this is a reinterpret cast. */
8054 if (!(complain & tf_error))
8055 return error_mark_node;
8057 error ("pointer to member conversion via virtual base %qT",
8058 BINFO_TYPE (binfo_from_vbase (binfo)));
8060 return size_zero_node;
8063 else
8064 return NULL_TREE;
8067 /* Get difference in deltas for different pointer to member function
8068 types. If the conversion is invalid and tf_error is not set in
8069 COMPLAIN, returns error_mark_node, otherwise returns an integer
8070 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8071 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8072 conversions as well. If C_CAST_P is true this conversion is taking
8073 place as part of a C-style cast.
8075 Note that the naming of FROM and TO is kind of backwards; the return
8076 value is what we add to a TO in order to get a FROM. They are named
8077 this way because we call this function to find out how to convert from
8078 a pointer to member of FROM to a pointer to member of TO. */
8080 static tree
8081 get_delta_difference (tree from, tree to,
8082 bool allow_inverse_p,
8083 bool c_cast_p, tsubst_flags_t complain)
8085 tree result;
8087 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8088 /* Pointer to member of incomplete class is permitted*/
8089 result = size_zero_node;
8090 else
8091 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8093 if (result == error_mark_node)
8094 return error_mark_node;
8096 if (!result)
8098 if (!allow_inverse_p)
8100 if (!(complain & tf_error))
8101 return error_mark_node;
8103 error_not_base_type (from, to);
8104 error (" in pointer to member conversion");
8105 result = size_zero_node;
8107 else
8109 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8111 if (result == error_mark_node)
8112 return error_mark_node;
8114 if (result)
8115 result = size_diffop_loc (input_location,
8116 size_zero_node, result);
8117 else
8119 if (!(complain & tf_error))
8120 return error_mark_node;
8122 error_not_base_type (from, to);
8123 error (" in pointer to member conversion");
8124 result = size_zero_node;
8129 return convert_to_integer (ptrdiff_type_node, result);
8132 /* Return a constructor for the pointer-to-member-function TYPE using
8133 the other components as specified. */
8135 tree
8136 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8138 tree u = NULL_TREE;
8139 tree delta_field;
8140 tree pfn_field;
8141 vec<constructor_elt, va_gc> *v;
8143 /* Pull the FIELD_DECLs out of the type. */
8144 pfn_field = TYPE_FIELDS (type);
8145 delta_field = DECL_CHAIN (pfn_field);
8147 /* Make sure DELTA has the type we want. */
8148 delta = convert_and_check (input_location, delta_type_node, delta);
8150 /* Convert to the correct target type if necessary. */
8151 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8153 /* Finish creating the initializer. */
8154 vec_alloc (v, 2);
8155 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8156 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8157 u = build_constructor (type, v);
8158 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8159 TREE_STATIC (u) = (TREE_CONSTANT (u)
8160 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8161 != NULL_TREE)
8162 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8163 != NULL_TREE));
8164 return u;
8167 /* Build a constructor for a pointer to member function. It can be
8168 used to initialize global variables, local variable, or used
8169 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8170 want to be.
8172 If FORCE is nonzero, then force this conversion, even if
8173 we would rather not do it. Usually set when using an explicit
8174 cast. A C-style cast is being processed iff C_CAST_P is true.
8176 Return error_mark_node, if something goes wrong. */
8178 tree
8179 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8180 tsubst_flags_t complain)
8182 tree fn;
8183 tree pfn_type;
8184 tree to_type;
8186 if (error_operand_p (pfn))
8187 return error_mark_node;
8189 pfn_type = TREE_TYPE (pfn);
8190 to_type = build_ptrmemfunc_type (type);
8192 /* Handle multiple conversions of pointer to member functions. */
8193 if (TYPE_PTRMEMFUNC_P (pfn_type))
8195 tree delta = NULL_TREE;
8196 tree npfn = NULL_TREE;
8197 tree n;
8199 if (!force
8200 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8201 LOOKUP_NORMAL, complain))
8203 if (complain & tf_error)
8204 error ("invalid conversion to type %qT from type %qT",
8205 to_type, pfn_type);
8206 else
8207 return error_mark_node;
8210 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8211 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8212 force,
8213 c_cast_p, complain);
8214 if (n == error_mark_node)
8215 return error_mark_node;
8217 /* We don't have to do any conversion to convert a
8218 pointer-to-member to its own type. But, we don't want to
8219 just return a PTRMEM_CST if there's an explicit cast; that
8220 cast should make the expression an invalid template argument. */
8221 if (TREE_CODE (pfn) != PTRMEM_CST)
8223 if (same_type_p (to_type, pfn_type))
8224 return pfn;
8225 else if (integer_zerop (n))
8226 return build_reinterpret_cast (to_type, pfn,
8227 complain);
8230 if (TREE_SIDE_EFFECTS (pfn))
8231 pfn = save_expr (pfn);
8233 /* Obtain the function pointer and the current DELTA. */
8234 if (TREE_CODE (pfn) == PTRMEM_CST)
8235 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8236 else
8238 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8239 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8242 /* Just adjust the DELTA field. */
8243 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8244 (TREE_TYPE (delta), ptrdiff_type_node));
8245 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8246 n = cp_build_binary_op (input_location,
8247 LSHIFT_EXPR, n, integer_one_node,
8248 complain);
8249 delta = cp_build_binary_op (input_location,
8250 PLUS_EXPR, delta, n, complain);
8251 return build_ptrmemfunc1 (to_type, delta, npfn);
8254 /* Handle null pointer to member function conversions. */
8255 if (null_ptr_cst_p (pfn))
8257 pfn = cp_build_c_cast (type, pfn, complain);
8258 return build_ptrmemfunc1 (to_type,
8259 integer_zero_node,
8260 pfn);
8263 if (type_unknown_p (pfn))
8264 return instantiate_type (type, pfn, complain);
8266 fn = TREE_OPERAND (pfn, 0);
8267 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8268 /* In a template, we will have preserved the
8269 OFFSET_REF. */
8270 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8271 return make_ptrmem_cst (to_type, fn);
8274 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8275 given by CST.
8277 ??? There is no consistency as to the types returned for the above
8278 values. Some code acts as if it were a sizetype and some as if it were
8279 integer_type_node. */
8281 void
8282 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8284 tree type = TREE_TYPE (cst);
8285 tree fn = PTRMEM_CST_MEMBER (cst);
8286 tree ptr_class, fn_class;
8288 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8290 /* The class that the function belongs to. */
8291 fn_class = DECL_CONTEXT (fn);
8293 /* The class that we're creating a pointer to member of. */
8294 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8296 /* First, calculate the adjustment to the function's class. */
8297 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8298 /*c_cast_p=*/0, tf_warning_or_error);
8300 if (!DECL_VIRTUAL_P (fn))
8301 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8302 build_addr_func (fn, tf_warning_or_error));
8303 else
8305 /* If we're dealing with a virtual function, we have to adjust 'this'
8306 again, to point to the base which provides the vtable entry for
8307 fn; the call will do the opposite adjustment. */
8308 tree orig_class = DECL_CONTEXT (fn);
8309 tree binfo = binfo_or_else (orig_class, fn_class);
8310 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8311 *delta, BINFO_OFFSET (binfo));
8313 /* We set PFN to the vtable offset at which the function can be
8314 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8315 case delta is shifted left, and then incremented). */
8316 *pfn = DECL_VINDEX (fn);
8317 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8318 TYPE_SIZE_UNIT (vtable_entry_type));
8320 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8322 case ptrmemfunc_vbit_in_pfn:
8323 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8324 integer_one_node);
8325 break;
8327 case ptrmemfunc_vbit_in_delta:
8328 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8329 *delta, integer_one_node);
8330 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8331 *delta, integer_one_node);
8332 break;
8334 default:
8335 gcc_unreachable ();
8338 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8342 /* Return an expression for PFN from the pointer-to-member function
8343 given by T. */
8345 static tree
8346 pfn_from_ptrmemfunc (tree t)
8348 if (TREE_CODE (t) == PTRMEM_CST)
8350 tree delta;
8351 tree pfn;
8353 expand_ptrmemfunc_cst (t, &delta, &pfn);
8354 if (pfn)
8355 return pfn;
8358 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8361 /* Return an expression for DELTA from the pointer-to-member function
8362 given by T. */
8364 static tree
8365 delta_from_ptrmemfunc (tree t)
8367 if (TREE_CODE (t) == PTRMEM_CST)
8369 tree delta;
8370 tree pfn;
8372 expand_ptrmemfunc_cst (t, &delta, &pfn);
8373 if (delta)
8374 return delta;
8377 return build_ptrmemfunc_access_expr (t, delta_identifier);
8380 /* Convert value RHS to type TYPE as preparation for an assignment to
8381 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8382 implicit conversion is. If FNDECL is non-NULL, we are doing the
8383 conversion in order to pass the PARMNUMth argument of FNDECL.
8384 If FNDECL is NULL, we are doing the conversion in function pointer
8385 argument passing, conversion in initialization, etc. */
8387 static tree
8388 convert_for_assignment (tree type, tree rhs,
8389 impl_conv_rhs errtype, tree fndecl, int parmnum,
8390 tsubst_flags_t complain, int flags)
8392 tree rhstype;
8393 enum tree_code coder;
8395 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8396 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8397 rhs = TREE_OPERAND (rhs, 0);
8399 /* Handle [dcl.init.list] direct-list-initialization from
8400 single element of enumeration with a fixed underlying type. */
8401 if (is_direct_enum_init (type, rhs))
8403 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8404 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8405 rhs = cp_build_c_cast (type, elt, complain);
8406 else
8407 rhs = error_mark_node;
8410 rhstype = TREE_TYPE (rhs);
8411 coder = TREE_CODE (rhstype);
8413 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8414 && vector_types_convertible_p (type, rhstype, true))
8416 rhs = mark_rvalue_use (rhs);
8417 return convert (type, rhs);
8420 if (rhs == error_mark_node || rhstype == error_mark_node)
8421 return error_mark_node;
8422 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8423 return error_mark_node;
8425 /* The RHS of an assignment cannot have void type. */
8426 if (coder == VOID_TYPE)
8428 if (complain & tf_error)
8429 error ("void value not ignored as it ought to be");
8430 return error_mark_node;
8433 if (c_dialect_objc ())
8435 int parmno;
8436 tree selector;
8437 tree rname = fndecl;
8439 switch (errtype)
8441 case ICR_ASSIGN:
8442 parmno = -1;
8443 break;
8444 case ICR_INIT:
8445 parmno = -2;
8446 break;
8447 default:
8448 selector = objc_message_selector ();
8449 parmno = parmnum;
8450 if (selector && parmno > 1)
8452 rname = selector;
8453 parmno -= 1;
8457 if (objc_compare_types (type, rhstype, parmno, rname))
8459 rhs = mark_rvalue_use (rhs);
8460 return convert (type, rhs);
8464 /* [expr.ass]
8466 The expression is implicitly converted (clause _conv_) to the
8467 cv-unqualified type of the left operand.
8469 We allow bad conversions here because by the time we get to this point
8470 we are committed to doing the conversion. If we end up doing a bad
8471 conversion, convert_like will complain. */
8472 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8474 /* When -Wno-pmf-conversions is use, we just silently allow
8475 conversions from pointers-to-members to plain pointers. If
8476 the conversion doesn't work, cp_convert will complain. */
8477 if (!warn_pmf2ptr
8478 && TYPE_PTR_P (type)
8479 && TYPE_PTRMEMFUNC_P (rhstype))
8480 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8481 else
8483 if (complain & tf_error)
8485 /* If the right-hand side has unknown type, then it is an
8486 overloaded function. Call instantiate_type to get error
8487 messages. */
8488 if (rhstype == unknown_type_node)
8489 instantiate_type (type, rhs, tf_warning_or_error);
8490 else if (fndecl)
8491 error ("cannot convert %qT to %qT for argument %qP to %qD",
8492 rhstype, type, parmnum, fndecl);
8493 else
8494 switch (errtype)
8496 case ICR_DEFAULT_ARGUMENT:
8497 error ("cannot convert %qT to %qT in default argument",
8498 rhstype, type);
8499 break;
8500 case ICR_ARGPASS:
8501 error ("cannot convert %qT to %qT in argument passing",
8502 rhstype, type);
8503 break;
8504 case ICR_CONVERTING:
8505 error ("cannot convert %qT to %qT",
8506 rhstype, type);
8507 break;
8508 case ICR_INIT:
8509 error ("cannot convert %qT to %qT in initialization",
8510 rhstype, type);
8511 break;
8512 case ICR_RETURN:
8513 error ("cannot convert %qT to %qT in return",
8514 rhstype, type);
8515 break;
8516 case ICR_ASSIGN:
8517 error ("cannot convert %qT to %qT in assignment",
8518 rhstype, type);
8519 break;
8520 default:
8521 gcc_unreachable();
8523 if (TYPE_PTR_P (rhstype)
8524 && TYPE_PTR_P (type)
8525 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8526 && CLASS_TYPE_P (TREE_TYPE (type))
8527 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8528 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8529 (TREE_TYPE (rhstype))),
8530 "class type %qT is incomplete", TREE_TYPE (rhstype));
8532 return error_mark_node;
8535 if (warn_suggest_attribute_format)
8537 const enum tree_code codel = TREE_CODE (type);
8538 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8539 && coder == codel
8540 && check_missing_format_attribute (type, rhstype)
8541 && (complain & tf_warning))
8542 switch (errtype)
8544 case ICR_ARGPASS:
8545 case ICR_DEFAULT_ARGUMENT:
8546 if (fndecl)
8547 warning (OPT_Wsuggest_attribute_format,
8548 "parameter %qP of %qD might be a candidate "
8549 "for a format attribute", parmnum, fndecl);
8550 else
8551 warning (OPT_Wsuggest_attribute_format,
8552 "parameter might be a candidate "
8553 "for a format attribute");
8554 break;
8555 case ICR_CONVERTING:
8556 warning (OPT_Wsuggest_attribute_format,
8557 "target of conversion might be a candidate "
8558 "for a format attribute");
8559 break;
8560 case ICR_INIT:
8561 warning (OPT_Wsuggest_attribute_format,
8562 "target of initialization might be a candidate "
8563 "for a format attribute");
8564 break;
8565 case ICR_RETURN:
8566 warning (OPT_Wsuggest_attribute_format,
8567 "return type might be a candidate "
8568 "for a format attribute");
8569 break;
8570 case ICR_ASSIGN:
8571 warning (OPT_Wsuggest_attribute_format,
8572 "left-hand side of assignment might be a candidate "
8573 "for a format attribute");
8574 break;
8575 default:
8576 gcc_unreachable();
8580 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8581 does not. */
8582 if (warn_parentheses
8583 && TREE_CODE (type) == BOOLEAN_TYPE
8584 && TREE_CODE (rhs) == MODIFY_EXPR
8585 && !TREE_NO_WARNING (rhs)
8586 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8587 && (complain & tf_warning))
8589 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8591 warning_at (loc, OPT_Wparentheses,
8592 "suggest parentheses around assignment used as truth value");
8593 TREE_NO_WARNING (rhs) = 1;
8596 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8597 complain, flags);
8600 /* Convert RHS to be of type TYPE.
8601 If EXP is nonzero, it is the target of the initialization.
8602 ERRTYPE indicates what kind of error the implicit conversion is.
8604 Two major differences between the behavior of
8605 `convert_for_assignment' and `convert_for_initialization'
8606 are that references are bashed in the former, while
8607 copied in the latter, and aggregates are assigned in
8608 the former (operator=) while initialized in the
8609 latter (X(X&)).
8611 If using constructor make sure no conversion operator exists, if one does
8612 exist, an ambiguity exists. */
8614 tree
8615 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8616 impl_conv_rhs errtype, tree fndecl, int parmnum,
8617 tsubst_flags_t complain)
8619 enum tree_code codel = TREE_CODE (type);
8620 tree rhstype;
8621 enum tree_code coder;
8623 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8624 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8625 if (TREE_CODE (rhs) == NOP_EXPR
8626 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8627 && codel != REFERENCE_TYPE)
8628 rhs = TREE_OPERAND (rhs, 0);
8630 if (type == error_mark_node
8631 || rhs == error_mark_node
8632 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8633 return error_mark_node;
8635 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8637 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8638 && TREE_CODE (type) != ARRAY_TYPE
8639 && (TREE_CODE (type) != REFERENCE_TYPE
8640 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8641 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8642 && !TYPE_REFFN_P (type))
8643 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8644 rhs = decay_conversion (rhs, complain);
8646 rhstype = TREE_TYPE (rhs);
8647 coder = TREE_CODE (rhstype);
8649 if (coder == ERROR_MARK)
8650 return error_mark_node;
8652 /* We accept references to incomplete types, so we can
8653 return here before checking if RHS is of complete type. */
8655 if (codel == REFERENCE_TYPE)
8657 /* This should eventually happen in convert_arguments. */
8658 int savew = 0, savee = 0;
8660 if (fndecl)
8661 savew = warningcount + werrorcount, savee = errorcount;
8662 rhs = initialize_reference (type, rhs, flags, complain);
8664 if (fndecl
8665 && (warningcount + werrorcount > savew || errorcount > savee))
8666 inform (DECL_SOURCE_LOCATION (fndecl),
8667 "in passing argument %P of %qD", parmnum, fndecl);
8669 return rhs;
8672 if (exp != 0)
8673 exp = require_complete_type_sfinae (exp, complain);
8674 if (exp == error_mark_node)
8675 return error_mark_node;
8677 rhstype = non_reference (rhstype);
8679 type = complete_type (type);
8681 if (DIRECT_INIT_EXPR_P (type, rhs))
8682 /* Don't try to do copy-initialization if we already have
8683 direct-initialization. */
8684 return rhs;
8686 if (MAYBE_CLASS_TYPE_P (type))
8687 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8689 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8690 complain, flags);
8693 /* If RETVAL is the address of, or a reference to, a local variable or
8694 temporary give an appropriate warning and return true. */
8696 static bool
8697 maybe_warn_about_returning_address_of_local (tree retval)
8699 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8700 tree whats_returned = fold_for_warn (retval);
8702 for (;;)
8704 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8705 whats_returned = TREE_OPERAND (whats_returned, 1);
8706 else if (CONVERT_EXPR_P (whats_returned)
8707 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8708 whats_returned = TREE_OPERAND (whats_returned, 0);
8709 else
8710 break;
8713 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8714 return false;
8715 whats_returned = TREE_OPERAND (whats_returned, 0);
8717 while (TREE_CODE (whats_returned) == COMPONENT_REF
8718 || TREE_CODE (whats_returned) == ARRAY_REF)
8719 whats_returned = TREE_OPERAND (whats_returned, 0);
8721 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8723 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8724 || TREE_CODE (whats_returned) == TARGET_EXPR)
8726 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8727 return true;
8729 if (VAR_P (whats_returned)
8730 && DECL_NAME (whats_returned)
8731 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8733 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8734 return true;
8738 if (DECL_P (whats_returned)
8739 && DECL_NAME (whats_returned)
8740 && DECL_FUNCTION_SCOPE_P (whats_returned)
8741 && !is_capture_proxy (whats_returned)
8742 && !(TREE_STATIC (whats_returned)
8743 || TREE_PUBLIC (whats_returned)))
8745 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8746 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8747 OPT_Wreturn_local_addr,
8748 "reference to local variable %qD returned",
8749 whats_returned);
8750 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8751 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8752 OPT_Wreturn_local_addr, "address of label %qD returned",
8753 whats_returned);
8754 else
8755 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8756 OPT_Wreturn_local_addr, "address of local variable %qD "
8757 "returned", whats_returned);
8758 return true;
8761 return false;
8764 /* Check that returning RETVAL from the current function is valid.
8765 Return an expression explicitly showing all conversions required to
8766 change RETVAL into the function return type, and to assign it to
8767 the DECL_RESULT for the function. Set *NO_WARNING to true if
8768 code reaches end of non-void function warning shouldn't be issued
8769 on this RETURN_EXPR. */
8771 tree
8772 check_return_expr (tree retval, bool *no_warning)
8774 tree result;
8775 /* The type actually returned by the function. */
8776 tree valtype;
8777 /* The type the function is declared to return, or void if
8778 the declared type is incomplete. */
8779 tree functype;
8780 int fn_returns_value_p;
8781 bool named_return_value_okay_p;
8783 *no_warning = false;
8785 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8787 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8788 "statement is not allowed");
8789 return NULL_TREE;
8792 /* A `volatile' function is one that isn't supposed to return, ever.
8793 (This is a G++ extension, used to get better code for functions
8794 that call the `volatile' function.) */
8795 if (TREE_THIS_VOLATILE (current_function_decl))
8796 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8798 /* Check for various simple errors. */
8799 if (DECL_DESTRUCTOR_P (current_function_decl))
8801 if (retval)
8802 error ("returning a value from a destructor");
8803 return NULL_TREE;
8805 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8807 if (in_function_try_handler)
8808 /* If a return statement appears in a handler of the
8809 function-try-block of a constructor, the program is ill-formed. */
8810 error ("cannot return from a handler of a function-try-block of a constructor");
8811 else if (retval)
8812 /* You can't return a value from a constructor. */
8813 error ("returning a value from a constructor");
8814 return NULL_TREE;
8817 const tree saved_retval = retval;
8819 if (processing_template_decl)
8821 current_function_returns_value = 1;
8823 if (check_for_bare_parameter_packs (retval))
8824 return error_mark_node;
8826 if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
8827 || (retval != NULL_TREE
8828 && type_dependent_expression_p (retval)))
8829 return retval;
8832 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8834 /* Deduce auto return type from a return statement. */
8835 if (current_function_auto_return_pattern)
8837 tree auto_node;
8838 tree type;
8840 if (!retval && !is_auto (current_function_auto_return_pattern))
8842 /* Give a helpful error message. */
8843 error ("return-statement with no value, in function returning %qT",
8844 current_function_auto_return_pattern);
8845 inform (input_location, "only plain %<auto%> return type can be "
8846 "deduced to %<void%>");
8847 type = error_mark_node;
8849 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8851 error ("returning initializer list");
8852 type = error_mark_node;
8854 else
8856 if (!retval)
8857 retval = void_node;
8858 auto_node = type_uses_auto (current_function_auto_return_pattern);
8859 type = do_auto_deduction (current_function_auto_return_pattern,
8860 retval, auto_node);
8863 if (type == error_mark_node)
8864 /* Leave it. */;
8865 else if (functype == current_function_auto_return_pattern)
8866 apply_deduced_return_type (current_function_decl, type);
8867 else if (!same_type_p (type, functype))
8869 if (LAMBDA_FUNCTION_P (current_function_decl))
8870 error ("inconsistent types %qT and %qT deduced for "
8871 "lambda return type", functype, type);
8872 else
8873 error ("inconsistent deduction for auto return type: "
8874 "%qT and then %qT", functype, type);
8876 functype = type;
8879 result = DECL_RESULT (current_function_decl);
8880 valtype = TREE_TYPE (result);
8881 gcc_assert (valtype != NULL_TREE);
8882 fn_returns_value_p = !VOID_TYPE_P (valtype);
8884 /* Check for a return statement with no return value in a function
8885 that's supposed to return a value. */
8886 if (!retval && fn_returns_value_p)
8888 if (functype != error_mark_node)
8889 permerror (input_location, "return-statement with no value, in "
8890 "function returning %qT", valtype);
8891 /* Remember that this function did return. */
8892 current_function_returns_value = 1;
8893 /* And signal caller that TREE_NO_WARNING should be set on the
8894 RETURN_EXPR to avoid control reaches end of non-void function
8895 warnings in tree-cfg.c. */
8896 *no_warning = true;
8898 /* Check for a return statement with a value in a function that
8899 isn't supposed to return a value. */
8900 else if (retval && !fn_returns_value_p)
8902 if (VOID_TYPE_P (TREE_TYPE (retval)))
8903 /* You can return a `void' value from a function of `void'
8904 type. In that case, we have to evaluate the expression for
8905 its side-effects. */
8906 finish_expr_stmt (retval);
8907 else
8908 permerror (input_location, "return-statement with a value, in function "
8909 "returning 'void'");
8910 current_function_returns_null = 1;
8912 /* There's really no value to return, after all. */
8913 return NULL_TREE;
8915 else if (!retval)
8916 /* Remember that this function can sometimes return without a
8917 value. */
8918 current_function_returns_null = 1;
8919 else
8920 /* Remember that this function did return a value. */
8921 current_function_returns_value = 1;
8923 /* Check for erroneous operands -- but after giving ourselves a
8924 chance to provide an error about returning a value from a void
8925 function. */
8926 if (error_operand_p (retval))
8928 current_function_return_value = error_mark_node;
8929 return error_mark_node;
8932 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8933 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8934 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8935 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8936 && ! flag_check_new
8937 && retval && null_ptr_cst_p (retval))
8938 warning (0, "%<operator new%> must not return NULL unless it is "
8939 "declared %<throw()%> (or -fcheck-new is in effect)");
8941 /* Effective C++ rule 15. See also start_function. */
8942 if (warn_ecpp
8943 && DECL_NAME (current_function_decl) == cp_assignment_operator_id (NOP_EXPR))
8945 bool warn = true;
8947 /* The function return type must be a reference to the current
8948 class. */
8949 if (TREE_CODE (valtype) == REFERENCE_TYPE
8950 && same_type_ignoring_top_level_qualifiers_p
8951 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8953 /* Returning '*this' is obviously OK. */
8954 if (retval == current_class_ref)
8955 warn = false;
8956 /* If we are calling a function whose return type is the same of
8957 the current class reference, it is ok. */
8958 else if (INDIRECT_REF_P (retval)
8959 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8960 warn = false;
8963 if (warn)
8964 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8967 if (processing_template_decl)
8969 /* We should not have changed the return value. */
8970 gcc_assert (retval == saved_retval);
8971 return retval;
8974 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8976 [...] For a function with a class return type, if the expression
8977 in the return statement is the name of a local object, and the cv-
8978 unqualified type of the local object is the same as the function
8979 return type, an implementation is permitted to omit creating the tem-
8980 porary object to hold the function return value [...]
8982 So, if this is a value-returning function that always returns the same
8983 local variable, remember it.
8985 It might be nice to be more flexible, and choose the first suitable
8986 variable even if the function sometimes returns something else, but
8987 then we run the risk of clobbering the variable we chose if the other
8988 returned expression uses the chosen variable somehow. And people expect
8989 this restriction, anyway. (jason 2000-11-19)
8991 See finish_function and finalize_nrv for the rest of this optimization. */
8993 named_return_value_okay_p =
8994 (retval != NULL_TREE
8995 /* Must be a local, automatic variable. */
8996 && VAR_P (retval)
8997 && DECL_CONTEXT (retval) == current_function_decl
8998 && ! TREE_STATIC (retval)
8999 /* And not a lambda or anonymous union proxy. */
9000 && !DECL_HAS_VALUE_EXPR_P (retval)
9001 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9002 /* The cv-unqualified type of the returned value must be the
9003 same as the cv-unqualified return type of the
9004 function. */
9005 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9006 (TYPE_MAIN_VARIANT (functype)))
9007 /* And the returned value must be non-volatile. */
9008 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9010 if (fn_returns_value_p && flag_elide_constructors)
9012 if (named_return_value_okay_p
9013 && (current_function_return_value == NULL_TREE
9014 || current_function_return_value == retval))
9015 current_function_return_value = retval;
9016 else
9017 current_function_return_value = error_mark_node;
9020 /* We don't need to do any conversions when there's nothing being
9021 returned. */
9022 if (!retval)
9023 return NULL_TREE;
9025 /* Do any required conversions. */
9026 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9027 /* No conversions are required. */
9029 else
9031 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9033 /* The functype's return type will have been set to void, if it
9034 was an incomplete type. Just treat this as 'return;' */
9035 if (VOID_TYPE_P (functype))
9036 return error_mark_node;
9038 /* If we had an id-expression obfuscated by force_paren_expr, we need
9039 to undo it so we can try to treat it as an rvalue below. */
9040 retval = maybe_undo_parenthesized_ref (retval);
9042 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9043 treated as an rvalue for the purposes of overload resolution to
9044 favor move constructors over copy constructors.
9046 Note that these conditions are similar to, but not as strict as,
9047 the conditions for the named return value optimization. */
9048 if ((cxx_dialect != cxx98)
9049 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9050 || TREE_CODE (retval) == PARM_DECL)
9051 && DECL_CONTEXT (retval) == current_function_decl
9052 && !TREE_STATIC (retval)
9053 /* This is only interesting for class type. */
9054 && CLASS_TYPE_P (functype))
9055 flags = flags | LOOKUP_PREFER_RVALUE;
9057 /* First convert the value to the function's return type, then
9058 to the type of return value's location to handle the
9059 case that functype is smaller than the valtype. */
9060 retval = convert_for_initialization
9061 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9062 tf_warning_or_error);
9063 retval = convert (valtype, retval);
9065 /* If the conversion failed, treat this just like `return;'. */
9066 if (retval == error_mark_node)
9067 return retval;
9068 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9069 else if (! cfun->returns_struct
9070 && TREE_CODE (retval) == TARGET_EXPR
9071 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9072 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9073 TREE_OPERAND (retval, 0));
9074 else if (maybe_warn_about_returning_address_of_local (retval))
9075 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9076 build_zero_cst (TREE_TYPE (retval)));
9079 /* Actually copy the value returned into the appropriate location. */
9080 if (retval && retval != result)
9081 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9083 return retval;
9087 /* Returns nonzero if the pointer-type FROM can be converted to the
9088 pointer-type TO via a qualification conversion. If CONSTP is -1,
9089 then we return nonzero if the pointers are similar, and the
9090 cv-qualification signature of FROM is a proper subset of that of TO.
9092 If CONSTP is positive, then all outer pointers have been
9093 const-qualified. */
9095 static int
9096 comp_ptr_ttypes_real (tree to, tree from, int constp)
9098 bool to_more_cv_qualified = false;
9099 bool is_opaque_pointer = false;
9101 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9103 if (TREE_CODE (to) != TREE_CODE (from))
9104 return 0;
9106 if (TREE_CODE (from) == OFFSET_TYPE
9107 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9108 TYPE_OFFSET_BASETYPE (to)))
9109 return 0;
9111 /* Const and volatile mean something different for function types,
9112 so the usual checks are not appropriate. */
9113 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9115 if (!at_least_as_qualified_p (to, from))
9116 return 0;
9118 if (!at_least_as_qualified_p (from, to))
9120 if (constp == 0)
9121 return 0;
9122 to_more_cv_qualified = true;
9125 if (constp > 0)
9126 constp &= TYPE_READONLY (to);
9129 if (VECTOR_TYPE_P (to))
9130 is_opaque_pointer = vector_targets_convertible_p (to, from);
9132 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9133 return ((constp >= 0 || to_more_cv_qualified)
9134 && (is_opaque_pointer
9135 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9139 /* When comparing, say, char ** to char const **, this function takes
9140 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9141 types to this function. */
9144 comp_ptr_ttypes (tree to, tree from)
9146 return comp_ptr_ttypes_real (to, from, 1);
9149 /* Returns true iff FNTYPE is a non-class type that involves
9150 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9151 if a parameter type is ill-formed. */
9153 bool
9154 error_type_p (const_tree type)
9156 tree t;
9158 switch (TREE_CODE (type))
9160 case ERROR_MARK:
9161 return true;
9163 case POINTER_TYPE:
9164 case REFERENCE_TYPE:
9165 case OFFSET_TYPE:
9166 return error_type_p (TREE_TYPE (type));
9168 case FUNCTION_TYPE:
9169 case METHOD_TYPE:
9170 if (error_type_p (TREE_TYPE (type)))
9171 return true;
9172 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9173 if (error_type_p (TREE_VALUE (t)))
9174 return true;
9175 return false;
9177 case RECORD_TYPE:
9178 if (TYPE_PTRMEMFUNC_P (type))
9179 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9180 return false;
9182 default:
9183 return false;
9187 /* Returns true if to and from are (possibly multi-level) pointers to the same
9188 type or inheritance-related types, regardless of cv-quals. */
9190 bool
9191 ptr_reasonably_similar (const_tree to, const_tree from)
9193 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9195 /* Any target type is similar enough to void. */
9196 if (VOID_TYPE_P (to))
9197 return !error_type_p (from);
9198 if (VOID_TYPE_P (from))
9199 return !error_type_p (to);
9201 if (TREE_CODE (to) != TREE_CODE (from))
9202 return false;
9204 if (TREE_CODE (from) == OFFSET_TYPE
9205 && comptypes (TYPE_OFFSET_BASETYPE (to),
9206 TYPE_OFFSET_BASETYPE (from),
9207 COMPARE_BASE | COMPARE_DERIVED))
9208 continue;
9210 if (VECTOR_TYPE_P (to)
9211 && vector_types_convertible_p (to, from, false))
9212 return true;
9214 if (TREE_CODE (to) == INTEGER_TYPE
9215 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9216 return true;
9218 if (TREE_CODE (to) == FUNCTION_TYPE)
9219 return !error_type_p (to) && !error_type_p (from);
9221 if (!TYPE_PTR_P (to))
9223 /* When either type is incomplete avoid DERIVED_FROM_P,
9224 which may call complete_type (c++/57942). */
9225 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9226 return comptypes
9227 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9228 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9233 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9234 pointer-to-member types) are the same, ignoring cv-qualification at
9235 all levels. */
9237 bool
9238 comp_ptr_ttypes_const (tree to, tree from)
9240 bool is_opaque_pointer = false;
9242 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9244 if (TREE_CODE (to) != TREE_CODE (from))
9245 return false;
9247 if (TREE_CODE (from) == OFFSET_TYPE
9248 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9249 TYPE_OFFSET_BASETYPE (to)))
9250 continue;
9252 if (VECTOR_TYPE_P (to))
9253 is_opaque_pointer = vector_targets_convertible_p (to, from);
9255 if (!TYPE_PTR_P (to))
9256 return (is_opaque_pointer
9257 || same_type_ignoring_top_level_qualifiers_p (to, from));
9261 /* Returns the type qualifiers for this type, including the qualifiers on the
9262 elements for an array type. */
9265 cp_type_quals (const_tree type)
9267 int quals;
9268 /* This CONST_CAST is okay because strip_array_types returns its
9269 argument unmodified and we assign it to a const_tree. */
9270 type = strip_array_types (CONST_CAST_TREE (type));
9271 if (type == error_mark_node
9272 /* Quals on a FUNCTION_TYPE are memfn quals. */
9273 || TREE_CODE (type) == FUNCTION_TYPE)
9274 return TYPE_UNQUALIFIED;
9275 quals = TYPE_QUALS (type);
9276 /* METHOD and REFERENCE_TYPEs should never have quals. */
9277 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9278 && TREE_CODE (type) != REFERENCE_TYPE)
9279 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9280 == TYPE_UNQUALIFIED));
9281 return quals;
9284 /* Returns the function-ref-qualifier for TYPE */
9286 cp_ref_qualifier
9287 type_memfn_rqual (const_tree type)
9289 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9290 || TREE_CODE (type) == METHOD_TYPE);
9292 if (!FUNCTION_REF_QUALIFIED (type))
9293 return REF_QUAL_NONE;
9294 else if (FUNCTION_RVALUE_QUALIFIED (type))
9295 return REF_QUAL_RVALUE;
9296 else
9297 return REF_QUAL_LVALUE;
9300 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9301 METHOD_TYPE. */
9304 type_memfn_quals (const_tree type)
9306 if (TREE_CODE (type) == FUNCTION_TYPE)
9307 return TYPE_QUALS (type);
9308 else if (TREE_CODE (type) == METHOD_TYPE)
9309 return cp_type_quals (class_of_this_parm (type));
9310 else
9311 gcc_unreachable ();
9314 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9315 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9317 tree
9318 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9320 /* Could handle METHOD_TYPE here if necessary. */
9321 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9322 if (TYPE_QUALS (type) == memfn_quals
9323 && type_memfn_rqual (type) == rqual)
9324 return type;
9326 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9327 complex. */
9328 tree result = build_qualified_type (type, memfn_quals);
9329 return build_ref_qualified_type (result, rqual);
9332 /* Returns nonzero if TYPE is const or volatile. */
9334 bool
9335 cv_qualified_p (const_tree type)
9337 int quals = cp_type_quals (type);
9338 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9341 /* Returns nonzero if the TYPE contains a mutable member. */
9343 bool
9344 cp_has_mutable_p (const_tree type)
9346 /* This CONST_CAST is okay because strip_array_types returns its
9347 argument unmodified and we assign it to a const_tree. */
9348 type = strip_array_types (CONST_CAST_TREE(type));
9350 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9353 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9354 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9355 approximation. In particular, consider:
9357 int f();
9358 struct S { int i; };
9359 const S s = { f(); }
9361 Here, we will make "s" as TREE_READONLY (because it is declared
9362 "const") -- only to reverse ourselves upon seeing that the
9363 initializer is non-constant. */
9365 void
9366 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9368 tree type = TREE_TYPE (decl);
9370 if (type == error_mark_node)
9371 return;
9373 if (TREE_CODE (decl) == TYPE_DECL)
9374 return;
9376 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9377 && type_quals != TYPE_UNQUALIFIED));
9379 /* Avoid setting TREE_READONLY incorrectly. */
9380 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9381 constructor can produce constant init, so rely on cp_finish_decl to
9382 clear TREE_READONLY if the variable has non-constant init. */
9384 /* If the type has (or might have) a mutable component, that component
9385 might be modified. */
9386 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9387 type_quals &= ~TYPE_QUAL_CONST;
9389 c_apply_type_quals_to_decl (type_quals, decl);
9392 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9393 exemplar types such that casting T1 to T2 is casting away constness
9394 if and only if there is no implicit conversion from T1 to T2. */
9396 static void
9397 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9399 int quals1;
9400 int quals2;
9402 /* [expr.const.cast]
9404 For multi-level pointer to members and multi-level mixed pointers
9405 and pointers to members (conv.qual), the "member" aspect of a
9406 pointer to member level is ignored when determining if a const
9407 cv-qualifier has been cast away. */
9408 /* [expr.const.cast]
9410 For two pointer types:
9412 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9413 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9414 K is min(N,M)
9416 casting from X1 to X2 casts away constness if, for a non-pointer
9417 type T there does not exist an implicit conversion (clause
9418 _conv_) from:
9420 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9424 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9425 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9426 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9428 *t1 = cp_build_qualified_type (void_type_node,
9429 cp_type_quals (*t1));
9430 *t2 = cp_build_qualified_type (void_type_node,
9431 cp_type_quals (*t2));
9432 return;
9435 quals1 = cp_type_quals (*t1);
9436 quals2 = cp_type_quals (*t2);
9438 if (TYPE_PTRDATAMEM_P (*t1))
9439 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9440 else
9441 *t1 = TREE_TYPE (*t1);
9442 if (TYPE_PTRDATAMEM_P (*t2))
9443 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9444 else
9445 *t2 = TREE_TYPE (*t2);
9447 casts_away_constness_r (t1, t2, complain);
9448 *t1 = build_pointer_type (*t1);
9449 *t2 = build_pointer_type (*t2);
9450 *t1 = cp_build_qualified_type (*t1, quals1);
9451 *t2 = cp_build_qualified_type (*t2, quals2);
9454 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9455 constness.
9457 ??? This function returns non-zero if casting away qualifiers not
9458 just const. We would like to return to the caller exactly which
9459 qualifiers are casted away to give more accurate diagnostics.
9462 static bool
9463 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9465 if (TREE_CODE (t2) == REFERENCE_TYPE)
9467 /* [expr.const.cast]
9469 Casting from an lvalue of type T1 to an lvalue of type T2
9470 using a reference cast casts away constness if a cast from an
9471 rvalue of type "pointer to T1" to the type "pointer to T2"
9472 casts away constness. */
9473 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9474 return casts_away_constness (build_pointer_type (t1),
9475 build_pointer_type (TREE_TYPE (t2)),
9476 complain);
9479 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9480 /* [expr.const.cast]
9482 Casting from an rvalue of type "pointer to data member of X
9483 of type T1" to the type "pointer to data member of Y of type
9484 T2" casts away constness if a cast from an rvalue of type
9485 "pointer to T1" to the type "pointer to T2" casts away
9486 constness. */
9487 return casts_away_constness
9488 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9489 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9490 complain);
9492 /* Casting away constness is only something that makes sense for
9493 pointer or reference types. */
9494 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9495 return false;
9497 /* Top-level qualifiers don't matter. */
9498 t1 = TYPE_MAIN_VARIANT (t1);
9499 t2 = TYPE_MAIN_VARIANT (t2);
9500 casts_away_constness_r (&t1, &t2, complain);
9501 if (!can_convert (t2, t1, complain))
9502 return true;
9504 return false;
9507 /* If T is a REFERENCE_TYPE return the type to which T refers.
9508 Otherwise, return T itself. */
9510 tree
9511 non_reference (tree t)
9513 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9514 t = TREE_TYPE (t);
9515 return t;
9519 /* Return nonzero if REF is an lvalue valid for this language;
9520 otherwise, print an error message and return zero. USE says
9521 how the lvalue is being used and so selects the error message. */
9524 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9526 cp_lvalue_kind kind = lvalue_kind (ref);
9528 if (kind == clk_none)
9530 if (complain & tf_error)
9531 lvalue_error (input_location, use);
9532 return 0;
9534 else if (kind & (clk_rvalueref|clk_class))
9536 if (!(complain & tf_error))
9537 return 0;
9538 if (kind & clk_class)
9539 /* Make this a permerror because we used to accept it. */
9540 permerror (input_location, "using temporary as lvalue");
9541 else
9542 error ("using xvalue (rvalue reference) as lvalue");
9544 return 1;
9547 /* Return true if a user-defined literal operator is a raw operator. */
9549 bool
9550 check_raw_literal_operator (const_tree decl)
9552 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9553 tree argtype;
9554 int arity;
9555 bool maybe_raw_p = false;
9557 /* Count the number and type of arguments and check for ellipsis. */
9558 for (argtype = argtypes, arity = 0;
9559 argtype && argtype != void_list_node;
9560 ++arity, argtype = TREE_CHAIN (argtype))
9562 tree t = TREE_VALUE (argtype);
9564 if (same_type_p (t, const_string_type_node))
9565 maybe_raw_p = true;
9567 if (!argtype)
9568 return false; /* Found ellipsis. */
9570 if (!maybe_raw_p || arity != 1)
9571 return false;
9573 return true;
9577 /* Return true if a user-defined literal operator has one of the allowed
9578 argument types. */
9580 bool
9581 check_literal_operator_args (const_tree decl,
9582 bool *long_long_unsigned_p, bool *long_double_p)
9584 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9586 *long_long_unsigned_p = false;
9587 *long_double_p = false;
9588 if (processing_template_decl || processing_specialization)
9589 return argtypes == void_list_node;
9590 else
9592 tree argtype;
9593 int arity;
9594 int max_arity = 2;
9596 /* Count the number and type of arguments and check for ellipsis. */
9597 for (argtype = argtypes, arity = 0;
9598 argtype && argtype != void_list_node;
9599 argtype = TREE_CHAIN (argtype))
9601 tree t = TREE_VALUE (argtype);
9602 ++arity;
9604 if (TYPE_PTR_P (t))
9606 bool maybe_raw_p = false;
9607 t = TREE_TYPE (t);
9608 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9609 return false;
9610 t = TYPE_MAIN_VARIANT (t);
9611 if ((maybe_raw_p = same_type_p (t, char_type_node))
9612 || same_type_p (t, wchar_type_node)
9613 || same_type_p (t, char16_type_node)
9614 || same_type_p (t, char32_type_node))
9616 argtype = TREE_CHAIN (argtype);
9617 if (!argtype)
9618 return false;
9619 t = TREE_VALUE (argtype);
9620 if (maybe_raw_p && argtype == void_list_node)
9621 return true;
9622 else if (same_type_p (t, size_type_node))
9624 ++arity;
9625 continue;
9627 else
9628 return false;
9631 else if (same_type_p (t, long_long_unsigned_type_node))
9633 max_arity = 1;
9634 *long_long_unsigned_p = true;
9636 else if (same_type_p (t, long_double_type_node))
9638 max_arity = 1;
9639 *long_double_p = true;
9641 else if (same_type_p (t, char_type_node))
9642 max_arity = 1;
9643 else if (same_type_p (t, wchar_type_node))
9644 max_arity = 1;
9645 else if (same_type_p (t, char16_type_node))
9646 max_arity = 1;
9647 else if (same_type_p (t, char32_type_node))
9648 max_arity = 1;
9649 else
9650 return false;
9652 if (!argtype)
9653 return false; /* Found ellipsis. */
9655 if (arity != max_arity)
9656 return false;
9658 return true;
9662 /* Always returns false since unlike C90, C++ has no concept of implicit
9663 function declarations. */
9665 bool
9666 c_decl_implicit (const_tree)
9668 return false;