Merge from trunk @217148.
[official-gcc.git] / gcc / cp / typeck.c
blobecd5328acdea12b6acd09b29113c91da44ad6b6e
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "diagnostic.h"
37 #include "intl.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "c-family/c-ubsan.h"
43 #include "params.h"
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
48 tsubst_flags_t, int);
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
50 static tree rationalize_conditional_expr (enum tree_code, tree,
51 tsubst_flags_t);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
58 static bool casts_away_constness (tree, tree, tsubst_flags_t);
59 static bool maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
63 tsubst_flags_t);
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
70 tree
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
73 tree type;
75 if (processing_template_decl || value == error_mark_node)
76 return value;
78 if (TREE_CODE (value) == OVERLOAD)
79 type = unknown_type_node;
80 else
81 type = TREE_TYPE (value);
83 if (type == error_mark_node)
84 return error_mark_node;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type))
88 return value;
90 if (complete_type_or_maybe_complain (type, value, complain))
91 return value;
92 else
93 return error_mark_node;
96 tree
97 require_complete_type (tree value)
99 return require_complete_type_sfinae (value, tf_warning_or_error);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
107 tree
108 complete_type (tree type)
110 if (type == NULL_TREE)
111 /* Rather than crash, we return something sure to cause an error
112 at some point. */
113 return error_mark_node;
115 if (type == error_mark_node || COMPLETE_TYPE_P (type))
117 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
119 tree t = complete_type (TREE_TYPE (type));
120 unsigned int needs_constructing, has_nontrivial_dtor;
121 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
122 layout_type (type);
123 needs_constructing
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
125 has_nontrivial_dtor
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
129 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
136 return type;
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
143 tree
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
146 type = complete_type (type);
147 if (type == error_mark_node)
148 /* We already issued an error. */
149 return NULL_TREE;
150 else if (!COMPLETE_TYPE_P (type))
152 if (complain & tf_error)
153 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
154 return NULL_TREE;
156 else
157 return type;
160 tree
161 complete_type_or_else (tree type, tree value)
163 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
166 /* Return truthvalue of whether type of EXP is instantiated. */
169 type_unknown_p (const_tree exp)
171 return (TREE_CODE (exp) == TREE_LIST
172 || TREE_TYPE (exp) == unknown_type_node);
176 /* Return the common type of two parameter lists.
177 We assume that comptypes has already been done and returned 1;
178 if that isn't so, this may crash.
180 As an optimization, free the space we allocate if the parameter
181 lists are already common. */
183 static tree
184 commonparms (tree p1, tree p2)
186 tree oldargs = p1, newargs, n;
187 int i, len;
188 int any_change = 0;
190 len = list_length (p1);
191 newargs = tree_last (p1);
193 if (newargs == void_list_node)
194 i = 1;
195 else
197 i = 0;
198 newargs = 0;
201 for (; i < len; i++)
202 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
204 n = newargs;
206 for (i = 0; p1;
207 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
209 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
212 any_change = 1;
214 else if (! TREE_PURPOSE (p1))
216 if (TREE_PURPOSE (p2))
218 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219 any_change = 1;
222 else
224 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
225 any_change = 1;
226 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
228 if (TREE_VALUE (p1) != TREE_VALUE (p2))
230 any_change = 1;
231 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
233 else
234 TREE_VALUE (n) = TREE_VALUE (p1);
236 if (! any_change)
237 return oldargs;
239 return newargs;
242 /* Given a type, perhaps copied for a typedef,
243 find the "original" version of it. */
244 static tree
245 original_type (tree t)
247 int quals = cp_type_quals (t);
248 while (t != error_mark_node
249 && TYPE_NAME (t) != NULL_TREE)
251 tree x = TYPE_NAME (t);
252 if (TREE_CODE (x) != TYPE_DECL)
253 break;
254 x = DECL_ORIGINAL_TYPE (x);
255 if (x == NULL_TREE)
256 break;
257 t = x;
259 return cp_build_qualified_type (t, quals);
262 /* Return the common type for two arithmetic types T1 and T2 under the
263 usual arithmetic conversions. The default conversions have already
264 been applied, and enumerated types converted to their compatible
265 integer types. */
267 static tree
268 cp_common_type (tree t1, tree t2)
270 enum tree_code code1 = TREE_CODE (t1);
271 enum tree_code code2 = TREE_CODE (t2);
272 tree attributes;
273 int i;
276 /* In what follows, we slightly generalize the rules given in [expr] so
277 as to deal with `long long' and `complex'. First, merge the
278 attributes. */
279 attributes = (*targetm.merge_type_attributes) (t1, t2);
281 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
283 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
284 return build_type_attribute_variant (t1, attributes);
285 else
286 return NULL_TREE;
289 /* FIXME: Attributes. */
290 gcc_assert (ARITHMETIC_TYPE_P (t1)
291 || TREE_CODE (t1) == VECTOR_TYPE
292 || UNSCOPED_ENUM_P (t1));
293 gcc_assert (ARITHMETIC_TYPE_P (t2)
294 || TREE_CODE (t2) == VECTOR_TYPE
295 || UNSCOPED_ENUM_P (t2));
297 /* If one type is complex, form the common type of the non-complex
298 components, then make that complex. Use T1 or T2 if it is the
299 required type. */
300 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
302 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
303 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
304 tree subtype
305 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
307 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
308 return build_type_attribute_variant (t1, attributes);
309 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
310 return build_type_attribute_variant (t2, attributes);
311 else
312 return build_type_attribute_variant (build_complex_type (subtype),
313 attributes);
316 if (code1 == VECTOR_TYPE)
318 /* When we get here we should have two vectors of the same size.
319 Just prefer the unsigned one if present. */
320 if (TYPE_UNSIGNED (t1))
321 return build_type_attribute_variant (t1, attributes);
322 else
323 return build_type_attribute_variant (t2, attributes);
326 /* If only one is real, use it as the result. */
327 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
328 return build_type_attribute_variant (t1, attributes);
329 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
330 return build_type_attribute_variant (t2, attributes);
332 /* Both real or both integers; use the one with greater precision. */
333 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
334 return build_type_attribute_variant (t1, attributes);
335 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
336 return build_type_attribute_variant (t2, attributes);
338 /* The types are the same; no need to do anything fancy. */
339 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
340 return build_type_attribute_variant (t1, attributes);
342 if (code1 != REAL_TYPE)
344 /* If one is unsigned long long, then convert the other to unsigned
345 long long. */
346 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
347 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
348 return build_type_attribute_variant (long_long_unsigned_type_node,
349 attributes);
350 /* If one is a long long, and the other is an unsigned long, and
351 long long can represent all the values of an unsigned long, then
352 convert to a long long. Otherwise, convert to an unsigned long
353 long. Otherwise, if either operand is long long, convert the
354 other to long long.
356 Since we're here, we know the TYPE_PRECISION is the same;
357 therefore converting to long long cannot represent all the values
358 of an unsigned long, so we choose unsigned long long in that
359 case. */
360 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
361 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
363 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
364 ? long_long_unsigned_type_node
365 : long_long_integer_type_node);
366 return build_type_attribute_variant (t, attributes);
369 /* Go through the same procedure, but for longs. */
370 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
371 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
372 return build_type_attribute_variant (long_unsigned_type_node,
373 attributes);
374 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
375 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
377 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
378 ? long_unsigned_type_node : long_integer_type_node);
379 return build_type_attribute_variant (t, attributes);
382 /* For __intN types, either the type is __int128 (and is lower
383 priority than the types checked above, but higher than other
384 128-bit types) or it's known to not be the same size as other
385 types (enforced in toplev.c). Prefer the unsigned type. */
386 for (i = 0; i < NUM_INT_N_ENTS; i ++)
388 if (int_n_enabled_p [i]
389 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
390 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
391 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
392 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
394 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
395 ? int_n_trees[i].unsigned_type
396 : int_n_trees[i].signed_type);
397 return build_type_attribute_variant (t, attributes);
401 /* Otherwise prefer the unsigned one. */
402 if (TYPE_UNSIGNED (t1))
403 return build_type_attribute_variant (t1, attributes);
404 else
405 return build_type_attribute_variant (t2, attributes);
407 else
409 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
410 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
411 return build_type_attribute_variant (long_double_type_node,
412 attributes);
413 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
414 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
415 return build_type_attribute_variant (double_type_node,
416 attributes);
417 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
418 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
419 return build_type_attribute_variant (float_type_node,
420 attributes);
422 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
423 the standard C++ floating-point types. Logic earlier in this
424 function has already eliminated the possibility that
425 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
426 compelling reason to choose one or the other. */
427 return build_type_attribute_variant (t1, attributes);
431 /* T1 and T2 are arithmetic or enumeration types. Return the type
432 that will result from the "usual arithmetic conversions" on T1 and
433 T2 as described in [expr]. */
435 tree
436 type_after_usual_arithmetic_conversions (tree t1, tree t2)
438 gcc_assert (ARITHMETIC_TYPE_P (t1)
439 || TREE_CODE (t1) == VECTOR_TYPE
440 || UNSCOPED_ENUM_P (t1));
441 gcc_assert (ARITHMETIC_TYPE_P (t2)
442 || TREE_CODE (t2) == VECTOR_TYPE
443 || UNSCOPED_ENUM_P (t2));
445 /* Perform the integral promotions. We do not promote real types here. */
446 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
447 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
449 t1 = type_promotes_to (t1);
450 t2 = type_promotes_to (t2);
453 return cp_common_type (t1, t2);
456 static void
457 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
458 composite_pointer_operation operation)
460 switch (operation)
462 case CPO_COMPARISON:
463 emit_diagnostic (kind, input_location, 0,
464 "comparison between "
465 "distinct pointer types %qT and %qT lacks a cast",
466 t1, t2);
467 break;
468 case CPO_CONVERSION:
469 emit_diagnostic (kind, input_location, 0,
470 "conversion between "
471 "distinct pointer types %qT and %qT lacks a cast",
472 t1, t2);
473 break;
474 case CPO_CONDITIONAL_EXPR:
475 emit_diagnostic (kind, input_location, 0,
476 "conditional expression between "
477 "distinct pointer types %qT and %qT lacks a cast",
478 t1, t2);
479 break;
480 default:
481 gcc_unreachable ();
485 /* Subroutine of composite_pointer_type to implement the recursive
486 case. See that function for documentation of the parameters. */
488 static tree
489 composite_pointer_type_r (tree t1, tree t2,
490 composite_pointer_operation operation,
491 tsubst_flags_t complain)
493 tree pointee1;
494 tree pointee2;
495 tree result_type;
496 tree attributes;
498 /* Determine the types pointed to by T1 and T2. */
499 if (TYPE_PTR_P (t1))
501 pointee1 = TREE_TYPE (t1);
502 pointee2 = TREE_TYPE (t2);
504 else
506 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
507 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
510 /* [expr.rel]
512 Otherwise, the composite pointer type is a pointer type
513 similar (_conv.qual_) to the type of one of the operands,
514 with a cv-qualification signature (_conv.qual_) that is the
515 union of the cv-qualification signatures of the operand
516 types. */
517 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
518 result_type = pointee1;
519 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
520 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
522 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
523 complain);
524 if (result_type == error_mark_node)
525 return error_mark_node;
527 else
529 if (complain & tf_error)
530 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
531 else
532 return error_mark_node;
533 result_type = void_type_node;
535 result_type = cp_build_qualified_type (result_type,
536 (cp_type_quals (pointee1)
537 | cp_type_quals (pointee2)));
538 /* If the original types were pointers to members, so is the
539 result. */
540 if (TYPE_PTRMEM_P (t1))
542 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
543 TYPE_PTRMEM_CLASS_TYPE (t2)))
545 if (complain & tf_error)
546 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
547 else
548 return error_mark_node;
550 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
551 result_type);
553 else
554 result_type = build_pointer_type (result_type);
556 /* Merge the attributes. */
557 attributes = (*targetm.merge_type_attributes) (t1, t2);
558 return build_type_attribute_variant (result_type, attributes);
561 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
562 ARG1 and ARG2 are the values with those types. The OPERATION is to
563 describe the operation between the pointer types,
564 in case an error occurs.
566 This routine also implements the computation of a common type for
567 pointers-to-members as per [expr.eq]. */
569 tree
570 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
571 composite_pointer_operation operation,
572 tsubst_flags_t complain)
574 tree class1;
575 tree class2;
577 /* [expr.rel]
579 If one operand is a null pointer constant, the composite pointer
580 type is the type of the other operand. */
581 if (null_ptr_cst_p (arg1))
582 return t2;
583 if (null_ptr_cst_p (arg2))
584 return t1;
586 /* We have:
588 [expr.rel]
590 If one of the operands has type "pointer to cv1 void*", then
591 the other has type "pointer to cv2T", and the composite pointer
592 type is "pointer to cv12 void", where cv12 is the union of cv1
593 and cv2.
595 If either type is a pointer to void, make sure it is T1. */
596 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
598 tree t;
599 t = t1;
600 t1 = t2;
601 t2 = t;
604 /* Now, if T1 is a pointer to void, merge the qualifiers. */
605 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
607 tree attributes;
608 tree result_type;
610 if (TYPE_PTRFN_P (t2))
612 if (complain & tf_error)
614 switch (operation)
616 case CPO_COMPARISON:
617 pedwarn (input_location, OPT_Wpedantic,
618 "ISO C++ forbids comparison between pointer "
619 "of type %<void *%> and pointer-to-function");
620 break;
621 case CPO_CONVERSION:
622 pedwarn (input_location, OPT_Wpedantic,
623 "ISO C++ forbids conversion between pointer "
624 "of type %<void *%> and pointer-to-function");
625 break;
626 case CPO_CONDITIONAL_EXPR:
627 pedwarn (input_location, OPT_Wpedantic,
628 "ISO C++ forbids conditional expression between "
629 "pointer of type %<void *%> and "
630 "pointer-to-function");
631 break;
632 default:
633 gcc_unreachable ();
636 else
637 return error_mark_node;
639 result_type
640 = cp_build_qualified_type (void_type_node,
641 (cp_type_quals (TREE_TYPE (t1))
642 | cp_type_quals (TREE_TYPE (t2))));
643 result_type = build_pointer_type (result_type);
644 /* Merge the attributes. */
645 attributes = (*targetm.merge_type_attributes) (t1, t2);
646 return build_type_attribute_variant (result_type, attributes);
649 if (c_dialect_objc () && TYPE_PTR_P (t1)
650 && TYPE_PTR_P (t2))
652 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
653 return objc_common_type (t1, t2);
656 /* [expr.eq] permits the application of a pointer conversion to
657 bring the pointers to a common type. */
658 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
659 && CLASS_TYPE_P (TREE_TYPE (t1))
660 && CLASS_TYPE_P (TREE_TYPE (t2))
661 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
662 TREE_TYPE (t2)))
664 class1 = TREE_TYPE (t1);
665 class2 = TREE_TYPE (t2);
667 if (DERIVED_FROM_P (class1, class2))
668 t2 = (build_pointer_type
669 (cp_build_qualified_type (class1, cp_type_quals (class2))));
670 else if (DERIVED_FROM_P (class2, class1))
671 t1 = (build_pointer_type
672 (cp_build_qualified_type (class2, cp_type_quals (class1))));
673 else
675 if (complain & tf_error)
676 composite_pointer_error (DK_ERROR, t1, t2, operation);
677 return error_mark_node;
680 /* [expr.eq] permits the application of a pointer-to-member
681 conversion to change the class type of one of the types. */
682 else if (TYPE_PTRMEM_P (t1)
683 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
684 TYPE_PTRMEM_CLASS_TYPE (t2)))
686 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
687 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
689 if (DERIVED_FROM_P (class1, class2))
690 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
691 else if (DERIVED_FROM_P (class2, class1))
692 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
693 else
695 if (complain & tf_error)
696 switch (operation)
698 case CPO_COMPARISON:
699 error ("comparison between distinct "
700 "pointer-to-member types %qT and %qT lacks a cast",
701 t1, t2);
702 break;
703 case CPO_CONVERSION:
704 error ("conversion between distinct "
705 "pointer-to-member types %qT and %qT lacks a cast",
706 t1, t2);
707 break;
708 case CPO_CONDITIONAL_EXPR:
709 error ("conditional expression between distinct "
710 "pointer-to-member types %qT and %qT lacks a cast",
711 t1, t2);
712 break;
713 default:
714 gcc_unreachable ();
716 return error_mark_node;
720 return composite_pointer_type_r (t1, t2, operation, complain);
723 /* Return the merged type of two types.
724 We assume that comptypes has already been done and returned 1;
725 if that isn't so, this may crash.
727 This just combines attributes and default arguments; any other
728 differences would cause the two types to compare unalike. */
730 tree
731 merge_types (tree t1, tree t2)
733 enum tree_code code1;
734 enum tree_code code2;
735 tree attributes;
737 /* Save time if the two types are the same. */
738 if (t1 == t2)
739 return t1;
740 if (original_type (t1) == original_type (t2))
741 return t1;
743 /* If one type is nonsense, use the other. */
744 if (t1 == error_mark_node)
745 return t2;
746 if (t2 == error_mark_node)
747 return t1;
749 /* Handle merging an auto redeclaration with a previous deduced
750 return type. */
751 if (is_auto (t1))
752 return t2;
754 /* Merge the attributes. */
755 attributes = (*targetm.merge_type_attributes) (t1, t2);
757 if (TYPE_PTRMEMFUNC_P (t1))
758 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
759 if (TYPE_PTRMEMFUNC_P (t2))
760 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
762 code1 = TREE_CODE (t1);
763 code2 = TREE_CODE (t2);
764 if (code1 != code2)
766 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
767 if (code1 == TYPENAME_TYPE)
769 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
770 code1 = TREE_CODE (t1);
772 else
774 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
775 code2 = TREE_CODE (t2);
779 switch (code1)
781 case POINTER_TYPE:
782 case REFERENCE_TYPE:
783 /* For two pointers, do this recursively on the target type. */
785 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
786 int quals = cp_type_quals (t1);
788 if (code1 == POINTER_TYPE)
789 t1 = build_pointer_type (target);
790 else
791 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
792 t1 = build_type_attribute_variant (t1, attributes);
793 t1 = cp_build_qualified_type (t1, quals);
795 if (TREE_CODE (target) == METHOD_TYPE)
796 t1 = build_ptrmemfunc_type (t1);
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;
1220 if (AGGREGATE_TYPE_P (t1)
1221 && TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
1222 return false;
1224 /* Allow for two different type nodes which have essentially the same
1225 definition. Note that we already checked for equality of the type
1226 qualifiers (just above). */
1228 if (TREE_CODE (t1) != ARRAY_TYPE
1229 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1230 return true;
1233 /* Compare the types. Break out if they could be the same. */
1234 switch (TREE_CODE (t1))
1236 case VOID_TYPE:
1237 case BOOLEAN_TYPE:
1238 /* All void and bool types are the same. */
1239 break;
1241 case INTEGER_TYPE:
1242 case FIXED_POINT_TYPE:
1243 case REAL_TYPE:
1244 /* With these nodes, we can't determine type equivalence by
1245 looking at what is stored in the nodes themselves, because
1246 two nodes might have different TYPE_MAIN_VARIANTs but still
1247 represent the same type. For example, wchar_t and int could
1248 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1249 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1250 and are distinct types. On the other hand, int and the
1251 following typedef
1253 typedef int INT __attribute((may_alias));
1255 have identical properties, different TYPE_MAIN_VARIANTs, but
1256 represent the same type. The canonical type system keeps
1257 track of equivalence in this case, so we fall back on it. */
1258 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1260 case TEMPLATE_TEMPLATE_PARM:
1261 case BOUND_TEMPLATE_TEMPLATE_PARM:
1262 if (!comp_template_parms_position (t1, t2))
1263 return false;
1264 if (!comp_template_parms
1265 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1266 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1267 return false;
1268 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1269 break;
1270 /* Don't check inheritance. */
1271 strict = COMPARE_STRICT;
1272 /* Fall through. */
1274 case RECORD_TYPE:
1275 case UNION_TYPE:
1276 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1277 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1278 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1279 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1280 break;
1282 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1283 break;
1284 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1285 break;
1287 return false;
1289 case OFFSET_TYPE:
1290 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1291 strict & ~COMPARE_REDECLARATION))
1292 return false;
1293 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1294 return false;
1295 break;
1297 case REFERENCE_TYPE:
1298 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1299 return false;
1300 /* fall through to checks for pointer types */
1302 case POINTER_TYPE:
1303 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1304 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1305 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1306 return false;
1307 break;
1309 case METHOD_TYPE:
1310 case FUNCTION_TYPE:
1311 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1312 return false;
1313 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1314 return false;
1315 break;
1317 case ARRAY_TYPE:
1318 /* Target types must match incl. qualifiers. */
1319 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1320 return false;
1321 break;
1323 case TEMPLATE_TYPE_PARM:
1324 /* If T1 and T2 don't have the same relative position in their
1325 template parameters set, they can't be equal. */
1326 if (!comp_template_parms_position (t1, t2))
1327 return false;
1328 break;
1330 case TYPENAME_TYPE:
1331 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1332 TYPENAME_TYPE_FULLNAME (t2)))
1333 return false;
1334 /* Qualifiers don't matter on scopes. */
1335 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1336 TYPE_CONTEXT (t2)))
1337 return false;
1338 break;
1340 case UNBOUND_CLASS_TEMPLATE:
1341 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1342 return false;
1343 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1344 return false;
1345 break;
1347 case COMPLEX_TYPE:
1348 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1349 return false;
1350 break;
1352 case VECTOR_TYPE:
1353 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1354 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1355 return false;
1356 break;
1358 case TYPE_PACK_EXPANSION:
1359 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1360 PACK_EXPANSION_PATTERN (t2))
1361 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1362 PACK_EXPANSION_EXTRA_ARGS (t2)));
1364 case DECLTYPE_TYPE:
1365 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1366 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1367 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1368 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1369 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1370 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1371 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1372 DECLTYPE_TYPE_EXPR (t2)))
1373 return false;
1374 break;
1376 case UNDERLYING_TYPE:
1377 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1378 UNDERLYING_TYPE_TYPE (t2));
1380 default:
1381 return false;
1384 /* If we get here, we know that from a target independent POV the
1385 types are the same. Make sure the target attributes are also
1386 the same. */
1387 return comp_type_attributes (t1, t2);
1390 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1391 is a bitwise-or of the COMPARE_* flags. */
1393 bool
1394 comptypes (tree t1, tree t2, int strict)
1396 if (strict == COMPARE_STRICT)
1398 if (t1 == t2)
1399 return true;
1401 if (t1 == error_mark_node || t2 == error_mark_node)
1402 return false;
1404 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1405 /* At least one of the types requires structural equality, so
1406 perform a deep check. */
1407 return structural_comptypes (t1, t2, strict);
1409 #ifdef ENABLE_CHECKING
1410 if (USE_CANONICAL_TYPES)
1412 bool result = structural_comptypes (t1, t2, strict);
1414 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1415 /* The two types are structurally equivalent, but their
1416 canonical types were different. This is a failure of the
1417 canonical type propagation code.*/
1418 internal_error
1419 ("canonical types differ for identical types %T and %T",
1420 t1, t2);
1421 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1422 /* Two types are structurally different, but the canonical
1423 types are the same. This means we were over-eager in
1424 assigning canonical types. */
1425 internal_error
1426 ("same canonical type node for different types %T and %T",
1427 t1, t2);
1429 return result;
1431 #else
1432 if (USE_CANONICAL_TYPES)
1433 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1434 #endif
1435 else
1436 return structural_comptypes (t1, t2, strict);
1438 else if (strict == COMPARE_STRUCTURAL)
1439 return structural_comptypes (t1, t2, COMPARE_STRICT);
1440 else
1441 return structural_comptypes (t1, t2, strict);
1444 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1445 top-level qualifiers. */
1447 bool
1448 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1450 if (type1 == error_mark_node || type2 == error_mark_node)
1451 return false;
1453 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1456 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1458 bool
1459 at_least_as_qualified_p (const_tree type1, const_tree type2)
1461 int q1 = cp_type_quals (type1);
1462 int q2 = cp_type_quals (type2);
1464 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1465 return (q1 & q2) == q2;
1468 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1469 more cv-qualified that TYPE1, and 0 otherwise. */
1472 comp_cv_qualification (int q1, int q2)
1474 if (q1 == q2)
1475 return 0;
1477 if ((q1 & q2) == q2)
1478 return 1;
1479 else if ((q1 & q2) == q1)
1480 return -1;
1482 return 0;
1486 comp_cv_qualification (const_tree type1, const_tree type2)
1488 int q1 = cp_type_quals (type1);
1489 int q2 = cp_type_quals (type2);
1490 return comp_cv_qualification (q1, q2);
1493 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1494 subset of the cv-qualification signature of TYPE2, and the types
1495 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1498 comp_cv_qual_signature (tree type1, tree type2)
1500 if (comp_ptr_ttypes_real (type2, type1, -1))
1501 return 1;
1502 else if (comp_ptr_ttypes_real (type1, type2, -1))
1503 return -1;
1504 else
1505 return 0;
1508 /* Subroutines of `comptypes'. */
1510 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1511 equivalent in the sense that functions with those parameter types
1512 can have equivalent types. The two lists must be equivalent,
1513 element by element. */
1515 bool
1516 compparms (const_tree parms1, const_tree parms2)
1518 const_tree t1, t2;
1520 /* An unspecified parmlist matches any specified parmlist
1521 whose argument types don't need default promotions. */
1523 for (t1 = parms1, t2 = parms2;
1524 t1 || t2;
1525 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1527 /* If one parmlist is shorter than the other,
1528 they fail to match. */
1529 if (!t1 || !t2)
1530 return false;
1531 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1532 return false;
1534 return true;
1538 /* Process a sizeof or alignof expression where the operand is a
1539 type. */
1541 tree
1542 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1544 tree value;
1545 bool dependent_p;
1547 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1548 if (type == error_mark_node)
1549 return error_mark_node;
1551 type = non_reference (type);
1552 if (TREE_CODE (type) == METHOD_TYPE)
1554 if (complain)
1555 pedwarn (input_location, OPT_Wpointer_arith,
1556 "invalid application of %qs to a member function",
1557 operator_name_info[(int) op].name);
1558 else
1559 return error_mark_node;
1560 value = size_one_node;
1563 dependent_p = dependent_type_p (type);
1564 if (!dependent_p)
1565 complete_type (type);
1566 if (dependent_p
1567 /* VLA types will have a non-constant size. In the body of an
1568 uninstantiated template, we don't need to try to compute the
1569 value, because the sizeof expression is not an integral
1570 constant expression in that case. And, if we do try to
1571 compute the value, we'll likely end up with SAVE_EXPRs, which
1572 the template substitution machinery does not expect to see. */
1573 || (processing_template_decl
1574 && COMPLETE_TYPE_P (type)
1575 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1577 value = build_min (op, size_type_node, type);
1578 TREE_READONLY (value) = 1;
1579 return value;
1582 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (type)
1583 && (flag_iso || warn_vla > 0))
1585 if (complain)
1586 pedwarn (input_location, OPT_Wvla,
1587 "taking sizeof array of runtime bound");
1588 else
1589 return error_mark_node;
1592 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1593 op == SIZEOF_EXPR, false,
1594 complain);
1597 /* Return the size of the type, without producing any warnings for
1598 types whose size cannot be taken. This routine should be used only
1599 in some other routine that has already produced a diagnostic about
1600 using the size of such a type. */
1601 tree
1602 cxx_sizeof_nowarn (tree type)
1604 if (TREE_CODE (type) == FUNCTION_TYPE
1605 || VOID_TYPE_P (type)
1606 || TREE_CODE (type) == ERROR_MARK)
1607 return size_one_node;
1608 else if (!COMPLETE_TYPE_P (type))
1609 return size_zero_node;
1610 else
1611 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1614 /* Process a sizeof expression where the operand is an expression. */
1616 static tree
1617 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1619 if (e == error_mark_node)
1620 return error_mark_node;
1622 if (processing_template_decl)
1624 e = build_min (SIZEOF_EXPR, size_type_node, e);
1625 TREE_SIDE_EFFECTS (e) = 0;
1626 TREE_READONLY (e) = 1;
1628 return e;
1631 /* To get the size of a static data member declared as an array of
1632 unknown bound, we need to instantiate it. */
1633 if (VAR_P (e)
1634 && VAR_HAD_UNKNOWN_BOUND (e)
1635 && DECL_TEMPLATE_INSTANTIATION (e))
1636 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1638 if (TREE_CODE (e) == PARM_DECL
1639 && DECL_ARRAY_PARAMETER_P (e)
1640 && (complain & tf_warning))
1642 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1643 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1644 inform (DECL_SOURCE_LOCATION (e), "declared here");
1647 e = mark_type_use (e);
1649 if (TREE_CODE (e) == COMPONENT_REF
1650 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1651 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1653 if (complain & tf_error)
1654 error ("invalid application of %<sizeof%> to a bit-field");
1655 else
1656 return error_mark_node;
1657 e = char_type_node;
1659 else if (is_overloaded_fn (e))
1661 if (complain & tf_error)
1662 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1663 "function type");
1664 else
1665 return error_mark_node;
1666 e = char_type_node;
1668 else if (type_unknown_p (e))
1670 if (complain & tf_error)
1671 cxx_incomplete_type_error (e, TREE_TYPE (e));
1672 else
1673 return error_mark_node;
1674 e = char_type_node;
1676 else
1677 e = TREE_TYPE (e);
1679 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1682 /* Implement the __alignof keyword: Return the minimum required
1683 alignment of E, measured in bytes. For VAR_DECL's and
1684 FIELD_DECL's return DECL_ALIGN (which can be set from an
1685 "aligned" __attribute__ specification). */
1687 static tree
1688 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1690 tree t;
1692 if (e == error_mark_node)
1693 return error_mark_node;
1695 if (processing_template_decl)
1697 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1698 TREE_SIDE_EFFECTS (e) = 0;
1699 TREE_READONLY (e) = 1;
1701 return e;
1704 e = mark_type_use (e);
1706 if (VAR_P (e))
1707 t = size_int (DECL_ALIGN_UNIT (e));
1708 else if (TREE_CODE (e) == COMPONENT_REF
1709 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1710 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1712 if (complain & tf_error)
1713 error ("invalid application of %<__alignof%> to a bit-field");
1714 else
1715 return error_mark_node;
1716 t = size_one_node;
1718 else if (TREE_CODE (e) == COMPONENT_REF
1719 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1720 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1721 else if (is_overloaded_fn (e))
1723 if (complain & tf_error)
1724 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1725 "function type");
1726 else
1727 return error_mark_node;
1728 if (TREE_CODE (e) == FUNCTION_DECL)
1729 t = size_int (DECL_ALIGN_UNIT (e));
1730 else
1731 t = size_one_node;
1733 else if (type_unknown_p (e))
1735 if (complain & tf_error)
1736 cxx_incomplete_type_error (e, TREE_TYPE (e));
1737 else
1738 return error_mark_node;
1739 t = size_one_node;
1741 else
1742 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1743 complain & tf_error);
1745 return fold_convert (size_type_node, t);
1748 /* Process a sizeof or alignof expression E with code OP where the operand
1749 is an expression. */
1751 tree
1752 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1754 if (op == SIZEOF_EXPR)
1755 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1756 else
1757 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1760 /* Build a representation of an expression 'alignas(E).' Return the
1761 folded integer value of E if it is an integral constant expression
1762 that resolves to a valid alignment. If E depends on a template
1763 parameter, return a syntactic representation tree of kind
1764 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1765 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1767 tree
1768 cxx_alignas_expr (tree e)
1770 if (e == NULL_TREE || e == error_mark_node
1771 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1772 return e;
1774 if (TYPE_P (e))
1775 /* [dcl.align]/3:
1777 When the alignment-specifier is of the form
1778 alignas(type-id ), it shall have the same effect as
1779 alignas(alignof(type-id )). */
1781 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1783 /* If we reach this point, it means the alignas expression if of
1784 the form "alignas(assignment-expression)", so we should follow
1785 what is stated by [dcl.align]/2. */
1787 if (value_dependent_expression_p (e))
1788 /* Leave value-dependent expression alone for now. */
1789 return e;
1791 e = fold_non_dependent_expr (e);
1792 e = mark_rvalue_use (e);
1794 /* [dcl.align]/2 says:
1796 the assignment-expression shall be an integral constant
1797 expression. */
1799 return cxx_constant_value (e);
1803 /* EXPR is being used in a context that is not a function call.
1804 Enforce:
1806 [expr.ref]
1808 The expression can be used only as the left-hand operand of a
1809 member function call.
1811 [expr.mptr.operator]
1813 If the result of .* or ->* is a function, then that result can be
1814 used only as the operand for the function call operator ().
1816 by issuing an error message if appropriate. Returns true iff EXPR
1817 violates these rules. */
1819 bool
1820 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1822 if (expr == NULL_TREE)
1823 return false;
1824 /* Don't enforce this in MS mode. */
1825 if (flag_ms_extensions)
1826 return false;
1827 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1828 expr = get_first_fn (expr);
1829 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1831 if (complain & tf_error)
1832 error ("invalid use of non-static member function");
1833 return true;
1835 return false;
1838 /* If EXP is a reference to a bitfield, and the type of EXP does not
1839 match the declared type of the bitfield, return the declared type
1840 of the bitfield. Otherwise, return NULL_TREE. */
1842 tree
1843 is_bitfield_expr_with_lowered_type (const_tree exp)
1845 switch (TREE_CODE (exp))
1847 case COND_EXPR:
1848 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1849 ? TREE_OPERAND (exp, 1)
1850 : TREE_OPERAND (exp, 0)))
1851 return NULL_TREE;
1852 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1854 case COMPOUND_EXPR:
1855 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1857 case MODIFY_EXPR:
1858 case SAVE_EXPR:
1859 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1861 case COMPONENT_REF:
1863 tree field;
1865 field = TREE_OPERAND (exp, 1);
1866 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1867 return NULL_TREE;
1868 if (same_type_ignoring_top_level_qualifiers_p
1869 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1870 return NULL_TREE;
1871 return DECL_BIT_FIELD_TYPE (field);
1874 CASE_CONVERT:
1875 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1876 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1877 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1878 /* Fallthrough. */
1880 default:
1881 return NULL_TREE;
1885 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1886 bitfield with a lowered type, the type of EXP is returned, rather
1887 than NULL_TREE. */
1889 tree
1890 unlowered_expr_type (const_tree exp)
1892 tree type;
1893 tree etype = TREE_TYPE (exp);
1895 type = is_bitfield_expr_with_lowered_type (exp);
1896 if (type)
1897 type = cp_build_qualified_type (type, cp_type_quals (etype));
1898 else
1899 type = etype;
1901 return type;
1904 /* Perform the conversions in [expr] that apply when an lvalue appears
1905 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1906 function-to-pointer conversions. In addition, manifest constants
1907 are replaced by their values, and bitfield references are converted
1908 to their declared types. Note that this function does not perform the
1909 lvalue-to-rvalue conversion for class types. If you need that conversion
1910 to for class types, then you probably need to use force_rvalue.
1912 Although the returned value is being used as an rvalue, this
1913 function does not wrap the returned expression in a
1914 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1915 that the return value is no longer an lvalue. */
1917 tree
1918 decay_conversion (tree exp, tsubst_flags_t complain)
1920 tree type;
1921 enum tree_code code;
1922 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1924 type = TREE_TYPE (exp);
1925 if (type == error_mark_node)
1926 return error_mark_node;
1928 exp = mark_rvalue_use (exp);
1930 exp = resolve_nondeduced_context (exp);
1931 if (type_unknown_p (exp))
1933 if (complain & tf_error)
1934 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1935 return error_mark_node;
1938 code = TREE_CODE (type);
1940 /* For an array decl decay_conversion should not try to return its
1941 initializer. */
1942 if (code != ARRAY_TYPE)
1944 /* FIXME remove? at least need to remember that this isn't really a
1945 constant expression if EXP isn't decl_constant_var_p, like with
1946 C_MAYBE_CONST_EXPR. */
1947 exp = decl_constant_value_safe (exp);
1948 if (error_operand_p (exp))
1949 return error_mark_node;
1952 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1953 return nullptr_node;
1955 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1956 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1957 if (code == VOID_TYPE)
1959 if (complain & tf_error)
1960 error_at (loc, "void value not ignored as it ought to be");
1961 return error_mark_node;
1963 if (invalid_nonstatic_memfn_p (exp, complain))
1964 return error_mark_node;
1965 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1966 return cp_build_addr_expr (exp, complain);
1967 if (code == ARRAY_TYPE)
1969 tree adr;
1970 tree ptrtype;
1972 if (INDIRECT_REF_P (exp))
1973 return build_nop (build_pointer_type (TREE_TYPE (type)),
1974 TREE_OPERAND (exp, 0));
1976 if (TREE_CODE (exp) == COMPOUND_EXPR)
1978 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1979 if (op1 == error_mark_node)
1980 return error_mark_node;
1981 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1982 TREE_OPERAND (exp, 0), op1);
1985 if (!lvalue_p (exp)
1986 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1988 if (complain & tf_error)
1989 error_at (loc, "invalid use of non-lvalue array");
1990 return error_mark_node;
1993 /* Don't let an array compound literal decay to a pointer. It can
1994 still be used to initialize an array or bind to a reference. */
1995 if (TREE_CODE (exp) == TARGET_EXPR)
1997 if (complain & tf_error)
1998 error_at (loc, "taking address of temporary array");
1999 return error_mark_node;
2002 ptrtype = build_pointer_type (TREE_TYPE (type));
2004 if (VAR_P (exp))
2006 if (!cxx_mark_addressable (exp))
2007 return error_mark_node;
2008 adr = build_nop (ptrtype, build_address (exp));
2009 return adr;
2011 /* This way is better for a COMPONENT_REF since it can
2012 simplify the offset for a component. */
2013 adr = cp_build_addr_expr (exp, complain);
2014 return cp_convert (ptrtype, adr, complain);
2017 /* If a bitfield is used in a context where integral promotion
2018 applies, then the caller is expected to have used
2019 default_conversion. That function promotes bitfields correctly
2020 before calling this function. At this point, if we have a
2021 bitfield referenced, we may assume that is not subject to
2022 promotion, and that, therefore, the type of the resulting rvalue
2023 is the declared type of the bitfield. */
2024 exp = convert_bitfield_to_declared_type (exp);
2026 /* We do not call rvalue() here because we do not want to wrap EXP
2027 in a NON_LVALUE_EXPR. */
2029 /* [basic.lval]
2031 Non-class rvalues always have cv-unqualified types. */
2032 type = TREE_TYPE (exp);
2033 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2034 exp = build_nop (cv_unqualified (type), exp);
2036 return exp;
2039 /* Perform preparatory conversions, as part of the "usual arithmetic
2040 conversions". In particular, as per [expr]:
2042 Whenever an lvalue expression appears as an operand of an
2043 operator that expects the rvalue for that operand, the
2044 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2045 standard conversions are applied to convert the expression to an
2046 rvalue.
2048 In addition, we perform integral promotions here, as those are
2049 applied to both operands to a binary operator before determining
2050 what additional conversions should apply. */
2052 static tree
2053 cp_default_conversion (tree exp, tsubst_flags_t complain)
2055 /* Check for target-specific promotions. */
2056 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2057 if (promoted_type)
2058 exp = cp_convert (promoted_type, exp, complain);
2059 /* Perform the integral promotions first so that bitfield
2060 expressions (which may promote to "int", even if the bitfield is
2061 declared "unsigned") are promoted correctly. */
2062 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2063 exp = cp_perform_integral_promotions (exp, complain);
2064 /* Perform the other conversions. */
2065 exp = decay_conversion (exp, complain);
2067 return exp;
2070 /* C version. */
2072 tree
2073 default_conversion (tree exp)
2075 return cp_default_conversion (exp, tf_warning_or_error);
2078 /* EXPR is an expression with an integral or enumeration type.
2079 Perform the integral promotions in [conv.prom], and return the
2080 converted value. */
2082 tree
2083 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2085 tree type;
2086 tree promoted_type;
2088 expr = mark_rvalue_use (expr);
2090 /* [conv.prom]
2092 If the bitfield has an enumerated type, it is treated as any
2093 other value of that type for promotion purposes. */
2094 type = is_bitfield_expr_with_lowered_type (expr);
2095 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2096 type = TREE_TYPE (expr);
2097 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2098 /* Scoped enums don't promote. */
2099 if (SCOPED_ENUM_P (type))
2100 return expr;
2101 promoted_type = type_promotes_to (type);
2102 if (type != promoted_type)
2103 expr = cp_convert (promoted_type, expr, complain);
2104 return expr;
2107 /* C version. */
2109 tree
2110 perform_integral_promotions (tree expr)
2112 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2115 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2116 decay_conversion to one. */
2119 string_conv_p (const_tree totype, const_tree exp, int warn)
2121 tree t;
2123 if (!TYPE_PTR_P (totype))
2124 return 0;
2126 t = TREE_TYPE (totype);
2127 if (!same_type_p (t, char_type_node)
2128 && !same_type_p (t, char16_type_node)
2129 && !same_type_p (t, char32_type_node)
2130 && !same_type_p (t, wchar_type_node))
2131 return 0;
2133 if (TREE_CODE (exp) == STRING_CST)
2135 /* Make sure that we don't try to convert between char and wide chars. */
2136 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2137 return 0;
2139 else
2141 /* Is this a string constant which has decayed to 'const char *'? */
2142 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2143 if (!same_type_p (TREE_TYPE (exp), t))
2144 return 0;
2145 STRIP_NOPS (exp);
2146 if (TREE_CODE (exp) != ADDR_EXPR
2147 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2148 return 0;
2151 /* This warning is not very useful, as it complains about printf. */
2152 if (warn)
2153 warning (OPT_Wwrite_strings,
2154 "deprecated conversion from string constant to %qT",
2155 totype);
2157 return 1;
2160 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2161 can, for example, use as an lvalue. This code used to be in
2162 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2163 expressions, where we're dealing with aggregates. But now it's again only
2164 called from unary_complex_lvalue. The case (in particular) that led to
2165 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2166 get it there. */
2168 static tree
2169 rationalize_conditional_expr (enum tree_code code, tree t,
2170 tsubst_flags_t complain)
2172 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2174 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2175 the first operand is always the one to be used if both operands
2176 are equal, so we know what conditional expression this used to be. */
2177 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2179 tree op0 = TREE_OPERAND (t, 0);
2180 tree op1 = TREE_OPERAND (t, 1);
2182 /* The following code is incorrect if either operand side-effects. */
2183 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2184 && !TREE_SIDE_EFFECTS (op1));
2185 return
2186 build_conditional_expr (loc,
2187 build_x_binary_op (loc,
2188 (TREE_CODE (t) == MIN_EXPR
2189 ? LE_EXPR : GE_EXPR),
2190 op0, TREE_CODE (op0),
2191 op1, TREE_CODE (op1),
2192 /*overload=*/NULL,
2193 complain),
2194 cp_build_unary_op (code, op0, 0, complain),
2195 cp_build_unary_op (code, op1, 0, complain),
2196 complain);
2199 return
2200 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2201 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2202 complain),
2203 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2204 complain),
2205 complain);
2208 /* Given the TYPE of an anonymous union field inside T, return the
2209 FIELD_DECL for the field. If not found return NULL_TREE. Because
2210 anonymous unions can nest, we must also search all anonymous unions
2211 that are directly reachable. */
2213 tree
2214 lookup_anon_field (tree t, tree type)
2216 tree field;
2218 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2220 if (TREE_STATIC (field))
2221 continue;
2222 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2223 continue;
2225 /* If we find it directly, return the field. */
2226 if (DECL_NAME (field) == NULL_TREE
2227 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2229 return field;
2232 /* Otherwise, it could be nested, search harder. */
2233 if (DECL_NAME (field) == NULL_TREE
2234 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2236 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2237 if (subfield)
2238 return subfield;
2241 return NULL_TREE;
2244 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2245 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2246 non-NULL, it indicates the path to the base used to name MEMBER.
2247 If PRESERVE_REFERENCE is true, the expression returned will have
2248 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2249 returned will have the type referred to by the reference.
2251 This function does not perform access control; that is either done
2252 earlier by the parser when the name of MEMBER is resolved to MEMBER
2253 itself, or later when overload resolution selects one of the
2254 functions indicated by MEMBER. */
2256 tree
2257 build_class_member_access_expr (tree object, tree member,
2258 tree access_path, bool preserve_reference,
2259 tsubst_flags_t complain)
2261 tree object_type;
2262 tree member_scope;
2263 tree result = NULL_TREE;
2264 tree using_decl = NULL_TREE;
2266 if (error_operand_p (object) || error_operand_p (member))
2267 return error_mark_node;
2269 gcc_assert (DECL_P (member) || BASELINK_P (member));
2271 /* [expr.ref]
2273 The type of the first expression shall be "class object" (of a
2274 complete type). */
2275 object_type = TREE_TYPE (object);
2276 if (!currently_open_class (object_type)
2277 && !complete_type_or_maybe_complain (object_type, object, complain))
2278 return error_mark_node;
2279 if (!CLASS_TYPE_P (object_type))
2281 if (complain & tf_error)
2283 if (POINTER_TYPE_P (object_type)
2284 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2285 error ("request for member %qD in %qE, which is of pointer "
2286 "type %qT (maybe you meant to use %<->%> ?)",
2287 member, object, object_type);
2288 else
2289 error ("request for member %qD in %qE, which is of non-class "
2290 "type %qT", member, object, object_type);
2292 return error_mark_node;
2295 /* The standard does not seem to actually say that MEMBER must be a
2296 member of OBJECT_TYPE. However, that is clearly what is
2297 intended. */
2298 if (DECL_P (member))
2300 member_scope = DECL_CLASS_CONTEXT (member);
2301 mark_used (member);
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 (COMPONENT_REF, member_type, object, member,
2431 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 expr = (adjust_result_of_qualified_name_lookup
2542 (expr, dtor_type, object_type));
2543 if (scope == NULL_TREE)
2544 /* We need to call adjust_result_of_qualified_name_lookup in case the
2545 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2546 that we still get virtual function binding. */
2547 BASELINK_QUALIFIED_P (expr) = false;
2548 return expr;
2551 /* An expression of the form "A::template B" has been resolved to
2552 DECL. Issue a diagnostic if B is not a template or template
2553 specialization. */
2555 void
2556 check_template_keyword (tree decl)
2558 /* The standard says:
2560 [temp.names]
2562 If a name prefixed by the keyword template is not a member
2563 template, the program is ill-formed.
2565 DR 228 removed the restriction that the template be a member
2566 template.
2568 DR 96, if accepted would add the further restriction that explicit
2569 template arguments must be provided if the template keyword is
2570 used, but, as of 2005-10-16, that DR is still in "drafting". If
2571 this DR is accepted, then the semantic checks here can be
2572 simplified, as the entity named must in fact be a template
2573 specialization, rather than, as at present, a set of overloaded
2574 functions containing at least one template function. */
2575 if (TREE_CODE (decl) != TEMPLATE_DECL
2576 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2578 if (!is_overloaded_fn (decl))
2579 permerror (input_location, "%qD is not a template", decl);
2580 else
2582 tree fns;
2583 fns = decl;
2584 if (BASELINK_P (fns))
2585 fns = BASELINK_FUNCTIONS (fns);
2586 while (fns)
2588 tree fn = OVL_CURRENT (fns);
2589 if (TREE_CODE (fn) == TEMPLATE_DECL
2590 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2591 break;
2592 if (TREE_CODE (fn) == FUNCTION_DECL
2593 && DECL_USE_TEMPLATE (fn)
2594 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2595 break;
2596 fns = OVL_NEXT (fns);
2598 if (!fns)
2599 permerror (input_location, "%qD is not a template", decl);
2604 /* This function is called by the parser to process a class member
2605 access expression of the form OBJECT.NAME. NAME is a node used by
2606 the parser to represent a name; it is not yet a DECL. It may,
2607 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2608 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2609 there is no reason to do the lookup twice, so the parser keeps the
2610 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2611 be a template via the use of the "A::template B" syntax. */
2613 tree
2614 finish_class_member_access_expr (tree object, tree name, bool template_p,
2615 tsubst_flags_t complain)
2617 tree expr;
2618 tree object_type;
2619 tree member;
2620 tree access_path = NULL_TREE;
2621 tree orig_object = object;
2622 tree orig_name = name;
2624 if (object == error_mark_node || name == error_mark_node)
2625 return error_mark_node;
2627 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2628 if (!objc_is_public (object, name))
2629 return error_mark_node;
2631 object_type = TREE_TYPE (object);
2633 if (processing_template_decl)
2635 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2636 dependent_type_p (object_type)
2637 /* If NAME is just an IDENTIFIER_NODE, then the expression
2638 is dependent. */
2639 || identifier_p (object)
2640 /* If NAME is "f<args>", where either 'f' or 'args' is
2641 dependent, then the expression is dependent. */
2642 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2643 && dependent_template_id_p (TREE_OPERAND (name, 0),
2644 TREE_OPERAND (name, 1)))
2645 /* If NAME is "T::X" where "T" is dependent, then the
2646 expression is dependent. */
2647 || (TREE_CODE (name) == SCOPE_REF
2648 && TYPE_P (TREE_OPERAND (name, 0))
2649 && dependent_type_p (TREE_OPERAND (name, 0))))
2650 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2651 object, name, NULL_TREE);
2652 object = build_non_dependent_expr (object);
2654 else if (c_dialect_objc ()
2655 && identifier_p (name)
2656 && (expr = objc_maybe_build_component_ref (object, name)))
2657 return expr;
2659 /* [expr.ref]
2661 The type of the first expression shall be "class object" (of a
2662 complete type). */
2663 if (!currently_open_class (object_type)
2664 && !complete_type_or_maybe_complain (object_type, object, complain))
2665 return error_mark_node;
2666 if (!CLASS_TYPE_P (object_type))
2668 if (complain & tf_error)
2670 if (POINTER_TYPE_P (object_type)
2671 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2672 error ("request for member %qD in %qE, which is of pointer "
2673 "type %qT (maybe you meant to use %<->%> ?)",
2674 name, object, object_type);
2675 else
2676 error ("request for member %qD in %qE, which is of non-class "
2677 "type %qT", name, object, object_type);
2679 return error_mark_node;
2682 if (BASELINK_P (name))
2683 /* A member function that has already been looked up. */
2684 member = name;
2685 else
2687 bool is_template_id = false;
2688 tree template_args = NULL_TREE;
2689 tree scope;
2691 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2693 is_template_id = true;
2694 template_args = TREE_OPERAND (name, 1);
2695 name = TREE_OPERAND (name, 0);
2697 if (TREE_CODE (name) == OVERLOAD)
2698 name = DECL_NAME (get_first_fn (name));
2699 else if (DECL_P (name))
2700 name = DECL_NAME (name);
2703 if (TREE_CODE (name) == SCOPE_REF)
2705 /* A qualified name. The qualifying class or namespace `S'
2706 has already been looked up; it is either a TYPE or a
2707 NAMESPACE_DECL. */
2708 scope = TREE_OPERAND (name, 0);
2709 name = TREE_OPERAND (name, 1);
2711 /* If SCOPE is a namespace, then the qualified name does not
2712 name a member of OBJECT_TYPE. */
2713 if (TREE_CODE (scope) == NAMESPACE_DECL)
2715 if (complain & tf_error)
2716 error ("%<%D::%D%> is not a member of %qT",
2717 scope, name, object_type);
2718 return error_mark_node;
2721 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2723 /* Looking up a member enumerator (c++/56793). */
2724 if (!TYPE_CLASS_SCOPE_P (scope)
2725 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2727 if (complain & tf_error)
2728 error ("%<%D::%D%> is not a member of %qT",
2729 scope, name, object_type);
2730 return error_mark_node;
2732 tree val = lookup_enumerator (scope, name);
2733 if (TREE_SIDE_EFFECTS (object))
2734 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2735 return val;
2738 gcc_assert (CLASS_TYPE_P (scope));
2739 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2741 if (constructor_name_p (name, scope))
2743 if (complain & tf_error)
2744 error ("cannot call constructor %<%T::%D%> directly",
2745 scope, name);
2746 return error_mark_node;
2749 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2750 access_path = lookup_base (object_type, scope, ba_check,
2751 NULL, complain);
2752 if (access_path == error_mark_node)
2753 return error_mark_node;
2754 if (!access_path)
2756 if (complain & tf_error)
2757 error ("%qT is not a base of %qT", scope, object_type);
2758 return error_mark_node;
2761 else
2763 scope = NULL_TREE;
2764 access_path = object_type;
2767 if (TREE_CODE (name) == BIT_NOT_EXPR)
2768 member = lookup_destructor (object, scope, name, complain);
2769 else
2771 /* Look up the member. */
2772 member = lookup_member (access_path, name, /*protect=*/1,
2773 /*want_type=*/false, complain);
2774 if (member == NULL_TREE)
2776 if (complain & tf_error)
2777 error ("%qD has no member named %qE",
2778 TREE_CODE (access_path) == TREE_BINFO
2779 ? TREE_TYPE (access_path) : object_type, name);
2780 return error_mark_node;
2782 if (member == error_mark_node)
2783 return error_mark_node;
2786 if (is_template_id)
2788 tree templ = member;
2790 if (BASELINK_P (templ))
2791 templ = lookup_template_function (templ, template_args);
2792 else
2794 if (complain & tf_error)
2795 error ("%qD is not a member template function", name);
2796 return error_mark_node;
2801 if (TREE_DEPRECATED (member))
2802 warn_deprecated_use (member, NULL_TREE);
2804 if (template_p)
2805 check_template_keyword (member);
2807 expr = build_class_member_access_expr (object, member, access_path,
2808 /*preserve_reference=*/false,
2809 complain);
2810 if (processing_template_decl && expr != error_mark_node)
2812 if (BASELINK_P (member))
2814 if (TREE_CODE (orig_name) == SCOPE_REF)
2815 BASELINK_QUALIFIED_P (member) = 1;
2816 orig_name = member;
2818 return build_min_non_dep (COMPONENT_REF, expr,
2819 orig_object, orig_name,
2820 NULL_TREE);
2823 return expr;
2826 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2827 type. */
2829 tree
2830 build_simple_component_ref (tree object, tree member)
2832 tree type = cp_build_qualified_type (TREE_TYPE (member),
2833 cp_type_quals (TREE_TYPE (object)));
2834 return fold_build3_loc (input_location,
2835 COMPONENT_REF, type,
2836 object, member, NULL_TREE);
2839 /* Return an expression for the MEMBER_NAME field in the internal
2840 representation of PTRMEM, a pointer-to-member function. (Each
2841 pointer-to-member function type gets its own RECORD_TYPE so it is
2842 more convenient to access the fields by name than by FIELD_DECL.)
2843 This routine converts the NAME to a FIELD_DECL and then creates the
2844 node for the complete expression. */
2846 tree
2847 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2849 tree ptrmem_type;
2850 tree member;
2852 /* This code is a stripped down version of
2853 build_class_member_access_expr. It does not work to use that
2854 routine directly because it expects the object to be of class
2855 type. */
2856 ptrmem_type = TREE_TYPE (ptrmem);
2857 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2858 for (member = TYPE_FIELDS (ptrmem_type); member;
2859 member = DECL_CHAIN (member))
2860 if (DECL_NAME (member) == member_name)
2861 break;
2862 return build_simple_component_ref (ptrmem, member);
2865 /* Given an expression PTR for a pointer, return an expression
2866 for the value pointed to.
2867 ERRORSTRING is the name of the operator to appear in error messages.
2869 This function may need to overload OPERATOR_FNNAME.
2870 Must also handle REFERENCE_TYPEs for C++. */
2872 tree
2873 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2874 tsubst_flags_t complain)
2876 tree orig_expr = expr;
2877 tree rval;
2879 if (processing_template_decl)
2881 /* Retain the type if we know the operand is a pointer. */
2882 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2883 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2884 if (type_dependent_expression_p (expr))
2885 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2886 expr = build_non_dependent_expr (expr);
2889 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2890 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2891 if (!rval)
2892 rval = cp_build_indirect_ref (expr, errorstring, complain);
2894 if (processing_template_decl && rval != error_mark_node)
2895 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2896 else
2897 return rval;
2900 /* Helper function called from c-common. */
2901 tree
2902 build_indirect_ref (location_t /*loc*/,
2903 tree ptr, ref_operator errorstring)
2905 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2908 tree
2909 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2910 tsubst_flags_t complain)
2912 tree pointer, type;
2914 if (ptr == current_class_ptr
2915 || (TREE_CODE (ptr) == NOP_EXPR
2916 && TREE_OPERAND (ptr, 0) == current_class_ptr
2917 && (same_type_ignoring_top_level_qualifiers_p
2918 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2919 return current_class_ref;
2921 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2922 ? ptr : decay_conversion (ptr, complain));
2923 if (pointer == error_mark_node)
2924 return error_mark_node;
2926 type = TREE_TYPE (pointer);
2928 if (POINTER_TYPE_P (type))
2930 /* [expr.unary.op]
2932 If the type of the expression is "pointer to T," the type
2933 of the result is "T." */
2934 tree t = TREE_TYPE (type);
2936 if ((CONVERT_EXPR_P (ptr)
2937 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2938 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2940 /* If a warning is issued, mark it to avoid duplicates from
2941 the backend. This only needs to be done at
2942 warn_strict_aliasing > 2. */
2943 if (warn_strict_aliasing > 2)
2944 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2945 type, TREE_OPERAND (ptr, 0)))
2946 TREE_NO_WARNING (ptr) = 1;
2949 if (VOID_TYPE_P (t))
2951 /* A pointer to incomplete type (other than cv void) can be
2952 dereferenced [expr.unary.op]/1 */
2953 if (complain & tf_error)
2954 error ("%qT is not a pointer-to-object type", type);
2955 return error_mark_node;
2957 else if (TREE_CODE (pointer) == ADDR_EXPR
2958 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2959 /* The POINTER was something like `&x'. We simplify `*&x' to
2960 `x'. */
2961 return TREE_OPERAND (pointer, 0);
2962 else
2964 tree ref = build1 (INDIRECT_REF, t, pointer);
2966 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2967 so that we get the proper error message if the result is used
2968 to assign to. Also, &* is supposed to be a no-op. */
2969 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2970 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2971 TREE_SIDE_EFFECTS (ref)
2972 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2973 return ref;
2976 else if (!(complain & tf_error))
2977 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2979 /* `pointer' won't be an error_mark_node if we were given a
2980 pointer to member, so it's cool to check for this here. */
2981 else if (TYPE_PTRMEM_P (type))
2982 switch (errorstring)
2984 case RO_ARRAY_INDEXING:
2985 error ("invalid use of array indexing on pointer to member");
2986 break;
2987 case RO_UNARY_STAR:
2988 error ("invalid use of unary %<*%> on pointer to member");
2989 break;
2990 case RO_IMPLICIT_CONVERSION:
2991 error ("invalid use of implicit conversion on pointer to member");
2992 break;
2993 case RO_ARROW_STAR:
2994 error ("left hand operand of %<->*%> must be a pointer to class, "
2995 "but is a pointer to member of type %qT", type);
2996 break;
2997 default:
2998 gcc_unreachable ();
3000 else if (pointer != error_mark_node)
3001 invalid_indirection_error (input_location, type, errorstring);
3003 return error_mark_node;
3006 /* This handles expressions of the form "a[i]", which denotes
3007 an array reference.
3009 This is logically equivalent in C to *(a+i), but we may do it differently.
3010 If A is a variable or a member, we generate a primitive ARRAY_REF.
3011 This avoids forcing the array out of registers, and can work on
3012 arrays that are not lvalues (for example, members of structures returned
3013 by functions).
3015 If INDEX is of some user-defined type, it must be converted to
3016 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3017 will inherit the type of the array, which will be some pointer type.
3019 LOC is the location to use in building the array reference. */
3021 tree
3022 cp_build_array_ref (location_t loc, tree array, tree idx,
3023 tsubst_flags_t complain)
3025 tree ret;
3027 if (idx == 0)
3029 if (complain & tf_error)
3030 error_at (loc, "subscript missing in array reference");
3031 return error_mark_node;
3034 /* If an array's index is an array notation, then its rank cannot be
3035 greater than one. */
3036 if (flag_cilkplus && contains_array_notation_expr (idx))
3038 size_t rank = 0;
3040 /* If find_rank returns false, then it should have reported an error,
3041 thus it is unnecessary for repetition. */
3042 if (!find_rank (loc, idx, idx, true, &rank))
3043 return error_mark_node;
3044 if (rank > 1)
3046 error_at (loc, "rank of the array%'s index is greater than 1");
3047 return error_mark_node;
3050 if (TREE_TYPE (array) == error_mark_node
3051 || TREE_TYPE (idx) == error_mark_node)
3052 return error_mark_node;
3054 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3055 inside it. */
3056 switch (TREE_CODE (array))
3058 case COMPOUND_EXPR:
3060 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3061 complain);
3062 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3063 TREE_OPERAND (array, 0), value);
3064 SET_EXPR_LOCATION (ret, loc);
3065 return ret;
3068 case COND_EXPR:
3069 ret = build_conditional_expr
3070 (loc, TREE_OPERAND (array, 0),
3071 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3072 complain),
3073 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3074 complain),
3075 complain);
3076 protected_set_expr_location (ret, loc);
3077 return ret;
3079 default:
3080 break;
3083 convert_vector_to_pointer_for_subscript (loc, &array, idx);
3085 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3087 tree rval, type;
3089 warn_array_subscript_with_type_char (idx);
3091 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3093 if (complain & tf_error)
3094 error_at (loc, "array subscript is not an integer");
3095 return error_mark_node;
3098 /* Apply integral promotions *after* noticing character types.
3099 (It is unclear why we do these promotions -- the standard
3100 does not say that we should. In fact, the natural thing would
3101 seem to be to convert IDX to ptrdiff_t; we're performing
3102 pointer arithmetic.) */
3103 idx = cp_perform_integral_promotions (idx, complain);
3105 /* An array that is indexed by a non-constant
3106 cannot be stored in a register; we must be able to do
3107 address arithmetic on its address.
3108 Likewise an array of elements of variable size. */
3109 if (TREE_CODE (idx) != INTEGER_CST
3110 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3111 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3112 != INTEGER_CST)))
3114 if (!cxx_mark_addressable (array))
3115 return error_mark_node;
3118 /* An array that is indexed by a constant value which is not within
3119 the array bounds cannot be stored in a register either; because we
3120 would get a crash in store_bit_field/extract_bit_field when trying
3121 to access a non-existent part of the register. */
3122 if (TREE_CODE (idx) == INTEGER_CST
3123 && TYPE_DOMAIN (TREE_TYPE (array))
3124 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3126 if (!cxx_mark_addressable (array))
3127 return error_mark_node;
3130 if (!lvalue_p (array))
3132 if (complain & tf_error)
3133 pedwarn (loc, OPT_Wpedantic,
3134 "ISO C++ forbids subscripting non-lvalue array");
3135 else
3136 return error_mark_node;
3139 /* Note in C++ it is valid to subscript a `register' array, since
3140 it is valid to take the address of something with that
3141 storage specification. */
3142 if (extra_warnings)
3144 tree foo = array;
3145 while (TREE_CODE (foo) == COMPONENT_REF)
3146 foo = TREE_OPERAND (foo, 0);
3147 if (VAR_P (foo) && DECL_REGISTER (foo)
3148 && (complain & tf_warning))
3149 warning_at (loc, OPT_Wextra,
3150 "subscripting array declared %<register%>");
3153 type = TREE_TYPE (TREE_TYPE (array));
3154 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3155 /* Array ref is const/volatile if the array elements are
3156 or if the array is.. */
3157 TREE_READONLY (rval)
3158 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3159 TREE_SIDE_EFFECTS (rval)
3160 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3161 TREE_THIS_VOLATILE (rval)
3162 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3163 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3164 complain);
3165 protected_set_expr_location (ret, loc);
3166 return ret;
3170 tree ar = cp_default_conversion (array, complain);
3171 tree ind = cp_default_conversion (idx, complain);
3173 /* Put the integer in IND to simplify error checking. */
3174 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3176 tree temp = ar;
3177 ar = ind;
3178 ind = temp;
3181 if (ar == error_mark_node || ind == error_mark_node)
3182 return error_mark_node;
3184 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3186 if (complain & tf_error)
3187 error_at (loc, "subscripted value is neither array nor pointer");
3188 return error_mark_node;
3190 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3192 if (complain & tf_error)
3193 error_at (loc, "array subscript is not an integer");
3194 return error_mark_node;
3197 warn_array_subscript_with_type_char (idx);
3199 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3200 PLUS_EXPR, ar, ind,
3201 complain),
3202 RO_ARRAY_INDEXING,
3203 complain);
3204 protected_set_expr_location (ret, loc);
3205 return ret;
3209 /* Entry point for Obj-C++. */
3211 tree
3212 build_array_ref (location_t loc, tree array, tree idx)
3214 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3217 /* Resolve a pointer to member function. INSTANCE is the object
3218 instance to use, if the member points to a virtual member.
3220 This used to avoid checking for virtual functions if basetype
3221 has no virtual functions, according to an earlier ANSI draft.
3222 With the final ISO C++ rules, such an optimization is
3223 incorrect: A pointer to a derived member can be static_cast
3224 to pointer-to-base-member, as long as the dynamic object
3225 later has the right member. So now we only do this optimization
3226 when we know the dynamic type of the object. */
3228 tree
3229 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3230 tsubst_flags_t complain)
3232 if (TREE_CODE (function) == OFFSET_REF)
3233 function = TREE_OPERAND (function, 1);
3235 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3237 tree idx, delta, e1, e2, e3, vtbl;
3238 bool nonvirtual;
3239 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3240 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3242 tree instance_ptr = *instance_ptrptr;
3243 tree instance_save_expr = 0;
3244 if (instance_ptr == error_mark_node)
3246 if (TREE_CODE (function) == PTRMEM_CST)
3248 /* Extracting the function address from a pmf is only
3249 allowed with -Wno-pmf-conversions. It only works for
3250 pmf constants. */
3251 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3252 e1 = convert (fntype, e1);
3253 return e1;
3255 else
3257 if (complain & tf_error)
3258 error ("object missing in use of %qE", function);
3259 return error_mark_node;
3263 /* True if we know that the dynamic type of the object doesn't have
3264 virtual functions, so we can assume the PFN field is a pointer. */
3265 nonvirtual = (COMPLETE_TYPE_P (basetype)
3266 && !TYPE_POLYMORPHIC_P (basetype)
3267 && resolves_to_fixed_type_p (instance_ptr, 0));
3269 /* If we don't really have an object (i.e. in an ill-formed
3270 conversion from PMF to pointer), we can't resolve virtual
3271 functions anyway. */
3272 if (!nonvirtual && is_dummy_object (instance_ptr))
3273 nonvirtual = true;
3275 if (TREE_SIDE_EFFECTS (instance_ptr))
3276 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3278 if (TREE_SIDE_EFFECTS (function))
3279 function = save_expr (function);
3281 /* Start by extracting all the information from the PMF itself. */
3282 e3 = pfn_from_ptrmemfunc (function);
3283 delta = delta_from_ptrmemfunc (function);
3284 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3285 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3287 case ptrmemfunc_vbit_in_pfn:
3288 e1 = cp_build_binary_op (input_location,
3289 BIT_AND_EXPR, idx, integer_one_node,
3290 complain);
3291 idx = cp_build_binary_op (input_location,
3292 MINUS_EXPR, idx, integer_one_node,
3293 complain);
3294 if (idx == error_mark_node)
3295 return error_mark_node;
3296 break;
3298 case ptrmemfunc_vbit_in_delta:
3299 e1 = cp_build_binary_op (input_location,
3300 BIT_AND_EXPR, delta, integer_one_node,
3301 complain);
3302 delta = cp_build_binary_op (input_location,
3303 RSHIFT_EXPR, delta, integer_one_node,
3304 complain);
3305 if (delta == error_mark_node)
3306 return error_mark_node;
3307 break;
3309 default:
3310 gcc_unreachable ();
3313 if (e1 == error_mark_node)
3314 return error_mark_node;
3316 /* Convert down to the right base before using the instance. A
3317 special case is that in a pointer to member of class C, C may
3318 be incomplete. In that case, the function will of course be
3319 a member of C, and no conversion is required. In fact,
3320 lookup_base will fail in that case, because incomplete
3321 classes do not have BINFOs. */
3322 if (!same_type_ignoring_top_level_qualifiers_p
3323 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3325 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3326 basetype, ba_check, NULL, complain);
3327 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3328 1, complain);
3329 if (instance_ptr == error_mark_node)
3330 return error_mark_node;
3332 /* ...and then the delta in the PMF. */
3333 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3335 /* Hand back the adjusted 'this' argument to our caller. */
3336 *instance_ptrptr = instance_ptr;
3338 if (nonvirtual)
3339 /* Now just return the pointer. */
3340 return e3;
3342 /* Next extract the vtable pointer from the object. */
3343 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3344 instance_ptr);
3345 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3346 if (vtbl == error_mark_node)
3347 return error_mark_node;
3349 /* Finally, extract the function pointer from the vtable. */
3350 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3351 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3352 if (e2 == error_mark_node)
3353 return error_mark_node;
3354 TREE_CONSTANT (e2) = 1;
3356 /* When using function descriptors, the address of the
3357 vtable entry is treated as a function pointer. */
3358 if (TARGET_VTABLE_USES_DESCRIPTORS)
3359 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3360 cp_build_addr_expr (e2, complain));
3362 e2 = fold_convert (TREE_TYPE (e3), e2);
3363 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3364 if (e1 == error_mark_node)
3365 return error_mark_node;
3367 /* Make sure this doesn't get evaluated first inside one of the
3368 branches of the COND_EXPR. */
3369 if (instance_save_expr)
3370 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3371 instance_save_expr, e1);
3373 function = e1;
3375 return function;
3378 /* Used by the C-common bits. */
3379 tree
3380 build_function_call (location_t /*loc*/,
3381 tree function, tree params)
3383 return cp_build_function_call (function, params, tf_warning_or_error);
3386 /* Used by the C-common bits. */
3387 tree
3388 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3389 tree function, vec<tree, va_gc> *params,
3390 vec<tree, va_gc> * /*origtypes*/)
3392 vec<tree, va_gc> *orig_params = params;
3393 tree ret = cp_build_function_call_vec (function, &params,
3394 tf_warning_or_error);
3396 /* cp_build_function_call_vec can reallocate PARAMS by adding
3397 default arguments. That should never happen here. Verify
3398 that. */
3399 gcc_assert (params == orig_params);
3401 return ret;
3404 /* Build a function call using a tree list of arguments. */
3406 tree
3407 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3409 vec<tree, va_gc> *vec;
3410 tree ret;
3412 vec = make_tree_vector ();
3413 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3414 vec_safe_push (vec, TREE_VALUE (params));
3415 ret = cp_build_function_call_vec (function, &vec, complain);
3416 release_tree_vector (vec);
3417 return ret;
3420 /* Build a function call using varargs. */
3422 tree
3423 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3425 vec<tree, va_gc> *vec;
3426 va_list args;
3427 tree ret, t;
3429 vec = make_tree_vector ();
3430 va_start (args, complain);
3431 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3432 vec_safe_push (vec, t);
3433 va_end (args);
3434 ret = cp_build_function_call_vec (function, &vec, complain);
3435 release_tree_vector (vec);
3436 return ret;
3439 /* Build a function call using a vector of arguments. PARAMS may be
3440 NULL if there are no parameters. This changes the contents of
3441 PARAMS. */
3443 tree
3444 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3445 tsubst_flags_t complain)
3447 tree fntype, fndecl;
3448 int is_method;
3449 tree original = function;
3450 int nargs;
3451 tree *argarray;
3452 tree parm_types;
3453 vec<tree, va_gc> *allocated = NULL;
3454 tree ret;
3456 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3457 expressions, like those used for ObjC messenger dispatches. */
3458 if (params != NULL && !vec_safe_is_empty (*params))
3459 function = objc_rewrite_function_call (function, (**params)[0]);
3461 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3462 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3463 if (TREE_CODE (function) == NOP_EXPR
3464 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3465 function = TREE_OPERAND (function, 0);
3467 if (TREE_CODE (function) == FUNCTION_DECL)
3469 mark_used (function);
3470 fndecl = function;
3472 /* Convert anything with function type to a pointer-to-function. */
3473 if (DECL_MAIN_P (function))
3475 if (complain & tf_error)
3476 pedwarn (input_location, OPT_Wpedantic,
3477 "ISO C++ forbids calling %<::main%> from within program");
3478 else
3479 return error_mark_node;
3481 function = build_addr_func (function, complain);
3483 else
3485 fndecl = NULL_TREE;
3487 function = build_addr_func (function, complain);
3490 if (function == error_mark_node)
3491 return error_mark_node;
3493 fntype = TREE_TYPE (function);
3495 if (TYPE_PTRMEMFUNC_P (fntype))
3497 if (complain & tf_error)
3498 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3499 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3500 original, original);
3501 return error_mark_node;
3504 is_method = (TYPE_PTR_P (fntype)
3505 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3507 if (!(TYPE_PTRFN_P (fntype)
3508 || is_method
3509 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3511 if (complain & tf_error)
3513 if (!flag_diagnostics_show_caret)
3514 error_at (input_location,
3515 "%qE cannot be used as a function", original);
3516 else if (DECL_P (original))
3517 error_at (input_location,
3518 "%qD cannot be used as a function", original);
3519 else
3520 error_at (input_location,
3521 "expression cannot be used as a function");
3524 return error_mark_node;
3527 /* fntype now gets the type of function pointed to. */
3528 fntype = TREE_TYPE (fntype);
3529 parm_types = TYPE_ARG_TYPES (fntype);
3531 if (params == NULL)
3533 allocated = make_tree_vector ();
3534 params = &allocated;
3537 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3538 complain);
3539 if (nargs < 0)
3540 return error_mark_node;
3542 argarray = (*params)->address ();
3544 /* Check for errors in format strings and inappropriately
3545 null parameters. */
3546 check_function_arguments (fntype, nargs, argarray);
3548 ret = build_cxx_call (function, nargs, argarray, complain);
3550 if (allocated != NULL)
3551 release_tree_vector (allocated);
3553 return ret;
3556 /* Subroutine of convert_arguments.
3557 Warn about wrong number of args are genereted. */
3559 static void
3560 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3562 if (fndecl)
3564 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3566 if (DECL_NAME (fndecl) == NULL_TREE
3567 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3568 error_at (loc,
3569 too_many_p
3570 ? G_("too many arguments to constructor %q#D")
3571 : G_("too few arguments to constructor %q#D"),
3572 fndecl);
3573 else
3574 error_at (loc,
3575 too_many_p
3576 ? G_("too many arguments to member function %q#D")
3577 : G_("too few arguments to member function %q#D"),
3578 fndecl);
3580 else
3581 error_at (loc,
3582 too_many_p
3583 ? G_("too many arguments to function %q#D")
3584 : G_("too few arguments to function %q#D"),
3585 fndecl);
3586 inform (DECL_SOURCE_LOCATION (fndecl),
3587 "declared here");
3589 else
3591 if (c_dialect_objc () && objc_message_selector ())
3592 error_at (loc,
3593 too_many_p
3594 ? G_("too many arguments to method %q#D")
3595 : G_("too few arguments to method %q#D"),
3596 objc_message_selector ());
3597 else
3598 error_at (loc, too_many_p ? G_("too many arguments to function")
3599 : G_("too few arguments to function"));
3603 /* Convert the actual parameter expressions in the list VALUES to the
3604 types in the list TYPELIST. The converted expressions are stored
3605 back in the VALUES vector.
3606 If parmdecls is exhausted, or when an element has NULL as its type,
3607 perform the default conversions.
3609 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3611 This is also where warnings about wrong number of args are generated.
3613 Returns the actual number of arguments processed (which might be less
3614 than the length of the vector), or -1 on error.
3616 In C++, unspecified trailing parameters can be filled in with their
3617 default arguments, if such were specified. Do so here. */
3619 static int
3620 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3621 int flags, tsubst_flags_t complain)
3623 tree typetail;
3624 unsigned int i;
3626 /* Argument passing is always copy-initialization. */
3627 flags |= LOOKUP_ONLYCONVERTING;
3629 for (i = 0, typetail = typelist;
3630 i < vec_safe_length (*values);
3631 i++)
3633 tree type = typetail ? TREE_VALUE (typetail) : 0;
3634 tree val = (**values)[i];
3636 if (val == error_mark_node || type == error_mark_node)
3637 return -1;
3639 if (type == void_type_node)
3641 if (complain & tf_error)
3643 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3644 return i;
3646 else
3647 return -1;
3650 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3651 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3652 if (TREE_CODE (val) == NOP_EXPR
3653 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3654 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3655 val = TREE_OPERAND (val, 0);
3657 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3659 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3660 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3661 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3662 val = decay_conversion (val, complain);
3665 if (val == error_mark_node)
3666 return -1;
3668 if (type != 0)
3670 /* Formal parm type is specified by a function prototype. */
3671 tree parmval;
3673 if (!COMPLETE_TYPE_P (complete_type (type)))
3675 if (complain & tf_error)
3677 if (fndecl)
3678 error ("parameter %P of %qD has incomplete type %qT",
3679 i, fndecl, type);
3680 else
3681 error ("parameter %P has incomplete type %qT", i, type);
3683 parmval = error_mark_node;
3685 else
3687 parmval = convert_for_initialization
3688 (NULL_TREE, type, val, flags,
3689 ICR_ARGPASS, fndecl, i, complain);
3690 parmval = convert_for_arg_passing (type, parmval, complain);
3693 if (parmval == error_mark_node)
3694 return -1;
3696 (**values)[i] = parmval;
3698 else
3700 if (fndecl && magic_varargs_p (fndecl))
3701 /* Don't do ellipsis conversion for __built_in_constant_p
3702 as this will result in spurious errors for non-trivial
3703 types. */
3704 val = require_complete_type_sfinae (val, complain);
3705 else
3706 val = convert_arg_to_ellipsis (val, complain);
3708 (**values)[i] = val;
3711 if (typetail)
3712 typetail = TREE_CHAIN (typetail);
3715 if (typetail != 0 && typetail != void_list_node)
3717 /* See if there are default arguments that can be used. Because
3718 we hold default arguments in the FUNCTION_TYPE (which is so
3719 wrong), we can see default parameters here from deduced
3720 contexts (and via typeof) for indirect function calls.
3721 Fortunately we know whether we have a function decl to
3722 provide default arguments in a language conformant
3723 manner. */
3724 if (fndecl && TREE_PURPOSE (typetail)
3725 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3727 for (; typetail != void_list_node; ++i)
3729 tree parmval
3730 = convert_default_arg (TREE_VALUE (typetail),
3731 TREE_PURPOSE (typetail),
3732 fndecl, i, complain);
3734 if (parmval == error_mark_node)
3735 return -1;
3737 vec_safe_push (*values, parmval);
3738 typetail = TREE_CHAIN (typetail);
3739 /* ends with `...'. */
3740 if (typetail == NULL_TREE)
3741 break;
3744 else
3746 if (complain & tf_error)
3747 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3748 return -1;
3752 return (int) i;
3755 /* Build a binary-operation expression, after performing default
3756 conversions on the operands. CODE is the kind of expression to
3757 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3758 are the tree codes which correspond to ARG1 and ARG2 when issuing
3759 warnings about possibly misplaced parentheses. They may differ
3760 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3761 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3762 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3763 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3764 ARG2_CODE as ERROR_MARK. */
3766 tree
3767 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3768 enum tree_code arg1_code, tree arg2,
3769 enum tree_code arg2_code, tree *overload,
3770 tsubst_flags_t complain)
3772 tree orig_arg1;
3773 tree orig_arg2;
3774 tree expr;
3776 orig_arg1 = arg1;
3777 orig_arg2 = arg2;
3779 if (processing_template_decl)
3781 if (type_dependent_expression_p (arg1)
3782 || type_dependent_expression_p (arg2))
3783 return build_min_nt_loc (loc, code, arg1, arg2);
3784 arg1 = build_non_dependent_expr (arg1);
3785 arg2 = build_non_dependent_expr (arg2);
3788 if (code == DOTSTAR_EXPR)
3789 expr = build_m_component_ref (arg1, arg2, complain);
3790 else
3791 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3792 overload, complain);
3794 /* Check for cases such as x+y<<z which users are likely to
3795 misinterpret. But don't warn about obj << x + y, since that is a
3796 common idiom for I/O. */
3797 if (warn_parentheses
3798 && (complain & tf_warning)
3799 && !processing_template_decl
3800 && !error_operand_p (arg1)
3801 && !error_operand_p (arg2)
3802 && (code != LSHIFT_EXPR
3803 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3804 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3805 arg2_code, orig_arg2);
3807 if (processing_template_decl && expr != error_mark_node)
3808 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3810 return expr;
3813 /* Build and return an ARRAY_REF expression. */
3815 tree
3816 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3817 tsubst_flags_t complain)
3819 tree orig_arg1 = arg1;
3820 tree orig_arg2 = arg2;
3821 tree expr;
3823 if (processing_template_decl)
3825 if (type_dependent_expression_p (arg1)
3826 || type_dependent_expression_p (arg2))
3827 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3828 NULL_TREE, NULL_TREE);
3829 arg1 = build_non_dependent_expr (arg1);
3830 arg2 = build_non_dependent_expr (arg2);
3833 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3834 NULL_TREE, /*overload=*/NULL, complain);
3836 if (processing_template_decl && expr != error_mark_node)
3837 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3838 NULL_TREE, NULL_TREE);
3839 return expr;
3842 /* Return whether OP is an expression of enum type cast to integer
3843 type. In C++ even unsigned enum types are cast to signed integer
3844 types. We do not want to issue warnings about comparisons between
3845 signed and unsigned types when one of the types is an enum type.
3846 Those warnings are always false positives in practice. */
3848 static bool
3849 enum_cast_to_int (tree op)
3851 if (CONVERT_EXPR_P (op)
3852 && TREE_TYPE (op) == integer_type_node
3853 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3854 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3855 return true;
3857 /* The cast may have been pushed into a COND_EXPR. */
3858 if (TREE_CODE (op) == COND_EXPR)
3859 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3860 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3862 return false;
3865 /* For the c-common bits. */
3866 tree
3867 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3868 int /*convert_p*/)
3870 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3874 /* Build a binary-operation expression without default conversions.
3875 CODE is the kind of expression to build.
3876 LOCATION is the location_t of the operator in the source code.
3877 This function differs from `build' in several ways:
3878 the data type of the result is computed and recorded in it,
3879 warnings are generated if arg data types are invalid,
3880 special handling for addition and subtraction of pointers is known,
3881 and some optimization is done (operations on narrow ints
3882 are done in the narrower type when that gives the same result).
3883 Constant folding is also done before the result is returned.
3885 Note that the operands will never have enumeral types
3886 because either they have just had the default conversions performed
3887 or they have both just been converted to some other type in which
3888 the arithmetic is to be done.
3890 C++: must do special pointer arithmetic when implementing
3891 multiple inheritance, and deal with pointer to member functions. */
3893 tree
3894 cp_build_binary_op (location_t location,
3895 enum tree_code code, tree orig_op0, tree orig_op1,
3896 tsubst_flags_t complain)
3898 tree op0, op1;
3899 enum tree_code code0, code1;
3900 tree type0, type1;
3901 const char *invalid_op_diag;
3903 /* Expression code to give to the expression when it is built.
3904 Normally this is CODE, which is what the caller asked for,
3905 but in some special cases we change it. */
3906 enum tree_code resultcode = code;
3908 /* Data type in which the computation is to be performed.
3909 In the simplest cases this is the common type of the arguments. */
3910 tree result_type = NULL;
3912 /* Nonzero means operands have already been type-converted
3913 in whatever way is necessary.
3914 Zero means they need to be converted to RESULT_TYPE. */
3915 int converted = 0;
3917 /* Nonzero means create the expression with this type, rather than
3918 RESULT_TYPE. */
3919 tree build_type = 0;
3921 /* Nonzero means after finally constructing the expression
3922 convert it to this type. */
3923 tree final_type = 0;
3925 tree result;
3926 tree orig_type = NULL;
3928 /* Nonzero if this is an operation like MIN or MAX which can
3929 safely be computed in short if both args are promoted shorts.
3930 Also implies COMMON.
3931 -1 indicates a bitwise operation; this makes a difference
3932 in the exact conditions for when it is safe to do the operation
3933 in a narrower mode. */
3934 int shorten = 0;
3936 /* Nonzero if this is a comparison operation;
3937 if both args are promoted shorts, compare the original shorts.
3938 Also implies COMMON. */
3939 int short_compare = 0;
3941 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3942 int common = 0;
3944 /* True if both operands have arithmetic type. */
3945 bool arithmetic_types_p;
3947 /* Apply default conversions. */
3948 op0 = orig_op0;
3949 op1 = orig_op1;
3951 /* Remember whether we're doing / or %. */
3952 bool doing_div_or_mod = false;
3954 /* Remember whether we're doing << or >>. */
3955 bool doing_shift = false;
3957 /* Tree holding instrumentation expression. */
3958 tree instrument_expr = NULL;
3960 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3961 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3962 || code == TRUTH_XOR_EXPR)
3964 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3965 op0 = decay_conversion (op0, complain);
3966 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3967 op1 = decay_conversion (op1, complain);
3969 else
3971 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3972 op0 = cp_default_conversion (op0, complain);
3973 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3974 op1 = cp_default_conversion (op1, complain);
3977 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3978 STRIP_TYPE_NOPS (op0);
3979 STRIP_TYPE_NOPS (op1);
3981 /* DTRT if one side is an overloaded function, but complain about it. */
3982 if (type_unknown_p (op0))
3984 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3985 if (t != error_mark_node)
3987 if (complain & tf_error)
3988 permerror (input_location, "assuming cast to type %qT from overloaded function",
3989 TREE_TYPE (t));
3990 op0 = t;
3993 if (type_unknown_p (op1))
3995 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3996 if (t != error_mark_node)
3998 if (complain & tf_error)
3999 permerror (input_location, "assuming cast to type %qT from overloaded function",
4000 TREE_TYPE (t));
4001 op1 = t;
4005 type0 = TREE_TYPE (op0);
4006 type1 = TREE_TYPE (op1);
4008 /* The expression codes of the data types of the arguments tell us
4009 whether the arguments are integers, floating, pointers, etc. */
4010 code0 = TREE_CODE (type0);
4011 code1 = TREE_CODE (type1);
4013 /* If an error was already reported for one of the arguments,
4014 avoid reporting another error. */
4015 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4016 return error_mark_node;
4018 if ((invalid_op_diag
4019 = targetm.invalid_binary_op (code, type0, type1)))
4021 if (complain & tf_error)
4022 error (invalid_op_diag);
4023 return error_mark_node;
4026 /* Issue warnings about peculiar, but valid, uses of NULL. */
4027 if ((orig_op0 == null_node || orig_op1 == null_node)
4028 /* It's reasonable to use pointer values as operands of &&
4029 and ||, so NULL is no exception. */
4030 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4031 && ( /* Both are NULL (or 0) and the operation was not a
4032 comparison or a pointer subtraction. */
4033 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4034 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4035 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4036 || (!null_ptr_cst_p (orig_op0)
4037 && !TYPE_PTR_OR_PTRMEM_P (type0))
4038 || (!null_ptr_cst_p (orig_op1)
4039 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4040 && (complain & tf_warning))
4042 source_location loc =
4043 expansion_point_location_if_in_system_header (input_location);
4045 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4048 /* In case when one of the operands of the binary operation is
4049 a vector and another is a scalar -- convert scalar to vector. */
4050 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4052 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4053 complain & tf_error);
4055 switch (convert_flag)
4057 case stv_error:
4058 return error_mark_node;
4059 case stv_firstarg:
4061 op0 = convert (TREE_TYPE (type1), op0);
4062 op0 = save_expr (op0);
4063 op0 = build_vector_from_val (type1, op0);
4064 type0 = TREE_TYPE (op0);
4065 code0 = TREE_CODE (type0);
4066 converted = 1;
4067 break;
4069 case stv_secondarg:
4071 op1 = convert (TREE_TYPE (type0), op1);
4072 op1 = save_expr (op1);
4073 op1 = build_vector_from_val (type0, op1);
4074 type1 = TREE_TYPE (op1);
4075 code1 = TREE_CODE (type1);
4076 converted = 1;
4077 break;
4079 default:
4080 break;
4084 switch (code)
4086 case MINUS_EXPR:
4087 /* Subtraction of two similar pointers.
4088 We must subtract them as integers, then divide by object size. */
4089 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4090 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4091 TREE_TYPE (type1)))
4092 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4093 complain);
4094 /* In all other cases except pointer - int, the usual arithmetic
4095 rules apply. */
4096 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4098 common = 1;
4099 break;
4101 /* The pointer - int case is just like pointer + int; fall
4102 through. */
4103 case PLUS_EXPR:
4104 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4105 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4107 tree ptr_operand;
4108 tree int_operand;
4109 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4110 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4111 if (processing_template_decl)
4113 result_type = TREE_TYPE (ptr_operand);
4114 break;
4116 return cp_pointer_int_sum (code,
4117 ptr_operand,
4118 int_operand,
4119 complain);
4121 common = 1;
4122 break;
4124 case MULT_EXPR:
4125 common = 1;
4126 break;
4128 case TRUNC_DIV_EXPR:
4129 case CEIL_DIV_EXPR:
4130 case FLOOR_DIV_EXPR:
4131 case ROUND_DIV_EXPR:
4132 case EXACT_DIV_EXPR:
4133 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4134 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4135 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4136 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4138 enum tree_code tcode0 = code0, tcode1 = code1;
4139 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4140 cop1 = maybe_constant_value (cop1);
4141 doing_div_or_mod = true;
4142 warn_for_div_by_zero (location, cop1);
4144 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4145 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4146 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4147 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4149 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4150 resultcode = RDIV_EXPR;
4151 else
4152 /* When dividing two signed integers, we have to promote to int.
4153 unless we divide by a constant != -1. Note that default
4154 conversion will have been performed on the operands at this
4155 point, so we have to dig out the original type to find out if
4156 it was unsigned. */
4157 shorten = ((TREE_CODE (op0) == NOP_EXPR
4158 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4159 || (TREE_CODE (op1) == INTEGER_CST
4160 && ! integer_all_onesp (op1)));
4162 common = 1;
4164 break;
4166 case BIT_AND_EXPR:
4167 case BIT_IOR_EXPR:
4168 case BIT_XOR_EXPR:
4169 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4170 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4171 && !VECTOR_FLOAT_TYPE_P (type0)
4172 && !VECTOR_FLOAT_TYPE_P (type1)))
4173 shorten = -1;
4174 break;
4176 case TRUNC_MOD_EXPR:
4177 case FLOOR_MOD_EXPR:
4179 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4180 cop1 = maybe_constant_value (cop1);
4181 doing_div_or_mod = true;
4182 warn_for_div_by_zero (location, cop1);
4185 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4186 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4187 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4188 common = 1;
4189 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4191 /* Although it would be tempting to shorten always here, that loses
4192 on some targets, since the modulo instruction is undefined if the
4193 quotient can't be represented in the computation mode. We shorten
4194 only if unsigned or if dividing by something we know != -1. */
4195 shorten = ((TREE_CODE (op0) == NOP_EXPR
4196 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4197 || (TREE_CODE (op1) == INTEGER_CST
4198 && ! integer_all_onesp (op1)));
4199 common = 1;
4201 break;
4203 case TRUTH_ANDIF_EXPR:
4204 case TRUTH_ORIF_EXPR:
4205 case TRUTH_AND_EXPR:
4206 case TRUTH_OR_EXPR:
4207 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4209 if (!COMPARISON_CLASS_P (op1))
4210 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4211 build_zero_cst (type1), complain);
4212 if (code == TRUTH_ANDIF_EXPR)
4214 tree z = build_zero_cst (TREE_TYPE (op1));
4215 return build_conditional_expr (location, op0, op1, z, complain);
4217 else if (code == TRUTH_ORIF_EXPR)
4219 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4220 return build_conditional_expr (location, op0, m1, op1, complain);
4222 else
4223 gcc_unreachable ();
4225 if (VECTOR_TYPE_P (type0))
4227 if (!COMPARISON_CLASS_P (op0))
4228 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4229 build_zero_cst (type0), complain);
4230 if (!VECTOR_TYPE_P (type1))
4232 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4233 tree z = build_zero_cst (TREE_TYPE (op0));
4234 op1 = build_conditional_expr (location, op1, z, m1, complain);
4236 else if (!COMPARISON_CLASS_P (op1))
4237 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4238 build_zero_cst (type1), complain);
4240 if (code == TRUTH_ANDIF_EXPR)
4241 code = BIT_AND_EXPR;
4242 else if (code == TRUTH_ORIF_EXPR)
4243 code = BIT_IOR_EXPR;
4244 else
4245 gcc_unreachable ();
4247 return cp_build_binary_op (location, code, op0, op1, complain);
4250 result_type = boolean_type_node;
4251 break;
4253 /* Shift operations: result has same type as first operand;
4254 always convert second operand to int.
4255 Also set SHORT_SHIFT if shifting rightward. */
4257 case RSHIFT_EXPR:
4258 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4259 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4261 result_type = type0;
4262 converted = 1;
4264 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4265 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4266 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4267 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4269 result_type = type0;
4270 converted = 1;
4272 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4274 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4275 const_op1 = maybe_constant_value (const_op1);
4276 if (TREE_CODE (const_op1) != INTEGER_CST)
4277 const_op1 = op1;
4278 result_type = type0;
4279 doing_shift = true;
4280 if (TREE_CODE (const_op1) == INTEGER_CST)
4282 if (tree_int_cst_lt (const_op1, integer_zero_node))
4284 if ((complain & tf_warning)
4285 && c_inhibit_evaluation_warnings == 0)
4286 warning (0, "right shift count is negative");
4288 else
4290 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4291 && (complain & tf_warning)
4292 && c_inhibit_evaluation_warnings == 0)
4293 warning (0, "right shift count >= width of type");
4296 /* Convert the shift-count to an integer, regardless of
4297 size of value being shifted. */
4298 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4299 op1 = cp_convert (integer_type_node, op1, complain);
4300 /* Avoid converting op1 to result_type later. */
4301 converted = 1;
4303 break;
4305 case LSHIFT_EXPR:
4306 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4307 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4309 result_type = type0;
4310 converted = 1;
4312 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4313 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4314 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4315 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4317 result_type = type0;
4318 converted = 1;
4320 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4322 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4323 const_op1 = maybe_constant_value (const_op1);
4324 if (TREE_CODE (const_op1) != INTEGER_CST)
4325 const_op1 = op1;
4326 result_type = type0;
4327 doing_shift = true;
4328 if (TREE_CODE (const_op1) == INTEGER_CST)
4330 if (tree_int_cst_lt (const_op1, integer_zero_node))
4332 if ((complain & tf_warning)
4333 && c_inhibit_evaluation_warnings == 0)
4334 warning (0, "left shift count is negative");
4336 else if (compare_tree_int (const_op1,
4337 TYPE_PRECISION (type0)) >= 0)
4339 if ((complain & tf_warning)
4340 && c_inhibit_evaluation_warnings == 0)
4341 warning (0, "left shift count >= width of type");
4344 /* Convert the shift-count to an integer, regardless of
4345 size of value being shifted. */
4346 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4347 op1 = cp_convert (integer_type_node, op1, complain);
4348 /* Avoid converting op1 to result_type later. */
4349 converted = 1;
4351 break;
4353 case RROTATE_EXPR:
4354 case LROTATE_EXPR:
4355 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4357 result_type = type0;
4358 if (TREE_CODE (op1) == INTEGER_CST)
4360 if (tree_int_cst_lt (op1, integer_zero_node))
4362 if (complain & tf_warning)
4363 warning (0, (code == LROTATE_EXPR)
4364 ? G_("left rotate count is negative")
4365 : G_("right rotate count is negative"));
4367 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4369 if (complain & tf_warning)
4370 warning (0, (code == LROTATE_EXPR)
4371 ? G_("left rotate count >= width of type")
4372 : G_("right rotate count >= width of type"));
4375 /* Convert the shift-count to an integer, regardless of
4376 size of value being shifted. */
4377 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4378 op1 = cp_convert (integer_type_node, op1, complain);
4380 break;
4382 case EQ_EXPR:
4383 case NE_EXPR:
4384 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4385 goto vector_compare;
4386 if ((complain & tf_warning)
4387 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4388 warning (OPT_Wfloat_equal,
4389 "comparing floating point with == or != is unsafe");
4390 if ((complain & tf_warning)
4391 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4392 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4393 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4395 build_type = boolean_type_node;
4396 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4397 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4398 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4399 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4400 short_compare = 1;
4401 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4402 && null_ptr_cst_p (op1))
4403 /* Handle, eg, (void*)0 (c++/43906), and more. */
4404 || (code0 == POINTER_TYPE
4405 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4407 if (TYPE_PTR_P (type1))
4408 result_type = composite_pointer_type (type0, type1, op0, op1,
4409 CPO_COMPARISON, complain);
4410 else
4411 result_type = type0;
4413 if (TREE_CODE (op0) == ADDR_EXPR
4414 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4416 if ((complain & tf_warning)
4417 && c_inhibit_evaluation_warnings == 0)
4418 warning (OPT_Waddress, "the address of %qD will never be NULL",
4419 TREE_OPERAND (op0, 0));
4422 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4423 && null_ptr_cst_p (op0))
4424 /* Handle, eg, (void*)0 (c++/43906), and more. */
4425 || (code1 == POINTER_TYPE
4426 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4428 if (TYPE_PTR_P (type0))
4429 result_type = composite_pointer_type (type0, type1, op0, op1,
4430 CPO_COMPARISON, complain);
4431 else
4432 result_type = type1;
4434 if (TREE_CODE (op1) == ADDR_EXPR
4435 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4437 if ((complain & tf_warning)
4438 && c_inhibit_evaluation_warnings == 0)
4439 warning (OPT_Waddress, "the address of %qD will never be NULL",
4440 TREE_OPERAND (op1, 0));
4443 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4444 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4445 result_type = composite_pointer_type (type0, type1, op0, op1,
4446 CPO_COMPARISON, complain);
4447 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4448 /* One of the operands must be of nullptr_t type. */
4449 result_type = TREE_TYPE (nullptr_node);
4450 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4452 result_type = type0;
4453 if (complain & tf_error)
4454 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4455 else
4456 return error_mark_node;
4458 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4460 result_type = type1;
4461 if (complain & tf_error)
4462 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4463 else
4464 return error_mark_node;
4466 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4468 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4469 == ptrmemfunc_vbit_in_delta)
4471 tree pfn0, delta0, e1, e2;
4473 if (TREE_SIDE_EFFECTS (op0))
4474 op0 = save_expr (op0);
4476 pfn0 = pfn_from_ptrmemfunc (op0);
4477 delta0 = delta_from_ptrmemfunc (op0);
4478 e1 = cp_build_binary_op (location,
4479 EQ_EXPR,
4480 pfn0,
4481 build_zero_cst (TREE_TYPE (pfn0)),
4482 complain);
4483 e2 = cp_build_binary_op (location,
4484 BIT_AND_EXPR,
4485 delta0,
4486 integer_one_node,
4487 complain);
4489 if (complain & tf_warning)
4490 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4492 e2 = cp_build_binary_op (location,
4493 EQ_EXPR, e2, integer_zero_node,
4494 complain);
4495 op0 = cp_build_binary_op (location,
4496 TRUTH_ANDIF_EXPR, e1, e2,
4497 complain);
4498 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4500 else
4502 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4503 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4505 result_type = TREE_TYPE (op0);
4507 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4508 return cp_build_binary_op (location, code, op1, op0, complain);
4509 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4511 tree type;
4512 /* E will be the final comparison. */
4513 tree e;
4514 /* E1 and E2 are for scratch. */
4515 tree e1;
4516 tree e2;
4517 tree pfn0;
4518 tree pfn1;
4519 tree delta0;
4520 tree delta1;
4522 type = composite_pointer_type (type0, type1, op0, op1,
4523 CPO_COMPARISON, complain);
4525 if (!same_type_p (TREE_TYPE (op0), type))
4526 op0 = cp_convert_and_check (type, op0, complain);
4527 if (!same_type_p (TREE_TYPE (op1), type))
4528 op1 = cp_convert_and_check (type, op1, complain);
4530 if (op0 == error_mark_node || op1 == error_mark_node)
4531 return error_mark_node;
4533 if (TREE_SIDE_EFFECTS (op0))
4534 op0 = save_expr (op0);
4535 if (TREE_SIDE_EFFECTS (op1))
4536 op1 = save_expr (op1);
4538 pfn0 = pfn_from_ptrmemfunc (op0);
4539 pfn1 = pfn_from_ptrmemfunc (op1);
4540 delta0 = delta_from_ptrmemfunc (op0);
4541 delta1 = delta_from_ptrmemfunc (op1);
4542 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4543 == ptrmemfunc_vbit_in_delta)
4545 /* We generate:
4547 (op0.pfn == op1.pfn
4548 && ((op0.delta == op1.delta)
4549 || (!op0.pfn && op0.delta & 1 == 0
4550 && op1.delta & 1 == 0))
4552 The reason for the `!op0.pfn' bit is that a NULL
4553 pointer-to-member is any member with a zero PFN and
4554 LSB of the DELTA field is 0. */
4556 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4557 delta0,
4558 integer_one_node,
4559 complain);
4560 e1 = cp_build_binary_op (location,
4561 EQ_EXPR, e1, integer_zero_node,
4562 complain);
4563 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4564 delta1,
4565 integer_one_node,
4566 complain);
4567 e2 = cp_build_binary_op (location,
4568 EQ_EXPR, e2, integer_zero_node,
4569 complain);
4570 e1 = cp_build_binary_op (location,
4571 TRUTH_ANDIF_EXPR, e2, e1,
4572 complain);
4573 e2 = cp_build_binary_op (location, EQ_EXPR,
4574 pfn0,
4575 build_zero_cst (TREE_TYPE (pfn0)),
4576 complain);
4577 e2 = cp_build_binary_op (location,
4578 TRUTH_ANDIF_EXPR, e2, e1, complain);
4579 e1 = cp_build_binary_op (location,
4580 EQ_EXPR, delta0, delta1, complain);
4581 e1 = cp_build_binary_op (location,
4582 TRUTH_ORIF_EXPR, e1, e2, complain);
4584 else
4586 /* We generate:
4588 (op0.pfn == op1.pfn
4589 && (!op0.pfn || op0.delta == op1.delta))
4591 The reason for the `!op0.pfn' bit is that a NULL
4592 pointer-to-member is any member with a zero PFN; the
4593 DELTA field is unspecified. */
4595 e1 = cp_build_binary_op (location,
4596 EQ_EXPR, delta0, delta1, complain);
4597 e2 = cp_build_binary_op (location,
4598 EQ_EXPR,
4599 pfn0,
4600 build_zero_cst (TREE_TYPE (pfn0)),
4601 complain);
4602 e1 = cp_build_binary_op (location,
4603 TRUTH_ORIF_EXPR, e1, e2, complain);
4605 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4606 e = cp_build_binary_op (location,
4607 TRUTH_ANDIF_EXPR, e2, e1, complain);
4608 if (code == EQ_EXPR)
4609 return e;
4610 return cp_build_binary_op (location,
4611 EQ_EXPR, e, integer_zero_node, complain);
4613 else
4615 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4616 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4617 type1));
4618 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4619 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4620 type0));
4623 break;
4625 case MAX_EXPR:
4626 case MIN_EXPR:
4627 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4628 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4629 shorten = 1;
4630 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4631 result_type = composite_pointer_type (type0, type1, op0, op1,
4632 CPO_COMPARISON, complain);
4633 break;
4635 case LE_EXPR:
4636 case GE_EXPR:
4637 case LT_EXPR:
4638 case GT_EXPR:
4639 if (TREE_CODE (orig_op0) == STRING_CST
4640 || TREE_CODE (orig_op1) == STRING_CST)
4642 if (complain & tf_warning)
4643 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4646 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4648 vector_compare:
4649 tree intt;
4650 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4651 TREE_TYPE (type1))
4652 && !vector_types_compatible_elements_p (type0, type1))
4654 if (complain & tf_error)
4656 error_at (location, "comparing vectors with different "
4657 "element types");
4658 inform (location, "operand types are %qT and %qT",
4659 type0, type1);
4661 return error_mark_node;
4664 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4666 if (complain & tf_error)
4668 error_at (location, "comparing vectors with different "
4669 "number of elements");
4670 inform (location, "operand types are %qT and %qT",
4671 type0, type1);
4673 return error_mark_node;
4676 /* Always construct signed integer vector type. */
4677 intt = c_common_type_for_size (GET_MODE_BITSIZE
4678 (TYPE_MODE (TREE_TYPE (type0))), 0);
4679 if (!intt)
4681 if (complain & tf_error)
4682 error_at (location, "could not find an integer type "
4683 "of the same size as %qT", TREE_TYPE (type0));
4684 return error_mark_node;
4686 result_type = build_opaque_vector_type (intt,
4687 TYPE_VECTOR_SUBPARTS (type0));
4688 converted = 1;
4689 break;
4691 build_type = boolean_type_node;
4692 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4693 || code0 == ENUMERAL_TYPE)
4694 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4695 || code1 == ENUMERAL_TYPE))
4696 short_compare = 1;
4697 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4698 result_type = composite_pointer_type (type0, type1, op0, op1,
4699 CPO_COMPARISON, complain);
4700 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4702 result_type = type0;
4703 if (extra_warnings && (complain & tf_warning))
4704 warning (OPT_Wextra,
4705 "ordered comparison of pointer with integer zero");
4707 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4709 result_type = type1;
4710 if (extra_warnings && (complain & tf_warning))
4711 warning (OPT_Wextra,
4712 "ordered comparison of pointer with integer zero");
4714 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4715 /* One of the operands must be of nullptr_t type. */
4716 result_type = TREE_TYPE (nullptr_node);
4717 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4719 result_type = type0;
4720 if (complain & tf_error)
4721 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4722 else
4723 return error_mark_node;
4725 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4727 result_type = type1;
4728 if (complain & tf_error)
4729 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4730 else
4731 return error_mark_node;
4733 break;
4735 case UNORDERED_EXPR:
4736 case ORDERED_EXPR:
4737 case UNLT_EXPR:
4738 case UNLE_EXPR:
4739 case UNGT_EXPR:
4740 case UNGE_EXPR:
4741 case UNEQ_EXPR:
4742 build_type = integer_type_node;
4743 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4745 if (complain & tf_error)
4746 error ("unordered comparison on non-floating point argument");
4747 return error_mark_node;
4749 common = 1;
4750 break;
4752 default:
4753 break;
4756 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4757 || code0 == ENUMERAL_TYPE)
4758 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4759 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4760 arithmetic_types_p = 1;
4761 else
4763 arithmetic_types_p = 0;
4764 /* Vector arithmetic is only allowed when both sides are vectors. */
4765 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4767 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4768 || !vector_types_compatible_elements_p (type0, type1))
4770 if (complain & tf_error)
4771 binary_op_error (location, code, type0, type1);
4772 return error_mark_node;
4774 arithmetic_types_p = 1;
4777 /* Determine the RESULT_TYPE, if it is not already known. */
4778 if (!result_type
4779 && arithmetic_types_p
4780 && (shorten || common || short_compare))
4782 result_type = cp_common_type (type0, type1);
4783 if (complain & tf_warning)
4784 do_warn_double_promotion (result_type, type0, type1,
4785 "implicit conversion from %qT to %qT "
4786 "to match other operand of binary "
4787 "expression",
4788 location);
4791 if (!result_type)
4793 if (complain & tf_error)
4794 error ("invalid operands of types %qT and %qT to binary %qO",
4795 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4796 return error_mark_node;
4799 /* If we're in a template, the only thing we need to know is the
4800 RESULT_TYPE. */
4801 if (processing_template_decl)
4803 /* Since the middle-end checks the type when doing a build2, we
4804 need to build the tree in pieces. This built tree will never
4805 get out of the front-end as we replace it when instantiating
4806 the template. */
4807 tree tmp = build2 (resultcode,
4808 build_type ? build_type : result_type,
4809 NULL_TREE, op1);
4810 TREE_OPERAND (tmp, 0) = op0;
4811 return tmp;
4814 if (arithmetic_types_p)
4816 bool first_complex = (code0 == COMPLEX_TYPE);
4817 bool second_complex = (code1 == COMPLEX_TYPE);
4818 int none_complex = (!first_complex && !second_complex);
4820 /* Adapted from patch for c/24581. */
4821 if (first_complex != second_complex
4822 && (code == PLUS_EXPR
4823 || code == MINUS_EXPR
4824 || code == MULT_EXPR
4825 || (code == TRUNC_DIV_EXPR && first_complex))
4826 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4827 && flag_signed_zeros)
4829 /* An operation on mixed real/complex operands must be
4830 handled specially, but the language-independent code can
4831 more easily optimize the plain complex arithmetic if
4832 -fno-signed-zeros. */
4833 tree real_type = TREE_TYPE (result_type);
4834 tree real, imag;
4835 if (first_complex)
4837 if (TREE_TYPE (op0) != result_type)
4838 op0 = cp_convert_and_check (result_type, op0, complain);
4839 if (TREE_TYPE (op1) != real_type)
4840 op1 = cp_convert_and_check (real_type, op1, complain);
4842 else
4844 if (TREE_TYPE (op0) != real_type)
4845 op0 = cp_convert_and_check (real_type, op0, complain);
4846 if (TREE_TYPE (op1) != result_type)
4847 op1 = cp_convert_and_check (result_type, op1, complain);
4849 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4850 return error_mark_node;
4851 if (first_complex)
4853 op0 = save_expr (op0);
4854 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4855 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4856 switch (code)
4858 case MULT_EXPR:
4859 case TRUNC_DIV_EXPR:
4860 op1 = save_expr (op1);
4861 imag = build2 (resultcode, real_type, imag, op1);
4862 /* Fall through. */
4863 case PLUS_EXPR:
4864 case MINUS_EXPR:
4865 real = build2 (resultcode, real_type, real, op1);
4866 break;
4867 default:
4868 gcc_unreachable();
4871 else
4873 op1 = save_expr (op1);
4874 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4875 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4876 switch (code)
4878 case MULT_EXPR:
4879 op0 = save_expr (op0);
4880 imag = build2 (resultcode, real_type, op0, imag);
4881 /* Fall through. */
4882 case PLUS_EXPR:
4883 real = build2 (resultcode, real_type, op0, real);
4884 break;
4885 case MINUS_EXPR:
4886 real = build2 (resultcode, real_type, op0, real);
4887 imag = build1 (NEGATE_EXPR, real_type, imag);
4888 break;
4889 default:
4890 gcc_unreachable();
4893 real = fold_if_not_in_template (real);
4894 imag = fold_if_not_in_template (imag);
4895 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4896 result = fold_if_not_in_template (result);
4897 return result;
4900 /* For certain operations (which identify themselves by shorten != 0)
4901 if both args were extended from the same smaller type,
4902 do the arithmetic in that type and then extend.
4904 shorten !=0 and !=1 indicates a bitwise operation.
4905 For them, this optimization is safe only if
4906 both args are zero-extended or both are sign-extended.
4907 Otherwise, we might change the result.
4908 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4909 but calculated in (unsigned short) it would be (unsigned short)-1. */
4911 if (shorten && none_complex)
4913 orig_type = result_type;
4914 final_type = result_type;
4915 result_type = shorten_binary_op (result_type, op0, op1,
4916 shorten == -1);
4919 /* Comparison operations are shortened too but differently.
4920 They identify themselves by setting short_compare = 1. */
4922 if (short_compare)
4924 /* Don't write &op0, etc., because that would prevent op0
4925 from being kept in a register.
4926 Instead, make copies of the our local variables and
4927 pass the copies by reference, then copy them back afterward. */
4928 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4929 enum tree_code xresultcode = resultcode;
4930 tree val
4931 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4932 &xresultcode);
4933 if (val != 0)
4934 return cp_convert (boolean_type_node, val, complain);
4935 op0 = xop0, op1 = xop1;
4936 converted = 1;
4937 resultcode = xresultcode;
4940 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4941 && warn_sign_compare
4942 /* Do not warn until the template is instantiated; we cannot
4943 bound the ranges of the arguments until that point. */
4944 && !processing_template_decl
4945 && (complain & tf_warning)
4946 && c_inhibit_evaluation_warnings == 0
4947 /* Even unsigned enum types promote to signed int. We don't
4948 want to issue -Wsign-compare warnings for this case. */
4949 && !enum_cast_to_int (orig_op0)
4950 && !enum_cast_to_int (orig_op1))
4952 tree oop0 = maybe_constant_value (orig_op0);
4953 tree oop1 = maybe_constant_value (orig_op1);
4955 if (TREE_CODE (oop0) != INTEGER_CST)
4956 oop0 = orig_op0;
4957 if (TREE_CODE (oop1) != INTEGER_CST)
4958 oop1 = orig_op1;
4959 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4960 result_type, resultcode);
4964 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4965 Then the expression will be built.
4966 It will be given type FINAL_TYPE if that is nonzero;
4967 otherwise, it will be given type RESULT_TYPE. */
4968 if (! converted)
4970 if (TREE_TYPE (op0) != result_type)
4971 op0 = cp_convert_and_check (result_type, op0, complain);
4972 if (TREE_TYPE (op1) != result_type)
4973 op1 = cp_convert_and_check (result_type, op1, complain);
4975 if (op0 == error_mark_node || op1 == error_mark_node)
4976 return error_mark_node;
4979 if (build_type == NULL_TREE)
4980 build_type = result_type;
4982 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
4983 | SANITIZE_FLOAT_DIVIDE))
4984 && !processing_template_decl
4985 && current_function_decl != 0
4986 && !lookup_attribute ("no_sanitize_undefined",
4987 DECL_ATTRIBUTES (current_function_decl))
4988 && (doing_div_or_mod || doing_shift))
4990 /* OP0 and/or OP1 might have side-effects. */
4991 op0 = cp_save_expr (op0);
4992 op1 = cp_save_expr (op1);
4993 op0 = maybe_constant_value (fold_non_dependent_expr_sfinae (op0,
4994 tf_none));
4995 op1 = maybe_constant_value (fold_non_dependent_expr_sfinae (op1,
4996 tf_none));
4997 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
4998 | SANITIZE_FLOAT_DIVIDE)))
5000 /* For diagnostics we want to use the promoted types without
5001 shorten_binary_op. So convert the arguments to the
5002 original result_type. */
5003 tree cop0 = op0;
5004 tree cop1 = op1;
5005 if (orig_type != NULL && result_type != orig_type)
5007 cop0 = cp_convert (orig_type, op0, complain);
5008 cop1 = cp_convert (orig_type, op1, complain);
5010 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5012 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5013 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5016 result = build2 (resultcode, build_type, op0, op1);
5017 result = fold_if_not_in_template (result);
5018 if (final_type != 0)
5019 result = cp_convert (final_type, result, complain);
5021 if (TREE_OVERFLOW_P (result)
5022 && !TREE_OVERFLOW_P (op0)
5023 && !TREE_OVERFLOW_P (op1))
5024 overflow_warning (location, result);
5026 if (instrument_expr != NULL)
5027 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
5028 instrument_expr, result);
5030 return result;
5033 /* Build a VEC_PERM_EXPR.
5034 This is a simple wrapper for c_build_vec_perm_expr. */
5035 tree
5036 build_x_vec_perm_expr (location_t loc,
5037 tree arg0, tree arg1, tree arg2,
5038 tsubst_flags_t complain)
5040 tree orig_arg0 = arg0;
5041 tree orig_arg1 = arg1;
5042 tree orig_arg2 = arg2;
5043 if (processing_template_decl)
5045 if (type_dependent_expression_p (arg0)
5046 || type_dependent_expression_p (arg1)
5047 || type_dependent_expression_p (arg2))
5048 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5049 arg0 = build_non_dependent_expr (arg0);
5050 if (arg1)
5051 arg1 = build_non_dependent_expr (arg1);
5052 arg2 = build_non_dependent_expr (arg2);
5054 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5055 if (processing_template_decl && exp != error_mark_node)
5056 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5057 orig_arg1, orig_arg2);
5058 return exp;
5061 /* Return a tree for the sum or difference (RESULTCODE says which)
5062 of pointer PTROP and integer INTOP. */
5064 static tree
5065 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5066 tsubst_flags_t complain)
5068 tree res_type = TREE_TYPE (ptrop);
5070 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5071 in certain circumstance (when it's valid to do so). So we need
5072 to make sure it's complete. We don't need to check here, if we
5073 can actually complete it at all, as those checks will be done in
5074 pointer_int_sum() anyway. */
5075 complete_type (TREE_TYPE (res_type));
5077 return pointer_int_sum (input_location, resultcode, ptrop,
5078 fold_if_not_in_template (intop),
5079 complain & tf_warning_or_error);
5082 /* Return a tree for the difference of pointers OP0 and OP1.
5083 The resulting tree has type int. */
5085 static tree
5086 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5088 tree result;
5089 tree restype = ptrdiff_type_node;
5090 tree target_type = TREE_TYPE (ptrtype);
5092 if (!complete_type_or_else (target_type, NULL_TREE))
5093 return error_mark_node;
5095 if (VOID_TYPE_P (target_type))
5097 if (complain & tf_error)
5098 permerror (input_location, "ISO C++ forbids using pointer of "
5099 "type %<void *%> in subtraction");
5100 else
5101 return error_mark_node;
5103 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5105 if (complain & tf_error)
5106 permerror (input_location, "ISO C++ forbids using pointer to "
5107 "a function in subtraction");
5108 else
5109 return error_mark_node;
5111 if (TREE_CODE (target_type) == METHOD_TYPE)
5113 if (complain & tf_error)
5114 permerror (input_location, "ISO C++ forbids using pointer to "
5115 "a method in subtraction");
5116 else
5117 return error_mark_node;
5120 /* First do the subtraction as integers;
5121 then drop through to build the divide operator. */
5123 op0 = cp_build_binary_op (input_location,
5124 MINUS_EXPR,
5125 cp_convert (restype, op0, complain),
5126 cp_convert (restype, op1, complain),
5127 complain);
5129 /* This generates an error if op1 is a pointer to an incomplete type. */
5130 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5132 if (complain & tf_error)
5133 error ("invalid use of a pointer to an incomplete type in "
5134 "pointer arithmetic");
5135 else
5136 return error_mark_node;
5139 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5141 if (complain & tf_error)
5142 error ("arithmetic on pointer to an empty aggregate");
5143 else
5144 return error_mark_node;
5147 op1 = (TYPE_PTROB_P (ptrtype)
5148 ? size_in_bytes (target_type)
5149 : integer_one_node);
5151 /* Do the division. */
5153 result = build2 (EXACT_DIV_EXPR, restype, op0,
5154 cp_convert (restype, op1, complain));
5155 return fold_if_not_in_template (result);
5158 /* Construct and perhaps optimize a tree representation
5159 for a unary operation. CODE, a tree_code, specifies the operation
5160 and XARG is the operand. */
5162 tree
5163 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5164 tsubst_flags_t complain)
5166 tree orig_expr = xarg;
5167 tree exp;
5168 int ptrmem = 0;
5170 if (processing_template_decl)
5172 if (type_dependent_expression_p (xarg))
5173 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5175 xarg = build_non_dependent_expr (xarg);
5178 exp = NULL_TREE;
5180 /* [expr.unary.op] says:
5182 The address of an object of incomplete type can be taken.
5184 (And is just the ordinary address operator, not an overloaded
5185 "operator &".) However, if the type is a template
5186 specialization, we must complete the type at this point so that
5187 an overloaded "operator &" will be available if required. */
5188 if (code == ADDR_EXPR
5189 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5190 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5191 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5192 || (TREE_CODE (xarg) == OFFSET_REF)))
5193 /* Don't look for a function. */;
5194 else
5195 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5196 NULL_TREE, /*overload=*/NULL, complain);
5197 if (!exp && code == ADDR_EXPR)
5199 if (is_overloaded_fn (xarg))
5201 tree fn = get_first_fn (xarg);
5202 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5204 if (complain & tf_error)
5205 error (DECL_CONSTRUCTOR_P (fn)
5206 ? G_("taking address of constructor %qE")
5207 : G_("taking address of destructor %qE"),
5208 xarg);
5209 return error_mark_node;
5213 /* A pointer to member-function can be formed only by saying
5214 &X::mf. */
5215 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5216 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5218 if (TREE_CODE (xarg) != OFFSET_REF
5219 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5221 if (complain & tf_error)
5223 error ("invalid use of %qE to form a "
5224 "pointer-to-member-function", xarg);
5225 if (TREE_CODE (xarg) != OFFSET_REF)
5226 inform (input_location, " a qualified-id is required");
5228 return error_mark_node;
5230 else
5232 if (complain & tf_error)
5233 error ("parentheses around %qE cannot be used to form a"
5234 " pointer-to-member-function",
5235 xarg);
5236 else
5237 return error_mark_node;
5238 PTRMEM_OK_P (xarg) = 1;
5242 if (TREE_CODE (xarg) == OFFSET_REF)
5244 ptrmem = PTRMEM_OK_P (xarg);
5246 if (!ptrmem && !flag_ms_extensions
5247 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5249 /* A single non-static member, make sure we don't allow a
5250 pointer-to-member. */
5251 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5252 TREE_OPERAND (xarg, 0),
5253 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5254 PTRMEM_OK_P (xarg) = ptrmem;
5258 exp = cp_build_addr_expr_strict (xarg, complain);
5261 if (processing_template_decl && exp != error_mark_node)
5262 exp = build_min_non_dep (code, exp, orig_expr,
5263 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5264 if (TREE_CODE (exp) == ADDR_EXPR)
5265 PTRMEM_OK_P (exp) = ptrmem;
5266 return exp;
5269 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5270 constants, where a null value is represented by an INTEGER_CST of
5271 -1. */
5273 tree
5274 cp_truthvalue_conversion (tree expr)
5276 tree type = TREE_TYPE (expr);
5277 if (TYPE_PTRDATAMEM_P (type)
5278 /* Avoid ICE on invalid use of non-static member function. */
5279 || TREE_CODE (expr) == FUNCTION_DECL)
5280 return build_binary_op (EXPR_LOCATION (expr),
5281 NE_EXPR, expr, nullptr_node, 1);
5282 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5284 /* With -Wzero-as-null-pointer-constant do not warn for an
5285 'if (p)' or a 'while (!p)', where p is a pointer. */
5286 tree ret;
5287 ++c_inhibit_evaluation_warnings;
5288 ret = c_common_truthvalue_conversion (input_location, expr);
5289 --c_inhibit_evaluation_warnings;
5290 return ret;
5292 else
5293 return c_common_truthvalue_conversion (input_location, expr);
5296 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5298 tree
5299 condition_conversion (tree expr)
5301 tree t;
5302 if (processing_template_decl)
5303 return expr;
5304 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5305 tf_warning_or_error, LOOKUP_NORMAL);
5306 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5307 return t;
5310 /* Returns the address of T. This function will fold away
5311 ADDR_EXPR of INDIRECT_REF. */
5313 tree
5314 build_address (tree t)
5316 if (error_operand_p (t) || !cxx_mark_addressable (t))
5317 return error_mark_node;
5318 t = build_fold_addr_expr (t);
5319 if (TREE_CODE (t) != ADDR_EXPR)
5320 t = rvalue (t);
5321 return t;
5324 /* Returns the address of T with type TYPE. */
5326 tree
5327 build_typed_address (tree t, tree type)
5329 if (error_operand_p (t) || !cxx_mark_addressable (t))
5330 return error_mark_node;
5331 t = build_fold_addr_expr_with_type (t, type);
5332 if (TREE_CODE (t) != ADDR_EXPR)
5333 t = rvalue (t);
5334 return t;
5337 /* Return a NOP_EXPR converting EXPR to TYPE. */
5339 tree
5340 build_nop (tree type, tree expr)
5342 if (type == error_mark_node || error_operand_p (expr))
5343 return expr;
5344 return build1 (NOP_EXPR, type, expr);
5347 /* Take the address of ARG, whatever that means under C++ semantics.
5348 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5349 and class rvalues as well.
5351 Nothing should call this function directly; instead, callers should use
5352 cp_build_addr_expr or cp_build_addr_expr_strict. */
5354 static tree
5355 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5357 tree argtype;
5358 tree val;
5360 if (!arg || error_operand_p (arg))
5361 return error_mark_node;
5363 arg = mark_lvalue_use (arg);
5364 argtype = lvalue_type (arg);
5366 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5368 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5369 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5371 /* They're trying to take the address of a unique non-static
5372 member function. This is ill-formed (except in MS-land),
5373 but let's try to DTRT.
5374 Note: We only handle unique functions here because we don't
5375 want to complain if there's a static overload; non-unique
5376 cases will be handled by instantiate_type. But we need to
5377 handle this case here to allow casts on the resulting PMF.
5378 We could defer this in non-MS mode, but it's easier to give
5379 a useful error here. */
5381 /* Inside constant member functions, the `this' pointer
5382 contains an extra const qualifier. TYPE_MAIN_VARIANT
5383 is used here to remove this const from the diagnostics
5384 and the created OFFSET_REF. */
5385 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5386 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5387 mark_used (fn);
5389 if (! flag_ms_extensions)
5391 tree name = DECL_NAME (fn);
5392 if (!(complain & tf_error))
5393 return error_mark_node;
5394 else if (current_class_type
5395 && TREE_OPERAND (arg, 0) == current_class_ref)
5396 /* An expression like &memfn. */
5397 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5398 " or parenthesized non-static member function to form"
5399 " a pointer to member function. Say %<&%T::%D%>",
5400 base, name);
5401 else
5402 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5403 " function to form a pointer to member function."
5404 " Say %<&%T::%D%>",
5405 base, name);
5407 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5410 /* Uninstantiated types are all functions. Taking the
5411 address of a function is a no-op, so just return the
5412 argument. */
5413 if (type_unknown_p (arg))
5414 return build1 (ADDR_EXPR, unknown_type_node, arg);
5416 if (TREE_CODE (arg) == OFFSET_REF)
5417 /* We want a pointer to member; bypass all the code for actually taking
5418 the address of something. */
5419 goto offset_ref;
5421 /* Anything not already handled and not a true memory reference
5422 is an error. */
5423 if (TREE_CODE (argtype) != FUNCTION_TYPE
5424 && TREE_CODE (argtype) != METHOD_TYPE)
5426 cp_lvalue_kind kind = lvalue_kind (arg);
5427 if (kind == clk_none)
5429 if (complain & tf_error)
5430 lvalue_error (input_location, lv_addressof);
5431 return error_mark_node;
5433 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5435 if (!(complain & tf_error))
5436 return error_mark_node;
5437 if (kind & clk_class)
5438 /* Make this a permerror because we used to accept it. */
5439 permerror (input_location, "taking address of temporary");
5440 else
5441 error ("taking address of xvalue (rvalue reference)");
5445 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5447 tree type = build_pointer_type (TREE_TYPE (argtype));
5448 arg = build1 (CONVERT_EXPR, type, arg);
5449 return arg;
5451 else if (pedantic && DECL_MAIN_P (arg))
5453 /* ARM $3.4 */
5454 /* Apparently a lot of autoconf scripts for C++ packages do this,
5455 so only complain if -Wpedantic. */
5456 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5457 pedwarn (input_location, OPT_Wpedantic,
5458 "ISO C++ forbids taking address of function %<::main%>");
5459 else if (flag_pedantic_errors)
5460 return error_mark_node;
5463 /* Let &* cancel out to simplify resulting code. */
5464 if (INDIRECT_REF_P (arg))
5466 /* We don't need to have `current_class_ptr' wrapped in a
5467 NON_LVALUE_EXPR node. */
5468 if (arg == current_class_ref)
5469 return current_class_ptr;
5471 arg = TREE_OPERAND (arg, 0);
5472 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5474 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5475 arg = build1 (CONVERT_EXPR, type, arg);
5477 else
5478 /* Don't let this be an lvalue. */
5479 arg = rvalue (arg);
5480 return arg;
5483 /* ??? Cope with user tricks that amount to offsetof. */
5484 if (TREE_CODE (argtype) != FUNCTION_TYPE
5485 && TREE_CODE (argtype) != METHOD_TYPE
5486 && argtype != unknown_type_node
5487 && (val = get_base_address (arg))
5488 && COMPLETE_TYPE_P (TREE_TYPE (val))
5489 && INDIRECT_REF_P (val)
5490 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5492 tree type = build_pointer_type (argtype);
5493 return fold_convert (type, fold_offsetof_1 (arg));
5496 /* Handle complex lvalues (when permitted)
5497 by reduction to simpler cases. */
5498 val = unary_complex_lvalue (ADDR_EXPR, arg);
5499 if (val != 0)
5500 return val;
5502 switch (TREE_CODE (arg))
5504 CASE_CONVERT:
5505 case FLOAT_EXPR:
5506 case FIX_TRUNC_EXPR:
5507 /* Even if we're not being pedantic, we cannot allow this
5508 extension when we're instantiating in a SFINAE
5509 context. */
5510 if (! lvalue_p (arg) && complain == tf_none)
5512 if (complain & tf_error)
5513 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5514 else
5515 return error_mark_node;
5517 break;
5519 case COMPONENT_REF:
5520 if (BASELINK_P (TREE_OPERAND (arg, 1)))
5521 break;
5523 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5525 if (complain & tf_error)
5526 error ("attempt to take address of bit-field structure member %qD",
5527 TREE_OPERAND (arg, 1));
5528 return error_mark_node;
5530 /* Fall through. */
5532 case ARRAY_REF:
5533 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
5535 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg)))
5537 if (complain & tf_error)
5538 error ("attempt to take address of scalar with reverse "
5539 "storage order");
5540 return error_mark_node;
5543 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
5544 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
5546 if (complain & tf_warning)
5547 warning (OPT_Wscalar_storage_order, "address of array with "
5548 "reverse scalar storage order requested");
5551 break;
5553 case BASELINK:
5554 arg = BASELINK_FUNCTIONS (arg);
5555 /* Fall through. */
5557 case OVERLOAD:
5558 arg = OVL_CURRENT (arg);
5559 break;
5561 case OFFSET_REF:
5562 offset_ref:
5563 /* Turn a reference to a non-static data member into a
5564 pointer-to-member. */
5566 tree type;
5567 tree t;
5569 gcc_assert (PTRMEM_OK_P (arg));
5571 t = TREE_OPERAND (arg, 1);
5572 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5574 if (complain & tf_error)
5575 error ("cannot create pointer to reference member %qD", t);
5576 return error_mark_node;
5579 type = build_ptrmem_type (context_for_name_lookup (t),
5580 TREE_TYPE (t));
5581 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5582 return t;
5585 default:
5586 break;
5589 if (argtype != error_mark_node)
5591 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (argtype)
5592 && (flag_iso || warn_vla > 0))
5594 if (complain & tf_warning_or_error)
5595 pedwarn (input_location, OPT_Wvla,
5596 "taking address of array of runtime bound");
5597 else
5598 return error_mark_node;
5600 argtype = build_pointer_type (argtype);
5603 /* In a template, we are processing a non-dependent expression
5604 so we can just form an ADDR_EXPR with the correct type. */
5605 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5607 val = build_address (arg);
5608 if (TREE_CODE (arg) == OFFSET_REF)
5609 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5611 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5613 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5615 /* We can only get here with a single static member
5616 function. */
5617 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5618 && DECL_STATIC_FUNCTION_P (fn));
5619 mark_used (fn);
5620 val = build_address (fn);
5621 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5622 /* Do not lose object's side effects. */
5623 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5624 TREE_OPERAND (arg, 0), val);
5626 else
5628 tree object = TREE_OPERAND (arg, 0);
5629 tree field = TREE_OPERAND (arg, 1);
5630 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5631 (TREE_TYPE (object), decl_type_context (field)));
5632 val = build_address (arg);
5635 if (TYPE_PTR_P (argtype)
5636 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5638 build_ptrmemfunc_type (argtype);
5639 val = build_ptrmemfunc (argtype, val, 0,
5640 /*c_cast_p=*/false,
5641 complain);
5644 return val;
5647 /* Take the address of ARG if it has one, even if it's an rvalue. */
5649 tree
5650 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5652 return cp_build_addr_expr_1 (arg, 0, complain);
5655 /* Take the address of ARG, but only if it's an lvalue. */
5657 tree
5658 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5660 return cp_build_addr_expr_1 (arg, 1, complain);
5663 /* C++: Must handle pointers to members.
5665 Perhaps type instantiation should be extended to handle conversion
5666 from aggregates to types we don't yet know we want? (Or are those
5667 cases typically errors which should be reported?)
5669 NOCONVERT nonzero suppresses the default promotions
5670 (such as from short to int). */
5672 tree
5673 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5674 tsubst_flags_t complain)
5676 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5677 tree arg = xarg;
5678 tree argtype = 0;
5679 const char *errstring = NULL;
5680 tree val;
5681 const char *invalid_op_diag;
5683 if (!arg || error_operand_p (arg))
5684 return error_mark_node;
5686 if ((invalid_op_diag
5687 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5688 ? CONVERT_EXPR
5689 : code),
5690 TREE_TYPE (xarg))))
5692 if (complain & tf_error)
5693 error (invalid_op_diag);
5694 return error_mark_node;
5697 switch (code)
5699 case UNARY_PLUS_EXPR:
5700 case NEGATE_EXPR:
5702 int flags = WANT_ARITH | WANT_ENUM;
5703 /* Unary plus (but not unary minus) is allowed on pointers. */
5704 if (code == UNARY_PLUS_EXPR)
5705 flags |= WANT_POINTER;
5706 arg = build_expr_type_conversion (flags, arg, true);
5707 if (!arg)
5708 errstring = (code == NEGATE_EXPR
5709 ? _("wrong type argument to unary minus")
5710 : _("wrong type argument to unary plus"));
5711 else
5713 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5714 arg = cp_perform_integral_promotions (arg, complain);
5716 /* Make sure the result is not an lvalue: a unary plus or minus
5717 expression is always a rvalue. */
5718 arg = rvalue (arg);
5721 break;
5723 case BIT_NOT_EXPR:
5724 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5726 code = CONJ_EXPR;
5727 if (!noconvert)
5729 arg = cp_default_conversion (arg, complain);
5730 if (arg == error_mark_node)
5731 return error_mark_node;
5734 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5735 | WANT_VECTOR_OR_COMPLEX,
5736 arg, true)))
5737 errstring = _("wrong type argument to bit-complement");
5738 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5739 arg = cp_perform_integral_promotions (arg, complain);
5740 break;
5742 case ABS_EXPR:
5743 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5744 errstring = _("wrong type argument to abs");
5745 else if (!noconvert)
5747 arg = cp_default_conversion (arg, complain);
5748 if (arg == error_mark_node)
5749 return error_mark_node;
5751 break;
5753 case CONJ_EXPR:
5754 /* Conjugating a real value is a no-op, but allow it anyway. */
5755 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5756 errstring = _("wrong type argument to conjugation");
5757 else if (!noconvert)
5759 arg = cp_default_conversion (arg, complain);
5760 if (arg == error_mark_node)
5761 return error_mark_node;
5763 break;
5765 case TRUTH_NOT_EXPR:
5766 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5767 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5768 build_zero_cst (TREE_TYPE (arg)), complain);
5769 arg = perform_implicit_conversion (boolean_type_node, arg,
5770 complain);
5771 val = invert_truthvalue_loc (input_location, arg);
5772 if (arg != error_mark_node)
5773 return val;
5774 errstring = _("in argument to unary !");
5775 break;
5777 case NOP_EXPR:
5778 break;
5780 case REALPART_EXPR:
5781 case IMAGPART_EXPR:
5782 arg = build_real_imag_expr (input_location, code, arg);
5783 if (arg == error_mark_node)
5784 return arg;
5785 else
5786 return fold_if_not_in_template (arg);
5788 case PREINCREMENT_EXPR:
5789 case POSTINCREMENT_EXPR:
5790 case PREDECREMENT_EXPR:
5791 case POSTDECREMENT_EXPR:
5792 /* Handle complex lvalues (when permitted)
5793 by reduction to simpler cases. */
5795 val = unary_complex_lvalue (code, arg);
5796 if (val != 0)
5797 return val;
5799 arg = mark_lvalue_use (arg);
5801 /* Increment or decrement the real part of the value,
5802 and don't change the imaginary part. */
5803 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5805 tree real, imag;
5807 arg = stabilize_reference (arg);
5808 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5809 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5810 real = cp_build_unary_op (code, real, 1, complain);
5811 if (real == error_mark_node || imag == error_mark_node)
5812 return error_mark_node;
5813 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5814 real, imag);
5817 /* Report invalid types. */
5819 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5820 arg, true)))
5822 if (code == PREINCREMENT_EXPR)
5823 errstring = _("no pre-increment operator for type");
5824 else if (code == POSTINCREMENT_EXPR)
5825 errstring = _("no post-increment operator for type");
5826 else if (code == PREDECREMENT_EXPR)
5827 errstring = _("no pre-decrement operator for type");
5828 else
5829 errstring = _("no post-decrement operator for type");
5830 break;
5832 else if (arg == error_mark_node)
5833 return error_mark_node;
5835 /* Report something read-only. */
5837 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5838 || TREE_READONLY (arg))
5840 if (complain & tf_error)
5841 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5842 || code == POSTINCREMENT_EXPR)
5843 ? lv_increment : lv_decrement));
5844 else
5845 return error_mark_node;
5849 tree inc;
5850 tree declared_type = unlowered_expr_type (arg);
5852 argtype = TREE_TYPE (arg);
5854 /* ARM $5.2.5 last annotation says this should be forbidden. */
5855 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5857 if (complain & tf_error)
5858 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5859 ? G_("ISO C++ forbids incrementing an enum")
5860 : G_("ISO C++ forbids decrementing an enum"));
5861 else
5862 return error_mark_node;
5865 /* Compute the increment. */
5867 if (TYPE_PTR_P (argtype))
5869 tree type = complete_type (TREE_TYPE (argtype));
5871 if (!COMPLETE_OR_VOID_TYPE_P (type))
5873 if (complain & tf_error)
5874 error (((code == PREINCREMENT_EXPR
5875 || code == POSTINCREMENT_EXPR))
5876 ? G_("cannot increment a pointer to incomplete type %qT")
5877 : G_("cannot decrement a pointer to incomplete type %qT"),
5878 TREE_TYPE (argtype));
5879 else
5880 return error_mark_node;
5882 else if (!TYPE_PTROB_P (argtype))
5884 if (complain & tf_error)
5885 pedwarn (input_location, OPT_Wpointer_arith,
5886 (code == PREINCREMENT_EXPR
5887 || code == POSTINCREMENT_EXPR)
5888 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5889 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5890 argtype);
5891 else
5892 return error_mark_node;
5895 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5897 else
5898 inc = VECTOR_TYPE_P (argtype)
5899 ? build_one_cst (argtype)
5900 : integer_one_node;
5902 inc = cp_convert (argtype, inc, complain);
5904 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5905 need to ask Objective-C to build the increment or decrement
5906 expression for it. */
5907 if (objc_is_property_ref (arg))
5908 return objc_build_incr_expr_for_property_ref (input_location, code,
5909 arg, inc);
5911 /* Complain about anything else that is not a true lvalue. */
5912 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5913 || code == POSTINCREMENT_EXPR)
5914 ? lv_increment : lv_decrement),
5915 complain))
5916 return error_mark_node;
5918 /* Forbid using -- on `bool'. */
5919 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5921 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5923 if (complain & tf_error)
5924 error ("invalid use of Boolean expression as operand "
5925 "to %<operator--%>");
5926 return error_mark_node;
5928 val = boolean_increment (code, arg);
5930 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5931 /* An rvalue has no cv-qualifiers. */
5932 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5933 else
5934 val = build2 (code, TREE_TYPE (arg), arg, inc);
5936 TREE_SIDE_EFFECTS (val) = 1;
5937 return val;
5940 case ADDR_EXPR:
5941 /* Note that this operation never does default_conversion
5942 regardless of NOCONVERT. */
5943 return cp_build_addr_expr (arg, complain);
5945 default:
5946 break;
5949 if (!errstring)
5951 if (argtype == 0)
5952 argtype = TREE_TYPE (arg);
5953 return fold_if_not_in_template (build1 (code, argtype, arg));
5956 if (complain & tf_error)
5957 error ("%s", errstring);
5958 return error_mark_node;
5961 /* Hook for the c-common bits that build a unary op. */
5962 tree
5963 build_unary_op (location_t /*location*/,
5964 enum tree_code code, tree xarg, int noconvert)
5966 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5969 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5970 for certain kinds of expressions which are not really lvalues
5971 but which we can accept as lvalues.
5973 If ARG is not a kind of expression we can handle, return
5974 NULL_TREE. */
5976 tree
5977 unary_complex_lvalue (enum tree_code code, tree arg)
5979 /* Inside a template, making these kinds of adjustments is
5980 pointless; we are only concerned with the type of the
5981 expression. */
5982 if (processing_template_decl)
5983 return NULL_TREE;
5985 /* Handle (a, b) used as an "lvalue". */
5986 if (TREE_CODE (arg) == COMPOUND_EXPR)
5988 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5989 tf_warning_or_error);
5990 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5991 TREE_OPERAND (arg, 0), real_result);
5994 /* Handle (a ? b : c) used as an "lvalue". */
5995 if (TREE_CODE (arg) == COND_EXPR
5996 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5997 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5999 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6000 if (TREE_CODE (arg) == MODIFY_EXPR
6001 || TREE_CODE (arg) == PREINCREMENT_EXPR
6002 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6004 tree lvalue = TREE_OPERAND (arg, 0);
6005 if (TREE_SIDE_EFFECTS (lvalue))
6007 lvalue = stabilize_reference (lvalue);
6008 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6009 lvalue, TREE_OPERAND (arg, 1));
6011 return unary_complex_lvalue
6012 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
6015 if (code != ADDR_EXPR)
6016 return NULL_TREE;
6018 /* Handle (a = b) used as an "lvalue" for `&'. */
6019 if (TREE_CODE (arg) == MODIFY_EXPR
6020 || TREE_CODE (arg) == INIT_EXPR)
6022 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
6023 tf_warning_or_error);
6024 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6025 arg, real_result);
6026 TREE_NO_WARNING (arg) = 1;
6027 return arg;
6030 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6031 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6032 || TREE_CODE (arg) == OFFSET_REF)
6033 return NULL_TREE;
6035 /* We permit compiler to make function calls returning
6036 objects of aggregate type look like lvalues. */
6038 tree targ = arg;
6040 if (TREE_CODE (targ) == SAVE_EXPR)
6041 targ = TREE_OPERAND (targ, 0);
6043 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6045 if (TREE_CODE (arg) == SAVE_EXPR)
6046 targ = arg;
6047 else
6048 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6049 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6052 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6053 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6054 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6057 /* Don't let anything else be handled specially. */
6058 return NULL_TREE;
6061 /* Mark EXP saying that we need to be able to take the
6062 address of it; it should not be allocated in a register.
6063 Value is true if successful.
6065 C++: we do not allow `current_class_ptr' to be addressable. */
6067 bool
6068 cxx_mark_addressable (tree exp)
6070 tree x = exp;
6072 while (1)
6073 switch (TREE_CODE (x))
6075 case ADDR_EXPR:
6076 case COMPONENT_REF:
6077 case ARRAY_REF:
6078 case REALPART_EXPR:
6079 case IMAGPART_EXPR:
6080 x = TREE_OPERAND (x, 0);
6081 break;
6083 case PARM_DECL:
6084 if (x == current_class_ptr)
6086 error ("cannot take the address of %<this%>, which is an rvalue expression");
6087 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6088 return true;
6090 /* Fall through. */
6092 case VAR_DECL:
6093 /* Caller should not be trying to mark initialized
6094 constant fields addressable. */
6095 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6096 || DECL_IN_AGGR_P (x) == 0
6097 || TREE_STATIC (x)
6098 || DECL_EXTERNAL (x));
6099 /* Fall through. */
6101 case RESULT_DECL:
6102 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6103 && !DECL_ARTIFICIAL (x))
6105 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6107 error
6108 ("address of explicit register variable %qD requested", x);
6109 return false;
6111 else if (extra_warnings)
6112 warning
6113 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6115 TREE_ADDRESSABLE (x) = 1;
6116 return true;
6118 case CONST_DECL:
6119 case FUNCTION_DECL:
6120 TREE_ADDRESSABLE (x) = 1;
6121 return true;
6123 case CONSTRUCTOR:
6124 TREE_ADDRESSABLE (x) = 1;
6125 return true;
6127 case TARGET_EXPR:
6128 TREE_ADDRESSABLE (x) = 1;
6129 cxx_mark_addressable (TREE_OPERAND (x, 0));
6130 return true;
6132 default:
6133 return true;
6137 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6139 tree
6140 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6141 tsubst_flags_t complain)
6143 tree orig_ifexp = ifexp;
6144 tree orig_op1 = op1;
6145 tree orig_op2 = op2;
6146 tree expr;
6148 if (processing_template_decl)
6150 /* The standard says that the expression is type-dependent if
6151 IFEXP is type-dependent, even though the eventual type of the
6152 expression doesn't dependent on IFEXP. */
6153 if (type_dependent_expression_p (ifexp)
6154 /* As a GNU extension, the middle operand may be omitted. */
6155 || (op1 && type_dependent_expression_p (op1))
6156 || type_dependent_expression_p (op2))
6157 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6158 ifexp = build_non_dependent_expr (ifexp);
6159 if (op1)
6160 op1 = build_non_dependent_expr (op1);
6161 op2 = build_non_dependent_expr (op2);
6164 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6165 if (processing_template_decl && expr != error_mark_node
6166 && TREE_CODE (expr) != VEC_COND_EXPR)
6168 tree min = build_min_non_dep (COND_EXPR, expr,
6169 orig_ifexp, orig_op1, orig_op2);
6170 /* In C++11, remember that the result is an lvalue or xvalue.
6171 In C++98, lvalue_kind can just assume lvalue in a template. */
6172 if (cxx_dialect >= cxx11
6173 && lvalue_or_rvalue_with_address_p (expr)
6174 && !lvalue_or_rvalue_with_address_p (min))
6175 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6176 !real_lvalue_p (expr));
6177 expr = convert_from_reference (min);
6179 return expr;
6182 /* Given a list of expressions, return a compound expression
6183 that performs them all and returns the value of the last of them. */
6185 tree
6186 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6187 tsubst_flags_t complain)
6189 tree expr = TREE_VALUE (list);
6191 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6192 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6194 if (complain & tf_error)
6195 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6196 "list-initializer for non-class type must not "
6197 "be parenthesized");
6198 else
6199 return error_mark_node;
6202 if (TREE_CHAIN (list))
6204 if (complain & tf_error)
6205 switch (exp)
6207 case ELK_INIT:
6208 permerror (input_location, "expression list treated as compound "
6209 "expression in initializer");
6210 break;
6211 case ELK_MEM_INIT:
6212 permerror (input_location, "expression list treated as compound "
6213 "expression in mem-initializer");
6214 break;
6215 case ELK_FUNC_CAST:
6216 permerror (input_location, "expression list treated as compound "
6217 "expression in functional cast");
6218 break;
6219 default:
6220 gcc_unreachable ();
6222 else
6223 return error_mark_node;
6225 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6226 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6227 expr, TREE_VALUE (list), complain);
6230 return expr;
6233 /* Like build_x_compound_expr_from_list, but using a VEC. */
6235 tree
6236 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6237 tsubst_flags_t complain)
6239 if (vec_safe_is_empty (vec))
6240 return NULL_TREE;
6241 else if (vec->length () == 1)
6242 return (*vec)[0];
6243 else
6245 tree expr;
6246 unsigned int ix;
6247 tree t;
6249 if (msg != NULL)
6251 if (complain & tf_error)
6252 permerror (input_location,
6253 "%s expression list treated as compound expression",
6254 msg);
6255 else
6256 return error_mark_node;
6259 expr = (*vec)[0];
6260 for (ix = 1; vec->iterate (ix, &t); ++ix)
6261 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6262 t, complain);
6264 return expr;
6268 /* Handle overloading of the ',' operator when needed. */
6270 tree
6271 build_x_compound_expr (location_t loc, tree op1, tree op2,
6272 tsubst_flags_t complain)
6274 tree result;
6275 tree orig_op1 = op1;
6276 tree orig_op2 = op2;
6278 if (processing_template_decl)
6280 if (type_dependent_expression_p (op1)
6281 || type_dependent_expression_p (op2))
6282 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6283 op1 = build_non_dependent_expr (op1);
6284 op2 = build_non_dependent_expr (op2);
6287 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6288 NULL_TREE, /*overload=*/NULL, complain);
6289 if (!result)
6290 result = cp_build_compound_expr (op1, op2, complain);
6292 if (processing_template_decl && result != error_mark_node)
6293 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6295 return result;
6298 /* Like cp_build_compound_expr, but for the c-common bits. */
6300 tree
6301 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6303 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6306 /* Build a compound expression. */
6308 tree
6309 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6311 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6313 if (lhs == error_mark_node || rhs == error_mark_node)
6314 return error_mark_node;
6316 if (flag_cilkplus
6317 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6318 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6320 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6321 : EXPR_LOCATION (rhs));
6322 error_at (loc,
6323 "spawned function call cannot be part of a comma expression");
6324 return error_mark_node;
6327 if (TREE_CODE (rhs) == TARGET_EXPR)
6329 /* If the rhs is a TARGET_EXPR, then build the compound
6330 expression inside the target_expr's initializer. This
6331 helps the compiler to eliminate unnecessary temporaries. */
6332 tree init = TREE_OPERAND (rhs, 1);
6334 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6335 TREE_OPERAND (rhs, 1) = init;
6337 return rhs;
6340 if (type_unknown_p (rhs))
6342 if (complain & tf_error)
6343 error ("no context to resolve type of %qE", rhs);
6344 return error_mark_node;
6347 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6350 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6351 casts away constness. CAST gives the type of cast. Returns true
6352 if the cast is ill-formed, false if it is well-formed.
6354 ??? This function warns for casting away any qualifier not just
6355 const. We would like to specify exactly what qualifiers are casted
6356 away.
6359 static bool
6360 check_for_casting_away_constness (tree src_type, tree dest_type,
6361 enum tree_code cast, tsubst_flags_t complain)
6363 /* C-style casts are allowed to cast away constness. With
6364 WARN_CAST_QUAL, we still want to issue a warning. */
6365 if (cast == CAST_EXPR && !warn_cast_qual)
6366 return false;
6368 if (!casts_away_constness (src_type, dest_type, complain))
6369 return false;
6371 switch (cast)
6373 case CAST_EXPR:
6374 if (complain & tf_warning)
6375 warning (OPT_Wcast_qual,
6376 "cast from type %qT to type %qT casts away qualifiers",
6377 src_type, dest_type);
6378 return false;
6380 case STATIC_CAST_EXPR:
6381 if (complain & tf_error)
6382 error ("static_cast from type %qT to type %qT casts away qualifiers",
6383 src_type, dest_type);
6384 return true;
6386 case REINTERPRET_CAST_EXPR:
6387 if (complain & tf_error)
6388 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6389 src_type, dest_type);
6390 return true;
6392 default:
6393 gcc_unreachable();
6398 Warns if the cast from expression EXPR to type TYPE is useless.
6400 void
6401 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6403 if (warn_useless_cast
6404 && complain & tf_warning)
6406 /* In C++14 mode, this interacts badly with force_paren_expr. And it
6407 isn't necessary in any mode, because the code below handles
6408 glvalues properly. For 4.9, just skip it in C++14 mode. */
6409 if (cxx_dialect < cxx14 && REFERENCE_REF_P (expr))
6410 expr = TREE_OPERAND (expr, 0);
6412 if ((TREE_CODE (type) == REFERENCE_TYPE
6413 && (TYPE_REF_IS_RVALUE (type)
6414 ? xvalue_p (expr) : real_lvalue_p (expr))
6415 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6416 || same_type_p (TREE_TYPE (expr), type))
6417 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6421 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6422 (another pointer-to-member type in the same hierarchy) and return
6423 the converted expression. If ALLOW_INVERSE_P is permitted, a
6424 pointer-to-derived may be converted to pointer-to-base; otherwise,
6425 only the other direction is permitted. If C_CAST_P is true, this
6426 conversion is taking place as part of a C-style cast. */
6428 tree
6429 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6430 bool c_cast_p, tsubst_flags_t complain)
6432 if (TYPE_PTRDATAMEM_P (type))
6434 tree delta;
6436 if (TREE_CODE (expr) == PTRMEM_CST)
6437 expr = cplus_expand_constant (expr);
6438 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6439 TYPE_PTRMEM_CLASS_TYPE (type),
6440 allow_inverse_p,
6441 c_cast_p, complain);
6442 if (delta == error_mark_node)
6443 return error_mark_node;
6445 if (!integer_zerop (delta))
6447 tree cond, op1, op2;
6449 cond = cp_build_binary_op (input_location,
6450 EQ_EXPR,
6451 expr,
6452 build_int_cst (TREE_TYPE (expr), -1),
6453 complain);
6454 op1 = build_nop (ptrdiff_type_node, expr);
6455 op2 = cp_build_binary_op (input_location,
6456 PLUS_EXPR, op1, delta,
6457 complain);
6459 expr = fold_build3_loc (input_location,
6460 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6464 return build_nop (type, expr);
6466 else
6467 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6468 allow_inverse_p, c_cast_p, complain);
6471 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6472 this static_cast is being attempted as one of the possible casts
6473 allowed by a C-style cast. (In that case, accessibility of base
6474 classes is not considered, and it is OK to cast away
6475 constness.) Return the result of the cast. *VALID_P is set to
6476 indicate whether or not the cast was valid. */
6478 static tree
6479 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6480 bool *valid_p, tsubst_flags_t complain)
6482 tree intype;
6483 tree result;
6484 cp_lvalue_kind clk;
6486 /* Assume the cast is valid. */
6487 *valid_p = true;
6489 intype = unlowered_expr_type (expr);
6491 /* Save casted types in the function's used types hash table. */
6492 used_types_insert (type);
6494 /* [expr.static.cast]
6496 An lvalue of type "cv1 B", where B is a class type, can be cast
6497 to type "reference to cv2 D", where D is a class derived (clause
6498 _class.derived_) from B, if a valid standard conversion from
6499 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6500 same cv-qualification as, or greater cv-qualification than, cv1,
6501 and B is not a virtual base class of D. */
6502 /* We check this case before checking the validity of "TYPE t =
6503 EXPR;" below because for this case:
6505 struct B {};
6506 struct D : public B { D(const B&); };
6507 extern B& b;
6508 void f() { static_cast<const D&>(b); }
6510 we want to avoid constructing a new D. The standard is not
6511 completely clear about this issue, but our interpretation is
6512 consistent with other compilers. */
6513 if (TREE_CODE (type) == REFERENCE_TYPE
6514 && CLASS_TYPE_P (TREE_TYPE (type))
6515 && CLASS_TYPE_P (intype)
6516 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6517 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6518 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6519 build_pointer_type (TYPE_MAIN_VARIANT
6520 (TREE_TYPE (type))),
6521 complain)
6522 && (c_cast_p
6523 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6525 tree base;
6527 /* There is a standard conversion from "D*" to "B*" even if "B"
6528 is ambiguous or inaccessible. If this is really a
6529 static_cast, then we check both for inaccessibility and
6530 ambiguity. However, if this is a static_cast being performed
6531 because the user wrote a C-style cast, then accessibility is
6532 not considered. */
6533 base = lookup_base (TREE_TYPE (type), intype,
6534 c_cast_p ? ba_unique : ba_check,
6535 NULL, complain);
6537 /* Convert from "B*" to "D*". This function will check that "B"
6538 is not a virtual base of "D". */
6539 expr = build_base_path (MINUS_EXPR, build_address (expr),
6540 base, /*nonnull=*/false, complain);
6541 /* Convert the pointer to a reference -- but then remember that
6542 there are no expressions with reference type in C++.
6544 We call rvalue so that there's an actual tree code
6545 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6546 is a variable with the same type, the conversion would get folded
6547 away, leaving just the variable and causing lvalue_kind to give
6548 the wrong answer. */
6549 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6552 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6553 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6554 if (TREE_CODE (type) == REFERENCE_TYPE
6555 && TYPE_REF_IS_RVALUE (type)
6556 && (clk = real_lvalue_p (expr))
6557 && reference_related_p (TREE_TYPE (type), intype)
6558 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6560 if (clk == clk_ordinary)
6562 /* Handle the (non-bit-field) lvalue case here by casting to
6563 lvalue reference and then changing it to an rvalue reference.
6564 Casting an xvalue to rvalue reference will be handled by the
6565 main code path. */
6566 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6567 result = (perform_direct_initialization_if_possible
6568 (lref, expr, c_cast_p, complain));
6569 result = cp_fold_convert (type, result);
6570 /* Make sure we don't fold back down to a named rvalue reference,
6571 because that would be an lvalue. */
6572 if (DECL_P (result))
6573 result = build1 (NON_LVALUE_EXPR, type, result);
6574 return convert_from_reference (result);
6576 else
6577 /* For a bit-field or packed field, bind to a temporary. */
6578 expr = rvalue (expr);
6581 /* Resolve overloaded address here rather than once in
6582 implicit_conversion and again in the inverse code below. */
6583 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6585 expr = instantiate_type (type, expr, complain);
6586 intype = TREE_TYPE (expr);
6589 /* [expr.static.cast]
6591 Any expression can be explicitly converted to type cv void. */
6592 if (VOID_TYPE_P (type))
6593 return convert_to_void (expr, ICV_CAST, complain);
6595 /* [class.abstract]
6596 An abstract class shall not be used ... as the type of an explicit
6597 conversion. */
6598 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6599 return error_mark_node;
6601 /* [expr.static.cast]
6603 An expression e can be explicitly converted to a type T using a
6604 static_cast of the form static_cast<T>(e) if the declaration T
6605 t(e);" is well-formed, for some invented temporary variable
6606 t. */
6607 result = perform_direct_initialization_if_possible (type, expr,
6608 c_cast_p, complain);
6609 if (result)
6611 result = convert_from_reference (result);
6613 /* [expr.static.cast]
6615 If T is a reference type, the result is an lvalue; otherwise,
6616 the result is an rvalue. */
6617 if (TREE_CODE (type) != REFERENCE_TYPE)
6618 result = rvalue (result);
6619 return result;
6622 /* [expr.static.cast]
6624 The inverse of any standard conversion sequence (clause _conv_),
6625 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6626 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6627 (_conv.bool_) conversions, can be performed explicitly using
6628 static_cast subject to the restriction that the explicit
6629 conversion does not cast away constness (_expr.const.cast_), and
6630 the following additional rules for specific cases: */
6631 /* For reference, the conversions not excluded are: integral
6632 promotions, floating point promotion, integral conversions,
6633 floating point conversions, floating-integral conversions,
6634 pointer conversions, and pointer to member conversions. */
6635 /* DR 128
6637 A value of integral _or enumeration_ type can be explicitly
6638 converted to an enumeration type. */
6639 /* The effect of all that is that any conversion between any two
6640 types which are integral, floating, or enumeration types can be
6641 performed. */
6642 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6643 || SCALAR_FLOAT_TYPE_P (type))
6644 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6645 || SCALAR_FLOAT_TYPE_P (intype)))
6646 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6648 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6649 && CLASS_TYPE_P (TREE_TYPE (type))
6650 && CLASS_TYPE_P (TREE_TYPE (intype))
6651 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6652 (TREE_TYPE (intype))),
6653 build_pointer_type (TYPE_MAIN_VARIANT
6654 (TREE_TYPE (type))),
6655 complain))
6657 tree base;
6659 if (!c_cast_p
6660 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6661 complain))
6662 return error_mark_node;
6663 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6664 c_cast_p ? ba_unique : ba_check,
6665 NULL, complain);
6666 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6667 complain);
6668 return cp_fold_convert(type, expr);
6671 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6672 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6674 tree c1;
6675 tree c2;
6676 tree t1;
6677 tree t2;
6679 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6680 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6682 if (TYPE_PTRDATAMEM_P (type))
6684 t1 = (build_ptrmem_type
6685 (c1,
6686 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6687 t2 = (build_ptrmem_type
6688 (c2,
6689 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6691 else
6693 t1 = intype;
6694 t2 = type;
6696 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6698 if (!c_cast_p
6699 && check_for_casting_away_constness (intype, type,
6700 STATIC_CAST_EXPR,
6701 complain))
6702 return error_mark_node;
6703 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6704 c_cast_p, complain);
6708 /* [expr.static.cast]
6710 An rvalue of type "pointer to cv void" can be explicitly
6711 converted to a pointer to object type. A value of type pointer
6712 to object converted to "pointer to cv void" and back to the
6713 original pointer type will have its original value. */
6714 if (TYPE_PTR_P (intype)
6715 && VOID_TYPE_P (TREE_TYPE (intype))
6716 && TYPE_PTROB_P (type))
6718 if (!c_cast_p
6719 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6720 complain))
6721 return error_mark_node;
6722 return build_nop (type, expr);
6725 *valid_p = false;
6726 return error_mark_node;
6729 /* Return an expression representing static_cast<TYPE>(EXPR). */
6731 tree
6732 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6734 tree result;
6735 bool valid_p;
6737 if (type == error_mark_node || expr == error_mark_node)
6738 return error_mark_node;
6740 if (processing_template_decl)
6742 expr = build_min (STATIC_CAST_EXPR, type, expr);
6743 /* We don't know if it will or will not have side effects. */
6744 TREE_SIDE_EFFECTS (expr) = 1;
6745 return convert_from_reference (expr);
6748 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6749 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6750 if (TREE_CODE (type) != REFERENCE_TYPE
6751 && TREE_CODE (expr) == NOP_EXPR
6752 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6753 expr = TREE_OPERAND (expr, 0);
6755 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6756 complain);
6757 if (valid_p)
6759 if (result != error_mark_node)
6760 maybe_warn_about_useless_cast (type, expr, complain);
6761 return result;
6764 if (complain & tf_error)
6765 error ("invalid static_cast from type %qT to type %qT",
6766 TREE_TYPE (expr), type);
6767 return error_mark_node;
6770 /* EXPR is an expression with member function or pointer-to-member
6771 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6772 not permitted by ISO C++, but we accept it in some modes. If we
6773 are not in one of those modes, issue a diagnostic. Return the
6774 converted expression. */
6776 tree
6777 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6779 tree intype;
6780 tree decl;
6782 intype = TREE_TYPE (expr);
6783 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6784 || TREE_CODE (intype) == METHOD_TYPE);
6786 if (!(complain & tf_warning_or_error))
6787 return error_mark_node;
6789 if (pedantic || warn_pmf2ptr)
6790 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6791 "converting from %qT to %qT", intype, type);
6793 if (TREE_CODE (intype) == METHOD_TYPE)
6794 expr = build_addr_func (expr, complain);
6795 else if (TREE_CODE (expr) == PTRMEM_CST)
6796 expr = build_address (PTRMEM_CST_MEMBER (expr));
6797 else
6799 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6800 decl = build_address (decl);
6801 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6804 if (expr == error_mark_node)
6805 return error_mark_node;
6807 return build_nop (type, expr);
6810 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6811 If C_CAST_P is true, this reinterpret cast is being done as part of
6812 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6813 indicate whether or not reinterpret_cast was valid. */
6815 static tree
6816 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6817 bool *valid_p, tsubst_flags_t complain)
6819 tree intype;
6821 /* Assume the cast is invalid. */
6822 if (valid_p)
6823 *valid_p = true;
6825 if (type == error_mark_node || error_operand_p (expr))
6826 return error_mark_node;
6828 intype = TREE_TYPE (expr);
6830 /* Save casted types in the function's used types hash table. */
6831 used_types_insert (type);
6833 /* [expr.reinterpret.cast]
6834 An lvalue expression of type T1 can be cast to the type
6835 "reference to T2" if an expression of type "pointer to T1" can be
6836 explicitly converted to the type "pointer to T2" using a
6837 reinterpret_cast. */
6838 if (TREE_CODE (type) == REFERENCE_TYPE)
6840 if (! real_lvalue_p (expr))
6842 if (complain & tf_error)
6843 error ("invalid cast of an rvalue expression of type "
6844 "%qT to type %qT",
6845 intype, type);
6846 return error_mark_node;
6849 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6850 "B" are related class types; the reinterpret_cast does not
6851 adjust the pointer. */
6852 if (TYPE_PTR_P (intype)
6853 && (complain & tf_warning)
6854 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6855 COMPARE_BASE | COMPARE_DERIVED)))
6856 warning (0, "casting %qT to %qT does not dereference pointer",
6857 intype, type);
6859 expr = cp_build_addr_expr (expr, complain);
6861 if (warn_strict_aliasing > 2)
6862 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6864 if (expr != error_mark_node)
6865 expr = build_reinterpret_cast_1
6866 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6867 valid_p, complain);
6868 if (expr != error_mark_node)
6869 /* cp_build_indirect_ref isn't right for rvalue refs. */
6870 expr = convert_from_reference (fold_convert (type, expr));
6871 return expr;
6874 /* As a G++ extension, we consider conversions from member
6875 functions, and pointers to member functions to
6876 pointer-to-function and pointer-to-void types. If
6877 -Wno-pmf-conversions has not been specified,
6878 convert_member_func_to_ptr will issue an error message. */
6879 if ((TYPE_PTRMEMFUNC_P (intype)
6880 || TREE_CODE (intype) == METHOD_TYPE)
6881 && TYPE_PTR_P (type)
6882 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6883 || VOID_TYPE_P (TREE_TYPE (type))))
6884 return convert_member_func_to_ptr (type, expr, complain);
6886 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6887 array-to-pointer, and function-to-pointer conversions are
6888 performed. */
6889 expr = decay_conversion (expr, complain);
6891 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6892 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6893 if (TREE_CODE (expr) == NOP_EXPR
6894 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6895 expr = TREE_OPERAND (expr, 0);
6897 if (error_operand_p (expr))
6898 return error_mark_node;
6900 intype = TREE_TYPE (expr);
6902 /* [expr.reinterpret.cast]
6903 A pointer can be converted to any integral type large enough to
6904 hold it. ... A value of type std::nullptr_t can be converted to
6905 an integral type; the conversion has the same meaning and
6906 validity as a conversion of (void*)0 to the integral type. */
6907 if (CP_INTEGRAL_TYPE_P (type)
6908 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6910 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6912 if (complain & tf_error)
6913 permerror (input_location, "cast from %qT to %qT loses precision",
6914 intype, type);
6915 else
6916 return error_mark_node;
6918 if (NULLPTR_TYPE_P (intype))
6919 return build_int_cst (type, 0);
6921 /* [expr.reinterpret.cast]
6922 A value of integral or enumeration type can be explicitly
6923 converted to a pointer. */
6924 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6925 /* OK */
6927 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6928 || TYPE_PTR_OR_PTRMEM_P (type))
6929 && same_type_p (type, intype))
6930 /* DR 799 */
6931 return fold_if_not_in_template (build_nop (type, expr));
6932 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6933 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6934 return fold_if_not_in_template (build_nop (type, expr));
6935 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6936 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6938 tree sexpr = expr;
6940 if (!c_cast_p
6941 && check_for_casting_away_constness (intype, type,
6942 REINTERPRET_CAST_EXPR,
6943 complain))
6944 return error_mark_node;
6945 /* Warn about possible alignment problems. */
6946 if (STRICT_ALIGNMENT && warn_cast_align
6947 && (complain & tf_warning)
6948 && !VOID_TYPE_P (type)
6949 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6950 && COMPLETE_TYPE_P (TREE_TYPE (type))
6951 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6952 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6953 warning (OPT_Wcast_align, "cast from %qT to %qT "
6954 "increases required alignment of target type", intype, type);
6956 /* We need to strip nops here, because the front end likes to
6957 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6958 STRIP_NOPS (sexpr);
6959 if (warn_strict_aliasing <= 2)
6960 strict_aliasing_warning (intype, type, sexpr);
6962 return fold_if_not_in_template (build_nop (type, expr));
6964 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6965 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6967 if (complain & tf_warning)
6968 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6969 object pointer type or vice versa is conditionally-supported." */
6970 warning (OPT_Wconditionally_supported,
6971 "casting between pointer-to-function and pointer-to-object "
6972 "is conditionally-supported");
6973 return fold_if_not_in_template (build_nop (type, expr));
6975 else if (TREE_CODE (type) == VECTOR_TYPE)
6976 return fold_if_not_in_template (convert_to_vector (type, expr));
6977 else if (TREE_CODE (intype) == VECTOR_TYPE
6978 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6979 return fold_if_not_in_template (convert_to_integer (type, expr));
6980 else
6982 if (valid_p)
6983 *valid_p = false;
6984 if (complain & tf_error)
6985 error ("invalid cast from type %qT to type %qT", intype, type);
6986 return error_mark_node;
6989 return cp_convert (type, expr, complain);
6992 tree
6993 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6995 tree r;
6997 if (type == error_mark_node || expr == error_mark_node)
6998 return error_mark_node;
7000 if (processing_template_decl)
7002 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7004 if (!TREE_SIDE_EFFECTS (t)
7005 && type_dependent_expression_p (expr))
7006 /* There might turn out to be side effects inside expr. */
7007 TREE_SIDE_EFFECTS (t) = 1;
7008 return convert_from_reference (t);
7011 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7012 /*valid_p=*/NULL, complain);
7013 if (r != error_mark_node)
7014 maybe_warn_about_useless_cast (type, expr, complain);
7015 return r;
7018 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7019 return an appropriate expression. Otherwise, return
7020 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7021 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7022 performing a C-style cast, its value upon return will indicate
7023 whether or not the conversion succeeded. */
7025 static tree
7026 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7027 bool *valid_p)
7029 tree src_type;
7030 tree reference_type;
7032 /* Callers are responsible for handling error_mark_node as a
7033 destination type. */
7034 gcc_assert (dst_type != error_mark_node);
7035 /* In a template, callers should be building syntactic
7036 representations of casts, not using this machinery. */
7037 gcc_assert (!processing_template_decl);
7039 /* Assume the conversion is invalid. */
7040 if (valid_p)
7041 *valid_p = false;
7043 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7045 if (complain & tf_error)
7046 error ("invalid use of const_cast with type %qT, "
7047 "which is not a pointer, "
7048 "reference, nor a pointer-to-data-member type", dst_type);
7049 return error_mark_node;
7052 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7054 if (complain & tf_error)
7055 error ("invalid use of const_cast with type %qT, which is a pointer "
7056 "or reference to a function type", dst_type);
7057 return error_mark_node;
7060 /* Save casted types in the function's used types hash table. */
7061 used_types_insert (dst_type);
7063 src_type = TREE_TYPE (expr);
7064 /* Expressions do not really have reference types. */
7065 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7066 src_type = TREE_TYPE (src_type);
7068 /* [expr.const.cast]
7070 For two object types T1 and T2, if a pointer to T1 can be explicitly
7071 converted to the type "pointer to T2" using a const_cast, then the
7072 following conversions can also be made:
7074 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7075 type T2 using the cast const_cast<T2&>;
7077 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7078 type T2 using the cast const_cast<T2&&>; and
7080 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7081 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7083 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7085 reference_type = dst_type;
7086 if (!TYPE_REF_IS_RVALUE (dst_type)
7087 ? real_lvalue_p (expr)
7088 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7089 ? lvalue_p (expr)
7090 : lvalue_or_rvalue_with_address_p (expr)))
7091 /* OK. */;
7092 else
7094 if (complain & tf_error)
7095 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7096 src_type, dst_type);
7097 return error_mark_node;
7099 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7100 src_type = build_pointer_type (src_type);
7102 else
7104 reference_type = NULL_TREE;
7105 /* If the destination type is not a reference type, the
7106 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7107 conversions are performed. */
7108 src_type = type_decays_to (src_type);
7109 if (src_type == error_mark_node)
7110 return error_mark_node;
7113 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7115 if (comp_ptr_ttypes_const (dst_type, src_type))
7117 if (valid_p)
7119 *valid_p = true;
7120 /* This cast is actually a C-style cast. Issue a warning if
7121 the user is making a potentially unsafe cast. */
7122 check_for_casting_away_constness (src_type, dst_type,
7123 CAST_EXPR, complain);
7125 if (reference_type)
7127 expr = cp_build_addr_expr (expr, complain);
7128 if (expr == error_mark_node)
7129 return error_mark_node;
7130 expr = build_nop (reference_type, expr);
7131 return convert_from_reference (expr);
7133 else
7135 expr = decay_conversion (expr, complain);
7136 if (expr == error_mark_node)
7137 return error_mark_node;
7139 /* build_c_cast puts on a NOP_EXPR to make the result not an
7140 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7141 non-lvalue context. */
7142 if (TREE_CODE (expr) == NOP_EXPR
7143 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7144 expr = TREE_OPERAND (expr, 0);
7145 return build_nop (dst_type, expr);
7148 else if (valid_p
7149 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7150 TREE_TYPE (src_type)))
7151 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7152 complain);
7155 if (complain & tf_error)
7156 error ("invalid const_cast from type %qT to type %qT",
7157 src_type, dst_type);
7158 return error_mark_node;
7161 tree
7162 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7164 tree r;
7166 if (type == error_mark_node || error_operand_p (expr))
7167 return error_mark_node;
7169 if (processing_template_decl)
7171 tree t = build_min (CONST_CAST_EXPR, type, expr);
7173 if (!TREE_SIDE_EFFECTS (t)
7174 && type_dependent_expression_p (expr))
7175 /* There might turn out to be side effects inside expr. */
7176 TREE_SIDE_EFFECTS (t) = 1;
7177 return convert_from_reference (t);
7180 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7181 if (r != error_mark_node)
7182 maybe_warn_about_useless_cast (type, expr, complain);
7183 return r;
7186 /* Like cp_build_c_cast, but for the c-common bits. */
7188 tree
7189 build_c_cast (location_t /*loc*/, tree type, tree expr)
7191 return cp_build_c_cast (type, expr, tf_warning_or_error);
7194 /* Build an expression representing an explicit C-style cast to type
7195 TYPE of expression EXPR. */
7197 tree
7198 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7200 tree value = expr;
7201 tree result;
7202 bool valid_p;
7204 if (type == error_mark_node || error_operand_p (expr))
7205 return error_mark_node;
7207 if (processing_template_decl)
7209 tree t = build_min (CAST_EXPR, type,
7210 tree_cons (NULL_TREE, value, NULL_TREE));
7211 /* We don't know if it will or will not have side effects. */
7212 TREE_SIDE_EFFECTS (t) = 1;
7213 return convert_from_reference (t);
7216 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7217 'Class') should always be retained, because this information aids
7218 in method lookup. */
7219 if (objc_is_object_ptr (type)
7220 && objc_is_object_ptr (TREE_TYPE (expr)))
7221 return build_nop (type, expr);
7223 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7224 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7225 if (TREE_CODE (type) != REFERENCE_TYPE
7226 && TREE_CODE (value) == NOP_EXPR
7227 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7228 value = TREE_OPERAND (value, 0);
7230 if (TREE_CODE (type) == ARRAY_TYPE)
7232 /* Allow casting from T1* to T2[] because Cfront allows it.
7233 NIHCL uses it. It is not valid ISO C++ however. */
7234 if (TYPE_PTR_P (TREE_TYPE (expr)))
7236 if (complain & tf_error)
7237 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7238 else
7239 return error_mark_node;
7240 type = build_pointer_type (TREE_TYPE (type));
7242 else
7244 if (complain & tf_error)
7245 error ("ISO C++ forbids casting to an array type %qT", type);
7246 return error_mark_node;
7250 if (TREE_CODE (type) == FUNCTION_TYPE
7251 || TREE_CODE (type) == METHOD_TYPE)
7253 if (complain & tf_error)
7254 error ("invalid cast to function type %qT", type);
7255 return error_mark_node;
7258 if (TYPE_PTR_P (type)
7259 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7260 /* Casting to an integer of smaller size is an error detected elsewhere. */
7261 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7262 /* Don't warn about converting any constant. */
7263 && !TREE_CONSTANT (value))
7264 warning_at (input_location, OPT_Wint_to_pointer_cast,
7265 "cast to pointer from integer of different size");
7267 /* A C-style cast can be a const_cast. */
7268 result = build_const_cast_1 (type, value, complain & tf_warning,
7269 &valid_p);
7270 if (valid_p)
7272 if (result != error_mark_node)
7273 maybe_warn_about_useless_cast (type, value, complain);
7274 return result;
7277 /* Or a static cast. */
7278 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7279 &valid_p, complain);
7280 /* Or a reinterpret_cast. */
7281 if (!valid_p)
7282 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7283 &valid_p, complain);
7284 /* The static_cast or reinterpret_cast may be followed by a
7285 const_cast. */
7286 if (valid_p
7287 /* A valid cast may result in errors if, for example, a
7288 conversion to an ambiguous base class is required. */
7289 && !error_operand_p (result))
7291 tree result_type;
7293 maybe_warn_about_useless_cast (type, value, complain);
7295 /* Non-class rvalues always have cv-unqualified type. */
7296 if (!CLASS_TYPE_P (type))
7297 type = TYPE_MAIN_VARIANT (type);
7298 result_type = TREE_TYPE (result);
7299 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7300 result_type = TYPE_MAIN_VARIANT (result_type);
7301 /* If the type of RESULT does not match TYPE, perform a
7302 const_cast to make it match. If the static_cast or
7303 reinterpret_cast succeeded, we will differ by at most
7304 cv-qualification, so the follow-on const_cast is guaranteed
7305 to succeed. */
7306 if (!same_type_p (non_reference (type), non_reference (result_type)))
7308 result = build_const_cast_1 (type, result, false, &valid_p);
7309 gcc_assert (valid_p);
7311 return result;
7314 return error_mark_node;
7317 /* For use from the C common bits. */
7318 tree
7319 build_modify_expr (location_t /*location*/,
7320 tree lhs, tree /*lhs_origtype*/,
7321 enum tree_code modifycode,
7322 location_t /*rhs_location*/, tree rhs,
7323 tree /*rhs_origtype*/)
7325 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7328 /* Build an assignment expression of lvalue LHS from value RHS.
7329 MODIFYCODE is the code for a binary operator that we use
7330 to combine the old value of LHS with RHS to get the new value.
7331 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7333 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7335 tree
7336 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7337 tsubst_flags_t complain)
7339 tree result;
7340 tree newrhs = rhs;
7341 tree lhstype = TREE_TYPE (lhs);
7342 tree olhstype = lhstype;
7343 bool plain_assign = (modifycode == NOP_EXPR);
7345 /* Avoid duplicate error messages from operands that had errors. */
7346 if (error_operand_p (lhs) || error_operand_p (rhs))
7347 return error_mark_node;
7349 /* Handle control structure constructs used as "lvalues". */
7350 switch (TREE_CODE (lhs))
7352 /* Handle --foo = 5; as these are valid constructs in C++. */
7353 case PREDECREMENT_EXPR:
7354 case PREINCREMENT_EXPR:
7355 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7356 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7357 stabilize_reference (TREE_OPERAND (lhs, 0)),
7358 TREE_OPERAND (lhs, 1));
7359 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7360 modifycode, rhs, complain);
7361 if (newrhs == error_mark_node)
7362 return error_mark_node;
7363 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7365 /* Handle (a, b) used as an "lvalue". */
7366 case COMPOUND_EXPR:
7367 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7368 modifycode, rhs, complain);
7369 if (newrhs == error_mark_node)
7370 return error_mark_node;
7371 return build2 (COMPOUND_EXPR, lhstype,
7372 TREE_OPERAND (lhs, 0), newrhs);
7374 case MODIFY_EXPR:
7375 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7376 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7377 stabilize_reference (TREE_OPERAND (lhs, 0)),
7378 TREE_OPERAND (lhs, 1));
7379 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7380 complain);
7381 if (newrhs == error_mark_node)
7382 return error_mark_node;
7383 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7385 case MIN_EXPR:
7386 case MAX_EXPR:
7387 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7388 when neither operand has side-effects. */
7389 if (!lvalue_or_else (lhs, lv_assign, complain))
7390 return error_mark_node;
7392 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7393 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7395 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7396 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7397 boolean_type_node,
7398 TREE_OPERAND (lhs, 0),
7399 TREE_OPERAND (lhs, 1)),
7400 TREE_OPERAND (lhs, 0),
7401 TREE_OPERAND (lhs, 1));
7402 /* Fall through. */
7404 /* Handle (a ? b : c) used as an "lvalue". */
7405 case COND_EXPR:
7407 /* Produce (a ? (b = rhs) : (c = rhs))
7408 except that the RHS goes through a save-expr
7409 so the code to compute it is only emitted once. */
7410 tree cond;
7411 tree preeval = NULL_TREE;
7413 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7415 if (complain & tf_error)
7416 error ("void value not ignored as it ought to be");
7417 return error_mark_node;
7420 rhs = stabilize_expr (rhs, &preeval);
7422 /* Check this here to avoid odd errors when trying to convert
7423 a throw to the type of the COND_EXPR. */
7424 if (!lvalue_or_else (lhs, lv_assign, complain))
7425 return error_mark_node;
7427 cond = build_conditional_expr
7428 (input_location, TREE_OPERAND (lhs, 0),
7429 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7430 modifycode, rhs, complain),
7431 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7432 modifycode, rhs, complain),
7433 complain);
7435 if (cond == error_mark_node)
7436 return cond;
7437 /* Make sure the code to compute the rhs comes out
7438 before the split. */
7439 if (preeval)
7440 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7441 return cond;
7444 default:
7445 break;
7448 if (modifycode == INIT_EXPR)
7450 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7451 /* Do the default thing. */;
7452 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7454 /* Compound literal. */
7455 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7456 /* Call convert to generate an error; see PR 11063. */
7457 rhs = convert (lhstype, rhs);
7458 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7459 TREE_SIDE_EFFECTS (result) = 1;
7460 return result;
7462 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7463 /* Do the default thing. */;
7464 else
7466 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7467 result = build_special_member_call (lhs, complete_ctor_identifier,
7468 &rhs_vec, lhstype, LOOKUP_NORMAL,
7469 complain);
7470 release_tree_vector (rhs_vec);
7471 if (result == NULL_TREE)
7472 return error_mark_node;
7473 return result;
7476 else
7478 lhs = require_complete_type_sfinae (lhs, complain);
7479 if (lhs == error_mark_node)
7480 return error_mark_node;
7482 if (modifycode == NOP_EXPR)
7484 if (c_dialect_objc ())
7486 result = objc_maybe_build_modify_expr (lhs, rhs);
7487 if (result)
7488 return result;
7491 /* `operator=' is not an inheritable operator. */
7492 if (! MAYBE_CLASS_TYPE_P (lhstype))
7493 /* Do the default thing. */;
7494 else
7496 result = build_new_op (input_location, MODIFY_EXPR,
7497 LOOKUP_NORMAL, lhs, rhs,
7498 make_node (NOP_EXPR), /*overload=*/NULL,
7499 complain);
7500 if (result == NULL_TREE)
7501 return error_mark_node;
7502 return result;
7504 lhstype = olhstype;
7506 else
7508 tree init = NULL_TREE;
7510 /* A binary op has been requested. Combine the old LHS
7511 value with the RHS producing the value we should actually
7512 store into the LHS. */
7513 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7514 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7515 || MAYBE_CLASS_TYPE_P (lhstype)));
7517 /* Preevaluate the RHS to make sure its evaluation is complete
7518 before the lvalue-to-rvalue conversion of the LHS:
7520 [expr.ass] With respect to an indeterminately-sequenced
7521 function call, the operation of a compound assignment is a
7522 single evaluation. [ Note: Therefore, a function call shall
7523 not intervene between the lvalue-to-rvalue conversion and the
7524 side effect associated with any single compound assignment
7525 operator. -- end note ] */
7526 lhs = stabilize_reference (lhs);
7527 rhs = rvalue (rhs);
7528 rhs = stabilize_expr (rhs, &init);
7529 newrhs = cp_build_binary_op (input_location,
7530 modifycode, lhs, rhs,
7531 complain);
7532 if (newrhs == error_mark_node)
7534 if (complain & tf_error)
7535 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7536 TREE_TYPE (lhs), TREE_TYPE (rhs));
7537 return error_mark_node;
7540 if (init)
7541 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7543 /* Now it looks like a plain assignment. */
7544 modifycode = NOP_EXPR;
7545 if (c_dialect_objc ())
7547 result = objc_maybe_build_modify_expr (lhs, newrhs);
7548 if (result)
7549 return result;
7552 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7553 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7556 /* The left-hand side must be an lvalue. */
7557 if (!lvalue_or_else (lhs, lv_assign, complain))
7558 return error_mark_node;
7560 /* Warn about modifying something that is `const'. Don't warn if
7561 this is initialization. */
7562 if (modifycode != INIT_EXPR
7563 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7564 /* Functions are not modifiable, even though they are
7565 lvalues. */
7566 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7567 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7568 /* If it's an aggregate and any field is const, then it is
7569 effectively const. */
7570 || (CLASS_TYPE_P (lhstype)
7571 && C_TYPE_FIELDS_READONLY (lhstype))))
7573 if (complain & tf_error)
7574 cxx_readonly_error (lhs, lv_assign);
7575 else
7576 return error_mark_node;
7579 /* If storing into a structure or union member, it may have been given a
7580 lowered bitfield type. We need to convert to the declared type first,
7581 so retrieve it now. */
7583 olhstype = unlowered_expr_type (lhs);
7585 /* Convert new value to destination type. */
7587 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7589 int from_array;
7591 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7593 if (modifycode != INIT_EXPR)
7595 if (complain & tf_error)
7596 error ("assigning to an array from an initializer list");
7597 return error_mark_node;
7599 if (check_array_initializer (lhs, lhstype, newrhs))
7600 return error_mark_node;
7601 newrhs = digest_init (lhstype, newrhs, complain);
7602 if (newrhs == error_mark_node)
7603 return error_mark_node;
7606 /* C++11 8.5/17: "If the destination type is an array of characters,
7607 an array of char16_t, an array of char32_t, or an array of wchar_t,
7608 and the initializer is a string literal...". */
7609 else if (TREE_CODE (newrhs) == STRING_CST
7610 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7611 && modifycode == INIT_EXPR)
7613 newrhs = digest_init (lhstype, newrhs, complain);
7614 if (newrhs == error_mark_node)
7615 return error_mark_node;
7618 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7619 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7621 if (complain & tf_error)
7622 error ("incompatible types in assignment of %qT to %qT",
7623 TREE_TYPE (rhs), lhstype);
7624 return error_mark_node;
7627 /* Allow array assignment in compiler-generated code. */
7628 else if (!current_function_decl
7629 || !DECL_DEFAULTED_FN (current_function_decl))
7631 /* This routine is used for both initialization and assignment.
7632 Make sure the diagnostic message differentiates the context. */
7633 if (complain & tf_error)
7635 if (modifycode == INIT_EXPR)
7636 error ("array used as initializer");
7637 else
7638 error ("invalid array assignment");
7640 return error_mark_node;
7643 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7644 ? 1 + (modifycode != INIT_EXPR): 0;
7645 return build_vec_init (lhs, NULL_TREE, newrhs,
7646 /*explicit_value_init_p=*/false,
7647 from_array, complain);
7650 if (modifycode == INIT_EXPR)
7651 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7652 LOOKUP_ONLYCONVERTING. */
7653 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7654 ICR_INIT, NULL_TREE, 0,
7655 complain);
7656 else
7657 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7658 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7660 if (!same_type_p (lhstype, olhstype))
7661 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7663 if (modifycode != INIT_EXPR)
7665 if (TREE_CODE (newrhs) == CALL_EXPR
7666 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7667 newrhs = build_cplus_new (lhstype, newrhs, complain);
7669 /* Can't initialize directly from a TARGET_EXPR, since that would
7670 cause the lhs to be constructed twice, and possibly result in
7671 accidental self-initialization. So we force the TARGET_EXPR to be
7672 expanded without a target. */
7673 if (TREE_CODE (newrhs) == TARGET_EXPR)
7674 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7675 TREE_OPERAND (newrhs, 0));
7678 if (newrhs == error_mark_node)
7679 return error_mark_node;
7681 if (c_dialect_objc () && flag_objc_gc)
7683 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7685 if (result)
7686 return result;
7689 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7690 lhstype, lhs, newrhs);
7692 TREE_SIDE_EFFECTS (result) = 1;
7693 if (!plain_assign)
7694 TREE_NO_WARNING (result) = 1;
7696 return result;
7699 tree
7700 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7701 tree rhs, tsubst_flags_t complain)
7703 if (processing_template_decl)
7704 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7705 build_min_nt_loc (loc, modifycode, NULL_TREE,
7706 NULL_TREE), rhs);
7708 if (modifycode != NOP_EXPR)
7710 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7711 make_node (modifycode), /*overload=*/NULL,
7712 complain);
7713 if (rval)
7715 TREE_NO_WARNING (rval) = 1;
7716 return rval;
7719 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7722 /* Helper function for get_delta_difference which assumes FROM is a base
7723 class of TO. Returns a delta for the conversion of pointer-to-member
7724 of FROM to pointer-to-member of TO. If the conversion is invalid and
7725 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7726 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7727 If C_CAST_P is true, this conversion is taking place as part of a
7728 C-style cast. */
7730 static tree
7731 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7732 tsubst_flags_t complain)
7734 tree binfo;
7735 base_kind kind;
7737 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7738 &kind, complain);
7740 if (binfo == error_mark_node)
7742 if (!(complain & tf_error))
7743 return error_mark_node;
7745 error (" in pointer to member function conversion");
7746 return size_zero_node;
7748 else if (binfo)
7750 if (kind != bk_via_virtual)
7751 return BINFO_OFFSET (binfo);
7752 else
7753 /* FROM is a virtual base class of TO. Issue an error or warning
7754 depending on whether or not this is a reinterpret cast. */
7756 if (!(complain & tf_error))
7757 return error_mark_node;
7759 error ("pointer to member conversion via virtual base %qT",
7760 BINFO_TYPE (binfo_from_vbase (binfo)));
7762 return size_zero_node;
7765 else
7766 return NULL_TREE;
7769 /* Get difference in deltas for different pointer to member function
7770 types. If the conversion is invalid and tf_error is not set in
7771 COMPLAIN, returns error_mark_node, otherwise returns an integer
7772 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7773 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7774 conversions as well. If C_CAST_P is true this conversion is taking
7775 place as part of a C-style cast.
7777 Note that the naming of FROM and TO is kind of backwards; the return
7778 value is what we add to a TO in order to get a FROM. They are named
7779 this way because we call this function to find out how to convert from
7780 a pointer to member of FROM to a pointer to member of TO. */
7782 static tree
7783 get_delta_difference (tree from, tree to,
7784 bool allow_inverse_p,
7785 bool c_cast_p, tsubst_flags_t complain)
7787 tree result;
7789 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7790 /* Pointer to member of incomplete class is permitted*/
7791 result = size_zero_node;
7792 else
7793 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7795 if (result == error_mark_node)
7796 return error_mark_node;
7798 if (!result)
7800 if (!allow_inverse_p)
7802 if (!(complain & tf_error))
7803 return error_mark_node;
7805 error_not_base_type (from, to);
7806 error (" in pointer to member conversion");
7807 result = size_zero_node;
7809 else
7811 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7813 if (result == error_mark_node)
7814 return error_mark_node;
7816 if (result)
7817 result = size_diffop_loc (input_location,
7818 size_zero_node, result);
7819 else
7821 if (!(complain & tf_error))
7822 return error_mark_node;
7824 error_not_base_type (from, to);
7825 error (" in pointer to member conversion");
7826 result = size_zero_node;
7831 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7832 result));
7835 /* Return a constructor for the pointer-to-member-function TYPE using
7836 the other components as specified. */
7838 tree
7839 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7841 tree u = NULL_TREE;
7842 tree delta_field;
7843 tree pfn_field;
7844 vec<constructor_elt, va_gc> *v;
7846 /* Pull the FIELD_DECLs out of the type. */
7847 pfn_field = TYPE_FIELDS (type);
7848 delta_field = DECL_CHAIN (pfn_field);
7850 /* Make sure DELTA has the type we want. */
7851 delta = convert_and_check (input_location, delta_type_node, delta);
7853 /* Convert to the correct target type if necessary. */
7854 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7856 /* Finish creating the initializer. */
7857 vec_alloc (v, 2);
7858 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7859 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7860 u = build_constructor (type, v);
7861 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7862 TREE_STATIC (u) = (TREE_CONSTANT (u)
7863 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7864 != NULL_TREE)
7865 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7866 != NULL_TREE));
7867 return u;
7870 /* Build a constructor for a pointer to member function. It can be
7871 used to initialize global variables, local variable, or used
7872 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7873 want to be.
7875 If FORCE is nonzero, then force this conversion, even if
7876 we would rather not do it. Usually set when using an explicit
7877 cast. A C-style cast is being processed iff C_CAST_P is true.
7879 Return error_mark_node, if something goes wrong. */
7881 tree
7882 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7883 tsubst_flags_t complain)
7885 tree fn;
7886 tree pfn_type;
7887 tree to_type;
7889 if (error_operand_p (pfn))
7890 return error_mark_node;
7892 pfn_type = TREE_TYPE (pfn);
7893 to_type = build_ptrmemfunc_type (type);
7895 /* Handle multiple conversions of pointer to member functions. */
7896 if (TYPE_PTRMEMFUNC_P (pfn_type))
7898 tree delta = NULL_TREE;
7899 tree npfn = NULL_TREE;
7900 tree n;
7902 if (!force
7903 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7904 LOOKUP_NORMAL, complain))
7906 if (complain & tf_error)
7907 error ("invalid conversion to type %qT from type %qT",
7908 to_type, pfn_type);
7909 else
7910 return error_mark_node;
7913 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7914 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7915 force,
7916 c_cast_p, complain);
7917 if (n == error_mark_node)
7918 return error_mark_node;
7920 /* We don't have to do any conversion to convert a
7921 pointer-to-member to its own type. But, we don't want to
7922 just return a PTRMEM_CST if there's an explicit cast; that
7923 cast should make the expression an invalid template argument. */
7924 if (TREE_CODE (pfn) != PTRMEM_CST)
7926 if (same_type_p (to_type, pfn_type))
7927 return pfn;
7928 else if (integer_zerop (n))
7929 return build_reinterpret_cast (to_type, pfn,
7930 complain);
7933 if (TREE_SIDE_EFFECTS (pfn))
7934 pfn = save_expr (pfn);
7936 /* Obtain the function pointer and the current DELTA. */
7937 if (TREE_CODE (pfn) == PTRMEM_CST)
7938 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7939 else
7941 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7942 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7945 /* Just adjust the DELTA field. */
7946 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7947 (TREE_TYPE (delta), ptrdiff_type_node));
7948 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7949 n = cp_build_binary_op (input_location,
7950 LSHIFT_EXPR, n, integer_one_node,
7951 complain);
7952 delta = cp_build_binary_op (input_location,
7953 PLUS_EXPR, delta, n, complain);
7954 return build_ptrmemfunc1 (to_type, delta, npfn);
7957 /* Handle null pointer to member function conversions. */
7958 if (null_ptr_cst_p (pfn))
7960 pfn = cp_build_c_cast (type, pfn, complain);
7961 return build_ptrmemfunc1 (to_type,
7962 integer_zero_node,
7963 pfn);
7966 if (type_unknown_p (pfn))
7967 return instantiate_type (type, pfn, complain);
7969 fn = TREE_OPERAND (pfn, 0);
7970 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7971 /* In a template, we will have preserved the
7972 OFFSET_REF. */
7973 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7974 return make_ptrmem_cst (to_type, fn);
7977 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7978 given by CST.
7980 ??? There is no consistency as to the types returned for the above
7981 values. Some code acts as if it were a sizetype and some as if it were
7982 integer_type_node. */
7984 void
7985 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7987 tree type = TREE_TYPE (cst);
7988 tree fn = PTRMEM_CST_MEMBER (cst);
7989 tree ptr_class, fn_class;
7991 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7993 /* The class that the function belongs to. */
7994 fn_class = DECL_CONTEXT (fn);
7996 /* The class that we're creating a pointer to member of. */
7997 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7999 /* First, calculate the adjustment to the function's class. */
8000 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8001 /*c_cast_p=*/0, tf_warning_or_error);
8003 if (!DECL_VIRTUAL_P (fn))
8004 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8005 build_addr_func (fn, tf_warning_or_error));
8006 else
8008 /* If we're dealing with a virtual function, we have to adjust 'this'
8009 again, to point to the base which provides the vtable entry for
8010 fn; the call will do the opposite adjustment. */
8011 tree orig_class = DECL_CONTEXT (fn);
8012 tree binfo = binfo_or_else (orig_class, fn_class);
8013 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
8014 *delta, BINFO_OFFSET (binfo));
8015 *delta = fold_if_not_in_template (*delta);
8017 /* We set PFN to the vtable offset at which the function can be
8018 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8019 case delta is shifted left, and then incremented). */
8020 *pfn = DECL_VINDEX (fn);
8021 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
8022 TYPE_SIZE_UNIT (vtable_entry_type));
8023 *pfn = fold_if_not_in_template (*pfn);
8025 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8027 case ptrmemfunc_vbit_in_pfn:
8028 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
8029 integer_one_node);
8030 *pfn = fold_if_not_in_template (*pfn);
8031 break;
8033 case ptrmemfunc_vbit_in_delta:
8034 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8035 *delta, integer_one_node);
8036 *delta = fold_if_not_in_template (*delta);
8037 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
8038 *delta, integer_one_node);
8039 *delta = fold_if_not_in_template (*delta);
8040 break;
8042 default:
8043 gcc_unreachable ();
8046 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8047 *pfn = fold_if_not_in_template (*pfn);
8051 /* Return an expression for PFN from the pointer-to-member function
8052 given by T. */
8054 static tree
8055 pfn_from_ptrmemfunc (tree t)
8057 if (TREE_CODE (t) == PTRMEM_CST)
8059 tree delta;
8060 tree pfn;
8062 expand_ptrmemfunc_cst (t, &delta, &pfn);
8063 if (pfn)
8064 return pfn;
8067 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8070 /* Return an expression for DELTA from the pointer-to-member function
8071 given by T. */
8073 static tree
8074 delta_from_ptrmemfunc (tree t)
8076 if (TREE_CODE (t) == PTRMEM_CST)
8078 tree delta;
8079 tree pfn;
8081 expand_ptrmemfunc_cst (t, &delta, &pfn);
8082 if (delta)
8083 return delta;
8086 return build_ptrmemfunc_access_expr (t, delta_identifier);
8089 /* Convert value RHS to type TYPE as preparation for an assignment to
8090 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8091 implicit conversion is. If FNDECL is non-NULL, we are doing the
8092 conversion in order to pass the PARMNUMth argument of FNDECL.
8093 If FNDECL is NULL, we are doing the conversion in function pointer
8094 argument passing, conversion in initialization, etc. */
8096 static tree
8097 convert_for_assignment (tree type, tree rhs,
8098 impl_conv_rhs errtype, tree fndecl, int parmnum,
8099 tsubst_flags_t complain, int flags)
8101 tree rhstype;
8102 enum tree_code coder;
8104 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8105 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8106 rhs = TREE_OPERAND (rhs, 0);
8108 rhstype = TREE_TYPE (rhs);
8109 coder = TREE_CODE (rhstype);
8111 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8112 && vector_types_convertible_p (type, rhstype, true))
8114 rhs = mark_rvalue_use (rhs);
8115 return convert (type, rhs);
8118 if (rhs == error_mark_node || rhstype == error_mark_node)
8119 return error_mark_node;
8120 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8121 return error_mark_node;
8123 /* The RHS of an assignment cannot have void type. */
8124 if (coder == VOID_TYPE)
8126 if (complain & tf_error)
8127 error ("void value not ignored as it ought to be");
8128 return error_mark_node;
8131 if (c_dialect_objc ())
8133 int parmno;
8134 tree selector;
8135 tree rname = fndecl;
8137 switch (errtype)
8139 case ICR_ASSIGN:
8140 parmno = -1;
8141 break;
8142 case ICR_INIT:
8143 parmno = -2;
8144 break;
8145 default:
8146 selector = objc_message_selector ();
8147 parmno = parmnum;
8148 if (selector && parmno > 1)
8150 rname = selector;
8151 parmno -= 1;
8155 if (objc_compare_types (type, rhstype, parmno, rname))
8157 rhs = mark_rvalue_use (rhs);
8158 return convert (type, rhs);
8162 /* [expr.ass]
8164 The expression is implicitly converted (clause _conv_) to the
8165 cv-unqualified type of the left operand.
8167 We allow bad conversions here because by the time we get to this point
8168 we are committed to doing the conversion. If we end up doing a bad
8169 conversion, convert_like will complain. */
8170 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8172 /* When -Wno-pmf-conversions is use, we just silently allow
8173 conversions from pointers-to-members to plain pointers. If
8174 the conversion doesn't work, cp_convert will complain. */
8175 if (!warn_pmf2ptr
8176 && TYPE_PTR_P (type)
8177 && TYPE_PTRMEMFUNC_P (rhstype))
8178 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8179 else
8181 if (complain & tf_error)
8183 /* If the right-hand side has unknown type, then it is an
8184 overloaded function. Call instantiate_type to get error
8185 messages. */
8186 if (rhstype == unknown_type_node)
8187 instantiate_type (type, rhs, tf_warning_or_error);
8188 else if (fndecl)
8189 error ("cannot convert %qT to %qT for argument %qP to %qD",
8190 rhstype, type, parmnum, fndecl);
8191 else
8192 switch (errtype)
8194 case ICR_DEFAULT_ARGUMENT:
8195 error ("cannot convert %qT to %qT in default argument",
8196 rhstype, type);
8197 break;
8198 case ICR_ARGPASS:
8199 error ("cannot convert %qT to %qT in argument passing",
8200 rhstype, type);
8201 break;
8202 case ICR_CONVERTING:
8203 error ("cannot convert %qT to %qT",
8204 rhstype, type);
8205 break;
8206 case ICR_INIT:
8207 error ("cannot convert %qT to %qT in initialization",
8208 rhstype, type);
8209 break;
8210 case ICR_RETURN:
8211 error ("cannot convert %qT to %qT in return",
8212 rhstype, type);
8213 break;
8214 case ICR_ASSIGN:
8215 error ("cannot convert %qT to %qT in assignment",
8216 rhstype, type);
8217 break;
8218 default:
8219 gcc_unreachable();
8221 if (TYPE_PTR_P (rhstype)
8222 && TYPE_PTR_P (type)
8223 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8224 && CLASS_TYPE_P (TREE_TYPE (type))
8225 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8226 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8227 (TREE_TYPE (rhstype))),
8228 "class type %qT is incomplete", TREE_TYPE (rhstype));
8230 return error_mark_node;
8233 if (warn_suggest_attribute_format)
8235 const enum tree_code codel = TREE_CODE (type);
8236 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8237 && coder == codel
8238 && check_missing_format_attribute (type, rhstype)
8239 && (complain & tf_warning))
8240 switch (errtype)
8242 case ICR_ARGPASS:
8243 case ICR_DEFAULT_ARGUMENT:
8244 if (fndecl)
8245 warning (OPT_Wsuggest_attribute_format,
8246 "parameter %qP of %qD might be a candidate "
8247 "for a format attribute", parmnum, fndecl);
8248 else
8249 warning (OPT_Wsuggest_attribute_format,
8250 "parameter might be a candidate "
8251 "for a format attribute");
8252 break;
8253 case ICR_CONVERTING:
8254 warning (OPT_Wsuggest_attribute_format,
8255 "target of conversion might be a candidate "
8256 "for a format attribute");
8257 break;
8258 case ICR_INIT:
8259 warning (OPT_Wsuggest_attribute_format,
8260 "target of initialization might be a candidate "
8261 "for a format attribute");
8262 break;
8263 case ICR_RETURN:
8264 warning (OPT_Wsuggest_attribute_format,
8265 "return type might be a candidate "
8266 "for a format attribute");
8267 break;
8268 case ICR_ASSIGN:
8269 warning (OPT_Wsuggest_attribute_format,
8270 "left-hand side of assignment might be a candidate "
8271 "for a format attribute");
8272 break;
8273 default:
8274 gcc_unreachable();
8278 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8279 does not. */
8280 if (warn_parentheses
8281 && TREE_CODE (type) == BOOLEAN_TYPE
8282 && TREE_CODE (rhs) == MODIFY_EXPR
8283 && !TREE_NO_WARNING (rhs)
8284 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8285 && (complain & tf_warning))
8287 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8289 warning_at (loc, OPT_Wparentheses,
8290 "suggest parentheses around assignment used as truth value");
8291 TREE_NO_WARNING (rhs) = 1;
8294 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8295 complain, flags);
8298 /* Convert RHS to be of type TYPE.
8299 If EXP is nonzero, it is the target of the initialization.
8300 ERRTYPE indicates what kind of error the implicit conversion is.
8302 Two major differences between the behavior of
8303 `convert_for_assignment' and `convert_for_initialization'
8304 are that references are bashed in the former, while
8305 copied in the latter, and aggregates are assigned in
8306 the former (operator=) while initialized in the
8307 latter (X(X&)).
8309 If using constructor make sure no conversion operator exists, if one does
8310 exist, an ambiguity exists. */
8312 tree
8313 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8314 impl_conv_rhs errtype, tree fndecl, int parmnum,
8315 tsubst_flags_t complain)
8317 enum tree_code codel = TREE_CODE (type);
8318 tree rhstype;
8319 enum tree_code coder;
8321 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8322 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8323 if (TREE_CODE (rhs) == NOP_EXPR
8324 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8325 && codel != REFERENCE_TYPE)
8326 rhs = TREE_OPERAND (rhs, 0);
8328 if (type == error_mark_node
8329 || rhs == error_mark_node
8330 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8331 return error_mark_node;
8333 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8334 && TREE_CODE (type) != ARRAY_TYPE
8335 && (TREE_CODE (type) != REFERENCE_TYPE
8336 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8337 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8338 && !TYPE_REFFN_P (type))
8339 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8340 rhs = decay_conversion (rhs, complain);
8342 rhstype = TREE_TYPE (rhs);
8343 coder = TREE_CODE (rhstype);
8345 if (coder == ERROR_MARK)
8346 return error_mark_node;
8348 /* We accept references to incomplete types, so we can
8349 return here before checking if RHS is of complete type. */
8351 if (codel == REFERENCE_TYPE)
8353 /* This should eventually happen in convert_arguments. */
8354 int savew = 0, savee = 0;
8356 if (fndecl)
8357 savew = warningcount + werrorcount, savee = errorcount;
8358 rhs = initialize_reference (type, rhs, flags, complain);
8360 if (fndecl
8361 && (warningcount + werrorcount > savew || errorcount > savee))
8362 inform (input_location,
8363 "in passing argument %P of %q+D", parmnum, fndecl);
8365 return rhs;
8368 if (exp != 0)
8369 exp = require_complete_type_sfinae (exp, complain);
8370 if (exp == error_mark_node)
8371 return error_mark_node;
8373 rhstype = non_reference (rhstype);
8375 type = complete_type (type);
8377 if (DIRECT_INIT_EXPR_P (type, rhs))
8378 /* Don't try to do copy-initialization if we already have
8379 direct-initialization. */
8380 return rhs;
8382 if (MAYBE_CLASS_TYPE_P (type))
8383 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8385 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8386 complain, flags);
8389 /* If RETVAL is the address of, or a reference to, a local variable or
8390 temporary give an appropriate warning and return true. */
8392 static bool
8393 maybe_warn_about_returning_address_of_local (tree retval)
8395 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8396 tree whats_returned = retval;
8398 for (;;)
8400 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8401 whats_returned = TREE_OPERAND (whats_returned, 1);
8402 else if (CONVERT_EXPR_P (whats_returned)
8403 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8404 whats_returned = TREE_OPERAND (whats_returned, 0);
8405 else
8406 break;
8409 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8410 return false;
8411 whats_returned = TREE_OPERAND (whats_returned, 0);
8413 while (TREE_CODE (whats_returned) == COMPONENT_REF
8414 || TREE_CODE (whats_returned) == ARRAY_REF)
8415 whats_returned = TREE_OPERAND (whats_returned, 0);
8417 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8419 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8420 || TREE_CODE (whats_returned) == TARGET_EXPR)
8422 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8423 return true;
8425 if (VAR_P (whats_returned)
8426 && DECL_NAME (whats_returned)
8427 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8429 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8430 return true;
8434 if (DECL_P (whats_returned)
8435 && DECL_NAME (whats_returned)
8436 && DECL_FUNCTION_SCOPE_P (whats_returned)
8437 && !is_capture_proxy (whats_returned)
8438 && !(TREE_STATIC (whats_returned)
8439 || TREE_PUBLIC (whats_returned)))
8441 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8442 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8443 whats_returned);
8444 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8445 warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
8446 whats_returned);
8447 else
8448 warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
8449 "returned", whats_returned);
8450 return true;
8453 return false;
8456 /* Check that returning RETVAL from the current function is valid.
8457 Return an expression explicitly showing all conversions required to
8458 change RETVAL into the function return type, and to assign it to
8459 the DECL_RESULT for the function. Set *NO_WARNING to true if
8460 code reaches end of non-void function warning shouldn't be issued
8461 on this RETURN_EXPR. */
8463 tree
8464 check_return_expr (tree retval, bool *no_warning)
8466 tree result;
8467 /* The type actually returned by the function. */
8468 tree valtype;
8469 /* The type the function is declared to return, or void if
8470 the declared type is incomplete. */
8471 tree functype;
8472 int fn_returns_value_p;
8473 bool named_return_value_okay_p;
8475 *no_warning = false;
8477 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8479 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8480 "statement is not allowed");
8481 return NULL_TREE;
8484 /* A `volatile' function is one that isn't supposed to return, ever.
8485 (This is a G++ extension, used to get better code for functions
8486 that call the `volatile' function.) */
8487 if (TREE_THIS_VOLATILE (current_function_decl))
8488 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8490 /* Check for various simple errors. */
8491 if (DECL_DESTRUCTOR_P (current_function_decl))
8493 if (retval)
8494 error ("returning a value from a destructor");
8495 return NULL_TREE;
8497 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8499 if (in_function_try_handler)
8500 /* If a return statement appears in a handler of the
8501 function-try-block of a constructor, the program is ill-formed. */
8502 error ("cannot return from a handler of a function-try-block of a constructor");
8503 else if (retval)
8504 /* You can't return a value from a constructor. */
8505 error ("returning a value from a constructor");
8506 return NULL_TREE;
8509 if (processing_template_decl)
8511 current_function_returns_value = 1;
8512 if (check_for_bare_parameter_packs (retval))
8513 retval = error_mark_node;
8514 return retval;
8517 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8519 /* Deduce auto return type from a return statement. */
8520 if (current_function_auto_return_pattern)
8522 tree auto_node;
8523 tree type;
8525 if (!retval && !is_auto (current_function_auto_return_pattern))
8527 /* Give a helpful error message. */
8528 error ("return-statement with no value, in function returning %qT",
8529 current_function_auto_return_pattern);
8530 inform (input_location, "only plain %<auto%> return type can be "
8531 "deduced to %<void%>");
8532 type = error_mark_node;
8534 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8536 error ("returning initializer list");
8537 type = error_mark_node;
8539 else
8541 if (!retval)
8542 retval = void_node;
8543 auto_node = type_uses_auto (current_function_auto_return_pattern);
8544 type = do_auto_deduction (current_function_auto_return_pattern,
8545 retval, auto_node);
8548 if (type == error_mark_node)
8549 /* Leave it. */;
8550 else if (functype == current_function_auto_return_pattern)
8551 apply_deduced_return_type (current_function_decl, type);
8552 else
8553 /* A mismatch should have been diagnosed in do_auto_deduction. */
8554 gcc_assert (same_type_p (type, functype));
8555 functype = type;
8558 /* When no explicit return-value is given in a function with a named
8559 return value, the named return value is used. */
8560 result = DECL_RESULT (current_function_decl);
8561 valtype = TREE_TYPE (result);
8562 gcc_assert (valtype != NULL_TREE);
8563 fn_returns_value_p = !VOID_TYPE_P (valtype);
8564 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8565 retval = result;
8567 /* Check for a return statement with no return value in a function
8568 that's supposed to return a value. */
8569 if (!retval && fn_returns_value_p)
8571 if (functype != error_mark_node)
8572 permerror (input_location, "return-statement with no value, in "
8573 "function returning %qT", valtype);
8574 /* Remember that this function did return. */
8575 current_function_returns_value = 1;
8576 /* And signal caller that TREE_NO_WARNING should be set on the
8577 RETURN_EXPR to avoid control reaches end of non-void function
8578 warnings in tree-cfg.c. */
8579 *no_warning = true;
8581 /* Check for a return statement with a value in a function that
8582 isn't supposed to return a value. */
8583 else if (retval && !fn_returns_value_p)
8585 if (VOID_TYPE_P (TREE_TYPE (retval)))
8586 /* You can return a `void' value from a function of `void'
8587 type. In that case, we have to evaluate the expression for
8588 its side-effects. */
8589 finish_expr_stmt (retval);
8590 else
8591 permerror (input_location, "return-statement with a value, in function "
8592 "returning 'void'");
8593 current_function_returns_null = 1;
8595 /* There's really no value to return, after all. */
8596 return NULL_TREE;
8598 else if (!retval)
8599 /* Remember that this function can sometimes return without a
8600 value. */
8601 current_function_returns_null = 1;
8602 else
8603 /* Remember that this function did return a value. */
8604 current_function_returns_value = 1;
8606 /* Check for erroneous operands -- but after giving ourselves a
8607 chance to provide an error about returning a value from a void
8608 function. */
8609 if (error_operand_p (retval))
8611 current_function_return_value = error_mark_node;
8612 return error_mark_node;
8615 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8616 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8617 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8618 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8619 && ! flag_check_new
8620 && retval && null_ptr_cst_p (retval))
8621 warning (0, "%<operator new%> must not return NULL unless it is "
8622 "declared %<throw()%> (or -fcheck-new is in effect)");
8624 /* Effective C++ rule 15. See also start_function. */
8625 if (warn_ecpp
8626 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8628 bool warn = true;
8630 /* The function return type must be a reference to the current
8631 class. */
8632 if (TREE_CODE (valtype) == REFERENCE_TYPE
8633 && same_type_ignoring_top_level_qualifiers_p
8634 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8636 /* Returning '*this' is obviously OK. */
8637 if (retval == current_class_ref)
8638 warn = false;
8639 /* If we are calling a function whose return type is the same of
8640 the current class reference, it is ok. */
8641 else if (INDIRECT_REF_P (retval)
8642 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8643 warn = false;
8646 if (warn)
8647 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8650 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8652 [...] For a function with a class return type, if the expression
8653 in the return statement is the name of a local object, and the cv-
8654 unqualified type of the local object is the same as the function
8655 return type, an implementation is permitted to omit creating the tem-
8656 porary object to hold the function return value [...]
8658 So, if this is a value-returning function that always returns the same
8659 local variable, remember it.
8661 It might be nice to be more flexible, and choose the first suitable
8662 variable even if the function sometimes returns something else, but
8663 then we run the risk of clobbering the variable we chose if the other
8664 returned expression uses the chosen variable somehow. And people expect
8665 this restriction, anyway. (jason 2000-11-19)
8667 See finish_function and finalize_nrv for the rest of this optimization. */
8669 named_return_value_okay_p =
8670 (retval != NULL_TREE
8671 /* Must be a local, automatic variable. */
8672 && VAR_P (retval)
8673 && DECL_CONTEXT (retval) == current_function_decl
8674 && ! TREE_STATIC (retval)
8675 /* And not a lambda or anonymous union proxy. */
8676 && !DECL_HAS_VALUE_EXPR_P (retval)
8677 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8678 /* The cv-unqualified type of the returned value must be the
8679 same as the cv-unqualified return type of the
8680 function. */
8681 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8682 (TYPE_MAIN_VARIANT (functype)))
8683 /* And the returned value must be non-volatile. */
8684 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8686 if (fn_returns_value_p && flag_elide_constructors)
8688 if (named_return_value_okay_p
8689 && (current_function_return_value == NULL_TREE
8690 || current_function_return_value == retval))
8691 current_function_return_value = retval;
8692 else
8693 current_function_return_value = error_mark_node;
8696 /* We don't need to do any conversions when there's nothing being
8697 returned. */
8698 if (!retval)
8699 return NULL_TREE;
8701 /* Do any required conversions. */
8702 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8703 /* No conversions are required. */
8705 else
8707 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8709 /* The functype's return type will have been set to void, if it
8710 was an incomplete type. Just treat this as 'return;' */
8711 if (VOID_TYPE_P (functype))
8712 return error_mark_node;
8714 /* If we had an id-expression obfuscated by force_paren_expr, we need
8715 to undo it so we can try to treat it as an rvalue below. */
8716 if (cxx_dialect >= cxx14
8717 && INDIRECT_REF_P (retval)
8718 && REF_PARENTHESIZED_P (retval))
8720 retval = TREE_OPERAND (retval, 0);
8721 while (TREE_CODE (retval) == NON_LVALUE_EXPR
8722 || TREE_CODE (retval) == NOP_EXPR)
8723 retval = TREE_OPERAND (retval, 0);
8724 gcc_assert (TREE_CODE (retval) == ADDR_EXPR);
8725 retval = TREE_OPERAND (retval, 0);
8728 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8729 treated as an rvalue for the purposes of overload resolution to
8730 favor move constructors over copy constructors.
8732 Note that these conditions are similar to, but not as strict as,
8733 the conditions for the named return value optimization. */
8734 if ((cxx_dialect != cxx98)
8735 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8736 || TREE_CODE (retval) == PARM_DECL)
8737 && DECL_CONTEXT (retval) == current_function_decl
8738 && !TREE_STATIC (retval)
8739 /* This is only interesting for class type. */
8740 && CLASS_TYPE_P (functype))
8741 flags = flags | LOOKUP_PREFER_RVALUE;
8743 /* First convert the value to the function's return type, then
8744 to the type of return value's location to handle the
8745 case that functype is smaller than the valtype. */
8746 retval = convert_for_initialization
8747 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8748 tf_warning_or_error);
8749 retval = convert (valtype, retval);
8751 /* If the conversion failed, treat this just like `return;'. */
8752 if (retval == error_mark_node)
8753 return retval;
8754 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8755 else if (! cfun->returns_struct
8756 && TREE_CODE (retval) == TARGET_EXPR
8757 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8758 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8759 TREE_OPERAND (retval, 0));
8760 else if (maybe_warn_about_returning_address_of_local (retval))
8761 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8762 build_zero_cst (TREE_TYPE (retval)));
8765 /* Actually copy the value returned into the appropriate location. */
8766 if (retval && retval != result)
8767 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8769 return retval;
8773 /* Returns nonzero if the pointer-type FROM can be converted to the
8774 pointer-type TO via a qualification conversion. If CONSTP is -1,
8775 then we return nonzero if the pointers are similar, and the
8776 cv-qualification signature of FROM is a proper subset of that of TO.
8778 If CONSTP is positive, then all outer pointers have been
8779 const-qualified. */
8781 static int
8782 comp_ptr_ttypes_real (tree to, tree from, int constp)
8784 bool to_more_cv_qualified = false;
8785 bool is_opaque_pointer = false;
8787 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8789 if (TREE_CODE (to) != TREE_CODE (from))
8790 return 0;
8792 if (TREE_CODE (from) == OFFSET_TYPE
8793 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8794 TYPE_OFFSET_BASETYPE (to)))
8795 return 0;
8797 /* Const and volatile mean something different for function types,
8798 so the usual checks are not appropriate. */
8799 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8801 if (!at_least_as_qualified_p (to, from))
8802 return 0;
8804 if (!at_least_as_qualified_p (from, to))
8806 if (constp == 0)
8807 return 0;
8808 to_more_cv_qualified = true;
8811 if (constp > 0)
8812 constp &= TYPE_READONLY (to);
8815 if (TREE_CODE (to) == VECTOR_TYPE)
8816 is_opaque_pointer = vector_targets_convertible_p (to, from);
8818 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8819 return ((constp >= 0 || to_more_cv_qualified)
8820 && (is_opaque_pointer
8821 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8825 /* When comparing, say, char ** to char const **, this function takes
8826 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8827 types to this function. */
8830 comp_ptr_ttypes (tree to, tree from)
8832 return comp_ptr_ttypes_real (to, from, 1);
8835 /* Returns true iff FNTYPE is a non-class type that involves
8836 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8837 if a parameter type is ill-formed. */
8839 bool
8840 error_type_p (const_tree type)
8842 tree t;
8844 switch (TREE_CODE (type))
8846 case ERROR_MARK:
8847 return true;
8849 case POINTER_TYPE:
8850 case REFERENCE_TYPE:
8851 case OFFSET_TYPE:
8852 return error_type_p (TREE_TYPE (type));
8854 case FUNCTION_TYPE:
8855 case METHOD_TYPE:
8856 if (error_type_p (TREE_TYPE (type)))
8857 return true;
8858 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8859 if (error_type_p (TREE_VALUE (t)))
8860 return true;
8861 return false;
8863 case RECORD_TYPE:
8864 if (TYPE_PTRMEMFUNC_P (type))
8865 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8866 return false;
8868 default:
8869 return false;
8873 /* Returns true if to and from are (possibly multi-level) pointers to the same
8874 type or inheritance-related types, regardless of cv-quals. */
8876 bool
8877 ptr_reasonably_similar (const_tree to, const_tree from)
8879 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8881 /* Any target type is similar enough to void. */
8882 if (VOID_TYPE_P (to))
8883 return !error_type_p (from);
8884 if (VOID_TYPE_P (from))
8885 return !error_type_p (to);
8887 if (TREE_CODE (to) != TREE_CODE (from))
8888 return false;
8890 if (TREE_CODE (from) == OFFSET_TYPE
8891 && comptypes (TYPE_OFFSET_BASETYPE (to),
8892 TYPE_OFFSET_BASETYPE (from),
8893 COMPARE_BASE | COMPARE_DERIVED))
8894 continue;
8896 if (TREE_CODE (to) == VECTOR_TYPE
8897 && vector_types_convertible_p (to, from, false))
8898 return true;
8900 if (TREE_CODE (to) == INTEGER_TYPE
8901 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8902 return true;
8904 if (TREE_CODE (to) == FUNCTION_TYPE)
8905 return !error_type_p (to) && !error_type_p (from);
8907 if (!TYPE_PTR_P (to))
8909 /* When either type is incomplete avoid DERIVED_FROM_P,
8910 which may call complete_type (c++/57942). */
8911 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8912 return comptypes
8913 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8914 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8919 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8920 pointer-to-member types) are the same, ignoring cv-qualification at
8921 all levels. */
8923 bool
8924 comp_ptr_ttypes_const (tree to, tree from)
8926 bool is_opaque_pointer = false;
8928 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8930 if (TREE_CODE (to) != TREE_CODE (from))
8931 return false;
8933 if (TREE_CODE (from) == OFFSET_TYPE
8934 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8935 TYPE_OFFSET_BASETYPE (to)))
8936 continue;
8938 if (TREE_CODE (to) == VECTOR_TYPE)
8939 is_opaque_pointer = vector_targets_convertible_p (to, from);
8941 if (!TYPE_PTR_P (to))
8942 return (is_opaque_pointer
8943 || same_type_ignoring_top_level_qualifiers_p (to, from));
8947 /* Returns the type qualifiers for this type, including the qualifiers on the
8948 elements for an array type. */
8951 cp_type_quals (const_tree type)
8953 int quals;
8954 /* This CONST_CAST is okay because strip_array_types returns its
8955 argument unmodified and we assign it to a const_tree. */
8956 type = strip_array_types (CONST_CAST_TREE (type));
8957 if (type == error_mark_node
8958 /* Quals on a FUNCTION_TYPE are memfn quals. */
8959 || TREE_CODE (type) == FUNCTION_TYPE)
8960 return TYPE_UNQUALIFIED;
8961 quals = TYPE_QUALS (type);
8962 /* METHOD and REFERENCE_TYPEs should never have quals. */
8963 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8964 && TREE_CODE (type) != REFERENCE_TYPE)
8965 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8966 == TYPE_UNQUALIFIED));
8967 return quals;
8970 /* Returns the function-ref-qualifier for TYPE */
8972 cp_ref_qualifier
8973 type_memfn_rqual (const_tree type)
8975 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8976 || TREE_CODE (type) == METHOD_TYPE);
8978 if (!FUNCTION_REF_QUALIFIED (type))
8979 return REF_QUAL_NONE;
8980 else if (FUNCTION_RVALUE_QUALIFIED (type))
8981 return REF_QUAL_RVALUE;
8982 else
8983 return REF_QUAL_LVALUE;
8986 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8987 METHOD_TYPE. */
8990 type_memfn_quals (const_tree type)
8992 if (TREE_CODE (type) == FUNCTION_TYPE)
8993 return TYPE_QUALS (type);
8994 else if (TREE_CODE (type) == METHOD_TYPE)
8995 return cp_type_quals (class_of_this_parm (type));
8996 else
8997 gcc_unreachable ();
9000 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9001 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9003 tree
9004 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9006 /* Could handle METHOD_TYPE here if necessary. */
9007 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9008 if (TYPE_QUALS (type) == memfn_quals
9009 && type_memfn_rqual (type) == rqual)
9010 return type;
9012 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9013 complex. */
9014 tree result = build_qualified_type (type, memfn_quals);
9015 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
9016 return build_ref_qualified_type (result, rqual);
9019 /* Returns nonzero if TYPE is const or volatile. */
9021 bool
9022 cv_qualified_p (const_tree type)
9024 int quals = cp_type_quals (type);
9025 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9028 /* Returns nonzero if the TYPE contains a mutable member. */
9030 bool
9031 cp_has_mutable_p (const_tree type)
9033 /* This CONST_CAST is okay because strip_array_types returns its
9034 argument unmodified and we assign it to a const_tree. */
9035 type = strip_array_types (CONST_CAST_TREE(type));
9037 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9040 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9041 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9042 approximation. In particular, consider:
9044 int f();
9045 struct S { int i; };
9046 const S s = { f(); }
9048 Here, we will make "s" as TREE_READONLY (because it is declared
9049 "const") -- only to reverse ourselves upon seeing that the
9050 initializer is non-constant. */
9052 void
9053 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9055 tree type = TREE_TYPE (decl);
9057 if (type == error_mark_node)
9058 return;
9060 if (TREE_CODE (decl) == TYPE_DECL)
9061 return;
9063 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9064 && type_quals != TYPE_UNQUALIFIED));
9066 /* Avoid setting TREE_READONLY incorrectly. */
9067 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9068 constructor can produce constant init, so rely on cp_finish_decl to
9069 clear TREE_READONLY if the variable has non-constant init. */
9071 /* If the type has (or might have) a mutable component, that component
9072 might be modified. */
9073 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9074 type_quals &= ~TYPE_QUAL_CONST;
9076 c_apply_type_quals_to_decl (type_quals, decl);
9079 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9080 exemplar types such that casting T1 to T2 is casting away constness
9081 if and only if there is no implicit conversion from T1 to T2. */
9083 static void
9084 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9086 int quals1;
9087 int quals2;
9089 /* [expr.const.cast]
9091 For multi-level pointer to members and multi-level mixed pointers
9092 and pointers to members (conv.qual), the "member" aspect of a
9093 pointer to member level is ignored when determining if a const
9094 cv-qualifier has been cast away. */
9095 /* [expr.const.cast]
9097 For two pointer types:
9099 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9100 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9101 K is min(N,M)
9103 casting from X1 to X2 casts away constness if, for a non-pointer
9104 type T there does not exist an implicit conversion (clause
9105 _conv_) from:
9107 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9111 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9112 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9113 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9115 *t1 = cp_build_qualified_type (void_type_node,
9116 cp_type_quals (*t1));
9117 *t2 = cp_build_qualified_type (void_type_node,
9118 cp_type_quals (*t2));
9119 return;
9122 quals1 = cp_type_quals (*t1);
9123 quals2 = cp_type_quals (*t2);
9125 if (TYPE_PTRDATAMEM_P (*t1))
9126 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9127 else
9128 *t1 = TREE_TYPE (*t1);
9129 if (TYPE_PTRDATAMEM_P (*t2))
9130 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9131 else
9132 *t2 = TREE_TYPE (*t2);
9134 casts_away_constness_r (t1, t2, complain);
9135 *t1 = build_pointer_type (*t1);
9136 *t2 = build_pointer_type (*t2);
9137 *t1 = cp_build_qualified_type (*t1, quals1);
9138 *t2 = cp_build_qualified_type (*t2, quals2);
9141 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9142 constness.
9144 ??? This function returns non-zero if casting away qualifiers not
9145 just const. We would like to return to the caller exactly which
9146 qualifiers are casted away to give more accurate diagnostics.
9149 static bool
9150 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9152 if (TREE_CODE (t2) == REFERENCE_TYPE)
9154 /* [expr.const.cast]
9156 Casting from an lvalue of type T1 to an lvalue of type T2
9157 using a reference cast casts away constness if a cast from an
9158 rvalue of type "pointer to T1" to the type "pointer to T2"
9159 casts away constness. */
9160 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9161 return casts_away_constness (build_pointer_type (t1),
9162 build_pointer_type (TREE_TYPE (t2)),
9163 complain);
9166 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9167 /* [expr.const.cast]
9169 Casting from an rvalue of type "pointer to data member of X
9170 of type T1" to the type "pointer to data member of Y of type
9171 T2" casts away constness if a cast from an rvalue of type
9172 "pointer to T1" to the type "pointer to T2" casts away
9173 constness. */
9174 return casts_away_constness
9175 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9176 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9177 complain);
9179 /* Casting away constness is only something that makes sense for
9180 pointer or reference types. */
9181 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9182 return false;
9184 /* Top-level qualifiers don't matter. */
9185 t1 = TYPE_MAIN_VARIANT (t1);
9186 t2 = TYPE_MAIN_VARIANT (t2);
9187 casts_away_constness_r (&t1, &t2, complain);
9188 if (!can_convert (t2, t1, complain))
9189 return true;
9191 return false;
9194 /* If T is a REFERENCE_TYPE return the type to which T refers.
9195 Otherwise, return T itself. */
9197 tree
9198 non_reference (tree t)
9200 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9201 t = TREE_TYPE (t);
9202 return t;
9206 /* Return nonzero if REF is an lvalue valid for this language;
9207 otherwise, print an error message and return zero. USE says
9208 how the lvalue is being used and so selects the error message. */
9211 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9213 cp_lvalue_kind kind = lvalue_kind (ref);
9215 if (kind == clk_none)
9217 if (complain & tf_error)
9218 lvalue_error (input_location, use);
9219 return 0;
9221 else if (kind & (clk_rvalueref|clk_class))
9223 if (!(complain & tf_error))
9224 return 0;
9225 if (kind & clk_class)
9226 /* Make this a permerror because we used to accept it. */
9227 permerror (input_location, "using temporary as lvalue");
9228 else
9229 error ("using xvalue (rvalue reference) as lvalue");
9231 return 1;
9234 /* Return true if a user-defined literal operator is a raw operator. */
9236 bool
9237 check_raw_literal_operator (const_tree decl)
9239 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9240 tree argtype;
9241 int arity;
9242 bool maybe_raw_p = false;
9244 /* Count the number and type of arguments and check for ellipsis. */
9245 for (argtype = argtypes, arity = 0;
9246 argtype && argtype != void_list_node;
9247 ++arity, argtype = TREE_CHAIN (argtype))
9249 tree t = TREE_VALUE (argtype);
9251 if (same_type_p (t, const_string_type_node))
9252 maybe_raw_p = true;
9254 if (!argtype)
9255 return false; /* Found ellipsis. */
9257 if (!maybe_raw_p || arity != 1)
9258 return false;
9260 return true;
9264 /* Return true if a user-defined literal operator has one of the allowed
9265 argument types. */
9267 bool
9268 check_literal_operator_args (const_tree decl,
9269 bool *long_long_unsigned_p, bool *long_double_p)
9271 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9273 *long_long_unsigned_p = false;
9274 *long_double_p = false;
9275 if (processing_template_decl || processing_specialization)
9276 return argtypes == void_list_node;
9277 else
9279 tree argtype;
9280 int arity;
9281 int max_arity = 2;
9283 /* Count the number and type of arguments and check for ellipsis. */
9284 for (argtype = argtypes, arity = 0;
9285 argtype && argtype != void_list_node;
9286 argtype = TREE_CHAIN (argtype))
9288 tree t = TREE_VALUE (argtype);
9289 ++arity;
9291 if (TYPE_PTR_P (t))
9293 bool maybe_raw_p = false;
9294 t = TREE_TYPE (t);
9295 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9296 return false;
9297 t = TYPE_MAIN_VARIANT (t);
9298 if ((maybe_raw_p = same_type_p (t, char_type_node))
9299 || same_type_p (t, wchar_type_node)
9300 || same_type_p (t, char16_type_node)
9301 || same_type_p (t, char32_type_node))
9303 argtype = TREE_CHAIN (argtype);
9304 if (!argtype)
9305 return false;
9306 t = TREE_VALUE (argtype);
9307 if (maybe_raw_p && argtype == void_list_node)
9308 return true;
9309 else if (same_type_p (t, size_type_node))
9311 ++arity;
9312 continue;
9314 else
9315 return false;
9318 else if (same_type_p (t, long_long_unsigned_type_node))
9320 max_arity = 1;
9321 *long_long_unsigned_p = true;
9323 else if (same_type_p (t, long_double_type_node))
9325 max_arity = 1;
9326 *long_double_p = true;
9328 else if (same_type_p (t, char_type_node))
9329 max_arity = 1;
9330 else if (same_type_p (t, wchar_type_node))
9331 max_arity = 1;
9332 else if (same_type_p (t, char16_type_node))
9333 max_arity = 1;
9334 else if (same_type_p (t, char32_type_node))
9335 max_arity = 1;
9336 else
9337 return false;
9339 if (!argtype)
9340 return false; /* Found ellipsis. */
9342 if (arity != max_arity)
9343 return false;
9345 return true;