* xvasprintf.c: New file.
[official-gcc.git] / gcc / cp / typeck.c
blob2abff6bdb76edd04ace5d11237fde078276100a3
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "diagnostic.h"
37 #include "intl.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "c-family/c-ubsan.h"
43 #include "params.h"
45 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
46 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
47 static tree pfn_from_ptrmemfunc (tree);
48 static tree delta_from_ptrmemfunc (tree);
49 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
50 tsubst_flags_t, int);
51 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
52 static tree rationalize_conditional_expr (enum tree_code, tree,
53 tsubst_flags_t);
54 static int comp_ptr_ttypes_real (tree, tree, int);
55 static bool comp_except_types (tree, tree, bool);
56 static bool comp_array_types (const_tree, const_tree, bool);
57 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
58 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 static bool maybe_warn_about_returning_address_of_local (tree);
62 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
63 static void warn_args_num (location_t, tree, bool);
64 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
65 tsubst_flags_t);
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.)
69 Returns error_mark_node if the VALUE does not have
70 complete type when this function returns. */
72 tree
73 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
75 tree type;
77 if (processing_template_decl || value == error_mark_node)
78 return value;
80 if (TREE_CODE (value) == OVERLOAD)
81 type = unknown_type_node;
82 else
83 type = TREE_TYPE (value);
85 if (type == error_mark_node)
86 return error_mark_node;
88 /* First, detect a valid value with a complete type. */
89 if (COMPLETE_TYPE_P (type))
90 return value;
92 if (complete_type_or_maybe_complain (type, value, complain))
93 return value;
94 else
95 return error_mark_node;
98 tree
99 require_complete_type (tree value)
101 return require_complete_type_sfinae (value, tf_warning_or_error);
104 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
105 a template instantiation, do the instantiation. Returns TYPE,
106 whether or not it could be completed, unless something goes
107 horribly wrong, in which case the error_mark_node is returned. */
109 tree
110 complete_type (tree type)
112 if (type == NULL_TREE)
113 /* Rather than crash, we return something sure to cause an error
114 at some point. */
115 return error_mark_node;
117 if (type == error_mark_node || COMPLETE_TYPE_P (type))
119 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
121 tree t = complete_type (TREE_TYPE (type));
122 unsigned int needs_constructing, has_nontrivial_dtor;
123 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
124 layout_type (type);
125 needs_constructing
126 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
127 has_nontrivial_dtor
128 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
129 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
131 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
132 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
135 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
136 instantiate_class_template (TYPE_MAIN_VARIANT (type));
138 return type;
141 /* Like complete_type, but issue an error if the TYPE cannot be completed.
142 VALUE is used for informative diagnostics.
143 Returns NULL_TREE if the type cannot be made complete. */
145 tree
146 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
148 type = complete_type (type);
149 if (type == error_mark_node)
150 /* We already issued an error. */
151 return NULL_TREE;
152 else if (!COMPLETE_TYPE_P (type))
154 if (complain & tf_error)
155 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
156 return NULL_TREE;
158 else
159 return type;
162 tree
163 complete_type_or_else (tree type, tree value)
165 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
168 /* Return truthvalue of whether type of EXP is instantiated. */
171 type_unknown_p (const_tree exp)
173 return (TREE_CODE (exp) == TREE_LIST
174 || TREE_TYPE (exp) == unknown_type_node);
178 /* Return the common type of two parameter lists.
179 We assume that comptypes has already been done and returned 1;
180 if that isn't so, this may crash.
182 As an optimization, free the space we allocate if the parameter
183 lists are already common. */
185 static tree
186 commonparms (tree p1, tree p2)
188 tree oldargs = p1, newargs, n;
189 int i, len;
190 int any_change = 0;
192 len = list_length (p1);
193 newargs = tree_last (p1);
195 if (newargs == void_list_node)
196 i = 1;
197 else
199 i = 0;
200 newargs = 0;
203 for (; i < len; i++)
204 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
206 n = newargs;
208 for (i = 0; p1;
209 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
211 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
213 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
214 any_change = 1;
216 else if (! TREE_PURPOSE (p1))
218 if (TREE_PURPOSE (p2))
220 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
221 any_change = 1;
224 else
226 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
227 any_change = 1;
228 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
230 if (TREE_VALUE (p1) != TREE_VALUE (p2))
232 any_change = 1;
233 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
235 else
236 TREE_VALUE (n) = TREE_VALUE (p1);
238 if (! any_change)
239 return oldargs;
241 return newargs;
244 /* Given a type, perhaps copied for a typedef,
245 find the "original" version of it. */
246 static tree
247 original_type (tree t)
249 int quals = cp_type_quals (t);
250 while (t != error_mark_node
251 && TYPE_NAME (t) != NULL_TREE)
253 tree x = TYPE_NAME (t);
254 if (TREE_CODE (x) != TYPE_DECL)
255 break;
256 x = DECL_ORIGINAL_TYPE (x);
257 if (x == NULL_TREE)
258 break;
259 t = x;
261 return cp_build_qualified_type (t, quals);
264 /* Return the common type for two arithmetic types T1 and T2 under the
265 usual arithmetic conversions. The default conversions have already
266 been applied, and enumerated types converted to their compatible
267 integer types. */
269 static tree
270 cp_common_type (tree t1, tree t2)
272 enum tree_code code1 = TREE_CODE (t1);
273 enum tree_code code2 = TREE_CODE (t2);
274 tree attributes;
275 int i;
278 /* In what follows, we slightly generalize the rules given in [expr] so
279 as to deal with `long long' and `complex'. First, merge the
280 attributes. */
281 attributes = (*targetm.merge_type_attributes) (t1, t2);
283 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
285 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
286 return build_type_attribute_variant (t1, attributes);
287 else
288 return NULL_TREE;
291 /* FIXME: Attributes. */
292 gcc_assert (ARITHMETIC_TYPE_P (t1)
293 || TREE_CODE (t1) == VECTOR_TYPE
294 || UNSCOPED_ENUM_P (t1));
295 gcc_assert (ARITHMETIC_TYPE_P (t2)
296 || TREE_CODE (t2) == VECTOR_TYPE
297 || UNSCOPED_ENUM_P (t2));
299 /* If one type is complex, form the common type of the non-complex
300 components, then make that complex. Use T1 or T2 if it is the
301 required type. */
302 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
304 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
305 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
306 tree subtype
307 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
309 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
310 return build_type_attribute_variant (t1, attributes);
311 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
312 return build_type_attribute_variant (t2, attributes);
313 else
314 return build_type_attribute_variant (build_complex_type (subtype),
315 attributes);
318 if (code1 == VECTOR_TYPE)
320 /* When we get here we should have two vectors of the same size.
321 Just prefer the unsigned one if present. */
322 if (TYPE_UNSIGNED (t1))
323 return build_type_attribute_variant (t1, attributes);
324 else
325 return build_type_attribute_variant (t2, attributes);
328 /* If only one is real, use it as the result. */
329 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
330 return build_type_attribute_variant (t1, attributes);
331 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
332 return build_type_attribute_variant (t2, attributes);
334 /* Both real or both integers; use the one with greater precision. */
335 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
336 return build_type_attribute_variant (t1, attributes);
337 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
338 return build_type_attribute_variant (t2, attributes);
340 /* The types are the same; no need to do anything fancy. */
341 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
342 return build_type_attribute_variant (t1, attributes);
344 if (code1 != REAL_TYPE)
346 /* If one is unsigned long long, then convert the other to unsigned
347 long long. */
348 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
349 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
350 return build_type_attribute_variant (long_long_unsigned_type_node,
351 attributes);
352 /* If one is a long long, and the other is an unsigned long, and
353 long long can represent all the values of an unsigned long, then
354 convert to a long long. Otherwise, convert to an unsigned long
355 long. Otherwise, if either operand is long long, convert the
356 other to long long.
358 Since we're here, we know the TYPE_PRECISION is the same;
359 therefore converting to long long cannot represent all the values
360 of an unsigned long, so we choose unsigned long long in that
361 case. */
362 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
363 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
365 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
366 ? long_long_unsigned_type_node
367 : long_long_integer_type_node);
368 return build_type_attribute_variant (t, attributes);
371 /* Go through the same procedure, but for longs. */
372 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
373 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
374 return build_type_attribute_variant (long_unsigned_type_node,
375 attributes);
376 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
377 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
379 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
380 ? long_unsigned_type_node : long_integer_type_node);
381 return build_type_attribute_variant (t, attributes);
384 /* For __intN types, either the type is __int128 (and is lower
385 priority than the types checked above, but higher than other
386 128-bit types) or it's known to not be the same size as other
387 types (enforced in toplev.c). Prefer the unsigned type. */
388 for (i = 0; i < NUM_INT_N_ENTS; i ++)
390 if (int_n_enabled_p [i]
391 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
392 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
393 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
394 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
396 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
397 ? int_n_trees[i].unsigned_type
398 : int_n_trees[i].signed_type);
399 return build_type_attribute_variant (t, attributes);
403 /* Otherwise prefer the unsigned one. */
404 if (TYPE_UNSIGNED (t1))
405 return build_type_attribute_variant (t1, attributes);
406 else
407 return build_type_attribute_variant (t2, attributes);
409 else
411 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
412 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
413 return build_type_attribute_variant (long_double_type_node,
414 attributes);
415 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
416 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
417 return build_type_attribute_variant (double_type_node,
418 attributes);
419 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
420 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
421 return build_type_attribute_variant (float_type_node,
422 attributes);
424 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
425 the standard C++ floating-point types. Logic earlier in this
426 function has already eliminated the possibility that
427 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
428 compelling reason to choose one or the other. */
429 return build_type_attribute_variant (t1, attributes);
433 /* T1 and T2 are arithmetic or enumeration types. Return the type
434 that will result from the "usual arithmetic conversions" on T1 and
435 T2 as described in [expr]. */
437 tree
438 type_after_usual_arithmetic_conversions (tree t1, tree t2)
440 gcc_assert (ARITHMETIC_TYPE_P (t1)
441 || TREE_CODE (t1) == VECTOR_TYPE
442 || UNSCOPED_ENUM_P (t1));
443 gcc_assert (ARITHMETIC_TYPE_P (t2)
444 || TREE_CODE (t2) == VECTOR_TYPE
445 || UNSCOPED_ENUM_P (t2));
447 /* Perform the integral promotions. We do not promote real types here. */
448 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
449 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
451 t1 = type_promotes_to (t1);
452 t2 = type_promotes_to (t2);
455 return cp_common_type (t1, t2);
458 static void
459 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
460 composite_pointer_operation operation)
462 switch (operation)
464 case CPO_COMPARISON:
465 emit_diagnostic (kind, input_location, 0,
466 "comparison between "
467 "distinct pointer types %qT and %qT lacks a cast",
468 t1, t2);
469 break;
470 case CPO_CONVERSION:
471 emit_diagnostic (kind, input_location, 0,
472 "conversion between "
473 "distinct pointer types %qT and %qT lacks a cast",
474 t1, t2);
475 break;
476 case CPO_CONDITIONAL_EXPR:
477 emit_diagnostic (kind, input_location, 0,
478 "conditional expression between "
479 "distinct pointer types %qT and %qT lacks a cast",
480 t1, t2);
481 break;
482 default:
483 gcc_unreachable ();
487 /* Subroutine of composite_pointer_type to implement the recursive
488 case. See that function for documentation of the parameters. */
490 static tree
491 composite_pointer_type_r (tree t1, tree t2,
492 composite_pointer_operation operation,
493 tsubst_flags_t complain)
495 tree pointee1;
496 tree pointee2;
497 tree result_type;
498 tree attributes;
500 /* Determine the types pointed to by T1 and T2. */
501 if (TYPE_PTR_P (t1))
503 pointee1 = TREE_TYPE (t1);
504 pointee2 = TREE_TYPE (t2);
506 else
508 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
509 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
512 /* [expr.rel]
514 Otherwise, the composite pointer type is a pointer type
515 similar (_conv.qual_) to the type of one of the operands,
516 with a cv-qualification signature (_conv.qual_) that is the
517 union of the cv-qualification signatures of the operand
518 types. */
519 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
520 result_type = pointee1;
521 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
522 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
524 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
525 complain);
526 if (result_type == error_mark_node)
527 return error_mark_node;
529 else
531 if (complain & tf_error)
532 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
533 else
534 return error_mark_node;
535 result_type = void_type_node;
537 result_type = cp_build_qualified_type (result_type,
538 (cp_type_quals (pointee1)
539 | cp_type_quals (pointee2)));
540 /* If the original types were pointers to members, so is the
541 result. */
542 if (TYPE_PTRMEM_P (t1))
544 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
545 TYPE_PTRMEM_CLASS_TYPE (t2)))
547 if (complain & tf_error)
548 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
549 else
550 return error_mark_node;
552 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
553 result_type);
555 else
556 result_type = build_pointer_type (result_type);
558 /* Merge the attributes. */
559 attributes = (*targetm.merge_type_attributes) (t1, t2);
560 return build_type_attribute_variant (result_type, attributes);
563 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
564 ARG1 and ARG2 are the values with those types. The OPERATION is to
565 describe the operation between the pointer types,
566 in case an error occurs.
568 This routine also implements the computation of a common type for
569 pointers-to-members as per [expr.eq]. */
571 tree
572 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
573 composite_pointer_operation operation,
574 tsubst_flags_t complain)
576 tree class1;
577 tree class2;
579 /* [expr.rel]
581 If one operand is a null pointer constant, the composite pointer
582 type is the type of the other operand. */
583 if (null_ptr_cst_p (arg1))
584 return t2;
585 if (null_ptr_cst_p (arg2))
586 return t1;
588 /* We have:
590 [expr.rel]
592 If one of the operands has type "pointer to cv1 void*", then
593 the other has type "pointer to cv2T", and the composite pointer
594 type is "pointer to cv12 void", where cv12 is the union of cv1
595 and cv2.
597 If either type is a pointer to void, make sure it is T1. */
598 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
600 tree t;
601 t = t1;
602 t1 = t2;
603 t2 = t;
606 /* Now, if T1 is a pointer to void, merge the qualifiers. */
607 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
609 tree attributes;
610 tree result_type;
612 if (TYPE_PTRFN_P (t2))
614 if (complain & tf_error)
616 switch (operation)
618 case CPO_COMPARISON:
619 pedwarn (input_location, OPT_Wpedantic,
620 "ISO C++ forbids comparison between pointer "
621 "of type %<void *%> and pointer-to-function");
622 break;
623 case CPO_CONVERSION:
624 pedwarn (input_location, OPT_Wpedantic,
625 "ISO C++ forbids conversion between pointer "
626 "of type %<void *%> and pointer-to-function");
627 break;
628 case CPO_CONDITIONAL_EXPR:
629 pedwarn (input_location, OPT_Wpedantic,
630 "ISO C++ forbids conditional expression between "
631 "pointer of type %<void *%> and "
632 "pointer-to-function");
633 break;
634 default:
635 gcc_unreachable ();
638 else
639 return error_mark_node;
641 result_type
642 = cp_build_qualified_type (void_type_node,
643 (cp_type_quals (TREE_TYPE (t1))
644 | cp_type_quals (TREE_TYPE (t2))));
645 result_type = build_pointer_type (result_type);
646 /* Merge the attributes. */
647 attributes = (*targetm.merge_type_attributes) (t1, t2);
648 return build_type_attribute_variant (result_type, attributes);
651 if (c_dialect_objc () && TYPE_PTR_P (t1)
652 && TYPE_PTR_P (t2))
654 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
655 return objc_common_type (t1, t2);
658 /* [expr.eq] permits the application of a pointer conversion to
659 bring the pointers to a common type. */
660 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
661 && CLASS_TYPE_P (TREE_TYPE (t1))
662 && CLASS_TYPE_P (TREE_TYPE (t2))
663 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
664 TREE_TYPE (t2)))
666 class1 = TREE_TYPE (t1);
667 class2 = TREE_TYPE (t2);
669 if (DERIVED_FROM_P (class1, class2))
670 t2 = (build_pointer_type
671 (cp_build_qualified_type (class1, cp_type_quals (class2))));
672 else if (DERIVED_FROM_P (class2, class1))
673 t1 = (build_pointer_type
674 (cp_build_qualified_type (class2, cp_type_quals (class1))));
675 else
677 if (complain & tf_error)
678 composite_pointer_error (DK_ERROR, t1, t2, operation);
679 return error_mark_node;
682 /* [expr.eq] permits the application of a pointer-to-member
683 conversion to change the class type of one of the types. */
684 else if (TYPE_PTRMEM_P (t1)
685 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
686 TYPE_PTRMEM_CLASS_TYPE (t2)))
688 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
689 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
691 if (DERIVED_FROM_P (class1, class2))
692 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
693 else if (DERIVED_FROM_P (class2, class1))
694 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
695 else
697 if (complain & tf_error)
698 switch (operation)
700 case CPO_COMPARISON:
701 error ("comparison between distinct "
702 "pointer-to-member types %qT and %qT lacks a cast",
703 t1, t2);
704 break;
705 case CPO_CONVERSION:
706 error ("conversion between distinct "
707 "pointer-to-member types %qT and %qT lacks a cast",
708 t1, t2);
709 break;
710 case CPO_CONDITIONAL_EXPR:
711 error ("conditional expression between distinct "
712 "pointer-to-member types %qT and %qT lacks a cast",
713 t1, t2);
714 break;
715 default:
716 gcc_unreachable ();
718 return error_mark_node;
722 return composite_pointer_type_r (t1, t2, operation, complain);
725 /* Return the merged type of two types.
726 We assume that comptypes has already been done and returned 1;
727 if that isn't so, this may crash.
729 This just combines attributes and default arguments; any other
730 differences would cause the two types to compare unalike. */
732 tree
733 merge_types (tree t1, tree t2)
735 enum tree_code code1;
736 enum tree_code code2;
737 tree attributes;
739 /* Save time if the two types are the same. */
740 if (t1 == t2)
741 return t1;
742 if (original_type (t1) == original_type (t2))
743 return t1;
745 /* If one type is nonsense, use the other. */
746 if (t1 == error_mark_node)
747 return t2;
748 if (t2 == error_mark_node)
749 return t1;
751 /* Handle merging an auto redeclaration with a previous deduced
752 return type. */
753 if (is_auto (t1))
754 return t2;
756 /* Merge the attributes. */
757 attributes = (*targetm.merge_type_attributes) (t1, t2);
759 if (TYPE_PTRMEMFUNC_P (t1))
760 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
761 if (TYPE_PTRMEMFUNC_P (t2))
762 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
764 code1 = TREE_CODE (t1);
765 code2 = TREE_CODE (t2);
766 if (code1 != code2)
768 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
769 if (code1 == TYPENAME_TYPE)
771 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
772 code1 = TREE_CODE (t1);
774 else
776 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
777 code2 = TREE_CODE (t2);
781 switch (code1)
783 case POINTER_TYPE:
784 case REFERENCE_TYPE:
785 /* For two pointers, do this recursively on the target type. */
787 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
788 int quals = cp_type_quals (t1);
790 if (code1 == POINTER_TYPE)
791 t1 = build_pointer_type (target);
792 else
793 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
794 t1 = build_type_attribute_variant (t1, attributes);
795 t1 = cp_build_qualified_type (t1, quals);
797 if (TREE_CODE (target) == METHOD_TYPE)
798 t1 = build_ptrmemfunc_type (t1);
800 return t1;
803 case OFFSET_TYPE:
805 int quals;
806 tree pointee;
807 quals = cp_type_quals (t1);
808 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
809 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
810 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
811 pointee);
812 t1 = cp_build_qualified_type (t1, quals);
813 break;
816 case ARRAY_TYPE:
818 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
819 /* Save space: see if the result is identical to one of the args. */
820 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
821 return build_type_attribute_variant (t1, attributes);
822 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
823 return build_type_attribute_variant (t2, attributes);
824 /* Merge the element types, and have a size if either arg has one. */
825 t1 = build_cplus_array_type
826 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
827 break;
830 case FUNCTION_TYPE:
831 /* Function types: prefer the one that specified arg types.
832 If both do, merge the arg types. Also merge the return types. */
834 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
835 tree p1 = TYPE_ARG_TYPES (t1);
836 tree p2 = TYPE_ARG_TYPES (t2);
837 tree parms;
838 tree rval, raises;
839 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
841 /* Save space: see if the result is identical to one of the args. */
842 if (valtype == TREE_TYPE (t1) && ! p2)
843 return cp_build_type_attribute_variant (t1, attributes);
844 if (valtype == TREE_TYPE (t2) && ! p1)
845 return cp_build_type_attribute_variant (t2, attributes);
847 /* Simple way if one arg fails to specify argument types. */
848 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
849 parms = p2;
850 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
851 parms = p1;
852 else
853 parms = commonparms (p1, p2);
855 rval = build_function_type (valtype, parms);
856 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
857 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
858 rval = apply_memfn_quals (rval,
859 type_memfn_quals (t1),
860 type_memfn_rqual (t1));
861 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
862 TYPE_RAISES_EXCEPTIONS (t2));
863 t1 = build_exception_variant (rval, raises);
864 if (late_return_type_p)
865 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
866 break;
869 case METHOD_TYPE:
871 /* Get this value the long way, since TYPE_METHOD_BASETYPE
872 is just the main variant of this. */
873 tree basetype = class_of_this_parm (t2);
874 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
875 TYPE_RAISES_EXCEPTIONS (t2));
876 cp_ref_qualifier rqual = type_memfn_rqual (t1);
877 tree t3;
878 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
879 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
881 /* If this was a member function type, get back to the
882 original type of type member function (i.e., without
883 the class instance variable up front. */
884 t1 = build_function_type (TREE_TYPE (t1),
885 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
886 t2 = build_function_type (TREE_TYPE (t2),
887 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
888 t3 = merge_types (t1, t2);
889 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
890 TYPE_ARG_TYPES (t3));
891 t1 = build_exception_variant (t3, raises);
892 t1 = build_ref_qualified_type (t1, rqual);
893 if (late_return_type_1_p)
894 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
895 if (late_return_type_2_p)
896 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
897 break;
900 case TYPENAME_TYPE:
901 /* There is no need to merge attributes into a TYPENAME_TYPE.
902 When the type is instantiated it will have whatever
903 attributes result from the instantiation. */
904 return t1;
906 default:;
909 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
910 return t1;
911 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
912 return t2;
913 else
914 return cp_build_type_attribute_variant (t1, attributes);
917 /* Return the ARRAY_TYPE type without its domain. */
919 tree
920 strip_array_domain (tree type)
922 tree t2;
923 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
924 if (TYPE_DOMAIN (type) == NULL_TREE)
925 return type;
926 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
927 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
930 /* Wrapper around cp_common_type that is used by c-common.c and other
931 front end optimizations that remove promotions.
933 Return the common type for two arithmetic types T1 and T2 under the
934 usual arithmetic conversions. The default conversions have already
935 been applied, and enumerated types converted to their compatible
936 integer types. */
938 tree
939 common_type (tree t1, tree t2)
941 /* If one type is nonsense, use the other */
942 if (t1 == error_mark_node)
943 return t2;
944 if (t2 == error_mark_node)
945 return t1;
947 return cp_common_type (t1, t2);
950 /* Return the common type of two pointer types T1 and T2. This is the
951 type for the result of most arithmetic operations if the operands
952 have the given two types.
954 We assume that comp_target_types has already been done and returned
955 nonzero; if that isn't so, this may crash. */
957 tree
958 common_pointer_type (tree t1, tree t2)
960 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
961 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
962 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
964 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
965 CPO_CONVERSION, tf_warning_or_error);
968 /* Compare two exception specifier types for exactness or subsetness, if
969 allowed. Returns false for mismatch, true for match (same, or
970 derived and !exact).
972 [except.spec] "If a class X ... objects of class X or any class publicly
973 and unambiguously derived from X. Similarly, if a pointer type Y * ...
974 exceptions of type Y * or that are pointers to any type publicly and
975 unambiguously derived from Y. Otherwise a function only allows exceptions
976 that have the same type ..."
977 This does not mention cv qualifiers and is different to what throw
978 [except.throw] and catch [except.catch] will do. They will ignore the
979 top level cv qualifiers, and allow qualifiers in the pointer to class
980 example.
982 We implement the letter of the standard. */
984 static bool
985 comp_except_types (tree a, tree b, bool exact)
987 if (same_type_p (a, b))
988 return true;
989 else if (!exact)
991 if (cp_type_quals (a) || cp_type_quals (b))
992 return false;
994 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
996 a = TREE_TYPE (a);
997 b = TREE_TYPE (b);
998 if (cp_type_quals (a) || cp_type_quals (b))
999 return false;
1002 if (TREE_CODE (a) != RECORD_TYPE
1003 || TREE_CODE (b) != RECORD_TYPE)
1004 return false;
1006 if (publicly_uniquely_derived_p (a, b))
1007 return true;
1009 return false;
1012 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1013 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1014 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1015 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1016 are unordered, but we've already filtered out duplicates. Most lists will
1017 be in order, we should try to make use of that. */
1019 bool
1020 comp_except_specs (const_tree t1, const_tree t2, int exact)
1022 const_tree probe;
1023 const_tree base;
1024 int length = 0;
1026 if (t1 == t2)
1027 return true;
1029 /* First handle noexcept. */
1030 if (exact < ce_exact)
1032 /* noexcept(false) is compatible with no exception-specification,
1033 and stricter than any spec. */
1034 if (t1 == noexcept_false_spec)
1035 return t2 == NULL_TREE || exact == ce_derived;
1036 /* Even a derived noexcept(false) is compatible with no
1037 exception-specification. */
1038 if (t2 == noexcept_false_spec)
1039 return t1 == NULL_TREE;
1041 /* Otherwise, if we aren't looking for an exact match, noexcept is
1042 equivalent to throw(). */
1043 if (t1 == noexcept_true_spec)
1044 t1 = empty_except_spec;
1045 if (t2 == noexcept_true_spec)
1046 t2 = empty_except_spec;
1049 /* If any noexcept is left, it is only comparable to itself;
1050 either we're looking for an exact match or we're redeclaring a
1051 template with dependent noexcept. */
1052 if ((t1 && TREE_PURPOSE (t1))
1053 || (t2 && TREE_PURPOSE (t2)))
1054 return (t1 && t2
1055 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1057 if (t1 == NULL_TREE) /* T1 is ... */
1058 return t2 == NULL_TREE || exact == ce_derived;
1059 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1060 return t2 != NULL_TREE && !TREE_VALUE (t2);
1061 if (t2 == NULL_TREE) /* T2 is ... */
1062 return false;
1063 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1064 return exact == ce_derived;
1066 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1067 Count how many we find, to determine exactness. For exact matching and
1068 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1069 O(nm). */
1070 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1072 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1074 tree a = TREE_VALUE (probe);
1075 tree b = TREE_VALUE (t2);
1077 if (comp_except_types (a, b, exact))
1079 if (probe == base && exact > ce_derived)
1080 base = TREE_CHAIN (probe);
1081 length++;
1082 break;
1085 if (probe == NULL_TREE)
1086 return false;
1088 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1091 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1092 [] can match [size]. */
1094 static bool
1095 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1097 tree d1;
1098 tree d2;
1099 tree max1, max2;
1101 if (t1 == t2)
1102 return true;
1104 /* The type of the array elements must be the same. */
1105 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1106 return false;
1108 d1 = TYPE_DOMAIN (t1);
1109 d2 = TYPE_DOMAIN (t2);
1111 if (d1 == d2)
1112 return true;
1114 /* If one of the arrays is dimensionless, and the other has a
1115 dimension, they are of different types. However, it is valid to
1116 write:
1118 extern int a[];
1119 int a[3];
1121 by [basic.link]:
1123 declarations for an array object can specify
1124 array types that differ by the presence or absence of a major
1125 array bound (_dcl.array_). */
1126 if (!d1 || !d2)
1127 return allow_redeclaration;
1129 /* Check that the dimensions are the same. */
1131 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1132 return false;
1133 max1 = TYPE_MAX_VALUE (d1);
1134 max2 = TYPE_MAX_VALUE (d2);
1136 if (!cp_tree_equal (max1, max2))
1137 return false;
1139 return true;
1142 /* Compare the relative position of T1 and T2 into their respective
1143 template parameter list.
1144 T1 and T2 must be template parameter types.
1145 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1147 static bool
1148 comp_template_parms_position (tree t1, tree t2)
1150 tree index1, index2;
1151 gcc_assert (t1 && t2
1152 && TREE_CODE (t1) == TREE_CODE (t2)
1153 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1154 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1155 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1157 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1158 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1160 /* Then compare their relative position. */
1161 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1162 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1163 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1164 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1165 return false;
1167 /* In C++14 we can end up comparing 'auto' to a normal template
1168 parameter. Don't confuse them. */
1169 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1170 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1172 return true;
1175 /* Subroutine in comptypes. */
1177 static bool
1178 structural_comptypes (tree t1, tree t2, int strict)
1180 if (t1 == t2)
1181 return true;
1183 /* Suppress errors caused by previously reported errors. */
1184 if (t1 == error_mark_node || t2 == error_mark_node)
1185 return false;
1187 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1189 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1190 current instantiation. */
1191 if (TREE_CODE (t1) == TYPENAME_TYPE)
1192 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1194 if (TREE_CODE (t2) == TYPENAME_TYPE)
1195 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1197 if (TYPE_PTRMEMFUNC_P (t1))
1198 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1199 if (TYPE_PTRMEMFUNC_P (t2))
1200 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1202 /* Different classes of types can't be compatible. */
1203 if (TREE_CODE (t1) != TREE_CODE (t2))
1204 return false;
1206 /* Qualifiers must match. For array types, we will check when we
1207 recur on the array element types. */
1208 if (TREE_CODE (t1) != ARRAY_TYPE
1209 && cp_type_quals (t1) != cp_type_quals (t2))
1210 return false;
1211 if (TREE_CODE (t1) == FUNCTION_TYPE
1212 && type_memfn_quals (t1) != type_memfn_quals (t2))
1213 return false;
1214 /* Need to check this before TYPE_MAIN_VARIANT.
1215 FIXME function qualifiers should really change the main variant. */
1216 if ((TREE_CODE (t1) == FUNCTION_TYPE
1217 || TREE_CODE (t1) == METHOD_TYPE)
1218 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1219 return false;
1220 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1221 return false;
1223 /* Allow for two different type nodes which have essentially the same
1224 definition. Note that we already checked for equality of the type
1225 qualifiers (just above). */
1227 if (TREE_CODE (t1) != ARRAY_TYPE
1228 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1229 return true;
1232 /* Compare the types. Break out if they could be the same. */
1233 switch (TREE_CODE (t1))
1235 case VOID_TYPE:
1236 case BOOLEAN_TYPE:
1237 /* All void and bool types are the same. */
1238 break;
1240 case INTEGER_TYPE:
1241 case FIXED_POINT_TYPE:
1242 case REAL_TYPE:
1243 /* With these nodes, we can't determine type equivalence by
1244 looking at what is stored in the nodes themselves, because
1245 two nodes might have different TYPE_MAIN_VARIANTs but still
1246 represent the same type. For example, wchar_t and int could
1247 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1248 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1249 and are distinct types. On the other hand, int and the
1250 following typedef
1252 typedef int INT __attribute((may_alias));
1254 have identical properties, different TYPE_MAIN_VARIANTs, but
1255 represent the same type. The canonical type system keeps
1256 track of equivalence in this case, so we fall back on it. */
1257 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1259 case TEMPLATE_TEMPLATE_PARM:
1260 case BOUND_TEMPLATE_TEMPLATE_PARM:
1261 if (!comp_template_parms_position (t1, t2))
1262 return false;
1263 if (!comp_template_parms
1264 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1265 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1266 return false;
1267 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1268 break;
1269 /* Don't check inheritance. */
1270 strict = COMPARE_STRICT;
1271 /* Fall through. */
1273 case RECORD_TYPE:
1274 case UNION_TYPE:
1275 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1276 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1277 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1278 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1279 break;
1281 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1282 break;
1283 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1284 break;
1286 return false;
1288 case OFFSET_TYPE:
1289 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1290 strict & ~COMPARE_REDECLARATION))
1291 return false;
1292 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1293 return false;
1294 break;
1296 case REFERENCE_TYPE:
1297 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1298 return false;
1299 /* fall through to checks for pointer types */
1301 case POINTER_TYPE:
1302 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1303 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1304 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1305 return false;
1306 break;
1308 case METHOD_TYPE:
1309 case FUNCTION_TYPE:
1310 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1311 return false;
1312 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1313 return false;
1314 break;
1316 case ARRAY_TYPE:
1317 /* Target types must match incl. qualifiers. */
1318 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1319 return false;
1320 break;
1322 case TEMPLATE_TYPE_PARM:
1323 /* If T1 and T2 don't have the same relative position in their
1324 template parameters set, they can't be equal. */
1325 if (!comp_template_parms_position (t1, t2))
1326 return false;
1327 break;
1329 case TYPENAME_TYPE:
1330 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1331 TYPENAME_TYPE_FULLNAME (t2)))
1332 return false;
1333 /* Qualifiers don't matter on scopes. */
1334 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1335 TYPE_CONTEXT (t2)))
1336 return false;
1337 break;
1339 case UNBOUND_CLASS_TEMPLATE:
1340 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1341 return false;
1342 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1343 return false;
1344 break;
1346 case COMPLEX_TYPE:
1347 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1348 return false;
1349 break;
1351 case VECTOR_TYPE:
1352 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1353 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1354 return false;
1355 break;
1357 case TYPE_PACK_EXPANSION:
1358 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1359 PACK_EXPANSION_PATTERN (t2))
1360 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1361 PACK_EXPANSION_EXTRA_ARGS (t2)));
1363 case DECLTYPE_TYPE:
1364 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1365 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1366 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1367 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1368 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1369 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1370 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1371 DECLTYPE_TYPE_EXPR (t2)))
1372 return false;
1373 break;
1375 case UNDERLYING_TYPE:
1376 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1377 UNDERLYING_TYPE_TYPE (t2));
1379 default:
1380 return false;
1383 /* If we get here, we know that from a target independent POV the
1384 types are the same. Make sure the target attributes are also
1385 the same. */
1386 return comp_type_attributes (t1, t2);
1389 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1390 is a bitwise-or of the COMPARE_* flags. */
1392 bool
1393 comptypes (tree t1, tree t2, int strict)
1395 if (strict == COMPARE_STRICT)
1397 if (t1 == t2)
1398 return true;
1400 if (t1 == error_mark_node || t2 == error_mark_node)
1401 return false;
1403 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1404 /* At least one of the types requires structural equality, so
1405 perform a deep check. */
1406 return structural_comptypes (t1, t2, strict);
1408 #ifdef ENABLE_CHECKING
1409 if (USE_CANONICAL_TYPES)
1411 bool result = structural_comptypes (t1, t2, strict);
1413 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1414 /* The two types are structurally equivalent, but their
1415 canonical types were different. This is a failure of the
1416 canonical type propagation code.*/
1417 internal_error
1418 ("canonical types differ for identical types %T and %T",
1419 t1, t2);
1420 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1421 /* Two types are structurally different, but the canonical
1422 types are the same. This means we were over-eager in
1423 assigning canonical types. */
1424 internal_error
1425 ("same canonical type node for different types %T and %T",
1426 t1, t2);
1428 return result;
1430 #else
1431 if (USE_CANONICAL_TYPES)
1432 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1433 #endif
1434 else
1435 return structural_comptypes (t1, t2, strict);
1437 else if (strict == COMPARE_STRUCTURAL)
1438 return structural_comptypes (t1, t2, COMPARE_STRICT);
1439 else
1440 return structural_comptypes (t1, t2, strict);
1443 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1444 top-level qualifiers. */
1446 bool
1447 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1449 if (type1 == error_mark_node || type2 == error_mark_node)
1450 return false;
1452 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1455 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1457 bool
1458 at_least_as_qualified_p (const_tree type1, const_tree type2)
1460 int q1 = cp_type_quals (type1);
1461 int q2 = cp_type_quals (type2);
1463 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1464 return (q1 & q2) == q2;
1467 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1468 more cv-qualified that TYPE1, and 0 otherwise. */
1471 comp_cv_qualification (int q1, int q2)
1473 if (q1 == q2)
1474 return 0;
1476 if ((q1 & q2) == q2)
1477 return 1;
1478 else if ((q1 & q2) == q1)
1479 return -1;
1481 return 0;
1485 comp_cv_qualification (const_tree type1, const_tree type2)
1487 int q1 = cp_type_quals (type1);
1488 int q2 = cp_type_quals (type2);
1489 return comp_cv_qualification (q1, q2);
1492 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1493 subset of the cv-qualification signature of TYPE2, and the types
1494 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1497 comp_cv_qual_signature (tree type1, tree type2)
1499 if (comp_ptr_ttypes_real (type2, type1, -1))
1500 return 1;
1501 else if (comp_ptr_ttypes_real (type1, type2, -1))
1502 return -1;
1503 else
1504 return 0;
1507 /* Subroutines of `comptypes'. */
1509 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1510 equivalent in the sense that functions with those parameter types
1511 can have equivalent types. The two lists must be equivalent,
1512 element by element. */
1514 bool
1515 compparms (const_tree parms1, const_tree parms2)
1517 const_tree t1, t2;
1519 /* An unspecified parmlist matches any specified parmlist
1520 whose argument types don't need default promotions. */
1522 for (t1 = parms1, t2 = parms2;
1523 t1 || t2;
1524 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1526 /* If one parmlist is shorter than the other,
1527 they fail to match. */
1528 if (!t1 || !t2)
1529 return false;
1530 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1531 return false;
1533 return true;
1537 /* Process a sizeof or alignof expression where the operand is a
1538 type. */
1540 tree
1541 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1543 tree value;
1544 bool dependent_p;
1546 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1547 if (type == error_mark_node)
1548 return error_mark_node;
1550 type = non_reference (type);
1551 if (TREE_CODE (type) == METHOD_TYPE)
1553 if (complain)
1554 pedwarn (input_location, OPT_Wpointer_arith,
1555 "invalid application of %qs to a member function",
1556 operator_name_info[(int) op].name);
1557 else
1558 return error_mark_node;
1559 value = size_one_node;
1562 dependent_p = dependent_type_p (type);
1563 if (!dependent_p)
1564 complete_type (type);
1565 if (dependent_p
1566 /* VLA types will have a non-constant size. In the body of an
1567 uninstantiated template, we don't need to try to compute the
1568 value, because the sizeof expression is not an integral
1569 constant expression in that case. And, if we do try to
1570 compute the value, we'll likely end up with SAVE_EXPRs, which
1571 the template substitution machinery does not expect to see. */
1572 || (processing_template_decl
1573 && COMPLETE_TYPE_P (type)
1574 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1576 value = build_min (op, size_type_node, type);
1577 TREE_READONLY (value) = 1;
1578 return value;
1581 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (type)
1582 && (flag_iso || warn_vla > 0))
1584 if (complain)
1585 pedwarn (input_location, OPT_Wvla,
1586 "taking sizeof array of runtime bound");
1587 else
1588 return error_mark_node;
1591 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1592 op == SIZEOF_EXPR, false,
1593 complain);
1596 /* Return the size of the type, without producing any warnings for
1597 types whose size cannot be taken. This routine should be used only
1598 in some other routine that has already produced a diagnostic about
1599 using the size of such a type. */
1600 tree
1601 cxx_sizeof_nowarn (tree type)
1603 if (TREE_CODE (type) == FUNCTION_TYPE
1604 || VOID_TYPE_P (type)
1605 || TREE_CODE (type) == ERROR_MARK)
1606 return size_one_node;
1607 else if (!COMPLETE_TYPE_P (type))
1608 return size_zero_node;
1609 else
1610 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1613 /* Process a sizeof expression where the operand is an expression. */
1615 static tree
1616 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1618 if (e == error_mark_node)
1619 return error_mark_node;
1621 if (processing_template_decl)
1623 e = build_min (SIZEOF_EXPR, size_type_node, e);
1624 TREE_SIDE_EFFECTS (e) = 0;
1625 TREE_READONLY (e) = 1;
1627 return e;
1630 /* To get the size of a static data member declared as an array of
1631 unknown bound, we need to instantiate it. */
1632 if (VAR_P (e)
1633 && VAR_HAD_UNKNOWN_BOUND (e)
1634 && DECL_TEMPLATE_INSTANTIATION (e))
1635 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1637 if (TREE_CODE (e) == PARM_DECL
1638 && DECL_ARRAY_PARAMETER_P (e)
1639 && (complain & tf_warning))
1641 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1642 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1643 inform (DECL_SOURCE_LOCATION (e), "declared here");
1646 e = mark_type_use (e);
1648 if (TREE_CODE (e) == COMPONENT_REF
1649 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1650 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1652 if (complain & tf_error)
1653 error ("invalid application of %<sizeof%> to a bit-field");
1654 else
1655 return error_mark_node;
1656 e = char_type_node;
1658 else if (is_overloaded_fn (e))
1660 if (complain & tf_error)
1661 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1662 "function type");
1663 else
1664 return error_mark_node;
1665 e = char_type_node;
1667 else if (type_unknown_p (e))
1669 if (complain & tf_error)
1670 cxx_incomplete_type_error (e, TREE_TYPE (e));
1671 else
1672 return error_mark_node;
1673 e = char_type_node;
1675 else
1676 e = TREE_TYPE (e);
1678 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1681 /* Implement the __alignof keyword: Return the minimum required
1682 alignment of E, measured in bytes. For VAR_DECL's and
1683 FIELD_DECL's return DECL_ALIGN (which can be set from an
1684 "aligned" __attribute__ specification). */
1686 static tree
1687 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1689 tree t;
1691 if (e == error_mark_node)
1692 return error_mark_node;
1694 if (processing_template_decl)
1696 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1697 TREE_SIDE_EFFECTS (e) = 0;
1698 TREE_READONLY (e) = 1;
1700 return e;
1703 e = mark_type_use (e);
1705 if (VAR_P (e))
1706 t = size_int (DECL_ALIGN_UNIT (e));
1707 else if (TREE_CODE (e) == COMPONENT_REF
1708 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1709 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1711 if (complain & tf_error)
1712 error ("invalid application of %<__alignof%> to a bit-field");
1713 else
1714 return error_mark_node;
1715 t = size_one_node;
1717 else if (TREE_CODE (e) == COMPONENT_REF
1718 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1719 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1720 else if (is_overloaded_fn (e))
1722 if (complain & tf_error)
1723 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1724 "function type");
1725 else
1726 return error_mark_node;
1727 if (TREE_CODE (e) == FUNCTION_DECL)
1728 t = size_int (DECL_ALIGN_UNIT (e));
1729 else
1730 t = size_one_node;
1732 else if (type_unknown_p (e))
1734 if (complain & tf_error)
1735 cxx_incomplete_type_error (e, TREE_TYPE (e));
1736 else
1737 return error_mark_node;
1738 t = size_one_node;
1740 else
1741 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1742 complain & tf_error);
1744 return fold_convert (size_type_node, t);
1747 /* Process a sizeof or alignof expression E with code OP where the operand
1748 is an expression. */
1750 tree
1751 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1753 if (op == SIZEOF_EXPR)
1754 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1755 else
1756 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1759 /* Build a representation of an expression 'alignas(E).' Return the
1760 folded integer value of E if it is an integral constant expression
1761 that resolves to a valid alignment. If E depends on a template
1762 parameter, return a syntactic representation tree of kind
1763 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1764 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1766 tree
1767 cxx_alignas_expr (tree e)
1769 if (e == NULL_TREE || e == error_mark_node
1770 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1771 return e;
1773 if (TYPE_P (e))
1774 /* [dcl.align]/3:
1776 When the alignment-specifier is of the form
1777 alignas(type-id ), it shall have the same effect as
1778 alignas(alignof(type-id )). */
1780 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1782 /* If we reach this point, it means the alignas expression if of
1783 the form "alignas(assignment-expression)", so we should follow
1784 what is stated by [dcl.align]/2. */
1786 if (value_dependent_expression_p (e))
1787 /* Leave value-dependent expression alone for now. */
1788 return e;
1790 e = instantiate_non_dependent_expr (e);
1791 e = mark_rvalue_use (e);
1793 /* [dcl.align]/2 says:
1795 the assignment-expression shall be an integral constant
1796 expression. */
1798 return cxx_constant_value (e);
1802 /* EXPR is being used in a context that is not a function call.
1803 Enforce:
1805 [expr.ref]
1807 The expression can be used only as the left-hand operand of a
1808 member function call.
1810 [expr.mptr.operator]
1812 If the result of .* or ->* is a function, then that result can be
1813 used only as the operand for the function call operator ().
1815 by issuing an error message if appropriate. Returns true iff EXPR
1816 violates these rules. */
1818 bool
1819 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1821 if (expr == NULL_TREE)
1822 return false;
1823 /* Don't enforce this in MS mode. */
1824 if (flag_ms_extensions)
1825 return false;
1826 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1827 expr = get_first_fn (expr);
1828 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1830 if (complain & tf_error)
1831 error ("invalid use of non-static member function");
1832 return true;
1834 return false;
1837 /* If EXP is a reference to a bitfield, and the type of EXP does not
1838 match the declared type of the bitfield, return the declared type
1839 of the bitfield. Otherwise, return NULL_TREE. */
1841 tree
1842 is_bitfield_expr_with_lowered_type (const_tree exp)
1844 switch (TREE_CODE (exp))
1846 case COND_EXPR:
1847 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1848 ? TREE_OPERAND (exp, 1)
1849 : TREE_OPERAND (exp, 0)))
1850 return NULL_TREE;
1851 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1853 case COMPOUND_EXPR:
1854 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1856 case MODIFY_EXPR:
1857 case SAVE_EXPR:
1858 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1860 case COMPONENT_REF:
1862 tree field;
1864 field = TREE_OPERAND (exp, 1);
1865 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1866 return NULL_TREE;
1867 if (same_type_ignoring_top_level_qualifiers_p
1868 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1869 return NULL_TREE;
1870 return DECL_BIT_FIELD_TYPE (field);
1873 CASE_CONVERT:
1874 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1875 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1876 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1877 /* Fallthrough. */
1879 default:
1880 return NULL_TREE;
1884 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1885 bitfield with a lowered type, the type of EXP is returned, rather
1886 than NULL_TREE. */
1888 tree
1889 unlowered_expr_type (const_tree exp)
1891 tree type;
1892 tree etype = TREE_TYPE (exp);
1894 type = is_bitfield_expr_with_lowered_type (exp);
1895 if (type)
1896 type = cp_build_qualified_type (type, cp_type_quals (etype));
1897 else
1898 type = etype;
1900 return type;
1903 /* Perform the conversions in [expr] that apply when an lvalue appears
1904 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1905 function-to-pointer conversions. In addition, manifest constants
1906 are replaced by their values, and bitfield references are converted
1907 to their declared types. Note that this function does not perform the
1908 lvalue-to-rvalue conversion for class types. If you need that conversion
1909 to for class types, then you probably need to use force_rvalue.
1911 Although the returned value is being used as an rvalue, this
1912 function does not wrap the returned expression in a
1913 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1914 that the return value is no longer an lvalue. */
1916 tree
1917 decay_conversion (tree exp, tsubst_flags_t complain)
1919 tree type;
1920 enum tree_code code;
1921 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1923 type = TREE_TYPE (exp);
1924 if (type == error_mark_node)
1925 return error_mark_node;
1927 exp = mark_rvalue_use (exp);
1929 exp = resolve_nondeduced_context (exp);
1930 if (type_unknown_p (exp))
1932 if (complain & tf_error)
1933 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1934 return error_mark_node;
1937 code = TREE_CODE (type);
1939 /* FIXME remove for delayed folding. */
1940 exp = scalar_constant_value (exp);
1941 if (error_operand_p (exp))
1942 return error_mark_node;
1944 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1945 return nullptr_node;
1947 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1948 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1949 if (code == VOID_TYPE)
1951 if (complain & tf_error)
1952 error_at (loc, "void value not ignored as it ought to be");
1953 return error_mark_node;
1955 if (invalid_nonstatic_memfn_p (exp, complain))
1956 return error_mark_node;
1957 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1958 return cp_build_addr_expr (exp, complain);
1959 if (code == ARRAY_TYPE)
1961 tree adr;
1962 tree ptrtype;
1964 if (INDIRECT_REF_P (exp))
1965 return build_nop (build_pointer_type (TREE_TYPE (type)),
1966 TREE_OPERAND (exp, 0));
1968 if (TREE_CODE (exp) == COMPOUND_EXPR)
1970 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1971 if (op1 == error_mark_node)
1972 return error_mark_node;
1973 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1974 TREE_OPERAND (exp, 0), op1);
1977 if (!lvalue_p (exp)
1978 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1980 if (complain & tf_error)
1981 error_at (loc, "invalid use of non-lvalue array");
1982 return error_mark_node;
1985 /* Don't let an array compound literal decay to a pointer. It can
1986 still be used to initialize an array or bind to a reference. */
1987 if (TREE_CODE (exp) == TARGET_EXPR)
1989 if (complain & tf_error)
1990 error_at (loc, "taking address of temporary array");
1991 return error_mark_node;
1994 ptrtype = build_pointer_type (TREE_TYPE (type));
1996 if (VAR_P (exp))
1998 if (!cxx_mark_addressable (exp))
1999 return error_mark_node;
2000 adr = build_nop (ptrtype, build_address (exp));
2001 return adr;
2003 /* This way is better for a COMPONENT_REF since it can
2004 simplify the offset for a component. */
2005 adr = cp_build_addr_expr (exp, complain);
2006 return cp_convert (ptrtype, adr, complain);
2009 /* If a bitfield is used in a context where integral promotion
2010 applies, then the caller is expected to have used
2011 default_conversion. That function promotes bitfields correctly
2012 before calling this function. At this point, if we have a
2013 bitfield referenced, we may assume that is not subject to
2014 promotion, and that, therefore, the type of the resulting rvalue
2015 is the declared type of the bitfield. */
2016 exp = convert_bitfield_to_declared_type (exp);
2018 /* We do not call rvalue() here because we do not want to wrap EXP
2019 in a NON_LVALUE_EXPR. */
2021 /* [basic.lval]
2023 Non-class rvalues always have cv-unqualified types. */
2024 type = TREE_TYPE (exp);
2025 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2026 exp = build_nop (cv_unqualified (type), exp);
2028 return exp;
2031 /* Perform preparatory conversions, as part of the "usual arithmetic
2032 conversions". In particular, as per [expr]:
2034 Whenever an lvalue expression appears as an operand of an
2035 operator that expects the rvalue for that operand, the
2036 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2037 standard conversions are applied to convert the expression to an
2038 rvalue.
2040 In addition, we perform integral promotions here, as those are
2041 applied to both operands to a binary operator before determining
2042 what additional conversions should apply. */
2044 static tree
2045 cp_default_conversion (tree exp, tsubst_flags_t complain)
2047 /* Check for target-specific promotions. */
2048 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2049 if (promoted_type)
2050 exp = cp_convert (promoted_type, exp, complain);
2051 /* Perform the integral promotions first so that bitfield
2052 expressions (which may promote to "int", even if the bitfield is
2053 declared "unsigned") are promoted correctly. */
2054 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2055 exp = cp_perform_integral_promotions (exp, complain);
2056 /* Perform the other conversions. */
2057 exp = decay_conversion (exp, complain);
2059 return exp;
2062 /* C version. */
2064 tree
2065 default_conversion (tree exp)
2067 return cp_default_conversion (exp, tf_warning_or_error);
2070 /* EXPR is an expression with an integral or enumeration type.
2071 Perform the integral promotions in [conv.prom], and return the
2072 converted value. */
2074 tree
2075 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2077 tree type;
2078 tree promoted_type;
2080 expr = mark_rvalue_use (expr);
2082 /* [conv.prom]
2084 If the bitfield has an enumerated type, it is treated as any
2085 other value of that type for promotion purposes. */
2086 type = is_bitfield_expr_with_lowered_type (expr);
2087 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2088 type = TREE_TYPE (expr);
2089 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2090 /* Scoped enums don't promote. */
2091 if (SCOPED_ENUM_P (type))
2092 return expr;
2093 promoted_type = type_promotes_to (type);
2094 if (type != promoted_type)
2095 expr = cp_convert (promoted_type, expr, complain);
2096 return expr;
2099 /* C version. */
2101 tree
2102 perform_integral_promotions (tree expr)
2104 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2107 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2108 decay_conversion to one. */
2111 string_conv_p (const_tree totype, const_tree exp, int warn)
2113 tree t;
2115 if (!TYPE_PTR_P (totype))
2116 return 0;
2118 t = TREE_TYPE (totype);
2119 if (!same_type_p (t, char_type_node)
2120 && !same_type_p (t, char16_type_node)
2121 && !same_type_p (t, char32_type_node)
2122 && !same_type_p (t, wchar_type_node))
2123 return 0;
2125 if (TREE_CODE (exp) == STRING_CST)
2127 /* Make sure that we don't try to convert between char and wide chars. */
2128 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2129 return 0;
2131 else
2133 /* Is this a string constant which has decayed to 'const char *'? */
2134 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2135 if (!same_type_p (TREE_TYPE (exp), t))
2136 return 0;
2137 STRIP_NOPS (exp);
2138 if (TREE_CODE (exp) != ADDR_EXPR
2139 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2140 return 0;
2142 if (warn)
2144 if (cxx_dialect >= cxx11)
2145 pedwarn (input_location,
2146 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
2147 "ISO C++ forbids converting a string constant to %qT",
2148 totype);
2149 else
2150 warning (OPT_Wwrite_strings,
2151 "deprecated conversion from string constant to %qT",
2152 totype);
2155 return 1;
2158 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2159 can, for example, use as an lvalue. This code used to be in
2160 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2161 expressions, where we're dealing with aggregates. But now it's again only
2162 called from unary_complex_lvalue. The case (in particular) that led to
2163 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2164 get it there. */
2166 static tree
2167 rationalize_conditional_expr (enum tree_code code, tree t,
2168 tsubst_flags_t complain)
2170 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2172 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2173 the first operand is always the one to be used if both operands
2174 are equal, so we know what conditional expression this used to be. */
2175 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2177 tree op0 = TREE_OPERAND (t, 0);
2178 tree op1 = TREE_OPERAND (t, 1);
2180 /* The following code is incorrect if either operand side-effects. */
2181 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2182 && !TREE_SIDE_EFFECTS (op1));
2183 return
2184 build_conditional_expr (loc,
2185 build_x_binary_op (loc,
2186 (TREE_CODE (t) == MIN_EXPR
2187 ? LE_EXPR : GE_EXPR),
2188 op0, TREE_CODE (op0),
2189 op1, TREE_CODE (op1),
2190 /*overload=*/NULL,
2191 complain),
2192 cp_build_unary_op (code, op0, 0, complain),
2193 cp_build_unary_op (code, op1, 0, complain),
2194 complain);
2197 return
2198 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2199 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2200 complain),
2201 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2202 complain),
2203 complain);
2206 /* Given the TYPE of an anonymous union field inside T, return the
2207 FIELD_DECL for the field. If not found return NULL_TREE. Because
2208 anonymous unions can nest, we must also search all anonymous unions
2209 that are directly reachable. */
2211 tree
2212 lookup_anon_field (tree t, tree type)
2214 tree field;
2216 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2218 if (TREE_STATIC (field))
2219 continue;
2220 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2221 continue;
2223 /* If we find it directly, return the field. */
2224 if (DECL_NAME (field) == NULL_TREE
2225 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2227 return field;
2230 /* Otherwise, it could be nested, search harder. */
2231 if (DECL_NAME (field) == NULL_TREE
2232 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2234 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2235 if (subfield)
2236 return subfield;
2239 return NULL_TREE;
2242 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2243 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2244 non-NULL, it indicates the path to the base used to name MEMBER.
2245 If PRESERVE_REFERENCE is true, the expression returned will have
2246 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2247 returned will have the type referred to by the reference.
2249 This function does not perform access control; that is either done
2250 earlier by the parser when the name of MEMBER is resolved to MEMBER
2251 itself, or later when overload resolution selects one of the
2252 functions indicated by MEMBER. */
2254 tree
2255 build_class_member_access_expr (tree object, tree member,
2256 tree access_path, bool preserve_reference,
2257 tsubst_flags_t complain)
2259 tree object_type;
2260 tree member_scope;
2261 tree result = NULL_TREE;
2262 tree using_decl = NULL_TREE;
2264 if (error_operand_p (object) || error_operand_p (member))
2265 return error_mark_node;
2267 gcc_assert (DECL_P (member) || BASELINK_P (member));
2269 /* [expr.ref]
2271 The type of the first expression shall be "class object" (of a
2272 complete type). */
2273 object_type = TREE_TYPE (object);
2274 if (!currently_open_class (object_type)
2275 && !complete_type_or_maybe_complain (object_type, object, complain))
2276 return error_mark_node;
2277 if (!CLASS_TYPE_P (object_type))
2279 if (complain & tf_error)
2281 if (POINTER_TYPE_P (object_type)
2282 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2283 error ("request for member %qD in %qE, which is of pointer "
2284 "type %qT (maybe you meant to use %<->%> ?)",
2285 member, object, object_type);
2286 else
2287 error ("request for member %qD in %qE, which is of non-class "
2288 "type %qT", member, object, object_type);
2290 return error_mark_node;
2293 /* The standard does not seem to actually say that MEMBER must be a
2294 member of OBJECT_TYPE. However, that is clearly what is
2295 intended. */
2296 if (DECL_P (member))
2298 member_scope = DECL_CLASS_CONTEXT (member);
2299 mark_used (member);
2300 if (TREE_DEPRECATED (member))
2301 warn_deprecated_use (member, NULL_TREE);
2303 else
2304 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2305 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2306 presently be the anonymous union. Go outwards until we find a
2307 type related to OBJECT_TYPE. */
2308 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2309 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2310 object_type))
2311 member_scope = TYPE_CONTEXT (member_scope);
2312 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2314 if (complain & tf_error)
2316 if (TREE_CODE (member) == FIELD_DECL)
2317 error ("invalid use of nonstatic data member %qE", member);
2318 else
2319 error ("%qD is not a member of %qT", member, object_type);
2321 return error_mark_node;
2324 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2325 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2326 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2328 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2329 if (temp)
2330 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2333 /* In [expr.ref], there is an explicit list of the valid choices for
2334 MEMBER. We check for each of those cases here. */
2335 if (VAR_P (member))
2337 /* A static data member. */
2338 result = member;
2339 mark_exp_read (object);
2340 /* If OBJECT has side-effects, they are supposed to occur. */
2341 if (TREE_SIDE_EFFECTS (object))
2342 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2344 else if (TREE_CODE (member) == FIELD_DECL)
2346 /* A non-static data member. */
2347 bool null_object_p;
2348 int type_quals;
2349 tree member_type;
2351 null_object_p = (INDIRECT_REF_P (object)
2352 && integer_zerop (TREE_OPERAND (object, 0)));
2354 /* Convert OBJECT to the type of MEMBER. */
2355 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2356 TYPE_MAIN_VARIANT (member_scope)))
2358 tree binfo;
2359 base_kind kind;
2361 binfo = lookup_base (access_path ? access_path : object_type,
2362 member_scope, ba_unique, &kind, complain);
2363 if (binfo == error_mark_node)
2364 return error_mark_node;
2366 /* It is invalid to try to get to a virtual base of a
2367 NULL object. The most common cause is invalid use of
2368 offsetof macro. */
2369 if (null_object_p && kind == bk_via_virtual)
2371 if (complain & tf_error)
2373 error ("invalid access to non-static data member %qD in "
2374 "virtual base of NULL object", member);
2376 return error_mark_node;
2379 /* Convert to the base. */
2380 object = build_base_path (PLUS_EXPR, object, binfo,
2381 /*nonnull=*/1, complain);
2382 /* If we found the base successfully then we should be able
2383 to convert to it successfully. */
2384 gcc_assert (object != error_mark_node);
2387 /* If MEMBER is from an anonymous aggregate, we have converted
2388 OBJECT so that it refers to the class containing the
2389 anonymous union. Generate a reference to the anonymous union
2390 itself, and recur to find MEMBER. */
2391 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2392 /* When this code is called from build_field_call, the
2393 object already has the type of the anonymous union.
2394 That is because the COMPONENT_REF was already
2395 constructed, and was then disassembled before calling
2396 build_field_call. After the function-call code is
2397 cleaned up, this waste can be eliminated. */
2398 && (!same_type_ignoring_top_level_qualifiers_p
2399 (TREE_TYPE (object), DECL_CONTEXT (member))))
2401 tree anonymous_union;
2403 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2404 DECL_CONTEXT (member));
2405 object = build_class_member_access_expr (object,
2406 anonymous_union,
2407 /*access_path=*/NULL_TREE,
2408 preserve_reference,
2409 complain);
2412 /* Compute the type of the field, as described in [expr.ref]. */
2413 type_quals = TYPE_UNQUALIFIED;
2414 member_type = TREE_TYPE (member);
2415 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2417 type_quals = (cp_type_quals (member_type)
2418 | cp_type_quals (object_type));
2420 /* A field is const (volatile) if the enclosing object, or the
2421 field itself, is const (volatile). But, a mutable field is
2422 not const, even within a const object. */
2423 if (DECL_MUTABLE_P (member))
2424 type_quals &= ~TYPE_QUAL_CONST;
2425 member_type = cp_build_qualified_type (member_type, type_quals);
2428 result = build3 (COMPONENT_REF, member_type, object, member,
2429 NULL_TREE);
2430 result = fold_if_not_in_template (result);
2432 /* Mark the expression const or volatile, as appropriate. Even
2433 though we've dealt with the type above, we still have to mark the
2434 expression itself. */
2435 if (type_quals & TYPE_QUAL_CONST)
2436 TREE_READONLY (result) = 1;
2437 if (type_quals & TYPE_QUAL_VOLATILE)
2438 TREE_THIS_VOLATILE (result) = 1;
2440 else if (BASELINK_P (member))
2442 /* The member is a (possibly overloaded) member function. */
2443 tree functions;
2444 tree type;
2446 /* If the MEMBER is exactly one static member function, then we
2447 know the type of the expression. Otherwise, we must wait
2448 until overload resolution has been performed. */
2449 functions = BASELINK_FUNCTIONS (member);
2450 if (TREE_CODE (functions) == FUNCTION_DECL
2451 && DECL_STATIC_FUNCTION_P (functions))
2452 type = TREE_TYPE (functions);
2453 else
2454 type = unknown_type_node;
2455 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2456 base. That will happen when the function is called. */
2457 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2459 else if (TREE_CODE (member) == CONST_DECL)
2461 /* The member is an enumerator. */
2462 result = member;
2463 /* If OBJECT has side-effects, they are supposed to occur. */
2464 if (TREE_SIDE_EFFECTS (object))
2465 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2466 object, result);
2468 else if ((using_decl = strip_using_decl (member)) != member)
2469 result = build_class_member_access_expr (object,
2470 using_decl,
2471 access_path, preserve_reference,
2472 complain);
2473 else
2475 if (complain & tf_error)
2476 error ("invalid use of %qD", member);
2477 return error_mark_node;
2480 if (!preserve_reference)
2481 /* [expr.ref]
2483 If E2 is declared to have type "reference to T", then ... the
2484 type of E1.E2 is T. */
2485 result = convert_from_reference (result);
2487 return result;
2490 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2491 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2493 static tree
2494 lookup_destructor (tree object, tree scope, tree dtor_name,
2495 tsubst_flags_t complain)
2497 tree object_type = TREE_TYPE (object);
2498 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2499 tree expr;
2501 /* We've already complained about this destructor. */
2502 if (dtor_type == error_mark_node)
2503 return error_mark_node;
2505 if (scope && !check_dtor_name (scope, dtor_type))
2507 if (complain & tf_error)
2508 error ("qualified type %qT does not match destructor name ~%qT",
2509 scope, dtor_type);
2510 return error_mark_node;
2512 if (is_auto (dtor_type))
2513 dtor_type = object_type;
2514 else if (identifier_p (dtor_type))
2516 /* In a template, names we can't find a match for are still accepted
2517 destructor names, and we check them here. */
2518 if (check_dtor_name (object_type, dtor_type))
2519 dtor_type = object_type;
2520 else
2522 if (complain & tf_error)
2523 error ("object type %qT does not match destructor name ~%qT",
2524 object_type, dtor_type);
2525 return error_mark_node;
2529 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2531 if (complain & tf_error)
2532 error ("the type being destroyed is %qT, but the destructor "
2533 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2534 return error_mark_node;
2536 expr = lookup_member (dtor_type, complete_dtor_identifier,
2537 /*protect=*/1, /*want_type=*/false,
2538 tf_warning_or_error);
2539 if (!expr)
2541 if (complain & tf_error)
2542 cxx_incomplete_type_error (dtor_name, dtor_type);
2543 return error_mark_node;
2545 expr = (adjust_result_of_qualified_name_lookup
2546 (expr, dtor_type, object_type));
2547 if (scope == NULL_TREE)
2548 /* We need to call adjust_result_of_qualified_name_lookup in case the
2549 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2550 that we still get virtual function binding. */
2551 BASELINK_QUALIFIED_P (expr) = false;
2552 return expr;
2555 /* An expression of the form "A::template B" has been resolved to
2556 DECL. Issue a diagnostic if B is not a template or template
2557 specialization. */
2559 void
2560 check_template_keyword (tree decl)
2562 /* The standard says:
2564 [temp.names]
2566 If a name prefixed by the keyword template is not a member
2567 template, the program is ill-formed.
2569 DR 228 removed the restriction that the template be a member
2570 template.
2572 DR 96, if accepted would add the further restriction that explicit
2573 template arguments must be provided if the template keyword is
2574 used, but, as of 2005-10-16, that DR is still in "drafting". If
2575 this DR is accepted, then the semantic checks here can be
2576 simplified, as the entity named must in fact be a template
2577 specialization, rather than, as at present, a set of overloaded
2578 functions containing at least one template function. */
2579 if (TREE_CODE (decl) != TEMPLATE_DECL
2580 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2582 if (!is_overloaded_fn (decl))
2583 permerror (input_location, "%qD is not a template", decl);
2584 else
2586 tree fns;
2587 fns = decl;
2588 if (BASELINK_P (fns))
2589 fns = BASELINK_FUNCTIONS (fns);
2590 while (fns)
2592 tree fn = OVL_CURRENT (fns);
2593 if (TREE_CODE (fn) == TEMPLATE_DECL
2594 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2595 break;
2596 if (TREE_CODE (fn) == FUNCTION_DECL
2597 && DECL_USE_TEMPLATE (fn)
2598 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2599 break;
2600 fns = OVL_NEXT (fns);
2602 if (!fns)
2603 permerror (input_location, "%qD is not a template", decl);
2608 /* This function is called by the parser to process a class member
2609 access expression of the form OBJECT.NAME. NAME is a node used by
2610 the parser to represent a name; it is not yet a DECL. It may,
2611 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2612 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2613 there is no reason to do the lookup twice, so the parser keeps the
2614 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2615 be a template via the use of the "A::template B" syntax. */
2617 tree
2618 finish_class_member_access_expr (tree object, tree name, bool template_p,
2619 tsubst_flags_t complain)
2621 tree expr;
2622 tree object_type;
2623 tree member;
2624 tree access_path = NULL_TREE;
2625 tree orig_object = object;
2626 tree orig_name = name;
2628 if (object == error_mark_node || name == error_mark_node)
2629 return error_mark_node;
2631 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2632 if (!objc_is_public (object, name))
2633 return error_mark_node;
2635 object_type = TREE_TYPE (object);
2637 if (processing_template_decl)
2639 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2640 dependent_type_p (object_type)
2641 /* If NAME is just an IDENTIFIER_NODE, then the expression
2642 is dependent. */
2643 || identifier_p (object)
2644 /* If NAME is "f<args>", where either 'f' or 'args' is
2645 dependent, then the expression is dependent. */
2646 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2647 && dependent_template_id_p (TREE_OPERAND (name, 0),
2648 TREE_OPERAND (name, 1)))
2649 /* If NAME is "T::X" where "T" is dependent, then the
2650 expression is dependent. */
2651 || (TREE_CODE (name) == SCOPE_REF
2652 && TYPE_P (TREE_OPERAND (name, 0))
2653 && dependent_type_p (TREE_OPERAND (name, 0))))
2654 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2655 object, name, NULL_TREE);
2656 object = build_non_dependent_expr (object);
2658 else if (c_dialect_objc ()
2659 && identifier_p (name)
2660 && (expr = objc_maybe_build_component_ref (object, name)))
2661 return expr;
2663 /* [expr.ref]
2665 The type of the first expression shall be "class object" (of a
2666 complete type). */
2667 if (!currently_open_class (object_type)
2668 && !complete_type_or_maybe_complain (object_type, object, complain))
2669 return error_mark_node;
2670 if (!CLASS_TYPE_P (object_type))
2672 if (complain & tf_error)
2674 if (POINTER_TYPE_P (object_type)
2675 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2676 error ("request for member %qD in %qE, which is of pointer "
2677 "type %qT (maybe you meant to use %<->%> ?)",
2678 name, object, object_type);
2679 else
2680 error ("request for member %qD in %qE, which is of non-class "
2681 "type %qT", name, object, object_type);
2683 return error_mark_node;
2686 if (BASELINK_P (name))
2687 /* A member function that has already been looked up. */
2688 member = name;
2689 else
2691 bool is_template_id = false;
2692 tree template_args = NULL_TREE;
2693 tree scope;
2695 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2697 is_template_id = true;
2698 template_args = TREE_OPERAND (name, 1);
2699 name = TREE_OPERAND (name, 0);
2701 if (TREE_CODE (name) == OVERLOAD)
2702 name = DECL_NAME (get_first_fn (name));
2703 else if (DECL_P (name))
2704 name = DECL_NAME (name);
2707 if (TREE_CODE (name) == SCOPE_REF)
2709 /* A qualified name. The qualifying class or namespace `S'
2710 has already been looked up; it is either a TYPE or a
2711 NAMESPACE_DECL. */
2712 scope = TREE_OPERAND (name, 0);
2713 name = TREE_OPERAND (name, 1);
2715 /* If SCOPE is a namespace, then the qualified name does not
2716 name a member of OBJECT_TYPE. */
2717 if (TREE_CODE (scope) == NAMESPACE_DECL)
2719 if (complain & tf_error)
2720 error ("%<%D::%D%> is not a member of %qT",
2721 scope, name, object_type);
2722 return error_mark_node;
2725 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2727 /* Looking up a member enumerator (c++/56793). */
2728 if (!TYPE_CLASS_SCOPE_P (scope)
2729 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2731 if (complain & tf_error)
2732 error ("%<%D::%D%> is not a member of %qT",
2733 scope, name, object_type);
2734 return error_mark_node;
2736 tree val = lookup_enumerator (scope, name);
2737 if (TREE_SIDE_EFFECTS (object))
2738 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2739 return val;
2742 gcc_assert (CLASS_TYPE_P (scope));
2743 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2745 if (constructor_name_p (name, scope))
2747 if (complain & tf_error)
2748 error ("cannot call constructor %<%T::%D%> directly",
2749 scope, name);
2750 return error_mark_node;
2753 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2754 access_path = lookup_base (object_type, scope, ba_check,
2755 NULL, complain);
2756 if (access_path == error_mark_node)
2757 return error_mark_node;
2758 if (!access_path)
2760 if (complain & tf_error)
2761 error ("%qT is not a base of %qT", scope, object_type);
2762 return error_mark_node;
2765 else
2767 scope = NULL_TREE;
2768 access_path = object_type;
2771 if (TREE_CODE (name) == BIT_NOT_EXPR)
2772 member = lookup_destructor (object, scope, name, complain);
2773 else
2775 /* Look up the member. */
2776 member = lookup_member (access_path, name, /*protect=*/1,
2777 /*want_type=*/false, complain);
2778 if (member == NULL_TREE)
2780 if (complain & tf_error)
2781 error ("%q#T has no member named %qE",
2782 TREE_CODE (access_path) == TREE_BINFO
2783 ? TREE_TYPE (access_path) : object_type, name);
2784 return error_mark_node;
2786 if (member == error_mark_node)
2787 return error_mark_node;
2790 if (is_template_id)
2792 tree templ = member;
2794 if (BASELINK_P (templ))
2795 templ = lookup_template_function (templ, template_args);
2796 else
2798 if (complain & tf_error)
2799 error ("%qD is not a member template function", name);
2800 return error_mark_node;
2805 if (TREE_DEPRECATED (member))
2806 warn_deprecated_use (member, NULL_TREE);
2808 if (template_p)
2809 check_template_keyword (member);
2811 expr = build_class_member_access_expr (object, member, access_path,
2812 /*preserve_reference=*/false,
2813 complain);
2814 if (processing_template_decl && expr != error_mark_node)
2816 if (BASELINK_P (member))
2818 if (TREE_CODE (orig_name) == SCOPE_REF)
2819 BASELINK_QUALIFIED_P (member) = 1;
2820 orig_name = member;
2822 return build_min_non_dep (COMPONENT_REF, expr,
2823 orig_object, orig_name,
2824 NULL_TREE);
2827 return expr;
2830 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2831 type. */
2833 tree
2834 build_simple_component_ref (tree object, tree member)
2836 tree type = cp_build_qualified_type (TREE_TYPE (member),
2837 cp_type_quals (TREE_TYPE (object)));
2838 return fold_build3_loc (input_location,
2839 COMPONENT_REF, type,
2840 object, member, NULL_TREE);
2843 /* Return an expression for the MEMBER_NAME field in the internal
2844 representation of PTRMEM, a pointer-to-member function. (Each
2845 pointer-to-member function type gets its own RECORD_TYPE so it is
2846 more convenient to access the fields by name than by FIELD_DECL.)
2847 This routine converts the NAME to a FIELD_DECL and then creates the
2848 node for the complete expression. */
2850 tree
2851 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2853 tree ptrmem_type;
2854 tree member;
2856 /* This code is a stripped down version of
2857 build_class_member_access_expr. It does not work to use that
2858 routine directly because it expects the object to be of class
2859 type. */
2860 ptrmem_type = TREE_TYPE (ptrmem);
2861 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2862 for (member = TYPE_FIELDS (ptrmem_type); member;
2863 member = DECL_CHAIN (member))
2864 if (DECL_NAME (member) == member_name)
2865 break;
2866 return build_simple_component_ref (ptrmem, member);
2869 /* Given an expression PTR for a pointer, return an expression
2870 for the value pointed to.
2871 ERRORSTRING is the name of the operator to appear in error messages.
2873 This function may need to overload OPERATOR_FNNAME.
2874 Must also handle REFERENCE_TYPEs for C++. */
2876 tree
2877 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2878 tsubst_flags_t complain)
2880 tree orig_expr = expr;
2881 tree rval;
2883 if (processing_template_decl)
2885 /* Retain the type if we know the operand is a pointer. */
2886 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2887 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2888 if (type_dependent_expression_p (expr))
2889 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2890 expr = build_non_dependent_expr (expr);
2893 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2894 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2895 if (!rval)
2896 rval = cp_build_indirect_ref (expr, errorstring, complain);
2898 if (processing_template_decl && rval != error_mark_node)
2899 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2900 else
2901 return rval;
2904 /* Helper function called from c-common. */
2905 tree
2906 build_indirect_ref (location_t /*loc*/,
2907 tree ptr, ref_operator errorstring)
2909 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2912 tree
2913 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2914 tsubst_flags_t complain)
2916 tree pointer, type;
2918 if (ptr == current_class_ptr
2919 || (TREE_CODE (ptr) == NOP_EXPR
2920 && TREE_OPERAND (ptr, 0) == current_class_ptr
2921 && (same_type_ignoring_top_level_qualifiers_p
2922 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2923 return current_class_ref;
2925 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2926 ? ptr : decay_conversion (ptr, complain));
2927 if (pointer == error_mark_node)
2928 return error_mark_node;
2930 type = TREE_TYPE (pointer);
2932 if (POINTER_TYPE_P (type))
2934 /* [expr.unary.op]
2936 If the type of the expression is "pointer to T," the type
2937 of the result is "T." */
2938 tree t = TREE_TYPE (type);
2940 if ((CONVERT_EXPR_P (ptr)
2941 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2942 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2944 /* If a warning is issued, mark it to avoid duplicates from
2945 the backend. This only needs to be done at
2946 warn_strict_aliasing > 2. */
2947 if (warn_strict_aliasing > 2)
2948 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2949 type, TREE_OPERAND (ptr, 0)))
2950 TREE_NO_WARNING (ptr) = 1;
2953 if (VOID_TYPE_P (t))
2955 /* A pointer to incomplete type (other than cv void) can be
2956 dereferenced [expr.unary.op]/1 */
2957 if (complain & tf_error)
2958 error ("%qT is not a pointer-to-object type", type);
2959 return error_mark_node;
2961 else if (TREE_CODE (pointer) == ADDR_EXPR
2962 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2963 /* The POINTER was something like `&x'. We simplify `*&x' to
2964 `x'. */
2965 return TREE_OPERAND (pointer, 0);
2966 else
2968 tree ref = build1 (INDIRECT_REF, t, pointer);
2970 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2971 so that we get the proper error message if the result is used
2972 to assign to. Also, &* is supposed to be a no-op. */
2973 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2974 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2975 TREE_SIDE_EFFECTS (ref)
2976 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2977 return ref;
2980 else if (!(complain & tf_error))
2981 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2983 /* `pointer' won't be an error_mark_node if we were given a
2984 pointer to member, so it's cool to check for this here. */
2985 else if (TYPE_PTRMEM_P (type))
2986 switch (errorstring)
2988 case RO_ARRAY_INDEXING:
2989 error ("invalid use of array indexing on pointer to member");
2990 break;
2991 case RO_UNARY_STAR:
2992 error ("invalid use of unary %<*%> on pointer to member");
2993 break;
2994 case RO_IMPLICIT_CONVERSION:
2995 error ("invalid use of implicit conversion on pointer to member");
2996 break;
2997 case RO_ARROW_STAR:
2998 error ("left hand operand of %<->*%> must be a pointer to class, "
2999 "but is a pointer to member of type %qT", type);
3000 break;
3001 default:
3002 gcc_unreachable ();
3004 else if (pointer != error_mark_node)
3005 invalid_indirection_error (input_location, type, errorstring);
3007 return error_mark_node;
3010 /* This handles expressions of the form "a[i]", which denotes
3011 an array reference.
3013 This is logically equivalent in C to *(a+i), but we may do it differently.
3014 If A is a variable or a member, we generate a primitive ARRAY_REF.
3015 This avoids forcing the array out of registers, and can work on
3016 arrays that are not lvalues (for example, members of structures returned
3017 by functions).
3019 If INDEX is of some user-defined type, it must be converted to
3020 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3021 will inherit the type of the array, which will be some pointer type.
3023 LOC is the location to use in building the array reference. */
3025 tree
3026 cp_build_array_ref (location_t loc, tree array, tree idx,
3027 tsubst_flags_t complain)
3029 tree ret;
3031 if (idx == 0)
3033 if (complain & tf_error)
3034 error_at (loc, "subscript missing in array reference");
3035 return error_mark_node;
3038 /* If an array's index is an array notation, then its rank cannot be
3039 greater than one. */
3040 if (flag_cilkplus && contains_array_notation_expr (idx))
3042 size_t rank = 0;
3044 /* If find_rank returns false, then it should have reported an error,
3045 thus it is unnecessary for repetition. */
3046 if (!find_rank (loc, idx, idx, true, &rank))
3047 return error_mark_node;
3048 if (rank > 1)
3050 error_at (loc, "rank of the array%'s index is greater than 1");
3051 return error_mark_node;
3054 if (TREE_TYPE (array) == error_mark_node
3055 || TREE_TYPE (idx) == error_mark_node)
3056 return error_mark_node;
3058 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3059 inside it. */
3060 switch (TREE_CODE (array))
3062 case COMPOUND_EXPR:
3064 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3065 complain);
3066 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3067 TREE_OPERAND (array, 0), value);
3068 SET_EXPR_LOCATION (ret, loc);
3069 return ret;
3072 case COND_EXPR:
3073 ret = build_conditional_expr
3074 (loc, TREE_OPERAND (array, 0),
3075 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3076 complain),
3077 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3078 complain),
3079 complain);
3080 protected_set_expr_location (ret, loc);
3081 return ret;
3083 default:
3084 break;
3087 bool non_lvalue
3088 = convert_vector_to_pointer_for_subscript (loc, &array, idx);
3090 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3092 tree rval, type;
3094 warn_array_subscript_with_type_char (idx);
3096 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3098 if (complain & tf_error)
3099 error_at (loc, "array subscript is not an integer");
3100 return error_mark_node;
3103 /* Apply integral promotions *after* noticing character types.
3104 (It is unclear why we do these promotions -- the standard
3105 does not say that we should. In fact, the natural thing would
3106 seem to be to convert IDX to ptrdiff_t; we're performing
3107 pointer arithmetic.) */
3108 idx = cp_perform_integral_promotions (idx, complain);
3110 /* An array that is indexed by a non-constant
3111 cannot be stored in a register; we must be able to do
3112 address arithmetic on its address.
3113 Likewise an array of elements of variable size. */
3114 if (TREE_CODE (idx) != INTEGER_CST
3115 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3116 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3117 != INTEGER_CST)))
3119 if (!cxx_mark_addressable (array))
3120 return error_mark_node;
3123 /* An array that is indexed by a constant value which is not within
3124 the array bounds cannot be stored in a register either; because we
3125 would get a crash in store_bit_field/extract_bit_field when trying
3126 to access a non-existent part of the register. */
3127 if (TREE_CODE (idx) == INTEGER_CST
3128 && TYPE_DOMAIN (TREE_TYPE (array))
3129 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3131 if (!cxx_mark_addressable (array))
3132 return error_mark_node;
3135 if (!lvalue_p (array))
3137 if (complain & tf_error)
3138 pedwarn (loc, OPT_Wpedantic,
3139 "ISO C++ forbids subscripting non-lvalue array");
3140 else
3141 return error_mark_node;
3144 /* Note in C++ it is valid to subscript a `register' array, since
3145 it is valid to take the address of something with that
3146 storage specification. */
3147 if (extra_warnings)
3149 tree foo = array;
3150 while (TREE_CODE (foo) == COMPONENT_REF)
3151 foo = TREE_OPERAND (foo, 0);
3152 if (VAR_P (foo) && DECL_REGISTER (foo)
3153 && (complain & tf_warning))
3154 warning_at (loc, OPT_Wextra,
3155 "subscripting array declared %<register%>");
3158 type = TREE_TYPE (TREE_TYPE (array));
3159 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3160 /* Array ref is const/volatile if the array elements are
3161 or if the array is.. */
3162 TREE_READONLY (rval)
3163 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3164 TREE_SIDE_EFFECTS (rval)
3165 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3166 TREE_THIS_VOLATILE (rval)
3167 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3168 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3169 complain);
3170 protected_set_expr_location (ret, loc);
3171 if (non_lvalue)
3172 ret = non_lvalue_loc (loc, ret);
3173 return ret;
3177 tree ar = cp_default_conversion (array, complain);
3178 tree ind = cp_default_conversion (idx, complain);
3180 /* Put the integer in IND to simplify error checking. */
3181 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3183 tree temp = ar;
3184 ar = ind;
3185 ind = temp;
3188 if (ar == error_mark_node || ind == error_mark_node)
3189 return error_mark_node;
3191 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3193 if (complain & tf_error)
3194 error_at (loc, "subscripted value is neither array nor pointer");
3195 return error_mark_node;
3197 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3199 if (complain & tf_error)
3200 error_at (loc, "array subscript is not an integer");
3201 return error_mark_node;
3204 warn_array_subscript_with_type_char (idx);
3206 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3207 PLUS_EXPR, ar, ind,
3208 complain),
3209 RO_ARRAY_INDEXING,
3210 complain);
3211 protected_set_expr_location (ret, loc);
3212 if (non_lvalue)
3213 ret = non_lvalue_loc (loc, ret);
3214 return ret;
3218 /* Entry point for Obj-C++. */
3220 tree
3221 build_array_ref (location_t loc, tree array, tree idx)
3223 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3226 /* Resolve a pointer to member function. INSTANCE is the object
3227 instance to use, if the member points to a virtual member.
3229 This used to avoid checking for virtual functions if basetype
3230 has no virtual functions, according to an earlier ANSI draft.
3231 With the final ISO C++ rules, such an optimization is
3232 incorrect: A pointer to a derived member can be static_cast
3233 to pointer-to-base-member, as long as the dynamic object
3234 later has the right member. So now we only do this optimization
3235 when we know the dynamic type of the object. */
3237 tree
3238 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3239 tsubst_flags_t complain)
3241 if (TREE_CODE (function) == OFFSET_REF)
3242 function = TREE_OPERAND (function, 1);
3244 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3246 tree idx, delta, e1, e2, e3, vtbl;
3247 bool nonvirtual;
3248 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3249 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3251 tree instance_ptr = *instance_ptrptr;
3252 tree instance_save_expr = 0;
3253 if (instance_ptr == error_mark_node)
3255 if (TREE_CODE (function) == PTRMEM_CST)
3257 /* Extracting the function address from a pmf is only
3258 allowed with -Wno-pmf-conversions. It only works for
3259 pmf constants. */
3260 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3261 e1 = convert (fntype, e1);
3262 return e1;
3264 else
3266 if (complain & tf_error)
3267 error ("object missing in use of %qE", function);
3268 return error_mark_node;
3272 /* True if we know that the dynamic type of the object doesn't have
3273 virtual functions, so we can assume the PFN field is a pointer. */
3274 nonvirtual = (COMPLETE_TYPE_P (basetype)
3275 && !TYPE_POLYMORPHIC_P (basetype)
3276 && resolves_to_fixed_type_p (instance_ptr, 0));
3278 /* If we don't really have an object (i.e. in an ill-formed
3279 conversion from PMF to pointer), we can't resolve virtual
3280 functions anyway. */
3281 if (!nonvirtual && is_dummy_object (instance_ptr))
3282 nonvirtual = true;
3284 if (TREE_SIDE_EFFECTS (instance_ptr))
3285 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3287 if (TREE_SIDE_EFFECTS (function))
3288 function = save_expr (function);
3290 /* Start by extracting all the information from the PMF itself. */
3291 e3 = pfn_from_ptrmemfunc (function);
3292 delta = delta_from_ptrmemfunc (function);
3293 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3294 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3296 case ptrmemfunc_vbit_in_pfn:
3297 e1 = cp_build_binary_op (input_location,
3298 BIT_AND_EXPR, idx, integer_one_node,
3299 complain);
3300 idx = cp_build_binary_op (input_location,
3301 MINUS_EXPR, idx, integer_one_node,
3302 complain);
3303 if (idx == error_mark_node)
3304 return error_mark_node;
3305 break;
3307 case ptrmemfunc_vbit_in_delta:
3308 e1 = cp_build_binary_op (input_location,
3309 BIT_AND_EXPR, delta, integer_one_node,
3310 complain);
3311 delta = cp_build_binary_op (input_location,
3312 RSHIFT_EXPR, delta, integer_one_node,
3313 complain);
3314 if (delta == error_mark_node)
3315 return error_mark_node;
3316 break;
3318 default:
3319 gcc_unreachable ();
3322 if (e1 == error_mark_node)
3323 return error_mark_node;
3325 /* Convert down to the right base before using the instance. A
3326 special case is that in a pointer to member of class C, C may
3327 be incomplete. In that case, the function will of course be
3328 a member of C, and no conversion is required. In fact,
3329 lookup_base will fail in that case, because incomplete
3330 classes do not have BINFOs. */
3331 if (!same_type_ignoring_top_level_qualifiers_p
3332 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3334 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3335 basetype, ba_check, NULL, complain);
3336 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3337 1, complain);
3338 if (instance_ptr == error_mark_node)
3339 return error_mark_node;
3341 /* ...and then the delta in the PMF. */
3342 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3344 /* Hand back the adjusted 'this' argument to our caller. */
3345 *instance_ptrptr = instance_ptr;
3347 if (nonvirtual)
3348 /* Now just return the pointer. */
3349 return e3;
3351 /* Next extract the vtable pointer from the object. */
3352 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3353 instance_ptr);
3354 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3355 if (vtbl == error_mark_node)
3356 return error_mark_node;
3358 /* Finally, extract the function pointer from the vtable. */
3359 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3360 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3361 if (e2 == error_mark_node)
3362 return error_mark_node;
3363 TREE_CONSTANT (e2) = 1;
3365 /* When using function descriptors, the address of the
3366 vtable entry is treated as a function pointer. */
3367 if (TARGET_VTABLE_USES_DESCRIPTORS)
3368 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3369 cp_build_addr_expr (e2, complain));
3371 e2 = fold_convert (TREE_TYPE (e3), e2);
3372 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3373 if (e1 == error_mark_node)
3374 return error_mark_node;
3376 /* Make sure this doesn't get evaluated first inside one of the
3377 branches of the COND_EXPR. */
3378 if (instance_save_expr)
3379 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3380 instance_save_expr, e1);
3382 function = e1;
3384 return function;
3387 /* Used by the C-common bits. */
3388 tree
3389 build_function_call (location_t /*loc*/,
3390 tree function, tree params)
3392 return cp_build_function_call (function, params, tf_warning_or_error);
3395 /* Used by the C-common bits. */
3396 tree
3397 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3398 tree function, vec<tree, va_gc> *params,
3399 vec<tree, va_gc> * /*origtypes*/)
3401 vec<tree, va_gc> *orig_params = params;
3402 tree ret = cp_build_function_call_vec (function, &params,
3403 tf_warning_or_error);
3405 /* cp_build_function_call_vec can reallocate PARAMS by adding
3406 default arguments. That should never happen here. Verify
3407 that. */
3408 gcc_assert (params == orig_params);
3410 return ret;
3413 /* Build a function call using a tree list of arguments. */
3415 static tree
3416 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3418 vec<tree, va_gc> *vec;
3419 tree ret;
3421 vec = make_tree_vector ();
3422 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3423 vec_safe_push (vec, TREE_VALUE (params));
3424 ret = cp_build_function_call_vec (function, &vec, complain);
3425 release_tree_vector (vec);
3426 return ret;
3429 /* Build a function call using varargs. */
3431 tree
3432 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3434 vec<tree, va_gc> *vec;
3435 va_list args;
3436 tree ret, t;
3438 vec = make_tree_vector ();
3439 va_start (args, complain);
3440 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3441 vec_safe_push (vec, t);
3442 va_end (args);
3443 ret = cp_build_function_call_vec (function, &vec, complain);
3444 release_tree_vector (vec);
3445 return ret;
3448 /* Build a function call using a vector of arguments. PARAMS may be
3449 NULL if there are no parameters. This changes the contents of
3450 PARAMS. */
3452 tree
3453 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3454 tsubst_flags_t complain)
3456 tree fntype, fndecl;
3457 int is_method;
3458 tree original = function;
3459 int nargs;
3460 tree *argarray;
3461 tree parm_types;
3462 vec<tree, va_gc> *allocated = NULL;
3463 tree ret;
3465 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3466 expressions, like those used for ObjC messenger dispatches. */
3467 if (params != NULL && !vec_safe_is_empty (*params))
3468 function = objc_rewrite_function_call (function, (**params)[0]);
3470 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3471 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3472 if (TREE_CODE (function) == NOP_EXPR
3473 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3474 function = TREE_OPERAND (function, 0);
3476 if (TREE_CODE (function) == FUNCTION_DECL)
3478 mark_used (function);
3479 fndecl = function;
3481 /* Convert anything with function type to a pointer-to-function. */
3482 if (DECL_MAIN_P (function))
3484 if (complain & tf_error)
3485 pedwarn (input_location, OPT_Wpedantic,
3486 "ISO C++ forbids calling %<::main%> from within program");
3487 else
3488 return error_mark_node;
3490 function = build_addr_func (function, complain);
3492 else
3494 fndecl = NULL_TREE;
3496 function = build_addr_func (function, complain);
3499 if (function == error_mark_node)
3500 return error_mark_node;
3502 fntype = TREE_TYPE (function);
3504 if (TYPE_PTRMEMFUNC_P (fntype))
3506 if (complain & tf_error)
3507 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3508 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3509 original, original);
3510 return error_mark_node;
3513 is_method = (TYPE_PTR_P (fntype)
3514 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3516 if (!(TYPE_PTRFN_P (fntype)
3517 || is_method
3518 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3520 if (complain & tf_error)
3522 if (!flag_diagnostics_show_caret)
3523 error_at (input_location,
3524 "%qE cannot be used as a function", original);
3525 else if (DECL_P (original))
3526 error_at (input_location,
3527 "%qD cannot be used as a function", original);
3528 else
3529 error_at (input_location,
3530 "expression cannot be used as a function");
3533 return error_mark_node;
3536 /* fntype now gets the type of function pointed to. */
3537 fntype = TREE_TYPE (fntype);
3538 parm_types = TYPE_ARG_TYPES (fntype);
3540 if (params == NULL)
3542 allocated = make_tree_vector ();
3543 params = &allocated;
3546 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3547 complain);
3548 if (nargs < 0)
3549 return error_mark_node;
3551 argarray = (*params)->address ();
3553 /* Check for errors in format strings and inappropriately
3554 null parameters. */
3555 check_function_arguments (fntype, nargs, argarray);
3557 ret = build_cxx_call (function, nargs, argarray, complain);
3559 if (allocated != NULL)
3560 release_tree_vector (allocated);
3562 return ret;
3565 /* Subroutine of convert_arguments.
3566 Warn about wrong number of args are genereted. */
3568 static void
3569 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3571 if (fndecl)
3573 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3575 if (DECL_NAME (fndecl) == NULL_TREE
3576 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3577 error_at (loc,
3578 too_many_p
3579 ? G_("too many arguments to constructor %q#D")
3580 : G_("too few arguments to constructor %q#D"),
3581 fndecl);
3582 else
3583 error_at (loc,
3584 too_many_p
3585 ? G_("too many arguments to member function %q#D")
3586 : G_("too few arguments to member function %q#D"),
3587 fndecl);
3589 else
3590 error_at (loc,
3591 too_many_p
3592 ? G_("too many arguments to function %q#D")
3593 : G_("too few arguments to function %q#D"),
3594 fndecl);
3595 inform (DECL_SOURCE_LOCATION (fndecl),
3596 "declared here");
3598 else
3600 if (c_dialect_objc () && objc_message_selector ())
3601 error_at (loc,
3602 too_many_p
3603 ? G_("too many arguments to method %q#D")
3604 : G_("too few arguments to method %q#D"),
3605 objc_message_selector ());
3606 else
3607 error_at (loc, too_many_p ? G_("too many arguments to function")
3608 : G_("too few arguments to function"));
3612 /* Convert the actual parameter expressions in the list VALUES to the
3613 types in the list TYPELIST. The converted expressions are stored
3614 back in the VALUES vector.
3615 If parmdecls is exhausted, or when an element has NULL as its type,
3616 perform the default conversions.
3618 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3620 This is also where warnings about wrong number of args are generated.
3622 Returns the actual number of arguments processed (which might be less
3623 than the length of the vector), or -1 on error.
3625 In C++, unspecified trailing parameters can be filled in with their
3626 default arguments, if such were specified. Do so here. */
3628 static int
3629 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3630 int flags, tsubst_flags_t complain)
3632 tree typetail;
3633 unsigned int i;
3635 /* Argument passing is always copy-initialization. */
3636 flags |= LOOKUP_ONLYCONVERTING;
3638 for (i = 0, typetail = typelist;
3639 i < vec_safe_length (*values);
3640 i++)
3642 tree type = typetail ? TREE_VALUE (typetail) : 0;
3643 tree val = (**values)[i];
3645 if (val == error_mark_node || type == error_mark_node)
3646 return -1;
3648 if (type == void_type_node)
3650 if (complain & tf_error)
3652 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3653 return i;
3655 else
3656 return -1;
3659 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3660 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3661 if (TREE_CODE (val) == NOP_EXPR
3662 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3663 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3664 val = TREE_OPERAND (val, 0);
3666 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3668 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3669 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3670 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3671 val = decay_conversion (val, complain);
3674 if (val == error_mark_node)
3675 return -1;
3677 if (type != 0)
3679 /* Formal parm type is specified by a function prototype. */
3680 tree parmval;
3682 if (!COMPLETE_TYPE_P (complete_type (type)))
3684 if (complain & tf_error)
3686 if (fndecl)
3687 error ("parameter %P of %qD has incomplete type %qT",
3688 i, fndecl, type);
3689 else
3690 error ("parameter %P has incomplete type %qT", i, type);
3692 parmval = error_mark_node;
3694 else
3696 parmval = convert_for_initialization
3697 (NULL_TREE, type, val, flags,
3698 ICR_ARGPASS, fndecl, i, complain);
3699 parmval = convert_for_arg_passing (type, parmval, complain);
3702 if (parmval == error_mark_node)
3703 return -1;
3705 (**values)[i] = parmval;
3707 else
3709 if (fndecl && magic_varargs_p (fndecl))
3710 /* Don't do ellipsis conversion for __built_in_constant_p
3711 as this will result in spurious errors for non-trivial
3712 types. */
3713 val = require_complete_type_sfinae (val, complain);
3714 else
3715 val = convert_arg_to_ellipsis (val, complain);
3717 (**values)[i] = val;
3720 if (typetail)
3721 typetail = TREE_CHAIN (typetail);
3724 if (typetail != 0 && typetail != void_list_node)
3726 /* See if there are default arguments that can be used. Because
3727 we hold default arguments in the FUNCTION_TYPE (which is so
3728 wrong), we can see default parameters here from deduced
3729 contexts (and via typeof) for indirect function calls.
3730 Fortunately we know whether we have a function decl to
3731 provide default arguments in a language conformant
3732 manner. */
3733 if (fndecl && TREE_PURPOSE (typetail)
3734 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3736 for (; typetail != void_list_node; ++i)
3738 tree parmval
3739 = convert_default_arg (TREE_VALUE (typetail),
3740 TREE_PURPOSE (typetail),
3741 fndecl, i, complain);
3743 if (parmval == error_mark_node)
3744 return -1;
3746 vec_safe_push (*values, parmval);
3747 typetail = TREE_CHAIN (typetail);
3748 /* ends with `...'. */
3749 if (typetail == NULL_TREE)
3750 break;
3753 else
3755 if (complain & tf_error)
3756 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3757 return -1;
3761 return (int) i;
3764 /* Build a binary-operation expression, after performing default
3765 conversions on the operands. CODE is the kind of expression to
3766 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3767 are the tree codes which correspond to ARG1 and ARG2 when issuing
3768 warnings about possibly misplaced parentheses. They may differ
3769 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3770 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3771 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3772 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3773 ARG2_CODE as ERROR_MARK. */
3775 tree
3776 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3777 enum tree_code arg1_code, tree arg2,
3778 enum tree_code arg2_code, tree *overload,
3779 tsubst_flags_t complain)
3781 tree orig_arg1;
3782 tree orig_arg2;
3783 tree expr;
3785 orig_arg1 = arg1;
3786 orig_arg2 = arg2;
3788 if (processing_template_decl)
3790 if (type_dependent_expression_p (arg1)
3791 || type_dependent_expression_p (arg2))
3792 return build_min_nt_loc (loc, code, arg1, arg2);
3793 arg1 = build_non_dependent_expr (arg1);
3794 arg2 = build_non_dependent_expr (arg2);
3797 if (code == DOTSTAR_EXPR)
3798 expr = build_m_component_ref (arg1, arg2, complain);
3799 else
3800 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3801 overload, complain);
3803 /* Check for cases such as x+y<<z which users are likely to
3804 misinterpret. But don't warn about obj << x + y, since that is a
3805 common idiom for I/O. */
3806 if (warn_parentheses
3807 && (complain & tf_warning)
3808 && !processing_template_decl
3809 && !error_operand_p (arg1)
3810 && !error_operand_p (arg2)
3811 && (code != LSHIFT_EXPR
3812 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3813 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3814 arg2_code, orig_arg2);
3816 if (processing_template_decl && expr != error_mark_node)
3817 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3819 return expr;
3822 /* Build and return an ARRAY_REF expression. */
3824 tree
3825 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3826 tsubst_flags_t complain)
3828 tree orig_arg1 = arg1;
3829 tree orig_arg2 = arg2;
3830 tree expr;
3832 if (processing_template_decl)
3834 if (type_dependent_expression_p (arg1)
3835 || type_dependent_expression_p (arg2))
3836 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3837 NULL_TREE, NULL_TREE);
3838 arg1 = build_non_dependent_expr (arg1);
3839 arg2 = build_non_dependent_expr (arg2);
3842 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3843 NULL_TREE, /*overload=*/NULL, complain);
3845 if (processing_template_decl && expr != error_mark_node)
3846 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3847 NULL_TREE, NULL_TREE);
3848 return expr;
3851 /* Return whether OP is an expression of enum type cast to integer
3852 type. In C++ even unsigned enum types are cast to signed integer
3853 types. We do not want to issue warnings about comparisons between
3854 signed and unsigned types when one of the types is an enum type.
3855 Those warnings are always false positives in practice. */
3857 static bool
3858 enum_cast_to_int (tree op)
3860 if (CONVERT_EXPR_P (op)
3861 && TREE_TYPE (op) == integer_type_node
3862 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3863 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3864 return true;
3866 /* The cast may have been pushed into a COND_EXPR. */
3867 if (TREE_CODE (op) == COND_EXPR)
3868 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3869 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3871 return false;
3874 /* For the c-common bits. */
3875 tree
3876 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3877 int /*convert_p*/)
3879 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3883 /* Build a binary-operation expression without default conversions.
3884 CODE is the kind of expression to build.
3885 LOCATION is the location_t of the operator in the source code.
3886 This function differs from `build' in several ways:
3887 the data type of the result is computed and recorded in it,
3888 warnings are generated if arg data types are invalid,
3889 special handling for addition and subtraction of pointers is known,
3890 and some optimization is done (operations on narrow ints
3891 are done in the narrower type when that gives the same result).
3892 Constant folding is also done before the result is returned.
3894 Note that the operands will never have enumeral types
3895 because either they have just had the default conversions performed
3896 or they have both just been converted to some other type in which
3897 the arithmetic is to be done.
3899 C++: must do special pointer arithmetic when implementing
3900 multiple inheritance, and deal with pointer to member functions. */
3902 tree
3903 cp_build_binary_op (location_t location,
3904 enum tree_code code, tree orig_op0, tree orig_op1,
3905 tsubst_flags_t complain)
3907 tree op0, op1;
3908 enum tree_code code0, code1;
3909 tree type0, type1;
3910 const char *invalid_op_diag;
3912 /* Expression code to give to the expression when it is built.
3913 Normally this is CODE, which is what the caller asked for,
3914 but in some special cases we change it. */
3915 enum tree_code resultcode = code;
3917 /* Data type in which the computation is to be performed.
3918 In the simplest cases this is the common type of the arguments. */
3919 tree result_type = NULL;
3921 /* Nonzero means operands have already been type-converted
3922 in whatever way is necessary.
3923 Zero means they need to be converted to RESULT_TYPE. */
3924 int converted = 0;
3926 /* Nonzero means create the expression with this type, rather than
3927 RESULT_TYPE. */
3928 tree build_type = 0;
3930 /* Nonzero means after finally constructing the expression
3931 convert it to this type. */
3932 tree final_type = 0;
3934 tree result;
3935 tree orig_type = NULL;
3937 /* Nonzero if this is an operation like MIN or MAX which can
3938 safely be computed in short if both args are promoted shorts.
3939 Also implies COMMON.
3940 -1 indicates a bitwise operation; this makes a difference
3941 in the exact conditions for when it is safe to do the operation
3942 in a narrower mode. */
3943 int shorten = 0;
3945 /* Nonzero if this is a comparison operation;
3946 if both args are promoted shorts, compare the original shorts.
3947 Also implies COMMON. */
3948 int short_compare = 0;
3950 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3951 int common = 0;
3953 /* True if both operands have arithmetic type. */
3954 bool arithmetic_types_p;
3956 /* Apply default conversions. */
3957 op0 = orig_op0;
3958 op1 = orig_op1;
3960 /* Remember whether we're doing / or %. */
3961 bool doing_div_or_mod = false;
3963 /* Remember whether we're doing << or >>. */
3964 bool doing_shift = false;
3966 /* Tree holding instrumentation expression. */
3967 tree instrument_expr = NULL;
3969 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3970 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3971 || code == TRUTH_XOR_EXPR)
3973 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3974 op0 = decay_conversion (op0, complain);
3975 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3976 op1 = decay_conversion (op1, complain);
3978 else
3980 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3981 op0 = cp_default_conversion (op0, complain);
3982 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3983 op1 = cp_default_conversion (op1, complain);
3986 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3987 STRIP_TYPE_NOPS (op0);
3988 STRIP_TYPE_NOPS (op1);
3990 /* DTRT if one side is an overloaded function, but complain about it. */
3991 if (type_unknown_p (op0))
3993 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3994 if (t != error_mark_node)
3996 if (complain & tf_error)
3997 permerror (input_location, "assuming cast to type %qT from overloaded function",
3998 TREE_TYPE (t));
3999 op0 = t;
4002 if (type_unknown_p (op1))
4004 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4005 if (t != error_mark_node)
4007 if (complain & tf_error)
4008 permerror (input_location, "assuming cast to type %qT from overloaded function",
4009 TREE_TYPE (t));
4010 op1 = t;
4014 type0 = TREE_TYPE (op0);
4015 type1 = TREE_TYPE (op1);
4017 /* The expression codes of the data types of the arguments tell us
4018 whether the arguments are integers, floating, pointers, etc. */
4019 code0 = TREE_CODE (type0);
4020 code1 = TREE_CODE (type1);
4022 /* If an error was already reported for one of the arguments,
4023 avoid reporting another error. */
4024 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4025 return error_mark_node;
4027 if ((invalid_op_diag
4028 = targetm.invalid_binary_op (code, type0, type1)))
4030 if (complain & tf_error)
4031 error (invalid_op_diag);
4032 return error_mark_node;
4035 /* Issue warnings about peculiar, but valid, uses of NULL. */
4036 if ((orig_op0 == null_node || orig_op1 == null_node)
4037 /* It's reasonable to use pointer values as operands of &&
4038 and ||, so NULL is no exception. */
4039 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4040 && ( /* Both are NULL (or 0) and the operation was not a
4041 comparison or a pointer subtraction. */
4042 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4043 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4044 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4045 || (!null_ptr_cst_p (orig_op0)
4046 && !TYPE_PTR_OR_PTRMEM_P (type0))
4047 || (!null_ptr_cst_p (orig_op1)
4048 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4049 && (complain & tf_warning))
4051 source_location loc =
4052 expansion_point_location_if_in_system_header (input_location);
4054 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4057 /* In case when one of the operands of the binary operation is
4058 a vector and another is a scalar -- convert scalar to vector. */
4059 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4061 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4062 complain & tf_error);
4064 switch (convert_flag)
4066 case stv_error:
4067 return error_mark_node;
4068 case stv_firstarg:
4070 op0 = convert (TREE_TYPE (type1), op0);
4071 op0 = save_expr (op0);
4072 op0 = build_vector_from_val (type1, op0);
4073 type0 = TREE_TYPE (op0);
4074 code0 = TREE_CODE (type0);
4075 converted = 1;
4076 break;
4078 case stv_secondarg:
4080 op1 = convert (TREE_TYPE (type0), op1);
4081 op1 = save_expr (op1);
4082 op1 = build_vector_from_val (type0, op1);
4083 type1 = TREE_TYPE (op1);
4084 code1 = TREE_CODE (type1);
4085 converted = 1;
4086 break;
4088 default:
4089 break;
4093 switch (code)
4095 case MINUS_EXPR:
4096 /* Subtraction of two similar pointers.
4097 We must subtract them as integers, then divide by object size. */
4098 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4099 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4100 TREE_TYPE (type1)))
4101 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4102 complain);
4103 /* In all other cases except pointer - int, the usual arithmetic
4104 rules apply. */
4105 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4107 common = 1;
4108 break;
4110 /* The pointer - int case is just like pointer + int; fall
4111 through. */
4112 case PLUS_EXPR:
4113 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4114 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4116 tree ptr_operand;
4117 tree int_operand;
4118 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4119 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4120 if (processing_template_decl)
4122 result_type = TREE_TYPE (ptr_operand);
4123 break;
4125 return cp_pointer_int_sum (code,
4126 ptr_operand,
4127 int_operand,
4128 complain);
4130 common = 1;
4131 break;
4133 case MULT_EXPR:
4134 common = 1;
4135 break;
4137 case TRUNC_DIV_EXPR:
4138 case CEIL_DIV_EXPR:
4139 case FLOOR_DIV_EXPR:
4140 case ROUND_DIV_EXPR:
4141 case EXACT_DIV_EXPR:
4142 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4143 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4144 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4145 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4147 enum tree_code tcode0 = code0, tcode1 = code1;
4148 tree cop1 = fold_non_dependent_expr (op1);
4149 doing_div_or_mod = true;
4150 warn_for_div_by_zero (location, cop1);
4152 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4153 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4154 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4155 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4157 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4158 resultcode = RDIV_EXPR;
4159 else
4160 /* When dividing two signed integers, we have to promote to int.
4161 unless we divide by a constant != -1. Note that default
4162 conversion will have been performed on the operands at this
4163 point, so we have to dig out the original type to find out if
4164 it was unsigned. */
4165 shorten = ((TREE_CODE (op0) == NOP_EXPR
4166 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4167 || (TREE_CODE (op1) == INTEGER_CST
4168 && ! integer_all_onesp (op1)));
4170 common = 1;
4172 break;
4174 case BIT_AND_EXPR:
4175 case BIT_IOR_EXPR:
4176 case BIT_XOR_EXPR:
4177 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4178 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4179 && !VECTOR_FLOAT_TYPE_P (type0)
4180 && !VECTOR_FLOAT_TYPE_P (type1)))
4181 shorten = -1;
4182 break;
4184 case TRUNC_MOD_EXPR:
4185 case FLOOR_MOD_EXPR:
4187 tree cop1 = fold_non_dependent_expr (op1);
4188 doing_div_or_mod = true;
4189 warn_for_div_by_zero (location, cop1);
4192 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4193 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4194 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4195 common = 1;
4196 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4198 /* Although it would be tempting to shorten always here, that loses
4199 on some targets, since the modulo instruction is undefined if the
4200 quotient can't be represented in the computation mode. We shorten
4201 only if unsigned or if dividing by something we know != -1. */
4202 shorten = ((TREE_CODE (op0) == NOP_EXPR
4203 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4204 || (TREE_CODE (op1) == INTEGER_CST
4205 && ! integer_all_onesp (op1)));
4206 common = 1;
4208 break;
4210 case TRUTH_ANDIF_EXPR:
4211 case TRUTH_ORIF_EXPR:
4212 case TRUTH_AND_EXPR:
4213 case TRUTH_OR_EXPR:
4214 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4216 if (!COMPARISON_CLASS_P (op1))
4217 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4218 build_zero_cst (type1), complain);
4219 if (code == TRUTH_ANDIF_EXPR)
4221 tree z = build_zero_cst (TREE_TYPE (op1));
4222 return build_conditional_expr (location, op0, op1, z, complain);
4224 else if (code == TRUTH_ORIF_EXPR)
4226 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4227 return build_conditional_expr (location, op0, m1, op1, complain);
4229 else
4230 gcc_unreachable ();
4232 if (VECTOR_TYPE_P (type0))
4234 if (!COMPARISON_CLASS_P (op0))
4235 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4236 build_zero_cst (type0), complain);
4237 if (!VECTOR_TYPE_P (type1))
4239 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4240 tree z = build_zero_cst (TREE_TYPE (op0));
4241 op1 = build_conditional_expr (location, op1, z, m1, complain);
4243 else if (!COMPARISON_CLASS_P (op1))
4244 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4245 build_zero_cst (type1), complain);
4247 if (code == TRUTH_ANDIF_EXPR)
4248 code = BIT_AND_EXPR;
4249 else if (code == TRUTH_ORIF_EXPR)
4250 code = BIT_IOR_EXPR;
4251 else
4252 gcc_unreachable ();
4254 return cp_build_binary_op (location, code, op0, op1, complain);
4257 result_type = boolean_type_node;
4258 break;
4260 /* Shift operations: result has same type as first operand;
4261 always convert second operand to int.
4262 Also set SHORT_SHIFT if shifting rightward. */
4264 case RSHIFT_EXPR:
4265 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4266 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4268 result_type = type0;
4269 converted = 1;
4271 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4272 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4273 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4274 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4276 result_type = type0;
4277 converted = 1;
4279 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4281 tree const_op1 = fold_non_dependent_expr (op1);
4282 if (TREE_CODE (const_op1) != INTEGER_CST)
4283 const_op1 = op1;
4284 result_type = type0;
4285 doing_shift = true;
4286 if (TREE_CODE (const_op1) == INTEGER_CST)
4288 if (tree_int_cst_lt (const_op1, integer_zero_node))
4290 if ((complain & tf_warning)
4291 && c_inhibit_evaluation_warnings == 0)
4292 warning (OPT_Wshift_count_negative,
4293 "right shift count is negative");
4295 else
4297 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4298 && (complain & tf_warning)
4299 && c_inhibit_evaluation_warnings == 0)
4300 warning (OPT_Wshift_count_overflow,
4301 "right shift count >= width of type");
4304 /* Avoid converting op1 to result_type later. */
4305 converted = 1;
4307 break;
4309 case LSHIFT_EXPR:
4310 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4311 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4313 result_type = type0;
4314 converted = 1;
4316 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4317 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4318 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4319 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4321 result_type = type0;
4322 converted = 1;
4324 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4326 tree const_op1 = fold_non_dependent_expr (op1);
4327 if (TREE_CODE (const_op1) != INTEGER_CST)
4328 const_op1 = op1;
4329 result_type = type0;
4330 doing_shift = true;
4331 if (TREE_CODE (const_op1) == INTEGER_CST)
4333 if (tree_int_cst_lt (const_op1, integer_zero_node))
4335 if ((complain & tf_warning)
4336 && c_inhibit_evaluation_warnings == 0)
4337 warning (OPT_Wshift_count_negative,
4338 "left shift count is negative");
4340 else if (compare_tree_int (const_op1,
4341 TYPE_PRECISION (type0)) >= 0)
4343 if ((complain & tf_warning)
4344 && c_inhibit_evaluation_warnings == 0)
4345 warning (OPT_Wshift_count_overflow,
4346 "left shift count >= width of type");
4349 /* Avoid converting op1 to result_type later. */
4350 converted = 1;
4352 break;
4354 case RROTATE_EXPR:
4355 case LROTATE_EXPR:
4356 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4358 result_type = type0;
4359 if (TREE_CODE (op1) == INTEGER_CST)
4361 if (tree_int_cst_lt (op1, integer_zero_node))
4363 if (complain & tf_warning)
4364 warning (0, (code == LROTATE_EXPR)
4365 ? G_("left rotate count is negative")
4366 : G_("right rotate count is negative"));
4368 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4370 if (complain & tf_warning)
4371 warning (0, (code == LROTATE_EXPR)
4372 ? G_("left rotate count >= width of type")
4373 : G_("right rotate count >= width of type"));
4376 /* Convert the shift-count to an integer, regardless of
4377 size of value being shifted. */
4378 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4379 op1 = cp_convert (integer_type_node, op1, complain);
4381 break;
4383 case EQ_EXPR:
4384 case NE_EXPR:
4385 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4386 goto vector_compare;
4387 if ((complain & tf_warning)
4388 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4389 warning (OPT_Wfloat_equal,
4390 "comparing floating point with == or != is unsafe");
4391 if ((complain & tf_warning)
4392 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4393 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4394 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4396 build_type = boolean_type_node;
4397 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4398 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4399 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4400 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4401 short_compare = 1;
4402 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4403 && null_ptr_cst_p (op1))
4404 /* Handle, eg, (void*)0 (c++/43906), and more. */
4405 || (code0 == POINTER_TYPE
4406 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4408 if (TYPE_PTR_P (type1))
4409 result_type = composite_pointer_type (type0, type1, op0, op1,
4410 CPO_COMPARISON, complain);
4411 else
4412 result_type = type0;
4414 if (TREE_CODE (op0) == ADDR_EXPR
4415 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4417 if ((complain & tf_warning)
4418 && c_inhibit_evaluation_warnings == 0)
4419 warning (OPT_Waddress, "the address of %qD will never be NULL",
4420 TREE_OPERAND (op0, 0));
4423 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4424 && null_ptr_cst_p (op0))
4425 /* Handle, eg, (void*)0 (c++/43906), and more. */
4426 || (code1 == POINTER_TYPE
4427 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4429 if (TYPE_PTR_P (type0))
4430 result_type = composite_pointer_type (type0, type1, op0, op1,
4431 CPO_COMPARISON, complain);
4432 else
4433 result_type = type1;
4435 if (TREE_CODE (op1) == ADDR_EXPR
4436 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4438 if ((complain & tf_warning)
4439 && c_inhibit_evaluation_warnings == 0)
4440 warning (OPT_Waddress, "the address of %qD will never be NULL",
4441 TREE_OPERAND (op1, 0));
4444 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4445 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4446 result_type = composite_pointer_type (type0, type1, op0, op1,
4447 CPO_COMPARISON, complain);
4448 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4449 /* One of the operands must be of nullptr_t type. */
4450 result_type = TREE_TYPE (nullptr_node);
4451 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4453 result_type = type0;
4454 if (complain & tf_error)
4455 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4456 else
4457 return error_mark_node;
4459 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4461 result_type = type1;
4462 if (complain & tf_error)
4463 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4464 else
4465 return error_mark_node;
4467 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4469 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4470 == ptrmemfunc_vbit_in_delta)
4472 tree pfn0, delta0, e1, e2;
4474 if (TREE_SIDE_EFFECTS (op0))
4475 op0 = save_expr (op0);
4477 pfn0 = pfn_from_ptrmemfunc (op0);
4478 delta0 = delta_from_ptrmemfunc (op0);
4479 e1 = cp_build_binary_op (location,
4480 EQ_EXPR,
4481 pfn0,
4482 build_zero_cst (TREE_TYPE (pfn0)),
4483 complain);
4484 e2 = cp_build_binary_op (location,
4485 BIT_AND_EXPR,
4486 delta0,
4487 integer_one_node,
4488 complain);
4490 if (complain & tf_warning)
4491 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4493 e2 = cp_build_binary_op (location,
4494 EQ_EXPR, e2, integer_zero_node,
4495 complain);
4496 op0 = cp_build_binary_op (location,
4497 TRUTH_ANDIF_EXPR, e1, e2,
4498 complain);
4499 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4501 else
4503 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4504 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4506 result_type = TREE_TYPE (op0);
4508 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4509 return cp_build_binary_op (location, code, op1, op0, complain);
4510 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4512 tree type;
4513 /* E will be the final comparison. */
4514 tree e;
4515 /* E1 and E2 are for scratch. */
4516 tree e1;
4517 tree e2;
4518 tree pfn0;
4519 tree pfn1;
4520 tree delta0;
4521 tree delta1;
4523 type = composite_pointer_type (type0, type1, op0, op1,
4524 CPO_COMPARISON, complain);
4526 if (!same_type_p (TREE_TYPE (op0), type))
4527 op0 = cp_convert_and_check (type, op0, complain);
4528 if (!same_type_p (TREE_TYPE (op1), type))
4529 op1 = cp_convert_and_check (type, op1, complain);
4531 if (op0 == error_mark_node || op1 == error_mark_node)
4532 return error_mark_node;
4534 if (TREE_SIDE_EFFECTS (op0))
4535 op0 = save_expr (op0);
4536 if (TREE_SIDE_EFFECTS (op1))
4537 op1 = save_expr (op1);
4539 pfn0 = pfn_from_ptrmemfunc (op0);
4540 pfn1 = pfn_from_ptrmemfunc (op1);
4541 delta0 = delta_from_ptrmemfunc (op0);
4542 delta1 = delta_from_ptrmemfunc (op1);
4543 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4544 == ptrmemfunc_vbit_in_delta)
4546 /* We generate:
4548 (op0.pfn == op1.pfn
4549 && ((op0.delta == op1.delta)
4550 || (!op0.pfn && op0.delta & 1 == 0
4551 && op1.delta & 1 == 0))
4553 The reason for the `!op0.pfn' bit is that a NULL
4554 pointer-to-member is any member with a zero PFN and
4555 LSB of the DELTA field is 0. */
4557 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4558 delta0,
4559 integer_one_node,
4560 complain);
4561 e1 = cp_build_binary_op (location,
4562 EQ_EXPR, e1, integer_zero_node,
4563 complain);
4564 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4565 delta1,
4566 integer_one_node,
4567 complain);
4568 e2 = cp_build_binary_op (location,
4569 EQ_EXPR, e2, integer_zero_node,
4570 complain);
4571 e1 = cp_build_binary_op (location,
4572 TRUTH_ANDIF_EXPR, e2, e1,
4573 complain);
4574 e2 = cp_build_binary_op (location, EQ_EXPR,
4575 pfn0,
4576 build_zero_cst (TREE_TYPE (pfn0)),
4577 complain);
4578 e2 = cp_build_binary_op (location,
4579 TRUTH_ANDIF_EXPR, e2, e1, complain);
4580 e1 = cp_build_binary_op (location,
4581 EQ_EXPR, delta0, delta1, complain);
4582 e1 = cp_build_binary_op (location,
4583 TRUTH_ORIF_EXPR, e1, e2, complain);
4585 else
4587 /* We generate:
4589 (op0.pfn == op1.pfn
4590 && (!op0.pfn || op0.delta == op1.delta))
4592 The reason for the `!op0.pfn' bit is that a NULL
4593 pointer-to-member is any member with a zero PFN; the
4594 DELTA field is unspecified. */
4596 e1 = cp_build_binary_op (location,
4597 EQ_EXPR, delta0, delta1, complain);
4598 e2 = cp_build_binary_op (location,
4599 EQ_EXPR,
4600 pfn0,
4601 build_zero_cst (TREE_TYPE (pfn0)),
4602 complain);
4603 e1 = cp_build_binary_op (location,
4604 TRUTH_ORIF_EXPR, e1, e2, complain);
4606 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4607 e = cp_build_binary_op (location,
4608 TRUTH_ANDIF_EXPR, e2, e1, complain);
4609 if (code == EQ_EXPR)
4610 return e;
4611 return cp_build_binary_op (location,
4612 EQ_EXPR, e, integer_zero_node, complain);
4614 else
4616 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4617 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4618 type1));
4619 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4620 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4621 type0));
4624 break;
4626 case MAX_EXPR:
4627 case MIN_EXPR:
4628 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4629 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4630 shorten = 1;
4631 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4632 result_type = composite_pointer_type (type0, type1, op0, op1,
4633 CPO_COMPARISON, complain);
4634 break;
4636 case LE_EXPR:
4637 case GE_EXPR:
4638 case LT_EXPR:
4639 case GT_EXPR:
4640 if (TREE_CODE (orig_op0) == STRING_CST
4641 || TREE_CODE (orig_op1) == STRING_CST)
4643 if (complain & tf_warning)
4644 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4647 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4649 vector_compare:
4650 tree intt;
4651 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4652 TREE_TYPE (type1))
4653 && !vector_types_compatible_elements_p (type0, type1))
4655 if (complain & tf_error)
4657 error_at (location, "comparing vectors with different "
4658 "element types");
4659 inform (location, "operand types are %qT and %qT",
4660 type0, type1);
4662 return error_mark_node;
4665 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4667 if (complain & tf_error)
4669 error_at (location, "comparing vectors with different "
4670 "number of elements");
4671 inform (location, "operand types are %qT and %qT",
4672 type0, type1);
4674 return error_mark_node;
4677 /* Always construct signed integer vector type. */
4678 intt = c_common_type_for_size (GET_MODE_BITSIZE
4679 (TYPE_MODE (TREE_TYPE (type0))), 0);
4680 if (!intt)
4682 if (complain & tf_error)
4683 error_at (location, "could not find an integer type "
4684 "of the same size as %qT", TREE_TYPE (type0));
4685 return error_mark_node;
4687 result_type = build_opaque_vector_type (intt,
4688 TYPE_VECTOR_SUBPARTS (type0));
4689 converted = 1;
4690 break;
4692 build_type = boolean_type_node;
4693 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4694 || code0 == ENUMERAL_TYPE)
4695 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4696 || code1 == ENUMERAL_TYPE))
4697 short_compare = 1;
4698 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4699 result_type = composite_pointer_type (type0, type1, op0, op1,
4700 CPO_COMPARISON, complain);
4701 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4703 result_type = type0;
4704 if (extra_warnings && (complain & tf_warning))
4705 warning (OPT_Wextra,
4706 "ordered comparison of pointer with integer zero");
4708 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4710 result_type = type1;
4711 if (extra_warnings && (complain & tf_warning))
4712 warning (OPT_Wextra,
4713 "ordered comparison of pointer with integer zero");
4715 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4716 /* One of the operands must be of nullptr_t type. */
4717 result_type = TREE_TYPE (nullptr_node);
4718 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4720 result_type = type0;
4721 if (complain & tf_error)
4722 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4723 else
4724 return error_mark_node;
4726 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4728 result_type = type1;
4729 if (complain & tf_error)
4730 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4731 else
4732 return error_mark_node;
4734 break;
4736 case UNORDERED_EXPR:
4737 case ORDERED_EXPR:
4738 case UNLT_EXPR:
4739 case UNLE_EXPR:
4740 case UNGT_EXPR:
4741 case UNGE_EXPR:
4742 case UNEQ_EXPR:
4743 build_type = integer_type_node;
4744 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4746 if (complain & tf_error)
4747 error ("unordered comparison on non-floating point argument");
4748 return error_mark_node;
4750 common = 1;
4751 break;
4753 default:
4754 break;
4757 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4758 || code0 == ENUMERAL_TYPE)
4759 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4760 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4761 arithmetic_types_p = 1;
4762 else
4764 arithmetic_types_p = 0;
4765 /* Vector arithmetic is only allowed when both sides are vectors. */
4766 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4768 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4769 || !vector_types_compatible_elements_p (type0, type1))
4771 if (complain & tf_error)
4772 binary_op_error (location, code, type0, type1);
4773 return error_mark_node;
4775 arithmetic_types_p = 1;
4778 /* Determine the RESULT_TYPE, if it is not already known. */
4779 if (!result_type
4780 && arithmetic_types_p
4781 && (shorten || common || short_compare))
4783 result_type = cp_common_type (type0, type1);
4784 if (complain & tf_warning)
4785 do_warn_double_promotion (result_type, type0, type1,
4786 "implicit conversion from %qT to %qT "
4787 "to match other operand of binary "
4788 "expression",
4789 location);
4792 if (!result_type)
4794 if (complain & tf_error)
4795 error ("invalid operands of types %qT and %qT to binary %qO",
4796 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4797 return error_mark_node;
4800 /* If we're in a template, the only thing we need to know is the
4801 RESULT_TYPE. */
4802 if (processing_template_decl)
4804 /* Since the middle-end checks the type when doing a build2, we
4805 need to build the tree in pieces. This built tree will never
4806 get out of the front-end as we replace it when instantiating
4807 the template. */
4808 tree tmp = build2 (resultcode,
4809 build_type ? build_type : result_type,
4810 NULL_TREE, op1);
4811 TREE_OPERAND (tmp, 0) = op0;
4812 return tmp;
4815 if (arithmetic_types_p)
4817 bool first_complex = (code0 == COMPLEX_TYPE);
4818 bool second_complex = (code1 == COMPLEX_TYPE);
4819 int none_complex = (!first_complex && !second_complex);
4821 /* Adapted from patch for c/24581. */
4822 if (first_complex != second_complex
4823 && (code == PLUS_EXPR
4824 || code == MINUS_EXPR
4825 || code == MULT_EXPR
4826 || (code == TRUNC_DIV_EXPR && first_complex))
4827 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4828 && flag_signed_zeros)
4830 /* An operation on mixed real/complex operands must be
4831 handled specially, but the language-independent code can
4832 more easily optimize the plain complex arithmetic if
4833 -fno-signed-zeros. */
4834 tree real_type = TREE_TYPE (result_type);
4835 tree real, imag;
4836 if (first_complex)
4838 if (TREE_TYPE (op0) != result_type)
4839 op0 = cp_convert_and_check (result_type, op0, complain);
4840 if (TREE_TYPE (op1) != real_type)
4841 op1 = cp_convert_and_check (real_type, op1, complain);
4843 else
4845 if (TREE_TYPE (op0) != real_type)
4846 op0 = cp_convert_and_check (real_type, op0, complain);
4847 if (TREE_TYPE (op1) != result_type)
4848 op1 = cp_convert_and_check (result_type, op1, complain);
4850 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4851 return error_mark_node;
4852 if (first_complex)
4854 op0 = save_expr (op0);
4855 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4856 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4857 switch (code)
4859 case MULT_EXPR:
4860 case TRUNC_DIV_EXPR:
4861 op1 = save_expr (op1);
4862 imag = build2 (resultcode, real_type, imag, op1);
4863 /* Fall through. */
4864 case PLUS_EXPR:
4865 case MINUS_EXPR:
4866 real = build2 (resultcode, real_type, real, op1);
4867 break;
4868 default:
4869 gcc_unreachable();
4872 else
4874 op1 = save_expr (op1);
4875 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4876 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4877 switch (code)
4879 case MULT_EXPR:
4880 op0 = save_expr (op0);
4881 imag = build2 (resultcode, real_type, op0, imag);
4882 /* Fall through. */
4883 case PLUS_EXPR:
4884 real = build2 (resultcode, real_type, op0, real);
4885 break;
4886 case MINUS_EXPR:
4887 real = build2 (resultcode, real_type, op0, real);
4888 imag = build1 (NEGATE_EXPR, real_type, imag);
4889 break;
4890 default:
4891 gcc_unreachable();
4894 real = fold_if_not_in_template (real);
4895 imag = fold_if_not_in_template (imag);
4896 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4897 result = fold_if_not_in_template (result);
4898 return result;
4901 /* For certain operations (which identify themselves by shorten != 0)
4902 if both args were extended from the same smaller type,
4903 do the arithmetic in that type and then extend.
4905 shorten !=0 and !=1 indicates a bitwise operation.
4906 For them, this optimization is safe only if
4907 both args are zero-extended or both are sign-extended.
4908 Otherwise, we might change the result.
4909 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4910 but calculated in (unsigned short) it would be (unsigned short)-1. */
4912 if (shorten && none_complex)
4914 orig_type = result_type;
4915 final_type = result_type;
4916 result_type = shorten_binary_op (result_type, op0, op1,
4917 shorten == -1);
4920 /* Comparison operations are shortened too but differently.
4921 They identify themselves by setting short_compare = 1. */
4923 if (short_compare)
4925 /* Don't write &op0, etc., because that would prevent op0
4926 from being kept in a register.
4927 Instead, make copies of the our local variables and
4928 pass the copies by reference, then copy them back afterward. */
4929 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4930 enum tree_code xresultcode = resultcode;
4931 tree val
4932 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4933 &xresultcode);
4934 if (val != 0)
4935 return cp_convert (boolean_type_node, val, complain);
4936 op0 = xop0, op1 = xop1;
4937 converted = 1;
4938 resultcode = xresultcode;
4941 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4942 && warn_sign_compare
4943 /* Do not warn until the template is instantiated; we cannot
4944 bound the ranges of the arguments until that point. */
4945 && !processing_template_decl
4946 && (complain & tf_warning)
4947 && c_inhibit_evaluation_warnings == 0
4948 /* Even unsigned enum types promote to signed int. We don't
4949 want to issue -Wsign-compare warnings for this case. */
4950 && !enum_cast_to_int (orig_op0)
4951 && !enum_cast_to_int (orig_op1))
4953 tree oop0 = maybe_constant_value (orig_op0);
4954 tree oop1 = maybe_constant_value (orig_op1);
4956 if (TREE_CODE (oop0) != INTEGER_CST)
4957 oop0 = orig_op0;
4958 if (TREE_CODE (oop1) != INTEGER_CST)
4959 oop1 = orig_op1;
4960 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4961 result_type, resultcode);
4965 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4966 Then the expression will be built.
4967 It will be given type FINAL_TYPE if that is nonzero;
4968 otherwise, it will be given type RESULT_TYPE. */
4969 if (! converted)
4971 if (TREE_TYPE (op0) != result_type)
4972 op0 = cp_convert_and_check (result_type, op0, complain);
4973 if (TREE_TYPE (op1) != result_type)
4974 op1 = cp_convert_and_check (result_type, op1, complain);
4976 if (op0 == error_mark_node || op1 == error_mark_node)
4977 return error_mark_node;
4980 if (build_type == NULL_TREE)
4981 build_type = result_type;
4983 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
4984 | SANITIZE_FLOAT_DIVIDE))
4985 && !processing_template_decl
4986 && current_function_decl != 0
4987 && !lookup_attribute ("no_sanitize_undefined",
4988 DECL_ATTRIBUTES (current_function_decl))
4989 && (doing_div_or_mod || doing_shift))
4991 /* OP0 and/or OP1 might have side-effects. */
4992 op0 = cp_save_expr (op0);
4993 op1 = cp_save_expr (op1);
4994 op0 = fold_non_dependent_expr (op0);
4995 op1 = fold_non_dependent_expr (op1);
4996 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
4997 | SANITIZE_FLOAT_DIVIDE)))
4999 /* For diagnostics we want to use the promoted types without
5000 shorten_binary_op. So convert the arguments to the
5001 original result_type. */
5002 tree cop0 = op0;
5003 tree cop1 = op1;
5004 if (orig_type != NULL && result_type != orig_type)
5006 cop0 = cp_convert (orig_type, op0, complain);
5007 cop1 = cp_convert (orig_type, op1, complain);
5009 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5011 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5012 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5015 result = build2 (resultcode, build_type, op0, op1);
5016 result = fold_if_not_in_template (result);
5017 if (final_type != 0)
5018 result = cp_convert (final_type, result, complain);
5020 if (TREE_OVERFLOW_P (result)
5021 && !TREE_OVERFLOW_P (op0)
5022 && !TREE_OVERFLOW_P (op1))
5023 overflow_warning (location, result);
5025 if (instrument_expr != NULL)
5026 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
5027 instrument_expr, result);
5029 return result;
5032 /* Build a VEC_PERM_EXPR.
5033 This is a simple wrapper for c_build_vec_perm_expr. */
5034 tree
5035 build_x_vec_perm_expr (location_t loc,
5036 tree arg0, tree arg1, tree arg2,
5037 tsubst_flags_t complain)
5039 tree orig_arg0 = arg0;
5040 tree orig_arg1 = arg1;
5041 tree orig_arg2 = arg2;
5042 if (processing_template_decl)
5044 if (type_dependent_expression_p (arg0)
5045 || type_dependent_expression_p (arg1)
5046 || type_dependent_expression_p (arg2))
5047 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5048 arg0 = build_non_dependent_expr (arg0);
5049 if (arg1)
5050 arg1 = build_non_dependent_expr (arg1);
5051 arg2 = build_non_dependent_expr (arg2);
5053 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5054 if (processing_template_decl && exp != error_mark_node)
5055 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5056 orig_arg1, orig_arg2);
5057 return exp;
5060 /* Return a tree for the sum or difference (RESULTCODE says which)
5061 of pointer PTROP and integer INTOP. */
5063 static tree
5064 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5065 tsubst_flags_t complain)
5067 tree res_type = TREE_TYPE (ptrop);
5069 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5070 in certain circumstance (when it's valid to do so). So we need
5071 to make sure it's complete. We don't need to check here, if we
5072 can actually complete it at all, as those checks will be done in
5073 pointer_int_sum() anyway. */
5074 complete_type (TREE_TYPE (res_type));
5076 return pointer_int_sum (input_location, resultcode, ptrop,
5077 fold_if_not_in_template (intop),
5078 complain & tf_warning_or_error);
5081 /* Return a tree for the difference of pointers OP0 and OP1.
5082 The resulting tree has type int. */
5084 static tree
5085 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5087 tree result;
5088 tree restype = ptrdiff_type_node;
5089 tree target_type = TREE_TYPE (ptrtype);
5091 if (!complete_type_or_else (target_type, NULL_TREE))
5092 return error_mark_node;
5094 if (VOID_TYPE_P (target_type))
5096 if (complain & tf_error)
5097 permerror (input_location, "ISO C++ forbids using pointer of "
5098 "type %<void *%> in subtraction");
5099 else
5100 return error_mark_node;
5102 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5104 if (complain & tf_error)
5105 permerror (input_location, "ISO C++ forbids using pointer to "
5106 "a function in subtraction");
5107 else
5108 return error_mark_node;
5110 if (TREE_CODE (target_type) == METHOD_TYPE)
5112 if (complain & tf_error)
5113 permerror (input_location, "ISO C++ forbids using pointer to "
5114 "a method in subtraction");
5115 else
5116 return error_mark_node;
5119 /* First do the subtraction as integers;
5120 then drop through to build the divide operator. */
5122 op0 = cp_build_binary_op (input_location,
5123 MINUS_EXPR,
5124 cp_convert (restype, op0, complain),
5125 cp_convert (restype, op1, complain),
5126 complain);
5128 /* This generates an error if op1 is a pointer to an incomplete type. */
5129 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5131 if (complain & tf_error)
5132 error ("invalid use of a pointer to an incomplete type in "
5133 "pointer arithmetic");
5134 else
5135 return error_mark_node;
5138 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5140 if (complain & tf_error)
5141 error ("arithmetic on pointer to an empty aggregate");
5142 else
5143 return error_mark_node;
5146 op1 = (TYPE_PTROB_P (ptrtype)
5147 ? size_in_bytes (target_type)
5148 : integer_one_node);
5150 /* Do the division. */
5152 result = build2 (EXACT_DIV_EXPR, restype, op0,
5153 cp_convert (restype, op1, complain));
5154 return fold_if_not_in_template (result);
5157 /* Construct and perhaps optimize a tree representation
5158 for a unary operation. CODE, a tree_code, specifies the operation
5159 and XARG is the operand. */
5161 tree
5162 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5163 tsubst_flags_t complain)
5165 tree orig_expr = xarg;
5166 tree exp;
5167 int ptrmem = 0;
5169 if (processing_template_decl)
5171 if (type_dependent_expression_p (xarg))
5172 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5174 xarg = build_non_dependent_expr (xarg);
5177 exp = NULL_TREE;
5179 /* [expr.unary.op] says:
5181 The address of an object of incomplete type can be taken.
5183 (And is just the ordinary address operator, not an overloaded
5184 "operator &".) However, if the type is a template
5185 specialization, we must complete the type at this point so that
5186 an overloaded "operator &" will be available if required. */
5187 if (code == ADDR_EXPR
5188 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5189 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5190 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5191 || (TREE_CODE (xarg) == OFFSET_REF)))
5192 /* Don't look for a function. */;
5193 else
5194 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5195 NULL_TREE, /*overload=*/NULL, complain);
5196 if (!exp && code == ADDR_EXPR)
5198 if (is_overloaded_fn (xarg))
5200 tree fn = get_first_fn (xarg);
5201 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5203 if (complain & tf_error)
5204 error (DECL_CONSTRUCTOR_P (fn)
5205 ? G_("taking address of constructor %qE")
5206 : G_("taking address of destructor %qE"),
5207 xarg);
5208 return error_mark_node;
5212 /* A pointer to member-function can be formed only by saying
5213 &X::mf. */
5214 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5215 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5217 if (TREE_CODE (xarg) != OFFSET_REF
5218 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5220 if (complain & tf_error)
5222 error ("invalid use of %qE to form a "
5223 "pointer-to-member-function", xarg);
5224 if (TREE_CODE (xarg) != OFFSET_REF)
5225 inform (input_location, " a qualified-id is required");
5227 return error_mark_node;
5229 else
5231 if (complain & tf_error)
5232 error ("parentheses around %qE cannot be used to form a"
5233 " pointer-to-member-function",
5234 xarg);
5235 else
5236 return error_mark_node;
5237 PTRMEM_OK_P (xarg) = 1;
5241 if (TREE_CODE (xarg) == OFFSET_REF)
5243 ptrmem = PTRMEM_OK_P (xarg);
5245 if (!ptrmem && !flag_ms_extensions
5246 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5248 /* A single non-static member, make sure we don't allow a
5249 pointer-to-member. */
5250 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5251 TREE_OPERAND (xarg, 0),
5252 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5253 PTRMEM_OK_P (xarg) = ptrmem;
5257 exp = cp_build_addr_expr_strict (xarg, complain);
5260 if (processing_template_decl && exp != error_mark_node)
5261 exp = build_min_non_dep (code, exp, orig_expr,
5262 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5263 if (TREE_CODE (exp) == ADDR_EXPR)
5264 PTRMEM_OK_P (exp) = ptrmem;
5265 return exp;
5268 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5269 constants, where a null value is represented by an INTEGER_CST of
5270 -1. */
5272 tree
5273 cp_truthvalue_conversion (tree expr)
5275 tree type = TREE_TYPE (expr);
5276 if (TYPE_PTRDATAMEM_P (type)
5277 /* Avoid ICE on invalid use of non-static member function. */
5278 || TREE_CODE (expr) == FUNCTION_DECL)
5279 return build_binary_op (EXPR_LOCATION (expr),
5280 NE_EXPR, expr, nullptr_node, 1);
5281 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5283 /* With -Wzero-as-null-pointer-constant do not warn for an
5284 'if (p)' or a 'while (!p)', where p is a pointer. */
5285 tree ret;
5286 ++c_inhibit_evaluation_warnings;
5287 ret = c_common_truthvalue_conversion (input_location, expr);
5288 --c_inhibit_evaluation_warnings;
5289 return ret;
5291 else
5292 return c_common_truthvalue_conversion (input_location, expr);
5295 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5297 tree
5298 condition_conversion (tree expr)
5300 tree t;
5301 if (processing_template_decl)
5302 return expr;
5303 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5304 tf_warning_or_error, LOOKUP_NORMAL);
5305 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5306 return t;
5309 /* Returns the address of T. This function will fold away
5310 ADDR_EXPR of INDIRECT_REF. */
5312 tree
5313 build_address (tree t)
5315 if (error_operand_p (t) || !cxx_mark_addressable (t))
5316 return error_mark_node;
5317 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5318 t = build_fold_addr_expr (t);
5319 if (TREE_CODE (t) != ADDR_EXPR)
5320 t = rvalue (t);
5321 return t;
5324 /* Return a NOP_EXPR converting EXPR to TYPE. */
5326 tree
5327 build_nop (tree type, tree expr)
5329 if (type == error_mark_node || error_operand_p (expr))
5330 return expr;
5331 return build1 (NOP_EXPR, type, expr);
5334 /* Take the address of ARG, whatever that means under C++ semantics.
5335 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5336 and class rvalues as well.
5338 Nothing should call this function directly; instead, callers should use
5339 cp_build_addr_expr or cp_build_addr_expr_strict. */
5341 static tree
5342 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5344 tree argtype;
5345 tree val;
5347 if (!arg || error_operand_p (arg))
5348 return error_mark_node;
5350 arg = mark_lvalue_use (arg);
5351 argtype = lvalue_type (arg);
5353 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5355 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5356 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5358 /* They're trying to take the address of a unique non-static
5359 member function. This is ill-formed (except in MS-land),
5360 but let's try to DTRT.
5361 Note: We only handle unique functions here because we don't
5362 want to complain if there's a static overload; non-unique
5363 cases will be handled by instantiate_type. But we need to
5364 handle this case here to allow casts on the resulting PMF.
5365 We could defer this in non-MS mode, but it's easier to give
5366 a useful error here. */
5368 /* Inside constant member functions, the `this' pointer
5369 contains an extra const qualifier. TYPE_MAIN_VARIANT
5370 is used here to remove this const from the diagnostics
5371 and the created OFFSET_REF. */
5372 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5373 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5374 mark_used (fn);
5376 if (! flag_ms_extensions)
5378 tree name = DECL_NAME (fn);
5379 if (!(complain & tf_error))
5380 return error_mark_node;
5381 else if (current_class_type
5382 && TREE_OPERAND (arg, 0) == current_class_ref)
5383 /* An expression like &memfn. */
5384 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5385 " or parenthesized non-static member function to form"
5386 " a pointer to member function. Say %<&%T::%D%>",
5387 base, name);
5388 else
5389 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5390 " function to form a pointer to member function."
5391 " Say %<&%T::%D%>",
5392 base, name);
5394 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5397 /* Uninstantiated types are all functions. Taking the
5398 address of a function is a no-op, so just return the
5399 argument. */
5400 if (type_unknown_p (arg))
5401 return build1 (ADDR_EXPR, unknown_type_node, arg);
5403 if (TREE_CODE (arg) == OFFSET_REF)
5404 /* We want a pointer to member; bypass all the code for actually taking
5405 the address of something. */
5406 goto offset_ref;
5408 /* Anything not already handled and not a true memory reference
5409 is an error. */
5410 if (TREE_CODE (argtype) != FUNCTION_TYPE
5411 && TREE_CODE (argtype) != METHOD_TYPE)
5413 cp_lvalue_kind kind = lvalue_kind (arg);
5414 if (kind == clk_none)
5416 if (complain & tf_error)
5417 lvalue_error (input_location, lv_addressof);
5418 return error_mark_node;
5420 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5422 if (!(complain & tf_error))
5423 return error_mark_node;
5424 if (kind & clk_class)
5425 /* Make this a permerror because we used to accept it. */
5426 permerror (input_location, "taking address of temporary");
5427 else
5428 error ("taking address of xvalue (rvalue reference)");
5432 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5434 tree type = build_pointer_type (TREE_TYPE (argtype));
5435 arg = build1 (CONVERT_EXPR, type, arg);
5436 return arg;
5438 else if (pedantic && DECL_MAIN_P (arg))
5440 /* ARM $3.4 */
5441 /* Apparently a lot of autoconf scripts for C++ packages do this,
5442 so only complain if -Wpedantic. */
5443 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5444 pedwarn (input_location, OPT_Wpedantic,
5445 "ISO C++ forbids taking address of function %<::main%>");
5446 else if (flag_pedantic_errors)
5447 return error_mark_node;
5450 /* Let &* cancel out to simplify resulting code. */
5451 if (INDIRECT_REF_P (arg))
5453 /* We don't need to have `current_class_ptr' wrapped in a
5454 NON_LVALUE_EXPR node. */
5455 if (arg == current_class_ref)
5456 return current_class_ptr;
5458 arg = TREE_OPERAND (arg, 0);
5459 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5461 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5462 arg = build1 (CONVERT_EXPR, type, arg);
5464 else
5465 /* Don't let this be an lvalue. */
5466 arg = rvalue (arg);
5467 return arg;
5470 /* ??? Cope with user tricks that amount to offsetof. */
5471 if (TREE_CODE (argtype) != FUNCTION_TYPE
5472 && TREE_CODE (argtype) != METHOD_TYPE
5473 && argtype != unknown_type_node
5474 && (val = get_base_address (arg))
5475 && COMPLETE_TYPE_P (TREE_TYPE (val))
5476 && INDIRECT_REF_P (val)
5477 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5479 tree type = build_pointer_type (argtype);
5480 return fold_convert (type, fold_offsetof_1 (arg));
5483 /* Handle complex lvalues (when permitted)
5484 by reduction to simpler cases. */
5485 val = unary_complex_lvalue (ADDR_EXPR, arg);
5486 if (val != 0)
5487 return val;
5489 switch (TREE_CODE (arg))
5491 CASE_CONVERT:
5492 case FLOAT_EXPR:
5493 case FIX_TRUNC_EXPR:
5494 /* Even if we're not being pedantic, we cannot allow this
5495 extension when we're instantiating in a SFINAE
5496 context. */
5497 if (! lvalue_p (arg) && complain == tf_none)
5499 if (complain & tf_error)
5500 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5501 else
5502 return error_mark_node;
5504 break;
5506 case BASELINK:
5507 arg = BASELINK_FUNCTIONS (arg);
5508 /* Fall through. */
5510 case OVERLOAD:
5511 arg = OVL_CURRENT (arg);
5512 break;
5514 case OFFSET_REF:
5515 offset_ref:
5516 /* Turn a reference to a non-static data member into a
5517 pointer-to-member. */
5519 tree type;
5520 tree t;
5522 gcc_assert (PTRMEM_OK_P (arg));
5524 t = TREE_OPERAND (arg, 1);
5525 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5527 if (complain & tf_error)
5528 error ("cannot create pointer to reference member %qD", t);
5529 return error_mark_node;
5532 type = build_ptrmem_type (context_for_name_lookup (t),
5533 TREE_TYPE (t));
5534 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5535 return t;
5538 default:
5539 break;
5542 if (argtype != error_mark_node)
5544 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (argtype)
5545 && (flag_iso || warn_vla > 0))
5547 if (complain & tf_warning_or_error)
5548 pedwarn (input_location, OPT_Wvla,
5549 "taking address of array of runtime bound");
5550 else
5551 return error_mark_node;
5553 argtype = build_pointer_type (argtype);
5556 /* In a template, we are processing a non-dependent expression
5557 so we can just form an ADDR_EXPR with the correct type. */
5558 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5560 val = build_address (arg);
5561 if (TREE_CODE (arg) == OFFSET_REF)
5562 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5564 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5566 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5568 /* We can only get here with a single static member
5569 function. */
5570 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5571 && DECL_STATIC_FUNCTION_P (fn));
5572 mark_used (fn);
5573 val = build_address (fn);
5574 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5575 /* Do not lose object's side effects. */
5576 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5577 TREE_OPERAND (arg, 0), val);
5579 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5581 if (complain & tf_error)
5582 error ("attempt to take address of bit-field structure member %qD",
5583 TREE_OPERAND (arg, 1));
5584 return error_mark_node;
5586 else
5588 tree object = TREE_OPERAND (arg, 0);
5589 tree field = TREE_OPERAND (arg, 1);
5590 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5591 (TREE_TYPE (object), decl_type_context (field)));
5592 val = build_address (arg);
5595 if (TYPE_PTR_P (argtype)
5596 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5598 build_ptrmemfunc_type (argtype);
5599 val = build_ptrmemfunc (argtype, val, 0,
5600 /*c_cast_p=*/false,
5601 complain);
5604 return val;
5607 /* Take the address of ARG if it has one, even if it's an rvalue. */
5609 tree
5610 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5612 return cp_build_addr_expr_1 (arg, 0, complain);
5615 /* Take the address of ARG, but only if it's an lvalue. */
5617 static tree
5618 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5620 return cp_build_addr_expr_1 (arg, 1, complain);
5623 /* C++: Must handle pointers to members.
5625 Perhaps type instantiation should be extended to handle conversion
5626 from aggregates to types we don't yet know we want? (Or are those
5627 cases typically errors which should be reported?)
5629 NOCONVERT nonzero suppresses the default promotions
5630 (such as from short to int). */
5632 tree
5633 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5634 tsubst_flags_t complain)
5636 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5637 tree arg = xarg;
5638 tree argtype = 0;
5639 const char *errstring = NULL;
5640 tree val;
5641 const char *invalid_op_diag;
5643 if (!arg || error_operand_p (arg))
5644 return error_mark_node;
5646 if ((invalid_op_diag
5647 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5648 ? CONVERT_EXPR
5649 : code),
5650 TREE_TYPE (xarg))))
5652 if (complain & tf_error)
5653 error (invalid_op_diag);
5654 return error_mark_node;
5657 switch (code)
5659 case UNARY_PLUS_EXPR:
5660 case NEGATE_EXPR:
5662 int flags = WANT_ARITH | WANT_ENUM;
5663 /* Unary plus (but not unary minus) is allowed on pointers. */
5664 if (code == UNARY_PLUS_EXPR)
5665 flags |= WANT_POINTER;
5666 arg = build_expr_type_conversion (flags, arg, true);
5667 if (!arg)
5668 errstring = (code == NEGATE_EXPR
5669 ? _("wrong type argument to unary minus")
5670 : _("wrong type argument to unary plus"));
5671 else
5673 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5674 arg = cp_perform_integral_promotions (arg, complain);
5676 /* Make sure the result is not an lvalue: a unary plus or minus
5677 expression is always a rvalue. */
5678 arg = rvalue (arg);
5681 break;
5683 case BIT_NOT_EXPR:
5684 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5686 code = CONJ_EXPR;
5687 if (!noconvert)
5689 arg = cp_default_conversion (arg, complain);
5690 if (arg == error_mark_node)
5691 return error_mark_node;
5694 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5695 | WANT_VECTOR_OR_COMPLEX,
5696 arg, true)))
5697 errstring = _("wrong type argument to bit-complement");
5698 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5699 arg = cp_perform_integral_promotions (arg, complain);
5700 break;
5702 case ABS_EXPR:
5703 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5704 errstring = _("wrong type argument to abs");
5705 else if (!noconvert)
5707 arg = cp_default_conversion (arg, complain);
5708 if (arg == error_mark_node)
5709 return error_mark_node;
5711 break;
5713 case CONJ_EXPR:
5714 /* Conjugating a real value is a no-op, but allow it anyway. */
5715 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5716 errstring = _("wrong type argument to conjugation");
5717 else if (!noconvert)
5719 arg = cp_default_conversion (arg, complain);
5720 if (arg == error_mark_node)
5721 return error_mark_node;
5723 break;
5725 case TRUTH_NOT_EXPR:
5726 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5727 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5728 build_zero_cst (TREE_TYPE (arg)), complain);
5729 arg = perform_implicit_conversion (boolean_type_node, arg,
5730 complain);
5731 val = invert_truthvalue_loc (input_location, arg);
5732 if (arg != error_mark_node)
5733 return val;
5734 errstring = _("in argument to unary !");
5735 break;
5737 case NOP_EXPR:
5738 break;
5740 case REALPART_EXPR:
5741 case IMAGPART_EXPR:
5742 arg = build_real_imag_expr (input_location, code, arg);
5743 if (arg == error_mark_node)
5744 return arg;
5745 else
5746 return fold_if_not_in_template (arg);
5748 case PREINCREMENT_EXPR:
5749 case POSTINCREMENT_EXPR:
5750 case PREDECREMENT_EXPR:
5751 case POSTDECREMENT_EXPR:
5752 /* Handle complex lvalues (when permitted)
5753 by reduction to simpler cases. */
5755 val = unary_complex_lvalue (code, arg);
5756 if (val != 0)
5757 return val;
5759 arg = mark_lvalue_use (arg);
5761 /* Increment or decrement the real part of the value,
5762 and don't change the imaginary part. */
5763 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5765 tree real, imag;
5767 arg = stabilize_reference (arg);
5768 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5769 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5770 real = cp_build_unary_op (code, real, 1, complain);
5771 if (real == error_mark_node || imag == error_mark_node)
5772 return error_mark_node;
5773 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5774 real, imag);
5777 /* Report invalid types. */
5779 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5780 arg, true)))
5782 if (code == PREINCREMENT_EXPR)
5783 errstring = _("no pre-increment operator for type");
5784 else if (code == POSTINCREMENT_EXPR)
5785 errstring = _("no post-increment operator for type");
5786 else if (code == PREDECREMENT_EXPR)
5787 errstring = _("no pre-decrement operator for type");
5788 else
5789 errstring = _("no post-decrement operator for type");
5790 break;
5792 else if (arg == error_mark_node)
5793 return error_mark_node;
5795 /* Report something read-only. */
5797 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5798 || TREE_READONLY (arg))
5800 if (complain & tf_error)
5801 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5802 || code == POSTINCREMENT_EXPR)
5803 ? lv_increment : lv_decrement));
5804 else
5805 return error_mark_node;
5809 tree inc;
5810 tree declared_type = unlowered_expr_type (arg);
5812 argtype = TREE_TYPE (arg);
5814 /* ARM $5.2.5 last annotation says this should be forbidden. */
5815 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5817 if (complain & tf_error)
5818 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5819 ? G_("ISO C++ forbids incrementing an enum")
5820 : G_("ISO C++ forbids decrementing an enum"));
5821 else
5822 return error_mark_node;
5825 /* Compute the increment. */
5827 if (TYPE_PTR_P (argtype))
5829 tree type = complete_type (TREE_TYPE (argtype));
5831 if (!COMPLETE_OR_VOID_TYPE_P (type))
5833 if (complain & tf_error)
5834 error (((code == PREINCREMENT_EXPR
5835 || code == POSTINCREMENT_EXPR))
5836 ? G_("cannot increment a pointer to incomplete type %qT")
5837 : G_("cannot decrement a pointer to incomplete type %qT"),
5838 TREE_TYPE (argtype));
5839 else
5840 return error_mark_node;
5842 else if (!TYPE_PTROB_P (argtype))
5844 if (complain & tf_error)
5845 pedwarn (input_location, OPT_Wpointer_arith,
5846 (code == PREINCREMENT_EXPR
5847 || code == POSTINCREMENT_EXPR)
5848 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5849 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5850 argtype);
5851 else
5852 return error_mark_node;
5855 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5857 else
5858 inc = VECTOR_TYPE_P (argtype)
5859 ? build_one_cst (argtype)
5860 : integer_one_node;
5862 inc = cp_convert (argtype, inc, complain);
5864 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5865 need to ask Objective-C to build the increment or decrement
5866 expression for it. */
5867 if (objc_is_property_ref (arg))
5868 return objc_build_incr_expr_for_property_ref (input_location, code,
5869 arg, inc);
5871 /* Complain about anything else that is not a true lvalue. */
5872 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5873 || code == POSTINCREMENT_EXPR)
5874 ? lv_increment : lv_decrement),
5875 complain))
5876 return error_mark_node;
5878 /* Forbid using -- on `bool'. */
5879 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5881 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5883 if (complain & tf_error)
5884 error ("invalid use of Boolean expression as operand "
5885 "to %<operator--%>");
5886 return error_mark_node;
5888 val = boolean_increment (code, arg);
5890 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5891 /* An rvalue has no cv-qualifiers. */
5892 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5893 else
5894 val = build2 (code, TREE_TYPE (arg), arg, inc);
5896 TREE_SIDE_EFFECTS (val) = 1;
5897 return val;
5900 case ADDR_EXPR:
5901 /* Note that this operation never does default_conversion
5902 regardless of NOCONVERT. */
5903 return cp_build_addr_expr (arg, complain);
5905 default:
5906 break;
5909 if (!errstring)
5911 if (argtype == 0)
5912 argtype = TREE_TYPE (arg);
5913 return fold_if_not_in_template (build1 (code, argtype, arg));
5916 if (complain & tf_error)
5917 error ("%s", errstring);
5918 return error_mark_node;
5921 /* Hook for the c-common bits that build a unary op. */
5922 tree
5923 build_unary_op (location_t /*location*/,
5924 enum tree_code code, tree xarg, int noconvert)
5926 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5929 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5930 for certain kinds of expressions which are not really lvalues
5931 but which we can accept as lvalues.
5933 If ARG is not a kind of expression we can handle, return
5934 NULL_TREE. */
5936 tree
5937 unary_complex_lvalue (enum tree_code code, tree arg)
5939 /* Inside a template, making these kinds of adjustments is
5940 pointless; we are only concerned with the type of the
5941 expression. */
5942 if (processing_template_decl)
5943 return NULL_TREE;
5945 /* Handle (a, b) used as an "lvalue". */
5946 if (TREE_CODE (arg) == COMPOUND_EXPR)
5948 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5949 tf_warning_or_error);
5950 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5951 TREE_OPERAND (arg, 0), real_result);
5954 /* Handle (a ? b : c) used as an "lvalue". */
5955 if (TREE_CODE (arg) == COND_EXPR
5956 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5957 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5959 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5960 if (TREE_CODE (arg) == MODIFY_EXPR
5961 || TREE_CODE (arg) == PREINCREMENT_EXPR
5962 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5964 tree lvalue = TREE_OPERAND (arg, 0);
5965 if (TREE_SIDE_EFFECTS (lvalue))
5967 lvalue = stabilize_reference (lvalue);
5968 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5969 lvalue, TREE_OPERAND (arg, 1));
5971 return unary_complex_lvalue
5972 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5975 if (code != ADDR_EXPR)
5976 return NULL_TREE;
5978 /* Handle (a = b) used as an "lvalue" for `&'. */
5979 if (TREE_CODE (arg) == MODIFY_EXPR
5980 || TREE_CODE (arg) == INIT_EXPR)
5982 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5983 tf_warning_or_error);
5984 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5985 arg, real_result);
5986 TREE_NO_WARNING (arg) = 1;
5987 return arg;
5990 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5991 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5992 || TREE_CODE (arg) == OFFSET_REF)
5993 return NULL_TREE;
5995 /* We permit compiler to make function calls returning
5996 objects of aggregate type look like lvalues. */
5998 tree targ = arg;
6000 if (TREE_CODE (targ) == SAVE_EXPR)
6001 targ = TREE_OPERAND (targ, 0);
6003 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6005 if (TREE_CODE (arg) == SAVE_EXPR)
6006 targ = arg;
6007 else
6008 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6009 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6012 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6013 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6014 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6017 /* Don't let anything else be handled specially. */
6018 return NULL_TREE;
6021 /* Mark EXP saying that we need to be able to take the
6022 address of it; it should not be allocated in a register.
6023 Value is true if successful.
6025 C++: we do not allow `current_class_ptr' to be addressable. */
6027 bool
6028 cxx_mark_addressable (tree exp)
6030 tree x = exp;
6032 while (1)
6033 switch (TREE_CODE (x))
6035 case ADDR_EXPR:
6036 case COMPONENT_REF:
6037 case ARRAY_REF:
6038 case REALPART_EXPR:
6039 case IMAGPART_EXPR:
6040 x = TREE_OPERAND (x, 0);
6041 break;
6043 case PARM_DECL:
6044 if (x == current_class_ptr)
6046 error ("cannot take the address of %<this%>, which is an rvalue expression");
6047 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6048 return true;
6050 /* Fall through. */
6052 case VAR_DECL:
6053 /* Caller should not be trying to mark initialized
6054 constant fields addressable. */
6055 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6056 || DECL_IN_AGGR_P (x) == 0
6057 || TREE_STATIC (x)
6058 || DECL_EXTERNAL (x));
6059 /* Fall through. */
6061 case RESULT_DECL:
6062 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6063 && !DECL_ARTIFICIAL (x))
6065 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6067 error
6068 ("address of explicit register variable %qD requested", x);
6069 return false;
6071 else if (extra_warnings)
6072 warning
6073 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6075 TREE_ADDRESSABLE (x) = 1;
6076 return true;
6078 case CONST_DECL:
6079 case FUNCTION_DECL:
6080 TREE_ADDRESSABLE (x) = 1;
6081 return true;
6083 case CONSTRUCTOR:
6084 TREE_ADDRESSABLE (x) = 1;
6085 return true;
6087 case TARGET_EXPR:
6088 TREE_ADDRESSABLE (x) = 1;
6089 cxx_mark_addressable (TREE_OPERAND (x, 0));
6090 return true;
6092 default:
6093 return true;
6097 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6099 tree
6100 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6101 tsubst_flags_t complain)
6103 tree orig_ifexp = ifexp;
6104 tree orig_op1 = op1;
6105 tree orig_op2 = op2;
6106 tree expr;
6108 if (processing_template_decl)
6110 /* The standard says that the expression is type-dependent if
6111 IFEXP is type-dependent, even though the eventual type of the
6112 expression doesn't dependent on IFEXP. */
6113 if (type_dependent_expression_p (ifexp)
6114 /* As a GNU extension, the middle operand may be omitted. */
6115 || (op1 && type_dependent_expression_p (op1))
6116 || type_dependent_expression_p (op2))
6117 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6118 ifexp = build_non_dependent_expr (ifexp);
6119 if (op1)
6120 op1 = build_non_dependent_expr (op1);
6121 op2 = build_non_dependent_expr (op2);
6124 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6125 if (processing_template_decl && expr != error_mark_node
6126 && TREE_CODE (expr) != VEC_COND_EXPR)
6128 tree min = build_min_non_dep (COND_EXPR, expr,
6129 orig_ifexp, orig_op1, orig_op2);
6130 /* In C++11, remember that the result is an lvalue or xvalue.
6131 In C++98, lvalue_kind can just assume lvalue in a template. */
6132 if (cxx_dialect >= cxx11
6133 && lvalue_or_rvalue_with_address_p (expr)
6134 && !lvalue_or_rvalue_with_address_p (min))
6135 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6136 !real_lvalue_p (expr));
6137 expr = convert_from_reference (min);
6139 return expr;
6142 /* Given a list of expressions, return a compound expression
6143 that performs them all and returns the value of the last of them. */
6145 tree
6146 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6147 tsubst_flags_t complain)
6149 tree expr = TREE_VALUE (list);
6151 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6152 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6154 if (complain & tf_error)
6155 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6156 "list-initializer for non-class type must not "
6157 "be parenthesized");
6158 else
6159 return error_mark_node;
6162 if (TREE_CHAIN (list))
6164 if (complain & tf_error)
6165 switch (exp)
6167 case ELK_INIT:
6168 permerror (input_location, "expression list treated as compound "
6169 "expression in initializer");
6170 break;
6171 case ELK_MEM_INIT:
6172 permerror (input_location, "expression list treated as compound "
6173 "expression in mem-initializer");
6174 break;
6175 case ELK_FUNC_CAST:
6176 permerror (input_location, "expression list treated as compound "
6177 "expression in functional cast");
6178 break;
6179 default:
6180 gcc_unreachable ();
6182 else
6183 return error_mark_node;
6185 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6186 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6187 expr, TREE_VALUE (list), complain);
6190 return expr;
6193 /* Like build_x_compound_expr_from_list, but using a VEC. */
6195 tree
6196 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6197 tsubst_flags_t complain)
6199 if (vec_safe_is_empty (vec))
6200 return NULL_TREE;
6201 else if (vec->length () == 1)
6202 return (*vec)[0];
6203 else
6205 tree expr;
6206 unsigned int ix;
6207 tree t;
6209 if (msg != NULL)
6211 if (complain & tf_error)
6212 permerror (input_location,
6213 "%s expression list treated as compound expression",
6214 msg);
6215 else
6216 return error_mark_node;
6219 expr = (*vec)[0];
6220 for (ix = 1; vec->iterate (ix, &t); ++ix)
6221 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6222 t, complain);
6224 return expr;
6228 /* Handle overloading of the ',' operator when needed. */
6230 tree
6231 build_x_compound_expr (location_t loc, tree op1, tree op2,
6232 tsubst_flags_t complain)
6234 tree result;
6235 tree orig_op1 = op1;
6236 tree orig_op2 = op2;
6238 if (processing_template_decl)
6240 if (type_dependent_expression_p (op1)
6241 || type_dependent_expression_p (op2))
6242 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6243 op1 = build_non_dependent_expr (op1);
6244 op2 = build_non_dependent_expr (op2);
6247 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6248 NULL_TREE, /*overload=*/NULL, complain);
6249 if (!result)
6250 result = cp_build_compound_expr (op1, op2, complain);
6252 if (processing_template_decl && result != error_mark_node)
6253 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6255 return result;
6258 /* Like cp_build_compound_expr, but for the c-common bits. */
6260 tree
6261 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6263 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6266 /* Build a compound expression. */
6268 tree
6269 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6271 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6273 if (lhs == error_mark_node || rhs == error_mark_node)
6274 return error_mark_node;
6276 if (flag_cilkplus
6277 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6278 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6280 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6281 : EXPR_LOCATION (rhs));
6282 error_at (loc,
6283 "spawned function call cannot be part of a comma expression");
6284 return error_mark_node;
6287 if (TREE_CODE (rhs) == TARGET_EXPR)
6289 /* If the rhs is a TARGET_EXPR, then build the compound
6290 expression inside the target_expr's initializer. This
6291 helps the compiler to eliminate unnecessary temporaries. */
6292 tree init = TREE_OPERAND (rhs, 1);
6294 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6295 TREE_OPERAND (rhs, 1) = init;
6297 return rhs;
6300 if (type_unknown_p (rhs))
6302 if (complain & tf_error)
6303 error ("no context to resolve type of %qE", rhs);
6304 return error_mark_node;
6307 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6310 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6311 casts away constness. CAST gives the type of cast. Returns true
6312 if the cast is ill-formed, false if it is well-formed.
6314 ??? This function warns for casting away any qualifier not just
6315 const. We would like to specify exactly what qualifiers are casted
6316 away.
6319 static bool
6320 check_for_casting_away_constness (tree src_type, tree dest_type,
6321 enum tree_code cast, tsubst_flags_t complain)
6323 /* C-style casts are allowed to cast away constness. With
6324 WARN_CAST_QUAL, we still want to issue a warning. */
6325 if (cast == CAST_EXPR && !warn_cast_qual)
6326 return false;
6328 if (!casts_away_constness (src_type, dest_type, complain))
6329 return false;
6331 switch (cast)
6333 case CAST_EXPR:
6334 if (complain & tf_warning)
6335 warning (OPT_Wcast_qual,
6336 "cast from type %qT to type %qT casts away qualifiers",
6337 src_type, dest_type);
6338 return false;
6340 case STATIC_CAST_EXPR:
6341 if (complain & tf_error)
6342 error ("static_cast from type %qT to type %qT casts away qualifiers",
6343 src_type, dest_type);
6344 return true;
6346 case REINTERPRET_CAST_EXPR:
6347 if (complain & tf_error)
6348 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6349 src_type, dest_type);
6350 return true;
6352 default:
6353 gcc_unreachable();
6358 Warns if the cast from expression EXPR to type TYPE is useless.
6360 void
6361 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6363 if (warn_useless_cast
6364 && complain & tf_warning)
6366 /* In C++14 mode, this interacts badly with force_paren_expr. And it
6367 isn't necessary in any mode, because the code below handles
6368 glvalues properly. For 4.9, just skip it in C++14 mode. */
6369 if (cxx_dialect < cxx14 && REFERENCE_REF_P (expr))
6370 expr = TREE_OPERAND (expr, 0);
6372 if ((TREE_CODE (type) == REFERENCE_TYPE
6373 && (TYPE_REF_IS_RVALUE (type)
6374 ? xvalue_p (expr) : real_lvalue_p (expr))
6375 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6376 || same_type_p (TREE_TYPE (expr), type))
6377 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6381 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6382 (another pointer-to-member type in the same hierarchy) and return
6383 the converted expression. If ALLOW_INVERSE_P is permitted, a
6384 pointer-to-derived may be converted to pointer-to-base; otherwise,
6385 only the other direction is permitted. If C_CAST_P is true, this
6386 conversion is taking place as part of a C-style cast. */
6388 tree
6389 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6390 bool c_cast_p, tsubst_flags_t complain)
6392 if (TYPE_PTRDATAMEM_P (type))
6394 tree delta;
6396 if (TREE_CODE (expr) == PTRMEM_CST)
6397 expr = cplus_expand_constant (expr);
6398 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6399 TYPE_PTRMEM_CLASS_TYPE (type),
6400 allow_inverse_p,
6401 c_cast_p, complain);
6402 if (delta == error_mark_node)
6403 return error_mark_node;
6405 if (!integer_zerop (delta))
6407 tree cond, op1, op2;
6409 cond = cp_build_binary_op (input_location,
6410 EQ_EXPR,
6411 expr,
6412 build_int_cst (TREE_TYPE (expr), -1),
6413 complain);
6414 op1 = build_nop (ptrdiff_type_node, expr);
6415 op2 = cp_build_binary_op (input_location,
6416 PLUS_EXPR, op1, delta,
6417 complain);
6419 expr = fold_build3_loc (input_location,
6420 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6424 return build_nop (type, expr);
6426 else
6427 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6428 allow_inverse_p, c_cast_p, complain);
6431 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6432 this static_cast is being attempted as one of the possible casts
6433 allowed by a C-style cast. (In that case, accessibility of base
6434 classes is not considered, and it is OK to cast away
6435 constness.) Return the result of the cast. *VALID_P is set to
6436 indicate whether or not the cast was valid. */
6438 static tree
6439 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6440 bool *valid_p, tsubst_flags_t complain)
6442 tree intype;
6443 tree result;
6444 cp_lvalue_kind clk;
6446 /* Assume the cast is valid. */
6447 *valid_p = true;
6449 intype = unlowered_expr_type (expr);
6451 /* Save casted types in the function's used types hash table. */
6452 used_types_insert (type);
6454 /* [expr.static.cast]
6456 An lvalue of type "cv1 B", where B is a class type, can be cast
6457 to type "reference to cv2 D", where D is a class derived (clause
6458 _class.derived_) from B, if a valid standard conversion from
6459 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6460 same cv-qualification as, or greater cv-qualification than, cv1,
6461 and B is not a virtual base class of D. */
6462 /* We check this case before checking the validity of "TYPE t =
6463 EXPR;" below because for this case:
6465 struct B {};
6466 struct D : public B { D(const B&); };
6467 extern B& b;
6468 void f() { static_cast<const D&>(b); }
6470 we want to avoid constructing a new D. The standard is not
6471 completely clear about this issue, but our interpretation is
6472 consistent with other compilers. */
6473 if (TREE_CODE (type) == REFERENCE_TYPE
6474 && CLASS_TYPE_P (TREE_TYPE (type))
6475 && CLASS_TYPE_P (intype)
6476 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6477 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6478 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6479 build_pointer_type (TYPE_MAIN_VARIANT
6480 (TREE_TYPE (type))),
6481 complain)
6482 && (c_cast_p
6483 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6485 tree base;
6487 /* There is a standard conversion from "D*" to "B*" even if "B"
6488 is ambiguous or inaccessible. If this is really a
6489 static_cast, then we check both for inaccessibility and
6490 ambiguity. However, if this is a static_cast being performed
6491 because the user wrote a C-style cast, then accessibility is
6492 not considered. */
6493 base = lookup_base (TREE_TYPE (type), intype,
6494 c_cast_p ? ba_unique : ba_check,
6495 NULL, complain);
6497 /* Convert from "B*" to "D*". This function will check that "B"
6498 is not a virtual base of "D". */
6499 expr = build_base_path (MINUS_EXPR, build_address (expr),
6500 base, /*nonnull=*/false, complain);
6501 /* Convert the pointer to a reference -- but then remember that
6502 there are no expressions with reference type in C++.
6504 We call rvalue so that there's an actual tree code
6505 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6506 is a variable with the same type, the conversion would get folded
6507 away, leaving just the variable and causing lvalue_kind to give
6508 the wrong answer. */
6509 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6512 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6513 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6514 if (TREE_CODE (type) == REFERENCE_TYPE
6515 && TYPE_REF_IS_RVALUE (type)
6516 && (clk = real_lvalue_p (expr))
6517 && reference_related_p (TREE_TYPE (type), intype)
6518 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6520 if (clk == clk_ordinary)
6522 /* Handle the (non-bit-field) lvalue case here by casting to
6523 lvalue reference and then changing it to an rvalue reference.
6524 Casting an xvalue to rvalue reference will be handled by the
6525 main code path. */
6526 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6527 result = (perform_direct_initialization_if_possible
6528 (lref, expr, c_cast_p, complain));
6529 result = cp_fold_convert (type, result);
6530 /* Make sure we don't fold back down to a named rvalue reference,
6531 because that would be an lvalue. */
6532 if (DECL_P (result))
6533 result = build1 (NON_LVALUE_EXPR, type, result);
6534 return convert_from_reference (result);
6536 else
6537 /* For a bit-field or packed field, bind to a temporary. */
6538 expr = rvalue (expr);
6541 /* Resolve overloaded address here rather than once in
6542 implicit_conversion and again in the inverse code below. */
6543 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6545 expr = instantiate_type (type, expr, complain);
6546 intype = TREE_TYPE (expr);
6549 /* [expr.static.cast]
6551 Any expression can be explicitly converted to type cv void. */
6552 if (VOID_TYPE_P (type))
6553 return convert_to_void (expr, ICV_CAST, complain);
6555 /* [class.abstract]
6556 An abstract class shall not be used ... as the type of an explicit
6557 conversion. */
6558 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6559 return error_mark_node;
6561 /* [expr.static.cast]
6563 An expression e can be explicitly converted to a type T using a
6564 static_cast of the form static_cast<T>(e) if the declaration T
6565 t(e);" is well-formed, for some invented temporary variable
6566 t. */
6567 result = perform_direct_initialization_if_possible (type, expr,
6568 c_cast_p, complain);
6569 if (result)
6571 result = convert_from_reference (result);
6573 /* [expr.static.cast]
6575 If T is a reference type, the result is an lvalue; otherwise,
6576 the result is an rvalue. */
6577 if (TREE_CODE (type) != REFERENCE_TYPE)
6578 result = rvalue (result);
6579 return result;
6582 /* [expr.static.cast]
6584 The inverse of any standard conversion sequence (clause _conv_),
6585 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6586 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6587 (_conv.bool_) conversions, can be performed explicitly using
6588 static_cast subject to the restriction that the explicit
6589 conversion does not cast away constness (_expr.const.cast_), and
6590 the following additional rules for specific cases: */
6591 /* For reference, the conversions not excluded are: integral
6592 promotions, floating point promotion, integral conversions,
6593 floating point conversions, floating-integral conversions,
6594 pointer conversions, and pointer to member conversions. */
6595 /* DR 128
6597 A value of integral _or enumeration_ type can be explicitly
6598 converted to an enumeration type. */
6599 /* The effect of all that is that any conversion between any two
6600 types which are integral, floating, or enumeration types can be
6601 performed. */
6602 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6603 || SCALAR_FLOAT_TYPE_P (type))
6604 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6605 || SCALAR_FLOAT_TYPE_P (intype)))
6606 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6608 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6609 && CLASS_TYPE_P (TREE_TYPE (type))
6610 && CLASS_TYPE_P (TREE_TYPE (intype))
6611 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6612 (TREE_TYPE (intype))),
6613 build_pointer_type (TYPE_MAIN_VARIANT
6614 (TREE_TYPE (type))),
6615 complain))
6617 tree base;
6619 if (!c_cast_p
6620 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6621 complain))
6622 return error_mark_node;
6623 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6624 c_cast_p ? ba_unique : ba_check,
6625 NULL, complain);
6626 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6627 complain);
6628 return cp_fold_convert(type, expr);
6631 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6632 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6634 tree c1;
6635 tree c2;
6636 tree t1;
6637 tree t2;
6639 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6640 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6642 if (TYPE_PTRDATAMEM_P (type))
6644 t1 = (build_ptrmem_type
6645 (c1,
6646 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6647 t2 = (build_ptrmem_type
6648 (c2,
6649 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6651 else
6653 t1 = intype;
6654 t2 = type;
6656 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6658 if (!c_cast_p
6659 && check_for_casting_away_constness (intype, type,
6660 STATIC_CAST_EXPR,
6661 complain))
6662 return error_mark_node;
6663 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6664 c_cast_p, complain);
6668 /* [expr.static.cast]
6670 An rvalue of type "pointer to cv void" can be explicitly
6671 converted to a pointer to object type. A value of type pointer
6672 to object converted to "pointer to cv void" and back to the
6673 original pointer type will have its original value. */
6674 if (TYPE_PTR_P (intype)
6675 && VOID_TYPE_P (TREE_TYPE (intype))
6676 && TYPE_PTROB_P (type))
6678 if (!c_cast_p
6679 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6680 complain))
6681 return error_mark_node;
6682 return build_nop (type, expr);
6685 *valid_p = false;
6686 return error_mark_node;
6689 /* Return an expression representing static_cast<TYPE>(EXPR). */
6691 tree
6692 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6694 tree result;
6695 bool valid_p;
6697 if (type == error_mark_node || expr == error_mark_node)
6698 return error_mark_node;
6700 if (processing_template_decl)
6702 expr = build_min (STATIC_CAST_EXPR, type, expr);
6703 /* We don't know if it will or will not have side effects. */
6704 TREE_SIDE_EFFECTS (expr) = 1;
6705 return convert_from_reference (expr);
6708 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6709 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6710 if (TREE_CODE (type) != REFERENCE_TYPE
6711 && TREE_CODE (expr) == NOP_EXPR
6712 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6713 expr = TREE_OPERAND (expr, 0);
6715 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6716 complain);
6717 if (valid_p)
6719 if (result != error_mark_node)
6720 maybe_warn_about_useless_cast (type, expr, complain);
6721 return result;
6724 if (complain & tf_error)
6725 error ("invalid static_cast from type %qT to type %qT",
6726 TREE_TYPE (expr), type);
6727 return error_mark_node;
6730 /* EXPR is an expression with member function or pointer-to-member
6731 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6732 not permitted by ISO C++, but we accept it in some modes. If we
6733 are not in one of those modes, issue a diagnostic. Return the
6734 converted expression. */
6736 tree
6737 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6739 tree intype;
6740 tree decl;
6742 intype = TREE_TYPE (expr);
6743 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6744 || TREE_CODE (intype) == METHOD_TYPE);
6746 if (!(complain & tf_warning_or_error))
6747 return error_mark_node;
6749 if (pedantic || warn_pmf2ptr)
6750 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6751 "converting from %qT to %qT", intype, type);
6753 if (TREE_CODE (intype) == METHOD_TYPE)
6754 expr = build_addr_func (expr, complain);
6755 else if (TREE_CODE (expr) == PTRMEM_CST)
6756 expr = build_address (PTRMEM_CST_MEMBER (expr));
6757 else
6759 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6760 decl = build_address (decl);
6761 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6764 if (expr == error_mark_node)
6765 return error_mark_node;
6767 return build_nop (type, expr);
6770 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6771 If C_CAST_P is true, this reinterpret cast is being done as part of
6772 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6773 indicate whether or not reinterpret_cast was valid. */
6775 static tree
6776 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6777 bool *valid_p, tsubst_flags_t complain)
6779 tree intype;
6781 /* Assume the cast is invalid. */
6782 if (valid_p)
6783 *valid_p = true;
6785 if (type == error_mark_node || error_operand_p (expr))
6786 return error_mark_node;
6788 intype = TREE_TYPE (expr);
6790 /* Save casted types in the function's used types hash table. */
6791 used_types_insert (type);
6793 /* [expr.reinterpret.cast]
6794 An lvalue expression of type T1 can be cast to the type
6795 "reference to T2" if an expression of type "pointer to T1" can be
6796 explicitly converted to the type "pointer to T2" using a
6797 reinterpret_cast. */
6798 if (TREE_CODE (type) == REFERENCE_TYPE)
6800 if (! real_lvalue_p (expr))
6802 if (complain & tf_error)
6803 error ("invalid cast of an rvalue expression of type "
6804 "%qT to type %qT",
6805 intype, type);
6806 return error_mark_node;
6809 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6810 "B" are related class types; the reinterpret_cast does not
6811 adjust the pointer. */
6812 if (TYPE_PTR_P (intype)
6813 && (complain & tf_warning)
6814 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6815 COMPARE_BASE | COMPARE_DERIVED)))
6816 warning (0, "casting %qT to %qT does not dereference pointer",
6817 intype, type);
6819 expr = cp_build_addr_expr (expr, complain);
6821 if (warn_strict_aliasing > 2)
6822 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6824 if (expr != error_mark_node)
6825 expr = build_reinterpret_cast_1
6826 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6827 valid_p, complain);
6828 if (expr != error_mark_node)
6829 /* cp_build_indirect_ref isn't right for rvalue refs. */
6830 expr = convert_from_reference (fold_convert (type, expr));
6831 return expr;
6834 /* As a G++ extension, we consider conversions from member
6835 functions, and pointers to member functions to
6836 pointer-to-function and pointer-to-void types. If
6837 -Wno-pmf-conversions has not been specified,
6838 convert_member_func_to_ptr will issue an error message. */
6839 if ((TYPE_PTRMEMFUNC_P (intype)
6840 || TREE_CODE (intype) == METHOD_TYPE)
6841 && TYPE_PTR_P (type)
6842 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6843 || VOID_TYPE_P (TREE_TYPE (type))))
6844 return convert_member_func_to_ptr (type, expr, complain);
6846 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6847 array-to-pointer, and function-to-pointer conversions are
6848 performed. */
6849 expr = decay_conversion (expr, complain);
6851 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6852 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6853 if (TREE_CODE (expr) == NOP_EXPR
6854 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6855 expr = TREE_OPERAND (expr, 0);
6857 if (error_operand_p (expr))
6858 return error_mark_node;
6860 intype = TREE_TYPE (expr);
6862 /* [expr.reinterpret.cast]
6863 A pointer can be converted to any integral type large enough to
6864 hold it. ... A value of type std::nullptr_t can be converted to
6865 an integral type; the conversion has the same meaning and
6866 validity as a conversion of (void*)0 to the integral type. */
6867 if (CP_INTEGRAL_TYPE_P (type)
6868 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6870 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6872 if (complain & tf_error)
6873 permerror (input_location, "cast from %qT to %qT loses precision",
6874 intype, type);
6875 else
6876 return error_mark_node;
6878 if (NULLPTR_TYPE_P (intype))
6879 return build_int_cst (type, 0);
6881 /* [expr.reinterpret.cast]
6882 A value of integral or enumeration type can be explicitly
6883 converted to a pointer. */
6884 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6885 /* OK */
6887 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6888 || TYPE_PTR_OR_PTRMEM_P (type))
6889 && same_type_p (type, intype))
6890 /* DR 799 */
6891 return rvalue (expr);
6892 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6893 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6894 return fold_if_not_in_template (build_nop (type, expr));
6895 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6896 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6898 tree sexpr = expr;
6900 if (!c_cast_p
6901 && check_for_casting_away_constness (intype, type,
6902 REINTERPRET_CAST_EXPR,
6903 complain))
6904 return error_mark_node;
6905 /* Warn about possible alignment problems. */
6906 if (STRICT_ALIGNMENT && warn_cast_align
6907 && (complain & tf_warning)
6908 && !VOID_TYPE_P (type)
6909 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6910 && COMPLETE_TYPE_P (TREE_TYPE (type))
6911 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6912 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6913 warning (OPT_Wcast_align, "cast from %qT to %qT "
6914 "increases required alignment of target type", intype, type);
6916 /* We need to strip nops here, because the front end likes to
6917 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6918 STRIP_NOPS (sexpr);
6919 if (warn_strict_aliasing <= 2)
6920 strict_aliasing_warning (intype, type, sexpr);
6922 return fold_if_not_in_template (build_nop (type, expr));
6924 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6925 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6927 if (complain & tf_warning)
6928 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6929 object pointer type or vice versa is conditionally-supported." */
6930 warning (OPT_Wconditionally_supported,
6931 "casting between pointer-to-function and pointer-to-object "
6932 "is conditionally-supported");
6933 return fold_if_not_in_template (build_nop (type, expr));
6935 else if (TREE_CODE (type) == VECTOR_TYPE)
6936 return fold_if_not_in_template (convert_to_vector (type, expr));
6937 else if (TREE_CODE (intype) == VECTOR_TYPE
6938 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6939 return fold_if_not_in_template (convert_to_integer (type, expr));
6940 else
6942 if (valid_p)
6943 *valid_p = false;
6944 if (complain & tf_error)
6945 error ("invalid cast from type %qT to type %qT", intype, type);
6946 return error_mark_node;
6949 return cp_convert (type, expr, complain);
6952 tree
6953 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6955 tree r;
6957 if (type == error_mark_node || expr == error_mark_node)
6958 return error_mark_node;
6960 if (processing_template_decl)
6962 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6964 if (!TREE_SIDE_EFFECTS (t)
6965 && type_dependent_expression_p (expr))
6966 /* There might turn out to be side effects inside expr. */
6967 TREE_SIDE_EFFECTS (t) = 1;
6968 return convert_from_reference (t);
6971 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6972 /*valid_p=*/NULL, complain);
6973 if (r != error_mark_node)
6974 maybe_warn_about_useless_cast (type, expr, complain);
6975 return r;
6978 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6979 return an appropriate expression. Otherwise, return
6980 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6981 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6982 performing a C-style cast, its value upon return will indicate
6983 whether or not the conversion succeeded. */
6985 static tree
6986 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6987 bool *valid_p)
6989 tree src_type;
6990 tree reference_type;
6992 /* Callers are responsible for handling error_mark_node as a
6993 destination type. */
6994 gcc_assert (dst_type != error_mark_node);
6995 /* In a template, callers should be building syntactic
6996 representations of casts, not using this machinery. */
6997 gcc_assert (!processing_template_decl);
6999 /* Assume the conversion is invalid. */
7000 if (valid_p)
7001 *valid_p = false;
7003 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7005 if (complain & tf_error)
7006 error ("invalid use of const_cast with type %qT, "
7007 "which is not a pointer, "
7008 "reference, nor a pointer-to-data-member type", dst_type);
7009 return error_mark_node;
7012 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7014 if (complain & tf_error)
7015 error ("invalid use of const_cast with type %qT, which is a pointer "
7016 "or reference to a function type", dst_type);
7017 return error_mark_node;
7020 /* Save casted types in the function's used types hash table. */
7021 used_types_insert (dst_type);
7023 src_type = TREE_TYPE (expr);
7024 /* Expressions do not really have reference types. */
7025 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7026 src_type = TREE_TYPE (src_type);
7028 /* [expr.const.cast]
7030 For two object types T1 and T2, if a pointer to T1 can be explicitly
7031 converted to the type "pointer to T2" using a const_cast, then the
7032 following conversions can also be made:
7034 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7035 type T2 using the cast const_cast<T2&>;
7037 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7038 type T2 using the cast const_cast<T2&&>; and
7040 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7041 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7043 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7045 reference_type = dst_type;
7046 if (!TYPE_REF_IS_RVALUE (dst_type)
7047 ? real_lvalue_p (expr)
7048 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7049 ? lvalue_p (expr)
7050 : lvalue_or_rvalue_with_address_p (expr)))
7051 /* OK. */;
7052 else
7054 if (complain & tf_error)
7055 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7056 src_type, dst_type);
7057 return error_mark_node;
7059 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7060 src_type = build_pointer_type (src_type);
7062 else
7064 reference_type = NULL_TREE;
7065 /* If the destination type is not a reference type, the
7066 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7067 conversions are performed. */
7068 src_type = type_decays_to (src_type);
7069 if (src_type == error_mark_node)
7070 return error_mark_node;
7073 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7075 if (comp_ptr_ttypes_const (dst_type, src_type))
7077 if (valid_p)
7079 *valid_p = true;
7080 /* This cast is actually a C-style cast. Issue a warning if
7081 the user is making a potentially unsafe cast. */
7082 check_for_casting_away_constness (src_type, dst_type,
7083 CAST_EXPR, complain);
7085 if (reference_type)
7087 expr = cp_build_addr_expr (expr, complain);
7088 if (expr == error_mark_node)
7089 return error_mark_node;
7090 expr = build_nop (reference_type, expr);
7091 return convert_from_reference (expr);
7093 else
7095 expr = decay_conversion (expr, complain);
7096 if (expr == error_mark_node)
7097 return error_mark_node;
7099 /* build_c_cast puts on a NOP_EXPR to make the result not an
7100 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7101 non-lvalue context. */
7102 if (TREE_CODE (expr) == NOP_EXPR
7103 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7104 expr = TREE_OPERAND (expr, 0);
7105 return build_nop (dst_type, expr);
7108 else if (valid_p
7109 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7110 TREE_TYPE (src_type)))
7111 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7112 complain);
7115 if (complain & tf_error)
7116 error ("invalid const_cast from type %qT to type %qT",
7117 src_type, dst_type);
7118 return error_mark_node;
7121 tree
7122 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7124 tree r;
7126 if (type == error_mark_node || error_operand_p (expr))
7127 return error_mark_node;
7129 if (processing_template_decl)
7131 tree t = build_min (CONST_CAST_EXPR, type, expr);
7133 if (!TREE_SIDE_EFFECTS (t)
7134 && type_dependent_expression_p (expr))
7135 /* There might turn out to be side effects inside expr. */
7136 TREE_SIDE_EFFECTS (t) = 1;
7137 return convert_from_reference (t);
7140 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7141 if (r != error_mark_node)
7142 maybe_warn_about_useless_cast (type, expr, complain);
7143 return r;
7146 /* Like cp_build_c_cast, but for the c-common bits. */
7148 tree
7149 build_c_cast (location_t /*loc*/, tree type, tree expr)
7151 return cp_build_c_cast (type, expr, tf_warning_or_error);
7154 /* Build an expression representing an explicit C-style cast to type
7155 TYPE of expression EXPR. */
7157 tree
7158 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7160 tree value = expr;
7161 tree result;
7162 bool valid_p;
7164 if (type == error_mark_node || error_operand_p (expr))
7165 return error_mark_node;
7167 if (processing_template_decl)
7169 tree t = build_min (CAST_EXPR, type,
7170 tree_cons (NULL_TREE, value, NULL_TREE));
7171 /* We don't know if it will or will not have side effects. */
7172 TREE_SIDE_EFFECTS (t) = 1;
7173 return convert_from_reference (t);
7176 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7177 'Class') should always be retained, because this information aids
7178 in method lookup. */
7179 if (objc_is_object_ptr (type)
7180 && objc_is_object_ptr (TREE_TYPE (expr)))
7181 return build_nop (type, expr);
7183 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7184 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7185 if (TREE_CODE (type) != REFERENCE_TYPE
7186 && TREE_CODE (value) == NOP_EXPR
7187 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7188 value = TREE_OPERAND (value, 0);
7190 if (TREE_CODE (type) == ARRAY_TYPE)
7192 /* Allow casting from T1* to T2[] because Cfront allows it.
7193 NIHCL uses it. It is not valid ISO C++ however. */
7194 if (TYPE_PTR_P (TREE_TYPE (expr)))
7196 if (complain & tf_error)
7197 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7198 else
7199 return error_mark_node;
7200 type = build_pointer_type (TREE_TYPE (type));
7202 else
7204 if (complain & tf_error)
7205 error ("ISO C++ forbids casting to an array type %qT", type);
7206 return error_mark_node;
7210 if (TREE_CODE (type) == FUNCTION_TYPE
7211 || TREE_CODE (type) == METHOD_TYPE)
7213 if (complain & tf_error)
7214 error ("invalid cast to function type %qT", type);
7215 return error_mark_node;
7218 if (TYPE_PTR_P (type)
7219 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7220 /* Casting to an integer of smaller size is an error detected elsewhere. */
7221 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7222 /* Don't warn about converting any constant. */
7223 && !TREE_CONSTANT (value))
7224 warning_at (input_location, OPT_Wint_to_pointer_cast,
7225 "cast to pointer from integer of different size");
7227 /* A C-style cast can be a const_cast. */
7228 result = build_const_cast_1 (type, value, complain & tf_warning,
7229 &valid_p);
7230 if (valid_p)
7232 if (result != error_mark_node)
7233 maybe_warn_about_useless_cast (type, value, complain);
7234 return result;
7237 /* Or a static cast. */
7238 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7239 &valid_p, complain);
7240 /* Or a reinterpret_cast. */
7241 if (!valid_p)
7242 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7243 &valid_p, complain);
7244 /* The static_cast or reinterpret_cast may be followed by a
7245 const_cast. */
7246 if (valid_p
7247 /* A valid cast may result in errors if, for example, a
7248 conversion to an ambiguous base class is required. */
7249 && !error_operand_p (result))
7251 tree result_type;
7253 maybe_warn_about_useless_cast (type, value, complain);
7255 /* Non-class rvalues always have cv-unqualified type. */
7256 if (!CLASS_TYPE_P (type))
7257 type = TYPE_MAIN_VARIANT (type);
7258 result_type = TREE_TYPE (result);
7259 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7260 result_type = TYPE_MAIN_VARIANT (result_type);
7261 /* If the type of RESULT does not match TYPE, perform a
7262 const_cast to make it match. If the static_cast or
7263 reinterpret_cast succeeded, we will differ by at most
7264 cv-qualification, so the follow-on const_cast is guaranteed
7265 to succeed. */
7266 if (!same_type_p (non_reference (type), non_reference (result_type)))
7268 result = build_const_cast_1 (type, result, false, &valid_p);
7269 gcc_assert (valid_p);
7271 return result;
7274 return error_mark_node;
7277 /* For use from the C common bits. */
7278 tree
7279 build_modify_expr (location_t /*location*/,
7280 tree lhs, tree /*lhs_origtype*/,
7281 enum tree_code modifycode,
7282 location_t /*rhs_location*/, tree rhs,
7283 tree /*rhs_origtype*/)
7285 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7288 /* Build an assignment expression of lvalue LHS from value RHS.
7289 MODIFYCODE is the code for a binary operator that we use
7290 to combine the old value of LHS with RHS to get the new value.
7291 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7293 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7295 tree
7296 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7297 tsubst_flags_t complain)
7299 tree result;
7300 tree newrhs = rhs;
7301 tree lhstype = TREE_TYPE (lhs);
7302 tree olhstype = lhstype;
7303 bool plain_assign = (modifycode == NOP_EXPR);
7305 /* Avoid duplicate error messages from operands that had errors. */
7306 if (error_operand_p (lhs) || error_operand_p (rhs))
7307 return error_mark_node;
7309 /* Handle control structure constructs used as "lvalues". */
7310 switch (TREE_CODE (lhs))
7312 /* Handle --foo = 5; as these are valid constructs in C++. */
7313 case PREDECREMENT_EXPR:
7314 case PREINCREMENT_EXPR:
7315 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7316 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7317 stabilize_reference (TREE_OPERAND (lhs, 0)),
7318 TREE_OPERAND (lhs, 1));
7319 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7320 modifycode, rhs, complain);
7321 if (newrhs == error_mark_node)
7322 return error_mark_node;
7323 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7325 /* Handle (a, b) used as an "lvalue". */
7326 case COMPOUND_EXPR:
7327 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7328 modifycode, rhs, complain);
7329 if (newrhs == error_mark_node)
7330 return error_mark_node;
7331 return build2 (COMPOUND_EXPR, lhstype,
7332 TREE_OPERAND (lhs, 0), newrhs);
7334 case MODIFY_EXPR:
7335 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7336 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7337 stabilize_reference (TREE_OPERAND (lhs, 0)),
7338 TREE_OPERAND (lhs, 1));
7339 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7340 complain);
7341 if (newrhs == error_mark_node)
7342 return error_mark_node;
7343 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7345 case MIN_EXPR:
7346 case MAX_EXPR:
7347 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7348 when neither operand has side-effects. */
7349 if (!lvalue_or_else (lhs, lv_assign, complain))
7350 return error_mark_node;
7352 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7353 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7355 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7356 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7357 boolean_type_node,
7358 TREE_OPERAND (lhs, 0),
7359 TREE_OPERAND (lhs, 1)),
7360 TREE_OPERAND (lhs, 0),
7361 TREE_OPERAND (lhs, 1));
7362 /* Fall through. */
7364 /* Handle (a ? b : c) used as an "lvalue". */
7365 case COND_EXPR:
7367 /* Produce (a ? (b = rhs) : (c = rhs))
7368 except that the RHS goes through a save-expr
7369 so the code to compute it is only emitted once. */
7370 tree cond;
7371 tree preeval = NULL_TREE;
7373 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7375 if (complain & tf_error)
7376 error ("void value not ignored as it ought to be");
7377 return error_mark_node;
7380 rhs = stabilize_expr (rhs, &preeval);
7382 /* Check this here to avoid odd errors when trying to convert
7383 a throw to the type of the COND_EXPR. */
7384 if (!lvalue_or_else (lhs, lv_assign, complain))
7385 return error_mark_node;
7387 cond = build_conditional_expr
7388 (input_location, TREE_OPERAND (lhs, 0),
7389 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7390 modifycode, rhs, complain),
7391 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7392 modifycode, rhs, complain),
7393 complain);
7395 if (cond == error_mark_node)
7396 return cond;
7397 /* Make sure the code to compute the rhs comes out
7398 before the split. */
7399 if (preeval)
7400 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7401 return cond;
7404 default:
7405 break;
7408 if (modifycode == INIT_EXPR)
7410 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7411 /* Do the default thing. */;
7412 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7414 /* Compound literal. */
7415 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7416 /* Call convert to generate an error; see PR 11063. */
7417 rhs = convert (lhstype, rhs);
7418 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7419 TREE_SIDE_EFFECTS (result) = 1;
7420 return result;
7422 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7423 /* Do the default thing. */;
7424 else
7426 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7427 result = build_special_member_call (lhs, complete_ctor_identifier,
7428 &rhs_vec, lhstype, LOOKUP_NORMAL,
7429 complain);
7430 release_tree_vector (rhs_vec);
7431 if (result == NULL_TREE)
7432 return error_mark_node;
7433 return result;
7436 else
7438 lhs = require_complete_type_sfinae (lhs, complain);
7439 if (lhs == error_mark_node)
7440 return error_mark_node;
7442 if (modifycode == NOP_EXPR)
7444 if (c_dialect_objc ())
7446 result = objc_maybe_build_modify_expr (lhs, rhs);
7447 if (result)
7448 return result;
7451 /* `operator=' is not an inheritable operator. */
7452 if (! MAYBE_CLASS_TYPE_P (lhstype))
7453 /* Do the default thing. */;
7454 else
7456 result = build_new_op (input_location, MODIFY_EXPR,
7457 LOOKUP_NORMAL, lhs, rhs,
7458 make_node (NOP_EXPR), /*overload=*/NULL,
7459 complain);
7460 if (result == NULL_TREE)
7461 return error_mark_node;
7462 return result;
7464 lhstype = olhstype;
7466 else
7468 tree init = NULL_TREE;
7470 /* A binary op has been requested. Combine the old LHS
7471 value with the RHS producing the value we should actually
7472 store into the LHS. */
7473 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7474 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7475 || MAYBE_CLASS_TYPE_P (lhstype)));
7477 /* Preevaluate the RHS to make sure its evaluation is complete
7478 before the lvalue-to-rvalue conversion of the LHS:
7480 [expr.ass] With respect to an indeterminately-sequenced
7481 function call, the operation of a compound assignment is a
7482 single evaluation. [ Note: Therefore, a function call shall
7483 not intervene between the lvalue-to-rvalue conversion and the
7484 side effect associated with any single compound assignment
7485 operator. -- end note ] */
7486 lhs = stabilize_reference (lhs);
7487 rhs = rvalue (rhs);
7488 rhs = stabilize_expr (rhs, &init);
7489 newrhs = cp_build_binary_op (input_location,
7490 modifycode, lhs, rhs,
7491 complain);
7492 if (newrhs == error_mark_node)
7494 if (complain & tf_error)
7495 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7496 TREE_TYPE (lhs), TREE_TYPE (rhs));
7497 return error_mark_node;
7500 if (init)
7501 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7503 /* Now it looks like a plain assignment. */
7504 modifycode = NOP_EXPR;
7505 if (c_dialect_objc ())
7507 result = objc_maybe_build_modify_expr (lhs, newrhs);
7508 if (result)
7509 return result;
7512 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7513 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7516 /* The left-hand side must be an lvalue. */
7517 if (!lvalue_or_else (lhs, lv_assign, complain))
7518 return error_mark_node;
7520 /* Warn about modifying something that is `const'. Don't warn if
7521 this is initialization. */
7522 if (modifycode != INIT_EXPR
7523 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7524 /* Functions are not modifiable, even though they are
7525 lvalues. */
7526 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7527 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7528 /* If it's an aggregate and any field is const, then it is
7529 effectively const. */
7530 || (CLASS_TYPE_P (lhstype)
7531 && C_TYPE_FIELDS_READONLY (lhstype))))
7533 if (complain & tf_error)
7534 cxx_readonly_error (lhs, lv_assign);
7535 else
7536 return error_mark_node;
7539 /* If storing into a structure or union member, it may have been given a
7540 lowered bitfield type. We need to convert to the declared type first,
7541 so retrieve it now. */
7543 olhstype = unlowered_expr_type (lhs);
7545 /* Convert new value to destination type. */
7547 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7549 int from_array;
7551 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7553 if (modifycode != INIT_EXPR)
7555 if (complain & tf_error)
7556 error ("assigning to an array from an initializer list");
7557 return error_mark_node;
7559 if (check_array_initializer (lhs, lhstype, newrhs))
7560 return error_mark_node;
7561 newrhs = digest_init (lhstype, newrhs, complain);
7562 if (newrhs == error_mark_node)
7563 return error_mark_node;
7566 /* C++11 8.5/17: "If the destination type is an array of characters,
7567 an array of char16_t, an array of char32_t, or an array of wchar_t,
7568 and the initializer is a string literal...". */
7569 else if (TREE_CODE (newrhs) == STRING_CST
7570 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7571 && modifycode == INIT_EXPR)
7573 newrhs = digest_init (lhstype, newrhs, complain);
7574 if (newrhs == error_mark_node)
7575 return error_mark_node;
7578 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7579 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7581 if (complain & tf_error)
7582 error ("incompatible types in assignment of %qT to %qT",
7583 TREE_TYPE (rhs), lhstype);
7584 return error_mark_node;
7587 /* Allow array assignment in compiler-generated code. */
7588 else if (!current_function_decl
7589 || !DECL_DEFAULTED_FN (current_function_decl))
7591 /* This routine is used for both initialization and assignment.
7592 Make sure the diagnostic message differentiates the context. */
7593 if (complain & tf_error)
7595 if (modifycode == INIT_EXPR)
7596 error ("array used as initializer");
7597 else
7598 error ("invalid array assignment");
7600 return error_mark_node;
7603 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7604 ? 1 + (modifycode != INIT_EXPR): 0;
7605 return build_vec_init (lhs, NULL_TREE, newrhs,
7606 /*explicit_value_init_p=*/false,
7607 from_array, complain);
7610 if (modifycode == INIT_EXPR)
7611 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7612 LOOKUP_ONLYCONVERTING. */
7613 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7614 ICR_INIT, NULL_TREE, 0,
7615 complain);
7616 else
7617 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7618 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7620 if (!same_type_p (lhstype, olhstype))
7621 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7623 if (modifycode != INIT_EXPR)
7625 if (TREE_CODE (newrhs) == CALL_EXPR
7626 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7627 newrhs = build_cplus_new (lhstype, newrhs, complain);
7629 /* Can't initialize directly from a TARGET_EXPR, since that would
7630 cause the lhs to be constructed twice, and possibly result in
7631 accidental self-initialization. So we force the TARGET_EXPR to be
7632 expanded without a target. */
7633 if (TREE_CODE (newrhs) == TARGET_EXPR)
7634 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7635 TREE_OPERAND (newrhs, 0));
7638 if (newrhs == error_mark_node)
7639 return error_mark_node;
7641 if (c_dialect_objc () && flag_objc_gc)
7643 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7645 if (result)
7646 return result;
7649 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7650 lhstype, lhs, newrhs);
7652 TREE_SIDE_EFFECTS (result) = 1;
7653 if (!plain_assign)
7654 TREE_NO_WARNING (result) = 1;
7656 return result;
7659 tree
7660 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7661 tree rhs, tsubst_flags_t complain)
7663 if (processing_template_decl)
7664 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7665 build_min_nt_loc (loc, modifycode, NULL_TREE,
7666 NULL_TREE), rhs);
7668 if (modifycode != NOP_EXPR)
7670 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7671 make_node (modifycode), /*overload=*/NULL,
7672 complain);
7673 if (rval)
7675 TREE_NO_WARNING (rval) = 1;
7676 return rval;
7679 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7682 /* Helper function for get_delta_difference which assumes FROM is a base
7683 class of TO. Returns a delta for the conversion of pointer-to-member
7684 of FROM to pointer-to-member of TO. If the conversion is invalid and
7685 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7686 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7687 If C_CAST_P is true, this conversion is taking place as part of a
7688 C-style cast. */
7690 static tree
7691 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7692 tsubst_flags_t complain)
7694 tree binfo;
7695 base_kind kind;
7697 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7698 &kind, complain);
7700 if (binfo == error_mark_node)
7702 if (!(complain & tf_error))
7703 return error_mark_node;
7705 error (" in pointer to member function conversion");
7706 return size_zero_node;
7708 else if (binfo)
7710 if (kind != bk_via_virtual)
7711 return BINFO_OFFSET (binfo);
7712 else
7713 /* FROM is a virtual base class of TO. Issue an error or warning
7714 depending on whether or not this is a reinterpret cast. */
7716 if (!(complain & tf_error))
7717 return error_mark_node;
7719 error ("pointer to member conversion via virtual base %qT",
7720 BINFO_TYPE (binfo_from_vbase (binfo)));
7722 return size_zero_node;
7725 else
7726 return NULL_TREE;
7729 /* Get difference in deltas for different pointer to member function
7730 types. If the conversion is invalid and tf_error is not set in
7731 COMPLAIN, returns error_mark_node, otherwise returns an integer
7732 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7733 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7734 conversions as well. If C_CAST_P is true this conversion is taking
7735 place as part of a C-style cast.
7737 Note that the naming of FROM and TO is kind of backwards; the return
7738 value is what we add to a TO in order to get a FROM. They are named
7739 this way because we call this function to find out how to convert from
7740 a pointer to member of FROM to a pointer to member of TO. */
7742 static tree
7743 get_delta_difference (tree from, tree to,
7744 bool allow_inverse_p,
7745 bool c_cast_p, tsubst_flags_t complain)
7747 tree result;
7749 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7750 /* Pointer to member of incomplete class is permitted*/
7751 result = size_zero_node;
7752 else
7753 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7755 if (result == error_mark_node)
7756 return error_mark_node;
7758 if (!result)
7760 if (!allow_inverse_p)
7762 if (!(complain & tf_error))
7763 return error_mark_node;
7765 error_not_base_type (from, to);
7766 error (" in pointer to member conversion");
7767 result = size_zero_node;
7769 else
7771 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7773 if (result == error_mark_node)
7774 return error_mark_node;
7776 if (result)
7777 result = size_diffop_loc (input_location,
7778 size_zero_node, result);
7779 else
7781 if (!(complain & tf_error))
7782 return error_mark_node;
7784 error_not_base_type (from, to);
7785 error (" in pointer to member conversion");
7786 result = size_zero_node;
7791 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7792 result));
7795 /* Return a constructor for the pointer-to-member-function TYPE using
7796 the other components as specified. */
7798 tree
7799 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7801 tree u = NULL_TREE;
7802 tree delta_field;
7803 tree pfn_field;
7804 vec<constructor_elt, va_gc> *v;
7806 /* Pull the FIELD_DECLs out of the type. */
7807 pfn_field = TYPE_FIELDS (type);
7808 delta_field = DECL_CHAIN (pfn_field);
7810 /* Make sure DELTA has the type we want. */
7811 delta = convert_and_check (input_location, delta_type_node, delta);
7813 /* Convert to the correct target type if necessary. */
7814 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7816 /* Finish creating the initializer. */
7817 vec_alloc (v, 2);
7818 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7819 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7820 u = build_constructor (type, v);
7821 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7822 TREE_STATIC (u) = (TREE_CONSTANT (u)
7823 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7824 != NULL_TREE)
7825 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7826 != NULL_TREE));
7827 return u;
7830 /* Build a constructor for a pointer to member function. It can be
7831 used to initialize global variables, local variable, or used
7832 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7833 want to be.
7835 If FORCE is nonzero, then force this conversion, even if
7836 we would rather not do it. Usually set when using an explicit
7837 cast. A C-style cast is being processed iff C_CAST_P is true.
7839 Return error_mark_node, if something goes wrong. */
7841 tree
7842 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7843 tsubst_flags_t complain)
7845 tree fn;
7846 tree pfn_type;
7847 tree to_type;
7849 if (error_operand_p (pfn))
7850 return error_mark_node;
7852 pfn_type = TREE_TYPE (pfn);
7853 to_type = build_ptrmemfunc_type (type);
7855 /* Handle multiple conversions of pointer to member functions. */
7856 if (TYPE_PTRMEMFUNC_P (pfn_type))
7858 tree delta = NULL_TREE;
7859 tree npfn = NULL_TREE;
7860 tree n;
7862 if (!force
7863 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7864 LOOKUP_NORMAL, complain))
7866 if (complain & tf_error)
7867 error ("invalid conversion to type %qT from type %qT",
7868 to_type, pfn_type);
7869 else
7870 return error_mark_node;
7873 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7874 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7875 force,
7876 c_cast_p, complain);
7877 if (n == error_mark_node)
7878 return error_mark_node;
7880 /* We don't have to do any conversion to convert a
7881 pointer-to-member to its own type. But, we don't want to
7882 just return a PTRMEM_CST if there's an explicit cast; that
7883 cast should make the expression an invalid template argument. */
7884 if (TREE_CODE (pfn) != PTRMEM_CST)
7886 if (same_type_p (to_type, pfn_type))
7887 return pfn;
7888 else if (integer_zerop (n))
7889 return build_reinterpret_cast (to_type, pfn,
7890 complain);
7893 if (TREE_SIDE_EFFECTS (pfn))
7894 pfn = save_expr (pfn);
7896 /* Obtain the function pointer and the current DELTA. */
7897 if (TREE_CODE (pfn) == PTRMEM_CST)
7898 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7899 else
7901 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7902 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7905 /* Just adjust the DELTA field. */
7906 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7907 (TREE_TYPE (delta), ptrdiff_type_node));
7908 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7909 n = cp_build_binary_op (input_location,
7910 LSHIFT_EXPR, n, integer_one_node,
7911 complain);
7912 delta = cp_build_binary_op (input_location,
7913 PLUS_EXPR, delta, n, complain);
7914 return build_ptrmemfunc1 (to_type, delta, npfn);
7917 /* Handle null pointer to member function conversions. */
7918 if (null_ptr_cst_p (pfn))
7920 pfn = cp_build_c_cast (type, pfn, complain);
7921 return build_ptrmemfunc1 (to_type,
7922 integer_zero_node,
7923 pfn);
7926 if (type_unknown_p (pfn))
7927 return instantiate_type (type, pfn, complain);
7929 fn = TREE_OPERAND (pfn, 0);
7930 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7931 /* In a template, we will have preserved the
7932 OFFSET_REF. */
7933 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7934 return make_ptrmem_cst (to_type, fn);
7937 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7938 given by CST.
7940 ??? There is no consistency as to the types returned for the above
7941 values. Some code acts as if it were a sizetype and some as if it were
7942 integer_type_node. */
7944 void
7945 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7947 tree type = TREE_TYPE (cst);
7948 tree fn = PTRMEM_CST_MEMBER (cst);
7949 tree ptr_class, fn_class;
7951 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7953 /* The class that the function belongs to. */
7954 fn_class = DECL_CONTEXT (fn);
7956 /* The class that we're creating a pointer to member of. */
7957 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7959 /* First, calculate the adjustment to the function's class. */
7960 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7961 /*c_cast_p=*/0, tf_warning_or_error);
7963 if (!DECL_VIRTUAL_P (fn))
7964 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7965 build_addr_func (fn, tf_warning_or_error));
7966 else
7968 /* If we're dealing with a virtual function, we have to adjust 'this'
7969 again, to point to the base which provides the vtable entry for
7970 fn; the call will do the opposite adjustment. */
7971 tree orig_class = DECL_CONTEXT (fn);
7972 tree binfo = binfo_or_else (orig_class, fn_class);
7973 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7974 *delta, BINFO_OFFSET (binfo));
7975 *delta = fold_if_not_in_template (*delta);
7977 /* We set PFN to the vtable offset at which the function can be
7978 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7979 case delta is shifted left, and then incremented). */
7980 *pfn = DECL_VINDEX (fn);
7981 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7982 TYPE_SIZE_UNIT (vtable_entry_type));
7983 *pfn = fold_if_not_in_template (*pfn);
7985 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7987 case ptrmemfunc_vbit_in_pfn:
7988 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7989 integer_one_node);
7990 *pfn = fold_if_not_in_template (*pfn);
7991 break;
7993 case ptrmemfunc_vbit_in_delta:
7994 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7995 *delta, integer_one_node);
7996 *delta = fold_if_not_in_template (*delta);
7997 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7998 *delta, integer_one_node);
7999 *delta = fold_if_not_in_template (*delta);
8000 break;
8002 default:
8003 gcc_unreachable ();
8006 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8007 *pfn = fold_if_not_in_template (*pfn);
8011 /* Return an expression for PFN from the pointer-to-member function
8012 given by T. */
8014 static tree
8015 pfn_from_ptrmemfunc (tree t)
8017 if (TREE_CODE (t) == PTRMEM_CST)
8019 tree delta;
8020 tree pfn;
8022 expand_ptrmemfunc_cst (t, &delta, &pfn);
8023 if (pfn)
8024 return pfn;
8027 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8030 /* Return an expression for DELTA from the pointer-to-member function
8031 given by T. */
8033 static tree
8034 delta_from_ptrmemfunc (tree t)
8036 if (TREE_CODE (t) == PTRMEM_CST)
8038 tree delta;
8039 tree pfn;
8041 expand_ptrmemfunc_cst (t, &delta, &pfn);
8042 if (delta)
8043 return delta;
8046 return build_ptrmemfunc_access_expr (t, delta_identifier);
8049 /* Convert value RHS to type TYPE as preparation for an assignment to
8050 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8051 implicit conversion is. If FNDECL is non-NULL, we are doing the
8052 conversion in order to pass the PARMNUMth argument of FNDECL.
8053 If FNDECL is NULL, we are doing the conversion in function pointer
8054 argument passing, conversion in initialization, etc. */
8056 static tree
8057 convert_for_assignment (tree type, tree rhs,
8058 impl_conv_rhs errtype, tree fndecl, int parmnum,
8059 tsubst_flags_t complain, int flags)
8061 tree rhstype;
8062 enum tree_code coder;
8064 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8065 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8066 rhs = TREE_OPERAND (rhs, 0);
8068 rhstype = TREE_TYPE (rhs);
8069 coder = TREE_CODE (rhstype);
8071 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8072 && vector_types_convertible_p (type, rhstype, true))
8074 rhs = mark_rvalue_use (rhs);
8075 return convert (type, rhs);
8078 if (rhs == error_mark_node || rhstype == error_mark_node)
8079 return error_mark_node;
8080 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8081 return error_mark_node;
8083 /* The RHS of an assignment cannot have void type. */
8084 if (coder == VOID_TYPE)
8086 if (complain & tf_error)
8087 error ("void value not ignored as it ought to be");
8088 return error_mark_node;
8091 if (c_dialect_objc ())
8093 int parmno;
8094 tree selector;
8095 tree rname = fndecl;
8097 switch (errtype)
8099 case ICR_ASSIGN:
8100 parmno = -1;
8101 break;
8102 case ICR_INIT:
8103 parmno = -2;
8104 break;
8105 default:
8106 selector = objc_message_selector ();
8107 parmno = parmnum;
8108 if (selector && parmno > 1)
8110 rname = selector;
8111 parmno -= 1;
8115 if (objc_compare_types (type, rhstype, parmno, rname))
8117 rhs = mark_rvalue_use (rhs);
8118 return convert (type, rhs);
8122 /* [expr.ass]
8124 The expression is implicitly converted (clause _conv_) to the
8125 cv-unqualified type of the left operand.
8127 We allow bad conversions here because by the time we get to this point
8128 we are committed to doing the conversion. If we end up doing a bad
8129 conversion, convert_like will complain. */
8130 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8132 /* When -Wno-pmf-conversions is use, we just silently allow
8133 conversions from pointers-to-members to plain pointers. If
8134 the conversion doesn't work, cp_convert will complain. */
8135 if (!warn_pmf2ptr
8136 && TYPE_PTR_P (type)
8137 && TYPE_PTRMEMFUNC_P (rhstype))
8138 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8139 else
8141 if (complain & tf_error)
8143 /* If the right-hand side has unknown type, then it is an
8144 overloaded function. Call instantiate_type to get error
8145 messages. */
8146 if (rhstype == unknown_type_node)
8147 instantiate_type (type, rhs, tf_warning_or_error);
8148 else if (fndecl)
8149 error ("cannot convert %qT to %qT for argument %qP to %qD",
8150 rhstype, type, parmnum, fndecl);
8151 else
8152 switch (errtype)
8154 case ICR_DEFAULT_ARGUMENT:
8155 error ("cannot convert %qT to %qT in default argument",
8156 rhstype, type);
8157 break;
8158 case ICR_ARGPASS:
8159 error ("cannot convert %qT to %qT in argument passing",
8160 rhstype, type);
8161 break;
8162 case ICR_CONVERTING:
8163 error ("cannot convert %qT to %qT",
8164 rhstype, type);
8165 break;
8166 case ICR_INIT:
8167 error ("cannot convert %qT to %qT in initialization",
8168 rhstype, type);
8169 break;
8170 case ICR_RETURN:
8171 error ("cannot convert %qT to %qT in return",
8172 rhstype, type);
8173 break;
8174 case ICR_ASSIGN:
8175 error ("cannot convert %qT to %qT in assignment",
8176 rhstype, type);
8177 break;
8178 default:
8179 gcc_unreachable();
8181 if (TYPE_PTR_P (rhstype)
8182 && TYPE_PTR_P (type)
8183 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8184 && CLASS_TYPE_P (TREE_TYPE (type))
8185 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8186 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8187 (TREE_TYPE (rhstype))),
8188 "class type %qT is incomplete", TREE_TYPE (rhstype));
8190 return error_mark_node;
8193 if (warn_suggest_attribute_format)
8195 const enum tree_code codel = TREE_CODE (type);
8196 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8197 && coder == codel
8198 && check_missing_format_attribute (type, rhstype)
8199 && (complain & tf_warning))
8200 switch (errtype)
8202 case ICR_ARGPASS:
8203 case ICR_DEFAULT_ARGUMENT:
8204 if (fndecl)
8205 warning (OPT_Wsuggest_attribute_format,
8206 "parameter %qP of %qD might be a candidate "
8207 "for a format attribute", parmnum, fndecl);
8208 else
8209 warning (OPT_Wsuggest_attribute_format,
8210 "parameter might be a candidate "
8211 "for a format attribute");
8212 break;
8213 case ICR_CONVERTING:
8214 warning (OPT_Wsuggest_attribute_format,
8215 "target of conversion might be a candidate "
8216 "for a format attribute");
8217 break;
8218 case ICR_INIT:
8219 warning (OPT_Wsuggest_attribute_format,
8220 "target of initialization might be a candidate "
8221 "for a format attribute");
8222 break;
8223 case ICR_RETURN:
8224 warning (OPT_Wsuggest_attribute_format,
8225 "return type might be a candidate "
8226 "for a format attribute");
8227 break;
8228 case ICR_ASSIGN:
8229 warning (OPT_Wsuggest_attribute_format,
8230 "left-hand side of assignment might be a candidate "
8231 "for a format attribute");
8232 break;
8233 default:
8234 gcc_unreachable();
8238 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8239 does not. */
8240 if (warn_parentheses
8241 && TREE_CODE (type) == BOOLEAN_TYPE
8242 && TREE_CODE (rhs) == MODIFY_EXPR
8243 && !TREE_NO_WARNING (rhs)
8244 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8245 && (complain & tf_warning))
8247 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8249 warning_at (loc, OPT_Wparentheses,
8250 "suggest parentheses around assignment used as truth value");
8251 TREE_NO_WARNING (rhs) = 1;
8254 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8255 complain, flags);
8258 /* Convert RHS to be of type TYPE.
8259 If EXP is nonzero, it is the target of the initialization.
8260 ERRTYPE indicates what kind of error the implicit conversion is.
8262 Two major differences between the behavior of
8263 `convert_for_assignment' and `convert_for_initialization'
8264 are that references are bashed in the former, while
8265 copied in the latter, and aggregates are assigned in
8266 the former (operator=) while initialized in the
8267 latter (X(X&)).
8269 If using constructor make sure no conversion operator exists, if one does
8270 exist, an ambiguity exists. */
8272 tree
8273 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8274 impl_conv_rhs errtype, tree fndecl, int parmnum,
8275 tsubst_flags_t complain)
8277 enum tree_code codel = TREE_CODE (type);
8278 tree rhstype;
8279 enum tree_code coder;
8281 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8282 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8283 if (TREE_CODE (rhs) == NOP_EXPR
8284 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8285 && codel != REFERENCE_TYPE)
8286 rhs = TREE_OPERAND (rhs, 0);
8288 if (type == error_mark_node
8289 || rhs == error_mark_node
8290 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8291 return error_mark_node;
8293 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8294 && TREE_CODE (type) != ARRAY_TYPE
8295 && (TREE_CODE (type) != REFERENCE_TYPE
8296 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8297 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8298 && !TYPE_REFFN_P (type))
8299 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8300 rhs = decay_conversion (rhs, complain);
8302 rhstype = TREE_TYPE (rhs);
8303 coder = TREE_CODE (rhstype);
8305 if (coder == ERROR_MARK)
8306 return error_mark_node;
8308 /* We accept references to incomplete types, so we can
8309 return here before checking if RHS is of complete type. */
8311 if (codel == REFERENCE_TYPE)
8313 /* This should eventually happen in convert_arguments. */
8314 int savew = 0, savee = 0;
8316 if (fndecl)
8317 savew = warningcount + werrorcount, savee = errorcount;
8318 rhs = initialize_reference (type, rhs, flags, complain);
8320 if (fndecl
8321 && (warningcount + werrorcount > savew || errorcount > savee))
8322 inform (input_location,
8323 "in passing argument %P of %q+D", parmnum, fndecl);
8325 return rhs;
8328 if (exp != 0)
8329 exp = require_complete_type_sfinae (exp, complain);
8330 if (exp == error_mark_node)
8331 return error_mark_node;
8333 rhstype = non_reference (rhstype);
8335 type = complete_type (type);
8337 if (DIRECT_INIT_EXPR_P (type, rhs))
8338 /* Don't try to do copy-initialization if we already have
8339 direct-initialization. */
8340 return rhs;
8342 if (MAYBE_CLASS_TYPE_P (type))
8343 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8345 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8346 complain, flags);
8349 /* If RETVAL is the address of, or a reference to, a local variable or
8350 temporary give an appropriate warning and return true. */
8352 static bool
8353 maybe_warn_about_returning_address_of_local (tree retval)
8355 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8356 tree whats_returned = retval;
8358 for (;;)
8360 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8361 whats_returned = TREE_OPERAND (whats_returned, 1);
8362 else if (CONVERT_EXPR_P (whats_returned)
8363 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8364 whats_returned = TREE_OPERAND (whats_returned, 0);
8365 else
8366 break;
8369 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8370 return false;
8371 whats_returned = TREE_OPERAND (whats_returned, 0);
8373 while (TREE_CODE (whats_returned) == COMPONENT_REF
8374 || TREE_CODE (whats_returned) == ARRAY_REF)
8375 whats_returned = TREE_OPERAND (whats_returned, 0);
8377 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8379 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8380 || TREE_CODE (whats_returned) == TARGET_EXPR)
8382 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8383 return true;
8385 if (VAR_P (whats_returned)
8386 && DECL_NAME (whats_returned)
8387 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8389 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8390 return true;
8394 if (DECL_P (whats_returned)
8395 && DECL_NAME (whats_returned)
8396 && DECL_FUNCTION_SCOPE_P (whats_returned)
8397 && !is_capture_proxy (whats_returned)
8398 && !(TREE_STATIC (whats_returned)
8399 || TREE_PUBLIC (whats_returned)))
8401 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8402 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8403 whats_returned);
8404 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8405 warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
8406 whats_returned);
8407 else
8408 warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
8409 "returned", whats_returned);
8410 return true;
8413 return false;
8416 /* Check that returning RETVAL from the current function is valid.
8417 Return an expression explicitly showing all conversions required to
8418 change RETVAL into the function return type, and to assign it to
8419 the DECL_RESULT for the function. Set *NO_WARNING to true if
8420 code reaches end of non-void function warning shouldn't be issued
8421 on this RETURN_EXPR. */
8423 tree
8424 check_return_expr (tree retval, bool *no_warning)
8426 tree result;
8427 /* The type actually returned by the function. */
8428 tree valtype;
8429 /* The type the function is declared to return, or void if
8430 the declared type is incomplete. */
8431 tree functype;
8432 int fn_returns_value_p;
8433 bool named_return_value_okay_p;
8435 *no_warning = false;
8437 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8439 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8440 "statement is not allowed");
8441 return NULL_TREE;
8444 /* A `volatile' function is one that isn't supposed to return, ever.
8445 (This is a G++ extension, used to get better code for functions
8446 that call the `volatile' function.) */
8447 if (TREE_THIS_VOLATILE (current_function_decl))
8448 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8450 /* Check for various simple errors. */
8451 if (DECL_DESTRUCTOR_P (current_function_decl))
8453 if (retval)
8454 error ("returning a value from a destructor");
8455 return NULL_TREE;
8457 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8459 if (in_function_try_handler)
8460 /* If a return statement appears in a handler of the
8461 function-try-block of a constructor, the program is ill-formed. */
8462 error ("cannot return from a handler of a function-try-block of a constructor");
8463 else if (retval)
8464 /* You can't return a value from a constructor. */
8465 error ("returning a value from a constructor");
8466 return NULL_TREE;
8469 if (processing_template_decl)
8471 current_function_returns_value = 1;
8472 if (check_for_bare_parameter_packs (retval))
8473 retval = error_mark_node;
8474 return retval;
8477 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8479 /* Deduce auto return type from a return statement. */
8480 if (current_function_auto_return_pattern)
8482 tree auto_node;
8483 tree type;
8485 if (!retval && !is_auto (current_function_auto_return_pattern))
8487 /* Give a helpful error message. */
8488 error ("return-statement with no value, in function returning %qT",
8489 current_function_auto_return_pattern);
8490 inform (input_location, "only plain %<auto%> return type can be "
8491 "deduced to %<void%>");
8492 type = error_mark_node;
8494 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8496 error ("returning initializer list");
8497 type = error_mark_node;
8499 else
8501 if (!retval)
8502 retval = void_node;
8503 auto_node = type_uses_auto (current_function_auto_return_pattern);
8504 type = do_auto_deduction (current_function_auto_return_pattern,
8505 retval, auto_node);
8508 if (type == error_mark_node)
8509 /* Leave it. */;
8510 else if (functype == current_function_auto_return_pattern)
8511 apply_deduced_return_type (current_function_decl, type);
8512 else
8513 /* A mismatch should have been diagnosed in do_auto_deduction. */
8514 gcc_assert (same_type_p (type, functype));
8515 functype = type;
8518 /* When no explicit return-value is given in a function with a named
8519 return value, the named return value is used. */
8520 result = DECL_RESULT (current_function_decl);
8521 valtype = TREE_TYPE (result);
8522 gcc_assert (valtype != NULL_TREE);
8523 fn_returns_value_p = !VOID_TYPE_P (valtype);
8524 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8525 retval = result;
8527 /* Check for a return statement with no return value in a function
8528 that's supposed to return a value. */
8529 if (!retval && fn_returns_value_p)
8531 if (functype != error_mark_node)
8532 permerror (input_location, "return-statement with no value, in "
8533 "function returning %qT", valtype);
8534 /* Remember that this function did return. */
8535 current_function_returns_value = 1;
8536 /* And signal caller that TREE_NO_WARNING should be set on the
8537 RETURN_EXPR to avoid control reaches end of non-void function
8538 warnings in tree-cfg.c. */
8539 *no_warning = true;
8541 /* Check for a return statement with a value in a function that
8542 isn't supposed to return a value. */
8543 else if (retval && !fn_returns_value_p)
8545 if (VOID_TYPE_P (TREE_TYPE (retval)))
8546 /* You can return a `void' value from a function of `void'
8547 type. In that case, we have to evaluate the expression for
8548 its side-effects. */
8549 finish_expr_stmt (retval);
8550 else
8551 permerror (input_location, "return-statement with a value, in function "
8552 "returning 'void'");
8553 current_function_returns_null = 1;
8555 /* There's really no value to return, after all. */
8556 return NULL_TREE;
8558 else if (!retval)
8559 /* Remember that this function can sometimes return without a
8560 value. */
8561 current_function_returns_null = 1;
8562 else
8563 /* Remember that this function did return a value. */
8564 current_function_returns_value = 1;
8566 /* Check for erroneous operands -- but after giving ourselves a
8567 chance to provide an error about returning a value from a void
8568 function. */
8569 if (error_operand_p (retval))
8571 current_function_return_value = error_mark_node;
8572 return error_mark_node;
8575 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8576 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8577 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8578 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8579 && ! flag_check_new
8580 && retval && null_ptr_cst_p (retval))
8581 warning (0, "%<operator new%> must not return NULL unless it is "
8582 "declared %<throw()%> (or -fcheck-new is in effect)");
8584 /* Effective C++ rule 15. See also start_function. */
8585 if (warn_ecpp
8586 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8588 bool warn = true;
8590 /* The function return type must be a reference to the current
8591 class. */
8592 if (TREE_CODE (valtype) == REFERENCE_TYPE
8593 && same_type_ignoring_top_level_qualifiers_p
8594 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8596 /* Returning '*this' is obviously OK. */
8597 if (retval == current_class_ref)
8598 warn = false;
8599 /* If we are calling a function whose return type is the same of
8600 the current class reference, it is ok. */
8601 else if (INDIRECT_REF_P (retval)
8602 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8603 warn = false;
8606 if (warn)
8607 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8610 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8612 [...] For a function with a class return type, if the expression
8613 in the return statement is the name of a local object, and the cv-
8614 unqualified type of the local object is the same as the function
8615 return type, an implementation is permitted to omit creating the tem-
8616 porary object to hold the function return value [...]
8618 So, if this is a value-returning function that always returns the same
8619 local variable, remember it.
8621 It might be nice to be more flexible, and choose the first suitable
8622 variable even if the function sometimes returns something else, but
8623 then we run the risk of clobbering the variable we chose if the other
8624 returned expression uses the chosen variable somehow. And people expect
8625 this restriction, anyway. (jason 2000-11-19)
8627 See finish_function and finalize_nrv for the rest of this optimization. */
8629 named_return_value_okay_p =
8630 (retval != NULL_TREE
8631 /* Must be a local, automatic variable. */
8632 && VAR_P (retval)
8633 && DECL_CONTEXT (retval) == current_function_decl
8634 && ! TREE_STATIC (retval)
8635 /* And not a lambda or anonymous union proxy. */
8636 && !DECL_HAS_VALUE_EXPR_P (retval)
8637 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8638 /* The cv-unqualified type of the returned value must be the
8639 same as the cv-unqualified return type of the
8640 function. */
8641 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8642 (TYPE_MAIN_VARIANT (functype)))
8643 /* And the returned value must be non-volatile. */
8644 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8646 if (fn_returns_value_p && flag_elide_constructors)
8648 if (named_return_value_okay_p
8649 && (current_function_return_value == NULL_TREE
8650 || current_function_return_value == retval))
8651 current_function_return_value = retval;
8652 else
8653 current_function_return_value = error_mark_node;
8656 /* We don't need to do any conversions when there's nothing being
8657 returned. */
8658 if (!retval)
8659 return NULL_TREE;
8661 /* Do any required conversions. */
8662 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8663 /* No conversions are required. */
8665 else
8667 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8669 /* The functype's return type will have been set to void, if it
8670 was an incomplete type. Just treat this as 'return;' */
8671 if (VOID_TYPE_P (functype))
8672 return error_mark_node;
8674 /* If we had an id-expression obfuscated by force_paren_expr, we need
8675 to undo it so we can try to treat it as an rvalue below. */
8676 if (cxx_dialect >= cxx14
8677 && INDIRECT_REF_P (retval)
8678 && REF_PARENTHESIZED_P (retval))
8680 retval = TREE_OPERAND (retval, 0);
8681 while (TREE_CODE (retval) == NON_LVALUE_EXPR
8682 || TREE_CODE (retval) == NOP_EXPR)
8683 retval = TREE_OPERAND (retval, 0);
8684 gcc_assert (TREE_CODE (retval) == ADDR_EXPR);
8685 retval = TREE_OPERAND (retval, 0);
8688 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8689 treated as an rvalue for the purposes of overload resolution to
8690 favor move constructors over copy constructors.
8692 Note that these conditions are similar to, but not as strict as,
8693 the conditions for the named return value optimization. */
8694 if ((cxx_dialect != cxx98)
8695 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8696 || TREE_CODE (retval) == PARM_DECL)
8697 && DECL_CONTEXT (retval) == current_function_decl
8698 && !TREE_STATIC (retval)
8699 /* This is only interesting for class type. */
8700 && CLASS_TYPE_P (functype))
8701 flags = flags | LOOKUP_PREFER_RVALUE;
8703 /* First convert the value to the function's return type, then
8704 to the type of return value's location to handle the
8705 case that functype is smaller than the valtype. */
8706 retval = convert_for_initialization
8707 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8708 tf_warning_or_error);
8709 retval = convert (valtype, retval);
8711 /* If the conversion failed, treat this just like `return;'. */
8712 if (retval == error_mark_node)
8713 return retval;
8714 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8715 else if (! cfun->returns_struct
8716 && TREE_CODE (retval) == TARGET_EXPR
8717 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8718 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8719 TREE_OPERAND (retval, 0));
8720 else if (maybe_warn_about_returning_address_of_local (retval))
8721 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8722 build_zero_cst (TREE_TYPE (retval)));
8725 /* Actually copy the value returned into the appropriate location. */
8726 if (retval && retval != result)
8727 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8729 return retval;
8733 /* Returns nonzero if the pointer-type FROM can be converted to the
8734 pointer-type TO via a qualification conversion. If CONSTP is -1,
8735 then we return nonzero if the pointers are similar, and the
8736 cv-qualification signature of FROM is a proper subset of that of TO.
8738 If CONSTP is positive, then all outer pointers have been
8739 const-qualified. */
8741 static int
8742 comp_ptr_ttypes_real (tree to, tree from, int constp)
8744 bool to_more_cv_qualified = false;
8745 bool is_opaque_pointer = false;
8747 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8749 if (TREE_CODE (to) != TREE_CODE (from))
8750 return 0;
8752 if (TREE_CODE (from) == OFFSET_TYPE
8753 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8754 TYPE_OFFSET_BASETYPE (to)))
8755 return 0;
8757 /* Const and volatile mean something different for function types,
8758 so the usual checks are not appropriate. */
8759 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8761 if (!at_least_as_qualified_p (to, from))
8762 return 0;
8764 if (!at_least_as_qualified_p (from, to))
8766 if (constp == 0)
8767 return 0;
8768 to_more_cv_qualified = true;
8771 if (constp > 0)
8772 constp &= TYPE_READONLY (to);
8775 if (TREE_CODE (to) == VECTOR_TYPE)
8776 is_opaque_pointer = vector_targets_convertible_p (to, from);
8778 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8779 return ((constp >= 0 || to_more_cv_qualified)
8780 && (is_opaque_pointer
8781 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8785 /* When comparing, say, char ** to char const **, this function takes
8786 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8787 types to this function. */
8790 comp_ptr_ttypes (tree to, tree from)
8792 return comp_ptr_ttypes_real (to, from, 1);
8795 /* Returns true iff FNTYPE is a non-class type that involves
8796 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8797 if a parameter type is ill-formed. */
8799 bool
8800 error_type_p (const_tree type)
8802 tree t;
8804 switch (TREE_CODE (type))
8806 case ERROR_MARK:
8807 return true;
8809 case POINTER_TYPE:
8810 case REFERENCE_TYPE:
8811 case OFFSET_TYPE:
8812 return error_type_p (TREE_TYPE (type));
8814 case FUNCTION_TYPE:
8815 case METHOD_TYPE:
8816 if (error_type_p (TREE_TYPE (type)))
8817 return true;
8818 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8819 if (error_type_p (TREE_VALUE (t)))
8820 return true;
8821 return false;
8823 case RECORD_TYPE:
8824 if (TYPE_PTRMEMFUNC_P (type))
8825 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8826 return false;
8828 default:
8829 return false;
8833 /* Returns true if to and from are (possibly multi-level) pointers to the same
8834 type or inheritance-related types, regardless of cv-quals. */
8836 bool
8837 ptr_reasonably_similar (const_tree to, const_tree from)
8839 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8841 /* Any target type is similar enough to void. */
8842 if (VOID_TYPE_P (to))
8843 return !error_type_p (from);
8844 if (VOID_TYPE_P (from))
8845 return !error_type_p (to);
8847 if (TREE_CODE (to) != TREE_CODE (from))
8848 return false;
8850 if (TREE_CODE (from) == OFFSET_TYPE
8851 && comptypes (TYPE_OFFSET_BASETYPE (to),
8852 TYPE_OFFSET_BASETYPE (from),
8853 COMPARE_BASE | COMPARE_DERIVED))
8854 continue;
8856 if (TREE_CODE (to) == VECTOR_TYPE
8857 && vector_types_convertible_p (to, from, false))
8858 return true;
8860 if (TREE_CODE (to) == INTEGER_TYPE
8861 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8862 return true;
8864 if (TREE_CODE (to) == FUNCTION_TYPE)
8865 return !error_type_p (to) && !error_type_p (from);
8867 if (!TYPE_PTR_P (to))
8869 /* When either type is incomplete avoid DERIVED_FROM_P,
8870 which may call complete_type (c++/57942). */
8871 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8872 return comptypes
8873 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8874 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8879 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8880 pointer-to-member types) are the same, ignoring cv-qualification at
8881 all levels. */
8883 bool
8884 comp_ptr_ttypes_const (tree to, tree from)
8886 bool is_opaque_pointer = false;
8888 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8890 if (TREE_CODE (to) != TREE_CODE (from))
8891 return false;
8893 if (TREE_CODE (from) == OFFSET_TYPE
8894 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8895 TYPE_OFFSET_BASETYPE (to)))
8896 continue;
8898 if (TREE_CODE (to) == VECTOR_TYPE)
8899 is_opaque_pointer = vector_targets_convertible_p (to, from);
8901 if (!TYPE_PTR_P (to))
8902 return (is_opaque_pointer
8903 || same_type_ignoring_top_level_qualifiers_p (to, from));
8907 /* Returns the type qualifiers for this type, including the qualifiers on the
8908 elements for an array type. */
8911 cp_type_quals (const_tree type)
8913 int quals;
8914 /* This CONST_CAST is okay because strip_array_types returns its
8915 argument unmodified and we assign it to a const_tree. */
8916 type = strip_array_types (CONST_CAST_TREE (type));
8917 if (type == error_mark_node
8918 /* Quals on a FUNCTION_TYPE are memfn quals. */
8919 || TREE_CODE (type) == FUNCTION_TYPE)
8920 return TYPE_UNQUALIFIED;
8921 quals = TYPE_QUALS (type);
8922 /* METHOD and REFERENCE_TYPEs should never have quals. */
8923 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8924 && TREE_CODE (type) != REFERENCE_TYPE)
8925 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8926 == TYPE_UNQUALIFIED));
8927 return quals;
8930 /* Returns the function-ref-qualifier for TYPE */
8932 cp_ref_qualifier
8933 type_memfn_rqual (const_tree type)
8935 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8936 || TREE_CODE (type) == METHOD_TYPE);
8938 if (!FUNCTION_REF_QUALIFIED (type))
8939 return REF_QUAL_NONE;
8940 else if (FUNCTION_RVALUE_QUALIFIED (type))
8941 return REF_QUAL_RVALUE;
8942 else
8943 return REF_QUAL_LVALUE;
8946 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8947 METHOD_TYPE. */
8950 type_memfn_quals (const_tree type)
8952 if (TREE_CODE (type) == FUNCTION_TYPE)
8953 return TYPE_QUALS (type);
8954 else if (TREE_CODE (type) == METHOD_TYPE)
8955 return cp_type_quals (class_of_this_parm (type));
8956 else
8957 gcc_unreachable ();
8960 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8961 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8963 tree
8964 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8966 /* Could handle METHOD_TYPE here if necessary. */
8967 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8968 if (TYPE_QUALS (type) == memfn_quals
8969 && type_memfn_rqual (type) == rqual)
8970 return type;
8972 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8973 complex. */
8974 tree result = build_qualified_type (type, memfn_quals);
8975 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8976 return build_ref_qualified_type (result, rqual);
8979 /* Returns nonzero if TYPE is const or volatile. */
8981 bool
8982 cv_qualified_p (const_tree type)
8984 int quals = cp_type_quals (type);
8985 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8988 /* Returns nonzero if the TYPE contains a mutable member. */
8990 bool
8991 cp_has_mutable_p (const_tree type)
8993 /* This CONST_CAST is okay because strip_array_types returns its
8994 argument unmodified and we assign it to a const_tree. */
8995 type = strip_array_types (CONST_CAST_TREE(type));
8997 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9000 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9001 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9002 approximation. In particular, consider:
9004 int f();
9005 struct S { int i; };
9006 const S s = { f(); }
9008 Here, we will make "s" as TREE_READONLY (because it is declared
9009 "const") -- only to reverse ourselves upon seeing that the
9010 initializer is non-constant. */
9012 void
9013 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9015 tree type = TREE_TYPE (decl);
9017 if (type == error_mark_node)
9018 return;
9020 if (TREE_CODE (decl) == TYPE_DECL)
9021 return;
9023 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9024 && type_quals != TYPE_UNQUALIFIED));
9026 /* Avoid setting TREE_READONLY incorrectly. */
9027 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9028 constructor can produce constant init, so rely on cp_finish_decl to
9029 clear TREE_READONLY if the variable has non-constant init. */
9031 /* If the type has (or might have) a mutable component, that component
9032 might be modified. */
9033 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9034 type_quals &= ~TYPE_QUAL_CONST;
9036 c_apply_type_quals_to_decl (type_quals, decl);
9039 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9040 exemplar types such that casting T1 to T2 is casting away constness
9041 if and only if there is no implicit conversion from T1 to T2. */
9043 static void
9044 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9046 int quals1;
9047 int quals2;
9049 /* [expr.const.cast]
9051 For multi-level pointer to members and multi-level mixed pointers
9052 and pointers to members (conv.qual), the "member" aspect of a
9053 pointer to member level is ignored when determining if a const
9054 cv-qualifier has been cast away. */
9055 /* [expr.const.cast]
9057 For two pointer types:
9059 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9060 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9061 K is min(N,M)
9063 casting from X1 to X2 casts away constness if, for a non-pointer
9064 type T there does not exist an implicit conversion (clause
9065 _conv_) from:
9067 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9071 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9072 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9073 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9075 *t1 = cp_build_qualified_type (void_type_node,
9076 cp_type_quals (*t1));
9077 *t2 = cp_build_qualified_type (void_type_node,
9078 cp_type_quals (*t2));
9079 return;
9082 quals1 = cp_type_quals (*t1);
9083 quals2 = cp_type_quals (*t2);
9085 if (TYPE_PTRDATAMEM_P (*t1))
9086 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9087 else
9088 *t1 = TREE_TYPE (*t1);
9089 if (TYPE_PTRDATAMEM_P (*t2))
9090 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9091 else
9092 *t2 = TREE_TYPE (*t2);
9094 casts_away_constness_r (t1, t2, complain);
9095 *t1 = build_pointer_type (*t1);
9096 *t2 = build_pointer_type (*t2);
9097 *t1 = cp_build_qualified_type (*t1, quals1);
9098 *t2 = cp_build_qualified_type (*t2, quals2);
9101 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9102 constness.
9104 ??? This function returns non-zero if casting away qualifiers not
9105 just const. We would like to return to the caller exactly which
9106 qualifiers are casted away to give more accurate diagnostics.
9109 static bool
9110 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9112 if (TREE_CODE (t2) == REFERENCE_TYPE)
9114 /* [expr.const.cast]
9116 Casting from an lvalue of type T1 to an lvalue of type T2
9117 using a reference cast casts away constness if a cast from an
9118 rvalue of type "pointer to T1" to the type "pointer to T2"
9119 casts away constness. */
9120 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9121 return casts_away_constness (build_pointer_type (t1),
9122 build_pointer_type (TREE_TYPE (t2)),
9123 complain);
9126 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9127 /* [expr.const.cast]
9129 Casting from an rvalue of type "pointer to data member of X
9130 of type T1" to the type "pointer to data member of Y of type
9131 T2" casts away constness if a cast from an rvalue of type
9132 "pointer to T1" to the type "pointer to T2" casts away
9133 constness. */
9134 return casts_away_constness
9135 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9136 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9137 complain);
9139 /* Casting away constness is only something that makes sense for
9140 pointer or reference types. */
9141 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9142 return false;
9144 /* Top-level qualifiers don't matter. */
9145 t1 = TYPE_MAIN_VARIANT (t1);
9146 t2 = TYPE_MAIN_VARIANT (t2);
9147 casts_away_constness_r (&t1, &t2, complain);
9148 if (!can_convert (t2, t1, complain))
9149 return true;
9151 return false;
9154 /* If T is a REFERENCE_TYPE return the type to which T refers.
9155 Otherwise, return T itself. */
9157 tree
9158 non_reference (tree t)
9160 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9161 t = TREE_TYPE (t);
9162 return t;
9166 /* Return nonzero if REF is an lvalue valid for this language;
9167 otherwise, print an error message and return zero. USE says
9168 how the lvalue is being used and so selects the error message. */
9171 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9173 cp_lvalue_kind kind = lvalue_kind (ref);
9175 if (kind == clk_none)
9177 if (complain & tf_error)
9178 lvalue_error (input_location, use);
9179 return 0;
9181 else if (kind & (clk_rvalueref|clk_class))
9183 if (!(complain & tf_error))
9184 return 0;
9185 if (kind & clk_class)
9186 /* Make this a permerror because we used to accept it. */
9187 permerror (input_location, "using temporary as lvalue");
9188 else
9189 error ("using xvalue (rvalue reference) as lvalue");
9191 return 1;
9194 /* Return true if a user-defined literal operator is a raw operator. */
9196 bool
9197 check_raw_literal_operator (const_tree decl)
9199 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9200 tree argtype;
9201 int arity;
9202 bool maybe_raw_p = false;
9204 /* Count the number and type of arguments and check for ellipsis. */
9205 for (argtype = argtypes, arity = 0;
9206 argtype && argtype != void_list_node;
9207 ++arity, argtype = TREE_CHAIN (argtype))
9209 tree t = TREE_VALUE (argtype);
9211 if (same_type_p (t, const_string_type_node))
9212 maybe_raw_p = true;
9214 if (!argtype)
9215 return false; /* Found ellipsis. */
9217 if (!maybe_raw_p || arity != 1)
9218 return false;
9220 return true;
9224 /* Return true if a user-defined literal operator has one of the allowed
9225 argument types. */
9227 bool
9228 check_literal_operator_args (const_tree decl,
9229 bool *long_long_unsigned_p, bool *long_double_p)
9231 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9233 *long_long_unsigned_p = false;
9234 *long_double_p = false;
9235 if (processing_template_decl || processing_specialization)
9236 return argtypes == void_list_node;
9237 else
9239 tree argtype;
9240 int arity;
9241 int max_arity = 2;
9243 /* Count the number and type of arguments and check for ellipsis. */
9244 for (argtype = argtypes, arity = 0;
9245 argtype && argtype != void_list_node;
9246 argtype = TREE_CHAIN (argtype))
9248 tree t = TREE_VALUE (argtype);
9249 ++arity;
9251 if (TYPE_PTR_P (t))
9253 bool maybe_raw_p = false;
9254 t = TREE_TYPE (t);
9255 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9256 return false;
9257 t = TYPE_MAIN_VARIANT (t);
9258 if ((maybe_raw_p = same_type_p (t, char_type_node))
9259 || same_type_p (t, wchar_type_node)
9260 || same_type_p (t, char16_type_node)
9261 || same_type_p (t, char32_type_node))
9263 argtype = TREE_CHAIN (argtype);
9264 if (!argtype)
9265 return false;
9266 t = TREE_VALUE (argtype);
9267 if (maybe_raw_p && argtype == void_list_node)
9268 return true;
9269 else if (same_type_p (t, size_type_node))
9271 ++arity;
9272 continue;
9274 else
9275 return false;
9278 else if (same_type_p (t, long_long_unsigned_type_node))
9280 max_arity = 1;
9281 *long_long_unsigned_p = true;
9283 else if (same_type_p (t, long_double_type_node))
9285 max_arity = 1;
9286 *long_double_p = true;
9288 else if (same_type_p (t, char_type_node))
9289 max_arity = 1;
9290 else if (same_type_p (t, wchar_type_node))
9291 max_arity = 1;
9292 else if (same_type_p (t, char16_type_node))
9293 max_arity = 1;
9294 else if (same_type_p (t, char32_type_node))
9295 max_arity = 1;
9296 else
9297 return false;
9299 if (!argtype)
9300 return false; /* Found ellipsis. */
9302 if (arity != max_arity)
9303 return false;
9305 return true;