PR c++/79232
[official-gcc.git] / gcc / cp / typeck.c
blob87e7cc0b02996b7e2bc15f5d6f598600f14301ae
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 return cxx_constant_value (e);
1803 /* EXPR is being used in a context that is not a function call.
1804 Enforce:
1806 [expr.ref]
1808 The expression can be used only as the left-hand operand of a
1809 member function call.
1811 [expr.mptr.operator]
1813 If the result of .* or ->* is a function, then that result can be
1814 used only as the operand for the function call operator ().
1816 by issuing an error message if appropriate. Returns true iff EXPR
1817 violates these rules. */
1819 bool
1820 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1822 if (expr == NULL_TREE)
1823 return false;
1824 /* Don't enforce this in MS mode. */
1825 if (flag_ms_extensions)
1826 return false;
1827 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1828 expr = get_first_fn (expr);
1829 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1831 if (complain & tf_error)
1833 if (DECL_P (expr))
1835 error_at (loc, "invalid use of non-static member function %qD",
1836 expr);
1837 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1839 else
1840 error_at (loc, "invalid use of non-static member function of "
1841 "type %qT", TREE_TYPE (expr));
1843 return true;
1845 return false;
1848 /* If EXP is a reference to a bitfield, and the type of EXP does not
1849 match the declared type of the bitfield, return the declared type
1850 of the bitfield. Otherwise, return NULL_TREE. */
1852 tree
1853 is_bitfield_expr_with_lowered_type (const_tree exp)
1855 switch (TREE_CODE (exp))
1857 case COND_EXPR:
1858 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1859 ? TREE_OPERAND (exp, 1)
1860 : TREE_OPERAND (exp, 0)))
1861 return NULL_TREE;
1862 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1864 case COMPOUND_EXPR:
1865 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1867 case MODIFY_EXPR:
1868 case SAVE_EXPR:
1869 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1871 case COMPONENT_REF:
1873 tree field;
1875 field = TREE_OPERAND (exp, 1);
1876 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1877 return NULL_TREE;
1878 if (same_type_ignoring_top_level_qualifiers_p
1879 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1880 return NULL_TREE;
1881 return DECL_BIT_FIELD_TYPE (field);
1884 case VAR_DECL:
1885 if (DECL_HAS_VALUE_EXPR_P (exp))
1886 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1887 (CONST_CAST_TREE (exp)));
1888 return NULL_TREE;
1890 CASE_CONVERT:
1891 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1892 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1893 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1894 /* Fallthrough. */
1896 default:
1897 return NULL_TREE;
1901 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1902 bitfield with a lowered type, the type of EXP is returned, rather
1903 than NULL_TREE. */
1905 tree
1906 unlowered_expr_type (const_tree exp)
1908 tree type;
1909 tree etype = TREE_TYPE (exp);
1911 type = is_bitfield_expr_with_lowered_type (exp);
1912 if (type)
1913 type = cp_build_qualified_type (type, cp_type_quals (etype));
1914 else
1915 type = etype;
1917 return type;
1920 /* Perform the conversions in [expr] that apply when an lvalue appears
1921 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1922 function-to-pointer conversions. In addition, bitfield references are
1923 converted to their declared types. Note that this function does not perform
1924 the lvalue-to-rvalue conversion for class types. If you need that conversion
1925 for class types, then you probably need to use force_rvalue.
1927 Although the returned value is being used as an rvalue, this
1928 function does not wrap the returned expression in a
1929 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1930 that the return value is no longer an lvalue. */
1932 tree
1933 decay_conversion (tree exp,
1934 tsubst_flags_t complain,
1935 bool reject_builtin /* = true */)
1937 tree type;
1938 enum tree_code code;
1939 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1941 type = TREE_TYPE (exp);
1942 if (type == error_mark_node)
1943 return error_mark_node;
1945 exp = resolve_nondeduced_context (exp, complain);
1946 if (type_unknown_p (exp))
1948 if (complain & tf_error)
1949 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1950 return error_mark_node;
1953 code = TREE_CODE (type);
1955 if (error_operand_p (exp))
1956 return error_mark_node;
1958 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1959 return nullptr_node;
1961 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1962 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1963 if (code == VOID_TYPE)
1965 if (complain & tf_error)
1966 error_at (loc, "void value not ignored as it ought to be");
1967 return error_mark_node;
1969 if (invalid_nonstatic_memfn_p (loc, exp, complain))
1970 return error_mark_node;
1971 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1973 exp = mark_lvalue_use (exp);
1974 if (reject_builtin && reject_gcc_builtin (exp, loc))
1975 return error_mark_node;
1976 return cp_build_addr_expr (exp, complain);
1978 if (code == ARRAY_TYPE)
1980 tree adr;
1981 tree ptrtype;
1983 exp = mark_lvalue_use (exp);
1985 if (INDIRECT_REF_P (exp))
1986 return build_nop (build_pointer_type (TREE_TYPE (type)),
1987 TREE_OPERAND (exp, 0));
1989 if (TREE_CODE (exp) == COMPOUND_EXPR)
1991 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1992 if (op1 == error_mark_node)
1993 return error_mark_node;
1994 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1995 TREE_OPERAND (exp, 0), op1);
1998 if (!obvalue_p (exp)
1999 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2001 if (complain & tf_error)
2002 error_at (loc, "invalid use of non-lvalue array");
2003 return error_mark_node;
2006 /* Don't let an array compound literal decay to a pointer. It can
2007 still be used to initialize an array or bind to a reference. */
2008 if (TREE_CODE (exp) == TARGET_EXPR)
2010 if (complain & tf_error)
2011 error_at (loc, "taking address of temporary array");
2012 return error_mark_node;
2015 ptrtype = build_pointer_type (TREE_TYPE (type));
2017 if (VAR_P (exp))
2019 if (!cxx_mark_addressable (exp))
2020 return error_mark_node;
2021 adr = build_nop (ptrtype, build_address (exp));
2022 return adr;
2024 /* This way is better for a COMPONENT_REF since it can
2025 simplify the offset for a component. */
2026 adr = cp_build_addr_expr (exp, complain);
2027 return cp_convert (ptrtype, adr, complain);
2030 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2031 exp = mark_rvalue_use (exp, loc, reject_builtin);
2033 /* If a bitfield is used in a context where integral promotion
2034 applies, then the caller is expected to have used
2035 default_conversion. That function promotes bitfields correctly
2036 before calling this function. At this point, if we have a
2037 bitfield referenced, we may assume that is not subject to
2038 promotion, and that, therefore, the type of the resulting rvalue
2039 is the declared type of the bitfield. */
2040 exp = convert_bitfield_to_declared_type (exp);
2042 /* We do not call rvalue() here because we do not want to wrap EXP
2043 in a NON_LVALUE_EXPR. */
2045 /* [basic.lval]
2047 Non-class rvalues always have cv-unqualified types. */
2048 type = TREE_TYPE (exp);
2049 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2050 exp = build_nop (cv_unqualified (type), exp);
2052 if (!complete_type_or_maybe_complain (type, exp, complain))
2053 return error_mark_node;
2055 return exp;
2058 /* Perform preparatory conversions, as part of the "usual arithmetic
2059 conversions". In particular, as per [expr]:
2061 Whenever an lvalue expression appears as an operand of an
2062 operator that expects the rvalue for that operand, the
2063 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2064 standard conversions are applied to convert the expression to an
2065 rvalue.
2067 In addition, we perform integral promotions here, as those are
2068 applied to both operands to a binary operator before determining
2069 what additional conversions should apply. */
2071 static tree
2072 cp_default_conversion (tree exp, tsubst_flags_t complain)
2074 /* Check for target-specific promotions. */
2075 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2076 if (promoted_type)
2077 exp = cp_convert (promoted_type, exp, complain);
2078 /* Perform the integral promotions first so that bitfield
2079 expressions (which may promote to "int", even if the bitfield is
2080 declared "unsigned") are promoted correctly. */
2081 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2082 exp = cp_perform_integral_promotions (exp, complain);
2083 /* Perform the other conversions. */
2084 exp = decay_conversion (exp, complain);
2086 return exp;
2089 /* C version. */
2091 tree
2092 default_conversion (tree exp)
2094 return cp_default_conversion (exp, tf_warning_or_error);
2097 /* EXPR is an expression with an integral or enumeration type.
2098 Perform the integral promotions in [conv.prom], and return the
2099 converted value. */
2101 tree
2102 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2104 tree type;
2105 tree promoted_type;
2107 expr = mark_rvalue_use (expr);
2109 /* [conv.prom]
2111 If the bitfield has an enumerated type, it is treated as any
2112 other value of that type for promotion purposes. */
2113 type = is_bitfield_expr_with_lowered_type (expr);
2114 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2115 type = TREE_TYPE (expr);
2116 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2117 /* Scoped enums don't promote. */
2118 if (SCOPED_ENUM_P (type))
2119 return expr;
2120 promoted_type = type_promotes_to (type);
2121 if (type != promoted_type)
2122 expr = cp_convert (promoted_type, expr, complain);
2123 return expr;
2126 /* C version. */
2128 tree
2129 perform_integral_promotions (tree expr)
2131 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2134 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2135 decay_conversion to one. */
2138 string_conv_p (const_tree totype, const_tree exp, int warn)
2140 tree t;
2142 if (!TYPE_PTR_P (totype))
2143 return 0;
2145 t = TREE_TYPE (totype);
2146 if (!same_type_p (t, char_type_node)
2147 && !same_type_p (t, char16_type_node)
2148 && !same_type_p (t, char32_type_node)
2149 && !same_type_p (t, wchar_type_node))
2150 return 0;
2152 if (TREE_CODE (exp) == STRING_CST)
2154 /* Make sure that we don't try to convert between char and wide chars. */
2155 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2156 return 0;
2158 else
2160 /* Is this a string constant which has decayed to 'const char *'? */
2161 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2162 if (!same_type_p (TREE_TYPE (exp), t))
2163 return 0;
2164 STRIP_NOPS (exp);
2165 if (TREE_CODE (exp) != ADDR_EXPR
2166 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2167 return 0;
2169 if (warn)
2171 if (cxx_dialect >= cxx11)
2172 pedwarn (input_location,
2173 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
2174 "ISO C++ forbids converting a string constant to %qT",
2175 totype);
2176 else
2177 warning (OPT_Wwrite_strings,
2178 "deprecated conversion from string constant to %qT",
2179 totype);
2182 return 1;
2185 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2186 can, for example, use as an lvalue. This code used to be in
2187 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2188 expressions, where we're dealing with aggregates. But now it's again only
2189 called from unary_complex_lvalue. The case (in particular) that led to
2190 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2191 get it there. */
2193 static tree
2194 rationalize_conditional_expr (enum tree_code code, tree t,
2195 tsubst_flags_t complain)
2197 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2199 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2200 the first operand is always the one to be used if both operands
2201 are equal, so we know what conditional expression this used to be. */
2202 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2204 tree op0 = TREE_OPERAND (t, 0);
2205 tree op1 = TREE_OPERAND (t, 1);
2207 /* The following code is incorrect if either operand side-effects. */
2208 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2209 && !TREE_SIDE_EFFECTS (op1));
2210 return
2211 build_conditional_expr (loc,
2212 build_x_binary_op (loc,
2213 (TREE_CODE (t) == MIN_EXPR
2214 ? LE_EXPR : GE_EXPR),
2215 op0, TREE_CODE (op0),
2216 op1, TREE_CODE (op1),
2217 /*overload=*/NULL,
2218 complain),
2219 cp_build_unary_op (code, op0, false, complain),
2220 cp_build_unary_op (code, op1, false, complain),
2221 complain);
2224 return
2225 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2226 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2227 complain),
2228 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2229 complain),
2230 complain);
2233 /* Given the TYPE of an anonymous union field inside T, return the
2234 FIELD_DECL for the field. If not found return NULL_TREE. Because
2235 anonymous unions can nest, we must also search all anonymous unions
2236 that are directly reachable. */
2238 tree
2239 lookup_anon_field (tree t, tree type)
2241 tree field;
2243 t = TYPE_MAIN_VARIANT (t);
2245 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2247 if (TREE_STATIC (field))
2248 continue;
2249 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2250 continue;
2252 /* If we find it directly, return the field. */
2253 if (DECL_NAME (field) == NULL_TREE
2254 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2256 return field;
2259 /* Otherwise, it could be nested, search harder. */
2260 if (DECL_NAME (field) == NULL_TREE
2261 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2263 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2264 if (subfield)
2265 return subfield;
2268 return NULL_TREE;
2271 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2272 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2273 non-NULL, it indicates the path to the base used to name MEMBER.
2274 If PRESERVE_REFERENCE is true, the expression returned will have
2275 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2276 returned will have the type referred to by the reference.
2278 This function does not perform access control; that is either done
2279 earlier by the parser when the name of MEMBER is resolved to MEMBER
2280 itself, or later when overload resolution selects one of the
2281 functions indicated by MEMBER. */
2283 tree
2284 build_class_member_access_expr (cp_expr object, tree member,
2285 tree access_path, bool preserve_reference,
2286 tsubst_flags_t complain)
2288 tree object_type;
2289 tree member_scope;
2290 tree result = NULL_TREE;
2291 tree using_decl = NULL_TREE;
2293 if (error_operand_p (object) || error_operand_p (member))
2294 return error_mark_node;
2296 gcc_assert (DECL_P (member) || BASELINK_P (member));
2298 /* [expr.ref]
2300 The type of the first expression shall be "class object" (of a
2301 complete type). */
2302 object_type = TREE_TYPE (object);
2303 if (!currently_open_class (object_type)
2304 && !complete_type_or_maybe_complain (object_type, object, complain))
2305 return error_mark_node;
2306 if (!CLASS_TYPE_P (object_type))
2308 if (complain & tf_error)
2310 if (POINTER_TYPE_P (object_type)
2311 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2312 error ("request for member %qD in %qE, which is of pointer "
2313 "type %qT (maybe you meant to use %<->%> ?)",
2314 member, object.get_value (), object_type);
2315 else
2316 error ("request for member %qD in %qE, which is of non-class "
2317 "type %qT", member, object.get_value (), object_type);
2319 return error_mark_node;
2322 /* The standard does not seem to actually say that MEMBER must be a
2323 member of OBJECT_TYPE. However, that is clearly what is
2324 intended. */
2325 if (DECL_P (member))
2327 member_scope = DECL_CLASS_CONTEXT (member);
2328 if (!mark_used (member, complain) && !(complain & tf_error))
2329 return error_mark_node;
2330 if (TREE_DEPRECATED (member))
2331 warn_deprecated_use (member, NULL_TREE);
2333 else
2334 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2335 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2336 presently be the anonymous union. Go outwards until we find a
2337 type related to OBJECT_TYPE. */
2338 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2339 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2340 object_type))
2341 member_scope = TYPE_CONTEXT (member_scope);
2342 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2344 if (complain & tf_error)
2346 if (TREE_CODE (member) == FIELD_DECL)
2347 error ("invalid use of nonstatic data member %qE", member);
2348 else
2349 error ("%qD is not a member of %qT", member, object_type);
2351 return error_mark_node;
2354 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2355 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2356 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2358 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2359 if (temp)
2360 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2363 /* In [expr.ref], there is an explicit list of the valid choices for
2364 MEMBER. We check for each of those cases here. */
2365 if (VAR_P (member))
2367 /* A static data member. */
2368 result = member;
2369 mark_exp_read (object);
2370 /* If OBJECT has side-effects, they are supposed to occur. */
2371 if (TREE_SIDE_EFFECTS (object))
2372 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2374 else if (TREE_CODE (member) == FIELD_DECL)
2376 /* A non-static data member. */
2377 bool null_object_p;
2378 int type_quals;
2379 tree member_type;
2381 if (INDIRECT_REF_P (object))
2382 null_object_p =
2383 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2384 else
2385 null_object_p = false;
2387 /* Convert OBJECT to the type of MEMBER. */
2388 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2389 TYPE_MAIN_VARIANT (member_scope)))
2391 tree binfo;
2392 base_kind kind;
2394 binfo = lookup_base (access_path ? access_path : object_type,
2395 member_scope, ba_unique, &kind, complain);
2396 if (binfo == error_mark_node)
2397 return error_mark_node;
2399 /* It is invalid to try to get to a virtual base of a
2400 NULL object. The most common cause is invalid use of
2401 offsetof macro. */
2402 if (null_object_p && kind == bk_via_virtual)
2404 if (complain & tf_error)
2406 error ("invalid access to non-static data member %qD in "
2407 "virtual base of NULL object", member);
2409 return error_mark_node;
2412 /* Convert to the base. */
2413 object = build_base_path (PLUS_EXPR, object, binfo,
2414 /*nonnull=*/1, complain);
2415 /* If we found the base successfully then we should be able
2416 to convert to it successfully. */
2417 gcc_assert (object != error_mark_node);
2420 /* If MEMBER is from an anonymous aggregate, we have converted
2421 OBJECT so that it refers to the class containing the
2422 anonymous union. Generate a reference to the anonymous union
2423 itself, and recur to find MEMBER. */
2424 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2425 /* When this code is called from build_field_call, the
2426 object already has the type of the anonymous union.
2427 That is because the COMPONENT_REF was already
2428 constructed, and was then disassembled before calling
2429 build_field_call. After the function-call code is
2430 cleaned up, this waste can be eliminated. */
2431 && (!same_type_ignoring_top_level_qualifiers_p
2432 (TREE_TYPE (object), DECL_CONTEXT (member))))
2434 tree anonymous_union;
2436 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2437 DECL_CONTEXT (member));
2438 object = build_class_member_access_expr (object,
2439 anonymous_union,
2440 /*access_path=*/NULL_TREE,
2441 preserve_reference,
2442 complain);
2445 /* Compute the type of the field, as described in [expr.ref]. */
2446 type_quals = TYPE_UNQUALIFIED;
2447 member_type = TREE_TYPE (member);
2448 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2450 type_quals = (cp_type_quals (member_type)
2451 | cp_type_quals (object_type));
2453 /* A field is const (volatile) if the enclosing object, or the
2454 field itself, is const (volatile). But, a mutable field is
2455 not const, even within a const object. */
2456 if (DECL_MUTABLE_P (member))
2457 type_quals &= ~TYPE_QUAL_CONST;
2458 member_type = cp_build_qualified_type (member_type, type_quals);
2461 result = build3_loc (input_location, COMPONENT_REF, member_type,
2462 object, member, NULL_TREE);
2464 /* Mark the expression const or volatile, as appropriate. Even
2465 though we've dealt with the type above, we still have to mark the
2466 expression itself. */
2467 if (type_quals & TYPE_QUAL_CONST)
2468 TREE_READONLY (result) = 1;
2469 if (type_quals & TYPE_QUAL_VOLATILE)
2470 TREE_THIS_VOLATILE (result) = 1;
2472 else if (BASELINK_P (member))
2474 /* The member is a (possibly overloaded) member function. */
2475 tree functions;
2476 tree type;
2478 /* If the MEMBER is exactly one static member function, then we
2479 know the type of the expression. Otherwise, we must wait
2480 until overload resolution has been performed. */
2481 functions = BASELINK_FUNCTIONS (member);
2482 if (TREE_CODE (functions) == FUNCTION_DECL
2483 && DECL_STATIC_FUNCTION_P (functions))
2484 type = TREE_TYPE (functions);
2485 else
2486 type = unknown_type_node;
2487 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2488 base. That will happen when the function is called. */
2489 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2491 else if (TREE_CODE (member) == CONST_DECL)
2493 /* The member is an enumerator. */
2494 result = member;
2495 /* If OBJECT has side-effects, they are supposed to occur. */
2496 if (TREE_SIDE_EFFECTS (object))
2497 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2498 object, result);
2500 else if ((using_decl = strip_using_decl (member)) != member)
2501 result = build_class_member_access_expr (object,
2502 using_decl,
2503 access_path, preserve_reference,
2504 complain);
2505 else
2507 if (complain & tf_error)
2508 error ("invalid use of %qD", member);
2509 return error_mark_node;
2512 if (!preserve_reference)
2513 /* [expr.ref]
2515 If E2 is declared to have type "reference to T", then ... the
2516 type of E1.E2 is T. */
2517 result = convert_from_reference (result);
2519 return result;
2522 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2523 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2525 static tree
2526 lookup_destructor (tree object, tree scope, tree dtor_name,
2527 tsubst_flags_t complain)
2529 tree object_type = TREE_TYPE (object);
2530 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2531 tree expr;
2533 /* We've already complained about this destructor. */
2534 if (dtor_type == error_mark_node)
2535 return error_mark_node;
2537 if (scope && !check_dtor_name (scope, dtor_type))
2539 if (complain & tf_error)
2540 error ("qualified type %qT does not match destructor name ~%qT",
2541 scope, dtor_type);
2542 return error_mark_node;
2544 if (is_auto (dtor_type))
2545 dtor_type = object_type;
2546 else if (identifier_p (dtor_type))
2548 /* In a template, names we can't find a match for are still accepted
2549 destructor names, and we check them here. */
2550 if (check_dtor_name (object_type, dtor_type))
2551 dtor_type = object_type;
2552 else
2554 if (complain & tf_error)
2555 error ("object type %qT does not match destructor name ~%qT",
2556 object_type, dtor_type);
2557 return error_mark_node;
2561 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2563 if (complain & tf_error)
2564 error ("the type being destroyed is %qT, but the destructor "
2565 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2566 return error_mark_node;
2568 expr = lookup_member (dtor_type, complete_dtor_identifier,
2569 /*protect=*/1, /*want_type=*/false,
2570 tf_warning_or_error);
2571 if (!expr)
2573 if (complain & tf_error)
2574 cxx_incomplete_type_error (dtor_name, dtor_type);
2575 return error_mark_node;
2577 expr = (adjust_result_of_qualified_name_lookup
2578 (expr, dtor_type, object_type));
2579 if (scope == NULL_TREE)
2580 /* We need to call adjust_result_of_qualified_name_lookup in case the
2581 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2582 that we still get virtual function binding. */
2583 BASELINK_QUALIFIED_P (expr) = false;
2584 return expr;
2587 /* An expression of the form "A::template B" has been resolved to
2588 DECL. Issue a diagnostic if B is not a template or template
2589 specialization. */
2591 void
2592 check_template_keyword (tree decl)
2594 /* The standard says:
2596 [temp.names]
2598 If a name prefixed by the keyword template is not a member
2599 template, the program is ill-formed.
2601 DR 228 removed the restriction that the template be a member
2602 template.
2604 DR 96, if accepted would add the further restriction that explicit
2605 template arguments must be provided if the template keyword is
2606 used, but, as of 2005-10-16, that DR is still in "drafting". If
2607 this DR is accepted, then the semantic checks here can be
2608 simplified, as the entity named must in fact be a template
2609 specialization, rather than, as at present, a set of overloaded
2610 functions containing at least one template function. */
2611 if (TREE_CODE (decl) != TEMPLATE_DECL
2612 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2614 if (VAR_P (decl))
2616 if (DECL_USE_TEMPLATE (decl)
2617 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2619 else
2620 permerror (input_location, "%qD is not a template", decl);
2622 else if (!is_overloaded_fn (decl))
2623 permerror (input_location, "%qD is not a template", decl);
2624 else
2626 tree fns;
2627 fns = decl;
2628 if (BASELINK_P (fns))
2629 fns = BASELINK_FUNCTIONS (fns);
2630 while (fns)
2632 tree fn = OVL_CURRENT (fns);
2633 if (TREE_CODE (fn) == TEMPLATE_DECL
2634 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2635 break;
2636 if (TREE_CODE (fn) == FUNCTION_DECL
2637 && DECL_USE_TEMPLATE (fn)
2638 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2639 break;
2640 fns = OVL_NEXT (fns);
2642 if (!fns)
2643 permerror (input_location, "%qD is not a template", decl);
2648 /* This function is called by the parser to process a class member
2649 access expression of the form OBJECT.NAME. NAME is a node used by
2650 the parser to represent a name; it is not yet a DECL. It may,
2651 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2652 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2653 there is no reason to do the lookup twice, so the parser keeps the
2654 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2655 be a template via the use of the "A::template B" syntax. */
2657 tree
2658 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2659 tsubst_flags_t complain)
2661 tree expr;
2662 tree object_type;
2663 tree member;
2664 tree access_path = NULL_TREE;
2665 tree orig_object = object;
2666 tree orig_name = name;
2668 if (object == error_mark_node || name == error_mark_node)
2669 return error_mark_node;
2671 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2672 if (!objc_is_public (object, name))
2673 return error_mark_node;
2675 object_type = TREE_TYPE (object);
2677 if (processing_template_decl)
2679 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2680 type_dependent_object_expression_p (object)
2681 /* If NAME is "f<args>", where either 'f' or 'args' is
2682 dependent, then the expression is dependent. */
2683 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2684 && dependent_template_id_p (TREE_OPERAND (name, 0),
2685 TREE_OPERAND (name, 1)))
2686 /* If NAME is "T::X" where "T" is dependent, then the
2687 expression is dependent. */
2688 || (TREE_CODE (name) == SCOPE_REF
2689 && TYPE_P (TREE_OPERAND (name, 0))
2690 && dependent_scope_p (TREE_OPERAND (name, 0))))
2692 dependent:
2693 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2694 orig_object, orig_name, NULL_TREE);
2696 object = build_non_dependent_expr (object);
2698 else if (c_dialect_objc ()
2699 && identifier_p (name)
2700 && (expr = objc_maybe_build_component_ref (object, name)))
2701 return expr;
2703 /* [expr.ref]
2705 The type of the first expression shall be "class object" (of a
2706 complete type). */
2707 if (!currently_open_class (object_type)
2708 && !complete_type_or_maybe_complain (object_type, object, complain))
2709 return error_mark_node;
2710 if (!CLASS_TYPE_P (object_type))
2712 if (complain & tf_error)
2714 if (POINTER_TYPE_P (object_type)
2715 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2716 error ("request for member %qD in %qE, which is of pointer "
2717 "type %qT (maybe you meant to use %<->%> ?)",
2718 name, object.get_value (), object_type);
2719 else
2720 error ("request for member %qD in %qE, which is of non-class "
2721 "type %qT", name, object.get_value (), object_type);
2723 return error_mark_node;
2726 if (BASELINK_P (name))
2727 /* A member function that has already been looked up. */
2728 member = name;
2729 else
2731 bool is_template_id = false;
2732 tree template_args = NULL_TREE;
2733 tree scope = NULL_TREE;
2735 access_path = object_type;
2737 if (TREE_CODE (name) == SCOPE_REF)
2739 /* A qualified name. The qualifying class or namespace `S'
2740 has already been looked up; it is either a TYPE or a
2741 NAMESPACE_DECL. */
2742 scope = TREE_OPERAND (name, 0);
2743 name = TREE_OPERAND (name, 1);
2745 /* If SCOPE is a namespace, then the qualified name does not
2746 name a member of OBJECT_TYPE. */
2747 if (TREE_CODE (scope) == NAMESPACE_DECL)
2749 if (complain & tf_error)
2750 error ("%<%D::%D%> is not a member of %qT",
2751 scope, name, object_type);
2752 return error_mark_node;
2756 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2758 is_template_id = true;
2759 template_args = TREE_OPERAND (name, 1);
2760 name = TREE_OPERAND (name, 0);
2762 if (TREE_CODE (name) == OVERLOAD)
2763 name = DECL_NAME (get_first_fn (name));
2764 else if (DECL_P (name))
2765 name = DECL_NAME (name);
2768 if (scope)
2770 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2772 gcc_assert (!is_template_id);
2773 /* Looking up a member enumerator (c++/56793). */
2774 if (!TYPE_CLASS_SCOPE_P (scope)
2775 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2777 if (complain & tf_error)
2778 error ("%<%D::%D%> is not a member of %qT",
2779 scope, name, object_type);
2780 return error_mark_node;
2782 tree val = lookup_enumerator (scope, name);
2783 if (!val)
2785 if (complain & tf_error)
2786 error ("%qD is not a member of %qD",
2787 name, scope);
2788 return error_mark_node;
2791 if (TREE_SIDE_EFFECTS (object))
2792 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2793 return val;
2796 gcc_assert (CLASS_TYPE_P (scope));
2797 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2799 if (constructor_name_p (name, scope))
2801 if (complain & tf_error)
2802 error ("cannot call constructor %<%T::%D%> directly",
2803 scope, name);
2804 return error_mark_node;
2807 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2808 access_path = lookup_base (object_type, scope, ba_check,
2809 NULL, complain);
2810 if (access_path == error_mark_node)
2811 return error_mark_node;
2812 if (!access_path)
2814 if (any_dependent_bases_p (object_type))
2815 goto dependent;
2816 if (complain & tf_error)
2817 error ("%qT is not a base of %qT", scope, object_type);
2818 return error_mark_node;
2822 if (TREE_CODE (name) == BIT_NOT_EXPR)
2824 if (dependent_type_p (object_type))
2825 /* The destructor isn't declared yet. */
2826 goto dependent;
2827 member = lookup_destructor (object, scope, name, complain);
2829 else
2831 /* Look up the member. */
2832 member = lookup_member (access_path, name, /*protect=*/1,
2833 /*want_type=*/false, complain);
2834 if (member == NULL_TREE)
2836 if (dependent_type_p (object_type))
2837 /* Try again at instantiation time. */
2838 goto dependent;
2839 if (complain & tf_error)
2841 tree guessed_id = lookup_member_fuzzy (access_path, name,
2842 /*want_type=*/false);
2843 if (guessed_id)
2845 location_t bogus_component_loc = input_location;
2846 gcc_rich_location rich_loc (bogus_component_loc);
2847 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2848 guessed_id);
2849 error_at_rich_loc
2850 (&rich_loc,
2851 "%q#T has no member named %qE; did you mean %qE?",
2852 TREE_CODE (access_path) == TREE_BINFO
2853 ? TREE_TYPE (access_path) : object_type, name,
2854 guessed_id);
2856 else
2857 error ("%q#T has no member named %qE",
2858 TREE_CODE (access_path) == TREE_BINFO
2859 ? TREE_TYPE (access_path) : object_type, name);
2861 return error_mark_node;
2863 if (member == error_mark_node)
2864 return error_mark_node;
2865 if (DECL_P (member)
2866 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2867 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2868 wrong, so don't use it. */
2869 goto dependent;
2870 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2871 goto dependent;
2874 if (is_template_id)
2876 tree templ = member;
2878 if (BASELINK_P (templ))
2879 member = lookup_template_function (templ, template_args);
2880 else if (variable_template_p (templ))
2881 member = (lookup_and_finish_template_variable
2882 (templ, template_args, complain));
2883 else
2885 if (complain & tf_error)
2886 error ("%qD is not a member template function", name);
2887 return error_mark_node;
2892 if (TREE_DEPRECATED (member))
2893 warn_deprecated_use (member, NULL_TREE);
2895 if (template_p)
2896 check_template_keyword (member);
2898 expr = build_class_member_access_expr (object, member, access_path,
2899 /*preserve_reference=*/false,
2900 complain);
2901 if (processing_template_decl && expr != error_mark_node)
2903 if (BASELINK_P (member))
2905 if (TREE_CODE (orig_name) == SCOPE_REF)
2906 BASELINK_QUALIFIED_P (member) = 1;
2907 orig_name = member;
2909 return build_min_non_dep (COMPONENT_REF, expr,
2910 orig_object, orig_name,
2911 NULL_TREE);
2914 return expr;
2917 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2918 type. */
2920 tree
2921 build_simple_component_ref (tree object, tree member)
2923 tree type = cp_build_qualified_type (TREE_TYPE (member),
2924 cp_type_quals (TREE_TYPE (object)));
2925 return build3_loc (input_location,
2926 COMPONENT_REF, type,
2927 object, member, NULL_TREE);
2930 /* Return an expression for the MEMBER_NAME field in the internal
2931 representation of PTRMEM, a pointer-to-member function. (Each
2932 pointer-to-member function type gets its own RECORD_TYPE so it is
2933 more convenient to access the fields by name than by FIELD_DECL.)
2934 This routine converts the NAME to a FIELD_DECL and then creates the
2935 node for the complete expression. */
2937 tree
2938 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2940 tree ptrmem_type;
2941 tree member;
2943 /* This code is a stripped down version of
2944 build_class_member_access_expr. It does not work to use that
2945 routine directly because it expects the object to be of class
2946 type. */
2947 ptrmem_type = TREE_TYPE (ptrmem);
2948 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2949 for (member = TYPE_FIELDS (ptrmem_type); member;
2950 member = DECL_CHAIN (member))
2951 if (DECL_NAME (member) == member_name)
2952 break;
2953 tree res = build_simple_component_ref (ptrmem, member);
2955 TREE_NO_WARNING (res) = 1;
2956 return res;
2959 /* Given an expression PTR for a pointer, return an expression
2960 for the value pointed to.
2961 ERRORSTRING is the name of the operator to appear in error messages.
2963 This function may need to overload OPERATOR_FNNAME.
2964 Must also handle REFERENCE_TYPEs for C++. */
2966 tree
2967 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2968 tsubst_flags_t complain)
2970 tree orig_expr = expr;
2971 tree rval;
2972 tree overload = NULL_TREE;
2974 if (processing_template_decl)
2976 /* Retain the type if we know the operand is a pointer. */
2977 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2978 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2979 if (type_dependent_expression_p (expr))
2980 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2981 expr = build_non_dependent_expr (expr);
2984 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2985 NULL_TREE, NULL_TREE, &overload, complain);
2986 if (!rval)
2987 rval = cp_build_indirect_ref (expr, errorstring, complain);
2989 if (processing_template_decl && rval != error_mark_node)
2991 if (overload != NULL_TREE)
2992 return (build_min_non_dep_op_overload
2993 (INDIRECT_REF, rval, overload, orig_expr));
2995 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2997 else
2998 return rval;
3001 /* Helper function called from c-common. */
3002 tree
3003 build_indirect_ref (location_t /*loc*/,
3004 tree ptr, ref_operator errorstring)
3006 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
3009 tree
3010 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3011 tsubst_flags_t complain)
3013 tree pointer, type;
3015 if (ptr == current_class_ptr
3016 || (TREE_CODE (ptr) == NOP_EXPR
3017 && TREE_OPERAND (ptr, 0) == current_class_ptr
3018 && (same_type_ignoring_top_level_qualifiers_p
3019 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3020 return current_class_ref;
3022 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
3023 ? ptr : decay_conversion (ptr, complain));
3024 if (pointer == error_mark_node)
3025 return error_mark_node;
3027 type = TREE_TYPE (pointer);
3029 if (POINTER_TYPE_P (type))
3031 /* [expr.unary.op]
3033 If the type of the expression is "pointer to T," the type
3034 of the result is "T." */
3035 tree t = TREE_TYPE (type);
3037 if ((CONVERT_EXPR_P (ptr)
3038 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3039 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3041 /* If a warning is issued, mark it to avoid duplicates from
3042 the backend. This only needs to be done at
3043 warn_strict_aliasing > 2. */
3044 if (warn_strict_aliasing > 2)
3045 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
3046 type, TREE_OPERAND (ptr, 0)))
3047 TREE_NO_WARNING (ptr) = 1;
3050 if (VOID_TYPE_P (t))
3052 /* A pointer to incomplete type (other than cv void) can be
3053 dereferenced [expr.unary.op]/1 */
3054 if (complain & tf_error)
3055 error ("%qT is not a pointer-to-object type", type);
3056 return error_mark_node;
3058 else if (TREE_CODE (pointer) == ADDR_EXPR
3059 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3060 /* The POINTER was something like `&x'. We simplify `*&x' to
3061 `x'. */
3062 return TREE_OPERAND (pointer, 0);
3063 else
3065 tree ref = build1 (INDIRECT_REF, t, pointer);
3067 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3068 so that we get the proper error message if the result is used
3069 to assign to. Also, &* is supposed to be a no-op. */
3070 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3071 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3072 TREE_SIDE_EFFECTS (ref)
3073 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3074 return ref;
3077 else if (!(complain & tf_error))
3078 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3080 /* `pointer' won't be an error_mark_node if we were given a
3081 pointer to member, so it's cool to check for this here. */
3082 else if (TYPE_PTRMEM_P (type))
3083 switch (errorstring)
3085 case RO_ARRAY_INDEXING:
3086 error ("invalid use of array indexing on pointer to member");
3087 break;
3088 case RO_UNARY_STAR:
3089 error ("invalid use of unary %<*%> on pointer to member");
3090 break;
3091 case RO_IMPLICIT_CONVERSION:
3092 error ("invalid use of implicit conversion on pointer to member");
3093 break;
3094 case RO_ARROW_STAR:
3095 error ("left hand operand of %<->*%> must be a pointer to class, "
3096 "but is a pointer to member of type %qT", type);
3097 break;
3098 default:
3099 gcc_unreachable ();
3101 else if (pointer != error_mark_node)
3102 invalid_indirection_error (input_location, type, errorstring);
3104 return error_mark_node;
3107 /* This handles expressions of the form "a[i]", which denotes
3108 an array reference.
3110 This is logically equivalent in C to *(a+i), but we may do it differently.
3111 If A is a variable or a member, we generate a primitive ARRAY_REF.
3112 This avoids forcing the array out of registers, and can work on
3113 arrays that are not lvalues (for example, members of structures returned
3114 by functions).
3116 If INDEX is of some user-defined type, it must be converted to
3117 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3118 will inherit the type of the array, which will be some pointer type.
3120 LOC is the location to use in building the array reference. */
3122 tree
3123 cp_build_array_ref (location_t loc, tree array, tree idx,
3124 tsubst_flags_t complain)
3126 tree ret;
3128 if (idx == 0)
3130 if (complain & tf_error)
3131 error_at (loc, "subscript missing in array reference");
3132 return error_mark_node;
3135 /* If an array's index is an array notation, then its rank cannot be
3136 greater than one. */
3137 if (flag_cilkplus && contains_array_notation_expr (idx))
3139 size_t rank = 0;
3141 /* If find_rank returns false, then it should have reported an error,
3142 thus it is unnecessary for repetition. */
3143 if (!find_rank (loc, idx, idx, true, &rank))
3144 return error_mark_node;
3145 if (rank > 1)
3147 error_at (loc, "rank of the array%'s index is greater than 1");
3148 return error_mark_node;
3151 if (TREE_TYPE (array) == error_mark_node
3152 || TREE_TYPE (idx) == error_mark_node)
3153 return error_mark_node;
3155 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3156 inside it. */
3157 switch (TREE_CODE (array))
3159 case COMPOUND_EXPR:
3161 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3162 complain);
3163 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3164 TREE_OPERAND (array, 0), value);
3165 SET_EXPR_LOCATION (ret, loc);
3166 return ret;
3169 case COND_EXPR:
3170 ret = build_conditional_expr
3171 (loc, TREE_OPERAND (array, 0),
3172 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3173 complain),
3174 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3175 complain),
3176 complain);
3177 protected_set_expr_location (ret, loc);
3178 return ret;
3180 default:
3181 break;
3184 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3186 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3188 tree rval, type;
3190 warn_array_subscript_with_type_char (loc, idx);
3192 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3194 if (complain & tf_error)
3195 error_at (loc, "array subscript is not an integer");
3196 return error_mark_node;
3199 /* Apply integral promotions *after* noticing character types.
3200 (It is unclear why we do these promotions -- the standard
3201 does not say that we should. In fact, the natural thing would
3202 seem to be to convert IDX to ptrdiff_t; we're performing
3203 pointer arithmetic.) */
3204 idx = cp_perform_integral_promotions (idx, complain);
3206 /* An array that is indexed by a non-constant
3207 cannot be stored in a register; we must be able to do
3208 address arithmetic on its address.
3209 Likewise an array of elements of variable size. */
3210 if (TREE_CODE (idx) != INTEGER_CST
3211 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3212 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3213 != INTEGER_CST)))
3215 if (!cxx_mark_addressable (array))
3216 return error_mark_node;
3219 /* An array that is indexed by a constant value which is not within
3220 the array bounds cannot be stored in a register either; because we
3221 would get a crash in store_bit_field/extract_bit_field when trying
3222 to access a non-existent part of the register. */
3223 if (TREE_CODE (idx) == INTEGER_CST
3224 && TYPE_DOMAIN (TREE_TYPE (array))
3225 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3227 if (!cxx_mark_addressable (array))
3228 return error_mark_node;
3231 /* Note in C++ it is valid to subscript a `register' array, since
3232 it is valid to take the address of something with that
3233 storage specification. */
3234 if (extra_warnings)
3236 tree foo = array;
3237 while (TREE_CODE (foo) == COMPONENT_REF)
3238 foo = TREE_OPERAND (foo, 0);
3239 if (VAR_P (foo) && DECL_REGISTER (foo)
3240 && (complain & tf_warning))
3241 warning_at (loc, OPT_Wextra,
3242 "subscripting array declared %<register%>");
3245 type = TREE_TYPE (TREE_TYPE (array));
3246 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3247 /* Array ref is const/volatile if the array elements are
3248 or if the array is.. */
3249 TREE_READONLY (rval)
3250 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3251 TREE_SIDE_EFFECTS (rval)
3252 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3253 TREE_THIS_VOLATILE (rval)
3254 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3255 ret = require_complete_type_sfinae (rval, complain);
3256 protected_set_expr_location (ret, loc);
3257 if (non_lvalue)
3258 ret = non_lvalue_loc (loc, ret);
3259 return ret;
3263 tree ar = cp_default_conversion (array, complain);
3264 tree ind = cp_default_conversion (idx, complain);
3266 /* Put the integer in IND to simplify error checking. */
3267 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3268 std::swap (ar, ind);
3270 if (ar == error_mark_node || ind == error_mark_node)
3271 return error_mark_node;
3273 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3275 if (complain & tf_error)
3276 error_at (loc, "subscripted value is neither array nor pointer");
3277 return error_mark_node;
3279 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3281 if (complain & tf_error)
3282 error_at (loc, "array subscript is not an integer");
3283 return error_mark_node;
3286 warn_array_subscript_with_type_char (loc, idx);
3288 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3289 PLUS_EXPR, ar, ind,
3290 complain),
3291 RO_ARRAY_INDEXING,
3292 complain);
3293 protected_set_expr_location (ret, loc);
3294 if (non_lvalue)
3295 ret = non_lvalue_loc (loc, ret);
3296 return ret;
3300 /* Entry point for Obj-C++. */
3302 tree
3303 build_array_ref (location_t loc, tree array, tree idx)
3305 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3308 /* Resolve a pointer to member function. INSTANCE is the object
3309 instance to use, if the member points to a virtual member.
3311 This used to avoid checking for virtual functions if basetype
3312 has no virtual functions, according to an earlier ANSI draft.
3313 With the final ISO C++ rules, such an optimization is
3314 incorrect: A pointer to a derived member can be static_cast
3315 to pointer-to-base-member, as long as the dynamic object
3316 later has the right member. So now we only do this optimization
3317 when we know the dynamic type of the object. */
3319 tree
3320 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3321 tsubst_flags_t complain)
3323 if (TREE_CODE (function) == OFFSET_REF)
3324 function = TREE_OPERAND (function, 1);
3326 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3328 tree idx, delta, e1, e2, e3, vtbl;
3329 bool nonvirtual;
3330 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3331 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3333 tree instance_ptr = *instance_ptrptr;
3334 tree instance_save_expr = 0;
3335 if (instance_ptr == error_mark_node)
3337 if (TREE_CODE (function) == PTRMEM_CST)
3339 /* Extracting the function address from a pmf is only
3340 allowed with -Wno-pmf-conversions. It only works for
3341 pmf constants. */
3342 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3343 e1 = convert (fntype, e1);
3344 return e1;
3346 else
3348 if (complain & tf_error)
3349 error ("object missing in use of %qE", function);
3350 return error_mark_node;
3354 /* True if we know that the dynamic type of the object doesn't have
3355 virtual functions, so we can assume the PFN field is a pointer. */
3356 nonvirtual = (COMPLETE_TYPE_P (basetype)
3357 && !TYPE_POLYMORPHIC_P (basetype)
3358 && resolves_to_fixed_type_p (instance_ptr, 0));
3360 /* If we don't really have an object (i.e. in an ill-formed
3361 conversion from PMF to pointer), we can't resolve virtual
3362 functions anyway. */
3363 if (!nonvirtual && is_dummy_object (instance_ptr))
3364 nonvirtual = true;
3366 if (TREE_SIDE_EFFECTS (instance_ptr))
3367 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3369 if (TREE_SIDE_EFFECTS (function))
3370 function = save_expr (function);
3372 /* Start by extracting all the information from the PMF itself. */
3373 e3 = pfn_from_ptrmemfunc (function);
3374 delta = delta_from_ptrmemfunc (function);
3375 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3376 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3378 int flag_sanitize_save;
3379 case ptrmemfunc_vbit_in_pfn:
3380 e1 = cp_build_binary_op (input_location,
3381 BIT_AND_EXPR, idx, integer_one_node,
3382 complain);
3383 idx = cp_build_binary_op (input_location,
3384 MINUS_EXPR, idx, integer_one_node,
3385 complain);
3386 if (idx == error_mark_node)
3387 return error_mark_node;
3388 break;
3390 case ptrmemfunc_vbit_in_delta:
3391 e1 = cp_build_binary_op (input_location,
3392 BIT_AND_EXPR, delta, integer_one_node,
3393 complain);
3394 /* Don't instrument the RSHIFT_EXPR we're about to create because
3395 we're going to use DELTA number of times, and that wouldn't play
3396 well with SAVE_EXPRs therein. */
3397 flag_sanitize_save = flag_sanitize;
3398 flag_sanitize = 0;
3399 delta = cp_build_binary_op (input_location,
3400 RSHIFT_EXPR, delta, integer_one_node,
3401 complain);
3402 flag_sanitize = flag_sanitize_save;
3403 if (delta == error_mark_node)
3404 return error_mark_node;
3405 break;
3407 default:
3408 gcc_unreachable ();
3411 if (e1 == error_mark_node)
3412 return error_mark_node;
3414 /* Convert down to the right base before using the instance. A
3415 special case is that in a pointer to member of class C, C may
3416 be incomplete. In that case, the function will of course be
3417 a member of C, and no conversion is required. In fact,
3418 lookup_base will fail in that case, because incomplete
3419 classes do not have BINFOs. */
3420 if (!same_type_ignoring_top_level_qualifiers_p
3421 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3423 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3424 basetype, ba_check, NULL, complain);
3425 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3426 1, complain);
3427 if (instance_ptr == error_mark_node)
3428 return error_mark_node;
3430 /* ...and then the delta in the PMF. */
3431 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3433 /* Hand back the adjusted 'this' argument to our caller. */
3434 *instance_ptrptr = instance_ptr;
3436 if (nonvirtual)
3437 /* Now just return the pointer. */
3438 return e3;
3440 /* Next extract the vtable pointer from the object. */
3441 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3442 instance_ptr);
3443 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3444 if (vtbl == error_mark_node)
3445 return error_mark_node;
3447 /* Finally, extract the function pointer from the vtable. */
3448 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3449 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3450 if (e2 == error_mark_node)
3451 return error_mark_node;
3452 TREE_CONSTANT (e2) = 1;
3454 /* When using function descriptors, the address of the
3455 vtable entry is treated as a function pointer. */
3456 if (TARGET_VTABLE_USES_DESCRIPTORS)
3457 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3458 cp_build_addr_expr (e2, complain));
3460 e2 = fold_convert (TREE_TYPE (e3), e2);
3461 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3462 if (e1 == error_mark_node)
3463 return error_mark_node;
3465 /* Make sure this doesn't get evaluated first inside one of the
3466 branches of the COND_EXPR. */
3467 if (instance_save_expr)
3468 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3469 instance_save_expr, e1);
3471 function = e1;
3473 return function;
3476 /* Used by the C-common bits. */
3477 tree
3478 build_function_call (location_t /*loc*/,
3479 tree function, tree params)
3481 return cp_build_function_call (function, params, tf_warning_or_error);
3484 /* Used by the C-common bits. */
3485 tree
3486 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3487 tree function, vec<tree, va_gc> *params,
3488 vec<tree, va_gc> * /*origtypes*/)
3490 vec<tree, va_gc> *orig_params = params;
3491 tree ret = cp_build_function_call_vec (function, &params,
3492 tf_warning_or_error);
3494 /* cp_build_function_call_vec can reallocate PARAMS by adding
3495 default arguments. That should never happen here. Verify
3496 that. */
3497 gcc_assert (params == orig_params);
3499 return ret;
3502 /* Build a function call using a tree list of arguments. */
3504 static tree
3505 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3507 vec<tree, va_gc> *vec;
3508 tree ret;
3510 vec = make_tree_vector ();
3511 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3512 vec_safe_push (vec, TREE_VALUE (params));
3513 ret = cp_build_function_call_vec (function, &vec, complain);
3514 release_tree_vector (vec);
3515 return ret;
3518 /* Build a function call using varargs. */
3520 tree
3521 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3523 vec<tree, va_gc> *vec;
3524 va_list args;
3525 tree ret, t;
3527 vec = make_tree_vector ();
3528 va_start (args, complain);
3529 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3530 vec_safe_push (vec, t);
3531 va_end (args);
3532 ret = cp_build_function_call_vec (function, &vec, complain);
3533 release_tree_vector (vec);
3534 return ret;
3537 /* Build a function call using a vector of arguments. PARAMS may be
3538 NULL if there are no parameters. This changes the contents of
3539 PARAMS. */
3541 tree
3542 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3543 tsubst_flags_t complain)
3545 tree fntype, fndecl;
3546 int is_method;
3547 tree original = function;
3548 int nargs;
3549 tree *argarray;
3550 tree parm_types;
3551 vec<tree, va_gc> *allocated = NULL;
3552 tree ret;
3554 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3555 expressions, like those used for ObjC messenger dispatches. */
3556 if (params != NULL && !vec_safe_is_empty (*params))
3557 function = objc_rewrite_function_call (function, (**params)[0]);
3559 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3560 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3561 if (TREE_CODE (function) == NOP_EXPR
3562 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3563 function = TREE_OPERAND (function, 0);
3565 if (TREE_CODE (function) == FUNCTION_DECL)
3567 /* If the function is a non-template member function
3568 or a non-template friend, then we need to check the
3569 constraints.
3571 Note that if overload resolution failed with a single
3572 candidate this function will be used to explicitly diagnose
3573 the failure for the single call expression. The check is
3574 technically redundant since we also would have failed in
3575 add_function_candidate. */
3576 if (flag_concepts
3577 && (complain & tf_error)
3578 && !constraints_satisfied_p (function))
3580 error ("cannot call function %qD", function);
3581 location_t loc = DECL_SOURCE_LOCATION (function);
3582 diagnose_constraints (loc, function, NULL_TREE);
3583 return error_mark_node;
3586 if (!mark_used (function, complain) && !(complain & tf_error))
3587 return error_mark_node;
3588 fndecl = function;
3590 /* Convert anything with function type to a pointer-to-function. */
3591 if (DECL_MAIN_P (function))
3593 if (complain & tf_error)
3594 pedwarn (input_location, OPT_Wpedantic,
3595 "ISO C++ forbids calling %<::main%> from within program");
3596 else
3597 return error_mark_node;
3599 function = build_addr_func (function, complain);
3601 else
3603 fndecl = NULL_TREE;
3605 function = build_addr_func (function, complain);
3608 if (function == error_mark_node)
3609 return error_mark_node;
3611 fntype = TREE_TYPE (function);
3613 if (TYPE_PTRMEMFUNC_P (fntype))
3615 if (complain & tf_error)
3616 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3617 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3618 original, original);
3619 return error_mark_node;
3622 is_method = (TYPE_PTR_P (fntype)
3623 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3625 if (!(TYPE_PTRFN_P (fntype)
3626 || is_method
3627 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3629 if (complain & tf_error)
3631 if (!flag_diagnostics_show_caret)
3632 error_at (input_location,
3633 "%qE cannot be used as a function", original);
3634 else if (DECL_P (original))
3635 error_at (input_location,
3636 "%qD cannot be used as a function", original);
3637 else
3638 error_at (input_location,
3639 "expression cannot be used as a function");
3642 return error_mark_node;
3645 /* fntype now gets the type of function pointed to. */
3646 fntype = TREE_TYPE (fntype);
3647 parm_types = TYPE_ARG_TYPES (fntype);
3649 if (params == NULL)
3651 allocated = make_tree_vector ();
3652 params = &allocated;
3655 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3656 complain);
3657 if (nargs < 0)
3658 return error_mark_node;
3660 argarray = (*params)->address ();
3662 /* Check for errors in format strings and inappropriately
3663 null parameters. */
3664 bool warned_p = check_function_arguments (input_location, fntype,
3665 nargs, argarray);
3667 ret = build_cxx_call (function, nargs, argarray, complain);
3669 if (warned_p)
3671 tree c = extract_call_expr (ret);
3672 if (TREE_CODE (c) == CALL_EXPR)
3673 TREE_NO_WARNING (c) = 1;
3676 if (allocated != NULL)
3677 release_tree_vector (allocated);
3679 return ret;
3682 /* Subroutine of convert_arguments.
3683 Print an error message about a wrong number of arguments. */
3685 static void
3686 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3688 if (fndecl)
3690 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3692 if (DECL_NAME (fndecl) == NULL_TREE
3693 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3694 error_at (loc,
3695 too_many_p
3696 ? G_("too many arguments to constructor %q#D")
3697 : G_("too few arguments to constructor %q#D"),
3698 fndecl);
3699 else
3700 error_at (loc,
3701 too_many_p
3702 ? G_("too many arguments to member function %q#D")
3703 : G_("too few arguments to member function %q#D"),
3704 fndecl);
3706 else
3707 error_at (loc,
3708 too_many_p
3709 ? G_("too many arguments to function %q#D")
3710 : G_("too few arguments to function %q#D"),
3711 fndecl);
3712 if (!DECL_IS_BUILTIN (fndecl))
3713 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3715 else
3717 if (c_dialect_objc () && objc_message_selector ())
3718 error_at (loc,
3719 too_many_p
3720 ? G_("too many arguments to method %q#D")
3721 : G_("too few arguments to method %q#D"),
3722 objc_message_selector ());
3723 else
3724 error_at (loc, too_many_p ? G_("too many arguments to function")
3725 : G_("too few arguments to function"));
3729 /* Convert the actual parameter expressions in the list VALUES to the
3730 types in the list TYPELIST. The converted expressions are stored
3731 back in the VALUES vector.
3732 If parmdecls is exhausted, or when an element has NULL as its type,
3733 perform the default conversions.
3735 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3737 This is also where warnings about wrong number of args are generated.
3739 Returns the actual number of arguments processed (which might be less
3740 than the length of the vector), or -1 on error.
3742 In C++, unspecified trailing parameters can be filled in with their
3743 default arguments, if such were specified. Do so here. */
3745 static int
3746 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3747 int flags, tsubst_flags_t complain)
3749 tree typetail;
3750 unsigned int i;
3752 /* Argument passing is always copy-initialization. */
3753 flags |= LOOKUP_ONLYCONVERTING;
3755 for (i = 0, typetail = typelist;
3756 i < vec_safe_length (*values);
3757 i++)
3759 tree type = typetail ? TREE_VALUE (typetail) : 0;
3760 tree val = (**values)[i];
3762 if (val == error_mark_node || type == error_mark_node)
3763 return -1;
3765 if (type == void_type_node)
3767 if (complain & tf_error)
3769 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3770 return i;
3772 else
3773 return -1;
3776 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3777 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3778 if (TREE_CODE (val) == NOP_EXPR
3779 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3780 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3781 val = TREE_OPERAND (val, 0);
3783 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3785 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3786 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3787 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3788 val = decay_conversion (val, complain);
3791 if (val == error_mark_node)
3792 return -1;
3794 if (type != 0)
3796 /* Formal parm type is specified by a function prototype. */
3797 tree parmval;
3799 if (!COMPLETE_TYPE_P (complete_type (type)))
3801 if (complain & tf_error)
3803 if (fndecl)
3804 error ("parameter %P of %qD has incomplete type %qT",
3805 i, fndecl, type);
3806 else
3807 error ("parameter %P has incomplete type %qT", i, type);
3809 parmval = error_mark_node;
3811 else
3813 parmval = convert_for_initialization
3814 (NULL_TREE, type, val, flags,
3815 ICR_ARGPASS, fndecl, i, complain);
3816 parmval = convert_for_arg_passing (type, parmval, complain);
3819 if (parmval == error_mark_node)
3820 return -1;
3822 (**values)[i] = parmval;
3824 else
3826 if (fndecl && magic_varargs_p (fndecl))
3827 /* Don't do ellipsis conversion for __built_in_constant_p
3828 as this will result in spurious errors for non-trivial
3829 types. */
3830 val = require_complete_type_sfinae (val, complain);
3831 else
3832 val = convert_arg_to_ellipsis (val, complain);
3834 (**values)[i] = val;
3837 if (typetail)
3838 typetail = TREE_CHAIN (typetail);
3841 if (typetail != 0 && typetail != void_list_node)
3843 /* See if there are default arguments that can be used. Because
3844 we hold default arguments in the FUNCTION_TYPE (which is so
3845 wrong), we can see default parameters here from deduced
3846 contexts (and via typeof) for indirect function calls.
3847 Fortunately we know whether we have a function decl to
3848 provide default arguments in a language conformant
3849 manner. */
3850 if (fndecl && TREE_PURPOSE (typetail)
3851 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3853 for (; typetail != void_list_node; ++i)
3855 /* After DR777, with explicit template args we can end up with a
3856 default argument followed by no default argument. */
3857 if (!TREE_PURPOSE (typetail))
3858 break;
3859 tree parmval
3860 = convert_default_arg (TREE_VALUE (typetail),
3861 TREE_PURPOSE (typetail),
3862 fndecl, i, complain);
3864 if (parmval == error_mark_node)
3865 return -1;
3867 vec_safe_push (*values, parmval);
3868 typetail = TREE_CHAIN (typetail);
3869 /* ends with `...'. */
3870 if (typetail == NULL_TREE)
3871 break;
3875 if (typetail && typetail != void_list_node)
3877 if (complain & tf_error)
3878 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3879 return -1;
3883 return (int) i;
3886 /* Build a binary-operation expression, after performing default
3887 conversions on the operands. CODE is the kind of expression to
3888 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3889 are the tree codes which correspond to ARG1 and ARG2 when issuing
3890 warnings about possibly misplaced parentheses. They may differ
3891 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3892 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3893 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3894 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3895 ARG2_CODE as ERROR_MARK. */
3897 tree
3898 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3899 enum tree_code arg1_code, tree arg2,
3900 enum tree_code arg2_code, tree *overload_p,
3901 tsubst_flags_t complain)
3903 tree orig_arg1;
3904 tree orig_arg2;
3905 tree expr;
3906 tree overload = NULL_TREE;
3908 orig_arg1 = arg1;
3909 orig_arg2 = arg2;
3911 if (processing_template_decl)
3913 if (type_dependent_expression_p (arg1)
3914 || type_dependent_expression_p (arg2))
3915 return build_min_nt_loc (loc, code, arg1, arg2);
3916 arg1 = build_non_dependent_expr (arg1);
3917 arg2 = build_non_dependent_expr (arg2);
3920 if (code == DOTSTAR_EXPR)
3921 expr = build_m_component_ref (arg1, arg2, complain);
3922 else
3923 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3924 &overload, complain);
3926 if (overload_p != NULL)
3927 *overload_p = overload;
3929 /* Check for cases such as x+y<<z which users are likely to
3930 misinterpret. But don't warn about obj << x + y, since that is a
3931 common idiom for I/O. */
3932 if (warn_parentheses
3933 && (complain & tf_warning)
3934 && !processing_template_decl
3935 && !error_operand_p (arg1)
3936 && !error_operand_p (arg2)
3937 && (code != LSHIFT_EXPR
3938 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3939 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3940 arg2_code, orig_arg2);
3942 if (processing_template_decl && expr != error_mark_node)
3944 if (overload != NULL_TREE)
3945 return (build_min_non_dep_op_overload
3946 (code, expr, overload, orig_arg1, orig_arg2));
3948 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3951 return expr;
3954 /* Build and return an ARRAY_REF expression. */
3956 tree
3957 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3958 tsubst_flags_t complain)
3960 tree orig_arg1 = arg1;
3961 tree orig_arg2 = arg2;
3962 tree expr;
3963 tree overload = NULL_TREE;
3965 if (processing_template_decl)
3967 if (type_dependent_expression_p (arg1)
3968 || type_dependent_expression_p (arg2))
3969 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3970 NULL_TREE, NULL_TREE);
3971 arg1 = build_non_dependent_expr (arg1);
3972 arg2 = build_non_dependent_expr (arg2);
3975 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3976 NULL_TREE, &overload, complain);
3978 if (processing_template_decl && expr != error_mark_node)
3980 if (overload != NULL_TREE)
3981 return (build_min_non_dep_op_overload
3982 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
3984 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3985 NULL_TREE, NULL_TREE);
3987 return expr;
3990 /* Return whether OP is an expression of enum type cast to integer
3991 type. In C++ even unsigned enum types are cast to signed integer
3992 types. We do not want to issue warnings about comparisons between
3993 signed and unsigned types when one of the types is an enum type.
3994 Those warnings are always false positives in practice. */
3996 static bool
3997 enum_cast_to_int (tree op)
3999 if (CONVERT_EXPR_P (op)
4000 && TREE_TYPE (op) == integer_type_node
4001 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4002 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4003 return true;
4005 /* The cast may have been pushed into a COND_EXPR. */
4006 if (TREE_CODE (op) == COND_EXPR)
4007 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4008 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4010 return false;
4013 /* For the c-common bits. */
4014 tree
4015 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4016 int /*convert_p*/)
4018 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4021 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4022 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4024 static tree
4025 build_vec_cmp (tree_code code, tree type,
4026 tree arg0, tree arg1)
4028 tree zero_vec = build_zero_cst (type);
4029 tree minus_one_vec = build_minus_one_cst (type);
4030 tree cmp_type = build_same_sized_truth_vector_type(type);
4031 tree cmp = build2 (code, cmp_type, arg0, arg1);
4032 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4035 /* Possibly warn about an address never being NULL. */
4037 static void
4038 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4040 if (!warn_address
4041 || (complain & tf_warning) == 0
4042 || c_inhibit_evaluation_warnings != 0
4043 || TREE_NO_WARNING (op))
4044 return;
4046 tree cop = fold_non_dependent_expr (op);
4048 if (TREE_CODE (cop) == ADDR_EXPR
4049 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4050 && !TREE_NO_WARNING (cop))
4051 warning_at (location, OPT_Waddress, "the address of %qD will never "
4052 "be NULL", TREE_OPERAND (cop, 0));
4054 if (CONVERT_EXPR_P (op)
4055 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4057 tree inner_op = op;
4058 STRIP_NOPS (inner_op);
4060 if (DECL_P (inner_op))
4061 warning_at (location, OPT_Waddress,
4062 "the compiler can assume that the address of "
4063 "%qD will never be NULL", inner_op);
4067 /* Build a binary-operation expression without default conversions.
4068 CODE is the kind of expression to build.
4069 LOCATION is the location_t of the operator in the source code.
4070 This function differs from `build' in several ways:
4071 the data type of the result is computed and recorded in it,
4072 warnings are generated if arg data types are invalid,
4073 special handling for addition and subtraction of pointers is known,
4074 and some optimization is done (operations on narrow ints
4075 are done in the narrower type when that gives the same result).
4076 Constant folding is also done before the result is returned.
4078 Note that the operands will never have enumeral types
4079 because either they have just had the default conversions performed
4080 or they have both just been converted to some other type in which
4081 the arithmetic is to be done.
4083 C++: must do special pointer arithmetic when implementing
4084 multiple inheritance, and deal with pointer to member functions. */
4086 tree
4087 cp_build_binary_op (location_t location,
4088 enum tree_code code, tree orig_op0, tree orig_op1,
4089 tsubst_flags_t complain)
4091 tree op0, op1;
4092 enum tree_code code0, code1;
4093 tree type0, type1;
4094 const char *invalid_op_diag;
4096 /* Expression code to give to the expression when it is built.
4097 Normally this is CODE, which is what the caller asked for,
4098 but in some special cases we change it. */
4099 enum tree_code resultcode = code;
4101 /* Data type in which the computation is to be performed.
4102 In the simplest cases this is the common type of the arguments. */
4103 tree result_type = NULL;
4105 /* Nonzero means operands have already been type-converted
4106 in whatever way is necessary.
4107 Zero means they need to be converted to RESULT_TYPE. */
4108 int converted = 0;
4110 /* Nonzero means create the expression with this type, rather than
4111 RESULT_TYPE. */
4112 tree build_type = 0;
4114 /* Nonzero means after finally constructing the expression
4115 convert it to this type. */
4116 tree final_type = 0;
4118 tree result, result_ovl;
4119 tree orig_type = NULL;
4121 /* Nonzero if this is an operation like MIN or MAX which can
4122 safely be computed in short if both args are promoted shorts.
4123 Also implies COMMON.
4124 -1 indicates a bitwise operation; this makes a difference
4125 in the exact conditions for when it is safe to do the operation
4126 in a narrower mode. */
4127 int shorten = 0;
4129 /* Nonzero if this is a comparison operation;
4130 if both args are promoted shorts, compare the original shorts.
4131 Also implies COMMON. */
4132 int short_compare = 0;
4134 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4135 int common = 0;
4137 /* True if both operands have arithmetic type. */
4138 bool arithmetic_types_p;
4140 /* Apply default conversions. */
4141 op0 = orig_op0;
4142 op1 = orig_op1;
4144 /* Remember whether we're doing / or %. */
4145 bool doing_div_or_mod = false;
4147 /* Remember whether we're doing << or >>. */
4148 bool doing_shift = false;
4150 /* Tree holding instrumentation expression. */
4151 tree instrument_expr = NULL;
4153 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4154 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4155 || code == TRUTH_XOR_EXPR)
4157 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4158 op0 = decay_conversion (op0, complain);
4159 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4160 op1 = decay_conversion (op1, complain);
4162 else
4164 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4165 op0 = cp_default_conversion (op0, complain);
4166 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4167 op1 = cp_default_conversion (op1, complain);
4170 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4171 STRIP_TYPE_NOPS (op0);
4172 STRIP_TYPE_NOPS (op1);
4174 /* DTRT if one side is an overloaded function, but complain about it. */
4175 if (type_unknown_p (op0))
4177 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4178 if (t != error_mark_node)
4180 if (complain & tf_error)
4181 permerror (input_location, "assuming cast to type %qT from overloaded function",
4182 TREE_TYPE (t));
4183 op0 = t;
4186 if (type_unknown_p (op1))
4188 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4189 if (t != error_mark_node)
4191 if (complain & tf_error)
4192 permerror (input_location, "assuming cast to type %qT from overloaded function",
4193 TREE_TYPE (t));
4194 op1 = t;
4198 type0 = TREE_TYPE (op0);
4199 type1 = TREE_TYPE (op1);
4201 /* The expression codes of the data types of the arguments tell us
4202 whether the arguments are integers, floating, pointers, etc. */
4203 code0 = TREE_CODE (type0);
4204 code1 = TREE_CODE (type1);
4206 /* If an error was already reported for one of the arguments,
4207 avoid reporting another error. */
4208 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4209 return error_mark_node;
4211 if ((invalid_op_diag
4212 = targetm.invalid_binary_op (code, type0, type1)))
4214 if (complain & tf_error)
4215 error (invalid_op_diag);
4216 return error_mark_node;
4219 /* Issue warnings about peculiar, but valid, uses of NULL. */
4220 if ((orig_op0 == null_node || orig_op1 == null_node)
4221 /* It's reasonable to use pointer values as operands of &&
4222 and ||, so NULL is no exception. */
4223 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4224 && ( /* Both are NULL (or 0) and the operation was not a
4225 comparison or a pointer subtraction. */
4226 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4227 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4228 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4229 || (!null_ptr_cst_p (orig_op0)
4230 && !TYPE_PTR_OR_PTRMEM_P (type0))
4231 || (!null_ptr_cst_p (orig_op1)
4232 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4233 && (complain & tf_warning))
4235 source_location loc =
4236 expansion_point_location_if_in_system_header (input_location);
4238 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4241 /* In case when one of the operands of the binary operation is
4242 a vector and another is a scalar -- convert scalar to vector. */
4243 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4245 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4246 complain & tf_error);
4248 switch (convert_flag)
4250 case stv_error:
4251 return error_mark_node;
4252 case stv_firstarg:
4254 op0 = convert (TREE_TYPE (type1), op0);
4255 op0 = save_expr (op0);
4256 op0 = build_vector_from_val (type1, op0);
4257 type0 = TREE_TYPE (op0);
4258 code0 = TREE_CODE (type0);
4259 converted = 1;
4260 break;
4262 case stv_secondarg:
4264 op1 = convert (TREE_TYPE (type0), op1);
4265 op1 = save_expr (op1);
4266 op1 = build_vector_from_val (type0, op1);
4267 type1 = TREE_TYPE (op1);
4268 code1 = TREE_CODE (type1);
4269 converted = 1;
4270 break;
4272 default:
4273 break;
4277 switch (code)
4279 case MINUS_EXPR:
4280 /* Subtraction of two similar pointers.
4281 We must subtract them as integers, then divide by object size. */
4282 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4283 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4284 TREE_TYPE (type1)))
4285 return pointer_diff (location, op0, op1,
4286 common_pointer_type (type0, type1), complain);
4287 /* In all other cases except pointer - int, the usual arithmetic
4288 rules apply. */
4289 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4291 common = 1;
4292 break;
4294 /* The pointer - int case is just like pointer + int; fall
4295 through. */
4296 gcc_fallthrough ();
4297 case PLUS_EXPR:
4298 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4299 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4301 tree ptr_operand;
4302 tree int_operand;
4303 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4304 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4305 if (processing_template_decl)
4307 result_type = TREE_TYPE (ptr_operand);
4308 break;
4310 return cp_pointer_int_sum (location, code,
4311 ptr_operand,
4312 int_operand,
4313 complain);
4315 common = 1;
4316 break;
4318 case MULT_EXPR:
4319 common = 1;
4320 break;
4322 case TRUNC_DIV_EXPR:
4323 case CEIL_DIV_EXPR:
4324 case FLOOR_DIV_EXPR:
4325 case ROUND_DIV_EXPR:
4326 case EXACT_DIV_EXPR:
4327 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4328 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4329 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4330 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4332 enum tree_code tcode0 = code0, tcode1 = code1;
4333 tree cop1 = fold_non_dependent_expr (op1);
4334 doing_div_or_mod = true;
4335 warn_for_div_by_zero (location, cop1);
4337 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4338 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4339 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4340 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4342 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4343 resultcode = RDIV_EXPR;
4344 else
4345 /* When dividing two signed integers, we have to promote to int.
4346 unless we divide by a constant != -1. Note that default
4347 conversion will have been performed on the operands at this
4348 point, so we have to dig out the original type to find out if
4349 it was unsigned. */
4350 shorten = ((TREE_CODE (op0) == NOP_EXPR
4351 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4352 || (TREE_CODE (op1) == INTEGER_CST
4353 && ! integer_all_onesp (op1)));
4355 common = 1;
4357 break;
4359 case BIT_AND_EXPR:
4360 case BIT_IOR_EXPR:
4361 case BIT_XOR_EXPR:
4362 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4363 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4364 && !VECTOR_FLOAT_TYPE_P (type0)
4365 && !VECTOR_FLOAT_TYPE_P (type1)))
4366 shorten = -1;
4367 break;
4369 case TRUNC_MOD_EXPR:
4370 case FLOOR_MOD_EXPR:
4372 tree cop1 = fold_non_dependent_expr (op1);
4373 doing_div_or_mod = true;
4374 warn_for_div_by_zero (location, cop1);
4377 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4378 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4379 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4380 common = 1;
4381 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4383 /* Although it would be tempting to shorten always here, that loses
4384 on some targets, since the modulo instruction is undefined if the
4385 quotient can't be represented in the computation mode. We shorten
4386 only if unsigned or if dividing by something we know != -1. */
4387 shorten = ((TREE_CODE (op0) == NOP_EXPR
4388 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4389 || (TREE_CODE (op1) == INTEGER_CST
4390 && ! integer_all_onesp (op1)));
4391 common = 1;
4393 break;
4395 case TRUTH_ANDIF_EXPR:
4396 case TRUTH_ORIF_EXPR:
4397 case TRUTH_AND_EXPR:
4398 case TRUTH_OR_EXPR:
4399 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4401 if (!COMPARISON_CLASS_P (op1))
4402 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4403 build_zero_cst (type1), complain);
4404 if (code == TRUTH_ANDIF_EXPR)
4406 tree z = build_zero_cst (TREE_TYPE (op1));
4407 return build_conditional_expr (location, op0, op1, z, complain);
4409 else if (code == TRUTH_ORIF_EXPR)
4411 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4412 return build_conditional_expr (location, op0, m1, op1, complain);
4414 else
4415 gcc_unreachable ();
4417 if (VECTOR_TYPE_P (type0))
4419 if (!COMPARISON_CLASS_P (op0))
4420 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4421 build_zero_cst (type0), complain);
4422 if (!VECTOR_TYPE_P (type1))
4424 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4425 tree z = build_zero_cst (TREE_TYPE (op0));
4426 op1 = build_conditional_expr (location, op1, m1, z, complain);
4428 else if (!COMPARISON_CLASS_P (op1))
4429 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4430 build_zero_cst (type1), complain);
4432 if (code == TRUTH_ANDIF_EXPR)
4433 code = BIT_AND_EXPR;
4434 else if (code == TRUTH_ORIF_EXPR)
4435 code = BIT_IOR_EXPR;
4436 else
4437 gcc_unreachable ();
4439 return cp_build_binary_op (location, code, op0, op1, complain);
4442 result_type = boolean_type_node;
4443 break;
4445 /* Shift operations: result has same type as first operand;
4446 always convert second operand to int.
4447 Also set SHORT_SHIFT if shifting rightward. */
4449 case RSHIFT_EXPR:
4450 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4451 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4453 result_type = type0;
4454 converted = 1;
4456 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4457 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4458 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4459 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4461 result_type = type0;
4462 converted = 1;
4464 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4466 tree const_op1 = fold_non_dependent_expr (op1);
4467 if (TREE_CODE (const_op1) != INTEGER_CST)
4468 const_op1 = op1;
4469 result_type = type0;
4470 doing_shift = true;
4471 if (TREE_CODE (const_op1) == INTEGER_CST)
4473 if (tree_int_cst_lt (const_op1, integer_zero_node))
4475 if ((complain & tf_warning)
4476 && c_inhibit_evaluation_warnings == 0)
4477 warning (OPT_Wshift_count_negative,
4478 "right shift count is negative");
4480 else
4482 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4483 && (complain & tf_warning)
4484 && c_inhibit_evaluation_warnings == 0)
4485 warning (OPT_Wshift_count_overflow,
4486 "right shift count >= width of type");
4489 /* Avoid converting op1 to result_type later. */
4490 converted = 1;
4492 break;
4494 case LSHIFT_EXPR:
4495 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4496 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4498 result_type = type0;
4499 converted = 1;
4501 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4502 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4503 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4504 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4506 result_type = type0;
4507 converted = 1;
4509 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4511 tree const_op0 = fold_non_dependent_expr (op0);
4512 if (TREE_CODE (const_op0) != INTEGER_CST)
4513 const_op0 = op0;
4514 tree const_op1 = fold_non_dependent_expr (op1);
4515 if (TREE_CODE (const_op1) != INTEGER_CST)
4516 const_op1 = op1;
4517 result_type = type0;
4518 doing_shift = true;
4519 if (TREE_CODE (const_op0) == INTEGER_CST
4520 && tree_int_cst_sgn (const_op0) < 0
4521 && (complain & tf_warning)
4522 && c_inhibit_evaluation_warnings == 0)
4523 warning (OPT_Wshift_negative_value,
4524 "left shift of negative value");
4525 if (TREE_CODE (const_op1) == INTEGER_CST)
4527 if (tree_int_cst_lt (const_op1, integer_zero_node))
4529 if ((complain & tf_warning)
4530 && c_inhibit_evaluation_warnings == 0)
4531 warning (OPT_Wshift_count_negative,
4532 "left shift count is negative");
4534 else if (compare_tree_int (const_op1,
4535 TYPE_PRECISION (type0)) >= 0)
4537 if ((complain & tf_warning)
4538 && c_inhibit_evaluation_warnings == 0)
4539 warning (OPT_Wshift_count_overflow,
4540 "left shift count >= width of type");
4542 else if (TREE_CODE (const_op0) == INTEGER_CST
4543 && (complain & tf_warning))
4544 maybe_warn_shift_overflow (location, const_op0, const_op1);
4546 /* Avoid converting op1 to result_type later. */
4547 converted = 1;
4549 break;
4551 case RROTATE_EXPR:
4552 case LROTATE_EXPR:
4553 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4555 result_type = type0;
4556 if (TREE_CODE (op1) == INTEGER_CST)
4558 if (tree_int_cst_lt (op1, integer_zero_node))
4560 if (complain & tf_warning)
4561 warning (0, (code == LROTATE_EXPR)
4562 ? G_("left rotate count is negative")
4563 : G_("right rotate count is negative"));
4565 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4567 if (complain & tf_warning)
4568 warning (0, (code == LROTATE_EXPR)
4569 ? G_("left rotate count >= width of type")
4570 : G_("right rotate count >= width of type"));
4573 /* Convert the shift-count to an integer, regardless of
4574 size of value being shifted. */
4575 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4576 op1 = cp_convert (integer_type_node, op1, complain);
4578 break;
4580 case EQ_EXPR:
4581 case NE_EXPR:
4582 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4583 goto vector_compare;
4584 if ((complain & tf_warning)
4585 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4586 warning (OPT_Wfloat_equal,
4587 "comparing floating point with == or != is unsafe");
4588 if ((complain & tf_warning)
4589 && ((TREE_CODE (orig_op0) == STRING_CST
4590 && !integer_zerop (cp_fully_fold (op1)))
4591 || (TREE_CODE (orig_op1) == STRING_CST
4592 && !integer_zerop (cp_fully_fold (op0)))))
4593 warning (OPT_Waddress, "comparison with string literal results "
4594 "in unspecified behavior");
4596 build_type = boolean_type_node;
4597 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4598 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4599 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4600 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4601 short_compare = 1;
4602 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4603 && null_ptr_cst_p (orig_op1))
4604 /* Handle, eg, (void*)0 (c++/43906), and more. */
4605 || (code0 == POINTER_TYPE
4606 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4608 if (TYPE_PTR_P (type1))
4609 result_type = composite_pointer_type (type0, type1, op0, op1,
4610 CPO_COMPARISON, complain);
4611 else
4612 result_type = type0;
4614 if (char_type_p (TREE_TYPE (orig_op1))
4615 && warning (OPT_Wpointer_compare,
4616 "comparison between pointer and zero character "
4617 "constant"))
4618 inform (input_location,
4619 "did you mean to dereference the pointer?");
4620 warn_for_null_address (location, op0, complain);
4622 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4623 && null_ptr_cst_p (orig_op0))
4624 /* Handle, eg, (void*)0 (c++/43906), and more. */
4625 || (code1 == POINTER_TYPE
4626 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4628 if (TYPE_PTR_P (type0))
4629 result_type = composite_pointer_type (type0, type1, op0, op1,
4630 CPO_COMPARISON, complain);
4631 else
4632 result_type = type1;
4634 if (char_type_p (TREE_TYPE (orig_op0))
4635 && warning (OPT_Wpointer_compare,
4636 "comparison between pointer and zero character "
4637 "constant"))
4638 inform (input_location,
4639 "did you mean to dereference the pointer?");
4640 warn_for_null_address (location, op1, complain);
4642 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4643 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4644 result_type = composite_pointer_type (type0, type1, op0, op1,
4645 CPO_COMPARISON, complain);
4646 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4647 /* One of the operands must be of nullptr_t type. */
4648 result_type = TREE_TYPE (nullptr_node);
4649 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4651 result_type = type0;
4652 if (complain & tf_error)
4653 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4654 else
4655 return error_mark_node;
4657 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4659 result_type = type1;
4660 if (complain & tf_error)
4661 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4662 else
4663 return error_mark_node;
4665 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4667 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4668 == ptrmemfunc_vbit_in_delta)
4670 tree pfn0, delta0, e1, e2;
4672 if (TREE_SIDE_EFFECTS (op0))
4673 op0 = save_expr (op0);
4675 pfn0 = pfn_from_ptrmemfunc (op0);
4676 delta0 = delta_from_ptrmemfunc (op0);
4677 e1 = cp_build_binary_op (location,
4678 EQ_EXPR,
4679 pfn0,
4680 build_zero_cst (TREE_TYPE (pfn0)),
4681 complain);
4682 e2 = cp_build_binary_op (location,
4683 BIT_AND_EXPR,
4684 delta0,
4685 integer_one_node,
4686 complain);
4688 if (complain & tf_warning)
4689 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4691 e2 = cp_build_binary_op (location,
4692 EQ_EXPR, e2, integer_zero_node,
4693 complain);
4694 op0 = cp_build_binary_op (location,
4695 TRUTH_ANDIF_EXPR, e1, e2,
4696 complain);
4697 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4699 else
4701 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4702 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4704 result_type = TREE_TYPE (op0);
4706 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4707 return cp_build_binary_op (location, code, op1, op0, complain);
4708 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4710 tree type;
4711 /* E will be the final comparison. */
4712 tree e;
4713 /* E1 and E2 are for scratch. */
4714 tree e1;
4715 tree e2;
4716 tree pfn0;
4717 tree pfn1;
4718 tree delta0;
4719 tree delta1;
4721 type = composite_pointer_type (type0, type1, op0, op1,
4722 CPO_COMPARISON, complain);
4724 if (!same_type_p (TREE_TYPE (op0), type))
4725 op0 = cp_convert_and_check (type, op0, complain);
4726 if (!same_type_p (TREE_TYPE (op1), type))
4727 op1 = cp_convert_and_check (type, op1, complain);
4729 if (op0 == error_mark_node || op1 == error_mark_node)
4730 return error_mark_node;
4732 if (TREE_SIDE_EFFECTS (op0))
4733 op0 = save_expr (op0);
4734 if (TREE_SIDE_EFFECTS (op1))
4735 op1 = save_expr (op1);
4737 pfn0 = pfn_from_ptrmemfunc (op0);
4738 pfn0 = cp_fully_fold (pfn0);
4739 /* Avoid -Waddress warnings (c++/64877). */
4740 if (TREE_CODE (pfn0) == ADDR_EXPR)
4741 TREE_NO_WARNING (pfn0) = 1;
4742 pfn1 = pfn_from_ptrmemfunc (op1);
4743 pfn1 = cp_fully_fold (pfn1);
4744 delta0 = delta_from_ptrmemfunc (op0);
4745 delta1 = delta_from_ptrmemfunc (op1);
4746 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4747 == ptrmemfunc_vbit_in_delta)
4749 /* We generate:
4751 (op0.pfn == op1.pfn
4752 && ((op0.delta == op1.delta)
4753 || (!op0.pfn && op0.delta & 1 == 0
4754 && op1.delta & 1 == 0))
4756 The reason for the `!op0.pfn' bit is that a NULL
4757 pointer-to-member is any member with a zero PFN and
4758 LSB of the DELTA field is 0. */
4760 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4761 delta0,
4762 integer_one_node,
4763 complain);
4764 e1 = cp_build_binary_op (location,
4765 EQ_EXPR, e1, integer_zero_node,
4766 complain);
4767 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4768 delta1,
4769 integer_one_node,
4770 complain);
4771 e2 = cp_build_binary_op (location,
4772 EQ_EXPR, e2, integer_zero_node,
4773 complain);
4774 e1 = cp_build_binary_op (location,
4775 TRUTH_ANDIF_EXPR, e2, e1,
4776 complain);
4777 e2 = cp_build_binary_op (location, EQ_EXPR,
4778 pfn0,
4779 build_zero_cst (TREE_TYPE (pfn0)),
4780 complain);
4781 e2 = cp_build_binary_op (location,
4782 TRUTH_ANDIF_EXPR, e2, e1, complain);
4783 e1 = cp_build_binary_op (location,
4784 EQ_EXPR, delta0, delta1, complain);
4785 e1 = cp_build_binary_op (location,
4786 TRUTH_ORIF_EXPR, e1, e2, complain);
4788 else
4790 /* We generate:
4792 (op0.pfn == op1.pfn
4793 && (!op0.pfn || op0.delta == op1.delta))
4795 The reason for the `!op0.pfn' bit is that a NULL
4796 pointer-to-member is any member with a zero PFN; the
4797 DELTA field is unspecified. */
4799 e1 = cp_build_binary_op (location,
4800 EQ_EXPR, delta0, delta1, complain);
4801 e2 = cp_build_binary_op (location,
4802 EQ_EXPR,
4803 pfn0,
4804 build_zero_cst (TREE_TYPE (pfn0)),
4805 complain);
4806 e1 = cp_build_binary_op (location,
4807 TRUTH_ORIF_EXPR, e1, e2, complain);
4809 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4810 e = cp_build_binary_op (location,
4811 TRUTH_ANDIF_EXPR, e2, e1, complain);
4812 if (code == EQ_EXPR)
4813 return e;
4814 return cp_build_binary_op (location,
4815 EQ_EXPR, e, integer_zero_node, complain);
4817 else
4819 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4820 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4821 type1));
4822 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4823 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4824 type0));
4827 break;
4829 case MAX_EXPR:
4830 case MIN_EXPR:
4831 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4832 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4833 shorten = 1;
4834 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4835 result_type = composite_pointer_type (type0, type1, op0, op1,
4836 CPO_COMPARISON, complain);
4837 break;
4839 case LE_EXPR:
4840 case GE_EXPR:
4841 case LT_EXPR:
4842 case GT_EXPR:
4843 if (TREE_CODE (orig_op0) == STRING_CST
4844 || TREE_CODE (orig_op1) == STRING_CST)
4846 if (complain & tf_warning)
4847 warning (OPT_Waddress, "comparison with string literal results "
4848 "in unspecified behavior");
4851 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4853 vector_compare:
4854 tree intt;
4855 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4856 TREE_TYPE (type1))
4857 && !vector_types_compatible_elements_p (type0, type1))
4859 if (complain & tf_error)
4861 error_at (location, "comparing vectors with different "
4862 "element types");
4863 inform (location, "operand types are %qT and %qT",
4864 type0, type1);
4866 return error_mark_node;
4869 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4871 if (complain & tf_error)
4873 error_at (location, "comparing vectors with different "
4874 "number of elements");
4875 inform (location, "operand types are %qT and %qT",
4876 type0, type1);
4878 return error_mark_node;
4881 /* It's not precisely specified how the usual arithmetic
4882 conversions apply to the vector types. Here, we use
4883 the unsigned type if one of the operands is signed and
4884 the other one is unsigned. */
4885 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
4887 if (!TYPE_UNSIGNED (type0))
4888 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
4889 else
4890 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
4891 warning_at (location, OPT_Wsign_compare, "comparison between "
4892 "types %qT and %qT", type0, type1);
4895 /* Always construct signed integer vector type. */
4896 intt = c_common_type_for_size (GET_MODE_BITSIZE
4897 (TYPE_MODE (TREE_TYPE (type0))), 0);
4898 if (!intt)
4900 if (complain & tf_error)
4901 error_at (location, "could not find an integer type "
4902 "of the same size as %qT", TREE_TYPE (type0));
4903 return error_mark_node;
4905 result_type = build_opaque_vector_type (intt,
4906 TYPE_VECTOR_SUBPARTS (type0));
4907 converted = 1;
4908 return build_vec_cmp (resultcode, result_type, op0, op1);
4910 build_type = boolean_type_node;
4911 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4912 || code0 == ENUMERAL_TYPE)
4913 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4914 || code1 == ENUMERAL_TYPE))
4915 short_compare = 1;
4916 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4917 result_type = composite_pointer_type (type0, type1, op0, op1,
4918 CPO_COMPARISON, complain);
4919 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
4921 result_type = type0;
4922 if (extra_warnings && (complain & tf_warning))
4923 warning (OPT_Wextra,
4924 "ordered comparison of pointer with integer zero");
4926 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
4928 result_type = type1;
4929 if (extra_warnings && (complain & tf_warning))
4930 warning (OPT_Wextra,
4931 "ordered comparison of pointer with integer zero");
4933 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4934 /* One of the operands must be of nullptr_t type. */
4935 result_type = TREE_TYPE (nullptr_node);
4936 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4938 result_type = type0;
4939 if (complain & tf_error)
4940 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4941 else
4942 return error_mark_node;
4944 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4946 result_type = type1;
4947 if (complain & tf_error)
4948 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4949 else
4950 return error_mark_node;
4952 break;
4954 case UNORDERED_EXPR:
4955 case ORDERED_EXPR:
4956 case UNLT_EXPR:
4957 case UNLE_EXPR:
4958 case UNGT_EXPR:
4959 case UNGE_EXPR:
4960 case UNEQ_EXPR:
4961 build_type = integer_type_node;
4962 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4964 if (complain & tf_error)
4965 error ("unordered comparison on non-floating point argument");
4966 return error_mark_node;
4968 common = 1;
4969 break;
4971 default:
4972 break;
4975 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4976 || code0 == ENUMERAL_TYPE)
4977 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4978 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4979 arithmetic_types_p = 1;
4980 else
4982 arithmetic_types_p = 0;
4983 /* Vector arithmetic is only allowed when both sides are vectors. */
4984 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4986 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4987 || !vector_types_compatible_elements_p (type0, type1))
4989 if (complain & tf_error)
4991 /* "location" already embeds the locations of the
4992 operands, so we don't need to add them separately
4993 to richloc. */
4994 rich_location richloc (line_table, location);
4995 binary_op_error (&richloc, code, type0, type1);
4997 return error_mark_node;
4999 arithmetic_types_p = 1;
5002 /* Determine the RESULT_TYPE, if it is not already known. */
5003 if (!result_type
5004 && arithmetic_types_p
5005 && (shorten || common || short_compare))
5007 result_type = cp_common_type (type0, type1);
5008 if (complain & tf_warning)
5009 do_warn_double_promotion (result_type, type0, type1,
5010 "implicit conversion from %qT to %qT "
5011 "to match other operand of binary "
5012 "expression",
5013 location);
5016 if (!result_type)
5018 if (complain & tf_error)
5019 error_at (location,
5020 "invalid operands of types %qT and %qT to binary %qO",
5021 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5022 return error_mark_node;
5025 /* If we're in a template, the only thing we need to know is the
5026 RESULT_TYPE. */
5027 if (processing_template_decl)
5029 /* Since the middle-end checks the type when doing a build2, we
5030 need to build the tree in pieces. This built tree will never
5031 get out of the front-end as we replace it when instantiating
5032 the template. */
5033 tree tmp = build2 (resultcode,
5034 build_type ? build_type : result_type,
5035 NULL_TREE, op1);
5036 TREE_OPERAND (tmp, 0) = op0;
5037 return tmp;
5040 if (arithmetic_types_p)
5042 bool first_complex = (code0 == COMPLEX_TYPE);
5043 bool second_complex = (code1 == COMPLEX_TYPE);
5044 int none_complex = (!first_complex && !second_complex);
5046 /* Adapted from patch for c/24581. */
5047 if (first_complex != second_complex
5048 && (code == PLUS_EXPR
5049 || code == MINUS_EXPR
5050 || code == MULT_EXPR
5051 || (code == TRUNC_DIV_EXPR && first_complex))
5052 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5053 && flag_signed_zeros)
5055 /* An operation on mixed real/complex operands must be
5056 handled specially, but the language-independent code can
5057 more easily optimize the plain complex arithmetic if
5058 -fno-signed-zeros. */
5059 tree real_type = TREE_TYPE (result_type);
5060 tree real, imag;
5061 if (first_complex)
5063 if (TREE_TYPE (op0) != result_type)
5064 op0 = cp_convert_and_check (result_type, op0, complain);
5065 if (TREE_TYPE (op1) != real_type)
5066 op1 = cp_convert_and_check (real_type, op1, complain);
5068 else
5070 if (TREE_TYPE (op0) != real_type)
5071 op0 = cp_convert_and_check (real_type, op0, complain);
5072 if (TREE_TYPE (op1) != result_type)
5073 op1 = cp_convert_and_check (result_type, op1, complain);
5075 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5076 return error_mark_node;
5077 if (first_complex)
5079 op0 = save_expr (op0);
5080 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5081 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5082 switch (code)
5084 case MULT_EXPR:
5085 case TRUNC_DIV_EXPR:
5086 op1 = save_expr (op1);
5087 imag = build2 (resultcode, real_type, imag, op1);
5088 /* Fall through. */
5089 case PLUS_EXPR:
5090 case MINUS_EXPR:
5091 real = build2 (resultcode, real_type, real, op1);
5092 break;
5093 default:
5094 gcc_unreachable();
5097 else
5099 op1 = save_expr (op1);
5100 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5101 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5102 switch (code)
5104 case MULT_EXPR:
5105 op0 = save_expr (op0);
5106 imag = build2 (resultcode, real_type, op0, imag);
5107 /* Fall through. */
5108 case PLUS_EXPR:
5109 real = build2 (resultcode, real_type, op0, real);
5110 break;
5111 case MINUS_EXPR:
5112 real = build2 (resultcode, real_type, op0, real);
5113 imag = build1 (NEGATE_EXPR, real_type, imag);
5114 break;
5115 default:
5116 gcc_unreachable();
5119 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5120 return result;
5123 /* For certain operations (which identify themselves by shorten != 0)
5124 if both args were extended from the same smaller type,
5125 do the arithmetic in that type and then extend.
5127 shorten !=0 and !=1 indicates a bitwise operation.
5128 For them, this optimization is safe only if
5129 both args are zero-extended or both are sign-extended.
5130 Otherwise, we might change the result.
5131 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5132 but calculated in (unsigned short) it would be (unsigned short)-1. */
5134 if (shorten && none_complex)
5136 orig_type = result_type;
5137 final_type = result_type;
5138 result_type = shorten_binary_op (result_type, op0, op1,
5139 shorten == -1);
5142 /* Comparison operations are shortened too but differently.
5143 They identify themselves by setting short_compare = 1. */
5145 if (short_compare)
5147 /* We call shorten_compare only for diagnostic-reason. */
5148 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5149 xresult_type = result_type;
5150 enum tree_code xresultcode = resultcode;
5151 shorten_compare (location, &xop0, &xop1, &xresult_type,
5152 &xresultcode);
5155 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5156 && warn_sign_compare
5157 /* Do not warn until the template is instantiated; we cannot
5158 bound the ranges of the arguments until that point. */
5159 && !processing_template_decl
5160 && (complain & tf_warning)
5161 && c_inhibit_evaluation_warnings == 0
5162 /* Even unsigned enum types promote to signed int. We don't
5163 want to issue -Wsign-compare warnings for this case. */
5164 && !enum_cast_to_int (orig_op0)
5165 && !enum_cast_to_int (orig_op1))
5167 tree oop0 = maybe_constant_value (orig_op0);
5168 tree oop1 = maybe_constant_value (orig_op1);
5170 if (TREE_CODE (oop0) != INTEGER_CST)
5171 oop0 = cp_fully_fold (orig_op0);
5172 if (TREE_CODE (oop1) != INTEGER_CST)
5173 oop1 = cp_fully_fold (orig_op1);
5174 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5175 result_type, resultcode);
5179 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5180 Then the expression will be built.
5181 It will be given type FINAL_TYPE if that is nonzero;
5182 otherwise, it will be given type RESULT_TYPE. */
5183 if (! converted)
5185 if (TREE_TYPE (op0) != result_type)
5186 op0 = cp_convert_and_check (result_type, op0, complain);
5187 if (TREE_TYPE (op1) != result_type)
5188 op1 = cp_convert_and_check (result_type, op1, complain);
5190 if (op0 == error_mark_node || op1 == error_mark_node)
5191 return error_mark_node;
5194 if (build_type == NULL_TREE)
5195 build_type = result_type;
5197 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
5198 | SANITIZE_FLOAT_DIVIDE))
5199 && !processing_template_decl
5200 && do_ubsan_in_current_function ()
5201 && (doing_div_or_mod || doing_shift))
5203 /* OP0 and/or OP1 might have side-effects. */
5204 op0 = cp_save_expr (op0);
5205 op1 = cp_save_expr (op1);
5206 op0 = fold_non_dependent_expr (op0);
5207 op1 = fold_non_dependent_expr (op1);
5208 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
5209 | SANITIZE_FLOAT_DIVIDE)))
5211 /* For diagnostics we want to use the promoted types without
5212 shorten_binary_op. So convert the arguments to the
5213 original result_type. */
5214 tree cop0 = op0;
5215 tree cop1 = op1;
5216 if (orig_type != NULL && result_type != orig_type)
5218 cop0 = cp_convert (orig_type, op0, complain);
5219 cop1 = cp_convert (orig_type, op1, complain);
5221 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5223 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5224 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5227 result = build2_loc (location, resultcode, build_type, op0, op1);
5228 if (final_type != 0)
5229 result = cp_convert (final_type, result, complain);
5231 if (instrument_expr != NULL)
5232 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5233 instrument_expr, result);
5235 if (!processing_template_decl)
5237 op0 = cp_fully_fold (op0);
5238 /* Only consider the second argument if the first isn't overflowed. */
5239 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5240 return result;
5241 op1 = cp_fully_fold (op1);
5242 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5243 return result;
5245 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5246 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5247 return result;
5249 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5250 if (TREE_OVERFLOW_P (result_ovl))
5251 overflow_warning (location, result_ovl);
5253 return result;
5256 /* Build a VEC_PERM_EXPR.
5257 This is a simple wrapper for c_build_vec_perm_expr. */
5258 tree
5259 build_x_vec_perm_expr (location_t loc,
5260 tree arg0, tree arg1, tree arg2,
5261 tsubst_flags_t complain)
5263 tree orig_arg0 = arg0;
5264 tree orig_arg1 = arg1;
5265 tree orig_arg2 = arg2;
5266 if (processing_template_decl)
5268 if (type_dependent_expression_p (arg0)
5269 || type_dependent_expression_p (arg1)
5270 || type_dependent_expression_p (arg2))
5271 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5272 arg0 = build_non_dependent_expr (arg0);
5273 if (arg1)
5274 arg1 = build_non_dependent_expr (arg1);
5275 arg2 = build_non_dependent_expr (arg2);
5277 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5278 if (processing_template_decl && exp != error_mark_node)
5279 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5280 orig_arg1, orig_arg2);
5281 return exp;
5284 /* Return a tree for the sum or difference (RESULTCODE says which)
5285 of pointer PTROP and integer INTOP. */
5287 static tree
5288 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5289 tree intop, tsubst_flags_t complain)
5291 tree res_type = TREE_TYPE (ptrop);
5293 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5294 in certain circumstance (when it's valid to do so). So we need
5295 to make sure it's complete. We don't need to check here, if we
5296 can actually complete it at all, as those checks will be done in
5297 pointer_int_sum() anyway. */
5298 complete_type (TREE_TYPE (res_type));
5300 return pointer_int_sum (loc, resultcode, ptrop,
5301 intop, complain & tf_warning_or_error);
5304 /* Return a tree for the difference of pointers OP0 and OP1.
5305 The resulting tree has type int. */
5307 static tree
5308 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5309 tsubst_flags_t complain)
5311 tree result;
5312 tree restype = ptrdiff_type_node;
5313 tree target_type = TREE_TYPE (ptrtype);
5315 if (!complete_type_or_else (target_type, NULL_TREE))
5316 return error_mark_node;
5318 if (VOID_TYPE_P (target_type))
5320 if (complain & tf_error)
5321 permerror (loc, "ISO C++ forbids using pointer of "
5322 "type %<void *%> in subtraction");
5323 else
5324 return error_mark_node;
5326 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5328 if (complain & tf_error)
5329 permerror (loc, "ISO C++ forbids using pointer to "
5330 "a function in subtraction");
5331 else
5332 return error_mark_node;
5334 if (TREE_CODE (target_type) == METHOD_TYPE)
5336 if (complain & tf_error)
5337 permerror (loc, "ISO C++ forbids using pointer to "
5338 "a method in subtraction");
5339 else
5340 return error_mark_node;
5343 /* First do the subtraction as integers;
5344 then drop through to build the divide operator. */
5346 op0 = cp_build_binary_op (loc,
5347 MINUS_EXPR,
5348 cp_convert (restype, op0, complain),
5349 cp_convert (restype, op1, complain),
5350 complain);
5352 /* This generates an error if op1 is a pointer to an incomplete type. */
5353 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5355 if (complain & tf_error)
5356 error_at (loc, "invalid use of a pointer to an incomplete type in "
5357 "pointer arithmetic");
5358 else
5359 return error_mark_node;
5362 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5364 if (complain & tf_error)
5365 error_at (loc, "arithmetic on pointer to an empty aggregate");
5366 else
5367 return error_mark_node;
5370 op1 = (TYPE_PTROB_P (ptrtype)
5371 ? size_in_bytes_loc (loc, target_type)
5372 : integer_one_node);
5374 /* Do the division. */
5376 result = build2_loc (loc, EXACT_DIV_EXPR, restype, op0,
5377 cp_convert (restype, op1, complain));
5378 return result;
5381 /* Construct and perhaps optimize a tree representation
5382 for a unary operation. CODE, a tree_code, specifies the operation
5383 and XARG is the operand. */
5385 tree
5386 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5387 tsubst_flags_t complain)
5389 tree orig_expr = xarg;
5390 tree exp;
5391 int ptrmem = 0;
5392 tree overload = NULL_TREE;
5394 if (processing_template_decl)
5396 if (type_dependent_expression_p (xarg))
5397 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5399 xarg = build_non_dependent_expr (xarg);
5402 exp = NULL_TREE;
5404 /* [expr.unary.op] says:
5406 The address of an object of incomplete type can be taken.
5408 (And is just the ordinary address operator, not an overloaded
5409 "operator &".) However, if the type is a template
5410 specialization, we must complete the type at this point so that
5411 an overloaded "operator &" will be available if required. */
5412 if (code == ADDR_EXPR
5413 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5414 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5415 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5416 || (TREE_CODE (xarg) == OFFSET_REF)))
5417 /* Don't look for a function. */;
5418 else
5419 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5420 NULL_TREE, &overload, complain);
5422 if (!exp && code == ADDR_EXPR)
5424 if (is_overloaded_fn (xarg))
5426 tree fn = get_first_fn (xarg);
5427 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5429 if (complain & tf_error)
5430 error (DECL_CONSTRUCTOR_P (fn)
5431 ? G_("taking address of constructor %qE")
5432 : G_("taking address of destructor %qE"),
5433 xarg.get_value ());
5434 return error_mark_node;
5438 /* A pointer to member-function can be formed only by saying
5439 &X::mf. */
5440 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5441 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5443 if (TREE_CODE (xarg) != OFFSET_REF
5444 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5446 if (complain & tf_error)
5448 error ("invalid use of %qE to form a "
5449 "pointer-to-member-function", xarg.get_value ());
5450 if (TREE_CODE (xarg) != OFFSET_REF)
5451 inform (input_location, " a qualified-id is required");
5453 return error_mark_node;
5455 else
5457 if (complain & tf_error)
5458 error ("parentheses around %qE cannot be used to form a"
5459 " pointer-to-member-function",
5460 xarg.get_value ());
5461 else
5462 return error_mark_node;
5463 PTRMEM_OK_P (xarg) = 1;
5467 if (TREE_CODE (xarg) == OFFSET_REF)
5469 ptrmem = PTRMEM_OK_P (xarg);
5471 if (!ptrmem && !flag_ms_extensions
5472 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5474 /* A single non-static member, make sure we don't allow a
5475 pointer-to-member. */
5476 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5477 TREE_OPERAND (xarg, 0),
5478 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5479 PTRMEM_OK_P (xarg) = ptrmem;
5483 exp = cp_build_addr_expr_strict (xarg, complain);
5486 if (processing_template_decl && exp != error_mark_node)
5488 if (overload != NULL_TREE)
5489 return (build_min_non_dep_op_overload
5490 (code, exp, overload, orig_expr, integer_zero_node));
5492 exp = build_min_non_dep (code, exp, orig_expr,
5493 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5495 if (TREE_CODE (exp) == ADDR_EXPR)
5496 PTRMEM_OK_P (exp) = ptrmem;
5497 return exp;
5500 /* Construct and perhaps optimize a tree representation
5501 for __builtin_addressof operation. ARG specifies the operand. */
5503 tree
5504 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5506 tree orig_expr = arg;
5508 if (processing_template_decl)
5510 if (type_dependent_expression_p (arg))
5511 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5513 arg = build_non_dependent_expr (arg);
5516 tree exp = cp_build_addr_expr_strict (arg, complain);
5518 if (processing_template_decl && exp != error_mark_node)
5519 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5520 return exp;
5523 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5524 constants, where a null value is represented by an INTEGER_CST of
5525 -1. */
5527 tree
5528 cp_truthvalue_conversion (tree expr)
5530 tree type = TREE_TYPE (expr);
5531 if (TYPE_PTR_OR_PTRMEM_P (type)
5532 /* Avoid ICE on invalid use of non-static member function. */
5533 || TREE_CODE (expr) == FUNCTION_DECL)
5534 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, 1);
5535 else
5536 return c_common_truthvalue_conversion (input_location, expr);
5539 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5541 tree
5542 condition_conversion (tree expr)
5544 tree t;
5545 if (processing_template_decl)
5546 return expr;
5547 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5548 tf_warning_or_error, LOOKUP_NORMAL);
5549 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5550 return t;
5553 /* Returns the address of T. This function will fold away
5554 ADDR_EXPR of INDIRECT_REF. */
5556 tree
5557 build_address (tree t)
5559 if (error_operand_p (t) || !cxx_mark_addressable (t))
5560 return error_mark_node;
5561 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5562 t = build_fold_addr_expr (t);
5563 if (TREE_CODE (t) != ADDR_EXPR)
5564 t = rvalue (t);
5565 return t;
5568 /* Return a NOP_EXPR converting EXPR to TYPE. */
5570 tree
5571 build_nop (tree type, tree expr)
5573 if (type == error_mark_node || error_operand_p (expr))
5574 return expr;
5575 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5578 /* Take the address of ARG, whatever that means under C++ semantics.
5579 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5580 and class rvalues as well.
5582 Nothing should call this function directly; instead, callers should use
5583 cp_build_addr_expr or cp_build_addr_expr_strict. */
5585 static tree
5586 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5588 tree argtype;
5589 tree val;
5591 if (!arg || error_operand_p (arg))
5592 return error_mark_node;
5594 arg = mark_lvalue_use (arg);
5595 argtype = lvalue_type (arg);
5597 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5599 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5600 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5602 /* They're trying to take the address of a unique non-static
5603 member function. This is ill-formed (except in MS-land),
5604 but let's try to DTRT.
5605 Note: We only handle unique functions here because we don't
5606 want to complain if there's a static overload; non-unique
5607 cases will be handled by instantiate_type. But we need to
5608 handle this case here to allow casts on the resulting PMF.
5609 We could defer this in non-MS mode, but it's easier to give
5610 a useful error here. */
5612 /* Inside constant member functions, the `this' pointer
5613 contains an extra const qualifier. TYPE_MAIN_VARIANT
5614 is used here to remove this const from the diagnostics
5615 and the created OFFSET_REF. */
5616 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5617 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5618 if (!mark_used (fn, complain) && !(complain & tf_error))
5619 return error_mark_node;
5621 if (! flag_ms_extensions)
5623 tree name = DECL_NAME (fn);
5624 if (!(complain & tf_error))
5625 return error_mark_node;
5626 else if (current_class_type
5627 && TREE_OPERAND (arg, 0) == current_class_ref)
5628 /* An expression like &memfn. */
5629 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5630 " or parenthesized non-static member function to form"
5631 " a pointer to member function. Say %<&%T::%D%>",
5632 base, name);
5633 else
5634 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5635 " function to form a pointer to member function."
5636 " Say %<&%T::%D%>",
5637 base, name);
5639 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5642 /* Uninstantiated types are all functions. Taking the
5643 address of a function is a no-op, so just return the
5644 argument. */
5645 if (type_unknown_p (arg))
5646 return build1 (ADDR_EXPR, unknown_type_node, arg);
5648 if (TREE_CODE (arg) == OFFSET_REF)
5649 /* We want a pointer to member; bypass all the code for actually taking
5650 the address of something. */
5651 goto offset_ref;
5653 /* Anything not already handled and not a true memory reference
5654 is an error. */
5655 if (TREE_CODE (argtype) != FUNCTION_TYPE
5656 && TREE_CODE (argtype) != METHOD_TYPE)
5658 cp_lvalue_kind kind = lvalue_kind (arg);
5659 if (kind == clk_none)
5661 if (complain & tf_error)
5662 lvalue_error (input_location, lv_addressof);
5663 return error_mark_node;
5665 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5667 if (!(complain & tf_error))
5668 return error_mark_node;
5669 if (kind & clk_class)
5670 /* Make this a permerror because we used to accept it. */
5671 permerror (input_location, "taking address of temporary");
5672 else
5673 error ("taking address of xvalue (rvalue reference)");
5677 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5679 tree type = build_pointer_type (TREE_TYPE (argtype));
5680 arg = build1 (CONVERT_EXPR, type, arg);
5681 return arg;
5683 else if (pedantic && DECL_MAIN_P (arg))
5685 /* ARM $3.4 */
5686 /* Apparently a lot of autoconf scripts for C++ packages do this,
5687 so only complain if -Wpedantic. */
5688 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5689 pedwarn (input_location, OPT_Wpedantic,
5690 "ISO C++ forbids taking address of function %<::main%>");
5691 else if (flag_pedantic_errors)
5692 return error_mark_node;
5695 /* Let &* cancel out to simplify resulting code. */
5696 if (INDIRECT_REF_P (arg))
5698 arg = TREE_OPERAND (arg, 0);
5699 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5701 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5702 arg = build1 (CONVERT_EXPR, type, arg);
5704 else
5705 /* Don't let this be an lvalue. */
5706 arg = rvalue (arg);
5707 return arg;
5710 /* ??? Cope with user tricks that amount to offsetof. */
5711 if (TREE_CODE (argtype) != FUNCTION_TYPE
5712 && TREE_CODE (argtype) != METHOD_TYPE
5713 && argtype != unknown_type_node
5714 && (val = get_base_address (arg))
5715 && COMPLETE_TYPE_P (TREE_TYPE (val))
5716 && INDIRECT_REF_P (val)
5717 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5719 tree type = build_pointer_type (argtype);
5720 return fold_convert (type, fold_offsetof_1 (arg));
5723 /* Handle complex lvalues (when permitted)
5724 by reduction to simpler cases. */
5725 val = unary_complex_lvalue (ADDR_EXPR, arg);
5726 if (val != 0)
5727 return val;
5729 switch (TREE_CODE (arg))
5731 CASE_CONVERT:
5732 case FLOAT_EXPR:
5733 case FIX_TRUNC_EXPR:
5734 /* We should have handled this above in the lvalue_kind check. */
5735 gcc_unreachable ();
5736 break;
5738 case BASELINK:
5739 arg = BASELINK_FUNCTIONS (arg);
5740 /* Fall through. */
5742 case OVERLOAD:
5743 arg = OVL_CURRENT (arg);
5744 break;
5746 case OFFSET_REF:
5747 offset_ref:
5748 /* Turn a reference to a non-static data member into a
5749 pointer-to-member. */
5751 tree type;
5752 tree t;
5754 gcc_assert (PTRMEM_OK_P (arg));
5756 t = TREE_OPERAND (arg, 1);
5757 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5759 if (complain & tf_error)
5760 error ("cannot create pointer to reference member %qD", t);
5761 return error_mark_node;
5764 type = build_ptrmem_type (context_for_name_lookup (t),
5765 TREE_TYPE (t));
5766 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5767 return t;
5770 default:
5771 break;
5774 if (argtype != error_mark_node)
5775 argtype = build_pointer_type (argtype);
5777 if (bitfield_p (arg))
5779 if (complain & tf_error)
5780 error ("attempt to take address of bit-field");
5781 return error_mark_node;
5784 /* In a template, we are processing a non-dependent expression
5785 so we can just form an ADDR_EXPR with the correct type. */
5786 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5788 val = build_address (arg);
5789 if (TREE_CODE (arg) == OFFSET_REF)
5790 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5792 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5794 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5796 /* We can only get here with a single static member
5797 function. */
5798 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5799 && DECL_STATIC_FUNCTION_P (fn));
5800 if (!mark_used (fn, complain) && !(complain & tf_error))
5801 return error_mark_node;
5802 val = build_address (fn);
5803 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5804 /* Do not lose object's side effects. */
5805 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5806 TREE_OPERAND (arg, 0), val);
5808 else
5810 tree object = TREE_OPERAND (arg, 0);
5811 tree field = TREE_OPERAND (arg, 1);
5812 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5813 (TREE_TYPE (object), decl_type_context (field)));
5814 val = build_address (arg);
5817 if (TYPE_PTR_P (argtype)
5818 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5820 build_ptrmemfunc_type (argtype);
5821 val = build_ptrmemfunc (argtype, val, 0,
5822 /*c_cast_p=*/false,
5823 complain);
5826 return val;
5829 /* Take the address of ARG if it has one, even if it's an rvalue. */
5831 tree
5832 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5834 return cp_build_addr_expr_1 (arg, 0, complain);
5837 /* Take the address of ARG, but only if it's an lvalue. */
5839 static tree
5840 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5842 return cp_build_addr_expr_1 (arg, 1, complain);
5845 /* C++: Must handle pointers to members.
5847 Perhaps type instantiation should be extended to handle conversion
5848 from aggregates to types we don't yet know we want? (Or are those
5849 cases typically errors which should be reported?)
5851 NOCONVERT suppresses the default promotions (such as from short to int). */
5853 tree
5854 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
5855 tsubst_flags_t complain)
5857 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5858 tree arg = xarg;
5859 location_t location = EXPR_LOC_OR_LOC (arg, input_location);
5860 tree argtype = 0;
5861 const char *errstring = NULL;
5862 tree val;
5863 const char *invalid_op_diag;
5865 if (!arg || error_operand_p (arg))
5866 return error_mark_node;
5868 if ((invalid_op_diag
5869 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5870 ? CONVERT_EXPR
5871 : code),
5872 TREE_TYPE (xarg))))
5874 if (complain & tf_error)
5875 error (invalid_op_diag);
5876 return error_mark_node;
5879 switch (code)
5881 case UNARY_PLUS_EXPR:
5882 case NEGATE_EXPR:
5884 int flags = WANT_ARITH | WANT_ENUM;
5885 /* Unary plus (but not unary minus) is allowed on pointers. */
5886 if (code == UNARY_PLUS_EXPR)
5887 flags |= WANT_POINTER;
5888 arg = build_expr_type_conversion (flags, arg, true);
5889 if (!arg)
5890 errstring = (code == NEGATE_EXPR
5891 ? _("wrong type argument to unary minus")
5892 : _("wrong type argument to unary plus"));
5893 else
5895 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5896 arg = cp_perform_integral_promotions (arg, complain);
5898 /* Make sure the result is not an lvalue: a unary plus or minus
5899 expression is always a rvalue. */
5900 arg = rvalue (arg);
5903 break;
5905 case BIT_NOT_EXPR:
5906 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5908 code = CONJ_EXPR;
5909 if (!noconvert)
5911 arg = cp_default_conversion (arg, complain);
5912 if (arg == error_mark_node)
5913 return error_mark_node;
5916 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5917 | WANT_VECTOR_OR_COMPLEX,
5918 arg, true)))
5919 errstring = _("wrong type argument to bit-complement");
5920 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5922 /* Warn if the expression has boolean value. */
5923 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
5924 && warning_at (location, OPT_Wbool_operation,
5925 "%<~%> on an expression of type bool"))
5926 inform (location, "did you mean to use logical not (%<!%>)?");
5927 arg = cp_perform_integral_promotions (arg, complain);
5929 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
5930 arg = mark_rvalue_use (arg);
5931 break;
5933 case ABS_EXPR:
5934 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5935 errstring = _("wrong type argument to abs");
5936 else if (!noconvert)
5938 arg = cp_default_conversion (arg, complain);
5939 if (arg == error_mark_node)
5940 return error_mark_node;
5942 break;
5944 case CONJ_EXPR:
5945 /* Conjugating a real value is a no-op, but allow it anyway. */
5946 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5947 errstring = _("wrong type argument to conjugation");
5948 else if (!noconvert)
5950 arg = cp_default_conversion (arg, complain);
5951 if (arg == error_mark_node)
5952 return error_mark_node;
5954 break;
5956 case TRUTH_NOT_EXPR:
5957 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5958 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5959 build_zero_cst (TREE_TYPE (arg)), complain);
5960 arg = perform_implicit_conversion (boolean_type_node, arg,
5961 complain);
5962 val = invert_truthvalue_loc (input_location, arg);
5963 if (arg != error_mark_node)
5964 return val;
5965 errstring = _("in argument to unary !");
5966 break;
5968 case NOP_EXPR:
5969 break;
5971 case REALPART_EXPR:
5972 case IMAGPART_EXPR:
5973 arg = build_real_imag_expr (input_location, code, arg);
5974 return arg;
5976 case PREINCREMENT_EXPR:
5977 case POSTINCREMENT_EXPR:
5978 case PREDECREMENT_EXPR:
5979 case POSTDECREMENT_EXPR:
5980 /* Handle complex lvalues (when permitted)
5981 by reduction to simpler cases. */
5983 val = unary_complex_lvalue (code, arg);
5984 if (val != 0)
5985 return val;
5987 arg = mark_lvalue_use (arg);
5989 /* Increment or decrement the real part of the value,
5990 and don't change the imaginary part. */
5991 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5993 tree real, imag;
5995 arg = cp_stabilize_reference (arg);
5996 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
5997 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
5998 real = cp_build_unary_op (code, real, true, complain);
5999 if (real == error_mark_node || imag == error_mark_node)
6000 return error_mark_node;
6001 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6002 real, imag);
6005 /* Report invalid types. */
6007 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6008 arg, true)))
6010 if (code == PREINCREMENT_EXPR)
6011 errstring = _("no pre-increment operator for type");
6012 else if (code == POSTINCREMENT_EXPR)
6013 errstring = _("no post-increment operator for type");
6014 else if (code == PREDECREMENT_EXPR)
6015 errstring = _("no pre-decrement operator for type");
6016 else
6017 errstring = _("no post-decrement operator for type");
6018 break;
6020 else if (arg == error_mark_node)
6021 return error_mark_node;
6023 /* Report something read-only. */
6025 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6026 || TREE_READONLY (arg))
6028 if (complain & tf_error)
6029 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6030 || code == POSTINCREMENT_EXPR)
6031 ? lv_increment : lv_decrement));
6032 else
6033 return error_mark_node;
6037 tree inc;
6038 tree declared_type = unlowered_expr_type (arg);
6040 argtype = TREE_TYPE (arg);
6042 /* ARM $5.2.5 last annotation says this should be forbidden. */
6043 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6045 if (complain & tf_error)
6046 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6047 ? G_("ISO C++ forbids incrementing an enum")
6048 : G_("ISO C++ forbids decrementing an enum"));
6049 else
6050 return error_mark_node;
6053 /* Compute the increment. */
6055 if (TYPE_PTR_P (argtype))
6057 tree type = complete_type (TREE_TYPE (argtype));
6059 if (!COMPLETE_OR_VOID_TYPE_P (type))
6061 if (complain & tf_error)
6062 error (((code == PREINCREMENT_EXPR
6063 || code == POSTINCREMENT_EXPR))
6064 ? G_("cannot increment a pointer to incomplete type %qT")
6065 : G_("cannot decrement a pointer to incomplete type %qT"),
6066 TREE_TYPE (argtype));
6067 else
6068 return error_mark_node;
6070 else if (!TYPE_PTROB_P (argtype))
6072 if (complain & tf_error)
6073 pedwarn (input_location, OPT_Wpointer_arith,
6074 (code == PREINCREMENT_EXPR
6075 || code == POSTINCREMENT_EXPR)
6076 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6077 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6078 argtype);
6079 else
6080 return error_mark_node;
6083 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6085 else
6086 inc = VECTOR_TYPE_P (argtype)
6087 ? build_one_cst (argtype)
6088 : integer_one_node;
6090 inc = cp_convert (argtype, inc, complain);
6092 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6093 need to ask Objective-C to build the increment or decrement
6094 expression for it. */
6095 if (objc_is_property_ref (arg))
6096 return objc_build_incr_expr_for_property_ref (input_location, code,
6097 arg, inc);
6099 /* Complain about anything else that is not a true lvalue. */
6100 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6101 || code == POSTINCREMENT_EXPR)
6102 ? lv_increment : lv_decrement),
6103 complain))
6104 return error_mark_node;
6106 /* Forbid using -- or ++ in C++17 on `bool'. */
6107 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6109 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6111 if (complain & tf_error)
6112 error ("use of an operand of type %qT in %<operator--%> "
6113 "is forbidden", boolean_type_node);
6114 return error_mark_node;
6116 else
6118 if (cxx_dialect >= cxx1z)
6120 if (complain & tf_error)
6121 error ("use of an operand of type %qT in "
6122 "%<operator++%> is forbidden in C++1z",
6123 boolean_type_node);
6124 return error_mark_node;
6126 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6127 else if (!in_system_header_at (input_location))
6128 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6129 "in %<operator++%> is deprecated",
6130 boolean_type_node);
6132 val = boolean_increment (code, arg);
6134 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6135 /* An rvalue has no cv-qualifiers. */
6136 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6137 else
6138 val = build2 (code, TREE_TYPE (arg), arg, inc);
6140 TREE_SIDE_EFFECTS (val) = 1;
6141 return val;
6144 case ADDR_EXPR:
6145 /* Note that this operation never does default_conversion
6146 regardless of NOCONVERT. */
6147 return cp_build_addr_expr (arg, complain);
6149 default:
6150 break;
6153 if (!errstring)
6155 if (argtype == 0)
6156 argtype = TREE_TYPE (arg);
6157 return build1 (code, argtype, arg);
6160 if (complain & tf_error)
6161 error ("%s", errstring);
6162 return error_mark_node;
6165 /* Hook for the c-common bits that build a unary op. */
6166 tree
6167 build_unary_op (location_t /*location*/,
6168 enum tree_code code, tree xarg, bool noconvert)
6170 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6173 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6174 for certain kinds of expressions which are not really lvalues
6175 but which we can accept as lvalues.
6177 If ARG is not a kind of expression we can handle, return
6178 NULL_TREE. */
6180 tree
6181 unary_complex_lvalue (enum tree_code code, tree arg)
6183 /* Inside a template, making these kinds of adjustments is
6184 pointless; we are only concerned with the type of the
6185 expression. */
6186 if (processing_template_decl)
6187 return NULL_TREE;
6189 /* Handle (a, b) used as an "lvalue". */
6190 if (TREE_CODE (arg) == COMPOUND_EXPR)
6192 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6193 tf_warning_or_error);
6194 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6195 TREE_OPERAND (arg, 0), real_result);
6198 /* Handle (a ? b : c) used as an "lvalue". */
6199 if (TREE_CODE (arg) == COND_EXPR
6200 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6201 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6203 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6204 if (TREE_CODE (arg) == MODIFY_EXPR
6205 || TREE_CODE (arg) == PREINCREMENT_EXPR
6206 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6208 tree lvalue = TREE_OPERAND (arg, 0);
6209 if (TREE_SIDE_EFFECTS (lvalue))
6211 lvalue = cp_stabilize_reference (lvalue);
6212 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6213 lvalue, TREE_OPERAND (arg, 1));
6215 return unary_complex_lvalue
6216 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
6219 if (code != ADDR_EXPR)
6220 return NULL_TREE;
6222 /* Handle (a = b) used as an "lvalue" for `&'. */
6223 if (TREE_CODE (arg) == MODIFY_EXPR
6224 || TREE_CODE (arg) == INIT_EXPR)
6226 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6227 tf_warning_or_error);
6228 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6229 arg, real_result);
6230 TREE_NO_WARNING (arg) = 1;
6231 return arg;
6234 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6235 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6236 || TREE_CODE (arg) == OFFSET_REF)
6237 return NULL_TREE;
6239 /* We permit compiler to make function calls returning
6240 objects of aggregate type look like lvalues. */
6242 tree targ = arg;
6244 if (TREE_CODE (targ) == SAVE_EXPR)
6245 targ = TREE_OPERAND (targ, 0);
6247 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6249 if (TREE_CODE (arg) == SAVE_EXPR)
6250 targ = arg;
6251 else
6252 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6253 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6256 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6257 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6258 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6261 /* Don't let anything else be handled specially. */
6262 return NULL_TREE;
6265 /* Mark EXP saying that we need to be able to take the
6266 address of it; it should not be allocated in a register.
6267 Value is true if successful.
6269 C++: we do not allow `current_class_ptr' to be addressable. */
6271 bool
6272 cxx_mark_addressable (tree exp)
6274 tree x = exp;
6276 while (1)
6277 switch (TREE_CODE (x))
6279 case ADDR_EXPR:
6280 case COMPONENT_REF:
6281 case ARRAY_REF:
6282 case REALPART_EXPR:
6283 case IMAGPART_EXPR:
6284 x = TREE_OPERAND (x, 0);
6285 break;
6287 case PARM_DECL:
6288 if (x == current_class_ptr)
6290 error ("cannot take the address of %<this%>, which is an rvalue expression");
6291 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6292 return true;
6294 /* Fall through. */
6296 case VAR_DECL:
6297 /* Caller should not be trying to mark initialized
6298 constant fields addressable. */
6299 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6300 || DECL_IN_AGGR_P (x) == 0
6301 || TREE_STATIC (x)
6302 || DECL_EXTERNAL (x));
6303 /* Fall through. */
6305 case RESULT_DECL:
6306 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6307 && !DECL_ARTIFICIAL (x))
6309 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6311 error
6312 ("address of explicit register variable %qD requested", x);
6313 return false;
6315 else if (extra_warnings)
6316 warning
6317 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6319 TREE_ADDRESSABLE (x) = 1;
6320 return true;
6322 case CONST_DECL:
6323 case FUNCTION_DECL:
6324 TREE_ADDRESSABLE (x) = 1;
6325 return true;
6327 case CONSTRUCTOR:
6328 TREE_ADDRESSABLE (x) = 1;
6329 return true;
6331 case TARGET_EXPR:
6332 TREE_ADDRESSABLE (x) = 1;
6333 cxx_mark_addressable (TREE_OPERAND (x, 0));
6334 return true;
6336 default:
6337 return true;
6341 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6343 tree
6344 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6345 tsubst_flags_t complain)
6347 tree orig_ifexp = ifexp;
6348 tree orig_op1 = op1;
6349 tree orig_op2 = op2;
6350 tree expr;
6352 if (processing_template_decl)
6354 /* The standard says that the expression is type-dependent if
6355 IFEXP is type-dependent, even though the eventual type of the
6356 expression doesn't dependent on IFEXP. */
6357 if (type_dependent_expression_p (ifexp)
6358 /* As a GNU extension, the middle operand may be omitted. */
6359 || (op1 && type_dependent_expression_p (op1))
6360 || type_dependent_expression_p (op2))
6361 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6362 ifexp = build_non_dependent_expr (ifexp);
6363 if (op1)
6364 op1 = build_non_dependent_expr (op1);
6365 op2 = build_non_dependent_expr (op2);
6368 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6369 if (processing_template_decl && expr != error_mark_node)
6371 tree min = build_min_non_dep (COND_EXPR, expr,
6372 orig_ifexp, orig_op1, orig_op2);
6373 /* Remember that the result is an lvalue or xvalue. */
6374 if (glvalue_p (expr) && !glvalue_p (min))
6375 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6376 !lvalue_p (expr));
6377 expr = convert_from_reference (min);
6379 return expr;
6382 /* Given a list of expressions, return a compound expression
6383 that performs them all and returns the value of the last of them. */
6385 tree
6386 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6387 tsubst_flags_t complain)
6389 tree expr = TREE_VALUE (list);
6391 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6392 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6394 if (complain & tf_error)
6395 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6396 "list-initializer for non-class type must not "
6397 "be parenthesized");
6398 else
6399 return error_mark_node;
6402 if (TREE_CHAIN (list))
6404 if (complain & tf_error)
6405 switch (exp)
6407 case ELK_INIT:
6408 permerror (input_location, "expression list treated as compound "
6409 "expression in initializer");
6410 break;
6411 case ELK_MEM_INIT:
6412 permerror (input_location, "expression list treated as compound "
6413 "expression in mem-initializer");
6414 break;
6415 case ELK_FUNC_CAST:
6416 permerror (input_location, "expression list treated as compound "
6417 "expression in functional cast");
6418 break;
6419 default:
6420 gcc_unreachable ();
6422 else
6423 return error_mark_node;
6425 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6426 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6427 expr, TREE_VALUE (list), complain);
6430 return expr;
6433 /* Like build_x_compound_expr_from_list, but using a VEC. */
6435 tree
6436 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6437 tsubst_flags_t complain)
6439 if (vec_safe_is_empty (vec))
6440 return NULL_TREE;
6441 else if (vec->length () == 1)
6442 return (*vec)[0];
6443 else
6445 tree expr;
6446 unsigned int ix;
6447 tree t;
6449 if (msg != NULL)
6451 if (complain & tf_error)
6452 permerror (input_location,
6453 "%s expression list treated as compound expression",
6454 msg);
6455 else
6456 return error_mark_node;
6459 expr = (*vec)[0];
6460 for (ix = 1; vec->iterate (ix, &t); ++ix)
6461 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6462 t, complain);
6464 return expr;
6468 /* Handle overloading of the ',' operator when needed. */
6470 tree
6471 build_x_compound_expr (location_t loc, tree op1, tree op2,
6472 tsubst_flags_t complain)
6474 tree result;
6475 tree orig_op1 = op1;
6476 tree orig_op2 = op2;
6477 tree overload = NULL_TREE;
6479 if (processing_template_decl)
6481 if (type_dependent_expression_p (op1)
6482 || type_dependent_expression_p (op2))
6483 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6484 op1 = build_non_dependent_expr (op1);
6485 op2 = build_non_dependent_expr (op2);
6488 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6489 NULL_TREE, &overload, complain);
6490 if (!result)
6491 result = cp_build_compound_expr (op1, op2, complain);
6493 if (processing_template_decl && result != error_mark_node)
6495 if (overload != NULL_TREE)
6496 return (build_min_non_dep_op_overload
6497 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6499 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6502 return result;
6505 /* Like cp_build_compound_expr, but for the c-common bits. */
6507 tree
6508 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6510 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6513 /* Build a compound expression. */
6515 tree
6516 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6518 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6520 if (lhs == error_mark_node || rhs == error_mark_node)
6521 return error_mark_node;
6523 if (flag_cilkplus
6524 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6525 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6527 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6528 : EXPR_LOCATION (rhs));
6529 error_at (loc,
6530 "spawned function call cannot be part of a comma expression");
6531 return error_mark_node;
6534 if (TREE_CODE (rhs) == TARGET_EXPR)
6536 /* If the rhs is a TARGET_EXPR, then build the compound
6537 expression inside the target_expr's initializer. This
6538 helps the compiler to eliminate unnecessary temporaries. */
6539 tree init = TREE_OPERAND (rhs, 1);
6541 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6542 TREE_OPERAND (rhs, 1) = init;
6544 return rhs;
6547 if (type_unknown_p (rhs))
6549 if (complain & tf_error)
6550 error ("no context to resolve type of %qE", rhs);
6551 return error_mark_node;
6554 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6557 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6558 casts away constness. CAST gives the type of cast. Returns true
6559 if the cast is ill-formed, false if it is well-formed.
6561 ??? This function warns for casting away any qualifier not just
6562 const. We would like to specify exactly what qualifiers are casted
6563 away.
6566 static bool
6567 check_for_casting_away_constness (tree src_type, tree dest_type,
6568 enum tree_code cast, tsubst_flags_t complain)
6570 /* C-style casts are allowed to cast away constness. With
6571 WARN_CAST_QUAL, we still want to issue a warning. */
6572 if (cast == CAST_EXPR && !warn_cast_qual)
6573 return false;
6575 if (!casts_away_constness (src_type, dest_type, complain))
6576 return false;
6578 switch (cast)
6580 case CAST_EXPR:
6581 if (complain & tf_warning)
6582 warning (OPT_Wcast_qual,
6583 "cast from type %qT to type %qT casts away qualifiers",
6584 src_type, dest_type);
6585 return false;
6587 case STATIC_CAST_EXPR:
6588 if (complain & tf_error)
6589 error ("static_cast from type %qT to type %qT casts away qualifiers",
6590 src_type, dest_type);
6591 return true;
6593 case REINTERPRET_CAST_EXPR:
6594 if (complain & tf_error)
6595 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6596 src_type, dest_type);
6597 return true;
6599 default:
6600 gcc_unreachable();
6605 Warns if the cast from expression EXPR to type TYPE is useless.
6607 void
6608 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6610 if (warn_useless_cast
6611 && complain & tf_warning)
6613 if ((TREE_CODE (type) == REFERENCE_TYPE
6614 && (TYPE_REF_IS_RVALUE (type)
6615 ? xvalue_p (expr) : lvalue_p (expr))
6616 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6617 || same_type_p (TREE_TYPE (expr), type))
6618 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6622 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6623 (another pointer-to-member type in the same hierarchy) and return
6624 the converted expression. If ALLOW_INVERSE_P is permitted, a
6625 pointer-to-derived may be converted to pointer-to-base; otherwise,
6626 only the other direction is permitted. If C_CAST_P is true, this
6627 conversion is taking place as part of a C-style cast. */
6629 tree
6630 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6631 bool c_cast_p, tsubst_flags_t complain)
6633 if (TYPE_PTRDATAMEM_P (type))
6635 tree delta;
6637 if (TREE_CODE (expr) == PTRMEM_CST)
6638 expr = cplus_expand_constant (expr);
6639 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6640 TYPE_PTRMEM_CLASS_TYPE (type),
6641 allow_inverse_p,
6642 c_cast_p, complain);
6643 if (delta == error_mark_node)
6644 return error_mark_node;
6646 if (!integer_zerop (delta))
6648 tree cond, op1, op2;
6650 cond = cp_build_binary_op (input_location,
6651 EQ_EXPR,
6652 expr,
6653 build_int_cst (TREE_TYPE (expr), -1),
6654 complain);
6655 op1 = build_nop (ptrdiff_type_node, expr);
6656 op2 = cp_build_binary_op (input_location,
6657 PLUS_EXPR, op1, delta,
6658 complain);
6660 expr = fold_build3_loc (input_location,
6661 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6665 return build_nop (type, expr);
6667 else
6668 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6669 allow_inverse_p, c_cast_p, complain);
6672 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6673 this static_cast is being attempted as one of the possible casts
6674 allowed by a C-style cast. (In that case, accessibility of base
6675 classes is not considered, and it is OK to cast away
6676 constness.) Return the result of the cast. *VALID_P is set to
6677 indicate whether or not the cast was valid. */
6679 static tree
6680 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6681 bool *valid_p, tsubst_flags_t complain)
6683 tree intype;
6684 tree result;
6685 cp_lvalue_kind clk;
6687 /* Assume the cast is valid. */
6688 *valid_p = true;
6690 intype = unlowered_expr_type (expr);
6692 /* Save casted types in the function's used types hash table. */
6693 used_types_insert (type);
6695 /* [expr.static.cast]
6697 An lvalue of type "cv1 B", where B is a class type, can be cast
6698 to type "reference to cv2 D", where D is a class derived (clause
6699 _class.derived_) from B, if a valid standard conversion from
6700 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6701 same cv-qualification as, or greater cv-qualification than, cv1,
6702 and B is not a virtual base class of D. */
6703 /* We check this case before checking the validity of "TYPE t =
6704 EXPR;" below because for this case:
6706 struct B {};
6707 struct D : public B { D(const B&); };
6708 extern B& b;
6709 void f() { static_cast<const D&>(b); }
6711 we want to avoid constructing a new D. The standard is not
6712 completely clear about this issue, but our interpretation is
6713 consistent with other compilers. */
6714 if (TREE_CODE (type) == REFERENCE_TYPE
6715 && CLASS_TYPE_P (TREE_TYPE (type))
6716 && CLASS_TYPE_P (intype)
6717 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6718 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6719 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6720 build_pointer_type (TYPE_MAIN_VARIANT
6721 (TREE_TYPE (type))),
6722 complain)
6723 && (c_cast_p
6724 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6726 tree base;
6728 /* There is a standard conversion from "D*" to "B*" even if "B"
6729 is ambiguous or inaccessible. If this is really a
6730 static_cast, then we check both for inaccessibility and
6731 ambiguity. However, if this is a static_cast being performed
6732 because the user wrote a C-style cast, then accessibility is
6733 not considered. */
6734 base = lookup_base (TREE_TYPE (type), intype,
6735 c_cast_p ? ba_unique : ba_check,
6736 NULL, complain);
6737 expr = build_address (expr);
6739 if (flag_sanitize & SANITIZE_VPTR)
6741 tree ubsan_check
6742 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6743 intype, expr);
6744 if (ubsan_check)
6745 expr = ubsan_check;
6748 /* Convert from "B*" to "D*". This function will check that "B"
6749 is not a virtual base of "D". */
6750 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6751 complain);
6753 /* Convert the pointer to a reference -- but then remember that
6754 there are no expressions with reference type in C++.
6756 We call rvalue so that there's an actual tree code
6757 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6758 is a variable with the same type, the conversion would get folded
6759 away, leaving just the variable and causing lvalue_kind to give
6760 the wrong answer. */
6761 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6764 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6765 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6766 if (TREE_CODE (type) == REFERENCE_TYPE
6767 && TYPE_REF_IS_RVALUE (type)
6768 && (clk = real_lvalue_p (expr))
6769 && reference_related_p (TREE_TYPE (type), intype)
6770 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6772 if (clk == clk_ordinary)
6774 /* Handle the (non-bit-field) lvalue case here by casting to
6775 lvalue reference and then changing it to an rvalue reference.
6776 Casting an xvalue to rvalue reference will be handled by the
6777 main code path. */
6778 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6779 result = (perform_direct_initialization_if_possible
6780 (lref, expr, c_cast_p, complain));
6781 result = build1 (NON_LVALUE_EXPR, type, result);
6782 return convert_from_reference (result);
6784 else
6785 /* For a bit-field or packed field, bind to a temporary. */
6786 expr = rvalue (expr);
6789 /* Resolve overloaded address here rather than once in
6790 implicit_conversion and again in the inverse code below. */
6791 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6793 expr = instantiate_type (type, expr, complain);
6794 intype = TREE_TYPE (expr);
6797 /* [expr.static.cast]
6799 Any expression can be explicitly converted to type cv void. */
6800 if (VOID_TYPE_P (type))
6801 return convert_to_void (expr, ICV_CAST, complain);
6803 /* [class.abstract]
6804 An abstract class shall not be used ... as the type of an explicit
6805 conversion. */
6806 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6807 return error_mark_node;
6809 /* [expr.static.cast]
6811 An expression e can be explicitly converted to a type T using a
6812 static_cast of the form static_cast<T>(e) if the declaration T
6813 t(e);" is well-formed, for some invented temporary variable
6814 t. */
6815 result = perform_direct_initialization_if_possible (type, expr,
6816 c_cast_p, complain);
6817 if (result)
6819 result = convert_from_reference (result);
6821 /* [expr.static.cast]
6823 If T is a reference type, the result is an lvalue; otherwise,
6824 the result is an rvalue. */
6825 if (TREE_CODE (type) != REFERENCE_TYPE)
6827 result = rvalue (result);
6829 if (result == expr && SCALAR_TYPE_P (type))
6830 /* Leave some record of the cast. */
6831 result = build_nop (type, expr);
6833 return result;
6836 /* [expr.static.cast]
6838 The inverse of any standard conversion sequence (clause _conv_),
6839 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6840 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6841 (_conv.bool_) conversions, can be performed explicitly using
6842 static_cast subject to the restriction that the explicit
6843 conversion does not cast away constness (_expr.const.cast_), and
6844 the following additional rules for specific cases: */
6845 /* For reference, the conversions not excluded are: integral
6846 promotions, floating point promotion, integral conversions,
6847 floating point conversions, floating-integral conversions,
6848 pointer conversions, and pointer to member conversions. */
6849 /* DR 128
6851 A value of integral _or enumeration_ type can be explicitly
6852 converted to an enumeration type. */
6853 /* The effect of all that is that any conversion between any two
6854 types which are integral, floating, or enumeration types can be
6855 performed. */
6856 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6857 || SCALAR_FLOAT_TYPE_P (type))
6858 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6859 || SCALAR_FLOAT_TYPE_P (intype)))
6860 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6862 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6863 && CLASS_TYPE_P (TREE_TYPE (type))
6864 && CLASS_TYPE_P (TREE_TYPE (intype))
6865 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6866 (TREE_TYPE (intype))),
6867 build_pointer_type (TYPE_MAIN_VARIANT
6868 (TREE_TYPE (type))),
6869 complain))
6871 tree base;
6873 if (!c_cast_p
6874 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6875 complain))
6876 return error_mark_node;
6877 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6878 c_cast_p ? ba_unique : ba_check,
6879 NULL, complain);
6880 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6881 complain);
6883 if (flag_sanitize & SANITIZE_VPTR)
6885 tree ubsan_check
6886 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6887 intype, expr);
6888 if (ubsan_check)
6889 expr = ubsan_check;
6892 return cp_fold_convert (type, expr);
6895 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6896 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6898 tree c1;
6899 tree c2;
6900 tree t1;
6901 tree t2;
6903 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6904 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6906 if (TYPE_PTRDATAMEM_P (type))
6908 t1 = (build_ptrmem_type
6909 (c1,
6910 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6911 t2 = (build_ptrmem_type
6912 (c2,
6913 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6915 else
6917 t1 = intype;
6918 t2 = type;
6920 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6922 if (!c_cast_p
6923 && check_for_casting_away_constness (intype, type,
6924 STATIC_CAST_EXPR,
6925 complain))
6926 return error_mark_node;
6927 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6928 c_cast_p, complain);
6932 /* [expr.static.cast]
6934 An rvalue of type "pointer to cv void" can be explicitly
6935 converted to a pointer to object type. A value of type pointer
6936 to object converted to "pointer to cv void" and back to the
6937 original pointer type will have its original value. */
6938 if (TYPE_PTR_P (intype)
6939 && VOID_TYPE_P (TREE_TYPE (intype))
6940 && TYPE_PTROB_P (type))
6942 if (!c_cast_p
6943 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6944 complain))
6945 return error_mark_node;
6946 return build_nop (type, expr);
6949 *valid_p = false;
6950 return error_mark_node;
6953 /* Return an expression representing static_cast<TYPE>(EXPR). */
6955 tree
6956 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6958 tree result;
6959 bool valid_p;
6961 if (type == error_mark_node || expr == error_mark_node)
6962 return error_mark_node;
6964 if (processing_template_decl)
6966 expr = build_min (STATIC_CAST_EXPR, type, expr);
6967 /* We don't know if it will or will not have side effects. */
6968 TREE_SIDE_EFFECTS (expr) = 1;
6969 return convert_from_reference (expr);
6972 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6973 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6974 if (TREE_CODE (type) != REFERENCE_TYPE
6975 && TREE_CODE (expr) == NOP_EXPR
6976 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6977 expr = TREE_OPERAND (expr, 0);
6979 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6980 complain);
6981 if (valid_p)
6983 if (result != error_mark_node)
6984 maybe_warn_about_useless_cast (type, expr, complain);
6985 return result;
6988 if (complain & tf_error)
6989 error ("invalid static_cast from type %qT to type %qT",
6990 TREE_TYPE (expr), type);
6991 return error_mark_node;
6994 /* EXPR is an expression with member function or pointer-to-member
6995 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6996 not permitted by ISO C++, but we accept it in some modes. If we
6997 are not in one of those modes, issue a diagnostic. Return the
6998 converted expression. */
7000 tree
7001 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7003 tree intype;
7004 tree decl;
7006 intype = TREE_TYPE (expr);
7007 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7008 || TREE_CODE (intype) == METHOD_TYPE);
7010 if (!(complain & tf_warning_or_error))
7011 return error_mark_node;
7013 if (pedantic || warn_pmf2ptr)
7014 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7015 "converting from %qT to %qT", intype, type);
7017 if (TREE_CODE (intype) == METHOD_TYPE)
7018 expr = build_addr_func (expr, complain);
7019 else if (TREE_CODE (expr) == PTRMEM_CST)
7020 expr = build_address (PTRMEM_CST_MEMBER (expr));
7021 else
7023 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7024 decl = build_address (decl);
7025 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7028 if (expr == error_mark_node)
7029 return error_mark_node;
7031 return build_nop (type, expr);
7034 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7035 If C_CAST_P is true, this reinterpret cast is being done as part of
7036 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7037 indicate whether or not reinterpret_cast was valid. */
7039 static tree
7040 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7041 bool *valid_p, tsubst_flags_t complain)
7043 tree intype;
7045 /* Assume the cast is invalid. */
7046 if (valid_p)
7047 *valid_p = true;
7049 if (type == error_mark_node || error_operand_p (expr))
7050 return error_mark_node;
7052 intype = TREE_TYPE (expr);
7054 /* Save casted types in the function's used types hash table. */
7055 used_types_insert (type);
7057 /* [expr.reinterpret.cast]
7058 An lvalue expression of type T1 can be cast to the type
7059 "reference to T2" if an expression of type "pointer to T1" can be
7060 explicitly converted to the type "pointer to T2" using a
7061 reinterpret_cast. */
7062 if (TREE_CODE (type) == REFERENCE_TYPE)
7064 if (! lvalue_p (expr))
7066 if (complain & tf_error)
7067 error ("invalid cast of an rvalue expression of type "
7068 "%qT to type %qT",
7069 intype, type);
7070 return error_mark_node;
7073 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7074 "B" are related class types; the reinterpret_cast does not
7075 adjust the pointer. */
7076 if (TYPE_PTR_P (intype)
7077 && (complain & tf_warning)
7078 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7079 COMPARE_BASE | COMPARE_DERIVED)))
7080 warning (0, "casting %qT to %qT does not dereference pointer",
7081 intype, type);
7083 expr = cp_build_addr_expr (expr, complain);
7085 if (warn_strict_aliasing > 2)
7086 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
7088 if (expr != error_mark_node)
7089 expr = build_reinterpret_cast_1
7090 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7091 valid_p, complain);
7092 if (expr != error_mark_node)
7093 /* cp_build_indirect_ref isn't right for rvalue refs. */
7094 expr = convert_from_reference (fold_convert (type, expr));
7095 return expr;
7098 /* As a G++ extension, we consider conversions from member
7099 functions, and pointers to member functions to
7100 pointer-to-function and pointer-to-void types. If
7101 -Wno-pmf-conversions has not been specified,
7102 convert_member_func_to_ptr will issue an error message. */
7103 if ((TYPE_PTRMEMFUNC_P (intype)
7104 || TREE_CODE (intype) == METHOD_TYPE)
7105 && TYPE_PTR_P (type)
7106 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7107 || VOID_TYPE_P (TREE_TYPE (type))))
7108 return convert_member_func_to_ptr (type, expr, complain);
7110 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7111 array-to-pointer, and function-to-pointer conversions are
7112 performed. */
7113 expr = decay_conversion (expr, complain);
7115 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7116 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7117 if (TREE_CODE (expr) == NOP_EXPR
7118 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7119 expr = TREE_OPERAND (expr, 0);
7121 if (error_operand_p (expr))
7122 return error_mark_node;
7124 intype = TREE_TYPE (expr);
7126 /* [expr.reinterpret.cast]
7127 A pointer can be converted to any integral type large enough to
7128 hold it. ... A value of type std::nullptr_t can be converted to
7129 an integral type; the conversion has the same meaning and
7130 validity as a conversion of (void*)0 to the integral type. */
7131 if (CP_INTEGRAL_TYPE_P (type)
7132 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7134 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7136 if (complain & tf_error)
7137 permerror (input_location, "cast from %qT to %qT loses precision",
7138 intype, type);
7139 else
7140 return error_mark_node;
7142 if (NULLPTR_TYPE_P (intype))
7143 return build_int_cst (type, 0);
7145 /* [expr.reinterpret.cast]
7146 A value of integral or enumeration type can be explicitly
7147 converted to a pointer. */
7148 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7149 /* OK */
7151 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7152 || TYPE_PTR_OR_PTRMEM_P (type))
7153 && same_type_p (type, intype))
7154 /* DR 799 */
7155 return rvalue (expr);
7156 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7157 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7158 return build_nop (type, expr);
7159 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7160 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7162 tree sexpr = expr;
7164 if (!c_cast_p
7165 && check_for_casting_away_constness (intype, type,
7166 REINTERPRET_CAST_EXPR,
7167 complain))
7168 return error_mark_node;
7169 /* Warn about possible alignment problems. */
7170 if (STRICT_ALIGNMENT && warn_cast_align
7171 && (complain & tf_warning)
7172 && !VOID_TYPE_P (type)
7173 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7174 && COMPLETE_TYPE_P (TREE_TYPE (type))
7175 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7176 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
7177 warning (OPT_Wcast_align, "cast from %qT to %qT "
7178 "increases required alignment of target type", intype, type);
7180 /* We need to strip nops here, because the front end likes to
7181 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7182 STRIP_NOPS (sexpr);
7183 if (warn_strict_aliasing <= 2)
7184 strict_aliasing_warning (intype, type, sexpr);
7186 return build_nop (type, expr);
7188 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7189 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7191 if (complain & tf_warning)
7192 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7193 object pointer type or vice versa is conditionally-supported." */
7194 warning (OPT_Wconditionally_supported,
7195 "casting between pointer-to-function and pointer-to-object "
7196 "is conditionally-supported");
7197 return build_nop (type, expr);
7199 else if (VECTOR_TYPE_P (type))
7200 return convert_to_vector (type, expr);
7201 else if (VECTOR_TYPE_P (intype)
7202 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7203 return convert_to_integer_nofold (type, expr);
7204 else
7206 if (valid_p)
7207 *valid_p = false;
7208 if (complain & tf_error)
7209 error ("invalid cast from type %qT to type %qT", intype, type);
7210 return error_mark_node;
7213 return cp_convert (type, expr, complain);
7216 tree
7217 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7219 tree r;
7221 if (type == error_mark_node || expr == error_mark_node)
7222 return error_mark_node;
7224 if (processing_template_decl)
7226 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7228 if (!TREE_SIDE_EFFECTS (t)
7229 && type_dependent_expression_p (expr))
7230 /* There might turn out to be side effects inside expr. */
7231 TREE_SIDE_EFFECTS (t) = 1;
7232 return convert_from_reference (t);
7235 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7236 /*valid_p=*/NULL, complain);
7237 if (r != error_mark_node)
7238 maybe_warn_about_useless_cast (type, expr, complain);
7239 return r;
7242 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7243 return an appropriate expression. Otherwise, return
7244 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7245 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7246 performing a C-style cast, its value upon return will indicate
7247 whether or not the conversion succeeded. */
7249 static tree
7250 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7251 bool *valid_p)
7253 tree src_type;
7254 tree reference_type;
7256 /* Callers are responsible for handling error_mark_node as a
7257 destination type. */
7258 gcc_assert (dst_type != error_mark_node);
7259 /* In a template, callers should be building syntactic
7260 representations of casts, not using this machinery. */
7261 gcc_assert (!processing_template_decl);
7263 /* Assume the conversion is invalid. */
7264 if (valid_p)
7265 *valid_p = false;
7267 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7269 if (complain & tf_error)
7270 error ("invalid use of const_cast with type %qT, "
7271 "which is not a pointer, "
7272 "reference, nor a pointer-to-data-member type", dst_type);
7273 return error_mark_node;
7276 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7278 if (complain & tf_error)
7279 error ("invalid use of const_cast with type %qT, which is a pointer "
7280 "or reference to a function type", dst_type);
7281 return error_mark_node;
7284 /* Save casted types in the function's used types hash table. */
7285 used_types_insert (dst_type);
7287 src_type = TREE_TYPE (expr);
7288 /* Expressions do not really have reference types. */
7289 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7290 src_type = TREE_TYPE (src_type);
7292 /* [expr.const.cast]
7294 For two object types T1 and T2, if a pointer to T1 can be explicitly
7295 converted to the type "pointer to T2" using a const_cast, then the
7296 following conversions can also be made:
7298 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7299 type T2 using the cast const_cast<T2&>;
7301 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7302 type T2 using the cast const_cast<T2&&>; and
7304 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7305 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7307 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7309 reference_type = dst_type;
7310 if (!TYPE_REF_IS_RVALUE (dst_type)
7311 ? lvalue_p (expr)
7312 : obvalue_p (expr))
7313 /* OK. */;
7314 else
7316 if (complain & tf_error)
7317 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7318 src_type, dst_type);
7319 return error_mark_node;
7321 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7322 src_type = build_pointer_type (src_type);
7324 else
7326 reference_type = NULL_TREE;
7327 /* If the destination type is not a reference type, the
7328 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7329 conversions are performed. */
7330 src_type = type_decays_to (src_type);
7331 if (src_type == error_mark_node)
7332 return error_mark_node;
7335 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7337 if (comp_ptr_ttypes_const (dst_type, src_type))
7339 if (valid_p)
7341 *valid_p = true;
7342 /* This cast is actually a C-style cast. Issue a warning if
7343 the user is making a potentially unsafe cast. */
7344 check_for_casting_away_constness (src_type, dst_type,
7345 CAST_EXPR, complain);
7347 if (reference_type)
7349 expr = cp_build_addr_expr (expr, complain);
7350 if (expr == error_mark_node)
7351 return error_mark_node;
7352 expr = build_nop (reference_type, expr);
7353 return convert_from_reference (expr);
7355 else
7357 expr = decay_conversion (expr, complain);
7358 if (expr == error_mark_node)
7359 return error_mark_node;
7361 /* build_c_cast puts on a NOP_EXPR to make the result not an
7362 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7363 non-lvalue context. */
7364 if (TREE_CODE (expr) == NOP_EXPR
7365 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7366 expr = TREE_OPERAND (expr, 0);
7367 return build_nop (dst_type, expr);
7370 else if (valid_p
7371 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7372 TREE_TYPE (src_type)))
7373 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7374 complain);
7377 if (complain & tf_error)
7378 error ("invalid const_cast from type %qT to type %qT",
7379 src_type, dst_type);
7380 return error_mark_node;
7383 tree
7384 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7386 tree r;
7388 if (type == error_mark_node || error_operand_p (expr))
7389 return error_mark_node;
7391 if (processing_template_decl)
7393 tree t = build_min (CONST_CAST_EXPR, type, expr);
7395 if (!TREE_SIDE_EFFECTS (t)
7396 && type_dependent_expression_p (expr))
7397 /* There might turn out to be side effects inside expr. */
7398 TREE_SIDE_EFFECTS (t) = 1;
7399 return convert_from_reference (t);
7402 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7403 if (r != error_mark_node)
7404 maybe_warn_about_useless_cast (type, expr, complain);
7405 return r;
7408 /* Like cp_build_c_cast, but for the c-common bits. */
7410 tree
7411 build_c_cast (location_t /*loc*/, tree type, tree expr)
7413 return cp_build_c_cast (type, expr, tf_warning_or_error);
7416 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7417 preserve location information even for tree nodes that don't
7418 support it. */
7420 cp_expr
7421 build_c_cast (location_t loc, tree type, cp_expr expr)
7423 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7424 result.set_location (loc);
7425 return result;
7428 /* Build an expression representing an explicit C-style cast to type
7429 TYPE of expression EXPR. */
7431 tree
7432 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7434 tree value = expr;
7435 tree result;
7436 bool valid_p;
7438 if (type == error_mark_node || error_operand_p (expr))
7439 return error_mark_node;
7441 if (processing_template_decl)
7443 tree t = build_min (CAST_EXPR, type,
7444 tree_cons (NULL_TREE, value, NULL_TREE));
7445 /* We don't know if it will or will not have side effects. */
7446 TREE_SIDE_EFFECTS (t) = 1;
7447 return convert_from_reference (t);
7450 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7451 'Class') should always be retained, because this information aids
7452 in method lookup. */
7453 if (objc_is_object_ptr (type)
7454 && objc_is_object_ptr (TREE_TYPE (expr)))
7455 return build_nop (type, expr);
7457 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7458 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7459 if (TREE_CODE (type) != REFERENCE_TYPE
7460 && TREE_CODE (value) == NOP_EXPR
7461 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7462 value = TREE_OPERAND (value, 0);
7464 if (TREE_CODE (type) == ARRAY_TYPE)
7466 /* Allow casting from T1* to T2[] because Cfront allows it.
7467 NIHCL uses it. It is not valid ISO C++ however. */
7468 if (TYPE_PTR_P (TREE_TYPE (expr)))
7470 if (complain & tf_error)
7471 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7472 else
7473 return error_mark_node;
7474 type = build_pointer_type (TREE_TYPE (type));
7476 else
7478 if (complain & tf_error)
7479 error ("ISO C++ forbids casting to an array type %qT", type);
7480 return error_mark_node;
7484 if (TREE_CODE (type) == FUNCTION_TYPE
7485 || TREE_CODE (type) == METHOD_TYPE)
7487 if (complain & tf_error)
7488 error ("invalid cast to function type %qT", type);
7489 return error_mark_node;
7492 if (TYPE_PTR_P (type)
7493 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7494 /* Casting to an integer of smaller size is an error detected elsewhere. */
7495 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7496 /* Don't warn about converting any constant. */
7497 && !TREE_CONSTANT (value))
7498 warning_at (input_location, OPT_Wint_to_pointer_cast,
7499 "cast to pointer from integer of different size");
7501 /* A C-style cast can be a const_cast. */
7502 result = build_const_cast_1 (type, value, complain & tf_warning,
7503 &valid_p);
7504 if (valid_p)
7506 if (result != error_mark_node)
7507 maybe_warn_about_useless_cast (type, value, complain);
7508 return result;
7511 /* Or a static cast. */
7512 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7513 &valid_p, complain);
7514 /* Or a reinterpret_cast. */
7515 if (!valid_p)
7516 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7517 &valid_p, complain);
7518 /* The static_cast or reinterpret_cast may be followed by a
7519 const_cast. */
7520 if (valid_p
7521 /* A valid cast may result in errors if, for example, a
7522 conversion to an ambiguous base class is required. */
7523 && !error_operand_p (result))
7525 tree result_type;
7527 maybe_warn_about_useless_cast (type, value, complain);
7529 /* Non-class rvalues always have cv-unqualified type. */
7530 if (!CLASS_TYPE_P (type))
7531 type = TYPE_MAIN_VARIANT (type);
7532 result_type = TREE_TYPE (result);
7533 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7534 result_type = TYPE_MAIN_VARIANT (result_type);
7535 /* If the type of RESULT does not match TYPE, perform a
7536 const_cast to make it match. If the static_cast or
7537 reinterpret_cast succeeded, we will differ by at most
7538 cv-qualification, so the follow-on const_cast is guaranteed
7539 to succeed. */
7540 if (!same_type_p (non_reference (type), non_reference (result_type)))
7542 result = build_const_cast_1 (type, result, false, &valid_p);
7543 gcc_assert (valid_p);
7545 return result;
7548 return error_mark_node;
7551 /* For use from the C common bits. */
7552 tree
7553 build_modify_expr (location_t location,
7554 tree lhs, tree /*lhs_origtype*/,
7555 enum tree_code modifycode,
7556 location_t /*rhs_location*/, tree rhs,
7557 tree /*rhs_origtype*/)
7559 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7560 tf_warning_or_error);
7563 /* Build an assignment expression of lvalue LHS from value RHS.
7564 MODIFYCODE is the code for a binary operator that we use
7565 to combine the old value of LHS with RHS to get the new value.
7566 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7568 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7570 tree
7571 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7572 tree rhs, tsubst_flags_t complain)
7574 tree result = NULL_TREE;
7575 tree newrhs = rhs;
7576 tree lhstype = TREE_TYPE (lhs);
7577 tree olhs = lhs;
7578 tree olhstype = lhstype;
7579 bool plain_assign = (modifycode == NOP_EXPR);
7580 bool compound_side_effects_p = false;
7581 tree preeval = NULL_TREE;
7583 /* Avoid duplicate error messages from operands that had errors. */
7584 if (error_operand_p (lhs) || error_operand_p (rhs))
7585 return error_mark_node;
7587 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7589 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7590 compound_side_effects_p = true;
7591 lhs = TREE_OPERAND (lhs, 1);
7594 /* Handle control structure constructs used as "lvalues". Note that we
7595 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7596 switch (TREE_CODE (lhs))
7598 /* Handle --foo = 5; as these are valid constructs in C++. */
7599 case PREDECREMENT_EXPR:
7600 case PREINCREMENT_EXPR:
7601 if (compound_side_effects_p)
7602 newrhs = rhs = stabilize_expr (rhs, &preeval);
7603 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7604 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7605 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7606 TREE_OPERAND (lhs, 1));
7607 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7608 maybe_add_compound:
7609 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7610 and looked through the COMPOUND_EXPRs, readd them now around
7611 the resulting lhs. */
7612 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7614 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7615 tree *ptr = &TREE_OPERAND (lhs, 1);
7616 for (olhs = TREE_OPERAND (olhs, 1);
7617 TREE_CODE (olhs) == COMPOUND_EXPR;
7618 olhs = TREE_OPERAND (olhs, 1))
7620 *ptr = build2 (COMPOUND_EXPR, lhstype,
7621 TREE_OPERAND (olhs, 0), *ptr);
7622 ptr = &TREE_OPERAND (*ptr, 1);
7625 break;
7627 case MODIFY_EXPR:
7628 if (compound_side_effects_p)
7629 newrhs = rhs = stabilize_expr (rhs, &preeval);
7630 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7631 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7632 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7633 TREE_OPERAND (lhs, 1));
7634 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7635 goto maybe_add_compound;
7637 case MIN_EXPR:
7638 case MAX_EXPR:
7639 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7640 when neither operand has side-effects. */
7641 if (!lvalue_or_else (lhs, lv_assign, complain))
7642 return error_mark_node;
7644 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7645 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7647 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7648 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7649 boolean_type_node,
7650 TREE_OPERAND (lhs, 0),
7651 TREE_OPERAND (lhs, 1)),
7652 TREE_OPERAND (lhs, 0),
7653 TREE_OPERAND (lhs, 1));
7654 gcc_fallthrough ();
7656 /* Handle (a ? b : c) used as an "lvalue". */
7657 case COND_EXPR:
7659 /* Produce (a ? (b = rhs) : (c = rhs))
7660 except that the RHS goes through a save-expr
7661 so the code to compute it is only emitted once. */
7662 tree cond;
7664 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7666 if (complain & tf_error)
7667 error ("void value not ignored as it ought to be");
7668 return error_mark_node;
7671 rhs = stabilize_expr (rhs, &preeval);
7673 /* Check this here to avoid odd errors when trying to convert
7674 a throw to the type of the COND_EXPR. */
7675 if (!lvalue_or_else (lhs, lv_assign, complain))
7676 return error_mark_node;
7678 cond = build_conditional_expr
7679 (input_location, TREE_OPERAND (lhs, 0),
7680 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7681 modifycode, rhs, complain),
7682 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7683 modifycode, rhs, complain),
7684 complain);
7686 if (cond == error_mark_node)
7687 return cond;
7688 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7689 and looked through the COMPOUND_EXPRs, readd them now around
7690 the resulting cond before adding the preevaluated rhs. */
7691 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7693 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7694 TREE_OPERAND (olhs, 0), cond);
7695 tree *ptr = &TREE_OPERAND (cond, 1);
7696 for (olhs = TREE_OPERAND (olhs, 1);
7697 TREE_CODE (olhs) == COMPOUND_EXPR;
7698 olhs = TREE_OPERAND (olhs, 1))
7700 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7701 TREE_OPERAND (olhs, 0), *ptr);
7702 ptr = &TREE_OPERAND (*ptr, 1);
7705 /* Make sure the code to compute the rhs comes out
7706 before the split. */
7707 result = cond;
7708 goto ret;
7711 default:
7712 lhs = olhs;
7713 break;
7716 if (modifycode == INIT_EXPR)
7718 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7719 /* Do the default thing. */;
7720 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7722 /* Compound literal. */
7723 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7724 /* Call convert to generate an error; see PR 11063. */
7725 rhs = convert (lhstype, rhs);
7726 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7727 TREE_SIDE_EFFECTS (result) = 1;
7728 goto ret;
7730 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7731 /* Do the default thing. */;
7732 else
7734 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7735 result = build_special_member_call (lhs, complete_ctor_identifier,
7736 &rhs_vec, lhstype, LOOKUP_NORMAL,
7737 complain);
7738 release_tree_vector (rhs_vec);
7739 if (result == NULL_TREE)
7740 return error_mark_node;
7741 goto ret;
7744 else
7746 lhs = require_complete_type_sfinae (lhs, complain);
7747 if (lhs == error_mark_node)
7748 return error_mark_node;
7750 if (modifycode == NOP_EXPR)
7752 if (c_dialect_objc ())
7754 result = objc_maybe_build_modify_expr (lhs, rhs);
7755 if (result)
7756 goto ret;
7759 /* `operator=' is not an inheritable operator. */
7760 if (! MAYBE_CLASS_TYPE_P (lhstype))
7761 /* Do the default thing. */;
7762 else
7764 result = build_new_op (input_location, MODIFY_EXPR,
7765 LOOKUP_NORMAL, lhs, rhs,
7766 make_node (NOP_EXPR), /*overload=*/NULL,
7767 complain);
7768 if (result == NULL_TREE)
7769 return error_mark_node;
7770 goto ret;
7772 lhstype = olhstype;
7774 else
7776 tree init = NULL_TREE;
7778 /* A binary op has been requested. Combine the old LHS
7779 value with the RHS producing the value we should actually
7780 store into the LHS. */
7781 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7782 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7783 || MAYBE_CLASS_TYPE_P (lhstype)));
7785 /* Preevaluate the RHS to make sure its evaluation is complete
7786 before the lvalue-to-rvalue conversion of the LHS:
7788 [expr.ass] With respect to an indeterminately-sequenced
7789 function call, the operation of a compound assignment is a
7790 single evaluation. [ Note: Therefore, a function call shall
7791 not intervene between the lvalue-to-rvalue conversion and the
7792 side effect associated with any single compound assignment
7793 operator. -- end note ] */
7794 lhs = cp_stabilize_reference (lhs);
7795 rhs = rvalue (rhs);
7796 rhs = stabilize_expr (rhs, &init);
7797 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
7798 if (newrhs == error_mark_node)
7800 if (complain & tf_error)
7801 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7802 TREE_TYPE (lhs), TREE_TYPE (rhs));
7803 return error_mark_node;
7806 if (init)
7807 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7809 /* Now it looks like a plain assignment. */
7810 modifycode = NOP_EXPR;
7811 if (c_dialect_objc ())
7813 result = objc_maybe_build_modify_expr (lhs, newrhs);
7814 if (result)
7815 goto ret;
7818 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7819 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7822 /* The left-hand side must be an lvalue. */
7823 if (!lvalue_or_else (lhs, lv_assign, complain))
7824 return error_mark_node;
7826 /* Warn about modifying something that is `const'. Don't warn if
7827 this is initialization. */
7828 if (modifycode != INIT_EXPR
7829 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7830 /* Functions are not modifiable, even though they are
7831 lvalues. */
7832 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7833 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7834 /* If it's an aggregate and any field is const, then it is
7835 effectively const. */
7836 || (CLASS_TYPE_P (lhstype)
7837 && C_TYPE_FIELDS_READONLY (lhstype))))
7839 if (complain & tf_error)
7840 cxx_readonly_error (lhs, lv_assign);
7841 else
7842 return error_mark_node;
7845 /* If storing into a structure or union member, it may have been given a
7846 lowered bitfield type. We need to convert to the declared type first,
7847 so retrieve it now. */
7849 olhstype = unlowered_expr_type (lhs);
7851 /* Convert new value to destination type. */
7853 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7855 int from_array;
7857 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7859 if (modifycode != INIT_EXPR)
7861 if (complain & tf_error)
7862 error ("assigning to an array from an initializer list");
7863 return error_mark_node;
7865 if (check_array_initializer (lhs, lhstype, newrhs))
7866 return error_mark_node;
7867 newrhs = digest_init (lhstype, newrhs, complain);
7868 if (newrhs == error_mark_node)
7869 return error_mark_node;
7872 /* C++11 8.5/17: "If the destination type is an array of characters,
7873 an array of char16_t, an array of char32_t, or an array of wchar_t,
7874 and the initializer is a string literal...". */
7875 else if (TREE_CODE (newrhs) == STRING_CST
7876 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7877 && modifycode == INIT_EXPR)
7879 newrhs = digest_init (lhstype, newrhs, complain);
7880 if (newrhs == error_mark_node)
7881 return error_mark_node;
7884 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7885 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7887 if (complain & tf_error)
7888 error ("incompatible types in assignment of %qT to %qT",
7889 TREE_TYPE (rhs), lhstype);
7890 return error_mark_node;
7893 /* Allow array assignment in compiler-generated code. */
7894 else if (!current_function_decl
7895 || !DECL_DEFAULTED_FN (current_function_decl))
7897 /* This routine is used for both initialization and assignment.
7898 Make sure the diagnostic message differentiates the context. */
7899 if (complain & tf_error)
7901 if (modifycode == INIT_EXPR)
7902 error ("array used as initializer");
7903 else
7904 error ("invalid array assignment");
7906 return error_mark_node;
7909 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7910 ? 1 + (modifycode != INIT_EXPR): 0;
7911 result = build_vec_init (lhs, NULL_TREE, newrhs,
7912 /*explicit_value_init_p=*/false,
7913 from_array, complain);
7914 goto ret;
7917 if (modifycode == INIT_EXPR)
7918 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7919 LOOKUP_ONLYCONVERTING. */
7920 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7921 ICR_INIT, NULL_TREE, 0,
7922 complain);
7923 else
7924 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7925 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7927 if (!same_type_p (lhstype, olhstype))
7928 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7930 if (modifycode != INIT_EXPR)
7932 if (TREE_CODE (newrhs) == CALL_EXPR
7933 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7934 newrhs = build_cplus_new (lhstype, newrhs, complain);
7936 /* Can't initialize directly from a TARGET_EXPR, since that would
7937 cause the lhs to be constructed twice, and possibly result in
7938 accidental self-initialization. So we force the TARGET_EXPR to be
7939 expanded without a target. */
7940 if (TREE_CODE (newrhs) == TARGET_EXPR)
7941 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7942 TREE_OPERAND (newrhs, 0));
7945 if (newrhs == error_mark_node)
7946 return error_mark_node;
7948 if (c_dialect_objc () && flag_objc_gc)
7950 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7952 if (result)
7953 goto ret;
7956 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7957 lhstype, lhs, newrhs);
7959 TREE_SIDE_EFFECTS (result) = 1;
7960 if (!plain_assign)
7961 TREE_NO_WARNING (result) = 1;
7963 ret:
7964 if (preeval)
7965 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
7966 return result;
7969 cp_expr
7970 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7971 tree rhs, tsubst_flags_t complain)
7973 tree orig_lhs = lhs;
7974 tree orig_rhs = rhs;
7975 tree overload = NULL_TREE;
7976 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
7978 if (processing_template_decl)
7980 if (modifycode == NOP_EXPR
7981 || type_dependent_expression_p (lhs)
7982 || type_dependent_expression_p (rhs))
7983 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7984 build_min_nt_loc (loc, modifycode, NULL_TREE,
7985 NULL_TREE), rhs);
7987 lhs = build_non_dependent_expr (lhs);
7988 rhs = build_non_dependent_expr (rhs);
7991 if (modifycode != NOP_EXPR)
7993 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
7994 lhs, rhs, op, &overload, complain);
7995 if (rval)
7997 if (rval == error_mark_node)
7998 return rval;
7999 TREE_NO_WARNING (rval) = 1;
8000 if (processing_template_decl)
8002 if (overload != NULL_TREE)
8003 return (build_min_non_dep_op_overload
8004 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8006 return (build_min_non_dep
8007 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8009 return rval;
8012 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8015 /* Helper function for get_delta_difference which assumes FROM is a base
8016 class of TO. Returns a delta for the conversion of pointer-to-member
8017 of FROM to pointer-to-member of TO. If the conversion is invalid and
8018 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8019 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8020 If C_CAST_P is true, this conversion is taking place as part of a
8021 C-style cast. */
8023 static tree
8024 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8025 tsubst_flags_t complain)
8027 tree binfo;
8028 base_kind kind;
8030 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8031 &kind, complain);
8033 if (binfo == error_mark_node)
8035 if (!(complain & tf_error))
8036 return error_mark_node;
8038 error (" in pointer to member function conversion");
8039 return size_zero_node;
8041 else if (binfo)
8043 if (kind != bk_via_virtual)
8044 return BINFO_OFFSET (binfo);
8045 else
8046 /* FROM is a virtual base class of TO. Issue an error or warning
8047 depending on whether or not this is a reinterpret cast. */
8049 if (!(complain & tf_error))
8050 return error_mark_node;
8052 error ("pointer to member conversion via virtual base %qT",
8053 BINFO_TYPE (binfo_from_vbase (binfo)));
8055 return size_zero_node;
8058 else
8059 return NULL_TREE;
8062 /* Get difference in deltas for different pointer to member function
8063 types. If the conversion is invalid and tf_error is not set in
8064 COMPLAIN, returns error_mark_node, otherwise returns an integer
8065 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8066 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8067 conversions as well. If C_CAST_P is true this conversion is taking
8068 place as part of a C-style cast.
8070 Note that the naming of FROM and TO is kind of backwards; the return
8071 value is what we add to a TO in order to get a FROM. They are named
8072 this way because we call this function to find out how to convert from
8073 a pointer to member of FROM to a pointer to member of TO. */
8075 static tree
8076 get_delta_difference (tree from, tree to,
8077 bool allow_inverse_p,
8078 bool c_cast_p, tsubst_flags_t complain)
8080 tree result;
8082 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8083 /* Pointer to member of incomplete class is permitted*/
8084 result = size_zero_node;
8085 else
8086 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8088 if (result == error_mark_node)
8089 return error_mark_node;
8091 if (!result)
8093 if (!allow_inverse_p)
8095 if (!(complain & tf_error))
8096 return error_mark_node;
8098 error_not_base_type (from, to);
8099 error (" in pointer to member conversion");
8100 result = size_zero_node;
8102 else
8104 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8106 if (result == error_mark_node)
8107 return error_mark_node;
8109 if (result)
8110 result = size_diffop_loc (input_location,
8111 size_zero_node, result);
8112 else
8114 if (!(complain & tf_error))
8115 return error_mark_node;
8117 error_not_base_type (from, to);
8118 error (" in pointer to member conversion");
8119 result = size_zero_node;
8124 return convert_to_integer (ptrdiff_type_node, result);
8127 /* Return a constructor for the pointer-to-member-function TYPE using
8128 the other components as specified. */
8130 tree
8131 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8133 tree u = NULL_TREE;
8134 tree delta_field;
8135 tree pfn_field;
8136 vec<constructor_elt, va_gc> *v;
8138 /* Pull the FIELD_DECLs out of the type. */
8139 pfn_field = TYPE_FIELDS (type);
8140 delta_field = DECL_CHAIN (pfn_field);
8142 /* Make sure DELTA has the type we want. */
8143 delta = convert_and_check (input_location, delta_type_node, delta);
8145 /* Convert to the correct target type if necessary. */
8146 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8148 /* Finish creating the initializer. */
8149 vec_alloc (v, 2);
8150 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8151 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8152 u = build_constructor (type, v);
8153 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8154 TREE_STATIC (u) = (TREE_CONSTANT (u)
8155 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8156 != NULL_TREE)
8157 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8158 != NULL_TREE));
8159 return u;
8162 /* Build a constructor for a pointer to member function. It can be
8163 used to initialize global variables, local variable, or used
8164 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8165 want to be.
8167 If FORCE is nonzero, then force this conversion, even if
8168 we would rather not do it. Usually set when using an explicit
8169 cast. A C-style cast is being processed iff C_CAST_P is true.
8171 Return error_mark_node, if something goes wrong. */
8173 tree
8174 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8175 tsubst_flags_t complain)
8177 tree fn;
8178 tree pfn_type;
8179 tree to_type;
8181 if (error_operand_p (pfn))
8182 return error_mark_node;
8184 pfn_type = TREE_TYPE (pfn);
8185 to_type = build_ptrmemfunc_type (type);
8187 /* Handle multiple conversions of pointer to member functions. */
8188 if (TYPE_PTRMEMFUNC_P (pfn_type))
8190 tree delta = NULL_TREE;
8191 tree npfn = NULL_TREE;
8192 tree n;
8194 if (!force
8195 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8196 LOOKUP_NORMAL, complain))
8198 if (complain & tf_error)
8199 error ("invalid conversion to type %qT from type %qT",
8200 to_type, pfn_type);
8201 else
8202 return error_mark_node;
8205 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8206 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8207 force,
8208 c_cast_p, complain);
8209 if (n == error_mark_node)
8210 return error_mark_node;
8212 /* We don't have to do any conversion to convert a
8213 pointer-to-member to its own type. But, we don't want to
8214 just return a PTRMEM_CST if there's an explicit cast; that
8215 cast should make the expression an invalid template argument. */
8216 if (TREE_CODE (pfn) != PTRMEM_CST)
8218 if (same_type_p (to_type, pfn_type))
8219 return pfn;
8220 else if (integer_zerop (n))
8221 return build_reinterpret_cast (to_type, pfn,
8222 complain);
8225 if (TREE_SIDE_EFFECTS (pfn))
8226 pfn = save_expr (pfn);
8228 /* Obtain the function pointer and the current DELTA. */
8229 if (TREE_CODE (pfn) == PTRMEM_CST)
8230 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8231 else
8233 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8234 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8237 /* Just adjust the DELTA field. */
8238 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8239 (TREE_TYPE (delta), ptrdiff_type_node));
8240 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8241 n = cp_build_binary_op (input_location,
8242 LSHIFT_EXPR, n, integer_one_node,
8243 complain);
8244 delta = cp_build_binary_op (input_location,
8245 PLUS_EXPR, delta, n, complain);
8246 return build_ptrmemfunc1 (to_type, delta, npfn);
8249 /* Handle null pointer to member function conversions. */
8250 if (null_ptr_cst_p (pfn))
8252 pfn = cp_build_c_cast (type, pfn, complain);
8253 return build_ptrmemfunc1 (to_type,
8254 integer_zero_node,
8255 pfn);
8258 if (type_unknown_p (pfn))
8259 return instantiate_type (type, pfn, complain);
8261 fn = TREE_OPERAND (pfn, 0);
8262 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8263 /* In a template, we will have preserved the
8264 OFFSET_REF. */
8265 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8266 return make_ptrmem_cst (to_type, fn);
8269 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8270 given by CST.
8272 ??? There is no consistency as to the types returned for the above
8273 values. Some code acts as if it were a sizetype and some as if it were
8274 integer_type_node. */
8276 void
8277 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8279 tree type = TREE_TYPE (cst);
8280 tree fn = PTRMEM_CST_MEMBER (cst);
8281 tree ptr_class, fn_class;
8283 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8285 /* The class that the function belongs to. */
8286 fn_class = DECL_CONTEXT (fn);
8288 /* The class that we're creating a pointer to member of. */
8289 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8291 /* First, calculate the adjustment to the function's class. */
8292 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8293 /*c_cast_p=*/0, tf_warning_or_error);
8295 if (!DECL_VIRTUAL_P (fn))
8296 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8297 build_addr_func (fn, tf_warning_or_error));
8298 else
8300 /* If we're dealing with a virtual function, we have to adjust 'this'
8301 again, to point to the base which provides the vtable entry for
8302 fn; the call will do the opposite adjustment. */
8303 tree orig_class = DECL_CONTEXT (fn);
8304 tree binfo = binfo_or_else (orig_class, fn_class);
8305 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8306 *delta, BINFO_OFFSET (binfo));
8308 /* We set PFN to the vtable offset at which the function can be
8309 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8310 case delta is shifted left, and then incremented). */
8311 *pfn = DECL_VINDEX (fn);
8312 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8313 TYPE_SIZE_UNIT (vtable_entry_type));
8315 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8317 case ptrmemfunc_vbit_in_pfn:
8318 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8319 integer_one_node);
8320 break;
8322 case ptrmemfunc_vbit_in_delta:
8323 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8324 *delta, integer_one_node);
8325 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8326 *delta, integer_one_node);
8327 break;
8329 default:
8330 gcc_unreachable ();
8333 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8337 /* Return an expression for PFN from the pointer-to-member function
8338 given by T. */
8340 static tree
8341 pfn_from_ptrmemfunc (tree t)
8343 if (TREE_CODE (t) == PTRMEM_CST)
8345 tree delta;
8346 tree pfn;
8348 expand_ptrmemfunc_cst (t, &delta, &pfn);
8349 if (pfn)
8350 return pfn;
8353 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8356 /* Return an expression for DELTA from the pointer-to-member function
8357 given by T. */
8359 static tree
8360 delta_from_ptrmemfunc (tree t)
8362 if (TREE_CODE (t) == PTRMEM_CST)
8364 tree delta;
8365 tree pfn;
8367 expand_ptrmemfunc_cst (t, &delta, &pfn);
8368 if (delta)
8369 return delta;
8372 return build_ptrmemfunc_access_expr (t, delta_identifier);
8375 /* Convert value RHS to type TYPE as preparation for an assignment to
8376 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8377 implicit conversion is. If FNDECL is non-NULL, we are doing the
8378 conversion in order to pass the PARMNUMth argument of FNDECL.
8379 If FNDECL is NULL, we are doing the conversion in function pointer
8380 argument passing, conversion in initialization, etc. */
8382 static tree
8383 convert_for_assignment (tree type, tree rhs,
8384 impl_conv_rhs errtype, tree fndecl, int parmnum,
8385 tsubst_flags_t complain, int flags)
8387 tree rhstype;
8388 enum tree_code coder;
8390 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8391 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8392 rhs = TREE_OPERAND (rhs, 0);
8394 /* Handle [dcl.init.list] direct-list-initialization from
8395 single element of enumeration with a fixed underlying type. */
8396 if (is_direct_enum_init (type, rhs))
8398 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8399 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8400 rhs = cp_build_c_cast (type, elt, complain);
8401 else
8402 rhs = error_mark_node;
8405 rhstype = TREE_TYPE (rhs);
8406 coder = TREE_CODE (rhstype);
8408 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8409 && vector_types_convertible_p (type, rhstype, true))
8411 rhs = mark_rvalue_use (rhs);
8412 return convert (type, rhs);
8415 if (rhs == error_mark_node || rhstype == error_mark_node)
8416 return error_mark_node;
8417 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8418 return error_mark_node;
8420 /* The RHS of an assignment cannot have void type. */
8421 if (coder == VOID_TYPE)
8423 if (complain & tf_error)
8424 error ("void value not ignored as it ought to be");
8425 return error_mark_node;
8428 if (c_dialect_objc ())
8430 int parmno;
8431 tree selector;
8432 tree rname = fndecl;
8434 switch (errtype)
8436 case ICR_ASSIGN:
8437 parmno = -1;
8438 break;
8439 case ICR_INIT:
8440 parmno = -2;
8441 break;
8442 default:
8443 selector = objc_message_selector ();
8444 parmno = parmnum;
8445 if (selector && parmno > 1)
8447 rname = selector;
8448 parmno -= 1;
8452 if (objc_compare_types (type, rhstype, parmno, rname))
8454 rhs = mark_rvalue_use (rhs);
8455 return convert (type, rhs);
8459 /* [expr.ass]
8461 The expression is implicitly converted (clause _conv_) to the
8462 cv-unqualified type of the left operand.
8464 We allow bad conversions here because by the time we get to this point
8465 we are committed to doing the conversion. If we end up doing a bad
8466 conversion, convert_like will complain. */
8467 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8469 /* When -Wno-pmf-conversions is use, we just silently allow
8470 conversions from pointers-to-members to plain pointers. If
8471 the conversion doesn't work, cp_convert will complain. */
8472 if (!warn_pmf2ptr
8473 && TYPE_PTR_P (type)
8474 && TYPE_PTRMEMFUNC_P (rhstype))
8475 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8476 else
8478 if (complain & tf_error)
8480 /* If the right-hand side has unknown type, then it is an
8481 overloaded function. Call instantiate_type to get error
8482 messages. */
8483 if (rhstype == unknown_type_node)
8484 instantiate_type (type, rhs, tf_warning_or_error);
8485 else if (fndecl)
8486 error ("cannot convert %qT to %qT for argument %qP to %qD",
8487 rhstype, type, parmnum, fndecl);
8488 else
8489 switch (errtype)
8491 case ICR_DEFAULT_ARGUMENT:
8492 error ("cannot convert %qT to %qT in default argument",
8493 rhstype, type);
8494 break;
8495 case ICR_ARGPASS:
8496 error ("cannot convert %qT to %qT in argument passing",
8497 rhstype, type);
8498 break;
8499 case ICR_CONVERTING:
8500 error ("cannot convert %qT to %qT",
8501 rhstype, type);
8502 break;
8503 case ICR_INIT:
8504 error ("cannot convert %qT to %qT in initialization",
8505 rhstype, type);
8506 break;
8507 case ICR_RETURN:
8508 error ("cannot convert %qT to %qT in return",
8509 rhstype, type);
8510 break;
8511 case ICR_ASSIGN:
8512 error ("cannot convert %qT to %qT in assignment",
8513 rhstype, type);
8514 break;
8515 default:
8516 gcc_unreachable();
8518 if (TYPE_PTR_P (rhstype)
8519 && TYPE_PTR_P (type)
8520 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8521 && CLASS_TYPE_P (TREE_TYPE (type))
8522 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8523 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8524 (TREE_TYPE (rhstype))),
8525 "class type %qT is incomplete", TREE_TYPE (rhstype));
8527 return error_mark_node;
8530 if (warn_suggest_attribute_format)
8532 const enum tree_code codel = TREE_CODE (type);
8533 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8534 && coder == codel
8535 && check_missing_format_attribute (type, rhstype)
8536 && (complain & tf_warning))
8537 switch (errtype)
8539 case ICR_ARGPASS:
8540 case ICR_DEFAULT_ARGUMENT:
8541 if (fndecl)
8542 warning (OPT_Wsuggest_attribute_format,
8543 "parameter %qP of %qD might be a candidate "
8544 "for a format attribute", parmnum, fndecl);
8545 else
8546 warning (OPT_Wsuggest_attribute_format,
8547 "parameter might be a candidate "
8548 "for a format attribute");
8549 break;
8550 case ICR_CONVERTING:
8551 warning (OPT_Wsuggest_attribute_format,
8552 "target of conversion might be a candidate "
8553 "for a format attribute");
8554 break;
8555 case ICR_INIT:
8556 warning (OPT_Wsuggest_attribute_format,
8557 "target of initialization might be a candidate "
8558 "for a format attribute");
8559 break;
8560 case ICR_RETURN:
8561 warning (OPT_Wsuggest_attribute_format,
8562 "return type might be a candidate "
8563 "for a format attribute");
8564 break;
8565 case ICR_ASSIGN:
8566 warning (OPT_Wsuggest_attribute_format,
8567 "left-hand side of assignment might be a candidate "
8568 "for a format attribute");
8569 break;
8570 default:
8571 gcc_unreachable();
8575 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8576 does not. */
8577 if (warn_parentheses
8578 && TREE_CODE (type) == BOOLEAN_TYPE
8579 && TREE_CODE (rhs) == MODIFY_EXPR
8580 && !TREE_NO_WARNING (rhs)
8581 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8582 && (complain & tf_warning))
8584 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8586 warning_at (loc, OPT_Wparentheses,
8587 "suggest parentheses around assignment used as truth value");
8588 TREE_NO_WARNING (rhs) = 1;
8591 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8592 complain, flags);
8595 /* Convert RHS to be of type TYPE.
8596 If EXP is nonzero, it is the target of the initialization.
8597 ERRTYPE indicates what kind of error the implicit conversion is.
8599 Two major differences between the behavior of
8600 `convert_for_assignment' and `convert_for_initialization'
8601 are that references are bashed in the former, while
8602 copied in the latter, and aggregates are assigned in
8603 the former (operator=) while initialized in the
8604 latter (X(X&)).
8606 If using constructor make sure no conversion operator exists, if one does
8607 exist, an ambiguity exists. */
8609 tree
8610 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8611 impl_conv_rhs errtype, tree fndecl, int parmnum,
8612 tsubst_flags_t complain)
8614 enum tree_code codel = TREE_CODE (type);
8615 tree rhstype;
8616 enum tree_code coder;
8618 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8619 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8620 if (TREE_CODE (rhs) == NOP_EXPR
8621 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8622 && codel != REFERENCE_TYPE)
8623 rhs = TREE_OPERAND (rhs, 0);
8625 if (type == error_mark_node
8626 || rhs == error_mark_node
8627 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8628 return error_mark_node;
8630 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8632 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8633 && TREE_CODE (type) != ARRAY_TYPE
8634 && (TREE_CODE (type) != REFERENCE_TYPE
8635 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8636 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8637 && !TYPE_REFFN_P (type))
8638 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8639 rhs = decay_conversion (rhs, complain);
8641 rhstype = TREE_TYPE (rhs);
8642 coder = TREE_CODE (rhstype);
8644 if (coder == ERROR_MARK)
8645 return error_mark_node;
8647 /* We accept references to incomplete types, so we can
8648 return here before checking if RHS is of complete type. */
8650 if (codel == REFERENCE_TYPE)
8652 /* This should eventually happen in convert_arguments. */
8653 int savew = 0, savee = 0;
8655 if (fndecl)
8656 savew = warningcount + werrorcount, savee = errorcount;
8657 rhs = initialize_reference (type, rhs, flags, complain);
8659 if (fndecl
8660 && (warningcount + werrorcount > savew || errorcount > savee))
8661 inform (DECL_SOURCE_LOCATION (fndecl),
8662 "in passing argument %P of %qD", parmnum, fndecl);
8664 return rhs;
8667 if (exp != 0)
8668 exp = require_complete_type_sfinae (exp, complain);
8669 if (exp == error_mark_node)
8670 return error_mark_node;
8672 rhstype = non_reference (rhstype);
8674 type = complete_type (type);
8676 if (DIRECT_INIT_EXPR_P (type, rhs))
8677 /* Don't try to do copy-initialization if we already have
8678 direct-initialization. */
8679 return rhs;
8681 if (MAYBE_CLASS_TYPE_P (type))
8682 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8684 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8685 complain, flags);
8688 /* If RETVAL is the address of, or a reference to, a local variable or
8689 temporary give an appropriate warning and return true. */
8691 static bool
8692 maybe_warn_about_returning_address_of_local (tree retval)
8694 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8695 tree whats_returned = fold_for_warn (retval);
8697 for (;;)
8699 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8700 whats_returned = TREE_OPERAND (whats_returned, 1);
8701 else if (CONVERT_EXPR_P (whats_returned)
8702 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8703 whats_returned = TREE_OPERAND (whats_returned, 0);
8704 else
8705 break;
8708 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8709 return false;
8710 whats_returned = TREE_OPERAND (whats_returned, 0);
8712 while (TREE_CODE (whats_returned) == COMPONENT_REF
8713 || TREE_CODE (whats_returned) == ARRAY_REF)
8714 whats_returned = TREE_OPERAND (whats_returned, 0);
8716 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8718 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8719 || TREE_CODE (whats_returned) == TARGET_EXPR)
8721 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8722 return true;
8724 if (VAR_P (whats_returned)
8725 && DECL_NAME (whats_returned)
8726 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8728 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8729 return true;
8733 if (DECL_P (whats_returned)
8734 && DECL_NAME (whats_returned)
8735 && DECL_FUNCTION_SCOPE_P (whats_returned)
8736 && !is_capture_proxy (whats_returned)
8737 && !(TREE_STATIC (whats_returned)
8738 || TREE_PUBLIC (whats_returned)))
8740 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8741 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8742 OPT_Wreturn_local_addr,
8743 "reference to local variable %qD returned",
8744 whats_returned);
8745 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8746 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8747 OPT_Wreturn_local_addr, "address of label %qD returned",
8748 whats_returned);
8749 else
8750 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8751 OPT_Wreturn_local_addr, "address of local variable %qD "
8752 "returned", whats_returned);
8753 return true;
8756 return false;
8759 /* Check that returning RETVAL from the current function is valid.
8760 Return an expression explicitly showing all conversions required to
8761 change RETVAL into the function return type, and to assign it to
8762 the DECL_RESULT for the function. Set *NO_WARNING to true if
8763 code reaches end of non-void function warning shouldn't be issued
8764 on this RETURN_EXPR. */
8766 tree
8767 check_return_expr (tree retval, bool *no_warning)
8769 tree result;
8770 /* The type actually returned by the function. */
8771 tree valtype;
8772 /* The type the function is declared to return, or void if
8773 the declared type is incomplete. */
8774 tree functype;
8775 int fn_returns_value_p;
8776 bool named_return_value_okay_p;
8778 *no_warning = false;
8780 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8782 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8783 "statement is not allowed");
8784 return NULL_TREE;
8787 /* A `volatile' function is one that isn't supposed to return, ever.
8788 (This is a G++ extension, used to get better code for functions
8789 that call the `volatile' function.) */
8790 if (TREE_THIS_VOLATILE (current_function_decl))
8791 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8793 /* Check for various simple errors. */
8794 if (DECL_DESTRUCTOR_P (current_function_decl))
8796 if (retval)
8797 error ("returning a value from a destructor");
8798 return NULL_TREE;
8800 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8802 if (in_function_try_handler)
8803 /* If a return statement appears in a handler of the
8804 function-try-block of a constructor, the program is ill-formed. */
8805 error ("cannot return from a handler of a function-try-block of a constructor");
8806 else if (retval)
8807 /* You can't return a value from a constructor. */
8808 error ("returning a value from a constructor");
8809 return NULL_TREE;
8812 const tree saved_retval = retval;
8814 if (processing_template_decl)
8816 current_function_returns_value = 1;
8818 if (check_for_bare_parameter_packs (retval))
8819 return error_mark_node;
8821 if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
8822 || (retval != NULL_TREE
8823 && type_dependent_expression_p (retval)))
8824 return retval;
8827 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8829 /* Deduce auto return type from a return statement. */
8830 if (current_function_auto_return_pattern)
8832 tree auto_node;
8833 tree type;
8835 if (!retval && !is_auto (current_function_auto_return_pattern))
8837 /* Give a helpful error message. */
8838 error ("return-statement with no value, in function returning %qT",
8839 current_function_auto_return_pattern);
8840 inform (input_location, "only plain %<auto%> return type can be "
8841 "deduced to %<void%>");
8842 type = error_mark_node;
8844 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8846 error ("returning initializer list");
8847 type = error_mark_node;
8849 else
8851 if (!retval)
8852 retval = void_node;
8853 auto_node = type_uses_auto (current_function_auto_return_pattern);
8854 type = do_auto_deduction (current_function_auto_return_pattern,
8855 retval, auto_node);
8858 if (type == error_mark_node)
8859 /* Leave it. */;
8860 else if (functype == current_function_auto_return_pattern)
8861 apply_deduced_return_type (current_function_decl, type);
8862 else if (!same_type_p (type, functype))
8864 if (LAMBDA_FUNCTION_P (current_function_decl))
8865 error ("inconsistent types %qT and %qT deduced for "
8866 "lambda return type", functype, type);
8867 else
8868 error ("inconsistent deduction for auto return type: "
8869 "%qT and then %qT", functype, type);
8871 functype = type;
8874 result = DECL_RESULT (current_function_decl);
8875 valtype = TREE_TYPE (result);
8876 gcc_assert (valtype != NULL_TREE);
8877 fn_returns_value_p = !VOID_TYPE_P (valtype);
8879 /* Check for a return statement with no return value in a function
8880 that's supposed to return a value. */
8881 if (!retval && fn_returns_value_p)
8883 if (functype != error_mark_node)
8884 permerror (input_location, "return-statement with no value, in "
8885 "function returning %qT", valtype);
8886 /* Remember that this function did return. */
8887 current_function_returns_value = 1;
8888 /* And signal caller that TREE_NO_WARNING should be set on the
8889 RETURN_EXPR to avoid control reaches end of non-void function
8890 warnings in tree-cfg.c. */
8891 *no_warning = true;
8893 /* Check for a return statement with a value in a function that
8894 isn't supposed to return a value. */
8895 else if (retval && !fn_returns_value_p)
8897 if (VOID_TYPE_P (TREE_TYPE (retval)))
8898 /* You can return a `void' value from a function of `void'
8899 type. In that case, we have to evaluate the expression for
8900 its side-effects. */
8901 finish_expr_stmt (retval);
8902 else
8903 permerror (input_location, "return-statement with a value, in function "
8904 "returning 'void'");
8905 current_function_returns_null = 1;
8907 /* There's really no value to return, after all. */
8908 return NULL_TREE;
8910 else if (!retval)
8911 /* Remember that this function can sometimes return without a
8912 value. */
8913 current_function_returns_null = 1;
8914 else
8915 /* Remember that this function did return a value. */
8916 current_function_returns_value = 1;
8918 /* Check for erroneous operands -- but after giving ourselves a
8919 chance to provide an error about returning a value from a void
8920 function. */
8921 if (error_operand_p (retval))
8923 current_function_return_value = error_mark_node;
8924 return error_mark_node;
8927 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8928 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8929 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8930 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8931 && ! flag_check_new
8932 && retval && null_ptr_cst_p (retval))
8933 warning (0, "%<operator new%> must not return NULL unless it is "
8934 "declared %<throw()%> (or -fcheck-new is in effect)");
8936 /* Effective C++ rule 15. See also start_function. */
8937 if (warn_ecpp
8938 && DECL_NAME (current_function_decl) == cp_assignment_operator_id (NOP_EXPR))
8940 bool warn = true;
8942 /* The function return type must be a reference to the current
8943 class. */
8944 if (TREE_CODE (valtype) == REFERENCE_TYPE
8945 && same_type_ignoring_top_level_qualifiers_p
8946 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8948 /* Returning '*this' is obviously OK. */
8949 if (retval == current_class_ref)
8950 warn = false;
8951 /* If we are calling a function whose return type is the same of
8952 the current class reference, it is ok. */
8953 else if (INDIRECT_REF_P (retval)
8954 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8955 warn = false;
8958 if (warn)
8959 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8962 if (processing_template_decl)
8964 /* We should not have changed the return value. */
8965 gcc_assert (retval == saved_retval);
8966 return retval;
8969 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8971 [...] For a function with a class return type, if the expression
8972 in the return statement is the name of a local object, and the cv-
8973 unqualified type of the local object is the same as the function
8974 return type, an implementation is permitted to omit creating the tem-
8975 porary object to hold the function return value [...]
8977 So, if this is a value-returning function that always returns the same
8978 local variable, remember it.
8980 It might be nice to be more flexible, and choose the first suitable
8981 variable even if the function sometimes returns something else, but
8982 then we run the risk of clobbering the variable we chose if the other
8983 returned expression uses the chosen variable somehow. And people expect
8984 this restriction, anyway. (jason 2000-11-19)
8986 See finish_function and finalize_nrv for the rest of this optimization. */
8988 named_return_value_okay_p =
8989 (retval != NULL_TREE
8990 /* Must be a local, automatic variable. */
8991 && VAR_P (retval)
8992 && DECL_CONTEXT (retval) == current_function_decl
8993 && ! TREE_STATIC (retval)
8994 /* And not a lambda or anonymous union proxy. */
8995 && !DECL_HAS_VALUE_EXPR_P (retval)
8996 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8997 /* The cv-unqualified type of the returned value must be the
8998 same as the cv-unqualified return type of the
8999 function. */
9000 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9001 (TYPE_MAIN_VARIANT (functype)))
9002 /* And the returned value must be non-volatile. */
9003 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9005 if (fn_returns_value_p && flag_elide_constructors)
9007 if (named_return_value_okay_p
9008 && (current_function_return_value == NULL_TREE
9009 || current_function_return_value == retval))
9010 current_function_return_value = retval;
9011 else
9012 current_function_return_value = error_mark_node;
9015 /* We don't need to do any conversions when there's nothing being
9016 returned. */
9017 if (!retval)
9018 return NULL_TREE;
9020 /* Do any required conversions. */
9021 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9022 /* No conversions are required. */
9024 else
9026 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9028 /* The functype's return type will have been set to void, if it
9029 was an incomplete type. Just treat this as 'return;' */
9030 if (VOID_TYPE_P (functype))
9031 return error_mark_node;
9033 /* If we had an id-expression obfuscated by force_paren_expr, we need
9034 to undo it so we can try to treat it as an rvalue below. */
9035 retval = maybe_undo_parenthesized_ref (retval);
9037 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9038 treated as an rvalue for the purposes of overload resolution to
9039 favor move constructors over copy constructors.
9041 Note that these conditions are similar to, but not as strict as,
9042 the conditions for the named return value optimization. */
9043 if ((cxx_dialect != cxx98)
9044 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9045 || TREE_CODE (retval) == PARM_DECL)
9046 && DECL_CONTEXT (retval) == current_function_decl
9047 && !TREE_STATIC (retval)
9048 /* This is only interesting for class type. */
9049 && CLASS_TYPE_P (functype))
9050 flags = flags | LOOKUP_PREFER_RVALUE;
9052 /* First convert the value to the function's return type, then
9053 to the type of return value's location to handle the
9054 case that functype is smaller than the valtype. */
9055 retval = convert_for_initialization
9056 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9057 tf_warning_or_error);
9058 retval = convert (valtype, retval);
9060 /* If the conversion failed, treat this just like `return;'. */
9061 if (retval == error_mark_node)
9062 return retval;
9063 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9064 else if (! cfun->returns_struct
9065 && TREE_CODE (retval) == TARGET_EXPR
9066 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9067 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9068 TREE_OPERAND (retval, 0));
9069 else if (maybe_warn_about_returning_address_of_local (retval))
9070 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9071 build_zero_cst (TREE_TYPE (retval)));
9074 /* Actually copy the value returned into the appropriate location. */
9075 if (retval && retval != result)
9076 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9078 return retval;
9082 /* Returns nonzero if the pointer-type FROM can be converted to the
9083 pointer-type TO via a qualification conversion. If CONSTP is -1,
9084 then we return nonzero if the pointers are similar, and the
9085 cv-qualification signature of FROM is a proper subset of that of TO.
9087 If CONSTP is positive, then all outer pointers have been
9088 const-qualified. */
9090 static int
9091 comp_ptr_ttypes_real (tree to, tree from, int constp)
9093 bool to_more_cv_qualified = false;
9094 bool is_opaque_pointer = false;
9096 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9098 if (TREE_CODE (to) != TREE_CODE (from))
9099 return 0;
9101 if (TREE_CODE (from) == OFFSET_TYPE
9102 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9103 TYPE_OFFSET_BASETYPE (to)))
9104 return 0;
9106 /* Const and volatile mean something different for function types,
9107 so the usual checks are not appropriate. */
9108 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9110 if (!at_least_as_qualified_p (to, from))
9111 return 0;
9113 if (!at_least_as_qualified_p (from, to))
9115 if (constp == 0)
9116 return 0;
9117 to_more_cv_qualified = true;
9120 if (constp > 0)
9121 constp &= TYPE_READONLY (to);
9124 if (VECTOR_TYPE_P (to))
9125 is_opaque_pointer = vector_targets_convertible_p (to, from);
9127 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9128 return ((constp >= 0 || to_more_cv_qualified)
9129 && (is_opaque_pointer
9130 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9134 /* When comparing, say, char ** to char const **, this function takes
9135 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9136 types to this function. */
9139 comp_ptr_ttypes (tree to, tree from)
9141 return comp_ptr_ttypes_real (to, from, 1);
9144 /* Returns true iff FNTYPE is a non-class type that involves
9145 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9146 if a parameter type is ill-formed. */
9148 bool
9149 error_type_p (const_tree type)
9151 tree t;
9153 switch (TREE_CODE (type))
9155 case ERROR_MARK:
9156 return true;
9158 case POINTER_TYPE:
9159 case REFERENCE_TYPE:
9160 case OFFSET_TYPE:
9161 return error_type_p (TREE_TYPE (type));
9163 case FUNCTION_TYPE:
9164 case METHOD_TYPE:
9165 if (error_type_p (TREE_TYPE (type)))
9166 return true;
9167 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9168 if (error_type_p (TREE_VALUE (t)))
9169 return true;
9170 return false;
9172 case RECORD_TYPE:
9173 if (TYPE_PTRMEMFUNC_P (type))
9174 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9175 return false;
9177 default:
9178 return false;
9182 /* Returns true if to and from are (possibly multi-level) pointers to the same
9183 type or inheritance-related types, regardless of cv-quals. */
9185 bool
9186 ptr_reasonably_similar (const_tree to, const_tree from)
9188 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9190 /* Any target type is similar enough to void. */
9191 if (VOID_TYPE_P (to))
9192 return !error_type_p (from);
9193 if (VOID_TYPE_P (from))
9194 return !error_type_p (to);
9196 if (TREE_CODE (to) != TREE_CODE (from))
9197 return false;
9199 if (TREE_CODE (from) == OFFSET_TYPE
9200 && comptypes (TYPE_OFFSET_BASETYPE (to),
9201 TYPE_OFFSET_BASETYPE (from),
9202 COMPARE_BASE | COMPARE_DERIVED))
9203 continue;
9205 if (VECTOR_TYPE_P (to)
9206 && vector_types_convertible_p (to, from, false))
9207 return true;
9209 if (TREE_CODE (to) == INTEGER_TYPE
9210 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9211 return true;
9213 if (TREE_CODE (to) == FUNCTION_TYPE)
9214 return !error_type_p (to) && !error_type_p (from);
9216 if (!TYPE_PTR_P (to))
9218 /* When either type is incomplete avoid DERIVED_FROM_P,
9219 which may call complete_type (c++/57942). */
9220 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9221 return comptypes
9222 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9223 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9228 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9229 pointer-to-member types) are the same, ignoring cv-qualification at
9230 all levels. */
9232 bool
9233 comp_ptr_ttypes_const (tree to, tree from)
9235 bool is_opaque_pointer = false;
9237 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9239 if (TREE_CODE (to) != TREE_CODE (from))
9240 return false;
9242 if (TREE_CODE (from) == OFFSET_TYPE
9243 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9244 TYPE_OFFSET_BASETYPE (to)))
9245 continue;
9247 if (VECTOR_TYPE_P (to))
9248 is_opaque_pointer = vector_targets_convertible_p (to, from);
9250 if (!TYPE_PTR_P (to))
9251 return (is_opaque_pointer
9252 || same_type_ignoring_top_level_qualifiers_p (to, from));
9256 /* Returns the type qualifiers for this type, including the qualifiers on the
9257 elements for an array type. */
9260 cp_type_quals (const_tree type)
9262 int quals;
9263 /* This CONST_CAST is okay because strip_array_types returns its
9264 argument unmodified and we assign it to a const_tree. */
9265 type = strip_array_types (CONST_CAST_TREE (type));
9266 if (type == error_mark_node
9267 /* Quals on a FUNCTION_TYPE are memfn quals. */
9268 || TREE_CODE (type) == FUNCTION_TYPE)
9269 return TYPE_UNQUALIFIED;
9270 quals = TYPE_QUALS (type);
9271 /* METHOD and REFERENCE_TYPEs should never have quals. */
9272 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9273 && TREE_CODE (type) != REFERENCE_TYPE)
9274 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9275 == TYPE_UNQUALIFIED));
9276 return quals;
9279 /* Returns the function-ref-qualifier for TYPE */
9281 cp_ref_qualifier
9282 type_memfn_rqual (const_tree type)
9284 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9285 || TREE_CODE (type) == METHOD_TYPE);
9287 if (!FUNCTION_REF_QUALIFIED (type))
9288 return REF_QUAL_NONE;
9289 else if (FUNCTION_RVALUE_QUALIFIED (type))
9290 return REF_QUAL_RVALUE;
9291 else
9292 return REF_QUAL_LVALUE;
9295 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9296 METHOD_TYPE. */
9299 type_memfn_quals (const_tree type)
9301 if (TREE_CODE (type) == FUNCTION_TYPE)
9302 return TYPE_QUALS (type);
9303 else if (TREE_CODE (type) == METHOD_TYPE)
9304 return cp_type_quals (class_of_this_parm (type));
9305 else
9306 gcc_unreachable ();
9309 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9310 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9312 tree
9313 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9315 /* Could handle METHOD_TYPE here if necessary. */
9316 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9317 if (TYPE_QUALS (type) == memfn_quals
9318 && type_memfn_rqual (type) == rqual)
9319 return type;
9321 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9322 complex. */
9323 tree result = build_qualified_type (type, memfn_quals);
9324 return build_ref_qualified_type (result, rqual);
9327 /* Returns nonzero if TYPE is const or volatile. */
9329 bool
9330 cv_qualified_p (const_tree type)
9332 int quals = cp_type_quals (type);
9333 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9336 /* Returns nonzero if the TYPE contains a mutable member. */
9338 bool
9339 cp_has_mutable_p (const_tree type)
9341 /* This CONST_CAST is okay because strip_array_types returns its
9342 argument unmodified and we assign it to a const_tree. */
9343 type = strip_array_types (CONST_CAST_TREE(type));
9345 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9348 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9349 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9350 approximation. In particular, consider:
9352 int f();
9353 struct S { int i; };
9354 const S s = { f(); }
9356 Here, we will make "s" as TREE_READONLY (because it is declared
9357 "const") -- only to reverse ourselves upon seeing that the
9358 initializer is non-constant. */
9360 void
9361 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9363 tree type = TREE_TYPE (decl);
9365 if (type == error_mark_node)
9366 return;
9368 if (TREE_CODE (decl) == TYPE_DECL)
9369 return;
9371 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9372 && type_quals != TYPE_UNQUALIFIED));
9374 /* Avoid setting TREE_READONLY incorrectly. */
9375 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9376 constructor can produce constant init, so rely on cp_finish_decl to
9377 clear TREE_READONLY if the variable has non-constant init. */
9379 /* If the type has (or might have) a mutable component, that component
9380 might be modified. */
9381 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9382 type_quals &= ~TYPE_QUAL_CONST;
9384 c_apply_type_quals_to_decl (type_quals, decl);
9387 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9388 exemplar types such that casting T1 to T2 is casting away constness
9389 if and only if there is no implicit conversion from T1 to T2. */
9391 static void
9392 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9394 int quals1;
9395 int quals2;
9397 /* [expr.const.cast]
9399 For multi-level pointer to members and multi-level mixed pointers
9400 and pointers to members (conv.qual), the "member" aspect of a
9401 pointer to member level is ignored when determining if a const
9402 cv-qualifier has been cast away. */
9403 /* [expr.const.cast]
9405 For two pointer types:
9407 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9408 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9409 K is min(N,M)
9411 casting from X1 to X2 casts away constness if, for a non-pointer
9412 type T there does not exist an implicit conversion (clause
9413 _conv_) from:
9415 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9419 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9420 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9421 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9423 *t1 = cp_build_qualified_type (void_type_node,
9424 cp_type_quals (*t1));
9425 *t2 = cp_build_qualified_type (void_type_node,
9426 cp_type_quals (*t2));
9427 return;
9430 quals1 = cp_type_quals (*t1);
9431 quals2 = cp_type_quals (*t2);
9433 if (TYPE_PTRDATAMEM_P (*t1))
9434 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9435 else
9436 *t1 = TREE_TYPE (*t1);
9437 if (TYPE_PTRDATAMEM_P (*t2))
9438 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9439 else
9440 *t2 = TREE_TYPE (*t2);
9442 casts_away_constness_r (t1, t2, complain);
9443 *t1 = build_pointer_type (*t1);
9444 *t2 = build_pointer_type (*t2);
9445 *t1 = cp_build_qualified_type (*t1, quals1);
9446 *t2 = cp_build_qualified_type (*t2, quals2);
9449 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9450 constness.
9452 ??? This function returns non-zero if casting away qualifiers not
9453 just const. We would like to return to the caller exactly which
9454 qualifiers are casted away to give more accurate diagnostics.
9457 static bool
9458 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9460 if (TREE_CODE (t2) == REFERENCE_TYPE)
9462 /* [expr.const.cast]
9464 Casting from an lvalue of type T1 to an lvalue of type T2
9465 using a reference cast casts away constness if a cast from an
9466 rvalue of type "pointer to T1" to the type "pointer to T2"
9467 casts away constness. */
9468 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9469 return casts_away_constness (build_pointer_type (t1),
9470 build_pointer_type (TREE_TYPE (t2)),
9471 complain);
9474 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9475 /* [expr.const.cast]
9477 Casting from an rvalue of type "pointer to data member of X
9478 of type T1" to the type "pointer to data member of Y of type
9479 T2" casts away constness if a cast from an rvalue of type
9480 "pointer to T1" to the type "pointer to T2" casts away
9481 constness. */
9482 return casts_away_constness
9483 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9484 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9485 complain);
9487 /* Casting away constness is only something that makes sense for
9488 pointer or reference types. */
9489 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9490 return false;
9492 /* Top-level qualifiers don't matter. */
9493 t1 = TYPE_MAIN_VARIANT (t1);
9494 t2 = TYPE_MAIN_VARIANT (t2);
9495 casts_away_constness_r (&t1, &t2, complain);
9496 if (!can_convert (t2, t1, complain))
9497 return true;
9499 return false;
9502 /* If T is a REFERENCE_TYPE return the type to which T refers.
9503 Otherwise, return T itself. */
9505 tree
9506 non_reference (tree t)
9508 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9509 t = TREE_TYPE (t);
9510 return t;
9514 /* Return nonzero if REF is an lvalue valid for this language;
9515 otherwise, print an error message and return zero. USE says
9516 how the lvalue is being used and so selects the error message. */
9519 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9521 cp_lvalue_kind kind = lvalue_kind (ref);
9523 if (kind == clk_none)
9525 if (complain & tf_error)
9526 lvalue_error (input_location, use);
9527 return 0;
9529 else if (kind & (clk_rvalueref|clk_class))
9531 if (!(complain & tf_error))
9532 return 0;
9533 if (kind & clk_class)
9534 /* Make this a permerror because we used to accept it. */
9535 permerror (input_location, "using temporary as lvalue");
9536 else
9537 error ("using xvalue (rvalue reference) as lvalue");
9539 return 1;
9542 /* Return true if a user-defined literal operator is a raw operator. */
9544 bool
9545 check_raw_literal_operator (const_tree decl)
9547 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9548 tree argtype;
9549 int arity;
9550 bool maybe_raw_p = false;
9552 /* Count the number and type of arguments and check for ellipsis. */
9553 for (argtype = argtypes, arity = 0;
9554 argtype && argtype != void_list_node;
9555 ++arity, argtype = TREE_CHAIN (argtype))
9557 tree t = TREE_VALUE (argtype);
9559 if (same_type_p (t, const_string_type_node))
9560 maybe_raw_p = true;
9562 if (!argtype)
9563 return false; /* Found ellipsis. */
9565 if (!maybe_raw_p || arity != 1)
9566 return false;
9568 return true;
9572 /* Return true if a user-defined literal operator has one of the allowed
9573 argument types. */
9575 bool
9576 check_literal_operator_args (const_tree decl,
9577 bool *long_long_unsigned_p, bool *long_double_p)
9579 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9581 *long_long_unsigned_p = false;
9582 *long_double_p = false;
9583 if (processing_template_decl || processing_specialization)
9584 return argtypes == void_list_node;
9585 else
9587 tree argtype;
9588 int arity;
9589 int max_arity = 2;
9591 /* Count the number and type of arguments and check for ellipsis. */
9592 for (argtype = argtypes, arity = 0;
9593 argtype && argtype != void_list_node;
9594 argtype = TREE_CHAIN (argtype))
9596 tree t = TREE_VALUE (argtype);
9597 ++arity;
9599 if (TYPE_PTR_P (t))
9601 bool maybe_raw_p = false;
9602 t = TREE_TYPE (t);
9603 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9604 return false;
9605 t = TYPE_MAIN_VARIANT (t);
9606 if ((maybe_raw_p = same_type_p (t, char_type_node))
9607 || same_type_p (t, wchar_type_node)
9608 || same_type_p (t, char16_type_node)
9609 || same_type_p (t, char32_type_node))
9611 argtype = TREE_CHAIN (argtype);
9612 if (!argtype)
9613 return false;
9614 t = TREE_VALUE (argtype);
9615 if (maybe_raw_p && argtype == void_list_node)
9616 return true;
9617 else if (same_type_p (t, size_type_node))
9619 ++arity;
9620 continue;
9622 else
9623 return false;
9626 else if (same_type_p (t, long_long_unsigned_type_node))
9628 max_arity = 1;
9629 *long_long_unsigned_p = true;
9631 else if (same_type_p (t, long_double_type_node))
9633 max_arity = 1;
9634 *long_double_p = true;
9636 else if (same_type_p (t, char_type_node))
9637 max_arity = 1;
9638 else if (same_type_p (t, wchar_type_node))
9639 max_arity = 1;
9640 else if (same_type_p (t, char16_type_node))
9641 max_arity = 1;
9642 else if (same_type_p (t, char32_type_node))
9643 max_arity = 1;
9644 else
9645 return false;
9647 if (!argtype)
9648 return false; /* Found ellipsis. */
9650 if (arity != max_arity)
9651 return false;
9653 return true;
9657 /* Always returns false since unlike C90, C++ has no concept of implicit
9658 function declarations. */
9660 bool
9661 c_decl_implicit (const_tree)
9663 return false;