gcc/ChangeLog
[official-gcc.git] / gcc / cp / typeck.c
blob388558c347dd2b3bba00ae0634935655684c80ee
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2015 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 "alias.h"
32 #include "tree.h"
33 #include "fold-const.h"
34 #include "stor-layout.h"
35 #include "varasm.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "diagnostic.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "convert.h"
42 #include "c-family/c-common.h"
43 #include "c-family/c-objc.h"
44 #include "c-family/c-ubsan.h"
45 #include "params.h"
47 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
48 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
49 static tree pfn_from_ptrmemfunc (tree);
50 static tree delta_from_ptrmemfunc (tree);
51 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
52 tsubst_flags_t, int);
53 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
54 static tree rationalize_conditional_expr (enum tree_code, tree,
55 tsubst_flags_t);
56 static int comp_ptr_ttypes_real (tree, tree, int);
57 static bool comp_except_types (tree, tree, bool);
58 static bool comp_array_types (const_tree, const_tree, bool);
59 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
60 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
61 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
62 static bool casts_away_constness (tree, tree, tsubst_flags_t);
63 static bool maybe_warn_about_returning_address_of_local (tree);
64 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
65 static void error_args_num (location_t, tree, bool);
66 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
67 tsubst_flags_t);
69 /* Do `exp = require_complete_type (exp);' to make sure exp
70 does not have an incomplete type. (That includes void types.)
71 Returns error_mark_node if the VALUE does not have
72 complete type when this function returns. */
74 tree
75 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
77 tree type;
79 if (processing_template_decl || value == error_mark_node)
80 return value;
82 if (TREE_CODE (value) == OVERLOAD)
83 type = unknown_type_node;
84 else
85 type = TREE_TYPE (value);
87 if (type == error_mark_node)
88 return error_mark_node;
90 /* First, detect a valid value with a complete type. */
91 if (COMPLETE_TYPE_P (type))
92 return value;
94 if (complete_type_or_maybe_complain (type, value, complain))
95 return value;
96 else
97 return error_mark_node;
100 tree
101 require_complete_type (tree value)
103 return require_complete_type_sfinae (value, tf_warning_or_error);
106 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
107 a template instantiation, do the instantiation. Returns TYPE,
108 whether or not it could be completed, unless something goes
109 horribly wrong, in which case the error_mark_node is returned. */
111 tree
112 complete_type (tree type)
114 if (type == NULL_TREE)
115 /* Rather than crash, we return something sure to cause an error
116 at some point. */
117 return error_mark_node;
119 if (type == error_mark_node || COMPLETE_TYPE_P (type))
121 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
123 tree t = complete_type (TREE_TYPE (type));
124 unsigned int needs_constructing, has_nontrivial_dtor;
125 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
126 layout_type (type);
127 needs_constructing
128 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
129 has_nontrivial_dtor
130 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
131 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
133 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
134 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
137 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 instantiate_class_template (TYPE_MAIN_VARIANT (type));
140 return type;
143 /* Like complete_type, but issue an error if the TYPE cannot be completed.
144 VALUE is used for informative diagnostics.
145 Returns NULL_TREE if the type cannot be made complete. */
147 tree
148 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 type = complete_type (type);
151 if (type == error_mark_node)
152 /* We already issued an error. */
153 return NULL_TREE;
154 else if (!COMPLETE_TYPE_P (type))
156 if (complain & tf_error)
157 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
158 return NULL_TREE;
160 else
161 return type;
164 tree
165 complete_type_or_else (tree type, tree value)
167 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
170 /* Return truthvalue of whether type of EXP is instantiated. */
173 type_unknown_p (const_tree exp)
175 return (TREE_CODE (exp) == TREE_LIST
176 || TREE_TYPE (exp) == unknown_type_node);
180 /* Return the common type of two parameter lists.
181 We assume that comptypes has already been done and returned 1;
182 if that isn't so, this may crash.
184 As an optimization, free the space we allocate if the parameter
185 lists are already common. */
187 static tree
188 commonparms (tree p1, tree p2)
190 tree oldargs = p1, newargs, n;
191 int i, len;
192 int any_change = 0;
194 len = list_length (p1);
195 newargs = tree_last (p1);
197 if (newargs == void_list_node)
198 i = 1;
199 else
201 i = 0;
202 newargs = 0;
205 for (; i < len; i++)
206 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
208 n = newargs;
210 for (i = 0; p1;
211 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
213 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
215 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
216 any_change = 1;
218 else if (! TREE_PURPOSE (p1))
220 if (TREE_PURPOSE (p2))
222 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
223 any_change = 1;
226 else
228 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
229 any_change = 1;
230 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
232 if (TREE_VALUE (p1) != TREE_VALUE (p2))
234 any_change = 1;
235 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
237 else
238 TREE_VALUE (n) = TREE_VALUE (p1);
240 if (! any_change)
241 return oldargs;
243 return newargs;
246 /* Given a type, perhaps copied for a typedef,
247 find the "original" version of it. */
248 static tree
249 original_type (tree t)
251 int quals = cp_type_quals (t);
252 while (t != error_mark_node
253 && TYPE_NAME (t) != NULL_TREE)
255 tree x = TYPE_NAME (t);
256 if (TREE_CODE (x) != TYPE_DECL)
257 break;
258 x = DECL_ORIGINAL_TYPE (x);
259 if (x == NULL_TREE)
260 break;
261 t = x;
263 return cp_build_qualified_type (t, quals);
266 /* Return the common type for two arithmetic types T1 and T2 under the
267 usual arithmetic conversions. The default conversions have already
268 been applied, and enumerated types converted to their compatible
269 integer types. */
271 static tree
272 cp_common_type (tree t1, tree t2)
274 enum tree_code code1 = TREE_CODE (t1);
275 enum tree_code code2 = TREE_CODE (t2);
276 tree attributes;
277 int i;
280 /* In what follows, we slightly generalize the rules given in [expr] so
281 as to deal with `long long' and `complex'. First, merge the
282 attributes. */
283 attributes = (*targetm.merge_type_attributes) (t1, t2);
285 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
287 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
288 return build_type_attribute_variant (t1, attributes);
289 else
290 return NULL_TREE;
293 /* FIXME: Attributes. */
294 gcc_assert (ARITHMETIC_TYPE_P (t1)
295 || VECTOR_TYPE_P (t1)
296 || UNSCOPED_ENUM_P (t1));
297 gcc_assert (ARITHMETIC_TYPE_P (t2)
298 || VECTOR_TYPE_P (t2)
299 || UNSCOPED_ENUM_P (t2));
301 /* If one type is complex, form the common type of the non-complex
302 components, then make that complex. Use T1 or T2 if it is the
303 required type. */
304 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
306 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
307 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
308 tree subtype
309 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
311 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
312 return build_type_attribute_variant (t1, attributes);
313 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
314 return build_type_attribute_variant (t2, attributes);
315 else
316 return build_type_attribute_variant (build_complex_type (subtype),
317 attributes);
320 if (code1 == VECTOR_TYPE)
322 /* When we get here we should have two vectors of the same size.
323 Just prefer the unsigned one if present. */
324 if (TYPE_UNSIGNED (t1))
325 return build_type_attribute_variant (t1, attributes);
326 else
327 return build_type_attribute_variant (t2, attributes);
330 /* If only one is real, use it as the result. */
331 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
332 return build_type_attribute_variant (t1, attributes);
333 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
334 return build_type_attribute_variant (t2, attributes);
336 /* Both real or both integers; use the one with greater precision. */
337 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
338 return build_type_attribute_variant (t1, attributes);
339 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
340 return build_type_attribute_variant (t2, attributes);
342 /* The types are the same; no need to do anything fancy. */
343 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
344 return build_type_attribute_variant (t1, attributes);
346 if (code1 != REAL_TYPE)
348 /* If one is unsigned long long, then convert the other to unsigned
349 long long. */
350 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
351 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
352 return build_type_attribute_variant (long_long_unsigned_type_node,
353 attributes);
354 /* If one is a long long, and the other is an unsigned long, and
355 long long can represent all the values of an unsigned long, then
356 convert to a long long. Otherwise, convert to an unsigned long
357 long. Otherwise, if either operand is long long, convert the
358 other to long long.
360 Since we're here, we know the TYPE_PRECISION is the same;
361 therefore converting to long long cannot represent all the values
362 of an unsigned long, so we choose unsigned long long in that
363 case. */
364 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
365 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
367 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
368 ? long_long_unsigned_type_node
369 : long_long_integer_type_node);
370 return build_type_attribute_variant (t, attributes);
373 /* Go through the same procedure, but for longs. */
374 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
375 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
376 return build_type_attribute_variant (long_unsigned_type_node,
377 attributes);
378 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
379 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
381 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
382 ? long_unsigned_type_node : long_integer_type_node);
383 return build_type_attribute_variant (t, attributes);
386 /* For __intN types, either the type is __int128 (and is lower
387 priority than the types checked above, but higher than other
388 128-bit types) or it's known to not be the same size as other
389 types (enforced in toplev.c). Prefer the unsigned type. */
390 for (i = 0; i < NUM_INT_N_ENTS; i ++)
392 if (int_n_enabled_p [i]
393 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
394 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
395 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
396 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
398 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
399 ? int_n_trees[i].unsigned_type
400 : int_n_trees[i].signed_type);
401 return build_type_attribute_variant (t, attributes);
405 /* Otherwise prefer the unsigned one. */
406 if (TYPE_UNSIGNED (t1))
407 return build_type_attribute_variant (t1, attributes);
408 else
409 return build_type_attribute_variant (t2, attributes);
411 else
413 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
414 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
415 return build_type_attribute_variant (long_double_type_node,
416 attributes);
417 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
418 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
419 return build_type_attribute_variant (double_type_node,
420 attributes);
421 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
422 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
423 return build_type_attribute_variant (float_type_node,
424 attributes);
426 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
427 the standard C++ floating-point types. Logic earlier in this
428 function has already eliminated the possibility that
429 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
430 compelling reason to choose one or the other. */
431 return build_type_attribute_variant (t1, attributes);
435 /* T1 and T2 are arithmetic or enumeration types. Return the type
436 that will result from the "usual arithmetic conversions" on T1 and
437 T2 as described in [expr]. */
439 tree
440 type_after_usual_arithmetic_conversions (tree t1, tree t2)
442 gcc_assert (ARITHMETIC_TYPE_P (t1)
443 || VECTOR_TYPE_P (t1)
444 || UNSCOPED_ENUM_P (t1));
445 gcc_assert (ARITHMETIC_TYPE_P (t2)
446 || VECTOR_TYPE_P (t2)
447 || UNSCOPED_ENUM_P (t2));
449 /* Perform the integral promotions. We do not promote real types here. */
450 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
451 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
453 t1 = type_promotes_to (t1);
454 t2 = type_promotes_to (t2);
457 return cp_common_type (t1, t2);
460 static void
461 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
462 composite_pointer_operation operation)
464 switch (operation)
466 case CPO_COMPARISON:
467 emit_diagnostic (kind, input_location, 0,
468 "comparison between "
469 "distinct pointer types %qT and %qT lacks a cast",
470 t1, t2);
471 break;
472 case CPO_CONVERSION:
473 emit_diagnostic (kind, input_location, 0,
474 "conversion between "
475 "distinct pointer types %qT and %qT lacks a cast",
476 t1, t2);
477 break;
478 case CPO_CONDITIONAL_EXPR:
479 emit_diagnostic (kind, input_location, 0,
480 "conditional expression between "
481 "distinct pointer types %qT and %qT lacks a cast",
482 t1, t2);
483 break;
484 default:
485 gcc_unreachable ();
489 /* Subroutine of composite_pointer_type to implement the recursive
490 case. See that function for documentation of the parameters. */
492 static tree
493 composite_pointer_type_r (tree t1, tree t2,
494 composite_pointer_operation operation,
495 tsubst_flags_t complain)
497 tree pointee1;
498 tree pointee2;
499 tree result_type;
500 tree attributes;
502 /* Determine the types pointed to by T1 and T2. */
503 if (TYPE_PTR_P (t1))
505 pointee1 = TREE_TYPE (t1);
506 pointee2 = TREE_TYPE (t2);
508 else
510 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
511 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
514 /* [expr.rel]
516 Otherwise, the composite pointer type is a pointer type
517 similar (_conv.qual_) to the type of one of the operands,
518 with a cv-qualification signature (_conv.qual_) that is the
519 union of the cv-qualification signatures of the operand
520 types. */
521 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
522 result_type = pointee1;
523 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
524 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
526 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
527 complain);
528 if (result_type == error_mark_node)
529 return error_mark_node;
531 else
533 if (complain & tf_error)
534 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
535 else
536 return error_mark_node;
537 result_type = void_type_node;
539 result_type = cp_build_qualified_type (result_type,
540 (cp_type_quals (pointee1)
541 | cp_type_quals (pointee2)));
542 /* If the original types were pointers to members, so is the
543 result. */
544 if (TYPE_PTRMEM_P (t1))
546 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
547 TYPE_PTRMEM_CLASS_TYPE (t2)))
549 if (complain & tf_error)
550 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
551 else
552 return error_mark_node;
554 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
555 result_type);
557 else
558 result_type = build_pointer_type (result_type);
560 /* Merge the attributes. */
561 attributes = (*targetm.merge_type_attributes) (t1, t2);
562 return build_type_attribute_variant (result_type, attributes);
565 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
566 ARG1 and ARG2 are the values with those types. The OPERATION is to
567 describe the operation between the pointer types,
568 in case an error occurs.
570 This routine also implements the computation of a common type for
571 pointers-to-members as per [expr.eq]. */
573 tree
574 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
575 composite_pointer_operation operation,
576 tsubst_flags_t complain)
578 tree class1;
579 tree class2;
581 /* [expr.rel]
583 If one operand is a null pointer constant, the composite pointer
584 type is the type of the other operand. */
585 if (null_ptr_cst_p (arg1))
586 return t2;
587 if (null_ptr_cst_p (arg2))
588 return t1;
590 /* We have:
592 [expr.rel]
594 If one of the operands has type "pointer to cv1 void*", then
595 the other has type "pointer to cv2T", and the composite pointer
596 type is "pointer to cv12 void", where cv12 is the union of cv1
597 and cv2.
599 If either type is a pointer to void, make sure it is T1. */
600 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
601 std::swap (t1, t2);
603 /* Now, if T1 is a pointer to void, merge the qualifiers. */
604 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
606 tree attributes;
607 tree result_type;
609 if (TYPE_PTRFN_P (t2))
611 if (complain & tf_error)
613 switch (operation)
615 case CPO_COMPARISON:
616 pedwarn (input_location, OPT_Wpedantic,
617 "ISO C++ forbids comparison between pointer "
618 "of type %<void *%> and pointer-to-function");
619 break;
620 case CPO_CONVERSION:
621 pedwarn (input_location, OPT_Wpedantic,
622 "ISO C++ forbids conversion between pointer "
623 "of type %<void *%> and pointer-to-function");
624 break;
625 case CPO_CONDITIONAL_EXPR:
626 pedwarn (input_location, OPT_Wpedantic,
627 "ISO C++ forbids conditional expression between "
628 "pointer of type %<void *%> and "
629 "pointer-to-function");
630 break;
631 default:
632 gcc_unreachable ();
635 else
636 return error_mark_node;
638 result_type
639 = cp_build_qualified_type (void_type_node,
640 (cp_type_quals (TREE_TYPE (t1))
641 | cp_type_quals (TREE_TYPE (t2))));
642 result_type = build_pointer_type (result_type);
643 /* Merge the attributes. */
644 attributes = (*targetm.merge_type_attributes) (t1, t2);
645 return build_type_attribute_variant (result_type, attributes);
648 if (c_dialect_objc () && TYPE_PTR_P (t1)
649 && TYPE_PTR_P (t2))
651 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
652 return objc_common_type (t1, t2);
655 /* [expr.eq] permits the application of a pointer conversion to
656 bring the pointers to a common type. */
657 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
658 && CLASS_TYPE_P (TREE_TYPE (t1))
659 && CLASS_TYPE_P (TREE_TYPE (t2))
660 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
661 TREE_TYPE (t2)))
663 class1 = TREE_TYPE (t1);
664 class2 = TREE_TYPE (t2);
666 if (DERIVED_FROM_P (class1, class2))
667 t2 = (build_pointer_type
668 (cp_build_qualified_type (class1, cp_type_quals (class2))));
669 else if (DERIVED_FROM_P (class2, class1))
670 t1 = (build_pointer_type
671 (cp_build_qualified_type (class2, cp_type_quals (class1))));
672 else
674 if (complain & tf_error)
675 composite_pointer_error (DK_ERROR, t1, t2, operation);
676 return error_mark_node;
679 /* [expr.eq] permits the application of a pointer-to-member
680 conversion to change the class type of one of the types. */
681 else if (TYPE_PTRMEM_P (t1)
682 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
683 TYPE_PTRMEM_CLASS_TYPE (t2)))
685 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
686 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
688 if (DERIVED_FROM_P (class1, class2))
689 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
690 else if (DERIVED_FROM_P (class2, class1))
691 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
692 else
694 if (complain & tf_error)
695 switch (operation)
697 case CPO_COMPARISON:
698 error ("comparison between distinct "
699 "pointer-to-member types %qT and %qT lacks a cast",
700 t1, t2);
701 break;
702 case CPO_CONVERSION:
703 error ("conversion between distinct "
704 "pointer-to-member types %qT and %qT lacks a cast",
705 t1, t2);
706 break;
707 case CPO_CONDITIONAL_EXPR:
708 error ("conditional expression between distinct "
709 "pointer-to-member types %qT and %qT lacks a cast",
710 t1, t2);
711 break;
712 default:
713 gcc_unreachable ();
715 return error_mark_node;
719 return composite_pointer_type_r (t1, t2, operation, complain);
722 /* Return the merged type of two types.
723 We assume that comptypes has already been done and returned 1;
724 if that isn't so, this may crash.
726 This just combines attributes and default arguments; any other
727 differences would cause the two types to compare unalike. */
729 tree
730 merge_types (tree t1, tree t2)
732 enum tree_code code1;
733 enum tree_code code2;
734 tree attributes;
736 /* Save time if the two types are the same. */
737 if (t1 == t2)
738 return t1;
739 if (original_type (t1) == original_type (t2))
740 return t1;
742 /* If one type is nonsense, use the other. */
743 if (t1 == error_mark_node)
744 return t2;
745 if (t2 == error_mark_node)
746 return t1;
748 /* Handle merging an auto redeclaration with a previous deduced
749 return type. */
750 if (is_auto (t1))
751 return t2;
753 /* Merge the attributes. */
754 attributes = (*targetm.merge_type_attributes) (t1, t2);
756 if (TYPE_PTRMEMFUNC_P (t1))
757 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
758 if (TYPE_PTRMEMFUNC_P (t2))
759 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
761 code1 = TREE_CODE (t1);
762 code2 = TREE_CODE (t2);
763 if (code1 != code2)
765 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
766 if (code1 == TYPENAME_TYPE)
768 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
769 code1 = TREE_CODE (t1);
771 else
773 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
774 code2 = TREE_CODE (t2);
778 switch (code1)
780 case POINTER_TYPE:
781 case REFERENCE_TYPE:
782 /* For two pointers, do this recursively on the target type. */
784 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
785 int quals = cp_type_quals (t1);
787 if (code1 == POINTER_TYPE)
789 t1 = build_pointer_type (target);
790 if (TREE_CODE (target) == METHOD_TYPE)
791 t1 = build_ptrmemfunc_type (t1);
793 else
794 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
795 t1 = build_type_attribute_variant (t1, attributes);
796 t1 = cp_build_qualified_type (t1, quals);
798 return t1;
801 case OFFSET_TYPE:
803 int quals;
804 tree pointee;
805 quals = cp_type_quals (t1);
806 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
807 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
808 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
809 pointee);
810 t1 = cp_build_qualified_type (t1, quals);
811 break;
814 case ARRAY_TYPE:
816 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
817 /* Save space: see if the result is identical to one of the args. */
818 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
819 return build_type_attribute_variant (t1, attributes);
820 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
821 return build_type_attribute_variant (t2, attributes);
822 /* Merge the element types, and have a size if either arg has one. */
823 t1 = build_cplus_array_type
824 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
825 break;
828 case FUNCTION_TYPE:
829 /* Function types: prefer the one that specified arg types.
830 If both do, merge the arg types. Also merge the return types. */
832 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
833 tree p1 = TYPE_ARG_TYPES (t1);
834 tree p2 = TYPE_ARG_TYPES (t2);
835 tree parms;
836 tree rval, raises;
837 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
839 /* Save space: see if the result is identical to one of the args. */
840 if (valtype == TREE_TYPE (t1) && ! p2)
841 return cp_build_type_attribute_variant (t1, attributes);
842 if (valtype == TREE_TYPE (t2) && ! p1)
843 return cp_build_type_attribute_variant (t2, attributes);
845 /* Simple way if one arg fails to specify argument types. */
846 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
847 parms = p2;
848 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
849 parms = p1;
850 else
851 parms = commonparms (p1, p2);
853 rval = build_function_type (valtype, parms);
854 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
855 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
856 rval = apply_memfn_quals (rval,
857 type_memfn_quals (t1),
858 type_memfn_rqual (t1));
859 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
860 TYPE_RAISES_EXCEPTIONS (t2));
861 t1 = build_exception_variant (rval, raises);
862 if (late_return_type_p)
863 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
864 break;
867 case METHOD_TYPE:
869 /* Get this value the long way, since TYPE_METHOD_BASETYPE
870 is just the main variant of this. */
871 tree basetype = class_of_this_parm (t2);
872 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
873 TYPE_RAISES_EXCEPTIONS (t2));
874 cp_ref_qualifier rqual = type_memfn_rqual (t1);
875 tree t3;
876 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
877 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
879 /* If this was a member function type, get back to the
880 original type of type member function (i.e., without
881 the class instance variable up front. */
882 t1 = build_function_type (TREE_TYPE (t1),
883 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
884 t2 = build_function_type (TREE_TYPE (t2),
885 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
886 t3 = merge_types (t1, t2);
887 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
888 TYPE_ARG_TYPES (t3));
889 t1 = build_exception_variant (t3, raises);
890 t1 = build_ref_qualified_type (t1, rqual);
891 if (late_return_type_1_p)
892 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
893 if (late_return_type_2_p)
894 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
895 break;
898 case TYPENAME_TYPE:
899 /* There is no need to merge attributes into a TYPENAME_TYPE.
900 When the type is instantiated it will have whatever
901 attributes result from the instantiation. */
902 return t1;
904 default:;
907 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
908 return t1;
909 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
910 return t2;
911 else
912 return cp_build_type_attribute_variant (t1, attributes);
915 /* Return the ARRAY_TYPE type without its domain. */
917 tree
918 strip_array_domain (tree type)
920 tree t2;
921 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
922 if (TYPE_DOMAIN (type) == NULL_TREE)
923 return type;
924 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
925 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
928 /* Wrapper around cp_common_type that is used by c-common.c and other
929 front end optimizations that remove promotions.
931 Return the common type for two arithmetic types T1 and T2 under the
932 usual arithmetic conversions. The default conversions have already
933 been applied, and enumerated types converted to their compatible
934 integer types. */
936 tree
937 common_type (tree t1, tree t2)
939 /* If one type is nonsense, use the other */
940 if (t1 == error_mark_node)
941 return t2;
942 if (t2 == error_mark_node)
943 return t1;
945 return cp_common_type (t1, t2);
948 /* Return the common type of two pointer types T1 and T2. This is the
949 type for the result of most arithmetic operations if the operands
950 have the given two types.
952 We assume that comp_target_types has already been done and returned
953 nonzero; if that isn't so, this may crash. */
955 tree
956 common_pointer_type (tree t1, tree t2)
958 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
959 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
960 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
962 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
963 CPO_CONVERSION, tf_warning_or_error);
966 /* Compare two exception specifier types for exactness or subsetness, if
967 allowed. Returns false for mismatch, true for match (same, or
968 derived and !exact).
970 [except.spec] "If a class X ... objects of class X or any class publicly
971 and unambiguously derived from X. Similarly, if a pointer type Y * ...
972 exceptions of type Y * or that are pointers to any type publicly and
973 unambiguously derived from Y. Otherwise a function only allows exceptions
974 that have the same type ..."
975 This does not mention cv qualifiers and is different to what throw
976 [except.throw] and catch [except.catch] will do. They will ignore the
977 top level cv qualifiers, and allow qualifiers in the pointer to class
978 example.
980 We implement the letter of the standard. */
982 static bool
983 comp_except_types (tree a, tree b, bool exact)
985 if (same_type_p (a, b))
986 return true;
987 else if (!exact)
989 if (cp_type_quals (a) || cp_type_quals (b))
990 return false;
992 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
994 a = TREE_TYPE (a);
995 b = TREE_TYPE (b);
996 if (cp_type_quals (a) || cp_type_quals (b))
997 return false;
1000 if (TREE_CODE (a) != RECORD_TYPE
1001 || TREE_CODE (b) != RECORD_TYPE)
1002 return false;
1004 if (publicly_uniquely_derived_p (a, b))
1005 return true;
1007 return false;
1010 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1011 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1012 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1013 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1014 are unordered, but we've already filtered out duplicates. Most lists will
1015 be in order, we should try to make use of that. */
1017 bool
1018 comp_except_specs (const_tree t1, const_tree t2, int exact)
1020 const_tree probe;
1021 const_tree base;
1022 int length = 0;
1024 if (t1 == t2)
1025 return true;
1027 /* First handle noexcept. */
1028 if (exact < ce_exact)
1030 /* noexcept(false) is compatible with no exception-specification,
1031 and stricter than any spec. */
1032 if (t1 == noexcept_false_spec)
1033 return t2 == NULL_TREE || exact == ce_derived;
1034 /* Even a derived noexcept(false) is compatible with no
1035 exception-specification. */
1036 if (t2 == noexcept_false_spec)
1037 return t1 == NULL_TREE;
1039 /* Otherwise, if we aren't looking for an exact match, noexcept is
1040 equivalent to throw(). */
1041 if (t1 == noexcept_true_spec)
1042 t1 = empty_except_spec;
1043 if (t2 == noexcept_true_spec)
1044 t2 = empty_except_spec;
1047 /* If any noexcept is left, it is only comparable to itself;
1048 either we're looking for an exact match or we're redeclaring a
1049 template with dependent noexcept. */
1050 if ((t1 && TREE_PURPOSE (t1))
1051 || (t2 && TREE_PURPOSE (t2)))
1052 return (t1 && t2
1053 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1055 if (t1 == NULL_TREE) /* T1 is ... */
1056 return t2 == NULL_TREE || exact == ce_derived;
1057 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1058 return t2 != NULL_TREE && !TREE_VALUE (t2);
1059 if (t2 == NULL_TREE) /* T2 is ... */
1060 return false;
1061 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1062 return exact == ce_derived;
1064 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1065 Count how many we find, to determine exactness. For exact matching and
1066 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1067 O(nm). */
1068 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1070 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1072 tree a = TREE_VALUE (probe);
1073 tree b = TREE_VALUE (t2);
1075 if (comp_except_types (a, b, exact))
1077 if (probe == base && exact > ce_derived)
1078 base = TREE_CHAIN (probe);
1079 length++;
1080 break;
1083 if (probe == NULL_TREE)
1084 return false;
1086 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1089 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1090 [] can match [size]. */
1092 static bool
1093 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1095 tree d1;
1096 tree d2;
1097 tree max1, max2;
1099 if (t1 == t2)
1100 return true;
1102 /* The type of the array elements must be the same. */
1103 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1104 return false;
1106 d1 = TYPE_DOMAIN (t1);
1107 d2 = TYPE_DOMAIN (t2);
1109 if (d1 == d2)
1110 return true;
1112 /* If one of the arrays is dimensionless, and the other has a
1113 dimension, they are of different types. However, it is valid to
1114 write:
1116 extern int a[];
1117 int a[3];
1119 by [basic.link]:
1121 declarations for an array object can specify
1122 array types that differ by the presence or absence of a major
1123 array bound (_dcl.array_). */
1124 if (!d1 || !d2)
1125 return allow_redeclaration;
1127 /* Check that the dimensions are the same. */
1129 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1130 return false;
1131 max1 = TYPE_MAX_VALUE (d1);
1132 max2 = TYPE_MAX_VALUE (d2);
1134 if (!cp_tree_equal (max1, max2))
1135 return false;
1137 return true;
1140 /* Compare the relative position of T1 and T2 into their respective
1141 template parameter list.
1142 T1 and T2 must be template parameter types.
1143 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1145 static bool
1146 comp_template_parms_position (tree t1, tree t2)
1148 tree index1, index2;
1149 gcc_assert (t1 && t2
1150 && TREE_CODE (t1) == TREE_CODE (t2)
1151 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1152 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1153 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1155 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1156 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1158 /* Then compare their relative position. */
1159 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1160 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1161 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1162 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1163 return false;
1165 /* In C++14 we can end up comparing 'auto' to a normal template
1166 parameter. Don't confuse them. */
1167 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1168 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1170 return true;
1173 /* Subroutine in comptypes. */
1175 static bool
1176 structural_comptypes (tree t1, tree t2, int strict)
1178 if (t1 == t2)
1179 return true;
1181 /* Suppress errors caused by previously reported errors. */
1182 if (t1 == error_mark_node || t2 == error_mark_node)
1183 return false;
1185 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1187 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1188 current instantiation. */
1189 if (TREE_CODE (t1) == TYPENAME_TYPE)
1190 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1192 if (TREE_CODE (t2) == TYPENAME_TYPE)
1193 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1195 if (TYPE_PTRMEMFUNC_P (t1))
1196 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1197 if (TYPE_PTRMEMFUNC_P (t2))
1198 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1200 /* Different classes of types can't be compatible. */
1201 if (TREE_CODE (t1) != TREE_CODE (t2))
1202 return false;
1204 /* Qualifiers must match. For array types, we will check when we
1205 recur on the array element types. */
1206 if (TREE_CODE (t1) != ARRAY_TYPE
1207 && cp_type_quals (t1) != cp_type_quals (t2))
1208 return false;
1209 if (TREE_CODE (t1) == FUNCTION_TYPE
1210 && type_memfn_quals (t1) != type_memfn_quals (t2))
1211 return false;
1212 /* Need to check this before TYPE_MAIN_VARIANT.
1213 FIXME function qualifiers should really change the main variant. */
1214 if ((TREE_CODE (t1) == FUNCTION_TYPE
1215 || TREE_CODE (t1) == METHOD_TYPE)
1216 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1217 return false;
1218 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1219 return false;
1221 /* Allow for two different type nodes which have essentially the same
1222 definition. Note that we already checked for equality of the type
1223 qualifiers (just above). */
1225 if (TREE_CODE (t1) != ARRAY_TYPE
1226 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1227 return true;
1230 /* Compare the types. Break out if they could be the same. */
1231 switch (TREE_CODE (t1))
1233 case VOID_TYPE:
1234 case BOOLEAN_TYPE:
1235 /* All void and bool types are the same. */
1236 break;
1238 case INTEGER_TYPE:
1239 case FIXED_POINT_TYPE:
1240 case REAL_TYPE:
1241 /* With these nodes, we can't determine type equivalence by
1242 looking at what is stored in the nodes themselves, because
1243 two nodes might have different TYPE_MAIN_VARIANTs but still
1244 represent the same type. For example, wchar_t and int could
1245 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1246 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1247 and are distinct types. On the other hand, int and the
1248 following typedef
1250 typedef int INT __attribute((may_alias));
1252 have identical properties, different TYPE_MAIN_VARIANTs, but
1253 represent the same type. The canonical type system keeps
1254 track of equivalence in this case, so we fall back on it. */
1255 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1257 case TEMPLATE_TEMPLATE_PARM:
1258 case BOUND_TEMPLATE_TEMPLATE_PARM:
1259 if (!comp_template_parms_position (t1, t2))
1260 return false;
1261 if (!comp_template_parms
1262 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1263 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1264 return false;
1265 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1266 break;
1267 /* Don't check inheritance. */
1268 strict = COMPARE_STRICT;
1269 /* Fall through. */
1271 case RECORD_TYPE:
1272 case UNION_TYPE:
1273 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1274 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1275 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1276 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1277 break;
1279 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1280 break;
1281 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1282 break;
1284 return false;
1286 case OFFSET_TYPE:
1287 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1288 strict & ~COMPARE_REDECLARATION))
1289 return false;
1290 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1291 return false;
1292 break;
1294 case REFERENCE_TYPE:
1295 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1296 return false;
1297 /* fall through to checks for pointer types */
1299 case POINTER_TYPE:
1300 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1301 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1302 return false;
1303 break;
1305 case METHOD_TYPE:
1306 case FUNCTION_TYPE:
1307 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1308 return false;
1309 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1310 return false;
1311 break;
1313 case ARRAY_TYPE:
1314 /* Target types must match incl. qualifiers. */
1315 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1316 return false;
1317 break;
1319 case TEMPLATE_TYPE_PARM:
1320 /* If T1 and T2 don't have the same relative position in their
1321 template parameters set, they can't be equal. */
1322 if (!comp_template_parms_position (t1, t2))
1323 return false;
1324 break;
1326 case TYPENAME_TYPE:
1327 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1328 TYPENAME_TYPE_FULLNAME (t2)))
1329 return false;
1330 /* Qualifiers don't matter on scopes. */
1331 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1332 TYPE_CONTEXT (t2)))
1333 return false;
1334 break;
1336 case UNBOUND_CLASS_TEMPLATE:
1337 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1338 return false;
1339 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1340 return false;
1341 break;
1343 case COMPLEX_TYPE:
1344 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1345 return false;
1346 break;
1348 case VECTOR_TYPE:
1349 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1350 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1351 return false;
1352 break;
1354 case TYPE_PACK_EXPANSION:
1355 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1356 PACK_EXPANSION_PATTERN (t2))
1357 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1358 PACK_EXPANSION_EXTRA_ARGS (t2)));
1360 case DECLTYPE_TYPE:
1361 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1362 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1363 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1364 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1365 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1366 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1367 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1368 DECLTYPE_TYPE_EXPR (t2)))
1369 return false;
1370 break;
1372 case UNDERLYING_TYPE:
1373 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1374 UNDERLYING_TYPE_TYPE (t2));
1376 default:
1377 return false;
1380 /* If we get here, we know that from a target independent POV the
1381 types are the same. Make sure the target attributes are also
1382 the same. */
1383 return comp_type_attributes (t1, t2);
1386 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1387 is a bitwise-or of the COMPARE_* flags. */
1389 bool
1390 comptypes (tree t1, tree t2, int strict)
1392 if (strict == COMPARE_STRICT)
1394 if (t1 == t2)
1395 return true;
1397 if (t1 == error_mark_node || t2 == error_mark_node)
1398 return false;
1400 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1401 /* At least one of the types requires structural equality, so
1402 perform a deep check. */
1403 return structural_comptypes (t1, t2, strict);
1405 #ifdef ENABLE_CHECKING
1406 if (USE_CANONICAL_TYPES)
1408 bool result = structural_comptypes (t1, t2, strict);
1410 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1411 /* The two types are structurally equivalent, but their
1412 canonical types were different. This is a failure of the
1413 canonical type propagation code.*/
1414 internal_error
1415 ("canonical types differ for identical types %T and %T",
1416 t1, t2);
1417 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1418 /* Two types are structurally different, but the canonical
1419 types are the same. This means we were over-eager in
1420 assigning canonical types. */
1421 internal_error
1422 ("same canonical type node for different types %T and %T",
1423 t1, t2);
1425 return result;
1427 #else
1428 if (USE_CANONICAL_TYPES)
1429 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1430 #endif
1431 else
1432 return structural_comptypes (t1, t2, strict);
1434 else if (strict == COMPARE_STRUCTURAL)
1435 return structural_comptypes (t1, t2, COMPARE_STRICT);
1436 else
1437 return structural_comptypes (t1, t2, strict);
1440 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1441 top-level qualifiers. */
1443 bool
1444 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1446 if (type1 == error_mark_node || type2 == error_mark_node)
1447 return false;
1449 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1452 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1454 bool
1455 at_least_as_qualified_p (const_tree type1, const_tree type2)
1457 int q1 = cp_type_quals (type1);
1458 int q2 = cp_type_quals (type2);
1460 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1461 return (q1 & q2) == q2;
1464 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1465 more cv-qualified that TYPE1, and 0 otherwise. */
1468 comp_cv_qualification (int q1, int q2)
1470 if (q1 == q2)
1471 return 0;
1473 if ((q1 & q2) == q2)
1474 return 1;
1475 else if ((q1 & q2) == q1)
1476 return -1;
1478 return 0;
1482 comp_cv_qualification (const_tree type1, const_tree type2)
1484 int q1 = cp_type_quals (type1);
1485 int q2 = cp_type_quals (type2);
1486 return comp_cv_qualification (q1, q2);
1489 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1490 subset of the cv-qualification signature of TYPE2, and the types
1491 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1494 comp_cv_qual_signature (tree type1, tree type2)
1496 if (comp_ptr_ttypes_real (type2, type1, -1))
1497 return 1;
1498 else if (comp_ptr_ttypes_real (type1, type2, -1))
1499 return -1;
1500 else
1501 return 0;
1504 /* Subroutines of `comptypes'. */
1506 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1507 equivalent in the sense that functions with those parameter types
1508 can have equivalent types. The two lists must be equivalent,
1509 element by element. */
1511 bool
1512 compparms (const_tree parms1, const_tree parms2)
1514 const_tree t1, t2;
1516 /* An unspecified parmlist matches any specified parmlist
1517 whose argument types don't need default promotions. */
1519 for (t1 = parms1, t2 = parms2;
1520 t1 || t2;
1521 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1523 /* If one parmlist is shorter than the other,
1524 they fail to match. */
1525 if (!t1 || !t2)
1526 return false;
1527 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1528 return false;
1530 return true;
1534 /* Process a sizeof or alignof expression where the operand is a
1535 type. */
1537 tree
1538 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1540 tree value;
1541 bool dependent_p;
1543 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1544 if (type == error_mark_node)
1545 return error_mark_node;
1547 type = non_reference (type);
1548 if (TREE_CODE (type) == METHOD_TYPE)
1550 if (complain)
1551 pedwarn (input_location, OPT_Wpointer_arith,
1552 "invalid application of %qs to a member function",
1553 operator_name_info[(int) op].name);
1554 else
1555 return error_mark_node;
1556 value = size_one_node;
1559 dependent_p = dependent_type_p (type);
1560 if (!dependent_p)
1561 complete_type (type);
1562 if (dependent_p
1563 /* VLA types will have a non-constant size. In the body of an
1564 uninstantiated template, we don't need to try to compute the
1565 value, because the sizeof expression is not an integral
1566 constant expression in that case. And, if we do try to
1567 compute the value, we'll likely end up with SAVE_EXPRs, which
1568 the template substitution machinery does not expect to see. */
1569 || (processing_template_decl
1570 && COMPLETE_TYPE_P (type)
1571 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1573 value = build_min (op, size_type_node, type);
1574 TREE_READONLY (value) = 1;
1575 return value;
1578 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1579 op == SIZEOF_EXPR, false,
1580 complain);
1583 /* Return the size of the type, without producing any warnings for
1584 types whose size cannot be taken. This routine should be used only
1585 in some other routine that has already produced a diagnostic about
1586 using the size of such a type. */
1587 tree
1588 cxx_sizeof_nowarn (tree type)
1590 if (TREE_CODE (type) == FUNCTION_TYPE
1591 || VOID_TYPE_P (type)
1592 || TREE_CODE (type) == ERROR_MARK)
1593 return size_one_node;
1594 else if (!COMPLETE_TYPE_P (type))
1595 return size_zero_node;
1596 else
1597 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1600 /* Process a sizeof expression where the operand is an expression. */
1602 static tree
1603 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1605 if (e == error_mark_node)
1606 return error_mark_node;
1608 if (processing_template_decl)
1610 e = build_min (SIZEOF_EXPR, size_type_node, e);
1611 TREE_SIDE_EFFECTS (e) = 0;
1612 TREE_READONLY (e) = 1;
1614 return e;
1617 /* To get the size of a static data member declared as an array of
1618 unknown bound, we need to instantiate it. */
1619 if (VAR_P (e)
1620 && VAR_HAD_UNKNOWN_BOUND (e)
1621 && DECL_TEMPLATE_INSTANTIATION (e))
1622 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1624 if (TREE_CODE (e) == PARM_DECL
1625 && DECL_ARRAY_PARAMETER_P (e)
1626 && (complain & tf_warning))
1628 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1629 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1630 inform (DECL_SOURCE_LOCATION (e), "declared here");
1633 e = mark_type_use (e);
1635 if (TREE_CODE (e) == COMPONENT_REF
1636 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1637 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1639 if (complain & tf_error)
1640 error ("invalid application of %<sizeof%> to a bit-field");
1641 else
1642 return error_mark_node;
1643 e = char_type_node;
1645 else if (is_overloaded_fn (e))
1647 if (complain & tf_error)
1648 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1649 "function type");
1650 else
1651 return error_mark_node;
1652 e = char_type_node;
1654 else if (type_unknown_p (e))
1656 if (complain & tf_error)
1657 cxx_incomplete_type_error (e, TREE_TYPE (e));
1658 else
1659 return error_mark_node;
1660 e = char_type_node;
1662 else
1663 e = TREE_TYPE (e);
1665 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1668 /* Implement the __alignof keyword: Return the minimum required
1669 alignment of E, measured in bytes. For VAR_DECL's and
1670 FIELD_DECL's return DECL_ALIGN (which can be set from an
1671 "aligned" __attribute__ specification). */
1673 static tree
1674 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1676 tree t;
1678 if (e == error_mark_node)
1679 return error_mark_node;
1681 if (processing_template_decl)
1683 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1684 TREE_SIDE_EFFECTS (e) = 0;
1685 TREE_READONLY (e) = 1;
1687 return e;
1690 e = mark_type_use (e);
1692 if (VAR_P (e))
1693 t = size_int (DECL_ALIGN_UNIT (e));
1694 else if (TREE_CODE (e) == COMPONENT_REF
1695 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1696 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1698 if (complain & tf_error)
1699 error ("invalid application of %<__alignof%> to a bit-field");
1700 else
1701 return error_mark_node;
1702 t = size_one_node;
1704 else if (TREE_CODE (e) == COMPONENT_REF
1705 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1706 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1707 else if (is_overloaded_fn (e))
1709 if (complain & tf_error)
1710 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1711 "function type");
1712 else
1713 return error_mark_node;
1714 if (TREE_CODE (e) == FUNCTION_DECL)
1715 t = size_int (DECL_ALIGN_UNIT (e));
1716 else
1717 t = size_one_node;
1719 else if (type_unknown_p (e))
1721 if (complain & tf_error)
1722 cxx_incomplete_type_error (e, TREE_TYPE (e));
1723 else
1724 return error_mark_node;
1725 t = size_one_node;
1727 else
1728 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1729 complain & tf_error);
1731 return fold_convert (size_type_node, t);
1734 /* Process a sizeof or alignof expression E with code OP where the operand
1735 is an expression. */
1737 tree
1738 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1740 if (op == SIZEOF_EXPR)
1741 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1742 else
1743 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1746 /* Build a representation of an expression 'alignas(E).' Return the
1747 folded integer value of E if it is an integral constant expression
1748 that resolves to a valid alignment. If E depends on a template
1749 parameter, return a syntactic representation tree of kind
1750 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1751 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1753 tree
1754 cxx_alignas_expr (tree e)
1756 if (e == NULL_TREE || e == error_mark_node
1757 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1758 return e;
1760 if (TYPE_P (e))
1761 /* [dcl.align]/3:
1763 When the alignment-specifier is of the form
1764 alignas(type-id ), it shall have the same effect as
1765 alignas(alignof(type-id )). */
1767 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1769 /* If we reach this point, it means the alignas expression if of
1770 the form "alignas(assignment-expression)", so we should follow
1771 what is stated by [dcl.align]/2. */
1773 if (value_dependent_expression_p (e))
1774 /* Leave value-dependent expression alone for now. */
1775 return e;
1777 e = instantiate_non_dependent_expr (e);
1778 e = mark_rvalue_use (e);
1780 /* [dcl.align]/2 says:
1782 the assignment-expression shall be an integral constant
1783 expression. */
1785 return cxx_constant_value (e);
1789 /* EXPR is being used in a context that is not a function call.
1790 Enforce:
1792 [expr.ref]
1794 The expression can be used only as the left-hand operand of a
1795 member function call.
1797 [expr.mptr.operator]
1799 If the result of .* or ->* is a function, then that result can be
1800 used only as the operand for the function call operator ().
1802 by issuing an error message if appropriate. Returns true iff EXPR
1803 violates these rules. */
1805 bool
1806 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1808 if (expr == NULL_TREE)
1809 return false;
1810 /* Don't enforce this in MS mode. */
1811 if (flag_ms_extensions)
1812 return false;
1813 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1814 expr = get_first_fn (expr);
1815 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1817 if (complain & tf_error)
1819 if (DECL_P (expr))
1821 error_at (loc, "invalid use of non-static member function %qD",
1822 expr);
1823 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1825 else
1826 error_at (loc, "invalid use of non-static member function of "
1827 "type %qT", TREE_TYPE (expr));
1829 return true;
1831 return false;
1834 /* If EXP is a reference to a bitfield, and the type of EXP does not
1835 match the declared type of the bitfield, return the declared type
1836 of the bitfield. Otherwise, return NULL_TREE. */
1838 tree
1839 is_bitfield_expr_with_lowered_type (const_tree exp)
1841 switch (TREE_CODE (exp))
1843 case COND_EXPR:
1844 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1845 ? TREE_OPERAND (exp, 1)
1846 : TREE_OPERAND (exp, 0)))
1847 return NULL_TREE;
1848 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1850 case COMPOUND_EXPR:
1851 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1853 case MODIFY_EXPR:
1854 case SAVE_EXPR:
1855 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1857 case COMPONENT_REF:
1859 tree field;
1861 field = TREE_OPERAND (exp, 1);
1862 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1863 return NULL_TREE;
1864 if (same_type_ignoring_top_level_qualifiers_p
1865 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1866 return NULL_TREE;
1867 return DECL_BIT_FIELD_TYPE (field);
1870 CASE_CONVERT:
1871 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1872 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1873 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1874 /* Fallthrough. */
1876 default:
1877 return NULL_TREE;
1881 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1882 bitfield with a lowered type, the type of EXP is returned, rather
1883 than NULL_TREE. */
1885 tree
1886 unlowered_expr_type (const_tree exp)
1888 tree type;
1889 tree etype = TREE_TYPE (exp);
1891 type = is_bitfield_expr_with_lowered_type (exp);
1892 if (type)
1893 type = cp_build_qualified_type (type, cp_type_quals (etype));
1894 else
1895 type = etype;
1897 return type;
1900 /* Perform the conversions in [expr] that apply when an lvalue appears
1901 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1902 function-to-pointer conversions. In addition, manifest constants
1903 are replaced by their values, and bitfield references are converted
1904 to their declared types. Note that this function does not perform the
1905 lvalue-to-rvalue conversion for class types. If you need that conversion
1906 to for class types, then you probably need to use force_rvalue.
1908 Although the returned value is being used as an rvalue, this
1909 function does not wrap the returned expression in a
1910 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1911 that the return value is no longer an lvalue. */
1913 tree
1914 decay_conversion (tree exp,
1915 tsubst_flags_t complain,
1916 bool reject_builtin /* = true */)
1918 tree type;
1919 enum tree_code code;
1920 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1922 type = TREE_TYPE (exp);
1923 if (type == error_mark_node)
1924 return error_mark_node;
1926 exp = mark_rvalue_use (exp, loc, reject_builtin);
1928 exp = resolve_nondeduced_context (exp);
1929 if (type_unknown_p (exp))
1931 if (complain & tf_error)
1932 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1933 return error_mark_node;
1936 code = TREE_CODE (type);
1938 /* FIXME remove for delayed folding. */
1939 exp = scalar_constant_value (exp);
1940 if (error_operand_p (exp))
1941 return error_mark_node;
1943 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1944 return nullptr_node;
1946 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1947 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1948 if (code == VOID_TYPE)
1950 if (complain & tf_error)
1951 error_at (loc, "void value not ignored as it ought to be");
1952 return error_mark_node;
1954 if (invalid_nonstatic_memfn_p (loc, exp, complain))
1955 return error_mark_node;
1956 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1957 return cp_build_addr_expr (exp, complain);
1958 if (code == ARRAY_TYPE)
1960 tree adr;
1961 tree ptrtype;
1963 if (INDIRECT_REF_P (exp))
1964 return build_nop (build_pointer_type (TREE_TYPE (type)),
1965 TREE_OPERAND (exp, 0));
1967 if (TREE_CODE (exp) == COMPOUND_EXPR)
1969 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1970 if (op1 == error_mark_node)
1971 return error_mark_node;
1972 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1973 TREE_OPERAND (exp, 0), op1);
1976 if (!lvalue_p (exp)
1977 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1979 if (complain & tf_error)
1980 error_at (loc, "invalid use of non-lvalue array");
1981 return error_mark_node;
1984 /* Don't let an array compound literal decay to a pointer. It can
1985 still be used to initialize an array or bind to a reference. */
1986 if (TREE_CODE (exp) == TARGET_EXPR)
1988 if (complain & tf_error)
1989 error_at (loc, "taking address of temporary array");
1990 return error_mark_node;
1993 ptrtype = build_pointer_type (TREE_TYPE (type));
1995 if (VAR_P (exp))
1997 if (!cxx_mark_addressable (exp))
1998 return error_mark_node;
1999 adr = build_nop (ptrtype, build_address (exp));
2000 return adr;
2002 /* This way is better for a COMPONENT_REF since it can
2003 simplify the offset for a component. */
2004 adr = cp_build_addr_expr (exp, complain);
2005 return cp_convert (ptrtype, adr, complain);
2008 /* If a bitfield is used in a context where integral promotion
2009 applies, then the caller is expected to have used
2010 default_conversion. That function promotes bitfields correctly
2011 before calling this function. At this point, if we have a
2012 bitfield referenced, we may assume that is not subject to
2013 promotion, and that, therefore, the type of the resulting rvalue
2014 is the declared type of the bitfield. */
2015 exp = convert_bitfield_to_declared_type (exp);
2017 /* We do not call rvalue() here because we do not want to wrap EXP
2018 in a NON_LVALUE_EXPR. */
2020 /* [basic.lval]
2022 Non-class rvalues always have cv-unqualified types. */
2023 type = TREE_TYPE (exp);
2024 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2025 exp = build_nop (cv_unqualified (type), exp);
2027 return exp;
2030 /* Perform preparatory conversions, as part of the "usual arithmetic
2031 conversions". In particular, as per [expr]:
2033 Whenever an lvalue expression appears as an operand of an
2034 operator that expects the rvalue for that operand, the
2035 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2036 standard conversions are applied to convert the expression to an
2037 rvalue.
2039 In addition, we perform integral promotions here, as those are
2040 applied to both operands to a binary operator before determining
2041 what additional conversions should apply. */
2043 static tree
2044 cp_default_conversion (tree exp, tsubst_flags_t complain)
2046 /* Check for target-specific promotions. */
2047 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2048 if (promoted_type)
2049 exp = cp_convert (promoted_type, exp, complain);
2050 /* Perform the integral promotions first so that bitfield
2051 expressions (which may promote to "int", even if the bitfield is
2052 declared "unsigned") are promoted correctly. */
2053 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2054 exp = cp_perform_integral_promotions (exp, complain);
2055 /* Perform the other conversions. */
2056 exp = decay_conversion (exp, complain);
2058 return exp;
2061 /* C version. */
2063 tree
2064 default_conversion (tree exp)
2066 return cp_default_conversion (exp, tf_warning_or_error);
2069 /* EXPR is an expression with an integral or enumeration type.
2070 Perform the integral promotions in [conv.prom], and return the
2071 converted value. */
2073 tree
2074 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2076 tree type;
2077 tree promoted_type;
2079 expr = mark_rvalue_use (expr);
2081 /* [conv.prom]
2083 If the bitfield has an enumerated type, it is treated as any
2084 other value of that type for promotion purposes. */
2085 type = is_bitfield_expr_with_lowered_type (expr);
2086 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2087 type = TREE_TYPE (expr);
2088 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2089 /* Scoped enums don't promote. */
2090 if (SCOPED_ENUM_P (type))
2091 return expr;
2092 promoted_type = type_promotes_to (type);
2093 if (type != promoted_type)
2094 expr = cp_convert (promoted_type, expr, complain);
2095 return expr;
2098 /* C version. */
2100 tree
2101 perform_integral_promotions (tree expr)
2103 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2106 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2107 decay_conversion to one. */
2110 string_conv_p (const_tree totype, const_tree exp, int warn)
2112 tree t;
2114 if (!TYPE_PTR_P (totype))
2115 return 0;
2117 t = TREE_TYPE (totype);
2118 if (!same_type_p (t, char_type_node)
2119 && !same_type_p (t, char16_type_node)
2120 && !same_type_p (t, char32_type_node)
2121 && !same_type_p (t, wchar_type_node))
2122 return 0;
2124 if (TREE_CODE (exp) == STRING_CST)
2126 /* Make sure that we don't try to convert between char and wide chars. */
2127 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2128 return 0;
2130 else
2132 /* Is this a string constant which has decayed to 'const char *'? */
2133 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2134 if (!same_type_p (TREE_TYPE (exp), t))
2135 return 0;
2136 STRIP_NOPS (exp);
2137 if (TREE_CODE (exp) != ADDR_EXPR
2138 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2139 return 0;
2141 if (warn)
2143 if (cxx_dialect >= cxx11)
2144 pedwarn (input_location,
2145 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
2146 "ISO C++ forbids converting a string constant to %qT",
2147 totype);
2148 else
2149 warning (OPT_Wwrite_strings,
2150 "deprecated conversion from string constant to %qT",
2151 totype);
2154 return 1;
2157 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2158 can, for example, use as an lvalue. This code used to be in
2159 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2160 expressions, where we're dealing with aggregates. But now it's again only
2161 called from unary_complex_lvalue. The case (in particular) that led to
2162 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2163 get it there. */
2165 static tree
2166 rationalize_conditional_expr (enum tree_code code, tree t,
2167 tsubst_flags_t complain)
2169 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2171 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2172 the first operand is always the one to be used if both operands
2173 are equal, so we know what conditional expression this used to be. */
2174 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2176 tree op0 = TREE_OPERAND (t, 0);
2177 tree op1 = TREE_OPERAND (t, 1);
2179 /* The following code is incorrect if either operand side-effects. */
2180 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2181 && !TREE_SIDE_EFFECTS (op1));
2182 return
2183 build_conditional_expr (loc,
2184 build_x_binary_op (loc,
2185 (TREE_CODE (t) == MIN_EXPR
2186 ? LE_EXPR : GE_EXPR),
2187 op0, TREE_CODE (op0),
2188 op1, TREE_CODE (op1),
2189 /*overload=*/NULL,
2190 complain),
2191 cp_build_unary_op (code, op0, 0, complain),
2192 cp_build_unary_op (code, op1, 0, complain),
2193 complain);
2196 return
2197 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2198 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2199 complain),
2200 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2201 complain),
2202 complain);
2205 /* Given the TYPE of an anonymous union field inside T, return the
2206 FIELD_DECL for the field. If not found return NULL_TREE. Because
2207 anonymous unions can nest, we must also search all anonymous unions
2208 that are directly reachable. */
2210 tree
2211 lookup_anon_field (tree t, tree type)
2213 tree field;
2215 t = TYPE_MAIN_VARIANT (t);
2217 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2219 if (TREE_STATIC (field))
2220 continue;
2221 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2222 continue;
2224 /* If we find it directly, return the field. */
2225 if (DECL_NAME (field) == NULL_TREE
2226 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2228 return field;
2231 /* Otherwise, it could be nested, search harder. */
2232 if (DECL_NAME (field) == NULL_TREE
2233 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2235 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2236 if (subfield)
2237 return subfield;
2240 return NULL_TREE;
2243 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2244 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2245 non-NULL, it indicates the path to the base used to name MEMBER.
2246 If PRESERVE_REFERENCE is true, the expression returned will have
2247 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2248 returned will have the type referred to by the reference.
2250 This function does not perform access control; that is either done
2251 earlier by the parser when the name of MEMBER is resolved to MEMBER
2252 itself, or later when overload resolution selects one of the
2253 functions indicated by MEMBER. */
2255 tree
2256 build_class_member_access_expr (tree object, tree member,
2257 tree access_path, bool preserve_reference,
2258 tsubst_flags_t complain)
2260 tree object_type;
2261 tree member_scope;
2262 tree result = NULL_TREE;
2263 tree using_decl = NULL_TREE;
2265 if (error_operand_p (object) || error_operand_p (member))
2266 return error_mark_node;
2268 gcc_assert (DECL_P (member) || BASELINK_P (member));
2270 /* [expr.ref]
2272 The type of the first expression shall be "class object" (of a
2273 complete type). */
2274 object_type = TREE_TYPE (object);
2275 if (!currently_open_class (object_type)
2276 && !complete_type_or_maybe_complain (object_type, object, complain))
2277 return error_mark_node;
2278 if (!CLASS_TYPE_P (object_type))
2280 if (complain & tf_error)
2282 if (POINTER_TYPE_P (object_type)
2283 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2284 error ("request for member %qD in %qE, which is of pointer "
2285 "type %qT (maybe you meant to use %<->%> ?)",
2286 member, object, object_type);
2287 else
2288 error ("request for member %qD in %qE, which is of non-class "
2289 "type %qT", member, object, object_type);
2291 return error_mark_node;
2294 /* The standard does not seem to actually say that MEMBER must be a
2295 member of OBJECT_TYPE. However, that is clearly what is
2296 intended. */
2297 if (DECL_P (member))
2299 member_scope = DECL_CLASS_CONTEXT (member);
2300 if (!mark_used (member, complain) && !(complain & tf_error))
2301 return error_mark_node;
2302 if (TREE_DEPRECATED (member))
2303 warn_deprecated_use (member, NULL_TREE);
2305 else
2306 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2307 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2308 presently be the anonymous union. Go outwards until we find a
2309 type related to OBJECT_TYPE. */
2310 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2311 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2312 object_type))
2313 member_scope = TYPE_CONTEXT (member_scope);
2314 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2316 if (complain & tf_error)
2318 if (TREE_CODE (member) == FIELD_DECL)
2319 error ("invalid use of nonstatic data member %qE", member);
2320 else
2321 error ("%qD is not a member of %qT", member, object_type);
2323 return error_mark_node;
2326 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2327 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2328 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2330 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2331 if (temp)
2332 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2335 /* In [expr.ref], there is an explicit list of the valid choices for
2336 MEMBER. We check for each of those cases here. */
2337 if (VAR_P (member))
2339 /* A static data member. */
2340 result = member;
2341 mark_exp_read (object);
2342 /* If OBJECT has side-effects, they are supposed to occur. */
2343 if (TREE_SIDE_EFFECTS (object))
2344 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2346 else if (TREE_CODE (member) == FIELD_DECL)
2348 /* A non-static data member. */
2349 bool null_object_p;
2350 int type_quals;
2351 tree member_type;
2353 null_object_p = (INDIRECT_REF_P (object)
2354 && integer_zerop (TREE_OPERAND (object, 0)));
2356 /* Convert OBJECT to the type of MEMBER. */
2357 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2358 TYPE_MAIN_VARIANT (member_scope)))
2360 tree binfo;
2361 base_kind kind;
2363 binfo = lookup_base (access_path ? access_path : object_type,
2364 member_scope, ba_unique, &kind, complain);
2365 if (binfo == error_mark_node)
2366 return error_mark_node;
2368 /* It is invalid to try to get to a virtual base of a
2369 NULL object. The most common cause is invalid use of
2370 offsetof macro. */
2371 if (null_object_p && kind == bk_via_virtual)
2373 if (complain & tf_error)
2375 error ("invalid access to non-static data member %qD in "
2376 "virtual base of NULL object", member);
2378 return error_mark_node;
2381 /* Convert to the base. */
2382 object = build_base_path (PLUS_EXPR, object, binfo,
2383 /*nonnull=*/1, complain);
2384 /* If we found the base successfully then we should be able
2385 to convert to it successfully. */
2386 gcc_assert (object != error_mark_node);
2389 /* If MEMBER is from an anonymous aggregate, we have converted
2390 OBJECT so that it refers to the class containing the
2391 anonymous union. Generate a reference to the anonymous union
2392 itself, and recur to find MEMBER. */
2393 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2394 /* When this code is called from build_field_call, the
2395 object already has the type of the anonymous union.
2396 That is because the COMPONENT_REF was already
2397 constructed, and was then disassembled before calling
2398 build_field_call. After the function-call code is
2399 cleaned up, this waste can be eliminated. */
2400 && (!same_type_ignoring_top_level_qualifiers_p
2401 (TREE_TYPE (object), DECL_CONTEXT (member))))
2403 tree anonymous_union;
2405 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2406 DECL_CONTEXT (member));
2407 object = build_class_member_access_expr (object,
2408 anonymous_union,
2409 /*access_path=*/NULL_TREE,
2410 preserve_reference,
2411 complain);
2414 /* Compute the type of the field, as described in [expr.ref]. */
2415 type_quals = TYPE_UNQUALIFIED;
2416 member_type = TREE_TYPE (member);
2417 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2419 type_quals = (cp_type_quals (member_type)
2420 | cp_type_quals (object_type));
2422 /* A field is const (volatile) if the enclosing object, or the
2423 field itself, is const (volatile). But, a mutable field is
2424 not const, even within a const object. */
2425 if (DECL_MUTABLE_P (member))
2426 type_quals &= ~TYPE_QUAL_CONST;
2427 member_type = cp_build_qualified_type (member_type, type_quals);
2430 result = build3_loc (input_location, COMPONENT_REF, member_type,
2431 object, member, NULL_TREE);
2432 result = fold_if_not_in_template (result);
2434 /* Mark the expression const or volatile, as appropriate. Even
2435 though we've dealt with the type above, we still have to mark the
2436 expression itself. */
2437 if (type_quals & TYPE_QUAL_CONST)
2438 TREE_READONLY (result) = 1;
2439 if (type_quals & TYPE_QUAL_VOLATILE)
2440 TREE_THIS_VOLATILE (result) = 1;
2442 else if (BASELINK_P (member))
2444 /* The member is a (possibly overloaded) member function. */
2445 tree functions;
2446 tree type;
2448 /* If the MEMBER is exactly one static member function, then we
2449 know the type of the expression. Otherwise, we must wait
2450 until overload resolution has been performed. */
2451 functions = BASELINK_FUNCTIONS (member);
2452 if (TREE_CODE (functions) == FUNCTION_DECL
2453 && DECL_STATIC_FUNCTION_P (functions))
2454 type = TREE_TYPE (functions);
2455 else
2456 type = unknown_type_node;
2457 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2458 base. That will happen when the function is called. */
2459 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2461 else if (TREE_CODE (member) == CONST_DECL)
2463 /* The member is an enumerator. */
2464 result = member;
2465 /* If OBJECT has side-effects, they are supposed to occur. */
2466 if (TREE_SIDE_EFFECTS (object))
2467 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2468 object, result);
2470 else if ((using_decl = strip_using_decl (member)) != member)
2471 result = build_class_member_access_expr (object,
2472 using_decl,
2473 access_path, preserve_reference,
2474 complain);
2475 else
2477 if (complain & tf_error)
2478 error ("invalid use of %qD", member);
2479 return error_mark_node;
2482 if (!preserve_reference)
2483 /* [expr.ref]
2485 If E2 is declared to have type "reference to T", then ... the
2486 type of E1.E2 is T. */
2487 result = convert_from_reference (result);
2489 return result;
2492 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2493 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2495 static tree
2496 lookup_destructor (tree object, tree scope, tree dtor_name,
2497 tsubst_flags_t complain)
2499 tree object_type = TREE_TYPE (object);
2500 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2501 tree expr;
2503 /* We've already complained about this destructor. */
2504 if (dtor_type == error_mark_node)
2505 return error_mark_node;
2507 if (scope && !check_dtor_name (scope, dtor_type))
2509 if (complain & tf_error)
2510 error ("qualified type %qT does not match destructor name ~%qT",
2511 scope, dtor_type);
2512 return error_mark_node;
2514 if (is_auto (dtor_type))
2515 dtor_type = object_type;
2516 else if (identifier_p (dtor_type))
2518 /* In a template, names we can't find a match for are still accepted
2519 destructor names, and we check them here. */
2520 if (check_dtor_name (object_type, dtor_type))
2521 dtor_type = object_type;
2522 else
2524 if (complain & tf_error)
2525 error ("object type %qT does not match destructor name ~%qT",
2526 object_type, dtor_type);
2527 return error_mark_node;
2531 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2533 if (complain & tf_error)
2534 error ("the type being destroyed is %qT, but the destructor "
2535 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2536 return error_mark_node;
2538 expr = lookup_member (dtor_type, complete_dtor_identifier,
2539 /*protect=*/1, /*want_type=*/false,
2540 tf_warning_or_error);
2541 if (!expr)
2543 if (complain & tf_error)
2544 cxx_incomplete_type_error (dtor_name, dtor_type);
2545 return error_mark_node;
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 is dependent, so is OBJECT.NAME. */
2642 type_dependent_expression_p (object)
2643 /* If NAME is "f<args>", where either 'f' or 'args' is
2644 dependent, then the expression is dependent. */
2645 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2646 && dependent_template_id_p (TREE_OPERAND (name, 0),
2647 TREE_OPERAND (name, 1)))
2648 /* If NAME is "T::X" where "T" is dependent, then the
2649 expression is dependent. */
2650 || (TREE_CODE (name) == SCOPE_REF
2651 && TYPE_P (TREE_OPERAND (name, 0))
2652 && dependent_type_p (TREE_OPERAND (name, 0))))
2653 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2654 object, name, NULL_TREE);
2655 object = build_non_dependent_expr (object);
2657 else if (c_dialect_objc ()
2658 && identifier_p (name)
2659 && (expr = objc_maybe_build_component_ref (object, name)))
2660 return expr;
2662 /* [expr.ref]
2664 The type of the first expression shall be "class object" (of a
2665 complete type). */
2666 if (!currently_open_class (object_type)
2667 && !complete_type_or_maybe_complain (object_type, object, complain))
2668 return error_mark_node;
2669 if (!CLASS_TYPE_P (object_type))
2671 if (complain & tf_error)
2673 if (POINTER_TYPE_P (object_type)
2674 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2675 error ("request for member %qD in %qE, which is of pointer "
2676 "type %qT (maybe you meant to use %<->%> ?)",
2677 name, object, object_type);
2678 else
2679 error ("request for member %qD in %qE, which is of non-class "
2680 "type %qT", name, object, object_type);
2682 return error_mark_node;
2685 if (BASELINK_P (name))
2686 /* A member function that has already been looked up. */
2687 member = name;
2688 else
2690 bool is_template_id = false;
2691 tree template_args = NULL_TREE;
2692 tree scope;
2694 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2696 is_template_id = true;
2697 template_args = TREE_OPERAND (name, 1);
2698 name = TREE_OPERAND (name, 0);
2700 if (TREE_CODE (name) == OVERLOAD)
2701 name = DECL_NAME (get_first_fn (name));
2702 else if (DECL_P (name))
2703 name = DECL_NAME (name);
2706 if (TREE_CODE (name) == SCOPE_REF)
2708 /* A qualified name. The qualifying class or namespace `S'
2709 has already been looked up; it is either a TYPE or a
2710 NAMESPACE_DECL. */
2711 scope = TREE_OPERAND (name, 0);
2712 name = TREE_OPERAND (name, 1);
2714 /* If SCOPE is a namespace, then the qualified name does not
2715 name a member of OBJECT_TYPE. */
2716 if (TREE_CODE (scope) == NAMESPACE_DECL)
2718 if (complain & tf_error)
2719 error ("%<%D::%D%> is not a member of %qT",
2720 scope, name, object_type);
2721 return error_mark_node;
2724 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2726 /* Looking up a member enumerator (c++/56793). */
2727 if (!TYPE_CLASS_SCOPE_P (scope)
2728 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2730 if (complain & tf_error)
2731 error ("%<%D::%D%> is not a member of %qT",
2732 scope, name, object_type);
2733 return error_mark_node;
2735 tree val = lookup_enumerator (scope, name);
2736 if (!val)
2738 if (complain & tf_error)
2739 error ("%qD is not a member of %qD",
2740 name, scope);
2741 return error_mark_node;
2744 if (TREE_SIDE_EFFECTS (object))
2745 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2746 return val;
2749 gcc_assert (CLASS_TYPE_P (scope));
2750 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2752 if (constructor_name_p (name, scope))
2754 if (complain & tf_error)
2755 error ("cannot call constructor %<%T::%D%> directly",
2756 scope, name);
2757 return error_mark_node;
2760 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2761 access_path = lookup_base (object_type, scope, ba_check,
2762 NULL, complain);
2763 if (access_path == error_mark_node)
2764 return error_mark_node;
2765 if (!access_path)
2767 if (complain & tf_error)
2768 error ("%qT is not a base of %qT", scope, object_type);
2769 return error_mark_node;
2772 else
2774 scope = NULL_TREE;
2775 access_path = object_type;
2778 if (TREE_CODE (name) == BIT_NOT_EXPR)
2779 member = lookup_destructor (object, scope, name, complain);
2780 else
2782 /* Look up the member. */
2783 member = lookup_member (access_path, name, /*protect=*/1,
2784 /*want_type=*/false, complain);
2785 if (member == NULL_TREE)
2787 if (complain & tf_error)
2788 error ("%q#T has no member named %qE",
2789 TREE_CODE (access_path) == TREE_BINFO
2790 ? TREE_TYPE (access_path) : object_type, name);
2791 return error_mark_node;
2793 if (member == error_mark_node)
2794 return error_mark_node;
2797 if (is_template_id)
2799 tree templ = member;
2801 if (BASELINK_P (templ))
2802 templ = lookup_template_function (templ, template_args);
2803 else
2805 if (complain & tf_error)
2806 error ("%qD is not a member template function", name);
2807 return error_mark_node;
2812 if (TREE_DEPRECATED (member))
2813 warn_deprecated_use (member, NULL_TREE);
2815 if (template_p)
2816 check_template_keyword (member);
2818 expr = build_class_member_access_expr (object, member, access_path,
2819 /*preserve_reference=*/false,
2820 complain);
2821 if (processing_template_decl && expr != error_mark_node)
2823 if (BASELINK_P (member))
2825 if (TREE_CODE (orig_name) == SCOPE_REF)
2826 BASELINK_QUALIFIED_P (member) = 1;
2827 orig_name = member;
2829 return build_min_non_dep (COMPONENT_REF, expr,
2830 orig_object, orig_name,
2831 NULL_TREE);
2834 return expr;
2837 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2838 type. */
2840 tree
2841 build_simple_component_ref (tree object, tree member)
2843 tree type = cp_build_qualified_type (TREE_TYPE (member),
2844 cp_type_quals (TREE_TYPE (object)));
2845 return fold_build3_loc (input_location,
2846 COMPONENT_REF, type,
2847 object, member, NULL_TREE);
2850 /* Return an expression for the MEMBER_NAME field in the internal
2851 representation of PTRMEM, a pointer-to-member function. (Each
2852 pointer-to-member function type gets its own RECORD_TYPE so it is
2853 more convenient to access the fields by name than by FIELD_DECL.)
2854 This routine converts the NAME to a FIELD_DECL and then creates the
2855 node for the complete expression. */
2857 tree
2858 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2860 tree ptrmem_type;
2861 tree member;
2863 /* This code is a stripped down version of
2864 build_class_member_access_expr. It does not work to use that
2865 routine directly because it expects the object to be of class
2866 type. */
2867 ptrmem_type = TREE_TYPE (ptrmem);
2868 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2869 for (member = TYPE_FIELDS (ptrmem_type); member;
2870 member = DECL_CHAIN (member))
2871 if (DECL_NAME (member) == member_name)
2872 break;
2873 return build_simple_component_ref (ptrmem, member);
2876 /* Given an expression PTR for a pointer, return an expression
2877 for the value pointed to.
2878 ERRORSTRING is the name of the operator to appear in error messages.
2880 This function may need to overload OPERATOR_FNNAME.
2881 Must also handle REFERENCE_TYPEs for C++. */
2883 tree
2884 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2885 tsubst_flags_t complain)
2887 tree orig_expr = expr;
2888 tree rval;
2890 if (processing_template_decl)
2892 /* Retain the type if we know the operand is a pointer. */
2893 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2894 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2895 if (type_dependent_expression_p (expr))
2896 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2897 expr = build_non_dependent_expr (expr);
2900 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2901 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2902 if (!rval)
2903 rval = cp_build_indirect_ref (expr, errorstring, complain);
2905 if (processing_template_decl && rval != error_mark_node)
2906 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2907 else
2908 return rval;
2911 /* Helper function called from c-common. */
2912 tree
2913 build_indirect_ref (location_t /*loc*/,
2914 tree ptr, ref_operator errorstring)
2916 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2919 tree
2920 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2921 tsubst_flags_t complain)
2923 tree pointer, type;
2925 if (ptr == current_class_ptr
2926 || (TREE_CODE (ptr) == NOP_EXPR
2927 && TREE_OPERAND (ptr, 0) == current_class_ptr
2928 && (same_type_ignoring_top_level_qualifiers_p
2929 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2930 return current_class_ref;
2932 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2933 ? ptr : decay_conversion (ptr, complain));
2934 if (pointer == error_mark_node)
2935 return error_mark_node;
2937 type = TREE_TYPE (pointer);
2939 if (POINTER_TYPE_P (type))
2941 /* [expr.unary.op]
2943 If the type of the expression is "pointer to T," the type
2944 of the result is "T." */
2945 tree t = TREE_TYPE (type);
2947 if ((CONVERT_EXPR_P (ptr)
2948 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2949 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2951 /* If a warning is issued, mark it to avoid duplicates from
2952 the backend. This only needs to be done at
2953 warn_strict_aliasing > 2. */
2954 if (warn_strict_aliasing > 2)
2955 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2956 type, TREE_OPERAND (ptr, 0)))
2957 TREE_NO_WARNING (ptr) = 1;
2960 if (VOID_TYPE_P (t))
2962 /* A pointer to incomplete type (other than cv void) can be
2963 dereferenced [expr.unary.op]/1 */
2964 if (complain & tf_error)
2965 error ("%qT is not a pointer-to-object type", type);
2966 return error_mark_node;
2968 else if (TREE_CODE (pointer) == ADDR_EXPR
2969 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2970 /* The POINTER was something like `&x'. We simplify `*&x' to
2971 `x'. */
2972 return TREE_OPERAND (pointer, 0);
2973 else
2975 tree ref = build1 (INDIRECT_REF, t, pointer);
2977 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2978 so that we get the proper error message if the result is used
2979 to assign to. Also, &* is supposed to be a no-op. */
2980 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2981 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2982 TREE_SIDE_EFFECTS (ref)
2983 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2984 return ref;
2987 else if (!(complain & tf_error))
2988 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2990 /* `pointer' won't be an error_mark_node if we were given a
2991 pointer to member, so it's cool to check for this here. */
2992 else if (TYPE_PTRMEM_P (type))
2993 switch (errorstring)
2995 case RO_ARRAY_INDEXING:
2996 error ("invalid use of array indexing on pointer to member");
2997 break;
2998 case RO_UNARY_STAR:
2999 error ("invalid use of unary %<*%> on pointer to member");
3000 break;
3001 case RO_IMPLICIT_CONVERSION:
3002 error ("invalid use of implicit conversion on pointer to member");
3003 break;
3004 case RO_ARROW_STAR:
3005 error ("left hand operand of %<->*%> must be a pointer to class, "
3006 "but is a pointer to member of type %qT", type);
3007 break;
3008 default:
3009 gcc_unreachable ();
3011 else if (pointer != error_mark_node)
3012 invalid_indirection_error (input_location, type, errorstring);
3014 return error_mark_node;
3017 /* This handles expressions of the form "a[i]", which denotes
3018 an array reference.
3020 This is logically equivalent in C to *(a+i), but we may do it differently.
3021 If A is a variable or a member, we generate a primitive ARRAY_REF.
3022 This avoids forcing the array out of registers, and can work on
3023 arrays that are not lvalues (for example, members of structures returned
3024 by functions).
3026 If INDEX is of some user-defined type, it must be converted to
3027 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3028 will inherit the type of the array, which will be some pointer type.
3030 LOC is the location to use in building the array reference. */
3032 tree
3033 cp_build_array_ref (location_t loc, tree array, tree idx,
3034 tsubst_flags_t complain)
3036 tree ret;
3038 if (idx == 0)
3040 if (complain & tf_error)
3041 error_at (loc, "subscript missing in array reference");
3042 return error_mark_node;
3045 /* If an array's index is an array notation, then its rank cannot be
3046 greater than one. */
3047 if (flag_cilkplus && contains_array_notation_expr (idx))
3049 size_t rank = 0;
3051 /* If find_rank returns false, then it should have reported an error,
3052 thus it is unnecessary for repetition. */
3053 if (!find_rank (loc, idx, idx, true, &rank))
3054 return error_mark_node;
3055 if (rank > 1)
3057 error_at (loc, "rank of the array%'s index is greater than 1");
3058 return error_mark_node;
3061 if (TREE_TYPE (array) == error_mark_node
3062 || TREE_TYPE (idx) == error_mark_node)
3063 return error_mark_node;
3065 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3066 inside it. */
3067 switch (TREE_CODE (array))
3069 case COMPOUND_EXPR:
3071 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3072 complain);
3073 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3074 TREE_OPERAND (array, 0), value);
3075 SET_EXPR_LOCATION (ret, loc);
3076 return ret;
3079 case COND_EXPR:
3080 ret = build_conditional_expr
3081 (loc, TREE_OPERAND (array, 0),
3082 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3083 complain),
3084 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3085 complain),
3086 complain);
3087 protected_set_expr_location (ret, loc);
3088 return ret;
3090 default:
3091 break;
3094 bool non_lvalue
3095 = convert_vector_to_pointer_for_subscript (loc, &array, idx);
3097 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3099 tree rval, type;
3101 warn_array_subscript_with_type_char (loc, idx);
3103 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3105 if (complain & tf_error)
3106 error_at (loc, "array subscript is not an integer");
3107 return error_mark_node;
3110 /* Apply integral promotions *after* noticing character types.
3111 (It is unclear why we do these promotions -- the standard
3112 does not say that we should. In fact, the natural thing would
3113 seem to be to convert IDX to ptrdiff_t; we're performing
3114 pointer arithmetic.) */
3115 idx = cp_perform_integral_promotions (idx, complain);
3117 /* An array that is indexed by a non-constant
3118 cannot be stored in a register; we must be able to do
3119 address arithmetic on its address.
3120 Likewise an array of elements of variable size. */
3121 if (TREE_CODE (idx) != INTEGER_CST
3122 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3123 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3124 != INTEGER_CST)))
3126 if (!cxx_mark_addressable (array))
3127 return error_mark_node;
3130 /* An array that is indexed by a constant value which is not within
3131 the array bounds cannot be stored in a register either; because we
3132 would get a crash in store_bit_field/extract_bit_field when trying
3133 to access a non-existent part of the register. */
3134 if (TREE_CODE (idx) == INTEGER_CST
3135 && TYPE_DOMAIN (TREE_TYPE (array))
3136 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3138 if (!cxx_mark_addressable (array))
3139 return error_mark_node;
3142 /* Note in C++ it is valid to subscript a `register' array, since
3143 it is valid to take the address of something with that
3144 storage specification. */
3145 if (extra_warnings)
3147 tree foo = array;
3148 while (TREE_CODE (foo) == COMPONENT_REF)
3149 foo = TREE_OPERAND (foo, 0);
3150 if (VAR_P (foo) && DECL_REGISTER (foo)
3151 && (complain & tf_warning))
3152 warning_at (loc, OPT_Wextra,
3153 "subscripting array declared %<register%>");
3156 type = TREE_TYPE (TREE_TYPE (array));
3157 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3158 /* Array ref is const/volatile if the array elements are
3159 or if the array is.. */
3160 TREE_READONLY (rval)
3161 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3162 TREE_SIDE_EFFECTS (rval)
3163 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3164 TREE_THIS_VOLATILE (rval)
3165 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3166 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3167 complain);
3168 protected_set_expr_location (ret, loc);
3169 if (non_lvalue)
3170 ret = non_lvalue_loc (loc, ret);
3171 return ret;
3175 tree ar = cp_default_conversion (array, complain);
3176 tree ind = cp_default_conversion (idx, complain);
3178 /* Put the integer in IND to simplify error checking. */
3179 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3180 std::swap (ar, ind);
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 (loc, 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 if (non_lvalue)
3207 ret = non_lvalue_loc (loc, ret);
3208 return ret;
3212 /* Entry point for Obj-C++. */
3214 tree
3215 build_array_ref (location_t loc, tree array, tree idx)
3217 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3220 /* Resolve a pointer to member function. INSTANCE is the object
3221 instance to use, if the member points to a virtual member.
3223 This used to avoid checking for virtual functions if basetype
3224 has no virtual functions, according to an earlier ANSI draft.
3225 With the final ISO C++ rules, such an optimization is
3226 incorrect: A pointer to a derived member can be static_cast
3227 to pointer-to-base-member, as long as the dynamic object
3228 later has the right member. So now we only do this optimization
3229 when we know the dynamic type of the object. */
3231 tree
3232 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3233 tsubst_flags_t complain)
3235 if (TREE_CODE (function) == OFFSET_REF)
3236 function = TREE_OPERAND (function, 1);
3238 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3240 tree idx, delta, e1, e2, e3, vtbl;
3241 bool nonvirtual;
3242 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3243 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3245 tree instance_ptr = *instance_ptrptr;
3246 tree instance_save_expr = 0;
3247 if (instance_ptr == error_mark_node)
3249 if (TREE_CODE (function) == PTRMEM_CST)
3251 /* Extracting the function address from a pmf is only
3252 allowed with -Wno-pmf-conversions. It only works for
3253 pmf constants. */
3254 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3255 e1 = convert (fntype, e1);
3256 return e1;
3258 else
3260 if (complain & tf_error)
3261 error ("object missing in use of %qE", function);
3262 return error_mark_node;
3266 /* True if we know that the dynamic type of the object doesn't have
3267 virtual functions, so we can assume the PFN field is a pointer. */
3268 nonvirtual = (COMPLETE_TYPE_P (basetype)
3269 && !TYPE_POLYMORPHIC_P (basetype)
3270 && resolves_to_fixed_type_p (instance_ptr, 0));
3272 /* If we don't really have an object (i.e. in an ill-formed
3273 conversion from PMF to pointer), we can't resolve virtual
3274 functions anyway. */
3275 if (!nonvirtual && is_dummy_object (instance_ptr))
3276 nonvirtual = true;
3278 if (TREE_SIDE_EFFECTS (instance_ptr))
3279 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3281 if (TREE_SIDE_EFFECTS (function))
3282 function = save_expr (function);
3284 /* Start by extracting all the information from the PMF itself. */
3285 e3 = pfn_from_ptrmemfunc (function);
3286 delta = delta_from_ptrmemfunc (function);
3287 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3288 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3290 int flag_sanitize_save;
3291 case ptrmemfunc_vbit_in_pfn:
3292 e1 = cp_build_binary_op (input_location,
3293 BIT_AND_EXPR, idx, integer_one_node,
3294 complain);
3295 idx = cp_build_binary_op (input_location,
3296 MINUS_EXPR, idx, integer_one_node,
3297 complain);
3298 if (idx == error_mark_node)
3299 return error_mark_node;
3300 break;
3302 case ptrmemfunc_vbit_in_delta:
3303 e1 = cp_build_binary_op (input_location,
3304 BIT_AND_EXPR, delta, integer_one_node,
3305 complain);
3306 /* Don't instrument the RSHIFT_EXPR we're about to create because
3307 we're going to use DELTA number of times, and that wouldn't play
3308 well with SAVE_EXPRs therein. */
3309 flag_sanitize_save = flag_sanitize;
3310 flag_sanitize = 0;
3311 delta = cp_build_binary_op (input_location,
3312 RSHIFT_EXPR, delta, integer_one_node,
3313 complain);
3314 flag_sanitize = flag_sanitize_save;
3315 if (delta == error_mark_node)
3316 return error_mark_node;
3317 break;
3319 default:
3320 gcc_unreachable ();
3323 if (e1 == error_mark_node)
3324 return error_mark_node;
3326 /* Convert down to the right base before using the instance. A
3327 special case is that in a pointer to member of class C, C may
3328 be incomplete. In that case, the function will of course be
3329 a member of C, and no conversion is required. In fact,
3330 lookup_base will fail in that case, because incomplete
3331 classes do not have BINFOs. */
3332 if (!same_type_ignoring_top_level_qualifiers_p
3333 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3335 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3336 basetype, ba_check, NULL, complain);
3337 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3338 1, complain);
3339 if (instance_ptr == error_mark_node)
3340 return error_mark_node;
3342 /* ...and then the delta in the PMF. */
3343 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3345 /* Hand back the adjusted 'this' argument to our caller. */
3346 *instance_ptrptr = instance_ptr;
3348 if (nonvirtual)
3349 /* Now just return the pointer. */
3350 return e3;
3352 /* Next extract the vtable pointer from the object. */
3353 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3354 instance_ptr);
3355 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3356 if (vtbl == error_mark_node)
3357 return error_mark_node;
3359 /* Finally, extract the function pointer from the vtable. */
3360 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3361 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3362 if (e2 == error_mark_node)
3363 return error_mark_node;
3364 TREE_CONSTANT (e2) = 1;
3366 /* When using function descriptors, the address of the
3367 vtable entry is treated as a function pointer. */
3368 if (TARGET_VTABLE_USES_DESCRIPTORS)
3369 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3370 cp_build_addr_expr (e2, complain));
3372 e2 = fold_convert (TREE_TYPE (e3), e2);
3373 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3374 if (e1 == error_mark_node)
3375 return error_mark_node;
3377 /* Make sure this doesn't get evaluated first inside one of the
3378 branches of the COND_EXPR. */
3379 if (instance_save_expr)
3380 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3381 instance_save_expr, e1);
3383 function = e1;
3385 return function;
3388 /* Used by the C-common bits. */
3389 tree
3390 build_function_call (location_t /*loc*/,
3391 tree function, tree params)
3393 return cp_build_function_call (function, params, tf_warning_or_error);
3396 /* Used by the C-common bits. */
3397 tree
3398 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3399 tree function, vec<tree, va_gc> *params,
3400 vec<tree, va_gc> * /*origtypes*/)
3402 vec<tree, va_gc> *orig_params = params;
3403 tree ret = cp_build_function_call_vec (function, &params,
3404 tf_warning_or_error);
3406 /* cp_build_function_call_vec can reallocate PARAMS by adding
3407 default arguments. That should never happen here. Verify
3408 that. */
3409 gcc_assert (params == orig_params);
3411 return ret;
3414 /* Build a function call using a tree list of arguments. */
3416 static tree
3417 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3419 vec<tree, va_gc> *vec;
3420 tree ret;
3422 vec = make_tree_vector ();
3423 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3424 vec_safe_push (vec, TREE_VALUE (params));
3425 ret = cp_build_function_call_vec (function, &vec, complain);
3426 release_tree_vector (vec);
3427 return ret;
3430 /* Build a function call using varargs. */
3432 tree
3433 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3435 vec<tree, va_gc> *vec;
3436 va_list args;
3437 tree ret, t;
3439 vec = make_tree_vector ();
3440 va_start (args, complain);
3441 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3442 vec_safe_push (vec, t);
3443 va_end (args);
3444 ret = cp_build_function_call_vec (function, &vec, complain);
3445 release_tree_vector (vec);
3446 return ret;
3449 /* Build a function call using a vector of arguments. PARAMS may be
3450 NULL if there are no parameters. This changes the contents of
3451 PARAMS. */
3453 tree
3454 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3455 tsubst_flags_t complain)
3457 tree fntype, fndecl;
3458 int is_method;
3459 tree original = function;
3460 int nargs;
3461 tree *argarray;
3462 tree parm_types;
3463 vec<tree, va_gc> *allocated = NULL;
3464 tree ret;
3466 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3467 expressions, like those used for ObjC messenger dispatches. */
3468 if (params != NULL && !vec_safe_is_empty (*params))
3469 function = objc_rewrite_function_call (function, (**params)[0]);
3471 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3472 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3473 if (TREE_CODE (function) == NOP_EXPR
3474 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3475 function = TREE_OPERAND (function, 0);
3477 if (TREE_CODE (function) == FUNCTION_DECL)
3479 /* If the function is a non-template member function
3480 or a non-template friend, then we need to check the
3481 constraints.
3483 Note that if overload resolution failed with a single
3484 candidate this function will be used to explicitly diagnose
3485 the failure for the single call expression. The check is
3486 technically redundant since we also would have failed in
3487 add_function_candidate. */
3488 if (flag_concepts
3489 && (complain & tf_error)
3490 && !constraints_satisfied_p (function))
3492 error ("cannot call function %qD", function);
3493 location_t loc = DECL_SOURCE_LOCATION (function);
3494 diagnose_constraints (loc, function, NULL_TREE);
3495 return error_mark_node;
3498 if (!mark_used (function, complain) && !(complain & tf_error))
3499 return error_mark_node;
3500 fndecl = function;
3502 /* Convert anything with function type to a pointer-to-function. */
3503 if (DECL_MAIN_P (function))
3505 if (complain & tf_error)
3506 pedwarn (input_location, OPT_Wpedantic,
3507 "ISO C++ forbids calling %<::main%> from within program");
3508 else
3509 return error_mark_node;
3511 function = build_addr_func (function, complain);
3513 else
3515 fndecl = NULL_TREE;
3517 function = build_addr_func (function, complain);
3520 if (function == error_mark_node)
3521 return error_mark_node;
3523 fntype = TREE_TYPE (function);
3525 if (TYPE_PTRMEMFUNC_P (fntype))
3527 if (complain & tf_error)
3528 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3529 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3530 original, original);
3531 return error_mark_node;
3534 is_method = (TYPE_PTR_P (fntype)
3535 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3537 if (!(TYPE_PTRFN_P (fntype)
3538 || is_method
3539 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3541 if (complain & tf_error)
3543 if (!flag_diagnostics_show_caret)
3544 error_at (input_location,
3545 "%qE cannot be used as a function", original);
3546 else if (DECL_P (original))
3547 error_at (input_location,
3548 "%qD cannot be used as a function", original);
3549 else
3550 error_at (input_location,
3551 "expression cannot be used as a function");
3554 return error_mark_node;
3557 /* fntype now gets the type of function pointed to. */
3558 fntype = TREE_TYPE (fntype);
3559 parm_types = TYPE_ARG_TYPES (fntype);
3561 if (params == NULL)
3563 allocated = make_tree_vector ();
3564 params = &allocated;
3567 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3568 complain);
3569 if (nargs < 0)
3570 return error_mark_node;
3572 argarray = (*params)->address ();
3574 /* Check for errors in format strings and inappropriately
3575 null parameters. */
3576 check_function_arguments (fntype, nargs, argarray);
3578 ret = build_cxx_call (function, nargs, argarray, complain);
3580 if (allocated != NULL)
3581 release_tree_vector (allocated);
3583 return ret;
3586 /* Subroutine of convert_arguments.
3587 Print an error message about a wrong number of arguments. */
3589 static void
3590 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3592 if (fndecl)
3594 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3596 if (DECL_NAME (fndecl) == NULL_TREE
3597 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3598 error_at (loc,
3599 too_many_p
3600 ? G_("too many arguments to constructor %q#D")
3601 : G_("too few arguments to constructor %q#D"),
3602 fndecl);
3603 else
3604 error_at (loc,
3605 too_many_p
3606 ? G_("too many arguments to member function %q#D")
3607 : G_("too few arguments to member function %q#D"),
3608 fndecl);
3610 else
3611 error_at (loc,
3612 too_many_p
3613 ? G_("too many arguments to function %q#D")
3614 : G_("too few arguments to function %q#D"),
3615 fndecl);
3616 if (!DECL_IS_BUILTIN (fndecl))
3617 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3619 else
3621 if (c_dialect_objc () && objc_message_selector ())
3622 error_at (loc,
3623 too_many_p
3624 ? G_("too many arguments to method %q#D")
3625 : G_("too few arguments to method %q#D"),
3626 objc_message_selector ());
3627 else
3628 error_at (loc, too_many_p ? G_("too many arguments to function")
3629 : G_("too few arguments to function"));
3633 /* Convert the actual parameter expressions in the list VALUES to the
3634 types in the list TYPELIST. The converted expressions are stored
3635 back in the VALUES vector.
3636 If parmdecls is exhausted, or when an element has NULL as its type,
3637 perform the default conversions.
3639 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3641 This is also where warnings about wrong number of args are generated.
3643 Returns the actual number of arguments processed (which might be less
3644 than the length of the vector), or -1 on error.
3646 In C++, unspecified trailing parameters can be filled in with their
3647 default arguments, if such were specified. Do so here. */
3649 static int
3650 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3651 int flags, tsubst_flags_t complain)
3653 tree typetail;
3654 unsigned int i;
3656 /* Argument passing is always copy-initialization. */
3657 flags |= LOOKUP_ONLYCONVERTING;
3659 for (i = 0, typetail = typelist;
3660 i < vec_safe_length (*values);
3661 i++)
3663 tree type = typetail ? TREE_VALUE (typetail) : 0;
3664 tree val = (**values)[i];
3666 if (val == error_mark_node || type == error_mark_node)
3667 return -1;
3669 if (type == void_type_node)
3671 if (complain & tf_error)
3673 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3674 return i;
3676 else
3677 return -1;
3680 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3681 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3682 if (TREE_CODE (val) == NOP_EXPR
3683 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3684 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3685 val = TREE_OPERAND (val, 0);
3687 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3689 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3690 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3691 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3692 val = decay_conversion (val, complain);
3695 if (val == error_mark_node)
3696 return -1;
3698 if (type != 0)
3700 /* Formal parm type is specified by a function prototype. */
3701 tree parmval;
3703 if (!COMPLETE_TYPE_P (complete_type (type)))
3705 if (complain & tf_error)
3707 if (fndecl)
3708 error ("parameter %P of %qD has incomplete type %qT",
3709 i, fndecl, type);
3710 else
3711 error ("parameter %P has incomplete type %qT", i, type);
3713 parmval = error_mark_node;
3715 else
3717 parmval = convert_for_initialization
3718 (NULL_TREE, type, val, flags,
3719 ICR_ARGPASS, fndecl, i, complain);
3720 parmval = convert_for_arg_passing (type, parmval, complain);
3723 if (parmval == error_mark_node)
3724 return -1;
3726 (**values)[i] = parmval;
3728 else
3730 if (fndecl && magic_varargs_p (fndecl))
3731 /* Don't do ellipsis conversion for __built_in_constant_p
3732 as this will result in spurious errors for non-trivial
3733 types. */
3734 val = require_complete_type_sfinae (val, complain);
3735 else
3736 val = convert_arg_to_ellipsis (val, complain);
3738 (**values)[i] = val;
3741 if (typetail)
3742 typetail = TREE_CHAIN (typetail);
3745 if (typetail != 0 && typetail != void_list_node)
3747 /* See if there are default arguments that can be used. Because
3748 we hold default arguments in the FUNCTION_TYPE (which is so
3749 wrong), we can see default parameters here from deduced
3750 contexts (and via typeof) for indirect function calls.
3751 Fortunately we know whether we have a function decl to
3752 provide default arguments in a language conformant
3753 manner. */
3754 if (fndecl && TREE_PURPOSE (typetail)
3755 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3757 for (; typetail != void_list_node; ++i)
3759 tree parmval
3760 = convert_default_arg (TREE_VALUE (typetail),
3761 TREE_PURPOSE (typetail),
3762 fndecl, i, complain);
3764 if (parmval == error_mark_node)
3765 return -1;
3767 vec_safe_push (*values, parmval);
3768 typetail = TREE_CHAIN (typetail);
3769 /* ends with `...'. */
3770 if (typetail == NULL_TREE)
3771 break;
3774 else
3776 if (complain & tf_error)
3777 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3778 return -1;
3782 return (int) i;
3785 /* Build a binary-operation expression, after performing default
3786 conversions on the operands. CODE is the kind of expression to
3787 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3788 are the tree codes which correspond to ARG1 and ARG2 when issuing
3789 warnings about possibly misplaced parentheses. They may differ
3790 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3791 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3792 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3793 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3794 ARG2_CODE as ERROR_MARK. */
3796 tree
3797 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3798 enum tree_code arg1_code, tree arg2,
3799 enum tree_code arg2_code, tree *overload,
3800 tsubst_flags_t complain)
3802 tree orig_arg1;
3803 tree orig_arg2;
3804 tree expr;
3806 orig_arg1 = arg1;
3807 orig_arg2 = arg2;
3809 if (processing_template_decl)
3811 if (type_dependent_expression_p (arg1)
3812 || type_dependent_expression_p (arg2))
3813 return build_min_nt_loc (loc, code, arg1, arg2);
3814 arg1 = build_non_dependent_expr (arg1);
3815 arg2 = build_non_dependent_expr (arg2);
3818 if (code == DOTSTAR_EXPR)
3819 expr = build_m_component_ref (arg1, arg2, complain);
3820 else
3821 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3822 overload, complain);
3824 /* Check for cases such as x+y<<z which users are likely to
3825 misinterpret. But don't warn about obj << x + y, since that is a
3826 common idiom for I/O. */
3827 if (warn_parentheses
3828 && (complain & tf_warning)
3829 && !processing_template_decl
3830 && !error_operand_p (arg1)
3831 && !error_operand_p (arg2)
3832 && (code != LSHIFT_EXPR
3833 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3834 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3835 arg2_code, orig_arg2);
3837 if (processing_template_decl && expr != error_mark_node)
3838 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3840 return expr;
3843 /* Build and return an ARRAY_REF expression. */
3845 tree
3846 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3847 tsubst_flags_t complain)
3849 tree orig_arg1 = arg1;
3850 tree orig_arg2 = arg2;
3851 tree expr;
3853 if (processing_template_decl)
3855 if (type_dependent_expression_p (arg1)
3856 || type_dependent_expression_p (arg2))
3857 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3858 NULL_TREE, NULL_TREE);
3859 arg1 = build_non_dependent_expr (arg1);
3860 arg2 = build_non_dependent_expr (arg2);
3863 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3864 NULL_TREE, /*overload=*/NULL, complain);
3866 if (processing_template_decl && expr != error_mark_node)
3867 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3868 NULL_TREE, NULL_TREE);
3869 return expr;
3872 /* Return whether OP is an expression of enum type cast to integer
3873 type. In C++ even unsigned enum types are cast to signed integer
3874 types. We do not want to issue warnings about comparisons between
3875 signed and unsigned types when one of the types is an enum type.
3876 Those warnings are always false positives in practice. */
3878 static bool
3879 enum_cast_to_int (tree op)
3881 if (CONVERT_EXPR_P (op)
3882 && TREE_TYPE (op) == integer_type_node
3883 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3884 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3885 return true;
3887 /* The cast may have been pushed into a COND_EXPR. */
3888 if (TREE_CODE (op) == COND_EXPR)
3889 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3890 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3892 return false;
3895 /* For the c-common bits. */
3896 tree
3897 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3898 int /*convert_p*/)
3900 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3904 /* Build a binary-operation expression without default conversions.
3905 CODE is the kind of expression to build.
3906 LOCATION is the location_t of the operator in the source code.
3907 This function differs from `build' in several ways:
3908 the data type of the result is computed and recorded in it,
3909 warnings are generated if arg data types are invalid,
3910 special handling for addition and subtraction of pointers is known,
3911 and some optimization is done (operations on narrow ints
3912 are done in the narrower type when that gives the same result).
3913 Constant folding is also done before the result is returned.
3915 Note that the operands will never have enumeral types
3916 because either they have just had the default conversions performed
3917 or they have both just been converted to some other type in which
3918 the arithmetic is to be done.
3920 C++: must do special pointer arithmetic when implementing
3921 multiple inheritance, and deal with pointer to member functions. */
3923 tree
3924 cp_build_binary_op (location_t location,
3925 enum tree_code code, tree orig_op0, tree orig_op1,
3926 tsubst_flags_t complain)
3928 tree op0, op1;
3929 enum tree_code code0, code1;
3930 tree type0, type1;
3931 const char *invalid_op_diag;
3933 /* Expression code to give to the expression when it is built.
3934 Normally this is CODE, which is what the caller asked for,
3935 but in some special cases we change it. */
3936 enum tree_code resultcode = code;
3938 /* Data type in which the computation is to be performed.
3939 In the simplest cases this is the common type of the arguments. */
3940 tree result_type = NULL;
3942 /* Nonzero means operands have already been type-converted
3943 in whatever way is necessary.
3944 Zero means they need to be converted to RESULT_TYPE. */
3945 int converted = 0;
3947 /* Nonzero means create the expression with this type, rather than
3948 RESULT_TYPE. */
3949 tree build_type = 0;
3951 /* Nonzero means after finally constructing the expression
3952 convert it to this type. */
3953 tree final_type = 0;
3955 tree result;
3956 tree orig_type = NULL;
3958 /* Nonzero if this is an operation like MIN or MAX which can
3959 safely be computed in short if both args are promoted shorts.
3960 Also implies COMMON.
3961 -1 indicates a bitwise operation; this makes a difference
3962 in the exact conditions for when it is safe to do the operation
3963 in a narrower mode. */
3964 int shorten = 0;
3966 /* Nonzero if this is a comparison operation;
3967 if both args are promoted shorts, compare the original shorts.
3968 Also implies COMMON. */
3969 int short_compare = 0;
3971 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3972 int common = 0;
3974 /* True if both operands have arithmetic type. */
3975 bool arithmetic_types_p;
3977 /* Apply default conversions. */
3978 op0 = orig_op0;
3979 op1 = orig_op1;
3981 /* Remember whether we're doing / or %. */
3982 bool doing_div_or_mod = false;
3984 /* Remember whether we're doing << or >>. */
3985 bool doing_shift = false;
3987 /* Tree holding instrumentation expression. */
3988 tree instrument_expr = NULL;
3990 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3991 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3992 || code == TRUTH_XOR_EXPR)
3994 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3995 op0 = decay_conversion (op0, complain);
3996 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3997 op1 = decay_conversion (op1, complain);
3999 else
4001 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4002 op0 = cp_default_conversion (op0, complain);
4003 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4004 op1 = cp_default_conversion (op1, complain);
4007 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4008 STRIP_TYPE_NOPS (op0);
4009 STRIP_TYPE_NOPS (op1);
4011 /* DTRT if one side is an overloaded function, but complain about it. */
4012 if (type_unknown_p (op0))
4014 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4015 if (t != error_mark_node)
4017 if (complain & tf_error)
4018 permerror (input_location, "assuming cast to type %qT from overloaded function",
4019 TREE_TYPE (t));
4020 op0 = t;
4023 if (type_unknown_p (op1))
4025 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4026 if (t != error_mark_node)
4028 if (complain & tf_error)
4029 permerror (input_location, "assuming cast to type %qT from overloaded function",
4030 TREE_TYPE (t));
4031 op1 = t;
4035 type0 = TREE_TYPE (op0);
4036 type1 = TREE_TYPE (op1);
4038 /* The expression codes of the data types of the arguments tell us
4039 whether the arguments are integers, floating, pointers, etc. */
4040 code0 = TREE_CODE (type0);
4041 code1 = TREE_CODE (type1);
4043 /* If an error was already reported for one of the arguments,
4044 avoid reporting another error. */
4045 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4046 return error_mark_node;
4048 if ((invalid_op_diag
4049 = targetm.invalid_binary_op (code, type0, type1)))
4051 if (complain & tf_error)
4052 error (invalid_op_diag);
4053 return error_mark_node;
4056 /* Issue warnings about peculiar, but valid, uses of NULL. */
4057 if ((orig_op0 == null_node || orig_op1 == null_node)
4058 /* It's reasonable to use pointer values as operands of &&
4059 and ||, so NULL is no exception. */
4060 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4061 && ( /* Both are NULL (or 0) and the operation was not a
4062 comparison or a pointer subtraction. */
4063 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4064 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4065 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4066 || (!null_ptr_cst_p (orig_op0)
4067 && !TYPE_PTR_OR_PTRMEM_P (type0))
4068 || (!null_ptr_cst_p (orig_op1)
4069 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4070 && (complain & tf_warning))
4072 source_location loc =
4073 expansion_point_location_if_in_system_header (input_location);
4075 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4078 /* In case when one of the operands of the binary operation is
4079 a vector and another is a scalar -- convert scalar to vector. */
4080 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4082 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4083 complain & tf_error);
4085 switch (convert_flag)
4087 case stv_error:
4088 return error_mark_node;
4089 case stv_firstarg:
4091 op0 = convert (TREE_TYPE (type1), op0);
4092 op0 = save_expr (op0);
4093 op0 = build_vector_from_val (type1, op0);
4094 type0 = TREE_TYPE (op0);
4095 code0 = TREE_CODE (type0);
4096 converted = 1;
4097 break;
4099 case stv_secondarg:
4101 op1 = convert (TREE_TYPE (type0), op1);
4102 op1 = save_expr (op1);
4103 op1 = build_vector_from_val (type0, op1);
4104 type1 = TREE_TYPE (op1);
4105 code1 = TREE_CODE (type1);
4106 converted = 1;
4107 break;
4109 default:
4110 break;
4114 switch (code)
4116 case MINUS_EXPR:
4117 /* Subtraction of two similar pointers.
4118 We must subtract them as integers, then divide by object size. */
4119 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4120 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4121 TREE_TYPE (type1)))
4122 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4123 complain);
4124 /* In all other cases except pointer - int, the usual arithmetic
4125 rules apply. */
4126 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4128 common = 1;
4129 break;
4131 /* The pointer - int case is just like pointer + int; fall
4132 through. */
4133 case PLUS_EXPR:
4134 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4135 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4137 tree ptr_operand;
4138 tree int_operand;
4139 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4140 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4141 if (processing_template_decl)
4143 result_type = TREE_TYPE (ptr_operand);
4144 break;
4146 return cp_pointer_int_sum (code,
4147 ptr_operand,
4148 int_operand,
4149 complain);
4151 common = 1;
4152 break;
4154 case MULT_EXPR:
4155 common = 1;
4156 break;
4158 case TRUNC_DIV_EXPR:
4159 case CEIL_DIV_EXPR:
4160 case FLOOR_DIV_EXPR:
4161 case ROUND_DIV_EXPR:
4162 case EXACT_DIV_EXPR:
4163 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4164 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4165 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4166 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4168 enum tree_code tcode0 = code0, tcode1 = code1;
4169 tree cop1 = fold_non_dependent_expr (op1);
4170 doing_div_or_mod = true;
4171 warn_for_div_by_zero (location, cop1);
4173 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4174 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4175 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4176 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4178 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4179 resultcode = RDIV_EXPR;
4180 else
4181 /* When dividing two signed integers, we have to promote to int.
4182 unless we divide by a constant != -1. Note that default
4183 conversion will have been performed on the operands at this
4184 point, so we have to dig out the original type to find out if
4185 it was unsigned. */
4186 shorten = ((TREE_CODE (op0) == NOP_EXPR
4187 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4188 || (TREE_CODE (op1) == INTEGER_CST
4189 && ! integer_all_onesp (op1)));
4191 common = 1;
4193 break;
4195 case BIT_AND_EXPR:
4196 case BIT_IOR_EXPR:
4197 case BIT_XOR_EXPR:
4198 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4199 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4200 && !VECTOR_FLOAT_TYPE_P (type0)
4201 && !VECTOR_FLOAT_TYPE_P (type1)))
4202 shorten = -1;
4203 break;
4205 case TRUNC_MOD_EXPR:
4206 case FLOOR_MOD_EXPR:
4208 tree cop1 = fold_non_dependent_expr (op1);
4209 doing_div_or_mod = true;
4210 warn_for_div_by_zero (location, cop1);
4213 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4214 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4215 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4216 common = 1;
4217 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4219 /* Although it would be tempting to shorten always here, that loses
4220 on some targets, since the modulo instruction is undefined if the
4221 quotient can't be represented in the computation mode. We shorten
4222 only if unsigned or if dividing by something we know != -1. */
4223 shorten = ((TREE_CODE (op0) == NOP_EXPR
4224 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4225 || (TREE_CODE (op1) == INTEGER_CST
4226 && ! integer_all_onesp (op1)));
4227 common = 1;
4229 break;
4231 case TRUTH_ANDIF_EXPR:
4232 case TRUTH_ORIF_EXPR:
4233 case TRUTH_AND_EXPR:
4234 case TRUTH_OR_EXPR:
4235 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4237 if (!COMPARISON_CLASS_P (op1))
4238 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4239 build_zero_cst (type1), complain);
4240 if (code == TRUTH_ANDIF_EXPR)
4242 tree z = build_zero_cst (TREE_TYPE (op1));
4243 return build_conditional_expr (location, op0, op1, z, complain);
4245 else if (code == TRUTH_ORIF_EXPR)
4247 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4248 return build_conditional_expr (location, op0, m1, op1, complain);
4250 else
4251 gcc_unreachable ();
4253 if (VECTOR_TYPE_P (type0))
4255 if (!COMPARISON_CLASS_P (op0))
4256 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4257 build_zero_cst (type0), complain);
4258 if (!VECTOR_TYPE_P (type1))
4260 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4261 tree z = build_zero_cst (TREE_TYPE (op0));
4262 op1 = build_conditional_expr (location, op1, z, m1, complain);
4264 else if (!COMPARISON_CLASS_P (op1))
4265 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4266 build_zero_cst (type1), complain);
4268 if (code == TRUTH_ANDIF_EXPR)
4269 code = BIT_AND_EXPR;
4270 else if (code == TRUTH_ORIF_EXPR)
4271 code = BIT_IOR_EXPR;
4272 else
4273 gcc_unreachable ();
4275 return cp_build_binary_op (location, code, op0, op1, complain);
4278 result_type = boolean_type_node;
4279 break;
4281 /* Shift operations: result has same type as first operand;
4282 always convert second operand to int.
4283 Also set SHORT_SHIFT if shifting rightward. */
4285 case RSHIFT_EXPR:
4286 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4287 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4289 result_type = type0;
4290 converted = 1;
4292 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4293 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4294 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4295 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4297 result_type = type0;
4298 converted = 1;
4300 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4302 tree const_op1 = fold_non_dependent_expr (op1);
4303 if (TREE_CODE (const_op1) != INTEGER_CST)
4304 const_op1 = op1;
4305 result_type = type0;
4306 doing_shift = true;
4307 if (TREE_CODE (const_op1) == INTEGER_CST)
4309 if (tree_int_cst_lt (const_op1, integer_zero_node))
4311 if ((complain & tf_warning)
4312 && c_inhibit_evaluation_warnings == 0)
4313 warning (OPT_Wshift_count_negative,
4314 "right shift count is negative");
4316 else
4318 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4319 && (complain & tf_warning)
4320 && c_inhibit_evaluation_warnings == 0)
4321 warning (OPT_Wshift_count_overflow,
4322 "right shift count >= width of type");
4325 /* Avoid converting op1 to result_type later. */
4326 converted = 1;
4328 break;
4330 case LSHIFT_EXPR:
4331 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4332 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4334 result_type = type0;
4335 converted = 1;
4337 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4338 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4339 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4340 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4342 result_type = type0;
4343 converted = 1;
4345 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4347 tree const_op0 = fold_non_dependent_expr (op0);
4348 if (TREE_CODE (const_op0) != INTEGER_CST)
4349 const_op0 = op0;
4350 tree const_op1 = fold_non_dependent_expr (op1);
4351 if (TREE_CODE (const_op1) != INTEGER_CST)
4352 const_op1 = op1;
4353 result_type = type0;
4354 doing_shift = true;
4355 if (TREE_CODE (const_op0) == INTEGER_CST
4356 && tree_int_cst_sgn (const_op0) < 0
4357 && (complain & tf_warning)
4358 && c_inhibit_evaluation_warnings == 0)
4359 warning (OPT_Wshift_negative_value,
4360 "left shift of negative value");
4361 if (TREE_CODE (const_op1) == INTEGER_CST)
4363 if (tree_int_cst_lt (const_op1, integer_zero_node))
4365 if ((complain & tf_warning)
4366 && c_inhibit_evaluation_warnings == 0)
4367 warning (OPT_Wshift_count_negative,
4368 "left shift count is negative");
4370 else if (compare_tree_int (const_op1,
4371 TYPE_PRECISION (type0)) >= 0)
4373 if ((complain & tf_warning)
4374 && c_inhibit_evaluation_warnings == 0)
4375 warning (OPT_Wshift_count_overflow,
4376 "left shift count >= width of type");
4378 else if (TREE_CODE (const_op0) == INTEGER_CST
4379 && (complain & tf_warning))
4380 maybe_warn_shift_overflow (location, const_op0, const_op1);
4382 /* Avoid converting op1 to result_type later. */
4383 converted = 1;
4385 break;
4387 case RROTATE_EXPR:
4388 case LROTATE_EXPR:
4389 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4391 result_type = type0;
4392 if (TREE_CODE (op1) == INTEGER_CST)
4394 if (tree_int_cst_lt (op1, integer_zero_node))
4396 if (complain & tf_warning)
4397 warning (0, (code == LROTATE_EXPR)
4398 ? G_("left rotate count is negative")
4399 : G_("right rotate count is negative"));
4401 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4403 if (complain & tf_warning)
4404 warning (0, (code == LROTATE_EXPR)
4405 ? G_("left rotate count >= width of type")
4406 : G_("right rotate count >= width of type"));
4409 /* Convert the shift-count to an integer, regardless of
4410 size of value being shifted. */
4411 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4412 op1 = cp_convert (integer_type_node, op1, complain);
4414 break;
4416 case EQ_EXPR:
4417 case NE_EXPR:
4418 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4419 goto vector_compare;
4420 if ((complain & tf_warning)
4421 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4422 warning (OPT_Wfloat_equal,
4423 "comparing floating point with == or != is unsafe");
4424 if ((complain & tf_warning)
4425 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4426 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4427 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4429 build_type = boolean_type_node;
4430 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4431 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4432 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4433 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4434 short_compare = 1;
4435 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4436 && null_ptr_cst_p (op1))
4437 /* Handle, eg, (void*)0 (c++/43906), and more. */
4438 || (code0 == POINTER_TYPE
4439 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4441 if (TYPE_PTR_P (type1))
4442 result_type = composite_pointer_type (type0, type1, op0, op1,
4443 CPO_COMPARISON, complain);
4444 else
4445 result_type = type0;
4447 if (TREE_CODE (op0) == ADDR_EXPR
4448 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4450 if ((complain & tf_warning)
4451 && c_inhibit_evaluation_warnings == 0
4452 && !TREE_NO_WARNING (op0))
4453 warning (OPT_Waddress, "the address of %qD will never be NULL",
4454 TREE_OPERAND (op0, 0));
4457 if (CONVERT_EXPR_P (op0)
4458 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op0, 0)))
4459 == REFERENCE_TYPE)
4461 tree inner_op0 = op0;
4462 STRIP_NOPS (inner_op0);
4464 if ((complain & tf_warning)
4465 && c_inhibit_evaluation_warnings == 0
4466 && !TREE_NO_WARNING (op0)
4467 && DECL_P (inner_op0))
4468 warning_at (location, OPT_Waddress,
4469 "the compiler can assume that the address of "
4470 "%qD will never be NULL",
4471 inner_op0);
4474 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4475 && null_ptr_cst_p (op0))
4476 /* Handle, eg, (void*)0 (c++/43906), and more. */
4477 || (code1 == POINTER_TYPE
4478 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4480 if (TYPE_PTR_P (type0))
4481 result_type = composite_pointer_type (type0, type1, op0, op1,
4482 CPO_COMPARISON, complain);
4483 else
4484 result_type = type1;
4486 if (TREE_CODE (op1) == ADDR_EXPR
4487 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4489 if ((complain & tf_warning)
4490 && c_inhibit_evaluation_warnings == 0
4491 && !TREE_NO_WARNING (op1))
4492 warning (OPT_Waddress, "the address of %qD will never be NULL",
4493 TREE_OPERAND (op1, 0));
4496 if (CONVERT_EXPR_P (op1)
4497 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op1, 0)))
4498 == REFERENCE_TYPE)
4500 tree inner_op1 = op1;
4501 STRIP_NOPS (inner_op1);
4503 if ((complain & tf_warning)
4504 && c_inhibit_evaluation_warnings == 0
4505 && !TREE_NO_WARNING (op1)
4506 && DECL_P (inner_op1))
4507 warning_at (location, OPT_Waddress,
4508 "the compiler can assume that the address of "
4509 "%qD will never be NULL",
4510 inner_op1);
4513 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4514 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4515 result_type = composite_pointer_type (type0, type1, op0, op1,
4516 CPO_COMPARISON, complain);
4517 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4518 /* One of the operands must be of nullptr_t type. */
4519 result_type = TREE_TYPE (nullptr_node);
4520 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4522 result_type = type0;
4523 if (complain & tf_error)
4524 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4525 else
4526 return error_mark_node;
4528 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4530 result_type = type1;
4531 if (complain & tf_error)
4532 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4533 else
4534 return error_mark_node;
4536 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4538 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4539 == ptrmemfunc_vbit_in_delta)
4541 tree pfn0, delta0, e1, e2;
4543 if (TREE_SIDE_EFFECTS (op0))
4544 op0 = save_expr (op0);
4546 pfn0 = pfn_from_ptrmemfunc (op0);
4547 delta0 = delta_from_ptrmemfunc (op0);
4548 e1 = cp_build_binary_op (location,
4549 EQ_EXPR,
4550 pfn0,
4551 build_zero_cst (TREE_TYPE (pfn0)),
4552 complain);
4553 e2 = cp_build_binary_op (location,
4554 BIT_AND_EXPR,
4555 delta0,
4556 integer_one_node,
4557 complain);
4559 if (complain & tf_warning)
4560 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4562 e2 = cp_build_binary_op (location,
4563 EQ_EXPR, e2, integer_zero_node,
4564 complain);
4565 op0 = cp_build_binary_op (location,
4566 TRUTH_ANDIF_EXPR, e1, e2,
4567 complain);
4568 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4570 else
4572 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4573 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4575 result_type = TREE_TYPE (op0);
4577 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4578 return cp_build_binary_op (location, code, op1, op0, complain);
4579 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4581 tree type;
4582 /* E will be the final comparison. */
4583 tree e;
4584 /* E1 and E2 are for scratch. */
4585 tree e1;
4586 tree e2;
4587 tree pfn0;
4588 tree pfn1;
4589 tree delta0;
4590 tree delta1;
4592 type = composite_pointer_type (type0, type1, op0, op1,
4593 CPO_COMPARISON, complain);
4595 if (!same_type_p (TREE_TYPE (op0), type))
4596 op0 = cp_convert_and_check (type, op0, complain);
4597 if (!same_type_p (TREE_TYPE (op1), type))
4598 op1 = cp_convert_and_check (type, op1, complain);
4600 if (op0 == error_mark_node || op1 == error_mark_node)
4601 return error_mark_node;
4603 if (TREE_SIDE_EFFECTS (op0))
4604 op0 = save_expr (op0);
4605 if (TREE_SIDE_EFFECTS (op1))
4606 op1 = save_expr (op1);
4608 pfn0 = pfn_from_ptrmemfunc (op0);
4609 /* Avoid -Waddress warnings (c++/64877). */
4610 if (TREE_CODE (pfn0) == ADDR_EXPR)
4611 TREE_NO_WARNING (pfn0) = 1;
4612 pfn1 = pfn_from_ptrmemfunc (op1);
4613 delta0 = delta_from_ptrmemfunc (op0);
4614 delta1 = delta_from_ptrmemfunc (op1);
4615 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4616 == ptrmemfunc_vbit_in_delta)
4618 /* We generate:
4620 (op0.pfn == op1.pfn
4621 && ((op0.delta == op1.delta)
4622 || (!op0.pfn && op0.delta & 1 == 0
4623 && op1.delta & 1 == 0))
4625 The reason for the `!op0.pfn' bit is that a NULL
4626 pointer-to-member is any member with a zero PFN and
4627 LSB of the DELTA field is 0. */
4629 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4630 delta0,
4631 integer_one_node,
4632 complain);
4633 e1 = cp_build_binary_op (location,
4634 EQ_EXPR, e1, integer_zero_node,
4635 complain);
4636 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4637 delta1,
4638 integer_one_node,
4639 complain);
4640 e2 = cp_build_binary_op (location,
4641 EQ_EXPR, e2, integer_zero_node,
4642 complain);
4643 e1 = cp_build_binary_op (location,
4644 TRUTH_ANDIF_EXPR, e2, e1,
4645 complain);
4646 e2 = cp_build_binary_op (location, EQ_EXPR,
4647 pfn0,
4648 build_zero_cst (TREE_TYPE (pfn0)),
4649 complain);
4650 e2 = cp_build_binary_op (location,
4651 TRUTH_ANDIF_EXPR, e2, e1, complain);
4652 e1 = cp_build_binary_op (location,
4653 EQ_EXPR, delta0, delta1, complain);
4654 e1 = cp_build_binary_op (location,
4655 TRUTH_ORIF_EXPR, e1, e2, complain);
4657 else
4659 /* We generate:
4661 (op0.pfn == op1.pfn
4662 && (!op0.pfn || op0.delta == op1.delta))
4664 The reason for the `!op0.pfn' bit is that a NULL
4665 pointer-to-member is any member with a zero PFN; the
4666 DELTA field is unspecified. */
4668 e1 = cp_build_binary_op (location,
4669 EQ_EXPR, delta0, delta1, complain);
4670 e2 = cp_build_binary_op (location,
4671 EQ_EXPR,
4672 pfn0,
4673 build_zero_cst (TREE_TYPE (pfn0)),
4674 complain);
4675 e1 = cp_build_binary_op (location,
4676 TRUTH_ORIF_EXPR, e1, e2, complain);
4678 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4679 e = cp_build_binary_op (location,
4680 TRUTH_ANDIF_EXPR, e2, e1, complain);
4681 if (code == EQ_EXPR)
4682 return e;
4683 return cp_build_binary_op (location,
4684 EQ_EXPR, e, integer_zero_node, complain);
4686 else
4688 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4689 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4690 type1));
4691 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4692 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4693 type0));
4696 break;
4698 case MAX_EXPR:
4699 case MIN_EXPR:
4700 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4701 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4702 shorten = 1;
4703 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4704 result_type = composite_pointer_type (type0, type1, op0, op1,
4705 CPO_COMPARISON, complain);
4706 break;
4708 case LE_EXPR:
4709 case GE_EXPR:
4710 case LT_EXPR:
4711 case GT_EXPR:
4712 if (TREE_CODE (orig_op0) == STRING_CST
4713 || TREE_CODE (orig_op1) == STRING_CST)
4715 if (complain & tf_warning)
4716 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4719 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4721 vector_compare:
4722 tree intt;
4723 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4724 TREE_TYPE (type1))
4725 && !vector_types_compatible_elements_p (type0, type1))
4727 if (complain & tf_error)
4729 error_at (location, "comparing vectors with different "
4730 "element types");
4731 inform (location, "operand types are %qT and %qT",
4732 type0, type1);
4734 return error_mark_node;
4737 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4739 if (complain & tf_error)
4741 error_at (location, "comparing vectors with different "
4742 "number of elements");
4743 inform (location, "operand types are %qT and %qT",
4744 type0, type1);
4746 return error_mark_node;
4749 /* Always construct signed integer vector type. */
4750 intt = c_common_type_for_size (GET_MODE_BITSIZE
4751 (TYPE_MODE (TREE_TYPE (type0))), 0);
4752 if (!intt)
4754 if (complain & tf_error)
4755 error_at (location, "could not find an integer type "
4756 "of the same size as %qT", TREE_TYPE (type0));
4757 return error_mark_node;
4759 result_type = build_opaque_vector_type (intt,
4760 TYPE_VECTOR_SUBPARTS (type0));
4761 converted = 1;
4762 break;
4764 build_type = boolean_type_node;
4765 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4766 || code0 == ENUMERAL_TYPE)
4767 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4768 || code1 == ENUMERAL_TYPE))
4769 short_compare = 1;
4770 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4771 result_type = composite_pointer_type (type0, type1, op0, op1,
4772 CPO_COMPARISON, complain);
4773 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4775 result_type = type0;
4776 if (extra_warnings && (complain & tf_warning))
4777 warning (OPT_Wextra,
4778 "ordered comparison of pointer with integer zero");
4780 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4782 result_type = type1;
4783 if (extra_warnings && (complain & tf_warning))
4784 warning (OPT_Wextra,
4785 "ordered comparison of pointer with integer zero");
4787 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4788 /* One of the operands must be of nullptr_t type. */
4789 result_type = TREE_TYPE (nullptr_node);
4790 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4792 result_type = type0;
4793 if (complain & tf_error)
4794 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4795 else
4796 return error_mark_node;
4798 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4800 result_type = type1;
4801 if (complain & tf_error)
4802 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4803 else
4804 return error_mark_node;
4806 break;
4808 case UNORDERED_EXPR:
4809 case ORDERED_EXPR:
4810 case UNLT_EXPR:
4811 case UNLE_EXPR:
4812 case UNGT_EXPR:
4813 case UNGE_EXPR:
4814 case UNEQ_EXPR:
4815 build_type = integer_type_node;
4816 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4818 if (complain & tf_error)
4819 error ("unordered comparison on non-floating point argument");
4820 return error_mark_node;
4822 common = 1;
4823 break;
4825 default:
4826 break;
4829 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4830 || code0 == ENUMERAL_TYPE)
4831 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4832 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4833 arithmetic_types_p = 1;
4834 else
4836 arithmetic_types_p = 0;
4837 /* Vector arithmetic is only allowed when both sides are vectors. */
4838 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4840 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4841 || !vector_types_compatible_elements_p (type0, type1))
4843 if (complain & tf_error)
4844 binary_op_error (location, code, type0, type1);
4845 return error_mark_node;
4847 arithmetic_types_p = 1;
4850 /* Determine the RESULT_TYPE, if it is not already known. */
4851 if (!result_type
4852 && arithmetic_types_p
4853 && (shorten || common || short_compare))
4855 result_type = cp_common_type (type0, type1);
4856 if (complain & tf_warning)
4857 do_warn_double_promotion (result_type, type0, type1,
4858 "implicit conversion from %qT to %qT "
4859 "to match other operand of binary "
4860 "expression",
4861 location);
4864 if (!result_type)
4866 if (complain & tf_error)
4867 error ("invalid operands of types %qT and %qT to binary %qO",
4868 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4869 return error_mark_node;
4872 /* If we're in a template, the only thing we need to know is the
4873 RESULT_TYPE. */
4874 if (processing_template_decl)
4876 /* Since the middle-end checks the type when doing a build2, we
4877 need to build the tree in pieces. This built tree will never
4878 get out of the front-end as we replace it when instantiating
4879 the template. */
4880 tree tmp = build2 (resultcode,
4881 build_type ? build_type : result_type,
4882 NULL_TREE, op1);
4883 TREE_OPERAND (tmp, 0) = op0;
4884 return tmp;
4887 if (arithmetic_types_p)
4889 bool first_complex = (code0 == COMPLEX_TYPE);
4890 bool second_complex = (code1 == COMPLEX_TYPE);
4891 int none_complex = (!first_complex && !second_complex);
4893 /* Adapted from patch for c/24581. */
4894 if (first_complex != second_complex
4895 && (code == PLUS_EXPR
4896 || code == MINUS_EXPR
4897 || code == MULT_EXPR
4898 || (code == TRUNC_DIV_EXPR && first_complex))
4899 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4900 && flag_signed_zeros)
4902 /* An operation on mixed real/complex operands must be
4903 handled specially, but the language-independent code can
4904 more easily optimize the plain complex arithmetic if
4905 -fno-signed-zeros. */
4906 tree real_type = TREE_TYPE (result_type);
4907 tree real, imag;
4908 if (first_complex)
4910 if (TREE_TYPE (op0) != result_type)
4911 op0 = cp_convert_and_check (result_type, op0, complain);
4912 if (TREE_TYPE (op1) != real_type)
4913 op1 = cp_convert_and_check (real_type, op1, complain);
4915 else
4917 if (TREE_TYPE (op0) != real_type)
4918 op0 = cp_convert_and_check (real_type, op0, complain);
4919 if (TREE_TYPE (op1) != result_type)
4920 op1 = cp_convert_and_check (result_type, op1, complain);
4922 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4923 return error_mark_node;
4924 if (first_complex)
4926 op0 = save_expr (op0);
4927 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4928 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4929 switch (code)
4931 case MULT_EXPR:
4932 case TRUNC_DIV_EXPR:
4933 op1 = save_expr (op1);
4934 imag = build2 (resultcode, real_type, imag, op1);
4935 /* Fall through. */
4936 case PLUS_EXPR:
4937 case MINUS_EXPR:
4938 real = build2 (resultcode, real_type, real, op1);
4939 break;
4940 default:
4941 gcc_unreachable();
4944 else
4946 op1 = save_expr (op1);
4947 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4948 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4949 switch (code)
4951 case MULT_EXPR:
4952 op0 = save_expr (op0);
4953 imag = build2 (resultcode, real_type, op0, imag);
4954 /* Fall through. */
4955 case PLUS_EXPR:
4956 real = build2 (resultcode, real_type, op0, real);
4957 break;
4958 case MINUS_EXPR:
4959 real = build2 (resultcode, real_type, op0, real);
4960 imag = build1 (NEGATE_EXPR, real_type, imag);
4961 break;
4962 default:
4963 gcc_unreachable();
4966 real = fold_if_not_in_template (real);
4967 imag = fold_if_not_in_template (imag);
4968 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4969 result = fold_if_not_in_template (result);
4970 return result;
4973 /* For certain operations (which identify themselves by shorten != 0)
4974 if both args were extended from the same smaller type,
4975 do the arithmetic in that type and then extend.
4977 shorten !=0 and !=1 indicates a bitwise operation.
4978 For them, this optimization is safe only if
4979 both args are zero-extended or both are sign-extended.
4980 Otherwise, we might change the result.
4981 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4982 but calculated in (unsigned short) it would be (unsigned short)-1. */
4984 if (shorten && none_complex)
4986 orig_type = result_type;
4987 final_type = result_type;
4988 result_type = shorten_binary_op (result_type, op0, op1,
4989 shorten == -1);
4992 /* Comparison operations are shortened too but differently.
4993 They identify themselves by setting short_compare = 1. */
4995 if (short_compare)
4997 /* Don't write &op0, etc., because that would prevent op0
4998 from being kept in a register.
4999 Instead, make copies of the our local variables and
5000 pass the copies by reference, then copy them back afterward. */
5001 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
5002 enum tree_code xresultcode = resultcode;
5003 tree val
5004 = shorten_compare (location, &xop0, &xop1, &xresult_type,
5005 &xresultcode);
5006 if (val != 0)
5007 return cp_convert (boolean_type_node, val, complain);
5008 op0 = xop0, op1 = xop1;
5009 converted = 1;
5010 resultcode = xresultcode;
5013 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5014 && warn_sign_compare
5015 /* Do not warn until the template is instantiated; we cannot
5016 bound the ranges of the arguments until that point. */
5017 && !processing_template_decl
5018 && (complain & tf_warning)
5019 && c_inhibit_evaluation_warnings == 0
5020 /* Even unsigned enum types promote to signed int. We don't
5021 want to issue -Wsign-compare warnings for this case. */
5022 && !enum_cast_to_int (orig_op0)
5023 && !enum_cast_to_int (orig_op1))
5025 tree oop0 = maybe_constant_value (orig_op0);
5026 tree oop1 = maybe_constant_value (orig_op1);
5028 if (TREE_CODE (oop0) != INTEGER_CST)
5029 oop0 = orig_op0;
5030 if (TREE_CODE (oop1) != INTEGER_CST)
5031 oop1 = orig_op1;
5032 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5033 result_type, resultcode);
5037 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5038 Then the expression will be built.
5039 It will be given type FINAL_TYPE if that is nonzero;
5040 otherwise, it will be given type RESULT_TYPE. */
5041 if (! converted)
5043 if (TREE_TYPE (op0) != result_type)
5044 op0 = cp_convert_and_check (result_type, op0, complain);
5045 if (TREE_TYPE (op1) != result_type)
5046 op1 = cp_convert_and_check (result_type, op1, complain);
5048 if (op0 == error_mark_node || op1 == error_mark_node)
5049 return error_mark_node;
5052 if (build_type == NULL_TREE)
5053 build_type = result_type;
5055 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
5056 | SANITIZE_FLOAT_DIVIDE))
5057 && !processing_template_decl
5058 && do_ubsan_in_current_function ()
5059 && (doing_div_or_mod || doing_shift))
5061 /* OP0 and/or OP1 might have side-effects. */
5062 op0 = cp_save_expr (op0);
5063 op1 = cp_save_expr (op1);
5064 op0 = fold_non_dependent_expr (op0);
5065 op1 = fold_non_dependent_expr (op1);
5066 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
5067 | SANITIZE_FLOAT_DIVIDE)))
5069 /* For diagnostics we want to use the promoted types without
5070 shorten_binary_op. So convert the arguments to the
5071 original result_type. */
5072 tree cop0 = op0;
5073 tree cop1 = op1;
5074 if (orig_type != NULL && result_type != orig_type)
5076 cop0 = cp_convert (orig_type, op0, complain);
5077 cop1 = cp_convert (orig_type, op1, complain);
5079 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5081 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5082 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5085 result = build2 (resultcode, build_type, op0, op1);
5086 result = fold_if_not_in_template (result);
5087 if (final_type != 0)
5088 result = cp_convert (final_type, result, complain);
5090 if (TREE_OVERFLOW_P (result)
5091 && !TREE_OVERFLOW_P (op0)
5092 && !TREE_OVERFLOW_P (op1))
5093 overflow_warning (location, result);
5095 if (instrument_expr != NULL)
5096 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
5097 instrument_expr, result);
5099 return result;
5102 /* Build a VEC_PERM_EXPR.
5103 This is a simple wrapper for c_build_vec_perm_expr. */
5104 tree
5105 build_x_vec_perm_expr (location_t loc,
5106 tree arg0, tree arg1, tree arg2,
5107 tsubst_flags_t complain)
5109 tree orig_arg0 = arg0;
5110 tree orig_arg1 = arg1;
5111 tree orig_arg2 = arg2;
5112 if (processing_template_decl)
5114 if (type_dependent_expression_p (arg0)
5115 || type_dependent_expression_p (arg1)
5116 || type_dependent_expression_p (arg2))
5117 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5118 arg0 = build_non_dependent_expr (arg0);
5119 if (arg1)
5120 arg1 = build_non_dependent_expr (arg1);
5121 arg2 = build_non_dependent_expr (arg2);
5123 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5124 if (processing_template_decl && exp != error_mark_node)
5125 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5126 orig_arg1, orig_arg2);
5127 return exp;
5130 /* Return a tree for the sum or difference (RESULTCODE says which)
5131 of pointer PTROP and integer INTOP. */
5133 static tree
5134 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5135 tsubst_flags_t complain)
5137 tree res_type = TREE_TYPE (ptrop);
5139 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5140 in certain circumstance (when it's valid to do so). So we need
5141 to make sure it's complete. We don't need to check here, if we
5142 can actually complete it at all, as those checks will be done in
5143 pointer_int_sum() anyway. */
5144 complete_type (TREE_TYPE (res_type));
5146 return pointer_int_sum (input_location, resultcode, ptrop,
5147 fold_if_not_in_template (intop),
5148 complain & tf_warning_or_error);
5151 /* Return a tree for the difference of pointers OP0 and OP1.
5152 The resulting tree has type int. */
5154 static tree
5155 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5157 tree result;
5158 tree restype = ptrdiff_type_node;
5159 tree target_type = TREE_TYPE (ptrtype);
5161 if (!complete_type_or_else (target_type, NULL_TREE))
5162 return error_mark_node;
5164 if (VOID_TYPE_P (target_type))
5166 if (complain & tf_error)
5167 permerror (input_location, "ISO C++ forbids using pointer of "
5168 "type %<void *%> in subtraction");
5169 else
5170 return error_mark_node;
5172 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5174 if (complain & tf_error)
5175 permerror (input_location, "ISO C++ forbids using pointer to "
5176 "a function in subtraction");
5177 else
5178 return error_mark_node;
5180 if (TREE_CODE (target_type) == METHOD_TYPE)
5182 if (complain & tf_error)
5183 permerror (input_location, "ISO C++ forbids using pointer to "
5184 "a method in subtraction");
5185 else
5186 return error_mark_node;
5189 /* First do the subtraction as integers;
5190 then drop through to build the divide operator. */
5192 op0 = cp_build_binary_op (input_location,
5193 MINUS_EXPR,
5194 cp_convert (restype, op0, complain),
5195 cp_convert (restype, op1, complain),
5196 complain);
5198 /* This generates an error if op1 is a pointer to an incomplete type. */
5199 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5201 if (complain & tf_error)
5202 error ("invalid use of a pointer to an incomplete type in "
5203 "pointer arithmetic");
5204 else
5205 return error_mark_node;
5208 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5210 if (complain & tf_error)
5211 error ("arithmetic on pointer to an empty aggregate");
5212 else
5213 return error_mark_node;
5216 op1 = (TYPE_PTROB_P (ptrtype)
5217 ? size_in_bytes (target_type)
5218 : integer_one_node);
5220 /* Do the division. */
5222 result = build2 (EXACT_DIV_EXPR, restype, op0,
5223 cp_convert (restype, op1, complain));
5224 return fold_if_not_in_template (result);
5227 /* Construct and perhaps optimize a tree representation
5228 for a unary operation. CODE, a tree_code, specifies the operation
5229 and XARG is the operand. */
5231 tree
5232 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5233 tsubst_flags_t complain)
5235 tree orig_expr = xarg;
5236 tree exp;
5237 int ptrmem = 0;
5239 if (processing_template_decl)
5241 if (type_dependent_expression_p (xarg))
5242 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5244 xarg = build_non_dependent_expr (xarg);
5247 exp = NULL_TREE;
5249 /* [expr.unary.op] says:
5251 The address of an object of incomplete type can be taken.
5253 (And is just the ordinary address operator, not an overloaded
5254 "operator &".) However, if the type is a template
5255 specialization, we must complete the type at this point so that
5256 an overloaded "operator &" will be available if required. */
5257 if (code == ADDR_EXPR
5258 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5259 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5260 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5261 || (TREE_CODE (xarg) == OFFSET_REF)))
5262 /* Don't look for a function. */;
5263 else
5264 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5265 NULL_TREE, /*overload=*/NULL, complain);
5266 if (!exp && code == ADDR_EXPR)
5268 if (is_overloaded_fn (xarg))
5270 tree fn = get_first_fn (xarg);
5271 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5273 if (complain & tf_error)
5274 error (DECL_CONSTRUCTOR_P (fn)
5275 ? G_("taking address of constructor %qE")
5276 : G_("taking address of destructor %qE"),
5277 xarg);
5278 return error_mark_node;
5282 /* A pointer to member-function can be formed only by saying
5283 &X::mf. */
5284 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5285 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5287 if (TREE_CODE (xarg) != OFFSET_REF
5288 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5290 if (complain & tf_error)
5292 error ("invalid use of %qE to form a "
5293 "pointer-to-member-function", xarg);
5294 if (TREE_CODE (xarg) != OFFSET_REF)
5295 inform (input_location, " a qualified-id is required");
5297 return error_mark_node;
5299 else
5301 if (complain & tf_error)
5302 error ("parentheses around %qE cannot be used to form a"
5303 " pointer-to-member-function",
5304 xarg);
5305 else
5306 return error_mark_node;
5307 PTRMEM_OK_P (xarg) = 1;
5311 if (TREE_CODE (xarg) == OFFSET_REF)
5313 ptrmem = PTRMEM_OK_P (xarg);
5315 if (!ptrmem && !flag_ms_extensions
5316 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5318 /* A single non-static member, make sure we don't allow a
5319 pointer-to-member. */
5320 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5321 TREE_OPERAND (xarg, 0),
5322 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5323 PTRMEM_OK_P (xarg) = ptrmem;
5327 exp = cp_build_addr_expr_strict (xarg, complain);
5330 if (processing_template_decl && exp != error_mark_node)
5331 exp = build_min_non_dep (code, exp, orig_expr,
5332 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5333 if (TREE_CODE (exp) == ADDR_EXPR)
5334 PTRMEM_OK_P (exp) = ptrmem;
5335 return exp;
5338 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5339 constants, where a null value is represented by an INTEGER_CST of
5340 -1. */
5342 tree
5343 cp_truthvalue_conversion (tree expr)
5345 tree type = TREE_TYPE (expr);
5346 if (TYPE_PTRDATAMEM_P (type)
5347 /* Avoid ICE on invalid use of non-static member function. */
5348 || TREE_CODE (expr) == FUNCTION_DECL)
5349 return build_binary_op (EXPR_LOCATION (expr),
5350 NE_EXPR, expr, nullptr_node, 1);
5351 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5353 /* With -Wzero-as-null-pointer-constant do not warn for an
5354 'if (p)' or a 'while (!p)', where p is a pointer. */
5355 tree ret;
5356 ++c_inhibit_evaluation_warnings;
5357 ret = c_common_truthvalue_conversion (input_location, expr);
5358 --c_inhibit_evaluation_warnings;
5359 return ret;
5361 else
5362 return c_common_truthvalue_conversion (input_location, expr);
5365 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5367 tree
5368 condition_conversion (tree expr)
5370 tree t;
5371 if (processing_template_decl)
5372 return expr;
5373 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5374 tf_warning_or_error, LOOKUP_NORMAL);
5375 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5376 return t;
5379 /* Returns the address of T. This function will fold away
5380 ADDR_EXPR of INDIRECT_REF. */
5382 tree
5383 build_address (tree t)
5385 if (error_operand_p (t) || !cxx_mark_addressable (t))
5386 return error_mark_node;
5387 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5388 t = build_fold_addr_expr (t);
5389 if (TREE_CODE (t) != ADDR_EXPR)
5390 t = rvalue (t);
5391 return t;
5394 /* Return a NOP_EXPR converting EXPR to TYPE. */
5396 tree
5397 build_nop (tree type, tree expr)
5399 if (type == error_mark_node || error_operand_p (expr))
5400 return expr;
5401 return build1 (NOP_EXPR, type, expr);
5404 /* Take the address of ARG, whatever that means under C++ semantics.
5405 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5406 and class rvalues as well.
5408 Nothing should call this function directly; instead, callers should use
5409 cp_build_addr_expr or cp_build_addr_expr_strict. */
5411 static tree
5412 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5414 tree argtype;
5415 tree val;
5417 if (!arg || error_operand_p (arg))
5418 return error_mark_node;
5420 arg = mark_lvalue_use (arg);
5421 argtype = lvalue_type (arg);
5423 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5425 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5426 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5428 /* They're trying to take the address of a unique non-static
5429 member function. This is ill-formed (except in MS-land),
5430 but let's try to DTRT.
5431 Note: We only handle unique functions here because we don't
5432 want to complain if there's a static overload; non-unique
5433 cases will be handled by instantiate_type. But we need to
5434 handle this case here to allow casts on the resulting PMF.
5435 We could defer this in non-MS mode, but it's easier to give
5436 a useful error here. */
5438 /* Inside constant member functions, the `this' pointer
5439 contains an extra const qualifier. TYPE_MAIN_VARIANT
5440 is used here to remove this const from the diagnostics
5441 and the created OFFSET_REF. */
5442 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5443 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5444 if (!mark_used (fn, complain) && !(complain & tf_error))
5445 return error_mark_node;
5447 if (! flag_ms_extensions)
5449 tree name = DECL_NAME (fn);
5450 if (!(complain & tf_error))
5451 return error_mark_node;
5452 else if (current_class_type
5453 && TREE_OPERAND (arg, 0) == current_class_ref)
5454 /* An expression like &memfn. */
5455 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5456 " or parenthesized non-static member function to form"
5457 " a pointer to member function. Say %<&%T::%D%>",
5458 base, name);
5459 else
5460 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5461 " function to form a pointer to member function."
5462 " Say %<&%T::%D%>",
5463 base, name);
5465 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5468 /* Uninstantiated types are all functions. Taking the
5469 address of a function is a no-op, so just return the
5470 argument. */
5471 if (type_unknown_p (arg))
5472 return build1 (ADDR_EXPR, unknown_type_node, arg);
5474 if (TREE_CODE (arg) == OFFSET_REF)
5475 /* We want a pointer to member; bypass all the code for actually taking
5476 the address of something. */
5477 goto offset_ref;
5479 /* Anything not already handled and not a true memory reference
5480 is an error. */
5481 if (TREE_CODE (argtype) != FUNCTION_TYPE
5482 && TREE_CODE (argtype) != METHOD_TYPE)
5484 cp_lvalue_kind kind = lvalue_kind (arg);
5485 if (kind == clk_none)
5487 if (complain & tf_error)
5488 lvalue_error (input_location, lv_addressof);
5489 return error_mark_node;
5491 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5493 if (!(complain & tf_error))
5494 return error_mark_node;
5495 if (kind & clk_class)
5496 /* Make this a permerror because we used to accept it. */
5497 permerror (input_location, "taking address of temporary");
5498 else
5499 error ("taking address of xvalue (rvalue reference)");
5503 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5505 tree type = build_pointer_type (TREE_TYPE (argtype));
5506 arg = build1 (CONVERT_EXPR, type, arg);
5507 return arg;
5509 else if (pedantic && DECL_MAIN_P (arg))
5511 /* ARM $3.4 */
5512 /* Apparently a lot of autoconf scripts for C++ packages do this,
5513 so only complain if -Wpedantic. */
5514 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5515 pedwarn (input_location, OPT_Wpedantic,
5516 "ISO C++ forbids taking address of function %<::main%>");
5517 else if (flag_pedantic_errors)
5518 return error_mark_node;
5521 /* Let &* cancel out to simplify resulting code. */
5522 if (INDIRECT_REF_P (arg))
5524 /* We don't need to have `current_class_ptr' wrapped in a
5525 NON_LVALUE_EXPR node. */
5526 if (arg == current_class_ref)
5527 return current_class_ptr;
5529 arg = TREE_OPERAND (arg, 0);
5530 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5532 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5533 arg = build1 (CONVERT_EXPR, type, arg);
5535 else
5536 /* Don't let this be an lvalue. */
5537 arg = rvalue (arg);
5538 return arg;
5541 /* ??? Cope with user tricks that amount to offsetof. */
5542 if (TREE_CODE (argtype) != FUNCTION_TYPE
5543 && TREE_CODE (argtype) != METHOD_TYPE
5544 && argtype != unknown_type_node
5545 && (val = get_base_address (arg))
5546 && COMPLETE_TYPE_P (TREE_TYPE (val))
5547 && INDIRECT_REF_P (val)
5548 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5550 tree type = build_pointer_type (argtype);
5551 return fold_convert (type, fold_offsetof_1 (arg));
5554 /* Handle complex lvalues (when permitted)
5555 by reduction to simpler cases. */
5556 val = unary_complex_lvalue (ADDR_EXPR, arg);
5557 if (val != 0)
5558 return val;
5560 switch (TREE_CODE (arg))
5562 CASE_CONVERT:
5563 case FLOAT_EXPR:
5564 case FIX_TRUNC_EXPR:
5565 /* Even if we're not being pedantic, we cannot allow this
5566 extension when we're instantiating in a SFINAE
5567 context. */
5568 if (! lvalue_p (arg) && complain == tf_none)
5570 if (complain & tf_error)
5571 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5572 else
5573 return error_mark_node;
5575 break;
5577 case BASELINK:
5578 arg = BASELINK_FUNCTIONS (arg);
5579 /* Fall through. */
5581 case OVERLOAD:
5582 arg = OVL_CURRENT (arg);
5583 break;
5585 case OFFSET_REF:
5586 offset_ref:
5587 /* Turn a reference to a non-static data member into a
5588 pointer-to-member. */
5590 tree type;
5591 tree t;
5593 gcc_assert (PTRMEM_OK_P (arg));
5595 t = TREE_OPERAND (arg, 1);
5596 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5598 if (complain & tf_error)
5599 error ("cannot create pointer to reference member %qD", t);
5600 return error_mark_node;
5603 type = build_ptrmem_type (context_for_name_lookup (t),
5604 TREE_TYPE (t));
5605 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5606 return t;
5609 default:
5610 break;
5613 if (argtype != error_mark_node)
5614 argtype = build_pointer_type (argtype);
5616 /* In a template, we are processing a non-dependent expression
5617 so we can just form an ADDR_EXPR with the correct type. */
5618 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5620 val = build_address (arg);
5621 if (TREE_CODE (arg) == OFFSET_REF)
5622 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5624 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5626 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5628 /* We can only get here with a single static member
5629 function. */
5630 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5631 && DECL_STATIC_FUNCTION_P (fn));
5632 if (!mark_used (fn, complain) && !(complain & tf_error))
5633 return error_mark_node;
5634 val = build_address (fn);
5635 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5636 /* Do not lose object's side effects. */
5637 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5638 TREE_OPERAND (arg, 0), val);
5640 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5642 if (complain & tf_error)
5643 error ("attempt to take address of bit-field structure member %qD",
5644 TREE_OPERAND (arg, 1));
5645 return error_mark_node;
5647 else
5649 tree object = TREE_OPERAND (arg, 0);
5650 tree field = TREE_OPERAND (arg, 1);
5651 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5652 (TREE_TYPE (object), decl_type_context (field)));
5653 val = build_address (arg);
5656 if (TYPE_PTR_P (argtype)
5657 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5659 build_ptrmemfunc_type (argtype);
5660 val = build_ptrmemfunc (argtype, val, 0,
5661 /*c_cast_p=*/false,
5662 complain);
5665 return val;
5668 /* Take the address of ARG if it has one, even if it's an rvalue. */
5670 tree
5671 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5673 return cp_build_addr_expr_1 (arg, 0, complain);
5676 /* Take the address of ARG, but only if it's an lvalue. */
5678 static tree
5679 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5681 return cp_build_addr_expr_1 (arg, 1, complain);
5684 /* C++: Must handle pointers to members.
5686 Perhaps type instantiation should be extended to handle conversion
5687 from aggregates to types we don't yet know we want? (Or are those
5688 cases typically errors which should be reported?)
5690 NOCONVERT nonzero suppresses the default promotions
5691 (such as from short to int). */
5693 tree
5694 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5695 tsubst_flags_t complain)
5697 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5698 tree arg = xarg;
5699 tree argtype = 0;
5700 const char *errstring = NULL;
5701 tree val;
5702 const char *invalid_op_diag;
5704 if (!arg || error_operand_p (arg))
5705 return error_mark_node;
5707 if ((invalid_op_diag
5708 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5709 ? CONVERT_EXPR
5710 : code),
5711 TREE_TYPE (xarg))))
5713 if (complain & tf_error)
5714 error (invalid_op_diag);
5715 return error_mark_node;
5718 switch (code)
5720 case UNARY_PLUS_EXPR:
5721 case NEGATE_EXPR:
5723 int flags = WANT_ARITH | WANT_ENUM;
5724 /* Unary plus (but not unary minus) is allowed on pointers. */
5725 if (code == UNARY_PLUS_EXPR)
5726 flags |= WANT_POINTER;
5727 arg = build_expr_type_conversion (flags, arg, true);
5728 if (!arg)
5729 errstring = (code == NEGATE_EXPR
5730 ? _("wrong type argument to unary minus")
5731 : _("wrong type argument to unary plus"));
5732 else
5734 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5735 arg = cp_perform_integral_promotions (arg, complain);
5737 /* Make sure the result is not an lvalue: a unary plus or minus
5738 expression is always a rvalue. */
5739 arg = rvalue (arg);
5742 break;
5744 case BIT_NOT_EXPR:
5745 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5747 code = CONJ_EXPR;
5748 if (!noconvert)
5750 arg = cp_default_conversion (arg, complain);
5751 if (arg == error_mark_node)
5752 return error_mark_node;
5755 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5756 | WANT_VECTOR_OR_COMPLEX,
5757 arg, true)))
5758 errstring = _("wrong type argument to bit-complement");
5759 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5760 arg = cp_perform_integral_promotions (arg, complain);
5761 break;
5763 case ABS_EXPR:
5764 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5765 errstring = _("wrong type argument to abs");
5766 else if (!noconvert)
5768 arg = cp_default_conversion (arg, complain);
5769 if (arg == error_mark_node)
5770 return error_mark_node;
5772 break;
5774 case CONJ_EXPR:
5775 /* Conjugating a real value is a no-op, but allow it anyway. */
5776 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5777 errstring = _("wrong type argument to conjugation");
5778 else if (!noconvert)
5780 arg = cp_default_conversion (arg, complain);
5781 if (arg == error_mark_node)
5782 return error_mark_node;
5784 break;
5786 case TRUTH_NOT_EXPR:
5787 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5788 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5789 build_zero_cst (TREE_TYPE (arg)), complain);
5790 arg = perform_implicit_conversion (boolean_type_node, arg,
5791 complain);
5792 val = invert_truthvalue_loc (input_location, arg);
5793 if (arg != error_mark_node)
5794 return val;
5795 errstring = _("in argument to unary !");
5796 break;
5798 case NOP_EXPR:
5799 break;
5801 case REALPART_EXPR:
5802 case IMAGPART_EXPR:
5803 arg = build_real_imag_expr (input_location, code, arg);
5804 if (arg == error_mark_node)
5805 return arg;
5806 else
5807 return fold_if_not_in_template (arg);
5809 case PREINCREMENT_EXPR:
5810 case POSTINCREMENT_EXPR:
5811 case PREDECREMENT_EXPR:
5812 case POSTDECREMENT_EXPR:
5813 /* Handle complex lvalues (when permitted)
5814 by reduction to simpler cases. */
5816 val = unary_complex_lvalue (code, arg);
5817 if (val != 0)
5818 return val;
5820 arg = mark_lvalue_use (arg);
5822 /* Increment or decrement the real part of the value,
5823 and don't change the imaginary part. */
5824 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5826 tree real, imag;
5828 arg = stabilize_reference (arg);
5829 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5830 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5831 real = cp_build_unary_op (code, real, 1, complain);
5832 if (real == error_mark_node || imag == error_mark_node)
5833 return error_mark_node;
5834 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5835 real, imag);
5838 /* Report invalid types. */
5840 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5841 arg, true)))
5843 if (code == PREINCREMENT_EXPR)
5844 errstring = _("no pre-increment operator for type");
5845 else if (code == POSTINCREMENT_EXPR)
5846 errstring = _("no post-increment operator for type");
5847 else if (code == PREDECREMENT_EXPR)
5848 errstring = _("no pre-decrement operator for type");
5849 else
5850 errstring = _("no post-decrement operator for type");
5851 break;
5853 else if (arg == error_mark_node)
5854 return error_mark_node;
5856 /* Report something read-only. */
5858 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5859 || TREE_READONLY (arg))
5861 if (complain & tf_error)
5862 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5863 || code == POSTINCREMENT_EXPR)
5864 ? lv_increment : lv_decrement));
5865 else
5866 return error_mark_node;
5870 tree inc;
5871 tree declared_type = unlowered_expr_type (arg);
5873 argtype = TREE_TYPE (arg);
5875 /* ARM $5.2.5 last annotation says this should be forbidden. */
5876 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5878 if (complain & tf_error)
5879 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5880 ? G_("ISO C++ forbids incrementing an enum")
5881 : G_("ISO C++ forbids decrementing an enum"));
5882 else
5883 return error_mark_node;
5886 /* Compute the increment. */
5888 if (TYPE_PTR_P (argtype))
5890 tree type = complete_type (TREE_TYPE (argtype));
5892 if (!COMPLETE_OR_VOID_TYPE_P (type))
5894 if (complain & tf_error)
5895 error (((code == PREINCREMENT_EXPR
5896 || code == POSTINCREMENT_EXPR))
5897 ? G_("cannot increment a pointer to incomplete type %qT")
5898 : G_("cannot decrement a pointer to incomplete type %qT"),
5899 TREE_TYPE (argtype));
5900 else
5901 return error_mark_node;
5903 else if (!TYPE_PTROB_P (argtype))
5905 if (complain & tf_error)
5906 pedwarn (input_location, OPT_Wpointer_arith,
5907 (code == PREINCREMENT_EXPR
5908 || code == POSTINCREMENT_EXPR)
5909 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5910 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5911 argtype);
5912 else
5913 return error_mark_node;
5916 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5918 else
5919 inc = VECTOR_TYPE_P (argtype)
5920 ? build_one_cst (argtype)
5921 : integer_one_node;
5923 inc = cp_convert (argtype, inc, complain);
5925 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5926 need to ask Objective-C to build the increment or decrement
5927 expression for it. */
5928 if (objc_is_property_ref (arg))
5929 return objc_build_incr_expr_for_property_ref (input_location, code,
5930 arg, inc);
5932 /* Complain about anything else that is not a true lvalue. */
5933 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5934 || code == POSTINCREMENT_EXPR)
5935 ? lv_increment : lv_decrement),
5936 complain))
5937 return error_mark_node;
5939 /* Forbid using -- on `bool'. */
5940 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5942 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5944 if (complain & tf_error)
5945 error ("invalid use of Boolean expression as operand "
5946 "to %<operator--%>");
5947 return error_mark_node;
5949 val = boolean_increment (code, arg);
5951 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5952 /* An rvalue has no cv-qualifiers. */
5953 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5954 else
5955 val = build2 (code, TREE_TYPE (arg), arg, inc);
5957 TREE_SIDE_EFFECTS (val) = 1;
5958 return val;
5961 case ADDR_EXPR:
5962 /* Note that this operation never does default_conversion
5963 regardless of NOCONVERT. */
5964 return cp_build_addr_expr (arg, complain);
5966 default:
5967 break;
5970 if (!errstring)
5972 if (argtype == 0)
5973 argtype = TREE_TYPE (arg);
5974 return fold_if_not_in_template (build1 (code, argtype, arg));
5977 if (complain & tf_error)
5978 error ("%s", errstring);
5979 return error_mark_node;
5982 /* Hook for the c-common bits that build a unary op. */
5983 tree
5984 build_unary_op (location_t /*location*/,
5985 enum tree_code code, tree xarg, int noconvert)
5987 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5990 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5991 for certain kinds of expressions which are not really lvalues
5992 but which we can accept as lvalues.
5994 If ARG is not a kind of expression we can handle, return
5995 NULL_TREE. */
5997 tree
5998 unary_complex_lvalue (enum tree_code code, tree arg)
6000 /* Inside a template, making these kinds of adjustments is
6001 pointless; we are only concerned with the type of the
6002 expression. */
6003 if (processing_template_decl)
6004 return NULL_TREE;
6006 /* Handle (a, b) used as an "lvalue". */
6007 if (TREE_CODE (arg) == COMPOUND_EXPR)
6009 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
6010 tf_warning_or_error);
6011 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6012 TREE_OPERAND (arg, 0), real_result);
6015 /* Handle (a ? b : c) used as an "lvalue". */
6016 if (TREE_CODE (arg) == COND_EXPR
6017 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6018 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6020 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6021 if (TREE_CODE (arg) == MODIFY_EXPR
6022 || TREE_CODE (arg) == PREINCREMENT_EXPR
6023 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6025 tree lvalue = TREE_OPERAND (arg, 0);
6026 if (TREE_SIDE_EFFECTS (lvalue))
6028 lvalue = stabilize_reference (lvalue);
6029 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6030 lvalue, TREE_OPERAND (arg, 1));
6032 return unary_complex_lvalue
6033 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
6036 if (code != ADDR_EXPR)
6037 return NULL_TREE;
6039 /* Handle (a = b) used as an "lvalue" for `&'. */
6040 if (TREE_CODE (arg) == MODIFY_EXPR
6041 || TREE_CODE (arg) == INIT_EXPR)
6043 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
6044 tf_warning_or_error);
6045 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6046 arg, real_result);
6047 TREE_NO_WARNING (arg) = 1;
6048 return arg;
6051 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6052 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6053 || TREE_CODE (arg) == OFFSET_REF)
6054 return NULL_TREE;
6056 /* We permit compiler to make function calls returning
6057 objects of aggregate type look like lvalues. */
6059 tree targ = arg;
6061 if (TREE_CODE (targ) == SAVE_EXPR)
6062 targ = TREE_OPERAND (targ, 0);
6064 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6066 if (TREE_CODE (arg) == SAVE_EXPR)
6067 targ = arg;
6068 else
6069 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6070 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6073 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6074 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6075 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6078 /* Don't let anything else be handled specially. */
6079 return NULL_TREE;
6082 /* Mark EXP saying that we need to be able to take the
6083 address of it; it should not be allocated in a register.
6084 Value is true if successful.
6086 C++: we do not allow `current_class_ptr' to be addressable. */
6088 bool
6089 cxx_mark_addressable (tree exp)
6091 tree x = exp;
6093 while (1)
6094 switch (TREE_CODE (x))
6096 case ADDR_EXPR:
6097 case COMPONENT_REF:
6098 case ARRAY_REF:
6099 case REALPART_EXPR:
6100 case IMAGPART_EXPR:
6101 x = TREE_OPERAND (x, 0);
6102 break;
6104 case PARM_DECL:
6105 if (x == current_class_ptr)
6107 error ("cannot take the address of %<this%>, which is an rvalue expression");
6108 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6109 return true;
6111 /* Fall through. */
6113 case VAR_DECL:
6114 /* Caller should not be trying to mark initialized
6115 constant fields addressable. */
6116 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6117 || DECL_IN_AGGR_P (x) == 0
6118 || TREE_STATIC (x)
6119 || DECL_EXTERNAL (x));
6120 /* Fall through. */
6122 case RESULT_DECL:
6123 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6124 && !DECL_ARTIFICIAL (x))
6126 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6128 error
6129 ("address of explicit register variable %qD requested", x);
6130 return false;
6132 else if (extra_warnings)
6133 warning
6134 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6136 TREE_ADDRESSABLE (x) = 1;
6137 return true;
6139 case CONST_DECL:
6140 case FUNCTION_DECL:
6141 TREE_ADDRESSABLE (x) = 1;
6142 return true;
6144 case CONSTRUCTOR:
6145 TREE_ADDRESSABLE (x) = 1;
6146 return true;
6148 case TARGET_EXPR:
6149 TREE_ADDRESSABLE (x) = 1;
6150 cxx_mark_addressable (TREE_OPERAND (x, 0));
6151 return true;
6153 default:
6154 return true;
6158 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6160 tree
6161 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6162 tsubst_flags_t complain)
6164 tree orig_ifexp = ifexp;
6165 tree orig_op1 = op1;
6166 tree orig_op2 = op2;
6167 tree expr;
6169 if (processing_template_decl)
6171 /* The standard says that the expression is type-dependent if
6172 IFEXP is type-dependent, even though the eventual type of the
6173 expression doesn't dependent on IFEXP. */
6174 if (type_dependent_expression_p (ifexp)
6175 /* As a GNU extension, the middle operand may be omitted. */
6176 || (op1 && type_dependent_expression_p (op1))
6177 || type_dependent_expression_p (op2))
6178 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6179 ifexp = build_non_dependent_expr (ifexp);
6180 if (op1)
6181 op1 = build_non_dependent_expr (op1);
6182 op2 = build_non_dependent_expr (op2);
6185 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6186 if (processing_template_decl && expr != error_mark_node
6187 && TREE_CODE (expr) != VEC_COND_EXPR)
6189 tree min = build_min_non_dep (COND_EXPR, expr,
6190 orig_ifexp, orig_op1, orig_op2);
6191 /* In C++11, remember that the result is an lvalue or xvalue.
6192 In C++98, lvalue_kind can just assume lvalue in a template. */
6193 if (cxx_dialect >= cxx11
6194 && lvalue_or_rvalue_with_address_p (expr)
6195 && !lvalue_or_rvalue_with_address_p (min))
6196 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6197 !real_lvalue_p (expr));
6198 expr = convert_from_reference (min);
6200 return expr;
6203 /* Given a list of expressions, return a compound expression
6204 that performs them all and returns the value of the last of them. */
6206 tree
6207 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6208 tsubst_flags_t complain)
6210 tree expr = TREE_VALUE (list);
6212 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6213 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6215 if (complain & tf_error)
6216 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6217 "list-initializer for non-class type must not "
6218 "be parenthesized");
6219 else
6220 return error_mark_node;
6223 if (TREE_CHAIN (list))
6225 if (complain & tf_error)
6226 switch (exp)
6228 case ELK_INIT:
6229 permerror (input_location, "expression list treated as compound "
6230 "expression in initializer");
6231 break;
6232 case ELK_MEM_INIT:
6233 permerror (input_location, "expression list treated as compound "
6234 "expression in mem-initializer");
6235 break;
6236 case ELK_FUNC_CAST:
6237 permerror (input_location, "expression list treated as compound "
6238 "expression in functional cast");
6239 break;
6240 default:
6241 gcc_unreachable ();
6243 else
6244 return error_mark_node;
6246 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6247 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6248 expr, TREE_VALUE (list), complain);
6251 return expr;
6254 /* Like build_x_compound_expr_from_list, but using a VEC. */
6256 tree
6257 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6258 tsubst_flags_t complain)
6260 if (vec_safe_is_empty (vec))
6261 return NULL_TREE;
6262 else if (vec->length () == 1)
6263 return (*vec)[0];
6264 else
6266 tree expr;
6267 unsigned int ix;
6268 tree t;
6270 if (msg != NULL)
6272 if (complain & tf_error)
6273 permerror (input_location,
6274 "%s expression list treated as compound expression",
6275 msg);
6276 else
6277 return error_mark_node;
6280 expr = (*vec)[0];
6281 for (ix = 1; vec->iterate (ix, &t); ++ix)
6282 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6283 t, complain);
6285 return expr;
6289 /* Handle overloading of the ',' operator when needed. */
6291 tree
6292 build_x_compound_expr (location_t loc, tree op1, tree op2,
6293 tsubst_flags_t complain)
6295 tree result;
6296 tree orig_op1 = op1;
6297 tree orig_op2 = op2;
6299 if (processing_template_decl)
6301 if (type_dependent_expression_p (op1)
6302 || type_dependent_expression_p (op2))
6303 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6304 op1 = build_non_dependent_expr (op1);
6305 op2 = build_non_dependent_expr (op2);
6308 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6309 NULL_TREE, /*overload=*/NULL, complain);
6310 if (!result)
6311 result = cp_build_compound_expr (op1, op2, complain);
6313 if (processing_template_decl && result != error_mark_node)
6314 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6316 return result;
6319 /* Like cp_build_compound_expr, but for the c-common bits. */
6321 tree
6322 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6324 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6327 /* Build a compound expression. */
6329 tree
6330 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6332 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6334 if (lhs == error_mark_node || rhs == error_mark_node)
6335 return error_mark_node;
6337 if (flag_cilkplus
6338 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6339 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6341 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6342 : EXPR_LOCATION (rhs));
6343 error_at (loc,
6344 "spawned function call cannot be part of a comma expression");
6345 return error_mark_node;
6348 if (TREE_CODE (rhs) == TARGET_EXPR)
6350 /* If the rhs is a TARGET_EXPR, then build the compound
6351 expression inside the target_expr's initializer. This
6352 helps the compiler to eliminate unnecessary temporaries. */
6353 tree init = TREE_OPERAND (rhs, 1);
6355 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6356 TREE_OPERAND (rhs, 1) = init;
6358 return rhs;
6361 if (type_unknown_p (rhs))
6363 if (complain & tf_error)
6364 error ("no context to resolve type of %qE", rhs);
6365 return error_mark_node;
6368 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6371 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6372 casts away constness. CAST gives the type of cast. Returns true
6373 if the cast is ill-formed, false if it is well-formed.
6375 ??? This function warns for casting away any qualifier not just
6376 const. We would like to specify exactly what qualifiers are casted
6377 away.
6380 static bool
6381 check_for_casting_away_constness (tree src_type, tree dest_type,
6382 enum tree_code cast, tsubst_flags_t complain)
6384 /* C-style casts are allowed to cast away constness. With
6385 WARN_CAST_QUAL, we still want to issue a warning. */
6386 if (cast == CAST_EXPR && !warn_cast_qual)
6387 return false;
6389 if (!casts_away_constness (src_type, dest_type, complain))
6390 return false;
6392 switch (cast)
6394 case CAST_EXPR:
6395 if (complain & tf_warning)
6396 warning (OPT_Wcast_qual,
6397 "cast from type %qT to type %qT casts away qualifiers",
6398 src_type, dest_type);
6399 return false;
6401 case STATIC_CAST_EXPR:
6402 if (complain & tf_error)
6403 error ("static_cast from type %qT to type %qT casts away qualifiers",
6404 src_type, dest_type);
6405 return true;
6407 case REINTERPRET_CAST_EXPR:
6408 if (complain & tf_error)
6409 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6410 src_type, dest_type);
6411 return true;
6413 default:
6414 gcc_unreachable();
6419 Warns if the cast from expression EXPR to type TYPE is useless.
6421 void
6422 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6424 if (warn_useless_cast
6425 && complain & tf_warning)
6427 if ((TREE_CODE (type) == REFERENCE_TYPE
6428 && (TYPE_REF_IS_RVALUE (type)
6429 ? xvalue_p (expr) : real_lvalue_p (expr))
6430 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6431 || same_type_p (TREE_TYPE (expr), type))
6432 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6436 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6437 (another pointer-to-member type in the same hierarchy) and return
6438 the converted expression. If ALLOW_INVERSE_P is permitted, a
6439 pointer-to-derived may be converted to pointer-to-base; otherwise,
6440 only the other direction is permitted. If C_CAST_P is true, this
6441 conversion is taking place as part of a C-style cast. */
6443 tree
6444 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6445 bool c_cast_p, tsubst_flags_t complain)
6447 if (TYPE_PTRDATAMEM_P (type))
6449 tree delta;
6451 if (TREE_CODE (expr) == PTRMEM_CST)
6452 expr = cplus_expand_constant (expr);
6453 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6454 TYPE_PTRMEM_CLASS_TYPE (type),
6455 allow_inverse_p,
6456 c_cast_p, complain);
6457 if (delta == error_mark_node)
6458 return error_mark_node;
6460 if (!integer_zerop (delta))
6462 tree cond, op1, op2;
6464 cond = cp_build_binary_op (input_location,
6465 EQ_EXPR,
6466 expr,
6467 build_int_cst (TREE_TYPE (expr), -1),
6468 complain);
6469 op1 = build_nop (ptrdiff_type_node, expr);
6470 op2 = cp_build_binary_op (input_location,
6471 PLUS_EXPR, op1, delta,
6472 complain);
6474 expr = fold_build3_loc (input_location,
6475 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6479 return build_nop (type, expr);
6481 else
6482 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6483 allow_inverse_p, c_cast_p, complain);
6486 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6487 this static_cast is being attempted as one of the possible casts
6488 allowed by a C-style cast. (In that case, accessibility of base
6489 classes is not considered, and it is OK to cast away
6490 constness.) Return the result of the cast. *VALID_P is set to
6491 indicate whether or not the cast was valid. */
6493 static tree
6494 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6495 bool *valid_p, tsubst_flags_t complain)
6497 tree intype;
6498 tree result;
6499 cp_lvalue_kind clk;
6501 /* Assume the cast is valid. */
6502 *valid_p = true;
6504 intype = unlowered_expr_type (expr);
6506 /* Save casted types in the function's used types hash table. */
6507 used_types_insert (type);
6509 /* [expr.static.cast]
6511 An lvalue of type "cv1 B", where B is a class type, can be cast
6512 to type "reference to cv2 D", where D is a class derived (clause
6513 _class.derived_) from B, if a valid standard conversion from
6514 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6515 same cv-qualification as, or greater cv-qualification than, cv1,
6516 and B is not a virtual base class of D. */
6517 /* We check this case before checking the validity of "TYPE t =
6518 EXPR;" below because for this case:
6520 struct B {};
6521 struct D : public B { D(const B&); };
6522 extern B& b;
6523 void f() { static_cast<const D&>(b); }
6525 we want to avoid constructing a new D. The standard is not
6526 completely clear about this issue, but our interpretation is
6527 consistent with other compilers. */
6528 if (TREE_CODE (type) == REFERENCE_TYPE
6529 && CLASS_TYPE_P (TREE_TYPE (type))
6530 && CLASS_TYPE_P (intype)
6531 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6532 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6533 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6534 build_pointer_type (TYPE_MAIN_VARIANT
6535 (TREE_TYPE (type))),
6536 complain)
6537 && (c_cast_p
6538 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6540 tree base;
6542 /* There is a standard conversion from "D*" to "B*" even if "B"
6543 is ambiguous or inaccessible. If this is really a
6544 static_cast, then we check both for inaccessibility and
6545 ambiguity. However, if this is a static_cast being performed
6546 because the user wrote a C-style cast, then accessibility is
6547 not considered. */
6548 base = lookup_base (TREE_TYPE (type), intype,
6549 c_cast_p ? ba_unique : ba_check,
6550 NULL, complain);
6551 expr = build_address (expr);
6553 if (flag_sanitize & SANITIZE_VPTR)
6555 tree ubsan_check
6556 = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
6557 if (ubsan_check)
6558 expr = ubsan_check;
6561 /* Convert from "B*" to "D*". This function will check that "B"
6562 is not a virtual base of "D". */
6563 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6564 complain);
6566 /* Convert the pointer to a reference -- but then remember that
6567 there are no expressions with reference type in C++.
6569 We call rvalue so that there's an actual tree code
6570 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6571 is a variable with the same type, the conversion would get folded
6572 away, leaving just the variable and causing lvalue_kind to give
6573 the wrong answer. */
6574 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6577 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6578 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6579 if (TREE_CODE (type) == REFERENCE_TYPE
6580 && TYPE_REF_IS_RVALUE (type)
6581 && (clk = real_lvalue_p (expr))
6582 && reference_related_p (TREE_TYPE (type), intype)
6583 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6585 if (clk == clk_ordinary)
6587 /* Handle the (non-bit-field) lvalue case here by casting to
6588 lvalue reference and then changing it to an rvalue reference.
6589 Casting an xvalue to rvalue reference will be handled by the
6590 main code path. */
6591 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6592 result = (perform_direct_initialization_if_possible
6593 (lref, expr, c_cast_p, complain));
6594 result = cp_fold_convert (type, result);
6595 /* Make sure we don't fold back down to a named rvalue reference,
6596 because that would be an lvalue. */
6597 if (DECL_P (result))
6598 result = build1 (NON_LVALUE_EXPR, type, result);
6599 return convert_from_reference (result);
6601 else
6602 /* For a bit-field or packed field, bind to a temporary. */
6603 expr = rvalue (expr);
6606 /* Resolve overloaded address here rather than once in
6607 implicit_conversion and again in the inverse code below. */
6608 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6610 expr = instantiate_type (type, expr, complain);
6611 intype = TREE_TYPE (expr);
6614 /* [expr.static.cast]
6616 Any expression can be explicitly converted to type cv void. */
6617 if (VOID_TYPE_P (type))
6618 return convert_to_void (expr, ICV_CAST, complain);
6620 /* [class.abstract]
6621 An abstract class shall not be used ... as the type of an explicit
6622 conversion. */
6623 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6624 return error_mark_node;
6626 /* [expr.static.cast]
6628 An expression e can be explicitly converted to a type T using a
6629 static_cast of the form static_cast<T>(e) if the declaration T
6630 t(e);" is well-formed, for some invented temporary variable
6631 t. */
6632 result = perform_direct_initialization_if_possible (type, expr,
6633 c_cast_p, complain);
6634 if (result)
6636 result = convert_from_reference (result);
6638 /* [expr.static.cast]
6640 If T is a reference type, the result is an lvalue; otherwise,
6641 the result is an rvalue. */
6642 if (TREE_CODE (type) != REFERENCE_TYPE)
6643 result = rvalue (result);
6644 return result;
6647 /* [expr.static.cast]
6649 The inverse of any standard conversion sequence (clause _conv_),
6650 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6651 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6652 (_conv.bool_) conversions, can be performed explicitly using
6653 static_cast subject to the restriction that the explicit
6654 conversion does not cast away constness (_expr.const.cast_), and
6655 the following additional rules for specific cases: */
6656 /* For reference, the conversions not excluded are: integral
6657 promotions, floating point promotion, integral conversions,
6658 floating point conversions, floating-integral conversions,
6659 pointer conversions, and pointer to member conversions. */
6660 /* DR 128
6662 A value of integral _or enumeration_ type can be explicitly
6663 converted to an enumeration type. */
6664 /* The effect of all that is that any conversion between any two
6665 types which are integral, floating, or enumeration types can be
6666 performed. */
6667 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6668 || SCALAR_FLOAT_TYPE_P (type))
6669 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6670 || SCALAR_FLOAT_TYPE_P (intype)))
6671 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6673 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6674 && CLASS_TYPE_P (TREE_TYPE (type))
6675 && CLASS_TYPE_P (TREE_TYPE (intype))
6676 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6677 (TREE_TYPE (intype))),
6678 build_pointer_type (TYPE_MAIN_VARIANT
6679 (TREE_TYPE (type))),
6680 complain))
6682 tree base;
6684 if (!c_cast_p
6685 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6686 complain))
6687 return error_mark_node;
6688 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6689 c_cast_p ? ba_unique : ba_check,
6690 NULL, complain);
6691 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6692 complain);
6694 if (flag_sanitize & SANITIZE_VPTR)
6696 tree ubsan_check
6697 = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
6698 if (ubsan_check)
6699 expr = ubsan_check;
6702 return cp_fold_convert (type, expr);
6705 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6706 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6708 tree c1;
6709 tree c2;
6710 tree t1;
6711 tree t2;
6713 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6714 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6716 if (TYPE_PTRDATAMEM_P (type))
6718 t1 = (build_ptrmem_type
6719 (c1,
6720 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6721 t2 = (build_ptrmem_type
6722 (c2,
6723 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6725 else
6727 t1 = intype;
6728 t2 = type;
6730 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6732 if (!c_cast_p
6733 && check_for_casting_away_constness (intype, type,
6734 STATIC_CAST_EXPR,
6735 complain))
6736 return error_mark_node;
6737 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6738 c_cast_p, complain);
6742 /* [expr.static.cast]
6744 An rvalue of type "pointer to cv void" can be explicitly
6745 converted to a pointer to object type. A value of type pointer
6746 to object converted to "pointer to cv void" and back to the
6747 original pointer type will have its original value. */
6748 if (TYPE_PTR_P (intype)
6749 && VOID_TYPE_P (TREE_TYPE (intype))
6750 && TYPE_PTROB_P (type))
6752 if (!c_cast_p
6753 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6754 complain))
6755 return error_mark_node;
6756 return build_nop (type, expr);
6759 *valid_p = false;
6760 return error_mark_node;
6763 /* Return an expression representing static_cast<TYPE>(EXPR). */
6765 tree
6766 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6768 tree result;
6769 bool valid_p;
6771 if (type == error_mark_node || expr == error_mark_node)
6772 return error_mark_node;
6774 if (processing_template_decl)
6776 expr = build_min (STATIC_CAST_EXPR, type, expr);
6777 /* We don't know if it will or will not have side effects. */
6778 TREE_SIDE_EFFECTS (expr) = 1;
6779 return convert_from_reference (expr);
6782 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6783 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6784 if (TREE_CODE (type) != REFERENCE_TYPE
6785 && TREE_CODE (expr) == NOP_EXPR
6786 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6787 expr = TREE_OPERAND (expr, 0);
6789 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6790 complain);
6791 if (valid_p)
6793 if (result != error_mark_node)
6794 maybe_warn_about_useless_cast (type, expr, complain);
6795 return result;
6798 if (complain & tf_error)
6799 error ("invalid static_cast from type %qT to type %qT",
6800 TREE_TYPE (expr), type);
6801 return error_mark_node;
6804 /* EXPR is an expression with member function or pointer-to-member
6805 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6806 not permitted by ISO C++, but we accept it in some modes. If we
6807 are not in one of those modes, issue a diagnostic. Return the
6808 converted expression. */
6810 tree
6811 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6813 tree intype;
6814 tree decl;
6816 intype = TREE_TYPE (expr);
6817 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6818 || TREE_CODE (intype) == METHOD_TYPE);
6820 if (!(complain & tf_warning_or_error))
6821 return error_mark_node;
6823 if (pedantic || warn_pmf2ptr)
6824 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6825 "converting from %qT to %qT", intype, type);
6827 if (TREE_CODE (intype) == METHOD_TYPE)
6828 expr = build_addr_func (expr, complain);
6829 else if (TREE_CODE (expr) == PTRMEM_CST)
6830 expr = build_address (PTRMEM_CST_MEMBER (expr));
6831 else
6833 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6834 decl = build_address (decl);
6835 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6838 if (expr == error_mark_node)
6839 return error_mark_node;
6841 return build_nop (type, expr);
6844 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6845 If C_CAST_P is true, this reinterpret cast is being done as part of
6846 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6847 indicate whether or not reinterpret_cast was valid. */
6849 static tree
6850 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6851 bool *valid_p, tsubst_flags_t complain)
6853 tree intype;
6855 /* Assume the cast is invalid. */
6856 if (valid_p)
6857 *valid_p = true;
6859 if (type == error_mark_node || error_operand_p (expr))
6860 return error_mark_node;
6862 intype = TREE_TYPE (expr);
6864 /* Save casted types in the function's used types hash table. */
6865 used_types_insert (type);
6867 /* [expr.reinterpret.cast]
6868 An lvalue expression of type T1 can be cast to the type
6869 "reference to T2" if an expression of type "pointer to T1" can be
6870 explicitly converted to the type "pointer to T2" using a
6871 reinterpret_cast. */
6872 if (TREE_CODE (type) == REFERENCE_TYPE)
6874 if (! real_lvalue_p (expr))
6876 if (complain & tf_error)
6877 error ("invalid cast of an rvalue expression of type "
6878 "%qT to type %qT",
6879 intype, type);
6880 return error_mark_node;
6883 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6884 "B" are related class types; the reinterpret_cast does not
6885 adjust the pointer. */
6886 if (TYPE_PTR_P (intype)
6887 && (complain & tf_warning)
6888 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6889 COMPARE_BASE | COMPARE_DERIVED)))
6890 warning (0, "casting %qT to %qT does not dereference pointer",
6891 intype, type);
6893 expr = cp_build_addr_expr (expr, complain);
6895 if (warn_strict_aliasing > 2)
6896 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6898 if (expr != error_mark_node)
6899 expr = build_reinterpret_cast_1
6900 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6901 valid_p, complain);
6902 if (expr != error_mark_node)
6903 /* cp_build_indirect_ref isn't right for rvalue refs. */
6904 expr = convert_from_reference (fold_convert (type, expr));
6905 return expr;
6908 /* As a G++ extension, we consider conversions from member
6909 functions, and pointers to member functions to
6910 pointer-to-function and pointer-to-void types. If
6911 -Wno-pmf-conversions has not been specified,
6912 convert_member_func_to_ptr will issue an error message. */
6913 if ((TYPE_PTRMEMFUNC_P (intype)
6914 || TREE_CODE (intype) == METHOD_TYPE)
6915 && TYPE_PTR_P (type)
6916 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6917 || VOID_TYPE_P (TREE_TYPE (type))))
6918 return convert_member_func_to_ptr (type, expr, complain);
6920 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6921 array-to-pointer, and function-to-pointer conversions are
6922 performed. */
6923 expr = decay_conversion (expr, complain);
6925 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6926 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6927 if (TREE_CODE (expr) == NOP_EXPR
6928 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6929 expr = TREE_OPERAND (expr, 0);
6931 if (error_operand_p (expr))
6932 return error_mark_node;
6934 intype = TREE_TYPE (expr);
6936 /* [expr.reinterpret.cast]
6937 A pointer can be converted to any integral type large enough to
6938 hold it. ... A value of type std::nullptr_t can be converted to
6939 an integral type; the conversion has the same meaning and
6940 validity as a conversion of (void*)0 to the integral type. */
6941 if (CP_INTEGRAL_TYPE_P (type)
6942 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6944 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6946 if (complain & tf_error)
6947 permerror (input_location, "cast from %qT to %qT loses precision",
6948 intype, type);
6949 else
6950 return error_mark_node;
6952 if (NULLPTR_TYPE_P (intype))
6953 return build_int_cst (type, 0);
6955 /* [expr.reinterpret.cast]
6956 A value of integral or enumeration type can be explicitly
6957 converted to a pointer. */
6958 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6959 /* OK */
6961 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6962 || TYPE_PTR_OR_PTRMEM_P (type))
6963 && same_type_p (type, intype))
6964 /* DR 799 */
6965 return rvalue (expr);
6966 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6967 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6968 return fold_if_not_in_template (build_nop (type, expr));
6969 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6970 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6972 tree sexpr = expr;
6974 if (!c_cast_p
6975 && check_for_casting_away_constness (intype, type,
6976 REINTERPRET_CAST_EXPR,
6977 complain))
6978 return error_mark_node;
6979 /* Warn about possible alignment problems. */
6980 if (STRICT_ALIGNMENT && warn_cast_align
6981 && (complain & tf_warning)
6982 && !VOID_TYPE_P (type)
6983 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6984 && COMPLETE_TYPE_P (TREE_TYPE (type))
6985 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6986 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6987 warning (OPT_Wcast_align, "cast from %qT to %qT "
6988 "increases required alignment of target type", intype, type);
6990 /* We need to strip nops here, because the front end likes to
6991 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6992 STRIP_NOPS (sexpr);
6993 if (warn_strict_aliasing <= 2)
6994 strict_aliasing_warning (intype, type, sexpr);
6996 return fold_if_not_in_template (build_nop (type, expr));
6998 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6999 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7001 if (complain & tf_warning)
7002 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7003 object pointer type or vice versa is conditionally-supported." */
7004 warning (OPT_Wconditionally_supported,
7005 "casting between pointer-to-function and pointer-to-object "
7006 "is conditionally-supported");
7007 return fold_if_not_in_template (build_nop (type, expr));
7009 else if (VECTOR_TYPE_P (type))
7010 return fold_if_not_in_template (convert_to_vector (type, expr));
7011 else if (VECTOR_TYPE_P (intype)
7012 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7013 return fold_if_not_in_template (convert_to_integer (type, expr));
7014 else
7016 if (valid_p)
7017 *valid_p = false;
7018 if (complain & tf_error)
7019 error ("invalid cast from type %qT to type %qT", intype, type);
7020 return error_mark_node;
7023 return cp_convert (type, expr, complain);
7026 tree
7027 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7029 tree r;
7031 if (type == error_mark_node || expr == error_mark_node)
7032 return error_mark_node;
7034 if (processing_template_decl)
7036 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7038 if (!TREE_SIDE_EFFECTS (t)
7039 && type_dependent_expression_p (expr))
7040 /* There might turn out to be side effects inside expr. */
7041 TREE_SIDE_EFFECTS (t) = 1;
7042 return convert_from_reference (t);
7045 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7046 /*valid_p=*/NULL, complain);
7047 if (r != error_mark_node)
7048 maybe_warn_about_useless_cast (type, expr, complain);
7049 return r;
7052 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7053 return an appropriate expression. Otherwise, return
7054 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7055 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7056 performing a C-style cast, its value upon return will indicate
7057 whether or not the conversion succeeded. */
7059 static tree
7060 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7061 bool *valid_p)
7063 tree src_type;
7064 tree reference_type;
7066 /* Callers are responsible for handling error_mark_node as a
7067 destination type. */
7068 gcc_assert (dst_type != error_mark_node);
7069 /* In a template, callers should be building syntactic
7070 representations of casts, not using this machinery. */
7071 gcc_assert (!processing_template_decl);
7073 /* Assume the conversion is invalid. */
7074 if (valid_p)
7075 *valid_p = false;
7077 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7079 if (complain & tf_error)
7080 error ("invalid use of const_cast with type %qT, "
7081 "which is not a pointer, "
7082 "reference, nor a pointer-to-data-member type", dst_type);
7083 return error_mark_node;
7086 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7088 if (complain & tf_error)
7089 error ("invalid use of const_cast with type %qT, which is a pointer "
7090 "or reference to a function type", dst_type);
7091 return error_mark_node;
7094 /* Save casted types in the function's used types hash table. */
7095 used_types_insert (dst_type);
7097 src_type = TREE_TYPE (expr);
7098 /* Expressions do not really have reference types. */
7099 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7100 src_type = TREE_TYPE (src_type);
7102 /* [expr.const.cast]
7104 For two object types T1 and T2, if a pointer to T1 can be explicitly
7105 converted to the type "pointer to T2" using a const_cast, then the
7106 following conversions can also be made:
7108 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7109 type T2 using the cast const_cast<T2&>;
7111 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7112 type T2 using the cast const_cast<T2&&>; and
7114 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7115 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7117 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7119 reference_type = dst_type;
7120 if (!TYPE_REF_IS_RVALUE (dst_type)
7121 ? real_lvalue_p (expr)
7122 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7123 ? lvalue_p (expr)
7124 : lvalue_or_rvalue_with_address_p (expr)))
7125 /* OK. */;
7126 else
7128 if (complain & tf_error)
7129 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7130 src_type, dst_type);
7131 return error_mark_node;
7133 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7134 src_type = build_pointer_type (src_type);
7136 else
7138 reference_type = NULL_TREE;
7139 /* If the destination type is not a reference type, the
7140 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7141 conversions are performed. */
7142 src_type = type_decays_to (src_type);
7143 if (src_type == error_mark_node)
7144 return error_mark_node;
7147 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7149 if (comp_ptr_ttypes_const (dst_type, src_type))
7151 if (valid_p)
7153 *valid_p = true;
7154 /* This cast is actually a C-style cast. Issue a warning if
7155 the user is making a potentially unsafe cast. */
7156 check_for_casting_away_constness (src_type, dst_type,
7157 CAST_EXPR, complain);
7159 if (reference_type)
7161 expr = cp_build_addr_expr (expr, complain);
7162 if (expr == error_mark_node)
7163 return error_mark_node;
7164 expr = build_nop (reference_type, expr);
7165 return convert_from_reference (expr);
7167 else
7169 expr = decay_conversion (expr, complain);
7170 if (expr == error_mark_node)
7171 return error_mark_node;
7173 /* build_c_cast puts on a NOP_EXPR to make the result not an
7174 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7175 non-lvalue context. */
7176 if (TREE_CODE (expr) == NOP_EXPR
7177 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7178 expr = TREE_OPERAND (expr, 0);
7179 return build_nop (dst_type, expr);
7182 else if (valid_p
7183 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7184 TREE_TYPE (src_type)))
7185 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7186 complain);
7189 if (complain & tf_error)
7190 error ("invalid const_cast from type %qT to type %qT",
7191 src_type, dst_type);
7192 return error_mark_node;
7195 tree
7196 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7198 tree r;
7200 if (type == error_mark_node || error_operand_p (expr))
7201 return error_mark_node;
7203 if (processing_template_decl)
7205 tree t = build_min (CONST_CAST_EXPR, type, expr);
7207 if (!TREE_SIDE_EFFECTS (t)
7208 && type_dependent_expression_p (expr))
7209 /* There might turn out to be side effects inside expr. */
7210 TREE_SIDE_EFFECTS (t) = 1;
7211 return convert_from_reference (t);
7214 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7215 if (r != error_mark_node)
7216 maybe_warn_about_useless_cast (type, expr, complain);
7217 return r;
7220 /* Like cp_build_c_cast, but for the c-common bits. */
7222 tree
7223 build_c_cast (location_t /*loc*/, tree type, tree expr)
7225 return cp_build_c_cast (type, expr, tf_warning_or_error);
7228 /* Build an expression representing an explicit C-style cast to type
7229 TYPE of expression EXPR. */
7231 tree
7232 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7234 tree value = expr;
7235 tree result;
7236 bool valid_p;
7238 if (type == error_mark_node || error_operand_p (expr))
7239 return error_mark_node;
7241 if (processing_template_decl)
7243 tree t = build_min (CAST_EXPR, type,
7244 tree_cons (NULL_TREE, value, NULL_TREE));
7245 /* We don't know if it will or will not have side effects. */
7246 TREE_SIDE_EFFECTS (t) = 1;
7247 return convert_from_reference (t);
7250 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7251 'Class') should always be retained, because this information aids
7252 in method lookup. */
7253 if (objc_is_object_ptr (type)
7254 && objc_is_object_ptr (TREE_TYPE (expr)))
7255 return build_nop (type, expr);
7257 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7258 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7259 if (TREE_CODE (type) != REFERENCE_TYPE
7260 && TREE_CODE (value) == NOP_EXPR
7261 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7262 value = TREE_OPERAND (value, 0);
7264 if (TREE_CODE (type) == ARRAY_TYPE)
7266 /* Allow casting from T1* to T2[] because Cfront allows it.
7267 NIHCL uses it. It is not valid ISO C++ however. */
7268 if (TYPE_PTR_P (TREE_TYPE (expr)))
7270 if (complain & tf_error)
7271 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7272 else
7273 return error_mark_node;
7274 type = build_pointer_type (TREE_TYPE (type));
7276 else
7278 if (complain & tf_error)
7279 error ("ISO C++ forbids casting to an array type %qT", type);
7280 return error_mark_node;
7284 if (TREE_CODE (type) == FUNCTION_TYPE
7285 || TREE_CODE (type) == METHOD_TYPE)
7287 if (complain & tf_error)
7288 error ("invalid cast to function type %qT", type);
7289 return error_mark_node;
7292 if (TYPE_PTR_P (type)
7293 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7294 /* Casting to an integer of smaller size is an error detected elsewhere. */
7295 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7296 /* Don't warn about converting any constant. */
7297 && !TREE_CONSTANT (value))
7298 warning_at (input_location, OPT_Wint_to_pointer_cast,
7299 "cast to pointer from integer of different size");
7301 /* A C-style cast can be a const_cast. */
7302 result = build_const_cast_1 (type, value, complain & tf_warning,
7303 &valid_p);
7304 if (valid_p)
7306 if (result != error_mark_node)
7307 maybe_warn_about_useless_cast (type, value, complain);
7308 return result;
7311 /* Or a static cast. */
7312 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7313 &valid_p, complain);
7314 /* Or a reinterpret_cast. */
7315 if (!valid_p)
7316 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7317 &valid_p, complain);
7318 /* The static_cast or reinterpret_cast may be followed by a
7319 const_cast. */
7320 if (valid_p
7321 /* A valid cast may result in errors if, for example, a
7322 conversion to an ambiguous base class is required. */
7323 && !error_operand_p (result))
7325 tree result_type;
7327 maybe_warn_about_useless_cast (type, value, complain);
7329 /* Non-class rvalues always have cv-unqualified type. */
7330 if (!CLASS_TYPE_P (type))
7331 type = TYPE_MAIN_VARIANT (type);
7332 result_type = TREE_TYPE (result);
7333 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7334 result_type = TYPE_MAIN_VARIANT (result_type);
7335 /* If the type of RESULT does not match TYPE, perform a
7336 const_cast to make it match. If the static_cast or
7337 reinterpret_cast succeeded, we will differ by at most
7338 cv-qualification, so the follow-on const_cast is guaranteed
7339 to succeed. */
7340 if (!same_type_p (non_reference (type), non_reference (result_type)))
7342 result = build_const_cast_1 (type, result, false, &valid_p);
7343 gcc_assert (valid_p);
7345 return result;
7348 return error_mark_node;
7351 /* For use from the C common bits. */
7352 tree
7353 build_modify_expr (location_t /*location*/,
7354 tree lhs, tree /*lhs_origtype*/,
7355 enum tree_code modifycode,
7356 location_t /*rhs_location*/, tree rhs,
7357 tree /*rhs_origtype*/)
7359 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7362 /* Build an assignment expression of lvalue LHS from value RHS.
7363 MODIFYCODE is the code for a binary operator that we use
7364 to combine the old value of LHS with RHS to get the new value.
7365 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7367 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7369 tree
7370 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7371 tsubst_flags_t complain)
7373 tree result;
7374 tree newrhs = rhs;
7375 tree lhstype = TREE_TYPE (lhs);
7376 tree olhstype = lhstype;
7377 bool plain_assign = (modifycode == NOP_EXPR);
7379 /* Avoid duplicate error messages from operands that had errors. */
7380 if (error_operand_p (lhs) || error_operand_p (rhs))
7381 return error_mark_node;
7383 /* Handle control structure constructs used as "lvalues". */
7384 switch (TREE_CODE (lhs))
7386 /* Handle --foo = 5; as these are valid constructs in C++. */
7387 case PREDECREMENT_EXPR:
7388 case PREINCREMENT_EXPR:
7389 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7390 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7391 stabilize_reference (TREE_OPERAND (lhs, 0)),
7392 TREE_OPERAND (lhs, 1));
7393 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7394 modifycode, rhs, complain);
7395 if (newrhs == error_mark_node)
7396 return error_mark_node;
7397 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7399 /* Handle (a, b) used as an "lvalue". */
7400 case COMPOUND_EXPR:
7401 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7402 modifycode, rhs, complain);
7403 if (newrhs == error_mark_node)
7404 return error_mark_node;
7405 return build2 (COMPOUND_EXPR, lhstype,
7406 TREE_OPERAND (lhs, 0), newrhs);
7408 case MODIFY_EXPR:
7409 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7410 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7411 stabilize_reference (TREE_OPERAND (lhs, 0)),
7412 TREE_OPERAND (lhs, 1));
7413 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7414 complain);
7415 if (newrhs == error_mark_node)
7416 return error_mark_node;
7417 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7419 case MIN_EXPR:
7420 case MAX_EXPR:
7421 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7422 when neither operand has side-effects. */
7423 if (!lvalue_or_else (lhs, lv_assign, complain))
7424 return error_mark_node;
7426 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7427 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7429 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7430 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7431 boolean_type_node,
7432 TREE_OPERAND (lhs, 0),
7433 TREE_OPERAND (lhs, 1)),
7434 TREE_OPERAND (lhs, 0),
7435 TREE_OPERAND (lhs, 1));
7436 /* Fall through. */
7438 /* Handle (a ? b : c) used as an "lvalue". */
7439 case COND_EXPR:
7441 /* Produce (a ? (b = rhs) : (c = rhs))
7442 except that the RHS goes through a save-expr
7443 so the code to compute it is only emitted once. */
7444 tree cond;
7445 tree preeval = NULL_TREE;
7447 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7449 if (complain & tf_error)
7450 error ("void value not ignored as it ought to be");
7451 return error_mark_node;
7454 rhs = stabilize_expr (rhs, &preeval);
7456 /* Check this here to avoid odd errors when trying to convert
7457 a throw to the type of the COND_EXPR. */
7458 if (!lvalue_or_else (lhs, lv_assign, complain))
7459 return error_mark_node;
7461 cond = build_conditional_expr
7462 (input_location, TREE_OPERAND (lhs, 0),
7463 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7464 modifycode, rhs, complain),
7465 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7466 modifycode, rhs, complain),
7467 complain);
7469 if (cond == error_mark_node)
7470 return cond;
7471 /* Make sure the code to compute the rhs comes out
7472 before the split. */
7473 if (preeval)
7474 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7475 return cond;
7478 default:
7479 break;
7482 if (modifycode == INIT_EXPR)
7484 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7485 /* Do the default thing. */;
7486 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7488 /* Compound literal. */
7489 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7490 /* Call convert to generate an error; see PR 11063. */
7491 rhs = convert (lhstype, rhs);
7492 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7493 TREE_SIDE_EFFECTS (result) = 1;
7494 return result;
7496 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7497 /* Do the default thing. */;
7498 else
7500 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7501 result = build_special_member_call (lhs, complete_ctor_identifier,
7502 &rhs_vec, lhstype, LOOKUP_NORMAL,
7503 complain);
7504 release_tree_vector (rhs_vec);
7505 if (result == NULL_TREE)
7506 return error_mark_node;
7507 return result;
7510 else
7512 lhs = require_complete_type_sfinae (lhs, complain);
7513 if (lhs == error_mark_node)
7514 return error_mark_node;
7516 if (modifycode == NOP_EXPR)
7518 if (c_dialect_objc ())
7520 result = objc_maybe_build_modify_expr (lhs, rhs);
7521 if (result)
7522 return result;
7525 /* `operator=' is not an inheritable operator. */
7526 if (! MAYBE_CLASS_TYPE_P (lhstype))
7527 /* Do the default thing. */;
7528 else
7530 result = build_new_op (input_location, MODIFY_EXPR,
7531 LOOKUP_NORMAL, lhs, rhs,
7532 make_node (NOP_EXPR), /*overload=*/NULL,
7533 complain);
7534 if (result == NULL_TREE)
7535 return error_mark_node;
7536 return result;
7538 lhstype = olhstype;
7540 else
7542 tree init = NULL_TREE;
7544 /* A binary op has been requested. Combine the old LHS
7545 value with the RHS producing the value we should actually
7546 store into the LHS. */
7547 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7548 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7549 || MAYBE_CLASS_TYPE_P (lhstype)));
7551 /* Preevaluate the RHS to make sure its evaluation is complete
7552 before the lvalue-to-rvalue conversion of the LHS:
7554 [expr.ass] With respect to an indeterminately-sequenced
7555 function call, the operation of a compound assignment is a
7556 single evaluation. [ Note: Therefore, a function call shall
7557 not intervene between the lvalue-to-rvalue conversion and the
7558 side effect associated with any single compound assignment
7559 operator. -- end note ] */
7560 lhs = stabilize_reference (lhs);
7561 rhs = rvalue (rhs);
7562 rhs = stabilize_expr (rhs, &init);
7563 newrhs = cp_build_binary_op (input_location,
7564 modifycode, lhs, rhs,
7565 complain);
7566 if (newrhs == error_mark_node)
7568 if (complain & tf_error)
7569 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7570 TREE_TYPE (lhs), TREE_TYPE (rhs));
7571 return error_mark_node;
7574 if (init)
7575 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7577 /* Now it looks like a plain assignment. */
7578 modifycode = NOP_EXPR;
7579 if (c_dialect_objc ())
7581 result = objc_maybe_build_modify_expr (lhs, newrhs);
7582 if (result)
7583 return result;
7586 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7587 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7590 /* The left-hand side must be an lvalue. */
7591 if (!lvalue_or_else (lhs, lv_assign, complain))
7592 return error_mark_node;
7594 /* Warn about modifying something that is `const'. Don't warn if
7595 this is initialization. */
7596 if (modifycode != INIT_EXPR
7597 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7598 /* Functions are not modifiable, even though they are
7599 lvalues. */
7600 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7601 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7602 /* If it's an aggregate and any field is const, then it is
7603 effectively const. */
7604 || (CLASS_TYPE_P (lhstype)
7605 && C_TYPE_FIELDS_READONLY (lhstype))))
7607 if (complain & tf_error)
7608 cxx_readonly_error (lhs, lv_assign);
7609 else
7610 return error_mark_node;
7613 /* If storing into a structure or union member, it may have been given a
7614 lowered bitfield type. We need to convert to the declared type first,
7615 so retrieve it now. */
7617 olhstype = unlowered_expr_type (lhs);
7619 /* Convert new value to destination type. */
7621 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7623 int from_array;
7625 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7627 if (modifycode != INIT_EXPR)
7629 if (complain & tf_error)
7630 error ("assigning to an array from an initializer list");
7631 return error_mark_node;
7633 if (check_array_initializer (lhs, lhstype, newrhs))
7634 return error_mark_node;
7635 newrhs = digest_init (lhstype, newrhs, complain);
7636 if (newrhs == error_mark_node)
7637 return error_mark_node;
7640 /* C++11 8.5/17: "If the destination type is an array of characters,
7641 an array of char16_t, an array of char32_t, or an array of wchar_t,
7642 and the initializer is a string literal...". */
7643 else if (TREE_CODE (newrhs) == STRING_CST
7644 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7645 && modifycode == INIT_EXPR)
7647 newrhs = digest_init (lhstype, newrhs, complain);
7648 if (newrhs == error_mark_node)
7649 return error_mark_node;
7652 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7653 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7655 if (complain & tf_error)
7656 error ("incompatible types in assignment of %qT to %qT",
7657 TREE_TYPE (rhs), lhstype);
7658 return error_mark_node;
7661 /* Allow array assignment in compiler-generated code. */
7662 else if (!current_function_decl
7663 || !DECL_DEFAULTED_FN (current_function_decl))
7665 /* This routine is used for both initialization and assignment.
7666 Make sure the diagnostic message differentiates the context. */
7667 if (complain & tf_error)
7669 if (modifycode == INIT_EXPR)
7670 error ("array used as initializer");
7671 else
7672 error ("invalid array assignment");
7674 return error_mark_node;
7677 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7678 ? 1 + (modifycode != INIT_EXPR): 0;
7679 return build_vec_init (lhs, NULL_TREE, newrhs,
7680 /*explicit_value_init_p=*/false,
7681 from_array, complain);
7684 if (modifycode == INIT_EXPR)
7685 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7686 LOOKUP_ONLYCONVERTING. */
7687 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7688 ICR_INIT, NULL_TREE, 0,
7689 complain);
7690 else
7691 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7692 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7694 if (!same_type_p (lhstype, olhstype))
7695 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7697 if (modifycode != INIT_EXPR)
7699 if (TREE_CODE (newrhs) == CALL_EXPR
7700 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7701 newrhs = build_cplus_new (lhstype, newrhs, complain);
7703 /* Can't initialize directly from a TARGET_EXPR, since that would
7704 cause the lhs to be constructed twice, and possibly result in
7705 accidental self-initialization. So we force the TARGET_EXPR to be
7706 expanded without a target. */
7707 if (TREE_CODE (newrhs) == TARGET_EXPR)
7708 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7709 TREE_OPERAND (newrhs, 0));
7712 if (newrhs == error_mark_node)
7713 return error_mark_node;
7715 if (c_dialect_objc () && flag_objc_gc)
7717 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7719 if (result)
7720 return result;
7723 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7724 lhstype, lhs, newrhs);
7726 TREE_SIDE_EFFECTS (result) = 1;
7727 if (!plain_assign)
7728 TREE_NO_WARNING (result) = 1;
7730 return result;
7733 tree
7734 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7735 tree rhs, tsubst_flags_t complain)
7737 if (processing_template_decl)
7738 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7739 build_min_nt_loc (loc, modifycode, NULL_TREE,
7740 NULL_TREE), rhs);
7742 if (modifycode != NOP_EXPR)
7744 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7745 make_node (modifycode), /*overload=*/NULL,
7746 complain);
7747 if (rval)
7749 TREE_NO_WARNING (rval) = 1;
7750 return rval;
7753 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7756 /* Helper function for get_delta_difference which assumes FROM is a base
7757 class of TO. Returns a delta for the conversion of pointer-to-member
7758 of FROM to pointer-to-member of TO. If the conversion is invalid and
7759 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7760 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7761 If C_CAST_P is true, this conversion is taking place as part of a
7762 C-style cast. */
7764 static tree
7765 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7766 tsubst_flags_t complain)
7768 tree binfo;
7769 base_kind kind;
7771 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7772 &kind, complain);
7774 if (binfo == error_mark_node)
7776 if (!(complain & tf_error))
7777 return error_mark_node;
7779 error (" in pointer to member function conversion");
7780 return size_zero_node;
7782 else if (binfo)
7784 if (kind != bk_via_virtual)
7785 return BINFO_OFFSET (binfo);
7786 else
7787 /* FROM is a virtual base class of TO. Issue an error or warning
7788 depending on whether or not this is a reinterpret cast. */
7790 if (!(complain & tf_error))
7791 return error_mark_node;
7793 error ("pointer to member conversion via virtual base %qT",
7794 BINFO_TYPE (binfo_from_vbase (binfo)));
7796 return size_zero_node;
7799 else
7800 return NULL_TREE;
7803 /* Get difference in deltas for different pointer to member function
7804 types. If the conversion is invalid and tf_error is not set in
7805 COMPLAIN, returns error_mark_node, otherwise returns an integer
7806 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7807 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7808 conversions as well. If C_CAST_P is true this conversion is taking
7809 place as part of a C-style cast.
7811 Note that the naming of FROM and TO is kind of backwards; the return
7812 value is what we add to a TO in order to get a FROM. They are named
7813 this way because we call this function to find out how to convert from
7814 a pointer to member of FROM to a pointer to member of TO. */
7816 static tree
7817 get_delta_difference (tree from, tree to,
7818 bool allow_inverse_p,
7819 bool c_cast_p, tsubst_flags_t complain)
7821 tree result;
7823 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7824 /* Pointer to member of incomplete class is permitted*/
7825 result = size_zero_node;
7826 else
7827 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7829 if (result == error_mark_node)
7830 return error_mark_node;
7832 if (!result)
7834 if (!allow_inverse_p)
7836 if (!(complain & tf_error))
7837 return error_mark_node;
7839 error_not_base_type (from, to);
7840 error (" in pointer to member conversion");
7841 result = size_zero_node;
7843 else
7845 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7847 if (result == error_mark_node)
7848 return error_mark_node;
7850 if (result)
7851 result = size_diffop_loc (input_location,
7852 size_zero_node, result);
7853 else
7855 if (!(complain & tf_error))
7856 return error_mark_node;
7858 error_not_base_type (from, to);
7859 error (" in pointer to member conversion");
7860 result = size_zero_node;
7865 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7866 result));
7869 /* Return a constructor for the pointer-to-member-function TYPE using
7870 the other components as specified. */
7872 tree
7873 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7875 tree u = NULL_TREE;
7876 tree delta_field;
7877 tree pfn_field;
7878 vec<constructor_elt, va_gc> *v;
7880 /* Pull the FIELD_DECLs out of the type. */
7881 pfn_field = TYPE_FIELDS (type);
7882 delta_field = DECL_CHAIN (pfn_field);
7884 /* Make sure DELTA has the type we want. */
7885 delta = convert_and_check (input_location, delta_type_node, delta);
7887 /* Convert to the correct target type if necessary. */
7888 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7890 /* Finish creating the initializer. */
7891 vec_alloc (v, 2);
7892 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7893 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7894 u = build_constructor (type, v);
7895 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7896 TREE_STATIC (u) = (TREE_CONSTANT (u)
7897 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7898 != NULL_TREE)
7899 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7900 != NULL_TREE));
7901 return u;
7904 /* Build a constructor for a pointer to member function. It can be
7905 used to initialize global variables, local variable, or used
7906 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7907 want to be.
7909 If FORCE is nonzero, then force this conversion, even if
7910 we would rather not do it. Usually set when using an explicit
7911 cast. A C-style cast is being processed iff C_CAST_P is true.
7913 Return error_mark_node, if something goes wrong. */
7915 tree
7916 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7917 tsubst_flags_t complain)
7919 tree fn;
7920 tree pfn_type;
7921 tree to_type;
7923 if (error_operand_p (pfn))
7924 return error_mark_node;
7926 pfn_type = TREE_TYPE (pfn);
7927 to_type = build_ptrmemfunc_type (type);
7929 /* Handle multiple conversions of pointer to member functions. */
7930 if (TYPE_PTRMEMFUNC_P (pfn_type))
7932 tree delta = NULL_TREE;
7933 tree npfn = NULL_TREE;
7934 tree n;
7936 if (!force
7937 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7938 LOOKUP_NORMAL, complain))
7940 if (complain & tf_error)
7941 error ("invalid conversion to type %qT from type %qT",
7942 to_type, pfn_type);
7943 else
7944 return error_mark_node;
7947 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7948 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7949 force,
7950 c_cast_p, complain);
7951 if (n == error_mark_node)
7952 return error_mark_node;
7954 /* We don't have to do any conversion to convert a
7955 pointer-to-member to its own type. But, we don't want to
7956 just return a PTRMEM_CST if there's an explicit cast; that
7957 cast should make the expression an invalid template argument. */
7958 if (TREE_CODE (pfn) != PTRMEM_CST)
7960 if (same_type_p (to_type, pfn_type))
7961 return pfn;
7962 else if (integer_zerop (n))
7963 return build_reinterpret_cast (to_type, pfn,
7964 complain);
7967 if (TREE_SIDE_EFFECTS (pfn))
7968 pfn = save_expr (pfn);
7970 /* Obtain the function pointer and the current DELTA. */
7971 if (TREE_CODE (pfn) == PTRMEM_CST)
7972 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7973 else
7975 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7976 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7979 /* Just adjust the DELTA field. */
7980 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7981 (TREE_TYPE (delta), ptrdiff_type_node));
7982 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7983 n = cp_build_binary_op (input_location,
7984 LSHIFT_EXPR, n, integer_one_node,
7985 complain);
7986 delta = cp_build_binary_op (input_location,
7987 PLUS_EXPR, delta, n, complain);
7988 return build_ptrmemfunc1 (to_type, delta, npfn);
7991 /* Handle null pointer to member function conversions. */
7992 if (null_ptr_cst_p (pfn))
7994 pfn = cp_build_c_cast (type, pfn, complain);
7995 return build_ptrmemfunc1 (to_type,
7996 integer_zero_node,
7997 pfn);
8000 if (type_unknown_p (pfn))
8001 return instantiate_type (type, pfn, complain);
8003 fn = TREE_OPERAND (pfn, 0);
8004 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8005 /* In a template, we will have preserved the
8006 OFFSET_REF. */
8007 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8008 return make_ptrmem_cst (to_type, fn);
8011 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8012 given by CST.
8014 ??? There is no consistency as to the types returned for the above
8015 values. Some code acts as if it were a sizetype and some as if it were
8016 integer_type_node. */
8018 void
8019 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8021 tree type = TREE_TYPE (cst);
8022 tree fn = PTRMEM_CST_MEMBER (cst);
8023 tree ptr_class, fn_class;
8025 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8027 /* The class that the function belongs to. */
8028 fn_class = DECL_CONTEXT (fn);
8030 /* The class that we're creating a pointer to member of. */
8031 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8033 /* First, calculate the adjustment to the function's class. */
8034 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8035 /*c_cast_p=*/0, tf_warning_or_error);
8037 if (!DECL_VIRTUAL_P (fn))
8038 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8039 build_addr_func (fn, tf_warning_or_error));
8040 else
8042 /* If we're dealing with a virtual function, we have to adjust 'this'
8043 again, to point to the base which provides the vtable entry for
8044 fn; the call will do the opposite adjustment. */
8045 tree orig_class = DECL_CONTEXT (fn);
8046 tree binfo = binfo_or_else (orig_class, fn_class);
8047 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
8048 *delta, BINFO_OFFSET (binfo));
8049 *delta = fold_if_not_in_template (*delta);
8051 /* We set PFN to the vtable offset at which the function can be
8052 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8053 case delta is shifted left, and then incremented). */
8054 *pfn = DECL_VINDEX (fn);
8055 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
8056 TYPE_SIZE_UNIT (vtable_entry_type));
8057 *pfn = fold_if_not_in_template (*pfn);
8059 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8061 case ptrmemfunc_vbit_in_pfn:
8062 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
8063 integer_one_node);
8064 *pfn = fold_if_not_in_template (*pfn);
8065 break;
8067 case ptrmemfunc_vbit_in_delta:
8068 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8069 *delta, integer_one_node);
8070 *delta = fold_if_not_in_template (*delta);
8071 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
8072 *delta, integer_one_node);
8073 *delta = fold_if_not_in_template (*delta);
8074 break;
8076 default:
8077 gcc_unreachable ();
8080 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8081 *pfn = fold_if_not_in_template (*pfn);
8085 /* Return an expression for PFN from the pointer-to-member function
8086 given by T. */
8088 static tree
8089 pfn_from_ptrmemfunc (tree t)
8091 if (TREE_CODE (t) == PTRMEM_CST)
8093 tree delta;
8094 tree pfn;
8096 expand_ptrmemfunc_cst (t, &delta, &pfn);
8097 if (pfn)
8098 return pfn;
8101 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8104 /* Return an expression for DELTA from the pointer-to-member function
8105 given by T. */
8107 static tree
8108 delta_from_ptrmemfunc (tree t)
8110 if (TREE_CODE (t) == PTRMEM_CST)
8112 tree delta;
8113 tree pfn;
8115 expand_ptrmemfunc_cst (t, &delta, &pfn);
8116 if (delta)
8117 return delta;
8120 return build_ptrmemfunc_access_expr (t, delta_identifier);
8123 /* Convert value RHS to type TYPE as preparation for an assignment to
8124 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8125 implicit conversion is. If FNDECL is non-NULL, we are doing the
8126 conversion in order to pass the PARMNUMth argument of FNDECL.
8127 If FNDECL is NULL, we are doing the conversion in function pointer
8128 argument passing, conversion in initialization, etc. */
8130 static tree
8131 convert_for_assignment (tree type, tree rhs,
8132 impl_conv_rhs errtype, tree fndecl, int parmnum,
8133 tsubst_flags_t complain, int flags)
8135 tree rhstype;
8136 enum tree_code coder;
8138 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8139 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8140 rhs = TREE_OPERAND (rhs, 0);
8142 rhstype = TREE_TYPE (rhs);
8143 coder = TREE_CODE (rhstype);
8145 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8146 && vector_types_convertible_p (type, rhstype, true))
8148 rhs = mark_rvalue_use (rhs);
8149 return convert (type, rhs);
8152 if (rhs == error_mark_node || rhstype == error_mark_node)
8153 return error_mark_node;
8154 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8155 return error_mark_node;
8157 /* The RHS of an assignment cannot have void type. */
8158 if (coder == VOID_TYPE)
8160 if (complain & tf_error)
8161 error ("void value not ignored as it ought to be");
8162 return error_mark_node;
8165 if (c_dialect_objc ())
8167 int parmno;
8168 tree selector;
8169 tree rname = fndecl;
8171 switch (errtype)
8173 case ICR_ASSIGN:
8174 parmno = -1;
8175 break;
8176 case ICR_INIT:
8177 parmno = -2;
8178 break;
8179 default:
8180 selector = objc_message_selector ();
8181 parmno = parmnum;
8182 if (selector && parmno > 1)
8184 rname = selector;
8185 parmno -= 1;
8189 if (objc_compare_types (type, rhstype, parmno, rname))
8191 rhs = mark_rvalue_use (rhs);
8192 return convert (type, rhs);
8196 /* [expr.ass]
8198 The expression is implicitly converted (clause _conv_) to the
8199 cv-unqualified type of the left operand.
8201 We allow bad conversions here because by the time we get to this point
8202 we are committed to doing the conversion. If we end up doing a bad
8203 conversion, convert_like will complain. */
8204 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8206 /* When -Wno-pmf-conversions is use, we just silently allow
8207 conversions from pointers-to-members to plain pointers. If
8208 the conversion doesn't work, cp_convert will complain. */
8209 if (!warn_pmf2ptr
8210 && TYPE_PTR_P (type)
8211 && TYPE_PTRMEMFUNC_P (rhstype))
8212 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8213 else
8215 if (complain & tf_error)
8217 /* If the right-hand side has unknown type, then it is an
8218 overloaded function. Call instantiate_type to get error
8219 messages. */
8220 if (rhstype == unknown_type_node)
8221 instantiate_type (type, rhs, tf_warning_or_error);
8222 else if (fndecl)
8223 error ("cannot convert %qT to %qT for argument %qP to %qD",
8224 rhstype, type, parmnum, fndecl);
8225 else
8226 switch (errtype)
8228 case ICR_DEFAULT_ARGUMENT:
8229 error ("cannot convert %qT to %qT in default argument",
8230 rhstype, type);
8231 break;
8232 case ICR_ARGPASS:
8233 error ("cannot convert %qT to %qT in argument passing",
8234 rhstype, type);
8235 break;
8236 case ICR_CONVERTING:
8237 error ("cannot convert %qT to %qT",
8238 rhstype, type);
8239 break;
8240 case ICR_INIT:
8241 error ("cannot convert %qT to %qT in initialization",
8242 rhstype, type);
8243 break;
8244 case ICR_RETURN:
8245 error ("cannot convert %qT to %qT in return",
8246 rhstype, type);
8247 break;
8248 case ICR_ASSIGN:
8249 error ("cannot convert %qT to %qT in assignment",
8250 rhstype, type);
8251 break;
8252 default:
8253 gcc_unreachable();
8255 if (TYPE_PTR_P (rhstype)
8256 && TYPE_PTR_P (type)
8257 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8258 && CLASS_TYPE_P (TREE_TYPE (type))
8259 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8260 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8261 (TREE_TYPE (rhstype))),
8262 "class type %qT is incomplete", TREE_TYPE (rhstype));
8264 return error_mark_node;
8267 if (warn_suggest_attribute_format)
8269 const enum tree_code codel = TREE_CODE (type);
8270 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8271 && coder == codel
8272 && check_missing_format_attribute (type, rhstype)
8273 && (complain & tf_warning))
8274 switch (errtype)
8276 case ICR_ARGPASS:
8277 case ICR_DEFAULT_ARGUMENT:
8278 if (fndecl)
8279 warning (OPT_Wsuggest_attribute_format,
8280 "parameter %qP of %qD might be a candidate "
8281 "for a format attribute", parmnum, fndecl);
8282 else
8283 warning (OPT_Wsuggest_attribute_format,
8284 "parameter might be a candidate "
8285 "for a format attribute");
8286 break;
8287 case ICR_CONVERTING:
8288 warning (OPT_Wsuggest_attribute_format,
8289 "target of conversion might be a candidate "
8290 "for a format attribute");
8291 break;
8292 case ICR_INIT:
8293 warning (OPT_Wsuggest_attribute_format,
8294 "target of initialization might be a candidate "
8295 "for a format attribute");
8296 break;
8297 case ICR_RETURN:
8298 warning (OPT_Wsuggest_attribute_format,
8299 "return type might be a candidate "
8300 "for a format attribute");
8301 break;
8302 case ICR_ASSIGN:
8303 warning (OPT_Wsuggest_attribute_format,
8304 "left-hand side of assignment might be a candidate "
8305 "for a format attribute");
8306 break;
8307 default:
8308 gcc_unreachable();
8312 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8313 does not. */
8314 if (warn_parentheses
8315 && TREE_CODE (type) == BOOLEAN_TYPE
8316 && TREE_CODE (rhs) == MODIFY_EXPR
8317 && !TREE_NO_WARNING (rhs)
8318 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8319 && (complain & tf_warning))
8321 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8323 warning_at (loc, OPT_Wparentheses,
8324 "suggest parentheses around assignment used as truth value");
8325 TREE_NO_WARNING (rhs) = 1;
8328 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8329 complain, flags);
8332 /* Convert RHS to be of type TYPE.
8333 If EXP is nonzero, it is the target of the initialization.
8334 ERRTYPE indicates what kind of error the implicit conversion is.
8336 Two major differences between the behavior of
8337 `convert_for_assignment' and `convert_for_initialization'
8338 are that references are bashed in the former, while
8339 copied in the latter, and aggregates are assigned in
8340 the former (operator=) while initialized in the
8341 latter (X(X&)).
8343 If using constructor make sure no conversion operator exists, if one does
8344 exist, an ambiguity exists. */
8346 tree
8347 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8348 impl_conv_rhs errtype, tree fndecl, int parmnum,
8349 tsubst_flags_t complain)
8351 enum tree_code codel = TREE_CODE (type);
8352 tree rhstype;
8353 enum tree_code coder;
8355 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8356 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8357 if (TREE_CODE (rhs) == NOP_EXPR
8358 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8359 && codel != REFERENCE_TYPE)
8360 rhs = TREE_OPERAND (rhs, 0);
8362 if (type == error_mark_node
8363 || rhs == error_mark_node
8364 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8365 return error_mark_node;
8367 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8368 && TREE_CODE (type) != ARRAY_TYPE
8369 && (TREE_CODE (type) != REFERENCE_TYPE
8370 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8371 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8372 && !TYPE_REFFN_P (type))
8373 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8374 rhs = decay_conversion (rhs, complain);
8376 rhstype = TREE_TYPE (rhs);
8377 coder = TREE_CODE (rhstype);
8379 if (coder == ERROR_MARK)
8380 return error_mark_node;
8382 /* We accept references to incomplete types, so we can
8383 return here before checking if RHS is of complete type. */
8385 if (codel == REFERENCE_TYPE)
8387 /* This should eventually happen in convert_arguments. */
8388 int savew = 0, savee = 0;
8390 if (fndecl)
8391 savew = warningcount + werrorcount, savee = errorcount;
8392 rhs = initialize_reference (type, rhs, flags, complain);
8394 if (fndecl
8395 && (warningcount + werrorcount > savew || errorcount > savee))
8396 inform (DECL_SOURCE_LOCATION (fndecl),
8397 "in passing argument %P of %qD", parmnum, fndecl);
8399 return rhs;
8402 if (exp != 0)
8403 exp = require_complete_type_sfinae (exp, complain);
8404 if (exp == error_mark_node)
8405 return error_mark_node;
8407 rhstype = non_reference (rhstype);
8409 type = complete_type (type);
8411 if (DIRECT_INIT_EXPR_P (type, rhs))
8412 /* Don't try to do copy-initialization if we already have
8413 direct-initialization. */
8414 return rhs;
8416 if (MAYBE_CLASS_TYPE_P (type))
8417 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8419 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8420 complain, flags);
8423 /* If RETVAL is the address of, or a reference to, a local variable or
8424 temporary give an appropriate warning and return true. */
8426 static bool
8427 maybe_warn_about_returning_address_of_local (tree retval)
8429 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8430 tree whats_returned = retval;
8432 for (;;)
8434 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8435 whats_returned = TREE_OPERAND (whats_returned, 1);
8436 else if (CONVERT_EXPR_P (whats_returned)
8437 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8438 whats_returned = TREE_OPERAND (whats_returned, 0);
8439 else
8440 break;
8443 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8444 return false;
8445 whats_returned = TREE_OPERAND (whats_returned, 0);
8447 while (TREE_CODE (whats_returned) == COMPONENT_REF
8448 || TREE_CODE (whats_returned) == ARRAY_REF)
8449 whats_returned = TREE_OPERAND (whats_returned, 0);
8451 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8453 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8454 || TREE_CODE (whats_returned) == TARGET_EXPR)
8456 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8457 return true;
8459 if (VAR_P (whats_returned)
8460 && DECL_NAME (whats_returned)
8461 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8463 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8464 return true;
8468 if (DECL_P (whats_returned)
8469 && DECL_NAME (whats_returned)
8470 && DECL_FUNCTION_SCOPE_P (whats_returned)
8471 && !is_capture_proxy (whats_returned)
8472 && !(TREE_STATIC (whats_returned)
8473 || TREE_PUBLIC (whats_returned)))
8475 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8476 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8477 OPT_Wreturn_local_addr,
8478 "reference to local variable %qD returned",
8479 whats_returned);
8480 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8481 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8482 OPT_Wreturn_local_addr, "address of label %qD returned",
8483 whats_returned);
8484 else
8485 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8486 OPT_Wreturn_local_addr, "address of local variable %qD "
8487 "returned", whats_returned);
8488 return true;
8491 return false;
8494 /* Check that returning RETVAL from the current function is valid.
8495 Return an expression explicitly showing all conversions required to
8496 change RETVAL into the function return type, and to assign it to
8497 the DECL_RESULT for the function. Set *NO_WARNING to true if
8498 code reaches end of non-void function warning shouldn't be issued
8499 on this RETURN_EXPR. */
8501 tree
8502 check_return_expr (tree retval, bool *no_warning)
8504 tree result;
8505 /* The type actually returned by the function. */
8506 tree valtype;
8507 /* The type the function is declared to return, or void if
8508 the declared type is incomplete. */
8509 tree functype;
8510 int fn_returns_value_p;
8511 bool named_return_value_okay_p;
8513 *no_warning = false;
8515 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8517 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8518 "statement is not allowed");
8519 return NULL_TREE;
8522 /* A `volatile' function is one that isn't supposed to return, ever.
8523 (This is a G++ extension, used to get better code for functions
8524 that call the `volatile' function.) */
8525 if (TREE_THIS_VOLATILE (current_function_decl))
8526 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8528 /* Check for various simple errors. */
8529 if (DECL_DESTRUCTOR_P (current_function_decl))
8531 if (retval)
8532 error ("returning a value from a destructor");
8533 return NULL_TREE;
8535 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8537 if (in_function_try_handler)
8538 /* If a return statement appears in a handler of the
8539 function-try-block of a constructor, the program is ill-formed. */
8540 error ("cannot return from a handler of a function-try-block of a constructor");
8541 else if (retval)
8542 /* You can't return a value from a constructor. */
8543 error ("returning a value from a constructor");
8544 return NULL_TREE;
8547 const tree saved_retval = retval;
8549 if (processing_template_decl)
8551 current_function_returns_value = 1;
8553 if (check_for_bare_parameter_packs (retval))
8554 return error_mark_node;
8556 if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
8557 || (retval != NULL_TREE
8558 && type_dependent_expression_p (retval)))
8559 return retval;
8562 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8564 /* Deduce auto return type from a return statement. */
8565 if (current_function_auto_return_pattern)
8567 tree auto_node;
8568 tree type;
8570 if (!retval && !is_auto (current_function_auto_return_pattern))
8572 /* Give a helpful error message. */
8573 error ("return-statement with no value, in function returning %qT",
8574 current_function_auto_return_pattern);
8575 inform (input_location, "only plain %<auto%> return type can be "
8576 "deduced to %<void%>");
8577 type = error_mark_node;
8579 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8581 error ("returning initializer list");
8582 type = error_mark_node;
8584 else
8586 if (!retval)
8587 retval = void_node;
8588 auto_node = type_uses_auto (current_function_auto_return_pattern);
8589 type = do_auto_deduction (current_function_auto_return_pattern,
8590 retval, auto_node);
8593 if (type == error_mark_node)
8594 /* Leave it. */;
8595 else if (functype == current_function_auto_return_pattern)
8596 apply_deduced_return_type (current_function_decl, type);
8597 else
8598 /* A mismatch should have been diagnosed in do_auto_deduction. */
8599 gcc_assert (same_type_p (type, functype));
8600 functype = type;
8603 result = DECL_RESULT (current_function_decl);
8604 valtype = TREE_TYPE (result);
8605 gcc_assert (valtype != NULL_TREE);
8606 fn_returns_value_p = !VOID_TYPE_P (valtype);
8608 /* Check for a return statement with no return value in a function
8609 that's supposed to return a value. */
8610 if (!retval && fn_returns_value_p)
8612 if (functype != error_mark_node)
8613 permerror (input_location, "return-statement with no value, in "
8614 "function returning %qT", valtype);
8615 /* Remember that this function did return. */
8616 current_function_returns_value = 1;
8617 /* And signal caller that TREE_NO_WARNING should be set on the
8618 RETURN_EXPR to avoid control reaches end of non-void function
8619 warnings in tree-cfg.c. */
8620 *no_warning = true;
8622 /* Check for a return statement with a value in a function that
8623 isn't supposed to return a value. */
8624 else if (retval && !fn_returns_value_p)
8626 if (VOID_TYPE_P (TREE_TYPE (retval)))
8627 /* You can return a `void' value from a function of `void'
8628 type. In that case, we have to evaluate the expression for
8629 its side-effects. */
8630 finish_expr_stmt (retval);
8631 else
8632 permerror (input_location, "return-statement with a value, in function "
8633 "returning 'void'");
8634 current_function_returns_null = 1;
8636 /* There's really no value to return, after all. */
8637 return NULL_TREE;
8639 else if (!retval)
8640 /* Remember that this function can sometimes return without a
8641 value. */
8642 current_function_returns_null = 1;
8643 else
8644 /* Remember that this function did return a value. */
8645 current_function_returns_value = 1;
8647 /* Check for erroneous operands -- but after giving ourselves a
8648 chance to provide an error about returning a value from a void
8649 function. */
8650 if (error_operand_p (retval))
8652 current_function_return_value = error_mark_node;
8653 return error_mark_node;
8656 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8657 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8658 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8659 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8660 && ! flag_check_new
8661 && retval && null_ptr_cst_p (retval))
8662 warning (0, "%<operator new%> must not return NULL unless it is "
8663 "declared %<throw()%> (or -fcheck-new is in effect)");
8665 /* Effective C++ rule 15. See also start_function. */
8666 if (warn_ecpp
8667 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8669 bool warn = true;
8671 /* The function return type must be a reference to the current
8672 class. */
8673 if (TREE_CODE (valtype) == REFERENCE_TYPE
8674 && same_type_ignoring_top_level_qualifiers_p
8675 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8677 /* Returning '*this' is obviously OK. */
8678 if (retval == current_class_ref)
8679 warn = false;
8680 /* If we are calling a function whose return type is the same of
8681 the current class reference, it is ok. */
8682 else if (INDIRECT_REF_P (retval)
8683 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8684 warn = false;
8687 if (warn)
8688 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8691 if (processing_template_decl)
8693 /* We should not have changed the return value. */
8694 gcc_assert (retval == saved_retval);
8695 return retval;
8698 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8700 [...] For a function with a class return type, if the expression
8701 in the return statement is the name of a local object, and the cv-
8702 unqualified type of the local object is the same as the function
8703 return type, an implementation is permitted to omit creating the tem-
8704 porary object to hold the function return value [...]
8706 So, if this is a value-returning function that always returns the same
8707 local variable, remember it.
8709 It might be nice to be more flexible, and choose the first suitable
8710 variable even if the function sometimes returns something else, but
8711 then we run the risk of clobbering the variable we chose if the other
8712 returned expression uses the chosen variable somehow. And people expect
8713 this restriction, anyway. (jason 2000-11-19)
8715 See finish_function and finalize_nrv for the rest of this optimization. */
8717 named_return_value_okay_p =
8718 (retval != NULL_TREE
8719 /* Must be a local, automatic variable. */
8720 && VAR_P (retval)
8721 && DECL_CONTEXT (retval) == current_function_decl
8722 && ! TREE_STATIC (retval)
8723 /* And not a lambda or anonymous union proxy. */
8724 && !DECL_HAS_VALUE_EXPR_P (retval)
8725 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8726 /* The cv-unqualified type of the returned value must be the
8727 same as the cv-unqualified return type of the
8728 function. */
8729 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8730 (TYPE_MAIN_VARIANT (functype)))
8731 /* And the returned value must be non-volatile. */
8732 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8734 if (fn_returns_value_p && flag_elide_constructors)
8736 if (named_return_value_okay_p
8737 && (current_function_return_value == NULL_TREE
8738 || current_function_return_value == retval))
8739 current_function_return_value = retval;
8740 else
8741 current_function_return_value = error_mark_node;
8744 /* We don't need to do any conversions when there's nothing being
8745 returned. */
8746 if (!retval)
8747 return NULL_TREE;
8749 /* Do any required conversions. */
8750 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8751 /* No conversions are required. */
8753 else
8755 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8757 /* The functype's return type will have been set to void, if it
8758 was an incomplete type. Just treat this as 'return;' */
8759 if (VOID_TYPE_P (functype))
8760 return error_mark_node;
8762 /* If we had an id-expression obfuscated by force_paren_expr, we need
8763 to undo it so we can try to treat it as an rvalue below. */
8764 if (cxx_dialect >= cxx14
8765 && INDIRECT_REF_P (retval)
8766 && REF_PARENTHESIZED_P (retval))
8768 retval = TREE_OPERAND (retval, 0);
8769 while (TREE_CODE (retval) == NON_LVALUE_EXPR
8770 || TREE_CODE (retval) == NOP_EXPR)
8771 retval = TREE_OPERAND (retval, 0);
8772 gcc_assert (TREE_CODE (retval) == ADDR_EXPR);
8773 retval = TREE_OPERAND (retval, 0);
8776 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8777 treated as an rvalue for the purposes of overload resolution to
8778 favor move constructors over copy constructors.
8780 Note that these conditions are similar to, but not as strict as,
8781 the conditions for the named return value optimization. */
8782 if ((cxx_dialect != cxx98)
8783 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8784 || TREE_CODE (retval) == PARM_DECL)
8785 && DECL_CONTEXT (retval) == current_function_decl
8786 && !TREE_STATIC (retval)
8787 /* This is only interesting for class type. */
8788 && CLASS_TYPE_P (functype))
8789 flags = flags | LOOKUP_PREFER_RVALUE;
8791 /* First convert the value to the function's return type, then
8792 to the type of return value's location to handle the
8793 case that functype is smaller than the valtype. */
8794 retval = convert_for_initialization
8795 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8796 tf_warning_or_error);
8797 retval = convert (valtype, retval);
8799 /* If the conversion failed, treat this just like `return;'. */
8800 if (retval == error_mark_node)
8801 return retval;
8802 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8803 else if (! cfun->returns_struct
8804 && TREE_CODE (retval) == TARGET_EXPR
8805 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8806 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8807 TREE_OPERAND (retval, 0));
8808 else if (maybe_warn_about_returning_address_of_local (retval))
8809 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8810 build_zero_cst (TREE_TYPE (retval)));
8813 /* Actually copy the value returned into the appropriate location. */
8814 if (retval && retval != result)
8815 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8817 return retval;
8821 /* Returns nonzero if the pointer-type FROM can be converted to the
8822 pointer-type TO via a qualification conversion. If CONSTP is -1,
8823 then we return nonzero if the pointers are similar, and the
8824 cv-qualification signature of FROM is a proper subset of that of TO.
8826 If CONSTP is positive, then all outer pointers have been
8827 const-qualified. */
8829 static int
8830 comp_ptr_ttypes_real (tree to, tree from, int constp)
8832 bool to_more_cv_qualified = false;
8833 bool is_opaque_pointer = false;
8835 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8837 if (TREE_CODE (to) != TREE_CODE (from))
8838 return 0;
8840 if (TREE_CODE (from) == OFFSET_TYPE
8841 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8842 TYPE_OFFSET_BASETYPE (to)))
8843 return 0;
8845 /* Const and volatile mean something different for function types,
8846 so the usual checks are not appropriate. */
8847 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8849 if (!at_least_as_qualified_p (to, from))
8850 return 0;
8852 if (!at_least_as_qualified_p (from, to))
8854 if (constp == 0)
8855 return 0;
8856 to_more_cv_qualified = true;
8859 if (constp > 0)
8860 constp &= TYPE_READONLY (to);
8863 if (VECTOR_TYPE_P (to))
8864 is_opaque_pointer = vector_targets_convertible_p (to, from);
8866 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8867 return ((constp >= 0 || to_more_cv_qualified)
8868 && (is_opaque_pointer
8869 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8873 /* When comparing, say, char ** to char const **, this function takes
8874 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8875 types to this function. */
8878 comp_ptr_ttypes (tree to, tree from)
8880 return comp_ptr_ttypes_real (to, from, 1);
8883 /* Returns true iff FNTYPE is a non-class type that involves
8884 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8885 if a parameter type is ill-formed. */
8887 bool
8888 error_type_p (const_tree type)
8890 tree t;
8892 switch (TREE_CODE (type))
8894 case ERROR_MARK:
8895 return true;
8897 case POINTER_TYPE:
8898 case REFERENCE_TYPE:
8899 case OFFSET_TYPE:
8900 return error_type_p (TREE_TYPE (type));
8902 case FUNCTION_TYPE:
8903 case METHOD_TYPE:
8904 if (error_type_p (TREE_TYPE (type)))
8905 return true;
8906 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8907 if (error_type_p (TREE_VALUE (t)))
8908 return true;
8909 return false;
8911 case RECORD_TYPE:
8912 if (TYPE_PTRMEMFUNC_P (type))
8913 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8914 return false;
8916 default:
8917 return false;
8921 /* Returns true if to and from are (possibly multi-level) pointers to the same
8922 type or inheritance-related types, regardless of cv-quals. */
8924 bool
8925 ptr_reasonably_similar (const_tree to, const_tree from)
8927 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8929 /* Any target type is similar enough to void. */
8930 if (VOID_TYPE_P (to))
8931 return !error_type_p (from);
8932 if (VOID_TYPE_P (from))
8933 return !error_type_p (to);
8935 if (TREE_CODE (to) != TREE_CODE (from))
8936 return false;
8938 if (TREE_CODE (from) == OFFSET_TYPE
8939 && comptypes (TYPE_OFFSET_BASETYPE (to),
8940 TYPE_OFFSET_BASETYPE (from),
8941 COMPARE_BASE | COMPARE_DERIVED))
8942 continue;
8944 if (VECTOR_TYPE_P (to)
8945 && vector_types_convertible_p (to, from, false))
8946 return true;
8948 if (TREE_CODE (to) == INTEGER_TYPE
8949 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8950 return true;
8952 if (TREE_CODE (to) == FUNCTION_TYPE)
8953 return !error_type_p (to) && !error_type_p (from);
8955 if (!TYPE_PTR_P (to))
8957 /* When either type is incomplete avoid DERIVED_FROM_P,
8958 which may call complete_type (c++/57942). */
8959 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8960 return comptypes
8961 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8962 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8967 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8968 pointer-to-member types) are the same, ignoring cv-qualification at
8969 all levels. */
8971 bool
8972 comp_ptr_ttypes_const (tree to, tree from)
8974 bool is_opaque_pointer = false;
8976 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8978 if (TREE_CODE (to) != TREE_CODE (from))
8979 return false;
8981 if (TREE_CODE (from) == OFFSET_TYPE
8982 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8983 TYPE_OFFSET_BASETYPE (to)))
8984 continue;
8986 if (VECTOR_TYPE_P (to))
8987 is_opaque_pointer = vector_targets_convertible_p (to, from);
8989 if (!TYPE_PTR_P (to))
8990 return (is_opaque_pointer
8991 || same_type_ignoring_top_level_qualifiers_p (to, from));
8995 /* Returns the type qualifiers for this type, including the qualifiers on the
8996 elements for an array type. */
8999 cp_type_quals (const_tree type)
9001 int quals;
9002 /* This CONST_CAST is okay because strip_array_types returns its
9003 argument unmodified and we assign it to a const_tree. */
9004 type = strip_array_types (CONST_CAST_TREE (type));
9005 if (type == error_mark_node
9006 /* Quals on a FUNCTION_TYPE are memfn quals. */
9007 || TREE_CODE (type) == FUNCTION_TYPE)
9008 return TYPE_UNQUALIFIED;
9009 quals = TYPE_QUALS (type);
9010 /* METHOD and REFERENCE_TYPEs should never have quals. */
9011 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9012 && TREE_CODE (type) != REFERENCE_TYPE)
9013 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9014 == TYPE_UNQUALIFIED));
9015 return quals;
9018 /* Returns the function-ref-qualifier for TYPE */
9020 cp_ref_qualifier
9021 type_memfn_rqual (const_tree type)
9023 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9024 || TREE_CODE (type) == METHOD_TYPE);
9026 if (!FUNCTION_REF_QUALIFIED (type))
9027 return REF_QUAL_NONE;
9028 else if (FUNCTION_RVALUE_QUALIFIED (type))
9029 return REF_QUAL_RVALUE;
9030 else
9031 return REF_QUAL_LVALUE;
9034 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9035 METHOD_TYPE. */
9038 type_memfn_quals (const_tree type)
9040 if (TREE_CODE (type) == FUNCTION_TYPE)
9041 return TYPE_QUALS (type);
9042 else if (TREE_CODE (type) == METHOD_TYPE)
9043 return cp_type_quals (class_of_this_parm (type));
9044 else
9045 gcc_unreachable ();
9048 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9049 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9051 tree
9052 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9054 /* Could handle METHOD_TYPE here if necessary. */
9055 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9056 if (TYPE_QUALS (type) == memfn_quals
9057 && type_memfn_rqual (type) == rqual)
9058 return type;
9060 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9061 complex. */
9062 tree result = build_qualified_type (type, memfn_quals);
9063 if (tree canon = TYPE_CANONICAL (result))
9064 if (canon != result)
9065 /* check_qualified_type doesn't check the ref-qualifier, so make sure
9066 TYPE_CANONICAL is correct. */
9067 TYPE_CANONICAL (result)
9068 = build_ref_qualified_type (canon, type_memfn_rqual (result));
9069 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
9070 return build_ref_qualified_type (result, rqual);
9073 /* Returns nonzero if TYPE is const or volatile. */
9075 bool
9076 cv_qualified_p (const_tree type)
9078 int quals = cp_type_quals (type);
9079 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9082 /* Returns nonzero if the TYPE contains a mutable member. */
9084 bool
9085 cp_has_mutable_p (const_tree type)
9087 /* This CONST_CAST is okay because strip_array_types returns its
9088 argument unmodified and we assign it to a const_tree. */
9089 type = strip_array_types (CONST_CAST_TREE(type));
9091 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9094 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9095 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9096 approximation. In particular, consider:
9098 int f();
9099 struct S { int i; };
9100 const S s = { f(); }
9102 Here, we will make "s" as TREE_READONLY (because it is declared
9103 "const") -- only to reverse ourselves upon seeing that the
9104 initializer is non-constant. */
9106 void
9107 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9109 tree type = TREE_TYPE (decl);
9111 if (type == error_mark_node)
9112 return;
9114 if (TREE_CODE (decl) == TYPE_DECL)
9115 return;
9117 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9118 && type_quals != TYPE_UNQUALIFIED));
9120 /* Avoid setting TREE_READONLY incorrectly. */
9121 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9122 constructor can produce constant init, so rely on cp_finish_decl to
9123 clear TREE_READONLY if the variable has non-constant init. */
9125 /* If the type has (or might have) a mutable component, that component
9126 might be modified. */
9127 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9128 type_quals &= ~TYPE_QUAL_CONST;
9130 c_apply_type_quals_to_decl (type_quals, decl);
9133 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9134 exemplar types such that casting T1 to T2 is casting away constness
9135 if and only if there is no implicit conversion from T1 to T2. */
9137 static void
9138 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9140 int quals1;
9141 int quals2;
9143 /* [expr.const.cast]
9145 For multi-level pointer to members and multi-level mixed pointers
9146 and pointers to members (conv.qual), the "member" aspect of a
9147 pointer to member level is ignored when determining if a const
9148 cv-qualifier has been cast away. */
9149 /* [expr.const.cast]
9151 For two pointer types:
9153 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9154 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9155 K is min(N,M)
9157 casting from X1 to X2 casts away constness if, for a non-pointer
9158 type T there does not exist an implicit conversion (clause
9159 _conv_) from:
9161 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9165 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9166 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9167 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9169 *t1 = cp_build_qualified_type (void_type_node,
9170 cp_type_quals (*t1));
9171 *t2 = cp_build_qualified_type (void_type_node,
9172 cp_type_quals (*t2));
9173 return;
9176 quals1 = cp_type_quals (*t1);
9177 quals2 = cp_type_quals (*t2);
9179 if (TYPE_PTRDATAMEM_P (*t1))
9180 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9181 else
9182 *t1 = TREE_TYPE (*t1);
9183 if (TYPE_PTRDATAMEM_P (*t2))
9184 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9185 else
9186 *t2 = TREE_TYPE (*t2);
9188 casts_away_constness_r (t1, t2, complain);
9189 *t1 = build_pointer_type (*t1);
9190 *t2 = build_pointer_type (*t2);
9191 *t1 = cp_build_qualified_type (*t1, quals1);
9192 *t2 = cp_build_qualified_type (*t2, quals2);
9195 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9196 constness.
9198 ??? This function returns non-zero if casting away qualifiers not
9199 just const. We would like to return to the caller exactly which
9200 qualifiers are casted away to give more accurate diagnostics.
9203 static bool
9204 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9206 if (TREE_CODE (t2) == REFERENCE_TYPE)
9208 /* [expr.const.cast]
9210 Casting from an lvalue of type T1 to an lvalue of type T2
9211 using a reference cast casts away constness if a cast from an
9212 rvalue of type "pointer to T1" to the type "pointer to T2"
9213 casts away constness. */
9214 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9215 return casts_away_constness (build_pointer_type (t1),
9216 build_pointer_type (TREE_TYPE (t2)),
9217 complain);
9220 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9221 /* [expr.const.cast]
9223 Casting from an rvalue of type "pointer to data member of X
9224 of type T1" to the type "pointer to data member of Y of type
9225 T2" casts away constness if a cast from an rvalue of type
9226 "pointer to T1" to the type "pointer to T2" casts away
9227 constness. */
9228 return casts_away_constness
9229 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9230 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9231 complain);
9233 /* Casting away constness is only something that makes sense for
9234 pointer or reference types. */
9235 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9236 return false;
9238 /* Top-level qualifiers don't matter. */
9239 t1 = TYPE_MAIN_VARIANT (t1);
9240 t2 = TYPE_MAIN_VARIANT (t2);
9241 casts_away_constness_r (&t1, &t2, complain);
9242 if (!can_convert (t2, t1, complain))
9243 return true;
9245 return false;
9248 /* If T is a REFERENCE_TYPE return the type to which T refers.
9249 Otherwise, return T itself. */
9251 tree
9252 non_reference (tree t)
9254 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9255 t = TREE_TYPE (t);
9256 return t;
9260 /* Return nonzero if REF is an lvalue valid for this language;
9261 otherwise, print an error message and return zero. USE says
9262 how the lvalue is being used and so selects the error message. */
9265 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9267 cp_lvalue_kind kind = lvalue_kind (ref);
9269 if (kind == clk_none)
9271 if (complain & tf_error)
9272 lvalue_error (input_location, use);
9273 return 0;
9275 else if (kind & (clk_rvalueref|clk_class))
9277 if (!(complain & tf_error))
9278 return 0;
9279 if (kind & clk_class)
9280 /* Make this a permerror because we used to accept it. */
9281 permerror (input_location, "using temporary as lvalue");
9282 else
9283 error ("using xvalue (rvalue reference) as lvalue");
9285 return 1;
9288 /* Return true if a user-defined literal operator is a raw operator. */
9290 bool
9291 check_raw_literal_operator (const_tree decl)
9293 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9294 tree argtype;
9295 int arity;
9296 bool maybe_raw_p = false;
9298 /* Count the number and type of arguments and check for ellipsis. */
9299 for (argtype = argtypes, arity = 0;
9300 argtype && argtype != void_list_node;
9301 ++arity, argtype = TREE_CHAIN (argtype))
9303 tree t = TREE_VALUE (argtype);
9305 if (same_type_p (t, const_string_type_node))
9306 maybe_raw_p = true;
9308 if (!argtype)
9309 return false; /* Found ellipsis. */
9311 if (!maybe_raw_p || arity != 1)
9312 return false;
9314 return true;
9318 /* Return true if a user-defined literal operator has one of the allowed
9319 argument types. */
9321 bool
9322 check_literal_operator_args (const_tree decl,
9323 bool *long_long_unsigned_p, bool *long_double_p)
9325 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9327 *long_long_unsigned_p = false;
9328 *long_double_p = false;
9329 if (processing_template_decl || processing_specialization)
9330 return argtypes == void_list_node;
9331 else
9333 tree argtype;
9334 int arity;
9335 int max_arity = 2;
9337 /* Count the number and type of arguments and check for ellipsis. */
9338 for (argtype = argtypes, arity = 0;
9339 argtype && argtype != void_list_node;
9340 argtype = TREE_CHAIN (argtype))
9342 tree t = TREE_VALUE (argtype);
9343 ++arity;
9345 if (TYPE_PTR_P (t))
9347 bool maybe_raw_p = false;
9348 t = TREE_TYPE (t);
9349 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9350 return false;
9351 t = TYPE_MAIN_VARIANT (t);
9352 if ((maybe_raw_p = same_type_p (t, char_type_node))
9353 || same_type_p (t, wchar_type_node)
9354 || same_type_p (t, char16_type_node)
9355 || same_type_p (t, char32_type_node))
9357 argtype = TREE_CHAIN (argtype);
9358 if (!argtype)
9359 return false;
9360 t = TREE_VALUE (argtype);
9361 if (maybe_raw_p && argtype == void_list_node)
9362 return true;
9363 else if (same_type_p (t, size_type_node))
9365 ++arity;
9366 continue;
9368 else
9369 return false;
9372 else if (same_type_p (t, long_long_unsigned_type_node))
9374 max_arity = 1;
9375 *long_long_unsigned_p = true;
9377 else if (same_type_p (t, long_double_type_node))
9379 max_arity = 1;
9380 *long_double_p = true;
9382 else if (same_type_p (t, char_type_node))
9383 max_arity = 1;
9384 else if (same_type_p (t, wchar_type_node))
9385 max_arity = 1;
9386 else if (same_type_p (t, char16_type_node))
9387 max_arity = 1;
9388 else if (same_type_p (t, char32_type_node))
9389 max_arity = 1;
9390 else
9391 return false;
9393 if (!argtype)
9394 return false; /* Found ellipsis. */
9396 if (arity != max_arity)
9397 return false;
9399 return true;
9403 /* Always returns false since unlike C90, C++ has no concept of implicit
9404 function declarations. */
9406 bool
9407 c_decl_implicit (const_tree)
9409 return false;