Introduce gimple_omp_task
[official-gcc.git] / gcc / cp / typeck.c
blobb4e6824796b26b2ecb03a9794d221eccaf72e294
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "diagnostic.h"
37 #include "intl.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "c-family/c-ubsan.h"
43 #include "params.h"
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
48 tsubst_flags_t, int);
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
50 static tree rationalize_conditional_expr (enum tree_code, tree,
51 tsubst_flags_t);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
58 static bool casts_away_constness (tree, tree, tsubst_flags_t);
59 static bool maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
63 tsubst_flags_t);
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
70 tree
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
73 tree type;
75 if (processing_template_decl || value == error_mark_node)
76 return value;
78 if (TREE_CODE (value) == OVERLOAD)
79 type = unknown_type_node;
80 else
81 type = TREE_TYPE (value);
83 if (type == error_mark_node)
84 return error_mark_node;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type))
88 return value;
90 if (complete_type_or_maybe_complain (type, value, complain))
91 return value;
92 else
93 return error_mark_node;
96 tree
97 require_complete_type (tree value)
99 return require_complete_type_sfinae (value, tf_warning_or_error);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
107 tree
108 complete_type (tree type)
110 if (type == NULL_TREE)
111 /* Rather than crash, we return something sure to cause an error
112 at some point. */
113 return error_mark_node;
115 if (type == error_mark_node || COMPLETE_TYPE_P (type))
117 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
119 tree t = complete_type (TREE_TYPE (type));
120 unsigned int needs_constructing, has_nontrivial_dtor;
121 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
122 layout_type (type);
123 needs_constructing
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
125 has_nontrivial_dtor
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
129 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
136 return type;
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
143 tree
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
146 type = complete_type (type);
147 if (type == error_mark_node)
148 /* We already issued an error. */
149 return NULL_TREE;
150 else if (!COMPLETE_TYPE_P (type))
152 if (complain & tf_error)
153 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
154 return NULL_TREE;
156 else
157 return type;
160 tree
161 complete_type_or_else (tree type, tree value)
163 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
166 /* Return truthvalue of whether type of EXP is instantiated. */
169 type_unknown_p (const_tree exp)
171 return (TREE_CODE (exp) == TREE_LIST
172 || TREE_TYPE (exp) == unknown_type_node);
176 /* Return the common type of two parameter lists.
177 We assume that comptypes has already been done and returned 1;
178 if that isn't so, this may crash.
180 As an optimization, free the space we allocate if the parameter
181 lists are already common. */
183 static tree
184 commonparms (tree p1, tree p2)
186 tree oldargs = p1, newargs, n;
187 int i, len;
188 int any_change = 0;
190 len = list_length (p1);
191 newargs = tree_last (p1);
193 if (newargs == void_list_node)
194 i = 1;
195 else
197 i = 0;
198 newargs = 0;
201 for (; i < len; i++)
202 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
204 n = newargs;
206 for (i = 0; p1;
207 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
209 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
212 any_change = 1;
214 else if (! TREE_PURPOSE (p1))
216 if (TREE_PURPOSE (p2))
218 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219 any_change = 1;
222 else
224 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
225 any_change = 1;
226 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
228 if (TREE_VALUE (p1) != TREE_VALUE (p2))
230 any_change = 1;
231 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
233 else
234 TREE_VALUE (n) = TREE_VALUE (p1);
236 if (! any_change)
237 return oldargs;
239 return newargs;
242 /* Given a type, perhaps copied for a typedef,
243 find the "original" version of it. */
244 static tree
245 original_type (tree t)
247 int quals = cp_type_quals (t);
248 while (t != error_mark_node
249 && TYPE_NAME (t) != NULL_TREE)
251 tree x = TYPE_NAME (t);
252 if (TREE_CODE (x) != TYPE_DECL)
253 break;
254 x = DECL_ORIGINAL_TYPE (x);
255 if (x == NULL_TREE)
256 break;
257 t = x;
259 return cp_build_qualified_type (t, quals);
262 /* Return the common type for two arithmetic types T1 and T2 under the
263 usual arithmetic conversions. The default conversions have already
264 been applied, and enumerated types converted to their compatible
265 integer types. */
267 static tree
268 cp_common_type (tree t1, tree t2)
270 enum tree_code code1 = TREE_CODE (t1);
271 enum tree_code code2 = TREE_CODE (t2);
272 tree attributes;
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
277 attributes. */
278 attributes = (*targetm.merge_type_attributes) (t1, t2);
280 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
282 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283 return build_type_attribute_variant (t1, attributes);
284 else
285 return NULL_TREE;
288 /* FIXME: Attributes. */
289 gcc_assert (ARITHMETIC_TYPE_P (t1)
290 || TREE_CODE (t1) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t1));
292 gcc_assert (ARITHMETIC_TYPE_P (t2)
293 || TREE_CODE (t2) == VECTOR_TYPE
294 || UNSCOPED_ENUM_P (t2));
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
298 required type. */
299 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
301 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
303 tree subtype
304 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
306 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307 return build_type_attribute_variant (t1, attributes);
308 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309 return build_type_attribute_variant (t2, attributes);
310 else
311 return build_type_attribute_variant (build_complex_type (subtype),
312 attributes);
315 if (code1 == VECTOR_TYPE)
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1))
320 return build_type_attribute_variant (t1, attributes);
321 else
322 return build_type_attribute_variant (t2, attributes);
325 /* If only one is real, use it as the result. */
326 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327 return build_type_attribute_variant (t1, attributes);
328 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329 return build_type_attribute_variant (t2, attributes);
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333 return build_type_attribute_variant (t1, attributes);
334 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335 return build_type_attribute_variant (t2, attributes);
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339 return build_type_attribute_variant (t1, attributes);
341 if (code1 != REAL_TYPE)
343 /* If one is unsigned long long, then convert the other to unsigned
344 long long. */
345 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347 return build_type_attribute_variant (long_long_unsigned_type_node,
348 attributes);
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
353 other to long long.
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
358 case. */
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
362 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363 ? long_long_unsigned_type_node
364 : long_long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
367 if (int128_integer_type_node != NULL_TREE
368 && (same_type_p (TYPE_MAIN_VARIANT (t1),
369 int128_integer_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2),
371 int128_integer_type_node)))
373 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
374 ? int128_unsigned_type_node
375 : int128_integer_type_node);
376 return build_type_attribute_variant (t, attributes);
379 /* Go through the same procedure, but for longs. */
380 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
381 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
382 return build_type_attribute_variant (long_unsigned_type_node,
383 attributes);
384 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? long_unsigned_type_node : long_integer_type_node);
389 return build_type_attribute_variant (t, attributes);
391 /* Otherwise prefer the unsigned one. */
392 if (TYPE_UNSIGNED (t1))
393 return build_type_attribute_variant (t1, attributes);
394 else
395 return build_type_attribute_variant (t2, attributes);
397 else
399 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
400 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
401 return build_type_attribute_variant (long_double_type_node,
402 attributes);
403 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
405 return build_type_attribute_variant (double_type_node,
406 attributes);
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
409 return build_type_attribute_variant (float_type_node,
410 attributes);
412 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
413 the standard C++ floating-point types. Logic earlier in this
414 function has already eliminated the possibility that
415 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
416 compelling reason to choose one or the other. */
417 return build_type_attribute_variant (t1, attributes);
421 /* T1 and T2 are arithmetic or enumeration types. Return the type
422 that will result from the "usual arithmetic conversions" on T1 and
423 T2 as described in [expr]. */
425 tree
426 type_after_usual_arithmetic_conversions (tree t1, tree t2)
428 gcc_assert (ARITHMETIC_TYPE_P (t1)
429 || TREE_CODE (t1) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t1));
431 gcc_assert (ARITHMETIC_TYPE_P (t2)
432 || TREE_CODE (t2) == VECTOR_TYPE
433 || UNSCOPED_ENUM_P (t2));
435 /* Perform the integral promotions. We do not promote real types here. */
436 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
437 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
439 t1 = type_promotes_to (t1);
440 t2 = type_promotes_to (t2);
443 return cp_common_type (t1, t2);
446 static void
447 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
448 composite_pointer_operation operation)
450 switch (operation)
452 case CPO_COMPARISON:
453 emit_diagnostic (kind, input_location, 0,
454 "comparison between "
455 "distinct pointer types %qT and %qT lacks a cast",
456 t1, t2);
457 break;
458 case CPO_CONVERSION:
459 emit_diagnostic (kind, input_location, 0,
460 "conversion between "
461 "distinct pointer types %qT and %qT lacks a cast",
462 t1, t2);
463 break;
464 case CPO_CONDITIONAL_EXPR:
465 emit_diagnostic (kind, input_location, 0,
466 "conditional expression between "
467 "distinct pointer types %qT and %qT lacks a cast",
468 t1, t2);
469 break;
470 default:
471 gcc_unreachable ();
475 /* Subroutine of composite_pointer_type to implement the recursive
476 case. See that function for documentation of the parameters. */
478 static tree
479 composite_pointer_type_r (tree t1, tree t2,
480 composite_pointer_operation operation,
481 tsubst_flags_t complain)
483 tree pointee1;
484 tree pointee2;
485 tree result_type;
486 tree attributes;
488 /* Determine the types pointed to by T1 and T2. */
489 if (TYPE_PTR_P (t1))
491 pointee1 = TREE_TYPE (t1);
492 pointee2 = TREE_TYPE (t2);
494 else
496 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
497 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
500 /* [expr.rel]
502 Otherwise, the composite pointer type is a pointer type
503 similar (_conv.qual_) to the type of one of the operands,
504 with a cv-qualification signature (_conv.qual_) that is the
505 union of the cv-qualification signatures of the operand
506 types. */
507 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
508 result_type = pointee1;
509 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
510 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
512 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
513 complain);
514 if (result_type == error_mark_node)
515 return error_mark_node;
517 else
519 if (complain & tf_error)
520 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
521 else
522 return error_mark_node;
523 result_type = void_type_node;
525 result_type = cp_build_qualified_type (result_type,
526 (cp_type_quals (pointee1)
527 | cp_type_quals (pointee2)));
528 /* If the original types were pointers to members, so is the
529 result. */
530 if (TYPE_PTRMEM_P (t1))
532 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
533 TYPE_PTRMEM_CLASS_TYPE (t2)))
535 if (complain & tf_error)
536 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
537 else
538 return error_mark_node;
540 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
541 result_type);
543 else
544 result_type = build_pointer_type (result_type);
546 /* Merge the attributes. */
547 attributes = (*targetm.merge_type_attributes) (t1, t2);
548 return build_type_attribute_variant (result_type, attributes);
551 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
552 ARG1 and ARG2 are the values with those types. The OPERATION is to
553 describe the operation between the pointer types,
554 in case an error occurs.
556 This routine also implements the computation of a common type for
557 pointers-to-members as per [expr.eq]. */
559 tree
560 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
561 composite_pointer_operation operation,
562 tsubst_flags_t complain)
564 tree class1;
565 tree class2;
567 /* [expr.rel]
569 If one operand is a null pointer constant, the composite pointer
570 type is the type of the other operand. */
571 if (null_ptr_cst_p (arg1))
572 return t2;
573 if (null_ptr_cst_p (arg2))
574 return t1;
576 /* We have:
578 [expr.rel]
580 If one of the operands has type "pointer to cv1 void*", then
581 the other has type "pointer to cv2T", and the composite pointer
582 type is "pointer to cv12 void", where cv12 is the union of cv1
583 and cv2.
585 If either type is a pointer to void, make sure it is T1. */
586 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
588 tree t;
589 t = t1;
590 t1 = t2;
591 t2 = t;
594 /* Now, if T1 is a pointer to void, merge the qualifiers. */
595 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
597 tree attributes;
598 tree result_type;
600 if (TYPE_PTRFN_P (t2))
602 if (complain & tf_error)
604 switch (operation)
606 case CPO_COMPARISON:
607 pedwarn (input_location, OPT_Wpedantic,
608 "ISO C++ forbids comparison between pointer "
609 "of type %<void *%> and pointer-to-function");
610 break;
611 case CPO_CONVERSION:
612 pedwarn (input_location, OPT_Wpedantic,
613 "ISO C++ forbids conversion between pointer "
614 "of type %<void *%> and pointer-to-function");
615 break;
616 case CPO_CONDITIONAL_EXPR:
617 pedwarn (input_location, OPT_Wpedantic,
618 "ISO C++ forbids conditional expression between "
619 "pointer of type %<void *%> and "
620 "pointer-to-function");
621 break;
622 default:
623 gcc_unreachable ();
626 else
627 return error_mark_node;
629 result_type
630 = cp_build_qualified_type (void_type_node,
631 (cp_type_quals (TREE_TYPE (t1))
632 | cp_type_quals (TREE_TYPE (t2))));
633 result_type = build_pointer_type (result_type);
634 /* Merge the attributes. */
635 attributes = (*targetm.merge_type_attributes) (t1, t2);
636 return build_type_attribute_variant (result_type, attributes);
639 if (c_dialect_objc () && TYPE_PTR_P (t1)
640 && TYPE_PTR_P (t2))
642 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
643 return objc_common_type (t1, t2);
646 /* [expr.eq] permits the application of a pointer conversion to
647 bring the pointers to a common type. */
648 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
649 && CLASS_TYPE_P (TREE_TYPE (t1))
650 && CLASS_TYPE_P (TREE_TYPE (t2))
651 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
652 TREE_TYPE (t2)))
654 class1 = TREE_TYPE (t1);
655 class2 = TREE_TYPE (t2);
657 if (DERIVED_FROM_P (class1, class2))
658 t2 = (build_pointer_type
659 (cp_build_qualified_type (class1, cp_type_quals (class2))));
660 else if (DERIVED_FROM_P (class2, class1))
661 t1 = (build_pointer_type
662 (cp_build_qualified_type (class2, cp_type_quals (class1))));
663 else
665 if (complain & tf_error)
666 composite_pointer_error (DK_ERROR, t1, t2, operation);
667 return error_mark_node;
670 /* [expr.eq] permits the application of a pointer-to-member
671 conversion to change the class type of one of the types. */
672 else if (TYPE_PTRMEM_P (t1)
673 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
674 TYPE_PTRMEM_CLASS_TYPE (t2)))
676 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
677 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
679 if (DERIVED_FROM_P (class1, class2))
680 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
681 else if (DERIVED_FROM_P (class2, class1))
682 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
683 else
685 if (complain & tf_error)
686 switch (operation)
688 case CPO_COMPARISON:
689 error ("comparison between distinct "
690 "pointer-to-member types %qT and %qT lacks a cast",
691 t1, t2);
692 break;
693 case CPO_CONVERSION:
694 error ("conversion between distinct "
695 "pointer-to-member types %qT and %qT lacks a cast",
696 t1, t2);
697 break;
698 case CPO_CONDITIONAL_EXPR:
699 error ("conditional expression between distinct "
700 "pointer-to-member types %qT and %qT lacks a cast",
701 t1, t2);
702 break;
703 default:
704 gcc_unreachable ();
706 return error_mark_node;
710 return composite_pointer_type_r (t1, t2, operation, complain);
713 /* Return the merged type of two types.
714 We assume that comptypes has already been done and returned 1;
715 if that isn't so, this may crash.
717 This just combines attributes and default arguments; any other
718 differences would cause the two types to compare unalike. */
720 tree
721 merge_types (tree t1, tree t2)
723 enum tree_code code1;
724 enum tree_code code2;
725 tree attributes;
727 /* Save time if the two types are the same. */
728 if (t1 == t2)
729 return t1;
730 if (original_type (t1) == original_type (t2))
731 return t1;
733 /* If one type is nonsense, use the other. */
734 if (t1 == error_mark_node)
735 return t2;
736 if (t2 == error_mark_node)
737 return t1;
739 /* Handle merging an auto redeclaration with a previous deduced
740 return type. */
741 if (is_auto (t1))
742 return t2;
744 /* Merge the attributes. */
745 attributes = (*targetm.merge_type_attributes) (t1, t2);
747 if (TYPE_PTRMEMFUNC_P (t1))
748 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
749 if (TYPE_PTRMEMFUNC_P (t2))
750 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
752 code1 = TREE_CODE (t1);
753 code2 = TREE_CODE (t2);
754 if (code1 != code2)
756 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
757 if (code1 == TYPENAME_TYPE)
759 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
760 code1 = TREE_CODE (t1);
762 else
764 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
765 code2 = TREE_CODE (t2);
769 switch (code1)
771 case POINTER_TYPE:
772 case REFERENCE_TYPE:
773 /* For two pointers, do this recursively on the target type. */
775 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
776 int quals = cp_type_quals (t1);
778 if (code1 == POINTER_TYPE)
779 t1 = build_pointer_type (target);
780 else
781 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
782 t1 = build_type_attribute_variant (t1, attributes);
783 t1 = cp_build_qualified_type (t1, quals);
785 if (TREE_CODE (target) == METHOD_TYPE)
786 t1 = build_ptrmemfunc_type (t1);
788 return t1;
791 case OFFSET_TYPE:
793 int quals;
794 tree pointee;
795 quals = cp_type_quals (t1);
796 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
797 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
798 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
799 pointee);
800 t1 = cp_build_qualified_type (t1, quals);
801 break;
804 case ARRAY_TYPE:
806 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
807 /* Save space: see if the result is identical to one of the args. */
808 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
809 return build_type_attribute_variant (t1, attributes);
810 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
811 return build_type_attribute_variant (t2, attributes);
812 /* Merge the element types, and have a size if either arg has one. */
813 t1 = build_cplus_array_type
814 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
815 break;
818 case FUNCTION_TYPE:
819 /* Function types: prefer the one that specified arg types.
820 If both do, merge the arg types. Also merge the return types. */
822 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
823 tree p1 = TYPE_ARG_TYPES (t1);
824 tree p2 = TYPE_ARG_TYPES (t2);
825 tree parms;
826 tree rval, raises;
827 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
829 /* Save space: see if the result is identical to one of the args. */
830 if (valtype == TREE_TYPE (t1) && ! p2)
831 return cp_build_type_attribute_variant (t1, attributes);
832 if (valtype == TREE_TYPE (t2) && ! p1)
833 return cp_build_type_attribute_variant (t2, attributes);
835 /* Simple way if one arg fails to specify argument types. */
836 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
837 parms = p2;
838 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
839 parms = p1;
840 else
841 parms = commonparms (p1, p2);
843 rval = build_function_type (valtype, parms);
844 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
845 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
846 rval = apply_memfn_quals (rval,
847 type_memfn_quals (t1),
848 type_memfn_rqual (t1));
849 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
850 TYPE_RAISES_EXCEPTIONS (t2));
851 t1 = build_exception_variant (rval, raises);
852 if (late_return_type_p)
853 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
854 break;
857 case METHOD_TYPE:
859 /* Get this value the long way, since TYPE_METHOD_BASETYPE
860 is just the main variant of this. */
861 tree basetype = class_of_this_parm (t2);
862 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
863 TYPE_RAISES_EXCEPTIONS (t2));
864 cp_ref_qualifier rqual = type_memfn_rqual (t1);
865 tree t3;
866 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
867 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
869 /* If this was a member function type, get back to the
870 original type of type member function (i.e., without
871 the class instance variable up front. */
872 t1 = build_function_type (TREE_TYPE (t1),
873 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
874 t2 = build_function_type (TREE_TYPE (t2),
875 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
876 t3 = merge_types (t1, t2);
877 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
878 TYPE_ARG_TYPES (t3));
879 t1 = build_exception_variant (t3, raises);
880 t1 = build_ref_qualified_type (t1, rqual);
881 if (late_return_type_1_p)
882 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
883 if (late_return_type_2_p)
884 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
885 break;
888 case TYPENAME_TYPE:
889 /* There is no need to merge attributes into a TYPENAME_TYPE.
890 When the type is instantiated it will have whatever
891 attributes result from the instantiation. */
892 return t1;
894 default:;
897 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
898 return t1;
899 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
900 return t2;
901 else
902 return cp_build_type_attribute_variant (t1, attributes);
905 /* Return the ARRAY_TYPE type without its domain. */
907 tree
908 strip_array_domain (tree type)
910 tree t2;
911 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
912 if (TYPE_DOMAIN (type) == NULL_TREE)
913 return type;
914 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
915 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
918 /* Wrapper around cp_common_type that is used by c-common.c and other
919 front end optimizations that remove promotions.
921 Return the common type for two arithmetic types T1 and T2 under the
922 usual arithmetic conversions. The default conversions have already
923 been applied, and enumerated types converted to their compatible
924 integer types. */
926 tree
927 common_type (tree t1, tree t2)
929 /* If one type is nonsense, use the other */
930 if (t1 == error_mark_node)
931 return t2;
932 if (t2 == error_mark_node)
933 return t1;
935 return cp_common_type (t1, t2);
938 /* Return the common type of two pointer types T1 and T2. This is the
939 type for the result of most arithmetic operations if the operands
940 have the given two types.
942 We assume that comp_target_types has already been done and returned
943 nonzero; if that isn't so, this may crash. */
945 tree
946 common_pointer_type (tree t1, tree t2)
948 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
949 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
950 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
952 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
953 CPO_CONVERSION, tf_warning_or_error);
956 /* Compare two exception specifier types for exactness or subsetness, if
957 allowed. Returns false for mismatch, true for match (same, or
958 derived and !exact).
960 [except.spec] "If a class X ... objects of class X or any class publicly
961 and unambiguously derived from X. Similarly, if a pointer type Y * ...
962 exceptions of type Y * or that are pointers to any type publicly and
963 unambiguously derived from Y. Otherwise a function only allows exceptions
964 that have the same type ..."
965 This does not mention cv qualifiers and is different to what throw
966 [except.throw] and catch [except.catch] will do. They will ignore the
967 top level cv qualifiers, and allow qualifiers in the pointer to class
968 example.
970 We implement the letter of the standard. */
972 static bool
973 comp_except_types (tree a, tree b, bool exact)
975 if (same_type_p (a, b))
976 return true;
977 else if (!exact)
979 if (cp_type_quals (a) || cp_type_quals (b))
980 return false;
982 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
984 a = TREE_TYPE (a);
985 b = TREE_TYPE (b);
986 if (cp_type_quals (a) || cp_type_quals (b))
987 return false;
990 if (TREE_CODE (a) != RECORD_TYPE
991 || TREE_CODE (b) != RECORD_TYPE)
992 return false;
994 if (publicly_uniquely_derived_p (a, b))
995 return true;
997 return false;
1000 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1001 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1002 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1003 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1004 are unordered, but we've already filtered out duplicates. Most lists will
1005 be in order, we should try to make use of that. */
1007 bool
1008 comp_except_specs (const_tree t1, const_tree t2, int exact)
1010 const_tree probe;
1011 const_tree base;
1012 int length = 0;
1014 if (t1 == t2)
1015 return true;
1017 /* First handle noexcept. */
1018 if (exact < ce_exact)
1020 /* noexcept(false) is compatible with no exception-specification,
1021 and stricter than any spec. */
1022 if (t1 == noexcept_false_spec)
1023 return t2 == NULL_TREE || exact == ce_derived;
1024 /* Even a derived noexcept(false) is compatible with no
1025 exception-specification. */
1026 if (t2 == noexcept_false_spec)
1027 return t1 == NULL_TREE;
1029 /* Otherwise, if we aren't looking for an exact match, noexcept is
1030 equivalent to throw(). */
1031 if (t1 == noexcept_true_spec)
1032 t1 = empty_except_spec;
1033 if (t2 == noexcept_true_spec)
1034 t2 = empty_except_spec;
1037 /* If any noexcept is left, it is only comparable to itself;
1038 either we're looking for an exact match or we're redeclaring a
1039 template with dependent noexcept. */
1040 if ((t1 && TREE_PURPOSE (t1))
1041 || (t2 && TREE_PURPOSE (t2)))
1042 return (t1 && t2
1043 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1045 if (t1 == NULL_TREE) /* T1 is ... */
1046 return t2 == NULL_TREE || exact == ce_derived;
1047 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1048 return t2 != NULL_TREE && !TREE_VALUE (t2);
1049 if (t2 == NULL_TREE) /* T2 is ... */
1050 return false;
1051 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1052 return exact == ce_derived;
1054 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1055 Count how many we find, to determine exactness. For exact matching and
1056 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1057 O(nm). */
1058 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1060 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1062 tree a = TREE_VALUE (probe);
1063 tree b = TREE_VALUE (t2);
1065 if (comp_except_types (a, b, exact))
1067 if (probe == base && exact > ce_derived)
1068 base = TREE_CHAIN (probe);
1069 length++;
1070 break;
1073 if (probe == NULL_TREE)
1074 return false;
1076 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1079 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1080 [] can match [size]. */
1082 static bool
1083 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1085 tree d1;
1086 tree d2;
1087 tree max1, max2;
1089 if (t1 == t2)
1090 return true;
1092 /* The type of the array elements must be the same. */
1093 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1094 return false;
1096 d1 = TYPE_DOMAIN (t1);
1097 d2 = TYPE_DOMAIN (t2);
1099 if (d1 == d2)
1100 return true;
1102 /* If one of the arrays is dimensionless, and the other has a
1103 dimension, they are of different types. However, it is valid to
1104 write:
1106 extern int a[];
1107 int a[3];
1109 by [basic.link]:
1111 declarations for an array object can specify
1112 array types that differ by the presence or absence of a major
1113 array bound (_dcl.array_). */
1114 if (!d1 || !d2)
1115 return allow_redeclaration;
1117 /* Check that the dimensions are the same. */
1119 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1120 return false;
1121 max1 = TYPE_MAX_VALUE (d1);
1122 max2 = TYPE_MAX_VALUE (d2);
1124 if (!cp_tree_equal (max1, max2))
1125 return false;
1127 return true;
1130 /* Compare the relative position of T1 and T2 into their respective
1131 template parameter list.
1132 T1 and T2 must be template parameter types.
1133 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1135 static bool
1136 comp_template_parms_position (tree t1, tree t2)
1138 tree index1, index2;
1139 gcc_assert (t1 && t2
1140 && TREE_CODE (t1) == TREE_CODE (t2)
1141 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1142 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1143 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1145 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1146 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1148 /* Then compare their relative position. */
1149 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1150 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1151 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1152 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1153 return false;
1155 /* In C++14 we can end up comparing 'auto' to a normal template
1156 parameter. Don't confuse them. */
1157 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1158 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1160 return true;
1163 /* Subroutine in comptypes. */
1165 static bool
1166 structural_comptypes (tree t1, tree t2, int strict)
1168 if (t1 == t2)
1169 return true;
1171 /* Suppress errors caused by previously reported errors. */
1172 if (t1 == error_mark_node || t2 == error_mark_node)
1173 return false;
1175 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1177 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1178 current instantiation. */
1179 if (TREE_CODE (t1) == TYPENAME_TYPE)
1180 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1182 if (TREE_CODE (t2) == TYPENAME_TYPE)
1183 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1185 if (TYPE_PTRMEMFUNC_P (t1))
1186 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1187 if (TYPE_PTRMEMFUNC_P (t2))
1188 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1190 /* Different classes of types can't be compatible. */
1191 if (TREE_CODE (t1) != TREE_CODE (t2))
1192 return false;
1194 /* Qualifiers must match. For array types, we will check when we
1195 recur on the array element types. */
1196 if (TREE_CODE (t1) != ARRAY_TYPE
1197 && cp_type_quals (t1) != cp_type_quals (t2))
1198 return false;
1199 if (TREE_CODE (t1) == FUNCTION_TYPE
1200 && type_memfn_quals (t1) != type_memfn_quals (t2))
1201 return false;
1202 /* Need to check this before TYPE_MAIN_VARIANT.
1203 FIXME function qualifiers should really change the main variant. */
1204 if ((TREE_CODE (t1) == FUNCTION_TYPE
1205 || TREE_CODE (t1) == METHOD_TYPE)
1206 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1207 return false;
1208 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1209 return false;
1211 /* Allow for two different type nodes which have essentially the same
1212 definition. Note that we already checked for equality of the type
1213 qualifiers (just above). */
1215 if (TREE_CODE (t1) != ARRAY_TYPE
1216 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1217 return true;
1220 /* Compare the types. Break out if they could be the same. */
1221 switch (TREE_CODE (t1))
1223 case VOID_TYPE:
1224 case BOOLEAN_TYPE:
1225 /* All void and bool types are the same. */
1226 break;
1228 case INTEGER_TYPE:
1229 case FIXED_POINT_TYPE:
1230 case REAL_TYPE:
1231 /* With these nodes, we can't determine type equivalence by
1232 looking at what is stored in the nodes themselves, because
1233 two nodes might have different TYPE_MAIN_VARIANTs but still
1234 represent the same type. For example, wchar_t and int could
1235 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1236 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1237 and are distinct types. On the other hand, int and the
1238 following typedef
1240 typedef int INT __attribute((may_alias));
1242 have identical properties, different TYPE_MAIN_VARIANTs, but
1243 represent the same type. The canonical type system keeps
1244 track of equivalence in this case, so we fall back on it. */
1245 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1247 case TEMPLATE_TEMPLATE_PARM:
1248 case BOUND_TEMPLATE_TEMPLATE_PARM:
1249 if (!comp_template_parms_position (t1, t2))
1250 return false;
1251 if (!comp_template_parms
1252 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1253 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1254 return false;
1255 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1256 break;
1257 /* Don't check inheritance. */
1258 strict = COMPARE_STRICT;
1259 /* Fall through. */
1261 case RECORD_TYPE:
1262 case UNION_TYPE:
1263 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1264 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1265 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1266 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1267 break;
1269 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1270 break;
1271 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1272 break;
1274 return false;
1276 case OFFSET_TYPE:
1277 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1278 strict & ~COMPARE_REDECLARATION))
1279 return false;
1280 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1281 return false;
1282 break;
1284 case REFERENCE_TYPE:
1285 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1286 return false;
1287 /* fall through to checks for pointer types */
1289 case POINTER_TYPE:
1290 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1291 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1292 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1293 return false;
1294 break;
1296 case METHOD_TYPE:
1297 case FUNCTION_TYPE:
1298 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1299 return false;
1300 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1301 return false;
1302 break;
1304 case ARRAY_TYPE:
1305 /* Target types must match incl. qualifiers. */
1306 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1307 return false;
1308 break;
1310 case TEMPLATE_TYPE_PARM:
1311 /* If T1 and T2 don't have the same relative position in their
1312 template parameters set, they can't be equal. */
1313 if (!comp_template_parms_position (t1, t2))
1314 return false;
1315 break;
1317 case TYPENAME_TYPE:
1318 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1319 TYPENAME_TYPE_FULLNAME (t2)))
1320 return false;
1321 /* Qualifiers don't matter on scopes. */
1322 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1323 TYPE_CONTEXT (t2)))
1324 return false;
1325 break;
1327 case UNBOUND_CLASS_TEMPLATE:
1328 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1329 return false;
1330 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1331 return false;
1332 break;
1334 case COMPLEX_TYPE:
1335 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1336 return false;
1337 break;
1339 case VECTOR_TYPE:
1340 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1341 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1342 return false;
1343 break;
1345 case TYPE_PACK_EXPANSION:
1346 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1347 PACK_EXPANSION_PATTERN (t2))
1348 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1349 PACK_EXPANSION_EXTRA_ARGS (t2)));
1351 case DECLTYPE_TYPE:
1352 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1353 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1354 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1355 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1356 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1357 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1358 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1359 DECLTYPE_TYPE_EXPR (t2)))
1360 return false;
1361 break;
1363 case UNDERLYING_TYPE:
1364 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1365 UNDERLYING_TYPE_TYPE (t2));
1367 default:
1368 return false;
1371 /* If we get here, we know that from a target independent POV the
1372 types are the same. Make sure the target attributes are also
1373 the same. */
1374 return comp_type_attributes (t1, t2);
1377 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1378 is a bitwise-or of the COMPARE_* flags. */
1380 bool
1381 comptypes (tree t1, tree t2, int strict)
1383 if (strict == COMPARE_STRICT)
1385 if (t1 == t2)
1386 return true;
1388 if (t1 == error_mark_node || t2 == error_mark_node)
1389 return false;
1391 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1392 /* At least one of the types requires structural equality, so
1393 perform a deep check. */
1394 return structural_comptypes (t1, t2, strict);
1396 #ifdef ENABLE_CHECKING
1397 if (USE_CANONICAL_TYPES)
1399 bool result = structural_comptypes (t1, t2, strict);
1401 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1402 /* The two types are structurally equivalent, but their
1403 canonical types were different. This is a failure of the
1404 canonical type propagation code.*/
1405 internal_error
1406 ("canonical types differ for identical types %T and %T",
1407 t1, t2);
1408 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1409 /* Two types are structurally different, but the canonical
1410 types are the same. This means we were over-eager in
1411 assigning canonical types. */
1412 internal_error
1413 ("same canonical type node for different types %T and %T",
1414 t1, t2);
1416 return result;
1418 #else
1419 if (USE_CANONICAL_TYPES)
1420 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1421 #endif
1422 else
1423 return structural_comptypes (t1, t2, strict);
1425 else if (strict == COMPARE_STRUCTURAL)
1426 return structural_comptypes (t1, t2, COMPARE_STRICT);
1427 else
1428 return structural_comptypes (t1, t2, strict);
1431 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1432 top-level qualifiers. */
1434 bool
1435 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1437 if (type1 == error_mark_node || type2 == error_mark_node)
1438 return false;
1440 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1443 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1445 bool
1446 at_least_as_qualified_p (const_tree type1, const_tree type2)
1448 int q1 = cp_type_quals (type1);
1449 int q2 = cp_type_quals (type2);
1451 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1452 return (q1 & q2) == q2;
1455 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1456 more cv-qualified that TYPE1, and 0 otherwise. */
1459 comp_cv_qualification (int q1, int q2)
1461 if (q1 == q2)
1462 return 0;
1464 if ((q1 & q2) == q2)
1465 return 1;
1466 else if ((q1 & q2) == q1)
1467 return -1;
1469 return 0;
1473 comp_cv_qualification (const_tree type1, const_tree type2)
1475 int q1 = cp_type_quals (type1);
1476 int q2 = cp_type_quals (type2);
1477 return comp_cv_qualification (q1, q2);
1480 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1481 subset of the cv-qualification signature of TYPE2, and the types
1482 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1485 comp_cv_qual_signature (tree type1, tree type2)
1487 if (comp_ptr_ttypes_real (type2, type1, -1))
1488 return 1;
1489 else if (comp_ptr_ttypes_real (type1, type2, -1))
1490 return -1;
1491 else
1492 return 0;
1495 /* Subroutines of `comptypes'. */
1497 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1498 equivalent in the sense that functions with those parameter types
1499 can have equivalent types. The two lists must be equivalent,
1500 element by element. */
1502 bool
1503 compparms (const_tree parms1, const_tree parms2)
1505 const_tree t1, t2;
1507 /* An unspecified parmlist matches any specified parmlist
1508 whose argument types don't need default promotions. */
1510 for (t1 = parms1, t2 = parms2;
1511 t1 || t2;
1512 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1514 /* If one parmlist is shorter than the other,
1515 they fail to match. */
1516 if (!t1 || !t2)
1517 return false;
1518 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1519 return false;
1521 return true;
1525 /* Process a sizeof or alignof expression where the operand is a
1526 type. */
1528 tree
1529 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1531 tree value;
1532 bool dependent_p;
1534 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1535 if (type == error_mark_node)
1536 return error_mark_node;
1538 type = non_reference (type);
1539 if (TREE_CODE (type) == METHOD_TYPE)
1541 if (complain)
1542 pedwarn (input_location, OPT_Wpointer_arith,
1543 "invalid application of %qs to a member function",
1544 operator_name_info[(int) op].name);
1545 else
1546 return error_mark_node;
1547 value = size_one_node;
1550 dependent_p = dependent_type_p (type);
1551 if (!dependent_p)
1552 complete_type (type);
1553 if (dependent_p
1554 /* VLA types will have a non-constant size. In the body of an
1555 uninstantiated template, we don't need to try to compute the
1556 value, because the sizeof expression is not an integral
1557 constant expression in that case. And, if we do try to
1558 compute the value, we'll likely end up with SAVE_EXPRs, which
1559 the template substitution machinery does not expect to see. */
1560 || (processing_template_decl
1561 && COMPLETE_TYPE_P (type)
1562 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1564 value = build_min (op, size_type_node, type);
1565 TREE_READONLY (value) = 1;
1566 return value;
1569 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (type)
1570 && (flag_iso || warn_vla > 0))
1572 if (complain)
1573 pedwarn (input_location, OPT_Wvla,
1574 "taking sizeof array of runtime bound");
1575 else
1576 return error_mark_node;
1579 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1580 op == SIZEOF_EXPR, false,
1581 complain);
1584 /* Return the size of the type, without producing any warnings for
1585 types whose size cannot be taken. This routine should be used only
1586 in some other routine that has already produced a diagnostic about
1587 using the size of such a type. */
1588 tree
1589 cxx_sizeof_nowarn (tree type)
1591 if (TREE_CODE (type) == FUNCTION_TYPE
1592 || VOID_TYPE_P (type)
1593 || TREE_CODE (type) == ERROR_MARK)
1594 return size_one_node;
1595 else if (!COMPLETE_TYPE_P (type))
1596 return size_zero_node;
1597 else
1598 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1601 /* Process a sizeof expression where the operand is an expression. */
1603 static tree
1604 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1606 if (e == error_mark_node)
1607 return error_mark_node;
1609 if (processing_template_decl)
1611 e = build_min (SIZEOF_EXPR, size_type_node, e);
1612 TREE_SIDE_EFFECTS (e) = 0;
1613 TREE_READONLY (e) = 1;
1615 return e;
1618 /* To get the size of a static data member declared as an array of
1619 unknown bound, we need to instantiate it. */
1620 if (VAR_P (e)
1621 && VAR_HAD_UNKNOWN_BOUND (e)
1622 && DECL_TEMPLATE_INSTANTIATION (e))
1623 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1625 if (TREE_CODE (e) == PARM_DECL
1626 && DECL_ARRAY_PARAMETER_P (e)
1627 && (complain & tf_warning))
1629 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1630 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1631 inform (DECL_SOURCE_LOCATION (e), "declared here");
1634 e = mark_type_use (e);
1636 if (TREE_CODE (e) == COMPONENT_REF
1637 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1638 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1640 if (complain & tf_error)
1641 error ("invalid application of %<sizeof%> to a bit-field");
1642 else
1643 return error_mark_node;
1644 e = char_type_node;
1646 else if (is_overloaded_fn (e))
1648 if (complain & tf_error)
1649 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1650 "function type");
1651 else
1652 return error_mark_node;
1653 e = char_type_node;
1655 else if (type_unknown_p (e))
1657 if (complain & tf_error)
1658 cxx_incomplete_type_error (e, TREE_TYPE (e));
1659 else
1660 return error_mark_node;
1661 e = char_type_node;
1663 else
1664 e = TREE_TYPE (e);
1666 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1669 /* Implement the __alignof keyword: Return the minimum required
1670 alignment of E, measured in bytes. For VAR_DECL's and
1671 FIELD_DECL's return DECL_ALIGN (which can be set from an
1672 "aligned" __attribute__ specification). */
1674 static tree
1675 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1677 tree t;
1679 if (e == error_mark_node)
1680 return error_mark_node;
1682 if (processing_template_decl)
1684 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1685 TREE_SIDE_EFFECTS (e) = 0;
1686 TREE_READONLY (e) = 1;
1688 return e;
1691 e = mark_type_use (e);
1693 if (VAR_P (e))
1694 t = size_int (DECL_ALIGN_UNIT (e));
1695 else if (TREE_CODE (e) == COMPONENT_REF
1696 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1697 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1699 if (complain & tf_error)
1700 error ("invalid application of %<__alignof%> to a bit-field");
1701 else
1702 return error_mark_node;
1703 t = size_one_node;
1705 else if (TREE_CODE (e) == COMPONENT_REF
1706 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1707 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1708 else if (is_overloaded_fn (e))
1710 if (complain & tf_error)
1711 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1712 "function type");
1713 else
1714 return error_mark_node;
1715 if (TREE_CODE (e) == FUNCTION_DECL)
1716 t = size_int (DECL_ALIGN_UNIT (e));
1717 else
1718 t = size_one_node;
1720 else if (type_unknown_p (e))
1722 if (complain & tf_error)
1723 cxx_incomplete_type_error (e, TREE_TYPE (e));
1724 else
1725 return error_mark_node;
1726 t = size_one_node;
1728 else
1729 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1730 complain & tf_error);
1732 return fold_convert (size_type_node, t);
1735 /* Process a sizeof or alignof expression E with code OP where the operand
1736 is an expression. */
1738 tree
1739 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1741 if (op == SIZEOF_EXPR)
1742 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1743 else
1744 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1747 /* Build a representation of an expression 'alignas(E).' Return the
1748 folded integer value of E if it is an integral constant expression
1749 that resolves to a valid alignment. If E depends on a template
1750 parameter, return a syntactic representation tree of kind
1751 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1752 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1754 tree
1755 cxx_alignas_expr (tree e)
1757 if (e == NULL_TREE || e == error_mark_node
1758 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1759 return e;
1761 if (TYPE_P (e))
1762 /* [dcl.align]/3:
1764 When the alignment-specifier is of the form
1765 alignas(type-id ), it shall have the same effect as
1766 alignas(alignof(type-id )). */
1768 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1770 /* If we reach this point, it means the alignas expression if of
1771 the form "alignas(assignment-expression)", so we should follow
1772 what is stated by [dcl.align]/2. */
1774 if (value_dependent_expression_p (e))
1775 /* Leave value-dependent expression alone for now. */
1776 return e;
1778 e = fold_non_dependent_expr (e);
1779 e = mark_rvalue_use (e);
1781 /* [dcl.align]/2 says:
1783 the assignment-expression shall be an integral constant
1784 expression. */
1786 return cxx_constant_value (e);
1790 /* EXPR is being used in a context that is not a function call.
1791 Enforce:
1793 [expr.ref]
1795 The expression can be used only as the left-hand operand of a
1796 member function call.
1798 [expr.mptr.operator]
1800 If the result of .* or ->* is a function, then that result can be
1801 used only as the operand for the function call operator ().
1803 by issuing an error message if appropriate. Returns true iff EXPR
1804 violates these rules. */
1806 bool
1807 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1809 if (expr == NULL_TREE)
1810 return false;
1811 /* Don't enforce this in MS mode. */
1812 if (flag_ms_extensions)
1813 return false;
1814 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1815 expr = get_first_fn (expr);
1816 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1818 if (complain & tf_error)
1819 error ("invalid use of non-static member function");
1820 return true;
1822 return false;
1825 /* If EXP is a reference to a bitfield, and the type of EXP does not
1826 match the declared type of the bitfield, return the declared type
1827 of the bitfield. Otherwise, return NULL_TREE. */
1829 tree
1830 is_bitfield_expr_with_lowered_type (const_tree exp)
1832 switch (TREE_CODE (exp))
1834 case COND_EXPR:
1835 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1836 ? TREE_OPERAND (exp, 1)
1837 : TREE_OPERAND (exp, 0)))
1838 return NULL_TREE;
1839 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1841 case COMPOUND_EXPR:
1842 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1844 case MODIFY_EXPR:
1845 case SAVE_EXPR:
1846 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1848 case COMPONENT_REF:
1850 tree field;
1852 field = TREE_OPERAND (exp, 1);
1853 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1854 return NULL_TREE;
1855 if (same_type_ignoring_top_level_qualifiers_p
1856 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1857 return NULL_TREE;
1858 return DECL_BIT_FIELD_TYPE (field);
1861 CASE_CONVERT:
1862 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1863 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1864 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1865 /* Fallthrough. */
1867 default:
1868 return NULL_TREE;
1872 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1873 bitfield with a lowered type, the type of EXP is returned, rather
1874 than NULL_TREE. */
1876 tree
1877 unlowered_expr_type (const_tree exp)
1879 tree type;
1880 tree etype = TREE_TYPE (exp);
1882 type = is_bitfield_expr_with_lowered_type (exp);
1883 if (type)
1884 type = cp_build_qualified_type (type, cp_type_quals (etype));
1885 else
1886 type = etype;
1888 return type;
1891 /* Perform the conversions in [expr] that apply when an lvalue appears
1892 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1893 function-to-pointer conversions. In addition, manifest constants
1894 are replaced by their values, and bitfield references are converted
1895 to their declared types. Note that this function does not perform the
1896 lvalue-to-rvalue conversion for class types. If you need that conversion
1897 to for class types, then you probably need to use force_rvalue.
1899 Although the returned value is being used as an rvalue, this
1900 function does not wrap the returned expression in a
1901 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1902 that the return value is no longer an lvalue. */
1904 tree
1905 decay_conversion (tree exp, tsubst_flags_t complain)
1907 tree type;
1908 enum tree_code code;
1909 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1911 type = TREE_TYPE (exp);
1912 if (type == error_mark_node)
1913 return error_mark_node;
1915 exp = mark_rvalue_use (exp);
1917 exp = resolve_nondeduced_context (exp);
1918 if (type_unknown_p (exp))
1920 if (complain & tf_error)
1921 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1922 return error_mark_node;
1925 code = TREE_CODE (type);
1927 /* For an array decl decay_conversion should not try to return its
1928 initializer. */
1929 if (code != ARRAY_TYPE)
1931 /* FIXME remove? at least need to remember that this isn't really a
1932 constant expression if EXP isn't decl_constant_var_p, like with
1933 C_MAYBE_CONST_EXPR. */
1934 exp = decl_constant_value_safe (exp);
1935 if (error_operand_p (exp))
1936 return error_mark_node;
1939 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1940 return nullptr_node;
1942 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1943 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1944 if (code == VOID_TYPE)
1946 if (complain & tf_error)
1947 error_at (loc, "void value not ignored as it ought to be");
1948 return error_mark_node;
1950 if (invalid_nonstatic_memfn_p (exp, complain))
1951 return error_mark_node;
1952 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1953 return cp_build_addr_expr (exp, complain);
1954 if (code == ARRAY_TYPE)
1956 tree adr;
1957 tree ptrtype;
1959 if (INDIRECT_REF_P (exp))
1960 return build_nop (build_pointer_type (TREE_TYPE (type)),
1961 TREE_OPERAND (exp, 0));
1963 if (TREE_CODE (exp) == COMPOUND_EXPR)
1965 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1966 if (op1 == error_mark_node)
1967 return error_mark_node;
1968 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1969 TREE_OPERAND (exp, 0), op1);
1972 if (!lvalue_p (exp)
1973 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1975 if (complain & tf_error)
1976 error_at (loc, "invalid use of non-lvalue array");
1977 return error_mark_node;
1980 /* Don't let an array compound literal decay to a pointer. It can
1981 still be used to initialize an array or bind to a reference. */
1982 if (TREE_CODE (exp) == TARGET_EXPR)
1984 if (complain & tf_error)
1985 error_at (loc, "taking address of temporary array");
1986 return error_mark_node;
1989 ptrtype = build_pointer_type (TREE_TYPE (type));
1991 if (VAR_P (exp))
1993 if (!cxx_mark_addressable (exp))
1994 return error_mark_node;
1995 adr = build_nop (ptrtype, build_address (exp));
1996 return adr;
1998 /* This way is better for a COMPONENT_REF since it can
1999 simplify the offset for a component. */
2000 adr = cp_build_addr_expr (exp, complain);
2001 return cp_convert (ptrtype, adr, complain);
2004 /* If a bitfield is used in a context where integral promotion
2005 applies, then the caller is expected to have used
2006 default_conversion. That function promotes bitfields correctly
2007 before calling this function. At this point, if we have a
2008 bitfield referenced, we may assume that is not subject to
2009 promotion, and that, therefore, the type of the resulting rvalue
2010 is the declared type of the bitfield. */
2011 exp = convert_bitfield_to_declared_type (exp);
2013 /* We do not call rvalue() here because we do not want to wrap EXP
2014 in a NON_LVALUE_EXPR. */
2016 /* [basic.lval]
2018 Non-class rvalues always have cv-unqualified types. */
2019 type = TREE_TYPE (exp);
2020 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2021 exp = build_nop (cv_unqualified (type), exp);
2023 return exp;
2026 /* Perform preparatory conversions, as part of the "usual arithmetic
2027 conversions". In particular, as per [expr]:
2029 Whenever an lvalue expression appears as an operand of an
2030 operator that expects the rvalue for that operand, the
2031 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2032 standard conversions are applied to convert the expression to an
2033 rvalue.
2035 In addition, we perform integral promotions here, as those are
2036 applied to both operands to a binary operator before determining
2037 what additional conversions should apply. */
2039 static tree
2040 cp_default_conversion (tree exp, tsubst_flags_t complain)
2042 /* Check for target-specific promotions. */
2043 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2044 if (promoted_type)
2045 exp = cp_convert (promoted_type, exp, complain);
2046 /* Perform the integral promotions first so that bitfield
2047 expressions (which may promote to "int", even if the bitfield is
2048 declared "unsigned") are promoted correctly. */
2049 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2050 exp = cp_perform_integral_promotions (exp, complain);
2051 /* Perform the other conversions. */
2052 exp = decay_conversion (exp, complain);
2054 return exp;
2057 /* C version. */
2059 tree
2060 default_conversion (tree exp)
2062 return cp_default_conversion (exp, tf_warning_or_error);
2065 /* EXPR is an expression with an integral or enumeration type.
2066 Perform the integral promotions in [conv.prom], and return the
2067 converted value. */
2069 tree
2070 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2072 tree type;
2073 tree promoted_type;
2075 expr = mark_rvalue_use (expr);
2077 /* [conv.prom]
2079 If the bitfield has an enumerated type, it is treated as any
2080 other value of that type for promotion purposes. */
2081 type = is_bitfield_expr_with_lowered_type (expr);
2082 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2083 type = TREE_TYPE (expr);
2084 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2085 /* Scoped enums don't promote. */
2086 if (SCOPED_ENUM_P (type))
2087 return expr;
2088 promoted_type = type_promotes_to (type);
2089 if (type != promoted_type)
2090 expr = cp_convert (promoted_type, expr, complain);
2091 return expr;
2094 /* C version. */
2096 tree
2097 perform_integral_promotions (tree expr)
2099 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2102 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2103 decay_conversion to one. */
2106 string_conv_p (const_tree totype, const_tree exp, int warn)
2108 tree t;
2110 if (!TYPE_PTR_P (totype))
2111 return 0;
2113 t = TREE_TYPE (totype);
2114 if (!same_type_p (t, char_type_node)
2115 && !same_type_p (t, char16_type_node)
2116 && !same_type_p (t, char32_type_node)
2117 && !same_type_p (t, wchar_type_node))
2118 return 0;
2120 if (TREE_CODE (exp) == STRING_CST)
2122 /* Make sure that we don't try to convert between char and wide chars. */
2123 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2124 return 0;
2126 else
2128 /* Is this a string constant which has decayed to 'const char *'? */
2129 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2130 if (!same_type_p (TREE_TYPE (exp), t))
2131 return 0;
2132 STRIP_NOPS (exp);
2133 if (TREE_CODE (exp) != ADDR_EXPR
2134 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2135 return 0;
2138 /* This warning is not very useful, as it complains about printf. */
2139 if (warn)
2140 warning (OPT_Wwrite_strings,
2141 "deprecated conversion from string constant to %qT",
2142 totype);
2144 return 1;
2147 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2148 can, for example, use as an lvalue. This code used to be in
2149 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2150 expressions, where we're dealing with aggregates. But now it's again only
2151 called from unary_complex_lvalue. The case (in particular) that led to
2152 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2153 get it there. */
2155 static tree
2156 rationalize_conditional_expr (enum tree_code code, tree t,
2157 tsubst_flags_t complain)
2159 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2161 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2162 the first operand is always the one to be used if both operands
2163 are equal, so we know what conditional expression this used to be. */
2164 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2166 tree op0 = TREE_OPERAND (t, 0);
2167 tree op1 = TREE_OPERAND (t, 1);
2169 /* The following code is incorrect if either operand side-effects. */
2170 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2171 && !TREE_SIDE_EFFECTS (op1));
2172 return
2173 build_conditional_expr (loc,
2174 build_x_binary_op (loc,
2175 (TREE_CODE (t) == MIN_EXPR
2176 ? LE_EXPR : GE_EXPR),
2177 op0, TREE_CODE (op0),
2178 op1, TREE_CODE (op1),
2179 /*overload=*/NULL,
2180 complain),
2181 cp_build_unary_op (code, op0, 0, complain),
2182 cp_build_unary_op (code, op1, 0, complain),
2183 complain);
2186 return
2187 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2188 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2189 complain),
2190 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2191 complain),
2192 complain);
2195 /* Given the TYPE of an anonymous union field inside T, return the
2196 FIELD_DECL for the field. If not found return NULL_TREE. Because
2197 anonymous unions can nest, we must also search all anonymous unions
2198 that are directly reachable. */
2200 tree
2201 lookup_anon_field (tree t, tree type)
2203 tree field;
2205 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2207 if (TREE_STATIC (field))
2208 continue;
2209 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2210 continue;
2212 /* If we find it directly, return the field. */
2213 if (DECL_NAME (field) == NULL_TREE
2214 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2216 return field;
2219 /* Otherwise, it could be nested, search harder. */
2220 if (DECL_NAME (field) == NULL_TREE
2221 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2223 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2224 if (subfield)
2225 return subfield;
2228 return NULL_TREE;
2231 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2232 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2233 non-NULL, it indicates the path to the base used to name MEMBER.
2234 If PRESERVE_REFERENCE is true, the expression returned will have
2235 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2236 returned will have the type referred to by the reference.
2238 This function does not perform access control; that is either done
2239 earlier by the parser when the name of MEMBER is resolved to MEMBER
2240 itself, or later when overload resolution selects one of the
2241 functions indicated by MEMBER. */
2243 tree
2244 build_class_member_access_expr (tree object, tree member,
2245 tree access_path, bool preserve_reference,
2246 tsubst_flags_t complain)
2248 tree object_type;
2249 tree member_scope;
2250 tree result = NULL_TREE;
2251 tree using_decl = NULL_TREE;
2253 if (error_operand_p (object) || error_operand_p (member))
2254 return error_mark_node;
2256 gcc_assert (DECL_P (member) || BASELINK_P (member));
2258 /* [expr.ref]
2260 The type of the first expression shall be "class object" (of a
2261 complete type). */
2262 object_type = TREE_TYPE (object);
2263 if (!currently_open_class (object_type)
2264 && !complete_type_or_maybe_complain (object_type, object, complain))
2265 return error_mark_node;
2266 if (!CLASS_TYPE_P (object_type))
2268 if (complain & tf_error)
2270 if (POINTER_TYPE_P (object_type)
2271 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2272 error ("request for member %qD in %qE, which is of pointer "
2273 "type %qT (maybe you meant to use %<->%> ?)",
2274 member, object, object_type);
2275 else
2276 error ("request for member %qD in %qE, which is of non-class "
2277 "type %qT", member, object, object_type);
2279 return error_mark_node;
2282 /* The standard does not seem to actually say that MEMBER must be a
2283 member of OBJECT_TYPE. However, that is clearly what is
2284 intended. */
2285 if (DECL_P (member))
2287 member_scope = DECL_CLASS_CONTEXT (member);
2288 mark_used (member);
2289 if (TREE_DEPRECATED (member))
2290 warn_deprecated_use (member, NULL_TREE);
2292 else
2293 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2294 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2295 presently be the anonymous union. Go outwards until we find a
2296 type related to OBJECT_TYPE. */
2297 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2298 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2299 object_type))
2300 member_scope = TYPE_CONTEXT (member_scope);
2301 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2303 if (complain & tf_error)
2305 if (TREE_CODE (member) == FIELD_DECL)
2306 error ("invalid use of nonstatic data member %qE", member);
2307 else
2308 error ("%qD is not a member of %qT", member, object_type);
2310 return error_mark_node;
2313 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2314 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2315 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2317 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2318 if (temp)
2319 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2322 /* In [expr.ref], there is an explicit list of the valid choices for
2323 MEMBER. We check for each of those cases here. */
2324 if (VAR_P (member))
2326 /* A static data member. */
2327 result = member;
2328 mark_exp_read (object);
2329 /* If OBJECT has side-effects, they are supposed to occur. */
2330 if (TREE_SIDE_EFFECTS (object))
2331 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2333 else if (TREE_CODE (member) == FIELD_DECL)
2335 /* A non-static data member. */
2336 bool null_object_p;
2337 int type_quals;
2338 tree member_type;
2340 null_object_p = (INDIRECT_REF_P (object)
2341 && integer_zerop (TREE_OPERAND (object, 0)));
2343 /* Convert OBJECT to the type of MEMBER. */
2344 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2345 TYPE_MAIN_VARIANT (member_scope)))
2347 tree binfo;
2348 base_kind kind;
2350 binfo = lookup_base (access_path ? access_path : object_type,
2351 member_scope, ba_unique, &kind, complain);
2352 if (binfo == error_mark_node)
2353 return error_mark_node;
2355 /* It is invalid to try to get to a virtual base of a
2356 NULL object. The most common cause is invalid use of
2357 offsetof macro. */
2358 if (null_object_p && kind == bk_via_virtual)
2360 if (complain & tf_error)
2362 error ("invalid access to non-static data member %qD in "
2363 "virtual base of NULL object", member);
2365 return error_mark_node;
2368 /* Convert to the base. */
2369 object = build_base_path (PLUS_EXPR, object, binfo,
2370 /*nonnull=*/1, complain);
2371 /* If we found the base successfully then we should be able
2372 to convert to it successfully. */
2373 gcc_assert (object != error_mark_node);
2376 /* If MEMBER is from an anonymous aggregate, we have converted
2377 OBJECT so that it refers to the class containing the
2378 anonymous union. Generate a reference to the anonymous union
2379 itself, and recur to find MEMBER. */
2380 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2381 /* When this code is called from build_field_call, the
2382 object already has the type of the anonymous union.
2383 That is because the COMPONENT_REF was already
2384 constructed, and was then disassembled before calling
2385 build_field_call. After the function-call code is
2386 cleaned up, this waste can be eliminated. */
2387 && (!same_type_ignoring_top_level_qualifiers_p
2388 (TREE_TYPE (object), DECL_CONTEXT (member))))
2390 tree anonymous_union;
2392 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2393 DECL_CONTEXT (member));
2394 object = build_class_member_access_expr (object,
2395 anonymous_union,
2396 /*access_path=*/NULL_TREE,
2397 preserve_reference,
2398 complain);
2401 /* Compute the type of the field, as described in [expr.ref]. */
2402 type_quals = TYPE_UNQUALIFIED;
2403 member_type = TREE_TYPE (member);
2404 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2406 type_quals = (cp_type_quals (member_type)
2407 | cp_type_quals (object_type));
2409 /* A field is const (volatile) if the enclosing object, or the
2410 field itself, is const (volatile). But, a mutable field is
2411 not const, even within a const object. */
2412 if (DECL_MUTABLE_P (member))
2413 type_quals &= ~TYPE_QUAL_CONST;
2414 member_type = cp_build_qualified_type (member_type, type_quals);
2417 result = build3 (COMPONENT_REF, member_type, object, member,
2418 NULL_TREE);
2419 result = fold_if_not_in_template (result);
2421 /* Mark the expression const or volatile, as appropriate. Even
2422 though we've dealt with the type above, we still have to mark the
2423 expression itself. */
2424 if (type_quals & TYPE_QUAL_CONST)
2425 TREE_READONLY (result) = 1;
2426 if (type_quals & TYPE_QUAL_VOLATILE)
2427 TREE_THIS_VOLATILE (result) = 1;
2429 else if (BASELINK_P (member))
2431 /* The member is a (possibly overloaded) member function. */
2432 tree functions;
2433 tree type;
2435 /* If the MEMBER is exactly one static member function, then we
2436 know the type of the expression. Otherwise, we must wait
2437 until overload resolution has been performed. */
2438 functions = BASELINK_FUNCTIONS (member);
2439 if (TREE_CODE (functions) == FUNCTION_DECL
2440 && DECL_STATIC_FUNCTION_P (functions))
2441 type = TREE_TYPE (functions);
2442 else
2443 type = unknown_type_node;
2444 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2445 base. That will happen when the function is called. */
2446 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2448 else if (TREE_CODE (member) == CONST_DECL)
2450 /* The member is an enumerator. */
2451 result = member;
2452 /* If OBJECT has side-effects, they are supposed to occur. */
2453 if (TREE_SIDE_EFFECTS (object))
2454 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2455 object, result);
2457 else if ((using_decl = strip_using_decl (member)) != member)
2458 result = build_class_member_access_expr (object,
2459 using_decl,
2460 access_path, preserve_reference,
2461 complain);
2462 else
2464 if (complain & tf_error)
2465 error ("invalid use of %qD", member);
2466 return error_mark_node;
2469 if (!preserve_reference)
2470 /* [expr.ref]
2472 If E2 is declared to have type "reference to T", then ... the
2473 type of E1.E2 is T. */
2474 result = convert_from_reference (result);
2476 return result;
2479 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2480 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2482 static tree
2483 lookup_destructor (tree object, tree scope, tree dtor_name,
2484 tsubst_flags_t complain)
2486 tree object_type = TREE_TYPE (object);
2487 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2488 tree expr;
2490 /* We've already complained about this destructor. */
2491 if (dtor_type == error_mark_node)
2492 return error_mark_node;
2494 if (scope && !check_dtor_name (scope, dtor_type))
2496 if (complain & tf_error)
2497 error ("qualified type %qT does not match destructor name ~%qT",
2498 scope, dtor_type);
2499 return error_mark_node;
2501 if (is_auto (dtor_type))
2502 dtor_type = object_type;
2503 else if (identifier_p (dtor_type))
2505 /* In a template, names we can't find a match for are still accepted
2506 destructor names, and we check them here. */
2507 if (check_dtor_name (object_type, dtor_type))
2508 dtor_type = object_type;
2509 else
2511 if (complain & tf_error)
2512 error ("object type %qT does not match destructor name ~%qT",
2513 object_type, dtor_type);
2514 return error_mark_node;
2518 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2520 if (complain & tf_error)
2521 error ("the type being destroyed is %qT, but the destructor "
2522 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2523 return error_mark_node;
2525 expr = lookup_member (dtor_type, complete_dtor_identifier,
2526 /*protect=*/1, /*want_type=*/false,
2527 tf_warning_or_error);
2528 expr = (adjust_result_of_qualified_name_lookup
2529 (expr, dtor_type, object_type));
2530 if (scope == NULL_TREE)
2531 /* We need to call adjust_result_of_qualified_name_lookup in case the
2532 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2533 that we still get virtual function binding. */
2534 BASELINK_QUALIFIED_P (expr) = false;
2535 return expr;
2538 /* An expression of the form "A::template B" has been resolved to
2539 DECL. Issue a diagnostic if B is not a template or template
2540 specialization. */
2542 void
2543 check_template_keyword (tree decl)
2545 /* The standard says:
2547 [temp.names]
2549 If a name prefixed by the keyword template is not a member
2550 template, the program is ill-formed.
2552 DR 228 removed the restriction that the template be a member
2553 template.
2555 DR 96, if accepted would add the further restriction that explicit
2556 template arguments must be provided if the template keyword is
2557 used, but, as of 2005-10-16, that DR is still in "drafting". If
2558 this DR is accepted, then the semantic checks here can be
2559 simplified, as the entity named must in fact be a template
2560 specialization, rather than, as at present, a set of overloaded
2561 functions containing at least one template function. */
2562 if (TREE_CODE (decl) != TEMPLATE_DECL
2563 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2565 if (!is_overloaded_fn (decl))
2566 permerror (input_location, "%qD is not a template", decl);
2567 else
2569 tree fns;
2570 fns = decl;
2571 if (BASELINK_P (fns))
2572 fns = BASELINK_FUNCTIONS (fns);
2573 while (fns)
2575 tree fn = OVL_CURRENT (fns);
2576 if (TREE_CODE (fn) == TEMPLATE_DECL
2577 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2578 break;
2579 if (TREE_CODE (fn) == FUNCTION_DECL
2580 && DECL_USE_TEMPLATE (fn)
2581 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2582 break;
2583 fns = OVL_NEXT (fns);
2585 if (!fns)
2586 permerror (input_location, "%qD is not a template", decl);
2591 /* This function is called by the parser to process a class member
2592 access expression of the form OBJECT.NAME. NAME is a node used by
2593 the parser to represent a name; it is not yet a DECL. It may,
2594 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2595 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2596 there is no reason to do the lookup twice, so the parser keeps the
2597 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2598 be a template via the use of the "A::template B" syntax. */
2600 tree
2601 finish_class_member_access_expr (tree object, tree name, bool template_p,
2602 tsubst_flags_t complain)
2604 tree expr;
2605 tree object_type;
2606 tree member;
2607 tree access_path = NULL_TREE;
2608 tree orig_object = object;
2609 tree orig_name = name;
2611 if (object == error_mark_node || name == error_mark_node)
2612 return error_mark_node;
2614 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2615 if (!objc_is_public (object, name))
2616 return error_mark_node;
2618 object_type = TREE_TYPE (object);
2620 if (processing_template_decl)
2622 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2623 dependent_type_p (object_type)
2624 /* If NAME is just an IDENTIFIER_NODE, then the expression
2625 is dependent. */
2626 || identifier_p (object)
2627 /* If NAME is "f<args>", where either 'f' or 'args' is
2628 dependent, then the expression is dependent. */
2629 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2630 && dependent_template_id_p (TREE_OPERAND (name, 0),
2631 TREE_OPERAND (name, 1)))
2632 /* If NAME is "T::X" where "T" is dependent, then the
2633 expression is dependent. */
2634 || (TREE_CODE (name) == SCOPE_REF
2635 && TYPE_P (TREE_OPERAND (name, 0))
2636 && dependent_type_p (TREE_OPERAND (name, 0))))
2637 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2638 object, name, NULL_TREE);
2639 object = build_non_dependent_expr (object);
2641 else if (c_dialect_objc ()
2642 && identifier_p (name)
2643 && (expr = objc_maybe_build_component_ref (object, name)))
2644 return expr;
2646 /* [expr.ref]
2648 The type of the first expression shall be "class object" (of a
2649 complete type). */
2650 if (!currently_open_class (object_type)
2651 && !complete_type_or_maybe_complain (object_type, object, complain))
2652 return error_mark_node;
2653 if (!CLASS_TYPE_P (object_type))
2655 if (complain & tf_error)
2657 if (POINTER_TYPE_P (object_type)
2658 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2659 error ("request for member %qD in %qE, which is of pointer "
2660 "type %qT (maybe you meant to use %<->%> ?)",
2661 name, object, object_type);
2662 else
2663 error ("request for member %qD in %qE, which is of non-class "
2664 "type %qT", name, object, object_type);
2666 return error_mark_node;
2669 if (BASELINK_P (name))
2670 /* A member function that has already been looked up. */
2671 member = name;
2672 else
2674 bool is_template_id = false;
2675 tree template_args = NULL_TREE;
2676 tree scope;
2678 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2680 is_template_id = true;
2681 template_args = TREE_OPERAND (name, 1);
2682 name = TREE_OPERAND (name, 0);
2684 if (TREE_CODE (name) == OVERLOAD)
2685 name = DECL_NAME (get_first_fn (name));
2686 else if (DECL_P (name))
2687 name = DECL_NAME (name);
2690 if (TREE_CODE (name) == SCOPE_REF)
2692 /* A qualified name. The qualifying class or namespace `S'
2693 has already been looked up; it is either a TYPE or a
2694 NAMESPACE_DECL. */
2695 scope = TREE_OPERAND (name, 0);
2696 name = TREE_OPERAND (name, 1);
2698 /* If SCOPE is a namespace, then the qualified name does not
2699 name a member of OBJECT_TYPE. */
2700 if (TREE_CODE (scope) == NAMESPACE_DECL)
2702 if (complain & tf_error)
2703 error ("%<%D::%D%> is not a member of %qT",
2704 scope, name, object_type);
2705 return error_mark_node;
2708 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2710 /* Looking up a member enumerator (c++/56793). */
2711 if (!TYPE_CLASS_SCOPE_P (scope)
2712 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2714 if (complain & tf_error)
2715 error ("%<%D::%D%> is not a member of %qT",
2716 scope, name, object_type);
2717 return error_mark_node;
2719 tree val = lookup_enumerator (scope, name);
2720 if (TREE_SIDE_EFFECTS (object))
2721 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2722 return val;
2725 gcc_assert (CLASS_TYPE_P (scope));
2726 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2728 if (constructor_name_p (name, scope))
2730 if (complain & tf_error)
2731 error ("cannot call constructor %<%T::%D%> directly",
2732 scope, name);
2733 return error_mark_node;
2736 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2737 access_path = lookup_base (object_type, scope, ba_check,
2738 NULL, complain);
2739 if (access_path == error_mark_node)
2740 return error_mark_node;
2741 if (!access_path)
2743 if (complain & tf_error)
2744 error ("%qT is not a base of %qT", scope, object_type);
2745 return error_mark_node;
2748 else
2750 scope = NULL_TREE;
2751 access_path = object_type;
2754 if (TREE_CODE (name) == BIT_NOT_EXPR)
2755 member = lookup_destructor (object, scope, name, complain);
2756 else
2758 /* Look up the member. */
2759 member = lookup_member (access_path, name, /*protect=*/1,
2760 /*want_type=*/false, complain);
2761 if (member == NULL_TREE)
2763 if (complain & tf_error)
2764 error ("%qD has no member named %qE",
2765 TREE_CODE (access_path) == TREE_BINFO
2766 ? TREE_TYPE (access_path) : object_type, name);
2767 return error_mark_node;
2769 if (member == error_mark_node)
2770 return error_mark_node;
2773 if (is_template_id)
2775 tree templ = member;
2777 if (BASELINK_P (templ))
2778 templ = lookup_template_function (templ, template_args);
2779 else
2781 if (complain & tf_error)
2782 error ("%qD is not a member template function", name);
2783 return error_mark_node;
2788 if (TREE_DEPRECATED (member))
2789 warn_deprecated_use (member, NULL_TREE);
2791 if (template_p)
2792 check_template_keyword (member);
2794 expr = build_class_member_access_expr (object, member, access_path,
2795 /*preserve_reference=*/false,
2796 complain);
2797 if (processing_template_decl && expr != error_mark_node)
2799 if (BASELINK_P (member))
2801 if (TREE_CODE (orig_name) == SCOPE_REF)
2802 BASELINK_QUALIFIED_P (member) = 1;
2803 orig_name = member;
2805 return build_min_non_dep (COMPONENT_REF, expr,
2806 orig_object, orig_name,
2807 NULL_TREE);
2810 return expr;
2813 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2814 type. */
2816 tree
2817 build_simple_component_ref (tree object, tree member)
2819 tree type = cp_build_qualified_type (TREE_TYPE (member),
2820 cp_type_quals (TREE_TYPE (object)));
2821 return fold_build3_loc (input_location,
2822 COMPONENT_REF, type,
2823 object, member, NULL_TREE);
2826 /* Return an expression for the MEMBER_NAME field in the internal
2827 representation of PTRMEM, a pointer-to-member function. (Each
2828 pointer-to-member function type gets its own RECORD_TYPE so it is
2829 more convenient to access the fields by name than by FIELD_DECL.)
2830 This routine converts the NAME to a FIELD_DECL and then creates the
2831 node for the complete expression. */
2833 tree
2834 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2836 tree ptrmem_type;
2837 tree member;
2839 /* This code is a stripped down version of
2840 build_class_member_access_expr. It does not work to use that
2841 routine directly because it expects the object to be of class
2842 type. */
2843 ptrmem_type = TREE_TYPE (ptrmem);
2844 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2845 for (member = TYPE_FIELDS (ptrmem_type); member;
2846 member = DECL_CHAIN (member))
2847 if (DECL_NAME (member) == member_name)
2848 break;
2849 return build_simple_component_ref (ptrmem, member);
2852 /* Given an expression PTR for a pointer, return an expression
2853 for the value pointed to.
2854 ERRORSTRING is the name of the operator to appear in error messages.
2856 This function may need to overload OPERATOR_FNNAME.
2857 Must also handle REFERENCE_TYPEs for C++. */
2859 tree
2860 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2861 tsubst_flags_t complain)
2863 tree orig_expr = expr;
2864 tree rval;
2866 if (processing_template_decl)
2868 /* Retain the type if we know the operand is a pointer. */
2869 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2870 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2871 if (type_dependent_expression_p (expr))
2872 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2873 expr = build_non_dependent_expr (expr);
2876 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2877 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2878 if (!rval)
2879 rval = cp_build_indirect_ref (expr, errorstring, complain);
2881 if (processing_template_decl && rval != error_mark_node)
2882 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2883 else
2884 return rval;
2887 /* Helper function called from c-common. */
2888 tree
2889 build_indirect_ref (location_t /*loc*/,
2890 tree ptr, ref_operator errorstring)
2892 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2895 tree
2896 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2897 tsubst_flags_t complain)
2899 tree pointer, type;
2901 if (ptr == current_class_ptr
2902 || (TREE_CODE (ptr) == NOP_EXPR
2903 && TREE_OPERAND (ptr, 0) == current_class_ptr
2904 && (same_type_ignoring_top_level_qualifiers_p
2905 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2906 return current_class_ref;
2908 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2909 ? ptr : decay_conversion (ptr, complain));
2910 if (pointer == error_mark_node)
2911 return error_mark_node;
2913 type = TREE_TYPE (pointer);
2915 if (POINTER_TYPE_P (type))
2917 /* [expr.unary.op]
2919 If the type of the expression is "pointer to T," the type
2920 of the result is "T." */
2921 tree t = TREE_TYPE (type);
2923 if ((CONVERT_EXPR_P (ptr)
2924 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2925 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2927 /* If a warning is issued, mark it to avoid duplicates from
2928 the backend. This only needs to be done at
2929 warn_strict_aliasing > 2. */
2930 if (warn_strict_aliasing > 2)
2931 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2932 type, TREE_OPERAND (ptr, 0)))
2933 TREE_NO_WARNING (ptr) = 1;
2936 if (VOID_TYPE_P (t))
2938 /* A pointer to incomplete type (other than cv void) can be
2939 dereferenced [expr.unary.op]/1 */
2940 if (complain & tf_error)
2941 error ("%qT is not a pointer-to-object type", type);
2942 return error_mark_node;
2944 else if (TREE_CODE (pointer) == ADDR_EXPR
2945 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2946 /* The POINTER was something like `&x'. We simplify `*&x' to
2947 `x'. */
2948 return TREE_OPERAND (pointer, 0);
2949 else
2951 tree ref = build1 (INDIRECT_REF, t, pointer);
2953 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2954 so that we get the proper error message if the result is used
2955 to assign to. Also, &* is supposed to be a no-op. */
2956 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2957 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2958 TREE_SIDE_EFFECTS (ref)
2959 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2960 return ref;
2963 else if (!(complain & tf_error))
2964 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2966 /* `pointer' won't be an error_mark_node if we were given a
2967 pointer to member, so it's cool to check for this here. */
2968 else if (TYPE_PTRMEM_P (type))
2969 switch (errorstring)
2971 case RO_ARRAY_INDEXING:
2972 error ("invalid use of array indexing on pointer to member");
2973 break;
2974 case RO_UNARY_STAR:
2975 error ("invalid use of unary %<*%> on pointer to member");
2976 break;
2977 case RO_IMPLICIT_CONVERSION:
2978 error ("invalid use of implicit conversion on pointer to member");
2979 break;
2980 case RO_ARROW_STAR:
2981 error ("left hand operand of %<->*%> must be a pointer to class, "
2982 "but is a pointer to member of type %qT", type);
2983 break;
2984 default:
2985 gcc_unreachable ();
2987 else if (pointer != error_mark_node)
2988 invalid_indirection_error (input_location, type, errorstring);
2990 return error_mark_node;
2993 /* This handles expressions of the form "a[i]", which denotes
2994 an array reference.
2996 This is logically equivalent in C to *(a+i), but we may do it differently.
2997 If A is a variable or a member, we generate a primitive ARRAY_REF.
2998 This avoids forcing the array out of registers, and can work on
2999 arrays that are not lvalues (for example, members of structures returned
3000 by functions).
3002 If INDEX is of some user-defined type, it must be converted to
3003 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3004 will inherit the type of the array, which will be some pointer type.
3006 LOC is the location to use in building the array reference. */
3008 tree
3009 cp_build_array_ref (location_t loc, tree array, tree idx,
3010 tsubst_flags_t complain)
3012 tree ret;
3014 if (idx == 0)
3016 if (complain & tf_error)
3017 error_at (loc, "subscript missing in array reference");
3018 return error_mark_node;
3021 /* If an array's index is an array notation, then its rank cannot be
3022 greater than one. */
3023 if (flag_cilkplus && contains_array_notation_expr (idx))
3025 size_t rank = 0;
3027 /* If find_rank returns false, then it should have reported an error,
3028 thus it is unnecessary for repetition. */
3029 if (!find_rank (loc, idx, idx, true, &rank))
3030 return error_mark_node;
3031 if (rank > 1)
3033 error_at (loc, "rank of the array%'s index is greater than 1");
3034 return error_mark_node;
3037 if (TREE_TYPE (array) == error_mark_node
3038 || TREE_TYPE (idx) == error_mark_node)
3039 return error_mark_node;
3041 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3042 inside it. */
3043 switch (TREE_CODE (array))
3045 case COMPOUND_EXPR:
3047 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3048 complain);
3049 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3050 TREE_OPERAND (array, 0), value);
3051 SET_EXPR_LOCATION (ret, loc);
3052 return ret;
3055 case COND_EXPR:
3056 ret = build_conditional_expr
3057 (loc, TREE_OPERAND (array, 0),
3058 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3059 complain),
3060 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3061 complain),
3062 complain);
3063 protected_set_expr_location (ret, loc);
3064 return ret;
3066 default:
3067 break;
3070 convert_vector_to_pointer_for_subscript (loc, &array, idx);
3072 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3074 tree rval, type;
3076 warn_array_subscript_with_type_char (idx);
3078 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3080 if (complain & tf_error)
3081 error_at (loc, "array subscript is not an integer");
3082 return error_mark_node;
3085 /* Apply integral promotions *after* noticing character types.
3086 (It is unclear why we do these promotions -- the standard
3087 does not say that we should. In fact, the natural thing would
3088 seem to be to convert IDX to ptrdiff_t; we're performing
3089 pointer arithmetic.) */
3090 idx = cp_perform_integral_promotions (idx, complain);
3092 /* An array that is indexed by a non-constant
3093 cannot be stored in a register; we must be able to do
3094 address arithmetic on its address.
3095 Likewise an array of elements of variable size. */
3096 if (TREE_CODE (idx) != INTEGER_CST
3097 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3098 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3099 != INTEGER_CST)))
3101 if (!cxx_mark_addressable (array))
3102 return error_mark_node;
3105 /* An array that is indexed by a constant value which is not within
3106 the array bounds cannot be stored in a register either; because we
3107 would get a crash in store_bit_field/extract_bit_field when trying
3108 to access a non-existent part of the register. */
3109 if (TREE_CODE (idx) == INTEGER_CST
3110 && TYPE_DOMAIN (TREE_TYPE (array))
3111 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3113 if (!cxx_mark_addressable (array))
3114 return error_mark_node;
3117 if (!lvalue_p (array))
3119 if (complain & tf_error)
3120 pedwarn (loc, OPT_Wpedantic,
3121 "ISO C++ forbids subscripting non-lvalue array");
3122 else
3123 return error_mark_node;
3126 /* Note in C++ it is valid to subscript a `register' array, since
3127 it is valid to take the address of something with that
3128 storage specification. */
3129 if (extra_warnings)
3131 tree foo = array;
3132 while (TREE_CODE (foo) == COMPONENT_REF)
3133 foo = TREE_OPERAND (foo, 0);
3134 if (VAR_P (foo) && DECL_REGISTER (foo)
3135 && (complain & tf_warning))
3136 warning_at (loc, OPT_Wextra,
3137 "subscripting array declared %<register%>");
3140 type = TREE_TYPE (TREE_TYPE (array));
3141 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3142 /* Array ref is const/volatile if the array elements are
3143 or if the array is.. */
3144 TREE_READONLY (rval)
3145 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3146 TREE_SIDE_EFFECTS (rval)
3147 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3148 TREE_THIS_VOLATILE (rval)
3149 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3150 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3151 complain);
3152 protected_set_expr_location (ret, loc);
3153 return ret;
3157 tree ar = cp_default_conversion (array, complain);
3158 tree ind = cp_default_conversion (idx, complain);
3160 /* Put the integer in IND to simplify error checking. */
3161 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3163 tree temp = ar;
3164 ar = ind;
3165 ind = temp;
3168 if (ar == error_mark_node || ind == error_mark_node)
3169 return error_mark_node;
3171 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3173 if (complain & tf_error)
3174 error_at (loc, "subscripted value is neither array nor pointer");
3175 return error_mark_node;
3177 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3179 if (complain & tf_error)
3180 error_at (loc, "array subscript is not an integer");
3181 return error_mark_node;
3184 warn_array_subscript_with_type_char (idx);
3186 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3187 PLUS_EXPR, ar, ind,
3188 complain),
3189 RO_ARRAY_INDEXING,
3190 complain);
3191 protected_set_expr_location (ret, loc);
3192 return ret;
3196 /* Entry point for Obj-C++. */
3198 tree
3199 build_array_ref (location_t loc, tree array, tree idx)
3201 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3204 /* Resolve a pointer to member function. INSTANCE is the object
3205 instance to use, if the member points to a virtual member.
3207 This used to avoid checking for virtual functions if basetype
3208 has no virtual functions, according to an earlier ANSI draft.
3209 With the final ISO C++ rules, such an optimization is
3210 incorrect: A pointer to a derived member can be static_cast
3211 to pointer-to-base-member, as long as the dynamic object
3212 later has the right member. So now we only do this optimization
3213 when we know the dynamic type of the object. */
3215 tree
3216 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3217 tsubst_flags_t complain)
3219 if (TREE_CODE (function) == OFFSET_REF)
3220 function = TREE_OPERAND (function, 1);
3222 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3224 tree idx, delta, e1, e2, e3, vtbl;
3225 bool nonvirtual;
3226 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3227 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3229 tree instance_ptr = *instance_ptrptr;
3230 tree instance_save_expr = 0;
3231 if (instance_ptr == error_mark_node)
3233 if (TREE_CODE (function) == PTRMEM_CST)
3235 /* Extracting the function address from a pmf is only
3236 allowed with -Wno-pmf-conversions. It only works for
3237 pmf constants. */
3238 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3239 e1 = convert (fntype, e1);
3240 return e1;
3242 else
3244 if (complain & tf_error)
3245 error ("object missing in use of %qE", function);
3246 return error_mark_node;
3250 /* True if we know that the dynamic type of the object doesn't have
3251 virtual functions, so we can assume the PFN field is a pointer. */
3252 nonvirtual = (COMPLETE_TYPE_P (basetype)
3253 && !TYPE_POLYMORPHIC_P (basetype)
3254 && resolves_to_fixed_type_p (instance_ptr, 0));
3256 /* If we don't really have an object (i.e. in an ill-formed
3257 conversion from PMF to pointer), we can't resolve virtual
3258 functions anyway. */
3259 if (!nonvirtual && is_dummy_object (instance_ptr))
3260 nonvirtual = true;
3262 if (TREE_SIDE_EFFECTS (instance_ptr))
3263 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3265 if (TREE_SIDE_EFFECTS (function))
3266 function = save_expr (function);
3268 /* Start by extracting all the information from the PMF itself. */
3269 e3 = pfn_from_ptrmemfunc (function);
3270 delta = delta_from_ptrmemfunc (function);
3271 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3272 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3274 case ptrmemfunc_vbit_in_pfn:
3275 e1 = cp_build_binary_op (input_location,
3276 BIT_AND_EXPR, idx, integer_one_node,
3277 complain);
3278 idx = cp_build_binary_op (input_location,
3279 MINUS_EXPR, idx, integer_one_node,
3280 complain);
3281 if (idx == error_mark_node)
3282 return error_mark_node;
3283 break;
3285 case ptrmemfunc_vbit_in_delta:
3286 e1 = cp_build_binary_op (input_location,
3287 BIT_AND_EXPR, delta, integer_one_node,
3288 complain);
3289 delta = cp_build_binary_op (input_location,
3290 RSHIFT_EXPR, delta, integer_one_node,
3291 complain);
3292 if (delta == error_mark_node)
3293 return error_mark_node;
3294 break;
3296 default:
3297 gcc_unreachable ();
3300 if (e1 == error_mark_node)
3301 return error_mark_node;
3303 /* Convert down to the right base before using the instance. A
3304 special case is that in a pointer to member of class C, C may
3305 be incomplete. In that case, the function will of course be
3306 a member of C, and no conversion is required. In fact,
3307 lookup_base will fail in that case, because incomplete
3308 classes do not have BINFOs. */
3309 if (!same_type_ignoring_top_level_qualifiers_p
3310 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3312 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3313 basetype, ba_check, NULL, complain);
3314 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3315 1, complain);
3316 if (instance_ptr == error_mark_node)
3317 return error_mark_node;
3319 /* ...and then the delta in the PMF. */
3320 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3322 /* Hand back the adjusted 'this' argument to our caller. */
3323 *instance_ptrptr = instance_ptr;
3325 if (nonvirtual)
3326 /* Now just return the pointer. */
3327 return e3;
3329 /* Next extract the vtable pointer from the object. */
3330 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3331 instance_ptr);
3332 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3333 if (vtbl == error_mark_node)
3334 return error_mark_node;
3336 /* Finally, extract the function pointer from the vtable. */
3337 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3338 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3339 if (e2 == error_mark_node)
3340 return error_mark_node;
3341 TREE_CONSTANT (e2) = 1;
3343 /* When using function descriptors, the address of the
3344 vtable entry is treated as a function pointer. */
3345 if (TARGET_VTABLE_USES_DESCRIPTORS)
3346 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3347 cp_build_addr_expr (e2, complain));
3349 e2 = fold_convert (TREE_TYPE (e3), e2);
3350 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3351 if (e1 == error_mark_node)
3352 return error_mark_node;
3354 /* Make sure this doesn't get evaluated first inside one of the
3355 branches of the COND_EXPR. */
3356 if (instance_save_expr)
3357 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3358 instance_save_expr, e1);
3360 function = e1;
3362 return function;
3365 /* Used by the C-common bits. */
3366 tree
3367 build_function_call (location_t /*loc*/,
3368 tree function, tree params)
3370 return cp_build_function_call (function, params, tf_warning_or_error);
3373 /* Used by the C-common bits. */
3374 tree
3375 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3376 tree function, vec<tree, va_gc> *params,
3377 vec<tree, va_gc> * /*origtypes*/)
3379 vec<tree, va_gc> *orig_params = params;
3380 tree ret = cp_build_function_call_vec (function, &params,
3381 tf_warning_or_error);
3383 /* cp_build_function_call_vec can reallocate PARAMS by adding
3384 default arguments. That should never happen here. Verify
3385 that. */
3386 gcc_assert (params == orig_params);
3388 return ret;
3391 /* Build a function call using a tree list of arguments. */
3393 tree
3394 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3396 vec<tree, va_gc> *vec;
3397 tree ret;
3399 vec = make_tree_vector ();
3400 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3401 vec_safe_push (vec, TREE_VALUE (params));
3402 ret = cp_build_function_call_vec (function, &vec, complain);
3403 release_tree_vector (vec);
3404 return ret;
3407 /* Build a function call using varargs. */
3409 tree
3410 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3412 vec<tree, va_gc> *vec;
3413 va_list args;
3414 tree ret, t;
3416 vec = make_tree_vector ();
3417 va_start (args, complain);
3418 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3419 vec_safe_push (vec, t);
3420 va_end (args);
3421 ret = cp_build_function_call_vec (function, &vec, complain);
3422 release_tree_vector (vec);
3423 return ret;
3426 /* Build a function call using a vector of arguments. PARAMS may be
3427 NULL if there are no parameters. This changes the contents of
3428 PARAMS. */
3430 tree
3431 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3432 tsubst_flags_t complain)
3434 tree fntype, fndecl;
3435 int is_method;
3436 tree original = function;
3437 int nargs;
3438 tree *argarray;
3439 tree parm_types;
3440 vec<tree, va_gc> *allocated = NULL;
3441 tree ret;
3443 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3444 expressions, like those used for ObjC messenger dispatches. */
3445 if (params != NULL && !vec_safe_is_empty (*params))
3446 function = objc_rewrite_function_call (function, (**params)[0]);
3448 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3449 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3450 if (TREE_CODE (function) == NOP_EXPR
3451 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3452 function = TREE_OPERAND (function, 0);
3454 if (TREE_CODE (function) == FUNCTION_DECL)
3456 mark_used (function);
3457 fndecl = function;
3459 /* Convert anything with function type to a pointer-to-function. */
3460 if (DECL_MAIN_P (function))
3462 if (complain & tf_error)
3463 pedwarn (input_location, OPT_Wpedantic,
3464 "ISO C++ forbids calling %<::main%> from within program");
3465 else
3466 return error_mark_node;
3468 function = build_addr_func (function, complain);
3470 else
3472 fndecl = NULL_TREE;
3474 function = build_addr_func (function, complain);
3477 if (function == error_mark_node)
3478 return error_mark_node;
3480 fntype = TREE_TYPE (function);
3482 if (TYPE_PTRMEMFUNC_P (fntype))
3484 if (complain & tf_error)
3485 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3486 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3487 original, original);
3488 return error_mark_node;
3491 is_method = (TYPE_PTR_P (fntype)
3492 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3494 if (!(TYPE_PTRFN_P (fntype)
3495 || is_method
3496 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3498 if (complain & tf_error)
3500 if (!flag_diagnostics_show_caret)
3501 error_at (input_location,
3502 "%qE cannot be used as a function", original);
3503 else if (DECL_P (original))
3504 error_at (input_location,
3505 "%qD cannot be used as a function", original);
3506 else
3507 error_at (input_location,
3508 "expression cannot be used as a function");
3511 return error_mark_node;
3514 /* fntype now gets the type of function pointed to. */
3515 fntype = TREE_TYPE (fntype);
3516 parm_types = TYPE_ARG_TYPES (fntype);
3518 if (params == NULL)
3520 allocated = make_tree_vector ();
3521 params = &allocated;
3524 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3525 complain);
3526 if (nargs < 0)
3527 return error_mark_node;
3529 argarray = (*params)->address ();
3531 /* Check for errors in format strings and inappropriately
3532 null parameters. */
3533 check_function_arguments (fntype, nargs, argarray);
3535 ret = build_cxx_call (function, nargs, argarray, complain);
3537 if (allocated != NULL)
3538 release_tree_vector (allocated);
3540 return ret;
3543 /* Subroutine of convert_arguments.
3544 Warn about wrong number of args are genereted. */
3546 static void
3547 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3549 if (fndecl)
3551 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3553 if (DECL_NAME (fndecl) == NULL_TREE
3554 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3555 error_at (loc,
3556 too_many_p
3557 ? G_("too many arguments to constructor %q#D")
3558 : G_("too few arguments to constructor %q#D"),
3559 fndecl);
3560 else
3561 error_at (loc,
3562 too_many_p
3563 ? G_("too many arguments to member function %q#D")
3564 : G_("too few arguments to member function %q#D"),
3565 fndecl);
3567 else
3568 error_at (loc,
3569 too_many_p
3570 ? G_("too many arguments to function %q#D")
3571 : G_("too few arguments to function %q#D"),
3572 fndecl);
3573 inform (DECL_SOURCE_LOCATION (fndecl),
3574 "declared here");
3576 else
3578 if (c_dialect_objc () && objc_message_selector ())
3579 error_at (loc,
3580 too_many_p
3581 ? G_("too many arguments to method %q#D")
3582 : G_("too few arguments to method %q#D"),
3583 objc_message_selector ());
3584 else
3585 error_at (loc, too_many_p ? G_("too many arguments to function")
3586 : G_("too few arguments to function"));
3590 /* Convert the actual parameter expressions in the list VALUES to the
3591 types in the list TYPELIST. The converted expressions are stored
3592 back in the VALUES vector.
3593 If parmdecls is exhausted, or when an element has NULL as its type,
3594 perform the default conversions.
3596 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3598 This is also where warnings about wrong number of args are generated.
3600 Returns the actual number of arguments processed (which might be less
3601 than the length of the vector), or -1 on error.
3603 In C++, unspecified trailing parameters can be filled in with their
3604 default arguments, if such were specified. Do so here. */
3606 static int
3607 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3608 int flags, tsubst_flags_t complain)
3610 tree typetail;
3611 unsigned int i;
3613 /* Argument passing is always copy-initialization. */
3614 flags |= LOOKUP_ONLYCONVERTING;
3616 for (i = 0, typetail = typelist;
3617 i < vec_safe_length (*values);
3618 i++)
3620 tree type = typetail ? TREE_VALUE (typetail) : 0;
3621 tree val = (**values)[i];
3623 if (val == error_mark_node || type == error_mark_node)
3624 return -1;
3626 if (type == void_type_node)
3628 if (complain & tf_error)
3630 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3631 return i;
3633 else
3634 return -1;
3637 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3638 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3639 if (TREE_CODE (val) == NOP_EXPR
3640 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3641 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3642 val = TREE_OPERAND (val, 0);
3644 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3646 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3647 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3648 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3649 val = decay_conversion (val, complain);
3652 if (val == error_mark_node)
3653 return -1;
3655 if (type != 0)
3657 /* Formal parm type is specified by a function prototype. */
3658 tree parmval;
3660 if (!COMPLETE_TYPE_P (complete_type (type)))
3662 if (complain & tf_error)
3664 if (fndecl)
3665 error ("parameter %P of %qD has incomplete type %qT",
3666 i, fndecl, type);
3667 else
3668 error ("parameter %P has incomplete type %qT", i, type);
3670 parmval = error_mark_node;
3672 else
3674 parmval = convert_for_initialization
3675 (NULL_TREE, type, val, flags,
3676 ICR_ARGPASS, fndecl, i, complain);
3677 parmval = convert_for_arg_passing (type, parmval, complain);
3680 if (parmval == error_mark_node)
3681 return -1;
3683 (**values)[i] = parmval;
3685 else
3687 if (fndecl && magic_varargs_p (fndecl))
3688 /* Don't do ellipsis conversion for __built_in_constant_p
3689 as this will result in spurious errors for non-trivial
3690 types. */
3691 val = require_complete_type_sfinae (val, complain);
3692 else
3693 val = convert_arg_to_ellipsis (val, complain);
3695 (**values)[i] = val;
3698 if (typetail)
3699 typetail = TREE_CHAIN (typetail);
3702 if (typetail != 0 && typetail != void_list_node)
3704 /* See if there are default arguments that can be used. Because
3705 we hold default arguments in the FUNCTION_TYPE (which is so
3706 wrong), we can see default parameters here from deduced
3707 contexts (and via typeof) for indirect function calls.
3708 Fortunately we know whether we have a function decl to
3709 provide default arguments in a language conformant
3710 manner. */
3711 if (fndecl && TREE_PURPOSE (typetail)
3712 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3714 for (; typetail != void_list_node; ++i)
3716 tree parmval
3717 = convert_default_arg (TREE_VALUE (typetail),
3718 TREE_PURPOSE (typetail),
3719 fndecl, i, complain);
3721 if (parmval == error_mark_node)
3722 return -1;
3724 vec_safe_push (*values, parmval);
3725 typetail = TREE_CHAIN (typetail);
3726 /* ends with `...'. */
3727 if (typetail == NULL_TREE)
3728 break;
3731 else
3733 if (complain & tf_error)
3734 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3735 return -1;
3739 return (int) i;
3742 /* Build a binary-operation expression, after performing default
3743 conversions on the operands. CODE is the kind of expression to
3744 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3745 are the tree codes which correspond to ARG1 and ARG2 when issuing
3746 warnings about possibly misplaced parentheses. They may differ
3747 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3748 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3749 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3750 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3751 ARG2_CODE as ERROR_MARK. */
3753 tree
3754 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3755 enum tree_code arg1_code, tree arg2,
3756 enum tree_code arg2_code, tree *overload,
3757 tsubst_flags_t complain)
3759 tree orig_arg1;
3760 tree orig_arg2;
3761 tree expr;
3763 orig_arg1 = arg1;
3764 orig_arg2 = arg2;
3766 if (processing_template_decl)
3768 if (type_dependent_expression_p (arg1)
3769 || type_dependent_expression_p (arg2))
3770 return build_min_nt_loc (loc, code, arg1, arg2);
3771 arg1 = build_non_dependent_expr (arg1);
3772 arg2 = build_non_dependent_expr (arg2);
3775 if (code == DOTSTAR_EXPR)
3776 expr = build_m_component_ref (arg1, arg2, complain);
3777 else
3778 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3779 overload, complain);
3781 /* Check for cases such as x+y<<z which users are likely to
3782 misinterpret. But don't warn about obj << x + y, since that is a
3783 common idiom for I/O. */
3784 if (warn_parentheses
3785 && (complain & tf_warning)
3786 && !processing_template_decl
3787 && !error_operand_p (arg1)
3788 && !error_operand_p (arg2)
3789 && (code != LSHIFT_EXPR
3790 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3791 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3792 arg2_code, orig_arg2);
3794 if (processing_template_decl && expr != error_mark_node)
3795 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3797 return expr;
3800 /* Build and return an ARRAY_REF expression. */
3802 tree
3803 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3804 tsubst_flags_t complain)
3806 tree orig_arg1 = arg1;
3807 tree orig_arg2 = arg2;
3808 tree expr;
3810 if (processing_template_decl)
3812 if (type_dependent_expression_p (arg1)
3813 || type_dependent_expression_p (arg2))
3814 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3815 NULL_TREE, NULL_TREE);
3816 arg1 = build_non_dependent_expr (arg1);
3817 arg2 = build_non_dependent_expr (arg2);
3820 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3821 NULL_TREE, /*overload=*/NULL, complain);
3823 if (processing_template_decl && expr != error_mark_node)
3824 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3825 NULL_TREE, NULL_TREE);
3826 return expr;
3829 /* Return whether OP is an expression of enum type cast to integer
3830 type. In C++ even unsigned enum types are cast to signed integer
3831 types. We do not want to issue warnings about comparisons between
3832 signed and unsigned types when one of the types is an enum type.
3833 Those warnings are always false positives in practice. */
3835 static bool
3836 enum_cast_to_int (tree op)
3838 if (CONVERT_EXPR_P (op)
3839 && TREE_TYPE (op) == integer_type_node
3840 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3841 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3842 return true;
3844 /* The cast may have been pushed into a COND_EXPR. */
3845 if (TREE_CODE (op) == COND_EXPR)
3846 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3847 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3849 return false;
3852 /* For the c-common bits. */
3853 tree
3854 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3855 int /*convert_p*/)
3857 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3861 /* Build a binary-operation expression without default conversions.
3862 CODE is the kind of expression to build.
3863 LOCATION is the location_t of the operator in the source code.
3864 This function differs from `build' in several ways:
3865 the data type of the result is computed and recorded in it,
3866 warnings are generated if arg data types are invalid,
3867 special handling for addition and subtraction of pointers is known,
3868 and some optimization is done (operations on narrow ints
3869 are done in the narrower type when that gives the same result).
3870 Constant folding is also done before the result is returned.
3872 Note that the operands will never have enumeral types
3873 because either they have just had the default conversions performed
3874 or they have both just been converted to some other type in which
3875 the arithmetic is to be done.
3877 C++: must do special pointer arithmetic when implementing
3878 multiple inheritance, and deal with pointer to member functions. */
3880 tree
3881 cp_build_binary_op (location_t location,
3882 enum tree_code code, tree orig_op0, tree orig_op1,
3883 tsubst_flags_t complain)
3885 tree op0, op1;
3886 enum tree_code code0, code1;
3887 tree type0, type1;
3888 const char *invalid_op_diag;
3890 /* Expression code to give to the expression when it is built.
3891 Normally this is CODE, which is what the caller asked for,
3892 but in some special cases we change it. */
3893 enum tree_code resultcode = code;
3895 /* Data type in which the computation is to be performed.
3896 In the simplest cases this is the common type of the arguments. */
3897 tree result_type = NULL;
3899 /* Nonzero means operands have already been type-converted
3900 in whatever way is necessary.
3901 Zero means they need to be converted to RESULT_TYPE. */
3902 int converted = 0;
3904 /* Nonzero means create the expression with this type, rather than
3905 RESULT_TYPE. */
3906 tree build_type = 0;
3908 /* Nonzero means after finally constructing the expression
3909 convert it to this type. */
3910 tree final_type = 0;
3912 tree result;
3913 tree orig_type = NULL;
3915 /* Nonzero if this is an operation like MIN or MAX which can
3916 safely be computed in short if both args are promoted shorts.
3917 Also implies COMMON.
3918 -1 indicates a bitwise operation; this makes a difference
3919 in the exact conditions for when it is safe to do the operation
3920 in a narrower mode. */
3921 int shorten = 0;
3923 /* Nonzero if this is a comparison operation;
3924 if both args are promoted shorts, compare the original shorts.
3925 Also implies COMMON. */
3926 int short_compare = 0;
3928 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3929 int common = 0;
3931 /* True if both operands have arithmetic type. */
3932 bool arithmetic_types_p;
3934 /* Apply default conversions. */
3935 op0 = orig_op0;
3936 op1 = orig_op1;
3938 /* Remember whether we're doing / or %. */
3939 bool doing_div_or_mod = false;
3941 /* Remember whether we're doing << or >>. */
3942 bool doing_shift = false;
3944 /* Tree holding instrumentation expression. */
3945 tree instrument_expr = NULL;
3947 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3948 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3949 || code == TRUTH_XOR_EXPR)
3951 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3952 op0 = decay_conversion (op0, complain);
3953 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3954 op1 = decay_conversion (op1, complain);
3956 else
3958 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3959 op0 = cp_default_conversion (op0, complain);
3960 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3961 op1 = cp_default_conversion (op1, complain);
3964 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3965 STRIP_TYPE_NOPS (op0);
3966 STRIP_TYPE_NOPS (op1);
3968 /* DTRT if one side is an overloaded function, but complain about it. */
3969 if (type_unknown_p (op0))
3971 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3972 if (t != error_mark_node)
3974 if (complain & tf_error)
3975 permerror (input_location, "assuming cast to type %qT from overloaded function",
3976 TREE_TYPE (t));
3977 op0 = t;
3980 if (type_unknown_p (op1))
3982 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3983 if (t != error_mark_node)
3985 if (complain & tf_error)
3986 permerror (input_location, "assuming cast to type %qT from overloaded function",
3987 TREE_TYPE (t));
3988 op1 = t;
3992 type0 = TREE_TYPE (op0);
3993 type1 = TREE_TYPE (op1);
3995 /* The expression codes of the data types of the arguments tell us
3996 whether the arguments are integers, floating, pointers, etc. */
3997 code0 = TREE_CODE (type0);
3998 code1 = TREE_CODE (type1);
4000 /* If an error was already reported for one of the arguments,
4001 avoid reporting another error. */
4002 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4003 return error_mark_node;
4005 if ((invalid_op_diag
4006 = targetm.invalid_binary_op (code, type0, type1)))
4008 if (complain & tf_error)
4009 error (invalid_op_diag);
4010 return error_mark_node;
4013 /* Issue warnings about peculiar, but valid, uses of NULL. */
4014 if ((orig_op0 == null_node || orig_op1 == null_node)
4015 /* It's reasonable to use pointer values as operands of &&
4016 and ||, so NULL is no exception. */
4017 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4018 && ( /* Both are NULL (or 0) and the operation was not a
4019 comparison or a pointer subtraction. */
4020 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4021 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4022 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4023 || (!null_ptr_cst_p (orig_op0)
4024 && !TYPE_PTR_OR_PTRMEM_P (type0))
4025 || (!null_ptr_cst_p (orig_op1)
4026 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4027 && (complain & tf_warning))
4029 source_location loc =
4030 expansion_point_location_if_in_system_header (input_location);
4032 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4035 /* In case when one of the operands of the binary operation is
4036 a vector and another is a scalar -- convert scalar to vector. */
4037 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4039 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4040 complain & tf_error);
4042 switch (convert_flag)
4044 case stv_error:
4045 return error_mark_node;
4046 case stv_firstarg:
4048 op0 = convert (TREE_TYPE (type1), op0);
4049 op0 = save_expr (op0);
4050 op0 = build_vector_from_val (type1, op0);
4051 type0 = TREE_TYPE (op0);
4052 code0 = TREE_CODE (type0);
4053 converted = 1;
4054 break;
4056 case stv_secondarg:
4058 op1 = convert (TREE_TYPE (type0), op1);
4059 op1 = save_expr (op1);
4060 op1 = build_vector_from_val (type0, op1);
4061 type1 = TREE_TYPE (op1);
4062 code1 = TREE_CODE (type1);
4063 converted = 1;
4064 break;
4066 default:
4067 break;
4071 switch (code)
4073 case MINUS_EXPR:
4074 /* Subtraction of two similar pointers.
4075 We must subtract them as integers, then divide by object size. */
4076 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4077 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4078 TREE_TYPE (type1)))
4079 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4080 complain);
4081 /* In all other cases except pointer - int, the usual arithmetic
4082 rules apply. */
4083 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4085 common = 1;
4086 break;
4088 /* The pointer - int case is just like pointer + int; fall
4089 through. */
4090 case PLUS_EXPR:
4091 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4092 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4094 tree ptr_operand;
4095 tree int_operand;
4096 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4097 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4098 if (processing_template_decl)
4100 result_type = TREE_TYPE (ptr_operand);
4101 break;
4103 return cp_pointer_int_sum (code,
4104 ptr_operand,
4105 int_operand,
4106 complain);
4108 common = 1;
4109 break;
4111 case MULT_EXPR:
4112 common = 1;
4113 break;
4115 case TRUNC_DIV_EXPR:
4116 case CEIL_DIV_EXPR:
4117 case FLOOR_DIV_EXPR:
4118 case ROUND_DIV_EXPR:
4119 case EXACT_DIV_EXPR:
4120 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4121 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4122 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4123 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4125 enum tree_code tcode0 = code0, tcode1 = code1;
4126 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4127 cop1 = maybe_constant_value (cop1);
4128 doing_div_or_mod = true;
4129 warn_for_div_by_zero (location, cop1);
4131 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4132 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4133 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4134 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4136 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4137 resultcode = RDIV_EXPR;
4138 else
4139 /* When dividing two signed integers, we have to promote to int.
4140 unless we divide by a constant != -1. Note that default
4141 conversion will have been performed on the operands at this
4142 point, so we have to dig out the original type to find out if
4143 it was unsigned. */
4144 shorten = ((TREE_CODE (op0) == NOP_EXPR
4145 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4146 || (TREE_CODE (op1) == INTEGER_CST
4147 && ! integer_all_onesp (op1)));
4149 common = 1;
4151 break;
4153 case BIT_AND_EXPR:
4154 case BIT_IOR_EXPR:
4155 case BIT_XOR_EXPR:
4156 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4157 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4158 && !VECTOR_FLOAT_TYPE_P (type0)
4159 && !VECTOR_FLOAT_TYPE_P (type1)))
4160 shorten = -1;
4161 break;
4163 case TRUNC_MOD_EXPR:
4164 case FLOOR_MOD_EXPR:
4166 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4167 cop1 = maybe_constant_value (cop1);
4168 doing_div_or_mod = true;
4169 warn_for_div_by_zero (location, cop1);
4172 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4173 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4174 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4175 common = 1;
4176 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4178 /* Although it would be tempting to shorten always here, that loses
4179 on some targets, since the modulo instruction is undefined if the
4180 quotient can't be represented in the computation mode. We shorten
4181 only if unsigned or if dividing by something we know != -1. */
4182 shorten = ((TREE_CODE (op0) == NOP_EXPR
4183 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4184 || (TREE_CODE (op1) == INTEGER_CST
4185 && ! integer_all_onesp (op1)));
4186 common = 1;
4188 break;
4190 case TRUTH_ANDIF_EXPR:
4191 case TRUTH_ORIF_EXPR:
4192 case TRUTH_AND_EXPR:
4193 case TRUTH_OR_EXPR:
4194 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4196 if (!COMPARISON_CLASS_P (op1))
4197 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4198 build_zero_cst (type1), complain);
4199 if (code == TRUTH_ANDIF_EXPR)
4201 tree z = build_zero_cst (TREE_TYPE (op1));
4202 return build_conditional_expr (location, op0, op1, z, complain);
4204 else if (code == TRUTH_ORIF_EXPR)
4206 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4207 return build_conditional_expr (location, op0, m1, op1, complain);
4209 else
4210 gcc_unreachable ();
4212 if (VECTOR_TYPE_P (type0))
4214 if (!COMPARISON_CLASS_P (op0))
4215 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4216 build_zero_cst (type0), complain);
4217 if (!VECTOR_TYPE_P (type1))
4219 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4220 tree z = build_zero_cst (TREE_TYPE (op0));
4221 op1 = build_conditional_expr (location, op1, z, m1, complain);
4223 else if (!COMPARISON_CLASS_P (op1))
4224 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4225 build_zero_cst (type1), complain);
4227 if (code == TRUTH_ANDIF_EXPR)
4228 code = BIT_AND_EXPR;
4229 else if (code == TRUTH_ORIF_EXPR)
4230 code = BIT_IOR_EXPR;
4231 else
4232 gcc_unreachable ();
4234 return cp_build_binary_op (location, code, op0, op1, complain);
4237 result_type = boolean_type_node;
4238 break;
4240 /* Shift operations: result has same type as first operand;
4241 always convert second operand to int.
4242 Also set SHORT_SHIFT if shifting rightward. */
4244 case RSHIFT_EXPR:
4245 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4246 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4248 result_type = type0;
4249 converted = 1;
4251 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4252 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4253 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4254 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4256 result_type = type0;
4257 converted = 1;
4259 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4261 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4262 const_op1 = maybe_constant_value (const_op1);
4263 if (TREE_CODE (const_op1) != INTEGER_CST)
4264 const_op1 = op1;
4265 result_type = type0;
4266 doing_shift = true;
4267 if (TREE_CODE (const_op1) == INTEGER_CST)
4269 if (tree_int_cst_lt (const_op1, integer_zero_node))
4271 if ((complain & tf_warning)
4272 && c_inhibit_evaluation_warnings == 0)
4273 warning (0, "right shift count is negative");
4275 else
4277 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4278 && (complain & tf_warning)
4279 && c_inhibit_evaluation_warnings == 0)
4280 warning (0, "right shift count >= width of type");
4283 /* Convert the shift-count to an integer, regardless of
4284 size of value being shifted. */
4285 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4286 op1 = cp_convert (integer_type_node, op1, complain);
4287 /* Avoid converting op1 to result_type later. */
4288 converted = 1;
4290 break;
4292 case LSHIFT_EXPR:
4293 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4294 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4296 result_type = type0;
4297 converted = 1;
4299 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4300 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4301 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4302 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4304 result_type = type0;
4305 converted = 1;
4307 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4309 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4310 const_op1 = maybe_constant_value (const_op1);
4311 if (TREE_CODE (const_op1) != INTEGER_CST)
4312 const_op1 = op1;
4313 result_type = type0;
4314 doing_shift = true;
4315 if (TREE_CODE (const_op1) == INTEGER_CST)
4317 if (tree_int_cst_lt (const_op1, integer_zero_node))
4319 if ((complain & tf_warning)
4320 && c_inhibit_evaluation_warnings == 0)
4321 warning (0, "left shift count is negative");
4323 else if (compare_tree_int (const_op1,
4324 TYPE_PRECISION (type0)) >= 0)
4326 if ((complain & tf_warning)
4327 && c_inhibit_evaluation_warnings == 0)
4328 warning (0, "left shift count >= width of type");
4331 /* Convert the shift-count to an integer, regardless of
4332 size of value being shifted. */
4333 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4334 op1 = cp_convert (integer_type_node, op1, complain);
4335 /* Avoid converting op1 to result_type later. */
4336 converted = 1;
4338 break;
4340 case RROTATE_EXPR:
4341 case LROTATE_EXPR:
4342 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4344 result_type = type0;
4345 if (TREE_CODE (op1) == INTEGER_CST)
4347 if (tree_int_cst_lt (op1, integer_zero_node))
4349 if (complain & tf_warning)
4350 warning (0, (code == LROTATE_EXPR)
4351 ? G_("left rotate count is negative")
4352 : G_("right rotate count is negative"));
4354 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4356 if (complain & tf_warning)
4357 warning (0, (code == LROTATE_EXPR)
4358 ? G_("left rotate count >= width of type")
4359 : G_("right rotate count >= width of type"));
4362 /* Convert the shift-count to an integer, regardless of
4363 size of value being shifted. */
4364 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4365 op1 = cp_convert (integer_type_node, op1, complain);
4367 break;
4369 case EQ_EXPR:
4370 case NE_EXPR:
4371 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4372 goto vector_compare;
4373 if ((complain & tf_warning)
4374 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4375 warning (OPT_Wfloat_equal,
4376 "comparing floating point with == or != is unsafe");
4377 if ((complain & tf_warning)
4378 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4379 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4380 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4382 build_type = boolean_type_node;
4383 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4384 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4385 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4386 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4387 short_compare = 1;
4388 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4389 && null_ptr_cst_p (op1))
4390 /* Handle, eg, (void*)0 (c++/43906), and more. */
4391 || (code0 == POINTER_TYPE
4392 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4394 if (TYPE_PTR_P (type1))
4395 result_type = composite_pointer_type (type0, type1, op0, op1,
4396 CPO_COMPARISON, complain);
4397 else
4398 result_type = type0;
4400 if (TREE_CODE (op0) == ADDR_EXPR
4401 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4403 if ((complain & tf_warning)
4404 && c_inhibit_evaluation_warnings == 0)
4405 warning (OPT_Waddress, "the address of %qD will never be NULL",
4406 TREE_OPERAND (op0, 0));
4409 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4410 && null_ptr_cst_p (op0))
4411 /* Handle, eg, (void*)0 (c++/43906), and more. */
4412 || (code1 == POINTER_TYPE
4413 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4415 if (TYPE_PTR_P (type0))
4416 result_type = composite_pointer_type (type0, type1, op0, op1,
4417 CPO_COMPARISON, complain);
4418 else
4419 result_type = type1;
4421 if (TREE_CODE (op1) == ADDR_EXPR
4422 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4424 if ((complain & tf_warning)
4425 && c_inhibit_evaluation_warnings == 0)
4426 warning (OPT_Waddress, "the address of %qD will never be NULL",
4427 TREE_OPERAND (op1, 0));
4430 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4431 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4432 result_type = composite_pointer_type (type0, type1, op0, op1,
4433 CPO_COMPARISON, complain);
4434 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4435 /* One of the operands must be of nullptr_t type. */
4436 result_type = TREE_TYPE (nullptr_node);
4437 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4439 result_type = type0;
4440 if (complain & tf_error)
4441 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4442 else
4443 return error_mark_node;
4445 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4447 result_type = type1;
4448 if (complain & tf_error)
4449 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4450 else
4451 return error_mark_node;
4453 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4455 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4456 == ptrmemfunc_vbit_in_delta)
4458 tree pfn0, delta0, e1, e2;
4460 if (TREE_SIDE_EFFECTS (op0))
4461 op0 = save_expr (op0);
4463 pfn0 = pfn_from_ptrmemfunc (op0);
4464 delta0 = delta_from_ptrmemfunc (op0);
4465 e1 = cp_build_binary_op (location,
4466 EQ_EXPR,
4467 pfn0,
4468 build_zero_cst (TREE_TYPE (pfn0)),
4469 complain);
4470 e2 = cp_build_binary_op (location,
4471 BIT_AND_EXPR,
4472 delta0,
4473 integer_one_node,
4474 complain);
4476 if (complain & tf_warning)
4477 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4479 e2 = cp_build_binary_op (location,
4480 EQ_EXPR, e2, integer_zero_node,
4481 complain);
4482 op0 = cp_build_binary_op (location,
4483 TRUTH_ANDIF_EXPR, e1, e2,
4484 complain);
4485 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4487 else
4489 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4490 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4492 result_type = TREE_TYPE (op0);
4494 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4495 return cp_build_binary_op (location, code, op1, op0, complain);
4496 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4498 tree type;
4499 /* E will be the final comparison. */
4500 tree e;
4501 /* E1 and E2 are for scratch. */
4502 tree e1;
4503 tree e2;
4504 tree pfn0;
4505 tree pfn1;
4506 tree delta0;
4507 tree delta1;
4509 type = composite_pointer_type (type0, type1, op0, op1,
4510 CPO_COMPARISON, complain);
4512 if (!same_type_p (TREE_TYPE (op0), type))
4513 op0 = cp_convert_and_check (type, op0, complain);
4514 if (!same_type_p (TREE_TYPE (op1), type))
4515 op1 = cp_convert_and_check (type, op1, complain);
4517 if (op0 == error_mark_node || op1 == error_mark_node)
4518 return error_mark_node;
4520 if (TREE_SIDE_EFFECTS (op0))
4521 op0 = save_expr (op0);
4522 if (TREE_SIDE_EFFECTS (op1))
4523 op1 = save_expr (op1);
4525 pfn0 = pfn_from_ptrmemfunc (op0);
4526 pfn1 = pfn_from_ptrmemfunc (op1);
4527 delta0 = delta_from_ptrmemfunc (op0);
4528 delta1 = delta_from_ptrmemfunc (op1);
4529 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4530 == ptrmemfunc_vbit_in_delta)
4532 /* We generate:
4534 (op0.pfn == op1.pfn
4535 && ((op0.delta == op1.delta)
4536 || (!op0.pfn && op0.delta & 1 == 0
4537 && op1.delta & 1 == 0))
4539 The reason for the `!op0.pfn' bit is that a NULL
4540 pointer-to-member is any member with a zero PFN and
4541 LSB of the DELTA field is 0. */
4543 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4544 delta0,
4545 integer_one_node,
4546 complain);
4547 e1 = cp_build_binary_op (location,
4548 EQ_EXPR, e1, integer_zero_node,
4549 complain);
4550 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4551 delta1,
4552 integer_one_node,
4553 complain);
4554 e2 = cp_build_binary_op (location,
4555 EQ_EXPR, e2, integer_zero_node,
4556 complain);
4557 e1 = cp_build_binary_op (location,
4558 TRUTH_ANDIF_EXPR, e2, e1,
4559 complain);
4560 e2 = cp_build_binary_op (location, EQ_EXPR,
4561 pfn0,
4562 build_zero_cst (TREE_TYPE (pfn0)),
4563 complain);
4564 e2 = cp_build_binary_op (location,
4565 TRUTH_ANDIF_EXPR, e2, e1, complain);
4566 e1 = cp_build_binary_op (location,
4567 EQ_EXPR, delta0, delta1, complain);
4568 e1 = cp_build_binary_op (location,
4569 TRUTH_ORIF_EXPR, e1, e2, complain);
4571 else
4573 /* We generate:
4575 (op0.pfn == op1.pfn
4576 && (!op0.pfn || op0.delta == op1.delta))
4578 The reason for the `!op0.pfn' bit is that a NULL
4579 pointer-to-member is any member with a zero PFN; the
4580 DELTA field is unspecified. */
4582 e1 = cp_build_binary_op (location,
4583 EQ_EXPR, delta0, delta1, complain);
4584 e2 = cp_build_binary_op (location,
4585 EQ_EXPR,
4586 pfn0,
4587 build_zero_cst (TREE_TYPE (pfn0)),
4588 complain);
4589 e1 = cp_build_binary_op (location,
4590 TRUTH_ORIF_EXPR, e1, e2, complain);
4592 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4593 e = cp_build_binary_op (location,
4594 TRUTH_ANDIF_EXPR, e2, e1, complain);
4595 if (code == EQ_EXPR)
4596 return e;
4597 return cp_build_binary_op (location,
4598 EQ_EXPR, e, integer_zero_node, complain);
4600 else
4602 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4603 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4604 type1));
4605 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4606 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4607 type0));
4610 break;
4612 case MAX_EXPR:
4613 case MIN_EXPR:
4614 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4615 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4616 shorten = 1;
4617 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4618 result_type = composite_pointer_type (type0, type1, op0, op1,
4619 CPO_COMPARISON, complain);
4620 break;
4622 case LE_EXPR:
4623 case GE_EXPR:
4624 case LT_EXPR:
4625 case GT_EXPR:
4626 if (TREE_CODE (orig_op0) == STRING_CST
4627 || TREE_CODE (orig_op1) == STRING_CST)
4629 if (complain & tf_warning)
4630 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4633 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4635 vector_compare:
4636 tree intt;
4637 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4638 TREE_TYPE (type1))
4639 && !vector_types_compatible_elements_p (type0, type1))
4641 if (complain & tf_error)
4643 error_at (location, "comparing vectors with different "
4644 "element types");
4645 inform (location, "operand types are %qT and %qT",
4646 type0, type1);
4648 return error_mark_node;
4651 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4653 if (complain & tf_error)
4655 error_at (location, "comparing vectors with different "
4656 "number of elements");
4657 inform (location, "operand types are %qT and %qT",
4658 type0, type1);
4660 return error_mark_node;
4663 /* Always construct signed integer vector type. */
4664 intt = c_common_type_for_size (GET_MODE_BITSIZE
4665 (TYPE_MODE (TREE_TYPE (type0))), 0);
4666 if (!intt)
4668 if (complain & tf_error)
4669 error_at (location, "could not find an integer type "
4670 "of the same size as %qT", TREE_TYPE (type0));
4671 return error_mark_node;
4673 result_type = build_opaque_vector_type (intt,
4674 TYPE_VECTOR_SUBPARTS (type0));
4675 converted = 1;
4676 break;
4678 build_type = boolean_type_node;
4679 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4680 || code0 == ENUMERAL_TYPE)
4681 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4682 || code1 == ENUMERAL_TYPE))
4683 short_compare = 1;
4684 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4685 result_type = composite_pointer_type (type0, type1, op0, op1,
4686 CPO_COMPARISON, complain);
4687 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4689 result_type = type0;
4690 if (extra_warnings && (complain & tf_warning))
4691 warning (OPT_Wextra,
4692 "ordered comparison of pointer with integer zero");
4694 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4696 result_type = type1;
4697 if (extra_warnings && (complain & tf_warning))
4698 warning (OPT_Wextra,
4699 "ordered comparison of pointer with integer zero");
4701 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4702 /* One of the operands must be of nullptr_t type. */
4703 result_type = TREE_TYPE (nullptr_node);
4704 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4706 result_type = type0;
4707 if (complain & tf_error)
4708 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4709 else
4710 return error_mark_node;
4712 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4714 result_type = type1;
4715 if (complain & tf_error)
4716 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4717 else
4718 return error_mark_node;
4720 break;
4722 case UNORDERED_EXPR:
4723 case ORDERED_EXPR:
4724 case UNLT_EXPR:
4725 case UNLE_EXPR:
4726 case UNGT_EXPR:
4727 case UNGE_EXPR:
4728 case UNEQ_EXPR:
4729 build_type = integer_type_node;
4730 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4732 if (complain & tf_error)
4733 error ("unordered comparison on non-floating point argument");
4734 return error_mark_node;
4736 common = 1;
4737 break;
4739 default:
4740 break;
4743 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4744 || code0 == ENUMERAL_TYPE)
4745 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4746 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4747 arithmetic_types_p = 1;
4748 else
4750 arithmetic_types_p = 0;
4751 /* Vector arithmetic is only allowed when both sides are vectors. */
4752 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4754 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4755 || !vector_types_compatible_elements_p (type0, type1))
4757 if (complain & tf_error)
4758 binary_op_error (location, code, type0, type1);
4759 return error_mark_node;
4761 arithmetic_types_p = 1;
4764 /* Determine the RESULT_TYPE, if it is not already known. */
4765 if (!result_type
4766 && arithmetic_types_p
4767 && (shorten || common || short_compare))
4769 result_type = cp_common_type (type0, type1);
4770 if (complain & tf_warning)
4771 do_warn_double_promotion (result_type, type0, type1,
4772 "implicit conversion from %qT to %qT "
4773 "to match other operand of binary "
4774 "expression",
4775 location);
4778 if (!result_type)
4780 if (complain & tf_error)
4781 error ("invalid operands of types %qT and %qT to binary %qO",
4782 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4783 return error_mark_node;
4786 /* If we're in a template, the only thing we need to know is the
4787 RESULT_TYPE. */
4788 if (processing_template_decl)
4790 /* Since the middle-end checks the type when doing a build2, we
4791 need to build the tree in pieces. This built tree will never
4792 get out of the front-end as we replace it when instantiating
4793 the template. */
4794 tree tmp = build2 (resultcode,
4795 build_type ? build_type : result_type,
4796 NULL_TREE, op1);
4797 TREE_OPERAND (tmp, 0) = op0;
4798 return tmp;
4801 if (arithmetic_types_p)
4803 bool first_complex = (code0 == COMPLEX_TYPE);
4804 bool second_complex = (code1 == COMPLEX_TYPE);
4805 int none_complex = (!first_complex && !second_complex);
4807 /* Adapted from patch for c/24581. */
4808 if (first_complex != second_complex
4809 && (code == PLUS_EXPR
4810 || code == MINUS_EXPR
4811 || code == MULT_EXPR
4812 || (code == TRUNC_DIV_EXPR && first_complex))
4813 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4814 && flag_signed_zeros)
4816 /* An operation on mixed real/complex operands must be
4817 handled specially, but the language-independent code can
4818 more easily optimize the plain complex arithmetic if
4819 -fno-signed-zeros. */
4820 tree real_type = TREE_TYPE (result_type);
4821 tree real, imag;
4822 if (first_complex)
4824 if (TREE_TYPE (op0) != result_type)
4825 op0 = cp_convert_and_check (result_type, op0, complain);
4826 if (TREE_TYPE (op1) != real_type)
4827 op1 = cp_convert_and_check (real_type, op1, complain);
4829 else
4831 if (TREE_TYPE (op0) != real_type)
4832 op0 = cp_convert_and_check (real_type, op0, complain);
4833 if (TREE_TYPE (op1) != result_type)
4834 op1 = cp_convert_and_check (result_type, op1, complain);
4836 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4837 return error_mark_node;
4838 if (first_complex)
4840 op0 = save_expr (op0);
4841 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4842 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4843 switch (code)
4845 case MULT_EXPR:
4846 case TRUNC_DIV_EXPR:
4847 op1 = save_expr (op1);
4848 imag = build2 (resultcode, real_type, imag, op1);
4849 /* Fall through. */
4850 case PLUS_EXPR:
4851 case MINUS_EXPR:
4852 real = build2 (resultcode, real_type, real, op1);
4853 break;
4854 default:
4855 gcc_unreachable();
4858 else
4860 op1 = save_expr (op1);
4861 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4862 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4863 switch (code)
4865 case MULT_EXPR:
4866 op0 = save_expr (op0);
4867 imag = build2 (resultcode, real_type, op0, imag);
4868 /* Fall through. */
4869 case PLUS_EXPR:
4870 real = build2 (resultcode, real_type, op0, real);
4871 break;
4872 case MINUS_EXPR:
4873 real = build2 (resultcode, real_type, op0, real);
4874 imag = build1 (NEGATE_EXPR, real_type, imag);
4875 break;
4876 default:
4877 gcc_unreachable();
4880 real = fold_if_not_in_template (real);
4881 imag = fold_if_not_in_template (imag);
4882 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4883 result = fold_if_not_in_template (result);
4884 return result;
4887 /* For certain operations (which identify themselves by shorten != 0)
4888 if both args were extended from the same smaller type,
4889 do the arithmetic in that type and then extend.
4891 shorten !=0 and !=1 indicates a bitwise operation.
4892 For them, this optimization is safe only if
4893 both args are zero-extended or both are sign-extended.
4894 Otherwise, we might change the result.
4895 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4896 but calculated in (unsigned short) it would be (unsigned short)-1. */
4898 if (shorten && none_complex)
4900 orig_type = result_type;
4901 final_type = result_type;
4902 result_type = shorten_binary_op (result_type, op0, op1,
4903 shorten == -1);
4906 /* Comparison operations are shortened too but differently.
4907 They identify themselves by setting short_compare = 1. */
4909 if (short_compare)
4911 /* Don't write &op0, etc., because that would prevent op0
4912 from being kept in a register.
4913 Instead, make copies of the our local variables and
4914 pass the copies by reference, then copy them back afterward. */
4915 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4916 enum tree_code xresultcode = resultcode;
4917 tree val
4918 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4919 &xresultcode);
4920 if (val != 0)
4921 return cp_convert (boolean_type_node, val, complain);
4922 op0 = xop0, op1 = xop1;
4923 converted = 1;
4924 resultcode = xresultcode;
4927 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4928 && warn_sign_compare
4929 /* Do not warn until the template is instantiated; we cannot
4930 bound the ranges of the arguments until that point. */
4931 && !processing_template_decl
4932 && (complain & tf_warning)
4933 && c_inhibit_evaluation_warnings == 0
4934 /* Even unsigned enum types promote to signed int. We don't
4935 want to issue -Wsign-compare warnings for this case. */
4936 && !enum_cast_to_int (orig_op0)
4937 && !enum_cast_to_int (orig_op1))
4939 tree oop0 = maybe_constant_value (orig_op0);
4940 tree oop1 = maybe_constant_value (orig_op1);
4942 if (TREE_CODE (oop0) != INTEGER_CST)
4943 oop0 = orig_op0;
4944 if (TREE_CODE (oop1) != INTEGER_CST)
4945 oop1 = orig_op1;
4946 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4947 result_type, resultcode);
4951 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4952 Then the expression will be built.
4953 It will be given type FINAL_TYPE if that is nonzero;
4954 otherwise, it will be given type RESULT_TYPE. */
4955 if (! converted)
4957 if (TREE_TYPE (op0) != result_type)
4958 op0 = cp_convert_and_check (result_type, op0, complain);
4959 if (TREE_TYPE (op1) != result_type)
4960 op1 = cp_convert_and_check (result_type, op1, complain);
4962 if (op0 == error_mark_node || op1 == error_mark_node)
4963 return error_mark_node;
4966 if (build_type == NULL_TREE)
4967 build_type = result_type;
4969 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
4970 | SANITIZE_FLOAT_DIVIDE))
4971 && !processing_template_decl
4972 && current_function_decl != 0
4973 && !lookup_attribute ("no_sanitize_undefined",
4974 DECL_ATTRIBUTES (current_function_decl))
4975 && (doing_div_or_mod || doing_shift))
4977 /* OP0 and/or OP1 might have side-effects. */
4978 op0 = cp_save_expr (op0);
4979 op1 = cp_save_expr (op1);
4980 op0 = maybe_constant_value (fold_non_dependent_expr_sfinae (op0,
4981 tf_none));
4982 op1 = maybe_constant_value (fold_non_dependent_expr_sfinae (op1,
4983 tf_none));
4984 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
4985 | SANITIZE_FLOAT_DIVIDE)))
4987 /* For diagnostics we want to use the promoted types without
4988 shorten_binary_op. So convert the arguments to the
4989 original result_type. */
4990 tree cop0 = op0;
4991 tree cop1 = op1;
4992 if (orig_type != NULL && result_type != orig_type)
4994 cop0 = cp_convert (orig_type, op0, complain);
4995 cop1 = cp_convert (orig_type, op1, complain);
4997 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
4999 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5000 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5003 result = build2 (resultcode, build_type, op0, op1);
5004 result = fold_if_not_in_template (result);
5005 if (final_type != 0)
5006 result = cp_convert (final_type, result, complain);
5008 if (TREE_OVERFLOW_P (result)
5009 && !TREE_OVERFLOW_P (op0)
5010 && !TREE_OVERFLOW_P (op1))
5011 overflow_warning (location, result);
5013 if (instrument_expr != NULL)
5014 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
5015 instrument_expr, result);
5017 return result;
5020 /* Build a VEC_PERM_EXPR.
5021 This is a simple wrapper for c_build_vec_perm_expr. */
5022 tree
5023 build_x_vec_perm_expr (location_t loc,
5024 tree arg0, tree arg1, tree arg2,
5025 tsubst_flags_t complain)
5027 tree orig_arg0 = arg0;
5028 tree orig_arg1 = arg1;
5029 tree orig_arg2 = arg2;
5030 if (processing_template_decl)
5032 if (type_dependent_expression_p (arg0)
5033 || type_dependent_expression_p (arg1)
5034 || type_dependent_expression_p (arg2))
5035 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5036 arg0 = build_non_dependent_expr (arg0);
5037 if (arg1)
5038 arg1 = build_non_dependent_expr (arg1);
5039 arg2 = build_non_dependent_expr (arg2);
5041 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5042 if (processing_template_decl && exp != error_mark_node)
5043 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5044 orig_arg1, orig_arg2);
5045 return exp;
5048 /* Return a tree for the sum or difference (RESULTCODE says which)
5049 of pointer PTROP and integer INTOP. */
5051 static tree
5052 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5053 tsubst_flags_t complain)
5055 tree res_type = TREE_TYPE (ptrop);
5057 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5058 in certain circumstance (when it's valid to do so). So we need
5059 to make sure it's complete. We don't need to check here, if we
5060 can actually complete it at all, as those checks will be done in
5061 pointer_int_sum() anyway. */
5062 complete_type (TREE_TYPE (res_type));
5064 return pointer_int_sum (input_location, resultcode, ptrop,
5065 fold_if_not_in_template (intop),
5066 complain & tf_warning_or_error);
5069 /* Return a tree for the difference of pointers OP0 and OP1.
5070 The resulting tree has type int. */
5072 static tree
5073 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5075 tree result;
5076 tree restype = ptrdiff_type_node;
5077 tree target_type = TREE_TYPE (ptrtype);
5079 if (!complete_type_or_else (target_type, NULL_TREE))
5080 return error_mark_node;
5082 if (VOID_TYPE_P (target_type))
5084 if (complain & tf_error)
5085 permerror (input_location, "ISO C++ forbids using pointer of "
5086 "type %<void *%> in subtraction");
5087 else
5088 return error_mark_node;
5090 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5092 if (complain & tf_error)
5093 permerror (input_location, "ISO C++ forbids using pointer to "
5094 "a function in subtraction");
5095 else
5096 return error_mark_node;
5098 if (TREE_CODE (target_type) == METHOD_TYPE)
5100 if (complain & tf_error)
5101 permerror (input_location, "ISO C++ forbids using pointer to "
5102 "a method in subtraction");
5103 else
5104 return error_mark_node;
5107 /* First do the subtraction as integers;
5108 then drop through to build the divide operator. */
5110 op0 = cp_build_binary_op (input_location,
5111 MINUS_EXPR,
5112 cp_convert (restype, op0, complain),
5113 cp_convert (restype, op1, complain),
5114 complain);
5116 /* This generates an error if op1 is a pointer to an incomplete type. */
5117 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5119 if (complain & tf_error)
5120 error ("invalid use of a pointer to an incomplete type in "
5121 "pointer arithmetic");
5122 else
5123 return error_mark_node;
5126 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5128 if (complain & tf_error)
5129 error ("arithmetic on pointer to an empty aggregate");
5130 else
5131 return error_mark_node;
5134 op1 = (TYPE_PTROB_P (ptrtype)
5135 ? size_in_bytes (target_type)
5136 : integer_one_node);
5138 /* Do the division. */
5140 result = build2 (EXACT_DIV_EXPR, restype, op0,
5141 cp_convert (restype, op1, complain));
5142 return fold_if_not_in_template (result);
5145 /* Construct and perhaps optimize a tree representation
5146 for a unary operation. CODE, a tree_code, specifies the operation
5147 and XARG is the operand. */
5149 tree
5150 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5151 tsubst_flags_t complain)
5153 tree orig_expr = xarg;
5154 tree exp;
5155 int ptrmem = 0;
5157 if (processing_template_decl)
5159 if (type_dependent_expression_p (xarg))
5160 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5162 xarg = build_non_dependent_expr (xarg);
5165 exp = NULL_TREE;
5167 /* [expr.unary.op] says:
5169 The address of an object of incomplete type can be taken.
5171 (And is just the ordinary address operator, not an overloaded
5172 "operator &".) However, if the type is a template
5173 specialization, we must complete the type at this point so that
5174 an overloaded "operator &" will be available if required. */
5175 if (code == ADDR_EXPR
5176 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5177 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5178 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5179 || (TREE_CODE (xarg) == OFFSET_REF)))
5180 /* Don't look for a function. */;
5181 else
5182 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5183 NULL_TREE, /*overload=*/NULL, complain);
5184 if (!exp && code == ADDR_EXPR)
5186 if (is_overloaded_fn (xarg))
5188 tree fn = get_first_fn (xarg);
5189 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5191 if (complain & tf_error)
5192 error (DECL_CONSTRUCTOR_P (fn)
5193 ? G_("taking address of constructor %qE")
5194 : G_("taking address of destructor %qE"),
5195 xarg);
5196 return error_mark_node;
5200 /* A pointer to member-function can be formed only by saying
5201 &X::mf. */
5202 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5203 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5205 if (TREE_CODE (xarg) != OFFSET_REF
5206 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5208 if (complain & tf_error)
5210 error ("invalid use of %qE to form a "
5211 "pointer-to-member-function", xarg);
5212 if (TREE_CODE (xarg) != OFFSET_REF)
5213 inform (input_location, " a qualified-id is required");
5215 return error_mark_node;
5217 else
5219 if (complain & tf_error)
5220 error ("parentheses around %qE cannot be used to form a"
5221 " pointer-to-member-function",
5222 xarg);
5223 else
5224 return error_mark_node;
5225 PTRMEM_OK_P (xarg) = 1;
5229 if (TREE_CODE (xarg) == OFFSET_REF)
5231 ptrmem = PTRMEM_OK_P (xarg);
5233 if (!ptrmem && !flag_ms_extensions
5234 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5236 /* A single non-static member, make sure we don't allow a
5237 pointer-to-member. */
5238 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5239 TREE_OPERAND (xarg, 0),
5240 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5241 PTRMEM_OK_P (xarg) = ptrmem;
5245 exp = cp_build_addr_expr_strict (xarg, complain);
5248 if (processing_template_decl && exp != error_mark_node)
5249 exp = build_min_non_dep (code, exp, orig_expr,
5250 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5251 if (TREE_CODE (exp) == ADDR_EXPR)
5252 PTRMEM_OK_P (exp) = ptrmem;
5253 return exp;
5256 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5257 constants, where a null value is represented by an INTEGER_CST of
5258 -1. */
5260 tree
5261 cp_truthvalue_conversion (tree expr)
5263 tree type = TREE_TYPE (expr);
5264 if (TYPE_PTRDATAMEM_P (type)
5265 /* Avoid ICE on invalid use of non-static member function. */
5266 || TREE_CODE (expr) == FUNCTION_DECL)
5267 return build_binary_op (EXPR_LOCATION (expr),
5268 NE_EXPR, expr, nullptr_node, 1);
5269 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5271 /* With -Wzero-as-null-pointer-constant do not warn for an
5272 'if (p)' or a 'while (!p)', where p is a pointer. */
5273 tree ret;
5274 ++c_inhibit_evaluation_warnings;
5275 ret = c_common_truthvalue_conversion (input_location, expr);
5276 --c_inhibit_evaluation_warnings;
5277 return ret;
5279 else
5280 return c_common_truthvalue_conversion (input_location, expr);
5283 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5285 tree
5286 condition_conversion (tree expr)
5288 tree t;
5289 if (processing_template_decl)
5290 return expr;
5291 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5292 tf_warning_or_error, LOOKUP_NORMAL);
5293 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5294 return t;
5297 /* Returns the address of T. This function will fold away
5298 ADDR_EXPR of INDIRECT_REF. */
5300 tree
5301 build_address (tree t)
5303 if (error_operand_p (t) || !cxx_mark_addressable (t))
5304 return error_mark_node;
5305 t = build_fold_addr_expr (t);
5306 if (TREE_CODE (t) != ADDR_EXPR)
5307 t = rvalue (t);
5308 return t;
5311 /* Returns the address of T with type TYPE. */
5313 tree
5314 build_typed_address (tree t, tree type)
5316 if (error_operand_p (t) || !cxx_mark_addressable (t))
5317 return error_mark_node;
5318 t = build_fold_addr_expr_with_type (t, type);
5319 if (TREE_CODE (t) != ADDR_EXPR)
5320 t = rvalue (t);
5321 return t;
5324 /* Return a NOP_EXPR converting EXPR to TYPE. */
5326 tree
5327 build_nop (tree type, tree expr)
5329 if (type == error_mark_node || error_operand_p (expr))
5330 return expr;
5331 return build1 (NOP_EXPR, type, expr);
5334 /* Take the address of ARG, whatever that means under C++ semantics.
5335 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5336 and class rvalues as well.
5338 Nothing should call this function directly; instead, callers should use
5339 cp_build_addr_expr or cp_build_addr_expr_strict. */
5341 static tree
5342 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5344 tree argtype;
5345 tree val;
5347 if (!arg || error_operand_p (arg))
5348 return error_mark_node;
5350 arg = mark_lvalue_use (arg);
5351 argtype = lvalue_type (arg);
5353 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5355 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5356 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5358 /* They're trying to take the address of a unique non-static
5359 member function. This is ill-formed (except in MS-land),
5360 but let's try to DTRT.
5361 Note: We only handle unique functions here because we don't
5362 want to complain if there's a static overload; non-unique
5363 cases will be handled by instantiate_type. But we need to
5364 handle this case here to allow casts on the resulting PMF.
5365 We could defer this in non-MS mode, but it's easier to give
5366 a useful error here. */
5368 /* Inside constant member functions, the `this' pointer
5369 contains an extra const qualifier. TYPE_MAIN_VARIANT
5370 is used here to remove this const from the diagnostics
5371 and the created OFFSET_REF. */
5372 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5373 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5374 mark_used (fn);
5376 if (! flag_ms_extensions)
5378 tree name = DECL_NAME (fn);
5379 if (!(complain & tf_error))
5380 return error_mark_node;
5381 else if (current_class_type
5382 && TREE_OPERAND (arg, 0) == current_class_ref)
5383 /* An expression like &memfn. */
5384 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5385 " or parenthesized non-static member function to form"
5386 " a pointer to member function. Say %<&%T::%D%>",
5387 base, name);
5388 else
5389 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5390 " function to form a pointer to member function."
5391 " Say %<&%T::%D%>",
5392 base, name);
5394 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5397 /* Uninstantiated types are all functions. Taking the
5398 address of a function is a no-op, so just return the
5399 argument. */
5400 if (type_unknown_p (arg))
5401 return build1 (ADDR_EXPR, unknown_type_node, arg);
5403 if (TREE_CODE (arg) == OFFSET_REF)
5404 /* We want a pointer to member; bypass all the code for actually taking
5405 the address of something. */
5406 goto offset_ref;
5408 /* Anything not already handled and not a true memory reference
5409 is an error. */
5410 if (TREE_CODE (argtype) != FUNCTION_TYPE
5411 && TREE_CODE (argtype) != METHOD_TYPE)
5413 cp_lvalue_kind kind = lvalue_kind (arg);
5414 if (kind == clk_none)
5416 if (complain & tf_error)
5417 lvalue_error (input_location, lv_addressof);
5418 return error_mark_node;
5420 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5422 if (!(complain & tf_error))
5423 return error_mark_node;
5424 if (kind & clk_class)
5425 /* Make this a permerror because we used to accept it. */
5426 permerror (input_location, "taking address of temporary");
5427 else
5428 error ("taking address of xvalue (rvalue reference)");
5432 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5434 tree type = build_pointer_type (TREE_TYPE (argtype));
5435 arg = build1 (CONVERT_EXPR, type, arg);
5436 return arg;
5438 else if (pedantic && DECL_MAIN_P (arg))
5440 /* ARM $3.4 */
5441 /* Apparently a lot of autoconf scripts for C++ packages do this,
5442 so only complain if -Wpedantic. */
5443 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5444 pedwarn (input_location, OPT_Wpedantic,
5445 "ISO C++ forbids taking address of function %<::main%>");
5446 else if (flag_pedantic_errors)
5447 return error_mark_node;
5450 /* Let &* cancel out to simplify resulting code. */
5451 if (INDIRECT_REF_P (arg))
5453 /* We don't need to have `current_class_ptr' wrapped in a
5454 NON_LVALUE_EXPR node. */
5455 if (arg == current_class_ref)
5456 return current_class_ptr;
5458 arg = TREE_OPERAND (arg, 0);
5459 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5461 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5462 arg = build1 (CONVERT_EXPR, type, arg);
5464 else
5465 /* Don't let this be an lvalue. */
5466 arg = rvalue (arg);
5467 return arg;
5470 /* ??? Cope with user tricks that amount to offsetof. */
5471 if (TREE_CODE (argtype) != FUNCTION_TYPE
5472 && TREE_CODE (argtype) != METHOD_TYPE
5473 && argtype != unknown_type_node
5474 && (val = get_base_address (arg))
5475 && COMPLETE_TYPE_P (TREE_TYPE (val))
5476 && INDIRECT_REF_P (val)
5477 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5479 tree type = build_pointer_type (argtype);
5480 return fold_convert (type, fold_offsetof_1 (arg));
5483 /* Handle complex lvalues (when permitted)
5484 by reduction to simpler cases. */
5485 val = unary_complex_lvalue (ADDR_EXPR, arg);
5486 if (val != 0)
5487 return val;
5489 switch (TREE_CODE (arg))
5491 CASE_CONVERT:
5492 case FLOAT_EXPR:
5493 case FIX_TRUNC_EXPR:
5494 /* Even if we're not being pedantic, we cannot allow this
5495 extension when we're instantiating in a SFINAE
5496 context. */
5497 if (! lvalue_p (arg) && complain == tf_none)
5499 if (complain & tf_error)
5500 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5501 else
5502 return error_mark_node;
5504 break;
5506 case BASELINK:
5507 arg = BASELINK_FUNCTIONS (arg);
5508 /* Fall through. */
5510 case OVERLOAD:
5511 arg = OVL_CURRENT (arg);
5512 break;
5514 case OFFSET_REF:
5515 offset_ref:
5516 /* Turn a reference to a non-static data member into a
5517 pointer-to-member. */
5519 tree type;
5520 tree t;
5522 gcc_assert (PTRMEM_OK_P (arg));
5524 t = TREE_OPERAND (arg, 1);
5525 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5527 if (complain & tf_error)
5528 error ("cannot create pointer to reference member %qD", t);
5529 return error_mark_node;
5532 type = build_ptrmem_type (context_for_name_lookup (t),
5533 TREE_TYPE (t));
5534 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5535 return t;
5538 default:
5539 break;
5542 if (argtype != error_mark_node)
5544 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (argtype)
5545 && (flag_iso || warn_vla > 0))
5547 if (complain & tf_warning_or_error)
5548 pedwarn (input_location, OPT_Wvla,
5549 "taking address of array of runtime bound");
5550 else
5551 return error_mark_node;
5553 argtype = build_pointer_type (argtype);
5556 /* In a template, we are processing a non-dependent expression
5557 so we can just form an ADDR_EXPR with the correct type. */
5558 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5560 val = build_address (arg);
5561 if (TREE_CODE (arg) == OFFSET_REF)
5562 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5564 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5566 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5568 /* We can only get here with a single static member
5569 function. */
5570 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5571 && DECL_STATIC_FUNCTION_P (fn));
5572 mark_used (fn);
5573 val = build_address (fn);
5574 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5575 /* Do not lose object's side effects. */
5576 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5577 TREE_OPERAND (arg, 0), val);
5579 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5581 if (complain & tf_error)
5582 error ("attempt to take address of bit-field structure member %qD",
5583 TREE_OPERAND (arg, 1));
5584 return error_mark_node;
5586 else
5588 tree object = TREE_OPERAND (arg, 0);
5589 tree field = TREE_OPERAND (arg, 1);
5590 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5591 (TREE_TYPE (object), decl_type_context (field)));
5592 val = build_address (arg);
5595 if (TYPE_PTR_P (argtype)
5596 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5598 build_ptrmemfunc_type (argtype);
5599 val = build_ptrmemfunc (argtype, val, 0,
5600 /*c_cast_p=*/false,
5601 complain);
5604 return val;
5607 /* Take the address of ARG if it has one, even if it's an rvalue. */
5609 tree
5610 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5612 return cp_build_addr_expr_1 (arg, 0, complain);
5615 /* Take the address of ARG, but only if it's an lvalue. */
5617 tree
5618 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5620 return cp_build_addr_expr_1 (arg, 1, complain);
5623 /* C++: Must handle pointers to members.
5625 Perhaps type instantiation should be extended to handle conversion
5626 from aggregates to types we don't yet know we want? (Or are those
5627 cases typically errors which should be reported?)
5629 NOCONVERT nonzero suppresses the default promotions
5630 (such as from short to int). */
5632 tree
5633 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5634 tsubst_flags_t complain)
5636 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5637 tree arg = xarg;
5638 tree argtype = 0;
5639 const char *errstring = NULL;
5640 tree val;
5641 const char *invalid_op_diag;
5643 if (!arg || error_operand_p (arg))
5644 return error_mark_node;
5646 if ((invalid_op_diag
5647 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5648 ? CONVERT_EXPR
5649 : code),
5650 TREE_TYPE (xarg))))
5652 if (complain & tf_error)
5653 error (invalid_op_diag);
5654 return error_mark_node;
5657 switch (code)
5659 case UNARY_PLUS_EXPR:
5660 case NEGATE_EXPR:
5662 int flags = WANT_ARITH | WANT_ENUM;
5663 /* Unary plus (but not unary minus) is allowed on pointers. */
5664 if (code == UNARY_PLUS_EXPR)
5665 flags |= WANT_POINTER;
5666 arg = build_expr_type_conversion (flags, arg, true);
5667 if (!arg)
5668 errstring = (code == NEGATE_EXPR
5669 ? _("wrong type argument to unary minus")
5670 : _("wrong type argument to unary plus"));
5671 else
5673 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5674 arg = cp_perform_integral_promotions (arg, complain);
5676 /* Make sure the result is not an lvalue: a unary plus or minus
5677 expression is always a rvalue. */
5678 arg = rvalue (arg);
5681 break;
5683 case BIT_NOT_EXPR:
5684 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5686 code = CONJ_EXPR;
5687 if (!noconvert)
5689 arg = cp_default_conversion (arg, complain);
5690 if (arg == error_mark_node)
5691 return error_mark_node;
5694 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5695 | WANT_VECTOR_OR_COMPLEX,
5696 arg, true)))
5697 errstring = _("wrong type argument to bit-complement");
5698 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5699 arg = cp_perform_integral_promotions (arg, complain);
5700 break;
5702 case ABS_EXPR:
5703 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5704 errstring = _("wrong type argument to abs");
5705 else if (!noconvert)
5707 arg = cp_default_conversion (arg, complain);
5708 if (arg == error_mark_node)
5709 return error_mark_node;
5711 break;
5713 case CONJ_EXPR:
5714 /* Conjugating a real value is a no-op, but allow it anyway. */
5715 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5716 errstring = _("wrong type argument to conjugation");
5717 else if (!noconvert)
5719 arg = cp_default_conversion (arg, complain);
5720 if (arg == error_mark_node)
5721 return error_mark_node;
5723 break;
5725 case TRUTH_NOT_EXPR:
5726 if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg)))
5727 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5728 build_zero_cst (TREE_TYPE (arg)), complain);
5729 arg = perform_implicit_conversion (boolean_type_node, arg,
5730 complain);
5731 val = invert_truthvalue_loc (input_location, arg);
5732 if (arg != error_mark_node)
5733 return val;
5734 errstring = _("in argument to unary !");
5735 break;
5737 case NOP_EXPR:
5738 break;
5740 case REALPART_EXPR:
5741 case IMAGPART_EXPR:
5742 arg = build_real_imag_expr (input_location, code, arg);
5743 if (arg == error_mark_node)
5744 return arg;
5745 else
5746 return fold_if_not_in_template (arg);
5748 case PREINCREMENT_EXPR:
5749 case POSTINCREMENT_EXPR:
5750 case PREDECREMENT_EXPR:
5751 case POSTDECREMENT_EXPR:
5752 /* Handle complex lvalues (when permitted)
5753 by reduction to simpler cases. */
5755 val = unary_complex_lvalue (code, arg);
5756 if (val != 0)
5757 return val;
5759 arg = mark_lvalue_use (arg);
5761 /* Increment or decrement the real part of the value,
5762 and don't change the imaginary part. */
5763 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5765 tree real, imag;
5767 arg = stabilize_reference (arg);
5768 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5769 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5770 real = cp_build_unary_op (code, real, 1, complain);
5771 if (real == error_mark_node || imag == error_mark_node)
5772 return error_mark_node;
5773 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5774 real, imag);
5777 /* Report invalid types. */
5779 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5780 arg, true)))
5782 if (code == PREINCREMENT_EXPR)
5783 errstring = _("no pre-increment operator for type");
5784 else if (code == POSTINCREMENT_EXPR)
5785 errstring = _("no post-increment operator for type");
5786 else if (code == PREDECREMENT_EXPR)
5787 errstring = _("no pre-decrement operator for type");
5788 else
5789 errstring = _("no post-decrement operator for type");
5790 break;
5792 else if (arg == error_mark_node)
5793 return error_mark_node;
5795 /* Report something read-only. */
5797 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5798 || TREE_READONLY (arg))
5800 if (complain & tf_error)
5801 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5802 || code == POSTINCREMENT_EXPR)
5803 ? lv_increment : lv_decrement));
5804 else
5805 return error_mark_node;
5809 tree inc;
5810 tree declared_type = unlowered_expr_type (arg);
5812 argtype = TREE_TYPE (arg);
5814 /* ARM $5.2.5 last annotation says this should be forbidden. */
5815 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5817 if (complain & tf_error)
5818 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5819 ? G_("ISO C++ forbids incrementing an enum")
5820 : G_("ISO C++ forbids decrementing an enum"));
5821 else
5822 return error_mark_node;
5825 /* Compute the increment. */
5827 if (TYPE_PTR_P (argtype))
5829 tree type = complete_type (TREE_TYPE (argtype));
5831 if (!COMPLETE_OR_VOID_TYPE_P (type))
5833 if (complain & tf_error)
5834 error (((code == PREINCREMENT_EXPR
5835 || code == POSTINCREMENT_EXPR))
5836 ? G_("cannot increment a pointer to incomplete type %qT")
5837 : G_("cannot decrement a pointer to incomplete type %qT"),
5838 TREE_TYPE (argtype));
5839 else
5840 return error_mark_node;
5842 else if (!TYPE_PTROB_P (argtype))
5844 if (complain & tf_error)
5845 pedwarn (input_location, OPT_Wpointer_arith,
5846 (code == PREINCREMENT_EXPR
5847 || code == POSTINCREMENT_EXPR)
5848 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5849 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5850 argtype);
5851 else
5852 return error_mark_node;
5855 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5857 else
5858 inc = VECTOR_TYPE_P (argtype)
5859 ? build_one_cst (argtype)
5860 : integer_one_node;
5862 inc = cp_convert (argtype, inc, complain);
5864 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5865 need to ask Objective-C to build the increment or decrement
5866 expression for it. */
5867 if (objc_is_property_ref (arg))
5868 return objc_build_incr_expr_for_property_ref (input_location, code,
5869 arg, inc);
5871 /* Complain about anything else that is not a true lvalue. */
5872 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5873 || code == POSTINCREMENT_EXPR)
5874 ? lv_increment : lv_decrement),
5875 complain))
5876 return error_mark_node;
5878 /* Forbid using -- on `bool'. */
5879 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5881 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5883 if (complain & tf_error)
5884 error ("invalid use of Boolean expression as operand "
5885 "to %<operator--%>");
5886 return error_mark_node;
5888 val = boolean_increment (code, arg);
5890 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5891 /* An rvalue has no cv-qualifiers. */
5892 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5893 else
5894 val = build2 (code, TREE_TYPE (arg), arg, inc);
5896 TREE_SIDE_EFFECTS (val) = 1;
5897 return val;
5900 case ADDR_EXPR:
5901 /* Note that this operation never does default_conversion
5902 regardless of NOCONVERT. */
5903 return cp_build_addr_expr (arg, complain);
5905 default:
5906 break;
5909 if (!errstring)
5911 if (argtype == 0)
5912 argtype = TREE_TYPE (arg);
5913 return fold_if_not_in_template (build1 (code, argtype, arg));
5916 if (complain & tf_error)
5917 error ("%s", errstring);
5918 return error_mark_node;
5921 /* Hook for the c-common bits that build a unary op. */
5922 tree
5923 build_unary_op (location_t /*location*/,
5924 enum tree_code code, tree xarg, int noconvert)
5926 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5929 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5930 for certain kinds of expressions which are not really lvalues
5931 but which we can accept as lvalues.
5933 If ARG is not a kind of expression we can handle, return
5934 NULL_TREE. */
5936 tree
5937 unary_complex_lvalue (enum tree_code code, tree arg)
5939 /* Inside a template, making these kinds of adjustments is
5940 pointless; we are only concerned with the type of the
5941 expression. */
5942 if (processing_template_decl)
5943 return NULL_TREE;
5945 /* Handle (a, b) used as an "lvalue". */
5946 if (TREE_CODE (arg) == COMPOUND_EXPR)
5948 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5949 tf_warning_or_error);
5950 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5951 TREE_OPERAND (arg, 0), real_result);
5954 /* Handle (a ? b : c) used as an "lvalue". */
5955 if (TREE_CODE (arg) == COND_EXPR
5956 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5957 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5959 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5960 if (TREE_CODE (arg) == MODIFY_EXPR
5961 || TREE_CODE (arg) == PREINCREMENT_EXPR
5962 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5964 tree lvalue = TREE_OPERAND (arg, 0);
5965 if (TREE_SIDE_EFFECTS (lvalue))
5967 lvalue = stabilize_reference (lvalue);
5968 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5969 lvalue, TREE_OPERAND (arg, 1));
5971 return unary_complex_lvalue
5972 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5975 if (code != ADDR_EXPR)
5976 return NULL_TREE;
5978 /* Handle (a = b) used as an "lvalue" for `&'. */
5979 if (TREE_CODE (arg) == MODIFY_EXPR
5980 || TREE_CODE (arg) == INIT_EXPR)
5982 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5983 tf_warning_or_error);
5984 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5985 arg, real_result);
5986 TREE_NO_WARNING (arg) = 1;
5987 return arg;
5990 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5991 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5992 || TREE_CODE (arg) == OFFSET_REF)
5993 return NULL_TREE;
5995 /* We permit compiler to make function calls returning
5996 objects of aggregate type look like lvalues. */
5998 tree targ = arg;
6000 if (TREE_CODE (targ) == SAVE_EXPR)
6001 targ = TREE_OPERAND (targ, 0);
6003 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6005 if (TREE_CODE (arg) == SAVE_EXPR)
6006 targ = arg;
6007 else
6008 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6009 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6012 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6013 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6014 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6017 /* Don't let anything else be handled specially. */
6018 return NULL_TREE;
6021 /* Mark EXP saying that we need to be able to take the
6022 address of it; it should not be allocated in a register.
6023 Value is true if successful.
6025 C++: we do not allow `current_class_ptr' to be addressable. */
6027 bool
6028 cxx_mark_addressable (tree exp)
6030 tree x = exp;
6032 while (1)
6033 switch (TREE_CODE (x))
6035 case ADDR_EXPR:
6036 case COMPONENT_REF:
6037 case ARRAY_REF:
6038 case REALPART_EXPR:
6039 case IMAGPART_EXPR:
6040 x = TREE_OPERAND (x, 0);
6041 break;
6043 case PARM_DECL:
6044 if (x == current_class_ptr)
6046 error ("cannot take the address of %<this%>, which is an rvalue expression");
6047 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6048 return true;
6050 /* Fall through. */
6052 case VAR_DECL:
6053 /* Caller should not be trying to mark initialized
6054 constant fields addressable. */
6055 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6056 || DECL_IN_AGGR_P (x) == 0
6057 || TREE_STATIC (x)
6058 || DECL_EXTERNAL (x));
6059 /* Fall through. */
6061 case RESULT_DECL:
6062 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6063 && !DECL_ARTIFICIAL (x))
6065 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6067 error
6068 ("address of explicit register variable %qD requested", x);
6069 return false;
6071 else if (extra_warnings)
6072 warning
6073 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6075 TREE_ADDRESSABLE (x) = 1;
6076 return true;
6078 case CONST_DECL:
6079 case FUNCTION_DECL:
6080 TREE_ADDRESSABLE (x) = 1;
6081 return true;
6083 case CONSTRUCTOR:
6084 TREE_ADDRESSABLE (x) = 1;
6085 return true;
6087 case TARGET_EXPR:
6088 TREE_ADDRESSABLE (x) = 1;
6089 cxx_mark_addressable (TREE_OPERAND (x, 0));
6090 return true;
6092 default:
6093 return true;
6097 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6099 tree
6100 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6101 tsubst_flags_t complain)
6103 tree orig_ifexp = ifexp;
6104 tree orig_op1 = op1;
6105 tree orig_op2 = op2;
6106 tree expr;
6108 if (processing_template_decl)
6110 /* The standard says that the expression is type-dependent if
6111 IFEXP is type-dependent, even though the eventual type of the
6112 expression doesn't dependent on IFEXP. */
6113 if (type_dependent_expression_p (ifexp)
6114 /* As a GNU extension, the middle operand may be omitted. */
6115 || (op1 && type_dependent_expression_p (op1))
6116 || type_dependent_expression_p (op2))
6117 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6118 ifexp = build_non_dependent_expr (ifexp);
6119 if (op1)
6120 op1 = build_non_dependent_expr (op1);
6121 op2 = build_non_dependent_expr (op2);
6124 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6125 if (processing_template_decl && expr != error_mark_node
6126 && TREE_CODE (expr) != VEC_COND_EXPR)
6128 tree min = build_min_non_dep (COND_EXPR, expr,
6129 orig_ifexp, orig_op1, orig_op2);
6130 /* In C++11, remember that the result is an lvalue or xvalue.
6131 In C++98, lvalue_kind can just assume lvalue in a template. */
6132 if (cxx_dialect >= cxx11
6133 && lvalue_or_rvalue_with_address_p (expr)
6134 && !lvalue_or_rvalue_with_address_p (min))
6135 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6136 !real_lvalue_p (expr));
6137 expr = convert_from_reference (min);
6139 return expr;
6142 /* Given a list of expressions, return a compound expression
6143 that performs them all and returns the value of the last of them. */
6145 tree
6146 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6147 tsubst_flags_t complain)
6149 tree expr = TREE_VALUE (list);
6151 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6152 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6154 if (complain & tf_error)
6155 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6156 "list-initializer for non-class type must not "
6157 "be parenthesized");
6158 else
6159 return error_mark_node;
6162 if (TREE_CHAIN (list))
6164 if (complain & tf_error)
6165 switch (exp)
6167 case ELK_INIT:
6168 permerror (input_location, "expression list treated as compound "
6169 "expression in initializer");
6170 break;
6171 case ELK_MEM_INIT:
6172 permerror (input_location, "expression list treated as compound "
6173 "expression in mem-initializer");
6174 break;
6175 case ELK_FUNC_CAST:
6176 permerror (input_location, "expression list treated as compound "
6177 "expression in functional cast");
6178 break;
6179 default:
6180 gcc_unreachable ();
6182 else
6183 return error_mark_node;
6185 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6186 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6187 expr, TREE_VALUE (list), complain);
6190 return expr;
6193 /* Like build_x_compound_expr_from_list, but using a VEC. */
6195 tree
6196 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6197 tsubst_flags_t complain)
6199 if (vec_safe_is_empty (vec))
6200 return NULL_TREE;
6201 else if (vec->length () == 1)
6202 return (*vec)[0];
6203 else
6205 tree expr;
6206 unsigned int ix;
6207 tree t;
6209 if (msg != NULL)
6211 if (complain & tf_error)
6212 permerror (input_location,
6213 "%s expression list treated as compound expression",
6214 msg);
6215 else
6216 return error_mark_node;
6219 expr = (*vec)[0];
6220 for (ix = 1; vec->iterate (ix, &t); ++ix)
6221 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6222 t, complain);
6224 return expr;
6228 /* Handle overloading of the ',' operator when needed. */
6230 tree
6231 build_x_compound_expr (location_t loc, tree op1, tree op2,
6232 tsubst_flags_t complain)
6234 tree result;
6235 tree orig_op1 = op1;
6236 tree orig_op2 = op2;
6238 if (processing_template_decl)
6240 if (type_dependent_expression_p (op1)
6241 || type_dependent_expression_p (op2))
6242 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6243 op1 = build_non_dependent_expr (op1);
6244 op2 = build_non_dependent_expr (op2);
6247 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6248 NULL_TREE, /*overload=*/NULL, complain);
6249 if (!result)
6250 result = cp_build_compound_expr (op1, op2, complain);
6252 if (processing_template_decl && result != error_mark_node)
6253 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6255 return result;
6258 /* Like cp_build_compound_expr, but for the c-common bits. */
6260 tree
6261 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6263 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6266 /* Build a compound expression. */
6268 tree
6269 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6271 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6273 if (lhs == error_mark_node || rhs == error_mark_node)
6274 return error_mark_node;
6276 if (flag_cilkplus
6277 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6278 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6280 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6281 : EXPR_LOCATION (rhs));
6282 error_at (loc,
6283 "spawned function call cannot be part of a comma expression");
6284 return error_mark_node;
6287 if (TREE_CODE (rhs) == TARGET_EXPR)
6289 /* If the rhs is a TARGET_EXPR, then build the compound
6290 expression inside the target_expr's initializer. This
6291 helps the compiler to eliminate unnecessary temporaries. */
6292 tree init = TREE_OPERAND (rhs, 1);
6294 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6295 TREE_OPERAND (rhs, 1) = init;
6297 return rhs;
6300 if (type_unknown_p (rhs))
6302 if (complain & tf_error)
6303 error ("no context to resolve type of %qE", rhs);
6304 return error_mark_node;
6307 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6310 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6311 casts away constness. CAST gives the type of cast. Returns true
6312 if the cast is ill-formed, false if it is well-formed.
6314 ??? This function warns for casting away any qualifier not just
6315 const. We would like to specify exactly what qualifiers are casted
6316 away.
6319 static bool
6320 check_for_casting_away_constness (tree src_type, tree dest_type,
6321 enum tree_code cast, tsubst_flags_t complain)
6323 /* C-style casts are allowed to cast away constness. With
6324 WARN_CAST_QUAL, we still want to issue a warning. */
6325 if (cast == CAST_EXPR && !warn_cast_qual)
6326 return false;
6328 if (!casts_away_constness (src_type, dest_type, complain))
6329 return false;
6331 switch (cast)
6333 case CAST_EXPR:
6334 if (complain & tf_warning)
6335 warning (OPT_Wcast_qual,
6336 "cast from type %qT to type %qT casts away qualifiers",
6337 src_type, dest_type);
6338 return false;
6340 case STATIC_CAST_EXPR:
6341 if (complain & tf_error)
6342 error ("static_cast from type %qT to type %qT casts away qualifiers",
6343 src_type, dest_type);
6344 return true;
6346 case REINTERPRET_CAST_EXPR:
6347 if (complain & tf_error)
6348 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6349 src_type, dest_type);
6350 return true;
6352 default:
6353 gcc_unreachable();
6358 Warns if the cast from expression EXPR to type TYPE is useless.
6360 void
6361 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6363 if (warn_useless_cast
6364 && complain & tf_warning)
6366 /* In C++14 mode, this interacts badly with force_paren_expr. And it
6367 isn't necessary in any mode, because the code below handles
6368 glvalues properly. For 4.9, just skip it in C++14 mode. */
6369 if (cxx_dialect < cxx14 && REFERENCE_REF_P (expr))
6370 expr = TREE_OPERAND (expr, 0);
6372 if ((TREE_CODE (type) == REFERENCE_TYPE
6373 && (TYPE_REF_IS_RVALUE (type)
6374 ? xvalue_p (expr) : real_lvalue_p (expr))
6375 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6376 || same_type_p (TREE_TYPE (expr), type))
6377 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6381 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6382 (another pointer-to-member type in the same hierarchy) and return
6383 the converted expression. If ALLOW_INVERSE_P is permitted, a
6384 pointer-to-derived may be converted to pointer-to-base; otherwise,
6385 only the other direction is permitted. If C_CAST_P is true, this
6386 conversion is taking place as part of a C-style cast. */
6388 tree
6389 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6390 bool c_cast_p, tsubst_flags_t complain)
6392 if (TYPE_PTRDATAMEM_P (type))
6394 tree delta;
6396 if (TREE_CODE (expr) == PTRMEM_CST)
6397 expr = cplus_expand_constant (expr);
6398 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6399 TYPE_PTRMEM_CLASS_TYPE (type),
6400 allow_inverse_p,
6401 c_cast_p, complain);
6402 if (delta == error_mark_node)
6403 return error_mark_node;
6405 if (!integer_zerop (delta))
6407 tree cond, op1, op2;
6409 cond = cp_build_binary_op (input_location,
6410 EQ_EXPR,
6411 expr,
6412 build_int_cst (TREE_TYPE (expr), -1),
6413 complain);
6414 op1 = build_nop (ptrdiff_type_node, expr);
6415 op2 = cp_build_binary_op (input_location,
6416 PLUS_EXPR, op1, delta,
6417 complain);
6419 expr = fold_build3_loc (input_location,
6420 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6424 return build_nop (type, expr);
6426 else
6427 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6428 allow_inverse_p, c_cast_p, complain);
6431 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6432 this static_cast is being attempted as one of the possible casts
6433 allowed by a C-style cast. (In that case, accessibility of base
6434 classes is not considered, and it is OK to cast away
6435 constness.) Return the result of the cast. *VALID_P is set to
6436 indicate whether or not the cast was valid. */
6438 static tree
6439 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6440 bool *valid_p, tsubst_flags_t complain)
6442 tree intype;
6443 tree result;
6444 cp_lvalue_kind clk;
6446 /* Assume the cast is valid. */
6447 *valid_p = true;
6449 intype = unlowered_expr_type (expr);
6451 /* Save casted types in the function's used types hash table. */
6452 used_types_insert (type);
6454 /* [expr.static.cast]
6456 An lvalue of type "cv1 B", where B is a class type, can be cast
6457 to type "reference to cv2 D", where D is a class derived (clause
6458 _class.derived_) from B, if a valid standard conversion from
6459 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6460 same cv-qualification as, or greater cv-qualification than, cv1,
6461 and B is not a virtual base class of D. */
6462 /* We check this case before checking the validity of "TYPE t =
6463 EXPR;" below because for this case:
6465 struct B {};
6466 struct D : public B { D(const B&); };
6467 extern B& b;
6468 void f() { static_cast<const D&>(b); }
6470 we want to avoid constructing a new D. The standard is not
6471 completely clear about this issue, but our interpretation is
6472 consistent with other compilers. */
6473 if (TREE_CODE (type) == REFERENCE_TYPE
6474 && CLASS_TYPE_P (TREE_TYPE (type))
6475 && CLASS_TYPE_P (intype)
6476 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6477 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6478 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6479 build_pointer_type (TYPE_MAIN_VARIANT
6480 (TREE_TYPE (type))),
6481 complain)
6482 && (c_cast_p
6483 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6485 tree base;
6487 /* There is a standard conversion from "D*" to "B*" even if "B"
6488 is ambiguous or inaccessible. If this is really a
6489 static_cast, then we check both for inaccessibility and
6490 ambiguity. However, if this is a static_cast being performed
6491 because the user wrote a C-style cast, then accessibility is
6492 not considered. */
6493 base = lookup_base (TREE_TYPE (type), intype,
6494 c_cast_p ? ba_unique : ba_check,
6495 NULL, complain);
6497 /* Convert from "B*" to "D*". This function will check that "B"
6498 is not a virtual base of "D". */
6499 expr = build_base_path (MINUS_EXPR, build_address (expr),
6500 base, /*nonnull=*/false, complain);
6501 /* Convert the pointer to a reference -- but then remember that
6502 there are no expressions with reference type in C++.
6504 We call rvalue so that there's an actual tree code
6505 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6506 is a variable with the same type, the conversion would get folded
6507 away, leaving just the variable and causing lvalue_kind to give
6508 the wrong answer. */
6509 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6512 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6513 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6514 if (TREE_CODE (type) == REFERENCE_TYPE
6515 && TYPE_REF_IS_RVALUE (type)
6516 && (clk = real_lvalue_p (expr))
6517 && reference_related_p (TREE_TYPE (type), intype)
6518 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6520 if (clk == clk_ordinary)
6522 /* Handle the (non-bit-field) lvalue case here by casting to
6523 lvalue reference and then changing it to an rvalue reference.
6524 Casting an xvalue to rvalue reference will be handled by the
6525 main code path. */
6526 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6527 result = (perform_direct_initialization_if_possible
6528 (lref, expr, c_cast_p, complain));
6529 result = cp_fold_convert (type, result);
6530 /* Make sure we don't fold back down to a named rvalue reference,
6531 because that would be an lvalue. */
6532 if (DECL_P (result))
6533 result = build1 (NON_LVALUE_EXPR, type, result);
6534 return convert_from_reference (result);
6536 else
6537 /* For a bit-field or packed field, bind to a temporary. */
6538 expr = rvalue (expr);
6541 /* Resolve overloaded address here rather than once in
6542 implicit_conversion and again in the inverse code below. */
6543 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6545 expr = instantiate_type (type, expr, complain);
6546 intype = TREE_TYPE (expr);
6549 /* [expr.static.cast]
6551 Any expression can be explicitly converted to type cv void. */
6552 if (VOID_TYPE_P (type))
6553 return convert_to_void (expr, ICV_CAST, complain);
6555 /* [class.abstract]
6556 An abstract class shall not be used ... as the type of an explicit
6557 conversion. */
6558 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6559 return error_mark_node;
6561 /* [expr.static.cast]
6563 An expression e can be explicitly converted to a type T using a
6564 static_cast of the form static_cast<T>(e) if the declaration T
6565 t(e);" is well-formed, for some invented temporary variable
6566 t. */
6567 result = perform_direct_initialization_if_possible (type, expr,
6568 c_cast_p, complain);
6569 if (result)
6571 result = convert_from_reference (result);
6573 /* [expr.static.cast]
6575 If T is a reference type, the result is an lvalue; otherwise,
6576 the result is an rvalue. */
6577 if (TREE_CODE (type) != REFERENCE_TYPE)
6578 result = rvalue (result);
6579 return result;
6582 /* [expr.static.cast]
6584 The inverse of any standard conversion sequence (clause _conv_),
6585 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6586 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6587 (_conv.bool_) conversions, can be performed explicitly using
6588 static_cast subject to the restriction that the explicit
6589 conversion does not cast away constness (_expr.const.cast_), and
6590 the following additional rules for specific cases: */
6591 /* For reference, the conversions not excluded are: integral
6592 promotions, floating point promotion, integral conversions,
6593 floating point conversions, floating-integral conversions,
6594 pointer conversions, and pointer to member conversions. */
6595 /* DR 128
6597 A value of integral _or enumeration_ type can be explicitly
6598 converted to an enumeration type. */
6599 /* The effect of all that is that any conversion between any two
6600 types which are integral, floating, or enumeration types can be
6601 performed. */
6602 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6603 || SCALAR_FLOAT_TYPE_P (type))
6604 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6605 || SCALAR_FLOAT_TYPE_P (intype)))
6606 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6608 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6609 && CLASS_TYPE_P (TREE_TYPE (type))
6610 && CLASS_TYPE_P (TREE_TYPE (intype))
6611 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6612 (TREE_TYPE (intype))),
6613 build_pointer_type (TYPE_MAIN_VARIANT
6614 (TREE_TYPE (type))),
6615 complain))
6617 tree base;
6619 if (!c_cast_p
6620 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6621 complain))
6622 return error_mark_node;
6623 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6624 c_cast_p ? ba_unique : ba_check,
6625 NULL, complain);
6626 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6627 complain);
6628 return cp_fold_convert(type, expr);
6631 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6632 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6634 tree c1;
6635 tree c2;
6636 tree t1;
6637 tree t2;
6639 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6640 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6642 if (TYPE_PTRDATAMEM_P (type))
6644 t1 = (build_ptrmem_type
6645 (c1,
6646 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6647 t2 = (build_ptrmem_type
6648 (c2,
6649 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6651 else
6653 t1 = intype;
6654 t2 = type;
6656 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6658 if (!c_cast_p
6659 && check_for_casting_away_constness (intype, type,
6660 STATIC_CAST_EXPR,
6661 complain))
6662 return error_mark_node;
6663 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6664 c_cast_p, complain);
6668 /* [expr.static.cast]
6670 An rvalue of type "pointer to cv void" can be explicitly
6671 converted to a pointer to object type. A value of type pointer
6672 to object converted to "pointer to cv void" and back to the
6673 original pointer type will have its original value. */
6674 if (TYPE_PTR_P (intype)
6675 && VOID_TYPE_P (TREE_TYPE (intype))
6676 && TYPE_PTROB_P (type))
6678 if (!c_cast_p
6679 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6680 complain))
6681 return error_mark_node;
6682 return build_nop (type, expr);
6685 *valid_p = false;
6686 return error_mark_node;
6689 /* Return an expression representing static_cast<TYPE>(EXPR). */
6691 tree
6692 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6694 tree result;
6695 bool valid_p;
6697 if (type == error_mark_node || expr == error_mark_node)
6698 return error_mark_node;
6700 if (processing_template_decl)
6702 expr = build_min (STATIC_CAST_EXPR, type, expr);
6703 /* We don't know if it will or will not have side effects. */
6704 TREE_SIDE_EFFECTS (expr) = 1;
6705 return convert_from_reference (expr);
6708 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6709 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6710 if (TREE_CODE (type) != REFERENCE_TYPE
6711 && TREE_CODE (expr) == NOP_EXPR
6712 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6713 expr = TREE_OPERAND (expr, 0);
6715 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6716 complain);
6717 if (valid_p)
6719 if (result != error_mark_node)
6720 maybe_warn_about_useless_cast (type, expr, complain);
6721 return result;
6724 if (complain & tf_error)
6725 error ("invalid static_cast from type %qT to type %qT",
6726 TREE_TYPE (expr), type);
6727 return error_mark_node;
6730 /* EXPR is an expression with member function or pointer-to-member
6731 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6732 not permitted by ISO C++, but we accept it in some modes. If we
6733 are not in one of those modes, issue a diagnostic. Return the
6734 converted expression. */
6736 tree
6737 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6739 tree intype;
6740 tree decl;
6742 intype = TREE_TYPE (expr);
6743 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6744 || TREE_CODE (intype) == METHOD_TYPE);
6746 if (!(complain & tf_warning_or_error))
6747 return error_mark_node;
6749 if (pedantic || warn_pmf2ptr)
6750 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6751 "converting from %qT to %qT", intype, type);
6753 if (TREE_CODE (intype) == METHOD_TYPE)
6754 expr = build_addr_func (expr, complain);
6755 else if (TREE_CODE (expr) == PTRMEM_CST)
6756 expr = build_address (PTRMEM_CST_MEMBER (expr));
6757 else
6759 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6760 decl = build_address (decl);
6761 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6764 if (expr == error_mark_node)
6765 return error_mark_node;
6767 return build_nop (type, expr);
6770 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6771 If C_CAST_P is true, this reinterpret cast is being done as part of
6772 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6773 indicate whether or not reinterpret_cast was valid. */
6775 static tree
6776 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6777 bool *valid_p, tsubst_flags_t complain)
6779 tree intype;
6781 /* Assume the cast is invalid. */
6782 if (valid_p)
6783 *valid_p = true;
6785 if (type == error_mark_node || error_operand_p (expr))
6786 return error_mark_node;
6788 intype = TREE_TYPE (expr);
6790 /* Save casted types in the function's used types hash table. */
6791 used_types_insert (type);
6793 /* [expr.reinterpret.cast]
6794 An lvalue expression of type T1 can be cast to the type
6795 "reference to T2" if an expression of type "pointer to T1" can be
6796 explicitly converted to the type "pointer to T2" using a
6797 reinterpret_cast. */
6798 if (TREE_CODE (type) == REFERENCE_TYPE)
6800 if (! real_lvalue_p (expr))
6802 if (complain & tf_error)
6803 error ("invalid cast of an rvalue expression of type "
6804 "%qT to type %qT",
6805 intype, type);
6806 return error_mark_node;
6809 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6810 "B" are related class types; the reinterpret_cast does not
6811 adjust the pointer. */
6812 if (TYPE_PTR_P (intype)
6813 && (complain & tf_warning)
6814 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6815 COMPARE_BASE | COMPARE_DERIVED)))
6816 warning (0, "casting %qT to %qT does not dereference pointer",
6817 intype, type);
6819 expr = cp_build_addr_expr (expr, complain);
6821 if (warn_strict_aliasing > 2)
6822 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6824 if (expr != error_mark_node)
6825 expr = build_reinterpret_cast_1
6826 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6827 valid_p, complain);
6828 if (expr != error_mark_node)
6829 /* cp_build_indirect_ref isn't right for rvalue refs. */
6830 expr = convert_from_reference (fold_convert (type, expr));
6831 return expr;
6834 /* As a G++ extension, we consider conversions from member
6835 functions, and pointers to member functions to
6836 pointer-to-function and pointer-to-void types. If
6837 -Wno-pmf-conversions has not been specified,
6838 convert_member_func_to_ptr will issue an error message. */
6839 if ((TYPE_PTRMEMFUNC_P (intype)
6840 || TREE_CODE (intype) == METHOD_TYPE)
6841 && TYPE_PTR_P (type)
6842 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6843 || VOID_TYPE_P (TREE_TYPE (type))))
6844 return convert_member_func_to_ptr (type, expr, complain);
6846 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6847 array-to-pointer, and function-to-pointer conversions are
6848 performed. */
6849 expr = decay_conversion (expr, complain);
6851 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6852 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6853 if (TREE_CODE (expr) == NOP_EXPR
6854 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6855 expr = TREE_OPERAND (expr, 0);
6857 if (error_operand_p (expr))
6858 return error_mark_node;
6860 intype = TREE_TYPE (expr);
6862 /* [expr.reinterpret.cast]
6863 A pointer can be converted to any integral type large enough to
6864 hold it. ... A value of type std::nullptr_t can be converted to
6865 an integral type; the conversion has the same meaning and
6866 validity as a conversion of (void*)0 to the integral type. */
6867 if (CP_INTEGRAL_TYPE_P (type)
6868 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6870 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6872 if (complain & tf_error)
6873 permerror (input_location, "cast from %qT to %qT loses precision",
6874 intype, type);
6875 else
6876 return error_mark_node;
6878 if (NULLPTR_TYPE_P (intype))
6879 return build_int_cst (type, 0);
6881 /* [expr.reinterpret.cast]
6882 A value of integral or enumeration type can be explicitly
6883 converted to a pointer. */
6884 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6885 /* OK */
6887 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6888 || TYPE_PTR_OR_PTRMEM_P (type))
6889 && same_type_p (type, intype))
6890 /* DR 799 */
6891 return fold_if_not_in_template (build_nop (type, expr));
6892 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6893 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6894 return fold_if_not_in_template (build_nop (type, expr));
6895 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6896 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6898 tree sexpr = expr;
6900 if (!c_cast_p
6901 && check_for_casting_away_constness (intype, type,
6902 REINTERPRET_CAST_EXPR,
6903 complain))
6904 return error_mark_node;
6905 /* Warn about possible alignment problems. */
6906 if (STRICT_ALIGNMENT && warn_cast_align
6907 && (complain & tf_warning)
6908 && !VOID_TYPE_P (type)
6909 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6910 && COMPLETE_TYPE_P (TREE_TYPE (type))
6911 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6912 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6913 warning (OPT_Wcast_align, "cast from %qT to %qT "
6914 "increases required alignment of target type", intype, type);
6916 /* We need to strip nops here, because the front end likes to
6917 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6918 STRIP_NOPS (sexpr);
6919 if (warn_strict_aliasing <= 2)
6920 strict_aliasing_warning (intype, type, sexpr);
6922 return fold_if_not_in_template (build_nop (type, expr));
6924 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6925 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6927 if (complain & tf_warning)
6928 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6929 object pointer type or vice versa is conditionally-supported." */
6930 warning (OPT_Wconditionally_supported,
6931 "casting between pointer-to-function and pointer-to-object "
6932 "is conditionally-supported");
6933 return fold_if_not_in_template (build_nop (type, expr));
6935 else if (TREE_CODE (type) == VECTOR_TYPE)
6936 return fold_if_not_in_template (convert_to_vector (type, expr));
6937 else if (TREE_CODE (intype) == VECTOR_TYPE
6938 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6939 return fold_if_not_in_template (convert_to_integer (type, expr));
6940 else
6942 if (valid_p)
6943 *valid_p = false;
6944 if (complain & tf_error)
6945 error ("invalid cast from type %qT to type %qT", intype, type);
6946 return error_mark_node;
6949 return cp_convert (type, expr, complain);
6952 tree
6953 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6955 tree r;
6957 if (type == error_mark_node || expr == error_mark_node)
6958 return error_mark_node;
6960 if (processing_template_decl)
6962 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6964 if (!TREE_SIDE_EFFECTS (t)
6965 && type_dependent_expression_p (expr))
6966 /* There might turn out to be side effects inside expr. */
6967 TREE_SIDE_EFFECTS (t) = 1;
6968 return convert_from_reference (t);
6971 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6972 /*valid_p=*/NULL, complain);
6973 if (r != error_mark_node)
6974 maybe_warn_about_useless_cast (type, expr, complain);
6975 return r;
6978 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6979 return an appropriate expression. Otherwise, return
6980 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6981 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6982 performing a C-style cast, its value upon return will indicate
6983 whether or not the conversion succeeded. */
6985 static tree
6986 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6987 bool *valid_p)
6989 tree src_type;
6990 tree reference_type;
6992 /* Callers are responsible for handling error_mark_node as a
6993 destination type. */
6994 gcc_assert (dst_type != error_mark_node);
6995 /* In a template, callers should be building syntactic
6996 representations of casts, not using this machinery. */
6997 gcc_assert (!processing_template_decl);
6999 /* Assume the conversion is invalid. */
7000 if (valid_p)
7001 *valid_p = false;
7003 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7005 if (complain & tf_error)
7006 error ("invalid use of const_cast with type %qT, "
7007 "which is not a pointer, "
7008 "reference, nor a pointer-to-data-member type", dst_type);
7009 return error_mark_node;
7012 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7014 if (complain & tf_error)
7015 error ("invalid use of const_cast with type %qT, which is a pointer "
7016 "or reference to a function type", dst_type);
7017 return error_mark_node;
7020 /* Save casted types in the function's used types hash table. */
7021 used_types_insert (dst_type);
7023 src_type = TREE_TYPE (expr);
7024 /* Expressions do not really have reference types. */
7025 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7026 src_type = TREE_TYPE (src_type);
7028 /* [expr.const.cast]
7030 For two object types T1 and T2, if a pointer to T1 can be explicitly
7031 converted to the type "pointer to T2" using a const_cast, then the
7032 following conversions can also be made:
7034 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7035 type T2 using the cast const_cast<T2&>;
7037 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7038 type T2 using the cast const_cast<T2&&>; and
7040 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7041 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7043 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7045 reference_type = dst_type;
7046 if (!TYPE_REF_IS_RVALUE (dst_type)
7047 ? real_lvalue_p (expr)
7048 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7049 ? lvalue_p (expr)
7050 : lvalue_or_rvalue_with_address_p (expr)))
7051 /* OK. */;
7052 else
7054 if (complain & tf_error)
7055 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7056 src_type, dst_type);
7057 return error_mark_node;
7059 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7060 src_type = build_pointer_type (src_type);
7062 else
7064 reference_type = NULL_TREE;
7065 /* If the destination type is not a reference type, the
7066 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7067 conversions are performed. */
7068 src_type = type_decays_to (src_type);
7069 if (src_type == error_mark_node)
7070 return error_mark_node;
7073 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7075 if (comp_ptr_ttypes_const (dst_type, src_type))
7077 if (valid_p)
7079 *valid_p = true;
7080 /* This cast is actually a C-style cast. Issue a warning if
7081 the user is making a potentially unsafe cast. */
7082 check_for_casting_away_constness (src_type, dst_type,
7083 CAST_EXPR, complain);
7085 if (reference_type)
7087 expr = cp_build_addr_expr (expr, complain);
7088 if (expr == error_mark_node)
7089 return error_mark_node;
7090 expr = build_nop (reference_type, expr);
7091 return convert_from_reference (expr);
7093 else
7095 expr = decay_conversion (expr, complain);
7096 if (expr == error_mark_node)
7097 return error_mark_node;
7099 /* build_c_cast puts on a NOP_EXPR to make the result not an
7100 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7101 non-lvalue context. */
7102 if (TREE_CODE (expr) == NOP_EXPR
7103 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7104 expr = TREE_OPERAND (expr, 0);
7105 return build_nop (dst_type, expr);
7108 else if (valid_p
7109 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7110 TREE_TYPE (src_type)))
7111 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7112 complain);
7115 if (complain & tf_error)
7116 error ("invalid const_cast from type %qT to type %qT",
7117 src_type, dst_type);
7118 return error_mark_node;
7121 tree
7122 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7124 tree r;
7126 if (type == error_mark_node || error_operand_p (expr))
7127 return error_mark_node;
7129 if (processing_template_decl)
7131 tree t = build_min (CONST_CAST_EXPR, type, expr);
7133 if (!TREE_SIDE_EFFECTS (t)
7134 && type_dependent_expression_p (expr))
7135 /* There might turn out to be side effects inside expr. */
7136 TREE_SIDE_EFFECTS (t) = 1;
7137 return convert_from_reference (t);
7140 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7141 if (r != error_mark_node)
7142 maybe_warn_about_useless_cast (type, expr, complain);
7143 return r;
7146 /* Like cp_build_c_cast, but for the c-common bits. */
7148 tree
7149 build_c_cast (location_t /*loc*/, tree type, tree expr)
7151 return cp_build_c_cast (type, expr, tf_warning_or_error);
7154 /* Build an expression representing an explicit C-style cast to type
7155 TYPE of expression EXPR. */
7157 tree
7158 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7160 tree value = expr;
7161 tree result;
7162 bool valid_p;
7164 if (type == error_mark_node || error_operand_p (expr))
7165 return error_mark_node;
7167 if (processing_template_decl)
7169 tree t = build_min (CAST_EXPR, type,
7170 tree_cons (NULL_TREE, value, NULL_TREE));
7171 /* We don't know if it will or will not have side effects. */
7172 TREE_SIDE_EFFECTS (t) = 1;
7173 return convert_from_reference (t);
7176 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7177 'Class') should always be retained, because this information aids
7178 in method lookup. */
7179 if (objc_is_object_ptr (type)
7180 && objc_is_object_ptr (TREE_TYPE (expr)))
7181 return build_nop (type, expr);
7183 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7184 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7185 if (TREE_CODE (type) != REFERENCE_TYPE
7186 && TREE_CODE (value) == NOP_EXPR
7187 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7188 value = TREE_OPERAND (value, 0);
7190 if (TREE_CODE (type) == ARRAY_TYPE)
7192 /* Allow casting from T1* to T2[] because Cfront allows it.
7193 NIHCL uses it. It is not valid ISO C++ however. */
7194 if (TYPE_PTR_P (TREE_TYPE (expr)))
7196 if (complain & tf_error)
7197 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7198 else
7199 return error_mark_node;
7200 type = build_pointer_type (TREE_TYPE (type));
7202 else
7204 if (complain & tf_error)
7205 error ("ISO C++ forbids casting to an array type %qT", type);
7206 return error_mark_node;
7210 if (TREE_CODE (type) == FUNCTION_TYPE
7211 || TREE_CODE (type) == METHOD_TYPE)
7213 if (complain & tf_error)
7214 error ("invalid cast to function type %qT", type);
7215 return error_mark_node;
7218 if (TYPE_PTR_P (type)
7219 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7220 /* Casting to an integer of smaller size is an error detected elsewhere. */
7221 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7222 /* Don't warn about converting any constant. */
7223 && !TREE_CONSTANT (value))
7224 warning_at (input_location, OPT_Wint_to_pointer_cast,
7225 "cast to pointer from integer of different size");
7227 /* A C-style cast can be a const_cast. */
7228 result = build_const_cast_1 (type, value, complain & tf_warning,
7229 &valid_p);
7230 if (valid_p)
7232 if (result != error_mark_node)
7233 maybe_warn_about_useless_cast (type, value, complain);
7234 return result;
7237 /* Or a static cast. */
7238 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7239 &valid_p, complain);
7240 /* Or a reinterpret_cast. */
7241 if (!valid_p)
7242 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7243 &valid_p, complain);
7244 /* The static_cast or reinterpret_cast may be followed by a
7245 const_cast. */
7246 if (valid_p
7247 /* A valid cast may result in errors if, for example, a
7248 conversion to an ambiguous base class is required. */
7249 && !error_operand_p (result))
7251 tree result_type;
7253 maybe_warn_about_useless_cast (type, value, complain);
7255 /* Non-class rvalues always have cv-unqualified type. */
7256 if (!CLASS_TYPE_P (type))
7257 type = TYPE_MAIN_VARIANT (type);
7258 result_type = TREE_TYPE (result);
7259 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7260 result_type = TYPE_MAIN_VARIANT (result_type);
7261 /* If the type of RESULT does not match TYPE, perform a
7262 const_cast to make it match. If the static_cast or
7263 reinterpret_cast succeeded, we will differ by at most
7264 cv-qualification, so the follow-on const_cast is guaranteed
7265 to succeed. */
7266 if (!same_type_p (non_reference (type), non_reference (result_type)))
7268 result = build_const_cast_1 (type, result, false, &valid_p);
7269 gcc_assert (valid_p);
7271 return result;
7274 return error_mark_node;
7277 /* For use from the C common bits. */
7278 tree
7279 build_modify_expr (location_t /*location*/,
7280 tree lhs, tree /*lhs_origtype*/,
7281 enum tree_code modifycode,
7282 location_t /*rhs_location*/, tree rhs,
7283 tree /*rhs_origtype*/)
7285 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7288 /* Build an assignment expression of lvalue LHS from value RHS.
7289 MODIFYCODE is the code for a binary operator that we use
7290 to combine the old value of LHS with RHS to get the new value.
7291 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7293 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7295 tree
7296 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7297 tsubst_flags_t complain)
7299 tree result;
7300 tree newrhs = rhs;
7301 tree lhstype = TREE_TYPE (lhs);
7302 tree olhstype = lhstype;
7303 bool plain_assign = (modifycode == NOP_EXPR);
7305 /* Avoid duplicate error messages from operands that had errors. */
7306 if (error_operand_p (lhs) || error_operand_p (rhs))
7307 return error_mark_node;
7309 /* Handle control structure constructs used as "lvalues". */
7310 switch (TREE_CODE (lhs))
7312 /* Handle --foo = 5; as these are valid constructs in C++. */
7313 case PREDECREMENT_EXPR:
7314 case PREINCREMENT_EXPR:
7315 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7316 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7317 stabilize_reference (TREE_OPERAND (lhs, 0)),
7318 TREE_OPERAND (lhs, 1));
7319 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7320 modifycode, rhs, complain);
7321 if (newrhs == error_mark_node)
7322 return error_mark_node;
7323 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7325 /* Handle (a, b) used as an "lvalue". */
7326 case COMPOUND_EXPR:
7327 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7328 modifycode, rhs, complain);
7329 if (newrhs == error_mark_node)
7330 return error_mark_node;
7331 return build2 (COMPOUND_EXPR, lhstype,
7332 TREE_OPERAND (lhs, 0), newrhs);
7334 case MODIFY_EXPR:
7335 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7336 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7337 stabilize_reference (TREE_OPERAND (lhs, 0)),
7338 TREE_OPERAND (lhs, 1));
7339 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7340 complain);
7341 if (newrhs == error_mark_node)
7342 return error_mark_node;
7343 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7345 case MIN_EXPR:
7346 case MAX_EXPR:
7347 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7348 when neither operand has side-effects. */
7349 if (!lvalue_or_else (lhs, lv_assign, complain))
7350 return error_mark_node;
7352 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7353 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7355 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7356 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7357 boolean_type_node,
7358 TREE_OPERAND (lhs, 0),
7359 TREE_OPERAND (lhs, 1)),
7360 TREE_OPERAND (lhs, 0),
7361 TREE_OPERAND (lhs, 1));
7362 /* Fall through. */
7364 /* Handle (a ? b : c) used as an "lvalue". */
7365 case COND_EXPR:
7367 /* Produce (a ? (b = rhs) : (c = rhs))
7368 except that the RHS goes through a save-expr
7369 so the code to compute it is only emitted once. */
7370 tree cond;
7371 tree preeval = NULL_TREE;
7373 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7375 if (complain & tf_error)
7376 error ("void value not ignored as it ought to be");
7377 return error_mark_node;
7380 rhs = stabilize_expr (rhs, &preeval);
7382 /* Check this here to avoid odd errors when trying to convert
7383 a throw to the type of the COND_EXPR. */
7384 if (!lvalue_or_else (lhs, lv_assign, complain))
7385 return error_mark_node;
7387 cond = build_conditional_expr
7388 (input_location, TREE_OPERAND (lhs, 0),
7389 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7390 modifycode, rhs, complain),
7391 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7392 modifycode, rhs, complain),
7393 complain);
7395 if (cond == error_mark_node)
7396 return cond;
7397 /* Make sure the code to compute the rhs comes out
7398 before the split. */
7399 if (preeval)
7400 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7401 return cond;
7404 default:
7405 break;
7408 if (modifycode == INIT_EXPR)
7410 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7411 /* Do the default thing. */;
7412 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7414 /* Compound literal. */
7415 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7416 /* Call convert to generate an error; see PR 11063. */
7417 rhs = convert (lhstype, rhs);
7418 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7419 TREE_SIDE_EFFECTS (result) = 1;
7420 return result;
7422 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7423 /* Do the default thing. */;
7424 else
7426 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7427 result = build_special_member_call (lhs, complete_ctor_identifier,
7428 &rhs_vec, lhstype, LOOKUP_NORMAL,
7429 complain);
7430 release_tree_vector (rhs_vec);
7431 if (result == NULL_TREE)
7432 return error_mark_node;
7433 return result;
7436 else
7438 lhs = require_complete_type_sfinae (lhs, complain);
7439 if (lhs == error_mark_node)
7440 return error_mark_node;
7442 if (modifycode == NOP_EXPR)
7444 if (c_dialect_objc ())
7446 result = objc_maybe_build_modify_expr (lhs, rhs);
7447 if (result)
7448 return result;
7451 /* `operator=' is not an inheritable operator. */
7452 if (! MAYBE_CLASS_TYPE_P (lhstype))
7453 /* Do the default thing. */;
7454 else
7456 result = build_new_op (input_location, MODIFY_EXPR,
7457 LOOKUP_NORMAL, lhs, rhs,
7458 make_node (NOP_EXPR), /*overload=*/NULL,
7459 complain);
7460 if (result == NULL_TREE)
7461 return error_mark_node;
7462 return result;
7464 lhstype = olhstype;
7466 else
7468 tree init = NULL_TREE;
7470 /* A binary op has been requested. Combine the old LHS
7471 value with the RHS producing the value we should actually
7472 store into the LHS. */
7473 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7474 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7475 || MAYBE_CLASS_TYPE_P (lhstype)));
7477 /* Preevaluate the RHS to make sure its evaluation is complete
7478 before the lvalue-to-rvalue conversion of the LHS:
7480 [expr.ass] With respect to an indeterminately-sequenced
7481 function call, the operation of a compound assignment is a
7482 single evaluation. [ Note: Therefore, a function call shall
7483 not intervene between the lvalue-to-rvalue conversion and the
7484 side effect associated with any single compound assignment
7485 operator. -- end note ] */
7486 lhs = stabilize_reference (lhs);
7487 rhs = rvalue (rhs);
7488 rhs = stabilize_expr (rhs, &init);
7489 newrhs = cp_build_binary_op (input_location,
7490 modifycode, lhs, rhs,
7491 complain);
7492 if (newrhs == error_mark_node)
7494 if (complain & tf_error)
7495 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7496 TREE_TYPE (lhs), TREE_TYPE (rhs));
7497 return error_mark_node;
7500 if (init)
7501 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7503 /* Now it looks like a plain assignment. */
7504 modifycode = NOP_EXPR;
7505 if (c_dialect_objc ())
7507 result = objc_maybe_build_modify_expr (lhs, newrhs);
7508 if (result)
7509 return result;
7512 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7513 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7516 /* The left-hand side must be an lvalue. */
7517 if (!lvalue_or_else (lhs, lv_assign, complain))
7518 return error_mark_node;
7520 /* Warn about modifying something that is `const'. Don't warn if
7521 this is initialization. */
7522 if (modifycode != INIT_EXPR
7523 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7524 /* Functions are not modifiable, even though they are
7525 lvalues. */
7526 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7527 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7528 /* If it's an aggregate and any field is const, then it is
7529 effectively const. */
7530 || (CLASS_TYPE_P (lhstype)
7531 && C_TYPE_FIELDS_READONLY (lhstype))))
7533 if (complain & tf_error)
7534 cxx_readonly_error (lhs, lv_assign);
7535 else
7536 return error_mark_node;
7539 /* If storing into a structure or union member, it may have been given a
7540 lowered bitfield type. We need to convert to the declared type first,
7541 so retrieve it now. */
7543 olhstype = unlowered_expr_type (lhs);
7545 /* Convert new value to destination type. */
7547 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7549 int from_array;
7551 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7553 if (modifycode != INIT_EXPR)
7555 if (complain & tf_error)
7556 error ("assigning to an array from an initializer list");
7557 return error_mark_node;
7559 if (check_array_initializer (lhs, lhstype, newrhs))
7560 return error_mark_node;
7561 newrhs = digest_init (lhstype, newrhs, complain);
7562 if (newrhs == error_mark_node)
7563 return error_mark_node;
7566 /* C++11 8.5/17: "If the destination type is an array of characters,
7567 an array of char16_t, an array of char32_t, or an array of wchar_t,
7568 and the initializer is a string literal...". */
7569 else if (TREE_CODE (newrhs) == STRING_CST
7570 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7571 && modifycode == INIT_EXPR)
7573 newrhs = digest_init (lhstype, newrhs, complain);
7574 if (newrhs == error_mark_node)
7575 return error_mark_node;
7578 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7579 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7581 if (complain & tf_error)
7582 error ("incompatible types in assignment of %qT to %qT",
7583 TREE_TYPE (rhs), lhstype);
7584 return error_mark_node;
7587 /* Allow array assignment in compiler-generated code. */
7588 else if (!current_function_decl
7589 || !DECL_DEFAULTED_FN (current_function_decl))
7591 /* This routine is used for both initialization and assignment.
7592 Make sure the diagnostic message differentiates the context. */
7593 if (complain & tf_error)
7595 if (modifycode == INIT_EXPR)
7596 error ("array used as initializer");
7597 else
7598 error ("invalid array assignment");
7600 return error_mark_node;
7603 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7604 ? 1 + (modifycode != INIT_EXPR): 0;
7605 return build_vec_init (lhs, NULL_TREE, newrhs,
7606 /*explicit_value_init_p=*/false,
7607 from_array, complain);
7610 if (modifycode == INIT_EXPR)
7611 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7612 LOOKUP_ONLYCONVERTING. */
7613 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7614 ICR_INIT, NULL_TREE, 0,
7615 complain);
7616 else
7617 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7618 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7620 if (!same_type_p (lhstype, olhstype))
7621 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7623 if (modifycode != INIT_EXPR)
7625 if (TREE_CODE (newrhs) == CALL_EXPR
7626 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7627 newrhs = build_cplus_new (lhstype, newrhs, complain);
7629 /* Can't initialize directly from a TARGET_EXPR, since that would
7630 cause the lhs to be constructed twice, and possibly result in
7631 accidental self-initialization. So we force the TARGET_EXPR to be
7632 expanded without a target. */
7633 if (TREE_CODE (newrhs) == TARGET_EXPR)
7634 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7635 TREE_OPERAND (newrhs, 0));
7638 if (newrhs == error_mark_node)
7639 return error_mark_node;
7641 if (c_dialect_objc () && flag_objc_gc)
7643 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7645 if (result)
7646 return result;
7649 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7650 lhstype, lhs, newrhs);
7652 TREE_SIDE_EFFECTS (result) = 1;
7653 if (!plain_assign)
7654 TREE_NO_WARNING (result) = 1;
7656 return result;
7659 tree
7660 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7661 tree rhs, tsubst_flags_t complain)
7663 if (processing_template_decl)
7664 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7665 build_min_nt_loc (loc, modifycode, NULL_TREE,
7666 NULL_TREE), rhs);
7668 if (modifycode != NOP_EXPR)
7670 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7671 make_node (modifycode), /*overload=*/NULL,
7672 complain);
7673 if (rval)
7675 TREE_NO_WARNING (rval) = 1;
7676 return rval;
7679 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7682 /* Helper function for get_delta_difference which assumes FROM is a base
7683 class of TO. Returns a delta for the conversion of pointer-to-member
7684 of FROM to pointer-to-member of TO. If the conversion is invalid and
7685 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7686 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7687 If C_CAST_P is true, this conversion is taking place as part of a
7688 C-style cast. */
7690 static tree
7691 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7692 tsubst_flags_t complain)
7694 tree binfo;
7695 base_kind kind;
7697 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7698 &kind, complain);
7700 if (binfo == error_mark_node)
7702 if (!(complain & tf_error))
7703 return error_mark_node;
7705 error (" in pointer to member function conversion");
7706 return size_zero_node;
7708 else if (binfo)
7710 if (kind != bk_via_virtual)
7711 return BINFO_OFFSET (binfo);
7712 else
7713 /* FROM is a virtual base class of TO. Issue an error or warning
7714 depending on whether or not this is a reinterpret cast. */
7716 if (!(complain & tf_error))
7717 return error_mark_node;
7719 error ("pointer to member conversion via virtual base %qT",
7720 BINFO_TYPE (binfo_from_vbase (binfo)));
7722 return size_zero_node;
7725 else
7726 return NULL_TREE;
7729 /* Get difference in deltas for different pointer to member function
7730 types. If the conversion is invalid and tf_error is not set in
7731 COMPLAIN, returns error_mark_node, otherwise returns an integer
7732 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7733 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7734 conversions as well. If C_CAST_P is true this conversion is taking
7735 place as part of a C-style cast.
7737 Note that the naming of FROM and TO is kind of backwards; the return
7738 value is what we add to a TO in order to get a FROM. They are named
7739 this way because we call this function to find out how to convert from
7740 a pointer to member of FROM to a pointer to member of TO. */
7742 static tree
7743 get_delta_difference (tree from, tree to,
7744 bool allow_inverse_p,
7745 bool c_cast_p, tsubst_flags_t complain)
7747 tree result;
7749 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7750 /* Pointer to member of incomplete class is permitted*/
7751 result = size_zero_node;
7752 else
7753 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7755 if (result == error_mark_node)
7756 return error_mark_node;
7758 if (!result)
7760 if (!allow_inverse_p)
7762 if (!(complain & tf_error))
7763 return error_mark_node;
7765 error_not_base_type (from, to);
7766 error (" in pointer to member conversion");
7767 result = size_zero_node;
7769 else
7771 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7773 if (result == error_mark_node)
7774 return error_mark_node;
7776 if (result)
7777 result = size_diffop_loc (input_location,
7778 size_zero_node, result);
7779 else
7781 if (!(complain & tf_error))
7782 return error_mark_node;
7784 error_not_base_type (from, to);
7785 error (" in pointer to member conversion");
7786 result = size_zero_node;
7791 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7792 result));
7795 /* Return a constructor for the pointer-to-member-function TYPE using
7796 the other components as specified. */
7798 tree
7799 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7801 tree u = NULL_TREE;
7802 tree delta_field;
7803 tree pfn_field;
7804 vec<constructor_elt, va_gc> *v;
7806 /* Pull the FIELD_DECLs out of the type. */
7807 pfn_field = TYPE_FIELDS (type);
7808 delta_field = DECL_CHAIN (pfn_field);
7810 /* Make sure DELTA has the type we want. */
7811 delta = convert_and_check (input_location, delta_type_node, delta);
7813 /* Convert to the correct target type if necessary. */
7814 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7816 /* Finish creating the initializer. */
7817 vec_alloc (v, 2);
7818 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7819 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7820 u = build_constructor (type, v);
7821 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7822 TREE_STATIC (u) = (TREE_CONSTANT (u)
7823 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7824 != NULL_TREE)
7825 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7826 != NULL_TREE));
7827 return u;
7830 /* Build a constructor for a pointer to member function. It can be
7831 used to initialize global variables, local variable, or used
7832 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7833 want to be.
7835 If FORCE is nonzero, then force this conversion, even if
7836 we would rather not do it. Usually set when using an explicit
7837 cast. A C-style cast is being processed iff C_CAST_P is true.
7839 Return error_mark_node, if something goes wrong. */
7841 tree
7842 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7843 tsubst_flags_t complain)
7845 tree fn;
7846 tree pfn_type;
7847 tree to_type;
7849 if (error_operand_p (pfn))
7850 return error_mark_node;
7852 pfn_type = TREE_TYPE (pfn);
7853 to_type = build_ptrmemfunc_type (type);
7855 /* Handle multiple conversions of pointer to member functions. */
7856 if (TYPE_PTRMEMFUNC_P (pfn_type))
7858 tree delta = NULL_TREE;
7859 tree npfn = NULL_TREE;
7860 tree n;
7862 if (!force
7863 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7864 LOOKUP_NORMAL, complain))
7866 if (complain & tf_error)
7867 error ("invalid conversion to type %qT from type %qT",
7868 to_type, pfn_type);
7869 else
7870 return error_mark_node;
7873 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7874 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7875 force,
7876 c_cast_p, complain);
7877 if (n == error_mark_node)
7878 return error_mark_node;
7880 /* We don't have to do any conversion to convert a
7881 pointer-to-member to its own type. But, we don't want to
7882 just return a PTRMEM_CST if there's an explicit cast; that
7883 cast should make the expression an invalid template argument. */
7884 if (TREE_CODE (pfn) != PTRMEM_CST)
7886 if (same_type_p (to_type, pfn_type))
7887 return pfn;
7888 else if (integer_zerop (n))
7889 return build_reinterpret_cast (to_type, pfn,
7890 complain);
7893 if (TREE_SIDE_EFFECTS (pfn))
7894 pfn = save_expr (pfn);
7896 /* Obtain the function pointer and the current DELTA. */
7897 if (TREE_CODE (pfn) == PTRMEM_CST)
7898 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7899 else
7901 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7902 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7905 /* Just adjust the DELTA field. */
7906 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7907 (TREE_TYPE (delta), ptrdiff_type_node));
7908 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7909 n = cp_build_binary_op (input_location,
7910 LSHIFT_EXPR, n, integer_one_node,
7911 complain);
7912 delta = cp_build_binary_op (input_location,
7913 PLUS_EXPR, delta, n, complain);
7914 return build_ptrmemfunc1 (to_type, delta, npfn);
7917 /* Handle null pointer to member function conversions. */
7918 if (null_ptr_cst_p (pfn))
7920 pfn = cp_build_c_cast (type, pfn, complain);
7921 return build_ptrmemfunc1 (to_type,
7922 integer_zero_node,
7923 pfn);
7926 if (type_unknown_p (pfn))
7927 return instantiate_type (type, pfn, complain);
7929 fn = TREE_OPERAND (pfn, 0);
7930 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7931 /* In a template, we will have preserved the
7932 OFFSET_REF. */
7933 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7934 return make_ptrmem_cst (to_type, fn);
7937 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7938 given by CST.
7940 ??? There is no consistency as to the types returned for the above
7941 values. Some code acts as if it were a sizetype and some as if it were
7942 integer_type_node. */
7944 void
7945 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7947 tree type = TREE_TYPE (cst);
7948 tree fn = PTRMEM_CST_MEMBER (cst);
7949 tree ptr_class, fn_class;
7951 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7953 /* The class that the function belongs to. */
7954 fn_class = DECL_CONTEXT (fn);
7956 /* The class that we're creating a pointer to member of. */
7957 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7959 /* First, calculate the adjustment to the function's class. */
7960 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7961 /*c_cast_p=*/0, tf_warning_or_error);
7963 if (!DECL_VIRTUAL_P (fn))
7964 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7965 build_addr_func (fn, tf_warning_or_error));
7966 else
7968 /* If we're dealing with a virtual function, we have to adjust 'this'
7969 again, to point to the base which provides the vtable entry for
7970 fn; the call will do the opposite adjustment. */
7971 tree orig_class = DECL_CONTEXT (fn);
7972 tree binfo = binfo_or_else (orig_class, fn_class);
7973 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7974 *delta, BINFO_OFFSET (binfo));
7975 *delta = fold_if_not_in_template (*delta);
7977 /* We set PFN to the vtable offset at which the function can be
7978 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7979 case delta is shifted left, and then incremented). */
7980 *pfn = DECL_VINDEX (fn);
7981 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7982 TYPE_SIZE_UNIT (vtable_entry_type));
7983 *pfn = fold_if_not_in_template (*pfn);
7985 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7987 case ptrmemfunc_vbit_in_pfn:
7988 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7989 integer_one_node);
7990 *pfn = fold_if_not_in_template (*pfn);
7991 break;
7993 case ptrmemfunc_vbit_in_delta:
7994 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7995 *delta, integer_one_node);
7996 *delta = fold_if_not_in_template (*delta);
7997 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7998 *delta, integer_one_node);
7999 *delta = fold_if_not_in_template (*delta);
8000 break;
8002 default:
8003 gcc_unreachable ();
8006 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8007 *pfn = fold_if_not_in_template (*pfn);
8011 /* Return an expression for PFN from the pointer-to-member function
8012 given by T. */
8014 static tree
8015 pfn_from_ptrmemfunc (tree t)
8017 if (TREE_CODE (t) == PTRMEM_CST)
8019 tree delta;
8020 tree pfn;
8022 expand_ptrmemfunc_cst (t, &delta, &pfn);
8023 if (pfn)
8024 return pfn;
8027 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8030 /* Return an expression for DELTA from the pointer-to-member function
8031 given by T. */
8033 static tree
8034 delta_from_ptrmemfunc (tree t)
8036 if (TREE_CODE (t) == PTRMEM_CST)
8038 tree delta;
8039 tree pfn;
8041 expand_ptrmemfunc_cst (t, &delta, &pfn);
8042 if (delta)
8043 return delta;
8046 return build_ptrmemfunc_access_expr (t, delta_identifier);
8049 /* Convert value RHS to type TYPE as preparation for an assignment to
8050 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8051 implicit conversion is. If FNDECL is non-NULL, we are doing the
8052 conversion in order to pass the PARMNUMth argument of FNDECL.
8053 If FNDECL is NULL, we are doing the conversion in function pointer
8054 argument passing, conversion in initialization, etc. */
8056 static tree
8057 convert_for_assignment (tree type, tree rhs,
8058 impl_conv_rhs errtype, tree fndecl, int parmnum,
8059 tsubst_flags_t complain, int flags)
8061 tree rhstype;
8062 enum tree_code coder;
8064 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8065 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8066 rhs = TREE_OPERAND (rhs, 0);
8068 rhstype = TREE_TYPE (rhs);
8069 coder = TREE_CODE (rhstype);
8071 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8072 && vector_types_convertible_p (type, rhstype, true))
8074 rhs = mark_rvalue_use (rhs);
8075 return convert (type, rhs);
8078 if (rhs == error_mark_node || rhstype == error_mark_node)
8079 return error_mark_node;
8080 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8081 return error_mark_node;
8083 /* The RHS of an assignment cannot have void type. */
8084 if (coder == VOID_TYPE)
8086 if (complain & tf_error)
8087 error ("void value not ignored as it ought to be");
8088 return error_mark_node;
8091 if (c_dialect_objc ())
8093 int parmno;
8094 tree selector;
8095 tree rname = fndecl;
8097 switch (errtype)
8099 case ICR_ASSIGN:
8100 parmno = -1;
8101 break;
8102 case ICR_INIT:
8103 parmno = -2;
8104 break;
8105 default:
8106 selector = objc_message_selector ();
8107 parmno = parmnum;
8108 if (selector && parmno > 1)
8110 rname = selector;
8111 parmno -= 1;
8115 if (objc_compare_types (type, rhstype, parmno, rname))
8117 rhs = mark_rvalue_use (rhs);
8118 return convert (type, rhs);
8122 /* [expr.ass]
8124 The expression is implicitly converted (clause _conv_) to the
8125 cv-unqualified type of the left operand.
8127 We allow bad conversions here because by the time we get to this point
8128 we are committed to doing the conversion. If we end up doing a bad
8129 conversion, convert_like will complain. */
8130 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8132 /* When -Wno-pmf-conversions is use, we just silently allow
8133 conversions from pointers-to-members to plain pointers. If
8134 the conversion doesn't work, cp_convert will complain. */
8135 if (!warn_pmf2ptr
8136 && TYPE_PTR_P (type)
8137 && TYPE_PTRMEMFUNC_P (rhstype))
8138 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8139 else
8141 if (complain & tf_error)
8143 /* If the right-hand side has unknown type, then it is an
8144 overloaded function. Call instantiate_type to get error
8145 messages. */
8146 if (rhstype == unknown_type_node)
8147 instantiate_type (type, rhs, tf_warning_or_error);
8148 else if (fndecl)
8149 error ("cannot convert %qT to %qT for argument %qP to %qD",
8150 rhstype, type, parmnum, fndecl);
8151 else
8152 switch (errtype)
8154 case ICR_DEFAULT_ARGUMENT:
8155 error ("cannot convert %qT to %qT in default argument",
8156 rhstype, type);
8157 break;
8158 case ICR_ARGPASS:
8159 error ("cannot convert %qT to %qT in argument passing",
8160 rhstype, type);
8161 break;
8162 case ICR_CONVERTING:
8163 error ("cannot convert %qT to %qT",
8164 rhstype, type);
8165 break;
8166 case ICR_INIT:
8167 error ("cannot convert %qT to %qT in initialization",
8168 rhstype, type);
8169 break;
8170 case ICR_RETURN:
8171 error ("cannot convert %qT to %qT in return",
8172 rhstype, type);
8173 break;
8174 case ICR_ASSIGN:
8175 error ("cannot convert %qT to %qT in assignment",
8176 rhstype, type);
8177 break;
8178 default:
8179 gcc_unreachable();
8181 if (TYPE_PTR_P (rhstype)
8182 && TYPE_PTR_P (type)
8183 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8184 && CLASS_TYPE_P (TREE_TYPE (type))
8185 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8186 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8187 (TREE_TYPE (rhstype))),
8188 "class type %qT is incomplete", TREE_TYPE (rhstype));
8190 return error_mark_node;
8193 if (warn_suggest_attribute_format)
8195 const enum tree_code codel = TREE_CODE (type);
8196 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8197 && coder == codel
8198 && check_missing_format_attribute (type, rhstype)
8199 && (complain & tf_warning))
8200 switch (errtype)
8202 case ICR_ARGPASS:
8203 case ICR_DEFAULT_ARGUMENT:
8204 if (fndecl)
8205 warning (OPT_Wsuggest_attribute_format,
8206 "parameter %qP of %qD might be a candidate "
8207 "for a format attribute", parmnum, fndecl);
8208 else
8209 warning (OPT_Wsuggest_attribute_format,
8210 "parameter might be a candidate "
8211 "for a format attribute");
8212 break;
8213 case ICR_CONVERTING:
8214 warning (OPT_Wsuggest_attribute_format,
8215 "target of conversion might be a candidate "
8216 "for a format attribute");
8217 break;
8218 case ICR_INIT:
8219 warning (OPT_Wsuggest_attribute_format,
8220 "target of initialization might be a candidate "
8221 "for a format attribute");
8222 break;
8223 case ICR_RETURN:
8224 warning (OPT_Wsuggest_attribute_format,
8225 "return type might be a candidate "
8226 "for a format attribute");
8227 break;
8228 case ICR_ASSIGN:
8229 warning (OPT_Wsuggest_attribute_format,
8230 "left-hand side of assignment might be a candidate "
8231 "for a format attribute");
8232 break;
8233 default:
8234 gcc_unreachable();
8238 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8239 does not. */
8240 if (warn_parentheses
8241 && TREE_CODE (type) == BOOLEAN_TYPE
8242 && TREE_CODE (rhs) == MODIFY_EXPR
8243 && !TREE_NO_WARNING (rhs)
8244 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8245 && (complain & tf_warning))
8247 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8249 warning_at (loc, OPT_Wparentheses,
8250 "suggest parentheses around assignment used as truth value");
8251 TREE_NO_WARNING (rhs) = 1;
8254 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8255 complain, flags);
8258 /* Convert RHS to be of type TYPE.
8259 If EXP is nonzero, it is the target of the initialization.
8260 ERRTYPE indicates what kind of error the implicit conversion is.
8262 Two major differences between the behavior of
8263 `convert_for_assignment' and `convert_for_initialization'
8264 are that references are bashed in the former, while
8265 copied in the latter, and aggregates are assigned in
8266 the former (operator=) while initialized in the
8267 latter (X(X&)).
8269 If using constructor make sure no conversion operator exists, if one does
8270 exist, an ambiguity exists. */
8272 tree
8273 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8274 impl_conv_rhs errtype, tree fndecl, int parmnum,
8275 tsubst_flags_t complain)
8277 enum tree_code codel = TREE_CODE (type);
8278 tree rhstype;
8279 enum tree_code coder;
8281 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8282 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8283 if (TREE_CODE (rhs) == NOP_EXPR
8284 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8285 && codel != REFERENCE_TYPE)
8286 rhs = TREE_OPERAND (rhs, 0);
8288 if (type == error_mark_node
8289 || rhs == error_mark_node
8290 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8291 return error_mark_node;
8293 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8294 && TREE_CODE (type) != ARRAY_TYPE
8295 && (TREE_CODE (type) != REFERENCE_TYPE
8296 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8297 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8298 && !TYPE_REFFN_P (type))
8299 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8300 rhs = decay_conversion (rhs, complain);
8302 rhstype = TREE_TYPE (rhs);
8303 coder = TREE_CODE (rhstype);
8305 if (coder == ERROR_MARK)
8306 return error_mark_node;
8308 /* We accept references to incomplete types, so we can
8309 return here before checking if RHS is of complete type. */
8311 if (codel == REFERENCE_TYPE)
8313 /* This should eventually happen in convert_arguments. */
8314 int savew = 0, savee = 0;
8316 if (fndecl)
8317 savew = warningcount + werrorcount, savee = errorcount;
8318 rhs = initialize_reference (type, rhs, flags, complain);
8320 if (fndecl
8321 && (warningcount + werrorcount > savew || errorcount > savee))
8322 inform (input_location,
8323 "in passing argument %P of %q+D", parmnum, fndecl);
8325 return rhs;
8328 if (exp != 0)
8329 exp = require_complete_type_sfinae (exp, complain);
8330 if (exp == error_mark_node)
8331 return error_mark_node;
8333 rhstype = non_reference (rhstype);
8335 type = complete_type (type);
8337 if (DIRECT_INIT_EXPR_P (type, rhs))
8338 /* Don't try to do copy-initialization if we already have
8339 direct-initialization. */
8340 return rhs;
8342 if (MAYBE_CLASS_TYPE_P (type))
8343 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8345 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8346 complain, flags);
8349 /* If RETVAL is the address of, or a reference to, a local variable or
8350 temporary give an appropriate warning and return true. */
8352 static bool
8353 maybe_warn_about_returning_address_of_local (tree retval)
8355 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8356 tree whats_returned = retval;
8358 for (;;)
8360 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8361 whats_returned = TREE_OPERAND (whats_returned, 1);
8362 else if (CONVERT_EXPR_P (whats_returned)
8363 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8364 whats_returned = TREE_OPERAND (whats_returned, 0);
8365 else
8366 break;
8369 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8370 return false;
8371 whats_returned = TREE_OPERAND (whats_returned, 0);
8373 while (TREE_CODE (whats_returned) == COMPONENT_REF
8374 || TREE_CODE (whats_returned) == ARRAY_REF)
8375 whats_returned = TREE_OPERAND (whats_returned, 0);
8377 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8379 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8380 || TREE_CODE (whats_returned) == TARGET_EXPR)
8382 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8383 return true;
8385 if (VAR_P (whats_returned)
8386 && DECL_NAME (whats_returned)
8387 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8389 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8390 return true;
8394 if (DECL_P (whats_returned)
8395 && DECL_NAME (whats_returned)
8396 && DECL_FUNCTION_SCOPE_P (whats_returned)
8397 && !is_capture_proxy (whats_returned)
8398 && !(TREE_STATIC (whats_returned)
8399 || TREE_PUBLIC (whats_returned)))
8401 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8402 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8403 whats_returned);
8404 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8405 warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
8406 whats_returned);
8407 else
8408 warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
8409 "returned", whats_returned);
8410 return true;
8413 return false;
8416 /* Check that returning RETVAL from the current function is valid.
8417 Return an expression explicitly showing all conversions required to
8418 change RETVAL into the function return type, and to assign it to
8419 the DECL_RESULT for the function. Set *NO_WARNING to true if
8420 code reaches end of non-void function warning shouldn't be issued
8421 on this RETURN_EXPR. */
8423 tree
8424 check_return_expr (tree retval, bool *no_warning)
8426 tree result;
8427 /* The type actually returned by the function. */
8428 tree valtype;
8429 /* The type the function is declared to return, or void if
8430 the declared type is incomplete. */
8431 tree functype;
8432 int fn_returns_value_p;
8433 bool named_return_value_okay_p;
8435 *no_warning = false;
8437 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8439 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8440 "statement is not allowed");
8441 return NULL_TREE;
8444 /* A `volatile' function is one that isn't supposed to return, ever.
8445 (This is a G++ extension, used to get better code for functions
8446 that call the `volatile' function.) */
8447 if (TREE_THIS_VOLATILE (current_function_decl))
8448 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8450 /* Check for various simple errors. */
8451 if (DECL_DESTRUCTOR_P (current_function_decl))
8453 if (retval)
8454 error ("returning a value from a destructor");
8455 return NULL_TREE;
8457 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8459 if (in_function_try_handler)
8460 /* If a return statement appears in a handler of the
8461 function-try-block of a constructor, the program is ill-formed. */
8462 error ("cannot return from a handler of a function-try-block of a constructor");
8463 else if (retval)
8464 /* You can't return a value from a constructor. */
8465 error ("returning a value from a constructor");
8466 return NULL_TREE;
8469 if (processing_template_decl)
8471 current_function_returns_value = 1;
8472 if (check_for_bare_parameter_packs (retval))
8473 retval = error_mark_node;
8474 return retval;
8477 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8479 /* Deduce auto return type from a return statement. */
8480 if (current_function_auto_return_pattern)
8482 tree auto_node;
8483 tree type;
8485 if (!retval && !is_auto (current_function_auto_return_pattern))
8487 /* Give a helpful error message. */
8488 error ("return-statement with no value, in function returning %qT",
8489 current_function_auto_return_pattern);
8490 inform (input_location, "only plain %<auto%> return type can be "
8491 "deduced to %<void%>");
8492 type = error_mark_node;
8494 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8496 error ("returning initializer list");
8497 type = error_mark_node;
8499 else
8501 if (!retval)
8502 retval = void_node;
8503 auto_node = type_uses_auto (current_function_auto_return_pattern);
8504 type = do_auto_deduction (current_function_auto_return_pattern,
8505 retval, auto_node);
8508 if (type == error_mark_node)
8509 /* Leave it. */;
8510 else if (functype == current_function_auto_return_pattern)
8511 apply_deduced_return_type (current_function_decl, type);
8512 else
8513 /* A mismatch should have been diagnosed in do_auto_deduction. */
8514 gcc_assert (same_type_p (type, functype));
8515 functype = type;
8518 /* When no explicit return-value is given in a function with a named
8519 return value, the named return value is used. */
8520 result = DECL_RESULT (current_function_decl);
8521 valtype = TREE_TYPE (result);
8522 gcc_assert (valtype != NULL_TREE);
8523 fn_returns_value_p = !VOID_TYPE_P (valtype);
8524 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8525 retval = result;
8527 /* Check for a return statement with no return value in a function
8528 that's supposed to return a value. */
8529 if (!retval && fn_returns_value_p)
8531 if (functype != error_mark_node)
8532 permerror (input_location, "return-statement with no value, in "
8533 "function returning %qT", valtype);
8534 /* Remember that this function did return. */
8535 current_function_returns_value = 1;
8536 /* And signal caller that TREE_NO_WARNING should be set on the
8537 RETURN_EXPR to avoid control reaches end of non-void function
8538 warnings in tree-cfg.c. */
8539 *no_warning = true;
8541 /* Check for a return statement with a value in a function that
8542 isn't supposed to return a value. */
8543 else if (retval && !fn_returns_value_p)
8545 if (VOID_TYPE_P (TREE_TYPE (retval)))
8546 /* You can return a `void' value from a function of `void'
8547 type. In that case, we have to evaluate the expression for
8548 its side-effects. */
8549 finish_expr_stmt (retval);
8550 else
8551 permerror (input_location, "return-statement with a value, in function "
8552 "returning 'void'");
8553 current_function_returns_null = 1;
8555 /* There's really no value to return, after all. */
8556 return NULL_TREE;
8558 else if (!retval)
8559 /* Remember that this function can sometimes return without a
8560 value. */
8561 current_function_returns_null = 1;
8562 else
8563 /* Remember that this function did return a value. */
8564 current_function_returns_value = 1;
8566 /* Check for erroneous operands -- but after giving ourselves a
8567 chance to provide an error about returning a value from a void
8568 function. */
8569 if (error_operand_p (retval))
8571 current_function_return_value = error_mark_node;
8572 return error_mark_node;
8575 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8576 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8577 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8578 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8579 && ! flag_check_new
8580 && retval && null_ptr_cst_p (retval))
8581 warning (0, "%<operator new%> must not return NULL unless it is "
8582 "declared %<throw()%> (or -fcheck-new is in effect)");
8584 /* Effective C++ rule 15. See also start_function. */
8585 if (warn_ecpp
8586 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8588 bool warn = true;
8590 /* The function return type must be a reference to the current
8591 class. */
8592 if (TREE_CODE (valtype) == REFERENCE_TYPE
8593 && same_type_ignoring_top_level_qualifiers_p
8594 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8596 /* Returning '*this' is obviously OK. */
8597 if (retval == current_class_ref)
8598 warn = false;
8599 /* If we are calling a function whose return type is the same of
8600 the current class reference, it is ok. */
8601 else if (INDIRECT_REF_P (retval)
8602 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8603 warn = false;
8606 if (warn)
8607 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8610 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8612 [...] For a function with a class return type, if the expression
8613 in the return statement is the name of a local object, and the cv-
8614 unqualified type of the local object is the same as the function
8615 return type, an implementation is permitted to omit creating the tem-
8616 porary object to hold the function return value [...]
8618 So, if this is a value-returning function that always returns the same
8619 local variable, remember it.
8621 It might be nice to be more flexible, and choose the first suitable
8622 variable even if the function sometimes returns something else, but
8623 then we run the risk of clobbering the variable we chose if the other
8624 returned expression uses the chosen variable somehow. And people expect
8625 this restriction, anyway. (jason 2000-11-19)
8627 See finish_function and finalize_nrv for the rest of this optimization. */
8629 named_return_value_okay_p =
8630 (retval != NULL_TREE
8631 /* Must be a local, automatic variable. */
8632 && VAR_P (retval)
8633 && DECL_CONTEXT (retval) == current_function_decl
8634 && ! TREE_STATIC (retval)
8635 /* And not a lambda or anonymous union proxy. */
8636 && !DECL_HAS_VALUE_EXPR_P (retval)
8637 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8638 /* The cv-unqualified type of the returned value must be the
8639 same as the cv-unqualified return type of the
8640 function. */
8641 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8642 (TYPE_MAIN_VARIANT (functype)))
8643 /* And the returned value must be non-volatile. */
8644 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8646 if (fn_returns_value_p && flag_elide_constructors)
8648 if (named_return_value_okay_p
8649 && (current_function_return_value == NULL_TREE
8650 || current_function_return_value == retval))
8651 current_function_return_value = retval;
8652 else
8653 current_function_return_value = error_mark_node;
8656 /* We don't need to do any conversions when there's nothing being
8657 returned. */
8658 if (!retval)
8659 return NULL_TREE;
8661 /* Do any required conversions. */
8662 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8663 /* No conversions are required. */
8665 else
8667 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8669 /* The functype's return type will have been set to void, if it
8670 was an incomplete type. Just treat this as 'return;' */
8671 if (VOID_TYPE_P (functype))
8672 return error_mark_node;
8674 /* If we had an id-expression obfuscated by force_paren_expr, we need
8675 to undo it so we can try to treat it as an rvalue below. */
8676 if (cxx_dialect >= cxx14
8677 && INDIRECT_REF_P (retval)
8678 && REF_PARENTHESIZED_P (retval))
8680 retval = TREE_OPERAND (retval, 0);
8681 while (TREE_CODE (retval) == NON_LVALUE_EXPR
8682 || TREE_CODE (retval) == NOP_EXPR)
8683 retval = TREE_OPERAND (retval, 0);
8684 gcc_assert (TREE_CODE (retval) == ADDR_EXPR);
8685 retval = TREE_OPERAND (retval, 0);
8688 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8689 treated as an rvalue for the purposes of overload resolution to
8690 favor move constructors over copy constructors.
8692 Note that these conditions are similar to, but not as strict as,
8693 the conditions for the named return value optimization. */
8694 if ((cxx_dialect != cxx98)
8695 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8696 || TREE_CODE (retval) == PARM_DECL)
8697 && DECL_CONTEXT (retval) == current_function_decl
8698 && !TREE_STATIC (retval)
8699 /* This is only interesting for class type. */
8700 && CLASS_TYPE_P (functype))
8701 flags = flags | LOOKUP_PREFER_RVALUE;
8703 /* First convert the value to the function's return type, then
8704 to the type of return value's location to handle the
8705 case that functype is smaller than the valtype. */
8706 retval = convert_for_initialization
8707 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8708 tf_warning_or_error);
8709 retval = convert (valtype, retval);
8711 /* If the conversion failed, treat this just like `return;'. */
8712 if (retval == error_mark_node)
8713 return retval;
8714 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8715 else if (! cfun->returns_struct
8716 && TREE_CODE (retval) == TARGET_EXPR
8717 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8718 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8719 TREE_OPERAND (retval, 0));
8720 else if (maybe_warn_about_returning_address_of_local (retval))
8721 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8722 build_zero_cst (TREE_TYPE (retval)));
8725 /* Actually copy the value returned into the appropriate location. */
8726 if (retval && retval != result)
8727 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8729 return retval;
8733 /* Returns nonzero if the pointer-type FROM can be converted to the
8734 pointer-type TO via a qualification conversion. If CONSTP is -1,
8735 then we return nonzero if the pointers are similar, and the
8736 cv-qualification signature of FROM is a proper subset of that of TO.
8738 If CONSTP is positive, then all outer pointers have been
8739 const-qualified. */
8741 static int
8742 comp_ptr_ttypes_real (tree to, tree from, int constp)
8744 bool to_more_cv_qualified = false;
8745 bool is_opaque_pointer = false;
8747 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8749 if (TREE_CODE (to) != TREE_CODE (from))
8750 return 0;
8752 if (TREE_CODE (from) == OFFSET_TYPE
8753 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8754 TYPE_OFFSET_BASETYPE (to)))
8755 return 0;
8757 /* Const and volatile mean something different for function types,
8758 so the usual checks are not appropriate. */
8759 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8761 if (!at_least_as_qualified_p (to, from))
8762 return 0;
8764 if (!at_least_as_qualified_p (from, to))
8766 if (constp == 0)
8767 return 0;
8768 to_more_cv_qualified = true;
8771 if (constp > 0)
8772 constp &= TYPE_READONLY (to);
8775 if (TREE_CODE (to) == VECTOR_TYPE)
8776 is_opaque_pointer = vector_targets_convertible_p (to, from);
8778 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8779 return ((constp >= 0 || to_more_cv_qualified)
8780 && (is_opaque_pointer
8781 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8785 /* When comparing, say, char ** to char const **, this function takes
8786 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8787 types to this function. */
8790 comp_ptr_ttypes (tree to, tree from)
8792 return comp_ptr_ttypes_real (to, from, 1);
8795 /* Returns true iff FNTYPE is a non-class type that involves
8796 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8797 if a parameter type is ill-formed. */
8799 bool
8800 error_type_p (const_tree type)
8802 tree t;
8804 switch (TREE_CODE (type))
8806 case ERROR_MARK:
8807 return true;
8809 case POINTER_TYPE:
8810 case REFERENCE_TYPE:
8811 case OFFSET_TYPE:
8812 return error_type_p (TREE_TYPE (type));
8814 case FUNCTION_TYPE:
8815 case METHOD_TYPE:
8816 if (error_type_p (TREE_TYPE (type)))
8817 return true;
8818 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8819 if (error_type_p (TREE_VALUE (t)))
8820 return true;
8821 return false;
8823 case RECORD_TYPE:
8824 if (TYPE_PTRMEMFUNC_P (type))
8825 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8826 return false;
8828 default:
8829 return false;
8833 /* Returns true if to and from are (possibly multi-level) pointers to the same
8834 type or inheritance-related types, regardless of cv-quals. */
8836 bool
8837 ptr_reasonably_similar (const_tree to, const_tree from)
8839 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8841 /* Any target type is similar enough to void. */
8842 if (VOID_TYPE_P (to))
8843 return !error_type_p (from);
8844 if (VOID_TYPE_P (from))
8845 return !error_type_p (to);
8847 if (TREE_CODE (to) != TREE_CODE (from))
8848 return false;
8850 if (TREE_CODE (from) == OFFSET_TYPE
8851 && comptypes (TYPE_OFFSET_BASETYPE (to),
8852 TYPE_OFFSET_BASETYPE (from),
8853 COMPARE_BASE | COMPARE_DERIVED))
8854 continue;
8856 if (TREE_CODE (to) == VECTOR_TYPE
8857 && vector_types_convertible_p (to, from, false))
8858 return true;
8860 if (TREE_CODE (to) == INTEGER_TYPE
8861 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8862 return true;
8864 if (TREE_CODE (to) == FUNCTION_TYPE)
8865 return !error_type_p (to) && !error_type_p (from);
8867 if (!TYPE_PTR_P (to))
8869 /* When either type is incomplete avoid DERIVED_FROM_P,
8870 which may call complete_type (c++/57942). */
8871 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8872 return comptypes
8873 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8874 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8879 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8880 pointer-to-member types) are the same, ignoring cv-qualification at
8881 all levels. */
8883 bool
8884 comp_ptr_ttypes_const (tree to, tree from)
8886 bool is_opaque_pointer = false;
8888 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8890 if (TREE_CODE (to) != TREE_CODE (from))
8891 return false;
8893 if (TREE_CODE (from) == OFFSET_TYPE
8894 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8895 TYPE_OFFSET_BASETYPE (to)))
8896 continue;
8898 if (TREE_CODE (to) == VECTOR_TYPE)
8899 is_opaque_pointer = vector_targets_convertible_p (to, from);
8901 if (!TYPE_PTR_P (to))
8902 return (is_opaque_pointer
8903 || same_type_ignoring_top_level_qualifiers_p (to, from));
8907 /* Returns the type qualifiers for this type, including the qualifiers on the
8908 elements for an array type. */
8911 cp_type_quals (const_tree type)
8913 int quals;
8914 /* This CONST_CAST is okay because strip_array_types returns its
8915 argument unmodified and we assign it to a const_tree. */
8916 type = strip_array_types (CONST_CAST_TREE (type));
8917 if (type == error_mark_node
8918 /* Quals on a FUNCTION_TYPE are memfn quals. */
8919 || TREE_CODE (type) == FUNCTION_TYPE)
8920 return TYPE_UNQUALIFIED;
8921 quals = TYPE_QUALS (type);
8922 /* METHOD and REFERENCE_TYPEs should never have quals. */
8923 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8924 && TREE_CODE (type) != REFERENCE_TYPE)
8925 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8926 == TYPE_UNQUALIFIED));
8927 return quals;
8930 /* Returns the function-ref-qualifier for TYPE */
8932 cp_ref_qualifier
8933 type_memfn_rqual (const_tree type)
8935 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8936 || TREE_CODE (type) == METHOD_TYPE);
8938 if (!FUNCTION_REF_QUALIFIED (type))
8939 return REF_QUAL_NONE;
8940 else if (FUNCTION_RVALUE_QUALIFIED (type))
8941 return REF_QUAL_RVALUE;
8942 else
8943 return REF_QUAL_LVALUE;
8946 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8947 METHOD_TYPE. */
8950 type_memfn_quals (const_tree type)
8952 if (TREE_CODE (type) == FUNCTION_TYPE)
8953 return TYPE_QUALS (type);
8954 else if (TREE_CODE (type) == METHOD_TYPE)
8955 return cp_type_quals (class_of_this_parm (type));
8956 else
8957 gcc_unreachable ();
8960 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8961 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8963 tree
8964 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8966 /* Could handle METHOD_TYPE here if necessary. */
8967 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8968 if (TYPE_QUALS (type) == memfn_quals
8969 && type_memfn_rqual (type) == rqual)
8970 return type;
8972 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8973 complex. */
8974 tree result = build_qualified_type (type, memfn_quals);
8975 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8976 return build_ref_qualified_type (result, rqual);
8979 /* Returns nonzero if TYPE is const or volatile. */
8981 bool
8982 cv_qualified_p (const_tree type)
8984 int quals = cp_type_quals (type);
8985 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8988 /* Returns nonzero if the TYPE contains a mutable member. */
8990 bool
8991 cp_has_mutable_p (const_tree type)
8993 /* This CONST_CAST is okay because strip_array_types returns its
8994 argument unmodified and we assign it to a const_tree. */
8995 type = strip_array_types (CONST_CAST_TREE(type));
8997 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9000 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9001 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9002 approximation. In particular, consider:
9004 int f();
9005 struct S { int i; };
9006 const S s = { f(); }
9008 Here, we will make "s" as TREE_READONLY (because it is declared
9009 "const") -- only to reverse ourselves upon seeing that the
9010 initializer is non-constant. */
9012 void
9013 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9015 tree type = TREE_TYPE (decl);
9017 if (type == error_mark_node)
9018 return;
9020 if (TREE_CODE (decl) == TYPE_DECL)
9021 return;
9023 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9024 && type_quals != TYPE_UNQUALIFIED));
9026 /* Avoid setting TREE_READONLY incorrectly. */
9027 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9028 constructor can produce constant init, so rely on cp_finish_decl to
9029 clear TREE_READONLY if the variable has non-constant init. */
9031 /* If the type has (or might have) a mutable component, that component
9032 might be modified. */
9033 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9034 type_quals &= ~TYPE_QUAL_CONST;
9036 c_apply_type_quals_to_decl (type_quals, decl);
9039 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9040 exemplar types such that casting T1 to T2 is casting away constness
9041 if and only if there is no implicit conversion from T1 to T2. */
9043 static void
9044 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9046 int quals1;
9047 int quals2;
9049 /* [expr.const.cast]
9051 For multi-level pointer to members and multi-level mixed pointers
9052 and pointers to members (conv.qual), the "member" aspect of a
9053 pointer to member level is ignored when determining if a const
9054 cv-qualifier has been cast away. */
9055 /* [expr.const.cast]
9057 For two pointer types:
9059 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9060 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9061 K is min(N,M)
9063 casting from X1 to X2 casts away constness if, for a non-pointer
9064 type T there does not exist an implicit conversion (clause
9065 _conv_) from:
9067 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9071 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9072 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9073 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9075 *t1 = cp_build_qualified_type (void_type_node,
9076 cp_type_quals (*t1));
9077 *t2 = cp_build_qualified_type (void_type_node,
9078 cp_type_quals (*t2));
9079 return;
9082 quals1 = cp_type_quals (*t1);
9083 quals2 = cp_type_quals (*t2);
9085 if (TYPE_PTRDATAMEM_P (*t1))
9086 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9087 else
9088 *t1 = TREE_TYPE (*t1);
9089 if (TYPE_PTRDATAMEM_P (*t2))
9090 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9091 else
9092 *t2 = TREE_TYPE (*t2);
9094 casts_away_constness_r (t1, t2, complain);
9095 *t1 = build_pointer_type (*t1);
9096 *t2 = build_pointer_type (*t2);
9097 *t1 = cp_build_qualified_type (*t1, quals1);
9098 *t2 = cp_build_qualified_type (*t2, quals2);
9101 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9102 constness.
9104 ??? This function returns non-zero if casting away qualifiers not
9105 just const. We would like to return to the caller exactly which
9106 qualifiers are casted away to give more accurate diagnostics.
9109 static bool
9110 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9112 if (TREE_CODE (t2) == REFERENCE_TYPE)
9114 /* [expr.const.cast]
9116 Casting from an lvalue of type T1 to an lvalue of type T2
9117 using a reference cast casts away constness if a cast from an
9118 rvalue of type "pointer to T1" to the type "pointer to T2"
9119 casts away constness. */
9120 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9121 return casts_away_constness (build_pointer_type (t1),
9122 build_pointer_type (TREE_TYPE (t2)),
9123 complain);
9126 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9127 /* [expr.const.cast]
9129 Casting from an rvalue of type "pointer to data member of X
9130 of type T1" to the type "pointer to data member of Y of type
9131 T2" casts away constness if a cast from an rvalue of type
9132 "pointer to T1" to the type "pointer to T2" casts away
9133 constness. */
9134 return casts_away_constness
9135 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9136 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9137 complain);
9139 /* Casting away constness is only something that makes sense for
9140 pointer or reference types. */
9141 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9142 return false;
9144 /* Top-level qualifiers don't matter. */
9145 t1 = TYPE_MAIN_VARIANT (t1);
9146 t2 = TYPE_MAIN_VARIANT (t2);
9147 casts_away_constness_r (&t1, &t2, complain);
9148 if (!can_convert (t2, t1, complain))
9149 return true;
9151 return false;
9154 /* If T is a REFERENCE_TYPE return the type to which T refers.
9155 Otherwise, return T itself. */
9157 tree
9158 non_reference (tree t)
9160 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9161 t = TREE_TYPE (t);
9162 return t;
9166 /* Return nonzero if REF is an lvalue valid for this language;
9167 otherwise, print an error message and return zero. USE says
9168 how the lvalue is being used and so selects the error message. */
9171 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9173 cp_lvalue_kind kind = lvalue_kind (ref);
9175 if (kind == clk_none)
9177 if (complain & tf_error)
9178 lvalue_error (input_location, use);
9179 return 0;
9181 else if (kind & (clk_rvalueref|clk_class))
9183 if (!(complain & tf_error))
9184 return 0;
9185 if (kind & clk_class)
9186 /* Make this a permerror because we used to accept it. */
9187 permerror (input_location, "using temporary as lvalue");
9188 else
9189 error ("using xvalue (rvalue reference) as lvalue");
9191 return 1;
9194 /* Return true if a user-defined literal operator is a raw operator. */
9196 bool
9197 check_raw_literal_operator (const_tree decl)
9199 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9200 tree argtype;
9201 int arity;
9202 bool maybe_raw_p = false;
9204 /* Count the number and type of arguments and check for ellipsis. */
9205 for (argtype = argtypes, arity = 0;
9206 argtype && argtype != void_list_node;
9207 ++arity, argtype = TREE_CHAIN (argtype))
9209 tree t = TREE_VALUE (argtype);
9211 if (same_type_p (t, const_string_type_node))
9212 maybe_raw_p = true;
9214 if (!argtype)
9215 return false; /* Found ellipsis. */
9217 if (!maybe_raw_p || arity != 1)
9218 return false;
9220 return true;
9224 /* Return true if a user-defined literal operator has one of the allowed
9225 argument types. */
9227 bool
9228 check_literal_operator_args (const_tree decl,
9229 bool *long_long_unsigned_p, bool *long_double_p)
9231 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9233 *long_long_unsigned_p = false;
9234 *long_double_p = false;
9235 if (processing_template_decl || processing_specialization)
9236 return argtypes == void_list_node;
9237 else
9239 tree argtype;
9240 int arity;
9241 int max_arity = 2;
9243 /* Count the number and type of arguments and check for ellipsis. */
9244 for (argtype = argtypes, arity = 0;
9245 argtype && argtype != void_list_node;
9246 argtype = TREE_CHAIN (argtype))
9248 tree t = TREE_VALUE (argtype);
9249 ++arity;
9251 if (TYPE_PTR_P (t))
9253 bool maybe_raw_p = false;
9254 t = TREE_TYPE (t);
9255 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9256 return false;
9257 t = TYPE_MAIN_VARIANT (t);
9258 if ((maybe_raw_p = same_type_p (t, char_type_node))
9259 || same_type_p (t, wchar_type_node)
9260 || same_type_p (t, char16_type_node)
9261 || same_type_p (t, char32_type_node))
9263 argtype = TREE_CHAIN (argtype);
9264 if (!argtype)
9265 return false;
9266 t = TREE_VALUE (argtype);
9267 if (maybe_raw_p && argtype == void_list_node)
9268 return true;
9269 else if (same_type_p (t, size_type_node))
9271 ++arity;
9272 continue;
9274 else
9275 return false;
9278 else if (same_type_p (t, long_long_unsigned_type_node))
9280 max_arity = 1;
9281 *long_long_unsigned_p = true;
9283 else if (same_type_p (t, long_double_type_node))
9285 max_arity = 1;
9286 *long_double_p = true;
9288 else if (same_type_p (t, char_type_node))
9289 max_arity = 1;
9290 else if (same_type_p (t, wchar_type_node))
9291 max_arity = 1;
9292 else if (same_type_p (t, char16_type_node))
9293 max_arity = 1;
9294 else if (same_type_p (t, char32_type_node))
9295 max_arity = 1;
9296 else
9297 return false;
9299 if (!argtype)
9300 return false; /* Found ellipsis. */
9302 if (arity != max_arity)
9303 return false;
9305 return true;