Added Compiler Support for _Cilk_spawn and _Cilk_sync for C++.
[official-gcc.git] / gcc / cp / typeck.c
blob01afbac7350c5894d64b2745c029b68a4dba3c06
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "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 if (scope && !check_dtor_name (scope, dtor_type))
2491 if (complain & tf_error)
2492 error ("qualified type %qT does not match destructor name ~%qT",
2493 scope, dtor_type);
2494 return error_mark_node;
2496 if (is_auto (dtor_type))
2497 dtor_type = object_type;
2498 else if (identifier_p (dtor_type))
2500 /* In a template, names we can't find a match for are still accepted
2501 destructor names, and we check them here. */
2502 if (check_dtor_name (object_type, dtor_type))
2503 dtor_type = object_type;
2504 else
2506 if (complain & tf_error)
2507 error ("object type %qT does not match destructor name ~%qT",
2508 object_type, dtor_type);
2509 return error_mark_node;
2513 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2515 if (complain & tf_error)
2516 error ("the type being destroyed is %qT, but the destructor "
2517 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2518 return error_mark_node;
2520 expr = lookup_member (dtor_type, complete_dtor_identifier,
2521 /*protect=*/1, /*want_type=*/false,
2522 tf_warning_or_error);
2523 expr = (adjust_result_of_qualified_name_lookup
2524 (expr, dtor_type, object_type));
2525 if (scope == NULL_TREE)
2526 /* We need to call adjust_result_of_qualified_name_lookup in case the
2527 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2528 that we still get virtual function binding. */
2529 BASELINK_QUALIFIED_P (expr) = false;
2530 return expr;
2533 /* An expression of the form "A::template B" has been resolved to
2534 DECL. Issue a diagnostic if B is not a template or template
2535 specialization. */
2537 void
2538 check_template_keyword (tree decl)
2540 /* The standard says:
2542 [temp.names]
2544 If a name prefixed by the keyword template is not a member
2545 template, the program is ill-formed.
2547 DR 228 removed the restriction that the template be a member
2548 template.
2550 DR 96, if accepted would add the further restriction that explicit
2551 template arguments must be provided if the template keyword is
2552 used, but, as of 2005-10-16, that DR is still in "drafting". If
2553 this DR is accepted, then the semantic checks here can be
2554 simplified, as the entity named must in fact be a template
2555 specialization, rather than, as at present, a set of overloaded
2556 functions containing at least one template function. */
2557 if (TREE_CODE (decl) != TEMPLATE_DECL
2558 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2560 if (!is_overloaded_fn (decl))
2561 permerror (input_location, "%qD is not a template", decl);
2562 else
2564 tree fns;
2565 fns = decl;
2566 if (BASELINK_P (fns))
2567 fns = BASELINK_FUNCTIONS (fns);
2568 while (fns)
2570 tree fn = OVL_CURRENT (fns);
2571 if (TREE_CODE (fn) == TEMPLATE_DECL
2572 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2573 break;
2574 if (TREE_CODE (fn) == FUNCTION_DECL
2575 && DECL_USE_TEMPLATE (fn)
2576 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2577 break;
2578 fns = OVL_NEXT (fns);
2580 if (!fns)
2581 permerror (input_location, "%qD is not a template", decl);
2586 /* This function is called by the parser to process a class member
2587 access expression of the form OBJECT.NAME. NAME is a node used by
2588 the parser to represent a name; it is not yet a DECL. It may,
2589 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2590 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2591 there is no reason to do the lookup twice, so the parser keeps the
2592 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2593 be a template via the use of the "A::template B" syntax. */
2595 tree
2596 finish_class_member_access_expr (tree object, tree name, bool template_p,
2597 tsubst_flags_t complain)
2599 tree expr;
2600 tree object_type;
2601 tree member;
2602 tree access_path = NULL_TREE;
2603 tree orig_object = object;
2604 tree orig_name = name;
2606 if (object == error_mark_node || name == error_mark_node)
2607 return error_mark_node;
2609 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2610 if (!objc_is_public (object, name))
2611 return error_mark_node;
2613 object_type = TREE_TYPE (object);
2615 if (processing_template_decl)
2617 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2618 dependent_type_p (object_type)
2619 /* If NAME is just an IDENTIFIER_NODE, then the expression
2620 is dependent. */
2621 || identifier_p (object)
2622 /* If NAME is "f<args>", where either 'f' or 'args' is
2623 dependent, then the expression is dependent. */
2624 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2625 && dependent_template_id_p (TREE_OPERAND (name, 0),
2626 TREE_OPERAND (name, 1)))
2627 /* If NAME is "T::X" where "T" is dependent, then the
2628 expression is dependent. */
2629 || (TREE_CODE (name) == SCOPE_REF
2630 && TYPE_P (TREE_OPERAND (name, 0))
2631 && dependent_type_p (TREE_OPERAND (name, 0))))
2632 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2633 object, name, NULL_TREE);
2634 object = build_non_dependent_expr (object);
2636 else if (c_dialect_objc ()
2637 && identifier_p (name)
2638 && (expr = objc_maybe_build_component_ref (object, name)))
2639 return expr;
2641 /* [expr.ref]
2643 The type of the first expression shall be "class object" (of a
2644 complete type). */
2645 if (!currently_open_class (object_type)
2646 && !complete_type_or_maybe_complain (object_type, object, complain))
2647 return error_mark_node;
2648 if (!CLASS_TYPE_P (object_type))
2650 if (complain & tf_error)
2652 if (POINTER_TYPE_P (object_type)
2653 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2654 error ("request for member %qD in %qE, which is of pointer "
2655 "type %qT (maybe you meant to use %<->%> ?)",
2656 name, object, object_type);
2657 else
2658 error ("request for member %qD in %qE, which is of non-class "
2659 "type %qT", name, object, object_type);
2661 return error_mark_node;
2664 if (BASELINK_P (name))
2665 /* A member function that has already been looked up. */
2666 member = name;
2667 else
2669 bool is_template_id = false;
2670 tree template_args = NULL_TREE;
2671 tree scope;
2673 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2675 is_template_id = true;
2676 template_args = TREE_OPERAND (name, 1);
2677 name = TREE_OPERAND (name, 0);
2679 if (TREE_CODE (name) == OVERLOAD)
2680 name = DECL_NAME (get_first_fn (name));
2681 else if (DECL_P (name))
2682 name = DECL_NAME (name);
2685 if (TREE_CODE (name) == SCOPE_REF)
2687 /* A qualified name. The qualifying class or namespace `S'
2688 has already been looked up; it is either a TYPE or a
2689 NAMESPACE_DECL. */
2690 scope = TREE_OPERAND (name, 0);
2691 name = TREE_OPERAND (name, 1);
2693 /* If SCOPE is a namespace, then the qualified name does not
2694 name a member of OBJECT_TYPE. */
2695 if (TREE_CODE (scope) == NAMESPACE_DECL)
2697 if (complain & tf_error)
2698 error ("%<%D::%D%> is not a member of %qT",
2699 scope, name, object_type);
2700 return error_mark_node;
2703 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2705 /* Looking up a member enumerator (c++/56793). */
2706 if (!TYPE_CLASS_SCOPE_P (scope)
2707 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2709 if (complain & tf_error)
2710 error ("%<%D::%D%> is not a member of %qT",
2711 scope, name, object_type);
2712 return error_mark_node;
2714 tree val = lookup_enumerator (scope, name);
2715 if (TREE_SIDE_EFFECTS (object))
2716 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2717 return val;
2720 gcc_assert (CLASS_TYPE_P (scope));
2721 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2723 if (constructor_name_p (name, scope))
2725 if (complain & tf_error)
2726 error ("cannot call constructor %<%T::%D%> directly",
2727 scope, name);
2728 return error_mark_node;
2731 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2732 access_path = lookup_base (object_type, scope, ba_check,
2733 NULL, complain);
2734 if (access_path == error_mark_node)
2735 return error_mark_node;
2736 if (!access_path)
2738 if (complain & tf_error)
2739 error ("%qT is not a base of %qT", scope, object_type);
2740 return error_mark_node;
2743 else
2745 scope = NULL_TREE;
2746 access_path = object_type;
2749 if (TREE_CODE (name) == BIT_NOT_EXPR)
2750 member = lookup_destructor (object, scope, name, complain);
2751 else
2753 /* Look up the member. */
2754 member = lookup_member (access_path, name, /*protect=*/1,
2755 /*want_type=*/false, complain);
2756 if (member == NULL_TREE)
2758 if (complain & tf_error)
2759 error ("%qD has no member named %qE",
2760 TREE_CODE (access_path) == TREE_BINFO
2761 ? TREE_TYPE (access_path) : object_type, name);
2762 return error_mark_node;
2764 if (member == error_mark_node)
2765 return error_mark_node;
2768 if (is_template_id)
2770 tree templ = member;
2772 if (BASELINK_P (templ))
2773 templ = lookup_template_function (templ, template_args);
2774 else
2776 if (complain & tf_error)
2777 error ("%qD is not a member template function", name);
2778 return error_mark_node;
2783 if (TREE_DEPRECATED (member))
2784 warn_deprecated_use (member, NULL_TREE);
2786 if (template_p)
2787 check_template_keyword (member);
2789 expr = build_class_member_access_expr (object, member, access_path,
2790 /*preserve_reference=*/false,
2791 complain);
2792 if (processing_template_decl && expr != error_mark_node)
2794 if (BASELINK_P (member))
2796 if (TREE_CODE (orig_name) == SCOPE_REF)
2797 BASELINK_QUALIFIED_P (member) = 1;
2798 orig_name = member;
2800 return build_min_non_dep (COMPONENT_REF, expr,
2801 orig_object, orig_name,
2802 NULL_TREE);
2805 return expr;
2808 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2809 type. */
2811 tree
2812 build_simple_component_ref (tree object, tree member)
2814 tree type = cp_build_qualified_type (TREE_TYPE (member),
2815 cp_type_quals (TREE_TYPE (object)));
2816 return fold_build3_loc (input_location,
2817 COMPONENT_REF, type,
2818 object, member, NULL_TREE);
2821 /* Return an expression for the MEMBER_NAME field in the internal
2822 representation of PTRMEM, a pointer-to-member function. (Each
2823 pointer-to-member function type gets its own RECORD_TYPE so it is
2824 more convenient to access the fields by name than by FIELD_DECL.)
2825 This routine converts the NAME to a FIELD_DECL and then creates the
2826 node for the complete expression. */
2828 tree
2829 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2831 tree ptrmem_type;
2832 tree member;
2834 /* This code is a stripped down version of
2835 build_class_member_access_expr. It does not work to use that
2836 routine directly because it expects the object to be of class
2837 type. */
2838 ptrmem_type = TREE_TYPE (ptrmem);
2839 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2840 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2841 /*want_type=*/false, tf_warning_or_error);
2842 return build_simple_component_ref (ptrmem, member);
2845 /* Given an expression PTR for a pointer, return an expression
2846 for the value pointed to.
2847 ERRORSTRING is the name of the operator to appear in error messages.
2849 This function may need to overload OPERATOR_FNNAME.
2850 Must also handle REFERENCE_TYPEs for C++. */
2852 tree
2853 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2854 tsubst_flags_t complain)
2856 tree orig_expr = expr;
2857 tree rval;
2859 if (processing_template_decl)
2861 /* Retain the type if we know the operand is a pointer. */
2862 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2863 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2864 if (type_dependent_expression_p (expr))
2865 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2866 expr = build_non_dependent_expr (expr);
2869 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2870 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2871 if (!rval)
2872 rval = cp_build_indirect_ref (expr, errorstring, complain);
2874 if (processing_template_decl && rval != error_mark_node)
2875 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2876 else
2877 return rval;
2880 /* Helper function called from c-common. */
2881 tree
2882 build_indirect_ref (location_t /*loc*/,
2883 tree ptr, ref_operator errorstring)
2885 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2888 tree
2889 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2890 tsubst_flags_t complain)
2892 tree pointer, type;
2894 if (ptr == current_class_ptr
2895 || (TREE_CODE (ptr) == NOP_EXPR
2896 && TREE_OPERAND (ptr, 0) == current_class_ptr
2897 && (same_type_ignoring_top_level_qualifiers_p
2898 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2899 return current_class_ref;
2901 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2902 ? ptr : decay_conversion (ptr, complain));
2903 if (pointer == error_mark_node)
2904 return error_mark_node;
2906 type = TREE_TYPE (pointer);
2908 if (POINTER_TYPE_P (type))
2910 /* [expr.unary.op]
2912 If the type of the expression is "pointer to T," the type
2913 of the result is "T." */
2914 tree t = TREE_TYPE (type);
2916 if (CONVERT_EXPR_P (ptr)
2917 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2919 /* If a warning is issued, mark it to avoid duplicates from
2920 the backend. This only needs to be done at
2921 warn_strict_aliasing > 2. */
2922 if (warn_strict_aliasing > 2)
2923 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2924 type, TREE_OPERAND (ptr, 0)))
2925 TREE_NO_WARNING (ptr) = 1;
2928 if (VOID_TYPE_P (t))
2930 /* A pointer to incomplete type (other than cv void) can be
2931 dereferenced [expr.unary.op]/1 */
2932 if (complain & tf_error)
2933 error ("%qT is not a pointer-to-object type", type);
2934 return error_mark_node;
2936 else if (TREE_CODE (pointer) == ADDR_EXPR
2937 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2938 /* The POINTER was something like `&x'. We simplify `*&x' to
2939 `x'. */
2940 return TREE_OPERAND (pointer, 0);
2941 else
2943 tree ref = build1 (INDIRECT_REF, t, pointer);
2945 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2946 so that we get the proper error message if the result is used
2947 to assign to. Also, &* is supposed to be a no-op. */
2948 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2949 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2950 TREE_SIDE_EFFECTS (ref)
2951 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2952 return ref;
2955 else if (!(complain & tf_error))
2956 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2958 /* `pointer' won't be an error_mark_node if we were given a
2959 pointer to member, so it's cool to check for this here. */
2960 else if (TYPE_PTRMEM_P (type))
2961 switch (errorstring)
2963 case RO_ARRAY_INDEXING:
2964 error ("invalid use of array indexing on pointer to member");
2965 break;
2966 case RO_UNARY_STAR:
2967 error ("invalid use of unary %<*%> on pointer to member");
2968 break;
2969 case RO_IMPLICIT_CONVERSION:
2970 error ("invalid use of implicit conversion on pointer to member");
2971 break;
2972 case RO_ARROW_STAR:
2973 error ("left hand operand of %<->*%> must be a pointer to class, "
2974 "but is a pointer to member of type %qT", type);
2975 break;
2976 default:
2977 gcc_unreachable ();
2979 else if (pointer != error_mark_node)
2980 invalid_indirection_error (input_location, type, errorstring);
2982 return error_mark_node;
2985 /* This handles expressions of the form "a[i]", which denotes
2986 an array reference.
2988 This is logically equivalent in C to *(a+i), but we may do it differently.
2989 If A is a variable or a member, we generate a primitive ARRAY_REF.
2990 This avoids forcing the array out of registers, and can work on
2991 arrays that are not lvalues (for example, members of structures returned
2992 by functions).
2994 If INDEX is of some user-defined type, it must be converted to
2995 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2996 will inherit the type of the array, which will be some pointer type.
2998 LOC is the location to use in building the array reference. */
3000 tree
3001 cp_build_array_ref (location_t loc, tree array, tree idx,
3002 tsubst_flags_t complain)
3004 tree ret;
3006 if (idx == 0)
3008 if (complain & tf_error)
3009 error_at (loc, "subscript missing in array reference");
3010 return error_mark_node;
3013 /* If an array's index is an array notation, then its rank cannot be
3014 greater than one. */
3015 if (flag_enable_cilkplus && contains_array_notation_expr (idx))
3017 size_t rank = 0;
3019 /* If find_rank returns false, then it should have reported an error,
3020 thus it is unnecessary for repetition. */
3021 if (!find_rank (loc, idx, idx, true, &rank))
3022 return error_mark_node;
3023 if (rank > 1)
3025 error_at (loc, "rank of the array%'s index is greater than 1");
3026 return error_mark_node;
3029 if (TREE_TYPE (array) == error_mark_node
3030 || TREE_TYPE (idx) == error_mark_node)
3031 return error_mark_node;
3033 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3034 inside it. */
3035 switch (TREE_CODE (array))
3037 case COMPOUND_EXPR:
3039 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3040 complain);
3041 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3042 TREE_OPERAND (array, 0), value);
3043 SET_EXPR_LOCATION (ret, loc);
3044 return ret;
3047 case COND_EXPR:
3048 ret = build_conditional_expr
3049 (loc, TREE_OPERAND (array, 0),
3050 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3051 complain),
3052 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3053 complain),
3054 complain);
3055 protected_set_expr_location (ret, loc);
3056 return ret;
3058 default:
3059 break;
3062 convert_vector_to_pointer_for_subscript (loc, &array, idx);
3064 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3066 tree rval, type;
3068 warn_array_subscript_with_type_char (idx);
3070 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3072 if (complain & tf_error)
3073 error_at (loc, "array subscript is not an integer");
3074 return error_mark_node;
3077 /* Apply integral promotions *after* noticing character types.
3078 (It is unclear why we do these promotions -- the standard
3079 does not say that we should. In fact, the natural thing would
3080 seem to be to convert IDX to ptrdiff_t; we're performing
3081 pointer arithmetic.) */
3082 idx = cp_perform_integral_promotions (idx, complain);
3084 /* An array that is indexed by a non-constant
3085 cannot be stored in a register; we must be able to do
3086 address arithmetic on its address.
3087 Likewise an array of elements of variable size. */
3088 if (TREE_CODE (idx) != INTEGER_CST
3089 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3090 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3091 != INTEGER_CST)))
3093 if (!cxx_mark_addressable (array))
3094 return error_mark_node;
3097 /* An array that is indexed by a constant value which is not within
3098 the array bounds cannot be stored in a register either; because we
3099 would get a crash in store_bit_field/extract_bit_field when trying
3100 to access a non-existent part of the register. */
3101 if (TREE_CODE (idx) == INTEGER_CST
3102 && TYPE_DOMAIN (TREE_TYPE (array))
3103 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3105 if (!cxx_mark_addressable (array))
3106 return error_mark_node;
3109 if (!lvalue_p (array) && (complain & tf_error))
3110 pedwarn (loc, OPT_Wpedantic,
3111 "ISO C++ forbids subscripting non-lvalue array");
3113 /* Note in C++ it is valid to subscript a `register' array, since
3114 it is valid to take the address of something with that
3115 storage specification. */
3116 if (extra_warnings)
3118 tree foo = array;
3119 while (TREE_CODE (foo) == COMPONENT_REF)
3120 foo = TREE_OPERAND (foo, 0);
3121 if (VAR_P (foo) && DECL_REGISTER (foo)
3122 && (complain & tf_warning))
3123 warning_at (loc, OPT_Wextra,
3124 "subscripting array declared %<register%>");
3127 type = TREE_TYPE (TREE_TYPE (array));
3128 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3129 /* Array ref is const/volatile if the array elements are
3130 or if the array is.. */
3131 TREE_READONLY (rval)
3132 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3133 TREE_SIDE_EFFECTS (rval)
3134 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3135 TREE_THIS_VOLATILE (rval)
3136 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3137 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3138 complain);
3139 protected_set_expr_location (ret, loc);
3140 return ret;
3144 tree ar = cp_default_conversion (array, complain);
3145 tree ind = cp_default_conversion (idx, complain);
3147 /* Put the integer in IND to simplify error checking. */
3148 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3150 tree temp = ar;
3151 ar = ind;
3152 ind = temp;
3155 if (ar == error_mark_node || ind == error_mark_node)
3156 return error_mark_node;
3158 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3160 if (complain & tf_error)
3161 error_at (loc, "subscripted value is neither array nor pointer");
3162 return error_mark_node;
3164 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3166 if (complain & tf_error)
3167 error_at (loc, "array subscript is not an integer");
3168 return error_mark_node;
3171 warn_array_subscript_with_type_char (idx);
3173 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3174 PLUS_EXPR, ar, ind,
3175 complain),
3176 RO_ARRAY_INDEXING,
3177 complain);
3178 protected_set_expr_location (ret, loc);
3179 return ret;
3183 /* Entry point for Obj-C++. */
3185 tree
3186 build_array_ref (location_t loc, tree array, tree idx)
3188 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3191 /* Resolve a pointer to member function. INSTANCE is the object
3192 instance to use, if the member points to a virtual member.
3194 This used to avoid checking for virtual functions if basetype
3195 has no virtual functions, according to an earlier ANSI draft.
3196 With the final ISO C++ rules, such an optimization is
3197 incorrect: A pointer to a derived member can be static_cast
3198 to pointer-to-base-member, as long as the dynamic object
3199 later has the right member. So now we only do this optimization
3200 when we know the dynamic type of the object. */
3202 tree
3203 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3204 tsubst_flags_t complain)
3206 if (TREE_CODE (function) == OFFSET_REF)
3207 function = TREE_OPERAND (function, 1);
3209 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3211 tree idx, delta, e1, e2, e3, vtbl;
3212 bool nonvirtual;
3213 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3214 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3216 tree instance_ptr = *instance_ptrptr;
3217 tree instance_save_expr = 0;
3218 if (instance_ptr == error_mark_node)
3220 if (TREE_CODE (function) == PTRMEM_CST)
3222 /* Extracting the function address from a pmf is only
3223 allowed with -Wno-pmf-conversions. It only works for
3224 pmf constants. */
3225 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3226 e1 = convert (fntype, e1);
3227 return e1;
3229 else
3231 if (complain & tf_error)
3232 error ("object missing in use of %qE", function);
3233 return error_mark_node;
3237 /* True if we know that the dynamic type of the object doesn't have
3238 virtual functions, so we can assume the PFN field is a pointer. */
3239 nonvirtual = (COMPLETE_TYPE_P (basetype)
3240 && !TYPE_POLYMORPHIC_P (basetype)
3241 && resolves_to_fixed_type_p (instance_ptr, 0));
3243 if (TREE_SIDE_EFFECTS (instance_ptr))
3244 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3246 if (TREE_SIDE_EFFECTS (function))
3247 function = save_expr (function);
3249 /* Start by extracting all the information from the PMF itself. */
3250 e3 = pfn_from_ptrmemfunc (function);
3251 delta = delta_from_ptrmemfunc (function);
3252 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3253 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3255 case ptrmemfunc_vbit_in_pfn:
3256 e1 = cp_build_binary_op (input_location,
3257 BIT_AND_EXPR, idx, integer_one_node,
3258 complain);
3259 idx = cp_build_binary_op (input_location,
3260 MINUS_EXPR, idx, integer_one_node,
3261 complain);
3262 if (idx == error_mark_node)
3263 return error_mark_node;
3264 break;
3266 case ptrmemfunc_vbit_in_delta:
3267 e1 = cp_build_binary_op (input_location,
3268 BIT_AND_EXPR, delta, integer_one_node,
3269 complain);
3270 delta = cp_build_binary_op (input_location,
3271 RSHIFT_EXPR, delta, integer_one_node,
3272 complain);
3273 if (delta == error_mark_node)
3274 return error_mark_node;
3275 break;
3277 default:
3278 gcc_unreachable ();
3281 if (e1 == error_mark_node)
3282 return error_mark_node;
3284 /* Convert down to the right base before using the instance. A
3285 special case is that in a pointer to member of class C, C may
3286 be incomplete. In that case, the function will of course be
3287 a member of C, and no conversion is required. In fact,
3288 lookup_base will fail in that case, because incomplete
3289 classes do not have BINFOs. */
3290 if (!same_type_ignoring_top_level_qualifiers_p
3291 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3293 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3294 basetype, ba_check, NULL, complain);
3295 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3296 1, complain);
3297 if (instance_ptr == error_mark_node)
3298 return error_mark_node;
3300 /* ...and then the delta in the PMF. */
3301 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3303 /* Hand back the adjusted 'this' argument to our caller. */
3304 *instance_ptrptr = instance_ptr;
3306 if (nonvirtual)
3307 /* Now just return the pointer. */
3308 return e3;
3310 /* Next extract the vtable pointer from the object. */
3311 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3312 instance_ptr);
3313 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3314 if (vtbl == error_mark_node)
3315 return error_mark_node;
3317 /* Finally, extract the function pointer from the vtable. */
3318 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3319 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3320 if (e2 == error_mark_node)
3321 return error_mark_node;
3322 TREE_CONSTANT (e2) = 1;
3324 /* When using function descriptors, the address of the
3325 vtable entry is treated as a function pointer. */
3326 if (TARGET_VTABLE_USES_DESCRIPTORS)
3327 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3328 cp_build_addr_expr (e2, complain));
3330 e2 = fold_convert (TREE_TYPE (e3), e2);
3331 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3332 if (e1 == error_mark_node)
3333 return error_mark_node;
3335 /* Make sure this doesn't get evaluated first inside one of the
3336 branches of the COND_EXPR. */
3337 if (instance_save_expr)
3338 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3339 instance_save_expr, e1);
3341 function = e1;
3343 return function;
3346 /* Used by the C-common bits. */
3347 tree
3348 build_function_call (location_t /*loc*/,
3349 tree function, tree params)
3351 return cp_build_function_call (function, params, tf_warning_or_error);
3354 /* Used by the C-common bits. */
3355 tree
3356 build_function_call_vec (location_t /*loc*/,
3357 tree function, vec<tree, va_gc> *params,
3358 vec<tree, va_gc> * /*origtypes*/)
3360 vec<tree, va_gc> *orig_params = params;
3361 tree ret = cp_build_function_call_vec (function, &params,
3362 tf_warning_or_error);
3364 /* cp_build_function_call_vec can reallocate PARAMS by adding
3365 default arguments. That should never happen here. Verify
3366 that. */
3367 gcc_assert (params == orig_params);
3369 return ret;
3372 /* Build a function call using a tree list of arguments. */
3374 tree
3375 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3377 vec<tree, va_gc> *vec;
3378 tree ret;
3380 vec = make_tree_vector ();
3381 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3382 vec_safe_push (vec, TREE_VALUE (params));
3383 ret = cp_build_function_call_vec (function, &vec, complain);
3384 release_tree_vector (vec);
3385 return ret;
3388 /* Build a function call using varargs. */
3390 tree
3391 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3393 vec<tree, va_gc> *vec;
3394 va_list args;
3395 tree ret, t;
3397 vec = make_tree_vector ();
3398 va_start (args, complain);
3399 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3400 vec_safe_push (vec, t);
3401 va_end (args);
3402 ret = cp_build_function_call_vec (function, &vec, complain);
3403 release_tree_vector (vec);
3404 return ret;
3407 /* Build a function call using a vector of arguments. PARAMS may be
3408 NULL if there are no parameters. This changes the contents of
3409 PARAMS. */
3411 tree
3412 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3413 tsubst_flags_t complain)
3415 tree fntype, fndecl;
3416 int is_method;
3417 tree original = function;
3418 int nargs;
3419 tree *argarray;
3420 tree parm_types;
3421 vec<tree, va_gc> *allocated = NULL;
3422 tree ret;
3424 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3425 expressions, like those used for ObjC messenger dispatches. */
3426 if (params != NULL && !vec_safe_is_empty (*params))
3427 function = objc_rewrite_function_call (function, (**params)[0]);
3429 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3430 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3431 if (TREE_CODE (function) == NOP_EXPR
3432 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3433 function = TREE_OPERAND (function, 0);
3435 if (TREE_CODE (function) == FUNCTION_DECL)
3437 mark_used (function);
3438 fndecl = function;
3440 /* Convert anything with function type to a pointer-to-function. */
3441 if (DECL_MAIN_P (function) && (complain & tf_error))
3442 pedwarn (input_location, OPT_Wpedantic,
3443 "ISO C++ forbids calling %<::main%> from within program");
3445 function = build_addr_func (function, complain);
3447 else
3449 fndecl = NULL_TREE;
3451 function = build_addr_func (function, complain);
3454 if (function == error_mark_node)
3455 return error_mark_node;
3457 fntype = TREE_TYPE (function);
3459 if (TYPE_PTRMEMFUNC_P (fntype))
3461 if (complain & tf_error)
3462 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3463 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3464 original, original);
3465 return error_mark_node;
3468 is_method = (TYPE_PTR_P (fntype)
3469 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3471 if (!(TYPE_PTRFN_P (fntype)
3472 || is_method
3473 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3475 if (complain & tf_error)
3477 if (!flag_diagnostics_show_caret)
3478 error_at (input_location,
3479 "%qE cannot be used as a function", original);
3480 else if (DECL_P (original))
3481 error_at (input_location,
3482 "%qD cannot be used as a function", original);
3483 else
3484 error_at (input_location,
3485 "expression cannot be used as a function");
3488 return error_mark_node;
3491 /* fntype now gets the type of function pointed to. */
3492 fntype = TREE_TYPE (fntype);
3493 parm_types = TYPE_ARG_TYPES (fntype);
3495 if (params == NULL)
3497 allocated = make_tree_vector ();
3498 params = &allocated;
3501 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3502 complain);
3503 if (nargs < 0)
3504 return error_mark_node;
3506 argarray = (*params)->address ();
3508 /* Check for errors in format strings and inappropriately
3509 null parameters. */
3510 check_function_arguments (fntype, nargs, argarray);
3512 ret = build_cxx_call (function, nargs, argarray, complain);
3514 if (allocated != NULL)
3515 release_tree_vector (allocated);
3517 return ret;
3520 /* Subroutine of convert_arguments.
3521 Warn about wrong number of args are genereted. */
3523 static void
3524 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3526 if (fndecl)
3528 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3530 if (DECL_NAME (fndecl) == NULL_TREE
3531 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3532 error_at (loc,
3533 too_many_p
3534 ? G_("too many arguments to constructor %q#D")
3535 : G_("too few arguments to constructor %q#D"),
3536 fndecl);
3537 else
3538 error_at (loc,
3539 too_many_p
3540 ? G_("too many arguments to member function %q#D")
3541 : G_("too few arguments to member function %q#D"),
3542 fndecl);
3544 else
3545 error_at (loc,
3546 too_many_p
3547 ? G_("too many arguments to function %q#D")
3548 : G_("too few arguments to function %q#D"),
3549 fndecl);
3550 inform (DECL_SOURCE_LOCATION (fndecl),
3551 "declared here");
3553 else
3555 if (c_dialect_objc () && objc_message_selector ())
3556 error_at (loc,
3557 too_many_p
3558 ? G_("too many arguments to method %q#D")
3559 : G_("too few arguments to method %q#D"),
3560 objc_message_selector ());
3561 else
3562 error_at (loc, too_many_p ? G_("too many arguments to function")
3563 : G_("too few arguments to function"));
3567 /* Convert the actual parameter expressions in the list VALUES to the
3568 types in the list TYPELIST. The converted expressions are stored
3569 back in the VALUES vector.
3570 If parmdecls is exhausted, or when an element has NULL as its type,
3571 perform the default conversions.
3573 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3575 This is also where warnings about wrong number of args are generated.
3577 Returns the actual number of arguments processed (which might be less
3578 than the length of the vector), or -1 on error.
3580 In C++, unspecified trailing parameters can be filled in with their
3581 default arguments, if such were specified. Do so here. */
3583 static int
3584 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3585 int flags, tsubst_flags_t complain)
3587 tree typetail;
3588 unsigned int i;
3590 /* Argument passing is always copy-initialization. */
3591 flags |= LOOKUP_ONLYCONVERTING;
3593 for (i = 0, typetail = typelist;
3594 i < vec_safe_length (*values);
3595 i++)
3597 tree type = typetail ? TREE_VALUE (typetail) : 0;
3598 tree val = (**values)[i];
3600 if (val == error_mark_node || type == error_mark_node)
3601 return -1;
3603 if (type == void_type_node)
3605 if (complain & tf_error)
3607 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3608 return i;
3610 else
3611 return -1;
3614 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3615 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3616 if (TREE_CODE (val) == NOP_EXPR
3617 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3618 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3619 val = TREE_OPERAND (val, 0);
3621 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3623 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3624 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3625 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3626 val = decay_conversion (val, complain);
3629 if (val == error_mark_node)
3630 return -1;
3632 if (type != 0)
3634 /* Formal parm type is specified by a function prototype. */
3635 tree parmval;
3637 if (!COMPLETE_TYPE_P (complete_type (type)))
3639 if (complain & tf_error)
3641 if (fndecl)
3642 error ("parameter %P of %qD has incomplete type %qT",
3643 i, fndecl, type);
3644 else
3645 error ("parameter %P has incomplete type %qT", i, type);
3647 parmval = error_mark_node;
3649 else
3651 parmval = convert_for_initialization
3652 (NULL_TREE, type, val, flags,
3653 ICR_ARGPASS, fndecl, i, complain);
3654 parmval = convert_for_arg_passing (type, parmval, complain);
3657 if (parmval == error_mark_node)
3658 return -1;
3660 (**values)[i] = parmval;
3662 else
3664 if (fndecl && magic_varargs_p (fndecl))
3665 /* Don't do ellipsis conversion for __built_in_constant_p
3666 as this will result in spurious errors for non-trivial
3667 types. */
3668 val = require_complete_type_sfinae (val, complain);
3669 else
3670 val = convert_arg_to_ellipsis (val, complain);
3672 (**values)[i] = val;
3675 if (typetail)
3676 typetail = TREE_CHAIN (typetail);
3679 if (typetail != 0 && typetail != void_list_node)
3681 /* See if there are default arguments that can be used. Because
3682 we hold default arguments in the FUNCTION_TYPE (which is so
3683 wrong), we can see default parameters here from deduced
3684 contexts (and via typeof) for indirect function calls.
3685 Fortunately we know whether we have a function decl to
3686 provide default arguments in a language conformant
3687 manner. */
3688 if (fndecl && TREE_PURPOSE (typetail)
3689 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3691 for (; typetail != void_list_node; ++i)
3693 tree parmval
3694 = convert_default_arg (TREE_VALUE (typetail),
3695 TREE_PURPOSE (typetail),
3696 fndecl, i, complain);
3698 if (parmval == error_mark_node)
3699 return -1;
3701 vec_safe_push (*values, parmval);
3702 typetail = TREE_CHAIN (typetail);
3703 /* ends with `...'. */
3704 if (typetail == NULL_TREE)
3705 break;
3708 else
3710 if (complain & tf_error)
3711 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3712 return -1;
3716 return (int) i;
3719 /* Build a binary-operation expression, after performing default
3720 conversions on the operands. CODE is the kind of expression to
3721 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3722 are the tree codes which correspond to ARG1 and ARG2 when issuing
3723 warnings about possibly misplaced parentheses. They may differ
3724 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3725 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3726 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3727 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3728 ARG2_CODE as ERROR_MARK. */
3730 tree
3731 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3732 enum tree_code arg1_code, tree arg2,
3733 enum tree_code arg2_code, tree *overload,
3734 tsubst_flags_t complain)
3736 tree orig_arg1;
3737 tree orig_arg2;
3738 tree expr;
3740 orig_arg1 = arg1;
3741 orig_arg2 = arg2;
3743 if (processing_template_decl)
3745 if (type_dependent_expression_p (arg1)
3746 || type_dependent_expression_p (arg2))
3747 return build_min_nt_loc (loc, code, arg1, arg2);
3748 arg1 = build_non_dependent_expr (arg1);
3749 arg2 = build_non_dependent_expr (arg2);
3752 if (code == DOTSTAR_EXPR)
3753 expr = build_m_component_ref (arg1, arg2, complain);
3754 else
3755 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3756 overload, complain);
3758 /* Check for cases such as x+y<<z which users are likely to
3759 misinterpret. But don't warn about obj << x + y, since that is a
3760 common idiom for I/O. */
3761 if (warn_parentheses
3762 && (complain & tf_warning)
3763 && !processing_template_decl
3764 && !error_operand_p (arg1)
3765 && !error_operand_p (arg2)
3766 && (code != LSHIFT_EXPR
3767 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3768 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3769 arg2_code, orig_arg2);
3771 if (processing_template_decl && expr != error_mark_node)
3772 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3774 return expr;
3777 /* Build and return an ARRAY_REF expression. */
3779 tree
3780 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3781 tsubst_flags_t complain)
3783 tree orig_arg1 = arg1;
3784 tree orig_arg2 = arg2;
3785 tree expr;
3787 if (processing_template_decl)
3789 if (type_dependent_expression_p (arg1)
3790 || type_dependent_expression_p (arg2))
3791 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3792 NULL_TREE, NULL_TREE);
3793 arg1 = build_non_dependent_expr (arg1);
3794 arg2 = build_non_dependent_expr (arg2);
3797 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3798 NULL_TREE, /*overload=*/NULL, complain);
3800 if (processing_template_decl && expr != error_mark_node)
3801 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3802 NULL_TREE, NULL_TREE);
3803 return expr;
3806 /* Return whether OP is an expression of enum type cast to integer
3807 type. In C++ even unsigned enum types are cast to signed integer
3808 types. We do not want to issue warnings about comparisons between
3809 signed and unsigned types when one of the types is an enum type.
3810 Those warnings are always false positives in practice. */
3812 static bool
3813 enum_cast_to_int (tree op)
3815 if (TREE_CODE (op) == NOP_EXPR
3816 && TREE_TYPE (op) == integer_type_node
3817 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3818 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3819 return true;
3821 /* The cast may have been pushed into a COND_EXPR. */
3822 if (TREE_CODE (op) == COND_EXPR)
3823 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3824 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3826 return false;
3829 /* For the c-common bits. */
3830 tree
3831 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3832 int /*convert_p*/)
3834 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3838 /* Build a binary-operation expression without default conversions.
3839 CODE is the kind of expression to build.
3840 LOCATION is the location_t of the operator in the source code.
3841 This function differs from `build' in several ways:
3842 the data type of the result is computed and recorded in it,
3843 warnings are generated if arg data types are invalid,
3844 special handling for addition and subtraction of pointers is known,
3845 and some optimization is done (operations on narrow ints
3846 are done in the narrower type when that gives the same result).
3847 Constant folding is also done before the result is returned.
3849 Note that the operands will never have enumeral types
3850 because either they have just had the default conversions performed
3851 or they have both just been converted to some other type in which
3852 the arithmetic is to be done.
3854 C++: must do special pointer arithmetic when implementing
3855 multiple inheritance, and deal with pointer to member functions. */
3857 tree
3858 cp_build_binary_op (location_t location,
3859 enum tree_code code, tree orig_op0, tree orig_op1,
3860 tsubst_flags_t complain)
3862 tree op0, op1;
3863 enum tree_code code0, code1;
3864 tree type0, type1;
3865 const char *invalid_op_diag;
3867 /* Expression code to give to the expression when it is built.
3868 Normally this is CODE, which is what the caller asked for,
3869 but in some special cases we change it. */
3870 enum tree_code resultcode = code;
3872 /* Data type in which the computation is to be performed.
3873 In the simplest cases this is the common type of the arguments. */
3874 tree result_type = NULL;
3876 /* Nonzero means operands have already been type-converted
3877 in whatever way is necessary.
3878 Zero means they need to be converted to RESULT_TYPE. */
3879 int converted = 0;
3881 /* Nonzero means create the expression with this type, rather than
3882 RESULT_TYPE. */
3883 tree build_type = 0;
3885 /* Nonzero means after finally constructing the expression
3886 convert it to this type. */
3887 tree final_type = 0;
3889 tree result;
3890 tree orig_type = NULL;
3892 /* Nonzero if this is an operation like MIN or MAX which can
3893 safely be computed in short if both args are promoted shorts.
3894 Also implies COMMON.
3895 -1 indicates a bitwise operation; this makes a difference
3896 in the exact conditions for when it is safe to do the operation
3897 in a narrower mode. */
3898 int shorten = 0;
3900 /* Nonzero if this is a comparison operation;
3901 if both args are promoted shorts, compare the original shorts.
3902 Also implies COMMON. */
3903 int short_compare = 0;
3905 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3906 int common = 0;
3908 /* True if both operands have arithmetic type. */
3909 bool arithmetic_types_p;
3911 /* Apply default conversions. */
3912 op0 = orig_op0;
3913 op1 = orig_op1;
3915 /* Remember whether we're doing / or %. */
3916 bool doing_div_or_mod = false;
3918 /* Remember whether we're doing << or >>. */
3919 bool doing_shift = false;
3921 /* Tree holding instrumentation expression. */
3922 tree instrument_expr = NULL;
3924 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3925 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3926 || code == TRUTH_XOR_EXPR)
3928 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3929 op0 = decay_conversion (op0, complain);
3930 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3931 op1 = decay_conversion (op1, complain);
3933 else
3935 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3936 op0 = cp_default_conversion (op0, complain);
3937 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3938 op1 = cp_default_conversion (op1, complain);
3941 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3942 STRIP_TYPE_NOPS (op0);
3943 STRIP_TYPE_NOPS (op1);
3945 /* DTRT if one side is an overloaded function, but complain about it. */
3946 if (type_unknown_p (op0))
3948 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3949 if (t != error_mark_node)
3951 if (complain & tf_error)
3952 permerror (input_location, "assuming cast to type %qT from overloaded function",
3953 TREE_TYPE (t));
3954 op0 = t;
3957 if (type_unknown_p (op1))
3959 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3960 if (t != error_mark_node)
3962 if (complain & tf_error)
3963 permerror (input_location, "assuming cast to type %qT from overloaded function",
3964 TREE_TYPE (t));
3965 op1 = t;
3969 type0 = TREE_TYPE (op0);
3970 type1 = TREE_TYPE (op1);
3972 /* The expression codes of the data types of the arguments tell us
3973 whether the arguments are integers, floating, pointers, etc. */
3974 code0 = TREE_CODE (type0);
3975 code1 = TREE_CODE (type1);
3977 /* If an error was already reported for one of the arguments,
3978 avoid reporting another error. */
3979 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3980 return error_mark_node;
3982 if ((invalid_op_diag
3983 = targetm.invalid_binary_op (code, type0, type1)))
3985 if (complain & tf_error)
3986 error (invalid_op_diag);
3987 return error_mark_node;
3990 /* Issue warnings about peculiar, but valid, uses of NULL. */
3991 if ((orig_op0 == null_node || orig_op1 == null_node)
3992 /* It's reasonable to use pointer values as operands of &&
3993 and ||, so NULL is no exception. */
3994 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3995 && ( /* Both are NULL (or 0) and the operation was not a
3996 comparison or a pointer subtraction. */
3997 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3998 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3999 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4000 || (!null_ptr_cst_p (orig_op0)
4001 && !TYPE_PTR_OR_PTRMEM_P (type0))
4002 || (!null_ptr_cst_p (orig_op1)
4003 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4004 && (complain & tf_warning))
4006 source_location loc =
4007 expansion_point_location_if_in_system_header (input_location);
4009 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4012 /* In case when one of the operands of the binary operation is
4013 a vector and another is a scalar -- convert scalar to vector. */
4014 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4016 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4017 complain & tf_error);
4019 switch (convert_flag)
4021 case stv_error:
4022 return error_mark_node;
4023 case stv_firstarg:
4025 op0 = save_expr (op0);
4026 op0 = convert (TREE_TYPE (type1), op0);
4027 op0 = build_vector_from_val (type1, op0);
4028 type0 = TREE_TYPE (op0);
4029 code0 = TREE_CODE (type0);
4030 converted = 1;
4031 break;
4033 case stv_secondarg:
4035 op1 = save_expr (op1);
4036 op1 = convert (TREE_TYPE (type0), op1);
4037 op1 = build_vector_from_val (type0, op1);
4038 type1 = TREE_TYPE (op1);
4039 code1 = TREE_CODE (type1);
4040 converted = 1;
4041 break;
4043 default:
4044 break;
4048 switch (code)
4050 case MINUS_EXPR:
4051 /* Subtraction of two similar pointers.
4052 We must subtract them as integers, then divide by object size. */
4053 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4054 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4055 TREE_TYPE (type1)))
4056 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4057 complain);
4058 /* In all other cases except pointer - int, the usual arithmetic
4059 rules apply. */
4060 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4062 common = 1;
4063 break;
4065 /* The pointer - int case is just like pointer + int; fall
4066 through. */
4067 case PLUS_EXPR:
4068 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4069 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4071 tree ptr_operand;
4072 tree int_operand;
4073 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4074 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4075 if (processing_template_decl)
4077 result_type = TREE_TYPE (ptr_operand);
4078 break;
4080 return cp_pointer_int_sum (code,
4081 ptr_operand,
4082 int_operand,
4083 complain);
4085 common = 1;
4086 break;
4088 case MULT_EXPR:
4089 common = 1;
4090 break;
4092 case TRUNC_DIV_EXPR:
4093 case CEIL_DIV_EXPR:
4094 case FLOOR_DIV_EXPR:
4095 case ROUND_DIV_EXPR:
4096 case EXACT_DIV_EXPR:
4097 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4098 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4099 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4100 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4102 enum tree_code tcode0 = code0, tcode1 = code1;
4103 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4104 cop1 = maybe_constant_value (cop1);
4106 if (tcode0 == INTEGER_TYPE)
4107 doing_div_or_mod = true;
4109 warn_for_div_by_zero (location, cop1);
4111 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4112 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4113 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4114 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4116 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4117 resultcode = RDIV_EXPR;
4118 else
4119 /* When dividing two signed integers, we have to promote to int.
4120 unless we divide by a constant != -1. Note that default
4121 conversion will have been performed on the operands at this
4122 point, so we have to dig out the original type to find out if
4123 it was unsigned. */
4124 shorten = ((TREE_CODE (op0) == NOP_EXPR
4125 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4126 || (TREE_CODE (op1) == INTEGER_CST
4127 && ! integer_all_onesp (op1)));
4129 common = 1;
4131 break;
4133 case BIT_AND_EXPR:
4134 case BIT_IOR_EXPR:
4135 case BIT_XOR_EXPR:
4136 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4137 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4138 && !VECTOR_FLOAT_TYPE_P (type0)
4139 && !VECTOR_FLOAT_TYPE_P (type1)))
4140 shorten = -1;
4141 break;
4143 case TRUNC_MOD_EXPR:
4144 case FLOOR_MOD_EXPR:
4146 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4147 cop1 = maybe_constant_value (cop1);
4149 if (code0 == INTEGER_TYPE)
4150 doing_div_or_mod = true;
4151 warn_for_div_by_zero (location, cop1);
4154 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4155 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4156 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4157 common = 1;
4158 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4160 /* Although it would be tempting to shorten always here, that loses
4161 on some targets, since the modulo instruction is undefined if the
4162 quotient can't be represented in the computation mode. We shorten
4163 only if unsigned or if dividing by something we know != -1. */
4164 shorten = ((TREE_CODE (op0) == NOP_EXPR
4165 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4166 || (TREE_CODE (op1) == INTEGER_CST
4167 && ! integer_all_onesp (op1)));
4168 common = 1;
4170 break;
4172 case TRUTH_ANDIF_EXPR:
4173 case TRUTH_ORIF_EXPR:
4174 case TRUTH_AND_EXPR:
4175 case TRUTH_OR_EXPR:
4176 result_type = boolean_type_node;
4177 break;
4179 /* Shift operations: result has same type as first operand;
4180 always convert second operand to int.
4181 Also set SHORT_SHIFT if shifting rightward. */
4183 case RSHIFT_EXPR:
4184 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4185 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4187 result_type = type0;
4188 converted = 1;
4190 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4191 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4192 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4193 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4195 result_type = type0;
4196 converted = 1;
4198 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4200 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4201 const_op1 = maybe_constant_value (const_op1);
4202 if (TREE_CODE (const_op1) != INTEGER_CST)
4203 const_op1 = op1;
4204 result_type = type0;
4205 doing_shift = true;
4206 if (TREE_CODE (const_op1) == INTEGER_CST)
4208 if (tree_int_cst_lt (const_op1, integer_zero_node))
4210 if ((complain & tf_warning)
4211 && c_inhibit_evaluation_warnings == 0)
4212 warning (0, "right shift count is negative");
4214 else
4216 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4217 && (complain & tf_warning)
4218 && c_inhibit_evaluation_warnings == 0)
4219 warning (0, "right shift count >= width of type");
4222 /* Convert the shift-count to an integer, regardless of
4223 size of value being shifted. */
4224 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4225 op1 = cp_convert (integer_type_node, op1, complain);
4226 /* Avoid converting op1 to result_type later. */
4227 converted = 1;
4229 break;
4231 case LSHIFT_EXPR:
4232 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4233 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4235 result_type = type0;
4236 converted = 1;
4238 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4239 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4240 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4241 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4243 result_type = type0;
4244 converted = 1;
4246 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4248 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4249 const_op1 = maybe_constant_value (const_op1);
4250 if (TREE_CODE (const_op1) != INTEGER_CST)
4251 const_op1 = op1;
4252 result_type = type0;
4253 doing_shift = true;
4254 if (TREE_CODE (const_op1) == INTEGER_CST)
4256 if (tree_int_cst_lt (const_op1, integer_zero_node))
4258 if ((complain & tf_warning)
4259 && c_inhibit_evaluation_warnings == 0)
4260 warning (0, "left shift count is negative");
4262 else if (compare_tree_int (const_op1,
4263 TYPE_PRECISION (type0)) >= 0)
4265 if ((complain & tf_warning)
4266 && c_inhibit_evaluation_warnings == 0)
4267 warning (0, "left shift count >= width of type");
4270 /* Convert the shift-count to an integer, regardless of
4271 size of value being shifted. */
4272 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4273 op1 = cp_convert (integer_type_node, op1, complain);
4274 /* Avoid converting op1 to result_type later. */
4275 converted = 1;
4277 break;
4279 case RROTATE_EXPR:
4280 case LROTATE_EXPR:
4281 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4283 result_type = type0;
4284 if (TREE_CODE (op1) == INTEGER_CST)
4286 if (tree_int_cst_lt (op1, integer_zero_node))
4288 if (complain & tf_warning)
4289 warning (0, (code == LROTATE_EXPR)
4290 ? G_("left rotate count is negative")
4291 : G_("right rotate count is negative"));
4293 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4295 if (complain & tf_warning)
4296 warning (0, (code == LROTATE_EXPR)
4297 ? G_("left rotate count >= width of type")
4298 : G_("right rotate count >= width of type"));
4301 /* Convert the shift-count to an integer, regardless of
4302 size of value being shifted. */
4303 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4304 op1 = cp_convert (integer_type_node, op1, complain);
4306 break;
4308 case EQ_EXPR:
4309 case NE_EXPR:
4310 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4311 goto vector_compare;
4312 if ((complain & tf_warning)
4313 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4314 warning (OPT_Wfloat_equal,
4315 "comparing floating point with == or != is unsafe");
4316 if ((complain & tf_warning)
4317 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4318 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4319 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4321 build_type = boolean_type_node;
4322 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4323 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4324 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4325 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4326 short_compare = 1;
4327 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4328 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4329 result_type = composite_pointer_type (type0, type1, op0, op1,
4330 CPO_COMPARISON, complain);
4331 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4332 && null_ptr_cst_p (op1))
4334 if (TREE_CODE (op0) == ADDR_EXPR
4335 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4337 if ((complain & tf_warning)
4338 && c_inhibit_evaluation_warnings == 0)
4339 warning (OPT_Waddress, "the address of %qD will never be NULL",
4340 TREE_OPERAND (op0, 0));
4342 result_type = type0;
4344 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4345 && null_ptr_cst_p (op0))
4347 if (TREE_CODE (op1) == ADDR_EXPR
4348 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4350 if ((complain & tf_warning)
4351 && c_inhibit_evaluation_warnings == 0)
4352 warning (OPT_Waddress, "the address of %qD will never be NULL",
4353 TREE_OPERAND (op1, 0));
4355 result_type = type1;
4357 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4358 /* One of the operands must be of nullptr_t type. */
4359 result_type = TREE_TYPE (nullptr_node);
4360 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4362 result_type = type0;
4363 if (complain & tf_error)
4364 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4365 else
4366 return error_mark_node;
4368 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4370 result_type = type1;
4371 if (complain & tf_error)
4372 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4373 else
4374 return error_mark_node;
4376 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4378 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4379 == ptrmemfunc_vbit_in_delta)
4381 tree pfn0, delta0, e1, e2;
4383 if (TREE_SIDE_EFFECTS (op0))
4384 op0 = save_expr (op0);
4386 pfn0 = pfn_from_ptrmemfunc (op0);
4387 delta0 = delta_from_ptrmemfunc (op0);
4388 e1 = cp_build_binary_op (location,
4389 EQ_EXPR,
4390 pfn0,
4391 build_zero_cst (TREE_TYPE (pfn0)),
4392 complain);
4393 e2 = cp_build_binary_op (location,
4394 BIT_AND_EXPR,
4395 delta0,
4396 integer_one_node,
4397 complain);
4399 if (complain & tf_warning)
4400 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4402 e2 = cp_build_binary_op (location,
4403 EQ_EXPR, e2, integer_zero_node,
4404 complain);
4405 op0 = cp_build_binary_op (location,
4406 TRUTH_ANDIF_EXPR, e1, e2,
4407 complain);
4408 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4410 else
4412 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4413 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4415 result_type = TREE_TYPE (op0);
4417 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4418 return cp_build_binary_op (location, code, op1, op0, complain);
4419 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4421 tree type;
4422 /* E will be the final comparison. */
4423 tree e;
4424 /* E1 and E2 are for scratch. */
4425 tree e1;
4426 tree e2;
4427 tree pfn0;
4428 tree pfn1;
4429 tree delta0;
4430 tree delta1;
4432 type = composite_pointer_type (type0, type1, op0, op1,
4433 CPO_COMPARISON, complain);
4435 if (!same_type_p (TREE_TYPE (op0), type))
4436 op0 = cp_convert_and_check (type, op0, complain);
4437 if (!same_type_p (TREE_TYPE (op1), type))
4438 op1 = cp_convert_and_check (type, op1, complain);
4440 if (op0 == error_mark_node || op1 == error_mark_node)
4441 return error_mark_node;
4443 if (TREE_SIDE_EFFECTS (op0))
4444 op0 = save_expr (op0);
4445 if (TREE_SIDE_EFFECTS (op1))
4446 op1 = save_expr (op1);
4448 pfn0 = pfn_from_ptrmemfunc (op0);
4449 pfn1 = pfn_from_ptrmemfunc (op1);
4450 delta0 = delta_from_ptrmemfunc (op0);
4451 delta1 = delta_from_ptrmemfunc (op1);
4452 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4453 == ptrmemfunc_vbit_in_delta)
4455 /* We generate:
4457 (op0.pfn == op1.pfn
4458 && ((op0.delta == op1.delta)
4459 || (!op0.pfn && op0.delta & 1 == 0
4460 && op1.delta & 1 == 0))
4462 The reason for the `!op0.pfn' bit is that a NULL
4463 pointer-to-member is any member with a zero PFN and
4464 LSB of the DELTA field is 0. */
4466 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4467 delta0,
4468 integer_one_node,
4469 complain);
4470 e1 = cp_build_binary_op (location,
4471 EQ_EXPR, e1, integer_zero_node,
4472 complain);
4473 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4474 delta1,
4475 integer_one_node,
4476 complain);
4477 e2 = cp_build_binary_op (location,
4478 EQ_EXPR, e2, integer_zero_node,
4479 complain);
4480 e1 = cp_build_binary_op (location,
4481 TRUTH_ANDIF_EXPR, e2, e1,
4482 complain);
4483 e2 = cp_build_binary_op (location, EQ_EXPR,
4484 pfn0,
4485 build_zero_cst (TREE_TYPE (pfn0)),
4486 complain);
4487 e2 = cp_build_binary_op (location,
4488 TRUTH_ANDIF_EXPR, e2, e1, complain);
4489 e1 = cp_build_binary_op (location,
4490 EQ_EXPR, delta0, delta1, complain);
4491 e1 = cp_build_binary_op (location,
4492 TRUTH_ORIF_EXPR, e1, e2, complain);
4494 else
4496 /* We generate:
4498 (op0.pfn == op1.pfn
4499 && (!op0.pfn || op0.delta == op1.delta))
4501 The reason for the `!op0.pfn' bit is that a NULL
4502 pointer-to-member is any member with a zero PFN; the
4503 DELTA field is unspecified. */
4505 e1 = cp_build_binary_op (location,
4506 EQ_EXPR, delta0, delta1, complain);
4507 e2 = cp_build_binary_op (location,
4508 EQ_EXPR,
4509 pfn0,
4510 build_zero_cst (TREE_TYPE (pfn0)),
4511 complain);
4512 e1 = cp_build_binary_op (location,
4513 TRUTH_ORIF_EXPR, e1, e2, complain);
4515 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4516 e = cp_build_binary_op (location,
4517 TRUTH_ANDIF_EXPR, e2, e1, complain);
4518 if (code == EQ_EXPR)
4519 return e;
4520 return cp_build_binary_op (location,
4521 EQ_EXPR, e, integer_zero_node, complain);
4523 else
4525 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4526 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4527 type1));
4528 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4529 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4530 type0));
4533 break;
4535 case MAX_EXPR:
4536 case MIN_EXPR:
4537 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4538 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4539 shorten = 1;
4540 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4541 result_type = composite_pointer_type (type0, type1, op0, op1,
4542 CPO_COMPARISON, complain);
4543 break;
4545 case LE_EXPR:
4546 case GE_EXPR:
4547 case LT_EXPR:
4548 case GT_EXPR:
4549 if (TREE_CODE (orig_op0) == STRING_CST
4550 || TREE_CODE (orig_op1) == STRING_CST)
4552 if (complain & tf_warning)
4553 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4556 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4558 vector_compare:
4559 tree intt;
4560 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4561 TREE_TYPE (type1))
4562 && !vector_types_compatible_elements_p (type0, type1))
4564 if (complain & tf_error)
4566 error_at (location, "comparing vectors with different "
4567 "element types");
4568 inform (location, "operand types are %qT and %qT",
4569 type0, type1);
4571 return error_mark_node;
4574 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4576 if (complain & tf_error)
4578 error_at (location, "comparing vectors with different "
4579 "number of elements");
4580 inform (location, "operand types are %qT and %qT",
4581 type0, type1);
4583 return error_mark_node;
4586 /* Always construct signed integer vector type. */
4587 intt = c_common_type_for_size (GET_MODE_BITSIZE
4588 (TYPE_MODE (TREE_TYPE (type0))), 0);
4589 if (!intt)
4591 if (complain & tf_error)
4592 error_at (location, "could not find an integer type "
4593 "of the same size as %qT", TREE_TYPE (type0));
4594 return error_mark_node;
4596 result_type = build_opaque_vector_type (intt,
4597 TYPE_VECTOR_SUBPARTS (type0));
4598 converted = 1;
4599 break;
4601 build_type = boolean_type_node;
4602 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4603 || code0 == ENUMERAL_TYPE)
4604 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4605 || code1 == ENUMERAL_TYPE))
4606 short_compare = 1;
4607 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4608 result_type = composite_pointer_type (type0, type1, op0, op1,
4609 CPO_COMPARISON, complain);
4610 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4612 result_type = type0;
4613 if (extra_warnings && (complain & tf_warning))
4614 warning (OPT_Wextra,
4615 "ordered comparison of pointer with integer zero");
4617 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4619 result_type = type1;
4620 if (extra_warnings && (complain & tf_warning))
4621 warning (OPT_Wextra,
4622 "ordered comparison of pointer with integer zero");
4624 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4625 /* One of the operands must be of nullptr_t type. */
4626 result_type = TREE_TYPE (nullptr_node);
4627 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4629 result_type = type0;
4630 if (complain & tf_error)
4631 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4632 else
4633 return error_mark_node;
4635 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4637 result_type = type1;
4638 if (complain & tf_error)
4639 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4640 else
4641 return error_mark_node;
4643 break;
4645 case UNORDERED_EXPR:
4646 case ORDERED_EXPR:
4647 case UNLT_EXPR:
4648 case UNLE_EXPR:
4649 case UNGT_EXPR:
4650 case UNGE_EXPR:
4651 case UNEQ_EXPR:
4652 build_type = integer_type_node;
4653 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4655 if (complain & tf_error)
4656 error ("unordered comparison on non-floating point argument");
4657 return error_mark_node;
4659 common = 1;
4660 break;
4662 default:
4663 break;
4666 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4667 || code0 == ENUMERAL_TYPE)
4668 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4669 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4670 arithmetic_types_p = 1;
4671 else
4673 arithmetic_types_p = 0;
4674 /* Vector arithmetic is only allowed when both sides are vectors. */
4675 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4677 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4678 || !vector_types_compatible_elements_p (type0, type1))
4680 if (complain & tf_error)
4681 binary_op_error (location, code, type0, type1);
4682 return error_mark_node;
4684 arithmetic_types_p = 1;
4687 /* Determine the RESULT_TYPE, if it is not already known. */
4688 if (!result_type
4689 && arithmetic_types_p
4690 && (shorten || common || short_compare))
4692 result_type = cp_common_type (type0, type1);
4693 if (complain & tf_warning)
4694 do_warn_double_promotion (result_type, type0, type1,
4695 "implicit conversion from %qT to %qT "
4696 "to match other operand of binary "
4697 "expression",
4698 location);
4701 if (!result_type)
4703 if (complain & tf_error)
4704 error ("invalid operands of types %qT and %qT to binary %qO",
4705 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4706 return error_mark_node;
4709 /* If we're in a template, the only thing we need to know is the
4710 RESULT_TYPE. */
4711 if (processing_template_decl)
4713 /* Since the middle-end checks the type when doing a build2, we
4714 need to build the tree in pieces. This built tree will never
4715 get out of the front-end as we replace it when instantiating
4716 the template. */
4717 tree tmp = build2 (resultcode,
4718 build_type ? build_type : result_type,
4719 NULL_TREE, op1);
4720 TREE_OPERAND (tmp, 0) = op0;
4721 return tmp;
4724 if (arithmetic_types_p)
4726 bool first_complex = (code0 == COMPLEX_TYPE);
4727 bool second_complex = (code1 == COMPLEX_TYPE);
4728 int none_complex = (!first_complex && !second_complex);
4730 /* Adapted from patch for c/24581. */
4731 if (first_complex != second_complex
4732 && (code == PLUS_EXPR
4733 || code == MINUS_EXPR
4734 || code == MULT_EXPR
4735 || (code == TRUNC_DIV_EXPR && first_complex))
4736 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4737 && flag_signed_zeros)
4739 /* An operation on mixed real/complex operands must be
4740 handled specially, but the language-independent code can
4741 more easily optimize the plain complex arithmetic if
4742 -fno-signed-zeros. */
4743 tree real_type = TREE_TYPE (result_type);
4744 tree real, imag;
4745 if (first_complex)
4747 if (TREE_TYPE (op0) != result_type)
4748 op0 = cp_convert_and_check (result_type, op0, complain);
4749 if (TREE_TYPE (op1) != real_type)
4750 op1 = cp_convert_and_check (real_type, op1, complain);
4752 else
4754 if (TREE_TYPE (op0) != real_type)
4755 op0 = cp_convert_and_check (real_type, op0, complain);
4756 if (TREE_TYPE (op1) != result_type)
4757 op1 = cp_convert_and_check (result_type, op1, complain);
4759 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4760 return error_mark_node;
4761 if (first_complex)
4763 op0 = save_expr (op0);
4764 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4765 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4766 switch (code)
4768 case MULT_EXPR:
4769 case TRUNC_DIV_EXPR:
4770 op1 = save_expr (op1);
4771 imag = build2 (resultcode, real_type, imag, op1);
4772 /* Fall through. */
4773 case PLUS_EXPR:
4774 case MINUS_EXPR:
4775 real = build2 (resultcode, real_type, real, op1);
4776 break;
4777 default:
4778 gcc_unreachable();
4781 else
4783 op1 = save_expr (op1);
4784 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4785 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4786 switch (code)
4788 case MULT_EXPR:
4789 op0 = save_expr (op0);
4790 imag = build2 (resultcode, real_type, op0, imag);
4791 /* Fall through. */
4792 case PLUS_EXPR:
4793 real = build2 (resultcode, real_type, op0, real);
4794 break;
4795 case MINUS_EXPR:
4796 real = build2 (resultcode, real_type, op0, real);
4797 imag = build1 (NEGATE_EXPR, real_type, imag);
4798 break;
4799 default:
4800 gcc_unreachable();
4803 real = fold_if_not_in_template (real);
4804 imag = fold_if_not_in_template (imag);
4805 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4806 result = fold_if_not_in_template (result);
4807 return result;
4810 /* For certain operations (which identify themselves by shorten != 0)
4811 if both args were extended from the same smaller type,
4812 do the arithmetic in that type and then extend.
4814 shorten !=0 and !=1 indicates a bitwise operation.
4815 For them, this optimization is safe only if
4816 both args are zero-extended or both are sign-extended.
4817 Otherwise, we might change the result.
4818 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4819 but calculated in (unsigned short) it would be (unsigned short)-1. */
4821 if (shorten && none_complex)
4823 orig_type = result_type;
4824 final_type = result_type;
4825 result_type = shorten_binary_op (result_type, op0, op1,
4826 shorten == -1);
4829 /* Comparison operations are shortened too but differently.
4830 They identify themselves by setting short_compare = 1. */
4832 if (short_compare)
4834 /* Don't write &op0, etc., because that would prevent op0
4835 from being kept in a register.
4836 Instead, make copies of the our local variables and
4837 pass the copies by reference, then copy them back afterward. */
4838 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4839 enum tree_code xresultcode = resultcode;
4840 tree val
4841 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4842 if (val != 0)
4843 return cp_convert (boolean_type_node, val, complain);
4844 op0 = xop0, op1 = xop1;
4845 converted = 1;
4846 resultcode = xresultcode;
4849 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4850 && warn_sign_compare
4851 /* Do not warn until the template is instantiated; we cannot
4852 bound the ranges of the arguments until that point. */
4853 && !processing_template_decl
4854 && (complain & tf_warning)
4855 && c_inhibit_evaluation_warnings == 0
4856 /* Even unsigned enum types promote to signed int. We don't
4857 want to issue -Wsign-compare warnings for this case. */
4858 && !enum_cast_to_int (orig_op0)
4859 && !enum_cast_to_int (orig_op1))
4861 tree oop0 = maybe_constant_value (orig_op0);
4862 tree oop1 = maybe_constant_value (orig_op1);
4864 if (TREE_CODE (oop0) != INTEGER_CST)
4865 oop0 = orig_op0;
4866 if (TREE_CODE (oop1) != INTEGER_CST)
4867 oop1 = orig_op1;
4868 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4869 result_type, resultcode);
4873 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4874 Then the expression will be built.
4875 It will be given type FINAL_TYPE if that is nonzero;
4876 otherwise, it will be given type RESULT_TYPE. */
4877 if (! converted)
4879 if (TREE_TYPE (op0) != result_type)
4880 op0 = cp_convert_and_check (result_type, op0, complain);
4881 if (TREE_TYPE (op1) != result_type)
4882 op1 = cp_convert_and_check (result_type, op1, complain);
4884 if (op0 == error_mark_node || op1 == error_mark_node)
4885 return error_mark_node;
4888 if (build_type == NULL_TREE)
4889 build_type = result_type;
4891 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
4892 && !processing_template_decl
4893 && current_function_decl != 0
4894 && !lookup_attribute ("no_sanitize_undefined",
4895 DECL_ATTRIBUTES (current_function_decl))
4896 && (doing_div_or_mod || doing_shift))
4898 /* OP0 and/or OP1 might have side-effects. */
4899 op0 = cp_save_expr (op0);
4900 op1 = cp_save_expr (op1);
4901 op0 = maybe_constant_value (fold_non_dependent_expr_sfinae (op0,
4902 tf_none));
4903 op1 = maybe_constant_value (fold_non_dependent_expr_sfinae (op1,
4904 tf_none));
4905 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
4907 /* For diagnostics we want to use the promoted types without
4908 shorten_binary_op. So convert the arguments to the
4909 original result_type. */
4910 tree cop0 = op0;
4911 tree cop1 = op1;
4912 if (orig_type != NULL && result_type != orig_type)
4914 cop0 = cp_convert (orig_type, op0, complain);
4915 cop1 = cp_convert (orig_type, op1, complain);
4917 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
4919 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
4920 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
4923 result = build2 (resultcode, build_type, op0, op1);
4924 result = fold_if_not_in_template (result);
4925 if (final_type != 0)
4926 result = cp_convert (final_type, result, complain);
4928 if (TREE_OVERFLOW_P (result)
4929 && !TREE_OVERFLOW_P (op0)
4930 && !TREE_OVERFLOW_P (op1))
4931 overflow_warning (location, result);
4933 if (instrument_expr != NULL)
4934 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
4935 instrument_expr, result);
4937 return result;
4940 /* Build a VEC_PERM_EXPR.
4941 This is a simple wrapper for c_build_vec_perm_expr. */
4942 tree
4943 build_x_vec_perm_expr (location_t loc,
4944 tree arg0, tree arg1, tree arg2,
4945 tsubst_flags_t complain)
4947 if (processing_template_decl
4948 && (type_dependent_expression_p (arg0)
4949 || type_dependent_expression_p (arg1)
4950 || type_dependent_expression_p (arg2)))
4951 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
4952 return c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
4955 /* Return a tree for the sum or difference (RESULTCODE says which)
4956 of pointer PTROP and integer INTOP. */
4958 static tree
4959 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
4960 tsubst_flags_t complain)
4962 tree res_type = TREE_TYPE (ptrop);
4964 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4965 in certain circumstance (when it's valid to do so). So we need
4966 to make sure it's complete. We don't need to check here, if we
4967 can actually complete it at all, as those checks will be done in
4968 pointer_int_sum() anyway. */
4969 complete_type (TREE_TYPE (res_type));
4971 return pointer_int_sum (input_location, resultcode, ptrop,
4972 fold_if_not_in_template (intop),
4973 complain & tf_warning_or_error);
4976 /* Return a tree for the difference of pointers OP0 and OP1.
4977 The resulting tree has type int. */
4979 static tree
4980 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
4982 tree result;
4983 tree restype = ptrdiff_type_node;
4984 tree target_type = TREE_TYPE (ptrtype);
4986 if (!complete_type_or_else (target_type, NULL_TREE))
4987 return error_mark_node;
4989 if (VOID_TYPE_P (target_type))
4991 if (complain & tf_error)
4992 permerror (input_location, "ISO C++ forbids using pointer of "
4993 "type %<void *%> in subtraction");
4994 else
4995 return error_mark_node;
4997 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4999 if (complain & tf_error)
5000 permerror (input_location, "ISO C++ forbids using pointer to "
5001 "a function in subtraction");
5002 else
5003 return error_mark_node;
5005 if (TREE_CODE (target_type) == METHOD_TYPE)
5007 if (complain & tf_error)
5008 permerror (input_location, "ISO C++ forbids using pointer to "
5009 "a method in subtraction");
5010 else
5011 return error_mark_node;
5014 /* First do the subtraction as integers;
5015 then drop through to build the divide operator. */
5017 op0 = cp_build_binary_op (input_location,
5018 MINUS_EXPR,
5019 cp_convert (restype, op0, complain),
5020 cp_convert (restype, op1, complain),
5021 complain);
5023 /* This generates an error if op1 is a pointer to an incomplete type. */
5024 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5026 if (complain & tf_error)
5027 error ("invalid use of a pointer to an incomplete type in "
5028 "pointer arithmetic");
5029 else
5030 return error_mark_node;
5033 op1 = (TYPE_PTROB_P (ptrtype)
5034 ? size_in_bytes (target_type)
5035 : integer_one_node);
5037 /* Do the division. */
5039 result = build2 (EXACT_DIV_EXPR, restype, op0,
5040 cp_convert (restype, op1, complain));
5041 return fold_if_not_in_template (result);
5044 /* Construct and perhaps optimize a tree representation
5045 for a unary operation. CODE, a tree_code, specifies the operation
5046 and XARG is the operand. */
5048 tree
5049 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5050 tsubst_flags_t complain)
5052 tree orig_expr = xarg;
5053 tree exp;
5054 int ptrmem = 0;
5056 if (processing_template_decl)
5058 if (type_dependent_expression_p (xarg))
5059 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5061 xarg = build_non_dependent_expr (xarg);
5064 exp = NULL_TREE;
5066 /* [expr.unary.op] says:
5068 The address of an object of incomplete type can be taken.
5070 (And is just the ordinary address operator, not an overloaded
5071 "operator &".) However, if the type is a template
5072 specialization, we must complete the type at this point so that
5073 an overloaded "operator &" will be available if required. */
5074 if (code == ADDR_EXPR
5075 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5076 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5077 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5078 || (TREE_CODE (xarg) == OFFSET_REF)))
5079 /* Don't look for a function. */;
5080 else
5081 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5082 NULL_TREE, /*overload=*/NULL, complain);
5083 if (!exp && code == ADDR_EXPR)
5085 if (is_overloaded_fn (xarg))
5087 tree fn = get_first_fn (xarg);
5088 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5090 if (complain & tf_error)
5091 error (DECL_CONSTRUCTOR_P (fn)
5092 ? G_("taking address of constructor %qE")
5093 : G_("taking address of destructor %qE"),
5094 xarg);
5095 return error_mark_node;
5099 /* A pointer to member-function can be formed only by saying
5100 &X::mf. */
5101 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5102 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5104 if (TREE_CODE (xarg) != OFFSET_REF
5105 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5107 if (complain & tf_error)
5109 error ("invalid use of %qE to form a "
5110 "pointer-to-member-function", xarg);
5111 if (TREE_CODE (xarg) != OFFSET_REF)
5112 inform (input_location, " a qualified-id is required");
5114 return error_mark_node;
5116 else
5118 if (complain & tf_error)
5119 error ("parentheses around %qE cannot be used to form a"
5120 " pointer-to-member-function",
5121 xarg);
5122 else
5123 return error_mark_node;
5124 PTRMEM_OK_P (xarg) = 1;
5128 if (TREE_CODE (xarg) == OFFSET_REF)
5130 ptrmem = PTRMEM_OK_P (xarg);
5132 if (!ptrmem && !flag_ms_extensions
5133 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5135 /* A single non-static member, make sure we don't allow a
5136 pointer-to-member. */
5137 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5138 TREE_OPERAND (xarg, 0),
5139 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5140 PTRMEM_OK_P (xarg) = ptrmem;
5144 exp = cp_build_addr_expr_strict (xarg, complain);
5147 if (processing_template_decl && exp != error_mark_node)
5148 exp = build_min_non_dep (code, exp, orig_expr,
5149 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5150 if (TREE_CODE (exp) == ADDR_EXPR)
5151 PTRMEM_OK_P (exp) = ptrmem;
5152 return exp;
5155 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5156 constants, where a null value is represented by an INTEGER_CST of
5157 -1. */
5159 tree
5160 cp_truthvalue_conversion (tree expr)
5162 tree type = TREE_TYPE (expr);
5163 if (TYPE_PTRDATAMEM_P (type))
5164 return build_binary_op (EXPR_LOCATION (expr),
5165 NE_EXPR, expr, nullptr_node, 1);
5166 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5168 /* With -Wzero-as-null-pointer-constant do not warn for an
5169 'if (p)' or a 'while (!p)', where p is a pointer. */
5170 tree ret;
5171 ++c_inhibit_evaluation_warnings;
5172 ret = c_common_truthvalue_conversion (input_location, expr);
5173 --c_inhibit_evaluation_warnings;
5174 return ret;
5176 else
5177 return c_common_truthvalue_conversion (input_location, expr);
5180 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5182 tree
5183 condition_conversion (tree expr)
5185 tree t;
5186 if (processing_template_decl)
5187 return expr;
5188 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5189 tf_warning_or_error, LOOKUP_NORMAL);
5190 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5191 return t;
5194 /* Returns the address of T. This function will fold away
5195 ADDR_EXPR of INDIRECT_REF. */
5197 tree
5198 build_address (tree t)
5200 if (error_operand_p (t) || !cxx_mark_addressable (t))
5201 return error_mark_node;
5202 t = build_fold_addr_expr (t);
5203 if (TREE_CODE (t) != ADDR_EXPR)
5204 t = rvalue (t);
5205 return t;
5208 /* Returns the address of T with type TYPE. */
5210 tree
5211 build_typed_address (tree t, tree type)
5213 if (error_operand_p (t) || !cxx_mark_addressable (t))
5214 return error_mark_node;
5215 t = build_fold_addr_expr_with_type (t, type);
5216 if (TREE_CODE (t) != ADDR_EXPR)
5217 t = rvalue (t);
5218 return t;
5221 /* Return a NOP_EXPR converting EXPR to TYPE. */
5223 tree
5224 build_nop (tree type, tree expr)
5226 if (type == error_mark_node || error_operand_p (expr))
5227 return expr;
5228 return build1 (NOP_EXPR, type, expr);
5231 /* Take the address of ARG, whatever that means under C++ semantics.
5232 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5233 and class rvalues as well.
5235 Nothing should call this function directly; instead, callers should use
5236 cp_build_addr_expr or cp_build_addr_expr_strict. */
5238 static tree
5239 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5241 tree argtype;
5242 tree val;
5244 if (!arg || error_operand_p (arg))
5245 return error_mark_node;
5247 arg = mark_lvalue_use (arg);
5248 argtype = lvalue_type (arg);
5250 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5252 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5253 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5255 /* They're trying to take the address of a unique non-static
5256 member function. This is ill-formed (except in MS-land),
5257 but let's try to DTRT.
5258 Note: We only handle unique functions here because we don't
5259 want to complain if there's a static overload; non-unique
5260 cases will be handled by instantiate_type. But we need to
5261 handle this case here to allow casts on the resulting PMF.
5262 We could defer this in non-MS mode, but it's easier to give
5263 a useful error here. */
5265 /* Inside constant member functions, the `this' pointer
5266 contains an extra const qualifier. TYPE_MAIN_VARIANT
5267 is used here to remove this const from the diagnostics
5268 and the created OFFSET_REF. */
5269 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5270 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5271 mark_used (fn);
5273 if (! flag_ms_extensions)
5275 tree name = DECL_NAME (fn);
5276 if (!(complain & tf_error))
5277 return error_mark_node;
5278 else if (current_class_type
5279 && TREE_OPERAND (arg, 0) == current_class_ref)
5280 /* An expression like &memfn. */
5281 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5282 " or parenthesized non-static member function to form"
5283 " a pointer to member function. Say %<&%T::%D%>",
5284 base, name);
5285 else
5286 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5287 " function to form a pointer to member function."
5288 " Say %<&%T::%D%>",
5289 base, name);
5291 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5294 /* Uninstantiated types are all functions. Taking the
5295 address of a function is a no-op, so just return the
5296 argument. */
5297 if (type_unknown_p (arg))
5298 return build1 (ADDR_EXPR, unknown_type_node, arg);
5300 if (TREE_CODE (arg) == OFFSET_REF)
5301 /* We want a pointer to member; bypass all the code for actually taking
5302 the address of something. */
5303 goto offset_ref;
5305 /* Anything not already handled and not a true memory reference
5306 is an error. */
5307 if (TREE_CODE (argtype) != FUNCTION_TYPE
5308 && TREE_CODE (argtype) != METHOD_TYPE)
5310 cp_lvalue_kind kind = lvalue_kind (arg);
5311 if (kind == clk_none)
5313 if (complain & tf_error)
5314 lvalue_error (input_location, lv_addressof);
5315 return error_mark_node;
5317 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5319 if (!(complain & tf_error))
5320 return error_mark_node;
5321 if (kind & clk_class)
5322 /* Make this a permerror because we used to accept it. */
5323 permerror (input_location, "taking address of temporary");
5324 else
5325 error ("taking address of xvalue (rvalue reference)");
5329 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5331 tree type = build_pointer_type (TREE_TYPE (argtype));
5332 arg = build1 (CONVERT_EXPR, type, arg);
5333 return arg;
5335 else if (pedantic && DECL_MAIN_P (arg))
5337 /* ARM $3.4 */
5338 /* Apparently a lot of autoconf scripts for C++ packages do this,
5339 so only complain if -Wpedantic. */
5340 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5341 pedwarn (input_location, OPT_Wpedantic,
5342 "ISO C++ forbids taking address of function %<::main%>");
5343 else if (flag_pedantic_errors)
5344 return error_mark_node;
5347 /* Let &* cancel out to simplify resulting code. */
5348 if (INDIRECT_REF_P (arg))
5350 /* We don't need to have `current_class_ptr' wrapped in a
5351 NON_LVALUE_EXPR node. */
5352 if (arg == current_class_ref)
5353 return current_class_ptr;
5355 arg = TREE_OPERAND (arg, 0);
5356 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5358 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5359 arg = build1 (CONVERT_EXPR, type, arg);
5361 else
5362 /* Don't let this be an lvalue. */
5363 arg = rvalue (arg);
5364 return arg;
5367 /* ??? Cope with user tricks that amount to offsetof. */
5368 if (TREE_CODE (argtype) != FUNCTION_TYPE
5369 && TREE_CODE (argtype) != METHOD_TYPE
5370 && argtype != unknown_type_node
5371 && (val = get_base_address (arg))
5372 && COMPLETE_TYPE_P (TREE_TYPE (val))
5373 && INDIRECT_REF_P (val)
5374 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5376 tree type = build_pointer_type (argtype);
5377 return fold_convert (type, fold_offsetof_1 (arg));
5380 /* Handle complex lvalues (when permitted)
5381 by reduction to simpler cases. */
5382 val = unary_complex_lvalue (ADDR_EXPR, arg);
5383 if (val != 0)
5384 return val;
5386 switch (TREE_CODE (arg))
5388 CASE_CONVERT:
5389 case FLOAT_EXPR:
5390 case FIX_TRUNC_EXPR:
5391 /* Even if we're not being pedantic, we cannot allow this
5392 extension when we're instantiating in a SFINAE
5393 context. */
5394 if (! lvalue_p (arg) && complain == tf_none)
5396 if (complain & tf_error)
5397 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5398 else
5399 return error_mark_node;
5401 break;
5403 case BASELINK:
5404 arg = BASELINK_FUNCTIONS (arg);
5405 /* Fall through. */
5407 case OVERLOAD:
5408 arg = OVL_CURRENT (arg);
5409 break;
5411 case OFFSET_REF:
5412 offset_ref:
5413 /* Turn a reference to a non-static data member into a
5414 pointer-to-member. */
5416 tree type;
5417 tree t;
5419 gcc_assert (PTRMEM_OK_P (arg));
5421 t = TREE_OPERAND (arg, 1);
5422 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5424 if (complain & tf_error)
5425 error ("cannot create pointer to reference member %qD", t);
5426 return error_mark_node;
5429 type = build_ptrmem_type (context_for_name_lookup (t),
5430 TREE_TYPE (t));
5431 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5432 return t;
5435 default:
5436 break;
5439 if (argtype != error_mark_node)
5441 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (argtype))
5443 if (complain & tf_warning_or_error)
5444 pedwarn (input_location, OPT_Wvla,
5445 "taking address of array of runtime bound");
5446 else
5447 return error_mark_node;
5449 argtype = build_pointer_type (argtype);
5452 /* In a template, we are processing a non-dependent expression
5453 so we can just form an ADDR_EXPR with the correct type. */
5454 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5456 val = build_address (arg);
5457 if (TREE_CODE (arg) == OFFSET_REF)
5458 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5460 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5462 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5464 /* We can only get here with a single static member
5465 function. */
5466 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5467 && DECL_STATIC_FUNCTION_P (fn));
5468 mark_used (fn);
5469 val = build_address (fn);
5470 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5471 /* Do not lose object's side effects. */
5472 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5473 TREE_OPERAND (arg, 0), val);
5475 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5477 if (complain & tf_error)
5478 error ("attempt to take address of bit-field structure member %qD",
5479 TREE_OPERAND (arg, 1));
5480 return error_mark_node;
5482 else
5484 tree object = TREE_OPERAND (arg, 0);
5485 tree field = TREE_OPERAND (arg, 1);
5486 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5487 (TREE_TYPE (object), decl_type_context (field)));
5488 val = build_address (arg);
5491 if (TYPE_PTR_P (argtype)
5492 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5494 build_ptrmemfunc_type (argtype);
5495 val = build_ptrmemfunc (argtype, val, 0,
5496 /*c_cast_p=*/false,
5497 complain);
5500 return val;
5503 /* Take the address of ARG if it has one, even if it's an rvalue. */
5505 tree
5506 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5508 return cp_build_addr_expr_1 (arg, 0, complain);
5511 /* Take the address of ARG, but only if it's an lvalue. */
5513 tree
5514 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5516 return cp_build_addr_expr_1 (arg, 1, complain);
5519 /* C++: Must handle pointers to members.
5521 Perhaps type instantiation should be extended to handle conversion
5522 from aggregates to types we don't yet know we want? (Or are those
5523 cases typically errors which should be reported?)
5525 NOCONVERT nonzero suppresses the default promotions
5526 (such as from short to int). */
5528 tree
5529 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5530 tsubst_flags_t complain)
5532 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5533 tree arg = xarg;
5534 tree argtype = 0;
5535 const char *errstring = NULL;
5536 tree val;
5537 const char *invalid_op_diag;
5539 if (!arg || error_operand_p (arg))
5540 return error_mark_node;
5542 if ((invalid_op_diag
5543 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5544 ? CONVERT_EXPR
5545 : code),
5546 TREE_TYPE (xarg))))
5548 if (complain & tf_error)
5549 error (invalid_op_diag);
5550 return error_mark_node;
5553 switch (code)
5555 case UNARY_PLUS_EXPR:
5556 case NEGATE_EXPR:
5558 int flags = WANT_ARITH | WANT_ENUM;
5559 /* Unary plus (but not unary minus) is allowed on pointers. */
5560 if (code == UNARY_PLUS_EXPR)
5561 flags |= WANT_POINTER;
5562 arg = build_expr_type_conversion (flags, arg, true);
5563 if (!arg)
5564 errstring = (code == NEGATE_EXPR
5565 ? _("wrong type argument to unary minus")
5566 : _("wrong type argument to unary plus"));
5567 else
5569 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5570 arg = cp_perform_integral_promotions (arg, complain);
5572 /* Make sure the result is not an lvalue: a unary plus or minus
5573 expression is always a rvalue. */
5574 arg = rvalue (arg);
5577 break;
5579 case BIT_NOT_EXPR:
5580 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5582 code = CONJ_EXPR;
5583 if (!noconvert)
5585 arg = cp_default_conversion (arg, complain);
5586 if (arg == error_mark_node)
5587 return error_mark_node;
5590 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5591 | WANT_VECTOR_OR_COMPLEX,
5592 arg, true)))
5593 errstring = _("wrong type argument to bit-complement");
5594 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5595 arg = cp_perform_integral_promotions (arg, complain);
5596 break;
5598 case ABS_EXPR:
5599 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5600 errstring = _("wrong type argument to abs");
5601 else if (!noconvert)
5603 arg = cp_default_conversion (arg, complain);
5604 if (arg == error_mark_node)
5605 return error_mark_node;
5607 break;
5609 case CONJ_EXPR:
5610 /* Conjugating a real value is a no-op, but allow it anyway. */
5611 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5612 errstring = _("wrong type argument to conjugation");
5613 else if (!noconvert)
5615 arg = cp_default_conversion (arg, complain);
5616 if (arg == error_mark_node)
5617 return error_mark_node;
5619 break;
5621 case TRUTH_NOT_EXPR:
5622 arg = perform_implicit_conversion (boolean_type_node, arg,
5623 complain);
5624 val = invert_truthvalue_loc (input_location, arg);
5625 if (arg != error_mark_node)
5626 return val;
5627 errstring = _("in argument to unary !");
5628 break;
5630 case NOP_EXPR:
5631 break;
5633 case REALPART_EXPR:
5634 case IMAGPART_EXPR:
5635 arg = build_real_imag_expr (input_location, code, arg);
5636 if (arg == error_mark_node)
5637 return arg;
5638 else
5639 return fold_if_not_in_template (arg);
5641 case PREINCREMENT_EXPR:
5642 case POSTINCREMENT_EXPR:
5643 case PREDECREMENT_EXPR:
5644 case POSTDECREMENT_EXPR:
5645 /* Handle complex lvalues (when permitted)
5646 by reduction to simpler cases. */
5648 val = unary_complex_lvalue (code, arg);
5649 if (val != 0)
5650 return val;
5652 arg = mark_lvalue_use (arg);
5654 /* Increment or decrement the real part of the value,
5655 and don't change the imaginary part. */
5656 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5658 tree real, imag;
5660 arg = stabilize_reference (arg);
5661 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5662 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5663 real = cp_build_unary_op (code, real, 1, complain);
5664 if (real == error_mark_node || imag == error_mark_node)
5665 return error_mark_node;
5666 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5667 real, imag);
5670 /* Report invalid types. */
5672 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5673 arg, true)))
5675 if (code == PREINCREMENT_EXPR)
5676 errstring = _("no pre-increment operator for type");
5677 else if (code == POSTINCREMENT_EXPR)
5678 errstring = _("no post-increment operator for type");
5679 else if (code == PREDECREMENT_EXPR)
5680 errstring = _("no pre-decrement operator for type");
5681 else
5682 errstring = _("no post-decrement operator for type");
5683 break;
5685 else if (arg == error_mark_node)
5686 return error_mark_node;
5688 /* Report something read-only. */
5690 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5691 || TREE_READONLY (arg))
5693 if (complain & tf_error)
5694 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5695 || code == POSTINCREMENT_EXPR)
5696 ? lv_increment : lv_decrement));
5697 else
5698 return error_mark_node;
5702 tree inc;
5703 tree declared_type = unlowered_expr_type (arg);
5705 argtype = TREE_TYPE (arg);
5707 /* ARM $5.2.5 last annotation says this should be forbidden. */
5708 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5710 if (complain & tf_error)
5711 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5712 ? G_("ISO C++ forbids incrementing an enum")
5713 : G_("ISO C++ forbids decrementing an enum"));
5714 else
5715 return error_mark_node;
5718 /* Compute the increment. */
5720 if (TYPE_PTR_P (argtype))
5722 tree type = complete_type (TREE_TYPE (argtype));
5724 if (!COMPLETE_OR_VOID_TYPE_P (type))
5726 if (complain & tf_error)
5727 error (((code == PREINCREMENT_EXPR
5728 || code == POSTINCREMENT_EXPR))
5729 ? G_("cannot increment a pointer to incomplete type %qT")
5730 : G_("cannot decrement a pointer to incomplete type %qT"),
5731 TREE_TYPE (argtype));
5732 else
5733 return error_mark_node;
5735 else if (!TYPE_PTROB_P (argtype))
5737 if (complain & tf_error)
5738 pedwarn (input_location, OPT_Wpointer_arith,
5739 (code == PREINCREMENT_EXPR
5740 || code == POSTINCREMENT_EXPR)
5741 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5742 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5743 argtype);
5744 else
5745 return error_mark_node;
5748 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5750 else
5751 inc = VECTOR_TYPE_P (argtype)
5752 ? build_one_cst (argtype)
5753 : integer_one_node;
5755 inc = cp_convert (argtype, inc, complain);
5757 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5758 need to ask Objective-C to build the increment or decrement
5759 expression for it. */
5760 if (objc_is_property_ref (arg))
5761 return objc_build_incr_expr_for_property_ref (input_location, code,
5762 arg, inc);
5764 /* Complain about anything else that is not a true lvalue. */
5765 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5766 || code == POSTINCREMENT_EXPR)
5767 ? lv_increment : lv_decrement),
5768 complain))
5769 return error_mark_node;
5771 /* Forbid using -- on `bool'. */
5772 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5774 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5776 if (complain & tf_error)
5777 error ("invalid use of Boolean expression as operand "
5778 "to %<operator--%>");
5779 return error_mark_node;
5781 val = boolean_increment (code, arg);
5783 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5784 /* An rvalue has no cv-qualifiers. */
5785 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5786 else
5787 val = build2 (code, TREE_TYPE (arg), arg, inc);
5789 TREE_SIDE_EFFECTS (val) = 1;
5790 return val;
5793 case ADDR_EXPR:
5794 /* Note that this operation never does default_conversion
5795 regardless of NOCONVERT. */
5796 return cp_build_addr_expr (arg, complain);
5798 default:
5799 break;
5802 if (!errstring)
5804 if (argtype == 0)
5805 argtype = TREE_TYPE (arg);
5806 return fold_if_not_in_template (build1 (code, argtype, arg));
5809 if (complain & tf_error)
5810 error ("%s", errstring);
5811 return error_mark_node;
5814 /* Hook for the c-common bits that build a unary op. */
5815 tree
5816 build_unary_op (location_t /*location*/,
5817 enum tree_code code, tree xarg, int noconvert)
5819 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5822 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5823 for certain kinds of expressions which are not really lvalues
5824 but which we can accept as lvalues.
5826 If ARG is not a kind of expression we can handle, return
5827 NULL_TREE. */
5829 tree
5830 unary_complex_lvalue (enum tree_code code, tree arg)
5832 /* Inside a template, making these kinds of adjustments is
5833 pointless; we are only concerned with the type of the
5834 expression. */
5835 if (processing_template_decl)
5836 return NULL_TREE;
5838 /* Handle (a, b) used as an "lvalue". */
5839 if (TREE_CODE (arg) == COMPOUND_EXPR)
5841 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5842 tf_warning_or_error);
5843 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5844 TREE_OPERAND (arg, 0), real_result);
5847 /* Handle (a ? b : c) used as an "lvalue". */
5848 if (TREE_CODE (arg) == COND_EXPR
5849 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5850 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5852 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5853 if (TREE_CODE (arg) == MODIFY_EXPR
5854 || TREE_CODE (arg) == PREINCREMENT_EXPR
5855 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5857 tree lvalue = TREE_OPERAND (arg, 0);
5858 if (TREE_SIDE_EFFECTS (lvalue))
5860 lvalue = stabilize_reference (lvalue);
5861 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5862 lvalue, TREE_OPERAND (arg, 1));
5864 return unary_complex_lvalue
5865 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5868 if (code != ADDR_EXPR)
5869 return NULL_TREE;
5871 /* Handle (a = b) used as an "lvalue" for `&'. */
5872 if (TREE_CODE (arg) == MODIFY_EXPR
5873 || TREE_CODE (arg) == INIT_EXPR)
5875 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5876 tf_warning_or_error);
5877 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5878 arg, real_result);
5879 TREE_NO_WARNING (arg) = 1;
5880 return arg;
5883 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5884 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5885 || TREE_CODE (arg) == OFFSET_REF)
5886 return NULL_TREE;
5888 /* We permit compiler to make function calls returning
5889 objects of aggregate type look like lvalues. */
5891 tree targ = arg;
5893 if (TREE_CODE (targ) == SAVE_EXPR)
5894 targ = TREE_OPERAND (targ, 0);
5896 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5898 if (TREE_CODE (arg) == SAVE_EXPR)
5899 targ = arg;
5900 else
5901 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5902 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5905 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
5906 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5907 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5910 /* Don't let anything else be handled specially. */
5911 return NULL_TREE;
5914 /* Mark EXP saying that we need to be able to take the
5915 address of it; it should not be allocated in a register.
5916 Value is true if successful.
5918 C++: we do not allow `current_class_ptr' to be addressable. */
5920 bool
5921 cxx_mark_addressable (tree exp)
5923 tree x = exp;
5925 while (1)
5926 switch (TREE_CODE (x))
5928 case ADDR_EXPR:
5929 case COMPONENT_REF:
5930 case ARRAY_REF:
5931 case REALPART_EXPR:
5932 case IMAGPART_EXPR:
5933 x = TREE_OPERAND (x, 0);
5934 break;
5936 case PARM_DECL:
5937 if (x == current_class_ptr)
5939 error ("cannot take the address of %<this%>, which is an rvalue expression");
5940 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5941 return true;
5943 /* Fall through. */
5945 case VAR_DECL:
5946 /* Caller should not be trying to mark initialized
5947 constant fields addressable. */
5948 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5949 || DECL_IN_AGGR_P (x) == 0
5950 || TREE_STATIC (x)
5951 || DECL_EXTERNAL (x));
5952 /* Fall through. */
5954 case RESULT_DECL:
5955 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5956 && !DECL_ARTIFICIAL (x))
5958 if (VAR_P (x) && DECL_HARD_REGISTER (x))
5960 error
5961 ("address of explicit register variable %qD requested", x);
5962 return false;
5964 else if (extra_warnings)
5965 warning
5966 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5968 TREE_ADDRESSABLE (x) = 1;
5969 return true;
5971 case CONST_DECL:
5972 case FUNCTION_DECL:
5973 TREE_ADDRESSABLE (x) = 1;
5974 return true;
5976 case CONSTRUCTOR:
5977 TREE_ADDRESSABLE (x) = 1;
5978 return true;
5980 case TARGET_EXPR:
5981 TREE_ADDRESSABLE (x) = 1;
5982 cxx_mark_addressable (TREE_OPERAND (x, 0));
5983 return true;
5985 default:
5986 return true;
5990 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5992 tree
5993 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
5994 tsubst_flags_t complain)
5996 tree orig_ifexp = ifexp;
5997 tree orig_op1 = op1;
5998 tree orig_op2 = op2;
5999 tree expr;
6001 if (processing_template_decl)
6003 /* The standard says that the expression is type-dependent if
6004 IFEXP is type-dependent, even though the eventual type of the
6005 expression doesn't dependent on IFEXP. */
6006 if (type_dependent_expression_p (ifexp)
6007 /* As a GNU extension, the middle operand may be omitted. */
6008 || (op1 && type_dependent_expression_p (op1))
6009 || type_dependent_expression_p (op2))
6010 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6011 ifexp = build_non_dependent_expr (ifexp);
6012 if (op1)
6013 op1 = build_non_dependent_expr (op1);
6014 op2 = build_non_dependent_expr (op2);
6017 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6018 if (processing_template_decl && expr != error_mark_node
6019 && TREE_CODE (expr) != VEC_COND_EXPR)
6021 tree min = build_min_non_dep (COND_EXPR, expr,
6022 orig_ifexp, orig_op1, orig_op2);
6023 /* In C++11, remember that the result is an lvalue or xvalue.
6024 In C++98, lvalue_kind can just assume lvalue in a template. */
6025 if (cxx_dialect >= cxx11
6026 && lvalue_or_rvalue_with_address_p (expr)
6027 && !lvalue_or_rvalue_with_address_p (min))
6028 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6029 !real_lvalue_p (expr));
6030 expr = convert_from_reference (min);
6032 return expr;
6035 /* Given a list of expressions, return a compound expression
6036 that performs them all and returns the value of the last of them. */
6038 tree
6039 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6040 tsubst_flags_t complain)
6042 tree expr = TREE_VALUE (list);
6044 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6045 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6047 if (complain & tf_error)
6048 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6049 "list-initializer for non-class type must not "
6050 "be parenthesized");
6051 else
6052 return error_mark_node;
6055 if (TREE_CHAIN (list))
6057 if (complain & tf_error)
6058 switch (exp)
6060 case ELK_INIT:
6061 permerror (input_location, "expression list treated as compound "
6062 "expression in initializer");
6063 break;
6064 case ELK_MEM_INIT:
6065 permerror (input_location, "expression list treated as compound "
6066 "expression in mem-initializer");
6067 break;
6068 case ELK_FUNC_CAST:
6069 permerror (input_location, "expression list treated as compound "
6070 "expression in functional cast");
6071 break;
6072 default:
6073 gcc_unreachable ();
6075 else
6076 return error_mark_node;
6078 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6079 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6080 expr, TREE_VALUE (list), complain);
6083 return expr;
6086 /* Like build_x_compound_expr_from_list, but using a VEC. */
6088 tree
6089 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6090 tsubst_flags_t complain)
6092 if (vec_safe_is_empty (vec))
6093 return NULL_TREE;
6094 else if (vec->length () == 1)
6095 return (*vec)[0];
6096 else
6098 tree expr;
6099 unsigned int ix;
6100 tree t;
6102 if (msg != NULL)
6104 if (complain & tf_error)
6105 permerror (input_location,
6106 "%s expression list treated as compound expression",
6107 msg);
6108 else
6109 return error_mark_node;
6112 expr = (*vec)[0];
6113 for (ix = 1; vec->iterate (ix, &t); ++ix)
6114 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6115 t, complain);
6117 return expr;
6121 /* Handle overloading of the ',' operator when needed. */
6123 tree
6124 build_x_compound_expr (location_t loc, tree op1, tree op2,
6125 tsubst_flags_t complain)
6127 tree result;
6128 tree orig_op1 = op1;
6129 tree orig_op2 = op2;
6131 if (processing_template_decl)
6133 if (type_dependent_expression_p (op1)
6134 || type_dependent_expression_p (op2))
6135 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6136 op1 = build_non_dependent_expr (op1);
6137 op2 = build_non_dependent_expr (op2);
6140 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6141 NULL_TREE, /*overload=*/NULL, complain);
6142 if (!result)
6143 result = cp_build_compound_expr (op1, op2, complain);
6145 if (processing_template_decl && result != error_mark_node)
6146 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6148 return result;
6151 /* Like cp_build_compound_expr, but for the c-common bits. */
6153 tree
6154 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6156 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6159 /* Build a compound expression. */
6161 tree
6162 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6164 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6166 if (lhs == error_mark_node || rhs == error_mark_node)
6167 return error_mark_node;
6169 if (flag_enable_cilkplus
6170 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6171 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6173 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6174 : EXPR_LOCATION (rhs));
6175 error_at (loc,
6176 "spawned function call cannot be part of a comma expression");
6177 return error_mark_node;
6180 if (TREE_CODE (rhs) == TARGET_EXPR)
6182 /* If the rhs is a TARGET_EXPR, then build the compound
6183 expression inside the target_expr's initializer. This
6184 helps the compiler to eliminate unnecessary temporaries. */
6185 tree init = TREE_OPERAND (rhs, 1);
6187 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6188 TREE_OPERAND (rhs, 1) = init;
6190 return rhs;
6193 if (type_unknown_p (rhs))
6195 if (complain & tf_error)
6196 error ("no context to resolve type of %qE", rhs);
6197 return error_mark_node;
6200 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6203 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6204 casts away constness. CAST gives the type of cast. Returns true
6205 if the cast is ill-formed, false if it is well-formed.
6207 ??? This function warns for casting away any qualifier not just
6208 const. We would like to specify exactly what qualifiers are casted
6209 away.
6212 static bool
6213 check_for_casting_away_constness (tree src_type, tree dest_type,
6214 enum tree_code cast, tsubst_flags_t complain)
6216 /* C-style casts are allowed to cast away constness. With
6217 WARN_CAST_QUAL, we still want to issue a warning. */
6218 if (cast == CAST_EXPR && !warn_cast_qual)
6219 return false;
6221 if (!casts_away_constness (src_type, dest_type, complain))
6222 return false;
6224 switch (cast)
6226 case CAST_EXPR:
6227 if (complain & tf_warning)
6228 warning (OPT_Wcast_qual,
6229 "cast from type %qT to type %qT casts away qualifiers",
6230 src_type, dest_type);
6231 return false;
6233 case STATIC_CAST_EXPR:
6234 if (complain & tf_error)
6235 error ("static_cast from type %qT to type %qT casts away qualifiers",
6236 src_type, dest_type);
6237 return true;
6239 case REINTERPRET_CAST_EXPR:
6240 if (complain & tf_error)
6241 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6242 src_type, dest_type);
6243 return true;
6245 default:
6246 gcc_unreachable();
6251 Warns if the cast from expression EXPR to type TYPE is useless.
6253 void
6254 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6256 if (warn_useless_cast
6257 && complain & tf_warning
6258 && c_inhibit_evaluation_warnings == 0)
6260 if (REFERENCE_REF_P (expr))
6261 expr = TREE_OPERAND (expr, 0);
6263 if ((TREE_CODE (type) == REFERENCE_TYPE
6264 && (TYPE_REF_IS_RVALUE (type)
6265 ? xvalue_p (expr) : real_lvalue_p (expr))
6266 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6267 || same_type_p (TREE_TYPE (expr), type))
6268 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6272 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6273 (another pointer-to-member type in the same hierarchy) and return
6274 the converted expression. If ALLOW_INVERSE_P is permitted, a
6275 pointer-to-derived may be converted to pointer-to-base; otherwise,
6276 only the other direction is permitted. If C_CAST_P is true, this
6277 conversion is taking place as part of a C-style cast. */
6279 tree
6280 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6281 bool c_cast_p, tsubst_flags_t complain)
6283 if (TYPE_PTRDATAMEM_P (type))
6285 tree delta;
6287 if (TREE_CODE (expr) == PTRMEM_CST)
6288 expr = cplus_expand_constant (expr);
6289 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6290 TYPE_PTRMEM_CLASS_TYPE (type),
6291 allow_inverse_p,
6292 c_cast_p, complain);
6293 if (delta == error_mark_node)
6294 return error_mark_node;
6296 if (!integer_zerop (delta))
6298 tree cond, op1, op2;
6300 cond = cp_build_binary_op (input_location,
6301 EQ_EXPR,
6302 expr,
6303 build_int_cst (TREE_TYPE (expr), -1),
6304 complain);
6305 op1 = build_nop (ptrdiff_type_node, expr);
6306 op2 = cp_build_binary_op (input_location,
6307 PLUS_EXPR, op1, delta,
6308 complain);
6310 expr = fold_build3_loc (input_location,
6311 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6315 return build_nop (type, expr);
6317 else
6318 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6319 allow_inverse_p, c_cast_p, complain);
6322 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6323 this static_cast is being attempted as one of the possible casts
6324 allowed by a C-style cast. (In that case, accessibility of base
6325 classes is not considered, and it is OK to cast away
6326 constness.) Return the result of the cast. *VALID_P is set to
6327 indicate whether or not the cast was valid. */
6329 static tree
6330 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6331 bool *valid_p, tsubst_flags_t complain)
6333 tree intype;
6334 tree result;
6335 cp_lvalue_kind clk;
6337 /* Assume the cast is valid. */
6338 *valid_p = true;
6340 intype = unlowered_expr_type (expr);
6342 /* Save casted types in the function's used types hash table. */
6343 used_types_insert (type);
6345 /* [expr.static.cast]
6347 An lvalue of type "cv1 B", where B is a class type, can be cast
6348 to type "reference to cv2 D", where D is a class derived (clause
6349 _class.derived_) from B, if a valid standard conversion from
6350 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6351 same cv-qualification as, or greater cv-qualification than, cv1,
6352 and B is not a virtual base class of D. */
6353 /* We check this case before checking the validity of "TYPE t =
6354 EXPR;" below because for this case:
6356 struct B {};
6357 struct D : public B { D(const B&); };
6358 extern B& b;
6359 void f() { static_cast<const D&>(b); }
6361 we want to avoid constructing a new D. The standard is not
6362 completely clear about this issue, but our interpretation is
6363 consistent with other compilers. */
6364 if (TREE_CODE (type) == REFERENCE_TYPE
6365 && CLASS_TYPE_P (TREE_TYPE (type))
6366 && CLASS_TYPE_P (intype)
6367 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6368 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6369 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6370 build_pointer_type (TYPE_MAIN_VARIANT
6371 (TREE_TYPE (type))),
6372 complain)
6373 && (c_cast_p
6374 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6376 tree base;
6378 /* There is a standard conversion from "D*" to "B*" even if "B"
6379 is ambiguous or inaccessible. If this is really a
6380 static_cast, then we check both for inaccessibility and
6381 ambiguity. However, if this is a static_cast being performed
6382 because the user wrote a C-style cast, then accessibility is
6383 not considered. */
6384 base = lookup_base (TREE_TYPE (type), intype,
6385 c_cast_p ? ba_unique : ba_check,
6386 NULL, complain);
6388 /* Convert from "B*" to "D*". This function will check that "B"
6389 is not a virtual base of "D". */
6390 expr = build_base_path (MINUS_EXPR, build_address (expr),
6391 base, /*nonnull=*/false, complain);
6392 /* Convert the pointer to a reference -- but then remember that
6393 there are no expressions with reference type in C++.
6395 We call rvalue so that there's an actual tree code
6396 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6397 is a variable with the same type, the conversion would get folded
6398 away, leaving just the variable and causing lvalue_kind to give
6399 the wrong answer. */
6400 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6403 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6404 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6405 if (TREE_CODE (type) == REFERENCE_TYPE
6406 && TYPE_REF_IS_RVALUE (type)
6407 && (clk = real_lvalue_p (expr))
6408 && reference_related_p (TREE_TYPE (type), intype)
6409 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6411 if (clk == clk_ordinary)
6413 /* Handle the (non-bit-field) lvalue case here by casting to
6414 lvalue reference and then changing it to an rvalue reference.
6415 Casting an xvalue to rvalue reference will be handled by the
6416 main code path. */
6417 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6418 result = (perform_direct_initialization_if_possible
6419 (lref, expr, c_cast_p, complain));
6420 result = cp_fold_convert (type, result);
6421 /* Make sure we don't fold back down to a named rvalue reference,
6422 because that would be an lvalue. */
6423 if (DECL_P (result))
6424 result = build1 (NON_LVALUE_EXPR, type, result);
6425 return convert_from_reference (result);
6427 else
6428 /* For a bit-field or packed field, bind to a temporary. */
6429 expr = rvalue (expr);
6432 /* Resolve overloaded address here rather than once in
6433 implicit_conversion and again in the inverse code below. */
6434 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6436 expr = instantiate_type (type, expr, complain);
6437 intype = TREE_TYPE (expr);
6440 /* [expr.static.cast]
6442 Any expression can be explicitly converted to type cv void. */
6443 if (VOID_TYPE_P (type))
6444 return convert_to_void (expr, ICV_CAST, complain);
6446 /* [class.abstract]
6447 An abstract class shall not be used ... as the type of an explicit
6448 conversion. */
6449 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6450 return error_mark_node;
6452 /* [expr.static.cast]
6454 An expression e can be explicitly converted to a type T using a
6455 static_cast of the form static_cast<T>(e) if the declaration T
6456 t(e);" is well-formed, for some invented temporary variable
6457 t. */
6458 result = perform_direct_initialization_if_possible (type, expr,
6459 c_cast_p, complain);
6460 if (result)
6462 result = convert_from_reference (result);
6464 /* [expr.static.cast]
6466 If T is a reference type, the result is an lvalue; otherwise,
6467 the result is an rvalue. */
6468 if (TREE_CODE (type) != REFERENCE_TYPE)
6469 result = rvalue (result);
6470 return result;
6473 /* [expr.static.cast]
6475 The inverse of any standard conversion sequence (clause _conv_),
6476 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6477 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6478 (_conv.bool_) conversions, can be performed explicitly using
6479 static_cast subject to the restriction that the explicit
6480 conversion does not cast away constness (_expr.const.cast_), and
6481 the following additional rules for specific cases: */
6482 /* For reference, the conversions not excluded are: integral
6483 promotions, floating point promotion, integral conversions,
6484 floating point conversions, floating-integral conversions,
6485 pointer conversions, and pointer to member conversions. */
6486 /* DR 128
6488 A value of integral _or enumeration_ type can be explicitly
6489 converted to an enumeration type. */
6490 /* The effect of all that is that any conversion between any two
6491 types which are integral, floating, or enumeration types can be
6492 performed. */
6493 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6494 || SCALAR_FLOAT_TYPE_P (type))
6495 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6496 || SCALAR_FLOAT_TYPE_P (intype)))
6497 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6499 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6500 && CLASS_TYPE_P (TREE_TYPE (type))
6501 && CLASS_TYPE_P (TREE_TYPE (intype))
6502 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6503 (TREE_TYPE (intype))),
6504 build_pointer_type (TYPE_MAIN_VARIANT
6505 (TREE_TYPE (type))),
6506 complain))
6508 tree base;
6510 if (!c_cast_p
6511 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6512 complain))
6513 return error_mark_node;
6514 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6515 c_cast_p ? ba_unique : ba_check,
6516 NULL, complain);
6517 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6518 complain);
6519 return cp_fold_convert(type, expr);
6522 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6523 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6525 tree c1;
6526 tree c2;
6527 tree t1;
6528 tree t2;
6530 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6531 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6533 if (TYPE_PTRDATAMEM_P (type))
6535 t1 = (build_ptrmem_type
6536 (c1,
6537 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6538 t2 = (build_ptrmem_type
6539 (c2,
6540 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6542 else
6544 t1 = intype;
6545 t2 = type;
6547 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6549 if (!c_cast_p
6550 && check_for_casting_away_constness (intype, type,
6551 STATIC_CAST_EXPR,
6552 complain))
6553 return error_mark_node;
6554 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6555 c_cast_p, complain);
6559 /* [expr.static.cast]
6561 An rvalue of type "pointer to cv void" can be explicitly
6562 converted to a pointer to object type. A value of type pointer
6563 to object converted to "pointer to cv void" and back to the
6564 original pointer type will have its original value. */
6565 if (TYPE_PTR_P (intype)
6566 && VOID_TYPE_P (TREE_TYPE (intype))
6567 && TYPE_PTROB_P (type))
6569 if (!c_cast_p
6570 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6571 complain))
6572 return error_mark_node;
6573 return build_nop (type, expr);
6576 *valid_p = false;
6577 return error_mark_node;
6580 /* Return an expression representing static_cast<TYPE>(EXPR). */
6582 tree
6583 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6585 tree result;
6586 bool valid_p;
6588 if (type == error_mark_node || expr == error_mark_node)
6589 return error_mark_node;
6591 if (processing_template_decl)
6593 expr = build_min (STATIC_CAST_EXPR, type, expr);
6594 /* We don't know if it will or will not have side effects. */
6595 TREE_SIDE_EFFECTS (expr) = 1;
6596 return convert_from_reference (expr);
6599 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6600 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6601 if (TREE_CODE (type) != REFERENCE_TYPE
6602 && TREE_CODE (expr) == NOP_EXPR
6603 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6604 expr = TREE_OPERAND (expr, 0);
6606 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6607 complain);
6608 if (valid_p)
6610 if (result != error_mark_node)
6611 maybe_warn_about_useless_cast (type, expr, complain);
6612 return result;
6615 if (complain & tf_error)
6616 error ("invalid static_cast from type %qT to type %qT",
6617 TREE_TYPE (expr), type);
6618 return error_mark_node;
6621 /* EXPR is an expression with member function or pointer-to-member
6622 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6623 not permitted by ISO C++, but we accept it in some modes. If we
6624 are not in one of those modes, issue a diagnostic. Return the
6625 converted expression. */
6627 tree
6628 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6630 tree intype;
6631 tree decl;
6633 intype = TREE_TYPE (expr);
6634 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6635 || TREE_CODE (intype) == METHOD_TYPE);
6637 if (!(complain & tf_warning_or_error))
6638 return error_mark_node;
6640 if (pedantic || warn_pmf2ptr)
6641 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6642 "converting from %qT to %qT", intype, type);
6644 if (TREE_CODE (intype) == METHOD_TYPE)
6645 expr = build_addr_func (expr, complain);
6646 else if (TREE_CODE (expr) == PTRMEM_CST)
6647 expr = build_address (PTRMEM_CST_MEMBER (expr));
6648 else
6650 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6651 decl = build_address (decl);
6652 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6655 if (expr == error_mark_node)
6656 return error_mark_node;
6658 return build_nop (type, expr);
6661 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6662 If C_CAST_P is true, this reinterpret cast is being done as part of
6663 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6664 indicate whether or not reinterpret_cast was valid. */
6666 static tree
6667 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6668 bool *valid_p, tsubst_flags_t complain)
6670 tree intype;
6672 /* Assume the cast is invalid. */
6673 if (valid_p)
6674 *valid_p = true;
6676 if (type == error_mark_node || error_operand_p (expr))
6677 return error_mark_node;
6679 intype = TREE_TYPE (expr);
6681 /* Save casted types in the function's used types hash table. */
6682 used_types_insert (type);
6684 /* [expr.reinterpret.cast]
6685 An lvalue expression of type T1 can be cast to the type
6686 "reference to T2" if an expression of type "pointer to T1" can be
6687 explicitly converted to the type "pointer to T2" using a
6688 reinterpret_cast. */
6689 if (TREE_CODE (type) == REFERENCE_TYPE)
6691 if (! real_lvalue_p (expr))
6693 if (complain & tf_error)
6694 error ("invalid cast of an rvalue expression of type "
6695 "%qT to type %qT",
6696 intype, type);
6697 return error_mark_node;
6700 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6701 "B" are related class types; the reinterpret_cast does not
6702 adjust the pointer. */
6703 if (TYPE_PTR_P (intype)
6704 && (complain & tf_warning)
6705 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6706 COMPARE_BASE | COMPARE_DERIVED)))
6707 warning (0, "casting %qT to %qT does not dereference pointer",
6708 intype, type);
6710 expr = cp_build_addr_expr (expr, complain);
6712 if (warn_strict_aliasing > 2)
6713 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6715 if (expr != error_mark_node)
6716 expr = build_reinterpret_cast_1
6717 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6718 valid_p, complain);
6719 if (expr != error_mark_node)
6720 /* cp_build_indirect_ref isn't right for rvalue refs. */
6721 expr = convert_from_reference (fold_convert (type, expr));
6722 return expr;
6725 /* As a G++ extension, we consider conversions from member
6726 functions, and pointers to member functions to
6727 pointer-to-function and pointer-to-void types. If
6728 -Wno-pmf-conversions has not been specified,
6729 convert_member_func_to_ptr will issue an error message. */
6730 if ((TYPE_PTRMEMFUNC_P (intype)
6731 || TREE_CODE (intype) == METHOD_TYPE)
6732 && TYPE_PTR_P (type)
6733 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6734 || VOID_TYPE_P (TREE_TYPE (type))))
6735 return convert_member_func_to_ptr (type, expr, complain);
6737 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6738 array-to-pointer, and function-to-pointer conversions are
6739 performed. */
6740 expr = decay_conversion (expr, complain);
6742 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6743 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6744 if (TREE_CODE (expr) == NOP_EXPR
6745 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6746 expr = TREE_OPERAND (expr, 0);
6748 if (error_operand_p (expr))
6749 return error_mark_node;
6751 intype = TREE_TYPE (expr);
6753 /* [expr.reinterpret.cast]
6754 A pointer can be converted to any integral type large enough to
6755 hold it. ... A value of type std::nullptr_t can be converted to
6756 an integral type; the conversion has the same meaning and
6757 validity as a conversion of (void*)0 to the integral type. */
6758 if (CP_INTEGRAL_TYPE_P (type)
6759 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6761 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6763 if (complain & tf_error)
6764 permerror (input_location, "cast from %qT to %qT loses precision",
6765 intype, type);
6766 else
6767 return error_mark_node;
6769 if (NULLPTR_TYPE_P (intype))
6770 return build_int_cst (type, 0);
6772 /* [expr.reinterpret.cast]
6773 A value of integral or enumeration type can be explicitly
6774 converted to a pointer. */
6775 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6776 /* OK */
6778 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6779 || TYPE_PTR_OR_PTRMEM_P (type))
6780 && same_type_p (type, intype))
6781 /* DR 799 */
6782 return fold_if_not_in_template (build_nop (type, expr));
6783 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6784 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6785 return fold_if_not_in_template (build_nop (type, expr));
6786 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6787 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6789 tree sexpr = expr;
6791 if (!c_cast_p
6792 && check_for_casting_away_constness (intype, type,
6793 REINTERPRET_CAST_EXPR,
6794 complain))
6795 return error_mark_node;
6796 /* Warn about possible alignment problems. */
6797 if (STRICT_ALIGNMENT && warn_cast_align
6798 && (complain & tf_warning)
6799 && !VOID_TYPE_P (type)
6800 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6801 && COMPLETE_TYPE_P (TREE_TYPE (type))
6802 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6803 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6804 warning (OPT_Wcast_align, "cast from %qT to %qT "
6805 "increases required alignment of target type", intype, type);
6807 /* We need to strip nops here, because the front end likes to
6808 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6809 STRIP_NOPS (sexpr);
6810 if (warn_strict_aliasing <= 2)
6811 strict_aliasing_warning (intype, type, sexpr);
6813 return fold_if_not_in_template (build_nop (type, expr));
6815 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6816 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6818 if (complain & tf_warning)
6819 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6820 object pointer type or vice versa is conditionally-supported." */
6821 warning (OPT_Wconditionally_supported,
6822 "casting between pointer-to-function and pointer-to-object "
6823 "is conditionally-supported");
6824 return fold_if_not_in_template (build_nop (type, expr));
6826 else if (TREE_CODE (type) == VECTOR_TYPE)
6827 return fold_if_not_in_template (convert_to_vector (type, expr));
6828 else if (TREE_CODE (intype) == VECTOR_TYPE
6829 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6830 return fold_if_not_in_template (convert_to_integer (type, expr));
6831 else
6833 if (valid_p)
6834 *valid_p = false;
6835 if (complain & tf_error)
6836 error ("invalid cast from type %qT to type %qT", intype, type);
6837 return error_mark_node;
6840 return cp_convert (type, expr, complain);
6843 tree
6844 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6846 tree r;
6848 if (type == error_mark_node || expr == error_mark_node)
6849 return error_mark_node;
6851 if (processing_template_decl)
6853 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6855 if (!TREE_SIDE_EFFECTS (t)
6856 && type_dependent_expression_p (expr))
6857 /* There might turn out to be side effects inside expr. */
6858 TREE_SIDE_EFFECTS (t) = 1;
6859 return convert_from_reference (t);
6862 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6863 /*valid_p=*/NULL, complain);
6864 if (r != error_mark_node)
6865 maybe_warn_about_useless_cast (type, expr, complain);
6866 return r;
6869 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6870 return an appropriate expression. Otherwise, return
6871 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6872 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6873 performing a C-style cast, its value upon return will indicate
6874 whether or not the conversion succeeded. */
6876 static tree
6877 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6878 bool *valid_p)
6880 tree src_type;
6881 tree reference_type;
6883 /* Callers are responsible for handling error_mark_node as a
6884 destination type. */
6885 gcc_assert (dst_type != error_mark_node);
6886 /* In a template, callers should be building syntactic
6887 representations of casts, not using this machinery. */
6888 gcc_assert (!processing_template_decl);
6890 /* Assume the conversion is invalid. */
6891 if (valid_p)
6892 *valid_p = false;
6894 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6896 if (complain & tf_error)
6897 error ("invalid use of const_cast with type %qT, "
6898 "which is not a pointer, "
6899 "reference, nor a pointer-to-data-member type", dst_type);
6900 return error_mark_node;
6903 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6905 if (complain & tf_error)
6906 error ("invalid use of const_cast with type %qT, which is a pointer "
6907 "or reference to a function type", dst_type);
6908 return error_mark_node;
6911 /* Save casted types in the function's used types hash table. */
6912 used_types_insert (dst_type);
6914 src_type = TREE_TYPE (expr);
6915 /* Expressions do not really have reference types. */
6916 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6917 src_type = TREE_TYPE (src_type);
6919 /* [expr.const.cast]
6921 For two object types T1 and T2, if a pointer to T1 can be explicitly
6922 converted to the type "pointer to T2" using a const_cast, then the
6923 following conversions can also be made:
6925 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6926 type T2 using the cast const_cast<T2&>;
6928 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6929 type T2 using the cast const_cast<T2&&>; and
6931 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6932 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6934 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6936 reference_type = dst_type;
6937 if (!TYPE_REF_IS_RVALUE (dst_type)
6938 ? real_lvalue_p (expr)
6939 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6940 ? lvalue_p (expr)
6941 : lvalue_or_rvalue_with_address_p (expr)))
6942 /* OK. */;
6943 else
6945 if (complain & tf_error)
6946 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6947 src_type, dst_type);
6948 return error_mark_node;
6950 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6951 src_type = build_pointer_type (src_type);
6953 else
6955 reference_type = NULL_TREE;
6956 /* If the destination type is not a reference type, the
6957 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6958 conversions are performed. */
6959 src_type = type_decays_to (src_type);
6960 if (src_type == error_mark_node)
6961 return error_mark_node;
6964 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
6966 if (comp_ptr_ttypes_const (dst_type, src_type))
6968 if (valid_p)
6970 *valid_p = true;
6971 /* This cast is actually a C-style cast. Issue a warning if
6972 the user is making a potentially unsafe cast. */
6973 check_for_casting_away_constness (src_type, dst_type,
6974 CAST_EXPR, complain);
6976 if (reference_type)
6978 expr = cp_build_addr_expr (expr, complain);
6979 if (expr == error_mark_node)
6980 return error_mark_node;
6981 expr = build_nop (reference_type, expr);
6982 return convert_from_reference (expr);
6984 else
6986 expr = decay_conversion (expr, complain);
6987 if (expr == error_mark_node)
6988 return error_mark_node;
6990 /* build_c_cast puts on a NOP_EXPR to make the result not an
6991 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6992 non-lvalue context. */
6993 if (TREE_CODE (expr) == NOP_EXPR
6994 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6995 expr = TREE_OPERAND (expr, 0);
6996 return build_nop (dst_type, expr);
6999 else if (valid_p
7000 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7001 TREE_TYPE (src_type)))
7002 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7003 complain);
7006 if (complain & tf_error)
7007 error ("invalid const_cast from type %qT to type %qT",
7008 src_type, dst_type);
7009 return error_mark_node;
7012 tree
7013 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7015 tree r;
7017 if (type == error_mark_node || error_operand_p (expr))
7018 return error_mark_node;
7020 if (processing_template_decl)
7022 tree t = build_min (CONST_CAST_EXPR, type, expr);
7024 if (!TREE_SIDE_EFFECTS (t)
7025 && type_dependent_expression_p (expr))
7026 /* There might turn out to be side effects inside expr. */
7027 TREE_SIDE_EFFECTS (t) = 1;
7028 return convert_from_reference (t);
7031 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7032 if (r != error_mark_node)
7033 maybe_warn_about_useless_cast (type, expr, complain);
7034 return r;
7037 /* Like cp_build_c_cast, but for the c-common bits. */
7039 tree
7040 build_c_cast (location_t /*loc*/, tree type, tree expr)
7042 return cp_build_c_cast (type, expr, tf_warning_or_error);
7045 /* Build an expression representing an explicit C-style cast to type
7046 TYPE of expression EXPR. */
7048 tree
7049 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7051 tree value = expr;
7052 tree result;
7053 bool valid_p;
7055 if (type == error_mark_node || error_operand_p (expr))
7056 return error_mark_node;
7058 if (processing_template_decl)
7060 tree t = build_min (CAST_EXPR, type,
7061 tree_cons (NULL_TREE, value, NULL_TREE));
7062 /* We don't know if it will or will not have side effects. */
7063 TREE_SIDE_EFFECTS (t) = 1;
7064 return convert_from_reference (t);
7067 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7068 'Class') should always be retained, because this information aids
7069 in method lookup. */
7070 if (objc_is_object_ptr (type)
7071 && objc_is_object_ptr (TREE_TYPE (expr)))
7072 return build_nop (type, expr);
7074 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7075 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7076 if (TREE_CODE (type) != REFERENCE_TYPE
7077 && TREE_CODE (value) == NOP_EXPR
7078 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7079 value = TREE_OPERAND (value, 0);
7081 if (TREE_CODE (type) == ARRAY_TYPE)
7083 /* Allow casting from T1* to T2[] because Cfront allows it.
7084 NIHCL uses it. It is not valid ISO C++ however. */
7085 if (TYPE_PTR_P (TREE_TYPE (expr)))
7087 if (complain & tf_error)
7088 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7089 else
7090 return error_mark_node;
7091 type = build_pointer_type (TREE_TYPE (type));
7093 else
7095 if (complain & tf_error)
7096 error ("ISO C++ forbids casting to an array type %qT", type);
7097 return error_mark_node;
7101 if (TREE_CODE (type) == FUNCTION_TYPE
7102 || TREE_CODE (type) == METHOD_TYPE)
7104 if (complain & tf_error)
7105 error ("invalid cast to function type %qT", type);
7106 return error_mark_node;
7109 if (TYPE_PTR_P (type)
7110 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7111 /* Casting to an integer of smaller size is an error detected elsewhere. */
7112 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7113 /* Don't warn about converting any constant. */
7114 && !TREE_CONSTANT (value))
7115 warning_at (input_location, OPT_Wint_to_pointer_cast,
7116 "cast to pointer from integer of different size");
7118 /* A C-style cast can be a const_cast. */
7119 result = build_const_cast_1 (type, value, complain & tf_warning,
7120 &valid_p);
7121 if (valid_p)
7123 if (result != error_mark_node)
7124 maybe_warn_about_useless_cast (type, value, complain);
7125 return result;
7128 /* Or a static cast. */
7129 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7130 &valid_p, complain);
7131 /* Or a reinterpret_cast. */
7132 if (!valid_p)
7133 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7134 &valid_p, complain);
7135 /* The static_cast or reinterpret_cast may be followed by a
7136 const_cast. */
7137 if (valid_p
7138 /* A valid cast may result in errors if, for example, a
7139 conversion to an ambiguous base class is required. */
7140 && !error_operand_p (result))
7142 tree result_type;
7144 maybe_warn_about_useless_cast (type, value, complain);
7146 /* Non-class rvalues always have cv-unqualified type. */
7147 if (!CLASS_TYPE_P (type))
7148 type = TYPE_MAIN_VARIANT (type);
7149 result_type = TREE_TYPE (result);
7150 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7151 result_type = TYPE_MAIN_VARIANT (result_type);
7152 /* If the type of RESULT does not match TYPE, perform a
7153 const_cast to make it match. If the static_cast or
7154 reinterpret_cast succeeded, we will differ by at most
7155 cv-qualification, so the follow-on const_cast is guaranteed
7156 to succeed. */
7157 if (!same_type_p (non_reference (type), non_reference (result_type)))
7159 result = build_const_cast_1 (type, result, false, &valid_p);
7160 gcc_assert (valid_p);
7162 return result;
7165 return error_mark_node;
7168 /* For use from the C common bits. */
7169 tree
7170 build_modify_expr (location_t /*location*/,
7171 tree lhs, tree /*lhs_origtype*/,
7172 enum tree_code modifycode,
7173 location_t /*rhs_location*/, tree rhs,
7174 tree /*rhs_origtype*/)
7176 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7179 /* Build an assignment expression of lvalue LHS from value RHS.
7180 MODIFYCODE is the code for a binary operator that we use
7181 to combine the old value of LHS with RHS to get the new value.
7182 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7184 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7186 tree
7187 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7188 tsubst_flags_t complain)
7190 tree result;
7191 tree newrhs = rhs;
7192 tree lhstype = TREE_TYPE (lhs);
7193 tree olhstype = lhstype;
7194 bool plain_assign = (modifycode == NOP_EXPR);
7196 /* Avoid duplicate error messages from operands that had errors. */
7197 if (error_operand_p (lhs) || error_operand_p (rhs))
7198 return error_mark_node;
7200 /* Handle control structure constructs used as "lvalues". */
7201 switch (TREE_CODE (lhs))
7203 /* Handle --foo = 5; as these are valid constructs in C++. */
7204 case PREDECREMENT_EXPR:
7205 case PREINCREMENT_EXPR:
7206 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7207 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7208 stabilize_reference (TREE_OPERAND (lhs, 0)),
7209 TREE_OPERAND (lhs, 1));
7210 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7211 modifycode, rhs, complain);
7212 if (newrhs == error_mark_node)
7213 return error_mark_node;
7214 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7216 /* Handle (a, b) used as an "lvalue". */
7217 case COMPOUND_EXPR:
7218 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7219 modifycode, rhs, complain);
7220 if (newrhs == error_mark_node)
7221 return error_mark_node;
7222 return build2 (COMPOUND_EXPR, lhstype,
7223 TREE_OPERAND (lhs, 0), newrhs);
7225 case MODIFY_EXPR:
7226 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7227 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7228 stabilize_reference (TREE_OPERAND (lhs, 0)),
7229 TREE_OPERAND (lhs, 1));
7230 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7231 complain);
7232 if (newrhs == error_mark_node)
7233 return error_mark_node;
7234 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7236 case MIN_EXPR:
7237 case MAX_EXPR:
7238 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7239 when neither operand has side-effects. */
7240 if (!lvalue_or_else (lhs, lv_assign, complain))
7241 return error_mark_node;
7243 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7244 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7246 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7247 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7248 boolean_type_node,
7249 TREE_OPERAND (lhs, 0),
7250 TREE_OPERAND (lhs, 1)),
7251 TREE_OPERAND (lhs, 0),
7252 TREE_OPERAND (lhs, 1));
7253 /* Fall through. */
7255 /* Handle (a ? b : c) used as an "lvalue". */
7256 case COND_EXPR:
7258 /* Produce (a ? (b = rhs) : (c = rhs))
7259 except that the RHS goes through a save-expr
7260 so the code to compute it is only emitted once. */
7261 tree cond;
7262 tree preeval = NULL_TREE;
7264 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7266 if (complain & tf_error)
7267 error ("void value not ignored as it ought to be");
7268 return error_mark_node;
7271 rhs = stabilize_expr (rhs, &preeval);
7273 /* Check this here to avoid odd errors when trying to convert
7274 a throw to the type of the COND_EXPR. */
7275 if (!lvalue_or_else (lhs, lv_assign, complain))
7276 return error_mark_node;
7278 cond = build_conditional_expr
7279 (input_location, TREE_OPERAND (lhs, 0),
7280 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7281 modifycode, rhs, complain),
7282 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7283 modifycode, rhs, complain),
7284 complain);
7286 if (cond == error_mark_node)
7287 return cond;
7288 /* Make sure the code to compute the rhs comes out
7289 before the split. */
7290 if (preeval)
7291 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7292 return cond;
7295 default:
7296 break;
7299 if (modifycode == INIT_EXPR)
7301 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7302 /* Do the default thing. */;
7303 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7305 /* Compound literal. */
7306 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7307 /* Call convert to generate an error; see PR 11063. */
7308 rhs = convert (lhstype, rhs);
7309 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7310 TREE_SIDE_EFFECTS (result) = 1;
7311 return result;
7313 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7314 /* Do the default thing. */;
7315 else
7317 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7318 result = build_special_member_call (lhs, complete_ctor_identifier,
7319 &rhs_vec, lhstype, LOOKUP_NORMAL,
7320 complain);
7321 release_tree_vector (rhs_vec);
7322 if (result == NULL_TREE)
7323 return error_mark_node;
7324 return result;
7327 else
7329 lhs = require_complete_type_sfinae (lhs, complain);
7330 if (lhs == error_mark_node)
7331 return error_mark_node;
7333 if (modifycode == NOP_EXPR)
7335 if (c_dialect_objc ())
7337 result = objc_maybe_build_modify_expr (lhs, rhs);
7338 if (result)
7339 return result;
7342 /* `operator=' is not an inheritable operator. */
7343 if (! MAYBE_CLASS_TYPE_P (lhstype))
7344 /* Do the default thing. */;
7345 else
7347 result = build_new_op (input_location, MODIFY_EXPR,
7348 LOOKUP_NORMAL, lhs, rhs,
7349 make_node (NOP_EXPR), /*overload=*/NULL,
7350 complain);
7351 if (result == NULL_TREE)
7352 return error_mark_node;
7353 return result;
7355 lhstype = olhstype;
7357 else
7359 tree init = NULL_TREE;
7361 /* A binary op has been requested. Combine the old LHS
7362 value with the RHS producing the value we should actually
7363 store into the LHS. */
7364 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7365 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7366 || MAYBE_CLASS_TYPE_P (lhstype)));
7368 /* Preevaluate the RHS to make sure its evaluation is complete
7369 before the lvalue-to-rvalue conversion of the LHS:
7371 [expr.ass] With respect to an indeterminately-sequenced
7372 function call, the operation of a compound assignment is a
7373 single evaluation. [ Note: Therefore, a function call shall
7374 not intervene between the lvalue-to-rvalue conversion and the
7375 side effect associated with any single compound assignment
7376 operator. -- end note ] */
7377 lhs = stabilize_reference (lhs);
7378 if (TREE_SIDE_EFFECTS (rhs))
7379 rhs = mark_rvalue_use (rhs);
7380 rhs = stabilize_expr (rhs, &init);
7381 newrhs = cp_build_binary_op (input_location,
7382 modifycode, lhs, rhs,
7383 complain);
7384 if (newrhs == error_mark_node)
7386 if (complain & tf_error)
7387 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7388 TREE_TYPE (lhs), TREE_TYPE (rhs));
7389 return error_mark_node;
7392 if (init)
7393 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7395 /* Now it looks like a plain assignment. */
7396 modifycode = NOP_EXPR;
7397 if (c_dialect_objc ())
7399 result = objc_maybe_build_modify_expr (lhs, newrhs);
7400 if (result)
7401 return result;
7404 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7405 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7408 /* The left-hand side must be an lvalue. */
7409 if (!lvalue_or_else (lhs, lv_assign, complain))
7410 return error_mark_node;
7412 /* Warn about modifying something that is `const'. Don't warn if
7413 this is initialization. */
7414 if (modifycode != INIT_EXPR
7415 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7416 /* Functions are not modifiable, even though they are
7417 lvalues. */
7418 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7419 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7420 /* If it's an aggregate and any field is const, then it is
7421 effectively const. */
7422 || (CLASS_TYPE_P (lhstype)
7423 && C_TYPE_FIELDS_READONLY (lhstype))))
7425 if (complain & tf_error)
7426 cxx_readonly_error (lhs, lv_assign);
7427 else
7428 return error_mark_node;
7431 /* If storing into a structure or union member, it may have been given a
7432 lowered bitfield type. We need to convert to the declared type first,
7433 so retrieve it now. */
7435 olhstype = unlowered_expr_type (lhs);
7437 /* Convert new value to destination type. */
7439 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7441 int from_array;
7443 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7445 if (modifycode != INIT_EXPR)
7447 if (complain & tf_error)
7448 error ("assigning to an array from an initializer list");
7449 return error_mark_node;
7451 if (check_array_initializer (lhs, lhstype, newrhs))
7452 return error_mark_node;
7453 newrhs = digest_init (lhstype, newrhs, complain);
7454 if (newrhs == error_mark_node)
7455 return error_mark_node;
7458 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7459 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7461 if (complain & tf_error)
7462 error ("incompatible types in assignment of %qT to %qT",
7463 TREE_TYPE (rhs), lhstype);
7464 return error_mark_node;
7467 /* Allow array assignment in compiler-generated code. */
7468 else if (!current_function_decl
7469 || !DECL_DEFAULTED_FN (current_function_decl))
7471 /* This routine is used for both initialization and assignment.
7472 Make sure the diagnostic message differentiates the context. */
7473 if (complain & tf_error)
7475 if (modifycode == INIT_EXPR)
7476 error ("array used as initializer");
7477 else
7478 error ("invalid array assignment");
7480 return error_mark_node;
7483 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7484 ? 1 + (modifycode != INIT_EXPR): 0;
7485 return build_vec_init (lhs, NULL_TREE, newrhs,
7486 /*explicit_value_init_p=*/false,
7487 from_array, complain);
7490 if (modifycode == INIT_EXPR)
7491 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7492 LOOKUP_ONLYCONVERTING. */
7493 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7494 ICR_INIT, NULL_TREE, 0,
7495 complain);
7496 else
7497 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7498 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7500 if (!same_type_p (lhstype, olhstype))
7501 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7503 if (modifycode != INIT_EXPR)
7505 if (TREE_CODE (newrhs) == CALL_EXPR
7506 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7507 newrhs = build_cplus_new (lhstype, newrhs, complain);
7509 /* Can't initialize directly from a TARGET_EXPR, since that would
7510 cause the lhs to be constructed twice, and possibly result in
7511 accidental self-initialization. So we force the TARGET_EXPR to be
7512 expanded without a target. */
7513 if (TREE_CODE (newrhs) == TARGET_EXPR)
7514 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7515 TREE_OPERAND (newrhs, 0));
7518 if (newrhs == error_mark_node)
7519 return error_mark_node;
7521 if (c_dialect_objc () && flag_objc_gc)
7523 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7525 if (result)
7526 return result;
7529 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7530 lhstype, lhs, newrhs);
7532 TREE_SIDE_EFFECTS (result) = 1;
7533 if (!plain_assign)
7534 TREE_NO_WARNING (result) = 1;
7536 return result;
7539 tree
7540 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7541 tree rhs, tsubst_flags_t complain)
7543 if (processing_template_decl)
7544 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7545 build_min_nt_loc (loc, modifycode, NULL_TREE,
7546 NULL_TREE), rhs);
7548 if (modifycode != NOP_EXPR)
7550 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7551 make_node (modifycode), /*overload=*/NULL,
7552 complain);
7553 if (rval)
7555 TREE_NO_WARNING (rval) = 1;
7556 return rval;
7559 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7562 /* Helper function for get_delta_difference which assumes FROM is a base
7563 class of TO. Returns a delta for the conversion of pointer-to-member
7564 of FROM to pointer-to-member of TO. If the conversion is invalid and
7565 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7566 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7567 If C_CAST_P is true, this conversion is taking place as part of a
7568 C-style cast. */
7570 static tree
7571 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7572 tsubst_flags_t complain)
7574 tree binfo;
7575 base_kind kind;
7577 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7578 &kind, complain);
7580 if (binfo == error_mark_node)
7582 if (!(complain & tf_error))
7583 return error_mark_node;
7585 error (" in pointer to member function conversion");
7586 return size_zero_node;
7588 else if (binfo)
7590 if (kind != bk_via_virtual)
7591 return BINFO_OFFSET (binfo);
7592 else
7593 /* FROM is a virtual base class of TO. Issue an error or warning
7594 depending on whether or not this is a reinterpret cast. */
7596 if (!(complain & tf_error))
7597 return error_mark_node;
7599 error ("pointer to member conversion via virtual base %qT",
7600 BINFO_TYPE (binfo_from_vbase (binfo)));
7602 return size_zero_node;
7605 else
7606 return NULL_TREE;
7609 /* Get difference in deltas for different pointer to member function
7610 types. If the conversion is invalid and tf_error is not set in
7611 COMPLAIN, returns error_mark_node, otherwise returns an integer
7612 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7613 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7614 conversions as well. If C_CAST_P is true this conversion is taking
7615 place as part of a C-style cast.
7617 Note that the naming of FROM and TO is kind of backwards; the return
7618 value is what we add to a TO in order to get a FROM. They are named
7619 this way because we call this function to find out how to convert from
7620 a pointer to member of FROM to a pointer to member of TO. */
7622 static tree
7623 get_delta_difference (tree from, tree to,
7624 bool allow_inverse_p,
7625 bool c_cast_p, tsubst_flags_t complain)
7627 tree result;
7629 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7630 /* Pointer to member of incomplete class is permitted*/
7631 result = size_zero_node;
7632 else
7633 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7635 if (result == error_mark_node)
7636 return error_mark_node;
7638 if (!result)
7640 if (!allow_inverse_p)
7642 if (!(complain & tf_error))
7643 return error_mark_node;
7645 error_not_base_type (from, to);
7646 error (" in pointer to member conversion");
7647 result = size_zero_node;
7649 else
7651 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7653 if (result == error_mark_node)
7654 return error_mark_node;
7656 if (result)
7657 result = size_diffop_loc (input_location,
7658 size_zero_node, result);
7659 else
7661 if (!(complain & tf_error))
7662 return error_mark_node;
7664 error_not_base_type (from, to);
7665 error (" in pointer to member conversion");
7666 result = size_zero_node;
7671 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7672 result));
7675 /* Return a constructor for the pointer-to-member-function TYPE using
7676 the other components as specified. */
7678 tree
7679 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7681 tree u = NULL_TREE;
7682 tree delta_field;
7683 tree pfn_field;
7684 vec<constructor_elt, va_gc> *v;
7686 /* Pull the FIELD_DECLs out of the type. */
7687 pfn_field = TYPE_FIELDS (type);
7688 delta_field = DECL_CHAIN (pfn_field);
7690 /* Make sure DELTA has the type we want. */
7691 delta = convert_and_check (delta_type_node, delta);
7693 /* Convert to the correct target type if necessary. */
7694 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7696 /* Finish creating the initializer. */
7697 vec_alloc (v, 2);
7698 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7699 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7700 u = build_constructor (type, v);
7701 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7702 TREE_STATIC (u) = (TREE_CONSTANT (u)
7703 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7704 != NULL_TREE)
7705 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7706 != NULL_TREE));
7707 return u;
7710 /* Build a constructor for a pointer to member function. It can be
7711 used to initialize global variables, local variable, or used
7712 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7713 want to be.
7715 If FORCE is nonzero, then force this conversion, even if
7716 we would rather not do it. Usually set when using an explicit
7717 cast. A C-style cast is being processed iff C_CAST_P is true.
7719 Return error_mark_node, if something goes wrong. */
7721 tree
7722 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7723 tsubst_flags_t complain)
7725 tree fn;
7726 tree pfn_type;
7727 tree to_type;
7729 if (error_operand_p (pfn))
7730 return error_mark_node;
7732 pfn_type = TREE_TYPE (pfn);
7733 to_type = build_ptrmemfunc_type (type);
7735 /* Handle multiple conversions of pointer to member functions. */
7736 if (TYPE_PTRMEMFUNC_P (pfn_type))
7738 tree delta = NULL_TREE;
7739 tree npfn = NULL_TREE;
7740 tree n;
7742 if (!force
7743 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7744 LOOKUP_NORMAL, complain))
7746 if (complain & tf_error)
7747 error ("invalid conversion to type %qT from type %qT",
7748 to_type, pfn_type);
7749 else
7750 return error_mark_node;
7753 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7754 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7755 force,
7756 c_cast_p, complain);
7757 if (n == error_mark_node)
7758 return error_mark_node;
7760 /* We don't have to do any conversion to convert a
7761 pointer-to-member to its own type. But, we don't want to
7762 just return a PTRMEM_CST if there's an explicit cast; that
7763 cast should make the expression an invalid template argument. */
7764 if (TREE_CODE (pfn) != PTRMEM_CST)
7766 if (same_type_p (to_type, pfn_type))
7767 return pfn;
7768 else if (integer_zerop (n))
7769 return build_reinterpret_cast (to_type, pfn,
7770 complain);
7773 if (TREE_SIDE_EFFECTS (pfn))
7774 pfn = save_expr (pfn);
7776 /* Obtain the function pointer and the current DELTA. */
7777 if (TREE_CODE (pfn) == PTRMEM_CST)
7778 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7779 else
7781 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7782 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7785 /* Just adjust the DELTA field. */
7786 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7787 (TREE_TYPE (delta), ptrdiff_type_node));
7788 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7789 n = cp_build_binary_op (input_location,
7790 LSHIFT_EXPR, n, integer_one_node,
7791 complain);
7792 delta = cp_build_binary_op (input_location,
7793 PLUS_EXPR, delta, n, complain);
7794 return build_ptrmemfunc1 (to_type, delta, npfn);
7797 /* Handle null pointer to member function conversions. */
7798 if (null_ptr_cst_p (pfn))
7800 pfn = cp_build_c_cast (type, pfn, complain);
7801 return build_ptrmemfunc1 (to_type,
7802 integer_zero_node,
7803 pfn);
7806 if (type_unknown_p (pfn))
7807 return instantiate_type (type, pfn, complain);
7809 fn = TREE_OPERAND (pfn, 0);
7810 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7811 /* In a template, we will have preserved the
7812 OFFSET_REF. */
7813 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7814 return make_ptrmem_cst (to_type, fn);
7817 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7818 given by CST.
7820 ??? There is no consistency as to the types returned for the above
7821 values. Some code acts as if it were a sizetype and some as if it were
7822 integer_type_node. */
7824 void
7825 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7827 tree type = TREE_TYPE (cst);
7828 tree fn = PTRMEM_CST_MEMBER (cst);
7829 tree ptr_class, fn_class;
7831 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7833 /* The class that the function belongs to. */
7834 fn_class = DECL_CONTEXT (fn);
7836 /* The class that we're creating a pointer to member of. */
7837 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7839 /* First, calculate the adjustment to the function's class. */
7840 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7841 /*c_cast_p=*/0, tf_warning_or_error);
7843 if (!DECL_VIRTUAL_P (fn))
7844 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7845 build_addr_func (fn, tf_warning_or_error));
7846 else
7848 /* If we're dealing with a virtual function, we have to adjust 'this'
7849 again, to point to the base which provides the vtable entry for
7850 fn; the call will do the opposite adjustment. */
7851 tree orig_class = DECL_CONTEXT (fn);
7852 tree binfo = binfo_or_else (orig_class, fn_class);
7853 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7854 *delta, BINFO_OFFSET (binfo));
7855 *delta = fold_if_not_in_template (*delta);
7857 /* We set PFN to the vtable offset at which the function can be
7858 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7859 case delta is shifted left, and then incremented). */
7860 *pfn = DECL_VINDEX (fn);
7861 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7862 TYPE_SIZE_UNIT (vtable_entry_type));
7863 *pfn = fold_if_not_in_template (*pfn);
7865 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7867 case ptrmemfunc_vbit_in_pfn:
7868 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7869 integer_one_node);
7870 *pfn = fold_if_not_in_template (*pfn);
7871 break;
7873 case ptrmemfunc_vbit_in_delta:
7874 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7875 *delta, integer_one_node);
7876 *delta = fold_if_not_in_template (*delta);
7877 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7878 *delta, integer_one_node);
7879 *delta = fold_if_not_in_template (*delta);
7880 break;
7882 default:
7883 gcc_unreachable ();
7886 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7887 *pfn = fold_if_not_in_template (*pfn);
7891 /* Return an expression for PFN from the pointer-to-member function
7892 given by T. */
7894 static tree
7895 pfn_from_ptrmemfunc (tree t)
7897 if (TREE_CODE (t) == PTRMEM_CST)
7899 tree delta;
7900 tree pfn;
7902 expand_ptrmemfunc_cst (t, &delta, &pfn);
7903 if (pfn)
7904 return pfn;
7907 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7910 /* Return an expression for DELTA from the pointer-to-member function
7911 given by T. */
7913 static tree
7914 delta_from_ptrmemfunc (tree t)
7916 if (TREE_CODE (t) == PTRMEM_CST)
7918 tree delta;
7919 tree pfn;
7921 expand_ptrmemfunc_cst (t, &delta, &pfn);
7922 if (delta)
7923 return delta;
7926 return build_ptrmemfunc_access_expr (t, delta_identifier);
7929 /* Convert value RHS to type TYPE as preparation for an assignment to
7930 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7931 implicit conversion is. If FNDECL is non-NULL, we are doing the
7932 conversion in order to pass the PARMNUMth argument of FNDECL.
7933 If FNDECL is NULL, we are doing the conversion in function pointer
7934 argument passing, conversion in initialization, etc. */
7936 static tree
7937 convert_for_assignment (tree type, tree rhs,
7938 impl_conv_rhs errtype, tree fndecl, int parmnum,
7939 tsubst_flags_t complain, int flags)
7941 tree rhstype;
7942 enum tree_code coder;
7944 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7945 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7946 rhs = TREE_OPERAND (rhs, 0);
7948 rhstype = TREE_TYPE (rhs);
7949 coder = TREE_CODE (rhstype);
7951 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7952 && vector_types_convertible_p (type, rhstype, true))
7954 rhs = mark_rvalue_use (rhs);
7955 return convert (type, rhs);
7958 if (rhs == error_mark_node || rhstype == error_mark_node)
7959 return error_mark_node;
7960 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7961 return error_mark_node;
7963 /* The RHS of an assignment cannot have void type. */
7964 if (coder == VOID_TYPE)
7966 if (complain & tf_error)
7967 error ("void value not ignored as it ought to be");
7968 return error_mark_node;
7971 if (c_dialect_objc ())
7973 int parmno;
7974 tree selector;
7975 tree rname = fndecl;
7977 switch (errtype)
7979 case ICR_ASSIGN:
7980 parmno = -1;
7981 break;
7982 case ICR_INIT:
7983 parmno = -2;
7984 break;
7985 default:
7986 selector = objc_message_selector ();
7987 parmno = parmnum;
7988 if (selector && parmno > 1)
7990 rname = selector;
7991 parmno -= 1;
7995 if (objc_compare_types (type, rhstype, parmno, rname))
7997 rhs = mark_rvalue_use (rhs);
7998 return convert (type, rhs);
8002 /* [expr.ass]
8004 The expression is implicitly converted (clause _conv_) to the
8005 cv-unqualified type of the left operand.
8007 We allow bad conversions here because by the time we get to this point
8008 we are committed to doing the conversion. If we end up doing a bad
8009 conversion, convert_like will complain. */
8010 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8012 /* When -Wno-pmf-conversions is use, we just silently allow
8013 conversions from pointers-to-members to plain pointers. If
8014 the conversion doesn't work, cp_convert will complain. */
8015 if (!warn_pmf2ptr
8016 && TYPE_PTR_P (type)
8017 && TYPE_PTRMEMFUNC_P (rhstype))
8018 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8019 else
8021 if (complain & tf_error)
8023 /* If the right-hand side has unknown type, then it is an
8024 overloaded function. Call instantiate_type to get error
8025 messages. */
8026 if (rhstype == unknown_type_node)
8027 instantiate_type (type, rhs, tf_warning_or_error);
8028 else if (fndecl)
8029 error ("cannot convert %qT to %qT for argument %qP to %qD",
8030 rhstype, type, parmnum, fndecl);
8031 else
8032 switch (errtype)
8034 case ICR_DEFAULT_ARGUMENT:
8035 error ("cannot convert %qT to %qT in default argument",
8036 rhstype, type);
8037 break;
8038 case ICR_ARGPASS:
8039 error ("cannot convert %qT to %qT in argument passing",
8040 rhstype, type);
8041 break;
8042 case ICR_CONVERTING:
8043 error ("cannot convert %qT to %qT",
8044 rhstype, type);
8045 break;
8046 case ICR_INIT:
8047 error ("cannot convert %qT to %qT in initialization",
8048 rhstype, type);
8049 break;
8050 case ICR_RETURN:
8051 error ("cannot convert %qT to %qT in return",
8052 rhstype, type);
8053 break;
8054 case ICR_ASSIGN:
8055 error ("cannot convert %qT to %qT in assignment",
8056 rhstype, type);
8057 break;
8058 default:
8059 gcc_unreachable();
8062 return error_mark_node;
8065 if (warn_suggest_attribute_format)
8067 const enum tree_code codel = TREE_CODE (type);
8068 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8069 && coder == codel
8070 && check_missing_format_attribute (type, rhstype)
8071 && (complain & tf_warning))
8072 switch (errtype)
8074 case ICR_ARGPASS:
8075 case ICR_DEFAULT_ARGUMENT:
8076 if (fndecl)
8077 warning (OPT_Wsuggest_attribute_format,
8078 "parameter %qP of %qD might be a candidate "
8079 "for a format attribute", parmnum, fndecl);
8080 else
8081 warning (OPT_Wsuggest_attribute_format,
8082 "parameter might be a candidate "
8083 "for a format attribute");
8084 break;
8085 case ICR_CONVERTING:
8086 warning (OPT_Wsuggest_attribute_format,
8087 "target of conversion might be a candidate "
8088 "for a format attribute");
8089 break;
8090 case ICR_INIT:
8091 warning (OPT_Wsuggest_attribute_format,
8092 "target of initialization might be a candidate "
8093 "for a format attribute");
8094 break;
8095 case ICR_RETURN:
8096 warning (OPT_Wsuggest_attribute_format,
8097 "return type might be a candidate "
8098 "for a format attribute");
8099 break;
8100 case ICR_ASSIGN:
8101 warning (OPT_Wsuggest_attribute_format,
8102 "left-hand side of assignment might be a candidate "
8103 "for a format attribute");
8104 break;
8105 default:
8106 gcc_unreachable();
8110 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8111 does not. */
8112 if (warn_parentheses
8113 && TREE_CODE (type) == BOOLEAN_TYPE
8114 && TREE_CODE (rhs) == MODIFY_EXPR
8115 && !TREE_NO_WARNING (rhs)
8116 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8117 && (complain & tf_warning))
8119 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8121 warning_at (loc, OPT_Wparentheses,
8122 "suggest parentheses around assignment used as truth value");
8123 TREE_NO_WARNING (rhs) = 1;
8126 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8127 complain, flags);
8130 /* Convert RHS to be of type TYPE.
8131 If EXP is nonzero, it is the target of the initialization.
8132 ERRTYPE indicates what kind of error the implicit conversion is.
8134 Two major differences between the behavior of
8135 `convert_for_assignment' and `convert_for_initialization'
8136 are that references are bashed in the former, while
8137 copied in the latter, and aggregates are assigned in
8138 the former (operator=) while initialized in the
8139 latter (X(X&)).
8141 If using constructor make sure no conversion operator exists, if one does
8142 exist, an ambiguity exists. */
8144 tree
8145 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8146 impl_conv_rhs errtype, tree fndecl, int parmnum,
8147 tsubst_flags_t complain)
8149 enum tree_code codel = TREE_CODE (type);
8150 tree rhstype;
8151 enum tree_code coder;
8153 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8154 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8155 if (TREE_CODE (rhs) == NOP_EXPR
8156 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8157 && codel != REFERENCE_TYPE)
8158 rhs = TREE_OPERAND (rhs, 0);
8160 if (type == error_mark_node
8161 || rhs == error_mark_node
8162 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8163 return error_mark_node;
8165 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8166 && TREE_CODE (type) != ARRAY_TYPE
8167 && (TREE_CODE (type) != REFERENCE_TYPE
8168 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8169 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8170 && !TYPE_REFFN_P (type))
8171 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8172 rhs = decay_conversion (rhs, complain);
8174 rhstype = TREE_TYPE (rhs);
8175 coder = TREE_CODE (rhstype);
8177 if (coder == ERROR_MARK)
8178 return error_mark_node;
8180 /* We accept references to incomplete types, so we can
8181 return here before checking if RHS is of complete type. */
8183 if (codel == REFERENCE_TYPE)
8185 /* This should eventually happen in convert_arguments. */
8186 int savew = 0, savee = 0;
8188 if (fndecl)
8189 savew = warningcount + werrorcount, savee = errorcount;
8190 rhs = initialize_reference (type, rhs, flags, complain);
8192 if (fndecl
8193 && (warningcount + werrorcount > savew || errorcount > savee))
8194 inform (input_location,
8195 "in passing argument %P of %q+D", parmnum, fndecl);
8197 return rhs;
8200 if (exp != 0)
8201 exp = require_complete_type_sfinae (exp, complain);
8202 if (exp == error_mark_node)
8203 return error_mark_node;
8205 rhstype = non_reference (rhstype);
8207 type = complete_type (type);
8209 if (DIRECT_INIT_EXPR_P (type, rhs))
8210 /* Don't try to do copy-initialization if we already have
8211 direct-initialization. */
8212 return rhs;
8214 if (MAYBE_CLASS_TYPE_P (type))
8215 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8217 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8218 complain, flags);
8221 /* If RETVAL is the address of, or a reference to, a local variable or
8222 temporary give an appropriate warning. */
8224 static void
8225 maybe_warn_about_returning_address_of_local (tree retval)
8227 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8228 tree whats_returned = retval;
8230 for (;;)
8232 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8233 whats_returned = TREE_OPERAND (whats_returned, 1);
8234 else if (CONVERT_EXPR_P (whats_returned)
8235 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8236 whats_returned = TREE_OPERAND (whats_returned, 0);
8237 else
8238 break;
8241 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8242 return;
8243 whats_returned = TREE_OPERAND (whats_returned, 0);
8245 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8247 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8248 || TREE_CODE (whats_returned) == TARGET_EXPR)
8250 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8251 return;
8253 if (VAR_P (whats_returned)
8254 && DECL_NAME (whats_returned)
8255 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8257 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8258 return;
8262 while (TREE_CODE (whats_returned) == COMPONENT_REF
8263 || TREE_CODE (whats_returned) == ARRAY_REF)
8264 whats_returned = TREE_OPERAND (whats_returned, 0);
8266 if (DECL_P (whats_returned)
8267 && DECL_NAME (whats_returned)
8268 && DECL_FUNCTION_SCOPE_P (whats_returned)
8269 && !is_capture_proxy (whats_returned)
8270 && !(TREE_STATIC (whats_returned)
8271 || TREE_PUBLIC (whats_returned)))
8273 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8274 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8275 whats_returned);
8276 else
8277 warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
8278 whats_returned);
8279 return;
8283 /* Check that returning RETVAL from the current function is valid.
8284 Return an expression explicitly showing all conversions required to
8285 change RETVAL into the function return type, and to assign it to
8286 the DECL_RESULT for the function. Set *NO_WARNING to true if
8287 code reaches end of non-void function warning shouldn't be issued
8288 on this RETURN_EXPR. */
8290 tree
8291 check_return_expr (tree retval, bool *no_warning)
8293 tree result;
8294 /* The type actually returned by the function. */
8295 tree valtype;
8296 /* The type the function is declared to return, or void if
8297 the declared type is incomplete. */
8298 tree functype;
8299 int fn_returns_value_p;
8300 bool named_return_value_okay_p;
8302 *no_warning = false;
8304 if (flag_enable_cilkplus && retval && TREE_CODE (retval) == CILK_SPAWN_STMT)
8306 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8307 "statement is not allowed");
8308 return NULL_TREE;
8311 /* A `volatile' function is one that isn't supposed to return, ever.
8312 (This is a G++ extension, used to get better code for functions
8313 that call the `volatile' function.) */
8314 if (TREE_THIS_VOLATILE (current_function_decl))
8315 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8317 /* Check for various simple errors. */
8318 if (DECL_DESTRUCTOR_P (current_function_decl))
8320 if (retval)
8321 error ("returning a value from a destructor");
8322 return NULL_TREE;
8324 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8326 if (in_function_try_handler)
8327 /* If a return statement appears in a handler of the
8328 function-try-block of a constructor, the program is ill-formed. */
8329 error ("cannot return from a handler of a function-try-block of a constructor");
8330 else if (retval)
8331 /* You can't return a value from a constructor. */
8332 error ("returning a value from a constructor");
8333 return NULL_TREE;
8336 if (processing_template_decl)
8338 current_function_returns_value = 1;
8339 if (check_for_bare_parameter_packs (retval))
8340 retval = error_mark_node;
8341 return retval;
8344 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8346 /* Deduce auto return type from a return statement. */
8347 if (current_function_auto_return_pattern)
8349 tree auto_node;
8350 tree type;
8352 if (!retval && !is_auto (current_function_auto_return_pattern))
8354 /* Give a helpful error message. */
8355 error ("return-statement with no value, in function returning %qT",
8356 current_function_auto_return_pattern);
8357 inform (input_location, "only plain %<auto%> return type can be "
8358 "deduced to %<void%>");
8359 type = error_mark_node;
8361 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8363 error ("returning initializer list");
8364 type = error_mark_node;
8366 else
8368 if (!retval)
8369 retval = void_zero_node;
8370 auto_node = type_uses_auto (current_function_auto_return_pattern);
8371 type = do_auto_deduction (current_function_auto_return_pattern,
8372 retval, auto_node);
8375 if (type == error_mark_node)
8376 /* Leave it. */;
8377 else if (functype == current_function_auto_return_pattern)
8378 apply_deduced_return_type (current_function_decl, type);
8379 else
8380 /* A mismatch should have been diagnosed in do_auto_deduction. */
8381 gcc_assert (same_type_p (type, functype));
8382 functype = type;
8385 /* When no explicit return-value is given in a function with a named
8386 return value, the named return value is used. */
8387 result = DECL_RESULT (current_function_decl);
8388 valtype = TREE_TYPE (result);
8389 gcc_assert (valtype != NULL_TREE);
8390 fn_returns_value_p = !VOID_TYPE_P (valtype);
8391 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8392 retval = result;
8394 /* Check for a return statement with no return value in a function
8395 that's supposed to return a value. */
8396 if (!retval && fn_returns_value_p)
8398 if (functype != error_mark_node)
8399 permerror (input_location, "return-statement with no value, in "
8400 "function returning %qT", valtype);
8401 /* Remember that this function did return. */
8402 current_function_returns_value = 1;
8403 /* And signal caller that TREE_NO_WARNING should be set on the
8404 RETURN_EXPR to avoid control reaches end of non-void function
8405 warnings in tree-cfg.c. */
8406 *no_warning = true;
8408 /* Check for a return statement with a value in a function that
8409 isn't supposed to return a value. */
8410 else if (retval && !fn_returns_value_p)
8412 if (VOID_TYPE_P (TREE_TYPE (retval)))
8413 /* You can return a `void' value from a function of `void'
8414 type. In that case, we have to evaluate the expression for
8415 its side-effects. */
8416 finish_expr_stmt (retval);
8417 else
8418 permerror (input_location, "return-statement with a value, in function "
8419 "returning 'void'");
8420 current_function_returns_null = 1;
8422 /* There's really no value to return, after all. */
8423 return NULL_TREE;
8425 else if (!retval)
8426 /* Remember that this function can sometimes return without a
8427 value. */
8428 current_function_returns_null = 1;
8429 else
8430 /* Remember that this function did return a value. */
8431 current_function_returns_value = 1;
8433 /* Check for erroneous operands -- but after giving ourselves a
8434 chance to provide an error about returning a value from a void
8435 function. */
8436 if (error_operand_p (retval))
8438 current_function_return_value = error_mark_node;
8439 return error_mark_node;
8442 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8443 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8444 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8445 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8446 && ! flag_check_new
8447 && retval && null_ptr_cst_p (retval))
8448 warning (0, "%<operator new%> must not return NULL unless it is "
8449 "declared %<throw()%> (or -fcheck-new is in effect)");
8451 /* Effective C++ rule 15. See also start_function. */
8452 if (warn_ecpp
8453 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8455 bool warn = true;
8457 /* The function return type must be a reference to the current
8458 class. */
8459 if (TREE_CODE (valtype) == REFERENCE_TYPE
8460 && same_type_ignoring_top_level_qualifiers_p
8461 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8463 /* Returning '*this' is obviously OK. */
8464 if (retval == current_class_ref)
8465 warn = false;
8466 /* If we are calling a function whose return type is the same of
8467 the current class reference, it is ok. */
8468 else if (INDIRECT_REF_P (retval)
8469 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8470 warn = false;
8473 if (warn)
8474 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8477 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8479 [...] For a function with a class return type, if the expression
8480 in the return statement is the name of a local object, and the cv-
8481 unqualified type of the local object is the same as the function
8482 return type, an implementation is permitted to omit creating the tem-
8483 porary object to hold the function return value [...]
8485 So, if this is a value-returning function that always returns the same
8486 local variable, remember it.
8488 It might be nice to be more flexible, and choose the first suitable
8489 variable even if the function sometimes returns something else, but
8490 then we run the risk of clobbering the variable we chose if the other
8491 returned expression uses the chosen variable somehow. And people expect
8492 this restriction, anyway. (jason 2000-11-19)
8494 See finish_function and finalize_nrv for the rest of this optimization. */
8496 named_return_value_okay_p =
8497 (retval != NULL_TREE
8498 /* Must be a local, automatic variable. */
8499 && VAR_P (retval)
8500 && DECL_CONTEXT (retval) == current_function_decl
8501 && ! TREE_STATIC (retval)
8502 /* And not a lambda or anonymous union proxy. */
8503 && !DECL_HAS_VALUE_EXPR_P (retval)
8504 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8505 /* The cv-unqualified type of the returned value must be the
8506 same as the cv-unqualified return type of the
8507 function. */
8508 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8509 (TYPE_MAIN_VARIANT (functype)))
8510 /* And the returned value must be non-volatile. */
8511 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8513 if (fn_returns_value_p && flag_elide_constructors)
8515 if (named_return_value_okay_p
8516 && (current_function_return_value == NULL_TREE
8517 || current_function_return_value == retval))
8518 current_function_return_value = retval;
8519 else
8520 current_function_return_value = error_mark_node;
8523 /* We don't need to do any conversions when there's nothing being
8524 returned. */
8525 if (!retval)
8526 return NULL_TREE;
8528 /* Do any required conversions. */
8529 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8530 /* No conversions are required. */
8532 else
8534 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8536 /* The functype's return type will have been set to void, if it
8537 was an incomplete type. Just treat this as 'return;' */
8538 if (VOID_TYPE_P (functype))
8539 return error_mark_node;
8541 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8542 treated as an rvalue for the purposes of overload resolution to
8543 favor move constructors over copy constructors.
8545 Note that these conditions are similar to, but not as strict as,
8546 the conditions for the named return value optimization. */
8547 if ((cxx_dialect != cxx98)
8548 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8549 || TREE_CODE (retval) == PARM_DECL)
8550 && DECL_CONTEXT (retval) == current_function_decl
8551 && !TREE_STATIC (retval)
8552 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8553 (TYPE_MAIN_VARIANT (functype)))
8554 /* This is only interesting for class type. */
8555 && CLASS_TYPE_P (functype))
8556 flags = flags | LOOKUP_PREFER_RVALUE;
8558 /* First convert the value to the function's return type, then
8559 to the type of return value's location to handle the
8560 case that functype is smaller than the valtype. */
8561 retval = convert_for_initialization
8562 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8563 tf_warning_or_error);
8564 retval = convert (valtype, retval);
8566 /* If the conversion failed, treat this just like `return;'. */
8567 if (retval == error_mark_node)
8568 return retval;
8569 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8570 else if (! cfun->returns_struct
8571 && TREE_CODE (retval) == TARGET_EXPR
8572 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8573 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8574 TREE_OPERAND (retval, 0));
8575 else
8576 maybe_warn_about_returning_address_of_local (retval);
8579 /* Actually copy the value returned into the appropriate location. */
8580 if (retval && retval != result)
8581 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8583 return retval;
8587 /* Returns nonzero if the pointer-type FROM can be converted to the
8588 pointer-type TO via a qualification conversion. If CONSTP is -1,
8589 then we return nonzero if the pointers are similar, and the
8590 cv-qualification signature of FROM is a proper subset of that of TO.
8592 If CONSTP is positive, then all outer pointers have been
8593 const-qualified. */
8595 static int
8596 comp_ptr_ttypes_real (tree to, tree from, int constp)
8598 bool to_more_cv_qualified = false;
8599 bool is_opaque_pointer = false;
8601 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8603 if (TREE_CODE (to) != TREE_CODE (from))
8604 return 0;
8606 if (TREE_CODE (from) == OFFSET_TYPE
8607 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8608 TYPE_OFFSET_BASETYPE (to)))
8609 return 0;
8611 /* Const and volatile mean something different for function types,
8612 so the usual checks are not appropriate. */
8613 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8615 if (!at_least_as_qualified_p (to, from))
8616 return 0;
8618 if (!at_least_as_qualified_p (from, to))
8620 if (constp == 0)
8621 return 0;
8622 to_more_cv_qualified = true;
8625 if (constp > 0)
8626 constp &= TYPE_READONLY (to);
8629 if (TREE_CODE (to) == VECTOR_TYPE)
8630 is_opaque_pointer = vector_targets_convertible_p (to, from);
8632 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8633 return ((constp >= 0 || to_more_cv_qualified)
8634 && (is_opaque_pointer
8635 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8639 /* When comparing, say, char ** to char const **, this function takes
8640 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8641 types to this function. */
8644 comp_ptr_ttypes (tree to, tree from)
8646 return comp_ptr_ttypes_real (to, from, 1);
8649 /* Returns true iff FNTYPE is a non-class type that involves
8650 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8651 if a parameter type is ill-formed. */
8653 bool
8654 error_type_p (const_tree type)
8656 tree t;
8658 switch (TREE_CODE (type))
8660 case ERROR_MARK:
8661 return true;
8663 case POINTER_TYPE:
8664 case REFERENCE_TYPE:
8665 case OFFSET_TYPE:
8666 return error_type_p (TREE_TYPE (type));
8668 case FUNCTION_TYPE:
8669 case METHOD_TYPE:
8670 if (error_type_p (TREE_TYPE (type)))
8671 return true;
8672 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8673 if (error_type_p (TREE_VALUE (t)))
8674 return true;
8675 return false;
8677 case RECORD_TYPE:
8678 if (TYPE_PTRMEMFUNC_P (type))
8679 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8680 return false;
8682 default:
8683 return false;
8687 /* Returns true if to and from are (possibly multi-level) pointers to the same
8688 type or inheritance-related types, regardless of cv-quals. */
8690 bool
8691 ptr_reasonably_similar (const_tree to, const_tree from)
8693 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8695 /* Any target type is similar enough to void. */
8696 if (VOID_TYPE_P (to))
8697 return !error_type_p (from);
8698 if (VOID_TYPE_P (from))
8699 return !error_type_p (to);
8701 if (TREE_CODE (to) != TREE_CODE (from))
8702 return false;
8704 if (TREE_CODE (from) == OFFSET_TYPE
8705 && comptypes (TYPE_OFFSET_BASETYPE (to),
8706 TYPE_OFFSET_BASETYPE (from),
8707 COMPARE_BASE | COMPARE_DERIVED))
8708 continue;
8710 if (TREE_CODE (to) == VECTOR_TYPE
8711 && vector_types_convertible_p (to, from, false))
8712 return true;
8714 if (TREE_CODE (to) == INTEGER_TYPE
8715 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8716 return true;
8718 if (TREE_CODE (to) == FUNCTION_TYPE)
8719 return !error_type_p (to) && !error_type_p (from);
8721 if (!TYPE_PTR_P (to))
8723 /* When either type is incomplete avoid DERIVED_FROM_P,
8724 which may call complete_type (c++/57942). */
8725 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8726 return comptypes
8727 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8728 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8733 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8734 pointer-to-member types) are the same, ignoring cv-qualification at
8735 all levels. */
8737 bool
8738 comp_ptr_ttypes_const (tree to, tree from)
8740 bool is_opaque_pointer = false;
8742 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8744 if (TREE_CODE (to) != TREE_CODE (from))
8745 return false;
8747 if (TREE_CODE (from) == OFFSET_TYPE
8748 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8749 TYPE_OFFSET_BASETYPE (to)))
8750 continue;
8752 if (TREE_CODE (to) == VECTOR_TYPE)
8753 is_opaque_pointer = vector_targets_convertible_p (to, from);
8755 if (!TYPE_PTR_P (to))
8756 return (is_opaque_pointer
8757 || same_type_ignoring_top_level_qualifiers_p (to, from));
8761 /* Returns the type qualifiers for this type, including the qualifiers on the
8762 elements for an array type. */
8765 cp_type_quals (const_tree type)
8767 int quals;
8768 /* This CONST_CAST is okay because strip_array_types returns its
8769 argument unmodified and we assign it to a const_tree. */
8770 type = strip_array_types (CONST_CAST_TREE (type));
8771 if (type == error_mark_node
8772 /* Quals on a FUNCTION_TYPE are memfn quals. */
8773 || TREE_CODE (type) == FUNCTION_TYPE)
8774 return TYPE_UNQUALIFIED;
8775 quals = TYPE_QUALS (type);
8776 /* METHOD and REFERENCE_TYPEs should never have quals. */
8777 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8778 && TREE_CODE (type) != REFERENCE_TYPE)
8779 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8780 == TYPE_UNQUALIFIED));
8781 return quals;
8784 /* Returns the function-ref-qualifier for TYPE */
8786 cp_ref_qualifier
8787 type_memfn_rqual (const_tree type)
8789 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8790 || TREE_CODE (type) == METHOD_TYPE);
8792 if (!FUNCTION_REF_QUALIFIED (type))
8793 return REF_QUAL_NONE;
8794 else if (FUNCTION_RVALUE_QUALIFIED (type))
8795 return REF_QUAL_RVALUE;
8796 else
8797 return REF_QUAL_LVALUE;
8800 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8801 METHOD_TYPE. */
8804 type_memfn_quals (const_tree type)
8806 if (TREE_CODE (type) == FUNCTION_TYPE)
8807 return TYPE_QUALS (type);
8808 else if (TREE_CODE (type) == METHOD_TYPE)
8809 return cp_type_quals (class_of_this_parm (type));
8810 else
8811 gcc_unreachable ();
8814 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8815 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8817 tree
8818 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8820 /* Could handle METHOD_TYPE here if necessary. */
8821 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8822 if (TYPE_QUALS (type) == memfn_quals
8823 && type_memfn_rqual (type) == rqual)
8824 return type;
8826 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8827 complex. */
8828 tree result = build_qualified_type (type, memfn_quals);
8829 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8830 return build_ref_qualified_type (result, rqual);
8833 /* Returns nonzero if TYPE is const or volatile. */
8835 bool
8836 cv_qualified_p (const_tree type)
8838 int quals = cp_type_quals (type);
8839 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8842 /* Returns nonzero if the TYPE contains a mutable member. */
8844 bool
8845 cp_has_mutable_p (const_tree type)
8847 /* This CONST_CAST is okay because strip_array_types returns its
8848 argument unmodified and we assign it to a const_tree. */
8849 type = strip_array_types (CONST_CAST_TREE(type));
8851 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8854 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8855 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8856 approximation. In particular, consider:
8858 int f();
8859 struct S { int i; };
8860 const S s = { f(); }
8862 Here, we will make "s" as TREE_READONLY (because it is declared
8863 "const") -- only to reverse ourselves upon seeing that the
8864 initializer is non-constant. */
8866 void
8867 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8869 tree type = TREE_TYPE (decl);
8871 if (type == error_mark_node)
8872 return;
8874 if (TREE_CODE (decl) == TYPE_DECL)
8875 return;
8877 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8878 && type_quals != TYPE_UNQUALIFIED));
8880 /* Avoid setting TREE_READONLY incorrectly. */
8881 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8882 constructor can produce constant init, so rely on cp_finish_decl to
8883 clear TREE_READONLY if the variable has non-constant init. */
8885 /* If the type has (or might have) a mutable component, that component
8886 might be modified. */
8887 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8888 type_quals &= ~TYPE_QUAL_CONST;
8890 c_apply_type_quals_to_decl (type_quals, decl);
8893 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8894 exemplar types such that casting T1 to T2 is casting away constness
8895 if and only if there is no implicit conversion from T1 to T2. */
8897 static void
8898 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8900 int quals1;
8901 int quals2;
8903 /* [expr.const.cast]
8905 For multi-level pointer to members and multi-level mixed pointers
8906 and pointers to members (conv.qual), the "member" aspect of a
8907 pointer to member level is ignored when determining if a const
8908 cv-qualifier has been cast away. */
8909 /* [expr.const.cast]
8911 For two pointer types:
8913 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8914 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8915 K is min(N,M)
8917 casting from X1 to X2 casts away constness if, for a non-pointer
8918 type T there does not exist an implicit conversion (clause
8919 _conv_) from:
8921 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8925 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8926 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8927 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8929 *t1 = cp_build_qualified_type (void_type_node,
8930 cp_type_quals (*t1));
8931 *t2 = cp_build_qualified_type (void_type_node,
8932 cp_type_quals (*t2));
8933 return;
8936 quals1 = cp_type_quals (*t1);
8937 quals2 = cp_type_quals (*t2);
8939 if (TYPE_PTRDATAMEM_P (*t1))
8940 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8941 else
8942 *t1 = TREE_TYPE (*t1);
8943 if (TYPE_PTRDATAMEM_P (*t2))
8944 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8945 else
8946 *t2 = TREE_TYPE (*t2);
8948 casts_away_constness_r (t1, t2, complain);
8949 *t1 = build_pointer_type (*t1);
8950 *t2 = build_pointer_type (*t2);
8951 *t1 = cp_build_qualified_type (*t1, quals1);
8952 *t2 = cp_build_qualified_type (*t2, quals2);
8955 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8956 constness.
8958 ??? This function returns non-zero if casting away qualifiers not
8959 just const. We would like to return to the caller exactly which
8960 qualifiers are casted away to give more accurate diagnostics.
8963 static bool
8964 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8966 if (TREE_CODE (t2) == REFERENCE_TYPE)
8968 /* [expr.const.cast]
8970 Casting from an lvalue of type T1 to an lvalue of type T2
8971 using a reference cast casts away constness if a cast from an
8972 rvalue of type "pointer to T1" to the type "pointer to T2"
8973 casts away constness. */
8974 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8975 return casts_away_constness (build_pointer_type (t1),
8976 build_pointer_type (TREE_TYPE (t2)),
8977 complain);
8980 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
8981 /* [expr.const.cast]
8983 Casting from an rvalue of type "pointer to data member of X
8984 of type T1" to the type "pointer to data member of Y of type
8985 T2" casts away constness if a cast from an rvalue of type
8986 "pointer to T1" to the type "pointer to T2" casts away
8987 constness. */
8988 return casts_away_constness
8989 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8990 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
8991 complain);
8993 /* Casting away constness is only something that makes sense for
8994 pointer or reference types. */
8995 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
8996 return false;
8998 /* Top-level qualifiers don't matter. */
8999 t1 = TYPE_MAIN_VARIANT (t1);
9000 t2 = TYPE_MAIN_VARIANT (t2);
9001 casts_away_constness_r (&t1, &t2, complain);
9002 if (!can_convert (t2, t1, complain))
9003 return true;
9005 return false;
9008 /* If T is a REFERENCE_TYPE return the type to which T refers.
9009 Otherwise, return T itself. */
9011 tree
9012 non_reference (tree t)
9014 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9015 t = TREE_TYPE (t);
9016 return t;
9020 /* Return nonzero if REF is an lvalue valid for this language;
9021 otherwise, print an error message and return zero. USE says
9022 how the lvalue is being used and so selects the error message. */
9025 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9027 cp_lvalue_kind kind = lvalue_kind (ref);
9029 if (kind == clk_none)
9031 if (complain & tf_error)
9032 lvalue_error (input_location, use);
9033 return 0;
9035 else if (kind & (clk_rvalueref|clk_class))
9037 if (!(complain & tf_error))
9038 return 0;
9039 if (kind & clk_class)
9040 /* Make this a permerror because we used to accept it. */
9041 permerror (input_location, "using temporary as lvalue");
9042 else
9043 error ("using xvalue (rvalue reference) as lvalue");
9045 return 1;
9048 /* Return true if a user-defined literal operator is a raw operator. */
9050 bool
9051 check_raw_literal_operator (const_tree decl)
9053 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9054 tree argtype;
9055 int arity;
9056 bool maybe_raw_p = false;
9058 /* Count the number and type of arguments and check for ellipsis. */
9059 for (argtype = argtypes, arity = 0;
9060 argtype && argtype != void_list_node;
9061 ++arity, argtype = TREE_CHAIN (argtype))
9063 tree t = TREE_VALUE (argtype);
9065 if (same_type_p (t, const_string_type_node))
9066 maybe_raw_p = true;
9068 if (!argtype)
9069 return false; /* Found ellipsis. */
9071 if (!maybe_raw_p || arity != 1)
9072 return false;
9074 return true;
9078 /* Return true if a user-defined literal operator has one of the allowed
9079 argument types. */
9081 bool
9082 check_literal_operator_args (const_tree decl,
9083 bool *long_long_unsigned_p, bool *long_double_p)
9085 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9087 *long_long_unsigned_p = false;
9088 *long_double_p = false;
9089 if (processing_template_decl || processing_specialization)
9090 return argtypes == void_list_node;
9091 else
9093 tree argtype;
9094 int arity;
9095 int max_arity = 2;
9097 /* Count the number and type of arguments and check for ellipsis. */
9098 for (argtype = argtypes, arity = 0;
9099 argtype && argtype != void_list_node;
9100 argtype = TREE_CHAIN (argtype))
9102 tree t = TREE_VALUE (argtype);
9103 ++arity;
9105 if (TYPE_PTR_P (t))
9107 bool maybe_raw_p = false;
9108 t = TREE_TYPE (t);
9109 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9110 return false;
9111 t = TYPE_MAIN_VARIANT (t);
9112 if ((maybe_raw_p = same_type_p (t, char_type_node))
9113 || same_type_p (t, wchar_type_node)
9114 || same_type_p (t, char16_type_node)
9115 || same_type_p (t, char32_type_node))
9117 argtype = TREE_CHAIN (argtype);
9118 if (!argtype)
9119 return false;
9120 t = TREE_VALUE (argtype);
9121 if (maybe_raw_p && argtype == void_list_node)
9122 return true;
9123 else if (same_type_p (t, size_type_node))
9125 ++arity;
9126 continue;
9128 else
9129 return false;
9132 else if (same_type_p (t, long_long_unsigned_type_node))
9134 max_arity = 1;
9135 *long_long_unsigned_p = true;
9137 else if (same_type_p (t, long_double_type_node))
9139 max_arity = 1;
9140 *long_double_p = true;
9142 else if (same_type_p (t, char_type_node))
9143 max_arity = 1;
9144 else if (same_type_p (t, wchar_type_node))
9145 max_arity = 1;
9146 else if (same_type_p (t, char16_type_node))
9147 max_arity = 1;
9148 else if (same_type_p (t, char32_type_node))
9149 max_arity = 1;
9150 else
9151 return false;
9153 if (!argtype)
9154 return false; /* Found ellipsis. */
9156 if (arity != max_arity)
9157 return false;
9159 return true;