2013-11-01 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / cp / typeck.c
blobbcb87825a1e2f329238e100604052ff7dc1d4135
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 "c-family/c-ubsan.h"
41 #include "params.h"
43 static tree pfn_from_ptrmemfunc (tree);
44 static tree delta_from_ptrmemfunc (tree);
45 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
46 tsubst_flags_t, int);
47 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
48 static tree rationalize_conditional_expr (enum tree_code, tree,
49 tsubst_flags_t);
50 static int comp_ptr_ttypes_real (tree, tree, int);
51 static bool comp_except_types (tree, tree, bool);
52 static bool comp_array_types (const_tree, const_tree, bool);
53 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
54 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
55 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
56 static bool casts_away_constness (tree, tree, tsubst_flags_t);
57 static void maybe_warn_about_returning_address_of_local (tree);
58 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
59 static void warn_args_num (location_t, tree, bool);
60 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
61 tsubst_flags_t);
63 /* Do `exp = require_complete_type (exp);' to make sure exp
64 does not have an incomplete type. (That includes void types.)
65 Returns error_mark_node if the VALUE does not have
66 complete type when this function returns. */
68 tree
69 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
71 tree type;
73 if (processing_template_decl || value == error_mark_node)
74 return value;
76 if (TREE_CODE (value) == OVERLOAD)
77 type = unknown_type_node;
78 else
79 type = TREE_TYPE (value);
81 if (type == error_mark_node)
82 return error_mark_node;
84 /* First, detect a valid value with a complete type. */
85 if (COMPLETE_TYPE_P (type))
86 return value;
88 if (complete_type_or_maybe_complain (type, value, complain))
89 return value;
90 else
91 return error_mark_node;
94 tree
95 require_complete_type (tree value)
97 return require_complete_type_sfinae (value, tf_warning_or_error);
100 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
101 a template instantiation, do the instantiation. Returns TYPE,
102 whether or not it could be completed, unless something goes
103 horribly wrong, in which case the error_mark_node is returned. */
105 tree
106 complete_type (tree type)
108 if (type == NULL_TREE)
109 /* Rather than crash, we return something sure to cause an error
110 at some point. */
111 return error_mark_node;
113 if (type == error_mark_node || COMPLETE_TYPE_P (type))
115 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
117 tree t = complete_type (TREE_TYPE (type));
118 unsigned int needs_constructing, has_nontrivial_dtor;
119 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 layout_type (type);
121 needs_constructing
122 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 has_nontrivial_dtor
124 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
127 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
131 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
132 instantiate_class_template (TYPE_MAIN_VARIANT (type));
134 return type;
137 /* Like complete_type, but issue an error if the TYPE cannot be completed.
138 VALUE is used for informative diagnostics.
139 Returns NULL_TREE if the type cannot be made complete. */
141 tree
142 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
144 type = complete_type (type);
145 if (type == error_mark_node)
146 /* We already issued an error. */
147 return NULL_TREE;
148 else if (!COMPLETE_TYPE_P (type))
150 if (complain & tf_error)
151 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
152 return NULL_TREE;
154 else
155 return type;
158 tree
159 complete_type_or_else (tree type, tree value)
161 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
164 /* Return truthvalue of whether type of EXP is instantiated. */
167 type_unknown_p (const_tree exp)
169 return (TREE_CODE (exp) == TREE_LIST
170 || TREE_TYPE (exp) == unknown_type_node);
174 /* Return the common type of two parameter lists.
175 We assume that comptypes has already been done and returned 1;
176 if that isn't so, this may crash.
178 As an optimization, free the space we allocate if the parameter
179 lists are already common. */
181 static tree
182 commonparms (tree p1, tree p2)
184 tree oldargs = p1, newargs, n;
185 int i, len;
186 int any_change = 0;
188 len = list_length (p1);
189 newargs = tree_last (p1);
191 if (newargs == void_list_node)
192 i = 1;
193 else
195 i = 0;
196 newargs = 0;
199 for (; i < len; i++)
200 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
202 n = newargs;
204 for (i = 0; p1;
205 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
207 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
209 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
210 any_change = 1;
212 else if (! TREE_PURPOSE (p1))
214 if (TREE_PURPOSE (p2))
216 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
217 any_change = 1;
220 else
222 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
223 any_change = 1;
224 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
226 if (TREE_VALUE (p1) != TREE_VALUE (p2))
228 any_change = 1;
229 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
231 else
232 TREE_VALUE (n) = TREE_VALUE (p1);
234 if (! any_change)
235 return oldargs;
237 return newargs;
240 /* Given a type, perhaps copied for a typedef,
241 find the "original" version of it. */
242 static tree
243 original_type (tree t)
245 int quals = cp_type_quals (t);
246 while (t != error_mark_node
247 && TYPE_NAME (t) != NULL_TREE)
249 tree x = TYPE_NAME (t);
250 if (TREE_CODE (x) != TYPE_DECL)
251 break;
252 x = DECL_ORIGINAL_TYPE (x);
253 if (x == NULL_TREE)
254 break;
255 t = x;
257 return cp_build_qualified_type (t, quals);
260 /* Return the common type for two arithmetic types T1 and T2 under the
261 usual arithmetic conversions. The default conversions have already
262 been applied, and enumerated types converted to their compatible
263 integer types. */
265 static tree
266 cp_common_type (tree t1, tree t2)
268 enum tree_code code1 = TREE_CODE (t1);
269 enum tree_code code2 = TREE_CODE (t2);
270 tree attributes;
273 /* In what follows, we slightly generalize the rules given in [expr] so
274 as to deal with `long long' and `complex'. First, merge the
275 attributes. */
276 attributes = (*targetm.merge_type_attributes) (t1, t2);
278 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
280 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
281 return build_type_attribute_variant (t1, attributes);
282 else
283 return NULL_TREE;
286 /* FIXME: Attributes. */
287 gcc_assert (ARITHMETIC_TYPE_P (t1)
288 || TREE_CODE (t1) == VECTOR_TYPE
289 || UNSCOPED_ENUM_P (t1));
290 gcc_assert (ARITHMETIC_TYPE_P (t2)
291 || TREE_CODE (t2) == VECTOR_TYPE
292 || UNSCOPED_ENUM_P (t2));
294 /* If one type is complex, form the common type of the non-complex
295 components, then make that complex. Use T1 or T2 if it is the
296 required type. */
297 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
299 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
300 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
301 tree subtype
302 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
304 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
305 return build_type_attribute_variant (t1, attributes);
306 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
307 return build_type_attribute_variant (t2, attributes);
308 else
309 return build_type_attribute_variant (build_complex_type (subtype),
310 attributes);
313 if (code1 == VECTOR_TYPE)
315 /* When we get here we should have two vectors of the same size.
316 Just prefer the unsigned one if present. */
317 if (TYPE_UNSIGNED (t1))
318 return build_type_attribute_variant (t1, attributes);
319 else
320 return build_type_attribute_variant (t2, attributes);
323 /* If only one is real, use it as the result. */
324 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
325 return build_type_attribute_variant (t1, attributes);
326 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
327 return build_type_attribute_variant (t2, attributes);
329 /* Both real or both integers; use the one with greater precision. */
330 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
331 return build_type_attribute_variant (t1, attributes);
332 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
333 return build_type_attribute_variant (t2, attributes);
335 /* The types are the same; no need to do anything fancy. */
336 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
337 return build_type_attribute_variant (t1, attributes);
339 if (code1 != REAL_TYPE)
341 /* If one is unsigned long long, then convert the other to unsigned
342 long long. */
343 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
344 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
345 return build_type_attribute_variant (long_long_unsigned_type_node,
346 attributes);
347 /* If one is a long long, and the other is an unsigned long, and
348 long long can represent all the values of an unsigned long, then
349 convert to a long long. Otherwise, convert to an unsigned long
350 long. Otherwise, if either operand is long long, convert the
351 other to long long.
353 Since we're here, we know the TYPE_PRECISION is the same;
354 therefore converting to long long cannot represent all the values
355 of an unsigned long, so we choose unsigned long long in that
356 case. */
357 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
358 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
360 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
361 ? long_long_unsigned_type_node
362 : long_long_integer_type_node);
363 return build_type_attribute_variant (t, attributes);
365 if (int128_integer_type_node != NULL_TREE
366 && (same_type_p (TYPE_MAIN_VARIANT (t1),
367 int128_integer_type_node)
368 || same_type_p (TYPE_MAIN_VARIANT (t2),
369 int128_integer_type_node)))
371 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
372 ? int128_unsigned_type_node
373 : int128_integer_type_node);
374 return build_type_attribute_variant (t, attributes);
377 /* Go through the same procedure, but for longs. */
378 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
379 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
380 return build_type_attribute_variant (long_unsigned_type_node,
381 attributes);
382 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
383 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
385 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
386 ? long_unsigned_type_node : long_integer_type_node);
387 return build_type_attribute_variant (t, attributes);
389 /* Otherwise prefer the unsigned one. */
390 if (TYPE_UNSIGNED (t1))
391 return build_type_attribute_variant (t1, attributes);
392 else
393 return build_type_attribute_variant (t2, attributes);
395 else
397 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
398 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
399 return build_type_attribute_variant (long_double_type_node,
400 attributes);
401 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
402 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
403 return build_type_attribute_variant (double_type_node,
404 attributes);
405 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
406 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
407 return build_type_attribute_variant (float_type_node,
408 attributes);
410 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
411 the standard C++ floating-point types. Logic earlier in this
412 function has already eliminated the possibility that
413 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
414 compelling reason to choose one or the other. */
415 return build_type_attribute_variant (t1, attributes);
419 /* T1 and T2 are arithmetic or enumeration types. Return the type
420 that will result from the "usual arithmetic conversions" on T1 and
421 T2 as described in [expr]. */
423 tree
424 type_after_usual_arithmetic_conversions (tree t1, tree t2)
426 gcc_assert (ARITHMETIC_TYPE_P (t1)
427 || TREE_CODE (t1) == VECTOR_TYPE
428 || UNSCOPED_ENUM_P (t1));
429 gcc_assert (ARITHMETIC_TYPE_P (t2)
430 || TREE_CODE (t2) == VECTOR_TYPE
431 || UNSCOPED_ENUM_P (t2));
433 /* Perform the integral promotions. We do not promote real types here. */
434 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
435 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
437 t1 = type_promotes_to (t1);
438 t2 = type_promotes_to (t2);
441 return cp_common_type (t1, t2);
444 static void
445 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
446 composite_pointer_operation operation)
448 switch (operation)
450 case CPO_COMPARISON:
451 emit_diagnostic (kind, input_location, 0,
452 "comparison between "
453 "distinct pointer types %qT and %qT lacks a cast",
454 t1, t2);
455 break;
456 case CPO_CONVERSION:
457 emit_diagnostic (kind, input_location, 0,
458 "conversion between "
459 "distinct pointer types %qT and %qT lacks a cast",
460 t1, t2);
461 break;
462 case CPO_CONDITIONAL_EXPR:
463 emit_diagnostic (kind, input_location, 0,
464 "conditional expression between "
465 "distinct pointer types %qT and %qT lacks a cast",
466 t1, t2);
467 break;
468 default:
469 gcc_unreachable ();
473 /* Subroutine of composite_pointer_type to implement the recursive
474 case. See that function for documentation of the parameters. */
476 static tree
477 composite_pointer_type_r (tree t1, tree t2,
478 composite_pointer_operation operation,
479 tsubst_flags_t complain)
481 tree pointee1;
482 tree pointee2;
483 tree result_type;
484 tree attributes;
486 /* Determine the types pointed to by T1 and T2. */
487 if (TYPE_PTR_P (t1))
489 pointee1 = TREE_TYPE (t1);
490 pointee2 = TREE_TYPE (t2);
492 else
494 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
495 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
498 /* [expr.rel]
500 Otherwise, the composite pointer type is a pointer type
501 similar (_conv.qual_) to the type of one of the operands,
502 with a cv-qualification signature (_conv.qual_) that is the
503 union of the cv-qualification signatures of the operand
504 types. */
505 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
506 result_type = pointee1;
507 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
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 (TYPE_PTR_P (t2) && 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 (TYPE_PTR_P (t1) && 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 () && TYPE_PTR_P (t1)
632 && TYPE_PTR_P (t2))
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 (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
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 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
837 rval = apply_memfn_quals (rval,
838 type_memfn_quals (t1),
839 type_memfn_rqual (t1));
840 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
841 TYPE_RAISES_EXCEPTIONS (t2),
842 NULL_TREE);
843 t1 = build_exception_variant (rval, raises);
844 break;
847 case METHOD_TYPE:
849 /* Get this value the long way, since TYPE_METHOD_BASETYPE
850 is just the main variant of this. */
851 tree basetype = class_of_this_parm (t2);
852 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
853 TYPE_RAISES_EXCEPTIONS (t2),
854 NULL_TREE);
855 cp_ref_qualifier rqual = type_memfn_rqual (t1);
856 tree t3;
858 /* If this was a member function type, get back to the
859 original type of type member function (i.e., without
860 the class instance variable up front. */
861 t1 = build_function_type (TREE_TYPE (t1),
862 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
863 t2 = build_function_type (TREE_TYPE (t2),
864 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
865 t3 = merge_types (t1, t2);
866 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
867 TYPE_ARG_TYPES (t3));
868 t1 = build_exception_variant (t3, raises);
869 t1 = build_ref_qualified_type (t1, rqual);
870 break;
873 case TYPENAME_TYPE:
874 /* There is no need to merge attributes into a TYPENAME_TYPE.
875 When the type is instantiated it will have whatever
876 attributes result from the instantiation. */
877 return t1;
879 default:;
882 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
883 return t1;
884 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
885 return t2;
886 else
887 return cp_build_type_attribute_variant (t1, attributes);
890 /* Return the ARRAY_TYPE type without its domain. */
892 tree
893 strip_array_domain (tree type)
895 tree t2;
896 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
897 if (TYPE_DOMAIN (type) == NULL_TREE)
898 return type;
899 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
900 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
903 /* Wrapper around cp_common_type that is used by c-common.c and other
904 front end optimizations that remove promotions.
906 Return the common type for two arithmetic types T1 and T2 under the
907 usual arithmetic conversions. The default conversions have already
908 been applied, and enumerated types converted to their compatible
909 integer types. */
911 tree
912 common_type (tree t1, tree t2)
914 /* If one type is nonsense, use the other */
915 if (t1 == error_mark_node)
916 return t2;
917 if (t2 == error_mark_node)
918 return t1;
920 return cp_common_type (t1, t2);
923 /* Return the common type of two pointer types T1 and T2. This is the
924 type for the result of most arithmetic operations if the operands
925 have the given two types.
927 We assume that comp_target_types has already been done and returned
928 nonzero; if that isn't so, this may crash. */
930 tree
931 common_pointer_type (tree t1, tree t2)
933 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
934 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
935 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
937 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
938 CPO_CONVERSION, tf_warning_or_error);
941 /* Compare two exception specifier types for exactness or subsetness, if
942 allowed. Returns false for mismatch, true for match (same, or
943 derived and !exact).
945 [except.spec] "If a class X ... objects of class X or any class publicly
946 and unambiguously derived from X. Similarly, if a pointer type Y * ...
947 exceptions of type Y * or that are pointers to any type publicly and
948 unambiguously derived from Y. Otherwise a function only allows exceptions
949 that have the same type ..."
950 This does not mention cv qualifiers and is different to what throw
951 [except.throw] and catch [except.catch] will do. They will ignore the
952 top level cv qualifiers, and allow qualifiers in the pointer to class
953 example.
955 We implement the letter of the standard. */
957 static bool
958 comp_except_types (tree a, tree b, bool exact)
960 if (same_type_p (a, b))
961 return true;
962 else if (!exact)
964 if (cp_type_quals (a) || cp_type_quals (b))
965 return false;
967 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
969 a = TREE_TYPE (a);
970 b = TREE_TYPE (b);
971 if (cp_type_quals (a) || cp_type_quals (b))
972 return false;
975 if (TREE_CODE (a) != RECORD_TYPE
976 || TREE_CODE (b) != RECORD_TYPE)
977 return false;
979 if (publicly_uniquely_derived_p (a, b))
980 return true;
982 return false;
985 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
986 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
987 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
988 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
989 are unordered, but we've already filtered out duplicates. Most lists will
990 be in order, we should try to make use of that. */
992 bool
993 comp_except_specs (const_tree t1, const_tree t2, int exact)
995 const_tree probe;
996 const_tree base;
997 int length = 0;
999 if (t1 == t2)
1000 return true;
1002 /* First handle noexcept. */
1003 if (exact < ce_exact)
1005 /* noexcept(false) is compatible with no exception-specification,
1006 and stricter than any spec. */
1007 if (t1 == noexcept_false_spec)
1008 return t2 == NULL_TREE || exact == ce_derived;
1009 /* Even a derived noexcept(false) is compatible with no
1010 exception-specification. */
1011 if (t2 == noexcept_false_spec)
1012 return t1 == NULL_TREE;
1014 /* Otherwise, if we aren't looking for an exact match, noexcept is
1015 equivalent to throw(). */
1016 if (t1 == noexcept_true_spec)
1017 t1 = empty_except_spec;
1018 if (t2 == noexcept_true_spec)
1019 t2 = empty_except_spec;
1022 /* If any noexcept is left, it is only comparable to itself;
1023 either we're looking for an exact match or we're redeclaring a
1024 template with dependent noexcept. */
1025 if ((t1 && TREE_PURPOSE (t1))
1026 || (t2 && TREE_PURPOSE (t2)))
1027 return (t1 && t2
1028 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1030 if (t1 == NULL_TREE) /* T1 is ... */
1031 return t2 == NULL_TREE || exact == ce_derived;
1032 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1033 return t2 != NULL_TREE && !TREE_VALUE (t2);
1034 if (t2 == NULL_TREE) /* T2 is ... */
1035 return false;
1036 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1037 return exact == ce_derived;
1039 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1040 Count how many we find, to determine exactness. For exact matching and
1041 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1042 O(nm). */
1043 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1045 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1047 tree a = TREE_VALUE (probe);
1048 tree b = TREE_VALUE (t2);
1050 if (comp_except_types (a, b, exact))
1052 if (probe == base && exact > ce_derived)
1053 base = TREE_CHAIN (probe);
1054 length++;
1055 break;
1058 if (probe == NULL_TREE)
1059 return false;
1061 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1064 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1065 [] can match [size]. */
1067 static bool
1068 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1070 tree d1;
1071 tree d2;
1072 tree max1, max2;
1074 if (t1 == t2)
1075 return true;
1077 /* The type of the array elements must be the same. */
1078 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1079 return false;
1081 d1 = TYPE_DOMAIN (t1);
1082 d2 = TYPE_DOMAIN (t2);
1084 if (d1 == d2)
1085 return true;
1087 /* If one of the arrays is dimensionless, and the other has a
1088 dimension, they are of different types. However, it is valid to
1089 write:
1091 extern int a[];
1092 int a[3];
1094 by [basic.link]:
1096 declarations for an array object can specify
1097 array types that differ by the presence or absence of a major
1098 array bound (_dcl.array_). */
1099 if (!d1 || !d2)
1100 return allow_redeclaration;
1102 /* Check that the dimensions are the same. */
1104 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1105 return false;
1106 max1 = TYPE_MAX_VALUE (d1);
1107 max2 = TYPE_MAX_VALUE (d2);
1108 if (processing_template_decl && !abi_version_at_least (2)
1109 && !value_dependent_expression_p (max1)
1110 && !value_dependent_expression_p (max2))
1112 /* With abi-1 we do not fold non-dependent array bounds, (and
1113 consequently mangle them incorrectly). We must therefore
1114 fold them here, to verify the domains have the same
1115 value. */
1116 max1 = fold (max1);
1117 max2 = fold (max2);
1120 if (!cp_tree_equal (max1, max2))
1121 return false;
1123 return true;
1126 /* Compare the relative position of T1 and T2 into their respective
1127 template parameter list.
1128 T1 and T2 must be template parameter types.
1129 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1131 static bool
1132 comp_template_parms_position (tree t1, tree t2)
1134 tree index1, index2;
1135 gcc_assert (t1 && t2
1136 && TREE_CODE (t1) == TREE_CODE (t2)
1137 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1138 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1139 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1141 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1142 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1144 /* Then compare their relative position. */
1145 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1146 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1147 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1148 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1149 return false;
1151 return true;
1154 /* Subroutine in comptypes. */
1156 static bool
1157 structural_comptypes (tree t1, tree t2, int strict)
1159 if (t1 == t2)
1160 return true;
1162 /* Suppress errors caused by previously reported errors. */
1163 if (t1 == error_mark_node || t2 == error_mark_node)
1164 return false;
1166 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1168 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1169 current instantiation. */
1170 if (TREE_CODE (t1) == TYPENAME_TYPE)
1171 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1173 if (TREE_CODE (t2) == TYPENAME_TYPE)
1174 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1176 if (TYPE_PTRMEMFUNC_P (t1))
1177 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1178 if (TYPE_PTRMEMFUNC_P (t2))
1179 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1181 /* Different classes of types can't be compatible. */
1182 if (TREE_CODE (t1) != TREE_CODE (t2))
1183 return false;
1185 /* Qualifiers must match. For array types, we will check when we
1186 recur on the array element types. */
1187 if (TREE_CODE (t1) != ARRAY_TYPE
1188 && cp_type_quals (t1) != cp_type_quals (t2))
1189 return false;
1190 if (TREE_CODE (t1) == FUNCTION_TYPE
1191 && type_memfn_quals (t1) != type_memfn_quals (t2))
1192 return false;
1193 /* Need to check this before TYPE_MAIN_VARIANT.
1194 FIXME function qualifiers should really change the main variant. */
1195 if ((TREE_CODE (t1) == FUNCTION_TYPE
1196 || TREE_CODE (t1) == METHOD_TYPE)
1197 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1198 return false;
1199 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1200 return false;
1202 /* Allow for two different type nodes which have essentially the same
1203 definition. Note that we already checked for equality of the type
1204 qualifiers (just above). */
1206 if (TREE_CODE (t1) != ARRAY_TYPE
1207 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1208 return true;
1211 /* Compare the types. Break out if they could be the same. */
1212 switch (TREE_CODE (t1))
1214 case VOID_TYPE:
1215 case BOOLEAN_TYPE:
1216 /* All void and bool types are the same. */
1217 break;
1219 case INTEGER_TYPE:
1220 case FIXED_POINT_TYPE:
1221 case REAL_TYPE:
1222 /* With these nodes, we can't determine type equivalence by
1223 looking at what is stored in the nodes themselves, because
1224 two nodes might have different TYPE_MAIN_VARIANTs but still
1225 represent the same type. For example, wchar_t and int could
1226 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1227 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1228 and are distinct types. On the other hand, int and the
1229 following typedef
1231 typedef int INT __attribute((may_alias));
1233 have identical properties, different TYPE_MAIN_VARIANTs, but
1234 represent the same type. The canonical type system keeps
1235 track of equivalence in this case, so we fall back on it. */
1236 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1238 case TEMPLATE_TEMPLATE_PARM:
1239 case BOUND_TEMPLATE_TEMPLATE_PARM:
1240 if (!comp_template_parms_position (t1, t2))
1241 return false;
1242 if (!comp_template_parms
1243 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1244 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1245 return false;
1246 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1247 break;
1248 /* Don't check inheritance. */
1249 strict = COMPARE_STRICT;
1250 /* Fall through. */
1252 case RECORD_TYPE:
1253 case UNION_TYPE:
1254 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1255 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1256 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1257 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1258 break;
1260 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1261 break;
1262 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1263 break;
1265 return false;
1267 case OFFSET_TYPE:
1268 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1269 strict & ~COMPARE_REDECLARATION))
1270 return false;
1271 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1272 return false;
1273 break;
1275 case REFERENCE_TYPE:
1276 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1277 return false;
1278 /* fall through to checks for pointer types */
1280 case POINTER_TYPE:
1281 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1282 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1283 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1284 return false;
1285 break;
1287 case METHOD_TYPE:
1288 case FUNCTION_TYPE:
1289 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1290 return false;
1291 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1292 return false;
1293 break;
1295 case ARRAY_TYPE:
1296 /* Target types must match incl. qualifiers. */
1297 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1298 return false;
1299 break;
1301 case TEMPLATE_TYPE_PARM:
1302 /* If T1 and T2 don't have the same relative position in their
1303 template parameters set, they can't be equal. */
1304 if (!comp_template_parms_position (t1, t2))
1305 return false;
1306 break;
1308 case TYPENAME_TYPE:
1309 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1310 TYPENAME_TYPE_FULLNAME (t2)))
1311 return false;
1312 /* Qualifiers don't matter on scopes. */
1313 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1314 TYPE_CONTEXT (t2)))
1315 return false;
1316 break;
1318 case UNBOUND_CLASS_TEMPLATE:
1319 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1320 return false;
1321 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1322 return false;
1323 break;
1325 case COMPLEX_TYPE:
1326 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1327 return false;
1328 break;
1330 case VECTOR_TYPE:
1331 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1332 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1333 return false;
1334 break;
1336 case TYPE_PACK_EXPANSION:
1337 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1338 PACK_EXPANSION_PATTERN (t2))
1339 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1340 PACK_EXPANSION_EXTRA_ARGS (t2)));
1342 case DECLTYPE_TYPE:
1343 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1344 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1345 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1346 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1347 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1348 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1349 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1350 DECLTYPE_TYPE_EXPR (t2)))
1351 return false;
1352 break;
1354 case UNDERLYING_TYPE:
1355 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1356 UNDERLYING_TYPE_TYPE (t2));
1358 default:
1359 return false;
1362 /* If we get here, we know that from a target independent POV the
1363 types are the same. Make sure the target attributes are also
1364 the same. */
1365 return comp_type_attributes (t1, t2);
1368 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1369 is a bitwise-or of the COMPARE_* flags. */
1371 bool
1372 comptypes (tree t1, tree t2, int strict)
1374 if (strict == COMPARE_STRICT)
1376 if (t1 == t2)
1377 return true;
1379 if (t1 == error_mark_node || t2 == error_mark_node)
1380 return false;
1382 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1383 /* At least one of the types requires structural equality, so
1384 perform a deep check. */
1385 return structural_comptypes (t1, t2, strict);
1387 #ifdef ENABLE_CHECKING
1388 if (USE_CANONICAL_TYPES)
1390 bool result = structural_comptypes (t1, t2, strict);
1392 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1393 /* The two types are structurally equivalent, but their
1394 canonical types were different. This is a failure of the
1395 canonical type propagation code.*/
1396 internal_error
1397 ("canonical types differ for identical types %T and %T",
1398 t1, t2);
1399 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1400 /* Two types are structurally different, but the canonical
1401 types are the same. This means we were over-eager in
1402 assigning canonical types. */
1403 internal_error
1404 ("same canonical type node for different types %T and %T",
1405 t1, t2);
1407 return result;
1409 #else
1410 if (USE_CANONICAL_TYPES)
1411 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1412 #endif
1413 else
1414 return structural_comptypes (t1, t2, strict);
1416 else if (strict == COMPARE_STRUCTURAL)
1417 return structural_comptypes (t1, t2, COMPARE_STRICT);
1418 else
1419 return structural_comptypes (t1, t2, strict);
1422 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1423 top-level qualifiers. */
1425 bool
1426 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1428 if (type1 == error_mark_node || type2 == error_mark_node)
1429 return false;
1431 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1434 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1436 bool
1437 at_least_as_qualified_p (const_tree type1, const_tree type2)
1439 int q1 = cp_type_quals (type1);
1440 int q2 = cp_type_quals (type2);
1442 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1443 return (q1 & q2) == q2;
1446 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1447 more cv-qualified that TYPE1, and 0 otherwise. */
1450 comp_cv_qualification (const_tree type1, const_tree type2)
1452 int q1 = cp_type_quals (type1);
1453 int q2 = cp_type_quals (type2);
1455 if (q1 == q2)
1456 return 0;
1458 if ((q1 & q2) == q2)
1459 return 1;
1460 else if ((q1 & q2) == q1)
1461 return -1;
1463 return 0;
1466 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1467 subset of the cv-qualification signature of TYPE2, and the types
1468 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1471 comp_cv_qual_signature (tree type1, tree type2)
1473 if (comp_ptr_ttypes_real (type2, type1, -1))
1474 return 1;
1475 else if (comp_ptr_ttypes_real (type1, type2, -1))
1476 return -1;
1477 else
1478 return 0;
1481 /* Subroutines of `comptypes'. */
1483 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1484 equivalent in the sense that functions with those parameter types
1485 can have equivalent types. The two lists must be equivalent,
1486 element by element. */
1488 bool
1489 compparms (const_tree parms1, const_tree parms2)
1491 const_tree t1, t2;
1493 /* An unspecified parmlist matches any specified parmlist
1494 whose argument types don't need default promotions. */
1496 for (t1 = parms1, t2 = parms2;
1497 t1 || t2;
1498 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1500 /* If one parmlist is shorter than the other,
1501 they fail to match. */
1502 if (!t1 || !t2)
1503 return false;
1504 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1505 return false;
1507 return true;
1511 /* Process a sizeof or alignof expression where the operand is a
1512 type. */
1514 tree
1515 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1517 tree value;
1518 bool dependent_p;
1520 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1521 if (type == error_mark_node)
1522 return error_mark_node;
1524 type = non_reference (type);
1525 if (TREE_CODE (type) == METHOD_TYPE)
1527 if (complain)
1528 pedwarn (input_location, OPT_Wpointer_arith,
1529 "invalid application of %qs to a member function",
1530 operator_name_info[(int) op].name);
1531 value = size_one_node;
1534 dependent_p = dependent_type_p (type);
1535 if (!dependent_p)
1536 complete_type (type);
1537 if (dependent_p
1538 /* VLA types will have a non-constant size. In the body of an
1539 uninstantiated template, we don't need to try to compute the
1540 value, because the sizeof expression is not an integral
1541 constant expression in that case. And, if we do try to
1542 compute the value, we'll likely end up with SAVE_EXPRs, which
1543 the template substitution machinery does not expect to see. */
1544 || (processing_template_decl
1545 && COMPLETE_TYPE_P (type)
1546 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1548 value = build_min (op, size_type_node, type);
1549 TREE_READONLY (value) = 1;
1550 return value;
1553 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type))
1555 if (complain & tf_warning_or_error)
1556 pedwarn (input_location, OPT_Wvla,
1557 "taking sizeof array of runtime bound");
1558 else
1559 return error_mark_node;
1562 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1563 op == SIZEOF_EXPR,
1564 complain);
1567 /* Return the size of the type, without producing any warnings for
1568 types whose size cannot be taken. This routine should be used only
1569 in some other routine that has already produced a diagnostic about
1570 using the size of such a type. */
1571 tree
1572 cxx_sizeof_nowarn (tree type)
1574 if (TREE_CODE (type) == FUNCTION_TYPE
1575 || VOID_TYPE_P (type)
1576 || TREE_CODE (type) == ERROR_MARK)
1577 return size_one_node;
1578 else if (!COMPLETE_TYPE_P (type))
1579 return size_zero_node;
1580 else
1581 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1584 /* Process a sizeof expression where the operand is an expression. */
1586 static tree
1587 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1589 if (e == error_mark_node)
1590 return error_mark_node;
1592 if (processing_template_decl)
1594 e = build_min (SIZEOF_EXPR, size_type_node, e);
1595 TREE_SIDE_EFFECTS (e) = 0;
1596 TREE_READONLY (e) = 1;
1598 return e;
1601 /* To get the size of a static data member declared as an array of
1602 unknown bound, we need to instantiate it. */
1603 if (VAR_P (e)
1604 && VAR_HAD_UNKNOWN_BOUND (e)
1605 && DECL_TEMPLATE_INSTANTIATION (e))
1606 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1608 e = mark_type_use (e);
1610 if (TREE_CODE (e) == COMPONENT_REF
1611 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1612 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1614 if (complain & tf_error)
1615 error ("invalid application of %<sizeof%> to a bit-field");
1616 else
1617 return error_mark_node;
1618 e = char_type_node;
1620 else if (is_overloaded_fn (e))
1622 if (complain & tf_error)
1623 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1624 "function type");
1625 else
1626 return error_mark_node;
1627 e = char_type_node;
1629 else if (type_unknown_p (e))
1631 if (complain & tf_error)
1632 cxx_incomplete_type_error (e, TREE_TYPE (e));
1633 else
1634 return error_mark_node;
1635 e = char_type_node;
1637 else
1638 e = TREE_TYPE (e);
1640 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1643 /* Implement the __alignof keyword: Return the minimum required
1644 alignment of E, measured in bytes. For VAR_DECL's and
1645 FIELD_DECL's return DECL_ALIGN (which can be set from an
1646 "aligned" __attribute__ specification). */
1648 static tree
1649 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1651 tree t;
1653 if (e == error_mark_node)
1654 return error_mark_node;
1656 if (processing_template_decl)
1658 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1659 TREE_SIDE_EFFECTS (e) = 0;
1660 TREE_READONLY (e) = 1;
1662 return e;
1665 e = mark_type_use (e);
1667 if (VAR_P (e))
1668 t = size_int (DECL_ALIGN_UNIT (e));
1669 else if (TREE_CODE (e) == COMPONENT_REF
1670 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1671 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1673 if (complain & tf_error)
1674 error ("invalid application of %<__alignof%> to a bit-field");
1675 else
1676 return error_mark_node;
1677 t = size_one_node;
1679 else if (TREE_CODE (e) == COMPONENT_REF
1680 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1681 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1682 else if (is_overloaded_fn (e))
1684 if (complain & tf_error)
1685 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1686 "function type");
1687 else
1688 return error_mark_node;
1689 if (TREE_CODE (e) == FUNCTION_DECL)
1690 t = size_int (DECL_ALIGN_UNIT (e));
1691 else
1692 t = size_one_node;
1694 else if (type_unknown_p (e))
1696 if (complain & tf_error)
1697 cxx_incomplete_type_error (e, TREE_TYPE (e));
1698 else
1699 return error_mark_node;
1700 t = size_one_node;
1702 else
1703 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1704 complain & tf_error);
1706 return fold_convert (size_type_node, t);
1709 /* Process a sizeof or alignof expression E with code OP where the operand
1710 is an expression. */
1712 tree
1713 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1715 if (op == SIZEOF_EXPR)
1716 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1717 else
1718 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1721 /* Build a representation of an expression 'alignas(E).' Return the
1722 folded integer value of E if it is an integral constant expression
1723 that resolves to a valid alignment. If E depends on a template
1724 parameter, return a syntactic representation tree of kind
1725 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1726 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1728 tree
1729 cxx_alignas_expr (tree e)
1731 if (e == NULL_TREE || e == error_mark_node
1732 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1733 return e;
1735 if (TYPE_P (e))
1736 /* [dcl.align]/3:
1738 When the alignment-specifier is of the form
1739 alignas(type-id ), it shall have the same effect as
1740 alignas(alignof(type-id )). */
1742 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1744 /* If we reach this point, it means the alignas expression if of
1745 the form "alignas(assignment-expression)", so we should follow
1746 what is stated by [dcl.align]/2. */
1748 if (value_dependent_expression_p (e))
1749 /* Leave value-dependent expression alone for now. */
1750 return e;
1752 e = fold_non_dependent_expr (e);
1753 e = mark_rvalue_use (e);
1755 /* [dcl.align]/2 says:
1757 the assignment-expression shall be an integral constant
1758 expression. */
1760 return cxx_constant_value (e);
1764 /* EXPR is being used in a context that is not a function call.
1765 Enforce:
1767 [expr.ref]
1769 The expression can be used only as the left-hand operand of a
1770 member function call.
1772 [expr.mptr.operator]
1774 If the result of .* or ->* is a function, then that result can be
1775 used only as the operand for the function call operator ().
1777 by issuing an error message if appropriate. Returns true iff EXPR
1778 violates these rules. */
1780 bool
1781 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1783 if (expr == NULL_TREE)
1784 return false;
1785 /* Don't enforce this in MS mode. */
1786 if (flag_ms_extensions)
1787 return false;
1788 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1789 expr = get_first_fn (expr);
1790 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1792 if (complain & tf_error)
1793 error ("invalid use of non-static member function");
1794 return true;
1796 return false;
1799 /* If EXP is a reference to a bitfield, and the type of EXP does not
1800 match the declared type of the bitfield, return the declared type
1801 of the bitfield. Otherwise, return NULL_TREE. */
1803 tree
1804 is_bitfield_expr_with_lowered_type (const_tree exp)
1806 switch (TREE_CODE (exp))
1808 case COND_EXPR:
1809 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1810 ? TREE_OPERAND (exp, 1)
1811 : TREE_OPERAND (exp, 0)))
1812 return NULL_TREE;
1813 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1815 case COMPOUND_EXPR:
1816 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1818 case MODIFY_EXPR:
1819 case SAVE_EXPR:
1820 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1822 case COMPONENT_REF:
1824 tree field;
1826 field = TREE_OPERAND (exp, 1);
1827 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1828 return NULL_TREE;
1829 if (same_type_ignoring_top_level_qualifiers_p
1830 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1831 return NULL_TREE;
1832 return DECL_BIT_FIELD_TYPE (field);
1835 CASE_CONVERT:
1836 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1837 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1838 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1839 /* Fallthrough. */
1841 default:
1842 return NULL_TREE;
1846 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1847 bitfield with a lowered type, the type of EXP is returned, rather
1848 than NULL_TREE. */
1850 tree
1851 unlowered_expr_type (const_tree exp)
1853 tree type;
1854 tree etype = TREE_TYPE (exp);
1856 type = is_bitfield_expr_with_lowered_type (exp);
1857 if (type)
1858 type = cp_build_qualified_type (type, cp_type_quals (etype));
1859 else
1860 type = etype;
1862 return type;
1865 /* Perform the conversions in [expr] that apply when an lvalue appears
1866 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1867 function-to-pointer conversions. In addition, manifest constants
1868 are replaced by their values, and bitfield references are converted
1869 to their declared types. Note that this function does not perform the
1870 lvalue-to-rvalue conversion for class types. If you need that conversion
1871 to for class types, then you probably need to use force_rvalue.
1873 Although the returned value is being used as an rvalue, this
1874 function does not wrap the returned expression in a
1875 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1876 that the return value is no longer an lvalue. */
1878 tree
1879 decay_conversion (tree exp, tsubst_flags_t complain)
1881 tree type;
1882 enum tree_code code;
1883 location_t loc = EXPR_LOC_OR_HERE (exp);
1885 type = TREE_TYPE (exp);
1886 if (type == error_mark_node)
1887 return error_mark_node;
1889 exp = mark_rvalue_use (exp);
1891 exp = resolve_nondeduced_context (exp);
1892 if (type_unknown_p (exp))
1894 if (complain & tf_error)
1895 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1896 return error_mark_node;
1899 code = TREE_CODE (type);
1901 /* For an array decl decay_conversion should not try to return its
1902 initializer. */
1903 if (code != ARRAY_TYPE)
1905 /* FIXME remove? at least need to remember that this isn't really a
1906 constant expression if EXP isn't decl_constant_var_p, like with
1907 C_MAYBE_CONST_EXPR. */
1908 exp = decl_constant_value_safe (exp);
1909 if (error_operand_p (exp))
1910 return error_mark_node;
1913 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1914 return nullptr_node;
1916 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1917 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1918 if (code == VOID_TYPE)
1920 if (complain & tf_error)
1921 error_at (loc, "void value not ignored as it ought to be");
1922 return error_mark_node;
1924 if (invalid_nonstatic_memfn_p (exp, complain))
1925 return error_mark_node;
1926 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1927 return cp_build_addr_expr (exp, complain);
1928 if (code == ARRAY_TYPE)
1930 tree adr;
1931 tree ptrtype;
1933 if (INDIRECT_REF_P (exp))
1934 return build_nop (build_pointer_type (TREE_TYPE (type)),
1935 TREE_OPERAND (exp, 0));
1937 if (TREE_CODE (exp) == COMPOUND_EXPR)
1939 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1940 if (op1 == error_mark_node)
1941 return error_mark_node;
1942 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1943 TREE_OPERAND (exp, 0), op1);
1946 if (!lvalue_p (exp)
1947 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1949 if (complain & tf_error)
1950 error_at (loc, "invalid use of non-lvalue array");
1951 return error_mark_node;
1954 /* Don't let an array compound literal decay to a pointer. It can
1955 still be used to initialize an array or bind to a reference. */
1956 if (TREE_CODE (exp) == TARGET_EXPR)
1958 if (complain & tf_error)
1959 error_at (loc, "taking address of temporary array");
1960 return error_mark_node;
1963 ptrtype = build_pointer_type (TREE_TYPE (type));
1965 if (VAR_P (exp))
1967 if (!cxx_mark_addressable (exp))
1968 return error_mark_node;
1969 adr = build_nop (ptrtype, build_address (exp));
1970 return adr;
1972 /* This way is better for a COMPONENT_REF since it can
1973 simplify the offset for a component. */
1974 adr = cp_build_addr_expr (exp, complain);
1975 return cp_convert (ptrtype, adr, complain);
1978 /* If a bitfield is used in a context where integral promotion
1979 applies, then the caller is expected to have used
1980 default_conversion. That function promotes bitfields correctly
1981 before calling this function. At this point, if we have a
1982 bitfield referenced, we may assume that is not subject to
1983 promotion, and that, therefore, the type of the resulting rvalue
1984 is the declared type of the bitfield. */
1985 exp = convert_bitfield_to_declared_type (exp);
1987 /* We do not call rvalue() here because we do not want to wrap EXP
1988 in a NON_LVALUE_EXPR. */
1990 /* [basic.lval]
1992 Non-class rvalues always have cv-unqualified types. */
1993 type = TREE_TYPE (exp);
1994 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1995 exp = build_nop (cv_unqualified (type), exp);
1997 return exp;
2000 /* Perform preparatory conversions, as part of the "usual arithmetic
2001 conversions". In particular, as per [expr]:
2003 Whenever an lvalue expression appears as an operand of an
2004 operator that expects the rvalue for that operand, the
2005 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2006 standard conversions are applied to convert the expression to an
2007 rvalue.
2009 In addition, we perform integral promotions here, as those are
2010 applied to both operands to a binary operator before determining
2011 what additional conversions should apply. */
2013 static tree
2014 cp_default_conversion (tree exp, tsubst_flags_t complain)
2016 /* Check for target-specific promotions. */
2017 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2018 if (promoted_type)
2019 exp = cp_convert (promoted_type, exp, complain);
2020 /* Perform the integral promotions first so that bitfield
2021 expressions (which may promote to "int", even if the bitfield is
2022 declared "unsigned") are promoted correctly. */
2023 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2024 exp = cp_perform_integral_promotions (exp, complain);
2025 /* Perform the other conversions. */
2026 exp = decay_conversion (exp, complain);
2028 return exp;
2031 /* C version. */
2033 tree
2034 default_conversion (tree exp)
2036 return cp_default_conversion (exp, tf_warning_or_error);
2039 /* EXPR is an expression with an integral or enumeration type.
2040 Perform the integral promotions in [conv.prom], and return the
2041 converted value. */
2043 tree
2044 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2046 tree type;
2047 tree promoted_type;
2049 expr = mark_rvalue_use (expr);
2051 /* [conv.prom]
2053 If the bitfield has an enumerated type, it is treated as any
2054 other value of that type for promotion purposes. */
2055 type = is_bitfield_expr_with_lowered_type (expr);
2056 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2057 type = TREE_TYPE (expr);
2058 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2059 /* Scoped enums don't promote. */
2060 if (SCOPED_ENUM_P (type))
2061 return expr;
2062 promoted_type = type_promotes_to (type);
2063 if (type != promoted_type)
2064 expr = cp_convert (promoted_type, expr, complain);
2065 return expr;
2068 /* C version. */
2070 tree
2071 perform_integral_promotions (tree expr)
2073 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2076 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2077 decay_conversion to one. */
2080 string_conv_p (const_tree totype, const_tree exp, int warn)
2082 tree t;
2084 if (!TYPE_PTR_P (totype))
2085 return 0;
2087 t = TREE_TYPE (totype);
2088 if (!same_type_p (t, char_type_node)
2089 && !same_type_p (t, char16_type_node)
2090 && !same_type_p (t, char32_type_node)
2091 && !same_type_p (t, wchar_type_node))
2092 return 0;
2094 if (TREE_CODE (exp) == STRING_CST)
2096 /* Make sure that we don't try to convert between char and wide chars. */
2097 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2098 return 0;
2100 else
2102 /* Is this a string constant which has decayed to 'const char *'? */
2103 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2104 if (!same_type_p (TREE_TYPE (exp), t))
2105 return 0;
2106 STRIP_NOPS (exp);
2107 if (TREE_CODE (exp) != ADDR_EXPR
2108 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2109 return 0;
2112 /* This warning is not very useful, as it complains about printf. */
2113 if (warn)
2114 warning (OPT_Wwrite_strings,
2115 "deprecated conversion from string constant to %qT",
2116 totype);
2118 return 1;
2121 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2122 can, for example, use as an lvalue. This code used to be in
2123 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2124 expressions, where we're dealing with aggregates. But now it's again only
2125 called from unary_complex_lvalue. The case (in particular) that led to
2126 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2127 get it there. */
2129 static tree
2130 rationalize_conditional_expr (enum tree_code code, tree t,
2131 tsubst_flags_t complain)
2133 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2134 the first operand is always the one to be used if both operands
2135 are equal, so we know what conditional expression this used to be. */
2136 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2138 tree op0 = TREE_OPERAND (t, 0);
2139 tree op1 = TREE_OPERAND (t, 1);
2141 /* The following code is incorrect if either operand side-effects. */
2142 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2143 && !TREE_SIDE_EFFECTS (op1));
2144 return
2145 build_conditional_expr (EXPR_LOC_OR_HERE (t),
2146 build_x_binary_op (EXPR_LOC_OR_HERE (t),
2147 (TREE_CODE (t) == MIN_EXPR
2148 ? LE_EXPR : GE_EXPR),
2149 op0, TREE_CODE (op0),
2150 op1, TREE_CODE (op1),
2151 /*overload=*/NULL,
2152 complain),
2153 cp_build_unary_op (code, op0, 0, complain),
2154 cp_build_unary_op (code, op1, 0, complain),
2155 complain);
2158 return
2159 build_conditional_expr (EXPR_LOC_OR_HERE (t), TREE_OPERAND (t, 0),
2160 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2161 complain),
2162 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2163 complain),
2164 complain);
2167 /* Given the TYPE of an anonymous union field inside T, return the
2168 FIELD_DECL for the field. If not found return NULL_TREE. Because
2169 anonymous unions can nest, we must also search all anonymous unions
2170 that are directly reachable. */
2172 tree
2173 lookup_anon_field (tree t, tree type)
2175 tree field;
2177 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2179 if (TREE_STATIC (field))
2180 continue;
2181 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2182 continue;
2184 /* If we find it directly, return the field. */
2185 if (DECL_NAME (field) == NULL_TREE
2186 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2188 return field;
2191 /* Otherwise, it could be nested, search harder. */
2192 if (DECL_NAME (field) == NULL_TREE
2193 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2195 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2196 if (subfield)
2197 return subfield;
2200 return NULL_TREE;
2203 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2204 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2205 non-NULL, it indicates the path to the base used to name MEMBER.
2206 If PRESERVE_REFERENCE is true, the expression returned will have
2207 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2208 returned will have the type referred to by the reference.
2210 This function does not perform access control; that is either done
2211 earlier by the parser when the name of MEMBER is resolved to MEMBER
2212 itself, or later when overload resolution selects one of the
2213 functions indicated by MEMBER. */
2215 tree
2216 build_class_member_access_expr (tree object, tree member,
2217 tree access_path, bool preserve_reference,
2218 tsubst_flags_t complain)
2220 tree object_type;
2221 tree member_scope;
2222 tree result = NULL_TREE;
2223 tree using_decl = NULL_TREE;
2225 if (error_operand_p (object) || error_operand_p (member))
2226 return error_mark_node;
2228 gcc_assert (DECL_P (member) || BASELINK_P (member));
2230 /* [expr.ref]
2232 The type of the first expression shall be "class object" (of a
2233 complete type). */
2234 object_type = TREE_TYPE (object);
2235 if (!currently_open_class (object_type)
2236 && !complete_type_or_maybe_complain (object_type, object, complain))
2237 return error_mark_node;
2238 if (!CLASS_TYPE_P (object_type))
2240 if (complain & tf_error)
2242 if (POINTER_TYPE_P (object_type)
2243 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2244 error ("request for member %qD in %qE, which is of pointer "
2245 "type %qT (maybe you meant to use %<->%> ?)",
2246 member, object, object_type);
2247 else
2248 error ("request for member %qD in %qE, which is of non-class "
2249 "type %qT", member, object, object_type);
2251 return error_mark_node;
2254 /* The standard does not seem to actually say that MEMBER must be a
2255 member of OBJECT_TYPE. However, that is clearly what is
2256 intended. */
2257 if (DECL_P (member))
2259 member_scope = DECL_CLASS_CONTEXT (member);
2260 mark_used (member);
2261 if (TREE_DEPRECATED (member))
2262 warn_deprecated_use (member, NULL_TREE);
2264 else
2265 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2266 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2267 presently be the anonymous union. Go outwards until we find a
2268 type related to OBJECT_TYPE. */
2269 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2270 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2271 object_type))
2272 member_scope = TYPE_CONTEXT (member_scope);
2273 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2275 if (complain & tf_error)
2277 if (TREE_CODE (member) == FIELD_DECL)
2278 error ("invalid use of nonstatic data member %qE", member);
2279 else
2280 error ("%qD is not a member of %qT", member, object_type);
2282 return error_mark_node;
2285 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2286 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2287 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2289 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2290 if (temp)
2291 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2294 /* In [expr.ref], there is an explicit list of the valid choices for
2295 MEMBER. We check for each of those cases here. */
2296 if (VAR_P (member))
2298 /* A static data member. */
2299 result = member;
2300 mark_exp_read (object);
2301 /* If OBJECT has side-effects, they are supposed to occur. */
2302 if (TREE_SIDE_EFFECTS (object))
2303 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2305 else if (TREE_CODE (member) == FIELD_DECL)
2307 /* A non-static data member. */
2308 bool null_object_p;
2309 int type_quals;
2310 tree member_type;
2312 null_object_p = (INDIRECT_REF_P (object)
2313 && integer_zerop (TREE_OPERAND (object, 0)));
2315 /* Convert OBJECT to the type of MEMBER. */
2316 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2317 TYPE_MAIN_VARIANT (member_scope)))
2319 tree binfo;
2320 base_kind kind;
2322 binfo = lookup_base (access_path ? access_path : object_type,
2323 member_scope, ba_unique, &kind, complain);
2324 if (binfo == error_mark_node)
2325 return error_mark_node;
2327 /* It is invalid to try to get to a virtual base of a
2328 NULL object. The most common cause is invalid use of
2329 offsetof macro. */
2330 if (null_object_p && kind == bk_via_virtual)
2332 if (complain & tf_error)
2334 error ("invalid access to non-static data member %qD of "
2335 "NULL object",
2336 member);
2337 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2339 return error_mark_node;
2342 /* Convert to the base. */
2343 object = build_base_path (PLUS_EXPR, object, binfo,
2344 /*nonnull=*/1, complain);
2345 /* If we found the base successfully then we should be able
2346 to convert to it successfully. */
2347 gcc_assert (object != error_mark_node);
2350 /* Complain about other invalid uses of offsetof, even though they will
2351 give the right answer. Note that we complain whether or not they
2352 actually used the offsetof macro, since there's no way to know at this
2353 point. So we just give a warning, instead of a pedwarn. */
2354 /* Do not produce this warning for base class field references, because
2355 we know for a fact that didn't come from offsetof. This does occur
2356 in various testsuite cases where a null object is passed where a
2357 vtable access is required. */
2358 if (null_object_p && warn_invalid_offsetof
2359 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2360 && !DECL_FIELD_IS_BASE (member)
2361 && cp_unevaluated_operand == 0
2362 && (complain & tf_warning))
2364 warning (OPT_Winvalid_offsetof,
2365 "invalid access to non-static data member %qD "
2366 " of NULL object", member);
2367 warning (OPT_Winvalid_offsetof,
2368 "(perhaps the %<offsetof%> macro was used incorrectly)");
2371 /* If MEMBER is from an anonymous aggregate, we have converted
2372 OBJECT so that it refers to the class containing the
2373 anonymous union. Generate a reference to the anonymous union
2374 itself, and recur to find MEMBER. */
2375 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2376 /* When this code is called from build_field_call, the
2377 object already has the type of the anonymous union.
2378 That is because the COMPONENT_REF was already
2379 constructed, and was then disassembled before calling
2380 build_field_call. After the function-call code is
2381 cleaned up, this waste can be eliminated. */
2382 && (!same_type_ignoring_top_level_qualifiers_p
2383 (TREE_TYPE (object), DECL_CONTEXT (member))))
2385 tree anonymous_union;
2387 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2388 DECL_CONTEXT (member));
2389 object = build_class_member_access_expr (object,
2390 anonymous_union,
2391 /*access_path=*/NULL_TREE,
2392 preserve_reference,
2393 complain);
2396 /* Compute the type of the field, as described in [expr.ref]. */
2397 type_quals = TYPE_UNQUALIFIED;
2398 member_type = TREE_TYPE (member);
2399 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2401 type_quals = (cp_type_quals (member_type)
2402 | cp_type_quals (object_type));
2404 /* A field is const (volatile) if the enclosing object, or the
2405 field itself, is const (volatile). But, a mutable field is
2406 not const, even within a const object. */
2407 if (DECL_MUTABLE_P (member))
2408 type_quals &= ~TYPE_QUAL_CONST;
2409 member_type = cp_build_qualified_type (member_type, type_quals);
2412 result = build3 (COMPONENT_REF, member_type, object, member,
2413 NULL_TREE);
2414 result = fold_if_not_in_template (result);
2416 /* Mark the expression const or volatile, as appropriate. Even
2417 though we've dealt with the type above, we still have to mark the
2418 expression itself. */
2419 if (type_quals & TYPE_QUAL_CONST)
2420 TREE_READONLY (result) = 1;
2421 if (type_quals & TYPE_QUAL_VOLATILE)
2422 TREE_THIS_VOLATILE (result) = 1;
2424 else if (BASELINK_P (member))
2426 /* The member is a (possibly overloaded) member function. */
2427 tree functions;
2428 tree type;
2430 /* If the MEMBER is exactly one static member function, then we
2431 know the type of the expression. Otherwise, we must wait
2432 until overload resolution has been performed. */
2433 functions = BASELINK_FUNCTIONS (member);
2434 if (TREE_CODE (functions) == FUNCTION_DECL
2435 && DECL_STATIC_FUNCTION_P (functions))
2436 type = TREE_TYPE (functions);
2437 else
2438 type = unknown_type_node;
2439 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2440 base. That will happen when the function is called. */
2441 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2443 else if (TREE_CODE (member) == CONST_DECL)
2445 /* The member is an enumerator. */
2446 result = member;
2447 /* If OBJECT has side-effects, they are supposed to occur. */
2448 if (TREE_SIDE_EFFECTS (object))
2449 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2450 object, result);
2452 else if ((using_decl = strip_using_decl (member)) != member)
2453 result = build_class_member_access_expr (object,
2454 using_decl,
2455 access_path, preserve_reference,
2456 complain);
2457 else
2459 if (complain & tf_error)
2460 error ("invalid use of %qD", member);
2461 return error_mark_node;
2464 if (!preserve_reference)
2465 /* [expr.ref]
2467 If E2 is declared to have type "reference to T", then ... the
2468 type of E1.E2 is T. */
2469 result = convert_from_reference (result);
2471 return result;
2474 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2475 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2477 static tree
2478 lookup_destructor (tree object, tree scope, tree dtor_name,
2479 tsubst_flags_t complain)
2481 tree object_type = TREE_TYPE (object);
2482 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2483 tree expr;
2485 if (scope && !check_dtor_name (scope, dtor_type))
2487 if (complain & tf_error)
2488 error ("qualified type %qT does not match destructor name ~%qT",
2489 scope, dtor_type);
2490 return error_mark_node;
2492 if (is_auto (dtor_type))
2493 dtor_type = object_type;
2494 else if (identifier_p (dtor_type))
2496 /* In a template, names we can't find a match for are still accepted
2497 destructor names, and we check them here. */
2498 if (check_dtor_name (object_type, dtor_type))
2499 dtor_type = object_type;
2500 else
2502 if (complain & tf_error)
2503 error ("object type %qT does not match destructor name ~%qT",
2504 object_type, dtor_type);
2505 return error_mark_node;
2509 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2511 if (complain & tf_error)
2512 error ("the type being destroyed is %qT, but the destructor "
2513 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2514 return error_mark_node;
2516 expr = lookup_member (dtor_type, complete_dtor_identifier,
2517 /*protect=*/1, /*want_type=*/false,
2518 tf_warning_or_error);
2519 expr = (adjust_result_of_qualified_name_lookup
2520 (expr, dtor_type, object_type));
2521 if (scope == NULL_TREE)
2522 /* We need to call adjust_result_of_qualified_name_lookup in case the
2523 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2524 that we still get virtual function binding. */
2525 BASELINK_QUALIFIED_P (expr) = false;
2526 return expr;
2529 /* An expression of the form "A::template B" has been resolved to
2530 DECL. Issue a diagnostic if B is not a template or template
2531 specialization. */
2533 void
2534 check_template_keyword (tree decl)
2536 /* The standard says:
2538 [temp.names]
2540 If a name prefixed by the keyword template is not a member
2541 template, the program is ill-formed.
2543 DR 228 removed the restriction that the template be a member
2544 template.
2546 DR 96, if accepted would add the further restriction that explicit
2547 template arguments must be provided if the template keyword is
2548 used, but, as of 2005-10-16, that DR is still in "drafting". If
2549 this DR is accepted, then the semantic checks here can be
2550 simplified, as the entity named must in fact be a template
2551 specialization, rather than, as at present, a set of overloaded
2552 functions containing at least one template function. */
2553 if (TREE_CODE (decl) != TEMPLATE_DECL
2554 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2556 if (!is_overloaded_fn (decl))
2557 permerror (input_location, "%qD is not a template", decl);
2558 else
2560 tree fns;
2561 fns = decl;
2562 if (BASELINK_P (fns))
2563 fns = BASELINK_FUNCTIONS (fns);
2564 while (fns)
2566 tree fn = OVL_CURRENT (fns);
2567 if (TREE_CODE (fn) == TEMPLATE_DECL
2568 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2569 break;
2570 if (TREE_CODE (fn) == FUNCTION_DECL
2571 && DECL_USE_TEMPLATE (fn)
2572 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2573 break;
2574 fns = OVL_NEXT (fns);
2576 if (!fns)
2577 permerror (input_location, "%qD is not a template", decl);
2582 /* This function is called by the parser to process a class member
2583 access expression of the form OBJECT.NAME. NAME is a node used by
2584 the parser to represent a name; it is not yet a DECL. It may,
2585 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2586 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2587 there is no reason to do the lookup twice, so the parser keeps the
2588 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2589 be a template via the use of the "A::template B" syntax. */
2591 tree
2592 finish_class_member_access_expr (tree object, tree name, bool template_p,
2593 tsubst_flags_t complain)
2595 tree expr;
2596 tree object_type;
2597 tree member;
2598 tree access_path = NULL_TREE;
2599 tree orig_object = object;
2600 tree orig_name = name;
2602 if (object == error_mark_node || name == error_mark_node)
2603 return error_mark_node;
2605 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2606 if (!objc_is_public (object, name))
2607 return error_mark_node;
2609 object_type = TREE_TYPE (object);
2611 if (processing_template_decl)
2613 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2614 dependent_type_p (object_type)
2615 /* If NAME is just an IDENTIFIER_NODE, then the expression
2616 is dependent. */
2617 || identifier_p (object)
2618 /* If NAME is "f<args>", where either 'f' or 'args' is
2619 dependent, then the expression is dependent. */
2620 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2621 && dependent_template_id_p (TREE_OPERAND (name, 0),
2622 TREE_OPERAND (name, 1)))
2623 /* If NAME is "T::X" where "T" is dependent, then the
2624 expression is dependent. */
2625 || (TREE_CODE (name) == SCOPE_REF
2626 && TYPE_P (TREE_OPERAND (name, 0))
2627 && dependent_type_p (TREE_OPERAND (name, 0))))
2628 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2629 object, name, NULL_TREE);
2630 object = build_non_dependent_expr (object);
2632 else if (c_dialect_objc ()
2633 && identifier_p (name)
2634 && (expr = objc_maybe_build_component_ref (object, name)))
2635 return expr;
2637 /* [expr.ref]
2639 The type of the first expression shall be "class object" (of a
2640 complete type). */
2641 if (!currently_open_class (object_type)
2642 && !complete_type_or_maybe_complain (object_type, object, complain))
2643 return error_mark_node;
2644 if (!CLASS_TYPE_P (object_type))
2646 if (complain & tf_error)
2648 if (POINTER_TYPE_P (object_type)
2649 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2650 error ("request for member %qD in %qE, which is of pointer "
2651 "type %qT (maybe you meant to use %<->%> ?)",
2652 name, object, object_type);
2653 else
2654 error ("request for member %qD in %qE, which is of non-class "
2655 "type %qT", name, object, object_type);
2657 return error_mark_node;
2660 if (BASELINK_P (name))
2661 /* A member function that has already been looked up. */
2662 member = name;
2663 else
2665 bool is_template_id = false;
2666 tree template_args = NULL_TREE;
2667 tree scope;
2669 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2671 is_template_id = true;
2672 template_args = TREE_OPERAND (name, 1);
2673 name = TREE_OPERAND (name, 0);
2675 if (TREE_CODE (name) == OVERLOAD)
2676 name = DECL_NAME (get_first_fn (name));
2677 else if (DECL_P (name))
2678 name = DECL_NAME (name);
2681 if (TREE_CODE (name) == SCOPE_REF)
2683 /* A qualified name. The qualifying class or namespace `S'
2684 has already been looked up; it is either a TYPE or a
2685 NAMESPACE_DECL. */
2686 scope = TREE_OPERAND (name, 0);
2687 name = TREE_OPERAND (name, 1);
2689 /* If SCOPE is a namespace, then the qualified name does not
2690 name a member of OBJECT_TYPE. */
2691 if (TREE_CODE (scope) == NAMESPACE_DECL)
2693 if (complain & tf_error)
2694 error ("%<%D::%D%> is not a member of %qT",
2695 scope, name, object_type);
2696 return error_mark_node;
2699 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2701 /* Looking up a member enumerator (c++/56793). */
2702 if (!TYPE_CLASS_SCOPE_P (scope)
2703 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2705 if (complain & tf_error)
2706 error ("%<%D::%D%> is not a member of %qT",
2707 scope, name, object_type);
2708 return error_mark_node;
2710 tree val = lookup_enumerator (scope, name);
2711 if (TREE_SIDE_EFFECTS (object))
2712 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2713 return val;
2716 gcc_assert (CLASS_TYPE_P (scope));
2717 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2719 if (constructor_name_p (name, scope))
2721 if (complain & tf_error)
2722 error ("cannot call constructor %<%T::%D%> directly",
2723 scope, name);
2724 return error_mark_node;
2727 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2728 access_path = lookup_base (object_type, scope, ba_check,
2729 NULL, complain);
2730 if (access_path == error_mark_node)
2731 return error_mark_node;
2732 if (!access_path)
2734 if (complain & tf_error)
2735 error ("%qT is not a base of %qT", scope, object_type);
2736 return error_mark_node;
2739 else
2741 scope = NULL_TREE;
2742 access_path = object_type;
2745 if (TREE_CODE (name) == BIT_NOT_EXPR)
2746 member = lookup_destructor (object, scope, name, complain);
2747 else
2749 /* Look up the member. */
2750 member = lookup_member (access_path, name, /*protect=*/1,
2751 /*want_type=*/false, complain);
2752 if (member == NULL_TREE)
2754 if (complain & tf_error)
2755 error ("%qD has no member named %qE",
2756 TREE_CODE (access_path) == TREE_BINFO
2757 ? TREE_TYPE (access_path) : object_type, name);
2758 return error_mark_node;
2760 if (member == error_mark_node)
2761 return error_mark_node;
2764 if (is_template_id)
2766 tree templ = member;
2768 if (BASELINK_P (templ))
2769 templ = lookup_template_function (templ, template_args);
2770 else
2772 if (complain & tf_error)
2773 error ("%qD is not a member template function", name);
2774 return error_mark_node;
2779 if (TREE_DEPRECATED (member))
2780 warn_deprecated_use (member, NULL_TREE);
2782 if (template_p)
2783 check_template_keyword (member);
2785 expr = build_class_member_access_expr (object, member, access_path,
2786 /*preserve_reference=*/false,
2787 complain);
2788 if (processing_template_decl && expr != error_mark_node)
2790 if (BASELINK_P (member))
2792 if (TREE_CODE (orig_name) == SCOPE_REF)
2793 BASELINK_QUALIFIED_P (member) = 1;
2794 orig_name = member;
2796 return build_min_non_dep (COMPONENT_REF, expr,
2797 orig_object, orig_name,
2798 NULL_TREE);
2801 return expr;
2804 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2805 type. */
2807 tree
2808 build_simple_component_ref (tree object, tree member)
2810 tree type = cp_build_qualified_type (TREE_TYPE (member),
2811 cp_type_quals (TREE_TYPE (object)));
2812 return fold_build3_loc (input_location,
2813 COMPONENT_REF, type,
2814 object, member, NULL_TREE);
2817 /* Return an expression for the MEMBER_NAME field in the internal
2818 representation of PTRMEM, a pointer-to-member function. (Each
2819 pointer-to-member function type gets its own RECORD_TYPE so it is
2820 more convenient to access the fields by name than by FIELD_DECL.)
2821 This routine converts the NAME to a FIELD_DECL and then creates the
2822 node for the complete expression. */
2824 tree
2825 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2827 tree ptrmem_type;
2828 tree member;
2830 /* This code is a stripped down version of
2831 build_class_member_access_expr. It does not work to use that
2832 routine directly because it expects the object to be of class
2833 type. */
2834 ptrmem_type = TREE_TYPE (ptrmem);
2835 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2836 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2837 /*want_type=*/false, tf_warning_or_error);
2838 return build_simple_component_ref (ptrmem, member);
2841 /* Given an expression PTR for a pointer, return an expression
2842 for the value pointed to.
2843 ERRORSTRING is the name of the operator to appear in error messages.
2845 This function may need to overload OPERATOR_FNNAME.
2846 Must also handle REFERENCE_TYPEs for C++. */
2848 tree
2849 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2850 tsubst_flags_t complain)
2852 tree orig_expr = expr;
2853 tree rval;
2855 if (processing_template_decl)
2857 /* Retain the type if we know the operand is a pointer. */
2858 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2859 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2860 if (type_dependent_expression_p (expr))
2861 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2862 expr = build_non_dependent_expr (expr);
2865 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2866 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2867 if (!rval)
2868 rval = cp_build_indirect_ref (expr, errorstring, complain);
2870 if (processing_template_decl && rval != error_mark_node)
2871 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2872 else
2873 return rval;
2876 /* Helper function called from c-common. */
2877 tree
2878 build_indirect_ref (location_t /*loc*/,
2879 tree ptr, ref_operator errorstring)
2881 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2884 tree
2885 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2886 tsubst_flags_t complain)
2888 tree pointer, type;
2890 if (ptr == current_class_ptr
2891 || (TREE_CODE (ptr) == NOP_EXPR
2892 && TREE_OPERAND (ptr, 0) == current_class_ptr
2893 && (same_type_ignoring_top_level_qualifiers_p
2894 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2895 return current_class_ref;
2897 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2898 ? ptr : decay_conversion (ptr, complain));
2899 if (pointer == error_mark_node)
2900 return error_mark_node;
2902 type = TREE_TYPE (pointer);
2904 if (POINTER_TYPE_P (type))
2906 /* [expr.unary.op]
2908 If the type of the expression is "pointer to T," the type
2909 of the result is "T." */
2910 tree t = TREE_TYPE (type);
2912 if (CONVERT_EXPR_P (ptr)
2913 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2915 /* If a warning is issued, mark it to avoid duplicates from
2916 the backend. This only needs to be done at
2917 warn_strict_aliasing > 2. */
2918 if (warn_strict_aliasing > 2)
2919 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2920 type, TREE_OPERAND (ptr, 0)))
2921 TREE_NO_WARNING (ptr) = 1;
2924 if (VOID_TYPE_P (t))
2926 /* A pointer to incomplete type (other than cv void) can be
2927 dereferenced [expr.unary.op]/1 */
2928 if (complain & tf_error)
2929 error ("%qT is not a pointer-to-object type", type);
2930 return error_mark_node;
2932 else if (TREE_CODE (pointer) == ADDR_EXPR
2933 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2934 /* The POINTER was something like `&x'. We simplify `*&x' to
2935 `x'. */
2936 return TREE_OPERAND (pointer, 0);
2937 else
2939 tree ref = build1 (INDIRECT_REF, t, pointer);
2941 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2942 so that we get the proper error message if the result is used
2943 to assign to. Also, &* is supposed to be a no-op. */
2944 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2945 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2946 TREE_SIDE_EFFECTS (ref)
2947 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2948 return ref;
2951 else if (!(complain & tf_error))
2952 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2954 /* `pointer' won't be an error_mark_node if we were given a
2955 pointer to member, so it's cool to check for this here. */
2956 else if (TYPE_PTRMEM_P (type))
2957 switch (errorstring)
2959 case RO_ARRAY_INDEXING:
2960 error ("invalid use of array indexing on pointer to member");
2961 break;
2962 case RO_UNARY_STAR:
2963 error ("invalid use of unary %<*%> on pointer to member");
2964 break;
2965 case RO_IMPLICIT_CONVERSION:
2966 error ("invalid use of implicit conversion on pointer to member");
2967 break;
2968 case RO_ARROW_STAR:
2969 error ("left hand operand of %<->*%> must be a pointer to class, "
2970 "but is a pointer to member of type %qT", type);
2971 break;
2972 default:
2973 gcc_unreachable ();
2975 else if (pointer != error_mark_node)
2976 invalid_indirection_error (input_location, type, errorstring);
2978 return error_mark_node;
2981 /* This handles expressions of the form "a[i]", which denotes
2982 an array reference.
2984 This is logically equivalent in C to *(a+i), but we may do it differently.
2985 If A is a variable or a member, we generate a primitive ARRAY_REF.
2986 This avoids forcing the array out of registers, and can work on
2987 arrays that are not lvalues (for example, members of structures returned
2988 by functions).
2990 If INDEX is of some user-defined type, it must be converted to
2991 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2992 will inherit the type of the array, which will be some pointer type.
2994 LOC is the location to use in building the array reference. */
2996 tree
2997 cp_build_array_ref (location_t loc, tree array, tree idx,
2998 tsubst_flags_t complain)
3000 tree ret;
3002 if (idx == 0)
3004 if (complain & tf_error)
3005 error_at (loc, "subscript missing in array reference");
3006 return error_mark_node;
3009 /* If an array's index is an array notation, then its rank cannot be
3010 greater than one. */
3011 if (flag_enable_cilkplus && contains_array_notation_expr (idx))
3013 size_t rank = 0;
3015 /* If find_rank returns false, then it should have reported an error,
3016 thus it is unnecessary for repetition. */
3017 if (!find_rank (loc, idx, idx, true, &rank))
3018 return error_mark_node;
3019 if (rank > 1)
3021 error_at (loc, "rank of the array%'s index is greater than 1");
3022 return error_mark_node;
3025 if (TREE_TYPE (array) == error_mark_node
3026 || TREE_TYPE (idx) == error_mark_node)
3027 return error_mark_node;
3029 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3030 inside it. */
3031 switch (TREE_CODE (array))
3033 case COMPOUND_EXPR:
3035 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3036 complain);
3037 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3038 TREE_OPERAND (array, 0), value);
3039 SET_EXPR_LOCATION (ret, loc);
3040 return ret;
3043 case COND_EXPR:
3044 ret = build_conditional_expr
3045 (loc, TREE_OPERAND (array, 0),
3046 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3047 complain),
3048 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3049 complain),
3050 complain);
3051 protected_set_expr_location (ret, loc);
3052 return ret;
3054 default:
3055 break;
3058 convert_vector_to_pointer_for_subscript (loc, &array, idx);
3060 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3062 tree rval, type;
3064 warn_array_subscript_with_type_char (idx);
3066 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3068 if (complain & tf_error)
3069 error_at (loc, "array subscript is not an integer");
3070 return error_mark_node;
3073 /* Apply integral promotions *after* noticing character types.
3074 (It is unclear why we do these promotions -- the standard
3075 does not say that we should. In fact, the natural thing would
3076 seem to be to convert IDX to ptrdiff_t; we're performing
3077 pointer arithmetic.) */
3078 idx = cp_perform_integral_promotions (idx, complain);
3080 /* An array that is indexed by a non-constant
3081 cannot be stored in a register; we must be able to do
3082 address arithmetic on its address.
3083 Likewise an array of elements of variable size. */
3084 if (TREE_CODE (idx) != INTEGER_CST
3085 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3086 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3087 != INTEGER_CST)))
3089 if (!cxx_mark_addressable (array))
3090 return error_mark_node;
3093 /* An array that is indexed by a constant value which is not within
3094 the array bounds cannot be stored in a register either; because we
3095 would get a crash in store_bit_field/extract_bit_field when trying
3096 to access a non-existent part of the register. */
3097 if (TREE_CODE (idx) == INTEGER_CST
3098 && TYPE_DOMAIN (TREE_TYPE (array))
3099 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3101 if (!cxx_mark_addressable (array))
3102 return error_mark_node;
3105 if (!lvalue_p (array) && (complain & tf_error))
3106 pedwarn (loc, OPT_Wpedantic,
3107 "ISO C++ forbids subscripting non-lvalue array");
3109 /* Note in C++ it is valid to subscript a `register' array, since
3110 it is valid to take the address of something with that
3111 storage specification. */
3112 if (extra_warnings)
3114 tree foo = array;
3115 while (TREE_CODE (foo) == COMPONENT_REF)
3116 foo = TREE_OPERAND (foo, 0);
3117 if (VAR_P (foo) && DECL_REGISTER (foo)
3118 && (complain & tf_warning))
3119 warning_at (loc, OPT_Wextra,
3120 "subscripting array declared %<register%>");
3123 type = TREE_TYPE (TREE_TYPE (array));
3124 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3125 /* Array ref is const/volatile if the array elements are
3126 or if the array is.. */
3127 TREE_READONLY (rval)
3128 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3129 TREE_SIDE_EFFECTS (rval)
3130 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3131 TREE_THIS_VOLATILE (rval)
3132 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3133 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3134 complain);
3135 protected_set_expr_location (ret, loc);
3136 return ret;
3140 tree ar = cp_default_conversion (array, complain);
3141 tree ind = cp_default_conversion (idx, complain);
3143 /* Put the integer in IND to simplify error checking. */
3144 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3146 tree temp = ar;
3147 ar = ind;
3148 ind = temp;
3151 if (ar == error_mark_node || ind == error_mark_node)
3152 return error_mark_node;
3154 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3156 if (complain & tf_error)
3157 error_at (loc, "subscripted value is neither array nor pointer");
3158 return error_mark_node;
3160 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3162 if (complain & tf_error)
3163 error_at (loc, "array subscript is not an integer");
3164 return error_mark_node;
3167 warn_array_subscript_with_type_char (idx);
3169 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3170 PLUS_EXPR, ar, ind,
3171 complain),
3172 RO_ARRAY_INDEXING,
3173 complain);
3174 protected_set_expr_location (ret, loc);
3175 return ret;
3179 /* Entry point for Obj-C++. */
3181 tree
3182 build_array_ref (location_t loc, tree array, tree idx)
3184 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3187 /* Resolve a pointer to member function. INSTANCE is the object
3188 instance to use, if the member points to a virtual member.
3190 This used to avoid checking for virtual functions if basetype
3191 has no virtual functions, according to an earlier ANSI draft.
3192 With the final ISO C++ rules, such an optimization is
3193 incorrect: A pointer to a derived member can be static_cast
3194 to pointer-to-base-member, as long as the dynamic object
3195 later has the right member. So now we only do this optimization
3196 when we know the dynamic type of the object. */
3198 tree
3199 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3200 tsubst_flags_t complain)
3202 if (TREE_CODE (function) == OFFSET_REF)
3203 function = TREE_OPERAND (function, 1);
3205 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3207 tree idx, delta, e1, e2, e3, vtbl;
3208 bool nonvirtual;
3209 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3210 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3212 tree instance_ptr = *instance_ptrptr;
3213 tree instance_save_expr = 0;
3214 if (instance_ptr == error_mark_node)
3216 if (TREE_CODE (function) == PTRMEM_CST)
3218 /* Extracting the function address from a pmf is only
3219 allowed with -Wno-pmf-conversions. It only works for
3220 pmf constants. */
3221 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3222 e1 = convert (fntype, e1);
3223 return e1;
3225 else
3227 if (complain & tf_error)
3228 error ("object missing in use of %qE", function);
3229 return error_mark_node;
3233 /* True if we know that the dynamic type of the object doesn't have
3234 virtual functions, so we can assume the PFN field is a pointer. */
3235 nonvirtual = (COMPLETE_TYPE_P (basetype)
3236 && !TYPE_POLYMORPHIC_P (basetype)
3237 && resolves_to_fixed_type_p (instance_ptr, 0));
3239 if (TREE_SIDE_EFFECTS (instance_ptr))
3240 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3242 if (TREE_SIDE_EFFECTS (function))
3243 function = save_expr (function);
3245 /* Start by extracting all the information from the PMF itself. */
3246 e3 = pfn_from_ptrmemfunc (function);
3247 delta = delta_from_ptrmemfunc (function);
3248 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3249 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3251 case ptrmemfunc_vbit_in_pfn:
3252 e1 = cp_build_binary_op (input_location,
3253 BIT_AND_EXPR, idx, integer_one_node,
3254 complain);
3255 idx = cp_build_binary_op (input_location,
3256 MINUS_EXPR, idx, integer_one_node,
3257 complain);
3258 if (idx == error_mark_node)
3259 return error_mark_node;
3260 break;
3262 case ptrmemfunc_vbit_in_delta:
3263 e1 = cp_build_binary_op (input_location,
3264 BIT_AND_EXPR, delta, integer_one_node,
3265 complain);
3266 delta = cp_build_binary_op (input_location,
3267 RSHIFT_EXPR, delta, integer_one_node,
3268 complain);
3269 if (delta == error_mark_node)
3270 return error_mark_node;
3271 break;
3273 default:
3274 gcc_unreachable ();
3277 if (e1 == error_mark_node)
3278 return error_mark_node;
3280 /* Convert down to the right base before using the instance. A
3281 special case is that in a pointer to member of class C, C may
3282 be incomplete. In that case, the function will of course be
3283 a member of C, and no conversion is required. In fact,
3284 lookup_base will fail in that case, because incomplete
3285 classes do not have BINFOs. */
3286 if (!same_type_ignoring_top_level_qualifiers_p
3287 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3289 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3290 basetype, ba_check, NULL, complain);
3291 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3292 1, complain);
3293 if (instance_ptr == error_mark_node)
3294 return error_mark_node;
3296 /* ...and then the delta in the PMF. */
3297 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3299 /* Hand back the adjusted 'this' argument to our caller. */
3300 *instance_ptrptr = instance_ptr;
3302 if (nonvirtual)
3303 /* Now just return the pointer. */
3304 return e3;
3306 /* Next extract the vtable pointer from the object. */
3307 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3308 instance_ptr);
3309 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3310 if (vtbl == error_mark_node)
3311 return error_mark_node;
3313 /* Finally, extract the function pointer from the vtable. */
3314 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3315 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3316 if (e2 == error_mark_node)
3317 return error_mark_node;
3318 TREE_CONSTANT (e2) = 1;
3320 /* When using function descriptors, the address of the
3321 vtable entry is treated as a function pointer. */
3322 if (TARGET_VTABLE_USES_DESCRIPTORS)
3323 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3324 cp_build_addr_expr (e2, complain));
3326 e2 = fold_convert (TREE_TYPE (e3), e2);
3327 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3328 if (e1 == error_mark_node)
3329 return error_mark_node;
3331 /* Make sure this doesn't get evaluated first inside one of the
3332 branches of the COND_EXPR. */
3333 if (instance_save_expr)
3334 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3335 instance_save_expr, e1);
3337 function = e1;
3339 return function;
3342 /* Used by the C-common bits. */
3343 tree
3344 build_function_call (location_t /*loc*/,
3345 tree function, tree params)
3347 return cp_build_function_call (function, params, tf_warning_or_error);
3350 /* Used by the C-common bits. */
3351 tree
3352 build_function_call_vec (location_t /*loc*/,
3353 tree function, vec<tree, va_gc> *params,
3354 vec<tree, va_gc> * /*origtypes*/)
3356 vec<tree, va_gc> *orig_params = params;
3357 tree ret = cp_build_function_call_vec (function, &params,
3358 tf_warning_or_error);
3360 /* cp_build_function_call_vec can reallocate PARAMS by adding
3361 default arguments. That should never happen here. Verify
3362 that. */
3363 gcc_assert (params == orig_params);
3365 return ret;
3368 /* Build a function call using a tree list of arguments. */
3370 tree
3371 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3373 vec<tree, va_gc> *vec;
3374 tree ret;
3376 vec = make_tree_vector ();
3377 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3378 vec_safe_push (vec, TREE_VALUE (params));
3379 ret = cp_build_function_call_vec (function, &vec, complain);
3380 release_tree_vector (vec);
3381 return ret;
3384 /* Build a function call using varargs. */
3386 tree
3387 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3389 vec<tree, va_gc> *vec;
3390 va_list args;
3391 tree ret, t;
3393 vec = make_tree_vector ();
3394 va_start (args, complain);
3395 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3396 vec_safe_push (vec, t);
3397 va_end (args);
3398 ret = cp_build_function_call_vec (function, &vec, complain);
3399 release_tree_vector (vec);
3400 return ret;
3403 /* Build a function call using a vector of arguments. PARAMS may be
3404 NULL if there are no parameters. This changes the contents of
3405 PARAMS. */
3407 tree
3408 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3409 tsubst_flags_t complain)
3411 tree fntype, fndecl;
3412 int is_method;
3413 tree original = function;
3414 int nargs;
3415 tree *argarray;
3416 tree parm_types;
3417 vec<tree, va_gc> *allocated = NULL;
3418 tree ret;
3420 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3421 expressions, like those used for ObjC messenger dispatches. */
3422 if (params != NULL && !vec_safe_is_empty (*params))
3423 function = objc_rewrite_function_call (function, (**params)[0]);
3425 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3426 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3427 if (TREE_CODE (function) == NOP_EXPR
3428 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3429 function = TREE_OPERAND (function, 0);
3431 if (TREE_CODE (function) == FUNCTION_DECL)
3433 mark_used (function);
3434 fndecl = function;
3436 /* Convert anything with function type to a pointer-to-function. */
3437 if (DECL_MAIN_P (function) && (complain & tf_error))
3438 pedwarn (input_location, OPT_Wpedantic,
3439 "ISO C++ forbids calling %<::main%> from within program");
3441 function = build_addr_func (function, complain);
3443 else
3445 fndecl = NULL_TREE;
3447 function = build_addr_func (function, complain);
3450 if (function == error_mark_node)
3451 return error_mark_node;
3453 fntype = TREE_TYPE (function);
3455 if (TYPE_PTRMEMFUNC_P (fntype))
3457 if (complain & tf_error)
3458 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3459 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3460 original, original);
3461 return error_mark_node;
3464 is_method = (TYPE_PTR_P (fntype)
3465 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3467 if (!(TYPE_PTRFN_P (fntype)
3468 || is_method
3469 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3471 if (complain & tf_error)
3473 if (!flag_diagnostics_show_caret)
3474 error_at (input_location,
3475 "%qE cannot be used as a function", original);
3476 else if (DECL_P (original))
3477 error_at (input_location,
3478 "%qD cannot be used as a function", original);
3479 else
3480 error_at (input_location,
3481 "expression cannot be used as a function");
3484 return error_mark_node;
3487 /* fntype now gets the type of function pointed to. */
3488 fntype = TREE_TYPE (fntype);
3489 parm_types = TYPE_ARG_TYPES (fntype);
3491 if (params == NULL)
3493 allocated = make_tree_vector ();
3494 params = &allocated;
3497 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3498 complain);
3499 if (nargs < 0)
3500 return error_mark_node;
3502 argarray = (*params)->address ();
3504 /* Check for errors in format strings and inappropriately
3505 null parameters. */
3506 check_function_arguments (fntype, nargs, argarray);
3508 ret = build_cxx_call (function, nargs, argarray, complain);
3510 if (allocated != NULL)
3511 release_tree_vector (allocated);
3513 return ret;
3516 /* Subroutine of convert_arguments.
3517 Warn about wrong number of args are genereted. */
3519 static void
3520 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3522 if (fndecl)
3524 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3526 if (DECL_NAME (fndecl) == NULL_TREE
3527 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3528 error_at (loc,
3529 too_many_p
3530 ? G_("too many arguments to constructor %q#D")
3531 : G_("too few arguments to constructor %q#D"),
3532 fndecl);
3533 else
3534 error_at (loc,
3535 too_many_p
3536 ? G_("too many arguments to member function %q#D")
3537 : G_("too few arguments to member function %q#D"),
3538 fndecl);
3540 else
3541 error_at (loc,
3542 too_many_p
3543 ? G_("too many arguments to function %q#D")
3544 : G_("too few arguments to function %q#D"),
3545 fndecl);
3546 inform (DECL_SOURCE_LOCATION (fndecl),
3547 "declared here");
3549 else
3551 if (c_dialect_objc () && objc_message_selector ())
3552 error_at (loc,
3553 too_many_p
3554 ? G_("too many arguments to method %q#D")
3555 : G_("too few arguments to method %q#D"),
3556 objc_message_selector ());
3557 else
3558 error_at (loc, too_many_p ? G_("too many arguments to function")
3559 : G_("too few arguments to function"));
3563 /* Convert the actual parameter expressions in the list VALUES to the
3564 types in the list TYPELIST. The converted expressions are stored
3565 back in the VALUES vector.
3566 If parmdecls is exhausted, or when an element has NULL as its type,
3567 perform the default conversions.
3569 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3571 This is also where warnings about wrong number of args are generated.
3573 Returns the actual number of arguments processed (which might be less
3574 than the length of the vector), or -1 on error.
3576 In C++, unspecified trailing parameters can be filled in with their
3577 default arguments, if such were specified. Do so here. */
3579 static int
3580 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3581 int flags, tsubst_flags_t complain)
3583 tree typetail;
3584 unsigned int i;
3586 /* Argument passing is always copy-initialization. */
3587 flags |= LOOKUP_ONLYCONVERTING;
3589 for (i = 0, typetail = typelist;
3590 i < vec_safe_length (*values);
3591 i++)
3593 tree type = typetail ? TREE_VALUE (typetail) : 0;
3594 tree val = (**values)[i];
3596 if (val == error_mark_node || type == error_mark_node)
3597 return -1;
3599 if (type == void_type_node)
3601 if (complain & tf_error)
3603 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3604 return i;
3606 else
3607 return -1;
3610 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3611 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3612 if (TREE_CODE (val) == NOP_EXPR
3613 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3614 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3615 val = TREE_OPERAND (val, 0);
3617 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3619 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3620 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3621 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3622 val = decay_conversion (val, complain);
3625 if (val == error_mark_node)
3626 return -1;
3628 if (type != 0)
3630 /* Formal parm type is specified by a function prototype. */
3631 tree parmval;
3633 if (!COMPLETE_TYPE_P (complete_type (type)))
3635 if (complain & tf_error)
3637 if (fndecl)
3638 error ("parameter %P of %qD has incomplete type %qT",
3639 i, fndecl, type);
3640 else
3641 error ("parameter %P has incomplete type %qT", i, type);
3643 parmval = error_mark_node;
3645 else
3647 parmval = convert_for_initialization
3648 (NULL_TREE, type, val, flags,
3649 ICR_ARGPASS, fndecl, i, complain);
3650 parmval = convert_for_arg_passing (type, parmval, complain);
3653 if (parmval == error_mark_node)
3654 return -1;
3656 (**values)[i] = parmval;
3658 else
3660 if (fndecl && magic_varargs_p (fndecl))
3661 /* Don't do ellipsis conversion for __built_in_constant_p
3662 as this will result in spurious errors for non-trivial
3663 types. */
3664 val = require_complete_type_sfinae (val, complain);
3665 else
3666 val = convert_arg_to_ellipsis (val, complain);
3668 (**values)[i] = val;
3671 if (typetail)
3672 typetail = TREE_CHAIN (typetail);
3675 if (typetail != 0 && typetail != void_list_node)
3677 /* See if there are default arguments that can be used. Because
3678 we hold default arguments in the FUNCTION_TYPE (which is so
3679 wrong), we can see default parameters here from deduced
3680 contexts (and via typeof) for indirect function calls.
3681 Fortunately we know whether we have a function decl to
3682 provide default arguments in a language conformant
3683 manner. */
3684 if (fndecl && TREE_PURPOSE (typetail)
3685 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3687 for (; typetail != void_list_node; ++i)
3689 tree parmval
3690 = convert_default_arg (TREE_VALUE (typetail),
3691 TREE_PURPOSE (typetail),
3692 fndecl, i, complain);
3694 if (parmval == error_mark_node)
3695 return -1;
3697 vec_safe_push (*values, parmval);
3698 typetail = TREE_CHAIN (typetail);
3699 /* ends with `...'. */
3700 if (typetail == NULL_TREE)
3701 break;
3704 else
3706 if (complain & tf_error)
3707 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3708 return -1;
3712 return (int) i;
3715 /* Build a binary-operation expression, after performing default
3716 conversions on the operands. CODE is the kind of expression to
3717 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3718 are the tree codes which correspond to ARG1 and ARG2 when issuing
3719 warnings about possibly misplaced parentheses. They may differ
3720 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3721 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3722 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3723 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3724 ARG2_CODE as ERROR_MARK. */
3726 tree
3727 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3728 enum tree_code arg1_code, tree arg2,
3729 enum tree_code arg2_code, tree *overload,
3730 tsubst_flags_t complain)
3732 tree orig_arg1;
3733 tree orig_arg2;
3734 tree expr;
3736 orig_arg1 = arg1;
3737 orig_arg2 = arg2;
3739 if (processing_template_decl)
3741 if (type_dependent_expression_p (arg1)
3742 || type_dependent_expression_p (arg2))
3743 return build_min_nt_loc (loc, code, arg1, arg2);
3744 arg1 = build_non_dependent_expr (arg1);
3745 arg2 = build_non_dependent_expr (arg2);
3748 if (code == DOTSTAR_EXPR)
3749 expr = build_m_component_ref (arg1, arg2, complain);
3750 else
3751 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3752 overload, complain);
3754 /* Check for cases such as x+y<<z which users are likely to
3755 misinterpret. But don't warn about obj << x + y, since that is a
3756 common idiom for I/O. */
3757 if (warn_parentheses
3758 && (complain & tf_warning)
3759 && !processing_template_decl
3760 && !error_operand_p (arg1)
3761 && !error_operand_p (arg2)
3762 && (code != LSHIFT_EXPR
3763 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3764 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3765 arg2_code, orig_arg2);
3767 if (processing_template_decl && expr != error_mark_node)
3768 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3770 return expr;
3773 /* Build and return an ARRAY_REF expression. */
3775 tree
3776 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3777 tsubst_flags_t complain)
3779 tree orig_arg1 = arg1;
3780 tree orig_arg2 = arg2;
3781 tree expr;
3783 if (processing_template_decl)
3785 if (type_dependent_expression_p (arg1)
3786 || type_dependent_expression_p (arg2))
3787 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3788 NULL_TREE, NULL_TREE);
3789 arg1 = build_non_dependent_expr (arg1);
3790 arg2 = build_non_dependent_expr (arg2);
3793 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3794 NULL_TREE, /*overload=*/NULL, complain);
3796 if (processing_template_decl && expr != error_mark_node)
3797 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3798 NULL_TREE, NULL_TREE);
3799 return expr;
3802 /* Return whether OP is an expression of enum type cast to integer
3803 type. In C++ even unsigned enum types are cast to signed integer
3804 types. We do not want to issue warnings about comparisons between
3805 signed and unsigned types when one of the types is an enum type.
3806 Those warnings are always false positives in practice. */
3808 static bool
3809 enum_cast_to_int (tree op)
3811 if (TREE_CODE (op) == NOP_EXPR
3812 && TREE_TYPE (op) == integer_type_node
3813 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3814 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3815 return true;
3817 /* The cast may have been pushed into a COND_EXPR. */
3818 if (TREE_CODE (op) == COND_EXPR)
3819 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3820 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3822 return false;
3825 /* For the c-common bits. */
3826 tree
3827 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3828 int /*convert_p*/)
3830 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3834 /* Build a binary-operation expression without default conversions.
3835 CODE is the kind of expression to build.
3836 LOCATION is the location_t of the operator in the source code.
3837 This function differs from `build' in several ways:
3838 the data type of the result is computed and recorded in it,
3839 warnings are generated if arg data types are invalid,
3840 special handling for addition and subtraction of pointers is known,
3841 and some optimization is done (operations on narrow ints
3842 are done in the narrower type when that gives the same result).
3843 Constant folding is also done before the result is returned.
3845 Note that the operands will never have enumeral types
3846 because either they have just had the default conversions performed
3847 or they have both just been converted to some other type in which
3848 the arithmetic is to be done.
3850 C++: must do special pointer arithmetic when implementing
3851 multiple inheritance, and deal with pointer to member functions. */
3853 tree
3854 cp_build_binary_op (location_t location,
3855 enum tree_code code, tree orig_op0, tree orig_op1,
3856 tsubst_flags_t complain)
3858 tree op0, op1;
3859 enum tree_code code0, code1;
3860 tree type0, type1;
3861 const char *invalid_op_diag;
3863 /* Expression code to give to the expression when it is built.
3864 Normally this is CODE, which is what the caller asked for,
3865 but in some special cases we change it. */
3866 enum tree_code resultcode = code;
3868 /* Data type in which the computation is to be performed.
3869 In the simplest cases this is the common type of the arguments. */
3870 tree result_type = NULL;
3872 /* Nonzero means operands have already been type-converted
3873 in whatever way is necessary.
3874 Zero means they need to be converted to RESULT_TYPE. */
3875 int converted = 0;
3877 /* Nonzero means create the expression with this type, rather than
3878 RESULT_TYPE. */
3879 tree build_type = 0;
3881 /* Nonzero means after finally constructing the expression
3882 convert it to this type. */
3883 tree final_type = 0;
3885 tree result;
3886 tree orig_type = NULL;
3888 /* Nonzero if this is an operation like MIN or MAX which can
3889 safely be computed in short if both args are promoted shorts.
3890 Also implies COMMON.
3891 -1 indicates a bitwise operation; this makes a difference
3892 in the exact conditions for when it is safe to do the operation
3893 in a narrower mode. */
3894 int shorten = 0;
3896 /* Nonzero if this is a comparison operation;
3897 if both args are promoted shorts, compare the original shorts.
3898 Also implies COMMON. */
3899 int short_compare = 0;
3901 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3902 int common = 0;
3904 /* True if both operands have arithmetic type. */
3905 bool arithmetic_types_p;
3907 /* Apply default conversions. */
3908 op0 = orig_op0;
3909 op1 = orig_op1;
3911 /* Remember whether we're doing / or %. */
3912 bool doing_div_or_mod = false;
3914 /* Remember whether we're doing << or >>. */
3915 bool doing_shift = false;
3917 /* Tree holding instrumentation expression. */
3918 tree instrument_expr = NULL;
3920 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3921 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3922 || code == TRUTH_XOR_EXPR)
3924 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3925 op0 = decay_conversion (op0, complain);
3926 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3927 op1 = decay_conversion (op1, complain);
3929 else
3931 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3932 op0 = cp_default_conversion (op0, complain);
3933 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3934 op1 = cp_default_conversion (op1, complain);
3937 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3938 STRIP_TYPE_NOPS (op0);
3939 STRIP_TYPE_NOPS (op1);
3941 /* DTRT if one side is an overloaded function, but complain about it. */
3942 if (type_unknown_p (op0))
3944 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3945 if (t != error_mark_node)
3947 if (complain & tf_error)
3948 permerror (input_location, "assuming cast to type %qT from overloaded function",
3949 TREE_TYPE (t));
3950 op0 = t;
3953 if (type_unknown_p (op1))
3955 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3956 if (t != error_mark_node)
3958 if (complain & tf_error)
3959 permerror (input_location, "assuming cast to type %qT from overloaded function",
3960 TREE_TYPE (t));
3961 op1 = t;
3965 type0 = TREE_TYPE (op0);
3966 type1 = TREE_TYPE (op1);
3968 /* The expression codes of the data types of the arguments tell us
3969 whether the arguments are integers, floating, pointers, etc. */
3970 code0 = TREE_CODE (type0);
3971 code1 = TREE_CODE (type1);
3973 /* If an error was already reported for one of the arguments,
3974 avoid reporting another error. */
3975 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3976 return error_mark_node;
3978 if ((invalid_op_diag
3979 = targetm.invalid_binary_op (code, type0, type1)))
3981 if (complain & tf_error)
3982 error (invalid_op_diag);
3983 return error_mark_node;
3986 /* Issue warnings about peculiar, but valid, uses of NULL. */
3987 if ((orig_op0 == null_node || orig_op1 == null_node)
3988 /* It's reasonable to use pointer values as operands of &&
3989 and ||, so NULL is no exception. */
3990 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3991 && ( /* Both are NULL (or 0) and the operation was not a
3992 comparison or a pointer subtraction. */
3993 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3994 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3995 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3996 || (!null_ptr_cst_p (orig_op0)
3997 && !TYPE_PTR_OR_PTRMEM_P (type0))
3998 || (!null_ptr_cst_p (orig_op1)
3999 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4000 && (complain & tf_warning))
4002 source_location loc =
4003 expansion_point_location_if_in_system_header (input_location);
4005 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4008 /* In case when one of the operands of the binary operation is
4009 a vector and another is a scalar -- convert scalar to vector. */
4010 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4012 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4013 complain & tf_error);
4015 switch (convert_flag)
4017 case stv_error:
4018 return error_mark_node;
4019 case stv_firstarg:
4021 op0 = save_expr (op0);
4022 op0 = convert (TREE_TYPE (type1), op0);
4023 op0 = build_vector_from_val (type1, op0);
4024 type0 = TREE_TYPE (op0);
4025 code0 = TREE_CODE (type0);
4026 converted = 1;
4027 break;
4029 case stv_secondarg:
4031 op1 = save_expr (op1);
4032 op1 = convert (TREE_TYPE (type0), op1);
4033 op1 = build_vector_from_val (type0, op1);
4034 type1 = TREE_TYPE (op1);
4035 code1 = TREE_CODE (type1);
4036 converted = 1;
4037 break;
4039 default:
4040 break;
4044 switch (code)
4046 case MINUS_EXPR:
4047 /* Subtraction of two similar pointers.
4048 We must subtract them as integers, then divide by object size. */
4049 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4050 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4051 TREE_TYPE (type1)))
4052 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4053 complain);
4054 /* In all other cases except pointer - int, the usual arithmetic
4055 rules apply. */
4056 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4058 common = 1;
4059 break;
4061 /* The pointer - int case is just like pointer + int; fall
4062 through. */
4063 case PLUS_EXPR:
4064 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4065 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4067 tree ptr_operand;
4068 tree int_operand;
4069 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4070 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4071 if (processing_template_decl)
4073 result_type = TREE_TYPE (ptr_operand);
4074 break;
4076 return cp_pointer_int_sum (code,
4077 ptr_operand,
4078 int_operand,
4079 complain);
4081 common = 1;
4082 break;
4084 case MULT_EXPR:
4085 common = 1;
4086 break;
4088 case TRUNC_DIV_EXPR:
4089 case CEIL_DIV_EXPR:
4090 case FLOOR_DIV_EXPR:
4091 case ROUND_DIV_EXPR:
4092 case EXACT_DIV_EXPR:
4093 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4094 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4095 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4096 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4098 enum tree_code tcode0 = code0, tcode1 = code1;
4099 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4100 cop1 = maybe_constant_value (cop1);
4102 if (tcode0 == INTEGER_TYPE)
4103 doing_div_or_mod = true;
4105 warn_for_div_by_zero (location, cop1);
4107 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4108 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4109 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4110 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4112 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4113 resultcode = RDIV_EXPR;
4114 else
4115 /* When dividing two signed integers, we have to promote to int.
4116 unless we divide by a constant != -1. Note that default
4117 conversion will have been performed on the operands at this
4118 point, so we have to dig out the original type to find out if
4119 it was unsigned. */
4120 shorten = ((TREE_CODE (op0) == NOP_EXPR
4121 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4122 || (TREE_CODE (op1) == INTEGER_CST
4123 && ! integer_all_onesp (op1)));
4125 common = 1;
4127 break;
4129 case BIT_AND_EXPR:
4130 case BIT_IOR_EXPR:
4131 case BIT_XOR_EXPR:
4132 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4133 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4134 && !VECTOR_FLOAT_TYPE_P (type0)
4135 && !VECTOR_FLOAT_TYPE_P (type1)))
4136 shorten = -1;
4137 break;
4139 case TRUNC_MOD_EXPR:
4140 case FLOOR_MOD_EXPR:
4142 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4143 cop1 = maybe_constant_value (cop1);
4145 if (code0 == INTEGER_TYPE)
4146 doing_div_or_mod = true;
4147 warn_for_div_by_zero (location, cop1);
4150 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4151 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4152 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4153 common = 1;
4154 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4156 /* Although it would be tempting to shorten always here, that loses
4157 on some targets, since the modulo instruction is undefined if the
4158 quotient can't be represented in the computation mode. We shorten
4159 only if unsigned or if dividing by something we know != -1. */
4160 shorten = ((TREE_CODE (op0) == NOP_EXPR
4161 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4162 || (TREE_CODE (op1) == INTEGER_CST
4163 && ! integer_all_onesp (op1)));
4164 common = 1;
4166 break;
4168 case TRUTH_ANDIF_EXPR:
4169 case TRUTH_ORIF_EXPR:
4170 case TRUTH_AND_EXPR:
4171 case TRUTH_OR_EXPR:
4172 result_type = boolean_type_node;
4173 break;
4175 /* Shift operations: result has same type as first operand;
4176 always convert second operand to int.
4177 Also set SHORT_SHIFT if shifting rightward. */
4179 case RSHIFT_EXPR:
4180 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4181 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4183 result_type = type0;
4184 converted = 1;
4186 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4187 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4188 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4189 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4191 result_type = type0;
4192 converted = 1;
4194 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4196 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4197 const_op1 = maybe_constant_value (const_op1);
4198 if (TREE_CODE (const_op1) != INTEGER_CST)
4199 const_op1 = op1;
4200 result_type = type0;
4201 doing_shift = true;
4202 if (TREE_CODE (const_op1) == INTEGER_CST)
4204 if (tree_int_cst_lt (const_op1, integer_zero_node))
4206 if ((complain & tf_warning)
4207 && c_inhibit_evaluation_warnings == 0)
4208 warning (0, "right shift count is negative");
4210 else
4212 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4213 && (complain & tf_warning)
4214 && c_inhibit_evaluation_warnings == 0)
4215 warning (0, "right shift count >= width of type");
4218 /* Convert the shift-count to an integer, regardless of
4219 size of value being shifted. */
4220 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4221 op1 = cp_convert (integer_type_node, op1, complain);
4222 /* Avoid converting op1 to result_type later. */
4223 converted = 1;
4225 break;
4227 case LSHIFT_EXPR:
4228 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4229 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4231 result_type = type0;
4232 converted = 1;
4234 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4235 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4236 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4237 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4239 result_type = type0;
4240 converted = 1;
4242 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4244 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4245 const_op1 = maybe_constant_value (const_op1);
4246 if (TREE_CODE (const_op1) != INTEGER_CST)
4247 const_op1 = op1;
4248 result_type = type0;
4249 doing_shift = true;
4250 if (TREE_CODE (const_op1) == INTEGER_CST)
4252 if (tree_int_cst_lt (const_op1, integer_zero_node))
4254 if ((complain & tf_warning)
4255 && c_inhibit_evaluation_warnings == 0)
4256 warning (0, "left shift count is negative");
4258 else if (compare_tree_int (const_op1,
4259 TYPE_PRECISION (type0)) >= 0)
4261 if ((complain & tf_warning)
4262 && c_inhibit_evaluation_warnings == 0)
4263 warning (0, "left shift count >= width of type");
4266 /* Convert the shift-count to an integer, regardless of
4267 size of value being shifted. */
4268 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4269 op1 = cp_convert (integer_type_node, op1, complain);
4270 /* Avoid converting op1 to result_type later. */
4271 converted = 1;
4273 break;
4275 case RROTATE_EXPR:
4276 case LROTATE_EXPR:
4277 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4279 result_type = type0;
4280 if (TREE_CODE (op1) == INTEGER_CST)
4282 if (tree_int_cst_lt (op1, integer_zero_node))
4284 if (complain & tf_warning)
4285 warning (0, (code == LROTATE_EXPR)
4286 ? G_("left rotate count is negative")
4287 : G_("right rotate count is negative"));
4289 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4291 if (complain & tf_warning)
4292 warning (0, (code == LROTATE_EXPR)
4293 ? G_("left rotate count >= width of type")
4294 : G_("right rotate count >= width of type"));
4297 /* Convert the shift-count to an integer, regardless of
4298 size of value being shifted. */
4299 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4300 op1 = cp_convert (integer_type_node, op1, complain);
4302 break;
4304 case EQ_EXPR:
4305 case NE_EXPR:
4306 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4307 goto vector_compare;
4308 if ((complain & tf_warning)
4309 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4310 warning (OPT_Wfloat_equal,
4311 "comparing floating point with == or != is unsafe");
4312 if ((complain & tf_warning)
4313 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4314 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4315 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4317 build_type = boolean_type_node;
4318 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4319 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4320 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4321 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4322 short_compare = 1;
4323 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4324 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4325 result_type = composite_pointer_type (type0, type1, op0, op1,
4326 CPO_COMPARISON, complain);
4327 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4328 && null_ptr_cst_p (op1))
4330 if (TREE_CODE (op0) == ADDR_EXPR
4331 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4333 if ((complain & tf_warning)
4334 && c_inhibit_evaluation_warnings == 0)
4335 warning (OPT_Waddress, "the address of %qD will never be NULL",
4336 TREE_OPERAND (op0, 0));
4338 result_type = type0;
4340 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4341 && null_ptr_cst_p (op0))
4343 if (TREE_CODE (op1) == ADDR_EXPR
4344 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4346 if ((complain & tf_warning)
4347 && c_inhibit_evaluation_warnings == 0)
4348 warning (OPT_Waddress, "the address of %qD will never be NULL",
4349 TREE_OPERAND (op1, 0));
4351 result_type = type1;
4353 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4354 /* One of the operands must be of nullptr_t type. */
4355 result_type = TREE_TYPE (nullptr_node);
4356 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4358 result_type = type0;
4359 if (complain & tf_error)
4360 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4361 else
4362 return error_mark_node;
4364 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4366 result_type = type1;
4367 if (complain & tf_error)
4368 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4369 else
4370 return error_mark_node;
4372 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4374 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4375 == ptrmemfunc_vbit_in_delta)
4377 tree pfn0, delta0, e1, e2;
4379 if (TREE_SIDE_EFFECTS (op0))
4380 op0 = save_expr (op0);
4382 pfn0 = pfn_from_ptrmemfunc (op0);
4383 delta0 = delta_from_ptrmemfunc (op0);
4384 e1 = cp_build_binary_op (location,
4385 EQ_EXPR,
4386 pfn0,
4387 build_zero_cst (TREE_TYPE (pfn0)),
4388 complain);
4389 e2 = cp_build_binary_op (location,
4390 BIT_AND_EXPR,
4391 delta0,
4392 integer_one_node,
4393 complain);
4395 if (complain & tf_warning)
4396 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4398 e2 = cp_build_binary_op (location,
4399 EQ_EXPR, e2, integer_zero_node,
4400 complain);
4401 op0 = cp_build_binary_op (location,
4402 TRUTH_ANDIF_EXPR, e1, e2,
4403 complain);
4404 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4406 else
4408 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4409 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4411 result_type = TREE_TYPE (op0);
4413 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4414 return cp_build_binary_op (location, code, op1, op0, complain);
4415 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4417 tree type;
4418 /* E will be the final comparison. */
4419 tree e;
4420 /* E1 and E2 are for scratch. */
4421 tree e1;
4422 tree e2;
4423 tree pfn0;
4424 tree pfn1;
4425 tree delta0;
4426 tree delta1;
4428 type = composite_pointer_type (type0, type1, op0, op1,
4429 CPO_COMPARISON, complain);
4431 if (!same_type_p (TREE_TYPE (op0), type))
4432 op0 = cp_convert_and_check (type, op0, complain);
4433 if (!same_type_p (TREE_TYPE (op1), type))
4434 op1 = cp_convert_and_check (type, op1, complain);
4436 if (op0 == error_mark_node || op1 == error_mark_node)
4437 return error_mark_node;
4439 if (TREE_SIDE_EFFECTS (op0))
4440 op0 = save_expr (op0);
4441 if (TREE_SIDE_EFFECTS (op1))
4442 op1 = save_expr (op1);
4444 pfn0 = pfn_from_ptrmemfunc (op0);
4445 pfn1 = pfn_from_ptrmemfunc (op1);
4446 delta0 = delta_from_ptrmemfunc (op0);
4447 delta1 = delta_from_ptrmemfunc (op1);
4448 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4449 == ptrmemfunc_vbit_in_delta)
4451 /* We generate:
4453 (op0.pfn == op1.pfn
4454 && ((op0.delta == op1.delta)
4455 || (!op0.pfn && op0.delta & 1 == 0
4456 && op1.delta & 1 == 0))
4458 The reason for the `!op0.pfn' bit is that a NULL
4459 pointer-to-member is any member with a zero PFN and
4460 LSB of the DELTA field is 0. */
4462 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4463 delta0,
4464 integer_one_node,
4465 complain);
4466 e1 = cp_build_binary_op (location,
4467 EQ_EXPR, e1, integer_zero_node,
4468 complain);
4469 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4470 delta1,
4471 integer_one_node,
4472 complain);
4473 e2 = cp_build_binary_op (location,
4474 EQ_EXPR, e2, integer_zero_node,
4475 complain);
4476 e1 = cp_build_binary_op (location,
4477 TRUTH_ANDIF_EXPR, e2, e1,
4478 complain);
4479 e2 = cp_build_binary_op (location, EQ_EXPR,
4480 pfn0,
4481 build_zero_cst (TREE_TYPE (pfn0)),
4482 complain);
4483 e2 = cp_build_binary_op (location,
4484 TRUTH_ANDIF_EXPR, e2, e1, complain);
4485 e1 = cp_build_binary_op (location,
4486 EQ_EXPR, delta0, delta1, complain);
4487 e1 = cp_build_binary_op (location,
4488 TRUTH_ORIF_EXPR, e1, e2, complain);
4490 else
4492 /* We generate:
4494 (op0.pfn == op1.pfn
4495 && (!op0.pfn || op0.delta == op1.delta))
4497 The reason for the `!op0.pfn' bit is that a NULL
4498 pointer-to-member is any member with a zero PFN; the
4499 DELTA field is unspecified. */
4501 e1 = cp_build_binary_op (location,
4502 EQ_EXPR, delta0, delta1, complain);
4503 e2 = cp_build_binary_op (location,
4504 EQ_EXPR,
4505 pfn0,
4506 build_zero_cst (TREE_TYPE (pfn0)),
4507 complain);
4508 e1 = cp_build_binary_op (location,
4509 TRUTH_ORIF_EXPR, e1, e2, complain);
4511 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4512 e = cp_build_binary_op (location,
4513 TRUTH_ANDIF_EXPR, e2, e1, complain);
4514 if (code == EQ_EXPR)
4515 return e;
4516 return cp_build_binary_op (location,
4517 EQ_EXPR, e, integer_zero_node, complain);
4519 else
4521 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4522 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4523 type1));
4524 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4525 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4526 type0));
4529 break;
4531 case MAX_EXPR:
4532 case MIN_EXPR:
4533 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4534 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4535 shorten = 1;
4536 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4537 result_type = composite_pointer_type (type0, type1, op0, op1,
4538 CPO_COMPARISON, complain);
4539 break;
4541 case LE_EXPR:
4542 case GE_EXPR:
4543 case LT_EXPR:
4544 case GT_EXPR:
4545 if (TREE_CODE (orig_op0) == STRING_CST
4546 || TREE_CODE (orig_op1) == STRING_CST)
4548 if (complain & tf_warning)
4549 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4552 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4554 vector_compare:
4555 tree intt;
4556 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4557 TREE_TYPE (type1))
4558 && !vector_types_compatible_elements_p (type0, type1))
4560 if (complain & tf_error)
4562 error_at (location, "comparing vectors with different "
4563 "element types");
4564 inform (location, "operand types are %qT and %qT",
4565 type0, type1);
4567 return error_mark_node;
4570 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4572 if (complain & tf_error)
4574 error_at (location, "comparing vectors with different "
4575 "number of elements");
4576 inform (location, "operand types are %qT and %qT",
4577 type0, type1);
4579 return error_mark_node;
4582 /* Always construct signed integer vector type. */
4583 intt = c_common_type_for_size (GET_MODE_BITSIZE
4584 (TYPE_MODE (TREE_TYPE (type0))), 0);
4585 if (!intt)
4587 if (complain & tf_error)
4588 error_at (location, "could not find an integer type "
4589 "of the same size as %qT", TREE_TYPE (type0));
4590 return error_mark_node;
4592 result_type = build_opaque_vector_type (intt,
4593 TYPE_VECTOR_SUBPARTS (type0));
4594 converted = 1;
4595 break;
4597 build_type = boolean_type_node;
4598 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4599 || code0 == ENUMERAL_TYPE)
4600 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4601 || code1 == ENUMERAL_TYPE))
4602 short_compare = 1;
4603 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4604 result_type = composite_pointer_type (type0, type1, op0, op1,
4605 CPO_COMPARISON, complain);
4606 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4608 result_type = type0;
4609 if (extra_warnings && (complain & tf_warning))
4610 warning (OPT_Wextra,
4611 "ordered comparison of pointer with integer zero");
4613 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4615 result_type = type1;
4616 if (extra_warnings && (complain & tf_warning))
4617 warning (OPT_Wextra,
4618 "ordered comparison of pointer with integer zero");
4620 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4621 /* One of the operands must be of nullptr_t type. */
4622 result_type = TREE_TYPE (nullptr_node);
4623 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4625 result_type = type0;
4626 if (complain & tf_error)
4627 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4628 else
4629 return error_mark_node;
4631 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4633 result_type = type1;
4634 if (complain & tf_error)
4635 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4636 else
4637 return error_mark_node;
4639 break;
4641 case UNORDERED_EXPR:
4642 case ORDERED_EXPR:
4643 case UNLT_EXPR:
4644 case UNLE_EXPR:
4645 case UNGT_EXPR:
4646 case UNGE_EXPR:
4647 case UNEQ_EXPR:
4648 build_type = integer_type_node;
4649 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4651 if (complain & tf_error)
4652 error ("unordered comparison on non-floating point argument");
4653 return error_mark_node;
4655 common = 1;
4656 break;
4658 default:
4659 break;
4662 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4663 || code0 == ENUMERAL_TYPE)
4664 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4665 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4666 arithmetic_types_p = 1;
4667 else
4669 arithmetic_types_p = 0;
4670 /* Vector arithmetic is only allowed when both sides are vectors. */
4671 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4673 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4674 || !vector_types_compatible_elements_p (type0, type1))
4676 if (complain & tf_error)
4677 binary_op_error (location, code, type0, type1);
4678 return error_mark_node;
4680 arithmetic_types_p = 1;
4683 /* Determine the RESULT_TYPE, if it is not already known. */
4684 if (!result_type
4685 && arithmetic_types_p
4686 && (shorten || common || short_compare))
4688 result_type = cp_common_type (type0, type1);
4689 if (complain & tf_warning)
4690 do_warn_double_promotion (result_type, type0, type1,
4691 "implicit conversion from %qT to %qT "
4692 "to match other operand of binary "
4693 "expression",
4694 location);
4697 if (!result_type)
4699 if (complain & tf_error)
4700 error ("invalid operands of types %qT and %qT to binary %qO",
4701 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4702 return error_mark_node;
4705 /* If we're in a template, the only thing we need to know is the
4706 RESULT_TYPE. */
4707 if (processing_template_decl)
4709 /* Since the middle-end checks the type when doing a build2, we
4710 need to build the tree in pieces. This built tree will never
4711 get out of the front-end as we replace it when instantiating
4712 the template. */
4713 tree tmp = build2 (resultcode,
4714 build_type ? build_type : result_type,
4715 NULL_TREE, op1);
4716 TREE_OPERAND (tmp, 0) = op0;
4717 return tmp;
4720 if (arithmetic_types_p)
4722 bool first_complex = (code0 == COMPLEX_TYPE);
4723 bool second_complex = (code1 == COMPLEX_TYPE);
4724 int none_complex = (!first_complex && !second_complex);
4726 /* Adapted from patch for c/24581. */
4727 if (first_complex != second_complex
4728 && (code == PLUS_EXPR
4729 || code == MINUS_EXPR
4730 || code == MULT_EXPR
4731 || (code == TRUNC_DIV_EXPR && first_complex))
4732 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4733 && flag_signed_zeros)
4735 /* An operation on mixed real/complex operands must be
4736 handled specially, but the language-independent code can
4737 more easily optimize the plain complex arithmetic if
4738 -fno-signed-zeros. */
4739 tree real_type = TREE_TYPE (result_type);
4740 tree real, imag;
4741 if (first_complex)
4743 if (TREE_TYPE (op0) != result_type)
4744 op0 = cp_convert_and_check (result_type, op0, complain);
4745 if (TREE_TYPE (op1) != real_type)
4746 op1 = cp_convert_and_check (real_type, op1, complain);
4748 else
4750 if (TREE_TYPE (op0) != real_type)
4751 op0 = cp_convert_and_check (real_type, op0, complain);
4752 if (TREE_TYPE (op1) != result_type)
4753 op1 = cp_convert_and_check (result_type, op1, complain);
4755 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4756 return error_mark_node;
4757 if (first_complex)
4759 op0 = save_expr (op0);
4760 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4761 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4762 switch (code)
4764 case MULT_EXPR:
4765 case TRUNC_DIV_EXPR:
4766 op1 = save_expr (op1);
4767 imag = build2 (resultcode, real_type, imag, op1);
4768 /* Fall through. */
4769 case PLUS_EXPR:
4770 case MINUS_EXPR:
4771 real = build2 (resultcode, real_type, real, op1);
4772 break;
4773 default:
4774 gcc_unreachable();
4777 else
4779 op1 = save_expr (op1);
4780 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4781 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4782 switch (code)
4784 case MULT_EXPR:
4785 op0 = save_expr (op0);
4786 imag = build2 (resultcode, real_type, op0, imag);
4787 /* Fall through. */
4788 case PLUS_EXPR:
4789 real = build2 (resultcode, real_type, op0, real);
4790 break;
4791 case MINUS_EXPR:
4792 real = build2 (resultcode, real_type, op0, real);
4793 imag = build1 (NEGATE_EXPR, real_type, imag);
4794 break;
4795 default:
4796 gcc_unreachable();
4799 real = fold_if_not_in_template (real);
4800 imag = fold_if_not_in_template (imag);
4801 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4802 result = fold_if_not_in_template (result);
4803 return result;
4806 /* For certain operations (which identify themselves by shorten != 0)
4807 if both args were extended from the same smaller type,
4808 do the arithmetic in that type and then extend.
4810 shorten !=0 and !=1 indicates a bitwise operation.
4811 For them, this optimization is safe only if
4812 both args are zero-extended or both are sign-extended.
4813 Otherwise, we might change the result.
4814 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4815 but calculated in (unsigned short) it would be (unsigned short)-1. */
4817 if (shorten && none_complex)
4819 orig_type = result_type;
4820 final_type = result_type;
4821 result_type = shorten_binary_op (result_type, op0, op1,
4822 shorten == -1);
4825 /* Comparison operations are shortened too but differently.
4826 They identify themselves by setting short_compare = 1. */
4828 if (short_compare)
4830 /* Don't write &op0, etc., because that would prevent op0
4831 from being kept in a register.
4832 Instead, make copies of the our local variables and
4833 pass the copies by reference, then copy them back afterward. */
4834 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4835 enum tree_code xresultcode = resultcode;
4836 tree val
4837 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4838 if (val != 0)
4839 return cp_convert (boolean_type_node, val, complain);
4840 op0 = xop0, op1 = xop1;
4841 converted = 1;
4842 resultcode = xresultcode;
4845 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4846 && warn_sign_compare
4847 /* Do not warn until the template is instantiated; we cannot
4848 bound the ranges of the arguments until that point. */
4849 && !processing_template_decl
4850 && (complain & tf_warning)
4851 && c_inhibit_evaluation_warnings == 0
4852 /* Even unsigned enum types promote to signed int. We don't
4853 want to issue -Wsign-compare warnings for this case. */
4854 && !enum_cast_to_int (orig_op0)
4855 && !enum_cast_to_int (orig_op1))
4857 tree oop0 = maybe_constant_value (orig_op0);
4858 tree oop1 = maybe_constant_value (orig_op1);
4860 if (TREE_CODE (oop0) != INTEGER_CST)
4861 oop0 = orig_op0;
4862 if (TREE_CODE (oop1) != INTEGER_CST)
4863 oop1 = orig_op1;
4864 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4865 result_type, resultcode);
4869 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4870 Then the expression will be built.
4871 It will be given type FINAL_TYPE if that is nonzero;
4872 otherwise, it will be given type RESULT_TYPE. */
4873 if (! converted)
4875 if (TREE_TYPE (op0) != result_type)
4876 op0 = cp_convert_and_check (result_type, op0, complain);
4877 if (TREE_TYPE (op1) != result_type)
4878 op1 = cp_convert_and_check (result_type, op1, complain);
4880 if (op0 == error_mark_node || op1 == error_mark_node)
4881 return error_mark_node;
4884 if (build_type == NULL_TREE)
4885 build_type = result_type;
4887 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
4888 && !processing_template_decl
4889 && current_function_decl != 0
4890 && !lookup_attribute ("no_sanitize_undefined",
4891 DECL_ATTRIBUTES (current_function_decl))
4892 && (doing_div_or_mod || doing_shift))
4894 /* OP0 and/or OP1 might have side-effects. */
4895 op0 = cp_save_expr (op0);
4896 op1 = cp_save_expr (op1);
4897 op0 = maybe_constant_value (fold_non_dependent_expr_sfinae (op0,
4898 tf_none));
4899 op1 = maybe_constant_value (fold_non_dependent_expr_sfinae (op1,
4900 tf_none));
4901 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
4903 /* For diagnostics we want to use the promoted types without
4904 shorten_binary_op. So convert the arguments to the
4905 original result_type. */
4906 tree cop0 = op0;
4907 tree cop1 = op1;
4908 if (orig_type != NULL && result_type != orig_type)
4910 cop0 = cp_convert (orig_type, op0, complain);
4911 cop1 = cp_convert (orig_type, op1, complain);
4913 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
4915 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
4916 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
4919 result = build2 (resultcode, build_type, op0, op1);
4920 result = fold_if_not_in_template (result);
4921 if (final_type != 0)
4922 result = cp_convert (final_type, result, complain);
4924 if (TREE_OVERFLOW_P (result)
4925 && !TREE_OVERFLOW_P (op0)
4926 && !TREE_OVERFLOW_P (op1))
4927 overflow_warning (location, result);
4929 if (instrument_expr != NULL)
4930 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
4931 instrument_expr, result);
4933 return result;
4936 /* Build a VEC_PERM_EXPR.
4937 This is a simple wrapper for c_build_vec_perm_expr. */
4938 tree
4939 build_x_vec_perm_expr (location_t loc,
4940 tree arg0, tree arg1, tree arg2,
4941 tsubst_flags_t complain)
4943 if (processing_template_decl
4944 && (type_dependent_expression_p (arg0)
4945 || type_dependent_expression_p (arg1)
4946 || type_dependent_expression_p (arg2)))
4947 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
4948 return c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
4951 /* Return a tree for the sum or difference (RESULTCODE says which)
4952 of pointer PTROP and integer INTOP. */
4954 static tree
4955 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
4956 tsubst_flags_t complain)
4958 tree res_type = TREE_TYPE (ptrop);
4960 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4961 in certain circumstance (when it's valid to do so). So we need
4962 to make sure it's complete. We don't need to check here, if we
4963 can actually complete it at all, as those checks will be done in
4964 pointer_int_sum() anyway. */
4965 complete_type (TREE_TYPE (res_type));
4967 return pointer_int_sum (input_location, resultcode, ptrop,
4968 fold_if_not_in_template (intop),
4969 complain & tf_warning_or_error);
4972 /* Return a tree for the difference of pointers OP0 and OP1.
4973 The resulting tree has type int. */
4975 static tree
4976 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
4978 tree result;
4979 tree restype = ptrdiff_type_node;
4980 tree target_type = TREE_TYPE (ptrtype);
4982 if (!complete_type_or_else (target_type, NULL_TREE))
4983 return error_mark_node;
4985 if (VOID_TYPE_P (target_type))
4987 if (complain & tf_error)
4988 permerror (input_location, "ISO C++ forbids using pointer of "
4989 "type %<void *%> in subtraction");
4990 else
4991 return error_mark_node;
4993 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4995 if (complain & tf_error)
4996 permerror (input_location, "ISO C++ forbids using pointer to "
4997 "a function in subtraction");
4998 else
4999 return error_mark_node;
5001 if (TREE_CODE (target_type) == METHOD_TYPE)
5003 if (complain & tf_error)
5004 permerror (input_location, "ISO C++ forbids using pointer to "
5005 "a method in subtraction");
5006 else
5007 return error_mark_node;
5010 /* First do the subtraction as integers;
5011 then drop through to build the divide operator. */
5013 op0 = cp_build_binary_op (input_location,
5014 MINUS_EXPR,
5015 cp_convert (restype, op0, complain),
5016 cp_convert (restype, op1, complain),
5017 complain);
5019 /* This generates an error if op1 is a pointer to an incomplete type. */
5020 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5022 if (complain & tf_error)
5023 error ("invalid use of a pointer to an incomplete type in "
5024 "pointer arithmetic");
5025 else
5026 return error_mark_node;
5029 op1 = (TYPE_PTROB_P (ptrtype)
5030 ? size_in_bytes (target_type)
5031 : integer_one_node);
5033 /* Do the division. */
5035 result = build2 (EXACT_DIV_EXPR, restype, op0,
5036 cp_convert (restype, op1, complain));
5037 return fold_if_not_in_template (result);
5040 /* Construct and perhaps optimize a tree representation
5041 for a unary operation. CODE, a tree_code, specifies the operation
5042 and XARG is the operand. */
5044 tree
5045 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5046 tsubst_flags_t complain)
5048 tree orig_expr = xarg;
5049 tree exp;
5050 int ptrmem = 0;
5052 if (processing_template_decl)
5054 if (type_dependent_expression_p (xarg))
5055 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5057 xarg = build_non_dependent_expr (xarg);
5060 exp = NULL_TREE;
5062 /* [expr.unary.op] says:
5064 The address of an object of incomplete type can be taken.
5066 (And is just the ordinary address operator, not an overloaded
5067 "operator &".) However, if the type is a template
5068 specialization, we must complete the type at this point so that
5069 an overloaded "operator &" will be available if required. */
5070 if (code == ADDR_EXPR
5071 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5072 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5073 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5074 || (TREE_CODE (xarg) == OFFSET_REF)))
5075 /* Don't look for a function. */;
5076 else
5077 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5078 NULL_TREE, /*overload=*/NULL, complain);
5079 if (!exp && code == ADDR_EXPR)
5081 if (is_overloaded_fn (xarg))
5083 tree fn = get_first_fn (xarg);
5084 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5086 if (complain & tf_error)
5087 error (DECL_CONSTRUCTOR_P (fn)
5088 ? G_("taking address of constructor %qE")
5089 : G_("taking address of destructor %qE"),
5090 xarg);
5091 return error_mark_node;
5095 /* A pointer to member-function can be formed only by saying
5096 &X::mf. */
5097 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5098 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5100 if (TREE_CODE (xarg) != OFFSET_REF
5101 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5103 if (complain & tf_error)
5105 error ("invalid use of %qE to form a "
5106 "pointer-to-member-function", xarg);
5107 if (TREE_CODE (xarg) != OFFSET_REF)
5108 inform (input_location, " a qualified-id is required");
5110 return error_mark_node;
5112 else
5114 if (complain & tf_error)
5115 error ("parentheses around %qE cannot be used to form a"
5116 " pointer-to-member-function",
5117 xarg);
5118 else
5119 return error_mark_node;
5120 PTRMEM_OK_P (xarg) = 1;
5124 if (TREE_CODE (xarg) == OFFSET_REF)
5126 ptrmem = PTRMEM_OK_P (xarg);
5128 if (!ptrmem && !flag_ms_extensions
5129 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5131 /* A single non-static member, make sure we don't allow a
5132 pointer-to-member. */
5133 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5134 TREE_OPERAND (xarg, 0),
5135 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5136 PTRMEM_OK_P (xarg) = ptrmem;
5140 exp = cp_build_addr_expr_strict (xarg, complain);
5143 if (processing_template_decl && exp != error_mark_node)
5144 exp = build_min_non_dep (code, exp, orig_expr,
5145 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5146 if (TREE_CODE (exp) == ADDR_EXPR)
5147 PTRMEM_OK_P (exp) = ptrmem;
5148 return exp;
5151 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5152 constants, where a null value is represented by an INTEGER_CST of
5153 -1. */
5155 tree
5156 cp_truthvalue_conversion (tree expr)
5158 tree type = TREE_TYPE (expr);
5159 if (TYPE_PTRDATAMEM_P (type))
5160 return build_binary_op (EXPR_LOCATION (expr),
5161 NE_EXPR, expr, nullptr_node, 1);
5162 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5164 /* With -Wzero-as-null-pointer-constant do not warn for an
5165 'if (p)' or a 'while (!p)', where p is a pointer. */
5166 tree ret;
5167 ++c_inhibit_evaluation_warnings;
5168 ret = c_common_truthvalue_conversion (input_location, expr);
5169 --c_inhibit_evaluation_warnings;
5170 return ret;
5172 else
5173 return c_common_truthvalue_conversion (input_location, expr);
5176 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5178 tree
5179 condition_conversion (tree expr)
5181 tree t;
5182 if (processing_template_decl)
5183 return expr;
5184 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5185 tf_warning_or_error, LOOKUP_NORMAL);
5186 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5187 return t;
5190 /* Returns the address of T. This function will fold away
5191 ADDR_EXPR of INDIRECT_REF. */
5193 tree
5194 build_address (tree t)
5196 if (error_operand_p (t) || !cxx_mark_addressable (t))
5197 return error_mark_node;
5198 t = build_fold_addr_expr (t);
5199 if (TREE_CODE (t) != ADDR_EXPR)
5200 t = rvalue (t);
5201 return t;
5204 /* Returns the address of T with type TYPE. */
5206 tree
5207 build_typed_address (tree t, tree type)
5209 if (error_operand_p (t) || !cxx_mark_addressable (t))
5210 return error_mark_node;
5211 t = build_fold_addr_expr_with_type (t, type);
5212 if (TREE_CODE (t) != ADDR_EXPR)
5213 t = rvalue (t);
5214 return t;
5217 /* Return a NOP_EXPR converting EXPR to TYPE. */
5219 tree
5220 build_nop (tree type, tree expr)
5222 if (type == error_mark_node || error_operand_p (expr))
5223 return expr;
5224 return build1 (NOP_EXPR, type, expr);
5227 /* Take the address of ARG, whatever that means under C++ semantics.
5228 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5229 and class rvalues as well.
5231 Nothing should call this function directly; instead, callers should use
5232 cp_build_addr_expr or cp_build_addr_expr_strict. */
5234 static tree
5235 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5237 tree argtype;
5238 tree val;
5240 if (!arg || error_operand_p (arg))
5241 return error_mark_node;
5243 arg = mark_lvalue_use (arg);
5244 argtype = lvalue_type (arg);
5246 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5248 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5249 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5251 /* They're trying to take the address of a unique non-static
5252 member function. This is ill-formed (except in MS-land),
5253 but let's try to DTRT.
5254 Note: We only handle unique functions here because we don't
5255 want to complain if there's a static overload; non-unique
5256 cases will be handled by instantiate_type. But we need to
5257 handle this case here to allow casts on the resulting PMF.
5258 We could defer this in non-MS mode, but it's easier to give
5259 a useful error here. */
5261 /* Inside constant member functions, the `this' pointer
5262 contains an extra const qualifier. TYPE_MAIN_VARIANT
5263 is used here to remove this const from the diagnostics
5264 and the created OFFSET_REF. */
5265 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5266 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5267 mark_used (fn);
5269 if (! flag_ms_extensions)
5271 tree name = DECL_NAME (fn);
5272 if (!(complain & tf_error))
5273 return error_mark_node;
5274 else if (current_class_type
5275 && TREE_OPERAND (arg, 0) == current_class_ref)
5276 /* An expression like &memfn. */
5277 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5278 " or parenthesized non-static member function to form"
5279 " a pointer to member function. Say %<&%T::%D%>",
5280 base, name);
5281 else
5282 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5283 " function to form a pointer to member function."
5284 " Say %<&%T::%D%>",
5285 base, name);
5287 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5290 /* Uninstantiated types are all functions. Taking the
5291 address of a function is a no-op, so just return the
5292 argument. */
5293 if (type_unknown_p (arg))
5294 return build1 (ADDR_EXPR, unknown_type_node, arg);
5296 if (TREE_CODE (arg) == OFFSET_REF)
5297 /* We want a pointer to member; bypass all the code for actually taking
5298 the address of something. */
5299 goto offset_ref;
5301 /* Anything not already handled and not a true memory reference
5302 is an error. */
5303 if (TREE_CODE (argtype) != FUNCTION_TYPE
5304 && TREE_CODE (argtype) != METHOD_TYPE)
5306 cp_lvalue_kind kind = lvalue_kind (arg);
5307 if (kind == clk_none)
5309 if (complain & tf_error)
5310 lvalue_error (input_location, lv_addressof);
5311 return error_mark_node;
5313 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5315 if (!(complain & tf_error))
5316 return error_mark_node;
5317 if (kind & clk_class)
5318 /* Make this a permerror because we used to accept it. */
5319 permerror (input_location, "taking address of temporary");
5320 else
5321 error ("taking address of xvalue (rvalue reference)");
5325 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5327 tree type = build_pointer_type (TREE_TYPE (argtype));
5328 arg = build1 (CONVERT_EXPR, type, arg);
5329 return arg;
5331 else if (pedantic && DECL_MAIN_P (arg))
5333 /* ARM $3.4 */
5334 /* Apparently a lot of autoconf scripts for C++ packages do this,
5335 so only complain if -Wpedantic. */
5336 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5337 pedwarn (input_location, OPT_Wpedantic,
5338 "ISO C++ forbids taking address of function %<::main%>");
5339 else if (flag_pedantic_errors)
5340 return error_mark_node;
5343 /* Let &* cancel out to simplify resulting code. */
5344 if (INDIRECT_REF_P (arg))
5346 /* We don't need to have `current_class_ptr' wrapped in a
5347 NON_LVALUE_EXPR node. */
5348 if (arg == current_class_ref)
5349 return current_class_ptr;
5351 arg = TREE_OPERAND (arg, 0);
5352 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5354 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5355 arg = build1 (CONVERT_EXPR, type, arg);
5357 else
5358 /* Don't let this be an lvalue. */
5359 arg = rvalue (arg);
5360 return arg;
5363 /* ??? Cope with user tricks that amount to offsetof. */
5364 if (TREE_CODE (argtype) != FUNCTION_TYPE
5365 && TREE_CODE (argtype) != METHOD_TYPE
5366 && argtype != unknown_type_node
5367 && (val = get_base_address (arg))
5368 && COMPLETE_TYPE_P (TREE_TYPE (val))
5369 && INDIRECT_REF_P (val)
5370 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5372 tree type = build_pointer_type (argtype);
5373 return fold_convert (type, fold_offsetof_1 (arg));
5376 /* Handle complex lvalues (when permitted)
5377 by reduction to simpler cases. */
5378 val = unary_complex_lvalue (ADDR_EXPR, arg);
5379 if (val != 0)
5380 return val;
5382 switch (TREE_CODE (arg))
5384 CASE_CONVERT:
5385 case FLOAT_EXPR:
5386 case FIX_TRUNC_EXPR:
5387 /* Even if we're not being pedantic, we cannot allow this
5388 extension when we're instantiating in a SFINAE
5389 context. */
5390 if (! lvalue_p (arg) && complain == tf_none)
5392 if (complain & tf_error)
5393 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5394 else
5395 return error_mark_node;
5397 break;
5399 case BASELINK:
5400 arg = BASELINK_FUNCTIONS (arg);
5401 /* Fall through. */
5403 case OVERLOAD:
5404 arg = OVL_CURRENT (arg);
5405 break;
5407 case OFFSET_REF:
5408 offset_ref:
5409 /* Turn a reference to a non-static data member into a
5410 pointer-to-member. */
5412 tree type;
5413 tree t;
5415 gcc_assert (PTRMEM_OK_P (arg));
5417 t = TREE_OPERAND (arg, 1);
5418 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5420 if (complain & tf_error)
5421 error ("cannot create pointer to reference member %qD", t);
5422 return error_mark_node;
5425 type = build_ptrmem_type (context_for_name_lookup (t),
5426 TREE_TYPE (t));
5427 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5428 return t;
5431 default:
5432 break;
5435 if (argtype != error_mark_node)
5437 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (argtype))
5439 if (complain & tf_warning_or_error)
5440 pedwarn (input_location, OPT_Wvla,
5441 "taking address of array of runtime bound");
5442 else
5443 return error_mark_node;
5445 argtype = build_pointer_type (argtype);
5448 /* In a template, we are processing a non-dependent expression
5449 so we can just form an ADDR_EXPR with the correct type. */
5450 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5452 val = build_address (arg);
5453 if (TREE_CODE (arg) == OFFSET_REF)
5454 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5456 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5458 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5460 /* We can only get here with a single static member
5461 function. */
5462 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5463 && DECL_STATIC_FUNCTION_P (fn));
5464 mark_used (fn);
5465 val = build_address (fn);
5466 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5467 /* Do not lose object's side effects. */
5468 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5469 TREE_OPERAND (arg, 0), val);
5471 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5473 if (complain & tf_error)
5474 error ("attempt to take address of bit-field structure member %qD",
5475 TREE_OPERAND (arg, 1));
5476 return error_mark_node;
5478 else
5480 tree object = TREE_OPERAND (arg, 0);
5481 tree field = TREE_OPERAND (arg, 1);
5482 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5483 (TREE_TYPE (object), decl_type_context (field)));
5484 val = build_address (arg);
5487 if (TYPE_PTR_P (argtype)
5488 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5490 build_ptrmemfunc_type (argtype);
5491 val = build_ptrmemfunc (argtype, val, 0,
5492 /*c_cast_p=*/false,
5493 complain);
5496 return val;
5499 /* Take the address of ARG if it has one, even if it's an rvalue. */
5501 tree
5502 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5504 return cp_build_addr_expr_1 (arg, 0, complain);
5507 /* Take the address of ARG, but only if it's an lvalue. */
5509 tree
5510 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5512 return cp_build_addr_expr_1 (arg, 1, complain);
5515 /* C++: Must handle pointers to members.
5517 Perhaps type instantiation should be extended to handle conversion
5518 from aggregates to types we don't yet know we want? (Or are those
5519 cases typically errors which should be reported?)
5521 NOCONVERT nonzero suppresses the default promotions
5522 (such as from short to int). */
5524 tree
5525 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5526 tsubst_flags_t complain)
5528 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5529 tree arg = xarg;
5530 tree argtype = 0;
5531 const char *errstring = NULL;
5532 tree val;
5533 const char *invalid_op_diag;
5535 if (!arg || error_operand_p (arg))
5536 return error_mark_node;
5538 if ((invalid_op_diag
5539 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5540 ? CONVERT_EXPR
5541 : code),
5542 TREE_TYPE (xarg))))
5544 if (complain & tf_error)
5545 error (invalid_op_diag);
5546 return error_mark_node;
5549 switch (code)
5551 case UNARY_PLUS_EXPR:
5552 case NEGATE_EXPR:
5554 int flags = WANT_ARITH | WANT_ENUM;
5555 /* Unary plus (but not unary minus) is allowed on pointers. */
5556 if (code == UNARY_PLUS_EXPR)
5557 flags |= WANT_POINTER;
5558 arg = build_expr_type_conversion (flags, arg, true);
5559 if (!arg)
5560 errstring = (code == NEGATE_EXPR
5561 ? _("wrong type argument to unary minus")
5562 : _("wrong type argument to unary plus"));
5563 else
5565 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5566 arg = cp_perform_integral_promotions (arg, complain);
5568 /* Make sure the result is not an lvalue: a unary plus or minus
5569 expression is always a rvalue. */
5570 arg = rvalue (arg);
5573 break;
5575 case BIT_NOT_EXPR:
5576 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5578 code = CONJ_EXPR;
5579 if (!noconvert)
5581 arg = cp_default_conversion (arg, complain);
5582 if (arg == error_mark_node)
5583 return error_mark_node;
5586 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5587 | WANT_VECTOR_OR_COMPLEX,
5588 arg, true)))
5589 errstring = _("wrong type argument to bit-complement");
5590 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5591 arg = cp_perform_integral_promotions (arg, complain);
5592 break;
5594 case ABS_EXPR:
5595 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5596 errstring = _("wrong type argument to abs");
5597 else if (!noconvert)
5599 arg = cp_default_conversion (arg, complain);
5600 if (arg == error_mark_node)
5601 return error_mark_node;
5603 break;
5605 case CONJ_EXPR:
5606 /* Conjugating a real value is a no-op, but allow it anyway. */
5607 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5608 errstring = _("wrong type argument to conjugation");
5609 else if (!noconvert)
5611 arg = cp_default_conversion (arg, complain);
5612 if (arg == error_mark_node)
5613 return error_mark_node;
5615 break;
5617 case TRUTH_NOT_EXPR:
5618 arg = perform_implicit_conversion (boolean_type_node, arg,
5619 complain);
5620 val = invert_truthvalue_loc (input_location, arg);
5621 if (arg != error_mark_node)
5622 return val;
5623 errstring = _("in argument to unary !");
5624 break;
5626 case NOP_EXPR:
5627 break;
5629 case REALPART_EXPR:
5630 case IMAGPART_EXPR:
5631 arg = build_real_imag_expr (input_location, code, arg);
5632 if (arg == error_mark_node)
5633 return arg;
5634 else
5635 return fold_if_not_in_template (arg);
5637 case PREINCREMENT_EXPR:
5638 case POSTINCREMENT_EXPR:
5639 case PREDECREMENT_EXPR:
5640 case POSTDECREMENT_EXPR:
5641 /* Handle complex lvalues (when permitted)
5642 by reduction to simpler cases. */
5644 val = unary_complex_lvalue (code, arg);
5645 if (val != 0)
5646 return val;
5648 arg = mark_lvalue_use (arg);
5650 /* Increment or decrement the real part of the value,
5651 and don't change the imaginary part. */
5652 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5654 tree real, imag;
5656 arg = stabilize_reference (arg);
5657 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5658 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5659 real = cp_build_unary_op (code, real, 1, complain);
5660 if (real == error_mark_node || imag == error_mark_node)
5661 return error_mark_node;
5662 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5663 real, imag);
5666 /* Report invalid types. */
5668 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5669 arg, true)))
5671 if (code == PREINCREMENT_EXPR)
5672 errstring = _("no pre-increment operator for type");
5673 else if (code == POSTINCREMENT_EXPR)
5674 errstring = _("no post-increment operator for type");
5675 else if (code == PREDECREMENT_EXPR)
5676 errstring = _("no pre-decrement operator for type");
5677 else
5678 errstring = _("no post-decrement operator for type");
5679 break;
5681 else if (arg == error_mark_node)
5682 return error_mark_node;
5684 /* Report something read-only. */
5686 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5687 || TREE_READONLY (arg))
5689 if (complain & tf_error)
5690 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5691 || code == POSTINCREMENT_EXPR)
5692 ? lv_increment : lv_decrement));
5693 else
5694 return error_mark_node;
5698 tree inc;
5699 tree declared_type = unlowered_expr_type (arg);
5701 argtype = TREE_TYPE (arg);
5703 /* ARM $5.2.5 last annotation says this should be forbidden. */
5704 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5706 if (complain & tf_error)
5707 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5708 ? G_("ISO C++ forbids incrementing an enum")
5709 : G_("ISO C++ forbids decrementing an enum"));
5710 else
5711 return error_mark_node;
5714 /* Compute the increment. */
5716 if (TYPE_PTR_P (argtype))
5718 tree type = complete_type (TREE_TYPE (argtype));
5720 if (!COMPLETE_OR_VOID_TYPE_P (type))
5722 if (complain & tf_error)
5723 error (((code == PREINCREMENT_EXPR
5724 || code == POSTINCREMENT_EXPR))
5725 ? G_("cannot increment a pointer to incomplete type %qT")
5726 : G_("cannot decrement a pointer to incomplete type %qT"),
5727 TREE_TYPE (argtype));
5728 else
5729 return error_mark_node;
5731 else if (!TYPE_PTROB_P (argtype))
5733 if (complain & tf_error)
5734 pedwarn (input_location, OPT_Wpointer_arith,
5735 (code == PREINCREMENT_EXPR
5736 || code == POSTINCREMENT_EXPR)
5737 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5738 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5739 argtype);
5740 else
5741 return error_mark_node;
5744 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5746 else
5747 inc = integer_one_node;
5749 inc = cp_convert (argtype, inc, complain);
5751 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5752 need to ask Objective-C to build the increment or decrement
5753 expression for it. */
5754 if (objc_is_property_ref (arg))
5755 return objc_build_incr_expr_for_property_ref (input_location, code,
5756 arg, inc);
5758 /* Complain about anything else that is not a true lvalue. */
5759 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5760 || code == POSTINCREMENT_EXPR)
5761 ? lv_increment : lv_decrement),
5762 complain))
5763 return error_mark_node;
5765 /* Forbid using -- on `bool'. */
5766 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5768 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5770 if (complain & tf_error)
5771 error ("invalid use of Boolean expression as operand "
5772 "to %<operator--%>");
5773 return error_mark_node;
5775 val = boolean_increment (code, arg);
5777 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5778 /* An rvalue has no cv-qualifiers. */
5779 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5780 else
5781 val = build2 (code, TREE_TYPE (arg), arg, inc);
5783 TREE_SIDE_EFFECTS (val) = 1;
5784 return val;
5787 case ADDR_EXPR:
5788 /* Note that this operation never does default_conversion
5789 regardless of NOCONVERT. */
5790 return cp_build_addr_expr (arg, complain);
5792 default:
5793 break;
5796 if (!errstring)
5798 if (argtype == 0)
5799 argtype = TREE_TYPE (arg);
5800 return fold_if_not_in_template (build1 (code, argtype, arg));
5803 if (complain & tf_error)
5804 error ("%s", errstring);
5805 return error_mark_node;
5808 /* Hook for the c-common bits that build a unary op. */
5809 tree
5810 build_unary_op (location_t /*location*/,
5811 enum tree_code code, tree xarg, int noconvert)
5813 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5816 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5817 for certain kinds of expressions which are not really lvalues
5818 but which we can accept as lvalues.
5820 If ARG is not a kind of expression we can handle, return
5821 NULL_TREE. */
5823 tree
5824 unary_complex_lvalue (enum tree_code code, tree arg)
5826 /* Inside a template, making these kinds of adjustments is
5827 pointless; we are only concerned with the type of the
5828 expression. */
5829 if (processing_template_decl)
5830 return NULL_TREE;
5832 /* Handle (a, b) used as an "lvalue". */
5833 if (TREE_CODE (arg) == COMPOUND_EXPR)
5835 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5836 tf_warning_or_error);
5837 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5838 TREE_OPERAND (arg, 0), real_result);
5841 /* Handle (a ? b : c) used as an "lvalue". */
5842 if (TREE_CODE (arg) == COND_EXPR
5843 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5844 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5846 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5847 if (TREE_CODE (arg) == MODIFY_EXPR
5848 || TREE_CODE (arg) == PREINCREMENT_EXPR
5849 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5851 tree lvalue = TREE_OPERAND (arg, 0);
5852 if (TREE_SIDE_EFFECTS (lvalue))
5854 lvalue = stabilize_reference (lvalue);
5855 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5856 lvalue, TREE_OPERAND (arg, 1));
5858 return unary_complex_lvalue
5859 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5862 if (code != ADDR_EXPR)
5863 return NULL_TREE;
5865 /* Handle (a = b) used as an "lvalue" for `&'. */
5866 if (TREE_CODE (arg) == MODIFY_EXPR
5867 || TREE_CODE (arg) == INIT_EXPR)
5869 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5870 tf_warning_or_error);
5871 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5872 arg, real_result);
5873 TREE_NO_WARNING (arg) = 1;
5874 return arg;
5877 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5878 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5879 || TREE_CODE (arg) == OFFSET_REF)
5880 return NULL_TREE;
5882 /* We permit compiler to make function calls returning
5883 objects of aggregate type look like lvalues. */
5885 tree targ = arg;
5887 if (TREE_CODE (targ) == SAVE_EXPR)
5888 targ = TREE_OPERAND (targ, 0);
5890 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5892 if (TREE_CODE (arg) == SAVE_EXPR)
5893 targ = arg;
5894 else
5895 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5896 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5899 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
5900 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5901 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5904 /* Don't let anything else be handled specially. */
5905 return NULL_TREE;
5908 /* Mark EXP saying that we need to be able to take the
5909 address of it; it should not be allocated in a register.
5910 Value is true if successful.
5912 C++: we do not allow `current_class_ptr' to be addressable. */
5914 bool
5915 cxx_mark_addressable (tree exp)
5917 tree x = exp;
5919 while (1)
5920 switch (TREE_CODE (x))
5922 case ADDR_EXPR:
5923 case COMPONENT_REF:
5924 case ARRAY_REF:
5925 case REALPART_EXPR:
5926 case IMAGPART_EXPR:
5927 x = TREE_OPERAND (x, 0);
5928 break;
5930 case PARM_DECL:
5931 if (x == current_class_ptr)
5933 error ("cannot take the address of %<this%>, which is an rvalue expression");
5934 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5935 return true;
5937 /* Fall through. */
5939 case VAR_DECL:
5940 /* Caller should not be trying to mark initialized
5941 constant fields addressable. */
5942 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5943 || DECL_IN_AGGR_P (x) == 0
5944 || TREE_STATIC (x)
5945 || DECL_EXTERNAL (x));
5946 /* Fall through. */
5948 case RESULT_DECL:
5949 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5950 && !DECL_ARTIFICIAL (x))
5952 if (VAR_P (x) && DECL_HARD_REGISTER (x))
5954 error
5955 ("address of explicit register variable %qD requested", x);
5956 return false;
5958 else if (extra_warnings)
5959 warning
5960 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5962 TREE_ADDRESSABLE (x) = 1;
5963 return true;
5965 case CONST_DECL:
5966 case FUNCTION_DECL:
5967 TREE_ADDRESSABLE (x) = 1;
5968 return true;
5970 case CONSTRUCTOR:
5971 TREE_ADDRESSABLE (x) = 1;
5972 return true;
5974 case TARGET_EXPR:
5975 TREE_ADDRESSABLE (x) = 1;
5976 cxx_mark_addressable (TREE_OPERAND (x, 0));
5977 return true;
5979 default:
5980 return true;
5984 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5986 tree
5987 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
5988 tsubst_flags_t complain)
5990 tree orig_ifexp = ifexp;
5991 tree orig_op1 = op1;
5992 tree orig_op2 = op2;
5993 tree expr;
5995 if (processing_template_decl)
5997 /* The standard says that the expression is type-dependent if
5998 IFEXP is type-dependent, even though the eventual type of the
5999 expression doesn't dependent on IFEXP. */
6000 if (type_dependent_expression_p (ifexp)
6001 /* As a GNU extension, the middle operand may be omitted. */
6002 || (op1 && type_dependent_expression_p (op1))
6003 || type_dependent_expression_p (op2))
6004 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6005 ifexp = build_non_dependent_expr (ifexp);
6006 if (op1)
6007 op1 = build_non_dependent_expr (op1);
6008 op2 = build_non_dependent_expr (op2);
6011 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6012 if (processing_template_decl && expr != error_mark_node
6013 && TREE_CODE (expr) != VEC_COND_EXPR)
6015 tree min = build_min_non_dep (COND_EXPR, expr,
6016 orig_ifexp, orig_op1, orig_op2);
6017 /* In C++11, remember that the result is an lvalue or xvalue.
6018 In C++98, lvalue_kind can just assume lvalue in a template. */
6019 if (cxx_dialect >= cxx11
6020 && lvalue_or_rvalue_with_address_p (expr)
6021 && !lvalue_or_rvalue_with_address_p (min))
6022 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6023 !real_lvalue_p (expr));
6024 expr = convert_from_reference (min);
6026 return expr;
6029 /* Given a list of expressions, return a compound expression
6030 that performs them all and returns the value of the last of them. */
6032 tree
6033 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6034 tsubst_flags_t complain)
6036 tree expr = TREE_VALUE (list);
6038 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6039 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6041 if (complain & tf_error)
6042 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
6043 "non-class type must not be parenthesized");
6044 else
6045 return error_mark_node;
6048 if (TREE_CHAIN (list))
6050 if (complain & tf_error)
6051 switch (exp)
6053 case ELK_INIT:
6054 permerror (input_location, "expression list treated as compound "
6055 "expression in initializer");
6056 break;
6057 case ELK_MEM_INIT:
6058 permerror (input_location, "expression list treated as compound "
6059 "expression in mem-initializer");
6060 break;
6061 case ELK_FUNC_CAST:
6062 permerror (input_location, "expression list treated as compound "
6063 "expression in functional cast");
6064 break;
6065 default:
6066 gcc_unreachable ();
6068 else
6069 return error_mark_node;
6071 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6072 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6073 expr, TREE_VALUE (list), complain);
6076 return expr;
6079 /* Like build_x_compound_expr_from_list, but using a VEC. */
6081 tree
6082 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6083 tsubst_flags_t complain)
6085 if (vec_safe_is_empty (vec))
6086 return NULL_TREE;
6087 else if (vec->length () == 1)
6088 return (*vec)[0];
6089 else
6091 tree expr;
6092 unsigned int ix;
6093 tree t;
6095 if (msg != NULL)
6097 if (complain & tf_error)
6098 permerror (input_location,
6099 "%s expression list treated as compound expression",
6100 msg);
6101 else
6102 return error_mark_node;
6105 expr = (*vec)[0];
6106 for (ix = 1; vec->iterate (ix, &t); ++ix)
6107 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6108 t, complain);
6110 return expr;
6114 /* Handle overloading of the ',' operator when needed. */
6116 tree
6117 build_x_compound_expr (location_t loc, tree op1, tree op2,
6118 tsubst_flags_t complain)
6120 tree result;
6121 tree orig_op1 = op1;
6122 tree orig_op2 = op2;
6124 if (processing_template_decl)
6126 if (type_dependent_expression_p (op1)
6127 || type_dependent_expression_p (op2))
6128 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6129 op1 = build_non_dependent_expr (op1);
6130 op2 = build_non_dependent_expr (op2);
6133 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6134 NULL_TREE, /*overload=*/NULL, complain);
6135 if (!result)
6136 result = cp_build_compound_expr (op1, op2, complain);
6138 if (processing_template_decl && result != error_mark_node)
6139 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6141 return result;
6144 /* Like cp_build_compound_expr, but for the c-common bits. */
6146 tree
6147 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6149 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6152 /* Build a compound expression. */
6154 tree
6155 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6157 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6159 if (lhs == error_mark_node || rhs == error_mark_node)
6160 return error_mark_node;
6162 if (TREE_CODE (rhs) == TARGET_EXPR)
6164 /* If the rhs is a TARGET_EXPR, then build the compound
6165 expression inside the target_expr's initializer. This
6166 helps the compiler to eliminate unnecessary temporaries. */
6167 tree init = TREE_OPERAND (rhs, 1);
6169 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6170 TREE_OPERAND (rhs, 1) = init;
6172 return rhs;
6175 if (type_unknown_p (rhs))
6177 if (complain & tf_error)
6178 error ("no context to resolve type of %qE", rhs);
6179 return error_mark_node;
6182 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6185 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6186 casts away constness. CAST gives the type of cast. Returns true
6187 if the cast is ill-formed, false if it is well-formed.
6189 ??? This function warns for casting away any qualifier not just
6190 const. We would like to specify exactly what qualifiers are casted
6191 away.
6194 static bool
6195 check_for_casting_away_constness (tree src_type, tree dest_type,
6196 enum tree_code cast, tsubst_flags_t complain)
6198 /* C-style casts are allowed to cast away constness. With
6199 WARN_CAST_QUAL, we still want to issue a warning. */
6200 if (cast == CAST_EXPR && !warn_cast_qual)
6201 return false;
6203 if (!casts_away_constness (src_type, dest_type, complain))
6204 return false;
6206 switch (cast)
6208 case CAST_EXPR:
6209 if (complain & tf_warning)
6210 warning (OPT_Wcast_qual,
6211 "cast from type %qT to type %qT casts away qualifiers",
6212 src_type, dest_type);
6213 return false;
6215 case STATIC_CAST_EXPR:
6216 if (complain & tf_error)
6217 error ("static_cast from type %qT to type %qT casts away qualifiers",
6218 src_type, dest_type);
6219 return true;
6221 case REINTERPRET_CAST_EXPR:
6222 if (complain & tf_error)
6223 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6224 src_type, dest_type);
6225 return true;
6227 default:
6228 gcc_unreachable();
6233 Warns if the cast from expression EXPR to type TYPE is useless.
6235 void
6236 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6238 if (warn_useless_cast
6239 && complain & tf_warning
6240 && c_inhibit_evaluation_warnings == 0)
6242 if (REFERENCE_REF_P (expr))
6243 expr = TREE_OPERAND (expr, 0);
6245 if ((TREE_CODE (type) == REFERENCE_TYPE
6246 && (TYPE_REF_IS_RVALUE (type)
6247 ? xvalue_p (expr) : real_lvalue_p (expr))
6248 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6249 || same_type_p (TREE_TYPE (expr), type))
6250 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6254 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6255 (another pointer-to-member type in the same hierarchy) and return
6256 the converted expression. If ALLOW_INVERSE_P is permitted, a
6257 pointer-to-derived may be converted to pointer-to-base; otherwise,
6258 only the other direction is permitted. If C_CAST_P is true, this
6259 conversion is taking place as part of a C-style cast. */
6261 tree
6262 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6263 bool c_cast_p, tsubst_flags_t complain)
6265 if (TYPE_PTRDATAMEM_P (type))
6267 tree delta;
6269 if (TREE_CODE (expr) == PTRMEM_CST)
6270 expr = cplus_expand_constant (expr);
6271 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6272 TYPE_PTRMEM_CLASS_TYPE (type),
6273 allow_inverse_p,
6274 c_cast_p, complain);
6275 if (delta == error_mark_node)
6276 return error_mark_node;
6278 if (!integer_zerop (delta))
6280 tree cond, op1, op2;
6282 cond = cp_build_binary_op (input_location,
6283 EQ_EXPR,
6284 expr,
6285 build_int_cst (TREE_TYPE (expr), -1),
6286 complain);
6287 op1 = build_nop (ptrdiff_type_node, expr);
6288 op2 = cp_build_binary_op (input_location,
6289 PLUS_EXPR, op1, delta,
6290 complain);
6292 expr = fold_build3_loc (input_location,
6293 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6297 return build_nop (type, expr);
6299 else
6300 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6301 allow_inverse_p, c_cast_p, complain);
6304 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6305 this static_cast is being attempted as one of the possible casts
6306 allowed by a C-style cast. (In that case, accessibility of base
6307 classes is not considered, and it is OK to cast away
6308 constness.) Return the result of the cast. *VALID_P is set to
6309 indicate whether or not the cast was valid. */
6311 static tree
6312 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6313 bool *valid_p, tsubst_flags_t complain)
6315 tree intype;
6316 tree result;
6317 cp_lvalue_kind clk;
6319 /* Assume the cast is valid. */
6320 *valid_p = true;
6322 intype = unlowered_expr_type (expr);
6324 /* Save casted types in the function's used types hash table. */
6325 used_types_insert (type);
6327 /* [expr.static.cast]
6329 An lvalue of type "cv1 B", where B is a class type, can be cast
6330 to type "reference to cv2 D", where D is a class derived (clause
6331 _class.derived_) from B, if a valid standard conversion from
6332 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6333 same cv-qualification as, or greater cv-qualification than, cv1,
6334 and B is not a virtual base class of D. */
6335 /* We check this case before checking the validity of "TYPE t =
6336 EXPR;" below because for this case:
6338 struct B {};
6339 struct D : public B { D(const B&); };
6340 extern B& b;
6341 void f() { static_cast<const D&>(b); }
6343 we want to avoid constructing a new D. The standard is not
6344 completely clear about this issue, but our interpretation is
6345 consistent with other compilers. */
6346 if (TREE_CODE (type) == REFERENCE_TYPE
6347 && CLASS_TYPE_P (TREE_TYPE (type))
6348 && CLASS_TYPE_P (intype)
6349 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6350 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6351 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6352 build_pointer_type (TYPE_MAIN_VARIANT
6353 (TREE_TYPE (type))),
6354 complain)
6355 && (c_cast_p
6356 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6358 tree base;
6360 /* There is a standard conversion from "D*" to "B*" even if "B"
6361 is ambiguous or inaccessible. If this is really a
6362 static_cast, then we check both for inaccessibility and
6363 ambiguity. However, if this is a static_cast being performed
6364 because the user wrote a C-style cast, then accessibility is
6365 not considered. */
6366 base = lookup_base (TREE_TYPE (type), intype,
6367 c_cast_p ? ba_unique : ba_check,
6368 NULL, complain);
6370 /* Convert from "B*" to "D*". This function will check that "B"
6371 is not a virtual base of "D". */
6372 expr = build_base_path (MINUS_EXPR, build_address (expr),
6373 base, /*nonnull=*/false, complain);
6374 /* Convert the pointer to a reference -- but then remember that
6375 there are no expressions with reference type in C++.
6377 We call rvalue so that there's an actual tree code
6378 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6379 is a variable with the same type, the conversion would get folded
6380 away, leaving just the variable and causing lvalue_kind to give
6381 the wrong answer. */
6382 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6385 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6386 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6387 if (TREE_CODE (type) == REFERENCE_TYPE
6388 && TYPE_REF_IS_RVALUE (type)
6389 && (clk = real_lvalue_p (expr))
6390 && reference_related_p (TREE_TYPE (type), intype)
6391 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6393 if (clk == clk_ordinary)
6395 /* Handle the (non-bit-field) lvalue case here by casting to
6396 lvalue reference and then changing it to an rvalue reference.
6397 Casting an xvalue to rvalue reference will be handled by the
6398 main code path. */
6399 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6400 result = (perform_direct_initialization_if_possible
6401 (lref, expr, c_cast_p, complain));
6402 result = cp_fold_convert (type, result);
6403 /* Make sure we don't fold back down to a named rvalue reference,
6404 because that would be an lvalue. */
6405 if (DECL_P (result))
6406 result = build1 (NON_LVALUE_EXPR, type, result);
6407 return convert_from_reference (result);
6409 else
6410 /* For a bit-field or packed field, bind to a temporary. */
6411 expr = rvalue (expr);
6414 /* Resolve overloaded address here rather than once in
6415 implicit_conversion and again in the inverse code below. */
6416 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6418 expr = instantiate_type (type, expr, complain);
6419 intype = TREE_TYPE (expr);
6422 /* [expr.static.cast]
6424 Any expression can be explicitly converted to type cv void. */
6425 if (VOID_TYPE_P (type))
6426 return convert_to_void (expr, ICV_CAST, complain);
6428 /* [class.abstract]
6429 An abstract class shall not be used ... as the type of an explicit
6430 conversion. */
6431 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6432 return error_mark_node;
6434 /* [expr.static.cast]
6436 An expression e can be explicitly converted to a type T using a
6437 static_cast of the form static_cast<T>(e) if the declaration T
6438 t(e);" is well-formed, for some invented temporary variable
6439 t. */
6440 result = perform_direct_initialization_if_possible (type, expr,
6441 c_cast_p, complain);
6442 if (result)
6444 result = convert_from_reference (result);
6446 /* [expr.static.cast]
6448 If T is a reference type, the result is an lvalue; otherwise,
6449 the result is an rvalue. */
6450 if (TREE_CODE (type) != REFERENCE_TYPE)
6451 result = rvalue (result);
6452 return result;
6455 /* [expr.static.cast]
6457 The inverse of any standard conversion sequence (clause _conv_),
6458 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6459 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6460 (_conv.bool_) conversions, can be performed explicitly using
6461 static_cast subject to the restriction that the explicit
6462 conversion does not cast away constness (_expr.const.cast_), and
6463 the following additional rules for specific cases: */
6464 /* For reference, the conversions not excluded are: integral
6465 promotions, floating point promotion, integral conversions,
6466 floating point conversions, floating-integral conversions,
6467 pointer conversions, and pointer to member conversions. */
6468 /* DR 128
6470 A value of integral _or enumeration_ type can be explicitly
6471 converted to an enumeration type. */
6472 /* The effect of all that is that any conversion between any two
6473 types which are integral, floating, or enumeration types can be
6474 performed. */
6475 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6476 || SCALAR_FLOAT_TYPE_P (type))
6477 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6478 || SCALAR_FLOAT_TYPE_P (intype)))
6479 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6481 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6482 && CLASS_TYPE_P (TREE_TYPE (type))
6483 && CLASS_TYPE_P (TREE_TYPE (intype))
6484 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6485 (TREE_TYPE (intype))),
6486 build_pointer_type (TYPE_MAIN_VARIANT
6487 (TREE_TYPE (type))),
6488 complain))
6490 tree base;
6492 if (!c_cast_p
6493 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6494 complain))
6495 return error_mark_node;
6496 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6497 c_cast_p ? ba_unique : ba_check,
6498 NULL, complain);
6499 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6500 complain);
6501 return cp_fold_convert(type, expr);
6504 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6505 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6507 tree c1;
6508 tree c2;
6509 tree t1;
6510 tree t2;
6512 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6513 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6515 if (TYPE_PTRDATAMEM_P (type))
6517 t1 = (build_ptrmem_type
6518 (c1,
6519 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6520 t2 = (build_ptrmem_type
6521 (c2,
6522 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6524 else
6526 t1 = intype;
6527 t2 = type;
6529 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6531 if (!c_cast_p
6532 && check_for_casting_away_constness (intype, type,
6533 STATIC_CAST_EXPR,
6534 complain))
6535 return error_mark_node;
6536 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6537 c_cast_p, complain);
6541 /* [expr.static.cast]
6543 An rvalue of type "pointer to cv void" can be explicitly
6544 converted to a pointer to object type. A value of type pointer
6545 to object converted to "pointer to cv void" and back to the
6546 original pointer type will have its original value. */
6547 if (TYPE_PTR_P (intype)
6548 && VOID_TYPE_P (TREE_TYPE (intype))
6549 && TYPE_PTROB_P (type))
6551 if (!c_cast_p
6552 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6553 complain))
6554 return error_mark_node;
6555 return build_nop (type, expr);
6558 *valid_p = false;
6559 return error_mark_node;
6562 /* Return an expression representing static_cast<TYPE>(EXPR). */
6564 tree
6565 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6567 tree result;
6568 bool valid_p;
6570 if (type == error_mark_node || expr == error_mark_node)
6571 return error_mark_node;
6573 if (processing_template_decl)
6575 expr = build_min (STATIC_CAST_EXPR, type, expr);
6576 /* We don't know if it will or will not have side effects. */
6577 TREE_SIDE_EFFECTS (expr) = 1;
6578 return convert_from_reference (expr);
6581 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6582 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6583 if (TREE_CODE (type) != REFERENCE_TYPE
6584 && TREE_CODE (expr) == NOP_EXPR
6585 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6586 expr = TREE_OPERAND (expr, 0);
6588 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6589 complain);
6590 if (valid_p)
6592 if (result != error_mark_node)
6593 maybe_warn_about_useless_cast (type, expr, complain);
6594 return result;
6597 if (complain & tf_error)
6598 error ("invalid static_cast from type %qT to type %qT",
6599 TREE_TYPE (expr), type);
6600 return error_mark_node;
6603 /* EXPR is an expression with member function or pointer-to-member
6604 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6605 not permitted by ISO C++, but we accept it in some modes. If we
6606 are not in one of those modes, issue a diagnostic. Return the
6607 converted expression. */
6609 tree
6610 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6612 tree intype;
6613 tree decl;
6615 intype = TREE_TYPE (expr);
6616 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6617 || TREE_CODE (intype) == METHOD_TYPE);
6619 if (!(complain & tf_warning_or_error))
6620 return error_mark_node;
6622 if (pedantic || warn_pmf2ptr)
6623 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6624 "converting from %qT to %qT", intype, type);
6626 if (TREE_CODE (intype) == METHOD_TYPE)
6627 expr = build_addr_func (expr, complain);
6628 else if (TREE_CODE (expr) == PTRMEM_CST)
6629 expr = build_address (PTRMEM_CST_MEMBER (expr));
6630 else
6632 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6633 decl = build_address (decl);
6634 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6637 if (expr == error_mark_node)
6638 return error_mark_node;
6640 return build_nop (type, expr);
6643 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6644 If C_CAST_P is true, this reinterpret cast is being done as part of
6645 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6646 indicate whether or not reinterpret_cast was valid. */
6648 static tree
6649 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6650 bool *valid_p, tsubst_flags_t complain)
6652 tree intype;
6654 /* Assume the cast is invalid. */
6655 if (valid_p)
6656 *valid_p = true;
6658 if (type == error_mark_node || error_operand_p (expr))
6659 return error_mark_node;
6661 intype = TREE_TYPE (expr);
6663 /* Save casted types in the function's used types hash table. */
6664 used_types_insert (type);
6666 /* [expr.reinterpret.cast]
6667 An lvalue expression of type T1 can be cast to the type
6668 "reference to T2" if an expression of type "pointer to T1" can be
6669 explicitly converted to the type "pointer to T2" using a
6670 reinterpret_cast. */
6671 if (TREE_CODE (type) == REFERENCE_TYPE)
6673 if (! real_lvalue_p (expr))
6675 if (complain & tf_error)
6676 error ("invalid cast of an rvalue expression of type "
6677 "%qT to type %qT",
6678 intype, type);
6679 return error_mark_node;
6682 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6683 "B" are related class types; the reinterpret_cast does not
6684 adjust the pointer. */
6685 if (TYPE_PTR_P (intype)
6686 && (complain & tf_warning)
6687 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6688 COMPARE_BASE | COMPARE_DERIVED)))
6689 warning (0, "casting %qT to %qT does not dereference pointer",
6690 intype, type);
6692 expr = cp_build_addr_expr (expr, complain);
6694 if (warn_strict_aliasing > 2)
6695 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6697 if (expr != error_mark_node)
6698 expr = build_reinterpret_cast_1
6699 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6700 valid_p, complain);
6701 if (expr != error_mark_node)
6702 /* cp_build_indirect_ref isn't right for rvalue refs. */
6703 expr = convert_from_reference (fold_convert (type, expr));
6704 return expr;
6707 /* As a G++ extension, we consider conversions from member
6708 functions, and pointers to member functions to
6709 pointer-to-function and pointer-to-void types. If
6710 -Wno-pmf-conversions has not been specified,
6711 convert_member_func_to_ptr will issue an error message. */
6712 if ((TYPE_PTRMEMFUNC_P (intype)
6713 || TREE_CODE (intype) == METHOD_TYPE)
6714 && TYPE_PTR_P (type)
6715 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6716 || VOID_TYPE_P (TREE_TYPE (type))))
6717 return convert_member_func_to_ptr (type, expr, complain);
6719 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6720 array-to-pointer, and function-to-pointer conversions are
6721 performed. */
6722 expr = decay_conversion (expr, complain);
6724 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6725 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6726 if (TREE_CODE (expr) == NOP_EXPR
6727 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6728 expr = TREE_OPERAND (expr, 0);
6730 if (error_operand_p (expr))
6731 return error_mark_node;
6733 intype = TREE_TYPE (expr);
6735 /* [expr.reinterpret.cast]
6736 A pointer can be converted to any integral type large enough to
6737 hold it. ... A value of type std::nullptr_t can be converted to
6738 an integral type; the conversion has the same meaning and
6739 validity as a conversion of (void*)0 to the integral type. */
6740 if (CP_INTEGRAL_TYPE_P (type)
6741 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6743 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6745 if (complain & tf_error)
6746 permerror (input_location, "cast from %qT to %qT loses precision",
6747 intype, type);
6748 else
6749 return error_mark_node;
6751 if (NULLPTR_TYPE_P (intype))
6752 return build_int_cst (type, 0);
6754 /* [expr.reinterpret.cast]
6755 A value of integral or enumeration type can be explicitly
6756 converted to a pointer. */
6757 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6758 /* OK */
6760 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6761 || TYPE_PTR_OR_PTRMEM_P (type))
6762 && same_type_p (type, intype))
6763 /* DR 799 */
6764 return fold_if_not_in_template (build_nop (type, expr));
6765 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6766 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6767 return fold_if_not_in_template (build_nop (type, expr));
6768 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6769 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6771 tree sexpr = expr;
6773 if (!c_cast_p
6774 && check_for_casting_away_constness (intype, type,
6775 REINTERPRET_CAST_EXPR,
6776 complain))
6777 return error_mark_node;
6778 /* Warn about possible alignment problems. */
6779 if (STRICT_ALIGNMENT && warn_cast_align
6780 && (complain & tf_warning)
6781 && !VOID_TYPE_P (type)
6782 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6783 && COMPLETE_TYPE_P (TREE_TYPE (type))
6784 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6785 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6786 warning (OPT_Wcast_align, "cast from %qT to %qT "
6787 "increases required alignment of target type", intype, type);
6789 /* We need to strip nops here, because the front end likes to
6790 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6791 STRIP_NOPS (sexpr);
6792 if (warn_strict_aliasing <= 2)
6793 strict_aliasing_warning (intype, type, sexpr);
6795 return fold_if_not_in_template (build_nop (type, expr));
6797 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6798 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6800 if (complain & tf_warning)
6801 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6802 object pointer type or vice versa is conditionally-supported." */
6803 warning (OPT_Wconditionally_supported,
6804 "casting between pointer-to-function and pointer-to-object "
6805 "is conditionally-supported");
6806 return fold_if_not_in_template (build_nop (type, expr));
6808 else if (TREE_CODE (type) == VECTOR_TYPE)
6809 return fold_if_not_in_template (convert_to_vector (type, expr));
6810 else if (TREE_CODE (intype) == VECTOR_TYPE
6811 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6812 return fold_if_not_in_template (convert_to_integer (type, expr));
6813 else
6815 if (valid_p)
6816 *valid_p = false;
6817 if (complain & tf_error)
6818 error ("invalid cast from type %qT to type %qT", intype, type);
6819 return error_mark_node;
6822 return cp_convert (type, expr, complain);
6825 tree
6826 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6828 tree r;
6830 if (type == error_mark_node || expr == error_mark_node)
6831 return error_mark_node;
6833 if (processing_template_decl)
6835 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6837 if (!TREE_SIDE_EFFECTS (t)
6838 && type_dependent_expression_p (expr))
6839 /* There might turn out to be side effects inside expr. */
6840 TREE_SIDE_EFFECTS (t) = 1;
6841 return convert_from_reference (t);
6844 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6845 /*valid_p=*/NULL, complain);
6846 if (r != error_mark_node)
6847 maybe_warn_about_useless_cast (type, expr, complain);
6848 return r;
6851 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6852 return an appropriate expression. Otherwise, return
6853 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6854 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6855 performing a C-style cast, its value upon return will indicate
6856 whether or not the conversion succeeded. */
6858 static tree
6859 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6860 bool *valid_p)
6862 tree src_type;
6863 tree reference_type;
6865 /* Callers are responsible for handling error_mark_node as a
6866 destination type. */
6867 gcc_assert (dst_type != error_mark_node);
6868 /* In a template, callers should be building syntactic
6869 representations of casts, not using this machinery. */
6870 gcc_assert (!processing_template_decl);
6872 /* Assume the conversion is invalid. */
6873 if (valid_p)
6874 *valid_p = false;
6876 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6878 if (complain & tf_error)
6879 error ("invalid use of const_cast with type %qT, "
6880 "which is not a pointer, "
6881 "reference, nor a pointer-to-data-member type", dst_type);
6882 return error_mark_node;
6885 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6887 if (complain & tf_error)
6888 error ("invalid use of const_cast with type %qT, which is a pointer "
6889 "or reference to a function type", dst_type);
6890 return error_mark_node;
6893 /* Save casted types in the function's used types hash table. */
6894 used_types_insert (dst_type);
6896 src_type = TREE_TYPE (expr);
6897 /* Expressions do not really have reference types. */
6898 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6899 src_type = TREE_TYPE (src_type);
6901 /* [expr.const.cast]
6903 For two object types T1 and T2, if a pointer to T1 can be explicitly
6904 converted to the type "pointer to T2" using a const_cast, then the
6905 following conversions can also be made:
6907 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6908 type T2 using the cast const_cast<T2&>;
6910 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6911 type T2 using the cast const_cast<T2&&>; and
6913 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6914 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6916 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6918 reference_type = dst_type;
6919 if (!TYPE_REF_IS_RVALUE (dst_type)
6920 ? real_lvalue_p (expr)
6921 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6922 ? lvalue_p (expr)
6923 : lvalue_or_rvalue_with_address_p (expr)))
6924 /* OK. */;
6925 else
6927 if (complain & tf_error)
6928 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6929 src_type, dst_type);
6930 return error_mark_node;
6932 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6933 src_type = build_pointer_type (src_type);
6935 else
6937 reference_type = NULL_TREE;
6938 /* If the destination type is not a reference type, the
6939 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6940 conversions are performed. */
6941 src_type = type_decays_to (src_type);
6942 if (src_type == error_mark_node)
6943 return error_mark_node;
6946 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
6948 if (comp_ptr_ttypes_const (dst_type, src_type))
6950 if (valid_p)
6952 *valid_p = true;
6953 /* This cast is actually a C-style cast. Issue a warning if
6954 the user is making a potentially unsafe cast. */
6955 check_for_casting_away_constness (src_type, dst_type,
6956 CAST_EXPR, complain);
6958 if (reference_type)
6960 expr = cp_build_addr_expr (expr, complain);
6961 if (expr == error_mark_node)
6962 return error_mark_node;
6963 expr = build_nop (reference_type, expr);
6964 return convert_from_reference (expr);
6966 else
6968 expr = decay_conversion (expr, complain);
6969 if (expr == error_mark_node)
6970 return error_mark_node;
6972 /* build_c_cast puts on a NOP_EXPR to make the result not an
6973 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6974 non-lvalue context. */
6975 if (TREE_CODE (expr) == NOP_EXPR
6976 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6977 expr = TREE_OPERAND (expr, 0);
6978 return build_nop (dst_type, expr);
6981 else if (valid_p
6982 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6983 TREE_TYPE (src_type)))
6984 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6985 complain);
6988 if (complain & tf_error)
6989 error ("invalid const_cast from type %qT to type %qT",
6990 src_type, dst_type);
6991 return error_mark_node;
6994 tree
6995 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6997 tree r;
6999 if (type == error_mark_node || error_operand_p (expr))
7000 return error_mark_node;
7002 if (processing_template_decl)
7004 tree t = build_min (CONST_CAST_EXPR, type, expr);
7006 if (!TREE_SIDE_EFFECTS (t)
7007 && type_dependent_expression_p (expr))
7008 /* There might turn out to be side effects inside expr. */
7009 TREE_SIDE_EFFECTS (t) = 1;
7010 return convert_from_reference (t);
7013 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7014 if (r != error_mark_node)
7015 maybe_warn_about_useless_cast (type, expr, complain);
7016 return r;
7019 /* Like cp_build_c_cast, but for the c-common bits. */
7021 tree
7022 build_c_cast (location_t /*loc*/, tree type, tree expr)
7024 return cp_build_c_cast (type, expr, tf_warning_or_error);
7027 /* Build an expression representing an explicit C-style cast to type
7028 TYPE of expression EXPR. */
7030 tree
7031 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7033 tree value = expr;
7034 tree result;
7035 bool valid_p;
7037 if (type == error_mark_node || error_operand_p (expr))
7038 return error_mark_node;
7040 if (processing_template_decl)
7042 tree t = build_min (CAST_EXPR, type,
7043 tree_cons (NULL_TREE, value, NULL_TREE));
7044 /* We don't know if it will or will not have side effects. */
7045 TREE_SIDE_EFFECTS (t) = 1;
7046 return convert_from_reference (t);
7049 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7050 'Class') should always be retained, because this information aids
7051 in method lookup. */
7052 if (objc_is_object_ptr (type)
7053 && objc_is_object_ptr (TREE_TYPE (expr)))
7054 return build_nop (type, expr);
7056 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7057 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7058 if (TREE_CODE (type) != REFERENCE_TYPE
7059 && TREE_CODE (value) == NOP_EXPR
7060 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7061 value = TREE_OPERAND (value, 0);
7063 if (TREE_CODE (type) == ARRAY_TYPE)
7065 /* Allow casting from T1* to T2[] because Cfront allows it.
7066 NIHCL uses it. It is not valid ISO C++ however. */
7067 if (TYPE_PTR_P (TREE_TYPE (expr)))
7069 if (complain & tf_error)
7070 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7071 else
7072 return error_mark_node;
7073 type = build_pointer_type (TREE_TYPE (type));
7075 else
7077 if (complain & tf_error)
7078 error ("ISO C++ forbids casting to an array type %qT", type);
7079 return error_mark_node;
7083 if (TREE_CODE (type) == FUNCTION_TYPE
7084 || TREE_CODE (type) == METHOD_TYPE)
7086 if (complain & tf_error)
7087 error ("invalid cast to function type %qT", type);
7088 return error_mark_node;
7091 if (TYPE_PTR_P (type)
7092 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7093 /* Casting to an integer of smaller size is an error detected elsewhere. */
7094 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7095 /* Don't warn about converting any constant. */
7096 && !TREE_CONSTANT (value))
7097 warning_at (input_location, OPT_Wint_to_pointer_cast,
7098 "cast to pointer from integer of different size");
7100 /* A C-style cast can be a const_cast. */
7101 result = build_const_cast_1 (type, value, complain & tf_warning,
7102 &valid_p);
7103 if (valid_p)
7105 if (result != error_mark_node)
7106 maybe_warn_about_useless_cast (type, value, complain);
7107 return result;
7110 /* Or a static cast. */
7111 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7112 &valid_p, complain);
7113 /* Or a reinterpret_cast. */
7114 if (!valid_p)
7115 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7116 &valid_p, complain);
7117 /* The static_cast or reinterpret_cast may be followed by a
7118 const_cast. */
7119 if (valid_p
7120 /* A valid cast may result in errors if, for example, a
7121 conversion to an ambiguous base class is required. */
7122 && !error_operand_p (result))
7124 tree result_type;
7126 maybe_warn_about_useless_cast (type, value, complain);
7128 /* Non-class rvalues always have cv-unqualified type. */
7129 if (!CLASS_TYPE_P (type))
7130 type = TYPE_MAIN_VARIANT (type);
7131 result_type = TREE_TYPE (result);
7132 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7133 result_type = TYPE_MAIN_VARIANT (result_type);
7134 /* If the type of RESULT does not match TYPE, perform a
7135 const_cast to make it match. If the static_cast or
7136 reinterpret_cast succeeded, we will differ by at most
7137 cv-qualification, so the follow-on const_cast is guaranteed
7138 to succeed. */
7139 if (!same_type_p (non_reference (type), non_reference (result_type)))
7141 result = build_const_cast_1 (type, result, false, &valid_p);
7142 gcc_assert (valid_p);
7144 return result;
7147 return error_mark_node;
7150 /* For use from the C common bits. */
7151 tree
7152 build_modify_expr (location_t /*location*/,
7153 tree lhs, tree /*lhs_origtype*/,
7154 enum tree_code modifycode,
7155 location_t /*rhs_location*/, tree rhs,
7156 tree /*rhs_origtype*/)
7158 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7161 /* Build an assignment expression of lvalue LHS from value RHS.
7162 MODIFYCODE is the code for a binary operator that we use
7163 to combine the old value of LHS with RHS to get the new value.
7164 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7166 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7168 tree
7169 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7170 tsubst_flags_t complain)
7172 tree result;
7173 tree newrhs = rhs;
7174 tree lhstype = TREE_TYPE (lhs);
7175 tree olhstype = lhstype;
7176 bool plain_assign = (modifycode == NOP_EXPR);
7178 /* Avoid duplicate error messages from operands that had errors. */
7179 if (error_operand_p (lhs) || error_operand_p (rhs))
7180 return error_mark_node;
7182 /* Handle control structure constructs used as "lvalues". */
7183 switch (TREE_CODE (lhs))
7185 /* Handle --foo = 5; as these are valid constructs in C++. */
7186 case PREDECREMENT_EXPR:
7187 case PREINCREMENT_EXPR:
7188 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7189 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7190 stabilize_reference (TREE_OPERAND (lhs, 0)),
7191 TREE_OPERAND (lhs, 1));
7192 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7193 modifycode, rhs, complain);
7194 if (newrhs == error_mark_node)
7195 return error_mark_node;
7196 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7198 /* Handle (a, b) used as an "lvalue". */
7199 case COMPOUND_EXPR:
7200 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7201 modifycode, rhs, complain);
7202 if (newrhs == error_mark_node)
7203 return error_mark_node;
7204 return build2 (COMPOUND_EXPR, lhstype,
7205 TREE_OPERAND (lhs, 0), newrhs);
7207 case MODIFY_EXPR:
7208 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7209 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7210 stabilize_reference (TREE_OPERAND (lhs, 0)),
7211 TREE_OPERAND (lhs, 1));
7212 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7213 complain);
7214 if (newrhs == error_mark_node)
7215 return error_mark_node;
7216 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7218 case MIN_EXPR:
7219 case MAX_EXPR:
7220 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7221 when neither operand has side-effects. */
7222 if (!lvalue_or_else (lhs, lv_assign, complain))
7223 return error_mark_node;
7225 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7226 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7228 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7229 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7230 boolean_type_node,
7231 TREE_OPERAND (lhs, 0),
7232 TREE_OPERAND (lhs, 1)),
7233 TREE_OPERAND (lhs, 0),
7234 TREE_OPERAND (lhs, 1));
7235 /* Fall through. */
7237 /* Handle (a ? b : c) used as an "lvalue". */
7238 case COND_EXPR:
7240 /* Produce (a ? (b = rhs) : (c = rhs))
7241 except that the RHS goes through a save-expr
7242 so the code to compute it is only emitted once. */
7243 tree cond;
7244 tree preeval = NULL_TREE;
7246 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7248 if (complain & tf_error)
7249 error ("void value not ignored as it ought to be");
7250 return error_mark_node;
7253 rhs = stabilize_expr (rhs, &preeval);
7255 /* Check this here to avoid odd errors when trying to convert
7256 a throw to the type of the COND_EXPR. */
7257 if (!lvalue_or_else (lhs, lv_assign, complain))
7258 return error_mark_node;
7260 cond = build_conditional_expr
7261 (input_location, TREE_OPERAND (lhs, 0),
7262 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7263 modifycode, rhs, complain),
7264 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7265 modifycode, rhs, complain),
7266 complain);
7268 if (cond == error_mark_node)
7269 return cond;
7270 /* Make sure the code to compute the rhs comes out
7271 before the split. */
7272 if (preeval)
7273 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7274 return cond;
7277 default:
7278 break;
7281 if (modifycode == INIT_EXPR)
7283 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7284 /* Do the default thing. */;
7285 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7287 /* Compound literal. */
7288 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7289 /* Call convert to generate an error; see PR 11063. */
7290 rhs = convert (lhstype, rhs);
7291 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7292 TREE_SIDE_EFFECTS (result) = 1;
7293 return result;
7295 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7296 /* Do the default thing. */;
7297 else
7299 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7300 result = build_special_member_call (lhs, complete_ctor_identifier,
7301 &rhs_vec, lhstype, LOOKUP_NORMAL,
7302 complain);
7303 release_tree_vector (rhs_vec);
7304 if (result == NULL_TREE)
7305 return error_mark_node;
7306 return result;
7309 else
7311 lhs = require_complete_type_sfinae (lhs, complain);
7312 if (lhs == error_mark_node)
7313 return error_mark_node;
7315 if (modifycode == NOP_EXPR)
7317 if (c_dialect_objc ())
7319 result = objc_maybe_build_modify_expr (lhs, rhs);
7320 if (result)
7321 return result;
7324 /* `operator=' is not an inheritable operator. */
7325 if (! MAYBE_CLASS_TYPE_P (lhstype))
7326 /* Do the default thing. */;
7327 else
7329 result = build_new_op (input_location, MODIFY_EXPR,
7330 LOOKUP_NORMAL, lhs, rhs,
7331 make_node (NOP_EXPR), /*overload=*/NULL,
7332 complain);
7333 if (result == NULL_TREE)
7334 return error_mark_node;
7335 return result;
7337 lhstype = olhstype;
7339 else
7341 tree init = NULL_TREE;
7343 /* A binary op has been requested. Combine the old LHS
7344 value with the RHS producing the value we should actually
7345 store into the LHS. */
7346 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7347 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7348 || MAYBE_CLASS_TYPE_P (lhstype)));
7350 /* Preevaluate the RHS to make sure its evaluation is complete
7351 before the lvalue-to-rvalue conversion of the LHS:
7353 [expr.ass] With respect to an indeterminately-sequenced
7354 function call, the operation of a compound assignment is a
7355 single evaluation. [ Note: Therefore, a function call shall
7356 not intervene between the lvalue-to-rvalue conversion and the
7357 side effect associated with any single compound assignment
7358 operator. -- end note ] */
7359 lhs = stabilize_reference (lhs);
7360 if (TREE_SIDE_EFFECTS (rhs))
7361 rhs = mark_rvalue_use (rhs);
7362 rhs = stabilize_expr (rhs, &init);
7363 newrhs = cp_build_binary_op (input_location,
7364 modifycode, lhs, rhs,
7365 complain);
7366 if (newrhs == error_mark_node)
7368 if (complain & tf_error)
7369 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7370 TREE_TYPE (lhs), TREE_TYPE (rhs));
7371 return error_mark_node;
7374 if (init)
7375 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7377 /* Now it looks like a plain assignment. */
7378 modifycode = NOP_EXPR;
7379 if (c_dialect_objc ())
7381 result = objc_maybe_build_modify_expr (lhs, newrhs);
7382 if (result)
7383 return result;
7386 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7387 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7390 /* The left-hand side must be an lvalue. */
7391 if (!lvalue_or_else (lhs, lv_assign, complain))
7392 return error_mark_node;
7394 /* Warn about modifying something that is `const'. Don't warn if
7395 this is initialization. */
7396 if (modifycode != INIT_EXPR
7397 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7398 /* Functions are not modifiable, even though they are
7399 lvalues. */
7400 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7401 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7402 /* If it's an aggregate and any field is const, then it is
7403 effectively const. */
7404 || (CLASS_TYPE_P (lhstype)
7405 && C_TYPE_FIELDS_READONLY (lhstype))))
7407 if (complain & tf_error)
7408 cxx_readonly_error (lhs, lv_assign);
7409 else
7410 return error_mark_node;
7413 /* If storing into a structure or union member, it may have been given a
7414 lowered bitfield type. We need to convert to the declared type first,
7415 so retrieve it now. */
7417 olhstype = unlowered_expr_type (lhs);
7419 /* Convert new value to destination type. */
7421 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7423 int from_array;
7425 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7427 if (modifycode != INIT_EXPR)
7429 if (complain & tf_error)
7430 error ("assigning to an array from an initializer list");
7431 return error_mark_node;
7433 if (check_array_initializer (lhs, lhstype, newrhs))
7434 return error_mark_node;
7435 newrhs = digest_init (lhstype, newrhs, complain);
7436 if (newrhs == error_mark_node)
7437 return error_mark_node;
7440 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7441 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7443 if (complain & tf_error)
7444 error ("incompatible types in assignment of %qT to %qT",
7445 TREE_TYPE (rhs), lhstype);
7446 return error_mark_node;
7449 /* Allow array assignment in compiler-generated code. */
7450 else if (!current_function_decl
7451 || !DECL_DEFAULTED_FN (current_function_decl))
7453 /* This routine is used for both initialization and assignment.
7454 Make sure the diagnostic message differentiates the context. */
7455 if (complain & tf_error)
7457 if (modifycode == INIT_EXPR)
7458 error ("array used as initializer");
7459 else
7460 error ("invalid array assignment");
7462 return error_mark_node;
7465 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7466 ? 1 + (modifycode != INIT_EXPR): 0;
7467 return build_vec_init (lhs, NULL_TREE, newrhs,
7468 /*explicit_value_init_p=*/false,
7469 from_array, complain);
7472 if (modifycode == INIT_EXPR)
7473 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7474 LOOKUP_ONLYCONVERTING. */
7475 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7476 ICR_INIT, NULL_TREE, 0,
7477 complain);
7478 else
7479 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7480 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7482 if (!same_type_p (lhstype, olhstype))
7483 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7485 if (modifycode != INIT_EXPR)
7487 if (TREE_CODE (newrhs) == CALL_EXPR
7488 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7489 newrhs = build_cplus_new (lhstype, newrhs, complain);
7491 /* Can't initialize directly from a TARGET_EXPR, since that would
7492 cause the lhs to be constructed twice, and possibly result in
7493 accidental self-initialization. So we force the TARGET_EXPR to be
7494 expanded without a target. */
7495 if (TREE_CODE (newrhs) == TARGET_EXPR)
7496 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7497 TREE_OPERAND (newrhs, 0));
7500 if (newrhs == error_mark_node)
7501 return error_mark_node;
7503 if (c_dialect_objc () && flag_objc_gc)
7505 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7507 if (result)
7508 return result;
7511 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7512 lhstype, lhs, newrhs);
7514 TREE_SIDE_EFFECTS (result) = 1;
7515 if (!plain_assign)
7516 TREE_NO_WARNING (result) = 1;
7518 return result;
7521 tree
7522 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7523 tree rhs, tsubst_flags_t complain)
7525 if (processing_template_decl)
7526 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7527 build_min_nt_loc (loc, modifycode, NULL_TREE,
7528 NULL_TREE), rhs);
7530 if (modifycode != NOP_EXPR)
7532 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7533 make_node (modifycode), /*overload=*/NULL,
7534 complain);
7535 if (rval)
7537 TREE_NO_WARNING (rval) = 1;
7538 return rval;
7541 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7544 /* Helper function for get_delta_difference which assumes FROM is a base
7545 class of TO. Returns a delta for the conversion of pointer-to-member
7546 of FROM to pointer-to-member of TO. If the conversion is invalid and
7547 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7548 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7549 If C_CAST_P is true, this conversion is taking place as part of a
7550 C-style cast. */
7552 static tree
7553 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7554 tsubst_flags_t complain)
7556 tree binfo;
7557 base_kind kind;
7559 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7560 &kind, complain);
7562 if (binfo == error_mark_node)
7564 if (!(complain & tf_error))
7565 return error_mark_node;
7567 error (" in pointer to member function conversion");
7568 return size_zero_node;
7570 else if (binfo)
7572 if (kind != bk_via_virtual)
7573 return BINFO_OFFSET (binfo);
7574 else
7575 /* FROM is a virtual base class of TO. Issue an error or warning
7576 depending on whether or not this is a reinterpret cast. */
7578 if (!(complain & tf_error))
7579 return error_mark_node;
7581 error ("pointer to member conversion via virtual base %qT",
7582 BINFO_TYPE (binfo_from_vbase (binfo)));
7584 return size_zero_node;
7587 else
7588 return NULL_TREE;
7591 /* Get difference in deltas for different pointer to member function
7592 types. If the conversion is invalid and tf_error is not set in
7593 COMPLAIN, returns error_mark_node, otherwise returns an integer
7594 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7595 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7596 conversions as well. If C_CAST_P is true this conversion is taking
7597 place as part of a C-style cast.
7599 Note that the naming of FROM and TO is kind of backwards; the return
7600 value is what we add to a TO in order to get a FROM. They are named
7601 this way because we call this function to find out how to convert from
7602 a pointer to member of FROM to a pointer to member of TO. */
7604 static tree
7605 get_delta_difference (tree from, tree to,
7606 bool allow_inverse_p,
7607 bool c_cast_p, tsubst_flags_t complain)
7609 tree result;
7611 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7612 /* Pointer to member of incomplete class is permitted*/
7613 result = size_zero_node;
7614 else
7615 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7617 if (result == error_mark_node)
7618 return error_mark_node;
7620 if (!result)
7622 if (!allow_inverse_p)
7624 if (!(complain & tf_error))
7625 return error_mark_node;
7627 error_not_base_type (from, to);
7628 error (" in pointer to member conversion");
7629 result = size_zero_node;
7631 else
7633 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7635 if (result == error_mark_node)
7636 return error_mark_node;
7638 if (result)
7639 result = size_diffop_loc (input_location,
7640 size_zero_node, result);
7641 else
7643 if (!(complain & tf_error))
7644 return error_mark_node;
7646 error_not_base_type (from, to);
7647 error (" in pointer to member conversion");
7648 result = size_zero_node;
7653 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7654 result));
7657 /* Return a constructor for the pointer-to-member-function TYPE using
7658 the other components as specified. */
7660 tree
7661 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7663 tree u = NULL_TREE;
7664 tree delta_field;
7665 tree pfn_field;
7666 vec<constructor_elt, va_gc> *v;
7668 /* Pull the FIELD_DECLs out of the type. */
7669 pfn_field = TYPE_FIELDS (type);
7670 delta_field = DECL_CHAIN (pfn_field);
7672 /* Make sure DELTA has the type we want. */
7673 delta = convert_and_check (delta_type_node, delta);
7675 /* Convert to the correct target type if necessary. */
7676 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7678 /* Finish creating the initializer. */
7679 vec_alloc (v, 2);
7680 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7681 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7682 u = build_constructor (type, v);
7683 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7684 TREE_STATIC (u) = (TREE_CONSTANT (u)
7685 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7686 != NULL_TREE)
7687 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7688 != NULL_TREE));
7689 return u;
7692 /* Build a constructor for a pointer to member function. It can be
7693 used to initialize global variables, local variable, or used
7694 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7695 want to be.
7697 If FORCE is nonzero, then force this conversion, even if
7698 we would rather not do it. Usually set when using an explicit
7699 cast. A C-style cast is being processed iff C_CAST_P is true.
7701 Return error_mark_node, if something goes wrong. */
7703 tree
7704 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7705 tsubst_flags_t complain)
7707 tree fn;
7708 tree pfn_type;
7709 tree to_type;
7711 if (error_operand_p (pfn))
7712 return error_mark_node;
7714 pfn_type = TREE_TYPE (pfn);
7715 to_type = build_ptrmemfunc_type (type);
7717 /* Handle multiple conversions of pointer to member functions. */
7718 if (TYPE_PTRMEMFUNC_P (pfn_type))
7720 tree delta = NULL_TREE;
7721 tree npfn = NULL_TREE;
7722 tree n;
7724 if (!force
7725 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7726 LOOKUP_NORMAL, complain))
7728 if (complain & tf_error)
7729 error ("invalid conversion to type %qT from type %qT",
7730 to_type, pfn_type);
7731 else
7732 return error_mark_node;
7735 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7736 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7737 force,
7738 c_cast_p, complain);
7739 if (n == error_mark_node)
7740 return error_mark_node;
7742 /* We don't have to do any conversion to convert a
7743 pointer-to-member to its own type. But, we don't want to
7744 just return a PTRMEM_CST if there's an explicit cast; that
7745 cast should make the expression an invalid template argument. */
7746 if (TREE_CODE (pfn) != PTRMEM_CST)
7748 if (same_type_p (to_type, pfn_type))
7749 return pfn;
7750 else if (integer_zerop (n))
7751 return build_reinterpret_cast (to_type, pfn,
7752 complain);
7755 if (TREE_SIDE_EFFECTS (pfn))
7756 pfn = save_expr (pfn);
7758 /* Obtain the function pointer and the current DELTA. */
7759 if (TREE_CODE (pfn) == PTRMEM_CST)
7760 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7761 else
7763 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7764 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7767 /* Just adjust the DELTA field. */
7768 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7769 (TREE_TYPE (delta), ptrdiff_type_node));
7770 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7771 n = cp_build_binary_op (input_location,
7772 LSHIFT_EXPR, n, integer_one_node,
7773 complain);
7774 delta = cp_build_binary_op (input_location,
7775 PLUS_EXPR, delta, n, complain);
7776 return build_ptrmemfunc1 (to_type, delta, npfn);
7779 /* Handle null pointer to member function conversions. */
7780 if (null_ptr_cst_p (pfn))
7782 pfn = build_c_cast (input_location, type, pfn);
7783 return build_ptrmemfunc1 (to_type,
7784 integer_zero_node,
7785 pfn);
7788 if (type_unknown_p (pfn))
7789 return instantiate_type (type, pfn, complain);
7791 fn = TREE_OPERAND (pfn, 0);
7792 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7793 /* In a template, we will have preserved the
7794 OFFSET_REF. */
7795 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7796 return make_ptrmem_cst (to_type, fn);
7799 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7800 given by CST.
7802 ??? There is no consistency as to the types returned for the above
7803 values. Some code acts as if it were a sizetype and some as if it were
7804 integer_type_node. */
7806 void
7807 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7809 tree type = TREE_TYPE (cst);
7810 tree fn = PTRMEM_CST_MEMBER (cst);
7811 tree ptr_class, fn_class;
7813 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7815 /* The class that the function belongs to. */
7816 fn_class = DECL_CONTEXT (fn);
7818 /* The class that we're creating a pointer to member of. */
7819 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7821 /* First, calculate the adjustment to the function's class. */
7822 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7823 /*c_cast_p=*/0, tf_warning_or_error);
7825 if (!DECL_VIRTUAL_P (fn))
7826 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7827 build_addr_func (fn, tf_warning_or_error));
7828 else
7830 /* If we're dealing with a virtual function, we have to adjust 'this'
7831 again, to point to the base which provides the vtable entry for
7832 fn; the call will do the opposite adjustment. */
7833 tree orig_class = DECL_CONTEXT (fn);
7834 tree binfo = binfo_or_else (orig_class, fn_class);
7835 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7836 *delta, BINFO_OFFSET (binfo));
7837 *delta = fold_if_not_in_template (*delta);
7839 /* We set PFN to the vtable offset at which the function can be
7840 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7841 case delta is shifted left, and then incremented). */
7842 *pfn = DECL_VINDEX (fn);
7843 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7844 TYPE_SIZE_UNIT (vtable_entry_type));
7845 *pfn = fold_if_not_in_template (*pfn);
7847 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7849 case ptrmemfunc_vbit_in_pfn:
7850 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7851 integer_one_node);
7852 *pfn = fold_if_not_in_template (*pfn);
7853 break;
7855 case ptrmemfunc_vbit_in_delta:
7856 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7857 *delta, integer_one_node);
7858 *delta = fold_if_not_in_template (*delta);
7859 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7860 *delta, integer_one_node);
7861 *delta = fold_if_not_in_template (*delta);
7862 break;
7864 default:
7865 gcc_unreachable ();
7868 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7869 *pfn = fold_if_not_in_template (*pfn);
7873 /* Return an expression for PFN from the pointer-to-member function
7874 given by T. */
7876 static tree
7877 pfn_from_ptrmemfunc (tree t)
7879 if (TREE_CODE (t) == PTRMEM_CST)
7881 tree delta;
7882 tree pfn;
7884 expand_ptrmemfunc_cst (t, &delta, &pfn);
7885 if (pfn)
7886 return pfn;
7889 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7892 /* Return an expression for DELTA from the pointer-to-member function
7893 given by T. */
7895 static tree
7896 delta_from_ptrmemfunc (tree t)
7898 if (TREE_CODE (t) == PTRMEM_CST)
7900 tree delta;
7901 tree pfn;
7903 expand_ptrmemfunc_cst (t, &delta, &pfn);
7904 if (delta)
7905 return delta;
7908 return build_ptrmemfunc_access_expr (t, delta_identifier);
7911 /* Convert value RHS to type TYPE as preparation for an assignment to
7912 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7913 implicit conversion is. If FNDECL is non-NULL, we are doing the
7914 conversion in order to pass the PARMNUMth argument of FNDECL.
7915 If FNDECL is NULL, we are doing the conversion in function pointer
7916 argument passing, conversion in initialization, etc. */
7918 static tree
7919 convert_for_assignment (tree type, tree rhs,
7920 impl_conv_rhs errtype, tree fndecl, int parmnum,
7921 tsubst_flags_t complain, int flags)
7923 tree rhstype;
7924 enum tree_code coder;
7926 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7927 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7928 rhs = TREE_OPERAND (rhs, 0);
7930 rhstype = TREE_TYPE (rhs);
7931 coder = TREE_CODE (rhstype);
7933 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7934 && vector_types_convertible_p (type, rhstype, true))
7936 rhs = mark_rvalue_use (rhs);
7937 return convert (type, rhs);
7940 if (rhs == error_mark_node || rhstype == error_mark_node)
7941 return error_mark_node;
7942 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7943 return error_mark_node;
7945 /* The RHS of an assignment cannot have void type. */
7946 if (coder == VOID_TYPE)
7948 if (complain & tf_error)
7949 error ("void value not ignored as it ought to be");
7950 return error_mark_node;
7953 if (c_dialect_objc ())
7955 int parmno;
7956 tree selector;
7957 tree rname = fndecl;
7959 switch (errtype)
7961 case ICR_ASSIGN:
7962 parmno = -1;
7963 break;
7964 case ICR_INIT:
7965 parmno = -2;
7966 break;
7967 default:
7968 selector = objc_message_selector ();
7969 parmno = parmnum;
7970 if (selector && parmno > 1)
7972 rname = selector;
7973 parmno -= 1;
7977 if (objc_compare_types (type, rhstype, parmno, rname))
7979 rhs = mark_rvalue_use (rhs);
7980 return convert (type, rhs);
7984 /* [expr.ass]
7986 The expression is implicitly converted (clause _conv_) to the
7987 cv-unqualified type of the left operand.
7989 We allow bad conversions here because by the time we get to this point
7990 we are committed to doing the conversion. If we end up doing a bad
7991 conversion, convert_like will complain. */
7992 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
7994 /* When -Wno-pmf-conversions is use, we just silently allow
7995 conversions from pointers-to-members to plain pointers. If
7996 the conversion doesn't work, cp_convert will complain. */
7997 if (!warn_pmf2ptr
7998 && TYPE_PTR_P (type)
7999 && TYPE_PTRMEMFUNC_P (rhstype))
8000 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8001 else
8003 if (complain & tf_error)
8005 /* If the right-hand side has unknown type, then it is an
8006 overloaded function. Call instantiate_type to get error
8007 messages. */
8008 if (rhstype == unknown_type_node)
8009 instantiate_type (type, rhs, tf_warning_or_error);
8010 else if (fndecl)
8011 error ("cannot convert %qT to %qT for argument %qP to %qD",
8012 rhstype, type, parmnum, fndecl);
8013 else
8014 switch (errtype)
8016 case ICR_DEFAULT_ARGUMENT:
8017 error ("cannot convert %qT to %qT in default argument",
8018 rhstype, type);
8019 break;
8020 case ICR_ARGPASS:
8021 error ("cannot convert %qT to %qT in argument passing",
8022 rhstype, type);
8023 break;
8024 case ICR_CONVERTING:
8025 error ("cannot convert %qT to %qT",
8026 rhstype, type);
8027 break;
8028 case ICR_INIT:
8029 error ("cannot convert %qT to %qT in initialization",
8030 rhstype, type);
8031 break;
8032 case ICR_RETURN:
8033 error ("cannot convert %qT to %qT in return",
8034 rhstype, type);
8035 break;
8036 case ICR_ASSIGN:
8037 error ("cannot convert %qT to %qT in assignment",
8038 rhstype, type);
8039 break;
8040 default:
8041 gcc_unreachable();
8044 return error_mark_node;
8047 if (warn_suggest_attribute_format)
8049 const enum tree_code codel = TREE_CODE (type);
8050 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8051 && coder == codel
8052 && check_missing_format_attribute (type, rhstype)
8053 && (complain & tf_warning))
8054 switch (errtype)
8056 case ICR_ARGPASS:
8057 case ICR_DEFAULT_ARGUMENT:
8058 if (fndecl)
8059 warning (OPT_Wsuggest_attribute_format,
8060 "parameter %qP of %qD might be a candidate "
8061 "for a format attribute", parmnum, fndecl);
8062 else
8063 warning (OPT_Wsuggest_attribute_format,
8064 "parameter might be a candidate "
8065 "for a format attribute");
8066 break;
8067 case ICR_CONVERTING:
8068 warning (OPT_Wsuggest_attribute_format,
8069 "target of conversion might be a candidate "
8070 "for a format attribute");
8071 break;
8072 case ICR_INIT:
8073 warning (OPT_Wsuggest_attribute_format,
8074 "target of initialization might be a candidate "
8075 "for a format attribute");
8076 break;
8077 case ICR_RETURN:
8078 warning (OPT_Wsuggest_attribute_format,
8079 "return type might be a candidate "
8080 "for a format attribute");
8081 break;
8082 case ICR_ASSIGN:
8083 warning (OPT_Wsuggest_attribute_format,
8084 "left-hand side of assignment might be a candidate "
8085 "for a format attribute");
8086 break;
8087 default:
8088 gcc_unreachable();
8092 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8093 does not. */
8094 if (warn_parentheses
8095 && TREE_CODE (type) == BOOLEAN_TYPE
8096 && TREE_CODE (rhs) == MODIFY_EXPR
8097 && !TREE_NO_WARNING (rhs)
8098 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8099 && (complain & tf_warning))
8101 location_t loc = EXPR_LOC_OR_HERE (rhs);
8103 warning_at (loc, OPT_Wparentheses,
8104 "suggest parentheses around assignment used as truth value");
8105 TREE_NO_WARNING (rhs) = 1;
8108 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8109 complain, flags);
8112 /* Convert RHS to be of type TYPE.
8113 If EXP is nonzero, it is the target of the initialization.
8114 ERRTYPE indicates what kind of error the implicit conversion is.
8116 Two major differences between the behavior of
8117 `convert_for_assignment' and `convert_for_initialization'
8118 are that references are bashed in the former, while
8119 copied in the latter, and aggregates are assigned in
8120 the former (operator=) while initialized in the
8121 latter (X(X&)).
8123 If using constructor make sure no conversion operator exists, if one does
8124 exist, an ambiguity exists. */
8126 tree
8127 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8128 impl_conv_rhs errtype, tree fndecl, int parmnum,
8129 tsubst_flags_t complain)
8131 enum tree_code codel = TREE_CODE (type);
8132 tree rhstype;
8133 enum tree_code coder;
8135 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8136 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8137 if (TREE_CODE (rhs) == NOP_EXPR
8138 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8139 && codel != REFERENCE_TYPE)
8140 rhs = TREE_OPERAND (rhs, 0);
8142 if (type == error_mark_node
8143 || rhs == error_mark_node
8144 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8145 return error_mark_node;
8147 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8148 && TREE_CODE (type) != ARRAY_TYPE
8149 && (TREE_CODE (type) != REFERENCE_TYPE
8150 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8151 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8152 && !TYPE_REFFN_P (type))
8153 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8154 rhs = decay_conversion (rhs, complain);
8156 rhstype = TREE_TYPE (rhs);
8157 coder = TREE_CODE (rhstype);
8159 if (coder == ERROR_MARK)
8160 return error_mark_node;
8162 /* We accept references to incomplete types, so we can
8163 return here before checking if RHS is of complete type. */
8165 if (codel == REFERENCE_TYPE)
8167 /* This should eventually happen in convert_arguments. */
8168 int savew = 0, savee = 0;
8170 if (fndecl)
8171 savew = warningcount + werrorcount, savee = errorcount;
8172 rhs = initialize_reference (type, rhs, flags, complain);
8174 if (fndecl
8175 && (warningcount + werrorcount > savew || errorcount > savee))
8176 inform (input_location,
8177 "in passing argument %P of %q+D", parmnum, fndecl);
8179 return rhs;
8182 if (exp != 0)
8183 exp = require_complete_type_sfinae (exp, complain);
8184 if (exp == error_mark_node)
8185 return error_mark_node;
8187 rhstype = non_reference (rhstype);
8189 type = complete_type (type);
8191 if (DIRECT_INIT_EXPR_P (type, rhs))
8192 /* Don't try to do copy-initialization if we already have
8193 direct-initialization. */
8194 return rhs;
8196 if (MAYBE_CLASS_TYPE_P (type))
8197 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8199 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8200 complain, flags);
8203 /* If RETVAL is the address of, or a reference to, a local variable or
8204 temporary give an appropriate warning. */
8206 static void
8207 maybe_warn_about_returning_address_of_local (tree retval)
8209 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8210 tree whats_returned = retval;
8212 for (;;)
8214 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8215 whats_returned = TREE_OPERAND (whats_returned, 1);
8216 else if (CONVERT_EXPR_P (whats_returned)
8217 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8218 whats_returned = TREE_OPERAND (whats_returned, 0);
8219 else
8220 break;
8223 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8224 return;
8225 whats_returned = TREE_OPERAND (whats_returned, 0);
8227 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8229 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8230 || TREE_CODE (whats_returned) == TARGET_EXPR)
8232 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8233 return;
8235 if (VAR_P (whats_returned)
8236 && DECL_NAME (whats_returned)
8237 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8239 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8240 return;
8244 while (TREE_CODE (whats_returned) == COMPONENT_REF
8245 || TREE_CODE (whats_returned) == ARRAY_REF)
8246 whats_returned = TREE_OPERAND (whats_returned, 0);
8248 if (DECL_P (whats_returned)
8249 && DECL_NAME (whats_returned)
8250 && DECL_FUNCTION_SCOPE_P (whats_returned)
8251 && !is_capture_proxy (whats_returned)
8252 && !(TREE_STATIC (whats_returned)
8253 || TREE_PUBLIC (whats_returned)))
8255 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8256 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8257 whats_returned);
8258 else
8259 warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
8260 whats_returned);
8261 return;
8265 /* Check that returning RETVAL from the current function is valid.
8266 Return an expression explicitly showing all conversions required to
8267 change RETVAL into the function return type, and to assign it to
8268 the DECL_RESULT for the function. Set *NO_WARNING to true if
8269 code reaches end of non-void function warning shouldn't be issued
8270 on this RETURN_EXPR. */
8272 tree
8273 check_return_expr (tree retval, bool *no_warning)
8275 tree result;
8276 /* The type actually returned by the function. */
8277 tree valtype;
8278 /* The type the function is declared to return, or void if
8279 the declared type is incomplete. */
8280 tree functype;
8281 int fn_returns_value_p;
8282 bool named_return_value_okay_p;
8284 *no_warning = false;
8286 /* A `volatile' function is one that isn't supposed to return, ever.
8287 (This is a G++ extension, used to get better code for functions
8288 that call the `volatile' function.) */
8289 if (TREE_THIS_VOLATILE (current_function_decl))
8290 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8292 /* Check for various simple errors. */
8293 if (DECL_DESTRUCTOR_P (current_function_decl))
8295 if (retval)
8296 error ("returning a value from a destructor");
8297 return NULL_TREE;
8299 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8301 if (in_function_try_handler)
8302 /* If a return statement appears in a handler of the
8303 function-try-block of a constructor, the program is ill-formed. */
8304 error ("cannot return from a handler of a function-try-block of a constructor");
8305 else if (retval)
8306 /* You can't return a value from a constructor. */
8307 error ("returning a value from a constructor");
8308 return NULL_TREE;
8311 if (processing_template_decl)
8313 current_function_returns_value = 1;
8314 if (check_for_bare_parameter_packs (retval))
8315 retval = error_mark_node;
8316 return retval;
8319 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8321 /* Deduce auto return type from a return statement. */
8322 if (current_function_auto_return_pattern)
8324 tree auto_node;
8325 tree type;
8327 if (!retval && !is_auto (current_function_auto_return_pattern))
8329 /* Give a helpful error message. */
8330 error ("return-statement with no value, in function returning %qT",
8331 current_function_auto_return_pattern);
8332 inform (input_location, "only plain %<auto%> return type can be "
8333 "deduced to %<void%>");
8334 type = error_mark_node;
8336 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8338 error ("returning initializer list");
8339 type = error_mark_node;
8341 else
8343 if (!retval)
8344 retval = void_zero_node;
8345 auto_node = type_uses_auto (current_function_auto_return_pattern);
8346 type = do_auto_deduction (current_function_auto_return_pattern,
8347 retval, auto_node);
8350 if (type == error_mark_node)
8351 /* Leave it. */;
8352 else if (functype == current_function_auto_return_pattern)
8353 apply_deduced_return_type (current_function_decl, type);
8354 else
8355 /* A mismatch should have been diagnosed in do_auto_deduction. */
8356 gcc_assert (same_type_p (type, functype));
8357 functype = type;
8360 /* When no explicit return-value is given in a function with a named
8361 return value, the named return value is used. */
8362 result = DECL_RESULT (current_function_decl);
8363 valtype = TREE_TYPE (result);
8364 gcc_assert (valtype != NULL_TREE);
8365 fn_returns_value_p = !VOID_TYPE_P (valtype);
8366 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8367 retval = result;
8369 /* Check for a return statement with no return value in a function
8370 that's supposed to return a value. */
8371 if (!retval && fn_returns_value_p)
8373 if (functype != error_mark_node)
8374 permerror (input_location, "return-statement with no value, in "
8375 "function returning %qT", valtype);
8376 /* Remember that this function did return. */
8377 current_function_returns_value = 1;
8378 /* And signal caller that TREE_NO_WARNING should be set on the
8379 RETURN_EXPR to avoid control reaches end of non-void function
8380 warnings in tree-cfg.c. */
8381 *no_warning = true;
8383 /* Check for a return statement with a value in a function that
8384 isn't supposed to return a value. */
8385 else if (retval && !fn_returns_value_p)
8387 if (VOID_TYPE_P (TREE_TYPE (retval)))
8388 /* You can return a `void' value from a function of `void'
8389 type. In that case, we have to evaluate the expression for
8390 its side-effects. */
8391 finish_expr_stmt (retval);
8392 else
8393 permerror (input_location, "return-statement with a value, in function "
8394 "returning 'void'");
8395 current_function_returns_null = 1;
8397 /* There's really no value to return, after all. */
8398 return NULL_TREE;
8400 else if (!retval)
8401 /* Remember that this function can sometimes return without a
8402 value. */
8403 current_function_returns_null = 1;
8404 else
8405 /* Remember that this function did return a value. */
8406 current_function_returns_value = 1;
8408 /* Check for erroneous operands -- but after giving ourselves a
8409 chance to provide an error about returning a value from a void
8410 function. */
8411 if (error_operand_p (retval))
8413 current_function_return_value = error_mark_node;
8414 return error_mark_node;
8417 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8418 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8419 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8420 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8421 && ! flag_check_new
8422 && retval && null_ptr_cst_p (retval))
8423 warning (0, "%<operator new%> must not return NULL unless it is "
8424 "declared %<throw()%> (or -fcheck-new is in effect)");
8426 /* Effective C++ rule 15. See also start_function. */
8427 if (warn_ecpp
8428 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8430 bool warn = true;
8432 /* The function return type must be a reference to the current
8433 class. */
8434 if (TREE_CODE (valtype) == REFERENCE_TYPE
8435 && same_type_ignoring_top_level_qualifiers_p
8436 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8438 /* Returning '*this' is obviously OK. */
8439 if (retval == current_class_ref)
8440 warn = false;
8441 /* If we are calling a function whose return type is the same of
8442 the current class reference, it is ok. */
8443 else if (INDIRECT_REF_P (retval)
8444 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8445 warn = false;
8448 if (warn)
8449 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8452 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8454 [...] For a function with a class return type, if the expression
8455 in the return statement is the name of a local object, and the cv-
8456 unqualified type of the local object is the same as the function
8457 return type, an implementation is permitted to omit creating the tem-
8458 porary object to hold the function return value [...]
8460 So, if this is a value-returning function that always returns the same
8461 local variable, remember it.
8463 It might be nice to be more flexible, and choose the first suitable
8464 variable even if the function sometimes returns something else, but
8465 then we run the risk of clobbering the variable we chose if the other
8466 returned expression uses the chosen variable somehow. And people expect
8467 this restriction, anyway. (jason 2000-11-19)
8469 See finish_function and finalize_nrv for the rest of this optimization. */
8471 named_return_value_okay_p =
8472 (retval != NULL_TREE
8473 /* Must be a local, automatic variable. */
8474 && VAR_P (retval)
8475 && DECL_CONTEXT (retval) == current_function_decl
8476 && ! TREE_STATIC (retval)
8477 /* And not a lambda or anonymous union proxy. */
8478 && !DECL_HAS_VALUE_EXPR_P (retval)
8479 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8480 /* The cv-unqualified type of the returned value must be the
8481 same as the cv-unqualified return type of the
8482 function. */
8483 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8484 (TYPE_MAIN_VARIANT (functype)))
8485 /* And the returned value must be non-volatile. */
8486 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8488 if (fn_returns_value_p && flag_elide_constructors)
8490 if (named_return_value_okay_p
8491 && (current_function_return_value == NULL_TREE
8492 || current_function_return_value == retval))
8493 current_function_return_value = retval;
8494 else
8495 current_function_return_value = error_mark_node;
8498 /* We don't need to do any conversions when there's nothing being
8499 returned. */
8500 if (!retval)
8501 return NULL_TREE;
8503 /* Do any required conversions. */
8504 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8505 /* No conversions are required. */
8507 else
8509 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8511 /* The functype's return type will have been set to void, if it
8512 was an incomplete type. Just treat this as 'return;' */
8513 if (VOID_TYPE_P (functype))
8514 return error_mark_node;
8516 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8517 treated as an rvalue for the purposes of overload resolution to
8518 favor move constructors over copy constructors.
8520 Note that these conditions are similar to, but not as strict as,
8521 the conditions for the named return value optimization. */
8522 if ((cxx_dialect != cxx98)
8523 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8524 || TREE_CODE (retval) == PARM_DECL)
8525 && DECL_CONTEXT (retval) == current_function_decl
8526 && !TREE_STATIC (retval)
8527 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8528 (TYPE_MAIN_VARIANT (functype)))
8529 /* This is only interesting for class type. */
8530 && CLASS_TYPE_P (functype))
8531 flags = flags | LOOKUP_PREFER_RVALUE;
8533 /* First convert the value to the function's return type, then
8534 to the type of return value's location to handle the
8535 case that functype is smaller than the valtype. */
8536 retval = convert_for_initialization
8537 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8538 tf_warning_or_error);
8539 retval = convert (valtype, retval);
8541 /* If the conversion failed, treat this just like `return;'. */
8542 if (retval == error_mark_node)
8543 return retval;
8544 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8545 else if (! cfun->returns_struct
8546 && TREE_CODE (retval) == TARGET_EXPR
8547 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8548 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8549 TREE_OPERAND (retval, 0));
8550 else
8551 maybe_warn_about_returning_address_of_local (retval);
8554 /* Actually copy the value returned into the appropriate location. */
8555 if (retval && retval != result)
8556 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8558 return retval;
8562 /* Returns nonzero if the pointer-type FROM can be converted to the
8563 pointer-type TO via a qualification conversion. If CONSTP is -1,
8564 then we return nonzero if the pointers are similar, and the
8565 cv-qualification signature of FROM is a proper subset of that of TO.
8567 If CONSTP is positive, then all outer pointers have been
8568 const-qualified. */
8570 static int
8571 comp_ptr_ttypes_real (tree to, tree from, int constp)
8573 bool to_more_cv_qualified = false;
8574 bool is_opaque_pointer = false;
8576 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8578 if (TREE_CODE (to) != TREE_CODE (from))
8579 return 0;
8581 if (TREE_CODE (from) == OFFSET_TYPE
8582 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8583 TYPE_OFFSET_BASETYPE (to)))
8584 return 0;
8586 /* Const and volatile mean something different for function types,
8587 so the usual checks are not appropriate. */
8588 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8590 if (!at_least_as_qualified_p (to, from))
8591 return 0;
8593 if (!at_least_as_qualified_p (from, to))
8595 if (constp == 0)
8596 return 0;
8597 to_more_cv_qualified = true;
8600 if (constp > 0)
8601 constp &= TYPE_READONLY (to);
8604 if (TREE_CODE (to) == VECTOR_TYPE)
8605 is_opaque_pointer = vector_targets_convertible_p (to, from);
8607 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8608 return ((constp >= 0 || to_more_cv_qualified)
8609 && (is_opaque_pointer
8610 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8614 /* When comparing, say, char ** to char const **, this function takes
8615 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8616 types to this function. */
8619 comp_ptr_ttypes (tree to, tree from)
8621 return comp_ptr_ttypes_real (to, from, 1);
8624 /* Returns true iff FNTYPE is a non-class type that involves
8625 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8626 if a parameter type is ill-formed. */
8628 bool
8629 error_type_p (const_tree type)
8631 tree t;
8633 switch (TREE_CODE (type))
8635 case ERROR_MARK:
8636 return true;
8638 case POINTER_TYPE:
8639 case REFERENCE_TYPE:
8640 case OFFSET_TYPE:
8641 return error_type_p (TREE_TYPE (type));
8643 case FUNCTION_TYPE:
8644 case METHOD_TYPE:
8645 if (error_type_p (TREE_TYPE (type)))
8646 return true;
8647 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8648 if (error_type_p (TREE_VALUE (t)))
8649 return true;
8650 return false;
8652 case RECORD_TYPE:
8653 if (TYPE_PTRMEMFUNC_P (type))
8654 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8655 return false;
8657 default:
8658 return false;
8662 /* Returns true if to and from are (possibly multi-level) pointers to the same
8663 type or inheritance-related types, regardless of cv-quals. */
8665 bool
8666 ptr_reasonably_similar (const_tree to, const_tree from)
8668 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8670 /* Any target type is similar enough to void. */
8671 if (VOID_TYPE_P (to))
8672 return !error_type_p (from);
8673 if (VOID_TYPE_P (from))
8674 return !error_type_p (to);
8676 if (TREE_CODE (to) != TREE_CODE (from))
8677 return false;
8679 if (TREE_CODE (from) == OFFSET_TYPE
8680 && comptypes (TYPE_OFFSET_BASETYPE (to),
8681 TYPE_OFFSET_BASETYPE (from),
8682 COMPARE_BASE | COMPARE_DERIVED))
8683 continue;
8685 if (TREE_CODE (to) == VECTOR_TYPE
8686 && vector_types_convertible_p (to, from, false))
8687 return true;
8689 if (TREE_CODE (to) == INTEGER_TYPE
8690 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8691 return true;
8693 if (TREE_CODE (to) == FUNCTION_TYPE)
8694 return !error_type_p (to) && !error_type_p (from);
8696 if (!TYPE_PTR_P (to))
8698 /* When either type is incomplete avoid DERIVED_FROM_P,
8699 which may call complete_type (c++/57942). */
8700 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8701 return comptypes
8702 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8703 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8708 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8709 pointer-to-member types) are the same, ignoring cv-qualification at
8710 all levels. */
8712 bool
8713 comp_ptr_ttypes_const (tree to, tree from)
8715 bool is_opaque_pointer = false;
8717 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8719 if (TREE_CODE (to) != TREE_CODE (from))
8720 return false;
8722 if (TREE_CODE (from) == OFFSET_TYPE
8723 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8724 TYPE_OFFSET_BASETYPE (to)))
8725 continue;
8727 if (TREE_CODE (to) == VECTOR_TYPE)
8728 is_opaque_pointer = vector_targets_convertible_p (to, from);
8730 if (!TYPE_PTR_P (to))
8731 return (is_opaque_pointer
8732 || same_type_ignoring_top_level_qualifiers_p (to, from));
8736 /* Returns the type qualifiers for this type, including the qualifiers on the
8737 elements for an array type. */
8740 cp_type_quals (const_tree type)
8742 int quals;
8743 /* This CONST_CAST is okay because strip_array_types returns its
8744 argument unmodified and we assign it to a const_tree. */
8745 type = strip_array_types (CONST_CAST_TREE (type));
8746 if (type == error_mark_node
8747 /* Quals on a FUNCTION_TYPE are memfn quals. */
8748 || TREE_CODE (type) == FUNCTION_TYPE)
8749 return TYPE_UNQUALIFIED;
8750 quals = TYPE_QUALS (type);
8751 /* METHOD and REFERENCE_TYPEs should never have quals. */
8752 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8753 && TREE_CODE (type) != REFERENCE_TYPE)
8754 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8755 == TYPE_UNQUALIFIED));
8756 return quals;
8759 /* Returns the function-ref-qualifier for TYPE */
8761 cp_ref_qualifier
8762 type_memfn_rqual (const_tree type)
8764 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8765 || TREE_CODE (type) == METHOD_TYPE);
8767 if (!FUNCTION_REF_QUALIFIED (type))
8768 return REF_QUAL_NONE;
8769 else if (FUNCTION_RVALUE_QUALIFIED (type))
8770 return REF_QUAL_RVALUE;
8771 else
8772 return REF_QUAL_LVALUE;
8775 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8776 METHOD_TYPE. */
8779 type_memfn_quals (const_tree type)
8781 if (TREE_CODE (type) == FUNCTION_TYPE)
8782 return TYPE_QUALS (type);
8783 else if (TREE_CODE (type) == METHOD_TYPE)
8784 return cp_type_quals (class_of_this_parm (type));
8785 else
8786 gcc_unreachable ();
8789 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8790 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8792 tree
8793 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8795 /* Could handle METHOD_TYPE here if necessary. */
8796 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8797 if (TYPE_QUALS (type) == memfn_quals
8798 && type_memfn_rqual (type) == rqual)
8799 return type;
8801 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8802 complex. */
8803 tree result = build_qualified_type (type, memfn_quals);
8804 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8805 return build_ref_qualified_type (result, rqual);
8808 /* Returns nonzero if TYPE is const or volatile. */
8810 bool
8811 cv_qualified_p (const_tree type)
8813 int quals = cp_type_quals (type);
8814 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8817 /* Returns nonzero if the TYPE contains a mutable member. */
8819 bool
8820 cp_has_mutable_p (const_tree type)
8822 /* This CONST_CAST is okay because strip_array_types returns its
8823 argument unmodified and we assign it to a const_tree. */
8824 type = strip_array_types (CONST_CAST_TREE(type));
8826 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8829 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8830 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8831 approximation. In particular, consider:
8833 int f();
8834 struct S { int i; };
8835 const S s = { f(); }
8837 Here, we will make "s" as TREE_READONLY (because it is declared
8838 "const") -- only to reverse ourselves upon seeing that the
8839 initializer is non-constant. */
8841 void
8842 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8844 tree type = TREE_TYPE (decl);
8846 if (type == error_mark_node)
8847 return;
8849 if (TREE_CODE (decl) == TYPE_DECL)
8850 return;
8852 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8853 && type_quals != TYPE_UNQUALIFIED));
8855 /* Avoid setting TREE_READONLY incorrectly. */
8856 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8857 constructor can produce constant init, so rely on cp_finish_decl to
8858 clear TREE_READONLY if the variable has non-constant init. */
8860 /* If the type has (or might have) a mutable component, that component
8861 might be modified. */
8862 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8863 type_quals &= ~TYPE_QUAL_CONST;
8865 c_apply_type_quals_to_decl (type_quals, decl);
8868 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8869 exemplar types such that casting T1 to T2 is casting away constness
8870 if and only if there is no implicit conversion from T1 to T2. */
8872 static void
8873 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8875 int quals1;
8876 int quals2;
8878 /* [expr.const.cast]
8880 For multi-level pointer to members and multi-level mixed pointers
8881 and pointers to members (conv.qual), the "member" aspect of a
8882 pointer to member level is ignored when determining if a const
8883 cv-qualifier has been cast away. */
8884 /* [expr.const.cast]
8886 For two pointer types:
8888 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8889 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8890 K is min(N,M)
8892 casting from X1 to X2 casts away constness if, for a non-pointer
8893 type T there does not exist an implicit conversion (clause
8894 _conv_) from:
8896 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8900 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8901 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8902 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8904 *t1 = cp_build_qualified_type (void_type_node,
8905 cp_type_quals (*t1));
8906 *t2 = cp_build_qualified_type (void_type_node,
8907 cp_type_quals (*t2));
8908 return;
8911 quals1 = cp_type_quals (*t1);
8912 quals2 = cp_type_quals (*t2);
8914 if (TYPE_PTRDATAMEM_P (*t1))
8915 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8916 else
8917 *t1 = TREE_TYPE (*t1);
8918 if (TYPE_PTRDATAMEM_P (*t2))
8919 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8920 else
8921 *t2 = TREE_TYPE (*t2);
8923 casts_away_constness_r (t1, t2, complain);
8924 *t1 = build_pointer_type (*t1);
8925 *t2 = build_pointer_type (*t2);
8926 *t1 = cp_build_qualified_type (*t1, quals1);
8927 *t2 = cp_build_qualified_type (*t2, quals2);
8930 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8931 constness.
8933 ??? This function returns non-zero if casting away qualifiers not
8934 just const. We would like to return to the caller exactly which
8935 qualifiers are casted away to give more accurate diagnostics.
8938 static bool
8939 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8941 if (TREE_CODE (t2) == REFERENCE_TYPE)
8943 /* [expr.const.cast]
8945 Casting from an lvalue of type T1 to an lvalue of type T2
8946 using a reference cast casts away constness if a cast from an
8947 rvalue of type "pointer to T1" to the type "pointer to T2"
8948 casts away constness. */
8949 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8950 return casts_away_constness (build_pointer_type (t1),
8951 build_pointer_type (TREE_TYPE (t2)),
8952 complain);
8955 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
8956 /* [expr.const.cast]
8958 Casting from an rvalue of type "pointer to data member of X
8959 of type T1" to the type "pointer to data member of Y of type
8960 T2" casts away constness if a cast from an rvalue of type
8961 "pointer to T1" to the type "pointer to T2" casts away
8962 constness. */
8963 return casts_away_constness
8964 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8965 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
8966 complain);
8968 /* Casting away constness is only something that makes sense for
8969 pointer or reference types. */
8970 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
8971 return false;
8973 /* Top-level qualifiers don't matter. */
8974 t1 = TYPE_MAIN_VARIANT (t1);
8975 t2 = TYPE_MAIN_VARIANT (t2);
8976 casts_away_constness_r (&t1, &t2, complain);
8977 if (!can_convert (t2, t1, complain))
8978 return true;
8980 return false;
8983 /* If T is a REFERENCE_TYPE return the type to which T refers.
8984 Otherwise, return T itself. */
8986 tree
8987 non_reference (tree t)
8989 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8990 t = TREE_TYPE (t);
8991 return t;
8995 /* Return nonzero if REF is an lvalue valid for this language;
8996 otherwise, print an error message and return zero. USE says
8997 how the lvalue is being used and so selects the error message. */
9000 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9002 cp_lvalue_kind kind = lvalue_kind (ref);
9004 if (kind == clk_none)
9006 if (complain & tf_error)
9007 lvalue_error (input_location, use);
9008 return 0;
9010 else if (kind & (clk_rvalueref|clk_class))
9012 if (!(complain & tf_error))
9013 return 0;
9014 if (kind & clk_class)
9015 /* Make this a permerror because we used to accept it. */
9016 permerror (input_location, "using temporary as lvalue");
9017 else
9018 error ("using xvalue (rvalue reference) as lvalue");
9020 return 1;
9023 /* Return true if a user-defined literal operator is a raw operator. */
9025 bool
9026 check_raw_literal_operator (const_tree decl)
9028 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9029 tree argtype;
9030 int arity;
9031 bool maybe_raw_p = false;
9033 /* Count the number and type of arguments and check for ellipsis. */
9034 for (argtype = argtypes, arity = 0;
9035 argtype && argtype != void_list_node;
9036 ++arity, argtype = TREE_CHAIN (argtype))
9038 tree t = TREE_VALUE (argtype);
9040 if (same_type_p (t, const_string_type_node))
9041 maybe_raw_p = true;
9043 if (!argtype)
9044 return false; /* Found ellipsis. */
9046 if (!maybe_raw_p || arity != 1)
9047 return false;
9049 return true;
9053 /* Return true if a user-defined literal operator has one of the allowed
9054 argument types. */
9056 bool
9057 check_literal_operator_args (const_tree decl,
9058 bool *long_long_unsigned_p, bool *long_double_p)
9060 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9062 *long_long_unsigned_p = false;
9063 *long_double_p = false;
9064 if (processing_template_decl || processing_specialization)
9065 return argtypes == void_list_node;
9066 else
9068 tree argtype;
9069 int arity;
9070 int max_arity = 2;
9072 /* Count the number and type of arguments and check for ellipsis. */
9073 for (argtype = argtypes, arity = 0;
9074 argtype && argtype != void_list_node;
9075 argtype = TREE_CHAIN (argtype))
9077 tree t = TREE_VALUE (argtype);
9078 ++arity;
9080 if (TYPE_PTR_P (t))
9082 bool maybe_raw_p = false;
9083 t = TREE_TYPE (t);
9084 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9085 return false;
9086 t = TYPE_MAIN_VARIANT (t);
9087 if ((maybe_raw_p = same_type_p (t, char_type_node))
9088 || same_type_p (t, wchar_type_node)
9089 || same_type_p (t, char16_type_node)
9090 || same_type_p (t, char32_type_node))
9092 argtype = TREE_CHAIN (argtype);
9093 if (!argtype)
9094 return false;
9095 t = TREE_VALUE (argtype);
9096 if (maybe_raw_p && argtype == void_list_node)
9097 return true;
9098 else if (same_type_p (t, size_type_node))
9100 ++arity;
9101 continue;
9103 else
9104 return false;
9107 else if (same_type_p (t, long_long_unsigned_type_node))
9109 max_arity = 1;
9110 *long_long_unsigned_p = true;
9112 else if (same_type_p (t, long_double_type_node))
9114 max_arity = 1;
9115 *long_double_p = true;
9117 else if (same_type_p (t, char_type_node))
9118 max_arity = 1;
9119 else if (same_type_p (t, wchar_type_node))
9120 max_arity = 1;
9121 else if (same_type_p (t, char16_type_node))
9122 max_arity = 1;
9123 else if (same_type_p (t, char32_type_node))
9124 max_arity = 1;
9125 else
9126 return false;
9128 if (!argtype)
9129 return false; /* Found ellipsis. */
9131 if (arity != max_arity)
9132 return false;
9134 return true;