Merge from trunk
[official-gcc.git] / gcc / cp / typeck.c
blob75664594850d4f1fe9e2ae9180ab6763ebd16f11
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2014 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 "stor-layout.h"
33 #include "varasm.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "diagnostic.h"
37 #include "intl.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "c-family/c-ubsan.h"
43 #include "params.h"
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
48 tsubst_flags_t, int);
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
50 static tree rationalize_conditional_expr (enum tree_code, tree,
51 tsubst_flags_t);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
58 static bool casts_away_constness (tree, tree, tsubst_flags_t);
59 static void maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
63 tsubst_flags_t);
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
70 tree
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
73 tree type;
75 if (processing_template_decl || value == error_mark_node)
76 return value;
78 if (TREE_CODE (value) == OVERLOAD)
79 type = unknown_type_node;
80 else
81 type = TREE_TYPE (value);
83 if (type == error_mark_node)
84 return error_mark_node;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type))
88 return value;
90 if (complete_type_or_maybe_complain (type, value, complain))
91 return value;
92 else
93 return error_mark_node;
96 tree
97 require_complete_type (tree value)
99 return require_complete_type_sfinae (value, tf_warning_or_error);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
107 tree
108 complete_type (tree type)
110 if (type == NULL_TREE)
111 /* Rather than crash, we return something sure to cause an error
112 at some point. */
113 return error_mark_node;
115 if (type == error_mark_node || COMPLETE_TYPE_P (type))
117 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
119 tree t = complete_type (TREE_TYPE (type));
120 unsigned int needs_constructing, has_nontrivial_dtor;
121 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
122 layout_type (type);
123 needs_constructing
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
125 has_nontrivial_dtor
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
129 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
136 return type;
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
143 tree
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
146 type = complete_type (type);
147 if (type == error_mark_node)
148 /* We already issued an error. */
149 return NULL_TREE;
150 else if (!COMPLETE_TYPE_P (type))
152 if (complain & tf_error)
153 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
154 return NULL_TREE;
156 else
157 return type;
160 tree
161 complete_type_or_else (tree type, tree value)
163 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
166 /* Return truthvalue of whether type of EXP is instantiated. */
169 type_unknown_p (const_tree exp)
171 return (TREE_CODE (exp) == TREE_LIST
172 || TREE_TYPE (exp) == unknown_type_node);
176 /* Return the common type of two parameter lists.
177 We assume that comptypes has already been done and returned 1;
178 if that isn't so, this may crash.
180 As an optimization, free the space we allocate if the parameter
181 lists are already common. */
183 static tree
184 commonparms (tree p1, tree p2)
186 tree oldargs = p1, newargs, n;
187 int i, len;
188 int any_change = 0;
190 len = list_length (p1);
191 newargs = tree_last (p1);
193 if (newargs == void_list_node)
194 i = 1;
195 else
197 i = 0;
198 newargs = 0;
201 for (; i < len; i++)
202 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
204 n = newargs;
206 for (i = 0; p1;
207 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
209 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
212 any_change = 1;
214 else if (! TREE_PURPOSE (p1))
216 if (TREE_PURPOSE (p2))
218 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219 any_change = 1;
222 else
224 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
225 any_change = 1;
226 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
228 if (TREE_VALUE (p1) != TREE_VALUE (p2))
230 any_change = 1;
231 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
233 else
234 TREE_VALUE (n) = TREE_VALUE (p1);
236 if (! any_change)
237 return oldargs;
239 return newargs;
242 /* Given a type, perhaps copied for a typedef,
243 find the "original" version of it. */
244 static tree
245 original_type (tree t)
247 int quals = cp_type_quals (t);
248 while (t != error_mark_node
249 && TYPE_NAME (t) != NULL_TREE)
251 tree x = TYPE_NAME (t);
252 if (TREE_CODE (x) != TYPE_DECL)
253 break;
254 x = DECL_ORIGINAL_TYPE (x);
255 if (x == NULL_TREE)
256 break;
257 t = x;
259 return cp_build_qualified_type (t, quals);
262 /* Return the common type for two arithmetic types T1 and T2 under the
263 usual arithmetic conversions. The default conversions have already
264 been applied, and enumerated types converted to their compatible
265 integer types. */
267 static tree
268 cp_common_type (tree t1, tree t2)
270 enum tree_code code1 = TREE_CODE (t1);
271 enum tree_code code2 = TREE_CODE (t2);
272 tree attributes;
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
277 attributes. */
278 attributes = (*targetm.merge_type_attributes) (t1, t2);
280 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
282 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283 return build_type_attribute_variant (t1, attributes);
284 else
285 return NULL_TREE;
288 /* FIXME: Attributes. */
289 gcc_assert (ARITHMETIC_TYPE_P (t1)
290 || TREE_CODE (t1) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t1));
292 gcc_assert (ARITHMETIC_TYPE_P (t2)
293 || TREE_CODE (t2) == VECTOR_TYPE
294 || UNSCOPED_ENUM_P (t2));
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
298 required type. */
299 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
301 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
303 tree subtype
304 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
306 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307 return build_type_attribute_variant (t1, attributes);
308 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309 return build_type_attribute_variant (t2, attributes);
310 else
311 return build_type_attribute_variant (build_complex_type (subtype),
312 attributes);
315 if (code1 == VECTOR_TYPE)
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1))
320 return build_type_attribute_variant (t1, attributes);
321 else
322 return build_type_attribute_variant (t2, attributes);
325 /* If only one is real, use it as the result. */
326 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327 return build_type_attribute_variant (t1, attributes);
328 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329 return build_type_attribute_variant (t2, attributes);
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333 return build_type_attribute_variant (t1, attributes);
334 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335 return build_type_attribute_variant (t2, attributes);
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339 return build_type_attribute_variant (t1, attributes);
341 if (code1 != REAL_TYPE)
343 /* If one is unsigned long long, then convert the other to unsigned
344 long long. */
345 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347 return build_type_attribute_variant (long_long_unsigned_type_node,
348 attributes);
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
353 other to long long.
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
358 case. */
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
362 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363 ? long_long_unsigned_type_node
364 : long_long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
367 if (int128_integer_type_node != NULL_TREE
368 && (same_type_p (TYPE_MAIN_VARIANT (t1),
369 int128_integer_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2),
371 int128_integer_type_node)))
373 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
374 ? int128_unsigned_type_node
375 : int128_integer_type_node);
376 return build_type_attribute_variant (t, attributes);
379 /* Go through the same procedure, but for longs. */
380 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
381 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
382 return build_type_attribute_variant (long_unsigned_type_node,
383 attributes);
384 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? long_unsigned_type_node : long_integer_type_node);
389 return build_type_attribute_variant (t, attributes);
391 /* Otherwise prefer the unsigned one. */
392 if (TYPE_UNSIGNED (t1))
393 return build_type_attribute_variant (t1, attributes);
394 else
395 return build_type_attribute_variant (t2, attributes);
397 else
399 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
400 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
401 return build_type_attribute_variant (long_double_type_node,
402 attributes);
403 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
405 return build_type_attribute_variant (double_type_node,
406 attributes);
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
409 return build_type_attribute_variant (float_type_node,
410 attributes);
412 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
413 the standard C++ floating-point types. Logic earlier in this
414 function has already eliminated the possibility that
415 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
416 compelling reason to choose one or the other. */
417 return build_type_attribute_variant (t1, attributes);
421 /* T1 and T2 are arithmetic or enumeration types. Return the type
422 that will result from the "usual arithmetic conversions" on T1 and
423 T2 as described in [expr]. */
425 tree
426 type_after_usual_arithmetic_conversions (tree t1, tree t2)
428 gcc_assert (ARITHMETIC_TYPE_P (t1)
429 || TREE_CODE (t1) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t1));
431 gcc_assert (ARITHMETIC_TYPE_P (t2)
432 || TREE_CODE (t2) == VECTOR_TYPE
433 || UNSCOPED_ENUM_P (t2));
435 /* Perform the integral promotions. We do not promote real types here. */
436 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
437 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
439 t1 = type_promotes_to (t1);
440 t2 = type_promotes_to (t2);
443 return cp_common_type (t1, t2);
446 static void
447 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
448 composite_pointer_operation operation)
450 switch (operation)
452 case CPO_COMPARISON:
453 emit_diagnostic (kind, input_location, 0,
454 "comparison between "
455 "distinct pointer types %qT and %qT lacks a cast",
456 t1, t2);
457 break;
458 case CPO_CONVERSION:
459 emit_diagnostic (kind, input_location, 0,
460 "conversion between "
461 "distinct pointer types %qT and %qT lacks a cast",
462 t1, t2);
463 break;
464 case CPO_CONDITIONAL_EXPR:
465 emit_diagnostic (kind, input_location, 0,
466 "conditional expression between "
467 "distinct pointer types %qT and %qT lacks a cast",
468 t1, t2);
469 break;
470 default:
471 gcc_unreachable ();
475 /* Subroutine of composite_pointer_type to implement the recursive
476 case. See that function for documentation of the parameters. */
478 static tree
479 composite_pointer_type_r (tree t1, tree t2,
480 composite_pointer_operation operation,
481 tsubst_flags_t complain)
483 tree pointee1;
484 tree pointee2;
485 tree result_type;
486 tree attributes;
488 /* Determine the types pointed to by T1 and T2. */
489 if (TYPE_PTR_P (t1))
491 pointee1 = TREE_TYPE (t1);
492 pointee2 = TREE_TYPE (t2);
494 else
496 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
497 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
500 /* [expr.rel]
502 Otherwise, the composite pointer type is a pointer type
503 similar (_conv.qual_) to the type of one of the operands,
504 with a cv-qualification signature (_conv.qual_) that is the
505 union of the cv-qualification signatures of the operand
506 types. */
507 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
508 result_type = pointee1;
509 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
510 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
512 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
513 complain);
514 if (result_type == error_mark_node)
515 return error_mark_node;
517 else
519 if (complain & tf_error)
520 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
521 else
522 return error_mark_node;
523 result_type = void_type_node;
525 result_type = cp_build_qualified_type (result_type,
526 (cp_type_quals (pointee1)
527 | cp_type_quals (pointee2)));
528 /* If the original types were pointers to members, so is the
529 result. */
530 if (TYPE_PTRMEM_P (t1))
532 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
533 TYPE_PTRMEM_CLASS_TYPE (t2)))
535 if (complain & tf_error)
536 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
537 else
538 return error_mark_node;
540 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
541 result_type);
543 else
544 result_type = build_pointer_type (result_type);
546 /* Merge the attributes. */
547 attributes = (*targetm.merge_type_attributes) (t1, t2);
548 return build_type_attribute_variant (result_type, attributes);
551 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
552 ARG1 and ARG2 are the values with those types. The OPERATION is to
553 describe the operation between the pointer types,
554 in case an error occurs.
556 This routine also implements the computation of a common type for
557 pointers-to-members as per [expr.eq]. */
559 tree
560 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
561 composite_pointer_operation operation,
562 tsubst_flags_t complain)
564 tree class1;
565 tree class2;
567 /* [expr.rel]
569 If one operand is a null pointer constant, the composite pointer
570 type is the type of the other operand. */
571 if (null_ptr_cst_p (arg1))
572 return t2;
573 if (null_ptr_cst_p (arg2))
574 return t1;
576 /* We have:
578 [expr.rel]
580 If one of the operands has type "pointer to cv1 void*", then
581 the other has type "pointer to cv2T", and the composite pointer
582 type is "pointer to cv12 void", where cv12 is the union of cv1
583 and cv2.
585 If either type is a pointer to void, make sure it is T1. */
586 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
588 tree t;
589 t = t1;
590 t1 = t2;
591 t2 = t;
594 /* Now, if T1 is a pointer to void, merge the qualifiers. */
595 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
597 tree attributes;
598 tree result_type;
600 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
602 switch (operation)
604 case CPO_COMPARISON:
605 pedwarn (input_location, OPT_Wpedantic,
606 "ISO C++ forbids comparison between "
607 "pointer of type %<void *%> and pointer-to-function");
608 break;
609 case CPO_CONVERSION:
610 pedwarn (input_location, OPT_Wpedantic,
611 "ISO C++ forbids conversion between "
612 "pointer of type %<void *%> and pointer-to-function");
613 break;
614 case CPO_CONDITIONAL_EXPR:
615 pedwarn (input_location, OPT_Wpedantic,
616 "ISO C++ forbids conditional expression between "
617 "pointer of type %<void *%> and pointer-to-function");
618 break;
619 default:
620 gcc_unreachable ();
623 result_type
624 = cp_build_qualified_type (void_type_node,
625 (cp_type_quals (TREE_TYPE (t1))
626 | cp_type_quals (TREE_TYPE (t2))));
627 result_type = build_pointer_type (result_type);
628 /* Merge the attributes. */
629 attributes = (*targetm.merge_type_attributes) (t1, t2);
630 return build_type_attribute_variant (result_type, attributes);
633 if (c_dialect_objc () && TYPE_PTR_P (t1)
634 && TYPE_PTR_P (t2))
636 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
637 return objc_common_type (t1, t2);
640 /* [expr.eq] permits the application of a pointer conversion to
641 bring the pointers to a common type. */
642 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
643 && CLASS_TYPE_P (TREE_TYPE (t1))
644 && CLASS_TYPE_P (TREE_TYPE (t2))
645 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
646 TREE_TYPE (t2)))
648 class1 = TREE_TYPE (t1);
649 class2 = TREE_TYPE (t2);
651 if (DERIVED_FROM_P (class1, class2))
652 t2 = (build_pointer_type
653 (cp_build_qualified_type (class1, cp_type_quals (class2))));
654 else if (DERIVED_FROM_P (class2, class1))
655 t1 = (build_pointer_type
656 (cp_build_qualified_type (class2, cp_type_quals (class1))));
657 else
659 if (complain & tf_error)
660 composite_pointer_error (DK_ERROR, t1, t2, operation);
661 return error_mark_node;
664 /* [expr.eq] permits the application of a pointer-to-member
665 conversion to change the class type of one of the types. */
666 else if (TYPE_PTRMEM_P (t1)
667 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
668 TYPE_PTRMEM_CLASS_TYPE (t2)))
670 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
671 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
673 if (DERIVED_FROM_P (class1, class2))
674 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
675 else if (DERIVED_FROM_P (class2, class1))
676 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
677 else
679 if (complain & tf_error)
680 switch (operation)
682 case CPO_COMPARISON:
683 error ("comparison between distinct "
684 "pointer-to-member types %qT and %qT lacks a cast",
685 t1, t2);
686 break;
687 case CPO_CONVERSION:
688 error ("conversion between distinct "
689 "pointer-to-member types %qT and %qT lacks a cast",
690 t1, t2);
691 break;
692 case CPO_CONDITIONAL_EXPR:
693 error ("conditional expression between distinct "
694 "pointer-to-member types %qT and %qT lacks a cast",
695 t1, t2);
696 break;
697 default:
698 gcc_unreachable ();
700 return error_mark_node;
704 return composite_pointer_type_r (t1, t2, operation, complain);
707 /* Return the merged type of two types.
708 We assume that comptypes has already been done and returned 1;
709 if that isn't so, this may crash.
711 This just combines attributes and default arguments; any other
712 differences would cause the two types to compare unalike. */
714 tree
715 merge_types (tree t1, tree t2)
717 enum tree_code code1;
718 enum tree_code code2;
719 tree attributes;
721 /* Save time if the two types are the same. */
722 if (t1 == t2)
723 return t1;
724 if (original_type (t1) == original_type (t2))
725 return t1;
727 /* If one type is nonsense, use the other. */
728 if (t1 == error_mark_node)
729 return t2;
730 if (t2 == error_mark_node)
731 return t1;
733 /* Handle merging an auto redeclaration with a previous deduced
734 return type. */
735 if (is_auto (t1))
736 return t2;
738 /* Merge the attributes. */
739 attributes = (*targetm.merge_type_attributes) (t1, t2);
741 if (TYPE_PTRMEMFUNC_P (t1))
742 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
743 if (TYPE_PTRMEMFUNC_P (t2))
744 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
746 code1 = TREE_CODE (t1);
747 code2 = TREE_CODE (t2);
748 if (code1 != code2)
750 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
751 if (code1 == TYPENAME_TYPE)
753 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
754 code1 = TREE_CODE (t1);
756 else
758 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
759 code2 = TREE_CODE (t2);
763 switch (code1)
765 case POINTER_TYPE:
766 case REFERENCE_TYPE:
767 /* For two pointers, do this recursively on the target type. */
769 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
770 int quals = cp_type_quals (t1);
772 if (code1 == POINTER_TYPE)
773 t1 = build_pointer_type (target);
774 else
775 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
776 t1 = build_type_attribute_variant (t1, attributes);
777 t1 = cp_build_qualified_type (t1, quals);
779 if (TREE_CODE (target) == METHOD_TYPE)
780 t1 = build_ptrmemfunc_type (t1);
782 return t1;
785 case OFFSET_TYPE:
787 int quals;
788 tree pointee;
789 quals = cp_type_quals (t1);
790 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
791 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
792 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
793 pointee);
794 t1 = cp_build_qualified_type (t1, quals);
795 break;
798 case ARRAY_TYPE:
800 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
801 /* Save space: see if the result is identical to one of the args. */
802 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
803 return build_type_attribute_variant (t1, attributes);
804 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
805 return build_type_attribute_variant (t2, attributes);
806 /* Merge the element types, and have a size if either arg has one. */
807 t1 = build_cplus_array_type
808 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
809 break;
812 case FUNCTION_TYPE:
813 /* Function types: prefer the one that specified arg types.
814 If both do, merge the arg types. Also merge the return types. */
816 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
817 tree p1 = TYPE_ARG_TYPES (t1);
818 tree p2 = TYPE_ARG_TYPES (t2);
819 tree parms;
820 tree rval, raises;
822 /* Save space: see if the result is identical to one of the args. */
823 if (valtype == TREE_TYPE (t1) && ! p2)
824 return cp_build_type_attribute_variant (t1, attributes);
825 if (valtype == TREE_TYPE (t2) && ! p1)
826 return cp_build_type_attribute_variant (t2, attributes);
828 /* Simple way if one arg fails to specify argument types. */
829 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
830 parms = p2;
831 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
832 parms = p1;
833 else
834 parms = commonparms (p1, p2);
836 rval = build_function_type (valtype, parms);
837 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
838 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
839 rval = apply_memfn_quals (rval,
840 type_memfn_quals (t1),
841 type_memfn_rqual (t1));
842 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
843 TYPE_RAISES_EXCEPTIONS (t2),
844 NULL_TREE);
845 t1 = build_exception_variant (rval, raises);
846 break;
849 case METHOD_TYPE:
851 /* Get this value the long way, since TYPE_METHOD_BASETYPE
852 is just the main variant of this. */
853 tree basetype = class_of_this_parm (t2);
854 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
855 TYPE_RAISES_EXCEPTIONS (t2),
856 NULL_TREE);
857 cp_ref_qualifier rqual = type_memfn_rqual (t1);
858 tree t3;
860 /* If this was a member function type, get back to the
861 original type of type member function (i.e., without
862 the class instance variable up front. */
863 t1 = build_function_type (TREE_TYPE (t1),
864 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
865 t2 = build_function_type (TREE_TYPE (t2),
866 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
867 t3 = merge_types (t1, t2);
868 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
869 TYPE_ARG_TYPES (t3));
870 t1 = build_exception_variant (t3, raises);
871 t1 = build_ref_qualified_type (t1, rqual);
872 break;
875 case TYPENAME_TYPE:
876 /* There is no need to merge attributes into a TYPENAME_TYPE.
877 When the type is instantiated it will have whatever
878 attributes result from the instantiation. */
879 return t1;
881 default:;
884 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
885 return t1;
886 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
887 return t2;
888 else
889 return cp_build_type_attribute_variant (t1, attributes);
892 /* Return the ARRAY_TYPE type without its domain. */
894 tree
895 strip_array_domain (tree type)
897 tree t2;
898 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
899 if (TYPE_DOMAIN (type) == NULL_TREE)
900 return type;
901 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
902 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
905 /* Wrapper around cp_common_type that is used by c-common.c and other
906 front end optimizations that remove promotions.
908 Return the common type for two arithmetic types T1 and T2 under the
909 usual arithmetic conversions. The default conversions have already
910 been applied, and enumerated types converted to their compatible
911 integer types. */
913 tree
914 common_type (tree t1, tree t2)
916 /* If one type is nonsense, use the other */
917 if (t1 == error_mark_node)
918 return t2;
919 if (t2 == error_mark_node)
920 return t1;
922 return cp_common_type (t1, t2);
925 /* Return the common type of two pointer types T1 and T2. This is the
926 type for the result of most arithmetic operations if the operands
927 have the given two types.
929 We assume that comp_target_types has already been done and returned
930 nonzero; if that isn't so, this may crash. */
932 tree
933 common_pointer_type (tree t1, tree t2)
935 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
936 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
937 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
939 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
940 CPO_CONVERSION, tf_warning_or_error);
943 /* Compare two exception specifier types for exactness or subsetness, if
944 allowed. Returns false for mismatch, true for match (same, or
945 derived and !exact).
947 [except.spec] "If a class X ... objects of class X or any class publicly
948 and unambiguously derived from X. Similarly, if a pointer type Y * ...
949 exceptions of type Y * or that are pointers to any type publicly and
950 unambiguously derived from Y. Otherwise a function only allows exceptions
951 that have the same type ..."
952 This does not mention cv qualifiers and is different to what throw
953 [except.throw] and catch [except.catch] will do. They will ignore the
954 top level cv qualifiers, and allow qualifiers in the pointer to class
955 example.
957 We implement the letter of the standard. */
959 static bool
960 comp_except_types (tree a, tree b, bool exact)
962 if (same_type_p (a, b))
963 return true;
964 else if (!exact)
966 if (cp_type_quals (a) || cp_type_quals (b))
967 return false;
969 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
971 a = TREE_TYPE (a);
972 b = TREE_TYPE (b);
973 if (cp_type_quals (a) || cp_type_quals (b))
974 return false;
977 if (TREE_CODE (a) != RECORD_TYPE
978 || TREE_CODE (b) != RECORD_TYPE)
979 return false;
981 if (publicly_uniquely_derived_p (a, b))
982 return true;
984 return false;
987 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
988 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
989 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
990 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
991 are unordered, but we've already filtered out duplicates. Most lists will
992 be in order, we should try to make use of that. */
994 bool
995 comp_except_specs (const_tree t1, const_tree t2, int exact)
997 const_tree probe;
998 const_tree base;
999 int length = 0;
1001 if (t1 == t2)
1002 return true;
1004 /* First handle noexcept. */
1005 if (exact < ce_exact)
1007 /* noexcept(false) is compatible with no exception-specification,
1008 and stricter than any spec. */
1009 if (t1 == noexcept_false_spec)
1010 return t2 == NULL_TREE || exact == ce_derived;
1011 /* Even a derived noexcept(false) is compatible with no
1012 exception-specification. */
1013 if (t2 == noexcept_false_spec)
1014 return t1 == NULL_TREE;
1016 /* Otherwise, if we aren't looking for an exact match, noexcept is
1017 equivalent to throw(). */
1018 if (t1 == noexcept_true_spec)
1019 t1 = empty_except_spec;
1020 if (t2 == noexcept_true_spec)
1021 t2 = empty_except_spec;
1024 /* If any noexcept is left, it is only comparable to itself;
1025 either we're looking for an exact match or we're redeclaring a
1026 template with dependent noexcept. */
1027 if ((t1 && TREE_PURPOSE (t1))
1028 || (t2 && TREE_PURPOSE (t2)))
1029 return (t1 && t2
1030 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1032 if (t1 == NULL_TREE) /* T1 is ... */
1033 return t2 == NULL_TREE || exact == ce_derived;
1034 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1035 return t2 != NULL_TREE && !TREE_VALUE (t2);
1036 if (t2 == NULL_TREE) /* T2 is ... */
1037 return false;
1038 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1039 return exact == ce_derived;
1041 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1042 Count how many we find, to determine exactness. For exact matching and
1043 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1044 O(nm). */
1045 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1047 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1049 tree a = TREE_VALUE (probe);
1050 tree b = TREE_VALUE (t2);
1052 if (comp_except_types (a, b, exact))
1054 if (probe == base && exact > ce_derived)
1055 base = TREE_CHAIN (probe);
1056 length++;
1057 break;
1060 if (probe == NULL_TREE)
1061 return false;
1063 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1066 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1067 [] can match [size]. */
1069 static bool
1070 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1072 tree d1;
1073 tree d2;
1074 tree max1, max2;
1076 if (t1 == t2)
1077 return true;
1079 /* The type of the array elements must be the same. */
1080 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1081 return false;
1083 d1 = TYPE_DOMAIN (t1);
1084 d2 = TYPE_DOMAIN (t2);
1086 if (d1 == d2)
1087 return true;
1089 /* If one of the arrays is dimensionless, and the other has a
1090 dimension, they are of different types. However, it is valid to
1091 write:
1093 extern int a[];
1094 int a[3];
1096 by [basic.link]:
1098 declarations for an array object can specify
1099 array types that differ by the presence or absence of a major
1100 array bound (_dcl.array_). */
1101 if (!d1 || !d2)
1102 return allow_redeclaration;
1104 /* Check that the dimensions are the same. */
1106 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1107 return false;
1108 max1 = TYPE_MAX_VALUE (d1);
1109 max2 = TYPE_MAX_VALUE (d2);
1110 if (processing_template_decl && !abi_version_at_least (2)
1111 && !value_dependent_expression_p (max1)
1112 && !value_dependent_expression_p (max2))
1114 /* With abi-1 we do not fold non-dependent array bounds, (and
1115 consequently mangle them incorrectly). We must therefore
1116 fold them here, to verify the domains have the same
1117 value. */
1118 max1 = fold (max1);
1119 max2 = fold (max2);
1122 if (!cp_tree_equal (max1, max2))
1123 return false;
1125 return true;
1128 /* Compare the relative position of T1 and T2 into their respective
1129 template parameter list.
1130 T1 and T2 must be template parameter types.
1131 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1133 static bool
1134 comp_template_parms_position (tree t1, tree t2)
1136 tree index1, index2;
1137 gcc_assert (t1 && t2
1138 && TREE_CODE (t1) == TREE_CODE (t2)
1139 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1140 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1141 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1143 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1144 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1146 /* Then compare their relative position. */
1147 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1148 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1149 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1150 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1151 return false;
1153 /* In C++14 we can end up comparing 'auto' to a normal template
1154 parameter. Don't confuse them. */
1155 if (cxx_dialect >= cxx1y && (is_auto (t1) || is_auto (t2)))
1156 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1158 return true;
1161 /* Subroutine in comptypes. */
1163 static bool
1164 structural_comptypes (tree t1, tree t2, int strict)
1166 if (t1 == t2)
1167 return true;
1169 /* Suppress errors caused by previously reported errors. */
1170 if (t1 == error_mark_node || t2 == error_mark_node)
1171 return false;
1173 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1175 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1176 current instantiation. */
1177 if (TREE_CODE (t1) == TYPENAME_TYPE)
1178 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1180 if (TREE_CODE (t2) == TYPENAME_TYPE)
1181 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1183 if (TYPE_PTRMEMFUNC_P (t1))
1184 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1185 if (TYPE_PTRMEMFUNC_P (t2))
1186 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1188 /* Different classes of types can't be compatible. */
1189 if (TREE_CODE (t1) != TREE_CODE (t2))
1190 return false;
1192 /* Qualifiers must match. For array types, we will check when we
1193 recur on the array element types. */
1194 if (TREE_CODE (t1) != ARRAY_TYPE
1195 && cp_type_quals (t1) != cp_type_quals (t2))
1196 return false;
1197 if (TREE_CODE (t1) == FUNCTION_TYPE
1198 && type_memfn_quals (t1) != type_memfn_quals (t2))
1199 return false;
1200 /* Need to check this before TYPE_MAIN_VARIANT.
1201 FIXME function qualifiers should really change the main variant. */
1202 if ((TREE_CODE (t1) == FUNCTION_TYPE
1203 || TREE_CODE (t1) == METHOD_TYPE)
1204 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1205 return false;
1206 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1207 return false;
1209 /* Allow for two different type nodes which have essentially the same
1210 definition. Note that we already checked for equality of the type
1211 qualifiers (just above). */
1213 if (TREE_CODE (t1) != ARRAY_TYPE
1214 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1215 return true;
1218 /* Compare the types. Break out if they could be the same. */
1219 switch (TREE_CODE (t1))
1221 case VOID_TYPE:
1222 case BOOLEAN_TYPE:
1223 /* All void and bool types are the same. */
1224 break;
1226 case INTEGER_TYPE:
1227 case FIXED_POINT_TYPE:
1228 case REAL_TYPE:
1229 /* With these nodes, we can't determine type equivalence by
1230 looking at what is stored in the nodes themselves, because
1231 two nodes might have different TYPE_MAIN_VARIANTs but still
1232 represent the same type. For example, wchar_t and int could
1233 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1234 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1235 and are distinct types. On the other hand, int and the
1236 following typedef
1238 typedef int INT __attribute((may_alias));
1240 have identical properties, different TYPE_MAIN_VARIANTs, but
1241 represent the same type. The canonical type system keeps
1242 track of equivalence in this case, so we fall back on it. */
1243 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1245 case TEMPLATE_TEMPLATE_PARM:
1246 case BOUND_TEMPLATE_TEMPLATE_PARM:
1247 if (!comp_template_parms_position (t1, t2))
1248 return false;
1249 if (!comp_template_parms
1250 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1251 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1252 return false;
1253 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1254 break;
1255 /* Don't check inheritance. */
1256 strict = COMPARE_STRICT;
1257 /* Fall through. */
1259 case RECORD_TYPE:
1260 case UNION_TYPE:
1261 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1262 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1263 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1264 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1265 break;
1267 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1268 break;
1269 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1270 break;
1272 return false;
1274 case OFFSET_TYPE:
1275 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1276 strict & ~COMPARE_REDECLARATION))
1277 return false;
1278 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1279 return false;
1280 break;
1282 case REFERENCE_TYPE:
1283 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1284 return false;
1285 /* fall through to checks for pointer types */
1287 case POINTER_TYPE:
1288 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1289 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1290 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1291 return false;
1292 break;
1294 case METHOD_TYPE:
1295 case FUNCTION_TYPE:
1296 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1297 return false;
1298 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1299 return false;
1300 break;
1302 case ARRAY_TYPE:
1303 /* Target types must match incl. qualifiers. */
1304 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1305 return false;
1306 break;
1308 case TEMPLATE_TYPE_PARM:
1309 /* If T1 and T2 don't have the same relative position in their
1310 template parameters set, they can't be equal. */
1311 if (!comp_template_parms_position (t1, t2))
1312 return false;
1313 break;
1315 case TYPENAME_TYPE:
1316 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1317 TYPENAME_TYPE_FULLNAME (t2)))
1318 return false;
1319 /* Qualifiers don't matter on scopes. */
1320 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1321 TYPE_CONTEXT (t2)))
1322 return false;
1323 break;
1325 case UNBOUND_CLASS_TEMPLATE:
1326 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1327 return false;
1328 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1329 return false;
1330 break;
1332 case COMPLEX_TYPE:
1333 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1334 return false;
1335 break;
1337 case VECTOR_TYPE:
1338 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1339 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1340 return false;
1341 break;
1343 case TYPE_PACK_EXPANSION:
1344 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1345 PACK_EXPANSION_PATTERN (t2))
1346 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1347 PACK_EXPANSION_EXTRA_ARGS (t2)));
1349 case DECLTYPE_TYPE:
1350 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1351 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1352 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1353 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1354 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1355 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1356 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1357 DECLTYPE_TYPE_EXPR (t2)))
1358 return false;
1359 break;
1361 case UNDERLYING_TYPE:
1362 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1363 UNDERLYING_TYPE_TYPE (t2));
1365 default:
1366 return false;
1369 /* If we get here, we know that from a target independent POV the
1370 types are the same. Make sure the target attributes are also
1371 the same. */
1372 return comp_type_attributes (t1, t2);
1375 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1376 is a bitwise-or of the COMPARE_* flags. */
1378 bool
1379 comptypes (tree t1, tree t2, int strict)
1381 if (strict == COMPARE_STRICT)
1383 if (t1 == t2)
1384 return true;
1386 if (t1 == error_mark_node || t2 == error_mark_node)
1387 return false;
1389 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1390 /* At least one of the types requires structural equality, so
1391 perform a deep check. */
1392 return structural_comptypes (t1, t2, strict);
1394 #ifdef ENABLE_CHECKING
1395 if (USE_CANONICAL_TYPES)
1397 bool result = structural_comptypes (t1, t2, strict);
1399 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1400 /* The two types are structurally equivalent, but their
1401 canonical types were different. This is a failure of the
1402 canonical type propagation code.*/
1403 internal_error
1404 ("canonical types differ for identical types %T and %T",
1405 t1, t2);
1406 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1407 /* Two types are structurally different, but the canonical
1408 types are the same. This means we were over-eager in
1409 assigning canonical types. */
1410 internal_error
1411 ("same canonical type node for different types %T and %T",
1412 t1, t2);
1414 return result;
1416 #else
1417 if (USE_CANONICAL_TYPES)
1418 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1419 #endif
1420 else
1421 return structural_comptypes (t1, t2, strict);
1423 else if (strict == COMPARE_STRUCTURAL)
1424 return structural_comptypes (t1, t2, COMPARE_STRICT);
1425 else
1426 return structural_comptypes (t1, t2, strict);
1429 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1430 top-level qualifiers. */
1432 bool
1433 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1435 if (type1 == error_mark_node || type2 == error_mark_node)
1436 return false;
1438 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1441 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1443 bool
1444 at_least_as_qualified_p (const_tree type1, const_tree type2)
1446 int q1 = cp_type_quals (type1);
1447 int q2 = cp_type_quals (type2);
1449 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1450 return (q1 & q2) == q2;
1453 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1454 more cv-qualified that TYPE1, and 0 otherwise. */
1457 comp_cv_qualification (const_tree type1, const_tree type2)
1459 int q1 = cp_type_quals (type1);
1460 int q2 = cp_type_quals (type2);
1462 if (q1 == q2)
1463 return 0;
1465 if ((q1 & q2) == q2)
1466 return 1;
1467 else if ((q1 & q2) == q1)
1468 return -1;
1470 return 0;
1473 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1474 subset of the cv-qualification signature of TYPE2, and the types
1475 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1478 comp_cv_qual_signature (tree type1, tree type2)
1480 if (comp_ptr_ttypes_real (type2, type1, -1))
1481 return 1;
1482 else if (comp_ptr_ttypes_real (type1, type2, -1))
1483 return -1;
1484 else
1485 return 0;
1488 /* Subroutines of `comptypes'. */
1490 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1491 equivalent in the sense that functions with those parameter types
1492 can have equivalent types. The two lists must be equivalent,
1493 element by element. */
1495 bool
1496 compparms (const_tree parms1, const_tree parms2)
1498 const_tree t1, t2;
1500 /* An unspecified parmlist matches any specified parmlist
1501 whose argument types don't need default promotions. */
1503 for (t1 = parms1, t2 = parms2;
1504 t1 || t2;
1505 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1507 /* If one parmlist is shorter than the other,
1508 they fail to match. */
1509 if (!t1 || !t2)
1510 return false;
1511 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1512 return false;
1514 return true;
1518 /* Process a sizeof or alignof expression where the operand is a
1519 type. */
1521 tree
1522 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1524 tree value;
1525 bool dependent_p;
1527 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1528 if (type == error_mark_node)
1529 return error_mark_node;
1531 type = non_reference (type);
1532 if (TREE_CODE (type) == METHOD_TYPE)
1534 if (complain)
1535 pedwarn (input_location, OPT_Wpointer_arith,
1536 "invalid application of %qs to a member function",
1537 operator_name_info[(int) op].name);
1538 value = size_one_node;
1541 dependent_p = dependent_type_p (type);
1542 if (!dependent_p)
1543 complete_type (type);
1544 if (dependent_p
1545 /* VLA types will have a non-constant size. In the body of an
1546 uninstantiated template, we don't need to try to compute the
1547 value, because the sizeof expression is not an integral
1548 constant expression in that case. And, if we do try to
1549 compute the value, we'll likely end up with SAVE_EXPRs, which
1550 the template substitution machinery does not expect to see. */
1551 || (processing_template_decl
1552 && COMPLETE_TYPE_P (type)
1553 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1555 value = build_min (op, size_type_node, type);
1556 TREE_READONLY (value) = 1;
1557 return value;
1560 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)
1561 && (flag_iso || warn_vla > 0))
1563 if (complain & tf_warning_or_error)
1564 pedwarn (input_location, OPT_Wvla,
1565 "taking sizeof array of runtime bound");
1566 else
1567 return error_mark_node;
1570 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1571 op == SIZEOF_EXPR, false,
1572 complain);
1575 /* Return the size of the type, without producing any warnings for
1576 types whose size cannot be taken. This routine should be used only
1577 in some other routine that has already produced a diagnostic about
1578 using the size of such a type. */
1579 tree
1580 cxx_sizeof_nowarn (tree type)
1582 if (TREE_CODE (type) == FUNCTION_TYPE
1583 || VOID_TYPE_P (type)
1584 || TREE_CODE (type) == ERROR_MARK)
1585 return size_one_node;
1586 else if (!COMPLETE_TYPE_P (type))
1587 return size_zero_node;
1588 else
1589 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1592 /* Process a sizeof expression where the operand is an expression. */
1594 static tree
1595 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1597 if (e == error_mark_node)
1598 return error_mark_node;
1600 if (processing_template_decl)
1602 e = build_min (SIZEOF_EXPR, size_type_node, e);
1603 TREE_SIDE_EFFECTS (e) = 0;
1604 TREE_READONLY (e) = 1;
1606 return e;
1609 /* To get the size of a static data member declared as an array of
1610 unknown bound, we need to instantiate it. */
1611 if (VAR_P (e)
1612 && VAR_HAD_UNKNOWN_BOUND (e)
1613 && DECL_TEMPLATE_INSTANTIATION (e))
1614 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1616 e = mark_type_use (e);
1618 if (TREE_CODE (e) == COMPONENT_REF
1619 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1620 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1622 if (complain & tf_error)
1623 error ("invalid application of %<sizeof%> to a bit-field");
1624 else
1625 return error_mark_node;
1626 e = char_type_node;
1628 else if (is_overloaded_fn (e))
1630 if (complain & tf_error)
1631 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1632 "function type");
1633 else
1634 return error_mark_node;
1635 e = char_type_node;
1637 else if (type_unknown_p (e))
1639 if (complain & tf_error)
1640 cxx_incomplete_type_error (e, TREE_TYPE (e));
1641 else
1642 return error_mark_node;
1643 e = char_type_node;
1645 else
1646 e = TREE_TYPE (e);
1648 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1651 /* Implement the __alignof keyword: Return the minimum required
1652 alignment of E, measured in bytes. For VAR_DECL's and
1653 FIELD_DECL's return DECL_ALIGN (which can be set from an
1654 "aligned" __attribute__ specification). */
1656 static tree
1657 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1659 tree t;
1661 if (e == error_mark_node)
1662 return error_mark_node;
1664 if (processing_template_decl)
1666 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1667 TREE_SIDE_EFFECTS (e) = 0;
1668 TREE_READONLY (e) = 1;
1670 return e;
1673 e = mark_type_use (e);
1675 if (VAR_P (e))
1676 t = size_int (DECL_ALIGN_UNIT (e));
1677 else if (TREE_CODE (e) == COMPONENT_REF
1678 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1679 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1681 if (complain & tf_error)
1682 error ("invalid application of %<__alignof%> to a bit-field");
1683 else
1684 return error_mark_node;
1685 t = size_one_node;
1687 else if (TREE_CODE (e) == COMPONENT_REF
1688 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1689 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1690 else if (is_overloaded_fn (e))
1692 if (complain & tf_error)
1693 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1694 "function type");
1695 else
1696 return error_mark_node;
1697 if (TREE_CODE (e) == FUNCTION_DECL)
1698 t = size_int (DECL_ALIGN_UNIT (e));
1699 else
1700 t = size_one_node;
1702 else if (type_unknown_p (e))
1704 if (complain & tf_error)
1705 cxx_incomplete_type_error (e, TREE_TYPE (e));
1706 else
1707 return error_mark_node;
1708 t = size_one_node;
1710 else
1711 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1712 complain & tf_error);
1714 return fold_convert (size_type_node, t);
1717 /* Process a sizeof or alignof expression E with code OP where the operand
1718 is an expression. */
1720 tree
1721 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1723 if (op == SIZEOF_EXPR)
1724 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1725 else
1726 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1729 /* Build a representation of an expression 'alignas(E).' Return the
1730 folded integer value of E if it is an integral constant expression
1731 that resolves to a valid alignment. If E depends on a template
1732 parameter, return a syntactic representation tree of kind
1733 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1734 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1736 tree
1737 cxx_alignas_expr (tree e)
1739 if (e == NULL_TREE || e == error_mark_node
1740 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1741 return e;
1743 if (TYPE_P (e))
1744 /* [dcl.align]/3:
1746 When the alignment-specifier is of the form
1747 alignas(type-id ), it shall have the same effect as
1748 alignas(alignof(type-id )). */
1750 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1752 /* If we reach this point, it means the alignas expression if of
1753 the form "alignas(assignment-expression)", so we should follow
1754 what is stated by [dcl.align]/2. */
1756 if (value_dependent_expression_p (e))
1757 /* Leave value-dependent expression alone for now. */
1758 return e;
1760 e = fold_non_dependent_expr (e);
1761 e = mark_rvalue_use (e);
1763 /* [dcl.align]/2 says:
1765 the assignment-expression shall be an integral constant
1766 expression. */
1768 return cxx_constant_value (e);
1772 /* EXPR is being used in a context that is not a function call.
1773 Enforce:
1775 [expr.ref]
1777 The expression can be used only as the left-hand operand of a
1778 member function call.
1780 [expr.mptr.operator]
1782 If the result of .* or ->* is a function, then that result can be
1783 used only as the operand for the function call operator ().
1785 by issuing an error message if appropriate. Returns true iff EXPR
1786 violates these rules. */
1788 bool
1789 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1791 if (expr == NULL_TREE)
1792 return false;
1793 /* Don't enforce this in MS mode. */
1794 if (flag_ms_extensions)
1795 return false;
1796 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1797 expr = get_first_fn (expr);
1798 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1800 if (complain & tf_error)
1801 error ("invalid use of non-static member function");
1802 return true;
1804 return false;
1807 /* If EXP is a reference to a bitfield, and the type of EXP does not
1808 match the declared type of the bitfield, return the declared type
1809 of the bitfield. Otherwise, return NULL_TREE. */
1811 tree
1812 is_bitfield_expr_with_lowered_type (const_tree exp)
1814 switch (TREE_CODE (exp))
1816 case COND_EXPR:
1817 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1818 ? TREE_OPERAND (exp, 1)
1819 : TREE_OPERAND (exp, 0)))
1820 return NULL_TREE;
1821 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1823 case COMPOUND_EXPR:
1824 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1826 case MODIFY_EXPR:
1827 case SAVE_EXPR:
1828 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1830 case COMPONENT_REF:
1832 tree field;
1834 field = TREE_OPERAND (exp, 1);
1835 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1836 return NULL_TREE;
1837 if (same_type_ignoring_top_level_qualifiers_p
1838 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1839 return NULL_TREE;
1840 return DECL_BIT_FIELD_TYPE (field);
1843 CASE_CONVERT:
1844 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1845 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1846 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1847 /* Fallthrough. */
1849 default:
1850 return NULL_TREE;
1854 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1855 bitfield with a lowered type, the type of EXP is returned, rather
1856 than NULL_TREE. */
1858 tree
1859 unlowered_expr_type (const_tree exp)
1861 tree type;
1862 tree etype = TREE_TYPE (exp);
1864 type = is_bitfield_expr_with_lowered_type (exp);
1865 if (type)
1866 type = cp_build_qualified_type (type, cp_type_quals (etype));
1867 else
1868 type = etype;
1870 return type;
1873 /* Perform the conversions in [expr] that apply when an lvalue appears
1874 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1875 function-to-pointer conversions. In addition, manifest constants
1876 are replaced by their values, and bitfield references are converted
1877 to their declared types. Note that this function does not perform the
1878 lvalue-to-rvalue conversion for class types. If you need that conversion
1879 to for class types, then you probably need to use force_rvalue.
1881 Although the returned value is being used as an rvalue, this
1882 function does not wrap the returned expression in a
1883 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1884 that the return value is no longer an lvalue. */
1886 tree
1887 decay_conversion (tree exp, tsubst_flags_t complain)
1889 tree type;
1890 enum tree_code code;
1891 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1893 type = TREE_TYPE (exp);
1894 if (type == error_mark_node)
1895 return error_mark_node;
1897 exp = mark_rvalue_use (exp);
1899 exp = resolve_nondeduced_context (exp);
1900 if (type_unknown_p (exp))
1902 if (complain & tf_error)
1903 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1904 return error_mark_node;
1907 code = TREE_CODE (type);
1909 /* For an array decl decay_conversion should not try to return its
1910 initializer. */
1911 if (code != ARRAY_TYPE)
1913 /* FIXME remove? at least need to remember that this isn't really a
1914 constant expression if EXP isn't decl_constant_var_p, like with
1915 C_MAYBE_CONST_EXPR. */
1916 exp = decl_constant_value_safe (exp);
1917 if (error_operand_p (exp))
1918 return error_mark_node;
1921 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1922 return nullptr_node;
1924 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1925 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1926 if (code == VOID_TYPE)
1928 if (complain & tf_error)
1929 error_at (loc, "void value not ignored as it ought to be");
1930 return error_mark_node;
1932 if (invalid_nonstatic_memfn_p (exp, complain))
1933 return error_mark_node;
1934 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1935 return cp_build_addr_expr (exp, complain);
1936 if (code == ARRAY_TYPE)
1938 tree adr;
1939 tree ptrtype;
1941 if (INDIRECT_REF_P (exp))
1942 return build_nop (build_pointer_type (TREE_TYPE (type)),
1943 TREE_OPERAND (exp, 0));
1945 if (TREE_CODE (exp) == COMPOUND_EXPR)
1947 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1948 if (op1 == error_mark_node)
1949 return error_mark_node;
1950 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1951 TREE_OPERAND (exp, 0), op1);
1954 if (!lvalue_p (exp)
1955 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1957 if (complain & tf_error)
1958 error_at (loc, "invalid use of non-lvalue array");
1959 return error_mark_node;
1962 /* Don't let an array compound literal decay to a pointer. It can
1963 still be used to initialize an array or bind to a reference. */
1964 if (TREE_CODE (exp) == TARGET_EXPR)
1966 if (complain & tf_error)
1967 error_at (loc, "taking address of temporary array");
1968 return error_mark_node;
1971 ptrtype = build_pointer_type (TREE_TYPE (type));
1973 if (VAR_P (exp))
1975 if (!cxx_mark_addressable (exp))
1976 return error_mark_node;
1977 adr = build_nop (ptrtype, build_address (exp));
1978 return adr;
1980 /* This way is better for a COMPONENT_REF since it can
1981 simplify the offset for a component. */
1982 adr = cp_build_addr_expr (exp, complain);
1983 return cp_convert (ptrtype, adr, complain);
1986 /* If a bitfield is used in a context where integral promotion
1987 applies, then the caller is expected to have used
1988 default_conversion. That function promotes bitfields correctly
1989 before calling this function. At this point, if we have a
1990 bitfield referenced, we may assume that is not subject to
1991 promotion, and that, therefore, the type of the resulting rvalue
1992 is the declared type of the bitfield. */
1993 exp = convert_bitfield_to_declared_type (exp);
1995 /* We do not call rvalue() here because we do not want to wrap EXP
1996 in a NON_LVALUE_EXPR. */
1998 /* [basic.lval]
2000 Non-class rvalues always have cv-unqualified types. */
2001 type = TREE_TYPE (exp);
2002 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2003 exp = build_nop (cv_unqualified (type), exp);
2005 return exp;
2008 /* Perform preparatory conversions, as part of the "usual arithmetic
2009 conversions". In particular, as per [expr]:
2011 Whenever an lvalue expression appears as an operand of an
2012 operator that expects the rvalue for that operand, the
2013 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2014 standard conversions are applied to convert the expression to an
2015 rvalue.
2017 In addition, we perform integral promotions here, as those are
2018 applied to both operands to a binary operator before determining
2019 what additional conversions should apply. */
2021 static tree
2022 cp_default_conversion (tree exp, tsubst_flags_t complain)
2024 /* Check for target-specific promotions. */
2025 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2026 if (promoted_type)
2027 exp = cp_convert (promoted_type, exp, complain);
2028 /* Perform the integral promotions first so that bitfield
2029 expressions (which may promote to "int", even if the bitfield is
2030 declared "unsigned") are promoted correctly. */
2031 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2032 exp = cp_perform_integral_promotions (exp, complain);
2033 /* Perform the other conversions. */
2034 exp = decay_conversion (exp, complain);
2036 return exp;
2039 /* C version. */
2041 tree
2042 default_conversion (tree exp)
2044 return cp_default_conversion (exp, tf_warning_or_error);
2047 /* EXPR is an expression with an integral or enumeration type.
2048 Perform the integral promotions in [conv.prom], and return the
2049 converted value. */
2051 tree
2052 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2054 tree type;
2055 tree promoted_type;
2057 expr = mark_rvalue_use (expr);
2059 /* [conv.prom]
2061 If the bitfield has an enumerated type, it is treated as any
2062 other value of that type for promotion purposes. */
2063 type = is_bitfield_expr_with_lowered_type (expr);
2064 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2065 type = TREE_TYPE (expr);
2066 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2067 /* Scoped enums don't promote. */
2068 if (SCOPED_ENUM_P (type))
2069 return expr;
2070 promoted_type = type_promotes_to (type);
2071 if (type != promoted_type)
2072 expr = cp_convert (promoted_type, expr, complain);
2073 return expr;
2076 /* C version. */
2078 tree
2079 perform_integral_promotions (tree expr)
2081 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2084 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2085 decay_conversion to one. */
2088 string_conv_p (const_tree totype, const_tree exp, int warn)
2090 tree t;
2092 if (!TYPE_PTR_P (totype))
2093 return 0;
2095 t = TREE_TYPE (totype);
2096 if (!same_type_p (t, char_type_node)
2097 && !same_type_p (t, char16_type_node)
2098 && !same_type_p (t, char32_type_node)
2099 && !same_type_p (t, wchar_type_node))
2100 return 0;
2102 if (TREE_CODE (exp) == STRING_CST)
2104 /* Make sure that we don't try to convert between char and wide chars. */
2105 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2106 return 0;
2108 else
2110 /* Is this a string constant which has decayed to 'const char *'? */
2111 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2112 if (!same_type_p (TREE_TYPE (exp), t))
2113 return 0;
2114 STRIP_NOPS (exp);
2115 if (TREE_CODE (exp) != ADDR_EXPR
2116 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2117 return 0;
2120 /* This warning is not very useful, as it complains about printf. */
2121 if (warn)
2122 warning (OPT_Wwrite_strings,
2123 "deprecated conversion from string constant to %qT",
2124 totype);
2126 return 1;
2129 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2130 can, for example, use as an lvalue. This code used to be in
2131 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2132 expressions, where we're dealing with aggregates. But now it's again only
2133 called from unary_complex_lvalue. The case (in particular) that led to
2134 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2135 get it there. */
2137 static tree
2138 rationalize_conditional_expr (enum tree_code code, tree t,
2139 tsubst_flags_t complain)
2141 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2143 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2144 the first operand is always the one to be used if both operands
2145 are equal, so we know what conditional expression this used to be. */
2146 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2148 tree op0 = TREE_OPERAND (t, 0);
2149 tree op1 = TREE_OPERAND (t, 1);
2151 /* The following code is incorrect if either operand side-effects. */
2152 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2153 && !TREE_SIDE_EFFECTS (op1));
2154 return
2155 build_conditional_expr (loc,
2156 build_x_binary_op (loc,
2157 (TREE_CODE (t) == MIN_EXPR
2158 ? LE_EXPR : GE_EXPR),
2159 op0, TREE_CODE (op0),
2160 op1, TREE_CODE (op1),
2161 /*overload=*/NULL,
2162 complain),
2163 cp_build_unary_op (code, op0, 0, complain),
2164 cp_build_unary_op (code, op1, 0, complain),
2165 complain);
2168 return
2169 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2170 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2171 complain),
2172 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2173 complain),
2174 complain);
2177 /* Given the TYPE of an anonymous union field inside T, return the
2178 FIELD_DECL for the field. If not found return NULL_TREE. Because
2179 anonymous unions can nest, we must also search all anonymous unions
2180 that are directly reachable. */
2182 tree
2183 lookup_anon_field (tree t, tree type)
2185 tree field;
2187 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2189 if (TREE_STATIC (field))
2190 continue;
2191 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2192 continue;
2194 /* If we find it directly, return the field. */
2195 if (DECL_NAME (field) == NULL_TREE
2196 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2198 return field;
2201 /* Otherwise, it could be nested, search harder. */
2202 if (DECL_NAME (field) == NULL_TREE
2203 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2205 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2206 if (subfield)
2207 return subfield;
2210 return NULL_TREE;
2213 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2214 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2215 non-NULL, it indicates the path to the base used to name MEMBER.
2216 If PRESERVE_REFERENCE is true, the expression returned will have
2217 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2218 returned will have the type referred to by the reference.
2220 This function does not perform access control; that is either done
2221 earlier by the parser when the name of MEMBER is resolved to MEMBER
2222 itself, or later when overload resolution selects one of the
2223 functions indicated by MEMBER. */
2225 tree
2226 build_class_member_access_expr (tree object, tree member,
2227 tree access_path, bool preserve_reference,
2228 tsubst_flags_t complain)
2230 tree object_type;
2231 tree member_scope;
2232 tree result = NULL_TREE;
2233 tree using_decl = NULL_TREE;
2235 if (error_operand_p (object) || error_operand_p (member))
2236 return error_mark_node;
2238 gcc_assert (DECL_P (member) || BASELINK_P (member));
2240 /* [expr.ref]
2242 The type of the first expression shall be "class object" (of a
2243 complete type). */
2244 object_type = TREE_TYPE (object);
2245 if (!currently_open_class (object_type)
2246 && !complete_type_or_maybe_complain (object_type, object, complain))
2247 return error_mark_node;
2248 if (!CLASS_TYPE_P (object_type))
2250 if (complain & tf_error)
2252 if (POINTER_TYPE_P (object_type)
2253 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2254 error ("request for member %qD in %qE, which is of pointer "
2255 "type %qT (maybe you meant to use %<->%> ?)",
2256 member, object, object_type);
2257 else
2258 error ("request for member %qD in %qE, which is of non-class "
2259 "type %qT", member, object, object_type);
2261 return error_mark_node;
2264 /* The standard does not seem to actually say that MEMBER must be a
2265 member of OBJECT_TYPE. However, that is clearly what is
2266 intended. */
2267 if (DECL_P (member))
2269 member_scope = DECL_CLASS_CONTEXT (member);
2270 mark_used (member);
2271 if (TREE_DEPRECATED (member))
2272 warn_deprecated_use (member, NULL_TREE);
2274 else
2275 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2276 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2277 presently be the anonymous union. Go outwards until we find a
2278 type related to OBJECT_TYPE. */
2279 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2280 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2281 object_type))
2282 member_scope = TYPE_CONTEXT (member_scope);
2283 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2285 if (complain & tf_error)
2287 if (TREE_CODE (member) == FIELD_DECL)
2288 error ("invalid use of nonstatic data member %qE", member);
2289 else
2290 error ("%qD is not a member of %qT", member, object_type);
2292 return error_mark_node;
2295 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2296 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2297 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2299 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2300 if (temp)
2301 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2304 /* In [expr.ref], there is an explicit list of the valid choices for
2305 MEMBER. We check for each of those cases here. */
2306 if (VAR_P (member))
2308 /* A static data member. */
2309 result = member;
2310 mark_exp_read (object);
2311 /* If OBJECT has side-effects, they are supposed to occur. */
2312 if (TREE_SIDE_EFFECTS (object))
2313 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2315 else if (TREE_CODE (member) == FIELD_DECL)
2317 /* A non-static data member. */
2318 bool null_object_p;
2319 int type_quals;
2320 tree member_type;
2322 null_object_p = (INDIRECT_REF_P (object)
2323 && integer_zerop (TREE_OPERAND (object, 0)));
2325 /* Convert OBJECT to the type of MEMBER. */
2326 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2327 TYPE_MAIN_VARIANT (member_scope)))
2329 tree binfo;
2330 base_kind kind;
2332 binfo = lookup_base (access_path ? access_path : object_type,
2333 member_scope, ba_unique, &kind, complain);
2334 if (binfo == error_mark_node)
2335 return error_mark_node;
2337 /* It is invalid to try to get to a virtual base of a
2338 NULL object. The most common cause is invalid use of
2339 offsetof macro. */
2340 if (null_object_p && kind == bk_via_virtual)
2342 if (complain & tf_error)
2344 error ("invalid access to non-static data member %qD of "
2345 "NULL object",
2346 member);
2347 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2349 return error_mark_node;
2352 /* Convert to the base. */
2353 object = build_base_path (PLUS_EXPR, object, binfo,
2354 /*nonnull=*/1, complain);
2355 /* If we found the base successfully then we should be able
2356 to convert to it successfully. */
2357 gcc_assert (object != error_mark_node);
2360 /* Complain about other invalid uses of offsetof, even though they will
2361 give the right answer. Note that we complain whether or not they
2362 actually used the offsetof macro, since there's no way to know at this
2363 point. So we just give a warning, instead of a pedwarn. */
2364 /* Do not produce this warning for base class field references, because
2365 we know for a fact that didn't come from offsetof. This does occur
2366 in various testsuite cases where a null object is passed where a
2367 vtable access is required. */
2368 if (null_object_p && warn_invalid_offsetof
2369 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2370 && !DECL_FIELD_IS_BASE (member)
2371 && cp_unevaluated_operand == 0
2372 && (complain & tf_warning))
2374 warning (OPT_Winvalid_offsetof,
2375 "invalid access to non-static data member %qD "
2376 " of NULL object", member);
2377 warning (OPT_Winvalid_offsetof,
2378 "(perhaps the %<offsetof%> macro was used incorrectly)");
2381 /* If MEMBER is from an anonymous aggregate, we have converted
2382 OBJECT so that it refers to the class containing the
2383 anonymous union. Generate a reference to the anonymous union
2384 itself, and recur to find MEMBER. */
2385 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2386 /* When this code is called from build_field_call, the
2387 object already has the type of the anonymous union.
2388 That is because the COMPONENT_REF was already
2389 constructed, and was then disassembled before calling
2390 build_field_call. After the function-call code is
2391 cleaned up, this waste can be eliminated. */
2392 && (!same_type_ignoring_top_level_qualifiers_p
2393 (TREE_TYPE (object), DECL_CONTEXT (member))))
2395 tree anonymous_union;
2397 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2398 DECL_CONTEXT (member));
2399 object = build_class_member_access_expr (object,
2400 anonymous_union,
2401 /*access_path=*/NULL_TREE,
2402 preserve_reference,
2403 complain);
2406 /* Compute the type of the field, as described in [expr.ref]. */
2407 type_quals = TYPE_UNQUALIFIED;
2408 member_type = TREE_TYPE (member);
2409 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2411 type_quals = (cp_type_quals (member_type)
2412 | cp_type_quals (object_type));
2414 /* A field is const (volatile) if the enclosing object, or the
2415 field itself, is const (volatile). But, a mutable field is
2416 not const, even within a const object. */
2417 if (DECL_MUTABLE_P (member))
2418 type_quals &= ~TYPE_QUAL_CONST;
2419 member_type = cp_build_qualified_type (member_type, type_quals);
2422 result = build3 (COMPONENT_REF, member_type, object, member,
2423 NULL_TREE);
2424 result = fold_if_not_in_template (result);
2426 /* Mark the expression const or volatile, as appropriate. Even
2427 though we've dealt with the type above, we still have to mark the
2428 expression itself. */
2429 if (type_quals & TYPE_QUAL_CONST)
2430 TREE_READONLY (result) = 1;
2431 if (type_quals & TYPE_QUAL_VOLATILE)
2432 TREE_THIS_VOLATILE (result) = 1;
2434 else if (BASELINK_P (member))
2436 /* The member is a (possibly overloaded) member function. */
2437 tree functions;
2438 tree type;
2440 /* If the MEMBER is exactly one static member function, then we
2441 know the type of the expression. Otherwise, we must wait
2442 until overload resolution has been performed. */
2443 functions = BASELINK_FUNCTIONS (member);
2444 if (TREE_CODE (functions) == FUNCTION_DECL
2445 && DECL_STATIC_FUNCTION_P (functions))
2446 type = TREE_TYPE (functions);
2447 else
2448 type = unknown_type_node;
2449 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2450 base. That will happen when the function is called. */
2451 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2453 else if (TREE_CODE (member) == CONST_DECL)
2455 /* The member is an enumerator. */
2456 result = member;
2457 /* If OBJECT has side-effects, they are supposed to occur. */
2458 if (TREE_SIDE_EFFECTS (object))
2459 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2460 object, result);
2462 else if ((using_decl = strip_using_decl (member)) != member)
2463 result = build_class_member_access_expr (object,
2464 using_decl,
2465 access_path, preserve_reference,
2466 complain);
2467 else
2469 if (complain & tf_error)
2470 error ("invalid use of %qD", member);
2471 return error_mark_node;
2474 if (!preserve_reference)
2475 /* [expr.ref]
2477 If E2 is declared to have type "reference to T", then ... the
2478 type of E1.E2 is T. */
2479 result = convert_from_reference (result);
2481 return result;
2484 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2485 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2487 static tree
2488 lookup_destructor (tree object, tree scope, tree dtor_name,
2489 tsubst_flags_t complain)
2491 tree object_type = TREE_TYPE (object);
2492 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2493 tree expr;
2495 /* We've already complained about this destructor. */
2496 if (dtor_type == error_mark_node)
2497 return error_mark_node;
2499 if (scope && !check_dtor_name (scope, dtor_type))
2501 if (complain & tf_error)
2502 error ("qualified type %qT does not match destructor name ~%qT",
2503 scope, dtor_type);
2504 return error_mark_node;
2506 if (is_auto (dtor_type))
2507 dtor_type = object_type;
2508 else if (identifier_p (dtor_type))
2510 /* In a template, names we can't find a match for are still accepted
2511 destructor names, and we check them here. */
2512 if (check_dtor_name (object_type, dtor_type))
2513 dtor_type = object_type;
2514 else
2516 if (complain & tf_error)
2517 error ("object type %qT does not match destructor name ~%qT",
2518 object_type, dtor_type);
2519 return error_mark_node;
2523 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2525 if (complain & tf_error)
2526 error ("the type being destroyed is %qT, but the destructor "
2527 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2528 return error_mark_node;
2530 expr = lookup_member (dtor_type, complete_dtor_identifier,
2531 /*protect=*/1, /*want_type=*/false,
2532 tf_warning_or_error);
2533 expr = (adjust_result_of_qualified_name_lookup
2534 (expr, dtor_type, object_type));
2535 if (scope == NULL_TREE)
2536 /* We need to call adjust_result_of_qualified_name_lookup in case the
2537 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2538 that we still get virtual function binding. */
2539 BASELINK_QUALIFIED_P (expr) = false;
2540 return expr;
2543 /* An expression of the form "A::template B" has been resolved to
2544 DECL. Issue a diagnostic if B is not a template or template
2545 specialization. */
2547 void
2548 check_template_keyword (tree decl)
2550 /* The standard says:
2552 [temp.names]
2554 If a name prefixed by the keyword template is not a member
2555 template, the program is ill-formed.
2557 DR 228 removed the restriction that the template be a member
2558 template.
2560 DR 96, if accepted would add the further restriction that explicit
2561 template arguments must be provided if the template keyword is
2562 used, but, as of 2005-10-16, that DR is still in "drafting". If
2563 this DR is accepted, then the semantic checks here can be
2564 simplified, as the entity named must in fact be a template
2565 specialization, rather than, as at present, a set of overloaded
2566 functions containing at least one template function. */
2567 if (TREE_CODE (decl) != TEMPLATE_DECL
2568 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2570 if (!is_overloaded_fn (decl))
2571 permerror (input_location, "%qD is not a template", decl);
2572 else
2574 tree fns;
2575 fns = decl;
2576 if (BASELINK_P (fns))
2577 fns = BASELINK_FUNCTIONS (fns);
2578 while (fns)
2580 tree fn = OVL_CURRENT (fns);
2581 if (TREE_CODE (fn) == TEMPLATE_DECL
2582 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2583 break;
2584 if (TREE_CODE (fn) == FUNCTION_DECL
2585 && DECL_USE_TEMPLATE (fn)
2586 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2587 break;
2588 fns = OVL_NEXT (fns);
2590 if (!fns)
2591 permerror (input_location, "%qD is not a template", decl);
2596 /* This function is called by the parser to process a class member
2597 access expression of the form OBJECT.NAME. NAME is a node used by
2598 the parser to represent a name; it is not yet a DECL. It may,
2599 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2600 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2601 there is no reason to do the lookup twice, so the parser keeps the
2602 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2603 be a template via the use of the "A::template B" syntax. */
2605 tree
2606 finish_class_member_access_expr (tree object, tree name, bool template_p,
2607 tsubst_flags_t complain)
2609 tree expr;
2610 tree object_type;
2611 tree member;
2612 tree access_path = NULL_TREE;
2613 tree orig_object = object;
2614 tree orig_name = name;
2616 if (object == error_mark_node || name == error_mark_node)
2617 return error_mark_node;
2619 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2620 if (!objc_is_public (object, name))
2621 return error_mark_node;
2623 object_type = TREE_TYPE (object);
2625 if (processing_template_decl)
2627 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2628 dependent_type_p (object_type)
2629 /* If NAME is just an IDENTIFIER_NODE, then the expression
2630 is dependent. */
2631 || identifier_p (object)
2632 /* If NAME is "f<args>", where either 'f' or 'args' is
2633 dependent, then the expression is dependent. */
2634 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2635 && dependent_template_id_p (TREE_OPERAND (name, 0),
2636 TREE_OPERAND (name, 1)))
2637 /* If NAME is "T::X" where "T" is dependent, then the
2638 expression is dependent. */
2639 || (TREE_CODE (name) == SCOPE_REF
2640 && TYPE_P (TREE_OPERAND (name, 0))
2641 && dependent_type_p (TREE_OPERAND (name, 0))))
2642 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2643 object, name, NULL_TREE);
2644 object = build_non_dependent_expr (object);
2646 else if (c_dialect_objc ()
2647 && identifier_p (name)
2648 && (expr = objc_maybe_build_component_ref (object, name)))
2649 return expr;
2651 /* [expr.ref]
2653 The type of the first expression shall be "class object" (of a
2654 complete type). */
2655 if (!currently_open_class (object_type)
2656 && !complete_type_or_maybe_complain (object_type, object, complain))
2657 return error_mark_node;
2658 if (!CLASS_TYPE_P (object_type))
2660 if (complain & tf_error)
2662 if (POINTER_TYPE_P (object_type)
2663 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2664 error ("request for member %qD in %qE, which is of pointer "
2665 "type %qT (maybe you meant to use %<->%> ?)",
2666 name, object, object_type);
2667 else
2668 error ("request for member %qD in %qE, which is of non-class "
2669 "type %qT", name, object, object_type);
2671 return error_mark_node;
2674 if (BASELINK_P (name))
2675 /* A member function that has already been looked up. */
2676 member = name;
2677 else
2679 bool is_template_id = false;
2680 tree template_args = NULL_TREE;
2681 tree scope;
2683 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2685 is_template_id = true;
2686 template_args = TREE_OPERAND (name, 1);
2687 name = TREE_OPERAND (name, 0);
2689 if (TREE_CODE (name) == OVERLOAD)
2690 name = DECL_NAME (get_first_fn (name));
2691 else if (DECL_P (name))
2692 name = DECL_NAME (name);
2695 if (TREE_CODE (name) == SCOPE_REF)
2697 /* A qualified name. The qualifying class or namespace `S'
2698 has already been looked up; it is either a TYPE or a
2699 NAMESPACE_DECL. */
2700 scope = TREE_OPERAND (name, 0);
2701 name = TREE_OPERAND (name, 1);
2703 /* If SCOPE is a namespace, then the qualified name does not
2704 name a member of OBJECT_TYPE. */
2705 if (TREE_CODE (scope) == NAMESPACE_DECL)
2707 if (complain & tf_error)
2708 error ("%<%D::%D%> is not a member of %qT",
2709 scope, name, object_type);
2710 return error_mark_node;
2713 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2715 /* Looking up a member enumerator (c++/56793). */
2716 if (!TYPE_CLASS_SCOPE_P (scope)
2717 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2719 if (complain & tf_error)
2720 error ("%<%D::%D%> is not a member of %qT",
2721 scope, name, object_type);
2722 return error_mark_node;
2724 tree val = lookup_enumerator (scope, name);
2725 if (TREE_SIDE_EFFECTS (object))
2726 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2727 return val;
2730 gcc_assert (CLASS_TYPE_P (scope));
2731 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2733 if (constructor_name_p (name, scope))
2735 if (complain & tf_error)
2736 error ("cannot call constructor %<%T::%D%> directly",
2737 scope, name);
2738 return error_mark_node;
2741 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2742 access_path = lookup_base (object_type, scope, ba_check,
2743 NULL, complain);
2744 if (access_path == error_mark_node)
2745 return error_mark_node;
2746 if (!access_path)
2748 if (complain & tf_error)
2749 error ("%qT is not a base of %qT", scope, object_type);
2750 return error_mark_node;
2753 else
2755 scope = NULL_TREE;
2756 access_path = object_type;
2759 if (TREE_CODE (name) == BIT_NOT_EXPR)
2760 member = lookup_destructor (object, scope, name, complain);
2761 else
2763 /* Look up the member. */
2764 member = lookup_member (access_path, name, /*protect=*/1,
2765 /*want_type=*/false, complain);
2766 if (member == NULL_TREE)
2768 if (complain & tf_error)
2769 error ("%qD has no member named %qE",
2770 TREE_CODE (access_path) == TREE_BINFO
2771 ? TREE_TYPE (access_path) : object_type, name);
2772 return error_mark_node;
2774 if (member == error_mark_node)
2775 return error_mark_node;
2778 if (is_template_id)
2780 tree templ = member;
2782 if (BASELINK_P (templ))
2783 templ = lookup_template_function (templ, template_args);
2784 else
2786 if (complain & tf_error)
2787 error ("%qD is not a member template function", name);
2788 return error_mark_node;
2793 if (TREE_DEPRECATED (member))
2794 warn_deprecated_use (member, NULL_TREE);
2796 if (template_p)
2797 check_template_keyword (member);
2799 expr = build_class_member_access_expr (object, member, access_path,
2800 /*preserve_reference=*/false,
2801 complain);
2802 if (processing_template_decl && expr != error_mark_node)
2804 if (BASELINK_P (member))
2806 if (TREE_CODE (orig_name) == SCOPE_REF)
2807 BASELINK_QUALIFIED_P (member) = 1;
2808 orig_name = member;
2810 return build_min_non_dep (COMPONENT_REF, expr,
2811 orig_object, orig_name,
2812 NULL_TREE);
2815 return expr;
2818 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2819 type. */
2821 tree
2822 build_simple_component_ref (tree object, tree member)
2824 tree type = cp_build_qualified_type (TREE_TYPE (member),
2825 cp_type_quals (TREE_TYPE (object)));
2826 return fold_build3_loc (input_location,
2827 COMPONENT_REF, type,
2828 object, member, NULL_TREE);
2831 /* Return an expression for the MEMBER_NAME field in the internal
2832 representation of PTRMEM, a pointer-to-member function. (Each
2833 pointer-to-member function type gets its own RECORD_TYPE so it is
2834 more convenient to access the fields by name than by FIELD_DECL.)
2835 This routine converts the NAME to a FIELD_DECL and then creates the
2836 node for the complete expression. */
2838 tree
2839 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2841 tree ptrmem_type;
2842 tree member;
2844 /* This code is a stripped down version of
2845 build_class_member_access_expr. It does not work to use that
2846 routine directly because it expects the object to be of class
2847 type. */
2848 ptrmem_type = TREE_TYPE (ptrmem);
2849 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2850 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2851 /*want_type=*/false, tf_warning_or_error);
2852 return build_simple_component_ref (ptrmem, member);
2855 /* Given an expression PTR for a pointer, return an expression
2856 for the value pointed to.
2857 ERRORSTRING is the name of the operator to appear in error messages.
2859 This function may need to overload OPERATOR_FNNAME.
2860 Must also handle REFERENCE_TYPEs for C++. */
2862 tree
2863 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2864 tsubst_flags_t complain)
2866 tree orig_expr = expr;
2867 tree rval;
2869 if (processing_template_decl)
2871 /* Retain the type if we know the operand is a pointer. */
2872 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2873 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2874 if (type_dependent_expression_p (expr))
2875 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2876 expr = build_non_dependent_expr (expr);
2879 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2880 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2881 if (!rval)
2882 rval = cp_build_indirect_ref (expr, errorstring, complain);
2884 if (processing_template_decl && rval != error_mark_node)
2885 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2886 else
2887 return rval;
2890 /* Helper function called from c-common. */
2891 tree
2892 build_indirect_ref (location_t /*loc*/,
2893 tree ptr, ref_operator errorstring)
2895 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2898 tree
2899 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2900 tsubst_flags_t complain)
2902 tree pointer, type;
2904 if (ptr == current_class_ptr
2905 || (TREE_CODE (ptr) == NOP_EXPR
2906 && TREE_OPERAND (ptr, 0) == current_class_ptr
2907 && (same_type_ignoring_top_level_qualifiers_p
2908 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2909 return current_class_ref;
2911 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2912 ? ptr : decay_conversion (ptr, complain));
2913 if (pointer == error_mark_node)
2914 return error_mark_node;
2916 type = TREE_TYPE (pointer);
2918 if (POINTER_TYPE_P (type))
2920 /* [expr.unary.op]
2922 If the type of the expression is "pointer to T," the type
2923 of the result is "T." */
2924 tree t = TREE_TYPE (type);
2926 if (CONVERT_EXPR_P (ptr)
2927 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2929 /* If a warning is issued, mark it to avoid duplicates from
2930 the backend. This only needs to be done at
2931 warn_strict_aliasing > 2. */
2932 if (warn_strict_aliasing > 2)
2933 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2934 type, TREE_OPERAND (ptr, 0)))
2935 TREE_NO_WARNING (ptr) = 1;
2938 if (VOID_TYPE_P (t))
2940 /* A pointer to incomplete type (other than cv void) can be
2941 dereferenced [expr.unary.op]/1 */
2942 if (complain & tf_error)
2943 error ("%qT is not a pointer-to-object type", type);
2944 return error_mark_node;
2946 else if (TREE_CODE (pointer) == ADDR_EXPR
2947 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2948 /* The POINTER was something like `&x'. We simplify `*&x' to
2949 `x'. */
2950 return TREE_OPERAND (pointer, 0);
2951 else
2953 tree ref = build1 (INDIRECT_REF, t, pointer);
2955 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2956 so that we get the proper error message if the result is used
2957 to assign to. Also, &* is supposed to be a no-op. */
2958 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2959 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2960 TREE_SIDE_EFFECTS (ref)
2961 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2962 return ref;
2965 else if (!(complain & tf_error))
2966 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2968 /* `pointer' won't be an error_mark_node if we were given a
2969 pointer to member, so it's cool to check for this here. */
2970 else if (TYPE_PTRMEM_P (type))
2971 switch (errorstring)
2973 case RO_ARRAY_INDEXING:
2974 error ("invalid use of array indexing on pointer to member");
2975 break;
2976 case RO_UNARY_STAR:
2977 error ("invalid use of unary %<*%> on pointer to member");
2978 break;
2979 case RO_IMPLICIT_CONVERSION:
2980 error ("invalid use of implicit conversion on pointer to member");
2981 break;
2982 case RO_ARROW_STAR:
2983 error ("left hand operand of %<->*%> must be a pointer to class, "
2984 "but is a pointer to member of type %qT", type);
2985 break;
2986 default:
2987 gcc_unreachable ();
2989 else if (pointer != error_mark_node)
2990 invalid_indirection_error (input_location, type, errorstring);
2992 return error_mark_node;
2995 /* This handles expressions of the form "a[i]", which denotes
2996 an array reference.
2998 This is logically equivalent in C to *(a+i), but we may do it differently.
2999 If A is a variable or a member, we generate a primitive ARRAY_REF.
3000 This avoids forcing the array out of registers, and can work on
3001 arrays that are not lvalues (for example, members of structures returned
3002 by functions).
3004 If INDEX is of some user-defined type, it must be converted to
3005 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3006 will inherit the type of the array, which will be some pointer type.
3008 LOC is the location to use in building the array reference. */
3010 tree
3011 cp_build_array_ref (location_t loc, tree array, tree idx,
3012 tsubst_flags_t complain)
3014 tree ret;
3016 if (idx == 0)
3018 if (complain & tf_error)
3019 error_at (loc, "subscript missing in array reference");
3020 return error_mark_node;
3023 /* If an array's index is an array notation, then its rank cannot be
3024 greater than one. */
3025 if (flag_cilkplus && contains_array_notation_expr (idx))
3027 size_t rank = 0;
3029 /* If find_rank returns false, then it should have reported an error,
3030 thus it is unnecessary for repetition. */
3031 if (!find_rank (loc, idx, idx, true, &rank))
3032 return error_mark_node;
3033 if (rank > 1)
3035 error_at (loc, "rank of the array%'s index is greater than 1");
3036 return error_mark_node;
3039 if (TREE_TYPE (array) == error_mark_node
3040 || TREE_TYPE (idx) == error_mark_node)
3041 return error_mark_node;
3043 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3044 inside it. */
3045 switch (TREE_CODE (array))
3047 case COMPOUND_EXPR:
3049 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3050 complain);
3051 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3052 TREE_OPERAND (array, 0), value);
3053 SET_EXPR_LOCATION (ret, loc);
3054 return ret;
3057 case COND_EXPR:
3058 ret = build_conditional_expr
3059 (loc, TREE_OPERAND (array, 0),
3060 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3061 complain),
3062 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3063 complain),
3064 complain);
3065 protected_set_expr_location (ret, loc);
3066 return ret;
3068 default:
3069 break;
3072 convert_vector_to_pointer_for_subscript (loc, &array, idx);
3074 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3076 tree rval, type;
3078 warn_array_subscript_with_type_char (idx);
3080 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3082 if (complain & tf_error)
3083 error_at (loc, "array subscript is not an integer");
3084 return error_mark_node;
3087 /* Apply integral promotions *after* noticing character types.
3088 (It is unclear why we do these promotions -- the standard
3089 does not say that we should. In fact, the natural thing would
3090 seem to be to convert IDX to ptrdiff_t; we're performing
3091 pointer arithmetic.) */
3092 idx = cp_perform_integral_promotions (idx, complain);
3094 /* An array that is indexed by a non-constant
3095 cannot be stored in a register; we must be able to do
3096 address arithmetic on its address.
3097 Likewise an array of elements of variable size. */
3098 if (TREE_CODE (idx) != INTEGER_CST
3099 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3100 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3101 != INTEGER_CST)))
3103 if (!cxx_mark_addressable (array))
3104 return error_mark_node;
3107 /* An array that is indexed by a constant value which is not within
3108 the array bounds cannot be stored in a register either; because we
3109 would get a crash in store_bit_field/extract_bit_field when trying
3110 to access a non-existent part of the register. */
3111 if (TREE_CODE (idx) == INTEGER_CST
3112 && TYPE_DOMAIN (TREE_TYPE (array))
3113 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3115 if (!cxx_mark_addressable (array))
3116 return error_mark_node;
3119 if (!lvalue_p (array) && (complain & tf_error))
3120 pedwarn (loc, OPT_Wpedantic,
3121 "ISO C++ forbids subscripting non-lvalue array");
3123 /* Note in C++ it is valid to subscript a `register' array, since
3124 it is valid to take the address of something with that
3125 storage specification. */
3126 if (extra_warnings)
3128 tree foo = array;
3129 while (TREE_CODE (foo) == COMPONENT_REF)
3130 foo = TREE_OPERAND (foo, 0);
3131 if (VAR_P (foo) && DECL_REGISTER (foo)
3132 && (complain & tf_warning))
3133 warning_at (loc, OPT_Wextra,
3134 "subscripting array declared %<register%>");
3137 type = TREE_TYPE (TREE_TYPE (array));
3138 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3139 /* Array ref is const/volatile if the array elements are
3140 or if the array is.. */
3141 TREE_READONLY (rval)
3142 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3143 TREE_SIDE_EFFECTS (rval)
3144 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3145 TREE_THIS_VOLATILE (rval)
3146 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3147 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3148 complain);
3149 protected_set_expr_location (ret, loc);
3150 return ret;
3154 tree ar = cp_default_conversion (array, complain);
3155 tree ind = cp_default_conversion (idx, complain);
3157 /* Put the integer in IND to simplify error checking. */
3158 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3160 tree temp = ar;
3161 ar = ind;
3162 ind = temp;
3165 if (ar == error_mark_node || ind == error_mark_node)
3166 return error_mark_node;
3168 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3170 if (complain & tf_error)
3171 error_at (loc, "subscripted value is neither array nor pointer");
3172 return error_mark_node;
3174 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3176 if (complain & tf_error)
3177 error_at (loc, "array subscript is not an integer");
3178 return error_mark_node;
3181 warn_array_subscript_with_type_char (idx);
3183 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3184 PLUS_EXPR, ar, ind,
3185 complain),
3186 RO_ARRAY_INDEXING,
3187 complain);
3188 protected_set_expr_location (ret, loc);
3189 return ret;
3193 /* Entry point for Obj-C++. */
3195 tree
3196 build_array_ref (location_t loc, tree array, tree idx)
3198 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3201 /* Resolve a pointer to member function. INSTANCE is the object
3202 instance to use, if the member points to a virtual member.
3204 This used to avoid checking for virtual functions if basetype
3205 has no virtual functions, according to an earlier ANSI draft.
3206 With the final ISO C++ rules, such an optimization is
3207 incorrect: A pointer to a derived member can be static_cast
3208 to pointer-to-base-member, as long as the dynamic object
3209 later has the right member. So now we only do this optimization
3210 when we know the dynamic type of the object. */
3212 tree
3213 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3214 tsubst_flags_t complain)
3216 if (TREE_CODE (function) == OFFSET_REF)
3217 function = TREE_OPERAND (function, 1);
3219 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3221 tree idx, delta, e1, e2, e3, vtbl;
3222 bool nonvirtual;
3223 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3224 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3226 tree instance_ptr = *instance_ptrptr;
3227 tree instance_save_expr = 0;
3228 if (instance_ptr == error_mark_node)
3230 if (TREE_CODE (function) == PTRMEM_CST)
3232 /* Extracting the function address from a pmf is only
3233 allowed with -Wno-pmf-conversions. It only works for
3234 pmf constants. */
3235 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3236 e1 = convert (fntype, e1);
3237 return e1;
3239 else
3241 if (complain & tf_error)
3242 error ("object missing in use of %qE", function);
3243 return error_mark_node;
3247 /* True if we know that the dynamic type of the object doesn't have
3248 virtual functions, so we can assume the PFN field is a pointer. */
3249 nonvirtual = (COMPLETE_TYPE_P (basetype)
3250 && !TYPE_POLYMORPHIC_P (basetype)
3251 && resolves_to_fixed_type_p (instance_ptr, 0));
3253 if (TREE_SIDE_EFFECTS (instance_ptr))
3254 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3256 if (TREE_SIDE_EFFECTS (function))
3257 function = save_expr (function);
3259 /* Start by extracting all the information from the PMF itself. */
3260 e3 = pfn_from_ptrmemfunc (function);
3261 delta = delta_from_ptrmemfunc (function);
3262 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3263 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3265 case ptrmemfunc_vbit_in_pfn:
3266 e1 = cp_build_binary_op (input_location,
3267 BIT_AND_EXPR, idx, integer_one_node,
3268 complain);
3269 idx = cp_build_binary_op (input_location,
3270 MINUS_EXPR, idx, integer_one_node,
3271 complain);
3272 if (idx == error_mark_node)
3273 return error_mark_node;
3274 break;
3276 case ptrmemfunc_vbit_in_delta:
3277 e1 = cp_build_binary_op (input_location,
3278 BIT_AND_EXPR, delta, integer_one_node,
3279 complain);
3280 delta = cp_build_binary_op (input_location,
3281 RSHIFT_EXPR, delta, integer_one_node,
3282 complain);
3283 if (delta == error_mark_node)
3284 return error_mark_node;
3285 break;
3287 default:
3288 gcc_unreachable ();
3291 if (e1 == error_mark_node)
3292 return error_mark_node;
3294 /* Convert down to the right base before using the instance. A
3295 special case is that in a pointer to member of class C, C may
3296 be incomplete. In that case, the function will of course be
3297 a member of C, and no conversion is required. In fact,
3298 lookup_base will fail in that case, because incomplete
3299 classes do not have BINFOs. */
3300 if (!same_type_ignoring_top_level_qualifiers_p
3301 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3303 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3304 basetype, ba_check, NULL, complain);
3305 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3306 1, complain);
3307 if (instance_ptr == error_mark_node)
3308 return error_mark_node;
3310 /* ...and then the delta in the PMF. */
3311 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3313 /* Hand back the adjusted 'this' argument to our caller. */
3314 *instance_ptrptr = instance_ptr;
3316 if (nonvirtual)
3317 /* Now just return the pointer. */
3318 return e3;
3320 /* Next extract the vtable pointer from the object. */
3321 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3322 instance_ptr);
3323 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3324 if (vtbl == error_mark_node)
3325 return error_mark_node;
3327 /* Finally, extract the function pointer from the vtable. */
3328 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3329 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3330 if (e2 == error_mark_node)
3331 return error_mark_node;
3332 TREE_CONSTANT (e2) = 1;
3334 /* When using function descriptors, the address of the
3335 vtable entry is treated as a function pointer. */
3336 if (TARGET_VTABLE_USES_DESCRIPTORS)
3337 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3338 cp_build_addr_expr (e2, complain));
3340 e2 = fold_convert (TREE_TYPE (e3), e2);
3341 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3342 if (e1 == error_mark_node)
3343 return error_mark_node;
3345 /* Make sure this doesn't get evaluated first inside one of the
3346 branches of the COND_EXPR. */
3347 if (instance_save_expr)
3348 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3349 instance_save_expr, e1);
3351 function = e1;
3353 return function;
3356 /* Used by the C-common bits. */
3357 tree
3358 build_function_call (location_t /*loc*/,
3359 tree function, tree params)
3361 return cp_build_function_call (function, params, tf_warning_or_error);
3364 /* Used by the C-common bits. */
3365 tree
3366 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3367 tree function, vec<tree, va_gc> *params,
3368 vec<tree, va_gc> * /*origtypes*/)
3370 vec<tree, va_gc> *orig_params = params;
3371 tree ret = cp_build_function_call_vec (function, &params,
3372 tf_warning_or_error);
3374 /* cp_build_function_call_vec can reallocate PARAMS by adding
3375 default arguments. That should never happen here. Verify
3376 that. */
3377 gcc_assert (params == orig_params);
3379 return ret;
3382 /* Build a function call using a tree list of arguments. */
3384 tree
3385 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3387 vec<tree, va_gc> *vec;
3388 tree ret;
3390 vec = make_tree_vector ();
3391 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3392 vec_safe_push (vec, TREE_VALUE (params));
3393 ret = cp_build_function_call_vec (function, &vec, complain);
3394 release_tree_vector (vec);
3395 return ret;
3398 /* Build a function call using varargs. */
3400 tree
3401 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3403 vec<tree, va_gc> *vec;
3404 va_list args;
3405 tree ret, t;
3407 vec = make_tree_vector ();
3408 va_start (args, complain);
3409 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3410 vec_safe_push (vec, t);
3411 va_end (args);
3412 ret = cp_build_function_call_vec (function, &vec, complain);
3413 release_tree_vector (vec);
3414 return ret;
3417 /* Build a function call using a vector of arguments. PARAMS may be
3418 NULL if there are no parameters. This changes the contents of
3419 PARAMS. */
3421 tree
3422 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3423 tsubst_flags_t complain)
3425 tree fntype, fndecl;
3426 int is_method;
3427 tree original = function;
3428 int nargs;
3429 tree *argarray;
3430 tree parm_types;
3431 vec<tree, va_gc> *allocated = NULL;
3432 tree ret;
3434 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3435 expressions, like those used for ObjC messenger dispatches. */
3436 if (params != NULL && !vec_safe_is_empty (*params))
3437 function = objc_rewrite_function_call (function, (**params)[0]);
3439 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3440 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3441 if (TREE_CODE (function) == NOP_EXPR
3442 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3443 function = TREE_OPERAND (function, 0);
3445 if (TREE_CODE (function) == FUNCTION_DECL)
3447 // If the function is a non-template member function or a
3448 // non-template friend, then we need to check the constraints.
3450 // Note that if overload resolution failed with a single candidate
3451 // this function will be used to explicitly diagnose the failure
3452 // for the single call expression. The check is technically
3453 // redundant since we would have failed in add_function_candidate.
3454 if (tree ti = DECL_TEMPLATE_INFO (function))
3456 tree tmpl = TI_TEMPLATE (ti);
3457 tree args = TI_ARGS (ti);
3458 if (!check_template_constraints (tmpl, args))
3460 location_t loc = DECL_SOURCE_LOCATION (function);
3461 error ("%qD is not a viable candidate", function);
3462 diagnose_constraints (loc, tmpl, args);
3463 return error_mark_node;
3467 mark_used (function);
3468 fndecl = function;
3470 /* Convert anything with function type to a pointer-to-function. */
3471 if (DECL_MAIN_P (function) && (complain & tf_error))
3472 pedwarn (input_location, OPT_Wpedantic,
3473 "ISO C++ forbids calling %<::main%> from within program");
3475 function = build_addr_func (function, complain);
3477 else
3479 fndecl = NULL_TREE;
3481 function = build_addr_func (function, complain);
3484 if (function == error_mark_node)
3485 return error_mark_node;
3487 fntype = TREE_TYPE (function);
3489 if (TYPE_PTRMEMFUNC_P (fntype))
3491 if (complain & tf_error)
3492 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3493 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3494 original, original);
3495 return error_mark_node;
3498 is_method = (TYPE_PTR_P (fntype)
3499 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3501 if (!(TYPE_PTRFN_P (fntype)
3502 || is_method
3503 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3505 if (complain & tf_error)
3507 if (!flag_diagnostics_show_caret)
3508 error_at (input_location,
3509 "%qE cannot be used as a function", original);
3510 else if (DECL_P (original))
3511 error_at (input_location,
3512 "%qD cannot be used as a function", original);
3513 else
3514 error_at (input_location,
3515 "expression cannot be used as a function");
3518 return error_mark_node;
3521 /* fntype now gets the type of function pointed to. */
3522 fntype = TREE_TYPE (fntype);
3523 parm_types = TYPE_ARG_TYPES (fntype);
3525 if (params == NULL)
3527 allocated = make_tree_vector ();
3528 params = &allocated;
3531 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3532 complain);
3533 if (nargs < 0)
3534 return error_mark_node;
3536 argarray = (*params)->address ();
3538 /* Check for errors in format strings and inappropriately
3539 null parameters. */
3540 check_function_arguments (fntype, nargs, argarray);
3542 ret = build_cxx_call (function, nargs, argarray, complain);
3544 if (allocated != NULL)
3545 release_tree_vector (allocated);
3547 return ret;
3550 /* Subroutine of convert_arguments.
3551 Warn about wrong number of args are genereted. */
3553 static void
3554 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3556 if (fndecl)
3558 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3560 if (DECL_NAME (fndecl) == NULL_TREE
3561 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3562 error_at (loc,
3563 too_many_p
3564 ? G_("too many arguments to constructor %q#D")
3565 : G_("too few arguments to constructor %q#D"),
3566 fndecl);
3567 else
3568 error_at (loc,
3569 too_many_p
3570 ? G_("too many arguments to member function %q#D")
3571 : G_("too few arguments to member function %q#D"),
3572 fndecl);
3574 else
3575 error_at (loc,
3576 too_many_p
3577 ? G_("too many arguments to function %q#D")
3578 : G_("too few arguments to function %q#D"),
3579 fndecl);
3580 inform (DECL_SOURCE_LOCATION (fndecl),
3581 "declared here");
3583 else
3585 if (c_dialect_objc () && objc_message_selector ())
3586 error_at (loc,
3587 too_many_p
3588 ? G_("too many arguments to method %q#D")
3589 : G_("too few arguments to method %q#D"),
3590 objc_message_selector ());
3591 else
3592 error_at (loc, too_many_p ? G_("too many arguments to function")
3593 : G_("too few arguments to function"));
3597 /* Convert the actual parameter expressions in the list VALUES to the
3598 types in the list TYPELIST. The converted expressions are stored
3599 back in the VALUES vector.
3600 If parmdecls is exhausted, or when an element has NULL as its type,
3601 perform the default conversions.
3603 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3605 This is also where warnings about wrong number of args are generated.
3607 Returns the actual number of arguments processed (which might be less
3608 than the length of the vector), or -1 on error.
3610 In C++, unspecified trailing parameters can be filled in with their
3611 default arguments, if such were specified. Do so here. */
3613 static int
3614 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3615 int flags, tsubst_flags_t complain)
3617 tree typetail;
3618 unsigned int i;
3620 /* Argument passing is always copy-initialization. */
3621 flags |= LOOKUP_ONLYCONVERTING;
3623 for (i = 0, typetail = typelist;
3624 i < vec_safe_length (*values);
3625 i++)
3627 tree type = typetail ? TREE_VALUE (typetail) : 0;
3628 tree val = (**values)[i];
3630 if (val == error_mark_node || type == error_mark_node)
3631 return -1;
3633 if (type == void_type_node)
3635 if (complain & tf_error)
3637 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3638 return i;
3640 else
3641 return -1;
3644 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3645 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3646 if (TREE_CODE (val) == NOP_EXPR
3647 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3648 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3649 val = TREE_OPERAND (val, 0);
3651 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3653 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3654 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3655 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3656 val = decay_conversion (val, complain);
3659 if (val == error_mark_node)
3660 return -1;
3662 if (type != 0)
3664 /* Formal parm type is specified by a function prototype. */
3665 tree parmval;
3667 if (!COMPLETE_TYPE_P (complete_type (type)))
3669 if (complain & tf_error)
3671 if (fndecl)
3672 error ("parameter %P of %qD has incomplete type %qT",
3673 i, fndecl, type);
3674 else
3675 error ("parameter %P has incomplete type %qT", i, type);
3677 parmval = error_mark_node;
3679 else
3681 parmval = convert_for_initialization
3682 (NULL_TREE, type, val, flags,
3683 ICR_ARGPASS, fndecl, i, complain);
3684 parmval = convert_for_arg_passing (type, parmval, complain);
3687 if (parmval == error_mark_node)
3688 return -1;
3690 (**values)[i] = parmval;
3692 else
3694 if (fndecl && magic_varargs_p (fndecl))
3695 /* Don't do ellipsis conversion for __built_in_constant_p
3696 as this will result in spurious errors for non-trivial
3697 types. */
3698 val = require_complete_type_sfinae (val, complain);
3699 else
3700 val = convert_arg_to_ellipsis (val, complain);
3702 (**values)[i] = val;
3705 if (typetail)
3706 typetail = TREE_CHAIN (typetail);
3709 if (typetail != 0 && typetail != void_list_node)
3711 /* See if there are default arguments that can be used. Because
3712 we hold default arguments in the FUNCTION_TYPE (which is so
3713 wrong), we can see default parameters here from deduced
3714 contexts (and via typeof) for indirect function calls.
3715 Fortunately we know whether we have a function decl to
3716 provide default arguments in a language conformant
3717 manner. */
3718 if (fndecl && TREE_PURPOSE (typetail)
3719 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3721 for (; typetail != void_list_node; ++i)
3723 tree parmval
3724 = convert_default_arg (TREE_VALUE (typetail),
3725 TREE_PURPOSE (typetail),
3726 fndecl, i, complain);
3728 if (parmval == error_mark_node)
3729 return -1;
3731 vec_safe_push (*values, parmval);
3732 typetail = TREE_CHAIN (typetail);
3733 /* ends with `...'. */
3734 if (typetail == NULL_TREE)
3735 break;
3738 else
3740 if (complain & tf_error)
3741 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3742 return -1;
3746 return (int) i;
3749 /* Build a binary-operation expression, after performing default
3750 conversions on the operands. CODE is the kind of expression to
3751 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3752 are the tree codes which correspond to ARG1 and ARG2 when issuing
3753 warnings about possibly misplaced parentheses. They may differ
3754 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3755 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3756 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3757 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3758 ARG2_CODE as ERROR_MARK. */
3760 tree
3761 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3762 enum tree_code arg1_code, tree arg2,
3763 enum tree_code arg2_code, tree *overload,
3764 tsubst_flags_t complain)
3766 tree orig_arg1;
3767 tree orig_arg2;
3768 tree expr;
3770 orig_arg1 = arg1;
3771 orig_arg2 = arg2;
3773 if (processing_template_decl)
3775 if (type_dependent_expression_p (arg1)
3776 || type_dependent_expression_p (arg2))
3777 return build_min_nt_loc (loc, code, arg1, arg2);
3778 arg1 = build_non_dependent_expr (arg1);
3779 arg2 = build_non_dependent_expr (arg2);
3782 if (code == DOTSTAR_EXPR)
3783 expr = build_m_component_ref (arg1, arg2, complain);
3784 else
3785 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3786 overload, complain);
3788 /* Check for cases such as x+y<<z which users are likely to
3789 misinterpret. But don't warn about obj << x + y, since that is a
3790 common idiom for I/O. */
3791 if (warn_parentheses
3792 && (complain & tf_warning)
3793 && !processing_template_decl
3794 && !error_operand_p (arg1)
3795 && !error_operand_p (arg2)
3796 && (code != LSHIFT_EXPR
3797 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3798 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3799 arg2_code, orig_arg2);
3801 if (processing_template_decl && expr != error_mark_node)
3802 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3804 return expr;
3807 /* Build and return an ARRAY_REF expression. */
3809 tree
3810 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3811 tsubst_flags_t complain)
3813 tree orig_arg1 = arg1;
3814 tree orig_arg2 = arg2;
3815 tree expr;
3817 if (processing_template_decl)
3819 if (type_dependent_expression_p (arg1)
3820 || type_dependent_expression_p (arg2))
3821 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3822 NULL_TREE, NULL_TREE);
3823 arg1 = build_non_dependent_expr (arg1);
3824 arg2 = build_non_dependent_expr (arg2);
3827 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3828 NULL_TREE, /*overload=*/NULL, complain);
3830 if (processing_template_decl && expr != error_mark_node)
3831 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3832 NULL_TREE, NULL_TREE);
3833 return expr;
3836 /* Return whether OP is an expression of enum type cast to integer
3837 type. In C++ even unsigned enum types are cast to signed integer
3838 types. We do not want to issue warnings about comparisons between
3839 signed and unsigned types when one of the types is an enum type.
3840 Those warnings are always false positives in practice. */
3842 static bool
3843 enum_cast_to_int (tree op)
3845 if (TREE_CODE (op) == NOP_EXPR
3846 && TREE_TYPE (op) == integer_type_node
3847 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3848 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3849 return true;
3851 /* The cast may have been pushed into a COND_EXPR. */
3852 if (TREE_CODE (op) == COND_EXPR)
3853 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3854 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3856 return false;
3859 /* For the c-common bits. */
3860 tree
3861 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3862 int /*convert_p*/)
3864 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3868 /* Build a binary-operation expression without default conversions.
3869 CODE is the kind of expression to build.
3870 LOCATION is the location_t of the operator in the source code.
3871 This function differs from `build' in several ways:
3872 the data type of the result is computed and recorded in it,
3873 warnings are generated if arg data types are invalid,
3874 special handling for addition and subtraction of pointers is known,
3875 and some optimization is done (operations on narrow ints
3876 are done in the narrower type when that gives the same result).
3877 Constant folding is also done before the result is returned.
3879 Note that the operands will never have enumeral types
3880 because either they have just had the default conversions performed
3881 or they have both just been converted to some other type in which
3882 the arithmetic is to be done.
3884 C++: must do special pointer arithmetic when implementing
3885 multiple inheritance, and deal with pointer to member functions. */
3887 tree
3888 cp_build_binary_op (location_t location,
3889 enum tree_code code, tree orig_op0, tree orig_op1,
3890 tsubst_flags_t complain)
3892 tree op0, op1;
3893 enum tree_code code0, code1;
3894 tree type0, type1;
3895 const char *invalid_op_diag;
3897 /* Expression code to give to the expression when it is built.
3898 Normally this is CODE, which is what the caller asked for,
3899 but in some special cases we change it. */
3900 enum tree_code resultcode = code;
3902 /* Data type in which the computation is to be performed.
3903 In the simplest cases this is the common type of the arguments. */
3904 tree result_type = NULL;
3906 /* Nonzero means operands have already been type-converted
3907 in whatever way is necessary.
3908 Zero means they need to be converted to RESULT_TYPE. */
3909 int converted = 0;
3911 /* Nonzero means create the expression with this type, rather than
3912 RESULT_TYPE. */
3913 tree build_type = 0;
3915 /* Nonzero means after finally constructing the expression
3916 convert it to this type. */
3917 tree final_type = 0;
3919 tree result;
3920 tree orig_type = NULL;
3922 /* Nonzero if this is an operation like MIN or MAX which can
3923 safely be computed in short if both args are promoted shorts.
3924 Also implies COMMON.
3925 -1 indicates a bitwise operation; this makes a difference
3926 in the exact conditions for when it is safe to do the operation
3927 in a narrower mode. */
3928 int shorten = 0;
3930 /* Nonzero if this is a comparison operation;
3931 if both args are promoted shorts, compare the original shorts.
3932 Also implies COMMON. */
3933 int short_compare = 0;
3935 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3936 int common = 0;
3938 /* True if both operands have arithmetic type. */
3939 bool arithmetic_types_p;
3941 /* Apply default conversions. */
3942 op0 = orig_op0;
3943 op1 = orig_op1;
3945 /* Remember whether we're doing / or %. */
3946 bool doing_div_or_mod = false;
3948 /* Remember whether we're doing << or >>. */
3949 bool doing_shift = false;
3951 /* Tree holding instrumentation expression. */
3952 tree instrument_expr = NULL;
3954 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3955 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3956 || code == TRUTH_XOR_EXPR)
3958 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3959 op0 = decay_conversion (op0, complain);
3960 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3961 op1 = decay_conversion (op1, complain);
3963 else
3965 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3966 op0 = cp_default_conversion (op0, complain);
3967 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3968 op1 = cp_default_conversion (op1, complain);
3971 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3972 STRIP_TYPE_NOPS (op0);
3973 STRIP_TYPE_NOPS (op1);
3975 /* DTRT if one side is an overloaded function, but complain about it. */
3976 if (type_unknown_p (op0))
3978 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3979 if (t != error_mark_node)
3981 if (complain & tf_error)
3982 permerror (input_location, "assuming cast to type %qT from overloaded function",
3983 TREE_TYPE (t));
3984 op0 = t;
3987 if (type_unknown_p (op1))
3989 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3990 if (t != error_mark_node)
3992 if (complain & tf_error)
3993 permerror (input_location, "assuming cast to type %qT from overloaded function",
3994 TREE_TYPE (t));
3995 op1 = t;
3999 type0 = TREE_TYPE (op0);
4000 type1 = TREE_TYPE (op1);
4002 /* The expression codes of the data types of the arguments tell us
4003 whether the arguments are integers, floating, pointers, etc. */
4004 code0 = TREE_CODE (type0);
4005 code1 = TREE_CODE (type1);
4007 /* If an error was already reported for one of the arguments,
4008 avoid reporting another error. */
4009 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4010 return error_mark_node;
4012 if ((invalid_op_diag
4013 = targetm.invalid_binary_op (code, type0, type1)))
4015 if (complain & tf_error)
4016 error (invalid_op_diag);
4017 return error_mark_node;
4020 /* Issue warnings about peculiar, but valid, uses of NULL. */
4021 if ((orig_op0 == null_node || orig_op1 == null_node)
4022 /* It's reasonable to use pointer values as operands of &&
4023 and ||, so NULL is no exception. */
4024 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4025 && ( /* Both are NULL (or 0) and the operation was not a
4026 comparison or a pointer subtraction. */
4027 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4028 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4029 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4030 || (!null_ptr_cst_p (orig_op0)
4031 && !TYPE_PTR_OR_PTRMEM_P (type0))
4032 || (!null_ptr_cst_p (orig_op1)
4033 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4034 && (complain & tf_warning))
4036 source_location loc =
4037 expansion_point_location_if_in_system_header (input_location);
4039 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4042 /* In case when one of the operands of the binary operation is
4043 a vector and another is a scalar -- convert scalar to vector. */
4044 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4046 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4047 complain & tf_error);
4049 switch (convert_flag)
4051 case stv_error:
4052 return error_mark_node;
4053 case stv_firstarg:
4055 op0 = save_expr (op0);
4056 op0 = convert (TREE_TYPE (type1), op0);
4057 op0 = build_vector_from_val (type1, op0);
4058 type0 = TREE_TYPE (op0);
4059 code0 = TREE_CODE (type0);
4060 converted = 1;
4061 break;
4063 case stv_secondarg:
4065 op1 = save_expr (op1);
4066 op1 = convert (TREE_TYPE (type0), op1);
4067 op1 = build_vector_from_val (type0, op1);
4068 type1 = TREE_TYPE (op1);
4069 code1 = TREE_CODE (type1);
4070 converted = 1;
4071 break;
4073 default:
4074 break;
4078 switch (code)
4080 case MINUS_EXPR:
4081 /* Subtraction of two similar pointers.
4082 We must subtract them as integers, then divide by object size. */
4083 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4084 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4085 TREE_TYPE (type1)))
4086 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4087 complain);
4088 /* In all other cases except pointer - int, the usual arithmetic
4089 rules apply. */
4090 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4092 common = 1;
4093 break;
4095 /* The pointer - int case is just like pointer + int; fall
4096 through. */
4097 case PLUS_EXPR:
4098 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4099 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4101 tree ptr_operand;
4102 tree int_operand;
4103 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4104 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4105 if (processing_template_decl)
4107 result_type = TREE_TYPE (ptr_operand);
4108 break;
4110 return cp_pointer_int_sum (code,
4111 ptr_operand,
4112 int_operand,
4113 complain);
4115 common = 1;
4116 break;
4118 case MULT_EXPR:
4119 common = 1;
4120 break;
4122 case TRUNC_DIV_EXPR:
4123 case CEIL_DIV_EXPR:
4124 case FLOOR_DIV_EXPR:
4125 case ROUND_DIV_EXPR:
4126 case EXACT_DIV_EXPR:
4127 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4128 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4129 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4130 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4132 enum tree_code tcode0 = code0, tcode1 = code1;
4133 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4134 cop1 = maybe_constant_value (cop1);
4136 if (tcode0 == INTEGER_TYPE)
4137 doing_div_or_mod = true;
4139 warn_for_div_by_zero (location, cop1);
4141 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4142 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4143 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4144 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4146 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4147 resultcode = RDIV_EXPR;
4148 else
4149 /* When dividing two signed integers, we have to promote to int.
4150 unless we divide by a constant != -1. Note that default
4151 conversion will have been performed on the operands at this
4152 point, so we have to dig out the original type to find out if
4153 it was unsigned. */
4154 shorten = ((TREE_CODE (op0) == NOP_EXPR
4155 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4156 || (TREE_CODE (op1) == INTEGER_CST
4157 && ! integer_all_onesp (op1)));
4159 common = 1;
4161 break;
4163 case BIT_AND_EXPR:
4164 case BIT_IOR_EXPR:
4165 case BIT_XOR_EXPR:
4166 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4167 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4168 && !VECTOR_FLOAT_TYPE_P (type0)
4169 && !VECTOR_FLOAT_TYPE_P (type1)))
4170 shorten = -1;
4171 break;
4173 case TRUNC_MOD_EXPR:
4174 case FLOOR_MOD_EXPR:
4176 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4177 cop1 = maybe_constant_value (cop1);
4179 if (code0 == INTEGER_TYPE)
4180 doing_div_or_mod = true;
4181 warn_for_div_by_zero (location, cop1);
4184 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4185 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4186 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4187 common = 1;
4188 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4190 /* Although it would be tempting to shorten always here, that loses
4191 on some targets, since the modulo instruction is undefined if the
4192 quotient can't be represented in the computation mode. We shorten
4193 only if unsigned or if dividing by something we know != -1. */
4194 shorten = ((TREE_CODE (op0) == NOP_EXPR
4195 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4196 || (TREE_CODE (op1) == INTEGER_CST
4197 && ! integer_all_onesp (op1)));
4198 common = 1;
4200 break;
4202 case TRUTH_ANDIF_EXPR:
4203 case TRUTH_ORIF_EXPR:
4204 case TRUTH_AND_EXPR:
4205 case TRUTH_OR_EXPR:
4206 if (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1))
4208 sorry ("logical operation on vector type");
4209 return error_mark_node;
4211 result_type = boolean_type_node;
4212 break;
4214 /* Shift operations: result has same type as first operand;
4215 always convert second operand to int.
4216 Also set SHORT_SHIFT if shifting rightward. */
4218 case RSHIFT_EXPR:
4219 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4220 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4222 result_type = type0;
4223 converted = 1;
4225 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4226 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4227 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4228 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4230 result_type = type0;
4231 converted = 1;
4233 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4235 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4236 const_op1 = maybe_constant_value (const_op1);
4237 if (TREE_CODE (const_op1) != INTEGER_CST)
4238 const_op1 = op1;
4239 result_type = type0;
4240 doing_shift = true;
4241 if (TREE_CODE (const_op1) == INTEGER_CST)
4243 if (tree_int_cst_lt (const_op1, integer_zero_node))
4245 if ((complain & tf_warning)
4246 && c_inhibit_evaluation_warnings == 0)
4247 warning (0, "right shift count is negative");
4249 else
4251 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4252 && (complain & tf_warning)
4253 && c_inhibit_evaluation_warnings == 0)
4254 warning (0, "right shift count >= width of type");
4257 /* Convert the shift-count to an integer, regardless of
4258 size of value being shifted. */
4259 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4260 op1 = cp_convert (integer_type_node, op1, complain);
4261 /* Avoid converting op1 to result_type later. */
4262 converted = 1;
4264 break;
4266 case LSHIFT_EXPR:
4267 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4268 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4270 result_type = type0;
4271 converted = 1;
4273 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4274 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4275 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4276 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4278 result_type = type0;
4279 converted = 1;
4281 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4283 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4284 const_op1 = maybe_constant_value (const_op1);
4285 if (TREE_CODE (const_op1) != INTEGER_CST)
4286 const_op1 = op1;
4287 result_type = type0;
4288 doing_shift = true;
4289 if (TREE_CODE (const_op1) == INTEGER_CST)
4291 if (tree_int_cst_lt (const_op1, integer_zero_node))
4293 if ((complain & tf_warning)
4294 && c_inhibit_evaluation_warnings == 0)
4295 warning (0, "left shift count is negative");
4297 else if (compare_tree_int (const_op1,
4298 TYPE_PRECISION (type0)) >= 0)
4300 if ((complain & tf_warning)
4301 && c_inhibit_evaluation_warnings == 0)
4302 warning (0, "left shift count >= width of type");
4305 /* Convert the shift-count to an integer, regardless of
4306 size of value being shifted. */
4307 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4308 op1 = cp_convert (integer_type_node, op1, complain);
4309 /* Avoid converting op1 to result_type later. */
4310 converted = 1;
4312 break;
4314 case RROTATE_EXPR:
4315 case LROTATE_EXPR:
4316 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4318 result_type = type0;
4319 if (TREE_CODE (op1) == INTEGER_CST)
4321 if (tree_int_cst_lt (op1, integer_zero_node))
4323 if (complain & tf_warning)
4324 warning (0, (code == LROTATE_EXPR)
4325 ? G_("left rotate count is negative")
4326 : G_("right rotate count is negative"));
4328 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4330 if (complain & tf_warning)
4331 warning (0, (code == LROTATE_EXPR)
4332 ? G_("left rotate count >= width of type")
4333 : G_("right rotate count >= width of type"));
4336 /* Convert the shift-count to an integer, regardless of
4337 size of value being shifted. */
4338 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4339 op1 = cp_convert (integer_type_node, op1, complain);
4341 break;
4343 case EQ_EXPR:
4344 case NE_EXPR:
4345 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4346 goto vector_compare;
4347 if ((complain & tf_warning)
4348 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4349 warning (OPT_Wfloat_equal,
4350 "comparing floating point with == or != is unsafe");
4351 if ((complain & tf_warning)
4352 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4353 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4354 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4356 build_type = boolean_type_node;
4357 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4358 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4359 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4360 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4361 short_compare = 1;
4362 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4363 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4364 result_type = composite_pointer_type (type0, type1, op0, op1,
4365 CPO_COMPARISON, complain);
4366 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4367 && null_ptr_cst_p (op1))
4369 if (TREE_CODE (op0) == ADDR_EXPR
4370 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4372 if ((complain & tf_warning)
4373 && c_inhibit_evaluation_warnings == 0)
4374 warning (OPT_Waddress, "the address of %qD will never be NULL",
4375 TREE_OPERAND (op0, 0));
4377 result_type = type0;
4379 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4380 && null_ptr_cst_p (op0))
4382 if (TREE_CODE (op1) == ADDR_EXPR
4383 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4385 if ((complain & tf_warning)
4386 && c_inhibit_evaluation_warnings == 0)
4387 warning (OPT_Waddress, "the address of %qD will never be NULL",
4388 TREE_OPERAND (op1, 0));
4390 result_type = type1;
4392 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4393 /* One of the operands must be of nullptr_t type. */
4394 result_type = TREE_TYPE (nullptr_node);
4395 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4397 result_type = type0;
4398 if (complain & tf_error)
4399 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4400 else
4401 return error_mark_node;
4403 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4405 result_type = type1;
4406 if (complain & tf_error)
4407 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4408 else
4409 return error_mark_node;
4411 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4413 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4414 == ptrmemfunc_vbit_in_delta)
4416 tree pfn0, delta0, e1, e2;
4418 if (TREE_SIDE_EFFECTS (op0))
4419 op0 = save_expr (op0);
4421 pfn0 = pfn_from_ptrmemfunc (op0);
4422 delta0 = delta_from_ptrmemfunc (op0);
4423 e1 = cp_build_binary_op (location,
4424 EQ_EXPR,
4425 pfn0,
4426 build_zero_cst (TREE_TYPE (pfn0)),
4427 complain);
4428 e2 = cp_build_binary_op (location,
4429 BIT_AND_EXPR,
4430 delta0,
4431 integer_one_node,
4432 complain);
4434 if (complain & tf_warning)
4435 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4437 e2 = cp_build_binary_op (location,
4438 EQ_EXPR, e2, integer_zero_node,
4439 complain);
4440 op0 = cp_build_binary_op (location,
4441 TRUTH_ANDIF_EXPR, e1, e2,
4442 complain);
4443 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4445 else
4447 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4448 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4450 result_type = TREE_TYPE (op0);
4452 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4453 return cp_build_binary_op (location, code, op1, op0, complain);
4454 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4456 tree type;
4457 /* E will be the final comparison. */
4458 tree e;
4459 /* E1 and E2 are for scratch. */
4460 tree e1;
4461 tree e2;
4462 tree pfn0;
4463 tree pfn1;
4464 tree delta0;
4465 tree delta1;
4467 type = composite_pointer_type (type0, type1, op0, op1,
4468 CPO_COMPARISON, complain);
4470 if (!same_type_p (TREE_TYPE (op0), type))
4471 op0 = cp_convert_and_check (type, op0, complain);
4472 if (!same_type_p (TREE_TYPE (op1), type))
4473 op1 = cp_convert_and_check (type, op1, complain);
4475 if (op0 == error_mark_node || op1 == error_mark_node)
4476 return error_mark_node;
4478 if (TREE_SIDE_EFFECTS (op0))
4479 op0 = save_expr (op0);
4480 if (TREE_SIDE_EFFECTS (op1))
4481 op1 = save_expr (op1);
4483 pfn0 = pfn_from_ptrmemfunc (op0);
4484 pfn1 = pfn_from_ptrmemfunc (op1);
4485 delta0 = delta_from_ptrmemfunc (op0);
4486 delta1 = delta_from_ptrmemfunc (op1);
4487 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4488 == ptrmemfunc_vbit_in_delta)
4490 /* We generate:
4492 (op0.pfn == op1.pfn
4493 && ((op0.delta == op1.delta)
4494 || (!op0.pfn && op0.delta & 1 == 0
4495 && op1.delta & 1 == 0))
4497 The reason for the `!op0.pfn' bit is that a NULL
4498 pointer-to-member is any member with a zero PFN and
4499 LSB of the DELTA field is 0. */
4501 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4502 delta0,
4503 integer_one_node,
4504 complain);
4505 e1 = cp_build_binary_op (location,
4506 EQ_EXPR, e1, integer_zero_node,
4507 complain);
4508 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4509 delta1,
4510 integer_one_node,
4511 complain);
4512 e2 = cp_build_binary_op (location,
4513 EQ_EXPR, e2, integer_zero_node,
4514 complain);
4515 e1 = cp_build_binary_op (location,
4516 TRUTH_ANDIF_EXPR, e2, e1,
4517 complain);
4518 e2 = cp_build_binary_op (location, EQ_EXPR,
4519 pfn0,
4520 build_zero_cst (TREE_TYPE (pfn0)),
4521 complain);
4522 e2 = cp_build_binary_op (location,
4523 TRUTH_ANDIF_EXPR, e2, e1, complain);
4524 e1 = cp_build_binary_op (location,
4525 EQ_EXPR, delta0, delta1, complain);
4526 e1 = cp_build_binary_op (location,
4527 TRUTH_ORIF_EXPR, e1, e2, complain);
4529 else
4531 /* We generate:
4533 (op0.pfn == op1.pfn
4534 && (!op0.pfn || op0.delta == op1.delta))
4536 The reason for the `!op0.pfn' bit is that a NULL
4537 pointer-to-member is any member with a zero PFN; the
4538 DELTA field is unspecified. */
4540 e1 = cp_build_binary_op (location,
4541 EQ_EXPR, delta0, delta1, complain);
4542 e2 = cp_build_binary_op (location,
4543 EQ_EXPR,
4544 pfn0,
4545 build_zero_cst (TREE_TYPE (pfn0)),
4546 complain);
4547 e1 = cp_build_binary_op (location,
4548 TRUTH_ORIF_EXPR, e1, e2, complain);
4550 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4551 e = cp_build_binary_op (location,
4552 TRUTH_ANDIF_EXPR, e2, e1, complain);
4553 if (code == EQ_EXPR)
4554 return e;
4555 return cp_build_binary_op (location,
4556 EQ_EXPR, e, integer_zero_node, complain);
4558 else
4560 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4561 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4562 type1));
4563 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4564 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4565 type0));
4568 break;
4570 case MAX_EXPR:
4571 case MIN_EXPR:
4572 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4573 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4574 shorten = 1;
4575 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4576 result_type = composite_pointer_type (type0, type1, op0, op1,
4577 CPO_COMPARISON, complain);
4578 break;
4580 case LE_EXPR:
4581 case GE_EXPR:
4582 case LT_EXPR:
4583 case GT_EXPR:
4584 if (TREE_CODE (orig_op0) == STRING_CST
4585 || TREE_CODE (orig_op1) == STRING_CST)
4587 if (complain & tf_warning)
4588 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4591 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4593 vector_compare:
4594 tree intt;
4595 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4596 TREE_TYPE (type1))
4597 && !vector_types_compatible_elements_p (type0, type1))
4599 if (complain & tf_error)
4601 error_at (location, "comparing vectors with different "
4602 "element types");
4603 inform (location, "operand types are %qT and %qT",
4604 type0, type1);
4606 return error_mark_node;
4609 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4611 if (complain & tf_error)
4613 error_at (location, "comparing vectors with different "
4614 "number of elements");
4615 inform (location, "operand types are %qT and %qT",
4616 type0, type1);
4618 return error_mark_node;
4621 /* Always construct signed integer vector type. */
4622 intt = c_common_type_for_size (GET_MODE_BITSIZE
4623 (TYPE_MODE (TREE_TYPE (type0))), 0);
4624 if (!intt)
4626 if (complain & tf_error)
4627 error_at (location, "could not find an integer type "
4628 "of the same size as %qT", TREE_TYPE (type0));
4629 return error_mark_node;
4631 result_type = build_opaque_vector_type (intt,
4632 TYPE_VECTOR_SUBPARTS (type0));
4633 converted = 1;
4634 break;
4636 build_type = boolean_type_node;
4637 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4638 || code0 == ENUMERAL_TYPE)
4639 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4640 || code1 == ENUMERAL_TYPE))
4641 short_compare = 1;
4642 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4643 result_type = composite_pointer_type (type0, type1, op0, op1,
4644 CPO_COMPARISON, complain);
4645 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4647 result_type = type0;
4648 if (extra_warnings && (complain & tf_warning))
4649 warning (OPT_Wextra,
4650 "ordered comparison of pointer with integer zero");
4652 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4654 result_type = type1;
4655 if (extra_warnings && (complain & tf_warning))
4656 warning (OPT_Wextra,
4657 "ordered comparison of pointer with integer zero");
4659 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4660 /* One of the operands must be of nullptr_t type. */
4661 result_type = TREE_TYPE (nullptr_node);
4662 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4664 result_type = type0;
4665 if (complain & tf_error)
4666 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4667 else
4668 return error_mark_node;
4670 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4672 result_type = type1;
4673 if (complain & tf_error)
4674 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4675 else
4676 return error_mark_node;
4678 break;
4680 case UNORDERED_EXPR:
4681 case ORDERED_EXPR:
4682 case UNLT_EXPR:
4683 case UNLE_EXPR:
4684 case UNGT_EXPR:
4685 case UNGE_EXPR:
4686 case UNEQ_EXPR:
4687 build_type = integer_type_node;
4688 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4690 if (complain & tf_error)
4691 error ("unordered comparison on non-floating point argument");
4692 return error_mark_node;
4694 common = 1;
4695 break;
4697 default:
4698 break;
4701 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4702 || code0 == ENUMERAL_TYPE)
4703 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4704 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4705 arithmetic_types_p = 1;
4706 else
4708 arithmetic_types_p = 0;
4709 /* Vector arithmetic is only allowed when both sides are vectors. */
4710 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4712 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4713 || !vector_types_compatible_elements_p (type0, type1))
4715 if (complain & tf_error)
4716 binary_op_error (location, code, type0, type1);
4717 return error_mark_node;
4719 arithmetic_types_p = 1;
4722 /* Determine the RESULT_TYPE, if it is not already known. */
4723 if (!result_type
4724 && arithmetic_types_p
4725 && (shorten || common || short_compare))
4727 result_type = cp_common_type (type0, type1);
4728 if (complain & tf_warning)
4729 do_warn_double_promotion (result_type, type0, type1,
4730 "implicit conversion from %qT to %qT "
4731 "to match other operand of binary "
4732 "expression",
4733 location);
4736 if (!result_type)
4738 if (complain & tf_error)
4739 error ("invalid operands of types %qT and %qT to binary %qO",
4740 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4741 return error_mark_node;
4744 /* If we're in a template, the only thing we need to know is the
4745 RESULT_TYPE. */
4746 if (processing_template_decl)
4748 /* Since the middle-end checks the type when doing a build2, we
4749 need to build the tree in pieces. This built tree will never
4750 get out of the front-end as we replace it when instantiating
4751 the template. */
4752 tree tmp = build2 (resultcode,
4753 build_type ? build_type : result_type,
4754 NULL_TREE, op1);
4755 TREE_OPERAND (tmp, 0) = op0;
4756 return tmp;
4759 if (arithmetic_types_p)
4761 bool first_complex = (code0 == COMPLEX_TYPE);
4762 bool second_complex = (code1 == COMPLEX_TYPE);
4763 int none_complex = (!first_complex && !second_complex);
4765 /* Adapted from patch for c/24581. */
4766 if (first_complex != second_complex
4767 && (code == PLUS_EXPR
4768 || code == MINUS_EXPR
4769 || code == MULT_EXPR
4770 || (code == TRUNC_DIV_EXPR && first_complex))
4771 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4772 && flag_signed_zeros)
4774 /* An operation on mixed real/complex operands must be
4775 handled specially, but the language-independent code can
4776 more easily optimize the plain complex arithmetic if
4777 -fno-signed-zeros. */
4778 tree real_type = TREE_TYPE (result_type);
4779 tree real, imag;
4780 if (first_complex)
4782 if (TREE_TYPE (op0) != result_type)
4783 op0 = cp_convert_and_check (result_type, op0, complain);
4784 if (TREE_TYPE (op1) != real_type)
4785 op1 = cp_convert_and_check (real_type, op1, complain);
4787 else
4789 if (TREE_TYPE (op0) != real_type)
4790 op0 = cp_convert_and_check (real_type, op0, complain);
4791 if (TREE_TYPE (op1) != result_type)
4792 op1 = cp_convert_and_check (result_type, op1, complain);
4794 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4795 return error_mark_node;
4796 if (first_complex)
4798 op0 = save_expr (op0);
4799 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4800 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4801 switch (code)
4803 case MULT_EXPR:
4804 case TRUNC_DIV_EXPR:
4805 op1 = save_expr (op1);
4806 imag = build2 (resultcode, real_type, imag, op1);
4807 /* Fall through. */
4808 case PLUS_EXPR:
4809 case MINUS_EXPR:
4810 real = build2 (resultcode, real_type, real, op1);
4811 break;
4812 default:
4813 gcc_unreachable();
4816 else
4818 op1 = save_expr (op1);
4819 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4820 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4821 switch (code)
4823 case MULT_EXPR:
4824 op0 = save_expr (op0);
4825 imag = build2 (resultcode, real_type, op0, imag);
4826 /* Fall through. */
4827 case PLUS_EXPR:
4828 real = build2 (resultcode, real_type, op0, real);
4829 break;
4830 case MINUS_EXPR:
4831 real = build2 (resultcode, real_type, op0, real);
4832 imag = build1 (NEGATE_EXPR, real_type, imag);
4833 break;
4834 default:
4835 gcc_unreachable();
4838 real = fold_if_not_in_template (real);
4839 imag = fold_if_not_in_template (imag);
4840 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4841 result = fold_if_not_in_template (result);
4842 return result;
4845 /* For certain operations (which identify themselves by shorten != 0)
4846 if both args were extended from the same smaller type,
4847 do the arithmetic in that type and then extend.
4849 shorten !=0 and !=1 indicates a bitwise operation.
4850 For them, this optimization is safe only if
4851 both args are zero-extended or both are sign-extended.
4852 Otherwise, we might change the result.
4853 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4854 but calculated in (unsigned short) it would be (unsigned short)-1. */
4856 if (shorten && none_complex)
4858 orig_type = result_type;
4859 final_type = result_type;
4860 result_type = shorten_binary_op (result_type, op0, op1,
4861 shorten == -1);
4864 /* Comparison operations are shortened too but differently.
4865 They identify themselves by setting short_compare = 1. */
4867 if (short_compare)
4869 /* Don't write &op0, etc., because that would prevent op0
4870 from being kept in a register.
4871 Instead, make copies of the our local variables and
4872 pass the copies by reference, then copy them back afterward. */
4873 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4874 enum tree_code xresultcode = resultcode;
4875 tree val
4876 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4877 &xresultcode);
4878 if (val != 0)
4879 return cp_convert (boolean_type_node, val, complain);
4880 op0 = xop0, op1 = xop1;
4881 converted = 1;
4882 resultcode = xresultcode;
4885 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4886 && warn_sign_compare
4887 /* Do not warn until the template is instantiated; we cannot
4888 bound the ranges of the arguments until that point. */
4889 && !processing_template_decl
4890 && (complain & tf_warning)
4891 && c_inhibit_evaluation_warnings == 0
4892 /* Even unsigned enum types promote to signed int. We don't
4893 want to issue -Wsign-compare warnings for this case. */
4894 && !enum_cast_to_int (orig_op0)
4895 && !enum_cast_to_int (orig_op1))
4897 tree oop0 = maybe_constant_value (orig_op0);
4898 tree oop1 = maybe_constant_value (orig_op1);
4900 if (TREE_CODE (oop0) != INTEGER_CST)
4901 oop0 = orig_op0;
4902 if (TREE_CODE (oop1) != INTEGER_CST)
4903 oop1 = orig_op1;
4904 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4905 result_type, resultcode);
4909 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4910 Then the expression will be built.
4911 It will be given type FINAL_TYPE if that is nonzero;
4912 otherwise, it will be given type RESULT_TYPE. */
4913 if (! converted)
4915 if (TREE_TYPE (op0) != result_type)
4916 op0 = cp_convert_and_check (result_type, op0, complain);
4917 if (TREE_TYPE (op1) != result_type)
4918 op1 = cp_convert_and_check (result_type, op1, complain);
4920 if (op0 == error_mark_node || op1 == error_mark_node)
4921 return error_mark_node;
4924 if (build_type == NULL_TREE)
4925 build_type = result_type;
4927 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
4928 && !processing_template_decl
4929 && current_function_decl != 0
4930 && !lookup_attribute ("no_sanitize_undefined",
4931 DECL_ATTRIBUTES (current_function_decl))
4932 && (doing_div_or_mod || doing_shift))
4934 /* OP0 and/or OP1 might have side-effects. */
4935 op0 = cp_save_expr (op0);
4936 op1 = cp_save_expr (op1);
4937 op0 = maybe_constant_value (fold_non_dependent_expr_sfinae (op0,
4938 tf_none));
4939 op1 = maybe_constant_value (fold_non_dependent_expr_sfinae (op1,
4940 tf_none));
4941 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
4943 /* For diagnostics we want to use the promoted types without
4944 shorten_binary_op. So convert the arguments to the
4945 original result_type. */
4946 tree cop0 = op0;
4947 tree cop1 = op1;
4948 if (orig_type != NULL && result_type != orig_type)
4950 cop0 = cp_convert (orig_type, op0, complain);
4951 cop1 = cp_convert (orig_type, op1, complain);
4953 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
4955 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
4956 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
4959 result = build2 (resultcode, build_type, op0, op1);
4960 result = fold_if_not_in_template (result);
4961 if (final_type != 0)
4962 result = cp_convert (final_type, result, complain);
4964 if (TREE_OVERFLOW_P (result)
4965 && !TREE_OVERFLOW_P (op0)
4966 && !TREE_OVERFLOW_P (op1))
4967 overflow_warning (location, result);
4969 if (instrument_expr != NULL)
4970 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
4971 instrument_expr, result);
4973 return result;
4976 /* Build a VEC_PERM_EXPR.
4977 This is a simple wrapper for c_build_vec_perm_expr. */
4978 tree
4979 build_x_vec_perm_expr (location_t loc,
4980 tree arg0, tree arg1, tree arg2,
4981 tsubst_flags_t complain)
4983 tree orig_arg0 = arg0;
4984 tree orig_arg1 = arg1;
4985 tree orig_arg2 = arg2;
4986 if (processing_template_decl)
4988 if (type_dependent_expression_p (arg0)
4989 || type_dependent_expression_p (arg1)
4990 || type_dependent_expression_p (arg2))
4991 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
4992 arg0 = build_non_dependent_expr (arg0);
4993 if (arg1)
4994 arg1 = build_non_dependent_expr (arg1);
4995 arg2 = build_non_dependent_expr (arg2);
4997 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
4998 if (processing_template_decl && exp != error_mark_node)
4999 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5000 orig_arg1, orig_arg2);
5001 return exp;
5004 /* Return a tree for the sum or difference (RESULTCODE says which)
5005 of pointer PTROP and integer INTOP. */
5007 static tree
5008 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5009 tsubst_flags_t complain)
5011 tree res_type = TREE_TYPE (ptrop);
5013 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5014 in certain circumstance (when it's valid to do so). So we need
5015 to make sure it's complete. We don't need to check here, if we
5016 can actually complete it at all, as those checks will be done in
5017 pointer_int_sum() anyway. */
5018 complete_type (TREE_TYPE (res_type));
5020 return pointer_int_sum (input_location, resultcode, ptrop,
5021 fold_if_not_in_template (intop),
5022 complain & tf_warning_or_error);
5025 /* Return a tree for the difference of pointers OP0 and OP1.
5026 The resulting tree has type int. */
5028 static tree
5029 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5031 tree result;
5032 tree restype = ptrdiff_type_node;
5033 tree target_type = TREE_TYPE (ptrtype);
5035 if (!complete_type_or_else (target_type, NULL_TREE))
5036 return error_mark_node;
5038 if (VOID_TYPE_P (target_type))
5040 if (complain & tf_error)
5041 permerror (input_location, "ISO C++ forbids using pointer of "
5042 "type %<void *%> in subtraction");
5043 else
5044 return error_mark_node;
5046 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5048 if (complain & tf_error)
5049 permerror (input_location, "ISO C++ forbids using pointer to "
5050 "a function in subtraction");
5051 else
5052 return error_mark_node;
5054 if (TREE_CODE (target_type) == METHOD_TYPE)
5056 if (complain & tf_error)
5057 permerror (input_location, "ISO C++ forbids using pointer to "
5058 "a method in subtraction");
5059 else
5060 return error_mark_node;
5063 /* First do the subtraction as integers;
5064 then drop through to build the divide operator. */
5066 op0 = cp_build_binary_op (input_location,
5067 MINUS_EXPR,
5068 cp_convert (restype, op0, complain),
5069 cp_convert (restype, op1, complain),
5070 complain);
5072 /* This generates an error if op1 is a pointer to an incomplete type. */
5073 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5075 if (complain & tf_error)
5076 error ("invalid use of a pointer to an incomplete type in "
5077 "pointer arithmetic");
5078 else
5079 return error_mark_node;
5082 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5084 if (complain & tf_error)
5085 error ("arithmetic on pointer to an empty aggregate");
5086 else
5087 return error_mark_node;
5090 op1 = (TYPE_PTROB_P (ptrtype)
5091 ? size_in_bytes (target_type)
5092 : integer_one_node);
5094 /* Do the division. */
5096 result = build2 (EXACT_DIV_EXPR, restype, op0,
5097 cp_convert (restype, op1, complain));
5098 return fold_if_not_in_template (result);
5101 /* Construct and perhaps optimize a tree representation
5102 for a unary operation. CODE, a tree_code, specifies the operation
5103 and XARG is the operand. */
5105 tree
5106 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5107 tsubst_flags_t complain)
5109 tree orig_expr = xarg;
5110 tree exp;
5111 int ptrmem = 0;
5113 if (processing_template_decl)
5115 if (type_dependent_expression_p (xarg))
5116 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5118 xarg = build_non_dependent_expr (xarg);
5121 exp = NULL_TREE;
5123 /* [expr.unary.op] says:
5125 The address of an object of incomplete type can be taken.
5127 (And is just the ordinary address operator, not an overloaded
5128 "operator &".) However, if the type is a template
5129 specialization, we must complete the type at this point so that
5130 an overloaded "operator &" will be available if required. */
5131 if (code == ADDR_EXPR
5132 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5133 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5134 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5135 || (TREE_CODE (xarg) == OFFSET_REF)))
5136 /* Don't look for a function. */;
5137 else
5138 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5139 NULL_TREE, /*overload=*/NULL, complain);
5140 if (!exp && code == ADDR_EXPR)
5142 if (is_overloaded_fn (xarg))
5144 tree fn = get_first_fn (xarg);
5145 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5147 if (complain & tf_error)
5148 error (DECL_CONSTRUCTOR_P (fn)
5149 ? G_("taking address of constructor %qE")
5150 : G_("taking address of destructor %qE"),
5151 xarg);
5152 return error_mark_node;
5156 /* A pointer to member-function can be formed only by saying
5157 &X::mf. */
5158 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5159 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5161 if (TREE_CODE (xarg) != OFFSET_REF
5162 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5164 if (complain & tf_error)
5166 error ("invalid use of %qE to form a "
5167 "pointer-to-member-function", xarg);
5168 if (TREE_CODE (xarg) != OFFSET_REF)
5169 inform (input_location, " a qualified-id is required");
5171 return error_mark_node;
5173 else
5175 if (complain & tf_error)
5176 error ("parentheses around %qE cannot be used to form a"
5177 " pointer-to-member-function",
5178 xarg);
5179 else
5180 return error_mark_node;
5181 PTRMEM_OK_P (xarg) = 1;
5185 if (TREE_CODE (xarg) == OFFSET_REF)
5187 ptrmem = PTRMEM_OK_P (xarg);
5189 if (!ptrmem && !flag_ms_extensions
5190 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5192 /* A single non-static member, make sure we don't allow a
5193 pointer-to-member. */
5194 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5195 TREE_OPERAND (xarg, 0),
5196 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5197 PTRMEM_OK_P (xarg) = ptrmem;
5201 exp = cp_build_addr_expr_strict (xarg, complain);
5204 if (processing_template_decl && exp != error_mark_node)
5205 exp = build_min_non_dep (code, exp, orig_expr,
5206 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5207 if (TREE_CODE (exp) == ADDR_EXPR)
5208 PTRMEM_OK_P (exp) = ptrmem;
5209 return exp;
5212 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5213 constants, where a null value is represented by an INTEGER_CST of
5214 -1. */
5216 tree
5217 cp_truthvalue_conversion (tree expr)
5219 tree type = TREE_TYPE (expr);
5220 if (TYPE_PTRDATAMEM_P (type)
5221 /* Avoid ICE on invalid use of non-static member function. */
5222 || TREE_CODE (expr) == FUNCTION_DECL)
5223 return build_binary_op (EXPR_LOCATION (expr),
5224 NE_EXPR, expr, nullptr_node, 1);
5225 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5227 /* With -Wzero-as-null-pointer-constant do not warn for an
5228 'if (p)' or a 'while (!p)', where p is a pointer. */
5229 tree ret;
5230 ++c_inhibit_evaluation_warnings;
5231 ret = c_common_truthvalue_conversion (input_location, expr);
5232 --c_inhibit_evaluation_warnings;
5233 return ret;
5235 else
5236 return c_common_truthvalue_conversion (input_location, expr);
5239 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5241 tree
5242 condition_conversion (tree expr)
5244 tree t;
5245 if (processing_template_decl)
5246 return expr;
5247 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5248 tf_warning_or_error, LOOKUP_NORMAL);
5249 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5250 return t;
5253 /* Returns the address of T. This function will fold away
5254 ADDR_EXPR of INDIRECT_REF. */
5256 tree
5257 build_address (tree t)
5259 if (error_operand_p (t) || !cxx_mark_addressable (t))
5260 return error_mark_node;
5261 t = build_fold_addr_expr (t);
5262 if (TREE_CODE (t) != ADDR_EXPR)
5263 t = rvalue (t);
5264 return t;
5267 /* Returns the address of T with type TYPE. */
5269 tree
5270 build_typed_address (tree t, tree type)
5272 if (error_operand_p (t) || !cxx_mark_addressable (t))
5273 return error_mark_node;
5274 t = build_fold_addr_expr_with_type (t, type);
5275 if (TREE_CODE (t) != ADDR_EXPR)
5276 t = rvalue (t);
5277 return t;
5280 /* Return a NOP_EXPR converting EXPR to TYPE. */
5282 tree
5283 build_nop (tree type, tree expr)
5285 if (type == error_mark_node || error_operand_p (expr))
5286 return expr;
5287 return build1 (NOP_EXPR, type, expr);
5290 /* Take the address of ARG, whatever that means under C++ semantics.
5291 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5292 and class rvalues as well.
5294 Nothing should call this function directly; instead, callers should use
5295 cp_build_addr_expr or cp_build_addr_expr_strict. */
5297 static tree
5298 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5300 tree argtype;
5301 tree val;
5303 if (!arg || error_operand_p (arg))
5304 return error_mark_node;
5306 arg = mark_lvalue_use (arg);
5307 argtype = lvalue_type (arg);
5309 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5311 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5312 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5314 /* They're trying to take the address of a unique non-static
5315 member function. This is ill-formed (except in MS-land),
5316 but let's try to DTRT.
5317 Note: We only handle unique functions here because we don't
5318 want to complain if there's a static overload; non-unique
5319 cases will be handled by instantiate_type. But we need to
5320 handle this case here to allow casts on the resulting PMF.
5321 We could defer this in non-MS mode, but it's easier to give
5322 a useful error here. */
5324 /* Inside constant member functions, the `this' pointer
5325 contains an extra const qualifier. TYPE_MAIN_VARIANT
5326 is used here to remove this const from the diagnostics
5327 and the created OFFSET_REF. */
5328 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5329 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5330 mark_used (fn);
5332 if (! flag_ms_extensions)
5334 tree name = DECL_NAME (fn);
5335 if (!(complain & tf_error))
5336 return error_mark_node;
5337 else if (current_class_type
5338 && TREE_OPERAND (arg, 0) == current_class_ref)
5339 /* An expression like &memfn. */
5340 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5341 " or parenthesized non-static member function to form"
5342 " a pointer to member function. Say %<&%T::%D%>",
5343 base, name);
5344 else
5345 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5346 " function to form a pointer to member function."
5347 " Say %<&%T::%D%>",
5348 base, name);
5350 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5353 /* Uninstantiated types are all functions. Taking the
5354 address of a function is a no-op, so just return the
5355 argument. */
5356 if (type_unknown_p (arg))
5357 return build1 (ADDR_EXPR, unknown_type_node, arg);
5359 if (TREE_CODE (arg) == OFFSET_REF)
5360 /* We want a pointer to member; bypass all the code for actually taking
5361 the address of something. */
5362 goto offset_ref;
5364 /* Anything not already handled and not a true memory reference
5365 is an error. */
5366 if (TREE_CODE (argtype) != FUNCTION_TYPE
5367 && TREE_CODE (argtype) != METHOD_TYPE)
5369 cp_lvalue_kind kind = lvalue_kind (arg);
5370 if (kind == clk_none)
5372 if (complain & tf_error)
5373 lvalue_error (input_location, lv_addressof);
5374 return error_mark_node;
5376 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5378 if (!(complain & tf_error))
5379 return error_mark_node;
5380 if (kind & clk_class)
5381 /* Make this a permerror because we used to accept it. */
5382 permerror (input_location, "taking address of temporary");
5383 else
5384 error ("taking address of xvalue (rvalue reference)");
5388 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5390 tree type = build_pointer_type (TREE_TYPE (argtype));
5391 arg = build1 (CONVERT_EXPR, type, arg);
5392 return arg;
5394 else if (pedantic && DECL_MAIN_P (arg))
5396 /* ARM $3.4 */
5397 /* Apparently a lot of autoconf scripts for C++ packages do this,
5398 so only complain if -Wpedantic. */
5399 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5400 pedwarn (input_location, OPT_Wpedantic,
5401 "ISO C++ forbids taking address of function %<::main%>");
5402 else if (flag_pedantic_errors)
5403 return error_mark_node;
5406 /* Let &* cancel out to simplify resulting code. */
5407 if (INDIRECT_REF_P (arg))
5409 /* We don't need to have `current_class_ptr' wrapped in a
5410 NON_LVALUE_EXPR node. */
5411 if (arg == current_class_ref)
5412 return current_class_ptr;
5414 arg = TREE_OPERAND (arg, 0);
5415 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5417 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5418 arg = build1 (CONVERT_EXPR, type, arg);
5420 else
5421 /* Don't let this be an lvalue. */
5422 arg = rvalue (arg);
5423 return arg;
5426 /* ??? Cope with user tricks that amount to offsetof. */
5427 if (TREE_CODE (argtype) != FUNCTION_TYPE
5428 && TREE_CODE (argtype) != METHOD_TYPE
5429 && argtype != unknown_type_node
5430 && (val = get_base_address (arg))
5431 && COMPLETE_TYPE_P (TREE_TYPE (val))
5432 && INDIRECT_REF_P (val)
5433 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5435 tree type = build_pointer_type (argtype);
5436 return fold_convert (type, fold_offsetof_1 (arg));
5439 /* Handle complex lvalues (when permitted)
5440 by reduction to simpler cases. */
5441 val = unary_complex_lvalue (ADDR_EXPR, arg);
5442 if (val != 0)
5443 return val;
5445 switch (TREE_CODE (arg))
5447 CASE_CONVERT:
5448 case FLOAT_EXPR:
5449 case FIX_TRUNC_EXPR:
5450 /* Even if we're not being pedantic, we cannot allow this
5451 extension when we're instantiating in a SFINAE
5452 context. */
5453 if (! lvalue_p (arg) && complain == tf_none)
5455 if (complain & tf_error)
5456 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5457 else
5458 return error_mark_node;
5460 break;
5462 case BASELINK:
5463 arg = BASELINK_FUNCTIONS (arg);
5464 /* Fall through. */
5466 case OVERLOAD:
5467 arg = OVL_CURRENT (arg);
5468 break;
5470 case OFFSET_REF:
5471 offset_ref:
5472 /* Turn a reference to a non-static data member into a
5473 pointer-to-member. */
5475 tree type;
5476 tree t;
5478 gcc_assert (PTRMEM_OK_P (arg));
5480 t = TREE_OPERAND (arg, 1);
5481 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5483 if (complain & tf_error)
5484 error ("cannot create pointer to reference member %qD", t);
5485 return error_mark_node;
5488 type = build_ptrmem_type (context_for_name_lookup (t),
5489 TREE_TYPE (t));
5490 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5491 return t;
5494 default:
5495 break;
5498 if (argtype != error_mark_node)
5500 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (argtype)
5501 && (flag_iso || warn_vla > 0))
5503 if (complain & tf_warning_or_error)
5504 pedwarn (input_location, OPT_Wvla,
5505 "taking address of array of runtime bound");
5506 else
5507 return error_mark_node;
5509 argtype = build_pointer_type (argtype);
5512 /* In a template, we are processing a non-dependent expression
5513 so we can just form an ADDR_EXPR with the correct type. */
5514 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5516 val = build_address (arg);
5517 if (TREE_CODE (arg) == OFFSET_REF)
5518 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5520 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5522 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5524 /* We can only get here with a single static member
5525 function. */
5526 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5527 && DECL_STATIC_FUNCTION_P (fn));
5528 mark_used (fn);
5529 val = build_address (fn);
5530 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5531 /* Do not lose object's side effects. */
5532 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5533 TREE_OPERAND (arg, 0), val);
5535 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5537 if (complain & tf_error)
5538 error ("attempt to take address of bit-field structure member %qD",
5539 TREE_OPERAND (arg, 1));
5540 return error_mark_node;
5542 else
5544 tree object = TREE_OPERAND (arg, 0);
5545 tree field = TREE_OPERAND (arg, 1);
5546 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5547 (TREE_TYPE (object), decl_type_context (field)));
5548 val = build_address (arg);
5551 if (TYPE_PTR_P (argtype)
5552 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5554 build_ptrmemfunc_type (argtype);
5555 val = build_ptrmemfunc (argtype, val, 0,
5556 /*c_cast_p=*/false,
5557 complain);
5560 return val;
5563 /* Take the address of ARG if it has one, even if it's an rvalue. */
5565 tree
5566 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5568 return cp_build_addr_expr_1 (arg, 0, complain);
5571 /* Take the address of ARG, but only if it's an lvalue. */
5573 tree
5574 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5576 return cp_build_addr_expr_1 (arg, 1, complain);
5579 /* C++: Must handle pointers to members.
5581 Perhaps type instantiation should be extended to handle conversion
5582 from aggregates to types we don't yet know we want? (Or are those
5583 cases typically errors which should be reported?)
5585 NOCONVERT nonzero suppresses the default promotions
5586 (such as from short to int). */
5588 tree
5589 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5590 tsubst_flags_t complain)
5592 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5593 tree arg = xarg;
5594 tree argtype = 0;
5595 const char *errstring = NULL;
5596 tree val;
5597 const char *invalid_op_diag;
5599 if (!arg || error_operand_p (arg))
5600 return error_mark_node;
5602 if ((invalid_op_diag
5603 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5604 ? CONVERT_EXPR
5605 : code),
5606 TREE_TYPE (xarg))))
5608 if (complain & tf_error)
5609 error (invalid_op_diag);
5610 return error_mark_node;
5613 switch (code)
5615 case UNARY_PLUS_EXPR:
5616 case NEGATE_EXPR:
5618 int flags = WANT_ARITH | WANT_ENUM;
5619 /* Unary plus (but not unary minus) is allowed on pointers. */
5620 if (code == UNARY_PLUS_EXPR)
5621 flags |= WANT_POINTER;
5622 arg = build_expr_type_conversion (flags, arg, true);
5623 if (!arg)
5624 errstring = (code == NEGATE_EXPR
5625 ? _("wrong type argument to unary minus")
5626 : _("wrong type argument to unary plus"));
5627 else
5629 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5630 arg = cp_perform_integral_promotions (arg, complain);
5632 /* Make sure the result is not an lvalue: a unary plus or minus
5633 expression is always a rvalue. */
5634 arg = rvalue (arg);
5637 break;
5639 case BIT_NOT_EXPR:
5640 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5642 code = CONJ_EXPR;
5643 if (!noconvert)
5645 arg = cp_default_conversion (arg, complain);
5646 if (arg == error_mark_node)
5647 return error_mark_node;
5650 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5651 | WANT_VECTOR_OR_COMPLEX,
5652 arg, true)))
5653 errstring = _("wrong type argument to bit-complement");
5654 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5655 arg = cp_perform_integral_promotions (arg, complain);
5656 break;
5658 case ABS_EXPR:
5659 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5660 errstring = _("wrong type argument to abs");
5661 else if (!noconvert)
5663 arg = cp_default_conversion (arg, complain);
5664 if (arg == error_mark_node)
5665 return error_mark_node;
5667 break;
5669 case CONJ_EXPR:
5670 /* Conjugating a real value is a no-op, but allow it anyway. */
5671 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5672 errstring = _("wrong type argument to conjugation");
5673 else if (!noconvert)
5675 arg = cp_default_conversion (arg, complain);
5676 if (arg == error_mark_node)
5677 return error_mark_node;
5679 break;
5681 case TRUTH_NOT_EXPR:
5682 arg = perform_implicit_conversion (boolean_type_node, arg,
5683 complain);
5684 val = invert_truthvalue_loc (input_location, arg);
5685 if (arg != error_mark_node)
5686 return val;
5687 errstring = _("in argument to unary !");
5688 break;
5690 case NOP_EXPR:
5691 break;
5693 case REALPART_EXPR:
5694 case IMAGPART_EXPR:
5695 arg = build_real_imag_expr (input_location, code, arg);
5696 if (arg == error_mark_node)
5697 return arg;
5698 else
5699 return fold_if_not_in_template (arg);
5701 case PREINCREMENT_EXPR:
5702 case POSTINCREMENT_EXPR:
5703 case PREDECREMENT_EXPR:
5704 case POSTDECREMENT_EXPR:
5705 /* Handle complex lvalues (when permitted)
5706 by reduction to simpler cases. */
5708 val = unary_complex_lvalue (code, arg);
5709 if (val != 0)
5710 return val;
5712 arg = mark_lvalue_use (arg);
5714 /* Increment or decrement the real part of the value,
5715 and don't change the imaginary part. */
5716 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5718 tree real, imag;
5720 arg = stabilize_reference (arg);
5721 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5722 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5723 real = cp_build_unary_op (code, real, 1, complain);
5724 if (real == error_mark_node || imag == error_mark_node)
5725 return error_mark_node;
5726 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5727 real, imag);
5730 /* Report invalid types. */
5732 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5733 arg, true)))
5735 if (code == PREINCREMENT_EXPR)
5736 errstring = _("no pre-increment operator for type");
5737 else if (code == POSTINCREMENT_EXPR)
5738 errstring = _("no post-increment operator for type");
5739 else if (code == PREDECREMENT_EXPR)
5740 errstring = _("no pre-decrement operator for type");
5741 else
5742 errstring = _("no post-decrement operator for type");
5743 break;
5745 else if (arg == error_mark_node)
5746 return error_mark_node;
5748 /* Report something read-only. */
5750 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5751 || TREE_READONLY (arg))
5753 if (complain & tf_error)
5754 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5755 || code == POSTINCREMENT_EXPR)
5756 ? lv_increment : lv_decrement));
5757 else
5758 return error_mark_node;
5762 tree inc;
5763 tree declared_type = unlowered_expr_type (arg);
5765 argtype = TREE_TYPE (arg);
5767 /* ARM $5.2.5 last annotation says this should be forbidden. */
5768 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5770 if (complain & tf_error)
5771 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5772 ? G_("ISO C++ forbids incrementing an enum")
5773 : G_("ISO C++ forbids decrementing an enum"));
5774 else
5775 return error_mark_node;
5778 /* Compute the increment. */
5780 if (TYPE_PTR_P (argtype))
5782 tree type = complete_type (TREE_TYPE (argtype));
5784 if (!COMPLETE_OR_VOID_TYPE_P (type))
5786 if (complain & tf_error)
5787 error (((code == PREINCREMENT_EXPR
5788 || code == POSTINCREMENT_EXPR))
5789 ? G_("cannot increment a pointer to incomplete type %qT")
5790 : G_("cannot decrement a pointer to incomplete type %qT"),
5791 TREE_TYPE (argtype));
5792 else
5793 return error_mark_node;
5795 else if (!TYPE_PTROB_P (argtype))
5797 if (complain & tf_error)
5798 pedwarn (input_location, OPT_Wpointer_arith,
5799 (code == PREINCREMENT_EXPR
5800 || code == POSTINCREMENT_EXPR)
5801 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5802 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5803 argtype);
5804 else
5805 return error_mark_node;
5808 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5810 else
5811 inc = VECTOR_TYPE_P (argtype)
5812 ? build_one_cst (argtype)
5813 : integer_one_node;
5815 inc = cp_convert (argtype, inc, complain);
5817 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5818 need to ask Objective-C to build the increment or decrement
5819 expression for it. */
5820 if (objc_is_property_ref (arg))
5821 return objc_build_incr_expr_for_property_ref (input_location, code,
5822 arg, inc);
5824 /* Complain about anything else that is not a true lvalue. */
5825 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5826 || code == POSTINCREMENT_EXPR)
5827 ? lv_increment : lv_decrement),
5828 complain))
5829 return error_mark_node;
5831 /* Forbid using -- on `bool'. */
5832 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5834 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5836 if (complain & tf_error)
5837 error ("invalid use of Boolean expression as operand "
5838 "to %<operator--%>");
5839 return error_mark_node;
5841 val = boolean_increment (code, arg);
5843 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5844 /* An rvalue has no cv-qualifiers. */
5845 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5846 else
5847 val = build2 (code, TREE_TYPE (arg), arg, inc);
5849 TREE_SIDE_EFFECTS (val) = 1;
5850 return val;
5853 case ADDR_EXPR:
5854 /* Note that this operation never does default_conversion
5855 regardless of NOCONVERT. */
5856 return cp_build_addr_expr (arg, complain);
5858 default:
5859 break;
5862 if (!errstring)
5864 if (argtype == 0)
5865 argtype = TREE_TYPE (arg);
5866 return fold_if_not_in_template (build1 (code, argtype, arg));
5869 if (complain & tf_error)
5870 error ("%s", errstring);
5871 return error_mark_node;
5874 /* Hook for the c-common bits that build a unary op. */
5875 tree
5876 build_unary_op (location_t /*location*/,
5877 enum tree_code code, tree xarg, int noconvert)
5879 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5882 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5883 for certain kinds of expressions which are not really lvalues
5884 but which we can accept as lvalues.
5886 If ARG is not a kind of expression we can handle, return
5887 NULL_TREE. */
5889 tree
5890 unary_complex_lvalue (enum tree_code code, tree arg)
5892 /* Inside a template, making these kinds of adjustments is
5893 pointless; we are only concerned with the type of the
5894 expression. */
5895 if (processing_template_decl)
5896 return NULL_TREE;
5898 /* Handle (a, b) used as an "lvalue". */
5899 if (TREE_CODE (arg) == COMPOUND_EXPR)
5901 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5902 tf_warning_or_error);
5903 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5904 TREE_OPERAND (arg, 0), real_result);
5907 /* Handle (a ? b : c) used as an "lvalue". */
5908 if (TREE_CODE (arg) == COND_EXPR
5909 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5910 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5912 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5913 if (TREE_CODE (arg) == MODIFY_EXPR
5914 || TREE_CODE (arg) == PREINCREMENT_EXPR
5915 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5917 tree lvalue = TREE_OPERAND (arg, 0);
5918 if (TREE_SIDE_EFFECTS (lvalue))
5920 lvalue = stabilize_reference (lvalue);
5921 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5922 lvalue, TREE_OPERAND (arg, 1));
5924 return unary_complex_lvalue
5925 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5928 if (code != ADDR_EXPR)
5929 return NULL_TREE;
5931 /* Handle (a = b) used as an "lvalue" for `&'. */
5932 if (TREE_CODE (arg) == MODIFY_EXPR
5933 || TREE_CODE (arg) == INIT_EXPR)
5935 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5936 tf_warning_or_error);
5937 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5938 arg, real_result);
5939 TREE_NO_WARNING (arg) = 1;
5940 return arg;
5943 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5944 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5945 || TREE_CODE (arg) == OFFSET_REF)
5946 return NULL_TREE;
5948 /* We permit compiler to make function calls returning
5949 objects of aggregate type look like lvalues. */
5951 tree targ = arg;
5953 if (TREE_CODE (targ) == SAVE_EXPR)
5954 targ = TREE_OPERAND (targ, 0);
5956 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5958 if (TREE_CODE (arg) == SAVE_EXPR)
5959 targ = arg;
5960 else
5961 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5962 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5965 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
5966 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5967 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5970 /* Don't let anything else be handled specially. */
5971 return NULL_TREE;
5974 /* Mark EXP saying that we need to be able to take the
5975 address of it; it should not be allocated in a register.
5976 Value is true if successful.
5978 C++: we do not allow `current_class_ptr' to be addressable. */
5980 bool
5981 cxx_mark_addressable (tree exp)
5983 tree x = exp;
5985 while (1)
5986 switch (TREE_CODE (x))
5988 case ADDR_EXPR:
5989 case COMPONENT_REF:
5990 case ARRAY_REF:
5991 case REALPART_EXPR:
5992 case IMAGPART_EXPR:
5993 x = TREE_OPERAND (x, 0);
5994 break;
5996 case PARM_DECL:
5997 if (x == current_class_ptr)
5999 error ("cannot take the address of %<this%>, which is an rvalue expression");
6000 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6001 return true;
6003 /* Fall through. */
6005 case VAR_DECL:
6006 /* Caller should not be trying to mark initialized
6007 constant fields addressable. */
6008 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6009 || DECL_IN_AGGR_P (x) == 0
6010 || TREE_STATIC (x)
6011 || DECL_EXTERNAL (x));
6012 /* Fall through. */
6014 case RESULT_DECL:
6015 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6016 && !DECL_ARTIFICIAL (x))
6018 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6020 error
6021 ("address of explicit register variable %qD requested", x);
6022 return false;
6024 else if (extra_warnings)
6025 warning
6026 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6028 TREE_ADDRESSABLE (x) = 1;
6029 return true;
6031 case CONST_DECL:
6032 case FUNCTION_DECL:
6033 TREE_ADDRESSABLE (x) = 1;
6034 return true;
6036 case CONSTRUCTOR:
6037 TREE_ADDRESSABLE (x) = 1;
6038 return true;
6040 case TARGET_EXPR:
6041 TREE_ADDRESSABLE (x) = 1;
6042 cxx_mark_addressable (TREE_OPERAND (x, 0));
6043 return true;
6045 default:
6046 return true;
6050 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6052 tree
6053 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6054 tsubst_flags_t complain)
6056 tree orig_ifexp = ifexp;
6057 tree orig_op1 = op1;
6058 tree orig_op2 = op2;
6059 tree expr;
6061 if (processing_template_decl)
6063 /* The standard says that the expression is type-dependent if
6064 IFEXP is type-dependent, even though the eventual type of the
6065 expression doesn't dependent on IFEXP. */
6066 if (type_dependent_expression_p (ifexp)
6067 /* As a GNU extension, the middle operand may be omitted. */
6068 || (op1 && type_dependent_expression_p (op1))
6069 || type_dependent_expression_p (op2))
6070 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6071 ifexp = build_non_dependent_expr (ifexp);
6072 if (op1)
6073 op1 = build_non_dependent_expr (op1);
6074 op2 = build_non_dependent_expr (op2);
6077 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6078 if (processing_template_decl && expr != error_mark_node
6079 && TREE_CODE (expr) != VEC_COND_EXPR)
6081 tree min = build_min_non_dep (COND_EXPR, expr,
6082 orig_ifexp, orig_op1, orig_op2);
6083 /* In C++11, remember that the result is an lvalue or xvalue.
6084 In C++98, lvalue_kind can just assume lvalue in a template. */
6085 if (cxx_dialect >= cxx11
6086 && lvalue_or_rvalue_with_address_p (expr)
6087 && !lvalue_or_rvalue_with_address_p (min))
6088 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6089 !real_lvalue_p (expr));
6090 expr = convert_from_reference (min);
6092 return expr;
6095 /* Given a list of expressions, return a compound expression
6096 that performs them all and returns the value of the last of them. */
6098 tree
6099 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6100 tsubst_flags_t complain)
6102 tree expr = TREE_VALUE (list);
6104 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6105 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6107 if (complain & tf_error)
6108 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6109 "list-initializer for non-class type must not "
6110 "be parenthesized");
6111 else
6112 return error_mark_node;
6115 if (TREE_CHAIN (list))
6117 if (complain & tf_error)
6118 switch (exp)
6120 case ELK_INIT:
6121 permerror (input_location, "expression list treated as compound "
6122 "expression in initializer");
6123 break;
6124 case ELK_MEM_INIT:
6125 permerror (input_location, "expression list treated as compound "
6126 "expression in mem-initializer");
6127 break;
6128 case ELK_FUNC_CAST:
6129 permerror (input_location, "expression list treated as compound "
6130 "expression in functional cast");
6131 break;
6132 default:
6133 gcc_unreachable ();
6135 else
6136 return error_mark_node;
6138 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6139 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6140 expr, TREE_VALUE (list), complain);
6143 return expr;
6146 /* Like build_x_compound_expr_from_list, but using a VEC. */
6148 tree
6149 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6150 tsubst_flags_t complain)
6152 if (vec_safe_is_empty (vec))
6153 return NULL_TREE;
6154 else if (vec->length () == 1)
6155 return (*vec)[0];
6156 else
6158 tree expr;
6159 unsigned int ix;
6160 tree t;
6162 if (msg != NULL)
6164 if (complain & tf_error)
6165 permerror (input_location,
6166 "%s expression list treated as compound expression",
6167 msg);
6168 else
6169 return error_mark_node;
6172 expr = (*vec)[0];
6173 for (ix = 1; vec->iterate (ix, &t); ++ix)
6174 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6175 t, complain);
6177 return expr;
6181 /* Handle overloading of the ',' operator when needed. */
6183 tree
6184 build_x_compound_expr (location_t loc, tree op1, tree op2,
6185 tsubst_flags_t complain)
6187 tree result;
6188 tree orig_op1 = op1;
6189 tree orig_op2 = op2;
6191 if (processing_template_decl)
6193 if (type_dependent_expression_p (op1)
6194 || type_dependent_expression_p (op2))
6195 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6196 op1 = build_non_dependent_expr (op1);
6197 op2 = build_non_dependent_expr (op2);
6200 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6201 NULL_TREE, /*overload=*/NULL, complain);
6202 if (!result)
6203 result = cp_build_compound_expr (op1, op2, complain);
6205 if (processing_template_decl && result != error_mark_node)
6206 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6208 return result;
6211 /* Like cp_build_compound_expr, but for the c-common bits. */
6213 tree
6214 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6216 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6219 /* Build a compound expression. */
6221 tree
6222 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6224 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6226 if (lhs == error_mark_node || rhs == error_mark_node)
6227 return error_mark_node;
6229 if (flag_cilkplus
6230 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6231 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6233 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6234 : EXPR_LOCATION (rhs));
6235 error_at (loc,
6236 "spawned function call cannot be part of a comma expression");
6237 return error_mark_node;
6240 if (TREE_CODE (rhs) == TARGET_EXPR)
6242 /* If the rhs is a TARGET_EXPR, then build the compound
6243 expression inside the target_expr's initializer. This
6244 helps the compiler to eliminate unnecessary temporaries. */
6245 tree init = TREE_OPERAND (rhs, 1);
6247 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6248 TREE_OPERAND (rhs, 1) = init;
6250 return rhs;
6253 if (type_unknown_p (rhs))
6255 if (complain & tf_error)
6256 error ("no context to resolve type of %qE", rhs);
6257 return error_mark_node;
6260 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6263 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6264 casts away constness. CAST gives the type of cast. Returns true
6265 if the cast is ill-formed, false if it is well-formed.
6267 ??? This function warns for casting away any qualifier not just
6268 const. We would like to specify exactly what qualifiers are casted
6269 away.
6272 static bool
6273 check_for_casting_away_constness (tree src_type, tree dest_type,
6274 enum tree_code cast, tsubst_flags_t complain)
6276 /* C-style casts are allowed to cast away constness. With
6277 WARN_CAST_QUAL, we still want to issue a warning. */
6278 if (cast == CAST_EXPR && !warn_cast_qual)
6279 return false;
6281 if (!casts_away_constness (src_type, dest_type, complain))
6282 return false;
6284 switch (cast)
6286 case CAST_EXPR:
6287 if (complain & tf_warning)
6288 warning (OPT_Wcast_qual,
6289 "cast from type %qT to type %qT casts away qualifiers",
6290 src_type, dest_type);
6291 return false;
6293 case STATIC_CAST_EXPR:
6294 if (complain & tf_error)
6295 error ("static_cast from type %qT to type %qT casts away qualifiers",
6296 src_type, dest_type);
6297 return true;
6299 case REINTERPRET_CAST_EXPR:
6300 if (complain & tf_error)
6301 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6302 src_type, dest_type);
6303 return true;
6305 default:
6306 gcc_unreachable();
6311 Warns if the cast from expression EXPR to type TYPE is useless.
6313 void
6314 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6316 if (warn_useless_cast
6317 && complain & tf_warning)
6319 /* In C++14 mode, this interacts badly with force_paren_expr. And it
6320 isn't necessary in any mode, because the code below handles
6321 glvalues properly. For 4.9, just skip it in C++14 mode. */
6322 if (cxx_dialect < cxx1y && REFERENCE_REF_P (expr))
6323 expr = TREE_OPERAND (expr, 0);
6325 if ((TREE_CODE (type) == REFERENCE_TYPE
6326 && (TYPE_REF_IS_RVALUE (type)
6327 ? xvalue_p (expr) : real_lvalue_p (expr))
6328 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6329 || same_type_p (TREE_TYPE (expr), type))
6330 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6334 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6335 (another pointer-to-member type in the same hierarchy) and return
6336 the converted expression. If ALLOW_INVERSE_P is permitted, a
6337 pointer-to-derived may be converted to pointer-to-base; otherwise,
6338 only the other direction is permitted. If C_CAST_P is true, this
6339 conversion is taking place as part of a C-style cast. */
6341 tree
6342 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6343 bool c_cast_p, tsubst_flags_t complain)
6345 if (TYPE_PTRDATAMEM_P (type))
6347 tree delta;
6349 if (TREE_CODE (expr) == PTRMEM_CST)
6350 expr = cplus_expand_constant (expr);
6351 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6352 TYPE_PTRMEM_CLASS_TYPE (type),
6353 allow_inverse_p,
6354 c_cast_p, complain);
6355 if (delta == error_mark_node)
6356 return error_mark_node;
6358 if (!integer_zerop (delta))
6360 tree cond, op1, op2;
6362 cond = cp_build_binary_op (input_location,
6363 EQ_EXPR,
6364 expr,
6365 build_int_cst (TREE_TYPE (expr), -1),
6366 complain);
6367 op1 = build_nop (ptrdiff_type_node, expr);
6368 op2 = cp_build_binary_op (input_location,
6369 PLUS_EXPR, op1, delta,
6370 complain);
6372 expr = fold_build3_loc (input_location,
6373 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6377 return build_nop (type, expr);
6379 else
6380 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6381 allow_inverse_p, c_cast_p, complain);
6384 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6385 this static_cast is being attempted as one of the possible casts
6386 allowed by a C-style cast. (In that case, accessibility of base
6387 classes is not considered, and it is OK to cast away
6388 constness.) Return the result of the cast. *VALID_P is set to
6389 indicate whether or not the cast was valid. */
6391 static tree
6392 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6393 bool *valid_p, tsubst_flags_t complain)
6395 tree intype;
6396 tree result;
6397 cp_lvalue_kind clk;
6399 /* Assume the cast is valid. */
6400 *valid_p = true;
6402 intype = unlowered_expr_type (expr);
6404 /* Save casted types in the function's used types hash table. */
6405 used_types_insert (type);
6407 /* [expr.static.cast]
6409 An lvalue of type "cv1 B", where B is a class type, can be cast
6410 to type "reference to cv2 D", where D is a class derived (clause
6411 _class.derived_) from B, if a valid standard conversion from
6412 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6413 same cv-qualification as, or greater cv-qualification than, cv1,
6414 and B is not a virtual base class of D. */
6415 /* We check this case before checking the validity of "TYPE t =
6416 EXPR;" below because for this case:
6418 struct B {};
6419 struct D : public B { D(const B&); };
6420 extern B& b;
6421 void f() { static_cast<const D&>(b); }
6423 we want to avoid constructing a new D. The standard is not
6424 completely clear about this issue, but our interpretation is
6425 consistent with other compilers. */
6426 if (TREE_CODE (type) == REFERENCE_TYPE
6427 && CLASS_TYPE_P (TREE_TYPE (type))
6428 && CLASS_TYPE_P (intype)
6429 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6430 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6431 && can_convert_standard (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6432 build_pointer_type (TYPE_MAIN_VARIANT
6433 (TREE_TYPE (type))),
6434 complain)
6435 && (c_cast_p
6436 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6438 tree base;
6440 /* There is a standard conversion from "D*" to "B*" even if "B"
6441 is ambiguous or inaccessible. If this is really a
6442 static_cast, then we check both for inaccessibility and
6443 ambiguity. However, if this is a static_cast being performed
6444 because the user wrote a C-style cast, then accessibility is
6445 not considered. */
6446 base = lookup_base (TREE_TYPE (type), intype,
6447 c_cast_p ? ba_unique : ba_check,
6448 NULL, complain);
6450 /* Convert from "B*" to "D*". This function will check that "B"
6451 is not a virtual base of "D". */
6452 expr = build_base_path (MINUS_EXPR, build_address (expr),
6453 base, /*nonnull=*/false, complain);
6454 /* Convert the pointer to a reference -- but then remember that
6455 there are no expressions with reference type in C++.
6457 We call rvalue so that there's an actual tree code
6458 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6459 is a variable with the same type, the conversion would get folded
6460 away, leaving just the variable and causing lvalue_kind to give
6461 the wrong answer. */
6462 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6465 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6466 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6467 if (TREE_CODE (type) == REFERENCE_TYPE
6468 && TYPE_REF_IS_RVALUE (type)
6469 && (clk = real_lvalue_p (expr))
6470 && reference_related_p (TREE_TYPE (type), intype)
6471 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6473 if (clk == clk_ordinary)
6475 /* Handle the (non-bit-field) lvalue case here by casting to
6476 lvalue reference and then changing it to an rvalue reference.
6477 Casting an xvalue to rvalue reference will be handled by the
6478 main code path. */
6479 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6480 result = (perform_direct_initialization_if_possible
6481 (lref, expr, c_cast_p, complain));
6482 result = cp_fold_convert (type, result);
6483 /* Make sure we don't fold back down to a named rvalue reference,
6484 because that would be an lvalue. */
6485 if (DECL_P (result))
6486 result = build1 (NON_LVALUE_EXPR, type, result);
6487 return convert_from_reference (result);
6489 else
6490 /* For a bit-field or packed field, bind to a temporary. */
6491 expr = rvalue (expr);
6494 /* Resolve overloaded address here rather than once in
6495 implicit_conversion and again in the inverse code below. */
6496 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6498 expr = instantiate_type (type, expr, complain);
6499 intype = TREE_TYPE (expr);
6502 /* [expr.static.cast]
6504 Any expression can be explicitly converted to type cv void. */
6505 if (VOID_TYPE_P (type))
6506 return convert_to_void (expr, ICV_CAST, complain);
6508 /* [class.abstract]
6509 An abstract class shall not be used ... as the type of an explicit
6510 conversion. */
6511 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6512 return error_mark_node;
6514 /* [expr.static.cast]
6516 An expression e can be explicitly converted to a type T using a
6517 static_cast of the form static_cast<T>(e) if the declaration T
6518 t(e);" is well-formed, for some invented temporary variable
6519 t. */
6520 result = perform_direct_initialization_if_possible (type, expr,
6521 c_cast_p, complain);
6522 if (result)
6524 result = convert_from_reference (result);
6526 /* [expr.static.cast]
6528 If T is a reference type, the result is an lvalue; otherwise,
6529 the result is an rvalue. */
6530 if (TREE_CODE (type) != REFERENCE_TYPE)
6531 result = rvalue (result);
6532 return result;
6535 /* [expr.static.cast]
6537 The inverse of any standard conversion sequence (clause _conv_),
6538 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6539 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6540 (_conv.bool_) conversions, can be performed explicitly using
6541 static_cast subject to the restriction that the explicit
6542 conversion does not cast away constness (_expr.const.cast_), and
6543 the following additional rules for specific cases: */
6544 /* For reference, the conversions not excluded are: integral
6545 promotions, floating point promotion, integral conversions,
6546 floating point conversions, floating-integral conversions,
6547 pointer conversions, and pointer to member conversions. */
6548 /* DR 128
6550 A value of integral _or enumeration_ type can be explicitly
6551 converted to an enumeration type. */
6552 /* The effect of all that is that any conversion between any two
6553 types which are integral, floating, or enumeration types can be
6554 performed. */
6555 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6556 || SCALAR_FLOAT_TYPE_P (type))
6557 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6558 || SCALAR_FLOAT_TYPE_P (intype)))
6559 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6561 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6562 && CLASS_TYPE_P (TREE_TYPE (type))
6563 && CLASS_TYPE_P (TREE_TYPE (intype))
6564 && can_convert_standard (build_pointer_type (TYPE_MAIN_VARIANT
6565 (TREE_TYPE (intype))),
6566 build_pointer_type (TYPE_MAIN_VARIANT
6567 (TREE_TYPE (type))),
6568 complain))
6570 tree base;
6572 if (!c_cast_p
6573 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6574 complain))
6575 return error_mark_node;
6576 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6577 c_cast_p ? ba_unique : ba_check,
6578 NULL, complain);
6579 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6580 complain);
6581 return cp_fold_convert(type, expr);
6584 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6585 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6587 tree c1;
6588 tree c2;
6589 tree t1;
6590 tree t2;
6592 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6593 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6595 if (TYPE_PTRDATAMEM_P (type))
6597 t1 = (build_ptrmem_type
6598 (c1,
6599 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6600 t2 = (build_ptrmem_type
6601 (c2,
6602 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6604 else
6606 t1 = intype;
6607 t2 = type;
6609 if ((can_convert_standard (t1, t2, complain) ||
6610 can_convert_standard (t2, t1, complain)))
6612 if (!c_cast_p
6613 && check_for_casting_away_constness (intype, type,
6614 STATIC_CAST_EXPR,
6615 complain))
6616 return error_mark_node;
6617 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6618 c_cast_p, complain);
6622 /* [expr.static.cast]
6624 An rvalue of type "pointer to cv void" can be explicitly
6625 converted to a pointer to object type. A value of type pointer
6626 to object converted to "pointer to cv void" and back to the
6627 original pointer type will have its original value. */
6628 if (TYPE_PTR_P (intype)
6629 && VOID_TYPE_P (TREE_TYPE (intype))
6630 && TYPE_PTROB_P (type))
6632 if (!c_cast_p
6633 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6634 complain))
6635 return error_mark_node;
6636 return build_nop (type, expr);
6639 *valid_p = false;
6640 return error_mark_node;
6643 /* Return an expression representing static_cast<TYPE>(EXPR). */
6645 tree
6646 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6648 tree result;
6649 bool valid_p;
6651 if (type == error_mark_node || expr == error_mark_node)
6652 return error_mark_node;
6654 if (processing_template_decl)
6656 expr = build_min (STATIC_CAST_EXPR, type, expr);
6657 /* We don't know if it will or will not have side effects. */
6658 TREE_SIDE_EFFECTS (expr) = 1;
6659 return convert_from_reference (expr);
6662 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6663 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6664 if (TREE_CODE (type) != REFERENCE_TYPE
6665 && TREE_CODE (expr) == NOP_EXPR
6666 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6667 expr = TREE_OPERAND (expr, 0);
6669 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6670 complain);
6671 if (valid_p)
6673 if (result != error_mark_node)
6674 maybe_warn_about_useless_cast (type, expr, complain);
6675 return result;
6678 if (complain & tf_error)
6679 error ("invalid static_cast from type %qT to type %qT",
6680 TREE_TYPE (expr), type);
6681 return error_mark_node;
6684 /* EXPR is an expression with member function or pointer-to-member
6685 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6686 not permitted by ISO C++, but we accept it in some modes. If we
6687 are not in one of those modes, issue a diagnostic. Return the
6688 converted expression. */
6690 tree
6691 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6693 tree intype;
6694 tree decl;
6696 intype = TREE_TYPE (expr);
6697 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6698 || TREE_CODE (intype) == METHOD_TYPE);
6700 if (!(complain & tf_warning_or_error))
6701 return error_mark_node;
6703 if (pedantic || warn_pmf2ptr)
6704 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6705 "converting from %qT to %qT", intype, type);
6707 if (TREE_CODE (intype) == METHOD_TYPE)
6708 expr = build_addr_func (expr, complain);
6709 else if (TREE_CODE (expr) == PTRMEM_CST)
6710 expr = build_address (PTRMEM_CST_MEMBER (expr));
6711 else
6713 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6714 decl = build_address (decl);
6715 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6718 if (expr == error_mark_node)
6719 return error_mark_node;
6721 return build_nop (type, expr);
6724 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6725 If C_CAST_P is true, this reinterpret cast is being done as part of
6726 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6727 indicate whether or not reinterpret_cast was valid. */
6729 static tree
6730 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6731 bool *valid_p, tsubst_flags_t complain)
6733 tree intype;
6735 /* Assume the cast is invalid. */
6736 if (valid_p)
6737 *valid_p = true;
6739 if (type == error_mark_node || error_operand_p (expr))
6740 return error_mark_node;
6742 intype = TREE_TYPE (expr);
6744 /* Save casted types in the function's used types hash table. */
6745 used_types_insert (type);
6747 /* [expr.reinterpret.cast]
6748 An lvalue expression of type T1 can be cast to the type
6749 "reference to T2" if an expression of type "pointer to T1" can be
6750 explicitly converted to the type "pointer to T2" using a
6751 reinterpret_cast. */
6752 if (TREE_CODE (type) == REFERENCE_TYPE)
6754 if (! real_lvalue_p (expr))
6756 if (complain & tf_error)
6757 error ("invalid cast of an rvalue expression of type "
6758 "%qT to type %qT",
6759 intype, type);
6760 return error_mark_node;
6763 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6764 "B" are related class types; the reinterpret_cast does not
6765 adjust the pointer. */
6766 if (TYPE_PTR_P (intype)
6767 && (complain & tf_warning)
6768 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6769 COMPARE_BASE | COMPARE_DERIVED)))
6770 warning (0, "casting %qT to %qT does not dereference pointer",
6771 intype, type);
6773 expr = cp_build_addr_expr (expr, complain);
6775 if (warn_strict_aliasing > 2)
6776 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6778 if (expr != error_mark_node)
6779 expr = build_reinterpret_cast_1
6780 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6781 valid_p, complain);
6782 if (expr != error_mark_node)
6783 /* cp_build_indirect_ref isn't right for rvalue refs. */
6784 expr = convert_from_reference (fold_convert (type, expr));
6785 return expr;
6788 /* As a G++ extension, we consider conversions from member
6789 functions, and pointers to member functions to
6790 pointer-to-function and pointer-to-void types. If
6791 -Wno-pmf-conversions has not been specified,
6792 convert_member_func_to_ptr will issue an error message. */
6793 if ((TYPE_PTRMEMFUNC_P (intype)
6794 || TREE_CODE (intype) == METHOD_TYPE)
6795 && TYPE_PTR_P (type)
6796 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6797 || VOID_TYPE_P (TREE_TYPE (type))))
6798 return convert_member_func_to_ptr (type, expr, complain);
6800 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6801 array-to-pointer, and function-to-pointer conversions are
6802 performed. */
6803 expr = decay_conversion (expr, complain);
6805 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6806 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6807 if (TREE_CODE (expr) == NOP_EXPR
6808 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6809 expr = TREE_OPERAND (expr, 0);
6811 if (error_operand_p (expr))
6812 return error_mark_node;
6814 intype = TREE_TYPE (expr);
6816 /* [expr.reinterpret.cast]
6817 A pointer can be converted to any integral type large enough to
6818 hold it. ... A value of type std::nullptr_t can be converted to
6819 an integral type; the conversion has the same meaning and
6820 validity as a conversion of (void*)0 to the integral type. */
6821 if (CP_INTEGRAL_TYPE_P (type)
6822 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6824 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6826 if (complain & tf_error)
6827 permerror (input_location, "cast from %qT to %qT loses precision",
6828 intype, type);
6829 else
6830 return error_mark_node;
6832 if (NULLPTR_TYPE_P (intype))
6833 return build_int_cst (type, 0);
6835 /* [expr.reinterpret.cast]
6836 A value of integral or enumeration type can be explicitly
6837 converted to a pointer. */
6838 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6839 /* OK */
6841 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6842 || TYPE_PTR_OR_PTRMEM_P (type))
6843 && same_type_p (type, intype))
6844 /* DR 799 */
6845 return fold_if_not_in_template (build_nop (type, expr));
6846 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6847 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6848 return fold_if_not_in_template (build_nop (type, expr));
6849 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6850 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6852 tree sexpr = expr;
6854 if (!c_cast_p
6855 && check_for_casting_away_constness (intype, type,
6856 REINTERPRET_CAST_EXPR,
6857 complain))
6858 return error_mark_node;
6859 /* Warn about possible alignment problems. */
6860 if (STRICT_ALIGNMENT && warn_cast_align
6861 && (complain & tf_warning)
6862 && !VOID_TYPE_P (type)
6863 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6864 && COMPLETE_TYPE_P (TREE_TYPE (type))
6865 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6866 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6867 warning (OPT_Wcast_align, "cast from %qT to %qT "
6868 "increases required alignment of target type", intype, type);
6870 /* We need to strip nops here, because the front end likes to
6871 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6872 STRIP_NOPS (sexpr);
6873 if (warn_strict_aliasing <= 2)
6874 strict_aliasing_warning (intype, type, sexpr);
6876 return fold_if_not_in_template (build_nop (type, expr));
6878 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6879 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6881 if (complain & tf_warning)
6882 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6883 object pointer type or vice versa is conditionally-supported." */
6884 warning (OPT_Wconditionally_supported,
6885 "casting between pointer-to-function and pointer-to-object "
6886 "is conditionally-supported");
6887 return fold_if_not_in_template (build_nop (type, expr));
6889 else if (TREE_CODE (type) == VECTOR_TYPE)
6890 return fold_if_not_in_template (convert_to_vector (type, expr));
6891 else if (TREE_CODE (intype) == VECTOR_TYPE
6892 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6893 return fold_if_not_in_template (convert_to_integer (type, expr));
6894 else
6896 if (valid_p)
6897 *valid_p = false;
6898 if (complain & tf_error)
6899 error ("invalid cast from type %qT to type %qT", intype, type);
6900 return error_mark_node;
6903 return cp_convert (type, expr, complain);
6906 tree
6907 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6909 tree r;
6911 if (type == error_mark_node || expr == error_mark_node)
6912 return error_mark_node;
6914 if (processing_template_decl)
6916 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6918 if (!TREE_SIDE_EFFECTS (t)
6919 && type_dependent_expression_p (expr))
6920 /* There might turn out to be side effects inside expr. */
6921 TREE_SIDE_EFFECTS (t) = 1;
6922 return convert_from_reference (t);
6925 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6926 /*valid_p=*/NULL, complain);
6927 if (r != error_mark_node)
6928 maybe_warn_about_useless_cast (type, expr, complain);
6929 return r;
6932 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6933 return an appropriate expression. Otherwise, return
6934 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6935 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6936 performing a C-style cast, its value upon return will indicate
6937 whether or not the conversion succeeded. */
6939 static tree
6940 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6941 bool *valid_p)
6943 tree src_type;
6944 tree reference_type;
6946 /* Callers are responsible for handling error_mark_node as a
6947 destination type. */
6948 gcc_assert (dst_type != error_mark_node);
6949 /* In a template, callers should be building syntactic
6950 representations of casts, not using this machinery. */
6951 gcc_assert (!processing_template_decl);
6953 /* Assume the conversion is invalid. */
6954 if (valid_p)
6955 *valid_p = false;
6957 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6959 if (complain & tf_error)
6960 error ("invalid use of const_cast with type %qT, "
6961 "which is not a pointer, "
6962 "reference, nor a pointer-to-data-member type", dst_type);
6963 return error_mark_node;
6966 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6968 if (complain & tf_error)
6969 error ("invalid use of const_cast with type %qT, which is a pointer "
6970 "or reference to a function type", dst_type);
6971 return error_mark_node;
6974 /* Save casted types in the function's used types hash table. */
6975 used_types_insert (dst_type);
6977 src_type = TREE_TYPE (expr);
6978 /* Expressions do not really have reference types. */
6979 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6980 src_type = TREE_TYPE (src_type);
6982 /* [expr.const.cast]
6984 For two object types T1 and T2, if a pointer to T1 can be explicitly
6985 converted to the type "pointer to T2" using a const_cast, then the
6986 following conversions can also be made:
6988 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6989 type T2 using the cast const_cast<T2&>;
6991 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6992 type T2 using the cast const_cast<T2&&>; and
6994 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6995 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6997 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6999 reference_type = dst_type;
7000 if (!TYPE_REF_IS_RVALUE (dst_type)
7001 ? real_lvalue_p (expr)
7002 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7003 ? lvalue_p (expr)
7004 : lvalue_or_rvalue_with_address_p (expr)))
7005 /* OK. */;
7006 else
7008 if (complain & tf_error)
7009 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7010 src_type, dst_type);
7011 return error_mark_node;
7013 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7014 src_type = build_pointer_type (src_type);
7016 else
7018 reference_type = NULL_TREE;
7019 /* If the destination type is not a reference type, the
7020 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7021 conversions are performed. */
7022 src_type = type_decays_to (src_type);
7023 if (src_type == error_mark_node)
7024 return error_mark_node;
7027 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7029 if (comp_ptr_ttypes_const (dst_type, src_type))
7031 if (valid_p)
7033 *valid_p = true;
7034 /* This cast is actually a C-style cast. Issue a warning if
7035 the user is making a potentially unsafe cast. */
7036 check_for_casting_away_constness (src_type, dst_type,
7037 CAST_EXPR, complain);
7039 if (reference_type)
7041 expr = cp_build_addr_expr (expr, complain);
7042 if (expr == error_mark_node)
7043 return error_mark_node;
7044 expr = build_nop (reference_type, expr);
7045 return convert_from_reference (expr);
7047 else
7049 expr = decay_conversion (expr, complain);
7050 if (expr == error_mark_node)
7051 return error_mark_node;
7053 /* build_c_cast puts on a NOP_EXPR to make the result not an
7054 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7055 non-lvalue context. */
7056 if (TREE_CODE (expr) == NOP_EXPR
7057 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7058 expr = TREE_OPERAND (expr, 0);
7059 return build_nop (dst_type, expr);
7062 else if (valid_p
7063 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7064 TREE_TYPE (src_type)))
7065 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7066 complain);
7069 if (complain & tf_error)
7070 error ("invalid const_cast from type %qT to type %qT",
7071 src_type, dst_type);
7072 return error_mark_node;
7075 tree
7076 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7078 tree r;
7080 if (type == error_mark_node || error_operand_p (expr))
7081 return error_mark_node;
7083 if (processing_template_decl)
7085 tree t = build_min (CONST_CAST_EXPR, type, expr);
7087 if (!TREE_SIDE_EFFECTS (t)
7088 && type_dependent_expression_p (expr))
7089 /* There might turn out to be side effects inside expr. */
7090 TREE_SIDE_EFFECTS (t) = 1;
7091 return convert_from_reference (t);
7094 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7095 if (r != error_mark_node)
7096 maybe_warn_about_useless_cast (type, expr, complain);
7097 return r;
7100 /* Like cp_build_c_cast, but for the c-common bits. */
7102 tree
7103 build_c_cast (location_t /*loc*/, tree type, tree expr)
7105 return cp_build_c_cast (type, expr, tf_warning_or_error);
7108 /* Build an expression representing an explicit C-style cast to type
7109 TYPE of expression EXPR. */
7111 tree
7112 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7114 tree value = expr;
7115 tree result;
7116 bool valid_p;
7118 if (type == error_mark_node || error_operand_p (expr))
7119 return error_mark_node;
7121 if (processing_template_decl)
7123 tree t = build_min (CAST_EXPR, type,
7124 tree_cons (NULL_TREE, value, NULL_TREE));
7125 /* We don't know if it will or will not have side effects. */
7126 TREE_SIDE_EFFECTS (t) = 1;
7127 return convert_from_reference (t);
7130 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7131 'Class') should always be retained, because this information aids
7132 in method lookup. */
7133 if (objc_is_object_ptr (type)
7134 && objc_is_object_ptr (TREE_TYPE (expr)))
7135 return build_nop (type, expr);
7137 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7138 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7139 if (TREE_CODE (type) != REFERENCE_TYPE
7140 && TREE_CODE (value) == NOP_EXPR
7141 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7142 value = TREE_OPERAND (value, 0);
7144 if (TREE_CODE (type) == ARRAY_TYPE)
7146 /* Allow casting from T1* to T2[] because Cfront allows it.
7147 NIHCL uses it. It is not valid ISO C++ however. */
7148 if (TYPE_PTR_P (TREE_TYPE (expr)))
7150 if (complain & tf_error)
7151 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7152 else
7153 return error_mark_node;
7154 type = build_pointer_type (TREE_TYPE (type));
7156 else
7158 if (complain & tf_error)
7159 error ("ISO C++ forbids casting to an array type %qT", type);
7160 return error_mark_node;
7164 if (TREE_CODE (type) == FUNCTION_TYPE
7165 || TREE_CODE (type) == METHOD_TYPE)
7167 if (complain & tf_error)
7168 error ("invalid cast to function type %qT", type);
7169 return error_mark_node;
7172 if (TYPE_PTR_P (type)
7173 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7174 /* Casting to an integer of smaller size is an error detected elsewhere. */
7175 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7176 /* Don't warn about converting any constant. */
7177 && !TREE_CONSTANT (value))
7178 warning_at (input_location, OPT_Wint_to_pointer_cast,
7179 "cast to pointer from integer of different size");
7181 /* A C-style cast can be a const_cast. */
7182 result = build_const_cast_1 (type, value, complain & tf_warning,
7183 &valid_p);
7184 if (valid_p)
7186 if (result != error_mark_node)
7187 maybe_warn_about_useless_cast (type, value, complain);
7188 return result;
7191 /* Or a static cast. */
7192 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7193 &valid_p, complain);
7194 /* Or a reinterpret_cast. */
7195 if (!valid_p)
7196 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7197 &valid_p, complain);
7198 /* The static_cast or reinterpret_cast may be followed by a
7199 const_cast. */
7200 if (valid_p
7201 /* A valid cast may result in errors if, for example, a
7202 conversion to an ambiguous base class is required. */
7203 && !error_operand_p (result))
7205 tree result_type;
7207 maybe_warn_about_useless_cast (type, value, complain);
7209 /* Non-class rvalues always have cv-unqualified type. */
7210 if (!CLASS_TYPE_P (type))
7211 type = TYPE_MAIN_VARIANT (type);
7212 result_type = TREE_TYPE (result);
7213 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7214 result_type = TYPE_MAIN_VARIANT (result_type);
7215 /* If the type of RESULT does not match TYPE, perform a
7216 const_cast to make it match. If the static_cast or
7217 reinterpret_cast succeeded, we will differ by at most
7218 cv-qualification, so the follow-on const_cast is guaranteed
7219 to succeed. */
7220 if (!same_type_p (non_reference (type), non_reference (result_type)))
7222 result = build_const_cast_1 (type, result, false, &valid_p);
7223 gcc_assert (valid_p);
7225 return result;
7228 return error_mark_node;
7231 /* For use from the C common bits. */
7232 tree
7233 build_modify_expr (location_t /*location*/,
7234 tree lhs, tree /*lhs_origtype*/,
7235 enum tree_code modifycode,
7236 location_t /*rhs_location*/, tree rhs,
7237 tree /*rhs_origtype*/)
7239 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7242 /* Build an assignment expression of lvalue LHS from value RHS.
7243 MODIFYCODE is the code for a binary operator that we use
7244 to combine the old value of LHS with RHS to get the new value.
7245 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7247 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7249 tree
7250 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7251 tsubst_flags_t complain)
7253 tree result;
7254 tree newrhs = rhs;
7255 tree lhstype = TREE_TYPE (lhs);
7256 tree olhstype = lhstype;
7257 bool plain_assign = (modifycode == NOP_EXPR);
7259 /* Avoid duplicate error messages from operands that had errors. */
7260 if (error_operand_p (lhs) || error_operand_p (rhs))
7261 return error_mark_node;
7263 /* Handle control structure constructs used as "lvalues". */
7264 switch (TREE_CODE (lhs))
7266 /* Handle --foo = 5; as these are valid constructs in C++. */
7267 case PREDECREMENT_EXPR:
7268 case PREINCREMENT_EXPR:
7269 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7270 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7271 stabilize_reference (TREE_OPERAND (lhs, 0)),
7272 TREE_OPERAND (lhs, 1));
7273 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7274 modifycode, rhs, complain);
7275 if (newrhs == error_mark_node)
7276 return error_mark_node;
7277 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7279 /* Handle (a, b) used as an "lvalue". */
7280 case COMPOUND_EXPR:
7281 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7282 modifycode, rhs, complain);
7283 if (newrhs == error_mark_node)
7284 return error_mark_node;
7285 return build2 (COMPOUND_EXPR, lhstype,
7286 TREE_OPERAND (lhs, 0), newrhs);
7288 case MODIFY_EXPR:
7289 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7290 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7291 stabilize_reference (TREE_OPERAND (lhs, 0)),
7292 TREE_OPERAND (lhs, 1));
7293 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7294 complain);
7295 if (newrhs == error_mark_node)
7296 return error_mark_node;
7297 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7299 case MIN_EXPR:
7300 case MAX_EXPR:
7301 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7302 when neither operand has side-effects. */
7303 if (!lvalue_or_else (lhs, lv_assign, complain))
7304 return error_mark_node;
7306 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7307 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7309 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7310 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7311 boolean_type_node,
7312 TREE_OPERAND (lhs, 0),
7313 TREE_OPERAND (lhs, 1)),
7314 TREE_OPERAND (lhs, 0),
7315 TREE_OPERAND (lhs, 1));
7316 /* Fall through. */
7318 /* Handle (a ? b : c) used as an "lvalue". */
7319 case COND_EXPR:
7321 /* Produce (a ? (b = rhs) : (c = rhs))
7322 except that the RHS goes through a save-expr
7323 so the code to compute it is only emitted once. */
7324 tree cond;
7325 tree preeval = NULL_TREE;
7327 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7329 if (complain & tf_error)
7330 error ("void value not ignored as it ought to be");
7331 return error_mark_node;
7334 rhs = stabilize_expr (rhs, &preeval);
7336 /* Check this here to avoid odd errors when trying to convert
7337 a throw to the type of the COND_EXPR. */
7338 if (!lvalue_or_else (lhs, lv_assign, complain))
7339 return error_mark_node;
7341 cond = build_conditional_expr
7342 (input_location, TREE_OPERAND (lhs, 0),
7343 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7344 modifycode, rhs, complain),
7345 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7346 modifycode, rhs, complain),
7347 complain);
7349 if (cond == error_mark_node)
7350 return cond;
7351 /* Make sure the code to compute the rhs comes out
7352 before the split. */
7353 if (preeval)
7354 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7355 return cond;
7358 default:
7359 break;
7362 if (modifycode == INIT_EXPR)
7364 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7365 /* Do the default thing. */;
7366 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7368 /* Compound literal. */
7369 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7370 /* Call convert to generate an error; see PR 11063. */
7371 rhs = convert (lhstype, rhs);
7372 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7373 TREE_SIDE_EFFECTS (result) = 1;
7374 return result;
7376 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7377 /* Do the default thing. */;
7378 else
7380 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7381 result = build_special_member_call (lhs, complete_ctor_identifier,
7382 &rhs_vec, lhstype, LOOKUP_NORMAL,
7383 complain);
7384 release_tree_vector (rhs_vec);
7385 if (result == NULL_TREE)
7386 return error_mark_node;
7387 return result;
7390 else
7392 lhs = require_complete_type_sfinae (lhs, complain);
7393 if (lhs == error_mark_node)
7394 return error_mark_node;
7396 if (modifycode == NOP_EXPR)
7398 if (c_dialect_objc ())
7400 result = objc_maybe_build_modify_expr (lhs, rhs);
7401 if (result)
7402 return result;
7405 /* `operator=' is not an inheritable operator. */
7406 if (! MAYBE_CLASS_TYPE_P (lhstype))
7407 /* Do the default thing. */;
7408 else
7410 result = build_new_op (input_location, MODIFY_EXPR,
7411 LOOKUP_NORMAL, lhs, rhs,
7412 make_node (NOP_EXPR), /*overload=*/NULL,
7413 complain);
7414 if (result == NULL_TREE)
7415 return error_mark_node;
7416 return result;
7418 lhstype = olhstype;
7420 else
7422 tree init = NULL_TREE;
7424 /* A binary op has been requested. Combine the old LHS
7425 value with the RHS producing the value we should actually
7426 store into the LHS. */
7427 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7428 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7429 || MAYBE_CLASS_TYPE_P (lhstype)));
7431 /* Preevaluate the RHS to make sure its evaluation is complete
7432 before the lvalue-to-rvalue conversion of the LHS:
7434 [expr.ass] With respect to an indeterminately-sequenced
7435 function call, the operation of a compound assignment is a
7436 single evaluation. [ Note: Therefore, a function call shall
7437 not intervene between the lvalue-to-rvalue conversion and the
7438 side effect associated with any single compound assignment
7439 operator. -- end note ] */
7440 lhs = stabilize_reference (lhs);
7441 rhs = rvalue (rhs);
7442 rhs = stabilize_expr (rhs, &init);
7443 newrhs = cp_build_binary_op (input_location,
7444 modifycode, lhs, rhs,
7445 complain);
7446 if (newrhs == error_mark_node)
7448 if (complain & tf_error)
7449 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7450 TREE_TYPE (lhs), TREE_TYPE (rhs));
7451 return error_mark_node;
7454 if (init)
7455 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7457 /* Now it looks like a plain assignment. */
7458 modifycode = NOP_EXPR;
7459 if (c_dialect_objc ())
7461 result = objc_maybe_build_modify_expr (lhs, newrhs);
7462 if (result)
7463 return result;
7466 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7467 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7470 /* The left-hand side must be an lvalue. */
7471 if (!lvalue_or_else (lhs, lv_assign, complain))
7472 return error_mark_node;
7474 /* Warn about modifying something that is `const'. Don't warn if
7475 this is initialization. */
7476 if (modifycode != INIT_EXPR
7477 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7478 /* Functions are not modifiable, even though they are
7479 lvalues. */
7480 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7481 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7482 /* If it's an aggregate and any field is const, then it is
7483 effectively const. */
7484 || (CLASS_TYPE_P (lhstype)
7485 && C_TYPE_FIELDS_READONLY (lhstype))))
7487 if (complain & tf_error)
7488 cxx_readonly_error (lhs, lv_assign);
7489 else
7490 return error_mark_node;
7493 /* If storing into a structure or union member, it may have been given a
7494 lowered bitfield type. We need to convert to the declared type first,
7495 so retrieve it now. */
7497 olhstype = unlowered_expr_type (lhs);
7499 /* Convert new value to destination type. */
7501 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7503 int from_array;
7505 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7507 if (modifycode != INIT_EXPR)
7509 if (complain & tf_error)
7510 error ("assigning to an array from an initializer list");
7511 return error_mark_node;
7513 if (check_array_initializer (lhs, lhstype, newrhs))
7514 return error_mark_node;
7515 newrhs = digest_init (lhstype, newrhs, complain);
7516 if (newrhs == error_mark_node)
7517 return error_mark_node;
7520 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7521 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7523 if (complain & tf_error)
7524 error ("incompatible types in assignment of %qT to %qT",
7525 TREE_TYPE (rhs), lhstype);
7526 return error_mark_node;
7529 /* Allow array assignment in compiler-generated code. */
7530 else if (!current_function_decl
7531 || !DECL_DEFAULTED_FN (current_function_decl))
7533 /* This routine is used for both initialization and assignment.
7534 Make sure the diagnostic message differentiates the context. */
7535 if (complain & tf_error)
7537 if (modifycode == INIT_EXPR)
7538 error ("array used as initializer");
7539 else
7540 error ("invalid array assignment");
7542 return error_mark_node;
7545 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7546 ? 1 + (modifycode != INIT_EXPR): 0;
7547 return build_vec_init (lhs, NULL_TREE, newrhs,
7548 /*explicit_value_init_p=*/false,
7549 from_array, complain);
7552 if (modifycode == INIT_EXPR)
7553 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7554 LOOKUP_ONLYCONVERTING. */
7555 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7556 ICR_INIT, NULL_TREE, 0,
7557 complain);
7558 else
7559 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7560 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7562 if (!same_type_p (lhstype, olhstype))
7563 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7565 if (modifycode != INIT_EXPR)
7567 if (TREE_CODE (newrhs) == CALL_EXPR
7568 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7569 newrhs = build_cplus_new (lhstype, newrhs, complain);
7571 /* Can't initialize directly from a TARGET_EXPR, since that would
7572 cause the lhs to be constructed twice, and possibly result in
7573 accidental self-initialization. So we force the TARGET_EXPR to be
7574 expanded without a target. */
7575 if (TREE_CODE (newrhs) == TARGET_EXPR)
7576 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7577 TREE_OPERAND (newrhs, 0));
7580 if (newrhs == error_mark_node)
7581 return error_mark_node;
7583 if (c_dialect_objc () && flag_objc_gc)
7585 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7587 if (result)
7588 return result;
7591 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7592 lhstype, lhs, newrhs);
7594 TREE_SIDE_EFFECTS (result) = 1;
7595 if (!plain_assign)
7596 TREE_NO_WARNING (result) = 1;
7598 return result;
7601 tree
7602 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7603 tree rhs, tsubst_flags_t complain)
7605 if (processing_template_decl)
7606 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7607 build_min_nt_loc (loc, modifycode, NULL_TREE,
7608 NULL_TREE), rhs);
7610 if (modifycode != NOP_EXPR)
7612 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7613 make_node (modifycode), /*overload=*/NULL,
7614 complain);
7615 if (rval)
7617 TREE_NO_WARNING (rval) = 1;
7618 return rval;
7621 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7624 /* Helper function for get_delta_difference which assumes FROM is a base
7625 class of TO. Returns a delta for the conversion of pointer-to-member
7626 of FROM to pointer-to-member of TO. If the conversion is invalid and
7627 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7628 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7629 If C_CAST_P is true, this conversion is taking place as part of a
7630 C-style cast. */
7632 static tree
7633 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7634 tsubst_flags_t complain)
7636 tree binfo;
7637 base_kind kind;
7639 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7640 &kind, complain);
7642 if (binfo == error_mark_node)
7644 if (!(complain & tf_error))
7645 return error_mark_node;
7647 error (" in pointer to member function conversion");
7648 return size_zero_node;
7650 else if (binfo)
7652 if (kind != bk_via_virtual)
7653 return BINFO_OFFSET (binfo);
7654 else
7655 /* FROM is a virtual base class of TO. Issue an error or warning
7656 depending on whether or not this is a reinterpret cast. */
7658 if (!(complain & tf_error))
7659 return error_mark_node;
7661 error ("pointer to member conversion via virtual base %qT",
7662 BINFO_TYPE (binfo_from_vbase (binfo)));
7664 return size_zero_node;
7667 else
7668 return NULL_TREE;
7671 /* Get difference in deltas for different pointer to member function
7672 types. If the conversion is invalid and tf_error is not set in
7673 COMPLAIN, returns error_mark_node, otherwise returns an integer
7674 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7675 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7676 conversions as well. If C_CAST_P is true this conversion is taking
7677 place as part of a C-style cast.
7679 Note that the naming of FROM and TO is kind of backwards; the return
7680 value is what we add to a TO in order to get a FROM. They are named
7681 this way because we call this function to find out how to convert from
7682 a pointer to member of FROM to a pointer to member of TO. */
7684 static tree
7685 get_delta_difference (tree from, tree to,
7686 bool allow_inverse_p,
7687 bool c_cast_p, tsubst_flags_t complain)
7689 tree result;
7691 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7692 /* Pointer to member of incomplete class is permitted*/
7693 result = size_zero_node;
7694 else
7695 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7697 if (result == error_mark_node)
7698 return error_mark_node;
7700 if (!result)
7702 if (!allow_inverse_p)
7704 if (!(complain & tf_error))
7705 return error_mark_node;
7707 error_not_base_type (from, to);
7708 error (" in pointer to member conversion");
7709 result = size_zero_node;
7711 else
7713 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7715 if (result == error_mark_node)
7716 return error_mark_node;
7718 if (result)
7719 result = size_diffop_loc (input_location,
7720 size_zero_node, result);
7721 else
7723 if (!(complain & tf_error))
7724 return error_mark_node;
7726 error_not_base_type (from, to);
7727 error (" in pointer to member conversion");
7728 result = size_zero_node;
7733 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7734 result));
7737 /* Return a constructor for the pointer-to-member-function TYPE using
7738 the other components as specified. */
7740 tree
7741 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7743 tree u = NULL_TREE;
7744 tree delta_field;
7745 tree pfn_field;
7746 vec<constructor_elt, va_gc> *v;
7748 /* Pull the FIELD_DECLs out of the type. */
7749 pfn_field = TYPE_FIELDS (type);
7750 delta_field = DECL_CHAIN (pfn_field);
7752 /* Make sure DELTA has the type we want. */
7753 delta = convert_and_check (input_location, delta_type_node, delta);
7755 /* Convert to the correct target type if necessary. */
7756 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7758 /* Finish creating the initializer. */
7759 vec_alloc (v, 2);
7760 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7761 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7762 u = build_constructor (type, v);
7763 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7764 TREE_STATIC (u) = (TREE_CONSTANT (u)
7765 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7766 != NULL_TREE)
7767 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7768 != NULL_TREE));
7769 return u;
7772 /* Build a constructor for a pointer to member function. It can be
7773 used to initialize global variables, local variable, or used
7774 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7775 want to be.
7777 If FORCE is nonzero, then force this conversion, even if
7778 we would rather not do it. Usually set when using an explicit
7779 cast. A C-style cast is being processed iff C_CAST_P is true.
7781 Return error_mark_node, if something goes wrong. */
7783 tree
7784 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7785 tsubst_flags_t complain)
7787 tree fn;
7788 tree pfn_type;
7789 tree to_type;
7791 if (error_operand_p (pfn))
7792 return error_mark_node;
7794 pfn_type = TREE_TYPE (pfn);
7795 to_type = build_ptrmemfunc_type (type);
7797 /* Handle multiple conversions of pointer to member functions. */
7798 if (TYPE_PTRMEMFUNC_P (pfn_type))
7800 tree delta = NULL_TREE;
7801 tree npfn = NULL_TREE;
7802 tree n;
7804 if (!force
7805 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7806 LOOKUP_NORMAL, complain))
7808 if (complain & tf_error)
7809 error ("invalid conversion to type %qT from type %qT",
7810 to_type, pfn_type);
7811 else
7812 return error_mark_node;
7815 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7816 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7817 force,
7818 c_cast_p, complain);
7819 if (n == error_mark_node)
7820 return error_mark_node;
7822 /* We don't have to do any conversion to convert a
7823 pointer-to-member to its own type. But, we don't want to
7824 just return a PTRMEM_CST if there's an explicit cast; that
7825 cast should make the expression an invalid template argument. */
7826 if (TREE_CODE (pfn) != PTRMEM_CST)
7828 if (same_type_p (to_type, pfn_type))
7829 return pfn;
7830 else if (integer_zerop (n))
7831 return build_reinterpret_cast (to_type, pfn,
7832 complain);
7835 if (TREE_SIDE_EFFECTS (pfn))
7836 pfn = save_expr (pfn);
7838 /* Obtain the function pointer and the current DELTA. */
7839 if (TREE_CODE (pfn) == PTRMEM_CST)
7840 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7841 else
7843 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7844 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7847 /* Just adjust the DELTA field. */
7848 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7849 (TREE_TYPE (delta), ptrdiff_type_node));
7850 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7851 n = cp_build_binary_op (input_location,
7852 LSHIFT_EXPR, n, integer_one_node,
7853 complain);
7854 delta = cp_build_binary_op (input_location,
7855 PLUS_EXPR, delta, n, complain);
7856 return build_ptrmemfunc1 (to_type, delta, npfn);
7859 /* Handle null pointer to member function conversions. */
7860 if (null_ptr_cst_p (pfn))
7862 pfn = cp_build_c_cast (type, pfn, complain);
7863 return build_ptrmemfunc1 (to_type,
7864 integer_zero_node,
7865 pfn);
7868 if (type_unknown_p (pfn))
7869 return instantiate_type (type, pfn, complain);
7871 fn = TREE_OPERAND (pfn, 0);
7872 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7873 /* In a template, we will have preserved the
7874 OFFSET_REF. */
7875 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7876 return make_ptrmem_cst (to_type, fn);
7879 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7880 given by CST.
7882 ??? There is no consistency as to the types returned for the above
7883 values. Some code acts as if it were a sizetype and some as if it were
7884 integer_type_node. */
7886 void
7887 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7889 tree type = TREE_TYPE (cst);
7890 tree fn = PTRMEM_CST_MEMBER (cst);
7891 tree ptr_class, fn_class;
7893 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7895 /* The class that the function belongs to. */
7896 fn_class = DECL_CONTEXT (fn);
7898 /* The class that we're creating a pointer to member of. */
7899 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7901 /* First, calculate the adjustment to the function's class. */
7902 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7903 /*c_cast_p=*/0, tf_warning_or_error);
7905 if (!DECL_VIRTUAL_P (fn))
7906 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7907 build_addr_func (fn, tf_warning_or_error));
7908 else
7910 /* If we're dealing with a virtual function, we have to adjust 'this'
7911 again, to point to the base which provides the vtable entry for
7912 fn; the call will do the opposite adjustment. */
7913 tree orig_class = DECL_CONTEXT (fn);
7914 tree binfo = binfo_or_else (orig_class, fn_class);
7915 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7916 *delta, BINFO_OFFSET (binfo));
7917 *delta = fold_if_not_in_template (*delta);
7919 /* We set PFN to the vtable offset at which the function can be
7920 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7921 case delta is shifted left, and then incremented). */
7922 *pfn = DECL_VINDEX (fn);
7923 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7924 TYPE_SIZE_UNIT (vtable_entry_type));
7925 *pfn = fold_if_not_in_template (*pfn);
7927 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7929 case ptrmemfunc_vbit_in_pfn:
7930 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7931 integer_one_node);
7932 *pfn = fold_if_not_in_template (*pfn);
7933 break;
7935 case ptrmemfunc_vbit_in_delta:
7936 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7937 *delta, integer_one_node);
7938 *delta = fold_if_not_in_template (*delta);
7939 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7940 *delta, integer_one_node);
7941 *delta = fold_if_not_in_template (*delta);
7942 break;
7944 default:
7945 gcc_unreachable ();
7948 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7949 *pfn = fold_if_not_in_template (*pfn);
7953 /* Return an expression for PFN from the pointer-to-member function
7954 given by T. */
7956 static tree
7957 pfn_from_ptrmemfunc (tree t)
7959 if (TREE_CODE (t) == PTRMEM_CST)
7961 tree delta;
7962 tree pfn;
7964 expand_ptrmemfunc_cst (t, &delta, &pfn);
7965 if (pfn)
7966 return pfn;
7969 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7972 /* Return an expression for DELTA from the pointer-to-member function
7973 given by T. */
7975 static tree
7976 delta_from_ptrmemfunc (tree t)
7978 if (TREE_CODE (t) == PTRMEM_CST)
7980 tree delta;
7981 tree pfn;
7983 expand_ptrmemfunc_cst (t, &delta, &pfn);
7984 if (delta)
7985 return delta;
7988 return build_ptrmemfunc_access_expr (t, delta_identifier);
7991 /* Convert value RHS to type TYPE as preparation for an assignment to
7992 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7993 implicit conversion is. If FNDECL is non-NULL, we are doing the
7994 conversion in order to pass the PARMNUMth argument of FNDECL.
7995 If FNDECL is NULL, we are doing the conversion in function pointer
7996 argument passing, conversion in initialization, etc. */
7998 static tree
7999 convert_for_assignment (tree type, tree rhs,
8000 impl_conv_rhs errtype, tree fndecl, int parmnum,
8001 tsubst_flags_t complain, int flags)
8003 tree rhstype;
8004 enum tree_code coder;
8006 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8007 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8008 rhs = TREE_OPERAND (rhs, 0);
8010 rhstype = TREE_TYPE (rhs);
8011 coder = TREE_CODE (rhstype);
8013 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8014 && vector_types_convertible_p (type, rhstype, true))
8016 rhs = mark_rvalue_use (rhs);
8017 return convert (type, rhs);
8020 if (rhs == error_mark_node || rhstype == error_mark_node)
8021 return error_mark_node;
8022 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8023 return error_mark_node;
8025 /* The RHS of an assignment cannot have void type. */
8026 if (coder == VOID_TYPE)
8028 if (complain & tf_error)
8029 error ("void value not ignored as it ought to be");
8030 return error_mark_node;
8033 if (c_dialect_objc ())
8035 int parmno;
8036 tree selector;
8037 tree rname = fndecl;
8039 switch (errtype)
8041 case ICR_ASSIGN:
8042 parmno = -1;
8043 break;
8044 case ICR_INIT:
8045 parmno = -2;
8046 break;
8047 default:
8048 selector = objc_message_selector ();
8049 parmno = parmnum;
8050 if (selector && parmno > 1)
8052 rname = selector;
8053 parmno -= 1;
8057 if (objc_compare_types (type, rhstype, parmno, rname))
8059 rhs = mark_rvalue_use (rhs);
8060 return convert (type, rhs);
8064 /* [expr.ass]
8066 The expression is implicitly converted (clause _conv_) to the
8067 cv-unqualified type of the left operand.
8069 We allow bad conversions here because by the time we get to this point
8070 we are committed to doing the conversion. If we end up doing a bad
8071 conversion, convert_like will complain. */
8072 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8074 /* When -Wno-pmf-conversions is use, we just silently allow
8075 conversions from pointers-to-members to plain pointers. If
8076 the conversion doesn't work, cp_convert will complain. */
8077 if (!warn_pmf2ptr
8078 && TYPE_PTR_P (type)
8079 && TYPE_PTRMEMFUNC_P (rhstype))
8080 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8081 else
8083 if (complain & tf_error)
8085 /* If the right-hand side has unknown type, then it is an
8086 overloaded function. Call instantiate_type to get error
8087 messages. */
8088 if (rhstype == unknown_type_node)
8089 instantiate_type (type, rhs, tf_warning_or_error);
8090 else if (fndecl)
8091 error ("cannot convert %qT to %qT for argument %qP to %qD",
8092 rhstype, type, parmnum, fndecl);
8093 else
8094 switch (errtype)
8096 case ICR_DEFAULT_ARGUMENT:
8097 error ("cannot convert %qT to %qT in default argument",
8098 rhstype, type);
8099 break;
8100 case ICR_ARGPASS:
8101 error ("cannot convert %qT to %qT in argument passing",
8102 rhstype, type);
8103 break;
8104 case ICR_CONVERTING:
8105 error ("cannot convert %qT to %qT",
8106 rhstype, type);
8107 break;
8108 case ICR_INIT:
8109 error ("cannot convert %qT to %qT in initialization",
8110 rhstype, type);
8111 break;
8112 case ICR_RETURN:
8113 error ("cannot convert %qT to %qT in return",
8114 rhstype, type);
8115 break;
8116 case ICR_ASSIGN:
8117 error ("cannot convert %qT to %qT in assignment",
8118 rhstype, type);
8119 break;
8120 default:
8121 gcc_unreachable();
8124 return error_mark_node;
8127 if (warn_suggest_attribute_format)
8129 const enum tree_code codel = TREE_CODE (type);
8130 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8131 && coder == codel
8132 && check_missing_format_attribute (type, rhstype)
8133 && (complain & tf_warning))
8134 switch (errtype)
8136 case ICR_ARGPASS:
8137 case ICR_DEFAULT_ARGUMENT:
8138 if (fndecl)
8139 warning (OPT_Wsuggest_attribute_format,
8140 "parameter %qP of %qD might be a candidate "
8141 "for a format attribute", parmnum, fndecl);
8142 else
8143 warning (OPT_Wsuggest_attribute_format,
8144 "parameter might be a candidate "
8145 "for a format attribute");
8146 break;
8147 case ICR_CONVERTING:
8148 warning (OPT_Wsuggest_attribute_format,
8149 "target of conversion might be a candidate "
8150 "for a format attribute");
8151 break;
8152 case ICR_INIT:
8153 warning (OPT_Wsuggest_attribute_format,
8154 "target of initialization might be a candidate "
8155 "for a format attribute");
8156 break;
8157 case ICR_RETURN:
8158 warning (OPT_Wsuggest_attribute_format,
8159 "return type might be a candidate "
8160 "for a format attribute");
8161 break;
8162 case ICR_ASSIGN:
8163 warning (OPT_Wsuggest_attribute_format,
8164 "left-hand side of assignment might be a candidate "
8165 "for a format attribute");
8166 break;
8167 default:
8168 gcc_unreachable();
8172 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8173 does not. */
8174 if (warn_parentheses
8175 && TREE_CODE (type) == BOOLEAN_TYPE
8176 && TREE_CODE (rhs) == MODIFY_EXPR
8177 && !TREE_NO_WARNING (rhs)
8178 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8179 && (complain & tf_warning))
8181 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8183 warning_at (loc, OPT_Wparentheses,
8184 "suggest parentheses around assignment used as truth value");
8185 TREE_NO_WARNING (rhs) = 1;
8188 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8189 complain, flags);
8192 /* Convert RHS to be of type TYPE.
8193 If EXP is nonzero, it is the target of the initialization.
8194 ERRTYPE indicates what kind of error the implicit conversion is.
8196 Two major differences between the behavior of
8197 `convert_for_assignment' and `convert_for_initialization'
8198 are that references are bashed in the former, while
8199 copied in the latter, and aggregates are assigned in
8200 the former (operator=) while initialized in the
8201 latter (X(X&)).
8203 If using constructor make sure no conversion operator exists, if one does
8204 exist, an ambiguity exists. */
8206 tree
8207 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8208 impl_conv_rhs errtype, tree fndecl, int parmnum,
8209 tsubst_flags_t complain)
8211 enum tree_code codel = TREE_CODE (type);
8212 tree rhstype;
8213 enum tree_code coder;
8215 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8216 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8217 if (TREE_CODE (rhs) == NOP_EXPR
8218 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8219 && codel != REFERENCE_TYPE)
8220 rhs = TREE_OPERAND (rhs, 0);
8222 if (type == error_mark_node
8223 || rhs == error_mark_node
8224 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8225 return error_mark_node;
8227 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8228 && TREE_CODE (type) != ARRAY_TYPE
8229 && (TREE_CODE (type) != REFERENCE_TYPE
8230 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8231 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8232 && !TYPE_REFFN_P (type))
8233 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8234 rhs = decay_conversion (rhs, complain);
8236 rhstype = TREE_TYPE (rhs);
8237 coder = TREE_CODE (rhstype);
8239 if (coder == ERROR_MARK)
8240 return error_mark_node;
8242 /* We accept references to incomplete types, so we can
8243 return here before checking if RHS is of complete type. */
8245 if (codel == REFERENCE_TYPE)
8247 /* This should eventually happen in convert_arguments. */
8248 int savew = 0, savee = 0;
8250 if (fndecl)
8251 savew = warningcount + werrorcount, savee = errorcount;
8252 rhs = initialize_reference (type, rhs, flags, complain);
8254 if (fndecl
8255 && (warningcount + werrorcount > savew || errorcount > savee))
8256 inform (input_location,
8257 "in passing argument %P of %q+D", parmnum, fndecl);
8259 return rhs;
8262 if (exp != 0)
8263 exp = require_complete_type_sfinae (exp, complain);
8264 if (exp == error_mark_node)
8265 return error_mark_node;
8267 rhstype = non_reference (rhstype);
8269 type = complete_type (type);
8271 if (DIRECT_INIT_EXPR_P (type, rhs))
8272 /* Don't try to do copy-initialization if we already have
8273 direct-initialization. */
8274 return rhs;
8276 if (MAYBE_CLASS_TYPE_P (type))
8277 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8279 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8280 complain, flags);
8283 /* If RETVAL is the address of, or a reference to, a local variable or
8284 temporary give an appropriate warning. */
8286 static void
8287 maybe_warn_about_returning_address_of_local (tree retval)
8289 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8290 tree whats_returned = retval;
8292 for (;;)
8294 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8295 whats_returned = TREE_OPERAND (whats_returned, 1);
8296 else if (CONVERT_EXPR_P (whats_returned)
8297 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8298 whats_returned = TREE_OPERAND (whats_returned, 0);
8299 else
8300 break;
8303 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8304 return;
8305 whats_returned = TREE_OPERAND (whats_returned, 0);
8307 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8309 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8310 || TREE_CODE (whats_returned) == TARGET_EXPR)
8312 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8313 return;
8315 if (VAR_P (whats_returned)
8316 && DECL_NAME (whats_returned)
8317 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8319 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8320 return;
8324 while (TREE_CODE (whats_returned) == COMPONENT_REF
8325 || TREE_CODE (whats_returned) == ARRAY_REF)
8326 whats_returned = TREE_OPERAND (whats_returned, 0);
8328 if (DECL_P (whats_returned)
8329 && DECL_NAME (whats_returned)
8330 && DECL_FUNCTION_SCOPE_P (whats_returned)
8331 && !is_capture_proxy (whats_returned)
8332 && !(TREE_STATIC (whats_returned)
8333 || TREE_PUBLIC (whats_returned)))
8335 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8336 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8337 whats_returned);
8338 else
8339 warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
8340 whats_returned);
8341 return;
8345 /* Check that returning RETVAL from the current function is valid.
8346 Return an expression explicitly showing all conversions required to
8347 change RETVAL into the function return type, and to assign it to
8348 the DECL_RESULT for the function. Set *NO_WARNING to true if
8349 code reaches end of non-void function warning shouldn't be issued
8350 on this RETURN_EXPR. */
8352 tree
8353 check_return_expr (tree retval, bool *no_warning)
8355 tree result;
8356 /* The type actually returned by the function. */
8357 tree valtype;
8358 /* The type the function is declared to return, or void if
8359 the declared type is incomplete. */
8360 tree functype;
8361 int fn_returns_value_p;
8362 bool named_return_value_okay_p;
8364 *no_warning = false;
8366 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8368 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8369 "statement is not allowed");
8370 return NULL_TREE;
8373 /* A `volatile' function is one that isn't supposed to return, ever.
8374 (This is a G++ extension, used to get better code for functions
8375 that call the `volatile' function.) */
8376 if (TREE_THIS_VOLATILE (current_function_decl))
8377 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8379 /* Check for various simple errors. */
8380 if (DECL_DESTRUCTOR_P (current_function_decl))
8382 if (retval)
8383 error ("returning a value from a destructor");
8384 return NULL_TREE;
8386 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8388 if (in_function_try_handler)
8389 /* If a return statement appears in a handler of the
8390 function-try-block of a constructor, the program is ill-formed. */
8391 error ("cannot return from a handler of a function-try-block of a constructor");
8392 else if (retval)
8393 /* You can't return a value from a constructor. */
8394 error ("returning a value from a constructor");
8395 return NULL_TREE;
8398 if (processing_template_decl)
8400 current_function_returns_value = 1;
8401 if (check_for_bare_parameter_packs (retval))
8402 retval = error_mark_node;
8403 return retval;
8406 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8408 /* Deduce auto return type from a return statement. */
8409 if (current_function_auto_return_pattern)
8411 tree auto_node;
8412 tree type;
8414 if (!retval && !is_auto (current_function_auto_return_pattern))
8416 /* Give a helpful error message. */
8417 error ("return-statement with no value, in function returning %qT",
8418 current_function_auto_return_pattern);
8419 inform (input_location, "only plain %<auto%> return type can be "
8420 "deduced to %<void%>");
8421 type = error_mark_node;
8423 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8425 error ("returning initializer list");
8426 type = error_mark_node;
8428 else
8430 if (!retval)
8431 retval = void_zero_node;
8432 auto_node = type_uses_auto (current_function_auto_return_pattern);
8433 type = do_auto_deduction (current_function_auto_return_pattern,
8434 retval, auto_node);
8437 if (type == error_mark_node)
8438 /* Leave it. */;
8439 else if (functype == current_function_auto_return_pattern)
8440 apply_deduced_return_type (current_function_decl, type);
8441 else
8442 /* A mismatch should have been diagnosed in do_auto_deduction. */
8443 gcc_assert (same_type_p (type, functype));
8444 functype = type;
8447 /* When no explicit return-value is given in a function with a named
8448 return value, the named return value is used. */
8449 result = DECL_RESULT (current_function_decl);
8450 valtype = TREE_TYPE (result);
8451 gcc_assert (valtype != NULL_TREE);
8452 fn_returns_value_p = !VOID_TYPE_P (valtype);
8453 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8454 retval = result;
8456 /* Check for a return statement with no return value in a function
8457 that's supposed to return a value. */
8458 if (!retval && fn_returns_value_p)
8460 if (functype != error_mark_node)
8461 permerror (input_location, "return-statement with no value, in "
8462 "function returning %qT", valtype);
8463 /* Remember that this function did return. */
8464 current_function_returns_value = 1;
8465 /* And signal caller that TREE_NO_WARNING should be set on the
8466 RETURN_EXPR to avoid control reaches end of non-void function
8467 warnings in tree-cfg.c. */
8468 *no_warning = true;
8470 /* Check for a return statement with a value in a function that
8471 isn't supposed to return a value. */
8472 else if (retval && !fn_returns_value_p)
8474 if (VOID_TYPE_P (TREE_TYPE (retval)))
8475 /* You can return a `void' value from a function of `void'
8476 type. In that case, we have to evaluate the expression for
8477 its side-effects. */
8478 finish_expr_stmt (retval);
8479 else
8480 permerror (input_location, "return-statement with a value, in function "
8481 "returning 'void'");
8482 current_function_returns_null = 1;
8484 /* There's really no value to return, after all. */
8485 return NULL_TREE;
8487 else if (!retval)
8488 /* Remember that this function can sometimes return without a
8489 value. */
8490 current_function_returns_null = 1;
8491 else
8492 /* Remember that this function did return a value. */
8493 current_function_returns_value = 1;
8495 /* Check for erroneous operands -- but after giving ourselves a
8496 chance to provide an error about returning a value from a void
8497 function. */
8498 if (error_operand_p (retval))
8500 current_function_return_value = error_mark_node;
8501 return error_mark_node;
8504 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8505 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8506 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8507 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8508 && ! flag_check_new
8509 && retval && null_ptr_cst_p (retval))
8510 warning (0, "%<operator new%> must not return NULL unless it is "
8511 "declared %<throw()%> (or -fcheck-new is in effect)");
8513 /* Effective C++ rule 15. See also start_function. */
8514 if (warn_ecpp
8515 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8517 bool warn = true;
8519 /* The function return type must be a reference to the current
8520 class. */
8521 if (TREE_CODE (valtype) == REFERENCE_TYPE
8522 && same_type_ignoring_top_level_qualifiers_p
8523 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8525 /* Returning '*this' is obviously OK. */
8526 if (retval == current_class_ref)
8527 warn = false;
8528 /* If we are calling a function whose return type is the same of
8529 the current class reference, it is ok. */
8530 else if (INDIRECT_REF_P (retval)
8531 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8532 warn = false;
8535 if (warn)
8536 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8539 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8541 [...] For a function with a class return type, if the expression
8542 in the return statement is the name of a local object, and the cv-
8543 unqualified type of the local object is the same as the function
8544 return type, an implementation is permitted to omit creating the tem-
8545 porary object to hold the function return value [...]
8547 So, if this is a value-returning function that always returns the same
8548 local variable, remember it.
8550 It might be nice to be more flexible, and choose the first suitable
8551 variable even if the function sometimes returns something else, but
8552 then we run the risk of clobbering the variable we chose if the other
8553 returned expression uses the chosen variable somehow. And people expect
8554 this restriction, anyway. (jason 2000-11-19)
8556 See finish_function and finalize_nrv for the rest of this optimization. */
8558 named_return_value_okay_p =
8559 (retval != NULL_TREE
8560 /* Must be a local, automatic variable. */
8561 && VAR_P (retval)
8562 && DECL_CONTEXT (retval) == current_function_decl
8563 && ! TREE_STATIC (retval)
8564 /* And not a lambda or anonymous union proxy. */
8565 && !DECL_HAS_VALUE_EXPR_P (retval)
8566 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8567 /* The cv-unqualified type of the returned value must be the
8568 same as the cv-unqualified return type of the
8569 function. */
8570 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8571 (TYPE_MAIN_VARIANT (functype)))
8572 /* And the returned value must be non-volatile. */
8573 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8575 if (fn_returns_value_p && flag_elide_constructors)
8577 if (named_return_value_okay_p
8578 && (current_function_return_value == NULL_TREE
8579 || current_function_return_value == retval))
8580 current_function_return_value = retval;
8581 else
8582 current_function_return_value = error_mark_node;
8585 /* We don't need to do any conversions when there's nothing being
8586 returned. */
8587 if (!retval)
8588 return NULL_TREE;
8590 /* Do any required conversions. */
8591 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8592 /* No conversions are required. */
8594 else
8596 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8598 /* The functype's return type will have been set to void, if it
8599 was an incomplete type. Just treat this as 'return;' */
8600 if (VOID_TYPE_P (functype))
8601 return error_mark_node;
8603 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8604 treated as an rvalue for the purposes of overload resolution to
8605 favor move constructors over copy constructors.
8607 Note that these conditions are similar to, but not as strict as,
8608 the conditions for the named return value optimization. */
8609 if ((cxx_dialect != cxx98)
8610 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8611 || TREE_CODE (retval) == PARM_DECL)
8612 && DECL_CONTEXT (retval) == current_function_decl
8613 && !TREE_STATIC (retval)
8614 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8615 (TYPE_MAIN_VARIANT (functype)))
8616 /* This is only interesting for class type. */
8617 && CLASS_TYPE_P (functype))
8618 flags = flags | LOOKUP_PREFER_RVALUE;
8620 /* First convert the value to the function's return type, then
8621 to the type of return value's location to handle the
8622 case that functype is smaller than the valtype. */
8623 retval = convert_for_initialization
8624 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8625 tf_warning_or_error);
8626 retval = convert (valtype, retval);
8628 /* If the conversion failed, treat this just like `return;'. */
8629 if (retval == error_mark_node)
8630 return retval;
8631 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8632 else if (! cfun->returns_struct
8633 && TREE_CODE (retval) == TARGET_EXPR
8634 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8635 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8636 TREE_OPERAND (retval, 0));
8637 else
8638 maybe_warn_about_returning_address_of_local (retval);
8641 /* Actually copy the value returned into the appropriate location. */
8642 if (retval && retval != result)
8643 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8645 return retval;
8649 /* Returns nonzero if the pointer-type FROM can be converted to the
8650 pointer-type TO via a qualification conversion. If CONSTP is -1,
8651 then we return nonzero if the pointers are similar, and the
8652 cv-qualification signature of FROM is a proper subset of that of TO.
8654 If CONSTP is positive, then all outer pointers have been
8655 const-qualified. */
8657 static int
8658 comp_ptr_ttypes_real (tree to, tree from, int constp)
8660 bool to_more_cv_qualified = false;
8661 bool is_opaque_pointer = false;
8663 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8665 if (TREE_CODE (to) != TREE_CODE (from))
8666 return 0;
8668 if (TREE_CODE (from) == OFFSET_TYPE
8669 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8670 TYPE_OFFSET_BASETYPE (to)))
8671 return 0;
8673 /* Const and volatile mean something different for function types,
8674 so the usual checks are not appropriate. */
8675 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8677 if (!at_least_as_qualified_p (to, from))
8678 return 0;
8680 if (!at_least_as_qualified_p (from, to))
8682 if (constp == 0)
8683 return 0;
8684 to_more_cv_qualified = true;
8687 if (constp > 0)
8688 constp &= TYPE_READONLY (to);
8691 if (TREE_CODE (to) == VECTOR_TYPE)
8692 is_opaque_pointer = vector_targets_convertible_p (to, from);
8694 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8695 return ((constp >= 0 || to_more_cv_qualified)
8696 && (is_opaque_pointer
8697 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8701 /* When comparing, say, char ** to char const **, this function takes
8702 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8703 types to this function. */
8706 comp_ptr_ttypes (tree to, tree from)
8708 return comp_ptr_ttypes_real (to, from, 1);
8711 /* Returns true iff FNTYPE is a non-class type that involves
8712 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8713 if a parameter type is ill-formed. */
8715 bool
8716 error_type_p (const_tree type)
8718 tree t;
8720 switch (TREE_CODE (type))
8722 case ERROR_MARK:
8723 return true;
8725 case POINTER_TYPE:
8726 case REFERENCE_TYPE:
8727 case OFFSET_TYPE:
8728 return error_type_p (TREE_TYPE (type));
8730 case FUNCTION_TYPE:
8731 case METHOD_TYPE:
8732 if (error_type_p (TREE_TYPE (type)))
8733 return true;
8734 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8735 if (error_type_p (TREE_VALUE (t)))
8736 return true;
8737 return false;
8739 case RECORD_TYPE:
8740 if (TYPE_PTRMEMFUNC_P (type))
8741 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8742 return false;
8744 default:
8745 return false;
8749 /* Returns true if to and from are (possibly multi-level) pointers to the same
8750 type or inheritance-related types, regardless of cv-quals. */
8752 bool
8753 ptr_reasonably_similar (const_tree to, const_tree from)
8755 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8757 /* Any target type is similar enough to void. */
8758 if (VOID_TYPE_P (to))
8759 return !error_type_p (from);
8760 if (VOID_TYPE_P (from))
8761 return !error_type_p (to);
8763 if (TREE_CODE (to) != TREE_CODE (from))
8764 return false;
8766 if (TREE_CODE (from) == OFFSET_TYPE
8767 && comptypes (TYPE_OFFSET_BASETYPE (to),
8768 TYPE_OFFSET_BASETYPE (from),
8769 COMPARE_BASE | COMPARE_DERIVED))
8770 continue;
8772 if (TREE_CODE (to) == VECTOR_TYPE
8773 && vector_types_convertible_p (to, from, false))
8774 return true;
8776 if (TREE_CODE (to) == INTEGER_TYPE
8777 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8778 return true;
8780 if (TREE_CODE (to) == FUNCTION_TYPE)
8781 return !error_type_p (to) && !error_type_p (from);
8783 if (!TYPE_PTR_P (to))
8785 /* When either type is incomplete avoid DERIVED_FROM_P,
8786 which may call complete_type (c++/57942). */
8787 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8788 return comptypes
8789 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8790 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8795 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8796 pointer-to-member types) are the same, ignoring cv-qualification at
8797 all levels. */
8799 bool
8800 comp_ptr_ttypes_const (tree to, tree from)
8802 bool is_opaque_pointer = false;
8804 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8806 if (TREE_CODE (to) != TREE_CODE (from))
8807 return false;
8809 if (TREE_CODE (from) == OFFSET_TYPE
8810 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8811 TYPE_OFFSET_BASETYPE (to)))
8812 continue;
8814 if (TREE_CODE (to) == VECTOR_TYPE)
8815 is_opaque_pointer = vector_targets_convertible_p (to, from);
8817 if (!TYPE_PTR_P (to))
8818 return (is_opaque_pointer
8819 || same_type_ignoring_top_level_qualifiers_p (to, from));
8823 /* Returns the type qualifiers for this type, including the qualifiers on the
8824 elements for an array type. */
8827 cp_type_quals (const_tree type)
8829 int quals;
8830 /* This CONST_CAST is okay because strip_array_types returns its
8831 argument unmodified and we assign it to a const_tree. */
8832 type = strip_array_types (CONST_CAST_TREE (type));
8833 if (type == error_mark_node
8834 /* Quals on a FUNCTION_TYPE are memfn quals. */
8835 || TREE_CODE (type) == FUNCTION_TYPE)
8836 return TYPE_UNQUALIFIED;
8837 quals = TYPE_QUALS (type);
8838 /* METHOD and REFERENCE_TYPEs should never have quals. */
8839 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8840 && TREE_CODE (type) != REFERENCE_TYPE)
8841 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8842 == TYPE_UNQUALIFIED));
8843 return quals;
8846 /* Returns the function-ref-qualifier for TYPE */
8848 cp_ref_qualifier
8849 type_memfn_rqual (const_tree type)
8851 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8852 || TREE_CODE (type) == METHOD_TYPE);
8854 if (!FUNCTION_REF_QUALIFIED (type))
8855 return REF_QUAL_NONE;
8856 else if (FUNCTION_RVALUE_QUALIFIED (type))
8857 return REF_QUAL_RVALUE;
8858 else
8859 return REF_QUAL_LVALUE;
8862 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8863 METHOD_TYPE. */
8866 type_memfn_quals (const_tree type)
8868 if (TREE_CODE (type) == FUNCTION_TYPE)
8869 return TYPE_QUALS (type);
8870 else if (TREE_CODE (type) == METHOD_TYPE)
8871 return cp_type_quals (class_of_this_parm (type));
8872 else
8873 gcc_unreachable ();
8876 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8877 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8879 tree
8880 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8882 /* Could handle METHOD_TYPE here if necessary. */
8883 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8884 if (TYPE_QUALS (type) == memfn_quals
8885 && type_memfn_rqual (type) == rqual)
8886 return type;
8888 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8889 complex. */
8890 tree result = build_qualified_type (type, memfn_quals);
8891 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8892 return build_ref_qualified_type (result, rqual);
8895 /* Returns nonzero if TYPE is const or volatile. */
8897 bool
8898 cv_qualified_p (const_tree type)
8900 int quals = cp_type_quals (type);
8901 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8904 /* Returns nonzero if the TYPE contains a mutable member. */
8906 bool
8907 cp_has_mutable_p (const_tree type)
8909 /* This CONST_CAST is okay because strip_array_types returns its
8910 argument unmodified and we assign it to a const_tree. */
8911 type = strip_array_types (CONST_CAST_TREE(type));
8913 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8916 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8917 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8918 approximation. In particular, consider:
8920 int f();
8921 struct S { int i; };
8922 const S s = { f(); }
8924 Here, we will make "s" as TREE_READONLY (because it is declared
8925 "const") -- only to reverse ourselves upon seeing that the
8926 initializer is non-constant. */
8928 void
8929 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8931 tree type = TREE_TYPE (decl);
8933 if (type == error_mark_node)
8934 return;
8936 if (TREE_CODE (decl) == TYPE_DECL)
8937 return;
8939 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8940 && type_quals != TYPE_UNQUALIFIED));
8942 /* Avoid setting TREE_READONLY incorrectly. */
8943 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8944 constructor can produce constant init, so rely on cp_finish_decl to
8945 clear TREE_READONLY if the variable has non-constant init. */
8947 /* If the type has (or might have) a mutable component, that component
8948 might be modified. */
8949 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8950 type_quals &= ~TYPE_QUAL_CONST;
8952 c_apply_type_quals_to_decl (type_quals, decl);
8955 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8956 exemplar types such that casting T1 to T2 is casting away constness
8957 if and only if there is no implicit conversion from T1 to T2. */
8959 static void
8960 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8962 int quals1;
8963 int quals2;
8965 /* [expr.const.cast]
8967 For multi-level pointer to members and multi-level mixed pointers
8968 and pointers to members (conv.qual), the "member" aspect of a
8969 pointer to member level is ignored when determining if a const
8970 cv-qualifier has been cast away. */
8971 /* [expr.const.cast]
8973 For two pointer types:
8975 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8976 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8977 K is min(N,M)
8979 casting from X1 to X2 casts away constness if, for a non-pointer
8980 type T there does not exist an implicit conversion (clause
8981 _conv_) from:
8983 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8987 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8988 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8989 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8991 *t1 = cp_build_qualified_type (void_type_node,
8992 cp_type_quals (*t1));
8993 *t2 = cp_build_qualified_type (void_type_node,
8994 cp_type_quals (*t2));
8995 return;
8998 quals1 = cp_type_quals (*t1);
8999 quals2 = cp_type_quals (*t2);
9001 if (TYPE_PTRDATAMEM_P (*t1))
9002 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9003 else
9004 *t1 = TREE_TYPE (*t1);
9005 if (TYPE_PTRDATAMEM_P (*t2))
9006 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9007 else
9008 *t2 = TREE_TYPE (*t2);
9010 casts_away_constness_r (t1, t2, complain);
9011 *t1 = build_pointer_type (*t1);
9012 *t2 = build_pointer_type (*t2);
9013 *t1 = cp_build_qualified_type (*t1, quals1);
9014 *t2 = cp_build_qualified_type (*t2, quals2);
9017 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9018 constness.
9020 ??? This function returns non-zero if casting away qualifiers not
9021 just const. We would like to return to the caller exactly which
9022 qualifiers are casted away to give more accurate diagnostics.
9025 static bool
9026 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9028 if (TREE_CODE (t2) == REFERENCE_TYPE)
9030 /* [expr.const.cast]
9032 Casting from an lvalue of type T1 to an lvalue of type T2
9033 using a reference cast casts away constness if a cast from an
9034 rvalue of type "pointer to T1" to the type "pointer to T2"
9035 casts away constness. */
9036 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9037 return casts_away_constness (build_pointer_type (t1),
9038 build_pointer_type (TREE_TYPE (t2)),
9039 complain);
9042 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9043 /* [expr.const.cast]
9045 Casting from an rvalue of type "pointer to data member of X
9046 of type T1" to the type "pointer to data member of Y of type
9047 T2" casts away constness if a cast from an rvalue of type
9048 "pointer to T1" to the type "pointer to T2" casts away
9049 constness. */
9050 return casts_away_constness
9051 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9052 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9053 complain);
9055 /* Casting away constness is only something that makes sense for
9056 pointer or reference types. */
9057 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9058 return false;
9060 /* Top-level qualifiers don't matter. */
9061 t1 = TYPE_MAIN_VARIANT (t1);
9062 t2 = TYPE_MAIN_VARIANT (t2);
9063 casts_away_constness_r (&t1, &t2, complain);
9064 if (!can_convert_standard (t2, t1, complain))
9065 return true;
9067 return false;
9070 /* If T is a REFERENCE_TYPE return the type to which T refers.
9071 Otherwise, return T itself. */
9073 tree
9074 non_reference (tree t)
9076 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9077 t = TREE_TYPE (t);
9078 return t;
9082 /* Return nonzero if REF is an lvalue valid for this language;
9083 otherwise, print an error message and return zero. USE says
9084 how the lvalue is being used and so selects the error message. */
9087 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9089 cp_lvalue_kind kind = lvalue_kind (ref);
9091 if (kind == clk_none)
9093 if (complain & tf_error)
9094 lvalue_error (input_location, use);
9095 return 0;
9097 else if (kind & (clk_rvalueref|clk_class))
9099 if (!(complain & tf_error))
9100 return 0;
9101 if (kind & clk_class)
9102 /* Make this a permerror because we used to accept it. */
9103 permerror (input_location, "using temporary as lvalue");
9104 else
9105 error ("using xvalue (rvalue reference) as lvalue");
9107 return 1;
9110 /* Return true if a user-defined literal operator is a raw operator. */
9112 bool
9113 check_raw_literal_operator (const_tree decl)
9115 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9116 tree argtype;
9117 int arity;
9118 bool maybe_raw_p = false;
9120 /* Count the number and type of arguments and check for ellipsis. */
9121 for (argtype = argtypes, arity = 0;
9122 argtype && argtype != void_list_node;
9123 ++arity, argtype = TREE_CHAIN (argtype))
9125 tree t = TREE_VALUE (argtype);
9127 if (same_type_p (t, const_string_type_node))
9128 maybe_raw_p = true;
9130 if (!argtype)
9131 return false; /* Found ellipsis. */
9133 if (!maybe_raw_p || arity != 1)
9134 return false;
9136 return true;
9140 /* Return true if a user-defined literal operator has one of the allowed
9141 argument types. */
9143 bool
9144 check_literal_operator_args (const_tree decl,
9145 bool *long_long_unsigned_p, bool *long_double_p)
9147 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9149 *long_long_unsigned_p = false;
9150 *long_double_p = false;
9151 if (processing_template_decl || processing_specialization)
9152 return argtypes == void_list_node;
9153 else
9155 tree argtype;
9156 int arity;
9157 int max_arity = 2;
9159 /* Count the number and type of arguments and check for ellipsis. */
9160 for (argtype = argtypes, arity = 0;
9161 argtype && argtype != void_list_node;
9162 argtype = TREE_CHAIN (argtype))
9164 tree t = TREE_VALUE (argtype);
9165 ++arity;
9167 if (TYPE_PTR_P (t))
9169 bool maybe_raw_p = false;
9170 t = TREE_TYPE (t);
9171 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9172 return false;
9173 t = TYPE_MAIN_VARIANT (t);
9174 if ((maybe_raw_p = same_type_p (t, char_type_node))
9175 || same_type_p (t, wchar_type_node)
9176 || same_type_p (t, char16_type_node)
9177 || same_type_p (t, char32_type_node))
9179 argtype = TREE_CHAIN (argtype);
9180 if (!argtype)
9181 return false;
9182 t = TREE_VALUE (argtype);
9183 if (maybe_raw_p && argtype == void_list_node)
9184 return true;
9185 else if (same_type_p (t, size_type_node))
9187 ++arity;
9188 continue;
9190 else
9191 return false;
9194 else if (same_type_p (t, long_long_unsigned_type_node))
9196 max_arity = 1;
9197 *long_long_unsigned_p = true;
9199 else if (same_type_p (t, long_double_type_node))
9201 max_arity = 1;
9202 *long_double_p = true;
9204 else if (same_type_p (t, char_type_node))
9205 max_arity = 1;
9206 else if (same_type_p (t, wchar_type_node))
9207 max_arity = 1;
9208 else if (same_type_p (t, char16_type_node))
9209 max_arity = 1;
9210 else if (same_type_p (t, char32_type_node))
9211 max_arity = 1;
9212 else
9213 return false;
9215 if (!argtype)
9216 return false; /* Found ellipsis. */
9218 if (arity != max_arity)
9219 return false;
9221 return true;