* g++.dg/cpp0x/constexpr-53094-2.C: Ignore non-standard ABI
[official-gcc.git] / gcc / cp / typeck.c
blobbfac39494e2a6c736f3f550ac4f52cdaac0c04d0
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2013 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 "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "intl.h"
36 #include "target.h"
37 #include "convert.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "params.h"
42 static tree pfn_from_ptrmemfunc (tree);
43 static tree delta_from_ptrmemfunc (tree);
44 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
45 tsubst_flags_t, int);
46 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
47 static tree rationalize_conditional_expr (enum tree_code, tree,
48 tsubst_flags_t);
49 static int comp_ptr_ttypes_real (tree, tree, int);
50 static bool comp_except_types (tree, tree, bool);
51 static bool comp_array_types (const_tree, const_tree, bool);
52 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
53 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
54 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
55 static bool casts_away_constness (tree, tree, tsubst_flags_t);
56 static void maybe_warn_about_returning_address_of_local (tree);
57 static tree lookup_destructor (tree, tree, tree);
58 static void warn_args_num (location_t, tree, bool);
59 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
60 tsubst_flags_t);
62 /* Do `exp = require_complete_type (exp);' to make sure exp
63 does not have an incomplete type. (That includes void types.)
64 Returns error_mark_node if the VALUE does not have
65 complete type when this function returns. */
67 tree
68 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
70 tree type;
72 if (processing_template_decl || value == error_mark_node)
73 return value;
75 if (TREE_CODE (value) == OVERLOAD)
76 type = unknown_type_node;
77 else
78 type = TREE_TYPE (value);
80 if (type == error_mark_node)
81 return error_mark_node;
83 /* First, detect a valid value with a complete type. */
84 if (COMPLETE_TYPE_P (type))
85 return value;
87 if (complete_type_or_maybe_complain (type, value, complain))
88 return value;
89 else
90 return error_mark_node;
93 tree
94 require_complete_type (tree value)
96 return require_complete_type_sfinae (value, tf_warning_or_error);
99 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
100 a template instantiation, do the instantiation. Returns TYPE,
101 whether or not it could be completed, unless something goes
102 horribly wrong, in which case the error_mark_node is returned. */
104 tree
105 complete_type (tree type)
107 if (type == NULL_TREE)
108 /* Rather than crash, we return something sure to cause an error
109 at some point. */
110 return error_mark_node;
112 if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
116 tree t = complete_type (TREE_TYPE (type));
117 unsigned int needs_constructing, has_nontrivial_dtor;
118 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
119 layout_type (type);
120 needs_constructing
121 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
122 has_nontrivial_dtor
123 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
124 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
127 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
130 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
131 instantiate_class_template (TYPE_MAIN_VARIANT (type));
133 return type;
136 /* Like complete_type, but issue an error if the TYPE cannot be completed.
137 VALUE is used for informative diagnostics.
138 Returns NULL_TREE if the type cannot be made complete. */
140 tree
141 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
143 type = complete_type (type);
144 if (type == error_mark_node)
145 /* We already issued an error. */
146 return NULL_TREE;
147 else if (!COMPLETE_TYPE_P (type))
149 if (complain & tf_error)
150 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
151 return NULL_TREE;
153 else
154 return type;
157 tree
158 complete_type_or_else (tree type, tree value)
160 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
163 /* Return truthvalue of whether type of EXP is instantiated. */
166 type_unknown_p (const_tree exp)
168 return (TREE_CODE (exp) == TREE_LIST
169 || TREE_TYPE (exp) == unknown_type_node);
173 /* Return the common type of two parameter lists.
174 We assume that comptypes has already been done and returned 1;
175 if that isn't so, this may crash.
177 As an optimization, free the space we allocate if the parameter
178 lists are already common. */
180 static tree
181 commonparms (tree p1, tree p2)
183 tree oldargs = p1, newargs, n;
184 int i, len;
185 int any_change = 0;
187 len = list_length (p1);
188 newargs = tree_last (p1);
190 if (newargs == void_list_node)
191 i = 1;
192 else
194 i = 0;
195 newargs = 0;
198 for (; i < len; i++)
199 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
201 n = newargs;
203 for (i = 0; p1;
204 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
206 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
208 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 any_change = 1;
211 else if (! TREE_PURPOSE (p1))
213 if (TREE_PURPOSE (p2))
215 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 any_change = 1;
219 else
221 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
222 any_change = 1;
223 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
225 if (TREE_VALUE (p1) != TREE_VALUE (p2))
227 any_change = 1;
228 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
230 else
231 TREE_VALUE (n) = TREE_VALUE (p1);
233 if (! any_change)
234 return oldargs;
236 return newargs;
239 /* Given a type, perhaps copied for a typedef,
240 find the "original" version of it. */
241 static tree
242 original_type (tree t)
244 int quals = cp_type_quals (t);
245 while (t != error_mark_node
246 && TYPE_NAME (t) != NULL_TREE)
248 tree x = TYPE_NAME (t);
249 if (TREE_CODE (x) != TYPE_DECL)
250 break;
251 x = DECL_ORIGINAL_TYPE (x);
252 if (x == NULL_TREE)
253 break;
254 t = x;
256 return cp_build_qualified_type (t, quals);
259 /* Return the common type for two arithmetic types T1 and T2 under the
260 usual arithmetic conversions. The default conversions have already
261 been applied, and enumerated types converted to their compatible
262 integer types. */
264 static tree
265 cp_common_type (tree t1, tree t2)
267 enum tree_code code1 = TREE_CODE (t1);
268 enum tree_code code2 = TREE_CODE (t2);
269 tree attributes;
272 /* In what follows, we slightly generalize the rules given in [expr] so
273 as to deal with `long long' and `complex'. First, merge the
274 attributes. */
275 attributes = (*targetm.merge_type_attributes) (t1, t2);
277 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
279 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
280 return build_type_attribute_variant (t1, attributes);
281 else
282 return NULL_TREE;
285 /* FIXME: Attributes. */
286 gcc_assert (ARITHMETIC_TYPE_P (t1)
287 || TREE_CODE (t1) == VECTOR_TYPE
288 || UNSCOPED_ENUM_P (t1));
289 gcc_assert (ARITHMETIC_TYPE_P (t2)
290 || TREE_CODE (t2) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t2));
293 /* If one type is complex, form the common type of the non-complex
294 components, then make that complex. Use T1 or T2 if it is the
295 required type. */
296 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
298 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
299 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
300 tree subtype
301 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
303 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
304 return build_type_attribute_variant (t1, attributes);
305 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
306 return build_type_attribute_variant (t2, attributes);
307 else
308 return build_type_attribute_variant (build_complex_type (subtype),
309 attributes);
312 if (code1 == VECTOR_TYPE)
314 /* When we get here we should have two vectors of the same size.
315 Just prefer the unsigned one if present. */
316 if (TYPE_UNSIGNED (t1))
317 return build_type_attribute_variant (t1, attributes);
318 else
319 return build_type_attribute_variant (t2, attributes);
322 /* If only one is real, use it as the result. */
323 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
324 return build_type_attribute_variant (t1, attributes);
325 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
326 return build_type_attribute_variant (t2, attributes);
328 /* Both real or both integers; use the one with greater precision. */
329 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
330 return build_type_attribute_variant (t1, attributes);
331 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
332 return build_type_attribute_variant (t2, attributes);
334 /* The types are the same; no need to do anything fancy. */
335 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
336 return build_type_attribute_variant (t1, attributes);
338 if (code1 != REAL_TYPE)
340 /* If one is unsigned long long, then convert the other to unsigned
341 long long. */
342 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
343 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
344 return build_type_attribute_variant (long_long_unsigned_type_node,
345 attributes);
346 /* If one is a long long, and the other is an unsigned long, and
347 long long can represent all the values of an unsigned long, then
348 convert to a long long. Otherwise, convert to an unsigned long
349 long. Otherwise, if either operand is long long, convert the
350 other to long long.
352 Since we're here, we know the TYPE_PRECISION is the same;
353 therefore converting to long long cannot represent all the values
354 of an unsigned long, so we choose unsigned long long in that
355 case. */
356 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
357 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
359 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
360 ? long_long_unsigned_type_node
361 : long_long_integer_type_node);
362 return build_type_attribute_variant (t, attributes);
364 if (int128_integer_type_node != NULL_TREE
365 && (same_type_p (TYPE_MAIN_VARIANT (t1),
366 int128_integer_type_node)
367 || same_type_p (TYPE_MAIN_VARIANT (t2),
368 int128_integer_type_node)))
370 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
371 ? int128_unsigned_type_node
372 : int128_integer_type_node);
373 return build_type_attribute_variant (t, attributes);
376 /* Go through the same procedure, but for longs. */
377 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
378 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
379 return build_type_attribute_variant (long_unsigned_type_node,
380 attributes);
381 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
382 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
384 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
385 ? long_unsigned_type_node : long_integer_type_node);
386 return build_type_attribute_variant (t, attributes);
388 /* Otherwise prefer the unsigned one. */
389 if (TYPE_UNSIGNED (t1))
390 return build_type_attribute_variant (t1, attributes);
391 else
392 return build_type_attribute_variant (t2, attributes);
394 else
396 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
397 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
398 return build_type_attribute_variant (long_double_type_node,
399 attributes);
400 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
401 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
402 return build_type_attribute_variant (double_type_node,
403 attributes);
404 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
405 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
406 return build_type_attribute_variant (float_type_node,
407 attributes);
409 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
410 the standard C++ floating-point types. Logic earlier in this
411 function has already eliminated the possibility that
412 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
413 compelling reason to choose one or the other. */
414 return build_type_attribute_variant (t1, attributes);
418 /* T1 and T2 are arithmetic or enumeration types. Return the type
419 that will result from the "usual arithmetic conversions" on T1 and
420 T2 as described in [expr]. */
422 tree
423 type_after_usual_arithmetic_conversions (tree t1, tree t2)
425 gcc_assert (ARITHMETIC_TYPE_P (t1)
426 || TREE_CODE (t1) == VECTOR_TYPE
427 || UNSCOPED_ENUM_P (t1));
428 gcc_assert (ARITHMETIC_TYPE_P (t2)
429 || TREE_CODE (t2) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t2));
432 /* Perform the integral promotions. We do not promote real types here. */
433 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
434 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
436 t1 = type_promotes_to (t1);
437 t2 = type_promotes_to (t2);
440 return cp_common_type (t1, t2);
443 static void
444 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
445 composite_pointer_operation operation)
447 switch (operation)
449 case CPO_COMPARISON:
450 emit_diagnostic (kind, input_location, 0,
451 "comparison between "
452 "distinct pointer types %qT and %qT lacks a cast",
453 t1, t2);
454 break;
455 case CPO_CONVERSION:
456 emit_diagnostic (kind, input_location, 0,
457 "conversion between "
458 "distinct pointer types %qT and %qT lacks a cast",
459 t1, t2);
460 break;
461 case CPO_CONDITIONAL_EXPR:
462 emit_diagnostic (kind, input_location, 0,
463 "conditional expression between "
464 "distinct pointer types %qT and %qT lacks a cast",
465 t1, t2);
466 break;
467 default:
468 gcc_unreachable ();
472 /* Subroutine of composite_pointer_type to implement the recursive
473 case. See that function for documentation of the parameters. */
475 static tree
476 composite_pointer_type_r (tree t1, tree t2,
477 composite_pointer_operation operation,
478 tsubst_flags_t complain)
480 tree pointee1;
481 tree pointee2;
482 tree result_type;
483 tree attributes;
485 /* Determine the types pointed to by T1 and T2. */
486 if (TREE_CODE (t1) == POINTER_TYPE)
488 pointee1 = TREE_TYPE (t1);
489 pointee2 = TREE_TYPE (t2);
491 else
493 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
494 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
497 /* [expr.rel]
499 Otherwise, the composite pointer type is a pointer type
500 similar (_conv.qual_) to the type of one of the operands,
501 with a cv-qualification signature (_conv.qual_) that is the
502 union of the cv-qualification signatures of the operand
503 types. */
504 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
505 result_type = pointee1;
506 else if ((TREE_CODE (pointee1) == POINTER_TYPE
507 && TREE_CODE (pointee2) == POINTER_TYPE)
508 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
510 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
511 complain);
512 if (result_type == error_mark_node)
513 return error_mark_node;
515 else
517 if (complain & tf_error)
518 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
519 else
520 return error_mark_node;
521 result_type = void_type_node;
523 result_type = cp_build_qualified_type (result_type,
524 (cp_type_quals (pointee1)
525 | cp_type_quals (pointee2)));
526 /* If the original types were pointers to members, so is the
527 result. */
528 if (TYPE_PTRMEM_P (t1))
530 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
531 TYPE_PTRMEM_CLASS_TYPE (t2)))
533 if (complain & tf_error)
534 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
535 else
536 return error_mark_node;
538 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
539 result_type);
541 else
542 result_type = build_pointer_type (result_type);
544 /* Merge the attributes. */
545 attributes = (*targetm.merge_type_attributes) (t1, t2);
546 return build_type_attribute_variant (result_type, attributes);
549 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
550 ARG1 and ARG2 are the values with those types. The OPERATION is to
551 describe the operation between the pointer types,
552 in case an error occurs.
554 This routine also implements the computation of a common type for
555 pointers-to-members as per [expr.eq]. */
557 tree
558 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
559 composite_pointer_operation operation,
560 tsubst_flags_t complain)
562 tree class1;
563 tree class2;
565 /* [expr.rel]
567 If one operand is a null pointer constant, the composite pointer
568 type is the type of the other operand. */
569 if (null_ptr_cst_p (arg1))
570 return t2;
571 if (null_ptr_cst_p (arg2))
572 return t1;
574 /* We have:
576 [expr.rel]
578 If one of the operands has type "pointer to cv1 void*", then
579 the other has type "pointer to cv2T", and the composite pointer
580 type is "pointer to cv12 void", where cv12 is the union of cv1
581 and cv2.
583 If either type is a pointer to void, make sure it is T1. */
584 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
586 tree t;
587 t = t1;
588 t1 = t2;
589 t2 = t;
592 /* Now, if T1 is a pointer to void, merge the qualifiers. */
593 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
595 tree attributes;
596 tree result_type;
598 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
600 switch (operation)
602 case CPO_COMPARISON:
603 pedwarn (input_location, OPT_Wpedantic,
604 "ISO C++ forbids comparison between "
605 "pointer of type %<void *%> and pointer-to-function");
606 break;
607 case CPO_CONVERSION:
608 pedwarn (input_location, OPT_Wpedantic,
609 "ISO C++ forbids conversion between "
610 "pointer of type %<void *%> and pointer-to-function");
611 break;
612 case CPO_CONDITIONAL_EXPR:
613 pedwarn (input_location, OPT_Wpedantic,
614 "ISO C++ forbids conditional expression between "
615 "pointer of type %<void *%> and pointer-to-function");
616 break;
617 default:
618 gcc_unreachable ();
621 result_type
622 = cp_build_qualified_type (void_type_node,
623 (cp_type_quals (TREE_TYPE (t1))
624 | cp_type_quals (TREE_TYPE (t2))));
625 result_type = build_pointer_type (result_type);
626 /* Merge the attributes. */
627 attributes = (*targetm.merge_type_attributes) (t1, t2);
628 return build_type_attribute_variant (result_type, attributes);
631 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
632 && TREE_CODE (t2) == POINTER_TYPE)
634 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
635 return objc_common_type (t1, t2);
638 /* [expr.eq] permits the application of a pointer conversion to
639 bring the pointers to a common type. */
640 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
641 && CLASS_TYPE_P (TREE_TYPE (t1))
642 && CLASS_TYPE_P (TREE_TYPE (t2))
643 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
644 TREE_TYPE (t2)))
646 class1 = TREE_TYPE (t1);
647 class2 = TREE_TYPE (t2);
649 if (DERIVED_FROM_P (class1, class2))
650 t2 = (build_pointer_type
651 (cp_build_qualified_type (class1, cp_type_quals (class2))));
652 else if (DERIVED_FROM_P (class2, class1))
653 t1 = (build_pointer_type
654 (cp_build_qualified_type (class2, cp_type_quals (class1))));
655 else
657 if (complain & tf_error)
658 composite_pointer_error (DK_ERROR, t1, t2, operation);
659 return error_mark_node;
662 /* [expr.eq] permits the application of a pointer-to-member
663 conversion to change the class type of one of the types. */
664 else if (TYPE_PTRMEM_P (t1)
665 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
666 TYPE_PTRMEM_CLASS_TYPE (t2)))
668 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
669 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
671 if (DERIVED_FROM_P (class1, class2))
672 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
673 else if (DERIVED_FROM_P (class2, class1))
674 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
675 else
677 if (complain & tf_error)
678 switch (operation)
680 case CPO_COMPARISON:
681 error ("comparison between distinct "
682 "pointer-to-member types %qT and %qT lacks a cast",
683 t1, t2);
684 break;
685 case CPO_CONVERSION:
686 error ("conversion between distinct "
687 "pointer-to-member types %qT and %qT lacks a cast",
688 t1, t2);
689 break;
690 case CPO_CONDITIONAL_EXPR:
691 error ("conditional expression between distinct "
692 "pointer-to-member types %qT and %qT lacks a cast",
693 t1, t2);
694 break;
695 default:
696 gcc_unreachable ();
698 return error_mark_node;
702 return composite_pointer_type_r (t1, t2, operation, complain);
705 /* Return the merged type of two types.
706 We assume that comptypes has already been done and returned 1;
707 if that isn't so, this may crash.
709 This just combines attributes and default arguments; any other
710 differences would cause the two types to compare unalike. */
712 tree
713 merge_types (tree t1, tree t2)
715 enum tree_code code1;
716 enum tree_code code2;
717 tree attributes;
719 /* Save time if the two types are the same. */
720 if (t1 == t2)
721 return t1;
722 if (original_type (t1) == original_type (t2))
723 return t1;
725 /* If one type is nonsense, use the other. */
726 if (t1 == error_mark_node)
727 return t2;
728 if (t2 == error_mark_node)
729 return t1;
731 /* Handle merging an auto redeclaration with a previous deduced
732 return type. */
733 if (is_auto (t1))
734 return t2;
736 /* Merge the attributes. */
737 attributes = (*targetm.merge_type_attributes) (t1, t2);
739 if (TYPE_PTRMEMFUNC_P (t1))
740 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
741 if (TYPE_PTRMEMFUNC_P (t2))
742 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
744 code1 = TREE_CODE (t1);
745 code2 = TREE_CODE (t2);
746 if (code1 != code2)
748 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
749 if (code1 == TYPENAME_TYPE)
751 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
752 code1 = TREE_CODE (t1);
754 else
756 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
757 code2 = TREE_CODE (t2);
761 switch (code1)
763 case POINTER_TYPE:
764 case REFERENCE_TYPE:
765 /* For two pointers, do this recursively on the target type. */
767 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
768 int quals = cp_type_quals (t1);
770 if (code1 == POINTER_TYPE)
771 t1 = build_pointer_type (target);
772 else
773 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
774 t1 = build_type_attribute_variant (t1, attributes);
775 t1 = cp_build_qualified_type (t1, quals);
777 if (TREE_CODE (target) == METHOD_TYPE)
778 t1 = build_ptrmemfunc_type (t1);
780 return t1;
783 case OFFSET_TYPE:
785 int quals;
786 tree pointee;
787 quals = cp_type_quals (t1);
788 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
789 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
790 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
791 pointee);
792 t1 = cp_build_qualified_type (t1, quals);
793 break;
796 case ARRAY_TYPE:
798 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
799 /* Save space: see if the result is identical to one of the args. */
800 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
801 return build_type_attribute_variant (t1, attributes);
802 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
803 return build_type_attribute_variant (t2, attributes);
804 /* Merge the element types, and have a size if either arg has one. */
805 t1 = build_cplus_array_type
806 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
807 break;
810 case FUNCTION_TYPE:
811 /* Function types: prefer the one that specified arg types.
812 If both do, merge the arg types. Also merge the return types. */
814 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
815 tree p1 = TYPE_ARG_TYPES (t1);
816 tree p2 = TYPE_ARG_TYPES (t2);
817 tree parms;
818 tree rval, raises;
820 /* Save space: see if the result is identical to one of the args. */
821 if (valtype == TREE_TYPE (t1) && ! p2)
822 return cp_build_type_attribute_variant (t1, attributes);
823 if (valtype == TREE_TYPE (t2) && ! p1)
824 return cp_build_type_attribute_variant (t2, attributes);
826 /* Simple way if one arg fails to specify argument types. */
827 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
828 parms = p2;
829 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
830 parms = p1;
831 else
832 parms = commonparms (p1, p2);
834 rval = build_function_type (valtype, parms);
835 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
836 rval = apply_memfn_quals (rval, type_memfn_quals (t1));
837 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
838 TYPE_RAISES_EXCEPTIONS (t2),
839 NULL_TREE);
840 t1 = build_exception_variant (rval, raises);
841 break;
844 case METHOD_TYPE:
846 /* Get this value the long way, since TYPE_METHOD_BASETYPE
847 is just the main variant of this. */
848 tree basetype = class_of_this_parm (t2);
849 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
850 TYPE_RAISES_EXCEPTIONS (t2),
851 NULL_TREE);
852 tree t3;
854 /* If this was a member function type, get back to the
855 original type of type member function (i.e., without
856 the class instance variable up front. */
857 t1 = build_function_type (TREE_TYPE (t1),
858 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
859 t2 = build_function_type (TREE_TYPE (t2),
860 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
861 t3 = merge_types (t1, t2);
862 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
863 TYPE_ARG_TYPES (t3));
864 t1 = build_exception_variant (t3, raises);
865 break;
868 case TYPENAME_TYPE:
869 /* There is no need to merge attributes into a TYPENAME_TYPE.
870 When the type is instantiated it will have whatever
871 attributes result from the instantiation. */
872 return t1;
874 default:;
877 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
878 return t1;
879 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
880 return t2;
881 else
882 return cp_build_type_attribute_variant (t1, attributes);
885 /* Return the ARRAY_TYPE type without its domain. */
887 tree
888 strip_array_domain (tree type)
890 tree t2;
891 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
892 if (TYPE_DOMAIN (type) == NULL_TREE)
893 return type;
894 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
895 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
898 /* Wrapper around cp_common_type that is used by c-common.c and other
899 front end optimizations that remove promotions.
901 Return the common type for two arithmetic types T1 and T2 under the
902 usual arithmetic conversions. The default conversions have already
903 been applied, and enumerated types converted to their compatible
904 integer types. */
906 tree
907 common_type (tree t1, tree t2)
909 /* If one type is nonsense, use the other */
910 if (t1 == error_mark_node)
911 return t2;
912 if (t2 == error_mark_node)
913 return t1;
915 return cp_common_type (t1, t2);
918 /* Return the common type of two pointer types T1 and T2. This is the
919 type for the result of most arithmetic operations if the operands
920 have the given two types.
922 We assume that comp_target_types has already been done and returned
923 nonzero; if that isn't so, this may crash. */
925 tree
926 common_pointer_type (tree t1, tree t2)
928 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
929 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
930 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
932 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
933 CPO_CONVERSION, tf_warning_or_error);
936 /* Compare two exception specifier types for exactness or subsetness, if
937 allowed. Returns false for mismatch, true for match (same, or
938 derived and !exact).
940 [except.spec] "If a class X ... objects of class X or any class publicly
941 and unambiguously derived from X. Similarly, if a pointer type Y * ...
942 exceptions of type Y * or that are pointers to any type publicly and
943 unambiguously derived from Y. Otherwise a function only allows exceptions
944 that have the same type ..."
945 This does not mention cv qualifiers and is different to what throw
946 [except.throw] and catch [except.catch] will do. They will ignore the
947 top level cv qualifiers, and allow qualifiers in the pointer to class
948 example.
950 We implement the letter of the standard. */
952 static bool
953 comp_except_types (tree a, tree b, bool exact)
955 if (same_type_p (a, b))
956 return true;
957 else if (!exact)
959 if (cp_type_quals (a) || cp_type_quals (b))
960 return false;
962 if (TREE_CODE (a) == POINTER_TYPE
963 && TREE_CODE (b) == POINTER_TYPE)
965 a = TREE_TYPE (a);
966 b = TREE_TYPE (b);
967 if (cp_type_quals (a) || cp_type_quals (b))
968 return false;
971 if (TREE_CODE (a) != RECORD_TYPE
972 || TREE_CODE (b) != RECORD_TYPE)
973 return false;
975 if (publicly_uniquely_derived_p (a, b))
976 return true;
978 return false;
981 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
982 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
983 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
984 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
985 are unordered, but we've already filtered out duplicates. Most lists will
986 be in order, we should try to make use of that. */
988 bool
989 comp_except_specs (const_tree t1, const_tree t2, int exact)
991 const_tree probe;
992 const_tree base;
993 int length = 0;
995 if (t1 == t2)
996 return true;
998 /* First handle noexcept. */
999 if (exact < ce_exact)
1001 /* noexcept(false) is compatible with no exception-specification,
1002 and stricter than any spec. */
1003 if (t1 == noexcept_false_spec)
1004 return t2 == NULL_TREE || exact == ce_derived;
1005 /* Even a derived noexcept(false) is compatible with no
1006 exception-specification. */
1007 if (t2 == noexcept_false_spec)
1008 return t1 == NULL_TREE;
1010 /* Otherwise, if we aren't looking for an exact match, noexcept is
1011 equivalent to throw(). */
1012 if (t1 == noexcept_true_spec)
1013 t1 = empty_except_spec;
1014 if (t2 == noexcept_true_spec)
1015 t2 = empty_except_spec;
1018 /* If any noexcept is left, it is only comparable to itself;
1019 either we're looking for an exact match or we're redeclaring a
1020 template with dependent noexcept. */
1021 if ((t1 && TREE_PURPOSE (t1))
1022 || (t2 && TREE_PURPOSE (t2)))
1023 return (t1 && t2
1024 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1026 if (t1 == NULL_TREE) /* T1 is ... */
1027 return t2 == NULL_TREE || exact == ce_derived;
1028 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1029 return t2 != NULL_TREE && !TREE_VALUE (t2);
1030 if (t2 == NULL_TREE) /* T2 is ... */
1031 return false;
1032 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1033 return exact == ce_derived;
1035 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1036 Count how many we find, to determine exactness. For exact matching and
1037 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1038 O(nm). */
1039 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1041 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1043 tree a = TREE_VALUE (probe);
1044 tree b = TREE_VALUE (t2);
1046 if (comp_except_types (a, b, exact))
1048 if (probe == base && exact > ce_derived)
1049 base = TREE_CHAIN (probe);
1050 length++;
1051 break;
1054 if (probe == NULL_TREE)
1055 return false;
1057 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1060 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1061 [] can match [size]. */
1063 static bool
1064 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1066 tree d1;
1067 tree d2;
1068 tree max1, max2;
1070 if (t1 == t2)
1071 return true;
1073 /* The type of the array elements must be the same. */
1074 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1075 return false;
1077 d1 = TYPE_DOMAIN (t1);
1078 d2 = TYPE_DOMAIN (t2);
1080 if (d1 == d2)
1081 return true;
1083 /* If one of the arrays is dimensionless, and the other has a
1084 dimension, they are of different types. However, it is valid to
1085 write:
1087 extern int a[];
1088 int a[3];
1090 by [basic.link]:
1092 declarations for an array object can specify
1093 array types that differ by the presence or absence of a major
1094 array bound (_dcl.array_). */
1095 if (!d1 || !d2)
1096 return allow_redeclaration;
1098 /* Check that the dimensions are the same. */
1100 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1101 return false;
1102 max1 = TYPE_MAX_VALUE (d1);
1103 max2 = TYPE_MAX_VALUE (d2);
1104 if (processing_template_decl && !abi_version_at_least (2)
1105 && !value_dependent_expression_p (max1)
1106 && !value_dependent_expression_p (max2))
1108 /* With abi-1 we do not fold non-dependent array bounds, (and
1109 consequently mangle them incorrectly). We must therefore
1110 fold them here, to verify the domains have the same
1111 value. */
1112 max1 = fold (max1);
1113 max2 = fold (max2);
1116 if (!cp_tree_equal (max1, max2))
1117 return false;
1119 return true;
1122 /* Compare the relative position of T1 and T2 into their respective
1123 template parameter list.
1124 T1 and T2 must be template parameter types.
1125 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1127 static bool
1128 comp_template_parms_position (tree t1, tree t2)
1130 tree index1, index2;
1131 gcc_assert (t1 && t2
1132 && TREE_CODE (t1) == TREE_CODE (t2)
1133 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1134 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1135 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1137 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1138 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1140 /* Then compare their relative position. */
1141 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1142 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1143 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1144 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1145 return false;
1147 return true;
1150 /* Subroutine in comptypes. */
1152 static bool
1153 structural_comptypes (tree t1, tree t2, int strict)
1155 if (t1 == t2)
1156 return true;
1158 /* Suppress errors caused by previously reported errors. */
1159 if (t1 == error_mark_node || t2 == error_mark_node)
1160 return false;
1162 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1164 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1165 current instantiation. */
1166 if (TREE_CODE (t1) == TYPENAME_TYPE)
1167 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1169 if (TREE_CODE (t2) == TYPENAME_TYPE)
1170 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1172 if (TYPE_PTRMEMFUNC_P (t1))
1173 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1174 if (TYPE_PTRMEMFUNC_P (t2))
1175 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1177 /* Different classes of types can't be compatible. */
1178 if (TREE_CODE (t1) != TREE_CODE (t2))
1179 return false;
1181 /* Qualifiers must match. For array types, we will check when we
1182 recur on the array element types. */
1183 if (TREE_CODE (t1) != ARRAY_TYPE
1184 && cp_type_quals (t1) != cp_type_quals (t2))
1185 return false;
1186 if (TREE_CODE (t1) == FUNCTION_TYPE
1187 && type_memfn_quals (t1) != type_memfn_quals (t2))
1188 return false;
1189 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1190 return false;
1192 /* Allow for two different type nodes which have essentially the same
1193 definition. Note that we already checked for equality of the type
1194 qualifiers (just above). */
1196 if (TREE_CODE (t1) != ARRAY_TYPE
1197 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1198 return true;
1201 /* Compare the types. Break out if they could be the same. */
1202 switch (TREE_CODE (t1))
1204 case VOID_TYPE:
1205 case BOOLEAN_TYPE:
1206 /* All void and bool types are the same. */
1207 break;
1209 case INTEGER_TYPE:
1210 case FIXED_POINT_TYPE:
1211 case REAL_TYPE:
1212 /* With these nodes, we can't determine type equivalence by
1213 looking at what is stored in the nodes themselves, because
1214 two nodes might have different TYPE_MAIN_VARIANTs but still
1215 represent the same type. For example, wchar_t and int could
1216 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1217 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1218 and are distinct types. On the other hand, int and the
1219 following typedef
1221 typedef int INT __attribute((may_alias));
1223 have identical properties, different TYPE_MAIN_VARIANTs, but
1224 represent the same type. The canonical type system keeps
1225 track of equivalence in this case, so we fall back on it. */
1226 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1228 case TEMPLATE_TEMPLATE_PARM:
1229 case BOUND_TEMPLATE_TEMPLATE_PARM:
1230 if (!comp_template_parms_position (t1, t2))
1231 return false;
1232 if (!comp_template_parms
1233 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1234 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1235 return false;
1236 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1237 break;
1238 /* Don't check inheritance. */
1239 strict = COMPARE_STRICT;
1240 /* Fall through. */
1242 case RECORD_TYPE:
1243 case UNION_TYPE:
1244 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1245 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1246 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1247 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1248 break;
1250 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1251 break;
1252 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1253 break;
1255 return false;
1257 case OFFSET_TYPE:
1258 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1259 strict & ~COMPARE_REDECLARATION))
1260 return false;
1261 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1262 return false;
1263 break;
1265 case REFERENCE_TYPE:
1266 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1267 return false;
1268 /* fall through to checks for pointer types */
1270 case POINTER_TYPE:
1271 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1272 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1273 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1274 return false;
1275 break;
1277 case METHOD_TYPE:
1278 case FUNCTION_TYPE:
1279 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1280 return false;
1281 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1282 return false;
1283 break;
1285 case ARRAY_TYPE:
1286 /* Target types must match incl. qualifiers. */
1287 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1288 return false;
1289 break;
1291 case TEMPLATE_TYPE_PARM:
1292 /* If T1 and T2 don't have the same relative position in their
1293 template parameters set, they can't be equal. */
1294 if (!comp_template_parms_position (t1, t2))
1295 return false;
1296 break;
1298 case TYPENAME_TYPE:
1299 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1300 TYPENAME_TYPE_FULLNAME (t2)))
1301 return false;
1302 /* Qualifiers don't matter on scopes. */
1303 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1304 TYPE_CONTEXT (t2)))
1305 return false;
1306 break;
1308 case UNBOUND_CLASS_TEMPLATE:
1309 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1310 return false;
1311 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1312 return false;
1313 break;
1315 case COMPLEX_TYPE:
1316 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1317 return false;
1318 break;
1320 case VECTOR_TYPE:
1321 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1322 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1323 return false;
1324 break;
1326 case TYPE_PACK_EXPANSION:
1327 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1328 PACK_EXPANSION_PATTERN (t2))
1329 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1330 PACK_EXPANSION_EXTRA_ARGS (t2)));
1332 case DECLTYPE_TYPE:
1333 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1334 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1335 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1336 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1337 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1338 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1339 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1340 DECLTYPE_TYPE_EXPR (t2)))
1341 return false;
1342 break;
1344 case UNDERLYING_TYPE:
1345 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1346 UNDERLYING_TYPE_TYPE (t2));
1348 default:
1349 return false;
1352 /* If we get here, we know that from a target independent POV the
1353 types are the same. Make sure the target attributes are also
1354 the same. */
1355 return comp_type_attributes (t1, t2);
1358 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1359 is a bitwise-or of the COMPARE_* flags. */
1361 bool
1362 comptypes (tree t1, tree t2, int strict)
1364 if (strict == COMPARE_STRICT)
1366 if (t1 == t2)
1367 return true;
1369 if (t1 == error_mark_node || t2 == error_mark_node)
1370 return false;
1372 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1373 /* At least one of the types requires structural equality, so
1374 perform a deep check. */
1375 return structural_comptypes (t1, t2, strict);
1377 #ifdef ENABLE_CHECKING
1378 if (USE_CANONICAL_TYPES)
1380 bool result = structural_comptypes (t1, t2, strict);
1382 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1383 /* The two types are structurally equivalent, but their
1384 canonical types were different. This is a failure of the
1385 canonical type propagation code.*/
1386 internal_error
1387 ("canonical types differ for identical types %T and %T",
1388 t1, t2);
1389 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1390 /* Two types are structurally different, but the canonical
1391 types are the same. This means we were over-eager in
1392 assigning canonical types. */
1393 internal_error
1394 ("same canonical type node for different types %T and %T",
1395 t1, t2);
1397 return result;
1399 #else
1400 if (USE_CANONICAL_TYPES)
1401 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1402 #endif
1403 else
1404 return structural_comptypes (t1, t2, strict);
1406 else if (strict == COMPARE_STRUCTURAL)
1407 return structural_comptypes (t1, t2, COMPARE_STRICT);
1408 else
1409 return structural_comptypes (t1, t2, strict);
1412 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1413 top-level qualifiers. */
1415 bool
1416 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1418 if (type1 == error_mark_node || type2 == error_mark_node)
1419 return false;
1421 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1424 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1426 bool
1427 at_least_as_qualified_p (const_tree type1, const_tree type2)
1429 int q1 = cp_type_quals (type1);
1430 int q2 = cp_type_quals (type2);
1432 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1433 return (q1 & q2) == q2;
1436 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1437 more cv-qualified that TYPE1, and 0 otherwise. */
1440 comp_cv_qualification (const_tree type1, const_tree type2)
1442 int q1 = cp_type_quals (type1);
1443 int q2 = cp_type_quals (type2);
1445 if (q1 == q2)
1446 return 0;
1448 if ((q1 & q2) == q2)
1449 return 1;
1450 else if ((q1 & q2) == q1)
1451 return -1;
1453 return 0;
1456 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1457 subset of the cv-qualification signature of TYPE2, and the types
1458 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1461 comp_cv_qual_signature (tree type1, tree type2)
1463 if (comp_ptr_ttypes_real (type2, type1, -1))
1464 return 1;
1465 else if (comp_ptr_ttypes_real (type1, type2, -1))
1466 return -1;
1467 else
1468 return 0;
1471 /* Subroutines of `comptypes'. */
1473 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1474 equivalent in the sense that functions with those parameter types
1475 can have equivalent types. The two lists must be equivalent,
1476 element by element. */
1478 bool
1479 compparms (const_tree parms1, const_tree parms2)
1481 const_tree t1, t2;
1483 /* An unspecified parmlist matches any specified parmlist
1484 whose argument types don't need default promotions. */
1486 for (t1 = parms1, t2 = parms2;
1487 t1 || t2;
1488 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1490 /* If one parmlist is shorter than the other,
1491 they fail to match. */
1492 if (!t1 || !t2)
1493 return false;
1494 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1495 return false;
1497 return true;
1501 /* Process a sizeof or alignof expression where the operand is a
1502 type. */
1504 tree
1505 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1507 tree value;
1508 bool dependent_p;
1510 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1511 if (type == error_mark_node)
1512 return error_mark_node;
1514 type = non_reference (type);
1515 if (TREE_CODE (type) == METHOD_TYPE)
1517 if (complain)
1518 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
1519 "invalid application of %qs to a member function",
1520 operator_name_info[(int) op].name);
1521 value = size_one_node;
1524 dependent_p = dependent_type_p (type);
1525 if (!dependent_p)
1526 complete_type (type);
1527 if (dependent_p
1528 /* VLA types will have a non-constant size. In the body of an
1529 uninstantiated template, we don't need to try to compute the
1530 value, because the sizeof expression is not an integral
1531 constant expression in that case. And, if we do try to
1532 compute the value, we'll likely end up with SAVE_EXPRs, which
1533 the template substitution machinery does not expect to see. */
1534 || (processing_template_decl
1535 && COMPLETE_TYPE_P (type)
1536 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1538 value = build_min (op, size_type_node, type);
1539 TREE_READONLY (value) = 1;
1540 return value;
1543 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1544 op == SIZEOF_EXPR,
1545 complain);
1548 /* Return the size of the type, without producing any warnings for
1549 types whose size cannot be taken. This routine should be used only
1550 in some other routine that has already produced a diagnostic about
1551 using the size of such a type. */
1552 tree
1553 cxx_sizeof_nowarn (tree type)
1555 if (TREE_CODE (type) == FUNCTION_TYPE
1556 || TREE_CODE (type) == VOID_TYPE
1557 || TREE_CODE (type) == ERROR_MARK)
1558 return size_one_node;
1559 else if (!COMPLETE_TYPE_P (type))
1560 return size_zero_node;
1561 else
1562 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1565 /* Process a sizeof expression where the operand is an expression. */
1567 static tree
1568 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1570 if (e == error_mark_node)
1571 return error_mark_node;
1573 if (processing_template_decl)
1575 e = build_min (SIZEOF_EXPR, size_type_node, e);
1576 TREE_SIDE_EFFECTS (e) = 0;
1577 TREE_READONLY (e) = 1;
1579 return e;
1582 /* To get the size of a static data member declared as an array of
1583 unknown bound, we need to instantiate it. */
1584 if (TREE_CODE (e) == VAR_DECL
1585 && VAR_HAD_UNKNOWN_BOUND (e)
1586 && DECL_TEMPLATE_INSTANTIATION (e))
1587 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1589 e = mark_type_use (e);
1591 if (TREE_CODE (e) == COMPONENT_REF
1592 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1593 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1595 if (complain & tf_error)
1596 error ("invalid application of %<sizeof%> to a bit-field");
1597 else
1598 return error_mark_node;
1599 e = char_type_node;
1601 else if (is_overloaded_fn (e))
1603 if (complain & tf_error)
1604 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1605 "function type");
1606 else
1607 return error_mark_node;
1608 e = char_type_node;
1610 else if (type_unknown_p (e))
1612 if (complain & tf_error)
1613 cxx_incomplete_type_error (e, TREE_TYPE (e));
1614 else
1615 return error_mark_node;
1616 e = char_type_node;
1618 else
1619 e = TREE_TYPE (e);
1621 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1624 /* Implement the __alignof keyword: Return the minimum required
1625 alignment of E, measured in bytes. For VAR_DECL's and
1626 FIELD_DECL's return DECL_ALIGN (which can be set from an
1627 "aligned" __attribute__ specification). */
1629 static tree
1630 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1632 tree t;
1634 if (e == error_mark_node)
1635 return error_mark_node;
1637 if (processing_template_decl)
1639 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1640 TREE_SIDE_EFFECTS (e) = 0;
1641 TREE_READONLY (e) = 1;
1643 return e;
1646 e = mark_type_use (e);
1648 if (TREE_CODE (e) == VAR_DECL)
1649 t = size_int (DECL_ALIGN_UNIT (e));
1650 else if (TREE_CODE (e) == COMPONENT_REF
1651 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1652 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1654 if (complain & tf_error)
1655 error ("invalid application of %<__alignof%> to a bit-field");
1656 else
1657 return error_mark_node;
1658 t = size_one_node;
1660 else if (TREE_CODE (e) == COMPONENT_REF
1661 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1662 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1663 else if (is_overloaded_fn (e))
1665 if (complain & tf_error)
1666 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1667 "function type");
1668 else
1669 return error_mark_node;
1670 if (TREE_CODE (e) == FUNCTION_DECL)
1671 t = size_int (DECL_ALIGN_UNIT (e));
1672 else
1673 t = size_one_node;
1675 else if (type_unknown_p (e))
1677 if (complain & tf_error)
1678 cxx_incomplete_type_error (e, TREE_TYPE (e));
1679 else
1680 return error_mark_node;
1681 t = size_one_node;
1683 else
1684 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1685 complain & tf_error);
1687 return fold_convert (size_type_node, t);
1690 /* Process a sizeof or alignof expression E with code OP where the operand
1691 is an expression. */
1693 tree
1694 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1696 if (op == SIZEOF_EXPR)
1697 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1698 else
1699 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1702 /* Build a representation of an expression 'alignas(E).' Return the
1703 folded integer value of E if it is an integral constant expression
1704 that resolves to a valid alignment. If E depends on a template
1705 parameter, return a syntactic representation tree of kind
1706 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1707 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1709 tree
1710 cxx_alignas_expr (tree e)
1712 if (e == NULL_TREE || e == error_mark_node
1713 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1714 return e;
1716 if (TYPE_P (e))
1717 /* [dcl.align]/3:
1719 When the alignment-specifier is of the form
1720 alignas(type-id ), it shall have the same effect as
1721 alignas( alignof(type-id )). */
1723 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1726 /* If we reach this point, it means the alignas expression if of
1727 the form "alignas(assignment-expression)", so we should follow
1728 what is stated by [dcl.align]/2. */
1730 e = mark_rvalue_use (e);
1732 /* [dcl.align]/2 says:
1734 the assignment-expression shall be an integral constant
1735 expression. */
1737 e = fold_non_dependent_expr (e);
1738 if (value_dependent_expression_p (e))
1739 /* Leave value-dependent expression alone for now. */;
1740 else
1741 e = cxx_constant_value (e);
1743 if (e == NULL_TREE
1744 || e == error_mark_node
1745 || TREE_CODE (e) != INTEGER_CST)
1746 return error_mark_node;
1748 return e;
1752 /* EXPR is being used in a context that is not a function call.
1753 Enforce:
1755 [expr.ref]
1757 The expression can be used only as the left-hand operand of a
1758 member function call.
1760 [expr.mptr.operator]
1762 If the result of .* or ->* is a function, then that result can be
1763 used only as the operand for the function call operator ().
1765 by issuing an error message if appropriate. Returns true iff EXPR
1766 violates these rules. */
1768 bool
1769 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1771 if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1773 if (complain & tf_error)
1774 error ("invalid use of non-static member function");
1775 return true;
1777 return false;
1780 /* If EXP is a reference to a bitfield, and the type of EXP does not
1781 match the declared type of the bitfield, return the declared type
1782 of the bitfield. Otherwise, return NULL_TREE. */
1784 tree
1785 is_bitfield_expr_with_lowered_type (const_tree exp)
1787 switch (TREE_CODE (exp))
1789 case COND_EXPR:
1790 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1791 ? TREE_OPERAND (exp, 1)
1792 : TREE_OPERAND (exp, 0)))
1793 return NULL_TREE;
1794 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1796 case COMPOUND_EXPR:
1797 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1799 case MODIFY_EXPR:
1800 case SAVE_EXPR:
1801 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1803 case COMPONENT_REF:
1805 tree field;
1807 field = TREE_OPERAND (exp, 1);
1808 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1809 return NULL_TREE;
1810 if (same_type_ignoring_top_level_qualifiers_p
1811 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1812 return NULL_TREE;
1813 return DECL_BIT_FIELD_TYPE (field);
1816 CASE_CONVERT:
1817 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1818 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1819 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1820 /* Fallthrough. */
1822 default:
1823 return NULL_TREE;
1827 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1828 bitfield with a lowered type, the type of EXP is returned, rather
1829 than NULL_TREE. */
1831 tree
1832 unlowered_expr_type (const_tree exp)
1834 tree type;
1835 tree etype = TREE_TYPE (exp);
1837 type = is_bitfield_expr_with_lowered_type (exp);
1838 if (type)
1839 type = cp_build_qualified_type (type, cp_type_quals (etype));
1840 else
1841 type = etype;
1843 return type;
1846 /* Perform the conversions in [expr] that apply when an lvalue appears
1847 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1848 function-to-pointer conversions. In addition, manifest constants
1849 are replaced by their values, and bitfield references are converted
1850 to their declared types. Note that this function does not perform the
1851 lvalue-to-rvalue conversion for class types. If you need that conversion
1852 to for class types, then you probably need to use force_rvalue.
1854 Although the returned value is being used as an rvalue, this
1855 function does not wrap the returned expression in a
1856 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1857 that the return value is no longer an lvalue. */
1859 tree
1860 decay_conversion (tree exp, tsubst_flags_t complain)
1862 tree type;
1863 enum tree_code code;
1864 location_t loc = EXPR_LOC_OR_HERE (exp);
1866 type = TREE_TYPE (exp);
1867 if (type == error_mark_node)
1868 return error_mark_node;
1870 exp = mark_rvalue_use (exp);
1872 exp = resolve_nondeduced_context (exp);
1873 if (type_unknown_p (exp))
1875 if (complain & tf_error)
1876 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1877 return error_mark_node;
1880 code = TREE_CODE (type);
1882 /* For an array decl decay_conversion should not try to return its
1883 initializer. */
1884 if (code != ARRAY_TYPE)
1886 /* FIXME remove? at least need to remember that this isn't really a
1887 constant expression if EXP isn't decl_constant_var_p, like with
1888 C_MAYBE_CONST_EXPR. */
1889 exp = decl_constant_value_safe (exp);
1890 if (error_operand_p (exp))
1891 return error_mark_node;
1894 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1895 return nullptr_node;
1897 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1898 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1899 if (code == VOID_TYPE)
1901 if (complain & tf_error)
1902 error_at (loc, "void value not ignored as it ought to be");
1903 return error_mark_node;
1905 if (invalid_nonstatic_memfn_p (exp, complain))
1906 return error_mark_node;
1907 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1908 return cp_build_addr_expr (exp, complain);
1909 if (code == ARRAY_TYPE)
1911 tree adr;
1912 tree ptrtype;
1914 if (TREE_CODE (exp) == INDIRECT_REF)
1915 return build_nop (build_pointer_type (TREE_TYPE (type)),
1916 TREE_OPERAND (exp, 0));
1918 if (TREE_CODE (exp) == COMPOUND_EXPR)
1920 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1921 if (op1 == error_mark_node)
1922 return error_mark_node;
1923 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1924 TREE_OPERAND (exp, 0), op1);
1927 if (!lvalue_p (exp)
1928 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1930 if (complain & tf_error)
1931 error_at (loc, "invalid use of non-lvalue array");
1932 return error_mark_node;
1935 /* Don't let an array compound literal decay to a pointer. It can
1936 still be used to initialize an array or bind to a reference. */
1937 if (TREE_CODE (exp) == TARGET_EXPR)
1939 if (complain & tf_error)
1940 error_at (loc, "taking address of temporary array");
1941 return error_mark_node;
1944 ptrtype = build_pointer_type (TREE_TYPE (type));
1946 if (TREE_CODE (exp) == VAR_DECL)
1948 if (!cxx_mark_addressable (exp))
1949 return error_mark_node;
1950 adr = build_nop (ptrtype, build_address (exp));
1951 return adr;
1953 /* This way is better for a COMPONENT_REF since it can
1954 simplify the offset for a component. */
1955 adr = cp_build_addr_expr (exp, complain);
1956 return cp_convert (ptrtype, adr, complain);
1959 /* If a bitfield is used in a context where integral promotion
1960 applies, then the caller is expected to have used
1961 default_conversion. That function promotes bitfields correctly
1962 before calling this function. At this point, if we have a
1963 bitfield referenced, we may assume that is not subject to
1964 promotion, and that, therefore, the type of the resulting rvalue
1965 is the declared type of the bitfield. */
1966 exp = convert_bitfield_to_declared_type (exp);
1968 /* We do not call rvalue() here because we do not want to wrap EXP
1969 in a NON_LVALUE_EXPR. */
1971 /* [basic.lval]
1973 Non-class rvalues always have cv-unqualified types. */
1974 type = TREE_TYPE (exp);
1975 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1976 exp = build_nop (cv_unqualified (type), exp);
1978 return exp;
1981 /* Perform preparatory conversions, as part of the "usual arithmetic
1982 conversions". In particular, as per [expr]:
1984 Whenever an lvalue expression appears as an operand of an
1985 operator that expects the rvalue for that operand, the
1986 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1987 standard conversions are applied to convert the expression to an
1988 rvalue.
1990 In addition, we perform integral promotions here, as those are
1991 applied to both operands to a binary operator before determining
1992 what additional conversions should apply. */
1994 static tree
1995 cp_default_conversion (tree exp, tsubst_flags_t complain)
1997 /* Check for target-specific promotions. */
1998 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1999 if (promoted_type)
2000 exp = cp_convert (promoted_type, exp, complain);
2001 /* Perform the integral promotions first so that bitfield
2002 expressions (which may promote to "int", even if the bitfield is
2003 declared "unsigned") are promoted correctly. */
2004 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2005 exp = cp_perform_integral_promotions (exp, complain);
2006 /* Perform the other conversions. */
2007 exp = decay_conversion (exp, complain);
2009 return exp;
2012 /* C version. */
2014 tree
2015 default_conversion (tree exp)
2017 return cp_default_conversion (exp, tf_warning_or_error);
2020 /* EXPR is an expression with an integral or enumeration type.
2021 Perform the integral promotions in [conv.prom], and return the
2022 converted value. */
2024 tree
2025 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2027 tree type;
2028 tree promoted_type;
2030 expr = mark_rvalue_use (expr);
2032 /* [conv.prom]
2034 If the bitfield has an enumerated type, it is treated as any
2035 other value of that type for promotion purposes. */
2036 type = is_bitfield_expr_with_lowered_type (expr);
2037 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2038 type = TREE_TYPE (expr);
2039 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2040 /* Scoped enums don't promote. */
2041 if (SCOPED_ENUM_P (type))
2042 return expr;
2043 promoted_type = type_promotes_to (type);
2044 if (type != promoted_type)
2045 expr = cp_convert (promoted_type, expr, complain);
2046 return expr;
2049 /* C version. */
2051 tree
2052 perform_integral_promotions (tree expr)
2054 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2057 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2058 decay_conversion to one. */
2061 string_conv_p (const_tree totype, const_tree exp, int warn)
2063 tree t;
2065 if (TREE_CODE (totype) != POINTER_TYPE)
2066 return 0;
2068 t = TREE_TYPE (totype);
2069 if (!same_type_p (t, char_type_node)
2070 && !same_type_p (t, char16_type_node)
2071 && !same_type_p (t, char32_type_node)
2072 && !same_type_p (t, wchar_type_node))
2073 return 0;
2075 if (TREE_CODE (exp) == STRING_CST)
2077 /* Make sure that we don't try to convert between char and wide chars. */
2078 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2079 return 0;
2081 else
2083 /* Is this a string constant which has decayed to 'const char *'? */
2084 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2085 if (!same_type_p (TREE_TYPE (exp), t))
2086 return 0;
2087 STRIP_NOPS (exp);
2088 if (TREE_CODE (exp) != ADDR_EXPR
2089 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2090 return 0;
2093 /* This warning is not very useful, as it complains about printf. */
2094 if (warn)
2095 warning (OPT_Wwrite_strings,
2096 "deprecated conversion from string constant to %qT",
2097 totype);
2099 return 1;
2102 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2103 can, for example, use as an lvalue. This code used to be in
2104 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2105 expressions, where we're dealing with aggregates. But now it's again only
2106 called from unary_complex_lvalue. The case (in particular) that led to
2107 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2108 get it there. */
2110 static tree
2111 rationalize_conditional_expr (enum tree_code code, tree t,
2112 tsubst_flags_t complain)
2114 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2115 the first operand is always the one to be used if both operands
2116 are equal, so we know what conditional expression this used to be. */
2117 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2119 tree op0 = TREE_OPERAND (t, 0);
2120 tree op1 = TREE_OPERAND (t, 1);
2122 /* The following code is incorrect if either operand side-effects. */
2123 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2124 && !TREE_SIDE_EFFECTS (op1));
2125 return
2126 build_conditional_expr (build_x_binary_op (input_location,
2127 (TREE_CODE (t) == MIN_EXPR
2128 ? LE_EXPR : GE_EXPR),
2129 op0, TREE_CODE (op0),
2130 op1, TREE_CODE (op1),
2131 /*overload=*/NULL,
2132 complain),
2133 cp_build_unary_op (code, op0, 0, complain),
2134 cp_build_unary_op (code, op1, 0, complain),
2135 complain);
2138 return
2139 build_conditional_expr (TREE_OPERAND (t, 0),
2140 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2141 complain),
2142 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2143 complain),
2144 complain);
2147 /* Given the TYPE of an anonymous union field inside T, return the
2148 FIELD_DECL for the field. If not found return NULL_TREE. Because
2149 anonymous unions can nest, we must also search all anonymous unions
2150 that are directly reachable. */
2152 tree
2153 lookup_anon_field (tree t, tree type)
2155 tree field;
2157 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2159 if (TREE_STATIC (field))
2160 continue;
2161 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2162 continue;
2164 /* If we find it directly, return the field. */
2165 if (DECL_NAME (field) == NULL_TREE
2166 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2168 return field;
2171 /* Otherwise, it could be nested, search harder. */
2172 if (DECL_NAME (field) == NULL_TREE
2173 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2175 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2176 if (subfield)
2177 return subfield;
2180 return NULL_TREE;
2183 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2184 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2185 non-NULL, it indicates the path to the base used to name MEMBER.
2186 If PRESERVE_REFERENCE is true, the expression returned will have
2187 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2188 returned will have the type referred to by the reference.
2190 This function does not perform access control; that is either done
2191 earlier by the parser when the name of MEMBER is resolved to MEMBER
2192 itself, or later when overload resolution selects one of the
2193 functions indicated by MEMBER. */
2195 tree
2196 build_class_member_access_expr (tree object, tree member,
2197 tree access_path, bool preserve_reference,
2198 tsubst_flags_t complain)
2200 tree object_type;
2201 tree member_scope;
2202 tree result = NULL_TREE;
2203 tree using_decl = NULL_TREE;
2205 if (error_operand_p (object) || error_operand_p (member))
2206 return error_mark_node;
2208 gcc_assert (DECL_P (member) || BASELINK_P (member));
2210 /* [expr.ref]
2212 The type of the first expression shall be "class object" (of a
2213 complete type). */
2214 object_type = TREE_TYPE (object);
2215 if (!currently_open_class (object_type)
2216 && !complete_type_or_maybe_complain (object_type, object, complain))
2217 return error_mark_node;
2218 if (!CLASS_TYPE_P (object_type))
2220 if (complain & tf_error)
2222 if (POINTER_TYPE_P (object_type)
2223 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2224 error ("request for member %qD in %qE, which is of pointer "
2225 "type %qT (maybe you meant to use %<->%> ?)",
2226 member, object, object_type);
2227 else
2228 error ("request for member %qD in %qE, which is of non-class "
2229 "type %qT", member, object, object_type);
2231 return error_mark_node;
2234 /* The standard does not seem to actually say that MEMBER must be a
2235 member of OBJECT_TYPE. However, that is clearly what is
2236 intended. */
2237 if (DECL_P (member))
2239 member_scope = DECL_CLASS_CONTEXT (member);
2240 mark_used (member);
2241 if (TREE_DEPRECATED (member))
2242 warn_deprecated_use (member, NULL_TREE);
2244 else
2245 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2246 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2247 presently be the anonymous union. Go outwards until we find a
2248 type related to OBJECT_TYPE. */
2249 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2250 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2251 object_type))
2252 member_scope = TYPE_CONTEXT (member_scope);
2253 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2255 if (complain & tf_error)
2257 if (TREE_CODE (member) == FIELD_DECL)
2258 error ("invalid use of nonstatic data member %qE", member);
2259 else
2260 error ("%qD is not a member of %qT", member, object_type);
2262 return error_mark_node;
2265 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2266 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2267 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2269 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2270 if (temp)
2271 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2274 /* In [expr.ref], there is an explicit list of the valid choices for
2275 MEMBER. We check for each of those cases here. */
2276 if (TREE_CODE (member) == VAR_DECL)
2278 /* A static data member. */
2279 result = member;
2280 mark_exp_read (object);
2281 /* If OBJECT has side-effects, they are supposed to occur. */
2282 if (TREE_SIDE_EFFECTS (object))
2283 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2285 else if (TREE_CODE (member) == FIELD_DECL)
2287 /* A non-static data member. */
2288 bool null_object_p;
2289 int type_quals;
2290 tree member_type;
2292 null_object_p = (TREE_CODE (object) == INDIRECT_REF
2293 && integer_zerop (TREE_OPERAND (object, 0)));
2295 /* Convert OBJECT to the type of MEMBER. */
2296 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2297 TYPE_MAIN_VARIANT (member_scope)))
2299 tree binfo;
2300 base_kind kind;
2302 binfo = lookup_base (access_path ? access_path : object_type,
2303 member_scope, ba_unique, &kind, complain);
2304 if (binfo == error_mark_node)
2305 return error_mark_node;
2307 /* It is invalid to try to get to a virtual base of a
2308 NULL object. The most common cause is invalid use of
2309 offsetof macro. */
2310 if (null_object_p && kind == bk_via_virtual)
2312 if (complain & tf_error)
2314 error ("invalid access to non-static data member %qD of "
2315 "NULL object",
2316 member);
2317 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2319 return error_mark_node;
2322 /* Convert to the base. */
2323 object = build_base_path (PLUS_EXPR, object, binfo,
2324 /*nonnull=*/1, complain);
2325 /* If we found the base successfully then we should be able
2326 to convert to it successfully. */
2327 gcc_assert (object != error_mark_node);
2330 /* Complain about other invalid uses of offsetof, even though they will
2331 give the right answer. Note that we complain whether or not they
2332 actually used the offsetof macro, since there's no way to know at this
2333 point. So we just give a warning, instead of a pedwarn. */
2334 /* Do not produce this warning for base class field references, because
2335 we know for a fact that didn't come from offsetof. This does occur
2336 in various testsuite cases where a null object is passed where a
2337 vtable access is required. */
2338 if (null_object_p && warn_invalid_offsetof
2339 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2340 && !DECL_FIELD_IS_BASE (member)
2341 && cp_unevaluated_operand == 0
2342 && (complain & tf_warning))
2344 warning (OPT_Winvalid_offsetof,
2345 "invalid access to non-static data member %qD "
2346 " of NULL object", member);
2347 warning (OPT_Winvalid_offsetof,
2348 "(perhaps the %<offsetof%> macro was used incorrectly)");
2351 /* If MEMBER is from an anonymous aggregate, we have converted
2352 OBJECT so that it refers to the class containing the
2353 anonymous union. Generate a reference to the anonymous union
2354 itself, and recur to find MEMBER. */
2355 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2356 /* When this code is called from build_field_call, the
2357 object already has the type of the anonymous union.
2358 That is because the COMPONENT_REF was already
2359 constructed, and was then disassembled before calling
2360 build_field_call. After the function-call code is
2361 cleaned up, this waste can be eliminated. */
2362 && (!same_type_ignoring_top_level_qualifiers_p
2363 (TREE_TYPE (object), DECL_CONTEXT (member))))
2365 tree anonymous_union;
2367 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2368 DECL_CONTEXT (member));
2369 object = build_class_member_access_expr (object,
2370 anonymous_union,
2371 /*access_path=*/NULL_TREE,
2372 preserve_reference,
2373 complain);
2376 /* Compute the type of the field, as described in [expr.ref]. */
2377 type_quals = TYPE_UNQUALIFIED;
2378 member_type = TREE_TYPE (member);
2379 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2381 type_quals = (cp_type_quals (member_type)
2382 | cp_type_quals (object_type));
2384 /* A field is const (volatile) if the enclosing object, or the
2385 field itself, is const (volatile). But, a mutable field is
2386 not const, even within a const object. */
2387 if (DECL_MUTABLE_P (member))
2388 type_quals &= ~TYPE_QUAL_CONST;
2389 member_type = cp_build_qualified_type (member_type, type_quals);
2392 result = build3 (COMPONENT_REF, member_type, object, member,
2393 NULL_TREE);
2394 result = fold_if_not_in_template (result);
2396 /* Mark the expression const or volatile, as appropriate. Even
2397 though we've dealt with the type above, we still have to mark the
2398 expression itself. */
2399 if (type_quals & TYPE_QUAL_CONST)
2400 TREE_READONLY (result) = 1;
2401 if (type_quals & TYPE_QUAL_VOLATILE)
2402 TREE_THIS_VOLATILE (result) = 1;
2404 else if (BASELINK_P (member))
2406 /* The member is a (possibly overloaded) member function. */
2407 tree functions;
2408 tree type;
2410 /* If the MEMBER is exactly one static member function, then we
2411 know the type of the expression. Otherwise, we must wait
2412 until overload resolution has been performed. */
2413 functions = BASELINK_FUNCTIONS (member);
2414 if (TREE_CODE (functions) == FUNCTION_DECL
2415 && DECL_STATIC_FUNCTION_P (functions))
2416 type = TREE_TYPE (functions);
2417 else
2418 type = unknown_type_node;
2419 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2420 base. That will happen when the function is called. */
2421 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2423 else if (TREE_CODE (member) == CONST_DECL)
2425 /* The member is an enumerator. */
2426 result = member;
2427 /* If OBJECT has side-effects, they are supposed to occur. */
2428 if (TREE_SIDE_EFFECTS (object))
2429 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2430 object, result);
2432 else if ((using_decl = strip_using_decl (member)) != member)
2433 result = build_class_member_access_expr (object,
2434 using_decl,
2435 access_path, preserve_reference,
2436 complain);
2437 else
2439 if (complain & tf_error)
2440 error ("invalid use of %qD", member);
2441 return error_mark_node;
2444 if (!preserve_reference)
2445 /* [expr.ref]
2447 If E2 is declared to have type "reference to T", then ... the
2448 type of E1.E2 is T. */
2449 result = convert_from_reference (result);
2451 return result;
2454 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2455 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2457 static tree
2458 lookup_destructor (tree object, tree scope, tree dtor_name)
2460 tree object_type = TREE_TYPE (object);
2461 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2462 tree expr;
2464 if (scope && !check_dtor_name (scope, dtor_type))
2466 error ("qualified type %qT does not match destructor name ~%qT",
2467 scope, dtor_type);
2468 return error_mark_node;
2470 if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2472 /* In a template, names we can't find a match for are still accepted
2473 destructor names, and we check them here. */
2474 if (check_dtor_name (object_type, dtor_type))
2475 dtor_type = object_type;
2476 else
2478 error ("object type %qT does not match destructor name ~%qT",
2479 object_type, dtor_type);
2480 return error_mark_node;
2484 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2486 error ("the type being destroyed is %qT, but the destructor refers to %qT",
2487 TYPE_MAIN_VARIANT (object_type), dtor_type);
2488 return error_mark_node;
2490 expr = lookup_member (dtor_type, complete_dtor_identifier,
2491 /*protect=*/1, /*want_type=*/false,
2492 tf_warning_or_error);
2493 expr = (adjust_result_of_qualified_name_lookup
2494 (expr, dtor_type, object_type));
2495 if (scope == NULL_TREE)
2496 /* We need to call adjust_result_of_qualified_name_lookup in case the
2497 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2498 that we still get virtual function binding. */
2499 BASELINK_QUALIFIED_P (expr) = false;
2500 return expr;
2503 /* An expression of the form "A::template B" has been resolved to
2504 DECL. Issue a diagnostic if B is not a template or template
2505 specialization. */
2507 void
2508 check_template_keyword (tree decl)
2510 /* The standard says:
2512 [temp.names]
2514 If a name prefixed by the keyword template is not a member
2515 template, the program is ill-formed.
2517 DR 228 removed the restriction that the template be a member
2518 template.
2520 DR 96, if accepted would add the further restriction that explicit
2521 template arguments must be provided if the template keyword is
2522 used, but, as of 2005-10-16, that DR is still in "drafting". If
2523 this DR is accepted, then the semantic checks here can be
2524 simplified, as the entity named must in fact be a template
2525 specialization, rather than, as at present, a set of overloaded
2526 functions containing at least one template function. */
2527 if (TREE_CODE (decl) != TEMPLATE_DECL
2528 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2530 if (!is_overloaded_fn (decl))
2531 permerror (input_location, "%qD is not a template", decl);
2532 else
2534 tree fns;
2535 fns = decl;
2536 if (BASELINK_P (fns))
2537 fns = BASELINK_FUNCTIONS (fns);
2538 while (fns)
2540 tree fn = OVL_CURRENT (fns);
2541 if (TREE_CODE (fn) == TEMPLATE_DECL
2542 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2543 break;
2544 if (TREE_CODE (fn) == FUNCTION_DECL
2545 && DECL_USE_TEMPLATE (fn)
2546 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2547 break;
2548 fns = OVL_NEXT (fns);
2550 if (!fns)
2551 permerror (input_location, "%qD is not a template", decl);
2556 /* This function is called by the parser to process a class member
2557 access expression of the form OBJECT.NAME. NAME is a node used by
2558 the parser to represent a name; it is not yet a DECL. It may,
2559 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2560 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2561 there is no reason to do the lookup twice, so the parser keeps the
2562 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2563 be a template via the use of the "A::template B" syntax. */
2565 tree
2566 finish_class_member_access_expr (tree object, tree name, bool template_p,
2567 tsubst_flags_t complain)
2569 tree expr;
2570 tree object_type;
2571 tree member;
2572 tree access_path = NULL_TREE;
2573 tree orig_object = object;
2574 tree orig_name = name;
2576 if (object == error_mark_node || name == error_mark_node)
2577 return error_mark_node;
2579 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2580 if (!objc_is_public (object, name))
2581 return error_mark_node;
2583 object_type = TREE_TYPE (object);
2585 if (processing_template_decl)
2587 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2588 dependent_type_p (object_type)
2589 /* If NAME is just an IDENTIFIER_NODE, then the expression
2590 is dependent. */
2591 || TREE_CODE (object) == IDENTIFIER_NODE
2592 /* If NAME is "f<args>", where either 'f' or 'args' is
2593 dependent, then the expression is dependent. */
2594 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2595 && dependent_template_id_p (TREE_OPERAND (name, 0),
2596 TREE_OPERAND (name, 1)))
2597 /* If NAME is "T::X" where "T" is dependent, then the
2598 expression is dependent. */
2599 || (TREE_CODE (name) == SCOPE_REF
2600 && TYPE_P (TREE_OPERAND (name, 0))
2601 && dependent_type_p (TREE_OPERAND (name, 0))))
2602 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2603 object, name, NULL_TREE);
2604 object = build_non_dependent_expr (object);
2606 else if (c_dialect_objc ()
2607 && TREE_CODE (name) == IDENTIFIER_NODE
2608 && (expr = objc_maybe_build_component_ref (object, name)))
2609 return expr;
2611 /* [expr.ref]
2613 The type of the first expression shall be "class object" (of a
2614 complete type). */
2615 if (!currently_open_class (object_type)
2616 && !complete_type_or_maybe_complain (object_type, object, complain))
2617 return error_mark_node;
2618 if (!CLASS_TYPE_P (object_type))
2620 if (complain & tf_error)
2622 if (POINTER_TYPE_P (object_type)
2623 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2624 error ("request for member %qD in %qE, which is of pointer "
2625 "type %qT (maybe you meant to use %<->%> ?)",
2626 name, object, object_type);
2627 else
2628 error ("request for member %qD in %qE, which is of non-class "
2629 "type %qT", name, object, object_type);
2631 return error_mark_node;
2634 if (BASELINK_P (name))
2635 /* A member function that has already been looked up. */
2636 member = name;
2637 else
2639 bool is_template_id = false;
2640 tree template_args = NULL_TREE;
2641 tree scope;
2643 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2645 is_template_id = true;
2646 template_args = TREE_OPERAND (name, 1);
2647 name = TREE_OPERAND (name, 0);
2649 if (TREE_CODE (name) == OVERLOAD)
2650 name = DECL_NAME (get_first_fn (name));
2651 else if (DECL_P (name))
2652 name = DECL_NAME (name);
2655 if (TREE_CODE (name) == SCOPE_REF)
2657 /* A qualified name. The qualifying class or namespace `S'
2658 has already been looked up; it is either a TYPE or a
2659 NAMESPACE_DECL. */
2660 scope = TREE_OPERAND (name, 0);
2661 name = TREE_OPERAND (name, 1);
2663 /* If SCOPE is a namespace, then the qualified name does not
2664 name a member of OBJECT_TYPE. */
2665 if (TREE_CODE (scope) == NAMESPACE_DECL)
2667 if (complain & tf_error)
2668 error ("%<%D::%D%> is not a member of %qT",
2669 scope, name, object_type);
2670 return error_mark_node;
2673 gcc_assert (CLASS_TYPE_P (scope));
2674 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2675 || TREE_CODE (name) == BIT_NOT_EXPR);
2677 if (constructor_name_p (name, scope))
2679 if (complain & tf_error)
2680 error ("cannot call constructor %<%T::%D%> directly",
2681 scope, name);
2682 return error_mark_node;
2685 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2686 access_path = lookup_base (object_type, scope, ba_check,
2687 NULL, complain);
2688 if (access_path == error_mark_node)
2689 return error_mark_node;
2690 if (!access_path)
2692 if (complain & tf_error)
2693 error ("%qT is not a base of %qT", scope, object_type);
2694 return error_mark_node;
2697 else
2699 scope = NULL_TREE;
2700 access_path = object_type;
2703 if (TREE_CODE (name) == BIT_NOT_EXPR)
2704 member = lookup_destructor (object, scope, name);
2705 else
2707 /* Look up the member. */
2708 member = lookup_member (access_path, name, /*protect=*/1,
2709 /*want_type=*/false, complain);
2710 if (member == NULL_TREE)
2712 if (complain & tf_error)
2713 error ("%qD has no member named %qE",
2714 TREE_CODE (access_path) == TREE_BINFO
2715 ? TREE_TYPE (access_path) : object_type, name);
2716 return error_mark_node;
2718 if (member == error_mark_node)
2719 return error_mark_node;
2722 if (is_template_id)
2724 tree templ = member;
2726 if (BASELINK_P (templ))
2727 templ = lookup_template_function (templ, template_args);
2728 else
2730 if (complain & tf_error)
2731 error ("%qD is not a member template function", name);
2732 return error_mark_node;
2737 if (TREE_DEPRECATED (member))
2738 warn_deprecated_use (member, NULL_TREE);
2740 if (template_p)
2741 check_template_keyword (member);
2743 expr = build_class_member_access_expr (object, member, access_path,
2744 /*preserve_reference=*/false,
2745 complain);
2746 if (processing_template_decl && expr != error_mark_node)
2748 if (BASELINK_P (member))
2750 if (TREE_CODE (orig_name) == SCOPE_REF)
2751 BASELINK_QUALIFIED_P (member) = 1;
2752 orig_name = member;
2754 return build_min_non_dep (COMPONENT_REF, expr,
2755 orig_object, orig_name,
2756 NULL_TREE);
2759 return expr;
2762 /* Return an expression for the MEMBER_NAME field in the internal
2763 representation of PTRMEM, a pointer-to-member function. (Each
2764 pointer-to-member function type gets its own RECORD_TYPE so it is
2765 more convenient to access the fields by name than by FIELD_DECL.)
2766 This routine converts the NAME to a FIELD_DECL and then creates the
2767 node for the complete expression. */
2769 tree
2770 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2772 tree ptrmem_type;
2773 tree member;
2774 tree member_type;
2776 /* This code is a stripped down version of
2777 build_class_member_access_expr. It does not work to use that
2778 routine directly because it expects the object to be of class
2779 type. */
2780 ptrmem_type = TREE_TYPE (ptrmem);
2781 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2782 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2783 /*want_type=*/false, tf_warning_or_error);
2784 member_type = cp_build_qualified_type (TREE_TYPE (member),
2785 cp_type_quals (ptrmem_type));
2786 return fold_build3_loc (input_location,
2787 COMPONENT_REF, member_type,
2788 ptrmem, member, NULL_TREE);
2791 /* Given an expression PTR for a pointer, return an expression
2792 for the value pointed to.
2793 ERRORSTRING is the name of the operator to appear in error messages.
2795 This function may need to overload OPERATOR_FNNAME.
2796 Must also handle REFERENCE_TYPEs for C++. */
2798 tree
2799 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2800 tsubst_flags_t complain)
2802 tree orig_expr = expr;
2803 tree rval;
2805 if (processing_template_decl)
2807 /* Retain the type if we know the operand is a pointer. */
2808 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2809 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2810 if (type_dependent_expression_p (expr))
2811 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2812 expr = build_non_dependent_expr (expr);
2815 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2816 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2817 if (!rval)
2818 rval = cp_build_indirect_ref (expr, errorstring, complain);
2820 if (processing_template_decl && rval != error_mark_node)
2821 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2822 else
2823 return rval;
2826 /* Helper function called from c-common. */
2827 tree
2828 build_indirect_ref (location_t /*loc*/,
2829 tree ptr, ref_operator errorstring)
2831 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2834 tree
2835 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2836 tsubst_flags_t complain)
2838 tree pointer, type;
2840 if (ptr == current_class_ptr)
2841 return current_class_ref;
2843 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2844 ? ptr : decay_conversion (ptr, complain));
2845 if (pointer == error_mark_node)
2846 return error_mark_node;
2848 type = TREE_TYPE (pointer);
2850 if (POINTER_TYPE_P (type))
2852 /* [expr.unary.op]
2854 If the type of the expression is "pointer to T," the type
2855 of the result is "T." */
2856 tree t = TREE_TYPE (type);
2858 if (CONVERT_EXPR_P (ptr)
2859 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2861 /* If a warning is issued, mark it to avoid duplicates from
2862 the backend. This only needs to be done at
2863 warn_strict_aliasing > 2. */
2864 if (warn_strict_aliasing > 2)
2865 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2866 type, TREE_OPERAND (ptr, 0)))
2867 TREE_NO_WARNING (ptr) = 1;
2870 if (VOID_TYPE_P (t))
2872 /* A pointer to incomplete type (other than cv void) can be
2873 dereferenced [expr.unary.op]/1 */
2874 if (complain & tf_error)
2875 error ("%qT is not a pointer-to-object type", type);
2876 return error_mark_node;
2878 else if (TREE_CODE (pointer) == ADDR_EXPR
2879 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2880 /* The POINTER was something like `&x'. We simplify `*&x' to
2881 `x'. */
2882 return TREE_OPERAND (pointer, 0);
2883 else
2885 tree ref = build1 (INDIRECT_REF, t, pointer);
2887 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2888 so that we get the proper error message if the result is used
2889 to assign to. Also, &* is supposed to be a no-op. */
2890 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2891 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2892 TREE_SIDE_EFFECTS (ref)
2893 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2894 return ref;
2897 else if (!(complain & tf_error))
2898 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2900 /* `pointer' won't be an error_mark_node if we were given a
2901 pointer to member, so it's cool to check for this here. */
2902 else if (TYPE_PTRMEM_P (type))
2903 switch (errorstring)
2905 case RO_ARRAY_INDEXING:
2906 error ("invalid use of array indexing on pointer to member");
2907 break;
2908 case RO_UNARY_STAR:
2909 error ("invalid use of unary %<*%> on pointer to member");
2910 break;
2911 case RO_IMPLICIT_CONVERSION:
2912 error ("invalid use of implicit conversion on pointer to member");
2913 break;
2914 case RO_ARROW_STAR:
2915 error ("left hand operand of %<->*%> must be a pointer to class, "
2916 "but is a pointer to member of type %qT", type);
2917 break;
2918 default:
2919 gcc_unreachable ();
2921 else if (pointer != error_mark_node)
2922 invalid_indirection_error (input_location, type, errorstring);
2924 return error_mark_node;
2927 /* This handles expressions of the form "a[i]", which denotes
2928 an array reference.
2930 This is logically equivalent in C to *(a+i), but we may do it differently.
2931 If A is a variable or a member, we generate a primitive ARRAY_REF.
2932 This avoids forcing the array out of registers, and can work on
2933 arrays that are not lvalues (for example, members of structures returned
2934 by functions).
2936 If INDEX is of some user-defined type, it must be converted to
2937 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2938 will inherit the type of the array, which will be some pointer type.
2940 LOC is the location to use in building the array reference. */
2942 tree
2943 cp_build_array_ref (location_t loc, tree array, tree idx,
2944 tsubst_flags_t complain)
2946 tree ret;
2948 if (idx == 0)
2950 if (complain & tf_error)
2951 error_at (loc, "subscript missing in array reference");
2952 return error_mark_node;
2955 if (TREE_TYPE (array) == error_mark_node
2956 || TREE_TYPE (idx) == error_mark_node)
2957 return error_mark_node;
2959 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2960 inside it. */
2961 switch (TREE_CODE (array))
2963 case COMPOUND_EXPR:
2965 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2966 complain);
2967 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2968 TREE_OPERAND (array, 0), value);
2969 SET_EXPR_LOCATION (ret, loc);
2970 return ret;
2973 case COND_EXPR:
2974 ret = build_conditional_expr
2975 (TREE_OPERAND (array, 0),
2976 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2977 complain),
2978 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2979 complain),
2980 complain);
2981 protected_set_expr_location (ret, loc);
2982 return ret;
2984 default:
2985 break;
2988 convert_vector_to_pointer_for_subscript (loc, &array, idx);
2990 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2992 tree rval, type;
2994 warn_array_subscript_with_type_char (idx);
2996 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2998 if (complain & tf_error)
2999 error_at (loc, "array subscript is not an integer");
3000 return error_mark_node;
3003 /* Apply integral promotions *after* noticing character types.
3004 (It is unclear why we do these promotions -- the standard
3005 does not say that we should. In fact, the natural thing would
3006 seem to be to convert IDX to ptrdiff_t; we're performing
3007 pointer arithmetic.) */
3008 idx = cp_perform_integral_promotions (idx, complain);
3010 /* An array that is indexed by a non-constant
3011 cannot be stored in a register; we must be able to do
3012 address arithmetic on its address.
3013 Likewise an array of elements of variable size. */
3014 if (TREE_CODE (idx) != INTEGER_CST
3015 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3016 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3017 != INTEGER_CST)))
3019 if (!cxx_mark_addressable (array))
3020 return error_mark_node;
3023 /* An array that is indexed by a constant value which is not within
3024 the array bounds cannot be stored in a register either; because we
3025 would get a crash in store_bit_field/extract_bit_field when trying
3026 to access a non-existent part of the register. */
3027 if (TREE_CODE (idx) == INTEGER_CST
3028 && TYPE_DOMAIN (TREE_TYPE (array))
3029 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3031 if (!cxx_mark_addressable (array))
3032 return error_mark_node;
3035 if (!lvalue_p (array) && (complain & tf_error))
3036 pedwarn (loc, OPT_Wpedantic,
3037 "ISO C++ forbids subscripting non-lvalue array");
3039 /* Note in C++ it is valid to subscript a `register' array, since
3040 it is valid to take the address of something with that
3041 storage specification. */
3042 if (extra_warnings)
3044 tree foo = array;
3045 while (TREE_CODE (foo) == COMPONENT_REF)
3046 foo = TREE_OPERAND (foo, 0);
3047 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
3048 && (complain & tf_warning))
3049 warning_at (loc, OPT_Wextra,
3050 "subscripting array declared %<register%>");
3053 type = TREE_TYPE (TREE_TYPE (array));
3054 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3055 /* Array ref is const/volatile if the array elements are
3056 or if the array is.. */
3057 TREE_READONLY (rval)
3058 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3059 TREE_SIDE_EFFECTS (rval)
3060 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3061 TREE_THIS_VOLATILE (rval)
3062 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3063 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3064 complain);
3065 protected_set_expr_location (ret, loc);
3066 return ret;
3070 tree ar = cp_default_conversion (array, complain);
3071 tree ind = cp_default_conversion (idx, complain);
3073 /* Put the integer in IND to simplify error checking. */
3074 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3076 tree temp = ar;
3077 ar = ind;
3078 ind = temp;
3081 if (ar == error_mark_node || ind == error_mark_node)
3082 return error_mark_node;
3084 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
3086 if (complain & tf_error)
3087 error_at (loc, "subscripted value is neither array nor pointer");
3088 return error_mark_node;
3090 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3092 if (complain & tf_error)
3093 error_at (loc, "array subscript is not an integer");
3094 return error_mark_node;
3097 warn_array_subscript_with_type_char (idx);
3099 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3100 PLUS_EXPR, ar, ind,
3101 complain),
3102 RO_ARRAY_INDEXING,
3103 complain);
3104 protected_set_expr_location (ret, loc);
3105 return ret;
3109 /* Entry point for Obj-C++. */
3111 tree
3112 build_array_ref (location_t loc, tree array, tree idx)
3114 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3117 /* Resolve a pointer to member function. INSTANCE is the object
3118 instance to use, if the member points to a virtual member.
3120 This used to avoid checking for virtual functions if basetype
3121 has no virtual functions, according to an earlier ANSI draft.
3122 With the final ISO C++ rules, such an optimization is
3123 incorrect: A pointer to a derived member can be static_cast
3124 to pointer-to-base-member, as long as the dynamic object
3125 later has the right member. So now we only do this optimization
3126 when we know the dynamic type of the object. */
3128 tree
3129 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3130 tsubst_flags_t complain)
3132 if (TREE_CODE (function) == OFFSET_REF)
3133 function = TREE_OPERAND (function, 1);
3135 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3137 tree idx, delta, e1, e2, e3, vtbl;
3138 bool nonvirtual;
3139 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3140 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3142 tree instance_ptr = *instance_ptrptr;
3143 tree instance_save_expr = 0;
3144 if (instance_ptr == error_mark_node)
3146 if (TREE_CODE (function) == PTRMEM_CST)
3148 /* Extracting the function address from a pmf is only
3149 allowed with -Wno-pmf-conversions. It only works for
3150 pmf constants. */
3151 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3152 e1 = convert (fntype, e1);
3153 return e1;
3155 else
3157 if (complain & tf_error)
3158 error ("object missing in use of %qE", function);
3159 return error_mark_node;
3163 /* True if we know that the dynamic type of the object doesn't have
3164 virtual functions, so we can assume the PFN field is a pointer. */
3165 nonvirtual = (COMPLETE_TYPE_P (basetype)
3166 && !TYPE_POLYMORPHIC_P (basetype)
3167 && resolves_to_fixed_type_p (instance_ptr, 0));
3169 if (TREE_SIDE_EFFECTS (instance_ptr))
3170 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3172 if (TREE_SIDE_EFFECTS (function))
3173 function = save_expr (function);
3175 /* Start by extracting all the information from the PMF itself. */
3176 e3 = pfn_from_ptrmemfunc (function);
3177 delta = delta_from_ptrmemfunc (function);
3178 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3179 if (nonvirtual)
3180 e1 = integer_zero_node;
3181 else switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3183 case ptrmemfunc_vbit_in_pfn:
3184 e1 = cp_build_binary_op (input_location,
3185 BIT_AND_EXPR, idx, integer_one_node,
3186 complain);
3187 idx = cp_build_binary_op (input_location,
3188 MINUS_EXPR, idx, integer_one_node,
3189 complain);
3190 if (idx == error_mark_node)
3191 return error_mark_node;
3192 break;
3194 case ptrmemfunc_vbit_in_delta:
3195 e1 = cp_build_binary_op (input_location,
3196 BIT_AND_EXPR, delta, integer_one_node,
3197 complain);
3198 delta = cp_build_binary_op (input_location,
3199 RSHIFT_EXPR, delta, integer_one_node,
3200 complain);
3201 if (delta == error_mark_node)
3202 return error_mark_node;
3203 break;
3205 default:
3206 gcc_unreachable ();
3209 if (e1 == error_mark_node)
3210 return error_mark_node;
3212 /* Convert down to the right base before using the instance. A
3213 special case is that in a pointer to member of class C, C may
3214 be incomplete. In that case, the function will of course be
3215 a member of C, and no conversion is required. In fact,
3216 lookup_base will fail in that case, because incomplete
3217 classes do not have BINFOs. */
3218 if (!same_type_ignoring_top_level_qualifiers_p
3219 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3221 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3222 basetype, ba_check, NULL, complain);
3223 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3224 1, complain);
3225 if (instance_ptr == error_mark_node)
3226 return error_mark_node;
3228 /* ...and then the delta in the PMF. */
3229 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3231 /* Hand back the adjusted 'this' argument to our caller. */
3232 *instance_ptrptr = instance_ptr;
3234 if (nonvirtual)
3235 /* Now just return the pointer. */
3236 return e3;
3238 /* Next extract the vtable pointer from the object. */
3239 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3240 instance_ptr);
3241 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3242 if (vtbl == error_mark_node)
3243 return error_mark_node;
3245 /* Finally, extract the function pointer from the vtable. */
3246 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3247 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3248 if (e2 == error_mark_node)
3249 return error_mark_node;
3250 TREE_CONSTANT (e2) = 1;
3252 /* When using function descriptors, the address of the
3253 vtable entry is treated as a function pointer. */
3254 if (TARGET_VTABLE_USES_DESCRIPTORS)
3255 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3256 cp_build_addr_expr (e2, complain));
3258 e2 = fold_convert (TREE_TYPE (e3), e2);
3259 e1 = build_conditional_expr (e1, e2, e3, complain);
3260 if (e1 == error_mark_node)
3261 return error_mark_node;
3263 /* Make sure this doesn't get evaluated first inside one of the
3264 branches of the COND_EXPR. */
3265 if (instance_save_expr)
3266 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3267 instance_save_expr, e1);
3269 function = e1;
3271 return function;
3274 /* Used by the C-common bits. */
3275 tree
3276 build_function_call (location_t /*loc*/,
3277 tree function, tree params)
3279 return cp_build_function_call (function, params, tf_warning_or_error);
3282 /* Used by the C-common bits. */
3283 tree
3284 build_function_call_vec (location_t /*loc*/,
3285 tree function, vec<tree, va_gc> *params,
3286 vec<tree, va_gc> * /*origtypes*/)
3288 vec<tree, va_gc> *orig_params = params;
3289 tree ret = cp_build_function_call_vec (function, &params,
3290 tf_warning_or_error);
3292 /* cp_build_function_call_vec can reallocate PARAMS by adding
3293 default arguments. That should never happen here. Verify
3294 that. */
3295 gcc_assert (params == orig_params);
3297 return ret;
3300 /* Build a function call using a tree list of arguments. */
3302 tree
3303 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3305 vec<tree, va_gc> *vec;
3306 tree ret;
3308 vec = make_tree_vector ();
3309 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3310 vec_safe_push (vec, TREE_VALUE (params));
3311 ret = cp_build_function_call_vec (function, &vec, complain);
3312 release_tree_vector (vec);
3313 return ret;
3316 /* Build a function call using varargs. */
3318 tree
3319 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3321 vec<tree, va_gc> *vec;
3322 va_list args;
3323 tree ret, t;
3325 vec = make_tree_vector ();
3326 va_start (args, complain);
3327 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3328 vec_safe_push (vec, t);
3329 va_end (args);
3330 ret = cp_build_function_call_vec (function, &vec, complain);
3331 release_tree_vector (vec);
3332 return ret;
3335 /* Build a function call using a vector of arguments. PARAMS may be
3336 NULL if there are no parameters. This changes the contents of
3337 PARAMS. */
3339 tree
3340 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3341 tsubst_flags_t complain)
3343 tree fntype, fndecl;
3344 int is_method;
3345 tree original = function;
3346 int nargs;
3347 tree *argarray;
3348 tree parm_types;
3349 vec<tree, va_gc> *allocated = NULL;
3350 tree ret;
3352 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3353 expressions, like those used for ObjC messenger dispatches. */
3354 if (params != NULL && !vec_safe_is_empty (*params))
3355 function = objc_rewrite_function_call (function, (**params)[0]);
3357 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3358 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3359 if (TREE_CODE (function) == NOP_EXPR
3360 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3361 function = TREE_OPERAND (function, 0);
3363 if (TREE_CODE (function) == FUNCTION_DECL)
3365 mark_used (function);
3366 fndecl = function;
3368 /* Convert anything with function type to a pointer-to-function. */
3369 if (DECL_MAIN_P (function) && (complain & tf_error))
3370 pedwarn (input_location, OPT_Wpedantic,
3371 "ISO C++ forbids calling %<::main%> from within program");
3373 function = build_addr_func (function, complain);
3375 else
3377 fndecl = NULL_TREE;
3379 function = build_addr_func (function, complain);
3382 if (function == error_mark_node)
3383 return error_mark_node;
3385 fntype = TREE_TYPE (function);
3387 if (TYPE_PTRMEMFUNC_P (fntype))
3389 if (complain & tf_error)
3390 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3391 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3392 original, original);
3393 return error_mark_node;
3396 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3397 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3399 if (!((TREE_CODE (fntype) == POINTER_TYPE
3400 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3401 || is_method
3402 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3404 if (complain & tf_error)
3406 if (!flag_diagnostics_show_caret)
3407 error_at (input_location,
3408 "%qE cannot be used as a function", original);
3409 else if (DECL_P (original))
3410 error_at (input_location,
3411 "%qD cannot be used as a function", original);
3412 else
3413 error_at (input_location,
3414 "expression cannot be used as a function");
3417 return error_mark_node;
3420 /* fntype now gets the type of function pointed to. */
3421 fntype = TREE_TYPE (fntype);
3422 parm_types = TYPE_ARG_TYPES (fntype);
3424 if (params == NULL)
3426 allocated = make_tree_vector ();
3427 params = &allocated;
3430 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3431 complain);
3432 if (nargs < 0)
3433 return error_mark_node;
3435 argarray = (*params)->address ();
3437 /* Check for errors in format strings and inappropriately
3438 null parameters. */
3439 check_function_arguments (fntype, nargs, argarray);
3441 ret = build_cxx_call (function, nargs, argarray, complain);
3443 if (allocated != NULL)
3444 release_tree_vector (allocated);
3446 return ret;
3449 /* Subroutine of convert_arguments.
3450 Warn about wrong number of args are genereted. */
3452 static void
3453 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3455 if (fndecl)
3457 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3459 if (DECL_NAME (fndecl) == NULL_TREE
3460 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3461 error_at (loc,
3462 too_many_p
3463 ? G_("too many arguments to constructor %q#D")
3464 : G_("too few arguments to constructor %q#D"),
3465 fndecl);
3466 else
3467 error_at (loc,
3468 too_many_p
3469 ? G_("too many arguments to member function %q#D")
3470 : G_("too few arguments to member function %q#D"),
3471 fndecl);
3473 else
3474 error_at (loc,
3475 too_many_p
3476 ? G_("too many arguments to function %q#D")
3477 : G_("too few arguments to function %q#D"),
3478 fndecl);
3479 inform (DECL_SOURCE_LOCATION (fndecl),
3480 "declared here");
3482 else
3484 if (c_dialect_objc () && objc_message_selector ())
3485 error_at (loc,
3486 too_many_p
3487 ? G_("too many arguments to method %q#D")
3488 : G_("too few arguments to method %q#D"),
3489 objc_message_selector ());
3490 else
3491 error_at (loc, too_many_p ? G_("too many arguments to function")
3492 : G_("too few arguments to function"));
3496 /* Convert the actual parameter expressions in the list VALUES to the
3497 types in the list TYPELIST. The converted expressions are stored
3498 back in the VALUES vector.
3499 If parmdecls is exhausted, or when an element has NULL as its type,
3500 perform the default conversions.
3502 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3504 This is also where warnings about wrong number of args are generated.
3506 Returns the actual number of arguments processed (which might be less
3507 than the length of the vector), or -1 on error.
3509 In C++, unspecified trailing parameters can be filled in with their
3510 default arguments, if such were specified. Do so here. */
3512 static int
3513 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3514 int flags, tsubst_flags_t complain)
3516 tree typetail;
3517 unsigned int i;
3519 /* Argument passing is always copy-initialization. */
3520 flags |= LOOKUP_ONLYCONVERTING;
3522 for (i = 0, typetail = typelist;
3523 i < vec_safe_length (*values);
3524 i++)
3526 tree type = typetail ? TREE_VALUE (typetail) : 0;
3527 tree val = (**values)[i];
3529 if (val == error_mark_node || type == error_mark_node)
3530 return -1;
3532 if (type == void_type_node)
3534 if (complain & tf_error)
3536 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3537 return i;
3539 else
3540 return -1;
3543 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3544 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3545 if (TREE_CODE (val) == NOP_EXPR
3546 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3547 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3548 val = TREE_OPERAND (val, 0);
3550 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3552 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3553 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3554 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3555 val = decay_conversion (val, complain);
3558 if (val == error_mark_node)
3559 return -1;
3561 if (type != 0)
3563 /* Formal parm type is specified by a function prototype. */
3564 tree parmval;
3566 if (!COMPLETE_TYPE_P (complete_type (type)))
3568 if (complain & tf_error)
3570 if (fndecl)
3571 error ("parameter %P of %qD has incomplete type %qT",
3572 i, fndecl, type);
3573 else
3574 error ("parameter %P has incomplete type %qT", i, type);
3576 parmval = error_mark_node;
3578 else
3580 parmval = convert_for_initialization
3581 (NULL_TREE, type, val, flags,
3582 ICR_ARGPASS, fndecl, i, complain);
3583 parmval = convert_for_arg_passing (type, parmval, complain);
3586 if (parmval == error_mark_node)
3587 return -1;
3589 (**values)[i] = parmval;
3591 else
3593 if (fndecl && DECL_BUILT_IN (fndecl)
3594 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3595 /* Don't do ellipsis conversion for __built_in_constant_p
3596 as this will result in spurious errors for non-trivial
3597 types. */
3598 val = require_complete_type_sfinae (val, complain);
3599 else
3600 val = convert_arg_to_ellipsis (val, complain);
3602 (**values)[i] = val;
3605 if (typetail)
3606 typetail = TREE_CHAIN (typetail);
3609 if (typetail != 0 && typetail != void_list_node)
3611 /* See if there are default arguments that can be used. Because
3612 we hold default arguments in the FUNCTION_TYPE (which is so
3613 wrong), we can see default parameters here from deduced
3614 contexts (and via typeof) for indirect function calls.
3615 Fortunately we know whether we have a function decl to
3616 provide default arguments in a language conformant
3617 manner. */
3618 if (fndecl && TREE_PURPOSE (typetail)
3619 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3621 for (; typetail != void_list_node; ++i)
3623 tree parmval
3624 = convert_default_arg (TREE_VALUE (typetail),
3625 TREE_PURPOSE (typetail),
3626 fndecl, i, complain);
3628 if (parmval == error_mark_node)
3629 return -1;
3631 vec_safe_push (*values, parmval);
3632 typetail = TREE_CHAIN (typetail);
3633 /* ends with `...'. */
3634 if (typetail == NULL_TREE)
3635 break;
3638 else
3640 if (complain & tf_error)
3641 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3642 return -1;
3646 return (int) i;
3649 /* Build a binary-operation expression, after performing default
3650 conversions on the operands. CODE is the kind of expression to
3651 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3652 are the tree codes which correspond to ARG1 and ARG2 when issuing
3653 warnings about possibly misplaced parentheses. They may differ
3654 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3655 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3656 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3657 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3658 ARG2_CODE as ERROR_MARK. */
3660 tree
3661 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3662 enum tree_code arg1_code, tree arg2,
3663 enum tree_code arg2_code, tree *overload,
3664 tsubst_flags_t complain)
3666 tree orig_arg1;
3667 tree orig_arg2;
3668 tree expr;
3670 orig_arg1 = arg1;
3671 orig_arg2 = arg2;
3673 if (processing_template_decl)
3675 if (type_dependent_expression_p (arg1)
3676 || type_dependent_expression_p (arg2))
3677 return build_min_nt_loc (loc, code, arg1, arg2);
3678 arg1 = build_non_dependent_expr (arg1);
3679 arg2 = build_non_dependent_expr (arg2);
3682 if (code == DOTSTAR_EXPR)
3683 expr = build_m_component_ref (arg1, arg2, complain);
3684 else
3685 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3686 overload, complain);
3688 /* Check for cases such as x+y<<z which users are likely to
3689 misinterpret. But don't warn about obj << x + y, since that is a
3690 common idiom for I/O. */
3691 if (warn_parentheses
3692 && (complain & tf_warning)
3693 && !processing_template_decl
3694 && !error_operand_p (arg1)
3695 && !error_operand_p (arg2)
3696 && (code != LSHIFT_EXPR
3697 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3698 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3699 arg2_code, orig_arg2);
3701 if (processing_template_decl && expr != error_mark_node)
3702 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3704 return expr;
3707 /* Build and return an ARRAY_REF expression. */
3709 tree
3710 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3711 tsubst_flags_t complain)
3713 tree orig_arg1 = arg1;
3714 tree orig_arg2 = arg2;
3715 tree expr;
3717 if (processing_template_decl)
3719 if (type_dependent_expression_p (arg1)
3720 || type_dependent_expression_p (arg2))
3721 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3722 NULL_TREE, NULL_TREE);
3723 arg1 = build_non_dependent_expr (arg1);
3724 arg2 = build_non_dependent_expr (arg2);
3727 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3728 NULL_TREE, /*overload=*/NULL, complain);
3730 if (processing_template_decl && expr != error_mark_node)
3731 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3732 NULL_TREE, NULL_TREE);
3733 return expr;
3736 /* Return whether OP is an expression of enum type cast to integer
3737 type. In C++ even unsigned enum types are cast to signed integer
3738 types. We do not want to issue warnings about comparisons between
3739 signed and unsigned types when one of the types is an enum type.
3740 Those warnings are always false positives in practice. */
3742 static bool
3743 enum_cast_to_int (tree op)
3745 if (TREE_CODE (op) == NOP_EXPR
3746 && TREE_TYPE (op) == integer_type_node
3747 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3748 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3749 return true;
3751 /* The cast may have been pushed into a COND_EXPR. */
3752 if (TREE_CODE (op) == COND_EXPR)
3753 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3754 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3756 return false;
3759 /* For the c-common bits. */
3760 tree
3761 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3762 int /*convert_p*/)
3764 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3768 /* Build a binary-operation expression without default conversions.
3769 CODE is the kind of expression to build.
3770 LOCATION is the location_t of the operator in the source code.
3771 This function differs from `build' in several ways:
3772 the data type of the result is computed and recorded in it,
3773 warnings are generated if arg data types are invalid,
3774 special handling for addition and subtraction of pointers is known,
3775 and some optimization is done (operations on narrow ints
3776 are done in the narrower type when that gives the same result).
3777 Constant folding is also done before the result is returned.
3779 Note that the operands will never have enumeral types
3780 because either they have just had the default conversions performed
3781 or they have both just been converted to some other type in which
3782 the arithmetic is to be done.
3784 C++: must do special pointer arithmetic when implementing
3785 multiple inheritance, and deal with pointer to member functions. */
3787 tree
3788 cp_build_binary_op (location_t location,
3789 enum tree_code code, tree orig_op0, tree orig_op1,
3790 tsubst_flags_t complain)
3792 tree op0, op1;
3793 enum tree_code code0, code1;
3794 tree type0, type1;
3795 const char *invalid_op_diag;
3797 /* Expression code to give to the expression when it is built.
3798 Normally this is CODE, which is what the caller asked for,
3799 but in some special cases we change it. */
3800 enum tree_code resultcode = code;
3802 /* Data type in which the computation is to be performed.
3803 In the simplest cases this is the common type of the arguments. */
3804 tree result_type = NULL;
3806 /* Nonzero means operands have already been type-converted
3807 in whatever way is necessary.
3808 Zero means they need to be converted to RESULT_TYPE. */
3809 int converted = 0;
3811 /* Nonzero means create the expression with this type, rather than
3812 RESULT_TYPE. */
3813 tree build_type = 0;
3815 /* Nonzero means after finally constructing the expression
3816 convert it to this type. */
3817 tree final_type = 0;
3819 tree result;
3821 /* Nonzero if this is an operation like MIN or MAX which can
3822 safely be computed in short if both args are promoted shorts.
3823 Also implies COMMON.
3824 -1 indicates a bitwise operation; this makes a difference
3825 in the exact conditions for when it is safe to do the operation
3826 in a narrower mode. */
3827 int shorten = 0;
3829 /* Nonzero if this is a comparison operation;
3830 if both args are promoted shorts, compare the original shorts.
3831 Also implies COMMON. */
3832 int short_compare = 0;
3834 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3835 int common = 0;
3837 /* True if both operands have arithmetic type. */
3838 bool arithmetic_types_p;
3840 /* Apply default conversions. */
3841 op0 = orig_op0;
3842 op1 = orig_op1;
3844 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3845 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3846 || code == TRUTH_XOR_EXPR)
3848 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3849 op0 = decay_conversion (op0, complain);
3850 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3851 op1 = decay_conversion (op1, complain);
3853 else
3855 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3856 op0 = cp_default_conversion (op0, complain);
3857 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3858 op1 = cp_default_conversion (op1, complain);
3861 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3862 STRIP_TYPE_NOPS (op0);
3863 STRIP_TYPE_NOPS (op1);
3865 /* DTRT if one side is an overloaded function, but complain about it. */
3866 if (type_unknown_p (op0))
3868 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3869 if (t != error_mark_node)
3871 if (complain & tf_error)
3872 permerror (input_location, "assuming cast to type %qT from overloaded function",
3873 TREE_TYPE (t));
3874 op0 = t;
3877 if (type_unknown_p (op1))
3879 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3880 if (t != error_mark_node)
3882 if (complain & tf_error)
3883 permerror (input_location, "assuming cast to type %qT from overloaded function",
3884 TREE_TYPE (t));
3885 op1 = t;
3889 type0 = TREE_TYPE (op0);
3890 type1 = TREE_TYPE (op1);
3892 /* The expression codes of the data types of the arguments tell us
3893 whether the arguments are integers, floating, pointers, etc. */
3894 code0 = TREE_CODE (type0);
3895 code1 = TREE_CODE (type1);
3897 /* If an error was already reported for one of the arguments,
3898 avoid reporting another error. */
3899 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3900 return error_mark_node;
3902 if ((invalid_op_diag
3903 = targetm.invalid_binary_op (code, type0, type1)))
3905 error (invalid_op_diag);
3906 return error_mark_node;
3909 /* Issue warnings about peculiar, but valid, uses of NULL. */
3910 if ((orig_op0 == null_node || orig_op1 == null_node)
3911 /* It's reasonable to use pointer values as operands of &&
3912 and ||, so NULL is no exception. */
3913 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3914 && ( /* Both are NULL (or 0) and the operation was not a
3915 comparison or a pointer subtraction. */
3916 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3917 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3918 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3919 || (!null_ptr_cst_p (orig_op0)
3920 && !TYPE_PTR_OR_PTRMEM_P (type0))
3921 || (!null_ptr_cst_p (orig_op1)
3922 && !TYPE_PTR_OR_PTRMEM_P (type1)))
3923 && (complain & tf_warning))
3925 source_location loc =
3926 expansion_point_location_if_in_system_header (input_location);
3928 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
3931 /* In case when one of the operands of the binary operation is
3932 a vector and another is a scalar -- convert scalar to vector. */
3933 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
3935 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
3936 complain & tf_error);
3938 switch (convert_flag)
3940 case stv_error:
3941 return error_mark_node;
3942 case stv_firstarg:
3944 op0 = convert (TREE_TYPE (type1), op0);
3945 op0 = build_vector_from_val (type1, op0);
3946 type0 = TREE_TYPE (op0);
3947 code0 = TREE_CODE (type0);
3948 converted = 1;
3949 break;
3951 case stv_secondarg:
3953 op1 = convert (TREE_TYPE (type0), op1);
3954 op1 = build_vector_from_val (type0, op1);
3955 type1 = TREE_TYPE (op1);
3956 code1 = TREE_CODE (type1);
3957 converted = 1;
3958 break;
3960 default:
3961 break;
3965 switch (code)
3967 case MINUS_EXPR:
3968 /* Subtraction of two similar pointers.
3969 We must subtract them as integers, then divide by object size. */
3970 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3971 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3972 TREE_TYPE (type1)))
3973 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
3974 complain);
3975 /* In all other cases except pointer - int, the usual arithmetic
3976 rules apply. */
3977 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3979 common = 1;
3980 break;
3982 /* The pointer - int case is just like pointer + int; fall
3983 through. */
3984 case PLUS_EXPR:
3985 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3986 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3988 tree ptr_operand;
3989 tree int_operand;
3990 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3991 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3992 if (processing_template_decl)
3994 result_type = TREE_TYPE (ptr_operand);
3995 break;
3997 return cp_pointer_int_sum (code,
3998 ptr_operand,
3999 int_operand);
4001 common = 1;
4002 break;
4004 case MULT_EXPR:
4005 common = 1;
4006 break;
4008 case TRUNC_DIV_EXPR:
4009 case CEIL_DIV_EXPR:
4010 case FLOOR_DIV_EXPR:
4011 case ROUND_DIV_EXPR:
4012 case EXACT_DIV_EXPR:
4013 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4014 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4015 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4016 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4018 enum tree_code tcode0 = code0, tcode1 = code1;
4020 warn_for_div_by_zero (location, op1);
4022 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4023 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4024 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4025 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4027 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4028 resultcode = RDIV_EXPR;
4029 else
4030 /* When dividing two signed integers, we have to promote to int.
4031 unless we divide by a constant != -1. Note that default
4032 conversion will have been performed on the operands at this
4033 point, so we have to dig out the original type to find out if
4034 it was unsigned. */
4035 shorten = ((TREE_CODE (op0) == NOP_EXPR
4036 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4037 || (TREE_CODE (op1) == INTEGER_CST
4038 && ! integer_all_onesp (op1)));
4040 common = 1;
4042 break;
4044 case BIT_AND_EXPR:
4045 case BIT_IOR_EXPR:
4046 case BIT_XOR_EXPR:
4047 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4048 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4049 && !VECTOR_FLOAT_TYPE_P (type0)
4050 && !VECTOR_FLOAT_TYPE_P (type1)))
4051 shorten = -1;
4052 break;
4054 case TRUNC_MOD_EXPR:
4055 case FLOOR_MOD_EXPR:
4056 warn_for_div_by_zero (location, op1);
4058 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4059 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4060 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4061 common = 1;
4062 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4064 /* Although it would be tempting to shorten always here, that loses
4065 on some targets, since the modulo instruction is undefined if the
4066 quotient can't be represented in the computation mode. We shorten
4067 only if unsigned or if dividing by something we know != -1. */
4068 shorten = ((TREE_CODE (op0) == NOP_EXPR
4069 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4070 || (TREE_CODE (op1) == INTEGER_CST
4071 && ! integer_all_onesp (op1)));
4072 common = 1;
4074 break;
4076 case TRUTH_ANDIF_EXPR:
4077 case TRUTH_ORIF_EXPR:
4078 case TRUTH_AND_EXPR:
4079 case TRUTH_OR_EXPR:
4080 result_type = boolean_type_node;
4081 break;
4083 /* Shift operations: result has same type as first operand;
4084 always convert second operand to int.
4085 Also set SHORT_SHIFT if shifting rightward. */
4087 case RSHIFT_EXPR:
4088 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4089 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4091 result_type = type0;
4092 converted = 1;
4094 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4095 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4096 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4097 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4099 result_type = type0;
4100 converted = 1;
4102 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4104 tree const_op1 = maybe_constant_value (op1);
4105 if (TREE_CODE (const_op1) != INTEGER_CST)
4106 const_op1 = op1;
4107 result_type = type0;
4108 if (TREE_CODE (const_op1) == INTEGER_CST)
4110 if (tree_int_cst_lt (const_op1, integer_zero_node))
4112 if ((complain & tf_warning)
4113 && c_inhibit_evaluation_warnings == 0)
4114 warning (0, "right shift count is negative");
4116 else
4118 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4119 && (complain & tf_warning)
4120 && c_inhibit_evaluation_warnings == 0)
4121 warning (0, "right shift count >= width of type");
4124 /* Convert the shift-count to an integer, regardless of
4125 size of value being shifted. */
4126 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4127 op1 = cp_convert (integer_type_node, op1, complain);
4128 /* Avoid converting op1 to result_type later. */
4129 converted = 1;
4131 break;
4133 case LSHIFT_EXPR:
4134 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4135 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4137 result_type = type0;
4138 converted = 1;
4140 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4141 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4142 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4143 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4145 result_type = type0;
4146 converted = 1;
4148 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4150 tree const_op1 = maybe_constant_value (op1);
4151 if (TREE_CODE (const_op1) != INTEGER_CST)
4152 const_op1 = op1;
4153 result_type = type0;
4154 if (TREE_CODE (const_op1) == INTEGER_CST)
4156 if (tree_int_cst_lt (const_op1, integer_zero_node))
4158 if ((complain & tf_warning)
4159 && c_inhibit_evaluation_warnings == 0)
4160 warning (0, "left shift count is negative");
4162 else if (compare_tree_int (const_op1,
4163 TYPE_PRECISION (type0)) >= 0)
4165 if ((complain & tf_warning)
4166 && c_inhibit_evaluation_warnings == 0)
4167 warning (0, "left shift count >= width of type");
4170 /* Convert the shift-count to an integer, regardless of
4171 size of value being shifted. */
4172 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4173 op1 = cp_convert (integer_type_node, op1, complain);
4174 /* Avoid converting op1 to result_type later. */
4175 converted = 1;
4177 break;
4179 case RROTATE_EXPR:
4180 case LROTATE_EXPR:
4181 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4183 result_type = type0;
4184 if (TREE_CODE (op1) == INTEGER_CST)
4186 if (tree_int_cst_lt (op1, integer_zero_node))
4188 if (complain & tf_warning)
4189 warning (0, (code == LROTATE_EXPR)
4190 ? G_("left rotate count is negative")
4191 : G_("right rotate count is negative"));
4193 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4195 if (complain & tf_warning)
4196 warning (0, (code == LROTATE_EXPR)
4197 ? G_("left rotate count >= width of type")
4198 : G_("right rotate count >= width of type"));
4201 /* Convert the shift-count to an integer, regardless of
4202 size of value being shifted. */
4203 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4204 op1 = cp_convert (integer_type_node, op1, complain);
4206 break;
4208 case EQ_EXPR:
4209 case NE_EXPR:
4210 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4211 goto vector_compare;
4212 if ((complain & tf_warning)
4213 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4214 warning (OPT_Wfloat_equal,
4215 "comparing floating point with == or != is unsafe");
4216 if ((complain & tf_warning)
4217 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4218 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4219 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4221 build_type = boolean_type_node;
4222 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4223 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4224 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4225 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4226 short_compare = 1;
4227 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4228 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4229 result_type = composite_pointer_type (type0, type1, op0, op1,
4230 CPO_COMPARISON, complain);
4231 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4232 && null_ptr_cst_p (op1))
4234 if (TREE_CODE (op0) == ADDR_EXPR
4235 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4237 if ((complain & tf_warning)
4238 && c_inhibit_evaluation_warnings == 0)
4239 warning (OPT_Waddress, "the address of %qD will never be NULL",
4240 TREE_OPERAND (op0, 0));
4242 result_type = type0;
4244 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4245 && null_ptr_cst_p (op0))
4247 if (TREE_CODE (op1) == ADDR_EXPR
4248 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4250 if ((complain & tf_warning)
4251 && c_inhibit_evaluation_warnings == 0)
4252 warning (OPT_Waddress, "the address of %qD will never be NULL",
4253 TREE_OPERAND (op1, 0));
4255 result_type = type1;
4257 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4258 /* One of the operands must be of nullptr_t type. */
4259 result_type = TREE_TYPE (nullptr_node);
4260 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4262 result_type = type0;
4263 if (complain & tf_error)
4264 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4265 else
4266 return error_mark_node;
4268 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4270 result_type = type1;
4271 if (complain & tf_error)
4272 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4273 else
4274 return error_mark_node;
4276 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4278 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4279 == ptrmemfunc_vbit_in_delta)
4281 tree pfn0, delta0, e1, e2;
4283 if (TREE_SIDE_EFFECTS (op0))
4284 op0 = save_expr (op0);
4286 pfn0 = pfn_from_ptrmemfunc (op0);
4287 delta0 = delta_from_ptrmemfunc (op0);
4288 e1 = cp_build_binary_op (location,
4289 EQ_EXPR,
4290 pfn0,
4291 build_zero_cst (TREE_TYPE (pfn0)),
4292 complain);
4293 e2 = cp_build_binary_op (location,
4294 BIT_AND_EXPR,
4295 delta0,
4296 integer_one_node,
4297 complain);
4299 if ((complain & tf_warning)
4300 && c_inhibit_evaluation_warnings == 0
4301 && !NULLPTR_TYPE_P (TREE_TYPE (op1)))
4302 warning (OPT_Wzero_as_null_pointer_constant,
4303 "zero as null pointer constant");
4305 e2 = cp_build_binary_op (location,
4306 EQ_EXPR, e2, integer_zero_node,
4307 complain);
4308 op0 = cp_build_binary_op (location,
4309 TRUTH_ANDIF_EXPR, e1, e2,
4310 complain);
4311 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4313 else
4315 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4316 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4318 result_type = TREE_TYPE (op0);
4320 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4321 return cp_build_binary_op (location, code, op1, op0, complain);
4322 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4324 tree type;
4325 /* E will be the final comparison. */
4326 tree e;
4327 /* E1 and E2 are for scratch. */
4328 tree e1;
4329 tree e2;
4330 tree pfn0;
4331 tree pfn1;
4332 tree delta0;
4333 tree delta1;
4335 type = composite_pointer_type (type0, type1, op0, op1,
4336 CPO_COMPARISON, complain);
4338 if (!same_type_p (TREE_TYPE (op0), type))
4339 op0 = cp_convert_and_check (type, op0, complain);
4340 if (!same_type_p (TREE_TYPE (op1), type))
4341 op1 = cp_convert_and_check (type, op1, complain);
4343 if (op0 == error_mark_node || op1 == error_mark_node)
4344 return error_mark_node;
4346 if (TREE_SIDE_EFFECTS (op0))
4347 op0 = save_expr (op0);
4348 if (TREE_SIDE_EFFECTS (op1))
4349 op1 = save_expr (op1);
4351 pfn0 = pfn_from_ptrmemfunc (op0);
4352 pfn1 = pfn_from_ptrmemfunc (op1);
4353 delta0 = delta_from_ptrmemfunc (op0);
4354 delta1 = delta_from_ptrmemfunc (op1);
4355 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4356 == ptrmemfunc_vbit_in_delta)
4358 /* We generate:
4360 (op0.pfn == op1.pfn
4361 && ((op0.delta == op1.delta)
4362 || (!op0.pfn && op0.delta & 1 == 0
4363 && op1.delta & 1 == 0))
4365 The reason for the `!op0.pfn' bit is that a NULL
4366 pointer-to-member is any member with a zero PFN and
4367 LSB of the DELTA field is 0. */
4369 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4370 delta0,
4371 integer_one_node,
4372 complain);
4373 e1 = cp_build_binary_op (location,
4374 EQ_EXPR, e1, integer_zero_node,
4375 complain);
4376 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4377 delta1,
4378 integer_one_node,
4379 complain);
4380 e2 = cp_build_binary_op (location,
4381 EQ_EXPR, e2, integer_zero_node,
4382 complain);
4383 e1 = cp_build_binary_op (location,
4384 TRUTH_ANDIF_EXPR, e2, e1,
4385 complain);
4386 e2 = cp_build_binary_op (location, EQ_EXPR,
4387 pfn0,
4388 build_zero_cst (TREE_TYPE (pfn0)),
4389 complain);
4390 e2 = cp_build_binary_op (location,
4391 TRUTH_ANDIF_EXPR, e2, e1, complain);
4392 e1 = cp_build_binary_op (location,
4393 EQ_EXPR, delta0, delta1, complain);
4394 e1 = cp_build_binary_op (location,
4395 TRUTH_ORIF_EXPR, e1, e2, complain);
4397 else
4399 /* We generate:
4401 (op0.pfn == op1.pfn
4402 && (!op0.pfn || op0.delta == op1.delta))
4404 The reason for the `!op0.pfn' bit is that a NULL
4405 pointer-to-member is any member with a zero PFN; the
4406 DELTA field is unspecified. */
4408 e1 = cp_build_binary_op (location,
4409 EQ_EXPR, delta0, delta1, complain);
4410 e2 = cp_build_binary_op (location,
4411 EQ_EXPR,
4412 pfn0,
4413 build_zero_cst (TREE_TYPE (pfn0)),
4414 complain);
4415 e1 = cp_build_binary_op (location,
4416 TRUTH_ORIF_EXPR, e1, e2, complain);
4418 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4419 e = cp_build_binary_op (location,
4420 TRUTH_ANDIF_EXPR, e2, e1, complain);
4421 if (code == EQ_EXPR)
4422 return e;
4423 return cp_build_binary_op (location,
4424 EQ_EXPR, e, integer_zero_node, complain);
4426 else
4428 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4429 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4430 type1));
4431 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4432 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4433 type0));
4436 break;
4438 case MAX_EXPR:
4439 case MIN_EXPR:
4440 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4441 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4442 shorten = 1;
4443 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4444 result_type = composite_pointer_type (type0, type1, op0, op1,
4445 CPO_COMPARISON, complain);
4446 break;
4448 case LE_EXPR:
4449 case GE_EXPR:
4450 case LT_EXPR:
4451 case GT_EXPR:
4452 if (TREE_CODE (orig_op0) == STRING_CST
4453 || TREE_CODE (orig_op1) == STRING_CST)
4455 if (complain & tf_warning)
4456 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4459 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4461 vector_compare:
4462 tree intt;
4463 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4464 TREE_TYPE (type1)))
4466 error_at (location, "comparing vectors with different "
4467 "element types");
4468 inform (location, "operand types are %qT and %qT", type0, type1);
4469 return error_mark_node;
4472 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4474 error_at (location, "comparing vectors with different "
4475 "number of elements");
4476 inform (location, "operand types are %qT and %qT", type0, type1);
4477 return error_mark_node;
4480 /* Always construct signed integer vector type. */
4481 intt = c_common_type_for_size (GET_MODE_BITSIZE
4482 (TYPE_MODE (TREE_TYPE (type0))), 0);
4483 result_type = build_opaque_vector_type (intt,
4484 TYPE_VECTOR_SUBPARTS (type0));
4485 converted = 1;
4486 break;
4488 build_type = boolean_type_node;
4489 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4490 || code0 == ENUMERAL_TYPE)
4491 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4492 || code1 == ENUMERAL_TYPE))
4493 short_compare = 1;
4494 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4495 result_type = composite_pointer_type (type0, type1, op0, op1,
4496 CPO_COMPARISON, complain);
4497 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4499 result_type = type0;
4500 if (extra_warnings && (complain & tf_warning))
4501 warning (OPT_Wextra,
4502 "ordered comparison of pointer with integer zero");
4504 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4506 result_type = type1;
4507 if (extra_warnings && (complain & tf_warning))
4508 warning (OPT_Wextra,
4509 "ordered comparison of pointer with integer zero");
4511 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4512 /* One of the operands must be of nullptr_t type. */
4513 result_type = TREE_TYPE (nullptr_node);
4514 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4516 result_type = type0;
4517 if (complain & tf_error)
4518 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4519 else
4520 return error_mark_node;
4522 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4524 result_type = type1;
4525 if (complain & tf_error)
4526 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4527 else
4528 return error_mark_node;
4530 break;
4532 case UNORDERED_EXPR:
4533 case ORDERED_EXPR:
4534 case UNLT_EXPR:
4535 case UNLE_EXPR:
4536 case UNGT_EXPR:
4537 case UNGE_EXPR:
4538 case UNEQ_EXPR:
4539 build_type = integer_type_node;
4540 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4542 if (complain & tf_error)
4543 error ("unordered comparison on non-floating point argument");
4544 return error_mark_node;
4546 common = 1;
4547 break;
4549 default:
4550 break;
4553 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4554 || code0 == ENUMERAL_TYPE)
4555 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4556 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4557 arithmetic_types_p = 1;
4558 else
4560 arithmetic_types_p = 0;
4561 /* Vector arithmetic is only allowed when both sides are vectors. */
4562 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4564 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4565 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4566 TREE_TYPE (type1)))
4568 binary_op_error (location, code, type0, type1);
4569 return error_mark_node;
4571 arithmetic_types_p = 1;
4574 /* Determine the RESULT_TYPE, if it is not already known. */
4575 if (!result_type
4576 && arithmetic_types_p
4577 && (shorten || common || short_compare))
4579 result_type = cp_common_type (type0, type1);
4580 do_warn_double_promotion (result_type, type0, type1,
4581 "implicit conversion from %qT to %qT "
4582 "to match other operand of binary "
4583 "expression",
4584 location);
4587 if (!result_type)
4589 if (complain & tf_error)
4590 error ("invalid operands of types %qT and %qT to binary %qO",
4591 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4592 return error_mark_node;
4595 /* If we're in a template, the only thing we need to know is the
4596 RESULT_TYPE. */
4597 if (processing_template_decl)
4599 /* Since the middle-end checks the type when doing a build2, we
4600 need to build the tree in pieces. This built tree will never
4601 get out of the front-end as we replace it when instantiating
4602 the template. */
4603 tree tmp = build2 (resultcode,
4604 build_type ? build_type : result_type,
4605 NULL_TREE, op1);
4606 TREE_OPERAND (tmp, 0) = op0;
4607 return tmp;
4610 if (arithmetic_types_p)
4612 bool first_complex = (code0 == COMPLEX_TYPE);
4613 bool second_complex = (code1 == COMPLEX_TYPE);
4614 int none_complex = (!first_complex && !second_complex);
4616 /* Adapted from patch for c/24581. */
4617 if (first_complex != second_complex
4618 && (code == PLUS_EXPR
4619 || code == MINUS_EXPR
4620 || code == MULT_EXPR
4621 || (code == TRUNC_DIV_EXPR && first_complex))
4622 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4623 && flag_signed_zeros)
4625 /* An operation on mixed real/complex operands must be
4626 handled specially, but the language-independent code can
4627 more easily optimize the plain complex arithmetic if
4628 -fno-signed-zeros. */
4629 tree real_type = TREE_TYPE (result_type);
4630 tree real, imag;
4631 if (first_complex)
4633 if (TREE_TYPE (op0) != result_type)
4634 op0 = cp_convert_and_check (result_type, op0, complain);
4635 if (TREE_TYPE (op1) != real_type)
4636 op1 = cp_convert_and_check (real_type, op1, complain);
4638 else
4640 if (TREE_TYPE (op0) != real_type)
4641 op0 = cp_convert_and_check (real_type, op0, complain);
4642 if (TREE_TYPE (op1) != result_type)
4643 op1 = cp_convert_and_check (result_type, op1, complain);
4645 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4646 return error_mark_node;
4647 if (first_complex)
4649 op0 = save_expr (op0);
4650 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4651 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4652 switch (code)
4654 case MULT_EXPR:
4655 case TRUNC_DIV_EXPR:
4656 op1 = save_expr (op1);
4657 imag = build2 (resultcode, real_type, imag, op1);
4658 /* Fall through. */
4659 case PLUS_EXPR:
4660 case MINUS_EXPR:
4661 real = build2 (resultcode, real_type, real, op1);
4662 break;
4663 default:
4664 gcc_unreachable();
4667 else
4669 op1 = save_expr (op1);
4670 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4671 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4672 switch (code)
4674 case MULT_EXPR:
4675 op0 = save_expr (op0);
4676 imag = build2 (resultcode, real_type, op0, imag);
4677 /* Fall through. */
4678 case PLUS_EXPR:
4679 real = build2 (resultcode, real_type, op0, real);
4680 break;
4681 case MINUS_EXPR:
4682 real = build2 (resultcode, real_type, op0, real);
4683 imag = build1 (NEGATE_EXPR, real_type, imag);
4684 break;
4685 default:
4686 gcc_unreachable();
4689 real = fold_if_not_in_template (real);
4690 imag = fold_if_not_in_template (imag);
4691 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4692 result = fold_if_not_in_template (result);
4693 return result;
4696 /* For certain operations (which identify themselves by shorten != 0)
4697 if both args were extended from the same smaller type,
4698 do the arithmetic in that type and then extend.
4700 shorten !=0 and !=1 indicates a bitwise operation.
4701 For them, this optimization is safe only if
4702 both args are zero-extended or both are sign-extended.
4703 Otherwise, we might change the result.
4704 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4705 but calculated in (unsigned short) it would be (unsigned short)-1. */
4707 if (shorten && none_complex)
4709 final_type = result_type;
4710 result_type = shorten_binary_op (result_type, op0, op1,
4711 shorten == -1);
4714 /* Comparison operations are shortened too but differently.
4715 They identify themselves by setting short_compare = 1. */
4717 if (short_compare)
4719 /* Don't write &op0, etc., because that would prevent op0
4720 from being kept in a register.
4721 Instead, make copies of the our local variables and
4722 pass the copies by reference, then copy them back afterward. */
4723 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4724 enum tree_code xresultcode = resultcode;
4725 tree val
4726 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4727 if (val != 0)
4728 return cp_convert (boolean_type_node, val, complain);
4729 op0 = xop0, op1 = xop1;
4730 converted = 1;
4731 resultcode = xresultcode;
4734 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4735 && warn_sign_compare
4736 /* Do not warn until the template is instantiated; we cannot
4737 bound the ranges of the arguments until that point. */
4738 && !processing_template_decl
4739 && (complain & tf_warning)
4740 && c_inhibit_evaluation_warnings == 0
4741 /* Even unsigned enum types promote to signed int. We don't
4742 want to issue -Wsign-compare warnings for this case. */
4743 && !enum_cast_to_int (orig_op0)
4744 && !enum_cast_to_int (orig_op1))
4746 tree oop0 = maybe_constant_value (orig_op0);
4747 tree oop1 = maybe_constant_value (orig_op1);
4749 if (TREE_CODE (oop0) != INTEGER_CST)
4750 oop0 = orig_op0;
4751 if (TREE_CODE (oop1) != INTEGER_CST)
4752 oop1 = orig_op1;
4753 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4754 result_type, resultcode);
4758 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4759 Then the expression will be built.
4760 It will be given type FINAL_TYPE if that is nonzero;
4761 otherwise, it will be given type RESULT_TYPE. */
4762 if (! converted)
4764 if (TREE_TYPE (op0) != result_type)
4765 op0 = cp_convert_and_check (result_type, op0, complain);
4766 if (TREE_TYPE (op1) != result_type)
4767 op1 = cp_convert_and_check (result_type, op1, complain);
4769 if (op0 == error_mark_node || op1 == error_mark_node)
4770 return error_mark_node;
4773 if (build_type == NULL_TREE)
4774 build_type = result_type;
4776 result = build2 (resultcode, build_type, op0, op1);
4777 result = fold_if_not_in_template (result);
4778 if (final_type != 0)
4779 result = cp_convert (final_type, result, complain);
4781 if (TREE_OVERFLOW_P (result)
4782 && !TREE_OVERFLOW_P (op0)
4783 && !TREE_OVERFLOW_P (op1))
4784 overflow_warning (location, result);
4786 return result;
4789 /* Return a tree for the sum or difference (RESULTCODE says which)
4790 of pointer PTROP and integer INTOP. */
4792 static tree
4793 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4795 tree res_type = TREE_TYPE (ptrop);
4797 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4798 in certain circumstance (when it's valid to do so). So we need
4799 to make sure it's complete. We don't need to check here, if we
4800 can actually complete it at all, as those checks will be done in
4801 pointer_int_sum() anyway. */
4802 complete_type (TREE_TYPE (res_type));
4804 return pointer_int_sum (input_location, resultcode, ptrop,
4805 fold_if_not_in_template (intop));
4808 /* Return a tree for the difference of pointers OP0 and OP1.
4809 The resulting tree has type int. */
4811 static tree
4812 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
4814 tree result;
4815 tree restype = ptrdiff_type_node;
4816 tree target_type = TREE_TYPE (ptrtype);
4818 if (!complete_type_or_else (target_type, NULL_TREE))
4819 return error_mark_node;
4821 if (TREE_CODE (target_type) == VOID_TYPE)
4823 if (complain & tf_error)
4824 permerror (input_location, "ISO C++ forbids using pointer of "
4825 "type %<void *%> in subtraction");
4826 else
4827 return error_mark_node;
4829 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4831 if (complain & tf_error)
4832 permerror (input_location, "ISO C++ forbids using pointer to "
4833 "a function in subtraction");
4834 else
4835 return error_mark_node;
4837 if (TREE_CODE (target_type) == METHOD_TYPE)
4839 if (complain & tf_error)
4840 permerror (input_location, "ISO C++ forbids using pointer to "
4841 "a method in subtraction");
4842 else
4843 return error_mark_node;
4846 /* First do the subtraction as integers;
4847 then drop through to build the divide operator. */
4849 op0 = cp_build_binary_op (input_location,
4850 MINUS_EXPR,
4851 cp_convert (restype, op0, complain),
4852 cp_convert (restype, op1, complain),
4853 complain);
4855 /* This generates an error if op1 is a pointer to an incomplete type. */
4856 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4858 if (complain & tf_error)
4859 error ("invalid use of a pointer to an incomplete type in "
4860 "pointer arithmetic");
4861 else
4862 return error_mark_node;
4865 op1 = (TYPE_PTROB_P (ptrtype)
4866 ? size_in_bytes (target_type)
4867 : integer_one_node);
4869 /* Do the division. */
4871 result = build2 (EXACT_DIV_EXPR, restype, op0,
4872 cp_convert (restype, op1, complain));
4873 return fold_if_not_in_template (result);
4876 /* Construct and perhaps optimize a tree representation
4877 for a unary operation. CODE, a tree_code, specifies the operation
4878 and XARG is the operand. */
4880 tree
4881 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
4882 tsubst_flags_t complain)
4884 tree orig_expr = xarg;
4885 tree exp;
4886 int ptrmem = 0;
4888 if (processing_template_decl)
4890 if (type_dependent_expression_p (xarg))
4891 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
4893 xarg = build_non_dependent_expr (xarg);
4896 exp = NULL_TREE;
4898 /* [expr.unary.op] says:
4900 The address of an object of incomplete type can be taken.
4902 (And is just the ordinary address operator, not an overloaded
4903 "operator &".) However, if the type is a template
4904 specialization, we must complete the type at this point so that
4905 an overloaded "operator &" will be available if required. */
4906 if (code == ADDR_EXPR
4907 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4908 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4909 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4910 || (TREE_CODE (xarg) == OFFSET_REF)))
4911 /* Don't look for a function. */;
4912 else
4913 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
4914 NULL_TREE, /*overload=*/NULL, complain);
4915 if (!exp && code == ADDR_EXPR)
4917 if (is_overloaded_fn (xarg))
4919 tree fn = get_first_fn (xarg);
4920 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4922 error (DECL_CONSTRUCTOR_P (fn)
4923 ? G_("taking address of constructor %qE")
4924 : G_("taking address of destructor %qE"),
4925 xarg);
4926 return error_mark_node;
4930 /* A pointer to member-function can be formed only by saying
4931 &X::mf. */
4932 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4933 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4935 if (TREE_CODE (xarg) != OFFSET_REF
4936 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4938 error ("invalid use of %qE to form a pointer-to-member-function",
4939 xarg);
4940 if (TREE_CODE (xarg) != OFFSET_REF)
4941 inform (input_location, " a qualified-id is required");
4942 return error_mark_node;
4944 else
4946 error ("parentheses around %qE cannot be used to form a"
4947 " pointer-to-member-function",
4948 xarg);
4949 PTRMEM_OK_P (xarg) = 1;
4953 if (TREE_CODE (xarg) == OFFSET_REF)
4955 ptrmem = PTRMEM_OK_P (xarg);
4957 if (!ptrmem && !flag_ms_extensions
4958 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4960 /* A single non-static member, make sure we don't allow a
4961 pointer-to-member. */
4962 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4963 TREE_OPERAND (xarg, 0),
4964 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4965 PTRMEM_OK_P (xarg) = ptrmem;
4969 exp = cp_build_addr_expr_strict (xarg, complain);
4972 if (processing_template_decl && exp != error_mark_node)
4973 exp = build_min_non_dep (code, exp, orig_expr,
4974 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4975 if (TREE_CODE (exp) == ADDR_EXPR)
4976 PTRMEM_OK_P (exp) = ptrmem;
4977 return exp;
4980 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4981 constants, where a null value is represented by an INTEGER_CST of
4982 -1. */
4984 tree
4985 cp_truthvalue_conversion (tree expr)
4987 tree type = TREE_TYPE (expr);
4988 if (TYPE_PTRDATAMEM_P (type))
4989 return build_binary_op (EXPR_LOCATION (expr),
4990 NE_EXPR, expr, nullptr_node, 1);
4991 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
4993 /* With -Wzero-as-null-pointer-constant do not warn for an
4994 'if (p)' or a 'while (!p)', where p is a pointer. */
4995 tree ret;
4996 ++c_inhibit_evaluation_warnings;
4997 ret = c_common_truthvalue_conversion (input_location, expr);
4998 --c_inhibit_evaluation_warnings;
4999 return ret;
5001 else
5002 return c_common_truthvalue_conversion (input_location, expr);
5005 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5007 tree
5008 condition_conversion (tree expr)
5010 tree t;
5011 if (processing_template_decl)
5012 return expr;
5013 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5014 tf_warning_or_error, LOOKUP_NORMAL);
5015 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5016 return t;
5019 /* Returns the address of T. This function will fold away
5020 ADDR_EXPR of INDIRECT_REF. */
5022 tree
5023 build_address (tree t)
5025 if (error_operand_p (t) || !cxx_mark_addressable (t))
5026 return error_mark_node;
5027 t = build_fold_addr_expr (t);
5028 if (TREE_CODE (t) != ADDR_EXPR)
5029 t = rvalue (t);
5030 return t;
5033 /* Returns the address of T with type TYPE. */
5035 tree
5036 build_typed_address (tree t, tree type)
5038 if (error_operand_p (t) || !cxx_mark_addressable (t))
5039 return error_mark_node;
5040 t = build_fold_addr_expr_with_type (t, type);
5041 if (TREE_CODE (t) != ADDR_EXPR)
5042 t = rvalue (t);
5043 return t;
5046 /* Return a NOP_EXPR converting EXPR to TYPE. */
5048 tree
5049 build_nop (tree type, tree expr)
5051 if (type == error_mark_node || error_operand_p (expr))
5052 return expr;
5053 return build1 (NOP_EXPR, type, expr);
5056 /* Take the address of ARG, whatever that means under C++ semantics.
5057 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5058 and class rvalues as well.
5060 Nothing should call this function directly; instead, callers should use
5061 cp_build_addr_expr or cp_build_addr_expr_strict. */
5063 static tree
5064 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5066 tree argtype;
5067 tree val;
5069 if (!arg || error_operand_p (arg))
5070 return error_mark_node;
5072 arg = mark_lvalue_use (arg);
5073 argtype = lvalue_type (arg);
5075 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
5076 || !IDENTIFIER_OPNAME_P (arg));
5078 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5079 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5081 /* They're trying to take the address of a unique non-static
5082 member function. This is ill-formed (except in MS-land),
5083 but let's try to DTRT.
5084 Note: We only handle unique functions here because we don't
5085 want to complain if there's a static overload; non-unique
5086 cases will be handled by instantiate_type. But we need to
5087 handle this case here to allow casts on the resulting PMF.
5088 We could defer this in non-MS mode, but it's easier to give
5089 a useful error here. */
5091 /* Inside constant member functions, the `this' pointer
5092 contains an extra const qualifier. TYPE_MAIN_VARIANT
5093 is used here to remove this const from the diagnostics
5094 and the created OFFSET_REF. */
5095 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5096 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5097 mark_used (fn);
5099 if (! flag_ms_extensions)
5101 tree name = DECL_NAME (fn);
5102 if (!(complain & tf_error))
5103 return error_mark_node;
5104 else if (current_class_type
5105 && TREE_OPERAND (arg, 0) == current_class_ref)
5106 /* An expression like &memfn. */
5107 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5108 " or parenthesized non-static member function to form"
5109 " a pointer to member function. Say %<&%T::%D%>",
5110 base, name);
5111 else
5112 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5113 " function to form a pointer to member function."
5114 " Say %<&%T::%D%>",
5115 base, name);
5117 arg = build_offset_ref (base, fn, /*address_p=*/true);
5120 /* Uninstantiated types are all functions. Taking the
5121 address of a function is a no-op, so just return the
5122 argument. */
5123 if (type_unknown_p (arg))
5124 return build1 (ADDR_EXPR, unknown_type_node, arg);
5126 if (TREE_CODE (arg) == OFFSET_REF)
5127 /* We want a pointer to member; bypass all the code for actually taking
5128 the address of something. */
5129 goto offset_ref;
5131 /* Anything not already handled and not a true memory reference
5132 is an error. */
5133 if (TREE_CODE (argtype) != FUNCTION_TYPE
5134 && TREE_CODE (argtype) != METHOD_TYPE)
5136 cp_lvalue_kind kind = lvalue_kind (arg);
5137 if (kind == clk_none)
5139 if (complain & tf_error)
5140 lvalue_error (input_location, lv_addressof);
5141 return error_mark_node;
5143 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5145 if (!(complain & tf_error))
5146 return error_mark_node;
5147 if (kind & clk_class)
5148 /* Make this a permerror because we used to accept it. */
5149 permerror (input_location, "taking address of temporary");
5150 else
5151 error ("taking address of xvalue (rvalue reference)");
5155 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5157 tree type = build_pointer_type (TREE_TYPE (argtype));
5158 arg = build1 (CONVERT_EXPR, type, arg);
5159 return arg;
5161 else if (pedantic && DECL_MAIN_P (arg))
5163 /* ARM $3.4 */
5164 /* Apparently a lot of autoconf scripts for C++ packages do this,
5165 so only complain if -Wpedantic. */
5166 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5167 pedwarn (input_location, OPT_Wpedantic,
5168 "ISO C++ forbids taking address of function %<::main%>");
5169 else if (flag_pedantic_errors)
5170 return error_mark_node;
5173 /* Let &* cancel out to simplify resulting code. */
5174 if (TREE_CODE (arg) == INDIRECT_REF)
5176 /* We don't need to have `current_class_ptr' wrapped in a
5177 NON_LVALUE_EXPR node. */
5178 if (arg == current_class_ref)
5179 return current_class_ptr;
5181 arg = TREE_OPERAND (arg, 0);
5182 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5184 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5185 arg = build1 (CONVERT_EXPR, type, arg);
5187 else
5188 /* Don't let this be an lvalue. */
5189 arg = rvalue (arg);
5190 return arg;
5193 /* ??? Cope with user tricks that amount to offsetof. */
5194 if (TREE_CODE (argtype) != FUNCTION_TYPE
5195 && TREE_CODE (argtype) != METHOD_TYPE
5196 && argtype != unknown_type_node
5197 && (val = get_base_address (arg))
5198 && COMPLETE_TYPE_P (TREE_TYPE (val))
5199 && TREE_CODE (val) == INDIRECT_REF
5200 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5202 tree type = build_pointer_type (argtype);
5203 return fold_convert (type, fold_offsetof_1 (arg));
5206 /* Handle complex lvalues (when permitted)
5207 by reduction to simpler cases. */
5208 val = unary_complex_lvalue (ADDR_EXPR, arg);
5209 if (val != 0)
5210 return val;
5212 switch (TREE_CODE (arg))
5214 CASE_CONVERT:
5215 case FLOAT_EXPR:
5216 case FIX_TRUNC_EXPR:
5217 /* Even if we're not being pedantic, we cannot allow this
5218 extension when we're instantiating in a SFINAE
5219 context. */
5220 if (! lvalue_p (arg) && complain == tf_none)
5222 if (complain & tf_error)
5223 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5224 else
5225 return error_mark_node;
5227 break;
5229 case BASELINK:
5230 arg = BASELINK_FUNCTIONS (arg);
5231 /* Fall through. */
5233 case OVERLOAD:
5234 arg = OVL_CURRENT (arg);
5235 break;
5237 case OFFSET_REF:
5238 offset_ref:
5239 /* Turn a reference to a non-static data member into a
5240 pointer-to-member. */
5242 tree type;
5243 tree t;
5245 gcc_assert (PTRMEM_OK_P (arg));
5247 t = TREE_OPERAND (arg, 1);
5248 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5250 if (complain & tf_error)
5251 error ("cannot create pointer to reference member %qD", t);
5252 return error_mark_node;
5255 type = build_ptrmem_type (context_for_name_lookup (t),
5256 TREE_TYPE (t));
5257 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5258 return t;
5261 default:
5262 break;
5265 if (argtype != error_mark_node)
5266 argtype = build_pointer_type (argtype);
5268 /* In a template, we are processing a non-dependent expression
5269 so we can just form an ADDR_EXPR with the correct type. */
5270 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5272 val = build_address (arg);
5273 if (TREE_CODE (arg) == OFFSET_REF)
5274 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5276 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5278 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5280 /* We can only get here with a single static member
5281 function. */
5282 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5283 && DECL_STATIC_FUNCTION_P (fn));
5284 mark_used (fn);
5285 val = build_address (fn);
5286 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5287 /* Do not lose object's side effects. */
5288 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5289 TREE_OPERAND (arg, 0), val);
5291 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5293 if (complain & tf_error)
5294 error ("attempt to take address of bit-field structure member %qD",
5295 TREE_OPERAND (arg, 1));
5296 return error_mark_node;
5298 else
5300 tree object = TREE_OPERAND (arg, 0);
5301 tree field = TREE_OPERAND (arg, 1);
5302 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5303 (TREE_TYPE (object), decl_type_context (field)));
5304 val = build_address (arg);
5307 if (TREE_CODE (argtype) == POINTER_TYPE
5308 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5310 build_ptrmemfunc_type (argtype);
5311 val = build_ptrmemfunc (argtype, val, 0,
5312 /*c_cast_p=*/false,
5313 complain);
5316 return val;
5319 /* Take the address of ARG if it has one, even if it's an rvalue. */
5321 tree
5322 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5324 return cp_build_addr_expr_1 (arg, 0, complain);
5327 /* Take the address of ARG, but only if it's an lvalue. */
5329 tree
5330 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5332 return cp_build_addr_expr_1 (arg, 1, complain);
5335 /* C++: Must handle pointers to members.
5337 Perhaps type instantiation should be extended to handle conversion
5338 from aggregates to types we don't yet know we want? (Or are those
5339 cases typically errors which should be reported?)
5341 NOCONVERT nonzero suppresses the default promotions
5342 (such as from short to int). */
5344 tree
5345 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5346 tsubst_flags_t complain)
5348 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5349 tree arg = xarg;
5350 tree argtype = 0;
5351 const char *errstring = NULL;
5352 tree val;
5353 const char *invalid_op_diag;
5355 if (!arg || error_operand_p (arg))
5356 return error_mark_node;
5358 if ((invalid_op_diag
5359 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5360 ? CONVERT_EXPR
5361 : code),
5362 TREE_TYPE (xarg))))
5364 error (invalid_op_diag);
5365 return error_mark_node;
5368 switch (code)
5370 case UNARY_PLUS_EXPR:
5371 case NEGATE_EXPR:
5373 int flags = WANT_ARITH | WANT_ENUM;
5374 /* Unary plus (but not unary minus) is allowed on pointers. */
5375 if (code == UNARY_PLUS_EXPR)
5376 flags |= WANT_POINTER;
5377 arg = build_expr_type_conversion (flags, arg, true);
5378 if (!arg)
5379 errstring = (code == NEGATE_EXPR
5380 ? _("wrong type argument to unary minus")
5381 : _("wrong type argument to unary plus"));
5382 else
5384 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5385 arg = cp_perform_integral_promotions (arg, complain);
5387 /* Make sure the result is not an lvalue: a unary plus or minus
5388 expression is always a rvalue. */
5389 arg = rvalue (arg);
5392 break;
5394 case BIT_NOT_EXPR:
5395 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5397 code = CONJ_EXPR;
5398 if (!noconvert)
5400 arg = cp_default_conversion (arg, complain);
5401 if (arg == error_mark_node)
5402 return error_mark_node;
5405 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5406 | WANT_VECTOR_OR_COMPLEX,
5407 arg, true)))
5408 errstring = _("wrong type argument to bit-complement");
5409 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5410 arg = cp_perform_integral_promotions (arg, complain);
5411 break;
5413 case ABS_EXPR:
5414 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5415 errstring = _("wrong type argument to abs");
5416 else if (!noconvert)
5418 arg = cp_default_conversion (arg, complain);
5419 if (arg == error_mark_node)
5420 return error_mark_node;
5422 break;
5424 case CONJ_EXPR:
5425 /* Conjugating a real value is a no-op, but allow it anyway. */
5426 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5427 errstring = _("wrong type argument to conjugation");
5428 else if (!noconvert)
5430 arg = cp_default_conversion (arg, complain);
5431 if (arg == error_mark_node)
5432 return error_mark_node;
5434 break;
5436 case TRUTH_NOT_EXPR:
5437 arg = perform_implicit_conversion (boolean_type_node, arg,
5438 complain);
5439 val = invert_truthvalue_loc (input_location, arg);
5440 if (arg != error_mark_node)
5441 return val;
5442 errstring = _("in argument to unary !");
5443 break;
5445 case NOP_EXPR:
5446 break;
5448 case REALPART_EXPR:
5449 case IMAGPART_EXPR:
5450 arg = build_real_imag_expr (input_location, code, arg);
5451 if (arg == error_mark_node)
5452 return arg;
5453 else
5454 return fold_if_not_in_template (arg);
5456 case PREINCREMENT_EXPR:
5457 case POSTINCREMENT_EXPR:
5458 case PREDECREMENT_EXPR:
5459 case POSTDECREMENT_EXPR:
5460 /* Handle complex lvalues (when permitted)
5461 by reduction to simpler cases. */
5463 val = unary_complex_lvalue (code, arg);
5464 if (val != 0)
5465 return val;
5467 arg = mark_lvalue_use (arg);
5469 /* Increment or decrement the real part of the value,
5470 and don't change the imaginary part. */
5471 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5473 tree real, imag;
5475 arg = stabilize_reference (arg);
5476 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5477 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5478 real = cp_build_unary_op (code, real, 1, complain);
5479 if (real == error_mark_node || imag == error_mark_node)
5480 return error_mark_node;
5481 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5482 real, imag);
5485 /* Report invalid types. */
5487 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5488 arg, true)))
5490 if (code == PREINCREMENT_EXPR)
5491 errstring = _("no pre-increment operator for type");
5492 else if (code == POSTINCREMENT_EXPR)
5493 errstring = _("no post-increment operator for type");
5494 else if (code == PREDECREMENT_EXPR)
5495 errstring = _("no pre-decrement operator for type");
5496 else
5497 errstring = _("no post-decrement operator for type");
5498 break;
5500 else if (arg == error_mark_node)
5501 return error_mark_node;
5503 /* Report something read-only. */
5505 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5506 || TREE_READONLY (arg))
5508 if (complain & tf_error)
5509 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5510 || code == POSTINCREMENT_EXPR)
5511 ? lv_increment : lv_decrement));
5512 else
5513 return error_mark_node;
5517 tree inc;
5518 tree declared_type = unlowered_expr_type (arg);
5520 argtype = TREE_TYPE (arg);
5522 /* ARM $5.2.5 last annotation says this should be forbidden. */
5523 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5525 if (complain & tf_error)
5526 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5527 ? G_("ISO C++ forbids incrementing an enum")
5528 : G_("ISO C++ forbids decrementing an enum"));
5529 else
5530 return error_mark_node;
5533 /* Compute the increment. */
5535 if (TREE_CODE (argtype) == POINTER_TYPE)
5537 tree type = complete_type (TREE_TYPE (argtype));
5539 if (!COMPLETE_OR_VOID_TYPE_P (type))
5541 if (complain & tf_error)
5542 error (((code == PREINCREMENT_EXPR
5543 || code == POSTINCREMENT_EXPR))
5544 ? G_("cannot increment a pointer to incomplete type %qT")
5545 : G_("cannot decrement a pointer to incomplete type %qT"),
5546 TREE_TYPE (argtype));
5547 else
5548 return error_mark_node;
5550 else if ((pedantic || warn_pointer_arith)
5551 && !TYPE_PTROB_P (argtype))
5553 if (complain & tf_error)
5554 permerror (input_location, (code == PREINCREMENT_EXPR
5555 || code == POSTINCREMENT_EXPR)
5556 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5557 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5558 argtype);
5559 else
5560 return error_mark_node;
5563 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5565 else
5566 inc = integer_one_node;
5568 inc = cp_convert (argtype, inc, complain);
5570 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5571 need to ask Objective-C to build the increment or decrement
5572 expression for it. */
5573 if (objc_is_property_ref (arg))
5574 return objc_build_incr_expr_for_property_ref (input_location, code,
5575 arg, inc);
5577 /* Complain about anything else that is not a true lvalue. */
5578 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5579 || code == POSTINCREMENT_EXPR)
5580 ? lv_increment : lv_decrement),
5581 complain))
5582 return error_mark_node;
5584 /* Forbid using -- on `bool'. */
5585 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5587 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5589 if (complain & tf_error)
5590 error ("invalid use of Boolean expression as operand "
5591 "to %<operator--%>");
5592 return error_mark_node;
5594 val = boolean_increment (code, arg);
5596 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5597 /* An rvalue has no cv-qualifiers. */
5598 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5599 else
5600 val = build2 (code, TREE_TYPE (arg), arg, inc);
5602 TREE_SIDE_EFFECTS (val) = 1;
5603 return val;
5606 case ADDR_EXPR:
5607 /* Note that this operation never does default_conversion
5608 regardless of NOCONVERT. */
5609 return cp_build_addr_expr (arg, complain);
5611 default:
5612 break;
5615 if (!errstring)
5617 if (argtype == 0)
5618 argtype = TREE_TYPE (arg);
5619 return fold_if_not_in_template (build1 (code, argtype, arg));
5622 if (complain & tf_error)
5623 error ("%s", errstring);
5624 return error_mark_node;
5627 /* Hook for the c-common bits that build a unary op. */
5628 tree
5629 build_unary_op (location_t /*location*/,
5630 enum tree_code code, tree xarg, int noconvert)
5632 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5635 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5636 for certain kinds of expressions which are not really lvalues
5637 but which we can accept as lvalues.
5639 If ARG is not a kind of expression we can handle, return
5640 NULL_TREE. */
5642 tree
5643 unary_complex_lvalue (enum tree_code code, tree arg)
5645 /* Inside a template, making these kinds of adjustments is
5646 pointless; we are only concerned with the type of the
5647 expression. */
5648 if (processing_template_decl)
5649 return NULL_TREE;
5651 /* Handle (a, b) used as an "lvalue". */
5652 if (TREE_CODE (arg) == COMPOUND_EXPR)
5654 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5655 tf_warning_or_error);
5656 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5657 TREE_OPERAND (arg, 0), real_result);
5660 /* Handle (a ? b : c) used as an "lvalue". */
5661 if (TREE_CODE (arg) == COND_EXPR
5662 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5663 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5665 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5666 if (TREE_CODE (arg) == MODIFY_EXPR
5667 || TREE_CODE (arg) == PREINCREMENT_EXPR
5668 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5670 tree lvalue = TREE_OPERAND (arg, 0);
5671 if (TREE_SIDE_EFFECTS (lvalue))
5673 lvalue = stabilize_reference (lvalue);
5674 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5675 lvalue, TREE_OPERAND (arg, 1));
5677 return unary_complex_lvalue
5678 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5681 if (code != ADDR_EXPR)
5682 return NULL_TREE;
5684 /* Handle (a = b) used as an "lvalue" for `&'. */
5685 if (TREE_CODE (arg) == MODIFY_EXPR
5686 || TREE_CODE (arg) == INIT_EXPR)
5688 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5689 tf_warning_or_error);
5690 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5691 arg, real_result);
5692 TREE_NO_WARNING (arg) = 1;
5693 return arg;
5696 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5697 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5698 || TREE_CODE (arg) == OFFSET_REF)
5699 return NULL_TREE;
5701 /* We permit compiler to make function calls returning
5702 objects of aggregate type look like lvalues. */
5704 tree targ = arg;
5706 if (TREE_CODE (targ) == SAVE_EXPR)
5707 targ = TREE_OPERAND (targ, 0);
5709 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5711 if (TREE_CODE (arg) == SAVE_EXPR)
5712 targ = arg;
5713 else
5714 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5715 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5718 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5719 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5720 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5723 /* Don't let anything else be handled specially. */
5724 return NULL_TREE;
5727 /* Mark EXP saying that we need to be able to take the
5728 address of it; it should not be allocated in a register.
5729 Value is true if successful.
5731 C++: we do not allow `current_class_ptr' to be addressable. */
5733 bool
5734 cxx_mark_addressable (tree exp)
5736 tree x = exp;
5738 while (1)
5739 switch (TREE_CODE (x))
5741 case ADDR_EXPR:
5742 case COMPONENT_REF:
5743 case ARRAY_REF:
5744 case REALPART_EXPR:
5745 case IMAGPART_EXPR:
5746 x = TREE_OPERAND (x, 0);
5747 break;
5749 case PARM_DECL:
5750 if (x == current_class_ptr)
5752 error ("cannot take the address of %<this%>, which is an rvalue expression");
5753 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5754 return true;
5756 /* Fall through. */
5758 case VAR_DECL:
5759 /* Caller should not be trying to mark initialized
5760 constant fields addressable. */
5761 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5762 || DECL_IN_AGGR_P (x) == 0
5763 || TREE_STATIC (x)
5764 || DECL_EXTERNAL (x));
5765 /* Fall through. */
5767 case RESULT_DECL:
5768 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5769 && !DECL_ARTIFICIAL (x))
5771 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
5773 error
5774 ("address of explicit register variable %qD requested", x);
5775 return false;
5777 else if (extra_warnings)
5778 warning
5779 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5781 TREE_ADDRESSABLE (x) = 1;
5782 return true;
5784 case CONST_DECL:
5785 case FUNCTION_DECL:
5786 TREE_ADDRESSABLE (x) = 1;
5787 return true;
5789 case CONSTRUCTOR:
5790 TREE_ADDRESSABLE (x) = 1;
5791 return true;
5793 case TARGET_EXPR:
5794 TREE_ADDRESSABLE (x) = 1;
5795 cxx_mark_addressable (TREE_OPERAND (x, 0));
5796 return true;
5798 default:
5799 return true;
5803 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5805 tree
5806 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
5807 tsubst_flags_t complain)
5809 tree orig_ifexp = ifexp;
5810 tree orig_op1 = op1;
5811 tree orig_op2 = op2;
5812 tree expr;
5814 if (processing_template_decl)
5816 /* The standard says that the expression is type-dependent if
5817 IFEXP is type-dependent, even though the eventual type of the
5818 expression doesn't dependent on IFEXP. */
5819 if (type_dependent_expression_p (ifexp)
5820 /* As a GNU extension, the middle operand may be omitted. */
5821 || (op1 && type_dependent_expression_p (op1))
5822 || type_dependent_expression_p (op2))
5823 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
5824 ifexp = build_non_dependent_expr (ifexp);
5825 if (op1)
5826 op1 = build_non_dependent_expr (op1);
5827 op2 = build_non_dependent_expr (op2);
5830 expr = build_conditional_expr (ifexp, op1, op2, complain);
5831 if (processing_template_decl && expr != error_mark_node
5832 && TREE_CODE (expr) != VEC_COND_EXPR)
5834 tree min = build_min_non_dep (COND_EXPR, expr,
5835 orig_ifexp, orig_op1, orig_op2);
5836 /* In C++11, remember that the result is an lvalue or xvalue.
5837 In C++98, lvalue_kind can just assume lvalue in a template. */
5838 if (cxx_dialect >= cxx0x
5839 && lvalue_or_rvalue_with_address_p (expr)
5840 && !lvalue_or_rvalue_with_address_p (min))
5841 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5842 !real_lvalue_p (expr));
5843 expr = convert_from_reference (min);
5845 return expr;
5848 /* Given a list of expressions, return a compound expression
5849 that performs them all and returns the value of the last of them. */
5851 tree
5852 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5853 tsubst_flags_t complain)
5855 tree expr = TREE_VALUE (list);
5857 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5858 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5860 if (complain & tf_error)
5861 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5862 "non-class type must not be parenthesized");
5863 else
5864 return error_mark_node;
5867 if (TREE_CHAIN (list))
5869 if (complain & tf_error)
5870 switch (exp)
5872 case ELK_INIT:
5873 permerror (input_location, "expression list treated as compound "
5874 "expression in initializer");
5875 break;
5876 case ELK_MEM_INIT:
5877 permerror (input_location, "expression list treated as compound "
5878 "expression in mem-initializer");
5879 break;
5880 case ELK_FUNC_CAST:
5881 permerror (input_location, "expression list treated as compound "
5882 "expression in functional cast");
5883 break;
5884 default:
5885 gcc_unreachable ();
5887 else
5888 return error_mark_node;
5890 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5891 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
5892 expr, TREE_VALUE (list), complain);
5895 return expr;
5898 /* Like build_x_compound_expr_from_list, but using a VEC. */
5900 tree
5901 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
5902 tsubst_flags_t complain)
5904 if (vec_safe_is_empty (vec))
5905 return NULL_TREE;
5906 else if (vec->length () == 1)
5907 return (*vec)[0];
5908 else
5910 tree expr;
5911 unsigned int ix;
5912 tree t;
5914 if (msg != NULL)
5916 if (complain & tf_error)
5917 permerror (input_location,
5918 "%s expression list treated as compound expression",
5919 msg);
5920 else
5921 return error_mark_node;
5924 expr = (*vec)[0];
5925 for (ix = 1; vec->iterate (ix, &t); ++ix)
5926 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
5927 t, complain);
5929 return expr;
5933 /* Handle overloading of the ',' operator when needed. */
5935 tree
5936 build_x_compound_expr (location_t loc, tree op1, tree op2,
5937 tsubst_flags_t complain)
5939 tree result;
5940 tree orig_op1 = op1;
5941 tree orig_op2 = op2;
5943 if (processing_template_decl)
5945 if (type_dependent_expression_p (op1)
5946 || type_dependent_expression_p (op2))
5947 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
5948 op1 = build_non_dependent_expr (op1);
5949 op2 = build_non_dependent_expr (op2);
5952 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
5953 NULL_TREE, /*overload=*/NULL, complain);
5954 if (!result)
5955 result = cp_build_compound_expr (op1, op2, complain);
5957 if (processing_template_decl && result != error_mark_node)
5958 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
5960 return result;
5963 /* Like cp_build_compound_expr, but for the c-common bits. */
5965 tree
5966 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
5968 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
5971 /* Build a compound expression. */
5973 tree
5974 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
5976 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
5978 if (lhs == error_mark_node || rhs == error_mark_node)
5979 return error_mark_node;
5981 if (TREE_CODE (rhs) == TARGET_EXPR)
5983 /* If the rhs is a TARGET_EXPR, then build the compound
5984 expression inside the target_expr's initializer. This
5985 helps the compiler to eliminate unnecessary temporaries. */
5986 tree init = TREE_OPERAND (rhs, 1);
5988 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
5989 TREE_OPERAND (rhs, 1) = init;
5991 return rhs;
5994 if (type_unknown_p (rhs))
5996 error ("no context to resolve type of %qE", rhs);
5997 return error_mark_node;
6000 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6003 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6004 casts away constness. CAST gives the type of cast. Returns true
6005 if the cast is ill-formed, false if it is well-formed.
6007 ??? This function warns for casting away any qualifier not just
6008 const. We would like to specify exactly what qualifiers are casted
6009 away.
6012 static bool
6013 check_for_casting_away_constness (tree src_type, tree dest_type,
6014 enum tree_code cast, tsubst_flags_t complain)
6016 /* C-style casts are allowed to cast away constness. With
6017 WARN_CAST_QUAL, we still want to issue a warning. */
6018 if (cast == CAST_EXPR && !warn_cast_qual)
6019 return false;
6021 if (!casts_away_constness (src_type, dest_type, complain))
6022 return false;
6024 switch (cast)
6026 case CAST_EXPR:
6027 if (complain & tf_warning)
6028 warning (OPT_Wcast_qual,
6029 "cast from type %qT to type %qT casts away qualifiers",
6030 src_type, dest_type);
6031 return false;
6033 case STATIC_CAST_EXPR:
6034 if (complain & tf_error)
6035 error ("static_cast from type %qT to type %qT casts away qualifiers",
6036 src_type, dest_type);
6037 return true;
6039 case REINTERPRET_CAST_EXPR:
6040 if (complain & tf_error)
6041 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6042 src_type, dest_type);
6043 return true;
6045 default:
6046 gcc_unreachable();
6051 Warns if the cast from expression EXPR to type TYPE is useless.
6053 void
6054 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6056 if (warn_useless_cast
6057 && complain & tf_warning
6058 && c_inhibit_evaluation_warnings == 0)
6060 if (REFERENCE_REF_P (expr))
6061 expr = TREE_OPERAND (expr, 0);
6063 if ((TREE_CODE (type) == REFERENCE_TYPE
6064 && (TYPE_REF_IS_RVALUE (type)
6065 ? xvalue_p (expr) : real_lvalue_p (expr))
6066 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6067 || same_type_p (TREE_TYPE (expr), type))
6068 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6072 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6073 (another pointer-to-member type in the same hierarchy) and return
6074 the converted expression. If ALLOW_INVERSE_P is permitted, a
6075 pointer-to-derived may be converted to pointer-to-base; otherwise,
6076 only the other direction is permitted. If C_CAST_P is true, this
6077 conversion is taking place as part of a C-style cast. */
6079 tree
6080 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6081 bool c_cast_p, tsubst_flags_t complain)
6083 if (TYPE_PTRDATAMEM_P (type))
6085 tree delta;
6087 if (TREE_CODE (expr) == PTRMEM_CST)
6088 expr = cplus_expand_constant (expr);
6089 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6090 TYPE_PTRMEM_CLASS_TYPE (type),
6091 allow_inverse_p,
6092 c_cast_p, complain);
6093 if (delta == error_mark_node)
6094 return error_mark_node;
6096 if (!integer_zerop (delta))
6098 tree cond, op1, op2;
6100 cond = cp_build_binary_op (input_location,
6101 EQ_EXPR,
6102 expr,
6103 build_int_cst (TREE_TYPE (expr), -1),
6104 complain);
6105 op1 = build_nop (ptrdiff_type_node, expr);
6106 op2 = cp_build_binary_op (input_location,
6107 PLUS_EXPR, op1, delta,
6108 complain);
6110 expr = fold_build3_loc (input_location,
6111 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6115 return build_nop (type, expr);
6117 else
6118 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6119 allow_inverse_p, c_cast_p, complain);
6122 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6123 this static_cast is being attempted as one of the possible casts
6124 allowed by a C-style cast. (In that case, accessibility of base
6125 classes is not considered, and it is OK to cast away
6126 constness.) Return the result of the cast. *VALID_P is set to
6127 indicate whether or not the cast was valid. */
6129 static tree
6130 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6131 bool *valid_p, tsubst_flags_t complain)
6133 tree intype;
6134 tree result;
6135 cp_lvalue_kind clk;
6137 /* Assume the cast is valid. */
6138 *valid_p = true;
6140 intype = unlowered_expr_type (expr);
6142 /* Save casted types in the function's used types hash table. */
6143 used_types_insert (type);
6145 /* [expr.static.cast]
6147 An lvalue of type "cv1 B", where B is a class type, can be cast
6148 to type "reference to cv2 D", where D is a class derived (clause
6149 _class.derived_) from B, if a valid standard conversion from
6150 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6151 same cv-qualification as, or greater cv-qualification than, cv1,
6152 and B is not a virtual base class of D. */
6153 /* We check this case before checking the validity of "TYPE t =
6154 EXPR;" below because for this case:
6156 struct B {};
6157 struct D : public B { D(const B&); };
6158 extern B& b;
6159 void f() { static_cast<const D&>(b); }
6161 we want to avoid constructing a new D. The standard is not
6162 completely clear about this issue, but our interpretation is
6163 consistent with other compilers. */
6164 if (TREE_CODE (type) == REFERENCE_TYPE
6165 && CLASS_TYPE_P (TREE_TYPE (type))
6166 && CLASS_TYPE_P (intype)
6167 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6168 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6169 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6170 build_pointer_type (TYPE_MAIN_VARIANT
6171 (TREE_TYPE (type))),
6172 complain)
6173 && (c_cast_p
6174 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6176 tree base;
6178 /* There is a standard conversion from "D*" to "B*" even if "B"
6179 is ambiguous or inaccessible. If this is really a
6180 static_cast, then we check both for inaccessibility and
6181 ambiguity. However, if this is a static_cast being performed
6182 because the user wrote a C-style cast, then accessibility is
6183 not considered. */
6184 base = lookup_base (TREE_TYPE (type), intype,
6185 c_cast_p ? ba_unique : ba_check,
6186 NULL, complain);
6188 /* Convert from "B*" to "D*". This function will check that "B"
6189 is not a virtual base of "D". */
6190 expr = build_base_path (MINUS_EXPR, build_address (expr),
6191 base, /*nonnull=*/false, complain);
6192 /* Convert the pointer to a reference -- but then remember that
6193 there are no expressions with reference type in C++.
6195 We call rvalue so that there's an actual tree code
6196 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6197 is a variable with the same type, the conversion would get folded
6198 away, leaving just the variable and causing lvalue_kind to give
6199 the wrong answer. */
6200 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6203 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6204 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6205 if (TREE_CODE (type) == REFERENCE_TYPE
6206 && TYPE_REF_IS_RVALUE (type)
6207 && (clk = real_lvalue_p (expr))
6208 && reference_related_p (TREE_TYPE (type), intype)
6209 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6211 if (clk == clk_ordinary)
6213 /* Handle the (non-bit-field) lvalue case here by casting to
6214 lvalue reference and then changing it to an rvalue reference.
6215 Casting an xvalue to rvalue reference will be handled by the
6216 main code path. */
6217 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6218 result = (perform_direct_initialization_if_possible
6219 (lref, expr, c_cast_p, complain));
6220 result = cp_fold_convert (type, result);
6221 /* Make sure we don't fold back down to a named rvalue reference,
6222 because that would be an lvalue. */
6223 if (DECL_P (result))
6224 result = build1 (NON_LVALUE_EXPR, type, result);
6225 return convert_from_reference (result);
6227 else
6228 /* For a bit-field or packed field, bind to a temporary. */
6229 expr = rvalue (expr);
6232 /* Resolve overloaded address here rather than once in
6233 implicit_conversion and again in the inverse code below. */
6234 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6236 expr = instantiate_type (type, expr, complain);
6237 intype = TREE_TYPE (expr);
6240 /* [expr.static.cast]
6242 Any expression can be explicitly converted to type cv void. */
6243 if (TREE_CODE (type) == VOID_TYPE)
6244 return convert_to_void (expr, ICV_CAST, complain);
6246 /* [expr.static.cast]
6248 An expression e can be explicitly converted to a type T using a
6249 static_cast of the form static_cast<T>(e) if the declaration T
6250 t(e);" is well-formed, for some invented temporary variable
6251 t. */
6252 result = perform_direct_initialization_if_possible (type, expr,
6253 c_cast_p, complain);
6254 if (result)
6256 result = convert_from_reference (result);
6258 /* [expr.static.cast]
6260 If T is a reference type, the result is an lvalue; otherwise,
6261 the result is an rvalue. */
6262 if (TREE_CODE (type) != REFERENCE_TYPE)
6263 result = rvalue (result);
6264 return result;
6267 /* [expr.static.cast]
6269 The inverse of any standard conversion sequence (clause _conv_),
6270 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6271 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6272 (_conv.bool_) conversions, can be performed explicitly using
6273 static_cast subject to the restriction that the explicit
6274 conversion does not cast away constness (_expr.const.cast_), and
6275 the following additional rules for specific cases: */
6276 /* For reference, the conversions not excluded are: integral
6277 promotions, floating point promotion, integral conversions,
6278 floating point conversions, floating-integral conversions,
6279 pointer conversions, and pointer to member conversions. */
6280 /* DR 128
6282 A value of integral _or enumeration_ type can be explicitly
6283 converted to an enumeration type. */
6284 /* The effect of all that is that any conversion between any two
6285 types which are integral, floating, or enumeration types can be
6286 performed. */
6287 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6288 || SCALAR_FLOAT_TYPE_P (type))
6289 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6290 || SCALAR_FLOAT_TYPE_P (intype)))
6291 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6293 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6294 && CLASS_TYPE_P (TREE_TYPE (type))
6295 && CLASS_TYPE_P (TREE_TYPE (intype))
6296 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6297 (TREE_TYPE (intype))),
6298 build_pointer_type (TYPE_MAIN_VARIANT
6299 (TREE_TYPE (type))),
6300 complain))
6302 tree base;
6304 if (!c_cast_p
6305 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6306 complain))
6307 return error_mark_node;
6308 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6309 c_cast_p ? ba_unique : ba_check,
6310 NULL, complain);
6311 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6312 complain);
6313 return cp_fold_convert(type, expr);
6316 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6317 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6319 tree c1;
6320 tree c2;
6321 tree t1;
6322 tree t2;
6324 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6325 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6327 if (TYPE_PTRDATAMEM_P (type))
6329 t1 = (build_ptrmem_type
6330 (c1,
6331 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6332 t2 = (build_ptrmem_type
6333 (c2,
6334 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6336 else
6338 t1 = intype;
6339 t2 = type;
6341 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6343 if (!c_cast_p
6344 && check_for_casting_away_constness (intype, type,
6345 STATIC_CAST_EXPR,
6346 complain))
6347 return error_mark_node;
6348 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6349 c_cast_p, complain);
6353 /* [expr.static.cast]
6355 An rvalue of type "pointer to cv void" can be explicitly
6356 converted to a pointer to object type. A value of type pointer
6357 to object converted to "pointer to cv void" and back to the
6358 original pointer type will have its original value. */
6359 if (TREE_CODE (intype) == POINTER_TYPE
6360 && VOID_TYPE_P (TREE_TYPE (intype))
6361 && TYPE_PTROB_P (type))
6363 if (!c_cast_p
6364 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6365 complain))
6366 return error_mark_node;
6367 return build_nop (type, expr);
6370 *valid_p = false;
6371 return error_mark_node;
6374 /* Return an expression representing static_cast<TYPE>(EXPR). */
6376 tree
6377 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6379 tree result;
6380 bool valid_p;
6382 if (type == error_mark_node || expr == error_mark_node)
6383 return error_mark_node;
6385 if (processing_template_decl)
6387 expr = build_min (STATIC_CAST_EXPR, type, expr);
6388 /* We don't know if it will or will not have side effects. */
6389 TREE_SIDE_EFFECTS (expr) = 1;
6390 return convert_from_reference (expr);
6393 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6394 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6395 if (TREE_CODE (type) != REFERENCE_TYPE
6396 && TREE_CODE (expr) == NOP_EXPR
6397 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6398 expr = TREE_OPERAND (expr, 0);
6400 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6401 complain);
6402 if (valid_p)
6404 if (result != error_mark_node)
6405 maybe_warn_about_useless_cast (type, expr, complain);
6406 return result;
6409 if (complain & tf_error)
6410 error ("invalid static_cast from type %qT to type %qT",
6411 TREE_TYPE (expr), type);
6412 return error_mark_node;
6415 /* EXPR is an expression with member function or pointer-to-member
6416 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6417 not permitted by ISO C++, but we accept it in some modes. If we
6418 are not in one of those modes, issue a diagnostic. Return the
6419 converted expression. */
6421 tree
6422 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6424 tree intype;
6425 tree decl;
6427 intype = TREE_TYPE (expr);
6428 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6429 || TREE_CODE (intype) == METHOD_TYPE);
6431 if (!(complain & tf_warning_or_error))
6432 return error_mark_node;
6434 if (pedantic || warn_pmf2ptr)
6435 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6436 "converting from %qT to %qT", intype, type);
6438 if (TREE_CODE (intype) == METHOD_TYPE)
6439 expr = build_addr_func (expr, complain);
6440 else if (TREE_CODE (expr) == PTRMEM_CST)
6441 expr = build_address (PTRMEM_CST_MEMBER (expr));
6442 else
6444 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6445 decl = build_address (decl);
6446 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6449 if (expr == error_mark_node)
6450 return error_mark_node;
6452 return build_nop (type, expr);
6455 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6456 If C_CAST_P is true, this reinterpret cast is being done as part of
6457 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6458 indicate whether or not reinterpret_cast was valid. */
6460 static tree
6461 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6462 bool *valid_p, tsubst_flags_t complain)
6464 tree intype;
6466 /* Assume the cast is invalid. */
6467 if (valid_p)
6468 *valid_p = true;
6470 if (type == error_mark_node || error_operand_p (expr))
6471 return error_mark_node;
6473 intype = TREE_TYPE (expr);
6475 /* Save casted types in the function's used types hash table. */
6476 used_types_insert (type);
6478 /* [expr.reinterpret.cast]
6479 An lvalue expression of type T1 can be cast to the type
6480 "reference to T2" if an expression of type "pointer to T1" can be
6481 explicitly converted to the type "pointer to T2" using a
6482 reinterpret_cast. */
6483 if (TREE_CODE (type) == REFERENCE_TYPE)
6485 if (! real_lvalue_p (expr))
6487 if (complain & tf_error)
6488 error ("invalid cast of an rvalue expression of type "
6489 "%qT to type %qT",
6490 intype, type);
6491 return error_mark_node;
6494 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6495 "B" are related class types; the reinterpret_cast does not
6496 adjust the pointer. */
6497 if (TYPE_PTR_P (intype)
6498 && (complain & tf_warning)
6499 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6500 COMPARE_BASE | COMPARE_DERIVED)))
6501 warning (0, "casting %qT to %qT does not dereference pointer",
6502 intype, type);
6504 expr = cp_build_addr_expr (expr, complain);
6506 if (warn_strict_aliasing > 2)
6507 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6509 if (expr != error_mark_node)
6510 expr = build_reinterpret_cast_1
6511 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6512 valid_p, complain);
6513 if (expr != error_mark_node)
6514 /* cp_build_indirect_ref isn't right for rvalue refs. */
6515 expr = convert_from_reference (fold_convert (type, expr));
6516 return expr;
6519 /* As a G++ extension, we consider conversions from member
6520 functions, and pointers to member functions to
6521 pointer-to-function and pointer-to-void types. If
6522 -Wno-pmf-conversions has not been specified,
6523 convert_member_func_to_ptr will issue an error message. */
6524 if ((TYPE_PTRMEMFUNC_P (intype)
6525 || TREE_CODE (intype) == METHOD_TYPE)
6526 && TYPE_PTR_P (type)
6527 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6528 || VOID_TYPE_P (TREE_TYPE (type))))
6529 return convert_member_func_to_ptr (type, expr, complain);
6531 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6532 array-to-pointer, and function-to-pointer conversions are
6533 performed. */
6534 expr = decay_conversion (expr, complain);
6536 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6537 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6538 if (TREE_CODE (expr) == NOP_EXPR
6539 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6540 expr = TREE_OPERAND (expr, 0);
6542 if (error_operand_p (expr))
6543 return error_mark_node;
6545 intype = TREE_TYPE (expr);
6547 /* [expr.reinterpret.cast]
6548 A pointer can be converted to any integral type large enough to
6549 hold it. ... A value of type std::nullptr_t can be converted to
6550 an integral type; the conversion has the same meaning and
6551 validity as a conversion of (void*)0 to the integral type. */
6552 if (CP_INTEGRAL_TYPE_P (type)
6553 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6555 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6557 if (complain & tf_error)
6558 permerror (input_location, "cast from %qT to %qT loses precision",
6559 intype, type);
6560 else
6561 return error_mark_node;
6563 if (NULLPTR_TYPE_P (intype))
6564 return build_int_cst (type, 0);
6566 /* [expr.reinterpret.cast]
6567 A value of integral or enumeration type can be explicitly
6568 converted to a pointer. */
6569 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6570 /* OK */
6572 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6573 || TYPE_PTR_OR_PTRMEM_P (type))
6574 && same_type_p (type, intype))
6575 /* DR 799 */
6576 return fold_if_not_in_template (build_nop (type, expr));
6577 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6578 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6579 return fold_if_not_in_template (build_nop (type, expr));
6580 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6581 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6583 tree sexpr = expr;
6585 if (!c_cast_p
6586 && check_for_casting_away_constness (intype, type,
6587 REINTERPRET_CAST_EXPR,
6588 complain))
6589 return error_mark_node;
6590 /* Warn about possible alignment problems. */
6591 if (STRICT_ALIGNMENT && warn_cast_align
6592 && (complain & tf_warning)
6593 && !VOID_TYPE_P (type)
6594 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6595 && COMPLETE_TYPE_P (TREE_TYPE (type))
6596 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6597 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6598 warning (OPT_Wcast_align, "cast from %qT to %qT "
6599 "increases required alignment of target type", intype, type);
6601 /* We need to strip nops here, because the front end likes to
6602 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6603 STRIP_NOPS (sexpr);
6604 if (warn_strict_aliasing <= 2)
6605 strict_aliasing_warning (intype, type, sexpr);
6607 return fold_if_not_in_template (build_nop (type, expr));
6609 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6610 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6612 if (pedantic && (complain & tf_warning))
6613 /* Only issue a warning, as we have always supported this
6614 where possible, and it is necessary in some cases. DR 195
6615 addresses this issue, but as of 2004/10/26 is still in
6616 drafting. */
6617 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6618 return fold_if_not_in_template (build_nop (type, expr));
6620 else if (TREE_CODE (type) == VECTOR_TYPE)
6621 return fold_if_not_in_template (convert_to_vector (type, expr));
6622 else if (TREE_CODE (intype) == VECTOR_TYPE
6623 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6624 return fold_if_not_in_template (convert_to_integer (type, expr));
6625 else
6627 if (valid_p)
6628 *valid_p = false;
6629 if (complain & tf_error)
6630 error ("invalid cast from type %qT to type %qT", intype, type);
6631 return error_mark_node;
6634 return cp_convert (type, expr, complain);
6637 tree
6638 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6640 tree r;
6642 if (type == error_mark_node || expr == error_mark_node)
6643 return error_mark_node;
6645 if (processing_template_decl)
6647 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6649 if (!TREE_SIDE_EFFECTS (t)
6650 && type_dependent_expression_p (expr))
6651 /* There might turn out to be side effects inside expr. */
6652 TREE_SIDE_EFFECTS (t) = 1;
6653 return convert_from_reference (t);
6656 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6657 /*valid_p=*/NULL, complain);
6658 if (r != error_mark_node)
6659 maybe_warn_about_useless_cast (type, expr, complain);
6660 return r;
6663 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6664 return an appropriate expression. Otherwise, return
6665 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6666 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6667 performing a C-style cast, its value upon return will indicate
6668 whether or not the conversion succeeded. */
6670 static tree
6671 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6672 bool *valid_p)
6674 tree src_type;
6675 tree reference_type;
6677 /* Callers are responsible for handling error_mark_node as a
6678 destination type. */
6679 gcc_assert (dst_type != error_mark_node);
6680 /* In a template, callers should be building syntactic
6681 representations of casts, not using this machinery. */
6682 gcc_assert (!processing_template_decl);
6684 /* Assume the conversion is invalid. */
6685 if (valid_p)
6686 *valid_p = false;
6688 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6690 if (complain & tf_error)
6691 error ("invalid use of const_cast with type %qT, "
6692 "which is not a pointer, "
6693 "reference, nor a pointer-to-data-member type", dst_type);
6694 return error_mark_node;
6697 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6699 if (complain & tf_error)
6700 error ("invalid use of const_cast with type %qT, which is a pointer "
6701 "or reference to a function type", dst_type);
6702 return error_mark_node;
6705 /* Save casted types in the function's used types hash table. */
6706 used_types_insert (dst_type);
6708 src_type = TREE_TYPE (expr);
6709 /* Expressions do not really have reference types. */
6710 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6711 src_type = TREE_TYPE (src_type);
6713 /* [expr.const.cast]
6715 For two object types T1 and T2, if a pointer to T1 can be explicitly
6716 converted to the type "pointer to T2" using a const_cast, then the
6717 following conversions can also be made:
6719 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6720 type T2 using the cast const_cast<T2&>;
6722 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6723 type T2 using the cast const_cast<T2&&>; and
6725 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6726 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6728 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6730 reference_type = dst_type;
6731 if (!TYPE_REF_IS_RVALUE (dst_type)
6732 ? real_lvalue_p (expr)
6733 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6734 ? lvalue_p (expr)
6735 : lvalue_or_rvalue_with_address_p (expr)))
6736 /* OK. */;
6737 else
6739 if (complain & tf_error)
6740 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6741 src_type, dst_type);
6742 return error_mark_node;
6744 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6745 src_type = build_pointer_type (src_type);
6747 else
6749 reference_type = NULL_TREE;
6750 /* If the destination type is not a reference type, the
6751 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6752 conversions are performed. */
6753 src_type = type_decays_to (src_type);
6754 if (src_type == error_mark_node)
6755 return error_mark_node;
6758 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
6760 if (comp_ptr_ttypes_const (dst_type, src_type))
6762 if (valid_p)
6764 *valid_p = true;
6765 /* This cast is actually a C-style cast. Issue a warning if
6766 the user is making a potentially unsafe cast. */
6767 check_for_casting_away_constness (src_type, dst_type,
6768 CAST_EXPR, complain);
6770 if (reference_type)
6772 expr = cp_build_addr_expr (expr, complain);
6773 if (expr == error_mark_node)
6774 return error_mark_node;
6775 expr = build_nop (reference_type, expr);
6776 return convert_from_reference (expr);
6778 else
6780 expr = decay_conversion (expr, complain);
6781 if (expr == error_mark_node)
6782 return error_mark_node;
6784 /* build_c_cast puts on a NOP_EXPR to make the result not an
6785 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6786 non-lvalue context. */
6787 if (TREE_CODE (expr) == NOP_EXPR
6788 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6789 expr = TREE_OPERAND (expr, 0);
6790 return build_nop (dst_type, expr);
6793 else if (valid_p
6794 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6795 TREE_TYPE (src_type)))
6796 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6797 complain);
6800 if (complain & tf_error)
6801 error ("invalid const_cast from type %qT to type %qT",
6802 src_type, dst_type);
6803 return error_mark_node;
6806 tree
6807 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6809 tree r;
6811 if (type == error_mark_node || error_operand_p (expr))
6812 return error_mark_node;
6814 if (processing_template_decl)
6816 tree t = build_min (CONST_CAST_EXPR, type, expr);
6818 if (!TREE_SIDE_EFFECTS (t)
6819 && type_dependent_expression_p (expr))
6820 /* There might turn out to be side effects inside expr. */
6821 TREE_SIDE_EFFECTS (t) = 1;
6822 return convert_from_reference (t);
6825 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
6826 if (r != error_mark_node)
6827 maybe_warn_about_useless_cast (type, expr, complain);
6828 return r;
6831 /* Like cp_build_c_cast, but for the c-common bits. */
6833 tree
6834 build_c_cast (location_t /*loc*/, tree type, tree expr)
6836 return cp_build_c_cast (type, expr, tf_warning_or_error);
6839 /* Build an expression representing an explicit C-style cast to type
6840 TYPE of expression EXPR. */
6842 tree
6843 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6845 tree value = expr;
6846 tree result;
6847 bool valid_p;
6849 if (type == error_mark_node || error_operand_p (expr))
6850 return error_mark_node;
6852 if (processing_template_decl)
6854 tree t = build_min (CAST_EXPR, type,
6855 tree_cons (NULL_TREE, value, NULL_TREE));
6856 /* We don't know if it will or will not have side effects. */
6857 TREE_SIDE_EFFECTS (t) = 1;
6858 return convert_from_reference (t);
6861 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6862 'Class') should always be retained, because this information aids
6863 in method lookup. */
6864 if (objc_is_object_ptr (type)
6865 && objc_is_object_ptr (TREE_TYPE (expr)))
6866 return build_nop (type, expr);
6868 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6869 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6870 if (TREE_CODE (type) != REFERENCE_TYPE
6871 && TREE_CODE (value) == NOP_EXPR
6872 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6873 value = TREE_OPERAND (value, 0);
6875 if (TREE_CODE (type) == ARRAY_TYPE)
6877 /* Allow casting from T1* to T2[] because Cfront allows it.
6878 NIHCL uses it. It is not valid ISO C++ however. */
6879 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6881 if (complain & tf_error)
6882 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6883 else
6884 return error_mark_node;
6885 type = build_pointer_type (TREE_TYPE (type));
6887 else
6889 if (complain & tf_error)
6890 error ("ISO C++ forbids casting to an array type %qT", type);
6891 return error_mark_node;
6895 if (TREE_CODE (type) == FUNCTION_TYPE
6896 || TREE_CODE (type) == METHOD_TYPE)
6898 if (complain & tf_error)
6899 error ("invalid cast to function type %qT", type);
6900 return error_mark_node;
6903 if (TREE_CODE (type) == POINTER_TYPE
6904 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
6905 /* Casting to an integer of smaller size is an error detected elsewhere. */
6906 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
6907 /* Don't warn about converting any constant. */
6908 && !TREE_CONSTANT (value))
6909 warning_at (input_location, OPT_Wint_to_pointer_cast,
6910 "cast to pointer from integer of different size");
6912 /* A C-style cast can be a const_cast. */
6913 result = build_const_cast_1 (type, value, complain & tf_warning,
6914 &valid_p);
6915 if (valid_p)
6917 if (result != error_mark_node)
6918 maybe_warn_about_useless_cast (type, value, complain);
6919 return result;
6922 /* Or a static cast. */
6923 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
6924 &valid_p, complain);
6925 /* Or a reinterpret_cast. */
6926 if (!valid_p)
6927 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
6928 &valid_p, complain);
6929 /* The static_cast or reinterpret_cast may be followed by a
6930 const_cast. */
6931 if (valid_p
6932 /* A valid cast may result in errors if, for example, a
6933 conversion to an ambiguous base class is required. */
6934 && !error_operand_p (result))
6936 tree result_type;
6938 maybe_warn_about_useless_cast (type, value, complain);
6940 /* Non-class rvalues always have cv-unqualified type. */
6941 if (!CLASS_TYPE_P (type))
6942 type = TYPE_MAIN_VARIANT (type);
6943 result_type = TREE_TYPE (result);
6944 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
6945 result_type = TYPE_MAIN_VARIANT (result_type);
6946 /* If the type of RESULT does not match TYPE, perform a
6947 const_cast to make it match. If the static_cast or
6948 reinterpret_cast succeeded, we will differ by at most
6949 cv-qualification, so the follow-on const_cast is guaranteed
6950 to succeed. */
6951 if (!same_type_p (non_reference (type), non_reference (result_type)))
6953 result = build_const_cast_1 (type, result, false, &valid_p);
6954 gcc_assert (valid_p);
6956 return result;
6959 return error_mark_node;
6962 /* For use from the C common bits. */
6963 tree
6964 build_modify_expr (location_t /*location*/,
6965 tree lhs, tree /*lhs_origtype*/,
6966 enum tree_code modifycode,
6967 location_t /*rhs_location*/, tree rhs,
6968 tree /*rhs_origtype*/)
6970 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
6973 /* Build an assignment expression of lvalue LHS from value RHS.
6974 MODIFYCODE is the code for a binary operator that we use
6975 to combine the old value of LHS with RHS to get the new value.
6976 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6978 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
6980 tree
6981 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6982 tsubst_flags_t complain)
6984 tree result;
6985 tree newrhs = rhs;
6986 tree lhstype = TREE_TYPE (lhs);
6987 tree olhstype = lhstype;
6988 bool plain_assign = (modifycode == NOP_EXPR);
6990 /* Avoid duplicate error messages from operands that had errors. */
6991 if (error_operand_p (lhs) || error_operand_p (rhs))
6992 return error_mark_node;
6994 /* Handle control structure constructs used as "lvalues". */
6995 switch (TREE_CODE (lhs))
6997 /* Handle --foo = 5; as these are valid constructs in C++. */
6998 case PREDECREMENT_EXPR:
6999 case PREINCREMENT_EXPR:
7000 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7001 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7002 stabilize_reference (TREE_OPERAND (lhs, 0)),
7003 TREE_OPERAND (lhs, 1));
7004 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7005 modifycode, rhs, complain);
7006 if (newrhs == error_mark_node)
7007 return error_mark_node;
7008 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7010 /* Handle (a, b) used as an "lvalue". */
7011 case COMPOUND_EXPR:
7012 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7013 modifycode, rhs, complain);
7014 if (newrhs == error_mark_node)
7015 return error_mark_node;
7016 return build2 (COMPOUND_EXPR, lhstype,
7017 TREE_OPERAND (lhs, 0), newrhs);
7019 case MODIFY_EXPR:
7020 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7021 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7022 stabilize_reference (TREE_OPERAND (lhs, 0)),
7023 TREE_OPERAND (lhs, 1));
7024 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7025 complain);
7026 if (newrhs == error_mark_node)
7027 return error_mark_node;
7028 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7030 case MIN_EXPR:
7031 case MAX_EXPR:
7032 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7033 when neither operand has side-effects. */
7034 if (!lvalue_or_else (lhs, lv_assign, complain))
7035 return error_mark_node;
7037 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7038 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7040 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7041 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7042 boolean_type_node,
7043 TREE_OPERAND (lhs, 0),
7044 TREE_OPERAND (lhs, 1)),
7045 TREE_OPERAND (lhs, 0),
7046 TREE_OPERAND (lhs, 1));
7047 /* Fall through. */
7049 /* Handle (a ? b : c) used as an "lvalue". */
7050 case COND_EXPR:
7052 /* Produce (a ? (b = rhs) : (c = rhs))
7053 except that the RHS goes through a save-expr
7054 so the code to compute it is only emitted once. */
7055 tree cond;
7056 tree preeval = NULL_TREE;
7058 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7060 if (complain & tf_error)
7061 error ("void value not ignored as it ought to be");
7062 return error_mark_node;
7065 rhs = stabilize_expr (rhs, &preeval);
7067 /* Check this here to avoid odd errors when trying to convert
7068 a throw to the type of the COND_EXPR. */
7069 if (!lvalue_or_else (lhs, lv_assign, complain))
7070 return error_mark_node;
7072 cond = build_conditional_expr
7073 (TREE_OPERAND (lhs, 0),
7074 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7075 modifycode, rhs, complain),
7076 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7077 modifycode, rhs, complain),
7078 complain);
7080 if (cond == error_mark_node)
7081 return cond;
7082 /* Make sure the code to compute the rhs comes out
7083 before the split. */
7084 if (preeval)
7085 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7086 return cond;
7089 default:
7090 break;
7093 if (modifycode == INIT_EXPR)
7095 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7096 /* Do the default thing. */;
7097 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7099 /* Compound literal. */
7100 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7101 /* Call convert to generate an error; see PR 11063. */
7102 rhs = convert (lhstype, rhs);
7103 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7104 TREE_SIDE_EFFECTS (result) = 1;
7105 return result;
7107 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7108 /* Do the default thing. */;
7109 else
7111 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7112 result = build_special_member_call (lhs, complete_ctor_identifier,
7113 &rhs_vec, lhstype, LOOKUP_NORMAL,
7114 complain);
7115 release_tree_vector (rhs_vec);
7116 if (result == NULL_TREE)
7117 return error_mark_node;
7118 return result;
7121 else
7123 lhs = require_complete_type_sfinae (lhs, complain);
7124 if (lhs == error_mark_node)
7125 return error_mark_node;
7127 if (modifycode == NOP_EXPR)
7129 if (c_dialect_objc ())
7131 result = objc_maybe_build_modify_expr (lhs, rhs);
7132 if (result)
7133 return result;
7136 /* `operator=' is not an inheritable operator. */
7137 if (! MAYBE_CLASS_TYPE_P (lhstype))
7138 /* Do the default thing. */;
7139 else
7141 result = build_new_op (input_location, MODIFY_EXPR,
7142 LOOKUP_NORMAL, lhs, rhs,
7143 make_node (NOP_EXPR), /*overload=*/NULL,
7144 complain);
7145 if (result == NULL_TREE)
7146 return error_mark_node;
7147 return result;
7149 lhstype = olhstype;
7151 else
7153 tree init = NULL_TREE;
7155 /* A binary op has been requested. Combine the old LHS
7156 value with the RHS producing the value we should actually
7157 store into the LHS. */
7158 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7159 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7160 || MAYBE_CLASS_TYPE_P (lhstype)));
7162 /* Preevaluate the RHS to make sure its evaluation is complete
7163 before the lvalue-to-rvalue conversion of the LHS:
7165 [expr.ass] With respect to an indeterminately-sequenced
7166 function call, the operation of a compound assignment is a
7167 single evaluation. [ Note: Therefore, a function call shall
7168 not intervene between the lvalue-to-rvalue conversion and the
7169 side effect associated with any single compound assignment
7170 operator. -- end note ] */
7171 lhs = stabilize_reference (lhs);
7172 if (TREE_SIDE_EFFECTS (rhs))
7173 rhs = mark_rvalue_use (rhs);
7174 rhs = stabilize_expr (rhs, &init);
7175 newrhs = cp_build_binary_op (input_location,
7176 modifycode, lhs, rhs,
7177 complain);
7178 if (newrhs == error_mark_node)
7180 if (complain & tf_error)
7181 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7182 TREE_TYPE (lhs), TREE_TYPE (rhs));
7183 return error_mark_node;
7186 if (init)
7187 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7189 /* Now it looks like a plain assignment. */
7190 modifycode = NOP_EXPR;
7191 if (c_dialect_objc ())
7193 result = objc_maybe_build_modify_expr (lhs, newrhs);
7194 if (result)
7195 return result;
7198 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7199 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7202 /* The left-hand side must be an lvalue. */
7203 if (!lvalue_or_else (lhs, lv_assign, complain))
7204 return error_mark_node;
7206 /* Warn about modifying something that is `const'. Don't warn if
7207 this is initialization. */
7208 if (modifycode != INIT_EXPR
7209 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7210 /* Functions are not modifiable, even though they are
7211 lvalues. */
7212 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7213 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7214 /* If it's an aggregate and any field is const, then it is
7215 effectively const. */
7216 || (CLASS_TYPE_P (lhstype)
7217 && C_TYPE_FIELDS_READONLY (lhstype))))
7219 if (complain & tf_error)
7220 cxx_readonly_error (lhs, lv_assign);
7221 else
7222 return error_mark_node;
7225 /* If storing into a structure or union member, it may have been given a
7226 lowered bitfield type. We need to convert to the declared type first,
7227 so retrieve it now. */
7229 olhstype = unlowered_expr_type (lhs);
7231 /* Convert new value to destination type. */
7233 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7235 int from_array;
7237 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7239 if (modifycode != INIT_EXPR)
7241 if (complain & tf_error)
7242 error ("assigning to an array from an initializer list");
7243 return error_mark_node;
7245 if (check_array_initializer (lhs, lhstype, newrhs))
7246 return error_mark_node;
7247 newrhs = digest_init (lhstype, newrhs, complain);
7248 if (newrhs == error_mark_node)
7249 return error_mark_node;
7252 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7253 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7255 if (complain & tf_error)
7256 error ("incompatible types in assignment of %qT to %qT",
7257 TREE_TYPE (rhs), lhstype);
7258 return error_mark_node;
7261 /* Allow array assignment in compiler-generated code. */
7262 else if (!current_function_decl
7263 || !DECL_DEFAULTED_FN (current_function_decl))
7265 /* This routine is used for both initialization and assignment.
7266 Make sure the diagnostic message differentiates the context. */
7267 if (complain & tf_error)
7269 if (modifycode == INIT_EXPR)
7270 error ("array used as initializer");
7271 else
7272 error ("invalid array assignment");
7274 return error_mark_node;
7277 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7278 ? 1 + (modifycode != INIT_EXPR): 0;
7279 return build_vec_init (lhs, NULL_TREE, newrhs,
7280 /*explicit_value_init_p=*/false,
7281 from_array, complain);
7284 if (modifycode == INIT_EXPR)
7285 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7286 LOOKUP_ONLYCONVERTING. */
7287 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7288 ICR_INIT, NULL_TREE, 0,
7289 complain);
7290 else
7291 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7292 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7294 if (!same_type_p (lhstype, olhstype))
7295 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7297 if (modifycode != INIT_EXPR)
7299 if (TREE_CODE (newrhs) == CALL_EXPR
7300 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7301 newrhs = build_cplus_new (lhstype, newrhs, complain);
7303 /* Can't initialize directly from a TARGET_EXPR, since that would
7304 cause the lhs to be constructed twice, and possibly result in
7305 accidental self-initialization. So we force the TARGET_EXPR to be
7306 expanded without a target. */
7307 if (TREE_CODE (newrhs) == TARGET_EXPR)
7308 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7309 TREE_OPERAND (newrhs, 0));
7312 if (newrhs == error_mark_node)
7313 return error_mark_node;
7315 if (c_dialect_objc () && flag_objc_gc)
7317 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7319 if (result)
7320 return result;
7323 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7324 lhstype, lhs, newrhs);
7326 TREE_SIDE_EFFECTS (result) = 1;
7327 if (!plain_assign)
7328 TREE_NO_WARNING (result) = 1;
7330 return result;
7333 tree
7334 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7335 tree rhs, tsubst_flags_t complain)
7337 if (processing_template_decl)
7338 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7339 build_min_nt_loc (loc, modifycode, NULL_TREE,
7340 NULL_TREE), rhs);
7342 if (modifycode != NOP_EXPR)
7344 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7345 make_node (modifycode), /*overload=*/NULL,
7346 complain);
7347 if (rval)
7349 TREE_NO_WARNING (rval) = 1;
7350 return rval;
7353 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7356 /* Helper function for get_delta_difference which assumes FROM is a base
7357 class of TO. Returns a delta for the conversion of pointer-to-member
7358 of FROM to pointer-to-member of TO. If the conversion is invalid and
7359 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7360 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7361 If C_CAST_P is true, this conversion is taking place as part of a
7362 C-style cast. */
7364 static tree
7365 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7366 tsubst_flags_t complain)
7368 tree binfo;
7369 base_kind kind;
7371 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7372 &kind, complain);
7374 if (binfo == error_mark_node)
7376 if (!(complain & tf_error))
7377 return error_mark_node;
7379 error (" in pointer to member function conversion");
7380 return size_zero_node;
7382 else if (binfo)
7384 if (kind != bk_via_virtual)
7385 return BINFO_OFFSET (binfo);
7386 else
7387 /* FROM is a virtual base class of TO. Issue an error or warning
7388 depending on whether or not this is a reinterpret cast. */
7390 if (!(complain & tf_error))
7391 return error_mark_node;
7393 error ("pointer to member conversion via virtual base %qT",
7394 BINFO_TYPE (binfo_from_vbase (binfo)));
7396 return size_zero_node;
7399 else
7400 return NULL_TREE;
7403 /* Get difference in deltas for different pointer to member function
7404 types. If the conversion is invalid and tf_error is not set in
7405 COMPLAIN, returns error_mark_node, otherwise returns an integer
7406 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7407 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7408 conversions as well. If C_CAST_P is true this conversion is taking
7409 place as part of a C-style cast.
7411 Note that the naming of FROM and TO is kind of backwards; the return
7412 value is what we add to a TO in order to get a FROM. They are named
7413 this way because we call this function to find out how to convert from
7414 a pointer to member of FROM to a pointer to member of TO. */
7416 static tree
7417 get_delta_difference (tree from, tree to,
7418 bool allow_inverse_p,
7419 bool c_cast_p, tsubst_flags_t complain)
7421 tree result;
7423 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7424 /* Pointer to member of incomplete class is permitted*/
7425 result = size_zero_node;
7426 else
7427 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7429 if (result == error_mark_node)
7430 return error_mark_node;
7432 if (!result)
7434 if (!allow_inverse_p)
7436 if (!(complain & tf_error))
7437 return error_mark_node;
7439 error_not_base_type (from, to);
7440 error (" in pointer to member conversion");
7441 result = size_zero_node;
7443 else
7445 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7447 if (result == error_mark_node)
7448 return error_mark_node;
7450 if (result)
7451 result = size_diffop_loc (input_location,
7452 size_zero_node, result);
7453 else
7455 if (!(complain & tf_error))
7456 return error_mark_node;
7458 error_not_base_type (from, to);
7459 error (" in pointer to member conversion");
7460 result = size_zero_node;
7465 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7466 result));
7469 /* Return a constructor for the pointer-to-member-function TYPE using
7470 the other components as specified. */
7472 tree
7473 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7475 tree u = NULL_TREE;
7476 tree delta_field;
7477 tree pfn_field;
7478 vec<constructor_elt, va_gc> *v;
7480 /* Pull the FIELD_DECLs out of the type. */
7481 pfn_field = TYPE_FIELDS (type);
7482 delta_field = DECL_CHAIN (pfn_field);
7484 /* Make sure DELTA has the type we want. */
7485 delta = convert_and_check (delta_type_node, delta);
7487 /* Convert to the correct target type if necessary. */
7488 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7490 /* Finish creating the initializer. */
7491 vec_alloc (v, 2);
7492 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7493 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7494 u = build_constructor (type, v);
7495 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7496 TREE_STATIC (u) = (TREE_CONSTANT (u)
7497 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7498 != NULL_TREE)
7499 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7500 != NULL_TREE));
7501 return u;
7504 /* Build a constructor for a pointer to member function. It can be
7505 used to initialize global variables, local variable, or used
7506 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7507 want to be.
7509 If FORCE is nonzero, then force this conversion, even if
7510 we would rather not do it. Usually set when using an explicit
7511 cast. A C-style cast is being processed iff C_CAST_P is true.
7513 Return error_mark_node, if something goes wrong. */
7515 tree
7516 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7517 tsubst_flags_t complain)
7519 tree fn;
7520 tree pfn_type;
7521 tree to_type;
7523 if (error_operand_p (pfn))
7524 return error_mark_node;
7526 pfn_type = TREE_TYPE (pfn);
7527 to_type = build_ptrmemfunc_type (type);
7529 /* Handle multiple conversions of pointer to member functions. */
7530 if (TYPE_PTRMEMFUNC_P (pfn_type))
7532 tree delta = NULL_TREE;
7533 tree npfn = NULL_TREE;
7534 tree n;
7536 if (!force
7537 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7538 LOOKUP_NORMAL, complain))
7539 error ("invalid conversion to type %qT from type %qT",
7540 to_type, pfn_type);
7542 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7543 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7544 force,
7545 c_cast_p, complain);
7546 if (n == error_mark_node)
7547 return error_mark_node;
7549 /* We don't have to do any conversion to convert a
7550 pointer-to-member to its own type. But, we don't want to
7551 just return a PTRMEM_CST if there's an explicit cast; that
7552 cast should make the expression an invalid template argument. */
7553 if (TREE_CODE (pfn) != PTRMEM_CST)
7555 if (same_type_p (to_type, pfn_type))
7556 return pfn;
7557 else if (integer_zerop (n))
7558 return build_reinterpret_cast (to_type, pfn,
7559 complain);
7562 if (TREE_SIDE_EFFECTS (pfn))
7563 pfn = save_expr (pfn);
7565 /* Obtain the function pointer and the current DELTA. */
7566 if (TREE_CODE (pfn) == PTRMEM_CST)
7567 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7568 else
7570 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7571 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7574 /* Just adjust the DELTA field. */
7575 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7576 (TREE_TYPE (delta), ptrdiff_type_node));
7577 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7578 n = cp_build_binary_op (input_location,
7579 LSHIFT_EXPR, n, integer_one_node,
7580 complain);
7581 delta = cp_build_binary_op (input_location,
7582 PLUS_EXPR, delta, n, complain);
7583 return build_ptrmemfunc1 (to_type, delta, npfn);
7586 /* Handle null pointer to member function conversions. */
7587 if (null_ptr_cst_p (pfn))
7589 pfn = build_c_cast (input_location, type, pfn);
7590 return build_ptrmemfunc1 (to_type,
7591 integer_zero_node,
7592 pfn);
7595 if (type_unknown_p (pfn))
7596 return instantiate_type (type, pfn, complain);
7598 fn = TREE_OPERAND (pfn, 0);
7599 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7600 /* In a template, we will have preserved the
7601 OFFSET_REF. */
7602 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7603 return make_ptrmem_cst (to_type, fn);
7606 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7607 given by CST.
7609 ??? There is no consistency as to the types returned for the above
7610 values. Some code acts as if it were a sizetype and some as if it were
7611 integer_type_node. */
7613 void
7614 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7616 tree type = TREE_TYPE (cst);
7617 tree fn = PTRMEM_CST_MEMBER (cst);
7618 tree ptr_class, fn_class;
7620 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7622 /* The class that the function belongs to. */
7623 fn_class = DECL_CONTEXT (fn);
7625 /* The class that we're creating a pointer to member of. */
7626 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7628 /* First, calculate the adjustment to the function's class. */
7629 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7630 /*c_cast_p=*/0, tf_warning_or_error);
7632 if (!DECL_VIRTUAL_P (fn))
7633 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7634 build_addr_func (fn, tf_warning_or_error));
7635 else
7637 /* If we're dealing with a virtual function, we have to adjust 'this'
7638 again, to point to the base which provides the vtable entry for
7639 fn; the call will do the opposite adjustment. */
7640 tree orig_class = DECL_CONTEXT (fn);
7641 tree binfo = binfo_or_else (orig_class, fn_class);
7642 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7643 *delta, BINFO_OFFSET (binfo));
7644 *delta = fold_if_not_in_template (*delta);
7646 /* We set PFN to the vtable offset at which the function can be
7647 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7648 case delta is shifted left, and then incremented). */
7649 *pfn = DECL_VINDEX (fn);
7650 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7651 TYPE_SIZE_UNIT (vtable_entry_type));
7652 *pfn = fold_if_not_in_template (*pfn);
7654 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7656 case ptrmemfunc_vbit_in_pfn:
7657 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7658 integer_one_node);
7659 *pfn = fold_if_not_in_template (*pfn);
7660 break;
7662 case ptrmemfunc_vbit_in_delta:
7663 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7664 *delta, integer_one_node);
7665 *delta = fold_if_not_in_template (*delta);
7666 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7667 *delta, integer_one_node);
7668 *delta = fold_if_not_in_template (*delta);
7669 break;
7671 default:
7672 gcc_unreachable ();
7675 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7676 *pfn = fold_if_not_in_template (*pfn);
7680 /* Return an expression for PFN from the pointer-to-member function
7681 given by T. */
7683 static tree
7684 pfn_from_ptrmemfunc (tree t)
7686 if (TREE_CODE (t) == PTRMEM_CST)
7688 tree delta;
7689 tree pfn;
7691 expand_ptrmemfunc_cst (t, &delta, &pfn);
7692 if (pfn)
7693 return pfn;
7696 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7699 /* Return an expression for DELTA from the pointer-to-member function
7700 given by T. */
7702 static tree
7703 delta_from_ptrmemfunc (tree t)
7705 if (TREE_CODE (t) == PTRMEM_CST)
7707 tree delta;
7708 tree pfn;
7710 expand_ptrmemfunc_cst (t, &delta, &pfn);
7711 if (delta)
7712 return delta;
7715 return build_ptrmemfunc_access_expr (t, delta_identifier);
7718 /* Convert value RHS to type TYPE as preparation for an assignment to
7719 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7720 implicit conversion is. If FNDECL is non-NULL, we are doing the
7721 conversion in order to pass the PARMNUMth argument of FNDECL.
7722 If FNDECL is NULL, we are doing the conversion in function pointer
7723 argument passing, conversion in initialization, etc. */
7725 static tree
7726 convert_for_assignment (tree type, tree rhs,
7727 impl_conv_rhs errtype, tree fndecl, int parmnum,
7728 tsubst_flags_t complain, int flags)
7730 tree rhstype;
7731 enum tree_code coder;
7733 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7734 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7735 rhs = TREE_OPERAND (rhs, 0);
7737 rhstype = TREE_TYPE (rhs);
7738 coder = TREE_CODE (rhstype);
7740 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7741 && vector_types_convertible_p (type, rhstype, true))
7743 rhs = mark_rvalue_use (rhs);
7744 return convert (type, rhs);
7747 if (rhs == error_mark_node || rhstype == error_mark_node)
7748 return error_mark_node;
7749 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7750 return error_mark_node;
7752 /* The RHS of an assignment cannot have void type. */
7753 if (coder == VOID_TYPE)
7755 if (complain & tf_error)
7756 error ("void value not ignored as it ought to be");
7757 return error_mark_node;
7760 if (c_dialect_objc ())
7762 int parmno;
7763 tree selector;
7764 tree rname = fndecl;
7766 switch (errtype)
7768 case ICR_ASSIGN:
7769 parmno = -1;
7770 break;
7771 case ICR_INIT:
7772 parmno = -2;
7773 break;
7774 default:
7775 selector = objc_message_selector ();
7776 parmno = parmnum;
7777 if (selector && parmno > 1)
7779 rname = selector;
7780 parmno -= 1;
7784 if (objc_compare_types (type, rhstype, parmno, rname))
7786 rhs = mark_rvalue_use (rhs);
7787 return convert (type, rhs);
7791 /* [expr.ass]
7793 The expression is implicitly converted (clause _conv_) to the
7794 cv-unqualified type of the left operand.
7796 We allow bad conversions here because by the time we get to this point
7797 we are committed to doing the conversion. If we end up doing a bad
7798 conversion, convert_like will complain. */
7799 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
7801 /* When -Wno-pmf-conversions is use, we just silently allow
7802 conversions from pointers-to-members to plain pointers. If
7803 the conversion doesn't work, cp_convert will complain. */
7804 if (!warn_pmf2ptr
7805 && TYPE_PTR_P (type)
7806 && TYPE_PTRMEMFUNC_P (rhstype))
7807 rhs = cp_convert (strip_top_quals (type), rhs, complain);
7808 else
7810 if (complain & tf_error)
7812 /* If the right-hand side has unknown type, then it is an
7813 overloaded function. Call instantiate_type to get error
7814 messages. */
7815 if (rhstype == unknown_type_node)
7816 instantiate_type (type, rhs, tf_warning_or_error);
7817 else if (fndecl)
7818 error ("cannot convert %qT to %qT for argument %qP to %qD",
7819 rhstype, type, parmnum, fndecl);
7820 else
7821 switch (errtype)
7823 case ICR_DEFAULT_ARGUMENT:
7824 error ("cannot convert %qT to %qT in default argument",
7825 rhstype, type);
7826 break;
7827 case ICR_ARGPASS:
7828 error ("cannot convert %qT to %qT in argument passing",
7829 rhstype, type);
7830 break;
7831 case ICR_CONVERTING:
7832 error ("cannot convert %qT to %qT",
7833 rhstype, type);
7834 break;
7835 case ICR_INIT:
7836 error ("cannot convert %qT to %qT in initialization",
7837 rhstype, type);
7838 break;
7839 case ICR_RETURN:
7840 error ("cannot convert %qT to %qT in return",
7841 rhstype, type);
7842 break;
7843 case ICR_ASSIGN:
7844 error ("cannot convert %qT to %qT in assignment",
7845 rhstype, type);
7846 break;
7847 default:
7848 gcc_unreachable();
7851 return error_mark_node;
7854 if (warn_suggest_attribute_format)
7856 const enum tree_code codel = TREE_CODE (type);
7857 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7858 && coder == codel
7859 && check_missing_format_attribute (type, rhstype)
7860 && (complain & tf_warning))
7861 switch (errtype)
7863 case ICR_ARGPASS:
7864 case ICR_DEFAULT_ARGUMENT:
7865 if (fndecl)
7866 warning (OPT_Wsuggest_attribute_format,
7867 "parameter %qP of %qD might be a candidate "
7868 "for a format attribute", parmnum, fndecl);
7869 else
7870 warning (OPT_Wsuggest_attribute_format,
7871 "parameter might be a candidate "
7872 "for a format attribute");
7873 break;
7874 case ICR_CONVERTING:
7875 warning (OPT_Wsuggest_attribute_format,
7876 "target of conversion might be a candidate "
7877 "for a format attribute");
7878 break;
7879 case ICR_INIT:
7880 warning (OPT_Wsuggest_attribute_format,
7881 "target of initialization might be a candidate "
7882 "for a format attribute");
7883 break;
7884 case ICR_RETURN:
7885 warning (OPT_Wsuggest_attribute_format,
7886 "return type might be a candidate "
7887 "for a format attribute");
7888 break;
7889 case ICR_ASSIGN:
7890 warning (OPT_Wsuggest_attribute_format,
7891 "left-hand side of assignment might be a candidate "
7892 "for a format attribute");
7893 break;
7894 default:
7895 gcc_unreachable();
7899 /* If -Wparentheses, warn about a = b = c when a has type bool and b
7900 does not. */
7901 if (warn_parentheses
7902 && TREE_CODE (type) == BOOLEAN_TYPE
7903 && TREE_CODE (rhs) == MODIFY_EXPR
7904 && !TREE_NO_WARNING (rhs)
7905 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7906 && (complain & tf_warning))
7908 location_t loc = EXPR_LOC_OR_HERE (rhs);
7910 warning_at (loc, OPT_Wparentheses,
7911 "suggest parentheses around assignment used as truth value");
7912 TREE_NO_WARNING (rhs) = 1;
7915 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
7916 complain, flags);
7919 /* Convert RHS to be of type TYPE.
7920 If EXP is nonzero, it is the target of the initialization.
7921 ERRTYPE indicates what kind of error the implicit conversion is.
7923 Two major differences between the behavior of
7924 `convert_for_assignment' and `convert_for_initialization'
7925 are that references are bashed in the former, while
7926 copied in the latter, and aggregates are assigned in
7927 the former (operator=) while initialized in the
7928 latter (X(X&)).
7930 If using constructor make sure no conversion operator exists, if one does
7931 exist, an ambiguity exists. */
7933 tree
7934 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
7935 impl_conv_rhs errtype, tree fndecl, int parmnum,
7936 tsubst_flags_t complain)
7938 enum tree_code codel = TREE_CODE (type);
7939 tree rhstype;
7940 enum tree_code coder;
7942 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7943 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7944 if (TREE_CODE (rhs) == NOP_EXPR
7945 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7946 && codel != REFERENCE_TYPE)
7947 rhs = TREE_OPERAND (rhs, 0);
7949 if (type == error_mark_node
7950 || rhs == error_mark_node
7951 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7952 return error_mark_node;
7954 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7955 && TREE_CODE (type) != ARRAY_TYPE
7956 && (TREE_CODE (type) != REFERENCE_TYPE
7957 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7958 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7959 && (TREE_CODE (type) != REFERENCE_TYPE
7960 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7961 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7962 rhs = decay_conversion (rhs, complain);
7964 rhstype = TREE_TYPE (rhs);
7965 coder = TREE_CODE (rhstype);
7967 if (coder == ERROR_MARK)
7968 return error_mark_node;
7970 /* We accept references to incomplete types, so we can
7971 return here before checking if RHS is of complete type. */
7973 if (codel == REFERENCE_TYPE)
7975 /* This should eventually happen in convert_arguments. */
7976 int savew = 0, savee = 0;
7978 if (fndecl)
7979 savew = warningcount, savee = errorcount;
7980 rhs = initialize_reference (type, rhs, flags, complain);
7981 if (fndecl)
7983 if (warningcount > savew)
7984 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
7985 else if (errorcount > savee)
7986 error ("in passing argument %P of %q+D", parmnum, fndecl);
7988 return rhs;
7991 if (exp != 0)
7992 exp = require_complete_type_sfinae (exp, complain);
7993 if (exp == error_mark_node)
7994 return error_mark_node;
7996 rhstype = non_reference (rhstype);
7998 type = complete_type (type);
8000 if (DIRECT_INIT_EXPR_P (type, rhs))
8001 /* Don't try to do copy-initialization if we already have
8002 direct-initialization. */
8003 return rhs;
8005 if (MAYBE_CLASS_TYPE_P (type))
8006 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8008 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8009 complain, flags);
8012 /* If RETVAL is the address of, or a reference to, a local variable or
8013 temporary give an appropriate warning. */
8015 static void
8016 maybe_warn_about_returning_address_of_local (tree retval)
8018 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8019 tree whats_returned = retval;
8021 for (;;)
8023 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8024 whats_returned = TREE_OPERAND (whats_returned, 1);
8025 else if (CONVERT_EXPR_P (whats_returned)
8026 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8027 whats_returned = TREE_OPERAND (whats_returned, 0);
8028 else
8029 break;
8032 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8033 return;
8034 whats_returned = TREE_OPERAND (whats_returned, 0);
8036 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8038 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8039 || TREE_CODE (whats_returned) == TARGET_EXPR)
8041 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8042 return;
8044 if (TREE_CODE (whats_returned) == VAR_DECL
8045 && DECL_NAME (whats_returned)
8046 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8048 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8049 return;
8053 while (TREE_CODE (whats_returned) == COMPONENT_REF
8054 || TREE_CODE (whats_returned) == ARRAY_REF)
8055 whats_returned = TREE_OPERAND (whats_returned, 0);
8057 if (DECL_P (whats_returned)
8058 && DECL_NAME (whats_returned)
8059 && DECL_FUNCTION_SCOPE_P (whats_returned)
8060 && !(TREE_STATIC (whats_returned)
8061 || TREE_PUBLIC (whats_returned)))
8063 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8064 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8065 whats_returned);
8066 else
8067 warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
8068 whats_returned);
8069 return;
8073 /* Check that returning RETVAL from the current function is valid.
8074 Return an expression explicitly showing all conversions required to
8075 change RETVAL into the function return type, and to assign it to
8076 the DECL_RESULT for the function. Set *NO_WARNING to true if
8077 code reaches end of non-void function warning shouldn't be issued
8078 on this RETURN_EXPR. */
8080 tree
8081 check_return_expr (tree retval, bool *no_warning)
8083 tree result;
8084 /* The type actually returned by the function. */
8085 tree valtype;
8086 /* The type the function is declared to return, or void if
8087 the declared type is incomplete. */
8088 tree functype;
8089 int fn_returns_value_p;
8090 bool named_return_value_okay_p;
8092 *no_warning = false;
8094 /* A `volatile' function is one that isn't supposed to return, ever.
8095 (This is a G++ extension, used to get better code for functions
8096 that call the `volatile' function.) */
8097 if (TREE_THIS_VOLATILE (current_function_decl))
8098 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8100 /* Check for various simple errors. */
8101 if (DECL_DESTRUCTOR_P (current_function_decl))
8103 if (retval)
8104 error ("returning a value from a destructor");
8105 return NULL_TREE;
8107 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8109 if (in_function_try_handler)
8110 /* If a return statement appears in a handler of the
8111 function-try-block of a constructor, the program is ill-formed. */
8112 error ("cannot return from a handler of a function-try-block of a constructor");
8113 else if (retval)
8114 /* You can't return a value from a constructor. */
8115 error ("returning a value from a constructor");
8116 return NULL_TREE;
8119 if (processing_template_decl)
8121 current_function_returns_value = 1;
8122 if (check_for_bare_parameter_packs (retval))
8123 retval = error_mark_node;
8124 return retval;
8127 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8129 /* Deduce auto return type from a return statement. */
8130 if (current_function_auto_return_pattern)
8132 tree auto_node;
8133 tree type;
8135 if (!retval && !is_auto (current_function_auto_return_pattern))
8137 /* Give a helpful error message. */
8138 error ("return-statement with no value, in function returning %qT",
8139 current_function_auto_return_pattern);
8140 inform (input_location, "only plain %<auto%> return type can be "
8141 "deduced to %<void%>");
8142 type = error_mark_node;
8144 else
8146 if (!retval)
8147 retval = void_zero_node;
8148 auto_node = type_uses_auto (current_function_auto_return_pattern);
8149 type = do_auto_deduction (current_function_auto_return_pattern,
8150 retval, auto_node);
8153 if (type == error_mark_node)
8154 /* Leave it. */;
8155 else if (functype == current_function_auto_return_pattern)
8156 apply_deduced_return_type (current_function_decl, type);
8157 else
8158 /* A mismatch should have been diagnosed in do_auto_deduction. */
8159 gcc_assert (same_type_p (type, functype));
8160 functype = type;
8163 /* When no explicit return-value is given in a function with a named
8164 return value, the named return value is used. */
8165 result = DECL_RESULT (current_function_decl);
8166 valtype = TREE_TYPE (result);
8167 gcc_assert (valtype != NULL_TREE);
8168 fn_returns_value_p = !VOID_TYPE_P (valtype);
8169 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8170 retval = result;
8172 /* Check for a return statement with no return value in a function
8173 that's supposed to return a value. */
8174 if (!retval && fn_returns_value_p)
8176 if (functype != error_mark_node)
8177 permerror (input_location, "return-statement with no value, in "
8178 "function returning %qT", valtype);
8179 /* Remember that this function did return. */
8180 current_function_returns_value = 1;
8181 /* And signal caller that TREE_NO_WARNING should be set on the
8182 RETURN_EXPR to avoid control reaches end of non-void function
8183 warnings in tree-cfg.c. */
8184 *no_warning = true;
8186 /* Check for a return statement with a value in a function that
8187 isn't supposed to return a value. */
8188 else if (retval && !fn_returns_value_p)
8190 if (VOID_TYPE_P (TREE_TYPE (retval)))
8191 /* You can return a `void' value from a function of `void'
8192 type. In that case, we have to evaluate the expression for
8193 its side-effects. */
8194 finish_expr_stmt (retval);
8195 else
8196 permerror (input_location, "return-statement with a value, in function "
8197 "returning 'void'");
8198 current_function_returns_null = 1;
8200 /* There's really no value to return, after all. */
8201 return NULL_TREE;
8203 else if (!retval)
8204 /* Remember that this function can sometimes return without a
8205 value. */
8206 current_function_returns_null = 1;
8207 else
8208 /* Remember that this function did return a value. */
8209 current_function_returns_value = 1;
8211 /* Check for erroneous operands -- but after giving ourselves a
8212 chance to provide an error about returning a value from a void
8213 function. */
8214 if (error_operand_p (retval))
8216 current_function_return_value = error_mark_node;
8217 return error_mark_node;
8220 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8221 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8222 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8223 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8224 && ! flag_check_new
8225 && retval && null_ptr_cst_p (retval))
8226 warning (0, "%<operator new%> must not return NULL unless it is "
8227 "declared %<throw()%> (or -fcheck-new is in effect)");
8229 /* Effective C++ rule 15. See also start_function. */
8230 if (warn_ecpp
8231 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8233 bool warn = true;
8235 /* The function return type must be a reference to the current
8236 class. */
8237 if (TREE_CODE (valtype) == REFERENCE_TYPE
8238 && same_type_ignoring_top_level_qualifiers_p
8239 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8241 /* Returning '*this' is obviously OK. */
8242 if (retval == current_class_ref)
8243 warn = false;
8244 /* If we are calling a function whose return type is the same of
8245 the current class reference, it is ok. */
8246 else if (TREE_CODE (retval) == INDIRECT_REF
8247 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8248 warn = false;
8251 if (warn)
8252 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8255 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8257 [...] For a function with a class return type, if the expression
8258 in the return statement is the name of a local object, and the cv-
8259 unqualified type of the local object is the same as the function
8260 return type, an implementation is permitted to omit creating the tem-
8261 porary object to hold the function return value [...]
8263 So, if this is a value-returning function that always returns the same
8264 local variable, remember it.
8266 It might be nice to be more flexible, and choose the first suitable
8267 variable even if the function sometimes returns something else, but
8268 then we run the risk of clobbering the variable we chose if the other
8269 returned expression uses the chosen variable somehow. And people expect
8270 this restriction, anyway. (jason 2000-11-19)
8272 See finish_function and finalize_nrv for the rest of this optimization. */
8274 named_return_value_okay_p =
8275 (retval != NULL_TREE
8276 /* Must be a local, automatic variable. */
8277 && TREE_CODE (retval) == VAR_DECL
8278 && DECL_CONTEXT (retval) == current_function_decl
8279 && ! TREE_STATIC (retval)
8280 && ! DECL_ANON_UNION_VAR_P (retval)
8281 && (DECL_ALIGN (retval) >= DECL_ALIGN (result))
8282 /* The cv-unqualified type of the returned value must be the
8283 same as the cv-unqualified return type of the
8284 function. */
8285 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8286 (TYPE_MAIN_VARIANT (functype)))
8287 /* And the returned value must be non-volatile. */
8288 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8290 if (fn_returns_value_p && flag_elide_constructors)
8292 if (named_return_value_okay_p
8293 && (current_function_return_value == NULL_TREE
8294 || current_function_return_value == retval))
8295 current_function_return_value = retval;
8296 else
8297 current_function_return_value = error_mark_node;
8300 /* We don't need to do any conversions when there's nothing being
8301 returned. */
8302 if (!retval)
8303 return NULL_TREE;
8305 /* Do any required conversions. */
8306 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8307 /* No conversions are required. */
8309 else
8311 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8313 /* The functype's return type will have been set to void, if it
8314 was an incomplete type. Just treat this as 'return;' */
8315 if (VOID_TYPE_P (functype))
8316 return error_mark_node;
8318 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8319 treated as an rvalue for the purposes of overload resolution to
8320 favor move constructors over copy constructors.
8322 Note that these conditions are similar to, but not as strict as,
8323 the conditions for the named return value optimization. */
8324 if ((cxx_dialect != cxx98)
8325 && (TREE_CODE (retval) == VAR_DECL
8326 || TREE_CODE (retval) == PARM_DECL)
8327 && DECL_CONTEXT (retval) == current_function_decl
8328 && !TREE_STATIC (retval)
8329 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8330 (TYPE_MAIN_VARIANT (functype)))
8331 /* This is only interesting for class type. */
8332 && CLASS_TYPE_P (functype))
8333 flags = flags | LOOKUP_PREFER_RVALUE;
8335 /* First convert the value to the function's return type, then
8336 to the type of return value's location to handle the
8337 case that functype is smaller than the valtype. */
8338 retval = convert_for_initialization
8339 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8340 tf_warning_or_error);
8341 retval = convert (valtype, retval);
8343 /* If the conversion failed, treat this just like `return;'. */
8344 if (retval == error_mark_node)
8345 return retval;
8346 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8347 else if (! cfun->returns_struct
8348 && TREE_CODE (retval) == TARGET_EXPR
8349 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8350 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8351 TREE_OPERAND (retval, 0));
8352 else
8353 maybe_warn_about_returning_address_of_local (retval);
8356 /* Actually copy the value returned into the appropriate location. */
8357 if (retval && retval != result)
8358 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8360 return retval;
8364 /* Returns nonzero if the pointer-type FROM can be converted to the
8365 pointer-type TO via a qualification conversion. If CONSTP is -1,
8366 then we return nonzero if the pointers are similar, and the
8367 cv-qualification signature of FROM is a proper subset of that of TO.
8369 If CONSTP is positive, then all outer pointers have been
8370 const-qualified. */
8372 static int
8373 comp_ptr_ttypes_real (tree to, tree from, int constp)
8375 bool to_more_cv_qualified = false;
8376 bool is_opaque_pointer = false;
8378 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8380 if (TREE_CODE (to) != TREE_CODE (from))
8381 return 0;
8383 if (TREE_CODE (from) == OFFSET_TYPE
8384 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8385 TYPE_OFFSET_BASETYPE (to)))
8386 return 0;
8388 /* Const and volatile mean something different for function types,
8389 so the usual checks are not appropriate. */
8390 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8392 if (!at_least_as_qualified_p (to, from))
8393 return 0;
8395 if (!at_least_as_qualified_p (from, to))
8397 if (constp == 0)
8398 return 0;
8399 to_more_cv_qualified = true;
8402 if (constp > 0)
8403 constp &= TYPE_READONLY (to);
8406 if (TREE_CODE (to) == VECTOR_TYPE)
8407 is_opaque_pointer = vector_targets_convertible_p (to, from);
8409 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRDATAMEM_P (to))
8410 return ((constp >= 0 || to_more_cv_qualified)
8411 && (is_opaque_pointer
8412 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8416 /* When comparing, say, char ** to char const **, this function takes
8417 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8418 types to this function. */
8421 comp_ptr_ttypes (tree to, tree from)
8423 return comp_ptr_ttypes_real (to, from, 1);
8426 /* Returns true iff FNTYPE is a non-class type that involves
8427 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8428 if a parameter type is ill-formed. */
8430 bool
8431 error_type_p (const_tree type)
8433 tree t;
8435 switch (TREE_CODE (type))
8437 case ERROR_MARK:
8438 return true;
8440 case POINTER_TYPE:
8441 case REFERENCE_TYPE:
8442 case OFFSET_TYPE:
8443 return error_type_p (TREE_TYPE (type));
8445 case FUNCTION_TYPE:
8446 case METHOD_TYPE:
8447 if (error_type_p (TREE_TYPE (type)))
8448 return true;
8449 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8450 if (error_type_p (TREE_VALUE (t)))
8451 return true;
8452 return false;
8454 case RECORD_TYPE:
8455 if (TYPE_PTRMEMFUNC_P (type))
8456 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8457 return false;
8459 default:
8460 return false;
8464 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8465 type or inheritance-related types, regardless of cv-quals. */
8468 ptr_reasonably_similar (const_tree to, const_tree from)
8470 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8472 /* Any target type is similar enough to void. */
8473 if (TREE_CODE (to) == VOID_TYPE)
8474 return !error_type_p (from);
8475 if (TREE_CODE (from) == VOID_TYPE)
8476 return !error_type_p (to);
8478 if (TREE_CODE (to) != TREE_CODE (from))
8479 return 0;
8481 if (TREE_CODE (from) == OFFSET_TYPE
8482 && comptypes (TYPE_OFFSET_BASETYPE (to),
8483 TYPE_OFFSET_BASETYPE (from),
8484 COMPARE_BASE | COMPARE_DERIVED))
8485 continue;
8487 if (TREE_CODE (to) == VECTOR_TYPE
8488 && vector_types_convertible_p (to, from, false))
8489 return 1;
8491 if (TREE_CODE (to) == INTEGER_TYPE
8492 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8493 return 1;
8495 if (TREE_CODE (to) == FUNCTION_TYPE)
8496 return !error_type_p (to) && !error_type_p (from);
8498 if (TREE_CODE (to) != POINTER_TYPE)
8499 return comptypes
8500 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8501 COMPARE_BASE | COMPARE_DERIVED);
8505 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8506 pointer-to-member types) are the same, ignoring cv-qualification at
8507 all levels. */
8509 bool
8510 comp_ptr_ttypes_const (tree to, tree from)
8512 bool is_opaque_pointer = false;
8514 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8516 if (TREE_CODE (to) != TREE_CODE (from))
8517 return false;
8519 if (TREE_CODE (from) == OFFSET_TYPE
8520 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8521 TYPE_OFFSET_BASETYPE (to)))
8522 continue;
8524 if (TREE_CODE (to) == VECTOR_TYPE)
8525 is_opaque_pointer = vector_targets_convertible_p (to, from);
8527 if (TREE_CODE (to) != POINTER_TYPE)
8528 return (is_opaque_pointer
8529 || same_type_ignoring_top_level_qualifiers_p (to, from));
8533 /* Returns the type qualifiers for this type, including the qualifiers on the
8534 elements for an array type. */
8537 cp_type_quals (const_tree type)
8539 int quals;
8540 /* This CONST_CAST is okay because strip_array_types returns its
8541 argument unmodified and we assign it to a const_tree. */
8542 type = strip_array_types (CONST_CAST_TREE (type));
8543 if (type == error_mark_node
8544 /* Quals on a FUNCTION_TYPE are memfn quals. */
8545 || TREE_CODE (type) == FUNCTION_TYPE)
8546 return TYPE_UNQUALIFIED;
8547 quals = TYPE_QUALS (type);
8548 /* METHOD and REFERENCE_TYPEs should never have quals. */
8549 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8550 && TREE_CODE (type) != REFERENCE_TYPE)
8551 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8552 == TYPE_UNQUALIFIED));
8553 return quals;
8556 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8557 METHOD_TYPE. */
8560 type_memfn_quals (const_tree type)
8562 if (TREE_CODE (type) == FUNCTION_TYPE)
8563 return TYPE_QUALS (type);
8564 else if (TREE_CODE (type) == METHOD_TYPE)
8565 return cp_type_quals (class_of_this_parm (type));
8566 else
8567 gcc_unreachable ();
8570 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8571 MEMFN_QUALS. */
8573 tree
8574 apply_memfn_quals (tree type, cp_cv_quals memfn_quals)
8576 /* Could handle METHOD_TYPE here if necessary. */
8577 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8578 if (TYPE_QUALS (type) == memfn_quals)
8579 return type;
8580 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8581 complex. */
8582 return build_qualified_type (type, memfn_quals);
8585 /* Returns nonzero if TYPE is const or volatile. */
8587 bool
8588 cv_qualified_p (const_tree type)
8590 int quals = cp_type_quals (type);
8591 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8594 /* Returns nonzero if the TYPE contains a mutable member. */
8596 bool
8597 cp_has_mutable_p (const_tree type)
8599 /* This CONST_CAST is okay because strip_array_types returns its
8600 argument unmodified and we assign it to a const_tree. */
8601 type = strip_array_types (CONST_CAST_TREE(type));
8603 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8606 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8607 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8608 approximation. In particular, consider:
8610 int f();
8611 struct S { int i; };
8612 const S s = { f(); }
8614 Here, we will make "s" as TREE_READONLY (because it is declared
8615 "const") -- only to reverse ourselves upon seeing that the
8616 initializer is non-constant. */
8618 void
8619 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8621 tree type = TREE_TYPE (decl);
8623 if (type == error_mark_node)
8624 return;
8626 if (TREE_CODE (decl) == TYPE_DECL)
8627 return;
8629 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8630 && type_quals != TYPE_UNQUALIFIED));
8632 /* Avoid setting TREE_READONLY incorrectly. */
8633 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8634 constructor can produce constant init, so rely on cp_finish_decl to
8635 clear TREE_READONLY if the variable has non-constant init. */
8637 /* If the type has (or might have) a mutable component, that component
8638 might be modified. */
8639 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8640 type_quals &= ~TYPE_QUAL_CONST;
8642 c_apply_type_quals_to_decl (type_quals, decl);
8645 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8646 exemplar types such that casting T1 to T2 is casting away constness
8647 if and only if there is no implicit conversion from T1 to T2. */
8649 static void
8650 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8652 int quals1;
8653 int quals2;
8655 /* [expr.const.cast]
8657 For multi-level pointer to members and multi-level mixed pointers
8658 and pointers to members (conv.qual), the "member" aspect of a
8659 pointer to member level is ignored when determining if a const
8660 cv-qualifier has been cast away. */
8661 /* [expr.const.cast]
8663 For two pointer types:
8665 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8666 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8667 K is min(N,M)
8669 casting from X1 to X2 casts away constness if, for a non-pointer
8670 type T there does not exist an implicit conversion (clause
8671 _conv_) from:
8673 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8677 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8678 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8679 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8681 *t1 = cp_build_qualified_type (void_type_node,
8682 cp_type_quals (*t1));
8683 *t2 = cp_build_qualified_type (void_type_node,
8684 cp_type_quals (*t2));
8685 return;
8688 quals1 = cp_type_quals (*t1);
8689 quals2 = cp_type_quals (*t2);
8691 if (TYPE_PTRDATAMEM_P (*t1))
8692 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8693 else
8694 *t1 = TREE_TYPE (*t1);
8695 if (TYPE_PTRDATAMEM_P (*t2))
8696 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8697 else
8698 *t2 = TREE_TYPE (*t2);
8700 casts_away_constness_r (t1, t2, complain);
8701 *t1 = build_pointer_type (*t1);
8702 *t2 = build_pointer_type (*t2);
8703 *t1 = cp_build_qualified_type (*t1, quals1);
8704 *t2 = cp_build_qualified_type (*t2, quals2);
8707 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8708 constness.
8710 ??? This function returns non-zero if casting away qualifiers not
8711 just const. We would like to return to the caller exactly which
8712 qualifiers are casted away to give more accurate diagnostics.
8715 static bool
8716 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8718 if (TREE_CODE (t2) == REFERENCE_TYPE)
8720 /* [expr.const.cast]
8722 Casting from an lvalue of type T1 to an lvalue of type T2
8723 using a reference cast casts away constness if a cast from an
8724 rvalue of type "pointer to T1" to the type "pointer to T2"
8725 casts away constness. */
8726 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8727 return casts_away_constness (build_pointer_type (t1),
8728 build_pointer_type (TREE_TYPE (t2)),
8729 complain);
8732 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
8733 /* [expr.const.cast]
8735 Casting from an rvalue of type "pointer to data member of X
8736 of type T1" to the type "pointer to data member of Y of type
8737 T2" casts away constness if a cast from an rvalue of type
8738 "pointer to T1" to the type "pointer to T2" casts away
8739 constness. */
8740 return casts_away_constness
8741 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8742 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
8743 complain);
8745 /* Casting away constness is only something that makes sense for
8746 pointer or reference types. */
8747 if (TREE_CODE (t1) != POINTER_TYPE
8748 || TREE_CODE (t2) != POINTER_TYPE)
8749 return false;
8751 /* Top-level qualifiers don't matter. */
8752 t1 = TYPE_MAIN_VARIANT (t1);
8753 t2 = TYPE_MAIN_VARIANT (t2);
8754 casts_away_constness_r (&t1, &t2, complain);
8755 if (!can_convert (t2, t1, complain))
8756 return true;
8758 return false;
8761 /* If T is a REFERENCE_TYPE return the type to which T refers.
8762 Otherwise, return T itself. */
8764 tree
8765 non_reference (tree t)
8767 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8768 t = TREE_TYPE (t);
8769 return t;
8773 /* Return nonzero if REF is an lvalue valid for this language;
8774 otherwise, print an error message and return zero. USE says
8775 how the lvalue is being used and so selects the error message. */
8778 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8780 cp_lvalue_kind kind = lvalue_kind (ref);
8782 if (kind == clk_none)
8784 if (complain & tf_error)
8785 lvalue_error (input_location, use);
8786 return 0;
8788 else if (kind & (clk_rvalueref|clk_class))
8790 if (!(complain & tf_error))
8791 return 0;
8792 if (kind & clk_class)
8793 /* Make this a permerror because we used to accept it. */
8794 permerror (input_location, "using temporary as lvalue");
8795 else
8796 error ("using xvalue (rvalue reference) as lvalue");
8798 return 1;
8801 /* Return true if a user-defined literal operator is a raw operator. */
8803 bool
8804 check_raw_literal_operator (const_tree decl)
8806 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8807 tree argtype;
8808 int arity;
8809 bool maybe_raw_p = false;
8811 /* Count the number and type of arguments and check for ellipsis. */
8812 for (argtype = argtypes, arity = 0;
8813 argtype && argtype != void_list_node;
8814 ++arity, argtype = TREE_CHAIN (argtype))
8816 tree t = TREE_VALUE (argtype);
8818 if (same_type_p (t, const_string_type_node))
8819 maybe_raw_p = true;
8821 if (!argtype)
8822 return false; /* Found ellipsis. */
8824 if (!maybe_raw_p || arity != 1)
8825 return false;
8827 return true;
8831 /* Return true if a user-defined literal operator has one of the allowed
8832 argument types. */
8834 bool
8835 check_literal_operator_args (const_tree decl,
8836 bool *long_long_unsigned_p, bool *long_double_p)
8838 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8840 *long_long_unsigned_p = false;
8841 *long_double_p = false;
8842 if (processing_template_decl || processing_specialization)
8843 return argtypes == void_list_node;
8844 else
8846 tree argtype;
8847 int arity;
8848 int max_arity = 2;
8850 /* Count the number and type of arguments and check for ellipsis. */
8851 for (argtype = argtypes, arity = 0;
8852 argtype && argtype != void_list_node;
8853 argtype = TREE_CHAIN (argtype))
8855 tree t = TREE_VALUE (argtype);
8856 ++arity;
8858 if (TREE_CODE (t) == POINTER_TYPE)
8860 bool maybe_raw_p = false;
8861 t = TREE_TYPE (t);
8862 if (cp_type_quals (t) != TYPE_QUAL_CONST)
8863 return false;
8864 t = TYPE_MAIN_VARIANT (t);
8865 if ((maybe_raw_p = same_type_p (t, char_type_node))
8866 || same_type_p (t, wchar_type_node)
8867 || same_type_p (t, char16_type_node)
8868 || same_type_p (t, char32_type_node))
8870 argtype = TREE_CHAIN (argtype);
8871 if (!argtype)
8872 return false;
8873 t = TREE_VALUE (argtype);
8874 if (maybe_raw_p && argtype == void_list_node)
8875 return true;
8876 else if (same_type_p (t, size_type_node))
8878 ++arity;
8879 continue;
8881 else
8882 return false;
8885 else if (same_type_p (t, long_long_unsigned_type_node))
8887 max_arity = 1;
8888 *long_long_unsigned_p = true;
8890 else if (same_type_p (t, long_double_type_node))
8892 max_arity = 1;
8893 *long_double_p = true;
8895 else if (same_type_p (t, char_type_node))
8896 max_arity = 1;
8897 else if (same_type_p (t, wchar_type_node))
8898 max_arity = 1;
8899 else if (same_type_p (t, char16_type_node))
8900 max_arity = 1;
8901 else if (same_type_p (t, char32_type_node))
8902 max_arity = 1;
8903 else
8904 return false;
8906 if (!argtype)
8907 return false; /* Found ellipsis. */
8909 if (arity != max_arity)
8910 return false;
8912 return true;