Implement P0138R2, C++17 construction rules for enum class values
[official-gcc.git] / gcc / cp / typeck.c
blobb2af615d89e74a30d5bdcf0f70c302c743cf4591
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "target.h"
31 #include "cp-tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "intl.h"
35 #include "convert.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
38 #include "params.h"
39 #include "gcc-rich-location.h"
41 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
42 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
43 static tree pfn_from_ptrmemfunc (tree);
44 static tree delta_from_ptrmemfunc (tree);
45 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
46 tsubst_flags_t, int);
47 static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
48 tsubst_flags_t);
49 static tree rationalize_conditional_expr (enum tree_code, tree,
50 tsubst_flags_t);
51 static int comp_ptr_ttypes_real (tree, tree, int);
52 static bool comp_except_types (tree, tree, bool);
53 static bool comp_array_types (const_tree, const_tree, bool);
54 static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t);
55 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
56 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
57 static bool casts_away_constness (tree, tree, tsubst_flags_t);
58 static bool maybe_warn_about_returning_address_of_local (tree);
59 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
60 static void error_args_num (location_t, tree, bool);
61 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
62 tsubst_flags_t);
64 /* Do `exp = require_complete_type (exp);' to make sure exp
65 does not have an incomplete type. (That includes void types.)
66 Returns error_mark_node if the VALUE does not have
67 complete type when this function returns. */
69 tree
70 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
72 tree type;
74 if (processing_template_decl || value == error_mark_node)
75 return value;
77 if (TREE_CODE (value) == OVERLOAD)
78 type = unknown_type_node;
79 else
80 type = TREE_TYPE (value);
82 if (type == error_mark_node)
83 return error_mark_node;
85 /* First, detect a valid value with a complete type. */
86 if (COMPLETE_TYPE_P (type))
87 return value;
89 if (complete_type_or_maybe_complain (type, value, complain))
90 return value;
91 else
92 return error_mark_node;
95 tree
96 require_complete_type (tree value)
98 return require_complete_type_sfinae (value, tf_warning_or_error);
101 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
102 a template instantiation, do the instantiation. Returns TYPE,
103 whether or not it could be completed, unless something goes
104 horribly wrong, in which case the error_mark_node is returned. */
106 tree
107 complete_type (tree type)
109 if (type == NULL_TREE)
110 /* Rather than crash, we return something sure to cause an error
111 at some point. */
112 return error_mark_node;
114 if (type == error_mark_node || COMPLETE_TYPE_P (type))
116 else if (TREE_CODE (type) == ARRAY_TYPE)
118 tree t = complete_type (TREE_TYPE (type));
119 unsigned int needs_constructing, has_nontrivial_dtor;
120 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
121 layout_type (type);
122 needs_constructing
123 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
124 has_nontrivial_dtor
125 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
126 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
128 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
129 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
132 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
133 instantiate_class_template (TYPE_MAIN_VARIANT (type));
135 return type;
138 /* Like complete_type, but issue an error if the TYPE cannot be completed.
139 VALUE is used for informative diagnostics.
140 Returns NULL_TREE if the type cannot be made complete. */
142 tree
143 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
145 type = complete_type (type);
146 if (type == error_mark_node)
147 /* We already issued an error. */
148 return NULL_TREE;
149 else if (!COMPLETE_TYPE_P (type))
151 if (complain & tf_error)
152 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
153 return NULL_TREE;
155 else
156 return type;
159 tree
160 complete_type_or_else (tree type, tree value)
162 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
165 /* Return truthvalue of whether type of EXP is instantiated. */
168 type_unknown_p (const_tree exp)
170 return (TREE_CODE (exp) == TREE_LIST
171 || TREE_TYPE (exp) == unknown_type_node);
175 /* Return the common type of two parameter lists.
176 We assume that comptypes has already been done and returned 1;
177 if that isn't so, this may crash.
179 As an optimization, free the space we allocate if the parameter
180 lists are already common. */
182 static tree
183 commonparms (tree p1, tree p2)
185 tree oldargs = p1, newargs, n;
186 int i, len;
187 int any_change = 0;
189 len = list_length (p1);
190 newargs = tree_last (p1);
192 if (newargs == void_list_node)
193 i = 1;
194 else
196 i = 0;
197 newargs = 0;
200 for (; i < len; i++)
201 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
203 n = newargs;
205 for (i = 0; p1;
206 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
208 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
210 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
211 any_change = 1;
213 else if (! TREE_PURPOSE (p1))
215 if (TREE_PURPOSE (p2))
217 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
218 any_change = 1;
221 else
223 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
224 any_change = 1;
225 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
227 if (TREE_VALUE (p1) != TREE_VALUE (p2))
229 any_change = 1;
230 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
232 else
233 TREE_VALUE (n) = TREE_VALUE (p1);
235 if (! any_change)
236 return oldargs;
238 return newargs;
241 /* Given a type, perhaps copied for a typedef,
242 find the "original" version of it. */
243 static tree
244 original_type (tree t)
246 int quals = cp_type_quals (t);
247 while (t != error_mark_node
248 && TYPE_NAME (t) != NULL_TREE)
250 tree x = TYPE_NAME (t);
251 if (TREE_CODE (x) != TYPE_DECL)
252 break;
253 x = DECL_ORIGINAL_TYPE (x);
254 if (x == NULL_TREE)
255 break;
256 t = x;
258 return cp_build_qualified_type (t, quals);
261 /* Return the common type for two arithmetic types T1 and T2 under the
262 usual arithmetic conversions. The default conversions have already
263 been applied, and enumerated types converted to their compatible
264 integer types. */
266 static tree
267 cp_common_type (tree t1, tree t2)
269 enum tree_code code1 = TREE_CODE (t1);
270 enum tree_code code2 = TREE_CODE (t2);
271 tree attributes;
272 int i;
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
277 attributes. */
278 attributes = (*targetm.merge_type_attributes) (t1, t2);
280 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
282 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283 return build_type_attribute_variant (t1, attributes);
284 else
285 return NULL_TREE;
288 /* FIXME: Attributes. */
289 gcc_assert (ARITHMETIC_TYPE_P (t1)
290 || VECTOR_TYPE_P (t1)
291 || UNSCOPED_ENUM_P (t1));
292 gcc_assert (ARITHMETIC_TYPE_P (t2)
293 || VECTOR_TYPE_P (t2)
294 || UNSCOPED_ENUM_P (t2));
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
298 required type. */
299 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
301 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
303 tree subtype
304 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
306 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307 return build_type_attribute_variant (t1, attributes);
308 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309 return build_type_attribute_variant (t2, attributes);
310 else
311 return build_type_attribute_variant (build_complex_type (subtype),
312 attributes);
315 if (code1 == VECTOR_TYPE)
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1))
320 return build_type_attribute_variant (t1, attributes);
321 else
322 return build_type_attribute_variant (t2, attributes);
325 /* If only one is real, use it as the result. */
326 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327 return build_type_attribute_variant (t1, attributes);
328 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329 return build_type_attribute_variant (t2, attributes);
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333 return build_type_attribute_variant (t1, attributes);
334 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335 return build_type_attribute_variant (t2, attributes);
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339 return build_type_attribute_variant (t1, attributes);
341 if (code1 != REAL_TYPE)
343 /* If one is unsigned long long, then convert the other to unsigned
344 long long. */
345 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347 return build_type_attribute_variant (long_long_unsigned_type_node,
348 attributes);
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
353 other to long long.
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
358 case. */
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
362 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363 ? long_long_unsigned_type_node
364 : long_long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
368 /* Go through the same procedure, but for longs. */
369 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
371 return build_type_attribute_variant (long_unsigned_type_node,
372 attributes);
373 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
374 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
376 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
377 ? long_unsigned_type_node : long_integer_type_node);
378 return build_type_attribute_variant (t, attributes);
381 /* For __intN types, either the type is __int128 (and is lower
382 priority than the types checked above, but higher than other
383 128-bit types) or it's known to not be the same size as other
384 types (enforced in toplev.c). Prefer the unsigned type. */
385 for (i = 0; i < NUM_INT_N_ENTS; i ++)
387 if (int_n_enabled_p [i]
388 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
389 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
390 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
391 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
393 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
394 ? int_n_trees[i].unsigned_type
395 : int_n_trees[i].signed_type);
396 return build_type_attribute_variant (t, attributes);
400 /* Otherwise prefer the unsigned one. */
401 if (TYPE_UNSIGNED (t1))
402 return build_type_attribute_variant (t1, attributes);
403 else
404 return build_type_attribute_variant (t2, attributes);
406 else
408 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
409 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
410 return build_type_attribute_variant (long_double_type_node,
411 attributes);
412 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
413 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
414 return build_type_attribute_variant (double_type_node,
415 attributes);
416 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
417 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
418 return build_type_attribute_variant (float_type_node,
419 attributes);
421 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
422 the standard C++ floating-point types. Logic earlier in this
423 function has already eliminated the possibility that
424 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
425 compelling reason to choose one or the other. */
426 return build_type_attribute_variant (t1, attributes);
430 /* T1 and T2 are arithmetic or enumeration types. Return the type
431 that will result from the "usual arithmetic conversions" on T1 and
432 T2 as described in [expr]. */
434 tree
435 type_after_usual_arithmetic_conversions (tree t1, tree t2)
437 gcc_assert (ARITHMETIC_TYPE_P (t1)
438 || VECTOR_TYPE_P (t1)
439 || UNSCOPED_ENUM_P (t1));
440 gcc_assert (ARITHMETIC_TYPE_P (t2)
441 || VECTOR_TYPE_P (t2)
442 || UNSCOPED_ENUM_P (t2));
444 /* Perform the integral promotions. We do not promote real types here. */
445 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
446 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
448 t1 = type_promotes_to (t1);
449 t2 = type_promotes_to (t2);
452 return cp_common_type (t1, t2);
455 static void
456 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
457 composite_pointer_operation operation)
459 switch (operation)
461 case CPO_COMPARISON:
462 emit_diagnostic (kind, input_location, 0,
463 "comparison between "
464 "distinct pointer types %qT and %qT lacks a cast",
465 t1, t2);
466 break;
467 case CPO_CONVERSION:
468 emit_diagnostic (kind, input_location, 0,
469 "conversion between "
470 "distinct pointer types %qT and %qT lacks a cast",
471 t1, t2);
472 break;
473 case CPO_CONDITIONAL_EXPR:
474 emit_diagnostic (kind, input_location, 0,
475 "conditional expression between "
476 "distinct pointer types %qT and %qT lacks a cast",
477 t1, t2);
478 break;
479 default:
480 gcc_unreachable ();
484 /* Subroutine of composite_pointer_type to implement the recursive
485 case. See that function for documentation of the parameters. */
487 static tree
488 composite_pointer_type_r (tree t1, tree t2,
489 composite_pointer_operation operation,
490 tsubst_flags_t complain)
492 tree pointee1;
493 tree pointee2;
494 tree result_type;
495 tree attributes;
497 /* Determine the types pointed to by T1 and T2. */
498 if (TYPE_PTR_P (t1))
500 pointee1 = TREE_TYPE (t1);
501 pointee2 = TREE_TYPE (t2);
503 else
505 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
506 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
509 /* [expr.rel]
511 Otherwise, the composite pointer type is a pointer type
512 similar (_conv.qual_) to the type of one of the operands,
513 with a cv-qualification signature (_conv.qual_) that is the
514 union of the cv-qualification signatures of the operand
515 types. */
516 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
517 result_type = pointee1;
518 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
519 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
521 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
522 complain);
523 if (result_type == error_mark_node)
524 return error_mark_node;
526 else
528 if (complain & tf_error)
529 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
530 else
531 return error_mark_node;
532 result_type = void_type_node;
534 result_type = cp_build_qualified_type (result_type,
535 (cp_type_quals (pointee1)
536 | cp_type_quals (pointee2)));
537 /* If the original types were pointers to members, so is the
538 result. */
539 if (TYPE_PTRMEM_P (t1))
541 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
542 TYPE_PTRMEM_CLASS_TYPE (t2)))
544 if (complain & tf_error)
545 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
546 else
547 return error_mark_node;
549 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
550 result_type);
552 else
553 result_type = build_pointer_type (result_type);
555 /* Merge the attributes. */
556 attributes = (*targetm.merge_type_attributes) (t1, t2);
557 return build_type_attribute_variant (result_type, attributes);
560 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
561 ARG1 and ARG2 are the values with those types. The OPERATION is to
562 describe the operation between the pointer types,
563 in case an error occurs.
565 This routine also implements the computation of a common type for
566 pointers-to-members as per [expr.eq]. */
568 tree
569 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
570 composite_pointer_operation operation,
571 tsubst_flags_t complain)
573 tree class1;
574 tree class2;
576 /* [expr.rel]
578 If one operand is a null pointer constant, the composite pointer
579 type is the type of the other operand. */
580 if (null_ptr_cst_p (arg1))
581 return t2;
582 if (null_ptr_cst_p (arg2))
583 return t1;
585 /* We have:
587 [expr.rel]
589 If one of the operands has type "pointer to cv1 void*", then
590 the other has type "pointer to cv2T", and the composite pointer
591 type is "pointer to cv12 void", where cv12 is the union of cv1
592 and cv2.
594 If either type is a pointer to void, make sure it is T1. */
595 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
596 std::swap (t1, t2);
598 /* Now, if T1 is a pointer to void, merge the qualifiers. */
599 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
601 tree attributes;
602 tree result_type;
604 if (TYPE_PTRFN_P (t2))
606 if (complain & tf_error)
608 switch (operation)
610 case CPO_COMPARISON:
611 pedwarn (input_location, OPT_Wpedantic,
612 "ISO C++ forbids comparison between pointer "
613 "of type %<void *%> and pointer-to-function");
614 break;
615 case CPO_CONVERSION:
616 pedwarn (input_location, OPT_Wpedantic,
617 "ISO C++ forbids conversion between pointer "
618 "of type %<void *%> and pointer-to-function");
619 break;
620 case CPO_CONDITIONAL_EXPR:
621 pedwarn (input_location, OPT_Wpedantic,
622 "ISO C++ forbids conditional expression between "
623 "pointer of type %<void *%> and "
624 "pointer-to-function");
625 break;
626 default:
627 gcc_unreachable ();
630 else
631 return error_mark_node;
633 result_type
634 = cp_build_qualified_type (void_type_node,
635 (cp_type_quals (TREE_TYPE (t1))
636 | cp_type_quals (TREE_TYPE (t2))));
637 result_type = build_pointer_type (result_type);
638 /* Merge the attributes. */
639 attributes = (*targetm.merge_type_attributes) (t1, t2);
640 return build_type_attribute_variant (result_type, attributes);
643 if (c_dialect_objc () && TYPE_PTR_P (t1)
644 && TYPE_PTR_P (t2))
646 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
647 return objc_common_type (t1, t2);
650 /* [expr.eq] permits the application of a pointer conversion to
651 bring the pointers to a common type. */
652 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
653 && CLASS_TYPE_P (TREE_TYPE (t1))
654 && CLASS_TYPE_P (TREE_TYPE (t2))
655 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
656 TREE_TYPE (t2)))
658 class1 = TREE_TYPE (t1);
659 class2 = TREE_TYPE (t2);
661 if (DERIVED_FROM_P (class1, class2))
662 t2 = (build_pointer_type
663 (cp_build_qualified_type (class1, cp_type_quals (class2))));
664 else if (DERIVED_FROM_P (class2, class1))
665 t1 = (build_pointer_type
666 (cp_build_qualified_type (class2, cp_type_quals (class1))));
667 else
669 if (complain & tf_error)
670 composite_pointer_error (DK_ERROR, t1, t2, operation);
671 return error_mark_node;
674 /* [expr.eq] permits the application of a pointer-to-member
675 conversion to change the class type of one of the types. */
676 else if (TYPE_PTRMEM_P (t1)
677 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
678 TYPE_PTRMEM_CLASS_TYPE (t2)))
680 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
681 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
683 if (DERIVED_FROM_P (class1, class2))
684 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
685 else if (DERIVED_FROM_P (class2, class1))
686 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
687 else
689 if (complain & tf_error)
690 switch (operation)
692 case CPO_COMPARISON:
693 error ("comparison between distinct "
694 "pointer-to-member types %qT and %qT lacks a cast",
695 t1, t2);
696 break;
697 case CPO_CONVERSION:
698 error ("conversion between distinct "
699 "pointer-to-member types %qT and %qT lacks a cast",
700 t1, t2);
701 break;
702 case CPO_CONDITIONAL_EXPR:
703 error ("conditional expression between distinct "
704 "pointer-to-member types %qT and %qT lacks a cast",
705 t1, t2);
706 break;
707 default:
708 gcc_unreachable ();
710 return error_mark_node;
713 else if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
714 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t1))
715 && TREE_CODE (TREE_TYPE (t2)) == TREE_CODE (TREE_TYPE (t1)))
717 /* ...if T1 is "pointer to transaction_safe function" and T2 is "pointer
718 to function", where the function types are otherwise the same, T2, and
719 vice versa.... */
720 tree f1 = TREE_TYPE (t1);
721 tree f2 = TREE_TYPE (t2);
722 bool safe1 = tx_safe_fn_type_p (f1);
723 bool safe2 = tx_safe_fn_type_p (f2);
724 if (safe1 && !safe2)
725 t1 = build_pointer_type (tx_unsafe_fn_variant (f1));
726 else if (safe2 && !safe1)
727 t2 = build_pointer_type (tx_unsafe_fn_variant (f2));
730 return composite_pointer_type_r (t1, t2, operation, complain);
733 /* Return the merged type of two types.
734 We assume that comptypes has already been done and returned 1;
735 if that isn't so, this may crash.
737 This just combines attributes and default arguments; any other
738 differences would cause the two types to compare unalike. */
740 tree
741 merge_types (tree t1, tree t2)
743 enum tree_code code1;
744 enum tree_code code2;
745 tree attributes;
747 /* Save time if the two types are the same. */
748 if (t1 == t2)
749 return t1;
750 if (original_type (t1) == original_type (t2))
751 return t1;
753 /* If one type is nonsense, use the other. */
754 if (t1 == error_mark_node)
755 return t2;
756 if (t2 == error_mark_node)
757 return t1;
759 /* Handle merging an auto redeclaration with a previous deduced
760 return type. */
761 if (is_auto (t1))
762 return t2;
764 /* Merge the attributes. */
765 attributes = (*targetm.merge_type_attributes) (t1, t2);
767 if (TYPE_PTRMEMFUNC_P (t1))
768 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
769 if (TYPE_PTRMEMFUNC_P (t2))
770 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
772 code1 = TREE_CODE (t1);
773 code2 = TREE_CODE (t2);
774 if (code1 != code2)
776 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
777 if (code1 == TYPENAME_TYPE)
779 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
780 code1 = TREE_CODE (t1);
782 else
784 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
785 code2 = TREE_CODE (t2);
789 switch (code1)
791 case POINTER_TYPE:
792 case REFERENCE_TYPE:
793 /* For two pointers, do this recursively on the target type. */
795 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
796 int quals = cp_type_quals (t1);
798 if (code1 == POINTER_TYPE)
800 t1 = build_pointer_type (target);
801 if (TREE_CODE (target) == METHOD_TYPE)
802 t1 = build_ptrmemfunc_type (t1);
804 else
805 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
806 t1 = build_type_attribute_variant (t1, attributes);
807 t1 = cp_build_qualified_type (t1, quals);
809 return t1;
812 case OFFSET_TYPE:
814 int quals;
815 tree pointee;
816 quals = cp_type_quals (t1);
817 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
818 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
819 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
820 pointee);
821 t1 = cp_build_qualified_type (t1, quals);
822 break;
825 case ARRAY_TYPE:
827 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
828 /* Save space: see if the result is identical to one of the args. */
829 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
830 return build_type_attribute_variant (t1, attributes);
831 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
832 return build_type_attribute_variant (t2, attributes);
833 /* Merge the element types, and have a size if either arg has one. */
834 t1 = build_cplus_array_type
835 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
836 break;
839 case FUNCTION_TYPE:
840 /* Function types: prefer the one that specified arg types.
841 If both do, merge the arg types. Also merge the return types. */
843 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
844 tree p1 = TYPE_ARG_TYPES (t1);
845 tree p2 = TYPE_ARG_TYPES (t2);
846 tree parms;
847 tree rval, raises;
848 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
850 /* Save space: see if the result is identical to one of the args. */
851 if (valtype == TREE_TYPE (t1) && ! p2)
852 return cp_build_type_attribute_variant (t1, attributes);
853 if (valtype == TREE_TYPE (t2) && ! p1)
854 return cp_build_type_attribute_variant (t2, attributes);
856 /* Simple way if one arg fails to specify argument types. */
857 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
858 parms = p2;
859 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
860 parms = p1;
861 else
862 parms = commonparms (p1, p2);
864 rval = build_function_type (valtype, parms);
865 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
866 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
867 rval = apply_memfn_quals (rval,
868 type_memfn_quals (t1),
869 type_memfn_rqual (t1));
870 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
871 TYPE_RAISES_EXCEPTIONS (t2));
872 t1 = build_exception_variant (rval, raises);
873 if (late_return_type_p)
874 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
875 break;
878 case METHOD_TYPE:
880 /* Get this value the long way, since TYPE_METHOD_BASETYPE
881 is just the main variant of this. */
882 tree basetype = class_of_this_parm (t2);
883 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
884 TYPE_RAISES_EXCEPTIONS (t2));
885 cp_ref_qualifier rqual = type_memfn_rqual (t1);
886 tree t3;
887 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
888 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
890 /* If this was a member function type, get back to the
891 original type of type member function (i.e., without
892 the class instance variable up front. */
893 t1 = build_function_type (TREE_TYPE (t1),
894 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
895 t2 = build_function_type (TREE_TYPE (t2),
896 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
897 t3 = merge_types (t1, t2);
898 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
899 TYPE_ARG_TYPES (t3));
900 t1 = build_exception_variant (t3, raises);
901 t1 = build_ref_qualified_type (t1, rqual);
902 if (late_return_type_1_p)
903 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
904 if (late_return_type_2_p)
905 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
906 break;
909 case TYPENAME_TYPE:
910 /* There is no need to merge attributes into a TYPENAME_TYPE.
911 When the type is instantiated it will have whatever
912 attributes result from the instantiation. */
913 return t1;
915 default:;
918 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
919 return t1;
920 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
921 return t2;
922 else
923 return cp_build_type_attribute_variant (t1, attributes);
926 /* Return the ARRAY_TYPE type without its domain. */
928 tree
929 strip_array_domain (tree type)
931 tree t2;
932 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
933 if (TYPE_DOMAIN (type) == NULL_TREE)
934 return type;
935 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
936 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
939 /* Wrapper around cp_common_type that is used by c-common.c and other
940 front end optimizations that remove promotions.
942 Return the common type for two arithmetic types T1 and T2 under the
943 usual arithmetic conversions. The default conversions have already
944 been applied, and enumerated types converted to their compatible
945 integer types. */
947 tree
948 common_type (tree t1, tree t2)
950 /* If one type is nonsense, use the other */
951 if (t1 == error_mark_node)
952 return t2;
953 if (t2 == error_mark_node)
954 return t1;
956 return cp_common_type (t1, t2);
959 /* Return the common type of two pointer types T1 and T2. This is the
960 type for the result of most arithmetic operations if the operands
961 have the given two types.
963 We assume that comp_target_types has already been done and returned
964 nonzero; if that isn't so, this may crash. */
966 tree
967 common_pointer_type (tree t1, tree t2)
969 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
970 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
971 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
973 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
974 CPO_CONVERSION, tf_warning_or_error);
977 /* Compare two exception specifier types for exactness or subsetness, if
978 allowed. Returns false for mismatch, true for match (same, or
979 derived and !exact).
981 [except.spec] "If a class X ... objects of class X or any class publicly
982 and unambiguously derived from X. Similarly, if a pointer type Y * ...
983 exceptions of type Y * or that are pointers to any type publicly and
984 unambiguously derived from Y. Otherwise a function only allows exceptions
985 that have the same type ..."
986 This does not mention cv qualifiers and is different to what throw
987 [except.throw] and catch [except.catch] will do. They will ignore the
988 top level cv qualifiers, and allow qualifiers in the pointer to class
989 example.
991 We implement the letter of the standard. */
993 static bool
994 comp_except_types (tree a, tree b, bool exact)
996 if (same_type_p (a, b))
997 return true;
998 else if (!exact)
1000 if (cp_type_quals (a) || cp_type_quals (b))
1001 return false;
1003 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1005 a = TREE_TYPE (a);
1006 b = TREE_TYPE (b);
1007 if (cp_type_quals (a) || cp_type_quals (b))
1008 return false;
1011 if (TREE_CODE (a) != RECORD_TYPE
1012 || TREE_CODE (b) != RECORD_TYPE)
1013 return false;
1015 if (publicly_uniquely_derived_p (a, b))
1016 return true;
1018 return false;
1021 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1022 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1023 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1024 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1025 are unordered, but we've already filtered out duplicates. Most lists will
1026 be in order, we should try to make use of that. */
1028 bool
1029 comp_except_specs (const_tree t1, const_tree t2, int exact)
1031 const_tree probe;
1032 const_tree base;
1033 int length = 0;
1035 if (t1 == t2)
1036 return true;
1038 /* First handle noexcept. */
1039 if (exact < ce_exact)
1041 /* noexcept(false) is compatible with no exception-specification,
1042 and stricter than any spec. */
1043 if (t1 == noexcept_false_spec)
1044 return t2 == NULL_TREE || exact == ce_derived;
1045 /* Even a derived noexcept(false) is compatible with no
1046 exception-specification. */
1047 if (t2 == noexcept_false_spec)
1048 return t1 == NULL_TREE;
1050 /* Otherwise, if we aren't looking for an exact match, noexcept is
1051 equivalent to throw(). */
1052 if (t1 == noexcept_true_spec)
1053 t1 = empty_except_spec;
1054 if (t2 == noexcept_true_spec)
1055 t2 = empty_except_spec;
1058 /* If any noexcept is left, it is only comparable to itself;
1059 either we're looking for an exact match or we're redeclaring a
1060 template with dependent noexcept. */
1061 if ((t1 && TREE_PURPOSE (t1))
1062 || (t2 && TREE_PURPOSE (t2)))
1063 return (t1 && t2
1064 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1066 if (t1 == NULL_TREE) /* T1 is ... */
1067 return t2 == NULL_TREE || exact == ce_derived;
1068 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1069 return t2 != NULL_TREE && !TREE_VALUE (t2);
1070 if (t2 == NULL_TREE) /* T2 is ... */
1071 return false;
1072 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1073 return exact == ce_derived;
1075 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1076 Count how many we find, to determine exactness. For exact matching and
1077 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1078 O(nm). */
1079 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1081 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1083 tree a = TREE_VALUE (probe);
1084 tree b = TREE_VALUE (t2);
1086 if (comp_except_types (a, b, exact))
1088 if (probe == base && exact > ce_derived)
1089 base = TREE_CHAIN (probe);
1090 length++;
1091 break;
1094 if (probe == NULL_TREE)
1095 return false;
1097 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1100 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1101 [] can match [size]. */
1103 static bool
1104 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1106 tree d1;
1107 tree d2;
1108 tree max1, max2;
1110 if (t1 == t2)
1111 return true;
1113 /* The type of the array elements must be the same. */
1114 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1115 return false;
1117 d1 = TYPE_DOMAIN (t1);
1118 d2 = TYPE_DOMAIN (t2);
1120 if (d1 == d2)
1121 return true;
1123 /* If one of the arrays is dimensionless, and the other has a
1124 dimension, they are of different types. However, it is valid to
1125 write:
1127 extern int a[];
1128 int a[3];
1130 by [basic.link]:
1132 declarations for an array object can specify
1133 array types that differ by the presence or absence of a major
1134 array bound (_dcl.array_). */
1135 if (!d1 || !d2)
1136 return allow_redeclaration;
1138 /* Check that the dimensions are the same. */
1140 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1141 return false;
1142 max1 = TYPE_MAX_VALUE (d1);
1143 max2 = TYPE_MAX_VALUE (d2);
1145 if (!cp_tree_equal (max1, max2))
1146 return false;
1148 return true;
1151 /* Compare the relative position of T1 and T2 into their respective
1152 template parameter list.
1153 T1 and T2 must be template parameter types.
1154 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1156 static bool
1157 comp_template_parms_position (tree t1, tree t2)
1159 tree index1, index2;
1160 gcc_assert (t1 && t2
1161 && TREE_CODE (t1) == TREE_CODE (t2)
1162 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1163 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1164 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1166 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1167 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1169 /* Then compare their relative position. */
1170 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1171 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1172 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1173 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1174 return false;
1176 /* In C++14 we can end up comparing 'auto' to a normal template
1177 parameter. Don't confuse them. */
1178 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1179 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1181 return true;
1184 /* Subroutine in comptypes. */
1186 static bool
1187 structural_comptypes (tree t1, tree t2, int strict)
1189 if (t1 == t2)
1190 return true;
1192 /* Suppress errors caused by previously reported errors. */
1193 if (t1 == error_mark_node || t2 == error_mark_node)
1194 return false;
1196 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1198 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1199 current instantiation. */
1200 if (TREE_CODE (t1) == TYPENAME_TYPE)
1201 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1203 if (TREE_CODE (t2) == TYPENAME_TYPE)
1204 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1206 if (TYPE_PTRMEMFUNC_P (t1))
1207 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1208 if (TYPE_PTRMEMFUNC_P (t2))
1209 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1211 /* Different classes of types can't be compatible. */
1212 if (TREE_CODE (t1) != TREE_CODE (t2))
1213 return false;
1215 /* Qualifiers must match. For array types, we will check when we
1216 recur on the array element types. */
1217 if (TREE_CODE (t1) != ARRAY_TYPE
1218 && cp_type_quals (t1) != cp_type_quals (t2))
1219 return false;
1220 if (TREE_CODE (t1) == FUNCTION_TYPE
1221 && type_memfn_quals (t1) != type_memfn_quals (t2))
1222 return false;
1223 /* Need to check this before TYPE_MAIN_VARIANT.
1224 FIXME function qualifiers should really change the main variant. */
1225 if ((TREE_CODE (t1) == FUNCTION_TYPE
1226 || TREE_CODE (t1) == METHOD_TYPE)
1227 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1228 return false;
1229 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1230 return false;
1232 /* Allow for two different type nodes which have essentially the same
1233 definition. Note that we already checked for equality of the type
1234 qualifiers (just above). */
1236 if (TREE_CODE (t1) != ARRAY_TYPE
1237 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1238 return true;
1241 /* Compare the types. Break out if they could be the same. */
1242 switch (TREE_CODE (t1))
1244 case VOID_TYPE:
1245 case BOOLEAN_TYPE:
1246 /* All void and bool types are the same. */
1247 break;
1249 case INTEGER_TYPE:
1250 case FIXED_POINT_TYPE:
1251 case REAL_TYPE:
1252 /* With these nodes, we can't determine type equivalence by
1253 looking at what is stored in the nodes themselves, because
1254 two nodes might have different TYPE_MAIN_VARIANTs but still
1255 represent the same type. For example, wchar_t and int could
1256 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1257 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1258 and are distinct types. On the other hand, int and the
1259 following typedef
1261 typedef int INT __attribute((may_alias));
1263 have identical properties, different TYPE_MAIN_VARIANTs, but
1264 represent the same type. The canonical type system keeps
1265 track of equivalence in this case, so we fall back on it. */
1266 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1268 case TEMPLATE_TEMPLATE_PARM:
1269 case BOUND_TEMPLATE_TEMPLATE_PARM:
1270 if (!comp_template_parms_position (t1, t2))
1271 return false;
1272 if (!comp_template_parms
1273 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1274 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1275 return false;
1276 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1277 break;
1278 /* Don't check inheritance. */
1279 strict = COMPARE_STRICT;
1280 /* Fall through. */
1282 case RECORD_TYPE:
1283 case UNION_TYPE:
1284 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1285 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1286 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1287 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1288 break;
1290 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1291 break;
1292 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1293 break;
1295 return false;
1297 case OFFSET_TYPE:
1298 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1299 strict & ~COMPARE_REDECLARATION))
1300 return false;
1301 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1302 return false;
1303 break;
1305 case REFERENCE_TYPE:
1306 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1307 return false;
1308 /* fall through to checks for pointer types */
1310 case POINTER_TYPE:
1311 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1312 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1313 return false;
1314 break;
1316 case METHOD_TYPE:
1317 case FUNCTION_TYPE:
1318 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1319 return false;
1320 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1321 return false;
1322 break;
1324 case ARRAY_TYPE:
1325 /* Target types must match incl. qualifiers. */
1326 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1327 return false;
1328 break;
1330 case TEMPLATE_TYPE_PARM:
1331 /* If T1 and T2 don't have the same relative position in their
1332 template parameters set, they can't be equal. */
1333 if (!comp_template_parms_position (t1, t2))
1334 return false;
1335 /* Constrained 'auto's are distinct from parms that don't have the same
1336 constraints. */
1337 if (!equivalent_placeholder_constraints (t1, t2))
1338 return false;
1339 break;
1341 case TYPENAME_TYPE:
1342 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1343 TYPENAME_TYPE_FULLNAME (t2)))
1344 return false;
1345 /* Qualifiers don't matter on scopes. */
1346 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1347 TYPE_CONTEXT (t2)))
1348 return false;
1349 break;
1351 case UNBOUND_CLASS_TEMPLATE:
1352 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1353 return false;
1354 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1355 return false;
1356 break;
1358 case COMPLEX_TYPE:
1359 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1360 return false;
1361 break;
1363 case VECTOR_TYPE:
1364 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1365 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1366 return false;
1367 break;
1369 case TYPE_PACK_EXPANSION:
1370 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1371 PACK_EXPANSION_PATTERN (t2))
1372 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1373 PACK_EXPANSION_EXTRA_ARGS (t2)));
1375 case DECLTYPE_TYPE:
1376 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1377 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1378 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1379 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1380 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1381 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1382 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1383 DECLTYPE_TYPE_EXPR (t2)))
1384 return false;
1385 break;
1387 case UNDERLYING_TYPE:
1388 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1389 UNDERLYING_TYPE_TYPE (t2));
1391 default:
1392 return false;
1395 /* If we get here, we know that from a target independent POV the
1396 types are the same. Make sure the target attributes are also
1397 the same. */
1398 return comp_type_attributes (t1, t2);
1401 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1402 is a bitwise-or of the COMPARE_* flags. */
1404 bool
1405 comptypes (tree t1, tree t2, int strict)
1407 if (strict == COMPARE_STRICT)
1409 if (t1 == t2)
1410 return true;
1412 if (t1 == error_mark_node || t2 == error_mark_node)
1413 return false;
1415 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1416 /* At least one of the types requires structural equality, so
1417 perform a deep check. */
1418 return structural_comptypes (t1, t2, strict);
1420 if (flag_checking && USE_CANONICAL_TYPES)
1422 bool result = structural_comptypes (t1, t2, strict);
1424 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1425 /* The two types are structurally equivalent, but their
1426 canonical types were different. This is a failure of the
1427 canonical type propagation code.*/
1428 internal_error
1429 ("canonical types differ for identical types %T and %T",
1430 t1, t2);
1431 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1432 /* Two types are structurally different, but the canonical
1433 types are the same. This means we were over-eager in
1434 assigning canonical types. */
1435 internal_error
1436 ("same canonical type node for different types %T and %T",
1437 t1, t2);
1439 return result;
1441 if (!flag_checking && USE_CANONICAL_TYPES)
1442 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1443 else
1444 return structural_comptypes (t1, t2, strict);
1446 else if (strict == COMPARE_STRUCTURAL)
1447 return structural_comptypes (t1, t2, COMPARE_STRICT);
1448 else
1449 return structural_comptypes (t1, t2, strict);
1452 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1453 top-level qualifiers. */
1455 bool
1456 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1458 if (type1 == error_mark_node || type2 == error_mark_node)
1459 return false;
1461 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1464 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1466 bool
1467 at_least_as_qualified_p (const_tree type1, const_tree type2)
1469 int q1 = cp_type_quals (type1);
1470 int q2 = cp_type_quals (type2);
1472 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1473 return (q1 & q2) == q2;
1476 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1477 more cv-qualified that TYPE1, and 0 otherwise. */
1480 comp_cv_qualification (int q1, int q2)
1482 if (q1 == q2)
1483 return 0;
1485 if ((q1 & q2) == q2)
1486 return 1;
1487 else if ((q1 & q2) == q1)
1488 return -1;
1490 return 0;
1494 comp_cv_qualification (const_tree type1, const_tree type2)
1496 int q1 = cp_type_quals (type1);
1497 int q2 = cp_type_quals (type2);
1498 return comp_cv_qualification (q1, q2);
1501 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1502 subset of the cv-qualification signature of TYPE2, and the types
1503 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1506 comp_cv_qual_signature (tree type1, tree type2)
1508 if (comp_ptr_ttypes_real (type2, type1, -1))
1509 return 1;
1510 else if (comp_ptr_ttypes_real (type1, type2, -1))
1511 return -1;
1512 else
1513 return 0;
1516 /* Subroutines of `comptypes'. */
1518 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1519 equivalent in the sense that functions with those parameter types
1520 can have equivalent types. The two lists must be equivalent,
1521 element by element. */
1523 bool
1524 compparms (const_tree parms1, const_tree parms2)
1526 const_tree t1, t2;
1528 /* An unspecified parmlist matches any specified parmlist
1529 whose argument types don't need default promotions. */
1531 for (t1 = parms1, t2 = parms2;
1532 t1 || t2;
1533 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1535 /* If one parmlist is shorter than the other,
1536 they fail to match. */
1537 if (!t1 || !t2)
1538 return false;
1539 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1540 return false;
1542 return true;
1546 /* Process a sizeof or alignof expression where the operand is a
1547 type. */
1549 tree
1550 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1552 tree value;
1553 bool dependent_p;
1555 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1556 if (type == error_mark_node)
1557 return error_mark_node;
1559 type = non_reference (type);
1560 if (TREE_CODE (type) == METHOD_TYPE)
1562 if (complain)
1563 pedwarn (input_location, OPT_Wpointer_arith,
1564 "invalid application of %qs to a member function",
1565 operator_name_info[(int) op].name);
1566 else
1567 return error_mark_node;
1568 value = size_one_node;
1571 dependent_p = dependent_type_p (type);
1572 if (!dependent_p)
1573 complete_type (type);
1574 if (dependent_p
1575 /* VLA types will have a non-constant size. In the body of an
1576 uninstantiated template, we don't need to try to compute the
1577 value, because the sizeof expression is not an integral
1578 constant expression in that case. And, if we do try to
1579 compute the value, we'll likely end up with SAVE_EXPRs, which
1580 the template substitution machinery does not expect to see. */
1581 || (processing_template_decl
1582 && COMPLETE_TYPE_P (type)
1583 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1585 value = build_min (op, size_type_node, type);
1586 TREE_READONLY (value) = 1;
1587 return value;
1590 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1591 op == SIZEOF_EXPR, false,
1592 complain);
1595 /* Return the size of the type, without producing any warnings for
1596 types whose size cannot be taken. This routine should be used only
1597 in some other routine that has already produced a diagnostic about
1598 using the size of such a type. */
1599 tree
1600 cxx_sizeof_nowarn (tree type)
1602 if (TREE_CODE (type) == FUNCTION_TYPE
1603 || VOID_TYPE_P (type)
1604 || TREE_CODE (type) == ERROR_MARK)
1605 return size_one_node;
1606 else if (!COMPLETE_TYPE_P (type))
1607 return size_zero_node;
1608 else
1609 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1612 /* Process a sizeof expression where the operand is an expression. */
1614 static tree
1615 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1617 if (e == error_mark_node)
1618 return error_mark_node;
1620 if (processing_template_decl)
1622 e = build_min (SIZEOF_EXPR, size_type_node, e);
1623 TREE_SIDE_EFFECTS (e) = 0;
1624 TREE_READONLY (e) = 1;
1626 return e;
1629 /* To get the size of a static data member declared as an array of
1630 unknown bound, we need to instantiate it. */
1631 if (VAR_P (e)
1632 && VAR_HAD_UNKNOWN_BOUND (e)
1633 && DECL_TEMPLATE_INSTANTIATION (e))
1634 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1636 if (TREE_CODE (e) == PARM_DECL
1637 && DECL_ARRAY_PARAMETER_P (e)
1638 && (complain & tf_warning))
1640 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1641 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1642 inform (DECL_SOURCE_LOCATION (e), "declared here");
1645 e = mark_type_use (e);
1647 if (TREE_CODE (e) == COMPONENT_REF
1648 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1649 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1651 if (complain & tf_error)
1652 error ("invalid application of %<sizeof%> to a bit-field");
1653 else
1654 return error_mark_node;
1655 e = char_type_node;
1657 else if (is_overloaded_fn (e))
1659 if (complain & tf_error)
1660 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1661 "function type");
1662 else
1663 return error_mark_node;
1664 e = char_type_node;
1666 else if (type_unknown_p (e))
1668 if (complain & tf_error)
1669 cxx_incomplete_type_error (e, TREE_TYPE (e));
1670 else
1671 return error_mark_node;
1672 e = char_type_node;
1674 else
1675 e = TREE_TYPE (e);
1677 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1680 /* Implement the __alignof keyword: Return the minimum required
1681 alignment of E, measured in bytes. For VAR_DECL's and
1682 FIELD_DECL's return DECL_ALIGN (which can be set from an
1683 "aligned" __attribute__ specification). */
1685 static tree
1686 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1688 tree t;
1690 if (e == error_mark_node)
1691 return error_mark_node;
1693 if (processing_template_decl)
1695 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1696 TREE_SIDE_EFFECTS (e) = 0;
1697 TREE_READONLY (e) = 1;
1699 return e;
1702 e = mark_type_use (e);
1704 if (VAR_P (e))
1705 t = size_int (DECL_ALIGN_UNIT (e));
1706 else if (TREE_CODE (e) == COMPONENT_REF
1707 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1708 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1710 if (complain & tf_error)
1711 error ("invalid application of %<__alignof%> to a bit-field");
1712 else
1713 return error_mark_node;
1714 t = size_one_node;
1716 else if (TREE_CODE (e) == COMPONENT_REF
1717 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1718 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1719 else if (is_overloaded_fn (e))
1721 if (complain & tf_error)
1722 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1723 "function type");
1724 else
1725 return error_mark_node;
1726 if (TREE_CODE (e) == FUNCTION_DECL)
1727 t = size_int (DECL_ALIGN_UNIT (e));
1728 else
1729 t = size_one_node;
1731 else if (type_unknown_p (e))
1733 if (complain & tf_error)
1734 cxx_incomplete_type_error (e, TREE_TYPE (e));
1735 else
1736 return error_mark_node;
1737 t = size_one_node;
1739 else
1740 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1741 complain & tf_error);
1743 return fold_convert (size_type_node, t);
1746 /* Process a sizeof or alignof expression E with code OP where the operand
1747 is an expression. */
1749 tree
1750 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1752 if (op == SIZEOF_EXPR)
1753 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1754 else
1755 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1758 /* Build a representation of an expression 'alignas(E).' Return the
1759 folded integer value of E if it is an integral constant expression
1760 that resolves to a valid alignment. If E depends on a template
1761 parameter, return a syntactic representation tree of kind
1762 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1763 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1765 tree
1766 cxx_alignas_expr (tree e)
1768 if (e == NULL_TREE || e == error_mark_node
1769 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1770 return e;
1772 if (TYPE_P (e))
1773 /* [dcl.align]/3:
1775 When the alignment-specifier is of the form
1776 alignas(type-id ), it shall have the same effect as
1777 alignas(alignof(type-id )). */
1779 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1781 /* If we reach this point, it means the alignas expression if of
1782 the form "alignas(assignment-expression)", so we should follow
1783 what is stated by [dcl.align]/2. */
1785 if (value_dependent_expression_p (e))
1786 /* Leave value-dependent expression alone for now. */
1787 return e;
1789 e = instantiate_non_dependent_expr (e);
1790 e = mark_rvalue_use (e);
1792 /* [dcl.align]/2 says:
1794 the assignment-expression shall be an integral constant
1795 expression. */
1797 return cxx_constant_value (e);
1801 /* EXPR is being used in a context that is not a function call.
1802 Enforce:
1804 [expr.ref]
1806 The expression can be used only as the left-hand operand of a
1807 member function call.
1809 [expr.mptr.operator]
1811 If the result of .* or ->* is a function, then that result can be
1812 used only as the operand for the function call operator ().
1814 by issuing an error message if appropriate. Returns true iff EXPR
1815 violates these rules. */
1817 bool
1818 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1820 if (expr == NULL_TREE)
1821 return false;
1822 /* Don't enforce this in MS mode. */
1823 if (flag_ms_extensions)
1824 return false;
1825 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1826 expr = get_first_fn (expr);
1827 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1829 if (complain & tf_error)
1831 if (DECL_P (expr))
1833 error_at (loc, "invalid use of non-static member function %qD",
1834 expr);
1835 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1837 else
1838 error_at (loc, "invalid use of non-static member function of "
1839 "type %qT", TREE_TYPE (expr));
1841 return true;
1843 return false;
1846 /* If EXP is a reference to a bitfield, and the type of EXP does not
1847 match the declared type of the bitfield, return the declared type
1848 of the bitfield. Otherwise, return NULL_TREE. */
1850 tree
1851 is_bitfield_expr_with_lowered_type (const_tree exp)
1853 switch (TREE_CODE (exp))
1855 case COND_EXPR:
1856 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1857 ? TREE_OPERAND (exp, 1)
1858 : TREE_OPERAND (exp, 0)))
1859 return NULL_TREE;
1860 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1862 case COMPOUND_EXPR:
1863 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1865 case MODIFY_EXPR:
1866 case SAVE_EXPR:
1867 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1869 case COMPONENT_REF:
1871 tree field;
1873 field = TREE_OPERAND (exp, 1);
1874 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1875 return NULL_TREE;
1876 if (same_type_ignoring_top_level_qualifiers_p
1877 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1878 return NULL_TREE;
1879 return DECL_BIT_FIELD_TYPE (field);
1882 CASE_CONVERT:
1883 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1884 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1885 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1886 /* Fallthrough. */
1888 default:
1889 return NULL_TREE;
1893 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1894 bitfield with a lowered type, the type of EXP is returned, rather
1895 than NULL_TREE. */
1897 tree
1898 unlowered_expr_type (const_tree exp)
1900 tree type;
1901 tree etype = TREE_TYPE (exp);
1903 type = is_bitfield_expr_with_lowered_type (exp);
1904 if (type)
1905 type = cp_build_qualified_type (type, cp_type_quals (etype));
1906 else
1907 type = etype;
1909 return type;
1912 /* Perform the conversions in [expr] that apply when an lvalue appears
1913 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1914 function-to-pointer conversions. In addition, bitfield references are
1915 converted to their declared types. Note that this function does not perform
1916 the lvalue-to-rvalue conversion for class types. If you need that conversion
1917 for class types, then you probably need to use force_rvalue.
1919 Although the returned value is being used as an rvalue, this
1920 function does not wrap the returned expression in a
1921 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1922 that the return value is no longer an lvalue. */
1924 tree
1925 decay_conversion (tree exp,
1926 tsubst_flags_t complain,
1927 bool reject_builtin /* = true */)
1929 tree type;
1930 enum tree_code code;
1931 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1933 type = TREE_TYPE (exp);
1934 if (type == error_mark_node)
1935 return error_mark_node;
1937 exp = resolve_nondeduced_context (exp, complain);
1938 if (type_unknown_p (exp))
1940 if (complain & tf_error)
1941 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1942 return error_mark_node;
1945 code = TREE_CODE (type);
1947 if (error_operand_p (exp))
1948 return error_mark_node;
1950 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1951 return nullptr_node;
1953 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1954 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1955 if (code == VOID_TYPE)
1957 if (complain & tf_error)
1958 error_at (loc, "void value not ignored as it ought to be");
1959 return error_mark_node;
1961 if (invalid_nonstatic_memfn_p (loc, exp, complain))
1962 return error_mark_node;
1963 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1965 exp = mark_lvalue_use (exp);
1966 if (reject_builtin && reject_gcc_builtin (exp, loc))
1967 return error_mark_node;
1968 return cp_build_addr_expr (exp, complain);
1970 if (code == ARRAY_TYPE)
1972 tree adr;
1973 tree ptrtype;
1975 exp = mark_lvalue_use (exp);
1977 if (INDIRECT_REF_P (exp))
1978 return build_nop (build_pointer_type (TREE_TYPE (type)),
1979 TREE_OPERAND (exp, 0));
1981 if (TREE_CODE (exp) == COMPOUND_EXPR)
1983 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1984 if (op1 == error_mark_node)
1985 return error_mark_node;
1986 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1987 TREE_OPERAND (exp, 0), op1);
1990 if (!obvalue_p (exp)
1991 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1993 if (complain & tf_error)
1994 error_at (loc, "invalid use of non-lvalue array");
1995 return error_mark_node;
1998 /* Don't let an array compound literal decay to a pointer. It can
1999 still be used to initialize an array or bind to a reference. */
2000 if (TREE_CODE (exp) == TARGET_EXPR)
2002 if (complain & tf_error)
2003 error_at (loc, "taking address of temporary array");
2004 return error_mark_node;
2007 ptrtype = build_pointer_type (TREE_TYPE (type));
2009 if (VAR_P (exp))
2011 if (!cxx_mark_addressable (exp))
2012 return error_mark_node;
2013 adr = build_nop (ptrtype, build_address (exp));
2014 return adr;
2016 /* This way is better for a COMPONENT_REF since it can
2017 simplify the offset for a component. */
2018 adr = cp_build_addr_expr (exp, complain);
2019 return cp_convert (ptrtype, adr, complain);
2022 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2023 exp = mark_rvalue_use (exp, loc, reject_builtin);
2025 /* If a bitfield is used in a context where integral promotion
2026 applies, then the caller is expected to have used
2027 default_conversion. That function promotes bitfields correctly
2028 before calling this function. At this point, if we have a
2029 bitfield referenced, we may assume that is not subject to
2030 promotion, and that, therefore, the type of the resulting rvalue
2031 is the declared type of the bitfield. */
2032 exp = convert_bitfield_to_declared_type (exp);
2034 /* We do not call rvalue() here because we do not want to wrap EXP
2035 in a NON_LVALUE_EXPR. */
2037 /* [basic.lval]
2039 Non-class rvalues always have cv-unqualified types. */
2040 type = TREE_TYPE (exp);
2041 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2042 exp = build_nop (cv_unqualified (type), exp);
2044 if (!complete_type_or_maybe_complain (type, exp, complain))
2045 return error_mark_node;
2047 return exp;
2050 /* Perform preparatory conversions, as part of the "usual arithmetic
2051 conversions". In particular, as per [expr]:
2053 Whenever an lvalue expression appears as an operand of an
2054 operator that expects the rvalue for that operand, the
2055 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2056 standard conversions are applied to convert the expression to an
2057 rvalue.
2059 In addition, we perform integral promotions here, as those are
2060 applied to both operands to a binary operator before determining
2061 what additional conversions should apply. */
2063 static tree
2064 cp_default_conversion (tree exp, tsubst_flags_t complain)
2066 /* Check for target-specific promotions. */
2067 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2068 if (promoted_type)
2069 exp = cp_convert (promoted_type, exp, complain);
2070 /* Perform the integral promotions first so that bitfield
2071 expressions (which may promote to "int", even if the bitfield is
2072 declared "unsigned") are promoted correctly. */
2073 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2074 exp = cp_perform_integral_promotions (exp, complain);
2075 /* Perform the other conversions. */
2076 exp = decay_conversion (exp, complain);
2078 return exp;
2081 /* C version. */
2083 tree
2084 default_conversion (tree exp)
2086 return cp_default_conversion (exp, tf_warning_or_error);
2089 /* EXPR is an expression with an integral or enumeration type.
2090 Perform the integral promotions in [conv.prom], and return the
2091 converted value. */
2093 tree
2094 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2096 tree type;
2097 tree promoted_type;
2099 expr = mark_rvalue_use (expr);
2101 /* [conv.prom]
2103 If the bitfield has an enumerated type, it is treated as any
2104 other value of that type for promotion purposes. */
2105 type = is_bitfield_expr_with_lowered_type (expr);
2106 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2107 type = TREE_TYPE (expr);
2108 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2109 /* Scoped enums don't promote. */
2110 if (SCOPED_ENUM_P (type))
2111 return expr;
2112 promoted_type = type_promotes_to (type);
2113 if (type != promoted_type)
2114 expr = cp_convert (promoted_type, expr, complain);
2115 return expr;
2118 /* C version. */
2120 tree
2121 perform_integral_promotions (tree expr)
2123 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2126 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2127 decay_conversion to one. */
2130 string_conv_p (const_tree totype, const_tree exp, int warn)
2132 tree t;
2134 if (!TYPE_PTR_P (totype))
2135 return 0;
2137 t = TREE_TYPE (totype);
2138 if (!same_type_p (t, char_type_node)
2139 && !same_type_p (t, char16_type_node)
2140 && !same_type_p (t, char32_type_node)
2141 && !same_type_p (t, wchar_type_node))
2142 return 0;
2144 if (TREE_CODE (exp) == STRING_CST)
2146 /* Make sure that we don't try to convert between char and wide chars. */
2147 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2148 return 0;
2150 else
2152 /* Is this a string constant which has decayed to 'const char *'? */
2153 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2154 if (!same_type_p (TREE_TYPE (exp), t))
2155 return 0;
2156 STRIP_NOPS (exp);
2157 if (TREE_CODE (exp) != ADDR_EXPR
2158 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2159 return 0;
2161 if (warn)
2163 if (cxx_dialect >= cxx11)
2164 pedwarn (input_location,
2165 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
2166 "ISO C++ forbids converting a string constant to %qT",
2167 totype);
2168 else
2169 warning (OPT_Wwrite_strings,
2170 "deprecated conversion from string constant to %qT",
2171 totype);
2174 return 1;
2177 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2178 can, for example, use as an lvalue. This code used to be in
2179 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2180 expressions, where we're dealing with aggregates. But now it's again only
2181 called from unary_complex_lvalue. The case (in particular) that led to
2182 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2183 get it there. */
2185 static tree
2186 rationalize_conditional_expr (enum tree_code code, tree t,
2187 tsubst_flags_t complain)
2189 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2191 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2192 the first operand is always the one to be used if both operands
2193 are equal, so we know what conditional expression this used to be. */
2194 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2196 tree op0 = TREE_OPERAND (t, 0);
2197 tree op1 = TREE_OPERAND (t, 1);
2199 /* The following code is incorrect if either operand side-effects. */
2200 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2201 && !TREE_SIDE_EFFECTS (op1));
2202 return
2203 build_conditional_expr (loc,
2204 build_x_binary_op (loc,
2205 (TREE_CODE (t) == MIN_EXPR
2206 ? LE_EXPR : GE_EXPR),
2207 op0, TREE_CODE (op0),
2208 op1, TREE_CODE (op1),
2209 /*overload=*/NULL,
2210 complain),
2211 cp_build_unary_op (code, op0, false, complain),
2212 cp_build_unary_op (code, op1, false, complain),
2213 complain);
2216 return
2217 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2218 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2219 complain),
2220 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2221 complain),
2222 complain);
2225 /* Given the TYPE of an anonymous union field inside T, return the
2226 FIELD_DECL for the field. If not found return NULL_TREE. Because
2227 anonymous unions can nest, we must also search all anonymous unions
2228 that are directly reachable. */
2230 tree
2231 lookup_anon_field (tree t, tree type)
2233 tree field;
2235 t = TYPE_MAIN_VARIANT (t);
2237 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2239 if (TREE_STATIC (field))
2240 continue;
2241 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2242 continue;
2244 /* If we find it directly, return the field. */
2245 if (DECL_NAME (field) == NULL_TREE
2246 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2248 return field;
2251 /* Otherwise, it could be nested, search harder. */
2252 if (DECL_NAME (field) == NULL_TREE
2253 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2255 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2256 if (subfield)
2257 return subfield;
2260 return NULL_TREE;
2263 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2264 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2265 non-NULL, it indicates the path to the base used to name MEMBER.
2266 If PRESERVE_REFERENCE is true, the expression returned will have
2267 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2268 returned will have the type referred to by the reference.
2270 This function does not perform access control; that is either done
2271 earlier by the parser when the name of MEMBER is resolved to MEMBER
2272 itself, or later when overload resolution selects one of the
2273 functions indicated by MEMBER. */
2275 tree
2276 build_class_member_access_expr (cp_expr object, tree member,
2277 tree access_path, bool preserve_reference,
2278 tsubst_flags_t complain)
2280 tree object_type;
2281 tree member_scope;
2282 tree result = NULL_TREE;
2283 tree using_decl = NULL_TREE;
2285 if (error_operand_p (object) || error_operand_p (member))
2286 return error_mark_node;
2288 gcc_assert (DECL_P (member) || BASELINK_P (member));
2290 /* [expr.ref]
2292 The type of the first expression shall be "class object" (of a
2293 complete type). */
2294 object_type = TREE_TYPE (object);
2295 if (!currently_open_class (object_type)
2296 && !complete_type_or_maybe_complain (object_type, object, complain))
2297 return error_mark_node;
2298 if (!CLASS_TYPE_P (object_type))
2300 if (complain & tf_error)
2302 if (POINTER_TYPE_P (object_type)
2303 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2304 error ("request for member %qD in %qE, which is of pointer "
2305 "type %qT (maybe you meant to use %<->%> ?)",
2306 member, object.get_value (), object_type);
2307 else
2308 error ("request for member %qD in %qE, which is of non-class "
2309 "type %qT", member, object.get_value (), object_type);
2311 return error_mark_node;
2314 /* The standard does not seem to actually say that MEMBER must be a
2315 member of OBJECT_TYPE. However, that is clearly what is
2316 intended. */
2317 if (DECL_P (member))
2319 member_scope = DECL_CLASS_CONTEXT (member);
2320 if (!mark_used (member, complain) && !(complain & tf_error))
2321 return error_mark_node;
2322 if (TREE_DEPRECATED (member))
2323 warn_deprecated_use (member, NULL_TREE);
2325 else
2326 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2327 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2328 presently be the anonymous union. Go outwards until we find a
2329 type related to OBJECT_TYPE. */
2330 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2331 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2332 object_type))
2333 member_scope = TYPE_CONTEXT (member_scope);
2334 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2336 if (complain & tf_error)
2338 if (TREE_CODE (member) == FIELD_DECL)
2339 error ("invalid use of nonstatic data member %qE", member);
2340 else
2341 error ("%qD is not a member of %qT", member, object_type);
2343 return error_mark_node;
2346 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2347 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2348 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2350 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2351 if (temp)
2352 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2355 /* In [expr.ref], there is an explicit list of the valid choices for
2356 MEMBER. We check for each of those cases here. */
2357 if (VAR_P (member))
2359 /* A static data member. */
2360 result = member;
2361 mark_exp_read (object);
2362 /* If OBJECT has side-effects, they are supposed to occur. */
2363 if (TREE_SIDE_EFFECTS (object))
2364 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2366 else if (TREE_CODE (member) == FIELD_DECL)
2368 /* A non-static data member. */
2369 bool null_object_p;
2370 int type_quals;
2371 tree member_type;
2373 if (INDIRECT_REF_P (object))
2374 null_object_p =
2375 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2376 else
2377 null_object_p = false;
2379 /* Convert OBJECT to the type of MEMBER. */
2380 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2381 TYPE_MAIN_VARIANT (member_scope)))
2383 tree binfo;
2384 base_kind kind;
2386 binfo = lookup_base (access_path ? access_path : object_type,
2387 member_scope, ba_unique, &kind, complain);
2388 if (binfo == error_mark_node)
2389 return error_mark_node;
2391 /* It is invalid to try to get to a virtual base of a
2392 NULL object. The most common cause is invalid use of
2393 offsetof macro. */
2394 if (null_object_p && kind == bk_via_virtual)
2396 if (complain & tf_error)
2398 error ("invalid access to non-static data member %qD in "
2399 "virtual base of NULL object", member);
2401 return error_mark_node;
2404 /* Convert to the base. */
2405 object = build_base_path (PLUS_EXPR, object, binfo,
2406 /*nonnull=*/1, complain);
2407 /* If we found the base successfully then we should be able
2408 to convert to it successfully. */
2409 gcc_assert (object != error_mark_node);
2412 /* If MEMBER is from an anonymous aggregate, we have converted
2413 OBJECT so that it refers to the class containing the
2414 anonymous union. Generate a reference to the anonymous union
2415 itself, and recur to find MEMBER. */
2416 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2417 /* When this code is called from build_field_call, the
2418 object already has the type of the anonymous union.
2419 That is because the COMPONENT_REF was already
2420 constructed, and was then disassembled before calling
2421 build_field_call. After the function-call code is
2422 cleaned up, this waste can be eliminated. */
2423 && (!same_type_ignoring_top_level_qualifiers_p
2424 (TREE_TYPE (object), DECL_CONTEXT (member))))
2426 tree anonymous_union;
2428 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2429 DECL_CONTEXT (member));
2430 object = build_class_member_access_expr (object,
2431 anonymous_union,
2432 /*access_path=*/NULL_TREE,
2433 preserve_reference,
2434 complain);
2437 /* Compute the type of the field, as described in [expr.ref]. */
2438 type_quals = TYPE_UNQUALIFIED;
2439 member_type = TREE_TYPE (member);
2440 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2442 type_quals = (cp_type_quals (member_type)
2443 | cp_type_quals (object_type));
2445 /* A field is const (volatile) if the enclosing object, or the
2446 field itself, is const (volatile). But, a mutable field is
2447 not const, even within a const object. */
2448 if (DECL_MUTABLE_P (member))
2449 type_quals &= ~TYPE_QUAL_CONST;
2450 member_type = cp_build_qualified_type (member_type, type_quals);
2453 result = build3_loc (input_location, COMPONENT_REF, member_type,
2454 object, member, NULL_TREE);
2456 /* Mark the expression const or volatile, as appropriate. Even
2457 though we've dealt with the type above, we still have to mark the
2458 expression itself. */
2459 if (type_quals & TYPE_QUAL_CONST)
2460 TREE_READONLY (result) = 1;
2461 if (type_quals & TYPE_QUAL_VOLATILE)
2462 TREE_THIS_VOLATILE (result) = 1;
2464 else if (BASELINK_P (member))
2466 /* The member is a (possibly overloaded) member function. */
2467 tree functions;
2468 tree type;
2470 /* If the MEMBER is exactly one static member function, then we
2471 know the type of the expression. Otherwise, we must wait
2472 until overload resolution has been performed. */
2473 functions = BASELINK_FUNCTIONS (member);
2474 if (TREE_CODE (functions) == FUNCTION_DECL
2475 && DECL_STATIC_FUNCTION_P (functions))
2476 type = TREE_TYPE (functions);
2477 else
2478 type = unknown_type_node;
2479 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2480 base. That will happen when the function is called. */
2481 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2483 else if (TREE_CODE (member) == CONST_DECL)
2485 /* The member is an enumerator. */
2486 result = member;
2487 /* If OBJECT has side-effects, they are supposed to occur. */
2488 if (TREE_SIDE_EFFECTS (object))
2489 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2490 object, result);
2492 else if ((using_decl = strip_using_decl (member)) != member)
2493 result = build_class_member_access_expr (object,
2494 using_decl,
2495 access_path, preserve_reference,
2496 complain);
2497 else
2499 if (complain & tf_error)
2500 error ("invalid use of %qD", member);
2501 return error_mark_node;
2504 if (!preserve_reference)
2505 /* [expr.ref]
2507 If E2 is declared to have type "reference to T", then ... the
2508 type of E1.E2 is T. */
2509 result = convert_from_reference (result);
2511 return result;
2514 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2515 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2517 static tree
2518 lookup_destructor (tree object, tree scope, tree dtor_name,
2519 tsubst_flags_t complain)
2521 tree object_type = TREE_TYPE (object);
2522 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2523 tree expr;
2525 /* We've already complained about this destructor. */
2526 if (dtor_type == error_mark_node)
2527 return error_mark_node;
2529 if (scope && !check_dtor_name (scope, dtor_type))
2531 if (complain & tf_error)
2532 error ("qualified type %qT does not match destructor name ~%qT",
2533 scope, dtor_type);
2534 return error_mark_node;
2536 if (is_auto (dtor_type))
2537 dtor_type = object_type;
2538 else if (identifier_p (dtor_type))
2540 /* In a template, names we can't find a match for are still accepted
2541 destructor names, and we check them here. */
2542 if (check_dtor_name (object_type, dtor_type))
2543 dtor_type = object_type;
2544 else
2546 if (complain & tf_error)
2547 error ("object type %qT does not match destructor name ~%qT",
2548 object_type, dtor_type);
2549 return error_mark_node;
2553 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2555 if (complain & tf_error)
2556 error ("the type being destroyed is %qT, but the destructor "
2557 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2558 return error_mark_node;
2560 expr = lookup_member (dtor_type, complete_dtor_identifier,
2561 /*protect=*/1, /*want_type=*/false,
2562 tf_warning_or_error);
2563 if (!expr)
2565 if (complain & tf_error)
2566 cxx_incomplete_type_error (dtor_name, dtor_type);
2567 return error_mark_node;
2569 expr = (adjust_result_of_qualified_name_lookup
2570 (expr, dtor_type, object_type));
2571 if (scope == NULL_TREE)
2572 /* We need to call adjust_result_of_qualified_name_lookup in case the
2573 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2574 that we still get virtual function binding. */
2575 BASELINK_QUALIFIED_P (expr) = false;
2576 return expr;
2579 /* An expression of the form "A::template B" has been resolved to
2580 DECL. Issue a diagnostic if B is not a template or template
2581 specialization. */
2583 void
2584 check_template_keyword (tree decl)
2586 /* The standard says:
2588 [temp.names]
2590 If a name prefixed by the keyword template is not a member
2591 template, the program is ill-formed.
2593 DR 228 removed the restriction that the template be a member
2594 template.
2596 DR 96, if accepted would add the further restriction that explicit
2597 template arguments must be provided if the template keyword is
2598 used, but, as of 2005-10-16, that DR is still in "drafting". If
2599 this DR is accepted, then the semantic checks here can be
2600 simplified, as the entity named must in fact be a template
2601 specialization, rather than, as at present, a set of overloaded
2602 functions containing at least one template function. */
2603 if (TREE_CODE (decl) != TEMPLATE_DECL
2604 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2606 if (VAR_P (decl))
2608 if (DECL_USE_TEMPLATE (decl)
2609 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2611 else
2612 permerror (input_location, "%qD is not a template", decl);
2614 else if (!is_overloaded_fn (decl))
2615 permerror (input_location, "%qD is not a template", decl);
2616 else
2618 tree fns;
2619 fns = decl;
2620 if (BASELINK_P (fns))
2621 fns = BASELINK_FUNCTIONS (fns);
2622 while (fns)
2624 tree fn = OVL_CURRENT (fns);
2625 if (TREE_CODE (fn) == TEMPLATE_DECL
2626 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2627 break;
2628 if (TREE_CODE (fn) == FUNCTION_DECL
2629 && DECL_USE_TEMPLATE (fn)
2630 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2631 break;
2632 fns = OVL_NEXT (fns);
2634 if (!fns)
2635 permerror (input_location, "%qD is not a template", decl);
2640 /* This function is called by the parser to process a class member
2641 access expression of the form OBJECT.NAME. NAME is a node used by
2642 the parser to represent a name; it is not yet a DECL. It may,
2643 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2644 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2645 there is no reason to do the lookup twice, so the parser keeps the
2646 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2647 be a template via the use of the "A::template B" syntax. */
2649 tree
2650 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2651 tsubst_flags_t complain)
2653 tree expr;
2654 tree object_type;
2655 tree member;
2656 tree access_path = NULL_TREE;
2657 tree orig_object = object;
2658 tree orig_name = name;
2660 if (object == error_mark_node || name == error_mark_node)
2661 return error_mark_node;
2663 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2664 if (!objc_is_public (object, name))
2665 return error_mark_node;
2667 object_type = TREE_TYPE (object);
2669 if (processing_template_decl)
2671 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2672 type_dependent_object_expression_p (object)
2673 /* If NAME is "f<args>", where either 'f' or 'args' is
2674 dependent, then the expression is dependent. */
2675 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2676 && dependent_template_id_p (TREE_OPERAND (name, 0),
2677 TREE_OPERAND (name, 1)))
2678 /* If NAME is "T::X" where "T" is dependent, then the
2679 expression is dependent. */
2680 || (TREE_CODE (name) == SCOPE_REF
2681 && TYPE_P (TREE_OPERAND (name, 0))
2682 && dependent_scope_p (TREE_OPERAND (name, 0))))
2684 dependent:
2685 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2686 orig_object, orig_name, NULL_TREE);
2688 object = build_non_dependent_expr (object);
2690 else if (c_dialect_objc ()
2691 && identifier_p (name)
2692 && (expr = objc_maybe_build_component_ref (object, name)))
2693 return expr;
2695 /* [expr.ref]
2697 The type of the first expression shall be "class object" (of a
2698 complete type). */
2699 if (!currently_open_class (object_type)
2700 && !complete_type_or_maybe_complain (object_type, object, complain))
2701 return error_mark_node;
2702 if (!CLASS_TYPE_P (object_type))
2704 if (complain & tf_error)
2706 if (POINTER_TYPE_P (object_type)
2707 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2708 error ("request for member %qD in %qE, which is of pointer "
2709 "type %qT (maybe you meant to use %<->%> ?)",
2710 name, object.get_value (), object_type);
2711 else
2712 error ("request for member %qD in %qE, which is of non-class "
2713 "type %qT", name, object.get_value (), object_type);
2715 return error_mark_node;
2718 if (BASELINK_P (name))
2719 /* A member function that has already been looked up. */
2720 member = name;
2721 else
2723 bool is_template_id = false;
2724 tree template_args = NULL_TREE;
2725 tree scope;
2727 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2729 is_template_id = true;
2730 template_args = TREE_OPERAND (name, 1);
2731 name = TREE_OPERAND (name, 0);
2733 if (TREE_CODE (name) == OVERLOAD)
2734 name = DECL_NAME (get_first_fn (name));
2735 else if (DECL_P (name))
2736 name = DECL_NAME (name);
2739 if (TREE_CODE (name) == SCOPE_REF)
2741 /* A qualified name. The qualifying class or namespace `S'
2742 has already been looked up; it is either a TYPE or a
2743 NAMESPACE_DECL. */
2744 scope = TREE_OPERAND (name, 0);
2745 name = TREE_OPERAND (name, 1);
2747 /* If SCOPE is a namespace, then the qualified name does not
2748 name a member of OBJECT_TYPE. */
2749 if (TREE_CODE (scope) == NAMESPACE_DECL)
2751 if (complain & tf_error)
2752 error ("%<%D::%D%> is not a member of %qT",
2753 scope, name, object_type);
2754 return error_mark_node;
2757 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2759 /* Looking up a member enumerator (c++/56793). */
2760 if (!TYPE_CLASS_SCOPE_P (scope)
2761 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2763 if (complain & tf_error)
2764 error ("%<%D::%D%> is not a member of %qT",
2765 scope, name, object_type);
2766 return error_mark_node;
2768 tree val = lookup_enumerator (scope, name);
2769 if (!val)
2771 if (complain & tf_error)
2772 error ("%qD is not a member of %qD",
2773 name, scope);
2774 return error_mark_node;
2777 if (TREE_SIDE_EFFECTS (object))
2778 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2779 return val;
2782 gcc_assert (CLASS_TYPE_P (scope));
2783 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2785 if (constructor_name_p (name, scope))
2787 if (complain & tf_error)
2788 error ("cannot call constructor %<%T::%D%> directly",
2789 scope, name);
2790 return error_mark_node;
2793 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2794 access_path = lookup_base (object_type, scope, ba_check,
2795 NULL, complain);
2796 if (access_path == error_mark_node)
2797 return error_mark_node;
2798 if (!access_path)
2800 if (any_dependent_bases_p (object_type))
2801 goto dependent;
2802 if (complain & tf_error)
2803 error ("%qT is not a base of %qT", scope, object_type);
2804 return error_mark_node;
2807 else
2809 scope = NULL_TREE;
2810 access_path = object_type;
2813 if (TREE_CODE (name) == BIT_NOT_EXPR)
2815 if (dependent_type_p (object_type))
2816 /* The destructor isn't declared yet. */
2817 goto dependent;
2818 member = lookup_destructor (object, scope, name, complain);
2820 else
2822 /* Look up the member. */
2823 member = lookup_member (access_path, name, /*protect=*/1,
2824 /*want_type=*/false, complain);
2825 if (member == NULL_TREE)
2827 if (dependent_type_p (object_type))
2828 /* Try again at instantiation time. */
2829 goto dependent;
2830 if (complain & tf_error)
2832 tree guessed_id = lookup_member_fuzzy (access_path, name,
2833 /*want_type=*/false);
2834 if (guessed_id)
2836 location_t bogus_component_loc = input_location;
2837 gcc_rich_location rich_loc (bogus_component_loc);
2838 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2839 guessed_id);
2840 error_at_rich_loc
2841 (&rich_loc,
2842 "%q#T has no member named %qE; did you mean %qE?",
2843 TREE_CODE (access_path) == TREE_BINFO
2844 ? TREE_TYPE (access_path) : object_type, name,
2845 guessed_id);
2847 else
2848 error ("%q#T has no member named %qE",
2849 TREE_CODE (access_path) == TREE_BINFO
2850 ? TREE_TYPE (access_path) : object_type, name);
2852 return error_mark_node;
2854 if (member == error_mark_node)
2855 return error_mark_node;
2856 if (DECL_P (member)
2857 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2858 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2859 wrong, so don't use it. */
2860 goto dependent;
2861 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2862 goto dependent;
2865 if (is_template_id)
2867 tree templ = member;
2869 if (BASELINK_P (templ))
2870 templ = lookup_template_function (templ, template_args);
2871 else
2873 if (complain & tf_error)
2874 error ("%qD is not a member template function", name);
2875 return error_mark_node;
2880 if (TREE_DEPRECATED (member))
2881 warn_deprecated_use (member, NULL_TREE);
2883 if (template_p)
2884 check_template_keyword (member);
2886 expr = build_class_member_access_expr (object, member, access_path,
2887 /*preserve_reference=*/false,
2888 complain);
2889 if (processing_template_decl && expr != error_mark_node)
2891 if (BASELINK_P (member))
2893 if (TREE_CODE (orig_name) == SCOPE_REF)
2894 BASELINK_QUALIFIED_P (member) = 1;
2895 orig_name = member;
2897 return build_min_non_dep (COMPONENT_REF, expr,
2898 orig_object, orig_name,
2899 NULL_TREE);
2902 return expr;
2905 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2906 type. */
2908 tree
2909 build_simple_component_ref (tree object, tree member)
2911 tree type = cp_build_qualified_type (TREE_TYPE (member),
2912 cp_type_quals (TREE_TYPE (object)));
2913 return build3_loc (input_location,
2914 COMPONENT_REF, type,
2915 object, member, NULL_TREE);
2918 /* Return an expression for the MEMBER_NAME field in the internal
2919 representation of PTRMEM, a pointer-to-member function. (Each
2920 pointer-to-member function type gets its own RECORD_TYPE so it is
2921 more convenient to access the fields by name than by FIELD_DECL.)
2922 This routine converts the NAME to a FIELD_DECL and then creates the
2923 node for the complete expression. */
2925 tree
2926 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2928 tree ptrmem_type;
2929 tree member;
2931 /* This code is a stripped down version of
2932 build_class_member_access_expr. It does not work to use that
2933 routine directly because it expects the object to be of class
2934 type. */
2935 ptrmem_type = TREE_TYPE (ptrmem);
2936 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2937 for (member = TYPE_FIELDS (ptrmem_type); member;
2938 member = DECL_CHAIN (member))
2939 if (DECL_NAME (member) == member_name)
2940 break;
2941 return build_simple_component_ref (ptrmem, member);
2944 /* Given an expression PTR for a pointer, return an expression
2945 for the value pointed to.
2946 ERRORSTRING is the name of the operator to appear in error messages.
2948 This function may need to overload OPERATOR_FNNAME.
2949 Must also handle REFERENCE_TYPEs for C++. */
2951 tree
2952 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2953 tsubst_flags_t complain)
2955 tree orig_expr = expr;
2956 tree rval;
2957 tree overload = NULL_TREE;
2959 if (processing_template_decl)
2961 /* Retain the type if we know the operand is a pointer. */
2962 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2963 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2964 if (type_dependent_expression_p (expr))
2965 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2966 expr = build_non_dependent_expr (expr);
2969 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2970 NULL_TREE, NULL_TREE, &overload, complain);
2971 if (!rval)
2972 rval = cp_build_indirect_ref (expr, errorstring, complain);
2974 if (processing_template_decl && rval != error_mark_node)
2976 if (overload != NULL_TREE)
2977 return (build_min_non_dep_op_overload
2978 (INDIRECT_REF, rval, overload, orig_expr));
2980 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2982 else
2983 return rval;
2986 /* Helper function called from c-common. */
2987 tree
2988 build_indirect_ref (location_t /*loc*/,
2989 tree ptr, ref_operator errorstring)
2991 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2994 tree
2995 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2996 tsubst_flags_t complain)
2998 tree pointer, type;
3000 if (ptr == current_class_ptr
3001 || (TREE_CODE (ptr) == NOP_EXPR
3002 && TREE_OPERAND (ptr, 0) == current_class_ptr
3003 && (same_type_ignoring_top_level_qualifiers_p
3004 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3005 return current_class_ref;
3007 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
3008 ? ptr : decay_conversion (ptr, complain));
3009 if (pointer == error_mark_node)
3010 return error_mark_node;
3012 type = TREE_TYPE (pointer);
3014 if (POINTER_TYPE_P (type))
3016 /* [expr.unary.op]
3018 If the type of the expression is "pointer to T," the type
3019 of the result is "T." */
3020 tree t = TREE_TYPE (type);
3022 if ((CONVERT_EXPR_P (ptr)
3023 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3024 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3026 /* If a warning is issued, mark it to avoid duplicates from
3027 the backend. This only needs to be done at
3028 warn_strict_aliasing > 2. */
3029 if (warn_strict_aliasing > 2)
3030 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
3031 type, TREE_OPERAND (ptr, 0)))
3032 TREE_NO_WARNING (ptr) = 1;
3035 if (VOID_TYPE_P (t))
3037 /* A pointer to incomplete type (other than cv void) can be
3038 dereferenced [expr.unary.op]/1 */
3039 if (complain & tf_error)
3040 error ("%qT is not a pointer-to-object type", type);
3041 return error_mark_node;
3043 else if (TREE_CODE (pointer) == ADDR_EXPR
3044 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3045 /* The POINTER was something like `&x'. We simplify `*&x' to
3046 `x'. */
3047 return TREE_OPERAND (pointer, 0);
3048 else
3050 tree ref = build1 (INDIRECT_REF, t, pointer);
3052 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3053 so that we get the proper error message if the result is used
3054 to assign to. Also, &* is supposed to be a no-op. */
3055 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3056 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3057 TREE_SIDE_EFFECTS (ref)
3058 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3059 return ref;
3062 else if (!(complain & tf_error))
3063 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3065 /* `pointer' won't be an error_mark_node if we were given a
3066 pointer to member, so it's cool to check for this here. */
3067 else if (TYPE_PTRMEM_P (type))
3068 switch (errorstring)
3070 case RO_ARRAY_INDEXING:
3071 error ("invalid use of array indexing on pointer to member");
3072 break;
3073 case RO_UNARY_STAR:
3074 error ("invalid use of unary %<*%> on pointer to member");
3075 break;
3076 case RO_IMPLICIT_CONVERSION:
3077 error ("invalid use of implicit conversion on pointer to member");
3078 break;
3079 case RO_ARROW_STAR:
3080 error ("left hand operand of %<->*%> must be a pointer to class, "
3081 "but is a pointer to member of type %qT", type);
3082 break;
3083 default:
3084 gcc_unreachable ();
3086 else if (pointer != error_mark_node)
3087 invalid_indirection_error (input_location, type, errorstring);
3089 return error_mark_node;
3092 /* This handles expressions of the form "a[i]", which denotes
3093 an array reference.
3095 This is logically equivalent in C to *(a+i), but we may do it differently.
3096 If A is a variable or a member, we generate a primitive ARRAY_REF.
3097 This avoids forcing the array out of registers, and can work on
3098 arrays that are not lvalues (for example, members of structures returned
3099 by functions).
3101 If INDEX is of some user-defined type, it must be converted to
3102 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3103 will inherit the type of the array, which will be some pointer type.
3105 LOC is the location to use in building the array reference. */
3107 tree
3108 cp_build_array_ref (location_t loc, tree array, tree idx,
3109 tsubst_flags_t complain)
3111 tree ret;
3113 if (idx == 0)
3115 if (complain & tf_error)
3116 error_at (loc, "subscript missing in array reference");
3117 return error_mark_node;
3120 /* If an array's index is an array notation, then its rank cannot be
3121 greater than one. */
3122 if (flag_cilkplus && contains_array_notation_expr (idx))
3124 size_t rank = 0;
3126 /* If find_rank returns false, then it should have reported an error,
3127 thus it is unnecessary for repetition. */
3128 if (!find_rank (loc, idx, idx, true, &rank))
3129 return error_mark_node;
3130 if (rank > 1)
3132 error_at (loc, "rank of the array%'s index is greater than 1");
3133 return error_mark_node;
3136 if (TREE_TYPE (array) == error_mark_node
3137 || TREE_TYPE (idx) == error_mark_node)
3138 return error_mark_node;
3140 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3141 inside it. */
3142 switch (TREE_CODE (array))
3144 case COMPOUND_EXPR:
3146 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3147 complain);
3148 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3149 TREE_OPERAND (array, 0), value);
3150 SET_EXPR_LOCATION (ret, loc);
3151 return ret;
3154 case COND_EXPR:
3155 ret = build_conditional_expr
3156 (loc, TREE_OPERAND (array, 0),
3157 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3158 complain),
3159 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3160 complain),
3161 complain);
3162 protected_set_expr_location (ret, loc);
3163 return ret;
3165 default:
3166 break;
3169 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3171 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3173 tree rval, type;
3175 warn_array_subscript_with_type_char (loc, idx);
3177 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3179 if (complain & tf_error)
3180 error_at (loc, "array subscript is not an integer");
3181 return error_mark_node;
3184 /* Apply integral promotions *after* noticing character types.
3185 (It is unclear why we do these promotions -- the standard
3186 does not say that we should. In fact, the natural thing would
3187 seem to be to convert IDX to ptrdiff_t; we're performing
3188 pointer arithmetic.) */
3189 idx = cp_perform_integral_promotions (idx, complain);
3191 /* An array that is indexed by a non-constant
3192 cannot be stored in a register; we must be able to do
3193 address arithmetic on its address.
3194 Likewise an array of elements of variable size. */
3195 if (TREE_CODE (idx) != INTEGER_CST
3196 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3197 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3198 != INTEGER_CST)))
3200 if (!cxx_mark_addressable (array))
3201 return error_mark_node;
3204 /* An array that is indexed by a constant value which is not within
3205 the array bounds cannot be stored in a register either; because we
3206 would get a crash in store_bit_field/extract_bit_field when trying
3207 to access a non-existent part of the register. */
3208 if (TREE_CODE (idx) == INTEGER_CST
3209 && TYPE_DOMAIN (TREE_TYPE (array))
3210 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3212 if (!cxx_mark_addressable (array))
3213 return error_mark_node;
3216 /* Note in C++ it is valid to subscript a `register' array, since
3217 it is valid to take the address of something with that
3218 storage specification. */
3219 if (extra_warnings)
3221 tree foo = array;
3222 while (TREE_CODE (foo) == COMPONENT_REF)
3223 foo = TREE_OPERAND (foo, 0);
3224 if (VAR_P (foo) && DECL_REGISTER (foo)
3225 && (complain & tf_warning))
3226 warning_at (loc, OPT_Wextra,
3227 "subscripting array declared %<register%>");
3230 type = TREE_TYPE (TREE_TYPE (array));
3231 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3232 /* Array ref is const/volatile if the array elements are
3233 or if the array is.. */
3234 TREE_READONLY (rval)
3235 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3236 TREE_SIDE_EFFECTS (rval)
3237 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3238 TREE_THIS_VOLATILE (rval)
3239 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3240 ret = require_complete_type_sfinae (rval, complain);
3241 protected_set_expr_location (ret, loc);
3242 if (non_lvalue)
3243 ret = non_lvalue_loc (loc, ret);
3244 return ret;
3248 tree ar = cp_default_conversion (array, complain);
3249 tree ind = cp_default_conversion (idx, complain);
3251 /* Put the integer in IND to simplify error checking. */
3252 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3253 std::swap (ar, ind);
3255 if (ar == error_mark_node || ind == error_mark_node)
3256 return error_mark_node;
3258 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3260 if (complain & tf_error)
3261 error_at (loc, "subscripted value is neither array nor pointer");
3262 return error_mark_node;
3264 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3266 if (complain & tf_error)
3267 error_at (loc, "array subscript is not an integer");
3268 return error_mark_node;
3271 warn_array_subscript_with_type_char (loc, idx);
3273 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3274 PLUS_EXPR, ar, ind,
3275 complain),
3276 RO_ARRAY_INDEXING,
3277 complain);
3278 protected_set_expr_location (ret, loc);
3279 if (non_lvalue)
3280 ret = non_lvalue_loc (loc, ret);
3281 return ret;
3285 /* Entry point for Obj-C++. */
3287 tree
3288 build_array_ref (location_t loc, tree array, tree idx)
3290 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3293 /* Resolve a pointer to member function. INSTANCE is the object
3294 instance to use, if the member points to a virtual member.
3296 This used to avoid checking for virtual functions if basetype
3297 has no virtual functions, according to an earlier ANSI draft.
3298 With the final ISO C++ rules, such an optimization is
3299 incorrect: A pointer to a derived member can be static_cast
3300 to pointer-to-base-member, as long as the dynamic object
3301 later has the right member. So now we only do this optimization
3302 when we know the dynamic type of the object. */
3304 tree
3305 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3306 tsubst_flags_t complain)
3308 if (TREE_CODE (function) == OFFSET_REF)
3309 function = TREE_OPERAND (function, 1);
3311 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3313 tree idx, delta, e1, e2, e3, vtbl;
3314 bool nonvirtual;
3315 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3316 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3318 tree instance_ptr = *instance_ptrptr;
3319 tree instance_save_expr = 0;
3320 if (instance_ptr == error_mark_node)
3322 if (TREE_CODE (function) == PTRMEM_CST)
3324 /* Extracting the function address from a pmf is only
3325 allowed with -Wno-pmf-conversions. It only works for
3326 pmf constants. */
3327 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3328 e1 = convert (fntype, e1);
3329 return e1;
3331 else
3333 if (complain & tf_error)
3334 error ("object missing in use of %qE", function);
3335 return error_mark_node;
3339 /* True if we know that the dynamic type of the object doesn't have
3340 virtual functions, so we can assume the PFN field is a pointer. */
3341 nonvirtual = (COMPLETE_TYPE_P (basetype)
3342 && !TYPE_POLYMORPHIC_P (basetype)
3343 && resolves_to_fixed_type_p (instance_ptr, 0));
3345 /* If we don't really have an object (i.e. in an ill-formed
3346 conversion from PMF to pointer), we can't resolve virtual
3347 functions anyway. */
3348 if (!nonvirtual && is_dummy_object (instance_ptr))
3349 nonvirtual = true;
3351 if (TREE_SIDE_EFFECTS (instance_ptr))
3352 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3354 if (TREE_SIDE_EFFECTS (function))
3355 function = save_expr (function);
3357 /* Start by extracting all the information from the PMF itself. */
3358 e3 = pfn_from_ptrmemfunc (function);
3359 delta = delta_from_ptrmemfunc (function);
3360 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3361 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3363 int flag_sanitize_save;
3364 case ptrmemfunc_vbit_in_pfn:
3365 e1 = cp_build_binary_op (input_location,
3366 BIT_AND_EXPR, idx, integer_one_node,
3367 complain);
3368 idx = cp_build_binary_op (input_location,
3369 MINUS_EXPR, idx, integer_one_node,
3370 complain);
3371 if (idx == error_mark_node)
3372 return error_mark_node;
3373 break;
3375 case ptrmemfunc_vbit_in_delta:
3376 e1 = cp_build_binary_op (input_location,
3377 BIT_AND_EXPR, delta, integer_one_node,
3378 complain);
3379 /* Don't instrument the RSHIFT_EXPR we're about to create because
3380 we're going to use DELTA number of times, and that wouldn't play
3381 well with SAVE_EXPRs therein. */
3382 flag_sanitize_save = flag_sanitize;
3383 flag_sanitize = 0;
3384 delta = cp_build_binary_op (input_location,
3385 RSHIFT_EXPR, delta, integer_one_node,
3386 complain);
3387 flag_sanitize = flag_sanitize_save;
3388 if (delta == error_mark_node)
3389 return error_mark_node;
3390 break;
3392 default:
3393 gcc_unreachable ();
3396 if (e1 == error_mark_node)
3397 return error_mark_node;
3399 /* Convert down to the right base before using the instance. A
3400 special case is that in a pointer to member of class C, C may
3401 be incomplete. In that case, the function will of course be
3402 a member of C, and no conversion is required. In fact,
3403 lookup_base will fail in that case, because incomplete
3404 classes do not have BINFOs. */
3405 if (!same_type_ignoring_top_level_qualifiers_p
3406 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3408 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3409 basetype, ba_check, NULL, complain);
3410 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3411 1, complain);
3412 if (instance_ptr == error_mark_node)
3413 return error_mark_node;
3415 /* ...and then the delta in the PMF. */
3416 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3418 /* Hand back the adjusted 'this' argument to our caller. */
3419 *instance_ptrptr = instance_ptr;
3421 if (nonvirtual)
3422 /* Now just return the pointer. */
3423 return e3;
3425 /* Next extract the vtable pointer from the object. */
3426 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3427 instance_ptr);
3428 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3429 if (vtbl == error_mark_node)
3430 return error_mark_node;
3432 /* Finally, extract the function pointer from the vtable. */
3433 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3434 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3435 if (e2 == error_mark_node)
3436 return error_mark_node;
3437 TREE_CONSTANT (e2) = 1;
3439 /* When using function descriptors, the address of the
3440 vtable entry is treated as a function pointer. */
3441 if (TARGET_VTABLE_USES_DESCRIPTORS)
3442 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3443 cp_build_addr_expr (e2, complain));
3445 e2 = fold_convert (TREE_TYPE (e3), e2);
3446 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3447 if (e1 == error_mark_node)
3448 return error_mark_node;
3450 /* Make sure this doesn't get evaluated first inside one of the
3451 branches of the COND_EXPR. */
3452 if (instance_save_expr)
3453 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3454 instance_save_expr, e1);
3456 function = e1;
3458 return function;
3461 /* Used by the C-common bits. */
3462 tree
3463 build_function_call (location_t /*loc*/,
3464 tree function, tree params)
3466 return cp_build_function_call (function, params, tf_warning_or_error);
3469 /* Used by the C-common bits. */
3470 tree
3471 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3472 tree function, vec<tree, va_gc> *params,
3473 vec<tree, va_gc> * /*origtypes*/)
3475 vec<tree, va_gc> *orig_params = params;
3476 tree ret = cp_build_function_call_vec (function, &params,
3477 tf_warning_or_error);
3479 /* cp_build_function_call_vec can reallocate PARAMS by adding
3480 default arguments. That should never happen here. Verify
3481 that. */
3482 gcc_assert (params == orig_params);
3484 return ret;
3487 /* Build a function call using a tree list of arguments. */
3489 static tree
3490 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3492 vec<tree, va_gc> *vec;
3493 tree ret;
3495 vec = make_tree_vector ();
3496 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3497 vec_safe_push (vec, TREE_VALUE (params));
3498 ret = cp_build_function_call_vec (function, &vec, complain);
3499 release_tree_vector (vec);
3500 return ret;
3503 /* Build a function call using varargs. */
3505 tree
3506 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3508 vec<tree, va_gc> *vec;
3509 va_list args;
3510 tree ret, t;
3512 vec = make_tree_vector ();
3513 va_start (args, complain);
3514 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3515 vec_safe_push (vec, t);
3516 va_end (args);
3517 ret = cp_build_function_call_vec (function, &vec, complain);
3518 release_tree_vector (vec);
3519 return ret;
3522 /* Build a function call using a vector of arguments. PARAMS may be
3523 NULL if there are no parameters. This changes the contents of
3524 PARAMS. */
3526 tree
3527 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3528 tsubst_flags_t complain)
3530 tree fntype, fndecl;
3531 int is_method;
3532 tree original = function;
3533 int nargs;
3534 tree *argarray;
3535 tree parm_types;
3536 vec<tree, va_gc> *allocated = NULL;
3537 tree ret;
3539 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3540 expressions, like those used for ObjC messenger dispatches. */
3541 if (params != NULL && !vec_safe_is_empty (*params))
3542 function = objc_rewrite_function_call (function, (**params)[0]);
3544 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3545 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3546 if (TREE_CODE (function) == NOP_EXPR
3547 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3548 function = TREE_OPERAND (function, 0);
3550 if (TREE_CODE (function) == FUNCTION_DECL)
3552 /* If the function is a non-template member function
3553 or a non-template friend, then we need to check the
3554 constraints.
3556 Note that if overload resolution failed with a single
3557 candidate this function will be used to explicitly diagnose
3558 the failure for the single call expression. The check is
3559 technically redundant since we also would have failed in
3560 add_function_candidate. */
3561 if (flag_concepts
3562 && (complain & tf_error)
3563 && !constraints_satisfied_p (function))
3565 error ("cannot call function %qD", function);
3566 location_t loc = DECL_SOURCE_LOCATION (function);
3567 diagnose_constraints (loc, function, NULL_TREE);
3568 return error_mark_node;
3571 if (!mark_used (function, complain) && !(complain & tf_error))
3572 return error_mark_node;
3573 fndecl = function;
3575 /* Convert anything with function type to a pointer-to-function. */
3576 if (DECL_MAIN_P (function))
3578 if (complain & tf_error)
3579 pedwarn (input_location, OPT_Wpedantic,
3580 "ISO C++ forbids calling %<::main%> from within program");
3581 else
3582 return error_mark_node;
3584 function = build_addr_func (function, complain);
3586 else
3588 fndecl = NULL_TREE;
3590 function = build_addr_func (function, complain);
3593 if (function == error_mark_node)
3594 return error_mark_node;
3596 fntype = TREE_TYPE (function);
3598 if (TYPE_PTRMEMFUNC_P (fntype))
3600 if (complain & tf_error)
3601 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3602 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3603 original, original);
3604 return error_mark_node;
3607 is_method = (TYPE_PTR_P (fntype)
3608 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3610 if (!(TYPE_PTRFN_P (fntype)
3611 || is_method
3612 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3614 if (complain & tf_error)
3616 if (!flag_diagnostics_show_caret)
3617 error_at (input_location,
3618 "%qE cannot be used as a function", original);
3619 else if (DECL_P (original))
3620 error_at (input_location,
3621 "%qD cannot be used as a function", original);
3622 else
3623 error_at (input_location,
3624 "expression cannot be used as a function");
3627 return error_mark_node;
3630 /* fntype now gets the type of function pointed to. */
3631 fntype = TREE_TYPE (fntype);
3632 parm_types = TYPE_ARG_TYPES (fntype);
3634 if (params == NULL)
3636 allocated = make_tree_vector ();
3637 params = &allocated;
3640 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3641 complain);
3642 if (nargs < 0)
3643 return error_mark_node;
3645 argarray = (*params)->address ();
3647 /* Check for errors in format strings and inappropriately
3648 null parameters. */
3649 check_function_arguments (input_location, fntype, nargs, argarray);
3651 ret = build_cxx_call (function, nargs, argarray, complain);
3653 if (allocated != NULL)
3654 release_tree_vector (allocated);
3656 return ret;
3659 /* Subroutine of convert_arguments.
3660 Print an error message about a wrong number of arguments. */
3662 static void
3663 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3665 if (fndecl)
3667 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3669 if (DECL_NAME (fndecl) == NULL_TREE
3670 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3671 error_at (loc,
3672 too_many_p
3673 ? G_("too many arguments to constructor %q#D")
3674 : G_("too few arguments to constructor %q#D"),
3675 fndecl);
3676 else
3677 error_at (loc,
3678 too_many_p
3679 ? G_("too many arguments to member function %q#D")
3680 : G_("too few arguments to member function %q#D"),
3681 fndecl);
3683 else
3684 error_at (loc,
3685 too_many_p
3686 ? G_("too many arguments to function %q#D")
3687 : G_("too few arguments to function %q#D"),
3688 fndecl);
3689 if (!DECL_IS_BUILTIN (fndecl))
3690 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3692 else
3694 if (c_dialect_objc () && objc_message_selector ())
3695 error_at (loc,
3696 too_many_p
3697 ? G_("too many arguments to method %q#D")
3698 : G_("too few arguments to method %q#D"),
3699 objc_message_selector ());
3700 else
3701 error_at (loc, too_many_p ? G_("too many arguments to function")
3702 : G_("too few arguments to function"));
3706 /* Convert the actual parameter expressions in the list VALUES to the
3707 types in the list TYPELIST. The converted expressions are stored
3708 back in the VALUES vector.
3709 If parmdecls is exhausted, or when an element has NULL as its type,
3710 perform the default conversions.
3712 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3714 This is also where warnings about wrong number of args are generated.
3716 Returns the actual number of arguments processed (which might be less
3717 than the length of the vector), or -1 on error.
3719 In C++, unspecified trailing parameters can be filled in with their
3720 default arguments, if such were specified. Do so here. */
3722 static int
3723 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3724 int flags, tsubst_flags_t complain)
3726 tree typetail;
3727 unsigned int i;
3729 /* Argument passing is always copy-initialization. */
3730 flags |= LOOKUP_ONLYCONVERTING;
3732 for (i = 0, typetail = typelist;
3733 i < vec_safe_length (*values);
3734 i++)
3736 tree type = typetail ? TREE_VALUE (typetail) : 0;
3737 tree val = (**values)[i];
3739 if (val == error_mark_node || type == error_mark_node)
3740 return -1;
3742 if (type == void_type_node)
3744 if (complain & tf_error)
3746 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3747 return i;
3749 else
3750 return -1;
3753 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3754 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3755 if (TREE_CODE (val) == NOP_EXPR
3756 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3757 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3758 val = TREE_OPERAND (val, 0);
3760 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3762 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3763 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3764 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3765 val = decay_conversion (val, complain);
3768 if (val == error_mark_node)
3769 return -1;
3771 if (type != 0)
3773 /* Formal parm type is specified by a function prototype. */
3774 tree parmval;
3776 if (!COMPLETE_TYPE_P (complete_type (type)))
3778 if (complain & tf_error)
3780 if (fndecl)
3781 error ("parameter %P of %qD has incomplete type %qT",
3782 i, fndecl, type);
3783 else
3784 error ("parameter %P has incomplete type %qT", i, type);
3786 parmval = error_mark_node;
3788 else
3790 parmval = convert_for_initialization
3791 (NULL_TREE, type, val, flags,
3792 ICR_ARGPASS, fndecl, i, complain);
3793 parmval = convert_for_arg_passing (type, parmval, complain);
3796 if (parmval == error_mark_node)
3797 return -1;
3799 (**values)[i] = parmval;
3801 else
3803 if (fndecl && magic_varargs_p (fndecl))
3804 /* Don't do ellipsis conversion for __built_in_constant_p
3805 as this will result in spurious errors for non-trivial
3806 types. */
3807 val = require_complete_type_sfinae (val, complain);
3808 else
3809 val = convert_arg_to_ellipsis (val, complain);
3811 (**values)[i] = val;
3814 if (typetail)
3815 typetail = TREE_CHAIN (typetail);
3818 if (typetail != 0 && typetail != void_list_node)
3820 /* See if there are default arguments that can be used. Because
3821 we hold default arguments in the FUNCTION_TYPE (which is so
3822 wrong), we can see default parameters here from deduced
3823 contexts (and via typeof) for indirect function calls.
3824 Fortunately we know whether we have a function decl to
3825 provide default arguments in a language conformant
3826 manner. */
3827 if (fndecl && TREE_PURPOSE (typetail)
3828 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3830 for (; typetail != void_list_node; ++i)
3832 tree parmval
3833 = convert_default_arg (TREE_VALUE (typetail),
3834 TREE_PURPOSE (typetail),
3835 fndecl, i, complain);
3837 if (parmval == error_mark_node)
3838 return -1;
3840 vec_safe_push (*values, parmval);
3841 typetail = TREE_CHAIN (typetail);
3842 /* ends with `...'. */
3843 if (typetail == NULL_TREE)
3844 break;
3847 else
3849 if (complain & tf_error)
3850 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3851 return -1;
3855 return (int) i;
3858 /* Build a binary-operation expression, after performing default
3859 conversions on the operands. CODE is the kind of expression to
3860 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3861 are the tree codes which correspond to ARG1 and ARG2 when issuing
3862 warnings about possibly misplaced parentheses. They may differ
3863 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3864 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3865 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3866 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3867 ARG2_CODE as ERROR_MARK. */
3869 tree
3870 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3871 enum tree_code arg1_code, tree arg2,
3872 enum tree_code arg2_code, tree *overload_p,
3873 tsubst_flags_t complain)
3875 tree orig_arg1;
3876 tree orig_arg2;
3877 tree expr;
3878 tree overload = NULL_TREE;
3880 orig_arg1 = arg1;
3881 orig_arg2 = arg2;
3883 if (processing_template_decl)
3885 if (type_dependent_expression_p (arg1)
3886 || type_dependent_expression_p (arg2))
3887 return build_min_nt_loc (loc, code, arg1, arg2);
3888 arg1 = build_non_dependent_expr (arg1);
3889 arg2 = build_non_dependent_expr (arg2);
3892 if (code == DOTSTAR_EXPR)
3893 expr = build_m_component_ref (arg1, arg2, complain);
3894 else
3895 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3896 &overload, complain);
3898 if (overload_p != NULL)
3899 *overload_p = overload;
3901 /* Check for cases such as x+y<<z which users are likely to
3902 misinterpret. But don't warn about obj << x + y, since that is a
3903 common idiom for I/O. */
3904 if (warn_parentheses
3905 && (complain & tf_warning)
3906 && !processing_template_decl
3907 && !error_operand_p (arg1)
3908 && !error_operand_p (arg2)
3909 && (code != LSHIFT_EXPR
3910 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3911 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3912 arg2_code, orig_arg2);
3914 if (processing_template_decl && expr != error_mark_node)
3916 if (overload != NULL_TREE)
3917 return (build_min_non_dep_op_overload
3918 (code, expr, overload, orig_arg1, orig_arg2));
3920 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3923 return expr;
3926 /* Build and return an ARRAY_REF expression. */
3928 tree
3929 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3930 tsubst_flags_t complain)
3932 tree orig_arg1 = arg1;
3933 tree orig_arg2 = arg2;
3934 tree expr;
3935 tree overload = NULL_TREE;
3937 if (processing_template_decl)
3939 if (type_dependent_expression_p (arg1)
3940 || type_dependent_expression_p (arg2))
3941 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3942 NULL_TREE, NULL_TREE);
3943 arg1 = build_non_dependent_expr (arg1);
3944 arg2 = build_non_dependent_expr (arg2);
3947 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3948 NULL_TREE, &overload, complain);
3950 if (processing_template_decl && expr != error_mark_node)
3952 if (overload != NULL_TREE)
3953 return (build_min_non_dep_op_overload
3954 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
3956 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3957 NULL_TREE, NULL_TREE);
3959 return expr;
3962 /* Return whether OP is an expression of enum type cast to integer
3963 type. In C++ even unsigned enum types are cast to signed integer
3964 types. We do not want to issue warnings about comparisons between
3965 signed and unsigned types when one of the types is an enum type.
3966 Those warnings are always false positives in practice. */
3968 static bool
3969 enum_cast_to_int (tree op)
3971 if (CONVERT_EXPR_P (op)
3972 && TREE_TYPE (op) == integer_type_node
3973 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3974 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3975 return true;
3977 /* The cast may have been pushed into a COND_EXPR. */
3978 if (TREE_CODE (op) == COND_EXPR)
3979 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3980 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3982 return false;
3985 /* For the c-common bits. */
3986 tree
3987 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3988 int /*convert_p*/)
3990 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3993 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
3994 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
3996 static tree
3997 build_vec_cmp (tree_code code, tree type,
3998 tree arg0, tree arg1)
4000 tree zero_vec = build_zero_cst (type);
4001 tree minus_one_vec = build_minus_one_cst (type);
4002 tree cmp_type = build_same_sized_truth_vector_type(type);
4003 tree cmp = build2 (code, cmp_type, arg0, arg1);
4004 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4007 /* Possibly warn about an address never being NULL. */
4009 static void
4010 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4012 if (!warn_address
4013 || (complain & tf_warning) == 0
4014 || c_inhibit_evaluation_warnings != 0
4015 || TREE_NO_WARNING (op))
4016 return;
4018 tree cop = fold_non_dependent_expr (op);
4020 if (TREE_CODE (cop) == ADDR_EXPR
4021 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4022 && !TREE_NO_WARNING (cop))
4023 warning_at (location, OPT_Waddress, "the address of %qD will never "
4024 "be NULL", TREE_OPERAND (cop, 0));
4026 if (CONVERT_EXPR_P (op)
4027 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4029 tree inner_op = op;
4030 STRIP_NOPS (inner_op);
4032 if (DECL_P (inner_op))
4033 warning_at (location, OPT_Waddress,
4034 "the compiler can assume that the address of "
4035 "%qD will never be NULL", inner_op);
4039 /* Build a binary-operation expression without default conversions.
4040 CODE is the kind of expression to build.
4041 LOCATION is the location_t of the operator in the source code.
4042 This function differs from `build' in several ways:
4043 the data type of the result is computed and recorded in it,
4044 warnings are generated if arg data types are invalid,
4045 special handling for addition and subtraction of pointers is known,
4046 and some optimization is done (operations on narrow ints
4047 are done in the narrower type when that gives the same result).
4048 Constant folding is also done before the result is returned.
4050 Note that the operands will never have enumeral types
4051 because either they have just had the default conversions performed
4052 or they have both just been converted to some other type in which
4053 the arithmetic is to be done.
4055 C++: must do special pointer arithmetic when implementing
4056 multiple inheritance, and deal with pointer to member functions. */
4058 tree
4059 cp_build_binary_op (location_t location,
4060 enum tree_code code, tree orig_op0, tree orig_op1,
4061 tsubst_flags_t complain)
4063 tree op0, op1;
4064 enum tree_code code0, code1;
4065 tree type0, type1;
4066 const char *invalid_op_diag;
4068 /* Expression code to give to the expression when it is built.
4069 Normally this is CODE, which is what the caller asked for,
4070 but in some special cases we change it. */
4071 enum tree_code resultcode = code;
4073 /* Data type in which the computation is to be performed.
4074 In the simplest cases this is the common type of the arguments. */
4075 tree result_type = NULL;
4077 /* Nonzero means operands have already been type-converted
4078 in whatever way is necessary.
4079 Zero means they need to be converted to RESULT_TYPE. */
4080 int converted = 0;
4082 /* Nonzero means create the expression with this type, rather than
4083 RESULT_TYPE. */
4084 tree build_type = 0;
4086 /* Nonzero means after finally constructing the expression
4087 convert it to this type. */
4088 tree final_type = 0;
4090 tree result, result_ovl;
4091 tree orig_type = NULL;
4093 /* Nonzero if this is an operation like MIN or MAX which can
4094 safely be computed in short if both args are promoted shorts.
4095 Also implies COMMON.
4096 -1 indicates a bitwise operation; this makes a difference
4097 in the exact conditions for when it is safe to do the operation
4098 in a narrower mode. */
4099 int shorten = 0;
4101 /* Nonzero if this is a comparison operation;
4102 if both args are promoted shorts, compare the original shorts.
4103 Also implies COMMON. */
4104 int short_compare = 0;
4106 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4107 int common = 0;
4109 /* True if both operands have arithmetic type. */
4110 bool arithmetic_types_p;
4112 /* Apply default conversions. */
4113 op0 = orig_op0;
4114 op1 = orig_op1;
4116 /* Remember whether we're doing / or %. */
4117 bool doing_div_or_mod = false;
4119 /* Remember whether we're doing << or >>. */
4120 bool doing_shift = false;
4122 /* Tree holding instrumentation expression. */
4123 tree instrument_expr = NULL;
4125 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4126 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4127 || code == TRUTH_XOR_EXPR)
4129 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4130 op0 = decay_conversion (op0, complain);
4131 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4132 op1 = decay_conversion (op1, complain);
4134 else
4136 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4137 op0 = cp_default_conversion (op0, complain);
4138 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4139 op1 = cp_default_conversion (op1, complain);
4142 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4143 STRIP_TYPE_NOPS (op0);
4144 STRIP_TYPE_NOPS (op1);
4146 /* DTRT if one side is an overloaded function, but complain about it. */
4147 if (type_unknown_p (op0))
4149 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4150 if (t != error_mark_node)
4152 if (complain & tf_error)
4153 permerror (input_location, "assuming cast to type %qT from overloaded function",
4154 TREE_TYPE (t));
4155 op0 = t;
4158 if (type_unknown_p (op1))
4160 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4161 if (t != error_mark_node)
4163 if (complain & tf_error)
4164 permerror (input_location, "assuming cast to type %qT from overloaded function",
4165 TREE_TYPE (t));
4166 op1 = t;
4170 type0 = TREE_TYPE (op0);
4171 type1 = TREE_TYPE (op1);
4173 /* The expression codes of the data types of the arguments tell us
4174 whether the arguments are integers, floating, pointers, etc. */
4175 code0 = TREE_CODE (type0);
4176 code1 = TREE_CODE (type1);
4178 /* If an error was already reported for one of the arguments,
4179 avoid reporting another error. */
4180 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4181 return error_mark_node;
4183 if ((invalid_op_diag
4184 = targetm.invalid_binary_op (code, type0, type1)))
4186 if (complain & tf_error)
4187 error (invalid_op_diag);
4188 return error_mark_node;
4191 /* Issue warnings about peculiar, but valid, uses of NULL. */
4192 if ((orig_op0 == null_node || orig_op1 == null_node)
4193 /* It's reasonable to use pointer values as operands of &&
4194 and ||, so NULL is no exception. */
4195 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4196 && ( /* Both are NULL (or 0) and the operation was not a
4197 comparison or a pointer subtraction. */
4198 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4199 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4200 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4201 || (!null_ptr_cst_p (orig_op0)
4202 && !TYPE_PTR_OR_PTRMEM_P (type0))
4203 || (!null_ptr_cst_p (orig_op1)
4204 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4205 && (complain & tf_warning))
4207 source_location loc =
4208 expansion_point_location_if_in_system_header (input_location);
4210 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4213 /* In case when one of the operands of the binary operation is
4214 a vector and another is a scalar -- convert scalar to vector. */
4215 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4217 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4218 complain & tf_error);
4220 switch (convert_flag)
4222 case stv_error:
4223 return error_mark_node;
4224 case stv_firstarg:
4226 op0 = convert (TREE_TYPE (type1), op0);
4227 op0 = save_expr (op0);
4228 op0 = build_vector_from_val (type1, op0);
4229 type0 = TREE_TYPE (op0);
4230 code0 = TREE_CODE (type0);
4231 converted = 1;
4232 break;
4234 case stv_secondarg:
4236 op1 = convert (TREE_TYPE (type0), op1);
4237 op1 = save_expr (op1);
4238 op1 = build_vector_from_val (type0, op1);
4239 type1 = TREE_TYPE (op1);
4240 code1 = TREE_CODE (type1);
4241 converted = 1;
4242 break;
4244 default:
4245 break;
4249 switch (code)
4251 case MINUS_EXPR:
4252 /* Subtraction of two similar pointers.
4253 We must subtract them as integers, then divide by object size. */
4254 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4255 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4256 TREE_TYPE (type1)))
4257 return pointer_diff (location, op0, op1,
4258 common_pointer_type (type0, type1), complain);
4259 /* In all other cases except pointer - int, the usual arithmetic
4260 rules apply. */
4261 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4263 common = 1;
4264 break;
4266 /* The pointer - int case is just like pointer + int; fall
4267 through. */
4268 case PLUS_EXPR:
4269 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4270 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4272 tree ptr_operand;
4273 tree int_operand;
4274 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4275 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4276 if (processing_template_decl)
4278 result_type = TREE_TYPE (ptr_operand);
4279 break;
4281 return cp_pointer_int_sum (location, code,
4282 ptr_operand,
4283 int_operand,
4284 complain);
4286 common = 1;
4287 break;
4289 case MULT_EXPR:
4290 common = 1;
4291 break;
4293 case TRUNC_DIV_EXPR:
4294 case CEIL_DIV_EXPR:
4295 case FLOOR_DIV_EXPR:
4296 case ROUND_DIV_EXPR:
4297 case EXACT_DIV_EXPR:
4298 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4299 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4300 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4301 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4303 enum tree_code tcode0 = code0, tcode1 = code1;
4304 tree cop1 = fold_non_dependent_expr (op1);
4305 doing_div_or_mod = true;
4306 warn_for_div_by_zero (location, cop1);
4308 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4309 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4310 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4311 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4313 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4314 resultcode = RDIV_EXPR;
4315 else
4316 /* When dividing two signed integers, we have to promote to int.
4317 unless we divide by a constant != -1. Note that default
4318 conversion will have been performed on the operands at this
4319 point, so we have to dig out the original type to find out if
4320 it was unsigned. */
4321 shorten = ((TREE_CODE (op0) == NOP_EXPR
4322 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4323 || (TREE_CODE (op1) == INTEGER_CST
4324 && ! integer_all_onesp (op1)));
4326 common = 1;
4328 break;
4330 case BIT_AND_EXPR:
4331 case BIT_IOR_EXPR:
4332 case BIT_XOR_EXPR:
4333 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4334 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4335 && !VECTOR_FLOAT_TYPE_P (type0)
4336 && !VECTOR_FLOAT_TYPE_P (type1)))
4337 shorten = -1;
4338 break;
4340 case TRUNC_MOD_EXPR:
4341 case FLOOR_MOD_EXPR:
4343 tree cop1 = fold_non_dependent_expr (op1);
4344 doing_div_or_mod = true;
4345 warn_for_div_by_zero (location, cop1);
4348 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4349 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4350 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4351 common = 1;
4352 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4354 /* Although it would be tempting to shorten always here, that loses
4355 on some targets, since the modulo instruction is undefined if the
4356 quotient can't be represented in the computation mode. We shorten
4357 only if unsigned or if dividing by something we know != -1. */
4358 shorten = ((TREE_CODE (op0) == NOP_EXPR
4359 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4360 || (TREE_CODE (op1) == INTEGER_CST
4361 && ! integer_all_onesp (op1)));
4362 common = 1;
4364 break;
4366 case TRUTH_ANDIF_EXPR:
4367 case TRUTH_ORIF_EXPR:
4368 case TRUTH_AND_EXPR:
4369 case TRUTH_OR_EXPR:
4370 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4372 if (!COMPARISON_CLASS_P (op1))
4373 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4374 build_zero_cst (type1), complain);
4375 if (code == TRUTH_ANDIF_EXPR)
4377 tree z = build_zero_cst (TREE_TYPE (op1));
4378 return build_conditional_expr (location, op0, op1, z, complain);
4380 else if (code == TRUTH_ORIF_EXPR)
4382 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4383 return build_conditional_expr (location, op0, m1, op1, complain);
4385 else
4386 gcc_unreachable ();
4388 if (VECTOR_TYPE_P (type0))
4390 if (!COMPARISON_CLASS_P (op0))
4391 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4392 build_zero_cst (type0), complain);
4393 if (!VECTOR_TYPE_P (type1))
4395 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4396 tree z = build_zero_cst (TREE_TYPE (op0));
4397 op1 = build_conditional_expr (location, op1, m1, z, complain);
4399 else if (!COMPARISON_CLASS_P (op1))
4400 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4401 build_zero_cst (type1), complain);
4403 if (code == TRUTH_ANDIF_EXPR)
4404 code = BIT_AND_EXPR;
4405 else if (code == TRUTH_ORIF_EXPR)
4406 code = BIT_IOR_EXPR;
4407 else
4408 gcc_unreachable ();
4410 return cp_build_binary_op (location, code, op0, op1, complain);
4413 result_type = boolean_type_node;
4414 break;
4416 /* Shift operations: result has same type as first operand;
4417 always convert second operand to int.
4418 Also set SHORT_SHIFT if shifting rightward. */
4420 case RSHIFT_EXPR:
4421 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4422 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4424 result_type = type0;
4425 converted = 1;
4427 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4428 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4429 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4430 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4432 result_type = type0;
4433 converted = 1;
4435 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4437 tree const_op1 = fold_non_dependent_expr (op1);
4438 if (TREE_CODE (const_op1) != INTEGER_CST)
4439 const_op1 = op1;
4440 result_type = type0;
4441 doing_shift = true;
4442 if (TREE_CODE (const_op1) == INTEGER_CST)
4444 if (tree_int_cst_lt (const_op1, integer_zero_node))
4446 if ((complain & tf_warning)
4447 && c_inhibit_evaluation_warnings == 0)
4448 warning (OPT_Wshift_count_negative,
4449 "right shift count is negative");
4451 else
4453 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4454 && (complain & tf_warning)
4455 && c_inhibit_evaluation_warnings == 0)
4456 warning (OPT_Wshift_count_overflow,
4457 "right shift count >= width of type");
4460 /* Avoid converting op1 to result_type later. */
4461 converted = 1;
4463 break;
4465 case LSHIFT_EXPR:
4466 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4467 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4469 result_type = type0;
4470 converted = 1;
4472 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4473 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4474 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4475 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4477 result_type = type0;
4478 converted = 1;
4480 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4482 tree const_op0 = fold_non_dependent_expr (op0);
4483 if (TREE_CODE (const_op0) != INTEGER_CST)
4484 const_op0 = op0;
4485 tree const_op1 = fold_non_dependent_expr (op1);
4486 if (TREE_CODE (const_op1) != INTEGER_CST)
4487 const_op1 = op1;
4488 result_type = type0;
4489 doing_shift = true;
4490 if (TREE_CODE (const_op0) == INTEGER_CST
4491 && tree_int_cst_sgn (const_op0) < 0
4492 && (complain & tf_warning)
4493 && c_inhibit_evaluation_warnings == 0)
4494 warning (OPT_Wshift_negative_value,
4495 "left shift of negative value");
4496 if (TREE_CODE (const_op1) == INTEGER_CST)
4498 if (tree_int_cst_lt (const_op1, integer_zero_node))
4500 if ((complain & tf_warning)
4501 && c_inhibit_evaluation_warnings == 0)
4502 warning (OPT_Wshift_count_negative,
4503 "left shift count is negative");
4505 else if (compare_tree_int (const_op1,
4506 TYPE_PRECISION (type0)) >= 0)
4508 if ((complain & tf_warning)
4509 && c_inhibit_evaluation_warnings == 0)
4510 warning (OPT_Wshift_count_overflow,
4511 "left shift count >= width of type");
4513 else if (TREE_CODE (const_op0) == INTEGER_CST
4514 && (complain & tf_warning))
4515 maybe_warn_shift_overflow (location, const_op0, const_op1);
4517 /* Avoid converting op1 to result_type later. */
4518 converted = 1;
4520 break;
4522 case RROTATE_EXPR:
4523 case LROTATE_EXPR:
4524 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4526 result_type = type0;
4527 if (TREE_CODE (op1) == INTEGER_CST)
4529 if (tree_int_cst_lt (op1, integer_zero_node))
4531 if (complain & tf_warning)
4532 warning (0, (code == LROTATE_EXPR)
4533 ? G_("left rotate count is negative")
4534 : G_("right rotate count is negative"));
4536 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4538 if (complain & tf_warning)
4539 warning (0, (code == LROTATE_EXPR)
4540 ? G_("left rotate count >= width of type")
4541 : G_("right rotate count >= width of type"));
4544 /* Convert the shift-count to an integer, regardless of
4545 size of value being shifted. */
4546 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4547 op1 = cp_convert (integer_type_node, op1, complain);
4549 break;
4551 case EQ_EXPR:
4552 case NE_EXPR:
4553 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4554 goto vector_compare;
4555 if ((complain & tf_warning)
4556 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4557 warning (OPT_Wfloat_equal,
4558 "comparing floating point with == or != is unsafe");
4559 if ((complain & tf_warning)
4560 && ((TREE_CODE (orig_op0) == STRING_CST
4561 && !integer_zerop (cp_fully_fold (op1)))
4562 || (TREE_CODE (orig_op1) == STRING_CST
4563 && !integer_zerop (cp_fully_fold (op0)))))
4564 warning (OPT_Waddress, "comparison with string literal results "
4565 "in unspecified behavior");
4567 build_type = boolean_type_node;
4568 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4569 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4570 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4571 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4572 short_compare = 1;
4573 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4574 && null_ptr_cst_p (op1))
4575 /* Handle, eg, (void*)0 (c++/43906), and more. */
4576 || (code0 == POINTER_TYPE
4577 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4579 if (TYPE_PTR_P (type1))
4580 result_type = composite_pointer_type (type0, type1, op0, op1,
4581 CPO_COMPARISON, complain);
4582 else
4583 result_type = type0;
4585 warn_for_null_address (location, op0, complain);
4587 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4588 && null_ptr_cst_p (op0))
4589 /* Handle, eg, (void*)0 (c++/43906), and more. */
4590 || (code1 == POINTER_TYPE
4591 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4593 if (TYPE_PTR_P (type0))
4594 result_type = composite_pointer_type (type0, type1, op0, op1,
4595 CPO_COMPARISON, complain);
4596 else
4597 result_type = type1;
4599 warn_for_null_address (location, op1, complain);
4601 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4602 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4603 result_type = composite_pointer_type (type0, type1, op0, op1,
4604 CPO_COMPARISON, complain);
4605 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4606 /* One of the operands must be of nullptr_t type. */
4607 result_type = TREE_TYPE (nullptr_node);
4608 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4610 result_type = type0;
4611 if (complain & tf_error)
4612 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4613 else
4614 return error_mark_node;
4616 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4618 result_type = type1;
4619 if (complain & tf_error)
4620 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4621 else
4622 return error_mark_node;
4624 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4626 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4627 == ptrmemfunc_vbit_in_delta)
4629 tree pfn0, delta0, e1, e2;
4631 if (TREE_SIDE_EFFECTS (op0))
4632 op0 = save_expr (op0);
4634 pfn0 = pfn_from_ptrmemfunc (op0);
4635 delta0 = delta_from_ptrmemfunc (op0);
4636 e1 = cp_build_binary_op (location,
4637 EQ_EXPR,
4638 pfn0,
4639 build_zero_cst (TREE_TYPE (pfn0)),
4640 complain);
4641 e2 = cp_build_binary_op (location,
4642 BIT_AND_EXPR,
4643 delta0,
4644 integer_one_node,
4645 complain);
4647 if (complain & tf_warning)
4648 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4650 e2 = cp_build_binary_op (location,
4651 EQ_EXPR, e2, integer_zero_node,
4652 complain);
4653 op0 = cp_build_binary_op (location,
4654 TRUTH_ANDIF_EXPR, e1, e2,
4655 complain);
4656 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4658 else
4660 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4661 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4663 result_type = TREE_TYPE (op0);
4665 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4666 return cp_build_binary_op (location, code, op1, op0, complain);
4667 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4669 tree type;
4670 /* E will be the final comparison. */
4671 tree e;
4672 /* E1 and E2 are for scratch. */
4673 tree e1;
4674 tree e2;
4675 tree pfn0;
4676 tree pfn1;
4677 tree delta0;
4678 tree delta1;
4680 type = composite_pointer_type (type0, type1, op0, op1,
4681 CPO_COMPARISON, complain);
4683 if (!same_type_p (TREE_TYPE (op0), type))
4684 op0 = cp_convert_and_check (type, op0, complain);
4685 if (!same_type_p (TREE_TYPE (op1), type))
4686 op1 = cp_convert_and_check (type, op1, complain);
4688 if (op0 == error_mark_node || op1 == error_mark_node)
4689 return error_mark_node;
4691 if (TREE_SIDE_EFFECTS (op0))
4692 op0 = save_expr (op0);
4693 if (TREE_SIDE_EFFECTS (op1))
4694 op1 = save_expr (op1);
4696 pfn0 = pfn_from_ptrmemfunc (op0);
4697 pfn0 = cp_fully_fold (pfn0);
4698 /* Avoid -Waddress warnings (c++/64877). */
4699 if (TREE_CODE (pfn0) == ADDR_EXPR)
4700 TREE_NO_WARNING (pfn0) = 1;
4701 pfn1 = pfn_from_ptrmemfunc (op1);
4702 pfn1 = cp_fully_fold (pfn1);
4703 delta0 = delta_from_ptrmemfunc (op0);
4704 delta1 = delta_from_ptrmemfunc (op1);
4705 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4706 == ptrmemfunc_vbit_in_delta)
4708 /* We generate:
4710 (op0.pfn == op1.pfn
4711 && ((op0.delta == op1.delta)
4712 || (!op0.pfn && op0.delta & 1 == 0
4713 && op1.delta & 1 == 0))
4715 The reason for the `!op0.pfn' bit is that a NULL
4716 pointer-to-member is any member with a zero PFN and
4717 LSB of the DELTA field is 0. */
4719 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4720 delta0,
4721 integer_one_node,
4722 complain);
4723 e1 = cp_build_binary_op (location,
4724 EQ_EXPR, e1, integer_zero_node,
4725 complain);
4726 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4727 delta1,
4728 integer_one_node,
4729 complain);
4730 e2 = cp_build_binary_op (location,
4731 EQ_EXPR, e2, integer_zero_node,
4732 complain);
4733 e1 = cp_build_binary_op (location,
4734 TRUTH_ANDIF_EXPR, e2, e1,
4735 complain);
4736 e2 = cp_build_binary_op (location, EQ_EXPR,
4737 pfn0,
4738 build_zero_cst (TREE_TYPE (pfn0)),
4739 complain);
4740 e2 = cp_build_binary_op (location,
4741 TRUTH_ANDIF_EXPR, e2, e1, complain);
4742 e1 = cp_build_binary_op (location,
4743 EQ_EXPR, delta0, delta1, complain);
4744 e1 = cp_build_binary_op (location,
4745 TRUTH_ORIF_EXPR, e1, e2, complain);
4747 else
4749 /* We generate:
4751 (op0.pfn == op1.pfn
4752 && (!op0.pfn || op0.delta == op1.delta))
4754 The reason for the `!op0.pfn' bit is that a NULL
4755 pointer-to-member is any member with a zero PFN; the
4756 DELTA field is unspecified. */
4758 e1 = cp_build_binary_op (location,
4759 EQ_EXPR, delta0, delta1, complain);
4760 e2 = cp_build_binary_op (location,
4761 EQ_EXPR,
4762 pfn0,
4763 build_zero_cst (TREE_TYPE (pfn0)),
4764 complain);
4765 e1 = cp_build_binary_op (location,
4766 TRUTH_ORIF_EXPR, e1, e2, complain);
4768 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4769 e = cp_build_binary_op (location,
4770 TRUTH_ANDIF_EXPR, e2, e1, complain);
4771 if (code == EQ_EXPR)
4772 return e;
4773 return cp_build_binary_op (location,
4774 EQ_EXPR, e, integer_zero_node, complain);
4776 else
4778 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4779 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4780 type1));
4781 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4782 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4783 type0));
4786 break;
4788 case MAX_EXPR:
4789 case MIN_EXPR:
4790 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4791 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4792 shorten = 1;
4793 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4794 result_type = composite_pointer_type (type0, type1, op0, op1,
4795 CPO_COMPARISON, complain);
4796 break;
4798 case LE_EXPR:
4799 case GE_EXPR:
4800 case LT_EXPR:
4801 case GT_EXPR:
4802 if (TREE_CODE (orig_op0) == STRING_CST
4803 || TREE_CODE (orig_op1) == STRING_CST)
4805 if (complain & tf_warning)
4806 warning (OPT_Waddress, "comparison with string literal results "
4807 "in unspecified behavior");
4810 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4812 vector_compare:
4813 tree intt;
4814 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4815 TREE_TYPE (type1))
4816 && !vector_types_compatible_elements_p (type0, type1))
4818 if (complain & tf_error)
4820 error_at (location, "comparing vectors with different "
4821 "element types");
4822 inform (location, "operand types are %qT and %qT",
4823 type0, type1);
4825 return error_mark_node;
4828 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4830 if (complain & tf_error)
4832 error_at (location, "comparing vectors with different "
4833 "number of elements");
4834 inform (location, "operand types are %qT and %qT",
4835 type0, type1);
4837 return error_mark_node;
4840 /* It's not precisely specified how the usual arithmetic
4841 conversions apply to the vector types. Here, we use
4842 the unsigned type if one of the operands is signed and
4843 the other one is unsigned. */
4844 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
4846 if (!TYPE_UNSIGNED (type0))
4847 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
4848 else
4849 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
4850 warning_at (location, OPT_Wsign_compare, "comparison between "
4851 "types %qT and %qT", type0, type1);
4854 /* Always construct signed integer vector type. */
4855 intt = c_common_type_for_size (GET_MODE_BITSIZE
4856 (TYPE_MODE (TREE_TYPE (type0))), 0);
4857 if (!intt)
4859 if (complain & tf_error)
4860 error_at (location, "could not find an integer type "
4861 "of the same size as %qT", TREE_TYPE (type0));
4862 return error_mark_node;
4864 result_type = build_opaque_vector_type (intt,
4865 TYPE_VECTOR_SUBPARTS (type0));
4866 converted = 1;
4867 return build_vec_cmp (resultcode, result_type, op0, op1);
4869 build_type = boolean_type_node;
4870 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4871 || code0 == ENUMERAL_TYPE)
4872 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4873 || code1 == ENUMERAL_TYPE))
4874 short_compare = 1;
4875 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4876 result_type = composite_pointer_type (type0, type1, op0, op1,
4877 CPO_COMPARISON, complain);
4878 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4880 result_type = type0;
4881 if (extra_warnings && (complain & tf_warning))
4882 warning (OPT_Wextra,
4883 "ordered comparison of pointer with integer zero");
4885 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4887 result_type = type1;
4888 if (extra_warnings && (complain & tf_warning))
4889 warning (OPT_Wextra,
4890 "ordered comparison of pointer with integer zero");
4892 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4893 /* One of the operands must be of nullptr_t type. */
4894 result_type = TREE_TYPE (nullptr_node);
4895 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4897 result_type = type0;
4898 if (complain & tf_error)
4899 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4900 else
4901 return error_mark_node;
4903 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4905 result_type = type1;
4906 if (complain & tf_error)
4907 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4908 else
4909 return error_mark_node;
4911 break;
4913 case UNORDERED_EXPR:
4914 case ORDERED_EXPR:
4915 case UNLT_EXPR:
4916 case UNLE_EXPR:
4917 case UNGT_EXPR:
4918 case UNGE_EXPR:
4919 case UNEQ_EXPR:
4920 build_type = integer_type_node;
4921 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4923 if (complain & tf_error)
4924 error ("unordered comparison on non-floating point argument");
4925 return error_mark_node;
4927 common = 1;
4928 break;
4930 default:
4931 break;
4934 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4935 || code0 == ENUMERAL_TYPE)
4936 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4937 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4938 arithmetic_types_p = 1;
4939 else
4941 arithmetic_types_p = 0;
4942 /* Vector arithmetic is only allowed when both sides are vectors. */
4943 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4945 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4946 || !vector_types_compatible_elements_p (type0, type1))
4948 if (complain & tf_error)
4950 /* "location" already embeds the locations of the
4951 operands, so we don't need to add them separately
4952 to richloc. */
4953 rich_location richloc (line_table, location);
4954 binary_op_error (&richloc, code, type0, type1);
4956 return error_mark_node;
4958 arithmetic_types_p = 1;
4961 /* Determine the RESULT_TYPE, if it is not already known. */
4962 if (!result_type
4963 && arithmetic_types_p
4964 && (shorten || common || short_compare))
4966 result_type = cp_common_type (type0, type1);
4967 if (complain & tf_warning)
4968 do_warn_double_promotion (result_type, type0, type1,
4969 "implicit conversion from %qT to %qT "
4970 "to match other operand of binary "
4971 "expression",
4972 location);
4975 if (!result_type)
4977 if (complain & tf_error)
4978 error_at (location,
4979 "invalid operands of types %qT and %qT to binary %qO",
4980 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4981 return error_mark_node;
4984 /* If we're in a template, the only thing we need to know is the
4985 RESULT_TYPE. */
4986 if (processing_template_decl)
4988 /* Since the middle-end checks the type when doing a build2, we
4989 need to build the tree in pieces. This built tree will never
4990 get out of the front-end as we replace it when instantiating
4991 the template. */
4992 tree tmp = build2 (resultcode,
4993 build_type ? build_type : result_type,
4994 NULL_TREE, op1);
4995 TREE_OPERAND (tmp, 0) = op0;
4996 return tmp;
4999 if (arithmetic_types_p)
5001 bool first_complex = (code0 == COMPLEX_TYPE);
5002 bool second_complex = (code1 == COMPLEX_TYPE);
5003 int none_complex = (!first_complex && !second_complex);
5005 /* Adapted from patch for c/24581. */
5006 if (first_complex != second_complex
5007 && (code == PLUS_EXPR
5008 || code == MINUS_EXPR
5009 || code == MULT_EXPR
5010 || (code == TRUNC_DIV_EXPR && first_complex))
5011 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5012 && flag_signed_zeros)
5014 /* An operation on mixed real/complex operands must be
5015 handled specially, but the language-independent code can
5016 more easily optimize the plain complex arithmetic if
5017 -fno-signed-zeros. */
5018 tree real_type = TREE_TYPE (result_type);
5019 tree real, imag;
5020 if (first_complex)
5022 if (TREE_TYPE (op0) != result_type)
5023 op0 = cp_convert_and_check (result_type, op0, complain);
5024 if (TREE_TYPE (op1) != real_type)
5025 op1 = cp_convert_and_check (real_type, op1, complain);
5027 else
5029 if (TREE_TYPE (op0) != real_type)
5030 op0 = cp_convert_and_check (real_type, op0, complain);
5031 if (TREE_TYPE (op1) != result_type)
5032 op1 = cp_convert_and_check (result_type, op1, complain);
5034 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5035 return error_mark_node;
5036 if (first_complex)
5038 op0 = save_expr (op0);
5039 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5040 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5041 switch (code)
5043 case MULT_EXPR:
5044 case TRUNC_DIV_EXPR:
5045 op1 = save_expr (op1);
5046 imag = build2 (resultcode, real_type, imag, op1);
5047 /* Fall through. */
5048 case PLUS_EXPR:
5049 case MINUS_EXPR:
5050 real = build2 (resultcode, real_type, real, op1);
5051 break;
5052 default:
5053 gcc_unreachable();
5056 else
5058 op1 = save_expr (op1);
5059 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5060 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5061 switch (code)
5063 case MULT_EXPR:
5064 op0 = save_expr (op0);
5065 imag = build2 (resultcode, real_type, op0, imag);
5066 /* Fall through. */
5067 case PLUS_EXPR:
5068 real = build2 (resultcode, real_type, op0, real);
5069 break;
5070 case MINUS_EXPR:
5071 real = build2 (resultcode, real_type, op0, real);
5072 imag = build1 (NEGATE_EXPR, real_type, imag);
5073 break;
5074 default:
5075 gcc_unreachable();
5078 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5079 return result;
5082 /* For certain operations (which identify themselves by shorten != 0)
5083 if both args were extended from the same smaller type,
5084 do the arithmetic in that type and then extend.
5086 shorten !=0 and !=1 indicates a bitwise operation.
5087 For them, this optimization is safe only if
5088 both args are zero-extended or both are sign-extended.
5089 Otherwise, we might change the result.
5090 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5091 but calculated in (unsigned short) it would be (unsigned short)-1. */
5093 if (shorten && none_complex)
5095 orig_type = result_type;
5096 final_type = result_type;
5097 result_type = shorten_binary_op (result_type, op0, op1,
5098 shorten == -1);
5101 /* Comparison operations are shortened too but differently.
5102 They identify themselves by setting short_compare = 1. */
5104 if (short_compare)
5106 /* We call shorten_compare only for diagnostic-reason. */
5107 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5108 xresult_type = result_type;
5109 enum tree_code xresultcode = resultcode;
5110 shorten_compare (location, &xop0, &xop1, &xresult_type,
5111 &xresultcode);
5114 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5115 && warn_sign_compare
5116 /* Do not warn until the template is instantiated; we cannot
5117 bound the ranges of the arguments until that point. */
5118 && !processing_template_decl
5119 && (complain & tf_warning)
5120 && c_inhibit_evaluation_warnings == 0
5121 /* Even unsigned enum types promote to signed int. We don't
5122 want to issue -Wsign-compare warnings for this case. */
5123 && !enum_cast_to_int (orig_op0)
5124 && !enum_cast_to_int (orig_op1))
5126 tree oop0 = maybe_constant_value (orig_op0);
5127 tree oop1 = maybe_constant_value (orig_op1);
5129 if (TREE_CODE (oop0) != INTEGER_CST)
5130 oop0 = cp_fully_fold (orig_op0);
5131 if (TREE_CODE (oop1) != INTEGER_CST)
5132 oop1 = cp_fully_fold (orig_op1);
5133 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5134 result_type, resultcode);
5138 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5139 Then the expression will be built.
5140 It will be given type FINAL_TYPE if that is nonzero;
5141 otherwise, it will be given type RESULT_TYPE. */
5142 if (! converted)
5144 if (TREE_TYPE (op0) != result_type)
5145 op0 = cp_convert_and_check (result_type, op0, complain);
5146 if (TREE_TYPE (op1) != result_type)
5147 op1 = cp_convert_and_check (result_type, op1, complain);
5149 if (op0 == error_mark_node || op1 == error_mark_node)
5150 return error_mark_node;
5153 if (build_type == NULL_TREE)
5154 build_type = result_type;
5156 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
5157 | SANITIZE_FLOAT_DIVIDE))
5158 && !processing_template_decl
5159 && do_ubsan_in_current_function ()
5160 && (doing_div_or_mod || doing_shift))
5162 /* OP0 and/or OP1 might have side-effects. */
5163 op0 = cp_save_expr (op0);
5164 op1 = cp_save_expr (op1);
5165 op0 = fold_non_dependent_expr (op0);
5166 op1 = fold_non_dependent_expr (op1);
5167 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
5168 | SANITIZE_FLOAT_DIVIDE)))
5170 /* For diagnostics we want to use the promoted types without
5171 shorten_binary_op. So convert the arguments to the
5172 original result_type. */
5173 tree cop0 = op0;
5174 tree cop1 = op1;
5175 if (orig_type != NULL && result_type != orig_type)
5177 cop0 = cp_convert (orig_type, op0, complain);
5178 cop1 = cp_convert (orig_type, op1, complain);
5180 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5182 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5183 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5186 result = build2_loc (location, resultcode, build_type, op0, op1);
5187 if (final_type != 0)
5188 result = cp_convert (final_type, result, complain);
5190 if (instrument_expr != NULL)
5191 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5192 instrument_expr, result);
5194 if (!processing_template_decl)
5196 op0 = cp_fully_fold (op0);
5197 /* Only consider the second argument if the first isn't overflowed. */
5198 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5199 return result;
5200 op1 = cp_fully_fold (op1);
5201 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5202 return result;
5204 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5205 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5206 return result;
5208 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5209 if (TREE_OVERFLOW_P (result_ovl))
5210 overflow_warning (location, result_ovl);
5212 return result;
5215 /* Build a VEC_PERM_EXPR.
5216 This is a simple wrapper for c_build_vec_perm_expr. */
5217 tree
5218 build_x_vec_perm_expr (location_t loc,
5219 tree arg0, tree arg1, tree arg2,
5220 tsubst_flags_t complain)
5222 tree orig_arg0 = arg0;
5223 tree orig_arg1 = arg1;
5224 tree orig_arg2 = arg2;
5225 if (processing_template_decl)
5227 if (type_dependent_expression_p (arg0)
5228 || type_dependent_expression_p (arg1)
5229 || type_dependent_expression_p (arg2))
5230 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5231 arg0 = build_non_dependent_expr (arg0);
5232 if (arg1)
5233 arg1 = build_non_dependent_expr (arg1);
5234 arg2 = build_non_dependent_expr (arg2);
5236 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5237 if (processing_template_decl && exp != error_mark_node)
5238 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5239 orig_arg1, orig_arg2);
5240 return exp;
5243 /* Return a tree for the sum or difference (RESULTCODE says which)
5244 of pointer PTROP and integer INTOP. */
5246 static tree
5247 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5248 tree intop, tsubst_flags_t complain)
5250 tree res_type = TREE_TYPE (ptrop);
5252 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5253 in certain circumstance (when it's valid to do so). So we need
5254 to make sure it's complete. We don't need to check here, if we
5255 can actually complete it at all, as those checks will be done in
5256 pointer_int_sum() anyway. */
5257 complete_type (TREE_TYPE (res_type));
5259 return pointer_int_sum (loc, resultcode, ptrop,
5260 intop, complain & tf_warning_or_error);
5263 /* Return a tree for the difference of pointers OP0 and OP1.
5264 The resulting tree has type int. */
5266 static tree
5267 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5268 tsubst_flags_t complain)
5270 tree result;
5271 tree restype = ptrdiff_type_node;
5272 tree target_type = TREE_TYPE (ptrtype);
5274 if (!complete_type_or_else (target_type, NULL_TREE))
5275 return error_mark_node;
5277 if (VOID_TYPE_P (target_type))
5279 if (complain & tf_error)
5280 permerror (loc, "ISO C++ forbids using pointer of "
5281 "type %<void *%> in subtraction");
5282 else
5283 return error_mark_node;
5285 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5287 if (complain & tf_error)
5288 permerror (loc, "ISO C++ forbids using pointer to "
5289 "a function in subtraction");
5290 else
5291 return error_mark_node;
5293 if (TREE_CODE (target_type) == METHOD_TYPE)
5295 if (complain & tf_error)
5296 permerror (loc, "ISO C++ forbids using pointer to "
5297 "a method in subtraction");
5298 else
5299 return error_mark_node;
5302 /* First do the subtraction as integers;
5303 then drop through to build the divide operator. */
5305 op0 = cp_build_binary_op (loc,
5306 MINUS_EXPR,
5307 cp_convert (restype, op0, complain),
5308 cp_convert (restype, op1, complain),
5309 complain);
5311 /* This generates an error if op1 is a pointer to an incomplete type. */
5312 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5314 if (complain & tf_error)
5315 error_at (loc, "invalid use of a pointer to an incomplete type in "
5316 "pointer arithmetic");
5317 else
5318 return error_mark_node;
5321 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5323 if (complain & tf_error)
5324 error_at (loc, "arithmetic on pointer to an empty aggregate");
5325 else
5326 return error_mark_node;
5329 op1 = (TYPE_PTROB_P (ptrtype)
5330 ? size_in_bytes_loc (loc, target_type)
5331 : integer_one_node);
5333 /* Do the division. */
5335 result = build2_loc (loc, EXACT_DIV_EXPR, restype, op0,
5336 cp_convert (restype, op1, complain));
5337 return result;
5340 /* Construct and perhaps optimize a tree representation
5341 for a unary operation. CODE, a tree_code, specifies the operation
5342 and XARG is the operand. */
5344 tree
5345 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5346 tsubst_flags_t complain)
5348 tree orig_expr = xarg;
5349 tree exp;
5350 int ptrmem = 0;
5351 tree overload = NULL_TREE;
5353 if (processing_template_decl)
5355 if (type_dependent_expression_p (xarg))
5356 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5358 xarg = build_non_dependent_expr (xarg);
5361 exp = NULL_TREE;
5363 /* [expr.unary.op] says:
5365 The address of an object of incomplete type can be taken.
5367 (And is just the ordinary address operator, not an overloaded
5368 "operator &".) However, if the type is a template
5369 specialization, we must complete the type at this point so that
5370 an overloaded "operator &" will be available if required. */
5371 if (code == ADDR_EXPR
5372 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5373 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5374 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5375 || (TREE_CODE (xarg) == OFFSET_REF)))
5376 /* Don't look for a function. */;
5377 else
5378 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5379 NULL_TREE, &overload, complain);
5381 if (!exp && code == ADDR_EXPR)
5383 if (is_overloaded_fn (xarg))
5385 tree fn = get_first_fn (xarg);
5386 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5388 if (complain & tf_error)
5389 error (DECL_CONSTRUCTOR_P (fn)
5390 ? G_("taking address of constructor %qE")
5391 : G_("taking address of destructor %qE"),
5392 xarg.get_value ());
5393 return error_mark_node;
5397 /* A pointer to member-function can be formed only by saying
5398 &X::mf. */
5399 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5400 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5402 if (TREE_CODE (xarg) != OFFSET_REF
5403 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5405 if (complain & tf_error)
5407 error ("invalid use of %qE to form a "
5408 "pointer-to-member-function", xarg.get_value ());
5409 if (TREE_CODE (xarg) != OFFSET_REF)
5410 inform (input_location, " a qualified-id is required");
5412 return error_mark_node;
5414 else
5416 if (complain & tf_error)
5417 error ("parentheses around %qE cannot be used to form a"
5418 " pointer-to-member-function",
5419 xarg.get_value ());
5420 else
5421 return error_mark_node;
5422 PTRMEM_OK_P (xarg) = 1;
5426 if (TREE_CODE (xarg) == OFFSET_REF)
5428 ptrmem = PTRMEM_OK_P (xarg);
5430 if (!ptrmem && !flag_ms_extensions
5431 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5433 /* A single non-static member, make sure we don't allow a
5434 pointer-to-member. */
5435 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5436 TREE_OPERAND (xarg, 0),
5437 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5438 PTRMEM_OK_P (xarg) = ptrmem;
5442 exp = cp_build_addr_expr_strict (xarg, complain);
5445 if (processing_template_decl && exp != error_mark_node)
5447 if (overload != NULL_TREE)
5448 return (build_min_non_dep_op_overload
5449 (code, exp, overload, orig_expr, integer_zero_node));
5451 exp = build_min_non_dep (code, exp, orig_expr,
5452 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5454 if (TREE_CODE (exp) == ADDR_EXPR)
5455 PTRMEM_OK_P (exp) = ptrmem;
5456 return exp;
5459 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5460 constants, where a null value is represented by an INTEGER_CST of
5461 -1. */
5463 tree
5464 cp_truthvalue_conversion (tree expr)
5466 tree type = TREE_TYPE (expr);
5467 if (TYPE_PTR_OR_PTRMEM_P (type)
5468 /* Avoid ICE on invalid use of non-static member function. */
5469 || TREE_CODE (expr) == FUNCTION_DECL)
5470 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, 1);
5471 else
5472 return c_common_truthvalue_conversion (input_location, expr);
5475 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5477 tree
5478 condition_conversion (tree expr)
5480 tree t;
5481 if (processing_template_decl)
5482 return expr;
5483 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5484 tf_warning_or_error, LOOKUP_NORMAL);
5485 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5486 return t;
5489 /* Returns the address of T. This function will fold away
5490 ADDR_EXPR of INDIRECT_REF. */
5492 tree
5493 build_address (tree t)
5495 if (error_operand_p (t) || !cxx_mark_addressable (t))
5496 return error_mark_node;
5497 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5498 t = build_fold_addr_expr (t);
5499 if (TREE_CODE (t) != ADDR_EXPR)
5500 t = rvalue (t);
5501 return t;
5504 /* Return a NOP_EXPR converting EXPR to TYPE. */
5506 tree
5507 build_nop (tree type, tree expr)
5509 if (type == error_mark_node || error_operand_p (expr))
5510 return expr;
5511 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5514 /* Take the address of ARG, whatever that means under C++ semantics.
5515 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5516 and class rvalues as well.
5518 Nothing should call this function directly; instead, callers should use
5519 cp_build_addr_expr or cp_build_addr_expr_strict. */
5521 static tree
5522 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5524 tree argtype;
5525 tree val;
5527 if (!arg || error_operand_p (arg))
5528 return error_mark_node;
5530 arg = mark_lvalue_use (arg);
5531 argtype = lvalue_type (arg);
5533 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5535 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5536 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5538 /* They're trying to take the address of a unique non-static
5539 member function. This is ill-formed (except in MS-land),
5540 but let's try to DTRT.
5541 Note: We only handle unique functions here because we don't
5542 want to complain if there's a static overload; non-unique
5543 cases will be handled by instantiate_type. But we need to
5544 handle this case here to allow casts on the resulting PMF.
5545 We could defer this in non-MS mode, but it's easier to give
5546 a useful error here. */
5548 /* Inside constant member functions, the `this' pointer
5549 contains an extra const qualifier. TYPE_MAIN_VARIANT
5550 is used here to remove this const from the diagnostics
5551 and the created OFFSET_REF. */
5552 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5553 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5554 if (!mark_used (fn, complain) && !(complain & tf_error))
5555 return error_mark_node;
5557 if (! flag_ms_extensions)
5559 tree name = DECL_NAME (fn);
5560 if (!(complain & tf_error))
5561 return error_mark_node;
5562 else if (current_class_type
5563 && TREE_OPERAND (arg, 0) == current_class_ref)
5564 /* An expression like &memfn. */
5565 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5566 " or parenthesized non-static member function to form"
5567 " a pointer to member function. Say %<&%T::%D%>",
5568 base, name);
5569 else
5570 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5571 " function to form a pointer to member function."
5572 " Say %<&%T::%D%>",
5573 base, name);
5575 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5578 /* Uninstantiated types are all functions. Taking the
5579 address of a function is a no-op, so just return the
5580 argument. */
5581 if (type_unknown_p (arg))
5582 return build1 (ADDR_EXPR, unknown_type_node, arg);
5584 if (TREE_CODE (arg) == OFFSET_REF)
5585 /* We want a pointer to member; bypass all the code for actually taking
5586 the address of something. */
5587 goto offset_ref;
5589 /* Anything not already handled and not a true memory reference
5590 is an error. */
5591 if (TREE_CODE (argtype) != FUNCTION_TYPE
5592 && TREE_CODE (argtype) != METHOD_TYPE)
5594 cp_lvalue_kind kind = lvalue_kind (arg);
5595 if (kind == clk_none)
5597 if (complain & tf_error)
5598 lvalue_error (input_location, lv_addressof);
5599 return error_mark_node;
5601 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5603 if (!(complain & tf_error))
5604 return error_mark_node;
5605 if (kind & clk_class)
5606 /* Make this a permerror because we used to accept it. */
5607 permerror (input_location, "taking address of temporary");
5608 else
5609 error ("taking address of xvalue (rvalue reference)");
5613 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5615 tree type = build_pointer_type (TREE_TYPE (argtype));
5616 arg = build1 (CONVERT_EXPR, type, arg);
5617 return arg;
5619 else if (pedantic && DECL_MAIN_P (arg))
5621 /* ARM $3.4 */
5622 /* Apparently a lot of autoconf scripts for C++ packages do this,
5623 so only complain if -Wpedantic. */
5624 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5625 pedwarn (input_location, OPT_Wpedantic,
5626 "ISO C++ forbids taking address of function %<::main%>");
5627 else if (flag_pedantic_errors)
5628 return error_mark_node;
5631 /* Let &* cancel out to simplify resulting code. */
5632 if (INDIRECT_REF_P (arg))
5634 arg = TREE_OPERAND (arg, 0);
5635 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5637 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5638 arg = build1 (CONVERT_EXPR, type, arg);
5640 else
5641 /* Don't let this be an lvalue. */
5642 arg = rvalue (arg);
5643 return arg;
5646 /* ??? Cope with user tricks that amount to offsetof. */
5647 if (TREE_CODE (argtype) != FUNCTION_TYPE
5648 && TREE_CODE (argtype) != METHOD_TYPE
5649 && argtype != unknown_type_node
5650 && (val = get_base_address (arg))
5651 && COMPLETE_TYPE_P (TREE_TYPE (val))
5652 && INDIRECT_REF_P (val)
5653 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5655 tree type = build_pointer_type (argtype);
5656 return fold_convert (type, fold_offsetof_1 (arg));
5659 /* Handle complex lvalues (when permitted)
5660 by reduction to simpler cases. */
5661 val = unary_complex_lvalue (ADDR_EXPR, arg);
5662 if (val != 0)
5663 return val;
5665 switch (TREE_CODE (arg))
5667 CASE_CONVERT:
5668 case FLOAT_EXPR:
5669 case FIX_TRUNC_EXPR:
5670 /* We should have handled this above in the lvalue_kind check. */
5671 gcc_unreachable ();
5672 break;
5674 case BASELINK:
5675 arg = BASELINK_FUNCTIONS (arg);
5676 /* Fall through. */
5678 case OVERLOAD:
5679 arg = OVL_CURRENT (arg);
5680 break;
5682 case OFFSET_REF:
5683 offset_ref:
5684 /* Turn a reference to a non-static data member into a
5685 pointer-to-member. */
5687 tree type;
5688 tree t;
5690 gcc_assert (PTRMEM_OK_P (arg));
5692 t = TREE_OPERAND (arg, 1);
5693 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5695 if (complain & tf_error)
5696 error ("cannot create pointer to reference member %qD", t);
5697 return error_mark_node;
5700 type = build_ptrmem_type (context_for_name_lookup (t),
5701 TREE_TYPE (t));
5702 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5703 return t;
5706 default:
5707 break;
5710 if (argtype != error_mark_node)
5711 argtype = build_pointer_type (argtype);
5713 /* In a template, we are processing a non-dependent expression
5714 so we can just form an ADDR_EXPR with the correct type. */
5715 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5717 val = build_address (arg);
5718 if (TREE_CODE (arg) == OFFSET_REF)
5719 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5721 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5723 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5725 /* We can only get here with a single static member
5726 function. */
5727 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5728 && DECL_STATIC_FUNCTION_P (fn));
5729 if (!mark_used (fn, complain) && !(complain & tf_error))
5730 return error_mark_node;
5731 val = build_address (fn);
5732 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5733 /* Do not lose object's side effects. */
5734 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5735 TREE_OPERAND (arg, 0), val);
5737 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5739 if (complain & tf_error)
5740 error ("attempt to take address of bit-field structure member %qD",
5741 TREE_OPERAND (arg, 1));
5742 return error_mark_node;
5744 else
5746 tree object = TREE_OPERAND (arg, 0);
5747 tree field = TREE_OPERAND (arg, 1);
5748 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5749 (TREE_TYPE (object), decl_type_context (field)));
5750 val = build_address (arg);
5753 if (TYPE_PTR_P (argtype)
5754 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5756 build_ptrmemfunc_type (argtype);
5757 val = build_ptrmemfunc (argtype, val, 0,
5758 /*c_cast_p=*/false,
5759 complain);
5762 return val;
5765 /* Take the address of ARG if it has one, even if it's an rvalue. */
5767 tree
5768 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5770 return cp_build_addr_expr_1 (arg, 0, complain);
5773 /* Take the address of ARG, but only if it's an lvalue. */
5775 static tree
5776 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5778 return cp_build_addr_expr_1 (arg, 1, complain);
5781 /* C++: Must handle pointers to members.
5783 Perhaps type instantiation should be extended to handle conversion
5784 from aggregates to types we don't yet know we want? (Or are those
5785 cases typically errors which should be reported?)
5787 NOCONVERT suppresses the default promotions (such as from short to int). */
5789 tree
5790 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
5791 tsubst_flags_t complain)
5793 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5794 tree arg = xarg;
5795 tree argtype = 0;
5796 const char *errstring = NULL;
5797 tree val;
5798 const char *invalid_op_diag;
5800 if (!arg || error_operand_p (arg))
5801 return error_mark_node;
5803 if ((invalid_op_diag
5804 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5805 ? CONVERT_EXPR
5806 : code),
5807 TREE_TYPE (xarg))))
5809 if (complain & tf_error)
5810 error (invalid_op_diag);
5811 return error_mark_node;
5814 switch (code)
5816 case UNARY_PLUS_EXPR:
5817 case NEGATE_EXPR:
5819 int flags = WANT_ARITH | WANT_ENUM;
5820 /* Unary plus (but not unary minus) is allowed on pointers. */
5821 if (code == UNARY_PLUS_EXPR)
5822 flags |= WANT_POINTER;
5823 arg = build_expr_type_conversion (flags, arg, true);
5824 if (!arg)
5825 errstring = (code == NEGATE_EXPR
5826 ? _("wrong type argument to unary minus")
5827 : _("wrong type argument to unary plus"));
5828 else
5830 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5831 arg = cp_perform_integral_promotions (arg, complain);
5833 /* Make sure the result is not an lvalue: a unary plus or minus
5834 expression is always a rvalue. */
5835 arg = rvalue (arg);
5838 break;
5840 case BIT_NOT_EXPR:
5841 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5843 code = CONJ_EXPR;
5844 if (!noconvert)
5846 arg = cp_default_conversion (arg, complain);
5847 if (arg == error_mark_node)
5848 return error_mark_node;
5851 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5852 | WANT_VECTOR_OR_COMPLEX,
5853 arg, true)))
5854 errstring = _("wrong type argument to bit-complement");
5855 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5856 arg = cp_perform_integral_promotions (arg, complain);
5857 break;
5859 case ABS_EXPR:
5860 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5861 errstring = _("wrong type argument to abs");
5862 else if (!noconvert)
5864 arg = cp_default_conversion (arg, complain);
5865 if (arg == error_mark_node)
5866 return error_mark_node;
5868 break;
5870 case CONJ_EXPR:
5871 /* Conjugating a real value is a no-op, but allow it anyway. */
5872 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5873 errstring = _("wrong type argument to conjugation");
5874 else if (!noconvert)
5876 arg = cp_default_conversion (arg, complain);
5877 if (arg == error_mark_node)
5878 return error_mark_node;
5880 break;
5882 case TRUTH_NOT_EXPR:
5883 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5884 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5885 build_zero_cst (TREE_TYPE (arg)), complain);
5886 arg = perform_implicit_conversion (boolean_type_node, arg,
5887 complain);
5888 val = invert_truthvalue_loc (input_location, arg);
5889 if (arg != error_mark_node)
5890 return val;
5891 errstring = _("in argument to unary !");
5892 break;
5894 case NOP_EXPR:
5895 break;
5897 case REALPART_EXPR:
5898 case IMAGPART_EXPR:
5899 arg = build_real_imag_expr (input_location, code, arg);
5900 return arg;
5902 case PREINCREMENT_EXPR:
5903 case POSTINCREMENT_EXPR:
5904 case PREDECREMENT_EXPR:
5905 case POSTDECREMENT_EXPR:
5906 /* Handle complex lvalues (when permitted)
5907 by reduction to simpler cases. */
5909 val = unary_complex_lvalue (code, arg);
5910 if (val != 0)
5911 return val;
5913 arg = mark_lvalue_use (arg);
5915 /* Increment or decrement the real part of the value,
5916 and don't change the imaginary part. */
5917 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5919 tree real, imag;
5921 arg = cp_stabilize_reference (arg);
5922 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
5923 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
5924 real = cp_build_unary_op (code, real, true, complain);
5925 if (real == error_mark_node || imag == error_mark_node)
5926 return error_mark_node;
5927 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5928 real, imag);
5931 /* Report invalid types. */
5933 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5934 arg, true)))
5936 if (code == PREINCREMENT_EXPR)
5937 errstring = _("no pre-increment operator for type");
5938 else if (code == POSTINCREMENT_EXPR)
5939 errstring = _("no post-increment operator for type");
5940 else if (code == PREDECREMENT_EXPR)
5941 errstring = _("no pre-decrement operator for type");
5942 else
5943 errstring = _("no post-decrement operator for type");
5944 break;
5946 else if (arg == error_mark_node)
5947 return error_mark_node;
5949 /* Report something read-only. */
5951 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5952 || TREE_READONLY (arg))
5954 if (complain & tf_error)
5955 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5956 || code == POSTINCREMENT_EXPR)
5957 ? lv_increment : lv_decrement));
5958 else
5959 return error_mark_node;
5963 tree inc;
5964 tree declared_type = unlowered_expr_type (arg);
5966 argtype = TREE_TYPE (arg);
5968 /* ARM $5.2.5 last annotation says this should be forbidden. */
5969 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5971 if (complain & tf_error)
5972 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5973 ? G_("ISO C++ forbids incrementing an enum")
5974 : G_("ISO C++ forbids decrementing an enum"));
5975 else
5976 return error_mark_node;
5979 /* Compute the increment. */
5981 if (TYPE_PTR_P (argtype))
5983 tree type = complete_type (TREE_TYPE (argtype));
5985 if (!COMPLETE_OR_VOID_TYPE_P (type))
5987 if (complain & tf_error)
5988 error (((code == PREINCREMENT_EXPR
5989 || code == POSTINCREMENT_EXPR))
5990 ? G_("cannot increment a pointer to incomplete type %qT")
5991 : G_("cannot decrement a pointer to incomplete type %qT"),
5992 TREE_TYPE (argtype));
5993 else
5994 return error_mark_node;
5996 else if (!TYPE_PTROB_P (argtype))
5998 if (complain & tf_error)
5999 pedwarn (input_location, OPT_Wpointer_arith,
6000 (code == PREINCREMENT_EXPR
6001 || code == POSTINCREMENT_EXPR)
6002 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6003 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6004 argtype);
6005 else
6006 return error_mark_node;
6009 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6011 else
6012 inc = VECTOR_TYPE_P (argtype)
6013 ? build_one_cst (argtype)
6014 : integer_one_node;
6016 inc = cp_convert (argtype, inc, complain);
6018 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6019 need to ask Objective-C to build the increment or decrement
6020 expression for it. */
6021 if (objc_is_property_ref (arg))
6022 return objc_build_incr_expr_for_property_ref (input_location, code,
6023 arg, inc);
6025 /* Complain about anything else that is not a true lvalue. */
6026 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6027 || code == POSTINCREMENT_EXPR)
6028 ? lv_increment : lv_decrement),
6029 complain))
6030 return error_mark_node;
6032 /* Forbid using -- or ++ in C++17 on `bool'. */
6033 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6035 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6037 if (complain & tf_error)
6038 error ("use of an operand of type %qT in %<operator--%> "
6039 "is forbidden", boolean_type_node);
6040 return error_mark_node;
6042 else
6044 if (cxx_dialect >= cxx1z)
6046 if (complain & tf_error)
6047 error ("use of an operand of type %qT in "
6048 "%<operator++%> is forbidden in C++1z",
6049 boolean_type_node);
6050 return error_mark_node;
6052 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6053 else if (!in_system_header_at (input_location))
6054 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6055 "in %<operator++%> is deprecated",
6056 boolean_type_node);
6058 val = boolean_increment (code, arg);
6060 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6061 /* An rvalue has no cv-qualifiers. */
6062 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6063 else
6064 val = build2 (code, TREE_TYPE (arg), arg, inc);
6066 TREE_SIDE_EFFECTS (val) = 1;
6067 return val;
6070 case ADDR_EXPR:
6071 /* Note that this operation never does default_conversion
6072 regardless of NOCONVERT. */
6073 return cp_build_addr_expr (arg, complain);
6075 default:
6076 break;
6079 if (!errstring)
6081 if (argtype == 0)
6082 argtype = TREE_TYPE (arg);
6083 return build1 (code, argtype, arg);
6086 if (complain & tf_error)
6087 error ("%s", errstring);
6088 return error_mark_node;
6091 /* Hook for the c-common bits that build a unary op. */
6092 tree
6093 build_unary_op (location_t /*location*/,
6094 enum tree_code code, tree xarg, bool noconvert)
6096 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6099 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6100 for certain kinds of expressions which are not really lvalues
6101 but which we can accept as lvalues.
6103 If ARG is not a kind of expression we can handle, return
6104 NULL_TREE. */
6106 tree
6107 unary_complex_lvalue (enum tree_code code, tree arg)
6109 /* Inside a template, making these kinds of adjustments is
6110 pointless; we are only concerned with the type of the
6111 expression. */
6112 if (processing_template_decl)
6113 return NULL_TREE;
6115 /* Handle (a, b) used as an "lvalue". */
6116 if (TREE_CODE (arg) == COMPOUND_EXPR)
6118 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6119 tf_warning_or_error);
6120 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6121 TREE_OPERAND (arg, 0), real_result);
6124 /* Handle (a ? b : c) used as an "lvalue". */
6125 if (TREE_CODE (arg) == COND_EXPR
6126 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6127 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6129 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6130 if (TREE_CODE (arg) == MODIFY_EXPR
6131 || TREE_CODE (arg) == PREINCREMENT_EXPR
6132 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6134 tree lvalue = TREE_OPERAND (arg, 0);
6135 if (TREE_SIDE_EFFECTS (lvalue))
6137 lvalue = cp_stabilize_reference (lvalue);
6138 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6139 lvalue, TREE_OPERAND (arg, 1));
6141 return unary_complex_lvalue
6142 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
6145 if (code != ADDR_EXPR)
6146 return NULL_TREE;
6148 /* Handle (a = b) used as an "lvalue" for `&'. */
6149 if (TREE_CODE (arg) == MODIFY_EXPR
6150 || TREE_CODE (arg) == INIT_EXPR)
6152 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6153 tf_warning_or_error);
6154 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6155 arg, real_result);
6156 TREE_NO_WARNING (arg) = 1;
6157 return arg;
6160 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6161 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6162 || TREE_CODE (arg) == OFFSET_REF)
6163 return NULL_TREE;
6165 /* We permit compiler to make function calls returning
6166 objects of aggregate type look like lvalues. */
6168 tree targ = arg;
6170 if (TREE_CODE (targ) == SAVE_EXPR)
6171 targ = TREE_OPERAND (targ, 0);
6173 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6175 if (TREE_CODE (arg) == SAVE_EXPR)
6176 targ = arg;
6177 else
6178 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6179 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6182 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6183 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6184 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6187 /* Don't let anything else be handled specially. */
6188 return NULL_TREE;
6191 /* Mark EXP saying that we need to be able to take the
6192 address of it; it should not be allocated in a register.
6193 Value is true if successful.
6195 C++: we do not allow `current_class_ptr' to be addressable. */
6197 bool
6198 cxx_mark_addressable (tree exp)
6200 tree x = exp;
6202 while (1)
6203 switch (TREE_CODE (x))
6205 case ADDR_EXPR:
6206 case COMPONENT_REF:
6207 case ARRAY_REF:
6208 case REALPART_EXPR:
6209 case IMAGPART_EXPR:
6210 x = TREE_OPERAND (x, 0);
6211 break;
6213 case PARM_DECL:
6214 if (x == current_class_ptr)
6216 error ("cannot take the address of %<this%>, which is an rvalue expression");
6217 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6218 return true;
6220 /* Fall through. */
6222 case VAR_DECL:
6223 /* Caller should not be trying to mark initialized
6224 constant fields addressable. */
6225 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6226 || DECL_IN_AGGR_P (x) == 0
6227 || TREE_STATIC (x)
6228 || DECL_EXTERNAL (x));
6229 /* Fall through. */
6231 case RESULT_DECL:
6232 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6233 && !DECL_ARTIFICIAL (x))
6235 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6237 error
6238 ("address of explicit register variable %qD requested", x);
6239 return false;
6241 else if (extra_warnings)
6242 warning
6243 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6245 TREE_ADDRESSABLE (x) = 1;
6246 return true;
6248 case CONST_DECL:
6249 case FUNCTION_DECL:
6250 TREE_ADDRESSABLE (x) = 1;
6251 return true;
6253 case CONSTRUCTOR:
6254 TREE_ADDRESSABLE (x) = 1;
6255 return true;
6257 case TARGET_EXPR:
6258 TREE_ADDRESSABLE (x) = 1;
6259 cxx_mark_addressable (TREE_OPERAND (x, 0));
6260 return true;
6262 default:
6263 return true;
6267 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6269 tree
6270 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6271 tsubst_flags_t complain)
6273 tree orig_ifexp = ifexp;
6274 tree orig_op1 = op1;
6275 tree orig_op2 = op2;
6276 tree expr;
6278 if (processing_template_decl)
6280 /* The standard says that the expression is type-dependent if
6281 IFEXP is type-dependent, even though the eventual type of the
6282 expression doesn't dependent on IFEXP. */
6283 if (type_dependent_expression_p (ifexp)
6284 /* As a GNU extension, the middle operand may be omitted. */
6285 || (op1 && type_dependent_expression_p (op1))
6286 || type_dependent_expression_p (op2))
6287 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6288 ifexp = build_non_dependent_expr (ifexp);
6289 if (op1)
6290 op1 = build_non_dependent_expr (op1);
6291 op2 = build_non_dependent_expr (op2);
6294 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6295 if (processing_template_decl && expr != error_mark_node)
6297 tree min = build_min_non_dep (COND_EXPR, expr,
6298 orig_ifexp, orig_op1, orig_op2);
6299 /* Remember that the result is an lvalue or xvalue. */
6300 if (glvalue_p (expr) && !glvalue_p (min))
6301 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6302 !lvalue_p (expr));
6303 expr = convert_from_reference (min);
6305 return expr;
6308 /* Given a list of expressions, return a compound expression
6309 that performs them all and returns the value of the last of them. */
6311 tree
6312 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6313 tsubst_flags_t complain)
6315 tree expr = TREE_VALUE (list);
6317 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6318 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6320 if (complain & tf_error)
6321 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6322 "list-initializer for non-class type must not "
6323 "be parenthesized");
6324 else
6325 return error_mark_node;
6328 if (TREE_CHAIN (list))
6330 if (complain & tf_error)
6331 switch (exp)
6333 case ELK_INIT:
6334 permerror (input_location, "expression list treated as compound "
6335 "expression in initializer");
6336 break;
6337 case ELK_MEM_INIT:
6338 permerror (input_location, "expression list treated as compound "
6339 "expression in mem-initializer");
6340 break;
6341 case ELK_FUNC_CAST:
6342 permerror (input_location, "expression list treated as compound "
6343 "expression in functional cast");
6344 break;
6345 default:
6346 gcc_unreachable ();
6348 else
6349 return error_mark_node;
6351 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6352 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6353 expr, TREE_VALUE (list), complain);
6356 return expr;
6359 /* Like build_x_compound_expr_from_list, but using a VEC. */
6361 tree
6362 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6363 tsubst_flags_t complain)
6365 if (vec_safe_is_empty (vec))
6366 return NULL_TREE;
6367 else if (vec->length () == 1)
6368 return (*vec)[0];
6369 else
6371 tree expr;
6372 unsigned int ix;
6373 tree t;
6375 if (msg != NULL)
6377 if (complain & tf_error)
6378 permerror (input_location,
6379 "%s expression list treated as compound expression",
6380 msg);
6381 else
6382 return error_mark_node;
6385 expr = (*vec)[0];
6386 for (ix = 1; vec->iterate (ix, &t); ++ix)
6387 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6388 t, complain);
6390 return expr;
6394 /* Handle overloading of the ',' operator when needed. */
6396 tree
6397 build_x_compound_expr (location_t loc, tree op1, tree op2,
6398 tsubst_flags_t complain)
6400 tree result;
6401 tree orig_op1 = op1;
6402 tree orig_op2 = op2;
6403 tree overload = NULL_TREE;
6405 if (processing_template_decl)
6407 if (type_dependent_expression_p (op1)
6408 || type_dependent_expression_p (op2))
6409 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6410 op1 = build_non_dependent_expr (op1);
6411 op2 = build_non_dependent_expr (op2);
6414 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6415 NULL_TREE, &overload, complain);
6416 if (!result)
6417 result = cp_build_compound_expr (op1, op2, complain);
6419 if (processing_template_decl && result != error_mark_node)
6421 if (overload != NULL_TREE)
6422 return (build_min_non_dep_op_overload
6423 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6425 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6428 return result;
6431 /* Like cp_build_compound_expr, but for the c-common bits. */
6433 tree
6434 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6436 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6439 /* Build a compound expression. */
6441 tree
6442 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6444 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6446 if (lhs == error_mark_node || rhs == error_mark_node)
6447 return error_mark_node;
6449 if (flag_cilkplus
6450 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6451 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6453 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6454 : EXPR_LOCATION (rhs));
6455 error_at (loc,
6456 "spawned function call cannot be part of a comma expression");
6457 return error_mark_node;
6460 if (TREE_CODE (rhs) == TARGET_EXPR)
6462 /* If the rhs is a TARGET_EXPR, then build the compound
6463 expression inside the target_expr's initializer. This
6464 helps the compiler to eliminate unnecessary temporaries. */
6465 tree init = TREE_OPERAND (rhs, 1);
6467 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6468 TREE_OPERAND (rhs, 1) = init;
6470 return rhs;
6473 if (type_unknown_p (rhs))
6475 if (complain & tf_error)
6476 error ("no context to resolve type of %qE", rhs);
6477 return error_mark_node;
6480 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6483 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6484 casts away constness. CAST gives the type of cast. Returns true
6485 if the cast is ill-formed, false if it is well-formed.
6487 ??? This function warns for casting away any qualifier not just
6488 const. We would like to specify exactly what qualifiers are casted
6489 away.
6492 static bool
6493 check_for_casting_away_constness (tree src_type, tree dest_type,
6494 enum tree_code cast, tsubst_flags_t complain)
6496 /* C-style casts are allowed to cast away constness. With
6497 WARN_CAST_QUAL, we still want to issue a warning. */
6498 if (cast == CAST_EXPR && !warn_cast_qual)
6499 return false;
6501 if (!casts_away_constness (src_type, dest_type, complain))
6502 return false;
6504 switch (cast)
6506 case CAST_EXPR:
6507 if (complain & tf_warning)
6508 warning (OPT_Wcast_qual,
6509 "cast from type %qT to type %qT casts away qualifiers",
6510 src_type, dest_type);
6511 return false;
6513 case STATIC_CAST_EXPR:
6514 if (complain & tf_error)
6515 error ("static_cast from type %qT to type %qT casts away qualifiers",
6516 src_type, dest_type);
6517 return true;
6519 case REINTERPRET_CAST_EXPR:
6520 if (complain & tf_error)
6521 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6522 src_type, dest_type);
6523 return true;
6525 default:
6526 gcc_unreachable();
6531 Warns if the cast from expression EXPR to type TYPE is useless.
6533 void
6534 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6536 if (warn_useless_cast
6537 && complain & tf_warning)
6539 if ((TREE_CODE (type) == REFERENCE_TYPE
6540 && (TYPE_REF_IS_RVALUE (type)
6541 ? xvalue_p (expr) : lvalue_p (expr))
6542 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6543 || same_type_p (TREE_TYPE (expr), type))
6544 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6548 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6549 (another pointer-to-member type in the same hierarchy) and return
6550 the converted expression. If ALLOW_INVERSE_P is permitted, a
6551 pointer-to-derived may be converted to pointer-to-base; otherwise,
6552 only the other direction is permitted. If C_CAST_P is true, this
6553 conversion is taking place as part of a C-style cast. */
6555 tree
6556 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6557 bool c_cast_p, tsubst_flags_t complain)
6559 if (TYPE_PTRDATAMEM_P (type))
6561 tree delta;
6563 if (TREE_CODE (expr) == PTRMEM_CST)
6564 expr = cplus_expand_constant (expr);
6565 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6566 TYPE_PTRMEM_CLASS_TYPE (type),
6567 allow_inverse_p,
6568 c_cast_p, complain);
6569 if (delta == error_mark_node)
6570 return error_mark_node;
6572 if (!integer_zerop (delta))
6574 tree cond, op1, op2;
6576 cond = cp_build_binary_op (input_location,
6577 EQ_EXPR,
6578 expr,
6579 build_int_cst (TREE_TYPE (expr), -1),
6580 complain);
6581 op1 = build_nop (ptrdiff_type_node, expr);
6582 op2 = cp_build_binary_op (input_location,
6583 PLUS_EXPR, op1, delta,
6584 complain);
6586 expr = fold_build3_loc (input_location,
6587 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6591 return build_nop (type, expr);
6593 else
6594 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6595 allow_inverse_p, c_cast_p, complain);
6598 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6599 this static_cast is being attempted as one of the possible casts
6600 allowed by a C-style cast. (In that case, accessibility of base
6601 classes is not considered, and it is OK to cast away
6602 constness.) Return the result of the cast. *VALID_P is set to
6603 indicate whether or not the cast was valid. */
6605 static tree
6606 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6607 bool *valid_p, tsubst_flags_t complain)
6609 tree intype;
6610 tree result;
6611 cp_lvalue_kind clk;
6613 /* Assume the cast is valid. */
6614 *valid_p = true;
6616 intype = unlowered_expr_type (expr);
6618 /* Save casted types in the function's used types hash table. */
6619 used_types_insert (type);
6621 /* [expr.static.cast]
6623 An lvalue of type "cv1 B", where B is a class type, can be cast
6624 to type "reference to cv2 D", where D is a class derived (clause
6625 _class.derived_) from B, if a valid standard conversion from
6626 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6627 same cv-qualification as, or greater cv-qualification than, cv1,
6628 and B is not a virtual base class of D. */
6629 /* We check this case before checking the validity of "TYPE t =
6630 EXPR;" below because for this case:
6632 struct B {};
6633 struct D : public B { D(const B&); };
6634 extern B& b;
6635 void f() { static_cast<const D&>(b); }
6637 we want to avoid constructing a new D. The standard is not
6638 completely clear about this issue, but our interpretation is
6639 consistent with other compilers. */
6640 if (TREE_CODE (type) == REFERENCE_TYPE
6641 && CLASS_TYPE_P (TREE_TYPE (type))
6642 && CLASS_TYPE_P (intype)
6643 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6644 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6645 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6646 build_pointer_type (TYPE_MAIN_VARIANT
6647 (TREE_TYPE (type))),
6648 complain)
6649 && (c_cast_p
6650 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6652 tree base;
6654 /* There is a standard conversion from "D*" to "B*" even if "B"
6655 is ambiguous or inaccessible. If this is really a
6656 static_cast, then we check both for inaccessibility and
6657 ambiguity. However, if this is a static_cast being performed
6658 because the user wrote a C-style cast, then accessibility is
6659 not considered. */
6660 base = lookup_base (TREE_TYPE (type), intype,
6661 c_cast_p ? ba_unique : ba_check,
6662 NULL, complain);
6663 expr = build_address (expr);
6665 if (flag_sanitize & SANITIZE_VPTR)
6667 tree ubsan_check
6668 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6669 intype, expr);
6670 if (ubsan_check)
6671 expr = ubsan_check;
6674 /* Convert from "B*" to "D*". This function will check that "B"
6675 is not a virtual base of "D". */
6676 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6677 complain);
6679 /* Convert the pointer to a reference -- but then remember that
6680 there are no expressions with reference type in C++.
6682 We call rvalue so that there's an actual tree code
6683 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6684 is a variable with the same type, the conversion would get folded
6685 away, leaving just the variable and causing lvalue_kind to give
6686 the wrong answer. */
6687 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6690 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6691 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6692 if (TREE_CODE (type) == REFERENCE_TYPE
6693 && TYPE_REF_IS_RVALUE (type)
6694 && (clk = real_lvalue_p (expr))
6695 && reference_related_p (TREE_TYPE (type), intype)
6696 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6698 if (clk == clk_ordinary)
6700 /* Handle the (non-bit-field) lvalue case here by casting to
6701 lvalue reference and then changing it to an rvalue reference.
6702 Casting an xvalue to rvalue reference will be handled by the
6703 main code path. */
6704 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6705 result = (perform_direct_initialization_if_possible
6706 (lref, expr, c_cast_p, complain));
6707 result = build1 (NON_LVALUE_EXPR, type, result);
6708 return convert_from_reference (result);
6710 else
6711 /* For a bit-field or packed field, bind to a temporary. */
6712 expr = rvalue (expr);
6715 /* Resolve overloaded address here rather than once in
6716 implicit_conversion and again in the inverse code below. */
6717 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6719 expr = instantiate_type (type, expr, complain);
6720 intype = TREE_TYPE (expr);
6723 /* [expr.static.cast]
6725 Any expression can be explicitly converted to type cv void. */
6726 if (VOID_TYPE_P (type))
6727 return convert_to_void (expr, ICV_CAST, complain);
6729 /* [class.abstract]
6730 An abstract class shall not be used ... as the type of an explicit
6731 conversion. */
6732 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6733 return error_mark_node;
6735 /* [expr.static.cast]
6737 An expression e can be explicitly converted to a type T using a
6738 static_cast of the form static_cast<T>(e) if the declaration T
6739 t(e);" is well-formed, for some invented temporary variable
6740 t. */
6741 result = perform_direct_initialization_if_possible (type, expr,
6742 c_cast_p, complain);
6743 if (result)
6745 result = convert_from_reference (result);
6747 /* [expr.static.cast]
6749 If T is a reference type, the result is an lvalue; otherwise,
6750 the result is an rvalue. */
6751 if (TREE_CODE (type) != REFERENCE_TYPE)
6753 result = rvalue (result);
6755 if (result == expr && SCALAR_TYPE_P (type))
6756 /* Leave some record of the cast. */
6757 result = build_nop (type, expr);
6759 return result;
6762 /* [expr.static.cast]
6764 The inverse of any standard conversion sequence (clause _conv_),
6765 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6766 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6767 (_conv.bool_) conversions, can be performed explicitly using
6768 static_cast subject to the restriction that the explicit
6769 conversion does not cast away constness (_expr.const.cast_), and
6770 the following additional rules for specific cases: */
6771 /* For reference, the conversions not excluded are: integral
6772 promotions, floating point promotion, integral conversions,
6773 floating point conversions, floating-integral conversions,
6774 pointer conversions, and pointer to member conversions. */
6775 /* DR 128
6777 A value of integral _or enumeration_ type can be explicitly
6778 converted to an enumeration type. */
6779 /* The effect of all that is that any conversion between any two
6780 types which are integral, floating, or enumeration types can be
6781 performed. */
6782 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6783 || SCALAR_FLOAT_TYPE_P (type))
6784 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6785 || SCALAR_FLOAT_TYPE_P (intype)))
6786 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6788 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6789 && CLASS_TYPE_P (TREE_TYPE (type))
6790 && CLASS_TYPE_P (TREE_TYPE (intype))
6791 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6792 (TREE_TYPE (intype))),
6793 build_pointer_type (TYPE_MAIN_VARIANT
6794 (TREE_TYPE (type))),
6795 complain))
6797 tree base;
6799 if (!c_cast_p
6800 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6801 complain))
6802 return error_mark_node;
6803 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6804 c_cast_p ? ba_unique : ba_check,
6805 NULL, complain);
6806 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6807 complain);
6809 if (flag_sanitize & SANITIZE_VPTR)
6811 tree ubsan_check
6812 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6813 intype, expr);
6814 if (ubsan_check)
6815 expr = ubsan_check;
6818 return cp_fold_convert (type, expr);
6821 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6822 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6824 tree c1;
6825 tree c2;
6826 tree t1;
6827 tree t2;
6829 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6830 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6832 if (TYPE_PTRDATAMEM_P (type))
6834 t1 = (build_ptrmem_type
6835 (c1,
6836 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6837 t2 = (build_ptrmem_type
6838 (c2,
6839 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6841 else
6843 t1 = intype;
6844 t2 = type;
6846 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6848 if (!c_cast_p
6849 && check_for_casting_away_constness (intype, type,
6850 STATIC_CAST_EXPR,
6851 complain))
6852 return error_mark_node;
6853 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6854 c_cast_p, complain);
6858 /* [expr.static.cast]
6860 An rvalue of type "pointer to cv void" can be explicitly
6861 converted to a pointer to object type. A value of type pointer
6862 to object converted to "pointer to cv void" and back to the
6863 original pointer type will have its original value. */
6864 if (TYPE_PTR_P (intype)
6865 && VOID_TYPE_P (TREE_TYPE (intype))
6866 && TYPE_PTROB_P (type))
6868 if (!c_cast_p
6869 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6870 complain))
6871 return error_mark_node;
6872 return build_nop (type, expr);
6875 *valid_p = false;
6876 return error_mark_node;
6879 /* Return an expression representing static_cast<TYPE>(EXPR). */
6881 tree
6882 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6884 tree result;
6885 bool valid_p;
6887 if (type == error_mark_node || expr == error_mark_node)
6888 return error_mark_node;
6890 if (processing_template_decl)
6892 expr = build_min (STATIC_CAST_EXPR, type, expr);
6893 /* We don't know if it will or will not have side effects. */
6894 TREE_SIDE_EFFECTS (expr) = 1;
6895 return convert_from_reference (expr);
6898 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6899 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6900 if (TREE_CODE (type) != REFERENCE_TYPE
6901 && TREE_CODE (expr) == NOP_EXPR
6902 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6903 expr = TREE_OPERAND (expr, 0);
6905 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6906 complain);
6907 if (valid_p)
6909 if (result != error_mark_node)
6910 maybe_warn_about_useless_cast (type, expr, complain);
6911 return result;
6914 if (complain & tf_error)
6915 error ("invalid static_cast from type %qT to type %qT",
6916 TREE_TYPE (expr), type);
6917 return error_mark_node;
6920 /* EXPR is an expression with member function or pointer-to-member
6921 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6922 not permitted by ISO C++, but we accept it in some modes. If we
6923 are not in one of those modes, issue a diagnostic. Return the
6924 converted expression. */
6926 tree
6927 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6929 tree intype;
6930 tree decl;
6932 intype = TREE_TYPE (expr);
6933 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6934 || TREE_CODE (intype) == METHOD_TYPE);
6936 if (!(complain & tf_warning_or_error))
6937 return error_mark_node;
6939 if (pedantic || warn_pmf2ptr)
6940 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6941 "converting from %qT to %qT", intype, type);
6943 if (TREE_CODE (intype) == METHOD_TYPE)
6944 expr = build_addr_func (expr, complain);
6945 else if (TREE_CODE (expr) == PTRMEM_CST)
6946 expr = build_address (PTRMEM_CST_MEMBER (expr));
6947 else
6949 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6950 decl = build_address (decl);
6951 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6954 if (expr == error_mark_node)
6955 return error_mark_node;
6957 return build_nop (type, expr);
6960 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6961 If C_CAST_P is true, this reinterpret cast is being done as part of
6962 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6963 indicate whether or not reinterpret_cast was valid. */
6965 static tree
6966 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6967 bool *valid_p, tsubst_flags_t complain)
6969 tree intype;
6971 /* Assume the cast is invalid. */
6972 if (valid_p)
6973 *valid_p = true;
6975 if (type == error_mark_node || error_operand_p (expr))
6976 return error_mark_node;
6978 intype = TREE_TYPE (expr);
6980 /* Save casted types in the function's used types hash table. */
6981 used_types_insert (type);
6983 /* [expr.reinterpret.cast]
6984 An lvalue expression of type T1 can be cast to the type
6985 "reference to T2" if an expression of type "pointer to T1" can be
6986 explicitly converted to the type "pointer to T2" using a
6987 reinterpret_cast. */
6988 if (TREE_CODE (type) == REFERENCE_TYPE)
6990 if (! lvalue_p (expr))
6992 if (complain & tf_error)
6993 error ("invalid cast of an rvalue expression of type "
6994 "%qT to type %qT",
6995 intype, type);
6996 return error_mark_node;
6999 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7000 "B" are related class types; the reinterpret_cast does not
7001 adjust the pointer. */
7002 if (TYPE_PTR_P (intype)
7003 && (complain & tf_warning)
7004 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7005 COMPARE_BASE | COMPARE_DERIVED)))
7006 warning (0, "casting %qT to %qT does not dereference pointer",
7007 intype, type);
7009 expr = cp_build_addr_expr (expr, complain);
7011 if (warn_strict_aliasing > 2)
7012 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
7014 if (expr != error_mark_node)
7015 expr = build_reinterpret_cast_1
7016 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7017 valid_p, complain);
7018 if (expr != error_mark_node)
7019 /* cp_build_indirect_ref isn't right for rvalue refs. */
7020 expr = convert_from_reference (fold_convert (type, expr));
7021 return expr;
7024 /* As a G++ extension, we consider conversions from member
7025 functions, and pointers to member functions to
7026 pointer-to-function and pointer-to-void types. If
7027 -Wno-pmf-conversions has not been specified,
7028 convert_member_func_to_ptr will issue an error message. */
7029 if ((TYPE_PTRMEMFUNC_P (intype)
7030 || TREE_CODE (intype) == METHOD_TYPE)
7031 && TYPE_PTR_P (type)
7032 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7033 || VOID_TYPE_P (TREE_TYPE (type))))
7034 return convert_member_func_to_ptr (type, expr, complain);
7036 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7037 array-to-pointer, and function-to-pointer conversions are
7038 performed. */
7039 expr = decay_conversion (expr, complain);
7041 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7042 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7043 if (TREE_CODE (expr) == NOP_EXPR
7044 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7045 expr = TREE_OPERAND (expr, 0);
7047 if (error_operand_p (expr))
7048 return error_mark_node;
7050 intype = TREE_TYPE (expr);
7052 /* [expr.reinterpret.cast]
7053 A pointer can be converted to any integral type large enough to
7054 hold it. ... A value of type std::nullptr_t can be converted to
7055 an integral type; the conversion has the same meaning and
7056 validity as a conversion of (void*)0 to the integral type. */
7057 if (CP_INTEGRAL_TYPE_P (type)
7058 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7060 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7062 if (complain & tf_error)
7063 permerror (input_location, "cast from %qT to %qT loses precision",
7064 intype, type);
7065 else
7066 return error_mark_node;
7068 if (NULLPTR_TYPE_P (intype))
7069 return build_int_cst (type, 0);
7071 /* [expr.reinterpret.cast]
7072 A value of integral or enumeration type can be explicitly
7073 converted to a pointer. */
7074 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7075 /* OK */
7077 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7078 || TYPE_PTR_OR_PTRMEM_P (type))
7079 && same_type_p (type, intype))
7080 /* DR 799 */
7081 return rvalue (expr);
7082 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7083 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7084 return build_nop (type, expr);
7085 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7086 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7088 tree sexpr = expr;
7090 if (!c_cast_p
7091 && check_for_casting_away_constness (intype, type,
7092 REINTERPRET_CAST_EXPR,
7093 complain))
7094 return error_mark_node;
7095 /* Warn about possible alignment problems. */
7096 if (STRICT_ALIGNMENT && warn_cast_align
7097 && (complain & tf_warning)
7098 && !VOID_TYPE_P (type)
7099 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7100 && COMPLETE_TYPE_P (TREE_TYPE (type))
7101 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7102 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
7103 warning (OPT_Wcast_align, "cast from %qT to %qT "
7104 "increases required alignment of target type", intype, type);
7106 /* We need to strip nops here, because the front end likes to
7107 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7108 STRIP_NOPS (sexpr);
7109 if (warn_strict_aliasing <= 2)
7110 strict_aliasing_warning (intype, type, sexpr);
7112 return build_nop (type, expr);
7114 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7115 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7117 if (complain & tf_warning)
7118 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7119 object pointer type or vice versa is conditionally-supported." */
7120 warning (OPT_Wconditionally_supported,
7121 "casting between pointer-to-function and pointer-to-object "
7122 "is conditionally-supported");
7123 return build_nop (type, expr);
7125 else if (VECTOR_TYPE_P (type))
7126 return convert_to_vector (type, expr);
7127 else if (VECTOR_TYPE_P (intype)
7128 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7129 return convert_to_integer_nofold (type, expr);
7130 else
7132 if (valid_p)
7133 *valid_p = false;
7134 if (complain & tf_error)
7135 error ("invalid cast from type %qT to type %qT", intype, type);
7136 return error_mark_node;
7139 return cp_convert (type, expr, complain);
7142 tree
7143 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7145 tree r;
7147 if (type == error_mark_node || expr == error_mark_node)
7148 return error_mark_node;
7150 if (processing_template_decl)
7152 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7154 if (!TREE_SIDE_EFFECTS (t)
7155 && type_dependent_expression_p (expr))
7156 /* There might turn out to be side effects inside expr. */
7157 TREE_SIDE_EFFECTS (t) = 1;
7158 return convert_from_reference (t);
7161 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7162 /*valid_p=*/NULL, complain);
7163 if (r != error_mark_node)
7164 maybe_warn_about_useless_cast (type, expr, complain);
7165 return r;
7168 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7169 return an appropriate expression. Otherwise, return
7170 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7171 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7172 performing a C-style cast, its value upon return will indicate
7173 whether or not the conversion succeeded. */
7175 static tree
7176 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7177 bool *valid_p)
7179 tree src_type;
7180 tree reference_type;
7182 /* Callers are responsible for handling error_mark_node as a
7183 destination type. */
7184 gcc_assert (dst_type != error_mark_node);
7185 /* In a template, callers should be building syntactic
7186 representations of casts, not using this machinery. */
7187 gcc_assert (!processing_template_decl);
7189 /* Assume the conversion is invalid. */
7190 if (valid_p)
7191 *valid_p = false;
7193 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7195 if (complain & tf_error)
7196 error ("invalid use of const_cast with type %qT, "
7197 "which is not a pointer, "
7198 "reference, nor a pointer-to-data-member type", dst_type);
7199 return error_mark_node;
7202 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7204 if (complain & tf_error)
7205 error ("invalid use of const_cast with type %qT, which is a pointer "
7206 "or reference to a function type", dst_type);
7207 return error_mark_node;
7210 /* Save casted types in the function's used types hash table. */
7211 used_types_insert (dst_type);
7213 src_type = TREE_TYPE (expr);
7214 /* Expressions do not really have reference types. */
7215 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7216 src_type = TREE_TYPE (src_type);
7218 /* [expr.const.cast]
7220 For two object types T1 and T2, if a pointer to T1 can be explicitly
7221 converted to the type "pointer to T2" using a const_cast, then the
7222 following conversions can also be made:
7224 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7225 type T2 using the cast const_cast<T2&>;
7227 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7228 type T2 using the cast const_cast<T2&&>; and
7230 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7231 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7233 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7235 reference_type = dst_type;
7236 if (!TYPE_REF_IS_RVALUE (dst_type)
7237 ? lvalue_p (expr)
7238 : obvalue_p (expr))
7239 /* OK. */;
7240 else
7242 if (complain & tf_error)
7243 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7244 src_type, dst_type);
7245 return error_mark_node;
7247 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7248 src_type = build_pointer_type (src_type);
7250 else
7252 reference_type = NULL_TREE;
7253 /* If the destination type is not a reference type, the
7254 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7255 conversions are performed. */
7256 src_type = type_decays_to (src_type);
7257 if (src_type == error_mark_node)
7258 return error_mark_node;
7261 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7263 if (comp_ptr_ttypes_const (dst_type, src_type))
7265 if (valid_p)
7267 *valid_p = true;
7268 /* This cast is actually a C-style cast. Issue a warning if
7269 the user is making a potentially unsafe cast. */
7270 check_for_casting_away_constness (src_type, dst_type,
7271 CAST_EXPR, complain);
7273 if (reference_type)
7275 expr = cp_build_addr_expr (expr, complain);
7276 if (expr == error_mark_node)
7277 return error_mark_node;
7278 expr = build_nop (reference_type, expr);
7279 return convert_from_reference (expr);
7281 else
7283 expr = decay_conversion (expr, complain);
7284 if (expr == error_mark_node)
7285 return error_mark_node;
7287 /* build_c_cast puts on a NOP_EXPR to make the result not an
7288 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7289 non-lvalue context. */
7290 if (TREE_CODE (expr) == NOP_EXPR
7291 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7292 expr = TREE_OPERAND (expr, 0);
7293 return build_nop (dst_type, expr);
7296 else if (valid_p
7297 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7298 TREE_TYPE (src_type)))
7299 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7300 complain);
7303 if (complain & tf_error)
7304 error ("invalid const_cast from type %qT to type %qT",
7305 src_type, dst_type);
7306 return error_mark_node;
7309 tree
7310 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7312 tree r;
7314 if (type == error_mark_node || error_operand_p (expr))
7315 return error_mark_node;
7317 if (processing_template_decl)
7319 tree t = build_min (CONST_CAST_EXPR, type, expr);
7321 if (!TREE_SIDE_EFFECTS (t)
7322 && type_dependent_expression_p (expr))
7323 /* There might turn out to be side effects inside expr. */
7324 TREE_SIDE_EFFECTS (t) = 1;
7325 return convert_from_reference (t);
7328 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7329 if (r != error_mark_node)
7330 maybe_warn_about_useless_cast (type, expr, complain);
7331 return r;
7334 /* Like cp_build_c_cast, but for the c-common bits. */
7336 tree
7337 build_c_cast (location_t /*loc*/, tree type, tree expr)
7339 return cp_build_c_cast (type, expr, tf_warning_or_error);
7342 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7343 preserve location information even for tree nodes that don't
7344 support it. */
7346 cp_expr
7347 build_c_cast (location_t loc, tree type, cp_expr expr)
7349 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7350 result.set_location (loc);
7351 return result;
7354 /* Build an expression representing an explicit C-style cast to type
7355 TYPE of expression EXPR. */
7357 tree
7358 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7360 tree value = expr;
7361 tree result;
7362 bool valid_p;
7364 if (type == error_mark_node || error_operand_p (expr))
7365 return error_mark_node;
7367 if (processing_template_decl)
7369 tree t = build_min (CAST_EXPR, type,
7370 tree_cons (NULL_TREE, value, NULL_TREE));
7371 /* We don't know if it will or will not have side effects. */
7372 TREE_SIDE_EFFECTS (t) = 1;
7373 return convert_from_reference (t);
7376 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7377 'Class') should always be retained, because this information aids
7378 in method lookup. */
7379 if (objc_is_object_ptr (type)
7380 && objc_is_object_ptr (TREE_TYPE (expr)))
7381 return build_nop (type, expr);
7383 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7384 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7385 if (TREE_CODE (type) != REFERENCE_TYPE
7386 && TREE_CODE (value) == NOP_EXPR
7387 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7388 value = TREE_OPERAND (value, 0);
7390 if (TREE_CODE (type) == ARRAY_TYPE)
7392 /* Allow casting from T1* to T2[] because Cfront allows it.
7393 NIHCL uses it. It is not valid ISO C++ however. */
7394 if (TYPE_PTR_P (TREE_TYPE (expr)))
7396 if (complain & tf_error)
7397 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7398 else
7399 return error_mark_node;
7400 type = build_pointer_type (TREE_TYPE (type));
7402 else
7404 if (complain & tf_error)
7405 error ("ISO C++ forbids casting to an array type %qT", type);
7406 return error_mark_node;
7410 if (TREE_CODE (type) == FUNCTION_TYPE
7411 || TREE_CODE (type) == METHOD_TYPE)
7413 if (complain & tf_error)
7414 error ("invalid cast to function type %qT", type);
7415 return error_mark_node;
7418 if (TYPE_PTR_P (type)
7419 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7420 /* Casting to an integer of smaller size is an error detected elsewhere. */
7421 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7422 /* Don't warn about converting any constant. */
7423 && !TREE_CONSTANT (value))
7424 warning_at (input_location, OPT_Wint_to_pointer_cast,
7425 "cast to pointer from integer of different size");
7427 /* A C-style cast can be a const_cast. */
7428 result = build_const_cast_1 (type, value, complain & tf_warning,
7429 &valid_p);
7430 if (valid_p)
7432 if (result != error_mark_node)
7433 maybe_warn_about_useless_cast (type, value, complain);
7434 return result;
7437 /* Or a static cast. */
7438 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7439 &valid_p, complain);
7440 /* Or a reinterpret_cast. */
7441 if (!valid_p)
7442 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7443 &valid_p, complain);
7444 /* The static_cast or reinterpret_cast may be followed by a
7445 const_cast. */
7446 if (valid_p
7447 /* A valid cast may result in errors if, for example, a
7448 conversion to an ambiguous base class is required. */
7449 && !error_operand_p (result))
7451 tree result_type;
7453 maybe_warn_about_useless_cast (type, value, complain);
7455 /* Non-class rvalues always have cv-unqualified type. */
7456 if (!CLASS_TYPE_P (type))
7457 type = TYPE_MAIN_VARIANT (type);
7458 result_type = TREE_TYPE (result);
7459 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7460 result_type = TYPE_MAIN_VARIANT (result_type);
7461 /* If the type of RESULT does not match TYPE, perform a
7462 const_cast to make it match. If the static_cast or
7463 reinterpret_cast succeeded, we will differ by at most
7464 cv-qualification, so the follow-on const_cast is guaranteed
7465 to succeed. */
7466 if (!same_type_p (non_reference (type), non_reference (result_type)))
7468 result = build_const_cast_1 (type, result, false, &valid_p);
7469 gcc_assert (valid_p);
7471 return result;
7474 return error_mark_node;
7477 /* For use from the C common bits. */
7478 tree
7479 build_modify_expr (location_t location,
7480 tree lhs, tree /*lhs_origtype*/,
7481 enum tree_code modifycode,
7482 location_t /*rhs_location*/, tree rhs,
7483 tree /*rhs_origtype*/)
7485 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7486 tf_warning_or_error);
7489 /* Build an assignment expression of lvalue LHS from value RHS.
7490 MODIFYCODE is the code for a binary operator that we use
7491 to combine the old value of LHS with RHS to get the new value.
7492 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7494 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7496 tree
7497 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7498 tree rhs, tsubst_flags_t complain)
7500 tree result;
7501 tree newrhs = rhs;
7502 tree lhstype = TREE_TYPE (lhs);
7503 tree olhstype = lhstype;
7504 bool plain_assign = (modifycode == NOP_EXPR);
7506 /* Avoid duplicate error messages from operands that had errors. */
7507 if (error_operand_p (lhs) || error_operand_p (rhs))
7508 return error_mark_node;
7510 /* Handle control structure constructs used as "lvalues". Note that we
7511 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7512 switch (TREE_CODE (lhs))
7514 /* Handle --foo = 5; as these are valid constructs in C++. */
7515 case PREDECREMENT_EXPR:
7516 case PREINCREMENT_EXPR:
7517 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7518 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7519 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7520 TREE_OPERAND (lhs, 1));
7521 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7522 break;
7524 case MODIFY_EXPR:
7525 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7526 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7527 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7528 TREE_OPERAND (lhs, 1));
7529 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7530 break;
7532 case MIN_EXPR:
7533 case MAX_EXPR:
7534 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7535 when neither operand has side-effects. */
7536 if (!lvalue_or_else (lhs, lv_assign, complain))
7537 return error_mark_node;
7539 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7540 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7542 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7543 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7544 boolean_type_node,
7545 TREE_OPERAND (lhs, 0),
7546 TREE_OPERAND (lhs, 1)),
7547 TREE_OPERAND (lhs, 0),
7548 TREE_OPERAND (lhs, 1));
7549 /* Fall through. */
7551 /* Handle (a ? b : c) used as an "lvalue". */
7552 case COND_EXPR:
7554 /* Produce (a ? (b = rhs) : (c = rhs))
7555 except that the RHS goes through a save-expr
7556 so the code to compute it is only emitted once. */
7557 tree cond;
7558 tree preeval = NULL_TREE;
7560 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7562 if (complain & tf_error)
7563 error ("void value not ignored as it ought to be");
7564 return error_mark_node;
7567 rhs = stabilize_expr (rhs, &preeval);
7569 /* Check this here to avoid odd errors when trying to convert
7570 a throw to the type of the COND_EXPR. */
7571 if (!lvalue_or_else (lhs, lv_assign, complain))
7572 return error_mark_node;
7574 cond = build_conditional_expr
7575 (input_location, TREE_OPERAND (lhs, 0),
7576 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7577 modifycode, rhs, complain),
7578 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7579 modifycode, rhs, complain),
7580 complain);
7582 if (cond == error_mark_node)
7583 return cond;
7584 /* Make sure the code to compute the rhs comes out
7585 before the split. */
7586 if (preeval)
7587 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7588 return cond;
7591 default:
7592 break;
7595 if (modifycode == INIT_EXPR)
7597 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7598 /* Do the default thing. */;
7599 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7601 /* Compound literal. */
7602 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7603 /* Call convert to generate an error; see PR 11063. */
7604 rhs = convert (lhstype, rhs);
7605 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7606 TREE_SIDE_EFFECTS (result) = 1;
7607 return result;
7609 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7610 /* Do the default thing. */;
7611 else
7613 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7614 result = build_special_member_call (lhs, complete_ctor_identifier,
7615 &rhs_vec, lhstype, LOOKUP_NORMAL,
7616 complain);
7617 release_tree_vector (rhs_vec);
7618 if (result == NULL_TREE)
7619 return error_mark_node;
7620 return result;
7623 else
7625 lhs = require_complete_type_sfinae (lhs, complain);
7626 if (lhs == error_mark_node)
7627 return error_mark_node;
7629 if (modifycode == NOP_EXPR)
7631 if (c_dialect_objc ())
7633 result = objc_maybe_build_modify_expr (lhs, rhs);
7634 if (result)
7635 return result;
7638 /* `operator=' is not an inheritable operator. */
7639 if (! MAYBE_CLASS_TYPE_P (lhstype))
7640 /* Do the default thing. */;
7641 else
7643 result = build_new_op (input_location, MODIFY_EXPR,
7644 LOOKUP_NORMAL, lhs, rhs,
7645 make_node (NOP_EXPR), /*overload=*/NULL,
7646 complain);
7647 if (result == NULL_TREE)
7648 return error_mark_node;
7649 return result;
7651 lhstype = olhstype;
7653 else
7655 tree init = NULL_TREE;
7657 /* A binary op has been requested. Combine the old LHS
7658 value with the RHS producing the value we should actually
7659 store into the LHS. */
7660 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7661 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7662 || MAYBE_CLASS_TYPE_P (lhstype)));
7664 /* Preevaluate the RHS to make sure its evaluation is complete
7665 before the lvalue-to-rvalue conversion of the LHS:
7667 [expr.ass] With respect to an indeterminately-sequenced
7668 function call, the operation of a compound assignment is a
7669 single evaluation. [ Note: Therefore, a function call shall
7670 not intervene between the lvalue-to-rvalue conversion and the
7671 side effect associated with any single compound assignment
7672 operator. -- end note ] */
7673 lhs = cp_stabilize_reference (lhs);
7674 rhs = rvalue (rhs);
7675 rhs = stabilize_expr (rhs, &init);
7676 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
7677 if (newrhs == error_mark_node)
7679 if (complain & tf_error)
7680 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7681 TREE_TYPE (lhs), TREE_TYPE (rhs));
7682 return error_mark_node;
7685 if (init)
7686 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7688 /* Now it looks like a plain assignment. */
7689 modifycode = NOP_EXPR;
7690 if (c_dialect_objc ())
7692 result = objc_maybe_build_modify_expr (lhs, newrhs);
7693 if (result)
7694 return result;
7697 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7698 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7701 /* The left-hand side must be an lvalue. */
7702 if (!lvalue_or_else (lhs, lv_assign, complain))
7703 return error_mark_node;
7705 /* Warn about modifying something that is `const'. Don't warn if
7706 this is initialization. */
7707 if (modifycode != INIT_EXPR
7708 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7709 /* Functions are not modifiable, even though they are
7710 lvalues. */
7711 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7712 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7713 /* If it's an aggregate and any field is const, then it is
7714 effectively const. */
7715 || (CLASS_TYPE_P (lhstype)
7716 && C_TYPE_FIELDS_READONLY (lhstype))))
7718 if (complain & tf_error)
7719 cxx_readonly_error (lhs, lv_assign);
7720 else
7721 return error_mark_node;
7724 /* If storing into a structure or union member, it may have been given a
7725 lowered bitfield type. We need to convert to the declared type first,
7726 so retrieve it now. */
7728 olhstype = unlowered_expr_type (lhs);
7730 /* Convert new value to destination type. */
7732 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7734 int from_array;
7736 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7738 if (modifycode != INIT_EXPR)
7740 if (complain & tf_error)
7741 error ("assigning to an array from an initializer list");
7742 return error_mark_node;
7744 if (check_array_initializer (lhs, lhstype, newrhs))
7745 return error_mark_node;
7746 newrhs = digest_init (lhstype, newrhs, complain);
7747 if (newrhs == error_mark_node)
7748 return error_mark_node;
7751 /* C++11 8.5/17: "If the destination type is an array of characters,
7752 an array of char16_t, an array of char32_t, or an array of wchar_t,
7753 and the initializer is a string literal...". */
7754 else if (TREE_CODE (newrhs) == STRING_CST
7755 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7756 && modifycode == INIT_EXPR)
7758 newrhs = digest_init (lhstype, newrhs, complain);
7759 if (newrhs == error_mark_node)
7760 return error_mark_node;
7763 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7764 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7766 if (complain & tf_error)
7767 error ("incompatible types in assignment of %qT to %qT",
7768 TREE_TYPE (rhs), lhstype);
7769 return error_mark_node;
7772 /* Allow array assignment in compiler-generated code. */
7773 else if (!current_function_decl
7774 || !DECL_DEFAULTED_FN (current_function_decl))
7776 /* This routine is used for both initialization and assignment.
7777 Make sure the diagnostic message differentiates the context. */
7778 if (complain & tf_error)
7780 if (modifycode == INIT_EXPR)
7781 error ("array used as initializer");
7782 else
7783 error ("invalid array assignment");
7785 return error_mark_node;
7788 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7789 ? 1 + (modifycode != INIT_EXPR): 0;
7790 return build_vec_init (lhs, NULL_TREE, newrhs,
7791 /*explicit_value_init_p=*/false,
7792 from_array, complain);
7795 if (modifycode == INIT_EXPR)
7796 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7797 LOOKUP_ONLYCONVERTING. */
7798 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7799 ICR_INIT, NULL_TREE, 0,
7800 complain);
7801 else
7802 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7803 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7805 if (!same_type_p (lhstype, olhstype))
7806 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7808 if (modifycode != INIT_EXPR)
7810 if (TREE_CODE (newrhs) == CALL_EXPR
7811 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7812 newrhs = build_cplus_new (lhstype, newrhs, complain);
7814 /* Can't initialize directly from a TARGET_EXPR, since that would
7815 cause the lhs to be constructed twice, and possibly result in
7816 accidental self-initialization. So we force the TARGET_EXPR to be
7817 expanded without a target. */
7818 if (TREE_CODE (newrhs) == TARGET_EXPR)
7819 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7820 TREE_OPERAND (newrhs, 0));
7823 if (newrhs == error_mark_node)
7824 return error_mark_node;
7826 if (c_dialect_objc () && flag_objc_gc)
7828 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7830 if (result)
7831 return result;
7834 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7835 lhstype, lhs, newrhs);
7837 TREE_SIDE_EFFECTS (result) = 1;
7838 if (!plain_assign)
7839 TREE_NO_WARNING (result) = 1;
7841 return result;
7844 cp_expr
7845 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7846 tree rhs, tsubst_flags_t complain)
7848 tree orig_lhs = lhs;
7849 tree orig_rhs = rhs;
7850 tree overload = NULL_TREE;
7851 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
7853 if (processing_template_decl)
7855 if (modifycode == NOP_EXPR
7856 || type_dependent_expression_p (lhs)
7857 || type_dependent_expression_p (rhs))
7858 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7859 build_min_nt_loc (loc, modifycode, NULL_TREE,
7860 NULL_TREE), rhs);
7862 lhs = build_non_dependent_expr (lhs);
7863 rhs = build_non_dependent_expr (rhs);
7866 if (modifycode != NOP_EXPR)
7868 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
7869 lhs, rhs, op, &overload, complain);
7870 if (rval)
7872 if (rval == error_mark_node)
7873 return rval;
7874 TREE_NO_WARNING (rval) = 1;
7875 if (processing_template_decl)
7877 if (overload != NULL_TREE)
7878 return (build_min_non_dep_op_overload
7879 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
7881 return (build_min_non_dep
7882 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
7884 return rval;
7887 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
7890 /* Helper function for get_delta_difference which assumes FROM is a base
7891 class of TO. Returns a delta for the conversion of pointer-to-member
7892 of FROM to pointer-to-member of TO. If the conversion is invalid and
7893 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7894 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7895 If C_CAST_P is true, this conversion is taking place as part of a
7896 C-style cast. */
7898 static tree
7899 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7900 tsubst_flags_t complain)
7902 tree binfo;
7903 base_kind kind;
7905 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7906 &kind, complain);
7908 if (binfo == error_mark_node)
7910 if (!(complain & tf_error))
7911 return error_mark_node;
7913 error (" in pointer to member function conversion");
7914 return size_zero_node;
7916 else if (binfo)
7918 if (kind != bk_via_virtual)
7919 return BINFO_OFFSET (binfo);
7920 else
7921 /* FROM is a virtual base class of TO. Issue an error or warning
7922 depending on whether or not this is a reinterpret cast. */
7924 if (!(complain & tf_error))
7925 return error_mark_node;
7927 error ("pointer to member conversion via virtual base %qT",
7928 BINFO_TYPE (binfo_from_vbase (binfo)));
7930 return size_zero_node;
7933 else
7934 return NULL_TREE;
7937 /* Get difference in deltas for different pointer to member function
7938 types. If the conversion is invalid and tf_error is not set in
7939 COMPLAIN, returns error_mark_node, otherwise returns an integer
7940 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7941 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7942 conversions as well. If C_CAST_P is true this conversion is taking
7943 place as part of a C-style cast.
7945 Note that the naming of FROM and TO is kind of backwards; the return
7946 value is what we add to a TO in order to get a FROM. They are named
7947 this way because we call this function to find out how to convert from
7948 a pointer to member of FROM to a pointer to member of TO. */
7950 static tree
7951 get_delta_difference (tree from, tree to,
7952 bool allow_inverse_p,
7953 bool c_cast_p, tsubst_flags_t complain)
7955 tree result;
7957 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7958 /* Pointer to member of incomplete class is permitted*/
7959 result = size_zero_node;
7960 else
7961 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7963 if (result == error_mark_node)
7964 return error_mark_node;
7966 if (!result)
7968 if (!allow_inverse_p)
7970 if (!(complain & tf_error))
7971 return error_mark_node;
7973 error_not_base_type (from, to);
7974 error (" in pointer to member conversion");
7975 result = size_zero_node;
7977 else
7979 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7981 if (result == error_mark_node)
7982 return error_mark_node;
7984 if (result)
7985 result = size_diffop_loc (input_location,
7986 size_zero_node, result);
7987 else
7989 if (!(complain & tf_error))
7990 return error_mark_node;
7992 error_not_base_type (from, to);
7993 error (" in pointer to member conversion");
7994 result = size_zero_node;
7999 return convert_to_integer (ptrdiff_type_node, result);
8002 /* Return a constructor for the pointer-to-member-function TYPE using
8003 the other components as specified. */
8005 tree
8006 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8008 tree u = NULL_TREE;
8009 tree delta_field;
8010 tree pfn_field;
8011 vec<constructor_elt, va_gc> *v;
8013 /* Pull the FIELD_DECLs out of the type. */
8014 pfn_field = TYPE_FIELDS (type);
8015 delta_field = DECL_CHAIN (pfn_field);
8017 /* Make sure DELTA has the type we want. */
8018 delta = convert_and_check (input_location, delta_type_node, delta);
8020 /* Convert to the correct target type if necessary. */
8021 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8023 /* Finish creating the initializer. */
8024 vec_alloc (v, 2);
8025 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8026 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8027 u = build_constructor (type, v);
8028 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8029 TREE_STATIC (u) = (TREE_CONSTANT (u)
8030 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8031 != NULL_TREE)
8032 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8033 != NULL_TREE));
8034 return u;
8037 /* Build a constructor for a pointer to member function. It can be
8038 used to initialize global variables, local variable, or used
8039 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8040 want to be.
8042 If FORCE is nonzero, then force this conversion, even if
8043 we would rather not do it. Usually set when using an explicit
8044 cast. A C-style cast is being processed iff C_CAST_P is true.
8046 Return error_mark_node, if something goes wrong. */
8048 tree
8049 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8050 tsubst_flags_t complain)
8052 tree fn;
8053 tree pfn_type;
8054 tree to_type;
8056 if (error_operand_p (pfn))
8057 return error_mark_node;
8059 pfn_type = TREE_TYPE (pfn);
8060 to_type = build_ptrmemfunc_type (type);
8062 /* Handle multiple conversions of pointer to member functions. */
8063 if (TYPE_PTRMEMFUNC_P (pfn_type))
8065 tree delta = NULL_TREE;
8066 tree npfn = NULL_TREE;
8067 tree n;
8069 if (!force
8070 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8071 LOOKUP_NORMAL, complain))
8073 if (complain & tf_error)
8074 error ("invalid conversion to type %qT from type %qT",
8075 to_type, pfn_type);
8076 else
8077 return error_mark_node;
8080 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8081 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8082 force,
8083 c_cast_p, complain);
8084 if (n == error_mark_node)
8085 return error_mark_node;
8087 /* We don't have to do any conversion to convert a
8088 pointer-to-member to its own type. But, we don't want to
8089 just return a PTRMEM_CST if there's an explicit cast; that
8090 cast should make the expression an invalid template argument. */
8091 if (TREE_CODE (pfn) != PTRMEM_CST)
8093 if (same_type_p (to_type, pfn_type))
8094 return pfn;
8095 else if (integer_zerop (n))
8096 return build_reinterpret_cast (to_type, pfn,
8097 complain);
8100 if (TREE_SIDE_EFFECTS (pfn))
8101 pfn = save_expr (pfn);
8103 /* Obtain the function pointer and the current DELTA. */
8104 if (TREE_CODE (pfn) == PTRMEM_CST)
8105 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8106 else
8108 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8109 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8112 /* Just adjust the DELTA field. */
8113 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8114 (TREE_TYPE (delta), ptrdiff_type_node));
8115 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8116 n = cp_build_binary_op (input_location,
8117 LSHIFT_EXPR, n, integer_one_node,
8118 complain);
8119 delta = cp_build_binary_op (input_location,
8120 PLUS_EXPR, delta, n, complain);
8121 return build_ptrmemfunc1 (to_type, delta, npfn);
8124 /* Handle null pointer to member function conversions. */
8125 if (null_ptr_cst_p (pfn))
8127 pfn = cp_build_c_cast (type, pfn, complain);
8128 return build_ptrmemfunc1 (to_type,
8129 integer_zero_node,
8130 pfn);
8133 if (type_unknown_p (pfn))
8134 return instantiate_type (type, pfn, complain);
8136 fn = TREE_OPERAND (pfn, 0);
8137 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8138 /* In a template, we will have preserved the
8139 OFFSET_REF. */
8140 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8141 return make_ptrmem_cst (to_type, fn);
8144 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8145 given by CST.
8147 ??? There is no consistency as to the types returned for the above
8148 values. Some code acts as if it were a sizetype and some as if it were
8149 integer_type_node. */
8151 void
8152 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8154 tree type = TREE_TYPE (cst);
8155 tree fn = PTRMEM_CST_MEMBER (cst);
8156 tree ptr_class, fn_class;
8158 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8160 /* The class that the function belongs to. */
8161 fn_class = DECL_CONTEXT (fn);
8163 /* The class that we're creating a pointer to member of. */
8164 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8166 /* First, calculate the adjustment to the function's class. */
8167 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8168 /*c_cast_p=*/0, tf_warning_or_error);
8170 if (!DECL_VIRTUAL_P (fn))
8171 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8172 build_addr_func (fn, tf_warning_or_error));
8173 else
8175 /* If we're dealing with a virtual function, we have to adjust 'this'
8176 again, to point to the base which provides the vtable entry for
8177 fn; the call will do the opposite adjustment. */
8178 tree orig_class = DECL_CONTEXT (fn);
8179 tree binfo = binfo_or_else (orig_class, fn_class);
8180 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8181 *delta, BINFO_OFFSET (binfo));
8183 /* We set PFN to the vtable offset at which the function can be
8184 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8185 case delta is shifted left, and then incremented). */
8186 *pfn = DECL_VINDEX (fn);
8187 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8188 TYPE_SIZE_UNIT (vtable_entry_type));
8190 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8192 case ptrmemfunc_vbit_in_pfn:
8193 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8194 integer_one_node);
8195 break;
8197 case ptrmemfunc_vbit_in_delta:
8198 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8199 *delta, integer_one_node);
8200 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8201 *delta, integer_one_node);
8202 break;
8204 default:
8205 gcc_unreachable ();
8208 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8212 /* Return an expression for PFN from the pointer-to-member function
8213 given by T. */
8215 static tree
8216 pfn_from_ptrmemfunc (tree t)
8218 if (TREE_CODE (t) == PTRMEM_CST)
8220 tree delta;
8221 tree pfn;
8223 expand_ptrmemfunc_cst (t, &delta, &pfn);
8224 if (pfn)
8225 return pfn;
8228 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8231 /* Return an expression for DELTA from the pointer-to-member function
8232 given by T. */
8234 static tree
8235 delta_from_ptrmemfunc (tree t)
8237 if (TREE_CODE (t) == PTRMEM_CST)
8239 tree delta;
8240 tree pfn;
8242 expand_ptrmemfunc_cst (t, &delta, &pfn);
8243 if (delta)
8244 return delta;
8247 return build_ptrmemfunc_access_expr (t, delta_identifier);
8250 /* Convert value RHS to type TYPE as preparation for an assignment to
8251 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8252 implicit conversion is. If FNDECL is non-NULL, we are doing the
8253 conversion in order to pass the PARMNUMth argument of FNDECL.
8254 If FNDECL is NULL, we are doing the conversion in function pointer
8255 argument passing, conversion in initialization, etc. */
8257 static tree
8258 convert_for_assignment (tree type, tree rhs,
8259 impl_conv_rhs errtype, tree fndecl, int parmnum,
8260 tsubst_flags_t complain, int flags)
8262 tree rhstype;
8263 enum tree_code coder;
8265 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8266 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8267 rhs = TREE_OPERAND (rhs, 0);
8269 /* Handle [dcl.init.list] direct-list-initialization from
8270 single element of enumeration with a fixed underlying type. */
8271 if (is_direct_enum_init (type, rhs))
8273 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8274 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8275 rhs = cp_build_c_cast (type, elt, complain);
8276 else
8277 rhs = error_mark_node;
8280 rhstype = TREE_TYPE (rhs);
8281 coder = TREE_CODE (rhstype);
8283 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8284 && vector_types_convertible_p (type, rhstype, true))
8286 rhs = mark_rvalue_use (rhs);
8287 return convert (type, rhs);
8290 if (rhs == error_mark_node || rhstype == error_mark_node)
8291 return error_mark_node;
8292 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8293 return error_mark_node;
8295 /* The RHS of an assignment cannot have void type. */
8296 if (coder == VOID_TYPE)
8298 if (complain & tf_error)
8299 error ("void value not ignored as it ought to be");
8300 return error_mark_node;
8303 if (c_dialect_objc ())
8305 int parmno;
8306 tree selector;
8307 tree rname = fndecl;
8309 switch (errtype)
8311 case ICR_ASSIGN:
8312 parmno = -1;
8313 break;
8314 case ICR_INIT:
8315 parmno = -2;
8316 break;
8317 default:
8318 selector = objc_message_selector ();
8319 parmno = parmnum;
8320 if (selector && parmno > 1)
8322 rname = selector;
8323 parmno -= 1;
8327 if (objc_compare_types (type, rhstype, parmno, rname))
8329 rhs = mark_rvalue_use (rhs);
8330 return convert (type, rhs);
8334 /* [expr.ass]
8336 The expression is implicitly converted (clause _conv_) to the
8337 cv-unqualified type of the left operand.
8339 We allow bad conversions here because by the time we get to this point
8340 we are committed to doing the conversion. If we end up doing a bad
8341 conversion, convert_like will complain. */
8342 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8344 /* When -Wno-pmf-conversions is use, we just silently allow
8345 conversions from pointers-to-members to plain pointers. If
8346 the conversion doesn't work, cp_convert will complain. */
8347 if (!warn_pmf2ptr
8348 && TYPE_PTR_P (type)
8349 && TYPE_PTRMEMFUNC_P (rhstype))
8350 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8351 else
8353 if (complain & tf_error)
8355 /* If the right-hand side has unknown type, then it is an
8356 overloaded function. Call instantiate_type to get error
8357 messages. */
8358 if (rhstype == unknown_type_node)
8359 instantiate_type (type, rhs, tf_warning_or_error);
8360 else if (fndecl)
8361 error ("cannot convert %qT to %qT for argument %qP to %qD",
8362 rhstype, type, parmnum, fndecl);
8363 else
8364 switch (errtype)
8366 case ICR_DEFAULT_ARGUMENT:
8367 error ("cannot convert %qT to %qT in default argument",
8368 rhstype, type);
8369 break;
8370 case ICR_ARGPASS:
8371 error ("cannot convert %qT to %qT in argument passing",
8372 rhstype, type);
8373 break;
8374 case ICR_CONVERTING:
8375 error ("cannot convert %qT to %qT",
8376 rhstype, type);
8377 break;
8378 case ICR_INIT:
8379 error ("cannot convert %qT to %qT in initialization",
8380 rhstype, type);
8381 break;
8382 case ICR_RETURN:
8383 error ("cannot convert %qT to %qT in return",
8384 rhstype, type);
8385 break;
8386 case ICR_ASSIGN:
8387 error ("cannot convert %qT to %qT in assignment",
8388 rhstype, type);
8389 break;
8390 default:
8391 gcc_unreachable();
8393 if (TYPE_PTR_P (rhstype)
8394 && TYPE_PTR_P (type)
8395 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8396 && CLASS_TYPE_P (TREE_TYPE (type))
8397 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8398 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8399 (TREE_TYPE (rhstype))),
8400 "class type %qT is incomplete", TREE_TYPE (rhstype));
8402 return error_mark_node;
8405 if (warn_suggest_attribute_format)
8407 const enum tree_code codel = TREE_CODE (type);
8408 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8409 && coder == codel
8410 && check_missing_format_attribute (type, rhstype)
8411 && (complain & tf_warning))
8412 switch (errtype)
8414 case ICR_ARGPASS:
8415 case ICR_DEFAULT_ARGUMENT:
8416 if (fndecl)
8417 warning (OPT_Wsuggest_attribute_format,
8418 "parameter %qP of %qD might be a candidate "
8419 "for a format attribute", parmnum, fndecl);
8420 else
8421 warning (OPT_Wsuggest_attribute_format,
8422 "parameter might be a candidate "
8423 "for a format attribute");
8424 break;
8425 case ICR_CONVERTING:
8426 warning (OPT_Wsuggest_attribute_format,
8427 "target of conversion might be a candidate "
8428 "for a format attribute");
8429 break;
8430 case ICR_INIT:
8431 warning (OPT_Wsuggest_attribute_format,
8432 "target of initialization might be a candidate "
8433 "for a format attribute");
8434 break;
8435 case ICR_RETURN:
8436 warning (OPT_Wsuggest_attribute_format,
8437 "return type might be a candidate "
8438 "for a format attribute");
8439 break;
8440 case ICR_ASSIGN:
8441 warning (OPT_Wsuggest_attribute_format,
8442 "left-hand side of assignment might be a candidate "
8443 "for a format attribute");
8444 break;
8445 default:
8446 gcc_unreachable();
8450 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8451 does not. */
8452 if (warn_parentheses
8453 && TREE_CODE (type) == BOOLEAN_TYPE
8454 && TREE_CODE (rhs) == MODIFY_EXPR
8455 && !TREE_NO_WARNING (rhs)
8456 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8457 && (complain & tf_warning))
8459 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8461 warning_at (loc, OPT_Wparentheses,
8462 "suggest parentheses around assignment used as truth value");
8463 TREE_NO_WARNING (rhs) = 1;
8466 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8467 complain, flags);
8470 /* Convert RHS to be of type TYPE.
8471 If EXP is nonzero, it is the target of the initialization.
8472 ERRTYPE indicates what kind of error the implicit conversion is.
8474 Two major differences between the behavior of
8475 `convert_for_assignment' and `convert_for_initialization'
8476 are that references are bashed in the former, while
8477 copied in the latter, and aggregates are assigned in
8478 the former (operator=) while initialized in the
8479 latter (X(X&)).
8481 If using constructor make sure no conversion operator exists, if one does
8482 exist, an ambiguity exists. */
8484 tree
8485 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8486 impl_conv_rhs errtype, tree fndecl, int parmnum,
8487 tsubst_flags_t complain)
8489 enum tree_code codel = TREE_CODE (type);
8490 tree rhstype;
8491 enum tree_code coder;
8493 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8494 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8495 if (TREE_CODE (rhs) == NOP_EXPR
8496 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8497 && codel != REFERENCE_TYPE)
8498 rhs = TREE_OPERAND (rhs, 0);
8500 if (type == error_mark_node
8501 || rhs == error_mark_node
8502 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8503 return error_mark_node;
8505 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8507 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8508 && TREE_CODE (type) != ARRAY_TYPE
8509 && (TREE_CODE (type) != REFERENCE_TYPE
8510 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8511 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8512 && !TYPE_REFFN_P (type))
8513 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8514 rhs = decay_conversion (rhs, complain);
8516 rhstype = TREE_TYPE (rhs);
8517 coder = TREE_CODE (rhstype);
8519 if (coder == ERROR_MARK)
8520 return error_mark_node;
8522 /* We accept references to incomplete types, so we can
8523 return here before checking if RHS is of complete type. */
8525 if (codel == REFERENCE_TYPE)
8527 /* This should eventually happen in convert_arguments. */
8528 int savew = 0, savee = 0;
8530 if (fndecl)
8531 savew = warningcount + werrorcount, savee = errorcount;
8532 rhs = initialize_reference (type, rhs, flags, complain);
8534 if (fndecl
8535 && (warningcount + werrorcount > savew || errorcount > savee))
8536 inform (DECL_SOURCE_LOCATION (fndecl),
8537 "in passing argument %P of %qD", parmnum, fndecl);
8539 return rhs;
8542 if (exp != 0)
8543 exp = require_complete_type_sfinae (exp, complain);
8544 if (exp == error_mark_node)
8545 return error_mark_node;
8547 rhstype = non_reference (rhstype);
8549 type = complete_type (type);
8551 if (DIRECT_INIT_EXPR_P (type, rhs))
8552 /* Don't try to do copy-initialization if we already have
8553 direct-initialization. */
8554 return rhs;
8556 if (MAYBE_CLASS_TYPE_P (type))
8557 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8559 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8560 complain, flags);
8563 /* If RETVAL is the address of, or a reference to, a local variable or
8564 temporary give an appropriate warning and return true. */
8566 static bool
8567 maybe_warn_about_returning_address_of_local (tree retval)
8569 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8570 tree whats_returned = retval;
8572 for (;;)
8574 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8575 whats_returned = TREE_OPERAND (whats_returned, 1);
8576 else if (CONVERT_EXPR_P (whats_returned)
8577 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8578 whats_returned = TREE_OPERAND (whats_returned, 0);
8579 else
8580 break;
8583 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8584 return false;
8585 whats_returned = TREE_OPERAND (whats_returned, 0);
8587 while (TREE_CODE (whats_returned) == COMPONENT_REF
8588 || TREE_CODE (whats_returned) == ARRAY_REF)
8589 whats_returned = TREE_OPERAND (whats_returned, 0);
8591 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8593 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8594 || TREE_CODE (whats_returned) == TARGET_EXPR)
8596 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8597 return true;
8599 if (VAR_P (whats_returned)
8600 && DECL_NAME (whats_returned)
8601 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8603 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8604 return true;
8608 if (DECL_P (whats_returned)
8609 && DECL_NAME (whats_returned)
8610 && DECL_FUNCTION_SCOPE_P (whats_returned)
8611 && !is_capture_proxy (whats_returned)
8612 && !(TREE_STATIC (whats_returned)
8613 || TREE_PUBLIC (whats_returned)))
8615 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8616 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8617 OPT_Wreturn_local_addr,
8618 "reference to local variable %qD returned",
8619 whats_returned);
8620 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8621 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8622 OPT_Wreturn_local_addr, "address of label %qD returned",
8623 whats_returned);
8624 else
8625 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8626 OPT_Wreturn_local_addr, "address of local variable %qD "
8627 "returned", whats_returned);
8628 return true;
8631 return false;
8634 /* Check that returning RETVAL from the current function is valid.
8635 Return an expression explicitly showing all conversions required to
8636 change RETVAL into the function return type, and to assign it to
8637 the DECL_RESULT for the function. Set *NO_WARNING to true if
8638 code reaches end of non-void function warning shouldn't be issued
8639 on this RETURN_EXPR. */
8641 tree
8642 check_return_expr (tree retval, bool *no_warning)
8644 tree result;
8645 /* The type actually returned by the function. */
8646 tree valtype;
8647 /* The type the function is declared to return, or void if
8648 the declared type is incomplete. */
8649 tree functype;
8650 int fn_returns_value_p;
8651 bool named_return_value_okay_p;
8653 *no_warning = false;
8655 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8657 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8658 "statement is not allowed");
8659 return NULL_TREE;
8662 /* A `volatile' function is one that isn't supposed to return, ever.
8663 (This is a G++ extension, used to get better code for functions
8664 that call the `volatile' function.) */
8665 if (TREE_THIS_VOLATILE (current_function_decl))
8666 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8668 /* Check for various simple errors. */
8669 if (DECL_DESTRUCTOR_P (current_function_decl))
8671 if (retval)
8672 error ("returning a value from a destructor");
8673 return NULL_TREE;
8675 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8677 if (in_function_try_handler)
8678 /* If a return statement appears in a handler of the
8679 function-try-block of a constructor, the program is ill-formed. */
8680 error ("cannot return from a handler of a function-try-block of a constructor");
8681 else if (retval)
8682 /* You can't return a value from a constructor. */
8683 error ("returning a value from a constructor");
8684 return NULL_TREE;
8687 const tree saved_retval = retval;
8689 if (processing_template_decl)
8691 current_function_returns_value = 1;
8693 if (check_for_bare_parameter_packs (retval))
8694 return error_mark_node;
8696 if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
8697 || (retval != NULL_TREE
8698 && type_dependent_expression_p (retval)))
8699 return retval;
8702 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8704 /* Deduce auto return type from a return statement. */
8705 if (current_function_auto_return_pattern)
8707 tree auto_node;
8708 tree type;
8710 if (!retval && !is_auto (current_function_auto_return_pattern))
8712 /* Give a helpful error message. */
8713 error ("return-statement with no value, in function returning %qT",
8714 current_function_auto_return_pattern);
8715 inform (input_location, "only plain %<auto%> return type can be "
8716 "deduced to %<void%>");
8717 type = error_mark_node;
8719 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8721 error ("returning initializer list");
8722 type = error_mark_node;
8724 else
8726 if (!retval)
8727 retval = void_node;
8728 auto_node = type_uses_auto (current_function_auto_return_pattern);
8729 type = do_auto_deduction (current_function_auto_return_pattern,
8730 retval, auto_node);
8733 if (type == error_mark_node)
8734 /* Leave it. */;
8735 else if (functype == current_function_auto_return_pattern)
8736 apply_deduced_return_type (current_function_decl, type);
8737 else if (!same_type_p (type, functype))
8739 if (LAMBDA_FUNCTION_P (current_function_decl))
8740 error ("inconsistent types %qT and %qT deduced for "
8741 "lambda return type", functype, type);
8742 else
8743 error ("inconsistent deduction for auto return type: "
8744 "%qT and then %qT", functype, type);
8746 functype = type;
8749 result = DECL_RESULT (current_function_decl);
8750 valtype = TREE_TYPE (result);
8751 gcc_assert (valtype != NULL_TREE);
8752 fn_returns_value_p = !VOID_TYPE_P (valtype);
8754 /* Check for a return statement with no return value in a function
8755 that's supposed to return a value. */
8756 if (!retval && fn_returns_value_p)
8758 if (functype != error_mark_node)
8759 permerror (input_location, "return-statement with no value, in "
8760 "function returning %qT", valtype);
8761 /* Remember that this function did return. */
8762 current_function_returns_value = 1;
8763 /* And signal caller that TREE_NO_WARNING should be set on the
8764 RETURN_EXPR to avoid control reaches end of non-void function
8765 warnings in tree-cfg.c. */
8766 *no_warning = true;
8768 /* Check for a return statement with a value in a function that
8769 isn't supposed to return a value. */
8770 else if (retval && !fn_returns_value_p)
8772 if (VOID_TYPE_P (TREE_TYPE (retval)))
8773 /* You can return a `void' value from a function of `void'
8774 type. In that case, we have to evaluate the expression for
8775 its side-effects. */
8776 finish_expr_stmt (retval);
8777 else
8778 permerror (input_location, "return-statement with a value, in function "
8779 "returning 'void'");
8780 current_function_returns_null = 1;
8782 /* There's really no value to return, after all. */
8783 return NULL_TREE;
8785 else if (!retval)
8786 /* Remember that this function can sometimes return without a
8787 value. */
8788 current_function_returns_null = 1;
8789 else
8790 /* Remember that this function did return a value. */
8791 current_function_returns_value = 1;
8793 /* Check for erroneous operands -- but after giving ourselves a
8794 chance to provide an error about returning a value from a void
8795 function. */
8796 if (error_operand_p (retval))
8798 current_function_return_value = error_mark_node;
8799 return error_mark_node;
8802 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8803 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8804 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8805 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8806 && ! flag_check_new
8807 && retval && null_ptr_cst_p (retval))
8808 warning (0, "%<operator new%> must not return NULL unless it is "
8809 "declared %<throw()%> (or -fcheck-new is in effect)");
8811 /* Effective C++ rule 15. See also start_function. */
8812 if (warn_ecpp
8813 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8815 bool warn = true;
8817 /* The function return type must be a reference to the current
8818 class. */
8819 if (TREE_CODE (valtype) == REFERENCE_TYPE
8820 && same_type_ignoring_top_level_qualifiers_p
8821 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8823 /* Returning '*this' is obviously OK. */
8824 if (retval == current_class_ref)
8825 warn = false;
8826 /* If we are calling a function whose return type is the same of
8827 the current class reference, it is ok. */
8828 else if (INDIRECT_REF_P (retval)
8829 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8830 warn = false;
8833 if (warn)
8834 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8837 if (processing_template_decl)
8839 /* We should not have changed the return value. */
8840 gcc_assert (retval == saved_retval);
8841 return retval;
8844 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8846 [...] For a function with a class return type, if the expression
8847 in the return statement is the name of a local object, and the cv-
8848 unqualified type of the local object is the same as the function
8849 return type, an implementation is permitted to omit creating the tem-
8850 porary object to hold the function return value [...]
8852 So, if this is a value-returning function that always returns the same
8853 local variable, remember it.
8855 It might be nice to be more flexible, and choose the first suitable
8856 variable even if the function sometimes returns something else, but
8857 then we run the risk of clobbering the variable we chose if the other
8858 returned expression uses the chosen variable somehow. And people expect
8859 this restriction, anyway. (jason 2000-11-19)
8861 See finish_function and finalize_nrv for the rest of this optimization. */
8863 named_return_value_okay_p =
8864 (retval != NULL_TREE
8865 /* Must be a local, automatic variable. */
8866 && VAR_P (retval)
8867 && DECL_CONTEXT (retval) == current_function_decl
8868 && ! TREE_STATIC (retval)
8869 /* And not a lambda or anonymous union proxy. */
8870 && !DECL_HAS_VALUE_EXPR_P (retval)
8871 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8872 /* The cv-unqualified type of the returned value must be the
8873 same as the cv-unqualified return type of the
8874 function. */
8875 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8876 (TYPE_MAIN_VARIANT (functype)))
8877 /* And the returned value must be non-volatile. */
8878 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8880 if (fn_returns_value_p && flag_elide_constructors)
8882 if (named_return_value_okay_p
8883 && (current_function_return_value == NULL_TREE
8884 || current_function_return_value == retval))
8885 current_function_return_value = retval;
8886 else
8887 current_function_return_value = error_mark_node;
8890 /* We don't need to do any conversions when there's nothing being
8891 returned. */
8892 if (!retval)
8893 return NULL_TREE;
8895 /* Do any required conversions. */
8896 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8897 /* No conversions are required. */
8899 else
8901 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8903 /* The functype's return type will have been set to void, if it
8904 was an incomplete type. Just treat this as 'return;' */
8905 if (VOID_TYPE_P (functype))
8906 return error_mark_node;
8908 /* If we had an id-expression obfuscated by force_paren_expr, we need
8909 to undo it so we can try to treat it as an rvalue below. */
8910 retval = maybe_undo_parenthesized_ref (retval);
8912 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8913 treated as an rvalue for the purposes of overload resolution to
8914 favor move constructors over copy constructors.
8916 Note that these conditions are similar to, but not as strict as,
8917 the conditions for the named return value optimization. */
8918 if ((cxx_dialect != cxx98)
8919 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8920 || TREE_CODE (retval) == PARM_DECL)
8921 && DECL_CONTEXT (retval) == current_function_decl
8922 && !TREE_STATIC (retval)
8923 /* This is only interesting for class type. */
8924 && CLASS_TYPE_P (functype))
8925 flags = flags | LOOKUP_PREFER_RVALUE;
8927 /* First convert the value to the function's return type, then
8928 to the type of return value's location to handle the
8929 case that functype is smaller than the valtype. */
8930 retval = convert_for_initialization
8931 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8932 tf_warning_or_error);
8933 retval = convert (valtype, retval);
8935 /* If the conversion failed, treat this just like `return;'. */
8936 if (retval == error_mark_node)
8937 return retval;
8938 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8939 else if (! cfun->returns_struct
8940 && TREE_CODE (retval) == TARGET_EXPR
8941 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8942 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8943 TREE_OPERAND (retval, 0));
8944 else if (maybe_warn_about_returning_address_of_local (retval))
8945 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8946 build_zero_cst (TREE_TYPE (retval)));
8949 /* Actually copy the value returned into the appropriate location. */
8950 if (retval && retval != result)
8951 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8953 return retval;
8957 /* Returns nonzero if the pointer-type FROM can be converted to the
8958 pointer-type TO via a qualification conversion. If CONSTP is -1,
8959 then we return nonzero if the pointers are similar, and the
8960 cv-qualification signature of FROM is a proper subset of that of TO.
8962 If CONSTP is positive, then all outer pointers have been
8963 const-qualified. */
8965 static int
8966 comp_ptr_ttypes_real (tree to, tree from, int constp)
8968 bool to_more_cv_qualified = false;
8969 bool is_opaque_pointer = false;
8971 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8973 if (TREE_CODE (to) != TREE_CODE (from))
8974 return 0;
8976 if (TREE_CODE (from) == OFFSET_TYPE
8977 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8978 TYPE_OFFSET_BASETYPE (to)))
8979 return 0;
8981 /* Const and volatile mean something different for function types,
8982 so the usual checks are not appropriate. */
8983 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8985 if (!at_least_as_qualified_p (to, from))
8986 return 0;
8988 if (!at_least_as_qualified_p (from, to))
8990 if (constp == 0)
8991 return 0;
8992 to_more_cv_qualified = true;
8995 if (constp > 0)
8996 constp &= TYPE_READONLY (to);
8999 if (VECTOR_TYPE_P (to))
9000 is_opaque_pointer = vector_targets_convertible_p (to, from);
9002 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9003 return ((constp >= 0 || to_more_cv_qualified)
9004 && (is_opaque_pointer
9005 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9009 /* When comparing, say, char ** to char const **, this function takes
9010 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9011 types to this function. */
9014 comp_ptr_ttypes (tree to, tree from)
9016 return comp_ptr_ttypes_real (to, from, 1);
9019 /* Returns true iff FNTYPE is a non-class type that involves
9020 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9021 if a parameter type is ill-formed. */
9023 bool
9024 error_type_p (const_tree type)
9026 tree t;
9028 switch (TREE_CODE (type))
9030 case ERROR_MARK:
9031 return true;
9033 case POINTER_TYPE:
9034 case REFERENCE_TYPE:
9035 case OFFSET_TYPE:
9036 return error_type_p (TREE_TYPE (type));
9038 case FUNCTION_TYPE:
9039 case METHOD_TYPE:
9040 if (error_type_p (TREE_TYPE (type)))
9041 return true;
9042 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9043 if (error_type_p (TREE_VALUE (t)))
9044 return true;
9045 return false;
9047 case RECORD_TYPE:
9048 if (TYPE_PTRMEMFUNC_P (type))
9049 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9050 return false;
9052 default:
9053 return false;
9057 /* Returns true if to and from are (possibly multi-level) pointers to the same
9058 type or inheritance-related types, regardless of cv-quals. */
9060 bool
9061 ptr_reasonably_similar (const_tree to, const_tree from)
9063 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9065 /* Any target type is similar enough to void. */
9066 if (VOID_TYPE_P (to))
9067 return !error_type_p (from);
9068 if (VOID_TYPE_P (from))
9069 return !error_type_p (to);
9071 if (TREE_CODE (to) != TREE_CODE (from))
9072 return false;
9074 if (TREE_CODE (from) == OFFSET_TYPE
9075 && comptypes (TYPE_OFFSET_BASETYPE (to),
9076 TYPE_OFFSET_BASETYPE (from),
9077 COMPARE_BASE | COMPARE_DERIVED))
9078 continue;
9080 if (VECTOR_TYPE_P (to)
9081 && vector_types_convertible_p (to, from, false))
9082 return true;
9084 if (TREE_CODE (to) == INTEGER_TYPE
9085 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9086 return true;
9088 if (TREE_CODE (to) == FUNCTION_TYPE)
9089 return !error_type_p (to) && !error_type_p (from);
9091 if (!TYPE_PTR_P (to))
9093 /* When either type is incomplete avoid DERIVED_FROM_P,
9094 which may call complete_type (c++/57942). */
9095 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9096 return comptypes
9097 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9098 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9103 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9104 pointer-to-member types) are the same, ignoring cv-qualification at
9105 all levels. */
9107 bool
9108 comp_ptr_ttypes_const (tree to, tree from)
9110 bool is_opaque_pointer = false;
9112 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9114 if (TREE_CODE (to) != TREE_CODE (from))
9115 return false;
9117 if (TREE_CODE (from) == OFFSET_TYPE
9118 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9119 TYPE_OFFSET_BASETYPE (to)))
9120 continue;
9122 if (VECTOR_TYPE_P (to))
9123 is_opaque_pointer = vector_targets_convertible_p (to, from);
9125 if (!TYPE_PTR_P (to))
9126 return (is_opaque_pointer
9127 || same_type_ignoring_top_level_qualifiers_p (to, from));
9131 /* Returns the type qualifiers for this type, including the qualifiers on the
9132 elements for an array type. */
9135 cp_type_quals (const_tree type)
9137 int quals;
9138 /* This CONST_CAST is okay because strip_array_types returns its
9139 argument unmodified and we assign it to a const_tree. */
9140 type = strip_array_types (CONST_CAST_TREE (type));
9141 if (type == error_mark_node
9142 /* Quals on a FUNCTION_TYPE are memfn quals. */
9143 || TREE_CODE (type) == FUNCTION_TYPE)
9144 return TYPE_UNQUALIFIED;
9145 quals = TYPE_QUALS (type);
9146 /* METHOD and REFERENCE_TYPEs should never have quals. */
9147 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9148 && TREE_CODE (type) != REFERENCE_TYPE)
9149 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9150 == TYPE_UNQUALIFIED));
9151 return quals;
9154 /* Returns the function-ref-qualifier for TYPE */
9156 cp_ref_qualifier
9157 type_memfn_rqual (const_tree type)
9159 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9160 || TREE_CODE (type) == METHOD_TYPE);
9162 if (!FUNCTION_REF_QUALIFIED (type))
9163 return REF_QUAL_NONE;
9164 else if (FUNCTION_RVALUE_QUALIFIED (type))
9165 return REF_QUAL_RVALUE;
9166 else
9167 return REF_QUAL_LVALUE;
9170 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9171 METHOD_TYPE. */
9174 type_memfn_quals (const_tree type)
9176 if (TREE_CODE (type) == FUNCTION_TYPE)
9177 return TYPE_QUALS (type);
9178 else if (TREE_CODE (type) == METHOD_TYPE)
9179 return cp_type_quals (class_of_this_parm (type));
9180 else
9181 gcc_unreachable ();
9184 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9185 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9187 tree
9188 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9190 /* Could handle METHOD_TYPE here if necessary. */
9191 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9192 if (TYPE_QUALS (type) == memfn_quals
9193 && type_memfn_rqual (type) == rqual)
9194 return type;
9196 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9197 complex. */
9198 tree result = build_qualified_type (type, memfn_quals);
9199 if (tree canon = TYPE_CANONICAL (result))
9200 if (canon != result)
9201 /* check_qualified_type doesn't check the ref-qualifier, so make sure
9202 TYPE_CANONICAL is correct. */
9203 TYPE_CANONICAL (result)
9204 = build_ref_qualified_type (canon, type_memfn_rqual (result));
9205 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
9206 return build_ref_qualified_type (result, rqual);
9209 /* Returns nonzero if TYPE is const or volatile. */
9211 bool
9212 cv_qualified_p (const_tree type)
9214 int quals = cp_type_quals (type);
9215 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9218 /* Returns nonzero if the TYPE contains a mutable member. */
9220 bool
9221 cp_has_mutable_p (const_tree type)
9223 /* This CONST_CAST is okay because strip_array_types returns its
9224 argument unmodified and we assign it to a const_tree. */
9225 type = strip_array_types (CONST_CAST_TREE(type));
9227 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9230 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9231 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9232 approximation. In particular, consider:
9234 int f();
9235 struct S { int i; };
9236 const S s = { f(); }
9238 Here, we will make "s" as TREE_READONLY (because it is declared
9239 "const") -- only to reverse ourselves upon seeing that the
9240 initializer is non-constant. */
9242 void
9243 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9245 tree type = TREE_TYPE (decl);
9247 if (type == error_mark_node)
9248 return;
9250 if (TREE_CODE (decl) == TYPE_DECL)
9251 return;
9253 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9254 && type_quals != TYPE_UNQUALIFIED));
9256 /* Avoid setting TREE_READONLY incorrectly. */
9257 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9258 constructor can produce constant init, so rely on cp_finish_decl to
9259 clear TREE_READONLY if the variable has non-constant init. */
9261 /* If the type has (or might have) a mutable component, that component
9262 might be modified. */
9263 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9264 type_quals &= ~TYPE_QUAL_CONST;
9266 c_apply_type_quals_to_decl (type_quals, decl);
9269 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9270 exemplar types such that casting T1 to T2 is casting away constness
9271 if and only if there is no implicit conversion from T1 to T2. */
9273 static void
9274 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9276 int quals1;
9277 int quals2;
9279 /* [expr.const.cast]
9281 For multi-level pointer to members and multi-level mixed pointers
9282 and pointers to members (conv.qual), the "member" aspect of a
9283 pointer to member level is ignored when determining if a const
9284 cv-qualifier has been cast away. */
9285 /* [expr.const.cast]
9287 For two pointer types:
9289 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9290 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9291 K is min(N,M)
9293 casting from X1 to X2 casts away constness if, for a non-pointer
9294 type T there does not exist an implicit conversion (clause
9295 _conv_) from:
9297 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9301 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9302 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9303 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9305 *t1 = cp_build_qualified_type (void_type_node,
9306 cp_type_quals (*t1));
9307 *t2 = cp_build_qualified_type (void_type_node,
9308 cp_type_quals (*t2));
9309 return;
9312 quals1 = cp_type_quals (*t1);
9313 quals2 = cp_type_quals (*t2);
9315 if (TYPE_PTRDATAMEM_P (*t1))
9316 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9317 else
9318 *t1 = TREE_TYPE (*t1);
9319 if (TYPE_PTRDATAMEM_P (*t2))
9320 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9321 else
9322 *t2 = TREE_TYPE (*t2);
9324 casts_away_constness_r (t1, t2, complain);
9325 *t1 = build_pointer_type (*t1);
9326 *t2 = build_pointer_type (*t2);
9327 *t1 = cp_build_qualified_type (*t1, quals1);
9328 *t2 = cp_build_qualified_type (*t2, quals2);
9331 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9332 constness.
9334 ??? This function returns non-zero if casting away qualifiers not
9335 just const. We would like to return to the caller exactly which
9336 qualifiers are casted away to give more accurate diagnostics.
9339 static bool
9340 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9342 if (TREE_CODE (t2) == REFERENCE_TYPE)
9344 /* [expr.const.cast]
9346 Casting from an lvalue of type T1 to an lvalue of type T2
9347 using a reference cast casts away constness if a cast from an
9348 rvalue of type "pointer to T1" to the type "pointer to T2"
9349 casts away constness. */
9350 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9351 return casts_away_constness (build_pointer_type (t1),
9352 build_pointer_type (TREE_TYPE (t2)),
9353 complain);
9356 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9357 /* [expr.const.cast]
9359 Casting from an rvalue of type "pointer to data member of X
9360 of type T1" to the type "pointer to data member of Y of type
9361 T2" casts away constness if a cast from an rvalue of type
9362 "pointer to T1" to the type "pointer to T2" casts away
9363 constness. */
9364 return casts_away_constness
9365 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9366 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9367 complain);
9369 /* Casting away constness is only something that makes sense for
9370 pointer or reference types. */
9371 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9372 return false;
9374 /* Top-level qualifiers don't matter. */
9375 t1 = TYPE_MAIN_VARIANT (t1);
9376 t2 = TYPE_MAIN_VARIANT (t2);
9377 casts_away_constness_r (&t1, &t2, complain);
9378 if (!can_convert (t2, t1, complain))
9379 return true;
9381 return false;
9384 /* If T is a REFERENCE_TYPE return the type to which T refers.
9385 Otherwise, return T itself. */
9387 tree
9388 non_reference (tree t)
9390 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9391 t = TREE_TYPE (t);
9392 return t;
9396 /* Return nonzero if REF is an lvalue valid for this language;
9397 otherwise, print an error message and return zero. USE says
9398 how the lvalue is being used and so selects the error message. */
9401 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9403 cp_lvalue_kind kind = lvalue_kind (ref);
9405 if (kind == clk_none)
9407 if (complain & tf_error)
9408 lvalue_error (input_location, use);
9409 return 0;
9411 else if (kind & (clk_rvalueref|clk_class))
9413 if (!(complain & tf_error))
9414 return 0;
9415 if (kind & clk_class)
9416 /* Make this a permerror because we used to accept it. */
9417 permerror (input_location, "using temporary as lvalue");
9418 else
9419 error ("using xvalue (rvalue reference) as lvalue");
9421 return 1;
9424 /* Return true if a user-defined literal operator is a raw operator. */
9426 bool
9427 check_raw_literal_operator (const_tree decl)
9429 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9430 tree argtype;
9431 int arity;
9432 bool maybe_raw_p = false;
9434 /* Count the number and type of arguments and check for ellipsis. */
9435 for (argtype = argtypes, arity = 0;
9436 argtype && argtype != void_list_node;
9437 ++arity, argtype = TREE_CHAIN (argtype))
9439 tree t = TREE_VALUE (argtype);
9441 if (same_type_p (t, const_string_type_node))
9442 maybe_raw_p = true;
9444 if (!argtype)
9445 return false; /* Found ellipsis. */
9447 if (!maybe_raw_p || arity != 1)
9448 return false;
9450 return true;
9454 /* Return true if a user-defined literal operator has one of the allowed
9455 argument types. */
9457 bool
9458 check_literal_operator_args (const_tree decl,
9459 bool *long_long_unsigned_p, bool *long_double_p)
9461 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9463 *long_long_unsigned_p = false;
9464 *long_double_p = false;
9465 if (processing_template_decl || processing_specialization)
9466 return argtypes == void_list_node;
9467 else
9469 tree argtype;
9470 int arity;
9471 int max_arity = 2;
9473 /* Count the number and type of arguments and check for ellipsis. */
9474 for (argtype = argtypes, arity = 0;
9475 argtype && argtype != void_list_node;
9476 argtype = TREE_CHAIN (argtype))
9478 tree t = TREE_VALUE (argtype);
9479 ++arity;
9481 if (TYPE_PTR_P (t))
9483 bool maybe_raw_p = false;
9484 t = TREE_TYPE (t);
9485 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9486 return false;
9487 t = TYPE_MAIN_VARIANT (t);
9488 if ((maybe_raw_p = same_type_p (t, char_type_node))
9489 || same_type_p (t, wchar_type_node)
9490 || same_type_p (t, char16_type_node)
9491 || same_type_p (t, char32_type_node))
9493 argtype = TREE_CHAIN (argtype);
9494 if (!argtype)
9495 return false;
9496 t = TREE_VALUE (argtype);
9497 if (maybe_raw_p && argtype == void_list_node)
9498 return true;
9499 else if (same_type_p (t, size_type_node))
9501 ++arity;
9502 continue;
9504 else
9505 return false;
9508 else if (same_type_p (t, long_long_unsigned_type_node))
9510 max_arity = 1;
9511 *long_long_unsigned_p = true;
9513 else if (same_type_p (t, long_double_type_node))
9515 max_arity = 1;
9516 *long_double_p = true;
9518 else if (same_type_p (t, char_type_node))
9519 max_arity = 1;
9520 else if (same_type_p (t, wchar_type_node))
9521 max_arity = 1;
9522 else if (same_type_p (t, char16_type_node))
9523 max_arity = 1;
9524 else if (same_type_p (t, char32_type_node))
9525 max_arity = 1;
9526 else
9527 return false;
9529 if (!argtype)
9530 return false; /* Found ellipsis. */
9532 if (arity != max_arity)
9533 return false;
9535 return true;
9539 /* Always returns false since unlike C90, C++ has no concept of implicit
9540 function declarations. */
9542 bool
9543 c_decl_implicit (const_tree)
9545 return false;