2014-01-30 Alangi Derick <alangiderick@gmail.com>
[official-gcc.git] / gcc / cp / typeck.c
blob924937832f578439d8e77a6be9b432db84a6ccef
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "diagnostic.h"
37 #include "intl.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "c-family/c-ubsan.h"
43 #include "params.h"
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
48 tsubst_flags_t, int);
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
50 static tree rationalize_conditional_expr (enum tree_code, tree,
51 tsubst_flags_t);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
58 static bool casts_away_constness (tree, tree, tsubst_flags_t);
59 static void maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
63 tsubst_flags_t);
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
70 tree
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
73 tree type;
75 if (processing_template_decl || value == error_mark_node)
76 return value;
78 if (TREE_CODE (value) == OVERLOAD)
79 type = unknown_type_node;
80 else
81 type = TREE_TYPE (value);
83 if (type == error_mark_node)
84 return error_mark_node;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type))
88 return value;
90 if (complete_type_or_maybe_complain (type, value, complain))
91 return value;
92 else
93 return error_mark_node;
96 tree
97 require_complete_type (tree value)
99 return require_complete_type_sfinae (value, tf_warning_or_error);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
107 tree
108 complete_type (tree type)
110 if (type == NULL_TREE)
111 /* Rather than crash, we return something sure to cause an error
112 at some point. */
113 return error_mark_node;
115 if (type == error_mark_node || COMPLETE_TYPE_P (type))
117 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
119 tree t = complete_type (TREE_TYPE (type));
120 unsigned int needs_constructing, has_nontrivial_dtor;
121 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
122 layout_type (type);
123 needs_constructing
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
125 has_nontrivial_dtor
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
129 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
136 return type;
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
143 tree
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
146 type = complete_type (type);
147 if (type == error_mark_node)
148 /* We already issued an error. */
149 return NULL_TREE;
150 else if (!COMPLETE_TYPE_P (type))
152 if (complain & tf_error)
153 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
154 return NULL_TREE;
156 else
157 return type;
160 tree
161 complete_type_or_else (tree type, tree value)
163 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
166 /* Return truthvalue of whether type of EXP is instantiated. */
169 type_unknown_p (const_tree exp)
171 return (TREE_CODE (exp) == TREE_LIST
172 || TREE_TYPE (exp) == unknown_type_node);
176 /* Return the common type of two parameter lists.
177 We assume that comptypes has already been done and returned 1;
178 if that isn't so, this may crash.
180 As an optimization, free the space we allocate if the parameter
181 lists are already common. */
183 static tree
184 commonparms (tree p1, tree p2)
186 tree oldargs = p1, newargs, n;
187 int i, len;
188 int any_change = 0;
190 len = list_length (p1);
191 newargs = tree_last (p1);
193 if (newargs == void_list_node)
194 i = 1;
195 else
197 i = 0;
198 newargs = 0;
201 for (; i < len; i++)
202 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
204 n = newargs;
206 for (i = 0; p1;
207 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
209 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
212 any_change = 1;
214 else if (! TREE_PURPOSE (p1))
216 if (TREE_PURPOSE (p2))
218 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219 any_change = 1;
222 else
224 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
225 any_change = 1;
226 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
228 if (TREE_VALUE (p1) != TREE_VALUE (p2))
230 any_change = 1;
231 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
233 else
234 TREE_VALUE (n) = TREE_VALUE (p1);
236 if (! any_change)
237 return oldargs;
239 return newargs;
242 /* Given a type, perhaps copied for a typedef,
243 find the "original" version of it. */
244 static tree
245 original_type (tree t)
247 int quals = cp_type_quals (t);
248 while (t != error_mark_node
249 && TYPE_NAME (t) != NULL_TREE)
251 tree x = TYPE_NAME (t);
252 if (TREE_CODE (x) != TYPE_DECL)
253 break;
254 x = DECL_ORIGINAL_TYPE (x);
255 if (x == NULL_TREE)
256 break;
257 t = x;
259 return cp_build_qualified_type (t, quals);
262 /* Return the common type for two arithmetic types T1 and T2 under the
263 usual arithmetic conversions. The default conversions have already
264 been applied, and enumerated types converted to their compatible
265 integer types. */
267 static tree
268 cp_common_type (tree t1, tree t2)
270 enum tree_code code1 = TREE_CODE (t1);
271 enum tree_code code2 = TREE_CODE (t2);
272 tree attributes;
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
277 attributes. */
278 attributes = (*targetm.merge_type_attributes) (t1, t2);
280 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
282 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283 return build_type_attribute_variant (t1, attributes);
284 else
285 return NULL_TREE;
288 /* FIXME: Attributes. */
289 gcc_assert (ARITHMETIC_TYPE_P (t1)
290 || TREE_CODE (t1) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t1));
292 gcc_assert (ARITHMETIC_TYPE_P (t2)
293 || TREE_CODE (t2) == VECTOR_TYPE
294 || UNSCOPED_ENUM_P (t2));
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
298 required type. */
299 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
301 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
303 tree subtype
304 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
306 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307 return build_type_attribute_variant (t1, attributes);
308 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309 return build_type_attribute_variant (t2, attributes);
310 else
311 return build_type_attribute_variant (build_complex_type (subtype),
312 attributes);
315 if (code1 == VECTOR_TYPE)
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1))
320 return build_type_attribute_variant (t1, attributes);
321 else
322 return build_type_attribute_variant (t2, attributes);
325 /* If only one is real, use it as the result. */
326 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327 return build_type_attribute_variant (t1, attributes);
328 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329 return build_type_attribute_variant (t2, attributes);
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333 return build_type_attribute_variant (t1, attributes);
334 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335 return build_type_attribute_variant (t2, attributes);
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339 return build_type_attribute_variant (t1, attributes);
341 if (code1 != REAL_TYPE)
343 /* If one is unsigned long long, then convert the other to unsigned
344 long long. */
345 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347 return build_type_attribute_variant (long_long_unsigned_type_node,
348 attributes);
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
353 other to long long.
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
358 case. */
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
362 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363 ? long_long_unsigned_type_node
364 : long_long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
367 if (int128_integer_type_node != NULL_TREE
368 && (same_type_p (TYPE_MAIN_VARIANT (t1),
369 int128_integer_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2),
371 int128_integer_type_node)))
373 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
374 ? int128_unsigned_type_node
375 : int128_integer_type_node);
376 return build_type_attribute_variant (t, attributes);
379 /* Go through the same procedure, but for longs. */
380 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
381 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
382 return build_type_attribute_variant (long_unsigned_type_node,
383 attributes);
384 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? long_unsigned_type_node : long_integer_type_node);
389 return build_type_attribute_variant (t, attributes);
391 /* Otherwise prefer the unsigned one. */
392 if (TYPE_UNSIGNED (t1))
393 return build_type_attribute_variant (t1, attributes);
394 else
395 return build_type_attribute_variant (t2, attributes);
397 else
399 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
400 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
401 return build_type_attribute_variant (long_double_type_node,
402 attributes);
403 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
405 return build_type_attribute_variant (double_type_node,
406 attributes);
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
409 return build_type_attribute_variant (float_type_node,
410 attributes);
412 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
413 the standard C++ floating-point types. Logic earlier in this
414 function has already eliminated the possibility that
415 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
416 compelling reason to choose one or the other. */
417 return build_type_attribute_variant (t1, attributes);
421 /* T1 and T2 are arithmetic or enumeration types. Return the type
422 that will result from the "usual arithmetic conversions" on T1 and
423 T2 as described in [expr]. */
425 tree
426 type_after_usual_arithmetic_conversions (tree t1, tree t2)
428 gcc_assert (ARITHMETIC_TYPE_P (t1)
429 || TREE_CODE (t1) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t1));
431 gcc_assert (ARITHMETIC_TYPE_P (t2)
432 || TREE_CODE (t2) == VECTOR_TYPE
433 || UNSCOPED_ENUM_P (t2));
435 /* Perform the integral promotions. We do not promote real types here. */
436 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
437 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
439 t1 = type_promotes_to (t1);
440 t2 = type_promotes_to (t2);
443 return cp_common_type (t1, t2);
446 static void
447 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
448 composite_pointer_operation operation)
450 switch (operation)
452 case CPO_COMPARISON:
453 emit_diagnostic (kind, input_location, 0,
454 "comparison between "
455 "distinct pointer types %qT and %qT lacks a cast",
456 t1, t2);
457 break;
458 case CPO_CONVERSION:
459 emit_diagnostic (kind, input_location, 0,
460 "conversion between "
461 "distinct pointer types %qT and %qT lacks a cast",
462 t1, t2);
463 break;
464 case CPO_CONDITIONAL_EXPR:
465 emit_diagnostic (kind, input_location, 0,
466 "conditional expression between "
467 "distinct pointer types %qT and %qT lacks a cast",
468 t1, t2);
469 break;
470 default:
471 gcc_unreachable ();
475 /* Subroutine of composite_pointer_type to implement the recursive
476 case. See that function for documentation of the parameters. */
478 static tree
479 composite_pointer_type_r (tree t1, tree t2,
480 composite_pointer_operation operation,
481 tsubst_flags_t complain)
483 tree pointee1;
484 tree pointee2;
485 tree result_type;
486 tree attributes;
488 /* Determine the types pointed to by T1 and T2. */
489 if (TYPE_PTR_P (t1))
491 pointee1 = TREE_TYPE (t1);
492 pointee2 = TREE_TYPE (t2);
494 else
496 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
497 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
500 /* [expr.rel]
502 Otherwise, the composite pointer type is a pointer type
503 similar (_conv.qual_) to the type of one of the operands,
504 with a cv-qualification signature (_conv.qual_) that is the
505 union of the cv-qualification signatures of the operand
506 types. */
507 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
508 result_type = pointee1;
509 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
510 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
512 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
513 complain);
514 if (result_type == error_mark_node)
515 return error_mark_node;
517 else
519 if (complain & tf_error)
520 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
521 else
522 return error_mark_node;
523 result_type = void_type_node;
525 result_type = cp_build_qualified_type (result_type,
526 (cp_type_quals (pointee1)
527 | cp_type_quals (pointee2)));
528 /* If the original types were pointers to members, so is the
529 result. */
530 if (TYPE_PTRMEM_P (t1))
532 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
533 TYPE_PTRMEM_CLASS_TYPE (t2)))
535 if (complain & tf_error)
536 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
537 else
538 return error_mark_node;
540 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
541 result_type);
543 else
544 result_type = build_pointer_type (result_type);
546 /* Merge the attributes. */
547 attributes = (*targetm.merge_type_attributes) (t1, t2);
548 return build_type_attribute_variant (result_type, attributes);
551 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
552 ARG1 and ARG2 are the values with those types. The OPERATION is to
553 describe the operation between the pointer types,
554 in case an error occurs.
556 This routine also implements the computation of a common type for
557 pointers-to-members as per [expr.eq]. */
559 tree
560 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
561 composite_pointer_operation operation,
562 tsubst_flags_t complain)
564 tree class1;
565 tree class2;
567 /* [expr.rel]
569 If one operand is a null pointer constant, the composite pointer
570 type is the type of the other operand. */
571 if (null_ptr_cst_p (arg1))
572 return t2;
573 if (null_ptr_cst_p (arg2))
574 return t1;
576 /* We have:
578 [expr.rel]
580 If one of the operands has type "pointer to cv1 void*", then
581 the other has type "pointer to cv2T", and the composite pointer
582 type is "pointer to cv12 void", where cv12 is the union of cv1
583 and cv2.
585 If either type is a pointer to void, make sure it is T1. */
586 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
588 tree t;
589 t = t1;
590 t1 = t2;
591 t2 = t;
594 /* Now, if T1 is a pointer to void, merge the qualifiers. */
595 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
597 tree attributes;
598 tree result_type;
600 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
602 switch (operation)
604 case CPO_COMPARISON:
605 pedwarn (input_location, OPT_Wpedantic,
606 "ISO C++ forbids comparison between "
607 "pointer of type %<void *%> and pointer-to-function");
608 break;
609 case CPO_CONVERSION:
610 pedwarn (input_location, OPT_Wpedantic,
611 "ISO C++ forbids conversion between "
612 "pointer of type %<void *%> and pointer-to-function");
613 break;
614 case CPO_CONDITIONAL_EXPR:
615 pedwarn (input_location, OPT_Wpedantic,
616 "ISO C++ forbids conditional expression between "
617 "pointer of type %<void *%> and pointer-to-function");
618 break;
619 default:
620 gcc_unreachable ();
623 result_type
624 = cp_build_qualified_type (void_type_node,
625 (cp_type_quals (TREE_TYPE (t1))
626 | cp_type_quals (TREE_TYPE (t2))));
627 result_type = build_pointer_type (result_type);
628 /* Merge the attributes. */
629 attributes = (*targetm.merge_type_attributes) (t1, t2);
630 return build_type_attribute_variant (result_type, attributes);
633 if (c_dialect_objc () && TYPE_PTR_P (t1)
634 && TYPE_PTR_P (t2))
636 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
637 return objc_common_type (t1, t2);
640 /* [expr.eq] permits the application of a pointer conversion to
641 bring the pointers to a common type. */
642 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
643 && CLASS_TYPE_P (TREE_TYPE (t1))
644 && CLASS_TYPE_P (TREE_TYPE (t2))
645 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
646 TREE_TYPE (t2)))
648 class1 = TREE_TYPE (t1);
649 class2 = TREE_TYPE (t2);
651 if (DERIVED_FROM_P (class1, class2))
652 t2 = (build_pointer_type
653 (cp_build_qualified_type (class1, cp_type_quals (class2))));
654 else if (DERIVED_FROM_P (class2, class1))
655 t1 = (build_pointer_type
656 (cp_build_qualified_type (class2, cp_type_quals (class1))));
657 else
659 if (complain & tf_error)
660 composite_pointer_error (DK_ERROR, t1, t2, operation);
661 return error_mark_node;
664 /* [expr.eq] permits the application of a pointer-to-member
665 conversion to change the class type of one of the types. */
666 else if (TYPE_PTRMEM_P (t1)
667 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
668 TYPE_PTRMEM_CLASS_TYPE (t2)))
670 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
671 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
673 if (DERIVED_FROM_P (class1, class2))
674 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
675 else if (DERIVED_FROM_P (class2, class1))
676 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
677 else
679 if (complain & tf_error)
680 switch (operation)
682 case CPO_COMPARISON:
683 error ("comparison between distinct "
684 "pointer-to-member types %qT and %qT lacks a cast",
685 t1, t2);
686 break;
687 case CPO_CONVERSION:
688 error ("conversion between distinct "
689 "pointer-to-member types %qT and %qT lacks a cast",
690 t1, t2);
691 break;
692 case CPO_CONDITIONAL_EXPR:
693 error ("conditional expression between distinct "
694 "pointer-to-member types %qT and %qT lacks a cast",
695 t1, t2);
696 break;
697 default:
698 gcc_unreachable ();
700 return error_mark_node;
704 return composite_pointer_type_r (t1, t2, operation, complain);
707 /* Return the merged type of two types.
708 We assume that comptypes has already been done and returned 1;
709 if that isn't so, this may crash.
711 This just combines attributes and default arguments; any other
712 differences would cause the two types to compare unalike. */
714 tree
715 merge_types (tree t1, tree t2)
717 enum tree_code code1;
718 enum tree_code code2;
719 tree attributes;
721 /* Save time if the two types are the same. */
722 if (t1 == t2)
723 return t1;
724 if (original_type (t1) == original_type (t2))
725 return t1;
727 /* If one type is nonsense, use the other. */
728 if (t1 == error_mark_node)
729 return t2;
730 if (t2 == error_mark_node)
731 return t1;
733 /* Handle merging an auto redeclaration with a previous deduced
734 return type. */
735 if (is_auto (t1))
736 return t2;
738 /* Merge the attributes. */
739 attributes = (*targetm.merge_type_attributes) (t1, t2);
741 if (TYPE_PTRMEMFUNC_P (t1))
742 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
743 if (TYPE_PTRMEMFUNC_P (t2))
744 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
746 code1 = TREE_CODE (t1);
747 code2 = TREE_CODE (t2);
748 if (code1 != code2)
750 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
751 if (code1 == TYPENAME_TYPE)
753 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
754 code1 = TREE_CODE (t1);
756 else
758 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
759 code2 = TREE_CODE (t2);
763 switch (code1)
765 case POINTER_TYPE:
766 case REFERENCE_TYPE:
767 /* For two pointers, do this recursively on the target type. */
769 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
770 int quals = cp_type_quals (t1);
772 if (code1 == POINTER_TYPE)
773 t1 = build_pointer_type (target);
774 else
775 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
776 t1 = build_type_attribute_variant (t1, attributes);
777 t1 = cp_build_qualified_type (t1, quals);
779 if (TREE_CODE (target) == METHOD_TYPE)
780 t1 = build_ptrmemfunc_type (t1);
782 return t1;
785 case OFFSET_TYPE:
787 int quals;
788 tree pointee;
789 quals = cp_type_quals (t1);
790 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
791 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
792 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
793 pointee);
794 t1 = cp_build_qualified_type (t1, quals);
795 break;
798 case ARRAY_TYPE:
800 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
801 /* Save space: see if the result is identical to one of the args. */
802 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
803 return build_type_attribute_variant (t1, attributes);
804 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
805 return build_type_attribute_variant (t2, attributes);
806 /* Merge the element types, and have a size if either arg has one. */
807 t1 = build_cplus_array_type
808 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
809 break;
812 case FUNCTION_TYPE:
813 /* Function types: prefer the one that specified arg types.
814 If both do, merge the arg types. Also merge the return types. */
816 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
817 tree p1 = TYPE_ARG_TYPES (t1);
818 tree p2 = TYPE_ARG_TYPES (t2);
819 tree parms;
820 tree rval, raises;
822 /* Save space: see if the result is identical to one of the args. */
823 if (valtype == TREE_TYPE (t1) && ! p2)
824 return cp_build_type_attribute_variant (t1, attributes);
825 if (valtype == TREE_TYPE (t2) && ! p1)
826 return cp_build_type_attribute_variant (t2, attributes);
828 /* Simple way if one arg fails to specify argument types. */
829 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
830 parms = p2;
831 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
832 parms = p1;
833 else
834 parms = commonparms (p1, p2);
836 rval = build_function_type (valtype, parms);
837 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
838 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
839 rval = apply_memfn_quals (rval,
840 type_memfn_quals (t1),
841 type_memfn_rqual (t1));
842 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
843 TYPE_RAISES_EXCEPTIONS (t2),
844 NULL_TREE);
845 t1 = build_exception_variant (rval, raises);
846 break;
849 case METHOD_TYPE:
851 /* Get this value the long way, since TYPE_METHOD_BASETYPE
852 is just the main variant of this. */
853 tree basetype = class_of_this_parm (t2);
854 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
855 TYPE_RAISES_EXCEPTIONS (t2),
856 NULL_TREE);
857 cp_ref_qualifier rqual = type_memfn_rqual (t1);
858 tree t3;
860 /* If this was a member function type, get back to the
861 original type of type member function (i.e., without
862 the class instance variable up front. */
863 t1 = build_function_type (TREE_TYPE (t1),
864 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
865 t2 = build_function_type (TREE_TYPE (t2),
866 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
867 t3 = merge_types (t1, t2);
868 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
869 TYPE_ARG_TYPES (t3));
870 t1 = build_exception_variant (t3, raises);
871 t1 = build_ref_qualified_type (t1, rqual);
872 break;
875 case TYPENAME_TYPE:
876 /* There is no need to merge attributes into a TYPENAME_TYPE.
877 When the type is instantiated it will have whatever
878 attributes result from the instantiation. */
879 return t1;
881 default:;
884 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
885 return t1;
886 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
887 return t2;
888 else
889 return cp_build_type_attribute_variant (t1, attributes);
892 /* Return the ARRAY_TYPE type without its domain. */
894 tree
895 strip_array_domain (tree type)
897 tree t2;
898 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
899 if (TYPE_DOMAIN (type) == NULL_TREE)
900 return type;
901 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
902 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
905 /* Wrapper around cp_common_type that is used by c-common.c and other
906 front end optimizations that remove promotions.
908 Return the common type for two arithmetic types T1 and T2 under the
909 usual arithmetic conversions. The default conversions have already
910 been applied, and enumerated types converted to their compatible
911 integer types. */
913 tree
914 common_type (tree t1, tree t2)
916 /* If one type is nonsense, use the other */
917 if (t1 == error_mark_node)
918 return t2;
919 if (t2 == error_mark_node)
920 return t1;
922 return cp_common_type (t1, t2);
925 /* Return the common type of two pointer types T1 and T2. This is the
926 type for the result of most arithmetic operations if the operands
927 have the given two types.
929 We assume that comp_target_types has already been done and returned
930 nonzero; if that isn't so, this may crash. */
932 tree
933 common_pointer_type (tree t1, tree t2)
935 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
936 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
937 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
939 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
940 CPO_CONVERSION, tf_warning_or_error);
943 /* Compare two exception specifier types for exactness or subsetness, if
944 allowed. Returns false for mismatch, true for match (same, or
945 derived and !exact).
947 [except.spec] "If a class X ... objects of class X or any class publicly
948 and unambiguously derived from X. Similarly, if a pointer type Y * ...
949 exceptions of type Y * or that are pointers to any type publicly and
950 unambiguously derived from Y. Otherwise a function only allows exceptions
951 that have the same type ..."
952 This does not mention cv qualifiers and is different to what throw
953 [except.throw] and catch [except.catch] will do. They will ignore the
954 top level cv qualifiers, and allow qualifiers in the pointer to class
955 example.
957 We implement the letter of the standard. */
959 static bool
960 comp_except_types (tree a, tree b, bool exact)
962 if (same_type_p (a, b))
963 return true;
964 else if (!exact)
966 if (cp_type_quals (a) || cp_type_quals (b))
967 return false;
969 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
971 a = TREE_TYPE (a);
972 b = TREE_TYPE (b);
973 if (cp_type_quals (a) || cp_type_quals (b))
974 return false;
977 if (TREE_CODE (a) != RECORD_TYPE
978 || TREE_CODE (b) != RECORD_TYPE)
979 return false;
981 if (publicly_uniquely_derived_p (a, b))
982 return true;
984 return false;
987 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
988 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
989 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
990 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
991 are unordered, but we've already filtered out duplicates. Most lists will
992 be in order, we should try to make use of that. */
994 bool
995 comp_except_specs (const_tree t1, const_tree t2, int exact)
997 const_tree probe;
998 const_tree base;
999 int length = 0;
1001 if (t1 == t2)
1002 return true;
1004 /* First handle noexcept. */
1005 if (exact < ce_exact)
1007 /* noexcept(false) is compatible with no exception-specification,
1008 and stricter than any spec. */
1009 if (t1 == noexcept_false_spec)
1010 return t2 == NULL_TREE || exact == ce_derived;
1011 /* Even a derived noexcept(false) is compatible with no
1012 exception-specification. */
1013 if (t2 == noexcept_false_spec)
1014 return t1 == NULL_TREE;
1016 /* Otherwise, if we aren't looking for an exact match, noexcept is
1017 equivalent to throw(). */
1018 if (t1 == noexcept_true_spec)
1019 t1 = empty_except_spec;
1020 if (t2 == noexcept_true_spec)
1021 t2 = empty_except_spec;
1024 /* If any noexcept is left, it is only comparable to itself;
1025 either we're looking for an exact match or we're redeclaring a
1026 template with dependent noexcept. */
1027 if ((t1 && TREE_PURPOSE (t1))
1028 || (t2 && TREE_PURPOSE (t2)))
1029 return (t1 && t2
1030 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1032 if (t1 == NULL_TREE) /* T1 is ... */
1033 return t2 == NULL_TREE || exact == ce_derived;
1034 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1035 return t2 != NULL_TREE && !TREE_VALUE (t2);
1036 if (t2 == NULL_TREE) /* T2 is ... */
1037 return false;
1038 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1039 return exact == ce_derived;
1041 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1042 Count how many we find, to determine exactness. For exact matching and
1043 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1044 O(nm). */
1045 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1047 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1049 tree a = TREE_VALUE (probe);
1050 tree b = TREE_VALUE (t2);
1052 if (comp_except_types (a, b, exact))
1054 if (probe == base && exact > ce_derived)
1055 base = TREE_CHAIN (probe);
1056 length++;
1057 break;
1060 if (probe == NULL_TREE)
1061 return false;
1063 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1066 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1067 [] can match [size]. */
1069 static bool
1070 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1072 tree d1;
1073 tree d2;
1074 tree max1, max2;
1076 if (t1 == t2)
1077 return true;
1079 /* The type of the array elements must be the same. */
1080 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1081 return false;
1083 d1 = TYPE_DOMAIN (t1);
1084 d2 = TYPE_DOMAIN (t2);
1086 if (d1 == d2)
1087 return true;
1089 /* If one of the arrays is dimensionless, and the other has a
1090 dimension, they are of different types. However, it is valid to
1091 write:
1093 extern int a[];
1094 int a[3];
1096 by [basic.link]:
1098 declarations for an array object can specify
1099 array types that differ by the presence or absence of a major
1100 array bound (_dcl.array_). */
1101 if (!d1 || !d2)
1102 return allow_redeclaration;
1104 /* Check that the dimensions are the same. */
1106 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1107 return false;
1108 max1 = TYPE_MAX_VALUE (d1);
1109 max2 = TYPE_MAX_VALUE (d2);
1110 if (processing_template_decl && !abi_version_at_least (2)
1111 && !value_dependent_expression_p (max1)
1112 && !value_dependent_expression_p (max2))
1114 /* With abi-1 we do not fold non-dependent array bounds, (and
1115 consequently mangle them incorrectly). We must therefore
1116 fold them here, to verify the domains have the same
1117 value. */
1118 max1 = fold (max1);
1119 max2 = fold (max2);
1122 if (!cp_tree_equal (max1, max2))
1123 return false;
1125 return true;
1128 /* Compare the relative position of T1 and T2 into their respective
1129 template parameter list.
1130 T1 and T2 must be template parameter types.
1131 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1133 static bool
1134 comp_template_parms_position (tree t1, tree t2)
1136 tree index1, index2;
1137 gcc_assert (t1 && t2
1138 && TREE_CODE (t1) == TREE_CODE (t2)
1139 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1140 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1141 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1143 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1144 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1146 /* Then compare their relative position. */
1147 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1148 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1149 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1150 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1151 return false;
1153 return true;
1156 /* Subroutine in comptypes. */
1158 static bool
1159 structural_comptypes (tree t1, tree t2, int strict)
1161 if (t1 == t2)
1162 return true;
1164 /* Suppress errors caused by previously reported errors. */
1165 if (t1 == error_mark_node || t2 == error_mark_node)
1166 return false;
1168 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1170 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1171 current instantiation. */
1172 if (TREE_CODE (t1) == TYPENAME_TYPE)
1173 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1175 if (TREE_CODE (t2) == TYPENAME_TYPE)
1176 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1178 if (TYPE_PTRMEMFUNC_P (t1))
1179 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1180 if (TYPE_PTRMEMFUNC_P (t2))
1181 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1183 /* Different classes of types can't be compatible. */
1184 if (TREE_CODE (t1) != TREE_CODE (t2))
1185 return false;
1187 /* Qualifiers must match. For array types, we will check when we
1188 recur on the array element types. */
1189 if (TREE_CODE (t1) != ARRAY_TYPE
1190 && cp_type_quals (t1) != cp_type_quals (t2))
1191 return false;
1192 if (TREE_CODE (t1) == FUNCTION_TYPE
1193 && type_memfn_quals (t1) != type_memfn_quals (t2))
1194 return false;
1195 /* Need to check this before TYPE_MAIN_VARIANT.
1196 FIXME function qualifiers should really change the main variant. */
1197 if ((TREE_CODE (t1) == FUNCTION_TYPE
1198 || TREE_CODE (t1) == METHOD_TYPE)
1199 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1200 return false;
1201 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1202 return false;
1204 /* Allow for two different type nodes which have essentially the same
1205 definition. Note that we already checked for equality of the type
1206 qualifiers (just above). */
1208 if (TREE_CODE (t1) != ARRAY_TYPE
1209 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1210 return true;
1213 /* Compare the types. Break out if they could be the same. */
1214 switch (TREE_CODE (t1))
1216 case VOID_TYPE:
1217 case BOOLEAN_TYPE:
1218 /* All void and bool types are the same. */
1219 break;
1221 case INTEGER_TYPE:
1222 case FIXED_POINT_TYPE:
1223 case REAL_TYPE:
1224 /* With these nodes, we can't determine type equivalence by
1225 looking at what is stored in the nodes themselves, because
1226 two nodes might have different TYPE_MAIN_VARIANTs but still
1227 represent the same type. For example, wchar_t and int could
1228 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1229 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1230 and are distinct types. On the other hand, int and the
1231 following typedef
1233 typedef int INT __attribute((may_alias));
1235 have identical properties, different TYPE_MAIN_VARIANTs, but
1236 represent the same type. The canonical type system keeps
1237 track of equivalence in this case, so we fall back on it. */
1238 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1240 case TEMPLATE_TEMPLATE_PARM:
1241 case BOUND_TEMPLATE_TEMPLATE_PARM:
1242 if (!comp_template_parms_position (t1, t2))
1243 return false;
1244 if (!comp_template_parms
1245 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1246 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1247 return false;
1248 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1249 break;
1250 /* Don't check inheritance. */
1251 strict = COMPARE_STRICT;
1252 /* Fall through. */
1254 case RECORD_TYPE:
1255 case UNION_TYPE:
1256 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1257 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1258 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1259 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1260 break;
1262 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1263 break;
1264 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1265 break;
1267 return false;
1269 case OFFSET_TYPE:
1270 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1271 strict & ~COMPARE_REDECLARATION))
1272 return false;
1273 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1274 return false;
1275 break;
1277 case REFERENCE_TYPE:
1278 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1279 return false;
1280 /* fall through to checks for pointer types */
1282 case POINTER_TYPE:
1283 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1284 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1285 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1286 return false;
1287 break;
1289 case METHOD_TYPE:
1290 case FUNCTION_TYPE:
1291 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1292 return false;
1293 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1294 return false;
1295 break;
1297 case ARRAY_TYPE:
1298 /* Target types must match incl. qualifiers. */
1299 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1300 return false;
1301 break;
1303 case TEMPLATE_TYPE_PARM:
1304 /* If T1 and T2 don't have the same relative position in their
1305 template parameters set, they can't be equal. */
1306 if (!comp_template_parms_position (t1, t2))
1307 return false;
1308 break;
1310 case TYPENAME_TYPE:
1311 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1312 TYPENAME_TYPE_FULLNAME (t2)))
1313 return false;
1314 /* Qualifiers don't matter on scopes. */
1315 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1316 TYPE_CONTEXT (t2)))
1317 return false;
1318 break;
1320 case UNBOUND_CLASS_TEMPLATE:
1321 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1322 return false;
1323 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1324 return false;
1325 break;
1327 case COMPLEX_TYPE:
1328 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1329 return false;
1330 break;
1332 case VECTOR_TYPE:
1333 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1334 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1335 return false;
1336 break;
1338 case TYPE_PACK_EXPANSION:
1339 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1340 PACK_EXPANSION_PATTERN (t2))
1341 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1342 PACK_EXPANSION_EXTRA_ARGS (t2)));
1344 case DECLTYPE_TYPE:
1345 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1346 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1347 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1348 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1349 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1350 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1351 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1352 DECLTYPE_TYPE_EXPR (t2)))
1353 return false;
1354 break;
1356 case UNDERLYING_TYPE:
1357 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1358 UNDERLYING_TYPE_TYPE (t2));
1360 default:
1361 return false;
1364 /* If we get here, we know that from a target independent POV the
1365 types are the same. Make sure the target attributes are also
1366 the same. */
1367 return comp_type_attributes (t1, t2);
1370 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1371 is a bitwise-or of the COMPARE_* flags. */
1373 bool
1374 comptypes (tree t1, tree t2, int strict)
1376 if (strict == COMPARE_STRICT)
1378 if (t1 == t2)
1379 return true;
1381 if (t1 == error_mark_node || t2 == error_mark_node)
1382 return false;
1384 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1385 /* At least one of the types requires structural equality, so
1386 perform a deep check. */
1387 return structural_comptypes (t1, t2, strict);
1389 #ifdef ENABLE_CHECKING
1390 if (USE_CANONICAL_TYPES)
1392 bool result = structural_comptypes (t1, t2, strict);
1394 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1395 /* The two types are structurally equivalent, but their
1396 canonical types were different. This is a failure of the
1397 canonical type propagation code.*/
1398 internal_error
1399 ("canonical types differ for identical types %T and %T",
1400 t1, t2);
1401 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1402 /* Two types are structurally different, but the canonical
1403 types are the same. This means we were over-eager in
1404 assigning canonical types. */
1405 internal_error
1406 ("same canonical type node for different types %T and %T",
1407 t1, t2);
1409 return result;
1411 #else
1412 if (USE_CANONICAL_TYPES)
1413 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1414 #endif
1415 else
1416 return structural_comptypes (t1, t2, strict);
1418 else if (strict == COMPARE_STRUCTURAL)
1419 return structural_comptypes (t1, t2, COMPARE_STRICT);
1420 else
1421 return structural_comptypes (t1, t2, strict);
1424 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1425 top-level qualifiers. */
1427 bool
1428 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1430 if (type1 == error_mark_node || type2 == error_mark_node)
1431 return false;
1433 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1436 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1438 bool
1439 at_least_as_qualified_p (const_tree type1, const_tree type2)
1441 int q1 = cp_type_quals (type1);
1442 int q2 = cp_type_quals (type2);
1444 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1445 return (q1 & q2) == q2;
1448 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1449 more cv-qualified that TYPE1, and 0 otherwise. */
1452 comp_cv_qualification (const_tree type1, const_tree type2)
1454 int q1 = cp_type_quals (type1);
1455 int q2 = cp_type_quals (type2);
1457 if (q1 == q2)
1458 return 0;
1460 if ((q1 & q2) == q2)
1461 return 1;
1462 else if ((q1 & q2) == q1)
1463 return -1;
1465 return 0;
1468 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1469 subset of the cv-qualification signature of TYPE2, and the types
1470 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1473 comp_cv_qual_signature (tree type1, tree type2)
1475 if (comp_ptr_ttypes_real (type2, type1, -1))
1476 return 1;
1477 else if (comp_ptr_ttypes_real (type1, type2, -1))
1478 return -1;
1479 else
1480 return 0;
1483 /* Subroutines of `comptypes'. */
1485 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1486 equivalent in the sense that functions with those parameter types
1487 can have equivalent types. The two lists must be equivalent,
1488 element by element. */
1490 bool
1491 compparms (const_tree parms1, const_tree parms2)
1493 const_tree t1, t2;
1495 /* An unspecified parmlist matches any specified parmlist
1496 whose argument types don't need default promotions. */
1498 for (t1 = parms1, t2 = parms2;
1499 t1 || t2;
1500 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1502 /* If one parmlist is shorter than the other,
1503 they fail to match. */
1504 if (!t1 || !t2)
1505 return false;
1506 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1507 return false;
1509 return true;
1513 /* Process a sizeof or alignof expression where the operand is a
1514 type. */
1516 tree
1517 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1519 tree value;
1520 bool dependent_p;
1522 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1523 if (type == error_mark_node)
1524 return error_mark_node;
1526 type = non_reference (type);
1527 if (TREE_CODE (type) == METHOD_TYPE)
1529 if (complain)
1530 pedwarn (input_location, OPT_Wpointer_arith,
1531 "invalid application of %qs to a member function",
1532 operator_name_info[(int) op].name);
1533 value = size_one_node;
1536 dependent_p = dependent_type_p (type);
1537 if (!dependent_p)
1538 complete_type (type);
1539 if (dependent_p
1540 /* VLA types will have a non-constant size. In the body of an
1541 uninstantiated template, we don't need to try to compute the
1542 value, because the sizeof expression is not an integral
1543 constant expression in that case. And, if we do try to
1544 compute the value, we'll likely end up with SAVE_EXPRs, which
1545 the template substitution machinery does not expect to see. */
1546 || (processing_template_decl
1547 && COMPLETE_TYPE_P (type)
1548 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1550 value = build_min (op, size_type_node, type);
1551 TREE_READONLY (value) = 1;
1552 return value;
1555 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type))
1557 if (complain & tf_warning_or_error)
1558 pedwarn (input_location, OPT_Wvla,
1559 "taking sizeof array of runtime bound");
1560 else
1561 return error_mark_node;
1564 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1565 op == SIZEOF_EXPR, false,
1566 complain);
1569 /* Return the size of the type, without producing any warnings for
1570 types whose size cannot be taken. This routine should be used only
1571 in some other routine that has already produced a diagnostic about
1572 using the size of such a type. */
1573 tree
1574 cxx_sizeof_nowarn (tree type)
1576 if (TREE_CODE (type) == FUNCTION_TYPE
1577 || VOID_TYPE_P (type)
1578 || TREE_CODE (type) == ERROR_MARK)
1579 return size_one_node;
1580 else if (!COMPLETE_TYPE_P (type))
1581 return size_zero_node;
1582 else
1583 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1586 /* Process a sizeof expression where the operand is an expression. */
1588 static tree
1589 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1591 if (e == error_mark_node)
1592 return error_mark_node;
1594 if (processing_template_decl)
1596 e = build_min (SIZEOF_EXPR, size_type_node, e);
1597 TREE_SIDE_EFFECTS (e) = 0;
1598 TREE_READONLY (e) = 1;
1600 return e;
1603 /* To get the size of a static data member declared as an array of
1604 unknown bound, we need to instantiate it. */
1605 if (VAR_P (e)
1606 && VAR_HAD_UNKNOWN_BOUND (e)
1607 && DECL_TEMPLATE_INSTANTIATION (e))
1608 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1610 e = mark_type_use (e);
1612 if (TREE_CODE (e) == COMPONENT_REF
1613 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1614 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1616 if (complain & tf_error)
1617 error ("invalid application of %<sizeof%> to a bit-field");
1618 else
1619 return error_mark_node;
1620 e = char_type_node;
1622 else if (is_overloaded_fn (e))
1624 if (complain & tf_error)
1625 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1626 "function type");
1627 else
1628 return error_mark_node;
1629 e = char_type_node;
1631 else if (type_unknown_p (e))
1633 if (complain & tf_error)
1634 cxx_incomplete_type_error (e, TREE_TYPE (e));
1635 else
1636 return error_mark_node;
1637 e = char_type_node;
1639 else
1640 e = TREE_TYPE (e);
1642 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1645 /* Implement the __alignof keyword: Return the minimum required
1646 alignment of E, measured in bytes. For VAR_DECL's and
1647 FIELD_DECL's return DECL_ALIGN (which can be set from an
1648 "aligned" __attribute__ specification). */
1650 static tree
1651 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1653 tree t;
1655 if (e == error_mark_node)
1656 return error_mark_node;
1658 if (processing_template_decl)
1660 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1661 TREE_SIDE_EFFECTS (e) = 0;
1662 TREE_READONLY (e) = 1;
1664 return e;
1667 e = mark_type_use (e);
1669 if (VAR_P (e))
1670 t = size_int (DECL_ALIGN_UNIT (e));
1671 else if (TREE_CODE (e) == COMPONENT_REF
1672 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1673 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1675 if (complain & tf_error)
1676 error ("invalid application of %<__alignof%> to a bit-field");
1677 else
1678 return error_mark_node;
1679 t = size_one_node;
1681 else if (TREE_CODE (e) == COMPONENT_REF
1682 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1683 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1684 else if (is_overloaded_fn (e))
1686 if (complain & tf_error)
1687 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1688 "function type");
1689 else
1690 return error_mark_node;
1691 if (TREE_CODE (e) == FUNCTION_DECL)
1692 t = size_int (DECL_ALIGN_UNIT (e));
1693 else
1694 t = size_one_node;
1696 else if (type_unknown_p (e))
1698 if (complain & tf_error)
1699 cxx_incomplete_type_error (e, TREE_TYPE (e));
1700 else
1701 return error_mark_node;
1702 t = size_one_node;
1704 else
1705 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1706 complain & tf_error);
1708 return fold_convert (size_type_node, t);
1711 /* Process a sizeof or alignof expression E with code OP where the operand
1712 is an expression. */
1714 tree
1715 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1717 if (op == SIZEOF_EXPR)
1718 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1719 else
1720 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1723 /* Build a representation of an expression 'alignas(E).' Return the
1724 folded integer value of E if it is an integral constant expression
1725 that resolves to a valid alignment. If E depends on a template
1726 parameter, return a syntactic representation tree of kind
1727 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1728 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1730 tree
1731 cxx_alignas_expr (tree e)
1733 if (e == NULL_TREE || e == error_mark_node
1734 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1735 return e;
1737 if (TYPE_P (e))
1738 /* [dcl.align]/3:
1740 When the alignment-specifier is of the form
1741 alignas(type-id ), it shall have the same effect as
1742 alignas(alignof(type-id )). */
1744 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1746 /* If we reach this point, it means the alignas expression if of
1747 the form "alignas(assignment-expression)", so we should follow
1748 what is stated by [dcl.align]/2. */
1750 if (value_dependent_expression_p (e))
1751 /* Leave value-dependent expression alone for now. */
1752 return e;
1754 e = fold_non_dependent_expr (e);
1755 e = mark_rvalue_use (e);
1757 /* [dcl.align]/2 says:
1759 the assignment-expression shall be an integral constant
1760 expression. */
1762 return cxx_constant_value (e);
1766 /* EXPR is being used in a context that is not a function call.
1767 Enforce:
1769 [expr.ref]
1771 The expression can be used only as the left-hand operand of a
1772 member function call.
1774 [expr.mptr.operator]
1776 If the result of .* or ->* is a function, then that result can be
1777 used only as the operand for the function call operator ().
1779 by issuing an error message if appropriate. Returns true iff EXPR
1780 violates these rules. */
1782 bool
1783 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1785 if (expr == NULL_TREE)
1786 return false;
1787 /* Don't enforce this in MS mode. */
1788 if (flag_ms_extensions)
1789 return false;
1790 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1791 expr = get_first_fn (expr);
1792 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1794 if (complain & tf_error)
1795 error ("invalid use of non-static member function");
1796 return true;
1798 return false;
1801 /* If EXP is a reference to a bitfield, and the type of EXP does not
1802 match the declared type of the bitfield, return the declared type
1803 of the bitfield. Otherwise, return NULL_TREE. */
1805 tree
1806 is_bitfield_expr_with_lowered_type (const_tree exp)
1808 switch (TREE_CODE (exp))
1810 case COND_EXPR:
1811 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1812 ? TREE_OPERAND (exp, 1)
1813 : TREE_OPERAND (exp, 0)))
1814 return NULL_TREE;
1815 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1817 case COMPOUND_EXPR:
1818 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1820 case MODIFY_EXPR:
1821 case SAVE_EXPR:
1822 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1824 case COMPONENT_REF:
1826 tree field;
1828 field = TREE_OPERAND (exp, 1);
1829 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1830 return NULL_TREE;
1831 if (same_type_ignoring_top_level_qualifiers_p
1832 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1833 return NULL_TREE;
1834 return DECL_BIT_FIELD_TYPE (field);
1837 CASE_CONVERT:
1838 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1839 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1840 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1841 /* Fallthrough. */
1843 default:
1844 return NULL_TREE;
1848 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1849 bitfield with a lowered type, the type of EXP is returned, rather
1850 than NULL_TREE. */
1852 tree
1853 unlowered_expr_type (const_tree exp)
1855 tree type;
1856 tree etype = TREE_TYPE (exp);
1858 type = is_bitfield_expr_with_lowered_type (exp);
1859 if (type)
1860 type = cp_build_qualified_type (type, cp_type_quals (etype));
1861 else
1862 type = etype;
1864 return type;
1867 /* Perform the conversions in [expr] that apply when an lvalue appears
1868 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1869 function-to-pointer conversions. In addition, manifest constants
1870 are replaced by their values, and bitfield references are converted
1871 to their declared types. Note that this function does not perform the
1872 lvalue-to-rvalue conversion for class types. If you need that conversion
1873 to for class types, then you probably need to use force_rvalue.
1875 Although the returned value is being used as an rvalue, this
1876 function does not wrap the returned expression in a
1877 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1878 that the return value is no longer an lvalue. */
1880 tree
1881 decay_conversion (tree exp, tsubst_flags_t complain)
1883 tree type;
1884 enum tree_code code;
1885 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1887 type = TREE_TYPE (exp);
1888 if (type == error_mark_node)
1889 return error_mark_node;
1891 exp = mark_rvalue_use (exp);
1893 exp = resolve_nondeduced_context (exp);
1894 if (type_unknown_p (exp))
1896 if (complain & tf_error)
1897 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1898 return error_mark_node;
1901 code = TREE_CODE (type);
1903 /* For an array decl decay_conversion should not try to return its
1904 initializer. */
1905 if (code != ARRAY_TYPE)
1907 /* FIXME remove? at least need to remember that this isn't really a
1908 constant expression if EXP isn't decl_constant_var_p, like with
1909 C_MAYBE_CONST_EXPR. */
1910 exp = decl_constant_value_safe (exp);
1911 if (error_operand_p (exp))
1912 return error_mark_node;
1915 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1916 return nullptr_node;
1918 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1919 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1920 if (code == VOID_TYPE)
1922 if (complain & tf_error)
1923 error_at (loc, "void value not ignored as it ought to be");
1924 return error_mark_node;
1926 if (invalid_nonstatic_memfn_p (exp, complain))
1927 return error_mark_node;
1928 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1929 return cp_build_addr_expr (exp, complain);
1930 if (code == ARRAY_TYPE)
1932 tree adr;
1933 tree ptrtype;
1935 if (INDIRECT_REF_P (exp))
1936 return build_nop (build_pointer_type (TREE_TYPE (type)),
1937 TREE_OPERAND (exp, 0));
1939 if (TREE_CODE (exp) == COMPOUND_EXPR)
1941 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1942 if (op1 == error_mark_node)
1943 return error_mark_node;
1944 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1945 TREE_OPERAND (exp, 0), op1);
1948 if (!lvalue_p (exp)
1949 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1951 if (complain & tf_error)
1952 error_at (loc, "invalid use of non-lvalue array");
1953 return error_mark_node;
1956 /* Don't let an array compound literal decay to a pointer. It can
1957 still be used to initialize an array or bind to a reference. */
1958 if (TREE_CODE (exp) == TARGET_EXPR)
1960 if (complain & tf_error)
1961 error_at (loc, "taking address of temporary array");
1962 return error_mark_node;
1965 ptrtype = build_pointer_type (TREE_TYPE (type));
1967 if (VAR_P (exp))
1969 if (!cxx_mark_addressable (exp))
1970 return error_mark_node;
1971 adr = build_nop (ptrtype, build_address (exp));
1972 return adr;
1974 /* This way is better for a COMPONENT_REF since it can
1975 simplify the offset for a component. */
1976 adr = cp_build_addr_expr (exp, complain);
1977 return cp_convert (ptrtype, adr, complain);
1980 /* If a bitfield is used in a context where integral promotion
1981 applies, then the caller is expected to have used
1982 default_conversion. That function promotes bitfields correctly
1983 before calling this function. At this point, if we have a
1984 bitfield referenced, we may assume that is not subject to
1985 promotion, and that, therefore, the type of the resulting rvalue
1986 is the declared type of the bitfield. */
1987 exp = convert_bitfield_to_declared_type (exp);
1989 /* We do not call rvalue() here because we do not want to wrap EXP
1990 in a NON_LVALUE_EXPR. */
1992 /* [basic.lval]
1994 Non-class rvalues always have cv-unqualified types. */
1995 type = TREE_TYPE (exp);
1996 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1997 exp = build_nop (cv_unqualified (type), exp);
1999 return exp;
2002 /* Perform preparatory conversions, as part of the "usual arithmetic
2003 conversions". In particular, as per [expr]:
2005 Whenever an lvalue expression appears as an operand of an
2006 operator that expects the rvalue for that operand, the
2007 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2008 standard conversions are applied to convert the expression to an
2009 rvalue.
2011 In addition, we perform integral promotions here, as those are
2012 applied to both operands to a binary operator before determining
2013 what additional conversions should apply. */
2015 static tree
2016 cp_default_conversion (tree exp, tsubst_flags_t complain)
2018 /* Check for target-specific promotions. */
2019 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2020 if (promoted_type)
2021 exp = cp_convert (promoted_type, exp, complain);
2022 /* Perform the integral promotions first so that bitfield
2023 expressions (which may promote to "int", even if the bitfield is
2024 declared "unsigned") are promoted correctly. */
2025 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2026 exp = cp_perform_integral_promotions (exp, complain);
2027 /* Perform the other conversions. */
2028 exp = decay_conversion (exp, complain);
2030 return exp;
2033 /* C version. */
2035 tree
2036 default_conversion (tree exp)
2038 return cp_default_conversion (exp, tf_warning_or_error);
2041 /* EXPR is an expression with an integral or enumeration type.
2042 Perform the integral promotions in [conv.prom], and return the
2043 converted value. */
2045 tree
2046 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2048 tree type;
2049 tree promoted_type;
2051 expr = mark_rvalue_use (expr);
2053 /* [conv.prom]
2055 If the bitfield has an enumerated type, it is treated as any
2056 other value of that type for promotion purposes. */
2057 type = is_bitfield_expr_with_lowered_type (expr);
2058 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2059 type = TREE_TYPE (expr);
2060 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2061 /* Scoped enums don't promote. */
2062 if (SCOPED_ENUM_P (type))
2063 return expr;
2064 promoted_type = type_promotes_to (type);
2065 if (type != promoted_type)
2066 expr = cp_convert (promoted_type, expr, complain);
2067 return expr;
2070 /* C version. */
2072 tree
2073 perform_integral_promotions (tree expr)
2075 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2078 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2079 decay_conversion to one. */
2082 string_conv_p (const_tree totype, const_tree exp, int warn)
2084 tree t;
2086 if (!TYPE_PTR_P (totype))
2087 return 0;
2089 t = TREE_TYPE (totype);
2090 if (!same_type_p (t, char_type_node)
2091 && !same_type_p (t, char16_type_node)
2092 && !same_type_p (t, char32_type_node)
2093 && !same_type_p (t, wchar_type_node))
2094 return 0;
2096 if (TREE_CODE (exp) == STRING_CST)
2098 /* Make sure that we don't try to convert between char and wide chars. */
2099 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2100 return 0;
2102 else
2104 /* Is this a string constant which has decayed to 'const char *'? */
2105 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2106 if (!same_type_p (TREE_TYPE (exp), t))
2107 return 0;
2108 STRIP_NOPS (exp);
2109 if (TREE_CODE (exp) != ADDR_EXPR
2110 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2111 return 0;
2114 /* This warning is not very useful, as it complains about printf. */
2115 if (warn)
2116 warning (OPT_Wwrite_strings,
2117 "deprecated conversion from string constant to %qT",
2118 totype);
2120 return 1;
2123 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2124 can, for example, use as an lvalue. This code used to be in
2125 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2126 expressions, where we're dealing with aggregates. But now it's again only
2127 called from unary_complex_lvalue. The case (in particular) that led to
2128 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2129 get it there. */
2131 static tree
2132 rationalize_conditional_expr (enum tree_code code, tree t,
2133 tsubst_flags_t complain)
2135 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2137 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2138 the first operand is always the one to be used if both operands
2139 are equal, so we know what conditional expression this used to be. */
2140 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2142 tree op0 = TREE_OPERAND (t, 0);
2143 tree op1 = TREE_OPERAND (t, 1);
2145 /* The following code is incorrect if either operand side-effects. */
2146 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2147 && !TREE_SIDE_EFFECTS (op1));
2148 return
2149 build_conditional_expr (loc,
2150 build_x_binary_op (loc,
2151 (TREE_CODE (t) == MIN_EXPR
2152 ? LE_EXPR : GE_EXPR),
2153 op0, TREE_CODE (op0),
2154 op1, TREE_CODE (op1),
2155 /*overload=*/NULL,
2156 complain),
2157 cp_build_unary_op (code, op0, 0, complain),
2158 cp_build_unary_op (code, op1, 0, complain),
2159 complain);
2162 return
2163 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2164 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2165 complain),
2166 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2167 complain),
2168 complain);
2171 /* Given the TYPE of an anonymous union field inside T, return the
2172 FIELD_DECL for the field. If not found return NULL_TREE. Because
2173 anonymous unions can nest, we must also search all anonymous unions
2174 that are directly reachable. */
2176 tree
2177 lookup_anon_field (tree t, tree type)
2179 tree field;
2181 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2183 if (TREE_STATIC (field))
2184 continue;
2185 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2186 continue;
2188 /* If we find it directly, return the field. */
2189 if (DECL_NAME (field) == NULL_TREE
2190 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2192 return field;
2195 /* Otherwise, it could be nested, search harder. */
2196 if (DECL_NAME (field) == NULL_TREE
2197 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2199 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2200 if (subfield)
2201 return subfield;
2204 return NULL_TREE;
2207 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2208 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2209 non-NULL, it indicates the path to the base used to name MEMBER.
2210 If PRESERVE_REFERENCE is true, the expression returned will have
2211 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2212 returned will have the type referred to by the reference.
2214 This function does not perform access control; that is either done
2215 earlier by the parser when the name of MEMBER is resolved to MEMBER
2216 itself, or later when overload resolution selects one of the
2217 functions indicated by MEMBER. */
2219 tree
2220 build_class_member_access_expr (tree object, tree member,
2221 tree access_path, bool preserve_reference,
2222 tsubst_flags_t complain)
2224 tree object_type;
2225 tree member_scope;
2226 tree result = NULL_TREE;
2227 tree using_decl = NULL_TREE;
2229 if (error_operand_p (object) || error_operand_p (member))
2230 return error_mark_node;
2232 gcc_assert (DECL_P (member) || BASELINK_P (member));
2234 /* [expr.ref]
2236 The type of the first expression shall be "class object" (of a
2237 complete type). */
2238 object_type = TREE_TYPE (object);
2239 if (!currently_open_class (object_type)
2240 && !complete_type_or_maybe_complain (object_type, object, complain))
2241 return error_mark_node;
2242 if (!CLASS_TYPE_P (object_type))
2244 if (complain & tf_error)
2246 if (POINTER_TYPE_P (object_type)
2247 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2248 error ("request for member %qD in %qE, which is of pointer "
2249 "type %qT (maybe you meant to use %<->%> ?)",
2250 member, object, object_type);
2251 else
2252 error ("request for member %qD in %qE, which is of non-class "
2253 "type %qT", member, object, object_type);
2255 return error_mark_node;
2258 /* The standard does not seem to actually say that MEMBER must be a
2259 member of OBJECT_TYPE. However, that is clearly what is
2260 intended. */
2261 if (DECL_P (member))
2263 member_scope = DECL_CLASS_CONTEXT (member);
2264 mark_used (member);
2265 if (TREE_DEPRECATED (member))
2266 warn_deprecated_use (member, NULL_TREE);
2268 else
2269 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2270 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2271 presently be the anonymous union. Go outwards until we find a
2272 type related to OBJECT_TYPE. */
2273 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2274 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2275 object_type))
2276 member_scope = TYPE_CONTEXT (member_scope);
2277 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2279 if (complain & tf_error)
2281 if (TREE_CODE (member) == FIELD_DECL)
2282 error ("invalid use of nonstatic data member %qE", member);
2283 else
2284 error ("%qD is not a member of %qT", member, object_type);
2286 return error_mark_node;
2289 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2290 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2291 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2293 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2294 if (temp)
2295 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2298 /* In [expr.ref], there is an explicit list of the valid choices for
2299 MEMBER. We check for each of those cases here. */
2300 if (VAR_P (member))
2302 /* A static data member. */
2303 result = member;
2304 mark_exp_read (object);
2305 /* If OBJECT has side-effects, they are supposed to occur. */
2306 if (TREE_SIDE_EFFECTS (object))
2307 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2309 else if (TREE_CODE (member) == FIELD_DECL)
2311 /* A non-static data member. */
2312 bool null_object_p;
2313 int type_quals;
2314 tree member_type;
2316 null_object_p = (INDIRECT_REF_P (object)
2317 && integer_zerop (TREE_OPERAND (object, 0)));
2319 /* Convert OBJECT to the type of MEMBER. */
2320 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2321 TYPE_MAIN_VARIANT (member_scope)))
2323 tree binfo;
2324 base_kind kind;
2326 binfo = lookup_base (access_path ? access_path : object_type,
2327 member_scope, ba_unique, &kind, complain);
2328 if (binfo == error_mark_node)
2329 return error_mark_node;
2331 /* It is invalid to try to get to a virtual base of a
2332 NULL object. The most common cause is invalid use of
2333 offsetof macro. */
2334 if (null_object_p && kind == bk_via_virtual)
2336 if (complain & tf_error)
2338 error ("invalid access to non-static data member %qD of "
2339 "NULL object",
2340 member);
2341 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2343 return error_mark_node;
2346 /* Convert to the base. */
2347 object = build_base_path (PLUS_EXPR, object, binfo,
2348 /*nonnull=*/1, complain);
2349 /* If we found the base successfully then we should be able
2350 to convert to it successfully. */
2351 gcc_assert (object != error_mark_node);
2354 /* Complain about other invalid uses of offsetof, even though they will
2355 give the right answer. Note that we complain whether or not they
2356 actually used the offsetof macro, since there's no way to know at this
2357 point. So we just give a warning, instead of a pedwarn. */
2358 /* Do not produce this warning for base class field references, because
2359 we know for a fact that didn't come from offsetof. This does occur
2360 in various testsuite cases where a null object is passed where a
2361 vtable access is required. */
2362 if (null_object_p && warn_invalid_offsetof
2363 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2364 && !DECL_FIELD_IS_BASE (member)
2365 && cp_unevaluated_operand == 0
2366 && (complain & tf_warning))
2368 warning (OPT_Winvalid_offsetof,
2369 "invalid access to non-static data member %qD "
2370 " of NULL object", member);
2371 warning (OPT_Winvalid_offsetof,
2372 "(perhaps the %<offsetof%> macro was used incorrectly)");
2375 /* If MEMBER is from an anonymous aggregate, we have converted
2376 OBJECT so that it refers to the class containing the
2377 anonymous union. Generate a reference to the anonymous union
2378 itself, and recur to find MEMBER. */
2379 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2380 /* When this code is called from build_field_call, the
2381 object already has the type of the anonymous union.
2382 That is because the COMPONENT_REF was already
2383 constructed, and was then disassembled before calling
2384 build_field_call. After the function-call code is
2385 cleaned up, this waste can be eliminated. */
2386 && (!same_type_ignoring_top_level_qualifiers_p
2387 (TREE_TYPE (object), DECL_CONTEXT (member))))
2389 tree anonymous_union;
2391 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2392 DECL_CONTEXT (member));
2393 object = build_class_member_access_expr (object,
2394 anonymous_union,
2395 /*access_path=*/NULL_TREE,
2396 preserve_reference,
2397 complain);
2400 /* Compute the type of the field, as described in [expr.ref]. */
2401 type_quals = TYPE_UNQUALIFIED;
2402 member_type = TREE_TYPE (member);
2403 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2405 type_quals = (cp_type_quals (member_type)
2406 | cp_type_quals (object_type));
2408 /* A field is const (volatile) if the enclosing object, or the
2409 field itself, is const (volatile). But, a mutable field is
2410 not const, even within a const object. */
2411 if (DECL_MUTABLE_P (member))
2412 type_quals &= ~TYPE_QUAL_CONST;
2413 member_type = cp_build_qualified_type (member_type, type_quals);
2416 result = build3 (COMPONENT_REF, member_type, object, member,
2417 NULL_TREE);
2418 result = fold_if_not_in_template (result);
2420 /* Mark the expression const or volatile, as appropriate. Even
2421 though we've dealt with the type above, we still have to mark the
2422 expression itself. */
2423 if (type_quals & TYPE_QUAL_CONST)
2424 TREE_READONLY (result) = 1;
2425 if (type_quals & TYPE_QUAL_VOLATILE)
2426 TREE_THIS_VOLATILE (result) = 1;
2428 else if (BASELINK_P (member))
2430 /* The member is a (possibly overloaded) member function. */
2431 tree functions;
2432 tree type;
2434 /* If the MEMBER is exactly one static member function, then we
2435 know the type of the expression. Otherwise, we must wait
2436 until overload resolution has been performed. */
2437 functions = BASELINK_FUNCTIONS (member);
2438 if (TREE_CODE (functions) == FUNCTION_DECL
2439 && DECL_STATIC_FUNCTION_P (functions))
2440 type = TREE_TYPE (functions);
2441 else
2442 type = unknown_type_node;
2443 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2444 base. That will happen when the function is called. */
2445 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2447 else if (TREE_CODE (member) == CONST_DECL)
2449 /* The member is an enumerator. */
2450 result = member;
2451 /* If OBJECT has side-effects, they are supposed to occur. */
2452 if (TREE_SIDE_EFFECTS (object))
2453 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2454 object, result);
2456 else if ((using_decl = strip_using_decl (member)) != member)
2457 result = build_class_member_access_expr (object,
2458 using_decl,
2459 access_path, preserve_reference,
2460 complain);
2461 else
2463 if (complain & tf_error)
2464 error ("invalid use of %qD", member);
2465 return error_mark_node;
2468 if (!preserve_reference)
2469 /* [expr.ref]
2471 If E2 is declared to have type "reference to T", then ... the
2472 type of E1.E2 is T. */
2473 result = convert_from_reference (result);
2475 return result;
2478 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2479 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2481 static tree
2482 lookup_destructor (tree object, tree scope, tree dtor_name,
2483 tsubst_flags_t complain)
2485 tree object_type = TREE_TYPE (object);
2486 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2487 tree expr;
2489 /* We've already complained about this destructor. */
2490 if (dtor_type == error_mark_node)
2491 return error_mark_node;
2493 if (scope && !check_dtor_name (scope, dtor_type))
2495 if (complain & tf_error)
2496 error ("qualified type %qT does not match destructor name ~%qT",
2497 scope, dtor_type);
2498 return error_mark_node;
2500 if (is_auto (dtor_type))
2501 dtor_type = object_type;
2502 else if (identifier_p (dtor_type))
2504 /* In a template, names we can't find a match for are still accepted
2505 destructor names, and we check them here. */
2506 if (check_dtor_name (object_type, dtor_type))
2507 dtor_type = object_type;
2508 else
2510 if (complain & tf_error)
2511 error ("object type %qT does not match destructor name ~%qT",
2512 object_type, dtor_type);
2513 return error_mark_node;
2517 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2519 if (complain & tf_error)
2520 error ("the type being destroyed is %qT, but the destructor "
2521 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2522 return error_mark_node;
2524 expr = lookup_member (dtor_type, complete_dtor_identifier,
2525 /*protect=*/1, /*want_type=*/false,
2526 tf_warning_or_error);
2527 expr = (adjust_result_of_qualified_name_lookup
2528 (expr, dtor_type, object_type));
2529 if (scope == NULL_TREE)
2530 /* We need to call adjust_result_of_qualified_name_lookup in case the
2531 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2532 that we still get virtual function binding. */
2533 BASELINK_QUALIFIED_P (expr) = false;
2534 return expr;
2537 /* An expression of the form "A::template B" has been resolved to
2538 DECL. Issue a diagnostic if B is not a template or template
2539 specialization. */
2541 void
2542 check_template_keyword (tree decl)
2544 /* The standard says:
2546 [temp.names]
2548 If a name prefixed by the keyword template is not a member
2549 template, the program is ill-formed.
2551 DR 228 removed the restriction that the template be a member
2552 template.
2554 DR 96, if accepted would add the further restriction that explicit
2555 template arguments must be provided if the template keyword is
2556 used, but, as of 2005-10-16, that DR is still in "drafting". If
2557 this DR is accepted, then the semantic checks here can be
2558 simplified, as the entity named must in fact be a template
2559 specialization, rather than, as at present, a set of overloaded
2560 functions containing at least one template function. */
2561 if (TREE_CODE (decl) != TEMPLATE_DECL
2562 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2564 if (!is_overloaded_fn (decl))
2565 permerror (input_location, "%qD is not a template", decl);
2566 else
2568 tree fns;
2569 fns = decl;
2570 if (BASELINK_P (fns))
2571 fns = BASELINK_FUNCTIONS (fns);
2572 while (fns)
2574 tree fn = OVL_CURRENT (fns);
2575 if (TREE_CODE (fn) == TEMPLATE_DECL
2576 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2577 break;
2578 if (TREE_CODE (fn) == FUNCTION_DECL
2579 && DECL_USE_TEMPLATE (fn)
2580 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2581 break;
2582 fns = OVL_NEXT (fns);
2584 if (!fns)
2585 permerror (input_location, "%qD is not a template", decl);
2590 /* This function is called by the parser to process a class member
2591 access expression of the form OBJECT.NAME. NAME is a node used by
2592 the parser to represent a name; it is not yet a DECL. It may,
2593 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2594 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2595 there is no reason to do the lookup twice, so the parser keeps the
2596 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2597 be a template via the use of the "A::template B" syntax. */
2599 tree
2600 finish_class_member_access_expr (tree object, tree name, bool template_p,
2601 tsubst_flags_t complain)
2603 tree expr;
2604 tree object_type;
2605 tree member;
2606 tree access_path = NULL_TREE;
2607 tree orig_object = object;
2608 tree orig_name = name;
2610 if (object == error_mark_node || name == error_mark_node)
2611 return error_mark_node;
2613 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2614 if (!objc_is_public (object, name))
2615 return error_mark_node;
2617 object_type = TREE_TYPE (object);
2619 if (processing_template_decl)
2621 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2622 dependent_type_p (object_type)
2623 /* If NAME is just an IDENTIFIER_NODE, then the expression
2624 is dependent. */
2625 || identifier_p (object)
2626 /* If NAME is "f<args>", where either 'f' or 'args' is
2627 dependent, then the expression is dependent. */
2628 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2629 && dependent_template_id_p (TREE_OPERAND (name, 0),
2630 TREE_OPERAND (name, 1)))
2631 /* If NAME is "T::X" where "T" is dependent, then the
2632 expression is dependent. */
2633 || (TREE_CODE (name) == SCOPE_REF
2634 && TYPE_P (TREE_OPERAND (name, 0))
2635 && dependent_type_p (TREE_OPERAND (name, 0))))
2636 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2637 object, name, NULL_TREE);
2638 object = build_non_dependent_expr (object);
2640 else if (c_dialect_objc ()
2641 && identifier_p (name)
2642 && (expr = objc_maybe_build_component_ref (object, name)))
2643 return expr;
2645 /* [expr.ref]
2647 The type of the first expression shall be "class object" (of a
2648 complete type). */
2649 if (!currently_open_class (object_type)
2650 && !complete_type_or_maybe_complain (object_type, object, complain))
2651 return error_mark_node;
2652 if (!CLASS_TYPE_P (object_type))
2654 if (complain & tf_error)
2656 if (POINTER_TYPE_P (object_type)
2657 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2658 error ("request for member %qD in %qE, which is of pointer "
2659 "type %qT (maybe you meant to use %<->%> ?)",
2660 name, object, object_type);
2661 else
2662 error ("request for member %qD in %qE, which is of non-class "
2663 "type %qT", name, object, object_type);
2665 return error_mark_node;
2668 if (BASELINK_P (name))
2669 /* A member function that has already been looked up. */
2670 member = name;
2671 else
2673 bool is_template_id = false;
2674 tree template_args = NULL_TREE;
2675 tree scope;
2677 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2679 is_template_id = true;
2680 template_args = TREE_OPERAND (name, 1);
2681 name = TREE_OPERAND (name, 0);
2683 if (TREE_CODE (name) == OVERLOAD)
2684 name = DECL_NAME (get_first_fn (name));
2685 else if (DECL_P (name))
2686 name = DECL_NAME (name);
2689 if (TREE_CODE (name) == SCOPE_REF)
2691 /* A qualified name. The qualifying class or namespace `S'
2692 has already been looked up; it is either a TYPE or a
2693 NAMESPACE_DECL. */
2694 scope = TREE_OPERAND (name, 0);
2695 name = TREE_OPERAND (name, 1);
2697 /* If SCOPE is a namespace, then the qualified name does not
2698 name a member of OBJECT_TYPE. */
2699 if (TREE_CODE (scope) == NAMESPACE_DECL)
2701 if (complain & tf_error)
2702 error ("%<%D::%D%> is not a member of %qT",
2703 scope, name, object_type);
2704 return error_mark_node;
2707 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2709 /* Looking up a member enumerator (c++/56793). */
2710 if (!TYPE_CLASS_SCOPE_P (scope)
2711 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2713 if (complain & tf_error)
2714 error ("%<%D::%D%> is not a member of %qT",
2715 scope, name, object_type);
2716 return error_mark_node;
2718 tree val = lookup_enumerator (scope, name);
2719 if (TREE_SIDE_EFFECTS (object))
2720 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2721 return val;
2724 gcc_assert (CLASS_TYPE_P (scope));
2725 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2727 if (constructor_name_p (name, scope))
2729 if (complain & tf_error)
2730 error ("cannot call constructor %<%T::%D%> directly",
2731 scope, name);
2732 return error_mark_node;
2735 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2736 access_path = lookup_base (object_type, scope, ba_check,
2737 NULL, complain);
2738 if (access_path == error_mark_node)
2739 return error_mark_node;
2740 if (!access_path)
2742 if (complain & tf_error)
2743 error ("%qT is not a base of %qT", scope, object_type);
2744 return error_mark_node;
2747 else
2749 scope = NULL_TREE;
2750 access_path = object_type;
2753 if (TREE_CODE (name) == BIT_NOT_EXPR)
2754 member = lookup_destructor (object, scope, name, complain);
2755 else
2757 /* Look up the member. */
2758 member = lookup_member (access_path, name, /*protect=*/1,
2759 /*want_type=*/false, complain);
2760 if (member == NULL_TREE)
2762 if (complain & tf_error)
2763 error ("%qD has no member named %qE",
2764 TREE_CODE (access_path) == TREE_BINFO
2765 ? TREE_TYPE (access_path) : object_type, name);
2766 return error_mark_node;
2768 if (member == error_mark_node)
2769 return error_mark_node;
2772 if (is_template_id)
2774 tree templ = member;
2776 if (BASELINK_P (templ))
2777 templ = lookup_template_function (templ, template_args);
2778 else
2780 if (complain & tf_error)
2781 error ("%qD is not a member template function", name);
2782 return error_mark_node;
2787 if (TREE_DEPRECATED (member))
2788 warn_deprecated_use (member, NULL_TREE);
2790 if (template_p)
2791 check_template_keyword (member);
2793 expr = build_class_member_access_expr (object, member, access_path,
2794 /*preserve_reference=*/false,
2795 complain);
2796 if (processing_template_decl && expr != error_mark_node)
2798 if (BASELINK_P (member))
2800 if (TREE_CODE (orig_name) == SCOPE_REF)
2801 BASELINK_QUALIFIED_P (member) = 1;
2802 orig_name = member;
2804 return build_min_non_dep (COMPONENT_REF, expr,
2805 orig_object, orig_name,
2806 NULL_TREE);
2809 return expr;
2812 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2813 type. */
2815 tree
2816 build_simple_component_ref (tree object, tree member)
2818 tree type = cp_build_qualified_type (TREE_TYPE (member),
2819 cp_type_quals (TREE_TYPE (object)));
2820 return fold_build3_loc (input_location,
2821 COMPONENT_REF, type,
2822 object, member, NULL_TREE);
2825 /* Return an expression for the MEMBER_NAME field in the internal
2826 representation of PTRMEM, a pointer-to-member function. (Each
2827 pointer-to-member function type gets its own RECORD_TYPE so it is
2828 more convenient to access the fields by name than by FIELD_DECL.)
2829 This routine converts the NAME to a FIELD_DECL and then creates the
2830 node for the complete expression. */
2832 tree
2833 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2835 tree ptrmem_type;
2836 tree member;
2838 /* This code is a stripped down version of
2839 build_class_member_access_expr. It does not work to use that
2840 routine directly because it expects the object to be of class
2841 type. */
2842 ptrmem_type = TREE_TYPE (ptrmem);
2843 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2844 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2845 /*want_type=*/false, tf_warning_or_error);
2846 return build_simple_component_ref (ptrmem, member);
2849 /* Given an expression PTR for a pointer, return an expression
2850 for the value pointed to.
2851 ERRORSTRING is the name of the operator to appear in error messages.
2853 This function may need to overload OPERATOR_FNNAME.
2854 Must also handle REFERENCE_TYPEs for C++. */
2856 tree
2857 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2858 tsubst_flags_t complain)
2860 tree orig_expr = expr;
2861 tree rval;
2863 if (processing_template_decl)
2865 /* Retain the type if we know the operand is a pointer. */
2866 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2867 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2868 if (type_dependent_expression_p (expr))
2869 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2870 expr = build_non_dependent_expr (expr);
2873 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2874 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2875 if (!rval)
2876 rval = cp_build_indirect_ref (expr, errorstring, complain);
2878 if (processing_template_decl && rval != error_mark_node)
2879 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2880 else
2881 return rval;
2884 /* Helper function called from c-common. */
2885 tree
2886 build_indirect_ref (location_t /*loc*/,
2887 tree ptr, ref_operator errorstring)
2889 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2892 tree
2893 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2894 tsubst_flags_t complain)
2896 tree pointer, type;
2898 if (ptr == current_class_ptr
2899 || (TREE_CODE (ptr) == NOP_EXPR
2900 && TREE_OPERAND (ptr, 0) == current_class_ptr
2901 && (same_type_ignoring_top_level_qualifiers_p
2902 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2903 return current_class_ref;
2905 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2906 ? ptr : decay_conversion (ptr, complain));
2907 if (pointer == error_mark_node)
2908 return error_mark_node;
2910 type = TREE_TYPE (pointer);
2912 if (POINTER_TYPE_P (type))
2914 /* [expr.unary.op]
2916 If the type of the expression is "pointer to T," the type
2917 of the result is "T." */
2918 tree t = TREE_TYPE (type);
2920 if (CONVERT_EXPR_P (ptr)
2921 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2923 /* If a warning is issued, mark it to avoid duplicates from
2924 the backend. This only needs to be done at
2925 warn_strict_aliasing > 2. */
2926 if (warn_strict_aliasing > 2)
2927 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2928 type, TREE_OPERAND (ptr, 0)))
2929 TREE_NO_WARNING (ptr) = 1;
2932 if (VOID_TYPE_P (t))
2934 /* A pointer to incomplete type (other than cv void) can be
2935 dereferenced [expr.unary.op]/1 */
2936 if (complain & tf_error)
2937 error ("%qT is not a pointer-to-object type", type);
2938 return error_mark_node;
2940 else if (TREE_CODE (pointer) == ADDR_EXPR
2941 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2942 /* The POINTER was something like `&x'. We simplify `*&x' to
2943 `x'. */
2944 return TREE_OPERAND (pointer, 0);
2945 else
2947 tree ref = build1 (INDIRECT_REF, t, pointer);
2949 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2950 so that we get the proper error message if the result is used
2951 to assign to. Also, &* is supposed to be a no-op. */
2952 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2953 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2954 TREE_SIDE_EFFECTS (ref)
2955 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2956 return ref;
2959 else if (!(complain & tf_error))
2960 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2962 /* `pointer' won't be an error_mark_node if we were given a
2963 pointer to member, so it's cool to check for this here. */
2964 else if (TYPE_PTRMEM_P (type))
2965 switch (errorstring)
2967 case RO_ARRAY_INDEXING:
2968 error ("invalid use of array indexing on pointer to member");
2969 break;
2970 case RO_UNARY_STAR:
2971 error ("invalid use of unary %<*%> on pointer to member");
2972 break;
2973 case RO_IMPLICIT_CONVERSION:
2974 error ("invalid use of implicit conversion on pointer to member");
2975 break;
2976 case RO_ARROW_STAR:
2977 error ("left hand operand of %<->*%> must be a pointer to class, "
2978 "but is a pointer to member of type %qT", type);
2979 break;
2980 default:
2981 gcc_unreachable ();
2983 else if (pointer != error_mark_node)
2984 invalid_indirection_error (input_location, type, errorstring);
2986 return error_mark_node;
2989 /* This handles expressions of the form "a[i]", which denotes
2990 an array reference.
2992 This is logically equivalent in C to *(a+i), but we may do it differently.
2993 If A is a variable or a member, we generate a primitive ARRAY_REF.
2994 This avoids forcing the array out of registers, and can work on
2995 arrays that are not lvalues (for example, members of structures returned
2996 by functions).
2998 If INDEX is of some user-defined type, it must be converted to
2999 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3000 will inherit the type of the array, which will be some pointer type.
3002 LOC is the location to use in building the array reference. */
3004 tree
3005 cp_build_array_ref (location_t loc, tree array, tree idx,
3006 tsubst_flags_t complain)
3008 tree ret;
3010 if (idx == 0)
3012 if (complain & tf_error)
3013 error_at (loc, "subscript missing in array reference");
3014 return error_mark_node;
3017 /* If an array's index is an array notation, then its rank cannot be
3018 greater than one. */
3019 if (flag_cilkplus && contains_array_notation_expr (idx))
3021 size_t rank = 0;
3023 /* If find_rank returns false, then it should have reported an error,
3024 thus it is unnecessary for repetition. */
3025 if (!find_rank (loc, idx, idx, true, &rank))
3026 return error_mark_node;
3027 if (rank > 1)
3029 error_at (loc, "rank of the array%'s index is greater than 1");
3030 return error_mark_node;
3033 if (TREE_TYPE (array) == error_mark_node
3034 || TREE_TYPE (idx) == error_mark_node)
3035 return error_mark_node;
3037 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3038 inside it. */
3039 switch (TREE_CODE (array))
3041 case COMPOUND_EXPR:
3043 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3044 complain);
3045 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3046 TREE_OPERAND (array, 0), value);
3047 SET_EXPR_LOCATION (ret, loc);
3048 return ret;
3051 case COND_EXPR:
3052 ret = build_conditional_expr
3053 (loc, TREE_OPERAND (array, 0),
3054 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3055 complain),
3056 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3057 complain),
3058 complain);
3059 protected_set_expr_location (ret, loc);
3060 return ret;
3062 default:
3063 break;
3066 convert_vector_to_pointer_for_subscript (loc, &array, idx);
3068 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3070 tree rval, type;
3072 warn_array_subscript_with_type_char (idx);
3074 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3076 if (complain & tf_error)
3077 error_at (loc, "array subscript is not an integer");
3078 return error_mark_node;
3081 /* Apply integral promotions *after* noticing character types.
3082 (It is unclear why we do these promotions -- the standard
3083 does not say that we should. In fact, the natural thing would
3084 seem to be to convert IDX to ptrdiff_t; we're performing
3085 pointer arithmetic.) */
3086 idx = cp_perform_integral_promotions (idx, complain);
3088 /* An array that is indexed by a non-constant
3089 cannot be stored in a register; we must be able to do
3090 address arithmetic on its address.
3091 Likewise an array of elements of variable size. */
3092 if (TREE_CODE (idx) != INTEGER_CST
3093 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3094 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3095 != INTEGER_CST)))
3097 if (!cxx_mark_addressable (array))
3098 return error_mark_node;
3101 /* An array that is indexed by a constant value which is not within
3102 the array bounds cannot be stored in a register either; because we
3103 would get a crash in store_bit_field/extract_bit_field when trying
3104 to access a non-existent part of the register. */
3105 if (TREE_CODE (idx) == INTEGER_CST
3106 && TYPE_DOMAIN (TREE_TYPE (array))
3107 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3109 if (!cxx_mark_addressable (array))
3110 return error_mark_node;
3113 if (!lvalue_p (array) && (complain & tf_error))
3114 pedwarn (loc, OPT_Wpedantic,
3115 "ISO C++ forbids subscripting non-lvalue array");
3117 /* Note in C++ it is valid to subscript a `register' array, since
3118 it is valid to take the address of something with that
3119 storage specification. */
3120 if (extra_warnings)
3122 tree foo = array;
3123 while (TREE_CODE (foo) == COMPONENT_REF)
3124 foo = TREE_OPERAND (foo, 0);
3125 if (VAR_P (foo) && DECL_REGISTER (foo)
3126 && (complain & tf_warning))
3127 warning_at (loc, OPT_Wextra,
3128 "subscripting array declared %<register%>");
3131 type = TREE_TYPE (TREE_TYPE (array));
3132 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3133 /* Array ref is const/volatile if the array elements are
3134 or if the array is.. */
3135 TREE_READONLY (rval)
3136 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3137 TREE_SIDE_EFFECTS (rval)
3138 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3139 TREE_THIS_VOLATILE (rval)
3140 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3141 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3142 complain);
3143 protected_set_expr_location (ret, loc);
3144 return ret;
3148 tree ar = cp_default_conversion (array, complain);
3149 tree ind = cp_default_conversion (idx, complain);
3151 /* Put the integer in IND to simplify error checking. */
3152 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3154 tree temp = ar;
3155 ar = ind;
3156 ind = temp;
3159 if (ar == error_mark_node || ind == error_mark_node)
3160 return error_mark_node;
3162 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3164 if (complain & tf_error)
3165 error_at (loc, "subscripted value is neither array nor pointer");
3166 return error_mark_node;
3168 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3170 if (complain & tf_error)
3171 error_at (loc, "array subscript is not an integer");
3172 return error_mark_node;
3175 warn_array_subscript_with_type_char (idx);
3177 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3178 PLUS_EXPR, ar, ind,
3179 complain),
3180 RO_ARRAY_INDEXING,
3181 complain);
3182 protected_set_expr_location (ret, loc);
3183 return ret;
3187 /* Entry point for Obj-C++. */
3189 tree
3190 build_array_ref (location_t loc, tree array, tree idx)
3192 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3195 /* Resolve a pointer to member function. INSTANCE is the object
3196 instance to use, if the member points to a virtual member.
3198 This used to avoid checking for virtual functions if basetype
3199 has no virtual functions, according to an earlier ANSI draft.
3200 With the final ISO C++ rules, such an optimization is
3201 incorrect: A pointer to a derived member can be static_cast
3202 to pointer-to-base-member, as long as the dynamic object
3203 later has the right member. So now we only do this optimization
3204 when we know the dynamic type of the object. */
3206 tree
3207 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3208 tsubst_flags_t complain)
3210 if (TREE_CODE (function) == OFFSET_REF)
3211 function = TREE_OPERAND (function, 1);
3213 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3215 tree idx, delta, e1, e2, e3, vtbl;
3216 bool nonvirtual;
3217 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3218 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3220 tree instance_ptr = *instance_ptrptr;
3221 tree instance_save_expr = 0;
3222 if (instance_ptr == error_mark_node)
3224 if (TREE_CODE (function) == PTRMEM_CST)
3226 /* Extracting the function address from a pmf is only
3227 allowed with -Wno-pmf-conversions. It only works for
3228 pmf constants. */
3229 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3230 e1 = convert (fntype, e1);
3231 return e1;
3233 else
3235 if (complain & tf_error)
3236 error ("object missing in use of %qE", function);
3237 return error_mark_node;
3241 /* True if we know that the dynamic type of the object doesn't have
3242 virtual functions, so we can assume the PFN field is a pointer. */
3243 nonvirtual = (COMPLETE_TYPE_P (basetype)
3244 && !TYPE_POLYMORPHIC_P (basetype)
3245 && resolves_to_fixed_type_p (instance_ptr, 0));
3247 if (TREE_SIDE_EFFECTS (instance_ptr))
3248 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3250 if (TREE_SIDE_EFFECTS (function))
3251 function = save_expr (function);
3253 /* Start by extracting all the information from the PMF itself. */
3254 e3 = pfn_from_ptrmemfunc (function);
3255 delta = delta_from_ptrmemfunc (function);
3256 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3257 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3259 case ptrmemfunc_vbit_in_pfn:
3260 e1 = cp_build_binary_op (input_location,
3261 BIT_AND_EXPR, idx, integer_one_node,
3262 complain);
3263 idx = cp_build_binary_op (input_location,
3264 MINUS_EXPR, idx, integer_one_node,
3265 complain);
3266 if (idx == error_mark_node)
3267 return error_mark_node;
3268 break;
3270 case ptrmemfunc_vbit_in_delta:
3271 e1 = cp_build_binary_op (input_location,
3272 BIT_AND_EXPR, delta, integer_one_node,
3273 complain);
3274 delta = cp_build_binary_op (input_location,
3275 RSHIFT_EXPR, delta, integer_one_node,
3276 complain);
3277 if (delta == error_mark_node)
3278 return error_mark_node;
3279 break;
3281 default:
3282 gcc_unreachable ();
3285 if (e1 == error_mark_node)
3286 return error_mark_node;
3288 /* Convert down to the right base before using the instance. A
3289 special case is that in a pointer to member of class C, C may
3290 be incomplete. In that case, the function will of course be
3291 a member of C, and no conversion is required. In fact,
3292 lookup_base will fail in that case, because incomplete
3293 classes do not have BINFOs. */
3294 if (!same_type_ignoring_top_level_qualifiers_p
3295 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3297 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3298 basetype, ba_check, NULL, complain);
3299 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3300 1, complain);
3301 if (instance_ptr == error_mark_node)
3302 return error_mark_node;
3304 /* ...and then the delta in the PMF. */
3305 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3307 /* Hand back the adjusted 'this' argument to our caller. */
3308 *instance_ptrptr = instance_ptr;
3310 if (nonvirtual)
3311 /* Now just return the pointer. */
3312 return e3;
3314 /* Next extract the vtable pointer from the object. */
3315 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3316 instance_ptr);
3317 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3318 if (vtbl == error_mark_node)
3319 return error_mark_node;
3321 /* Finally, extract the function pointer from the vtable. */
3322 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3323 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3324 if (e2 == error_mark_node)
3325 return error_mark_node;
3326 TREE_CONSTANT (e2) = 1;
3328 /* When using function descriptors, the address of the
3329 vtable entry is treated as a function pointer. */
3330 if (TARGET_VTABLE_USES_DESCRIPTORS)
3331 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3332 cp_build_addr_expr (e2, complain));
3334 e2 = fold_convert (TREE_TYPE (e3), e2);
3335 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3336 if (e1 == error_mark_node)
3337 return error_mark_node;
3339 /* Make sure this doesn't get evaluated first inside one of the
3340 branches of the COND_EXPR. */
3341 if (instance_save_expr)
3342 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3343 instance_save_expr, e1);
3345 function = e1;
3347 return function;
3350 /* Used by the C-common bits. */
3351 tree
3352 build_function_call (location_t /*loc*/,
3353 tree function, tree params)
3355 return cp_build_function_call (function, params, tf_warning_or_error);
3358 /* Used by the C-common bits. */
3359 tree
3360 build_function_call_vec (location_t /*loc*/,
3361 tree function, vec<tree, va_gc> *params,
3362 vec<tree, va_gc> * /*origtypes*/)
3364 vec<tree, va_gc> *orig_params = params;
3365 tree ret = cp_build_function_call_vec (function, &params,
3366 tf_warning_or_error);
3368 /* cp_build_function_call_vec can reallocate PARAMS by adding
3369 default arguments. That should never happen here. Verify
3370 that. */
3371 gcc_assert (params == orig_params);
3373 return ret;
3376 /* Build a function call using a tree list of arguments. */
3378 tree
3379 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3381 vec<tree, va_gc> *vec;
3382 tree ret;
3384 vec = make_tree_vector ();
3385 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3386 vec_safe_push (vec, TREE_VALUE (params));
3387 ret = cp_build_function_call_vec (function, &vec, complain);
3388 release_tree_vector (vec);
3389 return ret;
3392 /* Build a function call using varargs. */
3394 tree
3395 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3397 vec<tree, va_gc> *vec;
3398 va_list args;
3399 tree ret, t;
3401 vec = make_tree_vector ();
3402 va_start (args, complain);
3403 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3404 vec_safe_push (vec, t);
3405 va_end (args);
3406 ret = cp_build_function_call_vec (function, &vec, complain);
3407 release_tree_vector (vec);
3408 return ret;
3411 /* Build a function call using a vector of arguments. PARAMS may be
3412 NULL if there are no parameters. This changes the contents of
3413 PARAMS. */
3415 tree
3416 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3417 tsubst_flags_t complain)
3419 tree fntype, fndecl;
3420 int is_method;
3421 tree original = function;
3422 int nargs;
3423 tree *argarray;
3424 tree parm_types;
3425 vec<tree, va_gc> *allocated = NULL;
3426 tree ret;
3428 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3429 expressions, like those used for ObjC messenger dispatches. */
3430 if (params != NULL && !vec_safe_is_empty (*params))
3431 function = objc_rewrite_function_call (function, (**params)[0]);
3433 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3434 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3435 if (TREE_CODE (function) == NOP_EXPR
3436 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3437 function = TREE_OPERAND (function, 0);
3439 if (TREE_CODE (function) == FUNCTION_DECL)
3441 mark_used (function);
3442 fndecl = function;
3444 /* Convert anything with function type to a pointer-to-function. */
3445 if (DECL_MAIN_P (function) && (complain & tf_error))
3446 pedwarn (input_location, OPT_Wpedantic,
3447 "ISO C++ forbids calling %<::main%> from within program");
3449 function = build_addr_func (function, complain);
3451 else
3453 fndecl = NULL_TREE;
3455 function = build_addr_func (function, complain);
3458 if (function == error_mark_node)
3459 return error_mark_node;
3461 fntype = TREE_TYPE (function);
3463 if (TYPE_PTRMEMFUNC_P (fntype))
3465 if (complain & tf_error)
3466 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3467 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3468 original, original);
3469 return error_mark_node;
3472 is_method = (TYPE_PTR_P (fntype)
3473 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3475 if (!(TYPE_PTRFN_P (fntype)
3476 || is_method
3477 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3479 if (complain & tf_error)
3481 if (!flag_diagnostics_show_caret)
3482 error_at (input_location,
3483 "%qE cannot be used as a function", original);
3484 else if (DECL_P (original))
3485 error_at (input_location,
3486 "%qD cannot be used as a function", original);
3487 else
3488 error_at (input_location,
3489 "expression cannot be used as a function");
3492 return error_mark_node;
3495 /* fntype now gets the type of function pointed to. */
3496 fntype = TREE_TYPE (fntype);
3497 parm_types = TYPE_ARG_TYPES (fntype);
3499 if (params == NULL)
3501 allocated = make_tree_vector ();
3502 params = &allocated;
3505 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3506 complain);
3507 if (nargs < 0)
3508 return error_mark_node;
3510 argarray = (*params)->address ();
3512 /* Check for errors in format strings and inappropriately
3513 null parameters. */
3514 check_function_arguments (fntype, nargs, argarray);
3516 ret = build_cxx_call (function, nargs, argarray, complain);
3518 if (allocated != NULL)
3519 release_tree_vector (allocated);
3521 return ret;
3524 /* Subroutine of convert_arguments.
3525 Warn about wrong number of args are genereted. */
3527 static void
3528 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3530 if (fndecl)
3532 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3534 if (DECL_NAME (fndecl) == NULL_TREE
3535 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3536 error_at (loc,
3537 too_many_p
3538 ? G_("too many arguments to constructor %q#D")
3539 : G_("too few arguments to constructor %q#D"),
3540 fndecl);
3541 else
3542 error_at (loc,
3543 too_many_p
3544 ? G_("too many arguments to member function %q#D")
3545 : G_("too few arguments to member function %q#D"),
3546 fndecl);
3548 else
3549 error_at (loc,
3550 too_many_p
3551 ? G_("too many arguments to function %q#D")
3552 : G_("too few arguments to function %q#D"),
3553 fndecl);
3554 inform (DECL_SOURCE_LOCATION (fndecl),
3555 "declared here");
3557 else
3559 if (c_dialect_objc () && objc_message_selector ())
3560 error_at (loc,
3561 too_many_p
3562 ? G_("too many arguments to method %q#D")
3563 : G_("too few arguments to method %q#D"),
3564 objc_message_selector ());
3565 else
3566 error_at (loc, too_many_p ? G_("too many arguments to function")
3567 : G_("too few arguments to function"));
3571 /* Convert the actual parameter expressions in the list VALUES to the
3572 types in the list TYPELIST. The converted expressions are stored
3573 back in the VALUES vector.
3574 If parmdecls is exhausted, or when an element has NULL as its type,
3575 perform the default conversions.
3577 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3579 This is also where warnings about wrong number of args are generated.
3581 Returns the actual number of arguments processed (which might be less
3582 than the length of the vector), or -1 on error.
3584 In C++, unspecified trailing parameters can be filled in with their
3585 default arguments, if such were specified. Do so here. */
3587 static int
3588 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3589 int flags, tsubst_flags_t complain)
3591 tree typetail;
3592 unsigned int i;
3594 /* Argument passing is always copy-initialization. */
3595 flags |= LOOKUP_ONLYCONVERTING;
3597 for (i = 0, typetail = typelist;
3598 i < vec_safe_length (*values);
3599 i++)
3601 tree type = typetail ? TREE_VALUE (typetail) : 0;
3602 tree val = (**values)[i];
3604 if (val == error_mark_node || type == error_mark_node)
3605 return -1;
3607 if (type == void_type_node)
3609 if (complain & tf_error)
3611 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3612 return i;
3614 else
3615 return -1;
3618 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3619 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3620 if (TREE_CODE (val) == NOP_EXPR
3621 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3622 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3623 val = TREE_OPERAND (val, 0);
3625 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3627 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3628 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3629 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3630 val = decay_conversion (val, complain);
3633 if (val == error_mark_node)
3634 return -1;
3636 if (type != 0)
3638 /* Formal parm type is specified by a function prototype. */
3639 tree parmval;
3641 if (!COMPLETE_TYPE_P (complete_type (type)))
3643 if (complain & tf_error)
3645 if (fndecl)
3646 error ("parameter %P of %qD has incomplete type %qT",
3647 i, fndecl, type);
3648 else
3649 error ("parameter %P has incomplete type %qT", i, type);
3651 parmval = error_mark_node;
3653 else
3655 parmval = convert_for_initialization
3656 (NULL_TREE, type, val, flags,
3657 ICR_ARGPASS, fndecl, i, complain);
3658 parmval = convert_for_arg_passing (type, parmval, complain);
3661 if (parmval == error_mark_node)
3662 return -1;
3664 (**values)[i] = parmval;
3666 else
3668 if (fndecl && magic_varargs_p (fndecl))
3669 /* Don't do ellipsis conversion for __built_in_constant_p
3670 as this will result in spurious errors for non-trivial
3671 types. */
3672 val = require_complete_type_sfinae (val, complain);
3673 else
3674 val = convert_arg_to_ellipsis (val, complain);
3676 (**values)[i] = val;
3679 if (typetail)
3680 typetail = TREE_CHAIN (typetail);
3683 if (typetail != 0 && typetail != void_list_node)
3685 /* See if there are default arguments that can be used. Because
3686 we hold default arguments in the FUNCTION_TYPE (which is so
3687 wrong), we can see default parameters here from deduced
3688 contexts (and via typeof) for indirect function calls.
3689 Fortunately we know whether we have a function decl to
3690 provide default arguments in a language conformant
3691 manner. */
3692 if (fndecl && TREE_PURPOSE (typetail)
3693 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3695 for (; typetail != void_list_node; ++i)
3697 tree parmval
3698 = convert_default_arg (TREE_VALUE (typetail),
3699 TREE_PURPOSE (typetail),
3700 fndecl, i, complain);
3702 if (parmval == error_mark_node)
3703 return -1;
3705 vec_safe_push (*values, parmval);
3706 typetail = TREE_CHAIN (typetail);
3707 /* ends with `...'. */
3708 if (typetail == NULL_TREE)
3709 break;
3712 else
3714 if (complain & tf_error)
3715 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3716 return -1;
3720 return (int) i;
3723 /* Build a binary-operation expression, after performing default
3724 conversions on the operands. CODE is the kind of expression to
3725 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3726 are the tree codes which correspond to ARG1 and ARG2 when issuing
3727 warnings about possibly misplaced parentheses. They may differ
3728 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3729 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3730 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3731 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3732 ARG2_CODE as ERROR_MARK. */
3734 tree
3735 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3736 enum tree_code arg1_code, tree arg2,
3737 enum tree_code arg2_code, tree *overload,
3738 tsubst_flags_t complain)
3740 tree orig_arg1;
3741 tree orig_arg2;
3742 tree expr;
3744 orig_arg1 = arg1;
3745 orig_arg2 = arg2;
3747 if (processing_template_decl)
3749 if (type_dependent_expression_p (arg1)
3750 || type_dependent_expression_p (arg2))
3751 return build_min_nt_loc (loc, code, arg1, arg2);
3752 arg1 = build_non_dependent_expr (arg1);
3753 arg2 = build_non_dependent_expr (arg2);
3756 if (code == DOTSTAR_EXPR)
3757 expr = build_m_component_ref (arg1, arg2, complain);
3758 else
3759 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3760 overload, complain);
3762 /* Check for cases such as x+y<<z which users are likely to
3763 misinterpret. But don't warn about obj << x + y, since that is a
3764 common idiom for I/O. */
3765 if (warn_parentheses
3766 && (complain & tf_warning)
3767 && !processing_template_decl
3768 && !error_operand_p (arg1)
3769 && !error_operand_p (arg2)
3770 && (code != LSHIFT_EXPR
3771 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3772 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3773 arg2_code, orig_arg2);
3775 if (processing_template_decl && expr != error_mark_node)
3776 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3778 return expr;
3781 /* Build and return an ARRAY_REF expression. */
3783 tree
3784 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3785 tsubst_flags_t complain)
3787 tree orig_arg1 = arg1;
3788 tree orig_arg2 = arg2;
3789 tree expr;
3791 if (processing_template_decl)
3793 if (type_dependent_expression_p (arg1)
3794 || type_dependent_expression_p (arg2))
3795 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3796 NULL_TREE, NULL_TREE);
3797 arg1 = build_non_dependent_expr (arg1);
3798 arg2 = build_non_dependent_expr (arg2);
3801 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3802 NULL_TREE, /*overload=*/NULL, complain);
3804 if (processing_template_decl && expr != error_mark_node)
3805 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3806 NULL_TREE, NULL_TREE);
3807 return expr;
3810 /* Return whether OP is an expression of enum type cast to integer
3811 type. In C++ even unsigned enum types are cast to signed integer
3812 types. We do not want to issue warnings about comparisons between
3813 signed and unsigned types when one of the types is an enum type.
3814 Those warnings are always false positives in practice. */
3816 static bool
3817 enum_cast_to_int (tree op)
3819 if (TREE_CODE (op) == NOP_EXPR
3820 && TREE_TYPE (op) == integer_type_node
3821 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3822 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3823 return true;
3825 /* The cast may have been pushed into a COND_EXPR. */
3826 if (TREE_CODE (op) == COND_EXPR)
3827 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3828 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3830 return false;
3833 /* For the c-common bits. */
3834 tree
3835 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3836 int /*convert_p*/)
3838 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3842 /* Build a binary-operation expression without default conversions.
3843 CODE is the kind of expression to build.
3844 LOCATION is the location_t of the operator in the source code.
3845 This function differs from `build' in several ways:
3846 the data type of the result is computed and recorded in it,
3847 warnings are generated if arg data types are invalid,
3848 special handling for addition and subtraction of pointers is known,
3849 and some optimization is done (operations on narrow ints
3850 are done in the narrower type when that gives the same result).
3851 Constant folding is also done before the result is returned.
3853 Note that the operands will never have enumeral types
3854 because either they have just had the default conversions performed
3855 or they have both just been converted to some other type in which
3856 the arithmetic is to be done.
3858 C++: must do special pointer arithmetic when implementing
3859 multiple inheritance, and deal with pointer to member functions. */
3861 tree
3862 cp_build_binary_op (location_t location,
3863 enum tree_code code, tree orig_op0, tree orig_op1,
3864 tsubst_flags_t complain)
3866 tree op0, op1;
3867 enum tree_code code0, code1;
3868 tree type0, type1;
3869 const char *invalid_op_diag;
3871 /* Expression code to give to the expression when it is built.
3872 Normally this is CODE, which is what the caller asked for,
3873 but in some special cases we change it. */
3874 enum tree_code resultcode = code;
3876 /* Data type in which the computation is to be performed.
3877 In the simplest cases this is the common type of the arguments. */
3878 tree result_type = NULL;
3880 /* Nonzero means operands have already been type-converted
3881 in whatever way is necessary.
3882 Zero means they need to be converted to RESULT_TYPE. */
3883 int converted = 0;
3885 /* Nonzero means create the expression with this type, rather than
3886 RESULT_TYPE. */
3887 tree build_type = 0;
3889 /* Nonzero means after finally constructing the expression
3890 convert it to this type. */
3891 tree final_type = 0;
3893 tree result;
3894 tree orig_type = NULL;
3896 /* Nonzero if this is an operation like MIN or MAX which can
3897 safely be computed in short if both args are promoted shorts.
3898 Also implies COMMON.
3899 -1 indicates a bitwise operation; this makes a difference
3900 in the exact conditions for when it is safe to do the operation
3901 in a narrower mode. */
3902 int shorten = 0;
3904 /* Nonzero if this is a comparison operation;
3905 if both args are promoted shorts, compare the original shorts.
3906 Also implies COMMON. */
3907 int short_compare = 0;
3909 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3910 int common = 0;
3912 /* True if both operands have arithmetic type. */
3913 bool arithmetic_types_p;
3915 /* Apply default conversions. */
3916 op0 = orig_op0;
3917 op1 = orig_op1;
3919 /* Remember whether we're doing / or %. */
3920 bool doing_div_or_mod = false;
3922 /* Remember whether we're doing << or >>. */
3923 bool doing_shift = false;
3925 /* Tree holding instrumentation expression. */
3926 tree instrument_expr = NULL;
3928 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3929 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3930 || code == TRUTH_XOR_EXPR)
3932 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3933 op0 = decay_conversion (op0, complain);
3934 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3935 op1 = decay_conversion (op1, complain);
3937 else
3939 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3940 op0 = cp_default_conversion (op0, complain);
3941 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3942 op1 = cp_default_conversion (op1, complain);
3945 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3946 STRIP_TYPE_NOPS (op0);
3947 STRIP_TYPE_NOPS (op1);
3949 /* DTRT if one side is an overloaded function, but complain about it. */
3950 if (type_unknown_p (op0))
3952 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3953 if (t != error_mark_node)
3955 if (complain & tf_error)
3956 permerror (input_location, "assuming cast to type %qT from overloaded function",
3957 TREE_TYPE (t));
3958 op0 = t;
3961 if (type_unknown_p (op1))
3963 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3964 if (t != error_mark_node)
3966 if (complain & tf_error)
3967 permerror (input_location, "assuming cast to type %qT from overloaded function",
3968 TREE_TYPE (t));
3969 op1 = t;
3973 type0 = TREE_TYPE (op0);
3974 type1 = TREE_TYPE (op1);
3976 /* The expression codes of the data types of the arguments tell us
3977 whether the arguments are integers, floating, pointers, etc. */
3978 code0 = TREE_CODE (type0);
3979 code1 = TREE_CODE (type1);
3981 /* If an error was already reported for one of the arguments,
3982 avoid reporting another error. */
3983 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3984 return error_mark_node;
3986 if ((invalid_op_diag
3987 = targetm.invalid_binary_op (code, type0, type1)))
3989 if (complain & tf_error)
3990 error (invalid_op_diag);
3991 return error_mark_node;
3994 /* Issue warnings about peculiar, but valid, uses of NULL. */
3995 if ((orig_op0 == null_node || orig_op1 == null_node)
3996 /* It's reasonable to use pointer values as operands of &&
3997 and ||, so NULL is no exception. */
3998 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3999 && ( /* Both are NULL (or 0) and the operation was not a
4000 comparison or a pointer subtraction. */
4001 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4002 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4003 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4004 || (!null_ptr_cst_p (orig_op0)
4005 && !TYPE_PTR_OR_PTRMEM_P (type0))
4006 || (!null_ptr_cst_p (orig_op1)
4007 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4008 && (complain & tf_warning))
4010 source_location loc =
4011 expansion_point_location_if_in_system_header (input_location);
4013 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4016 /* In case when one of the operands of the binary operation is
4017 a vector and another is a scalar -- convert scalar to vector. */
4018 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4020 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4021 complain & tf_error);
4023 switch (convert_flag)
4025 case stv_error:
4026 return error_mark_node;
4027 case stv_firstarg:
4029 op0 = save_expr (op0);
4030 op0 = convert (TREE_TYPE (type1), op0);
4031 op0 = build_vector_from_val (type1, op0);
4032 type0 = TREE_TYPE (op0);
4033 code0 = TREE_CODE (type0);
4034 converted = 1;
4035 break;
4037 case stv_secondarg:
4039 op1 = save_expr (op1);
4040 op1 = convert (TREE_TYPE (type0), op1);
4041 op1 = build_vector_from_val (type0, op1);
4042 type1 = TREE_TYPE (op1);
4043 code1 = TREE_CODE (type1);
4044 converted = 1;
4045 break;
4047 default:
4048 break;
4052 switch (code)
4054 case MINUS_EXPR:
4055 /* Subtraction of two similar pointers.
4056 We must subtract them as integers, then divide by object size. */
4057 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4058 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4059 TREE_TYPE (type1)))
4060 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4061 complain);
4062 /* In all other cases except pointer - int, the usual arithmetic
4063 rules apply. */
4064 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4066 common = 1;
4067 break;
4069 /* The pointer - int case is just like pointer + int; fall
4070 through. */
4071 case PLUS_EXPR:
4072 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4073 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4075 tree ptr_operand;
4076 tree int_operand;
4077 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4078 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4079 if (processing_template_decl)
4081 result_type = TREE_TYPE (ptr_operand);
4082 break;
4084 return cp_pointer_int_sum (code,
4085 ptr_operand,
4086 int_operand,
4087 complain);
4089 common = 1;
4090 break;
4092 case MULT_EXPR:
4093 common = 1;
4094 break;
4096 case TRUNC_DIV_EXPR:
4097 case CEIL_DIV_EXPR:
4098 case FLOOR_DIV_EXPR:
4099 case ROUND_DIV_EXPR:
4100 case EXACT_DIV_EXPR:
4101 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4102 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4103 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4104 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4106 enum tree_code tcode0 = code0, tcode1 = code1;
4107 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4108 cop1 = maybe_constant_value (cop1);
4110 if (tcode0 == INTEGER_TYPE)
4111 doing_div_or_mod = true;
4113 warn_for_div_by_zero (location, cop1);
4115 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4116 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4117 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4118 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4120 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4121 resultcode = RDIV_EXPR;
4122 else
4123 /* When dividing two signed integers, we have to promote to int.
4124 unless we divide by a constant != -1. Note that default
4125 conversion will have been performed on the operands at this
4126 point, so we have to dig out the original type to find out if
4127 it was unsigned. */
4128 shorten = ((TREE_CODE (op0) == NOP_EXPR
4129 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4130 || (TREE_CODE (op1) == INTEGER_CST
4131 && ! integer_all_onesp (op1)));
4133 common = 1;
4135 break;
4137 case BIT_AND_EXPR:
4138 case BIT_IOR_EXPR:
4139 case BIT_XOR_EXPR:
4140 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4141 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4142 && !VECTOR_FLOAT_TYPE_P (type0)
4143 && !VECTOR_FLOAT_TYPE_P (type1)))
4144 shorten = -1;
4145 break;
4147 case TRUNC_MOD_EXPR:
4148 case FLOOR_MOD_EXPR:
4150 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4151 cop1 = maybe_constant_value (cop1);
4153 if (code0 == INTEGER_TYPE)
4154 doing_div_or_mod = true;
4155 warn_for_div_by_zero (location, cop1);
4158 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4159 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4160 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4161 common = 1;
4162 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4164 /* Although it would be tempting to shorten always here, that loses
4165 on some targets, since the modulo instruction is undefined if the
4166 quotient can't be represented in the computation mode. We shorten
4167 only if unsigned or if dividing by something we know != -1. */
4168 shorten = ((TREE_CODE (op0) == NOP_EXPR
4169 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4170 || (TREE_CODE (op1) == INTEGER_CST
4171 && ! integer_all_onesp (op1)));
4172 common = 1;
4174 break;
4176 case TRUTH_ANDIF_EXPR:
4177 case TRUTH_ORIF_EXPR:
4178 case TRUTH_AND_EXPR:
4179 case TRUTH_OR_EXPR:
4180 result_type = boolean_type_node;
4181 break;
4183 /* Shift operations: result has same type as first operand;
4184 always convert second operand to int.
4185 Also set SHORT_SHIFT if shifting rightward. */
4187 case RSHIFT_EXPR:
4188 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4189 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4191 result_type = type0;
4192 converted = 1;
4194 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4195 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4196 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4197 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4199 result_type = type0;
4200 converted = 1;
4202 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4204 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4205 const_op1 = maybe_constant_value (const_op1);
4206 if (TREE_CODE (const_op1) != INTEGER_CST)
4207 const_op1 = op1;
4208 result_type = type0;
4209 doing_shift = true;
4210 if (TREE_CODE (const_op1) == INTEGER_CST)
4212 if (tree_int_cst_lt (const_op1, integer_zero_node))
4214 if ((complain & tf_warning)
4215 && c_inhibit_evaluation_warnings == 0)
4216 warning (0, "right shift count is negative");
4218 else
4220 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4221 && (complain & tf_warning)
4222 && c_inhibit_evaluation_warnings == 0)
4223 warning (0, "right shift count >= width of type");
4226 /* Convert the shift-count to an integer, regardless of
4227 size of value being shifted. */
4228 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4229 op1 = cp_convert (integer_type_node, op1, complain);
4230 /* Avoid converting op1 to result_type later. */
4231 converted = 1;
4233 break;
4235 case LSHIFT_EXPR:
4236 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4237 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4239 result_type = type0;
4240 converted = 1;
4242 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4243 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4244 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4245 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4247 result_type = type0;
4248 converted = 1;
4250 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4252 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4253 const_op1 = maybe_constant_value (const_op1);
4254 if (TREE_CODE (const_op1) != INTEGER_CST)
4255 const_op1 = op1;
4256 result_type = type0;
4257 doing_shift = true;
4258 if (TREE_CODE (const_op1) == INTEGER_CST)
4260 if (tree_int_cst_lt (const_op1, integer_zero_node))
4262 if ((complain & tf_warning)
4263 && c_inhibit_evaluation_warnings == 0)
4264 warning (0, "left shift count is negative");
4266 else if (compare_tree_int (const_op1,
4267 TYPE_PRECISION (type0)) >= 0)
4269 if ((complain & tf_warning)
4270 && c_inhibit_evaluation_warnings == 0)
4271 warning (0, "left shift count >= width of type");
4274 /* Convert the shift-count to an integer, regardless of
4275 size of value being shifted. */
4276 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4277 op1 = cp_convert (integer_type_node, op1, complain);
4278 /* Avoid converting op1 to result_type later. */
4279 converted = 1;
4281 break;
4283 case RROTATE_EXPR:
4284 case LROTATE_EXPR:
4285 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4287 result_type = type0;
4288 if (TREE_CODE (op1) == INTEGER_CST)
4290 if (tree_int_cst_lt (op1, integer_zero_node))
4292 if (complain & tf_warning)
4293 warning (0, (code == LROTATE_EXPR)
4294 ? G_("left rotate count is negative")
4295 : G_("right rotate count is negative"));
4297 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4299 if (complain & tf_warning)
4300 warning (0, (code == LROTATE_EXPR)
4301 ? G_("left rotate count >= width of type")
4302 : G_("right rotate count >= width of type"));
4305 /* Convert the shift-count to an integer, regardless of
4306 size of value being shifted. */
4307 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4308 op1 = cp_convert (integer_type_node, op1, complain);
4310 break;
4312 case EQ_EXPR:
4313 case NE_EXPR:
4314 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4315 goto vector_compare;
4316 if ((complain & tf_warning)
4317 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4318 warning (OPT_Wfloat_equal,
4319 "comparing floating point with == or != is unsafe");
4320 if ((complain & tf_warning)
4321 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4322 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4323 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4325 build_type = boolean_type_node;
4326 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4327 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4328 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4329 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4330 short_compare = 1;
4331 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4332 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4333 result_type = composite_pointer_type (type0, type1, op0, op1,
4334 CPO_COMPARISON, complain);
4335 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4336 && null_ptr_cst_p (op1))
4338 if (TREE_CODE (op0) == ADDR_EXPR
4339 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4341 if ((complain & tf_warning)
4342 && c_inhibit_evaluation_warnings == 0)
4343 warning (OPT_Waddress, "the address of %qD will never be NULL",
4344 TREE_OPERAND (op0, 0));
4346 result_type = type0;
4348 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4349 && null_ptr_cst_p (op0))
4351 if (TREE_CODE (op1) == ADDR_EXPR
4352 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4354 if ((complain & tf_warning)
4355 && c_inhibit_evaluation_warnings == 0)
4356 warning (OPT_Waddress, "the address of %qD will never be NULL",
4357 TREE_OPERAND (op1, 0));
4359 result_type = type1;
4361 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4362 /* One of the operands must be of nullptr_t type. */
4363 result_type = TREE_TYPE (nullptr_node);
4364 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4366 result_type = type0;
4367 if (complain & tf_error)
4368 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4369 else
4370 return error_mark_node;
4372 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4374 result_type = type1;
4375 if (complain & tf_error)
4376 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4377 else
4378 return error_mark_node;
4380 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4382 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4383 == ptrmemfunc_vbit_in_delta)
4385 tree pfn0, delta0, e1, e2;
4387 if (TREE_SIDE_EFFECTS (op0))
4388 op0 = save_expr (op0);
4390 pfn0 = pfn_from_ptrmemfunc (op0);
4391 delta0 = delta_from_ptrmemfunc (op0);
4392 e1 = cp_build_binary_op (location,
4393 EQ_EXPR,
4394 pfn0,
4395 build_zero_cst (TREE_TYPE (pfn0)),
4396 complain);
4397 e2 = cp_build_binary_op (location,
4398 BIT_AND_EXPR,
4399 delta0,
4400 integer_one_node,
4401 complain);
4403 if (complain & tf_warning)
4404 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4406 e2 = cp_build_binary_op (location,
4407 EQ_EXPR, e2, integer_zero_node,
4408 complain);
4409 op0 = cp_build_binary_op (location,
4410 TRUTH_ANDIF_EXPR, e1, e2,
4411 complain);
4412 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4414 else
4416 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4417 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4419 result_type = TREE_TYPE (op0);
4421 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4422 return cp_build_binary_op (location, code, op1, op0, complain);
4423 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4425 tree type;
4426 /* E will be the final comparison. */
4427 tree e;
4428 /* E1 and E2 are for scratch. */
4429 tree e1;
4430 tree e2;
4431 tree pfn0;
4432 tree pfn1;
4433 tree delta0;
4434 tree delta1;
4436 type = composite_pointer_type (type0, type1, op0, op1,
4437 CPO_COMPARISON, complain);
4439 if (!same_type_p (TREE_TYPE (op0), type))
4440 op0 = cp_convert_and_check (type, op0, complain);
4441 if (!same_type_p (TREE_TYPE (op1), type))
4442 op1 = cp_convert_and_check (type, op1, complain);
4444 if (op0 == error_mark_node || op1 == error_mark_node)
4445 return error_mark_node;
4447 if (TREE_SIDE_EFFECTS (op0))
4448 op0 = save_expr (op0);
4449 if (TREE_SIDE_EFFECTS (op1))
4450 op1 = save_expr (op1);
4452 pfn0 = pfn_from_ptrmemfunc (op0);
4453 pfn1 = pfn_from_ptrmemfunc (op1);
4454 delta0 = delta_from_ptrmemfunc (op0);
4455 delta1 = delta_from_ptrmemfunc (op1);
4456 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4457 == ptrmemfunc_vbit_in_delta)
4459 /* We generate:
4461 (op0.pfn == op1.pfn
4462 && ((op0.delta == op1.delta)
4463 || (!op0.pfn && op0.delta & 1 == 0
4464 && op1.delta & 1 == 0))
4466 The reason for the `!op0.pfn' bit is that a NULL
4467 pointer-to-member is any member with a zero PFN and
4468 LSB of the DELTA field is 0. */
4470 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4471 delta0,
4472 integer_one_node,
4473 complain);
4474 e1 = cp_build_binary_op (location,
4475 EQ_EXPR, e1, integer_zero_node,
4476 complain);
4477 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4478 delta1,
4479 integer_one_node,
4480 complain);
4481 e2 = cp_build_binary_op (location,
4482 EQ_EXPR, e2, integer_zero_node,
4483 complain);
4484 e1 = cp_build_binary_op (location,
4485 TRUTH_ANDIF_EXPR, e2, e1,
4486 complain);
4487 e2 = cp_build_binary_op (location, EQ_EXPR,
4488 pfn0,
4489 build_zero_cst (TREE_TYPE (pfn0)),
4490 complain);
4491 e2 = cp_build_binary_op (location,
4492 TRUTH_ANDIF_EXPR, e2, e1, complain);
4493 e1 = cp_build_binary_op (location,
4494 EQ_EXPR, delta0, delta1, complain);
4495 e1 = cp_build_binary_op (location,
4496 TRUTH_ORIF_EXPR, e1, e2, complain);
4498 else
4500 /* We generate:
4502 (op0.pfn == op1.pfn
4503 && (!op0.pfn || op0.delta == op1.delta))
4505 The reason for the `!op0.pfn' bit is that a NULL
4506 pointer-to-member is any member with a zero PFN; the
4507 DELTA field is unspecified. */
4509 e1 = cp_build_binary_op (location,
4510 EQ_EXPR, delta0, delta1, complain);
4511 e2 = cp_build_binary_op (location,
4512 EQ_EXPR,
4513 pfn0,
4514 build_zero_cst (TREE_TYPE (pfn0)),
4515 complain);
4516 e1 = cp_build_binary_op (location,
4517 TRUTH_ORIF_EXPR, e1, e2, complain);
4519 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4520 e = cp_build_binary_op (location,
4521 TRUTH_ANDIF_EXPR, e2, e1, complain);
4522 if (code == EQ_EXPR)
4523 return e;
4524 return cp_build_binary_op (location,
4525 EQ_EXPR, e, integer_zero_node, complain);
4527 else
4529 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4530 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4531 type1));
4532 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4533 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4534 type0));
4537 break;
4539 case MAX_EXPR:
4540 case MIN_EXPR:
4541 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4542 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4543 shorten = 1;
4544 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4545 result_type = composite_pointer_type (type0, type1, op0, op1,
4546 CPO_COMPARISON, complain);
4547 break;
4549 case LE_EXPR:
4550 case GE_EXPR:
4551 case LT_EXPR:
4552 case GT_EXPR:
4553 if (TREE_CODE (orig_op0) == STRING_CST
4554 || TREE_CODE (orig_op1) == STRING_CST)
4556 if (complain & tf_warning)
4557 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4560 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4562 vector_compare:
4563 tree intt;
4564 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4565 TREE_TYPE (type1))
4566 && !vector_types_compatible_elements_p (type0, type1))
4568 if (complain & tf_error)
4570 error_at (location, "comparing vectors with different "
4571 "element types");
4572 inform (location, "operand types are %qT and %qT",
4573 type0, type1);
4575 return error_mark_node;
4578 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4580 if (complain & tf_error)
4582 error_at (location, "comparing vectors with different "
4583 "number of elements");
4584 inform (location, "operand types are %qT and %qT",
4585 type0, type1);
4587 return error_mark_node;
4590 /* Always construct signed integer vector type. */
4591 intt = c_common_type_for_size (GET_MODE_BITSIZE
4592 (TYPE_MODE (TREE_TYPE (type0))), 0);
4593 if (!intt)
4595 if (complain & tf_error)
4596 error_at (location, "could not find an integer type "
4597 "of the same size as %qT", TREE_TYPE (type0));
4598 return error_mark_node;
4600 result_type = build_opaque_vector_type (intt,
4601 TYPE_VECTOR_SUBPARTS (type0));
4602 converted = 1;
4603 break;
4605 build_type = boolean_type_node;
4606 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4607 || code0 == ENUMERAL_TYPE)
4608 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4609 || code1 == ENUMERAL_TYPE))
4610 short_compare = 1;
4611 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4612 result_type = composite_pointer_type (type0, type1, op0, op1,
4613 CPO_COMPARISON, complain);
4614 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4616 result_type = type0;
4617 if (extra_warnings && (complain & tf_warning))
4618 warning (OPT_Wextra,
4619 "ordered comparison of pointer with integer zero");
4621 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4623 result_type = type1;
4624 if (extra_warnings && (complain & tf_warning))
4625 warning (OPT_Wextra,
4626 "ordered comparison of pointer with integer zero");
4628 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4629 /* One of the operands must be of nullptr_t type. */
4630 result_type = TREE_TYPE (nullptr_node);
4631 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4633 result_type = type0;
4634 if (complain & tf_error)
4635 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4636 else
4637 return error_mark_node;
4639 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4641 result_type = type1;
4642 if (complain & tf_error)
4643 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4644 else
4645 return error_mark_node;
4647 break;
4649 case UNORDERED_EXPR:
4650 case ORDERED_EXPR:
4651 case UNLT_EXPR:
4652 case UNLE_EXPR:
4653 case UNGT_EXPR:
4654 case UNGE_EXPR:
4655 case UNEQ_EXPR:
4656 build_type = integer_type_node;
4657 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4659 if (complain & tf_error)
4660 error ("unordered comparison on non-floating point argument");
4661 return error_mark_node;
4663 common = 1;
4664 break;
4666 default:
4667 break;
4670 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4671 || code0 == ENUMERAL_TYPE)
4672 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4673 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4674 arithmetic_types_p = 1;
4675 else
4677 arithmetic_types_p = 0;
4678 /* Vector arithmetic is only allowed when both sides are vectors. */
4679 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4681 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4682 || !vector_types_compatible_elements_p (type0, type1))
4684 if (complain & tf_error)
4685 binary_op_error (location, code, type0, type1);
4686 return error_mark_node;
4688 arithmetic_types_p = 1;
4691 /* Determine the RESULT_TYPE, if it is not already known. */
4692 if (!result_type
4693 && arithmetic_types_p
4694 && (shorten || common || short_compare))
4696 result_type = cp_common_type (type0, type1);
4697 if (complain & tf_warning)
4698 do_warn_double_promotion (result_type, type0, type1,
4699 "implicit conversion from %qT to %qT "
4700 "to match other operand of binary "
4701 "expression",
4702 location);
4705 if (!result_type)
4707 if (complain & tf_error)
4708 error ("invalid operands of types %qT and %qT to binary %qO",
4709 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4710 return error_mark_node;
4713 /* If we're in a template, the only thing we need to know is the
4714 RESULT_TYPE. */
4715 if (processing_template_decl)
4717 /* Since the middle-end checks the type when doing a build2, we
4718 need to build the tree in pieces. This built tree will never
4719 get out of the front-end as we replace it when instantiating
4720 the template. */
4721 tree tmp = build2 (resultcode,
4722 build_type ? build_type : result_type,
4723 NULL_TREE, op1);
4724 TREE_OPERAND (tmp, 0) = op0;
4725 return tmp;
4728 if (arithmetic_types_p)
4730 bool first_complex = (code0 == COMPLEX_TYPE);
4731 bool second_complex = (code1 == COMPLEX_TYPE);
4732 int none_complex = (!first_complex && !second_complex);
4734 /* Adapted from patch for c/24581. */
4735 if (first_complex != second_complex
4736 && (code == PLUS_EXPR
4737 || code == MINUS_EXPR
4738 || code == MULT_EXPR
4739 || (code == TRUNC_DIV_EXPR && first_complex))
4740 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4741 && flag_signed_zeros)
4743 /* An operation on mixed real/complex operands must be
4744 handled specially, but the language-independent code can
4745 more easily optimize the plain complex arithmetic if
4746 -fno-signed-zeros. */
4747 tree real_type = TREE_TYPE (result_type);
4748 tree real, imag;
4749 if (first_complex)
4751 if (TREE_TYPE (op0) != result_type)
4752 op0 = cp_convert_and_check (result_type, op0, complain);
4753 if (TREE_TYPE (op1) != real_type)
4754 op1 = cp_convert_and_check (real_type, op1, complain);
4756 else
4758 if (TREE_TYPE (op0) != real_type)
4759 op0 = cp_convert_and_check (real_type, op0, complain);
4760 if (TREE_TYPE (op1) != result_type)
4761 op1 = cp_convert_and_check (result_type, op1, complain);
4763 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4764 return error_mark_node;
4765 if (first_complex)
4767 op0 = save_expr (op0);
4768 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4769 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4770 switch (code)
4772 case MULT_EXPR:
4773 case TRUNC_DIV_EXPR:
4774 op1 = save_expr (op1);
4775 imag = build2 (resultcode, real_type, imag, op1);
4776 /* Fall through. */
4777 case PLUS_EXPR:
4778 case MINUS_EXPR:
4779 real = build2 (resultcode, real_type, real, op1);
4780 break;
4781 default:
4782 gcc_unreachable();
4785 else
4787 op1 = save_expr (op1);
4788 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4789 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4790 switch (code)
4792 case MULT_EXPR:
4793 op0 = save_expr (op0);
4794 imag = build2 (resultcode, real_type, op0, imag);
4795 /* Fall through. */
4796 case PLUS_EXPR:
4797 real = build2 (resultcode, real_type, op0, real);
4798 break;
4799 case MINUS_EXPR:
4800 real = build2 (resultcode, real_type, op0, real);
4801 imag = build1 (NEGATE_EXPR, real_type, imag);
4802 break;
4803 default:
4804 gcc_unreachable();
4807 real = fold_if_not_in_template (real);
4808 imag = fold_if_not_in_template (imag);
4809 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4810 result = fold_if_not_in_template (result);
4811 return result;
4814 /* For certain operations (which identify themselves by shorten != 0)
4815 if both args were extended from the same smaller type,
4816 do the arithmetic in that type and then extend.
4818 shorten !=0 and !=1 indicates a bitwise operation.
4819 For them, this optimization is safe only if
4820 both args are zero-extended or both are sign-extended.
4821 Otherwise, we might change the result.
4822 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4823 but calculated in (unsigned short) it would be (unsigned short)-1. */
4825 if (shorten && none_complex)
4827 orig_type = result_type;
4828 final_type = result_type;
4829 result_type = shorten_binary_op (result_type, op0, op1,
4830 shorten == -1);
4833 /* Comparison operations are shortened too but differently.
4834 They identify themselves by setting short_compare = 1. */
4836 if (short_compare)
4838 /* Don't write &op0, etc., because that would prevent op0
4839 from being kept in a register.
4840 Instead, make copies of the our local variables and
4841 pass the copies by reference, then copy them back afterward. */
4842 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4843 enum tree_code xresultcode = resultcode;
4844 tree val
4845 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4846 &xresultcode);
4847 if (val != 0)
4848 return cp_convert (boolean_type_node, val, complain);
4849 op0 = xop0, op1 = xop1;
4850 converted = 1;
4851 resultcode = xresultcode;
4854 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4855 && warn_sign_compare
4856 /* Do not warn until the template is instantiated; we cannot
4857 bound the ranges of the arguments until that point. */
4858 && !processing_template_decl
4859 && (complain & tf_warning)
4860 && c_inhibit_evaluation_warnings == 0
4861 /* Even unsigned enum types promote to signed int. We don't
4862 want to issue -Wsign-compare warnings for this case. */
4863 && !enum_cast_to_int (orig_op0)
4864 && !enum_cast_to_int (orig_op1))
4866 tree oop0 = maybe_constant_value (orig_op0);
4867 tree oop1 = maybe_constant_value (orig_op1);
4869 if (TREE_CODE (oop0) != INTEGER_CST)
4870 oop0 = orig_op0;
4871 if (TREE_CODE (oop1) != INTEGER_CST)
4872 oop1 = orig_op1;
4873 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4874 result_type, resultcode);
4878 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4879 Then the expression will be built.
4880 It will be given type FINAL_TYPE if that is nonzero;
4881 otherwise, it will be given type RESULT_TYPE. */
4882 if (! converted)
4884 if (TREE_TYPE (op0) != result_type)
4885 op0 = cp_convert_and_check (result_type, op0, complain);
4886 if (TREE_TYPE (op1) != result_type)
4887 op1 = cp_convert_and_check (result_type, op1, complain);
4889 if (op0 == error_mark_node || op1 == error_mark_node)
4890 return error_mark_node;
4893 if (build_type == NULL_TREE)
4894 build_type = result_type;
4896 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
4897 && !processing_template_decl
4898 && current_function_decl != 0
4899 && !lookup_attribute ("no_sanitize_undefined",
4900 DECL_ATTRIBUTES (current_function_decl))
4901 && (doing_div_or_mod || doing_shift))
4903 /* OP0 and/or OP1 might have side-effects. */
4904 op0 = cp_save_expr (op0);
4905 op1 = cp_save_expr (op1);
4906 op0 = maybe_constant_value (fold_non_dependent_expr_sfinae (op0,
4907 tf_none));
4908 op1 = maybe_constant_value (fold_non_dependent_expr_sfinae (op1,
4909 tf_none));
4910 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
4912 /* For diagnostics we want to use the promoted types without
4913 shorten_binary_op. So convert the arguments to the
4914 original result_type. */
4915 tree cop0 = op0;
4916 tree cop1 = op1;
4917 if (orig_type != NULL && result_type != orig_type)
4919 cop0 = cp_convert (orig_type, op0, complain);
4920 cop1 = cp_convert (orig_type, op1, complain);
4922 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
4924 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
4925 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
4928 result = build2 (resultcode, build_type, op0, op1);
4929 result = fold_if_not_in_template (result);
4930 if (final_type != 0)
4931 result = cp_convert (final_type, result, complain);
4933 if (TREE_OVERFLOW_P (result)
4934 && !TREE_OVERFLOW_P (op0)
4935 && !TREE_OVERFLOW_P (op1))
4936 overflow_warning (location, result);
4938 if (instrument_expr != NULL)
4939 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
4940 instrument_expr, result);
4942 return result;
4945 /* Build a VEC_PERM_EXPR.
4946 This is a simple wrapper for c_build_vec_perm_expr. */
4947 tree
4948 build_x_vec_perm_expr (location_t loc,
4949 tree arg0, tree arg1, tree arg2,
4950 tsubst_flags_t complain)
4952 tree orig_arg0 = arg0;
4953 tree orig_arg1 = arg1;
4954 tree orig_arg2 = arg2;
4955 if (processing_template_decl)
4957 if (type_dependent_expression_p (arg0)
4958 || type_dependent_expression_p (arg1)
4959 || type_dependent_expression_p (arg2))
4960 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
4961 arg0 = build_non_dependent_expr (arg0);
4962 if (arg1)
4963 arg1 = build_non_dependent_expr (arg1);
4964 arg2 = build_non_dependent_expr (arg2);
4966 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
4967 if (processing_template_decl && exp != error_mark_node)
4968 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
4969 orig_arg1, orig_arg2);
4970 return exp;
4973 /* Return a tree for the sum or difference (RESULTCODE says which)
4974 of pointer PTROP and integer INTOP. */
4976 static tree
4977 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
4978 tsubst_flags_t complain)
4980 tree res_type = TREE_TYPE (ptrop);
4982 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4983 in certain circumstance (when it's valid to do so). So we need
4984 to make sure it's complete. We don't need to check here, if we
4985 can actually complete it at all, as those checks will be done in
4986 pointer_int_sum() anyway. */
4987 complete_type (TREE_TYPE (res_type));
4989 return pointer_int_sum (input_location, resultcode, ptrop,
4990 fold_if_not_in_template (intop),
4991 complain & tf_warning_or_error);
4994 /* Return a tree for the difference of pointers OP0 and OP1.
4995 The resulting tree has type int. */
4997 static tree
4998 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5000 tree result;
5001 tree restype = ptrdiff_type_node;
5002 tree target_type = TREE_TYPE (ptrtype);
5004 if (!complete_type_or_else (target_type, NULL_TREE))
5005 return error_mark_node;
5007 if (VOID_TYPE_P (target_type))
5009 if (complain & tf_error)
5010 permerror (input_location, "ISO C++ forbids using pointer of "
5011 "type %<void *%> in subtraction");
5012 else
5013 return error_mark_node;
5015 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5017 if (complain & tf_error)
5018 permerror (input_location, "ISO C++ forbids using pointer to "
5019 "a function in subtraction");
5020 else
5021 return error_mark_node;
5023 if (TREE_CODE (target_type) == METHOD_TYPE)
5025 if (complain & tf_error)
5026 permerror (input_location, "ISO C++ forbids using pointer to "
5027 "a method in subtraction");
5028 else
5029 return error_mark_node;
5032 /* First do the subtraction as integers;
5033 then drop through to build the divide operator. */
5035 op0 = cp_build_binary_op (input_location,
5036 MINUS_EXPR,
5037 cp_convert (restype, op0, complain),
5038 cp_convert (restype, op1, complain),
5039 complain);
5041 /* This generates an error if op1 is a pointer to an incomplete type. */
5042 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5044 if (complain & tf_error)
5045 error ("invalid use of a pointer to an incomplete type in "
5046 "pointer arithmetic");
5047 else
5048 return error_mark_node;
5051 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5053 if (complain & tf_error)
5054 error ("arithmetic on pointer to an empty aggregate");
5055 else
5056 return error_mark_node;
5059 op1 = (TYPE_PTROB_P (ptrtype)
5060 ? size_in_bytes (target_type)
5061 : integer_one_node);
5063 /* Do the division. */
5065 result = build2 (EXACT_DIV_EXPR, restype, op0,
5066 cp_convert (restype, op1, complain));
5067 return fold_if_not_in_template (result);
5070 /* Construct and perhaps optimize a tree representation
5071 for a unary operation. CODE, a tree_code, specifies the operation
5072 and XARG is the operand. */
5074 tree
5075 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5076 tsubst_flags_t complain)
5078 tree orig_expr = xarg;
5079 tree exp;
5080 int ptrmem = 0;
5082 if (processing_template_decl)
5084 if (type_dependent_expression_p (xarg))
5085 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5087 xarg = build_non_dependent_expr (xarg);
5090 exp = NULL_TREE;
5092 /* [expr.unary.op] says:
5094 The address of an object of incomplete type can be taken.
5096 (And is just the ordinary address operator, not an overloaded
5097 "operator &".) However, if the type is a template
5098 specialization, we must complete the type at this point so that
5099 an overloaded "operator &" will be available if required. */
5100 if (code == ADDR_EXPR
5101 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5102 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5103 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5104 || (TREE_CODE (xarg) == OFFSET_REF)))
5105 /* Don't look for a function. */;
5106 else
5107 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5108 NULL_TREE, /*overload=*/NULL, complain);
5109 if (!exp && code == ADDR_EXPR)
5111 if (is_overloaded_fn (xarg))
5113 tree fn = get_first_fn (xarg);
5114 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5116 if (complain & tf_error)
5117 error (DECL_CONSTRUCTOR_P (fn)
5118 ? G_("taking address of constructor %qE")
5119 : G_("taking address of destructor %qE"),
5120 xarg);
5121 return error_mark_node;
5125 /* A pointer to member-function can be formed only by saying
5126 &X::mf. */
5127 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5128 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5130 if (TREE_CODE (xarg) != OFFSET_REF
5131 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5133 if (complain & tf_error)
5135 error ("invalid use of %qE to form a "
5136 "pointer-to-member-function", xarg);
5137 if (TREE_CODE (xarg) != OFFSET_REF)
5138 inform (input_location, " a qualified-id is required");
5140 return error_mark_node;
5142 else
5144 if (complain & tf_error)
5145 error ("parentheses around %qE cannot be used to form a"
5146 " pointer-to-member-function",
5147 xarg);
5148 else
5149 return error_mark_node;
5150 PTRMEM_OK_P (xarg) = 1;
5154 if (TREE_CODE (xarg) == OFFSET_REF)
5156 ptrmem = PTRMEM_OK_P (xarg);
5158 if (!ptrmem && !flag_ms_extensions
5159 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5161 /* A single non-static member, make sure we don't allow a
5162 pointer-to-member. */
5163 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5164 TREE_OPERAND (xarg, 0),
5165 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5166 PTRMEM_OK_P (xarg) = ptrmem;
5170 exp = cp_build_addr_expr_strict (xarg, complain);
5173 if (processing_template_decl && exp != error_mark_node)
5174 exp = build_min_non_dep (code, exp, orig_expr,
5175 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5176 if (TREE_CODE (exp) == ADDR_EXPR)
5177 PTRMEM_OK_P (exp) = ptrmem;
5178 return exp;
5181 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5182 constants, where a null value is represented by an INTEGER_CST of
5183 -1. */
5185 tree
5186 cp_truthvalue_conversion (tree expr)
5188 tree type = TREE_TYPE (expr);
5189 if (TYPE_PTRDATAMEM_P (type)
5190 /* Avoid ICE on invalid use of non-static member function. */
5191 || TREE_CODE (expr) == FUNCTION_DECL)
5192 return build_binary_op (EXPR_LOCATION (expr),
5193 NE_EXPR, expr, nullptr_node, 1);
5194 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5196 /* With -Wzero-as-null-pointer-constant do not warn for an
5197 'if (p)' or a 'while (!p)', where p is a pointer. */
5198 tree ret;
5199 ++c_inhibit_evaluation_warnings;
5200 ret = c_common_truthvalue_conversion (input_location, expr);
5201 --c_inhibit_evaluation_warnings;
5202 return ret;
5204 else
5205 return c_common_truthvalue_conversion (input_location, expr);
5208 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5210 tree
5211 condition_conversion (tree expr)
5213 tree t;
5214 if (processing_template_decl)
5215 return expr;
5216 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5217 tf_warning_or_error, LOOKUP_NORMAL);
5218 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5219 return t;
5222 /* Returns the address of T. This function will fold away
5223 ADDR_EXPR of INDIRECT_REF. */
5225 tree
5226 build_address (tree t)
5228 if (error_operand_p (t) || !cxx_mark_addressable (t))
5229 return error_mark_node;
5230 t = build_fold_addr_expr (t);
5231 if (TREE_CODE (t) != ADDR_EXPR)
5232 t = rvalue (t);
5233 return t;
5236 /* Returns the address of T with type TYPE. */
5238 tree
5239 build_typed_address (tree t, tree type)
5241 if (error_operand_p (t) || !cxx_mark_addressable (t))
5242 return error_mark_node;
5243 t = build_fold_addr_expr_with_type (t, type);
5244 if (TREE_CODE (t) != ADDR_EXPR)
5245 t = rvalue (t);
5246 return t;
5249 /* Return a NOP_EXPR converting EXPR to TYPE. */
5251 tree
5252 build_nop (tree type, tree expr)
5254 if (type == error_mark_node || error_operand_p (expr))
5255 return expr;
5256 return build1 (NOP_EXPR, type, expr);
5259 /* Take the address of ARG, whatever that means under C++ semantics.
5260 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5261 and class rvalues as well.
5263 Nothing should call this function directly; instead, callers should use
5264 cp_build_addr_expr or cp_build_addr_expr_strict. */
5266 static tree
5267 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5269 tree argtype;
5270 tree val;
5272 if (!arg || error_operand_p (arg))
5273 return error_mark_node;
5275 arg = mark_lvalue_use (arg);
5276 argtype = lvalue_type (arg);
5278 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5280 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5281 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5283 /* They're trying to take the address of a unique non-static
5284 member function. This is ill-formed (except in MS-land),
5285 but let's try to DTRT.
5286 Note: We only handle unique functions here because we don't
5287 want to complain if there's a static overload; non-unique
5288 cases will be handled by instantiate_type. But we need to
5289 handle this case here to allow casts on the resulting PMF.
5290 We could defer this in non-MS mode, but it's easier to give
5291 a useful error here. */
5293 /* Inside constant member functions, the `this' pointer
5294 contains an extra const qualifier. TYPE_MAIN_VARIANT
5295 is used here to remove this const from the diagnostics
5296 and the created OFFSET_REF. */
5297 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5298 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5299 mark_used (fn);
5301 if (! flag_ms_extensions)
5303 tree name = DECL_NAME (fn);
5304 if (!(complain & tf_error))
5305 return error_mark_node;
5306 else if (current_class_type
5307 && TREE_OPERAND (arg, 0) == current_class_ref)
5308 /* An expression like &memfn. */
5309 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5310 " or parenthesized non-static member function to form"
5311 " a pointer to member function. Say %<&%T::%D%>",
5312 base, name);
5313 else
5314 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5315 " function to form a pointer to member function."
5316 " Say %<&%T::%D%>",
5317 base, name);
5319 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5322 /* Uninstantiated types are all functions. Taking the
5323 address of a function is a no-op, so just return the
5324 argument. */
5325 if (type_unknown_p (arg))
5326 return build1 (ADDR_EXPR, unknown_type_node, arg);
5328 if (TREE_CODE (arg) == OFFSET_REF)
5329 /* We want a pointer to member; bypass all the code for actually taking
5330 the address of something. */
5331 goto offset_ref;
5333 /* Anything not already handled and not a true memory reference
5334 is an error. */
5335 if (TREE_CODE (argtype) != FUNCTION_TYPE
5336 && TREE_CODE (argtype) != METHOD_TYPE)
5338 cp_lvalue_kind kind = lvalue_kind (arg);
5339 if (kind == clk_none)
5341 if (complain & tf_error)
5342 lvalue_error (input_location, lv_addressof);
5343 return error_mark_node;
5345 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5347 if (!(complain & tf_error))
5348 return error_mark_node;
5349 if (kind & clk_class)
5350 /* Make this a permerror because we used to accept it. */
5351 permerror (input_location, "taking address of temporary");
5352 else
5353 error ("taking address of xvalue (rvalue reference)");
5357 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5359 tree type = build_pointer_type (TREE_TYPE (argtype));
5360 arg = build1 (CONVERT_EXPR, type, arg);
5361 return arg;
5363 else if (pedantic && DECL_MAIN_P (arg))
5365 /* ARM $3.4 */
5366 /* Apparently a lot of autoconf scripts for C++ packages do this,
5367 so only complain if -Wpedantic. */
5368 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5369 pedwarn (input_location, OPT_Wpedantic,
5370 "ISO C++ forbids taking address of function %<::main%>");
5371 else if (flag_pedantic_errors)
5372 return error_mark_node;
5375 /* Let &* cancel out to simplify resulting code. */
5376 if (INDIRECT_REF_P (arg))
5378 /* We don't need to have `current_class_ptr' wrapped in a
5379 NON_LVALUE_EXPR node. */
5380 if (arg == current_class_ref)
5381 return current_class_ptr;
5383 arg = TREE_OPERAND (arg, 0);
5384 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5386 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5387 arg = build1 (CONVERT_EXPR, type, arg);
5389 else
5390 /* Don't let this be an lvalue. */
5391 arg = rvalue (arg);
5392 return arg;
5395 /* ??? Cope with user tricks that amount to offsetof. */
5396 if (TREE_CODE (argtype) != FUNCTION_TYPE
5397 && TREE_CODE (argtype) != METHOD_TYPE
5398 && argtype != unknown_type_node
5399 && (val = get_base_address (arg))
5400 && COMPLETE_TYPE_P (TREE_TYPE (val))
5401 && INDIRECT_REF_P (val)
5402 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5404 tree type = build_pointer_type (argtype);
5405 return fold_convert (type, fold_offsetof_1 (arg));
5408 /* Handle complex lvalues (when permitted)
5409 by reduction to simpler cases. */
5410 val = unary_complex_lvalue (ADDR_EXPR, arg);
5411 if (val != 0)
5412 return val;
5414 switch (TREE_CODE (arg))
5416 CASE_CONVERT:
5417 case FLOAT_EXPR:
5418 case FIX_TRUNC_EXPR:
5419 /* Even if we're not being pedantic, we cannot allow this
5420 extension when we're instantiating in a SFINAE
5421 context. */
5422 if (! lvalue_p (arg) && complain == tf_none)
5424 if (complain & tf_error)
5425 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5426 else
5427 return error_mark_node;
5429 break;
5431 case BASELINK:
5432 arg = BASELINK_FUNCTIONS (arg);
5433 /* Fall through. */
5435 case OVERLOAD:
5436 arg = OVL_CURRENT (arg);
5437 break;
5439 case OFFSET_REF:
5440 offset_ref:
5441 /* Turn a reference to a non-static data member into a
5442 pointer-to-member. */
5444 tree type;
5445 tree t;
5447 gcc_assert (PTRMEM_OK_P (arg));
5449 t = TREE_OPERAND (arg, 1);
5450 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5452 if (complain & tf_error)
5453 error ("cannot create pointer to reference member %qD", t);
5454 return error_mark_node;
5457 type = build_ptrmem_type (context_for_name_lookup (t),
5458 TREE_TYPE (t));
5459 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5460 return t;
5463 default:
5464 break;
5467 if (argtype != error_mark_node)
5469 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (argtype))
5471 if (complain & tf_warning_or_error)
5472 pedwarn (input_location, OPT_Wvla,
5473 "taking address of array of runtime bound");
5474 else
5475 return error_mark_node;
5477 argtype = build_pointer_type (argtype);
5480 /* In a template, we are processing a non-dependent expression
5481 so we can just form an ADDR_EXPR with the correct type. */
5482 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5484 val = build_address (arg);
5485 if (TREE_CODE (arg) == OFFSET_REF)
5486 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5488 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5490 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5492 /* We can only get here with a single static member
5493 function. */
5494 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5495 && DECL_STATIC_FUNCTION_P (fn));
5496 mark_used (fn);
5497 val = build_address (fn);
5498 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5499 /* Do not lose object's side effects. */
5500 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5501 TREE_OPERAND (arg, 0), val);
5503 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5505 if (complain & tf_error)
5506 error ("attempt to take address of bit-field structure member %qD",
5507 TREE_OPERAND (arg, 1));
5508 return error_mark_node;
5510 else
5512 tree object = TREE_OPERAND (arg, 0);
5513 tree field = TREE_OPERAND (arg, 1);
5514 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5515 (TREE_TYPE (object), decl_type_context (field)));
5516 val = build_address (arg);
5519 if (TYPE_PTR_P (argtype)
5520 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5522 build_ptrmemfunc_type (argtype);
5523 val = build_ptrmemfunc (argtype, val, 0,
5524 /*c_cast_p=*/false,
5525 complain);
5528 return val;
5531 /* Take the address of ARG if it has one, even if it's an rvalue. */
5533 tree
5534 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5536 return cp_build_addr_expr_1 (arg, 0, complain);
5539 /* Take the address of ARG, but only if it's an lvalue. */
5541 tree
5542 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5544 return cp_build_addr_expr_1 (arg, 1, complain);
5547 /* C++: Must handle pointers to members.
5549 Perhaps type instantiation should be extended to handle conversion
5550 from aggregates to types we don't yet know we want? (Or are those
5551 cases typically errors which should be reported?)
5553 NOCONVERT nonzero suppresses the default promotions
5554 (such as from short to int). */
5556 tree
5557 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5558 tsubst_flags_t complain)
5560 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5561 tree arg = xarg;
5562 tree argtype = 0;
5563 const char *errstring = NULL;
5564 tree val;
5565 const char *invalid_op_diag;
5567 if (!arg || error_operand_p (arg))
5568 return error_mark_node;
5570 if ((invalid_op_diag
5571 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5572 ? CONVERT_EXPR
5573 : code),
5574 TREE_TYPE (xarg))))
5576 if (complain & tf_error)
5577 error (invalid_op_diag);
5578 return error_mark_node;
5581 switch (code)
5583 case UNARY_PLUS_EXPR:
5584 case NEGATE_EXPR:
5586 int flags = WANT_ARITH | WANT_ENUM;
5587 /* Unary plus (but not unary minus) is allowed on pointers. */
5588 if (code == UNARY_PLUS_EXPR)
5589 flags |= WANT_POINTER;
5590 arg = build_expr_type_conversion (flags, arg, true);
5591 if (!arg)
5592 errstring = (code == NEGATE_EXPR
5593 ? _("wrong type argument to unary minus")
5594 : _("wrong type argument to unary plus"));
5595 else
5597 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5598 arg = cp_perform_integral_promotions (arg, complain);
5600 /* Make sure the result is not an lvalue: a unary plus or minus
5601 expression is always a rvalue. */
5602 arg = rvalue (arg);
5605 break;
5607 case BIT_NOT_EXPR:
5608 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5610 code = CONJ_EXPR;
5611 if (!noconvert)
5613 arg = cp_default_conversion (arg, complain);
5614 if (arg == error_mark_node)
5615 return error_mark_node;
5618 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5619 | WANT_VECTOR_OR_COMPLEX,
5620 arg, true)))
5621 errstring = _("wrong type argument to bit-complement");
5622 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5623 arg = cp_perform_integral_promotions (arg, complain);
5624 break;
5626 case ABS_EXPR:
5627 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5628 errstring = _("wrong type argument to abs");
5629 else if (!noconvert)
5631 arg = cp_default_conversion (arg, complain);
5632 if (arg == error_mark_node)
5633 return error_mark_node;
5635 break;
5637 case CONJ_EXPR:
5638 /* Conjugating a real value is a no-op, but allow it anyway. */
5639 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5640 errstring = _("wrong type argument to conjugation");
5641 else if (!noconvert)
5643 arg = cp_default_conversion (arg, complain);
5644 if (arg == error_mark_node)
5645 return error_mark_node;
5647 break;
5649 case TRUTH_NOT_EXPR:
5650 arg = perform_implicit_conversion (boolean_type_node, arg,
5651 complain);
5652 val = invert_truthvalue_loc (input_location, arg);
5653 if (arg != error_mark_node)
5654 return val;
5655 errstring = _("in argument to unary !");
5656 break;
5658 case NOP_EXPR:
5659 break;
5661 case REALPART_EXPR:
5662 case IMAGPART_EXPR:
5663 arg = build_real_imag_expr (input_location, code, arg);
5664 if (arg == error_mark_node)
5665 return arg;
5666 else
5667 return fold_if_not_in_template (arg);
5669 case PREINCREMENT_EXPR:
5670 case POSTINCREMENT_EXPR:
5671 case PREDECREMENT_EXPR:
5672 case POSTDECREMENT_EXPR:
5673 /* Handle complex lvalues (when permitted)
5674 by reduction to simpler cases. */
5676 val = unary_complex_lvalue (code, arg);
5677 if (val != 0)
5678 return val;
5680 arg = mark_lvalue_use (arg);
5682 /* Increment or decrement the real part of the value,
5683 and don't change the imaginary part. */
5684 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5686 tree real, imag;
5688 arg = stabilize_reference (arg);
5689 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5690 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5691 real = cp_build_unary_op (code, real, 1, complain);
5692 if (real == error_mark_node || imag == error_mark_node)
5693 return error_mark_node;
5694 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5695 real, imag);
5698 /* Report invalid types. */
5700 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5701 arg, true)))
5703 if (code == PREINCREMENT_EXPR)
5704 errstring = _("no pre-increment operator for type");
5705 else if (code == POSTINCREMENT_EXPR)
5706 errstring = _("no post-increment operator for type");
5707 else if (code == PREDECREMENT_EXPR)
5708 errstring = _("no pre-decrement operator for type");
5709 else
5710 errstring = _("no post-decrement operator for type");
5711 break;
5713 else if (arg == error_mark_node)
5714 return error_mark_node;
5716 /* Report something read-only. */
5718 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5719 || TREE_READONLY (arg))
5721 if (complain & tf_error)
5722 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5723 || code == POSTINCREMENT_EXPR)
5724 ? lv_increment : lv_decrement));
5725 else
5726 return error_mark_node;
5730 tree inc;
5731 tree declared_type = unlowered_expr_type (arg);
5733 argtype = TREE_TYPE (arg);
5735 /* ARM $5.2.5 last annotation says this should be forbidden. */
5736 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5738 if (complain & tf_error)
5739 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5740 ? G_("ISO C++ forbids incrementing an enum")
5741 : G_("ISO C++ forbids decrementing an enum"));
5742 else
5743 return error_mark_node;
5746 /* Compute the increment. */
5748 if (TYPE_PTR_P (argtype))
5750 tree type = complete_type (TREE_TYPE (argtype));
5752 if (!COMPLETE_OR_VOID_TYPE_P (type))
5754 if (complain & tf_error)
5755 error (((code == PREINCREMENT_EXPR
5756 || code == POSTINCREMENT_EXPR))
5757 ? G_("cannot increment a pointer to incomplete type %qT")
5758 : G_("cannot decrement a pointer to incomplete type %qT"),
5759 TREE_TYPE (argtype));
5760 else
5761 return error_mark_node;
5763 else if (!TYPE_PTROB_P (argtype))
5765 if (complain & tf_error)
5766 pedwarn (input_location, OPT_Wpointer_arith,
5767 (code == PREINCREMENT_EXPR
5768 || code == POSTINCREMENT_EXPR)
5769 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5770 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5771 argtype);
5772 else
5773 return error_mark_node;
5776 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5778 else
5779 inc = VECTOR_TYPE_P (argtype)
5780 ? build_one_cst (argtype)
5781 : integer_one_node;
5783 inc = cp_convert (argtype, inc, complain);
5785 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5786 need to ask Objective-C to build the increment or decrement
5787 expression for it. */
5788 if (objc_is_property_ref (arg))
5789 return objc_build_incr_expr_for_property_ref (input_location, code,
5790 arg, inc);
5792 /* Complain about anything else that is not a true lvalue. */
5793 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5794 || code == POSTINCREMENT_EXPR)
5795 ? lv_increment : lv_decrement),
5796 complain))
5797 return error_mark_node;
5799 /* Forbid using -- on `bool'. */
5800 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5802 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5804 if (complain & tf_error)
5805 error ("invalid use of Boolean expression as operand "
5806 "to %<operator--%>");
5807 return error_mark_node;
5809 val = boolean_increment (code, arg);
5811 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5812 /* An rvalue has no cv-qualifiers. */
5813 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5814 else
5815 val = build2 (code, TREE_TYPE (arg), arg, inc);
5817 TREE_SIDE_EFFECTS (val) = 1;
5818 return val;
5821 case ADDR_EXPR:
5822 /* Note that this operation never does default_conversion
5823 regardless of NOCONVERT. */
5824 return cp_build_addr_expr (arg, complain);
5826 default:
5827 break;
5830 if (!errstring)
5832 if (argtype == 0)
5833 argtype = TREE_TYPE (arg);
5834 return fold_if_not_in_template (build1 (code, argtype, arg));
5837 if (complain & tf_error)
5838 error ("%s", errstring);
5839 return error_mark_node;
5842 /* Hook for the c-common bits that build a unary op. */
5843 tree
5844 build_unary_op (location_t /*location*/,
5845 enum tree_code code, tree xarg, int noconvert)
5847 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5850 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5851 for certain kinds of expressions which are not really lvalues
5852 but which we can accept as lvalues.
5854 If ARG is not a kind of expression we can handle, return
5855 NULL_TREE. */
5857 tree
5858 unary_complex_lvalue (enum tree_code code, tree arg)
5860 /* Inside a template, making these kinds of adjustments is
5861 pointless; we are only concerned with the type of the
5862 expression. */
5863 if (processing_template_decl)
5864 return NULL_TREE;
5866 /* Handle (a, b) used as an "lvalue". */
5867 if (TREE_CODE (arg) == COMPOUND_EXPR)
5869 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5870 tf_warning_or_error);
5871 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5872 TREE_OPERAND (arg, 0), real_result);
5875 /* Handle (a ? b : c) used as an "lvalue". */
5876 if (TREE_CODE (arg) == COND_EXPR
5877 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5878 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5880 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5881 if (TREE_CODE (arg) == MODIFY_EXPR
5882 || TREE_CODE (arg) == PREINCREMENT_EXPR
5883 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5885 tree lvalue = TREE_OPERAND (arg, 0);
5886 if (TREE_SIDE_EFFECTS (lvalue))
5888 lvalue = stabilize_reference (lvalue);
5889 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5890 lvalue, TREE_OPERAND (arg, 1));
5892 return unary_complex_lvalue
5893 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5896 if (code != ADDR_EXPR)
5897 return NULL_TREE;
5899 /* Handle (a = b) used as an "lvalue" for `&'. */
5900 if (TREE_CODE (arg) == MODIFY_EXPR
5901 || TREE_CODE (arg) == INIT_EXPR)
5903 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5904 tf_warning_or_error);
5905 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5906 arg, real_result);
5907 TREE_NO_WARNING (arg) = 1;
5908 return arg;
5911 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5912 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5913 || TREE_CODE (arg) == OFFSET_REF)
5914 return NULL_TREE;
5916 /* We permit compiler to make function calls returning
5917 objects of aggregate type look like lvalues. */
5919 tree targ = arg;
5921 if (TREE_CODE (targ) == SAVE_EXPR)
5922 targ = TREE_OPERAND (targ, 0);
5924 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5926 if (TREE_CODE (arg) == SAVE_EXPR)
5927 targ = arg;
5928 else
5929 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5930 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5933 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
5934 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5935 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5938 /* Don't let anything else be handled specially. */
5939 return NULL_TREE;
5942 /* Mark EXP saying that we need to be able to take the
5943 address of it; it should not be allocated in a register.
5944 Value is true if successful.
5946 C++: we do not allow `current_class_ptr' to be addressable. */
5948 bool
5949 cxx_mark_addressable (tree exp)
5951 tree x = exp;
5953 while (1)
5954 switch (TREE_CODE (x))
5956 case ADDR_EXPR:
5957 case COMPONENT_REF:
5958 case ARRAY_REF:
5959 case REALPART_EXPR:
5960 case IMAGPART_EXPR:
5961 x = TREE_OPERAND (x, 0);
5962 break;
5964 case PARM_DECL:
5965 if (x == current_class_ptr)
5967 error ("cannot take the address of %<this%>, which is an rvalue expression");
5968 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5969 return true;
5971 /* Fall through. */
5973 case VAR_DECL:
5974 /* Caller should not be trying to mark initialized
5975 constant fields addressable. */
5976 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5977 || DECL_IN_AGGR_P (x) == 0
5978 || TREE_STATIC (x)
5979 || DECL_EXTERNAL (x));
5980 /* Fall through. */
5982 case RESULT_DECL:
5983 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5984 && !DECL_ARTIFICIAL (x))
5986 if (VAR_P (x) && DECL_HARD_REGISTER (x))
5988 error
5989 ("address of explicit register variable %qD requested", x);
5990 return false;
5992 else if (extra_warnings)
5993 warning
5994 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5996 TREE_ADDRESSABLE (x) = 1;
5997 return true;
5999 case CONST_DECL:
6000 case FUNCTION_DECL:
6001 TREE_ADDRESSABLE (x) = 1;
6002 return true;
6004 case CONSTRUCTOR:
6005 TREE_ADDRESSABLE (x) = 1;
6006 return true;
6008 case TARGET_EXPR:
6009 TREE_ADDRESSABLE (x) = 1;
6010 cxx_mark_addressable (TREE_OPERAND (x, 0));
6011 return true;
6013 default:
6014 return true;
6018 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6020 tree
6021 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6022 tsubst_flags_t complain)
6024 tree orig_ifexp = ifexp;
6025 tree orig_op1 = op1;
6026 tree orig_op2 = op2;
6027 tree expr;
6029 if (processing_template_decl)
6031 /* The standard says that the expression is type-dependent if
6032 IFEXP is type-dependent, even though the eventual type of the
6033 expression doesn't dependent on IFEXP. */
6034 if (type_dependent_expression_p (ifexp)
6035 /* As a GNU extension, the middle operand may be omitted. */
6036 || (op1 && type_dependent_expression_p (op1))
6037 || type_dependent_expression_p (op2))
6038 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6039 ifexp = build_non_dependent_expr (ifexp);
6040 if (op1)
6041 op1 = build_non_dependent_expr (op1);
6042 op2 = build_non_dependent_expr (op2);
6045 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6046 if (processing_template_decl && expr != error_mark_node
6047 && TREE_CODE (expr) != VEC_COND_EXPR)
6049 tree min = build_min_non_dep (COND_EXPR, expr,
6050 orig_ifexp, orig_op1, orig_op2);
6051 /* In C++11, remember that the result is an lvalue or xvalue.
6052 In C++98, lvalue_kind can just assume lvalue in a template. */
6053 if (cxx_dialect >= cxx11
6054 && lvalue_or_rvalue_with_address_p (expr)
6055 && !lvalue_or_rvalue_with_address_p (min))
6056 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6057 !real_lvalue_p (expr));
6058 expr = convert_from_reference (min);
6060 return expr;
6063 /* Given a list of expressions, return a compound expression
6064 that performs them all and returns the value of the last of them. */
6066 tree
6067 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6068 tsubst_flags_t complain)
6070 tree expr = TREE_VALUE (list);
6072 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6073 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6075 if (complain & tf_error)
6076 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6077 "list-initializer for non-class type must not "
6078 "be parenthesized");
6079 else
6080 return error_mark_node;
6083 if (TREE_CHAIN (list))
6085 if (complain & tf_error)
6086 switch (exp)
6088 case ELK_INIT:
6089 permerror (input_location, "expression list treated as compound "
6090 "expression in initializer");
6091 break;
6092 case ELK_MEM_INIT:
6093 permerror (input_location, "expression list treated as compound "
6094 "expression in mem-initializer");
6095 break;
6096 case ELK_FUNC_CAST:
6097 permerror (input_location, "expression list treated as compound "
6098 "expression in functional cast");
6099 break;
6100 default:
6101 gcc_unreachable ();
6103 else
6104 return error_mark_node;
6106 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6107 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6108 expr, TREE_VALUE (list), complain);
6111 return expr;
6114 /* Like build_x_compound_expr_from_list, but using a VEC. */
6116 tree
6117 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6118 tsubst_flags_t complain)
6120 if (vec_safe_is_empty (vec))
6121 return NULL_TREE;
6122 else if (vec->length () == 1)
6123 return (*vec)[0];
6124 else
6126 tree expr;
6127 unsigned int ix;
6128 tree t;
6130 if (msg != NULL)
6132 if (complain & tf_error)
6133 permerror (input_location,
6134 "%s expression list treated as compound expression",
6135 msg);
6136 else
6137 return error_mark_node;
6140 expr = (*vec)[0];
6141 for (ix = 1; vec->iterate (ix, &t); ++ix)
6142 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6143 t, complain);
6145 return expr;
6149 /* Handle overloading of the ',' operator when needed. */
6151 tree
6152 build_x_compound_expr (location_t loc, tree op1, tree op2,
6153 tsubst_flags_t complain)
6155 tree result;
6156 tree orig_op1 = op1;
6157 tree orig_op2 = op2;
6159 if (processing_template_decl)
6161 if (type_dependent_expression_p (op1)
6162 || type_dependent_expression_p (op2))
6163 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6164 op1 = build_non_dependent_expr (op1);
6165 op2 = build_non_dependent_expr (op2);
6168 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6169 NULL_TREE, /*overload=*/NULL, complain);
6170 if (!result)
6171 result = cp_build_compound_expr (op1, op2, complain);
6173 if (processing_template_decl && result != error_mark_node)
6174 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6176 return result;
6179 /* Like cp_build_compound_expr, but for the c-common bits. */
6181 tree
6182 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6184 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6187 /* Build a compound expression. */
6189 tree
6190 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6192 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6194 if (lhs == error_mark_node || rhs == error_mark_node)
6195 return error_mark_node;
6197 if (flag_cilkplus
6198 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6199 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6201 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6202 : EXPR_LOCATION (rhs));
6203 error_at (loc,
6204 "spawned function call cannot be part of a comma expression");
6205 return error_mark_node;
6208 if (TREE_CODE (rhs) == TARGET_EXPR)
6210 /* If the rhs is a TARGET_EXPR, then build the compound
6211 expression inside the target_expr's initializer. This
6212 helps the compiler to eliminate unnecessary temporaries. */
6213 tree init = TREE_OPERAND (rhs, 1);
6215 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6216 TREE_OPERAND (rhs, 1) = init;
6218 return rhs;
6221 if (type_unknown_p (rhs))
6223 if (complain & tf_error)
6224 error ("no context to resolve type of %qE", rhs);
6225 return error_mark_node;
6228 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6231 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6232 casts away constness. CAST gives the type of cast. Returns true
6233 if the cast is ill-formed, false if it is well-formed.
6235 ??? This function warns for casting away any qualifier not just
6236 const. We would like to specify exactly what qualifiers are casted
6237 away.
6240 static bool
6241 check_for_casting_away_constness (tree src_type, tree dest_type,
6242 enum tree_code cast, tsubst_flags_t complain)
6244 /* C-style casts are allowed to cast away constness. With
6245 WARN_CAST_QUAL, we still want to issue a warning. */
6246 if (cast == CAST_EXPR && !warn_cast_qual)
6247 return false;
6249 if (!casts_away_constness (src_type, dest_type, complain))
6250 return false;
6252 switch (cast)
6254 case CAST_EXPR:
6255 if (complain & tf_warning)
6256 warning (OPT_Wcast_qual,
6257 "cast from type %qT to type %qT casts away qualifiers",
6258 src_type, dest_type);
6259 return false;
6261 case STATIC_CAST_EXPR:
6262 if (complain & tf_error)
6263 error ("static_cast from type %qT to type %qT casts away qualifiers",
6264 src_type, dest_type);
6265 return true;
6267 case REINTERPRET_CAST_EXPR:
6268 if (complain & tf_error)
6269 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6270 src_type, dest_type);
6271 return true;
6273 default:
6274 gcc_unreachable();
6279 Warns if the cast from expression EXPR to type TYPE is useless.
6281 void
6282 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6284 if (warn_useless_cast
6285 && complain & tf_warning
6286 && c_inhibit_evaluation_warnings == 0)
6288 if (REFERENCE_REF_P (expr))
6289 expr = TREE_OPERAND (expr, 0);
6291 if ((TREE_CODE (type) == REFERENCE_TYPE
6292 && (TYPE_REF_IS_RVALUE (type)
6293 ? xvalue_p (expr) : real_lvalue_p (expr))
6294 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6295 || same_type_p (TREE_TYPE (expr), type))
6296 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6300 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6301 (another pointer-to-member type in the same hierarchy) and return
6302 the converted expression. If ALLOW_INVERSE_P is permitted, a
6303 pointer-to-derived may be converted to pointer-to-base; otherwise,
6304 only the other direction is permitted. If C_CAST_P is true, this
6305 conversion is taking place as part of a C-style cast. */
6307 tree
6308 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6309 bool c_cast_p, tsubst_flags_t complain)
6311 if (TYPE_PTRDATAMEM_P (type))
6313 tree delta;
6315 if (TREE_CODE (expr) == PTRMEM_CST)
6316 expr = cplus_expand_constant (expr);
6317 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6318 TYPE_PTRMEM_CLASS_TYPE (type),
6319 allow_inverse_p,
6320 c_cast_p, complain);
6321 if (delta == error_mark_node)
6322 return error_mark_node;
6324 if (!integer_zerop (delta))
6326 tree cond, op1, op2;
6328 cond = cp_build_binary_op (input_location,
6329 EQ_EXPR,
6330 expr,
6331 build_int_cst (TREE_TYPE (expr), -1),
6332 complain);
6333 op1 = build_nop (ptrdiff_type_node, expr);
6334 op2 = cp_build_binary_op (input_location,
6335 PLUS_EXPR, op1, delta,
6336 complain);
6338 expr = fold_build3_loc (input_location,
6339 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6343 return build_nop (type, expr);
6345 else
6346 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6347 allow_inverse_p, c_cast_p, complain);
6350 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6351 this static_cast is being attempted as one of the possible casts
6352 allowed by a C-style cast. (In that case, accessibility of base
6353 classes is not considered, and it is OK to cast away
6354 constness.) Return the result of the cast. *VALID_P is set to
6355 indicate whether or not the cast was valid. */
6357 static tree
6358 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6359 bool *valid_p, tsubst_flags_t complain)
6361 tree intype;
6362 tree result;
6363 cp_lvalue_kind clk;
6365 /* Assume the cast is valid. */
6366 *valid_p = true;
6368 intype = unlowered_expr_type (expr);
6370 /* Save casted types in the function's used types hash table. */
6371 used_types_insert (type);
6373 /* [expr.static.cast]
6375 An lvalue of type "cv1 B", where B is a class type, can be cast
6376 to type "reference to cv2 D", where D is a class derived (clause
6377 _class.derived_) from B, if a valid standard conversion from
6378 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6379 same cv-qualification as, or greater cv-qualification than, cv1,
6380 and B is not a virtual base class of D. */
6381 /* We check this case before checking the validity of "TYPE t =
6382 EXPR;" below because for this case:
6384 struct B {};
6385 struct D : public B { D(const B&); };
6386 extern B& b;
6387 void f() { static_cast<const D&>(b); }
6389 we want to avoid constructing a new D. The standard is not
6390 completely clear about this issue, but our interpretation is
6391 consistent with other compilers. */
6392 if (TREE_CODE (type) == REFERENCE_TYPE
6393 && CLASS_TYPE_P (TREE_TYPE (type))
6394 && CLASS_TYPE_P (intype)
6395 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6396 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6397 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6398 build_pointer_type (TYPE_MAIN_VARIANT
6399 (TREE_TYPE (type))),
6400 complain)
6401 && (c_cast_p
6402 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6404 tree base;
6406 /* There is a standard conversion from "D*" to "B*" even if "B"
6407 is ambiguous or inaccessible. If this is really a
6408 static_cast, then we check both for inaccessibility and
6409 ambiguity. However, if this is a static_cast being performed
6410 because the user wrote a C-style cast, then accessibility is
6411 not considered. */
6412 base = lookup_base (TREE_TYPE (type), intype,
6413 c_cast_p ? ba_unique : ba_check,
6414 NULL, complain);
6416 /* Convert from "B*" to "D*". This function will check that "B"
6417 is not a virtual base of "D". */
6418 expr = build_base_path (MINUS_EXPR, build_address (expr),
6419 base, /*nonnull=*/false, complain);
6420 /* Convert the pointer to a reference -- but then remember that
6421 there are no expressions with reference type in C++.
6423 We call rvalue so that there's an actual tree code
6424 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6425 is a variable with the same type, the conversion would get folded
6426 away, leaving just the variable and causing lvalue_kind to give
6427 the wrong answer. */
6428 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6431 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6432 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6433 if (TREE_CODE (type) == REFERENCE_TYPE
6434 && TYPE_REF_IS_RVALUE (type)
6435 && (clk = real_lvalue_p (expr))
6436 && reference_related_p (TREE_TYPE (type), intype)
6437 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6439 if (clk == clk_ordinary)
6441 /* Handle the (non-bit-field) lvalue case here by casting to
6442 lvalue reference and then changing it to an rvalue reference.
6443 Casting an xvalue to rvalue reference will be handled by the
6444 main code path. */
6445 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6446 result = (perform_direct_initialization_if_possible
6447 (lref, expr, c_cast_p, complain));
6448 result = cp_fold_convert (type, result);
6449 /* Make sure we don't fold back down to a named rvalue reference,
6450 because that would be an lvalue. */
6451 if (DECL_P (result))
6452 result = build1 (NON_LVALUE_EXPR, type, result);
6453 return convert_from_reference (result);
6455 else
6456 /* For a bit-field or packed field, bind to a temporary. */
6457 expr = rvalue (expr);
6460 /* Resolve overloaded address here rather than once in
6461 implicit_conversion and again in the inverse code below. */
6462 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6464 expr = instantiate_type (type, expr, complain);
6465 intype = TREE_TYPE (expr);
6468 /* [expr.static.cast]
6470 Any expression can be explicitly converted to type cv void. */
6471 if (VOID_TYPE_P (type))
6472 return convert_to_void (expr, ICV_CAST, complain);
6474 /* [class.abstract]
6475 An abstract class shall not be used ... as the type of an explicit
6476 conversion. */
6477 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6478 return error_mark_node;
6480 /* [expr.static.cast]
6482 An expression e can be explicitly converted to a type T using a
6483 static_cast of the form static_cast<T>(e) if the declaration T
6484 t(e);" is well-formed, for some invented temporary variable
6485 t. */
6486 result = perform_direct_initialization_if_possible (type, expr,
6487 c_cast_p, complain);
6488 if (result)
6490 result = convert_from_reference (result);
6492 /* [expr.static.cast]
6494 If T is a reference type, the result is an lvalue; otherwise,
6495 the result is an rvalue. */
6496 if (TREE_CODE (type) != REFERENCE_TYPE)
6497 result = rvalue (result);
6498 return result;
6501 /* [expr.static.cast]
6503 The inverse of any standard conversion sequence (clause _conv_),
6504 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6505 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6506 (_conv.bool_) conversions, can be performed explicitly using
6507 static_cast subject to the restriction that the explicit
6508 conversion does not cast away constness (_expr.const.cast_), and
6509 the following additional rules for specific cases: */
6510 /* For reference, the conversions not excluded are: integral
6511 promotions, floating point promotion, integral conversions,
6512 floating point conversions, floating-integral conversions,
6513 pointer conversions, and pointer to member conversions. */
6514 /* DR 128
6516 A value of integral _or enumeration_ type can be explicitly
6517 converted to an enumeration type. */
6518 /* The effect of all that is that any conversion between any two
6519 types which are integral, floating, or enumeration types can be
6520 performed. */
6521 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6522 || SCALAR_FLOAT_TYPE_P (type))
6523 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6524 || SCALAR_FLOAT_TYPE_P (intype)))
6525 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6527 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6528 && CLASS_TYPE_P (TREE_TYPE (type))
6529 && CLASS_TYPE_P (TREE_TYPE (intype))
6530 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6531 (TREE_TYPE (intype))),
6532 build_pointer_type (TYPE_MAIN_VARIANT
6533 (TREE_TYPE (type))),
6534 complain))
6536 tree base;
6538 if (!c_cast_p
6539 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6540 complain))
6541 return error_mark_node;
6542 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6543 c_cast_p ? ba_unique : ba_check,
6544 NULL, complain);
6545 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6546 complain);
6547 return cp_fold_convert(type, expr);
6550 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6551 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6553 tree c1;
6554 tree c2;
6555 tree t1;
6556 tree t2;
6558 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6559 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6561 if (TYPE_PTRDATAMEM_P (type))
6563 t1 = (build_ptrmem_type
6564 (c1,
6565 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6566 t2 = (build_ptrmem_type
6567 (c2,
6568 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6570 else
6572 t1 = intype;
6573 t2 = type;
6575 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6577 if (!c_cast_p
6578 && check_for_casting_away_constness (intype, type,
6579 STATIC_CAST_EXPR,
6580 complain))
6581 return error_mark_node;
6582 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6583 c_cast_p, complain);
6587 /* [expr.static.cast]
6589 An rvalue of type "pointer to cv void" can be explicitly
6590 converted to a pointer to object type. A value of type pointer
6591 to object converted to "pointer to cv void" and back to the
6592 original pointer type will have its original value. */
6593 if (TYPE_PTR_P (intype)
6594 && VOID_TYPE_P (TREE_TYPE (intype))
6595 && TYPE_PTROB_P (type))
6597 if (!c_cast_p
6598 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6599 complain))
6600 return error_mark_node;
6601 return build_nop (type, expr);
6604 *valid_p = false;
6605 return error_mark_node;
6608 /* Return an expression representing static_cast<TYPE>(EXPR). */
6610 tree
6611 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6613 tree result;
6614 bool valid_p;
6616 if (type == error_mark_node || expr == error_mark_node)
6617 return error_mark_node;
6619 if (processing_template_decl)
6621 expr = build_min (STATIC_CAST_EXPR, type, expr);
6622 /* We don't know if it will or will not have side effects. */
6623 TREE_SIDE_EFFECTS (expr) = 1;
6624 return convert_from_reference (expr);
6627 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6628 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6629 if (TREE_CODE (type) != REFERENCE_TYPE
6630 && TREE_CODE (expr) == NOP_EXPR
6631 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6632 expr = TREE_OPERAND (expr, 0);
6634 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6635 complain);
6636 if (valid_p)
6638 if (result != error_mark_node)
6639 maybe_warn_about_useless_cast (type, expr, complain);
6640 return result;
6643 if (complain & tf_error)
6644 error ("invalid static_cast from type %qT to type %qT",
6645 TREE_TYPE (expr), type);
6646 return error_mark_node;
6649 /* EXPR is an expression with member function or pointer-to-member
6650 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6651 not permitted by ISO C++, but we accept it in some modes. If we
6652 are not in one of those modes, issue a diagnostic. Return the
6653 converted expression. */
6655 tree
6656 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6658 tree intype;
6659 tree decl;
6661 intype = TREE_TYPE (expr);
6662 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6663 || TREE_CODE (intype) == METHOD_TYPE);
6665 if (!(complain & tf_warning_or_error))
6666 return error_mark_node;
6668 if (pedantic || warn_pmf2ptr)
6669 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6670 "converting from %qT to %qT", intype, type);
6672 if (TREE_CODE (intype) == METHOD_TYPE)
6673 expr = build_addr_func (expr, complain);
6674 else if (TREE_CODE (expr) == PTRMEM_CST)
6675 expr = build_address (PTRMEM_CST_MEMBER (expr));
6676 else
6678 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6679 decl = build_address (decl);
6680 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6683 if (expr == error_mark_node)
6684 return error_mark_node;
6686 return build_nop (type, expr);
6689 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6690 If C_CAST_P is true, this reinterpret cast is being done as part of
6691 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6692 indicate whether or not reinterpret_cast was valid. */
6694 static tree
6695 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6696 bool *valid_p, tsubst_flags_t complain)
6698 tree intype;
6700 /* Assume the cast is invalid. */
6701 if (valid_p)
6702 *valid_p = true;
6704 if (type == error_mark_node || error_operand_p (expr))
6705 return error_mark_node;
6707 intype = TREE_TYPE (expr);
6709 /* Save casted types in the function's used types hash table. */
6710 used_types_insert (type);
6712 /* [expr.reinterpret.cast]
6713 An lvalue expression of type T1 can be cast to the type
6714 "reference to T2" if an expression of type "pointer to T1" can be
6715 explicitly converted to the type "pointer to T2" using a
6716 reinterpret_cast. */
6717 if (TREE_CODE (type) == REFERENCE_TYPE)
6719 if (! real_lvalue_p (expr))
6721 if (complain & tf_error)
6722 error ("invalid cast of an rvalue expression of type "
6723 "%qT to type %qT",
6724 intype, type);
6725 return error_mark_node;
6728 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6729 "B" are related class types; the reinterpret_cast does not
6730 adjust the pointer. */
6731 if (TYPE_PTR_P (intype)
6732 && (complain & tf_warning)
6733 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6734 COMPARE_BASE | COMPARE_DERIVED)))
6735 warning (0, "casting %qT to %qT does not dereference pointer",
6736 intype, type);
6738 expr = cp_build_addr_expr (expr, complain);
6740 if (warn_strict_aliasing > 2)
6741 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6743 if (expr != error_mark_node)
6744 expr = build_reinterpret_cast_1
6745 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6746 valid_p, complain);
6747 if (expr != error_mark_node)
6748 /* cp_build_indirect_ref isn't right for rvalue refs. */
6749 expr = convert_from_reference (fold_convert (type, expr));
6750 return expr;
6753 /* As a G++ extension, we consider conversions from member
6754 functions, and pointers to member functions to
6755 pointer-to-function and pointer-to-void types. If
6756 -Wno-pmf-conversions has not been specified,
6757 convert_member_func_to_ptr will issue an error message. */
6758 if ((TYPE_PTRMEMFUNC_P (intype)
6759 || TREE_CODE (intype) == METHOD_TYPE)
6760 && TYPE_PTR_P (type)
6761 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6762 || VOID_TYPE_P (TREE_TYPE (type))))
6763 return convert_member_func_to_ptr (type, expr, complain);
6765 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6766 array-to-pointer, and function-to-pointer conversions are
6767 performed. */
6768 expr = decay_conversion (expr, complain);
6770 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6771 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6772 if (TREE_CODE (expr) == NOP_EXPR
6773 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6774 expr = TREE_OPERAND (expr, 0);
6776 if (error_operand_p (expr))
6777 return error_mark_node;
6779 intype = TREE_TYPE (expr);
6781 /* [expr.reinterpret.cast]
6782 A pointer can be converted to any integral type large enough to
6783 hold it. ... A value of type std::nullptr_t can be converted to
6784 an integral type; the conversion has the same meaning and
6785 validity as a conversion of (void*)0 to the integral type. */
6786 if (CP_INTEGRAL_TYPE_P (type)
6787 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6789 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6791 if (complain & tf_error)
6792 permerror (input_location, "cast from %qT to %qT loses precision",
6793 intype, type);
6794 else
6795 return error_mark_node;
6797 if (NULLPTR_TYPE_P (intype))
6798 return build_int_cst (type, 0);
6800 /* [expr.reinterpret.cast]
6801 A value of integral or enumeration type can be explicitly
6802 converted to a pointer. */
6803 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6804 /* OK */
6806 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6807 || TYPE_PTR_OR_PTRMEM_P (type))
6808 && same_type_p (type, intype))
6809 /* DR 799 */
6810 return fold_if_not_in_template (build_nop (type, expr));
6811 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6812 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6813 return fold_if_not_in_template (build_nop (type, expr));
6814 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6815 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6817 tree sexpr = expr;
6819 if (!c_cast_p
6820 && check_for_casting_away_constness (intype, type,
6821 REINTERPRET_CAST_EXPR,
6822 complain))
6823 return error_mark_node;
6824 /* Warn about possible alignment problems. */
6825 if (STRICT_ALIGNMENT && warn_cast_align
6826 && (complain & tf_warning)
6827 && !VOID_TYPE_P (type)
6828 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6829 && COMPLETE_TYPE_P (TREE_TYPE (type))
6830 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6831 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6832 warning (OPT_Wcast_align, "cast from %qT to %qT "
6833 "increases required alignment of target type", intype, type);
6835 /* We need to strip nops here, because the front end likes to
6836 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6837 STRIP_NOPS (sexpr);
6838 if (warn_strict_aliasing <= 2)
6839 strict_aliasing_warning (intype, type, sexpr);
6841 return fold_if_not_in_template (build_nop (type, expr));
6843 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6844 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6846 if (complain & tf_warning)
6847 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6848 object pointer type or vice versa is conditionally-supported." */
6849 warning (OPT_Wconditionally_supported,
6850 "casting between pointer-to-function and pointer-to-object "
6851 "is conditionally-supported");
6852 return fold_if_not_in_template (build_nop (type, expr));
6854 else if (TREE_CODE (type) == VECTOR_TYPE)
6855 return fold_if_not_in_template (convert_to_vector (type, expr));
6856 else if (TREE_CODE (intype) == VECTOR_TYPE
6857 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6858 return fold_if_not_in_template (convert_to_integer (type, expr));
6859 else
6861 if (valid_p)
6862 *valid_p = false;
6863 if (complain & tf_error)
6864 error ("invalid cast from type %qT to type %qT", intype, type);
6865 return error_mark_node;
6868 return cp_convert (type, expr, complain);
6871 tree
6872 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6874 tree r;
6876 if (type == error_mark_node || expr == error_mark_node)
6877 return error_mark_node;
6879 if (processing_template_decl)
6881 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6883 if (!TREE_SIDE_EFFECTS (t)
6884 && type_dependent_expression_p (expr))
6885 /* There might turn out to be side effects inside expr. */
6886 TREE_SIDE_EFFECTS (t) = 1;
6887 return convert_from_reference (t);
6890 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6891 /*valid_p=*/NULL, complain);
6892 if (r != error_mark_node)
6893 maybe_warn_about_useless_cast (type, expr, complain);
6894 return r;
6897 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6898 return an appropriate expression. Otherwise, return
6899 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6900 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6901 performing a C-style cast, its value upon return will indicate
6902 whether or not the conversion succeeded. */
6904 static tree
6905 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6906 bool *valid_p)
6908 tree src_type;
6909 tree reference_type;
6911 /* Callers are responsible for handling error_mark_node as a
6912 destination type. */
6913 gcc_assert (dst_type != error_mark_node);
6914 /* In a template, callers should be building syntactic
6915 representations of casts, not using this machinery. */
6916 gcc_assert (!processing_template_decl);
6918 /* Assume the conversion is invalid. */
6919 if (valid_p)
6920 *valid_p = false;
6922 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6924 if (complain & tf_error)
6925 error ("invalid use of const_cast with type %qT, "
6926 "which is not a pointer, "
6927 "reference, nor a pointer-to-data-member type", dst_type);
6928 return error_mark_node;
6931 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6933 if (complain & tf_error)
6934 error ("invalid use of const_cast with type %qT, which is a pointer "
6935 "or reference to a function type", dst_type);
6936 return error_mark_node;
6939 /* Save casted types in the function's used types hash table. */
6940 used_types_insert (dst_type);
6942 src_type = TREE_TYPE (expr);
6943 /* Expressions do not really have reference types. */
6944 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6945 src_type = TREE_TYPE (src_type);
6947 /* [expr.const.cast]
6949 For two object types T1 and T2, if a pointer to T1 can be explicitly
6950 converted to the type "pointer to T2" using a const_cast, then the
6951 following conversions can also be made:
6953 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6954 type T2 using the cast const_cast<T2&>;
6956 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6957 type T2 using the cast const_cast<T2&&>; and
6959 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6960 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6962 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6964 reference_type = dst_type;
6965 if (!TYPE_REF_IS_RVALUE (dst_type)
6966 ? real_lvalue_p (expr)
6967 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6968 ? lvalue_p (expr)
6969 : lvalue_or_rvalue_with_address_p (expr)))
6970 /* OK. */;
6971 else
6973 if (complain & tf_error)
6974 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6975 src_type, dst_type);
6976 return error_mark_node;
6978 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6979 src_type = build_pointer_type (src_type);
6981 else
6983 reference_type = NULL_TREE;
6984 /* If the destination type is not a reference type, the
6985 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6986 conversions are performed. */
6987 src_type = type_decays_to (src_type);
6988 if (src_type == error_mark_node)
6989 return error_mark_node;
6992 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
6994 if (comp_ptr_ttypes_const (dst_type, src_type))
6996 if (valid_p)
6998 *valid_p = true;
6999 /* This cast is actually a C-style cast. Issue a warning if
7000 the user is making a potentially unsafe cast. */
7001 check_for_casting_away_constness (src_type, dst_type,
7002 CAST_EXPR, complain);
7004 if (reference_type)
7006 expr = cp_build_addr_expr (expr, complain);
7007 if (expr == error_mark_node)
7008 return error_mark_node;
7009 expr = build_nop (reference_type, expr);
7010 return convert_from_reference (expr);
7012 else
7014 expr = decay_conversion (expr, complain);
7015 if (expr == error_mark_node)
7016 return error_mark_node;
7018 /* build_c_cast puts on a NOP_EXPR to make the result not an
7019 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7020 non-lvalue context. */
7021 if (TREE_CODE (expr) == NOP_EXPR
7022 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7023 expr = TREE_OPERAND (expr, 0);
7024 return build_nop (dst_type, expr);
7027 else if (valid_p
7028 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7029 TREE_TYPE (src_type)))
7030 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7031 complain);
7034 if (complain & tf_error)
7035 error ("invalid const_cast from type %qT to type %qT",
7036 src_type, dst_type);
7037 return error_mark_node;
7040 tree
7041 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7043 tree r;
7045 if (type == error_mark_node || error_operand_p (expr))
7046 return error_mark_node;
7048 if (processing_template_decl)
7050 tree t = build_min (CONST_CAST_EXPR, type, expr);
7052 if (!TREE_SIDE_EFFECTS (t)
7053 && type_dependent_expression_p (expr))
7054 /* There might turn out to be side effects inside expr. */
7055 TREE_SIDE_EFFECTS (t) = 1;
7056 return convert_from_reference (t);
7059 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7060 if (r != error_mark_node)
7061 maybe_warn_about_useless_cast (type, expr, complain);
7062 return r;
7065 /* Like cp_build_c_cast, but for the c-common bits. */
7067 tree
7068 build_c_cast (location_t /*loc*/, tree type, tree expr)
7070 return cp_build_c_cast (type, expr, tf_warning_or_error);
7073 /* Build an expression representing an explicit C-style cast to type
7074 TYPE of expression EXPR. */
7076 tree
7077 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7079 tree value = expr;
7080 tree result;
7081 bool valid_p;
7083 if (type == error_mark_node || error_operand_p (expr))
7084 return error_mark_node;
7086 if (processing_template_decl)
7088 tree t = build_min (CAST_EXPR, type,
7089 tree_cons (NULL_TREE, value, NULL_TREE));
7090 /* We don't know if it will or will not have side effects. */
7091 TREE_SIDE_EFFECTS (t) = 1;
7092 return convert_from_reference (t);
7095 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7096 'Class') should always be retained, because this information aids
7097 in method lookup. */
7098 if (objc_is_object_ptr (type)
7099 && objc_is_object_ptr (TREE_TYPE (expr)))
7100 return build_nop (type, expr);
7102 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7103 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7104 if (TREE_CODE (type) != REFERENCE_TYPE
7105 && TREE_CODE (value) == NOP_EXPR
7106 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7107 value = TREE_OPERAND (value, 0);
7109 if (TREE_CODE (type) == ARRAY_TYPE)
7111 /* Allow casting from T1* to T2[] because Cfront allows it.
7112 NIHCL uses it. It is not valid ISO C++ however. */
7113 if (TYPE_PTR_P (TREE_TYPE (expr)))
7115 if (complain & tf_error)
7116 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7117 else
7118 return error_mark_node;
7119 type = build_pointer_type (TREE_TYPE (type));
7121 else
7123 if (complain & tf_error)
7124 error ("ISO C++ forbids casting to an array type %qT", type);
7125 return error_mark_node;
7129 if (TREE_CODE (type) == FUNCTION_TYPE
7130 || TREE_CODE (type) == METHOD_TYPE)
7132 if (complain & tf_error)
7133 error ("invalid cast to function type %qT", type);
7134 return error_mark_node;
7137 if (TYPE_PTR_P (type)
7138 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7139 /* Casting to an integer of smaller size is an error detected elsewhere. */
7140 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7141 /* Don't warn about converting any constant. */
7142 && !TREE_CONSTANT (value))
7143 warning_at (input_location, OPT_Wint_to_pointer_cast,
7144 "cast to pointer from integer of different size");
7146 /* A C-style cast can be a const_cast. */
7147 result = build_const_cast_1 (type, value, complain & tf_warning,
7148 &valid_p);
7149 if (valid_p)
7151 if (result != error_mark_node)
7152 maybe_warn_about_useless_cast (type, value, complain);
7153 return result;
7156 /* Or a static cast. */
7157 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7158 &valid_p, complain);
7159 /* Or a reinterpret_cast. */
7160 if (!valid_p)
7161 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7162 &valid_p, complain);
7163 /* The static_cast or reinterpret_cast may be followed by a
7164 const_cast. */
7165 if (valid_p
7166 /* A valid cast may result in errors if, for example, a
7167 conversion to an ambiguous base class is required. */
7168 && !error_operand_p (result))
7170 tree result_type;
7172 maybe_warn_about_useless_cast (type, value, complain);
7174 /* Non-class rvalues always have cv-unqualified type. */
7175 if (!CLASS_TYPE_P (type))
7176 type = TYPE_MAIN_VARIANT (type);
7177 result_type = TREE_TYPE (result);
7178 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7179 result_type = TYPE_MAIN_VARIANT (result_type);
7180 /* If the type of RESULT does not match TYPE, perform a
7181 const_cast to make it match. If the static_cast or
7182 reinterpret_cast succeeded, we will differ by at most
7183 cv-qualification, so the follow-on const_cast is guaranteed
7184 to succeed. */
7185 if (!same_type_p (non_reference (type), non_reference (result_type)))
7187 result = build_const_cast_1 (type, result, false, &valid_p);
7188 gcc_assert (valid_p);
7190 return result;
7193 return error_mark_node;
7196 /* For use from the C common bits. */
7197 tree
7198 build_modify_expr (location_t /*location*/,
7199 tree lhs, tree /*lhs_origtype*/,
7200 enum tree_code modifycode,
7201 location_t /*rhs_location*/, tree rhs,
7202 tree /*rhs_origtype*/)
7204 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7207 /* Build an assignment expression of lvalue LHS from value RHS.
7208 MODIFYCODE is the code for a binary operator that we use
7209 to combine the old value of LHS with RHS to get the new value.
7210 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7212 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7214 tree
7215 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7216 tsubst_flags_t complain)
7218 tree result;
7219 tree newrhs = rhs;
7220 tree lhstype = TREE_TYPE (lhs);
7221 tree olhstype = lhstype;
7222 bool plain_assign = (modifycode == NOP_EXPR);
7224 /* Avoid duplicate error messages from operands that had errors. */
7225 if (error_operand_p (lhs) || error_operand_p (rhs))
7226 return error_mark_node;
7228 /* Handle control structure constructs used as "lvalues". */
7229 switch (TREE_CODE (lhs))
7231 /* Handle --foo = 5; as these are valid constructs in C++. */
7232 case PREDECREMENT_EXPR:
7233 case PREINCREMENT_EXPR:
7234 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7235 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7236 stabilize_reference (TREE_OPERAND (lhs, 0)),
7237 TREE_OPERAND (lhs, 1));
7238 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7239 modifycode, rhs, complain);
7240 if (newrhs == error_mark_node)
7241 return error_mark_node;
7242 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7244 /* Handle (a, b) used as an "lvalue". */
7245 case COMPOUND_EXPR:
7246 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7247 modifycode, rhs, complain);
7248 if (newrhs == error_mark_node)
7249 return error_mark_node;
7250 return build2 (COMPOUND_EXPR, lhstype,
7251 TREE_OPERAND (lhs, 0), newrhs);
7253 case MODIFY_EXPR:
7254 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7255 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7256 stabilize_reference (TREE_OPERAND (lhs, 0)),
7257 TREE_OPERAND (lhs, 1));
7258 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7259 complain);
7260 if (newrhs == error_mark_node)
7261 return error_mark_node;
7262 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7264 case MIN_EXPR:
7265 case MAX_EXPR:
7266 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7267 when neither operand has side-effects. */
7268 if (!lvalue_or_else (lhs, lv_assign, complain))
7269 return error_mark_node;
7271 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7272 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7274 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7275 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7276 boolean_type_node,
7277 TREE_OPERAND (lhs, 0),
7278 TREE_OPERAND (lhs, 1)),
7279 TREE_OPERAND (lhs, 0),
7280 TREE_OPERAND (lhs, 1));
7281 /* Fall through. */
7283 /* Handle (a ? b : c) used as an "lvalue". */
7284 case COND_EXPR:
7286 /* Produce (a ? (b = rhs) : (c = rhs))
7287 except that the RHS goes through a save-expr
7288 so the code to compute it is only emitted once. */
7289 tree cond;
7290 tree preeval = NULL_TREE;
7292 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7294 if (complain & tf_error)
7295 error ("void value not ignored as it ought to be");
7296 return error_mark_node;
7299 rhs = stabilize_expr (rhs, &preeval);
7301 /* Check this here to avoid odd errors when trying to convert
7302 a throw to the type of the COND_EXPR. */
7303 if (!lvalue_or_else (lhs, lv_assign, complain))
7304 return error_mark_node;
7306 cond = build_conditional_expr
7307 (input_location, TREE_OPERAND (lhs, 0),
7308 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7309 modifycode, rhs, complain),
7310 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7311 modifycode, rhs, complain),
7312 complain);
7314 if (cond == error_mark_node)
7315 return cond;
7316 /* Make sure the code to compute the rhs comes out
7317 before the split. */
7318 if (preeval)
7319 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7320 return cond;
7323 default:
7324 break;
7327 if (modifycode == INIT_EXPR)
7329 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7330 /* Do the default thing. */;
7331 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7333 /* Compound literal. */
7334 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7335 /* Call convert to generate an error; see PR 11063. */
7336 rhs = convert (lhstype, rhs);
7337 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7338 TREE_SIDE_EFFECTS (result) = 1;
7339 return result;
7341 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7342 /* Do the default thing. */;
7343 else
7345 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7346 result = build_special_member_call (lhs, complete_ctor_identifier,
7347 &rhs_vec, lhstype, LOOKUP_NORMAL,
7348 complain);
7349 release_tree_vector (rhs_vec);
7350 if (result == NULL_TREE)
7351 return error_mark_node;
7352 return result;
7355 else
7357 lhs = require_complete_type_sfinae (lhs, complain);
7358 if (lhs == error_mark_node)
7359 return error_mark_node;
7361 if (modifycode == NOP_EXPR)
7363 if (c_dialect_objc ())
7365 result = objc_maybe_build_modify_expr (lhs, rhs);
7366 if (result)
7367 return result;
7370 /* `operator=' is not an inheritable operator. */
7371 if (! MAYBE_CLASS_TYPE_P (lhstype))
7372 /* Do the default thing. */;
7373 else
7375 result = build_new_op (input_location, MODIFY_EXPR,
7376 LOOKUP_NORMAL, lhs, rhs,
7377 make_node (NOP_EXPR), /*overload=*/NULL,
7378 complain);
7379 if (result == NULL_TREE)
7380 return error_mark_node;
7381 return result;
7383 lhstype = olhstype;
7385 else
7387 tree init = NULL_TREE;
7389 /* A binary op has been requested. Combine the old LHS
7390 value with the RHS producing the value we should actually
7391 store into the LHS. */
7392 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7393 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7394 || MAYBE_CLASS_TYPE_P (lhstype)));
7396 /* Preevaluate the RHS to make sure its evaluation is complete
7397 before the lvalue-to-rvalue conversion of the LHS:
7399 [expr.ass] With respect to an indeterminately-sequenced
7400 function call, the operation of a compound assignment is a
7401 single evaluation. [ Note: Therefore, a function call shall
7402 not intervene between the lvalue-to-rvalue conversion and the
7403 side effect associated with any single compound assignment
7404 operator. -- end note ] */
7405 lhs = stabilize_reference (lhs);
7406 rhs = rvalue (rhs);
7407 rhs = stabilize_expr (rhs, &init);
7408 newrhs = cp_build_binary_op (input_location,
7409 modifycode, lhs, rhs,
7410 complain);
7411 if (newrhs == error_mark_node)
7413 if (complain & tf_error)
7414 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7415 TREE_TYPE (lhs), TREE_TYPE (rhs));
7416 return error_mark_node;
7419 if (init)
7420 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7422 /* Now it looks like a plain assignment. */
7423 modifycode = NOP_EXPR;
7424 if (c_dialect_objc ())
7426 result = objc_maybe_build_modify_expr (lhs, newrhs);
7427 if (result)
7428 return result;
7431 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7432 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7435 /* The left-hand side must be an lvalue. */
7436 if (!lvalue_or_else (lhs, lv_assign, complain))
7437 return error_mark_node;
7439 /* Warn about modifying something that is `const'. Don't warn if
7440 this is initialization. */
7441 if (modifycode != INIT_EXPR
7442 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7443 /* Functions are not modifiable, even though they are
7444 lvalues. */
7445 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7446 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7447 /* If it's an aggregate and any field is const, then it is
7448 effectively const. */
7449 || (CLASS_TYPE_P (lhstype)
7450 && C_TYPE_FIELDS_READONLY (lhstype))))
7452 if (complain & tf_error)
7453 cxx_readonly_error (lhs, lv_assign);
7454 else
7455 return error_mark_node;
7458 /* If storing into a structure or union member, it may have been given a
7459 lowered bitfield type. We need to convert to the declared type first,
7460 so retrieve it now. */
7462 olhstype = unlowered_expr_type (lhs);
7464 /* Convert new value to destination type. */
7466 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7468 int from_array;
7470 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7472 if (modifycode != INIT_EXPR)
7474 if (complain & tf_error)
7475 error ("assigning to an array from an initializer list");
7476 return error_mark_node;
7478 if (check_array_initializer (lhs, lhstype, newrhs))
7479 return error_mark_node;
7480 newrhs = digest_init (lhstype, newrhs, complain);
7481 if (newrhs == error_mark_node)
7482 return error_mark_node;
7485 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7486 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7488 if (complain & tf_error)
7489 error ("incompatible types in assignment of %qT to %qT",
7490 TREE_TYPE (rhs), lhstype);
7491 return error_mark_node;
7494 /* Allow array assignment in compiler-generated code. */
7495 else if (!current_function_decl
7496 || !DECL_DEFAULTED_FN (current_function_decl))
7498 /* This routine is used for both initialization and assignment.
7499 Make sure the diagnostic message differentiates the context. */
7500 if (complain & tf_error)
7502 if (modifycode == INIT_EXPR)
7503 error ("array used as initializer");
7504 else
7505 error ("invalid array assignment");
7507 return error_mark_node;
7510 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7511 ? 1 + (modifycode != INIT_EXPR): 0;
7512 return build_vec_init (lhs, NULL_TREE, newrhs,
7513 /*explicit_value_init_p=*/false,
7514 from_array, complain);
7517 if (modifycode == INIT_EXPR)
7518 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7519 LOOKUP_ONLYCONVERTING. */
7520 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7521 ICR_INIT, NULL_TREE, 0,
7522 complain);
7523 else
7524 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7525 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7527 if (!same_type_p (lhstype, olhstype))
7528 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7530 if (modifycode != INIT_EXPR)
7532 if (TREE_CODE (newrhs) == CALL_EXPR
7533 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7534 newrhs = build_cplus_new (lhstype, newrhs, complain);
7536 /* Can't initialize directly from a TARGET_EXPR, since that would
7537 cause the lhs to be constructed twice, and possibly result in
7538 accidental self-initialization. So we force the TARGET_EXPR to be
7539 expanded without a target. */
7540 if (TREE_CODE (newrhs) == TARGET_EXPR)
7541 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7542 TREE_OPERAND (newrhs, 0));
7545 if (newrhs == error_mark_node)
7546 return error_mark_node;
7548 if (c_dialect_objc () && flag_objc_gc)
7550 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7552 if (result)
7553 return result;
7556 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7557 lhstype, lhs, newrhs);
7559 TREE_SIDE_EFFECTS (result) = 1;
7560 if (!plain_assign)
7561 TREE_NO_WARNING (result) = 1;
7563 return result;
7566 tree
7567 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7568 tree rhs, tsubst_flags_t complain)
7570 if (processing_template_decl)
7571 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7572 build_min_nt_loc (loc, modifycode, NULL_TREE,
7573 NULL_TREE), rhs);
7575 if (modifycode != NOP_EXPR)
7577 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7578 make_node (modifycode), /*overload=*/NULL,
7579 complain);
7580 if (rval)
7582 TREE_NO_WARNING (rval) = 1;
7583 return rval;
7586 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7589 /* Helper function for get_delta_difference which assumes FROM is a base
7590 class of TO. Returns a delta for the conversion of pointer-to-member
7591 of FROM to pointer-to-member of TO. If the conversion is invalid and
7592 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7593 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7594 If C_CAST_P is true, this conversion is taking place as part of a
7595 C-style cast. */
7597 static tree
7598 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7599 tsubst_flags_t complain)
7601 tree binfo;
7602 base_kind kind;
7604 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7605 &kind, complain);
7607 if (binfo == error_mark_node)
7609 if (!(complain & tf_error))
7610 return error_mark_node;
7612 error (" in pointer to member function conversion");
7613 return size_zero_node;
7615 else if (binfo)
7617 if (kind != bk_via_virtual)
7618 return BINFO_OFFSET (binfo);
7619 else
7620 /* FROM is a virtual base class of TO. Issue an error or warning
7621 depending on whether or not this is a reinterpret cast. */
7623 if (!(complain & tf_error))
7624 return error_mark_node;
7626 error ("pointer to member conversion via virtual base %qT",
7627 BINFO_TYPE (binfo_from_vbase (binfo)));
7629 return size_zero_node;
7632 else
7633 return NULL_TREE;
7636 /* Get difference in deltas for different pointer to member function
7637 types. If the conversion is invalid and tf_error is not set in
7638 COMPLAIN, returns error_mark_node, otherwise returns an integer
7639 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7640 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7641 conversions as well. If C_CAST_P is true this conversion is taking
7642 place as part of a C-style cast.
7644 Note that the naming of FROM and TO is kind of backwards; the return
7645 value is what we add to a TO in order to get a FROM. They are named
7646 this way because we call this function to find out how to convert from
7647 a pointer to member of FROM to a pointer to member of TO. */
7649 static tree
7650 get_delta_difference (tree from, tree to,
7651 bool allow_inverse_p,
7652 bool c_cast_p, tsubst_flags_t complain)
7654 tree result;
7656 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7657 /* Pointer to member of incomplete class is permitted*/
7658 result = size_zero_node;
7659 else
7660 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7662 if (result == error_mark_node)
7663 return error_mark_node;
7665 if (!result)
7667 if (!allow_inverse_p)
7669 if (!(complain & tf_error))
7670 return error_mark_node;
7672 error_not_base_type (from, to);
7673 error (" in pointer to member conversion");
7674 result = size_zero_node;
7676 else
7678 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7680 if (result == error_mark_node)
7681 return error_mark_node;
7683 if (result)
7684 result = size_diffop_loc (input_location,
7685 size_zero_node, result);
7686 else
7688 if (!(complain & tf_error))
7689 return error_mark_node;
7691 error_not_base_type (from, to);
7692 error (" in pointer to member conversion");
7693 result = size_zero_node;
7698 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7699 result));
7702 /* Return a constructor for the pointer-to-member-function TYPE using
7703 the other components as specified. */
7705 tree
7706 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7708 tree u = NULL_TREE;
7709 tree delta_field;
7710 tree pfn_field;
7711 vec<constructor_elt, va_gc> *v;
7713 /* Pull the FIELD_DECLs out of the type. */
7714 pfn_field = TYPE_FIELDS (type);
7715 delta_field = DECL_CHAIN (pfn_field);
7717 /* Make sure DELTA has the type we want. */
7718 delta = convert_and_check (input_location, delta_type_node, delta);
7720 /* Convert to the correct target type if necessary. */
7721 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7723 /* Finish creating the initializer. */
7724 vec_alloc (v, 2);
7725 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7726 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7727 u = build_constructor (type, v);
7728 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7729 TREE_STATIC (u) = (TREE_CONSTANT (u)
7730 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7731 != NULL_TREE)
7732 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7733 != NULL_TREE));
7734 return u;
7737 /* Build a constructor for a pointer to member function. It can be
7738 used to initialize global variables, local variable, or used
7739 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7740 want to be.
7742 If FORCE is nonzero, then force this conversion, even if
7743 we would rather not do it. Usually set when using an explicit
7744 cast. A C-style cast is being processed iff C_CAST_P is true.
7746 Return error_mark_node, if something goes wrong. */
7748 tree
7749 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7750 tsubst_flags_t complain)
7752 tree fn;
7753 tree pfn_type;
7754 tree to_type;
7756 if (error_operand_p (pfn))
7757 return error_mark_node;
7759 pfn_type = TREE_TYPE (pfn);
7760 to_type = build_ptrmemfunc_type (type);
7762 /* Handle multiple conversions of pointer to member functions. */
7763 if (TYPE_PTRMEMFUNC_P (pfn_type))
7765 tree delta = NULL_TREE;
7766 tree npfn = NULL_TREE;
7767 tree n;
7769 if (!force
7770 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7771 LOOKUP_NORMAL, complain))
7773 if (complain & tf_error)
7774 error ("invalid conversion to type %qT from type %qT",
7775 to_type, pfn_type);
7776 else
7777 return error_mark_node;
7780 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7781 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7782 force,
7783 c_cast_p, complain);
7784 if (n == error_mark_node)
7785 return error_mark_node;
7787 /* We don't have to do any conversion to convert a
7788 pointer-to-member to its own type. But, we don't want to
7789 just return a PTRMEM_CST if there's an explicit cast; that
7790 cast should make the expression an invalid template argument. */
7791 if (TREE_CODE (pfn) != PTRMEM_CST)
7793 if (same_type_p (to_type, pfn_type))
7794 return pfn;
7795 else if (integer_zerop (n))
7796 return build_reinterpret_cast (to_type, pfn,
7797 complain);
7800 if (TREE_SIDE_EFFECTS (pfn))
7801 pfn = save_expr (pfn);
7803 /* Obtain the function pointer and the current DELTA. */
7804 if (TREE_CODE (pfn) == PTRMEM_CST)
7805 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7806 else
7808 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7809 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7812 /* Just adjust the DELTA field. */
7813 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7814 (TREE_TYPE (delta), ptrdiff_type_node));
7815 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7816 n = cp_build_binary_op (input_location,
7817 LSHIFT_EXPR, n, integer_one_node,
7818 complain);
7819 delta = cp_build_binary_op (input_location,
7820 PLUS_EXPR, delta, n, complain);
7821 return build_ptrmemfunc1 (to_type, delta, npfn);
7824 /* Handle null pointer to member function conversions. */
7825 if (null_ptr_cst_p (pfn))
7827 pfn = cp_build_c_cast (type, pfn, complain);
7828 return build_ptrmemfunc1 (to_type,
7829 integer_zero_node,
7830 pfn);
7833 if (type_unknown_p (pfn))
7834 return instantiate_type (type, pfn, complain);
7836 fn = TREE_OPERAND (pfn, 0);
7837 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7838 /* In a template, we will have preserved the
7839 OFFSET_REF. */
7840 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7841 return make_ptrmem_cst (to_type, fn);
7844 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7845 given by CST.
7847 ??? There is no consistency as to the types returned for the above
7848 values. Some code acts as if it were a sizetype and some as if it were
7849 integer_type_node. */
7851 void
7852 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7854 tree type = TREE_TYPE (cst);
7855 tree fn = PTRMEM_CST_MEMBER (cst);
7856 tree ptr_class, fn_class;
7858 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7860 /* The class that the function belongs to. */
7861 fn_class = DECL_CONTEXT (fn);
7863 /* The class that we're creating a pointer to member of. */
7864 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7866 /* First, calculate the adjustment to the function's class. */
7867 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7868 /*c_cast_p=*/0, tf_warning_or_error);
7870 if (!DECL_VIRTUAL_P (fn))
7871 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7872 build_addr_func (fn, tf_warning_or_error));
7873 else
7875 /* If we're dealing with a virtual function, we have to adjust 'this'
7876 again, to point to the base which provides the vtable entry for
7877 fn; the call will do the opposite adjustment. */
7878 tree orig_class = DECL_CONTEXT (fn);
7879 tree binfo = binfo_or_else (orig_class, fn_class);
7880 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7881 *delta, BINFO_OFFSET (binfo));
7882 *delta = fold_if_not_in_template (*delta);
7884 /* We set PFN to the vtable offset at which the function can be
7885 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7886 case delta is shifted left, and then incremented). */
7887 *pfn = DECL_VINDEX (fn);
7888 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7889 TYPE_SIZE_UNIT (vtable_entry_type));
7890 *pfn = fold_if_not_in_template (*pfn);
7892 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7894 case ptrmemfunc_vbit_in_pfn:
7895 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7896 integer_one_node);
7897 *pfn = fold_if_not_in_template (*pfn);
7898 break;
7900 case ptrmemfunc_vbit_in_delta:
7901 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7902 *delta, integer_one_node);
7903 *delta = fold_if_not_in_template (*delta);
7904 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7905 *delta, integer_one_node);
7906 *delta = fold_if_not_in_template (*delta);
7907 break;
7909 default:
7910 gcc_unreachable ();
7913 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7914 *pfn = fold_if_not_in_template (*pfn);
7918 /* Return an expression for PFN from the pointer-to-member function
7919 given by T. */
7921 static tree
7922 pfn_from_ptrmemfunc (tree t)
7924 if (TREE_CODE (t) == PTRMEM_CST)
7926 tree delta;
7927 tree pfn;
7929 expand_ptrmemfunc_cst (t, &delta, &pfn);
7930 if (pfn)
7931 return pfn;
7934 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7937 /* Return an expression for DELTA from the pointer-to-member function
7938 given by T. */
7940 static tree
7941 delta_from_ptrmemfunc (tree t)
7943 if (TREE_CODE (t) == PTRMEM_CST)
7945 tree delta;
7946 tree pfn;
7948 expand_ptrmemfunc_cst (t, &delta, &pfn);
7949 if (delta)
7950 return delta;
7953 return build_ptrmemfunc_access_expr (t, delta_identifier);
7956 /* Convert value RHS to type TYPE as preparation for an assignment to
7957 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7958 implicit conversion is. If FNDECL is non-NULL, we are doing the
7959 conversion in order to pass the PARMNUMth argument of FNDECL.
7960 If FNDECL is NULL, we are doing the conversion in function pointer
7961 argument passing, conversion in initialization, etc. */
7963 static tree
7964 convert_for_assignment (tree type, tree rhs,
7965 impl_conv_rhs errtype, tree fndecl, int parmnum,
7966 tsubst_flags_t complain, int flags)
7968 tree rhstype;
7969 enum tree_code coder;
7971 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7972 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7973 rhs = TREE_OPERAND (rhs, 0);
7975 rhstype = TREE_TYPE (rhs);
7976 coder = TREE_CODE (rhstype);
7978 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7979 && vector_types_convertible_p (type, rhstype, true))
7981 rhs = mark_rvalue_use (rhs);
7982 return convert (type, rhs);
7985 if (rhs == error_mark_node || rhstype == error_mark_node)
7986 return error_mark_node;
7987 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7988 return error_mark_node;
7990 /* The RHS of an assignment cannot have void type. */
7991 if (coder == VOID_TYPE)
7993 if (complain & tf_error)
7994 error ("void value not ignored as it ought to be");
7995 return error_mark_node;
7998 if (c_dialect_objc ())
8000 int parmno;
8001 tree selector;
8002 tree rname = fndecl;
8004 switch (errtype)
8006 case ICR_ASSIGN:
8007 parmno = -1;
8008 break;
8009 case ICR_INIT:
8010 parmno = -2;
8011 break;
8012 default:
8013 selector = objc_message_selector ();
8014 parmno = parmnum;
8015 if (selector && parmno > 1)
8017 rname = selector;
8018 parmno -= 1;
8022 if (objc_compare_types (type, rhstype, parmno, rname))
8024 rhs = mark_rvalue_use (rhs);
8025 return convert (type, rhs);
8029 /* [expr.ass]
8031 The expression is implicitly converted (clause _conv_) to the
8032 cv-unqualified type of the left operand.
8034 We allow bad conversions here because by the time we get to this point
8035 we are committed to doing the conversion. If we end up doing a bad
8036 conversion, convert_like will complain. */
8037 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8039 /* When -Wno-pmf-conversions is use, we just silently allow
8040 conversions from pointers-to-members to plain pointers. If
8041 the conversion doesn't work, cp_convert will complain. */
8042 if (!warn_pmf2ptr
8043 && TYPE_PTR_P (type)
8044 && TYPE_PTRMEMFUNC_P (rhstype))
8045 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8046 else
8048 if (complain & tf_error)
8050 /* If the right-hand side has unknown type, then it is an
8051 overloaded function. Call instantiate_type to get error
8052 messages. */
8053 if (rhstype == unknown_type_node)
8054 instantiate_type (type, rhs, tf_warning_or_error);
8055 else if (fndecl)
8056 error ("cannot convert %qT to %qT for argument %qP to %qD",
8057 rhstype, type, parmnum, fndecl);
8058 else
8059 switch (errtype)
8061 case ICR_DEFAULT_ARGUMENT:
8062 error ("cannot convert %qT to %qT in default argument",
8063 rhstype, type);
8064 break;
8065 case ICR_ARGPASS:
8066 error ("cannot convert %qT to %qT in argument passing",
8067 rhstype, type);
8068 break;
8069 case ICR_CONVERTING:
8070 error ("cannot convert %qT to %qT",
8071 rhstype, type);
8072 break;
8073 case ICR_INIT:
8074 error ("cannot convert %qT to %qT in initialization",
8075 rhstype, type);
8076 break;
8077 case ICR_RETURN:
8078 error ("cannot convert %qT to %qT in return",
8079 rhstype, type);
8080 break;
8081 case ICR_ASSIGN:
8082 error ("cannot convert %qT to %qT in assignment",
8083 rhstype, type);
8084 break;
8085 default:
8086 gcc_unreachable();
8089 return error_mark_node;
8092 if (warn_suggest_attribute_format)
8094 const enum tree_code codel = TREE_CODE (type);
8095 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8096 && coder == codel
8097 && check_missing_format_attribute (type, rhstype)
8098 && (complain & tf_warning))
8099 switch (errtype)
8101 case ICR_ARGPASS:
8102 case ICR_DEFAULT_ARGUMENT:
8103 if (fndecl)
8104 warning (OPT_Wsuggest_attribute_format,
8105 "parameter %qP of %qD might be a candidate "
8106 "for a format attribute", parmnum, fndecl);
8107 else
8108 warning (OPT_Wsuggest_attribute_format,
8109 "parameter might be a candidate "
8110 "for a format attribute");
8111 break;
8112 case ICR_CONVERTING:
8113 warning (OPT_Wsuggest_attribute_format,
8114 "target of conversion might be a candidate "
8115 "for a format attribute");
8116 break;
8117 case ICR_INIT:
8118 warning (OPT_Wsuggest_attribute_format,
8119 "target of initialization might be a candidate "
8120 "for a format attribute");
8121 break;
8122 case ICR_RETURN:
8123 warning (OPT_Wsuggest_attribute_format,
8124 "return type might be a candidate "
8125 "for a format attribute");
8126 break;
8127 case ICR_ASSIGN:
8128 warning (OPT_Wsuggest_attribute_format,
8129 "left-hand side of assignment might be a candidate "
8130 "for a format attribute");
8131 break;
8132 default:
8133 gcc_unreachable();
8137 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8138 does not. */
8139 if (warn_parentheses
8140 && TREE_CODE (type) == BOOLEAN_TYPE
8141 && TREE_CODE (rhs) == MODIFY_EXPR
8142 && !TREE_NO_WARNING (rhs)
8143 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8144 && (complain & tf_warning))
8146 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8148 warning_at (loc, OPT_Wparentheses,
8149 "suggest parentheses around assignment used as truth value");
8150 TREE_NO_WARNING (rhs) = 1;
8153 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8154 complain, flags);
8157 /* Convert RHS to be of type TYPE.
8158 If EXP is nonzero, it is the target of the initialization.
8159 ERRTYPE indicates what kind of error the implicit conversion is.
8161 Two major differences between the behavior of
8162 `convert_for_assignment' and `convert_for_initialization'
8163 are that references are bashed in the former, while
8164 copied in the latter, and aggregates are assigned in
8165 the former (operator=) while initialized in the
8166 latter (X(X&)).
8168 If using constructor make sure no conversion operator exists, if one does
8169 exist, an ambiguity exists. */
8171 tree
8172 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8173 impl_conv_rhs errtype, tree fndecl, int parmnum,
8174 tsubst_flags_t complain)
8176 enum tree_code codel = TREE_CODE (type);
8177 tree rhstype;
8178 enum tree_code coder;
8180 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8181 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8182 if (TREE_CODE (rhs) == NOP_EXPR
8183 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8184 && codel != REFERENCE_TYPE)
8185 rhs = TREE_OPERAND (rhs, 0);
8187 if (type == error_mark_node
8188 || rhs == error_mark_node
8189 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8190 return error_mark_node;
8192 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8193 && TREE_CODE (type) != ARRAY_TYPE
8194 && (TREE_CODE (type) != REFERENCE_TYPE
8195 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8196 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8197 && !TYPE_REFFN_P (type))
8198 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8199 rhs = decay_conversion (rhs, complain);
8201 rhstype = TREE_TYPE (rhs);
8202 coder = TREE_CODE (rhstype);
8204 if (coder == ERROR_MARK)
8205 return error_mark_node;
8207 /* We accept references to incomplete types, so we can
8208 return here before checking if RHS is of complete type. */
8210 if (codel == REFERENCE_TYPE)
8212 /* This should eventually happen in convert_arguments. */
8213 int savew = 0, savee = 0;
8215 if (fndecl)
8216 savew = warningcount + werrorcount, savee = errorcount;
8217 rhs = initialize_reference (type, rhs, flags, complain);
8219 if (fndecl
8220 && (warningcount + werrorcount > savew || errorcount > savee))
8221 inform (input_location,
8222 "in passing argument %P of %q+D", parmnum, fndecl);
8224 return rhs;
8227 if (exp != 0)
8228 exp = require_complete_type_sfinae (exp, complain);
8229 if (exp == error_mark_node)
8230 return error_mark_node;
8232 rhstype = non_reference (rhstype);
8234 type = complete_type (type);
8236 if (DIRECT_INIT_EXPR_P (type, rhs))
8237 /* Don't try to do copy-initialization if we already have
8238 direct-initialization. */
8239 return rhs;
8241 if (MAYBE_CLASS_TYPE_P (type))
8242 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8244 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8245 complain, flags);
8248 /* If RETVAL is the address of, or a reference to, a local variable or
8249 temporary give an appropriate warning. */
8251 static void
8252 maybe_warn_about_returning_address_of_local (tree retval)
8254 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8255 tree whats_returned = retval;
8257 for (;;)
8259 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8260 whats_returned = TREE_OPERAND (whats_returned, 1);
8261 else if (CONVERT_EXPR_P (whats_returned)
8262 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8263 whats_returned = TREE_OPERAND (whats_returned, 0);
8264 else
8265 break;
8268 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8269 return;
8270 whats_returned = TREE_OPERAND (whats_returned, 0);
8272 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8274 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8275 || TREE_CODE (whats_returned) == TARGET_EXPR)
8277 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8278 return;
8280 if (VAR_P (whats_returned)
8281 && DECL_NAME (whats_returned)
8282 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8284 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8285 return;
8289 while (TREE_CODE (whats_returned) == COMPONENT_REF
8290 || TREE_CODE (whats_returned) == ARRAY_REF)
8291 whats_returned = TREE_OPERAND (whats_returned, 0);
8293 if (DECL_P (whats_returned)
8294 && DECL_NAME (whats_returned)
8295 && DECL_FUNCTION_SCOPE_P (whats_returned)
8296 && !is_capture_proxy (whats_returned)
8297 && !(TREE_STATIC (whats_returned)
8298 || TREE_PUBLIC (whats_returned)))
8300 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8301 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8302 whats_returned);
8303 else
8304 warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
8305 whats_returned);
8306 return;
8310 /* Check that returning RETVAL from the current function is valid.
8311 Return an expression explicitly showing all conversions required to
8312 change RETVAL into the function return type, and to assign it to
8313 the DECL_RESULT for the function. Set *NO_WARNING to true if
8314 code reaches end of non-void function warning shouldn't be issued
8315 on this RETURN_EXPR. */
8317 tree
8318 check_return_expr (tree retval, bool *no_warning)
8320 tree result;
8321 /* The type actually returned by the function. */
8322 tree valtype;
8323 /* The type the function is declared to return, or void if
8324 the declared type is incomplete. */
8325 tree functype;
8326 int fn_returns_value_p;
8327 bool named_return_value_okay_p;
8329 *no_warning = false;
8331 if (flag_cilkplus && retval && TREE_CODE (retval) == CILK_SPAWN_STMT)
8333 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8334 "statement is not allowed");
8335 return NULL_TREE;
8338 /* A `volatile' function is one that isn't supposed to return, ever.
8339 (This is a G++ extension, used to get better code for functions
8340 that call the `volatile' function.) */
8341 if (TREE_THIS_VOLATILE (current_function_decl))
8342 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8344 /* Check for various simple errors. */
8345 if (DECL_DESTRUCTOR_P (current_function_decl))
8347 if (retval)
8348 error ("returning a value from a destructor");
8349 return NULL_TREE;
8351 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8353 if (in_function_try_handler)
8354 /* If a return statement appears in a handler of the
8355 function-try-block of a constructor, the program is ill-formed. */
8356 error ("cannot return from a handler of a function-try-block of a constructor");
8357 else if (retval)
8358 /* You can't return a value from a constructor. */
8359 error ("returning a value from a constructor");
8360 return NULL_TREE;
8363 if (processing_template_decl)
8365 current_function_returns_value = 1;
8366 if (check_for_bare_parameter_packs (retval))
8367 retval = error_mark_node;
8368 return retval;
8371 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8373 /* Deduce auto return type from a return statement. */
8374 if (current_function_auto_return_pattern)
8376 tree auto_node;
8377 tree type;
8379 if (!retval && !is_auto (current_function_auto_return_pattern))
8381 /* Give a helpful error message. */
8382 error ("return-statement with no value, in function returning %qT",
8383 current_function_auto_return_pattern);
8384 inform (input_location, "only plain %<auto%> return type can be "
8385 "deduced to %<void%>");
8386 type = error_mark_node;
8388 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8390 error ("returning initializer list");
8391 type = error_mark_node;
8393 else
8395 if (!retval)
8396 retval = void_zero_node;
8397 auto_node = type_uses_auto (current_function_auto_return_pattern);
8398 type = do_auto_deduction (current_function_auto_return_pattern,
8399 retval, auto_node);
8402 if (type == error_mark_node)
8403 /* Leave it. */;
8404 else if (functype == current_function_auto_return_pattern)
8405 apply_deduced_return_type (current_function_decl, type);
8406 else
8407 /* A mismatch should have been diagnosed in do_auto_deduction. */
8408 gcc_assert (same_type_p (type, functype));
8409 functype = type;
8412 /* When no explicit return-value is given in a function with a named
8413 return value, the named return value is used. */
8414 result = DECL_RESULT (current_function_decl);
8415 valtype = TREE_TYPE (result);
8416 gcc_assert (valtype != NULL_TREE);
8417 fn_returns_value_p = !VOID_TYPE_P (valtype);
8418 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8419 retval = result;
8421 /* Check for a return statement with no return value in a function
8422 that's supposed to return a value. */
8423 if (!retval && fn_returns_value_p)
8425 if (functype != error_mark_node)
8426 permerror (input_location, "return-statement with no value, in "
8427 "function returning %qT", valtype);
8428 /* Remember that this function did return. */
8429 current_function_returns_value = 1;
8430 /* And signal caller that TREE_NO_WARNING should be set on the
8431 RETURN_EXPR to avoid control reaches end of non-void function
8432 warnings in tree-cfg.c. */
8433 *no_warning = true;
8435 /* Check for a return statement with a value in a function that
8436 isn't supposed to return a value. */
8437 else if (retval && !fn_returns_value_p)
8439 if (VOID_TYPE_P (TREE_TYPE (retval)))
8440 /* You can return a `void' value from a function of `void'
8441 type. In that case, we have to evaluate the expression for
8442 its side-effects. */
8443 finish_expr_stmt (retval);
8444 else
8445 permerror (input_location, "return-statement with a value, in function "
8446 "returning 'void'");
8447 current_function_returns_null = 1;
8449 /* There's really no value to return, after all. */
8450 return NULL_TREE;
8452 else if (!retval)
8453 /* Remember that this function can sometimes return without a
8454 value. */
8455 current_function_returns_null = 1;
8456 else
8457 /* Remember that this function did return a value. */
8458 current_function_returns_value = 1;
8460 /* Check for erroneous operands -- but after giving ourselves a
8461 chance to provide an error about returning a value from a void
8462 function. */
8463 if (error_operand_p (retval))
8465 current_function_return_value = error_mark_node;
8466 return error_mark_node;
8469 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8470 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8471 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8472 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8473 && ! flag_check_new
8474 && retval && null_ptr_cst_p (retval))
8475 warning (0, "%<operator new%> must not return NULL unless it is "
8476 "declared %<throw()%> (or -fcheck-new is in effect)");
8478 /* Effective C++ rule 15. See also start_function. */
8479 if (warn_ecpp
8480 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8482 bool warn = true;
8484 /* The function return type must be a reference to the current
8485 class. */
8486 if (TREE_CODE (valtype) == REFERENCE_TYPE
8487 && same_type_ignoring_top_level_qualifiers_p
8488 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8490 /* Returning '*this' is obviously OK. */
8491 if (retval == current_class_ref)
8492 warn = false;
8493 /* If we are calling a function whose return type is the same of
8494 the current class reference, it is ok. */
8495 else if (INDIRECT_REF_P (retval)
8496 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8497 warn = false;
8500 if (warn)
8501 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8504 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8506 [...] For a function with a class return type, if the expression
8507 in the return statement is the name of a local object, and the cv-
8508 unqualified type of the local object is the same as the function
8509 return type, an implementation is permitted to omit creating the tem-
8510 porary object to hold the function return value [...]
8512 So, if this is a value-returning function that always returns the same
8513 local variable, remember it.
8515 It might be nice to be more flexible, and choose the first suitable
8516 variable even if the function sometimes returns something else, but
8517 then we run the risk of clobbering the variable we chose if the other
8518 returned expression uses the chosen variable somehow. And people expect
8519 this restriction, anyway. (jason 2000-11-19)
8521 See finish_function and finalize_nrv for the rest of this optimization. */
8523 named_return_value_okay_p =
8524 (retval != NULL_TREE
8525 /* Must be a local, automatic variable. */
8526 && VAR_P (retval)
8527 && DECL_CONTEXT (retval) == current_function_decl
8528 && ! TREE_STATIC (retval)
8529 /* And not a lambda or anonymous union proxy. */
8530 && !DECL_HAS_VALUE_EXPR_P (retval)
8531 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8532 /* The cv-unqualified type of the returned value must be the
8533 same as the cv-unqualified return type of the
8534 function. */
8535 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8536 (TYPE_MAIN_VARIANT (functype)))
8537 /* And the returned value must be non-volatile. */
8538 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8540 if (fn_returns_value_p && flag_elide_constructors)
8542 if (named_return_value_okay_p
8543 && (current_function_return_value == NULL_TREE
8544 || current_function_return_value == retval))
8545 current_function_return_value = retval;
8546 else
8547 current_function_return_value = error_mark_node;
8550 /* We don't need to do any conversions when there's nothing being
8551 returned. */
8552 if (!retval)
8553 return NULL_TREE;
8555 /* Do any required conversions. */
8556 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8557 /* No conversions are required. */
8559 else
8561 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8563 /* The functype's return type will have been set to void, if it
8564 was an incomplete type. Just treat this as 'return;' */
8565 if (VOID_TYPE_P (functype))
8566 return error_mark_node;
8568 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8569 treated as an rvalue for the purposes of overload resolution to
8570 favor move constructors over copy constructors.
8572 Note that these conditions are similar to, but not as strict as,
8573 the conditions for the named return value optimization. */
8574 if ((cxx_dialect != cxx98)
8575 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8576 || TREE_CODE (retval) == PARM_DECL)
8577 && DECL_CONTEXT (retval) == current_function_decl
8578 && !TREE_STATIC (retval)
8579 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8580 (TYPE_MAIN_VARIANT (functype)))
8581 /* This is only interesting for class type. */
8582 && CLASS_TYPE_P (functype))
8583 flags = flags | LOOKUP_PREFER_RVALUE;
8585 /* First convert the value to the function's return type, then
8586 to the type of return value's location to handle the
8587 case that functype is smaller than the valtype. */
8588 retval = convert_for_initialization
8589 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8590 tf_warning_or_error);
8591 retval = convert (valtype, retval);
8593 /* If the conversion failed, treat this just like `return;'. */
8594 if (retval == error_mark_node)
8595 return retval;
8596 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8597 else if (! cfun->returns_struct
8598 && TREE_CODE (retval) == TARGET_EXPR
8599 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8600 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8601 TREE_OPERAND (retval, 0));
8602 else
8603 maybe_warn_about_returning_address_of_local (retval);
8606 /* Actually copy the value returned into the appropriate location. */
8607 if (retval && retval != result)
8608 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8610 return retval;
8614 /* Returns nonzero if the pointer-type FROM can be converted to the
8615 pointer-type TO via a qualification conversion. If CONSTP is -1,
8616 then we return nonzero if the pointers are similar, and the
8617 cv-qualification signature of FROM is a proper subset of that of TO.
8619 If CONSTP is positive, then all outer pointers have been
8620 const-qualified. */
8622 static int
8623 comp_ptr_ttypes_real (tree to, tree from, int constp)
8625 bool to_more_cv_qualified = false;
8626 bool is_opaque_pointer = false;
8628 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8630 if (TREE_CODE (to) != TREE_CODE (from))
8631 return 0;
8633 if (TREE_CODE (from) == OFFSET_TYPE
8634 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8635 TYPE_OFFSET_BASETYPE (to)))
8636 return 0;
8638 /* Const and volatile mean something different for function types,
8639 so the usual checks are not appropriate. */
8640 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8642 if (!at_least_as_qualified_p (to, from))
8643 return 0;
8645 if (!at_least_as_qualified_p (from, to))
8647 if (constp == 0)
8648 return 0;
8649 to_more_cv_qualified = true;
8652 if (constp > 0)
8653 constp &= TYPE_READONLY (to);
8656 if (TREE_CODE (to) == VECTOR_TYPE)
8657 is_opaque_pointer = vector_targets_convertible_p (to, from);
8659 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8660 return ((constp >= 0 || to_more_cv_qualified)
8661 && (is_opaque_pointer
8662 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8666 /* When comparing, say, char ** to char const **, this function takes
8667 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8668 types to this function. */
8671 comp_ptr_ttypes (tree to, tree from)
8673 return comp_ptr_ttypes_real (to, from, 1);
8676 /* Returns true iff FNTYPE is a non-class type that involves
8677 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8678 if a parameter type is ill-formed. */
8680 bool
8681 error_type_p (const_tree type)
8683 tree t;
8685 switch (TREE_CODE (type))
8687 case ERROR_MARK:
8688 return true;
8690 case POINTER_TYPE:
8691 case REFERENCE_TYPE:
8692 case OFFSET_TYPE:
8693 return error_type_p (TREE_TYPE (type));
8695 case FUNCTION_TYPE:
8696 case METHOD_TYPE:
8697 if (error_type_p (TREE_TYPE (type)))
8698 return true;
8699 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8700 if (error_type_p (TREE_VALUE (t)))
8701 return true;
8702 return false;
8704 case RECORD_TYPE:
8705 if (TYPE_PTRMEMFUNC_P (type))
8706 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8707 return false;
8709 default:
8710 return false;
8714 /* Returns true if to and from are (possibly multi-level) pointers to the same
8715 type or inheritance-related types, regardless of cv-quals. */
8717 bool
8718 ptr_reasonably_similar (const_tree to, const_tree from)
8720 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8722 /* Any target type is similar enough to void. */
8723 if (VOID_TYPE_P (to))
8724 return !error_type_p (from);
8725 if (VOID_TYPE_P (from))
8726 return !error_type_p (to);
8728 if (TREE_CODE (to) != TREE_CODE (from))
8729 return false;
8731 if (TREE_CODE (from) == OFFSET_TYPE
8732 && comptypes (TYPE_OFFSET_BASETYPE (to),
8733 TYPE_OFFSET_BASETYPE (from),
8734 COMPARE_BASE | COMPARE_DERIVED))
8735 continue;
8737 if (TREE_CODE (to) == VECTOR_TYPE
8738 && vector_types_convertible_p (to, from, false))
8739 return true;
8741 if (TREE_CODE (to) == INTEGER_TYPE
8742 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8743 return true;
8745 if (TREE_CODE (to) == FUNCTION_TYPE)
8746 return !error_type_p (to) && !error_type_p (from);
8748 if (!TYPE_PTR_P (to))
8750 /* When either type is incomplete avoid DERIVED_FROM_P,
8751 which may call complete_type (c++/57942). */
8752 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8753 return comptypes
8754 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8755 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8760 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8761 pointer-to-member types) are the same, ignoring cv-qualification at
8762 all levels. */
8764 bool
8765 comp_ptr_ttypes_const (tree to, tree from)
8767 bool is_opaque_pointer = false;
8769 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8771 if (TREE_CODE (to) != TREE_CODE (from))
8772 return false;
8774 if (TREE_CODE (from) == OFFSET_TYPE
8775 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8776 TYPE_OFFSET_BASETYPE (to)))
8777 continue;
8779 if (TREE_CODE (to) == VECTOR_TYPE)
8780 is_opaque_pointer = vector_targets_convertible_p (to, from);
8782 if (!TYPE_PTR_P (to))
8783 return (is_opaque_pointer
8784 || same_type_ignoring_top_level_qualifiers_p (to, from));
8788 /* Returns the type qualifiers for this type, including the qualifiers on the
8789 elements for an array type. */
8792 cp_type_quals (const_tree type)
8794 int quals;
8795 /* This CONST_CAST is okay because strip_array_types returns its
8796 argument unmodified and we assign it to a const_tree. */
8797 type = strip_array_types (CONST_CAST_TREE (type));
8798 if (type == error_mark_node
8799 /* Quals on a FUNCTION_TYPE are memfn quals. */
8800 || TREE_CODE (type) == FUNCTION_TYPE)
8801 return TYPE_UNQUALIFIED;
8802 quals = TYPE_QUALS (type);
8803 /* METHOD and REFERENCE_TYPEs should never have quals. */
8804 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8805 && TREE_CODE (type) != REFERENCE_TYPE)
8806 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8807 == TYPE_UNQUALIFIED));
8808 return quals;
8811 /* Returns the function-ref-qualifier for TYPE */
8813 cp_ref_qualifier
8814 type_memfn_rqual (const_tree type)
8816 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8817 || TREE_CODE (type) == METHOD_TYPE);
8819 if (!FUNCTION_REF_QUALIFIED (type))
8820 return REF_QUAL_NONE;
8821 else if (FUNCTION_RVALUE_QUALIFIED (type))
8822 return REF_QUAL_RVALUE;
8823 else
8824 return REF_QUAL_LVALUE;
8827 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8828 METHOD_TYPE. */
8831 type_memfn_quals (const_tree type)
8833 if (TREE_CODE (type) == FUNCTION_TYPE)
8834 return TYPE_QUALS (type);
8835 else if (TREE_CODE (type) == METHOD_TYPE)
8836 return cp_type_quals (class_of_this_parm (type));
8837 else
8838 gcc_unreachable ();
8841 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8842 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8844 tree
8845 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8847 /* Could handle METHOD_TYPE here if necessary. */
8848 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8849 if (TYPE_QUALS (type) == memfn_quals
8850 && type_memfn_rqual (type) == rqual)
8851 return type;
8853 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8854 complex. */
8855 tree result = build_qualified_type (type, memfn_quals);
8856 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8857 return build_ref_qualified_type (result, rqual);
8860 /* Returns nonzero if TYPE is const or volatile. */
8862 bool
8863 cv_qualified_p (const_tree type)
8865 int quals = cp_type_quals (type);
8866 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8869 /* Returns nonzero if the TYPE contains a mutable member. */
8871 bool
8872 cp_has_mutable_p (const_tree type)
8874 /* This CONST_CAST is okay because strip_array_types returns its
8875 argument unmodified and we assign it to a const_tree. */
8876 type = strip_array_types (CONST_CAST_TREE(type));
8878 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8881 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8882 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8883 approximation. In particular, consider:
8885 int f();
8886 struct S { int i; };
8887 const S s = { f(); }
8889 Here, we will make "s" as TREE_READONLY (because it is declared
8890 "const") -- only to reverse ourselves upon seeing that the
8891 initializer is non-constant. */
8893 void
8894 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8896 tree type = TREE_TYPE (decl);
8898 if (type == error_mark_node)
8899 return;
8901 if (TREE_CODE (decl) == TYPE_DECL)
8902 return;
8904 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8905 && type_quals != TYPE_UNQUALIFIED));
8907 /* Avoid setting TREE_READONLY incorrectly. */
8908 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8909 constructor can produce constant init, so rely on cp_finish_decl to
8910 clear TREE_READONLY if the variable has non-constant init. */
8912 /* If the type has (or might have) a mutable component, that component
8913 might be modified. */
8914 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8915 type_quals &= ~TYPE_QUAL_CONST;
8917 c_apply_type_quals_to_decl (type_quals, decl);
8920 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8921 exemplar types such that casting T1 to T2 is casting away constness
8922 if and only if there is no implicit conversion from T1 to T2. */
8924 static void
8925 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8927 int quals1;
8928 int quals2;
8930 /* [expr.const.cast]
8932 For multi-level pointer to members and multi-level mixed pointers
8933 and pointers to members (conv.qual), the "member" aspect of a
8934 pointer to member level is ignored when determining if a const
8935 cv-qualifier has been cast away. */
8936 /* [expr.const.cast]
8938 For two pointer types:
8940 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8941 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8942 K is min(N,M)
8944 casting from X1 to X2 casts away constness if, for a non-pointer
8945 type T there does not exist an implicit conversion (clause
8946 _conv_) from:
8948 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8952 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8953 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8954 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8956 *t1 = cp_build_qualified_type (void_type_node,
8957 cp_type_quals (*t1));
8958 *t2 = cp_build_qualified_type (void_type_node,
8959 cp_type_quals (*t2));
8960 return;
8963 quals1 = cp_type_quals (*t1);
8964 quals2 = cp_type_quals (*t2);
8966 if (TYPE_PTRDATAMEM_P (*t1))
8967 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8968 else
8969 *t1 = TREE_TYPE (*t1);
8970 if (TYPE_PTRDATAMEM_P (*t2))
8971 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8972 else
8973 *t2 = TREE_TYPE (*t2);
8975 casts_away_constness_r (t1, t2, complain);
8976 *t1 = build_pointer_type (*t1);
8977 *t2 = build_pointer_type (*t2);
8978 *t1 = cp_build_qualified_type (*t1, quals1);
8979 *t2 = cp_build_qualified_type (*t2, quals2);
8982 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8983 constness.
8985 ??? This function returns non-zero if casting away qualifiers not
8986 just const. We would like to return to the caller exactly which
8987 qualifiers are casted away to give more accurate diagnostics.
8990 static bool
8991 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8993 if (TREE_CODE (t2) == REFERENCE_TYPE)
8995 /* [expr.const.cast]
8997 Casting from an lvalue of type T1 to an lvalue of type T2
8998 using a reference cast casts away constness if a cast from an
8999 rvalue of type "pointer to T1" to the type "pointer to T2"
9000 casts away constness. */
9001 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9002 return casts_away_constness (build_pointer_type (t1),
9003 build_pointer_type (TREE_TYPE (t2)),
9004 complain);
9007 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9008 /* [expr.const.cast]
9010 Casting from an rvalue of type "pointer to data member of X
9011 of type T1" to the type "pointer to data member of Y of type
9012 T2" casts away constness if a cast from an rvalue of type
9013 "pointer to T1" to the type "pointer to T2" casts away
9014 constness. */
9015 return casts_away_constness
9016 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9017 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9018 complain);
9020 /* Casting away constness is only something that makes sense for
9021 pointer or reference types. */
9022 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9023 return false;
9025 /* Top-level qualifiers don't matter. */
9026 t1 = TYPE_MAIN_VARIANT (t1);
9027 t2 = TYPE_MAIN_VARIANT (t2);
9028 casts_away_constness_r (&t1, &t2, complain);
9029 if (!can_convert (t2, t1, complain))
9030 return true;
9032 return false;
9035 /* If T is a REFERENCE_TYPE return the type to which T refers.
9036 Otherwise, return T itself. */
9038 tree
9039 non_reference (tree t)
9041 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9042 t = TREE_TYPE (t);
9043 return t;
9047 /* Return nonzero if REF is an lvalue valid for this language;
9048 otherwise, print an error message and return zero. USE says
9049 how the lvalue is being used and so selects the error message. */
9052 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9054 cp_lvalue_kind kind = lvalue_kind (ref);
9056 if (kind == clk_none)
9058 if (complain & tf_error)
9059 lvalue_error (input_location, use);
9060 return 0;
9062 else if (kind & (clk_rvalueref|clk_class))
9064 if (!(complain & tf_error))
9065 return 0;
9066 if (kind & clk_class)
9067 /* Make this a permerror because we used to accept it. */
9068 permerror (input_location, "using temporary as lvalue");
9069 else
9070 error ("using xvalue (rvalue reference) as lvalue");
9072 return 1;
9075 /* Return true if a user-defined literal operator is a raw operator. */
9077 bool
9078 check_raw_literal_operator (const_tree decl)
9080 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9081 tree argtype;
9082 int arity;
9083 bool maybe_raw_p = false;
9085 /* Count the number and type of arguments and check for ellipsis. */
9086 for (argtype = argtypes, arity = 0;
9087 argtype && argtype != void_list_node;
9088 ++arity, argtype = TREE_CHAIN (argtype))
9090 tree t = TREE_VALUE (argtype);
9092 if (same_type_p (t, const_string_type_node))
9093 maybe_raw_p = true;
9095 if (!argtype)
9096 return false; /* Found ellipsis. */
9098 if (!maybe_raw_p || arity != 1)
9099 return false;
9101 return true;
9105 /* Return true if a user-defined literal operator has one of the allowed
9106 argument types. */
9108 bool
9109 check_literal_operator_args (const_tree decl,
9110 bool *long_long_unsigned_p, bool *long_double_p)
9112 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9114 *long_long_unsigned_p = false;
9115 *long_double_p = false;
9116 if (processing_template_decl || processing_specialization)
9117 return argtypes == void_list_node;
9118 else
9120 tree argtype;
9121 int arity;
9122 int max_arity = 2;
9124 /* Count the number and type of arguments and check for ellipsis. */
9125 for (argtype = argtypes, arity = 0;
9126 argtype && argtype != void_list_node;
9127 argtype = TREE_CHAIN (argtype))
9129 tree t = TREE_VALUE (argtype);
9130 ++arity;
9132 if (TYPE_PTR_P (t))
9134 bool maybe_raw_p = false;
9135 t = TREE_TYPE (t);
9136 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9137 return false;
9138 t = TYPE_MAIN_VARIANT (t);
9139 if ((maybe_raw_p = same_type_p (t, char_type_node))
9140 || same_type_p (t, wchar_type_node)
9141 || same_type_p (t, char16_type_node)
9142 || same_type_p (t, char32_type_node))
9144 argtype = TREE_CHAIN (argtype);
9145 if (!argtype)
9146 return false;
9147 t = TREE_VALUE (argtype);
9148 if (maybe_raw_p && argtype == void_list_node)
9149 return true;
9150 else if (same_type_p (t, size_type_node))
9152 ++arity;
9153 continue;
9155 else
9156 return false;
9159 else if (same_type_p (t, long_long_unsigned_type_node))
9161 max_arity = 1;
9162 *long_long_unsigned_p = true;
9164 else if (same_type_p (t, long_double_type_node))
9166 max_arity = 1;
9167 *long_double_p = true;
9169 else if (same_type_p (t, char_type_node))
9170 max_arity = 1;
9171 else if (same_type_p (t, wchar_type_node))
9172 max_arity = 1;
9173 else if (same_type_p (t, char16_type_node))
9174 max_arity = 1;
9175 else if (same_type_p (t, char32_type_node))
9176 max_arity = 1;
9177 else
9178 return false;
9180 if (!argtype)
9181 return false; /* Found ellipsis. */
9183 if (arity != max_arity)
9184 return false;
9186 return true;