2014-10-24 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cp / typeck.c
blob99f4b8830d2b7424b42a49ebc562903e68de1dab
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "diagnostic.h"
37 #include "intl.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "c-family/c-ubsan.h"
43 #include "params.h"
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
48 tsubst_flags_t, int);
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
50 static tree rationalize_conditional_expr (enum tree_code, tree,
51 tsubst_flags_t);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
58 static bool casts_away_constness (tree, tree, tsubst_flags_t);
59 static bool maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
63 tsubst_flags_t);
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
70 tree
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
73 tree type;
75 if (processing_template_decl || value == error_mark_node)
76 return value;
78 if (TREE_CODE (value) == OVERLOAD)
79 type = unknown_type_node;
80 else
81 type = TREE_TYPE (value);
83 if (type == error_mark_node)
84 return error_mark_node;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type))
88 return value;
90 if (complete_type_or_maybe_complain (type, value, complain))
91 return value;
92 else
93 return error_mark_node;
96 tree
97 require_complete_type (tree value)
99 return require_complete_type_sfinae (value, tf_warning_or_error);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
107 tree
108 complete_type (tree type)
110 if (type == NULL_TREE)
111 /* Rather than crash, we return something sure to cause an error
112 at some point. */
113 return error_mark_node;
115 if (type == error_mark_node || COMPLETE_TYPE_P (type))
117 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
119 tree t = complete_type (TREE_TYPE (type));
120 unsigned int needs_constructing, has_nontrivial_dtor;
121 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
122 layout_type (type);
123 needs_constructing
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
125 has_nontrivial_dtor
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
129 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
136 return type;
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
143 tree
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
146 type = complete_type (type);
147 if (type == error_mark_node)
148 /* We already issued an error. */
149 return NULL_TREE;
150 else if (!COMPLETE_TYPE_P (type))
152 if (complain & tf_error)
153 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
154 return NULL_TREE;
156 else
157 return type;
160 tree
161 complete_type_or_else (tree type, tree value)
163 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
166 /* Return truthvalue of whether type of EXP is instantiated. */
169 type_unknown_p (const_tree exp)
171 return (TREE_CODE (exp) == TREE_LIST
172 || TREE_TYPE (exp) == unknown_type_node);
176 /* Return the common type of two parameter lists.
177 We assume that comptypes has already been done and returned 1;
178 if that isn't so, this may crash.
180 As an optimization, free the space we allocate if the parameter
181 lists are already common. */
183 static tree
184 commonparms (tree p1, tree p2)
186 tree oldargs = p1, newargs, n;
187 int i, len;
188 int any_change = 0;
190 len = list_length (p1);
191 newargs = tree_last (p1);
193 if (newargs == void_list_node)
194 i = 1;
195 else
197 i = 0;
198 newargs = 0;
201 for (; i < len; i++)
202 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
204 n = newargs;
206 for (i = 0; p1;
207 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
209 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
212 any_change = 1;
214 else if (! TREE_PURPOSE (p1))
216 if (TREE_PURPOSE (p2))
218 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219 any_change = 1;
222 else
224 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
225 any_change = 1;
226 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
228 if (TREE_VALUE (p1) != TREE_VALUE (p2))
230 any_change = 1;
231 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
233 else
234 TREE_VALUE (n) = TREE_VALUE (p1);
236 if (! any_change)
237 return oldargs;
239 return newargs;
242 /* Given a type, perhaps copied for a typedef,
243 find the "original" version of it. */
244 static tree
245 original_type (tree t)
247 int quals = cp_type_quals (t);
248 while (t != error_mark_node
249 && TYPE_NAME (t) != NULL_TREE)
251 tree x = TYPE_NAME (t);
252 if (TREE_CODE (x) != TYPE_DECL)
253 break;
254 x = DECL_ORIGINAL_TYPE (x);
255 if (x == NULL_TREE)
256 break;
257 t = x;
259 return cp_build_qualified_type (t, quals);
262 /* Return the common type for two arithmetic types T1 and T2 under the
263 usual arithmetic conversions. The default conversions have already
264 been applied, and enumerated types converted to their compatible
265 integer types. */
267 static tree
268 cp_common_type (tree t1, tree t2)
270 enum tree_code code1 = TREE_CODE (t1);
271 enum tree_code code2 = TREE_CODE (t2);
272 tree attributes;
273 int i;
276 /* In what follows, we slightly generalize the rules given in [expr] so
277 as to deal with `long long' and `complex'. First, merge the
278 attributes. */
279 attributes = (*targetm.merge_type_attributes) (t1, t2);
281 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
283 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
284 return build_type_attribute_variant (t1, attributes);
285 else
286 return NULL_TREE;
289 /* FIXME: Attributes. */
290 gcc_assert (ARITHMETIC_TYPE_P (t1)
291 || TREE_CODE (t1) == VECTOR_TYPE
292 || UNSCOPED_ENUM_P (t1));
293 gcc_assert (ARITHMETIC_TYPE_P (t2)
294 || TREE_CODE (t2) == VECTOR_TYPE
295 || UNSCOPED_ENUM_P (t2));
297 /* If one type is complex, form the common type of the non-complex
298 components, then make that complex. Use T1 or T2 if it is the
299 required type. */
300 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
302 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
303 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
304 tree subtype
305 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
307 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
308 return build_type_attribute_variant (t1, attributes);
309 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
310 return build_type_attribute_variant (t2, attributes);
311 else
312 return build_type_attribute_variant (build_complex_type (subtype),
313 attributes);
316 if (code1 == VECTOR_TYPE)
318 /* When we get here we should have two vectors of the same size.
319 Just prefer the unsigned one if present. */
320 if (TYPE_UNSIGNED (t1))
321 return build_type_attribute_variant (t1, attributes);
322 else
323 return build_type_attribute_variant (t2, attributes);
326 /* If only one is real, use it as the result. */
327 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
328 return build_type_attribute_variant (t1, attributes);
329 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
330 return build_type_attribute_variant (t2, attributes);
332 /* Both real or both integers; use the one with greater precision. */
333 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
334 return build_type_attribute_variant (t1, attributes);
335 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
336 return build_type_attribute_variant (t2, attributes);
338 /* The types are the same; no need to do anything fancy. */
339 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
340 return build_type_attribute_variant (t1, attributes);
342 if (code1 != REAL_TYPE)
344 /* If one is unsigned long long, then convert the other to unsigned
345 long long. */
346 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
347 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
348 return build_type_attribute_variant (long_long_unsigned_type_node,
349 attributes);
350 /* If one is a long long, and the other is an unsigned long, and
351 long long can represent all the values of an unsigned long, then
352 convert to a long long. Otherwise, convert to an unsigned long
353 long. Otherwise, if either operand is long long, convert the
354 other to long long.
356 Since we're here, we know the TYPE_PRECISION is the same;
357 therefore converting to long long cannot represent all the values
358 of an unsigned long, so we choose unsigned long long in that
359 case. */
360 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
361 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
363 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
364 ? long_long_unsigned_type_node
365 : long_long_integer_type_node);
366 return build_type_attribute_variant (t, attributes);
369 /* Go through the same procedure, but for longs. */
370 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
371 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
372 return build_type_attribute_variant (long_unsigned_type_node,
373 attributes);
374 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
375 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
377 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
378 ? long_unsigned_type_node : long_integer_type_node);
379 return build_type_attribute_variant (t, attributes);
382 /* For __intN types, either the type is __int128 (and is lower
383 priority than the types checked above, but higher than other
384 128-bit types) or it's known to not be the same size as other
385 types (enforced in toplev.c). Prefer the unsigned type. */
386 for (i = 0; i < NUM_INT_N_ENTS; i ++)
388 if (int_n_enabled_p [i]
389 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
390 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
391 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
392 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
394 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
395 ? int_n_trees[i].unsigned_type
396 : int_n_trees[i].signed_type);
397 return build_type_attribute_variant (t, attributes);
401 /* Otherwise prefer the unsigned one. */
402 if (TYPE_UNSIGNED (t1))
403 return build_type_attribute_variant (t1, attributes);
404 else
405 return build_type_attribute_variant (t2, attributes);
407 else
409 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
410 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
411 return build_type_attribute_variant (long_double_type_node,
412 attributes);
413 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
414 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
415 return build_type_attribute_variant (double_type_node,
416 attributes);
417 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
418 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
419 return build_type_attribute_variant (float_type_node,
420 attributes);
422 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
423 the standard C++ floating-point types. Logic earlier in this
424 function has already eliminated the possibility that
425 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
426 compelling reason to choose one or the other. */
427 return build_type_attribute_variant (t1, attributes);
431 /* T1 and T2 are arithmetic or enumeration types. Return the type
432 that will result from the "usual arithmetic conversions" on T1 and
433 T2 as described in [expr]. */
435 tree
436 type_after_usual_arithmetic_conversions (tree t1, tree t2)
438 gcc_assert (ARITHMETIC_TYPE_P (t1)
439 || TREE_CODE (t1) == VECTOR_TYPE
440 || UNSCOPED_ENUM_P (t1));
441 gcc_assert (ARITHMETIC_TYPE_P (t2)
442 || TREE_CODE (t2) == VECTOR_TYPE
443 || UNSCOPED_ENUM_P (t2));
445 /* Perform the integral promotions. We do not promote real types here. */
446 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
447 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
449 t1 = type_promotes_to (t1);
450 t2 = type_promotes_to (t2);
453 return cp_common_type (t1, t2);
456 static void
457 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
458 composite_pointer_operation operation)
460 switch (operation)
462 case CPO_COMPARISON:
463 emit_diagnostic (kind, input_location, 0,
464 "comparison between "
465 "distinct pointer types %qT and %qT lacks a cast",
466 t1, t2);
467 break;
468 case CPO_CONVERSION:
469 emit_diagnostic (kind, input_location, 0,
470 "conversion between "
471 "distinct pointer types %qT and %qT lacks a cast",
472 t1, t2);
473 break;
474 case CPO_CONDITIONAL_EXPR:
475 emit_diagnostic (kind, input_location, 0,
476 "conditional expression between "
477 "distinct pointer types %qT and %qT lacks a cast",
478 t1, t2);
479 break;
480 default:
481 gcc_unreachable ();
485 /* Subroutine of composite_pointer_type to implement the recursive
486 case. See that function for documentation of the parameters. */
488 static tree
489 composite_pointer_type_r (tree t1, tree t2,
490 composite_pointer_operation operation,
491 tsubst_flags_t complain)
493 tree pointee1;
494 tree pointee2;
495 tree result_type;
496 tree attributes;
498 /* Determine the types pointed to by T1 and T2. */
499 if (TYPE_PTR_P (t1))
501 pointee1 = TREE_TYPE (t1);
502 pointee2 = TREE_TYPE (t2);
504 else
506 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
507 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
510 /* [expr.rel]
512 Otherwise, the composite pointer type is a pointer type
513 similar (_conv.qual_) to the type of one of the operands,
514 with a cv-qualification signature (_conv.qual_) that is the
515 union of the cv-qualification signatures of the operand
516 types. */
517 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
518 result_type = pointee1;
519 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
520 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
522 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
523 complain);
524 if (result_type == error_mark_node)
525 return error_mark_node;
527 else
529 if (complain & tf_error)
530 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
531 else
532 return error_mark_node;
533 result_type = void_type_node;
535 result_type = cp_build_qualified_type (result_type,
536 (cp_type_quals (pointee1)
537 | cp_type_quals (pointee2)));
538 /* If the original types were pointers to members, so is the
539 result. */
540 if (TYPE_PTRMEM_P (t1))
542 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
543 TYPE_PTRMEM_CLASS_TYPE (t2)))
545 if (complain & tf_error)
546 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
547 else
548 return error_mark_node;
550 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
551 result_type);
553 else
554 result_type = build_pointer_type (result_type);
556 /* Merge the attributes. */
557 attributes = (*targetm.merge_type_attributes) (t1, t2);
558 return build_type_attribute_variant (result_type, attributes);
561 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
562 ARG1 and ARG2 are the values with those types. The OPERATION is to
563 describe the operation between the pointer types,
564 in case an error occurs.
566 This routine also implements the computation of a common type for
567 pointers-to-members as per [expr.eq]. */
569 tree
570 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
571 composite_pointer_operation operation,
572 tsubst_flags_t complain)
574 tree class1;
575 tree class2;
577 /* [expr.rel]
579 If one operand is a null pointer constant, the composite pointer
580 type is the type of the other operand. */
581 if (null_ptr_cst_p (arg1))
582 return t2;
583 if (null_ptr_cst_p (arg2))
584 return t1;
586 /* We have:
588 [expr.rel]
590 If one of the operands has type "pointer to cv1 void*", then
591 the other has type "pointer to cv2T", and the composite pointer
592 type is "pointer to cv12 void", where cv12 is the union of cv1
593 and cv2.
595 If either type is a pointer to void, make sure it is T1. */
596 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
598 tree t;
599 t = t1;
600 t1 = t2;
601 t2 = t;
604 /* Now, if T1 is a pointer to void, merge the qualifiers. */
605 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
607 tree attributes;
608 tree result_type;
610 if (TYPE_PTRFN_P (t2))
612 if (complain & tf_error)
614 switch (operation)
616 case CPO_COMPARISON:
617 pedwarn (input_location, OPT_Wpedantic,
618 "ISO C++ forbids comparison between pointer "
619 "of type %<void *%> and pointer-to-function");
620 break;
621 case CPO_CONVERSION:
622 pedwarn (input_location, OPT_Wpedantic,
623 "ISO C++ forbids conversion between pointer "
624 "of type %<void *%> and pointer-to-function");
625 break;
626 case CPO_CONDITIONAL_EXPR:
627 pedwarn (input_location, OPT_Wpedantic,
628 "ISO C++ forbids conditional expression between "
629 "pointer of type %<void *%> and "
630 "pointer-to-function");
631 break;
632 default:
633 gcc_unreachable ();
636 else
637 return error_mark_node;
639 result_type
640 = cp_build_qualified_type (void_type_node,
641 (cp_type_quals (TREE_TYPE (t1))
642 | cp_type_quals (TREE_TYPE (t2))));
643 result_type = build_pointer_type (result_type);
644 /* Merge the attributes. */
645 attributes = (*targetm.merge_type_attributes) (t1, t2);
646 return build_type_attribute_variant (result_type, attributes);
649 if (c_dialect_objc () && TYPE_PTR_P (t1)
650 && TYPE_PTR_P (t2))
652 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
653 return objc_common_type (t1, t2);
656 /* [expr.eq] permits the application of a pointer conversion to
657 bring the pointers to a common type. */
658 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
659 && CLASS_TYPE_P (TREE_TYPE (t1))
660 && CLASS_TYPE_P (TREE_TYPE (t2))
661 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
662 TREE_TYPE (t2)))
664 class1 = TREE_TYPE (t1);
665 class2 = TREE_TYPE (t2);
667 if (DERIVED_FROM_P (class1, class2))
668 t2 = (build_pointer_type
669 (cp_build_qualified_type (class1, cp_type_quals (class2))));
670 else if (DERIVED_FROM_P (class2, class1))
671 t1 = (build_pointer_type
672 (cp_build_qualified_type (class2, cp_type_quals (class1))));
673 else
675 if (complain & tf_error)
676 composite_pointer_error (DK_ERROR, t1, t2, operation);
677 return error_mark_node;
680 /* [expr.eq] permits the application of a pointer-to-member
681 conversion to change the class type of one of the types. */
682 else if (TYPE_PTRMEM_P (t1)
683 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
684 TYPE_PTRMEM_CLASS_TYPE (t2)))
686 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
687 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
689 if (DERIVED_FROM_P (class1, class2))
690 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
691 else if (DERIVED_FROM_P (class2, class1))
692 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
693 else
695 if (complain & tf_error)
696 switch (operation)
698 case CPO_COMPARISON:
699 error ("comparison between distinct "
700 "pointer-to-member types %qT and %qT lacks a cast",
701 t1, t2);
702 break;
703 case CPO_CONVERSION:
704 error ("conversion between distinct "
705 "pointer-to-member types %qT and %qT lacks a cast",
706 t1, t2);
707 break;
708 case CPO_CONDITIONAL_EXPR:
709 error ("conditional expression between distinct "
710 "pointer-to-member types %qT and %qT lacks a cast",
711 t1, t2);
712 break;
713 default:
714 gcc_unreachable ();
716 return error_mark_node;
720 return composite_pointer_type_r (t1, t2, operation, complain);
723 /* Return the merged type of two types.
724 We assume that comptypes has already been done and returned 1;
725 if that isn't so, this may crash.
727 This just combines attributes and default arguments; any other
728 differences would cause the two types to compare unalike. */
730 tree
731 merge_types (tree t1, tree t2)
733 enum tree_code code1;
734 enum tree_code code2;
735 tree attributes;
737 /* Save time if the two types are the same. */
738 if (t1 == t2)
739 return t1;
740 if (original_type (t1) == original_type (t2))
741 return t1;
743 /* If one type is nonsense, use the other. */
744 if (t1 == error_mark_node)
745 return t2;
746 if (t2 == error_mark_node)
747 return t1;
749 /* Handle merging an auto redeclaration with a previous deduced
750 return type. */
751 if (is_auto (t1))
752 return t2;
754 /* Merge the attributes. */
755 attributes = (*targetm.merge_type_attributes) (t1, t2);
757 if (TYPE_PTRMEMFUNC_P (t1))
758 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
759 if (TYPE_PTRMEMFUNC_P (t2))
760 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
762 code1 = TREE_CODE (t1);
763 code2 = TREE_CODE (t2);
764 if (code1 != code2)
766 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
767 if (code1 == TYPENAME_TYPE)
769 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
770 code1 = TREE_CODE (t1);
772 else
774 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
775 code2 = TREE_CODE (t2);
779 switch (code1)
781 case POINTER_TYPE:
782 case REFERENCE_TYPE:
783 /* For two pointers, do this recursively on the target type. */
785 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
786 int quals = cp_type_quals (t1);
788 if (code1 == POINTER_TYPE)
789 t1 = build_pointer_type (target);
790 else
791 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
792 t1 = build_type_attribute_variant (t1, attributes);
793 t1 = cp_build_qualified_type (t1, quals);
795 if (TREE_CODE (target) == METHOD_TYPE)
796 t1 = build_ptrmemfunc_type (t1);
798 return t1;
801 case OFFSET_TYPE:
803 int quals;
804 tree pointee;
805 quals = cp_type_quals (t1);
806 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
807 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
808 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
809 pointee);
810 t1 = cp_build_qualified_type (t1, quals);
811 break;
814 case ARRAY_TYPE:
816 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
817 /* Save space: see if the result is identical to one of the args. */
818 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
819 return build_type_attribute_variant (t1, attributes);
820 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
821 return build_type_attribute_variant (t2, attributes);
822 /* Merge the element types, and have a size if either arg has one. */
823 t1 = build_cplus_array_type
824 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
825 break;
828 case FUNCTION_TYPE:
829 /* Function types: prefer the one that specified arg types.
830 If both do, merge the arg types. Also merge the return types. */
832 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
833 tree p1 = TYPE_ARG_TYPES (t1);
834 tree p2 = TYPE_ARG_TYPES (t2);
835 tree parms;
836 tree rval, raises;
837 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
839 /* Save space: see if the result is identical to one of the args. */
840 if (valtype == TREE_TYPE (t1) && ! p2)
841 return cp_build_type_attribute_variant (t1, attributes);
842 if (valtype == TREE_TYPE (t2) && ! p1)
843 return cp_build_type_attribute_variant (t2, attributes);
845 /* Simple way if one arg fails to specify argument types. */
846 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
847 parms = p2;
848 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
849 parms = p1;
850 else
851 parms = commonparms (p1, p2);
853 rval = build_function_type (valtype, parms);
854 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
855 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
856 rval = apply_memfn_quals (rval,
857 type_memfn_quals (t1),
858 type_memfn_rqual (t1));
859 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
860 TYPE_RAISES_EXCEPTIONS (t2));
861 t1 = build_exception_variant (rval, raises);
862 if (late_return_type_p)
863 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
864 break;
867 case METHOD_TYPE:
869 /* Get this value the long way, since TYPE_METHOD_BASETYPE
870 is just the main variant of this. */
871 tree basetype = class_of_this_parm (t2);
872 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
873 TYPE_RAISES_EXCEPTIONS (t2));
874 cp_ref_qualifier rqual = type_memfn_rqual (t1);
875 tree t3;
876 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
877 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
879 /* If this was a member function type, get back to the
880 original type of type member function (i.e., without
881 the class instance variable up front. */
882 t1 = build_function_type (TREE_TYPE (t1),
883 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
884 t2 = build_function_type (TREE_TYPE (t2),
885 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
886 t3 = merge_types (t1, t2);
887 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
888 TYPE_ARG_TYPES (t3));
889 t1 = build_exception_variant (t3, raises);
890 t1 = build_ref_qualified_type (t1, rqual);
891 if (late_return_type_1_p)
892 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
893 if (late_return_type_2_p)
894 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
895 break;
898 case TYPENAME_TYPE:
899 /* There is no need to merge attributes into a TYPENAME_TYPE.
900 When the type is instantiated it will have whatever
901 attributes result from the instantiation. */
902 return t1;
904 default:;
907 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
908 return t1;
909 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
910 return t2;
911 else
912 return cp_build_type_attribute_variant (t1, attributes);
915 /* Return the ARRAY_TYPE type without its domain. */
917 tree
918 strip_array_domain (tree type)
920 tree t2;
921 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
922 if (TYPE_DOMAIN (type) == NULL_TREE)
923 return type;
924 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
925 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
928 /* Wrapper around cp_common_type that is used by c-common.c and other
929 front end optimizations that remove promotions.
931 Return the common type for two arithmetic types T1 and T2 under the
932 usual arithmetic conversions. The default conversions have already
933 been applied, and enumerated types converted to their compatible
934 integer types. */
936 tree
937 common_type (tree t1, tree t2)
939 /* If one type is nonsense, use the other */
940 if (t1 == error_mark_node)
941 return t2;
942 if (t2 == error_mark_node)
943 return t1;
945 return cp_common_type (t1, t2);
948 /* Return the common type of two pointer types T1 and T2. This is the
949 type for the result of most arithmetic operations if the operands
950 have the given two types.
952 We assume that comp_target_types has already been done and returned
953 nonzero; if that isn't so, this may crash. */
955 tree
956 common_pointer_type (tree t1, tree t2)
958 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
959 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
960 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
962 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
963 CPO_CONVERSION, tf_warning_or_error);
966 /* Compare two exception specifier types for exactness or subsetness, if
967 allowed. Returns false for mismatch, true for match (same, or
968 derived and !exact).
970 [except.spec] "If a class X ... objects of class X or any class publicly
971 and unambiguously derived from X. Similarly, if a pointer type Y * ...
972 exceptions of type Y * or that are pointers to any type publicly and
973 unambiguously derived from Y. Otherwise a function only allows exceptions
974 that have the same type ..."
975 This does not mention cv qualifiers and is different to what throw
976 [except.throw] and catch [except.catch] will do. They will ignore the
977 top level cv qualifiers, and allow qualifiers in the pointer to class
978 example.
980 We implement the letter of the standard. */
982 static bool
983 comp_except_types (tree a, tree b, bool exact)
985 if (same_type_p (a, b))
986 return true;
987 else if (!exact)
989 if (cp_type_quals (a) || cp_type_quals (b))
990 return false;
992 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
994 a = TREE_TYPE (a);
995 b = TREE_TYPE (b);
996 if (cp_type_quals (a) || cp_type_quals (b))
997 return false;
1000 if (TREE_CODE (a) != RECORD_TYPE
1001 || TREE_CODE (b) != RECORD_TYPE)
1002 return false;
1004 if (publicly_uniquely_derived_p (a, b))
1005 return true;
1007 return false;
1010 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1011 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1012 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1013 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1014 are unordered, but we've already filtered out duplicates. Most lists will
1015 be in order, we should try to make use of that. */
1017 bool
1018 comp_except_specs (const_tree t1, const_tree t2, int exact)
1020 const_tree probe;
1021 const_tree base;
1022 int length = 0;
1024 if (t1 == t2)
1025 return true;
1027 /* First handle noexcept. */
1028 if (exact < ce_exact)
1030 /* noexcept(false) is compatible with no exception-specification,
1031 and stricter than any spec. */
1032 if (t1 == noexcept_false_spec)
1033 return t2 == NULL_TREE || exact == ce_derived;
1034 /* Even a derived noexcept(false) is compatible with no
1035 exception-specification. */
1036 if (t2 == noexcept_false_spec)
1037 return t1 == NULL_TREE;
1039 /* Otherwise, if we aren't looking for an exact match, noexcept is
1040 equivalent to throw(). */
1041 if (t1 == noexcept_true_spec)
1042 t1 = empty_except_spec;
1043 if (t2 == noexcept_true_spec)
1044 t2 = empty_except_spec;
1047 /* If any noexcept is left, it is only comparable to itself;
1048 either we're looking for an exact match or we're redeclaring a
1049 template with dependent noexcept. */
1050 if ((t1 && TREE_PURPOSE (t1))
1051 || (t2 && TREE_PURPOSE (t2)))
1052 return (t1 && t2
1053 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1055 if (t1 == NULL_TREE) /* T1 is ... */
1056 return t2 == NULL_TREE || exact == ce_derived;
1057 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1058 return t2 != NULL_TREE && !TREE_VALUE (t2);
1059 if (t2 == NULL_TREE) /* T2 is ... */
1060 return false;
1061 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1062 return exact == ce_derived;
1064 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1065 Count how many we find, to determine exactness. For exact matching and
1066 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1067 O(nm). */
1068 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1070 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1072 tree a = TREE_VALUE (probe);
1073 tree b = TREE_VALUE (t2);
1075 if (comp_except_types (a, b, exact))
1077 if (probe == base && exact > ce_derived)
1078 base = TREE_CHAIN (probe);
1079 length++;
1080 break;
1083 if (probe == NULL_TREE)
1084 return false;
1086 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1089 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1090 [] can match [size]. */
1092 static bool
1093 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1095 tree d1;
1096 tree d2;
1097 tree max1, max2;
1099 if (t1 == t2)
1100 return true;
1102 /* The type of the array elements must be the same. */
1103 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1104 return false;
1106 d1 = TYPE_DOMAIN (t1);
1107 d2 = TYPE_DOMAIN (t2);
1109 if (d1 == d2)
1110 return true;
1112 /* If one of the arrays is dimensionless, and the other has a
1113 dimension, they are of different types. However, it is valid to
1114 write:
1116 extern int a[];
1117 int a[3];
1119 by [basic.link]:
1121 declarations for an array object can specify
1122 array types that differ by the presence or absence of a major
1123 array bound (_dcl.array_). */
1124 if (!d1 || !d2)
1125 return allow_redeclaration;
1127 /* Check that the dimensions are the same. */
1129 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1130 return false;
1131 max1 = TYPE_MAX_VALUE (d1);
1132 max2 = TYPE_MAX_VALUE (d2);
1134 if (!cp_tree_equal (max1, max2))
1135 return false;
1137 return true;
1140 /* Compare the relative position of T1 and T2 into their respective
1141 template parameter list.
1142 T1 and T2 must be template parameter types.
1143 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1145 static bool
1146 comp_template_parms_position (tree t1, tree t2)
1148 tree index1, index2;
1149 gcc_assert (t1 && t2
1150 && TREE_CODE (t1) == TREE_CODE (t2)
1151 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1152 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1153 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1155 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1156 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1158 /* Then compare their relative position. */
1159 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1160 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1161 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1162 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1163 return false;
1165 /* In C++14 we can end up comparing 'auto' to a normal template
1166 parameter. Don't confuse them. */
1167 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1168 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1170 return true;
1173 /* Subroutine in comptypes. */
1175 static bool
1176 structural_comptypes (tree t1, tree t2, int strict)
1178 if (t1 == t2)
1179 return true;
1181 /* Suppress errors caused by previously reported errors. */
1182 if (t1 == error_mark_node || t2 == error_mark_node)
1183 return false;
1185 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1187 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1188 current instantiation. */
1189 if (TREE_CODE (t1) == TYPENAME_TYPE)
1190 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1192 if (TREE_CODE (t2) == TYPENAME_TYPE)
1193 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1195 if (TYPE_PTRMEMFUNC_P (t1))
1196 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1197 if (TYPE_PTRMEMFUNC_P (t2))
1198 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1200 /* Different classes of types can't be compatible. */
1201 if (TREE_CODE (t1) != TREE_CODE (t2))
1202 return false;
1204 /* Qualifiers must match. For array types, we will check when we
1205 recur on the array element types. */
1206 if (TREE_CODE (t1) != ARRAY_TYPE
1207 && cp_type_quals (t1) != cp_type_quals (t2))
1208 return false;
1209 if (TREE_CODE (t1) == FUNCTION_TYPE
1210 && type_memfn_quals (t1) != type_memfn_quals (t2))
1211 return false;
1212 /* Need to check this before TYPE_MAIN_VARIANT.
1213 FIXME function qualifiers should really change the main variant. */
1214 if ((TREE_CODE (t1) == FUNCTION_TYPE
1215 || TREE_CODE (t1) == METHOD_TYPE)
1216 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1217 return false;
1218 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1219 return false;
1221 /* Allow for two different type nodes which have essentially the same
1222 definition. Note that we already checked for equality of the type
1223 qualifiers (just above). */
1225 if (TREE_CODE (t1) != ARRAY_TYPE
1226 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1227 return true;
1230 /* Compare the types. Break out if they could be the same. */
1231 switch (TREE_CODE (t1))
1233 case VOID_TYPE:
1234 case BOOLEAN_TYPE:
1235 /* All void and bool types are the same. */
1236 break;
1238 case INTEGER_TYPE:
1239 case FIXED_POINT_TYPE:
1240 case REAL_TYPE:
1241 /* With these nodes, we can't determine type equivalence by
1242 looking at what is stored in the nodes themselves, because
1243 two nodes might have different TYPE_MAIN_VARIANTs but still
1244 represent the same type. For example, wchar_t and int could
1245 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1246 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1247 and are distinct types. On the other hand, int and the
1248 following typedef
1250 typedef int INT __attribute((may_alias));
1252 have identical properties, different TYPE_MAIN_VARIANTs, but
1253 represent the same type. The canonical type system keeps
1254 track of equivalence in this case, so we fall back on it. */
1255 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1257 case TEMPLATE_TEMPLATE_PARM:
1258 case BOUND_TEMPLATE_TEMPLATE_PARM:
1259 if (!comp_template_parms_position (t1, t2))
1260 return false;
1261 if (!comp_template_parms
1262 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1263 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1264 return false;
1265 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1266 break;
1267 /* Don't check inheritance. */
1268 strict = COMPARE_STRICT;
1269 /* Fall through. */
1271 case RECORD_TYPE:
1272 case UNION_TYPE:
1273 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1274 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1275 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1276 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1277 break;
1279 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1280 break;
1281 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1282 break;
1284 return false;
1286 case OFFSET_TYPE:
1287 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1288 strict & ~COMPARE_REDECLARATION))
1289 return false;
1290 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1291 return false;
1292 break;
1294 case REFERENCE_TYPE:
1295 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1296 return false;
1297 /* fall through to checks for pointer types */
1299 case POINTER_TYPE:
1300 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1301 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1302 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1303 return false;
1304 break;
1306 case METHOD_TYPE:
1307 case FUNCTION_TYPE:
1308 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1309 return false;
1310 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1311 return false;
1312 break;
1314 case ARRAY_TYPE:
1315 /* Target types must match incl. qualifiers. */
1316 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1317 return false;
1318 break;
1320 case TEMPLATE_TYPE_PARM:
1321 /* If T1 and T2 don't have the same relative position in their
1322 template parameters set, they can't be equal. */
1323 if (!comp_template_parms_position (t1, t2))
1324 return false;
1325 break;
1327 case TYPENAME_TYPE:
1328 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1329 TYPENAME_TYPE_FULLNAME (t2)))
1330 return false;
1331 /* Qualifiers don't matter on scopes. */
1332 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1333 TYPE_CONTEXT (t2)))
1334 return false;
1335 break;
1337 case UNBOUND_CLASS_TEMPLATE:
1338 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1339 return false;
1340 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1341 return false;
1342 break;
1344 case COMPLEX_TYPE:
1345 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1346 return false;
1347 break;
1349 case VECTOR_TYPE:
1350 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1351 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1352 return false;
1353 break;
1355 case TYPE_PACK_EXPANSION:
1356 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1357 PACK_EXPANSION_PATTERN (t2))
1358 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1359 PACK_EXPANSION_EXTRA_ARGS (t2)));
1361 case DECLTYPE_TYPE:
1362 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1363 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1364 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1365 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1366 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1367 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1368 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1369 DECLTYPE_TYPE_EXPR (t2)))
1370 return false;
1371 break;
1373 case UNDERLYING_TYPE:
1374 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1375 UNDERLYING_TYPE_TYPE (t2));
1377 default:
1378 return false;
1381 /* If we get here, we know that from a target independent POV the
1382 types are the same. Make sure the target attributes are also
1383 the same. */
1384 return comp_type_attributes (t1, t2);
1387 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1388 is a bitwise-or of the COMPARE_* flags. */
1390 bool
1391 comptypes (tree t1, tree t2, int strict)
1393 if (strict == COMPARE_STRICT)
1395 if (t1 == t2)
1396 return true;
1398 if (t1 == error_mark_node || t2 == error_mark_node)
1399 return false;
1401 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1402 /* At least one of the types requires structural equality, so
1403 perform a deep check. */
1404 return structural_comptypes (t1, t2, strict);
1406 #ifdef ENABLE_CHECKING
1407 if (USE_CANONICAL_TYPES)
1409 bool result = structural_comptypes (t1, t2, strict);
1411 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1412 /* The two types are structurally equivalent, but their
1413 canonical types were different. This is a failure of the
1414 canonical type propagation code.*/
1415 internal_error
1416 ("canonical types differ for identical types %T and %T",
1417 t1, t2);
1418 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1419 /* Two types are structurally different, but the canonical
1420 types are the same. This means we were over-eager in
1421 assigning canonical types. */
1422 internal_error
1423 ("same canonical type node for different types %T and %T",
1424 t1, t2);
1426 return result;
1428 #else
1429 if (USE_CANONICAL_TYPES)
1430 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1431 #endif
1432 else
1433 return structural_comptypes (t1, t2, strict);
1435 else if (strict == COMPARE_STRUCTURAL)
1436 return structural_comptypes (t1, t2, COMPARE_STRICT);
1437 else
1438 return structural_comptypes (t1, t2, strict);
1441 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1442 top-level qualifiers. */
1444 bool
1445 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1447 if (type1 == error_mark_node || type2 == error_mark_node)
1448 return false;
1450 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1453 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1455 bool
1456 at_least_as_qualified_p (const_tree type1, const_tree type2)
1458 int q1 = cp_type_quals (type1);
1459 int q2 = cp_type_quals (type2);
1461 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1462 return (q1 & q2) == q2;
1465 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1466 more cv-qualified that TYPE1, and 0 otherwise. */
1469 comp_cv_qualification (int q1, int q2)
1471 if (q1 == q2)
1472 return 0;
1474 if ((q1 & q2) == q2)
1475 return 1;
1476 else if ((q1 & q2) == q1)
1477 return -1;
1479 return 0;
1483 comp_cv_qualification (const_tree type1, const_tree type2)
1485 int q1 = cp_type_quals (type1);
1486 int q2 = cp_type_quals (type2);
1487 return comp_cv_qualification (q1, q2);
1490 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1491 subset of the cv-qualification signature of TYPE2, and the types
1492 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1495 comp_cv_qual_signature (tree type1, tree type2)
1497 if (comp_ptr_ttypes_real (type2, type1, -1))
1498 return 1;
1499 else if (comp_ptr_ttypes_real (type1, type2, -1))
1500 return -1;
1501 else
1502 return 0;
1505 /* Subroutines of `comptypes'. */
1507 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1508 equivalent in the sense that functions with those parameter types
1509 can have equivalent types. The two lists must be equivalent,
1510 element by element. */
1512 bool
1513 compparms (const_tree parms1, const_tree parms2)
1515 const_tree t1, t2;
1517 /* An unspecified parmlist matches any specified parmlist
1518 whose argument types don't need default promotions. */
1520 for (t1 = parms1, t2 = parms2;
1521 t1 || t2;
1522 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1524 /* If one parmlist is shorter than the other,
1525 they fail to match. */
1526 if (!t1 || !t2)
1527 return false;
1528 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1529 return false;
1531 return true;
1535 /* Process a sizeof or alignof expression where the operand is a
1536 type. */
1538 tree
1539 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1541 tree value;
1542 bool dependent_p;
1544 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1545 if (type == error_mark_node)
1546 return error_mark_node;
1548 type = non_reference (type);
1549 if (TREE_CODE (type) == METHOD_TYPE)
1551 if (complain)
1552 pedwarn (input_location, OPT_Wpointer_arith,
1553 "invalid application of %qs to a member function",
1554 operator_name_info[(int) op].name);
1555 else
1556 return error_mark_node;
1557 value = size_one_node;
1560 dependent_p = dependent_type_p (type);
1561 if (!dependent_p)
1562 complete_type (type);
1563 if (dependent_p
1564 /* VLA types will have a non-constant size. In the body of an
1565 uninstantiated template, we don't need to try to compute the
1566 value, because the sizeof expression is not an integral
1567 constant expression in that case. And, if we do try to
1568 compute the value, we'll likely end up with SAVE_EXPRs, which
1569 the template substitution machinery does not expect to see. */
1570 || (processing_template_decl
1571 && COMPLETE_TYPE_P (type)
1572 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1574 value = build_min (op, size_type_node, type);
1575 TREE_READONLY (value) = 1;
1576 return value;
1579 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (type)
1580 && (flag_iso || warn_vla > 0))
1582 if (complain)
1583 pedwarn (input_location, OPT_Wvla,
1584 "taking sizeof array of runtime bound");
1585 else
1586 return error_mark_node;
1589 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1590 op == SIZEOF_EXPR, false,
1591 complain);
1594 /* Return the size of the type, without producing any warnings for
1595 types whose size cannot be taken. This routine should be used only
1596 in some other routine that has already produced a diagnostic about
1597 using the size of such a type. */
1598 tree
1599 cxx_sizeof_nowarn (tree type)
1601 if (TREE_CODE (type) == FUNCTION_TYPE
1602 || VOID_TYPE_P (type)
1603 || TREE_CODE (type) == ERROR_MARK)
1604 return size_one_node;
1605 else if (!COMPLETE_TYPE_P (type))
1606 return size_zero_node;
1607 else
1608 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1611 /* Process a sizeof expression where the operand is an expression. */
1613 static tree
1614 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1616 if (e == error_mark_node)
1617 return error_mark_node;
1619 if (processing_template_decl)
1621 e = build_min (SIZEOF_EXPR, size_type_node, e);
1622 TREE_SIDE_EFFECTS (e) = 0;
1623 TREE_READONLY (e) = 1;
1625 return e;
1628 /* To get the size of a static data member declared as an array of
1629 unknown bound, we need to instantiate it. */
1630 if (VAR_P (e)
1631 && VAR_HAD_UNKNOWN_BOUND (e)
1632 && DECL_TEMPLATE_INSTANTIATION (e))
1633 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1635 if (TREE_CODE (e) == PARM_DECL
1636 && DECL_ARRAY_PARAMETER_P (e)
1637 && (complain & tf_warning))
1639 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1640 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1641 inform (DECL_SOURCE_LOCATION (e), "declared here");
1644 e = mark_type_use (e);
1646 if (TREE_CODE (e) == COMPONENT_REF
1647 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1648 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1650 if (complain & tf_error)
1651 error ("invalid application of %<sizeof%> to a bit-field");
1652 else
1653 return error_mark_node;
1654 e = char_type_node;
1656 else if (is_overloaded_fn (e))
1658 if (complain & tf_error)
1659 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1660 "function type");
1661 else
1662 return error_mark_node;
1663 e = char_type_node;
1665 else if (type_unknown_p (e))
1667 if (complain & tf_error)
1668 cxx_incomplete_type_error (e, TREE_TYPE (e));
1669 else
1670 return error_mark_node;
1671 e = char_type_node;
1673 else
1674 e = TREE_TYPE (e);
1676 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1679 /* Implement the __alignof keyword: Return the minimum required
1680 alignment of E, measured in bytes. For VAR_DECL's and
1681 FIELD_DECL's return DECL_ALIGN (which can be set from an
1682 "aligned" __attribute__ specification). */
1684 static tree
1685 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1687 tree t;
1689 if (e == error_mark_node)
1690 return error_mark_node;
1692 if (processing_template_decl)
1694 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1695 TREE_SIDE_EFFECTS (e) = 0;
1696 TREE_READONLY (e) = 1;
1698 return e;
1701 e = mark_type_use (e);
1703 if (VAR_P (e))
1704 t = size_int (DECL_ALIGN_UNIT (e));
1705 else if (TREE_CODE (e) == COMPONENT_REF
1706 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1707 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1709 if (complain & tf_error)
1710 error ("invalid application of %<__alignof%> to a bit-field");
1711 else
1712 return error_mark_node;
1713 t = size_one_node;
1715 else if (TREE_CODE (e) == COMPONENT_REF
1716 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1717 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1718 else if (is_overloaded_fn (e))
1720 if (complain & tf_error)
1721 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1722 "function type");
1723 else
1724 return error_mark_node;
1725 if (TREE_CODE (e) == FUNCTION_DECL)
1726 t = size_int (DECL_ALIGN_UNIT (e));
1727 else
1728 t = size_one_node;
1730 else if (type_unknown_p (e))
1732 if (complain & tf_error)
1733 cxx_incomplete_type_error (e, TREE_TYPE (e));
1734 else
1735 return error_mark_node;
1736 t = size_one_node;
1738 else
1739 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1740 complain & tf_error);
1742 return fold_convert (size_type_node, t);
1745 /* Process a sizeof or alignof expression E with code OP where the operand
1746 is an expression. */
1748 tree
1749 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1751 if (op == SIZEOF_EXPR)
1752 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1753 else
1754 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1757 /* Build a representation of an expression 'alignas(E).' Return the
1758 folded integer value of E if it is an integral constant expression
1759 that resolves to a valid alignment. If E depends on a template
1760 parameter, return a syntactic representation tree of kind
1761 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1762 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1764 tree
1765 cxx_alignas_expr (tree e)
1767 if (e == NULL_TREE || e == error_mark_node
1768 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1769 return e;
1771 if (TYPE_P (e))
1772 /* [dcl.align]/3:
1774 When the alignment-specifier is of the form
1775 alignas(type-id ), it shall have the same effect as
1776 alignas(alignof(type-id )). */
1778 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1780 /* If we reach this point, it means the alignas expression if of
1781 the form "alignas(assignment-expression)", so we should follow
1782 what is stated by [dcl.align]/2. */
1784 if (value_dependent_expression_p (e))
1785 /* Leave value-dependent expression alone for now. */
1786 return e;
1788 e = fold_non_dependent_expr (e);
1789 e = mark_rvalue_use (e);
1791 /* [dcl.align]/2 says:
1793 the assignment-expression shall be an integral constant
1794 expression. */
1796 return cxx_constant_value (e);
1800 /* EXPR is being used in a context that is not a function call.
1801 Enforce:
1803 [expr.ref]
1805 The expression can be used only as the left-hand operand of a
1806 member function call.
1808 [expr.mptr.operator]
1810 If the result of .* or ->* is a function, then that result can be
1811 used only as the operand for the function call operator ().
1813 by issuing an error message if appropriate. Returns true iff EXPR
1814 violates these rules. */
1816 bool
1817 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1819 if (expr == NULL_TREE)
1820 return false;
1821 /* Don't enforce this in MS mode. */
1822 if (flag_ms_extensions)
1823 return false;
1824 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1825 expr = get_first_fn (expr);
1826 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1828 if (complain & tf_error)
1829 error ("invalid use of non-static member function");
1830 return true;
1832 return false;
1835 /* If EXP is a reference to a bitfield, and the type of EXP does not
1836 match the declared type of the bitfield, return the declared type
1837 of the bitfield. Otherwise, return NULL_TREE. */
1839 tree
1840 is_bitfield_expr_with_lowered_type (const_tree exp)
1842 switch (TREE_CODE (exp))
1844 case COND_EXPR:
1845 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1846 ? TREE_OPERAND (exp, 1)
1847 : TREE_OPERAND (exp, 0)))
1848 return NULL_TREE;
1849 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1851 case COMPOUND_EXPR:
1852 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1854 case MODIFY_EXPR:
1855 case SAVE_EXPR:
1856 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1858 case COMPONENT_REF:
1860 tree field;
1862 field = TREE_OPERAND (exp, 1);
1863 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1864 return NULL_TREE;
1865 if (same_type_ignoring_top_level_qualifiers_p
1866 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1867 return NULL_TREE;
1868 return DECL_BIT_FIELD_TYPE (field);
1871 CASE_CONVERT:
1872 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1873 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1874 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1875 /* Fallthrough. */
1877 default:
1878 return NULL_TREE;
1882 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1883 bitfield with a lowered type, the type of EXP is returned, rather
1884 than NULL_TREE. */
1886 tree
1887 unlowered_expr_type (const_tree exp)
1889 tree type;
1890 tree etype = TREE_TYPE (exp);
1892 type = is_bitfield_expr_with_lowered_type (exp);
1893 if (type)
1894 type = cp_build_qualified_type (type, cp_type_quals (etype));
1895 else
1896 type = etype;
1898 return type;
1901 /* Perform the conversions in [expr] that apply when an lvalue appears
1902 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1903 function-to-pointer conversions. In addition, manifest constants
1904 are replaced by their values, and bitfield references are converted
1905 to their declared types. Note that this function does not perform the
1906 lvalue-to-rvalue conversion for class types. If you need that conversion
1907 to for class types, then you probably need to use force_rvalue.
1909 Although the returned value is being used as an rvalue, this
1910 function does not wrap the returned expression in a
1911 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1912 that the return value is no longer an lvalue. */
1914 tree
1915 decay_conversion (tree exp, tsubst_flags_t complain)
1917 tree type;
1918 enum tree_code code;
1919 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1921 type = TREE_TYPE (exp);
1922 if (type == error_mark_node)
1923 return error_mark_node;
1925 exp = mark_rvalue_use (exp);
1927 exp = resolve_nondeduced_context (exp);
1928 if (type_unknown_p (exp))
1930 if (complain & tf_error)
1931 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1932 return error_mark_node;
1935 code = TREE_CODE (type);
1937 /* For an array decl decay_conversion should not try to return its
1938 initializer. */
1939 if (code != ARRAY_TYPE)
1941 /* FIXME remove? at least need to remember that this isn't really a
1942 constant expression if EXP isn't decl_constant_var_p, like with
1943 C_MAYBE_CONST_EXPR. */
1944 exp = decl_constant_value_safe (exp);
1945 if (error_operand_p (exp))
1946 return error_mark_node;
1949 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1950 return nullptr_node;
1952 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1953 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1954 if (code == VOID_TYPE)
1956 if (complain & tf_error)
1957 error_at (loc, "void value not ignored as it ought to be");
1958 return error_mark_node;
1960 if (invalid_nonstatic_memfn_p (exp, complain))
1961 return error_mark_node;
1962 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1963 return cp_build_addr_expr (exp, complain);
1964 if (code == ARRAY_TYPE)
1966 tree adr;
1967 tree ptrtype;
1969 if (INDIRECT_REF_P (exp))
1970 return build_nop (build_pointer_type (TREE_TYPE (type)),
1971 TREE_OPERAND (exp, 0));
1973 if (TREE_CODE (exp) == COMPOUND_EXPR)
1975 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1976 if (op1 == error_mark_node)
1977 return error_mark_node;
1978 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1979 TREE_OPERAND (exp, 0), op1);
1982 if (!lvalue_p (exp)
1983 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1985 if (complain & tf_error)
1986 error_at (loc, "invalid use of non-lvalue array");
1987 return error_mark_node;
1990 /* Don't let an array compound literal decay to a pointer. It can
1991 still be used to initialize an array or bind to a reference. */
1992 if (TREE_CODE (exp) == TARGET_EXPR)
1994 if (complain & tf_error)
1995 error_at (loc, "taking address of temporary array");
1996 return error_mark_node;
1999 ptrtype = build_pointer_type (TREE_TYPE (type));
2001 if (VAR_P (exp))
2003 if (!cxx_mark_addressable (exp))
2004 return error_mark_node;
2005 adr = build_nop (ptrtype, build_address (exp));
2006 return adr;
2008 /* This way is better for a COMPONENT_REF since it can
2009 simplify the offset for a component. */
2010 adr = cp_build_addr_expr (exp, complain);
2011 return cp_convert (ptrtype, adr, complain);
2014 /* If a bitfield is used in a context where integral promotion
2015 applies, then the caller is expected to have used
2016 default_conversion. That function promotes bitfields correctly
2017 before calling this function. At this point, if we have a
2018 bitfield referenced, we may assume that is not subject to
2019 promotion, and that, therefore, the type of the resulting rvalue
2020 is the declared type of the bitfield. */
2021 exp = convert_bitfield_to_declared_type (exp);
2023 /* We do not call rvalue() here because we do not want to wrap EXP
2024 in a NON_LVALUE_EXPR. */
2026 /* [basic.lval]
2028 Non-class rvalues always have cv-unqualified types. */
2029 type = TREE_TYPE (exp);
2030 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2031 exp = build_nop (cv_unqualified (type), exp);
2033 return exp;
2036 /* Perform preparatory conversions, as part of the "usual arithmetic
2037 conversions". In particular, as per [expr]:
2039 Whenever an lvalue expression appears as an operand of an
2040 operator that expects the rvalue for that operand, the
2041 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2042 standard conversions are applied to convert the expression to an
2043 rvalue.
2045 In addition, we perform integral promotions here, as those are
2046 applied to both operands to a binary operator before determining
2047 what additional conversions should apply. */
2049 static tree
2050 cp_default_conversion (tree exp, tsubst_flags_t complain)
2052 /* Check for target-specific promotions. */
2053 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2054 if (promoted_type)
2055 exp = cp_convert (promoted_type, exp, complain);
2056 /* Perform the integral promotions first so that bitfield
2057 expressions (which may promote to "int", even if the bitfield is
2058 declared "unsigned") are promoted correctly. */
2059 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2060 exp = cp_perform_integral_promotions (exp, complain);
2061 /* Perform the other conversions. */
2062 exp = decay_conversion (exp, complain);
2064 return exp;
2067 /* C version. */
2069 tree
2070 default_conversion (tree exp)
2072 return cp_default_conversion (exp, tf_warning_or_error);
2075 /* EXPR is an expression with an integral or enumeration type.
2076 Perform the integral promotions in [conv.prom], and return the
2077 converted value. */
2079 tree
2080 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2082 tree type;
2083 tree promoted_type;
2085 expr = mark_rvalue_use (expr);
2087 /* [conv.prom]
2089 If the bitfield has an enumerated type, it is treated as any
2090 other value of that type for promotion purposes. */
2091 type = is_bitfield_expr_with_lowered_type (expr);
2092 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2093 type = TREE_TYPE (expr);
2094 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2095 /* Scoped enums don't promote. */
2096 if (SCOPED_ENUM_P (type))
2097 return expr;
2098 promoted_type = type_promotes_to (type);
2099 if (type != promoted_type)
2100 expr = cp_convert (promoted_type, expr, complain);
2101 return expr;
2104 /* C version. */
2106 tree
2107 perform_integral_promotions (tree expr)
2109 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2112 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2113 decay_conversion to one. */
2116 string_conv_p (const_tree totype, const_tree exp, int warn)
2118 tree t;
2120 if (!TYPE_PTR_P (totype))
2121 return 0;
2123 t = TREE_TYPE (totype);
2124 if (!same_type_p (t, char_type_node)
2125 && !same_type_p (t, char16_type_node)
2126 && !same_type_p (t, char32_type_node)
2127 && !same_type_p (t, wchar_type_node))
2128 return 0;
2130 if (TREE_CODE (exp) == STRING_CST)
2132 /* Make sure that we don't try to convert between char and wide chars. */
2133 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2134 return 0;
2136 else
2138 /* Is this a string constant which has decayed to 'const char *'? */
2139 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2140 if (!same_type_p (TREE_TYPE (exp), t))
2141 return 0;
2142 STRIP_NOPS (exp);
2143 if (TREE_CODE (exp) != ADDR_EXPR
2144 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2145 return 0;
2148 /* This warning is not very useful, as it complains about printf. */
2149 if (warn)
2150 warning (OPT_Wwrite_strings,
2151 "deprecated conversion from string constant to %qT",
2152 totype);
2154 return 1;
2157 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2158 can, for example, use as an lvalue. This code used to be in
2159 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2160 expressions, where we're dealing with aggregates. But now it's again only
2161 called from unary_complex_lvalue. The case (in particular) that led to
2162 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2163 get it there. */
2165 static tree
2166 rationalize_conditional_expr (enum tree_code code, tree t,
2167 tsubst_flags_t complain)
2169 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2171 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2172 the first operand is always the one to be used if both operands
2173 are equal, so we know what conditional expression this used to be. */
2174 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2176 tree op0 = TREE_OPERAND (t, 0);
2177 tree op1 = TREE_OPERAND (t, 1);
2179 /* The following code is incorrect if either operand side-effects. */
2180 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2181 && !TREE_SIDE_EFFECTS (op1));
2182 return
2183 build_conditional_expr (loc,
2184 build_x_binary_op (loc,
2185 (TREE_CODE (t) == MIN_EXPR
2186 ? LE_EXPR : GE_EXPR),
2187 op0, TREE_CODE (op0),
2188 op1, TREE_CODE (op1),
2189 /*overload=*/NULL,
2190 complain),
2191 cp_build_unary_op (code, op0, 0, complain),
2192 cp_build_unary_op (code, op1, 0, complain),
2193 complain);
2196 return
2197 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2198 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2199 complain),
2200 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2201 complain),
2202 complain);
2205 /* Given the TYPE of an anonymous union field inside T, return the
2206 FIELD_DECL for the field. If not found return NULL_TREE. Because
2207 anonymous unions can nest, we must also search all anonymous unions
2208 that are directly reachable. */
2210 tree
2211 lookup_anon_field (tree t, tree type)
2213 tree field;
2215 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2217 if (TREE_STATIC (field))
2218 continue;
2219 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2220 continue;
2222 /* If we find it directly, return the field. */
2223 if (DECL_NAME (field) == NULL_TREE
2224 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2226 return field;
2229 /* Otherwise, it could be nested, search harder. */
2230 if (DECL_NAME (field) == NULL_TREE
2231 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2233 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2234 if (subfield)
2235 return subfield;
2238 return NULL_TREE;
2241 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2242 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2243 non-NULL, it indicates the path to the base used to name MEMBER.
2244 If PRESERVE_REFERENCE is true, the expression returned will have
2245 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2246 returned will have the type referred to by the reference.
2248 This function does not perform access control; that is either done
2249 earlier by the parser when the name of MEMBER is resolved to MEMBER
2250 itself, or later when overload resolution selects one of the
2251 functions indicated by MEMBER. */
2253 tree
2254 build_class_member_access_expr (tree object, tree member,
2255 tree access_path, bool preserve_reference,
2256 tsubst_flags_t complain)
2258 tree object_type;
2259 tree member_scope;
2260 tree result = NULL_TREE;
2261 tree using_decl = NULL_TREE;
2263 if (error_operand_p (object) || error_operand_p (member))
2264 return error_mark_node;
2266 gcc_assert (DECL_P (member) || BASELINK_P (member));
2268 /* [expr.ref]
2270 The type of the first expression shall be "class object" (of a
2271 complete type). */
2272 object_type = TREE_TYPE (object);
2273 if (!currently_open_class (object_type)
2274 && !complete_type_or_maybe_complain (object_type, object, complain))
2275 return error_mark_node;
2276 if (!CLASS_TYPE_P (object_type))
2278 if (complain & tf_error)
2280 if (POINTER_TYPE_P (object_type)
2281 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2282 error ("request for member %qD in %qE, which is of pointer "
2283 "type %qT (maybe you meant to use %<->%> ?)",
2284 member, object, object_type);
2285 else
2286 error ("request for member %qD in %qE, which is of non-class "
2287 "type %qT", member, object, object_type);
2289 return error_mark_node;
2292 /* The standard does not seem to actually say that MEMBER must be a
2293 member of OBJECT_TYPE. However, that is clearly what is
2294 intended. */
2295 if (DECL_P (member))
2297 member_scope = DECL_CLASS_CONTEXT (member);
2298 mark_used (member);
2299 if (TREE_DEPRECATED (member))
2300 warn_deprecated_use (member, NULL_TREE);
2302 else
2303 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2304 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2305 presently be the anonymous union. Go outwards until we find a
2306 type related to OBJECT_TYPE. */
2307 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2308 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2309 object_type))
2310 member_scope = TYPE_CONTEXT (member_scope);
2311 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2313 if (complain & tf_error)
2315 if (TREE_CODE (member) == FIELD_DECL)
2316 error ("invalid use of nonstatic data member %qE", member);
2317 else
2318 error ("%qD is not a member of %qT", member, object_type);
2320 return error_mark_node;
2323 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2324 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2325 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2327 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2328 if (temp)
2329 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2332 /* In [expr.ref], there is an explicit list of the valid choices for
2333 MEMBER. We check for each of those cases here. */
2334 if (VAR_P (member))
2336 /* A static data member. */
2337 result = member;
2338 mark_exp_read (object);
2339 /* If OBJECT has side-effects, they are supposed to occur. */
2340 if (TREE_SIDE_EFFECTS (object))
2341 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2343 else if (TREE_CODE (member) == FIELD_DECL)
2345 /* A non-static data member. */
2346 bool null_object_p;
2347 int type_quals;
2348 tree member_type;
2350 null_object_p = (INDIRECT_REF_P (object)
2351 && integer_zerop (TREE_OPERAND (object, 0)));
2353 /* Convert OBJECT to the type of MEMBER. */
2354 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2355 TYPE_MAIN_VARIANT (member_scope)))
2357 tree binfo;
2358 base_kind kind;
2360 binfo = lookup_base (access_path ? access_path : object_type,
2361 member_scope, ba_unique, &kind, complain);
2362 if (binfo == error_mark_node)
2363 return error_mark_node;
2365 /* It is invalid to try to get to a virtual base of a
2366 NULL object. The most common cause is invalid use of
2367 offsetof macro. */
2368 if (null_object_p && kind == bk_via_virtual)
2370 if (complain & tf_error)
2372 error ("invalid access to non-static data member %qD in "
2373 "virtual base of NULL object", member);
2375 return error_mark_node;
2378 /* Convert to the base. */
2379 object = build_base_path (PLUS_EXPR, object, binfo,
2380 /*nonnull=*/1, complain);
2381 /* If we found the base successfully then we should be able
2382 to convert to it successfully. */
2383 gcc_assert (object != error_mark_node);
2386 /* If MEMBER is from an anonymous aggregate, we have converted
2387 OBJECT so that it refers to the class containing the
2388 anonymous union. Generate a reference to the anonymous union
2389 itself, and recur to find MEMBER. */
2390 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2391 /* When this code is called from build_field_call, the
2392 object already has the type of the anonymous union.
2393 That is because the COMPONENT_REF was already
2394 constructed, and was then disassembled before calling
2395 build_field_call. After the function-call code is
2396 cleaned up, this waste can be eliminated. */
2397 && (!same_type_ignoring_top_level_qualifiers_p
2398 (TREE_TYPE (object), DECL_CONTEXT (member))))
2400 tree anonymous_union;
2402 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2403 DECL_CONTEXT (member));
2404 object = build_class_member_access_expr (object,
2405 anonymous_union,
2406 /*access_path=*/NULL_TREE,
2407 preserve_reference,
2408 complain);
2411 /* Compute the type of the field, as described in [expr.ref]. */
2412 type_quals = TYPE_UNQUALIFIED;
2413 member_type = TREE_TYPE (member);
2414 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2416 type_quals = (cp_type_quals (member_type)
2417 | cp_type_quals (object_type));
2419 /* A field is const (volatile) if the enclosing object, or the
2420 field itself, is const (volatile). But, a mutable field is
2421 not const, even within a const object. */
2422 if (DECL_MUTABLE_P (member))
2423 type_quals &= ~TYPE_QUAL_CONST;
2424 member_type = cp_build_qualified_type (member_type, type_quals);
2427 result = build3 (COMPONENT_REF, member_type, object, member,
2428 NULL_TREE);
2429 result = fold_if_not_in_template (result);
2431 /* Mark the expression const or volatile, as appropriate. Even
2432 though we've dealt with the type above, we still have to mark the
2433 expression itself. */
2434 if (type_quals & TYPE_QUAL_CONST)
2435 TREE_READONLY (result) = 1;
2436 if (type_quals & TYPE_QUAL_VOLATILE)
2437 TREE_THIS_VOLATILE (result) = 1;
2439 else if (BASELINK_P (member))
2441 /* The member is a (possibly overloaded) member function. */
2442 tree functions;
2443 tree type;
2445 /* If the MEMBER is exactly one static member function, then we
2446 know the type of the expression. Otherwise, we must wait
2447 until overload resolution has been performed. */
2448 functions = BASELINK_FUNCTIONS (member);
2449 if (TREE_CODE (functions) == FUNCTION_DECL
2450 && DECL_STATIC_FUNCTION_P (functions))
2451 type = TREE_TYPE (functions);
2452 else
2453 type = unknown_type_node;
2454 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2455 base. That will happen when the function is called. */
2456 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2458 else if (TREE_CODE (member) == CONST_DECL)
2460 /* The member is an enumerator. */
2461 result = member;
2462 /* If OBJECT has side-effects, they are supposed to occur. */
2463 if (TREE_SIDE_EFFECTS (object))
2464 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2465 object, result);
2467 else if ((using_decl = strip_using_decl (member)) != member)
2468 result = build_class_member_access_expr (object,
2469 using_decl,
2470 access_path, preserve_reference,
2471 complain);
2472 else
2474 if (complain & tf_error)
2475 error ("invalid use of %qD", member);
2476 return error_mark_node;
2479 if (!preserve_reference)
2480 /* [expr.ref]
2482 If E2 is declared to have type "reference to T", then ... the
2483 type of E1.E2 is T. */
2484 result = convert_from_reference (result);
2486 return result;
2489 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2490 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2492 static tree
2493 lookup_destructor (tree object, tree scope, tree dtor_name,
2494 tsubst_flags_t complain)
2496 tree object_type = TREE_TYPE (object);
2497 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2498 tree expr;
2500 /* We've already complained about this destructor. */
2501 if (dtor_type == error_mark_node)
2502 return error_mark_node;
2504 if (scope && !check_dtor_name (scope, dtor_type))
2506 if (complain & tf_error)
2507 error ("qualified type %qT does not match destructor name ~%qT",
2508 scope, dtor_type);
2509 return error_mark_node;
2511 if (is_auto (dtor_type))
2512 dtor_type = object_type;
2513 else if (identifier_p (dtor_type))
2515 /* In a template, names we can't find a match for are still accepted
2516 destructor names, and we check them here. */
2517 if (check_dtor_name (object_type, dtor_type))
2518 dtor_type = object_type;
2519 else
2521 if (complain & tf_error)
2522 error ("object type %qT does not match destructor name ~%qT",
2523 object_type, dtor_type);
2524 return error_mark_node;
2528 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2530 if (complain & tf_error)
2531 error ("the type being destroyed is %qT, but the destructor "
2532 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2533 return error_mark_node;
2535 expr = lookup_member (dtor_type, complete_dtor_identifier,
2536 /*protect=*/1, /*want_type=*/false,
2537 tf_warning_or_error);
2538 expr = (adjust_result_of_qualified_name_lookup
2539 (expr, dtor_type, object_type));
2540 if (scope == NULL_TREE)
2541 /* We need to call adjust_result_of_qualified_name_lookup in case the
2542 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2543 that we still get virtual function binding. */
2544 BASELINK_QUALIFIED_P (expr) = false;
2545 return expr;
2548 /* An expression of the form "A::template B" has been resolved to
2549 DECL. Issue a diagnostic if B is not a template or template
2550 specialization. */
2552 void
2553 check_template_keyword (tree decl)
2555 /* The standard says:
2557 [temp.names]
2559 If a name prefixed by the keyword template is not a member
2560 template, the program is ill-formed.
2562 DR 228 removed the restriction that the template be a member
2563 template.
2565 DR 96, if accepted would add the further restriction that explicit
2566 template arguments must be provided if the template keyword is
2567 used, but, as of 2005-10-16, that DR is still in "drafting". If
2568 this DR is accepted, then the semantic checks here can be
2569 simplified, as the entity named must in fact be a template
2570 specialization, rather than, as at present, a set of overloaded
2571 functions containing at least one template function. */
2572 if (TREE_CODE (decl) != TEMPLATE_DECL
2573 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2575 if (!is_overloaded_fn (decl))
2576 permerror (input_location, "%qD is not a template", decl);
2577 else
2579 tree fns;
2580 fns = decl;
2581 if (BASELINK_P (fns))
2582 fns = BASELINK_FUNCTIONS (fns);
2583 while (fns)
2585 tree fn = OVL_CURRENT (fns);
2586 if (TREE_CODE (fn) == TEMPLATE_DECL
2587 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2588 break;
2589 if (TREE_CODE (fn) == FUNCTION_DECL
2590 && DECL_USE_TEMPLATE (fn)
2591 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2592 break;
2593 fns = OVL_NEXT (fns);
2595 if (!fns)
2596 permerror (input_location, "%qD is not a template", decl);
2601 /* This function is called by the parser to process a class member
2602 access expression of the form OBJECT.NAME. NAME is a node used by
2603 the parser to represent a name; it is not yet a DECL. It may,
2604 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2605 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2606 there is no reason to do the lookup twice, so the parser keeps the
2607 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2608 be a template via the use of the "A::template B" syntax. */
2610 tree
2611 finish_class_member_access_expr (tree object, tree name, bool template_p,
2612 tsubst_flags_t complain)
2614 tree expr;
2615 tree object_type;
2616 tree member;
2617 tree access_path = NULL_TREE;
2618 tree orig_object = object;
2619 tree orig_name = name;
2621 if (object == error_mark_node || name == error_mark_node)
2622 return error_mark_node;
2624 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2625 if (!objc_is_public (object, name))
2626 return error_mark_node;
2628 object_type = TREE_TYPE (object);
2630 if (processing_template_decl)
2632 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2633 dependent_type_p (object_type)
2634 /* If NAME is just an IDENTIFIER_NODE, then the expression
2635 is dependent. */
2636 || identifier_p (object)
2637 /* If NAME is "f<args>", where either 'f' or 'args' is
2638 dependent, then the expression is dependent. */
2639 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2640 && dependent_template_id_p (TREE_OPERAND (name, 0),
2641 TREE_OPERAND (name, 1)))
2642 /* If NAME is "T::X" where "T" is dependent, then the
2643 expression is dependent. */
2644 || (TREE_CODE (name) == SCOPE_REF
2645 && TYPE_P (TREE_OPERAND (name, 0))
2646 && dependent_type_p (TREE_OPERAND (name, 0))))
2647 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2648 object, name, NULL_TREE);
2649 object = build_non_dependent_expr (object);
2651 else if (c_dialect_objc ()
2652 && identifier_p (name)
2653 && (expr = objc_maybe_build_component_ref (object, name)))
2654 return expr;
2656 /* [expr.ref]
2658 The type of the first expression shall be "class object" (of a
2659 complete type). */
2660 if (!currently_open_class (object_type)
2661 && !complete_type_or_maybe_complain (object_type, object, complain))
2662 return error_mark_node;
2663 if (!CLASS_TYPE_P (object_type))
2665 if (complain & tf_error)
2667 if (POINTER_TYPE_P (object_type)
2668 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2669 error ("request for member %qD in %qE, which is of pointer "
2670 "type %qT (maybe you meant to use %<->%> ?)",
2671 name, object, object_type);
2672 else
2673 error ("request for member %qD in %qE, which is of non-class "
2674 "type %qT", name, object, object_type);
2676 return error_mark_node;
2679 if (BASELINK_P (name))
2680 /* A member function that has already been looked up. */
2681 member = name;
2682 else
2684 bool is_template_id = false;
2685 tree template_args = NULL_TREE;
2686 tree scope;
2688 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2690 is_template_id = true;
2691 template_args = TREE_OPERAND (name, 1);
2692 name = TREE_OPERAND (name, 0);
2694 if (TREE_CODE (name) == OVERLOAD)
2695 name = DECL_NAME (get_first_fn (name));
2696 else if (DECL_P (name))
2697 name = DECL_NAME (name);
2700 if (TREE_CODE (name) == SCOPE_REF)
2702 /* A qualified name. The qualifying class or namespace `S'
2703 has already been looked up; it is either a TYPE or a
2704 NAMESPACE_DECL. */
2705 scope = TREE_OPERAND (name, 0);
2706 name = TREE_OPERAND (name, 1);
2708 /* If SCOPE is a namespace, then the qualified name does not
2709 name a member of OBJECT_TYPE. */
2710 if (TREE_CODE (scope) == NAMESPACE_DECL)
2712 if (complain & tf_error)
2713 error ("%<%D::%D%> is not a member of %qT",
2714 scope, name, object_type);
2715 return error_mark_node;
2718 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2720 /* Looking up a member enumerator (c++/56793). */
2721 if (!TYPE_CLASS_SCOPE_P (scope)
2722 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2724 if (complain & tf_error)
2725 error ("%<%D::%D%> is not a member of %qT",
2726 scope, name, object_type);
2727 return error_mark_node;
2729 tree val = lookup_enumerator (scope, name);
2730 if (TREE_SIDE_EFFECTS (object))
2731 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2732 return val;
2735 gcc_assert (CLASS_TYPE_P (scope));
2736 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2738 if (constructor_name_p (name, scope))
2740 if (complain & tf_error)
2741 error ("cannot call constructor %<%T::%D%> directly",
2742 scope, name);
2743 return error_mark_node;
2746 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2747 access_path = lookup_base (object_type, scope, ba_check,
2748 NULL, complain);
2749 if (access_path == error_mark_node)
2750 return error_mark_node;
2751 if (!access_path)
2753 if (complain & tf_error)
2754 error ("%qT is not a base of %qT", scope, object_type);
2755 return error_mark_node;
2758 else
2760 scope = NULL_TREE;
2761 access_path = object_type;
2764 if (TREE_CODE (name) == BIT_NOT_EXPR)
2765 member = lookup_destructor (object, scope, name, complain);
2766 else
2768 /* Look up the member. */
2769 member = lookup_member (access_path, name, /*protect=*/1,
2770 /*want_type=*/false, complain);
2771 if (member == NULL_TREE)
2773 if (complain & tf_error)
2774 error ("%qD has no member named %qE",
2775 TREE_CODE (access_path) == TREE_BINFO
2776 ? TREE_TYPE (access_path) : object_type, name);
2777 return error_mark_node;
2779 if (member == error_mark_node)
2780 return error_mark_node;
2783 if (is_template_id)
2785 tree templ = member;
2787 if (BASELINK_P (templ))
2788 templ = lookup_template_function (templ, template_args);
2789 else
2791 if (complain & tf_error)
2792 error ("%qD is not a member template function", name);
2793 return error_mark_node;
2798 if (TREE_DEPRECATED (member))
2799 warn_deprecated_use (member, NULL_TREE);
2801 if (template_p)
2802 check_template_keyword (member);
2804 expr = build_class_member_access_expr (object, member, access_path,
2805 /*preserve_reference=*/false,
2806 complain);
2807 if (processing_template_decl && expr != error_mark_node)
2809 if (BASELINK_P (member))
2811 if (TREE_CODE (orig_name) == SCOPE_REF)
2812 BASELINK_QUALIFIED_P (member) = 1;
2813 orig_name = member;
2815 return build_min_non_dep (COMPONENT_REF, expr,
2816 orig_object, orig_name,
2817 NULL_TREE);
2820 return expr;
2823 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2824 type. */
2826 tree
2827 build_simple_component_ref (tree object, tree member)
2829 tree type = cp_build_qualified_type (TREE_TYPE (member),
2830 cp_type_quals (TREE_TYPE (object)));
2831 return fold_build3_loc (input_location,
2832 COMPONENT_REF, type,
2833 object, member, NULL_TREE);
2836 /* Return an expression for the MEMBER_NAME field in the internal
2837 representation of PTRMEM, a pointer-to-member function. (Each
2838 pointer-to-member function type gets its own RECORD_TYPE so it is
2839 more convenient to access the fields by name than by FIELD_DECL.)
2840 This routine converts the NAME to a FIELD_DECL and then creates the
2841 node for the complete expression. */
2843 tree
2844 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2846 tree ptrmem_type;
2847 tree member;
2849 /* This code is a stripped down version of
2850 build_class_member_access_expr. It does not work to use that
2851 routine directly because it expects the object to be of class
2852 type. */
2853 ptrmem_type = TREE_TYPE (ptrmem);
2854 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2855 for (member = TYPE_FIELDS (ptrmem_type); member;
2856 member = DECL_CHAIN (member))
2857 if (DECL_NAME (member) == member_name)
2858 break;
2859 return build_simple_component_ref (ptrmem, member);
2862 /* Given an expression PTR for a pointer, return an expression
2863 for the value pointed to.
2864 ERRORSTRING is the name of the operator to appear in error messages.
2866 This function may need to overload OPERATOR_FNNAME.
2867 Must also handle REFERENCE_TYPEs for C++. */
2869 tree
2870 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2871 tsubst_flags_t complain)
2873 tree orig_expr = expr;
2874 tree rval;
2876 if (processing_template_decl)
2878 /* Retain the type if we know the operand is a pointer. */
2879 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2880 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2881 if (type_dependent_expression_p (expr))
2882 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2883 expr = build_non_dependent_expr (expr);
2886 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2887 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2888 if (!rval)
2889 rval = cp_build_indirect_ref (expr, errorstring, complain);
2891 if (processing_template_decl && rval != error_mark_node)
2892 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2893 else
2894 return rval;
2897 /* Helper function called from c-common. */
2898 tree
2899 build_indirect_ref (location_t /*loc*/,
2900 tree ptr, ref_operator errorstring)
2902 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2905 tree
2906 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2907 tsubst_flags_t complain)
2909 tree pointer, type;
2911 if (ptr == current_class_ptr
2912 || (TREE_CODE (ptr) == NOP_EXPR
2913 && TREE_OPERAND (ptr, 0) == current_class_ptr
2914 && (same_type_ignoring_top_level_qualifiers_p
2915 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2916 return current_class_ref;
2918 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2919 ? ptr : decay_conversion (ptr, complain));
2920 if (pointer == error_mark_node)
2921 return error_mark_node;
2923 type = TREE_TYPE (pointer);
2925 if (POINTER_TYPE_P (type))
2927 /* [expr.unary.op]
2929 If the type of the expression is "pointer to T," the type
2930 of the result is "T." */
2931 tree t = TREE_TYPE (type);
2933 if ((CONVERT_EXPR_P (ptr)
2934 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2935 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2937 /* If a warning is issued, mark it to avoid duplicates from
2938 the backend. This only needs to be done at
2939 warn_strict_aliasing > 2. */
2940 if (warn_strict_aliasing > 2)
2941 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2942 type, TREE_OPERAND (ptr, 0)))
2943 TREE_NO_WARNING (ptr) = 1;
2946 if (VOID_TYPE_P (t))
2948 /* A pointer to incomplete type (other than cv void) can be
2949 dereferenced [expr.unary.op]/1 */
2950 if (complain & tf_error)
2951 error ("%qT is not a pointer-to-object type", type);
2952 return error_mark_node;
2954 else if (TREE_CODE (pointer) == ADDR_EXPR
2955 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2956 /* The POINTER was something like `&x'. We simplify `*&x' to
2957 `x'. */
2958 return TREE_OPERAND (pointer, 0);
2959 else
2961 tree ref = build1 (INDIRECT_REF, t, pointer);
2963 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2964 so that we get the proper error message if the result is used
2965 to assign to. Also, &* is supposed to be a no-op. */
2966 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2967 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2968 TREE_SIDE_EFFECTS (ref)
2969 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2970 return ref;
2973 else if (!(complain & tf_error))
2974 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2976 /* `pointer' won't be an error_mark_node if we were given a
2977 pointer to member, so it's cool to check for this here. */
2978 else if (TYPE_PTRMEM_P (type))
2979 switch (errorstring)
2981 case RO_ARRAY_INDEXING:
2982 error ("invalid use of array indexing on pointer to member");
2983 break;
2984 case RO_UNARY_STAR:
2985 error ("invalid use of unary %<*%> on pointer to member");
2986 break;
2987 case RO_IMPLICIT_CONVERSION:
2988 error ("invalid use of implicit conversion on pointer to member");
2989 break;
2990 case RO_ARROW_STAR:
2991 error ("left hand operand of %<->*%> must be a pointer to class, "
2992 "but is a pointer to member of type %qT", type);
2993 break;
2994 default:
2995 gcc_unreachable ();
2997 else if (pointer != error_mark_node)
2998 invalid_indirection_error (input_location, type, errorstring);
3000 return error_mark_node;
3003 /* This handles expressions of the form "a[i]", which denotes
3004 an array reference.
3006 This is logically equivalent in C to *(a+i), but we may do it differently.
3007 If A is a variable or a member, we generate a primitive ARRAY_REF.
3008 This avoids forcing the array out of registers, and can work on
3009 arrays that are not lvalues (for example, members of structures returned
3010 by functions).
3012 If INDEX is of some user-defined type, it must be converted to
3013 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3014 will inherit the type of the array, which will be some pointer type.
3016 LOC is the location to use in building the array reference. */
3018 tree
3019 cp_build_array_ref (location_t loc, tree array, tree idx,
3020 tsubst_flags_t complain)
3022 tree ret;
3024 if (idx == 0)
3026 if (complain & tf_error)
3027 error_at (loc, "subscript missing in array reference");
3028 return error_mark_node;
3031 /* If an array's index is an array notation, then its rank cannot be
3032 greater than one. */
3033 if (flag_cilkplus && contains_array_notation_expr (idx))
3035 size_t rank = 0;
3037 /* If find_rank returns false, then it should have reported an error,
3038 thus it is unnecessary for repetition. */
3039 if (!find_rank (loc, idx, idx, true, &rank))
3040 return error_mark_node;
3041 if (rank > 1)
3043 error_at (loc, "rank of the array%'s index is greater than 1");
3044 return error_mark_node;
3047 if (TREE_TYPE (array) == error_mark_node
3048 || TREE_TYPE (idx) == error_mark_node)
3049 return error_mark_node;
3051 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3052 inside it. */
3053 switch (TREE_CODE (array))
3055 case COMPOUND_EXPR:
3057 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3058 complain);
3059 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3060 TREE_OPERAND (array, 0), value);
3061 SET_EXPR_LOCATION (ret, loc);
3062 return ret;
3065 case COND_EXPR:
3066 ret = build_conditional_expr
3067 (loc, TREE_OPERAND (array, 0),
3068 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3069 complain),
3070 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3071 complain),
3072 complain);
3073 protected_set_expr_location (ret, loc);
3074 return ret;
3076 default:
3077 break;
3080 convert_vector_to_pointer_for_subscript (loc, &array, idx);
3082 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3084 tree rval, type;
3086 warn_array_subscript_with_type_char (idx);
3088 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3090 if (complain & tf_error)
3091 error_at (loc, "array subscript is not an integer");
3092 return error_mark_node;
3095 /* Apply integral promotions *after* noticing character types.
3096 (It is unclear why we do these promotions -- the standard
3097 does not say that we should. In fact, the natural thing would
3098 seem to be to convert IDX to ptrdiff_t; we're performing
3099 pointer arithmetic.) */
3100 idx = cp_perform_integral_promotions (idx, complain);
3102 /* An array that is indexed by a non-constant
3103 cannot be stored in a register; we must be able to do
3104 address arithmetic on its address.
3105 Likewise an array of elements of variable size. */
3106 if (TREE_CODE (idx) != INTEGER_CST
3107 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3108 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3109 != INTEGER_CST)))
3111 if (!cxx_mark_addressable (array))
3112 return error_mark_node;
3115 /* An array that is indexed by a constant value which is not within
3116 the array bounds cannot be stored in a register either; because we
3117 would get a crash in store_bit_field/extract_bit_field when trying
3118 to access a non-existent part of the register. */
3119 if (TREE_CODE (idx) == INTEGER_CST
3120 && TYPE_DOMAIN (TREE_TYPE (array))
3121 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3123 if (!cxx_mark_addressable (array))
3124 return error_mark_node;
3127 if (!lvalue_p (array))
3129 if (complain & tf_error)
3130 pedwarn (loc, OPT_Wpedantic,
3131 "ISO C++ forbids subscripting non-lvalue array");
3132 else
3133 return error_mark_node;
3136 /* Note in C++ it is valid to subscript a `register' array, since
3137 it is valid to take the address of something with that
3138 storage specification. */
3139 if (extra_warnings)
3141 tree foo = array;
3142 while (TREE_CODE (foo) == COMPONENT_REF)
3143 foo = TREE_OPERAND (foo, 0);
3144 if (VAR_P (foo) && DECL_REGISTER (foo)
3145 && (complain & tf_warning))
3146 warning_at (loc, OPT_Wextra,
3147 "subscripting array declared %<register%>");
3150 type = TREE_TYPE (TREE_TYPE (array));
3151 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3152 /* Array ref is const/volatile if the array elements are
3153 or if the array is.. */
3154 TREE_READONLY (rval)
3155 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3156 TREE_SIDE_EFFECTS (rval)
3157 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3158 TREE_THIS_VOLATILE (rval)
3159 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3160 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3161 complain);
3162 protected_set_expr_location (ret, loc);
3163 return ret;
3167 tree ar = cp_default_conversion (array, complain);
3168 tree ind = cp_default_conversion (idx, complain);
3170 /* Put the integer in IND to simplify error checking. */
3171 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3173 tree temp = ar;
3174 ar = ind;
3175 ind = temp;
3178 if (ar == error_mark_node || ind == error_mark_node)
3179 return error_mark_node;
3181 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3183 if (complain & tf_error)
3184 error_at (loc, "subscripted value is neither array nor pointer");
3185 return error_mark_node;
3187 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3189 if (complain & tf_error)
3190 error_at (loc, "array subscript is not an integer");
3191 return error_mark_node;
3194 warn_array_subscript_with_type_char (idx);
3196 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3197 PLUS_EXPR, ar, ind,
3198 complain),
3199 RO_ARRAY_INDEXING,
3200 complain);
3201 protected_set_expr_location (ret, loc);
3202 return ret;
3206 /* Entry point for Obj-C++. */
3208 tree
3209 build_array_ref (location_t loc, tree array, tree idx)
3211 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3214 /* Resolve a pointer to member function. INSTANCE is the object
3215 instance to use, if the member points to a virtual member.
3217 This used to avoid checking for virtual functions if basetype
3218 has no virtual functions, according to an earlier ANSI draft.
3219 With the final ISO C++ rules, such an optimization is
3220 incorrect: A pointer to a derived member can be static_cast
3221 to pointer-to-base-member, as long as the dynamic object
3222 later has the right member. So now we only do this optimization
3223 when we know the dynamic type of the object. */
3225 tree
3226 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3227 tsubst_flags_t complain)
3229 if (TREE_CODE (function) == OFFSET_REF)
3230 function = TREE_OPERAND (function, 1);
3232 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3234 tree idx, delta, e1, e2, e3, vtbl;
3235 bool nonvirtual;
3236 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3237 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3239 tree instance_ptr = *instance_ptrptr;
3240 tree instance_save_expr = 0;
3241 if (instance_ptr == error_mark_node)
3243 if (TREE_CODE (function) == PTRMEM_CST)
3245 /* Extracting the function address from a pmf is only
3246 allowed with -Wno-pmf-conversions. It only works for
3247 pmf constants. */
3248 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3249 e1 = convert (fntype, e1);
3250 return e1;
3252 else
3254 if (complain & tf_error)
3255 error ("object missing in use of %qE", function);
3256 return error_mark_node;
3260 /* True if we know that the dynamic type of the object doesn't have
3261 virtual functions, so we can assume the PFN field is a pointer. */
3262 nonvirtual = (COMPLETE_TYPE_P (basetype)
3263 && !TYPE_POLYMORPHIC_P (basetype)
3264 && resolves_to_fixed_type_p (instance_ptr, 0));
3266 /* If we don't really have an object (i.e. in an ill-formed
3267 conversion from PMF to pointer), we can't resolve virtual
3268 functions anyway. */
3269 if (!nonvirtual && is_dummy_object (instance_ptr))
3270 nonvirtual = true;
3272 if (TREE_SIDE_EFFECTS (instance_ptr))
3273 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3275 if (TREE_SIDE_EFFECTS (function))
3276 function = save_expr (function);
3278 /* Start by extracting all the information from the PMF itself. */
3279 e3 = pfn_from_ptrmemfunc (function);
3280 delta = delta_from_ptrmemfunc (function);
3281 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3282 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3284 case ptrmemfunc_vbit_in_pfn:
3285 e1 = cp_build_binary_op (input_location,
3286 BIT_AND_EXPR, idx, integer_one_node,
3287 complain);
3288 idx = cp_build_binary_op (input_location,
3289 MINUS_EXPR, idx, integer_one_node,
3290 complain);
3291 if (idx == error_mark_node)
3292 return error_mark_node;
3293 break;
3295 case ptrmemfunc_vbit_in_delta:
3296 e1 = cp_build_binary_op (input_location,
3297 BIT_AND_EXPR, delta, integer_one_node,
3298 complain);
3299 delta = cp_build_binary_op (input_location,
3300 RSHIFT_EXPR, delta, integer_one_node,
3301 complain);
3302 if (delta == error_mark_node)
3303 return error_mark_node;
3304 break;
3306 default:
3307 gcc_unreachable ();
3310 if (e1 == error_mark_node)
3311 return error_mark_node;
3313 /* Convert down to the right base before using the instance. A
3314 special case is that in a pointer to member of class C, C may
3315 be incomplete. In that case, the function will of course be
3316 a member of C, and no conversion is required. In fact,
3317 lookup_base will fail in that case, because incomplete
3318 classes do not have BINFOs. */
3319 if (!same_type_ignoring_top_level_qualifiers_p
3320 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3322 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3323 basetype, ba_check, NULL, complain);
3324 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3325 1, complain);
3326 if (instance_ptr == error_mark_node)
3327 return error_mark_node;
3329 /* ...and then the delta in the PMF. */
3330 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3332 /* Hand back the adjusted 'this' argument to our caller. */
3333 *instance_ptrptr = instance_ptr;
3335 if (nonvirtual)
3336 /* Now just return the pointer. */
3337 return e3;
3339 /* Next extract the vtable pointer from the object. */
3340 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3341 instance_ptr);
3342 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3343 if (vtbl == error_mark_node)
3344 return error_mark_node;
3346 /* Finally, extract the function pointer from the vtable. */
3347 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3348 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3349 if (e2 == error_mark_node)
3350 return error_mark_node;
3351 TREE_CONSTANT (e2) = 1;
3353 /* When using function descriptors, the address of the
3354 vtable entry is treated as a function pointer. */
3355 if (TARGET_VTABLE_USES_DESCRIPTORS)
3356 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3357 cp_build_addr_expr (e2, complain));
3359 e2 = fold_convert (TREE_TYPE (e3), e2);
3360 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3361 if (e1 == error_mark_node)
3362 return error_mark_node;
3364 /* Make sure this doesn't get evaluated first inside one of the
3365 branches of the COND_EXPR. */
3366 if (instance_save_expr)
3367 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3368 instance_save_expr, e1);
3370 function = e1;
3372 return function;
3375 /* Used by the C-common bits. */
3376 tree
3377 build_function_call (location_t /*loc*/,
3378 tree function, tree params)
3380 return cp_build_function_call (function, params, tf_warning_or_error);
3383 /* Used by the C-common bits. */
3384 tree
3385 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3386 tree function, vec<tree, va_gc> *params,
3387 vec<tree, va_gc> * /*origtypes*/)
3389 vec<tree, va_gc> *orig_params = params;
3390 tree ret = cp_build_function_call_vec (function, &params,
3391 tf_warning_or_error);
3393 /* cp_build_function_call_vec can reallocate PARAMS by adding
3394 default arguments. That should never happen here. Verify
3395 that. */
3396 gcc_assert (params == orig_params);
3398 return ret;
3401 /* Build a function call using a tree list of arguments. */
3403 tree
3404 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3406 vec<tree, va_gc> *vec;
3407 tree ret;
3409 vec = make_tree_vector ();
3410 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3411 vec_safe_push (vec, TREE_VALUE (params));
3412 ret = cp_build_function_call_vec (function, &vec, complain);
3413 release_tree_vector (vec);
3414 return ret;
3417 /* Build a function call using varargs. */
3419 tree
3420 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3422 vec<tree, va_gc> *vec;
3423 va_list args;
3424 tree ret, t;
3426 vec = make_tree_vector ();
3427 va_start (args, complain);
3428 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3429 vec_safe_push (vec, t);
3430 va_end (args);
3431 ret = cp_build_function_call_vec (function, &vec, complain);
3432 release_tree_vector (vec);
3433 return ret;
3436 /* Build a function call using a vector of arguments. PARAMS may be
3437 NULL if there are no parameters. This changes the contents of
3438 PARAMS. */
3440 tree
3441 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3442 tsubst_flags_t complain)
3444 tree fntype, fndecl;
3445 int is_method;
3446 tree original = function;
3447 int nargs;
3448 tree *argarray;
3449 tree parm_types;
3450 vec<tree, va_gc> *allocated = NULL;
3451 tree ret;
3453 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3454 expressions, like those used for ObjC messenger dispatches. */
3455 if (params != NULL && !vec_safe_is_empty (*params))
3456 function = objc_rewrite_function_call (function, (**params)[0]);
3458 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3459 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3460 if (TREE_CODE (function) == NOP_EXPR
3461 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3462 function = TREE_OPERAND (function, 0);
3464 if (TREE_CODE (function) == FUNCTION_DECL)
3466 mark_used (function);
3467 fndecl = function;
3469 /* Convert anything with function type to a pointer-to-function. */
3470 if (DECL_MAIN_P (function))
3472 if (complain & tf_error)
3473 pedwarn (input_location, OPT_Wpedantic,
3474 "ISO C++ forbids calling %<::main%> from within program");
3475 else
3476 return error_mark_node;
3478 function = build_addr_func (function, complain);
3480 else
3482 fndecl = NULL_TREE;
3484 function = build_addr_func (function, complain);
3487 if (function == error_mark_node)
3488 return error_mark_node;
3490 fntype = TREE_TYPE (function);
3492 if (TYPE_PTRMEMFUNC_P (fntype))
3494 if (complain & tf_error)
3495 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3496 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3497 original, original);
3498 return error_mark_node;
3501 is_method = (TYPE_PTR_P (fntype)
3502 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3504 if (!(TYPE_PTRFN_P (fntype)
3505 || is_method
3506 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3508 if (complain & tf_error)
3510 if (!flag_diagnostics_show_caret)
3511 error_at (input_location,
3512 "%qE cannot be used as a function", original);
3513 else if (DECL_P (original))
3514 error_at (input_location,
3515 "%qD cannot be used as a function", original);
3516 else
3517 error_at (input_location,
3518 "expression cannot be used as a function");
3521 return error_mark_node;
3524 /* fntype now gets the type of function pointed to. */
3525 fntype = TREE_TYPE (fntype);
3526 parm_types = TYPE_ARG_TYPES (fntype);
3528 if (params == NULL)
3530 allocated = make_tree_vector ();
3531 params = &allocated;
3534 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3535 complain);
3536 if (nargs < 0)
3537 return error_mark_node;
3539 argarray = (*params)->address ();
3541 /* Check for errors in format strings and inappropriately
3542 null parameters. */
3543 check_function_arguments (fntype, nargs, argarray);
3545 ret = build_cxx_call (function, nargs, argarray, complain);
3547 if (allocated != NULL)
3548 release_tree_vector (allocated);
3550 return ret;
3553 /* Subroutine of convert_arguments.
3554 Warn about wrong number of args are genereted. */
3556 static void
3557 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3559 if (fndecl)
3561 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3563 if (DECL_NAME (fndecl) == NULL_TREE
3564 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3565 error_at (loc,
3566 too_many_p
3567 ? G_("too many arguments to constructor %q#D")
3568 : G_("too few arguments to constructor %q#D"),
3569 fndecl);
3570 else
3571 error_at (loc,
3572 too_many_p
3573 ? G_("too many arguments to member function %q#D")
3574 : G_("too few arguments to member function %q#D"),
3575 fndecl);
3577 else
3578 error_at (loc,
3579 too_many_p
3580 ? G_("too many arguments to function %q#D")
3581 : G_("too few arguments to function %q#D"),
3582 fndecl);
3583 inform (DECL_SOURCE_LOCATION (fndecl),
3584 "declared here");
3586 else
3588 if (c_dialect_objc () && objc_message_selector ())
3589 error_at (loc,
3590 too_many_p
3591 ? G_("too many arguments to method %q#D")
3592 : G_("too few arguments to method %q#D"),
3593 objc_message_selector ());
3594 else
3595 error_at (loc, too_many_p ? G_("too many arguments to function")
3596 : G_("too few arguments to function"));
3600 /* Convert the actual parameter expressions in the list VALUES to the
3601 types in the list TYPELIST. The converted expressions are stored
3602 back in the VALUES vector.
3603 If parmdecls is exhausted, or when an element has NULL as its type,
3604 perform the default conversions.
3606 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3608 This is also where warnings about wrong number of args are generated.
3610 Returns the actual number of arguments processed (which might be less
3611 than the length of the vector), or -1 on error.
3613 In C++, unspecified trailing parameters can be filled in with their
3614 default arguments, if such were specified. Do so here. */
3616 static int
3617 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3618 int flags, tsubst_flags_t complain)
3620 tree typetail;
3621 unsigned int i;
3623 /* Argument passing is always copy-initialization. */
3624 flags |= LOOKUP_ONLYCONVERTING;
3626 for (i = 0, typetail = typelist;
3627 i < vec_safe_length (*values);
3628 i++)
3630 tree type = typetail ? TREE_VALUE (typetail) : 0;
3631 tree val = (**values)[i];
3633 if (val == error_mark_node || type == error_mark_node)
3634 return -1;
3636 if (type == void_type_node)
3638 if (complain & tf_error)
3640 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3641 return i;
3643 else
3644 return -1;
3647 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3648 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3649 if (TREE_CODE (val) == NOP_EXPR
3650 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3651 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3652 val = TREE_OPERAND (val, 0);
3654 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3656 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3657 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3658 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3659 val = decay_conversion (val, complain);
3662 if (val == error_mark_node)
3663 return -1;
3665 if (type != 0)
3667 /* Formal parm type is specified by a function prototype. */
3668 tree parmval;
3670 if (!COMPLETE_TYPE_P (complete_type (type)))
3672 if (complain & tf_error)
3674 if (fndecl)
3675 error ("parameter %P of %qD has incomplete type %qT",
3676 i, fndecl, type);
3677 else
3678 error ("parameter %P has incomplete type %qT", i, type);
3680 parmval = error_mark_node;
3682 else
3684 parmval = convert_for_initialization
3685 (NULL_TREE, type, val, flags,
3686 ICR_ARGPASS, fndecl, i, complain);
3687 parmval = convert_for_arg_passing (type, parmval, complain);
3690 if (parmval == error_mark_node)
3691 return -1;
3693 (**values)[i] = parmval;
3695 else
3697 if (fndecl && magic_varargs_p (fndecl))
3698 /* Don't do ellipsis conversion for __built_in_constant_p
3699 as this will result in spurious errors for non-trivial
3700 types. */
3701 val = require_complete_type_sfinae (val, complain);
3702 else
3703 val = convert_arg_to_ellipsis (val, complain);
3705 (**values)[i] = val;
3708 if (typetail)
3709 typetail = TREE_CHAIN (typetail);
3712 if (typetail != 0 && typetail != void_list_node)
3714 /* See if there are default arguments that can be used. Because
3715 we hold default arguments in the FUNCTION_TYPE (which is so
3716 wrong), we can see default parameters here from deduced
3717 contexts (and via typeof) for indirect function calls.
3718 Fortunately we know whether we have a function decl to
3719 provide default arguments in a language conformant
3720 manner. */
3721 if (fndecl && TREE_PURPOSE (typetail)
3722 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3724 for (; typetail != void_list_node; ++i)
3726 tree parmval
3727 = convert_default_arg (TREE_VALUE (typetail),
3728 TREE_PURPOSE (typetail),
3729 fndecl, i, complain);
3731 if (parmval == error_mark_node)
3732 return -1;
3734 vec_safe_push (*values, parmval);
3735 typetail = TREE_CHAIN (typetail);
3736 /* ends with `...'. */
3737 if (typetail == NULL_TREE)
3738 break;
3741 else
3743 if (complain & tf_error)
3744 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3745 return -1;
3749 return (int) i;
3752 /* Build a binary-operation expression, after performing default
3753 conversions on the operands. CODE is the kind of expression to
3754 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3755 are the tree codes which correspond to ARG1 and ARG2 when issuing
3756 warnings about possibly misplaced parentheses. They may differ
3757 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3758 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3759 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3760 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3761 ARG2_CODE as ERROR_MARK. */
3763 tree
3764 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3765 enum tree_code arg1_code, tree arg2,
3766 enum tree_code arg2_code, tree *overload,
3767 tsubst_flags_t complain)
3769 tree orig_arg1;
3770 tree orig_arg2;
3771 tree expr;
3773 orig_arg1 = arg1;
3774 orig_arg2 = arg2;
3776 if (processing_template_decl)
3778 if (type_dependent_expression_p (arg1)
3779 || type_dependent_expression_p (arg2))
3780 return build_min_nt_loc (loc, code, arg1, arg2);
3781 arg1 = build_non_dependent_expr (arg1);
3782 arg2 = build_non_dependent_expr (arg2);
3785 if (code == DOTSTAR_EXPR)
3786 expr = build_m_component_ref (arg1, arg2, complain);
3787 else
3788 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3789 overload, complain);
3791 /* Check for cases such as x+y<<z which users are likely to
3792 misinterpret. But don't warn about obj << x + y, since that is a
3793 common idiom for I/O. */
3794 if (warn_parentheses
3795 && (complain & tf_warning)
3796 && !processing_template_decl
3797 && !error_operand_p (arg1)
3798 && !error_operand_p (arg2)
3799 && (code != LSHIFT_EXPR
3800 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3801 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3802 arg2_code, orig_arg2);
3804 if (processing_template_decl && expr != error_mark_node)
3805 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3807 return expr;
3810 /* Build and return an ARRAY_REF expression. */
3812 tree
3813 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3814 tsubst_flags_t complain)
3816 tree orig_arg1 = arg1;
3817 tree orig_arg2 = arg2;
3818 tree expr;
3820 if (processing_template_decl)
3822 if (type_dependent_expression_p (arg1)
3823 || type_dependent_expression_p (arg2))
3824 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3825 NULL_TREE, NULL_TREE);
3826 arg1 = build_non_dependent_expr (arg1);
3827 arg2 = build_non_dependent_expr (arg2);
3830 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3831 NULL_TREE, /*overload=*/NULL, complain);
3833 if (processing_template_decl && expr != error_mark_node)
3834 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3835 NULL_TREE, NULL_TREE);
3836 return expr;
3839 /* Return whether OP is an expression of enum type cast to integer
3840 type. In C++ even unsigned enum types are cast to signed integer
3841 types. We do not want to issue warnings about comparisons between
3842 signed and unsigned types when one of the types is an enum type.
3843 Those warnings are always false positives in practice. */
3845 static bool
3846 enum_cast_to_int (tree op)
3848 if (CONVERT_EXPR_P (op)
3849 && TREE_TYPE (op) == integer_type_node
3850 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3851 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3852 return true;
3854 /* The cast may have been pushed into a COND_EXPR. */
3855 if (TREE_CODE (op) == COND_EXPR)
3856 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3857 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3859 return false;
3862 /* For the c-common bits. */
3863 tree
3864 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3865 int /*convert_p*/)
3867 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3871 /* Build a binary-operation expression without default conversions.
3872 CODE is the kind of expression to build.
3873 LOCATION is the location_t of the operator in the source code.
3874 This function differs from `build' in several ways:
3875 the data type of the result is computed and recorded in it,
3876 warnings are generated if arg data types are invalid,
3877 special handling for addition and subtraction of pointers is known,
3878 and some optimization is done (operations on narrow ints
3879 are done in the narrower type when that gives the same result).
3880 Constant folding is also done before the result is returned.
3882 Note that the operands will never have enumeral types
3883 because either they have just had the default conversions performed
3884 or they have both just been converted to some other type in which
3885 the arithmetic is to be done.
3887 C++: must do special pointer arithmetic when implementing
3888 multiple inheritance, and deal with pointer to member functions. */
3890 tree
3891 cp_build_binary_op (location_t location,
3892 enum tree_code code, tree orig_op0, tree orig_op1,
3893 tsubst_flags_t complain)
3895 tree op0, op1;
3896 enum tree_code code0, code1;
3897 tree type0, type1;
3898 const char *invalid_op_diag;
3900 /* Expression code to give to the expression when it is built.
3901 Normally this is CODE, which is what the caller asked for,
3902 but in some special cases we change it. */
3903 enum tree_code resultcode = code;
3905 /* Data type in which the computation is to be performed.
3906 In the simplest cases this is the common type of the arguments. */
3907 tree result_type = NULL;
3909 /* Nonzero means operands have already been type-converted
3910 in whatever way is necessary.
3911 Zero means they need to be converted to RESULT_TYPE. */
3912 int converted = 0;
3914 /* Nonzero means create the expression with this type, rather than
3915 RESULT_TYPE. */
3916 tree build_type = 0;
3918 /* Nonzero means after finally constructing the expression
3919 convert it to this type. */
3920 tree final_type = 0;
3922 tree result;
3923 tree orig_type = NULL;
3925 /* Nonzero if this is an operation like MIN or MAX which can
3926 safely be computed in short if both args are promoted shorts.
3927 Also implies COMMON.
3928 -1 indicates a bitwise operation; this makes a difference
3929 in the exact conditions for when it is safe to do the operation
3930 in a narrower mode. */
3931 int shorten = 0;
3933 /* Nonzero if this is a comparison operation;
3934 if both args are promoted shorts, compare the original shorts.
3935 Also implies COMMON. */
3936 int short_compare = 0;
3938 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3939 int common = 0;
3941 /* True if both operands have arithmetic type. */
3942 bool arithmetic_types_p;
3944 /* Apply default conversions. */
3945 op0 = orig_op0;
3946 op1 = orig_op1;
3948 /* Remember whether we're doing / or %. */
3949 bool doing_div_or_mod = false;
3951 /* Remember whether we're doing << or >>. */
3952 bool doing_shift = false;
3954 /* Tree holding instrumentation expression. */
3955 tree instrument_expr = NULL;
3957 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3958 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3959 || code == TRUTH_XOR_EXPR)
3961 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3962 op0 = decay_conversion (op0, complain);
3963 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3964 op1 = decay_conversion (op1, complain);
3966 else
3968 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3969 op0 = cp_default_conversion (op0, complain);
3970 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3971 op1 = cp_default_conversion (op1, complain);
3974 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3975 STRIP_TYPE_NOPS (op0);
3976 STRIP_TYPE_NOPS (op1);
3978 /* DTRT if one side is an overloaded function, but complain about it. */
3979 if (type_unknown_p (op0))
3981 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3982 if (t != error_mark_node)
3984 if (complain & tf_error)
3985 permerror (input_location, "assuming cast to type %qT from overloaded function",
3986 TREE_TYPE (t));
3987 op0 = t;
3990 if (type_unknown_p (op1))
3992 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3993 if (t != error_mark_node)
3995 if (complain & tf_error)
3996 permerror (input_location, "assuming cast to type %qT from overloaded function",
3997 TREE_TYPE (t));
3998 op1 = t;
4002 type0 = TREE_TYPE (op0);
4003 type1 = TREE_TYPE (op1);
4005 /* The expression codes of the data types of the arguments tell us
4006 whether the arguments are integers, floating, pointers, etc. */
4007 code0 = TREE_CODE (type0);
4008 code1 = TREE_CODE (type1);
4010 /* If an error was already reported for one of the arguments,
4011 avoid reporting another error. */
4012 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4013 return error_mark_node;
4015 if ((invalid_op_diag
4016 = targetm.invalid_binary_op (code, type0, type1)))
4018 if (complain & tf_error)
4019 error (invalid_op_diag);
4020 return error_mark_node;
4023 /* Issue warnings about peculiar, but valid, uses of NULL. */
4024 if ((orig_op0 == null_node || orig_op1 == null_node)
4025 /* It's reasonable to use pointer values as operands of &&
4026 and ||, so NULL is no exception. */
4027 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4028 && ( /* Both are NULL (or 0) and the operation was not a
4029 comparison or a pointer subtraction. */
4030 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4031 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4032 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4033 || (!null_ptr_cst_p (orig_op0)
4034 && !TYPE_PTR_OR_PTRMEM_P (type0))
4035 || (!null_ptr_cst_p (orig_op1)
4036 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4037 && (complain & tf_warning))
4039 source_location loc =
4040 expansion_point_location_if_in_system_header (input_location);
4042 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4045 /* In case when one of the operands of the binary operation is
4046 a vector and another is a scalar -- convert scalar to vector. */
4047 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4049 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4050 complain & tf_error);
4052 switch (convert_flag)
4054 case stv_error:
4055 return error_mark_node;
4056 case stv_firstarg:
4058 op0 = convert (TREE_TYPE (type1), op0);
4059 op0 = save_expr (op0);
4060 op0 = build_vector_from_val (type1, op0);
4061 type0 = TREE_TYPE (op0);
4062 code0 = TREE_CODE (type0);
4063 converted = 1;
4064 break;
4066 case stv_secondarg:
4068 op1 = convert (TREE_TYPE (type0), op1);
4069 op1 = save_expr (op1);
4070 op1 = build_vector_from_val (type0, op1);
4071 type1 = TREE_TYPE (op1);
4072 code1 = TREE_CODE (type1);
4073 converted = 1;
4074 break;
4076 default:
4077 break;
4081 switch (code)
4083 case MINUS_EXPR:
4084 /* Subtraction of two similar pointers.
4085 We must subtract them as integers, then divide by object size. */
4086 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4087 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4088 TREE_TYPE (type1)))
4089 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4090 complain);
4091 /* In all other cases except pointer - int, the usual arithmetic
4092 rules apply. */
4093 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4095 common = 1;
4096 break;
4098 /* The pointer - int case is just like pointer + int; fall
4099 through. */
4100 case PLUS_EXPR:
4101 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4102 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4104 tree ptr_operand;
4105 tree int_operand;
4106 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4107 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4108 if (processing_template_decl)
4110 result_type = TREE_TYPE (ptr_operand);
4111 break;
4113 return cp_pointer_int_sum (code,
4114 ptr_operand,
4115 int_operand,
4116 complain);
4118 common = 1;
4119 break;
4121 case MULT_EXPR:
4122 common = 1;
4123 break;
4125 case TRUNC_DIV_EXPR:
4126 case CEIL_DIV_EXPR:
4127 case FLOOR_DIV_EXPR:
4128 case ROUND_DIV_EXPR:
4129 case EXACT_DIV_EXPR:
4130 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4131 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4132 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4133 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4135 enum tree_code tcode0 = code0, tcode1 = code1;
4136 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4137 cop1 = maybe_constant_value (cop1);
4138 doing_div_or_mod = true;
4139 warn_for_div_by_zero (location, cop1);
4141 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4142 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4143 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4144 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4146 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4147 resultcode = RDIV_EXPR;
4148 else
4149 /* When dividing two signed integers, we have to promote to int.
4150 unless we divide by a constant != -1. Note that default
4151 conversion will have been performed on the operands at this
4152 point, so we have to dig out the original type to find out if
4153 it was unsigned. */
4154 shorten = ((TREE_CODE (op0) == NOP_EXPR
4155 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4156 || (TREE_CODE (op1) == INTEGER_CST
4157 && ! integer_all_onesp (op1)));
4159 common = 1;
4161 break;
4163 case BIT_AND_EXPR:
4164 case BIT_IOR_EXPR:
4165 case BIT_XOR_EXPR:
4166 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4167 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4168 && !VECTOR_FLOAT_TYPE_P (type0)
4169 && !VECTOR_FLOAT_TYPE_P (type1)))
4170 shorten = -1;
4171 break;
4173 case TRUNC_MOD_EXPR:
4174 case FLOOR_MOD_EXPR:
4176 tree cop1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4177 cop1 = maybe_constant_value (cop1);
4178 doing_div_or_mod = true;
4179 warn_for_div_by_zero (location, cop1);
4182 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4183 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4184 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4185 common = 1;
4186 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4188 /* Although it would be tempting to shorten always here, that loses
4189 on some targets, since the modulo instruction is undefined if the
4190 quotient can't be represented in the computation mode. We shorten
4191 only if unsigned or if dividing by something we know != -1. */
4192 shorten = ((TREE_CODE (op0) == NOP_EXPR
4193 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4194 || (TREE_CODE (op1) == INTEGER_CST
4195 && ! integer_all_onesp (op1)));
4196 common = 1;
4198 break;
4200 case TRUTH_ANDIF_EXPR:
4201 case TRUTH_ORIF_EXPR:
4202 case TRUTH_AND_EXPR:
4203 case TRUTH_OR_EXPR:
4204 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4206 if (!COMPARISON_CLASS_P (op1))
4207 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4208 build_zero_cst (type1), complain);
4209 if (code == TRUTH_ANDIF_EXPR)
4211 tree z = build_zero_cst (TREE_TYPE (op1));
4212 return build_conditional_expr (location, op0, op1, z, complain);
4214 else if (code == TRUTH_ORIF_EXPR)
4216 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4217 return build_conditional_expr (location, op0, m1, op1, complain);
4219 else
4220 gcc_unreachable ();
4222 if (VECTOR_TYPE_P (type0))
4224 if (!COMPARISON_CLASS_P (op0))
4225 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4226 build_zero_cst (type0), complain);
4227 if (!VECTOR_TYPE_P (type1))
4229 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4230 tree z = build_zero_cst (TREE_TYPE (op0));
4231 op1 = build_conditional_expr (location, op1, z, m1, complain);
4233 else if (!COMPARISON_CLASS_P (op1))
4234 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4235 build_zero_cst (type1), complain);
4237 if (code == TRUTH_ANDIF_EXPR)
4238 code = BIT_AND_EXPR;
4239 else if (code == TRUTH_ORIF_EXPR)
4240 code = BIT_IOR_EXPR;
4241 else
4242 gcc_unreachable ();
4244 return cp_build_binary_op (location, code, op0, op1, complain);
4247 result_type = boolean_type_node;
4248 break;
4250 /* Shift operations: result has same type as first operand;
4251 always convert second operand to int.
4252 Also set SHORT_SHIFT if shifting rightward. */
4254 case RSHIFT_EXPR:
4255 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4256 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4258 result_type = type0;
4259 converted = 1;
4261 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4262 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4263 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4264 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4266 result_type = type0;
4267 converted = 1;
4269 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4271 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4272 const_op1 = maybe_constant_value (const_op1);
4273 if (TREE_CODE (const_op1) != INTEGER_CST)
4274 const_op1 = op1;
4275 result_type = type0;
4276 doing_shift = true;
4277 if (TREE_CODE (const_op1) == INTEGER_CST)
4279 if (tree_int_cst_lt (const_op1, integer_zero_node))
4281 if ((complain & tf_warning)
4282 && c_inhibit_evaluation_warnings == 0)
4283 warning (0, "right shift count is negative");
4285 else
4287 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4288 && (complain & tf_warning)
4289 && c_inhibit_evaluation_warnings == 0)
4290 warning (0, "right shift count >= width of type");
4293 /* Convert the shift-count to an integer, regardless of
4294 size of value being shifted. */
4295 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4296 op1 = cp_convert (integer_type_node, op1, complain);
4297 /* Avoid converting op1 to result_type later. */
4298 converted = 1;
4300 break;
4302 case LSHIFT_EXPR:
4303 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4304 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4306 result_type = type0;
4307 converted = 1;
4309 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4310 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4311 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4312 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4314 result_type = type0;
4315 converted = 1;
4317 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4319 tree const_op1 = fold_non_dependent_expr_sfinae (op1, tf_none);
4320 const_op1 = maybe_constant_value (const_op1);
4321 if (TREE_CODE (const_op1) != INTEGER_CST)
4322 const_op1 = op1;
4323 result_type = type0;
4324 doing_shift = true;
4325 if (TREE_CODE (const_op1) == INTEGER_CST)
4327 if (tree_int_cst_lt (const_op1, integer_zero_node))
4329 if ((complain & tf_warning)
4330 && c_inhibit_evaluation_warnings == 0)
4331 warning (0, "left shift count is negative");
4333 else if (compare_tree_int (const_op1,
4334 TYPE_PRECISION (type0)) >= 0)
4336 if ((complain & tf_warning)
4337 && c_inhibit_evaluation_warnings == 0)
4338 warning (0, "left shift count >= width of type");
4341 /* Convert the shift-count to an integer, regardless of
4342 size of value being shifted. */
4343 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4344 op1 = cp_convert (integer_type_node, op1, complain);
4345 /* Avoid converting op1 to result_type later. */
4346 converted = 1;
4348 break;
4350 case RROTATE_EXPR:
4351 case LROTATE_EXPR:
4352 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4354 result_type = type0;
4355 if (TREE_CODE (op1) == INTEGER_CST)
4357 if (tree_int_cst_lt (op1, integer_zero_node))
4359 if (complain & tf_warning)
4360 warning (0, (code == LROTATE_EXPR)
4361 ? G_("left rotate count is negative")
4362 : G_("right rotate count is negative"));
4364 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4366 if (complain & tf_warning)
4367 warning (0, (code == LROTATE_EXPR)
4368 ? G_("left rotate count >= width of type")
4369 : G_("right rotate count >= width of type"));
4372 /* Convert the shift-count to an integer, regardless of
4373 size of value being shifted. */
4374 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4375 op1 = cp_convert (integer_type_node, op1, complain);
4377 break;
4379 case EQ_EXPR:
4380 case NE_EXPR:
4381 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4382 goto vector_compare;
4383 if ((complain & tf_warning)
4384 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4385 warning (OPT_Wfloat_equal,
4386 "comparing floating point with == or != is unsafe");
4387 if ((complain & tf_warning)
4388 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4389 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4390 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4392 build_type = boolean_type_node;
4393 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4394 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4395 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4396 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4397 short_compare = 1;
4398 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4399 && null_ptr_cst_p (op1))
4400 /* Handle, eg, (void*)0 (c++/43906), and more. */
4401 || (code0 == POINTER_TYPE
4402 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4404 if (TYPE_PTR_P (type1))
4405 result_type = composite_pointer_type (type0, type1, op0, op1,
4406 CPO_COMPARISON, complain);
4407 else
4408 result_type = type0;
4410 if (TREE_CODE (op0) == ADDR_EXPR
4411 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4413 if ((complain & tf_warning)
4414 && c_inhibit_evaluation_warnings == 0)
4415 warning (OPT_Waddress, "the address of %qD will never be NULL",
4416 TREE_OPERAND (op0, 0));
4419 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4420 && null_ptr_cst_p (op0))
4421 /* Handle, eg, (void*)0 (c++/43906), and more. */
4422 || (code1 == POINTER_TYPE
4423 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4425 if (TYPE_PTR_P (type0))
4426 result_type = composite_pointer_type (type0, type1, op0, op1,
4427 CPO_COMPARISON, complain);
4428 else
4429 result_type = type1;
4431 if (TREE_CODE (op1) == ADDR_EXPR
4432 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4434 if ((complain & tf_warning)
4435 && c_inhibit_evaluation_warnings == 0)
4436 warning (OPT_Waddress, "the address of %qD will never be NULL",
4437 TREE_OPERAND (op1, 0));
4440 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4441 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4442 result_type = composite_pointer_type (type0, type1, op0, op1,
4443 CPO_COMPARISON, complain);
4444 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4445 /* One of the operands must be of nullptr_t type. */
4446 result_type = TREE_TYPE (nullptr_node);
4447 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4449 result_type = type0;
4450 if (complain & tf_error)
4451 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4452 else
4453 return error_mark_node;
4455 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4457 result_type = type1;
4458 if (complain & tf_error)
4459 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4460 else
4461 return error_mark_node;
4463 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4465 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4466 == ptrmemfunc_vbit_in_delta)
4468 tree pfn0, delta0, e1, e2;
4470 if (TREE_SIDE_EFFECTS (op0))
4471 op0 = save_expr (op0);
4473 pfn0 = pfn_from_ptrmemfunc (op0);
4474 delta0 = delta_from_ptrmemfunc (op0);
4475 e1 = cp_build_binary_op (location,
4476 EQ_EXPR,
4477 pfn0,
4478 build_zero_cst (TREE_TYPE (pfn0)),
4479 complain);
4480 e2 = cp_build_binary_op (location,
4481 BIT_AND_EXPR,
4482 delta0,
4483 integer_one_node,
4484 complain);
4486 if (complain & tf_warning)
4487 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4489 e2 = cp_build_binary_op (location,
4490 EQ_EXPR, e2, integer_zero_node,
4491 complain);
4492 op0 = cp_build_binary_op (location,
4493 TRUTH_ANDIF_EXPR, e1, e2,
4494 complain);
4495 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4497 else
4499 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4500 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4502 result_type = TREE_TYPE (op0);
4504 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4505 return cp_build_binary_op (location, code, op1, op0, complain);
4506 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4508 tree type;
4509 /* E will be the final comparison. */
4510 tree e;
4511 /* E1 and E2 are for scratch. */
4512 tree e1;
4513 tree e2;
4514 tree pfn0;
4515 tree pfn1;
4516 tree delta0;
4517 tree delta1;
4519 type = composite_pointer_type (type0, type1, op0, op1,
4520 CPO_COMPARISON, complain);
4522 if (!same_type_p (TREE_TYPE (op0), type))
4523 op0 = cp_convert_and_check (type, op0, complain);
4524 if (!same_type_p (TREE_TYPE (op1), type))
4525 op1 = cp_convert_and_check (type, op1, complain);
4527 if (op0 == error_mark_node || op1 == error_mark_node)
4528 return error_mark_node;
4530 if (TREE_SIDE_EFFECTS (op0))
4531 op0 = save_expr (op0);
4532 if (TREE_SIDE_EFFECTS (op1))
4533 op1 = save_expr (op1);
4535 pfn0 = pfn_from_ptrmemfunc (op0);
4536 pfn1 = pfn_from_ptrmemfunc (op1);
4537 delta0 = delta_from_ptrmemfunc (op0);
4538 delta1 = delta_from_ptrmemfunc (op1);
4539 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4540 == ptrmemfunc_vbit_in_delta)
4542 /* We generate:
4544 (op0.pfn == op1.pfn
4545 && ((op0.delta == op1.delta)
4546 || (!op0.pfn && op0.delta & 1 == 0
4547 && op1.delta & 1 == 0))
4549 The reason for the `!op0.pfn' bit is that a NULL
4550 pointer-to-member is any member with a zero PFN and
4551 LSB of the DELTA field is 0. */
4553 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4554 delta0,
4555 integer_one_node,
4556 complain);
4557 e1 = cp_build_binary_op (location,
4558 EQ_EXPR, e1, integer_zero_node,
4559 complain);
4560 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4561 delta1,
4562 integer_one_node,
4563 complain);
4564 e2 = cp_build_binary_op (location,
4565 EQ_EXPR, e2, integer_zero_node,
4566 complain);
4567 e1 = cp_build_binary_op (location,
4568 TRUTH_ANDIF_EXPR, e2, e1,
4569 complain);
4570 e2 = cp_build_binary_op (location, EQ_EXPR,
4571 pfn0,
4572 build_zero_cst (TREE_TYPE (pfn0)),
4573 complain);
4574 e2 = cp_build_binary_op (location,
4575 TRUTH_ANDIF_EXPR, e2, e1, complain);
4576 e1 = cp_build_binary_op (location,
4577 EQ_EXPR, delta0, delta1, complain);
4578 e1 = cp_build_binary_op (location,
4579 TRUTH_ORIF_EXPR, e1, e2, complain);
4581 else
4583 /* We generate:
4585 (op0.pfn == op1.pfn
4586 && (!op0.pfn || op0.delta == op1.delta))
4588 The reason for the `!op0.pfn' bit is that a NULL
4589 pointer-to-member is any member with a zero PFN; the
4590 DELTA field is unspecified. */
4592 e1 = cp_build_binary_op (location,
4593 EQ_EXPR, delta0, delta1, complain);
4594 e2 = cp_build_binary_op (location,
4595 EQ_EXPR,
4596 pfn0,
4597 build_zero_cst (TREE_TYPE (pfn0)),
4598 complain);
4599 e1 = cp_build_binary_op (location,
4600 TRUTH_ORIF_EXPR, e1, e2, complain);
4602 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4603 e = cp_build_binary_op (location,
4604 TRUTH_ANDIF_EXPR, e2, e1, complain);
4605 if (code == EQ_EXPR)
4606 return e;
4607 return cp_build_binary_op (location,
4608 EQ_EXPR, e, integer_zero_node, complain);
4610 else
4612 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4613 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4614 type1));
4615 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4616 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4617 type0));
4620 break;
4622 case MAX_EXPR:
4623 case MIN_EXPR:
4624 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4625 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4626 shorten = 1;
4627 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4628 result_type = composite_pointer_type (type0, type1, op0, op1,
4629 CPO_COMPARISON, complain);
4630 break;
4632 case LE_EXPR:
4633 case GE_EXPR:
4634 case LT_EXPR:
4635 case GT_EXPR:
4636 if (TREE_CODE (orig_op0) == STRING_CST
4637 || TREE_CODE (orig_op1) == STRING_CST)
4639 if (complain & tf_warning)
4640 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4643 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4645 vector_compare:
4646 tree intt;
4647 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4648 TREE_TYPE (type1))
4649 && !vector_types_compatible_elements_p (type0, type1))
4651 if (complain & tf_error)
4653 error_at (location, "comparing vectors with different "
4654 "element types");
4655 inform (location, "operand types are %qT and %qT",
4656 type0, type1);
4658 return error_mark_node;
4661 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4663 if (complain & tf_error)
4665 error_at (location, "comparing vectors with different "
4666 "number of elements");
4667 inform (location, "operand types are %qT and %qT",
4668 type0, type1);
4670 return error_mark_node;
4673 /* Always construct signed integer vector type. */
4674 intt = c_common_type_for_size (GET_MODE_BITSIZE
4675 (TYPE_MODE (TREE_TYPE (type0))), 0);
4676 if (!intt)
4678 if (complain & tf_error)
4679 error_at (location, "could not find an integer type "
4680 "of the same size as %qT", TREE_TYPE (type0));
4681 return error_mark_node;
4683 result_type = build_opaque_vector_type (intt,
4684 TYPE_VECTOR_SUBPARTS (type0));
4685 converted = 1;
4686 break;
4688 build_type = boolean_type_node;
4689 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4690 || code0 == ENUMERAL_TYPE)
4691 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4692 || code1 == ENUMERAL_TYPE))
4693 short_compare = 1;
4694 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4695 result_type = composite_pointer_type (type0, type1, op0, op1,
4696 CPO_COMPARISON, complain);
4697 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4699 result_type = type0;
4700 if (extra_warnings && (complain & tf_warning))
4701 warning (OPT_Wextra,
4702 "ordered comparison of pointer with integer zero");
4704 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4706 result_type = type1;
4707 if (extra_warnings && (complain & tf_warning))
4708 warning (OPT_Wextra,
4709 "ordered comparison of pointer with integer zero");
4711 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4712 /* One of the operands must be of nullptr_t type. */
4713 result_type = TREE_TYPE (nullptr_node);
4714 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4716 result_type = type0;
4717 if (complain & tf_error)
4718 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4719 else
4720 return error_mark_node;
4722 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4724 result_type = type1;
4725 if (complain & tf_error)
4726 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4727 else
4728 return error_mark_node;
4730 break;
4732 case UNORDERED_EXPR:
4733 case ORDERED_EXPR:
4734 case UNLT_EXPR:
4735 case UNLE_EXPR:
4736 case UNGT_EXPR:
4737 case UNGE_EXPR:
4738 case UNEQ_EXPR:
4739 build_type = integer_type_node;
4740 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4742 if (complain & tf_error)
4743 error ("unordered comparison on non-floating point argument");
4744 return error_mark_node;
4746 common = 1;
4747 break;
4749 default:
4750 break;
4753 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4754 || code0 == ENUMERAL_TYPE)
4755 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4756 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4757 arithmetic_types_p = 1;
4758 else
4760 arithmetic_types_p = 0;
4761 /* Vector arithmetic is only allowed when both sides are vectors. */
4762 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4764 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4765 || !vector_types_compatible_elements_p (type0, type1))
4767 if (complain & tf_error)
4768 binary_op_error (location, code, type0, type1);
4769 return error_mark_node;
4771 arithmetic_types_p = 1;
4774 /* Determine the RESULT_TYPE, if it is not already known. */
4775 if (!result_type
4776 && arithmetic_types_p
4777 && (shorten || common || short_compare))
4779 result_type = cp_common_type (type0, type1);
4780 if (complain & tf_warning)
4781 do_warn_double_promotion (result_type, type0, type1,
4782 "implicit conversion from %qT to %qT "
4783 "to match other operand of binary "
4784 "expression",
4785 location);
4788 if (!result_type)
4790 if (complain & tf_error)
4791 error ("invalid operands of types %qT and %qT to binary %qO",
4792 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4793 return error_mark_node;
4796 /* If we're in a template, the only thing we need to know is the
4797 RESULT_TYPE. */
4798 if (processing_template_decl)
4800 /* Since the middle-end checks the type when doing a build2, we
4801 need to build the tree in pieces. This built tree will never
4802 get out of the front-end as we replace it when instantiating
4803 the template. */
4804 tree tmp = build2 (resultcode,
4805 build_type ? build_type : result_type,
4806 NULL_TREE, op1);
4807 TREE_OPERAND (tmp, 0) = op0;
4808 return tmp;
4811 if (arithmetic_types_p)
4813 bool first_complex = (code0 == COMPLEX_TYPE);
4814 bool second_complex = (code1 == COMPLEX_TYPE);
4815 int none_complex = (!first_complex && !second_complex);
4817 /* Adapted from patch for c/24581. */
4818 if (first_complex != second_complex
4819 && (code == PLUS_EXPR
4820 || code == MINUS_EXPR
4821 || code == MULT_EXPR
4822 || (code == TRUNC_DIV_EXPR && first_complex))
4823 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4824 && flag_signed_zeros)
4826 /* An operation on mixed real/complex operands must be
4827 handled specially, but the language-independent code can
4828 more easily optimize the plain complex arithmetic if
4829 -fno-signed-zeros. */
4830 tree real_type = TREE_TYPE (result_type);
4831 tree real, imag;
4832 if (first_complex)
4834 if (TREE_TYPE (op0) != result_type)
4835 op0 = cp_convert_and_check (result_type, op0, complain);
4836 if (TREE_TYPE (op1) != real_type)
4837 op1 = cp_convert_and_check (real_type, op1, complain);
4839 else
4841 if (TREE_TYPE (op0) != real_type)
4842 op0 = cp_convert_and_check (real_type, op0, complain);
4843 if (TREE_TYPE (op1) != result_type)
4844 op1 = cp_convert_and_check (result_type, op1, complain);
4846 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4847 return error_mark_node;
4848 if (first_complex)
4850 op0 = save_expr (op0);
4851 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4852 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4853 switch (code)
4855 case MULT_EXPR:
4856 case TRUNC_DIV_EXPR:
4857 op1 = save_expr (op1);
4858 imag = build2 (resultcode, real_type, imag, op1);
4859 /* Fall through. */
4860 case PLUS_EXPR:
4861 case MINUS_EXPR:
4862 real = build2 (resultcode, real_type, real, op1);
4863 break;
4864 default:
4865 gcc_unreachable();
4868 else
4870 op1 = save_expr (op1);
4871 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4872 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4873 switch (code)
4875 case MULT_EXPR:
4876 op0 = save_expr (op0);
4877 imag = build2 (resultcode, real_type, op0, imag);
4878 /* Fall through. */
4879 case PLUS_EXPR:
4880 real = build2 (resultcode, real_type, op0, real);
4881 break;
4882 case MINUS_EXPR:
4883 real = build2 (resultcode, real_type, op0, real);
4884 imag = build1 (NEGATE_EXPR, real_type, imag);
4885 break;
4886 default:
4887 gcc_unreachable();
4890 real = fold_if_not_in_template (real);
4891 imag = fold_if_not_in_template (imag);
4892 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4893 result = fold_if_not_in_template (result);
4894 return result;
4897 /* For certain operations (which identify themselves by shorten != 0)
4898 if both args were extended from the same smaller type,
4899 do the arithmetic in that type and then extend.
4901 shorten !=0 and !=1 indicates a bitwise operation.
4902 For them, this optimization is safe only if
4903 both args are zero-extended or both are sign-extended.
4904 Otherwise, we might change the result.
4905 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4906 but calculated in (unsigned short) it would be (unsigned short)-1. */
4908 if (shorten && none_complex)
4910 orig_type = result_type;
4911 final_type = result_type;
4912 result_type = shorten_binary_op (result_type, op0, op1,
4913 shorten == -1);
4916 /* Comparison operations are shortened too but differently.
4917 They identify themselves by setting short_compare = 1. */
4919 if (short_compare)
4921 /* Don't write &op0, etc., because that would prevent op0
4922 from being kept in a register.
4923 Instead, make copies of the our local variables and
4924 pass the copies by reference, then copy them back afterward. */
4925 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4926 enum tree_code xresultcode = resultcode;
4927 tree val
4928 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4929 &xresultcode);
4930 if (val != 0)
4931 return cp_convert (boolean_type_node, val, complain);
4932 op0 = xop0, op1 = xop1;
4933 converted = 1;
4934 resultcode = xresultcode;
4937 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4938 && warn_sign_compare
4939 /* Do not warn until the template is instantiated; we cannot
4940 bound the ranges of the arguments until that point. */
4941 && !processing_template_decl
4942 && (complain & tf_warning)
4943 && c_inhibit_evaluation_warnings == 0
4944 /* Even unsigned enum types promote to signed int. We don't
4945 want to issue -Wsign-compare warnings for this case. */
4946 && !enum_cast_to_int (orig_op0)
4947 && !enum_cast_to_int (orig_op1))
4949 tree oop0 = maybe_constant_value (orig_op0);
4950 tree oop1 = maybe_constant_value (orig_op1);
4952 if (TREE_CODE (oop0) != INTEGER_CST)
4953 oop0 = orig_op0;
4954 if (TREE_CODE (oop1) != INTEGER_CST)
4955 oop1 = orig_op1;
4956 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4957 result_type, resultcode);
4961 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4962 Then the expression will be built.
4963 It will be given type FINAL_TYPE if that is nonzero;
4964 otherwise, it will be given type RESULT_TYPE. */
4965 if (! converted)
4967 if (TREE_TYPE (op0) != result_type)
4968 op0 = cp_convert_and_check (result_type, op0, complain);
4969 if (TREE_TYPE (op1) != result_type)
4970 op1 = cp_convert_and_check (result_type, op1, complain);
4972 if (op0 == error_mark_node || op1 == error_mark_node)
4973 return error_mark_node;
4976 if (build_type == NULL_TREE)
4977 build_type = result_type;
4979 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
4980 | SANITIZE_FLOAT_DIVIDE))
4981 && !processing_template_decl
4982 && current_function_decl != 0
4983 && !lookup_attribute ("no_sanitize_undefined",
4984 DECL_ATTRIBUTES (current_function_decl))
4985 && (doing_div_or_mod || doing_shift))
4987 /* OP0 and/or OP1 might have side-effects. */
4988 op0 = cp_save_expr (op0);
4989 op1 = cp_save_expr (op1);
4990 op0 = maybe_constant_value (fold_non_dependent_expr_sfinae (op0,
4991 tf_none));
4992 op1 = maybe_constant_value (fold_non_dependent_expr_sfinae (op1,
4993 tf_none));
4994 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
4995 | SANITIZE_FLOAT_DIVIDE)))
4997 /* For diagnostics we want to use the promoted types without
4998 shorten_binary_op. So convert the arguments to the
4999 original result_type. */
5000 tree cop0 = op0;
5001 tree cop1 = op1;
5002 if (orig_type != NULL && result_type != orig_type)
5004 cop0 = cp_convert (orig_type, op0, complain);
5005 cop1 = cp_convert (orig_type, op1, complain);
5007 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5009 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5010 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5013 result = build2 (resultcode, build_type, op0, op1);
5014 result = fold_if_not_in_template (result);
5015 if (final_type != 0)
5016 result = cp_convert (final_type, result, complain);
5018 if (TREE_OVERFLOW_P (result)
5019 && !TREE_OVERFLOW_P (op0)
5020 && !TREE_OVERFLOW_P (op1))
5021 overflow_warning (location, result);
5023 if (instrument_expr != NULL)
5024 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
5025 instrument_expr, result);
5027 return result;
5030 /* Build a VEC_PERM_EXPR.
5031 This is a simple wrapper for c_build_vec_perm_expr. */
5032 tree
5033 build_x_vec_perm_expr (location_t loc,
5034 tree arg0, tree arg1, tree arg2,
5035 tsubst_flags_t complain)
5037 tree orig_arg0 = arg0;
5038 tree orig_arg1 = arg1;
5039 tree orig_arg2 = arg2;
5040 if (processing_template_decl)
5042 if (type_dependent_expression_p (arg0)
5043 || type_dependent_expression_p (arg1)
5044 || type_dependent_expression_p (arg2))
5045 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5046 arg0 = build_non_dependent_expr (arg0);
5047 if (arg1)
5048 arg1 = build_non_dependent_expr (arg1);
5049 arg2 = build_non_dependent_expr (arg2);
5051 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5052 if (processing_template_decl && exp != error_mark_node)
5053 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5054 orig_arg1, orig_arg2);
5055 return exp;
5058 /* Return a tree for the sum or difference (RESULTCODE says which)
5059 of pointer PTROP and integer INTOP. */
5061 static tree
5062 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5063 tsubst_flags_t complain)
5065 tree res_type = TREE_TYPE (ptrop);
5067 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5068 in certain circumstance (when it's valid to do so). So we need
5069 to make sure it's complete. We don't need to check here, if we
5070 can actually complete it at all, as those checks will be done in
5071 pointer_int_sum() anyway. */
5072 complete_type (TREE_TYPE (res_type));
5074 return pointer_int_sum (input_location, resultcode, ptrop,
5075 fold_if_not_in_template (intop),
5076 complain & tf_warning_or_error);
5079 /* Return a tree for the difference of pointers OP0 and OP1.
5080 The resulting tree has type int. */
5082 static tree
5083 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5085 tree result;
5086 tree restype = ptrdiff_type_node;
5087 tree target_type = TREE_TYPE (ptrtype);
5089 if (!complete_type_or_else (target_type, NULL_TREE))
5090 return error_mark_node;
5092 if (VOID_TYPE_P (target_type))
5094 if (complain & tf_error)
5095 permerror (input_location, "ISO C++ forbids using pointer of "
5096 "type %<void *%> in subtraction");
5097 else
5098 return error_mark_node;
5100 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5102 if (complain & tf_error)
5103 permerror (input_location, "ISO C++ forbids using pointer to "
5104 "a function in subtraction");
5105 else
5106 return error_mark_node;
5108 if (TREE_CODE (target_type) == METHOD_TYPE)
5110 if (complain & tf_error)
5111 permerror (input_location, "ISO C++ forbids using pointer to "
5112 "a method in subtraction");
5113 else
5114 return error_mark_node;
5117 /* First do the subtraction as integers;
5118 then drop through to build the divide operator. */
5120 op0 = cp_build_binary_op (input_location,
5121 MINUS_EXPR,
5122 cp_convert (restype, op0, complain),
5123 cp_convert (restype, op1, complain),
5124 complain);
5126 /* This generates an error if op1 is a pointer to an incomplete type. */
5127 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5129 if (complain & tf_error)
5130 error ("invalid use of a pointer to an incomplete type in "
5131 "pointer arithmetic");
5132 else
5133 return error_mark_node;
5136 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5138 if (complain & tf_error)
5139 error ("arithmetic on pointer to an empty aggregate");
5140 else
5141 return error_mark_node;
5144 op1 = (TYPE_PTROB_P (ptrtype)
5145 ? size_in_bytes (target_type)
5146 : integer_one_node);
5148 /* Do the division. */
5150 result = build2 (EXACT_DIV_EXPR, restype, op0,
5151 cp_convert (restype, op1, complain));
5152 return fold_if_not_in_template (result);
5155 /* Construct and perhaps optimize a tree representation
5156 for a unary operation. CODE, a tree_code, specifies the operation
5157 and XARG is the operand. */
5159 tree
5160 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5161 tsubst_flags_t complain)
5163 tree orig_expr = xarg;
5164 tree exp;
5165 int ptrmem = 0;
5167 if (processing_template_decl)
5169 if (type_dependent_expression_p (xarg))
5170 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5172 xarg = build_non_dependent_expr (xarg);
5175 exp = NULL_TREE;
5177 /* [expr.unary.op] says:
5179 The address of an object of incomplete type can be taken.
5181 (And is just the ordinary address operator, not an overloaded
5182 "operator &".) However, if the type is a template
5183 specialization, we must complete the type at this point so that
5184 an overloaded "operator &" will be available if required. */
5185 if (code == ADDR_EXPR
5186 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5187 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5188 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5189 || (TREE_CODE (xarg) == OFFSET_REF)))
5190 /* Don't look for a function. */;
5191 else
5192 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5193 NULL_TREE, /*overload=*/NULL, complain);
5194 if (!exp && code == ADDR_EXPR)
5196 if (is_overloaded_fn (xarg))
5198 tree fn = get_first_fn (xarg);
5199 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5201 if (complain & tf_error)
5202 error (DECL_CONSTRUCTOR_P (fn)
5203 ? G_("taking address of constructor %qE")
5204 : G_("taking address of destructor %qE"),
5205 xarg);
5206 return error_mark_node;
5210 /* A pointer to member-function can be formed only by saying
5211 &X::mf. */
5212 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5213 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5215 if (TREE_CODE (xarg) != OFFSET_REF
5216 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5218 if (complain & tf_error)
5220 error ("invalid use of %qE to form a "
5221 "pointer-to-member-function", xarg);
5222 if (TREE_CODE (xarg) != OFFSET_REF)
5223 inform (input_location, " a qualified-id is required");
5225 return error_mark_node;
5227 else
5229 if (complain & tf_error)
5230 error ("parentheses around %qE cannot be used to form a"
5231 " pointer-to-member-function",
5232 xarg);
5233 else
5234 return error_mark_node;
5235 PTRMEM_OK_P (xarg) = 1;
5239 if (TREE_CODE (xarg) == OFFSET_REF)
5241 ptrmem = PTRMEM_OK_P (xarg);
5243 if (!ptrmem && !flag_ms_extensions
5244 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5246 /* A single non-static member, make sure we don't allow a
5247 pointer-to-member. */
5248 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5249 TREE_OPERAND (xarg, 0),
5250 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5251 PTRMEM_OK_P (xarg) = ptrmem;
5255 exp = cp_build_addr_expr_strict (xarg, complain);
5258 if (processing_template_decl && exp != error_mark_node)
5259 exp = build_min_non_dep (code, exp, orig_expr,
5260 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5261 if (TREE_CODE (exp) == ADDR_EXPR)
5262 PTRMEM_OK_P (exp) = ptrmem;
5263 return exp;
5266 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5267 constants, where a null value is represented by an INTEGER_CST of
5268 -1. */
5270 tree
5271 cp_truthvalue_conversion (tree expr)
5273 tree type = TREE_TYPE (expr);
5274 if (TYPE_PTRDATAMEM_P (type)
5275 /* Avoid ICE on invalid use of non-static member function. */
5276 || TREE_CODE (expr) == FUNCTION_DECL)
5277 return build_binary_op (EXPR_LOCATION (expr),
5278 NE_EXPR, expr, nullptr_node, 1);
5279 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5281 /* With -Wzero-as-null-pointer-constant do not warn for an
5282 'if (p)' or a 'while (!p)', where p is a pointer. */
5283 tree ret;
5284 ++c_inhibit_evaluation_warnings;
5285 ret = c_common_truthvalue_conversion (input_location, expr);
5286 --c_inhibit_evaluation_warnings;
5287 return ret;
5289 else
5290 return c_common_truthvalue_conversion (input_location, expr);
5293 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5295 tree
5296 condition_conversion (tree expr)
5298 tree t;
5299 if (processing_template_decl)
5300 return expr;
5301 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5302 tf_warning_or_error, LOOKUP_NORMAL);
5303 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5304 return t;
5307 /* Returns the address of T. This function will fold away
5308 ADDR_EXPR of INDIRECT_REF. */
5310 tree
5311 build_address (tree t)
5313 if (error_operand_p (t) || !cxx_mark_addressable (t))
5314 return error_mark_node;
5315 t = build_fold_addr_expr (t);
5316 if (TREE_CODE (t) != ADDR_EXPR)
5317 t = rvalue (t);
5318 return t;
5321 /* Returns the address of T with type TYPE. */
5323 tree
5324 build_typed_address (tree t, tree type)
5326 if (error_operand_p (t) || !cxx_mark_addressable (t))
5327 return error_mark_node;
5328 t = build_fold_addr_expr_with_type (t, type);
5329 if (TREE_CODE (t) != ADDR_EXPR)
5330 t = rvalue (t);
5331 return t;
5334 /* Return a NOP_EXPR converting EXPR to TYPE. */
5336 tree
5337 build_nop (tree type, tree expr)
5339 if (type == error_mark_node || error_operand_p (expr))
5340 return expr;
5341 return build1 (NOP_EXPR, type, expr);
5344 /* Take the address of ARG, whatever that means under C++ semantics.
5345 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5346 and class rvalues as well.
5348 Nothing should call this function directly; instead, callers should use
5349 cp_build_addr_expr or cp_build_addr_expr_strict. */
5351 static tree
5352 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5354 tree argtype;
5355 tree val;
5357 if (!arg || error_operand_p (arg))
5358 return error_mark_node;
5360 arg = mark_lvalue_use (arg);
5361 argtype = lvalue_type (arg);
5363 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5365 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5366 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5368 /* They're trying to take the address of a unique non-static
5369 member function. This is ill-formed (except in MS-land),
5370 but let's try to DTRT.
5371 Note: We only handle unique functions here because we don't
5372 want to complain if there's a static overload; non-unique
5373 cases will be handled by instantiate_type. But we need to
5374 handle this case here to allow casts on the resulting PMF.
5375 We could defer this in non-MS mode, but it's easier to give
5376 a useful error here. */
5378 /* Inside constant member functions, the `this' pointer
5379 contains an extra const qualifier. TYPE_MAIN_VARIANT
5380 is used here to remove this const from the diagnostics
5381 and the created OFFSET_REF. */
5382 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5383 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5384 mark_used (fn);
5386 if (! flag_ms_extensions)
5388 tree name = DECL_NAME (fn);
5389 if (!(complain & tf_error))
5390 return error_mark_node;
5391 else if (current_class_type
5392 && TREE_OPERAND (arg, 0) == current_class_ref)
5393 /* An expression like &memfn. */
5394 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5395 " or parenthesized non-static member function to form"
5396 " a pointer to member function. Say %<&%T::%D%>",
5397 base, name);
5398 else
5399 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5400 " function to form a pointer to member function."
5401 " Say %<&%T::%D%>",
5402 base, name);
5404 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5407 /* Uninstantiated types are all functions. Taking the
5408 address of a function is a no-op, so just return the
5409 argument. */
5410 if (type_unknown_p (arg))
5411 return build1 (ADDR_EXPR, unknown_type_node, arg);
5413 if (TREE_CODE (arg) == OFFSET_REF)
5414 /* We want a pointer to member; bypass all the code for actually taking
5415 the address of something. */
5416 goto offset_ref;
5418 /* Anything not already handled and not a true memory reference
5419 is an error. */
5420 if (TREE_CODE (argtype) != FUNCTION_TYPE
5421 && TREE_CODE (argtype) != METHOD_TYPE)
5423 cp_lvalue_kind kind = lvalue_kind (arg);
5424 if (kind == clk_none)
5426 if (complain & tf_error)
5427 lvalue_error (input_location, lv_addressof);
5428 return error_mark_node;
5430 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5432 if (!(complain & tf_error))
5433 return error_mark_node;
5434 if (kind & clk_class)
5435 /* Make this a permerror because we used to accept it. */
5436 permerror (input_location, "taking address of temporary");
5437 else
5438 error ("taking address of xvalue (rvalue reference)");
5442 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5444 tree type = build_pointer_type (TREE_TYPE (argtype));
5445 arg = build1 (CONVERT_EXPR, type, arg);
5446 return arg;
5448 else if (pedantic && DECL_MAIN_P (arg))
5450 /* ARM $3.4 */
5451 /* Apparently a lot of autoconf scripts for C++ packages do this,
5452 so only complain if -Wpedantic. */
5453 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5454 pedwarn (input_location, OPT_Wpedantic,
5455 "ISO C++ forbids taking address of function %<::main%>");
5456 else if (flag_pedantic_errors)
5457 return error_mark_node;
5460 /* Let &* cancel out to simplify resulting code. */
5461 if (INDIRECT_REF_P (arg))
5463 /* We don't need to have `current_class_ptr' wrapped in a
5464 NON_LVALUE_EXPR node. */
5465 if (arg == current_class_ref)
5466 return current_class_ptr;
5468 arg = TREE_OPERAND (arg, 0);
5469 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5471 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5472 arg = build1 (CONVERT_EXPR, type, arg);
5474 else
5475 /* Don't let this be an lvalue. */
5476 arg = rvalue (arg);
5477 return arg;
5480 /* ??? Cope with user tricks that amount to offsetof. */
5481 if (TREE_CODE (argtype) != FUNCTION_TYPE
5482 && TREE_CODE (argtype) != METHOD_TYPE
5483 && argtype != unknown_type_node
5484 && (val = get_base_address (arg))
5485 && COMPLETE_TYPE_P (TREE_TYPE (val))
5486 && INDIRECT_REF_P (val)
5487 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5489 tree type = build_pointer_type (argtype);
5490 return fold_convert (type, fold_offsetof_1 (arg));
5493 /* Handle complex lvalues (when permitted)
5494 by reduction to simpler cases. */
5495 val = unary_complex_lvalue (ADDR_EXPR, arg);
5496 if (val != 0)
5497 return val;
5499 switch (TREE_CODE (arg))
5501 CASE_CONVERT:
5502 case FLOAT_EXPR:
5503 case FIX_TRUNC_EXPR:
5504 /* Even if we're not being pedantic, we cannot allow this
5505 extension when we're instantiating in a SFINAE
5506 context. */
5507 if (! lvalue_p (arg) && complain == tf_none)
5509 if (complain & tf_error)
5510 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5511 else
5512 return error_mark_node;
5514 break;
5516 case BASELINK:
5517 arg = BASELINK_FUNCTIONS (arg);
5518 /* Fall through. */
5520 case OVERLOAD:
5521 arg = OVL_CURRENT (arg);
5522 break;
5524 case OFFSET_REF:
5525 offset_ref:
5526 /* Turn a reference to a non-static data member into a
5527 pointer-to-member. */
5529 tree type;
5530 tree t;
5532 gcc_assert (PTRMEM_OK_P (arg));
5534 t = TREE_OPERAND (arg, 1);
5535 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5537 if (complain & tf_error)
5538 error ("cannot create pointer to reference member %qD", t);
5539 return error_mark_node;
5542 type = build_ptrmem_type (context_for_name_lookup (t),
5543 TREE_TYPE (t));
5544 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5545 return t;
5548 default:
5549 break;
5552 if (argtype != error_mark_node)
5554 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (argtype)
5555 && (flag_iso || warn_vla > 0))
5557 if (complain & tf_warning_or_error)
5558 pedwarn (input_location, OPT_Wvla,
5559 "taking address of array of runtime bound");
5560 else
5561 return error_mark_node;
5563 argtype = build_pointer_type (argtype);
5566 /* In a template, we are processing a non-dependent expression
5567 so we can just form an ADDR_EXPR with the correct type. */
5568 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5570 val = build_address (arg);
5571 if (TREE_CODE (arg) == OFFSET_REF)
5572 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5574 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5576 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5578 /* We can only get here with a single static member
5579 function. */
5580 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5581 && DECL_STATIC_FUNCTION_P (fn));
5582 mark_used (fn);
5583 val = build_address (fn);
5584 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5585 /* Do not lose object's side effects. */
5586 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5587 TREE_OPERAND (arg, 0), val);
5589 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5591 if (complain & tf_error)
5592 error ("attempt to take address of bit-field structure member %qD",
5593 TREE_OPERAND (arg, 1));
5594 return error_mark_node;
5596 else
5598 tree object = TREE_OPERAND (arg, 0);
5599 tree field = TREE_OPERAND (arg, 1);
5600 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5601 (TREE_TYPE (object), decl_type_context (field)));
5602 val = build_address (arg);
5605 if (TYPE_PTR_P (argtype)
5606 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5608 build_ptrmemfunc_type (argtype);
5609 val = build_ptrmemfunc (argtype, val, 0,
5610 /*c_cast_p=*/false,
5611 complain);
5614 return val;
5617 /* Take the address of ARG if it has one, even if it's an rvalue. */
5619 tree
5620 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5622 return cp_build_addr_expr_1 (arg, 0, complain);
5625 /* Take the address of ARG, but only if it's an lvalue. */
5627 tree
5628 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5630 return cp_build_addr_expr_1 (arg, 1, complain);
5633 /* C++: Must handle pointers to members.
5635 Perhaps type instantiation should be extended to handle conversion
5636 from aggregates to types we don't yet know we want? (Or are those
5637 cases typically errors which should be reported?)
5639 NOCONVERT nonzero suppresses the default promotions
5640 (such as from short to int). */
5642 tree
5643 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5644 tsubst_flags_t complain)
5646 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5647 tree arg = xarg;
5648 tree argtype = 0;
5649 const char *errstring = NULL;
5650 tree val;
5651 const char *invalid_op_diag;
5653 if (!arg || error_operand_p (arg))
5654 return error_mark_node;
5656 if ((invalid_op_diag
5657 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5658 ? CONVERT_EXPR
5659 : code),
5660 TREE_TYPE (xarg))))
5662 if (complain & tf_error)
5663 error (invalid_op_diag);
5664 return error_mark_node;
5667 switch (code)
5669 case UNARY_PLUS_EXPR:
5670 case NEGATE_EXPR:
5672 int flags = WANT_ARITH | WANT_ENUM;
5673 /* Unary plus (but not unary minus) is allowed on pointers. */
5674 if (code == UNARY_PLUS_EXPR)
5675 flags |= WANT_POINTER;
5676 arg = build_expr_type_conversion (flags, arg, true);
5677 if (!arg)
5678 errstring = (code == NEGATE_EXPR
5679 ? _("wrong type argument to unary minus")
5680 : _("wrong type argument to unary plus"));
5681 else
5683 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5684 arg = cp_perform_integral_promotions (arg, complain);
5686 /* Make sure the result is not an lvalue: a unary plus or minus
5687 expression is always a rvalue. */
5688 arg = rvalue (arg);
5691 break;
5693 case BIT_NOT_EXPR:
5694 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5696 code = CONJ_EXPR;
5697 if (!noconvert)
5699 arg = cp_default_conversion (arg, complain);
5700 if (arg == error_mark_node)
5701 return error_mark_node;
5704 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5705 | WANT_VECTOR_OR_COMPLEX,
5706 arg, true)))
5707 errstring = _("wrong type argument to bit-complement");
5708 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5709 arg = cp_perform_integral_promotions (arg, complain);
5710 break;
5712 case ABS_EXPR:
5713 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5714 errstring = _("wrong type argument to abs");
5715 else if (!noconvert)
5717 arg = cp_default_conversion (arg, complain);
5718 if (arg == error_mark_node)
5719 return error_mark_node;
5721 break;
5723 case CONJ_EXPR:
5724 /* Conjugating a real value is a no-op, but allow it anyway. */
5725 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5726 errstring = _("wrong type argument to conjugation");
5727 else if (!noconvert)
5729 arg = cp_default_conversion (arg, complain);
5730 if (arg == error_mark_node)
5731 return error_mark_node;
5733 break;
5735 case TRUTH_NOT_EXPR:
5736 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5737 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5738 build_zero_cst (TREE_TYPE (arg)), complain);
5739 arg = perform_implicit_conversion (boolean_type_node, arg,
5740 complain);
5741 val = invert_truthvalue_loc (input_location, arg);
5742 if (arg != error_mark_node)
5743 return val;
5744 errstring = _("in argument to unary !");
5745 break;
5747 case NOP_EXPR:
5748 break;
5750 case REALPART_EXPR:
5751 case IMAGPART_EXPR:
5752 arg = build_real_imag_expr (input_location, code, arg);
5753 if (arg == error_mark_node)
5754 return arg;
5755 else
5756 return fold_if_not_in_template (arg);
5758 case PREINCREMENT_EXPR:
5759 case POSTINCREMENT_EXPR:
5760 case PREDECREMENT_EXPR:
5761 case POSTDECREMENT_EXPR:
5762 /* Handle complex lvalues (when permitted)
5763 by reduction to simpler cases. */
5765 val = unary_complex_lvalue (code, arg);
5766 if (val != 0)
5767 return val;
5769 arg = mark_lvalue_use (arg);
5771 /* Increment or decrement the real part of the value,
5772 and don't change the imaginary part. */
5773 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5775 tree real, imag;
5777 arg = stabilize_reference (arg);
5778 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5779 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5780 real = cp_build_unary_op (code, real, 1, complain);
5781 if (real == error_mark_node || imag == error_mark_node)
5782 return error_mark_node;
5783 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5784 real, imag);
5787 /* Report invalid types. */
5789 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5790 arg, true)))
5792 if (code == PREINCREMENT_EXPR)
5793 errstring = _("no pre-increment operator for type");
5794 else if (code == POSTINCREMENT_EXPR)
5795 errstring = _("no post-increment operator for type");
5796 else if (code == PREDECREMENT_EXPR)
5797 errstring = _("no pre-decrement operator for type");
5798 else
5799 errstring = _("no post-decrement operator for type");
5800 break;
5802 else if (arg == error_mark_node)
5803 return error_mark_node;
5805 /* Report something read-only. */
5807 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5808 || TREE_READONLY (arg))
5810 if (complain & tf_error)
5811 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5812 || code == POSTINCREMENT_EXPR)
5813 ? lv_increment : lv_decrement));
5814 else
5815 return error_mark_node;
5819 tree inc;
5820 tree declared_type = unlowered_expr_type (arg);
5822 argtype = TREE_TYPE (arg);
5824 /* ARM $5.2.5 last annotation says this should be forbidden. */
5825 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5827 if (complain & tf_error)
5828 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5829 ? G_("ISO C++ forbids incrementing an enum")
5830 : G_("ISO C++ forbids decrementing an enum"));
5831 else
5832 return error_mark_node;
5835 /* Compute the increment. */
5837 if (TYPE_PTR_P (argtype))
5839 tree type = complete_type (TREE_TYPE (argtype));
5841 if (!COMPLETE_OR_VOID_TYPE_P (type))
5843 if (complain & tf_error)
5844 error (((code == PREINCREMENT_EXPR
5845 || code == POSTINCREMENT_EXPR))
5846 ? G_("cannot increment a pointer to incomplete type %qT")
5847 : G_("cannot decrement a pointer to incomplete type %qT"),
5848 TREE_TYPE (argtype));
5849 else
5850 return error_mark_node;
5852 else if (!TYPE_PTROB_P (argtype))
5854 if (complain & tf_error)
5855 pedwarn (input_location, OPT_Wpointer_arith,
5856 (code == PREINCREMENT_EXPR
5857 || code == POSTINCREMENT_EXPR)
5858 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5859 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5860 argtype);
5861 else
5862 return error_mark_node;
5865 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5867 else
5868 inc = VECTOR_TYPE_P (argtype)
5869 ? build_one_cst (argtype)
5870 : integer_one_node;
5872 inc = cp_convert (argtype, inc, complain);
5874 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5875 need to ask Objective-C to build the increment or decrement
5876 expression for it. */
5877 if (objc_is_property_ref (arg))
5878 return objc_build_incr_expr_for_property_ref (input_location, code,
5879 arg, inc);
5881 /* Complain about anything else that is not a true lvalue. */
5882 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5883 || code == POSTINCREMENT_EXPR)
5884 ? lv_increment : lv_decrement),
5885 complain))
5886 return error_mark_node;
5888 /* Forbid using -- on `bool'. */
5889 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5891 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5893 if (complain & tf_error)
5894 error ("invalid use of Boolean expression as operand "
5895 "to %<operator--%>");
5896 return error_mark_node;
5898 val = boolean_increment (code, arg);
5900 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5901 /* An rvalue has no cv-qualifiers. */
5902 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5903 else
5904 val = build2 (code, TREE_TYPE (arg), arg, inc);
5906 TREE_SIDE_EFFECTS (val) = 1;
5907 return val;
5910 case ADDR_EXPR:
5911 /* Note that this operation never does default_conversion
5912 regardless of NOCONVERT. */
5913 return cp_build_addr_expr (arg, complain);
5915 default:
5916 break;
5919 if (!errstring)
5921 if (argtype == 0)
5922 argtype = TREE_TYPE (arg);
5923 return fold_if_not_in_template (build1 (code, argtype, arg));
5926 if (complain & tf_error)
5927 error ("%s", errstring);
5928 return error_mark_node;
5931 /* Hook for the c-common bits that build a unary op. */
5932 tree
5933 build_unary_op (location_t /*location*/,
5934 enum tree_code code, tree xarg, int noconvert)
5936 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5939 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5940 for certain kinds of expressions which are not really lvalues
5941 but which we can accept as lvalues.
5943 If ARG is not a kind of expression we can handle, return
5944 NULL_TREE. */
5946 tree
5947 unary_complex_lvalue (enum tree_code code, tree arg)
5949 /* Inside a template, making these kinds of adjustments is
5950 pointless; we are only concerned with the type of the
5951 expression. */
5952 if (processing_template_decl)
5953 return NULL_TREE;
5955 /* Handle (a, b) used as an "lvalue". */
5956 if (TREE_CODE (arg) == COMPOUND_EXPR)
5958 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5959 tf_warning_or_error);
5960 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5961 TREE_OPERAND (arg, 0), real_result);
5964 /* Handle (a ? b : c) used as an "lvalue". */
5965 if (TREE_CODE (arg) == COND_EXPR
5966 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5967 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5969 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5970 if (TREE_CODE (arg) == MODIFY_EXPR
5971 || TREE_CODE (arg) == PREINCREMENT_EXPR
5972 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5974 tree lvalue = TREE_OPERAND (arg, 0);
5975 if (TREE_SIDE_EFFECTS (lvalue))
5977 lvalue = stabilize_reference (lvalue);
5978 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5979 lvalue, TREE_OPERAND (arg, 1));
5981 return unary_complex_lvalue
5982 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5985 if (code != ADDR_EXPR)
5986 return NULL_TREE;
5988 /* Handle (a = b) used as an "lvalue" for `&'. */
5989 if (TREE_CODE (arg) == MODIFY_EXPR
5990 || TREE_CODE (arg) == INIT_EXPR)
5992 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5993 tf_warning_or_error);
5994 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5995 arg, real_result);
5996 TREE_NO_WARNING (arg) = 1;
5997 return arg;
6000 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6001 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6002 || TREE_CODE (arg) == OFFSET_REF)
6003 return NULL_TREE;
6005 /* We permit compiler to make function calls returning
6006 objects of aggregate type look like lvalues. */
6008 tree targ = arg;
6010 if (TREE_CODE (targ) == SAVE_EXPR)
6011 targ = TREE_OPERAND (targ, 0);
6013 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6015 if (TREE_CODE (arg) == SAVE_EXPR)
6016 targ = arg;
6017 else
6018 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6019 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6022 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6023 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6024 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6027 /* Don't let anything else be handled specially. */
6028 return NULL_TREE;
6031 /* Mark EXP saying that we need to be able to take the
6032 address of it; it should not be allocated in a register.
6033 Value is true if successful.
6035 C++: we do not allow `current_class_ptr' to be addressable. */
6037 bool
6038 cxx_mark_addressable (tree exp)
6040 tree x = exp;
6042 while (1)
6043 switch (TREE_CODE (x))
6045 case ADDR_EXPR:
6046 case COMPONENT_REF:
6047 case ARRAY_REF:
6048 case REALPART_EXPR:
6049 case IMAGPART_EXPR:
6050 x = TREE_OPERAND (x, 0);
6051 break;
6053 case PARM_DECL:
6054 if (x == current_class_ptr)
6056 error ("cannot take the address of %<this%>, which is an rvalue expression");
6057 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6058 return true;
6060 /* Fall through. */
6062 case VAR_DECL:
6063 /* Caller should not be trying to mark initialized
6064 constant fields addressable. */
6065 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6066 || DECL_IN_AGGR_P (x) == 0
6067 || TREE_STATIC (x)
6068 || DECL_EXTERNAL (x));
6069 /* Fall through. */
6071 case RESULT_DECL:
6072 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6073 && !DECL_ARTIFICIAL (x))
6075 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6077 error
6078 ("address of explicit register variable %qD requested", x);
6079 return false;
6081 else if (extra_warnings)
6082 warning
6083 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6085 TREE_ADDRESSABLE (x) = 1;
6086 return true;
6088 case CONST_DECL:
6089 case FUNCTION_DECL:
6090 TREE_ADDRESSABLE (x) = 1;
6091 return true;
6093 case CONSTRUCTOR:
6094 TREE_ADDRESSABLE (x) = 1;
6095 return true;
6097 case TARGET_EXPR:
6098 TREE_ADDRESSABLE (x) = 1;
6099 cxx_mark_addressable (TREE_OPERAND (x, 0));
6100 return true;
6102 default:
6103 return true;
6107 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6109 tree
6110 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6111 tsubst_flags_t complain)
6113 tree orig_ifexp = ifexp;
6114 tree orig_op1 = op1;
6115 tree orig_op2 = op2;
6116 tree expr;
6118 if (processing_template_decl)
6120 /* The standard says that the expression is type-dependent if
6121 IFEXP is type-dependent, even though the eventual type of the
6122 expression doesn't dependent on IFEXP. */
6123 if (type_dependent_expression_p (ifexp)
6124 /* As a GNU extension, the middle operand may be omitted. */
6125 || (op1 && type_dependent_expression_p (op1))
6126 || type_dependent_expression_p (op2))
6127 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6128 ifexp = build_non_dependent_expr (ifexp);
6129 if (op1)
6130 op1 = build_non_dependent_expr (op1);
6131 op2 = build_non_dependent_expr (op2);
6134 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6135 if (processing_template_decl && expr != error_mark_node
6136 && TREE_CODE (expr) != VEC_COND_EXPR)
6138 tree min = build_min_non_dep (COND_EXPR, expr,
6139 orig_ifexp, orig_op1, orig_op2);
6140 /* In C++11, remember that the result is an lvalue or xvalue.
6141 In C++98, lvalue_kind can just assume lvalue in a template. */
6142 if (cxx_dialect >= cxx11
6143 && lvalue_or_rvalue_with_address_p (expr)
6144 && !lvalue_or_rvalue_with_address_p (min))
6145 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6146 !real_lvalue_p (expr));
6147 expr = convert_from_reference (min);
6149 return expr;
6152 /* Given a list of expressions, return a compound expression
6153 that performs them all and returns the value of the last of them. */
6155 tree
6156 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6157 tsubst_flags_t complain)
6159 tree expr = TREE_VALUE (list);
6161 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6162 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6164 if (complain & tf_error)
6165 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6166 "list-initializer for non-class type must not "
6167 "be parenthesized");
6168 else
6169 return error_mark_node;
6172 if (TREE_CHAIN (list))
6174 if (complain & tf_error)
6175 switch (exp)
6177 case ELK_INIT:
6178 permerror (input_location, "expression list treated as compound "
6179 "expression in initializer");
6180 break;
6181 case ELK_MEM_INIT:
6182 permerror (input_location, "expression list treated as compound "
6183 "expression in mem-initializer");
6184 break;
6185 case ELK_FUNC_CAST:
6186 permerror (input_location, "expression list treated as compound "
6187 "expression in functional cast");
6188 break;
6189 default:
6190 gcc_unreachable ();
6192 else
6193 return error_mark_node;
6195 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6196 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6197 expr, TREE_VALUE (list), complain);
6200 return expr;
6203 /* Like build_x_compound_expr_from_list, but using a VEC. */
6205 tree
6206 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6207 tsubst_flags_t complain)
6209 if (vec_safe_is_empty (vec))
6210 return NULL_TREE;
6211 else if (vec->length () == 1)
6212 return (*vec)[0];
6213 else
6215 tree expr;
6216 unsigned int ix;
6217 tree t;
6219 if (msg != NULL)
6221 if (complain & tf_error)
6222 permerror (input_location,
6223 "%s expression list treated as compound expression",
6224 msg);
6225 else
6226 return error_mark_node;
6229 expr = (*vec)[0];
6230 for (ix = 1; vec->iterate (ix, &t); ++ix)
6231 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6232 t, complain);
6234 return expr;
6238 /* Handle overloading of the ',' operator when needed. */
6240 tree
6241 build_x_compound_expr (location_t loc, tree op1, tree op2,
6242 tsubst_flags_t complain)
6244 tree result;
6245 tree orig_op1 = op1;
6246 tree orig_op2 = op2;
6248 if (processing_template_decl)
6250 if (type_dependent_expression_p (op1)
6251 || type_dependent_expression_p (op2))
6252 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6253 op1 = build_non_dependent_expr (op1);
6254 op2 = build_non_dependent_expr (op2);
6257 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6258 NULL_TREE, /*overload=*/NULL, complain);
6259 if (!result)
6260 result = cp_build_compound_expr (op1, op2, complain);
6262 if (processing_template_decl && result != error_mark_node)
6263 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6265 return result;
6268 /* Like cp_build_compound_expr, but for the c-common bits. */
6270 tree
6271 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6273 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6276 /* Build a compound expression. */
6278 tree
6279 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6281 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6283 if (lhs == error_mark_node || rhs == error_mark_node)
6284 return error_mark_node;
6286 if (flag_cilkplus
6287 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6288 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6290 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6291 : EXPR_LOCATION (rhs));
6292 error_at (loc,
6293 "spawned function call cannot be part of a comma expression");
6294 return error_mark_node;
6297 if (TREE_CODE (rhs) == TARGET_EXPR)
6299 /* If the rhs is a TARGET_EXPR, then build the compound
6300 expression inside the target_expr's initializer. This
6301 helps the compiler to eliminate unnecessary temporaries. */
6302 tree init = TREE_OPERAND (rhs, 1);
6304 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6305 TREE_OPERAND (rhs, 1) = init;
6307 return rhs;
6310 if (type_unknown_p (rhs))
6312 if (complain & tf_error)
6313 error ("no context to resolve type of %qE", rhs);
6314 return error_mark_node;
6317 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6320 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6321 casts away constness. CAST gives the type of cast. Returns true
6322 if the cast is ill-formed, false if it is well-formed.
6324 ??? This function warns for casting away any qualifier not just
6325 const. We would like to specify exactly what qualifiers are casted
6326 away.
6329 static bool
6330 check_for_casting_away_constness (tree src_type, tree dest_type,
6331 enum tree_code cast, tsubst_flags_t complain)
6333 /* C-style casts are allowed to cast away constness. With
6334 WARN_CAST_QUAL, we still want to issue a warning. */
6335 if (cast == CAST_EXPR && !warn_cast_qual)
6336 return false;
6338 if (!casts_away_constness (src_type, dest_type, complain))
6339 return false;
6341 switch (cast)
6343 case CAST_EXPR:
6344 if (complain & tf_warning)
6345 warning (OPT_Wcast_qual,
6346 "cast from type %qT to type %qT casts away qualifiers",
6347 src_type, dest_type);
6348 return false;
6350 case STATIC_CAST_EXPR:
6351 if (complain & tf_error)
6352 error ("static_cast from type %qT to type %qT casts away qualifiers",
6353 src_type, dest_type);
6354 return true;
6356 case REINTERPRET_CAST_EXPR:
6357 if (complain & tf_error)
6358 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6359 src_type, dest_type);
6360 return true;
6362 default:
6363 gcc_unreachable();
6368 Warns if the cast from expression EXPR to type TYPE is useless.
6370 void
6371 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6373 if (warn_useless_cast
6374 && complain & tf_warning)
6376 /* In C++14 mode, this interacts badly with force_paren_expr. And it
6377 isn't necessary in any mode, because the code below handles
6378 glvalues properly. For 4.9, just skip it in C++14 mode. */
6379 if (cxx_dialect < cxx14 && REFERENCE_REF_P (expr))
6380 expr = TREE_OPERAND (expr, 0);
6382 if ((TREE_CODE (type) == REFERENCE_TYPE
6383 && (TYPE_REF_IS_RVALUE (type)
6384 ? xvalue_p (expr) : real_lvalue_p (expr))
6385 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6386 || same_type_p (TREE_TYPE (expr), type))
6387 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6391 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6392 (another pointer-to-member type in the same hierarchy) and return
6393 the converted expression. If ALLOW_INVERSE_P is permitted, a
6394 pointer-to-derived may be converted to pointer-to-base; otherwise,
6395 only the other direction is permitted. If C_CAST_P is true, this
6396 conversion is taking place as part of a C-style cast. */
6398 tree
6399 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6400 bool c_cast_p, tsubst_flags_t complain)
6402 if (TYPE_PTRDATAMEM_P (type))
6404 tree delta;
6406 if (TREE_CODE (expr) == PTRMEM_CST)
6407 expr = cplus_expand_constant (expr);
6408 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6409 TYPE_PTRMEM_CLASS_TYPE (type),
6410 allow_inverse_p,
6411 c_cast_p, complain);
6412 if (delta == error_mark_node)
6413 return error_mark_node;
6415 if (!integer_zerop (delta))
6417 tree cond, op1, op2;
6419 cond = cp_build_binary_op (input_location,
6420 EQ_EXPR,
6421 expr,
6422 build_int_cst (TREE_TYPE (expr), -1),
6423 complain);
6424 op1 = build_nop (ptrdiff_type_node, expr);
6425 op2 = cp_build_binary_op (input_location,
6426 PLUS_EXPR, op1, delta,
6427 complain);
6429 expr = fold_build3_loc (input_location,
6430 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6434 return build_nop (type, expr);
6436 else
6437 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6438 allow_inverse_p, c_cast_p, complain);
6441 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6442 this static_cast is being attempted as one of the possible casts
6443 allowed by a C-style cast. (In that case, accessibility of base
6444 classes is not considered, and it is OK to cast away
6445 constness.) Return the result of the cast. *VALID_P is set to
6446 indicate whether or not the cast was valid. */
6448 static tree
6449 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6450 bool *valid_p, tsubst_flags_t complain)
6452 tree intype;
6453 tree result;
6454 cp_lvalue_kind clk;
6456 /* Assume the cast is valid. */
6457 *valid_p = true;
6459 intype = unlowered_expr_type (expr);
6461 /* Save casted types in the function's used types hash table. */
6462 used_types_insert (type);
6464 /* [expr.static.cast]
6466 An lvalue of type "cv1 B", where B is a class type, can be cast
6467 to type "reference to cv2 D", where D is a class derived (clause
6468 _class.derived_) from B, if a valid standard conversion from
6469 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6470 same cv-qualification as, or greater cv-qualification than, cv1,
6471 and B is not a virtual base class of D. */
6472 /* We check this case before checking the validity of "TYPE t =
6473 EXPR;" below because for this case:
6475 struct B {};
6476 struct D : public B { D(const B&); };
6477 extern B& b;
6478 void f() { static_cast<const D&>(b); }
6480 we want to avoid constructing a new D. The standard is not
6481 completely clear about this issue, but our interpretation is
6482 consistent with other compilers. */
6483 if (TREE_CODE (type) == REFERENCE_TYPE
6484 && CLASS_TYPE_P (TREE_TYPE (type))
6485 && CLASS_TYPE_P (intype)
6486 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6487 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6488 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6489 build_pointer_type (TYPE_MAIN_VARIANT
6490 (TREE_TYPE (type))),
6491 complain)
6492 && (c_cast_p
6493 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6495 tree base;
6497 /* There is a standard conversion from "D*" to "B*" even if "B"
6498 is ambiguous or inaccessible. If this is really a
6499 static_cast, then we check both for inaccessibility and
6500 ambiguity. However, if this is a static_cast being performed
6501 because the user wrote a C-style cast, then accessibility is
6502 not considered. */
6503 base = lookup_base (TREE_TYPE (type), intype,
6504 c_cast_p ? ba_unique : ba_check,
6505 NULL, complain);
6507 /* Convert from "B*" to "D*". This function will check that "B"
6508 is not a virtual base of "D". */
6509 expr = build_base_path (MINUS_EXPR, build_address (expr),
6510 base, /*nonnull=*/false, complain);
6511 /* Convert the pointer to a reference -- but then remember that
6512 there are no expressions with reference type in C++.
6514 We call rvalue so that there's an actual tree code
6515 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6516 is a variable with the same type, the conversion would get folded
6517 away, leaving just the variable and causing lvalue_kind to give
6518 the wrong answer. */
6519 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6522 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6523 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6524 if (TREE_CODE (type) == REFERENCE_TYPE
6525 && TYPE_REF_IS_RVALUE (type)
6526 && (clk = real_lvalue_p (expr))
6527 && reference_related_p (TREE_TYPE (type), intype)
6528 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6530 if (clk == clk_ordinary)
6532 /* Handle the (non-bit-field) lvalue case here by casting to
6533 lvalue reference and then changing it to an rvalue reference.
6534 Casting an xvalue to rvalue reference will be handled by the
6535 main code path. */
6536 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6537 result = (perform_direct_initialization_if_possible
6538 (lref, expr, c_cast_p, complain));
6539 result = cp_fold_convert (type, result);
6540 /* Make sure we don't fold back down to a named rvalue reference,
6541 because that would be an lvalue. */
6542 if (DECL_P (result))
6543 result = build1 (NON_LVALUE_EXPR, type, result);
6544 return convert_from_reference (result);
6546 else
6547 /* For a bit-field or packed field, bind to a temporary. */
6548 expr = rvalue (expr);
6551 /* Resolve overloaded address here rather than once in
6552 implicit_conversion and again in the inverse code below. */
6553 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6555 expr = instantiate_type (type, expr, complain);
6556 intype = TREE_TYPE (expr);
6559 /* [expr.static.cast]
6561 Any expression can be explicitly converted to type cv void. */
6562 if (VOID_TYPE_P (type))
6563 return convert_to_void (expr, ICV_CAST, complain);
6565 /* [class.abstract]
6566 An abstract class shall not be used ... as the type of an explicit
6567 conversion. */
6568 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6569 return error_mark_node;
6571 /* [expr.static.cast]
6573 An expression e can be explicitly converted to a type T using a
6574 static_cast of the form static_cast<T>(e) if the declaration T
6575 t(e);" is well-formed, for some invented temporary variable
6576 t. */
6577 result = perform_direct_initialization_if_possible (type, expr,
6578 c_cast_p, complain);
6579 if (result)
6581 result = convert_from_reference (result);
6583 /* [expr.static.cast]
6585 If T is a reference type, the result is an lvalue; otherwise,
6586 the result is an rvalue. */
6587 if (TREE_CODE (type) != REFERENCE_TYPE)
6588 result = rvalue (result);
6589 return result;
6592 /* [expr.static.cast]
6594 The inverse of any standard conversion sequence (clause _conv_),
6595 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6596 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6597 (_conv.bool_) conversions, can be performed explicitly using
6598 static_cast subject to the restriction that the explicit
6599 conversion does not cast away constness (_expr.const.cast_), and
6600 the following additional rules for specific cases: */
6601 /* For reference, the conversions not excluded are: integral
6602 promotions, floating point promotion, integral conversions,
6603 floating point conversions, floating-integral conversions,
6604 pointer conversions, and pointer to member conversions. */
6605 /* DR 128
6607 A value of integral _or enumeration_ type can be explicitly
6608 converted to an enumeration type. */
6609 /* The effect of all that is that any conversion between any two
6610 types which are integral, floating, or enumeration types can be
6611 performed. */
6612 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6613 || SCALAR_FLOAT_TYPE_P (type))
6614 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6615 || SCALAR_FLOAT_TYPE_P (intype)))
6616 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6618 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6619 && CLASS_TYPE_P (TREE_TYPE (type))
6620 && CLASS_TYPE_P (TREE_TYPE (intype))
6621 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6622 (TREE_TYPE (intype))),
6623 build_pointer_type (TYPE_MAIN_VARIANT
6624 (TREE_TYPE (type))),
6625 complain))
6627 tree base;
6629 if (!c_cast_p
6630 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6631 complain))
6632 return error_mark_node;
6633 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6634 c_cast_p ? ba_unique : ba_check,
6635 NULL, complain);
6636 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6637 complain);
6638 return cp_fold_convert(type, expr);
6641 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6642 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6644 tree c1;
6645 tree c2;
6646 tree t1;
6647 tree t2;
6649 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6650 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6652 if (TYPE_PTRDATAMEM_P (type))
6654 t1 = (build_ptrmem_type
6655 (c1,
6656 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6657 t2 = (build_ptrmem_type
6658 (c2,
6659 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6661 else
6663 t1 = intype;
6664 t2 = type;
6666 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6668 if (!c_cast_p
6669 && check_for_casting_away_constness (intype, type,
6670 STATIC_CAST_EXPR,
6671 complain))
6672 return error_mark_node;
6673 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6674 c_cast_p, complain);
6678 /* [expr.static.cast]
6680 An rvalue of type "pointer to cv void" can be explicitly
6681 converted to a pointer to object type. A value of type pointer
6682 to object converted to "pointer to cv void" and back to the
6683 original pointer type will have its original value. */
6684 if (TYPE_PTR_P (intype)
6685 && VOID_TYPE_P (TREE_TYPE (intype))
6686 && TYPE_PTROB_P (type))
6688 if (!c_cast_p
6689 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6690 complain))
6691 return error_mark_node;
6692 return build_nop (type, expr);
6695 *valid_p = false;
6696 return error_mark_node;
6699 /* Return an expression representing static_cast<TYPE>(EXPR). */
6701 tree
6702 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6704 tree result;
6705 bool valid_p;
6707 if (type == error_mark_node || expr == error_mark_node)
6708 return error_mark_node;
6710 if (processing_template_decl)
6712 expr = build_min (STATIC_CAST_EXPR, type, expr);
6713 /* We don't know if it will or will not have side effects. */
6714 TREE_SIDE_EFFECTS (expr) = 1;
6715 return convert_from_reference (expr);
6718 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6719 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6720 if (TREE_CODE (type) != REFERENCE_TYPE
6721 && TREE_CODE (expr) == NOP_EXPR
6722 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6723 expr = TREE_OPERAND (expr, 0);
6725 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6726 complain);
6727 if (valid_p)
6729 if (result != error_mark_node)
6730 maybe_warn_about_useless_cast (type, expr, complain);
6731 return result;
6734 if (complain & tf_error)
6735 error ("invalid static_cast from type %qT to type %qT",
6736 TREE_TYPE (expr), type);
6737 return error_mark_node;
6740 /* EXPR is an expression with member function or pointer-to-member
6741 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6742 not permitted by ISO C++, but we accept it in some modes. If we
6743 are not in one of those modes, issue a diagnostic. Return the
6744 converted expression. */
6746 tree
6747 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6749 tree intype;
6750 tree decl;
6752 intype = TREE_TYPE (expr);
6753 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6754 || TREE_CODE (intype) == METHOD_TYPE);
6756 if (!(complain & tf_warning_or_error))
6757 return error_mark_node;
6759 if (pedantic || warn_pmf2ptr)
6760 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6761 "converting from %qT to %qT", intype, type);
6763 if (TREE_CODE (intype) == METHOD_TYPE)
6764 expr = build_addr_func (expr, complain);
6765 else if (TREE_CODE (expr) == PTRMEM_CST)
6766 expr = build_address (PTRMEM_CST_MEMBER (expr));
6767 else
6769 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6770 decl = build_address (decl);
6771 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6774 if (expr == error_mark_node)
6775 return error_mark_node;
6777 return build_nop (type, expr);
6780 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6781 If C_CAST_P is true, this reinterpret cast is being done as part of
6782 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6783 indicate whether or not reinterpret_cast was valid. */
6785 static tree
6786 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6787 bool *valid_p, tsubst_flags_t complain)
6789 tree intype;
6791 /* Assume the cast is invalid. */
6792 if (valid_p)
6793 *valid_p = true;
6795 if (type == error_mark_node || error_operand_p (expr))
6796 return error_mark_node;
6798 intype = TREE_TYPE (expr);
6800 /* Save casted types in the function's used types hash table. */
6801 used_types_insert (type);
6803 /* [expr.reinterpret.cast]
6804 An lvalue expression of type T1 can be cast to the type
6805 "reference to T2" if an expression of type "pointer to T1" can be
6806 explicitly converted to the type "pointer to T2" using a
6807 reinterpret_cast. */
6808 if (TREE_CODE (type) == REFERENCE_TYPE)
6810 if (! real_lvalue_p (expr))
6812 if (complain & tf_error)
6813 error ("invalid cast of an rvalue expression of type "
6814 "%qT to type %qT",
6815 intype, type);
6816 return error_mark_node;
6819 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6820 "B" are related class types; the reinterpret_cast does not
6821 adjust the pointer. */
6822 if (TYPE_PTR_P (intype)
6823 && (complain & tf_warning)
6824 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6825 COMPARE_BASE | COMPARE_DERIVED)))
6826 warning (0, "casting %qT to %qT does not dereference pointer",
6827 intype, type);
6829 expr = cp_build_addr_expr (expr, complain);
6831 if (warn_strict_aliasing > 2)
6832 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6834 if (expr != error_mark_node)
6835 expr = build_reinterpret_cast_1
6836 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6837 valid_p, complain);
6838 if (expr != error_mark_node)
6839 /* cp_build_indirect_ref isn't right for rvalue refs. */
6840 expr = convert_from_reference (fold_convert (type, expr));
6841 return expr;
6844 /* As a G++ extension, we consider conversions from member
6845 functions, and pointers to member functions to
6846 pointer-to-function and pointer-to-void types. If
6847 -Wno-pmf-conversions has not been specified,
6848 convert_member_func_to_ptr will issue an error message. */
6849 if ((TYPE_PTRMEMFUNC_P (intype)
6850 || TREE_CODE (intype) == METHOD_TYPE)
6851 && TYPE_PTR_P (type)
6852 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6853 || VOID_TYPE_P (TREE_TYPE (type))))
6854 return convert_member_func_to_ptr (type, expr, complain);
6856 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6857 array-to-pointer, and function-to-pointer conversions are
6858 performed. */
6859 expr = decay_conversion (expr, complain);
6861 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6862 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6863 if (TREE_CODE (expr) == NOP_EXPR
6864 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6865 expr = TREE_OPERAND (expr, 0);
6867 if (error_operand_p (expr))
6868 return error_mark_node;
6870 intype = TREE_TYPE (expr);
6872 /* [expr.reinterpret.cast]
6873 A pointer can be converted to any integral type large enough to
6874 hold it. ... A value of type std::nullptr_t can be converted to
6875 an integral type; the conversion has the same meaning and
6876 validity as a conversion of (void*)0 to the integral type. */
6877 if (CP_INTEGRAL_TYPE_P (type)
6878 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6880 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6882 if (complain & tf_error)
6883 permerror (input_location, "cast from %qT to %qT loses precision",
6884 intype, type);
6885 else
6886 return error_mark_node;
6888 if (NULLPTR_TYPE_P (intype))
6889 return build_int_cst (type, 0);
6891 /* [expr.reinterpret.cast]
6892 A value of integral or enumeration type can be explicitly
6893 converted to a pointer. */
6894 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6895 /* OK */
6897 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6898 || TYPE_PTR_OR_PTRMEM_P (type))
6899 && same_type_p (type, intype))
6900 /* DR 799 */
6901 return fold_if_not_in_template (build_nop (type, expr));
6902 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6903 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6904 return fold_if_not_in_template (build_nop (type, expr));
6905 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6906 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6908 tree sexpr = expr;
6910 if (!c_cast_p
6911 && check_for_casting_away_constness (intype, type,
6912 REINTERPRET_CAST_EXPR,
6913 complain))
6914 return error_mark_node;
6915 /* Warn about possible alignment problems. */
6916 if (STRICT_ALIGNMENT && warn_cast_align
6917 && (complain & tf_warning)
6918 && !VOID_TYPE_P (type)
6919 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6920 && COMPLETE_TYPE_P (TREE_TYPE (type))
6921 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6922 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6923 warning (OPT_Wcast_align, "cast from %qT to %qT "
6924 "increases required alignment of target type", intype, type);
6926 /* We need to strip nops here, because the front end likes to
6927 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6928 STRIP_NOPS (sexpr);
6929 if (warn_strict_aliasing <= 2)
6930 strict_aliasing_warning (intype, type, sexpr);
6932 return fold_if_not_in_template (build_nop (type, expr));
6934 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6935 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6937 if (complain & tf_warning)
6938 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6939 object pointer type or vice versa is conditionally-supported." */
6940 warning (OPT_Wconditionally_supported,
6941 "casting between pointer-to-function and pointer-to-object "
6942 "is conditionally-supported");
6943 return fold_if_not_in_template (build_nop (type, expr));
6945 else if (TREE_CODE (type) == VECTOR_TYPE)
6946 return fold_if_not_in_template (convert_to_vector (type, expr));
6947 else if (TREE_CODE (intype) == VECTOR_TYPE
6948 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6949 return fold_if_not_in_template (convert_to_integer (type, expr));
6950 else
6952 if (valid_p)
6953 *valid_p = false;
6954 if (complain & tf_error)
6955 error ("invalid cast from type %qT to type %qT", intype, type);
6956 return error_mark_node;
6959 return cp_convert (type, expr, complain);
6962 tree
6963 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6965 tree r;
6967 if (type == error_mark_node || expr == error_mark_node)
6968 return error_mark_node;
6970 if (processing_template_decl)
6972 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6974 if (!TREE_SIDE_EFFECTS (t)
6975 && type_dependent_expression_p (expr))
6976 /* There might turn out to be side effects inside expr. */
6977 TREE_SIDE_EFFECTS (t) = 1;
6978 return convert_from_reference (t);
6981 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6982 /*valid_p=*/NULL, complain);
6983 if (r != error_mark_node)
6984 maybe_warn_about_useless_cast (type, expr, complain);
6985 return r;
6988 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6989 return an appropriate expression. Otherwise, return
6990 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6991 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6992 performing a C-style cast, its value upon return will indicate
6993 whether or not the conversion succeeded. */
6995 static tree
6996 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6997 bool *valid_p)
6999 tree src_type;
7000 tree reference_type;
7002 /* Callers are responsible for handling error_mark_node as a
7003 destination type. */
7004 gcc_assert (dst_type != error_mark_node);
7005 /* In a template, callers should be building syntactic
7006 representations of casts, not using this machinery. */
7007 gcc_assert (!processing_template_decl);
7009 /* Assume the conversion is invalid. */
7010 if (valid_p)
7011 *valid_p = false;
7013 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7015 if (complain & tf_error)
7016 error ("invalid use of const_cast with type %qT, "
7017 "which is not a pointer, "
7018 "reference, nor a pointer-to-data-member type", dst_type);
7019 return error_mark_node;
7022 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7024 if (complain & tf_error)
7025 error ("invalid use of const_cast with type %qT, which is a pointer "
7026 "or reference to a function type", dst_type);
7027 return error_mark_node;
7030 /* Save casted types in the function's used types hash table. */
7031 used_types_insert (dst_type);
7033 src_type = TREE_TYPE (expr);
7034 /* Expressions do not really have reference types. */
7035 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7036 src_type = TREE_TYPE (src_type);
7038 /* [expr.const.cast]
7040 For two object types T1 and T2, if a pointer to T1 can be explicitly
7041 converted to the type "pointer to T2" using a const_cast, then the
7042 following conversions can also be made:
7044 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7045 type T2 using the cast const_cast<T2&>;
7047 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7048 type T2 using the cast const_cast<T2&&>; and
7050 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7051 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7053 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7055 reference_type = dst_type;
7056 if (!TYPE_REF_IS_RVALUE (dst_type)
7057 ? real_lvalue_p (expr)
7058 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7059 ? lvalue_p (expr)
7060 : lvalue_or_rvalue_with_address_p (expr)))
7061 /* OK. */;
7062 else
7064 if (complain & tf_error)
7065 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7066 src_type, dst_type);
7067 return error_mark_node;
7069 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7070 src_type = build_pointer_type (src_type);
7072 else
7074 reference_type = NULL_TREE;
7075 /* If the destination type is not a reference type, the
7076 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7077 conversions are performed. */
7078 src_type = type_decays_to (src_type);
7079 if (src_type == error_mark_node)
7080 return error_mark_node;
7083 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7085 if (comp_ptr_ttypes_const (dst_type, src_type))
7087 if (valid_p)
7089 *valid_p = true;
7090 /* This cast is actually a C-style cast. Issue a warning if
7091 the user is making a potentially unsafe cast. */
7092 check_for_casting_away_constness (src_type, dst_type,
7093 CAST_EXPR, complain);
7095 if (reference_type)
7097 expr = cp_build_addr_expr (expr, complain);
7098 if (expr == error_mark_node)
7099 return error_mark_node;
7100 expr = build_nop (reference_type, expr);
7101 return convert_from_reference (expr);
7103 else
7105 expr = decay_conversion (expr, complain);
7106 if (expr == error_mark_node)
7107 return error_mark_node;
7109 /* build_c_cast puts on a NOP_EXPR to make the result not an
7110 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7111 non-lvalue context. */
7112 if (TREE_CODE (expr) == NOP_EXPR
7113 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7114 expr = TREE_OPERAND (expr, 0);
7115 return build_nop (dst_type, expr);
7118 else if (valid_p
7119 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7120 TREE_TYPE (src_type)))
7121 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7122 complain);
7125 if (complain & tf_error)
7126 error ("invalid const_cast from type %qT to type %qT",
7127 src_type, dst_type);
7128 return error_mark_node;
7131 tree
7132 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7134 tree r;
7136 if (type == error_mark_node || error_operand_p (expr))
7137 return error_mark_node;
7139 if (processing_template_decl)
7141 tree t = build_min (CONST_CAST_EXPR, type, expr);
7143 if (!TREE_SIDE_EFFECTS (t)
7144 && type_dependent_expression_p (expr))
7145 /* There might turn out to be side effects inside expr. */
7146 TREE_SIDE_EFFECTS (t) = 1;
7147 return convert_from_reference (t);
7150 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7151 if (r != error_mark_node)
7152 maybe_warn_about_useless_cast (type, expr, complain);
7153 return r;
7156 /* Like cp_build_c_cast, but for the c-common bits. */
7158 tree
7159 build_c_cast (location_t /*loc*/, tree type, tree expr)
7161 return cp_build_c_cast (type, expr, tf_warning_or_error);
7164 /* Build an expression representing an explicit C-style cast to type
7165 TYPE of expression EXPR. */
7167 tree
7168 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7170 tree value = expr;
7171 tree result;
7172 bool valid_p;
7174 if (type == error_mark_node || error_operand_p (expr))
7175 return error_mark_node;
7177 if (processing_template_decl)
7179 tree t = build_min (CAST_EXPR, type,
7180 tree_cons (NULL_TREE, value, NULL_TREE));
7181 /* We don't know if it will or will not have side effects. */
7182 TREE_SIDE_EFFECTS (t) = 1;
7183 return convert_from_reference (t);
7186 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7187 'Class') should always be retained, because this information aids
7188 in method lookup. */
7189 if (objc_is_object_ptr (type)
7190 && objc_is_object_ptr (TREE_TYPE (expr)))
7191 return build_nop (type, expr);
7193 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7194 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7195 if (TREE_CODE (type) != REFERENCE_TYPE
7196 && TREE_CODE (value) == NOP_EXPR
7197 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7198 value = TREE_OPERAND (value, 0);
7200 if (TREE_CODE (type) == ARRAY_TYPE)
7202 /* Allow casting from T1* to T2[] because Cfront allows it.
7203 NIHCL uses it. It is not valid ISO C++ however. */
7204 if (TYPE_PTR_P (TREE_TYPE (expr)))
7206 if (complain & tf_error)
7207 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7208 else
7209 return error_mark_node;
7210 type = build_pointer_type (TREE_TYPE (type));
7212 else
7214 if (complain & tf_error)
7215 error ("ISO C++ forbids casting to an array type %qT", type);
7216 return error_mark_node;
7220 if (TREE_CODE (type) == FUNCTION_TYPE
7221 || TREE_CODE (type) == METHOD_TYPE)
7223 if (complain & tf_error)
7224 error ("invalid cast to function type %qT", type);
7225 return error_mark_node;
7228 if (TYPE_PTR_P (type)
7229 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7230 /* Casting to an integer of smaller size is an error detected elsewhere. */
7231 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7232 /* Don't warn about converting any constant. */
7233 && !TREE_CONSTANT (value))
7234 warning_at (input_location, OPT_Wint_to_pointer_cast,
7235 "cast to pointer from integer of different size");
7237 /* A C-style cast can be a const_cast. */
7238 result = build_const_cast_1 (type, value, complain & tf_warning,
7239 &valid_p);
7240 if (valid_p)
7242 if (result != error_mark_node)
7243 maybe_warn_about_useless_cast (type, value, complain);
7244 return result;
7247 /* Or a static cast. */
7248 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7249 &valid_p, complain);
7250 /* Or a reinterpret_cast. */
7251 if (!valid_p)
7252 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7253 &valid_p, complain);
7254 /* The static_cast or reinterpret_cast may be followed by a
7255 const_cast. */
7256 if (valid_p
7257 /* A valid cast may result in errors if, for example, a
7258 conversion to an ambiguous base class is required. */
7259 && !error_operand_p (result))
7261 tree result_type;
7263 maybe_warn_about_useless_cast (type, value, complain);
7265 /* Non-class rvalues always have cv-unqualified type. */
7266 if (!CLASS_TYPE_P (type))
7267 type = TYPE_MAIN_VARIANT (type);
7268 result_type = TREE_TYPE (result);
7269 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7270 result_type = TYPE_MAIN_VARIANT (result_type);
7271 /* If the type of RESULT does not match TYPE, perform a
7272 const_cast to make it match. If the static_cast or
7273 reinterpret_cast succeeded, we will differ by at most
7274 cv-qualification, so the follow-on const_cast is guaranteed
7275 to succeed. */
7276 if (!same_type_p (non_reference (type), non_reference (result_type)))
7278 result = build_const_cast_1 (type, result, false, &valid_p);
7279 gcc_assert (valid_p);
7281 return result;
7284 return error_mark_node;
7287 /* For use from the C common bits. */
7288 tree
7289 build_modify_expr (location_t /*location*/,
7290 tree lhs, tree /*lhs_origtype*/,
7291 enum tree_code modifycode,
7292 location_t /*rhs_location*/, tree rhs,
7293 tree /*rhs_origtype*/)
7295 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7298 /* Build an assignment expression of lvalue LHS from value RHS.
7299 MODIFYCODE is the code for a binary operator that we use
7300 to combine the old value of LHS with RHS to get the new value.
7301 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7303 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7305 tree
7306 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7307 tsubst_flags_t complain)
7309 tree result;
7310 tree newrhs = rhs;
7311 tree lhstype = TREE_TYPE (lhs);
7312 tree olhstype = lhstype;
7313 bool plain_assign = (modifycode == NOP_EXPR);
7315 /* Avoid duplicate error messages from operands that had errors. */
7316 if (error_operand_p (lhs) || error_operand_p (rhs))
7317 return error_mark_node;
7319 /* Handle control structure constructs used as "lvalues". */
7320 switch (TREE_CODE (lhs))
7322 /* Handle --foo = 5; as these are valid constructs in C++. */
7323 case PREDECREMENT_EXPR:
7324 case PREINCREMENT_EXPR:
7325 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7326 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7327 stabilize_reference (TREE_OPERAND (lhs, 0)),
7328 TREE_OPERAND (lhs, 1));
7329 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7330 modifycode, rhs, complain);
7331 if (newrhs == error_mark_node)
7332 return error_mark_node;
7333 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7335 /* Handle (a, b) used as an "lvalue". */
7336 case COMPOUND_EXPR:
7337 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7338 modifycode, rhs, complain);
7339 if (newrhs == error_mark_node)
7340 return error_mark_node;
7341 return build2 (COMPOUND_EXPR, lhstype,
7342 TREE_OPERAND (lhs, 0), newrhs);
7344 case MODIFY_EXPR:
7345 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7346 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7347 stabilize_reference (TREE_OPERAND (lhs, 0)),
7348 TREE_OPERAND (lhs, 1));
7349 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7350 complain);
7351 if (newrhs == error_mark_node)
7352 return error_mark_node;
7353 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7355 case MIN_EXPR:
7356 case MAX_EXPR:
7357 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7358 when neither operand has side-effects. */
7359 if (!lvalue_or_else (lhs, lv_assign, complain))
7360 return error_mark_node;
7362 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7363 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7365 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7366 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7367 boolean_type_node,
7368 TREE_OPERAND (lhs, 0),
7369 TREE_OPERAND (lhs, 1)),
7370 TREE_OPERAND (lhs, 0),
7371 TREE_OPERAND (lhs, 1));
7372 /* Fall through. */
7374 /* Handle (a ? b : c) used as an "lvalue". */
7375 case COND_EXPR:
7377 /* Produce (a ? (b = rhs) : (c = rhs))
7378 except that the RHS goes through a save-expr
7379 so the code to compute it is only emitted once. */
7380 tree cond;
7381 tree preeval = NULL_TREE;
7383 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7385 if (complain & tf_error)
7386 error ("void value not ignored as it ought to be");
7387 return error_mark_node;
7390 rhs = stabilize_expr (rhs, &preeval);
7392 /* Check this here to avoid odd errors when trying to convert
7393 a throw to the type of the COND_EXPR. */
7394 if (!lvalue_or_else (lhs, lv_assign, complain))
7395 return error_mark_node;
7397 cond = build_conditional_expr
7398 (input_location, TREE_OPERAND (lhs, 0),
7399 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7400 modifycode, rhs, complain),
7401 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7402 modifycode, rhs, complain),
7403 complain);
7405 if (cond == error_mark_node)
7406 return cond;
7407 /* Make sure the code to compute the rhs comes out
7408 before the split. */
7409 if (preeval)
7410 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7411 return cond;
7414 default:
7415 break;
7418 if (modifycode == INIT_EXPR)
7420 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7421 /* Do the default thing. */;
7422 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7424 /* Compound literal. */
7425 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7426 /* Call convert to generate an error; see PR 11063. */
7427 rhs = convert (lhstype, rhs);
7428 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7429 TREE_SIDE_EFFECTS (result) = 1;
7430 return result;
7432 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7433 /* Do the default thing. */;
7434 else
7436 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7437 result = build_special_member_call (lhs, complete_ctor_identifier,
7438 &rhs_vec, lhstype, LOOKUP_NORMAL,
7439 complain);
7440 release_tree_vector (rhs_vec);
7441 if (result == NULL_TREE)
7442 return error_mark_node;
7443 return result;
7446 else
7448 lhs = require_complete_type_sfinae (lhs, complain);
7449 if (lhs == error_mark_node)
7450 return error_mark_node;
7452 if (modifycode == NOP_EXPR)
7454 if (c_dialect_objc ())
7456 result = objc_maybe_build_modify_expr (lhs, rhs);
7457 if (result)
7458 return result;
7461 /* `operator=' is not an inheritable operator. */
7462 if (! MAYBE_CLASS_TYPE_P (lhstype))
7463 /* Do the default thing. */;
7464 else
7466 result = build_new_op (input_location, MODIFY_EXPR,
7467 LOOKUP_NORMAL, lhs, rhs,
7468 make_node (NOP_EXPR), /*overload=*/NULL,
7469 complain);
7470 if (result == NULL_TREE)
7471 return error_mark_node;
7472 return result;
7474 lhstype = olhstype;
7476 else
7478 tree init = NULL_TREE;
7480 /* A binary op has been requested. Combine the old LHS
7481 value with the RHS producing the value we should actually
7482 store into the LHS. */
7483 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7484 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7485 || MAYBE_CLASS_TYPE_P (lhstype)));
7487 /* Preevaluate the RHS to make sure its evaluation is complete
7488 before the lvalue-to-rvalue conversion of the LHS:
7490 [expr.ass] With respect to an indeterminately-sequenced
7491 function call, the operation of a compound assignment is a
7492 single evaluation. [ Note: Therefore, a function call shall
7493 not intervene between the lvalue-to-rvalue conversion and the
7494 side effect associated with any single compound assignment
7495 operator. -- end note ] */
7496 lhs = stabilize_reference (lhs);
7497 rhs = rvalue (rhs);
7498 rhs = stabilize_expr (rhs, &init);
7499 newrhs = cp_build_binary_op (input_location,
7500 modifycode, lhs, rhs,
7501 complain);
7502 if (newrhs == error_mark_node)
7504 if (complain & tf_error)
7505 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7506 TREE_TYPE (lhs), TREE_TYPE (rhs));
7507 return error_mark_node;
7510 if (init)
7511 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7513 /* Now it looks like a plain assignment. */
7514 modifycode = NOP_EXPR;
7515 if (c_dialect_objc ())
7517 result = objc_maybe_build_modify_expr (lhs, newrhs);
7518 if (result)
7519 return result;
7522 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7523 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7526 /* The left-hand side must be an lvalue. */
7527 if (!lvalue_or_else (lhs, lv_assign, complain))
7528 return error_mark_node;
7530 /* Warn about modifying something that is `const'. Don't warn if
7531 this is initialization. */
7532 if (modifycode != INIT_EXPR
7533 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7534 /* Functions are not modifiable, even though they are
7535 lvalues. */
7536 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7537 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7538 /* If it's an aggregate and any field is const, then it is
7539 effectively const. */
7540 || (CLASS_TYPE_P (lhstype)
7541 && C_TYPE_FIELDS_READONLY (lhstype))))
7543 if (complain & tf_error)
7544 cxx_readonly_error (lhs, lv_assign);
7545 else
7546 return error_mark_node;
7549 /* If storing into a structure or union member, it may have been given a
7550 lowered bitfield type. We need to convert to the declared type first,
7551 so retrieve it now. */
7553 olhstype = unlowered_expr_type (lhs);
7555 /* Convert new value to destination type. */
7557 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7559 int from_array;
7561 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7563 if (modifycode != INIT_EXPR)
7565 if (complain & tf_error)
7566 error ("assigning to an array from an initializer list");
7567 return error_mark_node;
7569 if (check_array_initializer (lhs, lhstype, newrhs))
7570 return error_mark_node;
7571 newrhs = digest_init (lhstype, newrhs, complain);
7572 if (newrhs == error_mark_node)
7573 return error_mark_node;
7576 /* C++11 8.5/17: "If the destination type is an array of characters,
7577 an array of char16_t, an array of char32_t, or an array of wchar_t,
7578 and the initializer is a string literal...". */
7579 else if (TREE_CODE (newrhs) == STRING_CST
7580 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7581 && modifycode == INIT_EXPR)
7583 newrhs = digest_init (lhstype, newrhs, complain);
7584 if (newrhs == error_mark_node)
7585 return error_mark_node;
7588 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7589 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7591 if (complain & tf_error)
7592 error ("incompatible types in assignment of %qT to %qT",
7593 TREE_TYPE (rhs), lhstype);
7594 return error_mark_node;
7597 /* Allow array assignment in compiler-generated code. */
7598 else if (!current_function_decl
7599 || !DECL_DEFAULTED_FN (current_function_decl))
7601 /* This routine is used for both initialization and assignment.
7602 Make sure the diagnostic message differentiates the context. */
7603 if (complain & tf_error)
7605 if (modifycode == INIT_EXPR)
7606 error ("array used as initializer");
7607 else
7608 error ("invalid array assignment");
7610 return error_mark_node;
7613 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7614 ? 1 + (modifycode != INIT_EXPR): 0;
7615 return build_vec_init (lhs, NULL_TREE, newrhs,
7616 /*explicit_value_init_p=*/false,
7617 from_array, complain);
7620 if (modifycode == INIT_EXPR)
7621 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7622 LOOKUP_ONLYCONVERTING. */
7623 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7624 ICR_INIT, NULL_TREE, 0,
7625 complain);
7626 else
7627 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7628 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7630 if (!same_type_p (lhstype, olhstype))
7631 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7633 if (modifycode != INIT_EXPR)
7635 if (TREE_CODE (newrhs) == CALL_EXPR
7636 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7637 newrhs = build_cplus_new (lhstype, newrhs, complain);
7639 /* Can't initialize directly from a TARGET_EXPR, since that would
7640 cause the lhs to be constructed twice, and possibly result in
7641 accidental self-initialization. So we force the TARGET_EXPR to be
7642 expanded without a target. */
7643 if (TREE_CODE (newrhs) == TARGET_EXPR)
7644 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7645 TREE_OPERAND (newrhs, 0));
7648 if (newrhs == error_mark_node)
7649 return error_mark_node;
7651 if (c_dialect_objc () && flag_objc_gc)
7653 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7655 if (result)
7656 return result;
7659 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7660 lhstype, lhs, newrhs);
7662 TREE_SIDE_EFFECTS (result) = 1;
7663 if (!plain_assign)
7664 TREE_NO_WARNING (result) = 1;
7666 return result;
7669 tree
7670 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7671 tree rhs, tsubst_flags_t complain)
7673 if (processing_template_decl)
7674 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7675 build_min_nt_loc (loc, modifycode, NULL_TREE,
7676 NULL_TREE), rhs);
7678 if (modifycode != NOP_EXPR)
7680 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7681 make_node (modifycode), /*overload=*/NULL,
7682 complain);
7683 if (rval)
7685 TREE_NO_WARNING (rval) = 1;
7686 return rval;
7689 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7692 /* Helper function for get_delta_difference which assumes FROM is a base
7693 class of TO. Returns a delta for the conversion of pointer-to-member
7694 of FROM to pointer-to-member of TO. If the conversion is invalid and
7695 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7696 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7697 If C_CAST_P is true, this conversion is taking place as part of a
7698 C-style cast. */
7700 static tree
7701 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7702 tsubst_flags_t complain)
7704 tree binfo;
7705 base_kind kind;
7707 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7708 &kind, complain);
7710 if (binfo == error_mark_node)
7712 if (!(complain & tf_error))
7713 return error_mark_node;
7715 error (" in pointer to member function conversion");
7716 return size_zero_node;
7718 else if (binfo)
7720 if (kind != bk_via_virtual)
7721 return BINFO_OFFSET (binfo);
7722 else
7723 /* FROM is a virtual base class of TO. Issue an error or warning
7724 depending on whether or not this is a reinterpret cast. */
7726 if (!(complain & tf_error))
7727 return error_mark_node;
7729 error ("pointer to member conversion via virtual base %qT",
7730 BINFO_TYPE (binfo_from_vbase (binfo)));
7732 return size_zero_node;
7735 else
7736 return NULL_TREE;
7739 /* Get difference in deltas for different pointer to member function
7740 types. If the conversion is invalid and tf_error is not set in
7741 COMPLAIN, returns error_mark_node, otherwise returns an integer
7742 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7743 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7744 conversions as well. If C_CAST_P is true this conversion is taking
7745 place as part of a C-style cast.
7747 Note that the naming of FROM and TO is kind of backwards; the return
7748 value is what we add to a TO in order to get a FROM. They are named
7749 this way because we call this function to find out how to convert from
7750 a pointer to member of FROM to a pointer to member of TO. */
7752 static tree
7753 get_delta_difference (tree from, tree to,
7754 bool allow_inverse_p,
7755 bool c_cast_p, tsubst_flags_t complain)
7757 tree result;
7759 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7760 /* Pointer to member of incomplete class is permitted*/
7761 result = size_zero_node;
7762 else
7763 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7765 if (result == error_mark_node)
7766 return error_mark_node;
7768 if (!result)
7770 if (!allow_inverse_p)
7772 if (!(complain & tf_error))
7773 return error_mark_node;
7775 error_not_base_type (from, to);
7776 error (" in pointer to member conversion");
7777 result = size_zero_node;
7779 else
7781 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7783 if (result == error_mark_node)
7784 return error_mark_node;
7786 if (result)
7787 result = size_diffop_loc (input_location,
7788 size_zero_node, result);
7789 else
7791 if (!(complain & tf_error))
7792 return error_mark_node;
7794 error_not_base_type (from, to);
7795 error (" in pointer to member conversion");
7796 result = size_zero_node;
7801 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7802 result));
7805 /* Return a constructor for the pointer-to-member-function TYPE using
7806 the other components as specified. */
7808 tree
7809 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7811 tree u = NULL_TREE;
7812 tree delta_field;
7813 tree pfn_field;
7814 vec<constructor_elt, va_gc> *v;
7816 /* Pull the FIELD_DECLs out of the type. */
7817 pfn_field = TYPE_FIELDS (type);
7818 delta_field = DECL_CHAIN (pfn_field);
7820 /* Make sure DELTA has the type we want. */
7821 delta = convert_and_check (input_location, delta_type_node, delta);
7823 /* Convert to the correct target type if necessary. */
7824 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7826 /* Finish creating the initializer. */
7827 vec_alloc (v, 2);
7828 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7829 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7830 u = build_constructor (type, v);
7831 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7832 TREE_STATIC (u) = (TREE_CONSTANT (u)
7833 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7834 != NULL_TREE)
7835 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7836 != NULL_TREE));
7837 return u;
7840 /* Build a constructor for a pointer to member function. It can be
7841 used to initialize global variables, local variable, or used
7842 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7843 want to be.
7845 If FORCE is nonzero, then force this conversion, even if
7846 we would rather not do it. Usually set when using an explicit
7847 cast. A C-style cast is being processed iff C_CAST_P is true.
7849 Return error_mark_node, if something goes wrong. */
7851 tree
7852 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7853 tsubst_flags_t complain)
7855 tree fn;
7856 tree pfn_type;
7857 tree to_type;
7859 if (error_operand_p (pfn))
7860 return error_mark_node;
7862 pfn_type = TREE_TYPE (pfn);
7863 to_type = build_ptrmemfunc_type (type);
7865 /* Handle multiple conversions of pointer to member functions. */
7866 if (TYPE_PTRMEMFUNC_P (pfn_type))
7868 tree delta = NULL_TREE;
7869 tree npfn = NULL_TREE;
7870 tree n;
7872 if (!force
7873 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7874 LOOKUP_NORMAL, complain))
7876 if (complain & tf_error)
7877 error ("invalid conversion to type %qT from type %qT",
7878 to_type, pfn_type);
7879 else
7880 return error_mark_node;
7883 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7884 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7885 force,
7886 c_cast_p, complain);
7887 if (n == error_mark_node)
7888 return error_mark_node;
7890 /* We don't have to do any conversion to convert a
7891 pointer-to-member to its own type. But, we don't want to
7892 just return a PTRMEM_CST if there's an explicit cast; that
7893 cast should make the expression an invalid template argument. */
7894 if (TREE_CODE (pfn) != PTRMEM_CST)
7896 if (same_type_p (to_type, pfn_type))
7897 return pfn;
7898 else if (integer_zerop (n))
7899 return build_reinterpret_cast (to_type, pfn,
7900 complain);
7903 if (TREE_SIDE_EFFECTS (pfn))
7904 pfn = save_expr (pfn);
7906 /* Obtain the function pointer and the current DELTA. */
7907 if (TREE_CODE (pfn) == PTRMEM_CST)
7908 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7909 else
7911 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7912 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7915 /* Just adjust the DELTA field. */
7916 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7917 (TREE_TYPE (delta), ptrdiff_type_node));
7918 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7919 n = cp_build_binary_op (input_location,
7920 LSHIFT_EXPR, n, integer_one_node,
7921 complain);
7922 delta = cp_build_binary_op (input_location,
7923 PLUS_EXPR, delta, n, complain);
7924 return build_ptrmemfunc1 (to_type, delta, npfn);
7927 /* Handle null pointer to member function conversions. */
7928 if (null_ptr_cst_p (pfn))
7930 pfn = cp_build_c_cast (type, pfn, complain);
7931 return build_ptrmemfunc1 (to_type,
7932 integer_zero_node,
7933 pfn);
7936 if (type_unknown_p (pfn))
7937 return instantiate_type (type, pfn, complain);
7939 fn = TREE_OPERAND (pfn, 0);
7940 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7941 /* In a template, we will have preserved the
7942 OFFSET_REF. */
7943 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7944 return make_ptrmem_cst (to_type, fn);
7947 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7948 given by CST.
7950 ??? There is no consistency as to the types returned for the above
7951 values. Some code acts as if it were a sizetype and some as if it were
7952 integer_type_node. */
7954 void
7955 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7957 tree type = TREE_TYPE (cst);
7958 tree fn = PTRMEM_CST_MEMBER (cst);
7959 tree ptr_class, fn_class;
7961 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7963 /* The class that the function belongs to. */
7964 fn_class = DECL_CONTEXT (fn);
7966 /* The class that we're creating a pointer to member of. */
7967 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7969 /* First, calculate the adjustment to the function's class. */
7970 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7971 /*c_cast_p=*/0, tf_warning_or_error);
7973 if (!DECL_VIRTUAL_P (fn))
7974 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7975 build_addr_func (fn, tf_warning_or_error));
7976 else
7978 /* If we're dealing with a virtual function, we have to adjust 'this'
7979 again, to point to the base which provides the vtable entry for
7980 fn; the call will do the opposite adjustment. */
7981 tree orig_class = DECL_CONTEXT (fn);
7982 tree binfo = binfo_or_else (orig_class, fn_class);
7983 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7984 *delta, BINFO_OFFSET (binfo));
7985 *delta = fold_if_not_in_template (*delta);
7987 /* We set PFN to the vtable offset at which the function can be
7988 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7989 case delta is shifted left, and then incremented). */
7990 *pfn = DECL_VINDEX (fn);
7991 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7992 TYPE_SIZE_UNIT (vtable_entry_type));
7993 *pfn = fold_if_not_in_template (*pfn);
7995 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7997 case ptrmemfunc_vbit_in_pfn:
7998 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7999 integer_one_node);
8000 *pfn = fold_if_not_in_template (*pfn);
8001 break;
8003 case ptrmemfunc_vbit_in_delta:
8004 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8005 *delta, integer_one_node);
8006 *delta = fold_if_not_in_template (*delta);
8007 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
8008 *delta, integer_one_node);
8009 *delta = fold_if_not_in_template (*delta);
8010 break;
8012 default:
8013 gcc_unreachable ();
8016 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8017 *pfn = fold_if_not_in_template (*pfn);
8021 /* Return an expression for PFN from the pointer-to-member function
8022 given by T. */
8024 static tree
8025 pfn_from_ptrmemfunc (tree t)
8027 if (TREE_CODE (t) == PTRMEM_CST)
8029 tree delta;
8030 tree pfn;
8032 expand_ptrmemfunc_cst (t, &delta, &pfn);
8033 if (pfn)
8034 return pfn;
8037 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8040 /* Return an expression for DELTA from the pointer-to-member function
8041 given by T. */
8043 static tree
8044 delta_from_ptrmemfunc (tree t)
8046 if (TREE_CODE (t) == PTRMEM_CST)
8048 tree delta;
8049 tree pfn;
8051 expand_ptrmemfunc_cst (t, &delta, &pfn);
8052 if (delta)
8053 return delta;
8056 return build_ptrmemfunc_access_expr (t, delta_identifier);
8059 /* Convert value RHS to type TYPE as preparation for an assignment to
8060 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8061 implicit conversion is. If FNDECL is non-NULL, we are doing the
8062 conversion in order to pass the PARMNUMth argument of FNDECL.
8063 If FNDECL is NULL, we are doing the conversion in function pointer
8064 argument passing, conversion in initialization, etc. */
8066 static tree
8067 convert_for_assignment (tree type, tree rhs,
8068 impl_conv_rhs errtype, tree fndecl, int parmnum,
8069 tsubst_flags_t complain, int flags)
8071 tree rhstype;
8072 enum tree_code coder;
8074 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8075 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8076 rhs = TREE_OPERAND (rhs, 0);
8078 rhstype = TREE_TYPE (rhs);
8079 coder = TREE_CODE (rhstype);
8081 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8082 && vector_types_convertible_p (type, rhstype, true))
8084 rhs = mark_rvalue_use (rhs);
8085 return convert (type, rhs);
8088 if (rhs == error_mark_node || rhstype == error_mark_node)
8089 return error_mark_node;
8090 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8091 return error_mark_node;
8093 /* The RHS of an assignment cannot have void type. */
8094 if (coder == VOID_TYPE)
8096 if (complain & tf_error)
8097 error ("void value not ignored as it ought to be");
8098 return error_mark_node;
8101 if (c_dialect_objc ())
8103 int parmno;
8104 tree selector;
8105 tree rname = fndecl;
8107 switch (errtype)
8109 case ICR_ASSIGN:
8110 parmno = -1;
8111 break;
8112 case ICR_INIT:
8113 parmno = -2;
8114 break;
8115 default:
8116 selector = objc_message_selector ();
8117 parmno = parmnum;
8118 if (selector && parmno > 1)
8120 rname = selector;
8121 parmno -= 1;
8125 if (objc_compare_types (type, rhstype, parmno, rname))
8127 rhs = mark_rvalue_use (rhs);
8128 return convert (type, rhs);
8132 /* [expr.ass]
8134 The expression is implicitly converted (clause _conv_) to the
8135 cv-unqualified type of the left operand.
8137 We allow bad conversions here because by the time we get to this point
8138 we are committed to doing the conversion. If we end up doing a bad
8139 conversion, convert_like will complain. */
8140 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8142 /* When -Wno-pmf-conversions is use, we just silently allow
8143 conversions from pointers-to-members to plain pointers. If
8144 the conversion doesn't work, cp_convert will complain. */
8145 if (!warn_pmf2ptr
8146 && TYPE_PTR_P (type)
8147 && TYPE_PTRMEMFUNC_P (rhstype))
8148 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8149 else
8151 if (complain & tf_error)
8153 /* If the right-hand side has unknown type, then it is an
8154 overloaded function. Call instantiate_type to get error
8155 messages. */
8156 if (rhstype == unknown_type_node)
8157 instantiate_type (type, rhs, tf_warning_or_error);
8158 else if (fndecl)
8159 error ("cannot convert %qT to %qT for argument %qP to %qD",
8160 rhstype, type, parmnum, fndecl);
8161 else
8162 switch (errtype)
8164 case ICR_DEFAULT_ARGUMENT:
8165 error ("cannot convert %qT to %qT in default argument",
8166 rhstype, type);
8167 break;
8168 case ICR_ARGPASS:
8169 error ("cannot convert %qT to %qT in argument passing",
8170 rhstype, type);
8171 break;
8172 case ICR_CONVERTING:
8173 error ("cannot convert %qT to %qT",
8174 rhstype, type);
8175 break;
8176 case ICR_INIT:
8177 error ("cannot convert %qT to %qT in initialization",
8178 rhstype, type);
8179 break;
8180 case ICR_RETURN:
8181 error ("cannot convert %qT to %qT in return",
8182 rhstype, type);
8183 break;
8184 case ICR_ASSIGN:
8185 error ("cannot convert %qT to %qT in assignment",
8186 rhstype, type);
8187 break;
8188 default:
8189 gcc_unreachable();
8191 if (TYPE_PTR_P (rhstype)
8192 && TYPE_PTR_P (type)
8193 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8194 && CLASS_TYPE_P (TREE_TYPE (type))
8195 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8196 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8197 (TREE_TYPE (rhstype))),
8198 "class type %qT is incomplete", TREE_TYPE (rhstype));
8200 return error_mark_node;
8203 if (warn_suggest_attribute_format)
8205 const enum tree_code codel = TREE_CODE (type);
8206 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8207 && coder == codel
8208 && check_missing_format_attribute (type, rhstype)
8209 && (complain & tf_warning))
8210 switch (errtype)
8212 case ICR_ARGPASS:
8213 case ICR_DEFAULT_ARGUMENT:
8214 if (fndecl)
8215 warning (OPT_Wsuggest_attribute_format,
8216 "parameter %qP of %qD might be a candidate "
8217 "for a format attribute", parmnum, fndecl);
8218 else
8219 warning (OPT_Wsuggest_attribute_format,
8220 "parameter might be a candidate "
8221 "for a format attribute");
8222 break;
8223 case ICR_CONVERTING:
8224 warning (OPT_Wsuggest_attribute_format,
8225 "target of conversion might be a candidate "
8226 "for a format attribute");
8227 break;
8228 case ICR_INIT:
8229 warning (OPT_Wsuggest_attribute_format,
8230 "target of initialization might be a candidate "
8231 "for a format attribute");
8232 break;
8233 case ICR_RETURN:
8234 warning (OPT_Wsuggest_attribute_format,
8235 "return type might be a candidate "
8236 "for a format attribute");
8237 break;
8238 case ICR_ASSIGN:
8239 warning (OPT_Wsuggest_attribute_format,
8240 "left-hand side of assignment might be a candidate "
8241 "for a format attribute");
8242 break;
8243 default:
8244 gcc_unreachable();
8248 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8249 does not. */
8250 if (warn_parentheses
8251 && TREE_CODE (type) == BOOLEAN_TYPE
8252 && TREE_CODE (rhs) == MODIFY_EXPR
8253 && !TREE_NO_WARNING (rhs)
8254 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8255 && (complain & tf_warning))
8257 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8259 warning_at (loc, OPT_Wparentheses,
8260 "suggest parentheses around assignment used as truth value");
8261 TREE_NO_WARNING (rhs) = 1;
8264 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8265 complain, flags);
8268 /* Convert RHS to be of type TYPE.
8269 If EXP is nonzero, it is the target of the initialization.
8270 ERRTYPE indicates what kind of error the implicit conversion is.
8272 Two major differences between the behavior of
8273 `convert_for_assignment' and `convert_for_initialization'
8274 are that references are bashed in the former, while
8275 copied in the latter, and aggregates are assigned in
8276 the former (operator=) while initialized in the
8277 latter (X(X&)).
8279 If using constructor make sure no conversion operator exists, if one does
8280 exist, an ambiguity exists. */
8282 tree
8283 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8284 impl_conv_rhs errtype, tree fndecl, int parmnum,
8285 tsubst_flags_t complain)
8287 enum tree_code codel = TREE_CODE (type);
8288 tree rhstype;
8289 enum tree_code coder;
8291 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8292 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8293 if (TREE_CODE (rhs) == NOP_EXPR
8294 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8295 && codel != REFERENCE_TYPE)
8296 rhs = TREE_OPERAND (rhs, 0);
8298 if (type == error_mark_node
8299 || rhs == error_mark_node
8300 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8301 return error_mark_node;
8303 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8304 && TREE_CODE (type) != ARRAY_TYPE
8305 && (TREE_CODE (type) != REFERENCE_TYPE
8306 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8307 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8308 && !TYPE_REFFN_P (type))
8309 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8310 rhs = decay_conversion (rhs, complain);
8312 rhstype = TREE_TYPE (rhs);
8313 coder = TREE_CODE (rhstype);
8315 if (coder == ERROR_MARK)
8316 return error_mark_node;
8318 /* We accept references to incomplete types, so we can
8319 return here before checking if RHS is of complete type. */
8321 if (codel == REFERENCE_TYPE)
8323 /* This should eventually happen in convert_arguments. */
8324 int savew = 0, savee = 0;
8326 if (fndecl)
8327 savew = warningcount + werrorcount, savee = errorcount;
8328 rhs = initialize_reference (type, rhs, flags, complain);
8330 if (fndecl
8331 && (warningcount + werrorcount > savew || errorcount > savee))
8332 inform (input_location,
8333 "in passing argument %P of %q+D", parmnum, fndecl);
8335 return rhs;
8338 if (exp != 0)
8339 exp = require_complete_type_sfinae (exp, complain);
8340 if (exp == error_mark_node)
8341 return error_mark_node;
8343 rhstype = non_reference (rhstype);
8345 type = complete_type (type);
8347 if (DIRECT_INIT_EXPR_P (type, rhs))
8348 /* Don't try to do copy-initialization if we already have
8349 direct-initialization. */
8350 return rhs;
8352 if (MAYBE_CLASS_TYPE_P (type))
8353 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8355 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8356 complain, flags);
8359 /* If RETVAL is the address of, or a reference to, a local variable or
8360 temporary give an appropriate warning and return true. */
8362 static bool
8363 maybe_warn_about_returning_address_of_local (tree retval)
8365 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8366 tree whats_returned = retval;
8368 for (;;)
8370 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8371 whats_returned = TREE_OPERAND (whats_returned, 1);
8372 else if (CONVERT_EXPR_P (whats_returned)
8373 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8374 whats_returned = TREE_OPERAND (whats_returned, 0);
8375 else
8376 break;
8379 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8380 return false;
8381 whats_returned = TREE_OPERAND (whats_returned, 0);
8383 while (TREE_CODE (whats_returned) == COMPONENT_REF
8384 || TREE_CODE (whats_returned) == ARRAY_REF)
8385 whats_returned = TREE_OPERAND (whats_returned, 0);
8387 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8389 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8390 || TREE_CODE (whats_returned) == TARGET_EXPR)
8392 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8393 return true;
8395 if (VAR_P (whats_returned)
8396 && DECL_NAME (whats_returned)
8397 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8399 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8400 return true;
8404 if (DECL_P (whats_returned)
8405 && DECL_NAME (whats_returned)
8406 && DECL_FUNCTION_SCOPE_P (whats_returned)
8407 && !is_capture_proxy (whats_returned)
8408 && !(TREE_STATIC (whats_returned)
8409 || TREE_PUBLIC (whats_returned)))
8411 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8412 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8413 whats_returned);
8414 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8415 warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
8416 whats_returned);
8417 else
8418 warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
8419 "returned", whats_returned);
8420 return true;
8423 return false;
8426 /* Check that returning RETVAL from the current function is valid.
8427 Return an expression explicitly showing all conversions required to
8428 change RETVAL into the function return type, and to assign it to
8429 the DECL_RESULT for the function. Set *NO_WARNING to true if
8430 code reaches end of non-void function warning shouldn't be issued
8431 on this RETURN_EXPR. */
8433 tree
8434 check_return_expr (tree retval, bool *no_warning)
8436 tree result;
8437 /* The type actually returned by the function. */
8438 tree valtype;
8439 /* The type the function is declared to return, or void if
8440 the declared type is incomplete. */
8441 tree functype;
8442 int fn_returns_value_p;
8443 bool named_return_value_okay_p;
8445 *no_warning = false;
8447 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8449 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8450 "statement is not allowed");
8451 return NULL_TREE;
8454 /* A `volatile' function is one that isn't supposed to return, ever.
8455 (This is a G++ extension, used to get better code for functions
8456 that call the `volatile' function.) */
8457 if (TREE_THIS_VOLATILE (current_function_decl))
8458 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8460 /* Check for various simple errors. */
8461 if (DECL_DESTRUCTOR_P (current_function_decl))
8463 if (retval)
8464 error ("returning a value from a destructor");
8465 return NULL_TREE;
8467 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8469 if (in_function_try_handler)
8470 /* If a return statement appears in a handler of the
8471 function-try-block of a constructor, the program is ill-formed. */
8472 error ("cannot return from a handler of a function-try-block of a constructor");
8473 else if (retval)
8474 /* You can't return a value from a constructor. */
8475 error ("returning a value from a constructor");
8476 return NULL_TREE;
8479 if (processing_template_decl)
8481 current_function_returns_value = 1;
8482 if (check_for_bare_parameter_packs (retval))
8483 retval = error_mark_node;
8484 return retval;
8487 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8489 /* Deduce auto return type from a return statement. */
8490 if (current_function_auto_return_pattern)
8492 tree auto_node;
8493 tree type;
8495 if (!retval && !is_auto (current_function_auto_return_pattern))
8497 /* Give a helpful error message. */
8498 error ("return-statement with no value, in function returning %qT",
8499 current_function_auto_return_pattern);
8500 inform (input_location, "only plain %<auto%> return type can be "
8501 "deduced to %<void%>");
8502 type = error_mark_node;
8504 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8506 error ("returning initializer list");
8507 type = error_mark_node;
8509 else
8511 if (!retval)
8512 retval = void_node;
8513 auto_node = type_uses_auto (current_function_auto_return_pattern);
8514 type = do_auto_deduction (current_function_auto_return_pattern,
8515 retval, auto_node);
8518 if (type == error_mark_node)
8519 /* Leave it. */;
8520 else if (functype == current_function_auto_return_pattern)
8521 apply_deduced_return_type (current_function_decl, type);
8522 else
8523 /* A mismatch should have been diagnosed in do_auto_deduction. */
8524 gcc_assert (same_type_p (type, functype));
8525 functype = type;
8528 /* When no explicit return-value is given in a function with a named
8529 return value, the named return value is used. */
8530 result = DECL_RESULT (current_function_decl);
8531 valtype = TREE_TYPE (result);
8532 gcc_assert (valtype != NULL_TREE);
8533 fn_returns_value_p = !VOID_TYPE_P (valtype);
8534 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8535 retval = result;
8537 /* Check for a return statement with no return value in a function
8538 that's supposed to return a value. */
8539 if (!retval && fn_returns_value_p)
8541 if (functype != error_mark_node)
8542 permerror (input_location, "return-statement with no value, in "
8543 "function returning %qT", valtype);
8544 /* Remember that this function did return. */
8545 current_function_returns_value = 1;
8546 /* And signal caller that TREE_NO_WARNING should be set on the
8547 RETURN_EXPR to avoid control reaches end of non-void function
8548 warnings in tree-cfg.c. */
8549 *no_warning = true;
8551 /* Check for a return statement with a value in a function that
8552 isn't supposed to return a value. */
8553 else if (retval && !fn_returns_value_p)
8555 if (VOID_TYPE_P (TREE_TYPE (retval)))
8556 /* You can return a `void' value from a function of `void'
8557 type. In that case, we have to evaluate the expression for
8558 its side-effects. */
8559 finish_expr_stmt (retval);
8560 else
8561 permerror (input_location, "return-statement with a value, in function "
8562 "returning 'void'");
8563 current_function_returns_null = 1;
8565 /* There's really no value to return, after all. */
8566 return NULL_TREE;
8568 else if (!retval)
8569 /* Remember that this function can sometimes return without a
8570 value. */
8571 current_function_returns_null = 1;
8572 else
8573 /* Remember that this function did return a value. */
8574 current_function_returns_value = 1;
8576 /* Check for erroneous operands -- but after giving ourselves a
8577 chance to provide an error about returning a value from a void
8578 function. */
8579 if (error_operand_p (retval))
8581 current_function_return_value = error_mark_node;
8582 return error_mark_node;
8585 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8586 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8587 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8588 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8589 && ! flag_check_new
8590 && retval && null_ptr_cst_p (retval))
8591 warning (0, "%<operator new%> must not return NULL unless it is "
8592 "declared %<throw()%> (or -fcheck-new is in effect)");
8594 /* Effective C++ rule 15. See also start_function. */
8595 if (warn_ecpp
8596 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8598 bool warn = true;
8600 /* The function return type must be a reference to the current
8601 class. */
8602 if (TREE_CODE (valtype) == REFERENCE_TYPE
8603 && same_type_ignoring_top_level_qualifiers_p
8604 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8606 /* Returning '*this' is obviously OK. */
8607 if (retval == current_class_ref)
8608 warn = false;
8609 /* If we are calling a function whose return type is the same of
8610 the current class reference, it is ok. */
8611 else if (INDIRECT_REF_P (retval)
8612 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8613 warn = false;
8616 if (warn)
8617 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8620 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8622 [...] For a function with a class return type, if the expression
8623 in the return statement is the name of a local object, and the cv-
8624 unqualified type of the local object is the same as the function
8625 return type, an implementation is permitted to omit creating the tem-
8626 porary object to hold the function return value [...]
8628 So, if this is a value-returning function that always returns the same
8629 local variable, remember it.
8631 It might be nice to be more flexible, and choose the first suitable
8632 variable even if the function sometimes returns something else, but
8633 then we run the risk of clobbering the variable we chose if the other
8634 returned expression uses the chosen variable somehow. And people expect
8635 this restriction, anyway. (jason 2000-11-19)
8637 See finish_function and finalize_nrv for the rest of this optimization. */
8639 named_return_value_okay_p =
8640 (retval != NULL_TREE
8641 /* Must be a local, automatic variable. */
8642 && VAR_P (retval)
8643 && DECL_CONTEXT (retval) == current_function_decl
8644 && ! TREE_STATIC (retval)
8645 /* And not a lambda or anonymous union proxy. */
8646 && !DECL_HAS_VALUE_EXPR_P (retval)
8647 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8648 /* The cv-unqualified type of the returned value must be the
8649 same as the cv-unqualified return type of the
8650 function. */
8651 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8652 (TYPE_MAIN_VARIANT (functype)))
8653 /* And the returned value must be non-volatile. */
8654 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8656 if (fn_returns_value_p && flag_elide_constructors)
8658 if (named_return_value_okay_p
8659 && (current_function_return_value == NULL_TREE
8660 || current_function_return_value == retval))
8661 current_function_return_value = retval;
8662 else
8663 current_function_return_value = error_mark_node;
8666 /* We don't need to do any conversions when there's nothing being
8667 returned. */
8668 if (!retval)
8669 return NULL_TREE;
8671 /* Do any required conversions. */
8672 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8673 /* No conversions are required. */
8675 else
8677 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8679 /* The functype's return type will have been set to void, if it
8680 was an incomplete type. Just treat this as 'return;' */
8681 if (VOID_TYPE_P (functype))
8682 return error_mark_node;
8684 /* If we had an id-expression obfuscated by force_paren_expr, we need
8685 to undo it so we can try to treat it as an rvalue below. */
8686 if (cxx_dialect >= cxx14
8687 && INDIRECT_REF_P (retval)
8688 && REF_PARENTHESIZED_P (retval))
8690 retval = TREE_OPERAND (retval, 0);
8691 while (TREE_CODE (retval) == NON_LVALUE_EXPR
8692 || TREE_CODE (retval) == NOP_EXPR)
8693 retval = TREE_OPERAND (retval, 0);
8694 gcc_assert (TREE_CODE (retval) == ADDR_EXPR);
8695 retval = TREE_OPERAND (retval, 0);
8698 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8699 treated as an rvalue for the purposes of overload resolution to
8700 favor move constructors over copy constructors.
8702 Note that these conditions are similar to, but not as strict as,
8703 the conditions for the named return value optimization. */
8704 if ((cxx_dialect != cxx98)
8705 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8706 || TREE_CODE (retval) == PARM_DECL)
8707 && DECL_CONTEXT (retval) == current_function_decl
8708 && !TREE_STATIC (retval)
8709 /* This is only interesting for class type. */
8710 && CLASS_TYPE_P (functype))
8711 flags = flags | LOOKUP_PREFER_RVALUE;
8713 /* First convert the value to the function's return type, then
8714 to the type of return value's location to handle the
8715 case that functype is smaller than the valtype. */
8716 retval = convert_for_initialization
8717 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8718 tf_warning_or_error);
8719 retval = convert (valtype, retval);
8721 /* If the conversion failed, treat this just like `return;'. */
8722 if (retval == error_mark_node)
8723 return retval;
8724 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8725 else if (! cfun->returns_struct
8726 && TREE_CODE (retval) == TARGET_EXPR
8727 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8728 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8729 TREE_OPERAND (retval, 0));
8730 else if (maybe_warn_about_returning_address_of_local (retval))
8731 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8732 build_zero_cst (TREE_TYPE (retval)));
8735 /* Actually copy the value returned into the appropriate location. */
8736 if (retval && retval != result)
8737 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8739 return retval;
8743 /* Returns nonzero if the pointer-type FROM can be converted to the
8744 pointer-type TO via a qualification conversion. If CONSTP is -1,
8745 then we return nonzero if the pointers are similar, and the
8746 cv-qualification signature of FROM is a proper subset of that of TO.
8748 If CONSTP is positive, then all outer pointers have been
8749 const-qualified. */
8751 static int
8752 comp_ptr_ttypes_real (tree to, tree from, int constp)
8754 bool to_more_cv_qualified = false;
8755 bool is_opaque_pointer = false;
8757 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8759 if (TREE_CODE (to) != TREE_CODE (from))
8760 return 0;
8762 if (TREE_CODE (from) == OFFSET_TYPE
8763 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8764 TYPE_OFFSET_BASETYPE (to)))
8765 return 0;
8767 /* Const and volatile mean something different for function types,
8768 so the usual checks are not appropriate. */
8769 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8771 if (!at_least_as_qualified_p (to, from))
8772 return 0;
8774 if (!at_least_as_qualified_p (from, to))
8776 if (constp == 0)
8777 return 0;
8778 to_more_cv_qualified = true;
8781 if (constp > 0)
8782 constp &= TYPE_READONLY (to);
8785 if (TREE_CODE (to) == VECTOR_TYPE)
8786 is_opaque_pointer = vector_targets_convertible_p (to, from);
8788 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8789 return ((constp >= 0 || to_more_cv_qualified)
8790 && (is_opaque_pointer
8791 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8795 /* When comparing, say, char ** to char const **, this function takes
8796 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8797 types to this function. */
8800 comp_ptr_ttypes (tree to, tree from)
8802 return comp_ptr_ttypes_real (to, from, 1);
8805 /* Returns true iff FNTYPE is a non-class type that involves
8806 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8807 if a parameter type is ill-formed. */
8809 bool
8810 error_type_p (const_tree type)
8812 tree t;
8814 switch (TREE_CODE (type))
8816 case ERROR_MARK:
8817 return true;
8819 case POINTER_TYPE:
8820 case REFERENCE_TYPE:
8821 case OFFSET_TYPE:
8822 return error_type_p (TREE_TYPE (type));
8824 case FUNCTION_TYPE:
8825 case METHOD_TYPE:
8826 if (error_type_p (TREE_TYPE (type)))
8827 return true;
8828 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8829 if (error_type_p (TREE_VALUE (t)))
8830 return true;
8831 return false;
8833 case RECORD_TYPE:
8834 if (TYPE_PTRMEMFUNC_P (type))
8835 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8836 return false;
8838 default:
8839 return false;
8843 /* Returns true if to and from are (possibly multi-level) pointers to the same
8844 type or inheritance-related types, regardless of cv-quals. */
8846 bool
8847 ptr_reasonably_similar (const_tree to, const_tree from)
8849 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8851 /* Any target type is similar enough to void. */
8852 if (VOID_TYPE_P (to))
8853 return !error_type_p (from);
8854 if (VOID_TYPE_P (from))
8855 return !error_type_p (to);
8857 if (TREE_CODE (to) != TREE_CODE (from))
8858 return false;
8860 if (TREE_CODE (from) == OFFSET_TYPE
8861 && comptypes (TYPE_OFFSET_BASETYPE (to),
8862 TYPE_OFFSET_BASETYPE (from),
8863 COMPARE_BASE | COMPARE_DERIVED))
8864 continue;
8866 if (TREE_CODE (to) == VECTOR_TYPE
8867 && vector_types_convertible_p (to, from, false))
8868 return true;
8870 if (TREE_CODE (to) == INTEGER_TYPE
8871 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8872 return true;
8874 if (TREE_CODE (to) == FUNCTION_TYPE)
8875 return !error_type_p (to) && !error_type_p (from);
8877 if (!TYPE_PTR_P (to))
8879 /* When either type is incomplete avoid DERIVED_FROM_P,
8880 which may call complete_type (c++/57942). */
8881 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8882 return comptypes
8883 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8884 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8889 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8890 pointer-to-member types) are the same, ignoring cv-qualification at
8891 all levels. */
8893 bool
8894 comp_ptr_ttypes_const (tree to, tree from)
8896 bool is_opaque_pointer = false;
8898 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8900 if (TREE_CODE (to) != TREE_CODE (from))
8901 return false;
8903 if (TREE_CODE (from) == OFFSET_TYPE
8904 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8905 TYPE_OFFSET_BASETYPE (to)))
8906 continue;
8908 if (TREE_CODE (to) == VECTOR_TYPE)
8909 is_opaque_pointer = vector_targets_convertible_p (to, from);
8911 if (!TYPE_PTR_P (to))
8912 return (is_opaque_pointer
8913 || same_type_ignoring_top_level_qualifiers_p (to, from));
8917 /* Returns the type qualifiers for this type, including the qualifiers on the
8918 elements for an array type. */
8921 cp_type_quals (const_tree type)
8923 int quals;
8924 /* This CONST_CAST is okay because strip_array_types returns its
8925 argument unmodified and we assign it to a const_tree. */
8926 type = strip_array_types (CONST_CAST_TREE (type));
8927 if (type == error_mark_node
8928 /* Quals on a FUNCTION_TYPE are memfn quals. */
8929 || TREE_CODE (type) == FUNCTION_TYPE)
8930 return TYPE_UNQUALIFIED;
8931 quals = TYPE_QUALS (type);
8932 /* METHOD and REFERENCE_TYPEs should never have quals. */
8933 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8934 && TREE_CODE (type) != REFERENCE_TYPE)
8935 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8936 == TYPE_UNQUALIFIED));
8937 return quals;
8940 /* Returns the function-ref-qualifier for TYPE */
8942 cp_ref_qualifier
8943 type_memfn_rqual (const_tree type)
8945 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8946 || TREE_CODE (type) == METHOD_TYPE);
8948 if (!FUNCTION_REF_QUALIFIED (type))
8949 return REF_QUAL_NONE;
8950 else if (FUNCTION_RVALUE_QUALIFIED (type))
8951 return REF_QUAL_RVALUE;
8952 else
8953 return REF_QUAL_LVALUE;
8956 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8957 METHOD_TYPE. */
8960 type_memfn_quals (const_tree type)
8962 if (TREE_CODE (type) == FUNCTION_TYPE)
8963 return TYPE_QUALS (type);
8964 else if (TREE_CODE (type) == METHOD_TYPE)
8965 return cp_type_quals (class_of_this_parm (type));
8966 else
8967 gcc_unreachable ();
8970 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8971 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8973 tree
8974 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8976 /* Could handle METHOD_TYPE here if necessary. */
8977 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8978 if (TYPE_QUALS (type) == memfn_quals
8979 && type_memfn_rqual (type) == rqual)
8980 return type;
8982 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8983 complex. */
8984 tree result = build_qualified_type (type, memfn_quals);
8985 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8986 return build_ref_qualified_type (result, rqual);
8989 /* Returns nonzero if TYPE is const or volatile. */
8991 bool
8992 cv_qualified_p (const_tree type)
8994 int quals = cp_type_quals (type);
8995 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8998 /* Returns nonzero if the TYPE contains a mutable member. */
9000 bool
9001 cp_has_mutable_p (const_tree type)
9003 /* This CONST_CAST is okay because strip_array_types returns its
9004 argument unmodified and we assign it to a const_tree. */
9005 type = strip_array_types (CONST_CAST_TREE(type));
9007 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9010 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9011 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9012 approximation. In particular, consider:
9014 int f();
9015 struct S { int i; };
9016 const S s = { f(); }
9018 Here, we will make "s" as TREE_READONLY (because it is declared
9019 "const") -- only to reverse ourselves upon seeing that the
9020 initializer is non-constant. */
9022 void
9023 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9025 tree type = TREE_TYPE (decl);
9027 if (type == error_mark_node)
9028 return;
9030 if (TREE_CODE (decl) == TYPE_DECL)
9031 return;
9033 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9034 && type_quals != TYPE_UNQUALIFIED));
9036 /* Avoid setting TREE_READONLY incorrectly. */
9037 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9038 constructor can produce constant init, so rely on cp_finish_decl to
9039 clear TREE_READONLY if the variable has non-constant init. */
9041 /* If the type has (or might have) a mutable component, that component
9042 might be modified. */
9043 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9044 type_quals &= ~TYPE_QUAL_CONST;
9046 c_apply_type_quals_to_decl (type_quals, decl);
9049 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9050 exemplar types such that casting T1 to T2 is casting away constness
9051 if and only if there is no implicit conversion from T1 to T2. */
9053 static void
9054 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9056 int quals1;
9057 int quals2;
9059 /* [expr.const.cast]
9061 For multi-level pointer to members and multi-level mixed pointers
9062 and pointers to members (conv.qual), the "member" aspect of a
9063 pointer to member level is ignored when determining if a const
9064 cv-qualifier has been cast away. */
9065 /* [expr.const.cast]
9067 For two pointer types:
9069 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9070 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9071 K is min(N,M)
9073 casting from X1 to X2 casts away constness if, for a non-pointer
9074 type T there does not exist an implicit conversion (clause
9075 _conv_) from:
9077 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9081 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9082 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9083 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9085 *t1 = cp_build_qualified_type (void_type_node,
9086 cp_type_quals (*t1));
9087 *t2 = cp_build_qualified_type (void_type_node,
9088 cp_type_quals (*t2));
9089 return;
9092 quals1 = cp_type_quals (*t1);
9093 quals2 = cp_type_quals (*t2);
9095 if (TYPE_PTRDATAMEM_P (*t1))
9096 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9097 else
9098 *t1 = TREE_TYPE (*t1);
9099 if (TYPE_PTRDATAMEM_P (*t2))
9100 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9101 else
9102 *t2 = TREE_TYPE (*t2);
9104 casts_away_constness_r (t1, t2, complain);
9105 *t1 = build_pointer_type (*t1);
9106 *t2 = build_pointer_type (*t2);
9107 *t1 = cp_build_qualified_type (*t1, quals1);
9108 *t2 = cp_build_qualified_type (*t2, quals2);
9111 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9112 constness.
9114 ??? This function returns non-zero if casting away qualifiers not
9115 just const. We would like to return to the caller exactly which
9116 qualifiers are casted away to give more accurate diagnostics.
9119 static bool
9120 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9122 if (TREE_CODE (t2) == REFERENCE_TYPE)
9124 /* [expr.const.cast]
9126 Casting from an lvalue of type T1 to an lvalue of type T2
9127 using a reference cast casts away constness if a cast from an
9128 rvalue of type "pointer to T1" to the type "pointer to T2"
9129 casts away constness. */
9130 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9131 return casts_away_constness (build_pointer_type (t1),
9132 build_pointer_type (TREE_TYPE (t2)),
9133 complain);
9136 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9137 /* [expr.const.cast]
9139 Casting from an rvalue of type "pointer to data member of X
9140 of type T1" to the type "pointer to data member of Y of type
9141 T2" casts away constness if a cast from an rvalue of type
9142 "pointer to T1" to the type "pointer to T2" casts away
9143 constness. */
9144 return casts_away_constness
9145 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9146 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9147 complain);
9149 /* Casting away constness is only something that makes sense for
9150 pointer or reference types. */
9151 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9152 return false;
9154 /* Top-level qualifiers don't matter. */
9155 t1 = TYPE_MAIN_VARIANT (t1);
9156 t2 = TYPE_MAIN_VARIANT (t2);
9157 casts_away_constness_r (&t1, &t2, complain);
9158 if (!can_convert (t2, t1, complain))
9159 return true;
9161 return false;
9164 /* If T is a REFERENCE_TYPE return the type to which T refers.
9165 Otherwise, return T itself. */
9167 tree
9168 non_reference (tree t)
9170 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9171 t = TREE_TYPE (t);
9172 return t;
9176 /* Return nonzero if REF is an lvalue valid for this language;
9177 otherwise, print an error message and return zero. USE says
9178 how the lvalue is being used and so selects the error message. */
9181 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9183 cp_lvalue_kind kind = lvalue_kind (ref);
9185 if (kind == clk_none)
9187 if (complain & tf_error)
9188 lvalue_error (input_location, use);
9189 return 0;
9191 else if (kind & (clk_rvalueref|clk_class))
9193 if (!(complain & tf_error))
9194 return 0;
9195 if (kind & clk_class)
9196 /* Make this a permerror because we used to accept it. */
9197 permerror (input_location, "using temporary as lvalue");
9198 else
9199 error ("using xvalue (rvalue reference) as lvalue");
9201 return 1;
9204 /* Return true if a user-defined literal operator is a raw operator. */
9206 bool
9207 check_raw_literal_operator (const_tree decl)
9209 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9210 tree argtype;
9211 int arity;
9212 bool maybe_raw_p = false;
9214 /* Count the number and type of arguments and check for ellipsis. */
9215 for (argtype = argtypes, arity = 0;
9216 argtype && argtype != void_list_node;
9217 ++arity, argtype = TREE_CHAIN (argtype))
9219 tree t = TREE_VALUE (argtype);
9221 if (same_type_p (t, const_string_type_node))
9222 maybe_raw_p = true;
9224 if (!argtype)
9225 return false; /* Found ellipsis. */
9227 if (!maybe_raw_p || arity != 1)
9228 return false;
9230 return true;
9234 /* Return true if a user-defined literal operator has one of the allowed
9235 argument types. */
9237 bool
9238 check_literal_operator_args (const_tree decl,
9239 bool *long_long_unsigned_p, bool *long_double_p)
9241 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9243 *long_long_unsigned_p = false;
9244 *long_double_p = false;
9245 if (processing_template_decl || processing_specialization)
9246 return argtypes == void_list_node;
9247 else
9249 tree argtype;
9250 int arity;
9251 int max_arity = 2;
9253 /* Count the number and type of arguments and check for ellipsis. */
9254 for (argtype = argtypes, arity = 0;
9255 argtype && argtype != void_list_node;
9256 argtype = TREE_CHAIN (argtype))
9258 tree t = TREE_VALUE (argtype);
9259 ++arity;
9261 if (TYPE_PTR_P (t))
9263 bool maybe_raw_p = false;
9264 t = TREE_TYPE (t);
9265 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9266 return false;
9267 t = TYPE_MAIN_VARIANT (t);
9268 if ((maybe_raw_p = same_type_p (t, char_type_node))
9269 || same_type_p (t, wchar_type_node)
9270 || same_type_p (t, char16_type_node)
9271 || same_type_p (t, char32_type_node))
9273 argtype = TREE_CHAIN (argtype);
9274 if (!argtype)
9275 return false;
9276 t = TREE_VALUE (argtype);
9277 if (maybe_raw_p && argtype == void_list_node)
9278 return true;
9279 else if (same_type_p (t, size_type_node))
9281 ++arity;
9282 continue;
9284 else
9285 return false;
9288 else if (same_type_p (t, long_long_unsigned_type_node))
9290 max_arity = 1;
9291 *long_long_unsigned_p = true;
9293 else if (same_type_p (t, long_double_type_node))
9295 max_arity = 1;
9296 *long_double_p = true;
9298 else if (same_type_p (t, char_type_node))
9299 max_arity = 1;
9300 else if (same_type_p (t, wchar_type_node))
9301 max_arity = 1;
9302 else if (same_type_p (t, char16_type_node))
9303 max_arity = 1;
9304 else if (same_type_p (t, char32_type_node))
9305 max_arity = 1;
9306 else
9307 return false;
9309 if (!argtype)
9310 return false; /* Found ellipsis. */
9312 if (arity != max_arity)
9313 return false;
9315 return true;