2013-06-18 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cp / typeck.c
blob11ac85b65cd920c839269772799ee047606a64b5
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2013 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 "cp-tree.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "intl.h"
36 #include "target.h"
37 #include "convert.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "params.h"
42 static tree pfn_from_ptrmemfunc (tree);
43 static tree delta_from_ptrmemfunc (tree);
44 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
45 tsubst_flags_t, int);
46 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
47 static tree rationalize_conditional_expr (enum tree_code, tree,
48 tsubst_flags_t);
49 static int comp_ptr_ttypes_real (tree, tree, int);
50 static bool comp_except_types (tree, tree, bool);
51 static bool comp_array_types (const_tree, const_tree, bool);
52 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
53 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
54 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
55 static bool casts_away_constness (tree, tree, tsubst_flags_t);
56 static void maybe_warn_about_returning_address_of_local (tree);
57 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
58 static void warn_args_num (location_t, tree, bool);
59 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
60 tsubst_flags_t);
62 /* Do `exp = require_complete_type (exp);' to make sure exp
63 does not have an incomplete type. (That includes void types.)
64 Returns error_mark_node if the VALUE does not have
65 complete type when this function returns. */
67 tree
68 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
70 tree type;
72 if (processing_template_decl || value == error_mark_node)
73 return value;
75 if (TREE_CODE (value) == OVERLOAD)
76 type = unknown_type_node;
77 else
78 type = TREE_TYPE (value);
80 if (type == error_mark_node)
81 return error_mark_node;
83 /* First, detect a valid value with a complete type. */
84 if (COMPLETE_TYPE_P (type))
85 return value;
87 if (complete_type_or_maybe_complain (type, value, complain))
88 return value;
89 else
90 return error_mark_node;
93 tree
94 require_complete_type (tree value)
96 return require_complete_type_sfinae (value, tf_warning_or_error);
99 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
100 a template instantiation, do the instantiation. Returns TYPE,
101 whether or not it could be completed, unless something goes
102 horribly wrong, in which case the error_mark_node is returned. */
104 tree
105 complete_type (tree type)
107 if (type == NULL_TREE)
108 /* Rather than crash, we return something sure to cause an error
109 at some point. */
110 return error_mark_node;
112 if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
116 tree t = complete_type (TREE_TYPE (type));
117 unsigned int needs_constructing, has_nontrivial_dtor;
118 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
119 layout_type (type);
120 needs_constructing
121 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
122 has_nontrivial_dtor
123 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
124 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
127 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
130 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
131 instantiate_class_template (TYPE_MAIN_VARIANT (type));
133 return type;
136 /* Like complete_type, but issue an error if the TYPE cannot be completed.
137 VALUE is used for informative diagnostics.
138 Returns NULL_TREE if the type cannot be made complete. */
140 tree
141 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
143 type = complete_type (type);
144 if (type == error_mark_node)
145 /* We already issued an error. */
146 return NULL_TREE;
147 else if (!COMPLETE_TYPE_P (type))
149 if (complain & tf_error)
150 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
151 return NULL_TREE;
153 else
154 return type;
157 tree
158 complete_type_or_else (tree type, tree value)
160 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
163 /* Return truthvalue of whether type of EXP is instantiated. */
166 type_unknown_p (const_tree exp)
168 return (TREE_CODE (exp) == TREE_LIST
169 || TREE_TYPE (exp) == unknown_type_node);
173 /* Return the common type of two parameter lists.
174 We assume that comptypes has already been done and returned 1;
175 if that isn't so, this may crash.
177 As an optimization, free the space we allocate if the parameter
178 lists are already common. */
180 static tree
181 commonparms (tree p1, tree p2)
183 tree oldargs = p1, newargs, n;
184 int i, len;
185 int any_change = 0;
187 len = list_length (p1);
188 newargs = tree_last (p1);
190 if (newargs == void_list_node)
191 i = 1;
192 else
194 i = 0;
195 newargs = 0;
198 for (; i < len; i++)
199 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
201 n = newargs;
203 for (i = 0; p1;
204 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
206 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
208 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 any_change = 1;
211 else if (! TREE_PURPOSE (p1))
213 if (TREE_PURPOSE (p2))
215 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 any_change = 1;
219 else
221 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
222 any_change = 1;
223 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
225 if (TREE_VALUE (p1) != TREE_VALUE (p2))
227 any_change = 1;
228 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
230 else
231 TREE_VALUE (n) = TREE_VALUE (p1);
233 if (! any_change)
234 return oldargs;
236 return newargs;
239 /* Given a type, perhaps copied for a typedef,
240 find the "original" version of it. */
241 static tree
242 original_type (tree t)
244 int quals = cp_type_quals (t);
245 while (t != error_mark_node
246 && TYPE_NAME (t) != NULL_TREE)
248 tree x = TYPE_NAME (t);
249 if (TREE_CODE (x) != TYPE_DECL)
250 break;
251 x = DECL_ORIGINAL_TYPE (x);
252 if (x == NULL_TREE)
253 break;
254 t = x;
256 return cp_build_qualified_type (t, quals);
259 /* Return the common type for two arithmetic types T1 and T2 under the
260 usual arithmetic conversions. The default conversions have already
261 been applied, and enumerated types converted to their compatible
262 integer types. */
264 static tree
265 cp_common_type (tree t1, tree t2)
267 enum tree_code code1 = TREE_CODE (t1);
268 enum tree_code code2 = TREE_CODE (t2);
269 tree attributes;
272 /* In what follows, we slightly generalize the rules given in [expr] so
273 as to deal with `long long' and `complex'. First, merge the
274 attributes. */
275 attributes = (*targetm.merge_type_attributes) (t1, t2);
277 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
279 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
280 return build_type_attribute_variant (t1, attributes);
281 else
282 return NULL_TREE;
285 /* FIXME: Attributes. */
286 gcc_assert (ARITHMETIC_TYPE_P (t1)
287 || TREE_CODE (t1) == VECTOR_TYPE
288 || UNSCOPED_ENUM_P (t1));
289 gcc_assert (ARITHMETIC_TYPE_P (t2)
290 || TREE_CODE (t2) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t2));
293 /* If one type is complex, form the common type of the non-complex
294 components, then make that complex. Use T1 or T2 if it is the
295 required type. */
296 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
298 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
299 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
300 tree subtype
301 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
303 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
304 return build_type_attribute_variant (t1, attributes);
305 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
306 return build_type_attribute_variant (t2, attributes);
307 else
308 return build_type_attribute_variant (build_complex_type (subtype),
309 attributes);
312 if (code1 == VECTOR_TYPE)
314 /* When we get here we should have two vectors of the same size.
315 Just prefer the unsigned one if present. */
316 if (TYPE_UNSIGNED (t1))
317 return build_type_attribute_variant (t1, attributes);
318 else
319 return build_type_attribute_variant (t2, attributes);
322 /* If only one is real, use it as the result. */
323 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
324 return build_type_attribute_variant (t1, attributes);
325 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
326 return build_type_attribute_variant (t2, attributes);
328 /* Both real or both integers; use the one with greater precision. */
329 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
330 return build_type_attribute_variant (t1, attributes);
331 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
332 return build_type_attribute_variant (t2, attributes);
334 /* The types are the same; no need to do anything fancy. */
335 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
336 return build_type_attribute_variant (t1, attributes);
338 if (code1 != REAL_TYPE)
340 /* If one is unsigned long long, then convert the other to unsigned
341 long long. */
342 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
343 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
344 return build_type_attribute_variant (long_long_unsigned_type_node,
345 attributes);
346 /* If one is a long long, and the other is an unsigned long, and
347 long long can represent all the values of an unsigned long, then
348 convert to a long long. Otherwise, convert to an unsigned long
349 long. Otherwise, if either operand is long long, convert the
350 other to long long.
352 Since we're here, we know the TYPE_PRECISION is the same;
353 therefore converting to long long cannot represent all the values
354 of an unsigned long, so we choose unsigned long long in that
355 case. */
356 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
357 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
359 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
360 ? long_long_unsigned_type_node
361 : long_long_integer_type_node);
362 return build_type_attribute_variant (t, attributes);
364 if (int128_integer_type_node != NULL_TREE
365 && (same_type_p (TYPE_MAIN_VARIANT (t1),
366 int128_integer_type_node)
367 || same_type_p (TYPE_MAIN_VARIANT (t2),
368 int128_integer_type_node)))
370 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
371 ? int128_unsigned_type_node
372 : int128_integer_type_node);
373 return build_type_attribute_variant (t, attributes);
376 /* Go through the same procedure, but for longs. */
377 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
378 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
379 return build_type_attribute_variant (long_unsigned_type_node,
380 attributes);
381 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
382 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
384 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
385 ? long_unsigned_type_node : long_integer_type_node);
386 return build_type_attribute_variant (t, attributes);
388 /* Otherwise prefer the unsigned one. */
389 if (TYPE_UNSIGNED (t1))
390 return build_type_attribute_variant (t1, attributes);
391 else
392 return build_type_attribute_variant (t2, attributes);
394 else
396 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
397 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
398 return build_type_attribute_variant (long_double_type_node,
399 attributes);
400 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
401 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
402 return build_type_attribute_variant (double_type_node,
403 attributes);
404 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
405 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
406 return build_type_attribute_variant (float_type_node,
407 attributes);
409 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
410 the standard C++ floating-point types. Logic earlier in this
411 function has already eliminated the possibility that
412 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
413 compelling reason to choose one or the other. */
414 return build_type_attribute_variant (t1, attributes);
418 /* T1 and T2 are arithmetic or enumeration types. Return the type
419 that will result from the "usual arithmetic conversions" on T1 and
420 T2 as described in [expr]. */
422 tree
423 type_after_usual_arithmetic_conversions (tree t1, tree t2)
425 gcc_assert (ARITHMETIC_TYPE_P (t1)
426 || TREE_CODE (t1) == VECTOR_TYPE
427 || UNSCOPED_ENUM_P (t1));
428 gcc_assert (ARITHMETIC_TYPE_P (t2)
429 || TREE_CODE (t2) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t2));
432 /* Perform the integral promotions. We do not promote real types here. */
433 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
434 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
436 t1 = type_promotes_to (t1);
437 t2 = type_promotes_to (t2);
440 return cp_common_type (t1, t2);
443 static void
444 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
445 composite_pointer_operation operation)
447 switch (operation)
449 case CPO_COMPARISON:
450 emit_diagnostic (kind, input_location, 0,
451 "comparison between "
452 "distinct pointer types %qT and %qT lacks a cast",
453 t1, t2);
454 break;
455 case CPO_CONVERSION:
456 emit_diagnostic (kind, input_location, 0,
457 "conversion between "
458 "distinct pointer types %qT and %qT lacks a cast",
459 t1, t2);
460 break;
461 case CPO_CONDITIONAL_EXPR:
462 emit_diagnostic (kind, input_location, 0,
463 "conditional expression between "
464 "distinct pointer types %qT and %qT lacks a cast",
465 t1, t2);
466 break;
467 default:
468 gcc_unreachable ();
472 /* Subroutine of composite_pointer_type to implement the recursive
473 case. See that function for documentation of the parameters. */
475 static tree
476 composite_pointer_type_r (tree t1, tree t2,
477 composite_pointer_operation operation,
478 tsubst_flags_t complain)
480 tree pointee1;
481 tree pointee2;
482 tree result_type;
483 tree attributes;
485 /* Determine the types pointed to by T1 and T2. */
486 if (TYPE_PTR_P (t1))
488 pointee1 = TREE_TYPE (t1);
489 pointee2 = TREE_TYPE (t2);
491 else
493 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
494 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
497 /* [expr.rel]
499 Otherwise, the composite pointer type is a pointer type
500 similar (_conv.qual_) to the type of one of the operands,
501 with a cv-qualification signature (_conv.qual_) that is the
502 union of the cv-qualification signatures of the operand
503 types. */
504 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
505 result_type = pointee1;
506 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
507 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
509 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
510 complain);
511 if (result_type == error_mark_node)
512 return error_mark_node;
514 else
516 if (complain & tf_error)
517 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
518 else
519 return error_mark_node;
520 result_type = void_type_node;
522 result_type = cp_build_qualified_type (result_type,
523 (cp_type_quals (pointee1)
524 | cp_type_quals (pointee2)));
525 /* If the original types were pointers to members, so is the
526 result. */
527 if (TYPE_PTRMEM_P (t1))
529 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
530 TYPE_PTRMEM_CLASS_TYPE (t2)))
532 if (complain & tf_error)
533 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
534 else
535 return error_mark_node;
537 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
538 result_type);
540 else
541 result_type = build_pointer_type (result_type);
543 /* Merge the attributes. */
544 attributes = (*targetm.merge_type_attributes) (t1, t2);
545 return build_type_attribute_variant (result_type, attributes);
548 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
549 ARG1 and ARG2 are the values with those types. The OPERATION is to
550 describe the operation between the pointer types,
551 in case an error occurs.
553 This routine also implements the computation of a common type for
554 pointers-to-members as per [expr.eq]. */
556 tree
557 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
558 composite_pointer_operation operation,
559 tsubst_flags_t complain)
561 tree class1;
562 tree class2;
564 /* [expr.rel]
566 If one operand is a null pointer constant, the composite pointer
567 type is the type of the other operand. */
568 if (null_ptr_cst_p (arg1))
569 return t2;
570 if (null_ptr_cst_p (arg2))
571 return t1;
573 /* We have:
575 [expr.rel]
577 If one of the operands has type "pointer to cv1 void*", then
578 the other has type "pointer to cv2T", and the composite pointer
579 type is "pointer to cv12 void", where cv12 is the union of cv1
580 and cv2.
582 If either type is a pointer to void, make sure it is T1. */
583 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
585 tree t;
586 t = t1;
587 t1 = t2;
588 t2 = t;
591 /* Now, if T1 is a pointer to void, merge the qualifiers. */
592 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
594 tree attributes;
595 tree result_type;
597 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
599 switch (operation)
601 case CPO_COMPARISON:
602 pedwarn (input_location, OPT_Wpedantic,
603 "ISO C++ forbids comparison between "
604 "pointer of type %<void *%> and pointer-to-function");
605 break;
606 case CPO_CONVERSION:
607 pedwarn (input_location, OPT_Wpedantic,
608 "ISO C++ forbids conversion between "
609 "pointer of type %<void *%> and pointer-to-function");
610 break;
611 case CPO_CONDITIONAL_EXPR:
612 pedwarn (input_location, OPT_Wpedantic,
613 "ISO C++ forbids conditional expression between "
614 "pointer of type %<void *%> and pointer-to-function");
615 break;
616 default:
617 gcc_unreachable ();
620 result_type
621 = cp_build_qualified_type (void_type_node,
622 (cp_type_quals (TREE_TYPE (t1))
623 | cp_type_quals (TREE_TYPE (t2))));
624 result_type = build_pointer_type (result_type);
625 /* Merge the attributes. */
626 attributes = (*targetm.merge_type_attributes) (t1, t2);
627 return build_type_attribute_variant (result_type, attributes);
630 if (c_dialect_objc () && TYPE_PTR_P (t1)
631 && TYPE_PTR_P (t2))
633 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
634 return objc_common_type (t1, t2);
637 /* [expr.eq] permits the application of a pointer conversion to
638 bring the pointers to a common type. */
639 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
640 && CLASS_TYPE_P (TREE_TYPE (t1))
641 && CLASS_TYPE_P (TREE_TYPE (t2))
642 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
643 TREE_TYPE (t2)))
645 class1 = TREE_TYPE (t1);
646 class2 = TREE_TYPE (t2);
648 if (DERIVED_FROM_P (class1, class2))
649 t2 = (build_pointer_type
650 (cp_build_qualified_type (class1, cp_type_quals (class2))));
651 else if (DERIVED_FROM_P (class2, class1))
652 t1 = (build_pointer_type
653 (cp_build_qualified_type (class2, cp_type_quals (class1))));
654 else
656 if (complain & tf_error)
657 composite_pointer_error (DK_ERROR, t1, t2, operation);
658 return error_mark_node;
661 /* [expr.eq] permits the application of a pointer-to-member
662 conversion to change the class type of one of the types. */
663 else if (TYPE_PTRMEM_P (t1)
664 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
665 TYPE_PTRMEM_CLASS_TYPE (t2)))
667 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
668 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
670 if (DERIVED_FROM_P (class1, class2))
671 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
672 else if (DERIVED_FROM_P (class2, class1))
673 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
674 else
676 if (complain & tf_error)
677 switch (operation)
679 case CPO_COMPARISON:
680 error ("comparison between distinct "
681 "pointer-to-member types %qT and %qT lacks a cast",
682 t1, t2);
683 break;
684 case CPO_CONVERSION:
685 error ("conversion between distinct "
686 "pointer-to-member types %qT and %qT lacks a cast",
687 t1, t2);
688 break;
689 case CPO_CONDITIONAL_EXPR:
690 error ("conditional expression between distinct "
691 "pointer-to-member types %qT and %qT lacks a cast",
692 t1, t2);
693 break;
694 default:
695 gcc_unreachable ();
697 return error_mark_node;
701 return composite_pointer_type_r (t1, t2, operation, complain);
704 /* Return the merged type of two types.
705 We assume that comptypes has already been done and returned 1;
706 if that isn't so, this may crash.
708 This just combines attributes and default arguments; any other
709 differences would cause the two types to compare unalike. */
711 tree
712 merge_types (tree t1, tree t2)
714 enum tree_code code1;
715 enum tree_code code2;
716 tree attributes;
718 /* Save time if the two types are the same. */
719 if (t1 == t2)
720 return t1;
721 if (original_type (t1) == original_type (t2))
722 return t1;
724 /* If one type is nonsense, use the other. */
725 if (t1 == error_mark_node)
726 return t2;
727 if (t2 == error_mark_node)
728 return t1;
730 /* Handle merging an auto redeclaration with a previous deduced
731 return type. */
732 if (is_auto (t1))
733 return t2;
735 /* Merge the attributes. */
736 attributes = (*targetm.merge_type_attributes) (t1, t2);
738 if (TYPE_PTRMEMFUNC_P (t1))
739 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
740 if (TYPE_PTRMEMFUNC_P (t2))
741 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
743 code1 = TREE_CODE (t1);
744 code2 = TREE_CODE (t2);
745 if (code1 != code2)
747 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
748 if (code1 == TYPENAME_TYPE)
750 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
751 code1 = TREE_CODE (t1);
753 else
755 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
756 code2 = TREE_CODE (t2);
760 switch (code1)
762 case POINTER_TYPE:
763 case REFERENCE_TYPE:
764 /* For two pointers, do this recursively on the target type. */
766 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
767 int quals = cp_type_quals (t1);
769 if (code1 == POINTER_TYPE)
770 t1 = build_pointer_type (target);
771 else
772 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
773 t1 = build_type_attribute_variant (t1, attributes);
774 t1 = cp_build_qualified_type (t1, quals);
776 if (TREE_CODE (target) == METHOD_TYPE)
777 t1 = build_ptrmemfunc_type (t1);
779 return t1;
782 case OFFSET_TYPE:
784 int quals;
785 tree pointee;
786 quals = cp_type_quals (t1);
787 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
788 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
789 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
790 pointee);
791 t1 = cp_build_qualified_type (t1, quals);
792 break;
795 case ARRAY_TYPE:
797 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
798 /* Save space: see if the result is identical to one of the args. */
799 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
800 return build_type_attribute_variant (t1, attributes);
801 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
802 return build_type_attribute_variant (t2, attributes);
803 /* Merge the element types, and have a size if either arg has one. */
804 t1 = build_cplus_array_type
805 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
806 break;
809 case FUNCTION_TYPE:
810 /* Function types: prefer the one that specified arg types.
811 If both do, merge the arg types. Also merge the return types. */
813 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
814 tree p1 = TYPE_ARG_TYPES (t1);
815 tree p2 = TYPE_ARG_TYPES (t2);
816 tree parms;
817 tree rval, raises;
819 /* Save space: see if the result is identical to one of the args. */
820 if (valtype == TREE_TYPE (t1) && ! p2)
821 return cp_build_type_attribute_variant (t1, attributes);
822 if (valtype == TREE_TYPE (t2) && ! p1)
823 return cp_build_type_attribute_variant (t2, attributes);
825 /* Simple way if one arg fails to specify argument types. */
826 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
827 parms = p2;
828 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
829 parms = p1;
830 else
831 parms = commonparms (p1, p2);
833 rval = build_function_type (valtype, parms);
834 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
835 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
836 rval = apply_memfn_quals (rval,
837 type_memfn_quals (t1),
838 type_memfn_rqual (t1));
839 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
840 TYPE_RAISES_EXCEPTIONS (t2),
841 NULL_TREE);
842 t1 = build_exception_variant (rval, raises);
843 break;
846 case METHOD_TYPE:
848 /* Get this value the long way, since TYPE_METHOD_BASETYPE
849 is just the main variant of this. */
850 tree basetype = class_of_this_parm (t2);
851 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
852 TYPE_RAISES_EXCEPTIONS (t2),
853 NULL_TREE);
854 cp_ref_qualifier rqual = type_memfn_rqual (t1);
855 tree t3;
857 /* If this was a member function type, get back to the
858 original type of type member function (i.e., without
859 the class instance variable up front. */
860 t1 = build_function_type (TREE_TYPE (t1),
861 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
862 t2 = build_function_type (TREE_TYPE (t2),
863 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
864 t3 = merge_types (t1, t2);
865 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
866 TYPE_ARG_TYPES (t3));
867 t1 = build_exception_variant (t3, raises);
868 t1 = build_ref_qualified_type (t1, rqual);
869 break;
872 case TYPENAME_TYPE:
873 /* There is no need to merge attributes into a TYPENAME_TYPE.
874 When the type is instantiated it will have whatever
875 attributes result from the instantiation. */
876 return t1;
878 default:;
881 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
882 return t1;
883 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
884 return t2;
885 else
886 return cp_build_type_attribute_variant (t1, attributes);
889 /* Return the ARRAY_TYPE type without its domain. */
891 tree
892 strip_array_domain (tree type)
894 tree t2;
895 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
896 if (TYPE_DOMAIN (type) == NULL_TREE)
897 return type;
898 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
899 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
902 /* Wrapper around cp_common_type that is used by c-common.c and other
903 front end optimizations that remove promotions.
905 Return the common type for two arithmetic types T1 and T2 under the
906 usual arithmetic conversions. The default conversions have already
907 been applied, and enumerated types converted to their compatible
908 integer types. */
910 tree
911 common_type (tree t1, tree t2)
913 /* If one type is nonsense, use the other */
914 if (t1 == error_mark_node)
915 return t2;
916 if (t2 == error_mark_node)
917 return t1;
919 return cp_common_type (t1, t2);
922 /* Return the common type of two pointer types T1 and T2. This is the
923 type for the result of most arithmetic operations if the operands
924 have the given two types.
926 We assume that comp_target_types has already been done and returned
927 nonzero; if that isn't so, this may crash. */
929 tree
930 common_pointer_type (tree t1, tree t2)
932 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
933 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
934 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
936 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
937 CPO_CONVERSION, tf_warning_or_error);
940 /* Compare two exception specifier types for exactness or subsetness, if
941 allowed. Returns false for mismatch, true for match (same, or
942 derived and !exact).
944 [except.spec] "If a class X ... objects of class X or any class publicly
945 and unambiguously derived from X. Similarly, if a pointer type Y * ...
946 exceptions of type Y * or that are pointers to any type publicly and
947 unambiguously derived from Y. Otherwise a function only allows exceptions
948 that have the same type ..."
949 This does not mention cv qualifiers and is different to what throw
950 [except.throw] and catch [except.catch] will do. They will ignore the
951 top level cv qualifiers, and allow qualifiers in the pointer to class
952 example.
954 We implement the letter of the standard. */
956 static bool
957 comp_except_types (tree a, tree b, bool exact)
959 if (same_type_p (a, b))
960 return true;
961 else if (!exact)
963 if (cp_type_quals (a) || cp_type_quals (b))
964 return false;
966 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
968 a = TREE_TYPE (a);
969 b = TREE_TYPE (b);
970 if (cp_type_quals (a) || cp_type_quals (b))
971 return false;
974 if (TREE_CODE (a) != RECORD_TYPE
975 || TREE_CODE (b) != RECORD_TYPE)
976 return false;
978 if (publicly_uniquely_derived_p (a, b))
979 return true;
981 return false;
984 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
985 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
986 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
987 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
988 are unordered, but we've already filtered out duplicates. Most lists will
989 be in order, we should try to make use of that. */
991 bool
992 comp_except_specs (const_tree t1, const_tree t2, int exact)
994 const_tree probe;
995 const_tree base;
996 int length = 0;
998 if (t1 == t2)
999 return true;
1001 /* First handle noexcept. */
1002 if (exact < ce_exact)
1004 /* noexcept(false) is compatible with no exception-specification,
1005 and stricter than any spec. */
1006 if (t1 == noexcept_false_spec)
1007 return t2 == NULL_TREE || exact == ce_derived;
1008 /* Even a derived noexcept(false) is compatible with no
1009 exception-specification. */
1010 if (t2 == noexcept_false_spec)
1011 return t1 == NULL_TREE;
1013 /* Otherwise, if we aren't looking for an exact match, noexcept is
1014 equivalent to throw(). */
1015 if (t1 == noexcept_true_spec)
1016 t1 = empty_except_spec;
1017 if (t2 == noexcept_true_spec)
1018 t2 = empty_except_spec;
1021 /* If any noexcept is left, it is only comparable to itself;
1022 either we're looking for an exact match or we're redeclaring a
1023 template with dependent noexcept. */
1024 if ((t1 && TREE_PURPOSE (t1))
1025 || (t2 && TREE_PURPOSE (t2)))
1026 return (t1 && t2
1027 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1029 if (t1 == NULL_TREE) /* T1 is ... */
1030 return t2 == NULL_TREE || exact == ce_derived;
1031 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1032 return t2 != NULL_TREE && !TREE_VALUE (t2);
1033 if (t2 == NULL_TREE) /* T2 is ... */
1034 return false;
1035 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1036 return exact == ce_derived;
1038 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1039 Count how many we find, to determine exactness. For exact matching and
1040 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1041 O(nm). */
1042 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1044 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1046 tree a = TREE_VALUE (probe);
1047 tree b = TREE_VALUE (t2);
1049 if (comp_except_types (a, b, exact))
1051 if (probe == base && exact > ce_derived)
1052 base = TREE_CHAIN (probe);
1053 length++;
1054 break;
1057 if (probe == NULL_TREE)
1058 return false;
1060 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1063 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1064 [] can match [size]. */
1066 static bool
1067 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1069 tree d1;
1070 tree d2;
1071 tree max1, max2;
1073 if (t1 == t2)
1074 return true;
1076 /* The type of the array elements must be the same. */
1077 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1078 return false;
1080 d1 = TYPE_DOMAIN (t1);
1081 d2 = TYPE_DOMAIN (t2);
1083 if (d1 == d2)
1084 return true;
1086 /* If one of the arrays is dimensionless, and the other has a
1087 dimension, they are of different types. However, it is valid to
1088 write:
1090 extern int a[];
1091 int a[3];
1093 by [basic.link]:
1095 declarations for an array object can specify
1096 array types that differ by the presence or absence of a major
1097 array bound (_dcl.array_). */
1098 if (!d1 || !d2)
1099 return allow_redeclaration;
1101 /* Check that the dimensions are the same. */
1103 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1104 return false;
1105 max1 = TYPE_MAX_VALUE (d1);
1106 max2 = TYPE_MAX_VALUE (d2);
1107 if (processing_template_decl && !abi_version_at_least (2)
1108 && !value_dependent_expression_p (max1)
1109 && !value_dependent_expression_p (max2))
1111 /* With abi-1 we do not fold non-dependent array bounds, (and
1112 consequently mangle them incorrectly). We must therefore
1113 fold them here, to verify the domains have the same
1114 value. */
1115 max1 = fold (max1);
1116 max2 = fold (max2);
1119 if (!cp_tree_equal (max1, max2))
1120 return false;
1122 return true;
1125 /* Compare the relative position of T1 and T2 into their respective
1126 template parameter list.
1127 T1 and T2 must be template parameter types.
1128 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1130 static bool
1131 comp_template_parms_position (tree t1, tree t2)
1133 tree index1, index2;
1134 gcc_assert (t1 && t2
1135 && TREE_CODE (t1) == TREE_CODE (t2)
1136 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1137 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1138 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1140 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1141 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1143 /* Then compare their relative position. */
1144 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1145 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1146 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1147 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1148 return false;
1150 return true;
1153 /* Subroutine in comptypes. */
1155 static bool
1156 structural_comptypes (tree t1, tree t2, int strict)
1158 if (t1 == t2)
1159 return true;
1161 /* Suppress errors caused by previously reported errors. */
1162 if (t1 == error_mark_node || t2 == error_mark_node)
1163 return false;
1165 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1167 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1168 current instantiation. */
1169 if (TREE_CODE (t1) == TYPENAME_TYPE)
1170 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1172 if (TREE_CODE (t2) == TYPENAME_TYPE)
1173 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1175 if (TYPE_PTRMEMFUNC_P (t1))
1176 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1177 if (TYPE_PTRMEMFUNC_P (t2))
1178 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1180 /* Different classes of types can't be compatible. */
1181 if (TREE_CODE (t1) != TREE_CODE (t2))
1182 return false;
1184 /* Qualifiers must match. For array types, we will check when we
1185 recur on the array element types. */
1186 if (TREE_CODE (t1) != ARRAY_TYPE
1187 && cp_type_quals (t1) != cp_type_quals (t2))
1188 return false;
1189 if (TREE_CODE (t1) == FUNCTION_TYPE
1190 && type_memfn_quals (t1) != type_memfn_quals (t2))
1191 return false;
1192 /* Need to check this before TYPE_MAIN_VARIANT.
1193 FIXME function qualifiers should really change the main variant. */
1194 if ((TREE_CODE (t1) == FUNCTION_TYPE
1195 || TREE_CODE (t1) == METHOD_TYPE)
1196 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1197 return false;
1198 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1199 return false;
1201 /* Allow for two different type nodes which have essentially the same
1202 definition. Note that we already checked for equality of the type
1203 qualifiers (just above). */
1205 if (TREE_CODE (t1) != ARRAY_TYPE
1206 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1207 return true;
1210 /* Compare the types. Break out if they could be the same. */
1211 switch (TREE_CODE (t1))
1213 case VOID_TYPE:
1214 case BOOLEAN_TYPE:
1215 /* All void and bool types are the same. */
1216 break;
1218 case INTEGER_TYPE:
1219 case FIXED_POINT_TYPE:
1220 case REAL_TYPE:
1221 /* With these nodes, we can't determine type equivalence by
1222 looking at what is stored in the nodes themselves, because
1223 two nodes might have different TYPE_MAIN_VARIANTs but still
1224 represent the same type. For example, wchar_t and int could
1225 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1226 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1227 and are distinct types. On the other hand, int and the
1228 following typedef
1230 typedef int INT __attribute((may_alias));
1232 have identical properties, different TYPE_MAIN_VARIANTs, but
1233 represent the same type. The canonical type system keeps
1234 track of equivalence in this case, so we fall back on it. */
1235 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1237 case TEMPLATE_TEMPLATE_PARM:
1238 case BOUND_TEMPLATE_TEMPLATE_PARM:
1239 if (!comp_template_parms_position (t1, t2))
1240 return false;
1241 if (!comp_template_parms
1242 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1243 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1244 return false;
1245 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1246 break;
1247 /* Don't check inheritance. */
1248 strict = COMPARE_STRICT;
1249 /* Fall through. */
1251 case RECORD_TYPE:
1252 case UNION_TYPE:
1253 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1254 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1255 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1256 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1257 break;
1259 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1260 break;
1261 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1262 break;
1264 return false;
1266 case OFFSET_TYPE:
1267 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1268 strict & ~COMPARE_REDECLARATION))
1269 return false;
1270 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1271 return false;
1272 break;
1274 case REFERENCE_TYPE:
1275 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1276 return false;
1277 /* fall through to checks for pointer types */
1279 case POINTER_TYPE:
1280 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1281 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1282 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1283 return false;
1284 break;
1286 case METHOD_TYPE:
1287 case FUNCTION_TYPE:
1288 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1289 return false;
1290 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1291 return false;
1292 break;
1294 case ARRAY_TYPE:
1295 /* Target types must match incl. qualifiers. */
1296 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1297 return false;
1298 break;
1300 case TEMPLATE_TYPE_PARM:
1301 /* If T1 and T2 don't have the same relative position in their
1302 template parameters set, they can't be equal. */
1303 if (!comp_template_parms_position (t1, t2))
1304 return false;
1305 break;
1307 case TYPENAME_TYPE:
1308 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1309 TYPENAME_TYPE_FULLNAME (t2)))
1310 return false;
1311 /* Qualifiers don't matter on scopes. */
1312 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1313 TYPE_CONTEXT (t2)))
1314 return false;
1315 break;
1317 case UNBOUND_CLASS_TEMPLATE:
1318 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1319 return false;
1320 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1321 return false;
1322 break;
1324 case COMPLEX_TYPE:
1325 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1326 return false;
1327 break;
1329 case VECTOR_TYPE:
1330 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1331 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1332 return false;
1333 break;
1335 case TYPE_PACK_EXPANSION:
1336 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1337 PACK_EXPANSION_PATTERN (t2))
1338 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1339 PACK_EXPANSION_EXTRA_ARGS (t2)));
1341 case DECLTYPE_TYPE:
1342 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1343 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1344 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1345 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1346 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1347 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1348 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1349 DECLTYPE_TYPE_EXPR (t2)))
1350 return false;
1351 break;
1353 case UNDERLYING_TYPE:
1354 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1355 UNDERLYING_TYPE_TYPE (t2));
1357 default:
1358 return false;
1361 /* If we get here, we know that from a target independent POV the
1362 types are the same. Make sure the target attributes are also
1363 the same. */
1364 return comp_type_attributes (t1, t2);
1367 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1368 is a bitwise-or of the COMPARE_* flags. */
1370 bool
1371 comptypes (tree t1, tree t2, int strict)
1373 if (strict == COMPARE_STRICT)
1375 if (t1 == t2)
1376 return true;
1378 if (t1 == error_mark_node || t2 == error_mark_node)
1379 return false;
1381 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1382 /* At least one of the types requires structural equality, so
1383 perform a deep check. */
1384 return structural_comptypes (t1, t2, strict);
1386 #ifdef ENABLE_CHECKING
1387 if (USE_CANONICAL_TYPES)
1389 bool result = structural_comptypes (t1, t2, strict);
1391 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1392 /* The two types are structurally equivalent, but their
1393 canonical types were different. This is a failure of the
1394 canonical type propagation code.*/
1395 internal_error
1396 ("canonical types differ for identical types %T and %T",
1397 t1, t2);
1398 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1399 /* Two types are structurally different, but the canonical
1400 types are the same. This means we were over-eager in
1401 assigning canonical types. */
1402 internal_error
1403 ("same canonical type node for different types %T and %T",
1404 t1, t2);
1406 return result;
1408 #else
1409 if (USE_CANONICAL_TYPES)
1410 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1411 #endif
1412 else
1413 return structural_comptypes (t1, t2, strict);
1415 else if (strict == COMPARE_STRUCTURAL)
1416 return structural_comptypes (t1, t2, COMPARE_STRICT);
1417 else
1418 return structural_comptypes (t1, t2, strict);
1421 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1422 top-level qualifiers. */
1424 bool
1425 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1427 if (type1 == error_mark_node || type2 == error_mark_node)
1428 return false;
1430 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1433 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1435 bool
1436 at_least_as_qualified_p (const_tree type1, const_tree type2)
1438 int q1 = cp_type_quals (type1);
1439 int q2 = cp_type_quals (type2);
1441 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1442 return (q1 & q2) == q2;
1445 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1446 more cv-qualified that TYPE1, and 0 otherwise. */
1449 comp_cv_qualification (const_tree type1, const_tree type2)
1451 int q1 = cp_type_quals (type1);
1452 int q2 = cp_type_quals (type2);
1454 if (q1 == q2)
1455 return 0;
1457 if ((q1 & q2) == q2)
1458 return 1;
1459 else if ((q1 & q2) == q1)
1460 return -1;
1462 return 0;
1465 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1466 subset of the cv-qualification signature of TYPE2, and the types
1467 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1470 comp_cv_qual_signature (tree type1, tree type2)
1472 if (comp_ptr_ttypes_real (type2, type1, -1))
1473 return 1;
1474 else if (comp_ptr_ttypes_real (type1, type2, -1))
1475 return -1;
1476 else
1477 return 0;
1480 /* Subroutines of `comptypes'. */
1482 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1483 equivalent in the sense that functions with those parameter types
1484 can have equivalent types. The two lists must be equivalent,
1485 element by element. */
1487 bool
1488 compparms (const_tree parms1, const_tree parms2)
1490 const_tree t1, t2;
1492 /* An unspecified parmlist matches any specified parmlist
1493 whose argument types don't need default promotions. */
1495 for (t1 = parms1, t2 = parms2;
1496 t1 || t2;
1497 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1499 /* If one parmlist is shorter than the other,
1500 they fail to match. */
1501 if (!t1 || !t2)
1502 return false;
1503 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1504 return false;
1506 return true;
1510 /* Process a sizeof or alignof expression where the operand is a
1511 type. */
1513 tree
1514 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1516 tree value;
1517 bool dependent_p;
1519 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1520 if (type == error_mark_node)
1521 return error_mark_node;
1523 type = non_reference (type);
1524 if (TREE_CODE (type) == METHOD_TYPE)
1526 if (complain)
1527 pedwarn (input_location, OPT_Wpointer_arith,
1528 "invalid application of %qs to a member function",
1529 operator_name_info[(int) op].name);
1530 value = size_one_node;
1533 dependent_p = dependent_type_p (type);
1534 if (!dependent_p)
1535 complete_type (type);
1536 if (dependent_p
1537 /* VLA types will have a non-constant size. In the body of an
1538 uninstantiated template, we don't need to try to compute the
1539 value, because the sizeof expression is not an integral
1540 constant expression in that case. And, if we do try to
1541 compute the value, we'll likely end up with SAVE_EXPRs, which
1542 the template substitution machinery does not expect to see. */
1543 || (processing_template_decl
1544 && COMPLETE_TYPE_P (type)
1545 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1547 value = build_min (op, size_type_node, type);
1548 TREE_READONLY (value) = 1;
1549 return value;
1552 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type))
1554 if (complain & tf_warning_or_error)
1555 pedwarn (input_location, OPT_Wvla,
1556 "taking sizeof array of runtime bound");
1557 else
1558 return error_mark_node;
1561 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1562 op == SIZEOF_EXPR,
1563 complain);
1566 /* Return the size of the type, without producing any warnings for
1567 types whose size cannot be taken. This routine should be used only
1568 in some other routine that has already produced a diagnostic about
1569 using the size of such a type. */
1570 tree
1571 cxx_sizeof_nowarn (tree type)
1573 if (TREE_CODE (type) == FUNCTION_TYPE
1574 || VOID_TYPE_P (type)
1575 || TREE_CODE (type) == ERROR_MARK)
1576 return size_one_node;
1577 else if (!COMPLETE_TYPE_P (type))
1578 return size_zero_node;
1579 else
1580 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1583 /* Process a sizeof expression where the operand is an expression. */
1585 static tree
1586 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1588 if (e == error_mark_node)
1589 return error_mark_node;
1591 if (processing_template_decl)
1593 e = build_min (SIZEOF_EXPR, size_type_node, e);
1594 TREE_SIDE_EFFECTS (e) = 0;
1595 TREE_READONLY (e) = 1;
1597 return e;
1600 /* To get the size of a static data member declared as an array of
1601 unknown bound, we need to instantiate it. */
1602 if (VAR_P (e)
1603 && VAR_HAD_UNKNOWN_BOUND (e)
1604 && DECL_TEMPLATE_INSTANTIATION (e))
1605 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1607 e = mark_type_use (e);
1609 if (TREE_CODE (e) == COMPONENT_REF
1610 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1611 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1613 if (complain & tf_error)
1614 error ("invalid application of %<sizeof%> to a bit-field");
1615 else
1616 return error_mark_node;
1617 e = char_type_node;
1619 else if (is_overloaded_fn (e))
1621 if (complain & tf_error)
1622 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1623 "function type");
1624 else
1625 return error_mark_node;
1626 e = char_type_node;
1628 else if (type_unknown_p (e))
1630 if (complain & tf_error)
1631 cxx_incomplete_type_error (e, TREE_TYPE (e));
1632 else
1633 return error_mark_node;
1634 e = char_type_node;
1636 else
1637 e = TREE_TYPE (e);
1639 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1642 /* Implement the __alignof keyword: Return the minimum required
1643 alignment of E, measured in bytes. For VAR_DECL's and
1644 FIELD_DECL's return DECL_ALIGN (which can be set from an
1645 "aligned" __attribute__ specification). */
1647 static tree
1648 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1650 tree t;
1652 if (e == error_mark_node)
1653 return error_mark_node;
1655 if (processing_template_decl)
1657 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1658 TREE_SIDE_EFFECTS (e) = 0;
1659 TREE_READONLY (e) = 1;
1661 return e;
1664 e = mark_type_use (e);
1666 if (VAR_P (e))
1667 t = size_int (DECL_ALIGN_UNIT (e));
1668 else if (TREE_CODE (e) == COMPONENT_REF
1669 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1670 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1672 if (complain & tf_error)
1673 error ("invalid application of %<__alignof%> to a bit-field");
1674 else
1675 return error_mark_node;
1676 t = size_one_node;
1678 else if (TREE_CODE (e) == COMPONENT_REF
1679 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1680 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1681 else if (is_overloaded_fn (e))
1683 if (complain & tf_error)
1684 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1685 "function type");
1686 else
1687 return error_mark_node;
1688 if (TREE_CODE (e) == FUNCTION_DECL)
1689 t = size_int (DECL_ALIGN_UNIT (e));
1690 else
1691 t = size_one_node;
1693 else if (type_unknown_p (e))
1695 if (complain & tf_error)
1696 cxx_incomplete_type_error (e, TREE_TYPE (e));
1697 else
1698 return error_mark_node;
1699 t = size_one_node;
1701 else
1702 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1703 complain & tf_error);
1705 return fold_convert (size_type_node, t);
1708 /* Process a sizeof or alignof expression E with code OP where the operand
1709 is an expression. */
1711 tree
1712 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1714 if (op == SIZEOF_EXPR)
1715 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1716 else
1717 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1720 /* Build a representation of an expression 'alignas(E).' Return the
1721 folded integer value of E if it is an integral constant expression
1722 that resolves to a valid alignment. If E depends on a template
1723 parameter, return a syntactic representation tree of kind
1724 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1725 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1727 tree
1728 cxx_alignas_expr (tree e)
1730 if (e == NULL_TREE || e == error_mark_node
1731 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1732 return e;
1734 if (TYPE_P (e))
1735 /* [dcl.align]/3:
1737 When the alignment-specifier is of the form
1738 alignas(type-id ), it shall have the same effect as
1739 alignas(alignof(type-id )). */
1741 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1743 /* If we reach this point, it means the alignas expression if of
1744 the form "alignas(assignment-expression)", so we should follow
1745 what is stated by [dcl.align]/2. */
1747 if (value_dependent_expression_p (e))
1748 /* Leave value-dependent expression alone for now. */
1749 return e;
1751 e = fold_non_dependent_expr (e);
1752 e = mark_rvalue_use (e);
1754 /* [dcl.align]/2 says:
1756 the assignment-expression shall be an integral constant
1757 expression. */
1759 return cxx_constant_value (e);
1763 /* EXPR is being used in a context that is not a function call.
1764 Enforce:
1766 [expr.ref]
1768 The expression can be used only as the left-hand operand of a
1769 member function call.
1771 [expr.mptr.operator]
1773 If the result of .* or ->* is a function, then that result can be
1774 used only as the operand for the function call operator ().
1776 by issuing an error message if appropriate. Returns true iff EXPR
1777 violates these rules. */
1779 bool
1780 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1782 if (expr == NULL_TREE)
1783 return false;
1784 /* Don't enforce this in MS mode. */
1785 if (flag_ms_extensions)
1786 return false;
1787 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1788 expr = get_first_fn (expr);
1789 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1791 if (complain & tf_error)
1792 error ("invalid use of non-static member function");
1793 return true;
1795 return false;
1798 /* If EXP is a reference to a bitfield, and the type of EXP does not
1799 match the declared type of the bitfield, return the declared type
1800 of the bitfield. Otherwise, return NULL_TREE. */
1802 tree
1803 is_bitfield_expr_with_lowered_type (const_tree exp)
1805 switch (TREE_CODE (exp))
1807 case COND_EXPR:
1808 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1809 ? TREE_OPERAND (exp, 1)
1810 : TREE_OPERAND (exp, 0)))
1811 return NULL_TREE;
1812 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1814 case COMPOUND_EXPR:
1815 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1817 case MODIFY_EXPR:
1818 case SAVE_EXPR:
1819 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1821 case COMPONENT_REF:
1823 tree field;
1825 field = TREE_OPERAND (exp, 1);
1826 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1827 return NULL_TREE;
1828 if (same_type_ignoring_top_level_qualifiers_p
1829 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1830 return NULL_TREE;
1831 return DECL_BIT_FIELD_TYPE (field);
1834 CASE_CONVERT:
1835 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1836 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1837 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1838 /* Fallthrough. */
1840 default:
1841 return NULL_TREE;
1845 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1846 bitfield with a lowered type, the type of EXP is returned, rather
1847 than NULL_TREE. */
1849 tree
1850 unlowered_expr_type (const_tree exp)
1852 tree type;
1853 tree etype = TREE_TYPE (exp);
1855 type = is_bitfield_expr_with_lowered_type (exp);
1856 if (type)
1857 type = cp_build_qualified_type (type, cp_type_quals (etype));
1858 else
1859 type = etype;
1861 return type;
1864 /* Perform the conversions in [expr] that apply when an lvalue appears
1865 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1866 function-to-pointer conversions. In addition, manifest constants
1867 are replaced by their values, and bitfield references are converted
1868 to their declared types. Note that this function does not perform the
1869 lvalue-to-rvalue conversion for class types. If you need that conversion
1870 to for class types, then you probably need to use force_rvalue.
1872 Although the returned value is being used as an rvalue, this
1873 function does not wrap the returned expression in a
1874 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1875 that the return value is no longer an lvalue. */
1877 tree
1878 decay_conversion (tree exp, tsubst_flags_t complain)
1880 tree type;
1881 enum tree_code code;
1882 location_t loc = EXPR_LOC_OR_HERE (exp);
1884 type = TREE_TYPE (exp);
1885 if (type == error_mark_node)
1886 return error_mark_node;
1888 exp = mark_rvalue_use (exp);
1890 exp = resolve_nondeduced_context (exp);
1891 if (type_unknown_p (exp))
1893 if (complain & tf_error)
1894 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1895 return error_mark_node;
1898 code = TREE_CODE (type);
1900 /* For an array decl decay_conversion should not try to return its
1901 initializer. */
1902 if (code != ARRAY_TYPE)
1904 /* FIXME remove? at least need to remember that this isn't really a
1905 constant expression if EXP isn't decl_constant_var_p, like with
1906 C_MAYBE_CONST_EXPR. */
1907 exp = decl_constant_value_safe (exp);
1908 if (error_operand_p (exp))
1909 return error_mark_node;
1912 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1913 return nullptr_node;
1915 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1916 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1917 if (code == VOID_TYPE)
1919 if (complain & tf_error)
1920 error_at (loc, "void value not ignored as it ought to be");
1921 return error_mark_node;
1923 if (invalid_nonstatic_memfn_p (exp, complain))
1924 return error_mark_node;
1925 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1926 return cp_build_addr_expr (exp, complain);
1927 if (code == ARRAY_TYPE)
1929 tree adr;
1930 tree ptrtype;
1932 if (INDIRECT_REF_P (exp))
1933 return build_nop (build_pointer_type (TREE_TYPE (type)),
1934 TREE_OPERAND (exp, 0));
1936 if (TREE_CODE (exp) == COMPOUND_EXPR)
1938 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1939 if (op1 == error_mark_node)
1940 return error_mark_node;
1941 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1942 TREE_OPERAND (exp, 0), op1);
1945 if (!lvalue_p (exp)
1946 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1948 if (complain & tf_error)
1949 error_at (loc, "invalid use of non-lvalue array");
1950 return error_mark_node;
1953 /* Don't let an array compound literal decay to a pointer. It can
1954 still be used to initialize an array or bind to a reference. */
1955 if (TREE_CODE (exp) == TARGET_EXPR)
1957 if (complain & tf_error)
1958 error_at (loc, "taking address of temporary array");
1959 return error_mark_node;
1962 ptrtype = build_pointer_type (TREE_TYPE (type));
1964 if (VAR_P (exp))
1966 if (!cxx_mark_addressable (exp))
1967 return error_mark_node;
1968 adr = build_nop (ptrtype, build_address (exp));
1969 return adr;
1971 /* This way is better for a COMPONENT_REF since it can
1972 simplify the offset for a component. */
1973 adr = cp_build_addr_expr (exp, complain);
1974 return cp_convert (ptrtype, adr, complain);
1977 /* If a bitfield is used in a context where integral promotion
1978 applies, then the caller is expected to have used
1979 default_conversion. That function promotes bitfields correctly
1980 before calling this function. At this point, if we have a
1981 bitfield referenced, we may assume that is not subject to
1982 promotion, and that, therefore, the type of the resulting rvalue
1983 is the declared type of the bitfield. */
1984 exp = convert_bitfield_to_declared_type (exp);
1986 /* We do not call rvalue() here because we do not want to wrap EXP
1987 in a NON_LVALUE_EXPR. */
1989 /* [basic.lval]
1991 Non-class rvalues always have cv-unqualified types. */
1992 type = TREE_TYPE (exp);
1993 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1994 exp = build_nop (cv_unqualified (type), exp);
1996 return exp;
1999 /* Perform preparatory conversions, as part of the "usual arithmetic
2000 conversions". In particular, as per [expr]:
2002 Whenever an lvalue expression appears as an operand of an
2003 operator that expects the rvalue for that operand, the
2004 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2005 standard conversions are applied to convert the expression to an
2006 rvalue.
2008 In addition, we perform integral promotions here, as those are
2009 applied to both operands to a binary operator before determining
2010 what additional conversions should apply. */
2012 static tree
2013 cp_default_conversion (tree exp, tsubst_flags_t complain)
2015 /* Check for target-specific promotions. */
2016 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2017 if (promoted_type)
2018 exp = cp_convert (promoted_type, exp, complain);
2019 /* Perform the integral promotions first so that bitfield
2020 expressions (which may promote to "int", even if the bitfield is
2021 declared "unsigned") are promoted correctly. */
2022 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2023 exp = cp_perform_integral_promotions (exp, complain);
2024 /* Perform the other conversions. */
2025 exp = decay_conversion (exp, complain);
2027 return exp;
2030 /* C version. */
2032 tree
2033 default_conversion (tree exp)
2035 return cp_default_conversion (exp, tf_warning_or_error);
2038 /* EXPR is an expression with an integral or enumeration type.
2039 Perform the integral promotions in [conv.prom], and return the
2040 converted value. */
2042 tree
2043 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2045 tree type;
2046 tree promoted_type;
2048 expr = mark_rvalue_use (expr);
2050 /* [conv.prom]
2052 If the bitfield has an enumerated type, it is treated as any
2053 other value of that type for promotion purposes. */
2054 type = is_bitfield_expr_with_lowered_type (expr);
2055 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2056 type = TREE_TYPE (expr);
2057 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2058 /* Scoped enums don't promote. */
2059 if (SCOPED_ENUM_P (type))
2060 return expr;
2061 promoted_type = type_promotes_to (type);
2062 if (type != promoted_type)
2063 expr = cp_convert (promoted_type, expr, complain);
2064 return expr;
2067 /* C version. */
2069 tree
2070 perform_integral_promotions (tree expr)
2072 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2075 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2076 decay_conversion to one. */
2079 string_conv_p (const_tree totype, const_tree exp, int warn)
2081 tree t;
2083 if (!TYPE_PTR_P (totype))
2084 return 0;
2086 t = TREE_TYPE (totype);
2087 if (!same_type_p (t, char_type_node)
2088 && !same_type_p (t, char16_type_node)
2089 && !same_type_p (t, char32_type_node)
2090 && !same_type_p (t, wchar_type_node))
2091 return 0;
2093 if (TREE_CODE (exp) == STRING_CST)
2095 /* Make sure that we don't try to convert between char and wide chars. */
2096 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2097 return 0;
2099 else
2101 /* Is this a string constant which has decayed to 'const char *'? */
2102 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2103 if (!same_type_p (TREE_TYPE (exp), t))
2104 return 0;
2105 STRIP_NOPS (exp);
2106 if (TREE_CODE (exp) != ADDR_EXPR
2107 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2108 return 0;
2111 /* This warning is not very useful, as it complains about printf. */
2112 if (warn)
2113 warning (OPT_Wwrite_strings,
2114 "deprecated conversion from string constant to %qT",
2115 totype);
2117 return 1;
2120 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2121 can, for example, use as an lvalue. This code used to be in
2122 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2123 expressions, where we're dealing with aggregates. But now it's again only
2124 called from unary_complex_lvalue. The case (in particular) that led to
2125 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2126 get it there. */
2128 static tree
2129 rationalize_conditional_expr (enum tree_code code, tree t,
2130 tsubst_flags_t complain)
2132 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2133 the first operand is always the one to be used if both operands
2134 are equal, so we know what conditional expression this used to be. */
2135 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2137 tree op0 = TREE_OPERAND (t, 0);
2138 tree op1 = TREE_OPERAND (t, 1);
2140 /* The following code is incorrect if either operand side-effects. */
2141 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2142 && !TREE_SIDE_EFFECTS (op1));
2143 return
2144 build_conditional_expr (EXPR_LOC_OR_HERE (t),
2145 build_x_binary_op (EXPR_LOC_OR_HERE (t),
2146 (TREE_CODE (t) == MIN_EXPR
2147 ? LE_EXPR : GE_EXPR),
2148 op0, TREE_CODE (op0),
2149 op1, TREE_CODE (op1),
2150 /*overload=*/NULL,
2151 complain),
2152 cp_build_unary_op (code, op0, 0, complain),
2153 cp_build_unary_op (code, op1, 0, complain),
2154 complain);
2157 return
2158 build_conditional_expr (EXPR_LOC_OR_HERE (t), TREE_OPERAND (t, 0),
2159 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2160 complain),
2161 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2162 complain),
2163 complain);
2166 /* Given the TYPE of an anonymous union field inside T, return the
2167 FIELD_DECL for the field. If not found return NULL_TREE. Because
2168 anonymous unions can nest, we must also search all anonymous unions
2169 that are directly reachable. */
2171 tree
2172 lookup_anon_field (tree t, tree type)
2174 tree field;
2176 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2178 if (TREE_STATIC (field))
2179 continue;
2180 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2181 continue;
2183 /* If we find it directly, return the field. */
2184 if (DECL_NAME (field) == NULL_TREE
2185 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2187 return field;
2190 /* Otherwise, it could be nested, search harder. */
2191 if (DECL_NAME (field) == NULL_TREE
2192 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2194 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2195 if (subfield)
2196 return subfield;
2199 return NULL_TREE;
2202 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2203 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2204 non-NULL, it indicates the path to the base used to name MEMBER.
2205 If PRESERVE_REFERENCE is true, the expression returned will have
2206 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2207 returned will have the type referred to by the reference.
2209 This function does not perform access control; that is either done
2210 earlier by the parser when the name of MEMBER is resolved to MEMBER
2211 itself, or later when overload resolution selects one of the
2212 functions indicated by MEMBER. */
2214 tree
2215 build_class_member_access_expr (tree object, tree member,
2216 tree access_path, bool preserve_reference,
2217 tsubst_flags_t complain)
2219 tree object_type;
2220 tree member_scope;
2221 tree result = NULL_TREE;
2222 tree using_decl = NULL_TREE;
2224 if (error_operand_p (object) || error_operand_p (member))
2225 return error_mark_node;
2227 gcc_assert (DECL_P (member) || BASELINK_P (member));
2229 /* [expr.ref]
2231 The type of the first expression shall be "class object" (of a
2232 complete type). */
2233 object_type = TREE_TYPE (object);
2234 if (!currently_open_class (object_type)
2235 && !complete_type_or_maybe_complain (object_type, object, complain))
2236 return error_mark_node;
2237 if (!CLASS_TYPE_P (object_type))
2239 if (complain & tf_error)
2241 if (POINTER_TYPE_P (object_type)
2242 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2243 error ("request for member %qD in %qE, which is of pointer "
2244 "type %qT (maybe you meant to use %<->%> ?)",
2245 member, object, object_type);
2246 else
2247 error ("request for member %qD in %qE, which is of non-class "
2248 "type %qT", member, object, object_type);
2250 return error_mark_node;
2253 /* The standard does not seem to actually say that MEMBER must be a
2254 member of OBJECT_TYPE. However, that is clearly what is
2255 intended. */
2256 if (DECL_P (member))
2258 member_scope = DECL_CLASS_CONTEXT (member);
2259 mark_used (member);
2260 if (TREE_DEPRECATED (member))
2261 warn_deprecated_use (member, NULL_TREE);
2263 else
2264 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2265 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2266 presently be the anonymous union. Go outwards until we find a
2267 type related to OBJECT_TYPE. */
2268 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2269 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2270 object_type))
2271 member_scope = TYPE_CONTEXT (member_scope);
2272 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2274 if (complain & tf_error)
2276 if (TREE_CODE (member) == FIELD_DECL)
2277 error ("invalid use of nonstatic data member %qE", member);
2278 else
2279 error ("%qD is not a member of %qT", member, object_type);
2281 return error_mark_node;
2284 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2285 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2286 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2288 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2289 if (temp)
2290 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2293 /* In [expr.ref], there is an explicit list of the valid choices for
2294 MEMBER. We check for each of those cases here. */
2295 if (VAR_P (member))
2297 /* A static data member. */
2298 result = member;
2299 mark_exp_read (object);
2300 /* If OBJECT has side-effects, they are supposed to occur. */
2301 if (TREE_SIDE_EFFECTS (object))
2302 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2304 else if (TREE_CODE (member) == FIELD_DECL)
2306 /* A non-static data member. */
2307 bool null_object_p;
2308 int type_quals;
2309 tree member_type;
2311 null_object_p = (INDIRECT_REF_P (object)
2312 && integer_zerop (TREE_OPERAND (object, 0)));
2314 /* Convert OBJECT to the type of MEMBER. */
2315 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2316 TYPE_MAIN_VARIANT (member_scope)))
2318 tree binfo;
2319 base_kind kind;
2321 binfo = lookup_base (access_path ? access_path : object_type,
2322 member_scope, ba_unique, &kind, complain);
2323 if (binfo == error_mark_node)
2324 return error_mark_node;
2326 /* It is invalid to try to get to a virtual base of a
2327 NULL object. The most common cause is invalid use of
2328 offsetof macro. */
2329 if (null_object_p && kind == bk_via_virtual)
2331 if (complain & tf_error)
2333 error ("invalid access to non-static data member %qD of "
2334 "NULL object",
2335 member);
2336 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2338 return error_mark_node;
2341 /* Convert to the base. */
2342 object = build_base_path (PLUS_EXPR, object, binfo,
2343 /*nonnull=*/1, complain);
2344 /* If we found the base successfully then we should be able
2345 to convert to it successfully. */
2346 gcc_assert (object != error_mark_node);
2349 /* Complain about other invalid uses of offsetof, even though they will
2350 give the right answer. Note that we complain whether or not they
2351 actually used the offsetof macro, since there's no way to know at this
2352 point. So we just give a warning, instead of a pedwarn. */
2353 /* Do not produce this warning for base class field references, because
2354 we know for a fact that didn't come from offsetof. This does occur
2355 in various testsuite cases where a null object is passed where a
2356 vtable access is required. */
2357 if (null_object_p && warn_invalid_offsetof
2358 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2359 && !DECL_FIELD_IS_BASE (member)
2360 && cp_unevaluated_operand == 0
2361 && (complain & tf_warning))
2363 warning (OPT_Winvalid_offsetof,
2364 "invalid access to non-static data member %qD "
2365 " of NULL object", member);
2366 warning (OPT_Winvalid_offsetof,
2367 "(perhaps the %<offsetof%> macro was used incorrectly)");
2370 /* If MEMBER is from an anonymous aggregate, we have converted
2371 OBJECT so that it refers to the class containing the
2372 anonymous union. Generate a reference to the anonymous union
2373 itself, and recur to find MEMBER. */
2374 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2375 /* When this code is called from build_field_call, the
2376 object already has the type of the anonymous union.
2377 That is because the COMPONENT_REF was already
2378 constructed, and was then disassembled before calling
2379 build_field_call. After the function-call code is
2380 cleaned up, this waste can be eliminated. */
2381 && (!same_type_ignoring_top_level_qualifiers_p
2382 (TREE_TYPE (object), DECL_CONTEXT (member))))
2384 tree anonymous_union;
2386 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2387 DECL_CONTEXT (member));
2388 object = build_class_member_access_expr (object,
2389 anonymous_union,
2390 /*access_path=*/NULL_TREE,
2391 preserve_reference,
2392 complain);
2395 /* Compute the type of the field, as described in [expr.ref]. */
2396 type_quals = TYPE_UNQUALIFIED;
2397 member_type = TREE_TYPE (member);
2398 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2400 type_quals = (cp_type_quals (member_type)
2401 | cp_type_quals (object_type));
2403 /* A field is const (volatile) if the enclosing object, or the
2404 field itself, is const (volatile). But, a mutable field is
2405 not const, even within a const object. */
2406 if (DECL_MUTABLE_P (member))
2407 type_quals &= ~TYPE_QUAL_CONST;
2408 member_type = cp_build_qualified_type (member_type, type_quals);
2411 result = build3 (COMPONENT_REF, member_type, object, member,
2412 NULL_TREE);
2413 result = fold_if_not_in_template (result);
2415 /* Mark the expression const or volatile, as appropriate. Even
2416 though we've dealt with the type above, we still have to mark the
2417 expression itself. */
2418 if (type_quals & TYPE_QUAL_CONST)
2419 TREE_READONLY (result) = 1;
2420 if (type_quals & TYPE_QUAL_VOLATILE)
2421 TREE_THIS_VOLATILE (result) = 1;
2423 else if (BASELINK_P (member))
2425 /* The member is a (possibly overloaded) member function. */
2426 tree functions;
2427 tree type;
2429 /* If the MEMBER is exactly one static member function, then we
2430 know the type of the expression. Otherwise, we must wait
2431 until overload resolution has been performed. */
2432 functions = BASELINK_FUNCTIONS (member);
2433 if (TREE_CODE (functions) == FUNCTION_DECL
2434 && DECL_STATIC_FUNCTION_P (functions))
2435 type = TREE_TYPE (functions);
2436 else
2437 type = unknown_type_node;
2438 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2439 base. That will happen when the function is called. */
2440 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2442 else if (TREE_CODE (member) == CONST_DECL)
2444 /* The member is an enumerator. */
2445 result = member;
2446 /* If OBJECT has side-effects, they are supposed to occur. */
2447 if (TREE_SIDE_EFFECTS (object))
2448 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2449 object, result);
2451 else if ((using_decl = strip_using_decl (member)) != member)
2452 result = build_class_member_access_expr (object,
2453 using_decl,
2454 access_path, preserve_reference,
2455 complain);
2456 else
2458 if (complain & tf_error)
2459 error ("invalid use of %qD", member);
2460 return error_mark_node;
2463 if (!preserve_reference)
2464 /* [expr.ref]
2466 If E2 is declared to have type "reference to T", then ... the
2467 type of E1.E2 is T. */
2468 result = convert_from_reference (result);
2470 return result;
2473 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2474 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2476 static tree
2477 lookup_destructor (tree object, tree scope, tree dtor_name,
2478 tsubst_flags_t complain)
2480 tree object_type = TREE_TYPE (object);
2481 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2482 tree expr;
2484 if (scope && !check_dtor_name (scope, dtor_type))
2486 if (complain & tf_error)
2487 error ("qualified type %qT does not match destructor name ~%qT",
2488 scope, dtor_type);
2489 return error_mark_node;
2491 if (is_auto (dtor_type))
2492 dtor_type = object_type;
2493 else if (identifier_p (dtor_type))
2495 /* In a template, names we can't find a match for are still accepted
2496 destructor names, and we check them here. */
2497 if (check_dtor_name (object_type, dtor_type))
2498 dtor_type = object_type;
2499 else
2501 if (complain & tf_error)
2502 error ("object type %qT does not match destructor name ~%qT",
2503 object_type, dtor_type);
2504 return error_mark_node;
2508 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2510 if (complain & tf_error)
2511 error ("the type being destroyed is %qT, but the destructor "
2512 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2513 return error_mark_node;
2515 expr = lookup_member (dtor_type, complete_dtor_identifier,
2516 /*protect=*/1, /*want_type=*/false,
2517 tf_warning_or_error);
2518 expr = (adjust_result_of_qualified_name_lookup
2519 (expr, dtor_type, object_type));
2520 if (scope == NULL_TREE)
2521 /* We need to call adjust_result_of_qualified_name_lookup in case the
2522 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2523 that we still get virtual function binding. */
2524 BASELINK_QUALIFIED_P (expr) = false;
2525 return expr;
2528 /* An expression of the form "A::template B" has been resolved to
2529 DECL. Issue a diagnostic if B is not a template or template
2530 specialization. */
2532 void
2533 check_template_keyword (tree decl)
2535 /* The standard says:
2537 [temp.names]
2539 If a name prefixed by the keyword template is not a member
2540 template, the program is ill-formed.
2542 DR 228 removed the restriction that the template be a member
2543 template.
2545 DR 96, if accepted would add the further restriction that explicit
2546 template arguments must be provided if the template keyword is
2547 used, but, as of 2005-10-16, that DR is still in "drafting". If
2548 this DR is accepted, then the semantic checks here can be
2549 simplified, as the entity named must in fact be a template
2550 specialization, rather than, as at present, a set of overloaded
2551 functions containing at least one template function. */
2552 if (TREE_CODE (decl) != TEMPLATE_DECL
2553 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2555 if (!is_overloaded_fn (decl))
2556 permerror (input_location, "%qD is not a template", decl);
2557 else
2559 tree fns;
2560 fns = decl;
2561 if (BASELINK_P (fns))
2562 fns = BASELINK_FUNCTIONS (fns);
2563 while (fns)
2565 tree fn = OVL_CURRENT (fns);
2566 if (TREE_CODE (fn) == TEMPLATE_DECL
2567 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2568 break;
2569 if (TREE_CODE (fn) == FUNCTION_DECL
2570 && DECL_USE_TEMPLATE (fn)
2571 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2572 break;
2573 fns = OVL_NEXT (fns);
2575 if (!fns)
2576 permerror (input_location, "%qD is not a template", decl);
2581 /* This function is called by the parser to process a class member
2582 access expression of the form OBJECT.NAME. NAME is a node used by
2583 the parser to represent a name; it is not yet a DECL. It may,
2584 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2585 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2586 there is no reason to do the lookup twice, so the parser keeps the
2587 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2588 be a template via the use of the "A::template B" syntax. */
2590 tree
2591 finish_class_member_access_expr (tree object, tree name, bool template_p,
2592 tsubst_flags_t complain)
2594 tree expr;
2595 tree object_type;
2596 tree member;
2597 tree access_path = NULL_TREE;
2598 tree orig_object = object;
2599 tree orig_name = name;
2601 if (object == error_mark_node || name == error_mark_node)
2602 return error_mark_node;
2604 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2605 if (!objc_is_public (object, name))
2606 return error_mark_node;
2608 object_type = TREE_TYPE (object);
2610 if (processing_template_decl)
2612 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2613 dependent_type_p (object_type)
2614 /* If NAME is just an IDENTIFIER_NODE, then the expression
2615 is dependent. */
2616 || identifier_p (object)
2617 /* If NAME is "f<args>", where either 'f' or 'args' is
2618 dependent, then the expression is dependent. */
2619 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2620 && dependent_template_id_p (TREE_OPERAND (name, 0),
2621 TREE_OPERAND (name, 1)))
2622 /* If NAME is "T::X" where "T" is dependent, then the
2623 expression is dependent. */
2624 || (TREE_CODE (name) == SCOPE_REF
2625 && TYPE_P (TREE_OPERAND (name, 0))
2626 && dependent_type_p (TREE_OPERAND (name, 0))))
2627 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2628 object, name, NULL_TREE);
2629 object = build_non_dependent_expr (object);
2631 else if (c_dialect_objc ()
2632 && identifier_p (name)
2633 && (expr = objc_maybe_build_component_ref (object, name)))
2634 return expr;
2636 /* [expr.ref]
2638 The type of the first expression shall be "class object" (of a
2639 complete type). */
2640 if (!currently_open_class (object_type)
2641 && !complete_type_or_maybe_complain (object_type, object, complain))
2642 return error_mark_node;
2643 if (!CLASS_TYPE_P (object_type))
2645 if (complain & tf_error)
2647 if (POINTER_TYPE_P (object_type)
2648 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2649 error ("request for member %qD in %qE, which is of pointer "
2650 "type %qT (maybe you meant to use %<->%> ?)",
2651 name, object, object_type);
2652 else
2653 error ("request for member %qD in %qE, which is of non-class "
2654 "type %qT", name, object, object_type);
2656 return error_mark_node;
2659 if (BASELINK_P (name))
2660 /* A member function that has already been looked up. */
2661 member = name;
2662 else
2664 bool is_template_id = false;
2665 tree template_args = NULL_TREE;
2666 tree scope;
2668 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2670 is_template_id = true;
2671 template_args = TREE_OPERAND (name, 1);
2672 name = TREE_OPERAND (name, 0);
2674 if (TREE_CODE (name) == OVERLOAD)
2675 name = DECL_NAME (get_first_fn (name));
2676 else if (DECL_P (name))
2677 name = DECL_NAME (name);
2680 if (TREE_CODE (name) == SCOPE_REF)
2682 /* A qualified name. The qualifying class or namespace `S'
2683 has already been looked up; it is either a TYPE or a
2684 NAMESPACE_DECL. */
2685 scope = TREE_OPERAND (name, 0);
2686 name = TREE_OPERAND (name, 1);
2688 /* If SCOPE is a namespace, then the qualified name does not
2689 name a member of OBJECT_TYPE. */
2690 if (TREE_CODE (scope) == NAMESPACE_DECL)
2692 if (complain & tf_error)
2693 error ("%<%D::%D%> is not a member of %qT",
2694 scope, name, object_type);
2695 return error_mark_node;
2698 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2700 /* Looking up a member enumerator (c++/56793). */
2701 if (!TYPE_CLASS_SCOPE_P (scope)
2702 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2704 if (complain & tf_error)
2705 error ("%<%D::%D%> is not a member of %qT",
2706 scope, name, object_type);
2707 return error_mark_node;
2709 tree val = lookup_enumerator (scope, name);
2710 if (TREE_SIDE_EFFECTS (object))
2711 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2712 return val;
2715 gcc_assert (CLASS_TYPE_P (scope));
2716 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2718 if (constructor_name_p (name, scope))
2720 if (complain & tf_error)
2721 error ("cannot call constructor %<%T::%D%> directly",
2722 scope, name);
2723 return error_mark_node;
2726 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2727 access_path = lookup_base (object_type, scope, ba_check,
2728 NULL, complain);
2729 if (access_path == error_mark_node)
2730 return error_mark_node;
2731 if (!access_path)
2733 if (complain & tf_error)
2734 error ("%qT is not a base of %qT", scope, object_type);
2735 return error_mark_node;
2738 else
2740 scope = NULL_TREE;
2741 access_path = object_type;
2744 if (TREE_CODE (name) == BIT_NOT_EXPR)
2745 member = lookup_destructor (object, scope, name, complain);
2746 else
2748 /* Look up the member. */
2749 member = lookup_member (access_path, name, /*protect=*/1,
2750 /*want_type=*/false, complain);
2751 if (member == NULL_TREE)
2753 if (complain & tf_error)
2754 error ("%qD has no member named %qE",
2755 TREE_CODE (access_path) == TREE_BINFO
2756 ? TREE_TYPE (access_path) : object_type, name);
2757 return error_mark_node;
2759 if (member == error_mark_node)
2760 return error_mark_node;
2763 if (is_template_id)
2765 tree templ = member;
2767 if (BASELINK_P (templ))
2768 templ = lookup_template_function (templ, template_args);
2769 else
2771 if (complain & tf_error)
2772 error ("%qD is not a member template function", name);
2773 return error_mark_node;
2778 if (TREE_DEPRECATED (member))
2779 warn_deprecated_use (member, NULL_TREE);
2781 if (template_p)
2782 check_template_keyword (member);
2784 expr = build_class_member_access_expr (object, member, access_path,
2785 /*preserve_reference=*/false,
2786 complain);
2787 if (processing_template_decl && expr != error_mark_node)
2789 if (BASELINK_P (member))
2791 if (TREE_CODE (orig_name) == SCOPE_REF)
2792 BASELINK_QUALIFIED_P (member) = 1;
2793 orig_name = member;
2795 return build_min_non_dep (COMPONENT_REF, expr,
2796 orig_object, orig_name,
2797 NULL_TREE);
2800 return expr;
2803 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2804 type. */
2806 tree
2807 build_simple_component_ref (tree object, tree member)
2809 tree type = cp_build_qualified_type (TREE_TYPE (member),
2810 cp_type_quals (TREE_TYPE (object)));
2811 return fold_build3_loc (input_location,
2812 COMPONENT_REF, type,
2813 object, member, NULL_TREE);
2816 /* Return an expression for the MEMBER_NAME field in the internal
2817 representation of PTRMEM, a pointer-to-member function. (Each
2818 pointer-to-member function type gets its own RECORD_TYPE so it is
2819 more convenient to access the fields by name than by FIELD_DECL.)
2820 This routine converts the NAME to a FIELD_DECL and then creates the
2821 node for the complete expression. */
2823 tree
2824 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2826 tree ptrmem_type;
2827 tree member;
2829 /* This code is a stripped down version of
2830 build_class_member_access_expr. It does not work to use that
2831 routine directly because it expects the object to be of class
2832 type. */
2833 ptrmem_type = TREE_TYPE (ptrmem);
2834 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2835 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2836 /*want_type=*/false, tf_warning_or_error);
2837 return build_simple_component_ref (ptrmem, member);
2840 /* Given an expression PTR for a pointer, return an expression
2841 for the value pointed to.
2842 ERRORSTRING is the name of the operator to appear in error messages.
2844 This function may need to overload OPERATOR_FNNAME.
2845 Must also handle REFERENCE_TYPEs for C++. */
2847 tree
2848 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2849 tsubst_flags_t complain)
2851 tree orig_expr = expr;
2852 tree rval;
2854 if (processing_template_decl)
2856 /* Retain the type if we know the operand is a pointer. */
2857 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2858 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2859 if (type_dependent_expression_p (expr))
2860 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2861 expr = build_non_dependent_expr (expr);
2864 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2865 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2866 if (!rval)
2867 rval = cp_build_indirect_ref (expr, errorstring, complain);
2869 if (processing_template_decl && rval != error_mark_node)
2870 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2871 else
2872 return rval;
2875 /* Helper function called from c-common. */
2876 tree
2877 build_indirect_ref (location_t /*loc*/,
2878 tree ptr, ref_operator errorstring)
2880 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2883 tree
2884 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2885 tsubst_flags_t complain)
2887 tree pointer, type;
2889 if (ptr == current_class_ptr
2890 || (TREE_CODE (ptr) == NOP_EXPR
2891 && TREE_OPERAND (ptr, 0) == current_class_ptr
2892 && (same_type_ignoring_top_level_qualifiers_p
2893 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2894 return current_class_ref;
2896 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2897 ? ptr : decay_conversion (ptr, complain));
2898 if (pointer == error_mark_node)
2899 return error_mark_node;
2901 type = TREE_TYPE (pointer);
2903 if (POINTER_TYPE_P (type))
2905 /* [expr.unary.op]
2907 If the type of the expression is "pointer to T," the type
2908 of the result is "T." */
2909 tree t = TREE_TYPE (type);
2911 if (CONVERT_EXPR_P (ptr)
2912 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2914 /* If a warning is issued, mark it to avoid duplicates from
2915 the backend. This only needs to be done at
2916 warn_strict_aliasing > 2. */
2917 if (warn_strict_aliasing > 2)
2918 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2919 type, TREE_OPERAND (ptr, 0)))
2920 TREE_NO_WARNING (ptr) = 1;
2923 if (VOID_TYPE_P (t))
2925 /* A pointer to incomplete type (other than cv void) can be
2926 dereferenced [expr.unary.op]/1 */
2927 if (complain & tf_error)
2928 error ("%qT is not a pointer-to-object type", type);
2929 return error_mark_node;
2931 else if (TREE_CODE (pointer) == ADDR_EXPR
2932 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2933 /* The POINTER was something like `&x'. We simplify `*&x' to
2934 `x'. */
2935 return TREE_OPERAND (pointer, 0);
2936 else
2938 tree ref = build1 (INDIRECT_REF, t, pointer);
2940 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2941 so that we get the proper error message if the result is used
2942 to assign to. Also, &* is supposed to be a no-op. */
2943 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2944 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2945 TREE_SIDE_EFFECTS (ref)
2946 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2947 return ref;
2950 else if (!(complain & tf_error))
2951 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2953 /* `pointer' won't be an error_mark_node if we were given a
2954 pointer to member, so it's cool to check for this here. */
2955 else if (TYPE_PTRMEM_P (type))
2956 switch (errorstring)
2958 case RO_ARRAY_INDEXING:
2959 error ("invalid use of array indexing on pointer to member");
2960 break;
2961 case RO_UNARY_STAR:
2962 error ("invalid use of unary %<*%> on pointer to member");
2963 break;
2964 case RO_IMPLICIT_CONVERSION:
2965 error ("invalid use of implicit conversion on pointer to member");
2966 break;
2967 case RO_ARROW_STAR:
2968 error ("left hand operand of %<->*%> must be a pointer to class, "
2969 "but is a pointer to member of type %qT", type);
2970 break;
2971 default:
2972 gcc_unreachable ();
2974 else if (pointer != error_mark_node)
2975 invalid_indirection_error (input_location, type, errorstring);
2977 return error_mark_node;
2980 /* This handles expressions of the form "a[i]", which denotes
2981 an array reference.
2983 This is logically equivalent in C to *(a+i), but we may do it differently.
2984 If A is a variable or a member, we generate a primitive ARRAY_REF.
2985 This avoids forcing the array out of registers, and can work on
2986 arrays that are not lvalues (for example, members of structures returned
2987 by functions).
2989 If INDEX is of some user-defined type, it must be converted to
2990 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2991 will inherit the type of the array, which will be some pointer type.
2993 LOC is the location to use in building the array reference. */
2995 tree
2996 cp_build_array_ref (location_t loc, tree array, tree idx,
2997 tsubst_flags_t complain)
2999 tree ret;
3001 if (idx == 0)
3003 if (complain & tf_error)
3004 error_at (loc, "subscript missing in array reference");
3005 return error_mark_node;
3008 if (TREE_TYPE (array) == error_mark_node
3009 || TREE_TYPE (idx) == error_mark_node)
3010 return error_mark_node;
3012 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3013 inside it. */
3014 switch (TREE_CODE (array))
3016 case COMPOUND_EXPR:
3018 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3019 complain);
3020 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3021 TREE_OPERAND (array, 0), value);
3022 SET_EXPR_LOCATION (ret, loc);
3023 return ret;
3026 case COND_EXPR:
3027 ret = build_conditional_expr
3028 (loc, TREE_OPERAND (array, 0),
3029 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3030 complain),
3031 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3032 complain),
3033 complain);
3034 protected_set_expr_location (ret, loc);
3035 return ret;
3037 default:
3038 break;
3041 convert_vector_to_pointer_for_subscript (loc, &array, idx);
3043 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3045 tree rval, type;
3047 warn_array_subscript_with_type_char (idx);
3049 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3051 if (complain & tf_error)
3052 error_at (loc, "array subscript is not an integer");
3053 return error_mark_node;
3056 /* Apply integral promotions *after* noticing character types.
3057 (It is unclear why we do these promotions -- the standard
3058 does not say that we should. In fact, the natural thing would
3059 seem to be to convert IDX to ptrdiff_t; we're performing
3060 pointer arithmetic.) */
3061 idx = cp_perform_integral_promotions (idx, complain);
3063 /* An array that is indexed by a non-constant
3064 cannot be stored in a register; we must be able to do
3065 address arithmetic on its address.
3066 Likewise an array of elements of variable size. */
3067 if (TREE_CODE (idx) != INTEGER_CST
3068 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3069 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3070 != INTEGER_CST)))
3072 if (!cxx_mark_addressable (array))
3073 return error_mark_node;
3076 /* An array that is indexed by a constant value which is not within
3077 the array bounds cannot be stored in a register either; because we
3078 would get a crash in store_bit_field/extract_bit_field when trying
3079 to access a non-existent part of the register. */
3080 if (TREE_CODE (idx) == INTEGER_CST
3081 && TYPE_DOMAIN (TREE_TYPE (array))
3082 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3084 if (!cxx_mark_addressable (array))
3085 return error_mark_node;
3088 if (!lvalue_p (array) && (complain & tf_error))
3089 pedwarn (loc, OPT_Wpedantic,
3090 "ISO C++ forbids subscripting non-lvalue array");
3092 /* Note in C++ it is valid to subscript a `register' array, since
3093 it is valid to take the address of something with that
3094 storage specification. */
3095 if (extra_warnings)
3097 tree foo = array;
3098 while (TREE_CODE (foo) == COMPONENT_REF)
3099 foo = TREE_OPERAND (foo, 0);
3100 if (VAR_P (foo) && DECL_REGISTER (foo)
3101 && (complain & tf_warning))
3102 warning_at (loc, OPT_Wextra,
3103 "subscripting array declared %<register%>");
3106 type = TREE_TYPE (TREE_TYPE (array));
3107 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3108 /* Array ref is const/volatile if the array elements are
3109 or if the array is.. */
3110 TREE_READONLY (rval)
3111 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3112 TREE_SIDE_EFFECTS (rval)
3113 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3114 TREE_THIS_VOLATILE (rval)
3115 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3116 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3117 complain);
3118 protected_set_expr_location (ret, loc);
3119 return ret;
3123 tree ar = cp_default_conversion (array, complain);
3124 tree ind = cp_default_conversion (idx, complain);
3126 /* Put the integer in IND to simplify error checking. */
3127 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3129 tree temp = ar;
3130 ar = ind;
3131 ind = temp;
3134 if (ar == error_mark_node || ind == error_mark_node)
3135 return error_mark_node;
3137 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3139 if (complain & tf_error)
3140 error_at (loc, "subscripted value is neither array nor pointer");
3141 return error_mark_node;
3143 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3145 if (complain & tf_error)
3146 error_at (loc, "array subscript is not an integer");
3147 return error_mark_node;
3150 warn_array_subscript_with_type_char (idx);
3152 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3153 PLUS_EXPR, ar, ind,
3154 complain),
3155 RO_ARRAY_INDEXING,
3156 complain);
3157 protected_set_expr_location (ret, loc);
3158 return ret;
3162 /* Entry point for Obj-C++. */
3164 tree
3165 build_array_ref (location_t loc, tree array, tree idx)
3167 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3170 /* Resolve a pointer to member function. INSTANCE is the object
3171 instance to use, if the member points to a virtual member.
3173 This used to avoid checking for virtual functions if basetype
3174 has no virtual functions, according to an earlier ANSI draft.
3175 With the final ISO C++ rules, such an optimization is
3176 incorrect: A pointer to a derived member can be static_cast
3177 to pointer-to-base-member, as long as the dynamic object
3178 later has the right member. So now we only do this optimization
3179 when we know the dynamic type of the object. */
3181 tree
3182 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3183 tsubst_flags_t complain)
3185 if (TREE_CODE (function) == OFFSET_REF)
3186 function = TREE_OPERAND (function, 1);
3188 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3190 tree idx, delta, e1, e2, e3, vtbl;
3191 bool nonvirtual;
3192 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3193 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3195 tree instance_ptr = *instance_ptrptr;
3196 tree instance_save_expr = 0;
3197 if (instance_ptr == error_mark_node)
3199 if (TREE_CODE (function) == PTRMEM_CST)
3201 /* Extracting the function address from a pmf is only
3202 allowed with -Wno-pmf-conversions. It only works for
3203 pmf constants. */
3204 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3205 e1 = convert (fntype, e1);
3206 return e1;
3208 else
3210 if (complain & tf_error)
3211 error ("object missing in use of %qE", function);
3212 return error_mark_node;
3216 /* True if we know that the dynamic type of the object doesn't have
3217 virtual functions, so we can assume the PFN field is a pointer. */
3218 nonvirtual = (COMPLETE_TYPE_P (basetype)
3219 && !TYPE_POLYMORPHIC_P (basetype)
3220 && resolves_to_fixed_type_p (instance_ptr, 0));
3222 if (TREE_SIDE_EFFECTS (instance_ptr))
3223 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3225 if (TREE_SIDE_EFFECTS (function))
3226 function = save_expr (function);
3228 /* Start by extracting all the information from the PMF itself. */
3229 e3 = pfn_from_ptrmemfunc (function);
3230 delta = delta_from_ptrmemfunc (function);
3231 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3232 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3234 case ptrmemfunc_vbit_in_pfn:
3235 e1 = cp_build_binary_op (input_location,
3236 BIT_AND_EXPR, idx, integer_one_node,
3237 complain);
3238 idx = cp_build_binary_op (input_location,
3239 MINUS_EXPR, idx, integer_one_node,
3240 complain);
3241 if (idx == error_mark_node)
3242 return error_mark_node;
3243 break;
3245 case ptrmemfunc_vbit_in_delta:
3246 e1 = cp_build_binary_op (input_location,
3247 BIT_AND_EXPR, delta, integer_one_node,
3248 complain);
3249 delta = cp_build_binary_op (input_location,
3250 RSHIFT_EXPR, delta, integer_one_node,
3251 complain);
3252 if (delta == error_mark_node)
3253 return error_mark_node;
3254 break;
3256 default:
3257 gcc_unreachable ();
3260 if (e1 == error_mark_node)
3261 return error_mark_node;
3263 /* Convert down to the right base before using the instance. A
3264 special case is that in a pointer to member of class C, C may
3265 be incomplete. In that case, the function will of course be
3266 a member of C, and no conversion is required. In fact,
3267 lookup_base will fail in that case, because incomplete
3268 classes do not have BINFOs. */
3269 if (!same_type_ignoring_top_level_qualifiers_p
3270 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3272 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3273 basetype, ba_check, NULL, complain);
3274 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3275 1, complain);
3276 if (instance_ptr == error_mark_node)
3277 return error_mark_node;
3279 /* ...and then the delta in the PMF. */
3280 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3282 /* Hand back the adjusted 'this' argument to our caller. */
3283 *instance_ptrptr = instance_ptr;
3285 if (nonvirtual)
3286 /* Now just return the pointer. */
3287 return e3;
3289 /* Next extract the vtable pointer from the object. */
3290 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3291 instance_ptr);
3292 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3293 if (vtbl == error_mark_node)
3294 return error_mark_node;
3296 /* Finally, extract the function pointer from the vtable. */
3297 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3298 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3299 if (e2 == error_mark_node)
3300 return error_mark_node;
3301 TREE_CONSTANT (e2) = 1;
3303 /* When using function descriptors, the address of the
3304 vtable entry is treated as a function pointer. */
3305 if (TARGET_VTABLE_USES_DESCRIPTORS)
3306 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3307 cp_build_addr_expr (e2, complain));
3309 e2 = fold_convert (TREE_TYPE (e3), e2);
3310 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3311 if (e1 == error_mark_node)
3312 return error_mark_node;
3314 /* Make sure this doesn't get evaluated first inside one of the
3315 branches of the COND_EXPR. */
3316 if (instance_save_expr)
3317 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3318 instance_save_expr, e1);
3320 function = e1;
3322 return function;
3325 /* Used by the C-common bits. */
3326 tree
3327 build_function_call (location_t /*loc*/,
3328 tree function, tree params)
3330 return cp_build_function_call (function, params, tf_warning_or_error);
3333 /* Used by the C-common bits. */
3334 tree
3335 build_function_call_vec (location_t /*loc*/,
3336 tree function, vec<tree, va_gc> *params,
3337 vec<tree, va_gc> * /*origtypes*/)
3339 vec<tree, va_gc> *orig_params = params;
3340 tree ret = cp_build_function_call_vec (function, &params,
3341 tf_warning_or_error);
3343 /* cp_build_function_call_vec can reallocate PARAMS by adding
3344 default arguments. That should never happen here. Verify
3345 that. */
3346 gcc_assert (params == orig_params);
3348 return ret;
3351 /* Build a function call using a tree list of arguments. */
3353 tree
3354 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3356 vec<tree, va_gc> *vec;
3357 tree ret;
3359 vec = make_tree_vector ();
3360 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3361 vec_safe_push (vec, TREE_VALUE (params));
3362 ret = cp_build_function_call_vec (function, &vec, complain);
3363 release_tree_vector (vec);
3364 return ret;
3367 /* Build a function call using varargs. */
3369 tree
3370 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3372 vec<tree, va_gc> *vec;
3373 va_list args;
3374 tree ret, t;
3376 vec = make_tree_vector ();
3377 va_start (args, complain);
3378 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3379 vec_safe_push (vec, t);
3380 va_end (args);
3381 ret = cp_build_function_call_vec (function, &vec, complain);
3382 release_tree_vector (vec);
3383 return ret;
3386 /* Build a function call using a vector of arguments. PARAMS may be
3387 NULL if there are no parameters. This changes the contents of
3388 PARAMS. */
3390 tree
3391 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3392 tsubst_flags_t complain)
3394 tree fntype, fndecl;
3395 int is_method;
3396 tree original = function;
3397 int nargs;
3398 tree *argarray;
3399 tree parm_types;
3400 vec<tree, va_gc> *allocated = NULL;
3401 tree ret;
3403 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3404 expressions, like those used for ObjC messenger dispatches. */
3405 if (params != NULL && !vec_safe_is_empty (*params))
3406 function = objc_rewrite_function_call (function, (**params)[0]);
3408 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3409 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3410 if (TREE_CODE (function) == NOP_EXPR
3411 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3412 function = TREE_OPERAND (function, 0);
3414 if (TREE_CODE (function) == FUNCTION_DECL)
3416 mark_used (function);
3417 fndecl = function;
3419 /* Convert anything with function type to a pointer-to-function. */
3420 if (DECL_MAIN_P (function) && (complain & tf_error))
3421 pedwarn (input_location, OPT_Wpedantic,
3422 "ISO C++ forbids calling %<::main%> from within program");
3424 function = build_addr_func (function, complain);
3426 else
3428 fndecl = NULL_TREE;
3430 function = build_addr_func (function, complain);
3433 if (function == error_mark_node)
3434 return error_mark_node;
3436 fntype = TREE_TYPE (function);
3438 if (TYPE_PTRMEMFUNC_P (fntype))
3440 if (complain & tf_error)
3441 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3442 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3443 original, original);
3444 return error_mark_node;
3447 is_method = (TYPE_PTR_P (fntype)
3448 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3450 if (!(TYPE_PTRFN_P (fntype)
3451 || is_method
3452 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3454 if (complain & tf_error)
3456 if (!flag_diagnostics_show_caret)
3457 error_at (input_location,
3458 "%qE cannot be used as a function", original);
3459 else if (DECL_P (original))
3460 error_at (input_location,
3461 "%qD cannot be used as a function", original);
3462 else
3463 error_at (input_location,
3464 "expression cannot be used as a function");
3467 return error_mark_node;
3470 /* fntype now gets the type of function pointed to. */
3471 fntype = TREE_TYPE (fntype);
3472 parm_types = TYPE_ARG_TYPES (fntype);
3474 if (params == NULL)
3476 allocated = make_tree_vector ();
3477 params = &allocated;
3480 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3481 complain);
3482 if (nargs < 0)
3483 return error_mark_node;
3485 argarray = (*params)->address ();
3487 /* Check for errors in format strings and inappropriately
3488 null parameters. */
3489 check_function_arguments (fntype, nargs, argarray);
3491 ret = build_cxx_call (function, nargs, argarray, complain);
3493 if (allocated != NULL)
3494 release_tree_vector (allocated);
3496 return ret;
3499 /* Subroutine of convert_arguments.
3500 Warn about wrong number of args are genereted. */
3502 static void
3503 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3505 if (fndecl)
3507 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3509 if (DECL_NAME (fndecl) == NULL_TREE
3510 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3511 error_at (loc,
3512 too_many_p
3513 ? G_("too many arguments to constructor %q#D")
3514 : G_("too few arguments to constructor %q#D"),
3515 fndecl);
3516 else
3517 error_at (loc,
3518 too_many_p
3519 ? G_("too many arguments to member function %q#D")
3520 : G_("too few arguments to member function %q#D"),
3521 fndecl);
3523 else
3524 error_at (loc,
3525 too_many_p
3526 ? G_("too many arguments to function %q#D")
3527 : G_("too few arguments to function %q#D"),
3528 fndecl);
3529 inform (DECL_SOURCE_LOCATION (fndecl),
3530 "declared here");
3532 else
3534 if (c_dialect_objc () && objc_message_selector ())
3535 error_at (loc,
3536 too_many_p
3537 ? G_("too many arguments to method %q#D")
3538 : G_("too few arguments to method %q#D"),
3539 objc_message_selector ());
3540 else
3541 error_at (loc, too_many_p ? G_("too many arguments to function")
3542 : G_("too few arguments to function"));
3546 /* Convert the actual parameter expressions in the list VALUES to the
3547 types in the list TYPELIST. The converted expressions are stored
3548 back in the VALUES vector.
3549 If parmdecls is exhausted, or when an element has NULL as its type,
3550 perform the default conversions.
3552 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3554 This is also where warnings about wrong number of args are generated.
3556 Returns the actual number of arguments processed (which might be less
3557 than the length of the vector), or -1 on error.
3559 In C++, unspecified trailing parameters can be filled in with their
3560 default arguments, if such were specified. Do so here. */
3562 static int
3563 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3564 int flags, tsubst_flags_t complain)
3566 tree typetail;
3567 unsigned int i;
3569 /* Argument passing is always copy-initialization. */
3570 flags |= LOOKUP_ONLYCONVERTING;
3572 for (i = 0, typetail = typelist;
3573 i < vec_safe_length (*values);
3574 i++)
3576 tree type = typetail ? TREE_VALUE (typetail) : 0;
3577 tree val = (**values)[i];
3579 if (val == error_mark_node || type == error_mark_node)
3580 return -1;
3582 if (type == void_type_node)
3584 if (complain & tf_error)
3586 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3587 return i;
3589 else
3590 return -1;
3593 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3594 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3595 if (TREE_CODE (val) == NOP_EXPR
3596 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3597 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3598 val = TREE_OPERAND (val, 0);
3600 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3602 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3603 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3604 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3605 val = decay_conversion (val, complain);
3608 if (val == error_mark_node)
3609 return -1;
3611 if (type != 0)
3613 /* Formal parm type is specified by a function prototype. */
3614 tree parmval;
3616 if (!COMPLETE_TYPE_P (complete_type (type)))
3618 if (complain & tf_error)
3620 if (fndecl)
3621 error ("parameter %P of %qD has incomplete type %qT",
3622 i, fndecl, type);
3623 else
3624 error ("parameter %P has incomplete type %qT", i, type);
3626 parmval = error_mark_node;
3628 else
3630 parmval = convert_for_initialization
3631 (NULL_TREE, type, val, flags,
3632 ICR_ARGPASS, fndecl, i, complain);
3633 parmval = convert_for_arg_passing (type, parmval, complain);
3636 if (parmval == error_mark_node)
3637 return -1;
3639 (**values)[i] = parmval;
3641 else
3643 if (fndecl && DECL_BUILT_IN (fndecl)
3644 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3645 /* Don't do ellipsis conversion for __built_in_constant_p
3646 as this will result in spurious errors for non-trivial
3647 types. */
3648 val = require_complete_type_sfinae (val, complain);
3649 else
3650 val = convert_arg_to_ellipsis (val, complain);
3652 (**values)[i] = val;
3655 if (typetail)
3656 typetail = TREE_CHAIN (typetail);
3659 if (typetail != 0 && typetail != void_list_node)
3661 /* See if there are default arguments that can be used. Because
3662 we hold default arguments in the FUNCTION_TYPE (which is so
3663 wrong), we can see default parameters here from deduced
3664 contexts (and via typeof) for indirect function calls.
3665 Fortunately we know whether we have a function decl to
3666 provide default arguments in a language conformant
3667 manner. */
3668 if (fndecl && TREE_PURPOSE (typetail)
3669 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3671 for (; typetail != void_list_node; ++i)
3673 tree parmval
3674 = convert_default_arg (TREE_VALUE (typetail),
3675 TREE_PURPOSE (typetail),
3676 fndecl, i, complain);
3678 if (parmval == error_mark_node)
3679 return -1;
3681 vec_safe_push (*values, parmval);
3682 typetail = TREE_CHAIN (typetail);
3683 /* ends with `...'. */
3684 if (typetail == NULL_TREE)
3685 break;
3688 else
3690 if (complain & tf_error)
3691 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3692 return -1;
3696 return (int) i;
3699 /* Build a binary-operation expression, after performing default
3700 conversions on the operands. CODE is the kind of expression to
3701 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3702 are the tree codes which correspond to ARG1 and ARG2 when issuing
3703 warnings about possibly misplaced parentheses. They may differ
3704 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3705 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3706 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3707 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3708 ARG2_CODE as ERROR_MARK. */
3710 tree
3711 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3712 enum tree_code arg1_code, tree arg2,
3713 enum tree_code arg2_code, tree *overload,
3714 tsubst_flags_t complain)
3716 tree orig_arg1;
3717 tree orig_arg2;
3718 tree expr;
3720 orig_arg1 = arg1;
3721 orig_arg2 = arg2;
3723 if (processing_template_decl)
3725 if (type_dependent_expression_p (arg1)
3726 || type_dependent_expression_p (arg2))
3727 return build_min_nt_loc (loc, code, arg1, arg2);
3728 arg1 = build_non_dependent_expr (arg1);
3729 arg2 = build_non_dependent_expr (arg2);
3732 if (code == DOTSTAR_EXPR)
3733 expr = build_m_component_ref (arg1, arg2, complain);
3734 else
3735 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3736 overload, complain);
3738 /* Check for cases such as x+y<<z which users are likely to
3739 misinterpret. But don't warn about obj << x + y, since that is a
3740 common idiom for I/O. */
3741 if (warn_parentheses
3742 && (complain & tf_warning)
3743 && !processing_template_decl
3744 && !error_operand_p (arg1)
3745 && !error_operand_p (arg2)
3746 && (code != LSHIFT_EXPR
3747 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3748 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3749 arg2_code, orig_arg2);
3751 if (processing_template_decl && expr != error_mark_node)
3752 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3754 return expr;
3757 /* Build and return an ARRAY_REF expression. */
3759 tree
3760 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3761 tsubst_flags_t complain)
3763 tree orig_arg1 = arg1;
3764 tree orig_arg2 = arg2;
3765 tree expr;
3767 if (processing_template_decl)
3769 if (type_dependent_expression_p (arg1)
3770 || type_dependent_expression_p (arg2))
3771 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3772 NULL_TREE, NULL_TREE);
3773 arg1 = build_non_dependent_expr (arg1);
3774 arg2 = build_non_dependent_expr (arg2);
3777 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3778 NULL_TREE, /*overload=*/NULL, complain);
3780 if (processing_template_decl && expr != error_mark_node)
3781 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3782 NULL_TREE, NULL_TREE);
3783 return expr;
3786 /* Return whether OP is an expression of enum type cast to integer
3787 type. In C++ even unsigned enum types are cast to signed integer
3788 types. We do not want to issue warnings about comparisons between
3789 signed and unsigned types when one of the types is an enum type.
3790 Those warnings are always false positives in practice. */
3792 static bool
3793 enum_cast_to_int (tree op)
3795 if (TREE_CODE (op) == NOP_EXPR
3796 && TREE_TYPE (op) == integer_type_node
3797 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3798 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3799 return true;
3801 /* The cast may have been pushed into a COND_EXPR. */
3802 if (TREE_CODE (op) == COND_EXPR)
3803 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3804 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3806 return false;
3809 /* For the c-common bits. */
3810 tree
3811 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3812 int /*convert_p*/)
3814 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3818 /* Build a binary-operation expression without default conversions.
3819 CODE is the kind of expression to build.
3820 LOCATION is the location_t of the operator in the source code.
3821 This function differs from `build' in several ways:
3822 the data type of the result is computed and recorded in it,
3823 warnings are generated if arg data types are invalid,
3824 special handling for addition and subtraction of pointers is known,
3825 and some optimization is done (operations on narrow ints
3826 are done in the narrower type when that gives the same result).
3827 Constant folding is also done before the result is returned.
3829 Note that the operands will never have enumeral types
3830 because either they have just had the default conversions performed
3831 or they have both just been converted to some other type in which
3832 the arithmetic is to be done.
3834 C++: must do special pointer arithmetic when implementing
3835 multiple inheritance, and deal with pointer to member functions. */
3837 tree
3838 cp_build_binary_op (location_t location,
3839 enum tree_code code, tree orig_op0, tree orig_op1,
3840 tsubst_flags_t complain)
3842 tree op0, op1;
3843 enum tree_code code0, code1;
3844 tree type0, type1;
3845 const char *invalid_op_diag;
3847 /* Expression code to give to the expression when it is built.
3848 Normally this is CODE, which is what the caller asked for,
3849 but in some special cases we change it. */
3850 enum tree_code resultcode = code;
3852 /* Data type in which the computation is to be performed.
3853 In the simplest cases this is the common type of the arguments. */
3854 tree result_type = NULL;
3856 /* Nonzero means operands have already been type-converted
3857 in whatever way is necessary.
3858 Zero means they need to be converted to RESULT_TYPE. */
3859 int converted = 0;
3861 /* Nonzero means create the expression with this type, rather than
3862 RESULT_TYPE. */
3863 tree build_type = 0;
3865 /* Nonzero means after finally constructing the expression
3866 convert it to this type. */
3867 tree final_type = 0;
3869 tree result;
3871 /* Nonzero if this is an operation like MIN or MAX which can
3872 safely be computed in short if both args are promoted shorts.
3873 Also implies COMMON.
3874 -1 indicates a bitwise operation; this makes a difference
3875 in the exact conditions for when it is safe to do the operation
3876 in a narrower mode. */
3877 int shorten = 0;
3879 /* Nonzero if this is a comparison operation;
3880 if both args are promoted shorts, compare the original shorts.
3881 Also implies COMMON. */
3882 int short_compare = 0;
3884 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3885 int common = 0;
3887 /* True if both operands have arithmetic type. */
3888 bool arithmetic_types_p;
3890 /* Apply default conversions. */
3891 op0 = orig_op0;
3892 op1 = orig_op1;
3894 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3895 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3896 || code == TRUTH_XOR_EXPR)
3898 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3899 op0 = decay_conversion (op0, complain);
3900 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3901 op1 = decay_conversion (op1, complain);
3903 else
3905 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3906 op0 = cp_default_conversion (op0, complain);
3907 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3908 op1 = cp_default_conversion (op1, complain);
3911 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3912 STRIP_TYPE_NOPS (op0);
3913 STRIP_TYPE_NOPS (op1);
3915 /* DTRT if one side is an overloaded function, but complain about it. */
3916 if (type_unknown_p (op0))
3918 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3919 if (t != error_mark_node)
3921 if (complain & tf_error)
3922 permerror (input_location, "assuming cast to type %qT from overloaded function",
3923 TREE_TYPE (t));
3924 op0 = t;
3927 if (type_unknown_p (op1))
3929 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3930 if (t != error_mark_node)
3932 if (complain & tf_error)
3933 permerror (input_location, "assuming cast to type %qT from overloaded function",
3934 TREE_TYPE (t));
3935 op1 = t;
3939 type0 = TREE_TYPE (op0);
3940 type1 = TREE_TYPE (op1);
3942 /* The expression codes of the data types of the arguments tell us
3943 whether the arguments are integers, floating, pointers, etc. */
3944 code0 = TREE_CODE (type0);
3945 code1 = TREE_CODE (type1);
3947 /* If an error was already reported for one of the arguments,
3948 avoid reporting another error. */
3949 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3950 return error_mark_node;
3952 if ((invalid_op_diag
3953 = targetm.invalid_binary_op (code, type0, type1)))
3955 if (complain & tf_error)
3956 error (invalid_op_diag);
3957 return error_mark_node;
3960 /* Issue warnings about peculiar, but valid, uses of NULL. */
3961 if ((orig_op0 == null_node || orig_op1 == null_node)
3962 /* It's reasonable to use pointer values as operands of &&
3963 and ||, so NULL is no exception. */
3964 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3965 && ( /* Both are NULL (or 0) and the operation was not a
3966 comparison or a pointer subtraction. */
3967 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3968 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3969 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3970 || (!null_ptr_cst_p (orig_op0)
3971 && !TYPE_PTR_OR_PTRMEM_P (type0))
3972 || (!null_ptr_cst_p (orig_op1)
3973 && !TYPE_PTR_OR_PTRMEM_P (type1)))
3974 && (complain & tf_warning))
3976 source_location loc =
3977 expansion_point_location_if_in_system_header (input_location);
3979 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
3982 /* In case when one of the operands of the binary operation is
3983 a vector and another is a scalar -- convert scalar to vector. */
3984 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
3986 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
3987 complain & tf_error);
3989 switch (convert_flag)
3991 case stv_error:
3992 return error_mark_node;
3993 case stv_firstarg:
3995 op0 = save_expr (op0);
3996 op0 = convert (TREE_TYPE (type1), op0);
3997 op0 = build_vector_from_val (type1, op0);
3998 type0 = TREE_TYPE (op0);
3999 code0 = TREE_CODE (type0);
4000 converted = 1;
4001 break;
4003 case stv_secondarg:
4005 op1 = save_expr (op1);
4006 op1 = convert (TREE_TYPE (type0), op1);
4007 op1 = build_vector_from_val (type0, op1);
4008 type1 = TREE_TYPE (op1);
4009 code1 = TREE_CODE (type1);
4010 converted = 1;
4011 break;
4013 default:
4014 break;
4018 switch (code)
4020 case MINUS_EXPR:
4021 /* Subtraction of two similar pointers.
4022 We must subtract them as integers, then divide by object size. */
4023 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4024 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4025 TREE_TYPE (type1)))
4026 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4027 complain);
4028 /* In all other cases except pointer - int, the usual arithmetic
4029 rules apply. */
4030 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4032 common = 1;
4033 break;
4035 /* The pointer - int case is just like pointer + int; fall
4036 through. */
4037 case PLUS_EXPR:
4038 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4039 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4041 tree ptr_operand;
4042 tree int_operand;
4043 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4044 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4045 if (processing_template_decl)
4047 result_type = TREE_TYPE (ptr_operand);
4048 break;
4050 return cp_pointer_int_sum (code,
4051 ptr_operand,
4052 int_operand);
4054 common = 1;
4055 break;
4057 case MULT_EXPR:
4058 common = 1;
4059 break;
4061 case TRUNC_DIV_EXPR:
4062 case CEIL_DIV_EXPR:
4063 case FLOOR_DIV_EXPR:
4064 case ROUND_DIV_EXPR:
4065 case EXACT_DIV_EXPR:
4066 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4067 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4068 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4069 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4071 enum tree_code tcode0 = code0, tcode1 = code1;
4072 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4074 warn_for_div_by_zero (location, maybe_constant_value (cop1));
4076 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4077 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4078 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4079 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4081 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4082 resultcode = RDIV_EXPR;
4083 else
4084 /* When dividing two signed integers, we have to promote to int.
4085 unless we divide by a constant != -1. Note that default
4086 conversion will have been performed on the operands at this
4087 point, so we have to dig out the original type to find out if
4088 it was unsigned. */
4089 shorten = ((TREE_CODE (op0) == NOP_EXPR
4090 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4091 || (TREE_CODE (op1) == INTEGER_CST
4092 && ! integer_all_onesp (op1)));
4094 common = 1;
4096 break;
4098 case BIT_AND_EXPR:
4099 case BIT_IOR_EXPR:
4100 case BIT_XOR_EXPR:
4101 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4102 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4103 && !VECTOR_FLOAT_TYPE_P (type0)
4104 && !VECTOR_FLOAT_TYPE_P (type1)))
4105 shorten = -1;
4106 break;
4108 case TRUNC_MOD_EXPR:
4109 case FLOOR_MOD_EXPR:
4111 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4113 warn_for_div_by_zero (location, maybe_constant_value (cop1));
4116 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4117 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4118 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4119 common = 1;
4120 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4122 /* Although it would be tempting to shorten always here, that loses
4123 on some targets, since the modulo instruction is undefined if the
4124 quotient can't be represented in the computation mode. We shorten
4125 only if unsigned or if dividing by something we know != -1. */
4126 shorten = ((TREE_CODE (op0) == NOP_EXPR
4127 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4128 || (TREE_CODE (op1) == INTEGER_CST
4129 && ! integer_all_onesp (op1)));
4130 common = 1;
4132 break;
4134 case TRUTH_ANDIF_EXPR:
4135 case TRUTH_ORIF_EXPR:
4136 case TRUTH_AND_EXPR:
4137 case TRUTH_OR_EXPR:
4138 result_type = boolean_type_node;
4139 break;
4141 /* Shift operations: result has same type as first operand;
4142 always convert second operand to int.
4143 Also set SHORT_SHIFT if shifting rightward. */
4145 case RSHIFT_EXPR:
4146 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4147 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4149 result_type = type0;
4150 converted = 1;
4152 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4153 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4154 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4155 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4157 result_type = type0;
4158 converted = 1;
4160 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4162 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4163 const_op1 = maybe_constant_value (const_op1);
4164 if (TREE_CODE (const_op1) != INTEGER_CST)
4165 const_op1 = op1;
4166 result_type = type0;
4167 if (TREE_CODE (const_op1) == INTEGER_CST)
4169 if (tree_int_cst_lt (const_op1, integer_zero_node))
4171 if ((complain & tf_warning)
4172 && c_inhibit_evaluation_warnings == 0)
4173 warning (0, "right shift count is negative");
4175 else
4177 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4178 && (complain & tf_warning)
4179 && c_inhibit_evaluation_warnings == 0)
4180 warning (0, "right shift count >= width of type");
4183 /* Convert the shift-count to an integer, regardless of
4184 size of value being shifted. */
4185 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4186 op1 = cp_convert (integer_type_node, op1, complain);
4187 /* Avoid converting op1 to result_type later. */
4188 converted = 1;
4190 break;
4192 case LSHIFT_EXPR:
4193 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4194 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4196 result_type = type0;
4197 converted = 1;
4199 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4200 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4201 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4202 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4204 result_type = type0;
4205 converted = 1;
4207 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4209 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4210 const_op1 = maybe_constant_value (const_op1);
4211 if (TREE_CODE (const_op1) != INTEGER_CST)
4212 const_op1 = op1;
4213 result_type = type0;
4214 if (TREE_CODE (const_op1) == INTEGER_CST)
4216 if (tree_int_cst_lt (const_op1, integer_zero_node))
4218 if ((complain & tf_warning)
4219 && c_inhibit_evaluation_warnings == 0)
4220 warning (0, "left shift count is negative");
4222 else if (compare_tree_int (const_op1,
4223 TYPE_PRECISION (type0)) >= 0)
4225 if ((complain & tf_warning)
4226 && c_inhibit_evaluation_warnings == 0)
4227 warning (0, "left shift count >= width of type");
4230 /* Convert the shift-count to an integer, regardless of
4231 size of value being shifted. */
4232 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4233 op1 = cp_convert (integer_type_node, op1, complain);
4234 /* Avoid converting op1 to result_type later. */
4235 converted = 1;
4237 break;
4239 case RROTATE_EXPR:
4240 case LROTATE_EXPR:
4241 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4243 result_type = type0;
4244 if (TREE_CODE (op1) == INTEGER_CST)
4246 if (tree_int_cst_lt (op1, integer_zero_node))
4248 if (complain & tf_warning)
4249 warning (0, (code == LROTATE_EXPR)
4250 ? G_("left rotate count is negative")
4251 : G_("right rotate count is negative"));
4253 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4255 if (complain & tf_warning)
4256 warning (0, (code == LROTATE_EXPR)
4257 ? G_("left rotate count >= width of type")
4258 : G_("right rotate count >= width of type"));
4261 /* Convert the shift-count to an integer, regardless of
4262 size of value being shifted. */
4263 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4264 op1 = cp_convert (integer_type_node, op1, complain);
4266 break;
4268 case EQ_EXPR:
4269 case NE_EXPR:
4270 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4271 goto vector_compare;
4272 if ((complain & tf_warning)
4273 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4274 warning (OPT_Wfloat_equal,
4275 "comparing floating point with == or != is unsafe");
4276 if ((complain & tf_warning)
4277 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4278 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4279 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4281 build_type = boolean_type_node;
4282 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4283 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4284 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4285 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4286 short_compare = 1;
4287 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4288 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4289 result_type = composite_pointer_type (type0, type1, op0, op1,
4290 CPO_COMPARISON, complain);
4291 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4292 && null_ptr_cst_p (op1))
4294 if (TREE_CODE (op0) == ADDR_EXPR
4295 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4297 if ((complain & tf_warning)
4298 && c_inhibit_evaluation_warnings == 0)
4299 warning (OPT_Waddress, "the address of %qD will never be NULL",
4300 TREE_OPERAND (op0, 0));
4302 result_type = type0;
4304 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4305 && null_ptr_cst_p (op0))
4307 if (TREE_CODE (op1) == ADDR_EXPR
4308 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4310 if ((complain & tf_warning)
4311 && c_inhibit_evaluation_warnings == 0)
4312 warning (OPT_Waddress, "the address of %qD will never be NULL",
4313 TREE_OPERAND (op1, 0));
4315 result_type = type1;
4317 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4318 /* One of the operands must be of nullptr_t type. */
4319 result_type = TREE_TYPE (nullptr_node);
4320 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4322 result_type = type0;
4323 if (complain & tf_error)
4324 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4325 else
4326 return error_mark_node;
4328 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4330 result_type = type1;
4331 if (complain & tf_error)
4332 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4333 else
4334 return error_mark_node;
4336 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4338 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4339 == ptrmemfunc_vbit_in_delta)
4341 tree pfn0, delta0, e1, e2;
4343 if (TREE_SIDE_EFFECTS (op0))
4344 op0 = save_expr (op0);
4346 pfn0 = pfn_from_ptrmemfunc (op0);
4347 delta0 = delta_from_ptrmemfunc (op0);
4348 e1 = cp_build_binary_op (location,
4349 EQ_EXPR,
4350 pfn0,
4351 build_zero_cst (TREE_TYPE (pfn0)),
4352 complain);
4353 e2 = cp_build_binary_op (location,
4354 BIT_AND_EXPR,
4355 delta0,
4356 integer_one_node,
4357 complain);
4359 if (complain & tf_warning)
4360 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4362 e2 = cp_build_binary_op (location,
4363 EQ_EXPR, e2, integer_zero_node,
4364 complain);
4365 op0 = cp_build_binary_op (location,
4366 TRUTH_ANDIF_EXPR, e1, e2,
4367 complain);
4368 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4370 else
4372 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4373 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4375 result_type = TREE_TYPE (op0);
4377 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4378 return cp_build_binary_op (location, code, op1, op0, complain);
4379 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4381 tree type;
4382 /* E will be the final comparison. */
4383 tree e;
4384 /* E1 and E2 are for scratch. */
4385 tree e1;
4386 tree e2;
4387 tree pfn0;
4388 tree pfn1;
4389 tree delta0;
4390 tree delta1;
4392 type = composite_pointer_type (type0, type1, op0, op1,
4393 CPO_COMPARISON, complain);
4395 if (!same_type_p (TREE_TYPE (op0), type))
4396 op0 = cp_convert_and_check (type, op0, complain);
4397 if (!same_type_p (TREE_TYPE (op1), type))
4398 op1 = cp_convert_and_check (type, op1, complain);
4400 if (op0 == error_mark_node || op1 == error_mark_node)
4401 return error_mark_node;
4403 if (TREE_SIDE_EFFECTS (op0))
4404 op0 = save_expr (op0);
4405 if (TREE_SIDE_EFFECTS (op1))
4406 op1 = save_expr (op1);
4408 pfn0 = pfn_from_ptrmemfunc (op0);
4409 pfn1 = pfn_from_ptrmemfunc (op1);
4410 delta0 = delta_from_ptrmemfunc (op0);
4411 delta1 = delta_from_ptrmemfunc (op1);
4412 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4413 == ptrmemfunc_vbit_in_delta)
4415 /* We generate:
4417 (op0.pfn == op1.pfn
4418 && ((op0.delta == op1.delta)
4419 || (!op0.pfn && op0.delta & 1 == 0
4420 && op1.delta & 1 == 0))
4422 The reason for the `!op0.pfn' bit is that a NULL
4423 pointer-to-member is any member with a zero PFN and
4424 LSB of the DELTA field is 0. */
4426 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4427 delta0,
4428 integer_one_node,
4429 complain);
4430 e1 = cp_build_binary_op (location,
4431 EQ_EXPR, e1, integer_zero_node,
4432 complain);
4433 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4434 delta1,
4435 integer_one_node,
4436 complain);
4437 e2 = cp_build_binary_op (location,
4438 EQ_EXPR, e2, integer_zero_node,
4439 complain);
4440 e1 = cp_build_binary_op (location,
4441 TRUTH_ANDIF_EXPR, e2, e1,
4442 complain);
4443 e2 = cp_build_binary_op (location, EQ_EXPR,
4444 pfn0,
4445 build_zero_cst (TREE_TYPE (pfn0)),
4446 complain);
4447 e2 = cp_build_binary_op (location,
4448 TRUTH_ANDIF_EXPR, e2, e1, complain);
4449 e1 = cp_build_binary_op (location,
4450 EQ_EXPR, delta0, delta1, complain);
4451 e1 = cp_build_binary_op (location,
4452 TRUTH_ORIF_EXPR, e1, e2, complain);
4454 else
4456 /* We generate:
4458 (op0.pfn == op1.pfn
4459 && (!op0.pfn || op0.delta == op1.delta))
4461 The reason for the `!op0.pfn' bit is that a NULL
4462 pointer-to-member is any member with a zero PFN; the
4463 DELTA field is unspecified. */
4465 e1 = cp_build_binary_op (location,
4466 EQ_EXPR, delta0, delta1, complain);
4467 e2 = cp_build_binary_op (location,
4468 EQ_EXPR,
4469 pfn0,
4470 build_zero_cst (TREE_TYPE (pfn0)),
4471 complain);
4472 e1 = cp_build_binary_op (location,
4473 TRUTH_ORIF_EXPR, e1, e2, complain);
4475 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4476 e = cp_build_binary_op (location,
4477 TRUTH_ANDIF_EXPR, e2, e1, complain);
4478 if (code == EQ_EXPR)
4479 return e;
4480 return cp_build_binary_op (location,
4481 EQ_EXPR, e, integer_zero_node, complain);
4483 else
4485 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4486 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4487 type1));
4488 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4489 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4490 type0));
4493 break;
4495 case MAX_EXPR:
4496 case MIN_EXPR:
4497 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4498 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4499 shorten = 1;
4500 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4501 result_type = composite_pointer_type (type0, type1, op0, op1,
4502 CPO_COMPARISON, complain);
4503 break;
4505 case LE_EXPR:
4506 case GE_EXPR:
4507 case LT_EXPR:
4508 case GT_EXPR:
4509 if (TREE_CODE (orig_op0) == STRING_CST
4510 || TREE_CODE (orig_op1) == STRING_CST)
4512 if (complain & tf_warning)
4513 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4516 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4518 vector_compare:
4519 tree intt;
4520 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4521 TREE_TYPE (type1)))
4523 error_at (location, "comparing vectors with different "
4524 "element types");
4525 inform (location, "operand types are %qT and %qT", type0, type1);
4526 return error_mark_node;
4529 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4531 error_at (location, "comparing vectors with different "
4532 "number of elements");
4533 inform (location, "operand types are %qT and %qT", type0, type1);
4534 return error_mark_node;
4537 /* Always construct signed integer vector type. */
4538 intt = c_common_type_for_size (GET_MODE_BITSIZE
4539 (TYPE_MODE (TREE_TYPE (type0))), 0);
4540 result_type = build_opaque_vector_type (intt,
4541 TYPE_VECTOR_SUBPARTS (type0));
4542 converted = 1;
4543 break;
4545 build_type = boolean_type_node;
4546 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4547 || code0 == ENUMERAL_TYPE)
4548 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4549 || code1 == ENUMERAL_TYPE))
4550 short_compare = 1;
4551 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4552 result_type = composite_pointer_type (type0, type1, op0, op1,
4553 CPO_COMPARISON, complain);
4554 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4556 result_type = type0;
4557 if (extra_warnings && (complain & tf_warning))
4558 warning (OPT_Wextra,
4559 "ordered comparison of pointer with integer zero");
4561 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4563 result_type = type1;
4564 if (extra_warnings && (complain & tf_warning))
4565 warning (OPT_Wextra,
4566 "ordered comparison of pointer with integer zero");
4568 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4569 /* One of the operands must be of nullptr_t type. */
4570 result_type = TREE_TYPE (nullptr_node);
4571 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4573 result_type = type0;
4574 if (complain & tf_error)
4575 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4576 else
4577 return error_mark_node;
4579 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4581 result_type = type1;
4582 if (complain & tf_error)
4583 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4584 else
4585 return error_mark_node;
4587 break;
4589 case UNORDERED_EXPR:
4590 case ORDERED_EXPR:
4591 case UNLT_EXPR:
4592 case UNLE_EXPR:
4593 case UNGT_EXPR:
4594 case UNGE_EXPR:
4595 case UNEQ_EXPR:
4596 build_type = integer_type_node;
4597 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4599 if (complain & tf_error)
4600 error ("unordered comparison on non-floating point argument");
4601 return error_mark_node;
4603 common = 1;
4604 break;
4606 default:
4607 break;
4610 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4611 || code0 == ENUMERAL_TYPE)
4612 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4613 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4614 arithmetic_types_p = 1;
4615 else
4617 arithmetic_types_p = 0;
4618 /* Vector arithmetic is only allowed when both sides are vectors. */
4619 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4621 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4622 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4623 TREE_TYPE (type1)))
4625 if (complain & tf_error)
4626 binary_op_error (location, code, type0, type1);
4627 return error_mark_node;
4629 arithmetic_types_p = 1;
4632 /* Determine the RESULT_TYPE, if it is not already known. */
4633 if (!result_type
4634 && arithmetic_types_p
4635 && (shorten || common || short_compare))
4637 result_type = cp_common_type (type0, type1);
4638 if (complain & tf_warning)
4639 do_warn_double_promotion (result_type, type0, type1,
4640 "implicit conversion from %qT to %qT "
4641 "to match other operand of binary "
4642 "expression",
4643 location);
4646 if (!result_type)
4648 if (complain & tf_error)
4649 error ("invalid operands of types %qT and %qT to binary %qO",
4650 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4651 return error_mark_node;
4654 /* If we're in a template, the only thing we need to know is the
4655 RESULT_TYPE. */
4656 if (processing_template_decl)
4658 /* Since the middle-end checks the type when doing a build2, we
4659 need to build the tree in pieces. This built tree will never
4660 get out of the front-end as we replace it when instantiating
4661 the template. */
4662 tree tmp = build2 (resultcode,
4663 build_type ? build_type : result_type,
4664 NULL_TREE, op1);
4665 TREE_OPERAND (tmp, 0) = op0;
4666 return tmp;
4669 if (arithmetic_types_p)
4671 bool first_complex = (code0 == COMPLEX_TYPE);
4672 bool second_complex = (code1 == COMPLEX_TYPE);
4673 int none_complex = (!first_complex && !second_complex);
4675 /* Adapted from patch for c/24581. */
4676 if (first_complex != second_complex
4677 && (code == PLUS_EXPR
4678 || code == MINUS_EXPR
4679 || code == MULT_EXPR
4680 || (code == TRUNC_DIV_EXPR && first_complex))
4681 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4682 && flag_signed_zeros)
4684 /* An operation on mixed real/complex operands must be
4685 handled specially, but the language-independent code can
4686 more easily optimize the plain complex arithmetic if
4687 -fno-signed-zeros. */
4688 tree real_type = TREE_TYPE (result_type);
4689 tree real, imag;
4690 if (first_complex)
4692 if (TREE_TYPE (op0) != result_type)
4693 op0 = cp_convert_and_check (result_type, op0, complain);
4694 if (TREE_TYPE (op1) != real_type)
4695 op1 = cp_convert_and_check (real_type, op1, complain);
4697 else
4699 if (TREE_TYPE (op0) != real_type)
4700 op0 = cp_convert_and_check (real_type, op0, complain);
4701 if (TREE_TYPE (op1) != result_type)
4702 op1 = cp_convert_and_check (result_type, op1, complain);
4704 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4705 return error_mark_node;
4706 if (first_complex)
4708 op0 = save_expr (op0);
4709 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4710 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4711 switch (code)
4713 case MULT_EXPR:
4714 case TRUNC_DIV_EXPR:
4715 op1 = save_expr (op1);
4716 imag = build2 (resultcode, real_type, imag, op1);
4717 /* Fall through. */
4718 case PLUS_EXPR:
4719 case MINUS_EXPR:
4720 real = build2 (resultcode, real_type, real, op1);
4721 break;
4722 default:
4723 gcc_unreachable();
4726 else
4728 op1 = save_expr (op1);
4729 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4730 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4731 switch (code)
4733 case MULT_EXPR:
4734 op0 = save_expr (op0);
4735 imag = build2 (resultcode, real_type, op0, imag);
4736 /* Fall through. */
4737 case PLUS_EXPR:
4738 real = build2 (resultcode, real_type, op0, real);
4739 break;
4740 case MINUS_EXPR:
4741 real = build2 (resultcode, real_type, op0, real);
4742 imag = build1 (NEGATE_EXPR, real_type, imag);
4743 break;
4744 default:
4745 gcc_unreachable();
4748 real = fold_if_not_in_template (real);
4749 imag = fold_if_not_in_template (imag);
4750 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4751 result = fold_if_not_in_template (result);
4752 return result;
4755 /* For certain operations (which identify themselves by shorten != 0)
4756 if both args were extended from the same smaller type,
4757 do the arithmetic in that type and then extend.
4759 shorten !=0 and !=1 indicates a bitwise operation.
4760 For them, this optimization is safe only if
4761 both args are zero-extended or both are sign-extended.
4762 Otherwise, we might change the result.
4763 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4764 but calculated in (unsigned short) it would be (unsigned short)-1. */
4766 if (shorten && none_complex)
4768 final_type = result_type;
4769 result_type = shorten_binary_op (result_type, op0, op1,
4770 shorten == -1);
4773 /* Comparison operations are shortened too but differently.
4774 They identify themselves by setting short_compare = 1. */
4776 if (short_compare)
4778 /* Don't write &op0, etc., because that would prevent op0
4779 from being kept in a register.
4780 Instead, make copies of the our local variables and
4781 pass the copies by reference, then copy them back afterward. */
4782 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4783 enum tree_code xresultcode = resultcode;
4784 tree val
4785 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4786 if (val != 0)
4787 return cp_convert (boolean_type_node, val, complain);
4788 op0 = xop0, op1 = xop1;
4789 converted = 1;
4790 resultcode = xresultcode;
4793 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4794 && warn_sign_compare
4795 /* Do not warn until the template is instantiated; we cannot
4796 bound the ranges of the arguments until that point. */
4797 && !processing_template_decl
4798 && (complain & tf_warning)
4799 && c_inhibit_evaluation_warnings == 0
4800 /* Even unsigned enum types promote to signed int. We don't
4801 want to issue -Wsign-compare warnings for this case. */
4802 && !enum_cast_to_int (orig_op0)
4803 && !enum_cast_to_int (orig_op1))
4805 tree oop0 = maybe_constant_value (orig_op0);
4806 tree oop1 = maybe_constant_value (orig_op1);
4808 if (TREE_CODE (oop0) != INTEGER_CST)
4809 oop0 = orig_op0;
4810 if (TREE_CODE (oop1) != INTEGER_CST)
4811 oop1 = orig_op1;
4812 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4813 result_type, resultcode);
4817 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4818 Then the expression will be built.
4819 It will be given type FINAL_TYPE if that is nonzero;
4820 otherwise, it will be given type RESULT_TYPE. */
4821 if (! converted)
4823 if (TREE_TYPE (op0) != result_type)
4824 op0 = cp_convert_and_check (result_type, op0, complain);
4825 if (TREE_TYPE (op1) != result_type)
4826 op1 = cp_convert_and_check (result_type, op1, complain);
4828 if (op0 == error_mark_node || op1 == error_mark_node)
4829 return error_mark_node;
4832 if (build_type == NULL_TREE)
4833 build_type = result_type;
4835 result = build2 (resultcode, build_type, op0, op1);
4836 result = fold_if_not_in_template (result);
4837 if (final_type != 0)
4838 result = cp_convert (final_type, result, complain);
4840 if (TREE_OVERFLOW_P (result)
4841 && !TREE_OVERFLOW_P (op0)
4842 && !TREE_OVERFLOW_P (op1))
4843 overflow_warning (location, result);
4845 return result;
4848 /* Return a tree for the sum or difference (RESULTCODE says which)
4849 of pointer PTROP and integer INTOP. */
4851 static tree
4852 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4854 tree res_type = TREE_TYPE (ptrop);
4856 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4857 in certain circumstance (when it's valid to do so). So we need
4858 to make sure it's complete. We don't need to check here, if we
4859 can actually complete it at all, as those checks will be done in
4860 pointer_int_sum() anyway. */
4861 complete_type (TREE_TYPE (res_type));
4863 return pointer_int_sum (input_location, resultcode, ptrop,
4864 fold_if_not_in_template (intop));
4867 /* Return a tree for the difference of pointers OP0 and OP1.
4868 The resulting tree has type int. */
4870 static tree
4871 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
4873 tree result;
4874 tree restype = ptrdiff_type_node;
4875 tree target_type = TREE_TYPE (ptrtype);
4877 if (!complete_type_or_else (target_type, NULL_TREE))
4878 return error_mark_node;
4880 if (VOID_TYPE_P (target_type))
4882 if (complain & tf_error)
4883 permerror (input_location, "ISO C++ forbids using pointer of "
4884 "type %<void *%> in subtraction");
4885 else
4886 return error_mark_node;
4888 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4890 if (complain & tf_error)
4891 permerror (input_location, "ISO C++ forbids using pointer to "
4892 "a function in subtraction");
4893 else
4894 return error_mark_node;
4896 if (TREE_CODE (target_type) == METHOD_TYPE)
4898 if (complain & tf_error)
4899 permerror (input_location, "ISO C++ forbids using pointer to "
4900 "a method in subtraction");
4901 else
4902 return error_mark_node;
4905 /* First do the subtraction as integers;
4906 then drop through to build the divide operator. */
4908 op0 = cp_build_binary_op (input_location,
4909 MINUS_EXPR,
4910 cp_convert (restype, op0, complain),
4911 cp_convert (restype, op1, complain),
4912 complain);
4914 /* This generates an error if op1 is a pointer to an incomplete type. */
4915 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4917 if (complain & tf_error)
4918 error ("invalid use of a pointer to an incomplete type in "
4919 "pointer arithmetic");
4920 else
4921 return error_mark_node;
4924 op1 = (TYPE_PTROB_P (ptrtype)
4925 ? size_in_bytes (target_type)
4926 : integer_one_node);
4928 /* Do the division. */
4930 result = build2 (EXACT_DIV_EXPR, restype, op0,
4931 cp_convert (restype, op1, complain));
4932 return fold_if_not_in_template (result);
4935 /* Construct and perhaps optimize a tree representation
4936 for a unary operation. CODE, a tree_code, specifies the operation
4937 and XARG is the operand. */
4939 tree
4940 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
4941 tsubst_flags_t complain)
4943 tree orig_expr = xarg;
4944 tree exp;
4945 int ptrmem = 0;
4947 if (processing_template_decl)
4949 if (type_dependent_expression_p (xarg))
4950 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
4952 xarg = build_non_dependent_expr (xarg);
4955 exp = NULL_TREE;
4957 /* [expr.unary.op] says:
4959 The address of an object of incomplete type can be taken.
4961 (And is just the ordinary address operator, not an overloaded
4962 "operator &".) However, if the type is a template
4963 specialization, we must complete the type at this point so that
4964 an overloaded "operator &" will be available if required. */
4965 if (code == ADDR_EXPR
4966 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4967 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4968 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4969 || (TREE_CODE (xarg) == OFFSET_REF)))
4970 /* Don't look for a function. */;
4971 else
4972 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
4973 NULL_TREE, /*overload=*/NULL, complain);
4974 if (!exp && code == ADDR_EXPR)
4976 if (is_overloaded_fn (xarg))
4978 tree fn = get_first_fn (xarg);
4979 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4981 if (complain & tf_error)
4982 error (DECL_CONSTRUCTOR_P (fn)
4983 ? G_("taking address of constructor %qE")
4984 : G_("taking address of destructor %qE"),
4985 xarg);
4986 return error_mark_node;
4990 /* A pointer to member-function can be formed only by saying
4991 &X::mf. */
4992 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4993 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4995 if (TREE_CODE (xarg) != OFFSET_REF
4996 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4998 if (complain & tf_error)
5000 error ("invalid use of %qE to form a "
5001 "pointer-to-member-function", xarg);
5002 if (TREE_CODE (xarg) != OFFSET_REF)
5003 inform (input_location, " a qualified-id is required");
5005 return error_mark_node;
5007 else
5009 if (complain & tf_error)
5010 error ("parentheses around %qE cannot be used to form a"
5011 " pointer-to-member-function",
5012 xarg);
5013 else
5014 return error_mark_node;
5015 PTRMEM_OK_P (xarg) = 1;
5019 if (TREE_CODE (xarg) == OFFSET_REF)
5021 ptrmem = PTRMEM_OK_P (xarg);
5023 if (!ptrmem && !flag_ms_extensions
5024 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5026 /* A single non-static member, make sure we don't allow a
5027 pointer-to-member. */
5028 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5029 TREE_OPERAND (xarg, 0),
5030 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5031 PTRMEM_OK_P (xarg) = ptrmem;
5035 exp = cp_build_addr_expr_strict (xarg, complain);
5038 if (processing_template_decl && exp != error_mark_node)
5039 exp = build_min_non_dep (code, exp, orig_expr,
5040 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5041 if (TREE_CODE (exp) == ADDR_EXPR)
5042 PTRMEM_OK_P (exp) = ptrmem;
5043 return exp;
5046 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5047 constants, where a null value is represented by an INTEGER_CST of
5048 -1. */
5050 tree
5051 cp_truthvalue_conversion (tree expr)
5053 tree type = TREE_TYPE (expr);
5054 if (TYPE_PTRDATAMEM_P (type))
5055 return build_binary_op (EXPR_LOCATION (expr),
5056 NE_EXPR, expr, nullptr_node, 1);
5057 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5059 /* With -Wzero-as-null-pointer-constant do not warn for an
5060 'if (p)' or a 'while (!p)', where p is a pointer. */
5061 tree ret;
5062 ++c_inhibit_evaluation_warnings;
5063 ret = c_common_truthvalue_conversion (input_location, expr);
5064 --c_inhibit_evaluation_warnings;
5065 return ret;
5067 else
5068 return c_common_truthvalue_conversion (input_location, expr);
5071 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5073 tree
5074 condition_conversion (tree expr)
5076 tree t;
5077 if (processing_template_decl)
5078 return expr;
5079 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5080 tf_warning_or_error, LOOKUP_NORMAL);
5081 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5082 return t;
5085 /* Returns the address of T. This function will fold away
5086 ADDR_EXPR of INDIRECT_REF. */
5088 tree
5089 build_address (tree t)
5091 if (error_operand_p (t) || !cxx_mark_addressable (t))
5092 return error_mark_node;
5093 t = build_fold_addr_expr (t);
5094 if (TREE_CODE (t) != ADDR_EXPR)
5095 t = rvalue (t);
5096 return t;
5099 /* Returns the address of T with type TYPE. */
5101 tree
5102 build_typed_address (tree t, tree type)
5104 if (error_operand_p (t) || !cxx_mark_addressable (t))
5105 return error_mark_node;
5106 t = build_fold_addr_expr_with_type (t, type);
5107 if (TREE_CODE (t) != ADDR_EXPR)
5108 t = rvalue (t);
5109 return t;
5112 /* Return a NOP_EXPR converting EXPR to TYPE. */
5114 tree
5115 build_nop (tree type, tree expr)
5117 if (type == error_mark_node || error_operand_p (expr))
5118 return expr;
5119 return build1 (NOP_EXPR, type, expr);
5122 /* Take the address of ARG, whatever that means under C++ semantics.
5123 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5124 and class rvalues as well.
5126 Nothing should call this function directly; instead, callers should use
5127 cp_build_addr_expr or cp_build_addr_expr_strict. */
5129 static tree
5130 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5132 tree argtype;
5133 tree val;
5135 if (!arg || error_operand_p (arg))
5136 return error_mark_node;
5138 arg = mark_lvalue_use (arg);
5139 argtype = lvalue_type (arg);
5141 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5143 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5144 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5146 /* They're trying to take the address of a unique non-static
5147 member function. This is ill-formed (except in MS-land),
5148 but let's try to DTRT.
5149 Note: We only handle unique functions here because we don't
5150 want to complain if there's a static overload; non-unique
5151 cases will be handled by instantiate_type. But we need to
5152 handle this case here to allow casts on the resulting PMF.
5153 We could defer this in non-MS mode, but it's easier to give
5154 a useful error here. */
5156 /* Inside constant member functions, the `this' pointer
5157 contains an extra const qualifier. TYPE_MAIN_VARIANT
5158 is used here to remove this const from the diagnostics
5159 and the created OFFSET_REF. */
5160 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5161 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5162 mark_used (fn);
5164 if (! flag_ms_extensions)
5166 tree name = DECL_NAME (fn);
5167 if (!(complain & tf_error))
5168 return error_mark_node;
5169 else if (current_class_type
5170 && TREE_OPERAND (arg, 0) == current_class_ref)
5171 /* An expression like &memfn. */
5172 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5173 " or parenthesized non-static member function to form"
5174 " a pointer to member function. Say %<&%T::%D%>",
5175 base, name);
5176 else
5177 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5178 " function to form a pointer to member function."
5179 " Say %<&%T::%D%>",
5180 base, name);
5182 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5185 /* Uninstantiated types are all functions. Taking the
5186 address of a function is a no-op, so just return the
5187 argument. */
5188 if (type_unknown_p (arg))
5189 return build1 (ADDR_EXPR, unknown_type_node, arg);
5191 if (TREE_CODE (arg) == OFFSET_REF)
5192 /* We want a pointer to member; bypass all the code for actually taking
5193 the address of something. */
5194 goto offset_ref;
5196 /* Anything not already handled and not a true memory reference
5197 is an error. */
5198 if (TREE_CODE (argtype) != FUNCTION_TYPE
5199 && TREE_CODE (argtype) != METHOD_TYPE)
5201 cp_lvalue_kind kind = lvalue_kind (arg);
5202 if (kind == clk_none)
5204 if (complain & tf_error)
5205 lvalue_error (input_location, lv_addressof);
5206 return error_mark_node;
5208 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5210 if (!(complain & tf_error))
5211 return error_mark_node;
5212 if (kind & clk_class)
5213 /* Make this a permerror because we used to accept it. */
5214 permerror (input_location, "taking address of temporary");
5215 else
5216 error ("taking address of xvalue (rvalue reference)");
5220 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5222 tree type = build_pointer_type (TREE_TYPE (argtype));
5223 arg = build1 (CONVERT_EXPR, type, arg);
5224 return arg;
5226 else if (pedantic && DECL_MAIN_P (arg))
5228 /* ARM $3.4 */
5229 /* Apparently a lot of autoconf scripts for C++ packages do this,
5230 so only complain if -Wpedantic. */
5231 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5232 pedwarn (input_location, OPT_Wpedantic,
5233 "ISO C++ forbids taking address of function %<::main%>");
5234 else if (flag_pedantic_errors)
5235 return error_mark_node;
5238 /* Let &* cancel out to simplify resulting code. */
5239 if (INDIRECT_REF_P (arg))
5241 /* We don't need to have `current_class_ptr' wrapped in a
5242 NON_LVALUE_EXPR node. */
5243 if (arg == current_class_ref)
5244 return current_class_ptr;
5246 arg = TREE_OPERAND (arg, 0);
5247 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5249 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5250 arg = build1 (CONVERT_EXPR, type, arg);
5252 else
5253 /* Don't let this be an lvalue. */
5254 arg = rvalue (arg);
5255 return arg;
5258 /* ??? Cope with user tricks that amount to offsetof. */
5259 if (TREE_CODE (argtype) != FUNCTION_TYPE
5260 && TREE_CODE (argtype) != METHOD_TYPE
5261 && argtype != unknown_type_node
5262 && (val = get_base_address (arg))
5263 && COMPLETE_TYPE_P (TREE_TYPE (val))
5264 && INDIRECT_REF_P (val)
5265 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5267 tree type = build_pointer_type (argtype);
5268 return fold_convert (type, fold_offsetof_1 (arg));
5271 /* Handle complex lvalues (when permitted)
5272 by reduction to simpler cases. */
5273 val = unary_complex_lvalue (ADDR_EXPR, arg);
5274 if (val != 0)
5275 return val;
5277 switch (TREE_CODE (arg))
5279 CASE_CONVERT:
5280 case FLOAT_EXPR:
5281 case FIX_TRUNC_EXPR:
5282 /* Even if we're not being pedantic, we cannot allow this
5283 extension when we're instantiating in a SFINAE
5284 context. */
5285 if (! lvalue_p (arg) && complain == tf_none)
5287 if (complain & tf_error)
5288 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5289 else
5290 return error_mark_node;
5292 break;
5294 case BASELINK:
5295 arg = BASELINK_FUNCTIONS (arg);
5296 /* Fall through. */
5298 case OVERLOAD:
5299 arg = OVL_CURRENT (arg);
5300 break;
5302 case OFFSET_REF:
5303 offset_ref:
5304 /* Turn a reference to a non-static data member into a
5305 pointer-to-member. */
5307 tree type;
5308 tree t;
5310 gcc_assert (PTRMEM_OK_P (arg));
5312 t = TREE_OPERAND (arg, 1);
5313 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5315 if (complain & tf_error)
5316 error ("cannot create pointer to reference member %qD", t);
5317 return error_mark_node;
5320 type = build_ptrmem_type (context_for_name_lookup (t),
5321 TREE_TYPE (t));
5322 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5323 return t;
5326 default:
5327 break;
5330 if (argtype != error_mark_node)
5332 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (argtype))
5334 if (complain & tf_warning_or_error)
5335 pedwarn (input_location, OPT_Wvla,
5336 "taking address of array of runtime bound");
5337 else
5338 return error_mark_node;
5340 argtype = build_pointer_type (argtype);
5343 /* In a template, we are processing a non-dependent expression
5344 so we can just form an ADDR_EXPR with the correct type. */
5345 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5347 val = build_address (arg);
5348 if (TREE_CODE (arg) == OFFSET_REF)
5349 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5351 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5353 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5355 /* We can only get here with a single static member
5356 function. */
5357 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5358 && DECL_STATIC_FUNCTION_P (fn));
5359 mark_used (fn);
5360 val = build_address (fn);
5361 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5362 /* Do not lose object's side effects. */
5363 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5364 TREE_OPERAND (arg, 0), val);
5366 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5368 if (complain & tf_error)
5369 error ("attempt to take address of bit-field structure member %qD",
5370 TREE_OPERAND (arg, 1));
5371 return error_mark_node;
5373 else
5375 tree object = TREE_OPERAND (arg, 0);
5376 tree field = TREE_OPERAND (arg, 1);
5377 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5378 (TREE_TYPE (object), decl_type_context (field)));
5379 val = build_address (arg);
5382 if (TYPE_PTR_P (argtype)
5383 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5385 build_ptrmemfunc_type (argtype);
5386 val = build_ptrmemfunc (argtype, val, 0,
5387 /*c_cast_p=*/false,
5388 complain);
5391 return val;
5394 /* Take the address of ARG if it has one, even if it's an rvalue. */
5396 tree
5397 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5399 return cp_build_addr_expr_1 (arg, 0, complain);
5402 /* Take the address of ARG, but only if it's an lvalue. */
5404 tree
5405 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5407 return cp_build_addr_expr_1 (arg, 1, complain);
5410 /* C++: Must handle pointers to members.
5412 Perhaps type instantiation should be extended to handle conversion
5413 from aggregates to types we don't yet know we want? (Or are those
5414 cases typically errors which should be reported?)
5416 NOCONVERT nonzero suppresses the default promotions
5417 (such as from short to int). */
5419 tree
5420 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5421 tsubst_flags_t complain)
5423 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5424 tree arg = xarg;
5425 tree argtype = 0;
5426 const char *errstring = NULL;
5427 tree val;
5428 const char *invalid_op_diag;
5430 if (!arg || error_operand_p (arg))
5431 return error_mark_node;
5433 if ((invalid_op_diag
5434 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5435 ? CONVERT_EXPR
5436 : code),
5437 TREE_TYPE (xarg))))
5439 if (complain & tf_error)
5440 error (invalid_op_diag);
5441 return error_mark_node;
5444 switch (code)
5446 case UNARY_PLUS_EXPR:
5447 case NEGATE_EXPR:
5449 int flags = WANT_ARITH | WANT_ENUM;
5450 /* Unary plus (but not unary minus) is allowed on pointers. */
5451 if (code == UNARY_PLUS_EXPR)
5452 flags |= WANT_POINTER;
5453 arg = build_expr_type_conversion (flags, arg, true);
5454 if (!arg)
5455 errstring = (code == NEGATE_EXPR
5456 ? _("wrong type argument to unary minus")
5457 : _("wrong type argument to unary plus"));
5458 else
5460 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5461 arg = cp_perform_integral_promotions (arg, complain);
5463 /* Make sure the result is not an lvalue: a unary plus or minus
5464 expression is always a rvalue. */
5465 arg = rvalue (arg);
5468 break;
5470 case BIT_NOT_EXPR:
5471 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5473 code = CONJ_EXPR;
5474 if (!noconvert)
5476 arg = cp_default_conversion (arg, complain);
5477 if (arg == error_mark_node)
5478 return error_mark_node;
5481 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5482 | WANT_VECTOR_OR_COMPLEX,
5483 arg, true)))
5484 errstring = _("wrong type argument to bit-complement");
5485 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5486 arg = cp_perform_integral_promotions (arg, complain);
5487 break;
5489 case ABS_EXPR:
5490 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5491 errstring = _("wrong type argument to abs");
5492 else if (!noconvert)
5494 arg = cp_default_conversion (arg, complain);
5495 if (arg == error_mark_node)
5496 return error_mark_node;
5498 break;
5500 case CONJ_EXPR:
5501 /* Conjugating a real value is a no-op, but allow it anyway. */
5502 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5503 errstring = _("wrong type argument to conjugation");
5504 else if (!noconvert)
5506 arg = cp_default_conversion (arg, complain);
5507 if (arg == error_mark_node)
5508 return error_mark_node;
5510 break;
5512 case TRUTH_NOT_EXPR:
5513 arg = perform_implicit_conversion (boolean_type_node, arg,
5514 complain);
5515 val = invert_truthvalue_loc (input_location, arg);
5516 if (arg != error_mark_node)
5517 return val;
5518 errstring = _("in argument to unary !");
5519 break;
5521 case NOP_EXPR:
5522 break;
5524 case REALPART_EXPR:
5525 case IMAGPART_EXPR:
5526 arg = build_real_imag_expr (input_location, code, arg);
5527 if (arg == error_mark_node)
5528 return arg;
5529 else
5530 return fold_if_not_in_template (arg);
5532 case PREINCREMENT_EXPR:
5533 case POSTINCREMENT_EXPR:
5534 case PREDECREMENT_EXPR:
5535 case POSTDECREMENT_EXPR:
5536 /* Handle complex lvalues (when permitted)
5537 by reduction to simpler cases. */
5539 val = unary_complex_lvalue (code, arg);
5540 if (val != 0)
5541 return val;
5543 arg = mark_lvalue_use (arg);
5545 /* Increment or decrement the real part of the value,
5546 and don't change the imaginary part. */
5547 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5549 tree real, imag;
5551 arg = stabilize_reference (arg);
5552 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5553 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5554 real = cp_build_unary_op (code, real, 1, complain);
5555 if (real == error_mark_node || imag == error_mark_node)
5556 return error_mark_node;
5557 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5558 real, imag);
5561 /* Report invalid types. */
5563 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5564 arg, true)))
5566 if (code == PREINCREMENT_EXPR)
5567 errstring = _("no pre-increment operator for type");
5568 else if (code == POSTINCREMENT_EXPR)
5569 errstring = _("no post-increment operator for type");
5570 else if (code == PREDECREMENT_EXPR)
5571 errstring = _("no pre-decrement operator for type");
5572 else
5573 errstring = _("no post-decrement operator for type");
5574 break;
5576 else if (arg == error_mark_node)
5577 return error_mark_node;
5579 /* Report something read-only. */
5581 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5582 || TREE_READONLY (arg))
5584 if (complain & tf_error)
5585 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5586 || code == POSTINCREMENT_EXPR)
5587 ? lv_increment : lv_decrement));
5588 else
5589 return error_mark_node;
5593 tree inc;
5594 tree declared_type = unlowered_expr_type (arg);
5596 argtype = TREE_TYPE (arg);
5598 /* ARM $5.2.5 last annotation says this should be forbidden. */
5599 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5601 if (complain & tf_error)
5602 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5603 ? G_("ISO C++ forbids incrementing an enum")
5604 : G_("ISO C++ forbids decrementing an enum"));
5605 else
5606 return error_mark_node;
5609 /* Compute the increment. */
5611 if (TYPE_PTR_P (argtype))
5613 tree type = complete_type (TREE_TYPE (argtype));
5615 if (!COMPLETE_OR_VOID_TYPE_P (type))
5617 if (complain & tf_error)
5618 error (((code == PREINCREMENT_EXPR
5619 || code == POSTINCREMENT_EXPR))
5620 ? G_("cannot increment a pointer to incomplete type %qT")
5621 : G_("cannot decrement a pointer to incomplete type %qT"),
5622 TREE_TYPE (argtype));
5623 else
5624 return error_mark_node;
5626 else if (!TYPE_PTROB_P (argtype))
5628 if (complain & tf_error)
5629 pedwarn (input_location, OPT_Wpointer_arith,
5630 (code == PREINCREMENT_EXPR
5631 || code == POSTINCREMENT_EXPR)
5632 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5633 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5634 argtype);
5635 else
5636 return error_mark_node;
5639 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5641 else
5642 inc = integer_one_node;
5644 inc = cp_convert (argtype, inc, complain);
5646 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5647 need to ask Objective-C to build the increment or decrement
5648 expression for it. */
5649 if (objc_is_property_ref (arg))
5650 return objc_build_incr_expr_for_property_ref (input_location, code,
5651 arg, inc);
5653 /* Complain about anything else that is not a true lvalue. */
5654 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5655 || code == POSTINCREMENT_EXPR)
5656 ? lv_increment : lv_decrement),
5657 complain))
5658 return error_mark_node;
5660 /* Forbid using -- on `bool'. */
5661 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5663 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5665 if (complain & tf_error)
5666 error ("invalid use of Boolean expression as operand "
5667 "to %<operator--%>");
5668 return error_mark_node;
5670 val = boolean_increment (code, arg);
5672 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5673 /* An rvalue has no cv-qualifiers. */
5674 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5675 else
5676 val = build2 (code, TREE_TYPE (arg), arg, inc);
5678 TREE_SIDE_EFFECTS (val) = 1;
5679 return val;
5682 case ADDR_EXPR:
5683 /* Note that this operation never does default_conversion
5684 regardless of NOCONVERT. */
5685 return cp_build_addr_expr (arg, complain);
5687 default:
5688 break;
5691 if (!errstring)
5693 if (argtype == 0)
5694 argtype = TREE_TYPE (arg);
5695 return fold_if_not_in_template (build1 (code, argtype, arg));
5698 if (complain & tf_error)
5699 error ("%s", errstring);
5700 return error_mark_node;
5703 /* Hook for the c-common bits that build a unary op. */
5704 tree
5705 build_unary_op (location_t /*location*/,
5706 enum tree_code code, tree xarg, int noconvert)
5708 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5711 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5712 for certain kinds of expressions which are not really lvalues
5713 but which we can accept as lvalues.
5715 If ARG is not a kind of expression we can handle, return
5716 NULL_TREE. */
5718 tree
5719 unary_complex_lvalue (enum tree_code code, tree arg)
5721 /* Inside a template, making these kinds of adjustments is
5722 pointless; we are only concerned with the type of the
5723 expression. */
5724 if (processing_template_decl)
5725 return NULL_TREE;
5727 /* Handle (a, b) used as an "lvalue". */
5728 if (TREE_CODE (arg) == COMPOUND_EXPR)
5730 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5731 tf_warning_or_error);
5732 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5733 TREE_OPERAND (arg, 0), real_result);
5736 /* Handle (a ? b : c) used as an "lvalue". */
5737 if (TREE_CODE (arg) == COND_EXPR
5738 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5739 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5741 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5742 if (TREE_CODE (arg) == MODIFY_EXPR
5743 || TREE_CODE (arg) == PREINCREMENT_EXPR
5744 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5746 tree lvalue = TREE_OPERAND (arg, 0);
5747 if (TREE_SIDE_EFFECTS (lvalue))
5749 lvalue = stabilize_reference (lvalue);
5750 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5751 lvalue, TREE_OPERAND (arg, 1));
5753 return unary_complex_lvalue
5754 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5757 if (code != ADDR_EXPR)
5758 return NULL_TREE;
5760 /* Handle (a = b) used as an "lvalue" for `&'. */
5761 if (TREE_CODE (arg) == MODIFY_EXPR
5762 || TREE_CODE (arg) == INIT_EXPR)
5764 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5765 tf_warning_or_error);
5766 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5767 arg, real_result);
5768 TREE_NO_WARNING (arg) = 1;
5769 return arg;
5772 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5773 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5774 || TREE_CODE (arg) == OFFSET_REF)
5775 return NULL_TREE;
5777 /* We permit compiler to make function calls returning
5778 objects of aggregate type look like lvalues. */
5780 tree targ = arg;
5782 if (TREE_CODE (targ) == SAVE_EXPR)
5783 targ = TREE_OPERAND (targ, 0);
5785 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5787 if (TREE_CODE (arg) == SAVE_EXPR)
5788 targ = arg;
5789 else
5790 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5791 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5794 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
5795 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5796 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5799 /* Don't let anything else be handled specially. */
5800 return NULL_TREE;
5803 /* Mark EXP saying that we need to be able to take the
5804 address of it; it should not be allocated in a register.
5805 Value is true if successful.
5807 C++: we do not allow `current_class_ptr' to be addressable. */
5809 bool
5810 cxx_mark_addressable (tree exp)
5812 tree x = exp;
5814 while (1)
5815 switch (TREE_CODE (x))
5817 case ADDR_EXPR:
5818 case COMPONENT_REF:
5819 case ARRAY_REF:
5820 case REALPART_EXPR:
5821 case IMAGPART_EXPR:
5822 x = TREE_OPERAND (x, 0);
5823 break;
5825 case PARM_DECL:
5826 if (x == current_class_ptr)
5828 error ("cannot take the address of %<this%>, which is an rvalue expression");
5829 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5830 return true;
5832 /* Fall through. */
5834 case VAR_DECL:
5835 /* Caller should not be trying to mark initialized
5836 constant fields addressable. */
5837 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5838 || DECL_IN_AGGR_P (x) == 0
5839 || TREE_STATIC (x)
5840 || DECL_EXTERNAL (x));
5841 /* Fall through. */
5843 case RESULT_DECL:
5844 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5845 && !DECL_ARTIFICIAL (x))
5847 if (VAR_P (x) && DECL_HARD_REGISTER (x))
5849 error
5850 ("address of explicit register variable %qD requested", x);
5851 return false;
5853 else if (extra_warnings)
5854 warning
5855 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5857 TREE_ADDRESSABLE (x) = 1;
5858 return true;
5860 case CONST_DECL:
5861 case FUNCTION_DECL:
5862 TREE_ADDRESSABLE (x) = 1;
5863 return true;
5865 case CONSTRUCTOR:
5866 TREE_ADDRESSABLE (x) = 1;
5867 return true;
5869 case TARGET_EXPR:
5870 TREE_ADDRESSABLE (x) = 1;
5871 cxx_mark_addressable (TREE_OPERAND (x, 0));
5872 return true;
5874 default:
5875 return true;
5879 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5881 tree
5882 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
5883 tsubst_flags_t complain)
5885 tree orig_ifexp = ifexp;
5886 tree orig_op1 = op1;
5887 tree orig_op2 = op2;
5888 tree expr;
5890 if (processing_template_decl)
5892 /* The standard says that the expression is type-dependent if
5893 IFEXP is type-dependent, even though the eventual type of the
5894 expression doesn't dependent on IFEXP. */
5895 if (type_dependent_expression_p (ifexp)
5896 /* As a GNU extension, the middle operand may be omitted. */
5897 || (op1 && type_dependent_expression_p (op1))
5898 || type_dependent_expression_p (op2))
5899 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
5900 ifexp = build_non_dependent_expr (ifexp);
5901 if (op1)
5902 op1 = build_non_dependent_expr (op1);
5903 op2 = build_non_dependent_expr (op2);
5906 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
5907 if (processing_template_decl && expr != error_mark_node
5908 && TREE_CODE (expr) != VEC_COND_EXPR)
5910 tree min = build_min_non_dep (COND_EXPR, expr,
5911 orig_ifexp, orig_op1, orig_op2);
5912 /* In C++11, remember that the result is an lvalue or xvalue.
5913 In C++98, lvalue_kind can just assume lvalue in a template. */
5914 if (cxx_dialect >= cxx0x
5915 && lvalue_or_rvalue_with_address_p (expr)
5916 && !lvalue_or_rvalue_with_address_p (min))
5917 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5918 !real_lvalue_p (expr));
5919 expr = convert_from_reference (min);
5921 return expr;
5924 /* Given a list of expressions, return a compound expression
5925 that performs them all and returns the value of the last of them. */
5927 tree
5928 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5929 tsubst_flags_t complain)
5931 tree expr = TREE_VALUE (list);
5933 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5934 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5936 if (complain & tf_error)
5937 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5938 "non-class type must not be parenthesized");
5939 else
5940 return error_mark_node;
5943 if (TREE_CHAIN (list))
5945 if (complain & tf_error)
5946 switch (exp)
5948 case ELK_INIT:
5949 permerror (input_location, "expression list treated as compound "
5950 "expression in initializer");
5951 break;
5952 case ELK_MEM_INIT:
5953 permerror (input_location, "expression list treated as compound "
5954 "expression in mem-initializer");
5955 break;
5956 case ELK_FUNC_CAST:
5957 permerror (input_location, "expression list treated as compound "
5958 "expression in functional cast");
5959 break;
5960 default:
5961 gcc_unreachable ();
5963 else
5964 return error_mark_node;
5966 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5967 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
5968 expr, TREE_VALUE (list), complain);
5971 return expr;
5974 /* Like build_x_compound_expr_from_list, but using a VEC. */
5976 tree
5977 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
5978 tsubst_flags_t complain)
5980 if (vec_safe_is_empty (vec))
5981 return NULL_TREE;
5982 else if (vec->length () == 1)
5983 return (*vec)[0];
5984 else
5986 tree expr;
5987 unsigned int ix;
5988 tree t;
5990 if (msg != NULL)
5992 if (complain & tf_error)
5993 permerror (input_location,
5994 "%s expression list treated as compound expression",
5995 msg);
5996 else
5997 return error_mark_node;
6000 expr = (*vec)[0];
6001 for (ix = 1; vec->iterate (ix, &t); ++ix)
6002 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6003 t, complain);
6005 return expr;
6009 /* Handle overloading of the ',' operator when needed. */
6011 tree
6012 build_x_compound_expr (location_t loc, tree op1, tree op2,
6013 tsubst_flags_t complain)
6015 tree result;
6016 tree orig_op1 = op1;
6017 tree orig_op2 = op2;
6019 if (processing_template_decl)
6021 if (type_dependent_expression_p (op1)
6022 || type_dependent_expression_p (op2))
6023 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6024 op1 = build_non_dependent_expr (op1);
6025 op2 = build_non_dependent_expr (op2);
6028 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6029 NULL_TREE, /*overload=*/NULL, complain);
6030 if (!result)
6031 result = cp_build_compound_expr (op1, op2, complain);
6033 if (processing_template_decl && result != error_mark_node)
6034 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6036 return result;
6039 /* Like cp_build_compound_expr, but for the c-common bits. */
6041 tree
6042 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6044 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6047 /* Build a compound expression. */
6049 tree
6050 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6052 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6054 if (lhs == error_mark_node || rhs == error_mark_node)
6055 return error_mark_node;
6057 if (TREE_CODE (rhs) == TARGET_EXPR)
6059 /* If the rhs is a TARGET_EXPR, then build the compound
6060 expression inside the target_expr's initializer. This
6061 helps the compiler to eliminate unnecessary temporaries. */
6062 tree init = TREE_OPERAND (rhs, 1);
6064 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6065 TREE_OPERAND (rhs, 1) = init;
6067 return rhs;
6070 if (type_unknown_p (rhs))
6072 if (complain & tf_error)
6073 error ("no context to resolve type of %qE", rhs);
6074 return error_mark_node;
6077 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6080 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6081 casts away constness. CAST gives the type of cast. Returns true
6082 if the cast is ill-formed, false if it is well-formed.
6084 ??? This function warns for casting away any qualifier not just
6085 const. We would like to specify exactly what qualifiers are casted
6086 away.
6089 static bool
6090 check_for_casting_away_constness (tree src_type, tree dest_type,
6091 enum tree_code cast, tsubst_flags_t complain)
6093 /* C-style casts are allowed to cast away constness. With
6094 WARN_CAST_QUAL, we still want to issue a warning. */
6095 if (cast == CAST_EXPR && !warn_cast_qual)
6096 return false;
6098 if (!casts_away_constness (src_type, dest_type, complain))
6099 return false;
6101 switch (cast)
6103 case CAST_EXPR:
6104 if (complain & tf_warning)
6105 warning (OPT_Wcast_qual,
6106 "cast from type %qT to type %qT casts away qualifiers",
6107 src_type, dest_type);
6108 return false;
6110 case STATIC_CAST_EXPR:
6111 if (complain & tf_error)
6112 error ("static_cast from type %qT to type %qT casts away qualifiers",
6113 src_type, dest_type);
6114 return true;
6116 case REINTERPRET_CAST_EXPR:
6117 if (complain & tf_error)
6118 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6119 src_type, dest_type);
6120 return true;
6122 default:
6123 gcc_unreachable();
6128 Warns if the cast from expression EXPR to type TYPE is useless.
6130 void
6131 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6133 if (warn_useless_cast
6134 && complain & tf_warning
6135 && c_inhibit_evaluation_warnings == 0)
6137 if (REFERENCE_REF_P (expr))
6138 expr = TREE_OPERAND (expr, 0);
6140 if ((TREE_CODE (type) == REFERENCE_TYPE
6141 && (TYPE_REF_IS_RVALUE (type)
6142 ? xvalue_p (expr) : real_lvalue_p (expr))
6143 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6144 || same_type_p (TREE_TYPE (expr), type))
6145 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6149 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6150 (another pointer-to-member type in the same hierarchy) and return
6151 the converted expression. If ALLOW_INVERSE_P is permitted, a
6152 pointer-to-derived may be converted to pointer-to-base; otherwise,
6153 only the other direction is permitted. If C_CAST_P is true, this
6154 conversion is taking place as part of a C-style cast. */
6156 tree
6157 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6158 bool c_cast_p, tsubst_flags_t complain)
6160 if (TYPE_PTRDATAMEM_P (type))
6162 tree delta;
6164 if (TREE_CODE (expr) == PTRMEM_CST)
6165 expr = cplus_expand_constant (expr);
6166 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6167 TYPE_PTRMEM_CLASS_TYPE (type),
6168 allow_inverse_p,
6169 c_cast_p, complain);
6170 if (delta == error_mark_node)
6171 return error_mark_node;
6173 if (!integer_zerop (delta))
6175 tree cond, op1, op2;
6177 cond = cp_build_binary_op (input_location,
6178 EQ_EXPR,
6179 expr,
6180 build_int_cst (TREE_TYPE (expr), -1),
6181 complain);
6182 op1 = build_nop (ptrdiff_type_node, expr);
6183 op2 = cp_build_binary_op (input_location,
6184 PLUS_EXPR, op1, delta,
6185 complain);
6187 expr = fold_build3_loc (input_location,
6188 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6192 return build_nop (type, expr);
6194 else
6195 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6196 allow_inverse_p, c_cast_p, complain);
6199 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6200 this static_cast is being attempted as one of the possible casts
6201 allowed by a C-style cast. (In that case, accessibility of base
6202 classes is not considered, and it is OK to cast away
6203 constness.) Return the result of the cast. *VALID_P is set to
6204 indicate whether or not the cast was valid. */
6206 static tree
6207 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6208 bool *valid_p, tsubst_flags_t complain)
6210 tree intype;
6211 tree result;
6212 cp_lvalue_kind clk;
6214 /* Assume the cast is valid. */
6215 *valid_p = true;
6217 intype = unlowered_expr_type (expr);
6219 /* Save casted types in the function's used types hash table. */
6220 used_types_insert (type);
6222 /* [expr.static.cast]
6224 An lvalue of type "cv1 B", where B is a class type, can be cast
6225 to type "reference to cv2 D", where D is a class derived (clause
6226 _class.derived_) from B, if a valid standard conversion from
6227 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6228 same cv-qualification as, or greater cv-qualification than, cv1,
6229 and B is not a virtual base class of D. */
6230 /* We check this case before checking the validity of "TYPE t =
6231 EXPR;" below because for this case:
6233 struct B {};
6234 struct D : public B { D(const B&); };
6235 extern B& b;
6236 void f() { static_cast<const D&>(b); }
6238 we want to avoid constructing a new D. The standard is not
6239 completely clear about this issue, but our interpretation is
6240 consistent with other compilers. */
6241 if (TREE_CODE (type) == REFERENCE_TYPE
6242 && CLASS_TYPE_P (TREE_TYPE (type))
6243 && CLASS_TYPE_P (intype)
6244 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6245 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6246 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6247 build_pointer_type (TYPE_MAIN_VARIANT
6248 (TREE_TYPE (type))),
6249 complain)
6250 && (c_cast_p
6251 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6253 tree base;
6255 /* There is a standard conversion from "D*" to "B*" even if "B"
6256 is ambiguous or inaccessible. If this is really a
6257 static_cast, then we check both for inaccessibility and
6258 ambiguity. However, if this is a static_cast being performed
6259 because the user wrote a C-style cast, then accessibility is
6260 not considered. */
6261 base = lookup_base (TREE_TYPE (type), intype,
6262 c_cast_p ? ba_unique : ba_check,
6263 NULL, complain);
6265 /* Convert from "B*" to "D*". This function will check that "B"
6266 is not a virtual base of "D". */
6267 expr = build_base_path (MINUS_EXPR, build_address (expr),
6268 base, /*nonnull=*/false, complain);
6269 /* Convert the pointer to a reference -- but then remember that
6270 there are no expressions with reference type in C++.
6272 We call rvalue so that there's an actual tree code
6273 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6274 is a variable with the same type, the conversion would get folded
6275 away, leaving just the variable and causing lvalue_kind to give
6276 the wrong answer. */
6277 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6280 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6281 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6282 if (TREE_CODE (type) == REFERENCE_TYPE
6283 && TYPE_REF_IS_RVALUE (type)
6284 && (clk = real_lvalue_p (expr))
6285 && reference_related_p (TREE_TYPE (type), intype)
6286 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6288 if (clk == clk_ordinary)
6290 /* Handle the (non-bit-field) lvalue case here by casting to
6291 lvalue reference and then changing it to an rvalue reference.
6292 Casting an xvalue to rvalue reference will be handled by the
6293 main code path. */
6294 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6295 result = (perform_direct_initialization_if_possible
6296 (lref, expr, c_cast_p, complain));
6297 result = cp_fold_convert (type, result);
6298 /* Make sure we don't fold back down to a named rvalue reference,
6299 because that would be an lvalue. */
6300 if (DECL_P (result))
6301 result = build1 (NON_LVALUE_EXPR, type, result);
6302 return convert_from_reference (result);
6304 else
6305 /* For a bit-field or packed field, bind to a temporary. */
6306 expr = rvalue (expr);
6309 /* Resolve overloaded address here rather than once in
6310 implicit_conversion and again in the inverse code below. */
6311 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6313 expr = instantiate_type (type, expr, complain);
6314 intype = TREE_TYPE (expr);
6317 /* [expr.static.cast]
6319 Any expression can be explicitly converted to type cv void. */
6320 if (VOID_TYPE_P (type))
6321 return convert_to_void (expr, ICV_CAST, complain);
6323 /* [class.abstract]
6324 An abstract class shall not be used ... as the type of an explicit
6325 conversion. */
6326 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6327 return error_mark_node;
6329 /* [expr.static.cast]
6331 An expression e can be explicitly converted to a type T using a
6332 static_cast of the form static_cast<T>(e) if the declaration T
6333 t(e);" is well-formed, for some invented temporary variable
6334 t. */
6335 result = perform_direct_initialization_if_possible (type, expr,
6336 c_cast_p, complain);
6337 if (result)
6339 result = convert_from_reference (result);
6341 /* [expr.static.cast]
6343 If T is a reference type, the result is an lvalue; otherwise,
6344 the result is an rvalue. */
6345 if (TREE_CODE (type) != REFERENCE_TYPE)
6346 result = rvalue (result);
6347 return result;
6350 /* [expr.static.cast]
6352 The inverse of any standard conversion sequence (clause _conv_),
6353 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6354 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6355 (_conv.bool_) conversions, can be performed explicitly using
6356 static_cast subject to the restriction that the explicit
6357 conversion does not cast away constness (_expr.const.cast_), and
6358 the following additional rules for specific cases: */
6359 /* For reference, the conversions not excluded are: integral
6360 promotions, floating point promotion, integral conversions,
6361 floating point conversions, floating-integral conversions,
6362 pointer conversions, and pointer to member conversions. */
6363 /* DR 128
6365 A value of integral _or enumeration_ type can be explicitly
6366 converted to an enumeration type. */
6367 /* The effect of all that is that any conversion between any two
6368 types which are integral, floating, or enumeration types can be
6369 performed. */
6370 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6371 || SCALAR_FLOAT_TYPE_P (type))
6372 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6373 || SCALAR_FLOAT_TYPE_P (intype)))
6374 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6376 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6377 && CLASS_TYPE_P (TREE_TYPE (type))
6378 && CLASS_TYPE_P (TREE_TYPE (intype))
6379 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6380 (TREE_TYPE (intype))),
6381 build_pointer_type (TYPE_MAIN_VARIANT
6382 (TREE_TYPE (type))),
6383 complain))
6385 tree base;
6387 if (!c_cast_p
6388 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6389 complain))
6390 return error_mark_node;
6391 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6392 c_cast_p ? ba_unique : ba_check,
6393 NULL, complain);
6394 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6395 complain);
6396 return cp_fold_convert(type, expr);
6399 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6400 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6402 tree c1;
6403 tree c2;
6404 tree t1;
6405 tree t2;
6407 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6408 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6410 if (TYPE_PTRDATAMEM_P (type))
6412 t1 = (build_ptrmem_type
6413 (c1,
6414 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6415 t2 = (build_ptrmem_type
6416 (c2,
6417 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6419 else
6421 t1 = intype;
6422 t2 = type;
6424 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6426 if (!c_cast_p
6427 && check_for_casting_away_constness (intype, type,
6428 STATIC_CAST_EXPR,
6429 complain))
6430 return error_mark_node;
6431 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6432 c_cast_p, complain);
6436 /* [expr.static.cast]
6438 An rvalue of type "pointer to cv void" can be explicitly
6439 converted to a pointer to object type. A value of type pointer
6440 to object converted to "pointer to cv void" and back to the
6441 original pointer type will have its original value. */
6442 if (TYPE_PTR_P (intype)
6443 && VOID_TYPE_P (TREE_TYPE (intype))
6444 && TYPE_PTROB_P (type))
6446 if (!c_cast_p
6447 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6448 complain))
6449 return error_mark_node;
6450 return build_nop (type, expr);
6453 *valid_p = false;
6454 return error_mark_node;
6457 /* Return an expression representing static_cast<TYPE>(EXPR). */
6459 tree
6460 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6462 tree result;
6463 bool valid_p;
6465 if (type == error_mark_node || expr == error_mark_node)
6466 return error_mark_node;
6468 if (processing_template_decl)
6470 expr = build_min (STATIC_CAST_EXPR, type, expr);
6471 /* We don't know if it will or will not have side effects. */
6472 TREE_SIDE_EFFECTS (expr) = 1;
6473 return convert_from_reference (expr);
6476 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6477 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6478 if (TREE_CODE (type) != REFERENCE_TYPE
6479 && TREE_CODE (expr) == NOP_EXPR
6480 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6481 expr = TREE_OPERAND (expr, 0);
6483 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6484 complain);
6485 if (valid_p)
6487 if (result != error_mark_node)
6488 maybe_warn_about_useless_cast (type, expr, complain);
6489 return result;
6492 if (complain & tf_error)
6493 error ("invalid static_cast from type %qT to type %qT",
6494 TREE_TYPE (expr), type);
6495 return error_mark_node;
6498 /* EXPR is an expression with member function or pointer-to-member
6499 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6500 not permitted by ISO C++, but we accept it in some modes. If we
6501 are not in one of those modes, issue a diagnostic. Return the
6502 converted expression. */
6504 tree
6505 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6507 tree intype;
6508 tree decl;
6510 intype = TREE_TYPE (expr);
6511 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6512 || TREE_CODE (intype) == METHOD_TYPE);
6514 if (!(complain & tf_warning_or_error))
6515 return error_mark_node;
6517 if (pedantic || warn_pmf2ptr)
6518 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6519 "converting from %qT to %qT", intype, type);
6521 if (TREE_CODE (intype) == METHOD_TYPE)
6522 expr = build_addr_func (expr, complain);
6523 else if (TREE_CODE (expr) == PTRMEM_CST)
6524 expr = build_address (PTRMEM_CST_MEMBER (expr));
6525 else
6527 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6528 decl = build_address (decl);
6529 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6532 if (expr == error_mark_node)
6533 return error_mark_node;
6535 return build_nop (type, expr);
6538 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6539 If C_CAST_P is true, this reinterpret cast is being done as part of
6540 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6541 indicate whether or not reinterpret_cast was valid. */
6543 static tree
6544 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6545 bool *valid_p, tsubst_flags_t complain)
6547 tree intype;
6549 /* Assume the cast is invalid. */
6550 if (valid_p)
6551 *valid_p = true;
6553 if (type == error_mark_node || error_operand_p (expr))
6554 return error_mark_node;
6556 intype = TREE_TYPE (expr);
6558 /* Save casted types in the function's used types hash table. */
6559 used_types_insert (type);
6561 /* [expr.reinterpret.cast]
6562 An lvalue expression of type T1 can be cast to the type
6563 "reference to T2" if an expression of type "pointer to T1" can be
6564 explicitly converted to the type "pointer to T2" using a
6565 reinterpret_cast. */
6566 if (TREE_CODE (type) == REFERENCE_TYPE)
6568 if (! real_lvalue_p (expr))
6570 if (complain & tf_error)
6571 error ("invalid cast of an rvalue expression of type "
6572 "%qT to type %qT",
6573 intype, type);
6574 return error_mark_node;
6577 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6578 "B" are related class types; the reinterpret_cast does not
6579 adjust the pointer. */
6580 if (TYPE_PTR_P (intype)
6581 && (complain & tf_warning)
6582 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6583 COMPARE_BASE | COMPARE_DERIVED)))
6584 warning (0, "casting %qT to %qT does not dereference pointer",
6585 intype, type);
6587 expr = cp_build_addr_expr (expr, complain);
6589 if (warn_strict_aliasing > 2)
6590 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6592 if (expr != error_mark_node)
6593 expr = build_reinterpret_cast_1
6594 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6595 valid_p, complain);
6596 if (expr != error_mark_node)
6597 /* cp_build_indirect_ref isn't right for rvalue refs. */
6598 expr = convert_from_reference (fold_convert (type, expr));
6599 return expr;
6602 /* As a G++ extension, we consider conversions from member
6603 functions, and pointers to member functions to
6604 pointer-to-function and pointer-to-void types. If
6605 -Wno-pmf-conversions has not been specified,
6606 convert_member_func_to_ptr will issue an error message. */
6607 if ((TYPE_PTRMEMFUNC_P (intype)
6608 || TREE_CODE (intype) == METHOD_TYPE)
6609 && TYPE_PTR_P (type)
6610 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6611 || VOID_TYPE_P (TREE_TYPE (type))))
6612 return convert_member_func_to_ptr (type, expr, complain);
6614 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6615 array-to-pointer, and function-to-pointer conversions are
6616 performed. */
6617 expr = decay_conversion (expr, complain);
6619 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6620 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6621 if (TREE_CODE (expr) == NOP_EXPR
6622 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6623 expr = TREE_OPERAND (expr, 0);
6625 if (error_operand_p (expr))
6626 return error_mark_node;
6628 intype = TREE_TYPE (expr);
6630 /* [expr.reinterpret.cast]
6631 A pointer can be converted to any integral type large enough to
6632 hold it. ... A value of type std::nullptr_t can be converted to
6633 an integral type; the conversion has the same meaning and
6634 validity as a conversion of (void*)0 to the integral type. */
6635 if (CP_INTEGRAL_TYPE_P (type)
6636 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6638 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6640 if (complain & tf_error)
6641 permerror (input_location, "cast from %qT to %qT loses precision",
6642 intype, type);
6643 else
6644 return error_mark_node;
6646 if (NULLPTR_TYPE_P (intype))
6647 return build_int_cst (type, 0);
6649 /* [expr.reinterpret.cast]
6650 A value of integral or enumeration type can be explicitly
6651 converted to a pointer. */
6652 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6653 /* OK */
6655 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6656 || TYPE_PTR_OR_PTRMEM_P (type))
6657 && same_type_p (type, intype))
6658 /* DR 799 */
6659 return fold_if_not_in_template (build_nop (type, expr));
6660 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6661 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6662 return fold_if_not_in_template (build_nop (type, expr));
6663 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6664 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6666 tree sexpr = expr;
6668 if (!c_cast_p
6669 && check_for_casting_away_constness (intype, type,
6670 REINTERPRET_CAST_EXPR,
6671 complain))
6672 return error_mark_node;
6673 /* Warn about possible alignment problems. */
6674 if (STRICT_ALIGNMENT && warn_cast_align
6675 && (complain & tf_warning)
6676 && !VOID_TYPE_P (type)
6677 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6678 && COMPLETE_TYPE_P (TREE_TYPE (type))
6679 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6680 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6681 warning (OPT_Wcast_align, "cast from %qT to %qT "
6682 "increases required alignment of target type", intype, type);
6684 /* We need to strip nops here, because the front end likes to
6685 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6686 STRIP_NOPS (sexpr);
6687 if (warn_strict_aliasing <= 2)
6688 strict_aliasing_warning (intype, type, sexpr);
6690 return fold_if_not_in_template (build_nop (type, expr));
6692 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6693 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6695 if (pedantic && (complain & tf_warning))
6696 /* Only issue a warning, as we have always supported this
6697 where possible, and it is necessary in some cases. DR 195
6698 addresses this issue, but as of 2004/10/26 is still in
6699 drafting. */
6700 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6701 return fold_if_not_in_template (build_nop (type, expr));
6703 else if (TREE_CODE (type) == VECTOR_TYPE)
6704 return fold_if_not_in_template (convert_to_vector (type, expr));
6705 else if (TREE_CODE (intype) == VECTOR_TYPE
6706 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6707 return fold_if_not_in_template (convert_to_integer (type, expr));
6708 else
6710 if (valid_p)
6711 *valid_p = false;
6712 if (complain & tf_error)
6713 error ("invalid cast from type %qT to type %qT", intype, type);
6714 return error_mark_node;
6717 return cp_convert (type, expr, complain);
6720 tree
6721 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6723 tree r;
6725 if (type == error_mark_node || expr == error_mark_node)
6726 return error_mark_node;
6728 if (processing_template_decl)
6730 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6732 if (!TREE_SIDE_EFFECTS (t)
6733 && type_dependent_expression_p (expr))
6734 /* There might turn out to be side effects inside expr. */
6735 TREE_SIDE_EFFECTS (t) = 1;
6736 return convert_from_reference (t);
6739 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6740 /*valid_p=*/NULL, complain);
6741 if (r != error_mark_node)
6742 maybe_warn_about_useless_cast (type, expr, complain);
6743 return r;
6746 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6747 return an appropriate expression. Otherwise, return
6748 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6749 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6750 performing a C-style cast, its value upon return will indicate
6751 whether or not the conversion succeeded. */
6753 static tree
6754 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6755 bool *valid_p)
6757 tree src_type;
6758 tree reference_type;
6760 /* Callers are responsible for handling error_mark_node as a
6761 destination type. */
6762 gcc_assert (dst_type != error_mark_node);
6763 /* In a template, callers should be building syntactic
6764 representations of casts, not using this machinery. */
6765 gcc_assert (!processing_template_decl);
6767 /* Assume the conversion is invalid. */
6768 if (valid_p)
6769 *valid_p = false;
6771 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6773 if (complain & tf_error)
6774 error ("invalid use of const_cast with type %qT, "
6775 "which is not a pointer, "
6776 "reference, nor a pointer-to-data-member type", dst_type);
6777 return error_mark_node;
6780 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6782 if (complain & tf_error)
6783 error ("invalid use of const_cast with type %qT, which is a pointer "
6784 "or reference to a function type", dst_type);
6785 return error_mark_node;
6788 /* Save casted types in the function's used types hash table. */
6789 used_types_insert (dst_type);
6791 src_type = TREE_TYPE (expr);
6792 /* Expressions do not really have reference types. */
6793 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6794 src_type = TREE_TYPE (src_type);
6796 /* [expr.const.cast]
6798 For two object types T1 and T2, if a pointer to T1 can be explicitly
6799 converted to the type "pointer to T2" using a const_cast, then the
6800 following conversions can also be made:
6802 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6803 type T2 using the cast const_cast<T2&>;
6805 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6806 type T2 using the cast const_cast<T2&&>; and
6808 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6809 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6811 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6813 reference_type = dst_type;
6814 if (!TYPE_REF_IS_RVALUE (dst_type)
6815 ? real_lvalue_p (expr)
6816 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6817 ? lvalue_p (expr)
6818 : lvalue_or_rvalue_with_address_p (expr)))
6819 /* OK. */;
6820 else
6822 if (complain & tf_error)
6823 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6824 src_type, dst_type);
6825 return error_mark_node;
6827 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6828 src_type = build_pointer_type (src_type);
6830 else
6832 reference_type = NULL_TREE;
6833 /* If the destination type is not a reference type, the
6834 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6835 conversions are performed. */
6836 src_type = type_decays_to (src_type);
6837 if (src_type == error_mark_node)
6838 return error_mark_node;
6841 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
6843 if (comp_ptr_ttypes_const (dst_type, src_type))
6845 if (valid_p)
6847 *valid_p = true;
6848 /* This cast is actually a C-style cast. Issue a warning if
6849 the user is making a potentially unsafe cast. */
6850 check_for_casting_away_constness (src_type, dst_type,
6851 CAST_EXPR, complain);
6853 if (reference_type)
6855 expr = cp_build_addr_expr (expr, complain);
6856 if (expr == error_mark_node)
6857 return error_mark_node;
6858 expr = build_nop (reference_type, expr);
6859 return convert_from_reference (expr);
6861 else
6863 expr = decay_conversion (expr, complain);
6864 if (expr == error_mark_node)
6865 return error_mark_node;
6867 /* build_c_cast puts on a NOP_EXPR to make the result not an
6868 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6869 non-lvalue context. */
6870 if (TREE_CODE (expr) == NOP_EXPR
6871 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6872 expr = TREE_OPERAND (expr, 0);
6873 return build_nop (dst_type, expr);
6876 else if (valid_p
6877 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6878 TREE_TYPE (src_type)))
6879 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6880 complain);
6883 if (complain & tf_error)
6884 error ("invalid const_cast from type %qT to type %qT",
6885 src_type, dst_type);
6886 return error_mark_node;
6889 tree
6890 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6892 tree r;
6894 if (type == error_mark_node || error_operand_p (expr))
6895 return error_mark_node;
6897 if (processing_template_decl)
6899 tree t = build_min (CONST_CAST_EXPR, type, expr);
6901 if (!TREE_SIDE_EFFECTS (t)
6902 && type_dependent_expression_p (expr))
6903 /* There might turn out to be side effects inside expr. */
6904 TREE_SIDE_EFFECTS (t) = 1;
6905 return convert_from_reference (t);
6908 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
6909 if (r != error_mark_node)
6910 maybe_warn_about_useless_cast (type, expr, complain);
6911 return r;
6914 /* Like cp_build_c_cast, but for the c-common bits. */
6916 tree
6917 build_c_cast (location_t /*loc*/, tree type, tree expr)
6919 return cp_build_c_cast (type, expr, tf_warning_or_error);
6922 /* Build an expression representing an explicit C-style cast to type
6923 TYPE of expression EXPR. */
6925 tree
6926 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6928 tree value = expr;
6929 tree result;
6930 bool valid_p;
6932 if (type == error_mark_node || error_operand_p (expr))
6933 return error_mark_node;
6935 if (processing_template_decl)
6937 tree t = build_min (CAST_EXPR, type,
6938 tree_cons (NULL_TREE, value, NULL_TREE));
6939 /* We don't know if it will or will not have side effects. */
6940 TREE_SIDE_EFFECTS (t) = 1;
6941 return convert_from_reference (t);
6944 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6945 'Class') should always be retained, because this information aids
6946 in method lookup. */
6947 if (objc_is_object_ptr (type)
6948 && objc_is_object_ptr (TREE_TYPE (expr)))
6949 return build_nop (type, expr);
6951 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6952 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6953 if (TREE_CODE (type) != REFERENCE_TYPE
6954 && TREE_CODE (value) == NOP_EXPR
6955 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6956 value = TREE_OPERAND (value, 0);
6958 if (TREE_CODE (type) == ARRAY_TYPE)
6960 /* Allow casting from T1* to T2[] because Cfront allows it.
6961 NIHCL uses it. It is not valid ISO C++ however. */
6962 if (TYPE_PTR_P (TREE_TYPE (expr)))
6964 if (complain & tf_error)
6965 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6966 else
6967 return error_mark_node;
6968 type = build_pointer_type (TREE_TYPE (type));
6970 else
6972 if (complain & tf_error)
6973 error ("ISO C++ forbids casting to an array type %qT", type);
6974 return error_mark_node;
6978 if (TREE_CODE (type) == FUNCTION_TYPE
6979 || TREE_CODE (type) == METHOD_TYPE)
6981 if (complain & tf_error)
6982 error ("invalid cast to function type %qT", type);
6983 return error_mark_node;
6986 if (TYPE_PTR_P (type)
6987 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
6988 /* Casting to an integer of smaller size is an error detected elsewhere. */
6989 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
6990 /* Don't warn about converting any constant. */
6991 && !TREE_CONSTANT (value))
6992 warning_at (input_location, OPT_Wint_to_pointer_cast,
6993 "cast to pointer from integer of different size");
6995 /* A C-style cast can be a const_cast. */
6996 result = build_const_cast_1 (type, value, complain & tf_warning,
6997 &valid_p);
6998 if (valid_p)
7000 if (result != error_mark_node)
7001 maybe_warn_about_useless_cast (type, value, complain);
7002 return result;
7005 /* Or a static cast. */
7006 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7007 &valid_p, complain);
7008 /* Or a reinterpret_cast. */
7009 if (!valid_p)
7010 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7011 &valid_p, complain);
7012 /* The static_cast or reinterpret_cast may be followed by a
7013 const_cast. */
7014 if (valid_p
7015 /* A valid cast may result in errors if, for example, a
7016 conversion to an ambiguous base class is required. */
7017 && !error_operand_p (result))
7019 tree result_type;
7021 maybe_warn_about_useless_cast (type, value, complain);
7023 /* Non-class rvalues always have cv-unqualified type. */
7024 if (!CLASS_TYPE_P (type))
7025 type = TYPE_MAIN_VARIANT (type);
7026 result_type = TREE_TYPE (result);
7027 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7028 result_type = TYPE_MAIN_VARIANT (result_type);
7029 /* If the type of RESULT does not match TYPE, perform a
7030 const_cast to make it match. If the static_cast or
7031 reinterpret_cast succeeded, we will differ by at most
7032 cv-qualification, so the follow-on const_cast is guaranteed
7033 to succeed. */
7034 if (!same_type_p (non_reference (type), non_reference (result_type)))
7036 result = build_const_cast_1 (type, result, false, &valid_p);
7037 gcc_assert (valid_p);
7039 return result;
7042 return error_mark_node;
7045 /* For use from the C common bits. */
7046 tree
7047 build_modify_expr (location_t /*location*/,
7048 tree lhs, tree /*lhs_origtype*/,
7049 enum tree_code modifycode,
7050 location_t /*rhs_location*/, tree rhs,
7051 tree /*rhs_origtype*/)
7053 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7056 /* Build an assignment expression of lvalue LHS from value RHS.
7057 MODIFYCODE is the code for a binary operator that we use
7058 to combine the old value of LHS with RHS to get the new value.
7059 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7061 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7063 tree
7064 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7065 tsubst_flags_t complain)
7067 tree result;
7068 tree newrhs = rhs;
7069 tree lhstype = TREE_TYPE (lhs);
7070 tree olhstype = lhstype;
7071 bool plain_assign = (modifycode == NOP_EXPR);
7073 /* Avoid duplicate error messages from operands that had errors. */
7074 if (error_operand_p (lhs) || error_operand_p (rhs))
7075 return error_mark_node;
7077 /* Handle control structure constructs used as "lvalues". */
7078 switch (TREE_CODE (lhs))
7080 /* Handle --foo = 5; as these are valid constructs in C++. */
7081 case PREDECREMENT_EXPR:
7082 case PREINCREMENT_EXPR:
7083 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7084 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7085 stabilize_reference (TREE_OPERAND (lhs, 0)),
7086 TREE_OPERAND (lhs, 1));
7087 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7088 modifycode, rhs, complain);
7089 if (newrhs == error_mark_node)
7090 return error_mark_node;
7091 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7093 /* Handle (a, b) used as an "lvalue". */
7094 case COMPOUND_EXPR:
7095 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7096 modifycode, rhs, complain);
7097 if (newrhs == error_mark_node)
7098 return error_mark_node;
7099 return build2 (COMPOUND_EXPR, lhstype,
7100 TREE_OPERAND (lhs, 0), newrhs);
7102 case MODIFY_EXPR:
7103 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7104 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7105 stabilize_reference (TREE_OPERAND (lhs, 0)),
7106 TREE_OPERAND (lhs, 1));
7107 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7108 complain);
7109 if (newrhs == error_mark_node)
7110 return error_mark_node;
7111 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7113 case MIN_EXPR:
7114 case MAX_EXPR:
7115 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7116 when neither operand has side-effects. */
7117 if (!lvalue_or_else (lhs, lv_assign, complain))
7118 return error_mark_node;
7120 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7121 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7123 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7124 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7125 boolean_type_node,
7126 TREE_OPERAND (lhs, 0),
7127 TREE_OPERAND (lhs, 1)),
7128 TREE_OPERAND (lhs, 0),
7129 TREE_OPERAND (lhs, 1));
7130 /* Fall through. */
7132 /* Handle (a ? b : c) used as an "lvalue". */
7133 case COND_EXPR:
7135 /* Produce (a ? (b = rhs) : (c = rhs))
7136 except that the RHS goes through a save-expr
7137 so the code to compute it is only emitted once. */
7138 tree cond;
7139 tree preeval = NULL_TREE;
7141 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7143 if (complain & tf_error)
7144 error ("void value not ignored as it ought to be");
7145 return error_mark_node;
7148 rhs = stabilize_expr (rhs, &preeval);
7150 /* Check this here to avoid odd errors when trying to convert
7151 a throw to the type of the COND_EXPR. */
7152 if (!lvalue_or_else (lhs, lv_assign, complain))
7153 return error_mark_node;
7155 cond = build_conditional_expr
7156 (input_location, TREE_OPERAND (lhs, 0),
7157 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7158 modifycode, rhs, complain),
7159 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7160 modifycode, rhs, complain),
7161 complain);
7163 if (cond == error_mark_node)
7164 return cond;
7165 /* Make sure the code to compute the rhs comes out
7166 before the split. */
7167 if (preeval)
7168 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7169 return cond;
7172 default:
7173 break;
7176 if (modifycode == INIT_EXPR)
7178 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7179 /* Do the default thing. */;
7180 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7182 /* Compound literal. */
7183 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7184 /* Call convert to generate an error; see PR 11063. */
7185 rhs = convert (lhstype, rhs);
7186 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7187 TREE_SIDE_EFFECTS (result) = 1;
7188 return result;
7190 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7191 /* Do the default thing. */;
7192 else
7194 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7195 result = build_special_member_call (lhs, complete_ctor_identifier,
7196 &rhs_vec, lhstype, LOOKUP_NORMAL,
7197 complain);
7198 release_tree_vector (rhs_vec);
7199 if (result == NULL_TREE)
7200 return error_mark_node;
7201 return result;
7204 else
7206 lhs = require_complete_type_sfinae (lhs, complain);
7207 if (lhs == error_mark_node)
7208 return error_mark_node;
7210 if (modifycode == NOP_EXPR)
7212 if (c_dialect_objc ())
7214 result = objc_maybe_build_modify_expr (lhs, rhs);
7215 if (result)
7216 return result;
7219 /* `operator=' is not an inheritable operator. */
7220 if (! MAYBE_CLASS_TYPE_P (lhstype))
7221 /* Do the default thing. */;
7222 else
7224 result = build_new_op (input_location, MODIFY_EXPR,
7225 LOOKUP_NORMAL, lhs, rhs,
7226 make_node (NOP_EXPR), /*overload=*/NULL,
7227 complain);
7228 if (result == NULL_TREE)
7229 return error_mark_node;
7230 return result;
7232 lhstype = olhstype;
7234 else
7236 tree init = NULL_TREE;
7238 /* A binary op has been requested. Combine the old LHS
7239 value with the RHS producing the value we should actually
7240 store into the LHS. */
7241 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7242 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7243 || MAYBE_CLASS_TYPE_P (lhstype)));
7245 /* Preevaluate the RHS to make sure its evaluation is complete
7246 before the lvalue-to-rvalue conversion of the LHS:
7248 [expr.ass] With respect to an indeterminately-sequenced
7249 function call, the operation of a compound assignment is a
7250 single evaluation. [ Note: Therefore, a function call shall
7251 not intervene between the lvalue-to-rvalue conversion and the
7252 side effect associated with any single compound assignment
7253 operator. -- end note ] */
7254 lhs = stabilize_reference (lhs);
7255 if (TREE_SIDE_EFFECTS (rhs))
7256 rhs = mark_rvalue_use (rhs);
7257 rhs = stabilize_expr (rhs, &init);
7258 newrhs = cp_build_binary_op (input_location,
7259 modifycode, lhs, rhs,
7260 complain);
7261 if (newrhs == error_mark_node)
7263 if (complain & tf_error)
7264 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7265 TREE_TYPE (lhs), TREE_TYPE (rhs));
7266 return error_mark_node;
7269 if (init)
7270 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7272 /* Now it looks like a plain assignment. */
7273 modifycode = NOP_EXPR;
7274 if (c_dialect_objc ())
7276 result = objc_maybe_build_modify_expr (lhs, newrhs);
7277 if (result)
7278 return result;
7281 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7282 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7285 /* The left-hand side must be an lvalue. */
7286 if (!lvalue_or_else (lhs, lv_assign, complain))
7287 return error_mark_node;
7289 /* Warn about modifying something that is `const'. Don't warn if
7290 this is initialization. */
7291 if (modifycode != INIT_EXPR
7292 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7293 /* Functions are not modifiable, even though they are
7294 lvalues. */
7295 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7296 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7297 /* If it's an aggregate and any field is const, then it is
7298 effectively const. */
7299 || (CLASS_TYPE_P (lhstype)
7300 && C_TYPE_FIELDS_READONLY (lhstype))))
7302 if (complain & tf_error)
7303 cxx_readonly_error (lhs, lv_assign);
7304 else
7305 return error_mark_node;
7308 /* If storing into a structure or union member, it may have been given a
7309 lowered bitfield type. We need to convert to the declared type first,
7310 so retrieve it now. */
7312 olhstype = unlowered_expr_type (lhs);
7314 /* Convert new value to destination type. */
7316 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7318 int from_array;
7320 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7322 if (modifycode != INIT_EXPR)
7324 if (complain & tf_error)
7325 error ("assigning to an array from an initializer list");
7326 return error_mark_node;
7328 if (check_array_initializer (lhs, lhstype, newrhs))
7329 return error_mark_node;
7330 newrhs = digest_init (lhstype, newrhs, complain);
7331 if (newrhs == error_mark_node)
7332 return error_mark_node;
7335 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7336 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7338 if (complain & tf_error)
7339 error ("incompatible types in assignment of %qT to %qT",
7340 TREE_TYPE (rhs), lhstype);
7341 return error_mark_node;
7344 /* Allow array assignment in compiler-generated code. */
7345 else if (!current_function_decl
7346 || !DECL_DEFAULTED_FN (current_function_decl))
7348 /* This routine is used for both initialization and assignment.
7349 Make sure the diagnostic message differentiates the context. */
7350 if (complain & tf_error)
7352 if (modifycode == INIT_EXPR)
7353 error ("array used as initializer");
7354 else
7355 error ("invalid array assignment");
7357 return error_mark_node;
7360 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7361 ? 1 + (modifycode != INIT_EXPR): 0;
7362 return build_vec_init (lhs, NULL_TREE, newrhs,
7363 /*explicit_value_init_p=*/false,
7364 from_array, complain);
7367 if (modifycode == INIT_EXPR)
7368 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7369 LOOKUP_ONLYCONVERTING. */
7370 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7371 ICR_INIT, NULL_TREE, 0,
7372 complain);
7373 else
7374 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7375 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7377 if (!same_type_p (lhstype, olhstype))
7378 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7380 if (modifycode != INIT_EXPR)
7382 if (TREE_CODE (newrhs) == CALL_EXPR
7383 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7384 newrhs = build_cplus_new (lhstype, newrhs, complain);
7386 /* Can't initialize directly from a TARGET_EXPR, since that would
7387 cause the lhs to be constructed twice, and possibly result in
7388 accidental self-initialization. So we force the TARGET_EXPR to be
7389 expanded without a target. */
7390 if (TREE_CODE (newrhs) == TARGET_EXPR)
7391 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7392 TREE_OPERAND (newrhs, 0));
7395 if (newrhs == error_mark_node)
7396 return error_mark_node;
7398 if (c_dialect_objc () && flag_objc_gc)
7400 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7402 if (result)
7403 return result;
7406 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7407 lhstype, lhs, newrhs);
7409 TREE_SIDE_EFFECTS (result) = 1;
7410 if (!plain_assign)
7411 TREE_NO_WARNING (result) = 1;
7413 return result;
7416 tree
7417 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7418 tree rhs, tsubst_flags_t complain)
7420 if (processing_template_decl)
7421 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7422 build_min_nt_loc (loc, modifycode, NULL_TREE,
7423 NULL_TREE), rhs);
7425 if (modifycode != NOP_EXPR)
7427 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7428 make_node (modifycode), /*overload=*/NULL,
7429 complain);
7430 if (rval)
7432 TREE_NO_WARNING (rval) = 1;
7433 return rval;
7436 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7439 /* Helper function for get_delta_difference which assumes FROM is a base
7440 class of TO. Returns a delta for the conversion of pointer-to-member
7441 of FROM to pointer-to-member of TO. If the conversion is invalid and
7442 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7443 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7444 If C_CAST_P is true, this conversion is taking place as part of a
7445 C-style cast. */
7447 static tree
7448 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7449 tsubst_flags_t complain)
7451 tree binfo;
7452 base_kind kind;
7454 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7455 &kind, complain);
7457 if (binfo == error_mark_node)
7459 if (!(complain & tf_error))
7460 return error_mark_node;
7462 error (" in pointer to member function conversion");
7463 return size_zero_node;
7465 else if (binfo)
7467 if (kind != bk_via_virtual)
7468 return BINFO_OFFSET (binfo);
7469 else
7470 /* FROM is a virtual base class of TO. Issue an error or warning
7471 depending on whether or not this is a reinterpret cast. */
7473 if (!(complain & tf_error))
7474 return error_mark_node;
7476 error ("pointer to member conversion via virtual base %qT",
7477 BINFO_TYPE (binfo_from_vbase (binfo)));
7479 return size_zero_node;
7482 else
7483 return NULL_TREE;
7486 /* Get difference in deltas for different pointer to member function
7487 types. If the conversion is invalid and tf_error is not set in
7488 COMPLAIN, returns error_mark_node, otherwise returns an integer
7489 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7490 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7491 conversions as well. If C_CAST_P is true this conversion is taking
7492 place as part of a C-style cast.
7494 Note that the naming of FROM and TO is kind of backwards; the return
7495 value is what we add to a TO in order to get a FROM. They are named
7496 this way because we call this function to find out how to convert from
7497 a pointer to member of FROM to a pointer to member of TO. */
7499 static tree
7500 get_delta_difference (tree from, tree to,
7501 bool allow_inverse_p,
7502 bool c_cast_p, tsubst_flags_t complain)
7504 tree result;
7506 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7507 /* Pointer to member of incomplete class is permitted*/
7508 result = size_zero_node;
7509 else
7510 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7512 if (result == error_mark_node)
7513 return error_mark_node;
7515 if (!result)
7517 if (!allow_inverse_p)
7519 if (!(complain & tf_error))
7520 return error_mark_node;
7522 error_not_base_type (from, to);
7523 error (" in pointer to member conversion");
7524 result = size_zero_node;
7526 else
7528 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7530 if (result == error_mark_node)
7531 return error_mark_node;
7533 if (result)
7534 result = size_diffop_loc (input_location,
7535 size_zero_node, result);
7536 else
7538 if (!(complain & tf_error))
7539 return error_mark_node;
7541 error_not_base_type (from, to);
7542 error (" in pointer to member conversion");
7543 result = size_zero_node;
7548 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7549 result));
7552 /* Return a constructor for the pointer-to-member-function TYPE using
7553 the other components as specified. */
7555 tree
7556 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7558 tree u = NULL_TREE;
7559 tree delta_field;
7560 tree pfn_field;
7561 vec<constructor_elt, va_gc> *v;
7563 /* Pull the FIELD_DECLs out of the type. */
7564 pfn_field = TYPE_FIELDS (type);
7565 delta_field = DECL_CHAIN (pfn_field);
7567 /* Make sure DELTA has the type we want. */
7568 delta = convert_and_check (delta_type_node, delta);
7570 /* Convert to the correct target type if necessary. */
7571 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7573 /* Finish creating the initializer. */
7574 vec_alloc (v, 2);
7575 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7576 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7577 u = build_constructor (type, v);
7578 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7579 TREE_STATIC (u) = (TREE_CONSTANT (u)
7580 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7581 != NULL_TREE)
7582 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7583 != NULL_TREE));
7584 return u;
7587 /* Build a constructor for a pointer to member function. It can be
7588 used to initialize global variables, local variable, or used
7589 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7590 want to be.
7592 If FORCE is nonzero, then force this conversion, even if
7593 we would rather not do it. Usually set when using an explicit
7594 cast. A C-style cast is being processed iff C_CAST_P is true.
7596 Return error_mark_node, if something goes wrong. */
7598 tree
7599 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7600 tsubst_flags_t complain)
7602 tree fn;
7603 tree pfn_type;
7604 tree to_type;
7606 if (error_operand_p (pfn))
7607 return error_mark_node;
7609 pfn_type = TREE_TYPE (pfn);
7610 to_type = build_ptrmemfunc_type (type);
7612 /* Handle multiple conversions of pointer to member functions. */
7613 if (TYPE_PTRMEMFUNC_P (pfn_type))
7615 tree delta = NULL_TREE;
7616 tree npfn = NULL_TREE;
7617 tree n;
7619 if (!force
7620 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7621 LOOKUP_NORMAL, complain))
7623 if (complain & tf_error)
7624 error ("invalid conversion to type %qT from type %qT",
7625 to_type, pfn_type);
7626 else
7627 return error_mark_node;
7630 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7631 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7632 force,
7633 c_cast_p, complain);
7634 if (n == error_mark_node)
7635 return error_mark_node;
7637 /* We don't have to do any conversion to convert a
7638 pointer-to-member to its own type. But, we don't want to
7639 just return a PTRMEM_CST if there's an explicit cast; that
7640 cast should make the expression an invalid template argument. */
7641 if (TREE_CODE (pfn) != PTRMEM_CST)
7643 if (same_type_p (to_type, pfn_type))
7644 return pfn;
7645 else if (integer_zerop (n))
7646 return build_reinterpret_cast (to_type, pfn,
7647 complain);
7650 if (TREE_SIDE_EFFECTS (pfn))
7651 pfn = save_expr (pfn);
7653 /* Obtain the function pointer and the current DELTA. */
7654 if (TREE_CODE (pfn) == PTRMEM_CST)
7655 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7656 else
7658 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7659 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7662 /* Just adjust the DELTA field. */
7663 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7664 (TREE_TYPE (delta), ptrdiff_type_node));
7665 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7666 n = cp_build_binary_op (input_location,
7667 LSHIFT_EXPR, n, integer_one_node,
7668 complain);
7669 delta = cp_build_binary_op (input_location,
7670 PLUS_EXPR, delta, n, complain);
7671 return build_ptrmemfunc1 (to_type, delta, npfn);
7674 /* Handle null pointer to member function conversions. */
7675 if (null_ptr_cst_p (pfn))
7677 pfn = build_c_cast (input_location, type, pfn);
7678 return build_ptrmemfunc1 (to_type,
7679 integer_zero_node,
7680 pfn);
7683 if (type_unknown_p (pfn))
7684 return instantiate_type (type, pfn, complain);
7686 fn = TREE_OPERAND (pfn, 0);
7687 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7688 /* In a template, we will have preserved the
7689 OFFSET_REF. */
7690 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7691 return make_ptrmem_cst (to_type, fn);
7694 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7695 given by CST.
7697 ??? There is no consistency as to the types returned for the above
7698 values. Some code acts as if it were a sizetype and some as if it were
7699 integer_type_node. */
7701 void
7702 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7704 tree type = TREE_TYPE (cst);
7705 tree fn = PTRMEM_CST_MEMBER (cst);
7706 tree ptr_class, fn_class;
7708 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7710 /* The class that the function belongs to. */
7711 fn_class = DECL_CONTEXT (fn);
7713 /* The class that we're creating a pointer to member of. */
7714 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7716 /* First, calculate the adjustment to the function's class. */
7717 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7718 /*c_cast_p=*/0, tf_warning_or_error);
7720 if (!DECL_VIRTUAL_P (fn))
7721 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7722 build_addr_func (fn, tf_warning_or_error));
7723 else
7725 /* If we're dealing with a virtual function, we have to adjust 'this'
7726 again, to point to the base which provides the vtable entry for
7727 fn; the call will do the opposite adjustment. */
7728 tree orig_class = DECL_CONTEXT (fn);
7729 tree binfo = binfo_or_else (orig_class, fn_class);
7730 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7731 *delta, BINFO_OFFSET (binfo));
7732 *delta = fold_if_not_in_template (*delta);
7734 /* We set PFN to the vtable offset at which the function can be
7735 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7736 case delta is shifted left, and then incremented). */
7737 *pfn = DECL_VINDEX (fn);
7738 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7739 TYPE_SIZE_UNIT (vtable_entry_type));
7740 *pfn = fold_if_not_in_template (*pfn);
7742 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7744 case ptrmemfunc_vbit_in_pfn:
7745 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7746 integer_one_node);
7747 *pfn = fold_if_not_in_template (*pfn);
7748 break;
7750 case ptrmemfunc_vbit_in_delta:
7751 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7752 *delta, integer_one_node);
7753 *delta = fold_if_not_in_template (*delta);
7754 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7755 *delta, integer_one_node);
7756 *delta = fold_if_not_in_template (*delta);
7757 break;
7759 default:
7760 gcc_unreachable ();
7763 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7764 *pfn = fold_if_not_in_template (*pfn);
7768 /* Return an expression for PFN from the pointer-to-member function
7769 given by T. */
7771 static tree
7772 pfn_from_ptrmemfunc (tree t)
7774 if (TREE_CODE (t) == PTRMEM_CST)
7776 tree delta;
7777 tree pfn;
7779 expand_ptrmemfunc_cst (t, &delta, &pfn);
7780 if (pfn)
7781 return pfn;
7784 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7787 /* Return an expression for DELTA from the pointer-to-member function
7788 given by T. */
7790 static tree
7791 delta_from_ptrmemfunc (tree t)
7793 if (TREE_CODE (t) == PTRMEM_CST)
7795 tree delta;
7796 tree pfn;
7798 expand_ptrmemfunc_cst (t, &delta, &pfn);
7799 if (delta)
7800 return delta;
7803 return build_ptrmemfunc_access_expr (t, delta_identifier);
7806 /* Convert value RHS to type TYPE as preparation for an assignment to
7807 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7808 implicit conversion is. If FNDECL is non-NULL, we are doing the
7809 conversion in order to pass the PARMNUMth argument of FNDECL.
7810 If FNDECL is NULL, we are doing the conversion in function pointer
7811 argument passing, conversion in initialization, etc. */
7813 static tree
7814 convert_for_assignment (tree type, tree rhs,
7815 impl_conv_rhs errtype, tree fndecl, int parmnum,
7816 tsubst_flags_t complain, int flags)
7818 tree rhstype;
7819 enum tree_code coder;
7821 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7822 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7823 rhs = TREE_OPERAND (rhs, 0);
7825 rhstype = TREE_TYPE (rhs);
7826 coder = TREE_CODE (rhstype);
7828 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7829 && vector_types_convertible_p (type, rhstype, true))
7831 rhs = mark_rvalue_use (rhs);
7832 return convert (type, rhs);
7835 if (rhs == error_mark_node || rhstype == error_mark_node)
7836 return error_mark_node;
7837 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7838 return error_mark_node;
7840 /* The RHS of an assignment cannot have void type. */
7841 if (coder == VOID_TYPE)
7843 if (complain & tf_error)
7844 error ("void value not ignored as it ought to be");
7845 return error_mark_node;
7848 if (c_dialect_objc ())
7850 int parmno;
7851 tree selector;
7852 tree rname = fndecl;
7854 switch (errtype)
7856 case ICR_ASSIGN:
7857 parmno = -1;
7858 break;
7859 case ICR_INIT:
7860 parmno = -2;
7861 break;
7862 default:
7863 selector = objc_message_selector ();
7864 parmno = parmnum;
7865 if (selector && parmno > 1)
7867 rname = selector;
7868 parmno -= 1;
7872 if (objc_compare_types (type, rhstype, parmno, rname))
7874 rhs = mark_rvalue_use (rhs);
7875 return convert (type, rhs);
7879 /* [expr.ass]
7881 The expression is implicitly converted (clause _conv_) to the
7882 cv-unqualified type of the left operand.
7884 We allow bad conversions here because by the time we get to this point
7885 we are committed to doing the conversion. If we end up doing a bad
7886 conversion, convert_like will complain. */
7887 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
7889 /* When -Wno-pmf-conversions is use, we just silently allow
7890 conversions from pointers-to-members to plain pointers. If
7891 the conversion doesn't work, cp_convert will complain. */
7892 if (!warn_pmf2ptr
7893 && TYPE_PTR_P (type)
7894 && TYPE_PTRMEMFUNC_P (rhstype))
7895 rhs = cp_convert (strip_top_quals (type), rhs, complain);
7896 else
7898 if (complain & tf_error)
7900 /* If the right-hand side has unknown type, then it is an
7901 overloaded function. Call instantiate_type to get error
7902 messages. */
7903 if (rhstype == unknown_type_node)
7904 instantiate_type (type, rhs, tf_warning_or_error);
7905 else if (fndecl)
7906 error ("cannot convert %qT to %qT for argument %qP to %qD",
7907 rhstype, type, parmnum, fndecl);
7908 else
7909 switch (errtype)
7911 case ICR_DEFAULT_ARGUMENT:
7912 error ("cannot convert %qT to %qT in default argument",
7913 rhstype, type);
7914 break;
7915 case ICR_ARGPASS:
7916 error ("cannot convert %qT to %qT in argument passing",
7917 rhstype, type);
7918 break;
7919 case ICR_CONVERTING:
7920 error ("cannot convert %qT to %qT",
7921 rhstype, type);
7922 break;
7923 case ICR_INIT:
7924 error ("cannot convert %qT to %qT in initialization",
7925 rhstype, type);
7926 break;
7927 case ICR_RETURN:
7928 error ("cannot convert %qT to %qT in return",
7929 rhstype, type);
7930 break;
7931 case ICR_ASSIGN:
7932 error ("cannot convert %qT to %qT in assignment",
7933 rhstype, type);
7934 break;
7935 default:
7936 gcc_unreachable();
7939 return error_mark_node;
7942 if (warn_suggest_attribute_format)
7944 const enum tree_code codel = TREE_CODE (type);
7945 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7946 && coder == codel
7947 && check_missing_format_attribute (type, rhstype)
7948 && (complain & tf_warning))
7949 switch (errtype)
7951 case ICR_ARGPASS:
7952 case ICR_DEFAULT_ARGUMENT:
7953 if (fndecl)
7954 warning (OPT_Wsuggest_attribute_format,
7955 "parameter %qP of %qD might be a candidate "
7956 "for a format attribute", parmnum, fndecl);
7957 else
7958 warning (OPT_Wsuggest_attribute_format,
7959 "parameter might be a candidate "
7960 "for a format attribute");
7961 break;
7962 case ICR_CONVERTING:
7963 warning (OPT_Wsuggest_attribute_format,
7964 "target of conversion might be a candidate "
7965 "for a format attribute");
7966 break;
7967 case ICR_INIT:
7968 warning (OPT_Wsuggest_attribute_format,
7969 "target of initialization might be a candidate "
7970 "for a format attribute");
7971 break;
7972 case ICR_RETURN:
7973 warning (OPT_Wsuggest_attribute_format,
7974 "return type might be a candidate "
7975 "for a format attribute");
7976 break;
7977 case ICR_ASSIGN:
7978 warning (OPT_Wsuggest_attribute_format,
7979 "left-hand side of assignment might be a candidate "
7980 "for a format attribute");
7981 break;
7982 default:
7983 gcc_unreachable();
7987 /* If -Wparentheses, warn about a = b = c when a has type bool and b
7988 does not. */
7989 if (warn_parentheses
7990 && TREE_CODE (type) == BOOLEAN_TYPE
7991 && TREE_CODE (rhs) == MODIFY_EXPR
7992 && !TREE_NO_WARNING (rhs)
7993 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7994 && (complain & tf_warning))
7996 location_t loc = EXPR_LOC_OR_HERE (rhs);
7998 warning_at (loc, OPT_Wparentheses,
7999 "suggest parentheses around assignment used as truth value");
8000 TREE_NO_WARNING (rhs) = 1;
8003 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8004 complain, flags);
8007 /* Convert RHS to be of type TYPE.
8008 If EXP is nonzero, it is the target of the initialization.
8009 ERRTYPE indicates what kind of error the implicit conversion is.
8011 Two major differences between the behavior of
8012 `convert_for_assignment' and `convert_for_initialization'
8013 are that references are bashed in the former, while
8014 copied in the latter, and aggregates are assigned in
8015 the former (operator=) while initialized in the
8016 latter (X(X&)).
8018 If using constructor make sure no conversion operator exists, if one does
8019 exist, an ambiguity exists. */
8021 tree
8022 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8023 impl_conv_rhs errtype, tree fndecl, int parmnum,
8024 tsubst_flags_t complain)
8026 enum tree_code codel = TREE_CODE (type);
8027 tree rhstype;
8028 enum tree_code coder;
8030 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8031 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8032 if (TREE_CODE (rhs) == NOP_EXPR
8033 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8034 && codel != REFERENCE_TYPE)
8035 rhs = TREE_OPERAND (rhs, 0);
8037 if (type == error_mark_node
8038 || rhs == error_mark_node
8039 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8040 return error_mark_node;
8042 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8043 && TREE_CODE (type) != ARRAY_TYPE
8044 && (TREE_CODE (type) != REFERENCE_TYPE
8045 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8046 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8047 && !TYPE_REFFN_P (type))
8048 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8049 rhs = decay_conversion (rhs, complain);
8051 rhstype = TREE_TYPE (rhs);
8052 coder = TREE_CODE (rhstype);
8054 if (coder == ERROR_MARK)
8055 return error_mark_node;
8057 /* We accept references to incomplete types, so we can
8058 return here before checking if RHS is of complete type. */
8060 if (codel == REFERENCE_TYPE)
8062 /* This should eventually happen in convert_arguments. */
8063 int savew = 0, savee = 0;
8065 if (fndecl)
8066 savew = warningcount + werrorcount, savee = errorcount;
8067 rhs = initialize_reference (type, rhs, flags, complain);
8069 if (fndecl
8070 && (warningcount + werrorcount > savew || errorcount > savee))
8071 inform (input_location,
8072 "in passing argument %P of %q+D", parmnum, fndecl);
8074 return rhs;
8077 if (exp != 0)
8078 exp = require_complete_type_sfinae (exp, complain);
8079 if (exp == error_mark_node)
8080 return error_mark_node;
8082 rhstype = non_reference (rhstype);
8084 type = complete_type (type);
8086 if (DIRECT_INIT_EXPR_P (type, rhs))
8087 /* Don't try to do copy-initialization if we already have
8088 direct-initialization. */
8089 return rhs;
8091 if (MAYBE_CLASS_TYPE_P (type))
8092 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8094 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8095 complain, flags);
8098 /* If RETVAL is the address of, or a reference to, a local variable or
8099 temporary give an appropriate warning. */
8101 static void
8102 maybe_warn_about_returning_address_of_local (tree retval)
8104 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8105 tree whats_returned = retval;
8107 for (;;)
8109 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8110 whats_returned = TREE_OPERAND (whats_returned, 1);
8111 else if (CONVERT_EXPR_P (whats_returned)
8112 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8113 whats_returned = TREE_OPERAND (whats_returned, 0);
8114 else
8115 break;
8118 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8119 return;
8120 whats_returned = TREE_OPERAND (whats_returned, 0);
8122 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8124 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8125 || TREE_CODE (whats_returned) == TARGET_EXPR)
8127 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8128 return;
8130 if (VAR_P (whats_returned)
8131 && DECL_NAME (whats_returned)
8132 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8134 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8135 return;
8139 while (TREE_CODE (whats_returned) == COMPONENT_REF
8140 || TREE_CODE (whats_returned) == ARRAY_REF)
8141 whats_returned = TREE_OPERAND (whats_returned, 0);
8143 if (DECL_P (whats_returned)
8144 && DECL_NAME (whats_returned)
8145 && DECL_FUNCTION_SCOPE_P (whats_returned)
8146 && !is_capture_proxy (whats_returned)
8147 && !(TREE_STATIC (whats_returned)
8148 || TREE_PUBLIC (whats_returned)))
8150 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8151 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8152 whats_returned);
8153 else
8154 warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
8155 whats_returned);
8156 return;
8160 /* Check that returning RETVAL from the current function is valid.
8161 Return an expression explicitly showing all conversions required to
8162 change RETVAL into the function return type, and to assign it to
8163 the DECL_RESULT for the function. Set *NO_WARNING to true if
8164 code reaches end of non-void function warning shouldn't be issued
8165 on this RETURN_EXPR. */
8167 tree
8168 check_return_expr (tree retval, bool *no_warning)
8170 tree result;
8171 /* The type actually returned by the function. */
8172 tree valtype;
8173 /* The type the function is declared to return, or void if
8174 the declared type is incomplete. */
8175 tree functype;
8176 int fn_returns_value_p;
8177 bool named_return_value_okay_p;
8179 *no_warning = false;
8181 /* A `volatile' function is one that isn't supposed to return, ever.
8182 (This is a G++ extension, used to get better code for functions
8183 that call the `volatile' function.) */
8184 if (TREE_THIS_VOLATILE (current_function_decl))
8185 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8187 /* Check for various simple errors. */
8188 if (DECL_DESTRUCTOR_P (current_function_decl))
8190 if (retval)
8191 error ("returning a value from a destructor");
8192 return NULL_TREE;
8194 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8196 if (in_function_try_handler)
8197 /* If a return statement appears in a handler of the
8198 function-try-block of a constructor, the program is ill-formed. */
8199 error ("cannot return from a handler of a function-try-block of a constructor");
8200 else if (retval)
8201 /* You can't return a value from a constructor. */
8202 error ("returning a value from a constructor");
8203 return NULL_TREE;
8206 if (processing_template_decl)
8208 current_function_returns_value = 1;
8209 if (check_for_bare_parameter_packs (retval))
8210 retval = error_mark_node;
8211 return retval;
8214 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8216 /* Deduce auto return type from a return statement. */
8217 if (current_function_auto_return_pattern)
8219 tree auto_node;
8220 tree type;
8222 if (!retval && !is_auto (current_function_auto_return_pattern))
8224 /* Give a helpful error message. */
8225 error ("return-statement with no value, in function returning %qT",
8226 current_function_auto_return_pattern);
8227 inform (input_location, "only plain %<auto%> return type can be "
8228 "deduced to %<void%>");
8229 type = error_mark_node;
8231 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8233 error ("returning initializer list");
8234 type = error_mark_node;
8236 else
8238 if (!retval)
8239 retval = void_zero_node;
8240 auto_node = type_uses_auto (current_function_auto_return_pattern);
8241 type = do_auto_deduction (current_function_auto_return_pattern,
8242 retval, auto_node);
8245 if (type == error_mark_node)
8246 /* Leave it. */;
8247 else if (functype == current_function_auto_return_pattern)
8248 apply_deduced_return_type (current_function_decl, type);
8249 else
8250 /* A mismatch should have been diagnosed in do_auto_deduction. */
8251 gcc_assert (same_type_p (type, functype));
8252 functype = type;
8255 /* When no explicit return-value is given in a function with a named
8256 return value, the named return value is used. */
8257 result = DECL_RESULT (current_function_decl);
8258 valtype = TREE_TYPE (result);
8259 gcc_assert (valtype != NULL_TREE);
8260 fn_returns_value_p = !VOID_TYPE_P (valtype);
8261 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8262 retval = result;
8264 /* Check for a return statement with no return value in a function
8265 that's supposed to return a value. */
8266 if (!retval && fn_returns_value_p)
8268 if (functype != error_mark_node)
8269 permerror (input_location, "return-statement with no value, in "
8270 "function returning %qT", valtype);
8271 /* Remember that this function did return. */
8272 current_function_returns_value = 1;
8273 /* And signal caller that TREE_NO_WARNING should be set on the
8274 RETURN_EXPR to avoid control reaches end of non-void function
8275 warnings in tree-cfg.c. */
8276 *no_warning = true;
8278 /* Check for a return statement with a value in a function that
8279 isn't supposed to return a value. */
8280 else if (retval && !fn_returns_value_p)
8282 if (VOID_TYPE_P (TREE_TYPE (retval)))
8283 /* You can return a `void' value from a function of `void'
8284 type. In that case, we have to evaluate the expression for
8285 its side-effects. */
8286 finish_expr_stmt (retval);
8287 else
8288 permerror (input_location, "return-statement with a value, in function "
8289 "returning 'void'");
8290 current_function_returns_null = 1;
8292 /* There's really no value to return, after all. */
8293 return NULL_TREE;
8295 else if (!retval)
8296 /* Remember that this function can sometimes return without a
8297 value. */
8298 current_function_returns_null = 1;
8299 else
8300 /* Remember that this function did return a value. */
8301 current_function_returns_value = 1;
8303 /* Check for erroneous operands -- but after giving ourselves a
8304 chance to provide an error about returning a value from a void
8305 function. */
8306 if (error_operand_p (retval))
8308 current_function_return_value = error_mark_node;
8309 return error_mark_node;
8312 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8313 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8314 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8315 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8316 && ! flag_check_new
8317 && retval && null_ptr_cst_p (retval))
8318 warning (0, "%<operator new%> must not return NULL unless it is "
8319 "declared %<throw()%> (or -fcheck-new is in effect)");
8321 /* Effective C++ rule 15. See also start_function. */
8322 if (warn_ecpp
8323 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8325 bool warn = true;
8327 /* The function return type must be a reference to the current
8328 class. */
8329 if (TREE_CODE (valtype) == REFERENCE_TYPE
8330 && same_type_ignoring_top_level_qualifiers_p
8331 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8333 /* Returning '*this' is obviously OK. */
8334 if (retval == current_class_ref)
8335 warn = false;
8336 /* If we are calling a function whose return type is the same of
8337 the current class reference, it is ok. */
8338 else if (INDIRECT_REF_P (retval)
8339 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8340 warn = false;
8343 if (warn)
8344 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8347 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8349 [...] For a function with a class return type, if the expression
8350 in the return statement is the name of a local object, and the cv-
8351 unqualified type of the local object is the same as the function
8352 return type, an implementation is permitted to omit creating the tem-
8353 porary object to hold the function return value [...]
8355 So, if this is a value-returning function that always returns the same
8356 local variable, remember it.
8358 It might be nice to be more flexible, and choose the first suitable
8359 variable even if the function sometimes returns something else, but
8360 then we run the risk of clobbering the variable we chose if the other
8361 returned expression uses the chosen variable somehow. And people expect
8362 this restriction, anyway. (jason 2000-11-19)
8364 See finish_function and finalize_nrv for the rest of this optimization. */
8366 named_return_value_okay_p =
8367 (retval != NULL_TREE
8368 /* Must be a local, automatic variable. */
8369 && VAR_P (retval)
8370 && DECL_CONTEXT (retval) == current_function_decl
8371 && ! TREE_STATIC (retval)
8372 && ! DECL_ANON_UNION_VAR_P (retval)
8373 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8374 /* The cv-unqualified type of the returned value must be the
8375 same as the cv-unqualified return type of the
8376 function. */
8377 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8378 (TYPE_MAIN_VARIANT (functype)))
8379 /* And the returned value must be non-volatile. */
8380 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8382 if (fn_returns_value_p && flag_elide_constructors)
8384 if (named_return_value_okay_p
8385 && (current_function_return_value == NULL_TREE
8386 || current_function_return_value == retval))
8387 current_function_return_value = retval;
8388 else
8389 current_function_return_value = error_mark_node;
8392 /* We don't need to do any conversions when there's nothing being
8393 returned. */
8394 if (!retval)
8395 return NULL_TREE;
8397 /* Do any required conversions. */
8398 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8399 /* No conversions are required. */
8401 else
8403 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8405 /* The functype's return type will have been set to void, if it
8406 was an incomplete type. Just treat this as 'return;' */
8407 if (VOID_TYPE_P (functype))
8408 return error_mark_node;
8410 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8411 treated as an rvalue for the purposes of overload resolution to
8412 favor move constructors over copy constructors.
8414 Note that these conditions are similar to, but not as strict as,
8415 the conditions for the named return value optimization. */
8416 if ((cxx_dialect != cxx98)
8417 && (VAR_P (retval)
8418 || TREE_CODE (retval) == PARM_DECL)
8419 && DECL_CONTEXT (retval) == current_function_decl
8420 && !TREE_STATIC (retval)
8421 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8422 (TYPE_MAIN_VARIANT (functype)))
8423 /* This is only interesting for class type. */
8424 && CLASS_TYPE_P (functype))
8425 flags = flags | LOOKUP_PREFER_RVALUE;
8427 /* First convert the value to the function's return type, then
8428 to the type of return value's location to handle the
8429 case that functype is smaller than the valtype. */
8430 retval = convert_for_initialization
8431 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8432 tf_warning_or_error);
8433 retval = convert (valtype, retval);
8435 /* If the conversion failed, treat this just like `return;'. */
8436 if (retval == error_mark_node)
8437 return retval;
8438 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8439 else if (! cfun->returns_struct
8440 && TREE_CODE (retval) == TARGET_EXPR
8441 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8442 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8443 TREE_OPERAND (retval, 0));
8444 else
8445 maybe_warn_about_returning_address_of_local (retval);
8448 /* Actually copy the value returned into the appropriate location. */
8449 if (retval && retval != result)
8450 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8452 return retval;
8456 /* Returns nonzero if the pointer-type FROM can be converted to the
8457 pointer-type TO via a qualification conversion. If CONSTP is -1,
8458 then we return nonzero if the pointers are similar, and the
8459 cv-qualification signature of FROM is a proper subset of that of TO.
8461 If CONSTP is positive, then all outer pointers have been
8462 const-qualified. */
8464 static int
8465 comp_ptr_ttypes_real (tree to, tree from, int constp)
8467 bool to_more_cv_qualified = false;
8468 bool is_opaque_pointer = false;
8470 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8472 if (TREE_CODE (to) != TREE_CODE (from))
8473 return 0;
8475 if (TREE_CODE (from) == OFFSET_TYPE
8476 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8477 TYPE_OFFSET_BASETYPE (to)))
8478 return 0;
8480 /* Const and volatile mean something different for function types,
8481 so the usual checks are not appropriate. */
8482 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8484 if (!at_least_as_qualified_p (to, from))
8485 return 0;
8487 if (!at_least_as_qualified_p (from, to))
8489 if (constp == 0)
8490 return 0;
8491 to_more_cv_qualified = true;
8494 if (constp > 0)
8495 constp &= TYPE_READONLY (to);
8498 if (TREE_CODE (to) == VECTOR_TYPE)
8499 is_opaque_pointer = vector_targets_convertible_p (to, from);
8501 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8502 return ((constp >= 0 || to_more_cv_qualified)
8503 && (is_opaque_pointer
8504 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8508 /* When comparing, say, char ** to char const **, this function takes
8509 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8510 types to this function. */
8513 comp_ptr_ttypes (tree to, tree from)
8515 return comp_ptr_ttypes_real (to, from, 1);
8518 /* Returns true iff FNTYPE is a non-class type that involves
8519 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8520 if a parameter type is ill-formed. */
8522 bool
8523 error_type_p (const_tree type)
8525 tree t;
8527 switch (TREE_CODE (type))
8529 case ERROR_MARK:
8530 return true;
8532 case POINTER_TYPE:
8533 case REFERENCE_TYPE:
8534 case OFFSET_TYPE:
8535 return error_type_p (TREE_TYPE (type));
8537 case FUNCTION_TYPE:
8538 case METHOD_TYPE:
8539 if (error_type_p (TREE_TYPE (type)))
8540 return true;
8541 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8542 if (error_type_p (TREE_VALUE (t)))
8543 return true;
8544 return false;
8546 case RECORD_TYPE:
8547 if (TYPE_PTRMEMFUNC_P (type))
8548 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8549 return false;
8551 default:
8552 return false;
8556 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8557 type or inheritance-related types, regardless of cv-quals. */
8560 ptr_reasonably_similar (const_tree to, const_tree from)
8562 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8564 /* Any target type is similar enough to void. */
8565 if (VOID_TYPE_P (to))
8566 return !error_type_p (from);
8567 if (VOID_TYPE_P (from))
8568 return !error_type_p (to);
8570 if (TREE_CODE (to) != TREE_CODE (from))
8571 return 0;
8573 if (TREE_CODE (from) == OFFSET_TYPE
8574 && comptypes (TYPE_OFFSET_BASETYPE (to),
8575 TYPE_OFFSET_BASETYPE (from),
8576 COMPARE_BASE | COMPARE_DERIVED))
8577 continue;
8579 if (TREE_CODE (to) == VECTOR_TYPE
8580 && vector_types_convertible_p (to, from, false))
8581 return 1;
8583 if (TREE_CODE (to) == INTEGER_TYPE
8584 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8585 return 1;
8587 if (TREE_CODE (to) == FUNCTION_TYPE)
8588 return !error_type_p (to) && !error_type_p (from);
8590 if (!TYPE_PTR_P (to))
8591 return comptypes
8592 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8593 COMPARE_BASE | COMPARE_DERIVED);
8597 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8598 pointer-to-member types) are the same, ignoring cv-qualification at
8599 all levels. */
8601 bool
8602 comp_ptr_ttypes_const (tree to, tree from)
8604 bool is_opaque_pointer = false;
8606 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8608 if (TREE_CODE (to) != TREE_CODE (from))
8609 return false;
8611 if (TREE_CODE (from) == OFFSET_TYPE
8612 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8613 TYPE_OFFSET_BASETYPE (to)))
8614 continue;
8616 if (TREE_CODE (to) == VECTOR_TYPE)
8617 is_opaque_pointer = vector_targets_convertible_p (to, from);
8619 if (!TYPE_PTR_P (to))
8620 return (is_opaque_pointer
8621 || same_type_ignoring_top_level_qualifiers_p (to, from));
8625 /* Returns the type qualifiers for this type, including the qualifiers on the
8626 elements for an array type. */
8629 cp_type_quals (const_tree type)
8631 int quals;
8632 /* This CONST_CAST is okay because strip_array_types returns its
8633 argument unmodified and we assign it to a const_tree. */
8634 type = strip_array_types (CONST_CAST_TREE (type));
8635 if (type == error_mark_node
8636 /* Quals on a FUNCTION_TYPE are memfn quals. */
8637 || TREE_CODE (type) == FUNCTION_TYPE)
8638 return TYPE_UNQUALIFIED;
8639 quals = TYPE_QUALS (type);
8640 /* METHOD and REFERENCE_TYPEs should never have quals. */
8641 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8642 && TREE_CODE (type) != REFERENCE_TYPE)
8643 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8644 == TYPE_UNQUALIFIED));
8645 return quals;
8648 /* Returns the function-ref-qualifier for TYPE */
8650 cp_ref_qualifier
8651 type_memfn_rqual (const_tree type)
8653 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8654 || TREE_CODE (type) == METHOD_TYPE);
8656 if (!FUNCTION_REF_QUALIFIED (type))
8657 return REF_QUAL_NONE;
8658 else if (FUNCTION_RVALUE_QUALIFIED (type))
8659 return REF_QUAL_RVALUE;
8660 else
8661 return REF_QUAL_LVALUE;
8664 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8665 METHOD_TYPE. */
8668 type_memfn_quals (const_tree type)
8670 if (TREE_CODE (type) == FUNCTION_TYPE)
8671 return TYPE_QUALS (type);
8672 else if (TREE_CODE (type) == METHOD_TYPE)
8673 return cp_type_quals (class_of_this_parm (type));
8674 else
8675 gcc_unreachable ();
8678 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8679 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8681 tree
8682 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8684 /* Could handle METHOD_TYPE here if necessary. */
8685 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8686 if (TYPE_QUALS (type) == memfn_quals
8687 && type_memfn_rqual (type) == rqual)
8688 return type;
8690 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8691 complex. */
8692 tree result = build_qualified_type (type, memfn_quals);
8693 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8694 return build_ref_qualified_type (result, rqual);
8697 /* Returns nonzero if TYPE is const or volatile. */
8699 bool
8700 cv_qualified_p (const_tree type)
8702 int quals = cp_type_quals (type);
8703 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8706 /* Returns nonzero if the TYPE contains a mutable member. */
8708 bool
8709 cp_has_mutable_p (const_tree type)
8711 /* This CONST_CAST is okay because strip_array_types returns its
8712 argument unmodified and we assign it to a const_tree. */
8713 type = strip_array_types (CONST_CAST_TREE(type));
8715 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8718 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8719 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8720 approximation. In particular, consider:
8722 int f();
8723 struct S { int i; };
8724 const S s = { f(); }
8726 Here, we will make "s" as TREE_READONLY (because it is declared
8727 "const") -- only to reverse ourselves upon seeing that the
8728 initializer is non-constant. */
8730 void
8731 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8733 tree type = TREE_TYPE (decl);
8735 if (type == error_mark_node)
8736 return;
8738 if (TREE_CODE (decl) == TYPE_DECL)
8739 return;
8741 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8742 && type_quals != TYPE_UNQUALIFIED));
8744 /* Avoid setting TREE_READONLY incorrectly. */
8745 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8746 constructor can produce constant init, so rely on cp_finish_decl to
8747 clear TREE_READONLY if the variable has non-constant init. */
8749 /* If the type has (or might have) a mutable component, that component
8750 might be modified. */
8751 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8752 type_quals &= ~TYPE_QUAL_CONST;
8754 c_apply_type_quals_to_decl (type_quals, decl);
8757 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8758 exemplar types such that casting T1 to T2 is casting away constness
8759 if and only if there is no implicit conversion from T1 to T2. */
8761 static void
8762 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8764 int quals1;
8765 int quals2;
8767 /* [expr.const.cast]
8769 For multi-level pointer to members and multi-level mixed pointers
8770 and pointers to members (conv.qual), the "member" aspect of a
8771 pointer to member level is ignored when determining if a const
8772 cv-qualifier has been cast away. */
8773 /* [expr.const.cast]
8775 For two pointer types:
8777 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8778 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8779 K is min(N,M)
8781 casting from X1 to X2 casts away constness if, for a non-pointer
8782 type T there does not exist an implicit conversion (clause
8783 _conv_) from:
8785 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8789 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8790 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8791 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8793 *t1 = cp_build_qualified_type (void_type_node,
8794 cp_type_quals (*t1));
8795 *t2 = cp_build_qualified_type (void_type_node,
8796 cp_type_quals (*t2));
8797 return;
8800 quals1 = cp_type_quals (*t1);
8801 quals2 = cp_type_quals (*t2);
8803 if (TYPE_PTRDATAMEM_P (*t1))
8804 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8805 else
8806 *t1 = TREE_TYPE (*t1);
8807 if (TYPE_PTRDATAMEM_P (*t2))
8808 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8809 else
8810 *t2 = TREE_TYPE (*t2);
8812 casts_away_constness_r (t1, t2, complain);
8813 *t1 = build_pointer_type (*t1);
8814 *t2 = build_pointer_type (*t2);
8815 *t1 = cp_build_qualified_type (*t1, quals1);
8816 *t2 = cp_build_qualified_type (*t2, quals2);
8819 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8820 constness.
8822 ??? This function returns non-zero if casting away qualifiers not
8823 just const. We would like to return to the caller exactly which
8824 qualifiers are casted away to give more accurate diagnostics.
8827 static bool
8828 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8830 if (TREE_CODE (t2) == REFERENCE_TYPE)
8832 /* [expr.const.cast]
8834 Casting from an lvalue of type T1 to an lvalue of type T2
8835 using a reference cast casts away constness if a cast from an
8836 rvalue of type "pointer to T1" to the type "pointer to T2"
8837 casts away constness. */
8838 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8839 return casts_away_constness (build_pointer_type (t1),
8840 build_pointer_type (TREE_TYPE (t2)),
8841 complain);
8844 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
8845 /* [expr.const.cast]
8847 Casting from an rvalue of type "pointer to data member of X
8848 of type T1" to the type "pointer to data member of Y of type
8849 T2" casts away constness if a cast from an rvalue of type
8850 "pointer to T1" to the type "pointer to T2" casts away
8851 constness. */
8852 return casts_away_constness
8853 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8854 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
8855 complain);
8857 /* Casting away constness is only something that makes sense for
8858 pointer or reference types. */
8859 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
8860 return false;
8862 /* Top-level qualifiers don't matter. */
8863 t1 = TYPE_MAIN_VARIANT (t1);
8864 t2 = TYPE_MAIN_VARIANT (t2);
8865 casts_away_constness_r (&t1, &t2, complain);
8866 if (!can_convert (t2, t1, complain))
8867 return true;
8869 return false;
8872 /* If T is a REFERENCE_TYPE return the type to which T refers.
8873 Otherwise, return T itself. */
8875 tree
8876 non_reference (tree t)
8878 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8879 t = TREE_TYPE (t);
8880 return t;
8884 /* Return nonzero if REF is an lvalue valid for this language;
8885 otherwise, print an error message and return zero. USE says
8886 how the lvalue is being used and so selects the error message. */
8889 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8891 cp_lvalue_kind kind = lvalue_kind (ref);
8893 if (kind == clk_none)
8895 if (complain & tf_error)
8896 lvalue_error (input_location, use);
8897 return 0;
8899 else if (kind & (clk_rvalueref|clk_class))
8901 if (!(complain & tf_error))
8902 return 0;
8903 if (kind & clk_class)
8904 /* Make this a permerror because we used to accept it. */
8905 permerror (input_location, "using temporary as lvalue");
8906 else
8907 error ("using xvalue (rvalue reference) as lvalue");
8909 return 1;
8912 /* Return true if a user-defined literal operator is a raw operator. */
8914 bool
8915 check_raw_literal_operator (const_tree decl)
8917 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8918 tree argtype;
8919 int arity;
8920 bool maybe_raw_p = false;
8922 /* Count the number and type of arguments and check for ellipsis. */
8923 for (argtype = argtypes, arity = 0;
8924 argtype && argtype != void_list_node;
8925 ++arity, argtype = TREE_CHAIN (argtype))
8927 tree t = TREE_VALUE (argtype);
8929 if (same_type_p (t, const_string_type_node))
8930 maybe_raw_p = true;
8932 if (!argtype)
8933 return false; /* Found ellipsis. */
8935 if (!maybe_raw_p || arity != 1)
8936 return false;
8938 return true;
8942 /* Return true if a user-defined literal operator has one of the allowed
8943 argument types. */
8945 bool
8946 check_literal_operator_args (const_tree decl,
8947 bool *long_long_unsigned_p, bool *long_double_p)
8949 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8951 *long_long_unsigned_p = false;
8952 *long_double_p = false;
8953 if (processing_template_decl || processing_specialization)
8954 return argtypes == void_list_node;
8955 else
8957 tree argtype;
8958 int arity;
8959 int max_arity = 2;
8961 /* Count the number and type of arguments and check for ellipsis. */
8962 for (argtype = argtypes, arity = 0;
8963 argtype && argtype != void_list_node;
8964 argtype = TREE_CHAIN (argtype))
8966 tree t = TREE_VALUE (argtype);
8967 ++arity;
8969 if (TYPE_PTR_P (t))
8971 bool maybe_raw_p = false;
8972 t = TREE_TYPE (t);
8973 if (cp_type_quals (t) != TYPE_QUAL_CONST)
8974 return false;
8975 t = TYPE_MAIN_VARIANT (t);
8976 if ((maybe_raw_p = same_type_p (t, char_type_node))
8977 || same_type_p (t, wchar_type_node)
8978 || same_type_p (t, char16_type_node)
8979 || same_type_p (t, char32_type_node))
8981 argtype = TREE_CHAIN (argtype);
8982 if (!argtype)
8983 return false;
8984 t = TREE_VALUE (argtype);
8985 if (maybe_raw_p && argtype == void_list_node)
8986 return true;
8987 else if (same_type_p (t, size_type_node))
8989 ++arity;
8990 continue;
8992 else
8993 return false;
8996 else if (same_type_p (t, long_long_unsigned_type_node))
8998 max_arity = 1;
8999 *long_long_unsigned_p = true;
9001 else if (same_type_p (t, long_double_type_node))
9003 max_arity = 1;
9004 *long_double_p = true;
9006 else if (same_type_p (t, char_type_node))
9007 max_arity = 1;
9008 else if (same_type_p (t, wchar_type_node))
9009 max_arity = 1;
9010 else if (same_type_p (t, char16_type_node))
9011 max_arity = 1;
9012 else if (same_type_p (t, char32_type_node))
9013 max_arity = 1;
9014 else
9015 return false;
9017 if (!argtype)
9018 return false; /* Found ellipsis. */
9020 if (arity != max_arity)
9021 return false;
9023 return true;