Merge from trunk:
[official-gcc.git] / main / gcc / cp / typeck.c
blob373272a0d872df73c854a57e4a6c8427cf32ada8
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 bool 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;
821 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
823 /* Save space: see if the result is identical to one of the args. */
824 if (valtype == TREE_TYPE (t1) && ! p2)
825 return cp_build_type_attribute_variant (t1, attributes);
826 if (valtype == TREE_TYPE (t2) && ! p1)
827 return cp_build_type_attribute_variant (t2, attributes);
829 /* Simple way if one arg fails to specify argument types. */
830 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
831 parms = p2;
832 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
833 parms = p1;
834 else
835 parms = commonparms (p1, p2);
837 rval = build_function_type (valtype, parms);
838 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
839 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
840 rval = apply_memfn_quals (rval,
841 type_memfn_quals (t1),
842 type_memfn_rqual (t1));
843 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
844 TYPE_RAISES_EXCEPTIONS (t2));
845 t1 = build_exception_variant (rval, raises);
846 if (late_return_type_p)
847 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
848 break;
851 case METHOD_TYPE:
853 /* Get this value the long way, since TYPE_METHOD_BASETYPE
854 is just the main variant of this. */
855 tree basetype = class_of_this_parm (t2);
856 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
857 TYPE_RAISES_EXCEPTIONS (t2));
858 cp_ref_qualifier rqual = type_memfn_rqual (t1);
859 tree t3;
860 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
861 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
863 /* If this was a member function type, get back to the
864 original type of type member function (i.e., without
865 the class instance variable up front. */
866 t1 = build_function_type (TREE_TYPE (t1),
867 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
868 t2 = build_function_type (TREE_TYPE (t2),
869 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
870 t3 = merge_types (t1, t2);
871 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
872 TYPE_ARG_TYPES (t3));
873 t1 = build_exception_variant (t3, raises);
874 t1 = build_ref_qualified_type (t1, rqual);
875 if (late_return_type_1_p)
876 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
877 if (late_return_type_2_p)
878 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
879 break;
882 case TYPENAME_TYPE:
883 /* There is no need to merge attributes into a TYPENAME_TYPE.
884 When the type is instantiated it will have whatever
885 attributes result from the instantiation. */
886 return t1;
888 default:;
891 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
892 return t1;
893 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
894 return t2;
895 else
896 return cp_build_type_attribute_variant (t1, attributes);
899 /* Return the ARRAY_TYPE type without its domain. */
901 tree
902 strip_array_domain (tree type)
904 tree t2;
905 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
906 if (TYPE_DOMAIN (type) == NULL_TREE)
907 return type;
908 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
909 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
912 /* Wrapper around cp_common_type that is used by c-common.c and other
913 front end optimizations that remove promotions.
915 Return the common type for two arithmetic types T1 and T2 under the
916 usual arithmetic conversions. The default conversions have already
917 been applied, and enumerated types converted to their compatible
918 integer types. */
920 tree
921 common_type (tree t1, tree t2)
923 /* If one type is nonsense, use the other */
924 if (t1 == error_mark_node)
925 return t2;
926 if (t2 == error_mark_node)
927 return t1;
929 return cp_common_type (t1, t2);
932 /* Return the common type of two pointer types T1 and T2. This is the
933 type for the result of most arithmetic operations if the operands
934 have the given two types.
936 We assume that comp_target_types has already been done and returned
937 nonzero; if that isn't so, this may crash. */
939 tree
940 common_pointer_type (tree t1, tree t2)
942 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
943 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
944 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
946 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
947 CPO_CONVERSION, tf_warning_or_error);
950 /* Compare two exception specifier types for exactness or subsetness, if
951 allowed. Returns false for mismatch, true for match (same, or
952 derived and !exact).
954 [except.spec] "If a class X ... objects of class X or any class publicly
955 and unambiguously derived from X. Similarly, if a pointer type Y * ...
956 exceptions of type Y * or that are pointers to any type publicly and
957 unambiguously derived from Y. Otherwise a function only allows exceptions
958 that have the same type ..."
959 This does not mention cv qualifiers and is different to what throw
960 [except.throw] and catch [except.catch] will do. They will ignore the
961 top level cv qualifiers, and allow qualifiers in the pointer to class
962 example.
964 We implement the letter of the standard. */
966 static bool
967 comp_except_types (tree a, tree b, bool exact)
969 if (same_type_p (a, b))
970 return true;
971 else if (!exact)
973 if (cp_type_quals (a) || cp_type_quals (b))
974 return false;
976 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
978 a = TREE_TYPE (a);
979 b = TREE_TYPE (b);
980 if (cp_type_quals (a) || cp_type_quals (b))
981 return false;
984 if (TREE_CODE (a) != RECORD_TYPE
985 || TREE_CODE (b) != RECORD_TYPE)
986 return false;
988 if (publicly_uniquely_derived_p (a, b))
989 return true;
991 return false;
994 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
995 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
996 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
997 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
998 are unordered, but we've already filtered out duplicates. Most lists will
999 be in order, we should try to make use of that. */
1001 bool
1002 comp_except_specs (const_tree t1, const_tree t2, int exact)
1004 const_tree probe;
1005 const_tree base;
1006 int length = 0;
1008 if (t1 == t2)
1009 return true;
1011 /* First handle noexcept. */
1012 if (exact < ce_exact)
1014 /* noexcept(false) is compatible with no exception-specification,
1015 and stricter than any spec. */
1016 if (t1 == noexcept_false_spec)
1017 return t2 == NULL_TREE || exact == ce_derived;
1018 /* Even a derived noexcept(false) is compatible with no
1019 exception-specification. */
1020 if (t2 == noexcept_false_spec)
1021 return t1 == NULL_TREE;
1023 /* Otherwise, if we aren't looking for an exact match, noexcept is
1024 equivalent to throw(). */
1025 if (t1 == noexcept_true_spec)
1026 t1 = empty_except_spec;
1027 if (t2 == noexcept_true_spec)
1028 t2 = empty_except_spec;
1031 /* If any noexcept is left, it is only comparable to itself;
1032 either we're looking for an exact match or we're redeclaring a
1033 template with dependent noexcept. */
1034 if ((t1 && TREE_PURPOSE (t1))
1035 || (t2 && TREE_PURPOSE (t2)))
1036 return (t1 && t2
1037 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1039 if (t1 == NULL_TREE) /* T1 is ... */
1040 return t2 == NULL_TREE || exact == ce_derived;
1041 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1042 return t2 != NULL_TREE && !TREE_VALUE (t2);
1043 if (t2 == NULL_TREE) /* T2 is ... */
1044 return false;
1045 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1046 return exact == ce_derived;
1048 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1049 Count how many we find, to determine exactness. For exact matching and
1050 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1051 O(nm). */
1052 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1054 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1056 tree a = TREE_VALUE (probe);
1057 tree b = TREE_VALUE (t2);
1059 if (comp_except_types (a, b, exact))
1061 if (probe == base && exact > ce_derived)
1062 base = TREE_CHAIN (probe);
1063 length++;
1064 break;
1067 if (probe == NULL_TREE)
1068 return false;
1070 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1073 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1074 [] can match [size]. */
1076 static bool
1077 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1079 tree d1;
1080 tree d2;
1081 tree max1, max2;
1083 if (t1 == t2)
1084 return true;
1086 /* The type of the array elements must be the same. */
1087 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1088 return false;
1090 d1 = TYPE_DOMAIN (t1);
1091 d2 = TYPE_DOMAIN (t2);
1093 if (d1 == d2)
1094 return true;
1096 /* If one of the arrays is dimensionless, and the other has a
1097 dimension, they are of different types. However, it is valid to
1098 write:
1100 extern int a[];
1101 int a[3];
1103 by [basic.link]:
1105 declarations for an array object can specify
1106 array types that differ by the presence or absence of a major
1107 array bound (_dcl.array_). */
1108 if (!d1 || !d2)
1109 return allow_redeclaration;
1111 /* Check that the dimensions are the same. */
1113 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1114 return false;
1115 max1 = TYPE_MAX_VALUE (d1);
1116 max2 = TYPE_MAX_VALUE (d2);
1118 if (!cp_tree_equal (max1, max2))
1119 return false;
1121 return true;
1124 /* Compare the relative position of T1 and T2 into their respective
1125 template parameter list.
1126 T1 and T2 must be template parameter types.
1127 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1129 static bool
1130 comp_template_parms_position (tree t1, tree t2)
1132 tree index1, index2;
1133 gcc_assert (t1 && t2
1134 && TREE_CODE (t1) == TREE_CODE (t2)
1135 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1136 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1137 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1139 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1140 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1142 /* Then compare their relative position. */
1143 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1144 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1145 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1146 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1147 return false;
1149 /* In C++14 we can end up comparing 'auto' to a normal template
1150 parameter. Don't confuse them. */
1151 if (cxx_dialect >= cxx1y && (is_auto (t1) || is_auto (t2)))
1152 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1154 return true;
1157 /* Subroutine in comptypes. */
1159 static bool
1160 structural_comptypes (tree t1, tree t2, int strict)
1162 if (t1 == t2)
1163 return true;
1165 /* Suppress errors caused by previously reported errors. */
1166 if (t1 == error_mark_node || t2 == error_mark_node)
1167 return false;
1169 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1171 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1172 current instantiation. */
1173 if (TREE_CODE (t1) == TYPENAME_TYPE)
1174 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1176 if (TREE_CODE (t2) == TYPENAME_TYPE)
1177 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1179 if (TYPE_PTRMEMFUNC_P (t1))
1180 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1181 if (TYPE_PTRMEMFUNC_P (t2))
1182 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1184 /* Different classes of types can't be compatible. */
1185 if (TREE_CODE (t1) != TREE_CODE (t2))
1186 return false;
1188 /* Qualifiers must match. For array types, we will check when we
1189 recur on the array element types. */
1190 if (TREE_CODE (t1) != ARRAY_TYPE
1191 && cp_type_quals (t1) != cp_type_quals (t2))
1192 return false;
1193 if (TREE_CODE (t1) == FUNCTION_TYPE
1194 && type_memfn_quals (t1) != type_memfn_quals (t2))
1195 return false;
1196 /* Need to check this before TYPE_MAIN_VARIANT.
1197 FIXME function qualifiers should really change the main variant. */
1198 if ((TREE_CODE (t1) == FUNCTION_TYPE
1199 || TREE_CODE (t1) == METHOD_TYPE)
1200 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1201 return false;
1202 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1203 return false;
1205 /* Allow for two different type nodes which have essentially the same
1206 definition. Note that we already checked for equality of the type
1207 qualifiers (just above). */
1209 if (TREE_CODE (t1) != ARRAY_TYPE
1210 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1211 return true;
1214 /* Compare the types. Break out if they could be the same. */
1215 switch (TREE_CODE (t1))
1217 case VOID_TYPE:
1218 case BOOLEAN_TYPE:
1219 /* All void and bool types are the same. */
1220 break;
1222 case INTEGER_TYPE:
1223 case FIXED_POINT_TYPE:
1224 case REAL_TYPE:
1225 /* With these nodes, we can't determine type equivalence by
1226 looking at what is stored in the nodes themselves, because
1227 two nodes might have different TYPE_MAIN_VARIANTs but still
1228 represent the same type. For example, wchar_t and int could
1229 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1230 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1231 and are distinct types. On the other hand, int and the
1232 following typedef
1234 typedef int INT __attribute((may_alias));
1236 have identical properties, different TYPE_MAIN_VARIANTs, but
1237 represent the same type. The canonical type system keeps
1238 track of equivalence in this case, so we fall back on it. */
1239 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1241 case TEMPLATE_TEMPLATE_PARM:
1242 case BOUND_TEMPLATE_TEMPLATE_PARM:
1243 if (!comp_template_parms_position (t1, t2))
1244 return false;
1245 if (!comp_template_parms
1246 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1247 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1248 return false;
1249 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1250 break;
1251 /* Don't check inheritance. */
1252 strict = COMPARE_STRICT;
1253 /* Fall through. */
1255 case RECORD_TYPE:
1256 case UNION_TYPE:
1257 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1258 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1259 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1260 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1261 break;
1263 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1264 break;
1265 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1266 break;
1268 return false;
1270 case OFFSET_TYPE:
1271 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1272 strict & ~COMPARE_REDECLARATION))
1273 return false;
1274 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1275 return false;
1276 break;
1278 case REFERENCE_TYPE:
1279 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1280 return false;
1281 /* fall through to checks for pointer types */
1283 case POINTER_TYPE:
1284 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1285 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1286 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1287 return false;
1288 break;
1290 case METHOD_TYPE:
1291 case FUNCTION_TYPE:
1292 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1293 return false;
1294 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1295 return false;
1296 break;
1298 case ARRAY_TYPE:
1299 /* Target types must match incl. qualifiers. */
1300 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1301 return false;
1302 break;
1304 case TEMPLATE_TYPE_PARM:
1305 /* If T1 and T2 don't have the same relative position in their
1306 template parameters set, they can't be equal. */
1307 if (!comp_template_parms_position (t1, t2))
1308 return false;
1309 break;
1311 case TYPENAME_TYPE:
1312 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1313 TYPENAME_TYPE_FULLNAME (t2)))
1314 return false;
1315 /* Qualifiers don't matter on scopes. */
1316 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1317 TYPE_CONTEXT (t2)))
1318 return false;
1319 break;
1321 case UNBOUND_CLASS_TEMPLATE:
1322 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1323 return false;
1324 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1325 return false;
1326 break;
1328 case COMPLEX_TYPE:
1329 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1330 return false;
1331 break;
1333 case VECTOR_TYPE:
1334 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1335 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1336 return false;
1337 break;
1339 case TYPE_PACK_EXPANSION:
1340 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1341 PACK_EXPANSION_PATTERN (t2))
1342 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1343 PACK_EXPANSION_EXTRA_ARGS (t2)));
1345 case DECLTYPE_TYPE:
1346 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1347 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1348 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1349 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1350 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1351 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1352 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1353 DECLTYPE_TYPE_EXPR (t2)))
1354 return false;
1355 break;
1357 case UNDERLYING_TYPE:
1358 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1359 UNDERLYING_TYPE_TYPE (t2));
1361 default:
1362 return false;
1365 /* If we get here, we know that from a target independent POV the
1366 types are the same. Make sure the target attributes are also
1367 the same. */
1368 return comp_type_attributes (t1, t2);
1371 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1372 is a bitwise-or of the COMPARE_* flags. */
1374 bool
1375 comptypes (tree t1, tree t2, int strict)
1377 if (strict == COMPARE_STRICT)
1379 if (t1 == t2)
1380 return true;
1382 if (t1 == error_mark_node || t2 == error_mark_node)
1383 return false;
1385 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1386 /* At least one of the types requires structural equality, so
1387 perform a deep check. */
1388 return structural_comptypes (t1, t2, strict);
1390 #ifdef ENABLE_CHECKING
1391 if (USE_CANONICAL_TYPES)
1393 bool result = structural_comptypes (t1, t2, strict);
1395 if (result && (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2)
1396 /* In LIPO mode, the builtin functions are shared across
1397 different TUs. The parameter type of the builtin may
1398 not be the same instance as the arg type. */
1399 && !L_IPO_COMP_MODE))
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 (int q1, int q2)
1459 if (q1 == q2)
1460 return 0;
1462 if ((q1 & q2) == q2)
1463 return 1;
1464 else if ((q1 & q2) == q1)
1465 return -1;
1467 return 0;
1471 comp_cv_qualification (const_tree type1, const_tree type2)
1473 int q1 = cp_type_quals (type1);
1474 int q2 = cp_type_quals (type2);
1475 return comp_cv_qualification (q1, q2);
1478 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1479 subset of the cv-qualification signature of TYPE2, and the types
1480 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1483 comp_cv_qual_signature (tree type1, tree type2)
1485 if (comp_ptr_ttypes_real (type2, type1, -1))
1486 return 1;
1487 else if (comp_ptr_ttypes_real (type1, type2, -1))
1488 return -1;
1489 else
1490 return 0;
1493 /* Subroutines of `comptypes'. */
1495 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1496 equivalent in the sense that functions with those parameter types
1497 can have equivalent types. The two lists must be equivalent,
1498 element by element. */
1500 bool
1501 compparms (const_tree parms1, const_tree parms2)
1503 const_tree t1, t2;
1505 /* An unspecified parmlist matches any specified parmlist
1506 whose argument types don't need default promotions. */
1508 for (t1 = parms1, t2 = parms2;
1509 t1 || t2;
1510 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1512 /* If one parmlist is shorter than the other,
1513 they fail to match. */
1514 if (!t1 || !t2)
1515 return false;
1516 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1517 return false;
1519 return true;
1523 /* Process a sizeof or alignof expression where the operand is a
1524 type. */
1526 tree
1527 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1529 tree value;
1530 bool dependent_p;
1532 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1533 if (type == error_mark_node)
1534 return error_mark_node;
1536 type = non_reference (type);
1537 if (TREE_CODE (type) == METHOD_TYPE)
1539 if (complain)
1540 pedwarn (input_location, OPT_Wpointer_arith,
1541 "invalid application of %qs to a member function",
1542 operator_name_info[(int) op].name);
1543 value = size_one_node;
1546 dependent_p = dependent_type_p (type);
1547 if (!dependent_p)
1548 complete_type (type);
1549 if (dependent_p
1550 /* VLA types will have a non-constant size. In the body of an
1551 uninstantiated template, we don't need to try to compute the
1552 value, because the sizeof expression is not an integral
1553 constant expression in that case. And, if we do try to
1554 compute the value, we'll likely end up with SAVE_EXPRs, which
1555 the template substitution machinery does not expect to see. */
1556 || (processing_template_decl
1557 && COMPLETE_TYPE_P (type)
1558 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1560 value = build_min (op, size_type_node, type);
1561 TREE_READONLY (value) = 1;
1562 return value;
1565 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)
1566 && (flag_iso || warn_vla > 0))
1568 if (complain & tf_warning_or_error)
1569 pedwarn (input_location, OPT_Wvla,
1570 "taking sizeof array of runtime bound");
1571 else
1572 return error_mark_node;
1575 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1576 op == SIZEOF_EXPR, false,
1577 complain);
1580 /* Return the size of the type, without producing any warnings for
1581 types whose size cannot be taken. This routine should be used only
1582 in some other routine that has already produced a diagnostic about
1583 using the size of such a type. */
1584 tree
1585 cxx_sizeof_nowarn (tree type)
1587 if (TREE_CODE (type) == FUNCTION_TYPE
1588 || VOID_TYPE_P (type)
1589 || TREE_CODE (type) == ERROR_MARK)
1590 return size_one_node;
1591 else if (!COMPLETE_TYPE_P (type))
1592 return size_zero_node;
1593 else
1594 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1597 /* Process a sizeof expression where the operand is an expression. */
1599 static tree
1600 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1602 if (e == error_mark_node)
1603 return error_mark_node;
1605 if (processing_template_decl)
1607 e = build_min (SIZEOF_EXPR, size_type_node, e);
1608 TREE_SIDE_EFFECTS (e) = 0;
1609 TREE_READONLY (e) = 1;
1611 return e;
1614 /* To get the size of a static data member declared as an array of
1615 unknown bound, we need to instantiate it. */
1616 if (VAR_P (e)
1617 && VAR_HAD_UNKNOWN_BOUND (e)
1618 && DECL_TEMPLATE_INSTANTIATION (e))
1619 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1621 if (TREE_CODE (e) == PARM_DECL
1622 && DECL_ARRAY_PARAMETER_P (e)
1623 && (complain & tf_warning))
1625 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1626 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1627 inform (DECL_SOURCE_LOCATION (e), "declared here");
1630 e = mark_type_use (e);
1632 if (TREE_CODE (e) == COMPONENT_REF
1633 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1634 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1636 if (complain & tf_error)
1637 error ("invalid application of %<sizeof%> to a bit-field");
1638 else
1639 return error_mark_node;
1640 e = char_type_node;
1642 else if (is_overloaded_fn (e))
1644 if (complain & tf_error)
1645 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1646 "function type");
1647 else
1648 return error_mark_node;
1649 e = char_type_node;
1651 else if (type_unknown_p (e))
1653 if (complain & tf_error)
1654 cxx_incomplete_type_error (e, TREE_TYPE (e));
1655 else
1656 return error_mark_node;
1657 e = char_type_node;
1659 else
1660 e = TREE_TYPE (e);
1662 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1665 /* Implement the __alignof keyword: Return the minimum required
1666 alignment of E, measured in bytes. For VAR_DECL's and
1667 FIELD_DECL's return DECL_ALIGN (which can be set from an
1668 "aligned" __attribute__ specification). */
1670 static tree
1671 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1673 tree t;
1675 if (e == error_mark_node)
1676 return error_mark_node;
1678 if (processing_template_decl)
1680 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1681 TREE_SIDE_EFFECTS (e) = 0;
1682 TREE_READONLY (e) = 1;
1684 return e;
1687 e = mark_type_use (e);
1689 if (VAR_P (e))
1690 t = size_int (DECL_ALIGN_UNIT (e));
1691 else if (TREE_CODE (e) == COMPONENT_REF
1692 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1693 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1695 if (complain & tf_error)
1696 error ("invalid application of %<__alignof%> to a bit-field");
1697 else
1698 return error_mark_node;
1699 t = size_one_node;
1701 else if (TREE_CODE (e) == COMPONENT_REF
1702 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1703 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1704 else if (is_overloaded_fn (e))
1706 if (complain & tf_error)
1707 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1708 "function type");
1709 else
1710 return error_mark_node;
1711 if (TREE_CODE (e) == FUNCTION_DECL)
1712 t = size_int (DECL_ALIGN_UNIT (e));
1713 else
1714 t = size_one_node;
1716 else if (type_unknown_p (e))
1718 if (complain & tf_error)
1719 cxx_incomplete_type_error (e, TREE_TYPE (e));
1720 else
1721 return error_mark_node;
1722 t = size_one_node;
1724 else
1725 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1726 complain & tf_error);
1728 return fold_convert (size_type_node, t);
1731 /* Process a sizeof or alignof expression E with code OP where the operand
1732 is an expression. */
1734 tree
1735 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1737 if (op == SIZEOF_EXPR)
1738 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1739 else
1740 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1743 /* Build a representation of an expression 'alignas(E).' Return the
1744 folded integer value of E if it is an integral constant expression
1745 that resolves to a valid alignment. If E depends on a template
1746 parameter, return a syntactic representation tree of kind
1747 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1748 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1750 tree
1751 cxx_alignas_expr (tree e)
1753 if (e == NULL_TREE || e == error_mark_node
1754 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1755 return e;
1757 if (TYPE_P (e))
1758 /* [dcl.align]/3:
1760 When the alignment-specifier is of the form
1761 alignas(type-id ), it shall have the same effect as
1762 alignas(alignof(type-id )). */
1764 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1766 /* If we reach this point, it means the alignas expression if of
1767 the form "alignas(assignment-expression)", so we should follow
1768 what is stated by [dcl.align]/2. */
1770 if (value_dependent_expression_p (e))
1771 /* Leave value-dependent expression alone for now. */
1772 return e;
1774 e = fold_non_dependent_expr (e);
1775 e = mark_rvalue_use (e);
1777 /* [dcl.align]/2 says:
1779 the assignment-expression shall be an integral constant
1780 expression. */
1782 return cxx_constant_value (e);
1786 /* EXPR is being used in a context that is not a function call.
1787 Enforce:
1789 [expr.ref]
1791 The expression can be used only as the left-hand operand of a
1792 member function call.
1794 [expr.mptr.operator]
1796 If the result of .* or ->* is a function, then that result can be
1797 used only as the operand for the function call operator ().
1799 by issuing an error message if appropriate. Returns true iff EXPR
1800 violates these rules. */
1802 bool
1803 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1805 if (expr == NULL_TREE)
1806 return false;
1807 /* Don't enforce this in MS mode. */
1808 if (flag_ms_extensions)
1809 return false;
1810 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1811 expr = get_first_fn (expr);
1812 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1814 if (complain & tf_error)
1815 error ("invalid use of non-static member function");
1816 return true;
1818 return false;
1821 /* If EXP is a reference to a bitfield, and the type of EXP does not
1822 match the declared type of the bitfield, return the declared type
1823 of the bitfield. Otherwise, return NULL_TREE. */
1825 tree
1826 is_bitfield_expr_with_lowered_type (const_tree exp)
1828 switch (TREE_CODE (exp))
1830 case COND_EXPR:
1831 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1832 ? TREE_OPERAND (exp, 1)
1833 : TREE_OPERAND (exp, 0)))
1834 return NULL_TREE;
1835 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1837 case COMPOUND_EXPR:
1838 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1840 case MODIFY_EXPR:
1841 case SAVE_EXPR:
1842 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1844 case COMPONENT_REF:
1846 tree field;
1848 field = TREE_OPERAND (exp, 1);
1849 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1850 return NULL_TREE;
1851 if (same_type_ignoring_top_level_qualifiers_p
1852 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1853 return NULL_TREE;
1854 return DECL_BIT_FIELD_TYPE (field);
1857 CASE_CONVERT:
1858 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1859 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1860 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1861 /* Fallthrough. */
1863 default:
1864 return NULL_TREE;
1868 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1869 bitfield with a lowered type, the type of EXP is returned, rather
1870 than NULL_TREE. */
1872 tree
1873 unlowered_expr_type (const_tree exp)
1875 tree type;
1876 tree etype = TREE_TYPE (exp);
1878 type = is_bitfield_expr_with_lowered_type (exp);
1879 if (type)
1880 type = cp_build_qualified_type (type, cp_type_quals (etype));
1881 else
1882 type = etype;
1884 return type;
1887 /* Perform the conversions in [expr] that apply when an lvalue appears
1888 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1889 function-to-pointer conversions. In addition, manifest constants
1890 are replaced by their values, and bitfield references are converted
1891 to their declared types. Note that this function does not perform the
1892 lvalue-to-rvalue conversion for class types. If you need that conversion
1893 to for class types, then you probably need to use force_rvalue.
1895 Although the returned value is being used as an rvalue, this
1896 function does not wrap the returned expression in a
1897 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1898 that the return value is no longer an lvalue. */
1900 tree
1901 decay_conversion (tree exp, tsubst_flags_t complain)
1903 tree type;
1904 enum tree_code code;
1905 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1907 type = TREE_TYPE (exp);
1908 if (type == error_mark_node)
1909 return error_mark_node;
1911 exp = mark_rvalue_use (exp);
1913 exp = resolve_nondeduced_context (exp);
1914 if (type_unknown_p (exp))
1916 if (complain & tf_error)
1917 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1918 return error_mark_node;
1921 code = TREE_CODE (type);
1923 /* For an array decl decay_conversion should not try to return its
1924 initializer. */
1925 if (code != ARRAY_TYPE)
1927 /* FIXME remove? at least need to remember that this isn't really a
1928 constant expression if EXP isn't decl_constant_var_p, like with
1929 C_MAYBE_CONST_EXPR. */
1930 exp = decl_constant_value_safe (exp);
1931 if (error_operand_p (exp))
1932 return error_mark_node;
1935 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1936 return nullptr_node;
1938 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1939 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1940 if (code == VOID_TYPE)
1942 if (complain & tf_error)
1943 error_at (loc, "void value not ignored as it ought to be");
1944 return error_mark_node;
1946 if (invalid_nonstatic_memfn_p (exp, complain))
1947 return error_mark_node;
1948 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1949 return cp_build_addr_expr (exp, complain);
1950 if (code == ARRAY_TYPE)
1952 tree adr;
1953 tree ptrtype;
1955 if (INDIRECT_REF_P (exp))
1956 return build_nop (build_pointer_type (TREE_TYPE (type)),
1957 TREE_OPERAND (exp, 0));
1959 if (TREE_CODE (exp) == COMPOUND_EXPR)
1961 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1962 if (op1 == error_mark_node)
1963 return error_mark_node;
1964 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1965 TREE_OPERAND (exp, 0), op1);
1968 if (!lvalue_p (exp)
1969 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1971 if (complain & tf_error)
1972 error_at (loc, "invalid use of non-lvalue array");
1973 return error_mark_node;
1976 /* Don't let an array compound literal decay to a pointer. It can
1977 still be used to initialize an array or bind to a reference. */
1978 if (TREE_CODE (exp) == TARGET_EXPR)
1980 if (complain & tf_error)
1981 error_at (loc, "taking address of temporary array");
1982 return error_mark_node;
1985 ptrtype = build_pointer_type (TREE_TYPE (type));
1987 if (VAR_P (exp))
1989 if (!cxx_mark_addressable (exp))
1990 return error_mark_node;
1991 adr = build_nop (ptrtype, build_address (exp));
1992 return adr;
1994 /* This way is better for a COMPONENT_REF since it can
1995 simplify the offset for a component. */
1996 adr = cp_build_addr_expr (exp, complain);
1997 return cp_convert (ptrtype, adr, complain);
2000 /* If a bitfield is used in a context where integral promotion
2001 applies, then the caller is expected to have used
2002 default_conversion. That function promotes bitfields correctly
2003 before calling this function. At this point, if we have a
2004 bitfield referenced, we may assume that is not subject to
2005 promotion, and that, therefore, the type of the resulting rvalue
2006 is the declared type of the bitfield. */
2007 exp = convert_bitfield_to_declared_type (exp);
2009 /* We do not call rvalue() here because we do not want to wrap EXP
2010 in a NON_LVALUE_EXPR. */
2012 /* [basic.lval]
2014 Non-class rvalues always have cv-unqualified types. */
2015 type = TREE_TYPE (exp);
2016 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2017 exp = build_nop (cv_unqualified (type), exp);
2019 return exp;
2022 /* Perform preparatory conversions, as part of the "usual arithmetic
2023 conversions". In particular, as per [expr]:
2025 Whenever an lvalue expression appears as an operand of an
2026 operator that expects the rvalue for that operand, the
2027 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2028 standard conversions are applied to convert the expression to an
2029 rvalue.
2031 In addition, we perform integral promotions here, as those are
2032 applied to both operands to a binary operator before determining
2033 what additional conversions should apply. */
2035 static tree
2036 cp_default_conversion (tree exp, tsubst_flags_t complain)
2038 /* Check for target-specific promotions. */
2039 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2040 if (promoted_type)
2041 exp = cp_convert (promoted_type, exp, complain);
2042 /* Perform the integral promotions first so that bitfield
2043 expressions (which may promote to "int", even if the bitfield is
2044 declared "unsigned") are promoted correctly. */
2045 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2046 exp = cp_perform_integral_promotions (exp, complain);
2047 /* Perform the other conversions. */
2048 exp = decay_conversion (exp, complain);
2050 return exp;
2053 /* C version. */
2055 tree
2056 default_conversion (tree exp)
2058 return cp_default_conversion (exp, tf_warning_or_error);
2061 /* EXPR is an expression with an integral or enumeration type.
2062 Perform the integral promotions in [conv.prom], and return the
2063 converted value. */
2065 tree
2066 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2068 tree type;
2069 tree promoted_type;
2071 expr = mark_rvalue_use (expr);
2073 /* [conv.prom]
2075 If the bitfield has an enumerated type, it is treated as any
2076 other value of that type for promotion purposes. */
2077 type = is_bitfield_expr_with_lowered_type (expr);
2078 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2079 type = TREE_TYPE (expr);
2080 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2081 /* Scoped enums don't promote. */
2082 if (SCOPED_ENUM_P (type))
2083 return expr;
2084 promoted_type = type_promotes_to (type);
2085 if (type != promoted_type)
2086 expr = cp_convert (promoted_type, expr, complain);
2087 return expr;
2090 /* C version. */
2092 tree
2093 perform_integral_promotions (tree expr)
2095 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2098 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2099 decay_conversion to one. */
2102 string_conv_p (const_tree totype, const_tree exp, int warn)
2104 tree t;
2106 if (!TYPE_PTR_P (totype))
2107 return 0;
2109 t = TREE_TYPE (totype);
2110 if (!same_type_p (t, char_type_node)
2111 && !same_type_p (t, char16_type_node)
2112 && !same_type_p (t, char32_type_node)
2113 && !same_type_p (t, wchar_type_node))
2114 return 0;
2116 if (TREE_CODE (exp) == STRING_CST)
2118 /* Make sure that we don't try to convert between char and wide chars. */
2119 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2120 return 0;
2122 else
2124 /* Is this a string constant which has decayed to 'const char *'? */
2125 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2126 if (!same_type_p (TREE_TYPE (exp), t))
2127 return 0;
2128 STRIP_NOPS (exp);
2129 if (TREE_CODE (exp) != ADDR_EXPR
2130 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2131 return 0;
2134 /* This warning is not very useful, as it complains about printf. */
2135 if (warn)
2136 warning (OPT_Wwrite_strings,
2137 "deprecated conversion from string constant to %qT",
2138 totype);
2140 return 1;
2143 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2144 can, for example, use as an lvalue. This code used to be in
2145 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2146 expressions, where we're dealing with aggregates. But now it's again only
2147 called from unary_complex_lvalue. The case (in particular) that led to
2148 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2149 get it there. */
2151 static tree
2152 rationalize_conditional_expr (enum tree_code code, tree t,
2153 tsubst_flags_t complain)
2155 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2157 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2158 the first operand is always the one to be used if both operands
2159 are equal, so we know what conditional expression this used to be. */
2160 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2162 tree op0 = TREE_OPERAND (t, 0);
2163 tree op1 = TREE_OPERAND (t, 1);
2165 /* The following code is incorrect if either operand side-effects. */
2166 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2167 && !TREE_SIDE_EFFECTS (op1));
2168 return
2169 build_conditional_expr (loc,
2170 build_x_binary_op (loc,
2171 (TREE_CODE (t) == MIN_EXPR
2172 ? LE_EXPR : GE_EXPR),
2173 op0, TREE_CODE (op0),
2174 op1, TREE_CODE (op1),
2175 /*overload=*/NULL,
2176 complain),
2177 cp_build_unary_op (code, op0, 0, complain),
2178 cp_build_unary_op (code, op1, 0, complain),
2179 complain);
2182 return
2183 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2184 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2185 complain),
2186 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2187 complain),
2188 complain);
2191 /* Given the TYPE of an anonymous union field inside T, return the
2192 FIELD_DECL for the field. If not found return NULL_TREE. Because
2193 anonymous unions can nest, we must also search all anonymous unions
2194 that are directly reachable. */
2196 tree
2197 lookup_anon_field (tree t, tree type)
2199 tree field;
2201 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2203 if (TREE_STATIC (field))
2204 continue;
2205 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2206 continue;
2208 /* If we find it directly, return the field. */
2209 if (DECL_NAME (field) == NULL_TREE
2210 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2212 return field;
2215 /* Otherwise, it could be nested, search harder. */
2216 if (DECL_NAME (field) == NULL_TREE
2217 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2219 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2220 if (subfield)
2221 return subfield;
2224 return NULL_TREE;
2227 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2228 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2229 non-NULL, it indicates the path to the base used to name MEMBER.
2230 If PRESERVE_REFERENCE is true, the expression returned will have
2231 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2232 returned will have the type referred to by the reference.
2234 This function does not perform access control; that is either done
2235 earlier by the parser when the name of MEMBER is resolved to MEMBER
2236 itself, or later when overload resolution selects one of the
2237 functions indicated by MEMBER. */
2239 tree
2240 build_class_member_access_expr (tree object, tree member,
2241 tree access_path, bool preserve_reference,
2242 tsubst_flags_t complain)
2244 tree object_type;
2245 tree member_scope;
2246 tree result = NULL_TREE;
2247 tree using_decl = NULL_TREE;
2249 if (error_operand_p (object) || error_operand_p (member))
2250 return error_mark_node;
2252 gcc_assert (DECL_P (member) || BASELINK_P (member));
2254 /* [expr.ref]
2256 The type of the first expression shall be "class object" (of a
2257 complete type). */
2258 object_type = TREE_TYPE (object);
2259 if (!currently_open_class (object_type)
2260 && !complete_type_or_maybe_complain (object_type, object, complain))
2261 return error_mark_node;
2262 if (!CLASS_TYPE_P (object_type))
2264 if (complain & tf_error)
2266 if (POINTER_TYPE_P (object_type)
2267 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2268 error ("request for member %qD in %qE, which is of pointer "
2269 "type %qT (maybe you meant to use %<->%> ?)",
2270 member, object, object_type);
2271 else
2272 error ("request for member %qD in %qE, which is of non-class "
2273 "type %qT", member, object, object_type);
2275 return error_mark_node;
2278 /* The standard does not seem to actually say that MEMBER must be a
2279 member of OBJECT_TYPE. However, that is clearly what is
2280 intended. */
2281 if (DECL_P (member))
2283 member_scope = DECL_CLASS_CONTEXT (member);
2284 mark_used (member);
2285 if (TREE_DEPRECATED (member))
2286 warn_deprecated_use (member, NULL_TREE);
2288 else
2289 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2290 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2291 presently be the anonymous union. Go outwards until we find a
2292 type related to OBJECT_TYPE. */
2293 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2294 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2295 object_type))
2296 member_scope = TYPE_CONTEXT (member_scope);
2297 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2299 if (complain & tf_error)
2301 if (TREE_CODE (member) == FIELD_DECL)
2302 error ("invalid use of nonstatic data member %qE", member);
2303 else
2304 error ("%qD is not a member of %qT", member, object_type);
2306 return error_mark_node;
2309 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2310 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2311 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2313 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2314 if (temp)
2315 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2318 /* In [expr.ref], there is an explicit list of the valid choices for
2319 MEMBER. We check for each of those cases here. */
2320 if (VAR_P (member))
2322 /* A static data member. */
2323 result = member;
2324 mark_exp_read (object);
2325 /* If OBJECT has side-effects, they are supposed to occur. */
2326 if (TREE_SIDE_EFFECTS (object))
2327 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2329 else if (TREE_CODE (member) == FIELD_DECL)
2331 /* A non-static data member. */
2332 bool null_object_p;
2333 int type_quals;
2334 tree member_type;
2336 null_object_p = (INDIRECT_REF_P (object)
2337 && integer_zerop (TREE_OPERAND (object, 0)));
2339 /* Convert OBJECT to the type of MEMBER. */
2340 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2341 TYPE_MAIN_VARIANT (member_scope)))
2343 tree binfo;
2344 base_kind kind;
2346 binfo = lookup_base (access_path ? access_path : object_type,
2347 member_scope, ba_unique, &kind, complain);
2348 if (binfo == error_mark_node)
2349 return error_mark_node;
2351 /* It is invalid to try to get to a virtual base of a
2352 NULL object. The most common cause is invalid use of
2353 offsetof macro. */
2354 if (null_object_p && kind == bk_via_virtual)
2356 if (complain & tf_error)
2358 error ("invalid access to non-static data member %qD of "
2359 "NULL object",
2360 member);
2361 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2363 return error_mark_node;
2366 /* Convert to the base. */
2367 object = build_base_path (PLUS_EXPR, object, binfo,
2368 /*nonnull=*/1, complain);
2369 /* If we found the base successfully then we should be able
2370 to convert to it successfully. */
2371 gcc_assert (object != error_mark_node);
2374 /* Complain about other invalid uses of offsetof, even though they will
2375 give the right answer. Note that we complain whether or not they
2376 actually used the offsetof macro, since there's no way to know at this
2377 point. So we just give a warning, instead of a pedwarn. */
2378 /* Do not produce this warning for base class field references, because
2379 we know for a fact that didn't come from offsetof. This does occur
2380 in various testsuite cases where a null object is passed where a
2381 vtable access is required. */
2382 if (null_object_p && warn_invalid_offsetof
2383 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2384 && !DECL_FIELD_IS_BASE (member)
2385 && cp_unevaluated_operand == 0
2386 && (complain & tf_warning))
2388 warning (OPT_Winvalid_offsetof,
2389 "invalid access to non-static data member %qD "
2390 " of NULL object", member);
2391 warning (OPT_Winvalid_offsetof,
2392 "(perhaps the %<offsetof%> macro was used incorrectly)");
2395 /* If MEMBER is from an anonymous aggregate, we have converted
2396 OBJECT so that it refers to the class containing the
2397 anonymous union. Generate a reference to the anonymous union
2398 itself, and recur to find MEMBER. */
2399 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2400 /* When this code is called from build_field_call, the
2401 object already has the type of the anonymous union.
2402 That is because the COMPONENT_REF was already
2403 constructed, and was then disassembled before calling
2404 build_field_call. After the function-call code is
2405 cleaned up, this waste can be eliminated. */
2406 && (!same_type_ignoring_top_level_qualifiers_p
2407 (TREE_TYPE (object), DECL_CONTEXT (member))))
2409 tree anonymous_union;
2411 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2412 DECL_CONTEXT (member));
2413 object = build_class_member_access_expr (object,
2414 anonymous_union,
2415 /*access_path=*/NULL_TREE,
2416 preserve_reference,
2417 complain);
2420 /* Compute the type of the field, as described in [expr.ref]. */
2421 type_quals = TYPE_UNQUALIFIED;
2422 member_type = TREE_TYPE (member);
2423 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2425 type_quals = (cp_type_quals (member_type)
2426 | cp_type_quals (object_type));
2428 /* A field is const (volatile) if the enclosing object, or the
2429 field itself, is const (volatile). But, a mutable field is
2430 not const, even within a const object. */
2431 if (DECL_MUTABLE_P (member))
2432 type_quals &= ~TYPE_QUAL_CONST;
2433 member_type = cp_build_qualified_type (member_type, type_quals);
2436 result = build3 (COMPONENT_REF, member_type, object, member,
2437 NULL_TREE);
2438 result = fold_if_not_in_template (result);
2440 /* Mark the expression const or volatile, as appropriate. Even
2441 though we've dealt with the type above, we still have to mark the
2442 expression itself. */
2443 if (type_quals & TYPE_QUAL_CONST)
2444 TREE_READONLY (result) = 1;
2445 if (type_quals & TYPE_QUAL_VOLATILE)
2446 TREE_THIS_VOLATILE (result) = 1;
2448 else if (BASELINK_P (member))
2450 /* The member is a (possibly overloaded) member function. */
2451 tree functions;
2452 tree type;
2454 /* If the MEMBER is exactly one static member function, then we
2455 know the type of the expression. Otherwise, we must wait
2456 until overload resolution has been performed. */
2457 functions = BASELINK_FUNCTIONS (member);
2458 if (TREE_CODE (functions) == FUNCTION_DECL
2459 && DECL_STATIC_FUNCTION_P (functions))
2460 type = TREE_TYPE (functions);
2461 else
2462 type = unknown_type_node;
2463 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2464 base. That will happen when the function is called. */
2465 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2467 else if (TREE_CODE (member) == CONST_DECL)
2469 /* The member is an enumerator. */
2470 result = member;
2471 /* If OBJECT has side-effects, they are supposed to occur. */
2472 if (TREE_SIDE_EFFECTS (object))
2473 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2474 object, result);
2476 else if ((using_decl = strip_using_decl (member)) != member)
2477 result = build_class_member_access_expr (object,
2478 using_decl,
2479 access_path, preserve_reference,
2480 complain);
2481 else
2483 if (complain & tf_error)
2484 error ("invalid use of %qD", member);
2485 return error_mark_node;
2488 if (!preserve_reference)
2489 /* [expr.ref]
2491 If E2 is declared to have type "reference to T", then ... the
2492 type of E1.E2 is T. */
2493 result = convert_from_reference (result);
2495 return result;
2498 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2499 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2501 static tree
2502 lookup_destructor (tree object, tree scope, tree dtor_name,
2503 tsubst_flags_t complain)
2505 tree object_type = TREE_TYPE (object);
2506 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2507 tree expr;
2509 /* We've already complained about this destructor. */
2510 if (dtor_type == error_mark_node)
2511 return error_mark_node;
2513 if (scope && !check_dtor_name (scope, dtor_type))
2515 if (complain & tf_error)
2516 error ("qualified type %qT does not match destructor name ~%qT",
2517 scope, dtor_type);
2518 return error_mark_node;
2520 if (is_auto (dtor_type))
2521 dtor_type = object_type;
2522 else if (identifier_p (dtor_type))
2524 /* In a template, names we can't find a match for are still accepted
2525 destructor names, and we check them here. */
2526 if (check_dtor_name (object_type, dtor_type))
2527 dtor_type = object_type;
2528 else
2530 if (complain & tf_error)
2531 error ("object type %qT does not match destructor name ~%qT",
2532 object_type, dtor_type);
2533 return error_mark_node;
2537 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2539 if (complain & tf_error)
2540 error ("the type being destroyed is %qT, but the destructor "
2541 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2542 return error_mark_node;
2544 expr = lookup_member (dtor_type, complete_dtor_identifier,
2545 /*protect=*/1, /*want_type=*/false,
2546 tf_warning_or_error);
2547 expr = (adjust_result_of_qualified_name_lookup
2548 (expr, dtor_type, object_type));
2549 if (scope == NULL_TREE)
2550 /* We need to call adjust_result_of_qualified_name_lookup in case the
2551 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2552 that we still get virtual function binding. */
2553 BASELINK_QUALIFIED_P (expr) = false;
2554 return expr;
2557 /* An expression of the form "A::template B" has been resolved to
2558 DECL. Issue a diagnostic if B is not a template or template
2559 specialization. */
2561 void
2562 check_template_keyword (tree decl)
2564 /* The standard says:
2566 [temp.names]
2568 If a name prefixed by the keyword template is not a member
2569 template, the program is ill-formed.
2571 DR 228 removed the restriction that the template be a member
2572 template.
2574 DR 96, if accepted would add the further restriction that explicit
2575 template arguments must be provided if the template keyword is
2576 used, but, as of 2005-10-16, that DR is still in "drafting". If
2577 this DR is accepted, then the semantic checks here can be
2578 simplified, as the entity named must in fact be a template
2579 specialization, rather than, as at present, a set of overloaded
2580 functions containing at least one template function. */
2581 if (TREE_CODE (decl) != TEMPLATE_DECL
2582 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2584 if (!is_overloaded_fn (decl))
2585 permerror (input_location, "%qD is not a template", decl);
2586 else
2588 tree fns;
2589 fns = decl;
2590 if (BASELINK_P (fns))
2591 fns = BASELINK_FUNCTIONS (fns);
2592 while (fns)
2594 tree fn = OVL_CURRENT (fns);
2595 if (TREE_CODE (fn) == TEMPLATE_DECL
2596 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2597 break;
2598 if (TREE_CODE (fn) == FUNCTION_DECL
2599 && DECL_USE_TEMPLATE (fn)
2600 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2601 break;
2602 fns = OVL_NEXT (fns);
2604 if (!fns)
2605 permerror (input_location, "%qD is not a template", decl);
2610 /* This function is called by the parser to process a class member
2611 access expression of the form OBJECT.NAME. NAME is a node used by
2612 the parser to represent a name; it is not yet a DECL. It may,
2613 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2614 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2615 there is no reason to do the lookup twice, so the parser keeps the
2616 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2617 be a template via the use of the "A::template B" syntax. */
2619 tree
2620 finish_class_member_access_expr (tree object, tree name, bool template_p,
2621 tsubst_flags_t complain)
2623 tree expr;
2624 tree object_type;
2625 tree member;
2626 tree access_path = NULL_TREE;
2627 tree orig_object = object;
2628 tree orig_name = name;
2630 if (object == error_mark_node || name == error_mark_node)
2631 return error_mark_node;
2633 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2634 if (!objc_is_public (object, name))
2635 return error_mark_node;
2637 object_type = TREE_TYPE (object);
2639 if (processing_template_decl)
2641 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2642 dependent_type_p (object_type)
2643 /* If NAME is just an IDENTIFIER_NODE, then the expression
2644 is dependent. */
2645 || identifier_p (object)
2646 /* If NAME is "f<args>", where either 'f' or 'args' is
2647 dependent, then the expression is dependent. */
2648 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2649 && dependent_template_id_p (TREE_OPERAND (name, 0),
2650 TREE_OPERAND (name, 1)))
2651 /* If NAME is "T::X" where "T" is dependent, then the
2652 expression is dependent. */
2653 || (TREE_CODE (name) == SCOPE_REF
2654 && TYPE_P (TREE_OPERAND (name, 0))
2655 && dependent_type_p (TREE_OPERAND (name, 0))))
2656 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2657 object, name, NULL_TREE);
2658 object = build_non_dependent_expr (object);
2660 else if (c_dialect_objc ()
2661 && identifier_p (name)
2662 && (expr = objc_maybe_build_component_ref (object, name)))
2663 return expr;
2665 /* [expr.ref]
2667 The type of the first expression shall be "class object" (of a
2668 complete type). */
2669 if (!currently_open_class (object_type)
2670 && !complete_type_or_maybe_complain (object_type, object, complain))
2671 return error_mark_node;
2672 if (!CLASS_TYPE_P (object_type))
2674 if (complain & tf_error)
2676 if (POINTER_TYPE_P (object_type)
2677 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2678 error ("request for member %qD in %qE, which is of pointer "
2679 "type %qT (maybe you meant to use %<->%> ?)",
2680 name, object, object_type);
2681 else
2682 error ("request for member %qD in %qE, which is of non-class "
2683 "type %qT", name, object, object_type);
2685 return error_mark_node;
2688 if (BASELINK_P (name))
2689 /* A member function that has already been looked up. */
2690 member = name;
2691 else
2693 bool is_template_id = false;
2694 tree template_args = NULL_TREE;
2695 tree scope;
2697 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2699 is_template_id = true;
2700 template_args = TREE_OPERAND (name, 1);
2701 name = TREE_OPERAND (name, 0);
2703 if (TREE_CODE (name) == OVERLOAD)
2704 name = DECL_NAME (get_first_fn (name));
2705 else if (DECL_P (name))
2706 name = DECL_NAME (name);
2709 if (TREE_CODE (name) == SCOPE_REF)
2711 /* A qualified name. The qualifying class or namespace `S'
2712 has already been looked up; it is either a TYPE or a
2713 NAMESPACE_DECL. */
2714 scope = TREE_OPERAND (name, 0);
2715 name = TREE_OPERAND (name, 1);
2717 /* If SCOPE is a namespace, then the qualified name does not
2718 name a member of OBJECT_TYPE. */
2719 if (TREE_CODE (scope) == NAMESPACE_DECL)
2721 if (complain & tf_error)
2722 error ("%<%D::%D%> is not a member of %qT",
2723 scope, name, object_type);
2724 return error_mark_node;
2727 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2729 /* Looking up a member enumerator (c++/56793). */
2730 if (!TYPE_CLASS_SCOPE_P (scope)
2731 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2733 if (complain & tf_error)
2734 error ("%<%D::%D%> is not a member of %qT",
2735 scope, name, object_type);
2736 return error_mark_node;
2738 tree val = lookup_enumerator (scope, name);
2739 if (TREE_SIDE_EFFECTS (object))
2740 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2741 return val;
2744 gcc_assert (CLASS_TYPE_P (scope));
2745 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2747 if (constructor_name_p (name, scope))
2749 if (complain & tf_error)
2750 error ("cannot call constructor %<%T::%D%> directly",
2751 scope, name);
2752 return error_mark_node;
2755 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2756 access_path = lookup_base (object_type, scope, ba_check,
2757 NULL, complain);
2758 if (access_path == error_mark_node)
2759 return error_mark_node;
2760 if (!access_path)
2762 if (complain & tf_error)
2763 error ("%qT is not a base of %qT", scope, object_type);
2764 return error_mark_node;
2767 else
2769 scope = NULL_TREE;
2770 access_path = object_type;
2773 if (TREE_CODE (name) == BIT_NOT_EXPR)
2774 member = lookup_destructor (object, scope, name, complain);
2775 else
2777 /* Look up the member. */
2778 member = lookup_member (access_path, name, /*protect=*/1,
2779 /*want_type=*/false, complain);
2780 if (member == NULL_TREE)
2782 if (complain & tf_error)
2783 error ("%qD has no member named %qE",
2784 TREE_CODE (access_path) == TREE_BINFO
2785 ? TREE_TYPE (access_path) : object_type, name);
2786 return error_mark_node;
2788 if (member == error_mark_node)
2789 return error_mark_node;
2792 if (is_template_id)
2794 tree templ = member;
2796 if (BASELINK_P (templ))
2797 templ = lookup_template_function (templ, template_args);
2798 else
2800 if (complain & tf_error)
2801 error ("%qD is not a member template function", name);
2802 return error_mark_node;
2807 if (TREE_DEPRECATED (member))
2808 warn_deprecated_use (member, NULL_TREE);
2810 if (template_p)
2811 check_template_keyword (member);
2813 expr = build_class_member_access_expr (object, member, access_path,
2814 /*preserve_reference=*/false,
2815 complain);
2816 if (processing_template_decl && expr != error_mark_node)
2818 if (BASELINK_P (member))
2820 if (TREE_CODE (orig_name) == SCOPE_REF)
2821 BASELINK_QUALIFIED_P (member) = 1;
2822 orig_name = member;
2824 return build_min_non_dep (COMPONENT_REF, expr,
2825 orig_object, orig_name,
2826 NULL_TREE);
2829 return expr;
2832 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2833 type. */
2835 tree
2836 build_simple_component_ref (tree object, tree member)
2838 tree type = cp_build_qualified_type (TREE_TYPE (member),
2839 cp_type_quals (TREE_TYPE (object)));
2840 return fold_build3_loc (input_location,
2841 COMPONENT_REF, type,
2842 object, member, NULL_TREE);
2845 /* Return an expression for the MEMBER_NAME field in the internal
2846 representation of PTRMEM, a pointer-to-member function. (Each
2847 pointer-to-member function type gets its own RECORD_TYPE so it is
2848 more convenient to access the fields by name than by FIELD_DECL.)
2849 This routine converts the NAME to a FIELD_DECL and then creates the
2850 node for the complete expression. */
2852 tree
2853 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2855 tree ptrmem_type;
2856 tree member;
2858 /* This code is a stripped down version of
2859 build_class_member_access_expr. It does not work to use that
2860 routine directly because it expects the object to be of class
2861 type. */
2862 ptrmem_type = TREE_TYPE (ptrmem);
2863 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2864 for (member = TYPE_FIELDS (ptrmem_type); member;
2865 member = DECL_CHAIN (member))
2866 if (DECL_NAME (member) == member_name)
2867 break;
2868 return build_simple_component_ref (ptrmem, member);
2871 /* Given an expression PTR for a pointer, return an expression
2872 for the value pointed to.
2873 ERRORSTRING is the name of the operator to appear in error messages.
2875 This function may need to overload OPERATOR_FNNAME.
2876 Must also handle REFERENCE_TYPEs for C++. */
2878 tree
2879 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2880 tsubst_flags_t complain)
2882 tree orig_expr = expr;
2883 tree rval;
2885 if (processing_template_decl)
2887 /* Retain the type if we know the operand is a pointer. */
2888 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2889 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2890 if (type_dependent_expression_p (expr))
2891 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2892 expr = build_non_dependent_expr (expr);
2895 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2896 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2897 if (!rval)
2898 rval = cp_build_indirect_ref (expr, errorstring, complain);
2900 if (processing_template_decl && rval != error_mark_node)
2901 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2902 else
2903 return rval;
2906 /* Helper function called from c-common. */
2907 tree
2908 build_indirect_ref (location_t /*loc*/,
2909 tree ptr, ref_operator errorstring)
2911 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2914 tree
2915 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2916 tsubst_flags_t complain)
2918 tree pointer, type;
2920 if (ptr == current_class_ptr
2921 || (TREE_CODE (ptr) == NOP_EXPR
2922 && TREE_OPERAND (ptr, 0) == current_class_ptr
2923 && (same_type_ignoring_top_level_qualifiers_p
2924 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2925 return current_class_ref;
2927 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2928 ? ptr : decay_conversion (ptr, complain));
2929 if (pointer == error_mark_node)
2930 return error_mark_node;
2932 type = TREE_TYPE (pointer);
2934 if (POINTER_TYPE_P (type))
2936 /* [expr.unary.op]
2938 If the type of the expression is "pointer to T," the type
2939 of the result is "T." */
2940 tree t = TREE_TYPE (type);
2942 if ((CONVERT_EXPR_P (ptr)
2943 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2944 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2946 /* If a warning is issued, mark it to avoid duplicates from
2947 the backend. This only needs to be done at
2948 warn_strict_aliasing > 2. */
2949 if (warn_strict_aliasing > 2)
2950 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2951 type, TREE_OPERAND (ptr, 0)))
2952 TREE_NO_WARNING (ptr) = 1;
2955 if (VOID_TYPE_P (t))
2957 /* A pointer to incomplete type (other than cv void) can be
2958 dereferenced [expr.unary.op]/1 */
2959 if (complain & tf_error)
2960 error ("%qT is not a pointer-to-object type", type);
2961 return error_mark_node;
2963 else if (TREE_CODE (pointer) == ADDR_EXPR
2964 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2965 /* The POINTER was something like `&x'. We simplify `*&x' to
2966 `x'. */
2967 return TREE_OPERAND (pointer, 0);
2968 else
2970 tree ref = build1 (INDIRECT_REF, t, pointer);
2972 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2973 so that we get the proper error message if the result is used
2974 to assign to. Also, &* is supposed to be a no-op. */
2975 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2976 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2977 TREE_SIDE_EFFECTS (ref)
2978 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2979 return ref;
2982 else if (!(complain & tf_error))
2983 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2985 /* `pointer' won't be an error_mark_node if we were given a
2986 pointer to member, so it's cool to check for this here. */
2987 else if (TYPE_PTRMEM_P (type))
2988 switch (errorstring)
2990 case RO_ARRAY_INDEXING:
2991 error ("invalid use of array indexing on pointer to member");
2992 break;
2993 case RO_UNARY_STAR:
2994 error ("invalid use of unary %<*%> on pointer to member");
2995 break;
2996 case RO_IMPLICIT_CONVERSION:
2997 error ("invalid use of implicit conversion on pointer to member");
2998 break;
2999 case RO_ARROW_STAR:
3000 error ("left hand operand of %<->*%> must be a pointer to class, "
3001 "but is a pointer to member of type %qT", type);
3002 break;
3003 default:
3004 gcc_unreachable ();
3006 else if (pointer != error_mark_node)
3007 invalid_indirection_error (input_location, type, errorstring);
3009 return error_mark_node;
3012 /* This handles expressions of the form "a[i]", which denotes
3013 an array reference.
3015 This is logically equivalent in C to *(a+i), but we may do it differently.
3016 If A is a variable or a member, we generate a primitive ARRAY_REF.
3017 This avoids forcing the array out of registers, and can work on
3018 arrays that are not lvalues (for example, members of structures returned
3019 by functions).
3021 If INDEX is of some user-defined type, it must be converted to
3022 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3023 will inherit the type of the array, which will be some pointer type.
3025 LOC is the location to use in building the array reference. */
3027 tree
3028 cp_build_array_ref (location_t loc, tree array, tree idx,
3029 tsubst_flags_t complain)
3031 tree ret;
3033 if (idx == 0)
3035 if (complain & tf_error)
3036 error_at (loc, "subscript missing in array reference");
3037 return error_mark_node;
3040 /* If an array's index is an array notation, then its rank cannot be
3041 greater than one. */
3042 if (flag_cilkplus && contains_array_notation_expr (idx))
3044 size_t rank = 0;
3046 /* If find_rank returns false, then it should have reported an error,
3047 thus it is unnecessary for repetition. */
3048 if (!find_rank (loc, idx, idx, true, &rank))
3049 return error_mark_node;
3050 if (rank > 1)
3052 error_at (loc, "rank of the array%'s index is greater than 1");
3053 return error_mark_node;
3056 if (TREE_TYPE (array) == error_mark_node
3057 || TREE_TYPE (idx) == error_mark_node)
3058 return error_mark_node;
3060 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3061 inside it. */
3062 switch (TREE_CODE (array))
3064 case COMPOUND_EXPR:
3066 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3067 complain);
3068 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3069 TREE_OPERAND (array, 0), value);
3070 SET_EXPR_LOCATION (ret, loc);
3071 return ret;
3074 case COND_EXPR:
3075 ret = build_conditional_expr
3076 (loc, TREE_OPERAND (array, 0),
3077 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3078 complain),
3079 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3080 complain),
3081 complain);
3082 protected_set_expr_location (ret, loc);
3083 return ret;
3085 default:
3086 break;
3089 convert_vector_to_pointer_for_subscript (loc, &array, idx);
3091 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3093 tree rval, type;
3095 warn_array_subscript_with_type_char (idx);
3097 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3099 if (complain & tf_error)
3100 error_at (loc, "array subscript is not an integer");
3101 return error_mark_node;
3104 /* Apply integral promotions *after* noticing character types.
3105 (It is unclear why we do these promotions -- the standard
3106 does not say that we should. In fact, the natural thing would
3107 seem to be to convert IDX to ptrdiff_t; we're performing
3108 pointer arithmetic.) */
3109 idx = cp_perform_integral_promotions (idx, complain);
3111 /* An array that is indexed by a non-constant
3112 cannot be stored in a register; we must be able to do
3113 address arithmetic on its address.
3114 Likewise an array of elements of variable size. */
3115 if (TREE_CODE (idx) != INTEGER_CST
3116 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3117 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3118 != INTEGER_CST)))
3120 if (!cxx_mark_addressable (array))
3121 return error_mark_node;
3124 /* An array that is indexed by a constant value which is not within
3125 the array bounds cannot be stored in a register either; because we
3126 would get a crash in store_bit_field/extract_bit_field when trying
3127 to access a non-existent part of the register. */
3128 if (TREE_CODE (idx) == INTEGER_CST
3129 && TYPE_DOMAIN (TREE_TYPE (array))
3130 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3132 if (!cxx_mark_addressable (array))
3133 return error_mark_node;
3136 if (!lvalue_p (array) && (complain & tf_error))
3137 pedwarn (loc, OPT_Wpedantic,
3138 "ISO C++ forbids subscripting non-lvalue array");
3140 /* Note in C++ it is valid to subscript a `register' array, since
3141 it is valid to take the address of something with that
3142 storage specification. */
3143 if (extra_warnings)
3145 tree foo = array;
3146 while (TREE_CODE (foo) == COMPONENT_REF)
3147 foo = TREE_OPERAND (foo, 0);
3148 if (VAR_P (foo) && DECL_REGISTER (foo)
3149 && (complain & tf_warning))
3150 warning_at (loc, OPT_Wextra,
3151 "subscripting array declared %<register%>");
3154 type = TREE_TYPE (TREE_TYPE (array));
3155 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3156 /* Array ref is const/volatile if the array elements are
3157 or if the array is.. */
3158 TREE_READONLY (rval)
3159 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3160 TREE_SIDE_EFFECTS (rval)
3161 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3162 TREE_THIS_VOLATILE (rval)
3163 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3164 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3165 complain);
3166 protected_set_expr_location (ret, loc);
3167 return ret;
3171 tree ar = cp_default_conversion (array, complain);
3172 tree ind = cp_default_conversion (idx, complain);
3174 /* Put the integer in IND to simplify error checking. */
3175 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3177 tree temp = ar;
3178 ar = ind;
3179 ind = temp;
3182 if (ar == error_mark_node || ind == error_mark_node)
3183 return error_mark_node;
3185 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3187 if (complain & tf_error)
3188 error_at (loc, "subscripted value is neither array nor pointer");
3189 return error_mark_node;
3191 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3193 if (complain & tf_error)
3194 error_at (loc, "array subscript is not an integer");
3195 return error_mark_node;
3198 warn_array_subscript_with_type_char (idx);
3200 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3201 PLUS_EXPR, ar, ind,
3202 complain),
3203 RO_ARRAY_INDEXING,
3204 complain);
3205 protected_set_expr_location (ret, loc);
3206 return ret;
3210 /* Entry point for Obj-C++. */
3212 tree
3213 build_array_ref (location_t loc, tree array, tree idx)
3215 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3218 /* Resolve a pointer to member function. INSTANCE is the object
3219 instance to use, if the member points to a virtual member.
3221 This used to avoid checking for virtual functions if basetype
3222 has no virtual functions, according to an earlier ANSI draft.
3223 With the final ISO C++ rules, such an optimization is
3224 incorrect: A pointer to a derived member can be static_cast
3225 to pointer-to-base-member, as long as the dynamic object
3226 later has the right member. So now we only do this optimization
3227 when we know the dynamic type of the object. */
3229 tree
3230 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3231 tsubst_flags_t complain)
3233 if (TREE_CODE (function) == OFFSET_REF)
3234 function = TREE_OPERAND (function, 1);
3236 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3238 tree idx, delta, e1, e2, e3, vtbl;
3239 bool nonvirtual;
3240 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3241 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3243 tree instance_ptr = *instance_ptrptr;
3244 tree instance_save_expr = 0;
3245 if (instance_ptr == error_mark_node)
3247 if (TREE_CODE (function) == PTRMEM_CST)
3249 /* Extracting the function address from a pmf is only
3250 allowed with -Wno-pmf-conversions. It only works for
3251 pmf constants. */
3252 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3253 e1 = convert (fntype, e1);
3254 return e1;
3256 else
3258 if (complain & tf_error)
3259 error ("object missing in use of %qE", function);
3260 return error_mark_node;
3264 /* True if we know that the dynamic type of the object doesn't have
3265 virtual functions, so we can assume the PFN field is a pointer. */
3266 nonvirtual = (COMPLETE_TYPE_P (basetype)
3267 && !TYPE_POLYMORPHIC_P (basetype)
3268 && resolves_to_fixed_type_p (instance_ptr, 0));
3270 /* If we don't really have an object (i.e. in an ill-formed
3271 conversion from PMF to pointer), we can't resolve virtual
3272 functions anyway. */
3273 if (!nonvirtual && is_dummy_object (instance_ptr))
3274 nonvirtual = true;
3276 if (TREE_SIDE_EFFECTS (instance_ptr))
3277 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3279 if (TREE_SIDE_EFFECTS (function))
3280 function = save_expr (function);
3282 /* Start by extracting all the information from the PMF itself. */
3283 e3 = pfn_from_ptrmemfunc (function);
3284 delta = delta_from_ptrmemfunc (function);
3285 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3286 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3288 case ptrmemfunc_vbit_in_pfn:
3289 e1 = cp_build_binary_op (input_location,
3290 BIT_AND_EXPR, idx, integer_one_node,
3291 complain);
3292 idx = cp_build_binary_op (input_location,
3293 MINUS_EXPR, idx, integer_one_node,
3294 complain);
3295 if (idx == error_mark_node)
3296 return error_mark_node;
3297 break;
3299 case ptrmemfunc_vbit_in_delta:
3300 e1 = cp_build_binary_op (input_location,
3301 BIT_AND_EXPR, delta, integer_one_node,
3302 complain);
3303 delta = cp_build_binary_op (input_location,
3304 RSHIFT_EXPR, delta, integer_one_node,
3305 complain);
3306 if (delta == error_mark_node)
3307 return error_mark_node;
3308 break;
3310 default:
3311 gcc_unreachable ();
3314 if (e1 == error_mark_node)
3315 return error_mark_node;
3317 /* Convert down to the right base before using the instance. A
3318 special case is that in a pointer to member of class C, C may
3319 be incomplete. In that case, the function will of course be
3320 a member of C, and no conversion is required. In fact,
3321 lookup_base will fail in that case, because incomplete
3322 classes do not have BINFOs. */
3323 if (!same_type_ignoring_top_level_qualifiers_p
3324 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3326 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3327 basetype, ba_check, NULL, complain);
3328 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3329 1, complain);
3330 if (instance_ptr == error_mark_node)
3331 return error_mark_node;
3333 /* ...and then the delta in the PMF. */
3334 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3336 /* Hand back the adjusted 'this' argument to our caller. */
3337 *instance_ptrptr = instance_ptr;
3339 if (nonvirtual)
3340 /* Now just return the pointer. */
3341 return e3;
3343 /* Next extract the vtable pointer from the object. */
3344 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3345 instance_ptr);
3346 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3347 if (vtbl == error_mark_node)
3348 return error_mark_node;
3350 /* Finally, extract the function pointer from the vtable. */
3351 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3352 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3353 if (e2 == error_mark_node)
3354 return error_mark_node;
3355 TREE_CONSTANT (e2) = 1;
3357 /* When using function descriptors, the address of the
3358 vtable entry is treated as a function pointer. */
3359 if (TARGET_VTABLE_USES_DESCRIPTORS)
3360 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3361 cp_build_addr_expr (e2, complain));
3363 e2 = fold_convert (TREE_TYPE (e3), e2);
3364 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3365 if (e1 == error_mark_node)
3366 return error_mark_node;
3368 /* Make sure this doesn't get evaluated first inside one of the
3369 branches of the COND_EXPR. */
3370 if (instance_save_expr)
3371 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3372 instance_save_expr, e1);
3374 function = e1;
3376 return function;
3379 /* Used by the C-common bits. */
3380 tree
3381 build_function_call (location_t /*loc*/,
3382 tree function, tree params)
3384 return cp_build_function_call (function, params, tf_warning_or_error);
3387 /* Used by the C-common bits. */
3388 tree
3389 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3390 tree function, vec<tree, va_gc> *params,
3391 vec<tree, va_gc> * /*origtypes*/)
3393 vec<tree, va_gc> *orig_params = params;
3394 tree ret = cp_build_function_call_vec (function, &params,
3395 tf_warning_or_error);
3397 /* cp_build_function_call_vec can reallocate PARAMS by adding
3398 default arguments. That should never happen here. Verify
3399 that. */
3400 gcc_assert (params == orig_params);
3402 return ret;
3405 /* Build a function call using a tree list of arguments. */
3407 tree
3408 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3410 vec<tree, va_gc> *vec;
3411 tree ret;
3413 vec = make_tree_vector ();
3414 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3415 vec_safe_push (vec, TREE_VALUE (params));
3416 ret = cp_build_function_call_vec (function, &vec, complain);
3417 release_tree_vector (vec);
3418 return ret;
3421 /* Build a function call using varargs. */
3423 tree
3424 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3426 vec<tree, va_gc> *vec;
3427 va_list args;
3428 tree ret, t;
3430 vec = make_tree_vector ();
3431 va_start (args, complain);
3432 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3433 vec_safe_push (vec, t);
3434 va_end (args);
3435 ret = cp_build_function_call_vec (function, &vec, complain);
3436 release_tree_vector (vec);
3437 return ret;
3440 /* Build a function call using a vector of arguments. PARAMS may be
3441 NULL if there are no parameters. This changes the contents of
3442 PARAMS. */
3444 tree
3445 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3446 tsubst_flags_t complain)
3448 tree fntype, fndecl;
3449 int is_method;
3450 tree original = function;
3451 int nargs;
3452 tree *argarray;
3453 tree parm_types;
3454 vec<tree, va_gc> *allocated = NULL;
3455 tree ret;
3457 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3458 expressions, like those used for ObjC messenger dispatches. */
3459 if (params != NULL && !vec_safe_is_empty (*params))
3460 function = objc_rewrite_function_call (function, (**params)[0]);
3462 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3463 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3464 if (TREE_CODE (function) == NOP_EXPR
3465 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3466 function = TREE_OPERAND (function, 0);
3468 if (TREE_CODE (function) == FUNCTION_DECL)
3470 mark_used (function);
3471 fndecl = function;
3473 /* Convert anything with function type to a pointer-to-function. */
3474 if (DECL_MAIN_P (function) && (complain & tf_error))
3475 pedwarn (input_location, OPT_Wpedantic,
3476 "ISO C++ forbids calling %<::main%> from within program");
3478 function = build_addr_func (function, complain);
3480 else
3482 fndecl = NULL_TREE;
3484 function = build_addr_func (function, complain);
3487 if (function == error_mark_node)
3488 return error_mark_node;
3490 fntype = TREE_TYPE (function);
3492 if (TYPE_PTRMEMFUNC_P (fntype))
3494 if (complain & tf_error)
3495 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3496 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3497 original, original);
3498 return error_mark_node;
3501 is_method = (TYPE_PTR_P (fntype)
3502 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3504 if (!(TYPE_PTRFN_P (fntype)
3505 || is_method
3506 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3508 if (complain & tf_error)
3510 if (!flag_diagnostics_show_caret)
3511 error_at (input_location,
3512 "%qE cannot be used as a function", original);
3513 else if (DECL_P (original))
3514 error_at (input_location,
3515 "%qD cannot be used as a function", original);
3516 else
3517 error_at (input_location,
3518 "expression cannot be used as a function");
3521 return error_mark_node;
3524 /* fntype now gets the type of function pointed to. */
3525 fntype = TREE_TYPE (fntype);
3526 parm_types = TYPE_ARG_TYPES (fntype);
3528 if (params == NULL)
3530 allocated = make_tree_vector ();
3531 params = &allocated;
3534 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3535 complain);
3536 if (nargs < 0)
3537 return error_mark_node;
3539 argarray = (*params)->address ();
3541 /* Check for errors in format strings and inappropriately
3542 null parameters. */
3543 check_function_arguments (fntype, nargs, argarray);
3545 ret = build_cxx_call (function, nargs, argarray, complain);
3547 if (allocated != NULL)
3548 release_tree_vector (allocated);
3550 return ret;
3553 /* Subroutine of convert_arguments.
3554 Warn about wrong number of args are genereted. */
3556 static void
3557 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3559 if (fndecl)
3561 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3563 if (DECL_NAME (fndecl) == NULL_TREE
3564 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3565 error_at (loc,
3566 too_many_p
3567 ? G_("too many arguments to constructor %q#D")
3568 : G_("too few arguments to constructor %q#D"),
3569 fndecl);
3570 else
3571 error_at (loc,
3572 too_many_p
3573 ? G_("too many arguments to member function %q#D")
3574 : G_("too few arguments to member function %q#D"),
3575 fndecl);
3577 else
3578 error_at (loc,
3579 too_many_p
3580 ? G_("too many arguments to function %q#D")
3581 : G_("too few arguments to function %q#D"),
3582 fndecl);
3583 inform (DECL_SOURCE_LOCATION (fndecl),
3584 "declared here");
3586 else
3588 if (c_dialect_objc () && objc_message_selector ())
3589 error_at (loc,
3590 too_many_p
3591 ? G_("too many arguments to method %q#D")
3592 : G_("too few arguments to method %q#D"),
3593 objc_message_selector ());
3594 else
3595 error_at (loc, too_many_p ? G_("too many arguments to function")
3596 : G_("too few arguments to function"));
3600 /* Convert the actual parameter expressions in the list VALUES to the
3601 types in the list TYPELIST. The converted expressions are stored
3602 back in the VALUES vector.
3603 If parmdecls is exhausted, or when an element has NULL as its type,
3604 perform the default conversions.
3606 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3608 This is also where warnings about wrong number of args are generated.
3610 Returns the actual number of arguments processed (which might be less
3611 than the length of the vector), or -1 on error.
3613 In C++, unspecified trailing parameters can be filled in with their
3614 default arguments, if such were specified. Do so here. */
3616 static int
3617 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3618 int flags, tsubst_flags_t complain)
3620 tree typetail;
3621 unsigned int i;
3623 /* Argument passing is always copy-initialization. */
3624 flags |= LOOKUP_ONLYCONVERTING;
3626 for (i = 0, typetail = typelist;
3627 i < vec_safe_length (*values);
3628 i++)
3630 tree type = typetail ? TREE_VALUE (typetail) : 0;
3631 tree val = (**values)[i];
3633 if (val == error_mark_node || type == error_mark_node)
3634 return -1;
3636 if (type == void_type_node)
3638 if (complain & tf_error)
3640 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3641 return i;
3643 else
3644 return -1;
3647 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3648 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3649 if (TREE_CODE (val) == NOP_EXPR
3650 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3651 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3652 val = TREE_OPERAND (val, 0);
3654 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3656 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3657 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3658 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3659 val = decay_conversion (val, complain);
3662 if (val == error_mark_node)
3663 return -1;
3665 if (type != 0)
3667 /* Formal parm type is specified by a function prototype. */
3668 tree parmval;
3670 if (!COMPLETE_TYPE_P (complete_type (type)))
3672 if (complain & tf_error)
3674 if (fndecl)
3675 error ("parameter %P of %qD has incomplete type %qT",
3676 i, fndecl, type);
3677 else
3678 error ("parameter %P has incomplete type %qT", i, type);
3680 parmval = error_mark_node;
3682 else
3684 parmval = convert_for_initialization
3685 (NULL_TREE, type, val, flags,
3686 ICR_ARGPASS, fndecl, i, complain);
3687 parmval = convert_for_arg_passing (type, parmval, complain);
3690 if (parmval == error_mark_node)
3691 return -1;
3693 (**values)[i] = parmval;
3695 else
3697 if (fndecl && magic_varargs_p (fndecl))
3698 /* Don't do ellipsis conversion for __built_in_constant_p
3699 as this will result in spurious errors for non-trivial
3700 types. */
3701 val = require_complete_type_sfinae (val, complain);
3702 else
3703 val = convert_arg_to_ellipsis (val, complain);
3705 (**values)[i] = val;
3708 if (typetail)
3709 typetail = TREE_CHAIN (typetail);
3712 if (typetail != 0 && typetail != void_list_node)
3714 /* See if there are default arguments that can be used. Because
3715 we hold default arguments in the FUNCTION_TYPE (which is so
3716 wrong), we can see default parameters here from deduced
3717 contexts (and via typeof) for indirect function calls.
3718 Fortunately we know whether we have a function decl to
3719 provide default arguments in a language conformant
3720 manner. */
3721 if (fndecl && TREE_PURPOSE (typetail)
3722 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3724 for (; typetail != void_list_node; ++i)
3726 tree parmval
3727 = convert_default_arg (TREE_VALUE (typetail),
3728 TREE_PURPOSE (typetail),
3729 fndecl, i, complain);
3731 if (parmval == error_mark_node)
3732 return -1;
3734 vec_safe_push (*values, parmval);
3735 typetail = TREE_CHAIN (typetail);
3736 /* ends with `...'. */
3737 if (typetail == NULL_TREE)
3738 break;
3741 else
3743 if (complain & tf_error)
3744 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3745 return -1;
3749 return (int) i;
3752 /* Build a binary-operation expression, after performing default
3753 conversions on the operands. CODE is the kind of expression to
3754 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3755 are the tree codes which correspond to ARG1 and ARG2 when issuing
3756 warnings about possibly misplaced parentheses. They may differ
3757 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3758 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3759 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3760 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3761 ARG2_CODE as ERROR_MARK. */
3763 tree
3764 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3765 enum tree_code arg1_code, tree arg2,
3766 enum tree_code arg2_code, tree *overload,
3767 tsubst_flags_t complain)
3769 tree orig_arg1;
3770 tree orig_arg2;
3771 tree expr;
3773 orig_arg1 = arg1;
3774 orig_arg2 = arg2;
3776 if (processing_template_decl)
3778 if (type_dependent_expression_p (arg1)
3779 || type_dependent_expression_p (arg2))
3780 return build_min_nt_loc (loc, code, arg1, arg2);
3781 arg1 = build_non_dependent_expr (arg1);
3782 arg2 = build_non_dependent_expr (arg2);
3785 if (code == DOTSTAR_EXPR)
3786 expr = build_m_component_ref (arg1, arg2, complain);
3787 else
3788 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3789 overload, complain);
3791 /* Check for cases such as x+y<<z which users are likely to
3792 misinterpret. But don't warn about obj << x + y, since that is a
3793 common idiom for I/O. */
3794 if (warn_parentheses
3795 && (complain & tf_warning)
3796 && !processing_template_decl
3797 && !error_operand_p (arg1)
3798 && !error_operand_p (arg2)
3799 && (code != LSHIFT_EXPR
3800 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3801 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3802 arg2_code, orig_arg2);
3804 if (processing_template_decl && expr != error_mark_node)
3805 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3807 return expr;
3810 /* Build and return an ARRAY_REF expression. */
3812 tree
3813 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3814 tsubst_flags_t complain)
3816 tree orig_arg1 = arg1;
3817 tree orig_arg2 = arg2;
3818 tree expr;
3820 if (processing_template_decl)
3822 if (type_dependent_expression_p (arg1)
3823 || type_dependent_expression_p (arg2))
3824 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3825 NULL_TREE, NULL_TREE);
3826 arg1 = build_non_dependent_expr (arg1);
3827 arg2 = build_non_dependent_expr (arg2);
3830 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3831 NULL_TREE, /*overload=*/NULL, complain);
3833 if (processing_template_decl && expr != error_mark_node)
3834 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3835 NULL_TREE, NULL_TREE);
3836 return expr;
3839 /* Return whether OP is an expression of enum type cast to integer
3840 type. In C++ even unsigned enum types are cast to signed integer
3841 types. We do not want to issue warnings about comparisons between
3842 signed and unsigned types when one of the types is an enum type.
3843 Those warnings are always false positives in practice. */
3845 static bool
3846 enum_cast_to_int (tree op)
3848 if (TREE_CODE (op) == NOP_EXPR
3849 && TREE_TYPE (op) == integer_type_node
3850 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3851 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3852 return true;
3854 /* The cast may have been pushed into a COND_EXPR. */
3855 if (TREE_CODE (op) == COND_EXPR)
3856 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3857 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3859 return false;
3862 /* For the c-common bits. */
3863 tree
3864 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3865 int /*convert_p*/)
3867 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3871 /* Build a binary-operation expression without default conversions.
3872 CODE is the kind of expression to build.
3873 LOCATION is the location_t of the operator in the source code.
3874 This function differs from `build' in several ways:
3875 the data type of the result is computed and recorded in it,
3876 warnings are generated if arg data types are invalid,
3877 special handling for addition and subtraction of pointers is known,
3878 and some optimization is done (operations on narrow ints
3879 are done in the narrower type when that gives the same result).
3880 Constant folding is also done before the result is returned.
3882 Note that the operands will never have enumeral types
3883 because either they have just had the default conversions performed
3884 or they have both just been converted to some other type in which
3885 the arithmetic is to be done.
3887 C++: must do special pointer arithmetic when implementing
3888 multiple inheritance, and deal with pointer to member functions. */
3890 tree
3891 cp_build_binary_op (location_t location,
3892 enum tree_code code, tree orig_op0, tree orig_op1,
3893 tsubst_flags_t complain)
3895 tree op0, op1;
3896 enum tree_code code0, code1;
3897 tree type0, type1;
3898 const char *invalid_op_diag;
3900 /* Expression code to give to the expression when it is built.
3901 Normally this is CODE, which is what the caller asked for,
3902 but in some special cases we change it. */
3903 enum tree_code resultcode = code;
3905 /* Data type in which the computation is to be performed.
3906 In the simplest cases this is the common type of the arguments. */
3907 tree result_type = NULL;
3909 /* Nonzero means operands have already been type-converted
3910 in whatever way is necessary.
3911 Zero means they need to be converted to RESULT_TYPE. */
3912 int converted = 0;
3914 /* Nonzero means create the expression with this type, rather than
3915 RESULT_TYPE. */
3916 tree build_type = 0;
3918 /* Nonzero means after finally constructing the expression
3919 convert it to this type. */
3920 tree final_type = 0;
3922 tree result;
3923 tree orig_type = NULL;
3925 /* Nonzero if this is an operation like MIN or MAX which can
3926 safely be computed in short if both args are promoted shorts.
3927 Also implies COMMON.
3928 -1 indicates a bitwise operation; this makes a difference
3929 in the exact conditions for when it is safe to do the operation
3930 in a narrower mode. */
3931 int shorten = 0;
3933 /* Nonzero if this is a comparison operation;
3934 if both args are promoted shorts, compare the original shorts.
3935 Also implies COMMON. */
3936 int short_compare = 0;
3938 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3939 int common = 0;
3941 /* True if both operands have arithmetic type. */
3942 bool arithmetic_types_p;
3944 /* Apply default conversions. */
3945 op0 = orig_op0;
3946 op1 = orig_op1;
3948 /* Remember whether we're doing / or %. */
3949 bool doing_div_or_mod = false;
3951 /* Remember whether we're doing << or >>. */
3952 bool doing_shift = false;
3954 /* Tree holding instrumentation expression. */
3955 tree instrument_expr = NULL;
3957 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3958 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3959 || code == TRUTH_XOR_EXPR)
3961 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3962 op0 = decay_conversion (op0, complain);
3963 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3964 op1 = decay_conversion (op1, complain);
3966 else
3968 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3969 op0 = cp_default_conversion (op0, complain);
3970 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3971 op1 = cp_default_conversion (op1, complain);
3974 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3975 STRIP_TYPE_NOPS (op0);
3976 STRIP_TYPE_NOPS (op1);
3978 /* DTRT if one side is an overloaded function, but complain about it. */
3979 if (type_unknown_p (op0))
3981 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3982 if (t != error_mark_node)
3984 if (complain & tf_error)
3985 permerror (input_location, "assuming cast to type %qT from overloaded function",
3986 TREE_TYPE (t));
3987 op0 = t;
3990 if (type_unknown_p (op1))
3992 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3993 if (t != error_mark_node)
3995 if (complain & tf_error)
3996 permerror (input_location, "assuming cast to type %qT from overloaded function",
3997 TREE_TYPE (t));
3998 op1 = t;
4002 type0 = TREE_TYPE (op0);
4003 type1 = TREE_TYPE (op1);
4005 /* The expression codes of the data types of the arguments tell us
4006 whether the arguments are integers, floating, pointers, etc. */
4007 code0 = TREE_CODE (type0);
4008 code1 = TREE_CODE (type1);
4010 /* If an error was already reported for one of the arguments,
4011 avoid reporting another error. */
4012 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4013 return error_mark_node;
4015 if ((invalid_op_diag
4016 = targetm.invalid_binary_op (code, type0, type1)))
4018 if (complain & tf_error)
4019 error (invalid_op_diag);
4020 return error_mark_node;
4023 /* Issue warnings about peculiar, but valid, uses of NULL. */
4024 if ((orig_op0 == null_node || orig_op1 == null_node)
4025 /* It's reasonable to use pointer values as operands of &&
4026 and ||, so NULL is no exception. */
4027 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4028 && ( /* Both are NULL (or 0) and the operation was not a
4029 comparison or a pointer subtraction. */
4030 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4031 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4032 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4033 || (!null_ptr_cst_p (orig_op0)
4034 && !TYPE_PTR_OR_PTRMEM_P (type0))
4035 || (!null_ptr_cst_p (orig_op1)
4036 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4037 && (complain & tf_warning))
4039 source_location loc =
4040 expansion_point_location_if_in_system_header (input_location);
4042 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4045 /* In case when one of the operands of the binary operation is
4046 a vector and another is a scalar -- convert scalar to vector. */
4047 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4049 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4050 complain & tf_error);
4052 switch (convert_flag)
4054 case stv_error:
4055 return error_mark_node;
4056 case stv_firstarg:
4058 op0 = save_expr (op0);
4059 op0 = convert (TREE_TYPE (type1), op0);
4060 op0 = build_vector_from_val (type1, op0);
4061 type0 = TREE_TYPE (op0);
4062 code0 = TREE_CODE (type0);
4063 converted = 1;
4064 break;
4066 case stv_secondarg:
4068 op1 = save_expr (op1);
4069 op1 = convert (TREE_TYPE (type0), op1);
4070 op1 = build_vector_from_val (type0, op1);
4071 type1 = TREE_TYPE (op1);
4072 code1 = TREE_CODE (type1);
4073 converted = 1;
4074 break;
4076 default:
4077 break;
4081 switch (code)
4083 case MINUS_EXPR:
4084 /* Subtraction of two similar pointers.
4085 We must subtract them as integers, then divide by object size. */
4086 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4087 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4088 TREE_TYPE (type1)))
4089 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4090 complain);
4091 /* In all other cases except pointer - int, the usual arithmetic
4092 rules apply. */
4093 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4095 common = 1;
4096 break;
4098 /* The pointer - int case is just like pointer + int; fall
4099 through. */
4100 case PLUS_EXPR:
4101 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4102 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4104 tree ptr_operand;
4105 tree int_operand;
4106 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4107 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4108 if (processing_template_decl)
4110 result_type = TREE_TYPE (ptr_operand);
4111 break;
4113 return cp_pointer_int_sum (code,
4114 ptr_operand,
4115 int_operand,
4116 complain);
4118 common = 1;
4119 break;
4121 case MULT_EXPR:
4122 common = 1;
4123 break;
4125 case TRUNC_DIV_EXPR:
4126 case CEIL_DIV_EXPR:
4127 case FLOOR_DIV_EXPR:
4128 case ROUND_DIV_EXPR:
4129 case EXACT_DIV_EXPR:
4130 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4131 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4132 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4133 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4135 enum tree_code tcode0 = code0, tcode1 = code1;
4136 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4137 cop1 = maybe_constant_value (cop1);
4138 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);
4178 doing_div_or_mod = true;
4179 warn_for_div_by_zero (location, cop1);
4182 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4183 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4184 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4185 common = 1;
4186 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4188 /* Although it would be tempting to shorten always here, that loses
4189 on some targets, since the modulo instruction is undefined if the
4190 quotient can't be represented in the computation mode. We shorten
4191 only if unsigned or if dividing by something we know != -1. */
4192 shorten = ((TREE_CODE (op0) == NOP_EXPR
4193 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4194 || (TREE_CODE (op1) == INTEGER_CST
4195 && ! integer_all_onesp (op1)));
4196 common = 1;
4198 break;
4200 case TRUTH_ANDIF_EXPR:
4201 case TRUTH_ORIF_EXPR:
4202 case TRUTH_AND_EXPR:
4203 case TRUTH_OR_EXPR:
4204 if (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1))
4206 sorry ("logical operation on vector type");
4207 return error_mark_node;
4209 result_type = boolean_type_node;
4210 break;
4212 /* Shift operations: result has same type as first operand;
4213 always convert second operand to int.
4214 Also set SHORT_SHIFT if shifting rightward. */
4216 case RSHIFT_EXPR:
4217 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4218 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4220 result_type = type0;
4221 converted = 1;
4223 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4224 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4225 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4226 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4228 result_type = type0;
4229 converted = 1;
4231 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4233 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4234 const_op1 = maybe_constant_value (const_op1);
4235 if (TREE_CODE (const_op1) != INTEGER_CST)
4236 const_op1 = op1;
4237 result_type = type0;
4238 doing_shift = true;
4239 if (TREE_CODE (const_op1) == INTEGER_CST)
4241 if (tree_int_cst_lt (const_op1, integer_zero_node))
4243 if ((complain & tf_warning)
4244 && c_inhibit_evaluation_warnings == 0)
4245 warning (0, "right shift count is negative");
4247 else
4249 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4250 && (complain & tf_warning)
4251 && c_inhibit_evaluation_warnings == 0)
4252 warning (0, "right shift count >= width of type");
4255 /* Convert the shift-count to an integer, regardless of
4256 size of value being shifted. */
4257 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4258 op1 = cp_convert (integer_type_node, op1, complain);
4259 /* Avoid converting op1 to result_type later. */
4260 converted = 1;
4262 break;
4264 case LSHIFT_EXPR:
4265 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4266 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4268 result_type = type0;
4269 converted = 1;
4271 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4272 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4273 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4274 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4276 result_type = type0;
4277 converted = 1;
4279 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4281 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4282 const_op1 = maybe_constant_value (const_op1);
4283 if (TREE_CODE (const_op1) != INTEGER_CST)
4284 const_op1 = op1;
4285 result_type = type0;
4286 doing_shift = true;
4287 if (TREE_CODE (const_op1) == INTEGER_CST)
4289 if (tree_int_cst_lt (const_op1, integer_zero_node))
4291 if ((complain & tf_warning)
4292 && c_inhibit_evaluation_warnings == 0)
4293 warning (0, "left shift count is negative");
4295 else if (compare_tree_int (const_op1,
4296 TYPE_PRECISION (type0)) >= 0)
4298 if ((complain & tf_warning)
4299 && c_inhibit_evaluation_warnings == 0)
4300 warning (0, "left shift count >= width of type");
4303 /* Convert the shift-count to an integer, regardless of
4304 size of value being shifted. */
4305 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4306 op1 = cp_convert (integer_type_node, op1, complain);
4307 /* Avoid converting op1 to result_type later. */
4308 converted = 1;
4310 break;
4312 case RROTATE_EXPR:
4313 case LROTATE_EXPR:
4314 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4316 result_type = type0;
4317 if (TREE_CODE (op1) == INTEGER_CST)
4319 if (tree_int_cst_lt (op1, integer_zero_node))
4321 if (complain & tf_warning)
4322 warning (0, (code == LROTATE_EXPR)
4323 ? G_("left rotate count is negative")
4324 : G_("right rotate count is negative"));
4326 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4328 if (complain & tf_warning)
4329 warning (0, (code == LROTATE_EXPR)
4330 ? G_("left rotate count >= width of type")
4331 : G_("right rotate count >= width of type"));
4334 /* Convert the shift-count to an integer, regardless of
4335 size of value being shifted. */
4336 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4337 op1 = cp_convert (integer_type_node, op1, complain);
4339 break;
4341 case EQ_EXPR:
4342 case NE_EXPR:
4343 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4344 goto vector_compare;
4345 if ((complain & tf_warning)
4346 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4347 warning (OPT_Wfloat_equal,
4348 "comparing floating point with == or != is unsafe");
4349 if ((complain & tf_warning)
4350 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4351 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4352 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4354 build_type = boolean_type_node;
4355 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4356 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4357 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4358 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4359 short_compare = 1;
4360 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4361 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4362 result_type = composite_pointer_type (type0, type1, op0, op1,
4363 CPO_COMPARISON, complain);
4364 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4365 && null_ptr_cst_p (op1))
4367 if (TREE_CODE (op0) == ADDR_EXPR
4368 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4370 if ((complain & tf_warning)
4371 && c_inhibit_evaluation_warnings == 0)
4372 warning (OPT_Waddress, "the address of %qD will never be NULL",
4373 TREE_OPERAND (op0, 0));
4375 result_type = type0;
4377 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4378 && null_ptr_cst_p (op0))
4380 if (TREE_CODE (op1) == ADDR_EXPR
4381 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4383 if ((complain & tf_warning)
4384 && c_inhibit_evaluation_warnings == 0)
4385 warning (OPT_Waddress, "the address of %qD will never be NULL",
4386 TREE_OPERAND (op1, 0));
4388 result_type = type1;
4390 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4391 /* One of the operands must be of nullptr_t type. */
4392 result_type = TREE_TYPE (nullptr_node);
4393 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4395 result_type = type0;
4396 if (complain & tf_error)
4397 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4398 else
4399 return error_mark_node;
4401 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4403 result_type = type1;
4404 if (complain & tf_error)
4405 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4406 else
4407 return error_mark_node;
4409 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4411 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4412 == ptrmemfunc_vbit_in_delta)
4414 tree pfn0, delta0, e1, e2;
4416 if (TREE_SIDE_EFFECTS (op0))
4417 op0 = save_expr (op0);
4419 pfn0 = pfn_from_ptrmemfunc (op0);
4420 delta0 = delta_from_ptrmemfunc (op0);
4421 e1 = cp_build_binary_op (location,
4422 EQ_EXPR,
4423 pfn0,
4424 build_zero_cst (TREE_TYPE (pfn0)),
4425 complain);
4426 e2 = cp_build_binary_op (location,
4427 BIT_AND_EXPR,
4428 delta0,
4429 integer_one_node,
4430 complain);
4432 if (complain & tf_warning)
4433 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4435 e2 = cp_build_binary_op (location,
4436 EQ_EXPR, e2, integer_zero_node,
4437 complain);
4438 op0 = cp_build_binary_op (location,
4439 TRUTH_ANDIF_EXPR, e1, e2,
4440 complain);
4441 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4443 else
4445 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4446 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4448 result_type = TREE_TYPE (op0);
4450 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4451 return cp_build_binary_op (location, code, op1, op0, complain);
4452 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4454 tree type;
4455 /* E will be the final comparison. */
4456 tree e;
4457 /* E1 and E2 are for scratch. */
4458 tree e1;
4459 tree e2;
4460 tree pfn0;
4461 tree pfn1;
4462 tree delta0;
4463 tree delta1;
4465 type = composite_pointer_type (type0, type1, op0, op1,
4466 CPO_COMPARISON, complain);
4468 if (!same_type_p (TREE_TYPE (op0), type))
4469 op0 = cp_convert_and_check (type, op0, complain);
4470 if (!same_type_p (TREE_TYPE (op1), type))
4471 op1 = cp_convert_and_check (type, op1, complain);
4473 if (op0 == error_mark_node || op1 == error_mark_node)
4474 return error_mark_node;
4476 if (TREE_SIDE_EFFECTS (op0))
4477 op0 = save_expr (op0);
4478 if (TREE_SIDE_EFFECTS (op1))
4479 op1 = save_expr (op1);
4481 pfn0 = pfn_from_ptrmemfunc (op0);
4482 pfn1 = pfn_from_ptrmemfunc (op1);
4483 delta0 = delta_from_ptrmemfunc (op0);
4484 delta1 = delta_from_ptrmemfunc (op1);
4485 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4486 == ptrmemfunc_vbit_in_delta)
4488 /* We generate:
4490 (op0.pfn == op1.pfn
4491 && ((op0.delta == op1.delta)
4492 || (!op0.pfn && op0.delta & 1 == 0
4493 && op1.delta & 1 == 0))
4495 The reason for the `!op0.pfn' bit is that a NULL
4496 pointer-to-member is any member with a zero PFN and
4497 LSB of the DELTA field is 0. */
4499 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4500 delta0,
4501 integer_one_node,
4502 complain);
4503 e1 = cp_build_binary_op (location,
4504 EQ_EXPR, e1, integer_zero_node,
4505 complain);
4506 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4507 delta1,
4508 integer_one_node,
4509 complain);
4510 e2 = cp_build_binary_op (location,
4511 EQ_EXPR, e2, integer_zero_node,
4512 complain);
4513 e1 = cp_build_binary_op (location,
4514 TRUTH_ANDIF_EXPR, e2, e1,
4515 complain);
4516 e2 = cp_build_binary_op (location, EQ_EXPR,
4517 pfn0,
4518 build_zero_cst (TREE_TYPE (pfn0)),
4519 complain);
4520 e2 = cp_build_binary_op (location,
4521 TRUTH_ANDIF_EXPR, e2, e1, complain);
4522 e1 = cp_build_binary_op (location,
4523 EQ_EXPR, delta0, delta1, complain);
4524 e1 = cp_build_binary_op (location,
4525 TRUTH_ORIF_EXPR, e1, e2, complain);
4527 else
4529 /* We generate:
4531 (op0.pfn == op1.pfn
4532 && (!op0.pfn || op0.delta == op1.delta))
4534 The reason for the `!op0.pfn' bit is that a NULL
4535 pointer-to-member is any member with a zero PFN; the
4536 DELTA field is unspecified. */
4538 e1 = cp_build_binary_op (location,
4539 EQ_EXPR, delta0, delta1, complain);
4540 e2 = cp_build_binary_op (location,
4541 EQ_EXPR,
4542 pfn0,
4543 build_zero_cst (TREE_TYPE (pfn0)),
4544 complain);
4545 e1 = cp_build_binary_op (location,
4546 TRUTH_ORIF_EXPR, e1, e2, complain);
4548 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4549 e = cp_build_binary_op (location,
4550 TRUTH_ANDIF_EXPR, e2, e1, complain);
4551 if (code == EQ_EXPR)
4552 return e;
4553 return cp_build_binary_op (location,
4554 EQ_EXPR, e, integer_zero_node, complain);
4556 else
4558 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4559 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4560 type1));
4561 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4562 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4563 type0));
4566 break;
4568 case MAX_EXPR:
4569 case MIN_EXPR:
4570 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4571 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4572 shorten = 1;
4573 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4574 result_type = composite_pointer_type (type0, type1, op0, op1,
4575 CPO_COMPARISON, complain);
4576 break;
4578 case LE_EXPR:
4579 case GE_EXPR:
4580 case LT_EXPR:
4581 case GT_EXPR:
4582 if (TREE_CODE (orig_op0) == STRING_CST
4583 || TREE_CODE (orig_op1) == STRING_CST)
4585 if (complain & tf_warning)
4586 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4589 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4591 vector_compare:
4592 tree intt;
4593 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4594 TREE_TYPE (type1))
4595 && !vector_types_compatible_elements_p (type0, type1))
4597 if (complain & tf_error)
4599 error_at (location, "comparing vectors with different "
4600 "element types");
4601 inform (location, "operand types are %qT and %qT",
4602 type0, type1);
4604 return error_mark_node;
4607 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4609 if (complain & tf_error)
4611 error_at (location, "comparing vectors with different "
4612 "number of elements");
4613 inform (location, "operand types are %qT and %qT",
4614 type0, type1);
4616 return error_mark_node;
4619 /* Always construct signed integer vector type. */
4620 intt = c_common_type_for_size (GET_MODE_BITSIZE
4621 (TYPE_MODE (TREE_TYPE (type0))), 0);
4622 if (!intt)
4624 if (complain & tf_error)
4625 error_at (location, "could not find an integer type "
4626 "of the same size as %qT", TREE_TYPE (type0));
4627 return error_mark_node;
4629 result_type = build_opaque_vector_type (intt,
4630 TYPE_VECTOR_SUBPARTS (type0));
4631 converted = 1;
4632 break;
4634 build_type = boolean_type_node;
4635 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4636 || code0 == ENUMERAL_TYPE)
4637 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4638 || code1 == ENUMERAL_TYPE))
4639 short_compare = 1;
4640 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4641 result_type = composite_pointer_type (type0, type1, op0, op1,
4642 CPO_COMPARISON, complain);
4643 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4645 result_type = type0;
4646 if (extra_warnings && (complain & tf_warning))
4647 warning (OPT_Wextra,
4648 "ordered comparison of pointer with integer zero");
4650 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4652 result_type = type1;
4653 if (extra_warnings && (complain & tf_warning))
4654 warning (OPT_Wextra,
4655 "ordered comparison of pointer with integer zero");
4657 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4658 /* One of the operands must be of nullptr_t type. */
4659 result_type = TREE_TYPE (nullptr_node);
4660 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4662 result_type = type0;
4663 if (complain & tf_error)
4664 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4665 else
4666 return error_mark_node;
4668 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4670 result_type = type1;
4671 if (complain & tf_error)
4672 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4673 else
4674 return error_mark_node;
4676 break;
4678 case UNORDERED_EXPR:
4679 case ORDERED_EXPR:
4680 case UNLT_EXPR:
4681 case UNLE_EXPR:
4682 case UNGT_EXPR:
4683 case UNGE_EXPR:
4684 case UNEQ_EXPR:
4685 build_type = integer_type_node;
4686 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4688 if (complain & tf_error)
4689 error ("unordered comparison on non-floating point argument");
4690 return error_mark_node;
4692 common = 1;
4693 break;
4695 default:
4696 break;
4699 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4700 || code0 == ENUMERAL_TYPE)
4701 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4702 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4703 arithmetic_types_p = 1;
4704 else
4706 arithmetic_types_p = 0;
4707 /* Vector arithmetic is only allowed when both sides are vectors. */
4708 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4710 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4711 || !vector_types_compatible_elements_p (type0, type1))
4713 if (complain & tf_error)
4714 binary_op_error (location, code, type0, type1);
4715 return error_mark_node;
4717 arithmetic_types_p = 1;
4720 /* Determine the RESULT_TYPE, if it is not already known. */
4721 if (!result_type
4722 && arithmetic_types_p
4723 && (shorten || common || short_compare))
4725 result_type = cp_common_type (type0, type1);
4726 if (complain & tf_warning)
4727 do_warn_double_promotion (result_type, type0, type1,
4728 "implicit conversion from %qT to %qT "
4729 "to match other operand of binary "
4730 "expression",
4731 location);
4734 if (!result_type)
4736 if (complain & tf_error)
4737 error ("invalid operands of types %qT and %qT to binary %qO",
4738 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4739 return error_mark_node;
4742 /* If we're in a template, the only thing we need to know is the
4743 RESULT_TYPE. */
4744 if (processing_template_decl)
4746 /* Since the middle-end checks the type when doing a build2, we
4747 need to build the tree in pieces. This built tree will never
4748 get out of the front-end as we replace it when instantiating
4749 the template. */
4750 tree tmp = build2 (resultcode,
4751 build_type ? build_type : result_type,
4752 NULL_TREE, op1);
4753 TREE_OPERAND (tmp, 0) = op0;
4754 return tmp;
4757 if (arithmetic_types_p)
4759 bool first_complex = (code0 == COMPLEX_TYPE);
4760 bool second_complex = (code1 == COMPLEX_TYPE);
4761 int none_complex = (!first_complex && !second_complex);
4763 /* Adapted from patch for c/24581. */
4764 if (first_complex != second_complex
4765 && (code == PLUS_EXPR
4766 || code == MINUS_EXPR
4767 || code == MULT_EXPR
4768 || (code == TRUNC_DIV_EXPR && first_complex))
4769 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4770 && flag_signed_zeros)
4772 /* An operation on mixed real/complex operands must be
4773 handled specially, but the language-independent code can
4774 more easily optimize the plain complex arithmetic if
4775 -fno-signed-zeros. */
4776 tree real_type = TREE_TYPE (result_type);
4777 tree real, imag;
4778 if (first_complex)
4780 if (TREE_TYPE (op0) != result_type)
4781 op0 = cp_convert_and_check (result_type, op0, complain);
4782 if (TREE_TYPE (op1) != real_type)
4783 op1 = cp_convert_and_check (real_type, op1, complain);
4785 else
4787 if (TREE_TYPE (op0) != real_type)
4788 op0 = cp_convert_and_check (real_type, op0, complain);
4789 if (TREE_TYPE (op1) != result_type)
4790 op1 = cp_convert_and_check (result_type, op1, complain);
4792 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4793 return error_mark_node;
4794 if (first_complex)
4796 op0 = save_expr (op0);
4797 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4798 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4799 switch (code)
4801 case MULT_EXPR:
4802 case TRUNC_DIV_EXPR:
4803 op1 = save_expr (op1);
4804 imag = build2 (resultcode, real_type, imag, op1);
4805 /* Fall through. */
4806 case PLUS_EXPR:
4807 case MINUS_EXPR:
4808 real = build2 (resultcode, real_type, real, op1);
4809 break;
4810 default:
4811 gcc_unreachable();
4814 else
4816 op1 = save_expr (op1);
4817 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4818 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4819 switch (code)
4821 case MULT_EXPR:
4822 op0 = save_expr (op0);
4823 imag = build2 (resultcode, real_type, op0, imag);
4824 /* Fall through. */
4825 case PLUS_EXPR:
4826 real = build2 (resultcode, real_type, op0, real);
4827 break;
4828 case MINUS_EXPR:
4829 real = build2 (resultcode, real_type, op0, real);
4830 imag = build1 (NEGATE_EXPR, real_type, imag);
4831 break;
4832 default:
4833 gcc_unreachable();
4836 real = fold_if_not_in_template (real);
4837 imag = fold_if_not_in_template (imag);
4838 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4839 result = fold_if_not_in_template (result);
4840 return result;
4843 /* For certain operations (which identify themselves by shorten != 0)
4844 if both args were extended from the same smaller type,
4845 do the arithmetic in that type and then extend.
4847 shorten !=0 and !=1 indicates a bitwise operation.
4848 For them, this optimization is safe only if
4849 both args are zero-extended or both are sign-extended.
4850 Otherwise, we might change the result.
4851 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4852 but calculated in (unsigned short) it would be (unsigned short)-1. */
4854 if (shorten && none_complex)
4856 orig_type = result_type;
4857 final_type = result_type;
4858 result_type = shorten_binary_op (result_type, op0, op1,
4859 shorten == -1);
4862 /* Comparison operations are shortened too but differently.
4863 They identify themselves by setting short_compare = 1. */
4865 if (short_compare)
4867 /* Don't write &op0, etc., because that would prevent op0
4868 from being kept in a register.
4869 Instead, make copies of the our local variables and
4870 pass the copies by reference, then copy them back afterward. */
4871 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4872 enum tree_code xresultcode = resultcode;
4873 tree val
4874 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4875 &xresultcode);
4876 if (val != 0)
4877 return cp_convert (boolean_type_node, val, complain);
4878 op0 = xop0, op1 = xop1;
4879 converted = 1;
4880 resultcode = xresultcode;
4883 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4884 && warn_sign_compare
4885 /* Do not warn until the template is instantiated; we cannot
4886 bound the ranges of the arguments until that point. */
4887 && !processing_template_decl
4888 && (complain & tf_warning)
4889 && c_inhibit_evaluation_warnings == 0
4890 /* Even unsigned enum types promote to signed int. We don't
4891 want to issue -Wsign-compare warnings for this case. */
4892 && !enum_cast_to_int (orig_op0)
4893 && !enum_cast_to_int (orig_op1))
4895 tree oop0 = maybe_constant_value (orig_op0);
4896 tree oop1 = maybe_constant_value (orig_op1);
4898 if (TREE_CODE (oop0) != INTEGER_CST)
4899 oop0 = orig_op0;
4900 if (TREE_CODE (oop1) != INTEGER_CST)
4901 oop1 = orig_op1;
4902 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4903 result_type, resultcode);
4907 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4908 Then the expression will be built.
4909 It will be given type FINAL_TYPE if that is nonzero;
4910 otherwise, it will be given type RESULT_TYPE. */
4911 if (! converted)
4913 if (TREE_TYPE (op0) != result_type)
4914 op0 = cp_convert_and_check (result_type, op0, complain);
4915 if (TREE_TYPE (op1) != result_type)
4916 op1 = cp_convert_and_check (result_type, op1, complain);
4918 if (op0 == error_mark_node || op1 == error_mark_node)
4919 return error_mark_node;
4922 if (build_type == NULL_TREE)
4923 build_type = result_type;
4925 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
4926 | SANITIZE_FLOAT_DIVIDE))
4927 && !processing_template_decl
4928 && current_function_decl != 0
4929 && !lookup_attribute ("no_sanitize_undefined",
4930 DECL_ATTRIBUTES (current_function_decl))
4931 && (doing_div_or_mod || doing_shift))
4933 /* OP0 and/or OP1 might have side-effects. */
4934 op0 = cp_save_expr (op0);
4935 op1 = cp_save_expr (op1);
4936 op0 = maybe_constant_value (fold_non_dependent_expr_sfinae (op0,
4937 tf_none));
4938 op1 = maybe_constant_value (fold_non_dependent_expr_sfinae (op1,
4939 tf_none));
4940 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
4941 | SANITIZE_FLOAT_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 (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 (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 (t1, t2, complain) || can_convert (t2, t1, complain))
6611 if (!c_cast_p
6612 && check_for_casting_away_constness (intype, type,
6613 STATIC_CAST_EXPR,
6614 complain))
6615 return error_mark_node;
6616 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6617 c_cast_p, complain);
6621 /* [expr.static.cast]
6623 An rvalue of type "pointer to cv void" can be explicitly
6624 converted to a pointer to object type. A value of type pointer
6625 to object converted to "pointer to cv void" and back to the
6626 original pointer type will have its original value. */
6627 if (TYPE_PTR_P (intype)
6628 && VOID_TYPE_P (TREE_TYPE (intype))
6629 && TYPE_PTROB_P (type))
6631 if (!c_cast_p
6632 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6633 complain))
6634 return error_mark_node;
6635 return build_nop (type, expr);
6638 *valid_p = false;
6639 return error_mark_node;
6642 /* Return an expression representing static_cast<TYPE>(EXPR). */
6644 tree
6645 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6647 tree result;
6648 bool valid_p;
6650 if (type == error_mark_node || expr == error_mark_node)
6651 return error_mark_node;
6653 if (processing_template_decl)
6655 expr = build_min (STATIC_CAST_EXPR, type, expr);
6656 /* We don't know if it will or will not have side effects. */
6657 TREE_SIDE_EFFECTS (expr) = 1;
6658 return convert_from_reference (expr);
6661 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6662 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6663 if (TREE_CODE (type) != REFERENCE_TYPE
6664 && TREE_CODE (expr) == NOP_EXPR
6665 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6666 expr = TREE_OPERAND (expr, 0);
6668 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6669 complain);
6670 if (valid_p)
6672 if (result != error_mark_node)
6673 maybe_warn_about_useless_cast (type, expr, complain);
6674 return result;
6677 if (complain & tf_error)
6678 error ("invalid static_cast from type %qT to type %qT",
6679 TREE_TYPE (expr), type);
6680 return error_mark_node;
6683 /* EXPR is an expression with member function or pointer-to-member
6684 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6685 not permitted by ISO C++, but we accept it in some modes. If we
6686 are not in one of those modes, issue a diagnostic. Return the
6687 converted expression. */
6689 tree
6690 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6692 tree intype;
6693 tree decl;
6695 intype = TREE_TYPE (expr);
6696 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6697 || TREE_CODE (intype) == METHOD_TYPE);
6699 if (!(complain & tf_warning_or_error))
6700 return error_mark_node;
6702 if (pedantic || warn_pmf2ptr)
6703 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6704 "converting from %qT to %qT", intype, type);
6706 if (TREE_CODE (intype) == METHOD_TYPE)
6707 expr = build_addr_func (expr, complain);
6708 else if (TREE_CODE (expr) == PTRMEM_CST)
6709 expr = build_address (PTRMEM_CST_MEMBER (expr));
6710 else
6712 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6713 decl = build_address (decl);
6714 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6717 if (expr == error_mark_node)
6718 return error_mark_node;
6720 return build_nop (type, expr);
6723 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6724 If C_CAST_P is true, this reinterpret cast is being done as part of
6725 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6726 indicate whether or not reinterpret_cast was valid. */
6728 static tree
6729 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6730 bool *valid_p, tsubst_flags_t complain)
6732 tree intype;
6734 /* Assume the cast is invalid. */
6735 if (valid_p)
6736 *valid_p = true;
6738 if (type == error_mark_node || error_operand_p (expr))
6739 return error_mark_node;
6741 intype = TREE_TYPE (expr);
6743 /* Save casted types in the function's used types hash table. */
6744 used_types_insert (type);
6746 /* [expr.reinterpret.cast]
6747 An lvalue expression of type T1 can be cast to the type
6748 "reference to T2" if an expression of type "pointer to T1" can be
6749 explicitly converted to the type "pointer to T2" using a
6750 reinterpret_cast. */
6751 if (TREE_CODE (type) == REFERENCE_TYPE)
6753 if (! real_lvalue_p (expr))
6755 if (complain & tf_error)
6756 error ("invalid cast of an rvalue expression of type "
6757 "%qT to type %qT",
6758 intype, type);
6759 return error_mark_node;
6762 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6763 "B" are related class types; the reinterpret_cast does not
6764 adjust the pointer. */
6765 if (TYPE_PTR_P (intype)
6766 && (complain & tf_warning)
6767 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6768 COMPARE_BASE | COMPARE_DERIVED)))
6769 warning (0, "casting %qT to %qT does not dereference pointer",
6770 intype, type);
6772 expr = cp_build_addr_expr (expr, complain);
6774 if (warn_strict_aliasing > 2)
6775 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6777 if (expr != error_mark_node)
6778 expr = build_reinterpret_cast_1
6779 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6780 valid_p, complain);
6781 if (expr != error_mark_node)
6782 /* cp_build_indirect_ref isn't right for rvalue refs. */
6783 expr = convert_from_reference (fold_convert (type, expr));
6784 return expr;
6787 /* As a G++ extension, we consider conversions from member
6788 functions, and pointers to member functions to
6789 pointer-to-function and pointer-to-void types. If
6790 -Wno-pmf-conversions has not been specified,
6791 convert_member_func_to_ptr will issue an error message. */
6792 if ((TYPE_PTRMEMFUNC_P (intype)
6793 || TREE_CODE (intype) == METHOD_TYPE)
6794 && TYPE_PTR_P (type)
6795 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6796 || VOID_TYPE_P (TREE_TYPE (type))))
6797 return convert_member_func_to_ptr (type, expr, complain);
6799 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6800 array-to-pointer, and function-to-pointer conversions are
6801 performed. */
6802 expr = decay_conversion (expr, complain);
6804 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6805 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6806 if (TREE_CODE (expr) == NOP_EXPR
6807 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6808 expr = TREE_OPERAND (expr, 0);
6810 if (error_operand_p (expr))
6811 return error_mark_node;
6813 intype = TREE_TYPE (expr);
6815 /* [expr.reinterpret.cast]
6816 A pointer can be converted to any integral type large enough to
6817 hold it. ... A value of type std::nullptr_t can be converted to
6818 an integral type; the conversion has the same meaning and
6819 validity as a conversion of (void*)0 to the integral type. */
6820 if (CP_INTEGRAL_TYPE_P (type)
6821 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6823 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6825 if (complain & tf_error)
6826 permerror (input_location, "cast from %qT to %qT loses precision",
6827 intype, type);
6828 else
6829 return error_mark_node;
6831 if (NULLPTR_TYPE_P (intype))
6832 return build_int_cst (type, 0);
6834 /* [expr.reinterpret.cast]
6835 A value of integral or enumeration type can be explicitly
6836 converted to a pointer. */
6837 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6838 /* OK */
6840 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6841 || TYPE_PTR_OR_PTRMEM_P (type))
6842 && same_type_p (type, intype))
6843 /* DR 799 */
6844 return fold_if_not_in_template (build_nop (type, expr));
6845 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6846 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6847 return fold_if_not_in_template (build_nop (type, expr));
6848 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6849 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6851 tree sexpr = expr;
6853 if (!c_cast_p
6854 && check_for_casting_away_constness (intype, type,
6855 REINTERPRET_CAST_EXPR,
6856 complain))
6857 return error_mark_node;
6858 /* Warn about possible alignment problems. */
6859 if (STRICT_ALIGNMENT && warn_cast_align
6860 && (complain & tf_warning)
6861 && !VOID_TYPE_P (type)
6862 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6863 && COMPLETE_TYPE_P (TREE_TYPE (type))
6864 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6865 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6866 warning (OPT_Wcast_align, "cast from %qT to %qT "
6867 "increases required alignment of target type", intype, type);
6869 /* We need to strip nops here, because the front end likes to
6870 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6871 STRIP_NOPS (sexpr);
6872 if (warn_strict_aliasing <= 2)
6873 strict_aliasing_warning (intype, type, sexpr);
6875 return fold_if_not_in_template (build_nop (type, expr));
6877 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6878 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6880 if (complain & tf_warning)
6881 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6882 object pointer type or vice versa is conditionally-supported." */
6883 warning (OPT_Wconditionally_supported,
6884 "casting between pointer-to-function and pointer-to-object "
6885 "is conditionally-supported");
6886 return fold_if_not_in_template (build_nop (type, expr));
6888 else if (TREE_CODE (type) == VECTOR_TYPE)
6889 return fold_if_not_in_template (convert_to_vector (type, expr));
6890 else if (TREE_CODE (intype) == VECTOR_TYPE
6891 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6892 return fold_if_not_in_template (convert_to_integer (type, expr));
6893 else
6895 if (valid_p)
6896 *valid_p = false;
6897 if (complain & tf_error)
6898 error ("invalid cast from type %qT to type %qT", intype, type);
6899 return error_mark_node;
6902 return cp_convert (type, expr, complain);
6905 tree
6906 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6908 tree r;
6910 if (type == error_mark_node || expr == error_mark_node)
6911 return error_mark_node;
6913 if (processing_template_decl)
6915 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6917 if (!TREE_SIDE_EFFECTS (t)
6918 && type_dependent_expression_p (expr))
6919 /* There might turn out to be side effects inside expr. */
6920 TREE_SIDE_EFFECTS (t) = 1;
6921 return convert_from_reference (t);
6924 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6925 /*valid_p=*/NULL, complain);
6926 if (r != error_mark_node)
6927 maybe_warn_about_useless_cast (type, expr, complain);
6928 return r;
6931 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6932 return an appropriate expression. Otherwise, return
6933 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6934 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6935 performing a C-style cast, its value upon return will indicate
6936 whether or not the conversion succeeded. */
6938 static tree
6939 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6940 bool *valid_p)
6942 tree src_type;
6943 tree reference_type;
6945 /* Callers are responsible for handling error_mark_node as a
6946 destination type. */
6947 gcc_assert (dst_type != error_mark_node);
6948 /* In a template, callers should be building syntactic
6949 representations of casts, not using this machinery. */
6950 gcc_assert (!processing_template_decl);
6952 /* Assume the conversion is invalid. */
6953 if (valid_p)
6954 *valid_p = false;
6956 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6958 if (complain & tf_error)
6959 error ("invalid use of const_cast with type %qT, "
6960 "which is not a pointer, "
6961 "reference, nor a pointer-to-data-member type", dst_type);
6962 return error_mark_node;
6965 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6967 if (complain & tf_error)
6968 error ("invalid use of const_cast with type %qT, which is a pointer "
6969 "or reference to a function type", dst_type);
6970 return error_mark_node;
6973 /* Save casted types in the function's used types hash table. */
6974 used_types_insert (dst_type);
6976 src_type = TREE_TYPE (expr);
6977 /* Expressions do not really have reference types. */
6978 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6979 src_type = TREE_TYPE (src_type);
6981 /* [expr.const.cast]
6983 For two object types T1 and T2, if a pointer to T1 can be explicitly
6984 converted to the type "pointer to T2" using a const_cast, then the
6985 following conversions can also be made:
6987 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6988 type T2 using the cast const_cast<T2&>;
6990 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6991 type T2 using the cast const_cast<T2&&>; and
6993 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6994 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6996 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6998 reference_type = dst_type;
6999 if (!TYPE_REF_IS_RVALUE (dst_type)
7000 ? real_lvalue_p (expr)
7001 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7002 ? lvalue_p (expr)
7003 : lvalue_or_rvalue_with_address_p (expr)))
7004 /* OK. */;
7005 else
7007 if (complain & tf_error)
7008 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7009 src_type, dst_type);
7010 return error_mark_node;
7012 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7013 src_type = build_pointer_type (src_type);
7015 else
7017 reference_type = NULL_TREE;
7018 /* If the destination type is not a reference type, the
7019 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7020 conversions are performed. */
7021 src_type = type_decays_to (src_type);
7022 if (src_type == error_mark_node)
7023 return error_mark_node;
7026 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7028 if (comp_ptr_ttypes_const (dst_type, src_type))
7030 if (valid_p)
7032 *valid_p = true;
7033 /* This cast is actually a C-style cast. Issue a warning if
7034 the user is making a potentially unsafe cast. */
7035 check_for_casting_away_constness (src_type, dst_type,
7036 CAST_EXPR, complain);
7038 if (reference_type)
7040 expr = cp_build_addr_expr (expr, complain);
7041 if (expr == error_mark_node)
7042 return error_mark_node;
7043 expr = build_nop (reference_type, expr);
7044 return convert_from_reference (expr);
7046 else
7048 expr = decay_conversion (expr, complain);
7049 if (expr == error_mark_node)
7050 return error_mark_node;
7052 /* build_c_cast puts on a NOP_EXPR to make the result not an
7053 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7054 non-lvalue context. */
7055 if (TREE_CODE (expr) == NOP_EXPR
7056 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7057 expr = TREE_OPERAND (expr, 0);
7058 return build_nop (dst_type, expr);
7061 else if (valid_p
7062 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7063 TREE_TYPE (src_type)))
7064 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7065 complain);
7068 if (complain & tf_error)
7069 error ("invalid const_cast from type %qT to type %qT",
7070 src_type, dst_type);
7071 return error_mark_node;
7074 tree
7075 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7077 tree r;
7079 if (type == error_mark_node || error_operand_p (expr))
7080 return error_mark_node;
7082 if (processing_template_decl)
7084 tree t = build_min (CONST_CAST_EXPR, type, expr);
7086 if (!TREE_SIDE_EFFECTS (t)
7087 && type_dependent_expression_p (expr))
7088 /* There might turn out to be side effects inside expr. */
7089 TREE_SIDE_EFFECTS (t) = 1;
7090 return convert_from_reference (t);
7093 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7094 if (r != error_mark_node)
7095 maybe_warn_about_useless_cast (type, expr, complain);
7096 return r;
7099 /* Like cp_build_c_cast, but for the c-common bits. */
7101 tree
7102 build_c_cast (location_t /*loc*/, tree type, tree expr)
7104 return cp_build_c_cast (type, expr, tf_warning_or_error);
7107 /* Build an expression representing an explicit C-style cast to type
7108 TYPE of expression EXPR. */
7110 tree
7111 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7113 tree value = expr;
7114 tree result;
7115 bool valid_p;
7117 if (type == error_mark_node || error_operand_p (expr))
7118 return error_mark_node;
7120 if (processing_template_decl)
7122 tree t = build_min (CAST_EXPR, type,
7123 tree_cons (NULL_TREE, value, NULL_TREE));
7124 /* We don't know if it will or will not have side effects. */
7125 TREE_SIDE_EFFECTS (t) = 1;
7126 return convert_from_reference (t);
7129 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7130 'Class') should always be retained, because this information aids
7131 in method lookup. */
7132 if (objc_is_object_ptr (type)
7133 && objc_is_object_ptr (TREE_TYPE (expr)))
7134 return build_nop (type, expr);
7136 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7137 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7138 if (TREE_CODE (type) != REFERENCE_TYPE
7139 && TREE_CODE (value) == NOP_EXPR
7140 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7141 value = TREE_OPERAND (value, 0);
7143 if (TREE_CODE (type) == ARRAY_TYPE)
7145 /* Allow casting from T1* to T2[] because Cfront allows it.
7146 NIHCL uses it. It is not valid ISO C++ however. */
7147 if (TYPE_PTR_P (TREE_TYPE (expr)))
7149 if (complain & tf_error)
7150 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7151 else
7152 return error_mark_node;
7153 type = build_pointer_type (TREE_TYPE (type));
7155 else
7157 if (complain & tf_error)
7158 error ("ISO C++ forbids casting to an array type %qT", type);
7159 return error_mark_node;
7163 if (TREE_CODE (type) == FUNCTION_TYPE
7164 || TREE_CODE (type) == METHOD_TYPE)
7166 if (complain & tf_error)
7167 error ("invalid cast to function type %qT", type);
7168 return error_mark_node;
7171 if (TYPE_PTR_P (type)
7172 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7173 /* Casting to an integer of smaller size is an error detected elsewhere. */
7174 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7175 /* Don't warn about converting any constant. */
7176 && !TREE_CONSTANT (value))
7177 warning_at (input_location, OPT_Wint_to_pointer_cast,
7178 "cast to pointer from integer of different size");
7180 /* A C-style cast can be a const_cast. */
7181 result = build_const_cast_1 (type, value, complain & tf_warning,
7182 &valid_p);
7183 if (valid_p)
7185 if (result != error_mark_node)
7186 maybe_warn_about_useless_cast (type, value, complain);
7187 return result;
7190 /* Or a static cast. */
7191 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7192 &valid_p, complain);
7193 /* Or a reinterpret_cast. */
7194 if (!valid_p)
7195 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7196 &valid_p, complain);
7197 /* The static_cast or reinterpret_cast may be followed by a
7198 const_cast. */
7199 if (valid_p
7200 /* A valid cast may result in errors if, for example, a
7201 conversion to an ambiguous base class is required. */
7202 && !error_operand_p (result))
7204 tree result_type;
7206 maybe_warn_about_useless_cast (type, value, complain);
7208 /* Non-class rvalues always have cv-unqualified type. */
7209 if (!CLASS_TYPE_P (type))
7210 type = TYPE_MAIN_VARIANT (type);
7211 result_type = TREE_TYPE (result);
7212 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7213 result_type = TYPE_MAIN_VARIANT (result_type);
7214 /* If the type of RESULT does not match TYPE, perform a
7215 const_cast to make it match. If the static_cast or
7216 reinterpret_cast succeeded, we will differ by at most
7217 cv-qualification, so the follow-on const_cast is guaranteed
7218 to succeed. */
7219 if (!same_type_p (non_reference (type), non_reference (result_type)))
7221 result = build_const_cast_1 (type, result, false, &valid_p);
7222 gcc_assert (valid_p);
7224 return result;
7227 return error_mark_node;
7230 /* For use from the C common bits. */
7231 tree
7232 build_modify_expr (location_t /*location*/,
7233 tree lhs, tree /*lhs_origtype*/,
7234 enum tree_code modifycode,
7235 location_t /*rhs_location*/, tree rhs,
7236 tree /*rhs_origtype*/)
7238 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7241 /* Build an assignment expression of lvalue LHS from value RHS.
7242 MODIFYCODE is the code for a binary operator that we use
7243 to combine the old value of LHS with RHS to get the new value.
7244 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7246 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7248 tree
7249 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7250 tsubst_flags_t complain)
7252 tree result;
7253 tree newrhs = rhs;
7254 tree lhstype = TREE_TYPE (lhs);
7255 tree olhstype = lhstype;
7256 bool plain_assign = (modifycode == NOP_EXPR);
7258 /* Avoid duplicate error messages from operands that had errors. */
7259 if (error_operand_p (lhs) || error_operand_p (rhs))
7260 return error_mark_node;
7262 /* Handle control structure constructs used as "lvalues". */
7263 switch (TREE_CODE (lhs))
7265 /* Handle --foo = 5; as these are valid constructs in C++. */
7266 case PREDECREMENT_EXPR:
7267 case PREINCREMENT_EXPR:
7268 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7269 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7270 stabilize_reference (TREE_OPERAND (lhs, 0)),
7271 TREE_OPERAND (lhs, 1));
7272 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7273 modifycode, rhs, complain);
7274 if (newrhs == error_mark_node)
7275 return error_mark_node;
7276 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7278 /* Handle (a, b) used as an "lvalue". */
7279 case COMPOUND_EXPR:
7280 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7281 modifycode, rhs, complain);
7282 if (newrhs == error_mark_node)
7283 return error_mark_node;
7284 return build2 (COMPOUND_EXPR, lhstype,
7285 TREE_OPERAND (lhs, 0), newrhs);
7287 case MODIFY_EXPR:
7288 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7289 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7290 stabilize_reference (TREE_OPERAND (lhs, 0)),
7291 TREE_OPERAND (lhs, 1));
7292 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7293 complain);
7294 if (newrhs == error_mark_node)
7295 return error_mark_node;
7296 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7298 case MIN_EXPR:
7299 case MAX_EXPR:
7300 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7301 when neither operand has side-effects. */
7302 if (!lvalue_or_else (lhs, lv_assign, complain))
7303 return error_mark_node;
7305 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7306 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7308 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7309 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7310 boolean_type_node,
7311 TREE_OPERAND (lhs, 0),
7312 TREE_OPERAND (lhs, 1)),
7313 TREE_OPERAND (lhs, 0),
7314 TREE_OPERAND (lhs, 1));
7315 /* Fall through. */
7317 /* Handle (a ? b : c) used as an "lvalue". */
7318 case COND_EXPR:
7320 /* Produce (a ? (b = rhs) : (c = rhs))
7321 except that the RHS goes through a save-expr
7322 so the code to compute it is only emitted once. */
7323 tree cond;
7324 tree preeval = NULL_TREE;
7326 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7328 if (complain & tf_error)
7329 error ("void value not ignored as it ought to be");
7330 return error_mark_node;
7333 rhs = stabilize_expr (rhs, &preeval);
7335 /* Check this here to avoid odd errors when trying to convert
7336 a throw to the type of the COND_EXPR. */
7337 if (!lvalue_or_else (lhs, lv_assign, complain))
7338 return error_mark_node;
7340 cond = build_conditional_expr
7341 (input_location, TREE_OPERAND (lhs, 0),
7342 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7343 modifycode, rhs, complain),
7344 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7345 modifycode, rhs, complain),
7346 complain);
7348 if (cond == error_mark_node)
7349 return cond;
7350 /* Make sure the code to compute the rhs comes out
7351 before the split. */
7352 if (preeval)
7353 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7354 return cond;
7357 default:
7358 break;
7361 if (modifycode == INIT_EXPR)
7363 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7364 /* Do the default thing. */;
7365 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7367 /* Compound literal. */
7368 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7369 /* Call convert to generate an error; see PR 11063. */
7370 rhs = convert (lhstype, rhs);
7371 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7372 TREE_SIDE_EFFECTS (result) = 1;
7373 return result;
7375 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7376 /* Do the default thing. */;
7377 else
7379 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7380 result = build_special_member_call (lhs, complete_ctor_identifier,
7381 &rhs_vec, lhstype, LOOKUP_NORMAL,
7382 complain);
7383 release_tree_vector (rhs_vec);
7384 if (result == NULL_TREE)
7385 return error_mark_node;
7386 return result;
7389 else
7391 lhs = require_complete_type_sfinae (lhs, complain);
7392 if (lhs == error_mark_node)
7393 return error_mark_node;
7395 if (modifycode == NOP_EXPR)
7397 if (c_dialect_objc ())
7399 result = objc_maybe_build_modify_expr (lhs, rhs);
7400 if (result)
7401 return result;
7404 /* `operator=' is not an inheritable operator. */
7405 if (! MAYBE_CLASS_TYPE_P (lhstype))
7406 /* Do the default thing. */;
7407 else
7409 result = build_new_op (input_location, MODIFY_EXPR,
7410 LOOKUP_NORMAL, lhs, rhs,
7411 make_node (NOP_EXPR), /*overload=*/NULL,
7412 complain);
7413 if (result == NULL_TREE)
7414 return error_mark_node;
7415 return result;
7417 lhstype = olhstype;
7419 else
7421 tree init = NULL_TREE;
7423 /* A binary op has been requested. Combine the old LHS
7424 value with the RHS producing the value we should actually
7425 store into the LHS. */
7426 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7427 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7428 || MAYBE_CLASS_TYPE_P (lhstype)));
7430 /* Preevaluate the RHS to make sure its evaluation is complete
7431 before the lvalue-to-rvalue conversion of the LHS:
7433 [expr.ass] With respect to an indeterminately-sequenced
7434 function call, the operation of a compound assignment is a
7435 single evaluation. [ Note: Therefore, a function call shall
7436 not intervene between the lvalue-to-rvalue conversion and the
7437 side effect associated with any single compound assignment
7438 operator. -- end note ] */
7439 lhs = stabilize_reference (lhs);
7440 rhs = rvalue (rhs);
7441 rhs = stabilize_expr (rhs, &init);
7442 newrhs = cp_build_binary_op (input_location,
7443 modifycode, lhs, rhs,
7444 complain);
7445 if (newrhs == error_mark_node)
7447 if (complain & tf_error)
7448 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7449 TREE_TYPE (lhs), TREE_TYPE (rhs));
7450 return error_mark_node;
7453 if (init)
7454 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7456 /* Now it looks like a plain assignment. */
7457 modifycode = NOP_EXPR;
7458 if (c_dialect_objc ())
7460 result = objc_maybe_build_modify_expr (lhs, newrhs);
7461 if (result)
7462 return result;
7465 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7466 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7469 /* The left-hand side must be an lvalue. */
7470 if (!lvalue_or_else (lhs, lv_assign, complain))
7471 return error_mark_node;
7473 /* Warn about modifying something that is `const'. Don't warn if
7474 this is initialization. */
7475 if (modifycode != INIT_EXPR
7476 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7477 /* Functions are not modifiable, even though they are
7478 lvalues. */
7479 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7480 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7481 /* If it's an aggregate and any field is const, then it is
7482 effectively const. */
7483 || (CLASS_TYPE_P (lhstype)
7484 && C_TYPE_FIELDS_READONLY (lhstype))))
7486 if (complain & tf_error)
7487 cxx_readonly_error (lhs, lv_assign);
7488 else
7489 return error_mark_node;
7492 /* If storing into a structure or union member, it may have been given a
7493 lowered bitfield type. We need to convert to the declared type first,
7494 so retrieve it now. */
7496 olhstype = unlowered_expr_type (lhs);
7498 /* Convert new value to destination type. */
7500 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7502 int from_array;
7504 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7506 if (modifycode != INIT_EXPR)
7508 if (complain & tf_error)
7509 error ("assigning to an array from an initializer list");
7510 return error_mark_node;
7512 if (check_array_initializer (lhs, lhstype, newrhs))
7513 return error_mark_node;
7514 newrhs = digest_init (lhstype, newrhs, complain);
7515 if (newrhs == error_mark_node)
7516 return error_mark_node;
7519 /* C++11 8.5/17: "If the destination type is an array of characters,
7520 an array of char16_t, an array of char32_t, or an array of wchar_t,
7521 and the initializer is a string literal...". */
7522 else if (TREE_CODE (newrhs) == STRING_CST
7523 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7524 && modifycode == INIT_EXPR)
7526 newrhs = digest_init (lhstype, newrhs, complain);
7527 if (newrhs == error_mark_node)
7528 return error_mark_node;
7531 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7532 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7534 if (complain & tf_error)
7535 error ("incompatible types in assignment of %qT to %qT",
7536 TREE_TYPE (rhs), lhstype);
7537 return error_mark_node;
7540 /* Allow array assignment in compiler-generated code. */
7541 else if (!current_function_decl
7542 || !DECL_DEFAULTED_FN (current_function_decl))
7544 /* This routine is used for both initialization and assignment.
7545 Make sure the diagnostic message differentiates the context. */
7546 if (complain & tf_error)
7548 if (modifycode == INIT_EXPR)
7549 error ("array used as initializer");
7550 else
7551 error ("invalid array assignment");
7553 return error_mark_node;
7556 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7557 ? 1 + (modifycode != INIT_EXPR): 0;
7558 return build_vec_init (lhs, NULL_TREE, newrhs,
7559 /*explicit_value_init_p=*/false,
7560 from_array, complain);
7563 if (modifycode == INIT_EXPR)
7564 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7565 LOOKUP_ONLYCONVERTING. */
7566 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7567 ICR_INIT, NULL_TREE, 0,
7568 complain);
7569 else
7570 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7571 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7573 if (!same_type_p (lhstype, olhstype))
7574 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7576 if (modifycode != INIT_EXPR)
7578 if (TREE_CODE (newrhs) == CALL_EXPR
7579 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7580 newrhs = build_cplus_new (lhstype, newrhs, complain);
7582 /* Can't initialize directly from a TARGET_EXPR, since that would
7583 cause the lhs to be constructed twice, and possibly result in
7584 accidental self-initialization. So we force the TARGET_EXPR to be
7585 expanded without a target. */
7586 if (TREE_CODE (newrhs) == TARGET_EXPR)
7587 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7588 TREE_OPERAND (newrhs, 0));
7591 if (newrhs == error_mark_node)
7592 return error_mark_node;
7594 if (c_dialect_objc () && flag_objc_gc)
7596 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7598 if (result)
7599 return result;
7602 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7603 lhstype, lhs, newrhs);
7605 TREE_SIDE_EFFECTS (result) = 1;
7606 if (!plain_assign)
7607 TREE_NO_WARNING (result) = 1;
7609 return result;
7612 tree
7613 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7614 tree rhs, tsubst_flags_t complain)
7616 if (processing_template_decl)
7617 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7618 build_min_nt_loc (loc, modifycode, NULL_TREE,
7619 NULL_TREE), rhs);
7621 if (modifycode != NOP_EXPR)
7623 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7624 make_node (modifycode), /*overload=*/NULL,
7625 complain);
7626 if (rval)
7628 TREE_NO_WARNING (rval) = 1;
7629 return rval;
7632 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7635 /* Helper function for get_delta_difference which assumes FROM is a base
7636 class of TO. Returns a delta for the conversion of pointer-to-member
7637 of FROM to pointer-to-member of TO. If the conversion is invalid and
7638 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7639 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7640 If C_CAST_P is true, this conversion is taking place as part of a
7641 C-style cast. */
7643 static tree
7644 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7645 tsubst_flags_t complain)
7647 tree binfo;
7648 base_kind kind;
7650 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7651 &kind, complain);
7653 if (binfo == error_mark_node)
7655 if (!(complain & tf_error))
7656 return error_mark_node;
7658 error (" in pointer to member function conversion");
7659 return size_zero_node;
7661 else if (binfo)
7663 if (kind != bk_via_virtual)
7664 return BINFO_OFFSET (binfo);
7665 else
7666 /* FROM is a virtual base class of TO. Issue an error or warning
7667 depending on whether or not this is a reinterpret cast. */
7669 if (!(complain & tf_error))
7670 return error_mark_node;
7672 error ("pointer to member conversion via virtual base %qT",
7673 BINFO_TYPE (binfo_from_vbase (binfo)));
7675 return size_zero_node;
7678 else
7679 return NULL_TREE;
7682 /* Get difference in deltas for different pointer to member function
7683 types. If the conversion is invalid and tf_error is not set in
7684 COMPLAIN, returns error_mark_node, otherwise returns an integer
7685 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7686 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7687 conversions as well. If C_CAST_P is true this conversion is taking
7688 place as part of a C-style cast.
7690 Note that the naming of FROM and TO is kind of backwards; the return
7691 value is what we add to a TO in order to get a FROM. They are named
7692 this way because we call this function to find out how to convert from
7693 a pointer to member of FROM to a pointer to member of TO. */
7695 static tree
7696 get_delta_difference (tree from, tree to,
7697 bool allow_inverse_p,
7698 bool c_cast_p, tsubst_flags_t complain)
7700 tree result;
7702 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7703 /* Pointer to member of incomplete class is permitted*/
7704 result = size_zero_node;
7705 else
7706 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7708 if (result == error_mark_node)
7709 return error_mark_node;
7711 if (!result)
7713 if (!allow_inverse_p)
7715 if (!(complain & tf_error))
7716 return error_mark_node;
7718 error_not_base_type (from, to);
7719 error (" in pointer to member conversion");
7720 result = size_zero_node;
7722 else
7724 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7726 if (result == error_mark_node)
7727 return error_mark_node;
7729 if (result)
7730 result = size_diffop_loc (input_location,
7731 size_zero_node, result);
7732 else
7734 if (!(complain & tf_error))
7735 return error_mark_node;
7737 error_not_base_type (from, to);
7738 error (" in pointer to member conversion");
7739 result = size_zero_node;
7744 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7745 result));
7748 /* Return a constructor for the pointer-to-member-function TYPE using
7749 the other components as specified. */
7751 tree
7752 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7754 tree u = NULL_TREE;
7755 tree delta_field;
7756 tree pfn_field;
7757 vec<constructor_elt, va_gc> *v;
7759 /* Pull the FIELD_DECLs out of the type. */
7760 pfn_field = TYPE_FIELDS (type);
7761 delta_field = DECL_CHAIN (pfn_field);
7763 /* Make sure DELTA has the type we want. */
7764 delta = convert_and_check (input_location, delta_type_node, delta);
7766 /* Convert to the correct target type if necessary. */
7767 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7769 /* Finish creating the initializer. */
7770 vec_alloc (v, 2);
7771 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7772 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7773 u = build_constructor (type, v);
7774 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7775 TREE_STATIC (u) = (TREE_CONSTANT (u)
7776 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7777 != NULL_TREE)
7778 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7779 != NULL_TREE));
7780 return u;
7783 /* Build a constructor for a pointer to member function. It can be
7784 used to initialize global variables, local variable, or used
7785 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7786 want to be.
7788 If FORCE is nonzero, then force this conversion, even if
7789 we would rather not do it. Usually set when using an explicit
7790 cast. A C-style cast is being processed iff C_CAST_P is true.
7792 Return error_mark_node, if something goes wrong. */
7794 tree
7795 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7796 tsubst_flags_t complain)
7798 tree fn;
7799 tree pfn_type;
7800 tree to_type;
7802 if (error_operand_p (pfn))
7803 return error_mark_node;
7805 pfn_type = TREE_TYPE (pfn);
7806 to_type = build_ptrmemfunc_type (type);
7808 /* Handle multiple conversions of pointer to member functions. */
7809 if (TYPE_PTRMEMFUNC_P (pfn_type))
7811 tree delta = NULL_TREE;
7812 tree npfn = NULL_TREE;
7813 tree n;
7815 if (!force
7816 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7817 LOOKUP_NORMAL, complain))
7819 if (complain & tf_error)
7820 error ("invalid conversion to type %qT from type %qT",
7821 to_type, pfn_type);
7822 else
7823 return error_mark_node;
7826 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7827 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7828 force,
7829 c_cast_p, complain);
7830 if (n == error_mark_node)
7831 return error_mark_node;
7833 /* We don't have to do any conversion to convert a
7834 pointer-to-member to its own type. But, we don't want to
7835 just return a PTRMEM_CST if there's an explicit cast; that
7836 cast should make the expression an invalid template argument. */
7837 if (TREE_CODE (pfn) != PTRMEM_CST)
7839 if (same_type_p (to_type, pfn_type))
7840 return pfn;
7841 else if (integer_zerop (n))
7842 return build_reinterpret_cast (to_type, pfn,
7843 complain);
7846 if (TREE_SIDE_EFFECTS (pfn))
7847 pfn = save_expr (pfn);
7849 /* Obtain the function pointer and the current DELTA. */
7850 if (TREE_CODE (pfn) == PTRMEM_CST)
7851 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7852 else
7854 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7855 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7858 /* Just adjust the DELTA field. */
7859 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7860 (TREE_TYPE (delta), ptrdiff_type_node));
7861 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7862 n = cp_build_binary_op (input_location,
7863 LSHIFT_EXPR, n, integer_one_node,
7864 complain);
7865 delta = cp_build_binary_op (input_location,
7866 PLUS_EXPR, delta, n, complain);
7867 return build_ptrmemfunc1 (to_type, delta, npfn);
7870 /* Handle null pointer to member function conversions. */
7871 if (null_ptr_cst_p (pfn))
7873 pfn = cp_build_c_cast (type, pfn, complain);
7874 return build_ptrmemfunc1 (to_type,
7875 integer_zero_node,
7876 pfn);
7879 if (type_unknown_p (pfn))
7880 return instantiate_type (type, pfn, complain);
7882 fn = TREE_OPERAND (pfn, 0);
7883 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7884 /* In a template, we will have preserved the
7885 OFFSET_REF. */
7886 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7887 return make_ptrmem_cst (to_type, fn);
7890 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7891 given by CST.
7893 ??? There is no consistency as to the types returned for the above
7894 values. Some code acts as if it were a sizetype and some as if it were
7895 integer_type_node. */
7897 void
7898 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7900 tree type = TREE_TYPE (cst);
7901 tree fn = PTRMEM_CST_MEMBER (cst);
7902 tree ptr_class, fn_class;
7904 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7906 /* The class that the function belongs to. */
7907 fn_class = DECL_CONTEXT (fn);
7909 /* The class that we're creating a pointer to member of. */
7910 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7912 /* First, calculate the adjustment to the function's class. */
7913 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7914 /*c_cast_p=*/0, tf_warning_or_error);
7916 if (!DECL_VIRTUAL_P (fn))
7917 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7918 build_addr_func (fn, tf_warning_or_error));
7919 else
7921 /* If we're dealing with a virtual function, we have to adjust 'this'
7922 again, to point to the base which provides the vtable entry for
7923 fn; the call will do the opposite adjustment. */
7924 tree orig_class = DECL_CONTEXT (fn);
7925 tree binfo = binfo_or_else (orig_class, fn_class);
7926 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7927 *delta, BINFO_OFFSET (binfo));
7928 *delta = fold_if_not_in_template (*delta);
7930 /* We set PFN to the vtable offset at which the function can be
7931 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7932 case delta is shifted left, and then incremented). */
7933 *pfn = DECL_VINDEX (fn);
7934 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7935 TYPE_SIZE_UNIT (vtable_entry_type));
7936 *pfn = fold_if_not_in_template (*pfn);
7938 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7940 case ptrmemfunc_vbit_in_pfn:
7941 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7942 integer_one_node);
7943 *pfn = fold_if_not_in_template (*pfn);
7944 break;
7946 case ptrmemfunc_vbit_in_delta:
7947 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7948 *delta, integer_one_node);
7949 *delta = fold_if_not_in_template (*delta);
7950 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7951 *delta, integer_one_node);
7952 *delta = fold_if_not_in_template (*delta);
7953 break;
7955 default:
7956 gcc_unreachable ();
7959 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7960 *pfn = fold_if_not_in_template (*pfn);
7964 /* Return an expression for PFN from the pointer-to-member function
7965 given by T. */
7967 static tree
7968 pfn_from_ptrmemfunc (tree t)
7970 if (TREE_CODE (t) == PTRMEM_CST)
7972 tree delta;
7973 tree pfn;
7975 expand_ptrmemfunc_cst (t, &delta, &pfn);
7976 if (pfn)
7977 return pfn;
7980 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7983 /* Return an expression for DELTA from the pointer-to-member function
7984 given by T. */
7986 static tree
7987 delta_from_ptrmemfunc (tree t)
7989 if (TREE_CODE (t) == PTRMEM_CST)
7991 tree delta;
7992 tree pfn;
7994 expand_ptrmemfunc_cst (t, &delta, &pfn);
7995 if (delta)
7996 return delta;
7999 return build_ptrmemfunc_access_expr (t, delta_identifier);
8002 /* Convert value RHS to type TYPE as preparation for an assignment to
8003 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8004 implicit conversion is. If FNDECL is non-NULL, we are doing the
8005 conversion in order to pass the PARMNUMth argument of FNDECL.
8006 If FNDECL is NULL, we are doing the conversion in function pointer
8007 argument passing, conversion in initialization, etc. */
8009 static tree
8010 convert_for_assignment (tree type, tree rhs,
8011 impl_conv_rhs errtype, tree fndecl, int parmnum,
8012 tsubst_flags_t complain, int flags)
8014 tree rhstype;
8015 enum tree_code coder;
8017 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8018 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8019 rhs = TREE_OPERAND (rhs, 0);
8021 rhstype = TREE_TYPE (rhs);
8022 coder = TREE_CODE (rhstype);
8024 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8025 && vector_types_convertible_p (type, rhstype, true))
8027 rhs = mark_rvalue_use (rhs);
8028 return convert (type, rhs);
8031 if (rhs == error_mark_node || rhstype == error_mark_node)
8032 return error_mark_node;
8033 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8034 return error_mark_node;
8036 /* The RHS of an assignment cannot have void type. */
8037 if (coder == VOID_TYPE)
8039 if (complain & tf_error)
8040 error ("void value not ignored as it ought to be");
8041 return error_mark_node;
8044 if (c_dialect_objc ())
8046 int parmno;
8047 tree selector;
8048 tree rname = fndecl;
8050 switch (errtype)
8052 case ICR_ASSIGN:
8053 parmno = -1;
8054 break;
8055 case ICR_INIT:
8056 parmno = -2;
8057 break;
8058 default:
8059 selector = objc_message_selector ();
8060 parmno = parmnum;
8061 if (selector && parmno > 1)
8063 rname = selector;
8064 parmno -= 1;
8068 if (objc_compare_types (type, rhstype, parmno, rname))
8070 rhs = mark_rvalue_use (rhs);
8071 return convert (type, rhs);
8075 /* [expr.ass]
8077 The expression is implicitly converted (clause _conv_) to the
8078 cv-unqualified type of the left operand.
8080 We allow bad conversions here because by the time we get to this point
8081 we are committed to doing the conversion. If we end up doing a bad
8082 conversion, convert_like will complain. */
8083 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8085 /* When -Wno-pmf-conversions is use, we just silently allow
8086 conversions from pointers-to-members to plain pointers. If
8087 the conversion doesn't work, cp_convert will complain. */
8088 if (!warn_pmf2ptr
8089 && TYPE_PTR_P (type)
8090 && TYPE_PTRMEMFUNC_P (rhstype))
8091 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8092 else
8094 if (complain & tf_error)
8096 /* If the right-hand side has unknown type, then it is an
8097 overloaded function. Call instantiate_type to get error
8098 messages. */
8099 if (rhstype == unknown_type_node)
8100 instantiate_type (type, rhs, tf_warning_or_error);
8101 else if (fndecl)
8102 error ("cannot convert %qT to %qT for argument %qP to %qD",
8103 rhstype, type, parmnum, fndecl);
8104 else
8105 switch (errtype)
8107 case ICR_DEFAULT_ARGUMENT:
8108 error ("cannot convert %qT to %qT in default argument",
8109 rhstype, type);
8110 break;
8111 case ICR_ARGPASS:
8112 error ("cannot convert %qT to %qT in argument passing",
8113 rhstype, type);
8114 break;
8115 case ICR_CONVERTING:
8116 error ("cannot convert %qT to %qT",
8117 rhstype, type);
8118 break;
8119 case ICR_INIT:
8120 error ("cannot convert %qT to %qT in initialization",
8121 rhstype, type);
8122 break;
8123 case ICR_RETURN:
8124 error ("cannot convert %qT to %qT in return",
8125 rhstype, type);
8126 break;
8127 case ICR_ASSIGN:
8128 error ("cannot convert %qT to %qT in assignment",
8129 rhstype, type);
8130 break;
8131 default:
8132 gcc_unreachable();
8134 if (TYPE_PTR_P (rhstype)
8135 && TYPE_PTR_P (type)
8136 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8137 && CLASS_TYPE_P (TREE_TYPE (type))
8138 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8139 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8140 (TREE_TYPE (rhstype))),
8141 "class type %qT is incomplete", TREE_TYPE (rhstype));
8143 return error_mark_node;
8146 if (warn_suggest_attribute_format)
8148 const enum tree_code codel = TREE_CODE (type);
8149 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8150 && coder == codel
8151 && check_missing_format_attribute (type, rhstype)
8152 && (complain & tf_warning))
8153 switch (errtype)
8155 case ICR_ARGPASS:
8156 case ICR_DEFAULT_ARGUMENT:
8157 if (fndecl)
8158 warning (OPT_Wsuggest_attribute_format,
8159 "parameter %qP of %qD might be a candidate "
8160 "for a format attribute", parmnum, fndecl);
8161 else
8162 warning (OPT_Wsuggest_attribute_format,
8163 "parameter might be a candidate "
8164 "for a format attribute");
8165 break;
8166 case ICR_CONVERTING:
8167 warning (OPT_Wsuggest_attribute_format,
8168 "target of conversion might be a candidate "
8169 "for a format attribute");
8170 break;
8171 case ICR_INIT:
8172 warning (OPT_Wsuggest_attribute_format,
8173 "target of initialization might be a candidate "
8174 "for a format attribute");
8175 break;
8176 case ICR_RETURN:
8177 warning (OPT_Wsuggest_attribute_format,
8178 "return type might be a candidate "
8179 "for a format attribute");
8180 break;
8181 case ICR_ASSIGN:
8182 warning (OPT_Wsuggest_attribute_format,
8183 "left-hand side of assignment might be a candidate "
8184 "for a format attribute");
8185 break;
8186 default:
8187 gcc_unreachable();
8191 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8192 does not. */
8193 if (warn_parentheses
8194 && TREE_CODE (type) == BOOLEAN_TYPE
8195 && TREE_CODE (rhs) == MODIFY_EXPR
8196 && !TREE_NO_WARNING (rhs)
8197 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8198 && (complain & tf_warning))
8200 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8202 warning_at (loc, OPT_Wparentheses,
8203 "suggest parentheses around assignment used as truth value");
8204 TREE_NO_WARNING (rhs) = 1;
8207 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8208 complain, flags);
8211 /* Convert RHS to be of type TYPE.
8212 If EXP is nonzero, it is the target of the initialization.
8213 ERRTYPE indicates what kind of error the implicit conversion is.
8215 Two major differences between the behavior of
8216 `convert_for_assignment' and `convert_for_initialization'
8217 are that references are bashed in the former, while
8218 copied in the latter, and aggregates are assigned in
8219 the former (operator=) while initialized in the
8220 latter (X(X&)).
8222 If using constructor make sure no conversion operator exists, if one does
8223 exist, an ambiguity exists. */
8225 tree
8226 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8227 impl_conv_rhs errtype, tree fndecl, int parmnum,
8228 tsubst_flags_t complain)
8230 enum tree_code codel = TREE_CODE (type);
8231 tree rhstype;
8232 enum tree_code coder;
8234 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8235 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8236 if (TREE_CODE (rhs) == NOP_EXPR
8237 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8238 && codel != REFERENCE_TYPE)
8239 rhs = TREE_OPERAND (rhs, 0);
8241 if (type == error_mark_node
8242 || rhs == error_mark_node
8243 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8244 return error_mark_node;
8246 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8247 && TREE_CODE (type) != ARRAY_TYPE
8248 && (TREE_CODE (type) != REFERENCE_TYPE
8249 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8250 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8251 && !TYPE_REFFN_P (type))
8252 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8253 rhs = decay_conversion (rhs, complain);
8255 rhstype = TREE_TYPE (rhs);
8256 coder = TREE_CODE (rhstype);
8258 if (coder == ERROR_MARK)
8259 return error_mark_node;
8261 /* We accept references to incomplete types, so we can
8262 return here before checking if RHS is of complete type. */
8264 if (codel == REFERENCE_TYPE)
8266 /* This should eventually happen in convert_arguments. */
8267 int savew = 0, savee = 0;
8269 if (fndecl)
8270 savew = warningcount + werrorcount, savee = errorcount;
8271 rhs = initialize_reference (type, rhs, flags, complain);
8273 if (fndecl
8274 && (warningcount + werrorcount > savew || errorcount > savee))
8275 inform (input_location,
8276 "in passing argument %P of %q+D", parmnum, fndecl);
8278 return rhs;
8281 if (exp != 0)
8282 exp = require_complete_type_sfinae (exp, complain);
8283 if (exp == error_mark_node)
8284 return error_mark_node;
8286 rhstype = non_reference (rhstype);
8288 type = complete_type (type);
8290 if (DIRECT_INIT_EXPR_P (type, rhs))
8291 /* Don't try to do copy-initialization if we already have
8292 direct-initialization. */
8293 return rhs;
8295 if (MAYBE_CLASS_TYPE_P (type))
8296 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8298 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8299 complain, flags);
8302 /* If RETVAL is the address of, or a reference to, a local variable or
8303 temporary give an appropriate warning and return true. */
8305 static bool
8306 maybe_warn_about_returning_address_of_local (tree retval)
8308 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8309 tree whats_returned = retval;
8311 for (;;)
8313 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8314 whats_returned = TREE_OPERAND (whats_returned, 1);
8315 else if (CONVERT_EXPR_P (whats_returned)
8316 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8317 whats_returned = TREE_OPERAND (whats_returned, 0);
8318 else
8319 break;
8322 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8323 return false;
8324 whats_returned = TREE_OPERAND (whats_returned, 0);
8326 while (TREE_CODE (whats_returned) == COMPONENT_REF
8327 || TREE_CODE (whats_returned) == ARRAY_REF)
8328 whats_returned = TREE_OPERAND (whats_returned, 0);
8330 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8332 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8333 || TREE_CODE (whats_returned) == TARGET_EXPR)
8335 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8336 return true;
8338 if (VAR_P (whats_returned)
8339 && DECL_NAME (whats_returned)
8340 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8342 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8343 return true;
8347 if (DECL_P (whats_returned)
8348 && DECL_NAME (whats_returned)
8349 && DECL_FUNCTION_SCOPE_P (whats_returned)
8350 && !is_capture_proxy (whats_returned)
8351 && !(TREE_STATIC (whats_returned)
8352 || TREE_PUBLIC (whats_returned)))
8354 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8355 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8356 whats_returned);
8357 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8358 warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
8359 whats_returned);
8360 else
8361 warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
8362 "returned", whats_returned);
8363 return true;
8366 return false;
8369 /* Check that returning RETVAL from the current function is valid.
8370 Return an expression explicitly showing all conversions required to
8371 change RETVAL into the function return type, and to assign it to
8372 the DECL_RESULT for the function. Set *NO_WARNING to true if
8373 code reaches end of non-void function warning shouldn't be issued
8374 on this RETURN_EXPR. */
8376 tree
8377 check_return_expr (tree retval, bool *no_warning)
8379 tree result;
8380 /* The type actually returned by the function. */
8381 tree valtype;
8382 /* The type the function is declared to return, or void if
8383 the declared type is incomplete. */
8384 tree functype;
8385 int fn_returns_value_p;
8386 bool named_return_value_okay_p;
8388 *no_warning = false;
8390 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8392 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8393 "statement is not allowed");
8394 return NULL_TREE;
8397 /* A `volatile' function is one that isn't supposed to return, ever.
8398 (This is a G++ extension, used to get better code for functions
8399 that call the `volatile' function.) */
8400 if (TREE_THIS_VOLATILE (current_function_decl))
8401 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8403 /* Check for various simple errors. */
8404 if (DECL_DESTRUCTOR_P (current_function_decl))
8406 if (retval)
8407 error ("returning a value from a destructor");
8408 return NULL_TREE;
8410 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8412 if (in_function_try_handler)
8413 /* If a return statement appears in a handler of the
8414 function-try-block of a constructor, the program is ill-formed. */
8415 error ("cannot return from a handler of a function-try-block of a constructor");
8416 else if (retval)
8417 /* You can't return a value from a constructor. */
8418 error ("returning a value from a constructor");
8419 return NULL_TREE;
8422 if (processing_template_decl)
8424 current_function_returns_value = 1;
8425 if (check_for_bare_parameter_packs (retval))
8426 retval = error_mark_node;
8427 return retval;
8430 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8432 /* Deduce auto return type from a return statement. */
8433 if (current_function_auto_return_pattern)
8435 tree auto_node;
8436 tree type;
8438 if (!retval && !is_auto (current_function_auto_return_pattern))
8440 /* Give a helpful error message. */
8441 error ("return-statement with no value, in function returning %qT",
8442 current_function_auto_return_pattern);
8443 inform (input_location, "only plain %<auto%> return type can be "
8444 "deduced to %<void%>");
8445 type = error_mark_node;
8447 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8449 error ("returning initializer list");
8450 type = error_mark_node;
8452 else
8454 if (!retval)
8455 retval = void_node;
8456 auto_node = type_uses_auto (current_function_auto_return_pattern);
8457 type = do_auto_deduction (current_function_auto_return_pattern,
8458 retval, auto_node);
8461 if (type == error_mark_node)
8462 /* Leave it. */;
8463 else if (functype == current_function_auto_return_pattern)
8464 apply_deduced_return_type (current_function_decl, type);
8465 else
8466 /* A mismatch should have been diagnosed in do_auto_deduction. */
8467 gcc_assert (same_type_p (type, functype));
8468 functype = type;
8471 /* When no explicit return-value is given in a function with a named
8472 return value, the named return value is used. */
8473 result = DECL_RESULT (current_function_decl);
8474 valtype = TREE_TYPE (result);
8475 gcc_assert (valtype != NULL_TREE);
8476 fn_returns_value_p = !VOID_TYPE_P (valtype);
8477 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8478 retval = result;
8480 /* Check for a return statement with no return value in a function
8481 that's supposed to return a value. */
8482 if (!retval && fn_returns_value_p)
8484 if (functype != error_mark_node)
8485 permerror (input_location, "return-statement with no value, in "
8486 "function returning %qT", valtype);
8487 /* Remember that this function did return. */
8488 current_function_returns_value = 1;
8489 /* And signal caller that TREE_NO_WARNING should be set on the
8490 RETURN_EXPR to avoid control reaches end of non-void function
8491 warnings in tree-cfg.c. */
8492 *no_warning = true;
8494 /* Check for a return statement with a value in a function that
8495 isn't supposed to return a value. */
8496 else if (retval && !fn_returns_value_p)
8498 if (VOID_TYPE_P (TREE_TYPE (retval)))
8499 /* You can return a `void' value from a function of `void'
8500 type. In that case, we have to evaluate the expression for
8501 its side-effects. */
8502 finish_expr_stmt (retval);
8503 else
8504 permerror (input_location, "return-statement with a value, in function "
8505 "returning 'void'");
8506 current_function_returns_null = 1;
8508 /* There's really no value to return, after all. */
8509 return NULL_TREE;
8511 else if (!retval)
8512 /* Remember that this function can sometimes return without a
8513 value. */
8514 current_function_returns_null = 1;
8515 else
8516 /* Remember that this function did return a value. */
8517 current_function_returns_value = 1;
8519 /* Check for erroneous operands -- but after giving ourselves a
8520 chance to provide an error about returning a value from a void
8521 function. */
8522 if (error_operand_p (retval))
8524 current_function_return_value = error_mark_node;
8525 return error_mark_node;
8528 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8529 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8530 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8531 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8532 && ! flag_check_new
8533 && retval && null_ptr_cst_p (retval))
8534 warning (0, "%<operator new%> must not return NULL unless it is "
8535 "declared %<throw()%> (or -fcheck-new is in effect)");
8537 /* Effective C++ rule 15. See also start_function. */
8538 if (warn_ecpp
8539 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8541 bool warn = true;
8543 /* The function return type must be a reference to the current
8544 class. */
8545 if (TREE_CODE (valtype) == REFERENCE_TYPE
8546 && same_type_ignoring_top_level_qualifiers_p
8547 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8549 /* Returning '*this' is obviously OK. */
8550 if (retval == current_class_ref)
8551 warn = false;
8552 /* If we are calling a function whose return type is the same of
8553 the current class reference, it is ok. */
8554 else if (INDIRECT_REF_P (retval)
8555 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8556 warn = false;
8559 if (warn)
8560 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8563 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8565 [...] For a function with a class return type, if the expression
8566 in the return statement is the name of a local object, and the cv-
8567 unqualified type of the local object is the same as the function
8568 return type, an implementation is permitted to omit creating the tem-
8569 porary object to hold the function return value [...]
8571 So, if this is a value-returning function that always returns the same
8572 local variable, remember it.
8574 It might be nice to be more flexible, and choose the first suitable
8575 variable even if the function sometimes returns something else, but
8576 then we run the risk of clobbering the variable we chose if the other
8577 returned expression uses the chosen variable somehow. And people expect
8578 this restriction, anyway. (jason 2000-11-19)
8580 See finish_function and finalize_nrv for the rest of this optimization. */
8582 named_return_value_okay_p =
8583 (retval != NULL_TREE
8584 /* Must be a local, automatic variable. */
8585 && VAR_P (retval)
8586 && DECL_CONTEXT (retval) == current_function_decl
8587 && ! TREE_STATIC (retval)
8588 /* And not a lambda or anonymous union proxy. */
8589 && !DECL_HAS_VALUE_EXPR_P (retval)
8590 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8591 /* The cv-unqualified type of the returned value must be the
8592 same as the cv-unqualified return type of the
8593 function. */
8594 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8595 (TYPE_MAIN_VARIANT (functype)))
8596 /* And the returned value must be non-volatile. */
8597 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8599 if (fn_returns_value_p && flag_elide_constructors)
8601 if (named_return_value_okay_p
8602 && (current_function_return_value == NULL_TREE
8603 || current_function_return_value == retval))
8604 current_function_return_value = retval;
8605 else
8606 current_function_return_value = error_mark_node;
8609 /* We don't need to do any conversions when there's nothing being
8610 returned. */
8611 if (!retval)
8612 return NULL_TREE;
8614 /* Do any required conversions. */
8615 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8616 /* No conversions are required. */
8618 else
8620 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8622 /* The functype's return type will have been set to void, if it
8623 was an incomplete type. Just treat this as 'return;' */
8624 if (VOID_TYPE_P (functype))
8625 return error_mark_node;
8627 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8628 treated as an rvalue for the purposes of overload resolution to
8629 favor move constructors over copy constructors.
8631 Note that these conditions are similar to, but not as strict as,
8632 the conditions for the named return value optimization. */
8633 if ((cxx_dialect != cxx98)
8634 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8635 || TREE_CODE (retval) == PARM_DECL)
8636 && DECL_CONTEXT (retval) == current_function_decl
8637 && !TREE_STATIC (retval)
8638 /* This is only interesting for class type. */
8639 && CLASS_TYPE_P (functype))
8640 flags = flags | LOOKUP_PREFER_RVALUE;
8642 /* First convert the value to the function's return type, then
8643 to the type of return value's location to handle the
8644 case that functype is smaller than the valtype. */
8645 retval = convert_for_initialization
8646 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8647 tf_warning_or_error);
8648 retval = convert (valtype, retval);
8650 /* If the conversion failed, treat this just like `return;'. */
8651 if (retval == error_mark_node)
8652 return retval;
8653 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8654 else if (! cfun->returns_struct
8655 && TREE_CODE (retval) == TARGET_EXPR
8656 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8657 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8658 TREE_OPERAND (retval, 0));
8659 else if (maybe_warn_about_returning_address_of_local (retval))
8660 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8661 build_zero_cst (TREE_TYPE (retval)));
8664 /* Actually copy the value returned into the appropriate location. */
8665 if (retval && retval != result)
8666 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8668 return retval;
8672 /* Returns nonzero if the pointer-type FROM can be converted to the
8673 pointer-type TO via a qualification conversion. If CONSTP is -1,
8674 then we return nonzero if the pointers are similar, and the
8675 cv-qualification signature of FROM is a proper subset of that of TO.
8677 If CONSTP is positive, then all outer pointers have been
8678 const-qualified. */
8680 static int
8681 comp_ptr_ttypes_real (tree to, tree from, int constp)
8683 bool to_more_cv_qualified = false;
8684 bool is_opaque_pointer = false;
8686 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8688 if (TREE_CODE (to) != TREE_CODE (from))
8689 return 0;
8691 if (TREE_CODE (from) == OFFSET_TYPE
8692 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8693 TYPE_OFFSET_BASETYPE (to)))
8694 return 0;
8696 /* Const and volatile mean something different for function types,
8697 so the usual checks are not appropriate. */
8698 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8700 if (!at_least_as_qualified_p (to, from))
8701 return 0;
8703 if (!at_least_as_qualified_p (from, to))
8705 if (constp == 0)
8706 return 0;
8707 to_more_cv_qualified = true;
8710 if (constp > 0)
8711 constp &= TYPE_READONLY (to);
8714 if (TREE_CODE (to) == VECTOR_TYPE)
8715 is_opaque_pointer = vector_targets_convertible_p (to, from);
8717 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8718 return ((constp >= 0 || to_more_cv_qualified)
8719 && (is_opaque_pointer
8720 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8724 /* When comparing, say, char ** to char const **, this function takes
8725 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8726 types to this function. */
8729 comp_ptr_ttypes (tree to, tree from)
8731 return comp_ptr_ttypes_real (to, from, 1);
8734 /* Returns true iff FNTYPE is a non-class type that involves
8735 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8736 if a parameter type is ill-formed. */
8738 bool
8739 error_type_p (const_tree type)
8741 tree t;
8743 switch (TREE_CODE (type))
8745 case ERROR_MARK:
8746 return true;
8748 case POINTER_TYPE:
8749 case REFERENCE_TYPE:
8750 case OFFSET_TYPE:
8751 return error_type_p (TREE_TYPE (type));
8753 case FUNCTION_TYPE:
8754 case METHOD_TYPE:
8755 if (error_type_p (TREE_TYPE (type)))
8756 return true;
8757 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8758 if (error_type_p (TREE_VALUE (t)))
8759 return true;
8760 return false;
8762 case RECORD_TYPE:
8763 if (TYPE_PTRMEMFUNC_P (type))
8764 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8765 return false;
8767 default:
8768 return false;
8772 /* Returns true if to and from are (possibly multi-level) pointers to the same
8773 type or inheritance-related types, regardless of cv-quals. */
8775 bool
8776 ptr_reasonably_similar (const_tree to, const_tree from)
8778 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8780 /* Any target type is similar enough to void. */
8781 if (VOID_TYPE_P (to))
8782 return !error_type_p (from);
8783 if (VOID_TYPE_P (from))
8784 return !error_type_p (to);
8786 if (TREE_CODE (to) != TREE_CODE (from))
8787 return false;
8789 if (TREE_CODE (from) == OFFSET_TYPE
8790 && comptypes (TYPE_OFFSET_BASETYPE (to),
8791 TYPE_OFFSET_BASETYPE (from),
8792 COMPARE_BASE | COMPARE_DERIVED))
8793 continue;
8795 if (TREE_CODE (to) == VECTOR_TYPE
8796 && vector_types_convertible_p (to, from, false))
8797 return true;
8799 if (TREE_CODE (to) == INTEGER_TYPE
8800 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8801 return true;
8803 if (TREE_CODE (to) == FUNCTION_TYPE)
8804 return !error_type_p (to) && !error_type_p (from);
8806 if (!TYPE_PTR_P (to))
8808 /* When either type is incomplete avoid DERIVED_FROM_P,
8809 which may call complete_type (c++/57942). */
8810 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8811 return comptypes
8812 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8813 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8818 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8819 pointer-to-member types) are the same, ignoring cv-qualification at
8820 all levels. */
8822 bool
8823 comp_ptr_ttypes_const (tree to, tree from)
8825 bool is_opaque_pointer = false;
8827 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8829 if (TREE_CODE (to) != TREE_CODE (from))
8830 return false;
8832 if (TREE_CODE (from) == OFFSET_TYPE
8833 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8834 TYPE_OFFSET_BASETYPE (to)))
8835 continue;
8837 if (TREE_CODE (to) == VECTOR_TYPE)
8838 is_opaque_pointer = vector_targets_convertible_p (to, from);
8840 if (!TYPE_PTR_P (to))
8841 return (is_opaque_pointer
8842 || same_type_ignoring_top_level_qualifiers_p (to, from));
8846 /* Returns the type qualifiers for this type, including the qualifiers on the
8847 elements for an array type. */
8850 cp_type_quals (const_tree type)
8852 int quals;
8853 /* This CONST_CAST is okay because strip_array_types returns its
8854 argument unmodified and we assign it to a const_tree. */
8855 type = strip_array_types (CONST_CAST_TREE (type));
8856 if (type == error_mark_node
8857 /* Quals on a FUNCTION_TYPE are memfn quals. */
8858 || TREE_CODE (type) == FUNCTION_TYPE)
8859 return TYPE_UNQUALIFIED;
8860 quals = TYPE_QUALS (type);
8861 /* METHOD and REFERENCE_TYPEs should never have quals. */
8862 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8863 && TREE_CODE (type) != REFERENCE_TYPE)
8864 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8865 == TYPE_UNQUALIFIED));
8866 return quals;
8869 /* Returns the function-ref-qualifier for TYPE */
8871 cp_ref_qualifier
8872 type_memfn_rqual (const_tree type)
8874 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8875 || TREE_CODE (type) == METHOD_TYPE);
8877 if (!FUNCTION_REF_QUALIFIED (type))
8878 return REF_QUAL_NONE;
8879 else if (FUNCTION_RVALUE_QUALIFIED (type))
8880 return REF_QUAL_RVALUE;
8881 else
8882 return REF_QUAL_LVALUE;
8885 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8886 METHOD_TYPE. */
8889 type_memfn_quals (const_tree type)
8891 if (TREE_CODE (type) == FUNCTION_TYPE)
8892 return TYPE_QUALS (type);
8893 else if (TREE_CODE (type) == METHOD_TYPE)
8894 return cp_type_quals (class_of_this_parm (type));
8895 else
8896 gcc_unreachable ();
8899 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8900 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8902 tree
8903 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8905 /* Could handle METHOD_TYPE here if necessary. */
8906 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8907 if (TYPE_QUALS (type) == memfn_quals
8908 && type_memfn_rqual (type) == rqual)
8909 return type;
8911 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8912 complex. */
8913 tree result = build_qualified_type (type, memfn_quals);
8914 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8915 return build_ref_qualified_type (result, rqual);
8918 /* Returns nonzero if TYPE is const or volatile. */
8920 bool
8921 cv_qualified_p (const_tree type)
8923 int quals = cp_type_quals (type);
8924 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8927 /* Returns nonzero if the TYPE contains a mutable member. */
8929 bool
8930 cp_has_mutable_p (const_tree type)
8932 /* This CONST_CAST is okay because strip_array_types returns its
8933 argument unmodified and we assign it to a const_tree. */
8934 type = strip_array_types (CONST_CAST_TREE(type));
8936 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8939 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8940 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8941 approximation. In particular, consider:
8943 int f();
8944 struct S { int i; };
8945 const S s = { f(); }
8947 Here, we will make "s" as TREE_READONLY (because it is declared
8948 "const") -- only to reverse ourselves upon seeing that the
8949 initializer is non-constant. */
8951 void
8952 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8954 tree type = TREE_TYPE (decl);
8956 if (type == error_mark_node)
8957 return;
8959 if (TREE_CODE (decl) == TYPE_DECL)
8960 return;
8962 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8963 && type_quals != TYPE_UNQUALIFIED));
8965 /* Avoid setting TREE_READONLY incorrectly. */
8966 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8967 constructor can produce constant init, so rely on cp_finish_decl to
8968 clear TREE_READONLY if the variable has non-constant init. */
8970 /* If the type has (or might have) a mutable component, that component
8971 might be modified. */
8972 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8973 type_quals &= ~TYPE_QUAL_CONST;
8975 c_apply_type_quals_to_decl (type_quals, decl);
8978 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8979 exemplar types such that casting T1 to T2 is casting away constness
8980 if and only if there is no implicit conversion from T1 to T2. */
8982 static void
8983 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8985 int quals1;
8986 int quals2;
8988 /* [expr.const.cast]
8990 For multi-level pointer to members and multi-level mixed pointers
8991 and pointers to members (conv.qual), the "member" aspect of a
8992 pointer to member level is ignored when determining if a const
8993 cv-qualifier has been cast away. */
8994 /* [expr.const.cast]
8996 For two pointer types:
8998 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8999 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9000 K is min(N,M)
9002 casting from X1 to X2 casts away constness if, for a non-pointer
9003 type T there does not exist an implicit conversion (clause
9004 _conv_) from:
9006 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9010 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9011 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9012 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9014 *t1 = cp_build_qualified_type (void_type_node,
9015 cp_type_quals (*t1));
9016 *t2 = cp_build_qualified_type (void_type_node,
9017 cp_type_quals (*t2));
9018 return;
9021 quals1 = cp_type_quals (*t1);
9022 quals2 = cp_type_quals (*t2);
9024 if (TYPE_PTRDATAMEM_P (*t1))
9025 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9026 else
9027 *t1 = TREE_TYPE (*t1);
9028 if (TYPE_PTRDATAMEM_P (*t2))
9029 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9030 else
9031 *t2 = TREE_TYPE (*t2);
9033 casts_away_constness_r (t1, t2, complain);
9034 *t1 = build_pointer_type (*t1);
9035 *t2 = build_pointer_type (*t2);
9036 *t1 = cp_build_qualified_type (*t1, quals1);
9037 *t2 = cp_build_qualified_type (*t2, quals2);
9040 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9041 constness.
9043 ??? This function returns non-zero if casting away qualifiers not
9044 just const. We would like to return to the caller exactly which
9045 qualifiers are casted away to give more accurate diagnostics.
9048 static bool
9049 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9051 if (TREE_CODE (t2) == REFERENCE_TYPE)
9053 /* [expr.const.cast]
9055 Casting from an lvalue of type T1 to an lvalue of type T2
9056 using a reference cast casts away constness if a cast from an
9057 rvalue of type "pointer to T1" to the type "pointer to T2"
9058 casts away constness. */
9059 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9060 return casts_away_constness (build_pointer_type (t1),
9061 build_pointer_type (TREE_TYPE (t2)),
9062 complain);
9065 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9066 /* [expr.const.cast]
9068 Casting from an rvalue of type "pointer to data member of X
9069 of type T1" to the type "pointer to data member of Y of type
9070 T2" casts away constness if a cast from an rvalue of type
9071 "pointer to T1" to the type "pointer to T2" casts away
9072 constness. */
9073 return casts_away_constness
9074 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9075 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9076 complain);
9078 /* Casting away constness is only something that makes sense for
9079 pointer or reference types. */
9080 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9081 return false;
9083 /* Top-level qualifiers don't matter. */
9084 t1 = TYPE_MAIN_VARIANT (t1);
9085 t2 = TYPE_MAIN_VARIANT (t2);
9086 casts_away_constness_r (&t1, &t2, complain);
9087 if (!can_convert (t2, t1, complain))
9088 return true;
9090 return false;
9093 /* If T is a REFERENCE_TYPE return the type to which T refers.
9094 Otherwise, return T itself. */
9096 tree
9097 non_reference (tree t)
9099 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9100 t = TREE_TYPE (t);
9101 return t;
9105 /* Return nonzero if REF is an lvalue valid for this language;
9106 otherwise, print an error message and return zero. USE says
9107 how the lvalue is being used and so selects the error message. */
9110 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9112 cp_lvalue_kind kind = lvalue_kind (ref);
9114 if (kind == clk_none)
9116 if (complain & tf_error)
9117 lvalue_error (input_location, use);
9118 return 0;
9120 else if (kind & (clk_rvalueref|clk_class))
9122 if (!(complain & tf_error))
9123 return 0;
9124 if (kind & clk_class)
9125 /* Make this a permerror because we used to accept it. */
9126 permerror (input_location, "using temporary as lvalue");
9127 else
9128 error ("using xvalue (rvalue reference) as lvalue");
9130 return 1;
9133 /* Return true if a user-defined literal operator is a raw operator. */
9135 bool
9136 check_raw_literal_operator (const_tree decl)
9138 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9139 tree argtype;
9140 int arity;
9141 bool maybe_raw_p = false;
9143 /* Count the number and type of arguments and check for ellipsis. */
9144 for (argtype = argtypes, arity = 0;
9145 argtype && argtype != void_list_node;
9146 ++arity, argtype = TREE_CHAIN (argtype))
9148 tree t = TREE_VALUE (argtype);
9150 if (same_type_p (t, const_string_type_node))
9151 maybe_raw_p = true;
9153 if (!argtype)
9154 return false; /* Found ellipsis. */
9156 if (!maybe_raw_p || arity != 1)
9157 return false;
9159 return true;
9163 /* Return true if a user-defined literal operator has one of the allowed
9164 argument types. */
9166 bool
9167 check_literal_operator_args (const_tree decl,
9168 bool *long_long_unsigned_p, bool *long_double_p)
9170 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9172 *long_long_unsigned_p = false;
9173 *long_double_p = false;
9174 if (processing_template_decl || processing_specialization)
9175 return argtypes == void_list_node;
9176 else
9178 tree argtype;
9179 int arity;
9180 int max_arity = 2;
9182 /* Count the number and type of arguments and check for ellipsis. */
9183 for (argtype = argtypes, arity = 0;
9184 argtype && argtype != void_list_node;
9185 argtype = TREE_CHAIN (argtype))
9187 tree t = TREE_VALUE (argtype);
9188 ++arity;
9190 if (TYPE_PTR_P (t))
9192 bool maybe_raw_p = false;
9193 t = TREE_TYPE (t);
9194 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9195 return false;
9196 t = TYPE_MAIN_VARIANT (t);
9197 if ((maybe_raw_p = same_type_p (t, char_type_node))
9198 || same_type_p (t, wchar_type_node)
9199 || same_type_p (t, char16_type_node)
9200 || same_type_p (t, char32_type_node))
9202 argtype = TREE_CHAIN (argtype);
9203 if (!argtype)
9204 return false;
9205 t = TREE_VALUE (argtype);
9206 if (maybe_raw_p && argtype == void_list_node)
9207 return true;
9208 else if (same_type_p (t, size_type_node))
9210 ++arity;
9211 continue;
9213 else
9214 return false;
9217 else if (same_type_p (t, long_long_unsigned_type_node))
9219 max_arity = 1;
9220 *long_long_unsigned_p = true;
9222 else if (same_type_p (t, long_double_type_node))
9224 max_arity = 1;
9225 *long_double_p = true;
9227 else if (same_type_p (t, char_type_node))
9228 max_arity = 1;
9229 else if (same_type_p (t, wchar_type_node))
9230 max_arity = 1;
9231 else if (same_type_p (t, char16_type_node))
9232 max_arity = 1;
9233 else if (same_type_p (t, char32_type_node))
9234 max_arity = 1;
9235 else
9236 return false;
9238 if (!argtype)
9239 return false; /* Found ellipsis. */
9241 if (arity != max_arity)
9242 return false;
9244 return true;