PR libstdc++/87308 adjust regex used in std::any pretty printer
[official-gcc.git] / gcc / cp / typeck.c
blob81cb4057a5b424882bdc82392ab00a15fccabaa8
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "target.h"
31 #include "cp-tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "intl.h"
35 #include "convert.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
38 #include "params.h"
39 #include "gcc-rich-location.h"
40 #include "stringpool.h"
41 #include "attribs.h"
42 #include "asan.h"
44 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
45 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
46 static tree pfn_from_ptrmemfunc (tree);
47 static tree delta_from_ptrmemfunc (tree);
48 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 tsubst_flags_t, int);
50 static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
51 tsubst_flags_t);
52 static tree rationalize_conditional_expr (enum tree_code, tree,
53 tsubst_flags_t);
54 static int comp_ptr_ttypes_real (tree, tree, int);
55 static bool comp_except_types (tree, tree, bool);
56 static bool comp_array_types (const_tree, const_tree, bool);
57 static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
58 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 static bool maybe_warn_about_returning_address_of_local (tree);
62 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
63 static void error_args_num (location_t, tree, bool);
64 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
65 tsubst_flags_t);
66 static bool is_std_move_p (tree);
67 static bool is_std_forward_p (tree);
69 /* Do `exp = require_complete_type (exp);' to make sure exp
70 does not have an incomplete type. (That includes void types.)
71 Returns error_mark_node if the VALUE does not have
72 complete type when this function returns. */
74 tree
75 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
77 tree type;
79 if (processing_template_decl || value == error_mark_node)
80 return value;
82 if (TREE_CODE (value) == OVERLOAD)
83 type = unknown_type_node;
84 else
85 type = TREE_TYPE (value);
87 if (type == error_mark_node)
88 return error_mark_node;
90 /* First, detect a valid value with a complete type. */
91 if (COMPLETE_TYPE_P (type))
92 return value;
94 if (complete_type_or_maybe_complain (type, value, complain))
95 return value;
96 else
97 return error_mark_node;
100 tree
101 require_complete_type (tree value)
103 return require_complete_type_sfinae (value, tf_warning_or_error);
106 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
107 a template instantiation, do the instantiation. Returns TYPE,
108 whether or not it could be completed, unless something goes
109 horribly wrong, in which case the error_mark_node is returned. */
111 tree
112 complete_type (tree type)
114 if (type == NULL_TREE)
115 /* Rather than crash, we return something sure to cause an error
116 at some point. */
117 return error_mark_node;
119 if (type == error_mark_node || COMPLETE_TYPE_P (type))
121 else if (TREE_CODE (type) == ARRAY_TYPE)
123 tree t = complete_type (TREE_TYPE (type));
124 unsigned int needs_constructing, has_nontrivial_dtor;
125 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
126 layout_type (type);
127 needs_constructing
128 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
129 has_nontrivial_dtor
130 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
131 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
133 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
134 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
137 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 instantiate_class_template (TYPE_MAIN_VARIANT (type));
140 return type;
143 /* Like complete_type, but issue an error if the TYPE cannot be completed.
144 VALUE is used for informative diagnostics.
145 Returns NULL_TREE if the type cannot be made complete. */
147 tree
148 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 type = complete_type (type);
151 if (type == error_mark_node)
152 /* We already issued an error. */
153 return NULL_TREE;
154 else if (!COMPLETE_TYPE_P (type))
156 if (complain & tf_error)
157 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
158 return NULL_TREE;
160 else
161 return type;
164 tree
165 complete_type_or_else (tree type, tree value)
167 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
171 /* Return the common type of two parameter lists.
172 We assume that comptypes has already been done and returned 1;
173 if that isn't so, this may crash.
175 As an optimization, free the space we allocate if the parameter
176 lists are already common. */
178 static tree
179 commonparms (tree p1, tree p2)
181 tree oldargs = p1, newargs, n;
182 int i, len;
183 int any_change = 0;
185 len = list_length (p1);
186 newargs = tree_last (p1);
188 if (newargs == void_list_node)
189 i = 1;
190 else
192 i = 0;
193 newargs = 0;
196 for (; i < len; i++)
197 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
199 n = newargs;
201 for (i = 0; p1;
202 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
204 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
206 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
207 any_change = 1;
209 else if (! TREE_PURPOSE (p1))
211 if (TREE_PURPOSE (p2))
213 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
214 any_change = 1;
217 else
219 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
220 any_change = 1;
221 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
223 if (TREE_VALUE (p1) != TREE_VALUE (p2))
225 any_change = 1;
226 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
228 else
229 TREE_VALUE (n) = TREE_VALUE (p1);
231 if (! any_change)
232 return oldargs;
234 return newargs;
237 /* Given a type, perhaps copied for a typedef,
238 find the "original" version of it. */
239 static tree
240 original_type (tree t)
242 int quals = cp_type_quals (t);
243 while (t != error_mark_node
244 && TYPE_NAME (t) != NULL_TREE)
246 tree x = TYPE_NAME (t);
247 if (TREE_CODE (x) != TYPE_DECL)
248 break;
249 x = DECL_ORIGINAL_TYPE (x);
250 if (x == NULL_TREE)
251 break;
252 t = x;
254 return cp_build_qualified_type (t, quals);
257 /* Return the common type for two arithmetic types T1 and T2 under the
258 usual arithmetic conversions. The default conversions have already
259 been applied, and enumerated types converted to their compatible
260 integer types. */
262 static tree
263 cp_common_type (tree t1, tree t2)
265 enum tree_code code1 = TREE_CODE (t1);
266 enum tree_code code2 = TREE_CODE (t2);
267 tree attributes;
268 int i;
271 /* In what follows, we slightly generalize the rules given in [expr] so
272 as to deal with `long long' and `complex'. First, merge the
273 attributes. */
274 attributes = (*targetm.merge_type_attributes) (t1, t2);
276 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
278 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
279 return build_type_attribute_variant (t1, attributes);
280 else
281 return NULL_TREE;
284 /* FIXME: Attributes. */
285 gcc_assert (ARITHMETIC_TYPE_P (t1)
286 || VECTOR_TYPE_P (t1)
287 || UNSCOPED_ENUM_P (t1));
288 gcc_assert (ARITHMETIC_TYPE_P (t2)
289 || VECTOR_TYPE_P (t2)
290 || UNSCOPED_ENUM_P (t2));
292 /* If one type is complex, form the common type of the non-complex
293 components, then make that complex. Use T1 or T2 if it is the
294 required type. */
295 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
297 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
298 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
299 tree subtype
300 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
302 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
303 return build_type_attribute_variant (t1, attributes);
304 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
305 return build_type_attribute_variant (t2, attributes);
306 else
307 return build_type_attribute_variant (build_complex_type (subtype),
308 attributes);
311 if (code1 == VECTOR_TYPE)
313 /* When we get here we should have two vectors of the same size.
314 Just prefer the unsigned one if present. */
315 if (TYPE_UNSIGNED (t1))
316 return build_type_attribute_variant (t1, attributes);
317 else
318 return build_type_attribute_variant (t2, attributes);
321 /* If only one is real, use it as the result. */
322 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
323 return build_type_attribute_variant (t1, attributes);
324 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
325 return build_type_attribute_variant (t2, attributes);
327 /* Both real or both integers; use the one with greater precision. */
328 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
329 return build_type_attribute_variant (t1, attributes);
330 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
331 return build_type_attribute_variant (t2, attributes);
333 /* The types are the same; no need to do anything fancy. */
334 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
335 return build_type_attribute_variant (t1, attributes);
337 if (code1 != REAL_TYPE)
339 /* If one is unsigned long long, then convert the other to unsigned
340 long long. */
341 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
342 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
343 return build_type_attribute_variant (long_long_unsigned_type_node,
344 attributes);
345 /* If one is a long long, and the other is an unsigned long, and
346 long long can represent all the values of an unsigned long, then
347 convert to a long long. Otherwise, convert to an unsigned long
348 long. Otherwise, if either operand is long long, convert the
349 other to long long.
351 Since we're here, we know the TYPE_PRECISION is the same;
352 therefore converting to long long cannot represent all the values
353 of an unsigned long, so we choose unsigned long long in that
354 case. */
355 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
356 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
358 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
359 ? long_long_unsigned_type_node
360 : long_long_integer_type_node);
361 return build_type_attribute_variant (t, attributes);
364 /* Go through the same procedure, but for longs. */
365 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
366 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
367 return build_type_attribute_variant (long_unsigned_type_node,
368 attributes);
369 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
372 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
373 ? long_unsigned_type_node : long_integer_type_node);
374 return build_type_attribute_variant (t, attributes);
377 /* For __intN types, either the type is __int128 (and is lower
378 priority than the types checked above, but higher than other
379 128-bit types) or it's known to not be the same size as other
380 types (enforced in toplev.c). Prefer the unsigned type. */
381 for (i = 0; i < NUM_INT_N_ENTS; i ++)
383 if (int_n_enabled_p [i]
384 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
386 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
387 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
389 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
390 ? int_n_trees[i].unsigned_type
391 : int_n_trees[i].signed_type);
392 return build_type_attribute_variant (t, attributes);
396 /* Otherwise prefer the unsigned one. */
397 if (TYPE_UNSIGNED (t1))
398 return build_type_attribute_variant (t1, attributes);
399 else
400 return build_type_attribute_variant (t2, attributes);
402 else
404 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
405 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
406 return build_type_attribute_variant (long_double_type_node,
407 attributes);
408 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
409 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
410 return build_type_attribute_variant (double_type_node,
411 attributes);
412 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
413 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
414 return build_type_attribute_variant (float_type_node,
415 attributes);
417 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
418 the standard C++ floating-point types. Logic earlier in this
419 function has already eliminated the possibility that
420 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
421 compelling reason to choose one or the other. */
422 return build_type_attribute_variant (t1, attributes);
426 /* T1 and T2 are arithmetic or enumeration types. Return the type
427 that will result from the "usual arithmetic conversions" on T1 and
428 T2 as described in [expr]. */
430 tree
431 type_after_usual_arithmetic_conversions (tree t1, tree t2)
433 gcc_assert (ARITHMETIC_TYPE_P (t1)
434 || VECTOR_TYPE_P (t1)
435 || UNSCOPED_ENUM_P (t1));
436 gcc_assert (ARITHMETIC_TYPE_P (t2)
437 || VECTOR_TYPE_P (t2)
438 || UNSCOPED_ENUM_P (t2));
440 /* Perform the integral promotions. We do not promote real types here. */
441 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
442 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
444 t1 = type_promotes_to (t1);
445 t2 = type_promotes_to (t2);
448 return cp_common_type (t1, t2);
451 static void
452 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
453 composite_pointer_operation operation)
455 switch (operation)
457 case CPO_COMPARISON:
458 emit_diagnostic (kind, input_location, 0,
459 "comparison between "
460 "distinct pointer types %qT and %qT lacks a cast",
461 t1, t2);
462 break;
463 case CPO_CONVERSION:
464 emit_diagnostic (kind, input_location, 0,
465 "conversion between "
466 "distinct pointer types %qT and %qT lacks a cast",
467 t1, t2);
468 break;
469 case CPO_CONDITIONAL_EXPR:
470 emit_diagnostic (kind, input_location, 0,
471 "conditional expression between "
472 "distinct pointer types %qT and %qT lacks a cast",
473 t1, t2);
474 break;
475 default:
476 gcc_unreachable ();
480 /* Subroutine of composite_pointer_type to implement the recursive
481 case. See that function for documentation of the parameters. */
483 static tree
484 composite_pointer_type_r (tree t1, tree t2,
485 composite_pointer_operation operation,
486 tsubst_flags_t complain)
488 tree pointee1;
489 tree pointee2;
490 tree result_type;
491 tree attributes;
493 /* Determine the types pointed to by T1 and T2. */
494 if (TYPE_PTR_P (t1))
496 pointee1 = TREE_TYPE (t1);
497 pointee2 = TREE_TYPE (t2);
499 else
501 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
502 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
505 /* [expr.rel]
507 Otherwise, the composite pointer type is a pointer type
508 similar (_conv.qual_) to the type of one of the operands,
509 with a cv-qualification signature (_conv.qual_) that is the
510 union of the cv-qualification signatures of the operand
511 types. */
512 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
513 result_type = pointee1;
514 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
515 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
517 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
518 complain);
519 if (result_type == error_mark_node)
520 return error_mark_node;
522 else
524 if (complain & tf_error)
525 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
526 else
527 return error_mark_node;
528 result_type = void_type_node;
530 result_type = cp_build_qualified_type (result_type,
531 (cp_type_quals (pointee1)
532 | cp_type_quals (pointee2)));
533 /* If the original types were pointers to members, so is the
534 result. */
535 if (TYPE_PTRMEM_P (t1))
537 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
538 TYPE_PTRMEM_CLASS_TYPE (t2)))
540 if (complain & tf_error)
541 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
542 else
543 return error_mark_node;
545 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
546 result_type);
548 else
549 result_type = build_pointer_type (result_type);
551 /* Merge the attributes. */
552 attributes = (*targetm.merge_type_attributes) (t1, t2);
553 return build_type_attribute_variant (result_type, attributes);
556 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
557 ARG1 and ARG2 are the values with those types. The OPERATION is to
558 describe the operation between the pointer types,
559 in case an error occurs.
561 This routine also implements the computation of a common type for
562 pointers-to-members as per [expr.eq]. */
564 tree
565 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
566 composite_pointer_operation operation,
567 tsubst_flags_t complain)
569 tree class1;
570 tree class2;
572 /* [expr.rel]
574 If one operand is a null pointer constant, the composite pointer
575 type is the type of the other operand. */
576 if (null_ptr_cst_p (arg1))
577 return t2;
578 if (null_ptr_cst_p (arg2))
579 return t1;
581 /* We have:
583 [expr.rel]
585 If one of the operands has type "pointer to cv1 void*", then
586 the other has type "pointer to cv2T", and the composite pointer
587 type is "pointer to cv12 void", where cv12 is the union of cv1
588 and cv2.
590 If either type is a pointer to void, make sure it is T1. */
591 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
592 std::swap (t1, t2);
594 /* Now, if T1 is a pointer to void, merge the qualifiers. */
595 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
597 tree attributes;
598 tree result_type;
600 if (TYPE_PTRFN_P (t2))
602 if (complain & tf_error)
604 switch (operation)
606 case CPO_COMPARISON:
607 pedwarn (input_location, OPT_Wpedantic,
608 "ISO C++ forbids comparison between pointer "
609 "of type %<void *%> and pointer-to-function");
610 break;
611 case CPO_CONVERSION:
612 pedwarn (input_location, OPT_Wpedantic,
613 "ISO C++ forbids conversion between pointer "
614 "of type %<void *%> and pointer-to-function");
615 break;
616 case CPO_CONDITIONAL_EXPR:
617 pedwarn (input_location, OPT_Wpedantic,
618 "ISO C++ forbids conditional expression between "
619 "pointer of type %<void *%> and "
620 "pointer-to-function");
621 break;
622 default:
623 gcc_unreachable ();
626 else
627 return error_mark_node;
629 result_type
630 = cp_build_qualified_type (void_type_node,
631 (cp_type_quals (TREE_TYPE (t1))
632 | cp_type_quals (TREE_TYPE (t2))));
633 result_type = build_pointer_type (result_type);
634 /* Merge the attributes. */
635 attributes = (*targetm.merge_type_attributes) (t1, t2);
636 return build_type_attribute_variant (result_type, attributes);
639 if (c_dialect_objc () && TYPE_PTR_P (t1)
640 && TYPE_PTR_P (t2))
642 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
643 return objc_common_type (t1, t2);
646 /* if T1 or T2 is "pointer to noexcept function" and the other type is
647 "pointer to function", where the function types are otherwise the same,
648 "pointer to function" */
649 if (fnptr_conv_p (t1, t2))
650 return t1;
651 if (fnptr_conv_p (t2, t1))
652 return t2;
654 /* [expr.eq] permits the application of a pointer conversion to
655 bring the pointers to a common type. */
656 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
657 && CLASS_TYPE_P (TREE_TYPE (t1))
658 && CLASS_TYPE_P (TREE_TYPE (t2))
659 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
660 TREE_TYPE (t2)))
662 class1 = TREE_TYPE (t1);
663 class2 = TREE_TYPE (t2);
665 if (DERIVED_FROM_P (class1, class2))
666 t2 = (build_pointer_type
667 (cp_build_qualified_type (class1, cp_type_quals (class2))));
668 else if (DERIVED_FROM_P (class2, class1))
669 t1 = (build_pointer_type
670 (cp_build_qualified_type (class2, cp_type_quals (class1))));
671 else
673 if (complain & tf_error)
674 composite_pointer_error (DK_ERROR, t1, t2, operation);
675 return error_mark_node;
678 /* [expr.eq] permits the application of a pointer-to-member
679 conversion to change the class type of one of the types. */
680 else if (TYPE_PTRMEM_P (t1)
681 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
682 TYPE_PTRMEM_CLASS_TYPE (t2)))
684 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
685 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
687 if (DERIVED_FROM_P (class1, class2))
688 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
689 else if (DERIVED_FROM_P (class2, class1))
690 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
691 else
693 if (complain & tf_error)
694 switch (operation)
696 case CPO_COMPARISON:
697 error ("comparison between distinct "
698 "pointer-to-member types %qT and %qT lacks a cast",
699 t1, t2);
700 break;
701 case CPO_CONVERSION:
702 error ("conversion between distinct "
703 "pointer-to-member types %qT and %qT lacks a cast",
704 t1, t2);
705 break;
706 case CPO_CONDITIONAL_EXPR:
707 error ("conditional expression between distinct "
708 "pointer-to-member types %qT and %qT lacks a cast",
709 t1, t2);
710 break;
711 default:
712 gcc_unreachable ();
714 return error_mark_node;
718 return composite_pointer_type_r (t1, t2, operation, complain);
721 /* Return the merged type of two types.
722 We assume that comptypes has already been done and returned 1;
723 if that isn't so, this may crash.
725 This just combines attributes and default arguments; any other
726 differences would cause the two types to compare unalike. */
728 tree
729 merge_types (tree t1, tree t2)
731 enum tree_code code1;
732 enum tree_code code2;
733 tree attributes;
735 /* Save time if the two types are the same. */
736 if (t1 == t2)
737 return t1;
738 if (original_type (t1) == original_type (t2))
739 return t1;
741 /* If one type is nonsense, use the other. */
742 if (t1 == error_mark_node)
743 return t2;
744 if (t2 == error_mark_node)
745 return t1;
747 /* Handle merging an auto redeclaration with a previous deduced
748 return type. */
749 if (is_auto (t1))
750 return t2;
752 /* Merge the attributes. */
753 attributes = (*targetm.merge_type_attributes) (t1, t2);
755 if (TYPE_PTRMEMFUNC_P (t1))
756 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
757 if (TYPE_PTRMEMFUNC_P (t2))
758 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
760 code1 = TREE_CODE (t1);
761 code2 = TREE_CODE (t2);
762 if (code1 != code2)
764 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
765 if (code1 == TYPENAME_TYPE)
767 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
768 code1 = TREE_CODE (t1);
770 else
772 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
773 code2 = TREE_CODE (t2);
777 switch (code1)
779 case POINTER_TYPE:
780 case REFERENCE_TYPE:
781 /* For two pointers, do this recursively on the target type. */
783 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
784 int quals = cp_type_quals (t1);
786 if (code1 == POINTER_TYPE)
788 t1 = build_pointer_type (target);
789 if (TREE_CODE (target) == METHOD_TYPE)
790 t1 = build_ptrmemfunc_type (t1);
792 else
793 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
794 t1 = build_type_attribute_variant (t1, attributes);
795 t1 = cp_build_qualified_type (t1, quals);
797 return t1;
800 case OFFSET_TYPE:
802 int quals;
803 tree pointee;
804 quals = cp_type_quals (t1);
805 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
806 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
807 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
808 pointee);
809 t1 = cp_build_qualified_type (t1, quals);
810 break;
813 case ARRAY_TYPE:
815 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
816 /* Save space: see if the result is identical to one of the args. */
817 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
818 return build_type_attribute_variant (t1, attributes);
819 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
820 return build_type_attribute_variant (t2, attributes);
821 /* Merge the element types, and have a size if either arg has one. */
822 t1 = build_cplus_array_type
823 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
824 break;
827 case FUNCTION_TYPE:
828 /* Function types: prefer the one that specified arg types.
829 If both do, merge the arg types. Also merge the return types. */
831 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
832 tree p1 = TYPE_ARG_TYPES (t1);
833 tree p2 = TYPE_ARG_TYPES (t2);
834 tree parms;
836 /* Save space: see if the result is identical to one of the args. */
837 if (valtype == TREE_TYPE (t1) && ! p2)
838 return cp_build_type_attribute_variant (t1, attributes);
839 if (valtype == TREE_TYPE (t2) && ! p1)
840 return cp_build_type_attribute_variant (t2, attributes);
842 /* Simple way if one arg fails to specify argument types. */
843 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
844 parms = p2;
845 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
846 parms = p1;
847 else
848 parms = commonparms (p1, p2);
850 cp_cv_quals quals = type_memfn_quals (t1);
851 cp_ref_qualifier rqual = type_memfn_rqual (t1);
852 gcc_assert (quals == type_memfn_quals (t2));
853 gcc_assert (rqual == type_memfn_rqual (t2));
855 tree rval = build_function_type (valtype, parms);
856 rval = apply_memfn_quals (rval, quals);
857 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
858 TYPE_RAISES_EXCEPTIONS (t2));
859 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
860 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
861 break;
864 case METHOD_TYPE:
866 /* Get this value the long way, since TYPE_METHOD_BASETYPE
867 is just the main variant of this. */
868 tree basetype = class_of_this_parm (t2);
869 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
870 TYPE_RAISES_EXCEPTIONS (t2));
871 cp_ref_qualifier rqual = type_memfn_rqual (t1);
872 tree t3;
873 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
875 /* If this was a member function type, get back to the
876 original type of type member function (i.e., without
877 the class instance variable up front. */
878 t1 = build_function_type (TREE_TYPE (t1),
879 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
880 t2 = build_function_type (TREE_TYPE (t2),
881 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
882 t3 = merge_types (t1, t2);
883 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
884 TYPE_ARG_TYPES (t3));
885 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
886 break;
889 case TYPENAME_TYPE:
890 /* There is no need to merge attributes into a TYPENAME_TYPE.
891 When the type is instantiated it will have whatever
892 attributes result from the instantiation. */
893 return t1;
895 default:;
896 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
897 return t1;
898 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
899 return t2;
900 break;
903 return cp_build_type_attribute_variant (t1, attributes);
906 /* Return the ARRAY_TYPE type without its domain. */
908 tree
909 strip_array_domain (tree type)
911 tree t2;
912 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
913 if (TYPE_DOMAIN (type) == NULL_TREE)
914 return type;
915 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
916 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
919 /* Wrapper around cp_common_type that is used by c-common.c and other
920 front end optimizations that remove promotions.
922 Return the common type for two arithmetic types T1 and T2 under the
923 usual arithmetic conversions. The default conversions have already
924 been applied, and enumerated types converted to their compatible
925 integer types. */
927 tree
928 common_type (tree t1, tree t2)
930 /* If one type is nonsense, use the other */
931 if (t1 == error_mark_node)
932 return t2;
933 if (t2 == error_mark_node)
934 return t1;
936 return cp_common_type (t1, t2);
939 /* Return the common type of two pointer types T1 and T2. This is the
940 type for the result of most arithmetic operations if the operands
941 have the given two types.
943 We assume that comp_target_types has already been done and returned
944 nonzero; if that isn't so, this may crash. */
946 tree
947 common_pointer_type (tree t1, tree t2)
949 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
950 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
951 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
953 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
954 CPO_CONVERSION, tf_warning_or_error);
957 /* Compare two exception specifier types for exactness or subsetness, if
958 allowed. Returns false for mismatch, true for match (same, or
959 derived and !exact).
961 [except.spec] "If a class X ... objects of class X or any class publicly
962 and unambiguously derived from X. Similarly, if a pointer type Y * ...
963 exceptions of type Y * or that are pointers to any type publicly and
964 unambiguously derived from Y. Otherwise a function only allows exceptions
965 that have the same type ..."
966 This does not mention cv qualifiers and is different to what throw
967 [except.throw] and catch [except.catch] will do. They will ignore the
968 top level cv qualifiers, and allow qualifiers in the pointer to class
969 example.
971 We implement the letter of the standard. */
973 static bool
974 comp_except_types (tree a, tree b, bool exact)
976 if (same_type_p (a, b))
977 return true;
978 else if (!exact)
980 if (cp_type_quals (a) || cp_type_quals (b))
981 return false;
983 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
985 a = TREE_TYPE (a);
986 b = TREE_TYPE (b);
987 if (cp_type_quals (a) || cp_type_quals (b))
988 return false;
991 if (TREE_CODE (a) != RECORD_TYPE
992 || TREE_CODE (b) != RECORD_TYPE)
993 return false;
995 if (publicly_uniquely_derived_p (a, b))
996 return true;
998 return false;
1001 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1002 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1003 If EXACT is ce_type, the C++17 type compatibility rules apply.
1004 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1005 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1006 are unordered, but we've already filtered out duplicates. Most lists will
1007 be in order, we should try to make use of that. */
1009 bool
1010 comp_except_specs (const_tree t1, const_tree t2, int exact)
1012 const_tree probe;
1013 const_tree base;
1014 int length = 0;
1016 if (t1 == t2)
1017 return true;
1019 /* First handle noexcept. */
1020 if (exact < ce_exact)
1022 if (exact == ce_type
1023 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1024 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1025 return true;
1027 /* noexcept(false) is compatible with no exception-specification,
1028 and less strict than any spec. */
1029 if (t1 == noexcept_false_spec)
1030 return t2 == NULL_TREE || exact == ce_derived;
1031 /* Even a derived noexcept(false) is compatible with no
1032 exception-specification. */
1033 if (t2 == noexcept_false_spec)
1034 return t1 == NULL_TREE;
1036 /* Otherwise, if we aren't looking for an exact match, noexcept is
1037 equivalent to throw(). */
1038 if (t1 == noexcept_true_spec)
1039 t1 = empty_except_spec;
1040 if (t2 == noexcept_true_spec)
1041 t2 = empty_except_spec;
1044 /* If any noexcept is left, it is only comparable to itself;
1045 either we're looking for an exact match or we're redeclaring a
1046 template with dependent noexcept. */
1047 if ((t1 && TREE_PURPOSE (t1))
1048 || (t2 && TREE_PURPOSE (t2)))
1049 return (t1 && t2
1050 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1052 if (t1 == NULL_TREE) /* T1 is ... */
1053 return t2 == NULL_TREE || exact == ce_derived;
1054 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1055 return t2 != NULL_TREE && !TREE_VALUE (t2);
1056 if (t2 == NULL_TREE) /* T2 is ... */
1057 return false;
1058 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1059 return exact == ce_derived;
1061 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1062 Count how many we find, to determine exactness. For exact matching and
1063 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1064 O(nm). */
1065 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1067 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1069 tree a = TREE_VALUE (probe);
1070 tree b = TREE_VALUE (t2);
1072 if (comp_except_types (a, b, exact))
1074 if (probe == base && exact > ce_derived)
1075 base = TREE_CHAIN (probe);
1076 length++;
1077 break;
1080 if (probe == NULL_TREE)
1081 return false;
1083 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1086 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1087 [] can match [size]. */
1089 static bool
1090 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1092 tree d1;
1093 tree d2;
1094 tree max1, max2;
1096 if (t1 == t2)
1097 return true;
1099 /* The type of the array elements must be the same. */
1100 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1101 return false;
1103 d1 = TYPE_DOMAIN (t1);
1104 d2 = TYPE_DOMAIN (t2);
1106 if (d1 == d2)
1107 return true;
1109 /* If one of the arrays is dimensionless, and the other has a
1110 dimension, they are of different types. However, it is valid to
1111 write:
1113 extern int a[];
1114 int a[3];
1116 by [basic.link]:
1118 declarations for an array object can specify
1119 array types that differ by the presence or absence of a major
1120 array bound (_dcl.array_). */
1121 if (!d1 || !d2)
1122 return allow_redeclaration;
1124 /* Check that the dimensions are the same. */
1126 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1127 return false;
1128 max1 = TYPE_MAX_VALUE (d1);
1129 max2 = TYPE_MAX_VALUE (d2);
1131 if (!cp_tree_equal (max1, max2))
1132 return false;
1134 return true;
1137 /* Compare the relative position of T1 and T2 into their respective
1138 template parameter list.
1139 T1 and T2 must be template parameter types.
1140 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1142 static bool
1143 comp_template_parms_position (tree t1, tree t2)
1145 tree index1, index2;
1146 gcc_assert (t1 && t2
1147 && TREE_CODE (t1) == TREE_CODE (t2)
1148 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1149 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1150 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1152 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1153 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1155 /* Then compare their relative position. */
1156 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1157 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1158 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1159 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1160 return false;
1162 /* In C++14 we can end up comparing 'auto' to a normal template
1163 parameter. Don't confuse them. */
1164 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1165 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1167 return true;
1170 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1172 static bool
1173 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1175 t1 = TYPE_MAIN_VARIANT (t1);
1176 t2 = TYPE_MAIN_VARIANT (t2);
1178 if (TYPE_PTR_P (t1)
1179 && TYPE_PTR_P (t2))
1180 return true;
1182 /* The signedness of the parameter matters only when an integral
1183 type smaller than int is promoted to int, otherwise only the
1184 precision of the parameter matters.
1185 This check should make sure that the callee does not see
1186 undefined values in argument registers. */
1187 if (INTEGRAL_TYPE_P (t1)
1188 && INTEGRAL_TYPE_P (t2)
1189 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1190 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1191 || !targetm.calls.promote_prototypes (NULL_TREE)
1192 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1193 return true;
1195 return same_type_p (t1, t2);
1198 /* Check if a type cast between two function types can be considered safe. */
1200 static bool
1201 cxx_safe_function_type_cast_p (tree t1, tree t2)
1203 if (TREE_TYPE (t1) == void_type_node &&
1204 TYPE_ARG_TYPES (t1) == void_list_node)
1205 return true;
1207 if (TREE_TYPE (t2) == void_type_node &&
1208 TYPE_ARG_TYPES (t2) == void_list_node)
1209 return true;
1211 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1212 return false;
1214 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1215 t1 && t2;
1216 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1217 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1218 return false;
1220 return true;
1223 /* Subroutine in comptypes. */
1225 static bool
1226 structural_comptypes (tree t1, tree t2, int strict)
1228 if (t1 == t2)
1229 return true;
1231 /* Suppress errors caused by previously reported errors. */
1232 if (t1 == error_mark_node || t2 == error_mark_node)
1233 return false;
1235 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1237 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1238 current instantiation. */
1239 if (TREE_CODE (t1) == TYPENAME_TYPE)
1240 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1242 if (TREE_CODE (t2) == TYPENAME_TYPE)
1243 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1245 if (TYPE_PTRMEMFUNC_P (t1))
1246 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1247 if (TYPE_PTRMEMFUNC_P (t2))
1248 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1250 /* Different classes of types can't be compatible. */
1251 if (TREE_CODE (t1) != TREE_CODE (t2))
1252 return false;
1254 /* Qualifiers must match. For array types, we will check when we
1255 recur on the array element types. */
1256 if (TREE_CODE (t1) != ARRAY_TYPE
1257 && cp_type_quals (t1) != cp_type_quals (t2))
1258 return false;
1259 if (TREE_CODE (t1) == FUNCTION_TYPE
1260 && type_memfn_quals (t1) != type_memfn_quals (t2))
1261 return false;
1262 /* Need to check this before TYPE_MAIN_VARIANT.
1263 FIXME function qualifiers should really change the main variant. */
1264 if (TREE_CODE (t1) == FUNCTION_TYPE
1265 || TREE_CODE (t1) == METHOD_TYPE)
1267 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1268 return false;
1269 if (flag_noexcept_type
1270 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1271 TYPE_RAISES_EXCEPTIONS (t2),
1272 ce_type))
1273 return false;
1276 /* Allow for two different type nodes which have essentially the same
1277 definition. Note that we already checked for equality of the type
1278 qualifiers (just above). */
1280 if (TREE_CODE (t1) != ARRAY_TYPE
1281 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1282 return true;
1285 /* Compare the types. Break out if they could be the same. */
1286 switch (TREE_CODE (t1))
1288 case VOID_TYPE:
1289 case BOOLEAN_TYPE:
1290 /* All void and bool types are the same. */
1291 break;
1293 case INTEGER_TYPE:
1294 case FIXED_POINT_TYPE:
1295 case REAL_TYPE:
1296 /* With these nodes, we can't determine type equivalence by
1297 looking at what is stored in the nodes themselves, because
1298 two nodes might have different TYPE_MAIN_VARIANTs but still
1299 represent the same type. For example, wchar_t and int could
1300 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1301 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1302 and are distinct types. On the other hand, int and the
1303 following typedef
1305 typedef int INT __attribute((may_alias));
1307 have identical properties, different TYPE_MAIN_VARIANTs, but
1308 represent the same type. The canonical type system keeps
1309 track of equivalence in this case, so we fall back on it. */
1310 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1312 case TEMPLATE_TEMPLATE_PARM:
1313 case BOUND_TEMPLATE_TEMPLATE_PARM:
1314 if (!comp_template_parms_position (t1, t2))
1315 return false;
1316 if (!comp_template_parms
1317 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1318 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1319 return false;
1320 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1321 break;
1322 /* Don't check inheritance. */
1323 strict = COMPARE_STRICT;
1324 /* Fall through. */
1326 case RECORD_TYPE:
1327 case UNION_TYPE:
1328 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1329 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1330 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1331 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1332 break;
1334 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1335 break;
1336 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1337 break;
1339 return false;
1341 case OFFSET_TYPE:
1342 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1343 strict & ~COMPARE_REDECLARATION))
1344 return false;
1345 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1346 return false;
1347 break;
1349 case REFERENCE_TYPE:
1350 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1351 return false;
1352 /* fall through to checks for pointer types */
1353 gcc_fallthrough ();
1355 case POINTER_TYPE:
1356 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1357 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1358 return false;
1359 break;
1361 case METHOD_TYPE:
1362 case FUNCTION_TYPE:
1363 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1364 return false;
1365 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1366 return false;
1367 break;
1369 case ARRAY_TYPE:
1370 /* Target types must match incl. qualifiers. */
1371 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1372 return false;
1373 break;
1375 case TEMPLATE_TYPE_PARM:
1376 /* If T1 and T2 don't have the same relative position in their
1377 template parameters set, they can't be equal. */
1378 if (!comp_template_parms_position (t1, t2))
1379 return false;
1380 /* If T1 and T2 don't represent the same class template deduction,
1381 they aren't equal. */
1382 if (CLASS_PLACEHOLDER_TEMPLATE (t1)
1383 != CLASS_PLACEHOLDER_TEMPLATE (t2))
1384 return false;
1385 /* Constrained 'auto's are distinct from parms that don't have the same
1386 constraints. */
1387 if (!equivalent_placeholder_constraints (t1, t2))
1388 return false;
1389 break;
1391 case TYPENAME_TYPE:
1392 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1393 TYPENAME_TYPE_FULLNAME (t2)))
1394 return false;
1395 /* Qualifiers don't matter on scopes. */
1396 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1397 TYPE_CONTEXT (t2)))
1398 return false;
1399 break;
1401 case UNBOUND_CLASS_TEMPLATE:
1402 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1403 return false;
1404 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1405 return false;
1406 break;
1408 case COMPLEX_TYPE:
1409 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1410 return false;
1411 break;
1413 case VECTOR_TYPE:
1414 if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1415 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1416 return false;
1417 break;
1419 case TYPE_PACK_EXPANSION:
1420 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1421 PACK_EXPANSION_PATTERN (t2))
1422 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1423 PACK_EXPANSION_EXTRA_ARGS (t2)));
1425 case DECLTYPE_TYPE:
1426 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1427 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1428 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1429 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1430 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1431 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1432 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1433 DECLTYPE_TYPE_EXPR (t2)))
1434 return false;
1435 break;
1437 case UNDERLYING_TYPE:
1438 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1439 UNDERLYING_TYPE_TYPE (t2));
1441 default:
1442 return false;
1445 /* If we get here, we know that from a target independent POV the
1446 types are the same. Make sure the target attributes are also
1447 the same. */
1448 return comp_type_attributes (t1, t2);
1451 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1452 is a bitwise-or of the COMPARE_* flags. */
1454 bool
1455 comptypes (tree t1, tree t2, int strict)
1457 if (strict == COMPARE_STRICT)
1459 if (t1 == t2)
1460 return true;
1462 if (t1 == error_mark_node || t2 == error_mark_node)
1463 return false;
1465 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1466 /* At least one of the types requires structural equality, so
1467 perform a deep check. */
1468 return structural_comptypes (t1, t2, strict);
1470 if (flag_checking && USE_CANONICAL_TYPES)
1472 bool result = structural_comptypes (t1, t2, strict);
1474 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1475 /* The two types are structurally equivalent, but their
1476 canonical types were different. This is a failure of the
1477 canonical type propagation code.*/
1478 internal_error
1479 ("canonical types differ for identical types %qT and %qT",
1480 t1, t2);
1481 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1482 /* Two types are structurally different, but the canonical
1483 types are the same. This means we were over-eager in
1484 assigning canonical types. */
1485 internal_error
1486 ("same canonical type node for different types %qT and %qT",
1487 t1, t2);
1489 return result;
1491 if (!flag_checking && USE_CANONICAL_TYPES)
1492 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1493 else
1494 return structural_comptypes (t1, t2, strict);
1496 else if (strict == COMPARE_STRUCTURAL)
1497 return structural_comptypes (t1, t2, COMPARE_STRICT);
1498 else
1499 return structural_comptypes (t1, t2, strict);
1502 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1503 top-level qualifiers. */
1505 bool
1506 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1508 if (type1 == error_mark_node || type2 == error_mark_node)
1509 return false;
1511 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1512 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1513 return same_type_p (type1, type2);
1516 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1518 bool
1519 at_least_as_qualified_p (const_tree type1, const_tree type2)
1521 int q1 = cp_type_quals (type1);
1522 int q2 = cp_type_quals (type2);
1524 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1525 return (q1 & q2) == q2;
1528 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1529 more cv-qualified that TYPE1, and 0 otherwise. */
1532 comp_cv_qualification (int q1, int q2)
1534 if (q1 == q2)
1535 return 0;
1537 if ((q1 & q2) == q2)
1538 return 1;
1539 else if ((q1 & q2) == q1)
1540 return -1;
1542 return 0;
1546 comp_cv_qualification (const_tree type1, const_tree type2)
1548 int q1 = cp_type_quals (type1);
1549 int q2 = cp_type_quals (type2);
1550 return comp_cv_qualification (q1, q2);
1553 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1554 subset of the cv-qualification signature of TYPE2, and the types
1555 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1558 comp_cv_qual_signature (tree type1, tree type2)
1560 if (comp_ptr_ttypes_real (type2, type1, -1))
1561 return 1;
1562 else if (comp_ptr_ttypes_real (type1, type2, -1))
1563 return -1;
1564 else
1565 return 0;
1568 /* Subroutines of `comptypes'. */
1570 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1571 equivalent in the sense that functions with those parameter types
1572 can have equivalent types. The two lists must be equivalent,
1573 element by element. */
1575 bool
1576 compparms (const_tree parms1, const_tree parms2)
1578 const_tree t1, t2;
1580 /* An unspecified parmlist matches any specified parmlist
1581 whose argument types don't need default promotions. */
1583 for (t1 = parms1, t2 = parms2;
1584 t1 || t2;
1585 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1587 /* If one parmlist is shorter than the other,
1588 they fail to match. */
1589 if (!t1 || !t2)
1590 return false;
1591 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1592 return false;
1594 return true;
1598 /* Process a sizeof or alignof expression where the operand is a
1599 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1600 or GNU (preferred alignment) semantics; it is ignored if op is
1601 SIZEOF_EXPR. */
1603 tree
1604 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool std_alignof,
1605 bool complain)
1607 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1608 if (type == error_mark_node)
1609 return error_mark_node;
1611 type = non_reference (type);
1612 if (TREE_CODE (type) == METHOD_TYPE)
1614 if (complain)
1616 pedwarn (input_location, OPT_Wpointer_arith,
1617 "invalid application of %qs to a member function",
1618 OVL_OP_INFO (false, op)->name);
1619 return size_one_node;
1621 else
1622 return error_mark_node;
1625 bool dependent_p = dependent_type_p (type);
1626 if (!dependent_p)
1627 complete_type (type);
1628 if (dependent_p
1629 /* VLA types will have a non-constant size. In the body of an
1630 uninstantiated template, we don't need to try to compute the
1631 value, because the sizeof expression is not an integral
1632 constant expression in that case. And, if we do try to
1633 compute the value, we'll likely end up with SAVE_EXPRs, which
1634 the template substitution machinery does not expect to see. */
1635 || (processing_template_decl
1636 && COMPLETE_TYPE_P (type)
1637 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1639 tree value = build_min (op, size_type_node, type);
1640 TREE_READONLY (value) = 1;
1641 if (op == ALIGNOF_EXPR && std_alignof)
1642 ALIGNOF_EXPR_STD_P (value) = true;
1643 return value;
1646 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1647 op == SIZEOF_EXPR, std_alignof,
1648 complain);
1651 /* Return the size of the type, without producing any warnings for
1652 types whose size cannot be taken. This routine should be used only
1653 in some other routine that has already produced a diagnostic about
1654 using the size of such a type. */
1655 tree
1656 cxx_sizeof_nowarn (tree type)
1658 if (TREE_CODE (type) == FUNCTION_TYPE
1659 || VOID_TYPE_P (type)
1660 || TREE_CODE (type) == ERROR_MARK)
1661 return size_one_node;
1662 else if (!COMPLETE_TYPE_P (type))
1663 return size_zero_node;
1664 else
1665 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false, false);
1668 /* Process a sizeof expression where the operand is an expression. */
1670 static tree
1671 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1673 if (e == error_mark_node)
1674 return error_mark_node;
1676 if (processing_template_decl)
1678 e = build_min (SIZEOF_EXPR, size_type_node, e);
1679 TREE_SIDE_EFFECTS (e) = 0;
1680 TREE_READONLY (e) = 1;
1682 return e;
1685 /* To get the size of a static data member declared as an array of
1686 unknown bound, we need to instantiate it. */
1687 if (VAR_P (e)
1688 && VAR_HAD_UNKNOWN_BOUND (e)
1689 && DECL_TEMPLATE_INSTANTIATION (e))
1690 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1692 if (TREE_CODE (e) == PARM_DECL
1693 && DECL_ARRAY_PARAMETER_P (e)
1694 && (complain & tf_warning))
1696 auto_diagnostic_group d;
1697 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1698 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1699 inform (DECL_SOURCE_LOCATION (e), "declared here");
1702 e = mark_type_use (e);
1704 if (bitfield_p (e))
1706 if (complain & tf_error)
1707 error ("invalid application of %<sizeof%> to a bit-field");
1708 else
1709 return error_mark_node;
1710 e = char_type_node;
1712 else if (is_overloaded_fn (e))
1714 if (complain & tf_error)
1715 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1716 "function type");
1717 else
1718 return error_mark_node;
1719 e = char_type_node;
1721 else if (type_unknown_p (e))
1723 if (complain & tf_error)
1724 cxx_incomplete_type_error (e, TREE_TYPE (e));
1725 else
1726 return error_mark_node;
1727 e = char_type_node;
1729 else
1730 e = TREE_TYPE (e);
1732 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, false, complain & tf_error);
1735 /* Implement the __alignof keyword: Return the minimum required
1736 alignment of E, measured in bytes. For VAR_DECL's and
1737 FIELD_DECL's return DECL_ALIGN (which can be set from an
1738 "aligned" __attribute__ specification). */
1740 static tree
1741 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1743 tree t;
1745 if (e == error_mark_node)
1746 return error_mark_node;
1748 if (processing_template_decl)
1750 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1751 TREE_SIDE_EFFECTS (e) = 0;
1752 TREE_READONLY (e) = 1;
1754 return e;
1757 e = mark_type_use (e);
1759 if (VAR_P (e))
1760 t = size_int (DECL_ALIGN_UNIT (e));
1761 else if (bitfield_p (e))
1763 if (complain & tf_error)
1764 error ("invalid application of %<__alignof%> to a bit-field");
1765 else
1766 return error_mark_node;
1767 t = size_one_node;
1769 else if (TREE_CODE (e) == COMPONENT_REF
1770 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1771 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1772 else if (is_overloaded_fn (e))
1774 if (complain & tf_error)
1775 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1776 "function type");
1777 else
1778 return error_mark_node;
1779 if (TREE_CODE (e) == FUNCTION_DECL)
1780 t = size_int (DECL_ALIGN_UNIT (e));
1781 else
1782 t = size_one_node;
1784 else if (type_unknown_p (e))
1786 if (complain & tf_error)
1787 cxx_incomplete_type_error (e, TREE_TYPE (e));
1788 else
1789 return error_mark_node;
1790 t = size_one_node;
1792 else
1793 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, false,
1794 complain & tf_error);
1796 return fold_convert (size_type_node, t);
1799 /* Process a sizeof or alignof expression E with code OP where the operand
1800 is an expression. */
1802 tree
1803 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1805 if (op == SIZEOF_EXPR)
1806 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1807 else
1808 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1811 /* Build a representation of an expression 'alignas(E).' Return the
1812 folded integer value of E if it is an integral constant expression
1813 that resolves to a valid alignment. If E depends on a template
1814 parameter, return a syntactic representation tree of kind
1815 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1816 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1818 tree
1819 cxx_alignas_expr (tree e)
1821 if (e == NULL_TREE || e == error_mark_node
1822 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1823 return e;
1825 if (TYPE_P (e))
1826 /* [dcl.align]/3:
1828 When the alignment-specifier is of the form
1829 alignas(type-id ), it shall have the same effect as
1830 alignas(alignof(type-id )). */
1832 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, true, false);
1834 /* If we reach this point, it means the alignas expression if of
1835 the form "alignas(assignment-expression)", so we should follow
1836 what is stated by [dcl.align]/2. */
1838 if (value_dependent_expression_p (e))
1839 /* Leave value-dependent expression alone for now. */
1840 return e;
1842 e = instantiate_non_dependent_expr (e);
1843 e = mark_rvalue_use (e);
1845 /* [dcl.align]/2 says:
1847 the assignment-expression shall be an integral constant
1848 expression. */
1850 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1852 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1853 return error_mark_node;
1856 return cxx_constant_value (e);
1860 /* EXPR is being used in a context that is not a function call.
1861 Enforce:
1863 [expr.ref]
1865 The expression can be used only as the left-hand operand of a
1866 member function call.
1868 [expr.mptr.operator]
1870 If the result of .* or ->* is a function, then that result can be
1871 used only as the operand for the function call operator ().
1873 by issuing an error message if appropriate. Returns true iff EXPR
1874 violates these rules. */
1876 bool
1877 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1879 if (expr == NULL_TREE)
1880 return false;
1881 /* Don't enforce this in MS mode. */
1882 if (flag_ms_extensions)
1883 return false;
1884 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1885 expr = get_first_fn (expr);
1886 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1888 if (complain & tf_error)
1890 if (DECL_P (expr))
1892 error_at (loc, "invalid use of non-static member function %qD",
1893 expr);
1894 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1896 else
1897 error_at (loc, "invalid use of non-static member function of "
1898 "type %qT", TREE_TYPE (expr));
1900 return true;
1902 return false;
1905 /* If EXP is a reference to a bitfield, and the type of EXP does not
1906 match the declared type of the bitfield, return the declared type
1907 of the bitfield. Otherwise, return NULL_TREE. */
1909 tree
1910 is_bitfield_expr_with_lowered_type (const_tree exp)
1912 switch (TREE_CODE (exp))
1914 case COND_EXPR:
1915 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1916 ? TREE_OPERAND (exp, 1)
1917 : TREE_OPERAND (exp, 0)))
1918 return NULL_TREE;
1919 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1921 case COMPOUND_EXPR:
1922 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1924 case MODIFY_EXPR:
1925 case SAVE_EXPR:
1926 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1928 case COMPONENT_REF:
1930 tree field;
1932 field = TREE_OPERAND (exp, 1);
1933 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1934 return NULL_TREE;
1935 if (same_type_ignoring_top_level_qualifiers_p
1936 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1937 return NULL_TREE;
1938 return DECL_BIT_FIELD_TYPE (field);
1941 case VAR_DECL:
1942 if (DECL_HAS_VALUE_EXPR_P (exp))
1943 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1944 (CONST_CAST_TREE (exp)));
1945 return NULL_TREE;
1947 CASE_CONVERT:
1948 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1949 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1950 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1951 /* Fallthrough. */
1953 default:
1954 return NULL_TREE;
1958 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1959 bitfield with a lowered type, the type of EXP is returned, rather
1960 than NULL_TREE. */
1962 tree
1963 unlowered_expr_type (const_tree exp)
1965 tree type;
1966 tree etype = TREE_TYPE (exp);
1968 type = is_bitfield_expr_with_lowered_type (exp);
1969 if (type)
1970 type = cp_build_qualified_type (type, cp_type_quals (etype));
1971 else
1972 type = etype;
1974 return type;
1977 /* Perform the conversions in [expr] that apply when an lvalue appears
1978 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1979 function-to-pointer conversions. In addition, bitfield references are
1980 converted to their declared types. Note that this function does not perform
1981 the lvalue-to-rvalue conversion for class types. If you need that conversion
1982 for class types, then you probably need to use force_rvalue.
1984 Although the returned value is being used as an rvalue, this
1985 function does not wrap the returned expression in a
1986 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1987 that the return value is no longer an lvalue. */
1989 tree
1990 decay_conversion (tree exp,
1991 tsubst_flags_t complain,
1992 bool reject_builtin /* = true */)
1994 tree type;
1995 enum tree_code code;
1996 location_t loc = cp_expr_loc_or_loc (exp, input_location);
1998 type = TREE_TYPE (exp);
1999 if (type == error_mark_node)
2000 return error_mark_node;
2002 exp = resolve_nondeduced_context (exp, complain);
2003 if (type_unknown_p (exp))
2005 if (complain & tf_error)
2006 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
2007 return error_mark_node;
2010 code = TREE_CODE (type);
2012 if (error_operand_p (exp))
2013 return error_mark_node;
2015 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2017 mark_rvalue_use (exp, loc, reject_builtin);
2018 return nullptr_node;
2021 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2022 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2023 if (code == VOID_TYPE)
2025 if (complain & tf_error)
2026 error_at (loc, "void value not ignored as it ought to be");
2027 return error_mark_node;
2029 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2030 return error_mark_node;
2031 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2033 exp = mark_lvalue_use (exp);
2034 if (reject_builtin && reject_gcc_builtin (exp, loc))
2035 return error_mark_node;
2036 return cp_build_addr_expr (exp, complain);
2038 if (code == ARRAY_TYPE)
2040 tree adr;
2041 tree ptrtype;
2043 exp = mark_lvalue_use (exp);
2045 if (INDIRECT_REF_P (exp))
2046 return build_nop (build_pointer_type (TREE_TYPE (type)),
2047 TREE_OPERAND (exp, 0));
2049 if (TREE_CODE (exp) == COMPOUND_EXPR)
2051 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2052 if (op1 == error_mark_node)
2053 return error_mark_node;
2054 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2055 TREE_OPERAND (exp, 0), op1);
2058 if (!obvalue_p (exp)
2059 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2061 if (complain & tf_error)
2062 error_at (loc, "invalid use of non-lvalue array");
2063 return error_mark_node;
2066 /* Don't let an array compound literal decay to a pointer. It can
2067 still be used to initialize an array or bind to a reference. */
2068 if (TREE_CODE (exp) == TARGET_EXPR)
2070 if (complain & tf_error)
2071 error_at (loc, "taking address of temporary array");
2072 return error_mark_node;
2075 ptrtype = build_pointer_type (TREE_TYPE (type));
2077 if (VAR_P (exp))
2079 if (!cxx_mark_addressable (exp))
2080 return error_mark_node;
2081 adr = build_nop (ptrtype, build_address (exp));
2082 return adr;
2084 /* This way is better for a COMPONENT_REF since it can
2085 simplify the offset for a component. */
2086 adr = cp_build_addr_expr (exp, complain);
2087 return cp_convert (ptrtype, adr, complain);
2090 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2091 exp = mark_rvalue_use (exp, loc, reject_builtin);
2093 /* If a bitfield is used in a context where integral promotion
2094 applies, then the caller is expected to have used
2095 default_conversion. That function promotes bitfields correctly
2096 before calling this function. At this point, if we have a
2097 bitfield referenced, we may assume that is not subject to
2098 promotion, and that, therefore, the type of the resulting rvalue
2099 is the declared type of the bitfield. */
2100 exp = convert_bitfield_to_declared_type (exp);
2102 /* We do not call rvalue() here because we do not want to wrap EXP
2103 in a NON_LVALUE_EXPR. */
2105 /* [basic.lval]
2107 Non-class rvalues always have cv-unqualified types. */
2108 type = TREE_TYPE (exp);
2109 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2110 exp = build_nop (cv_unqualified (type), exp);
2112 if (!complete_type_or_maybe_complain (type, exp, complain))
2113 return error_mark_node;
2115 return exp;
2118 /* Perform preparatory conversions, as part of the "usual arithmetic
2119 conversions". In particular, as per [expr]:
2121 Whenever an lvalue expression appears as an operand of an
2122 operator that expects the rvalue for that operand, the
2123 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2124 standard conversions are applied to convert the expression to an
2125 rvalue.
2127 In addition, we perform integral promotions here, as those are
2128 applied to both operands to a binary operator before determining
2129 what additional conversions should apply. */
2131 static tree
2132 cp_default_conversion (tree exp, tsubst_flags_t complain)
2134 /* Check for target-specific promotions. */
2135 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2136 if (promoted_type)
2137 exp = cp_convert (promoted_type, exp, complain);
2138 /* Perform the integral promotions first so that bitfield
2139 expressions (which may promote to "int", even if the bitfield is
2140 declared "unsigned") are promoted correctly. */
2141 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2142 exp = cp_perform_integral_promotions (exp, complain);
2143 /* Perform the other conversions. */
2144 exp = decay_conversion (exp, complain);
2146 return exp;
2149 /* C version. */
2151 tree
2152 default_conversion (tree exp)
2154 return cp_default_conversion (exp, tf_warning_or_error);
2157 /* EXPR is an expression with an integral or enumeration type.
2158 Perform the integral promotions in [conv.prom], and return the
2159 converted value. */
2161 tree
2162 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2164 tree type;
2165 tree promoted_type;
2167 expr = mark_rvalue_use (expr);
2168 if (error_operand_p (expr))
2169 return error_mark_node;
2171 /* [conv.prom]
2173 If the bitfield has an enumerated type, it is treated as any
2174 other value of that type for promotion purposes. */
2175 type = is_bitfield_expr_with_lowered_type (expr);
2176 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2177 type = TREE_TYPE (expr);
2178 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2179 /* Scoped enums don't promote. */
2180 if (SCOPED_ENUM_P (type))
2181 return expr;
2182 promoted_type = type_promotes_to (type);
2183 if (type != promoted_type)
2184 expr = cp_convert (promoted_type, expr, complain);
2185 return expr;
2188 /* C version. */
2190 tree
2191 perform_integral_promotions (tree expr)
2193 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2196 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2197 decay_conversion to one. */
2200 string_conv_p (const_tree totype, const_tree exp, int warn)
2202 tree t;
2204 if (!TYPE_PTR_P (totype))
2205 return 0;
2207 t = TREE_TYPE (totype);
2208 if (!same_type_p (t, char_type_node)
2209 && !same_type_p (t, char16_type_node)
2210 && !same_type_p (t, char32_type_node)
2211 && !same_type_p (t, wchar_type_node))
2212 return 0;
2214 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2216 STRIP_ANY_LOCATION_WRAPPER (exp);
2218 if (TREE_CODE (exp) == STRING_CST)
2220 /* Make sure that we don't try to convert between char and wide chars. */
2221 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2222 return 0;
2224 else
2226 /* Is this a string constant which has decayed to 'const char *'? */
2227 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2228 if (!same_type_p (TREE_TYPE (exp), t))
2229 return 0;
2230 STRIP_NOPS (exp);
2231 if (TREE_CODE (exp) != ADDR_EXPR
2232 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2233 return 0;
2235 if (warn)
2237 if (cxx_dialect >= cxx11)
2238 pedwarn (loc, OPT_Wwrite_strings,
2239 "ISO C++ forbids converting a string constant to %qT",
2240 totype);
2241 else
2242 warning_at (loc, OPT_Wwrite_strings,
2243 "deprecated conversion from string constant to %qT",
2244 totype);
2247 return 1;
2250 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2251 can, for example, use as an lvalue. This code used to be in
2252 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2253 expressions, where we're dealing with aggregates. But now it's again only
2254 called from unary_complex_lvalue. The case (in particular) that led to
2255 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2256 get it there. */
2258 static tree
2259 rationalize_conditional_expr (enum tree_code code, tree t,
2260 tsubst_flags_t complain)
2262 location_t loc = cp_expr_loc_or_loc (t, input_location);
2264 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2265 the first operand is always the one to be used if both operands
2266 are equal, so we know what conditional expression this used to be. */
2267 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2269 tree op0 = TREE_OPERAND (t, 0);
2270 tree op1 = TREE_OPERAND (t, 1);
2272 /* The following code is incorrect if either operand side-effects. */
2273 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2274 && !TREE_SIDE_EFFECTS (op1));
2275 return
2276 build_conditional_expr (loc,
2277 build_x_binary_op (loc,
2278 (TREE_CODE (t) == MIN_EXPR
2279 ? LE_EXPR : GE_EXPR),
2280 op0, TREE_CODE (op0),
2281 op1, TREE_CODE (op1),
2282 /*overload=*/NULL,
2283 complain),
2284 cp_build_unary_op (code, op0, false, complain),
2285 cp_build_unary_op (code, op1, false, complain),
2286 complain);
2289 return
2290 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2291 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2292 complain),
2293 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2294 complain),
2295 complain);
2298 /* Given the TYPE of an anonymous union field inside T, return the
2299 FIELD_DECL for the field. If not found return NULL_TREE. Because
2300 anonymous unions can nest, we must also search all anonymous unions
2301 that are directly reachable. */
2303 tree
2304 lookup_anon_field (tree t, tree type)
2306 tree field;
2308 t = TYPE_MAIN_VARIANT (t);
2310 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2312 if (TREE_STATIC (field))
2313 continue;
2314 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2315 continue;
2317 /* If we find it directly, return the field. */
2318 if (DECL_NAME (field) == NULL_TREE
2319 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2321 return field;
2324 /* Otherwise, it could be nested, search harder. */
2325 if (DECL_NAME (field) == NULL_TREE
2326 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2328 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2329 if (subfield)
2330 return subfield;
2333 return NULL_TREE;
2336 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2337 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2338 non-NULL, it indicates the path to the base used to name MEMBER.
2339 If PRESERVE_REFERENCE is true, the expression returned will have
2340 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2341 returned will have the type referred to by the reference.
2343 This function does not perform access control; that is either done
2344 earlier by the parser when the name of MEMBER is resolved to MEMBER
2345 itself, or later when overload resolution selects one of the
2346 functions indicated by MEMBER. */
2348 tree
2349 build_class_member_access_expr (cp_expr object, tree member,
2350 tree access_path, bool preserve_reference,
2351 tsubst_flags_t complain)
2353 tree object_type;
2354 tree member_scope;
2355 tree result = NULL_TREE;
2356 tree using_decl = NULL_TREE;
2358 if (error_operand_p (object) || error_operand_p (member))
2359 return error_mark_node;
2361 gcc_assert (DECL_P (member) || BASELINK_P (member));
2363 /* [expr.ref]
2365 The type of the first expression shall be "class object" (of a
2366 complete type). */
2367 object_type = TREE_TYPE (object);
2368 if (!currently_open_class (object_type)
2369 && !complete_type_or_maybe_complain (object_type, object, complain))
2370 return error_mark_node;
2371 if (!CLASS_TYPE_P (object_type))
2373 if (complain & tf_error)
2375 if (INDIRECT_TYPE_P (object_type)
2376 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2377 error ("request for member %qD in %qE, which is of pointer "
2378 "type %qT (maybe you meant to use %<->%> ?)",
2379 member, object.get_value (), object_type);
2380 else
2381 error ("request for member %qD in %qE, which is of non-class "
2382 "type %qT", member, object.get_value (), object_type);
2384 return error_mark_node;
2387 /* The standard does not seem to actually say that MEMBER must be a
2388 member of OBJECT_TYPE. However, that is clearly what is
2389 intended. */
2390 if (DECL_P (member))
2392 member_scope = DECL_CLASS_CONTEXT (member);
2393 if (!mark_used (member, complain) && !(complain & tf_error))
2394 return error_mark_node;
2395 if (TREE_DEPRECATED (member))
2396 warn_deprecated_use (member, NULL_TREE);
2398 else
2399 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2400 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2401 presently be the anonymous union. Go outwards until we find a
2402 type related to OBJECT_TYPE. */
2403 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2404 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2405 object_type))
2406 member_scope = TYPE_CONTEXT (member_scope);
2407 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2409 if (complain & tf_error)
2411 if (TREE_CODE (member) == FIELD_DECL)
2412 error ("invalid use of nonstatic data member %qE", member);
2413 else
2414 error ("%qD is not a member of %qT", member, object_type);
2416 return error_mark_node;
2419 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2420 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2421 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2423 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2424 if (temp)
2425 object = cp_build_fold_indirect_ref (temp);
2428 /* In [expr.ref], there is an explicit list of the valid choices for
2429 MEMBER. We check for each of those cases here. */
2430 if (VAR_P (member))
2432 /* A static data member. */
2433 result = member;
2434 mark_exp_read (object);
2435 /* If OBJECT has side-effects, they are supposed to occur. */
2436 if (TREE_SIDE_EFFECTS (object))
2437 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2439 else if (TREE_CODE (member) == FIELD_DECL)
2441 /* A non-static data member. */
2442 bool null_object_p;
2443 int type_quals;
2444 tree member_type;
2446 if (INDIRECT_REF_P (object))
2447 null_object_p =
2448 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2449 else
2450 null_object_p = false;
2452 /* Convert OBJECT to the type of MEMBER. */
2453 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2454 TYPE_MAIN_VARIANT (member_scope)))
2456 tree binfo;
2457 base_kind kind;
2459 binfo = lookup_base (access_path ? access_path : object_type,
2460 member_scope, ba_unique, &kind, complain);
2461 if (binfo == error_mark_node)
2462 return error_mark_node;
2464 /* It is invalid to try to get to a virtual base of a
2465 NULL object. The most common cause is invalid use of
2466 offsetof macro. */
2467 if (null_object_p && kind == bk_via_virtual)
2469 if (complain & tf_error)
2471 error ("invalid access to non-static data member %qD in "
2472 "virtual base of NULL object", member);
2474 return error_mark_node;
2477 /* Convert to the base. */
2478 object = build_base_path (PLUS_EXPR, object, binfo,
2479 /*nonnull=*/1, complain);
2480 /* If we found the base successfully then we should be able
2481 to convert to it successfully. */
2482 gcc_assert (object != error_mark_node);
2485 /* If MEMBER is from an anonymous aggregate, we have converted
2486 OBJECT so that it refers to the class containing the
2487 anonymous union. Generate a reference to the anonymous union
2488 itself, and recur to find MEMBER. */
2489 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2490 /* When this code is called from build_field_call, the
2491 object already has the type of the anonymous union.
2492 That is because the COMPONENT_REF was already
2493 constructed, and was then disassembled before calling
2494 build_field_call. After the function-call code is
2495 cleaned up, this waste can be eliminated. */
2496 && (!same_type_ignoring_top_level_qualifiers_p
2497 (TREE_TYPE (object), DECL_CONTEXT (member))))
2499 tree anonymous_union;
2501 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2502 DECL_CONTEXT (member));
2503 object = build_class_member_access_expr (object,
2504 anonymous_union,
2505 /*access_path=*/NULL_TREE,
2506 preserve_reference,
2507 complain);
2510 /* Compute the type of the field, as described in [expr.ref]. */
2511 type_quals = TYPE_UNQUALIFIED;
2512 member_type = TREE_TYPE (member);
2513 if (!TYPE_REF_P (member_type))
2515 type_quals = (cp_type_quals (member_type)
2516 | cp_type_quals (object_type));
2518 /* A field is const (volatile) if the enclosing object, or the
2519 field itself, is const (volatile). But, a mutable field is
2520 not const, even within a const object. */
2521 if (DECL_MUTABLE_P (member))
2522 type_quals &= ~TYPE_QUAL_CONST;
2523 member_type = cp_build_qualified_type (member_type, type_quals);
2526 result = build3_loc (input_location, COMPONENT_REF, member_type,
2527 object, member, NULL_TREE);
2529 /* Mark the expression const or volatile, as appropriate. Even
2530 though we've dealt with the type above, we still have to mark the
2531 expression itself. */
2532 if (type_quals & TYPE_QUAL_CONST)
2533 TREE_READONLY (result) = 1;
2534 if (type_quals & TYPE_QUAL_VOLATILE)
2535 TREE_THIS_VOLATILE (result) = 1;
2537 else if (BASELINK_P (member))
2539 /* The member is a (possibly overloaded) member function. */
2540 tree functions;
2541 tree type;
2543 /* If the MEMBER is exactly one static member function, then we
2544 know the type of the expression. Otherwise, we must wait
2545 until overload resolution has been performed. */
2546 functions = BASELINK_FUNCTIONS (member);
2547 if (TREE_CODE (functions) == FUNCTION_DECL
2548 && DECL_STATIC_FUNCTION_P (functions))
2549 type = TREE_TYPE (functions);
2550 else
2551 type = unknown_type_node;
2552 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2553 base. That will happen when the function is called. */
2554 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2556 else if (TREE_CODE (member) == CONST_DECL)
2558 /* The member is an enumerator. */
2559 result = member;
2560 /* If OBJECT has side-effects, they are supposed to occur. */
2561 if (TREE_SIDE_EFFECTS (object))
2562 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2563 object, result);
2565 else if ((using_decl = strip_using_decl (member)) != member)
2566 result = build_class_member_access_expr (object,
2567 using_decl,
2568 access_path, preserve_reference,
2569 complain);
2570 else
2572 if (complain & tf_error)
2573 error ("invalid use of %qD", member);
2574 return error_mark_node;
2577 if (!preserve_reference)
2578 /* [expr.ref]
2580 If E2 is declared to have type "reference to T", then ... the
2581 type of E1.E2 is T. */
2582 result = convert_from_reference (result);
2584 return result;
2587 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2588 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2590 static tree
2591 lookup_destructor (tree object, tree scope, tree dtor_name,
2592 tsubst_flags_t complain)
2594 tree object_type = TREE_TYPE (object);
2595 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2596 tree expr;
2598 /* We've already complained about this destructor. */
2599 if (dtor_type == error_mark_node)
2600 return error_mark_node;
2602 if (scope && !check_dtor_name (scope, dtor_type))
2604 if (complain & tf_error)
2605 error ("qualified type %qT does not match destructor name ~%qT",
2606 scope, dtor_type);
2607 return error_mark_node;
2609 if (is_auto (dtor_type))
2610 dtor_type = object_type;
2611 else if (identifier_p (dtor_type))
2613 /* In a template, names we can't find a match for are still accepted
2614 destructor names, and we check them here. */
2615 if (check_dtor_name (object_type, dtor_type))
2616 dtor_type = object_type;
2617 else
2619 if (complain & tf_error)
2620 error ("object type %qT does not match destructor name ~%qT",
2621 object_type, dtor_type);
2622 return error_mark_node;
2626 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2628 if (complain & tf_error)
2629 error ("the type being destroyed is %qT, but the destructor "
2630 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2631 return error_mark_node;
2633 expr = lookup_member (dtor_type, complete_dtor_identifier,
2634 /*protect=*/1, /*want_type=*/false,
2635 tf_warning_or_error);
2636 if (!expr)
2638 if (complain & tf_error)
2639 cxx_incomplete_type_error (dtor_name, dtor_type);
2640 return error_mark_node;
2642 expr = (adjust_result_of_qualified_name_lookup
2643 (expr, dtor_type, object_type));
2644 if (scope == NULL_TREE)
2645 /* We need to call adjust_result_of_qualified_name_lookup in case the
2646 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2647 that we still get virtual function binding. */
2648 BASELINK_QUALIFIED_P (expr) = false;
2649 return expr;
2652 /* An expression of the form "A::template B" has been resolved to
2653 DECL. Issue a diagnostic if B is not a template or template
2654 specialization. */
2656 void
2657 check_template_keyword (tree decl)
2659 /* The standard says:
2661 [temp.names]
2663 If a name prefixed by the keyword template is not a member
2664 template, the program is ill-formed.
2666 DR 228 removed the restriction that the template be a member
2667 template.
2669 DR 96, if accepted would add the further restriction that explicit
2670 template arguments must be provided if the template keyword is
2671 used, but, as of 2005-10-16, that DR is still in "drafting". If
2672 this DR is accepted, then the semantic checks here can be
2673 simplified, as the entity named must in fact be a template
2674 specialization, rather than, as at present, a set of overloaded
2675 functions containing at least one template function. */
2676 if (TREE_CODE (decl) != TEMPLATE_DECL
2677 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2679 if (VAR_P (decl))
2681 if (DECL_USE_TEMPLATE (decl)
2682 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2684 else
2685 permerror (input_location, "%qD is not a template", decl);
2687 else if (!is_overloaded_fn (decl))
2688 permerror (input_location, "%qD is not a template", decl);
2689 else
2691 bool found = false;
2693 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2694 !found && iter; ++iter)
2696 tree fn = *iter;
2697 if (TREE_CODE (fn) == TEMPLATE_DECL
2698 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2699 || (TREE_CODE (fn) == FUNCTION_DECL
2700 && DECL_USE_TEMPLATE (fn)
2701 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2702 found = true;
2704 if (!found)
2705 permerror (input_location, "%qD is not a template", decl);
2710 /* Record that an access failure occurred on BASETYPE_PATH attempting
2711 to access DECL, where DIAG_DECL should be used for diagnostics. */
2713 void
2714 access_failure_info::record_access_failure (tree basetype_path,
2715 tree decl, tree diag_decl)
2717 m_was_inaccessible = true;
2718 m_basetype_path = basetype_path;
2719 m_decl = decl;
2720 m_diag_decl = diag_decl;
2723 /* If an access failure was recorded, then attempt to locate an
2724 accessor function for the pertinent field.
2725 Otherwise, return NULL_TREE. */
2727 tree
2728 access_failure_info::get_any_accessor (bool const_p) const
2730 if (!was_inaccessible_p ())
2731 return NULL_TREE;
2733 tree accessor
2734 = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
2735 if (!accessor)
2736 return NULL_TREE;
2738 /* The accessor must itself be accessible for it to be a reasonable
2739 suggestion. */
2740 if (!accessible_p (m_basetype_path, accessor, true))
2741 return NULL_TREE;
2743 return accessor;
2746 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
2747 replacing the primary location in RICHLOC with "accessor()". */
2749 void
2750 access_failure_info::add_fixit_hint (rich_location *richloc,
2751 tree accessor_decl)
2753 pretty_printer pp;
2754 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
2755 richloc->add_fixit_replace (pp_formatted_text (&pp));
2758 /* If an access failure was recorded, then attempt to locate an
2759 accessor function for the pertinent field, and if one is
2760 available, add a note and fix-it hint suggesting using it. */
2762 void
2763 access_failure_info::maybe_suggest_accessor (bool const_p) const
2765 tree accessor = get_any_accessor (const_p);
2766 if (accessor == NULL_TREE)
2767 return;
2768 rich_location richloc (line_table, input_location);
2769 add_fixit_hint (&richloc, accessor);
2770 inform (&richloc, "field %q#D can be accessed via %q#D",
2771 m_diag_decl, accessor);
2774 /* Subroutine of finish_class_member_access_expr.
2775 Issue an error about NAME not being a member of ACCESS_PATH (or
2776 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
2777 names. */
2779 static void
2780 complain_about_unrecognized_member (tree access_path, tree name,
2781 tree object_type)
2783 /* Attempt to provide a hint about misspelled names. */
2784 tree guessed_id = lookup_member_fuzzy (access_path, name,
2785 /*want_type=*/false);
2786 if (guessed_id == NULL_TREE)
2788 /* No hint. */
2789 error ("%q#T has no member named %qE",
2790 TREE_CODE (access_path) == TREE_BINFO
2791 ? TREE_TYPE (access_path) : object_type, name);
2792 return;
2795 location_t bogus_component_loc = input_location;
2796 gcc_rich_location rich_loc (bogus_component_loc);
2798 /* Check that the guessed name is accessible along access_path. */
2799 access_failure_info afi;
2800 lookup_member (access_path, guessed_id, /*protect=*/1,
2801 /*want_type=*/false, /*complain=*/false,
2802 &afi);
2803 if (afi.was_inaccessible_p ())
2805 tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
2806 if (accessor)
2808 /* The guessed name isn't directly accessible, but can be accessed
2809 via an accessor member function. */
2810 afi.add_fixit_hint (&rich_loc, accessor);
2811 error_at (&rich_loc,
2812 "%q#T has no member named %qE;"
2813 " did you mean %q#D? (accessible via %q#D)",
2814 TREE_CODE (access_path) == TREE_BINFO
2815 ? TREE_TYPE (access_path) : object_type,
2816 name, afi.get_diag_decl (), accessor);
2818 else
2820 /* The guessed name isn't directly accessible, and no accessor
2821 member function could be found. */
2822 error_at (&rich_loc,
2823 "%q#T has no member named %qE;"
2824 " did you mean %q#D? (not accessible from this context)",
2825 TREE_CODE (access_path) == TREE_BINFO
2826 ? TREE_TYPE (access_path) : object_type,
2827 name, afi.get_diag_decl ());
2828 complain_about_access (afi.get_decl (), afi.get_diag_decl (), false);
2831 else
2833 /* The guessed name is directly accessible; suggest it. */
2834 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2835 guessed_id);
2836 error_at (&rich_loc,
2837 "%q#T has no member named %qE;"
2838 " did you mean %qE?",
2839 TREE_CODE (access_path) == TREE_BINFO
2840 ? TREE_TYPE (access_path) : object_type,
2841 name, guessed_id);
2845 /* This function is called by the parser to process a class member
2846 access expression of the form OBJECT.NAME. NAME is a node used by
2847 the parser to represent a name; it is not yet a DECL. It may,
2848 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2849 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2850 there is no reason to do the lookup twice, so the parser keeps the
2851 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2852 be a template via the use of the "A::template B" syntax. */
2854 tree
2855 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2856 tsubst_flags_t complain)
2858 tree expr;
2859 tree object_type;
2860 tree member;
2861 tree access_path = NULL_TREE;
2862 tree orig_object = object;
2863 tree orig_name = name;
2865 if (object == error_mark_node || name == error_mark_node)
2866 return error_mark_node;
2868 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2869 if (!objc_is_public (object, name))
2870 return error_mark_node;
2872 object_type = TREE_TYPE (object);
2874 if (processing_template_decl)
2876 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2877 type_dependent_object_expression_p (object)
2878 /* If NAME is "f<args>", where either 'f' or 'args' is
2879 dependent, then the expression is dependent. */
2880 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2881 && dependent_template_id_p (TREE_OPERAND (name, 0),
2882 TREE_OPERAND (name, 1)))
2883 /* If NAME is "T::X" where "T" is dependent, then the
2884 expression is dependent. */
2885 || (TREE_CODE (name) == SCOPE_REF
2886 && TYPE_P (TREE_OPERAND (name, 0))
2887 && dependent_scope_p (TREE_OPERAND (name, 0)))
2888 /* If NAME is operator T where "T" is dependent, we can't
2889 lookup until we instantiate the T. */
2890 || (TREE_CODE (name) == IDENTIFIER_NODE
2891 && IDENTIFIER_CONV_OP_P (name)
2892 && dependent_type_p (TREE_TYPE (name))))
2894 dependent:
2895 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2896 orig_object, orig_name, NULL_TREE);
2898 object = build_non_dependent_expr (object);
2900 else if (c_dialect_objc ()
2901 && identifier_p (name)
2902 && (expr = objc_maybe_build_component_ref (object, name)))
2903 return expr;
2905 /* [expr.ref]
2907 The type of the first expression shall be "class object" (of a
2908 complete type). */
2909 if (!currently_open_class (object_type)
2910 && !complete_type_or_maybe_complain (object_type, object, complain))
2911 return error_mark_node;
2912 if (!CLASS_TYPE_P (object_type))
2914 if (complain & tf_error)
2916 if (INDIRECT_TYPE_P (object_type)
2917 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2918 error ("request for member %qD in %qE, which is of pointer "
2919 "type %qT (maybe you meant to use %<->%> ?)",
2920 name, object.get_value (), object_type);
2921 else
2922 error ("request for member %qD in %qE, which is of non-class "
2923 "type %qT", name, object.get_value (), object_type);
2925 return error_mark_node;
2928 if (BASELINK_P (name))
2929 /* A member function that has already been looked up. */
2930 member = name;
2931 else
2933 bool is_template_id = false;
2934 tree template_args = NULL_TREE;
2935 tree scope = NULL_TREE;
2937 access_path = object_type;
2939 if (TREE_CODE (name) == SCOPE_REF)
2941 /* A qualified name. The qualifying class or namespace `S'
2942 has already been looked up; it is either a TYPE or a
2943 NAMESPACE_DECL. */
2944 scope = TREE_OPERAND (name, 0);
2945 name = TREE_OPERAND (name, 1);
2947 /* If SCOPE is a namespace, then the qualified name does not
2948 name a member of OBJECT_TYPE. */
2949 if (TREE_CODE (scope) == NAMESPACE_DECL)
2951 if (complain & tf_error)
2952 error ("%<%D::%D%> is not a member of %qT",
2953 scope, name, object_type);
2954 return error_mark_node;
2958 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2960 is_template_id = true;
2961 template_args = TREE_OPERAND (name, 1);
2962 name = TREE_OPERAND (name, 0);
2964 if (!identifier_p (name))
2965 name = OVL_NAME (name);
2968 if (scope)
2970 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2972 gcc_assert (!is_template_id);
2973 /* Looking up a member enumerator (c++/56793). */
2974 if (!TYPE_CLASS_SCOPE_P (scope)
2975 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2977 if (complain & tf_error)
2978 error ("%<%D::%D%> is not a member of %qT",
2979 scope, name, object_type);
2980 return error_mark_node;
2982 tree val = lookup_enumerator (scope, name);
2983 if (!val)
2985 if (complain & tf_error)
2986 error ("%qD is not a member of %qD",
2987 name, scope);
2988 return error_mark_node;
2991 if (TREE_SIDE_EFFECTS (object))
2992 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2993 return val;
2996 gcc_assert (CLASS_TYPE_P (scope));
2997 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2999 if (constructor_name_p (name, scope))
3001 if (complain & tf_error)
3002 error ("cannot call constructor %<%T::%D%> directly",
3003 scope, name);
3004 return error_mark_node;
3007 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3008 access_path = lookup_base (object_type, scope, ba_check,
3009 NULL, complain);
3010 if (access_path == error_mark_node)
3011 return error_mark_node;
3012 if (!access_path)
3014 if (any_dependent_bases_p (object_type))
3015 goto dependent;
3016 if (complain & tf_error)
3017 error ("%qT is not a base of %qT", scope, object_type);
3018 return error_mark_node;
3022 if (TREE_CODE (name) == BIT_NOT_EXPR)
3024 if (dependent_type_p (object_type))
3025 /* The destructor isn't declared yet. */
3026 goto dependent;
3027 member = lookup_destructor (object, scope, name, complain);
3029 else
3031 /* Look up the member. */
3032 access_failure_info afi;
3033 member = lookup_member (access_path, name, /*protect=*/1,
3034 /*want_type=*/false, complain,
3035 &afi);
3036 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3037 if (member == NULL_TREE)
3039 if (dependent_type_p (object_type))
3040 /* Try again at instantiation time. */
3041 goto dependent;
3042 if (complain & tf_error)
3043 complain_about_unrecognized_member (access_path, name,
3044 object_type);
3045 return error_mark_node;
3047 if (member == error_mark_node)
3048 return error_mark_node;
3049 if (DECL_P (member)
3050 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3051 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3052 wrong, so don't use it. */
3053 goto dependent;
3054 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3055 goto dependent;
3058 if (is_template_id)
3060 tree templ = member;
3062 if (BASELINK_P (templ))
3063 member = lookup_template_function (templ, template_args);
3064 else if (variable_template_p (templ))
3065 member = (lookup_and_finish_template_variable
3066 (templ, template_args, complain));
3067 else
3069 if (complain & tf_error)
3070 error ("%qD is not a member template function", name);
3071 return error_mark_node;
3076 if (TREE_DEPRECATED (member))
3077 warn_deprecated_use (member, NULL_TREE);
3079 if (template_p)
3080 check_template_keyword (member);
3082 expr = build_class_member_access_expr (object, member, access_path,
3083 /*preserve_reference=*/false,
3084 complain);
3085 if (processing_template_decl && expr != error_mark_node)
3087 if (BASELINK_P (member))
3089 if (TREE_CODE (orig_name) == SCOPE_REF)
3090 BASELINK_QUALIFIED_P (member) = 1;
3091 orig_name = member;
3093 return build_min_non_dep (COMPONENT_REF, expr,
3094 orig_object, orig_name,
3095 NULL_TREE);
3098 return expr;
3101 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3102 type. */
3104 tree
3105 build_simple_component_ref (tree object, tree member)
3107 tree type = cp_build_qualified_type (TREE_TYPE (member),
3108 cp_type_quals (TREE_TYPE (object)));
3109 return build3_loc (input_location,
3110 COMPONENT_REF, type,
3111 object, member, NULL_TREE);
3114 /* Return an expression for the MEMBER_NAME field in the internal
3115 representation of PTRMEM, a pointer-to-member function. (Each
3116 pointer-to-member function type gets its own RECORD_TYPE so it is
3117 more convenient to access the fields by name than by FIELD_DECL.)
3118 This routine converts the NAME to a FIELD_DECL and then creates the
3119 node for the complete expression. */
3121 tree
3122 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3124 tree ptrmem_type;
3125 tree member;
3127 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3129 unsigned int ix;
3130 tree index, value;
3131 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem),
3132 ix, index, value)
3133 if (index && DECL_P (index) && DECL_NAME (index) == member_name)
3134 return value;
3135 gcc_unreachable ();
3138 /* This code is a stripped down version of
3139 build_class_member_access_expr. It does not work to use that
3140 routine directly because it expects the object to be of class
3141 type. */
3142 ptrmem_type = TREE_TYPE (ptrmem);
3143 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3144 for (member = TYPE_FIELDS (ptrmem_type); member;
3145 member = DECL_CHAIN (member))
3146 if (DECL_NAME (member) == member_name)
3147 break;
3148 tree res = build_simple_component_ref (ptrmem, member);
3150 TREE_NO_WARNING (res) = 1;
3151 return res;
3154 /* Given an expression PTR for a pointer, return an expression
3155 for the value pointed to.
3156 ERRORSTRING is the name of the operator to appear in error messages.
3158 This function may need to overload OPERATOR_FNNAME.
3159 Must also handle REFERENCE_TYPEs for C++. */
3161 tree
3162 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3163 tsubst_flags_t complain)
3165 tree orig_expr = expr;
3166 tree rval;
3167 tree overload = NULL_TREE;
3169 if (processing_template_decl)
3171 /* Retain the type if we know the operand is a pointer. */
3172 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3173 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3174 if (type_dependent_expression_p (expr))
3175 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3176 expr = build_non_dependent_expr (expr);
3179 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3180 NULL_TREE, NULL_TREE, &overload, complain);
3181 if (!rval)
3182 rval = cp_build_indirect_ref (expr, errorstring, complain);
3184 if (processing_template_decl && rval != error_mark_node)
3186 if (overload != NULL_TREE)
3187 return (build_min_non_dep_op_overload
3188 (INDIRECT_REF, rval, overload, orig_expr));
3190 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3192 else
3193 return rval;
3196 /* The implementation of the above, and of indirection implied by other
3197 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3199 static tree
3200 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3201 tsubst_flags_t complain, bool do_fold)
3203 tree pointer, type;
3205 /* RO_NULL should only be used with the folding entry points below, not
3206 cp_build_indirect_ref. */
3207 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3209 if (ptr == current_class_ptr
3210 || (TREE_CODE (ptr) == NOP_EXPR
3211 && TREE_OPERAND (ptr, 0) == current_class_ptr
3212 && (same_type_ignoring_top_level_qualifiers_p
3213 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3214 return current_class_ref;
3216 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3217 ? ptr : decay_conversion (ptr, complain));
3218 if (pointer == error_mark_node)
3219 return error_mark_node;
3221 type = TREE_TYPE (pointer);
3223 if (INDIRECT_TYPE_P (type))
3225 /* [expr.unary.op]
3227 If the type of the expression is "pointer to T," the type
3228 of the result is "T." */
3229 tree t = TREE_TYPE (type);
3231 if ((CONVERT_EXPR_P (ptr)
3232 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3233 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3235 /* If a warning is issued, mark it to avoid duplicates from
3236 the backend. This only needs to be done at
3237 warn_strict_aliasing > 2. */
3238 if (warn_strict_aliasing > 2)
3239 if (strict_aliasing_warning (EXPR_LOCATION (ptr),
3240 type, TREE_OPERAND (ptr, 0)))
3241 TREE_NO_WARNING (ptr) = 1;
3244 if (VOID_TYPE_P (t))
3246 /* A pointer to incomplete type (other than cv void) can be
3247 dereferenced [expr.unary.op]/1 */
3248 if (complain & tf_error)
3249 error ("%qT is not a pointer-to-object type", type);
3250 return error_mark_node;
3252 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3253 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3254 /* The POINTER was something like `&x'. We simplify `*&x' to
3255 `x'. */
3256 return TREE_OPERAND (pointer, 0);
3257 else
3259 tree ref = build1 (INDIRECT_REF, t, pointer);
3261 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3262 so that we get the proper error message if the result is used
3263 to assign to. Also, &* is supposed to be a no-op. */
3264 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3265 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3266 TREE_SIDE_EFFECTS (ref)
3267 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3268 return ref;
3271 else if (!(complain & tf_error))
3272 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3274 /* `pointer' won't be an error_mark_node if we were given a
3275 pointer to member, so it's cool to check for this here. */
3276 else if (TYPE_PTRMEM_P (type))
3277 switch (errorstring)
3279 case RO_ARRAY_INDEXING:
3280 error ("invalid use of array indexing on pointer to member");
3281 break;
3282 case RO_UNARY_STAR:
3283 error ("invalid use of unary %<*%> on pointer to member");
3284 break;
3285 case RO_IMPLICIT_CONVERSION:
3286 error ("invalid use of implicit conversion on pointer to member");
3287 break;
3288 case RO_ARROW_STAR:
3289 error ("left hand operand of %<->*%> must be a pointer to class, "
3290 "but is a pointer to member of type %qT", type);
3291 break;
3292 default:
3293 gcc_unreachable ();
3295 else if (pointer != error_mark_node)
3296 invalid_indirection_error (input_location, type, errorstring);
3298 return error_mark_node;
3301 /* Entry point used by c-common, which expects folding. */
3303 tree
3304 build_indirect_ref (location_t /*loc*/,
3305 tree ptr, ref_operator errorstring)
3307 return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3310 /* Entry point used by internal indirection needs that don't correspond to any
3311 syntactic construct. */
3313 tree
3314 cp_build_fold_indirect_ref (tree pointer)
3316 return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3319 /* Entry point used by indirection needs that correspond to some syntactic
3320 construct. */
3322 tree
3323 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3324 tsubst_flags_t complain)
3326 return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3329 /* This handles expressions of the form "a[i]", which denotes
3330 an array reference.
3332 This is logically equivalent in C to *(a+i), but we may do it differently.
3333 If A is a variable or a member, we generate a primitive ARRAY_REF.
3334 This avoids forcing the array out of registers, and can work on
3335 arrays that are not lvalues (for example, members of structures returned
3336 by functions).
3338 If INDEX is of some user-defined type, it must be converted to
3339 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3340 will inherit the type of the array, which will be some pointer type.
3342 LOC is the location to use in building the array reference. */
3344 tree
3345 cp_build_array_ref (location_t loc, tree array, tree idx,
3346 tsubst_flags_t complain)
3348 tree ret;
3350 if (idx == 0)
3352 if (complain & tf_error)
3353 error_at (loc, "subscript missing in array reference");
3354 return error_mark_node;
3357 if (TREE_TYPE (array) == error_mark_node
3358 || TREE_TYPE (idx) == error_mark_node)
3359 return error_mark_node;
3361 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3362 inside it. */
3363 switch (TREE_CODE (array))
3365 case COMPOUND_EXPR:
3367 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3368 complain);
3369 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3370 TREE_OPERAND (array, 0), value);
3371 SET_EXPR_LOCATION (ret, loc);
3372 return ret;
3375 case COND_EXPR:
3376 ret = build_conditional_expr
3377 (loc, TREE_OPERAND (array, 0),
3378 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3379 complain),
3380 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3381 complain),
3382 complain);
3383 protected_set_expr_location (ret, loc);
3384 return ret;
3386 default:
3387 break;
3390 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3392 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3394 tree rval, type;
3396 warn_array_subscript_with_type_char (loc, idx);
3398 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3400 if (complain & tf_error)
3401 error_at (loc, "array subscript is not an integer");
3402 return error_mark_node;
3405 /* Apply integral promotions *after* noticing character types.
3406 (It is unclear why we do these promotions -- the standard
3407 does not say that we should. In fact, the natural thing would
3408 seem to be to convert IDX to ptrdiff_t; we're performing
3409 pointer arithmetic.) */
3410 idx = cp_perform_integral_promotions (idx, complain);
3412 /* An array that is indexed by a non-constant
3413 cannot be stored in a register; we must be able to do
3414 address arithmetic on its address.
3415 Likewise an array of elements of variable size. */
3416 if (TREE_CODE (idx) != INTEGER_CST
3417 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3418 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3419 != INTEGER_CST)))
3421 if (!cxx_mark_addressable (array, true))
3422 return error_mark_node;
3425 /* An array that is indexed by a constant value which is not within
3426 the array bounds cannot be stored in a register either; because we
3427 would get a crash in store_bit_field/extract_bit_field when trying
3428 to access a non-existent part of the register. */
3429 if (TREE_CODE (idx) == INTEGER_CST
3430 && TYPE_DOMAIN (TREE_TYPE (array))
3431 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3433 if (!cxx_mark_addressable (array))
3434 return error_mark_node;
3437 /* Note in C++ it is valid to subscript a `register' array, since
3438 it is valid to take the address of something with that
3439 storage specification. */
3440 if (extra_warnings)
3442 tree foo = array;
3443 while (TREE_CODE (foo) == COMPONENT_REF)
3444 foo = TREE_OPERAND (foo, 0);
3445 if (VAR_P (foo) && DECL_REGISTER (foo)
3446 && (complain & tf_warning))
3447 warning_at (loc, OPT_Wextra,
3448 "subscripting array declared %<register%>");
3451 type = TREE_TYPE (TREE_TYPE (array));
3452 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3453 /* Array ref is const/volatile if the array elements are
3454 or if the array is.. */
3455 TREE_READONLY (rval)
3456 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3457 TREE_SIDE_EFFECTS (rval)
3458 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3459 TREE_THIS_VOLATILE (rval)
3460 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3461 ret = require_complete_type_sfinae (rval, complain);
3462 protected_set_expr_location (ret, loc);
3463 if (non_lvalue)
3464 ret = non_lvalue_loc (loc, ret);
3465 return ret;
3469 tree ar = cp_default_conversion (array, complain);
3470 tree ind = cp_default_conversion (idx, complain);
3472 /* Put the integer in IND to simplify error checking. */
3473 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3474 std::swap (ar, ind);
3476 if (ar == error_mark_node || ind == error_mark_node)
3477 return error_mark_node;
3479 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3481 if (complain & tf_error)
3482 error_at (loc, "subscripted value is neither array nor pointer");
3483 return error_mark_node;
3485 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3487 if (complain & tf_error)
3488 error_at (loc, "array subscript is not an integer");
3489 return error_mark_node;
3492 warn_array_subscript_with_type_char (loc, idx);
3494 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3495 PLUS_EXPR, ar, ind,
3496 complain),
3497 RO_ARRAY_INDEXING,
3498 complain);
3499 protected_set_expr_location (ret, loc);
3500 if (non_lvalue)
3501 ret = non_lvalue_loc (loc, ret);
3502 return ret;
3506 /* Entry point for Obj-C++. */
3508 tree
3509 build_array_ref (location_t loc, tree array, tree idx)
3511 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3514 /* Resolve a pointer to member function. INSTANCE is the object
3515 instance to use, if the member points to a virtual member.
3517 This used to avoid checking for virtual functions if basetype
3518 has no virtual functions, according to an earlier ANSI draft.
3519 With the final ISO C++ rules, such an optimization is
3520 incorrect: A pointer to a derived member can be static_cast
3521 to pointer-to-base-member, as long as the dynamic object
3522 later has the right member. So now we only do this optimization
3523 when we know the dynamic type of the object. */
3525 tree
3526 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3527 tsubst_flags_t complain)
3529 if (TREE_CODE (function) == OFFSET_REF)
3530 function = TREE_OPERAND (function, 1);
3532 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3534 tree idx, delta, e1, e2, e3, vtbl;
3535 bool nonvirtual;
3536 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3537 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3539 tree instance_ptr = *instance_ptrptr;
3540 tree instance_save_expr = 0;
3541 if (instance_ptr == error_mark_node)
3543 if (TREE_CODE (function) == PTRMEM_CST)
3545 /* Extracting the function address from a pmf is only
3546 allowed with -Wno-pmf-conversions. It only works for
3547 pmf constants. */
3548 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3549 e1 = convert (fntype, e1);
3550 return e1;
3552 else
3554 if (complain & tf_error)
3555 error ("object missing in use of %qE", function);
3556 return error_mark_node;
3560 /* True if we know that the dynamic type of the object doesn't have
3561 virtual functions, so we can assume the PFN field is a pointer. */
3562 nonvirtual = (COMPLETE_TYPE_P (basetype)
3563 && !TYPE_POLYMORPHIC_P (basetype)
3564 && resolves_to_fixed_type_p (instance_ptr, 0));
3566 /* If we don't really have an object (i.e. in an ill-formed
3567 conversion from PMF to pointer), we can't resolve virtual
3568 functions anyway. */
3569 if (!nonvirtual && is_dummy_object (instance_ptr))
3570 nonvirtual = true;
3572 if (TREE_SIDE_EFFECTS (instance_ptr))
3573 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3575 if (TREE_SIDE_EFFECTS (function))
3576 function = save_expr (function);
3578 /* Start by extracting all the information from the PMF itself. */
3579 e3 = pfn_from_ptrmemfunc (function);
3580 delta = delta_from_ptrmemfunc (function);
3581 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3582 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3584 int flag_sanitize_save;
3585 case ptrmemfunc_vbit_in_pfn:
3586 e1 = cp_build_binary_op (input_location,
3587 BIT_AND_EXPR, idx, integer_one_node,
3588 complain);
3589 idx = cp_build_binary_op (input_location,
3590 MINUS_EXPR, idx, integer_one_node,
3591 complain);
3592 if (idx == error_mark_node)
3593 return error_mark_node;
3594 break;
3596 case ptrmemfunc_vbit_in_delta:
3597 e1 = cp_build_binary_op (input_location,
3598 BIT_AND_EXPR, delta, integer_one_node,
3599 complain);
3600 /* Don't instrument the RSHIFT_EXPR we're about to create because
3601 we're going to use DELTA number of times, and that wouldn't play
3602 well with SAVE_EXPRs therein. */
3603 flag_sanitize_save = flag_sanitize;
3604 flag_sanitize = 0;
3605 delta = cp_build_binary_op (input_location,
3606 RSHIFT_EXPR, delta, integer_one_node,
3607 complain);
3608 flag_sanitize = flag_sanitize_save;
3609 if (delta == error_mark_node)
3610 return error_mark_node;
3611 break;
3613 default:
3614 gcc_unreachable ();
3617 if (e1 == error_mark_node)
3618 return error_mark_node;
3620 /* Convert down to the right base before using the instance. A
3621 special case is that in a pointer to member of class C, C may
3622 be incomplete. In that case, the function will of course be
3623 a member of C, and no conversion is required. In fact,
3624 lookup_base will fail in that case, because incomplete
3625 classes do not have BINFOs. */
3626 if (!same_type_ignoring_top_level_qualifiers_p
3627 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3629 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3630 basetype, ba_check, NULL, complain);
3631 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3632 1, complain);
3633 if (instance_ptr == error_mark_node)
3634 return error_mark_node;
3636 /* ...and then the delta in the PMF. */
3637 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3639 /* Hand back the adjusted 'this' argument to our caller. */
3640 *instance_ptrptr = instance_ptr;
3642 if (nonvirtual)
3643 /* Now just return the pointer. */
3644 return e3;
3646 /* Next extract the vtable pointer from the object. */
3647 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3648 instance_ptr);
3649 vtbl = cp_build_fold_indirect_ref (vtbl);
3650 if (vtbl == error_mark_node)
3651 return error_mark_node;
3653 /* Finally, extract the function pointer from the vtable. */
3654 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3655 e2 = cp_build_fold_indirect_ref (e2);
3656 if (e2 == error_mark_node)
3657 return error_mark_node;
3658 TREE_CONSTANT (e2) = 1;
3660 /* When using function descriptors, the address of the
3661 vtable entry is treated as a function pointer. */
3662 if (TARGET_VTABLE_USES_DESCRIPTORS)
3663 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3664 cp_build_addr_expr (e2, complain));
3666 e2 = fold_convert (TREE_TYPE (e3), e2);
3667 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3668 if (e1 == error_mark_node)
3669 return error_mark_node;
3671 /* Make sure this doesn't get evaluated first inside one of the
3672 branches of the COND_EXPR. */
3673 if (instance_save_expr)
3674 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3675 instance_save_expr, e1);
3677 function = e1;
3679 return function;
3682 /* Used by the C-common bits. */
3683 tree
3684 build_function_call (location_t /*loc*/,
3685 tree function, tree params)
3687 return cp_build_function_call (function, params, tf_warning_or_error);
3690 /* Used by the C-common bits. */
3691 tree
3692 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3693 tree function, vec<tree, va_gc> *params,
3694 vec<tree, va_gc> * /*origtypes*/)
3696 vec<tree, va_gc> *orig_params = params;
3697 tree ret = cp_build_function_call_vec (function, &params,
3698 tf_warning_or_error);
3700 /* cp_build_function_call_vec can reallocate PARAMS by adding
3701 default arguments. That should never happen here. Verify
3702 that. */
3703 gcc_assert (params == orig_params);
3705 return ret;
3708 /* Build a function call using a tree list of arguments. */
3710 static tree
3711 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3713 vec<tree, va_gc> *vec;
3714 tree ret;
3716 vec = make_tree_vector ();
3717 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3718 vec_safe_push (vec, TREE_VALUE (params));
3719 ret = cp_build_function_call_vec (function, &vec, complain);
3720 release_tree_vector (vec);
3721 return ret;
3724 /* Build a function call using varargs. */
3726 tree
3727 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3729 vec<tree, va_gc> *vec;
3730 va_list args;
3731 tree ret, t;
3733 vec = make_tree_vector ();
3734 va_start (args, complain);
3735 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3736 vec_safe_push (vec, t);
3737 va_end (args);
3738 ret = cp_build_function_call_vec (function, &vec, complain);
3739 release_tree_vector (vec);
3740 return ret;
3743 /* Build a function call using a vector of arguments. PARAMS may be
3744 NULL if there are no parameters. This changes the contents of
3745 PARAMS. */
3747 tree
3748 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3749 tsubst_flags_t complain)
3751 tree fntype, fndecl;
3752 int is_method;
3753 tree original = function;
3754 int nargs;
3755 tree *argarray;
3756 tree parm_types;
3757 vec<tree, va_gc> *allocated = NULL;
3758 tree ret;
3760 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3761 expressions, like those used for ObjC messenger dispatches. */
3762 if (params != NULL && !vec_safe_is_empty (*params))
3763 function = objc_rewrite_function_call (function, (**params)[0]);
3765 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3766 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3767 if (TREE_CODE (function) == NOP_EXPR
3768 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3769 function = TREE_OPERAND (function, 0);
3771 if (TREE_CODE (function) == FUNCTION_DECL)
3773 /* If the function is a non-template member function
3774 or a non-template friend, then we need to check the
3775 constraints.
3777 Note that if overload resolution failed with a single
3778 candidate this function will be used to explicitly diagnose
3779 the failure for the single call expression. The check is
3780 technically redundant since we also would have failed in
3781 add_function_candidate. */
3782 if (flag_concepts
3783 && (complain & tf_error)
3784 && !constraints_satisfied_p (function))
3786 auto_diagnostic_group d;
3787 error ("cannot call function %qD", function);
3788 location_t loc = DECL_SOURCE_LOCATION (function);
3789 diagnose_constraints (loc, function, NULL_TREE);
3790 return error_mark_node;
3793 if (!mark_used (function, complain) && !(complain & tf_error))
3794 return error_mark_node;
3795 fndecl = function;
3797 /* Convert anything with function type to a pointer-to-function. */
3798 if (DECL_MAIN_P (function))
3800 if (complain & tf_error)
3801 pedwarn (input_location, OPT_Wpedantic,
3802 "ISO C++ forbids calling %<::main%> from within program");
3803 else
3804 return error_mark_node;
3806 function = build_addr_func (function, complain);
3808 else
3810 fndecl = NULL_TREE;
3812 function = build_addr_func (function, complain);
3815 if (function == error_mark_node)
3816 return error_mark_node;
3818 fntype = TREE_TYPE (function);
3820 if (TYPE_PTRMEMFUNC_P (fntype))
3822 if (complain & tf_error)
3823 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3824 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3825 original, original);
3826 return error_mark_node;
3829 is_method = (TYPE_PTR_P (fntype)
3830 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3832 if (!(TYPE_PTRFN_P (fntype)
3833 || is_method
3834 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3836 if (complain & tf_error)
3838 if (!flag_diagnostics_show_caret)
3839 error_at (input_location,
3840 "%qE cannot be used as a function", original);
3841 else if (DECL_P (original))
3842 error_at (input_location,
3843 "%qD cannot be used as a function", original);
3844 else
3845 error_at (input_location,
3846 "expression cannot be used as a function");
3849 return error_mark_node;
3852 /* fntype now gets the type of function pointed to. */
3853 fntype = TREE_TYPE (fntype);
3854 parm_types = TYPE_ARG_TYPES (fntype);
3856 if (params == NULL)
3858 allocated = make_tree_vector ();
3859 params = &allocated;
3862 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3863 complain);
3864 if (nargs < 0)
3865 return error_mark_node;
3867 argarray = (*params)->address ();
3869 /* Check for errors in format strings and inappropriately
3870 null parameters. */
3871 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3872 nargs, argarray, NULL);
3874 ret = build_cxx_call (function, nargs, argarray, complain);
3876 if (warned_p)
3878 tree c = extract_call_expr (ret);
3879 if (TREE_CODE (c) == CALL_EXPR)
3880 TREE_NO_WARNING (c) = 1;
3883 if (allocated != NULL)
3884 release_tree_vector (allocated);
3886 return ret;
3889 /* Subroutine of convert_arguments.
3890 Print an error message about a wrong number of arguments. */
3892 static void
3893 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3895 if (fndecl)
3897 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3899 if (DECL_NAME (fndecl) == NULL_TREE
3900 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3901 error_at (loc,
3902 too_many_p
3903 ? G_("too many arguments to constructor %q#D")
3904 : G_("too few arguments to constructor %q#D"),
3905 fndecl);
3906 else
3907 error_at (loc,
3908 too_many_p
3909 ? G_("too many arguments to member function %q#D")
3910 : G_("too few arguments to member function %q#D"),
3911 fndecl);
3913 else
3914 error_at (loc,
3915 too_many_p
3916 ? G_("too many arguments to function %q#D")
3917 : G_("too few arguments to function %q#D"),
3918 fndecl);
3919 if (!DECL_IS_BUILTIN (fndecl))
3920 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3922 else
3924 if (c_dialect_objc () && objc_message_selector ())
3925 error_at (loc,
3926 too_many_p
3927 ? G_("too many arguments to method %q#D")
3928 : G_("too few arguments to method %q#D"),
3929 objc_message_selector ());
3930 else
3931 error_at (loc, too_many_p ? G_("too many arguments to function")
3932 : G_("too few arguments to function"));
3936 /* Convert the actual parameter expressions in the list VALUES to the
3937 types in the list TYPELIST. The converted expressions are stored
3938 back in the VALUES vector.
3939 If parmdecls is exhausted, or when an element has NULL as its type,
3940 perform the default conversions.
3942 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3944 This is also where warnings about wrong number of args are generated.
3946 Returns the actual number of arguments processed (which might be less
3947 than the length of the vector), or -1 on error.
3949 In C++, unspecified trailing parameters can be filled in with their
3950 default arguments, if such were specified. Do so here. */
3952 static int
3953 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3954 int flags, tsubst_flags_t complain)
3956 tree typetail;
3957 unsigned int i;
3959 /* Argument passing is always copy-initialization. */
3960 flags |= LOOKUP_ONLYCONVERTING;
3962 for (i = 0, typetail = typelist;
3963 i < vec_safe_length (*values);
3964 i++)
3966 tree type = typetail ? TREE_VALUE (typetail) : 0;
3967 tree val = (**values)[i];
3969 if (val == error_mark_node || type == error_mark_node)
3970 return -1;
3972 if (type == void_type_node)
3974 if (complain & tf_error)
3976 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3977 return i;
3979 else
3980 return -1;
3983 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3984 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3985 if (TREE_CODE (val) == NOP_EXPR
3986 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3987 && (type == 0 || !TYPE_REF_P (type)))
3988 val = TREE_OPERAND (val, 0);
3990 if (type == 0 || !TYPE_REF_P (type))
3992 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3993 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3994 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3995 val = decay_conversion (val, complain);
3998 if (val == error_mark_node)
3999 return -1;
4001 if (type != 0)
4003 /* Formal parm type is specified by a function prototype. */
4004 tree parmval;
4006 if (!COMPLETE_TYPE_P (complete_type (type)))
4008 if (complain & tf_error)
4010 if (fndecl)
4011 error ("parameter %P of %qD has incomplete type %qT",
4012 i, fndecl, type);
4013 else
4014 error ("parameter %P has incomplete type %qT", i, type);
4016 parmval = error_mark_node;
4018 else
4020 parmval = convert_for_initialization
4021 (NULL_TREE, type, val, flags,
4022 ICR_ARGPASS, fndecl, i, complain);
4023 parmval = convert_for_arg_passing (type, parmval, complain);
4026 if (parmval == error_mark_node)
4027 return -1;
4029 (**values)[i] = parmval;
4031 else
4033 if (fndecl && magic_varargs_p (fndecl))
4034 /* Don't do ellipsis conversion for __built_in_constant_p
4035 as this will result in spurious errors for non-trivial
4036 types. */
4037 val = require_complete_type_sfinae (val, complain);
4038 else
4039 val = convert_arg_to_ellipsis (val, complain);
4041 (**values)[i] = val;
4044 if (typetail)
4045 typetail = TREE_CHAIN (typetail);
4048 if (typetail != 0 && typetail != void_list_node)
4050 /* See if there are default arguments that can be used. Because
4051 we hold default arguments in the FUNCTION_TYPE (which is so
4052 wrong), we can see default parameters here from deduced
4053 contexts (and via typeof) for indirect function calls.
4054 Fortunately we know whether we have a function decl to
4055 provide default arguments in a language conformant
4056 manner. */
4057 if (fndecl && TREE_PURPOSE (typetail)
4058 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
4060 for (; typetail != void_list_node; ++i)
4062 /* After DR777, with explicit template args we can end up with a
4063 default argument followed by no default argument. */
4064 if (!TREE_PURPOSE (typetail))
4065 break;
4066 tree parmval
4067 = convert_default_arg (TREE_VALUE (typetail),
4068 TREE_PURPOSE (typetail),
4069 fndecl, i, complain);
4071 if (parmval == error_mark_node)
4072 return -1;
4074 vec_safe_push (*values, parmval);
4075 typetail = TREE_CHAIN (typetail);
4076 /* ends with `...'. */
4077 if (typetail == NULL_TREE)
4078 break;
4082 if (typetail && typetail != void_list_node)
4084 if (complain & tf_error)
4085 error_args_num (input_location, fndecl, /*too_many_p=*/false);
4086 return -1;
4090 return (int) i;
4093 /* Build a binary-operation expression, after performing default
4094 conversions on the operands. CODE is the kind of expression to
4095 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4096 are the tree codes which correspond to ARG1 and ARG2 when issuing
4097 warnings about possibly misplaced parentheses. They may differ
4098 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4099 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4100 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4101 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4102 ARG2_CODE as ERROR_MARK. */
4104 tree
4105 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4106 enum tree_code arg1_code, tree arg2,
4107 enum tree_code arg2_code, tree *overload_p,
4108 tsubst_flags_t complain)
4110 tree orig_arg1;
4111 tree orig_arg2;
4112 tree expr;
4113 tree overload = NULL_TREE;
4115 orig_arg1 = arg1;
4116 orig_arg2 = arg2;
4118 if (processing_template_decl)
4120 if (type_dependent_expression_p (arg1)
4121 || type_dependent_expression_p (arg2))
4122 return build_min_nt_loc (loc, code, arg1, arg2);
4123 arg1 = build_non_dependent_expr (arg1);
4124 arg2 = build_non_dependent_expr (arg2);
4127 if (code == DOTSTAR_EXPR)
4128 expr = build_m_component_ref (arg1, arg2, complain);
4129 else
4130 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4131 &overload, complain);
4133 if (overload_p != NULL)
4134 *overload_p = overload;
4136 /* Check for cases such as x+y<<z which users are likely to
4137 misinterpret. But don't warn about obj << x + y, since that is a
4138 common idiom for I/O. */
4139 if (warn_parentheses
4140 && (complain & tf_warning)
4141 && !processing_template_decl
4142 && !error_operand_p (arg1)
4143 && !error_operand_p (arg2)
4144 && (code != LSHIFT_EXPR
4145 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4146 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4147 arg2_code, orig_arg2);
4149 if (processing_template_decl && expr != error_mark_node)
4151 if (overload != NULL_TREE)
4152 return (build_min_non_dep_op_overload
4153 (code, expr, overload, orig_arg1, orig_arg2));
4155 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4158 return expr;
4161 /* Build and return an ARRAY_REF expression. */
4163 tree
4164 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4165 tsubst_flags_t complain)
4167 tree orig_arg1 = arg1;
4168 tree orig_arg2 = arg2;
4169 tree expr;
4170 tree overload = NULL_TREE;
4172 if (processing_template_decl)
4174 if (type_dependent_expression_p (arg1)
4175 || type_dependent_expression_p (arg2))
4176 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4177 NULL_TREE, NULL_TREE);
4178 arg1 = build_non_dependent_expr (arg1);
4179 arg2 = build_non_dependent_expr (arg2);
4182 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4183 NULL_TREE, &overload, complain);
4185 if (processing_template_decl && expr != error_mark_node)
4187 if (overload != NULL_TREE)
4188 return (build_min_non_dep_op_overload
4189 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4191 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4192 NULL_TREE, NULL_TREE);
4194 return expr;
4197 /* Return whether OP is an expression of enum type cast to integer
4198 type. In C++ even unsigned enum types are cast to signed integer
4199 types. We do not want to issue warnings about comparisons between
4200 signed and unsigned types when one of the types is an enum type.
4201 Those warnings are always false positives in practice. */
4203 static bool
4204 enum_cast_to_int (tree op)
4206 if (CONVERT_EXPR_P (op)
4207 && TREE_TYPE (op) == integer_type_node
4208 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4209 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4210 return true;
4212 /* The cast may have been pushed into a COND_EXPR. */
4213 if (TREE_CODE (op) == COND_EXPR)
4214 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4215 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4217 return false;
4220 /* For the c-common bits. */
4221 tree
4222 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4223 bool /*convert_p*/)
4225 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4228 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4229 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4231 static tree
4232 build_vec_cmp (tree_code code, tree type,
4233 tree arg0, tree arg1)
4235 tree zero_vec = build_zero_cst (type);
4236 tree minus_one_vec = build_minus_one_cst (type);
4237 tree cmp_type = build_same_sized_truth_vector_type(type);
4238 tree cmp = build2 (code, cmp_type, arg0, arg1);
4239 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4242 /* Possibly warn about an address never being NULL. */
4244 static void
4245 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4247 if (!warn_address
4248 || (complain & tf_warning) == 0
4249 || c_inhibit_evaluation_warnings != 0
4250 || TREE_NO_WARNING (op))
4251 return;
4253 tree cop = fold_non_dependent_expr (op, complain);
4255 if (TREE_CODE (cop) == ADDR_EXPR
4256 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4257 && !TREE_NO_WARNING (cop))
4258 warning_at (location, OPT_Waddress, "the address of %qD will never "
4259 "be NULL", TREE_OPERAND (cop, 0));
4261 if (CONVERT_EXPR_P (op)
4262 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4264 tree inner_op = op;
4265 STRIP_NOPS (inner_op);
4267 if (DECL_P (inner_op))
4268 warning_at (location, OPT_Waddress,
4269 "the compiler can assume that the address of "
4270 "%qD will never be NULL", inner_op);
4274 /* Build a binary-operation expression without default conversions.
4275 CODE is the kind of expression to build.
4276 LOCATION is the location_t of the operator in the source code.
4277 This function differs from `build' in several ways:
4278 the data type of the result is computed and recorded in it,
4279 warnings are generated if arg data types are invalid,
4280 special handling for addition and subtraction of pointers is known,
4281 and some optimization is done (operations on narrow ints
4282 are done in the narrower type when that gives the same result).
4283 Constant folding is also done before the result is returned.
4285 Note that the operands will never have enumeral types
4286 because either they have just had the default conversions performed
4287 or they have both just been converted to some other type in which
4288 the arithmetic is to be done.
4290 C++: must do special pointer arithmetic when implementing
4291 multiple inheritance, and deal with pointer to member functions. */
4293 tree
4294 cp_build_binary_op (location_t location,
4295 enum tree_code code, tree orig_op0, tree orig_op1,
4296 tsubst_flags_t complain)
4298 tree op0, op1;
4299 enum tree_code code0, code1;
4300 tree type0, type1;
4301 const char *invalid_op_diag;
4303 /* Expression code to give to the expression when it is built.
4304 Normally this is CODE, which is what the caller asked for,
4305 but in some special cases we change it. */
4306 enum tree_code resultcode = code;
4308 /* Data type in which the computation is to be performed.
4309 In the simplest cases this is the common type of the arguments. */
4310 tree result_type = NULL_TREE;
4312 /* Nonzero means operands have already been type-converted
4313 in whatever way is necessary.
4314 Zero means they need to be converted to RESULT_TYPE. */
4315 int converted = 0;
4317 /* Nonzero means create the expression with this type, rather than
4318 RESULT_TYPE. */
4319 tree build_type = 0;
4321 /* Nonzero means after finally constructing the expression
4322 convert it to this type. */
4323 tree final_type = 0;
4325 tree result, result_ovl;
4327 /* Nonzero if this is an operation like MIN or MAX which can
4328 safely be computed in short if both args are promoted shorts.
4329 Also implies COMMON.
4330 -1 indicates a bitwise operation; this makes a difference
4331 in the exact conditions for when it is safe to do the operation
4332 in a narrower mode. */
4333 int shorten = 0;
4335 /* Nonzero if this is a comparison operation;
4336 if both args are promoted shorts, compare the original shorts.
4337 Also implies COMMON. */
4338 int short_compare = 0;
4340 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4341 int common = 0;
4343 /* True if both operands have arithmetic type. */
4344 bool arithmetic_types_p;
4346 /* Apply default conversions. */
4347 op0 = orig_op0;
4348 op1 = orig_op1;
4350 /* Remember whether we're doing / or %. */
4351 bool doing_div_or_mod = false;
4353 /* Remember whether we're doing << or >>. */
4354 bool doing_shift = false;
4356 /* Tree holding instrumentation expression. */
4357 tree instrument_expr = NULL_TREE;
4359 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4360 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4361 || code == TRUTH_XOR_EXPR)
4363 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4364 op0 = decay_conversion (op0, complain);
4365 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4366 op1 = decay_conversion (op1, complain);
4368 else
4370 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4371 op0 = cp_default_conversion (op0, complain);
4372 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4373 op1 = cp_default_conversion (op1, complain);
4376 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4377 STRIP_TYPE_NOPS (op0);
4378 STRIP_TYPE_NOPS (op1);
4380 /* DTRT if one side is an overloaded function, but complain about it. */
4381 if (type_unknown_p (op0))
4383 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4384 if (t != error_mark_node)
4386 if (complain & tf_error)
4387 permerror (input_location, "assuming cast to type %qT from overloaded function",
4388 TREE_TYPE (t));
4389 op0 = t;
4392 if (type_unknown_p (op1))
4394 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4395 if (t != error_mark_node)
4397 if (complain & tf_error)
4398 permerror (input_location, "assuming cast to type %qT from overloaded function",
4399 TREE_TYPE (t));
4400 op1 = t;
4404 type0 = TREE_TYPE (op0);
4405 type1 = TREE_TYPE (op1);
4407 /* The expression codes of the data types of the arguments tell us
4408 whether the arguments are integers, floating, pointers, etc. */
4409 code0 = TREE_CODE (type0);
4410 code1 = TREE_CODE (type1);
4412 /* If an error was already reported for one of the arguments,
4413 avoid reporting another error. */
4414 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4415 return error_mark_node;
4417 if ((invalid_op_diag
4418 = targetm.invalid_binary_op (code, type0, type1)))
4420 if (complain & tf_error)
4421 error (invalid_op_diag);
4422 return error_mark_node;
4425 /* Issue warnings about peculiar, but valid, uses of NULL. */
4426 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4427 /* It's reasonable to use pointer values as operands of &&
4428 and ||, so NULL is no exception. */
4429 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4430 && ( /* Both are NULL (or 0) and the operation was not a
4431 comparison or a pointer subtraction. */
4432 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4433 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4434 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4435 || (!null_ptr_cst_p (orig_op0)
4436 && !TYPE_PTR_OR_PTRMEM_P (type0))
4437 || (!null_ptr_cst_p (orig_op1)
4438 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4439 && (complain & tf_warning))
4441 location_t loc =
4442 expansion_point_location_if_in_system_header (input_location);
4444 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4447 /* In case when one of the operands of the binary operation is
4448 a vector and another is a scalar -- convert scalar to vector. */
4449 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4451 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4452 complain & tf_error);
4454 switch (convert_flag)
4456 case stv_error:
4457 return error_mark_node;
4458 case stv_firstarg:
4460 op0 = convert (TREE_TYPE (type1), op0);
4461 op0 = save_expr (op0);
4462 op0 = build_vector_from_val (type1, op0);
4463 type0 = TREE_TYPE (op0);
4464 code0 = TREE_CODE (type0);
4465 converted = 1;
4466 break;
4468 case stv_secondarg:
4470 op1 = convert (TREE_TYPE (type0), op1);
4471 op1 = save_expr (op1);
4472 op1 = build_vector_from_val (type0, op1);
4473 type1 = TREE_TYPE (op1);
4474 code1 = TREE_CODE (type1);
4475 converted = 1;
4476 break;
4478 default:
4479 break;
4483 switch (code)
4485 case MINUS_EXPR:
4486 /* Subtraction of two similar pointers.
4487 We must subtract them as integers, then divide by object size. */
4488 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4489 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4490 TREE_TYPE (type1)))
4492 result = pointer_diff (location, op0, op1,
4493 common_pointer_type (type0, type1), complain,
4494 &instrument_expr);
4495 if (instrument_expr != NULL)
4496 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4497 instrument_expr, result);
4499 return result;
4501 /* In all other cases except pointer - int, the usual arithmetic
4502 rules apply. */
4503 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4505 common = 1;
4506 break;
4508 /* The pointer - int case is just like pointer + int; fall
4509 through. */
4510 gcc_fallthrough ();
4511 case PLUS_EXPR:
4512 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4513 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4515 tree ptr_operand;
4516 tree int_operand;
4517 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4518 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4519 if (processing_template_decl)
4521 result_type = TREE_TYPE (ptr_operand);
4522 break;
4524 return cp_pointer_int_sum (location, code,
4525 ptr_operand,
4526 int_operand,
4527 complain);
4529 common = 1;
4530 break;
4532 case MULT_EXPR:
4533 common = 1;
4534 break;
4536 case TRUNC_DIV_EXPR:
4537 case CEIL_DIV_EXPR:
4538 case FLOOR_DIV_EXPR:
4539 case ROUND_DIV_EXPR:
4540 case EXACT_DIV_EXPR:
4541 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4543 tree type0 = TREE_OPERAND (op0, 0);
4544 tree type1 = TREE_OPERAND (op1, 0);
4545 tree first_arg = type0;
4546 if (!TYPE_P (type0))
4547 type0 = TREE_TYPE (type0);
4548 if (!TYPE_P (type1))
4549 type1 = TREE_TYPE (type1);
4550 if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4551 && !(TREE_CODE (first_arg) == PARM_DECL
4552 && DECL_ARRAY_PARAMETER_P (first_arg)
4553 && warn_sizeof_array_argument)
4554 && (complain & tf_warning))
4556 auto_diagnostic_group d;
4557 if (warning_at (location, OPT_Wsizeof_pointer_div,
4558 "division %<sizeof (%T) / sizeof (%T)%> does "
4559 "not compute the number of array elements",
4560 type0, type1))
4561 if (DECL_P (first_arg))
4562 inform (DECL_SOURCE_LOCATION (first_arg),
4563 "first %<sizeof%> operand was declared here");
4567 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4568 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4569 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4570 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4572 enum tree_code tcode0 = code0, tcode1 = code1;
4573 tree cop1 = fold_non_dependent_expr (op1, complain);
4574 doing_div_or_mod = true;
4575 warn_for_div_by_zero (location, cop1);
4577 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4578 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4579 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4580 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4582 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4583 resultcode = RDIV_EXPR;
4584 else
4585 /* When dividing two signed integers, we have to promote to int.
4586 unless we divide by a constant != -1. Note that default
4587 conversion will have been performed on the operands at this
4588 point, so we have to dig out the original type to find out if
4589 it was unsigned. */
4590 shorten = ((TREE_CODE (op0) == NOP_EXPR
4591 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4592 || (TREE_CODE (op1) == INTEGER_CST
4593 && ! integer_all_onesp (op1)));
4595 common = 1;
4597 break;
4599 case BIT_AND_EXPR:
4600 case BIT_IOR_EXPR:
4601 case BIT_XOR_EXPR:
4602 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4603 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4604 && !VECTOR_FLOAT_TYPE_P (type0)
4605 && !VECTOR_FLOAT_TYPE_P (type1)))
4606 shorten = -1;
4607 break;
4609 case TRUNC_MOD_EXPR:
4610 case FLOOR_MOD_EXPR:
4612 tree cop1 = fold_non_dependent_expr (op1, complain);
4613 doing_div_or_mod = true;
4614 warn_for_div_by_zero (location, cop1);
4617 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4618 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4619 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4620 common = 1;
4621 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4623 /* Although it would be tempting to shorten always here, that loses
4624 on some targets, since the modulo instruction is undefined if the
4625 quotient can't be represented in the computation mode. We shorten
4626 only if unsigned or if dividing by something we know != -1. */
4627 shorten = ((TREE_CODE (op0) == NOP_EXPR
4628 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4629 || (TREE_CODE (op1) == INTEGER_CST
4630 && ! integer_all_onesp (op1)));
4631 common = 1;
4633 break;
4635 case TRUTH_ANDIF_EXPR:
4636 case TRUTH_ORIF_EXPR:
4637 case TRUTH_AND_EXPR:
4638 case TRUTH_OR_EXPR:
4639 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4641 if (!COMPARISON_CLASS_P (op1))
4642 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4643 build_zero_cst (type1), complain);
4644 if (code == TRUTH_ANDIF_EXPR)
4646 tree z = build_zero_cst (TREE_TYPE (op1));
4647 return build_conditional_expr (location, op0, op1, z, complain);
4649 else if (code == TRUTH_ORIF_EXPR)
4651 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4652 return build_conditional_expr (location, op0, m1, op1, complain);
4654 else
4655 gcc_unreachable ();
4657 if (VECTOR_TYPE_P (type0))
4659 if (!COMPARISON_CLASS_P (op0))
4660 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4661 build_zero_cst (type0), complain);
4662 if (!VECTOR_TYPE_P (type1))
4664 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4665 tree z = build_zero_cst (TREE_TYPE (op0));
4666 op1 = build_conditional_expr (location, op1, m1, z, complain);
4668 else if (!COMPARISON_CLASS_P (op1))
4669 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4670 build_zero_cst (type1), complain);
4672 if (code == TRUTH_ANDIF_EXPR)
4673 code = BIT_AND_EXPR;
4674 else if (code == TRUTH_ORIF_EXPR)
4675 code = BIT_IOR_EXPR;
4676 else
4677 gcc_unreachable ();
4679 return cp_build_binary_op (location, code, op0, op1, complain);
4682 result_type = boolean_type_node;
4683 break;
4685 /* Shift operations: result has same type as first operand;
4686 always convert second operand to int.
4687 Also set SHORT_SHIFT if shifting rightward. */
4689 case RSHIFT_EXPR:
4690 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4691 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4693 result_type = type0;
4694 converted = 1;
4696 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4697 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4698 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4699 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4700 TYPE_VECTOR_SUBPARTS (type1)))
4702 result_type = type0;
4703 converted = 1;
4705 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4707 tree const_op1 = fold_non_dependent_expr (op1, complain);
4708 if (TREE_CODE (const_op1) != INTEGER_CST)
4709 const_op1 = op1;
4710 result_type = type0;
4711 doing_shift = true;
4712 if (TREE_CODE (const_op1) == INTEGER_CST)
4714 if (tree_int_cst_lt (const_op1, integer_zero_node))
4716 if ((complain & tf_warning)
4717 && c_inhibit_evaluation_warnings == 0)
4718 warning (OPT_Wshift_count_negative,
4719 "right shift count is negative");
4721 else
4723 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4724 && (complain & tf_warning)
4725 && c_inhibit_evaluation_warnings == 0)
4726 warning (OPT_Wshift_count_overflow,
4727 "right shift count >= width of type");
4730 /* Avoid converting op1 to result_type later. */
4731 converted = 1;
4733 break;
4735 case LSHIFT_EXPR:
4736 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4737 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4739 result_type = type0;
4740 converted = 1;
4742 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4743 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4744 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4745 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4746 TYPE_VECTOR_SUBPARTS (type1)))
4748 result_type = type0;
4749 converted = 1;
4751 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4753 tree const_op0 = fold_non_dependent_expr (op0, complain);
4754 if (TREE_CODE (const_op0) != INTEGER_CST)
4755 const_op0 = op0;
4756 tree const_op1 = fold_non_dependent_expr (op1, complain);
4757 if (TREE_CODE (const_op1) != INTEGER_CST)
4758 const_op1 = op1;
4759 result_type = type0;
4760 doing_shift = true;
4761 if (TREE_CODE (const_op0) == INTEGER_CST
4762 && tree_int_cst_sgn (const_op0) < 0
4763 && (complain & tf_warning)
4764 && c_inhibit_evaluation_warnings == 0)
4765 warning (OPT_Wshift_negative_value,
4766 "left shift of negative value");
4767 if (TREE_CODE (const_op1) == INTEGER_CST)
4769 if (tree_int_cst_lt (const_op1, integer_zero_node))
4771 if ((complain & tf_warning)
4772 && c_inhibit_evaluation_warnings == 0)
4773 warning (OPT_Wshift_count_negative,
4774 "left shift count is negative");
4776 else if (compare_tree_int (const_op1,
4777 TYPE_PRECISION (type0)) >= 0)
4779 if ((complain & tf_warning)
4780 && c_inhibit_evaluation_warnings == 0)
4781 warning (OPT_Wshift_count_overflow,
4782 "left shift count >= width of type");
4784 else if (TREE_CODE (const_op0) == INTEGER_CST
4785 && (complain & tf_warning))
4786 maybe_warn_shift_overflow (location, const_op0, const_op1);
4788 /* Avoid converting op1 to result_type later. */
4789 converted = 1;
4791 break;
4793 case RROTATE_EXPR:
4794 case LROTATE_EXPR:
4795 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4797 result_type = type0;
4798 if (TREE_CODE (op1) == INTEGER_CST)
4800 if (tree_int_cst_lt (op1, integer_zero_node))
4802 if (complain & tf_warning)
4803 warning (0, (code == LROTATE_EXPR)
4804 ? G_("left rotate count is negative")
4805 : G_("right rotate count is negative"));
4807 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4809 if (complain & tf_warning)
4810 warning (0, (code == LROTATE_EXPR)
4811 ? G_("left rotate count >= width of type")
4812 : G_("right rotate count >= width of type"));
4815 /* Convert the shift-count to an integer, regardless of
4816 size of value being shifted. */
4817 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4818 op1 = cp_convert (integer_type_node, op1, complain);
4820 break;
4822 case EQ_EXPR:
4823 case NE_EXPR:
4824 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4825 goto vector_compare;
4826 if ((complain & tf_warning)
4827 && c_inhibit_evaluation_warnings == 0
4828 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4829 warning (OPT_Wfloat_equal,
4830 "comparing floating point with == or != is unsafe");
4831 if ((complain & tf_warning)
4832 && ((TREE_CODE (orig_op0) == STRING_CST
4833 && !integer_zerop (cp_fully_fold (op1)))
4834 || (TREE_CODE (orig_op1) == STRING_CST
4835 && !integer_zerop (cp_fully_fold (op0)))))
4836 warning (OPT_Waddress, "comparison with string literal results "
4837 "in unspecified behavior");
4839 build_type = boolean_type_node;
4840 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4841 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4842 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4843 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4844 short_compare = 1;
4845 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4846 && null_ptr_cst_p (orig_op1))
4847 /* Handle, eg, (void*)0 (c++/43906), and more. */
4848 || (code0 == POINTER_TYPE
4849 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4851 if (TYPE_PTR_P (type1))
4852 result_type = composite_pointer_type (type0, type1, op0, op1,
4853 CPO_COMPARISON, complain);
4854 else
4855 result_type = type0;
4857 if (char_type_p (TREE_TYPE (orig_op1)))
4859 auto_diagnostic_group d;
4860 if (warning (OPT_Wpointer_compare,
4861 "comparison between pointer and zero character "
4862 "constant"))
4863 inform (input_location,
4864 "did you mean to dereference the pointer?");
4866 warn_for_null_address (location, op0, complain);
4868 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4869 && null_ptr_cst_p (orig_op0))
4870 /* Handle, eg, (void*)0 (c++/43906), and more. */
4871 || (code1 == POINTER_TYPE
4872 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4874 if (TYPE_PTR_P (type0))
4875 result_type = composite_pointer_type (type0, type1, op0, op1,
4876 CPO_COMPARISON, complain);
4877 else
4878 result_type = type1;
4880 if (char_type_p (TREE_TYPE (orig_op0)))
4882 auto_diagnostic_group d;
4883 if (warning (OPT_Wpointer_compare,
4884 "comparison between pointer and zero character "
4885 "constant"))
4886 inform (input_location,
4887 "did you mean to dereference the pointer?");
4889 warn_for_null_address (location, op1, complain);
4891 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4892 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4893 result_type = composite_pointer_type (type0, type1, op0, op1,
4894 CPO_COMPARISON, complain);
4895 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4896 /* One of the operands must be of nullptr_t type. */
4897 result_type = TREE_TYPE (nullptr_node);
4898 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4900 result_type = type0;
4901 if (complain & tf_error)
4902 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4903 else
4904 return error_mark_node;
4906 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4908 result_type = type1;
4909 if (complain & tf_error)
4910 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4911 else
4912 return error_mark_node;
4914 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4916 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4917 == ptrmemfunc_vbit_in_delta)
4919 tree pfn0, delta0, e1, e2;
4921 if (TREE_SIDE_EFFECTS (op0))
4922 op0 = save_expr (op0);
4924 pfn0 = pfn_from_ptrmemfunc (op0);
4925 delta0 = delta_from_ptrmemfunc (op0);
4926 e1 = cp_build_binary_op (location,
4927 EQ_EXPR,
4928 pfn0,
4929 build_zero_cst (TREE_TYPE (pfn0)),
4930 complain);
4931 e2 = cp_build_binary_op (location,
4932 BIT_AND_EXPR,
4933 delta0,
4934 integer_one_node,
4935 complain);
4937 if (complain & tf_warning)
4938 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4940 e2 = cp_build_binary_op (location,
4941 EQ_EXPR, e2, integer_zero_node,
4942 complain);
4943 op0 = cp_build_binary_op (location,
4944 TRUTH_ANDIF_EXPR, e1, e2,
4945 complain);
4946 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4948 else
4950 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4951 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4953 result_type = TREE_TYPE (op0);
4955 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4956 return cp_build_binary_op (location, code, op1, op0, complain);
4957 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4959 tree type;
4960 /* E will be the final comparison. */
4961 tree e;
4962 /* E1 and E2 are for scratch. */
4963 tree e1;
4964 tree e2;
4965 tree pfn0;
4966 tree pfn1;
4967 tree delta0;
4968 tree delta1;
4970 type = composite_pointer_type (type0, type1, op0, op1,
4971 CPO_COMPARISON, complain);
4973 if (!same_type_p (TREE_TYPE (op0), type))
4974 op0 = cp_convert_and_check (type, op0, complain);
4975 if (!same_type_p (TREE_TYPE (op1), type))
4976 op1 = cp_convert_and_check (type, op1, complain);
4978 if (op0 == error_mark_node || op1 == error_mark_node)
4979 return error_mark_node;
4981 if (TREE_SIDE_EFFECTS (op0))
4982 op0 = save_expr (op0);
4983 if (TREE_SIDE_EFFECTS (op1))
4984 op1 = save_expr (op1);
4986 pfn0 = pfn_from_ptrmemfunc (op0);
4987 pfn0 = cp_fully_fold (pfn0);
4988 /* Avoid -Waddress warnings (c++/64877). */
4989 if (TREE_CODE (pfn0) == ADDR_EXPR)
4990 TREE_NO_WARNING (pfn0) = 1;
4991 pfn1 = pfn_from_ptrmemfunc (op1);
4992 pfn1 = cp_fully_fold (pfn1);
4993 delta0 = delta_from_ptrmemfunc (op0);
4994 delta1 = delta_from_ptrmemfunc (op1);
4995 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4996 == ptrmemfunc_vbit_in_delta)
4998 /* We generate:
5000 (op0.pfn == op1.pfn
5001 && ((op0.delta == op1.delta)
5002 || (!op0.pfn && op0.delta & 1 == 0
5003 && op1.delta & 1 == 0))
5005 The reason for the `!op0.pfn' bit is that a NULL
5006 pointer-to-member is any member with a zero PFN and
5007 LSB of the DELTA field is 0. */
5009 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
5010 delta0,
5011 integer_one_node,
5012 complain);
5013 e1 = cp_build_binary_op (location,
5014 EQ_EXPR, e1, integer_zero_node,
5015 complain);
5016 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
5017 delta1,
5018 integer_one_node,
5019 complain);
5020 e2 = cp_build_binary_op (location,
5021 EQ_EXPR, e2, integer_zero_node,
5022 complain);
5023 e1 = cp_build_binary_op (location,
5024 TRUTH_ANDIF_EXPR, e2, e1,
5025 complain);
5026 e2 = cp_build_binary_op (location, EQ_EXPR,
5027 pfn0,
5028 build_zero_cst (TREE_TYPE (pfn0)),
5029 complain);
5030 e2 = cp_build_binary_op (location,
5031 TRUTH_ANDIF_EXPR, e2, e1, complain);
5032 e1 = cp_build_binary_op (location,
5033 EQ_EXPR, delta0, delta1, complain);
5034 e1 = cp_build_binary_op (location,
5035 TRUTH_ORIF_EXPR, e1, e2, complain);
5037 else
5039 /* We generate:
5041 (op0.pfn == op1.pfn
5042 && (!op0.pfn || op0.delta == op1.delta))
5044 The reason for the `!op0.pfn' bit is that a NULL
5045 pointer-to-member is any member with a zero PFN; the
5046 DELTA field is unspecified. */
5048 e1 = cp_build_binary_op (location,
5049 EQ_EXPR, delta0, delta1, complain);
5050 e2 = cp_build_binary_op (location,
5051 EQ_EXPR,
5052 pfn0,
5053 build_zero_cst (TREE_TYPE (pfn0)),
5054 complain);
5055 e1 = cp_build_binary_op (location,
5056 TRUTH_ORIF_EXPR, e1, e2, complain);
5058 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
5059 e = cp_build_binary_op (location,
5060 TRUTH_ANDIF_EXPR, e2, e1, complain);
5061 if (code == EQ_EXPR)
5062 return e;
5063 return cp_build_binary_op (location,
5064 EQ_EXPR, e, integer_zero_node, complain);
5066 else
5068 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
5069 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
5070 type1));
5071 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
5072 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
5073 type0));
5076 break;
5078 case MAX_EXPR:
5079 case MIN_EXPR:
5080 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
5081 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
5082 shorten = 1;
5083 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5084 result_type = composite_pointer_type (type0, type1, op0, op1,
5085 CPO_COMPARISON, complain);
5086 break;
5088 case LE_EXPR:
5089 case GE_EXPR:
5090 case LT_EXPR:
5091 case GT_EXPR:
5092 if (TREE_CODE (orig_op0) == STRING_CST
5093 || TREE_CODE (orig_op1) == STRING_CST)
5095 if (complain & tf_warning)
5096 warning (OPT_Waddress, "comparison with string literal results "
5097 "in unspecified behavior");
5100 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5102 vector_compare:
5103 tree intt;
5104 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5105 TREE_TYPE (type1))
5106 && !vector_types_compatible_elements_p (type0, type1))
5108 if (complain & tf_error)
5110 error_at (location, "comparing vectors with different "
5111 "element types");
5112 inform (location, "operand types are %qT and %qT",
5113 type0, type1);
5115 return error_mark_node;
5118 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5119 TYPE_VECTOR_SUBPARTS (type1)))
5121 if (complain & tf_error)
5123 error_at (location, "comparing vectors with different "
5124 "number of elements");
5125 inform (location, "operand types are %qT and %qT",
5126 type0, type1);
5128 return error_mark_node;
5131 /* It's not precisely specified how the usual arithmetic
5132 conversions apply to the vector types. Here, we use
5133 the unsigned type if one of the operands is signed and
5134 the other one is unsigned. */
5135 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5137 if (!TYPE_UNSIGNED (type0))
5138 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5139 else
5140 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5141 warning_at (location, OPT_Wsign_compare, "comparison between "
5142 "types %qT and %qT", type0, type1);
5145 /* Always construct signed integer vector type. */
5146 intt = c_common_type_for_size
5147 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5148 if (!intt)
5150 if (complain & tf_error)
5151 error_at (location, "could not find an integer type "
5152 "of the same size as %qT", TREE_TYPE (type0));
5153 return error_mark_node;
5155 result_type = build_opaque_vector_type (intt,
5156 TYPE_VECTOR_SUBPARTS (type0));
5157 converted = 1;
5158 return build_vec_cmp (resultcode, result_type, op0, op1);
5160 build_type = boolean_type_node;
5161 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5162 || code0 == ENUMERAL_TYPE)
5163 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5164 || code1 == ENUMERAL_TYPE))
5165 short_compare = 1;
5166 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5167 result_type = composite_pointer_type (type0, type1, op0, op1,
5168 CPO_COMPARISON, complain);
5169 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5171 result_type = type0;
5172 if (extra_warnings && (complain & tf_warning))
5173 warning (OPT_Wextra,
5174 "ordered comparison of pointer with integer zero");
5176 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5178 result_type = type1;
5179 if (extra_warnings && (complain & tf_warning))
5180 warning (OPT_Wextra,
5181 "ordered comparison of pointer with integer zero");
5183 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5184 /* One of the operands must be of nullptr_t type. */
5185 result_type = TREE_TYPE (nullptr_node);
5186 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5188 result_type = type0;
5189 if (complain & tf_error)
5190 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5191 else
5192 return error_mark_node;
5194 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5196 result_type = type1;
5197 if (complain & tf_error)
5198 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5199 else
5200 return error_mark_node;
5203 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5204 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5206 op0 = save_expr (op0);
5207 op1 = save_expr (op1);
5209 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5210 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5213 break;
5215 case UNORDERED_EXPR:
5216 case ORDERED_EXPR:
5217 case UNLT_EXPR:
5218 case UNLE_EXPR:
5219 case UNGT_EXPR:
5220 case UNGE_EXPR:
5221 case UNEQ_EXPR:
5222 build_type = integer_type_node;
5223 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5225 if (complain & tf_error)
5226 error ("unordered comparison on non-floating point argument");
5227 return error_mark_node;
5229 common = 1;
5230 break;
5232 default:
5233 break;
5236 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5237 || code0 == ENUMERAL_TYPE)
5238 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5239 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5240 arithmetic_types_p = 1;
5241 else
5243 arithmetic_types_p = 0;
5244 /* Vector arithmetic is only allowed when both sides are vectors. */
5245 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5247 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5248 || !vector_types_compatible_elements_p (type0, type1))
5250 if (complain & tf_error)
5252 /* "location" already embeds the locations of the
5253 operands, so we don't need to add them separately
5254 to richloc. */
5255 rich_location richloc (line_table, location);
5256 binary_op_error (&richloc, code, type0, type1);
5258 return error_mark_node;
5260 arithmetic_types_p = 1;
5263 /* Determine the RESULT_TYPE, if it is not already known. */
5264 if (!result_type
5265 && arithmetic_types_p
5266 && (shorten || common || short_compare))
5268 result_type = cp_common_type (type0, type1);
5269 if (complain & tf_warning)
5270 do_warn_double_promotion (result_type, type0, type1,
5271 "implicit conversion from %qH to %qI "
5272 "to match other operand of binary "
5273 "expression",
5274 location);
5277 if (!result_type)
5279 if (complain & tf_error)
5280 error_at (location,
5281 "invalid operands of types %qT and %qT to binary %qO",
5282 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5283 return error_mark_node;
5286 /* If we're in a template, the only thing we need to know is the
5287 RESULT_TYPE. */
5288 if (processing_template_decl)
5290 /* Since the middle-end checks the type when doing a build2, we
5291 need to build the tree in pieces. This built tree will never
5292 get out of the front-end as we replace it when instantiating
5293 the template. */
5294 tree tmp = build2 (resultcode,
5295 build_type ? build_type : result_type,
5296 NULL_TREE, op1);
5297 TREE_OPERAND (tmp, 0) = op0;
5298 return tmp;
5301 /* Remember the original type; RESULT_TYPE might be changed later on
5302 by shorten_binary_op. */
5303 tree orig_type = result_type;
5305 if (arithmetic_types_p)
5307 bool first_complex = (code0 == COMPLEX_TYPE);
5308 bool second_complex = (code1 == COMPLEX_TYPE);
5309 int none_complex = (!first_complex && !second_complex);
5311 /* Adapted from patch for c/24581. */
5312 if (first_complex != second_complex
5313 && (code == PLUS_EXPR
5314 || code == MINUS_EXPR
5315 || code == MULT_EXPR
5316 || (code == TRUNC_DIV_EXPR && first_complex))
5317 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5318 && flag_signed_zeros)
5320 /* An operation on mixed real/complex operands must be
5321 handled specially, but the language-independent code can
5322 more easily optimize the plain complex arithmetic if
5323 -fno-signed-zeros. */
5324 tree real_type = TREE_TYPE (result_type);
5325 tree real, imag;
5326 if (first_complex)
5328 if (TREE_TYPE (op0) != result_type)
5329 op0 = cp_convert_and_check (result_type, op0, complain);
5330 if (TREE_TYPE (op1) != real_type)
5331 op1 = cp_convert_and_check (real_type, op1, complain);
5333 else
5335 if (TREE_TYPE (op0) != real_type)
5336 op0 = cp_convert_and_check (real_type, op0, complain);
5337 if (TREE_TYPE (op1) != result_type)
5338 op1 = cp_convert_and_check (result_type, op1, complain);
5340 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5341 return error_mark_node;
5342 if (first_complex)
5344 op0 = save_expr (op0);
5345 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5346 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5347 switch (code)
5349 case MULT_EXPR:
5350 case TRUNC_DIV_EXPR:
5351 op1 = save_expr (op1);
5352 imag = build2 (resultcode, real_type, imag, op1);
5353 /* Fall through. */
5354 case PLUS_EXPR:
5355 case MINUS_EXPR:
5356 real = build2 (resultcode, real_type, real, op1);
5357 break;
5358 default:
5359 gcc_unreachable();
5362 else
5364 op1 = save_expr (op1);
5365 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5366 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5367 switch (code)
5369 case MULT_EXPR:
5370 op0 = save_expr (op0);
5371 imag = build2 (resultcode, real_type, op0, imag);
5372 /* Fall through. */
5373 case PLUS_EXPR:
5374 real = build2 (resultcode, real_type, op0, real);
5375 break;
5376 case MINUS_EXPR:
5377 real = build2 (resultcode, real_type, op0, real);
5378 imag = build1 (NEGATE_EXPR, real_type, imag);
5379 break;
5380 default:
5381 gcc_unreachable();
5384 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5385 return result;
5388 /* For certain operations (which identify themselves by shorten != 0)
5389 if both args were extended from the same smaller type,
5390 do the arithmetic in that type and then extend.
5392 shorten !=0 and !=1 indicates a bitwise operation.
5393 For them, this optimization is safe only if
5394 both args are zero-extended or both are sign-extended.
5395 Otherwise, we might change the result.
5396 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5397 but calculated in (unsigned short) it would be (unsigned short)-1. */
5399 if (shorten && none_complex)
5401 final_type = result_type;
5402 result_type = shorten_binary_op (result_type, op0, op1,
5403 shorten == -1);
5406 /* Comparison operations are shortened too but differently.
5407 They identify themselves by setting short_compare = 1. */
5409 if (short_compare)
5411 /* We call shorten_compare only for diagnostics. */
5412 tree xop0 = fold_simple (op0);
5413 tree xop1 = fold_simple (op1);
5414 tree xresult_type = result_type;
5415 enum tree_code xresultcode = resultcode;
5416 shorten_compare (location, &xop0, &xop1, &xresult_type,
5417 &xresultcode);
5420 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5421 && warn_sign_compare
5422 /* Do not warn until the template is instantiated; we cannot
5423 bound the ranges of the arguments until that point. */
5424 && !processing_template_decl
5425 && (complain & tf_warning)
5426 && c_inhibit_evaluation_warnings == 0
5427 /* Even unsigned enum types promote to signed int. We don't
5428 want to issue -Wsign-compare warnings for this case. */
5429 && !enum_cast_to_int (orig_op0)
5430 && !enum_cast_to_int (orig_op1))
5432 tree oop0 = maybe_constant_value (orig_op0);
5433 tree oop1 = maybe_constant_value (orig_op1);
5435 if (TREE_CODE (oop0) != INTEGER_CST)
5436 oop0 = cp_fully_fold (orig_op0);
5437 if (TREE_CODE (oop1) != INTEGER_CST)
5438 oop1 = cp_fully_fold (orig_op1);
5439 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5440 result_type, resultcode);
5444 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5445 Then the expression will be built.
5446 It will be given type FINAL_TYPE if that is nonzero;
5447 otherwise, it will be given type RESULT_TYPE. */
5448 if (! converted)
5450 warning_sentinel w (warn_sign_conversion, short_compare);
5451 if (TREE_TYPE (op0) != result_type)
5452 op0 = cp_convert_and_check (result_type, op0, complain);
5453 if (TREE_TYPE (op1) != result_type)
5454 op1 = cp_convert_and_check (result_type, op1, complain);
5456 if (op0 == error_mark_node || op1 == error_mark_node)
5457 return error_mark_node;
5460 if (build_type == NULL_TREE)
5461 build_type = result_type;
5463 if (sanitize_flags_p ((SANITIZE_SHIFT
5464 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5465 && current_function_decl != NULL_TREE
5466 && !processing_template_decl
5467 && (doing_div_or_mod || doing_shift))
5469 /* OP0 and/or OP1 might have side-effects. */
5470 op0 = cp_save_expr (op0);
5471 op1 = cp_save_expr (op1);
5472 op0 = fold_non_dependent_expr (op0, complain);
5473 op1 = fold_non_dependent_expr (op1, complain);
5474 if (doing_div_or_mod
5475 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5477 /* For diagnostics we want to use the promoted types without
5478 shorten_binary_op. So convert the arguments to the
5479 original result_type. */
5480 tree cop0 = op0;
5481 tree cop1 = op1;
5482 if (TREE_TYPE (cop0) != orig_type)
5483 cop0 = cp_convert (orig_type, op0, complain);
5484 if (TREE_TYPE (cop1) != orig_type)
5485 cop1 = cp_convert (orig_type, op1, complain);
5486 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5488 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5489 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5492 result = build2_loc (location, resultcode, build_type, op0, op1);
5493 if (final_type != 0)
5494 result = cp_convert (final_type, result, complain);
5496 if (instrument_expr != NULL)
5497 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5498 instrument_expr, result);
5500 if (!processing_template_decl)
5502 op0 = cp_fully_fold (op0);
5503 /* Only consider the second argument if the first isn't overflowed. */
5504 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5505 return result;
5506 op1 = cp_fully_fold (op1);
5507 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5508 return result;
5510 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5511 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5512 return result;
5514 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5515 if (TREE_OVERFLOW_P (result_ovl))
5516 overflow_warning (location, result_ovl);
5518 return result;
5521 /* Build a VEC_PERM_EXPR.
5522 This is a simple wrapper for c_build_vec_perm_expr. */
5523 tree
5524 build_x_vec_perm_expr (location_t loc,
5525 tree arg0, tree arg1, tree arg2,
5526 tsubst_flags_t complain)
5528 tree orig_arg0 = arg0;
5529 tree orig_arg1 = arg1;
5530 tree orig_arg2 = arg2;
5531 if (processing_template_decl)
5533 if (type_dependent_expression_p (arg0)
5534 || type_dependent_expression_p (arg1)
5535 || type_dependent_expression_p (arg2))
5536 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5537 arg0 = build_non_dependent_expr (arg0);
5538 if (arg1)
5539 arg1 = build_non_dependent_expr (arg1);
5540 arg2 = build_non_dependent_expr (arg2);
5542 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5543 if (processing_template_decl && exp != error_mark_node)
5544 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5545 orig_arg1, orig_arg2);
5546 return exp;
5549 /* Return a tree for the sum or difference (RESULTCODE says which)
5550 of pointer PTROP and integer INTOP. */
5552 static tree
5553 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5554 tree intop, tsubst_flags_t complain)
5556 tree res_type = TREE_TYPE (ptrop);
5558 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5559 in certain circumstance (when it's valid to do so). So we need
5560 to make sure it's complete. We don't need to check here, if we
5561 can actually complete it at all, as those checks will be done in
5562 pointer_int_sum() anyway. */
5563 complete_type (TREE_TYPE (res_type));
5565 return pointer_int_sum (loc, resultcode, ptrop,
5566 intop, complain & tf_warning_or_error);
5569 /* Return a tree for the difference of pointers OP0 and OP1.
5570 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5571 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5573 static tree
5574 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5575 tsubst_flags_t complain, tree *instrument_expr)
5577 tree result, inttype;
5578 tree restype = ptrdiff_type_node;
5579 tree target_type = TREE_TYPE (ptrtype);
5581 if (!complete_type_or_else (target_type, NULL_TREE))
5582 return error_mark_node;
5584 if (VOID_TYPE_P (target_type))
5586 if (complain & tf_error)
5587 permerror (loc, "ISO C++ forbids using pointer of "
5588 "type %<void *%> in subtraction");
5589 else
5590 return error_mark_node;
5592 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5594 if (complain & tf_error)
5595 permerror (loc, "ISO C++ forbids using pointer to "
5596 "a function in subtraction");
5597 else
5598 return error_mark_node;
5600 if (TREE_CODE (target_type) == METHOD_TYPE)
5602 if (complain & tf_error)
5603 permerror (loc, "ISO C++ forbids using pointer to "
5604 "a method in subtraction");
5605 else
5606 return error_mark_node;
5609 /* Determine integer type result of the subtraction. This will usually
5610 be the same as the result type (ptrdiff_t), but may need to be a wider
5611 type if pointers for the address space are wider than ptrdiff_t. */
5612 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5613 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5614 else
5615 inttype = restype;
5617 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5619 op0 = save_expr (op0);
5620 op1 = save_expr (op1);
5622 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5623 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5626 /* First do the subtraction, then build the divide operator
5627 and only convert at the very end.
5628 Do not do default conversions in case restype is a short type. */
5630 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5631 pointers. If some platform cannot provide that, or has a larger
5632 ptrdiff_type to support differences larger than half the address
5633 space, cast the pointers to some larger integer type and do the
5634 computations in that type. */
5635 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5636 op0 = cp_build_binary_op (loc,
5637 MINUS_EXPR,
5638 cp_convert (inttype, op0, complain),
5639 cp_convert (inttype, op1, complain),
5640 complain);
5641 else
5642 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5644 /* This generates an error if op1 is a pointer to an incomplete type. */
5645 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5647 if (complain & tf_error)
5648 error_at (loc, "invalid use of a pointer to an incomplete type in "
5649 "pointer arithmetic");
5650 else
5651 return error_mark_node;
5654 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5656 if (complain & tf_error)
5657 error_at (loc, "arithmetic on pointer to an empty aggregate");
5658 else
5659 return error_mark_node;
5662 op1 = (TYPE_PTROB_P (ptrtype)
5663 ? size_in_bytes_loc (loc, target_type)
5664 : integer_one_node);
5666 /* Do the division. */
5668 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5669 cp_convert (inttype, op1, complain));
5670 return cp_convert (restype, result, complain);
5673 /* Construct and perhaps optimize a tree representation
5674 for a unary operation. CODE, a tree_code, specifies the operation
5675 and XARG is the operand. */
5677 tree
5678 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5679 tsubst_flags_t complain)
5681 tree orig_expr = xarg;
5682 tree exp;
5683 int ptrmem = 0;
5684 tree overload = NULL_TREE;
5686 if (processing_template_decl)
5688 if (type_dependent_expression_p (xarg))
5689 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5691 xarg = build_non_dependent_expr (xarg);
5694 exp = NULL_TREE;
5696 /* [expr.unary.op] says:
5698 The address of an object of incomplete type can be taken.
5700 (And is just the ordinary address operator, not an overloaded
5701 "operator &".) However, if the type is a template
5702 specialization, we must complete the type at this point so that
5703 an overloaded "operator &" will be available if required. */
5704 if (code == ADDR_EXPR
5705 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5706 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5707 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5708 || (TREE_CODE (xarg) == OFFSET_REF)))
5709 /* Don't look for a function. */;
5710 else
5711 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5712 NULL_TREE, &overload, complain);
5714 if (!exp && code == ADDR_EXPR)
5716 if (is_overloaded_fn (xarg))
5718 tree fn = get_first_fn (xarg);
5719 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5721 if (complain & tf_error)
5722 error (DECL_CONSTRUCTOR_P (fn)
5723 ? G_("taking address of constructor %qD")
5724 : G_("taking address of destructor %qD"),
5725 fn);
5726 return error_mark_node;
5730 /* A pointer to member-function can be formed only by saying
5731 &X::mf. */
5732 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5733 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5735 if (TREE_CODE (xarg) != OFFSET_REF
5736 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5738 if (complain & tf_error)
5740 error ("invalid use of %qE to form a "
5741 "pointer-to-member-function", xarg.get_value ());
5742 if (TREE_CODE (xarg) != OFFSET_REF)
5743 inform (input_location, " a qualified-id is required");
5745 return error_mark_node;
5747 else
5749 if (complain & tf_error)
5750 error ("parentheses around %qE cannot be used to form a"
5751 " pointer-to-member-function",
5752 xarg.get_value ());
5753 else
5754 return error_mark_node;
5755 PTRMEM_OK_P (xarg) = 1;
5759 if (TREE_CODE (xarg) == OFFSET_REF)
5761 ptrmem = PTRMEM_OK_P (xarg);
5763 if (!ptrmem && !flag_ms_extensions
5764 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5766 /* A single non-static member, make sure we don't allow a
5767 pointer-to-member. */
5768 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5769 TREE_OPERAND (xarg, 0),
5770 ovl_make (TREE_OPERAND (xarg, 1)));
5771 PTRMEM_OK_P (xarg) = ptrmem;
5775 exp = cp_build_addr_expr_strict (xarg, complain);
5778 if (processing_template_decl && exp != error_mark_node)
5780 if (overload != NULL_TREE)
5781 return (build_min_non_dep_op_overload
5782 (code, exp, overload, orig_expr, integer_zero_node));
5784 exp = build_min_non_dep (code, exp, orig_expr,
5785 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5787 if (TREE_CODE (exp) == ADDR_EXPR)
5788 PTRMEM_OK_P (exp) = ptrmem;
5789 return exp;
5792 /* Construct and perhaps optimize a tree representation
5793 for __builtin_addressof operation. ARG specifies the operand. */
5795 tree
5796 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5798 tree orig_expr = arg;
5800 if (processing_template_decl)
5802 if (type_dependent_expression_p (arg))
5803 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5805 arg = build_non_dependent_expr (arg);
5808 tree exp = cp_build_addr_expr_strict (arg, complain);
5810 if (processing_template_decl && exp != error_mark_node)
5811 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5812 return exp;
5815 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5816 constants, where a null value is represented by an INTEGER_CST of
5817 -1. */
5819 tree
5820 cp_truthvalue_conversion (tree expr)
5822 tree type = TREE_TYPE (expr);
5823 if (TYPE_PTR_OR_PTRMEM_P (type)
5824 /* Avoid ICE on invalid use of non-static member function. */
5825 || TREE_CODE (expr) == FUNCTION_DECL)
5826 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5827 else
5828 return c_common_truthvalue_conversion (input_location, expr);
5831 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5833 tree
5834 condition_conversion (tree expr)
5836 tree t;
5837 /* Anything that might happen in a template should go through
5838 maybe_convert_cond. */
5839 gcc_assert (!processing_template_decl);
5840 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5841 tf_warning_or_error, LOOKUP_NORMAL);
5842 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5843 return t;
5846 /* Returns the address of T. This function will fold away
5847 ADDR_EXPR of INDIRECT_REF. */
5849 tree
5850 build_address (tree t)
5852 if (error_operand_p (t) || !cxx_mark_addressable (t))
5853 return error_mark_node;
5854 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
5855 || processing_template_decl);
5856 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
5857 if (TREE_CODE (t) != ADDR_EXPR)
5858 t = rvalue (t);
5859 return t;
5862 /* Return a NOP_EXPR converting EXPR to TYPE. */
5864 tree
5865 build_nop (tree type, tree expr)
5867 if (type == error_mark_node || error_operand_p (expr))
5868 return expr;
5869 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5872 /* Take the address of ARG, whatever that means under C++ semantics.
5873 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5874 and class rvalues as well.
5876 Nothing should call this function directly; instead, callers should use
5877 cp_build_addr_expr or cp_build_addr_expr_strict. */
5879 static tree
5880 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5882 tree argtype;
5883 tree val;
5885 if (!arg || error_operand_p (arg))
5886 return error_mark_node;
5888 arg = mark_lvalue_use (arg);
5889 if (error_operand_p (arg))
5890 return error_mark_node;
5892 argtype = lvalue_type (arg);
5894 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5896 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5897 && !really_overloaded_fn (arg))
5899 /* They're trying to take the address of a unique non-static
5900 member function. This is ill-formed (except in MS-land),
5901 but let's try to DTRT.
5902 Note: We only handle unique functions here because we don't
5903 want to complain if there's a static overload; non-unique
5904 cases will be handled by instantiate_type. But we need to
5905 handle this case here to allow casts on the resulting PMF.
5906 We could defer this in non-MS mode, but it's easier to give
5907 a useful error here. */
5909 /* Inside constant member functions, the `this' pointer
5910 contains an extra const qualifier. TYPE_MAIN_VARIANT
5911 is used here to remove this const from the diagnostics
5912 and the created OFFSET_REF. */
5913 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5914 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5915 if (!mark_used (fn, complain) && !(complain & tf_error))
5916 return error_mark_node;
5918 if (! flag_ms_extensions)
5920 tree name = DECL_NAME (fn);
5921 if (!(complain & tf_error))
5922 return error_mark_node;
5923 else if (current_class_type
5924 && TREE_OPERAND (arg, 0) == current_class_ref)
5925 /* An expression like &memfn. */
5926 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5927 " or parenthesized non-static member function to form"
5928 " a pointer to member function. Say %<&%T::%D%>",
5929 base, name);
5930 else
5931 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5932 " function to form a pointer to member function."
5933 " Say %<&%T::%D%>",
5934 base, name);
5936 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5939 /* Uninstantiated types are all functions. Taking the
5940 address of a function is a no-op, so just return the
5941 argument. */
5942 if (type_unknown_p (arg))
5943 return build1 (ADDR_EXPR, unknown_type_node, arg);
5945 if (TREE_CODE (arg) == OFFSET_REF)
5946 /* We want a pointer to member; bypass all the code for actually taking
5947 the address of something. */
5948 goto offset_ref;
5950 /* Anything not already handled and not a true memory reference
5951 is an error. */
5952 if (TREE_CODE (argtype) != FUNCTION_TYPE
5953 && TREE_CODE (argtype) != METHOD_TYPE)
5955 cp_lvalue_kind kind = lvalue_kind (arg);
5956 if (kind == clk_none)
5958 if (complain & tf_error)
5959 lvalue_error (input_location, lv_addressof);
5960 return error_mark_node;
5962 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5964 if (!(complain & tf_error))
5965 return error_mark_node;
5966 /* Make this a permerror because we used to accept it. */
5967 permerror (input_location, "taking address of rvalue");
5971 if (TYPE_REF_P (argtype))
5973 tree type = build_pointer_type (TREE_TYPE (argtype));
5974 arg = build1 (CONVERT_EXPR, type, arg);
5975 return arg;
5977 else if (pedantic && DECL_MAIN_P (arg))
5979 /* ARM $3.4 */
5980 /* Apparently a lot of autoconf scripts for C++ packages do this,
5981 so only complain if -Wpedantic. */
5982 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5983 pedwarn (input_location, OPT_Wpedantic,
5984 "ISO C++ forbids taking address of function %<::main%>");
5985 else if (flag_pedantic_errors)
5986 return error_mark_node;
5989 /* Let &* cancel out to simplify resulting code. */
5990 if (INDIRECT_REF_P (arg))
5992 arg = TREE_OPERAND (arg, 0);
5993 if (TYPE_REF_P (TREE_TYPE (arg)))
5995 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5996 arg = build1 (CONVERT_EXPR, type, arg);
5998 else
5999 /* Don't let this be an lvalue. */
6000 arg = rvalue (arg);
6001 return arg;
6004 /* Handle complex lvalues (when permitted)
6005 by reduction to simpler cases. */
6006 val = unary_complex_lvalue (ADDR_EXPR, arg);
6007 if (val != 0)
6008 return val;
6010 switch (TREE_CODE (arg))
6012 CASE_CONVERT:
6013 case FLOAT_EXPR:
6014 case FIX_TRUNC_EXPR:
6015 /* We should have handled this above in the lvalue_kind check. */
6016 gcc_unreachable ();
6017 break;
6019 case BASELINK:
6020 arg = BASELINK_FUNCTIONS (arg);
6021 /* Fall through. */
6023 case OVERLOAD:
6024 arg = OVL_FIRST (arg);
6025 break;
6027 case OFFSET_REF:
6028 offset_ref:
6029 /* Turn a reference to a non-static data member into a
6030 pointer-to-member. */
6032 tree type;
6033 tree t;
6035 gcc_assert (PTRMEM_OK_P (arg));
6037 t = TREE_OPERAND (arg, 1);
6038 if (TYPE_REF_P (TREE_TYPE (t)))
6040 if (complain & tf_error)
6041 error ("cannot create pointer to reference member %qD", t);
6042 return error_mark_node;
6045 type = build_ptrmem_type (context_for_name_lookup (t),
6046 TREE_TYPE (t));
6047 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
6048 return t;
6051 default:
6052 break;
6055 if (argtype != error_mark_node)
6056 argtype = build_pointer_type (argtype);
6058 if (bitfield_p (arg))
6060 if (complain & tf_error)
6061 error ("attempt to take address of bit-field");
6062 return error_mark_node;
6065 /* In a template, we are processing a non-dependent expression
6066 so we can just form an ADDR_EXPR with the correct type. */
6067 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
6069 if (TREE_CODE (arg) == FUNCTION_DECL
6070 && !mark_used (arg, complain) && !(complain & tf_error))
6071 return error_mark_node;
6072 val = build_address (arg);
6073 if (TREE_CODE (arg) == OFFSET_REF)
6074 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
6076 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
6078 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
6080 /* We can only get here with a single static member
6081 function. */
6082 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6083 && DECL_STATIC_FUNCTION_P (fn));
6084 if (!mark_used (fn, complain) && !(complain & tf_error))
6085 return error_mark_node;
6086 val = build_address (fn);
6087 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
6088 /* Do not lose object's side effects. */
6089 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
6090 TREE_OPERAND (arg, 0), val);
6092 else
6094 tree object = TREE_OPERAND (arg, 0);
6095 tree field = TREE_OPERAND (arg, 1);
6096 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6097 (TREE_TYPE (object), decl_type_context (field)));
6098 val = build_address (arg);
6101 if (TYPE_PTR_P (argtype)
6102 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6104 build_ptrmemfunc_type (argtype);
6105 val = build_ptrmemfunc (argtype, val, 0,
6106 /*c_cast_p=*/false,
6107 complain);
6110 return val;
6113 /* Take the address of ARG if it has one, even if it's an rvalue. */
6115 tree
6116 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6118 return cp_build_addr_expr_1 (arg, 0, complain);
6121 /* Take the address of ARG, but only if it's an lvalue. */
6123 static tree
6124 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6126 return cp_build_addr_expr_1 (arg, 1, complain);
6129 /* C++: Must handle pointers to members.
6131 Perhaps type instantiation should be extended to handle conversion
6132 from aggregates to types we don't yet know we want? (Or are those
6133 cases typically errors which should be reported?)
6135 NOCONVERT suppresses the default promotions (such as from short to int). */
6137 tree
6138 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6139 tsubst_flags_t complain)
6141 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6142 tree arg = xarg;
6143 location_t location = cp_expr_loc_or_loc (arg, input_location);
6144 tree argtype = 0;
6145 const char *errstring = NULL;
6146 tree val;
6147 const char *invalid_op_diag;
6149 if (!arg || error_operand_p (arg))
6150 return error_mark_node;
6152 if ((invalid_op_diag
6153 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6154 ? CONVERT_EXPR
6155 : code),
6156 TREE_TYPE (xarg))))
6158 if (complain & tf_error)
6159 error (invalid_op_diag);
6160 return error_mark_node;
6163 switch (code)
6165 case UNARY_PLUS_EXPR:
6166 case NEGATE_EXPR:
6168 int flags = WANT_ARITH | WANT_ENUM;
6169 /* Unary plus (but not unary minus) is allowed on pointers. */
6170 if (code == UNARY_PLUS_EXPR)
6171 flags |= WANT_POINTER;
6172 arg = build_expr_type_conversion (flags, arg, true);
6173 if (!arg)
6174 errstring = (code == NEGATE_EXPR
6175 ? _("wrong type argument to unary minus")
6176 : _("wrong type argument to unary plus"));
6177 else
6179 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6180 arg = cp_perform_integral_promotions (arg, complain);
6182 /* Make sure the result is not an lvalue: a unary plus or minus
6183 expression is always a rvalue. */
6184 arg = rvalue (arg);
6187 break;
6189 case BIT_NOT_EXPR:
6190 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6192 code = CONJ_EXPR;
6193 if (!noconvert)
6195 arg = cp_default_conversion (arg, complain);
6196 if (arg == error_mark_node)
6197 return error_mark_node;
6200 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6201 | WANT_VECTOR_OR_COMPLEX,
6202 arg, true)))
6203 errstring = _("wrong type argument to bit-complement");
6204 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6206 /* Warn if the expression has boolean value. */
6207 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6208 && (complain & tf_warning)
6209 && warning_at (location, OPT_Wbool_operation,
6210 "%<~%> on an expression of type bool"))
6211 inform (location, "did you mean to use logical not (%<!%>)?");
6212 arg = cp_perform_integral_promotions (arg, complain);
6214 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6215 arg = mark_rvalue_use (arg);
6216 break;
6218 case ABS_EXPR:
6219 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6220 errstring = _("wrong type argument to abs");
6221 else if (!noconvert)
6223 arg = cp_default_conversion (arg, complain);
6224 if (arg == error_mark_node)
6225 return error_mark_node;
6227 break;
6229 case CONJ_EXPR:
6230 /* Conjugating a real value is a no-op, but allow it anyway. */
6231 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6232 errstring = _("wrong type argument to conjugation");
6233 else if (!noconvert)
6235 arg = cp_default_conversion (arg, complain);
6236 if (arg == error_mark_node)
6237 return error_mark_node;
6239 break;
6241 case TRUTH_NOT_EXPR:
6242 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6243 return cp_build_binary_op (input_location, EQ_EXPR, arg,
6244 build_zero_cst (TREE_TYPE (arg)), complain);
6245 arg = perform_implicit_conversion (boolean_type_node, arg,
6246 complain);
6247 val = invert_truthvalue_loc (input_location, arg);
6248 if (arg != error_mark_node)
6249 return val;
6250 errstring = _("in argument to unary !");
6251 break;
6253 case NOP_EXPR:
6254 break;
6256 case REALPART_EXPR:
6257 case IMAGPART_EXPR:
6258 arg = build_real_imag_expr (input_location, code, arg);
6259 return arg;
6261 case PREINCREMENT_EXPR:
6262 case POSTINCREMENT_EXPR:
6263 case PREDECREMENT_EXPR:
6264 case POSTDECREMENT_EXPR:
6265 /* Handle complex lvalues (when permitted)
6266 by reduction to simpler cases. */
6268 val = unary_complex_lvalue (code, arg);
6269 if (val != 0)
6270 return val;
6272 arg = mark_lvalue_use (arg);
6274 /* Increment or decrement the real part of the value,
6275 and don't change the imaginary part. */
6276 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6278 tree real, imag;
6280 arg = cp_stabilize_reference (arg);
6281 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6282 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6283 real = cp_build_unary_op (code, real, true, complain);
6284 if (real == error_mark_node || imag == error_mark_node)
6285 return error_mark_node;
6286 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6287 real, imag);
6290 /* Report invalid types. */
6292 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6293 arg, true)))
6295 if (code == PREINCREMENT_EXPR)
6296 errstring = _("no pre-increment operator for type");
6297 else if (code == POSTINCREMENT_EXPR)
6298 errstring = _("no post-increment operator for type");
6299 else if (code == PREDECREMENT_EXPR)
6300 errstring = _("no pre-decrement operator for type");
6301 else
6302 errstring = _("no post-decrement operator for type");
6303 break;
6305 else if (arg == error_mark_node)
6306 return error_mark_node;
6308 /* Report something read-only. */
6310 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6311 || TREE_READONLY (arg))
6313 if (complain & tf_error)
6314 cxx_readonly_error (location, arg,
6315 ((code == PREINCREMENT_EXPR
6316 || code == POSTINCREMENT_EXPR)
6317 ? lv_increment : lv_decrement));
6318 else
6319 return error_mark_node;
6323 tree inc;
6324 tree declared_type = unlowered_expr_type (arg);
6326 argtype = TREE_TYPE (arg);
6328 /* ARM $5.2.5 last annotation says this should be forbidden. */
6329 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6331 if (complain & tf_error)
6332 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6333 ? G_("ISO C++ forbids incrementing an enum")
6334 : G_("ISO C++ forbids decrementing an enum"));
6335 else
6336 return error_mark_node;
6339 /* Compute the increment. */
6341 if (TYPE_PTR_P (argtype))
6343 tree type = complete_type (TREE_TYPE (argtype));
6345 if (!COMPLETE_OR_VOID_TYPE_P (type))
6347 if (complain & tf_error)
6348 error (((code == PREINCREMENT_EXPR
6349 || code == POSTINCREMENT_EXPR))
6350 ? G_("cannot increment a pointer to incomplete type %qT")
6351 : G_("cannot decrement a pointer to incomplete type %qT"),
6352 TREE_TYPE (argtype));
6353 else
6354 return error_mark_node;
6356 else if (!TYPE_PTROB_P (argtype))
6358 if (complain & tf_error)
6359 pedwarn (input_location, OPT_Wpointer_arith,
6360 (code == PREINCREMENT_EXPR
6361 || code == POSTINCREMENT_EXPR)
6362 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6363 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6364 argtype);
6365 else
6366 return error_mark_node;
6369 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6371 else
6372 inc = VECTOR_TYPE_P (argtype)
6373 ? build_one_cst (argtype)
6374 : integer_one_node;
6376 inc = cp_convert (argtype, inc, complain);
6378 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6379 need to ask Objective-C to build the increment or decrement
6380 expression for it. */
6381 if (objc_is_property_ref (arg))
6382 return objc_build_incr_expr_for_property_ref (input_location, code,
6383 arg, inc);
6385 /* Complain about anything else that is not a true lvalue. */
6386 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6387 || code == POSTINCREMENT_EXPR)
6388 ? lv_increment : lv_decrement),
6389 complain))
6390 return error_mark_node;
6392 /* Forbid using -- or ++ in C++17 on `bool'. */
6393 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6395 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6397 if (complain & tf_error)
6398 error ("use of an operand of type %qT in %<operator--%> "
6399 "is forbidden", boolean_type_node);
6400 return error_mark_node;
6402 else
6404 if (cxx_dialect >= cxx17)
6406 if (complain & tf_error)
6407 error ("use of an operand of type %qT in "
6408 "%<operator++%> is forbidden in C++17",
6409 boolean_type_node);
6410 return error_mark_node;
6412 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6413 else if (!in_system_header_at (input_location))
6414 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6415 "in %<operator++%> is deprecated",
6416 boolean_type_node);
6418 val = boolean_increment (code, arg);
6420 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6421 /* An rvalue has no cv-qualifiers. */
6422 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6423 else
6424 val = build2 (code, TREE_TYPE (arg), arg, inc);
6426 TREE_SIDE_EFFECTS (val) = 1;
6427 return val;
6430 case ADDR_EXPR:
6431 /* Note that this operation never does default_conversion
6432 regardless of NOCONVERT. */
6433 return cp_build_addr_expr (arg, complain);
6435 default:
6436 break;
6439 if (!errstring)
6441 if (argtype == 0)
6442 argtype = TREE_TYPE (arg);
6443 return build1 (code, argtype, arg);
6446 if (complain & tf_error)
6447 error ("%s", errstring);
6448 return error_mark_node;
6451 /* Hook for the c-common bits that build a unary op. */
6452 tree
6453 build_unary_op (location_t /*location*/,
6454 enum tree_code code, tree xarg, bool noconvert)
6456 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6459 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6460 so that it is a valid lvalue even for GENERIC by replacing
6461 (lhs = rhs) with ((lhs = rhs), lhs)
6462 (--lhs) with ((--lhs), lhs)
6463 (++lhs) with ((++lhs), lhs)
6464 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6465 that it can be evaluated multiple times. */
6467 tree
6468 genericize_compound_lvalue (tree lvalue)
6470 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
6471 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
6472 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
6473 TREE_OPERAND (lvalue, 1));
6474 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
6475 lvalue, TREE_OPERAND (lvalue, 0));
6478 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6479 for certain kinds of expressions which are not really lvalues
6480 but which we can accept as lvalues.
6482 If ARG is not a kind of expression we can handle, return
6483 NULL_TREE. */
6485 tree
6486 unary_complex_lvalue (enum tree_code code, tree arg)
6488 /* Inside a template, making these kinds of adjustments is
6489 pointless; we are only concerned with the type of the
6490 expression. */
6491 if (processing_template_decl)
6492 return NULL_TREE;
6494 /* Handle (a, b) used as an "lvalue". */
6495 if (TREE_CODE (arg) == COMPOUND_EXPR)
6497 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6498 tf_warning_or_error);
6499 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6500 TREE_OPERAND (arg, 0), real_result);
6503 /* Handle (a ? b : c) used as an "lvalue". */
6504 if (TREE_CODE (arg) == COND_EXPR
6505 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6506 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6508 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6509 if (TREE_CODE (arg) == MODIFY_EXPR
6510 || TREE_CODE (arg) == PREINCREMENT_EXPR
6511 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6512 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
6514 if (code != ADDR_EXPR)
6515 return NULL_TREE;
6517 /* Handle (a = b) used as an "lvalue" for `&'. */
6518 if (TREE_CODE (arg) == MODIFY_EXPR
6519 || TREE_CODE (arg) == INIT_EXPR)
6521 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6522 tf_warning_or_error);
6523 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6524 arg, real_result);
6525 TREE_NO_WARNING (arg) = 1;
6526 return arg;
6529 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6530 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6531 || TREE_CODE (arg) == OFFSET_REF)
6532 return NULL_TREE;
6534 /* We permit compiler to make function calls returning
6535 objects of aggregate type look like lvalues. */
6537 tree targ = arg;
6539 if (TREE_CODE (targ) == SAVE_EXPR)
6540 targ = TREE_OPERAND (targ, 0);
6542 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6544 if (TREE_CODE (arg) == SAVE_EXPR)
6545 targ = arg;
6546 else
6547 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6548 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6551 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6552 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6553 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6556 /* Don't let anything else be handled specially. */
6557 return NULL_TREE;
6560 /* Mark EXP saying that we need to be able to take the
6561 address of it; it should not be allocated in a register.
6562 Value is true if successful. ARRAY_REF_P is true if this
6563 is for ARRAY_REF construction - in that case we don't want
6564 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6565 it is fine to use ARRAY_REFs for vector subscripts on vector
6566 register variables.
6568 C++: we do not allow `current_class_ptr' to be addressable. */
6570 bool
6571 cxx_mark_addressable (tree exp, bool array_ref_p)
6573 tree x = exp;
6575 while (1)
6576 switch (TREE_CODE (x))
6578 case VIEW_CONVERT_EXPR:
6579 if (array_ref_p
6580 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6581 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6582 return true;
6583 /* FALLTHRU */
6584 case ADDR_EXPR:
6585 case COMPONENT_REF:
6586 case ARRAY_REF:
6587 case REALPART_EXPR:
6588 case IMAGPART_EXPR:
6589 x = TREE_OPERAND (x, 0);
6590 break;
6592 case PARM_DECL:
6593 if (x == current_class_ptr)
6595 error ("cannot take the address of %<this%>, which is an rvalue expression");
6596 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6597 return true;
6599 /* Fall through. */
6601 case VAR_DECL:
6602 /* Caller should not be trying to mark initialized
6603 constant fields addressable. */
6604 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6605 || DECL_IN_AGGR_P (x) == 0
6606 || TREE_STATIC (x)
6607 || DECL_EXTERNAL (x));
6608 /* Fall through. */
6610 case RESULT_DECL:
6611 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6612 && !DECL_ARTIFICIAL (x))
6614 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6616 error
6617 ("address of explicit register variable %qD requested", x);
6618 return false;
6620 else if (extra_warnings)
6621 warning
6622 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6624 TREE_ADDRESSABLE (x) = 1;
6625 return true;
6627 case CONST_DECL:
6628 case FUNCTION_DECL:
6629 TREE_ADDRESSABLE (x) = 1;
6630 return true;
6632 case CONSTRUCTOR:
6633 TREE_ADDRESSABLE (x) = 1;
6634 return true;
6636 case TARGET_EXPR:
6637 TREE_ADDRESSABLE (x) = 1;
6638 cxx_mark_addressable (TREE_OPERAND (x, 0));
6639 return true;
6641 default:
6642 return true;
6646 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6648 tree
6649 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6650 tsubst_flags_t complain)
6652 tree orig_ifexp = ifexp;
6653 tree orig_op1 = op1;
6654 tree orig_op2 = op2;
6655 tree expr;
6657 if (processing_template_decl)
6659 /* The standard says that the expression is type-dependent if
6660 IFEXP is type-dependent, even though the eventual type of the
6661 expression doesn't dependent on IFEXP. */
6662 if (type_dependent_expression_p (ifexp)
6663 /* As a GNU extension, the middle operand may be omitted. */
6664 || (op1 && type_dependent_expression_p (op1))
6665 || type_dependent_expression_p (op2))
6666 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6667 ifexp = build_non_dependent_expr (ifexp);
6668 if (op1)
6669 op1 = build_non_dependent_expr (op1);
6670 op2 = build_non_dependent_expr (op2);
6673 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6674 if (processing_template_decl && expr != error_mark_node)
6676 tree min = build_min_non_dep (COND_EXPR, expr,
6677 orig_ifexp, orig_op1, orig_op2);
6678 expr = convert_from_reference (min);
6680 return expr;
6683 /* Given a list of expressions, return a compound expression
6684 that performs them all and returns the value of the last of them. */
6686 tree
6687 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6688 tsubst_flags_t complain)
6690 tree expr = TREE_VALUE (list);
6692 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6693 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6695 if (complain & tf_error)
6696 pedwarn (cp_expr_loc_or_loc (expr, input_location), 0,
6697 "list-initializer for non-class type must not "
6698 "be parenthesized");
6699 else
6700 return error_mark_node;
6703 if (TREE_CHAIN (list))
6705 if (complain & tf_error)
6706 switch (exp)
6708 case ELK_INIT:
6709 permerror (input_location, "expression list treated as compound "
6710 "expression in initializer");
6711 break;
6712 case ELK_MEM_INIT:
6713 permerror (input_location, "expression list treated as compound "
6714 "expression in mem-initializer");
6715 break;
6716 case ELK_FUNC_CAST:
6717 permerror (input_location, "expression list treated as compound "
6718 "expression in functional cast");
6719 break;
6720 default:
6721 gcc_unreachable ();
6723 else
6724 return error_mark_node;
6726 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6727 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6728 expr, TREE_VALUE (list), complain);
6731 return expr;
6734 /* Like build_x_compound_expr_from_list, but using a VEC. */
6736 tree
6737 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6738 tsubst_flags_t complain)
6740 if (vec_safe_is_empty (vec))
6741 return NULL_TREE;
6742 else if (vec->length () == 1)
6743 return (*vec)[0];
6744 else
6746 tree expr;
6747 unsigned int ix;
6748 tree t;
6750 if (msg != NULL)
6752 if (complain & tf_error)
6753 permerror (input_location,
6754 "%s expression list treated as compound expression",
6755 msg);
6756 else
6757 return error_mark_node;
6760 expr = (*vec)[0];
6761 for (ix = 1; vec->iterate (ix, &t); ++ix)
6762 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6763 t, complain);
6765 return expr;
6769 /* Handle overloading of the ',' operator when needed. */
6771 tree
6772 build_x_compound_expr (location_t loc, tree op1, tree op2,
6773 tsubst_flags_t complain)
6775 tree result;
6776 tree orig_op1 = op1;
6777 tree orig_op2 = op2;
6778 tree overload = NULL_TREE;
6780 if (processing_template_decl)
6782 if (type_dependent_expression_p (op1)
6783 || type_dependent_expression_p (op2))
6784 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6785 op1 = build_non_dependent_expr (op1);
6786 op2 = build_non_dependent_expr (op2);
6789 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6790 NULL_TREE, &overload, complain);
6791 if (!result)
6792 result = cp_build_compound_expr (op1, op2, complain);
6794 if (processing_template_decl && result != error_mark_node)
6796 if (overload != NULL_TREE)
6797 return (build_min_non_dep_op_overload
6798 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6800 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6803 return result;
6806 /* Like cp_build_compound_expr, but for the c-common bits. */
6808 tree
6809 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6811 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6814 /* Build a compound expression. */
6816 tree
6817 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6819 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6821 if (lhs == error_mark_node || rhs == error_mark_node)
6822 return error_mark_node;
6824 if (TREE_CODE (rhs) == TARGET_EXPR)
6826 /* If the rhs is a TARGET_EXPR, then build the compound
6827 expression inside the target_expr's initializer. This
6828 helps the compiler to eliminate unnecessary temporaries. */
6829 tree init = TREE_OPERAND (rhs, 1);
6831 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6832 TREE_OPERAND (rhs, 1) = init;
6834 return rhs;
6837 if (type_unknown_p (rhs))
6839 if (complain & tf_error)
6840 error ("no context to resolve type of %qE", rhs);
6841 return error_mark_node;
6844 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6847 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6848 casts away constness. CAST gives the type of cast. Returns true
6849 if the cast is ill-formed, false if it is well-formed.
6851 ??? This function warns for casting away any qualifier not just
6852 const. We would like to specify exactly what qualifiers are casted
6853 away.
6856 static bool
6857 check_for_casting_away_constness (tree src_type, tree dest_type,
6858 enum tree_code cast, tsubst_flags_t complain)
6860 /* C-style casts are allowed to cast away constness. With
6861 WARN_CAST_QUAL, we still want to issue a warning. */
6862 if (cast == CAST_EXPR && !warn_cast_qual)
6863 return false;
6865 if (!casts_away_constness (src_type, dest_type, complain))
6866 return false;
6868 switch (cast)
6870 case CAST_EXPR:
6871 if (complain & tf_warning)
6872 warning (OPT_Wcast_qual,
6873 "cast from type %qT to type %qT casts away qualifiers",
6874 src_type, dest_type);
6875 return false;
6877 case STATIC_CAST_EXPR:
6878 if (complain & tf_error)
6879 error ("static_cast from type %qT to type %qT casts away qualifiers",
6880 src_type, dest_type);
6881 return true;
6883 case REINTERPRET_CAST_EXPR:
6884 if (complain & tf_error)
6885 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6886 src_type, dest_type);
6887 return true;
6889 default:
6890 gcc_unreachable();
6894 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6895 void
6896 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6898 if (warn_useless_cast
6899 && complain & tf_warning)
6901 if ((TYPE_REF_P (type)
6902 && (TYPE_REF_IS_RVALUE (type)
6903 ? xvalue_p (expr) : lvalue_p (expr))
6904 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6905 || same_type_p (TREE_TYPE (expr), type))
6906 warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6910 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6911 void
6912 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6914 if (warn_ignored_qualifiers
6915 && complain & tf_warning
6916 && !CLASS_TYPE_P (type)
6917 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6919 warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6920 "result type");
6924 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6925 (another pointer-to-member type in the same hierarchy) and return
6926 the converted expression. If ALLOW_INVERSE_P is permitted, a
6927 pointer-to-derived may be converted to pointer-to-base; otherwise,
6928 only the other direction is permitted. If C_CAST_P is true, this
6929 conversion is taking place as part of a C-style cast. */
6931 tree
6932 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6933 bool c_cast_p, tsubst_flags_t complain)
6935 if (same_type_p (type, TREE_TYPE (expr)))
6936 return expr;
6938 if (TYPE_PTRDATAMEM_P (type))
6940 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
6941 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
6942 tree delta = (get_delta_difference
6943 (obase, nbase,
6944 allow_inverse_p, c_cast_p, complain));
6946 if (delta == error_mark_node)
6947 return error_mark_node;
6949 if (!same_type_p (obase, nbase))
6951 if (TREE_CODE (expr) == PTRMEM_CST)
6952 expr = cplus_expand_constant (expr);
6954 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
6955 build_int_cst (TREE_TYPE (expr), -1),
6956 complain);
6957 tree op1 = build_nop (ptrdiff_type_node, expr);
6958 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
6959 complain);
6961 expr = fold_build3_loc (input_location,
6962 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6965 return build_nop (type, expr);
6967 else
6968 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6969 allow_inverse_p, c_cast_p, complain);
6972 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6973 this static_cast is being attempted as one of the possible casts
6974 allowed by a C-style cast. (In that case, accessibility of base
6975 classes is not considered, and it is OK to cast away
6976 constness.) Return the result of the cast. *VALID_P is set to
6977 indicate whether or not the cast was valid. */
6979 static tree
6980 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6981 bool *valid_p, tsubst_flags_t complain)
6983 tree intype;
6984 tree result;
6985 cp_lvalue_kind clk;
6987 /* Assume the cast is valid. */
6988 *valid_p = true;
6990 intype = unlowered_expr_type (expr);
6992 /* Save casted types in the function's used types hash table. */
6993 used_types_insert (type);
6995 /* A prvalue of non-class type is cv-unqualified. */
6996 if (!CLASS_TYPE_P (type))
6997 type = cv_unqualified (type);
6999 /* [expr.static.cast]
7001 An lvalue of type "cv1 B", where B is a class type, can be cast
7002 to type "reference to cv2 D", where D is a class derived (clause
7003 _class.derived_) from B, if a valid standard conversion from
7004 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7005 same cv-qualification as, or greater cv-qualification than, cv1,
7006 and B is not a virtual base class of D. */
7007 /* We check this case before checking the validity of "TYPE t =
7008 EXPR;" below because for this case:
7010 struct B {};
7011 struct D : public B { D(const B&); };
7012 extern B& b;
7013 void f() { static_cast<const D&>(b); }
7015 we want to avoid constructing a new D. The standard is not
7016 completely clear about this issue, but our interpretation is
7017 consistent with other compilers. */
7018 if (TYPE_REF_P (type)
7019 && CLASS_TYPE_P (TREE_TYPE (type))
7020 && CLASS_TYPE_P (intype)
7021 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
7022 && DERIVED_FROM_P (intype, TREE_TYPE (type))
7023 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
7024 build_pointer_type (TYPE_MAIN_VARIANT
7025 (TREE_TYPE (type))),
7026 complain)
7027 && (c_cast_p
7028 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7030 tree base;
7032 if (processing_template_decl)
7033 return expr;
7035 /* There is a standard conversion from "D*" to "B*" even if "B"
7036 is ambiguous or inaccessible. If this is really a
7037 static_cast, then we check both for inaccessibility and
7038 ambiguity. However, if this is a static_cast being performed
7039 because the user wrote a C-style cast, then accessibility is
7040 not considered. */
7041 base = lookup_base (TREE_TYPE (type), intype,
7042 c_cast_p ? ba_unique : ba_check,
7043 NULL, complain);
7044 expr = build_address (expr);
7046 if (sanitize_flags_p (SANITIZE_VPTR))
7048 tree ubsan_check
7049 = cp_ubsan_maybe_instrument_downcast (input_location, type,
7050 intype, expr);
7051 if (ubsan_check)
7052 expr = ubsan_check;
7055 /* Convert from "B*" to "D*". This function will check that "B"
7056 is not a virtual base of "D". Even if we don't have a guarantee
7057 that expr is NULL, if the static_cast is to a reference type,
7058 it is UB if it would be NULL, so omit the non-NULL check. */
7059 expr = build_base_path (MINUS_EXPR, expr, base,
7060 /*nonnull=*/flag_delete_null_pointer_checks,
7061 complain);
7063 /* Convert the pointer to a reference -- but then remember that
7064 there are no expressions with reference type in C++.
7066 We call rvalue so that there's an actual tree code
7067 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7068 is a variable with the same type, the conversion would get folded
7069 away, leaving just the variable and causing lvalue_kind to give
7070 the wrong answer. */
7071 expr = cp_fold_convert (type, expr);
7073 /* When -fsanitize=null, make sure to diagnose reference binding to
7074 NULL even when the reference is converted to pointer later on. */
7075 if (sanitize_flags_p (SANITIZE_NULL)
7076 && TREE_CODE (expr) == COND_EXPR
7077 && TREE_OPERAND (expr, 2)
7078 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
7079 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
7080 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
7082 return convert_from_reference (rvalue (expr));
7085 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7086 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7087 if (TYPE_REF_P (type)
7088 && TYPE_REF_IS_RVALUE (type)
7089 && (clk = real_lvalue_p (expr))
7090 && reference_related_p (TREE_TYPE (type), intype)
7091 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7093 if (processing_template_decl)
7094 return expr;
7095 if (clk == clk_ordinary)
7097 /* Handle the (non-bit-field) lvalue case here by casting to
7098 lvalue reference and then changing it to an rvalue reference.
7099 Casting an xvalue to rvalue reference will be handled by the
7100 main code path. */
7101 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
7102 result = (perform_direct_initialization_if_possible
7103 (lref, expr, c_cast_p, complain));
7104 result = build1 (NON_LVALUE_EXPR, type, result);
7105 return convert_from_reference (result);
7107 else
7108 /* For a bit-field or packed field, bind to a temporary. */
7109 expr = rvalue (expr);
7112 /* Resolve overloaded address here rather than once in
7113 implicit_conversion and again in the inverse code below. */
7114 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7116 expr = instantiate_type (type, expr, complain);
7117 intype = TREE_TYPE (expr);
7120 /* [expr.static.cast]
7122 Any expression can be explicitly converted to type cv void. */
7123 if (VOID_TYPE_P (type))
7124 return convert_to_void (expr, ICV_CAST, complain);
7126 /* [class.abstract]
7127 An abstract class shall not be used ... as the type of an explicit
7128 conversion. */
7129 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7130 return error_mark_node;
7132 /* [expr.static.cast]
7134 An expression e can be explicitly converted to a type T using a
7135 static_cast of the form static_cast<T>(e) if the declaration T
7136 t(e);" is well-formed, for some invented temporary variable
7137 t. */
7138 result = perform_direct_initialization_if_possible (type, expr,
7139 c_cast_p, complain);
7140 if (result)
7142 if (processing_template_decl)
7143 return expr;
7145 result = convert_from_reference (result);
7147 /* [expr.static.cast]
7149 If T is a reference type, the result is an lvalue; otherwise,
7150 the result is an rvalue. */
7151 if (!TYPE_REF_P (type))
7153 result = rvalue (result);
7155 if (result == expr && SCALAR_TYPE_P (type))
7156 /* Leave some record of the cast. */
7157 result = build_nop (type, expr);
7159 return result;
7162 /* [expr.static.cast]
7164 The inverse of any standard conversion sequence (clause _conv_),
7165 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7166 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7167 (_conv.bool_) conversions, can be performed explicitly using
7168 static_cast subject to the restriction that the explicit
7169 conversion does not cast away constness (_expr.const.cast_), and
7170 the following additional rules for specific cases: */
7171 /* For reference, the conversions not excluded are: integral
7172 promotions, floating point promotion, integral conversions,
7173 floating point conversions, floating-integral conversions,
7174 pointer conversions, and pointer to member conversions. */
7175 /* DR 128
7177 A value of integral _or enumeration_ type can be explicitly
7178 converted to an enumeration type. */
7179 /* The effect of all that is that any conversion between any two
7180 types which are integral, floating, or enumeration types can be
7181 performed. */
7182 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7183 || SCALAR_FLOAT_TYPE_P (type))
7184 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7185 || SCALAR_FLOAT_TYPE_P (intype)))
7187 if (processing_template_decl)
7188 return expr;
7189 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7192 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7193 && CLASS_TYPE_P (TREE_TYPE (type))
7194 && CLASS_TYPE_P (TREE_TYPE (intype))
7195 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7196 (TREE_TYPE (intype))),
7197 build_pointer_type (TYPE_MAIN_VARIANT
7198 (TREE_TYPE (type))),
7199 complain))
7201 tree base;
7203 if (processing_template_decl)
7204 return expr;
7206 if (!c_cast_p
7207 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7208 complain))
7209 return error_mark_node;
7210 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7211 c_cast_p ? ba_unique : ba_check,
7212 NULL, complain);
7213 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7214 complain);
7216 if (sanitize_flags_p (SANITIZE_VPTR))
7218 tree ubsan_check
7219 = cp_ubsan_maybe_instrument_downcast (input_location, type,
7220 intype, expr);
7221 if (ubsan_check)
7222 expr = ubsan_check;
7225 return cp_fold_convert (type, expr);
7228 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7229 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7231 tree c1;
7232 tree c2;
7233 tree t1;
7234 tree t2;
7236 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7237 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7239 if (TYPE_PTRDATAMEM_P (type))
7241 t1 = (build_ptrmem_type
7242 (c1,
7243 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7244 t2 = (build_ptrmem_type
7245 (c2,
7246 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7248 else
7250 t1 = intype;
7251 t2 = type;
7253 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7255 if (!c_cast_p
7256 && check_for_casting_away_constness (intype, type,
7257 STATIC_CAST_EXPR,
7258 complain))
7259 return error_mark_node;
7260 if (processing_template_decl)
7261 return expr;
7262 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7263 c_cast_p, complain);
7267 /* [expr.static.cast]
7269 An rvalue of type "pointer to cv void" can be explicitly
7270 converted to a pointer to object type. A value of type pointer
7271 to object converted to "pointer to cv void" and back to the
7272 original pointer type will have its original value. */
7273 if (TYPE_PTR_P (intype)
7274 && VOID_TYPE_P (TREE_TYPE (intype))
7275 && TYPE_PTROB_P (type))
7277 if (!c_cast_p
7278 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7279 complain))
7280 return error_mark_node;
7281 if (processing_template_decl)
7282 return expr;
7283 return build_nop (type, expr);
7286 *valid_p = false;
7287 return error_mark_node;
7290 /* Return an expression representing static_cast<TYPE>(EXPR). */
7292 tree
7293 build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
7295 tree expr = oexpr;
7296 tree result;
7297 bool valid_p;
7299 if (type == error_mark_node || expr == error_mark_node)
7300 return error_mark_node;
7302 bool dependent = (dependent_type_p (type)
7303 || type_dependent_expression_p (expr));
7304 if (dependent)
7306 tmpl:
7307 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7308 /* We don't know if it will or will not have side effects. */
7309 TREE_SIDE_EFFECTS (expr) = 1;
7310 return convert_from_reference (expr);
7312 else if (processing_template_decl)
7313 expr = build_non_dependent_expr (expr);
7315 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7316 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7317 if (!TYPE_REF_P (type)
7318 && TREE_CODE (expr) == NOP_EXPR
7319 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7320 expr = TREE_OPERAND (expr, 0);
7322 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7323 complain);
7324 if (valid_p)
7326 if (result != error_mark_node)
7328 maybe_warn_about_useless_cast (type, expr, complain);
7329 maybe_warn_about_cast_ignoring_quals (type, complain);
7331 if (processing_template_decl)
7332 goto tmpl;
7333 return result;
7336 if (complain & tf_error)
7337 error ("invalid static_cast from type %qT to type %qT",
7338 TREE_TYPE (expr), type);
7339 return error_mark_node;
7342 /* EXPR is an expression with member function or pointer-to-member
7343 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7344 not permitted by ISO C++, but we accept it in some modes. If we
7345 are not in one of those modes, issue a diagnostic. Return the
7346 converted expression. */
7348 tree
7349 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7351 tree intype;
7352 tree decl;
7354 intype = TREE_TYPE (expr);
7355 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7356 || TREE_CODE (intype) == METHOD_TYPE);
7358 if (!(complain & tf_warning_or_error))
7359 return error_mark_node;
7361 if (pedantic || warn_pmf2ptr)
7362 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7363 "converting from %qH to %qI", intype, type);
7365 if (TREE_CODE (intype) == METHOD_TYPE)
7366 expr = build_addr_func (expr, complain);
7367 else if (TREE_CODE (expr) == PTRMEM_CST)
7368 expr = build_address (PTRMEM_CST_MEMBER (expr));
7369 else
7371 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7372 decl = build_address (decl);
7373 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7376 if (expr == error_mark_node)
7377 return error_mark_node;
7379 return build_nop (type, expr);
7382 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7383 constexpr evaluation knows to reject it. */
7385 static tree
7386 build_nop_reinterpret (tree type, tree expr)
7388 tree ret = build_nop (type, expr);
7389 if (ret != expr)
7390 REINTERPRET_CAST_P (ret) = true;
7391 return ret;
7394 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7395 If C_CAST_P is true, this reinterpret cast is being done as part of
7396 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7397 indicate whether or not reinterpret_cast was valid. */
7399 static tree
7400 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7401 bool *valid_p, tsubst_flags_t complain)
7403 tree intype;
7405 /* Assume the cast is invalid. */
7406 if (valid_p)
7407 *valid_p = true;
7409 if (type == error_mark_node || error_operand_p (expr))
7410 return error_mark_node;
7412 intype = TREE_TYPE (expr);
7414 /* Save casted types in the function's used types hash table. */
7415 used_types_insert (type);
7417 /* A prvalue of non-class type is cv-unqualified. */
7418 if (!CLASS_TYPE_P (type))
7419 type = cv_unqualified (type);
7421 /* [expr.reinterpret.cast]
7422 A glvalue expression of type T1 can be cast to the type
7423 "reference to T2" if an expression of type "pointer to T1" can be
7424 explicitly converted to the type "pointer to T2" using a
7425 reinterpret_cast. */
7426 if (TYPE_REF_P (type))
7428 if (TYPE_REF_IS_RVALUE (type))
7430 if (!obvalue_p (expr))
7431 /* Perform the temporary materialization conversion. */
7432 expr = get_target_expr_sfinae (expr, complain);
7434 else if (!lvalue_p (expr))
7436 if (complain & tf_error)
7437 error ("invalid cast of an rvalue expression of type "
7438 "%qT to type %qT",
7439 intype, type);
7440 return error_mark_node;
7443 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7444 "B" are related class types; the reinterpret_cast does not
7445 adjust the pointer. */
7446 if (TYPE_PTR_P (intype)
7447 && (complain & tf_warning)
7448 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7449 COMPARE_BASE | COMPARE_DERIVED)))
7450 warning (0, "casting %qT to %qT does not dereference pointer",
7451 intype, type);
7453 expr = cp_build_addr_expr (expr, complain);
7455 if (warn_strict_aliasing > 2)
7456 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7458 if (expr != error_mark_node)
7459 expr = build_reinterpret_cast_1
7460 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7461 valid_p, complain);
7462 if (expr != error_mark_node)
7463 /* cp_build_indirect_ref isn't right for rvalue refs. */
7464 expr = convert_from_reference (fold_convert (type, expr));
7465 return expr;
7468 /* As a G++ extension, we consider conversions from member
7469 functions, and pointers to member functions to
7470 pointer-to-function and pointer-to-void types. If
7471 -Wno-pmf-conversions has not been specified,
7472 convert_member_func_to_ptr will issue an error message. */
7473 if ((TYPE_PTRMEMFUNC_P (intype)
7474 || TREE_CODE (intype) == METHOD_TYPE)
7475 && TYPE_PTR_P (type)
7476 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7477 || VOID_TYPE_P (TREE_TYPE (type))))
7478 return convert_member_func_to_ptr (type, expr, complain);
7480 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7481 array-to-pointer, and function-to-pointer conversions are
7482 performed. */
7483 expr = decay_conversion (expr, complain);
7485 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7486 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7487 if (TREE_CODE (expr) == NOP_EXPR
7488 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7489 expr = TREE_OPERAND (expr, 0);
7491 if (error_operand_p (expr))
7492 return error_mark_node;
7494 intype = TREE_TYPE (expr);
7496 /* [expr.reinterpret.cast]
7497 A pointer can be converted to any integral type large enough to
7498 hold it. ... A value of type std::nullptr_t can be converted to
7499 an integral type; the conversion has the same meaning and
7500 validity as a conversion of (void*)0 to the integral type. */
7501 if (CP_INTEGRAL_TYPE_P (type)
7502 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7504 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7506 if (complain & tf_error)
7507 permerror (input_location, "cast from %qH to %qI loses precision",
7508 intype, type);
7509 else
7510 return error_mark_node;
7512 if (NULLPTR_TYPE_P (intype))
7513 return build_int_cst (type, 0);
7515 /* [expr.reinterpret.cast]
7516 A value of integral or enumeration type can be explicitly
7517 converted to a pointer. */
7518 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7519 /* OK */
7521 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7522 || TYPE_PTR_OR_PTRMEM_P (type))
7523 && same_type_p (type, intype))
7524 /* DR 799 */
7525 return rvalue (expr);
7526 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7528 if ((complain & tf_warning)
7529 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7530 TREE_TYPE (intype)))
7531 warning (OPT_Wcast_function_type,
7532 "cast between incompatible function types"
7533 " from %qH to %qI", intype, type);
7534 return build_nop_reinterpret (type, expr);
7536 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7538 if ((complain & tf_warning)
7539 && !cxx_safe_function_type_cast_p
7540 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7541 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7542 warning (OPT_Wcast_function_type,
7543 "cast between incompatible pointer to member types"
7544 " from %qH to %qI", intype, type);
7545 return build_nop_reinterpret (type, expr);
7547 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7548 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7550 if (!c_cast_p
7551 && check_for_casting_away_constness (intype, type,
7552 REINTERPRET_CAST_EXPR,
7553 complain))
7554 return error_mark_node;
7555 /* Warn about possible alignment problems. */
7556 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7557 && (complain & tf_warning)
7558 && !VOID_TYPE_P (type)
7559 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7560 && COMPLETE_TYPE_P (TREE_TYPE (type))
7561 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7562 && min_align_of_type (TREE_TYPE (type))
7563 > min_align_of_type (TREE_TYPE (intype)))
7564 warning (OPT_Wcast_align, "cast from %qH to %qI "
7565 "increases required alignment of target type", intype, type);
7567 if (warn_strict_aliasing <= 2)
7568 /* strict_aliasing_warning STRIP_NOPs its expr. */
7569 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7571 return build_nop_reinterpret (type, expr);
7573 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7574 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7576 if (complain & tf_warning)
7577 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7578 object pointer type or vice versa is conditionally-supported." */
7579 warning (OPT_Wconditionally_supported,
7580 "casting between pointer-to-function and pointer-to-object "
7581 "is conditionally-supported");
7582 return build_nop_reinterpret (type, expr);
7584 else if (VECTOR_TYPE_P (type))
7585 return convert_to_vector (type, expr);
7586 else if (VECTOR_TYPE_P (intype)
7587 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7588 return convert_to_integer_nofold (type, expr);
7589 else
7591 if (valid_p)
7592 *valid_p = false;
7593 if (complain & tf_error)
7594 error ("invalid cast from type %qT to type %qT", intype, type);
7595 return error_mark_node;
7598 expr = cp_convert (type, expr, complain);
7599 if (TREE_CODE (expr) == NOP_EXPR)
7600 /* Mark any nop_expr that created as a reintepret_cast. */
7601 REINTERPRET_CAST_P (expr) = true;
7602 return expr;
7605 tree
7606 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7608 tree r;
7610 if (type == error_mark_node || expr == error_mark_node)
7611 return error_mark_node;
7613 if (processing_template_decl)
7615 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7617 if (!TREE_SIDE_EFFECTS (t)
7618 && type_dependent_expression_p (expr))
7619 /* There might turn out to be side effects inside expr. */
7620 TREE_SIDE_EFFECTS (t) = 1;
7621 return convert_from_reference (t);
7624 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7625 /*valid_p=*/NULL, complain);
7626 if (r != error_mark_node)
7628 maybe_warn_about_useless_cast (type, expr, complain);
7629 maybe_warn_about_cast_ignoring_quals (type, complain);
7631 return r;
7634 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7635 return an appropriate expression. Otherwise, return
7636 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7637 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7638 performing a C-style cast, its value upon return will indicate
7639 whether or not the conversion succeeded. */
7641 static tree
7642 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7643 bool *valid_p)
7645 tree src_type;
7646 tree reference_type;
7648 /* Callers are responsible for handling error_mark_node as a
7649 destination type. */
7650 gcc_assert (dst_type != error_mark_node);
7651 /* In a template, callers should be building syntactic
7652 representations of casts, not using this machinery. */
7653 gcc_assert (!processing_template_decl);
7655 /* Assume the conversion is invalid. */
7656 if (valid_p)
7657 *valid_p = false;
7659 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7661 if (complain & tf_error)
7662 error ("invalid use of const_cast with type %qT, "
7663 "which is not a pointer, "
7664 "reference, nor a pointer-to-data-member type", dst_type);
7665 return error_mark_node;
7668 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7670 if (complain & tf_error)
7671 error ("invalid use of const_cast with type %qT, which is a pointer "
7672 "or reference to a function type", dst_type);
7673 return error_mark_node;
7676 /* A prvalue of non-class type is cv-unqualified. */
7677 dst_type = cv_unqualified (dst_type);
7679 /* Save casted types in the function's used types hash table. */
7680 used_types_insert (dst_type);
7682 src_type = TREE_TYPE (expr);
7683 /* Expressions do not really have reference types. */
7684 if (TYPE_REF_P (src_type))
7685 src_type = TREE_TYPE (src_type);
7687 /* [expr.const.cast]
7689 For two object types T1 and T2, if a pointer to T1 can be explicitly
7690 converted to the type "pointer to T2" using a const_cast, then the
7691 following conversions can also be made:
7693 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7694 type T2 using the cast const_cast<T2&>;
7696 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7697 type T2 using the cast const_cast<T2&&>; and
7699 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7700 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7702 if (TYPE_REF_P (dst_type))
7704 reference_type = dst_type;
7705 if (!TYPE_REF_IS_RVALUE (dst_type)
7706 ? lvalue_p (expr)
7707 : obvalue_p (expr))
7708 /* OK. */;
7709 else
7711 if (complain & tf_error)
7712 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7713 src_type, dst_type);
7714 return error_mark_node;
7716 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7717 src_type = build_pointer_type (src_type);
7719 else
7721 reference_type = NULL_TREE;
7722 /* If the destination type is not a reference type, the
7723 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7724 conversions are performed. */
7725 src_type = type_decays_to (src_type);
7726 if (src_type == error_mark_node)
7727 return error_mark_node;
7730 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7732 if (comp_ptr_ttypes_const (dst_type, src_type))
7734 if (valid_p)
7736 *valid_p = true;
7737 /* This cast is actually a C-style cast. Issue a warning if
7738 the user is making a potentially unsafe cast. */
7739 check_for_casting_away_constness (src_type, dst_type,
7740 CAST_EXPR, complain);
7741 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7742 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7743 && (complain & tf_warning)
7744 && min_align_of_type (TREE_TYPE (dst_type))
7745 > min_align_of_type (TREE_TYPE (src_type)))
7746 warning (OPT_Wcast_align, "cast from %qH to %qI "
7747 "increases required alignment of target type",
7748 src_type, dst_type);
7750 if (reference_type)
7752 expr = cp_build_addr_expr (expr, complain);
7753 if (expr == error_mark_node)
7754 return error_mark_node;
7755 expr = build_nop (reference_type, expr);
7756 return convert_from_reference (expr);
7758 else
7760 expr = decay_conversion (expr, complain);
7761 if (expr == error_mark_node)
7762 return error_mark_node;
7764 /* build_c_cast puts on a NOP_EXPR to make the result not an
7765 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7766 non-lvalue context. */
7767 if (TREE_CODE (expr) == NOP_EXPR
7768 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7769 expr = TREE_OPERAND (expr, 0);
7770 return build_nop (dst_type, expr);
7773 else if (valid_p
7774 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7775 TREE_TYPE (src_type)))
7776 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7777 complain);
7780 if (complain & tf_error)
7781 error ("invalid const_cast from type %qT to type %qT",
7782 src_type, dst_type);
7783 return error_mark_node;
7786 tree
7787 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7789 tree r;
7791 if (type == error_mark_node || error_operand_p (expr))
7792 return error_mark_node;
7794 if (processing_template_decl)
7796 tree t = build_min (CONST_CAST_EXPR, type, expr);
7798 if (!TREE_SIDE_EFFECTS (t)
7799 && type_dependent_expression_p (expr))
7800 /* There might turn out to be side effects inside expr. */
7801 TREE_SIDE_EFFECTS (t) = 1;
7802 return convert_from_reference (t);
7805 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7806 if (r != error_mark_node)
7808 maybe_warn_about_useless_cast (type, expr, complain);
7809 maybe_warn_about_cast_ignoring_quals (type, complain);
7811 return r;
7814 /* Like cp_build_c_cast, but for the c-common bits. */
7816 tree
7817 build_c_cast (location_t /*loc*/, tree type, tree expr)
7819 return cp_build_c_cast (type, expr, tf_warning_or_error);
7822 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7823 preserve location information even for tree nodes that don't
7824 support it. */
7826 cp_expr
7827 build_c_cast (location_t loc, tree type, cp_expr expr)
7829 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7830 result.set_location (loc);
7831 return result;
7834 /* Build an expression representing an explicit C-style cast to type
7835 TYPE of expression EXPR. */
7837 tree
7838 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7840 tree value = expr;
7841 tree result;
7842 bool valid_p;
7844 if (type == error_mark_node || error_operand_p (expr))
7845 return error_mark_node;
7847 if (processing_template_decl)
7849 tree t = build_min (CAST_EXPR, type,
7850 tree_cons (NULL_TREE, value, NULL_TREE));
7851 /* We don't know if it will or will not have side effects. */
7852 TREE_SIDE_EFFECTS (t) = 1;
7853 return convert_from_reference (t);
7856 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7857 'Class') should always be retained, because this information aids
7858 in method lookup. */
7859 if (objc_is_object_ptr (type)
7860 && objc_is_object_ptr (TREE_TYPE (expr)))
7861 return build_nop (type, expr);
7863 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7864 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7865 if (!TYPE_REF_P (type)
7866 && TREE_CODE (value) == NOP_EXPR
7867 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7868 value = TREE_OPERAND (value, 0);
7870 if (TREE_CODE (type) == ARRAY_TYPE)
7872 /* Allow casting from T1* to T2[] because Cfront allows it.
7873 NIHCL uses it. It is not valid ISO C++ however. */
7874 if (TYPE_PTR_P (TREE_TYPE (expr)))
7876 if (complain & tf_error)
7877 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7878 else
7879 return error_mark_node;
7880 type = build_pointer_type (TREE_TYPE (type));
7882 else
7884 if (complain & tf_error)
7885 error ("ISO C++ forbids casting to an array type %qT", type);
7886 return error_mark_node;
7890 if (TREE_CODE (type) == FUNCTION_TYPE
7891 || TREE_CODE (type) == METHOD_TYPE)
7893 if (complain & tf_error)
7894 error ("invalid cast to function type %qT", type);
7895 return error_mark_node;
7898 if (TYPE_PTR_P (type)
7899 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7900 /* Casting to an integer of smaller size is an error detected elsewhere. */
7901 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7902 /* Don't warn about converting any constant. */
7903 && !TREE_CONSTANT (value))
7904 warning_at (input_location, OPT_Wint_to_pointer_cast,
7905 "cast to pointer from integer of different size");
7907 /* A C-style cast can be a const_cast. */
7908 result = build_const_cast_1 (type, value, complain & tf_warning,
7909 &valid_p);
7910 if (valid_p)
7912 if (result != error_mark_node)
7914 maybe_warn_about_useless_cast (type, value, complain);
7915 maybe_warn_about_cast_ignoring_quals (type, complain);
7917 return result;
7920 /* Or a static cast. */
7921 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7922 &valid_p, complain);
7923 /* Or a reinterpret_cast. */
7924 if (!valid_p)
7925 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7926 &valid_p, complain);
7927 /* The static_cast or reinterpret_cast may be followed by a
7928 const_cast. */
7929 if (valid_p
7930 /* A valid cast may result in errors if, for example, a
7931 conversion to an ambiguous base class is required. */
7932 && !error_operand_p (result))
7934 tree result_type;
7936 maybe_warn_about_useless_cast (type, value, complain);
7937 maybe_warn_about_cast_ignoring_quals (type, complain);
7939 /* Non-class rvalues always have cv-unqualified type. */
7940 if (!CLASS_TYPE_P (type))
7941 type = TYPE_MAIN_VARIANT (type);
7942 result_type = TREE_TYPE (result);
7943 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
7944 result_type = TYPE_MAIN_VARIANT (result_type);
7945 /* If the type of RESULT does not match TYPE, perform a
7946 const_cast to make it match. If the static_cast or
7947 reinterpret_cast succeeded, we will differ by at most
7948 cv-qualification, so the follow-on const_cast is guaranteed
7949 to succeed. */
7950 if (!same_type_p (non_reference (type), non_reference (result_type)))
7952 result = build_const_cast_1 (type, result, false, &valid_p);
7953 gcc_assert (valid_p);
7955 return result;
7958 return error_mark_node;
7961 /* For use from the C common bits. */
7962 tree
7963 build_modify_expr (location_t location,
7964 tree lhs, tree /*lhs_origtype*/,
7965 enum tree_code modifycode,
7966 location_t /*rhs_location*/, tree rhs,
7967 tree /*rhs_origtype*/)
7969 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7970 tf_warning_or_error);
7973 /* Build an assignment expression of lvalue LHS from value RHS.
7974 MODIFYCODE is the code for a binary operator that we use
7975 to combine the old value of LHS with RHS to get the new value.
7976 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7978 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7980 tree
7981 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7982 tree rhs, tsubst_flags_t complain)
7984 lhs = mark_lvalue_use_nonread (lhs);
7986 tree result = NULL_TREE;
7987 tree newrhs = rhs;
7988 tree lhstype = TREE_TYPE (lhs);
7989 tree olhs = lhs;
7990 tree olhstype = lhstype;
7991 bool plain_assign = (modifycode == NOP_EXPR);
7992 bool compound_side_effects_p = false;
7993 tree preeval = NULL_TREE;
7995 /* Avoid duplicate error messages from operands that had errors. */
7996 if (error_operand_p (lhs) || error_operand_p (rhs))
7997 return error_mark_node;
7999 while (TREE_CODE (lhs) == COMPOUND_EXPR)
8001 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
8002 compound_side_effects_p = true;
8003 lhs = TREE_OPERAND (lhs, 1);
8006 /* Handle control structure constructs used as "lvalues". Note that we
8007 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8008 switch (TREE_CODE (lhs))
8010 /* Handle --foo = 5; as these are valid constructs in C++. */
8011 case PREDECREMENT_EXPR:
8012 case PREINCREMENT_EXPR:
8013 if (compound_side_effects_p)
8014 newrhs = rhs = stabilize_expr (rhs, &preeval);
8015 lhs = genericize_compound_lvalue (lhs);
8016 maybe_add_compound:
8017 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8018 and looked through the COMPOUND_EXPRs, readd them now around
8019 the resulting lhs. */
8020 if (TREE_CODE (olhs) == COMPOUND_EXPR)
8022 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
8023 tree *ptr = &TREE_OPERAND (lhs, 1);
8024 for (olhs = TREE_OPERAND (olhs, 1);
8025 TREE_CODE (olhs) == COMPOUND_EXPR;
8026 olhs = TREE_OPERAND (olhs, 1))
8028 *ptr = build2 (COMPOUND_EXPR, lhstype,
8029 TREE_OPERAND (olhs, 0), *ptr);
8030 ptr = &TREE_OPERAND (*ptr, 1);
8033 break;
8035 case MODIFY_EXPR:
8036 if (compound_side_effects_p)
8037 newrhs = rhs = stabilize_expr (rhs, &preeval);
8038 lhs = genericize_compound_lvalue (lhs);
8039 goto maybe_add_compound;
8041 case MIN_EXPR:
8042 case MAX_EXPR:
8043 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8044 when neither operand has side-effects. */
8045 if (!lvalue_or_else (lhs, lv_assign, complain))
8046 return error_mark_node;
8048 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
8049 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
8051 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
8052 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
8053 boolean_type_node,
8054 TREE_OPERAND (lhs, 0),
8055 TREE_OPERAND (lhs, 1)),
8056 TREE_OPERAND (lhs, 0),
8057 TREE_OPERAND (lhs, 1));
8058 gcc_fallthrough ();
8060 /* Handle (a ? b : c) used as an "lvalue". */
8061 case COND_EXPR:
8063 /* Produce (a ? (b = rhs) : (c = rhs))
8064 except that the RHS goes through a save-expr
8065 so the code to compute it is only emitted once. */
8066 tree cond;
8068 if (VOID_TYPE_P (TREE_TYPE (rhs)))
8070 if (complain & tf_error)
8071 error ("void value not ignored as it ought to be");
8072 return error_mark_node;
8075 rhs = stabilize_expr (rhs, &preeval);
8077 /* Check this here to avoid odd errors when trying to convert
8078 a throw to the type of the COND_EXPR. */
8079 if (!lvalue_or_else (lhs, lv_assign, complain))
8080 return error_mark_node;
8082 cond = build_conditional_expr
8083 (input_location, TREE_OPERAND (lhs, 0),
8084 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
8085 modifycode, rhs, complain),
8086 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
8087 modifycode, rhs, complain),
8088 complain);
8090 if (cond == error_mark_node)
8091 return cond;
8092 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8093 and looked through the COMPOUND_EXPRs, readd them now around
8094 the resulting cond before adding the preevaluated rhs. */
8095 if (TREE_CODE (olhs) == COMPOUND_EXPR)
8097 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8098 TREE_OPERAND (olhs, 0), cond);
8099 tree *ptr = &TREE_OPERAND (cond, 1);
8100 for (olhs = TREE_OPERAND (olhs, 1);
8101 TREE_CODE (olhs) == COMPOUND_EXPR;
8102 olhs = TREE_OPERAND (olhs, 1))
8104 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8105 TREE_OPERAND (olhs, 0), *ptr);
8106 ptr = &TREE_OPERAND (*ptr, 1);
8109 /* Make sure the code to compute the rhs comes out
8110 before the split. */
8111 result = cond;
8112 goto ret;
8115 default:
8116 lhs = olhs;
8117 break;
8120 if (modifycode == INIT_EXPR)
8122 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8123 /* Do the default thing. */;
8124 else if (TREE_CODE (rhs) == CONSTRUCTOR)
8126 /* Compound literal. */
8127 if (! same_type_p (TREE_TYPE (rhs), lhstype))
8128 /* Call convert to generate an error; see PR 11063. */
8129 rhs = convert (lhstype, rhs);
8130 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8131 TREE_SIDE_EFFECTS (result) = 1;
8132 goto ret;
8134 else if (! MAYBE_CLASS_TYPE_P (lhstype))
8135 /* Do the default thing. */;
8136 else
8138 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8139 result = build_special_member_call (lhs, complete_ctor_identifier,
8140 &rhs_vec, lhstype, LOOKUP_NORMAL,
8141 complain);
8142 release_tree_vector (rhs_vec);
8143 if (result == NULL_TREE)
8144 return error_mark_node;
8145 goto ret;
8148 else
8150 lhs = require_complete_type_sfinae (lhs, complain);
8151 if (lhs == error_mark_node)
8152 return error_mark_node;
8154 if (modifycode == NOP_EXPR)
8156 if (c_dialect_objc ())
8158 result = objc_maybe_build_modify_expr (lhs, rhs);
8159 if (result)
8160 goto ret;
8163 /* `operator=' is not an inheritable operator. */
8164 if (! MAYBE_CLASS_TYPE_P (lhstype))
8165 /* Do the default thing. */;
8166 else
8168 result = build_new_op (input_location, MODIFY_EXPR,
8169 LOOKUP_NORMAL, lhs, rhs,
8170 make_node (NOP_EXPR), /*overload=*/NULL,
8171 complain);
8172 if (result == NULL_TREE)
8173 return error_mark_node;
8174 goto ret;
8176 lhstype = olhstype;
8178 else
8180 tree init = NULL_TREE;
8182 /* A binary op has been requested. Combine the old LHS
8183 value with the RHS producing the value we should actually
8184 store into the LHS. */
8185 gcc_assert (!((TYPE_REF_P (lhstype)
8186 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8187 || MAYBE_CLASS_TYPE_P (lhstype)));
8189 /* Preevaluate the RHS to make sure its evaluation is complete
8190 before the lvalue-to-rvalue conversion of the LHS:
8192 [expr.ass] With respect to an indeterminately-sequenced
8193 function call, the operation of a compound assignment is a
8194 single evaluation. [ Note: Therefore, a function call shall
8195 not intervene between the lvalue-to-rvalue conversion and the
8196 side effect associated with any single compound assignment
8197 operator. -- end note ] */
8198 lhs = cp_stabilize_reference (lhs);
8199 rhs = decay_conversion (rhs, complain);
8200 if (rhs == error_mark_node)
8201 return error_mark_node;
8202 rhs = stabilize_expr (rhs, &init);
8203 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8204 if (newrhs == error_mark_node)
8206 if (complain & tf_error)
8207 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8208 TREE_TYPE (lhs), TREE_TYPE (rhs));
8209 return error_mark_node;
8212 if (init)
8213 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8215 /* Now it looks like a plain assignment. */
8216 modifycode = NOP_EXPR;
8217 if (c_dialect_objc ())
8219 result = objc_maybe_build_modify_expr (lhs, newrhs);
8220 if (result)
8221 goto ret;
8224 gcc_assert (!TYPE_REF_P (lhstype));
8225 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
8228 /* The left-hand side must be an lvalue. */
8229 if (!lvalue_or_else (lhs, lv_assign, complain))
8230 return error_mark_node;
8232 /* Warn about modifying something that is `const'. Don't warn if
8233 this is initialization. */
8234 if (modifycode != INIT_EXPR
8235 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8236 /* Functions are not modifiable, even though they are
8237 lvalues. */
8238 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8239 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8240 /* If it's an aggregate and any field is const, then it is
8241 effectively const. */
8242 || (CLASS_TYPE_P (lhstype)
8243 && C_TYPE_FIELDS_READONLY (lhstype))))
8245 if (complain & tf_error)
8246 cxx_readonly_error (loc, lhs, lv_assign);
8247 return error_mark_node;
8250 /* If storing into a structure or union member, it may have been given a
8251 lowered bitfield type. We need to convert to the declared type first,
8252 so retrieve it now. */
8254 olhstype = unlowered_expr_type (lhs);
8256 /* Convert new value to destination type. */
8258 if (TREE_CODE (lhstype) == ARRAY_TYPE)
8260 int from_array;
8262 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8264 if (modifycode != INIT_EXPR)
8266 if (complain & tf_error)
8267 error ("assigning to an array from an initializer list");
8268 return error_mark_node;
8270 if (check_array_initializer (lhs, lhstype, newrhs))
8271 return error_mark_node;
8272 newrhs = digest_init (lhstype, newrhs, complain);
8273 if (newrhs == error_mark_node)
8274 return error_mark_node;
8277 /* C++11 8.5/17: "If the destination type is an array of characters,
8278 an array of char16_t, an array of char32_t, or an array of wchar_t,
8279 and the initializer is a string literal...". */
8280 else if (TREE_CODE (newrhs) == STRING_CST
8281 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8282 && modifycode == INIT_EXPR)
8284 newrhs = digest_init (lhstype, newrhs, complain);
8285 if (newrhs == error_mark_node)
8286 return error_mark_node;
8289 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8290 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8292 if (complain & tf_error)
8293 error ("incompatible types in assignment of %qT to %qT",
8294 TREE_TYPE (rhs), lhstype);
8295 return error_mark_node;
8298 /* Allow array assignment in compiler-generated code. */
8299 else if (!current_function_decl
8300 || !DECL_DEFAULTED_FN (current_function_decl))
8302 /* This routine is used for both initialization and assignment.
8303 Make sure the diagnostic message differentiates the context. */
8304 if (complain & tf_error)
8306 if (modifycode == INIT_EXPR)
8307 error ("array used as initializer");
8308 else
8309 error ("invalid array assignment");
8311 return error_mark_node;
8314 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8315 ? 1 + (modifycode != INIT_EXPR): 0;
8316 result = build_vec_init (lhs, NULL_TREE, newrhs,
8317 /*explicit_value_init_p=*/false,
8318 from_array, complain);
8319 goto ret;
8322 if (modifycode == INIT_EXPR)
8323 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8324 LOOKUP_ONLYCONVERTING. */
8325 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8326 ICR_INIT, NULL_TREE, 0,
8327 complain);
8328 else
8329 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8330 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8332 if (!same_type_p (lhstype, olhstype))
8333 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8335 if (modifycode != INIT_EXPR)
8337 if (TREE_CODE (newrhs) == CALL_EXPR
8338 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8339 newrhs = build_cplus_new (lhstype, newrhs, complain);
8341 /* Can't initialize directly from a TARGET_EXPR, since that would
8342 cause the lhs to be constructed twice, and possibly result in
8343 accidental self-initialization. So we force the TARGET_EXPR to be
8344 expanded without a target. */
8345 if (TREE_CODE (newrhs) == TARGET_EXPR)
8346 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8347 TREE_OPERAND (newrhs, 0));
8350 if (newrhs == error_mark_node)
8351 return error_mark_node;
8353 if (c_dialect_objc () && flag_objc_gc)
8355 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8357 if (result)
8358 goto ret;
8361 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8362 lhstype, lhs, newrhs);
8364 TREE_SIDE_EFFECTS (result) = 1;
8365 if (!plain_assign)
8366 TREE_NO_WARNING (result) = 1;
8368 ret:
8369 if (preeval)
8370 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8371 return result;
8374 cp_expr
8375 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8376 tree rhs, tsubst_flags_t complain)
8378 tree orig_lhs = lhs;
8379 tree orig_rhs = rhs;
8380 tree overload = NULL_TREE;
8381 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8383 if (processing_template_decl)
8385 if (modifycode == NOP_EXPR
8386 || type_dependent_expression_p (lhs)
8387 || type_dependent_expression_p (rhs))
8388 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8389 build_min_nt_loc (loc, modifycode, NULL_TREE,
8390 NULL_TREE), rhs);
8392 lhs = build_non_dependent_expr (lhs);
8393 rhs = build_non_dependent_expr (rhs);
8396 if (modifycode != NOP_EXPR)
8398 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8399 lhs, rhs, op, &overload, complain);
8400 if (rval)
8402 if (rval == error_mark_node)
8403 return rval;
8404 TREE_NO_WARNING (rval) = 1;
8405 if (processing_template_decl)
8407 if (overload != NULL_TREE)
8408 return (build_min_non_dep_op_overload
8409 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8411 return (build_min_non_dep
8412 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8414 return rval;
8417 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8420 /* Helper function for get_delta_difference which assumes FROM is a base
8421 class of TO. Returns a delta for the conversion of pointer-to-member
8422 of FROM to pointer-to-member of TO. If the conversion is invalid and
8423 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8424 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8425 If C_CAST_P is true, this conversion is taking place as part of a
8426 C-style cast. */
8428 static tree
8429 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8430 tsubst_flags_t complain)
8432 tree binfo;
8433 base_kind kind;
8435 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8436 &kind, complain);
8438 if (binfo == error_mark_node)
8440 if (!(complain & tf_error))
8441 return error_mark_node;
8443 error (" in pointer to member function conversion");
8444 return size_zero_node;
8446 else if (binfo)
8448 if (kind != bk_via_virtual)
8449 return BINFO_OFFSET (binfo);
8450 else
8451 /* FROM is a virtual base class of TO. Issue an error or warning
8452 depending on whether or not this is a reinterpret cast. */
8454 if (!(complain & tf_error))
8455 return error_mark_node;
8457 error ("pointer to member conversion via virtual base %qT",
8458 BINFO_TYPE (binfo_from_vbase (binfo)));
8460 return size_zero_node;
8463 else
8464 return NULL_TREE;
8467 /* Get difference in deltas for different pointer to member function
8468 types. If the conversion is invalid and tf_error is not set in
8469 COMPLAIN, returns error_mark_node, otherwise returns an integer
8470 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8471 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8472 conversions as well. If C_CAST_P is true this conversion is taking
8473 place as part of a C-style cast.
8475 Note that the naming of FROM and TO is kind of backwards; the return
8476 value is what we add to a TO in order to get a FROM. They are named
8477 this way because we call this function to find out how to convert from
8478 a pointer to member of FROM to a pointer to member of TO. */
8480 static tree
8481 get_delta_difference (tree from, tree to,
8482 bool allow_inverse_p,
8483 bool c_cast_p, tsubst_flags_t complain)
8485 tree result;
8487 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8488 /* Pointer to member of incomplete class is permitted*/
8489 result = size_zero_node;
8490 else
8491 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8493 if (result == error_mark_node)
8494 return error_mark_node;
8496 if (!result)
8498 if (!allow_inverse_p)
8500 if (!(complain & tf_error))
8501 return error_mark_node;
8503 error_not_base_type (from, to);
8504 error (" in pointer to member conversion");
8505 result = size_zero_node;
8507 else
8509 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8511 if (result == error_mark_node)
8512 return error_mark_node;
8514 if (result)
8515 result = size_diffop_loc (input_location,
8516 size_zero_node, result);
8517 else
8519 if (!(complain & tf_error))
8520 return error_mark_node;
8522 error_not_base_type (from, to);
8523 error (" in pointer to member conversion");
8524 result = size_zero_node;
8529 return convert_to_integer (ptrdiff_type_node, result);
8532 /* Return a constructor for the pointer-to-member-function TYPE using
8533 the other components as specified. */
8535 tree
8536 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8538 tree u = NULL_TREE;
8539 tree delta_field;
8540 tree pfn_field;
8541 vec<constructor_elt, va_gc> *v;
8543 /* Pull the FIELD_DECLs out of the type. */
8544 pfn_field = TYPE_FIELDS (type);
8545 delta_field = DECL_CHAIN (pfn_field);
8547 /* Make sure DELTA has the type we want. */
8548 delta = convert_and_check (input_location, delta_type_node, delta);
8550 /* Convert to the correct target type if necessary. */
8551 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8553 /* Finish creating the initializer. */
8554 vec_alloc (v, 2);
8555 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8556 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8557 u = build_constructor (type, v);
8558 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8559 TREE_STATIC (u) = (TREE_CONSTANT (u)
8560 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8561 != NULL_TREE)
8562 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8563 != NULL_TREE));
8564 return u;
8567 /* Build a constructor for a pointer to member function. It can be
8568 used to initialize global variables, local variable, or used
8569 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8570 want to be.
8572 If FORCE is nonzero, then force this conversion, even if
8573 we would rather not do it. Usually set when using an explicit
8574 cast. A C-style cast is being processed iff C_CAST_P is true.
8576 Return error_mark_node, if something goes wrong. */
8578 tree
8579 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8580 tsubst_flags_t complain)
8582 tree fn;
8583 tree pfn_type;
8584 tree to_type;
8586 if (error_operand_p (pfn))
8587 return error_mark_node;
8589 pfn_type = TREE_TYPE (pfn);
8590 to_type = build_ptrmemfunc_type (type);
8592 /* Handle multiple conversions of pointer to member functions. */
8593 if (TYPE_PTRMEMFUNC_P (pfn_type))
8595 tree delta = NULL_TREE;
8596 tree npfn = NULL_TREE;
8597 tree n;
8599 if (!force
8600 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8601 LOOKUP_NORMAL, complain))
8603 if (complain & tf_error)
8604 error ("invalid conversion to type %qT from type %qT",
8605 to_type, pfn_type);
8606 else
8607 return error_mark_node;
8610 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8611 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8612 force,
8613 c_cast_p, complain);
8614 if (n == error_mark_node)
8615 return error_mark_node;
8617 /* We don't have to do any conversion to convert a
8618 pointer-to-member to its own type. But, we don't want to
8619 just return a PTRMEM_CST if there's an explicit cast; that
8620 cast should make the expression an invalid template argument. */
8621 if (TREE_CODE (pfn) != PTRMEM_CST)
8623 if (same_type_p (to_type, pfn_type))
8624 return pfn;
8625 else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
8626 return build_reinterpret_cast (to_type, pfn,
8627 complain);
8630 if (TREE_SIDE_EFFECTS (pfn))
8631 pfn = save_expr (pfn);
8633 /* Obtain the function pointer and the current DELTA. */
8634 if (TREE_CODE (pfn) == PTRMEM_CST)
8635 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8636 else
8638 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8639 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8642 /* Just adjust the DELTA field. */
8643 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8644 (TREE_TYPE (delta), ptrdiff_type_node));
8645 if (!integer_zerop (n))
8647 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8648 n = cp_build_binary_op (input_location,
8649 LSHIFT_EXPR, n, integer_one_node,
8650 complain);
8651 delta = cp_build_binary_op (input_location,
8652 PLUS_EXPR, delta, n, complain);
8654 return build_ptrmemfunc1 (to_type, delta, npfn);
8657 /* Handle null pointer to member function conversions. */
8658 if (null_ptr_cst_p (pfn))
8660 pfn = cp_build_c_cast (type, pfn, complain);
8661 return build_ptrmemfunc1 (to_type,
8662 integer_zero_node,
8663 pfn);
8666 if (type_unknown_p (pfn))
8667 return instantiate_type (type, pfn, complain);
8669 fn = TREE_OPERAND (pfn, 0);
8670 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8671 /* In a template, we will have preserved the
8672 OFFSET_REF. */
8673 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8674 return make_ptrmem_cst (to_type, fn);
8677 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8678 given by CST.
8680 ??? There is no consistency as to the types returned for the above
8681 values. Some code acts as if it were a sizetype and some as if it were
8682 integer_type_node. */
8684 void
8685 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8687 tree type = TREE_TYPE (cst);
8688 tree fn = PTRMEM_CST_MEMBER (cst);
8689 tree ptr_class, fn_class;
8691 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8693 /* The class that the function belongs to. */
8694 fn_class = DECL_CONTEXT (fn);
8696 /* The class that we're creating a pointer to member of. */
8697 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8699 /* First, calculate the adjustment to the function's class. */
8700 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8701 /*c_cast_p=*/0, tf_warning_or_error);
8703 if (!DECL_VIRTUAL_P (fn))
8704 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8705 build_addr_func (fn, tf_warning_or_error));
8706 else
8708 /* If we're dealing with a virtual function, we have to adjust 'this'
8709 again, to point to the base which provides the vtable entry for
8710 fn; the call will do the opposite adjustment. */
8711 tree orig_class = DECL_CONTEXT (fn);
8712 tree binfo = binfo_or_else (orig_class, fn_class);
8713 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8714 *delta, BINFO_OFFSET (binfo));
8716 /* We set PFN to the vtable offset at which the function can be
8717 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8718 case delta is shifted left, and then incremented). */
8719 *pfn = DECL_VINDEX (fn);
8720 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8721 TYPE_SIZE_UNIT (vtable_entry_type));
8723 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8725 case ptrmemfunc_vbit_in_pfn:
8726 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8727 integer_one_node);
8728 break;
8730 case ptrmemfunc_vbit_in_delta:
8731 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8732 *delta, integer_one_node);
8733 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8734 *delta, integer_one_node);
8735 break;
8737 default:
8738 gcc_unreachable ();
8741 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8745 /* Return an expression for PFN from the pointer-to-member function
8746 given by T. */
8748 static tree
8749 pfn_from_ptrmemfunc (tree t)
8751 if (TREE_CODE (t) == PTRMEM_CST)
8753 tree delta;
8754 tree pfn;
8756 expand_ptrmemfunc_cst (t, &delta, &pfn);
8757 if (pfn)
8758 return pfn;
8761 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8764 /* Return an expression for DELTA from the pointer-to-member function
8765 given by T. */
8767 static tree
8768 delta_from_ptrmemfunc (tree t)
8770 if (TREE_CODE (t) == PTRMEM_CST)
8772 tree delta;
8773 tree pfn;
8775 expand_ptrmemfunc_cst (t, &delta, &pfn);
8776 if (delta)
8777 return delta;
8780 return build_ptrmemfunc_access_expr (t, delta_identifier);
8783 /* Convert value RHS to type TYPE as preparation for an assignment to
8784 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8785 implicit conversion is. If FNDECL is non-NULL, we are doing the
8786 conversion in order to pass the PARMNUMth argument of FNDECL.
8787 If FNDECL is NULL, we are doing the conversion in function pointer
8788 argument passing, conversion in initialization, etc. */
8790 static tree
8791 convert_for_assignment (tree type, tree rhs,
8792 impl_conv_rhs errtype, tree fndecl, int parmnum,
8793 tsubst_flags_t complain, int flags)
8795 tree rhstype;
8796 enum tree_code coder;
8798 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8799 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8800 rhs = TREE_OPERAND (rhs, 0);
8802 /* Handle [dcl.init.list] direct-list-initialization from
8803 single element of enumeration with a fixed underlying type. */
8804 if (is_direct_enum_init (type, rhs))
8806 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8807 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8809 warning_sentinel w (warn_useless_cast);
8810 warning_sentinel w2 (warn_ignored_qualifiers);
8811 rhs = cp_build_c_cast (type, elt, complain);
8813 else
8814 rhs = error_mark_node;
8817 rhstype = TREE_TYPE (rhs);
8818 coder = TREE_CODE (rhstype);
8820 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8821 && vector_types_convertible_p (type, rhstype, true))
8823 rhs = mark_rvalue_use (rhs);
8824 return convert (type, rhs);
8827 if (rhs == error_mark_node || rhstype == error_mark_node)
8828 return error_mark_node;
8829 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8830 return error_mark_node;
8832 /* The RHS of an assignment cannot have void type. */
8833 if (coder == VOID_TYPE)
8835 if (complain & tf_error)
8836 error ("void value not ignored as it ought to be");
8837 return error_mark_node;
8840 if (c_dialect_objc ())
8842 int parmno;
8843 tree selector;
8844 tree rname = fndecl;
8846 switch (errtype)
8848 case ICR_ASSIGN:
8849 parmno = -1;
8850 break;
8851 case ICR_INIT:
8852 parmno = -2;
8853 break;
8854 default:
8855 selector = objc_message_selector ();
8856 parmno = parmnum;
8857 if (selector && parmno > 1)
8859 rname = selector;
8860 parmno -= 1;
8864 if (objc_compare_types (type, rhstype, parmno, rname))
8866 rhs = mark_rvalue_use (rhs);
8867 return convert (type, rhs);
8871 /* [expr.ass]
8873 The expression is implicitly converted (clause _conv_) to the
8874 cv-unqualified type of the left operand.
8876 We allow bad conversions here because by the time we get to this point
8877 we are committed to doing the conversion. If we end up doing a bad
8878 conversion, convert_like will complain. */
8879 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8881 /* When -Wno-pmf-conversions is use, we just silently allow
8882 conversions from pointers-to-members to plain pointers. If
8883 the conversion doesn't work, cp_convert will complain. */
8884 if (!warn_pmf2ptr
8885 && TYPE_PTR_P (type)
8886 && TYPE_PTRMEMFUNC_P (rhstype))
8887 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8888 else
8890 if (complain & tf_error)
8892 /* If the right-hand side has unknown type, then it is an
8893 overloaded function. Call instantiate_type to get error
8894 messages. */
8895 if (rhstype == unknown_type_node)
8897 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8898 /* -fpermissive might allow this; recurse. */
8899 if (!seen_error ())
8900 return convert_for_assignment (type, r, errtype, fndecl,
8901 parmnum, complain, flags);
8903 else if (fndecl)
8904 complain_about_bad_argument (cp_expr_location (rhs),
8905 rhstype, type,
8906 fndecl, parmnum);
8907 else
8908 switch (errtype)
8910 case ICR_DEFAULT_ARGUMENT:
8911 error ("cannot convert %qH to %qI in default argument",
8912 rhstype, type);
8913 break;
8914 case ICR_ARGPASS:
8915 error ("cannot convert %qH to %qI in argument passing",
8916 rhstype, type);
8917 break;
8918 case ICR_CONVERTING:
8919 error ("cannot convert %qH to %qI",
8920 rhstype, type);
8921 break;
8922 case ICR_INIT:
8923 error ("cannot convert %qH to %qI in initialization",
8924 rhstype, type);
8925 break;
8926 case ICR_RETURN:
8927 error ("cannot convert %qH to %qI in return",
8928 rhstype, type);
8929 break;
8930 case ICR_ASSIGN:
8931 error ("cannot convert %qH to %qI in assignment",
8932 rhstype, type);
8933 break;
8934 default:
8935 gcc_unreachable();
8937 if (TYPE_PTR_P (rhstype)
8938 && TYPE_PTR_P (type)
8939 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8940 && CLASS_TYPE_P (TREE_TYPE (type))
8941 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8942 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8943 (TREE_TYPE (rhstype))),
8944 "class type %qT is incomplete", TREE_TYPE (rhstype));
8946 return error_mark_node;
8949 if (warn_suggest_attribute_format)
8951 const enum tree_code codel = TREE_CODE (type);
8952 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8953 && coder == codel
8954 && check_missing_format_attribute (type, rhstype)
8955 && (complain & tf_warning))
8956 switch (errtype)
8958 case ICR_ARGPASS:
8959 case ICR_DEFAULT_ARGUMENT:
8960 if (fndecl)
8961 warning (OPT_Wsuggest_attribute_format,
8962 "parameter %qP of %qD might be a candidate "
8963 "for a format attribute", parmnum, fndecl);
8964 else
8965 warning (OPT_Wsuggest_attribute_format,
8966 "parameter might be a candidate "
8967 "for a format attribute");
8968 break;
8969 case ICR_CONVERTING:
8970 warning (OPT_Wsuggest_attribute_format,
8971 "target of conversion might be a candidate "
8972 "for a format attribute");
8973 break;
8974 case ICR_INIT:
8975 warning (OPT_Wsuggest_attribute_format,
8976 "target of initialization might be a candidate "
8977 "for a format attribute");
8978 break;
8979 case ICR_RETURN:
8980 warning (OPT_Wsuggest_attribute_format,
8981 "return type might be a candidate "
8982 "for a format attribute");
8983 break;
8984 case ICR_ASSIGN:
8985 warning (OPT_Wsuggest_attribute_format,
8986 "left-hand side of assignment might be a candidate "
8987 "for a format attribute");
8988 break;
8989 default:
8990 gcc_unreachable();
8994 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8995 does not. */
8996 if (warn_parentheses
8997 && TREE_CODE (type) == BOOLEAN_TYPE
8998 && TREE_CODE (rhs) == MODIFY_EXPR
8999 && !TREE_NO_WARNING (rhs)
9000 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
9001 && (complain & tf_warning))
9003 location_t loc = cp_expr_loc_or_loc (rhs, input_location);
9005 warning_at (loc, OPT_Wparentheses,
9006 "suggest parentheses around assignment used as truth value");
9007 TREE_NO_WARNING (rhs) = 1;
9010 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
9011 complain, flags);
9014 /* Convert RHS to be of type TYPE.
9015 If EXP is nonzero, it is the target of the initialization.
9016 ERRTYPE indicates what kind of error the implicit conversion is.
9018 Two major differences between the behavior of
9019 `convert_for_assignment' and `convert_for_initialization'
9020 are that references are bashed in the former, while
9021 copied in the latter, and aggregates are assigned in
9022 the former (operator=) while initialized in the
9023 latter (X(X&)).
9025 If using constructor make sure no conversion operator exists, if one does
9026 exist, an ambiguity exists. */
9028 tree
9029 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
9030 impl_conv_rhs errtype, tree fndecl, int parmnum,
9031 tsubst_flags_t complain)
9033 enum tree_code codel = TREE_CODE (type);
9034 tree rhstype;
9035 enum tree_code coder;
9037 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9038 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
9039 if (TREE_CODE (rhs) == NOP_EXPR
9040 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
9041 && codel != REFERENCE_TYPE)
9042 rhs = TREE_OPERAND (rhs, 0);
9044 if (type == error_mark_node
9045 || rhs == error_mark_node
9046 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
9047 return error_mark_node;
9049 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
9051 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
9052 && TREE_CODE (type) != ARRAY_TYPE
9053 && (!TYPE_REF_P (type)
9054 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
9055 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
9056 && !TYPE_REFFN_P (type))
9057 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
9058 rhs = decay_conversion (rhs, complain);
9060 rhstype = TREE_TYPE (rhs);
9061 coder = TREE_CODE (rhstype);
9063 if (coder == ERROR_MARK)
9064 return error_mark_node;
9066 /* We accept references to incomplete types, so we can
9067 return here before checking if RHS is of complete type. */
9069 if (codel == REFERENCE_TYPE)
9071 /* This should eventually happen in convert_arguments. */
9072 int savew = 0, savee = 0;
9074 if (fndecl)
9075 savew = warningcount + werrorcount, savee = errorcount;
9076 rhs = initialize_reference (type, rhs, flags, complain);
9078 if (fndecl
9079 && (warningcount + werrorcount > savew || errorcount > savee))
9080 inform (DECL_SOURCE_LOCATION (fndecl),
9081 "in passing argument %P of %qD", parmnum, fndecl);
9083 return rhs;
9086 if (exp != 0)
9087 exp = require_complete_type_sfinae (exp, complain);
9088 if (exp == error_mark_node)
9089 return error_mark_node;
9091 rhstype = non_reference (rhstype);
9093 type = complete_type (type);
9095 if (DIRECT_INIT_EXPR_P (type, rhs))
9096 /* Don't try to do copy-initialization if we already have
9097 direct-initialization. */
9098 return rhs;
9100 if (MAYBE_CLASS_TYPE_P (type))
9101 return perform_implicit_conversion_flags (type, rhs, complain, flags);
9103 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
9104 complain, flags);
9107 /* If RETVAL is the address of, or a reference to, a local variable or
9108 temporary give an appropriate warning and return true. */
9110 static bool
9111 maybe_warn_about_returning_address_of_local (tree retval)
9113 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
9114 tree whats_returned = fold_for_warn (retval);
9115 location_t loc = cp_expr_loc_or_loc (retval, input_location);
9117 for (;;)
9119 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9120 whats_returned = TREE_OPERAND (whats_returned, 1);
9121 else if (CONVERT_EXPR_P (whats_returned)
9122 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9123 whats_returned = TREE_OPERAND (whats_returned, 0);
9124 else
9125 break;
9128 if (TREE_CODE (whats_returned) == TARGET_EXPR
9129 && is_std_init_list (TREE_TYPE (whats_returned)))
9131 tree init = TARGET_EXPR_INITIAL (whats_returned);
9132 if (TREE_CODE (init) == CONSTRUCTOR)
9133 /* Pull out the array address. */
9134 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
9135 else if (TREE_CODE (init) == INDIRECT_REF)
9136 /* The source of a trivial copy looks like *(T*)&var. */
9137 whats_returned = TREE_OPERAND (init, 0);
9138 else
9139 return false;
9140 STRIP_NOPS (whats_returned);
9143 /* As a special case, we handle a call to std::move or std::forward. */
9144 if (TREE_CODE (whats_returned) == CALL_EXPR
9145 && (is_std_move_p (whats_returned)
9146 || is_std_forward_p (whats_returned)))
9148 tree arg = CALL_EXPR_ARG (whats_returned, 0);
9149 return maybe_warn_about_returning_address_of_local (arg);
9152 if (TREE_CODE (whats_returned) != ADDR_EXPR)
9153 return false;
9154 whats_returned = TREE_OPERAND (whats_returned, 0);
9156 while (TREE_CODE (whats_returned) == COMPONENT_REF
9157 || TREE_CODE (whats_returned) == ARRAY_REF)
9158 whats_returned = TREE_OPERAND (whats_returned, 0);
9160 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9161 || TREE_CODE (whats_returned) == TARGET_EXPR)
9163 if (TYPE_REF_P (valtype))
9164 warning_at (loc, OPT_Wreturn_local_addr,
9165 "returning reference to temporary");
9166 else if (is_std_init_list (valtype))
9167 warning_at (loc, OPT_Winit_list_lifetime,
9168 "returning temporary initializer_list does not extend "
9169 "the lifetime of the underlying array");
9170 return true;
9173 if (DECL_P (whats_returned)
9174 && DECL_NAME (whats_returned)
9175 && DECL_FUNCTION_SCOPE_P (whats_returned)
9176 && !is_capture_proxy (whats_returned)
9177 && !(TREE_STATIC (whats_returned)
9178 || TREE_PUBLIC (whats_returned)))
9180 if (VAR_P (whats_returned)
9181 && DECL_DECOMPOSITION_P (whats_returned)
9182 && DECL_DECOMP_BASE (whats_returned)
9183 && DECL_HAS_VALUE_EXPR_P (whats_returned))
9185 /* When returning address of a structured binding, if the structured
9186 binding is not a reference, continue normally, if it is a
9187 reference, recurse on the initializer of the structured
9188 binding. */
9189 tree base = DECL_DECOMP_BASE (whats_returned);
9190 if (TYPE_REF_P (TREE_TYPE (base)))
9192 tree init = DECL_INITIAL (base);
9193 return maybe_warn_about_returning_address_of_local (init);
9196 bool w = false;
9197 auto_diagnostic_group d;
9198 if (TYPE_REF_P (valtype))
9199 w = warning_at (loc, OPT_Wreturn_local_addr,
9200 "reference to local variable %qD returned",
9201 whats_returned);
9202 else if (is_std_init_list (valtype))
9203 w = warning_at (loc, OPT_Winit_list_lifetime,
9204 "returning local initializer_list variable %qD "
9205 "does not extend the lifetime of the underlying array",
9206 whats_returned);
9207 else if (TREE_CODE (whats_returned) == LABEL_DECL)
9208 w = warning_at (loc, OPT_Wreturn_local_addr,
9209 "address of label %qD returned",
9210 whats_returned);
9211 else
9212 w = warning_at (loc, OPT_Wreturn_local_addr,
9213 "address of local variable %qD returned",
9214 whats_returned);
9215 if (w)
9216 inform (DECL_SOURCE_LOCATION (whats_returned),
9217 "declared here");
9218 return true;
9221 return false;
9224 /* Returns true if DECL is in the std namespace. */
9226 static bool
9227 decl_in_std_namespace_p (tree decl)
9229 return (decl != NULL_TREE
9230 && DECL_NAMESPACE_STD_P (decl_namespace_context (decl)));
9233 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
9235 static bool
9236 is_std_forward_p (tree fn)
9238 /* std::forward only takes one argument. */
9239 if (call_expr_nargs (fn) != 1)
9240 return false;
9242 tree fndecl = cp_get_callee_fndecl_nofold (fn);
9243 if (!decl_in_std_namespace_p (fndecl))
9244 return false;
9246 tree name = DECL_NAME (fndecl);
9247 return name && id_equal (name, "forward");
9250 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
9252 static bool
9253 is_std_move_p (tree fn)
9255 /* std::move only takes one argument. */
9256 if (call_expr_nargs (fn) != 1)
9257 return false;
9259 tree fndecl = cp_get_callee_fndecl_nofold (fn);
9260 if (!decl_in_std_namespace_p (fndecl))
9261 return false;
9263 tree name = DECL_NAME (fndecl);
9264 return name && id_equal (name, "move");
9267 /* Returns true if RETVAL is a good candidate for the NRVO as per
9268 [class.copy.elision]. FUNCTYPE is the type the function is declared
9269 to return. */
9271 static bool
9272 can_do_nrvo_p (tree retval, tree functype)
9274 tree result = DECL_RESULT (current_function_decl);
9275 return (retval != NULL_TREE
9276 && !processing_template_decl
9277 /* Must be a local, automatic variable. */
9278 && VAR_P (retval)
9279 && DECL_CONTEXT (retval) == current_function_decl
9280 && !TREE_STATIC (retval)
9281 /* And not a lambda or anonymous union proxy. */
9282 && !DECL_HAS_VALUE_EXPR_P (retval)
9283 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9284 /* The cv-unqualified type of the returned value must be the
9285 same as the cv-unqualified return type of the
9286 function. */
9287 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9288 (TYPE_MAIN_VARIANT (functype)))
9289 /* And the returned value must be non-volatile. */
9290 && !TYPE_VOLATILE (TREE_TYPE (retval)));
9293 /* Returns true if we should treat RETVAL, an expression being returned,
9294 as if it were designated by an rvalue. See [class.copy.elision].
9295 PARM_P is true if a function parameter is OK in this context. */
9297 bool
9298 treat_lvalue_as_rvalue_p (tree retval, bool parm_ok)
9300 return ((cxx_dialect != cxx98)
9301 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9302 || (parm_ok && TREE_CODE (retval) == PARM_DECL))
9303 && DECL_CONTEXT (retval) == current_function_decl
9304 && !TREE_STATIC (retval));
9307 /* Warn about wrong usage of std::move in a return statement. RETVAL
9308 is the expression we are returning; FUNCTYPE is the type the function
9309 is declared to return. */
9311 static void
9312 maybe_warn_pessimizing_move (tree retval, tree functype)
9314 if (!(warn_pessimizing_move || warn_redundant_move))
9315 return;
9317 location_t loc = cp_expr_loc_or_loc (retval, input_location);
9319 /* C++98 doesn't know move. */
9320 if (cxx_dialect < cxx11)
9321 return;
9323 /* Wait until instantiation time, since we can't gauge if we should do
9324 the NRVO until then. */
9325 if (processing_template_decl)
9326 return;
9328 /* This is only interesting for class types. */
9329 if (!CLASS_TYPE_P (functype))
9330 return;
9332 /* We're looking for *std::move<T&> (&arg). */
9333 if (REFERENCE_REF_P (retval)
9334 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9336 tree fn = TREE_OPERAND (retval, 0);
9337 if (is_std_move_p (fn))
9339 tree arg = CALL_EXPR_ARG (fn, 0);
9340 STRIP_NOPS (arg);
9341 if (TREE_CODE (arg) == ADDR_EXPR)
9342 arg = TREE_OPERAND (arg, 0);
9343 arg = convert_from_reference (arg);
9344 /* Warn if we could do copy elision were it not for the move. */
9345 if (can_do_nrvo_p (arg, functype))
9347 auto_diagnostic_group d;
9348 if (warning_at (loc, OPT_Wpessimizing_move,
9349 "moving a local object in a return statement "
9350 "prevents copy elision"))
9351 inform (loc, "remove %<std::move%> call");
9353 /* Warn if the move is redundant. It is redundant when we would
9354 do maybe-rvalue overload resolution even without std::move. */
9355 else if (treat_lvalue_as_rvalue_p (arg, /*parm_ok*/true))
9357 auto_diagnostic_group d;
9358 if (warning_at (loc, OPT_Wredundant_move,
9359 "redundant move in return statement"))
9360 inform (loc, "remove %<std::move%> call");
9366 /* Check that returning RETVAL from the current function is valid.
9367 Return an expression explicitly showing all conversions required to
9368 change RETVAL into the function return type, and to assign it to
9369 the DECL_RESULT for the function. Set *NO_WARNING to true if
9370 code reaches end of non-void function warning shouldn't be issued
9371 on this RETURN_EXPR. */
9373 tree
9374 check_return_expr (tree retval, bool *no_warning)
9376 tree result;
9377 /* The type actually returned by the function. */
9378 tree valtype;
9379 /* The type the function is declared to return, or void if
9380 the declared type is incomplete. */
9381 tree functype;
9382 int fn_returns_value_p;
9384 *no_warning = false;
9386 /* A `volatile' function is one that isn't supposed to return, ever.
9387 (This is a G++ extension, used to get better code for functions
9388 that call the `volatile' function.) */
9389 if (TREE_THIS_VOLATILE (current_function_decl))
9390 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9392 /* Check for various simple errors. */
9393 if (DECL_DESTRUCTOR_P (current_function_decl))
9395 if (retval)
9396 error ("returning a value from a destructor");
9397 return NULL_TREE;
9399 else if (DECL_CONSTRUCTOR_P (current_function_decl))
9401 if (in_function_try_handler)
9402 /* If a return statement appears in a handler of the
9403 function-try-block of a constructor, the program is ill-formed. */
9404 error ("cannot return from a handler of a function-try-block of a constructor");
9405 else if (retval)
9406 /* You can't return a value from a constructor. */
9407 error ("returning a value from a constructor");
9408 return NULL_TREE;
9411 const tree saved_retval = retval;
9413 if (processing_template_decl)
9415 current_function_returns_value = 1;
9417 if (check_for_bare_parameter_packs (retval))
9418 return error_mark_node;
9420 /* If one of the types might be void, we can't tell whether we're
9421 returning a value. */
9422 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9423 && !current_function_auto_return_pattern)
9424 || (retval != NULL_TREE
9425 && (TREE_TYPE (retval) == NULL_TREE
9426 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9427 goto dependent;
9430 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9432 /* Deduce auto return type from a return statement. */
9433 if (current_function_auto_return_pattern)
9435 tree auto_node;
9436 tree type;
9438 if (!retval && !is_auto (current_function_auto_return_pattern))
9440 /* Give a helpful error message. */
9441 error ("return-statement with no value, in function returning %qT",
9442 current_function_auto_return_pattern);
9443 inform (input_location, "only plain %<auto%> return type can be "
9444 "deduced to %<void%>");
9445 type = error_mark_node;
9447 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9449 error ("returning initializer list");
9450 type = error_mark_node;
9452 else
9454 if (!retval)
9455 retval = void_node;
9456 auto_node = type_uses_auto (current_function_auto_return_pattern);
9457 type = do_auto_deduction (current_function_auto_return_pattern,
9458 retval, auto_node);
9461 if (type == error_mark_node)
9462 /* Leave it. */;
9463 else if (functype == current_function_auto_return_pattern)
9464 apply_deduced_return_type (current_function_decl, type);
9465 else if (!same_type_p (type, functype))
9467 if (LAMBDA_FUNCTION_P (current_function_decl))
9468 error ("inconsistent types %qT and %qT deduced for "
9469 "lambda return type", functype, type);
9470 else
9471 error ("inconsistent deduction for auto return type: "
9472 "%qT and then %qT", functype, type);
9474 functype = type;
9477 result = DECL_RESULT (current_function_decl);
9478 valtype = TREE_TYPE (result);
9479 gcc_assert (valtype != NULL_TREE);
9480 fn_returns_value_p = !VOID_TYPE_P (valtype);
9482 /* Check for a return statement with no return value in a function
9483 that's supposed to return a value. */
9484 if (!retval && fn_returns_value_p)
9486 if (functype != error_mark_node)
9487 permerror (input_location, "return-statement with no value, in "
9488 "function returning %qT", valtype);
9489 /* Remember that this function did return. */
9490 current_function_returns_value = 1;
9491 /* And signal caller that TREE_NO_WARNING should be set on the
9492 RETURN_EXPR to avoid control reaches end of non-void function
9493 warnings in tree-cfg.c. */
9494 *no_warning = true;
9496 /* Check for a return statement with a value in a function that
9497 isn't supposed to return a value. */
9498 else if (retval && !fn_returns_value_p)
9500 if (VOID_TYPE_P (TREE_TYPE (retval)))
9501 /* You can return a `void' value from a function of `void'
9502 type. In that case, we have to evaluate the expression for
9503 its side-effects. */
9504 finish_expr_stmt (retval);
9505 else
9506 permerror (input_location,
9507 "return-statement with a value, in function "
9508 "returning %qT", valtype);
9509 current_function_returns_null = 1;
9511 /* There's really no value to return, after all. */
9512 return NULL_TREE;
9514 else if (!retval)
9515 /* Remember that this function can sometimes return without a
9516 value. */
9517 current_function_returns_null = 1;
9518 else
9519 /* Remember that this function did return a value. */
9520 current_function_returns_value = 1;
9522 /* Check for erroneous operands -- but after giving ourselves a
9523 chance to provide an error about returning a value from a void
9524 function. */
9525 if (error_operand_p (retval))
9527 current_function_return_value = error_mark_node;
9528 return error_mark_node;
9531 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9532 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9533 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9534 && ! flag_check_new
9535 && retval && null_ptr_cst_p (retval))
9536 warning (0, "%<operator new%> must not return NULL unless it is "
9537 "declared %<throw()%> (or -fcheck-new is in effect)");
9539 /* Effective C++ rule 15. See also start_function. */
9540 if (warn_ecpp
9541 && DECL_NAME (current_function_decl) == assign_op_identifier
9542 && !type_dependent_expression_p (retval))
9544 bool warn = true;
9546 /* The function return type must be a reference to the current
9547 class. */
9548 if (TYPE_REF_P (valtype)
9549 && same_type_ignoring_top_level_qualifiers_p
9550 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9552 /* Returning '*this' is obviously OK. */
9553 if (retval == current_class_ref)
9554 warn = false;
9555 /* If we are calling a function whose return type is the same of
9556 the current class reference, it is ok. */
9557 else if (INDIRECT_REF_P (retval)
9558 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9559 warn = false;
9562 if (warn)
9563 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9566 if (dependent_type_p (functype)
9567 || type_dependent_expression_p (retval))
9569 dependent:
9570 /* We should not have changed the return value. */
9571 gcc_assert (retval == saved_retval);
9572 return retval;
9575 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9577 [...] For a function with a class return type, if the expression
9578 in the return statement is the name of a local object, and the cv-
9579 unqualified type of the local object is the same as the function
9580 return type, an implementation is permitted to omit creating the tem-
9581 porary object to hold the function return value [...]
9583 So, if this is a value-returning function that always returns the same
9584 local variable, remember it.
9586 It might be nice to be more flexible, and choose the first suitable
9587 variable even if the function sometimes returns something else, but
9588 then we run the risk of clobbering the variable we chose if the other
9589 returned expression uses the chosen variable somehow. And people expect
9590 this restriction, anyway. (jason 2000-11-19)
9592 See finish_function and finalize_nrv for the rest of this optimization. */
9594 bool named_return_value_okay_p = can_do_nrvo_p (retval, functype);
9595 if (fn_returns_value_p && flag_elide_constructors)
9597 if (named_return_value_okay_p
9598 && (current_function_return_value == NULL_TREE
9599 || current_function_return_value == retval))
9600 current_function_return_value = retval;
9601 else
9602 current_function_return_value = error_mark_node;
9605 /* We don't need to do any conversions when there's nothing being
9606 returned. */
9607 if (!retval)
9608 return NULL_TREE;
9610 if (!named_return_value_okay_p)
9611 maybe_warn_pessimizing_move (retval, functype);
9613 /* Do any required conversions. */
9614 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9615 /* No conversions are required. */
9617 else
9619 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9621 /* The functype's return type will have been set to void, if it
9622 was an incomplete type. Just treat this as 'return;' */
9623 if (VOID_TYPE_P (functype))
9624 return error_mark_node;
9626 /* If we had an id-expression obfuscated by force_paren_expr, we need
9627 to undo it so we can try to treat it as an rvalue below. */
9628 retval = maybe_undo_parenthesized_ref (retval);
9630 if (processing_template_decl)
9631 retval = build_non_dependent_expr (retval);
9633 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9634 treated as an rvalue for the purposes of overload resolution to
9635 favor move constructors over copy constructors.
9637 Note that these conditions are similar to, but not as strict as,
9638 the conditions for the named return value optimization. */
9639 bool converted = false;
9640 if (treat_lvalue_as_rvalue_p (retval, /*parm_ok*/true)
9641 /* This is only interesting for class type. */
9642 && CLASS_TYPE_P (functype))
9644 tree moved = move (retval);
9645 moved = convert_for_initialization
9646 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9647 ICR_RETURN, NULL_TREE, 0, tf_none);
9648 if (moved != error_mark_node)
9650 retval = moved;
9651 converted = true;
9655 /* First convert the value to the function's return type, then
9656 to the type of return value's location to handle the
9657 case that functype is smaller than the valtype. */
9658 if (!converted)
9659 retval = convert_for_initialization
9660 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9661 tf_warning_or_error);
9662 retval = convert (valtype, retval);
9664 /* If the conversion failed, treat this just like `return;'. */
9665 if (retval == error_mark_node)
9666 return retval;
9667 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9668 else if (! cfun->returns_struct
9669 && TREE_CODE (retval) == TARGET_EXPR
9670 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9671 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9672 TREE_OPERAND (retval, 0));
9673 else if (!processing_template_decl
9674 && maybe_warn_about_returning_address_of_local (retval)
9675 && INDIRECT_TYPE_P (valtype))
9676 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9677 build_zero_cst (TREE_TYPE (retval)));
9680 if (processing_template_decl)
9681 return saved_retval;
9683 /* Actually copy the value returned into the appropriate location. */
9684 if (retval && retval != result)
9685 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9687 return retval;
9691 /* Returns nonzero if the pointer-type FROM can be converted to the
9692 pointer-type TO via a qualification conversion. If CONSTP is -1,
9693 then we return nonzero if the pointers are similar, and the
9694 cv-qualification signature of FROM is a proper subset of that of TO.
9696 If CONSTP is positive, then all outer pointers have been
9697 const-qualified. */
9699 static int
9700 comp_ptr_ttypes_real (tree to, tree from, int constp)
9702 bool to_more_cv_qualified = false;
9703 bool is_opaque_pointer = false;
9705 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9707 if (TREE_CODE (to) != TREE_CODE (from))
9708 return 0;
9710 if (TREE_CODE (from) == OFFSET_TYPE
9711 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9712 TYPE_OFFSET_BASETYPE (to)))
9713 return 0;
9715 /* Const and volatile mean something different for function types,
9716 so the usual checks are not appropriate. */
9717 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9719 if (!at_least_as_qualified_p (to, from))
9720 return 0;
9722 if (!at_least_as_qualified_p (from, to))
9724 if (constp == 0)
9725 return 0;
9726 to_more_cv_qualified = true;
9729 if (constp > 0)
9730 constp &= TYPE_READONLY (to);
9733 if (VECTOR_TYPE_P (to))
9734 is_opaque_pointer = vector_targets_convertible_p (to, from);
9736 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9737 return ((constp >= 0 || to_more_cv_qualified)
9738 && (is_opaque_pointer
9739 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9743 /* When comparing, say, char ** to char const **, this function takes
9744 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9745 types to this function. */
9748 comp_ptr_ttypes (tree to, tree from)
9750 return comp_ptr_ttypes_real (to, from, 1);
9753 /* Returns true iff FNTYPE is a non-class type that involves
9754 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9755 if a parameter type is ill-formed. */
9757 bool
9758 error_type_p (const_tree type)
9760 tree t;
9762 switch (TREE_CODE (type))
9764 case ERROR_MARK:
9765 return true;
9767 case POINTER_TYPE:
9768 case REFERENCE_TYPE:
9769 case OFFSET_TYPE:
9770 return error_type_p (TREE_TYPE (type));
9772 case FUNCTION_TYPE:
9773 case METHOD_TYPE:
9774 if (error_type_p (TREE_TYPE (type)))
9775 return true;
9776 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9777 if (error_type_p (TREE_VALUE (t)))
9778 return true;
9779 return false;
9781 case RECORD_TYPE:
9782 if (TYPE_PTRMEMFUNC_P (type))
9783 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9784 return false;
9786 default:
9787 return false;
9791 /* Returns true if to and from are (possibly multi-level) pointers to the same
9792 type or inheritance-related types, regardless of cv-quals. */
9794 bool
9795 ptr_reasonably_similar (const_tree to, const_tree from)
9797 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9799 /* Any target type is similar enough to void. */
9800 if (VOID_TYPE_P (to))
9801 return !error_type_p (from);
9802 if (VOID_TYPE_P (from))
9803 return !error_type_p (to);
9805 if (TREE_CODE (to) != TREE_CODE (from))
9806 return false;
9808 if (TREE_CODE (from) == OFFSET_TYPE
9809 && comptypes (TYPE_OFFSET_BASETYPE (to),
9810 TYPE_OFFSET_BASETYPE (from),
9811 COMPARE_BASE | COMPARE_DERIVED))
9812 continue;
9814 if (VECTOR_TYPE_P (to)
9815 && vector_types_convertible_p (to, from, false))
9816 return true;
9818 if (TREE_CODE (to) == INTEGER_TYPE
9819 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9820 return true;
9822 if (TREE_CODE (to) == FUNCTION_TYPE)
9823 return !error_type_p (to) && !error_type_p (from);
9825 if (!TYPE_PTR_P (to))
9827 /* When either type is incomplete avoid DERIVED_FROM_P,
9828 which may call complete_type (c++/57942). */
9829 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9830 return comptypes
9831 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9832 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9837 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9838 pointer-to-member types) are the same, ignoring cv-qualification at
9839 all levels. */
9841 bool
9842 comp_ptr_ttypes_const (tree to, tree from)
9844 bool is_opaque_pointer = false;
9846 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9848 if (TREE_CODE (to) != TREE_CODE (from))
9849 return false;
9851 if (TREE_CODE (from) == OFFSET_TYPE
9852 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9853 TYPE_OFFSET_BASETYPE (to)))
9854 continue;
9856 if (VECTOR_TYPE_P (to))
9857 is_opaque_pointer = vector_targets_convertible_p (to, from);
9859 if (!TYPE_PTR_P (to))
9860 return (is_opaque_pointer
9861 || same_type_ignoring_top_level_qualifiers_p (to, from));
9865 /* Returns the type qualifiers for this type, including the qualifiers on the
9866 elements for an array type. */
9869 cp_type_quals (const_tree type)
9871 int quals;
9872 /* This CONST_CAST is okay because strip_array_types returns its
9873 argument unmodified and we assign it to a const_tree. */
9874 type = strip_array_types (CONST_CAST_TREE (type));
9875 if (type == error_mark_node
9876 /* Quals on a FUNCTION_TYPE are memfn quals. */
9877 || TREE_CODE (type) == FUNCTION_TYPE)
9878 return TYPE_UNQUALIFIED;
9879 quals = TYPE_QUALS (type);
9880 /* METHOD and REFERENCE_TYPEs should never have quals. */
9881 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9882 && !TYPE_REF_P (type))
9883 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9884 == TYPE_UNQUALIFIED));
9885 return quals;
9888 /* Returns the function-ref-qualifier for TYPE */
9890 cp_ref_qualifier
9891 type_memfn_rqual (const_tree type)
9893 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9894 || TREE_CODE (type) == METHOD_TYPE);
9896 if (!FUNCTION_REF_QUALIFIED (type))
9897 return REF_QUAL_NONE;
9898 else if (FUNCTION_RVALUE_QUALIFIED (type))
9899 return REF_QUAL_RVALUE;
9900 else
9901 return REF_QUAL_LVALUE;
9904 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9905 METHOD_TYPE. */
9908 type_memfn_quals (const_tree type)
9910 if (TREE_CODE (type) == FUNCTION_TYPE)
9911 return TYPE_QUALS (type);
9912 else if (TREE_CODE (type) == METHOD_TYPE)
9913 return cp_type_quals (class_of_this_parm (type));
9914 else
9915 gcc_unreachable ();
9918 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9919 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9921 tree
9922 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9924 /* Could handle METHOD_TYPE here if necessary. */
9925 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9926 if (TYPE_QUALS (type) == memfn_quals
9927 && type_memfn_rqual (type) == rqual)
9928 return type;
9930 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9931 complex. */
9932 tree result = build_qualified_type (type, memfn_quals);
9933 return build_ref_qualified_type (result, rqual);
9936 /* Returns nonzero if TYPE is const or volatile. */
9938 bool
9939 cv_qualified_p (const_tree type)
9941 int quals = cp_type_quals (type);
9942 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9945 /* Returns nonzero if the TYPE contains a mutable member. */
9947 bool
9948 cp_has_mutable_p (const_tree type)
9950 /* This CONST_CAST is okay because strip_array_types returns its
9951 argument unmodified and we assign it to a const_tree. */
9952 type = strip_array_types (CONST_CAST_TREE(type));
9954 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9957 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9958 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9959 approximation. In particular, consider:
9961 int f();
9962 struct S { int i; };
9963 const S s = { f(); }
9965 Here, we will make "s" as TREE_READONLY (because it is declared
9966 "const") -- only to reverse ourselves upon seeing that the
9967 initializer is non-constant. */
9969 void
9970 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9972 tree type = TREE_TYPE (decl);
9974 if (type == error_mark_node)
9975 return;
9977 if (TREE_CODE (decl) == TYPE_DECL)
9978 return;
9980 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9981 && type_quals != TYPE_UNQUALIFIED));
9983 /* Avoid setting TREE_READONLY incorrectly. */
9984 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9985 constructor can produce constant init, so rely on cp_finish_decl to
9986 clear TREE_READONLY if the variable has non-constant init. */
9988 /* If the type has (or might have) a mutable component, that component
9989 might be modified. */
9990 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9991 type_quals &= ~TYPE_QUAL_CONST;
9993 c_apply_type_quals_to_decl (type_quals, decl);
9996 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9997 exemplar types such that casting T1 to T2 is casting away constness
9998 if and only if there is no implicit conversion from T1 to T2. */
10000 static void
10001 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
10003 int quals1;
10004 int quals2;
10006 /* [expr.const.cast]
10008 For multi-level pointer to members and multi-level mixed pointers
10009 and pointers to members (conv.qual), the "member" aspect of a
10010 pointer to member level is ignored when determining if a const
10011 cv-qualifier has been cast away. */
10012 /* [expr.const.cast]
10014 For two pointer types:
10016 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
10017 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
10018 K is min(N,M)
10020 casting from X1 to X2 casts away constness if, for a non-pointer
10021 type T there does not exist an implicit conversion (clause
10022 _conv_) from:
10024 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
10028 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
10029 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
10030 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
10032 *t1 = cp_build_qualified_type (void_type_node,
10033 cp_type_quals (*t1));
10034 *t2 = cp_build_qualified_type (void_type_node,
10035 cp_type_quals (*t2));
10036 return;
10039 quals1 = cp_type_quals (*t1);
10040 quals2 = cp_type_quals (*t2);
10042 if (TYPE_PTRDATAMEM_P (*t1))
10043 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
10044 else
10045 *t1 = TREE_TYPE (*t1);
10046 if (TYPE_PTRDATAMEM_P (*t2))
10047 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
10048 else
10049 *t2 = TREE_TYPE (*t2);
10051 casts_away_constness_r (t1, t2, complain);
10052 *t1 = build_pointer_type (*t1);
10053 *t2 = build_pointer_type (*t2);
10054 *t1 = cp_build_qualified_type (*t1, quals1);
10055 *t2 = cp_build_qualified_type (*t2, quals2);
10058 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
10059 constness.
10061 ??? This function returns non-zero if casting away qualifiers not
10062 just const. We would like to return to the caller exactly which
10063 qualifiers are casted away to give more accurate diagnostics.
10066 static bool
10067 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
10069 if (TYPE_REF_P (t2))
10071 /* [expr.const.cast]
10073 Casting from an lvalue of type T1 to an lvalue of type T2
10074 using a reference cast casts away constness if a cast from an
10075 rvalue of type "pointer to T1" to the type "pointer to T2"
10076 casts away constness. */
10077 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
10078 return casts_away_constness (build_pointer_type (t1),
10079 build_pointer_type (TREE_TYPE (t2)),
10080 complain);
10083 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
10084 /* [expr.const.cast]
10086 Casting from an rvalue of type "pointer to data member of X
10087 of type T1" to the type "pointer to data member of Y of type
10088 T2" casts away constness if a cast from an rvalue of type
10089 "pointer to T1" to the type "pointer to T2" casts away
10090 constness. */
10091 return casts_away_constness
10092 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
10093 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
10094 complain);
10096 /* Casting away constness is only something that makes sense for
10097 pointer or reference types. */
10098 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
10099 return false;
10101 /* Top-level qualifiers don't matter. */
10102 t1 = TYPE_MAIN_VARIANT (t1);
10103 t2 = TYPE_MAIN_VARIANT (t2);
10104 casts_away_constness_r (&t1, &t2, complain);
10105 if (!can_convert (t2, t1, complain))
10106 return true;
10108 return false;
10111 /* If T is a REFERENCE_TYPE return the type to which T refers.
10112 Otherwise, return T itself. */
10114 tree
10115 non_reference (tree t)
10117 if (t && TYPE_REF_P (t))
10118 t = TREE_TYPE (t);
10119 return t;
10123 /* Return nonzero if REF is an lvalue valid for this language;
10124 otherwise, print an error message and return zero. USE says
10125 how the lvalue is being used and so selects the error message. */
10128 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
10130 cp_lvalue_kind kind = lvalue_kind (ref);
10132 if (kind == clk_none)
10134 if (complain & tf_error)
10135 lvalue_error (input_location, use);
10136 return 0;
10138 else if (kind & (clk_rvalueref|clk_class))
10140 if (!(complain & tf_error))
10141 return 0;
10142 /* Make this a permerror because we used to accept it. */
10143 permerror (input_location, "using rvalue as lvalue");
10145 return 1;
10148 /* Return true if a user-defined literal operator is a raw operator. */
10150 bool
10151 check_raw_literal_operator (const_tree decl)
10153 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10154 tree argtype;
10155 int arity;
10156 bool maybe_raw_p = false;
10158 /* Count the number and type of arguments and check for ellipsis. */
10159 for (argtype = argtypes, arity = 0;
10160 argtype && argtype != void_list_node;
10161 ++arity, argtype = TREE_CHAIN (argtype))
10163 tree t = TREE_VALUE (argtype);
10165 if (same_type_p (t, const_string_type_node))
10166 maybe_raw_p = true;
10168 if (!argtype)
10169 return false; /* Found ellipsis. */
10171 if (!maybe_raw_p || arity != 1)
10172 return false;
10174 return true;
10178 /* Return true if a user-defined literal operator has one of the allowed
10179 argument types. */
10181 bool
10182 check_literal_operator_args (const_tree decl,
10183 bool *long_long_unsigned_p, bool *long_double_p)
10185 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10187 *long_long_unsigned_p = false;
10188 *long_double_p = false;
10189 if (processing_template_decl || processing_specialization)
10190 return argtypes == void_list_node;
10191 else
10193 tree argtype;
10194 int arity;
10195 int max_arity = 2;
10197 /* Count the number and type of arguments and check for ellipsis. */
10198 for (argtype = argtypes, arity = 0;
10199 argtype && argtype != void_list_node;
10200 argtype = TREE_CHAIN (argtype))
10202 tree t = TREE_VALUE (argtype);
10203 ++arity;
10205 if (TYPE_PTR_P (t))
10207 bool maybe_raw_p = false;
10208 t = TREE_TYPE (t);
10209 if (cp_type_quals (t) != TYPE_QUAL_CONST)
10210 return false;
10211 t = TYPE_MAIN_VARIANT (t);
10212 if ((maybe_raw_p = same_type_p (t, char_type_node))
10213 || same_type_p (t, wchar_type_node)
10214 || same_type_p (t, char16_type_node)
10215 || same_type_p (t, char32_type_node))
10217 argtype = TREE_CHAIN (argtype);
10218 if (!argtype)
10219 return false;
10220 t = TREE_VALUE (argtype);
10221 if (maybe_raw_p && argtype == void_list_node)
10222 return true;
10223 else if (same_type_p (t, size_type_node))
10225 ++arity;
10226 continue;
10228 else
10229 return false;
10232 else if (same_type_p (t, long_long_unsigned_type_node))
10234 max_arity = 1;
10235 *long_long_unsigned_p = true;
10237 else if (same_type_p (t, long_double_type_node))
10239 max_arity = 1;
10240 *long_double_p = true;
10242 else if (same_type_p (t, char_type_node))
10243 max_arity = 1;
10244 else if (same_type_p (t, wchar_type_node))
10245 max_arity = 1;
10246 else if (same_type_p (t, char16_type_node))
10247 max_arity = 1;
10248 else if (same_type_p (t, char32_type_node))
10249 max_arity = 1;
10250 else
10251 return false;
10253 if (!argtype)
10254 return false; /* Found ellipsis. */
10256 if (arity != max_arity)
10257 return false;
10259 return true;
10263 /* Always returns false since unlike C90, C++ has no concept of implicit
10264 function declarations. */
10266 bool
10267 c_decl_implicit (const_tree)
10269 return false;