c-common.c (c_common_nodes_and_builtins): Use cxx11 in lieu of cxx0x.
[official-gcc.git] / gcc / cp / typeck.c
blob5b321ce5a7f9df7001d4e29608d9e41f22b828cd
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 an array's index is an array notation, then its rank cannot be
3009 greater than one. */
3010 if (flag_enable_cilkplus && contains_array_notation_expr (idx))
3012 size_t rank = 0;
3014 /* If find_rank returns false, then it should have reported an error,
3015 thus it is unnecessary for repetition. */
3016 if (!find_rank (loc, idx, idx, true, &rank))
3017 return error_mark_node;
3018 if (rank > 1)
3020 error_at (loc, "rank of the array%'s index is greater than 1");
3021 return error_mark_node;
3024 if (TREE_TYPE (array) == error_mark_node
3025 || TREE_TYPE (idx) == error_mark_node)
3026 return error_mark_node;
3028 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3029 inside it. */
3030 switch (TREE_CODE (array))
3032 case COMPOUND_EXPR:
3034 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3035 complain);
3036 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3037 TREE_OPERAND (array, 0), value);
3038 SET_EXPR_LOCATION (ret, loc);
3039 return ret;
3042 case COND_EXPR:
3043 ret = build_conditional_expr
3044 (loc, TREE_OPERAND (array, 0),
3045 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3046 complain),
3047 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3048 complain),
3049 complain);
3050 protected_set_expr_location (ret, loc);
3051 return ret;
3053 default:
3054 break;
3057 convert_vector_to_pointer_for_subscript (loc, &array, idx);
3059 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3061 tree rval, type;
3063 warn_array_subscript_with_type_char (idx);
3065 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3067 if (complain & tf_error)
3068 error_at (loc, "array subscript is not an integer");
3069 return error_mark_node;
3072 /* Apply integral promotions *after* noticing character types.
3073 (It is unclear why we do these promotions -- the standard
3074 does not say that we should. In fact, the natural thing would
3075 seem to be to convert IDX to ptrdiff_t; we're performing
3076 pointer arithmetic.) */
3077 idx = cp_perform_integral_promotions (idx, complain);
3079 /* An array that is indexed by a non-constant
3080 cannot be stored in a register; we must be able to do
3081 address arithmetic on its address.
3082 Likewise an array of elements of variable size. */
3083 if (TREE_CODE (idx) != INTEGER_CST
3084 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3085 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3086 != INTEGER_CST)))
3088 if (!cxx_mark_addressable (array))
3089 return error_mark_node;
3092 /* An array that is indexed by a constant value which is not within
3093 the array bounds cannot be stored in a register either; because we
3094 would get a crash in store_bit_field/extract_bit_field when trying
3095 to access a non-existent part of the register. */
3096 if (TREE_CODE (idx) == INTEGER_CST
3097 && TYPE_DOMAIN (TREE_TYPE (array))
3098 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3100 if (!cxx_mark_addressable (array))
3101 return error_mark_node;
3104 if (!lvalue_p (array) && (complain & tf_error))
3105 pedwarn (loc, OPT_Wpedantic,
3106 "ISO C++ forbids subscripting non-lvalue array");
3108 /* Note in C++ it is valid to subscript a `register' array, since
3109 it is valid to take the address of something with that
3110 storage specification. */
3111 if (extra_warnings)
3113 tree foo = array;
3114 while (TREE_CODE (foo) == COMPONENT_REF)
3115 foo = TREE_OPERAND (foo, 0);
3116 if (VAR_P (foo) && DECL_REGISTER (foo)
3117 && (complain & tf_warning))
3118 warning_at (loc, OPT_Wextra,
3119 "subscripting array declared %<register%>");
3122 type = TREE_TYPE (TREE_TYPE (array));
3123 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3124 /* Array ref is const/volatile if the array elements are
3125 or if the array is.. */
3126 TREE_READONLY (rval)
3127 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3128 TREE_SIDE_EFFECTS (rval)
3129 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3130 TREE_THIS_VOLATILE (rval)
3131 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3132 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3133 complain);
3134 protected_set_expr_location (ret, loc);
3135 return ret;
3139 tree ar = cp_default_conversion (array, complain);
3140 tree ind = cp_default_conversion (idx, complain);
3142 /* Put the integer in IND to simplify error checking. */
3143 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3145 tree temp = ar;
3146 ar = ind;
3147 ind = temp;
3150 if (ar == error_mark_node || ind == error_mark_node)
3151 return error_mark_node;
3153 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3155 if (complain & tf_error)
3156 error_at (loc, "subscripted value is neither array nor pointer");
3157 return error_mark_node;
3159 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3161 if (complain & tf_error)
3162 error_at (loc, "array subscript is not an integer");
3163 return error_mark_node;
3166 warn_array_subscript_with_type_char (idx);
3168 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3169 PLUS_EXPR, ar, ind,
3170 complain),
3171 RO_ARRAY_INDEXING,
3172 complain);
3173 protected_set_expr_location (ret, loc);
3174 return ret;
3178 /* Entry point for Obj-C++. */
3180 tree
3181 build_array_ref (location_t loc, tree array, tree idx)
3183 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3186 /* Resolve a pointer to member function. INSTANCE is the object
3187 instance to use, if the member points to a virtual member.
3189 This used to avoid checking for virtual functions if basetype
3190 has no virtual functions, according to an earlier ANSI draft.
3191 With the final ISO C++ rules, such an optimization is
3192 incorrect: A pointer to a derived member can be static_cast
3193 to pointer-to-base-member, as long as the dynamic object
3194 later has the right member. So now we only do this optimization
3195 when we know the dynamic type of the object. */
3197 tree
3198 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3199 tsubst_flags_t complain)
3201 if (TREE_CODE (function) == OFFSET_REF)
3202 function = TREE_OPERAND (function, 1);
3204 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3206 tree idx, delta, e1, e2, e3, vtbl;
3207 bool nonvirtual;
3208 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3209 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3211 tree instance_ptr = *instance_ptrptr;
3212 tree instance_save_expr = 0;
3213 if (instance_ptr == error_mark_node)
3215 if (TREE_CODE (function) == PTRMEM_CST)
3217 /* Extracting the function address from a pmf is only
3218 allowed with -Wno-pmf-conversions. It only works for
3219 pmf constants. */
3220 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3221 e1 = convert (fntype, e1);
3222 return e1;
3224 else
3226 if (complain & tf_error)
3227 error ("object missing in use of %qE", function);
3228 return error_mark_node;
3232 /* True if we know that the dynamic type of the object doesn't have
3233 virtual functions, so we can assume the PFN field is a pointer. */
3234 nonvirtual = (COMPLETE_TYPE_P (basetype)
3235 && !TYPE_POLYMORPHIC_P (basetype)
3236 && resolves_to_fixed_type_p (instance_ptr, 0));
3238 if (TREE_SIDE_EFFECTS (instance_ptr))
3239 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3241 if (TREE_SIDE_EFFECTS (function))
3242 function = save_expr (function);
3244 /* Start by extracting all the information from the PMF itself. */
3245 e3 = pfn_from_ptrmemfunc (function);
3246 delta = delta_from_ptrmemfunc (function);
3247 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3248 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3250 case ptrmemfunc_vbit_in_pfn:
3251 e1 = cp_build_binary_op (input_location,
3252 BIT_AND_EXPR, idx, integer_one_node,
3253 complain);
3254 idx = cp_build_binary_op (input_location,
3255 MINUS_EXPR, idx, integer_one_node,
3256 complain);
3257 if (idx == error_mark_node)
3258 return error_mark_node;
3259 break;
3261 case ptrmemfunc_vbit_in_delta:
3262 e1 = cp_build_binary_op (input_location,
3263 BIT_AND_EXPR, delta, integer_one_node,
3264 complain);
3265 delta = cp_build_binary_op (input_location,
3266 RSHIFT_EXPR, delta, integer_one_node,
3267 complain);
3268 if (delta == error_mark_node)
3269 return error_mark_node;
3270 break;
3272 default:
3273 gcc_unreachable ();
3276 if (e1 == error_mark_node)
3277 return error_mark_node;
3279 /* Convert down to the right base before using the instance. A
3280 special case is that in a pointer to member of class C, C may
3281 be incomplete. In that case, the function will of course be
3282 a member of C, and no conversion is required. In fact,
3283 lookup_base will fail in that case, because incomplete
3284 classes do not have BINFOs. */
3285 if (!same_type_ignoring_top_level_qualifiers_p
3286 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3288 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3289 basetype, ba_check, NULL, complain);
3290 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3291 1, complain);
3292 if (instance_ptr == error_mark_node)
3293 return error_mark_node;
3295 /* ...and then the delta in the PMF. */
3296 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3298 /* Hand back the adjusted 'this' argument to our caller. */
3299 *instance_ptrptr = instance_ptr;
3301 if (nonvirtual)
3302 /* Now just return the pointer. */
3303 return e3;
3305 /* Next extract the vtable pointer from the object. */
3306 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3307 instance_ptr);
3308 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3309 if (vtbl == error_mark_node)
3310 return error_mark_node;
3312 /* Finally, extract the function pointer from the vtable. */
3313 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3314 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3315 if (e2 == error_mark_node)
3316 return error_mark_node;
3317 TREE_CONSTANT (e2) = 1;
3319 /* When using function descriptors, the address of the
3320 vtable entry is treated as a function pointer. */
3321 if (TARGET_VTABLE_USES_DESCRIPTORS)
3322 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3323 cp_build_addr_expr (e2, complain));
3325 e2 = fold_convert (TREE_TYPE (e3), e2);
3326 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3327 if (e1 == error_mark_node)
3328 return error_mark_node;
3330 /* Make sure this doesn't get evaluated first inside one of the
3331 branches of the COND_EXPR. */
3332 if (instance_save_expr)
3333 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3334 instance_save_expr, e1);
3336 function = e1;
3338 return function;
3341 /* Used by the C-common bits. */
3342 tree
3343 build_function_call (location_t /*loc*/,
3344 tree function, tree params)
3346 return cp_build_function_call (function, params, tf_warning_or_error);
3349 /* Used by the C-common bits. */
3350 tree
3351 build_function_call_vec (location_t /*loc*/,
3352 tree function, vec<tree, va_gc> *params,
3353 vec<tree, va_gc> * /*origtypes*/)
3355 vec<tree, va_gc> *orig_params = params;
3356 tree ret = cp_build_function_call_vec (function, &params,
3357 tf_warning_or_error);
3359 /* cp_build_function_call_vec can reallocate PARAMS by adding
3360 default arguments. That should never happen here. Verify
3361 that. */
3362 gcc_assert (params == orig_params);
3364 return ret;
3367 /* Build a function call using a tree list of arguments. */
3369 tree
3370 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3372 vec<tree, va_gc> *vec;
3373 tree ret;
3375 vec = make_tree_vector ();
3376 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3377 vec_safe_push (vec, TREE_VALUE (params));
3378 ret = cp_build_function_call_vec (function, &vec, complain);
3379 release_tree_vector (vec);
3380 return ret;
3383 /* Build a function call using varargs. */
3385 tree
3386 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3388 vec<tree, va_gc> *vec;
3389 va_list args;
3390 tree ret, t;
3392 vec = make_tree_vector ();
3393 va_start (args, complain);
3394 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3395 vec_safe_push (vec, t);
3396 va_end (args);
3397 ret = cp_build_function_call_vec (function, &vec, complain);
3398 release_tree_vector (vec);
3399 return ret;
3402 /* Build a function call using a vector of arguments. PARAMS may be
3403 NULL if there are no parameters. This changes the contents of
3404 PARAMS. */
3406 tree
3407 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3408 tsubst_flags_t complain)
3410 tree fntype, fndecl;
3411 int is_method;
3412 tree original = function;
3413 int nargs;
3414 tree *argarray;
3415 tree parm_types;
3416 vec<tree, va_gc> *allocated = NULL;
3417 tree ret;
3419 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3420 expressions, like those used for ObjC messenger dispatches. */
3421 if (params != NULL && !vec_safe_is_empty (*params))
3422 function = objc_rewrite_function_call (function, (**params)[0]);
3424 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3425 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3426 if (TREE_CODE (function) == NOP_EXPR
3427 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3428 function = TREE_OPERAND (function, 0);
3430 if (TREE_CODE (function) == FUNCTION_DECL)
3432 mark_used (function);
3433 fndecl = function;
3435 /* Convert anything with function type to a pointer-to-function. */
3436 if (DECL_MAIN_P (function) && (complain & tf_error))
3437 pedwarn (input_location, OPT_Wpedantic,
3438 "ISO C++ forbids calling %<::main%> from within program");
3440 function = build_addr_func (function, complain);
3442 else
3444 fndecl = NULL_TREE;
3446 function = build_addr_func (function, complain);
3449 if (function == error_mark_node)
3450 return error_mark_node;
3452 fntype = TREE_TYPE (function);
3454 if (TYPE_PTRMEMFUNC_P (fntype))
3456 if (complain & tf_error)
3457 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3458 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3459 original, original);
3460 return error_mark_node;
3463 is_method = (TYPE_PTR_P (fntype)
3464 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3466 if (!(TYPE_PTRFN_P (fntype)
3467 || is_method
3468 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3470 if (complain & tf_error)
3472 if (!flag_diagnostics_show_caret)
3473 error_at (input_location,
3474 "%qE cannot be used as a function", original);
3475 else if (DECL_P (original))
3476 error_at (input_location,
3477 "%qD cannot be used as a function", original);
3478 else
3479 error_at (input_location,
3480 "expression cannot be used as a function");
3483 return error_mark_node;
3486 /* fntype now gets the type of function pointed to. */
3487 fntype = TREE_TYPE (fntype);
3488 parm_types = TYPE_ARG_TYPES (fntype);
3490 if (params == NULL)
3492 allocated = make_tree_vector ();
3493 params = &allocated;
3496 if (flag_enable_cilkplus
3497 && is_cilkplus_reduce_builtin (fndecl) != BUILT_IN_NONE)
3498 nargs = (*params)->length ();
3499 else
3500 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3501 complain);
3502 if (nargs < 0)
3503 return error_mark_node;
3505 argarray = (*params)->address ();
3507 /* Check for errors in format strings and inappropriately
3508 null parameters. */
3509 check_function_arguments (fntype, nargs, argarray);
3511 ret = build_cxx_call (function, nargs, argarray, complain);
3513 if (allocated != NULL)
3514 release_tree_vector (allocated);
3516 return ret;
3519 /* Subroutine of convert_arguments.
3520 Warn about wrong number of args are genereted. */
3522 static void
3523 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3525 if (fndecl)
3527 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3529 if (DECL_NAME (fndecl) == NULL_TREE
3530 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3531 error_at (loc,
3532 too_many_p
3533 ? G_("too many arguments to constructor %q#D")
3534 : G_("too few arguments to constructor %q#D"),
3535 fndecl);
3536 else
3537 error_at (loc,
3538 too_many_p
3539 ? G_("too many arguments to member function %q#D")
3540 : G_("too few arguments to member function %q#D"),
3541 fndecl);
3543 else
3544 error_at (loc,
3545 too_many_p
3546 ? G_("too many arguments to function %q#D")
3547 : G_("too few arguments to function %q#D"),
3548 fndecl);
3549 inform (DECL_SOURCE_LOCATION (fndecl),
3550 "declared here");
3552 else
3554 if (c_dialect_objc () && objc_message_selector ())
3555 error_at (loc,
3556 too_many_p
3557 ? G_("too many arguments to method %q#D")
3558 : G_("too few arguments to method %q#D"),
3559 objc_message_selector ());
3560 else
3561 error_at (loc, too_many_p ? G_("too many arguments to function")
3562 : G_("too few arguments to function"));
3566 /* Convert the actual parameter expressions in the list VALUES to the
3567 types in the list TYPELIST. The converted expressions are stored
3568 back in the VALUES vector.
3569 If parmdecls is exhausted, or when an element has NULL as its type,
3570 perform the default conversions.
3572 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3574 This is also where warnings about wrong number of args are generated.
3576 Returns the actual number of arguments processed (which might be less
3577 than the length of the vector), or -1 on error.
3579 In C++, unspecified trailing parameters can be filled in with their
3580 default arguments, if such were specified. Do so here. */
3582 static int
3583 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3584 int flags, tsubst_flags_t complain)
3586 tree typetail;
3587 unsigned int i;
3589 /* Argument passing is always copy-initialization. */
3590 flags |= LOOKUP_ONLYCONVERTING;
3592 for (i = 0, typetail = typelist;
3593 i < vec_safe_length (*values);
3594 i++)
3596 tree type = typetail ? TREE_VALUE (typetail) : 0;
3597 tree val = (**values)[i];
3599 if (val == error_mark_node || type == error_mark_node)
3600 return -1;
3602 if (type == void_type_node)
3604 if (complain & tf_error)
3606 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3607 return i;
3609 else
3610 return -1;
3613 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3614 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3615 if (TREE_CODE (val) == NOP_EXPR
3616 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3617 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3618 val = TREE_OPERAND (val, 0);
3620 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3622 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3623 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3624 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3625 val = decay_conversion (val, complain);
3628 if (val == error_mark_node)
3629 return -1;
3631 if (type != 0)
3633 /* Formal parm type is specified by a function prototype. */
3634 tree parmval;
3636 if (!COMPLETE_TYPE_P (complete_type (type)))
3638 if (complain & tf_error)
3640 if (fndecl)
3641 error ("parameter %P of %qD has incomplete type %qT",
3642 i, fndecl, type);
3643 else
3644 error ("parameter %P has incomplete type %qT", i, type);
3646 parmval = error_mark_node;
3648 else
3650 parmval = convert_for_initialization
3651 (NULL_TREE, type, val, flags,
3652 ICR_ARGPASS, fndecl, i, complain);
3653 parmval = convert_for_arg_passing (type, parmval, complain);
3656 if (parmval == error_mark_node)
3657 return -1;
3659 (**values)[i] = parmval;
3661 else
3663 if (fndecl && DECL_BUILT_IN (fndecl)
3664 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3665 /* Don't do ellipsis conversion for __built_in_constant_p
3666 as this will result in spurious errors for non-trivial
3667 types. */
3668 val = require_complete_type_sfinae (val, complain);
3669 else
3670 val = convert_arg_to_ellipsis (val, complain);
3672 (**values)[i] = val;
3675 if (typetail)
3676 typetail = TREE_CHAIN (typetail);
3679 if (typetail != 0 && typetail != void_list_node)
3681 /* See if there are default arguments that can be used. Because
3682 we hold default arguments in the FUNCTION_TYPE (which is so
3683 wrong), we can see default parameters here from deduced
3684 contexts (and via typeof) for indirect function calls.
3685 Fortunately we know whether we have a function decl to
3686 provide default arguments in a language conformant
3687 manner. */
3688 if (fndecl && TREE_PURPOSE (typetail)
3689 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3691 for (; typetail != void_list_node; ++i)
3693 tree parmval
3694 = convert_default_arg (TREE_VALUE (typetail),
3695 TREE_PURPOSE (typetail),
3696 fndecl, i, complain);
3698 if (parmval == error_mark_node)
3699 return -1;
3701 vec_safe_push (*values, parmval);
3702 typetail = TREE_CHAIN (typetail);
3703 /* ends with `...'. */
3704 if (typetail == NULL_TREE)
3705 break;
3708 else
3710 if (complain & tf_error)
3711 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3712 return -1;
3716 return (int) i;
3719 /* Build a binary-operation expression, after performing default
3720 conversions on the operands. CODE is the kind of expression to
3721 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3722 are the tree codes which correspond to ARG1 and ARG2 when issuing
3723 warnings about possibly misplaced parentheses. They may differ
3724 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3725 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3726 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3727 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3728 ARG2_CODE as ERROR_MARK. */
3730 tree
3731 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3732 enum tree_code arg1_code, tree arg2,
3733 enum tree_code arg2_code, tree *overload,
3734 tsubst_flags_t complain)
3736 tree orig_arg1;
3737 tree orig_arg2;
3738 tree expr;
3740 orig_arg1 = arg1;
3741 orig_arg2 = arg2;
3743 if (processing_template_decl)
3745 if (type_dependent_expression_p (arg1)
3746 || type_dependent_expression_p (arg2))
3747 return build_min_nt_loc (loc, code, arg1, arg2);
3748 arg1 = build_non_dependent_expr (arg1);
3749 arg2 = build_non_dependent_expr (arg2);
3752 if (code == DOTSTAR_EXPR)
3753 expr = build_m_component_ref (arg1, arg2, complain);
3754 else
3755 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3756 overload, complain);
3758 /* Check for cases such as x+y<<z which users are likely to
3759 misinterpret. But don't warn about obj << x + y, since that is a
3760 common idiom for I/O. */
3761 if (warn_parentheses
3762 && (complain & tf_warning)
3763 && !processing_template_decl
3764 && !error_operand_p (arg1)
3765 && !error_operand_p (arg2)
3766 && (code != LSHIFT_EXPR
3767 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3768 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3769 arg2_code, orig_arg2);
3771 if (processing_template_decl && expr != error_mark_node)
3772 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3774 return expr;
3777 /* Build and return an ARRAY_REF expression. */
3779 tree
3780 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3781 tsubst_flags_t complain)
3783 tree orig_arg1 = arg1;
3784 tree orig_arg2 = arg2;
3785 tree expr;
3787 if (processing_template_decl)
3789 if (type_dependent_expression_p (arg1)
3790 || type_dependent_expression_p (arg2))
3791 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3792 NULL_TREE, NULL_TREE);
3793 arg1 = build_non_dependent_expr (arg1);
3794 arg2 = build_non_dependent_expr (arg2);
3797 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3798 NULL_TREE, /*overload=*/NULL, complain);
3800 if (processing_template_decl && expr != error_mark_node)
3801 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3802 NULL_TREE, NULL_TREE);
3803 return expr;
3806 /* Return whether OP is an expression of enum type cast to integer
3807 type. In C++ even unsigned enum types are cast to signed integer
3808 types. We do not want to issue warnings about comparisons between
3809 signed and unsigned types when one of the types is an enum type.
3810 Those warnings are always false positives in practice. */
3812 static bool
3813 enum_cast_to_int (tree op)
3815 if (TREE_CODE (op) == NOP_EXPR
3816 && TREE_TYPE (op) == integer_type_node
3817 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3818 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3819 return true;
3821 /* The cast may have been pushed into a COND_EXPR. */
3822 if (TREE_CODE (op) == COND_EXPR)
3823 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3824 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3826 return false;
3829 /* For the c-common bits. */
3830 tree
3831 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3832 int /*convert_p*/)
3834 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3838 /* Build a binary-operation expression without default conversions.
3839 CODE is the kind of expression to build.
3840 LOCATION is the location_t of the operator in the source code.
3841 This function differs from `build' in several ways:
3842 the data type of the result is computed and recorded in it,
3843 warnings are generated if arg data types are invalid,
3844 special handling for addition and subtraction of pointers is known,
3845 and some optimization is done (operations on narrow ints
3846 are done in the narrower type when that gives the same result).
3847 Constant folding is also done before the result is returned.
3849 Note that the operands will never have enumeral types
3850 because either they have just had the default conversions performed
3851 or they have both just been converted to some other type in which
3852 the arithmetic is to be done.
3854 C++: must do special pointer arithmetic when implementing
3855 multiple inheritance, and deal with pointer to member functions. */
3857 tree
3858 cp_build_binary_op (location_t location,
3859 enum tree_code code, tree orig_op0, tree orig_op1,
3860 tsubst_flags_t complain)
3862 tree op0, op1;
3863 enum tree_code code0, code1;
3864 tree type0, type1;
3865 const char *invalid_op_diag;
3867 /* Expression code to give to the expression when it is built.
3868 Normally this is CODE, which is what the caller asked for,
3869 but in some special cases we change it. */
3870 enum tree_code resultcode = code;
3872 /* Data type in which the computation is to be performed.
3873 In the simplest cases this is the common type of the arguments. */
3874 tree result_type = NULL;
3876 /* Nonzero means operands have already been type-converted
3877 in whatever way is necessary.
3878 Zero means they need to be converted to RESULT_TYPE. */
3879 int converted = 0;
3881 /* Nonzero means create the expression with this type, rather than
3882 RESULT_TYPE. */
3883 tree build_type = 0;
3885 /* Nonzero means after finally constructing the expression
3886 convert it to this type. */
3887 tree final_type = 0;
3889 tree result;
3891 /* Nonzero if this is an operation like MIN or MAX which can
3892 safely be computed in short if both args are promoted shorts.
3893 Also implies COMMON.
3894 -1 indicates a bitwise operation; this makes a difference
3895 in the exact conditions for when it is safe to do the operation
3896 in a narrower mode. */
3897 int shorten = 0;
3899 /* Nonzero if this is a comparison operation;
3900 if both args are promoted shorts, compare the original shorts.
3901 Also implies COMMON. */
3902 int short_compare = 0;
3904 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3905 int common = 0;
3907 /* True if both operands have arithmetic type. */
3908 bool arithmetic_types_p;
3910 /* Apply default conversions. */
3911 op0 = orig_op0;
3912 op1 = orig_op1;
3914 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3915 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3916 || code == TRUTH_XOR_EXPR)
3918 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3919 op0 = decay_conversion (op0, complain);
3920 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3921 op1 = decay_conversion (op1, complain);
3923 else
3925 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3926 op0 = cp_default_conversion (op0, complain);
3927 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3928 op1 = cp_default_conversion (op1, complain);
3931 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3932 STRIP_TYPE_NOPS (op0);
3933 STRIP_TYPE_NOPS (op1);
3935 /* DTRT if one side is an overloaded function, but complain about it. */
3936 if (type_unknown_p (op0))
3938 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3939 if (t != error_mark_node)
3941 if (complain & tf_error)
3942 permerror (input_location, "assuming cast to type %qT from overloaded function",
3943 TREE_TYPE (t));
3944 op0 = t;
3947 if (type_unknown_p (op1))
3949 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3950 if (t != error_mark_node)
3952 if (complain & tf_error)
3953 permerror (input_location, "assuming cast to type %qT from overloaded function",
3954 TREE_TYPE (t));
3955 op1 = t;
3959 if (flag_enable_cilkplus && contains_array_notation_expr (op0))
3960 type0 = find_correct_array_notation_type (op0);
3961 else
3962 type0 = TREE_TYPE (op0);
3964 if (flag_enable_cilkplus && contains_array_notation_expr (op1))
3965 type1 = find_correct_array_notation_type (op1);
3966 else
3967 type1 = TREE_TYPE (op1);
3969 /* The expression codes of the data types of the arguments tell us
3970 whether the arguments are integers, floating, pointers, etc. */
3971 code0 = TREE_CODE (type0);
3972 code1 = TREE_CODE (type1);
3974 /* If an error was already reported for one of the arguments,
3975 avoid reporting another error. */
3976 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3977 return error_mark_node;
3979 if ((invalid_op_diag
3980 = targetm.invalid_binary_op (code, type0, type1)))
3982 if (complain & tf_error)
3983 error (invalid_op_diag);
3984 return error_mark_node;
3987 /* Issue warnings about peculiar, but valid, uses of NULL. */
3988 if ((orig_op0 == null_node || orig_op1 == null_node)
3989 /* It's reasonable to use pointer values as operands of &&
3990 and ||, so NULL is no exception. */
3991 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3992 && ( /* Both are NULL (or 0) and the operation was not a
3993 comparison or a pointer subtraction. */
3994 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3995 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3996 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3997 || (!null_ptr_cst_p (orig_op0)
3998 && !TYPE_PTR_OR_PTRMEM_P (type0))
3999 || (!null_ptr_cst_p (orig_op1)
4000 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4001 && (complain & tf_warning))
4003 source_location loc =
4004 expansion_point_location_if_in_system_header (input_location);
4006 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4009 /* In case when one of the operands of the binary operation is
4010 a vector and another is a scalar -- convert scalar to vector. */
4011 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4013 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4014 complain & tf_error);
4016 switch (convert_flag)
4018 case stv_error:
4019 return error_mark_node;
4020 case stv_firstarg:
4022 op0 = save_expr (op0);
4023 op0 = convert (TREE_TYPE (type1), op0);
4024 op0 = build_vector_from_val (type1, op0);
4025 type0 = TREE_TYPE (op0);
4026 code0 = TREE_CODE (type0);
4027 converted = 1;
4028 break;
4030 case stv_secondarg:
4032 op1 = save_expr (op1);
4033 op1 = convert (TREE_TYPE (type0), op1);
4034 op1 = build_vector_from_val (type0, op1);
4035 type1 = TREE_TYPE (op1);
4036 code1 = TREE_CODE (type1);
4037 converted = 1;
4038 break;
4040 default:
4041 break;
4045 switch (code)
4047 case MINUS_EXPR:
4048 /* Subtraction of two similar pointers.
4049 We must subtract them as integers, then divide by object size. */
4050 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4051 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4052 TREE_TYPE (type1)))
4053 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4054 complain);
4055 /* In all other cases except pointer - int, the usual arithmetic
4056 rules apply. */
4057 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4059 common = 1;
4060 break;
4062 /* The pointer - int case is just like pointer + int; fall
4063 through. */
4064 case PLUS_EXPR:
4065 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4066 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4068 tree ptr_operand;
4069 tree int_operand;
4070 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4071 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4072 if (processing_template_decl)
4074 result_type = TREE_TYPE (ptr_operand);
4075 break;
4077 return cp_pointer_int_sum (code,
4078 ptr_operand,
4079 int_operand);
4081 common = 1;
4082 break;
4084 case MULT_EXPR:
4085 common = 1;
4086 break;
4088 case TRUNC_DIV_EXPR:
4089 case CEIL_DIV_EXPR:
4090 case FLOOR_DIV_EXPR:
4091 case ROUND_DIV_EXPR:
4092 case EXACT_DIV_EXPR:
4093 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4094 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4095 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4096 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4098 enum tree_code tcode0 = code0, tcode1 = code1;
4099 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4101 warn_for_div_by_zero (location, maybe_constant_value (cop1));
4103 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4104 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4105 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4106 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4108 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4109 resultcode = RDIV_EXPR;
4110 else
4111 /* When dividing two signed integers, we have to promote to int.
4112 unless we divide by a constant != -1. Note that default
4113 conversion will have been performed on the operands at this
4114 point, so we have to dig out the original type to find out if
4115 it was unsigned. */
4116 shorten = ((TREE_CODE (op0) == NOP_EXPR
4117 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4118 || (TREE_CODE (op1) == INTEGER_CST
4119 && ! integer_all_onesp (op1)));
4121 common = 1;
4123 break;
4125 case BIT_AND_EXPR:
4126 case BIT_IOR_EXPR:
4127 case BIT_XOR_EXPR:
4128 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4129 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4130 && !VECTOR_FLOAT_TYPE_P (type0)
4131 && !VECTOR_FLOAT_TYPE_P (type1)))
4132 shorten = -1;
4133 break;
4135 case TRUNC_MOD_EXPR:
4136 case FLOOR_MOD_EXPR:
4138 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4140 warn_for_div_by_zero (location, maybe_constant_value (cop1));
4143 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4144 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4145 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4146 common = 1;
4147 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4149 /* Although it would be tempting to shorten always here, that loses
4150 on some targets, since the modulo instruction is undefined if the
4151 quotient can't be represented in the computation mode. We shorten
4152 only if unsigned or if dividing by something we know != -1. */
4153 shorten = ((TREE_CODE (op0) == NOP_EXPR
4154 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4155 || (TREE_CODE (op1) == INTEGER_CST
4156 && ! integer_all_onesp (op1)));
4157 common = 1;
4159 break;
4161 case TRUTH_ANDIF_EXPR:
4162 case TRUTH_ORIF_EXPR:
4163 case TRUTH_AND_EXPR:
4164 case TRUTH_OR_EXPR:
4165 result_type = boolean_type_node;
4166 break;
4168 /* Shift operations: result has same type as first operand;
4169 always convert second operand to int.
4170 Also set SHORT_SHIFT if shifting rightward. */
4172 case RSHIFT_EXPR:
4173 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4174 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4176 result_type = type0;
4177 converted = 1;
4179 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4180 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4181 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4182 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4184 result_type = type0;
4185 converted = 1;
4187 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4189 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4190 const_op1 = maybe_constant_value (const_op1);
4191 if (TREE_CODE (const_op1) != INTEGER_CST)
4192 const_op1 = op1;
4193 result_type = type0;
4194 if (TREE_CODE (const_op1) == INTEGER_CST)
4196 if (tree_int_cst_lt (const_op1, integer_zero_node))
4198 if ((complain & tf_warning)
4199 && c_inhibit_evaluation_warnings == 0)
4200 warning (0, "right shift count is negative");
4202 else
4204 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4205 && (complain & tf_warning)
4206 && c_inhibit_evaluation_warnings == 0)
4207 warning (0, "right shift count >= width of type");
4210 /* Convert the shift-count to an integer, regardless of
4211 size of value being shifted. */
4212 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4213 op1 = cp_convert (integer_type_node, op1, complain);
4214 /* Avoid converting op1 to result_type later. */
4215 converted = 1;
4217 break;
4219 case LSHIFT_EXPR:
4220 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4221 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4223 result_type = type0;
4224 converted = 1;
4226 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4227 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4228 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4229 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4231 result_type = type0;
4232 converted = 1;
4234 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4236 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4237 const_op1 = maybe_constant_value (const_op1);
4238 if (TREE_CODE (const_op1) != INTEGER_CST)
4239 const_op1 = op1;
4240 result_type = type0;
4241 if (TREE_CODE (const_op1) == INTEGER_CST)
4243 if (tree_int_cst_lt (const_op1, integer_zero_node))
4245 if ((complain & tf_warning)
4246 && c_inhibit_evaluation_warnings == 0)
4247 warning (0, "left shift count is negative");
4249 else if (compare_tree_int (const_op1,
4250 TYPE_PRECISION (type0)) >= 0)
4252 if ((complain & tf_warning)
4253 && c_inhibit_evaluation_warnings == 0)
4254 warning (0, "left shift count >= width of type");
4257 /* Convert the shift-count to an integer, regardless of
4258 size of value being shifted. */
4259 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4260 op1 = cp_convert (integer_type_node, op1, complain);
4261 /* Avoid converting op1 to result_type later. */
4262 converted = 1;
4264 break;
4266 case RROTATE_EXPR:
4267 case LROTATE_EXPR:
4268 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4270 result_type = type0;
4271 if (TREE_CODE (op1) == INTEGER_CST)
4273 if (tree_int_cst_lt (op1, integer_zero_node))
4275 if (complain & tf_warning)
4276 warning (0, (code == LROTATE_EXPR)
4277 ? G_("left rotate count is negative")
4278 : G_("right rotate count is negative"));
4280 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4282 if (complain & tf_warning)
4283 warning (0, (code == LROTATE_EXPR)
4284 ? G_("left rotate count >= width of type")
4285 : G_("right rotate count >= width of type"));
4288 /* Convert the shift-count to an integer, regardless of
4289 size of value being shifted. */
4290 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4291 op1 = cp_convert (integer_type_node, op1, complain);
4293 break;
4295 case EQ_EXPR:
4296 case NE_EXPR:
4297 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4298 goto vector_compare;
4299 if ((complain & tf_warning)
4300 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4301 warning (OPT_Wfloat_equal,
4302 "comparing floating point with == or != is unsafe");
4303 if ((complain & tf_warning)
4304 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4305 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4306 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4308 build_type = boolean_type_node;
4309 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4310 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4311 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4312 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4313 short_compare = 1;
4314 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4315 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4316 result_type = composite_pointer_type (type0, type1, op0, op1,
4317 CPO_COMPARISON, complain);
4318 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4319 && null_ptr_cst_p (op1))
4321 if (TREE_CODE (op0) == ADDR_EXPR
4322 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4324 if ((complain & tf_warning)
4325 && c_inhibit_evaluation_warnings == 0)
4326 warning (OPT_Waddress, "the address of %qD will never be NULL",
4327 TREE_OPERAND (op0, 0));
4329 result_type = type0;
4331 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4332 && null_ptr_cst_p (op0))
4334 if (TREE_CODE (op1) == ADDR_EXPR
4335 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4337 if ((complain & tf_warning)
4338 && c_inhibit_evaluation_warnings == 0)
4339 warning (OPT_Waddress, "the address of %qD will never be NULL",
4340 TREE_OPERAND (op1, 0));
4342 result_type = type1;
4344 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4345 /* One of the operands must be of nullptr_t type. */
4346 result_type = TREE_TYPE (nullptr_node);
4347 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4349 result_type = type0;
4350 if (complain & tf_error)
4351 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4352 else
4353 return error_mark_node;
4355 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4357 result_type = type1;
4358 if (complain & tf_error)
4359 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4360 else
4361 return error_mark_node;
4363 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4365 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4366 == ptrmemfunc_vbit_in_delta)
4368 tree pfn0, delta0, e1, e2;
4370 if (TREE_SIDE_EFFECTS (op0))
4371 op0 = save_expr (op0);
4373 pfn0 = pfn_from_ptrmemfunc (op0);
4374 delta0 = delta_from_ptrmemfunc (op0);
4375 e1 = cp_build_binary_op (location,
4376 EQ_EXPR,
4377 pfn0,
4378 build_zero_cst (TREE_TYPE (pfn0)),
4379 complain);
4380 e2 = cp_build_binary_op (location,
4381 BIT_AND_EXPR,
4382 delta0,
4383 integer_one_node,
4384 complain);
4386 if (complain & tf_warning)
4387 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4389 e2 = cp_build_binary_op (location,
4390 EQ_EXPR, e2, integer_zero_node,
4391 complain);
4392 op0 = cp_build_binary_op (location,
4393 TRUTH_ANDIF_EXPR, e1, e2,
4394 complain);
4395 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4397 else
4399 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4400 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4402 result_type = TREE_TYPE (op0);
4404 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4405 return cp_build_binary_op (location, code, op1, op0, complain);
4406 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4408 tree type;
4409 /* E will be the final comparison. */
4410 tree e;
4411 /* E1 and E2 are for scratch. */
4412 tree e1;
4413 tree e2;
4414 tree pfn0;
4415 tree pfn1;
4416 tree delta0;
4417 tree delta1;
4419 type = composite_pointer_type (type0, type1, op0, op1,
4420 CPO_COMPARISON, complain);
4422 if (!same_type_p (TREE_TYPE (op0), type))
4423 op0 = cp_convert_and_check (type, op0, complain);
4424 if (!same_type_p (TREE_TYPE (op1), type))
4425 op1 = cp_convert_and_check (type, op1, complain);
4427 if (op0 == error_mark_node || op1 == error_mark_node)
4428 return error_mark_node;
4430 if (TREE_SIDE_EFFECTS (op0))
4431 op0 = save_expr (op0);
4432 if (TREE_SIDE_EFFECTS (op1))
4433 op1 = save_expr (op1);
4435 pfn0 = pfn_from_ptrmemfunc (op0);
4436 pfn1 = pfn_from_ptrmemfunc (op1);
4437 delta0 = delta_from_ptrmemfunc (op0);
4438 delta1 = delta_from_ptrmemfunc (op1);
4439 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4440 == ptrmemfunc_vbit_in_delta)
4442 /* We generate:
4444 (op0.pfn == op1.pfn
4445 && ((op0.delta == op1.delta)
4446 || (!op0.pfn && op0.delta & 1 == 0
4447 && op1.delta & 1 == 0))
4449 The reason for the `!op0.pfn' bit is that a NULL
4450 pointer-to-member is any member with a zero PFN and
4451 LSB of the DELTA field is 0. */
4453 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4454 delta0,
4455 integer_one_node,
4456 complain);
4457 e1 = cp_build_binary_op (location,
4458 EQ_EXPR, e1, integer_zero_node,
4459 complain);
4460 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4461 delta1,
4462 integer_one_node,
4463 complain);
4464 e2 = cp_build_binary_op (location,
4465 EQ_EXPR, e2, integer_zero_node,
4466 complain);
4467 e1 = cp_build_binary_op (location,
4468 TRUTH_ANDIF_EXPR, e2, e1,
4469 complain);
4470 e2 = cp_build_binary_op (location, EQ_EXPR,
4471 pfn0,
4472 build_zero_cst (TREE_TYPE (pfn0)),
4473 complain);
4474 e2 = cp_build_binary_op (location,
4475 TRUTH_ANDIF_EXPR, e2, e1, complain);
4476 e1 = cp_build_binary_op (location,
4477 EQ_EXPR, delta0, delta1, complain);
4478 e1 = cp_build_binary_op (location,
4479 TRUTH_ORIF_EXPR, e1, e2, complain);
4481 else
4483 /* We generate:
4485 (op0.pfn == op1.pfn
4486 && (!op0.pfn || op0.delta == op1.delta))
4488 The reason for the `!op0.pfn' bit is that a NULL
4489 pointer-to-member is any member with a zero PFN; the
4490 DELTA field is unspecified. */
4492 e1 = cp_build_binary_op (location,
4493 EQ_EXPR, delta0, delta1, complain);
4494 e2 = cp_build_binary_op (location,
4495 EQ_EXPR,
4496 pfn0,
4497 build_zero_cst (TREE_TYPE (pfn0)),
4498 complain);
4499 e1 = cp_build_binary_op (location,
4500 TRUTH_ORIF_EXPR, e1, e2, complain);
4502 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4503 e = cp_build_binary_op (location,
4504 TRUTH_ANDIF_EXPR, e2, e1, complain);
4505 if (code == EQ_EXPR)
4506 return e;
4507 return cp_build_binary_op (location,
4508 EQ_EXPR, e, integer_zero_node, complain);
4510 else
4512 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4513 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4514 type1));
4515 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4516 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4517 type0));
4520 break;
4522 case MAX_EXPR:
4523 case MIN_EXPR:
4524 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4525 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4526 shorten = 1;
4527 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4528 result_type = composite_pointer_type (type0, type1, op0, op1,
4529 CPO_COMPARISON, complain);
4530 break;
4532 case LE_EXPR:
4533 case GE_EXPR:
4534 case LT_EXPR:
4535 case GT_EXPR:
4536 if (TREE_CODE (orig_op0) == STRING_CST
4537 || TREE_CODE (orig_op1) == STRING_CST)
4539 if (complain & tf_warning)
4540 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4543 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4545 vector_compare:
4546 tree intt;
4547 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4548 TREE_TYPE (type1)))
4550 error_at (location, "comparing vectors with different "
4551 "element types");
4552 inform (location, "operand types are %qT and %qT", type0, type1);
4553 return error_mark_node;
4556 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4558 error_at (location, "comparing vectors with different "
4559 "number of elements");
4560 inform (location, "operand types are %qT and %qT", type0, type1);
4561 return error_mark_node;
4564 /* Always construct signed integer vector type. */
4565 intt = c_common_type_for_size (GET_MODE_BITSIZE
4566 (TYPE_MODE (TREE_TYPE (type0))), 0);
4567 result_type = build_opaque_vector_type (intt,
4568 TYPE_VECTOR_SUBPARTS (type0));
4569 converted = 1;
4570 break;
4572 build_type = boolean_type_node;
4573 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4574 || code0 == ENUMERAL_TYPE)
4575 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4576 || code1 == ENUMERAL_TYPE))
4577 short_compare = 1;
4578 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4579 result_type = composite_pointer_type (type0, type1, op0, op1,
4580 CPO_COMPARISON, complain);
4581 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4583 result_type = type0;
4584 if (extra_warnings && (complain & tf_warning))
4585 warning (OPT_Wextra,
4586 "ordered comparison of pointer with integer zero");
4588 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4590 result_type = type1;
4591 if (extra_warnings && (complain & tf_warning))
4592 warning (OPT_Wextra,
4593 "ordered comparison of pointer with integer zero");
4595 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4596 /* One of the operands must be of nullptr_t type. */
4597 result_type = TREE_TYPE (nullptr_node);
4598 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4600 result_type = type0;
4601 if (complain & tf_error)
4602 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4603 else
4604 return error_mark_node;
4606 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4608 result_type = type1;
4609 if (complain & tf_error)
4610 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4611 else
4612 return error_mark_node;
4614 break;
4616 case UNORDERED_EXPR:
4617 case ORDERED_EXPR:
4618 case UNLT_EXPR:
4619 case UNLE_EXPR:
4620 case UNGT_EXPR:
4621 case UNGE_EXPR:
4622 case UNEQ_EXPR:
4623 build_type = integer_type_node;
4624 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4626 if (complain & tf_error)
4627 error ("unordered comparison on non-floating point argument");
4628 return error_mark_node;
4630 common = 1;
4631 break;
4633 default:
4634 break;
4637 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4638 || code0 == ENUMERAL_TYPE)
4639 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4640 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4641 arithmetic_types_p = 1;
4642 else
4644 arithmetic_types_p = 0;
4645 /* Vector arithmetic is only allowed when both sides are vectors. */
4646 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4648 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4649 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4650 TREE_TYPE (type1)))
4652 if (complain & tf_error)
4653 binary_op_error (location, code, type0, type1);
4654 return error_mark_node;
4656 arithmetic_types_p = 1;
4659 /* Determine the RESULT_TYPE, if it is not already known. */
4660 if (!result_type
4661 && arithmetic_types_p
4662 && (shorten || common || short_compare))
4664 result_type = cp_common_type (type0, type1);
4665 if (complain & tf_warning)
4666 do_warn_double_promotion (result_type, type0, type1,
4667 "implicit conversion from %qT to %qT "
4668 "to match other operand of binary "
4669 "expression",
4670 location);
4673 if (!result_type)
4675 if (complain & tf_error)
4676 error ("invalid operands of types %qT and %qT to binary %qO",
4677 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4678 return error_mark_node;
4681 /* If we're in a template, the only thing we need to know is the
4682 RESULT_TYPE. */
4683 if (processing_template_decl)
4685 /* Since the middle-end checks the type when doing a build2, we
4686 need to build the tree in pieces. This built tree will never
4687 get out of the front-end as we replace it when instantiating
4688 the template. */
4689 tree tmp = build2 (resultcode,
4690 build_type ? build_type : result_type,
4691 NULL_TREE, op1);
4692 TREE_OPERAND (tmp, 0) = op0;
4693 return tmp;
4696 if (arithmetic_types_p)
4698 bool first_complex = (code0 == COMPLEX_TYPE);
4699 bool second_complex = (code1 == COMPLEX_TYPE);
4700 int none_complex = (!first_complex && !second_complex);
4702 /* Adapted from patch for c/24581. */
4703 if (first_complex != second_complex
4704 && (code == PLUS_EXPR
4705 || code == MINUS_EXPR
4706 || code == MULT_EXPR
4707 || (code == TRUNC_DIV_EXPR && first_complex))
4708 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4709 && flag_signed_zeros)
4711 /* An operation on mixed real/complex operands must be
4712 handled specially, but the language-independent code can
4713 more easily optimize the plain complex arithmetic if
4714 -fno-signed-zeros. */
4715 tree real_type = TREE_TYPE (result_type);
4716 tree real, imag;
4717 if (first_complex)
4719 if (TREE_TYPE (op0) != result_type)
4720 op0 = cp_convert_and_check (result_type, op0, complain);
4721 if (TREE_TYPE (op1) != real_type)
4722 op1 = cp_convert_and_check (real_type, op1, complain);
4724 else
4726 if (TREE_TYPE (op0) != real_type)
4727 op0 = cp_convert_and_check (real_type, op0, complain);
4728 if (TREE_TYPE (op1) != result_type)
4729 op1 = cp_convert_and_check (result_type, op1, complain);
4731 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4732 return error_mark_node;
4733 if (first_complex)
4735 op0 = save_expr (op0);
4736 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4737 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4738 switch (code)
4740 case MULT_EXPR:
4741 case TRUNC_DIV_EXPR:
4742 op1 = save_expr (op1);
4743 imag = build2 (resultcode, real_type, imag, op1);
4744 /* Fall through. */
4745 case PLUS_EXPR:
4746 case MINUS_EXPR:
4747 real = build2 (resultcode, real_type, real, op1);
4748 break;
4749 default:
4750 gcc_unreachable();
4753 else
4755 op1 = save_expr (op1);
4756 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4757 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4758 switch (code)
4760 case MULT_EXPR:
4761 op0 = save_expr (op0);
4762 imag = build2 (resultcode, real_type, op0, imag);
4763 /* Fall through. */
4764 case PLUS_EXPR:
4765 real = build2 (resultcode, real_type, op0, real);
4766 break;
4767 case MINUS_EXPR:
4768 real = build2 (resultcode, real_type, op0, real);
4769 imag = build1 (NEGATE_EXPR, real_type, imag);
4770 break;
4771 default:
4772 gcc_unreachable();
4775 real = fold_if_not_in_template (real);
4776 imag = fold_if_not_in_template (imag);
4777 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4778 result = fold_if_not_in_template (result);
4779 return result;
4782 /* For certain operations (which identify themselves by shorten != 0)
4783 if both args were extended from the same smaller type,
4784 do the arithmetic in that type and then extend.
4786 shorten !=0 and !=1 indicates a bitwise operation.
4787 For them, this optimization is safe only if
4788 both args are zero-extended or both are sign-extended.
4789 Otherwise, we might change the result.
4790 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4791 but calculated in (unsigned short) it would be (unsigned short)-1. */
4793 if (shorten && none_complex)
4795 final_type = result_type;
4796 result_type = shorten_binary_op (result_type, op0, op1,
4797 shorten == -1);
4800 /* Comparison operations are shortened too but differently.
4801 They identify themselves by setting short_compare = 1. */
4803 if (short_compare)
4805 /* Don't write &op0, etc., because that would prevent op0
4806 from being kept in a register.
4807 Instead, make copies of the our local variables and
4808 pass the copies by reference, then copy them back afterward. */
4809 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4810 enum tree_code xresultcode = resultcode;
4811 tree val
4812 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4813 if (val != 0)
4814 return cp_convert (boolean_type_node, val, complain);
4815 op0 = xop0, op1 = xop1;
4816 converted = 1;
4817 resultcode = xresultcode;
4820 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4821 && warn_sign_compare
4822 /* Do not warn until the template is instantiated; we cannot
4823 bound the ranges of the arguments until that point. */
4824 && !processing_template_decl
4825 && (complain & tf_warning)
4826 && c_inhibit_evaluation_warnings == 0
4827 /* Even unsigned enum types promote to signed int. We don't
4828 want to issue -Wsign-compare warnings for this case. */
4829 && !enum_cast_to_int (orig_op0)
4830 && !enum_cast_to_int (orig_op1))
4832 tree oop0 = maybe_constant_value (orig_op0);
4833 tree oop1 = maybe_constant_value (orig_op1);
4835 if (TREE_CODE (oop0) != INTEGER_CST)
4836 oop0 = orig_op0;
4837 if (TREE_CODE (oop1) != INTEGER_CST)
4838 oop1 = orig_op1;
4839 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4840 result_type, resultcode);
4844 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4845 Then the expression will be built.
4846 It will be given type FINAL_TYPE if that is nonzero;
4847 otherwise, it will be given type RESULT_TYPE. */
4848 if (! converted)
4850 if (TREE_TYPE (op0) != result_type)
4851 op0 = cp_convert_and_check (result_type, op0, complain);
4852 if (TREE_TYPE (op1) != result_type)
4853 op1 = cp_convert_and_check (result_type, op1, complain);
4855 if (op0 == error_mark_node || op1 == error_mark_node)
4856 return error_mark_node;
4859 if (build_type == NULL_TREE)
4860 build_type = result_type;
4862 result = build2 (resultcode, build_type, op0, op1);
4863 result = fold_if_not_in_template (result);
4864 if (final_type != 0)
4865 result = cp_convert (final_type, result, complain);
4867 if (TREE_OVERFLOW_P (result)
4868 && !TREE_OVERFLOW_P (op0)
4869 && !TREE_OVERFLOW_P (op1))
4870 overflow_warning (location, result);
4872 return result;
4875 /* Return a tree for the sum or difference (RESULTCODE says which)
4876 of pointer PTROP and integer INTOP. */
4878 static tree
4879 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4881 tree res_type = TREE_TYPE (ptrop);
4883 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4884 in certain circumstance (when it's valid to do so). So we need
4885 to make sure it's complete. We don't need to check here, if we
4886 can actually complete it at all, as those checks will be done in
4887 pointer_int_sum() anyway. */
4888 complete_type (TREE_TYPE (res_type));
4890 return pointer_int_sum (input_location, resultcode, ptrop,
4891 fold_if_not_in_template (intop));
4894 /* Return a tree for the difference of pointers OP0 and OP1.
4895 The resulting tree has type int. */
4897 static tree
4898 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
4900 tree result;
4901 tree restype = ptrdiff_type_node;
4902 tree target_type = TREE_TYPE (ptrtype);
4904 if (!complete_type_or_else (target_type, NULL_TREE))
4905 return error_mark_node;
4907 if (VOID_TYPE_P (target_type))
4909 if (complain & tf_error)
4910 permerror (input_location, "ISO C++ forbids using pointer of "
4911 "type %<void *%> in subtraction");
4912 else
4913 return error_mark_node;
4915 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4917 if (complain & tf_error)
4918 permerror (input_location, "ISO C++ forbids using pointer to "
4919 "a function in subtraction");
4920 else
4921 return error_mark_node;
4923 if (TREE_CODE (target_type) == METHOD_TYPE)
4925 if (complain & tf_error)
4926 permerror (input_location, "ISO C++ forbids using pointer to "
4927 "a method in subtraction");
4928 else
4929 return error_mark_node;
4932 /* First do the subtraction as integers;
4933 then drop through to build the divide operator. */
4935 op0 = cp_build_binary_op (input_location,
4936 MINUS_EXPR,
4937 cp_convert (restype, op0, complain),
4938 cp_convert (restype, op1, complain),
4939 complain);
4941 /* This generates an error if op1 is a pointer to an incomplete type. */
4942 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4944 if (complain & tf_error)
4945 error ("invalid use of a pointer to an incomplete type in "
4946 "pointer arithmetic");
4947 else
4948 return error_mark_node;
4951 op1 = (TYPE_PTROB_P (ptrtype)
4952 ? size_in_bytes (target_type)
4953 : integer_one_node);
4955 /* Do the division. */
4957 result = build2 (EXACT_DIV_EXPR, restype, op0,
4958 cp_convert (restype, op1, complain));
4959 return fold_if_not_in_template (result);
4962 /* Construct and perhaps optimize a tree representation
4963 for a unary operation. CODE, a tree_code, specifies the operation
4964 and XARG is the operand. */
4966 tree
4967 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
4968 tsubst_flags_t complain)
4970 tree orig_expr = xarg;
4971 tree exp;
4972 int ptrmem = 0;
4974 if (processing_template_decl)
4976 if (type_dependent_expression_p (xarg))
4977 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
4979 xarg = build_non_dependent_expr (xarg);
4982 exp = NULL_TREE;
4984 /* [expr.unary.op] says:
4986 The address of an object of incomplete type can be taken.
4988 (And is just the ordinary address operator, not an overloaded
4989 "operator &".) However, if the type is a template
4990 specialization, we must complete the type at this point so that
4991 an overloaded "operator &" will be available if required. */
4992 if (code == ADDR_EXPR
4993 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4994 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4995 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4996 || (TREE_CODE (xarg) == OFFSET_REF)))
4997 /* Don't look for a function. */;
4998 else
4999 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5000 NULL_TREE, /*overload=*/NULL, complain);
5001 if (!exp && code == ADDR_EXPR)
5003 if (is_overloaded_fn (xarg))
5005 tree fn = get_first_fn (xarg);
5006 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5008 if (complain & tf_error)
5009 error (DECL_CONSTRUCTOR_P (fn)
5010 ? G_("taking address of constructor %qE")
5011 : G_("taking address of destructor %qE"),
5012 xarg);
5013 return error_mark_node;
5017 /* A pointer to member-function can be formed only by saying
5018 &X::mf. */
5019 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5020 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5022 if (TREE_CODE (xarg) != OFFSET_REF
5023 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5025 if (complain & tf_error)
5027 error ("invalid use of %qE to form a "
5028 "pointer-to-member-function", xarg);
5029 if (TREE_CODE (xarg) != OFFSET_REF)
5030 inform (input_location, " a qualified-id is required");
5032 return error_mark_node;
5034 else
5036 if (complain & tf_error)
5037 error ("parentheses around %qE cannot be used to form a"
5038 " pointer-to-member-function",
5039 xarg);
5040 else
5041 return error_mark_node;
5042 PTRMEM_OK_P (xarg) = 1;
5046 if (TREE_CODE (xarg) == OFFSET_REF)
5048 ptrmem = PTRMEM_OK_P (xarg);
5050 if (!ptrmem && !flag_ms_extensions
5051 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5053 /* A single non-static member, make sure we don't allow a
5054 pointer-to-member. */
5055 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5056 TREE_OPERAND (xarg, 0),
5057 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5058 PTRMEM_OK_P (xarg) = ptrmem;
5062 exp = cp_build_addr_expr_strict (xarg, complain);
5065 if (processing_template_decl && exp != error_mark_node)
5066 exp = build_min_non_dep (code, exp, orig_expr,
5067 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5068 if (TREE_CODE (exp) == ADDR_EXPR)
5069 PTRMEM_OK_P (exp) = ptrmem;
5070 return exp;
5073 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5074 constants, where a null value is represented by an INTEGER_CST of
5075 -1. */
5077 tree
5078 cp_truthvalue_conversion (tree expr)
5080 tree type = TREE_TYPE (expr);
5081 if (TYPE_PTRDATAMEM_P (type))
5082 return build_binary_op (EXPR_LOCATION (expr),
5083 NE_EXPR, expr, nullptr_node, 1);
5084 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5086 /* With -Wzero-as-null-pointer-constant do not warn for an
5087 'if (p)' or a 'while (!p)', where p is a pointer. */
5088 tree ret;
5089 ++c_inhibit_evaluation_warnings;
5090 ret = c_common_truthvalue_conversion (input_location, expr);
5091 --c_inhibit_evaluation_warnings;
5092 return ret;
5094 else
5095 return c_common_truthvalue_conversion (input_location, expr);
5098 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5100 tree
5101 condition_conversion (tree expr)
5103 tree t;
5104 if (processing_template_decl)
5105 return expr;
5106 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5107 tf_warning_or_error, LOOKUP_NORMAL);
5108 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5109 return t;
5112 /* Returns the address of T. This function will fold away
5113 ADDR_EXPR of INDIRECT_REF. */
5115 tree
5116 build_address (tree t)
5118 if (error_operand_p (t) || !cxx_mark_addressable (t))
5119 return error_mark_node;
5120 t = build_fold_addr_expr (t);
5121 if (TREE_CODE (t) != ADDR_EXPR)
5122 t = rvalue (t);
5123 return t;
5126 /* Returns the address of T with type TYPE. */
5128 tree
5129 build_typed_address (tree t, tree type)
5131 if (error_operand_p (t) || !cxx_mark_addressable (t))
5132 return error_mark_node;
5133 t = build_fold_addr_expr_with_type (t, type);
5134 if (TREE_CODE (t) != ADDR_EXPR)
5135 t = rvalue (t);
5136 return t;
5139 /* Return a NOP_EXPR converting EXPR to TYPE. */
5141 tree
5142 build_nop (tree type, tree expr)
5144 if (type == error_mark_node || error_operand_p (expr))
5145 return expr;
5146 return build1 (NOP_EXPR, type, expr);
5149 /* Take the address of ARG, whatever that means under C++ semantics.
5150 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5151 and class rvalues as well.
5153 Nothing should call this function directly; instead, callers should use
5154 cp_build_addr_expr or cp_build_addr_expr_strict. */
5156 static tree
5157 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5159 tree argtype;
5160 tree val;
5162 if (!arg || error_operand_p (arg))
5163 return error_mark_node;
5165 arg = mark_lvalue_use (arg);
5166 argtype = lvalue_type (arg);
5168 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5170 if (flag_enable_cilkplus && TREE_CODE (arg) == ARRAY_NOTATION_REF)
5172 val = build_address (arg);
5173 if (TREE_CODE (arg) == OFFSET_REF)
5174 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5175 return val;
5177 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5178 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5180 /* They're trying to take the address of a unique non-static
5181 member function. This is ill-formed (except in MS-land),
5182 but let's try to DTRT.
5183 Note: We only handle unique functions here because we don't
5184 want to complain if there's a static overload; non-unique
5185 cases will be handled by instantiate_type. But we need to
5186 handle this case here to allow casts on the resulting PMF.
5187 We could defer this in non-MS mode, but it's easier to give
5188 a useful error here. */
5190 /* Inside constant member functions, the `this' pointer
5191 contains an extra const qualifier. TYPE_MAIN_VARIANT
5192 is used here to remove this const from the diagnostics
5193 and the created OFFSET_REF. */
5194 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5195 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5196 mark_used (fn);
5198 if (! flag_ms_extensions)
5200 tree name = DECL_NAME (fn);
5201 if (!(complain & tf_error))
5202 return error_mark_node;
5203 else if (current_class_type
5204 && TREE_OPERAND (arg, 0) == current_class_ref)
5205 /* An expression like &memfn. */
5206 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5207 " or parenthesized non-static member function to form"
5208 " a pointer to member function. Say %<&%T::%D%>",
5209 base, name);
5210 else
5211 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5212 " function to form a pointer to member function."
5213 " Say %<&%T::%D%>",
5214 base, name);
5216 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5219 /* Uninstantiated types are all functions. Taking the
5220 address of a function is a no-op, so just return the
5221 argument. */
5222 if (type_unknown_p (arg))
5223 return build1 (ADDR_EXPR, unknown_type_node, arg);
5225 if (TREE_CODE (arg) == OFFSET_REF)
5226 /* We want a pointer to member; bypass all the code for actually taking
5227 the address of something. */
5228 goto offset_ref;
5230 /* Anything not already handled and not a true memory reference
5231 is an error. */
5232 if (TREE_CODE (argtype) != FUNCTION_TYPE
5233 && TREE_CODE (argtype) != METHOD_TYPE)
5235 cp_lvalue_kind kind = lvalue_kind (arg);
5236 if (kind == clk_none)
5238 if (complain & tf_error)
5239 lvalue_error (input_location, lv_addressof);
5240 return error_mark_node;
5242 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5244 if (!(complain & tf_error))
5245 return error_mark_node;
5246 if (kind & clk_class)
5247 /* Make this a permerror because we used to accept it. */
5248 permerror (input_location, "taking address of temporary");
5249 else
5250 error ("taking address of xvalue (rvalue reference)");
5254 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5256 tree type = build_pointer_type (TREE_TYPE (argtype));
5257 arg = build1 (CONVERT_EXPR, type, arg);
5258 return arg;
5260 else if (pedantic && DECL_MAIN_P (arg))
5262 /* ARM $3.4 */
5263 /* Apparently a lot of autoconf scripts for C++ packages do this,
5264 so only complain if -Wpedantic. */
5265 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5266 pedwarn (input_location, OPT_Wpedantic,
5267 "ISO C++ forbids taking address of function %<::main%>");
5268 else if (flag_pedantic_errors)
5269 return error_mark_node;
5272 /* Let &* cancel out to simplify resulting code. */
5273 if (INDIRECT_REF_P (arg))
5275 /* We don't need to have `current_class_ptr' wrapped in a
5276 NON_LVALUE_EXPR node. */
5277 if (arg == current_class_ref)
5278 return current_class_ptr;
5280 arg = TREE_OPERAND (arg, 0);
5281 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5283 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5284 arg = build1 (CONVERT_EXPR, type, arg);
5286 else
5287 /* Don't let this be an lvalue. */
5288 arg = rvalue (arg);
5289 return arg;
5292 /* ??? Cope with user tricks that amount to offsetof. */
5293 if (TREE_CODE (argtype) != FUNCTION_TYPE
5294 && TREE_CODE (argtype) != METHOD_TYPE
5295 && argtype != unknown_type_node
5296 && (val = get_base_address (arg))
5297 && COMPLETE_TYPE_P (TREE_TYPE (val))
5298 && INDIRECT_REF_P (val)
5299 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5301 tree type = build_pointer_type (argtype);
5302 return fold_convert (type, fold_offsetof_1 (arg));
5305 /* Handle complex lvalues (when permitted)
5306 by reduction to simpler cases. */
5307 val = unary_complex_lvalue (ADDR_EXPR, arg);
5308 if (val != 0)
5309 return val;
5311 switch (TREE_CODE (arg))
5313 CASE_CONVERT:
5314 case FLOAT_EXPR:
5315 case FIX_TRUNC_EXPR:
5316 /* Even if we're not being pedantic, we cannot allow this
5317 extension when we're instantiating in a SFINAE
5318 context. */
5319 if (! lvalue_p (arg) && complain == tf_none)
5321 if (complain & tf_error)
5322 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5323 else
5324 return error_mark_node;
5326 break;
5328 case BASELINK:
5329 arg = BASELINK_FUNCTIONS (arg);
5330 /* Fall through. */
5332 case OVERLOAD:
5333 arg = OVL_CURRENT (arg);
5334 break;
5336 case OFFSET_REF:
5337 offset_ref:
5338 /* Turn a reference to a non-static data member into a
5339 pointer-to-member. */
5341 tree type;
5342 tree t;
5344 gcc_assert (PTRMEM_OK_P (arg));
5346 t = TREE_OPERAND (arg, 1);
5347 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5349 if (complain & tf_error)
5350 error ("cannot create pointer to reference member %qD", t);
5351 return error_mark_node;
5354 type = build_ptrmem_type (context_for_name_lookup (t),
5355 TREE_TYPE (t));
5356 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5357 return t;
5360 default:
5361 break;
5364 if (argtype != error_mark_node)
5366 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (argtype))
5368 if (complain & tf_warning_or_error)
5369 pedwarn (input_location, OPT_Wvla,
5370 "taking address of array of runtime bound");
5371 else
5372 return error_mark_node;
5374 argtype = build_pointer_type (argtype);
5377 /* In a template, we are processing a non-dependent expression
5378 so we can just form an ADDR_EXPR with the correct type. */
5379 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5381 val = build_address (arg);
5382 if (TREE_CODE (arg) == OFFSET_REF)
5383 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5385 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5387 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5389 /* We can only get here with a single static member
5390 function. */
5391 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5392 && DECL_STATIC_FUNCTION_P (fn));
5393 mark_used (fn);
5394 val = build_address (fn);
5395 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5396 /* Do not lose object's side effects. */
5397 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5398 TREE_OPERAND (arg, 0), val);
5400 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5402 if (complain & tf_error)
5403 error ("attempt to take address of bit-field structure member %qD",
5404 TREE_OPERAND (arg, 1));
5405 return error_mark_node;
5407 else
5409 tree object = TREE_OPERAND (arg, 0);
5410 tree field = TREE_OPERAND (arg, 1);
5411 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5412 (TREE_TYPE (object), decl_type_context (field)));
5413 val = build_address (arg);
5416 if (TYPE_PTR_P (argtype)
5417 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5419 build_ptrmemfunc_type (argtype);
5420 val = build_ptrmemfunc (argtype, val, 0,
5421 /*c_cast_p=*/false,
5422 complain);
5425 return val;
5428 /* Take the address of ARG if it has one, even if it's an rvalue. */
5430 tree
5431 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5433 return cp_build_addr_expr_1 (arg, 0, complain);
5436 /* Take the address of ARG, but only if it's an lvalue. */
5438 tree
5439 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5441 return cp_build_addr_expr_1 (arg, 1, complain);
5444 /* C++: Must handle pointers to members.
5446 Perhaps type instantiation should be extended to handle conversion
5447 from aggregates to types we don't yet know we want? (Or are those
5448 cases typically errors which should be reported?)
5450 NOCONVERT nonzero suppresses the default promotions
5451 (such as from short to int). */
5453 tree
5454 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5455 tsubst_flags_t complain)
5457 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5458 tree arg = xarg;
5459 tree argtype = 0;
5460 const char *errstring = NULL;
5461 tree val;
5462 const char *invalid_op_diag;
5464 if (!arg || error_operand_p (arg))
5465 return error_mark_node;
5467 if ((invalid_op_diag
5468 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5469 ? CONVERT_EXPR
5470 : code),
5471 TREE_TYPE (xarg))))
5473 if (complain & tf_error)
5474 error (invalid_op_diag);
5475 return error_mark_node;
5478 switch (code)
5480 case UNARY_PLUS_EXPR:
5481 case NEGATE_EXPR:
5483 int flags = WANT_ARITH | WANT_ENUM;
5484 /* Unary plus (but not unary minus) is allowed on pointers. */
5485 if (code == UNARY_PLUS_EXPR)
5486 flags |= WANT_POINTER;
5487 arg = build_expr_type_conversion (flags, arg, true);
5488 if (!arg)
5489 errstring = (code == NEGATE_EXPR
5490 ? _("wrong type argument to unary minus")
5491 : _("wrong type argument to unary plus"));
5492 else
5494 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5495 arg = cp_perform_integral_promotions (arg, complain);
5497 /* Make sure the result is not an lvalue: a unary plus or minus
5498 expression is always a rvalue. */
5499 arg = rvalue (arg);
5502 break;
5504 case BIT_NOT_EXPR:
5505 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5507 code = CONJ_EXPR;
5508 if (!noconvert)
5510 arg = cp_default_conversion (arg, complain);
5511 if (arg == error_mark_node)
5512 return error_mark_node;
5515 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5516 | WANT_VECTOR_OR_COMPLEX,
5517 arg, true)))
5518 errstring = _("wrong type argument to bit-complement");
5519 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5520 arg = cp_perform_integral_promotions (arg, complain);
5521 break;
5523 case ABS_EXPR:
5524 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5525 errstring = _("wrong type argument to abs");
5526 else if (!noconvert)
5528 arg = cp_default_conversion (arg, complain);
5529 if (arg == error_mark_node)
5530 return error_mark_node;
5532 break;
5534 case CONJ_EXPR:
5535 /* Conjugating a real value is a no-op, but allow it anyway. */
5536 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5537 errstring = _("wrong type argument to conjugation");
5538 else if (!noconvert)
5540 arg = cp_default_conversion (arg, complain);
5541 if (arg == error_mark_node)
5542 return error_mark_node;
5544 break;
5546 case TRUTH_NOT_EXPR:
5547 arg = perform_implicit_conversion (boolean_type_node, arg,
5548 complain);
5549 val = invert_truthvalue_loc (input_location, arg);
5550 if (arg != error_mark_node)
5551 return val;
5552 errstring = _("in argument to unary !");
5553 break;
5555 case NOP_EXPR:
5556 break;
5558 case REALPART_EXPR:
5559 case IMAGPART_EXPR:
5560 arg = build_real_imag_expr (input_location, code, arg);
5561 if (arg == error_mark_node)
5562 return arg;
5563 else
5564 return fold_if_not_in_template (arg);
5566 case PREINCREMENT_EXPR:
5567 case POSTINCREMENT_EXPR:
5568 case PREDECREMENT_EXPR:
5569 case POSTDECREMENT_EXPR:
5570 /* Handle complex lvalues (when permitted)
5571 by reduction to simpler cases. */
5573 val = unary_complex_lvalue (code, arg);
5574 if (val != 0)
5575 return val;
5577 arg = mark_lvalue_use (arg);
5579 /* Increment or decrement the real part of the value,
5580 and don't change the imaginary part. */
5581 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5583 tree real, imag;
5585 arg = stabilize_reference (arg);
5586 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5587 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5588 real = cp_build_unary_op (code, real, 1, complain);
5589 if (real == error_mark_node || imag == error_mark_node)
5590 return error_mark_node;
5591 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5592 real, imag);
5595 /* Report invalid types. */
5597 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5598 arg, true)))
5600 if (code == PREINCREMENT_EXPR)
5601 errstring = _("no pre-increment operator for type");
5602 else if (code == POSTINCREMENT_EXPR)
5603 errstring = _("no post-increment operator for type");
5604 else if (code == PREDECREMENT_EXPR)
5605 errstring = _("no pre-decrement operator for type");
5606 else
5607 errstring = _("no post-decrement operator for type");
5608 break;
5610 else if (arg == error_mark_node)
5611 return error_mark_node;
5613 /* Report something read-only. */
5615 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5616 || TREE_READONLY (arg))
5618 if (complain & tf_error)
5619 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5620 || code == POSTINCREMENT_EXPR)
5621 ? lv_increment : lv_decrement));
5622 else
5623 return error_mark_node;
5627 tree inc;
5628 tree declared_type = unlowered_expr_type (arg);
5630 argtype = TREE_TYPE (arg);
5632 /* ARM $5.2.5 last annotation says this should be forbidden. */
5633 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5635 if (complain & tf_error)
5636 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5637 ? G_("ISO C++ forbids incrementing an enum")
5638 : G_("ISO C++ forbids decrementing an enum"));
5639 else
5640 return error_mark_node;
5643 /* Compute the increment. */
5645 if (TYPE_PTR_P (argtype))
5647 tree type = complete_type (TREE_TYPE (argtype));
5649 if (!COMPLETE_OR_VOID_TYPE_P (type))
5651 if (complain & tf_error)
5652 error (((code == PREINCREMENT_EXPR
5653 || code == POSTINCREMENT_EXPR))
5654 ? G_("cannot increment a pointer to incomplete type %qT")
5655 : G_("cannot decrement a pointer to incomplete type %qT"),
5656 TREE_TYPE (argtype));
5657 else
5658 return error_mark_node;
5660 else if (!TYPE_PTROB_P (argtype))
5662 if (complain & tf_error)
5663 pedwarn (input_location, OPT_Wpointer_arith,
5664 (code == PREINCREMENT_EXPR
5665 || code == POSTINCREMENT_EXPR)
5666 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5667 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5668 argtype);
5669 else
5670 return error_mark_node;
5673 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5675 else
5676 inc = integer_one_node;
5678 inc = cp_convert (argtype, inc, complain);
5680 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5681 need to ask Objective-C to build the increment or decrement
5682 expression for it. */
5683 if (objc_is_property_ref (arg))
5684 return objc_build_incr_expr_for_property_ref (input_location, code,
5685 arg, inc);
5687 /* Complain about anything else that is not a true lvalue. */
5688 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5689 || code == POSTINCREMENT_EXPR)
5690 ? lv_increment : lv_decrement),
5691 complain))
5692 return error_mark_node;
5694 /* Forbid using -- on `bool'. */
5695 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5697 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5699 if (complain & tf_error)
5700 error ("invalid use of Boolean expression as operand "
5701 "to %<operator--%>");
5702 return error_mark_node;
5704 val = boolean_increment (code, arg);
5706 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5707 /* An rvalue has no cv-qualifiers. */
5708 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5709 else
5710 val = build2 (code, TREE_TYPE (arg), arg, inc);
5712 TREE_SIDE_EFFECTS (val) = 1;
5713 return val;
5716 case ADDR_EXPR:
5717 /* Note that this operation never does default_conversion
5718 regardless of NOCONVERT. */
5719 return cp_build_addr_expr (arg, complain);
5721 default:
5722 break;
5725 if (!errstring)
5727 if (argtype == 0)
5728 argtype = TREE_TYPE (arg);
5729 return fold_if_not_in_template (build1 (code, argtype, arg));
5732 if (complain & tf_error)
5733 error ("%s", errstring);
5734 return error_mark_node;
5737 /* Hook for the c-common bits that build a unary op. */
5738 tree
5739 build_unary_op (location_t /*location*/,
5740 enum tree_code code, tree xarg, int noconvert)
5742 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5745 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5746 for certain kinds of expressions which are not really lvalues
5747 but which we can accept as lvalues.
5749 If ARG is not a kind of expression we can handle, return
5750 NULL_TREE. */
5752 tree
5753 unary_complex_lvalue (enum tree_code code, tree arg)
5755 /* Inside a template, making these kinds of adjustments is
5756 pointless; we are only concerned with the type of the
5757 expression. */
5758 if (processing_template_decl)
5759 return NULL_TREE;
5761 /* Handle (a, b) used as an "lvalue". */
5762 if (TREE_CODE (arg) == COMPOUND_EXPR)
5764 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5765 tf_warning_or_error);
5766 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5767 TREE_OPERAND (arg, 0), real_result);
5770 /* Handle (a ? b : c) used as an "lvalue". */
5771 if (TREE_CODE (arg) == COND_EXPR
5772 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5773 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5775 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5776 if (TREE_CODE (arg) == MODIFY_EXPR
5777 || TREE_CODE (arg) == PREINCREMENT_EXPR
5778 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5780 tree lvalue = TREE_OPERAND (arg, 0);
5781 if (TREE_SIDE_EFFECTS (lvalue))
5783 lvalue = stabilize_reference (lvalue);
5784 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5785 lvalue, TREE_OPERAND (arg, 1));
5787 return unary_complex_lvalue
5788 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5791 if (code != ADDR_EXPR)
5792 return NULL_TREE;
5794 /* Handle (a = b) used as an "lvalue" for `&'. */
5795 if (TREE_CODE (arg) == MODIFY_EXPR
5796 || TREE_CODE (arg) == INIT_EXPR)
5798 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5799 tf_warning_or_error);
5800 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5801 arg, real_result);
5802 TREE_NO_WARNING (arg) = 1;
5803 return arg;
5806 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5807 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5808 || TREE_CODE (arg) == OFFSET_REF)
5809 return NULL_TREE;
5811 /* We permit compiler to make function calls returning
5812 objects of aggregate type look like lvalues. */
5814 tree targ = arg;
5816 if (TREE_CODE (targ) == SAVE_EXPR)
5817 targ = TREE_OPERAND (targ, 0);
5819 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5821 if (TREE_CODE (arg) == SAVE_EXPR)
5822 targ = arg;
5823 else
5824 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5825 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5828 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
5829 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5830 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5833 /* Don't let anything else be handled specially. */
5834 return NULL_TREE;
5837 /* Mark EXP saying that we need to be able to take the
5838 address of it; it should not be allocated in a register.
5839 Value is true if successful.
5841 C++: we do not allow `current_class_ptr' to be addressable. */
5843 bool
5844 cxx_mark_addressable (tree exp)
5846 tree x = exp;
5848 while (1)
5849 switch (TREE_CODE (x))
5851 case ADDR_EXPR:
5852 case COMPONENT_REF:
5853 case ARRAY_REF:
5854 case REALPART_EXPR:
5855 case IMAGPART_EXPR:
5856 x = TREE_OPERAND (x, 0);
5857 break;
5859 case PARM_DECL:
5860 if (x == current_class_ptr)
5862 error ("cannot take the address of %<this%>, which is an rvalue expression");
5863 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5864 return true;
5866 /* Fall through. */
5868 case VAR_DECL:
5869 /* Caller should not be trying to mark initialized
5870 constant fields addressable. */
5871 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5872 || DECL_IN_AGGR_P (x) == 0
5873 || TREE_STATIC (x)
5874 || DECL_EXTERNAL (x));
5875 /* Fall through. */
5877 case RESULT_DECL:
5878 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5879 && !DECL_ARTIFICIAL (x))
5881 if (VAR_P (x) && DECL_HARD_REGISTER (x))
5883 error
5884 ("address of explicit register variable %qD requested", x);
5885 return false;
5887 else if (extra_warnings)
5888 warning
5889 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5891 TREE_ADDRESSABLE (x) = 1;
5892 return true;
5894 case CONST_DECL:
5895 case FUNCTION_DECL:
5896 TREE_ADDRESSABLE (x) = 1;
5897 return true;
5899 case CONSTRUCTOR:
5900 TREE_ADDRESSABLE (x) = 1;
5901 return true;
5903 case TARGET_EXPR:
5904 TREE_ADDRESSABLE (x) = 1;
5905 cxx_mark_addressable (TREE_OPERAND (x, 0));
5906 return true;
5908 default:
5909 return true;
5913 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5915 tree
5916 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
5917 tsubst_flags_t complain)
5919 tree orig_ifexp = ifexp;
5920 tree orig_op1 = op1;
5921 tree orig_op2 = op2;
5922 tree expr;
5924 if (processing_template_decl)
5926 /* The standard says that the expression is type-dependent if
5927 IFEXP is type-dependent, even though the eventual type of the
5928 expression doesn't dependent on IFEXP. */
5929 if (type_dependent_expression_p (ifexp)
5930 /* As a GNU extension, the middle operand may be omitted. */
5931 || (op1 && type_dependent_expression_p (op1))
5932 || type_dependent_expression_p (op2))
5933 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
5934 ifexp = build_non_dependent_expr (ifexp);
5935 if (op1)
5936 op1 = build_non_dependent_expr (op1);
5937 op2 = build_non_dependent_expr (op2);
5940 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
5941 if (processing_template_decl && expr != error_mark_node
5942 && TREE_CODE (expr) != VEC_COND_EXPR)
5944 tree min = build_min_non_dep (COND_EXPR, expr,
5945 orig_ifexp, orig_op1, orig_op2);
5946 /* In C++11, remember that the result is an lvalue or xvalue.
5947 In C++98, lvalue_kind can just assume lvalue in a template. */
5948 if (cxx_dialect >= cxx11
5949 && lvalue_or_rvalue_with_address_p (expr)
5950 && !lvalue_or_rvalue_with_address_p (min))
5951 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5952 !real_lvalue_p (expr));
5953 expr = convert_from_reference (min);
5955 return expr;
5958 /* Given a list of expressions, return a compound expression
5959 that performs them all and returns the value of the last of them. */
5961 tree
5962 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5963 tsubst_flags_t complain)
5965 tree expr = TREE_VALUE (list);
5967 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5968 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5970 if (complain & tf_error)
5971 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5972 "non-class type must not be parenthesized");
5973 else
5974 return error_mark_node;
5977 if (TREE_CHAIN (list))
5979 if (complain & tf_error)
5980 switch (exp)
5982 case ELK_INIT:
5983 permerror (input_location, "expression list treated as compound "
5984 "expression in initializer");
5985 break;
5986 case ELK_MEM_INIT:
5987 permerror (input_location, "expression list treated as compound "
5988 "expression in mem-initializer");
5989 break;
5990 case ELK_FUNC_CAST:
5991 permerror (input_location, "expression list treated as compound "
5992 "expression in functional cast");
5993 break;
5994 default:
5995 gcc_unreachable ();
5997 else
5998 return error_mark_node;
6000 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6001 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6002 expr, TREE_VALUE (list), complain);
6005 return expr;
6008 /* Like build_x_compound_expr_from_list, but using a VEC. */
6010 tree
6011 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6012 tsubst_flags_t complain)
6014 if (vec_safe_is_empty (vec))
6015 return NULL_TREE;
6016 else if (vec->length () == 1)
6017 return (*vec)[0];
6018 else
6020 tree expr;
6021 unsigned int ix;
6022 tree t;
6024 if (msg != NULL)
6026 if (complain & tf_error)
6027 permerror (input_location,
6028 "%s expression list treated as compound expression",
6029 msg);
6030 else
6031 return error_mark_node;
6034 expr = (*vec)[0];
6035 for (ix = 1; vec->iterate (ix, &t); ++ix)
6036 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6037 t, complain);
6039 return expr;
6043 /* Handle overloading of the ',' operator when needed. */
6045 tree
6046 build_x_compound_expr (location_t loc, tree op1, tree op2,
6047 tsubst_flags_t complain)
6049 tree result;
6050 tree orig_op1 = op1;
6051 tree orig_op2 = op2;
6053 if (processing_template_decl)
6055 if (type_dependent_expression_p (op1)
6056 || type_dependent_expression_p (op2))
6057 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6058 op1 = build_non_dependent_expr (op1);
6059 op2 = build_non_dependent_expr (op2);
6062 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6063 NULL_TREE, /*overload=*/NULL, complain);
6064 if (!result)
6065 result = cp_build_compound_expr (op1, op2, complain);
6067 if (processing_template_decl && result != error_mark_node)
6068 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6070 return result;
6073 /* Like cp_build_compound_expr, but for the c-common bits. */
6075 tree
6076 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6078 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6081 /* Build a compound expression. */
6083 tree
6084 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6086 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6088 if (lhs == error_mark_node || rhs == error_mark_node)
6089 return error_mark_node;
6091 if (TREE_CODE (rhs) == TARGET_EXPR)
6093 /* If the rhs is a TARGET_EXPR, then build the compound
6094 expression inside the target_expr's initializer. This
6095 helps the compiler to eliminate unnecessary temporaries. */
6096 tree init = TREE_OPERAND (rhs, 1);
6098 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6099 TREE_OPERAND (rhs, 1) = init;
6101 return rhs;
6104 if (type_unknown_p (rhs))
6106 if (complain & tf_error)
6107 error ("no context to resolve type of %qE", rhs);
6108 return error_mark_node;
6111 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6114 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6115 casts away constness. CAST gives the type of cast. Returns true
6116 if the cast is ill-formed, false if it is well-formed.
6118 ??? This function warns for casting away any qualifier not just
6119 const. We would like to specify exactly what qualifiers are casted
6120 away.
6123 static bool
6124 check_for_casting_away_constness (tree src_type, tree dest_type,
6125 enum tree_code cast, tsubst_flags_t complain)
6127 /* C-style casts are allowed to cast away constness. With
6128 WARN_CAST_QUAL, we still want to issue a warning. */
6129 if (cast == CAST_EXPR && !warn_cast_qual)
6130 return false;
6132 if (!casts_away_constness (src_type, dest_type, complain))
6133 return false;
6135 switch (cast)
6137 case CAST_EXPR:
6138 if (complain & tf_warning)
6139 warning (OPT_Wcast_qual,
6140 "cast from type %qT to type %qT casts away qualifiers",
6141 src_type, dest_type);
6142 return false;
6144 case STATIC_CAST_EXPR:
6145 if (complain & tf_error)
6146 error ("static_cast from type %qT to type %qT casts away qualifiers",
6147 src_type, dest_type);
6148 return true;
6150 case REINTERPRET_CAST_EXPR:
6151 if (complain & tf_error)
6152 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6153 src_type, dest_type);
6154 return true;
6156 default:
6157 gcc_unreachable();
6162 Warns if the cast from expression EXPR to type TYPE is useless.
6164 void
6165 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6167 if (warn_useless_cast
6168 && complain & tf_warning
6169 && c_inhibit_evaluation_warnings == 0)
6171 if (REFERENCE_REF_P (expr))
6172 expr = TREE_OPERAND (expr, 0);
6174 if ((TREE_CODE (type) == REFERENCE_TYPE
6175 && (TYPE_REF_IS_RVALUE (type)
6176 ? xvalue_p (expr) : real_lvalue_p (expr))
6177 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6178 || same_type_p (TREE_TYPE (expr), type))
6179 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6183 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6184 (another pointer-to-member type in the same hierarchy) and return
6185 the converted expression. If ALLOW_INVERSE_P is permitted, a
6186 pointer-to-derived may be converted to pointer-to-base; otherwise,
6187 only the other direction is permitted. If C_CAST_P is true, this
6188 conversion is taking place as part of a C-style cast. */
6190 tree
6191 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6192 bool c_cast_p, tsubst_flags_t complain)
6194 if (TYPE_PTRDATAMEM_P (type))
6196 tree delta;
6198 if (TREE_CODE (expr) == PTRMEM_CST)
6199 expr = cplus_expand_constant (expr);
6200 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6201 TYPE_PTRMEM_CLASS_TYPE (type),
6202 allow_inverse_p,
6203 c_cast_p, complain);
6204 if (delta == error_mark_node)
6205 return error_mark_node;
6207 if (!integer_zerop (delta))
6209 tree cond, op1, op2;
6211 cond = cp_build_binary_op (input_location,
6212 EQ_EXPR,
6213 expr,
6214 build_int_cst (TREE_TYPE (expr), -1),
6215 complain);
6216 op1 = build_nop (ptrdiff_type_node, expr);
6217 op2 = cp_build_binary_op (input_location,
6218 PLUS_EXPR, op1, delta,
6219 complain);
6221 expr = fold_build3_loc (input_location,
6222 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6226 return build_nop (type, expr);
6228 else
6229 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6230 allow_inverse_p, c_cast_p, complain);
6233 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6234 this static_cast is being attempted as one of the possible casts
6235 allowed by a C-style cast. (In that case, accessibility of base
6236 classes is not considered, and it is OK to cast away
6237 constness.) Return the result of the cast. *VALID_P is set to
6238 indicate whether or not the cast was valid. */
6240 static tree
6241 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6242 bool *valid_p, tsubst_flags_t complain)
6244 tree intype;
6245 tree result;
6246 cp_lvalue_kind clk;
6248 /* Assume the cast is valid. */
6249 *valid_p = true;
6251 intype = unlowered_expr_type (expr);
6253 /* Save casted types in the function's used types hash table. */
6254 used_types_insert (type);
6256 /* [expr.static.cast]
6258 An lvalue of type "cv1 B", where B is a class type, can be cast
6259 to type "reference to cv2 D", where D is a class derived (clause
6260 _class.derived_) from B, if a valid standard conversion from
6261 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6262 same cv-qualification as, or greater cv-qualification than, cv1,
6263 and B is not a virtual base class of D. */
6264 /* We check this case before checking the validity of "TYPE t =
6265 EXPR;" below because for this case:
6267 struct B {};
6268 struct D : public B { D(const B&); };
6269 extern B& b;
6270 void f() { static_cast<const D&>(b); }
6272 we want to avoid constructing a new D. The standard is not
6273 completely clear about this issue, but our interpretation is
6274 consistent with other compilers. */
6275 if (TREE_CODE (type) == REFERENCE_TYPE
6276 && CLASS_TYPE_P (TREE_TYPE (type))
6277 && CLASS_TYPE_P (intype)
6278 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6279 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6280 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6281 build_pointer_type (TYPE_MAIN_VARIANT
6282 (TREE_TYPE (type))),
6283 complain)
6284 && (c_cast_p
6285 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6287 tree base;
6289 /* There is a standard conversion from "D*" to "B*" even if "B"
6290 is ambiguous or inaccessible. If this is really a
6291 static_cast, then we check both for inaccessibility and
6292 ambiguity. However, if this is a static_cast being performed
6293 because the user wrote a C-style cast, then accessibility is
6294 not considered. */
6295 base = lookup_base (TREE_TYPE (type), intype,
6296 c_cast_p ? ba_unique : ba_check,
6297 NULL, complain);
6299 /* Convert from "B*" to "D*". This function will check that "B"
6300 is not a virtual base of "D". */
6301 expr = build_base_path (MINUS_EXPR, build_address (expr),
6302 base, /*nonnull=*/false, complain);
6303 /* Convert the pointer to a reference -- but then remember that
6304 there are no expressions with reference type in C++.
6306 We call rvalue so that there's an actual tree code
6307 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6308 is a variable with the same type, the conversion would get folded
6309 away, leaving just the variable and causing lvalue_kind to give
6310 the wrong answer. */
6311 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6314 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6315 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6316 if (TREE_CODE (type) == REFERENCE_TYPE
6317 && TYPE_REF_IS_RVALUE (type)
6318 && (clk = real_lvalue_p (expr))
6319 && reference_related_p (TREE_TYPE (type), intype)
6320 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6322 if (clk == clk_ordinary)
6324 /* Handle the (non-bit-field) lvalue case here by casting to
6325 lvalue reference and then changing it to an rvalue reference.
6326 Casting an xvalue to rvalue reference will be handled by the
6327 main code path. */
6328 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6329 result = (perform_direct_initialization_if_possible
6330 (lref, expr, c_cast_p, complain));
6331 result = cp_fold_convert (type, result);
6332 /* Make sure we don't fold back down to a named rvalue reference,
6333 because that would be an lvalue. */
6334 if (DECL_P (result))
6335 result = build1 (NON_LVALUE_EXPR, type, result);
6336 return convert_from_reference (result);
6338 else
6339 /* For a bit-field or packed field, bind to a temporary. */
6340 expr = rvalue (expr);
6343 /* Resolve overloaded address here rather than once in
6344 implicit_conversion and again in the inverse code below. */
6345 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6347 expr = instantiate_type (type, expr, complain);
6348 intype = TREE_TYPE (expr);
6351 /* [expr.static.cast]
6353 Any expression can be explicitly converted to type cv void. */
6354 if (VOID_TYPE_P (type))
6355 return convert_to_void (expr, ICV_CAST, complain);
6357 /* [class.abstract]
6358 An abstract class shall not be used ... as the type of an explicit
6359 conversion. */
6360 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6361 return error_mark_node;
6363 /* [expr.static.cast]
6365 An expression e can be explicitly converted to a type T using a
6366 static_cast of the form static_cast<T>(e) if the declaration T
6367 t(e);" is well-formed, for some invented temporary variable
6368 t. */
6369 result = perform_direct_initialization_if_possible (type, expr,
6370 c_cast_p, complain);
6371 if (result)
6373 result = convert_from_reference (result);
6375 /* [expr.static.cast]
6377 If T is a reference type, the result is an lvalue; otherwise,
6378 the result is an rvalue. */
6379 if (TREE_CODE (type) != REFERENCE_TYPE)
6380 result = rvalue (result);
6381 return result;
6384 /* [expr.static.cast]
6386 The inverse of any standard conversion sequence (clause _conv_),
6387 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6388 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6389 (_conv.bool_) conversions, can be performed explicitly using
6390 static_cast subject to the restriction that the explicit
6391 conversion does not cast away constness (_expr.const.cast_), and
6392 the following additional rules for specific cases: */
6393 /* For reference, the conversions not excluded are: integral
6394 promotions, floating point promotion, integral conversions,
6395 floating point conversions, floating-integral conversions,
6396 pointer conversions, and pointer to member conversions. */
6397 /* DR 128
6399 A value of integral _or enumeration_ type can be explicitly
6400 converted to an enumeration type. */
6401 /* The effect of all that is that any conversion between any two
6402 types which are integral, floating, or enumeration types can be
6403 performed. */
6404 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6405 || SCALAR_FLOAT_TYPE_P (type))
6406 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6407 || SCALAR_FLOAT_TYPE_P (intype)))
6408 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6410 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6411 && CLASS_TYPE_P (TREE_TYPE (type))
6412 && CLASS_TYPE_P (TREE_TYPE (intype))
6413 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6414 (TREE_TYPE (intype))),
6415 build_pointer_type (TYPE_MAIN_VARIANT
6416 (TREE_TYPE (type))),
6417 complain))
6419 tree base;
6421 if (!c_cast_p
6422 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6423 complain))
6424 return error_mark_node;
6425 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6426 c_cast_p ? ba_unique : ba_check,
6427 NULL, complain);
6428 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6429 complain);
6430 return cp_fold_convert(type, expr);
6433 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6434 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6436 tree c1;
6437 tree c2;
6438 tree t1;
6439 tree t2;
6441 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6442 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6444 if (TYPE_PTRDATAMEM_P (type))
6446 t1 = (build_ptrmem_type
6447 (c1,
6448 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6449 t2 = (build_ptrmem_type
6450 (c2,
6451 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6453 else
6455 t1 = intype;
6456 t2 = type;
6458 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6460 if (!c_cast_p
6461 && check_for_casting_away_constness (intype, type,
6462 STATIC_CAST_EXPR,
6463 complain))
6464 return error_mark_node;
6465 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6466 c_cast_p, complain);
6470 /* [expr.static.cast]
6472 An rvalue of type "pointer to cv void" can be explicitly
6473 converted to a pointer to object type. A value of type pointer
6474 to object converted to "pointer to cv void" and back to the
6475 original pointer type will have its original value. */
6476 if (TYPE_PTR_P (intype)
6477 && VOID_TYPE_P (TREE_TYPE (intype))
6478 && TYPE_PTROB_P (type))
6480 if (!c_cast_p
6481 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6482 complain))
6483 return error_mark_node;
6484 return build_nop (type, expr);
6487 *valid_p = false;
6488 return error_mark_node;
6491 /* Return an expression representing static_cast<TYPE>(EXPR). */
6493 tree
6494 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6496 tree result;
6497 bool valid_p;
6499 if (type == error_mark_node || expr == error_mark_node)
6500 return error_mark_node;
6502 if (processing_template_decl)
6504 expr = build_min (STATIC_CAST_EXPR, type, expr);
6505 /* We don't know if it will or will not have side effects. */
6506 TREE_SIDE_EFFECTS (expr) = 1;
6507 return convert_from_reference (expr);
6510 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6511 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6512 if (TREE_CODE (type) != REFERENCE_TYPE
6513 && TREE_CODE (expr) == NOP_EXPR
6514 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6515 expr = TREE_OPERAND (expr, 0);
6517 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6518 complain);
6519 if (valid_p)
6521 if (result != error_mark_node)
6522 maybe_warn_about_useless_cast (type, expr, complain);
6523 return result;
6526 if (complain & tf_error)
6527 error ("invalid static_cast from type %qT to type %qT",
6528 TREE_TYPE (expr), type);
6529 return error_mark_node;
6532 /* EXPR is an expression with member function or pointer-to-member
6533 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6534 not permitted by ISO C++, but we accept it in some modes. If we
6535 are not in one of those modes, issue a diagnostic. Return the
6536 converted expression. */
6538 tree
6539 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6541 tree intype;
6542 tree decl;
6544 intype = TREE_TYPE (expr);
6545 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6546 || TREE_CODE (intype) == METHOD_TYPE);
6548 if (!(complain & tf_warning_or_error))
6549 return error_mark_node;
6551 if (pedantic || warn_pmf2ptr)
6552 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6553 "converting from %qT to %qT", intype, type);
6555 if (TREE_CODE (intype) == METHOD_TYPE)
6556 expr = build_addr_func (expr, complain);
6557 else if (TREE_CODE (expr) == PTRMEM_CST)
6558 expr = build_address (PTRMEM_CST_MEMBER (expr));
6559 else
6561 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6562 decl = build_address (decl);
6563 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6566 if (expr == error_mark_node)
6567 return error_mark_node;
6569 return build_nop (type, expr);
6572 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6573 If C_CAST_P is true, this reinterpret cast is being done as part of
6574 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6575 indicate whether or not reinterpret_cast was valid. */
6577 static tree
6578 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6579 bool *valid_p, tsubst_flags_t complain)
6581 tree intype;
6583 /* Assume the cast is invalid. */
6584 if (valid_p)
6585 *valid_p = true;
6587 if (type == error_mark_node || error_operand_p (expr))
6588 return error_mark_node;
6590 intype = TREE_TYPE (expr);
6592 /* Save casted types in the function's used types hash table. */
6593 used_types_insert (type);
6595 /* [expr.reinterpret.cast]
6596 An lvalue expression of type T1 can be cast to the type
6597 "reference to T2" if an expression of type "pointer to T1" can be
6598 explicitly converted to the type "pointer to T2" using a
6599 reinterpret_cast. */
6600 if (TREE_CODE (type) == REFERENCE_TYPE)
6602 if (! real_lvalue_p (expr))
6604 if (complain & tf_error)
6605 error ("invalid cast of an rvalue expression of type "
6606 "%qT to type %qT",
6607 intype, type);
6608 return error_mark_node;
6611 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6612 "B" are related class types; the reinterpret_cast does not
6613 adjust the pointer. */
6614 if (TYPE_PTR_P (intype)
6615 && (complain & tf_warning)
6616 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6617 COMPARE_BASE | COMPARE_DERIVED)))
6618 warning (0, "casting %qT to %qT does not dereference pointer",
6619 intype, type);
6621 expr = cp_build_addr_expr (expr, complain);
6623 if (warn_strict_aliasing > 2)
6624 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6626 if (expr != error_mark_node)
6627 expr = build_reinterpret_cast_1
6628 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6629 valid_p, complain);
6630 if (expr != error_mark_node)
6631 /* cp_build_indirect_ref isn't right for rvalue refs. */
6632 expr = convert_from_reference (fold_convert (type, expr));
6633 return expr;
6636 /* As a G++ extension, we consider conversions from member
6637 functions, and pointers to member functions to
6638 pointer-to-function and pointer-to-void types. If
6639 -Wno-pmf-conversions has not been specified,
6640 convert_member_func_to_ptr will issue an error message. */
6641 if ((TYPE_PTRMEMFUNC_P (intype)
6642 || TREE_CODE (intype) == METHOD_TYPE)
6643 && TYPE_PTR_P (type)
6644 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6645 || VOID_TYPE_P (TREE_TYPE (type))))
6646 return convert_member_func_to_ptr (type, expr, complain);
6648 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6649 array-to-pointer, and function-to-pointer conversions are
6650 performed. */
6651 expr = decay_conversion (expr, complain);
6653 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6654 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6655 if (TREE_CODE (expr) == NOP_EXPR
6656 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6657 expr = TREE_OPERAND (expr, 0);
6659 if (error_operand_p (expr))
6660 return error_mark_node;
6662 intype = TREE_TYPE (expr);
6664 /* [expr.reinterpret.cast]
6665 A pointer can be converted to any integral type large enough to
6666 hold it. ... A value of type std::nullptr_t can be converted to
6667 an integral type; the conversion has the same meaning and
6668 validity as a conversion of (void*)0 to the integral type. */
6669 if (CP_INTEGRAL_TYPE_P (type)
6670 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6672 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6674 if (complain & tf_error)
6675 permerror (input_location, "cast from %qT to %qT loses precision",
6676 intype, type);
6677 else
6678 return error_mark_node;
6680 if (NULLPTR_TYPE_P (intype))
6681 return build_int_cst (type, 0);
6683 /* [expr.reinterpret.cast]
6684 A value of integral or enumeration type can be explicitly
6685 converted to a pointer. */
6686 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6687 /* OK */
6689 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6690 || TYPE_PTR_OR_PTRMEM_P (type))
6691 && same_type_p (type, intype))
6692 /* DR 799 */
6693 return fold_if_not_in_template (build_nop (type, expr));
6694 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6695 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6696 return fold_if_not_in_template (build_nop (type, expr));
6697 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6698 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6700 tree sexpr = expr;
6702 if (!c_cast_p
6703 && check_for_casting_away_constness (intype, type,
6704 REINTERPRET_CAST_EXPR,
6705 complain))
6706 return error_mark_node;
6707 /* Warn about possible alignment problems. */
6708 if (STRICT_ALIGNMENT && warn_cast_align
6709 && (complain & tf_warning)
6710 && !VOID_TYPE_P (type)
6711 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6712 && COMPLETE_TYPE_P (TREE_TYPE (type))
6713 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6714 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6715 warning (OPT_Wcast_align, "cast from %qT to %qT "
6716 "increases required alignment of target type", intype, type);
6718 /* We need to strip nops here, because the front end likes to
6719 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6720 STRIP_NOPS (sexpr);
6721 if (warn_strict_aliasing <= 2)
6722 strict_aliasing_warning (intype, type, sexpr);
6724 return fold_if_not_in_template (build_nop (type, expr));
6726 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6727 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6729 if (pedantic && (complain & tf_warning))
6730 /* Only issue a warning, as we have always supported this
6731 where possible, and it is necessary in some cases. DR 195
6732 addresses this issue, but as of 2004/10/26 is still in
6733 drafting. */
6734 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6735 return fold_if_not_in_template (build_nop (type, expr));
6737 else if (TREE_CODE (type) == VECTOR_TYPE)
6738 return fold_if_not_in_template (convert_to_vector (type, expr));
6739 else if (TREE_CODE (intype) == VECTOR_TYPE
6740 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6741 return fold_if_not_in_template (convert_to_integer (type, expr));
6742 else
6744 if (valid_p)
6745 *valid_p = false;
6746 if (complain & tf_error)
6747 error ("invalid cast from type %qT to type %qT", intype, type);
6748 return error_mark_node;
6751 return cp_convert (type, expr, complain);
6754 tree
6755 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6757 tree r;
6759 if (type == error_mark_node || expr == error_mark_node)
6760 return error_mark_node;
6762 if (processing_template_decl)
6764 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6766 if (!TREE_SIDE_EFFECTS (t)
6767 && type_dependent_expression_p (expr))
6768 /* There might turn out to be side effects inside expr. */
6769 TREE_SIDE_EFFECTS (t) = 1;
6770 return convert_from_reference (t);
6773 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6774 /*valid_p=*/NULL, complain);
6775 if (r != error_mark_node)
6776 maybe_warn_about_useless_cast (type, expr, complain);
6777 return r;
6780 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6781 return an appropriate expression. Otherwise, return
6782 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6783 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6784 performing a C-style cast, its value upon return will indicate
6785 whether or not the conversion succeeded. */
6787 static tree
6788 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6789 bool *valid_p)
6791 tree src_type;
6792 tree reference_type;
6794 /* Callers are responsible for handling error_mark_node as a
6795 destination type. */
6796 gcc_assert (dst_type != error_mark_node);
6797 /* In a template, callers should be building syntactic
6798 representations of casts, not using this machinery. */
6799 gcc_assert (!processing_template_decl);
6801 /* Assume the conversion is invalid. */
6802 if (valid_p)
6803 *valid_p = false;
6805 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6807 if (complain & tf_error)
6808 error ("invalid use of const_cast with type %qT, "
6809 "which is not a pointer, "
6810 "reference, nor a pointer-to-data-member type", dst_type);
6811 return error_mark_node;
6814 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6816 if (complain & tf_error)
6817 error ("invalid use of const_cast with type %qT, which is a pointer "
6818 "or reference to a function type", dst_type);
6819 return error_mark_node;
6822 /* Save casted types in the function's used types hash table. */
6823 used_types_insert (dst_type);
6825 src_type = TREE_TYPE (expr);
6826 /* Expressions do not really have reference types. */
6827 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6828 src_type = TREE_TYPE (src_type);
6830 /* [expr.const.cast]
6832 For two object types T1 and T2, if a pointer to T1 can be explicitly
6833 converted to the type "pointer to T2" using a const_cast, then the
6834 following conversions can also be made:
6836 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6837 type T2 using the cast const_cast<T2&>;
6839 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6840 type T2 using the cast const_cast<T2&&>; and
6842 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6843 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6845 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6847 reference_type = dst_type;
6848 if (!TYPE_REF_IS_RVALUE (dst_type)
6849 ? real_lvalue_p (expr)
6850 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6851 ? lvalue_p (expr)
6852 : lvalue_or_rvalue_with_address_p (expr)))
6853 /* OK. */;
6854 else
6856 if (complain & tf_error)
6857 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6858 src_type, dst_type);
6859 return error_mark_node;
6861 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6862 src_type = build_pointer_type (src_type);
6864 else
6866 reference_type = NULL_TREE;
6867 /* If the destination type is not a reference type, the
6868 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6869 conversions are performed. */
6870 src_type = type_decays_to (src_type);
6871 if (src_type == error_mark_node)
6872 return error_mark_node;
6875 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
6877 if (comp_ptr_ttypes_const (dst_type, src_type))
6879 if (valid_p)
6881 *valid_p = true;
6882 /* This cast is actually a C-style cast. Issue a warning if
6883 the user is making a potentially unsafe cast. */
6884 check_for_casting_away_constness (src_type, dst_type,
6885 CAST_EXPR, complain);
6887 if (reference_type)
6889 expr = cp_build_addr_expr (expr, complain);
6890 if (expr == error_mark_node)
6891 return error_mark_node;
6892 expr = build_nop (reference_type, expr);
6893 return convert_from_reference (expr);
6895 else
6897 expr = decay_conversion (expr, complain);
6898 if (expr == error_mark_node)
6899 return error_mark_node;
6901 /* build_c_cast puts on a NOP_EXPR to make the result not an
6902 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6903 non-lvalue context. */
6904 if (TREE_CODE (expr) == NOP_EXPR
6905 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6906 expr = TREE_OPERAND (expr, 0);
6907 return build_nop (dst_type, expr);
6910 else if (valid_p
6911 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6912 TREE_TYPE (src_type)))
6913 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6914 complain);
6917 if (complain & tf_error)
6918 error ("invalid const_cast from type %qT to type %qT",
6919 src_type, dst_type);
6920 return error_mark_node;
6923 tree
6924 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6926 tree r;
6928 if (type == error_mark_node || error_operand_p (expr))
6929 return error_mark_node;
6931 if (processing_template_decl)
6933 tree t = build_min (CONST_CAST_EXPR, type, expr);
6935 if (!TREE_SIDE_EFFECTS (t)
6936 && type_dependent_expression_p (expr))
6937 /* There might turn out to be side effects inside expr. */
6938 TREE_SIDE_EFFECTS (t) = 1;
6939 return convert_from_reference (t);
6942 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
6943 if (r != error_mark_node)
6944 maybe_warn_about_useless_cast (type, expr, complain);
6945 return r;
6948 /* Like cp_build_c_cast, but for the c-common bits. */
6950 tree
6951 build_c_cast (location_t /*loc*/, tree type, tree expr)
6953 return cp_build_c_cast (type, expr, tf_warning_or_error);
6956 /* Build an expression representing an explicit C-style cast to type
6957 TYPE of expression EXPR. */
6959 tree
6960 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6962 tree value = expr;
6963 tree result;
6964 bool valid_p;
6966 if (type == error_mark_node || error_operand_p (expr))
6967 return error_mark_node;
6969 if (processing_template_decl)
6971 tree t = build_min (CAST_EXPR, type,
6972 tree_cons (NULL_TREE, value, NULL_TREE));
6973 /* We don't know if it will or will not have side effects. */
6974 TREE_SIDE_EFFECTS (t) = 1;
6975 return convert_from_reference (t);
6978 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6979 'Class') should always be retained, because this information aids
6980 in method lookup. */
6981 if (objc_is_object_ptr (type)
6982 && objc_is_object_ptr (TREE_TYPE (expr)))
6983 return build_nop (type, expr);
6985 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6986 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6987 if (TREE_CODE (type) != REFERENCE_TYPE
6988 && TREE_CODE (value) == NOP_EXPR
6989 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6990 value = TREE_OPERAND (value, 0);
6992 if (TREE_CODE (type) == ARRAY_TYPE)
6994 /* Allow casting from T1* to T2[] because Cfront allows it.
6995 NIHCL uses it. It is not valid ISO C++ however. */
6996 if (TYPE_PTR_P (TREE_TYPE (expr)))
6998 if (complain & tf_error)
6999 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7000 else
7001 return error_mark_node;
7002 type = build_pointer_type (TREE_TYPE (type));
7004 else
7006 if (complain & tf_error)
7007 error ("ISO C++ forbids casting to an array type %qT", type);
7008 return error_mark_node;
7012 if (TREE_CODE (type) == FUNCTION_TYPE
7013 || TREE_CODE (type) == METHOD_TYPE)
7015 if (complain & tf_error)
7016 error ("invalid cast to function type %qT", type);
7017 return error_mark_node;
7020 if (TYPE_PTR_P (type)
7021 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7022 /* Casting to an integer of smaller size is an error detected elsewhere. */
7023 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7024 /* Don't warn about converting any constant. */
7025 && !TREE_CONSTANT (value))
7026 warning_at (input_location, OPT_Wint_to_pointer_cast,
7027 "cast to pointer from integer of different size");
7029 /* A C-style cast can be a const_cast. */
7030 result = build_const_cast_1 (type, value, complain & tf_warning,
7031 &valid_p);
7032 if (valid_p)
7034 if (result != error_mark_node)
7035 maybe_warn_about_useless_cast (type, value, complain);
7036 return result;
7039 /* Or a static cast. */
7040 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7041 &valid_p, complain);
7042 /* Or a reinterpret_cast. */
7043 if (!valid_p)
7044 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7045 &valid_p, complain);
7046 /* The static_cast or reinterpret_cast may be followed by a
7047 const_cast. */
7048 if (valid_p
7049 /* A valid cast may result in errors if, for example, a
7050 conversion to an ambiguous base class is required. */
7051 && !error_operand_p (result))
7053 tree result_type;
7055 maybe_warn_about_useless_cast (type, value, complain);
7057 /* Non-class rvalues always have cv-unqualified type. */
7058 if (!CLASS_TYPE_P (type))
7059 type = TYPE_MAIN_VARIANT (type);
7060 result_type = TREE_TYPE (result);
7061 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7062 result_type = TYPE_MAIN_VARIANT (result_type);
7063 /* If the type of RESULT does not match TYPE, perform a
7064 const_cast to make it match. If the static_cast or
7065 reinterpret_cast succeeded, we will differ by at most
7066 cv-qualification, so the follow-on const_cast is guaranteed
7067 to succeed. */
7068 if (!same_type_p (non_reference (type), non_reference (result_type)))
7070 result = build_const_cast_1 (type, result, false, &valid_p);
7071 gcc_assert (valid_p);
7073 return result;
7076 return error_mark_node;
7079 /* For use from the C common bits. */
7080 tree
7081 build_modify_expr (location_t /*location*/,
7082 tree lhs, tree /*lhs_origtype*/,
7083 enum tree_code modifycode,
7084 location_t /*rhs_location*/, tree rhs,
7085 tree /*rhs_origtype*/)
7087 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7090 /* Build an assignment expression of lvalue LHS from value RHS.
7091 MODIFYCODE is the code for a binary operator that we use
7092 to combine the old value of LHS with RHS to get the new value.
7093 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7095 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7097 tree
7098 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7099 tsubst_flags_t complain)
7101 tree result;
7102 tree newrhs = rhs;
7103 tree lhstype = TREE_TYPE (lhs);
7104 tree olhstype = lhstype;
7105 bool plain_assign = (modifycode == NOP_EXPR);
7107 /* Avoid duplicate error messages from operands that had errors. */
7108 if (error_operand_p (lhs) || error_operand_p (rhs))
7109 return error_mark_node;
7111 /* Handle control structure constructs used as "lvalues". */
7112 switch (TREE_CODE (lhs))
7114 /* Handle --foo = 5; as these are valid constructs in C++. */
7115 case PREDECREMENT_EXPR:
7116 case PREINCREMENT_EXPR:
7117 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7118 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7119 stabilize_reference (TREE_OPERAND (lhs, 0)),
7120 TREE_OPERAND (lhs, 1));
7121 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7122 modifycode, rhs, complain);
7123 if (newrhs == error_mark_node)
7124 return error_mark_node;
7125 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7127 /* Handle (a, b) used as an "lvalue". */
7128 case COMPOUND_EXPR:
7129 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7130 modifycode, rhs, complain);
7131 if (newrhs == error_mark_node)
7132 return error_mark_node;
7133 return build2 (COMPOUND_EXPR, lhstype,
7134 TREE_OPERAND (lhs, 0), newrhs);
7136 case MODIFY_EXPR:
7137 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7138 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7139 stabilize_reference (TREE_OPERAND (lhs, 0)),
7140 TREE_OPERAND (lhs, 1));
7141 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7142 complain);
7143 if (newrhs == error_mark_node)
7144 return error_mark_node;
7145 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7147 case MIN_EXPR:
7148 case MAX_EXPR:
7149 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7150 when neither operand has side-effects. */
7151 if (!lvalue_or_else (lhs, lv_assign, complain))
7152 return error_mark_node;
7154 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7155 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7157 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7158 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7159 boolean_type_node,
7160 TREE_OPERAND (lhs, 0),
7161 TREE_OPERAND (lhs, 1)),
7162 TREE_OPERAND (lhs, 0),
7163 TREE_OPERAND (lhs, 1));
7164 /* Fall through. */
7166 /* Handle (a ? b : c) used as an "lvalue". */
7167 case COND_EXPR:
7169 /* Produce (a ? (b = rhs) : (c = rhs))
7170 except that the RHS goes through a save-expr
7171 so the code to compute it is only emitted once. */
7172 tree cond;
7173 tree preeval = NULL_TREE;
7175 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7177 if (complain & tf_error)
7178 error ("void value not ignored as it ought to be");
7179 return error_mark_node;
7182 rhs = stabilize_expr (rhs, &preeval);
7184 /* Check this here to avoid odd errors when trying to convert
7185 a throw to the type of the COND_EXPR. */
7186 if (!lvalue_or_else (lhs, lv_assign, complain))
7187 return error_mark_node;
7189 cond = build_conditional_expr
7190 (input_location, TREE_OPERAND (lhs, 0),
7191 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7192 modifycode, rhs, complain),
7193 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7194 modifycode, rhs, complain),
7195 complain);
7197 if (cond == error_mark_node)
7198 return cond;
7199 /* Make sure the code to compute the rhs comes out
7200 before the split. */
7201 if (preeval)
7202 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7203 return cond;
7206 default:
7207 break;
7210 if (modifycode == INIT_EXPR)
7212 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7213 /* Do the default thing. */;
7214 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7216 /* Compound literal. */
7217 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7218 /* Call convert to generate an error; see PR 11063. */
7219 rhs = convert (lhstype, rhs);
7220 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7221 TREE_SIDE_EFFECTS (result) = 1;
7222 return result;
7224 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7225 /* Do the default thing. */;
7226 else
7228 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7229 result = build_special_member_call (lhs, complete_ctor_identifier,
7230 &rhs_vec, lhstype, LOOKUP_NORMAL,
7231 complain);
7232 release_tree_vector (rhs_vec);
7233 if (result == NULL_TREE)
7234 return error_mark_node;
7235 return result;
7238 else
7240 lhs = require_complete_type_sfinae (lhs, complain);
7241 if (lhs == error_mark_node)
7242 return error_mark_node;
7244 if (modifycode == NOP_EXPR)
7246 if (c_dialect_objc ())
7248 result = objc_maybe_build_modify_expr (lhs, rhs);
7249 if (result)
7250 return result;
7253 /* `operator=' is not an inheritable operator. */
7254 if (! MAYBE_CLASS_TYPE_P (lhstype))
7255 /* Do the default thing. */;
7256 else
7258 result = build_new_op (input_location, MODIFY_EXPR,
7259 LOOKUP_NORMAL, lhs, rhs,
7260 make_node (NOP_EXPR), /*overload=*/NULL,
7261 complain);
7262 if (result == NULL_TREE)
7263 return error_mark_node;
7264 return result;
7266 lhstype = olhstype;
7268 else
7270 tree init = NULL_TREE;
7272 /* A binary op has been requested. Combine the old LHS
7273 value with the RHS producing the value we should actually
7274 store into the LHS. */
7275 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7276 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7277 || MAYBE_CLASS_TYPE_P (lhstype)));
7279 /* Preevaluate the RHS to make sure its evaluation is complete
7280 before the lvalue-to-rvalue conversion of the LHS:
7282 [expr.ass] With respect to an indeterminately-sequenced
7283 function call, the operation of a compound assignment is a
7284 single evaluation. [ Note: Therefore, a function call shall
7285 not intervene between the lvalue-to-rvalue conversion and the
7286 side effect associated with any single compound assignment
7287 operator. -- end note ] */
7288 lhs = stabilize_reference (lhs);
7289 if (TREE_SIDE_EFFECTS (rhs))
7290 rhs = mark_rvalue_use (rhs);
7291 rhs = stabilize_expr (rhs, &init);
7292 newrhs = cp_build_binary_op (input_location,
7293 modifycode, lhs, rhs,
7294 complain);
7295 if (newrhs == error_mark_node)
7297 if (complain & tf_error)
7298 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7299 TREE_TYPE (lhs), TREE_TYPE (rhs));
7300 return error_mark_node;
7303 if (init)
7304 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7306 /* Now it looks like a plain assignment. */
7307 modifycode = NOP_EXPR;
7308 if (c_dialect_objc ())
7310 result = objc_maybe_build_modify_expr (lhs, newrhs);
7311 if (result)
7312 return result;
7315 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7316 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7319 /* The left-hand side must be an lvalue. */
7320 if (!lvalue_or_else (lhs, lv_assign, complain))
7321 return error_mark_node;
7323 /* Warn about modifying something that is `const'. Don't warn if
7324 this is initialization. */
7325 if (modifycode != INIT_EXPR
7326 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7327 /* Functions are not modifiable, even though they are
7328 lvalues. */
7329 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7330 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7331 /* If it's an aggregate and any field is const, then it is
7332 effectively const. */
7333 || (CLASS_TYPE_P (lhstype)
7334 && C_TYPE_FIELDS_READONLY (lhstype))))
7336 if (complain & tf_error)
7337 cxx_readonly_error (lhs, lv_assign);
7338 else
7339 return error_mark_node;
7342 /* If storing into a structure or union member, it may have been given a
7343 lowered bitfield type. We need to convert to the declared type first,
7344 so retrieve it now. */
7346 olhstype = unlowered_expr_type (lhs);
7348 /* Convert new value to destination type. */
7350 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7352 int from_array;
7354 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7356 if (modifycode != INIT_EXPR)
7358 if (complain & tf_error)
7359 error ("assigning to an array from an initializer list");
7360 return error_mark_node;
7362 if (check_array_initializer (lhs, lhstype, newrhs))
7363 return error_mark_node;
7364 newrhs = digest_init (lhstype, newrhs, complain);
7365 if (newrhs == error_mark_node)
7366 return error_mark_node;
7369 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7370 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7372 if (complain & tf_error)
7373 error ("incompatible types in assignment of %qT to %qT",
7374 TREE_TYPE (rhs), lhstype);
7375 return error_mark_node;
7378 /* Allow array assignment in compiler-generated code. */
7379 else if (!current_function_decl
7380 || !DECL_DEFAULTED_FN (current_function_decl))
7382 /* This routine is used for both initialization and assignment.
7383 Make sure the diagnostic message differentiates the context. */
7384 if (complain & tf_error)
7386 if (modifycode == INIT_EXPR)
7387 error ("array used as initializer");
7388 else
7389 error ("invalid array assignment");
7391 return error_mark_node;
7394 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7395 ? 1 + (modifycode != INIT_EXPR): 0;
7396 return build_vec_init (lhs, NULL_TREE, newrhs,
7397 /*explicit_value_init_p=*/false,
7398 from_array, complain);
7401 if (modifycode == INIT_EXPR)
7402 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7403 LOOKUP_ONLYCONVERTING. */
7404 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7405 ICR_INIT, NULL_TREE, 0,
7406 complain);
7407 else
7408 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7409 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7411 if (!same_type_p (lhstype, olhstype))
7412 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7414 if (modifycode != INIT_EXPR)
7416 if (TREE_CODE (newrhs) == CALL_EXPR
7417 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7418 newrhs = build_cplus_new (lhstype, newrhs, complain);
7420 /* Can't initialize directly from a TARGET_EXPR, since that would
7421 cause the lhs to be constructed twice, and possibly result in
7422 accidental self-initialization. So we force the TARGET_EXPR to be
7423 expanded without a target. */
7424 if (TREE_CODE (newrhs) == TARGET_EXPR)
7425 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7426 TREE_OPERAND (newrhs, 0));
7429 if (newrhs == error_mark_node)
7430 return error_mark_node;
7432 if (c_dialect_objc () && flag_objc_gc)
7434 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7436 if (result)
7437 return result;
7440 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7441 lhstype, lhs, newrhs);
7443 TREE_SIDE_EFFECTS (result) = 1;
7444 if (!plain_assign)
7445 TREE_NO_WARNING (result) = 1;
7447 return result;
7450 tree
7451 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7452 tree rhs, tsubst_flags_t complain)
7454 if (processing_template_decl)
7455 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7456 build_min_nt_loc (loc, modifycode, NULL_TREE,
7457 NULL_TREE), rhs);
7459 if (modifycode != NOP_EXPR)
7461 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7462 make_node (modifycode), /*overload=*/NULL,
7463 complain);
7464 if (rval)
7466 TREE_NO_WARNING (rval) = 1;
7467 return rval;
7470 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7473 /* Helper function for get_delta_difference which assumes FROM is a base
7474 class of TO. Returns a delta for the conversion of pointer-to-member
7475 of FROM to pointer-to-member of TO. If the conversion is invalid and
7476 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7477 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7478 If C_CAST_P is true, this conversion is taking place as part of a
7479 C-style cast. */
7481 static tree
7482 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7483 tsubst_flags_t complain)
7485 tree binfo;
7486 base_kind kind;
7488 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7489 &kind, complain);
7491 if (binfo == error_mark_node)
7493 if (!(complain & tf_error))
7494 return error_mark_node;
7496 error (" in pointer to member function conversion");
7497 return size_zero_node;
7499 else if (binfo)
7501 if (kind != bk_via_virtual)
7502 return BINFO_OFFSET (binfo);
7503 else
7504 /* FROM is a virtual base class of TO. Issue an error or warning
7505 depending on whether or not this is a reinterpret cast. */
7507 if (!(complain & tf_error))
7508 return error_mark_node;
7510 error ("pointer to member conversion via virtual base %qT",
7511 BINFO_TYPE (binfo_from_vbase (binfo)));
7513 return size_zero_node;
7516 else
7517 return NULL_TREE;
7520 /* Get difference in deltas for different pointer to member function
7521 types. If the conversion is invalid and tf_error is not set in
7522 COMPLAIN, returns error_mark_node, otherwise returns an integer
7523 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7524 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7525 conversions as well. If C_CAST_P is true this conversion is taking
7526 place as part of a C-style cast.
7528 Note that the naming of FROM and TO is kind of backwards; the return
7529 value is what we add to a TO in order to get a FROM. They are named
7530 this way because we call this function to find out how to convert from
7531 a pointer to member of FROM to a pointer to member of TO. */
7533 static tree
7534 get_delta_difference (tree from, tree to,
7535 bool allow_inverse_p,
7536 bool c_cast_p, tsubst_flags_t complain)
7538 tree result;
7540 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7541 /* Pointer to member of incomplete class is permitted*/
7542 result = size_zero_node;
7543 else
7544 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7546 if (result == error_mark_node)
7547 return error_mark_node;
7549 if (!result)
7551 if (!allow_inverse_p)
7553 if (!(complain & tf_error))
7554 return error_mark_node;
7556 error_not_base_type (from, to);
7557 error (" in pointer to member conversion");
7558 result = size_zero_node;
7560 else
7562 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7564 if (result == error_mark_node)
7565 return error_mark_node;
7567 if (result)
7568 result = size_diffop_loc (input_location,
7569 size_zero_node, result);
7570 else
7572 if (!(complain & tf_error))
7573 return error_mark_node;
7575 error_not_base_type (from, to);
7576 error (" in pointer to member conversion");
7577 result = size_zero_node;
7582 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7583 result));
7586 /* Return a constructor for the pointer-to-member-function TYPE using
7587 the other components as specified. */
7589 tree
7590 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7592 tree u = NULL_TREE;
7593 tree delta_field;
7594 tree pfn_field;
7595 vec<constructor_elt, va_gc> *v;
7597 /* Pull the FIELD_DECLs out of the type. */
7598 pfn_field = TYPE_FIELDS (type);
7599 delta_field = DECL_CHAIN (pfn_field);
7601 /* Make sure DELTA has the type we want. */
7602 delta = convert_and_check (delta_type_node, delta);
7604 /* Convert to the correct target type if necessary. */
7605 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7607 /* Finish creating the initializer. */
7608 vec_alloc (v, 2);
7609 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7610 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7611 u = build_constructor (type, v);
7612 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7613 TREE_STATIC (u) = (TREE_CONSTANT (u)
7614 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7615 != NULL_TREE)
7616 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7617 != NULL_TREE));
7618 return u;
7621 /* Build a constructor for a pointer to member function. It can be
7622 used to initialize global variables, local variable, or used
7623 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7624 want to be.
7626 If FORCE is nonzero, then force this conversion, even if
7627 we would rather not do it. Usually set when using an explicit
7628 cast. A C-style cast is being processed iff C_CAST_P is true.
7630 Return error_mark_node, if something goes wrong. */
7632 tree
7633 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7634 tsubst_flags_t complain)
7636 tree fn;
7637 tree pfn_type;
7638 tree to_type;
7640 if (error_operand_p (pfn))
7641 return error_mark_node;
7643 pfn_type = TREE_TYPE (pfn);
7644 to_type = build_ptrmemfunc_type (type);
7646 /* Handle multiple conversions of pointer to member functions. */
7647 if (TYPE_PTRMEMFUNC_P (pfn_type))
7649 tree delta = NULL_TREE;
7650 tree npfn = NULL_TREE;
7651 tree n;
7653 if (!force
7654 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7655 LOOKUP_NORMAL, complain))
7657 if (complain & tf_error)
7658 error ("invalid conversion to type %qT from type %qT",
7659 to_type, pfn_type);
7660 else
7661 return error_mark_node;
7664 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7665 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7666 force,
7667 c_cast_p, complain);
7668 if (n == error_mark_node)
7669 return error_mark_node;
7671 /* We don't have to do any conversion to convert a
7672 pointer-to-member to its own type. But, we don't want to
7673 just return a PTRMEM_CST if there's an explicit cast; that
7674 cast should make the expression an invalid template argument. */
7675 if (TREE_CODE (pfn) != PTRMEM_CST)
7677 if (same_type_p (to_type, pfn_type))
7678 return pfn;
7679 else if (integer_zerop (n))
7680 return build_reinterpret_cast (to_type, pfn,
7681 complain);
7684 if (TREE_SIDE_EFFECTS (pfn))
7685 pfn = save_expr (pfn);
7687 /* Obtain the function pointer and the current DELTA. */
7688 if (TREE_CODE (pfn) == PTRMEM_CST)
7689 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7690 else
7692 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7693 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7696 /* Just adjust the DELTA field. */
7697 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7698 (TREE_TYPE (delta), ptrdiff_type_node));
7699 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7700 n = cp_build_binary_op (input_location,
7701 LSHIFT_EXPR, n, integer_one_node,
7702 complain);
7703 delta = cp_build_binary_op (input_location,
7704 PLUS_EXPR, delta, n, complain);
7705 return build_ptrmemfunc1 (to_type, delta, npfn);
7708 /* Handle null pointer to member function conversions. */
7709 if (null_ptr_cst_p (pfn))
7711 pfn = build_c_cast (input_location, type, pfn);
7712 return build_ptrmemfunc1 (to_type,
7713 integer_zero_node,
7714 pfn);
7717 if (type_unknown_p (pfn))
7718 return instantiate_type (type, pfn, complain);
7720 fn = TREE_OPERAND (pfn, 0);
7721 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7722 /* In a template, we will have preserved the
7723 OFFSET_REF. */
7724 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7725 return make_ptrmem_cst (to_type, fn);
7728 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7729 given by CST.
7731 ??? There is no consistency as to the types returned for the above
7732 values. Some code acts as if it were a sizetype and some as if it were
7733 integer_type_node. */
7735 void
7736 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7738 tree type = TREE_TYPE (cst);
7739 tree fn = PTRMEM_CST_MEMBER (cst);
7740 tree ptr_class, fn_class;
7742 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7744 /* The class that the function belongs to. */
7745 fn_class = DECL_CONTEXT (fn);
7747 /* The class that we're creating a pointer to member of. */
7748 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7750 /* First, calculate the adjustment to the function's class. */
7751 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7752 /*c_cast_p=*/0, tf_warning_or_error);
7754 if (!DECL_VIRTUAL_P (fn))
7755 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7756 build_addr_func (fn, tf_warning_or_error));
7757 else
7759 /* If we're dealing with a virtual function, we have to adjust 'this'
7760 again, to point to the base which provides the vtable entry for
7761 fn; the call will do the opposite adjustment. */
7762 tree orig_class = DECL_CONTEXT (fn);
7763 tree binfo = binfo_or_else (orig_class, fn_class);
7764 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7765 *delta, BINFO_OFFSET (binfo));
7766 *delta = fold_if_not_in_template (*delta);
7768 /* We set PFN to the vtable offset at which the function can be
7769 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7770 case delta is shifted left, and then incremented). */
7771 *pfn = DECL_VINDEX (fn);
7772 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7773 TYPE_SIZE_UNIT (vtable_entry_type));
7774 *pfn = fold_if_not_in_template (*pfn);
7776 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7778 case ptrmemfunc_vbit_in_pfn:
7779 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7780 integer_one_node);
7781 *pfn = fold_if_not_in_template (*pfn);
7782 break;
7784 case ptrmemfunc_vbit_in_delta:
7785 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7786 *delta, integer_one_node);
7787 *delta = fold_if_not_in_template (*delta);
7788 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7789 *delta, integer_one_node);
7790 *delta = fold_if_not_in_template (*delta);
7791 break;
7793 default:
7794 gcc_unreachable ();
7797 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7798 *pfn = fold_if_not_in_template (*pfn);
7802 /* Return an expression for PFN from the pointer-to-member function
7803 given by T. */
7805 static tree
7806 pfn_from_ptrmemfunc (tree t)
7808 if (TREE_CODE (t) == PTRMEM_CST)
7810 tree delta;
7811 tree pfn;
7813 expand_ptrmemfunc_cst (t, &delta, &pfn);
7814 if (pfn)
7815 return pfn;
7818 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7821 /* Return an expression for DELTA from the pointer-to-member function
7822 given by T. */
7824 static tree
7825 delta_from_ptrmemfunc (tree t)
7827 if (TREE_CODE (t) == PTRMEM_CST)
7829 tree delta;
7830 tree pfn;
7832 expand_ptrmemfunc_cst (t, &delta, &pfn);
7833 if (delta)
7834 return delta;
7837 return build_ptrmemfunc_access_expr (t, delta_identifier);
7840 /* Convert value RHS to type TYPE as preparation for an assignment to
7841 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7842 implicit conversion is. If FNDECL is non-NULL, we are doing the
7843 conversion in order to pass the PARMNUMth argument of FNDECL.
7844 If FNDECL is NULL, we are doing the conversion in function pointer
7845 argument passing, conversion in initialization, etc. */
7847 static tree
7848 convert_for_assignment (tree type, tree rhs,
7849 impl_conv_rhs errtype, tree fndecl, int parmnum,
7850 tsubst_flags_t complain, int flags)
7852 tree rhstype;
7853 enum tree_code coder;
7855 /* If we are dealing with built-in array notation function then we don't need
7856 to convert them. They will be broken up into modify exprs in future,
7857 during which all these checks will be done. */
7858 if (flag_enable_cilkplus
7859 && is_cilkplus_reduce_builtin (fndecl) != BUILT_IN_NONE)
7860 return rhs;
7862 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7863 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7864 rhs = TREE_OPERAND (rhs, 0);
7866 rhstype = TREE_TYPE (rhs);
7867 coder = TREE_CODE (rhstype);
7869 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7870 && vector_types_convertible_p (type, rhstype, true))
7872 rhs = mark_rvalue_use (rhs);
7873 return convert (type, rhs);
7876 if (rhs == error_mark_node || rhstype == error_mark_node)
7877 return error_mark_node;
7878 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7879 return error_mark_node;
7881 /* The RHS of an assignment cannot have void type. */
7882 if (coder == VOID_TYPE)
7884 if (complain & tf_error)
7885 error ("void value not ignored as it ought to be");
7886 return error_mark_node;
7889 if (c_dialect_objc ())
7891 int parmno;
7892 tree selector;
7893 tree rname = fndecl;
7895 switch (errtype)
7897 case ICR_ASSIGN:
7898 parmno = -1;
7899 break;
7900 case ICR_INIT:
7901 parmno = -2;
7902 break;
7903 default:
7904 selector = objc_message_selector ();
7905 parmno = parmnum;
7906 if (selector && parmno > 1)
7908 rname = selector;
7909 parmno -= 1;
7913 if (objc_compare_types (type, rhstype, parmno, rname))
7915 rhs = mark_rvalue_use (rhs);
7916 return convert (type, rhs);
7920 /* [expr.ass]
7922 The expression is implicitly converted (clause _conv_) to the
7923 cv-unqualified type of the left operand.
7925 We allow bad conversions here because by the time we get to this point
7926 we are committed to doing the conversion. If we end up doing a bad
7927 conversion, convert_like will complain. */
7928 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
7930 /* When -Wno-pmf-conversions is use, we just silently allow
7931 conversions from pointers-to-members to plain pointers. If
7932 the conversion doesn't work, cp_convert will complain. */
7933 if (!warn_pmf2ptr
7934 && TYPE_PTR_P (type)
7935 && TYPE_PTRMEMFUNC_P (rhstype))
7936 rhs = cp_convert (strip_top_quals (type), rhs, complain);
7937 else
7939 if (complain & tf_error)
7941 /* If the right-hand side has unknown type, then it is an
7942 overloaded function. Call instantiate_type to get error
7943 messages. */
7944 if (rhstype == unknown_type_node)
7945 instantiate_type (type, rhs, tf_warning_or_error);
7946 else if (fndecl)
7947 error ("cannot convert %qT to %qT for argument %qP to %qD",
7948 rhstype, type, parmnum, fndecl);
7949 else
7950 switch (errtype)
7952 case ICR_DEFAULT_ARGUMENT:
7953 error ("cannot convert %qT to %qT in default argument",
7954 rhstype, type);
7955 break;
7956 case ICR_ARGPASS:
7957 error ("cannot convert %qT to %qT in argument passing",
7958 rhstype, type);
7959 break;
7960 case ICR_CONVERTING:
7961 error ("cannot convert %qT to %qT",
7962 rhstype, type);
7963 break;
7964 case ICR_INIT:
7965 error ("cannot convert %qT to %qT in initialization",
7966 rhstype, type);
7967 break;
7968 case ICR_RETURN:
7969 error ("cannot convert %qT to %qT in return",
7970 rhstype, type);
7971 break;
7972 case ICR_ASSIGN:
7973 error ("cannot convert %qT to %qT in assignment",
7974 rhstype, type);
7975 break;
7976 default:
7977 gcc_unreachable();
7980 return error_mark_node;
7983 if (warn_suggest_attribute_format)
7985 const enum tree_code codel = TREE_CODE (type);
7986 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7987 && coder == codel
7988 && check_missing_format_attribute (type, rhstype)
7989 && (complain & tf_warning))
7990 switch (errtype)
7992 case ICR_ARGPASS:
7993 case ICR_DEFAULT_ARGUMENT:
7994 if (fndecl)
7995 warning (OPT_Wsuggest_attribute_format,
7996 "parameter %qP of %qD might be a candidate "
7997 "for a format attribute", parmnum, fndecl);
7998 else
7999 warning (OPT_Wsuggest_attribute_format,
8000 "parameter might be a candidate "
8001 "for a format attribute");
8002 break;
8003 case ICR_CONVERTING:
8004 warning (OPT_Wsuggest_attribute_format,
8005 "target of conversion might be a candidate "
8006 "for a format attribute");
8007 break;
8008 case ICR_INIT:
8009 warning (OPT_Wsuggest_attribute_format,
8010 "target of initialization might be a candidate "
8011 "for a format attribute");
8012 break;
8013 case ICR_RETURN:
8014 warning (OPT_Wsuggest_attribute_format,
8015 "return type might be a candidate "
8016 "for a format attribute");
8017 break;
8018 case ICR_ASSIGN:
8019 warning (OPT_Wsuggest_attribute_format,
8020 "left-hand side of assignment might be a candidate "
8021 "for a format attribute");
8022 break;
8023 default:
8024 gcc_unreachable();
8028 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8029 does not. */
8030 if (warn_parentheses
8031 && TREE_CODE (type) == BOOLEAN_TYPE
8032 && TREE_CODE (rhs) == MODIFY_EXPR
8033 && !TREE_NO_WARNING (rhs)
8034 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8035 && (complain & tf_warning))
8037 location_t loc = EXPR_LOC_OR_HERE (rhs);
8039 warning_at (loc, OPT_Wparentheses,
8040 "suggest parentheses around assignment used as truth value");
8041 TREE_NO_WARNING (rhs) = 1;
8044 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8045 complain, flags);
8048 /* Convert RHS to be of type TYPE.
8049 If EXP is nonzero, it is the target of the initialization.
8050 ERRTYPE indicates what kind of error the implicit conversion is.
8052 Two major differences between the behavior of
8053 `convert_for_assignment' and `convert_for_initialization'
8054 are that references are bashed in the former, while
8055 copied in the latter, and aggregates are assigned in
8056 the former (operator=) while initialized in the
8057 latter (X(X&)).
8059 If using constructor make sure no conversion operator exists, if one does
8060 exist, an ambiguity exists. */
8062 tree
8063 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8064 impl_conv_rhs errtype, tree fndecl, int parmnum,
8065 tsubst_flags_t complain)
8067 enum tree_code codel = TREE_CODE (type);
8068 tree rhstype;
8069 enum tree_code coder;
8071 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8072 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8073 if (TREE_CODE (rhs) == NOP_EXPR
8074 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8075 && codel != REFERENCE_TYPE)
8076 rhs = TREE_OPERAND (rhs, 0);
8078 if (type == error_mark_node
8079 || rhs == error_mark_node
8080 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8081 return error_mark_node;
8083 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8084 && TREE_CODE (type) != ARRAY_TYPE
8085 && (TREE_CODE (type) != REFERENCE_TYPE
8086 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8087 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8088 && !TYPE_REFFN_P (type))
8089 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8090 rhs = decay_conversion (rhs, complain);
8092 rhstype = TREE_TYPE (rhs);
8093 coder = TREE_CODE (rhstype);
8095 if (coder == ERROR_MARK)
8096 return error_mark_node;
8098 /* We accept references to incomplete types, so we can
8099 return here before checking if RHS is of complete type. */
8101 if (codel == REFERENCE_TYPE)
8103 /* This should eventually happen in convert_arguments. */
8104 int savew = 0, savee = 0;
8106 if (fndecl)
8107 savew = warningcount + werrorcount, savee = errorcount;
8108 rhs = initialize_reference (type, rhs, flags, complain);
8110 if (fndecl
8111 && (warningcount + werrorcount > savew || errorcount > savee))
8112 inform (input_location,
8113 "in passing argument %P of %q+D", parmnum, fndecl);
8115 return rhs;
8118 if (exp != 0)
8119 exp = require_complete_type_sfinae (exp, complain);
8120 if (exp == error_mark_node)
8121 return error_mark_node;
8123 rhstype = non_reference (rhstype);
8125 type = complete_type (type);
8127 if (DIRECT_INIT_EXPR_P (type, rhs))
8128 /* Don't try to do copy-initialization if we already have
8129 direct-initialization. */
8130 return rhs;
8132 if (MAYBE_CLASS_TYPE_P (type))
8133 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8135 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8136 complain, flags);
8139 /* If RETVAL is the address of, or a reference to, a local variable or
8140 temporary give an appropriate warning. */
8142 static void
8143 maybe_warn_about_returning_address_of_local (tree retval)
8145 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8146 tree whats_returned = retval;
8148 for (;;)
8150 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8151 whats_returned = TREE_OPERAND (whats_returned, 1);
8152 else if (CONVERT_EXPR_P (whats_returned)
8153 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8154 whats_returned = TREE_OPERAND (whats_returned, 0);
8155 else
8156 break;
8159 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8160 return;
8161 whats_returned = TREE_OPERAND (whats_returned, 0);
8163 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8165 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8166 || TREE_CODE (whats_returned) == TARGET_EXPR)
8168 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8169 return;
8171 if (VAR_P (whats_returned)
8172 && DECL_NAME (whats_returned)
8173 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8175 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8176 return;
8180 while (TREE_CODE (whats_returned) == COMPONENT_REF
8181 || TREE_CODE (whats_returned) == ARRAY_REF)
8182 whats_returned = TREE_OPERAND (whats_returned, 0);
8184 if (DECL_P (whats_returned)
8185 && DECL_NAME (whats_returned)
8186 && DECL_FUNCTION_SCOPE_P (whats_returned)
8187 && !is_capture_proxy (whats_returned)
8188 && !(TREE_STATIC (whats_returned)
8189 || TREE_PUBLIC (whats_returned)))
8191 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8192 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8193 whats_returned);
8194 else
8195 warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
8196 whats_returned);
8197 return;
8201 /* Check that returning RETVAL from the current function is valid.
8202 Return an expression explicitly showing all conversions required to
8203 change RETVAL into the function return type, and to assign it to
8204 the DECL_RESULT for the function. Set *NO_WARNING to true if
8205 code reaches end of non-void function warning shouldn't be issued
8206 on this RETURN_EXPR. */
8208 tree
8209 check_return_expr (tree retval, bool *no_warning)
8211 tree result;
8212 /* The type actually returned by the function. */
8213 tree valtype;
8214 /* The type the function is declared to return, or void if
8215 the declared type is incomplete. */
8216 tree functype;
8217 int fn_returns_value_p;
8218 bool named_return_value_okay_p;
8220 *no_warning = false;
8222 /* A `volatile' function is one that isn't supposed to return, ever.
8223 (This is a G++ extension, used to get better code for functions
8224 that call the `volatile' function.) */
8225 if (TREE_THIS_VOLATILE (current_function_decl))
8226 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8228 /* Check for various simple errors. */
8229 if (DECL_DESTRUCTOR_P (current_function_decl))
8231 if (retval)
8232 error ("returning a value from a destructor");
8233 return NULL_TREE;
8235 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8237 if (in_function_try_handler)
8238 /* If a return statement appears in a handler of the
8239 function-try-block of a constructor, the program is ill-formed. */
8240 error ("cannot return from a handler of a function-try-block of a constructor");
8241 else if (retval)
8242 /* You can't return a value from a constructor. */
8243 error ("returning a value from a constructor");
8244 return NULL_TREE;
8247 if (processing_template_decl)
8249 current_function_returns_value = 1;
8250 if (check_for_bare_parameter_packs (retval))
8251 retval = error_mark_node;
8252 return retval;
8255 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8257 /* Deduce auto return type from a return statement. */
8258 if (current_function_auto_return_pattern)
8260 tree auto_node;
8261 tree type;
8263 if (!retval && !is_auto (current_function_auto_return_pattern))
8265 /* Give a helpful error message. */
8266 error ("return-statement with no value, in function returning %qT",
8267 current_function_auto_return_pattern);
8268 inform (input_location, "only plain %<auto%> return type can be "
8269 "deduced to %<void%>");
8270 type = error_mark_node;
8272 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8274 error ("returning initializer list");
8275 type = error_mark_node;
8277 else
8279 if (!retval)
8280 retval = void_zero_node;
8281 auto_node = type_uses_auto (current_function_auto_return_pattern);
8282 type = do_auto_deduction (current_function_auto_return_pattern,
8283 retval, auto_node);
8286 if (type == error_mark_node)
8287 /* Leave it. */;
8288 else if (functype == current_function_auto_return_pattern)
8289 apply_deduced_return_type (current_function_decl, type);
8290 else
8291 /* A mismatch should have been diagnosed in do_auto_deduction. */
8292 gcc_assert (same_type_p (type, functype));
8293 functype = type;
8296 /* When no explicit return-value is given in a function with a named
8297 return value, the named return value is used. */
8298 result = DECL_RESULT (current_function_decl);
8299 valtype = TREE_TYPE (result);
8300 gcc_assert (valtype != NULL_TREE);
8301 fn_returns_value_p = !VOID_TYPE_P (valtype);
8302 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8303 retval = result;
8305 /* Check for a return statement with no return value in a function
8306 that's supposed to return a value. */
8307 if (!retval && fn_returns_value_p)
8309 if (functype != error_mark_node)
8310 permerror (input_location, "return-statement with no value, in "
8311 "function returning %qT", valtype);
8312 /* Remember that this function did return. */
8313 current_function_returns_value = 1;
8314 /* And signal caller that TREE_NO_WARNING should be set on the
8315 RETURN_EXPR to avoid control reaches end of non-void function
8316 warnings in tree-cfg.c. */
8317 *no_warning = true;
8319 /* Check for a return statement with a value in a function that
8320 isn't supposed to return a value. */
8321 else if (retval && !fn_returns_value_p)
8323 if (VOID_TYPE_P (TREE_TYPE (retval)))
8324 /* You can return a `void' value from a function of `void'
8325 type. In that case, we have to evaluate the expression for
8326 its side-effects. */
8327 finish_expr_stmt (retval);
8328 else
8329 permerror (input_location, "return-statement with a value, in function "
8330 "returning 'void'");
8331 current_function_returns_null = 1;
8333 /* There's really no value to return, after all. */
8334 return NULL_TREE;
8336 else if (!retval)
8337 /* Remember that this function can sometimes return without a
8338 value. */
8339 current_function_returns_null = 1;
8340 else
8341 /* Remember that this function did return a value. */
8342 current_function_returns_value = 1;
8344 /* Check for erroneous operands -- but after giving ourselves a
8345 chance to provide an error about returning a value from a void
8346 function. */
8347 if (error_operand_p (retval))
8349 current_function_return_value = error_mark_node;
8350 return error_mark_node;
8353 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8354 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8355 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8356 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8357 && ! flag_check_new
8358 && retval && null_ptr_cst_p (retval))
8359 warning (0, "%<operator new%> must not return NULL unless it is "
8360 "declared %<throw()%> (or -fcheck-new is in effect)");
8362 /* Effective C++ rule 15. See also start_function. */
8363 if (warn_ecpp
8364 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8366 bool warn = true;
8368 /* The function return type must be a reference to the current
8369 class. */
8370 if (TREE_CODE (valtype) == REFERENCE_TYPE
8371 && same_type_ignoring_top_level_qualifiers_p
8372 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8374 /* Returning '*this' is obviously OK. */
8375 if (retval == current_class_ref)
8376 warn = false;
8377 /* If we are calling a function whose return type is the same of
8378 the current class reference, it is ok. */
8379 else if (INDIRECT_REF_P (retval)
8380 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8381 warn = false;
8384 if (warn)
8385 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8388 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8390 [...] For a function with a class return type, if the expression
8391 in the return statement is the name of a local object, and the cv-
8392 unqualified type of the local object is the same as the function
8393 return type, an implementation is permitted to omit creating the tem-
8394 porary object to hold the function return value [...]
8396 So, if this is a value-returning function that always returns the same
8397 local variable, remember it.
8399 It might be nice to be more flexible, and choose the first suitable
8400 variable even if the function sometimes returns something else, but
8401 then we run the risk of clobbering the variable we chose if the other
8402 returned expression uses the chosen variable somehow. And people expect
8403 this restriction, anyway. (jason 2000-11-19)
8405 See finish_function and finalize_nrv for the rest of this optimization. */
8407 named_return_value_okay_p =
8408 (retval != NULL_TREE
8409 /* Must be a local, automatic variable. */
8410 && VAR_P (retval)
8411 && DECL_CONTEXT (retval) == current_function_decl
8412 && ! TREE_STATIC (retval)
8413 && ! DECL_ANON_UNION_VAR_P (retval)
8414 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8415 /* The cv-unqualified type of the returned value must be the
8416 same as the cv-unqualified return type of the
8417 function. */
8418 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8419 (TYPE_MAIN_VARIANT (functype)))
8420 /* And the returned value must be non-volatile. */
8421 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8423 if (fn_returns_value_p && flag_elide_constructors)
8425 if (named_return_value_okay_p
8426 && (current_function_return_value == NULL_TREE
8427 || current_function_return_value == retval))
8428 current_function_return_value = retval;
8429 else
8430 current_function_return_value = error_mark_node;
8433 /* We don't need to do any conversions when there's nothing being
8434 returned. */
8435 if (!retval)
8436 return NULL_TREE;
8438 /* Do any required conversions. */
8439 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8440 /* No conversions are required. */
8442 else
8444 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8446 /* The functype's return type will have been set to void, if it
8447 was an incomplete type. Just treat this as 'return;' */
8448 if (VOID_TYPE_P (functype))
8449 return error_mark_node;
8451 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8452 treated as an rvalue for the purposes of overload resolution to
8453 favor move constructors over copy constructors.
8455 Note that these conditions are similar to, but not as strict as,
8456 the conditions for the named return value optimization. */
8457 if ((cxx_dialect != cxx98)
8458 && (VAR_P (retval)
8459 || TREE_CODE (retval) == PARM_DECL)
8460 && DECL_CONTEXT (retval) == current_function_decl
8461 && !TREE_STATIC (retval)
8462 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8463 (TYPE_MAIN_VARIANT (functype)))
8464 /* This is only interesting for class type. */
8465 && CLASS_TYPE_P (functype))
8466 flags = flags | LOOKUP_PREFER_RVALUE;
8468 /* First convert the value to the function's return type, then
8469 to the type of return value's location to handle the
8470 case that functype is smaller than the valtype. */
8471 retval = convert_for_initialization
8472 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8473 tf_warning_or_error);
8474 retval = convert (valtype, retval);
8476 /* If the conversion failed, treat this just like `return;'. */
8477 if (retval == error_mark_node)
8478 return retval;
8479 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8480 else if (! cfun->returns_struct
8481 && TREE_CODE (retval) == TARGET_EXPR
8482 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8483 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8484 TREE_OPERAND (retval, 0));
8485 else
8486 maybe_warn_about_returning_address_of_local (retval);
8489 /* Actually copy the value returned into the appropriate location. */
8490 if (retval && retval != result)
8491 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8493 return retval;
8497 /* Returns nonzero if the pointer-type FROM can be converted to the
8498 pointer-type TO via a qualification conversion. If CONSTP is -1,
8499 then we return nonzero if the pointers are similar, and the
8500 cv-qualification signature of FROM is a proper subset of that of TO.
8502 If CONSTP is positive, then all outer pointers have been
8503 const-qualified. */
8505 static int
8506 comp_ptr_ttypes_real (tree to, tree from, int constp)
8508 bool to_more_cv_qualified = false;
8509 bool is_opaque_pointer = false;
8511 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8513 if (TREE_CODE (to) != TREE_CODE (from))
8514 return 0;
8516 if (TREE_CODE (from) == OFFSET_TYPE
8517 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8518 TYPE_OFFSET_BASETYPE (to)))
8519 return 0;
8521 /* Const and volatile mean something different for function types,
8522 so the usual checks are not appropriate. */
8523 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8525 if (!at_least_as_qualified_p (to, from))
8526 return 0;
8528 if (!at_least_as_qualified_p (from, to))
8530 if (constp == 0)
8531 return 0;
8532 to_more_cv_qualified = true;
8535 if (constp > 0)
8536 constp &= TYPE_READONLY (to);
8539 if (TREE_CODE (to) == VECTOR_TYPE)
8540 is_opaque_pointer = vector_targets_convertible_p (to, from);
8542 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8543 return ((constp >= 0 || to_more_cv_qualified)
8544 && (is_opaque_pointer
8545 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8549 /* When comparing, say, char ** to char const **, this function takes
8550 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8551 types to this function. */
8554 comp_ptr_ttypes (tree to, tree from)
8556 return comp_ptr_ttypes_real (to, from, 1);
8559 /* Returns true iff FNTYPE is a non-class type that involves
8560 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8561 if a parameter type is ill-formed. */
8563 bool
8564 error_type_p (const_tree type)
8566 tree t;
8568 switch (TREE_CODE (type))
8570 case ERROR_MARK:
8571 return true;
8573 case POINTER_TYPE:
8574 case REFERENCE_TYPE:
8575 case OFFSET_TYPE:
8576 return error_type_p (TREE_TYPE (type));
8578 case FUNCTION_TYPE:
8579 case METHOD_TYPE:
8580 if (error_type_p (TREE_TYPE (type)))
8581 return true;
8582 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8583 if (error_type_p (TREE_VALUE (t)))
8584 return true;
8585 return false;
8587 case RECORD_TYPE:
8588 if (TYPE_PTRMEMFUNC_P (type))
8589 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8590 return false;
8592 default:
8593 return false;
8597 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8598 type or inheritance-related types, regardless of cv-quals. */
8601 ptr_reasonably_similar (const_tree to, const_tree from)
8603 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8605 /* Any target type is similar enough to void. */
8606 if (VOID_TYPE_P (to))
8607 return !error_type_p (from);
8608 if (VOID_TYPE_P (from))
8609 return !error_type_p (to);
8611 if (TREE_CODE (to) != TREE_CODE (from))
8612 return 0;
8614 if (TREE_CODE (from) == OFFSET_TYPE
8615 && comptypes (TYPE_OFFSET_BASETYPE (to),
8616 TYPE_OFFSET_BASETYPE (from),
8617 COMPARE_BASE | COMPARE_DERIVED))
8618 continue;
8620 if (TREE_CODE (to) == VECTOR_TYPE
8621 && vector_types_convertible_p (to, from, false))
8622 return 1;
8624 if (TREE_CODE (to) == INTEGER_TYPE
8625 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8626 return 1;
8628 if (TREE_CODE (to) == FUNCTION_TYPE)
8629 return !error_type_p (to) && !error_type_p (from);
8631 if (!TYPE_PTR_P (to))
8632 return comptypes
8633 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8634 COMPARE_BASE | COMPARE_DERIVED);
8638 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8639 pointer-to-member types) are the same, ignoring cv-qualification at
8640 all levels. */
8642 bool
8643 comp_ptr_ttypes_const (tree to, tree from)
8645 bool is_opaque_pointer = false;
8647 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8649 if (TREE_CODE (to) != TREE_CODE (from))
8650 return false;
8652 if (TREE_CODE (from) == OFFSET_TYPE
8653 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8654 TYPE_OFFSET_BASETYPE (to)))
8655 continue;
8657 if (TREE_CODE (to) == VECTOR_TYPE)
8658 is_opaque_pointer = vector_targets_convertible_p (to, from);
8660 if (!TYPE_PTR_P (to))
8661 return (is_opaque_pointer
8662 || same_type_ignoring_top_level_qualifiers_p (to, from));
8666 /* Returns the type qualifiers for this type, including the qualifiers on the
8667 elements for an array type. */
8670 cp_type_quals (const_tree type)
8672 int quals;
8673 /* This CONST_CAST is okay because strip_array_types returns its
8674 argument unmodified and we assign it to a const_tree. */
8675 type = strip_array_types (CONST_CAST_TREE (type));
8676 if (type == error_mark_node
8677 /* Quals on a FUNCTION_TYPE are memfn quals. */
8678 || TREE_CODE (type) == FUNCTION_TYPE)
8679 return TYPE_UNQUALIFIED;
8680 quals = TYPE_QUALS (type);
8681 /* METHOD and REFERENCE_TYPEs should never have quals. */
8682 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8683 && TREE_CODE (type) != REFERENCE_TYPE)
8684 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8685 == TYPE_UNQUALIFIED));
8686 return quals;
8689 /* Returns the function-ref-qualifier for TYPE */
8691 cp_ref_qualifier
8692 type_memfn_rqual (const_tree type)
8694 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8695 || TREE_CODE (type) == METHOD_TYPE);
8697 if (!FUNCTION_REF_QUALIFIED (type))
8698 return REF_QUAL_NONE;
8699 else if (FUNCTION_RVALUE_QUALIFIED (type))
8700 return REF_QUAL_RVALUE;
8701 else
8702 return REF_QUAL_LVALUE;
8705 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8706 METHOD_TYPE. */
8709 type_memfn_quals (const_tree type)
8711 if (TREE_CODE (type) == FUNCTION_TYPE)
8712 return TYPE_QUALS (type);
8713 else if (TREE_CODE (type) == METHOD_TYPE)
8714 return cp_type_quals (class_of_this_parm (type));
8715 else
8716 gcc_unreachable ();
8719 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8720 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8722 tree
8723 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8725 /* Could handle METHOD_TYPE here if necessary. */
8726 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8727 if (TYPE_QUALS (type) == memfn_quals
8728 && type_memfn_rqual (type) == rqual)
8729 return type;
8731 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8732 complex. */
8733 tree result = build_qualified_type (type, memfn_quals);
8734 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8735 return build_ref_qualified_type (result, rqual);
8738 /* Returns nonzero if TYPE is const or volatile. */
8740 bool
8741 cv_qualified_p (const_tree type)
8743 int quals = cp_type_quals (type);
8744 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8747 /* Returns nonzero if the TYPE contains a mutable member. */
8749 bool
8750 cp_has_mutable_p (const_tree type)
8752 /* This CONST_CAST is okay because strip_array_types returns its
8753 argument unmodified and we assign it to a const_tree. */
8754 type = strip_array_types (CONST_CAST_TREE(type));
8756 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8759 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8760 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8761 approximation. In particular, consider:
8763 int f();
8764 struct S { int i; };
8765 const S s = { f(); }
8767 Here, we will make "s" as TREE_READONLY (because it is declared
8768 "const") -- only to reverse ourselves upon seeing that the
8769 initializer is non-constant. */
8771 void
8772 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8774 tree type = TREE_TYPE (decl);
8776 if (type == error_mark_node)
8777 return;
8779 if (TREE_CODE (decl) == TYPE_DECL)
8780 return;
8782 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8783 && type_quals != TYPE_UNQUALIFIED));
8785 /* Avoid setting TREE_READONLY incorrectly. */
8786 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8787 constructor can produce constant init, so rely on cp_finish_decl to
8788 clear TREE_READONLY if the variable has non-constant init. */
8790 /* If the type has (or might have) a mutable component, that component
8791 might be modified. */
8792 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8793 type_quals &= ~TYPE_QUAL_CONST;
8795 c_apply_type_quals_to_decl (type_quals, decl);
8798 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8799 exemplar types such that casting T1 to T2 is casting away constness
8800 if and only if there is no implicit conversion from T1 to T2. */
8802 static void
8803 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8805 int quals1;
8806 int quals2;
8808 /* [expr.const.cast]
8810 For multi-level pointer to members and multi-level mixed pointers
8811 and pointers to members (conv.qual), the "member" aspect of a
8812 pointer to member level is ignored when determining if a const
8813 cv-qualifier has been cast away. */
8814 /* [expr.const.cast]
8816 For two pointer types:
8818 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8819 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8820 K is min(N,M)
8822 casting from X1 to X2 casts away constness if, for a non-pointer
8823 type T there does not exist an implicit conversion (clause
8824 _conv_) from:
8826 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8830 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8831 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8832 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8834 *t1 = cp_build_qualified_type (void_type_node,
8835 cp_type_quals (*t1));
8836 *t2 = cp_build_qualified_type (void_type_node,
8837 cp_type_quals (*t2));
8838 return;
8841 quals1 = cp_type_quals (*t1);
8842 quals2 = cp_type_quals (*t2);
8844 if (TYPE_PTRDATAMEM_P (*t1))
8845 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8846 else
8847 *t1 = TREE_TYPE (*t1);
8848 if (TYPE_PTRDATAMEM_P (*t2))
8849 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8850 else
8851 *t2 = TREE_TYPE (*t2);
8853 casts_away_constness_r (t1, t2, complain);
8854 *t1 = build_pointer_type (*t1);
8855 *t2 = build_pointer_type (*t2);
8856 *t1 = cp_build_qualified_type (*t1, quals1);
8857 *t2 = cp_build_qualified_type (*t2, quals2);
8860 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8861 constness.
8863 ??? This function returns non-zero if casting away qualifiers not
8864 just const. We would like to return to the caller exactly which
8865 qualifiers are casted away to give more accurate diagnostics.
8868 static bool
8869 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8871 if (TREE_CODE (t2) == REFERENCE_TYPE)
8873 /* [expr.const.cast]
8875 Casting from an lvalue of type T1 to an lvalue of type T2
8876 using a reference cast casts away constness if a cast from an
8877 rvalue of type "pointer to T1" to the type "pointer to T2"
8878 casts away constness. */
8879 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8880 return casts_away_constness (build_pointer_type (t1),
8881 build_pointer_type (TREE_TYPE (t2)),
8882 complain);
8885 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
8886 /* [expr.const.cast]
8888 Casting from an rvalue of type "pointer to data member of X
8889 of type T1" to the type "pointer to data member of Y of type
8890 T2" casts away constness if a cast from an rvalue of type
8891 "pointer to T1" to the type "pointer to T2" casts away
8892 constness. */
8893 return casts_away_constness
8894 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8895 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
8896 complain);
8898 /* Casting away constness is only something that makes sense for
8899 pointer or reference types. */
8900 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
8901 return false;
8903 /* Top-level qualifiers don't matter. */
8904 t1 = TYPE_MAIN_VARIANT (t1);
8905 t2 = TYPE_MAIN_VARIANT (t2);
8906 casts_away_constness_r (&t1, &t2, complain);
8907 if (!can_convert (t2, t1, complain))
8908 return true;
8910 return false;
8913 /* If T is a REFERENCE_TYPE return the type to which T refers.
8914 Otherwise, return T itself. */
8916 tree
8917 non_reference (tree t)
8919 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8920 t = TREE_TYPE (t);
8921 return t;
8925 /* Return nonzero if REF is an lvalue valid for this language;
8926 otherwise, print an error message and return zero. USE says
8927 how the lvalue is being used and so selects the error message. */
8930 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8932 cp_lvalue_kind kind = lvalue_kind (ref);
8934 if (kind == clk_none)
8936 if (complain & tf_error)
8937 lvalue_error (input_location, use);
8938 return 0;
8940 else if (kind & (clk_rvalueref|clk_class))
8942 if (!(complain & tf_error))
8943 return 0;
8944 if (kind & clk_class)
8945 /* Make this a permerror because we used to accept it. */
8946 permerror (input_location, "using temporary as lvalue");
8947 else
8948 error ("using xvalue (rvalue reference) as lvalue");
8950 return 1;
8953 /* Return true if a user-defined literal operator is a raw operator. */
8955 bool
8956 check_raw_literal_operator (const_tree decl)
8958 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8959 tree argtype;
8960 int arity;
8961 bool maybe_raw_p = false;
8963 /* Count the number and type of arguments and check for ellipsis. */
8964 for (argtype = argtypes, arity = 0;
8965 argtype && argtype != void_list_node;
8966 ++arity, argtype = TREE_CHAIN (argtype))
8968 tree t = TREE_VALUE (argtype);
8970 if (same_type_p (t, const_string_type_node))
8971 maybe_raw_p = true;
8973 if (!argtype)
8974 return false; /* Found ellipsis. */
8976 if (!maybe_raw_p || arity != 1)
8977 return false;
8979 return true;
8983 /* Return true if a user-defined literal operator has one of the allowed
8984 argument types. */
8986 bool
8987 check_literal_operator_args (const_tree decl,
8988 bool *long_long_unsigned_p, bool *long_double_p)
8990 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8992 *long_long_unsigned_p = false;
8993 *long_double_p = false;
8994 if (processing_template_decl || processing_specialization)
8995 return argtypes == void_list_node;
8996 else
8998 tree argtype;
8999 int arity;
9000 int max_arity = 2;
9002 /* Count the number and type of arguments and check for ellipsis. */
9003 for (argtype = argtypes, arity = 0;
9004 argtype && argtype != void_list_node;
9005 argtype = TREE_CHAIN (argtype))
9007 tree t = TREE_VALUE (argtype);
9008 ++arity;
9010 if (TYPE_PTR_P (t))
9012 bool maybe_raw_p = false;
9013 t = TREE_TYPE (t);
9014 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9015 return false;
9016 t = TYPE_MAIN_VARIANT (t);
9017 if ((maybe_raw_p = same_type_p (t, char_type_node))
9018 || same_type_p (t, wchar_type_node)
9019 || same_type_p (t, char16_type_node)
9020 || same_type_p (t, char32_type_node))
9022 argtype = TREE_CHAIN (argtype);
9023 if (!argtype)
9024 return false;
9025 t = TREE_VALUE (argtype);
9026 if (maybe_raw_p && argtype == void_list_node)
9027 return true;
9028 else if (same_type_p (t, size_type_node))
9030 ++arity;
9031 continue;
9033 else
9034 return false;
9037 else if (same_type_p (t, long_long_unsigned_type_node))
9039 max_arity = 1;
9040 *long_long_unsigned_p = true;
9042 else if (same_type_p (t, long_double_type_node))
9044 max_arity = 1;
9045 *long_double_p = true;
9047 else if (same_type_p (t, char_type_node))
9048 max_arity = 1;
9049 else if (same_type_p (t, wchar_type_node))
9050 max_arity = 1;
9051 else if (same_type_p (t, char16_type_node))
9052 max_arity = 1;
9053 else if (same_type_p (t, char32_type_node))
9054 max_arity = 1;
9055 else
9056 return false;
9058 if (!argtype)
9059 return false; /* Found ellipsis. */
9061 if (arity != max_arity)
9062 return false;
9064 return true;