Daily bump.
[official-gcc.git] / gcc / cp / typeck.c
blobaa82f1c0a78fc4726d02dc5079d2726de4e0d7f6
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 (TREE_CODE (op) == NOP_EXPR
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 = save_expr (op0);
4049 op0 = convert (TREE_TYPE (type1), 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 = save_expr (op1);
4059 op1 = convert (TREE_TYPE (type0), 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 sorry ("logical operation on vector type");
4197 return error_mark_node;
4199 result_type = boolean_type_node;
4200 break;
4202 /* Shift operations: result has same type as first operand;
4203 always convert second operand to int.
4204 Also set SHORT_SHIFT if shifting rightward. */
4206 case RSHIFT_EXPR:
4207 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4208 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4210 result_type = type0;
4211 converted = 1;
4213 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4214 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4215 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4216 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4218 result_type = type0;
4219 converted = 1;
4221 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4223 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4224 const_op1 = maybe_constant_value (const_op1);
4225 if (TREE_CODE (const_op1) != INTEGER_CST)
4226 const_op1 = op1;
4227 result_type = type0;
4228 doing_shift = true;
4229 if (TREE_CODE (const_op1) == INTEGER_CST)
4231 if (tree_int_cst_lt (const_op1, integer_zero_node))
4233 if ((complain & tf_warning)
4234 && c_inhibit_evaluation_warnings == 0)
4235 warning (0, "right shift count is negative");
4237 else
4239 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4240 && (complain & tf_warning)
4241 && c_inhibit_evaluation_warnings == 0)
4242 warning (0, "right shift count >= width of type");
4245 /* Convert the shift-count to an integer, regardless of
4246 size of value being shifted. */
4247 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4248 op1 = cp_convert (integer_type_node, op1, complain);
4249 /* Avoid converting op1 to result_type later. */
4250 converted = 1;
4252 break;
4254 case LSHIFT_EXPR:
4255 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4256 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4258 result_type = type0;
4259 converted = 1;
4261 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4262 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4263 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4264 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4266 result_type = type0;
4267 converted = 1;
4269 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4271 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4272 const_op1 = maybe_constant_value (const_op1);
4273 if (TREE_CODE (const_op1) != INTEGER_CST)
4274 const_op1 = op1;
4275 result_type = type0;
4276 doing_shift = true;
4277 if (TREE_CODE (const_op1) == INTEGER_CST)
4279 if (tree_int_cst_lt (const_op1, integer_zero_node))
4281 if ((complain & tf_warning)
4282 && c_inhibit_evaluation_warnings == 0)
4283 warning (0, "left shift count is negative");
4285 else if (compare_tree_int (const_op1,
4286 TYPE_PRECISION (type0)) >= 0)
4288 if ((complain & tf_warning)
4289 && c_inhibit_evaluation_warnings == 0)
4290 warning (0, "left shift count >= width of type");
4293 /* Convert the shift-count to an integer, regardless of
4294 size of value being shifted. */
4295 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4296 op1 = cp_convert (integer_type_node, op1, complain);
4297 /* Avoid converting op1 to result_type later. */
4298 converted = 1;
4300 break;
4302 case RROTATE_EXPR:
4303 case LROTATE_EXPR:
4304 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4306 result_type = type0;
4307 if (TREE_CODE (op1) == INTEGER_CST)
4309 if (tree_int_cst_lt (op1, integer_zero_node))
4311 if (complain & tf_warning)
4312 warning (0, (code == LROTATE_EXPR)
4313 ? G_("left rotate count is negative")
4314 : G_("right rotate count is negative"));
4316 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4318 if (complain & tf_warning)
4319 warning (0, (code == LROTATE_EXPR)
4320 ? G_("left rotate count >= width of type")
4321 : G_("right rotate count >= width of type"));
4324 /* Convert the shift-count to an integer, regardless of
4325 size of value being shifted. */
4326 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4327 op1 = cp_convert (integer_type_node, op1, complain);
4329 break;
4331 case EQ_EXPR:
4332 case NE_EXPR:
4333 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4334 goto vector_compare;
4335 if ((complain & tf_warning)
4336 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4337 warning (OPT_Wfloat_equal,
4338 "comparing floating point with == or != is unsafe");
4339 if ((complain & tf_warning)
4340 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4341 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4342 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4344 build_type = boolean_type_node;
4345 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4346 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4347 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4348 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4349 short_compare = 1;
4350 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4351 && null_ptr_cst_p (op1))
4352 /* Handle, eg, (void*)0 (c++/43906), and more. */
4353 || (code0 == POINTER_TYPE
4354 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4356 if (TYPE_PTR_P (type1))
4357 result_type = composite_pointer_type (type0, type1, op0, op1,
4358 CPO_COMPARISON, complain);
4359 else
4360 result_type = type0;
4362 if (TREE_CODE (op0) == ADDR_EXPR
4363 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4365 if ((complain & tf_warning)
4366 && c_inhibit_evaluation_warnings == 0)
4367 warning (OPT_Waddress, "the address of %qD will never be NULL",
4368 TREE_OPERAND (op0, 0));
4371 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4372 && null_ptr_cst_p (op0))
4373 /* Handle, eg, (void*)0 (c++/43906), and more. */
4374 || (code1 == POINTER_TYPE
4375 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4377 if (TYPE_PTR_P (type0))
4378 result_type = composite_pointer_type (type0, type1, op0, op1,
4379 CPO_COMPARISON, complain);
4380 else
4381 result_type = type1;
4383 if (TREE_CODE (op1) == ADDR_EXPR
4384 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4386 if ((complain & tf_warning)
4387 && c_inhibit_evaluation_warnings == 0)
4388 warning (OPT_Waddress, "the address of %qD will never be NULL",
4389 TREE_OPERAND (op1, 0));
4392 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4393 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4394 result_type = composite_pointer_type (type0, type1, op0, op1,
4395 CPO_COMPARISON, complain);
4396 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4397 /* One of the operands must be of nullptr_t type. */
4398 result_type = TREE_TYPE (nullptr_node);
4399 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4401 result_type = type0;
4402 if (complain & tf_error)
4403 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4404 else
4405 return error_mark_node;
4407 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4409 result_type = type1;
4410 if (complain & tf_error)
4411 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4412 else
4413 return error_mark_node;
4415 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4417 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4418 == ptrmemfunc_vbit_in_delta)
4420 tree pfn0, delta0, e1, e2;
4422 if (TREE_SIDE_EFFECTS (op0))
4423 op0 = save_expr (op0);
4425 pfn0 = pfn_from_ptrmemfunc (op0);
4426 delta0 = delta_from_ptrmemfunc (op0);
4427 e1 = cp_build_binary_op (location,
4428 EQ_EXPR,
4429 pfn0,
4430 build_zero_cst (TREE_TYPE (pfn0)),
4431 complain);
4432 e2 = cp_build_binary_op (location,
4433 BIT_AND_EXPR,
4434 delta0,
4435 integer_one_node,
4436 complain);
4438 if (complain & tf_warning)
4439 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4441 e2 = cp_build_binary_op (location,
4442 EQ_EXPR, e2, integer_zero_node,
4443 complain);
4444 op0 = cp_build_binary_op (location,
4445 TRUTH_ANDIF_EXPR, e1, e2,
4446 complain);
4447 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4449 else
4451 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4452 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4454 result_type = TREE_TYPE (op0);
4456 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4457 return cp_build_binary_op (location, code, op1, op0, complain);
4458 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4460 tree type;
4461 /* E will be the final comparison. */
4462 tree e;
4463 /* E1 and E2 are for scratch. */
4464 tree e1;
4465 tree e2;
4466 tree pfn0;
4467 tree pfn1;
4468 tree delta0;
4469 tree delta1;
4471 type = composite_pointer_type (type0, type1, op0, op1,
4472 CPO_COMPARISON, complain);
4474 if (!same_type_p (TREE_TYPE (op0), type))
4475 op0 = cp_convert_and_check (type, op0, complain);
4476 if (!same_type_p (TREE_TYPE (op1), type))
4477 op1 = cp_convert_and_check (type, op1, complain);
4479 if (op0 == error_mark_node || op1 == error_mark_node)
4480 return error_mark_node;
4482 if (TREE_SIDE_EFFECTS (op0))
4483 op0 = save_expr (op0);
4484 if (TREE_SIDE_EFFECTS (op1))
4485 op1 = save_expr (op1);
4487 pfn0 = pfn_from_ptrmemfunc (op0);
4488 pfn1 = pfn_from_ptrmemfunc (op1);
4489 delta0 = delta_from_ptrmemfunc (op0);
4490 delta1 = delta_from_ptrmemfunc (op1);
4491 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4492 == ptrmemfunc_vbit_in_delta)
4494 /* We generate:
4496 (op0.pfn == op1.pfn
4497 && ((op0.delta == op1.delta)
4498 || (!op0.pfn && op0.delta & 1 == 0
4499 && op1.delta & 1 == 0))
4501 The reason for the `!op0.pfn' bit is that a NULL
4502 pointer-to-member is any member with a zero PFN and
4503 LSB of the DELTA field is 0. */
4505 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4506 delta0,
4507 integer_one_node,
4508 complain);
4509 e1 = cp_build_binary_op (location,
4510 EQ_EXPR, e1, integer_zero_node,
4511 complain);
4512 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4513 delta1,
4514 integer_one_node,
4515 complain);
4516 e2 = cp_build_binary_op (location,
4517 EQ_EXPR, e2, integer_zero_node,
4518 complain);
4519 e1 = cp_build_binary_op (location,
4520 TRUTH_ANDIF_EXPR, e2, e1,
4521 complain);
4522 e2 = cp_build_binary_op (location, EQ_EXPR,
4523 pfn0,
4524 build_zero_cst (TREE_TYPE (pfn0)),
4525 complain);
4526 e2 = cp_build_binary_op (location,
4527 TRUTH_ANDIF_EXPR, e2, e1, complain);
4528 e1 = cp_build_binary_op (location,
4529 EQ_EXPR, delta0, delta1, complain);
4530 e1 = cp_build_binary_op (location,
4531 TRUTH_ORIF_EXPR, e1, e2, complain);
4533 else
4535 /* We generate:
4537 (op0.pfn == op1.pfn
4538 && (!op0.pfn || op0.delta == op1.delta))
4540 The reason for the `!op0.pfn' bit is that a NULL
4541 pointer-to-member is any member with a zero PFN; the
4542 DELTA field is unspecified. */
4544 e1 = cp_build_binary_op (location,
4545 EQ_EXPR, delta0, delta1, complain);
4546 e2 = cp_build_binary_op (location,
4547 EQ_EXPR,
4548 pfn0,
4549 build_zero_cst (TREE_TYPE (pfn0)),
4550 complain);
4551 e1 = cp_build_binary_op (location,
4552 TRUTH_ORIF_EXPR, e1, e2, complain);
4554 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4555 e = cp_build_binary_op (location,
4556 TRUTH_ANDIF_EXPR, e2, e1, complain);
4557 if (code == EQ_EXPR)
4558 return e;
4559 return cp_build_binary_op (location,
4560 EQ_EXPR, e, integer_zero_node, complain);
4562 else
4564 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4565 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4566 type1));
4567 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4568 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4569 type0));
4572 break;
4574 case MAX_EXPR:
4575 case MIN_EXPR:
4576 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4577 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4578 shorten = 1;
4579 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4580 result_type = composite_pointer_type (type0, type1, op0, op1,
4581 CPO_COMPARISON, complain);
4582 break;
4584 case LE_EXPR:
4585 case GE_EXPR:
4586 case LT_EXPR:
4587 case GT_EXPR:
4588 if (TREE_CODE (orig_op0) == STRING_CST
4589 || TREE_CODE (orig_op1) == STRING_CST)
4591 if (complain & tf_warning)
4592 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4595 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4597 vector_compare:
4598 tree intt;
4599 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4600 TREE_TYPE (type1))
4601 && !vector_types_compatible_elements_p (type0, type1))
4603 if (complain & tf_error)
4605 error_at (location, "comparing vectors with different "
4606 "element types");
4607 inform (location, "operand types are %qT and %qT",
4608 type0, type1);
4610 return error_mark_node;
4613 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4615 if (complain & tf_error)
4617 error_at (location, "comparing vectors with different "
4618 "number of elements");
4619 inform (location, "operand types are %qT and %qT",
4620 type0, type1);
4622 return error_mark_node;
4625 /* Always construct signed integer vector type. */
4626 intt = c_common_type_for_size (GET_MODE_BITSIZE
4627 (TYPE_MODE (TREE_TYPE (type0))), 0);
4628 if (!intt)
4630 if (complain & tf_error)
4631 error_at (location, "could not find an integer type "
4632 "of the same size as %qT", TREE_TYPE (type0));
4633 return error_mark_node;
4635 result_type = build_opaque_vector_type (intt,
4636 TYPE_VECTOR_SUBPARTS (type0));
4637 converted = 1;
4638 break;
4640 build_type = boolean_type_node;
4641 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4642 || code0 == ENUMERAL_TYPE)
4643 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4644 || code1 == ENUMERAL_TYPE))
4645 short_compare = 1;
4646 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4647 result_type = composite_pointer_type (type0, type1, op0, op1,
4648 CPO_COMPARISON, complain);
4649 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4651 result_type = type0;
4652 if (extra_warnings && (complain & tf_warning))
4653 warning (OPT_Wextra,
4654 "ordered comparison of pointer with integer zero");
4656 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4658 result_type = type1;
4659 if (extra_warnings && (complain & tf_warning))
4660 warning (OPT_Wextra,
4661 "ordered comparison of pointer with integer zero");
4663 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4664 /* One of the operands must be of nullptr_t type. */
4665 result_type = TREE_TYPE (nullptr_node);
4666 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4668 result_type = type0;
4669 if (complain & tf_error)
4670 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4671 else
4672 return error_mark_node;
4674 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4676 result_type = type1;
4677 if (complain & tf_error)
4678 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4679 else
4680 return error_mark_node;
4682 break;
4684 case UNORDERED_EXPR:
4685 case ORDERED_EXPR:
4686 case UNLT_EXPR:
4687 case UNLE_EXPR:
4688 case UNGT_EXPR:
4689 case UNGE_EXPR:
4690 case UNEQ_EXPR:
4691 build_type = integer_type_node;
4692 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4694 if (complain & tf_error)
4695 error ("unordered comparison on non-floating point argument");
4696 return error_mark_node;
4698 common = 1;
4699 break;
4701 default:
4702 break;
4705 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4706 || code0 == ENUMERAL_TYPE)
4707 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4708 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4709 arithmetic_types_p = 1;
4710 else
4712 arithmetic_types_p = 0;
4713 /* Vector arithmetic is only allowed when both sides are vectors. */
4714 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4716 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4717 || !vector_types_compatible_elements_p (type0, type1))
4719 if (complain & tf_error)
4720 binary_op_error (location, code, type0, type1);
4721 return error_mark_node;
4723 arithmetic_types_p = 1;
4726 /* Determine the RESULT_TYPE, if it is not already known. */
4727 if (!result_type
4728 && arithmetic_types_p
4729 && (shorten || common || short_compare))
4731 result_type = cp_common_type (type0, type1);
4732 if (complain & tf_warning)
4733 do_warn_double_promotion (result_type, type0, type1,
4734 "implicit conversion from %qT to %qT "
4735 "to match other operand of binary "
4736 "expression",
4737 location);
4740 if (!result_type)
4742 if (complain & tf_error)
4743 error ("invalid operands of types %qT and %qT to binary %qO",
4744 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4745 return error_mark_node;
4748 /* If we're in a template, the only thing we need to know is the
4749 RESULT_TYPE. */
4750 if (processing_template_decl)
4752 /* Since the middle-end checks the type when doing a build2, we
4753 need to build the tree in pieces. This built tree will never
4754 get out of the front-end as we replace it when instantiating
4755 the template. */
4756 tree tmp = build2 (resultcode,
4757 build_type ? build_type : result_type,
4758 NULL_TREE, op1);
4759 TREE_OPERAND (tmp, 0) = op0;
4760 return tmp;
4763 if (arithmetic_types_p)
4765 bool first_complex = (code0 == COMPLEX_TYPE);
4766 bool second_complex = (code1 == COMPLEX_TYPE);
4767 int none_complex = (!first_complex && !second_complex);
4769 /* Adapted from patch for c/24581. */
4770 if (first_complex != second_complex
4771 && (code == PLUS_EXPR
4772 || code == MINUS_EXPR
4773 || code == MULT_EXPR
4774 || (code == TRUNC_DIV_EXPR && first_complex))
4775 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4776 && flag_signed_zeros)
4778 /* An operation on mixed real/complex operands must be
4779 handled specially, but the language-independent code can
4780 more easily optimize the plain complex arithmetic if
4781 -fno-signed-zeros. */
4782 tree real_type = TREE_TYPE (result_type);
4783 tree real, imag;
4784 if (first_complex)
4786 if (TREE_TYPE (op0) != result_type)
4787 op0 = cp_convert_and_check (result_type, op0, complain);
4788 if (TREE_TYPE (op1) != real_type)
4789 op1 = cp_convert_and_check (real_type, op1, complain);
4791 else
4793 if (TREE_TYPE (op0) != real_type)
4794 op0 = cp_convert_and_check (real_type, op0, complain);
4795 if (TREE_TYPE (op1) != result_type)
4796 op1 = cp_convert_and_check (result_type, op1, complain);
4798 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4799 return error_mark_node;
4800 if (first_complex)
4802 op0 = save_expr (op0);
4803 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4804 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4805 switch (code)
4807 case MULT_EXPR:
4808 case TRUNC_DIV_EXPR:
4809 op1 = save_expr (op1);
4810 imag = build2 (resultcode, real_type, imag, op1);
4811 /* Fall through. */
4812 case PLUS_EXPR:
4813 case MINUS_EXPR:
4814 real = build2 (resultcode, real_type, real, op1);
4815 break;
4816 default:
4817 gcc_unreachable();
4820 else
4822 op1 = save_expr (op1);
4823 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4824 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4825 switch (code)
4827 case MULT_EXPR:
4828 op0 = save_expr (op0);
4829 imag = build2 (resultcode, real_type, op0, imag);
4830 /* Fall through. */
4831 case PLUS_EXPR:
4832 real = build2 (resultcode, real_type, op0, real);
4833 break;
4834 case MINUS_EXPR:
4835 real = build2 (resultcode, real_type, op0, real);
4836 imag = build1 (NEGATE_EXPR, real_type, imag);
4837 break;
4838 default:
4839 gcc_unreachable();
4842 real = fold_if_not_in_template (real);
4843 imag = fold_if_not_in_template (imag);
4844 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4845 result = fold_if_not_in_template (result);
4846 return result;
4849 /* For certain operations (which identify themselves by shorten != 0)
4850 if both args were extended from the same smaller type,
4851 do the arithmetic in that type and then extend.
4853 shorten !=0 and !=1 indicates a bitwise operation.
4854 For them, this optimization is safe only if
4855 both args are zero-extended or both are sign-extended.
4856 Otherwise, we might change the result.
4857 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4858 but calculated in (unsigned short) it would be (unsigned short)-1. */
4860 if (shorten && none_complex)
4862 orig_type = result_type;
4863 final_type = result_type;
4864 result_type = shorten_binary_op (result_type, op0, op1,
4865 shorten == -1);
4868 /* Comparison operations are shortened too but differently.
4869 They identify themselves by setting short_compare = 1. */
4871 if (short_compare)
4873 /* Don't write &op0, etc., because that would prevent op0
4874 from being kept in a register.
4875 Instead, make copies of the our local variables and
4876 pass the copies by reference, then copy them back afterward. */
4877 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4878 enum tree_code xresultcode = resultcode;
4879 tree val
4880 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4881 &xresultcode);
4882 if (val != 0)
4883 return cp_convert (boolean_type_node, val, complain);
4884 op0 = xop0, op1 = xop1;
4885 converted = 1;
4886 resultcode = xresultcode;
4889 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4890 && warn_sign_compare
4891 /* Do not warn until the template is instantiated; we cannot
4892 bound the ranges of the arguments until that point. */
4893 && !processing_template_decl
4894 && (complain & tf_warning)
4895 && c_inhibit_evaluation_warnings == 0
4896 /* Even unsigned enum types promote to signed int. We don't
4897 want to issue -Wsign-compare warnings for this case. */
4898 && !enum_cast_to_int (orig_op0)
4899 && !enum_cast_to_int (orig_op1))
4901 tree oop0 = maybe_constant_value (orig_op0);
4902 tree oop1 = maybe_constant_value (orig_op1);
4904 if (TREE_CODE (oop0) != INTEGER_CST)
4905 oop0 = orig_op0;
4906 if (TREE_CODE (oop1) != INTEGER_CST)
4907 oop1 = orig_op1;
4908 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4909 result_type, resultcode);
4913 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4914 Then the expression will be built.
4915 It will be given type FINAL_TYPE if that is nonzero;
4916 otherwise, it will be given type RESULT_TYPE. */
4917 if (! converted)
4919 if (TREE_TYPE (op0) != result_type)
4920 op0 = cp_convert_and_check (result_type, op0, complain);
4921 if (TREE_TYPE (op1) != result_type)
4922 op1 = cp_convert_and_check (result_type, op1, complain);
4924 if (op0 == error_mark_node || op1 == error_mark_node)
4925 return error_mark_node;
4928 if (build_type == NULL_TREE)
4929 build_type = result_type;
4931 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
4932 | SANITIZE_FLOAT_DIVIDE))
4933 && !processing_template_decl
4934 && current_function_decl != 0
4935 && !lookup_attribute ("no_sanitize_undefined",
4936 DECL_ATTRIBUTES (current_function_decl))
4937 && (doing_div_or_mod || doing_shift))
4939 /* OP0 and/or OP1 might have side-effects. */
4940 op0 = cp_save_expr (op0);
4941 op1 = cp_save_expr (op1);
4942 op0 = maybe_constant_value (fold_non_dependent_expr_sfinae (op0,
4943 tf_none));
4944 op1 = maybe_constant_value (fold_non_dependent_expr_sfinae (op1,
4945 tf_none));
4946 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
4947 | SANITIZE_FLOAT_DIVIDE)))
4949 /* For diagnostics we want to use the promoted types without
4950 shorten_binary_op. So convert the arguments to the
4951 original result_type. */
4952 tree cop0 = op0;
4953 tree cop1 = op1;
4954 if (orig_type != NULL && result_type != orig_type)
4956 cop0 = cp_convert (orig_type, op0, complain);
4957 cop1 = cp_convert (orig_type, op1, complain);
4959 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
4961 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
4962 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
4965 result = build2 (resultcode, build_type, op0, op1);
4966 result = fold_if_not_in_template (result);
4967 if (final_type != 0)
4968 result = cp_convert (final_type, result, complain);
4970 if (TREE_OVERFLOW_P (result)
4971 && !TREE_OVERFLOW_P (op0)
4972 && !TREE_OVERFLOW_P (op1))
4973 overflow_warning (location, result);
4975 if (instrument_expr != NULL)
4976 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
4977 instrument_expr, result);
4979 return result;
4982 /* Build a VEC_PERM_EXPR.
4983 This is a simple wrapper for c_build_vec_perm_expr. */
4984 tree
4985 build_x_vec_perm_expr (location_t loc,
4986 tree arg0, tree arg1, tree arg2,
4987 tsubst_flags_t complain)
4989 tree orig_arg0 = arg0;
4990 tree orig_arg1 = arg1;
4991 tree orig_arg2 = arg2;
4992 if (processing_template_decl)
4994 if (type_dependent_expression_p (arg0)
4995 || type_dependent_expression_p (arg1)
4996 || type_dependent_expression_p (arg2))
4997 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
4998 arg0 = build_non_dependent_expr (arg0);
4999 if (arg1)
5000 arg1 = build_non_dependent_expr (arg1);
5001 arg2 = build_non_dependent_expr (arg2);
5003 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5004 if (processing_template_decl && exp != error_mark_node)
5005 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5006 orig_arg1, orig_arg2);
5007 return exp;
5010 /* Return a tree for the sum or difference (RESULTCODE says which)
5011 of pointer PTROP and integer INTOP. */
5013 static tree
5014 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5015 tsubst_flags_t complain)
5017 tree res_type = TREE_TYPE (ptrop);
5019 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5020 in certain circumstance (when it's valid to do so). So we need
5021 to make sure it's complete. We don't need to check here, if we
5022 can actually complete it at all, as those checks will be done in
5023 pointer_int_sum() anyway. */
5024 complete_type (TREE_TYPE (res_type));
5026 return pointer_int_sum (input_location, resultcode, ptrop,
5027 fold_if_not_in_template (intop),
5028 complain & tf_warning_or_error);
5031 /* Return a tree for the difference of pointers OP0 and OP1.
5032 The resulting tree has type int. */
5034 static tree
5035 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5037 tree result;
5038 tree restype = ptrdiff_type_node;
5039 tree target_type = TREE_TYPE (ptrtype);
5041 if (!complete_type_or_else (target_type, NULL_TREE))
5042 return error_mark_node;
5044 if (VOID_TYPE_P (target_type))
5046 if (complain & tf_error)
5047 permerror (input_location, "ISO C++ forbids using pointer of "
5048 "type %<void *%> in subtraction");
5049 else
5050 return error_mark_node;
5052 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5054 if (complain & tf_error)
5055 permerror (input_location, "ISO C++ forbids using pointer to "
5056 "a function in subtraction");
5057 else
5058 return error_mark_node;
5060 if (TREE_CODE (target_type) == METHOD_TYPE)
5062 if (complain & tf_error)
5063 permerror (input_location, "ISO C++ forbids using pointer to "
5064 "a method in subtraction");
5065 else
5066 return error_mark_node;
5069 /* First do the subtraction as integers;
5070 then drop through to build the divide operator. */
5072 op0 = cp_build_binary_op (input_location,
5073 MINUS_EXPR,
5074 cp_convert (restype, op0, complain),
5075 cp_convert (restype, op1, complain),
5076 complain);
5078 /* This generates an error if op1 is a pointer to an incomplete type. */
5079 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5081 if (complain & tf_error)
5082 error ("invalid use of a pointer to an incomplete type in "
5083 "pointer arithmetic");
5084 else
5085 return error_mark_node;
5088 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5090 if (complain & tf_error)
5091 error ("arithmetic on pointer to an empty aggregate");
5092 else
5093 return error_mark_node;
5096 op1 = (TYPE_PTROB_P (ptrtype)
5097 ? size_in_bytes (target_type)
5098 : integer_one_node);
5100 /* Do the division. */
5102 result = build2 (EXACT_DIV_EXPR, restype, op0,
5103 cp_convert (restype, op1, complain));
5104 return fold_if_not_in_template (result);
5107 /* Construct and perhaps optimize a tree representation
5108 for a unary operation. CODE, a tree_code, specifies the operation
5109 and XARG is the operand. */
5111 tree
5112 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5113 tsubst_flags_t complain)
5115 tree orig_expr = xarg;
5116 tree exp;
5117 int ptrmem = 0;
5119 if (processing_template_decl)
5121 if (type_dependent_expression_p (xarg))
5122 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5124 xarg = build_non_dependent_expr (xarg);
5127 exp = NULL_TREE;
5129 /* [expr.unary.op] says:
5131 The address of an object of incomplete type can be taken.
5133 (And is just the ordinary address operator, not an overloaded
5134 "operator &".) However, if the type is a template
5135 specialization, we must complete the type at this point so that
5136 an overloaded "operator &" will be available if required. */
5137 if (code == ADDR_EXPR
5138 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5139 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5140 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5141 || (TREE_CODE (xarg) == OFFSET_REF)))
5142 /* Don't look for a function. */;
5143 else
5144 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5145 NULL_TREE, /*overload=*/NULL, complain);
5146 if (!exp && code == ADDR_EXPR)
5148 if (is_overloaded_fn (xarg))
5150 tree fn = get_first_fn (xarg);
5151 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5153 if (complain & tf_error)
5154 error (DECL_CONSTRUCTOR_P (fn)
5155 ? G_("taking address of constructor %qE")
5156 : G_("taking address of destructor %qE"),
5157 xarg);
5158 return error_mark_node;
5162 /* A pointer to member-function can be formed only by saying
5163 &X::mf. */
5164 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5165 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5167 if (TREE_CODE (xarg) != OFFSET_REF
5168 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5170 if (complain & tf_error)
5172 error ("invalid use of %qE to form a "
5173 "pointer-to-member-function", xarg);
5174 if (TREE_CODE (xarg) != OFFSET_REF)
5175 inform (input_location, " a qualified-id is required");
5177 return error_mark_node;
5179 else
5181 if (complain & tf_error)
5182 error ("parentheses around %qE cannot be used to form a"
5183 " pointer-to-member-function",
5184 xarg);
5185 else
5186 return error_mark_node;
5187 PTRMEM_OK_P (xarg) = 1;
5191 if (TREE_CODE (xarg) == OFFSET_REF)
5193 ptrmem = PTRMEM_OK_P (xarg);
5195 if (!ptrmem && !flag_ms_extensions
5196 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5198 /* A single non-static member, make sure we don't allow a
5199 pointer-to-member. */
5200 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5201 TREE_OPERAND (xarg, 0),
5202 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5203 PTRMEM_OK_P (xarg) = ptrmem;
5207 exp = cp_build_addr_expr_strict (xarg, complain);
5210 if (processing_template_decl && exp != error_mark_node)
5211 exp = build_min_non_dep (code, exp, orig_expr,
5212 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5213 if (TREE_CODE (exp) == ADDR_EXPR)
5214 PTRMEM_OK_P (exp) = ptrmem;
5215 return exp;
5218 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5219 constants, where a null value is represented by an INTEGER_CST of
5220 -1. */
5222 tree
5223 cp_truthvalue_conversion (tree expr)
5225 tree type = TREE_TYPE (expr);
5226 if (TYPE_PTRDATAMEM_P (type)
5227 /* Avoid ICE on invalid use of non-static member function. */
5228 || TREE_CODE (expr) == FUNCTION_DECL)
5229 return build_binary_op (EXPR_LOCATION (expr),
5230 NE_EXPR, expr, nullptr_node, 1);
5231 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5233 /* With -Wzero-as-null-pointer-constant do not warn for an
5234 'if (p)' or a 'while (!p)', where p is a pointer. */
5235 tree ret;
5236 ++c_inhibit_evaluation_warnings;
5237 ret = c_common_truthvalue_conversion (input_location, expr);
5238 --c_inhibit_evaluation_warnings;
5239 return ret;
5241 else
5242 return c_common_truthvalue_conversion (input_location, expr);
5245 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5247 tree
5248 condition_conversion (tree expr)
5250 tree t;
5251 if (processing_template_decl)
5252 return expr;
5253 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5254 tf_warning_or_error, LOOKUP_NORMAL);
5255 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5256 return t;
5259 /* Returns the address of T. This function will fold away
5260 ADDR_EXPR of INDIRECT_REF. */
5262 tree
5263 build_address (tree t)
5265 if (error_operand_p (t) || !cxx_mark_addressable (t))
5266 return error_mark_node;
5267 t = build_fold_addr_expr (t);
5268 if (TREE_CODE (t) != ADDR_EXPR)
5269 t = rvalue (t);
5270 return t;
5273 /* Returns the address of T with type TYPE. */
5275 tree
5276 build_typed_address (tree t, tree type)
5278 if (error_operand_p (t) || !cxx_mark_addressable (t))
5279 return error_mark_node;
5280 t = build_fold_addr_expr_with_type (t, type);
5281 if (TREE_CODE (t) != ADDR_EXPR)
5282 t = rvalue (t);
5283 return t;
5286 /* Return a NOP_EXPR converting EXPR to TYPE. */
5288 tree
5289 build_nop (tree type, tree expr)
5291 if (type == error_mark_node || error_operand_p (expr))
5292 return expr;
5293 return build1 (NOP_EXPR, type, expr);
5296 /* Take the address of ARG, whatever that means under C++ semantics.
5297 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5298 and class rvalues as well.
5300 Nothing should call this function directly; instead, callers should use
5301 cp_build_addr_expr or cp_build_addr_expr_strict. */
5303 static tree
5304 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5306 tree argtype;
5307 tree val;
5309 if (!arg || error_operand_p (arg))
5310 return error_mark_node;
5312 arg = mark_lvalue_use (arg);
5313 argtype = lvalue_type (arg);
5315 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5317 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5318 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5320 /* They're trying to take the address of a unique non-static
5321 member function. This is ill-formed (except in MS-land),
5322 but let's try to DTRT.
5323 Note: We only handle unique functions here because we don't
5324 want to complain if there's a static overload; non-unique
5325 cases will be handled by instantiate_type. But we need to
5326 handle this case here to allow casts on the resulting PMF.
5327 We could defer this in non-MS mode, but it's easier to give
5328 a useful error here. */
5330 /* Inside constant member functions, the `this' pointer
5331 contains an extra const qualifier. TYPE_MAIN_VARIANT
5332 is used here to remove this const from the diagnostics
5333 and the created OFFSET_REF. */
5334 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5335 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5336 mark_used (fn);
5338 if (! flag_ms_extensions)
5340 tree name = DECL_NAME (fn);
5341 if (!(complain & tf_error))
5342 return error_mark_node;
5343 else if (current_class_type
5344 && TREE_OPERAND (arg, 0) == current_class_ref)
5345 /* An expression like &memfn. */
5346 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5347 " or parenthesized non-static member function to form"
5348 " a pointer to member function. Say %<&%T::%D%>",
5349 base, name);
5350 else
5351 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5352 " function to form a pointer to member function."
5353 " Say %<&%T::%D%>",
5354 base, name);
5356 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5359 /* Uninstantiated types are all functions. Taking the
5360 address of a function is a no-op, so just return the
5361 argument. */
5362 if (type_unknown_p (arg))
5363 return build1 (ADDR_EXPR, unknown_type_node, arg);
5365 if (TREE_CODE (arg) == OFFSET_REF)
5366 /* We want a pointer to member; bypass all the code for actually taking
5367 the address of something. */
5368 goto offset_ref;
5370 /* Anything not already handled and not a true memory reference
5371 is an error. */
5372 if (TREE_CODE (argtype) != FUNCTION_TYPE
5373 && TREE_CODE (argtype) != METHOD_TYPE)
5375 cp_lvalue_kind kind = lvalue_kind (arg);
5376 if (kind == clk_none)
5378 if (complain & tf_error)
5379 lvalue_error (input_location, lv_addressof);
5380 return error_mark_node;
5382 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5384 if (!(complain & tf_error))
5385 return error_mark_node;
5386 if (kind & clk_class)
5387 /* Make this a permerror because we used to accept it. */
5388 permerror (input_location, "taking address of temporary");
5389 else
5390 error ("taking address of xvalue (rvalue reference)");
5394 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5396 tree type = build_pointer_type (TREE_TYPE (argtype));
5397 arg = build1 (CONVERT_EXPR, type, arg);
5398 return arg;
5400 else if (pedantic && DECL_MAIN_P (arg))
5402 /* ARM $3.4 */
5403 /* Apparently a lot of autoconf scripts for C++ packages do this,
5404 so only complain if -Wpedantic. */
5405 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5406 pedwarn (input_location, OPT_Wpedantic,
5407 "ISO C++ forbids taking address of function %<::main%>");
5408 else if (flag_pedantic_errors)
5409 return error_mark_node;
5412 /* Let &* cancel out to simplify resulting code. */
5413 if (INDIRECT_REF_P (arg))
5415 /* We don't need to have `current_class_ptr' wrapped in a
5416 NON_LVALUE_EXPR node. */
5417 if (arg == current_class_ref)
5418 return current_class_ptr;
5420 arg = TREE_OPERAND (arg, 0);
5421 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5423 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5424 arg = build1 (CONVERT_EXPR, type, arg);
5426 else
5427 /* Don't let this be an lvalue. */
5428 arg = rvalue (arg);
5429 return arg;
5432 /* ??? Cope with user tricks that amount to offsetof. */
5433 if (TREE_CODE (argtype) != FUNCTION_TYPE
5434 && TREE_CODE (argtype) != METHOD_TYPE
5435 && argtype != unknown_type_node
5436 && (val = get_base_address (arg))
5437 && COMPLETE_TYPE_P (TREE_TYPE (val))
5438 && INDIRECT_REF_P (val)
5439 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5441 tree type = build_pointer_type (argtype);
5442 return fold_convert (type, fold_offsetof_1 (arg));
5445 /* Handle complex lvalues (when permitted)
5446 by reduction to simpler cases. */
5447 val = unary_complex_lvalue (ADDR_EXPR, arg);
5448 if (val != 0)
5449 return val;
5451 switch (TREE_CODE (arg))
5453 CASE_CONVERT:
5454 case FLOAT_EXPR:
5455 case FIX_TRUNC_EXPR:
5456 /* Even if we're not being pedantic, we cannot allow this
5457 extension when we're instantiating in a SFINAE
5458 context. */
5459 if (! lvalue_p (arg) && complain == tf_none)
5461 if (complain & tf_error)
5462 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5463 else
5464 return error_mark_node;
5466 break;
5468 case BASELINK:
5469 arg = BASELINK_FUNCTIONS (arg);
5470 /* Fall through. */
5472 case OVERLOAD:
5473 arg = OVL_CURRENT (arg);
5474 break;
5476 case OFFSET_REF:
5477 offset_ref:
5478 /* Turn a reference to a non-static data member into a
5479 pointer-to-member. */
5481 tree type;
5482 tree t;
5484 gcc_assert (PTRMEM_OK_P (arg));
5486 t = TREE_OPERAND (arg, 1);
5487 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5489 if (complain & tf_error)
5490 error ("cannot create pointer to reference member %qD", t);
5491 return error_mark_node;
5494 type = build_ptrmem_type (context_for_name_lookup (t),
5495 TREE_TYPE (t));
5496 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5497 return t;
5500 default:
5501 break;
5504 if (argtype != error_mark_node)
5506 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (argtype)
5507 && (flag_iso || warn_vla > 0))
5509 if (complain & tf_warning_or_error)
5510 pedwarn (input_location, OPT_Wvla,
5511 "taking address of array of runtime bound");
5512 else
5513 return error_mark_node;
5515 argtype = build_pointer_type (argtype);
5518 /* In a template, we are processing a non-dependent expression
5519 so we can just form an ADDR_EXPR with the correct type. */
5520 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5522 val = build_address (arg);
5523 if (TREE_CODE (arg) == OFFSET_REF)
5524 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5526 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5528 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5530 /* We can only get here with a single static member
5531 function. */
5532 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5533 && DECL_STATIC_FUNCTION_P (fn));
5534 mark_used (fn);
5535 val = build_address (fn);
5536 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5537 /* Do not lose object's side effects. */
5538 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5539 TREE_OPERAND (arg, 0), val);
5541 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5543 if (complain & tf_error)
5544 error ("attempt to take address of bit-field structure member %qD",
5545 TREE_OPERAND (arg, 1));
5546 return error_mark_node;
5548 else
5550 tree object = TREE_OPERAND (arg, 0);
5551 tree field = TREE_OPERAND (arg, 1);
5552 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5553 (TREE_TYPE (object), decl_type_context (field)));
5554 val = build_address (arg);
5557 if (TYPE_PTR_P (argtype)
5558 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5560 build_ptrmemfunc_type (argtype);
5561 val = build_ptrmemfunc (argtype, val, 0,
5562 /*c_cast_p=*/false,
5563 complain);
5566 return val;
5569 /* Take the address of ARG if it has one, even if it's an rvalue. */
5571 tree
5572 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5574 return cp_build_addr_expr_1 (arg, 0, complain);
5577 /* Take the address of ARG, but only if it's an lvalue. */
5579 tree
5580 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5582 return cp_build_addr_expr_1 (arg, 1, complain);
5585 /* C++: Must handle pointers to members.
5587 Perhaps type instantiation should be extended to handle conversion
5588 from aggregates to types we don't yet know we want? (Or are those
5589 cases typically errors which should be reported?)
5591 NOCONVERT nonzero suppresses the default promotions
5592 (such as from short to int). */
5594 tree
5595 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5596 tsubst_flags_t complain)
5598 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5599 tree arg = xarg;
5600 tree argtype = 0;
5601 const char *errstring = NULL;
5602 tree val;
5603 const char *invalid_op_diag;
5605 if (!arg || error_operand_p (arg))
5606 return error_mark_node;
5608 if ((invalid_op_diag
5609 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5610 ? CONVERT_EXPR
5611 : code),
5612 TREE_TYPE (xarg))))
5614 if (complain & tf_error)
5615 error (invalid_op_diag);
5616 return error_mark_node;
5619 switch (code)
5621 case UNARY_PLUS_EXPR:
5622 case NEGATE_EXPR:
5624 int flags = WANT_ARITH | WANT_ENUM;
5625 /* Unary plus (but not unary minus) is allowed on pointers. */
5626 if (code == UNARY_PLUS_EXPR)
5627 flags |= WANT_POINTER;
5628 arg = build_expr_type_conversion (flags, arg, true);
5629 if (!arg)
5630 errstring = (code == NEGATE_EXPR
5631 ? _("wrong type argument to unary minus")
5632 : _("wrong type argument to unary plus"));
5633 else
5635 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5636 arg = cp_perform_integral_promotions (arg, complain);
5638 /* Make sure the result is not an lvalue: a unary plus or minus
5639 expression is always a rvalue. */
5640 arg = rvalue (arg);
5643 break;
5645 case BIT_NOT_EXPR:
5646 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5648 code = CONJ_EXPR;
5649 if (!noconvert)
5651 arg = cp_default_conversion (arg, complain);
5652 if (arg == error_mark_node)
5653 return error_mark_node;
5656 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5657 | WANT_VECTOR_OR_COMPLEX,
5658 arg, true)))
5659 errstring = _("wrong type argument to bit-complement");
5660 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5661 arg = cp_perform_integral_promotions (arg, complain);
5662 break;
5664 case ABS_EXPR:
5665 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5666 errstring = _("wrong type argument to abs");
5667 else if (!noconvert)
5669 arg = cp_default_conversion (arg, complain);
5670 if (arg == error_mark_node)
5671 return error_mark_node;
5673 break;
5675 case CONJ_EXPR:
5676 /* Conjugating a real value is a no-op, but allow it anyway. */
5677 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5678 errstring = _("wrong type argument to conjugation");
5679 else if (!noconvert)
5681 arg = cp_default_conversion (arg, complain);
5682 if (arg == error_mark_node)
5683 return error_mark_node;
5685 break;
5687 case TRUTH_NOT_EXPR:
5688 arg = perform_implicit_conversion (boolean_type_node, arg,
5689 complain);
5690 val = invert_truthvalue_loc (input_location, arg);
5691 if (arg != error_mark_node)
5692 return val;
5693 errstring = _("in argument to unary !");
5694 break;
5696 case NOP_EXPR:
5697 break;
5699 case REALPART_EXPR:
5700 case IMAGPART_EXPR:
5701 arg = build_real_imag_expr (input_location, code, arg);
5702 if (arg == error_mark_node)
5703 return arg;
5704 else
5705 return fold_if_not_in_template (arg);
5707 case PREINCREMENT_EXPR:
5708 case POSTINCREMENT_EXPR:
5709 case PREDECREMENT_EXPR:
5710 case POSTDECREMENT_EXPR:
5711 /* Handle complex lvalues (when permitted)
5712 by reduction to simpler cases. */
5714 val = unary_complex_lvalue (code, arg);
5715 if (val != 0)
5716 return val;
5718 arg = mark_lvalue_use (arg);
5720 /* Increment or decrement the real part of the value,
5721 and don't change the imaginary part. */
5722 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5724 tree real, imag;
5726 arg = stabilize_reference (arg);
5727 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5728 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5729 real = cp_build_unary_op (code, real, 1, complain);
5730 if (real == error_mark_node || imag == error_mark_node)
5731 return error_mark_node;
5732 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5733 real, imag);
5736 /* Report invalid types. */
5738 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5739 arg, true)))
5741 if (code == PREINCREMENT_EXPR)
5742 errstring = _("no pre-increment operator for type");
5743 else if (code == POSTINCREMENT_EXPR)
5744 errstring = _("no post-increment operator for type");
5745 else if (code == PREDECREMENT_EXPR)
5746 errstring = _("no pre-decrement operator for type");
5747 else
5748 errstring = _("no post-decrement operator for type");
5749 break;
5751 else if (arg == error_mark_node)
5752 return error_mark_node;
5754 /* Report something read-only. */
5756 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5757 || TREE_READONLY (arg))
5759 if (complain & tf_error)
5760 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5761 || code == POSTINCREMENT_EXPR)
5762 ? lv_increment : lv_decrement));
5763 else
5764 return error_mark_node;
5768 tree inc;
5769 tree declared_type = unlowered_expr_type (arg);
5771 argtype = TREE_TYPE (arg);
5773 /* ARM $5.2.5 last annotation says this should be forbidden. */
5774 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5776 if (complain & tf_error)
5777 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5778 ? G_("ISO C++ forbids incrementing an enum")
5779 : G_("ISO C++ forbids decrementing an enum"));
5780 else
5781 return error_mark_node;
5784 /* Compute the increment. */
5786 if (TYPE_PTR_P (argtype))
5788 tree type = complete_type (TREE_TYPE (argtype));
5790 if (!COMPLETE_OR_VOID_TYPE_P (type))
5792 if (complain & tf_error)
5793 error (((code == PREINCREMENT_EXPR
5794 || code == POSTINCREMENT_EXPR))
5795 ? G_("cannot increment a pointer to incomplete type %qT")
5796 : G_("cannot decrement a pointer to incomplete type %qT"),
5797 TREE_TYPE (argtype));
5798 else
5799 return error_mark_node;
5801 else if (!TYPE_PTROB_P (argtype))
5803 if (complain & tf_error)
5804 pedwarn (input_location, OPT_Wpointer_arith,
5805 (code == PREINCREMENT_EXPR
5806 || code == POSTINCREMENT_EXPR)
5807 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5808 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5809 argtype);
5810 else
5811 return error_mark_node;
5814 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5816 else
5817 inc = VECTOR_TYPE_P (argtype)
5818 ? build_one_cst (argtype)
5819 : integer_one_node;
5821 inc = cp_convert (argtype, inc, complain);
5823 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5824 need to ask Objective-C to build the increment or decrement
5825 expression for it. */
5826 if (objc_is_property_ref (arg))
5827 return objc_build_incr_expr_for_property_ref (input_location, code,
5828 arg, inc);
5830 /* Complain about anything else that is not a true lvalue. */
5831 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5832 || code == POSTINCREMENT_EXPR)
5833 ? lv_increment : lv_decrement),
5834 complain))
5835 return error_mark_node;
5837 /* Forbid using -- on `bool'. */
5838 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5840 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5842 if (complain & tf_error)
5843 error ("invalid use of Boolean expression as operand "
5844 "to %<operator--%>");
5845 return error_mark_node;
5847 val = boolean_increment (code, arg);
5849 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5850 /* An rvalue has no cv-qualifiers. */
5851 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5852 else
5853 val = build2 (code, TREE_TYPE (arg), arg, inc);
5855 TREE_SIDE_EFFECTS (val) = 1;
5856 return val;
5859 case ADDR_EXPR:
5860 /* Note that this operation never does default_conversion
5861 regardless of NOCONVERT. */
5862 return cp_build_addr_expr (arg, complain);
5864 default:
5865 break;
5868 if (!errstring)
5870 if (argtype == 0)
5871 argtype = TREE_TYPE (arg);
5872 return fold_if_not_in_template (build1 (code, argtype, arg));
5875 if (complain & tf_error)
5876 error ("%s", errstring);
5877 return error_mark_node;
5880 /* Hook for the c-common bits that build a unary op. */
5881 tree
5882 build_unary_op (location_t /*location*/,
5883 enum tree_code code, tree xarg, int noconvert)
5885 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5888 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5889 for certain kinds of expressions which are not really lvalues
5890 but which we can accept as lvalues.
5892 If ARG is not a kind of expression we can handle, return
5893 NULL_TREE. */
5895 tree
5896 unary_complex_lvalue (enum tree_code code, tree arg)
5898 /* Inside a template, making these kinds of adjustments is
5899 pointless; we are only concerned with the type of the
5900 expression. */
5901 if (processing_template_decl)
5902 return NULL_TREE;
5904 /* Handle (a, b) used as an "lvalue". */
5905 if (TREE_CODE (arg) == COMPOUND_EXPR)
5907 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5908 tf_warning_or_error);
5909 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5910 TREE_OPERAND (arg, 0), real_result);
5913 /* Handle (a ? b : c) used as an "lvalue". */
5914 if (TREE_CODE (arg) == COND_EXPR
5915 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5916 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5918 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5919 if (TREE_CODE (arg) == MODIFY_EXPR
5920 || TREE_CODE (arg) == PREINCREMENT_EXPR
5921 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5923 tree lvalue = TREE_OPERAND (arg, 0);
5924 if (TREE_SIDE_EFFECTS (lvalue))
5926 lvalue = stabilize_reference (lvalue);
5927 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5928 lvalue, TREE_OPERAND (arg, 1));
5930 return unary_complex_lvalue
5931 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5934 if (code != ADDR_EXPR)
5935 return NULL_TREE;
5937 /* Handle (a = b) used as an "lvalue" for `&'. */
5938 if (TREE_CODE (arg) == MODIFY_EXPR
5939 || TREE_CODE (arg) == INIT_EXPR)
5941 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5942 tf_warning_or_error);
5943 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5944 arg, real_result);
5945 TREE_NO_WARNING (arg) = 1;
5946 return arg;
5949 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5950 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5951 || TREE_CODE (arg) == OFFSET_REF)
5952 return NULL_TREE;
5954 /* We permit compiler to make function calls returning
5955 objects of aggregate type look like lvalues. */
5957 tree targ = arg;
5959 if (TREE_CODE (targ) == SAVE_EXPR)
5960 targ = TREE_OPERAND (targ, 0);
5962 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5964 if (TREE_CODE (arg) == SAVE_EXPR)
5965 targ = arg;
5966 else
5967 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5968 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5971 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
5972 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5973 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5976 /* Don't let anything else be handled specially. */
5977 return NULL_TREE;
5980 /* Mark EXP saying that we need to be able to take the
5981 address of it; it should not be allocated in a register.
5982 Value is true if successful.
5984 C++: we do not allow `current_class_ptr' to be addressable. */
5986 bool
5987 cxx_mark_addressable (tree exp)
5989 tree x = exp;
5991 while (1)
5992 switch (TREE_CODE (x))
5994 case ADDR_EXPR:
5995 case COMPONENT_REF:
5996 case ARRAY_REF:
5997 case REALPART_EXPR:
5998 case IMAGPART_EXPR:
5999 x = TREE_OPERAND (x, 0);
6000 break;
6002 case PARM_DECL:
6003 if (x == current_class_ptr)
6005 error ("cannot take the address of %<this%>, which is an rvalue expression");
6006 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6007 return true;
6009 /* Fall through. */
6011 case VAR_DECL:
6012 /* Caller should not be trying to mark initialized
6013 constant fields addressable. */
6014 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6015 || DECL_IN_AGGR_P (x) == 0
6016 || TREE_STATIC (x)
6017 || DECL_EXTERNAL (x));
6018 /* Fall through. */
6020 case RESULT_DECL:
6021 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6022 && !DECL_ARTIFICIAL (x))
6024 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6026 error
6027 ("address of explicit register variable %qD requested", x);
6028 return false;
6030 else if (extra_warnings)
6031 warning
6032 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6034 TREE_ADDRESSABLE (x) = 1;
6035 return true;
6037 case CONST_DECL:
6038 case FUNCTION_DECL:
6039 TREE_ADDRESSABLE (x) = 1;
6040 return true;
6042 case CONSTRUCTOR:
6043 TREE_ADDRESSABLE (x) = 1;
6044 return true;
6046 case TARGET_EXPR:
6047 TREE_ADDRESSABLE (x) = 1;
6048 cxx_mark_addressable (TREE_OPERAND (x, 0));
6049 return true;
6051 default:
6052 return true;
6056 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6058 tree
6059 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6060 tsubst_flags_t complain)
6062 tree orig_ifexp = ifexp;
6063 tree orig_op1 = op1;
6064 tree orig_op2 = op2;
6065 tree expr;
6067 if (processing_template_decl)
6069 /* The standard says that the expression is type-dependent if
6070 IFEXP is type-dependent, even though the eventual type of the
6071 expression doesn't dependent on IFEXP. */
6072 if (type_dependent_expression_p (ifexp)
6073 /* As a GNU extension, the middle operand may be omitted. */
6074 || (op1 && type_dependent_expression_p (op1))
6075 || type_dependent_expression_p (op2))
6076 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6077 ifexp = build_non_dependent_expr (ifexp);
6078 if (op1)
6079 op1 = build_non_dependent_expr (op1);
6080 op2 = build_non_dependent_expr (op2);
6083 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6084 if (processing_template_decl && expr != error_mark_node
6085 && TREE_CODE (expr) != VEC_COND_EXPR)
6087 tree min = build_min_non_dep (COND_EXPR, expr,
6088 orig_ifexp, orig_op1, orig_op2);
6089 /* In C++11, remember that the result is an lvalue or xvalue.
6090 In C++98, lvalue_kind can just assume lvalue in a template. */
6091 if (cxx_dialect >= cxx11
6092 && lvalue_or_rvalue_with_address_p (expr)
6093 && !lvalue_or_rvalue_with_address_p (min))
6094 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6095 !real_lvalue_p (expr));
6096 expr = convert_from_reference (min);
6098 return expr;
6101 /* Given a list of expressions, return a compound expression
6102 that performs them all and returns the value of the last of them. */
6104 tree
6105 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6106 tsubst_flags_t complain)
6108 tree expr = TREE_VALUE (list);
6110 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6111 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6113 if (complain & tf_error)
6114 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6115 "list-initializer for non-class type must not "
6116 "be parenthesized");
6117 else
6118 return error_mark_node;
6121 if (TREE_CHAIN (list))
6123 if (complain & tf_error)
6124 switch (exp)
6126 case ELK_INIT:
6127 permerror (input_location, "expression list treated as compound "
6128 "expression in initializer");
6129 break;
6130 case ELK_MEM_INIT:
6131 permerror (input_location, "expression list treated as compound "
6132 "expression in mem-initializer");
6133 break;
6134 case ELK_FUNC_CAST:
6135 permerror (input_location, "expression list treated as compound "
6136 "expression in functional cast");
6137 break;
6138 default:
6139 gcc_unreachable ();
6141 else
6142 return error_mark_node;
6144 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6145 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6146 expr, TREE_VALUE (list), complain);
6149 return expr;
6152 /* Like build_x_compound_expr_from_list, but using a VEC. */
6154 tree
6155 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6156 tsubst_flags_t complain)
6158 if (vec_safe_is_empty (vec))
6159 return NULL_TREE;
6160 else if (vec->length () == 1)
6161 return (*vec)[0];
6162 else
6164 tree expr;
6165 unsigned int ix;
6166 tree t;
6168 if (msg != NULL)
6170 if (complain & tf_error)
6171 permerror (input_location,
6172 "%s expression list treated as compound expression",
6173 msg);
6174 else
6175 return error_mark_node;
6178 expr = (*vec)[0];
6179 for (ix = 1; vec->iterate (ix, &t); ++ix)
6180 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6181 t, complain);
6183 return expr;
6187 /* Handle overloading of the ',' operator when needed. */
6189 tree
6190 build_x_compound_expr (location_t loc, tree op1, tree op2,
6191 tsubst_flags_t complain)
6193 tree result;
6194 tree orig_op1 = op1;
6195 tree orig_op2 = op2;
6197 if (processing_template_decl)
6199 if (type_dependent_expression_p (op1)
6200 || type_dependent_expression_p (op2))
6201 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6202 op1 = build_non_dependent_expr (op1);
6203 op2 = build_non_dependent_expr (op2);
6206 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6207 NULL_TREE, /*overload=*/NULL, complain);
6208 if (!result)
6209 result = cp_build_compound_expr (op1, op2, complain);
6211 if (processing_template_decl && result != error_mark_node)
6212 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6214 return result;
6217 /* Like cp_build_compound_expr, but for the c-common bits. */
6219 tree
6220 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6222 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6225 /* Build a compound expression. */
6227 tree
6228 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6230 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6232 if (lhs == error_mark_node || rhs == error_mark_node)
6233 return error_mark_node;
6235 if (flag_cilkplus
6236 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6237 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6239 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6240 : EXPR_LOCATION (rhs));
6241 error_at (loc,
6242 "spawned function call cannot be part of a comma expression");
6243 return error_mark_node;
6246 if (TREE_CODE (rhs) == TARGET_EXPR)
6248 /* If the rhs is a TARGET_EXPR, then build the compound
6249 expression inside the target_expr's initializer. This
6250 helps the compiler to eliminate unnecessary temporaries. */
6251 tree init = TREE_OPERAND (rhs, 1);
6253 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6254 TREE_OPERAND (rhs, 1) = init;
6256 return rhs;
6259 if (type_unknown_p (rhs))
6261 if (complain & tf_error)
6262 error ("no context to resolve type of %qE", rhs);
6263 return error_mark_node;
6266 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6269 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6270 casts away constness. CAST gives the type of cast. Returns true
6271 if the cast is ill-formed, false if it is well-formed.
6273 ??? This function warns for casting away any qualifier not just
6274 const. We would like to specify exactly what qualifiers are casted
6275 away.
6278 static bool
6279 check_for_casting_away_constness (tree src_type, tree dest_type,
6280 enum tree_code cast, tsubst_flags_t complain)
6282 /* C-style casts are allowed to cast away constness. With
6283 WARN_CAST_QUAL, we still want to issue a warning. */
6284 if (cast == CAST_EXPR && !warn_cast_qual)
6285 return false;
6287 if (!casts_away_constness (src_type, dest_type, complain))
6288 return false;
6290 switch (cast)
6292 case CAST_EXPR:
6293 if (complain & tf_warning)
6294 warning (OPT_Wcast_qual,
6295 "cast from type %qT to type %qT casts away qualifiers",
6296 src_type, dest_type);
6297 return false;
6299 case STATIC_CAST_EXPR:
6300 if (complain & tf_error)
6301 error ("static_cast from type %qT to type %qT casts away qualifiers",
6302 src_type, dest_type);
6303 return true;
6305 case REINTERPRET_CAST_EXPR:
6306 if (complain & tf_error)
6307 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6308 src_type, dest_type);
6309 return true;
6311 default:
6312 gcc_unreachable();
6317 Warns if the cast from expression EXPR to type TYPE is useless.
6319 void
6320 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6322 if (warn_useless_cast
6323 && complain & tf_warning)
6325 /* In C++14 mode, this interacts badly with force_paren_expr. And it
6326 isn't necessary in any mode, because the code below handles
6327 glvalues properly. For 4.9, just skip it in C++14 mode. */
6328 if (cxx_dialect < cxx14 && REFERENCE_REF_P (expr))
6329 expr = TREE_OPERAND (expr, 0);
6331 if ((TREE_CODE (type) == REFERENCE_TYPE
6332 && (TYPE_REF_IS_RVALUE (type)
6333 ? xvalue_p (expr) : real_lvalue_p (expr))
6334 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6335 || same_type_p (TREE_TYPE (expr), type))
6336 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6340 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6341 (another pointer-to-member type in the same hierarchy) and return
6342 the converted expression. If ALLOW_INVERSE_P is permitted, a
6343 pointer-to-derived may be converted to pointer-to-base; otherwise,
6344 only the other direction is permitted. If C_CAST_P is true, this
6345 conversion is taking place as part of a C-style cast. */
6347 tree
6348 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6349 bool c_cast_p, tsubst_flags_t complain)
6351 if (TYPE_PTRDATAMEM_P (type))
6353 tree delta;
6355 if (TREE_CODE (expr) == PTRMEM_CST)
6356 expr = cplus_expand_constant (expr);
6357 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6358 TYPE_PTRMEM_CLASS_TYPE (type),
6359 allow_inverse_p,
6360 c_cast_p, complain);
6361 if (delta == error_mark_node)
6362 return error_mark_node;
6364 if (!integer_zerop (delta))
6366 tree cond, op1, op2;
6368 cond = cp_build_binary_op (input_location,
6369 EQ_EXPR,
6370 expr,
6371 build_int_cst (TREE_TYPE (expr), -1),
6372 complain);
6373 op1 = build_nop (ptrdiff_type_node, expr);
6374 op2 = cp_build_binary_op (input_location,
6375 PLUS_EXPR, op1, delta,
6376 complain);
6378 expr = fold_build3_loc (input_location,
6379 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6383 return build_nop (type, expr);
6385 else
6386 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6387 allow_inverse_p, c_cast_p, complain);
6390 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6391 this static_cast is being attempted as one of the possible casts
6392 allowed by a C-style cast. (In that case, accessibility of base
6393 classes is not considered, and it is OK to cast away
6394 constness.) Return the result of the cast. *VALID_P is set to
6395 indicate whether or not the cast was valid. */
6397 static tree
6398 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6399 bool *valid_p, tsubst_flags_t complain)
6401 tree intype;
6402 tree result;
6403 cp_lvalue_kind clk;
6405 /* Assume the cast is valid. */
6406 *valid_p = true;
6408 intype = unlowered_expr_type (expr);
6410 /* Save casted types in the function's used types hash table. */
6411 used_types_insert (type);
6413 /* [expr.static.cast]
6415 An lvalue of type "cv1 B", where B is a class type, can be cast
6416 to type "reference to cv2 D", where D is a class derived (clause
6417 _class.derived_) from B, if a valid standard conversion from
6418 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6419 same cv-qualification as, or greater cv-qualification than, cv1,
6420 and B is not a virtual base class of D. */
6421 /* We check this case before checking the validity of "TYPE t =
6422 EXPR;" below because for this case:
6424 struct B {};
6425 struct D : public B { D(const B&); };
6426 extern B& b;
6427 void f() { static_cast<const D&>(b); }
6429 we want to avoid constructing a new D. The standard is not
6430 completely clear about this issue, but our interpretation is
6431 consistent with other compilers. */
6432 if (TREE_CODE (type) == REFERENCE_TYPE
6433 && CLASS_TYPE_P (TREE_TYPE (type))
6434 && CLASS_TYPE_P (intype)
6435 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6436 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6437 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6438 build_pointer_type (TYPE_MAIN_VARIANT
6439 (TREE_TYPE (type))),
6440 complain)
6441 && (c_cast_p
6442 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6444 tree base;
6446 /* There is a standard conversion from "D*" to "B*" even if "B"
6447 is ambiguous or inaccessible. If this is really a
6448 static_cast, then we check both for inaccessibility and
6449 ambiguity. However, if this is a static_cast being performed
6450 because the user wrote a C-style cast, then accessibility is
6451 not considered. */
6452 base = lookup_base (TREE_TYPE (type), intype,
6453 c_cast_p ? ba_unique : ba_check,
6454 NULL, complain);
6456 /* Convert from "B*" to "D*". This function will check that "B"
6457 is not a virtual base of "D". */
6458 expr = build_base_path (MINUS_EXPR, build_address (expr),
6459 base, /*nonnull=*/false, complain);
6460 /* Convert the pointer to a reference -- but then remember that
6461 there are no expressions with reference type in C++.
6463 We call rvalue so that there's an actual tree code
6464 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6465 is a variable with the same type, the conversion would get folded
6466 away, leaving just the variable and causing lvalue_kind to give
6467 the wrong answer. */
6468 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6471 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6472 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6473 if (TREE_CODE (type) == REFERENCE_TYPE
6474 && TYPE_REF_IS_RVALUE (type)
6475 && (clk = real_lvalue_p (expr))
6476 && reference_related_p (TREE_TYPE (type), intype)
6477 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6479 if (clk == clk_ordinary)
6481 /* Handle the (non-bit-field) lvalue case here by casting to
6482 lvalue reference and then changing it to an rvalue reference.
6483 Casting an xvalue to rvalue reference will be handled by the
6484 main code path. */
6485 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6486 result = (perform_direct_initialization_if_possible
6487 (lref, expr, c_cast_p, complain));
6488 result = cp_fold_convert (type, result);
6489 /* Make sure we don't fold back down to a named rvalue reference,
6490 because that would be an lvalue. */
6491 if (DECL_P (result))
6492 result = build1 (NON_LVALUE_EXPR, type, result);
6493 return convert_from_reference (result);
6495 else
6496 /* For a bit-field or packed field, bind to a temporary. */
6497 expr = rvalue (expr);
6500 /* Resolve overloaded address here rather than once in
6501 implicit_conversion and again in the inverse code below. */
6502 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6504 expr = instantiate_type (type, expr, complain);
6505 intype = TREE_TYPE (expr);
6508 /* [expr.static.cast]
6510 Any expression can be explicitly converted to type cv void. */
6511 if (VOID_TYPE_P (type))
6512 return convert_to_void (expr, ICV_CAST, complain);
6514 /* [class.abstract]
6515 An abstract class shall not be used ... as the type of an explicit
6516 conversion. */
6517 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6518 return error_mark_node;
6520 /* [expr.static.cast]
6522 An expression e can be explicitly converted to a type T using a
6523 static_cast of the form static_cast<T>(e) if the declaration T
6524 t(e);" is well-formed, for some invented temporary variable
6525 t. */
6526 result = perform_direct_initialization_if_possible (type, expr,
6527 c_cast_p, complain);
6528 if (result)
6530 result = convert_from_reference (result);
6532 /* [expr.static.cast]
6534 If T is a reference type, the result is an lvalue; otherwise,
6535 the result is an rvalue. */
6536 if (TREE_CODE (type) != REFERENCE_TYPE)
6537 result = rvalue (result);
6538 return result;
6541 /* [expr.static.cast]
6543 The inverse of any standard conversion sequence (clause _conv_),
6544 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6545 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6546 (_conv.bool_) conversions, can be performed explicitly using
6547 static_cast subject to the restriction that the explicit
6548 conversion does not cast away constness (_expr.const.cast_), and
6549 the following additional rules for specific cases: */
6550 /* For reference, the conversions not excluded are: integral
6551 promotions, floating point promotion, integral conversions,
6552 floating point conversions, floating-integral conversions,
6553 pointer conversions, and pointer to member conversions. */
6554 /* DR 128
6556 A value of integral _or enumeration_ type can be explicitly
6557 converted to an enumeration type. */
6558 /* The effect of all that is that any conversion between any two
6559 types which are integral, floating, or enumeration types can be
6560 performed. */
6561 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6562 || SCALAR_FLOAT_TYPE_P (type))
6563 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6564 || SCALAR_FLOAT_TYPE_P (intype)))
6565 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6567 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6568 && CLASS_TYPE_P (TREE_TYPE (type))
6569 && CLASS_TYPE_P (TREE_TYPE (intype))
6570 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6571 (TREE_TYPE (intype))),
6572 build_pointer_type (TYPE_MAIN_VARIANT
6573 (TREE_TYPE (type))),
6574 complain))
6576 tree base;
6578 if (!c_cast_p
6579 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6580 complain))
6581 return error_mark_node;
6582 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6583 c_cast_p ? ba_unique : ba_check,
6584 NULL, complain);
6585 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6586 complain);
6587 return cp_fold_convert(type, expr);
6590 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6591 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6593 tree c1;
6594 tree c2;
6595 tree t1;
6596 tree t2;
6598 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6599 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6601 if (TYPE_PTRDATAMEM_P (type))
6603 t1 = (build_ptrmem_type
6604 (c1,
6605 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6606 t2 = (build_ptrmem_type
6607 (c2,
6608 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6610 else
6612 t1 = intype;
6613 t2 = type;
6615 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6617 if (!c_cast_p
6618 && check_for_casting_away_constness (intype, type,
6619 STATIC_CAST_EXPR,
6620 complain))
6621 return error_mark_node;
6622 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6623 c_cast_p, complain);
6627 /* [expr.static.cast]
6629 An rvalue of type "pointer to cv void" can be explicitly
6630 converted to a pointer to object type. A value of type pointer
6631 to object converted to "pointer to cv void" and back to the
6632 original pointer type will have its original value. */
6633 if (TYPE_PTR_P (intype)
6634 && VOID_TYPE_P (TREE_TYPE (intype))
6635 && TYPE_PTROB_P (type))
6637 if (!c_cast_p
6638 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6639 complain))
6640 return error_mark_node;
6641 return build_nop (type, expr);
6644 *valid_p = false;
6645 return error_mark_node;
6648 /* Return an expression representing static_cast<TYPE>(EXPR). */
6650 tree
6651 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6653 tree result;
6654 bool valid_p;
6656 if (type == error_mark_node || expr == error_mark_node)
6657 return error_mark_node;
6659 if (processing_template_decl)
6661 expr = build_min (STATIC_CAST_EXPR, type, expr);
6662 /* We don't know if it will or will not have side effects. */
6663 TREE_SIDE_EFFECTS (expr) = 1;
6664 return convert_from_reference (expr);
6667 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6668 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6669 if (TREE_CODE (type) != REFERENCE_TYPE
6670 && TREE_CODE (expr) == NOP_EXPR
6671 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6672 expr = TREE_OPERAND (expr, 0);
6674 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6675 complain);
6676 if (valid_p)
6678 if (result != error_mark_node)
6679 maybe_warn_about_useless_cast (type, expr, complain);
6680 return result;
6683 if (complain & tf_error)
6684 error ("invalid static_cast from type %qT to type %qT",
6685 TREE_TYPE (expr), type);
6686 return error_mark_node;
6689 /* EXPR is an expression with member function or pointer-to-member
6690 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6691 not permitted by ISO C++, but we accept it in some modes. If we
6692 are not in one of those modes, issue a diagnostic. Return the
6693 converted expression. */
6695 tree
6696 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6698 tree intype;
6699 tree decl;
6701 intype = TREE_TYPE (expr);
6702 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6703 || TREE_CODE (intype) == METHOD_TYPE);
6705 if (!(complain & tf_warning_or_error))
6706 return error_mark_node;
6708 if (pedantic || warn_pmf2ptr)
6709 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6710 "converting from %qT to %qT", intype, type);
6712 if (TREE_CODE (intype) == METHOD_TYPE)
6713 expr = build_addr_func (expr, complain);
6714 else if (TREE_CODE (expr) == PTRMEM_CST)
6715 expr = build_address (PTRMEM_CST_MEMBER (expr));
6716 else
6718 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6719 decl = build_address (decl);
6720 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6723 if (expr == error_mark_node)
6724 return error_mark_node;
6726 return build_nop (type, expr);
6729 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6730 If C_CAST_P is true, this reinterpret cast is being done as part of
6731 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6732 indicate whether or not reinterpret_cast was valid. */
6734 static tree
6735 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6736 bool *valid_p, tsubst_flags_t complain)
6738 tree intype;
6740 /* Assume the cast is invalid. */
6741 if (valid_p)
6742 *valid_p = true;
6744 if (type == error_mark_node || error_operand_p (expr))
6745 return error_mark_node;
6747 intype = TREE_TYPE (expr);
6749 /* Save casted types in the function's used types hash table. */
6750 used_types_insert (type);
6752 /* [expr.reinterpret.cast]
6753 An lvalue expression of type T1 can be cast to the type
6754 "reference to T2" if an expression of type "pointer to T1" can be
6755 explicitly converted to the type "pointer to T2" using a
6756 reinterpret_cast. */
6757 if (TREE_CODE (type) == REFERENCE_TYPE)
6759 if (! real_lvalue_p (expr))
6761 if (complain & tf_error)
6762 error ("invalid cast of an rvalue expression of type "
6763 "%qT to type %qT",
6764 intype, type);
6765 return error_mark_node;
6768 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6769 "B" are related class types; the reinterpret_cast does not
6770 adjust the pointer. */
6771 if (TYPE_PTR_P (intype)
6772 && (complain & tf_warning)
6773 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6774 COMPARE_BASE | COMPARE_DERIVED)))
6775 warning (0, "casting %qT to %qT does not dereference pointer",
6776 intype, type);
6778 expr = cp_build_addr_expr (expr, complain);
6780 if (warn_strict_aliasing > 2)
6781 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6783 if (expr != error_mark_node)
6784 expr = build_reinterpret_cast_1
6785 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6786 valid_p, complain);
6787 if (expr != error_mark_node)
6788 /* cp_build_indirect_ref isn't right for rvalue refs. */
6789 expr = convert_from_reference (fold_convert (type, expr));
6790 return expr;
6793 /* As a G++ extension, we consider conversions from member
6794 functions, and pointers to member functions to
6795 pointer-to-function and pointer-to-void types. If
6796 -Wno-pmf-conversions has not been specified,
6797 convert_member_func_to_ptr will issue an error message. */
6798 if ((TYPE_PTRMEMFUNC_P (intype)
6799 || TREE_CODE (intype) == METHOD_TYPE)
6800 && TYPE_PTR_P (type)
6801 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6802 || VOID_TYPE_P (TREE_TYPE (type))))
6803 return convert_member_func_to_ptr (type, expr, complain);
6805 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6806 array-to-pointer, and function-to-pointer conversions are
6807 performed. */
6808 expr = decay_conversion (expr, complain);
6810 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6811 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6812 if (TREE_CODE (expr) == NOP_EXPR
6813 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6814 expr = TREE_OPERAND (expr, 0);
6816 if (error_operand_p (expr))
6817 return error_mark_node;
6819 intype = TREE_TYPE (expr);
6821 /* [expr.reinterpret.cast]
6822 A pointer can be converted to any integral type large enough to
6823 hold it. ... A value of type std::nullptr_t can be converted to
6824 an integral type; the conversion has the same meaning and
6825 validity as a conversion of (void*)0 to the integral type. */
6826 if (CP_INTEGRAL_TYPE_P (type)
6827 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6829 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6831 if (complain & tf_error)
6832 permerror (input_location, "cast from %qT to %qT loses precision",
6833 intype, type);
6834 else
6835 return error_mark_node;
6837 if (NULLPTR_TYPE_P (intype))
6838 return build_int_cst (type, 0);
6840 /* [expr.reinterpret.cast]
6841 A value of integral or enumeration type can be explicitly
6842 converted to a pointer. */
6843 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6844 /* OK */
6846 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6847 || TYPE_PTR_OR_PTRMEM_P (type))
6848 && same_type_p (type, intype))
6849 /* DR 799 */
6850 return fold_if_not_in_template (build_nop (type, expr));
6851 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6852 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6853 return fold_if_not_in_template (build_nop (type, expr));
6854 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6855 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6857 tree sexpr = expr;
6859 if (!c_cast_p
6860 && check_for_casting_away_constness (intype, type,
6861 REINTERPRET_CAST_EXPR,
6862 complain))
6863 return error_mark_node;
6864 /* Warn about possible alignment problems. */
6865 if (STRICT_ALIGNMENT && warn_cast_align
6866 && (complain & tf_warning)
6867 && !VOID_TYPE_P (type)
6868 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6869 && COMPLETE_TYPE_P (TREE_TYPE (type))
6870 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6871 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6872 warning (OPT_Wcast_align, "cast from %qT to %qT "
6873 "increases required alignment of target type", intype, type);
6875 /* We need to strip nops here, because the front end likes to
6876 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6877 STRIP_NOPS (sexpr);
6878 if (warn_strict_aliasing <= 2)
6879 strict_aliasing_warning (intype, type, sexpr);
6881 return fold_if_not_in_template (build_nop (type, expr));
6883 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6884 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6886 if (complain & tf_warning)
6887 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6888 object pointer type or vice versa is conditionally-supported." */
6889 warning (OPT_Wconditionally_supported,
6890 "casting between pointer-to-function and pointer-to-object "
6891 "is conditionally-supported");
6892 return fold_if_not_in_template (build_nop (type, expr));
6894 else if (TREE_CODE (type) == VECTOR_TYPE)
6895 return fold_if_not_in_template (convert_to_vector (type, expr));
6896 else if (TREE_CODE (intype) == VECTOR_TYPE
6897 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6898 return fold_if_not_in_template (convert_to_integer (type, expr));
6899 else
6901 if (valid_p)
6902 *valid_p = false;
6903 if (complain & tf_error)
6904 error ("invalid cast from type %qT to type %qT", intype, type);
6905 return error_mark_node;
6908 return cp_convert (type, expr, complain);
6911 tree
6912 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6914 tree r;
6916 if (type == error_mark_node || expr == error_mark_node)
6917 return error_mark_node;
6919 if (processing_template_decl)
6921 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6923 if (!TREE_SIDE_EFFECTS (t)
6924 && type_dependent_expression_p (expr))
6925 /* There might turn out to be side effects inside expr. */
6926 TREE_SIDE_EFFECTS (t) = 1;
6927 return convert_from_reference (t);
6930 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6931 /*valid_p=*/NULL, complain);
6932 if (r != error_mark_node)
6933 maybe_warn_about_useless_cast (type, expr, complain);
6934 return r;
6937 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6938 return an appropriate expression. Otherwise, return
6939 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6940 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6941 performing a C-style cast, its value upon return will indicate
6942 whether or not the conversion succeeded. */
6944 static tree
6945 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6946 bool *valid_p)
6948 tree src_type;
6949 tree reference_type;
6951 /* Callers are responsible for handling error_mark_node as a
6952 destination type. */
6953 gcc_assert (dst_type != error_mark_node);
6954 /* In a template, callers should be building syntactic
6955 representations of casts, not using this machinery. */
6956 gcc_assert (!processing_template_decl);
6958 /* Assume the conversion is invalid. */
6959 if (valid_p)
6960 *valid_p = false;
6962 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6964 if (complain & tf_error)
6965 error ("invalid use of const_cast with type %qT, "
6966 "which is not a pointer, "
6967 "reference, nor a pointer-to-data-member type", dst_type);
6968 return error_mark_node;
6971 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6973 if (complain & tf_error)
6974 error ("invalid use of const_cast with type %qT, which is a pointer "
6975 "or reference to a function type", dst_type);
6976 return error_mark_node;
6979 /* Save casted types in the function's used types hash table. */
6980 used_types_insert (dst_type);
6982 src_type = TREE_TYPE (expr);
6983 /* Expressions do not really have reference types. */
6984 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6985 src_type = TREE_TYPE (src_type);
6987 /* [expr.const.cast]
6989 For two object types T1 and T2, if a pointer to T1 can be explicitly
6990 converted to the type "pointer to T2" using a const_cast, then the
6991 following conversions can also be made:
6993 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6994 type T2 using the cast const_cast<T2&>;
6996 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6997 type T2 using the cast const_cast<T2&&>; and
6999 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7000 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7002 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7004 reference_type = dst_type;
7005 if (!TYPE_REF_IS_RVALUE (dst_type)
7006 ? real_lvalue_p (expr)
7007 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7008 ? lvalue_p (expr)
7009 : lvalue_or_rvalue_with_address_p (expr)))
7010 /* OK. */;
7011 else
7013 if (complain & tf_error)
7014 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7015 src_type, dst_type);
7016 return error_mark_node;
7018 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7019 src_type = build_pointer_type (src_type);
7021 else
7023 reference_type = NULL_TREE;
7024 /* If the destination type is not a reference type, the
7025 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7026 conversions are performed. */
7027 src_type = type_decays_to (src_type);
7028 if (src_type == error_mark_node)
7029 return error_mark_node;
7032 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7034 if (comp_ptr_ttypes_const (dst_type, src_type))
7036 if (valid_p)
7038 *valid_p = true;
7039 /* This cast is actually a C-style cast. Issue a warning if
7040 the user is making a potentially unsafe cast. */
7041 check_for_casting_away_constness (src_type, dst_type,
7042 CAST_EXPR, complain);
7044 if (reference_type)
7046 expr = cp_build_addr_expr (expr, complain);
7047 if (expr == error_mark_node)
7048 return error_mark_node;
7049 expr = build_nop (reference_type, expr);
7050 return convert_from_reference (expr);
7052 else
7054 expr = decay_conversion (expr, complain);
7055 if (expr == error_mark_node)
7056 return error_mark_node;
7058 /* build_c_cast puts on a NOP_EXPR to make the result not an
7059 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7060 non-lvalue context. */
7061 if (TREE_CODE (expr) == NOP_EXPR
7062 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7063 expr = TREE_OPERAND (expr, 0);
7064 return build_nop (dst_type, expr);
7067 else if (valid_p
7068 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7069 TREE_TYPE (src_type)))
7070 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7071 complain);
7074 if (complain & tf_error)
7075 error ("invalid const_cast from type %qT to type %qT",
7076 src_type, dst_type);
7077 return error_mark_node;
7080 tree
7081 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7083 tree r;
7085 if (type == error_mark_node || error_operand_p (expr))
7086 return error_mark_node;
7088 if (processing_template_decl)
7090 tree t = build_min (CONST_CAST_EXPR, type, expr);
7092 if (!TREE_SIDE_EFFECTS (t)
7093 && type_dependent_expression_p (expr))
7094 /* There might turn out to be side effects inside expr. */
7095 TREE_SIDE_EFFECTS (t) = 1;
7096 return convert_from_reference (t);
7099 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7100 if (r != error_mark_node)
7101 maybe_warn_about_useless_cast (type, expr, complain);
7102 return r;
7105 /* Like cp_build_c_cast, but for the c-common bits. */
7107 tree
7108 build_c_cast (location_t /*loc*/, tree type, tree expr)
7110 return cp_build_c_cast (type, expr, tf_warning_or_error);
7113 /* Build an expression representing an explicit C-style cast to type
7114 TYPE of expression EXPR. */
7116 tree
7117 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7119 tree value = expr;
7120 tree result;
7121 bool valid_p;
7123 if (type == error_mark_node || error_operand_p (expr))
7124 return error_mark_node;
7126 if (processing_template_decl)
7128 tree t = build_min (CAST_EXPR, type,
7129 tree_cons (NULL_TREE, value, NULL_TREE));
7130 /* We don't know if it will or will not have side effects. */
7131 TREE_SIDE_EFFECTS (t) = 1;
7132 return convert_from_reference (t);
7135 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7136 'Class') should always be retained, because this information aids
7137 in method lookup. */
7138 if (objc_is_object_ptr (type)
7139 && objc_is_object_ptr (TREE_TYPE (expr)))
7140 return build_nop (type, expr);
7142 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7143 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7144 if (TREE_CODE (type) != REFERENCE_TYPE
7145 && TREE_CODE (value) == NOP_EXPR
7146 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7147 value = TREE_OPERAND (value, 0);
7149 if (TREE_CODE (type) == ARRAY_TYPE)
7151 /* Allow casting from T1* to T2[] because Cfront allows it.
7152 NIHCL uses it. It is not valid ISO C++ however. */
7153 if (TYPE_PTR_P (TREE_TYPE (expr)))
7155 if (complain & tf_error)
7156 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7157 else
7158 return error_mark_node;
7159 type = build_pointer_type (TREE_TYPE (type));
7161 else
7163 if (complain & tf_error)
7164 error ("ISO C++ forbids casting to an array type %qT", type);
7165 return error_mark_node;
7169 if (TREE_CODE (type) == FUNCTION_TYPE
7170 || TREE_CODE (type) == METHOD_TYPE)
7172 if (complain & tf_error)
7173 error ("invalid cast to function type %qT", type);
7174 return error_mark_node;
7177 if (TYPE_PTR_P (type)
7178 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7179 /* Casting to an integer of smaller size is an error detected elsewhere. */
7180 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7181 /* Don't warn about converting any constant. */
7182 && !TREE_CONSTANT (value))
7183 warning_at (input_location, OPT_Wint_to_pointer_cast,
7184 "cast to pointer from integer of different size");
7186 /* A C-style cast can be a const_cast. */
7187 result = build_const_cast_1 (type, value, complain & tf_warning,
7188 &valid_p);
7189 if (valid_p)
7191 if (result != error_mark_node)
7192 maybe_warn_about_useless_cast (type, value, complain);
7193 return result;
7196 /* Or a static cast. */
7197 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7198 &valid_p, complain);
7199 /* Or a reinterpret_cast. */
7200 if (!valid_p)
7201 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7202 &valid_p, complain);
7203 /* The static_cast or reinterpret_cast may be followed by a
7204 const_cast. */
7205 if (valid_p
7206 /* A valid cast may result in errors if, for example, a
7207 conversion to an ambiguous base class is required. */
7208 && !error_operand_p (result))
7210 tree result_type;
7212 maybe_warn_about_useless_cast (type, value, complain);
7214 /* Non-class rvalues always have cv-unqualified type. */
7215 if (!CLASS_TYPE_P (type))
7216 type = TYPE_MAIN_VARIANT (type);
7217 result_type = TREE_TYPE (result);
7218 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7219 result_type = TYPE_MAIN_VARIANT (result_type);
7220 /* If the type of RESULT does not match TYPE, perform a
7221 const_cast to make it match. If the static_cast or
7222 reinterpret_cast succeeded, we will differ by at most
7223 cv-qualification, so the follow-on const_cast is guaranteed
7224 to succeed. */
7225 if (!same_type_p (non_reference (type), non_reference (result_type)))
7227 result = build_const_cast_1 (type, result, false, &valid_p);
7228 gcc_assert (valid_p);
7230 return result;
7233 return error_mark_node;
7236 /* For use from the C common bits. */
7237 tree
7238 build_modify_expr (location_t /*location*/,
7239 tree lhs, tree /*lhs_origtype*/,
7240 enum tree_code modifycode,
7241 location_t /*rhs_location*/, tree rhs,
7242 tree /*rhs_origtype*/)
7244 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7247 /* Build an assignment expression of lvalue LHS from value RHS.
7248 MODIFYCODE is the code for a binary operator that we use
7249 to combine the old value of LHS with RHS to get the new value.
7250 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7252 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7254 tree
7255 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7256 tsubst_flags_t complain)
7258 tree result;
7259 tree newrhs = rhs;
7260 tree lhstype = TREE_TYPE (lhs);
7261 tree olhstype = lhstype;
7262 bool plain_assign = (modifycode == NOP_EXPR);
7264 /* Avoid duplicate error messages from operands that had errors. */
7265 if (error_operand_p (lhs) || error_operand_p (rhs))
7266 return error_mark_node;
7268 /* Handle control structure constructs used as "lvalues". */
7269 switch (TREE_CODE (lhs))
7271 /* Handle --foo = 5; as these are valid constructs in C++. */
7272 case PREDECREMENT_EXPR:
7273 case PREINCREMENT_EXPR:
7274 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7275 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7276 stabilize_reference (TREE_OPERAND (lhs, 0)),
7277 TREE_OPERAND (lhs, 1));
7278 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7279 modifycode, rhs, complain);
7280 if (newrhs == error_mark_node)
7281 return error_mark_node;
7282 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7284 /* Handle (a, b) used as an "lvalue". */
7285 case COMPOUND_EXPR:
7286 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7287 modifycode, rhs, complain);
7288 if (newrhs == error_mark_node)
7289 return error_mark_node;
7290 return build2 (COMPOUND_EXPR, lhstype,
7291 TREE_OPERAND (lhs, 0), newrhs);
7293 case MODIFY_EXPR:
7294 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7295 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7296 stabilize_reference (TREE_OPERAND (lhs, 0)),
7297 TREE_OPERAND (lhs, 1));
7298 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7299 complain);
7300 if (newrhs == error_mark_node)
7301 return error_mark_node;
7302 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7304 case MIN_EXPR:
7305 case MAX_EXPR:
7306 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7307 when neither operand has side-effects. */
7308 if (!lvalue_or_else (lhs, lv_assign, complain))
7309 return error_mark_node;
7311 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7312 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7314 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7315 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7316 boolean_type_node,
7317 TREE_OPERAND (lhs, 0),
7318 TREE_OPERAND (lhs, 1)),
7319 TREE_OPERAND (lhs, 0),
7320 TREE_OPERAND (lhs, 1));
7321 /* Fall through. */
7323 /* Handle (a ? b : c) used as an "lvalue". */
7324 case COND_EXPR:
7326 /* Produce (a ? (b = rhs) : (c = rhs))
7327 except that the RHS goes through a save-expr
7328 so the code to compute it is only emitted once. */
7329 tree cond;
7330 tree preeval = NULL_TREE;
7332 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7334 if (complain & tf_error)
7335 error ("void value not ignored as it ought to be");
7336 return error_mark_node;
7339 rhs = stabilize_expr (rhs, &preeval);
7341 /* Check this here to avoid odd errors when trying to convert
7342 a throw to the type of the COND_EXPR. */
7343 if (!lvalue_or_else (lhs, lv_assign, complain))
7344 return error_mark_node;
7346 cond = build_conditional_expr
7347 (input_location, TREE_OPERAND (lhs, 0),
7348 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7349 modifycode, rhs, complain),
7350 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7351 modifycode, rhs, complain),
7352 complain);
7354 if (cond == error_mark_node)
7355 return cond;
7356 /* Make sure the code to compute the rhs comes out
7357 before the split. */
7358 if (preeval)
7359 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7360 return cond;
7363 default:
7364 break;
7367 if (modifycode == INIT_EXPR)
7369 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7370 /* Do the default thing. */;
7371 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7373 /* Compound literal. */
7374 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7375 /* Call convert to generate an error; see PR 11063. */
7376 rhs = convert (lhstype, rhs);
7377 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7378 TREE_SIDE_EFFECTS (result) = 1;
7379 return result;
7381 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7382 /* Do the default thing. */;
7383 else
7385 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7386 result = build_special_member_call (lhs, complete_ctor_identifier,
7387 &rhs_vec, lhstype, LOOKUP_NORMAL,
7388 complain);
7389 release_tree_vector (rhs_vec);
7390 if (result == NULL_TREE)
7391 return error_mark_node;
7392 return result;
7395 else
7397 lhs = require_complete_type_sfinae (lhs, complain);
7398 if (lhs == error_mark_node)
7399 return error_mark_node;
7401 if (modifycode == NOP_EXPR)
7403 if (c_dialect_objc ())
7405 result = objc_maybe_build_modify_expr (lhs, rhs);
7406 if (result)
7407 return result;
7410 /* `operator=' is not an inheritable operator. */
7411 if (! MAYBE_CLASS_TYPE_P (lhstype))
7412 /* Do the default thing. */;
7413 else
7415 result = build_new_op (input_location, MODIFY_EXPR,
7416 LOOKUP_NORMAL, lhs, rhs,
7417 make_node (NOP_EXPR), /*overload=*/NULL,
7418 complain);
7419 if (result == NULL_TREE)
7420 return error_mark_node;
7421 return result;
7423 lhstype = olhstype;
7425 else
7427 tree init = NULL_TREE;
7429 /* A binary op has been requested. Combine the old LHS
7430 value with the RHS producing the value we should actually
7431 store into the LHS. */
7432 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7433 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7434 || MAYBE_CLASS_TYPE_P (lhstype)));
7436 /* Preevaluate the RHS to make sure its evaluation is complete
7437 before the lvalue-to-rvalue conversion of the LHS:
7439 [expr.ass] With respect to an indeterminately-sequenced
7440 function call, the operation of a compound assignment is a
7441 single evaluation. [ Note: Therefore, a function call shall
7442 not intervene between the lvalue-to-rvalue conversion and the
7443 side effect associated with any single compound assignment
7444 operator. -- end note ] */
7445 lhs = stabilize_reference (lhs);
7446 rhs = rvalue (rhs);
7447 rhs = stabilize_expr (rhs, &init);
7448 newrhs = cp_build_binary_op (input_location,
7449 modifycode, lhs, rhs,
7450 complain);
7451 if (newrhs == error_mark_node)
7453 if (complain & tf_error)
7454 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7455 TREE_TYPE (lhs), TREE_TYPE (rhs));
7456 return error_mark_node;
7459 if (init)
7460 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7462 /* Now it looks like a plain assignment. */
7463 modifycode = NOP_EXPR;
7464 if (c_dialect_objc ())
7466 result = objc_maybe_build_modify_expr (lhs, newrhs);
7467 if (result)
7468 return result;
7471 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7472 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7475 /* The left-hand side must be an lvalue. */
7476 if (!lvalue_or_else (lhs, lv_assign, complain))
7477 return error_mark_node;
7479 /* Warn about modifying something that is `const'. Don't warn if
7480 this is initialization. */
7481 if (modifycode != INIT_EXPR
7482 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7483 /* Functions are not modifiable, even though they are
7484 lvalues. */
7485 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7486 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7487 /* If it's an aggregate and any field is const, then it is
7488 effectively const. */
7489 || (CLASS_TYPE_P (lhstype)
7490 && C_TYPE_FIELDS_READONLY (lhstype))))
7492 if (complain & tf_error)
7493 cxx_readonly_error (lhs, lv_assign);
7494 else
7495 return error_mark_node;
7498 /* If storing into a structure or union member, it may have been given a
7499 lowered bitfield type. We need to convert to the declared type first,
7500 so retrieve it now. */
7502 olhstype = unlowered_expr_type (lhs);
7504 /* Convert new value to destination type. */
7506 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7508 int from_array;
7510 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7512 if (modifycode != INIT_EXPR)
7514 if (complain & tf_error)
7515 error ("assigning to an array from an initializer list");
7516 return error_mark_node;
7518 if (check_array_initializer (lhs, lhstype, newrhs))
7519 return error_mark_node;
7520 newrhs = digest_init (lhstype, newrhs, complain);
7521 if (newrhs == error_mark_node)
7522 return error_mark_node;
7525 /* C++11 8.5/17: "If the destination type is an array of characters,
7526 an array of char16_t, an array of char32_t, or an array of wchar_t,
7527 and the initializer is a string literal...". */
7528 else if (TREE_CODE (newrhs) == STRING_CST
7529 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7530 && modifycode == INIT_EXPR)
7532 newrhs = digest_init (lhstype, newrhs, complain);
7533 if (newrhs == error_mark_node)
7534 return error_mark_node;
7537 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7538 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7540 if (complain & tf_error)
7541 error ("incompatible types in assignment of %qT to %qT",
7542 TREE_TYPE (rhs), lhstype);
7543 return error_mark_node;
7546 /* Allow array assignment in compiler-generated code. */
7547 else if (!current_function_decl
7548 || !DECL_DEFAULTED_FN (current_function_decl))
7550 /* This routine is used for both initialization and assignment.
7551 Make sure the diagnostic message differentiates the context. */
7552 if (complain & tf_error)
7554 if (modifycode == INIT_EXPR)
7555 error ("array used as initializer");
7556 else
7557 error ("invalid array assignment");
7559 return error_mark_node;
7562 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7563 ? 1 + (modifycode != INIT_EXPR): 0;
7564 return build_vec_init (lhs, NULL_TREE, newrhs,
7565 /*explicit_value_init_p=*/false,
7566 from_array, complain);
7569 if (modifycode == INIT_EXPR)
7570 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7571 LOOKUP_ONLYCONVERTING. */
7572 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7573 ICR_INIT, NULL_TREE, 0,
7574 complain);
7575 else
7576 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7577 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7579 if (!same_type_p (lhstype, olhstype))
7580 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7582 if (modifycode != INIT_EXPR)
7584 if (TREE_CODE (newrhs) == CALL_EXPR
7585 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7586 newrhs = build_cplus_new (lhstype, newrhs, complain);
7588 /* Can't initialize directly from a TARGET_EXPR, since that would
7589 cause the lhs to be constructed twice, and possibly result in
7590 accidental self-initialization. So we force the TARGET_EXPR to be
7591 expanded without a target. */
7592 if (TREE_CODE (newrhs) == TARGET_EXPR)
7593 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7594 TREE_OPERAND (newrhs, 0));
7597 if (newrhs == error_mark_node)
7598 return error_mark_node;
7600 if (c_dialect_objc () && flag_objc_gc)
7602 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7604 if (result)
7605 return result;
7608 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7609 lhstype, lhs, newrhs);
7611 TREE_SIDE_EFFECTS (result) = 1;
7612 if (!plain_assign)
7613 TREE_NO_WARNING (result) = 1;
7615 return result;
7618 tree
7619 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7620 tree rhs, tsubst_flags_t complain)
7622 if (processing_template_decl)
7623 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7624 build_min_nt_loc (loc, modifycode, NULL_TREE,
7625 NULL_TREE), rhs);
7627 if (modifycode != NOP_EXPR)
7629 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7630 make_node (modifycode), /*overload=*/NULL,
7631 complain);
7632 if (rval)
7634 TREE_NO_WARNING (rval) = 1;
7635 return rval;
7638 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7641 /* Helper function for get_delta_difference which assumes FROM is a base
7642 class of TO. Returns a delta for the conversion of pointer-to-member
7643 of FROM to pointer-to-member of TO. If the conversion is invalid and
7644 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7645 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7646 If C_CAST_P is true, this conversion is taking place as part of a
7647 C-style cast. */
7649 static tree
7650 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7651 tsubst_flags_t complain)
7653 tree binfo;
7654 base_kind kind;
7656 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7657 &kind, complain);
7659 if (binfo == error_mark_node)
7661 if (!(complain & tf_error))
7662 return error_mark_node;
7664 error (" in pointer to member function conversion");
7665 return size_zero_node;
7667 else if (binfo)
7669 if (kind != bk_via_virtual)
7670 return BINFO_OFFSET (binfo);
7671 else
7672 /* FROM is a virtual base class of TO. Issue an error or warning
7673 depending on whether or not this is a reinterpret cast. */
7675 if (!(complain & tf_error))
7676 return error_mark_node;
7678 error ("pointer to member conversion via virtual base %qT",
7679 BINFO_TYPE (binfo_from_vbase (binfo)));
7681 return size_zero_node;
7684 else
7685 return NULL_TREE;
7688 /* Get difference in deltas for different pointer to member function
7689 types. If the conversion is invalid and tf_error is not set in
7690 COMPLAIN, returns error_mark_node, otherwise returns an integer
7691 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7692 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7693 conversions as well. If C_CAST_P is true this conversion is taking
7694 place as part of a C-style cast.
7696 Note that the naming of FROM and TO is kind of backwards; the return
7697 value is what we add to a TO in order to get a FROM. They are named
7698 this way because we call this function to find out how to convert from
7699 a pointer to member of FROM to a pointer to member of TO. */
7701 static tree
7702 get_delta_difference (tree from, tree to,
7703 bool allow_inverse_p,
7704 bool c_cast_p, tsubst_flags_t complain)
7706 tree result;
7708 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7709 /* Pointer to member of incomplete class is permitted*/
7710 result = size_zero_node;
7711 else
7712 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7714 if (result == error_mark_node)
7715 return error_mark_node;
7717 if (!result)
7719 if (!allow_inverse_p)
7721 if (!(complain & tf_error))
7722 return error_mark_node;
7724 error_not_base_type (from, to);
7725 error (" in pointer to member conversion");
7726 result = size_zero_node;
7728 else
7730 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7732 if (result == error_mark_node)
7733 return error_mark_node;
7735 if (result)
7736 result = size_diffop_loc (input_location,
7737 size_zero_node, result);
7738 else
7740 if (!(complain & tf_error))
7741 return error_mark_node;
7743 error_not_base_type (from, to);
7744 error (" in pointer to member conversion");
7745 result = size_zero_node;
7750 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7751 result));
7754 /* Return a constructor for the pointer-to-member-function TYPE using
7755 the other components as specified. */
7757 tree
7758 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7760 tree u = NULL_TREE;
7761 tree delta_field;
7762 tree pfn_field;
7763 vec<constructor_elt, va_gc> *v;
7765 /* Pull the FIELD_DECLs out of the type. */
7766 pfn_field = TYPE_FIELDS (type);
7767 delta_field = DECL_CHAIN (pfn_field);
7769 /* Make sure DELTA has the type we want. */
7770 delta = convert_and_check (input_location, delta_type_node, delta);
7772 /* Convert to the correct target type if necessary. */
7773 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7775 /* Finish creating the initializer. */
7776 vec_alloc (v, 2);
7777 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7778 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7779 u = build_constructor (type, v);
7780 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7781 TREE_STATIC (u) = (TREE_CONSTANT (u)
7782 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7783 != NULL_TREE)
7784 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7785 != NULL_TREE));
7786 return u;
7789 /* Build a constructor for a pointer to member function. It can be
7790 used to initialize global variables, local variable, or used
7791 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7792 want to be.
7794 If FORCE is nonzero, then force this conversion, even if
7795 we would rather not do it. Usually set when using an explicit
7796 cast. A C-style cast is being processed iff C_CAST_P is true.
7798 Return error_mark_node, if something goes wrong. */
7800 tree
7801 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7802 tsubst_flags_t complain)
7804 tree fn;
7805 tree pfn_type;
7806 tree to_type;
7808 if (error_operand_p (pfn))
7809 return error_mark_node;
7811 pfn_type = TREE_TYPE (pfn);
7812 to_type = build_ptrmemfunc_type (type);
7814 /* Handle multiple conversions of pointer to member functions. */
7815 if (TYPE_PTRMEMFUNC_P (pfn_type))
7817 tree delta = NULL_TREE;
7818 tree npfn = NULL_TREE;
7819 tree n;
7821 if (!force
7822 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7823 LOOKUP_NORMAL, complain))
7825 if (complain & tf_error)
7826 error ("invalid conversion to type %qT from type %qT",
7827 to_type, pfn_type);
7828 else
7829 return error_mark_node;
7832 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7833 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7834 force,
7835 c_cast_p, complain);
7836 if (n == error_mark_node)
7837 return error_mark_node;
7839 /* We don't have to do any conversion to convert a
7840 pointer-to-member to its own type. But, we don't want to
7841 just return a PTRMEM_CST if there's an explicit cast; that
7842 cast should make the expression an invalid template argument. */
7843 if (TREE_CODE (pfn) != PTRMEM_CST)
7845 if (same_type_p (to_type, pfn_type))
7846 return pfn;
7847 else if (integer_zerop (n))
7848 return build_reinterpret_cast (to_type, pfn,
7849 complain);
7852 if (TREE_SIDE_EFFECTS (pfn))
7853 pfn = save_expr (pfn);
7855 /* Obtain the function pointer and the current DELTA. */
7856 if (TREE_CODE (pfn) == PTRMEM_CST)
7857 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7858 else
7860 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7861 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7864 /* Just adjust the DELTA field. */
7865 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7866 (TREE_TYPE (delta), ptrdiff_type_node));
7867 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7868 n = cp_build_binary_op (input_location,
7869 LSHIFT_EXPR, n, integer_one_node,
7870 complain);
7871 delta = cp_build_binary_op (input_location,
7872 PLUS_EXPR, delta, n, complain);
7873 return build_ptrmemfunc1 (to_type, delta, npfn);
7876 /* Handle null pointer to member function conversions. */
7877 if (null_ptr_cst_p (pfn))
7879 pfn = cp_build_c_cast (type, pfn, complain);
7880 return build_ptrmemfunc1 (to_type,
7881 integer_zero_node,
7882 pfn);
7885 if (type_unknown_p (pfn))
7886 return instantiate_type (type, pfn, complain);
7888 fn = TREE_OPERAND (pfn, 0);
7889 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7890 /* In a template, we will have preserved the
7891 OFFSET_REF. */
7892 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7893 return make_ptrmem_cst (to_type, fn);
7896 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7897 given by CST.
7899 ??? There is no consistency as to the types returned for the above
7900 values. Some code acts as if it were a sizetype and some as if it were
7901 integer_type_node. */
7903 void
7904 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7906 tree type = TREE_TYPE (cst);
7907 tree fn = PTRMEM_CST_MEMBER (cst);
7908 tree ptr_class, fn_class;
7910 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7912 /* The class that the function belongs to. */
7913 fn_class = DECL_CONTEXT (fn);
7915 /* The class that we're creating a pointer to member of. */
7916 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7918 /* First, calculate the adjustment to the function's class. */
7919 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7920 /*c_cast_p=*/0, tf_warning_or_error);
7922 if (!DECL_VIRTUAL_P (fn))
7923 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7924 build_addr_func (fn, tf_warning_or_error));
7925 else
7927 /* If we're dealing with a virtual function, we have to adjust 'this'
7928 again, to point to the base which provides the vtable entry for
7929 fn; the call will do the opposite adjustment. */
7930 tree orig_class = DECL_CONTEXT (fn);
7931 tree binfo = binfo_or_else (orig_class, fn_class);
7932 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7933 *delta, BINFO_OFFSET (binfo));
7934 *delta = fold_if_not_in_template (*delta);
7936 /* We set PFN to the vtable offset at which the function can be
7937 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7938 case delta is shifted left, and then incremented). */
7939 *pfn = DECL_VINDEX (fn);
7940 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7941 TYPE_SIZE_UNIT (vtable_entry_type));
7942 *pfn = fold_if_not_in_template (*pfn);
7944 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7946 case ptrmemfunc_vbit_in_pfn:
7947 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7948 integer_one_node);
7949 *pfn = fold_if_not_in_template (*pfn);
7950 break;
7952 case ptrmemfunc_vbit_in_delta:
7953 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7954 *delta, integer_one_node);
7955 *delta = fold_if_not_in_template (*delta);
7956 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7957 *delta, integer_one_node);
7958 *delta = fold_if_not_in_template (*delta);
7959 break;
7961 default:
7962 gcc_unreachable ();
7965 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7966 *pfn = fold_if_not_in_template (*pfn);
7970 /* Return an expression for PFN from the pointer-to-member function
7971 given by T. */
7973 static tree
7974 pfn_from_ptrmemfunc (tree t)
7976 if (TREE_CODE (t) == PTRMEM_CST)
7978 tree delta;
7979 tree pfn;
7981 expand_ptrmemfunc_cst (t, &delta, &pfn);
7982 if (pfn)
7983 return pfn;
7986 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7989 /* Return an expression for DELTA from the pointer-to-member function
7990 given by T. */
7992 static tree
7993 delta_from_ptrmemfunc (tree t)
7995 if (TREE_CODE (t) == PTRMEM_CST)
7997 tree delta;
7998 tree pfn;
8000 expand_ptrmemfunc_cst (t, &delta, &pfn);
8001 if (delta)
8002 return delta;
8005 return build_ptrmemfunc_access_expr (t, delta_identifier);
8008 /* Convert value RHS to type TYPE as preparation for an assignment to
8009 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8010 implicit conversion is. If FNDECL is non-NULL, we are doing the
8011 conversion in order to pass the PARMNUMth argument of FNDECL.
8012 If FNDECL is NULL, we are doing the conversion in function pointer
8013 argument passing, conversion in initialization, etc. */
8015 static tree
8016 convert_for_assignment (tree type, tree rhs,
8017 impl_conv_rhs errtype, tree fndecl, int parmnum,
8018 tsubst_flags_t complain, int flags)
8020 tree rhstype;
8021 enum tree_code coder;
8023 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8024 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8025 rhs = TREE_OPERAND (rhs, 0);
8027 rhstype = TREE_TYPE (rhs);
8028 coder = TREE_CODE (rhstype);
8030 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8031 && vector_types_convertible_p (type, rhstype, true))
8033 rhs = mark_rvalue_use (rhs);
8034 return convert (type, rhs);
8037 if (rhs == error_mark_node || rhstype == error_mark_node)
8038 return error_mark_node;
8039 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8040 return error_mark_node;
8042 /* The RHS of an assignment cannot have void type. */
8043 if (coder == VOID_TYPE)
8045 if (complain & tf_error)
8046 error ("void value not ignored as it ought to be");
8047 return error_mark_node;
8050 if (c_dialect_objc ())
8052 int parmno;
8053 tree selector;
8054 tree rname = fndecl;
8056 switch (errtype)
8058 case ICR_ASSIGN:
8059 parmno = -1;
8060 break;
8061 case ICR_INIT:
8062 parmno = -2;
8063 break;
8064 default:
8065 selector = objc_message_selector ();
8066 parmno = parmnum;
8067 if (selector && parmno > 1)
8069 rname = selector;
8070 parmno -= 1;
8074 if (objc_compare_types (type, rhstype, parmno, rname))
8076 rhs = mark_rvalue_use (rhs);
8077 return convert (type, rhs);
8081 /* [expr.ass]
8083 The expression is implicitly converted (clause _conv_) to the
8084 cv-unqualified type of the left operand.
8086 We allow bad conversions here because by the time we get to this point
8087 we are committed to doing the conversion. If we end up doing a bad
8088 conversion, convert_like will complain. */
8089 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8091 /* When -Wno-pmf-conversions is use, we just silently allow
8092 conversions from pointers-to-members to plain pointers. If
8093 the conversion doesn't work, cp_convert will complain. */
8094 if (!warn_pmf2ptr
8095 && TYPE_PTR_P (type)
8096 && TYPE_PTRMEMFUNC_P (rhstype))
8097 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8098 else
8100 if (complain & tf_error)
8102 /* If the right-hand side has unknown type, then it is an
8103 overloaded function. Call instantiate_type to get error
8104 messages. */
8105 if (rhstype == unknown_type_node)
8106 instantiate_type (type, rhs, tf_warning_or_error);
8107 else if (fndecl)
8108 error ("cannot convert %qT to %qT for argument %qP to %qD",
8109 rhstype, type, parmnum, fndecl);
8110 else
8111 switch (errtype)
8113 case ICR_DEFAULT_ARGUMENT:
8114 error ("cannot convert %qT to %qT in default argument",
8115 rhstype, type);
8116 break;
8117 case ICR_ARGPASS:
8118 error ("cannot convert %qT to %qT in argument passing",
8119 rhstype, type);
8120 break;
8121 case ICR_CONVERTING:
8122 error ("cannot convert %qT to %qT",
8123 rhstype, type);
8124 break;
8125 case ICR_INIT:
8126 error ("cannot convert %qT to %qT in initialization",
8127 rhstype, type);
8128 break;
8129 case ICR_RETURN:
8130 error ("cannot convert %qT to %qT in return",
8131 rhstype, type);
8132 break;
8133 case ICR_ASSIGN:
8134 error ("cannot convert %qT to %qT in assignment",
8135 rhstype, type);
8136 break;
8137 default:
8138 gcc_unreachable();
8140 if (TYPE_PTR_P (rhstype)
8141 && TYPE_PTR_P (type)
8142 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8143 && CLASS_TYPE_P (TREE_TYPE (type))
8144 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8145 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8146 (TREE_TYPE (rhstype))),
8147 "class type %qT is incomplete", TREE_TYPE (rhstype));
8149 return error_mark_node;
8152 if (warn_suggest_attribute_format)
8154 const enum tree_code codel = TREE_CODE (type);
8155 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8156 && coder == codel
8157 && check_missing_format_attribute (type, rhstype)
8158 && (complain & tf_warning))
8159 switch (errtype)
8161 case ICR_ARGPASS:
8162 case ICR_DEFAULT_ARGUMENT:
8163 if (fndecl)
8164 warning (OPT_Wsuggest_attribute_format,
8165 "parameter %qP of %qD might be a candidate "
8166 "for a format attribute", parmnum, fndecl);
8167 else
8168 warning (OPT_Wsuggest_attribute_format,
8169 "parameter might be a candidate "
8170 "for a format attribute");
8171 break;
8172 case ICR_CONVERTING:
8173 warning (OPT_Wsuggest_attribute_format,
8174 "target of conversion might be a candidate "
8175 "for a format attribute");
8176 break;
8177 case ICR_INIT:
8178 warning (OPT_Wsuggest_attribute_format,
8179 "target of initialization might be a candidate "
8180 "for a format attribute");
8181 break;
8182 case ICR_RETURN:
8183 warning (OPT_Wsuggest_attribute_format,
8184 "return type might be a candidate "
8185 "for a format attribute");
8186 break;
8187 case ICR_ASSIGN:
8188 warning (OPT_Wsuggest_attribute_format,
8189 "left-hand side of assignment might be a candidate "
8190 "for a format attribute");
8191 break;
8192 default:
8193 gcc_unreachable();
8197 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8198 does not. */
8199 if (warn_parentheses
8200 && TREE_CODE (type) == BOOLEAN_TYPE
8201 && TREE_CODE (rhs) == MODIFY_EXPR
8202 && !TREE_NO_WARNING (rhs)
8203 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8204 && (complain & tf_warning))
8206 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8208 warning_at (loc, OPT_Wparentheses,
8209 "suggest parentheses around assignment used as truth value");
8210 TREE_NO_WARNING (rhs) = 1;
8213 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8214 complain, flags);
8217 /* Convert RHS to be of type TYPE.
8218 If EXP is nonzero, it is the target of the initialization.
8219 ERRTYPE indicates what kind of error the implicit conversion is.
8221 Two major differences between the behavior of
8222 `convert_for_assignment' and `convert_for_initialization'
8223 are that references are bashed in the former, while
8224 copied in the latter, and aggregates are assigned in
8225 the former (operator=) while initialized in the
8226 latter (X(X&)).
8228 If using constructor make sure no conversion operator exists, if one does
8229 exist, an ambiguity exists. */
8231 tree
8232 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8233 impl_conv_rhs errtype, tree fndecl, int parmnum,
8234 tsubst_flags_t complain)
8236 enum tree_code codel = TREE_CODE (type);
8237 tree rhstype;
8238 enum tree_code coder;
8240 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8241 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8242 if (TREE_CODE (rhs) == NOP_EXPR
8243 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8244 && codel != REFERENCE_TYPE)
8245 rhs = TREE_OPERAND (rhs, 0);
8247 if (type == error_mark_node
8248 || rhs == error_mark_node
8249 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8250 return error_mark_node;
8252 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8253 && TREE_CODE (type) != ARRAY_TYPE
8254 && (TREE_CODE (type) != REFERENCE_TYPE
8255 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8256 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8257 && !TYPE_REFFN_P (type))
8258 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8259 rhs = decay_conversion (rhs, complain);
8261 rhstype = TREE_TYPE (rhs);
8262 coder = TREE_CODE (rhstype);
8264 if (coder == ERROR_MARK)
8265 return error_mark_node;
8267 /* We accept references to incomplete types, so we can
8268 return here before checking if RHS is of complete type. */
8270 if (codel == REFERENCE_TYPE)
8272 /* This should eventually happen in convert_arguments. */
8273 int savew = 0, savee = 0;
8275 if (fndecl)
8276 savew = warningcount + werrorcount, savee = errorcount;
8277 rhs = initialize_reference (type, rhs, flags, complain);
8279 if (fndecl
8280 && (warningcount + werrorcount > savew || errorcount > savee))
8281 inform (input_location,
8282 "in passing argument %P of %q+D", parmnum, fndecl);
8284 return rhs;
8287 if (exp != 0)
8288 exp = require_complete_type_sfinae (exp, complain);
8289 if (exp == error_mark_node)
8290 return error_mark_node;
8292 rhstype = non_reference (rhstype);
8294 type = complete_type (type);
8296 if (DIRECT_INIT_EXPR_P (type, rhs))
8297 /* Don't try to do copy-initialization if we already have
8298 direct-initialization. */
8299 return rhs;
8301 if (MAYBE_CLASS_TYPE_P (type))
8302 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8304 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8305 complain, flags);
8308 /* If RETVAL is the address of, or a reference to, a local variable or
8309 temporary give an appropriate warning and return true. */
8311 static bool
8312 maybe_warn_about_returning_address_of_local (tree retval)
8314 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8315 tree whats_returned = retval;
8317 for (;;)
8319 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8320 whats_returned = TREE_OPERAND (whats_returned, 1);
8321 else if (CONVERT_EXPR_P (whats_returned)
8322 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8323 whats_returned = TREE_OPERAND (whats_returned, 0);
8324 else
8325 break;
8328 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8329 return false;
8330 whats_returned = TREE_OPERAND (whats_returned, 0);
8332 while (TREE_CODE (whats_returned) == COMPONENT_REF
8333 || TREE_CODE (whats_returned) == ARRAY_REF)
8334 whats_returned = TREE_OPERAND (whats_returned, 0);
8336 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8338 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8339 || TREE_CODE (whats_returned) == TARGET_EXPR)
8341 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8342 return true;
8344 if (VAR_P (whats_returned)
8345 && DECL_NAME (whats_returned)
8346 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8348 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8349 return true;
8353 if (DECL_P (whats_returned)
8354 && DECL_NAME (whats_returned)
8355 && DECL_FUNCTION_SCOPE_P (whats_returned)
8356 && !is_capture_proxy (whats_returned)
8357 && !(TREE_STATIC (whats_returned)
8358 || TREE_PUBLIC (whats_returned)))
8360 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8361 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8362 whats_returned);
8363 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8364 warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
8365 whats_returned);
8366 else
8367 warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
8368 "returned", whats_returned);
8369 return true;
8372 return false;
8375 /* Check that returning RETVAL from the current function is valid.
8376 Return an expression explicitly showing all conversions required to
8377 change RETVAL into the function return type, and to assign it to
8378 the DECL_RESULT for the function. Set *NO_WARNING to true if
8379 code reaches end of non-void function warning shouldn't be issued
8380 on this RETURN_EXPR. */
8382 tree
8383 check_return_expr (tree retval, bool *no_warning)
8385 tree result;
8386 /* The type actually returned by the function. */
8387 tree valtype;
8388 /* The type the function is declared to return, or void if
8389 the declared type is incomplete. */
8390 tree functype;
8391 int fn_returns_value_p;
8392 bool named_return_value_okay_p;
8394 *no_warning = false;
8396 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8398 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8399 "statement is not allowed");
8400 return NULL_TREE;
8403 /* A `volatile' function is one that isn't supposed to return, ever.
8404 (This is a G++ extension, used to get better code for functions
8405 that call the `volatile' function.) */
8406 if (TREE_THIS_VOLATILE (current_function_decl))
8407 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8409 /* Check for various simple errors. */
8410 if (DECL_DESTRUCTOR_P (current_function_decl))
8412 if (retval)
8413 error ("returning a value from a destructor");
8414 return NULL_TREE;
8416 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8418 if (in_function_try_handler)
8419 /* If a return statement appears in a handler of the
8420 function-try-block of a constructor, the program is ill-formed. */
8421 error ("cannot return from a handler of a function-try-block of a constructor");
8422 else if (retval)
8423 /* You can't return a value from a constructor. */
8424 error ("returning a value from a constructor");
8425 return NULL_TREE;
8428 if (processing_template_decl)
8430 current_function_returns_value = 1;
8431 if (check_for_bare_parameter_packs (retval))
8432 retval = error_mark_node;
8433 return retval;
8436 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8438 /* Deduce auto return type from a return statement. */
8439 if (current_function_auto_return_pattern)
8441 tree auto_node;
8442 tree type;
8444 if (!retval && !is_auto (current_function_auto_return_pattern))
8446 /* Give a helpful error message. */
8447 error ("return-statement with no value, in function returning %qT",
8448 current_function_auto_return_pattern);
8449 inform (input_location, "only plain %<auto%> return type can be "
8450 "deduced to %<void%>");
8451 type = error_mark_node;
8453 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8455 error ("returning initializer list");
8456 type = error_mark_node;
8458 else
8460 if (!retval)
8461 retval = void_node;
8462 auto_node = type_uses_auto (current_function_auto_return_pattern);
8463 type = do_auto_deduction (current_function_auto_return_pattern,
8464 retval, auto_node);
8467 if (type == error_mark_node)
8468 /* Leave it. */;
8469 else if (functype == current_function_auto_return_pattern)
8470 apply_deduced_return_type (current_function_decl, type);
8471 else
8472 /* A mismatch should have been diagnosed in do_auto_deduction. */
8473 gcc_assert (same_type_p (type, functype));
8474 functype = type;
8477 /* When no explicit return-value is given in a function with a named
8478 return value, the named return value is used. */
8479 result = DECL_RESULT (current_function_decl);
8480 valtype = TREE_TYPE (result);
8481 gcc_assert (valtype != NULL_TREE);
8482 fn_returns_value_p = !VOID_TYPE_P (valtype);
8483 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8484 retval = result;
8486 /* Check for a return statement with no return value in a function
8487 that's supposed to return a value. */
8488 if (!retval && fn_returns_value_p)
8490 if (functype != error_mark_node)
8491 permerror (input_location, "return-statement with no value, in "
8492 "function returning %qT", valtype);
8493 /* Remember that this function did return. */
8494 current_function_returns_value = 1;
8495 /* And signal caller that TREE_NO_WARNING should be set on the
8496 RETURN_EXPR to avoid control reaches end of non-void function
8497 warnings in tree-cfg.c. */
8498 *no_warning = true;
8500 /* Check for a return statement with a value in a function that
8501 isn't supposed to return a value. */
8502 else if (retval && !fn_returns_value_p)
8504 if (VOID_TYPE_P (TREE_TYPE (retval)))
8505 /* You can return a `void' value from a function of `void'
8506 type. In that case, we have to evaluate the expression for
8507 its side-effects. */
8508 finish_expr_stmt (retval);
8509 else
8510 permerror (input_location, "return-statement with a value, in function "
8511 "returning 'void'");
8512 current_function_returns_null = 1;
8514 /* There's really no value to return, after all. */
8515 return NULL_TREE;
8517 else if (!retval)
8518 /* Remember that this function can sometimes return without a
8519 value. */
8520 current_function_returns_null = 1;
8521 else
8522 /* Remember that this function did return a value. */
8523 current_function_returns_value = 1;
8525 /* Check for erroneous operands -- but after giving ourselves a
8526 chance to provide an error about returning a value from a void
8527 function. */
8528 if (error_operand_p (retval))
8530 current_function_return_value = error_mark_node;
8531 return error_mark_node;
8534 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8535 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8536 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8537 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8538 && ! flag_check_new
8539 && retval && null_ptr_cst_p (retval))
8540 warning (0, "%<operator new%> must not return NULL unless it is "
8541 "declared %<throw()%> (or -fcheck-new is in effect)");
8543 /* Effective C++ rule 15. See also start_function. */
8544 if (warn_ecpp
8545 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8547 bool warn = true;
8549 /* The function return type must be a reference to the current
8550 class. */
8551 if (TREE_CODE (valtype) == REFERENCE_TYPE
8552 && same_type_ignoring_top_level_qualifiers_p
8553 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8555 /* Returning '*this' is obviously OK. */
8556 if (retval == current_class_ref)
8557 warn = false;
8558 /* If we are calling a function whose return type is the same of
8559 the current class reference, it is ok. */
8560 else if (INDIRECT_REF_P (retval)
8561 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8562 warn = false;
8565 if (warn)
8566 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8569 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8571 [...] For a function with a class return type, if the expression
8572 in the return statement is the name of a local object, and the cv-
8573 unqualified type of the local object is the same as the function
8574 return type, an implementation is permitted to omit creating the tem-
8575 porary object to hold the function return value [...]
8577 So, if this is a value-returning function that always returns the same
8578 local variable, remember it.
8580 It might be nice to be more flexible, and choose the first suitable
8581 variable even if the function sometimes returns something else, but
8582 then we run the risk of clobbering the variable we chose if the other
8583 returned expression uses the chosen variable somehow. And people expect
8584 this restriction, anyway. (jason 2000-11-19)
8586 See finish_function and finalize_nrv for the rest of this optimization. */
8588 named_return_value_okay_p =
8589 (retval != NULL_TREE
8590 /* Must be a local, automatic variable. */
8591 && VAR_P (retval)
8592 && DECL_CONTEXT (retval) == current_function_decl
8593 && ! TREE_STATIC (retval)
8594 /* And not a lambda or anonymous union proxy. */
8595 && !DECL_HAS_VALUE_EXPR_P (retval)
8596 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8597 /* The cv-unqualified type of the returned value must be the
8598 same as the cv-unqualified return type of the
8599 function. */
8600 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8601 (TYPE_MAIN_VARIANT (functype)))
8602 /* And the returned value must be non-volatile. */
8603 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8605 if (fn_returns_value_p && flag_elide_constructors)
8607 if (named_return_value_okay_p
8608 && (current_function_return_value == NULL_TREE
8609 || current_function_return_value == retval))
8610 current_function_return_value = retval;
8611 else
8612 current_function_return_value = error_mark_node;
8615 /* We don't need to do any conversions when there's nothing being
8616 returned. */
8617 if (!retval)
8618 return NULL_TREE;
8620 /* Do any required conversions. */
8621 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8622 /* No conversions are required. */
8624 else
8626 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8628 /* The functype's return type will have been set to void, if it
8629 was an incomplete type. Just treat this as 'return;' */
8630 if (VOID_TYPE_P (functype))
8631 return error_mark_node;
8633 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8634 treated as an rvalue for the purposes of overload resolution to
8635 favor move constructors over copy constructors.
8637 Note that these conditions are similar to, but not as strict as,
8638 the conditions for the named return value optimization. */
8639 if ((cxx_dialect != cxx98)
8640 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8641 || TREE_CODE (retval) == PARM_DECL)
8642 && DECL_CONTEXT (retval) == current_function_decl
8643 && !TREE_STATIC (retval)
8644 /* This is only interesting for class type. */
8645 && CLASS_TYPE_P (functype))
8646 flags = flags | LOOKUP_PREFER_RVALUE;
8648 /* First convert the value to the function's return type, then
8649 to the type of return value's location to handle the
8650 case that functype is smaller than the valtype. */
8651 retval = convert_for_initialization
8652 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8653 tf_warning_or_error);
8654 retval = convert (valtype, retval);
8656 /* If the conversion failed, treat this just like `return;'. */
8657 if (retval == error_mark_node)
8658 return retval;
8659 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8660 else if (! cfun->returns_struct
8661 && TREE_CODE (retval) == TARGET_EXPR
8662 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8663 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8664 TREE_OPERAND (retval, 0));
8665 else if (maybe_warn_about_returning_address_of_local (retval))
8666 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8667 build_zero_cst (TREE_TYPE (retval)));
8670 /* Actually copy the value returned into the appropriate location. */
8671 if (retval && retval != result)
8672 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8674 return retval;
8678 /* Returns nonzero if the pointer-type FROM can be converted to the
8679 pointer-type TO via a qualification conversion. If CONSTP is -1,
8680 then we return nonzero if the pointers are similar, and the
8681 cv-qualification signature of FROM is a proper subset of that of TO.
8683 If CONSTP is positive, then all outer pointers have been
8684 const-qualified. */
8686 static int
8687 comp_ptr_ttypes_real (tree to, tree from, int constp)
8689 bool to_more_cv_qualified = false;
8690 bool is_opaque_pointer = false;
8692 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8694 if (TREE_CODE (to) != TREE_CODE (from))
8695 return 0;
8697 if (TREE_CODE (from) == OFFSET_TYPE
8698 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8699 TYPE_OFFSET_BASETYPE (to)))
8700 return 0;
8702 /* Const and volatile mean something different for function types,
8703 so the usual checks are not appropriate. */
8704 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8706 if (!at_least_as_qualified_p (to, from))
8707 return 0;
8709 if (!at_least_as_qualified_p (from, to))
8711 if (constp == 0)
8712 return 0;
8713 to_more_cv_qualified = true;
8716 if (constp > 0)
8717 constp &= TYPE_READONLY (to);
8720 if (TREE_CODE (to) == VECTOR_TYPE)
8721 is_opaque_pointer = vector_targets_convertible_p (to, from);
8723 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8724 return ((constp >= 0 || to_more_cv_qualified)
8725 && (is_opaque_pointer
8726 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8730 /* When comparing, say, char ** to char const **, this function takes
8731 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8732 types to this function. */
8735 comp_ptr_ttypes (tree to, tree from)
8737 return comp_ptr_ttypes_real (to, from, 1);
8740 /* Returns true iff FNTYPE is a non-class type that involves
8741 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8742 if a parameter type is ill-formed. */
8744 bool
8745 error_type_p (const_tree type)
8747 tree t;
8749 switch (TREE_CODE (type))
8751 case ERROR_MARK:
8752 return true;
8754 case POINTER_TYPE:
8755 case REFERENCE_TYPE:
8756 case OFFSET_TYPE:
8757 return error_type_p (TREE_TYPE (type));
8759 case FUNCTION_TYPE:
8760 case METHOD_TYPE:
8761 if (error_type_p (TREE_TYPE (type)))
8762 return true;
8763 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8764 if (error_type_p (TREE_VALUE (t)))
8765 return true;
8766 return false;
8768 case RECORD_TYPE:
8769 if (TYPE_PTRMEMFUNC_P (type))
8770 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8771 return false;
8773 default:
8774 return false;
8778 /* Returns true if to and from are (possibly multi-level) pointers to the same
8779 type or inheritance-related types, regardless of cv-quals. */
8781 bool
8782 ptr_reasonably_similar (const_tree to, const_tree from)
8784 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8786 /* Any target type is similar enough to void. */
8787 if (VOID_TYPE_P (to))
8788 return !error_type_p (from);
8789 if (VOID_TYPE_P (from))
8790 return !error_type_p (to);
8792 if (TREE_CODE (to) != TREE_CODE (from))
8793 return false;
8795 if (TREE_CODE (from) == OFFSET_TYPE
8796 && comptypes (TYPE_OFFSET_BASETYPE (to),
8797 TYPE_OFFSET_BASETYPE (from),
8798 COMPARE_BASE | COMPARE_DERIVED))
8799 continue;
8801 if (TREE_CODE (to) == VECTOR_TYPE
8802 && vector_types_convertible_p (to, from, false))
8803 return true;
8805 if (TREE_CODE (to) == INTEGER_TYPE
8806 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8807 return true;
8809 if (TREE_CODE (to) == FUNCTION_TYPE)
8810 return !error_type_p (to) && !error_type_p (from);
8812 if (!TYPE_PTR_P (to))
8814 /* When either type is incomplete avoid DERIVED_FROM_P,
8815 which may call complete_type (c++/57942). */
8816 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8817 return comptypes
8818 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8819 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8824 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8825 pointer-to-member types) are the same, ignoring cv-qualification at
8826 all levels. */
8828 bool
8829 comp_ptr_ttypes_const (tree to, tree from)
8831 bool is_opaque_pointer = false;
8833 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8835 if (TREE_CODE (to) != TREE_CODE (from))
8836 return false;
8838 if (TREE_CODE (from) == OFFSET_TYPE
8839 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8840 TYPE_OFFSET_BASETYPE (to)))
8841 continue;
8843 if (TREE_CODE (to) == VECTOR_TYPE)
8844 is_opaque_pointer = vector_targets_convertible_p (to, from);
8846 if (!TYPE_PTR_P (to))
8847 return (is_opaque_pointer
8848 || same_type_ignoring_top_level_qualifiers_p (to, from));
8852 /* Returns the type qualifiers for this type, including the qualifiers on the
8853 elements for an array type. */
8856 cp_type_quals (const_tree type)
8858 int quals;
8859 /* This CONST_CAST is okay because strip_array_types returns its
8860 argument unmodified and we assign it to a const_tree. */
8861 type = strip_array_types (CONST_CAST_TREE (type));
8862 if (type == error_mark_node
8863 /* Quals on a FUNCTION_TYPE are memfn quals. */
8864 || TREE_CODE (type) == FUNCTION_TYPE)
8865 return TYPE_UNQUALIFIED;
8866 quals = TYPE_QUALS (type);
8867 /* METHOD and REFERENCE_TYPEs should never have quals. */
8868 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8869 && TREE_CODE (type) != REFERENCE_TYPE)
8870 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8871 == TYPE_UNQUALIFIED));
8872 return quals;
8875 /* Returns the function-ref-qualifier for TYPE */
8877 cp_ref_qualifier
8878 type_memfn_rqual (const_tree type)
8880 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8881 || TREE_CODE (type) == METHOD_TYPE);
8883 if (!FUNCTION_REF_QUALIFIED (type))
8884 return REF_QUAL_NONE;
8885 else if (FUNCTION_RVALUE_QUALIFIED (type))
8886 return REF_QUAL_RVALUE;
8887 else
8888 return REF_QUAL_LVALUE;
8891 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8892 METHOD_TYPE. */
8895 type_memfn_quals (const_tree type)
8897 if (TREE_CODE (type) == FUNCTION_TYPE)
8898 return TYPE_QUALS (type);
8899 else if (TREE_CODE (type) == METHOD_TYPE)
8900 return cp_type_quals (class_of_this_parm (type));
8901 else
8902 gcc_unreachable ();
8905 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8906 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8908 tree
8909 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8911 /* Could handle METHOD_TYPE here if necessary. */
8912 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8913 if (TYPE_QUALS (type) == memfn_quals
8914 && type_memfn_rqual (type) == rqual)
8915 return type;
8917 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8918 complex. */
8919 tree result = build_qualified_type (type, memfn_quals);
8920 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8921 return build_ref_qualified_type (result, rqual);
8924 /* Returns nonzero if TYPE is const or volatile. */
8926 bool
8927 cv_qualified_p (const_tree type)
8929 int quals = cp_type_quals (type);
8930 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8933 /* Returns nonzero if the TYPE contains a mutable member. */
8935 bool
8936 cp_has_mutable_p (const_tree type)
8938 /* This CONST_CAST is okay because strip_array_types returns its
8939 argument unmodified and we assign it to a const_tree. */
8940 type = strip_array_types (CONST_CAST_TREE(type));
8942 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8945 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8946 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8947 approximation. In particular, consider:
8949 int f();
8950 struct S { int i; };
8951 const S s = { f(); }
8953 Here, we will make "s" as TREE_READONLY (because it is declared
8954 "const") -- only to reverse ourselves upon seeing that the
8955 initializer is non-constant. */
8957 void
8958 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8960 tree type = TREE_TYPE (decl);
8962 if (type == error_mark_node)
8963 return;
8965 if (TREE_CODE (decl) == TYPE_DECL)
8966 return;
8968 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8969 && type_quals != TYPE_UNQUALIFIED));
8971 /* Avoid setting TREE_READONLY incorrectly. */
8972 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8973 constructor can produce constant init, so rely on cp_finish_decl to
8974 clear TREE_READONLY if the variable has non-constant init. */
8976 /* If the type has (or might have) a mutable component, that component
8977 might be modified. */
8978 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8979 type_quals &= ~TYPE_QUAL_CONST;
8981 c_apply_type_quals_to_decl (type_quals, decl);
8984 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8985 exemplar types such that casting T1 to T2 is casting away constness
8986 if and only if there is no implicit conversion from T1 to T2. */
8988 static void
8989 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8991 int quals1;
8992 int quals2;
8994 /* [expr.const.cast]
8996 For multi-level pointer to members and multi-level mixed pointers
8997 and pointers to members (conv.qual), the "member" aspect of a
8998 pointer to member level is ignored when determining if a const
8999 cv-qualifier has been cast away. */
9000 /* [expr.const.cast]
9002 For two pointer types:
9004 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9005 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9006 K is min(N,M)
9008 casting from X1 to X2 casts away constness if, for a non-pointer
9009 type T there does not exist an implicit conversion (clause
9010 _conv_) from:
9012 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9016 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9017 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9018 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9020 *t1 = cp_build_qualified_type (void_type_node,
9021 cp_type_quals (*t1));
9022 *t2 = cp_build_qualified_type (void_type_node,
9023 cp_type_quals (*t2));
9024 return;
9027 quals1 = cp_type_quals (*t1);
9028 quals2 = cp_type_quals (*t2);
9030 if (TYPE_PTRDATAMEM_P (*t1))
9031 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9032 else
9033 *t1 = TREE_TYPE (*t1);
9034 if (TYPE_PTRDATAMEM_P (*t2))
9035 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9036 else
9037 *t2 = TREE_TYPE (*t2);
9039 casts_away_constness_r (t1, t2, complain);
9040 *t1 = build_pointer_type (*t1);
9041 *t2 = build_pointer_type (*t2);
9042 *t1 = cp_build_qualified_type (*t1, quals1);
9043 *t2 = cp_build_qualified_type (*t2, quals2);
9046 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9047 constness.
9049 ??? This function returns non-zero if casting away qualifiers not
9050 just const. We would like to return to the caller exactly which
9051 qualifiers are casted away to give more accurate diagnostics.
9054 static bool
9055 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9057 if (TREE_CODE (t2) == REFERENCE_TYPE)
9059 /* [expr.const.cast]
9061 Casting from an lvalue of type T1 to an lvalue of type T2
9062 using a reference cast casts away constness if a cast from an
9063 rvalue of type "pointer to T1" to the type "pointer to T2"
9064 casts away constness. */
9065 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9066 return casts_away_constness (build_pointer_type (t1),
9067 build_pointer_type (TREE_TYPE (t2)),
9068 complain);
9071 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9072 /* [expr.const.cast]
9074 Casting from an rvalue of type "pointer to data member of X
9075 of type T1" to the type "pointer to data member of Y of type
9076 T2" casts away constness if a cast from an rvalue of type
9077 "pointer to T1" to the type "pointer to T2" casts away
9078 constness. */
9079 return casts_away_constness
9080 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9081 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9082 complain);
9084 /* Casting away constness is only something that makes sense for
9085 pointer or reference types. */
9086 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9087 return false;
9089 /* Top-level qualifiers don't matter. */
9090 t1 = TYPE_MAIN_VARIANT (t1);
9091 t2 = TYPE_MAIN_VARIANT (t2);
9092 casts_away_constness_r (&t1, &t2, complain);
9093 if (!can_convert (t2, t1, complain))
9094 return true;
9096 return false;
9099 /* If T is a REFERENCE_TYPE return the type to which T refers.
9100 Otherwise, return T itself. */
9102 tree
9103 non_reference (tree t)
9105 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9106 t = TREE_TYPE (t);
9107 return t;
9111 /* Return nonzero if REF is an lvalue valid for this language;
9112 otherwise, print an error message and return zero. USE says
9113 how the lvalue is being used and so selects the error message. */
9116 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9118 cp_lvalue_kind kind = lvalue_kind (ref);
9120 if (kind == clk_none)
9122 if (complain & tf_error)
9123 lvalue_error (input_location, use);
9124 return 0;
9126 else if (kind & (clk_rvalueref|clk_class))
9128 if (!(complain & tf_error))
9129 return 0;
9130 if (kind & clk_class)
9131 /* Make this a permerror because we used to accept it. */
9132 permerror (input_location, "using temporary as lvalue");
9133 else
9134 error ("using xvalue (rvalue reference) as lvalue");
9136 return 1;
9139 /* Return true if a user-defined literal operator is a raw operator. */
9141 bool
9142 check_raw_literal_operator (const_tree decl)
9144 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9145 tree argtype;
9146 int arity;
9147 bool maybe_raw_p = false;
9149 /* Count the number and type of arguments and check for ellipsis. */
9150 for (argtype = argtypes, arity = 0;
9151 argtype && argtype != void_list_node;
9152 ++arity, argtype = TREE_CHAIN (argtype))
9154 tree t = TREE_VALUE (argtype);
9156 if (same_type_p (t, const_string_type_node))
9157 maybe_raw_p = true;
9159 if (!argtype)
9160 return false; /* Found ellipsis. */
9162 if (!maybe_raw_p || arity != 1)
9163 return false;
9165 return true;
9169 /* Return true if a user-defined literal operator has one of the allowed
9170 argument types. */
9172 bool
9173 check_literal_operator_args (const_tree decl,
9174 bool *long_long_unsigned_p, bool *long_double_p)
9176 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9178 *long_long_unsigned_p = false;
9179 *long_double_p = false;
9180 if (processing_template_decl || processing_specialization)
9181 return argtypes == void_list_node;
9182 else
9184 tree argtype;
9185 int arity;
9186 int max_arity = 2;
9188 /* Count the number and type of arguments and check for ellipsis. */
9189 for (argtype = argtypes, arity = 0;
9190 argtype && argtype != void_list_node;
9191 argtype = TREE_CHAIN (argtype))
9193 tree t = TREE_VALUE (argtype);
9194 ++arity;
9196 if (TYPE_PTR_P (t))
9198 bool maybe_raw_p = false;
9199 t = TREE_TYPE (t);
9200 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9201 return false;
9202 t = TYPE_MAIN_VARIANT (t);
9203 if ((maybe_raw_p = same_type_p (t, char_type_node))
9204 || same_type_p (t, wchar_type_node)
9205 || same_type_p (t, char16_type_node)
9206 || same_type_p (t, char32_type_node))
9208 argtype = TREE_CHAIN (argtype);
9209 if (!argtype)
9210 return false;
9211 t = TREE_VALUE (argtype);
9212 if (maybe_raw_p && argtype == void_list_node)
9213 return true;
9214 else if (same_type_p (t, size_type_node))
9216 ++arity;
9217 continue;
9219 else
9220 return false;
9223 else if (same_type_p (t, long_long_unsigned_type_node))
9225 max_arity = 1;
9226 *long_long_unsigned_p = true;
9228 else if (same_type_p (t, long_double_type_node))
9230 max_arity = 1;
9231 *long_double_p = true;
9233 else if (same_type_p (t, char_type_node))
9234 max_arity = 1;
9235 else if (same_type_p (t, wchar_type_node))
9236 max_arity = 1;
9237 else if (same_type_p (t, char16_type_node))
9238 max_arity = 1;
9239 else if (same_type_p (t, char32_type_node))
9240 max_arity = 1;
9241 else
9242 return false;
9244 if (!argtype)
9245 return false; /* Found ellipsis. */
9247 if (arity != max_arity)
9248 return false;
9250 return true;