Merge from trunk
[official-gcc.git] / gcc / cp / typeck.c
blob4b95f206d41b963b28600e41e8f93ca840f34b0d
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2015 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 "hash-set.h"
32 #include "machmode.h"
33 #include "vec.h"
34 #include "double-int.h"
35 #include "input.h"
36 #include "alias.h"
37 #include "symtab.h"
38 #include "wide-int.h"
39 #include "inchash.h"
40 #include "tree.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
43 #include "varasm.h"
44 #include "cp-tree.h"
45 #include "flags.h"
46 #include "diagnostic.h"
47 #include "intl.h"
48 #include "target.h"
49 #include "convert.h"
50 #include "c-family/c-common.h"
51 #include "c-family/c-objc.h"
52 #include "c-family/c-ubsan.h"
53 #include "params.h"
55 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
56 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
57 static tree pfn_from_ptrmemfunc (tree);
58 static tree delta_from_ptrmemfunc (tree);
59 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
60 tsubst_flags_t, int);
61 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
62 static tree rationalize_conditional_expr (enum tree_code, tree,
63 tsubst_flags_t);
64 static int comp_ptr_ttypes_real (tree, tree, int);
65 static bool comp_except_types (tree, tree, bool);
66 static bool comp_array_types (const_tree, const_tree, bool);
67 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
68 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
69 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
70 static bool casts_away_constness (tree, tree, tsubst_flags_t);
71 static bool maybe_warn_about_returning_address_of_local (tree);
72 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
73 static void warn_args_num (location_t, tree, bool);
74 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
75 tsubst_flags_t);
77 /* Do `exp = require_complete_type (exp);' to make sure exp
78 does not have an incomplete type. (That includes void types.)
79 Returns error_mark_node if the VALUE does not have
80 complete type when this function returns. */
82 tree
83 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
85 tree type;
87 if (processing_template_decl || value == error_mark_node)
88 return value;
90 if (TREE_CODE (value) == OVERLOAD)
91 type = unknown_type_node;
92 else
93 type = TREE_TYPE (value);
95 if (type == error_mark_node)
96 return error_mark_node;
98 /* First, detect a valid value with a complete type. */
99 if (COMPLETE_TYPE_P (type))
100 return value;
102 if (complete_type_or_maybe_complain (type, value, complain))
103 return value;
104 else
105 return error_mark_node;
108 tree
109 require_complete_type (tree value)
111 return require_complete_type_sfinae (value, tf_warning_or_error);
114 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
115 a template instantiation, do the instantiation. Returns TYPE,
116 whether or not it could be completed, unless something goes
117 horribly wrong, in which case the error_mark_node is returned. */
119 tree
120 complete_type (tree type)
122 if (type == NULL_TREE)
123 /* Rather than crash, we return something sure to cause an error
124 at some point. */
125 return error_mark_node;
127 if (type == error_mark_node || COMPLETE_TYPE_P (type))
129 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
131 tree t = complete_type (TREE_TYPE (type));
132 unsigned int needs_constructing, has_nontrivial_dtor;
133 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
134 layout_type (type);
135 needs_constructing
136 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
137 has_nontrivial_dtor
138 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
139 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
141 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
142 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
145 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
146 instantiate_class_template (TYPE_MAIN_VARIANT (type));
148 return type;
151 /* Like complete_type, but issue an error if the TYPE cannot be completed.
152 VALUE is used for informative diagnostics.
153 Returns NULL_TREE if the type cannot be made complete. */
155 tree
156 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
158 type = complete_type (type);
159 if (type == error_mark_node)
160 /* We already issued an error. */
161 return NULL_TREE;
162 else if (!COMPLETE_TYPE_P (type))
164 if (complain & tf_error)
165 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
166 return NULL_TREE;
168 else
169 return type;
172 tree
173 complete_type_or_else (tree type, tree value)
175 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
178 /* Return truthvalue of whether type of EXP is instantiated. */
181 type_unknown_p (const_tree exp)
183 return (TREE_CODE (exp) == TREE_LIST
184 || TREE_TYPE (exp) == unknown_type_node);
188 /* Return the common type of two parameter lists.
189 We assume that comptypes has already been done and returned 1;
190 if that isn't so, this may crash.
192 As an optimization, free the space we allocate if the parameter
193 lists are already common. */
195 static tree
196 commonparms (tree p1, tree p2)
198 tree oldargs = p1, newargs, n;
199 int i, len;
200 int any_change = 0;
202 len = list_length (p1);
203 newargs = tree_last (p1);
205 if (newargs == void_list_node)
206 i = 1;
207 else
209 i = 0;
210 newargs = 0;
213 for (; i < len; i++)
214 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
216 n = newargs;
218 for (i = 0; p1;
219 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
221 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
223 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
224 any_change = 1;
226 else if (! TREE_PURPOSE (p1))
228 if (TREE_PURPOSE (p2))
230 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
231 any_change = 1;
234 else
236 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
237 any_change = 1;
238 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
240 if (TREE_VALUE (p1) != TREE_VALUE (p2))
242 any_change = 1;
243 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
245 else
246 TREE_VALUE (n) = TREE_VALUE (p1);
248 if (! any_change)
249 return oldargs;
251 return newargs;
254 /* Given a type, perhaps copied for a typedef,
255 find the "original" version of it. */
256 static tree
257 original_type (tree t)
259 int quals = cp_type_quals (t);
260 while (t != error_mark_node
261 && TYPE_NAME (t) != NULL_TREE)
263 tree x = TYPE_NAME (t);
264 if (TREE_CODE (x) != TYPE_DECL)
265 break;
266 x = DECL_ORIGINAL_TYPE (x);
267 if (x == NULL_TREE)
268 break;
269 t = x;
271 return cp_build_qualified_type (t, quals);
274 /* Return the common type for two arithmetic types T1 and T2 under the
275 usual arithmetic conversions. The default conversions have already
276 been applied, and enumerated types converted to their compatible
277 integer types. */
279 static tree
280 cp_common_type (tree t1, tree t2)
282 enum tree_code code1 = TREE_CODE (t1);
283 enum tree_code code2 = TREE_CODE (t2);
284 tree attributes;
285 int i;
288 /* In what follows, we slightly generalize the rules given in [expr] so
289 as to deal with `long long' and `complex'. First, merge the
290 attributes. */
291 attributes = (*targetm.merge_type_attributes) (t1, t2);
293 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
295 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
296 return build_type_attribute_variant (t1, attributes);
297 else
298 return NULL_TREE;
301 /* FIXME: Attributes. */
302 gcc_assert (ARITHMETIC_TYPE_P (t1)
303 || TREE_CODE (t1) == VECTOR_TYPE
304 || UNSCOPED_ENUM_P (t1));
305 gcc_assert (ARITHMETIC_TYPE_P (t2)
306 || TREE_CODE (t2) == VECTOR_TYPE
307 || UNSCOPED_ENUM_P (t2));
309 /* If one type is complex, form the common type of the non-complex
310 components, then make that complex. Use T1 or T2 if it is the
311 required type. */
312 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
314 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
315 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
316 tree subtype
317 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
319 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
320 return build_type_attribute_variant (t1, attributes);
321 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
322 return build_type_attribute_variant (t2, attributes);
323 else
324 return build_type_attribute_variant (build_complex_type (subtype),
325 attributes);
328 if (code1 == VECTOR_TYPE)
330 /* When we get here we should have two vectors of the same size.
331 Just prefer the unsigned one if present. */
332 if (TYPE_UNSIGNED (t1))
333 return build_type_attribute_variant (t1, attributes);
334 else
335 return build_type_attribute_variant (t2, attributes);
338 /* If only one is real, use it as the result. */
339 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
340 return build_type_attribute_variant (t1, attributes);
341 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
342 return build_type_attribute_variant (t2, attributes);
344 /* Both real or both integers; use the one with greater precision. */
345 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
346 return build_type_attribute_variant (t1, attributes);
347 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
348 return build_type_attribute_variant (t2, attributes);
350 /* The types are the same; no need to do anything fancy. */
351 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
352 return build_type_attribute_variant (t1, attributes);
354 if (code1 != REAL_TYPE)
356 /* If one is unsigned long long, then convert the other to unsigned
357 long long. */
358 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
359 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
360 return build_type_attribute_variant (long_long_unsigned_type_node,
361 attributes);
362 /* If one is a long long, and the other is an unsigned long, and
363 long long can represent all the values of an unsigned long, then
364 convert to a long long. Otherwise, convert to an unsigned long
365 long. Otherwise, if either operand is long long, convert the
366 other to long long.
368 Since we're here, we know the TYPE_PRECISION is the same;
369 therefore converting to long long cannot represent all the values
370 of an unsigned long, so we choose unsigned long long in that
371 case. */
372 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
373 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
375 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
376 ? long_long_unsigned_type_node
377 : long_long_integer_type_node);
378 return build_type_attribute_variant (t, attributes);
381 /* Go through the same procedure, but for longs. */
382 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
383 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
384 return build_type_attribute_variant (long_unsigned_type_node,
385 attributes);
386 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
387 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
389 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
390 ? long_unsigned_type_node : long_integer_type_node);
391 return build_type_attribute_variant (t, attributes);
394 /* For __intN types, either the type is __int128 (and is lower
395 priority than the types checked above, but higher than other
396 128-bit types) or it's known to not be the same size as other
397 types (enforced in toplev.c). Prefer the unsigned type. */
398 for (i = 0; i < NUM_INT_N_ENTS; i ++)
400 if (int_n_enabled_p [i]
401 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
402 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
403 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
406 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
407 ? int_n_trees[i].unsigned_type
408 : int_n_trees[i].signed_type);
409 return build_type_attribute_variant (t, attributes);
413 /* Otherwise prefer the unsigned one. */
414 if (TYPE_UNSIGNED (t1))
415 return build_type_attribute_variant (t1, attributes);
416 else
417 return build_type_attribute_variant (t2, attributes);
419 else
421 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
422 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
423 return build_type_attribute_variant (long_double_type_node,
424 attributes);
425 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
426 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
427 return build_type_attribute_variant (double_type_node,
428 attributes);
429 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
430 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
431 return build_type_attribute_variant (float_type_node,
432 attributes);
434 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
435 the standard C++ floating-point types. Logic earlier in this
436 function has already eliminated the possibility that
437 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
438 compelling reason to choose one or the other. */
439 return build_type_attribute_variant (t1, attributes);
443 /* T1 and T2 are arithmetic or enumeration types. Return the type
444 that will result from the "usual arithmetic conversions" on T1 and
445 T2 as described in [expr]. */
447 tree
448 type_after_usual_arithmetic_conversions (tree t1, tree t2)
450 gcc_assert (ARITHMETIC_TYPE_P (t1)
451 || TREE_CODE (t1) == VECTOR_TYPE
452 || UNSCOPED_ENUM_P (t1));
453 gcc_assert (ARITHMETIC_TYPE_P (t2)
454 || TREE_CODE (t2) == VECTOR_TYPE
455 || UNSCOPED_ENUM_P (t2));
457 /* Perform the integral promotions. We do not promote real types here. */
458 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
459 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
461 t1 = type_promotes_to (t1);
462 t2 = type_promotes_to (t2);
465 return cp_common_type (t1, t2);
468 static void
469 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
470 composite_pointer_operation operation)
472 switch (operation)
474 case CPO_COMPARISON:
475 emit_diagnostic (kind, input_location, 0,
476 "comparison between "
477 "distinct pointer types %qT and %qT lacks a cast",
478 t1, t2);
479 break;
480 case CPO_CONVERSION:
481 emit_diagnostic (kind, input_location, 0,
482 "conversion between "
483 "distinct pointer types %qT and %qT lacks a cast",
484 t1, t2);
485 break;
486 case CPO_CONDITIONAL_EXPR:
487 emit_diagnostic (kind, input_location, 0,
488 "conditional expression between "
489 "distinct pointer types %qT and %qT lacks a cast",
490 t1, t2);
491 break;
492 default:
493 gcc_unreachable ();
497 /* Subroutine of composite_pointer_type to implement the recursive
498 case. See that function for documentation of the parameters. */
500 static tree
501 composite_pointer_type_r (tree t1, tree t2,
502 composite_pointer_operation operation,
503 tsubst_flags_t complain)
505 tree pointee1;
506 tree pointee2;
507 tree result_type;
508 tree attributes;
510 /* Determine the types pointed to by T1 and T2. */
511 if (TYPE_PTR_P (t1))
513 pointee1 = TREE_TYPE (t1);
514 pointee2 = TREE_TYPE (t2);
516 else
518 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
519 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
522 /* [expr.rel]
524 Otherwise, the composite pointer type is a pointer type
525 similar (_conv.qual_) to the type of one of the operands,
526 with a cv-qualification signature (_conv.qual_) that is the
527 union of the cv-qualification signatures of the operand
528 types. */
529 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
530 result_type = pointee1;
531 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
532 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
534 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
535 complain);
536 if (result_type == error_mark_node)
537 return error_mark_node;
539 else
541 if (complain & tf_error)
542 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
543 else
544 return error_mark_node;
545 result_type = void_type_node;
547 result_type = cp_build_qualified_type (result_type,
548 (cp_type_quals (pointee1)
549 | cp_type_quals (pointee2)));
550 /* If the original types were pointers to members, so is the
551 result. */
552 if (TYPE_PTRMEM_P (t1))
554 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
555 TYPE_PTRMEM_CLASS_TYPE (t2)))
557 if (complain & tf_error)
558 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
559 else
560 return error_mark_node;
562 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
563 result_type);
565 else
566 result_type = build_pointer_type (result_type);
568 /* Merge the attributes. */
569 attributes = (*targetm.merge_type_attributes) (t1, t2);
570 return build_type_attribute_variant (result_type, attributes);
573 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
574 ARG1 and ARG2 are the values with those types. The OPERATION is to
575 describe the operation between the pointer types,
576 in case an error occurs.
578 This routine also implements the computation of a common type for
579 pointers-to-members as per [expr.eq]. */
581 tree
582 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
583 composite_pointer_operation operation,
584 tsubst_flags_t complain)
586 tree class1;
587 tree class2;
589 /* [expr.rel]
591 If one operand is a null pointer constant, the composite pointer
592 type is the type of the other operand. */
593 if (null_ptr_cst_p (arg1))
594 return t2;
595 if (null_ptr_cst_p (arg2))
596 return t1;
598 /* We have:
600 [expr.rel]
602 If one of the operands has type "pointer to cv1 void*", then
603 the other has type "pointer to cv2T", and the composite pointer
604 type is "pointer to cv12 void", where cv12 is the union of cv1
605 and cv2.
607 If either type is a pointer to void, make sure it is T1. */
608 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
610 tree t;
611 t = t1;
612 t1 = t2;
613 t2 = t;
616 /* Now, if T1 is a pointer to void, merge the qualifiers. */
617 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
619 tree attributes;
620 tree result_type;
622 if (TYPE_PTRFN_P (t2))
624 if (complain & tf_error)
626 switch (operation)
628 case CPO_COMPARISON:
629 pedwarn (input_location, OPT_Wpedantic,
630 "ISO C++ forbids comparison between pointer "
631 "of type %<void *%> and pointer-to-function");
632 break;
633 case CPO_CONVERSION:
634 pedwarn (input_location, OPT_Wpedantic,
635 "ISO C++ forbids conversion between pointer "
636 "of type %<void *%> and pointer-to-function");
637 break;
638 case CPO_CONDITIONAL_EXPR:
639 pedwarn (input_location, OPT_Wpedantic,
640 "ISO C++ forbids conditional expression between "
641 "pointer of type %<void *%> and "
642 "pointer-to-function");
643 break;
644 default:
645 gcc_unreachable ();
648 else
649 return error_mark_node;
651 result_type
652 = cp_build_qualified_type (void_type_node,
653 (cp_type_quals (TREE_TYPE (t1))
654 | cp_type_quals (TREE_TYPE (t2))));
655 result_type = build_pointer_type (result_type);
656 /* Merge the attributes. */
657 attributes = (*targetm.merge_type_attributes) (t1, t2);
658 return build_type_attribute_variant (result_type, attributes);
661 if (c_dialect_objc () && TYPE_PTR_P (t1)
662 && TYPE_PTR_P (t2))
664 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
665 return objc_common_type (t1, t2);
668 /* [expr.eq] permits the application of a pointer conversion to
669 bring the pointers to a common type. */
670 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
671 && CLASS_TYPE_P (TREE_TYPE (t1))
672 && CLASS_TYPE_P (TREE_TYPE (t2))
673 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
674 TREE_TYPE (t2)))
676 class1 = TREE_TYPE (t1);
677 class2 = TREE_TYPE (t2);
679 if (DERIVED_FROM_P (class1, class2))
680 t2 = (build_pointer_type
681 (cp_build_qualified_type (class1, cp_type_quals (class2))));
682 else if (DERIVED_FROM_P (class2, class1))
683 t1 = (build_pointer_type
684 (cp_build_qualified_type (class2, cp_type_quals (class1))));
685 else
687 if (complain & tf_error)
688 composite_pointer_error (DK_ERROR, t1, t2, operation);
689 return error_mark_node;
692 /* [expr.eq] permits the application of a pointer-to-member
693 conversion to change the class type of one of the types. */
694 else if (TYPE_PTRMEM_P (t1)
695 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
696 TYPE_PTRMEM_CLASS_TYPE (t2)))
698 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
699 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
701 if (DERIVED_FROM_P (class1, class2))
702 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
703 else if (DERIVED_FROM_P (class2, class1))
704 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
705 else
707 if (complain & tf_error)
708 switch (operation)
710 case CPO_COMPARISON:
711 error ("comparison between distinct "
712 "pointer-to-member types %qT and %qT lacks a cast",
713 t1, t2);
714 break;
715 case CPO_CONVERSION:
716 error ("conversion between distinct "
717 "pointer-to-member types %qT and %qT lacks a cast",
718 t1, t2);
719 break;
720 case CPO_CONDITIONAL_EXPR:
721 error ("conditional expression between distinct "
722 "pointer-to-member types %qT and %qT lacks a cast",
723 t1, t2);
724 break;
725 default:
726 gcc_unreachable ();
728 return error_mark_node;
732 return composite_pointer_type_r (t1, t2, operation, complain);
735 /* Return the merged type of two types.
736 We assume that comptypes has already been done and returned 1;
737 if that isn't so, this may crash.
739 This just combines attributes and default arguments; any other
740 differences would cause the two types to compare unalike. */
742 tree
743 merge_types (tree t1, tree t2)
745 enum tree_code code1;
746 enum tree_code code2;
747 tree attributes;
749 /* Save time if the two types are the same. */
750 if (t1 == t2)
751 return t1;
752 if (original_type (t1) == original_type (t2))
753 return t1;
755 /* If one type is nonsense, use the other. */
756 if (t1 == error_mark_node)
757 return t2;
758 if (t2 == error_mark_node)
759 return t1;
761 /* Handle merging an auto redeclaration with a previous deduced
762 return type. */
763 if (is_auto (t1))
764 return t2;
766 /* Merge the attributes. */
767 attributes = (*targetm.merge_type_attributes) (t1, t2);
769 if (TYPE_PTRMEMFUNC_P (t1))
770 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
771 if (TYPE_PTRMEMFUNC_P (t2))
772 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
774 code1 = TREE_CODE (t1);
775 code2 = TREE_CODE (t2);
776 if (code1 != code2)
778 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
779 if (code1 == TYPENAME_TYPE)
781 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
782 code1 = TREE_CODE (t1);
784 else
786 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
787 code2 = TREE_CODE (t2);
791 switch (code1)
793 case POINTER_TYPE:
794 case REFERENCE_TYPE:
795 /* For two pointers, do this recursively on the target type. */
797 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
798 int quals = cp_type_quals (t1);
800 if (code1 == POINTER_TYPE)
801 t1 = build_pointer_type (target);
802 else
803 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
804 t1 = build_type_attribute_variant (t1, attributes);
805 t1 = cp_build_qualified_type (t1, quals);
807 if (TREE_CODE (target) == METHOD_TYPE)
808 t1 = build_ptrmemfunc_type (t1);
810 return t1;
813 case OFFSET_TYPE:
815 int quals;
816 tree pointee;
817 quals = cp_type_quals (t1);
818 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
819 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
820 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
821 pointee);
822 t1 = cp_build_qualified_type (t1, quals);
823 break;
826 case ARRAY_TYPE:
828 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
829 /* Save space: see if the result is identical to one of the args. */
830 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
831 return build_type_attribute_variant (t1, attributes);
832 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
833 return build_type_attribute_variant (t2, attributes);
834 /* Merge the element types, and have a size if either arg has one. */
835 t1 = build_cplus_array_type
836 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
837 break;
840 case FUNCTION_TYPE:
841 /* Function types: prefer the one that specified arg types.
842 If both do, merge the arg types. Also merge the return types. */
844 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
845 tree p1 = TYPE_ARG_TYPES (t1);
846 tree p2 = TYPE_ARG_TYPES (t2);
847 tree parms;
848 tree rval, raises;
849 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
851 /* Save space: see if the result is identical to one of the args. */
852 if (valtype == TREE_TYPE (t1) && ! p2)
853 return cp_build_type_attribute_variant (t1, attributes);
854 if (valtype == TREE_TYPE (t2) && ! p1)
855 return cp_build_type_attribute_variant (t2, attributes);
857 /* Simple way if one arg fails to specify argument types. */
858 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
859 parms = p2;
860 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
861 parms = p1;
862 else
863 parms = commonparms (p1, p2);
865 rval = build_function_type (valtype, parms);
866 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
867 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
868 rval = apply_memfn_quals (rval,
869 type_memfn_quals (t1),
870 type_memfn_rqual (t1));
871 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
872 TYPE_RAISES_EXCEPTIONS (t2));
873 t1 = build_exception_variant (rval, raises);
874 if (late_return_type_p)
875 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
876 break;
879 case METHOD_TYPE:
881 /* Get this value the long way, since TYPE_METHOD_BASETYPE
882 is just the main variant of this. */
883 tree basetype = class_of_this_parm (t2);
884 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
885 TYPE_RAISES_EXCEPTIONS (t2));
886 cp_ref_qualifier rqual = type_memfn_rqual (t1);
887 tree t3;
888 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
889 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
891 /* If this was a member function type, get back to the
892 original type of type member function (i.e., without
893 the class instance variable up front. */
894 t1 = build_function_type (TREE_TYPE (t1),
895 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
896 t2 = build_function_type (TREE_TYPE (t2),
897 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
898 t3 = merge_types (t1, t2);
899 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
900 TYPE_ARG_TYPES (t3));
901 t1 = build_exception_variant (t3, raises);
902 t1 = build_ref_qualified_type (t1, rqual);
903 if (late_return_type_1_p)
904 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
905 if (late_return_type_2_p)
906 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
907 break;
910 case TYPENAME_TYPE:
911 /* There is no need to merge attributes into a TYPENAME_TYPE.
912 When the type is instantiated it will have whatever
913 attributes result from the instantiation. */
914 return t1;
916 default:;
919 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
920 return t1;
921 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
922 return t2;
923 else
924 return cp_build_type_attribute_variant (t1, attributes);
927 /* Return the ARRAY_TYPE type without its domain. */
929 tree
930 strip_array_domain (tree type)
932 tree t2;
933 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
934 if (TYPE_DOMAIN (type) == NULL_TREE)
935 return type;
936 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
937 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
940 /* Wrapper around cp_common_type that is used by c-common.c and other
941 front end optimizations that remove promotions.
943 Return the common type for two arithmetic types T1 and T2 under the
944 usual arithmetic conversions. The default conversions have already
945 been applied, and enumerated types converted to their compatible
946 integer types. */
948 tree
949 common_type (tree t1, tree t2)
951 /* If one type is nonsense, use the other */
952 if (t1 == error_mark_node)
953 return t2;
954 if (t2 == error_mark_node)
955 return t1;
957 return cp_common_type (t1, t2);
960 /* Return the common type of two pointer types T1 and T2. This is the
961 type for the result of most arithmetic operations if the operands
962 have the given two types.
964 We assume that comp_target_types has already been done and returned
965 nonzero; if that isn't so, this may crash. */
967 tree
968 common_pointer_type (tree t1, tree t2)
970 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
971 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
972 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
974 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
975 CPO_CONVERSION, tf_warning_or_error);
978 /* Compare two exception specifier types for exactness or subsetness, if
979 allowed. Returns false for mismatch, true for match (same, or
980 derived and !exact).
982 [except.spec] "If a class X ... objects of class X or any class publicly
983 and unambiguously derived from X. Similarly, if a pointer type Y * ...
984 exceptions of type Y * or that are pointers to any type publicly and
985 unambiguously derived from Y. Otherwise a function only allows exceptions
986 that have the same type ..."
987 This does not mention cv qualifiers and is different to what throw
988 [except.throw] and catch [except.catch] will do. They will ignore the
989 top level cv qualifiers, and allow qualifiers in the pointer to class
990 example.
992 We implement the letter of the standard. */
994 static bool
995 comp_except_types (tree a, tree b, bool exact)
997 if (same_type_p (a, b))
998 return true;
999 else if (!exact)
1001 if (cp_type_quals (a) || cp_type_quals (b))
1002 return false;
1004 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1006 a = TREE_TYPE (a);
1007 b = TREE_TYPE (b);
1008 if (cp_type_quals (a) || cp_type_quals (b))
1009 return false;
1012 if (TREE_CODE (a) != RECORD_TYPE
1013 || TREE_CODE (b) != RECORD_TYPE)
1014 return false;
1016 if (publicly_uniquely_derived_p (a, b))
1017 return true;
1019 return false;
1022 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1023 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1024 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1025 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1026 are unordered, but we've already filtered out duplicates. Most lists will
1027 be in order, we should try to make use of that. */
1029 bool
1030 comp_except_specs (const_tree t1, const_tree t2, int exact)
1032 const_tree probe;
1033 const_tree base;
1034 int length = 0;
1036 if (t1 == t2)
1037 return true;
1039 /* First handle noexcept. */
1040 if (exact < ce_exact)
1042 /* noexcept(false) is compatible with no exception-specification,
1043 and stricter than any spec. */
1044 if (t1 == noexcept_false_spec)
1045 return t2 == NULL_TREE || exact == ce_derived;
1046 /* Even a derived noexcept(false) is compatible with no
1047 exception-specification. */
1048 if (t2 == noexcept_false_spec)
1049 return t1 == NULL_TREE;
1051 /* Otherwise, if we aren't looking for an exact match, noexcept is
1052 equivalent to throw(). */
1053 if (t1 == noexcept_true_spec)
1054 t1 = empty_except_spec;
1055 if (t2 == noexcept_true_spec)
1056 t2 = empty_except_spec;
1059 /* If any noexcept is left, it is only comparable to itself;
1060 either we're looking for an exact match or we're redeclaring a
1061 template with dependent noexcept. */
1062 if ((t1 && TREE_PURPOSE (t1))
1063 || (t2 && TREE_PURPOSE (t2)))
1064 return (t1 && t2
1065 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1067 if (t1 == NULL_TREE) /* T1 is ... */
1068 return t2 == NULL_TREE || exact == ce_derived;
1069 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1070 return t2 != NULL_TREE && !TREE_VALUE (t2);
1071 if (t2 == NULL_TREE) /* T2 is ... */
1072 return false;
1073 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1074 return exact == ce_derived;
1076 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1077 Count how many we find, to determine exactness. For exact matching and
1078 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1079 O(nm). */
1080 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1082 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1084 tree a = TREE_VALUE (probe);
1085 tree b = TREE_VALUE (t2);
1087 if (comp_except_types (a, b, exact))
1089 if (probe == base && exact > ce_derived)
1090 base = TREE_CHAIN (probe);
1091 length++;
1092 break;
1095 if (probe == NULL_TREE)
1096 return false;
1098 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1101 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1102 [] can match [size]. */
1104 static bool
1105 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1107 tree d1;
1108 tree d2;
1109 tree max1, max2;
1111 if (t1 == t2)
1112 return true;
1114 /* The type of the array elements must be the same. */
1115 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1116 return false;
1118 d1 = TYPE_DOMAIN (t1);
1119 d2 = TYPE_DOMAIN (t2);
1121 if (d1 == d2)
1122 return true;
1124 /* If one of the arrays is dimensionless, and the other has a
1125 dimension, they are of different types. However, it is valid to
1126 write:
1128 extern int a[];
1129 int a[3];
1131 by [basic.link]:
1133 declarations for an array object can specify
1134 array types that differ by the presence or absence of a major
1135 array bound (_dcl.array_). */
1136 if (!d1 || !d2)
1137 return allow_redeclaration;
1139 /* Check that the dimensions are the same. */
1141 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1142 return false;
1143 max1 = TYPE_MAX_VALUE (d1);
1144 max2 = TYPE_MAX_VALUE (d2);
1146 if (!cp_tree_equal (max1, max2))
1147 return false;
1149 return true;
1152 /* Compare the relative position of T1 and T2 into their respective
1153 template parameter list.
1154 T1 and T2 must be template parameter types.
1155 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1157 static bool
1158 comp_template_parms_position (tree t1, tree t2)
1160 tree index1, index2;
1161 gcc_assert (t1 && t2
1162 && TREE_CODE (t1) == TREE_CODE (t2)
1163 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1164 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1165 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1167 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1168 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1170 /* Then compare their relative position. */
1171 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1172 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1173 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1174 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1175 return false;
1177 /* In C++14 we can end up comparing 'auto' to a normal template
1178 parameter. Don't confuse them. */
1179 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1180 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1182 return true;
1185 /* Subroutine in comptypes. */
1187 static bool
1188 structural_comptypes (tree t1, tree t2, int strict)
1190 if (t1 == t2)
1191 return true;
1193 /* Suppress errors caused by previously reported errors. */
1194 if (t1 == error_mark_node || t2 == error_mark_node)
1195 return false;
1197 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1199 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1200 current instantiation. */
1201 if (TREE_CODE (t1) == TYPENAME_TYPE)
1202 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1204 if (TREE_CODE (t2) == TYPENAME_TYPE)
1205 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1207 if (TYPE_PTRMEMFUNC_P (t1))
1208 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1209 if (TYPE_PTRMEMFUNC_P (t2))
1210 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1212 /* Different classes of types can't be compatible. */
1213 if (TREE_CODE (t1) != TREE_CODE (t2))
1214 return false;
1216 /* Qualifiers must match. For array types, we will check when we
1217 recur on the array element types. */
1218 if (TREE_CODE (t1) != ARRAY_TYPE
1219 && cp_type_quals (t1) != cp_type_quals (t2))
1220 return false;
1221 if (TREE_CODE (t1) == FUNCTION_TYPE
1222 && type_memfn_quals (t1) != type_memfn_quals (t2))
1223 return false;
1224 /* Need to check this before TYPE_MAIN_VARIANT.
1225 FIXME function qualifiers should really change the main variant. */
1226 if ((TREE_CODE (t1) == FUNCTION_TYPE
1227 || TREE_CODE (t1) == METHOD_TYPE)
1228 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1229 return false;
1230 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1231 return false;
1233 /* Allow for two different type nodes which have essentially the same
1234 definition. Note that we already checked for equality of the type
1235 qualifiers (just above). */
1237 if (TREE_CODE (t1) != ARRAY_TYPE
1238 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1239 return true;
1242 /* Compare the types. Break out if they could be the same. */
1243 switch (TREE_CODE (t1))
1245 case VOID_TYPE:
1246 case BOOLEAN_TYPE:
1247 /* All void and bool types are the same. */
1248 break;
1250 case INTEGER_TYPE:
1251 case FIXED_POINT_TYPE:
1252 case REAL_TYPE:
1253 /* With these nodes, we can't determine type equivalence by
1254 looking at what is stored in the nodes themselves, because
1255 two nodes might have different TYPE_MAIN_VARIANTs but still
1256 represent the same type. For example, wchar_t and int could
1257 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1258 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1259 and are distinct types. On the other hand, int and the
1260 following typedef
1262 typedef int INT __attribute((may_alias));
1264 have identical properties, different TYPE_MAIN_VARIANTs, but
1265 represent the same type. The canonical type system keeps
1266 track of equivalence in this case, so we fall back on it. */
1267 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1269 case TEMPLATE_TEMPLATE_PARM:
1270 case BOUND_TEMPLATE_TEMPLATE_PARM:
1271 if (!comp_template_parms_position (t1, t2))
1272 return false;
1273 if (!comp_template_parms
1274 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1275 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1276 return false;
1277 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1278 break;
1279 /* Don't check inheritance. */
1280 strict = COMPARE_STRICT;
1281 /* Fall through. */
1283 case RECORD_TYPE:
1284 case UNION_TYPE:
1285 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1286 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1287 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1288 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1289 break;
1291 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1292 break;
1293 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1294 break;
1296 return false;
1298 case OFFSET_TYPE:
1299 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1300 strict & ~COMPARE_REDECLARATION))
1301 return false;
1302 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1303 return false;
1304 break;
1306 case REFERENCE_TYPE:
1307 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1308 return false;
1309 /* fall through to checks for pointer types */
1311 case POINTER_TYPE:
1312 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1313 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1314 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1315 return false;
1316 break;
1318 case METHOD_TYPE:
1319 case FUNCTION_TYPE:
1320 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1321 return false;
1322 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1323 return false;
1324 break;
1326 case ARRAY_TYPE:
1327 /* Target types must match incl. qualifiers. */
1328 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1329 return false;
1330 break;
1332 case TEMPLATE_TYPE_PARM:
1333 /* If T1 and T2 don't have the same relative position in their
1334 template parameters set, they can't be equal. */
1335 if (!comp_template_parms_position (t1, t2))
1336 return false;
1337 break;
1339 case TYPENAME_TYPE:
1340 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1341 TYPENAME_TYPE_FULLNAME (t2)))
1342 return false;
1343 /* Qualifiers don't matter on scopes. */
1344 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1345 TYPE_CONTEXT (t2)))
1346 return false;
1347 break;
1349 case UNBOUND_CLASS_TEMPLATE:
1350 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1351 return false;
1352 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1353 return false;
1354 break;
1356 case COMPLEX_TYPE:
1357 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1358 return false;
1359 break;
1361 case VECTOR_TYPE:
1362 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1363 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1364 return false;
1365 break;
1367 case TYPE_PACK_EXPANSION:
1368 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1369 PACK_EXPANSION_PATTERN (t2))
1370 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1371 PACK_EXPANSION_EXTRA_ARGS (t2)));
1373 case DECLTYPE_TYPE:
1374 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1375 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1376 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1377 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1378 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1379 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1380 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1381 DECLTYPE_TYPE_EXPR (t2)))
1382 return false;
1383 break;
1385 case UNDERLYING_TYPE:
1386 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1387 UNDERLYING_TYPE_TYPE (t2));
1389 default:
1390 return false;
1393 /* If we get here, we know that from a target independent POV the
1394 types are the same. Make sure the target attributes are also
1395 the same. */
1396 return comp_type_attributes (t1, t2);
1399 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1400 is a bitwise-or of the COMPARE_* flags. */
1402 bool
1403 comptypes (tree t1, tree t2, int strict)
1405 if (strict == COMPARE_STRICT)
1407 if (t1 == t2)
1408 return true;
1410 if (t1 == error_mark_node || t2 == error_mark_node)
1411 return false;
1413 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1414 /* At least one of the types requires structural equality, so
1415 perform a deep check. */
1416 return structural_comptypes (t1, t2, strict);
1418 #ifdef ENABLE_CHECKING
1419 if (USE_CANONICAL_TYPES)
1421 bool result = structural_comptypes (t1, t2, strict);
1423 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1424 /* The two types are structurally equivalent, but their
1425 canonical types were different. This is a failure of the
1426 canonical type propagation code.*/
1427 internal_error
1428 ("canonical types differ for identical types %T and %T",
1429 t1, t2);
1430 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1431 /* Two types are structurally different, but the canonical
1432 types are the same. This means we were over-eager in
1433 assigning canonical types. */
1434 internal_error
1435 ("same canonical type node for different types %T and %T",
1436 t1, t2);
1438 return result;
1440 #else
1441 if (USE_CANONICAL_TYPES)
1442 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1443 #endif
1444 else
1445 return structural_comptypes (t1, t2, strict);
1447 else if (strict == COMPARE_STRUCTURAL)
1448 return structural_comptypes (t1, t2, COMPARE_STRICT);
1449 else
1450 return structural_comptypes (t1, t2, strict);
1453 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1454 top-level qualifiers. */
1456 bool
1457 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1459 if (type1 == error_mark_node || type2 == error_mark_node)
1460 return false;
1462 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1465 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1467 bool
1468 at_least_as_qualified_p (const_tree type1, const_tree type2)
1470 int q1 = cp_type_quals (type1);
1471 int q2 = cp_type_quals (type2);
1473 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1474 return (q1 & q2) == q2;
1477 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1478 more cv-qualified that TYPE1, and 0 otherwise. */
1481 comp_cv_qualification (int q1, int q2)
1483 if (q1 == q2)
1484 return 0;
1486 if ((q1 & q2) == q2)
1487 return 1;
1488 else if ((q1 & q2) == q1)
1489 return -1;
1491 return 0;
1495 comp_cv_qualification (const_tree type1, const_tree type2)
1497 int q1 = cp_type_quals (type1);
1498 int q2 = cp_type_quals (type2);
1499 return comp_cv_qualification (q1, q2);
1502 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1503 subset of the cv-qualification signature of TYPE2, and the types
1504 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1507 comp_cv_qual_signature (tree type1, tree type2)
1509 if (comp_ptr_ttypes_real (type2, type1, -1))
1510 return 1;
1511 else if (comp_ptr_ttypes_real (type1, type2, -1))
1512 return -1;
1513 else
1514 return 0;
1517 /* Subroutines of `comptypes'. */
1519 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1520 equivalent in the sense that functions with those parameter types
1521 can have equivalent types. The two lists must be equivalent,
1522 element by element. */
1524 bool
1525 compparms (const_tree parms1, const_tree parms2)
1527 const_tree t1, t2;
1529 /* An unspecified parmlist matches any specified parmlist
1530 whose argument types don't need default promotions. */
1532 for (t1 = parms1, t2 = parms2;
1533 t1 || t2;
1534 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1536 /* If one parmlist is shorter than the other,
1537 they fail to match. */
1538 if (!t1 || !t2)
1539 return false;
1540 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1541 return false;
1543 return true;
1547 /* Process a sizeof or alignof expression where the operand is a
1548 type. */
1550 tree
1551 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1553 tree value;
1554 bool dependent_p;
1556 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1557 if (type == error_mark_node)
1558 return error_mark_node;
1560 type = non_reference (type);
1561 if (TREE_CODE (type) == METHOD_TYPE)
1563 if (complain)
1564 pedwarn (input_location, OPT_Wpointer_arith,
1565 "invalid application of %qs to a member function",
1566 operator_name_info[(int) op].name);
1567 else
1568 return error_mark_node;
1569 value = size_one_node;
1572 dependent_p = dependent_type_p (type);
1573 if (!dependent_p)
1574 complete_type (type);
1575 if (dependent_p
1576 /* VLA types will have a non-constant size. In the body of an
1577 uninstantiated template, we don't need to try to compute the
1578 value, because the sizeof expression is not an integral
1579 constant expression in that case. And, if we do try to
1580 compute the value, we'll likely end up with SAVE_EXPRs, which
1581 the template substitution machinery does not expect to see. */
1582 || (processing_template_decl
1583 && COMPLETE_TYPE_P (type)
1584 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1586 value = build_min (op, size_type_node, type);
1587 TREE_READONLY (value) = 1;
1588 return value;
1591 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1592 op == SIZEOF_EXPR, false,
1593 complain);
1596 /* Return the size of the type, without producing any warnings for
1597 types whose size cannot be taken. This routine should be used only
1598 in some other routine that has already produced a diagnostic about
1599 using the size of such a type. */
1600 tree
1601 cxx_sizeof_nowarn (tree type)
1603 if (TREE_CODE (type) == FUNCTION_TYPE
1604 || VOID_TYPE_P (type)
1605 || TREE_CODE (type) == ERROR_MARK)
1606 return size_one_node;
1607 else if (!COMPLETE_TYPE_P (type))
1608 return size_zero_node;
1609 else
1610 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1613 /* Process a sizeof expression where the operand is an expression. */
1615 static tree
1616 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1618 if (e == error_mark_node)
1619 return error_mark_node;
1621 if (processing_template_decl)
1623 e = build_min (SIZEOF_EXPR, size_type_node, e);
1624 TREE_SIDE_EFFECTS (e) = 0;
1625 TREE_READONLY (e) = 1;
1627 return e;
1630 /* To get the size of a static data member declared as an array of
1631 unknown bound, we need to instantiate it. */
1632 if (VAR_P (e)
1633 && VAR_HAD_UNKNOWN_BOUND (e)
1634 && DECL_TEMPLATE_INSTANTIATION (e))
1635 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1637 if (TREE_CODE (e) == PARM_DECL
1638 && DECL_ARRAY_PARAMETER_P (e)
1639 && (complain & tf_warning))
1641 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1642 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1643 inform (DECL_SOURCE_LOCATION (e), "declared here");
1646 e = mark_type_use (e);
1648 if (TREE_CODE (e) == COMPONENT_REF
1649 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1650 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1652 if (complain & tf_error)
1653 error ("invalid application of %<sizeof%> to a bit-field");
1654 else
1655 return error_mark_node;
1656 e = char_type_node;
1658 else if (is_overloaded_fn (e))
1660 if (complain & tf_error)
1661 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1662 "function type");
1663 else
1664 return error_mark_node;
1665 e = char_type_node;
1667 else if (type_unknown_p (e))
1669 if (complain & tf_error)
1670 cxx_incomplete_type_error (e, TREE_TYPE (e));
1671 else
1672 return error_mark_node;
1673 e = char_type_node;
1675 else
1676 e = TREE_TYPE (e);
1678 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1681 /* Implement the __alignof keyword: Return the minimum required
1682 alignment of E, measured in bytes. For VAR_DECL's and
1683 FIELD_DECL's return DECL_ALIGN (which can be set from an
1684 "aligned" __attribute__ specification). */
1686 static tree
1687 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1689 tree t;
1691 if (e == error_mark_node)
1692 return error_mark_node;
1694 if (processing_template_decl)
1696 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1697 TREE_SIDE_EFFECTS (e) = 0;
1698 TREE_READONLY (e) = 1;
1700 return e;
1703 e = mark_type_use (e);
1705 if (VAR_P (e))
1706 t = size_int (DECL_ALIGN_UNIT (e));
1707 else if (TREE_CODE (e) == COMPONENT_REF
1708 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1709 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1711 if (complain & tf_error)
1712 error ("invalid application of %<__alignof%> to a bit-field");
1713 else
1714 return error_mark_node;
1715 t = size_one_node;
1717 else if (TREE_CODE (e) == COMPONENT_REF
1718 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1719 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1720 else if (is_overloaded_fn (e))
1722 if (complain & tf_error)
1723 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1724 "function type");
1725 else
1726 return error_mark_node;
1727 if (TREE_CODE (e) == FUNCTION_DECL)
1728 t = size_int (DECL_ALIGN_UNIT (e));
1729 else
1730 t = size_one_node;
1732 else if (type_unknown_p (e))
1734 if (complain & tf_error)
1735 cxx_incomplete_type_error (e, TREE_TYPE (e));
1736 else
1737 return error_mark_node;
1738 t = size_one_node;
1740 else
1741 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1742 complain & tf_error);
1744 return fold_convert (size_type_node, t);
1747 /* Process a sizeof or alignof expression E with code OP where the operand
1748 is an expression. */
1750 tree
1751 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1753 if (op == SIZEOF_EXPR)
1754 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1755 else
1756 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1759 /* Build a representation of an expression 'alignas(E).' Return the
1760 folded integer value of E if it is an integral constant expression
1761 that resolves to a valid alignment. If E depends on a template
1762 parameter, return a syntactic representation tree of kind
1763 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1764 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1766 tree
1767 cxx_alignas_expr (tree e)
1769 if (e == NULL_TREE || e == error_mark_node
1770 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1771 return e;
1773 if (TYPE_P (e))
1774 /* [dcl.align]/3:
1776 When the alignment-specifier is of the form
1777 alignas(type-id ), it shall have the same effect as
1778 alignas(alignof(type-id )). */
1780 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1782 /* If we reach this point, it means the alignas expression if of
1783 the form "alignas(assignment-expression)", so we should follow
1784 what is stated by [dcl.align]/2. */
1786 if (value_dependent_expression_p (e))
1787 /* Leave value-dependent expression alone for now. */
1788 return e;
1790 e = instantiate_non_dependent_expr (e);
1791 e = mark_rvalue_use (e);
1793 /* [dcl.align]/2 says:
1795 the assignment-expression shall be an integral constant
1796 expression. */
1798 return cxx_constant_value (e);
1802 /* EXPR is being used in a context that is not a function call.
1803 Enforce:
1805 [expr.ref]
1807 The expression can be used only as the left-hand operand of a
1808 member function call.
1810 [expr.mptr.operator]
1812 If the result of .* or ->* is a function, then that result can be
1813 used only as the operand for the function call operator ().
1815 by issuing an error message if appropriate. Returns true iff EXPR
1816 violates these rules. */
1818 bool
1819 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1821 if (expr == NULL_TREE)
1822 return false;
1823 /* Don't enforce this in MS mode. */
1824 if (flag_ms_extensions)
1825 return false;
1826 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1827 expr = get_first_fn (expr);
1828 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1830 if (complain & tf_error)
1831 error ("invalid use of non-static member function");
1832 return true;
1834 return false;
1837 /* If EXP is a reference to a bitfield, and the type of EXP does not
1838 match the declared type of the bitfield, return the declared type
1839 of the bitfield. Otherwise, return NULL_TREE. */
1841 tree
1842 is_bitfield_expr_with_lowered_type (const_tree exp)
1844 switch (TREE_CODE (exp))
1846 case COND_EXPR:
1847 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1848 ? TREE_OPERAND (exp, 1)
1849 : TREE_OPERAND (exp, 0)))
1850 return NULL_TREE;
1851 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1853 case COMPOUND_EXPR:
1854 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1856 case MODIFY_EXPR:
1857 case SAVE_EXPR:
1858 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1860 case COMPONENT_REF:
1862 tree field;
1864 field = TREE_OPERAND (exp, 1);
1865 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1866 return NULL_TREE;
1867 if (same_type_ignoring_top_level_qualifiers_p
1868 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1869 return NULL_TREE;
1870 return DECL_BIT_FIELD_TYPE (field);
1873 CASE_CONVERT:
1874 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1875 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1876 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1877 /* Fallthrough. */
1879 default:
1880 return NULL_TREE;
1884 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1885 bitfield with a lowered type, the type of EXP is returned, rather
1886 than NULL_TREE. */
1888 tree
1889 unlowered_expr_type (const_tree exp)
1891 tree type;
1892 tree etype = TREE_TYPE (exp);
1894 type = is_bitfield_expr_with_lowered_type (exp);
1895 if (type)
1896 type = cp_build_qualified_type (type, cp_type_quals (etype));
1897 else
1898 type = etype;
1900 return type;
1903 /* Perform the conversions in [expr] that apply when an lvalue appears
1904 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1905 function-to-pointer conversions. In addition, manifest constants
1906 are replaced by their values, and bitfield references are converted
1907 to their declared types. Note that this function does not perform the
1908 lvalue-to-rvalue conversion for class types. If you need that conversion
1909 to for class types, then you probably need to use force_rvalue.
1911 Although the returned value is being used as an rvalue, this
1912 function does not wrap the returned expression in a
1913 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1914 that the return value is no longer an lvalue. */
1916 tree
1917 decay_conversion (tree exp, tsubst_flags_t complain)
1919 tree type;
1920 enum tree_code code;
1921 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1923 type = TREE_TYPE (exp);
1924 if (type == error_mark_node)
1925 return error_mark_node;
1927 exp = mark_rvalue_use (exp);
1929 exp = resolve_nondeduced_context (exp);
1930 if (type_unknown_p (exp))
1932 if (complain & tf_error)
1933 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1934 return error_mark_node;
1937 code = TREE_CODE (type);
1939 /* FIXME remove for delayed folding. */
1940 exp = scalar_constant_value (exp);
1941 if (error_operand_p (exp))
1942 return error_mark_node;
1944 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1945 return nullptr_node;
1947 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1948 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1949 if (code == VOID_TYPE)
1951 if (complain & tf_error)
1952 error_at (loc, "void value not ignored as it ought to be");
1953 return error_mark_node;
1955 if (invalid_nonstatic_memfn_p (exp, complain))
1956 return error_mark_node;
1957 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1958 return cp_build_addr_expr (exp, complain);
1959 if (code == ARRAY_TYPE)
1961 tree adr;
1962 tree ptrtype;
1964 if (INDIRECT_REF_P (exp))
1965 return build_nop (build_pointer_type (TREE_TYPE (type)),
1966 TREE_OPERAND (exp, 0));
1968 if (TREE_CODE (exp) == COMPOUND_EXPR)
1970 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1971 if (op1 == error_mark_node)
1972 return error_mark_node;
1973 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1974 TREE_OPERAND (exp, 0), op1);
1977 if (!lvalue_p (exp)
1978 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1980 if (complain & tf_error)
1981 error_at (loc, "invalid use of non-lvalue array");
1982 return error_mark_node;
1985 /* Don't let an array compound literal decay to a pointer. It can
1986 still be used to initialize an array or bind to a reference. */
1987 if (TREE_CODE (exp) == TARGET_EXPR)
1989 if (complain & tf_error)
1990 error_at (loc, "taking address of temporary array");
1991 return error_mark_node;
1994 ptrtype = build_pointer_type (TREE_TYPE (type));
1996 if (VAR_P (exp))
1998 if (!cxx_mark_addressable (exp))
1999 return error_mark_node;
2000 adr = build_nop (ptrtype, build_address (exp));
2001 return adr;
2003 /* This way is better for a COMPONENT_REF since it can
2004 simplify the offset for a component. */
2005 adr = cp_build_addr_expr (exp, complain);
2006 return cp_convert (ptrtype, adr, complain);
2009 /* If a bitfield is used in a context where integral promotion
2010 applies, then the caller is expected to have used
2011 default_conversion. That function promotes bitfields correctly
2012 before calling this function. At this point, if we have a
2013 bitfield referenced, we may assume that is not subject to
2014 promotion, and that, therefore, the type of the resulting rvalue
2015 is the declared type of the bitfield. */
2016 exp = convert_bitfield_to_declared_type (exp);
2018 /* We do not call rvalue() here because we do not want to wrap EXP
2019 in a NON_LVALUE_EXPR. */
2021 /* [basic.lval]
2023 Non-class rvalues always have cv-unqualified types. */
2024 type = TREE_TYPE (exp);
2025 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2026 exp = build_nop (cv_unqualified (type), exp);
2028 return exp;
2031 /* Perform preparatory conversions, as part of the "usual arithmetic
2032 conversions". In particular, as per [expr]:
2034 Whenever an lvalue expression appears as an operand of an
2035 operator that expects the rvalue for that operand, the
2036 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2037 standard conversions are applied to convert the expression to an
2038 rvalue.
2040 In addition, we perform integral promotions here, as those are
2041 applied to both operands to a binary operator before determining
2042 what additional conversions should apply. */
2044 static tree
2045 cp_default_conversion (tree exp, tsubst_flags_t complain)
2047 /* Check for target-specific promotions. */
2048 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2049 if (promoted_type)
2050 exp = cp_convert (promoted_type, exp, complain);
2051 /* Perform the integral promotions first so that bitfield
2052 expressions (which may promote to "int", even if the bitfield is
2053 declared "unsigned") are promoted correctly. */
2054 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2055 exp = cp_perform_integral_promotions (exp, complain);
2056 /* Perform the other conversions. */
2057 exp = decay_conversion (exp, complain);
2059 return exp;
2062 /* C version. */
2064 tree
2065 default_conversion (tree exp)
2067 return cp_default_conversion (exp, tf_warning_or_error);
2070 /* EXPR is an expression with an integral or enumeration type.
2071 Perform the integral promotions in [conv.prom], and return the
2072 converted value. */
2074 tree
2075 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2077 tree type;
2078 tree promoted_type;
2080 expr = mark_rvalue_use (expr);
2082 /* [conv.prom]
2084 If the bitfield has an enumerated type, it is treated as any
2085 other value of that type for promotion purposes. */
2086 type = is_bitfield_expr_with_lowered_type (expr);
2087 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2088 type = TREE_TYPE (expr);
2089 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2090 /* Scoped enums don't promote. */
2091 if (SCOPED_ENUM_P (type))
2092 return expr;
2093 promoted_type = type_promotes_to (type);
2094 if (type != promoted_type)
2095 expr = cp_convert (promoted_type, expr, complain);
2096 return expr;
2099 /* C version. */
2101 tree
2102 perform_integral_promotions (tree expr)
2104 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2107 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2108 decay_conversion to one. */
2111 string_conv_p (const_tree totype, const_tree exp, int warn)
2113 tree t;
2115 if (!TYPE_PTR_P (totype))
2116 return 0;
2118 t = TREE_TYPE (totype);
2119 if (!same_type_p (t, char_type_node)
2120 && !same_type_p (t, char16_type_node)
2121 && !same_type_p (t, char32_type_node)
2122 && !same_type_p (t, wchar_type_node))
2123 return 0;
2125 if (TREE_CODE (exp) == STRING_CST)
2127 /* Make sure that we don't try to convert between char and wide chars. */
2128 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2129 return 0;
2131 else
2133 /* Is this a string constant which has decayed to 'const char *'? */
2134 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2135 if (!same_type_p (TREE_TYPE (exp), t))
2136 return 0;
2137 STRIP_NOPS (exp);
2138 if (TREE_CODE (exp) != ADDR_EXPR
2139 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2140 return 0;
2142 if (warn)
2144 if (cxx_dialect >= cxx11)
2145 pedwarn (input_location,
2146 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
2147 "ISO C++ forbids converting a string constant to %qT",
2148 totype);
2149 else
2150 warning (OPT_Wwrite_strings,
2151 "deprecated conversion from string constant to %qT",
2152 totype);
2155 return 1;
2158 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2159 can, for example, use as an lvalue. This code used to be in
2160 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2161 expressions, where we're dealing with aggregates. But now it's again only
2162 called from unary_complex_lvalue. The case (in particular) that led to
2163 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2164 get it there. */
2166 static tree
2167 rationalize_conditional_expr (enum tree_code code, tree t,
2168 tsubst_flags_t complain)
2170 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2172 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2173 the first operand is always the one to be used if both operands
2174 are equal, so we know what conditional expression this used to be. */
2175 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2177 tree op0 = TREE_OPERAND (t, 0);
2178 tree op1 = TREE_OPERAND (t, 1);
2180 /* The following code is incorrect if either operand side-effects. */
2181 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2182 && !TREE_SIDE_EFFECTS (op1));
2183 return
2184 build_conditional_expr (loc,
2185 build_x_binary_op (loc,
2186 (TREE_CODE (t) == MIN_EXPR
2187 ? LE_EXPR : GE_EXPR),
2188 op0, TREE_CODE (op0),
2189 op1, TREE_CODE (op1),
2190 /*overload=*/NULL,
2191 complain),
2192 cp_build_unary_op (code, op0, 0, complain),
2193 cp_build_unary_op (code, op1, 0, complain),
2194 complain);
2197 return
2198 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2199 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2200 complain),
2201 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2202 complain),
2203 complain);
2206 /* Given the TYPE of an anonymous union field inside T, return the
2207 FIELD_DECL for the field. If not found return NULL_TREE. Because
2208 anonymous unions can nest, we must also search all anonymous unions
2209 that are directly reachable. */
2211 tree
2212 lookup_anon_field (tree t, tree type)
2214 tree field;
2216 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2218 if (TREE_STATIC (field))
2219 continue;
2220 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2221 continue;
2223 /* If we find it directly, return the field. */
2224 if (DECL_NAME (field) == NULL_TREE
2225 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2227 return field;
2230 /* Otherwise, it could be nested, search harder. */
2231 if (DECL_NAME (field) == NULL_TREE
2232 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2234 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2235 if (subfield)
2236 return subfield;
2239 return NULL_TREE;
2242 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2243 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2244 non-NULL, it indicates the path to the base used to name MEMBER.
2245 If PRESERVE_REFERENCE is true, the expression returned will have
2246 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2247 returned will have the type referred to by the reference.
2249 This function does not perform access control; that is either done
2250 earlier by the parser when the name of MEMBER is resolved to MEMBER
2251 itself, or later when overload resolution selects one of the
2252 functions indicated by MEMBER. */
2254 tree
2255 build_class_member_access_expr (tree object, tree member,
2256 tree access_path, bool preserve_reference,
2257 tsubst_flags_t complain)
2259 tree object_type;
2260 tree member_scope;
2261 tree result = NULL_TREE;
2262 tree using_decl = NULL_TREE;
2264 if (error_operand_p (object) || error_operand_p (member))
2265 return error_mark_node;
2267 gcc_assert (DECL_P (member) || BASELINK_P (member));
2269 /* [expr.ref]
2271 The type of the first expression shall be "class object" (of a
2272 complete type). */
2273 object_type = TREE_TYPE (object);
2274 if (!currently_open_class (object_type)
2275 && !complete_type_or_maybe_complain (object_type, object, complain))
2276 return error_mark_node;
2277 if (!CLASS_TYPE_P (object_type))
2279 if (complain & tf_error)
2281 if (POINTER_TYPE_P (object_type)
2282 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2283 error ("request for member %qD in %qE, which is of pointer "
2284 "type %qT (maybe you meant to use %<->%> ?)",
2285 member, object, object_type);
2286 else
2287 error ("request for member %qD in %qE, which is of non-class "
2288 "type %qT", member, object, object_type);
2290 return error_mark_node;
2293 /* The standard does not seem to actually say that MEMBER must be a
2294 member of OBJECT_TYPE. However, that is clearly what is
2295 intended. */
2296 if (DECL_P (member))
2298 member_scope = DECL_CLASS_CONTEXT (member);
2299 mark_used (member);
2300 if (TREE_DEPRECATED (member))
2301 warn_deprecated_use (member, NULL_TREE);
2303 else
2304 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2305 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2306 presently be the anonymous union. Go outwards until we find a
2307 type related to OBJECT_TYPE. */
2308 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2309 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2310 object_type))
2311 member_scope = TYPE_CONTEXT (member_scope);
2312 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2314 if (complain & tf_error)
2316 if (TREE_CODE (member) == FIELD_DECL)
2317 error ("invalid use of nonstatic data member %qE", member);
2318 else
2319 error ("%qD is not a member of %qT", member, object_type);
2321 return error_mark_node;
2324 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2325 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2326 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2328 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2329 if (temp)
2330 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2333 /* In [expr.ref], there is an explicit list of the valid choices for
2334 MEMBER. We check for each of those cases here. */
2335 if (VAR_P (member))
2337 /* A static data member. */
2338 result = member;
2339 mark_exp_read (object);
2340 /* If OBJECT has side-effects, they are supposed to occur. */
2341 if (TREE_SIDE_EFFECTS (object))
2342 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2344 else if (TREE_CODE (member) == FIELD_DECL)
2346 /* A non-static data member. */
2347 bool null_object_p;
2348 int type_quals;
2349 tree member_type;
2351 null_object_p = (INDIRECT_REF_P (object)
2352 && integer_zerop (TREE_OPERAND (object, 0)));
2354 /* Convert OBJECT to the type of MEMBER. */
2355 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2356 TYPE_MAIN_VARIANT (member_scope)))
2358 tree binfo;
2359 base_kind kind;
2361 binfo = lookup_base (access_path ? access_path : object_type,
2362 member_scope, ba_unique, &kind, complain);
2363 if (binfo == error_mark_node)
2364 return error_mark_node;
2366 /* It is invalid to try to get to a virtual base of a
2367 NULL object. The most common cause is invalid use of
2368 offsetof macro. */
2369 if (null_object_p && kind == bk_via_virtual)
2371 if (complain & tf_error)
2373 error ("invalid access to non-static data member %qD in "
2374 "virtual base of NULL object", member);
2376 return error_mark_node;
2379 /* Convert to the base. */
2380 object = build_base_path (PLUS_EXPR, object, binfo,
2381 /*nonnull=*/1, complain);
2382 /* If we found the base successfully then we should be able
2383 to convert to it successfully. */
2384 gcc_assert (object != error_mark_node);
2387 /* If MEMBER is from an anonymous aggregate, we have converted
2388 OBJECT so that it refers to the class containing the
2389 anonymous union. Generate a reference to the anonymous union
2390 itself, and recur to find MEMBER. */
2391 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2392 /* When this code is called from build_field_call, the
2393 object already has the type of the anonymous union.
2394 That is because the COMPONENT_REF was already
2395 constructed, and was then disassembled before calling
2396 build_field_call. After the function-call code is
2397 cleaned up, this waste can be eliminated. */
2398 && (!same_type_ignoring_top_level_qualifiers_p
2399 (TREE_TYPE (object), DECL_CONTEXT (member))))
2401 tree anonymous_union;
2403 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2404 DECL_CONTEXT (member));
2405 object = build_class_member_access_expr (object,
2406 anonymous_union,
2407 /*access_path=*/NULL_TREE,
2408 preserve_reference,
2409 complain);
2412 /* Compute the type of the field, as described in [expr.ref]. */
2413 type_quals = TYPE_UNQUALIFIED;
2414 member_type = TREE_TYPE (member);
2415 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2417 type_quals = (cp_type_quals (member_type)
2418 | cp_type_quals (object_type));
2420 /* A field is const (volatile) if the enclosing object, or the
2421 field itself, is const (volatile). But, a mutable field is
2422 not const, even within a const object. */
2423 if (DECL_MUTABLE_P (member))
2424 type_quals &= ~TYPE_QUAL_CONST;
2425 member_type = cp_build_qualified_type (member_type, type_quals);
2428 result = build3_loc (input_location, COMPONENT_REF, member_type,
2429 object, member, NULL_TREE);
2430 result = fold_if_not_in_template (result);
2432 /* Mark the expression const or volatile, as appropriate. Even
2433 though we've dealt with the type above, we still have to mark the
2434 expression itself. */
2435 if (type_quals & TYPE_QUAL_CONST)
2436 TREE_READONLY (result) = 1;
2437 if (type_quals & TYPE_QUAL_VOLATILE)
2438 TREE_THIS_VOLATILE (result) = 1;
2440 else if (BASELINK_P (member))
2442 /* The member is a (possibly overloaded) member function. */
2443 tree functions;
2444 tree type;
2446 /* If the MEMBER is exactly one static member function, then we
2447 know the type of the expression. Otherwise, we must wait
2448 until overload resolution has been performed. */
2449 functions = BASELINK_FUNCTIONS (member);
2450 if (TREE_CODE (functions) == FUNCTION_DECL
2451 && DECL_STATIC_FUNCTION_P (functions))
2452 type = TREE_TYPE (functions);
2453 else
2454 type = unknown_type_node;
2455 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2456 base. That will happen when the function is called. */
2457 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2459 else if (TREE_CODE (member) == CONST_DECL)
2461 /* The member is an enumerator. */
2462 result = member;
2463 /* If OBJECT has side-effects, they are supposed to occur. */
2464 if (TREE_SIDE_EFFECTS (object))
2465 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2466 object, result);
2468 else if ((using_decl = strip_using_decl (member)) != member)
2469 result = build_class_member_access_expr (object,
2470 using_decl,
2471 access_path, preserve_reference,
2472 complain);
2473 else
2475 if (complain & tf_error)
2476 error ("invalid use of %qD", member);
2477 return error_mark_node;
2480 if (!preserve_reference)
2481 /* [expr.ref]
2483 If E2 is declared to have type "reference to T", then ... the
2484 type of E1.E2 is T. */
2485 result = convert_from_reference (result);
2487 return result;
2490 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2491 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2493 static tree
2494 lookup_destructor (tree object, tree scope, tree dtor_name,
2495 tsubst_flags_t complain)
2497 tree object_type = TREE_TYPE (object);
2498 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2499 tree expr;
2501 /* We've already complained about this destructor. */
2502 if (dtor_type == error_mark_node)
2503 return error_mark_node;
2505 if (scope && !check_dtor_name (scope, dtor_type))
2507 if (complain & tf_error)
2508 error ("qualified type %qT does not match destructor name ~%qT",
2509 scope, dtor_type);
2510 return error_mark_node;
2512 if (is_auto (dtor_type))
2513 dtor_type = object_type;
2514 else if (identifier_p (dtor_type))
2516 /* In a template, names we can't find a match for are still accepted
2517 destructor names, and we check them here. */
2518 if (check_dtor_name (object_type, dtor_type))
2519 dtor_type = object_type;
2520 else
2522 if (complain & tf_error)
2523 error ("object type %qT does not match destructor name ~%qT",
2524 object_type, dtor_type);
2525 return error_mark_node;
2529 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2531 if (complain & tf_error)
2532 error ("the type being destroyed is %qT, but the destructor "
2533 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2534 return error_mark_node;
2536 expr = lookup_member (dtor_type, complete_dtor_identifier,
2537 /*protect=*/1, /*want_type=*/false,
2538 tf_warning_or_error);
2539 if (!expr)
2541 if (complain & tf_error)
2542 cxx_incomplete_type_error (dtor_name, dtor_type);
2543 return error_mark_node;
2545 expr = (adjust_result_of_qualified_name_lookup
2546 (expr, dtor_type, object_type));
2547 if (scope == NULL_TREE)
2548 /* We need to call adjust_result_of_qualified_name_lookup in case the
2549 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2550 that we still get virtual function binding. */
2551 BASELINK_QUALIFIED_P (expr) = false;
2552 return expr;
2555 /* An expression of the form "A::template B" has been resolved to
2556 DECL. Issue a diagnostic if B is not a template or template
2557 specialization. */
2559 void
2560 check_template_keyword (tree decl)
2562 /* The standard says:
2564 [temp.names]
2566 If a name prefixed by the keyword template is not a member
2567 template, the program is ill-formed.
2569 DR 228 removed the restriction that the template be a member
2570 template.
2572 DR 96, if accepted would add the further restriction that explicit
2573 template arguments must be provided if the template keyword is
2574 used, but, as of 2005-10-16, that DR is still in "drafting". If
2575 this DR is accepted, then the semantic checks here can be
2576 simplified, as the entity named must in fact be a template
2577 specialization, rather than, as at present, a set of overloaded
2578 functions containing at least one template function. */
2579 if (TREE_CODE (decl) != TEMPLATE_DECL
2580 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2582 if (!is_overloaded_fn (decl))
2583 permerror (input_location, "%qD is not a template", decl);
2584 else
2586 tree fns;
2587 fns = decl;
2588 if (BASELINK_P (fns))
2589 fns = BASELINK_FUNCTIONS (fns);
2590 while (fns)
2592 tree fn = OVL_CURRENT (fns);
2593 if (TREE_CODE (fn) == TEMPLATE_DECL
2594 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2595 break;
2596 if (TREE_CODE (fn) == FUNCTION_DECL
2597 && DECL_USE_TEMPLATE (fn)
2598 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2599 break;
2600 fns = OVL_NEXT (fns);
2602 if (!fns)
2603 permerror (input_location, "%qD is not a template", decl);
2608 /* This function is called by the parser to process a class member
2609 access expression of the form OBJECT.NAME. NAME is a node used by
2610 the parser to represent a name; it is not yet a DECL. It may,
2611 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2612 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2613 there is no reason to do the lookup twice, so the parser keeps the
2614 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2615 be a template via the use of the "A::template B" syntax. */
2617 tree
2618 finish_class_member_access_expr (tree object, tree name, bool template_p,
2619 tsubst_flags_t complain)
2621 tree expr;
2622 tree object_type;
2623 tree member;
2624 tree access_path = NULL_TREE;
2625 tree orig_object = object;
2626 tree orig_name = name;
2628 if (object == error_mark_node || name == error_mark_node)
2629 return error_mark_node;
2631 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2632 if (!objc_is_public (object, name))
2633 return error_mark_node;
2635 object_type = TREE_TYPE (object);
2637 if (processing_template_decl)
2639 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2640 dependent_type_p (object_type)
2641 /* If NAME is just an IDENTIFIER_NODE, then the expression
2642 is dependent. */
2643 || identifier_p (object)
2644 /* If NAME is "f<args>", where either 'f' or 'args' is
2645 dependent, then the expression is dependent. */
2646 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2647 && dependent_template_id_p (TREE_OPERAND (name, 0),
2648 TREE_OPERAND (name, 1)))
2649 /* If NAME is "T::X" where "T" is dependent, then the
2650 expression is dependent. */
2651 || (TREE_CODE (name) == SCOPE_REF
2652 && TYPE_P (TREE_OPERAND (name, 0))
2653 && dependent_type_p (TREE_OPERAND (name, 0))))
2654 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2655 object, name, NULL_TREE);
2656 object = build_non_dependent_expr (object);
2658 else if (c_dialect_objc ()
2659 && identifier_p (name)
2660 && (expr = objc_maybe_build_component_ref (object, name)))
2661 return expr;
2663 /* [expr.ref]
2665 The type of the first expression shall be "class object" (of a
2666 complete type). */
2667 if (!currently_open_class (object_type)
2668 && !complete_type_or_maybe_complain (object_type, object, complain))
2669 return error_mark_node;
2670 if (!CLASS_TYPE_P (object_type))
2672 if (complain & tf_error)
2674 if (POINTER_TYPE_P (object_type)
2675 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2676 error ("request for member %qD in %qE, which is of pointer "
2677 "type %qT (maybe you meant to use %<->%> ?)",
2678 name, object, object_type);
2679 else
2680 error ("request for member %qD in %qE, which is of non-class "
2681 "type %qT", name, object, object_type);
2683 return error_mark_node;
2686 if (BASELINK_P (name))
2687 /* A member function that has already been looked up. */
2688 member = name;
2689 else
2691 bool is_template_id = false;
2692 tree template_args = NULL_TREE;
2693 tree scope;
2695 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2697 is_template_id = true;
2698 template_args = TREE_OPERAND (name, 1);
2699 name = TREE_OPERAND (name, 0);
2701 if (TREE_CODE (name) == OVERLOAD)
2702 name = DECL_NAME (get_first_fn (name));
2703 else if (DECL_P (name))
2704 name = DECL_NAME (name);
2707 if (TREE_CODE (name) == SCOPE_REF)
2709 /* A qualified name. The qualifying class or namespace `S'
2710 has already been looked up; it is either a TYPE or a
2711 NAMESPACE_DECL. */
2712 scope = TREE_OPERAND (name, 0);
2713 name = TREE_OPERAND (name, 1);
2715 /* If SCOPE is a namespace, then the qualified name does not
2716 name a member of OBJECT_TYPE. */
2717 if (TREE_CODE (scope) == NAMESPACE_DECL)
2719 if (complain & tf_error)
2720 error ("%<%D::%D%> is not a member of %qT",
2721 scope, name, object_type);
2722 return error_mark_node;
2725 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2727 /* Looking up a member enumerator (c++/56793). */
2728 if (!TYPE_CLASS_SCOPE_P (scope)
2729 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2731 if (complain & tf_error)
2732 error ("%<%D::%D%> is not a member of %qT",
2733 scope, name, object_type);
2734 return error_mark_node;
2736 tree val = lookup_enumerator (scope, name);
2737 if (TREE_SIDE_EFFECTS (object))
2738 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2739 return val;
2742 gcc_assert (CLASS_TYPE_P (scope));
2743 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2745 if (constructor_name_p (name, scope))
2747 if (complain & tf_error)
2748 error ("cannot call constructor %<%T::%D%> directly",
2749 scope, name);
2750 return error_mark_node;
2753 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2754 access_path = lookup_base (object_type, scope, ba_check,
2755 NULL, complain);
2756 if (access_path == error_mark_node)
2757 return error_mark_node;
2758 if (!access_path)
2760 if (complain & tf_error)
2761 error ("%qT is not a base of %qT", scope, object_type);
2762 return error_mark_node;
2765 else
2767 scope = NULL_TREE;
2768 access_path = object_type;
2771 if (TREE_CODE (name) == BIT_NOT_EXPR)
2772 member = lookup_destructor (object, scope, name, complain);
2773 else
2775 /* Look up the member. */
2776 member = lookup_member (access_path, name, /*protect=*/1,
2777 /*want_type=*/false, complain);
2778 if (member == NULL_TREE)
2780 if (complain & tf_error)
2781 error ("%q#T has no member named %qE",
2782 TREE_CODE (access_path) == TREE_BINFO
2783 ? TREE_TYPE (access_path) : object_type, name);
2784 return error_mark_node;
2786 if (member == error_mark_node)
2787 return error_mark_node;
2790 if (is_template_id)
2792 tree templ = member;
2794 if (BASELINK_P (templ))
2795 templ = lookup_template_function (templ, template_args);
2796 else
2798 if (complain & tf_error)
2799 error ("%qD is not a member template function", name);
2800 return error_mark_node;
2805 if (TREE_DEPRECATED (member))
2806 warn_deprecated_use (member, NULL_TREE);
2808 if (template_p)
2809 check_template_keyword (member);
2811 expr = build_class_member_access_expr (object, member, access_path,
2812 /*preserve_reference=*/false,
2813 complain);
2814 if (processing_template_decl && expr != error_mark_node)
2816 if (BASELINK_P (member))
2818 if (TREE_CODE (orig_name) == SCOPE_REF)
2819 BASELINK_QUALIFIED_P (member) = 1;
2820 orig_name = member;
2822 return build_min_non_dep (COMPONENT_REF, expr,
2823 orig_object, orig_name,
2824 NULL_TREE);
2827 return expr;
2830 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2831 type. */
2833 tree
2834 build_simple_component_ref (tree object, tree member)
2836 tree type = cp_build_qualified_type (TREE_TYPE (member),
2837 cp_type_quals (TREE_TYPE (object)));
2838 return fold_build3_loc (input_location,
2839 COMPONENT_REF, type,
2840 object, member, NULL_TREE);
2843 /* Return an expression for the MEMBER_NAME field in the internal
2844 representation of PTRMEM, a pointer-to-member function. (Each
2845 pointer-to-member function type gets its own RECORD_TYPE so it is
2846 more convenient to access the fields by name than by FIELD_DECL.)
2847 This routine converts the NAME to a FIELD_DECL and then creates the
2848 node for the complete expression. */
2850 tree
2851 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2853 tree ptrmem_type;
2854 tree member;
2856 /* This code is a stripped down version of
2857 build_class_member_access_expr. It does not work to use that
2858 routine directly because it expects the object to be of class
2859 type. */
2860 ptrmem_type = TREE_TYPE (ptrmem);
2861 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2862 for (member = TYPE_FIELDS (ptrmem_type); member;
2863 member = DECL_CHAIN (member))
2864 if (DECL_NAME (member) == member_name)
2865 break;
2866 return build_simple_component_ref (ptrmem, member);
2869 /* Given an expression PTR for a pointer, return an expression
2870 for the value pointed to.
2871 ERRORSTRING is the name of the operator to appear in error messages.
2873 This function may need to overload OPERATOR_FNNAME.
2874 Must also handle REFERENCE_TYPEs for C++. */
2876 tree
2877 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2878 tsubst_flags_t complain)
2880 tree orig_expr = expr;
2881 tree rval;
2883 if (processing_template_decl)
2885 /* Retain the type if we know the operand is a pointer. */
2886 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2887 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2888 if (type_dependent_expression_p (expr))
2889 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2890 expr = build_non_dependent_expr (expr);
2893 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2894 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2895 if (!rval)
2896 rval = cp_build_indirect_ref (expr, errorstring, complain);
2898 if (processing_template_decl && rval != error_mark_node)
2899 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2900 else
2901 return rval;
2904 /* Helper function called from c-common. */
2905 tree
2906 build_indirect_ref (location_t /*loc*/,
2907 tree ptr, ref_operator errorstring)
2909 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2912 tree
2913 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2914 tsubst_flags_t complain)
2916 tree pointer, type;
2918 if (ptr == current_class_ptr
2919 || (TREE_CODE (ptr) == NOP_EXPR
2920 && TREE_OPERAND (ptr, 0) == current_class_ptr
2921 && (same_type_ignoring_top_level_qualifiers_p
2922 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2923 return current_class_ref;
2925 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2926 ? ptr : decay_conversion (ptr, complain));
2927 if (pointer == error_mark_node)
2928 return error_mark_node;
2930 type = TREE_TYPE (pointer);
2932 if (POINTER_TYPE_P (type))
2934 /* [expr.unary.op]
2936 If the type of the expression is "pointer to T," the type
2937 of the result is "T." */
2938 tree t = TREE_TYPE (type);
2940 if ((CONVERT_EXPR_P (ptr)
2941 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2942 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2944 /* If a warning is issued, mark it to avoid duplicates from
2945 the backend. This only needs to be done at
2946 warn_strict_aliasing > 2. */
2947 if (warn_strict_aliasing > 2)
2948 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2949 type, TREE_OPERAND (ptr, 0)))
2950 TREE_NO_WARNING (ptr) = 1;
2953 if (VOID_TYPE_P (t))
2955 /* A pointer to incomplete type (other than cv void) can be
2956 dereferenced [expr.unary.op]/1 */
2957 if (complain & tf_error)
2958 error ("%qT is not a pointer-to-object type", type);
2959 return error_mark_node;
2961 else if (TREE_CODE (pointer) == ADDR_EXPR
2962 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2963 /* The POINTER was something like `&x'. We simplify `*&x' to
2964 `x'. */
2965 return TREE_OPERAND (pointer, 0);
2966 else
2968 tree ref = build1 (INDIRECT_REF, t, pointer);
2970 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2971 so that we get the proper error message if the result is used
2972 to assign to. Also, &* is supposed to be a no-op. */
2973 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2974 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2975 TREE_SIDE_EFFECTS (ref)
2976 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2977 return ref;
2980 else if (!(complain & tf_error))
2981 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2983 /* `pointer' won't be an error_mark_node if we were given a
2984 pointer to member, so it's cool to check for this here. */
2985 else if (TYPE_PTRMEM_P (type))
2986 switch (errorstring)
2988 case RO_ARRAY_INDEXING:
2989 error ("invalid use of array indexing on pointer to member");
2990 break;
2991 case RO_UNARY_STAR:
2992 error ("invalid use of unary %<*%> on pointer to member");
2993 break;
2994 case RO_IMPLICIT_CONVERSION:
2995 error ("invalid use of implicit conversion on pointer to member");
2996 break;
2997 case RO_ARROW_STAR:
2998 error ("left hand operand of %<->*%> must be a pointer to class, "
2999 "but is a pointer to member of type %qT", type);
3000 break;
3001 default:
3002 gcc_unreachable ();
3004 else if (pointer != error_mark_node)
3005 invalid_indirection_error (input_location, type, errorstring);
3007 return error_mark_node;
3010 /* This handles expressions of the form "a[i]", which denotes
3011 an array reference.
3013 This is logically equivalent in C to *(a+i), but we may do it differently.
3014 If A is a variable or a member, we generate a primitive ARRAY_REF.
3015 This avoids forcing the array out of registers, and can work on
3016 arrays that are not lvalues (for example, members of structures returned
3017 by functions).
3019 If INDEX is of some user-defined type, it must be converted to
3020 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3021 will inherit the type of the array, which will be some pointer type.
3023 LOC is the location to use in building the array reference. */
3025 tree
3026 cp_build_array_ref (location_t loc, tree array, tree idx,
3027 tsubst_flags_t complain)
3029 tree ret;
3031 if (idx == 0)
3033 if (complain & tf_error)
3034 error_at (loc, "subscript missing in array reference");
3035 return error_mark_node;
3038 /* If an array's index is an array notation, then its rank cannot be
3039 greater than one. */
3040 if (flag_cilkplus && contains_array_notation_expr (idx))
3042 size_t rank = 0;
3044 /* If find_rank returns false, then it should have reported an error,
3045 thus it is unnecessary for repetition. */
3046 if (!find_rank (loc, idx, idx, true, &rank))
3047 return error_mark_node;
3048 if (rank > 1)
3050 error_at (loc, "rank of the array%'s index is greater than 1");
3051 return error_mark_node;
3054 if (TREE_TYPE (array) == error_mark_node
3055 || TREE_TYPE (idx) == error_mark_node)
3056 return error_mark_node;
3058 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3059 inside it. */
3060 switch (TREE_CODE (array))
3062 case COMPOUND_EXPR:
3064 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3065 complain);
3066 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3067 TREE_OPERAND (array, 0), value);
3068 SET_EXPR_LOCATION (ret, loc);
3069 return ret;
3072 case COND_EXPR:
3073 ret = build_conditional_expr
3074 (loc, TREE_OPERAND (array, 0),
3075 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3076 complain),
3077 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3078 complain),
3079 complain);
3080 protected_set_expr_location (ret, loc);
3081 return ret;
3083 default:
3084 break;
3087 bool non_lvalue
3088 = convert_vector_to_pointer_for_subscript (loc, &array, idx);
3090 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3092 tree rval, type;
3094 warn_array_subscript_with_type_char (loc, idx);
3096 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3098 if (complain & tf_error)
3099 error_at (loc, "array subscript is not an integer");
3100 return error_mark_node;
3103 /* Apply integral promotions *after* noticing character types.
3104 (It is unclear why we do these promotions -- the standard
3105 does not say that we should. In fact, the natural thing would
3106 seem to be to convert IDX to ptrdiff_t; we're performing
3107 pointer arithmetic.) */
3108 idx = cp_perform_integral_promotions (idx, complain);
3110 /* An array that is indexed by a non-constant
3111 cannot be stored in a register; we must be able to do
3112 address arithmetic on its address.
3113 Likewise an array of elements of variable size. */
3114 if (TREE_CODE (idx) != INTEGER_CST
3115 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3116 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3117 != INTEGER_CST)))
3119 if (!cxx_mark_addressable (array))
3120 return error_mark_node;
3123 /* An array that is indexed by a constant value which is not within
3124 the array bounds cannot be stored in a register either; because we
3125 would get a crash in store_bit_field/extract_bit_field when trying
3126 to access a non-existent part of the register. */
3127 if (TREE_CODE (idx) == INTEGER_CST
3128 && TYPE_DOMAIN (TREE_TYPE (array))
3129 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3131 if (!cxx_mark_addressable (array))
3132 return error_mark_node;
3135 if (!lvalue_p (array))
3137 if (complain & tf_error)
3138 pedwarn (loc, OPT_Wpedantic,
3139 "ISO C++ forbids subscripting non-lvalue array");
3140 else
3141 return error_mark_node;
3144 /* Note in C++ it is valid to subscript a `register' array, since
3145 it is valid to take the address of something with that
3146 storage specification. */
3147 if (extra_warnings)
3149 tree foo = array;
3150 while (TREE_CODE (foo) == COMPONENT_REF)
3151 foo = TREE_OPERAND (foo, 0);
3152 if (VAR_P (foo) && DECL_REGISTER (foo)
3153 && (complain & tf_warning))
3154 warning_at (loc, OPT_Wextra,
3155 "subscripting array declared %<register%>");
3158 type = TREE_TYPE (TREE_TYPE (array));
3159 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3160 /* Array ref is const/volatile if the array elements are
3161 or if the array is.. */
3162 TREE_READONLY (rval)
3163 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3164 TREE_SIDE_EFFECTS (rval)
3165 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3166 TREE_THIS_VOLATILE (rval)
3167 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3168 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3169 complain);
3170 protected_set_expr_location (ret, loc);
3171 if (non_lvalue)
3172 ret = non_lvalue_loc (loc, ret);
3173 return ret;
3177 tree ar = cp_default_conversion (array, complain);
3178 tree ind = cp_default_conversion (idx, complain);
3180 /* Put the integer in IND to simplify error checking. */
3181 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3183 tree temp = ar;
3184 ar = ind;
3185 ind = temp;
3188 if (ar == error_mark_node || ind == error_mark_node)
3189 return error_mark_node;
3191 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3193 if (complain & tf_error)
3194 error_at (loc, "subscripted value is neither array nor pointer");
3195 return error_mark_node;
3197 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3199 if (complain & tf_error)
3200 error_at (loc, "array subscript is not an integer");
3201 return error_mark_node;
3204 warn_array_subscript_with_type_char (loc, idx);
3206 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3207 PLUS_EXPR, ar, ind,
3208 complain),
3209 RO_ARRAY_INDEXING,
3210 complain);
3211 protected_set_expr_location (ret, loc);
3212 if (non_lvalue)
3213 ret = non_lvalue_loc (loc, ret);
3214 return ret;
3218 /* Entry point for Obj-C++. */
3220 tree
3221 build_array_ref (location_t loc, tree array, tree idx)
3223 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3226 /* Resolve a pointer to member function. INSTANCE is the object
3227 instance to use, if the member points to a virtual member.
3229 This used to avoid checking for virtual functions if basetype
3230 has no virtual functions, according to an earlier ANSI draft.
3231 With the final ISO C++ rules, such an optimization is
3232 incorrect: A pointer to a derived member can be static_cast
3233 to pointer-to-base-member, as long as the dynamic object
3234 later has the right member. So now we only do this optimization
3235 when we know the dynamic type of the object. */
3237 tree
3238 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3239 tsubst_flags_t complain)
3241 if (TREE_CODE (function) == OFFSET_REF)
3242 function = TREE_OPERAND (function, 1);
3244 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3246 tree idx, delta, e1, e2, e3, vtbl;
3247 bool nonvirtual;
3248 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3249 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3251 tree instance_ptr = *instance_ptrptr;
3252 tree instance_save_expr = 0;
3253 if (instance_ptr == error_mark_node)
3255 if (TREE_CODE (function) == PTRMEM_CST)
3257 /* Extracting the function address from a pmf is only
3258 allowed with -Wno-pmf-conversions. It only works for
3259 pmf constants. */
3260 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3261 e1 = convert (fntype, e1);
3262 return e1;
3264 else
3266 if (complain & tf_error)
3267 error ("object missing in use of %qE", function);
3268 return error_mark_node;
3272 /* True if we know that the dynamic type of the object doesn't have
3273 virtual functions, so we can assume the PFN field is a pointer. */
3274 nonvirtual = (COMPLETE_TYPE_P (basetype)
3275 && !TYPE_POLYMORPHIC_P (basetype)
3276 && resolves_to_fixed_type_p (instance_ptr, 0));
3278 /* If we don't really have an object (i.e. in an ill-formed
3279 conversion from PMF to pointer), we can't resolve virtual
3280 functions anyway. */
3281 if (!nonvirtual && is_dummy_object (instance_ptr))
3282 nonvirtual = true;
3284 if (TREE_SIDE_EFFECTS (instance_ptr))
3285 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3287 if (TREE_SIDE_EFFECTS (function))
3288 function = save_expr (function);
3290 /* Start by extracting all the information from the PMF itself. */
3291 e3 = pfn_from_ptrmemfunc (function);
3292 delta = delta_from_ptrmemfunc (function);
3293 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3294 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3296 case ptrmemfunc_vbit_in_pfn:
3297 e1 = cp_build_binary_op (input_location,
3298 BIT_AND_EXPR, idx, integer_one_node,
3299 complain);
3300 idx = cp_build_binary_op (input_location,
3301 MINUS_EXPR, idx, integer_one_node,
3302 complain);
3303 if (idx == error_mark_node)
3304 return error_mark_node;
3305 break;
3307 case ptrmemfunc_vbit_in_delta:
3308 e1 = cp_build_binary_op (input_location,
3309 BIT_AND_EXPR, delta, integer_one_node,
3310 complain);
3311 delta = cp_build_binary_op (input_location,
3312 RSHIFT_EXPR, delta, integer_one_node,
3313 complain);
3314 if (delta == error_mark_node)
3315 return error_mark_node;
3316 break;
3318 default:
3319 gcc_unreachable ();
3322 if (e1 == error_mark_node)
3323 return error_mark_node;
3325 /* Convert down to the right base before using the instance. A
3326 special case is that in a pointer to member of class C, C may
3327 be incomplete. In that case, the function will of course be
3328 a member of C, and no conversion is required. In fact,
3329 lookup_base will fail in that case, because incomplete
3330 classes do not have BINFOs. */
3331 if (!same_type_ignoring_top_level_qualifiers_p
3332 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3334 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3335 basetype, ba_check, NULL, complain);
3336 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3337 1, complain);
3338 if (instance_ptr == error_mark_node)
3339 return error_mark_node;
3341 /* ...and then the delta in the PMF. */
3342 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3344 /* Hand back the adjusted 'this' argument to our caller. */
3345 *instance_ptrptr = instance_ptr;
3347 if (nonvirtual)
3348 /* Now just return the pointer. */
3349 return e3;
3351 /* Next extract the vtable pointer from the object. */
3352 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3353 instance_ptr);
3354 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3355 if (vtbl == error_mark_node)
3356 return error_mark_node;
3358 /* Finally, extract the function pointer from the vtable. */
3359 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3360 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3361 if (e2 == error_mark_node)
3362 return error_mark_node;
3363 TREE_CONSTANT (e2) = 1;
3365 /* When using function descriptors, the address of the
3366 vtable entry is treated as a function pointer. */
3367 if (TARGET_VTABLE_USES_DESCRIPTORS)
3368 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3369 cp_build_addr_expr (e2, complain));
3371 e2 = fold_convert (TREE_TYPE (e3), e2);
3372 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3373 if (e1 == error_mark_node)
3374 return error_mark_node;
3376 /* Make sure this doesn't get evaluated first inside one of the
3377 branches of the COND_EXPR. */
3378 if (instance_save_expr)
3379 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3380 instance_save_expr, e1);
3382 function = e1;
3384 return function;
3387 /* Used by the C-common bits. */
3388 tree
3389 build_function_call (location_t /*loc*/,
3390 tree function, tree params)
3392 return cp_build_function_call (function, params, tf_warning_or_error);
3395 /* Used by the C-common bits. */
3396 tree
3397 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3398 tree function, vec<tree, va_gc> *params,
3399 vec<tree, va_gc> * /*origtypes*/)
3401 vec<tree, va_gc> *orig_params = params;
3402 tree ret = cp_build_function_call_vec (function, &params,
3403 tf_warning_or_error);
3405 /* cp_build_function_call_vec can reallocate PARAMS by adding
3406 default arguments. That should never happen here. Verify
3407 that. */
3408 gcc_assert (params == orig_params);
3410 return ret;
3413 /* Build a function call using a tree list of arguments. */
3415 static tree
3416 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3418 vec<tree, va_gc> *vec;
3419 tree ret;
3421 vec = make_tree_vector ();
3422 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3423 vec_safe_push (vec, TREE_VALUE (params));
3424 ret = cp_build_function_call_vec (function, &vec, complain);
3425 release_tree_vector (vec);
3426 return ret;
3429 /* Build a function call using varargs. */
3431 tree
3432 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3434 vec<tree, va_gc> *vec;
3435 va_list args;
3436 tree ret, t;
3438 vec = make_tree_vector ();
3439 va_start (args, complain);
3440 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3441 vec_safe_push (vec, t);
3442 va_end (args);
3443 ret = cp_build_function_call_vec (function, &vec, complain);
3444 release_tree_vector (vec);
3445 return ret;
3448 /* Build a function call using a vector of arguments. PARAMS may be
3449 NULL if there are no parameters. This changes the contents of
3450 PARAMS. */
3452 tree
3453 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3454 tsubst_flags_t complain)
3456 tree fntype, fndecl;
3457 int is_method;
3458 tree original = function;
3459 int nargs;
3460 tree *argarray;
3461 tree parm_types;
3462 vec<tree, va_gc> *allocated = NULL;
3463 tree ret;
3465 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3466 expressions, like those used for ObjC messenger dispatches. */
3467 if (params != NULL && !vec_safe_is_empty (*params))
3468 function = objc_rewrite_function_call (function, (**params)[0]);
3470 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3471 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3472 if (TREE_CODE (function) == NOP_EXPR
3473 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3474 function = TREE_OPERAND (function, 0);
3476 if (TREE_CODE (function) == FUNCTION_DECL)
3478 /* If the function is a non-template member function
3479 or a non-template friend, then we need to check the
3480 constraints.
3482 Note that if overload resolution failed with a single
3483 candidate this function will be used to explicitly diagnose
3484 the failure for the single call expression. The check is
3485 technically redundant since we also would have failed in
3486 add_function_candidate. */
3487 if (flag_concepts && !constraints_satisfied_p (function))
3489 error ("cannot call function %qD", function);
3490 location_t loc = DECL_SOURCE_LOCATION (function);
3491 diagnose_constraints (loc, function, NULL_TREE);
3492 return error_mark_node;
3495 mark_used (function);
3496 fndecl = function;
3498 /* Convert anything with function type to a pointer-to-function. */
3499 if (DECL_MAIN_P (function))
3501 if (complain & tf_error)
3502 pedwarn (input_location, OPT_Wpedantic,
3503 "ISO C++ forbids calling %<::main%> from within program");
3504 else
3505 return error_mark_node;
3507 function = build_addr_func (function, complain);
3509 else
3511 fndecl = NULL_TREE;
3513 function = build_addr_func (function, complain);
3516 if (function == error_mark_node)
3517 return error_mark_node;
3519 fntype = TREE_TYPE (function);
3521 if (TYPE_PTRMEMFUNC_P (fntype))
3523 if (complain & tf_error)
3524 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3525 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3526 original, original);
3527 return error_mark_node;
3530 is_method = (TYPE_PTR_P (fntype)
3531 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3533 if (!(TYPE_PTRFN_P (fntype)
3534 || is_method
3535 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3537 if (complain & tf_error)
3539 if (!flag_diagnostics_show_caret)
3540 error_at (input_location,
3541 "%qE cannot be used as a function", original);
3542 else if (DECL_P (original))
3543 error_at (input_location,
3544 "%qD cannot be used as a function", original);
3545 else
3546 error_at (input_location,
3547 "expression cannot be used as a function");
3550 return error_mark_node;
3553 /* fntype now gets the type of function pointed to. */
3554 fntype = TREE_TYPE (fntype);
3555 parm_types = TYPE_ARG_TYPES (fntype);
3557 if (params == NULL)
3559 allocated = make_tree_vector ();
3560 params = &allocated;
3563 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3564 complain);
3565 if (nargs < 0)
3566 return error_mark_node;
3568 argarray = (*params)->address ();
3570 /* Check for errors in format strings and inappropriately
3571 null parameters. */
3572 check_function_arguments (fntype, nargs, argarray);
3574 ret = build_cxx_call (function, nargs, argarray, complain);
3576 if (allocated != NULL)
3577 release_tree_vector (allocated);
3579 return ret;
3582 /* Subroutine of convert_arguments.
3583 Warn about wrong number of args are genereted. */
3585 static void
3586 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3588 if (fndecl)
3590 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3592 if (DECL_NAME (fndecl) == NULL_TREE
3593 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3594 error_at (loc,
3595 too_many_p
3596 ? G_("too many arguments to constructor %q#D")
3597 : G_("too few arguments to constructor %q#D"),
3598 fndecl);
3599 else
3600 error_at (loc,
3601 too_many_p
3602 ? G_("too many arguments to member function %q#D")
3603 : G_("too few arguments to member function %q#D"),
3604 fndecl);
3606 else
3607 error_at (loc,
3608 too_many_p
3609 ? G_("too many arguments to function %q#D")
3610 : G_("too few arguments to function %q#D"),
3611 fndecl);
3612 inform (DECL_SOURCE_LOCATION (fndecl),
3613 "declared here");
3615 else
3617 if (c_dialect_objc () && objc_message_selector ())
3618 error_at (loc,
3619 too_many_p
3620 ? G_("too many arguments to method %q#D")
3621 : G_("too few arguments to method %q#D"),
3622 objc_message_selector ());
3623 else
3624 error_at (loc, too_many_p ? G_("too many arguments to function")
3625 : G_("too few arguments to function"));
3629 /* Convert the actual parameter expressions in the list VALUES to the
3630 types in the list TYPELIST. The converted expressions are stored
3631 back in the VALUES vector.
3632 If parmdecls is exhausted, or when an element has NULL as its type,
3633 perform the default conversions.
3635 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3637 This is also where warnings about wrong number of args are generated.
3639 Returns the actual number of arguments processed (which might be less
3640 than the length of the vector), or -1 on error.
3642 In C++, unspecified trailing parameters can be filled in with their
3643 default arguments, if such were specified. Do so here. */
3645 static int
3646 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3647 int flags, tsubst_flags_t complain)
3649 tree typetail;
3650 unsigned int i;
3652 /* Argument passing is always copy-initialization. */
3653 flags |= LOOKUP_ONLYCONVERTING;
3655 for (i = 0, typetail = typelist;
3656 i < vec_safe_length (*values);
3657 i++)
3659 tree type = typetail ? TREE_VALUE (typetail) : 0;
3660 tree val = (**values)[i];
3662 if (val == error_mark_node || type == error_mark_node)
3663 return -1;
3665 if (type == void_type_node)
3667 if (complain & tf_error)
3669 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3670 return i;
3672 else
3673 return -1;
3676 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3677 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3678 if (TREE_CODE (val) == NOP_EXPR
3679 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3680 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3681 val = TREE_OPERAND (val, 0);
3683 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3685 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3686 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3687 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3688 val = decay_conversion (val, complain);
3691 if (val == error_mark_node)
3692 return -1;
3694 if (type != 0)
3696 /* Formal parm type is specified by a function prototype. */
3697 tree parmval;
3699 if (!COMPLETE_TYPE_P (complete_type (type)))
3701 if (complain & tf_error)
3703 if (fndecl)
3704 error ("parameter %P of %qD has incomplete type %qT",
3705 i, fndecl, type);
3706 else
3707 error ("parameter %P has incomplete type %qT", i, type);
3709 parmval = error_mark_node;
3711 else
3713 parmval = convert_for_initialization
3714 (NULL_TREE, type, val, flags,
3715 ICR_ARGPASS, fndecl, i, complain);
3716 parmval = convert_for_arg_passing (type, parmval, complain);
3719 if (parmval == error_mark_node)
3720 return -1;
3722 (**values)[i] = parmval;
3724 else
3726 if (fndecl && magic_varargs_p (fndecl))
3727 /* Don't do ellipsis conversion for __built_in_constant_p
3728 as this will result in spurious errors for non-trivial
3729 types. */
3730 val = require_complete_type_sfinae (val, complain);
3731 else
3732 val = convert_arg_to_ellipsis (val, complain);
3734 (**values)[i] = val;
3737 if (typetail)
3738 typetail = TREE_CHAIN (typetail);
3741 if (typetail != 0 && typetail != void_list_node)
3743 /* See if there are default arguments that can be used. Because
3744 we hold default arguments in the FUNCTION_TYPE (which is so
3745 wrong), we can see default parameters here from deduced
3746 contexts (and via typeof) for indirect function calls.
3747 Fortunately we know whether we have a function decl to
3748 provide default arguments in a language conformant
3749 manner. */
3750 if (fndecl && TREE_PURPOSE (typetail)
3751 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3753 for (; typetail != void_list_node; ++i)
3755 tree parmval
3756 = convert_default_arg (TREE_VALUE (typetail),
3757 TREE_PURPOSE (typetail),
3758 fndecl, i, complain);
3760 if (parmval == error_mark_node)
3761 return -1;
3763 vec_safe_push (*values, parmval);
3764 typetail = TREE_CHAIN (typetail);
3765 /* ends with `...'. */
3766 if (typetail == NULL_TREE)
3767 break;
3770 else
3772 if (complain & tf_error)
3773 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3774 return -1;
3778 return (int) i;
3781 /* Build a binary-operation expression, after performing default
3782 conversions on the operands. CODE is the kind of expression to
3783 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3784 are the tree codes which correspond to ARG1 and ARG2 when issuing
3785 warnings about possibly misplaced parentheses. They may differ
3786 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3787 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3788 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3789 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3790 ARG2_CODE as ERROR_MARK. */
3792 tree
3793 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3794 enum tree_code arg1_code, tree arg2,
3795 enum tree_code arg2_code, tree *overload,
3796 tsubst_flags_t complain)
3798 tree orig_arg1;
3799 tree orig_arg2;
3800 tree expr;
3802 orig_arg1 = arg1;
3803 orig_arg2 = arg2;
3805 if (processing_template_decl)
3807 if (type_dependent_expression_p (arg1)
3808 || type_dependent_expression_p (arg2))
3809 return build_min_nt_loc (loc, code, arg1, arg2);
3810 arg1 = build_non_dependent_expr (arg1);
3811 arg2 = build_non_dependent_expr (arg2);
3814 if (code == DOTSTAR_EXPR)
3815 expr = build_m_component_ref (arg1, arg2, complain);
3816 else
3817 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3818 overload, complain);
3820 /* Check for cases such as x+y<<z which users are likely to
3821 misinterpret. But don't warn about obj << x + y, since that is a
3822 common idiom for I/O. */
3823 if (warn_parentheses
3824 && (complain & tf_warning)
3825 && !processing_template_decl
3826 && !error_operand_p (arg1)
3827 && !error_operand_p (arg2)
3828 && (code != LSHIFT_EXPR
3829 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3830 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3831 arg2_code, orig_arg2);
3833 if (processing_template_decl && expr != error_mark_node)
3834 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3836 return expr;
3839 /* Build and return an ARRAY_REF expression. */
3841 tree
3842 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3843 tsubst_flags_t complain)
3845 tree orig_arg1 = arg1;
3846 tree orig_arg2 = arg2;
3847 tree expr;
3849 if (processing_template_decl)
3851 if (type_dependent_expression_p (arg1)
3852 || type_dependent_expression_p (arg2))
3853 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3854 NULL_TREE, NULL_TREE);
3855 arg1 = build_non_dependent_expr (arg1);
3856 arg2 = build_non_dependent_expr (arg2);
3859 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3860 NULL_TREE, /*overload=*/NULL, complain);
3862 if (processing_template_decl && expr != error_mark_node)
3863 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3864 NULL_TREE, NULL_TREE);
3865 return expr;
3868 /* Return whether OP is an expression of enum type cast to integer
3869 type. In C++ even unsigned enum types are cast to signed integer
3870 types. We do not want to issue warnings about comparisons between
3871 signed and unsigned types when one of the types is an enum type.
3872 Those warnings are always false positives in practice. */
3874 static bool
3875 enum_cast_to_int (tree op)
3877 if (CONVERT_EXPR_P (op)
3878 && TREE_TYPE (op) == integer_type_node
3879 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3880 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3881 return true;
3883 /* The cast may have been pushed into a COND_EXPR. */
3884 if (TREE_CODE (op) == COND_EXPR)
3885 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3886 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3888 return false;
3891 /* For the c-common bits. */
3892 tree
3893 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3894 int /*convert_p*/)
3896 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3900 /* Build a binary-operation expression without default conversions.
3901 CODE is the kind of expression to build.
3902 LOCATION is the location_t of the operator in the source code.
3903 This function differs from `build' in several ways:
3904 the data type of the result is computed and recorded in it,
3905 warnings are generated if arg data types are invalid,
3906 special handling for addition and subtraction of pointers is known,
3907 and some optimization is done (operations on narrow ints
3908 are done in the narrower type when that gives the same result).
3909 Constant folding is also done before the result is returned.
3911 Note that the operands will never have enumeral types
3912 because either they have just had the default conversions performed
3913 or they have both just been converted to some other type in which
3914 the arithmetic is to be done.
3916 C++: must do special pointer arithmetic when implementing
3917 multiple inheritance, and deal with pointer to member functions. */
3919 tree
3920 cp_build_binary_op (location_t location,
3921 enum tree_code code, tree orig_op0, tree orig_op1,
3922 tsubst_flags_t complain)
3924 tree op0, op1;
3925 enum tree_code code0, code1;
3926 tree type0, type1;
3927 const char *invalid_op_diag;
3929 /* Expression code to give to the expression when it is built.
3930 Normally this is CODE, which is what the caller asked for,
3931 but in some special cases we change it. */
3932 enum tree_code resultcode = code;
3934 /* Data type in which the computation is to be performed.
3935 In the simplest cases this is the common type of the arguments. */
3936 tree result_type = NULL;
3938 /* Nonzero means operands have already been type-converted
3939 in whatever way is necessary.
3940 Zero means they need to be converted to RESULT_TYPE. */
3941 int converted = 0;
3943 /* Nonzero means create the expression with this type, rather than
3944 RESULT_TYPE. */
3945 tree build_type = 0;
3947 /* Nonzero means after finally constructing the expression
3948 convert it to this type. */
3949 tree final_type = 0;
3951 tree result;
3952 tree orig_type = NULL;
3954 /* Nonzero if this is an operation like MIN or MAX which can
3955 safely be computed in short if both args are promoted shorts.
3956 Also implies COMMON.
3957 -1 indicates a bitwise operation; this makes a difference
3958 in the exact conditions for when it is safe to do the operation
3959 in a narrower mode. */
3960 int shorten = 0;
3962 /* Nonzero if this is a comparison operation;
3963 if both args are promoted shorts, compare the original shorts.
3964 Also implies COMMON. */
3965 int short_compare = 0;
3967 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3968 int common = 0;
3970 /* True if both operands have arithmetic type. */
3971 bool arithmetic_types_p;
3973 /* Apply default conversions. */
3974 op0 = orig_op0;
3975 op1 = orig_op1;
3977 /* Remember whether we're doing / or %. */
3978 bool doing_div_or_mod = false;
3980 /* Remember whether we're doing << or >>. */
3981 bool doing_shift = false;
3983 /* Tree holding instrumentation expression. */
3984 tree instrument_expr = NULL;
3986 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3987 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3988 || code == TRUTH_XOR_EXPR)
3990 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3991 op0 = decay_conversion (op0, complain);
3992 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3993 op1 = decay_conversion (op1, complain);
3995 else
3997 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3998 op0 = cp_default_conversion (op0, complain);
3999 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4000 op1 = cp_default_conversion (op1, complain);
4003 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4004 STRIP_TYPE_NOPS (op0);
4005 STRIP_TYPE_NOPS (op1);
4007 /* DTRT if one side is an overloaded function, but complain about it. */
4008 if (type_unknown_p (op0))
4010 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4011 if (t != error_mark_node)
4013 if (complain & tf_error)
4014 permerror (input_location, "assuming cast to type %qT from overloaded function",
4015 TREE_TYPE (t));
4016 op0 = t;
4019 if (type_unknown_p (op1))
4021 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4022 if (t != error_mark_node)
4024 if (complain & tf_error)
4025 permerror (input_location, "assuming cast to type %qT from overloaded function",
4026 TREE_TYPE (t));
4027 op1 = t;
4031 type0 = TREE_TYPE (op0);
4032 type1 = TREE_TYPE (op1);
4034 /* The expression codes of the data types of the arguments tell us
4035 whether the arguments are integers, floating, pointers, etc. */
4036 code0 = TREE_CODE (type0);
4037 code1 = TREE_CODE (type1);
4039 /* If an error was already reported for one of the arguments,
4040 avoid reporting another error. */
4041 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4042 return error_mark_node;
4044 if ((invalid_op_diag
4045 = targetm.invalid_binary_op (code, type0, type1)))
4047 if (complain & tf_error)
4048 error (invalid_op_diag);
4049 return error_mark_node;
4052 /* Issue warnings about peculiar, but valid, uses of NULL. */
4053 if ((orig_op0 == null_node || orig_op1 == null_node)
4054 /* It's reasonable to use pointer values as operands of &&
4055 and ||, so NULL is no exception. */
4056 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4057 && ( /* Both are NULL (or 0) and the operation was not a
4058 comparison or a pointer subtraction. */
4059 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4060 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4061 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4062 || (!null_ptr_cst_p (orig_op0)
4063 && !TYPE_PTR_OR_PTRMEM_P (type0))
4064 || (!null_ptr_cst_p (orig_op1)
4065 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4066 && (complain & tf_warning))
4068 source_location loc =
4069 expansion_point_location_if_in_system_header (input_location);
4071 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4074 /* In case when one of the operands of the binary operation is
4075 a vector and another is a scalar -- convert scalar to vector. */
4076 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4078 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4079 complain & tf_error);
4081 switch (convert_flag)
4083 case stv_error:
4084 return error_mark_node;
4085 case stv_firstarg:
4087 op0 = convert (TREE_TYPE (type1), op0);
4088 op0 = save_expr (op0);
4089 op0 = build_vector_from_val (type1, op0);
4090 type0 = TREE_TYPE (op0);
4091 code0 = TREE_CODE (type0);
4092 converted = 1;
4093 break;
4095 case stv_secondarg:
4097 op1 = convert (TREE_TYPE (type0), op1);
4098 op1 = save_expr (op1);
4099 op1 = build_vector_from_val (type0, op1);
4100 type1 = TREE_TYPE (op1);
4101 code1 = TREE_CODE (type1);
4102 converted = 1;
4103 break;
4105 default:
4106 break;
4110 switch (code)
4112 case MINUS_EXPR:
4113 /* Subtraction of two similar pointers.
4114 We must subtract them as integers, then divide by object size. */
4115 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4116 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4117 TREE_TYPE (type1)))
4118 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4119 complain);
4120 /* In all other cases except pointer - int, the usual arithmetic
4121 rules apply. */
4122 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4124 common = 1;
4125 break;
4127 /* The pointer - int case is just like pointer + int; fall
4128 through. */
4129 case PLUS_EXPR:
4130 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4131 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4133 tree ptr_operand;
4134 tree int_operand;
4135 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4136 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4137 if (processing_template_decl)
4139 result_type = TREE_TYPE (ptr_operand);
4140 break;
4142 return cp_pointer_int_sum (code,
4143 ptr_operand,
4144 int_operand,
4145 complain);
4147 common = 1;
4148 break;
4150 case MULT_EXPR:
4151 common = 1;
4152 break;
4154 case TRUNC_DIV_EXPR:
4155 case CEIL_DIV_EXPR:
4156 case FLOOR_DIV_EXPR:
4157 case ROUND_DIV_EXPR:
4158 case EXACT_DIV_EXPR:
4159 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4160 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4161 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4162 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4164 enum tree_code tcode0 = code0, tcode1 = code1;
4165 tree cop1 = fold_non_dependent_expr (op1);
4166 doing_div_or_mod = true;
4167 warn_for_div_by_zero (location, cop1);
4169 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4170 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4171 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4172 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4174 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4175 resultcode = RDIV_EXPR;
4176 else
4177 /* When dividing two signed integers, we have to promote to int.
4178 unless we divide by a constant != -1. Note that default
4179 conversion will have been performed on the operands at this
4180 point, so we have to dig out the original type to find out if
4181 it was unsigned. */
4182 shorten = ((TREE_CODE (op0) == NOP_EXPR
4183 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4184 || (TREE_CODE (op1) == INTEGER_CST
4185 && ! integer_all_onesp (op1)));
4187 common = 1;
4189 break;
4191 case BIT_AND_EXPR:
4192 case BIT_IOR_EXPR:
4193 case BIT_XOR_EXPR:
4194 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4195 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4196 && !VECTOR_FLOAT_TYPE_P (type0)
4197 && !VECTOR_FLOAT_TYPE_P (type1)))
4198 shorten = -1;
4199 break;
4201 case TRUNC_MOD_EXPR:
4202 case FLOOR_MOD_EXPR:
4204 tree cop1 = fold_non_dependent_expr (op1);
4205 doing_div_or_mod = true;
4206 warn_for_div_by_zero (location, cop1);
4209 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4210 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4211 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4212 common = 1;
4213 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4215 /* Although it would be tempting to shorten always here, that loses
4216 on some targets, since the modulo instruction is undefined if the
4217 quotient can't be represented in the computation mode. We shorten
4218 only if unsigned or if dividing by something we know != -1. */
4219 shorten = ((TREE_CODE (op0) == NOP_EXPR
4220 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4221 || (TREE_CODE (op1) == INTEGER_CST
4222 && ! integer_all_onesp (op1)));
4223 common = 1;
4225 break;
4227 case TRUTH_ANDIF_EXPR:
4228 case TRUTH_ORIF_EXPR:
4229 case TRUTH_AND_EXPR:
4230 case TRUTH_OR_EXPR:
4231 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4233 if (!COMPARISON_CLASS_P (op1))
4234 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4235 build_zero_cst (type1), complain);
4236 if (code == TRUTH_ANDIF_EXPR)
4238 tree z = build_zero_cst (TREE_TYPE (op1));
4239 return build_conditional_expr (location, op0, op1, z, complain);
4241 else if (code == TRUTH_ORIF_EXPR)
4243 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4244 return build_conditional_expr (location, op0, m1, op1, complain);
4246 else
4247 gcc_unreachable ();
4249 if (VECTOR_TYPE_P (type0))
4251 if (!COMPARISON_CLASS_P (op0))
4252 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4253 build_zero_cst (type0), complain);
4254 if (!VECTOR_TYPE_P (type1))
4256 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4257 tree z = build_zero_cst (TREE_TYPE (op0));
4258 op1 = build_conditional_expr (location, op1, z, m1, complain);
4260 else if (!COMPARISON_CLASS_P (op1))
4261 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4262 build_zero_cst (type1), complain);
4264 if (code == TRUTH_ANDIF_EXPR)
4265 code = BIT_AND_EXPR;
4266 else if (code == TRUTH_ORIF_EXPR)
4267 code = BIT_IOR_EXPR;
4268 else
4269 gcc_unreachable ();
4271 return cp_build_binary_op (location, code, op0, op1, complain);
4274 result_type = boolean_type_node;
4275 break;
4277 /* Shift operations: result has same type as first operand;
4278 always convert second operand to int.
4279 Also set SHORT_SHIFT if shifting rightward. */
4281 case RSHIFT_EXPR:
4282 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4283 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4285 result_type = type0;
4286 converted = 1;
4288 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4289 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4290 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4291 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4293 result_type = type0;
4294 converted = 1;
4296 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4298 tree const_op1 = fold_non_dependent_expr (op1);
4299 if (TREE_CODE (const_op1) != INTEGER_CST)
4300 const_op1 = op1;
4301 result_type = type0;
4302 doing_shift = true;
4303 if (TREE_CODE (const_op1) == INTEGER_CST)
4305 if (tree_int_cst_lt (const_op1, integer_zero_node))
4307 if ((complain & tf_warning)
4308 && c_inhibit_evaluation_warnings == 0)
4309 warning (OPT_Wshift_count_negative,
4310 "right shift count is negative");
4312 else
4314 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4315 && (complain & tf_warning)
4316 && c_inhibit_evaluation_warnings == 0)
4317 warning (OPT_Wshift_count_overflow,
4318 "right shift count >= width of type");
4321 /* Avoid converting op1 to result_type later. */
4322 converted = 1;
4324 break;
4326 case LSHIFT_EXPR:
4327 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4328 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4330 result_type = type0;
4331 converted = 1;
4333 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4334 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4335 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4336 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4338 result_type = type0;
4339 converted = 1;
4341 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4343 tree const_op1 = fold_non_dependent_expr (op1);
4344 if (TREE_CODE (const_op1) != INTEGER_CST)
4345 const_op1 = op1;
4346 result_type = type0;
4347 doing_shift = true;
4348 if (TREE_CODE (const_op1) == INTEGER_CST)
4350 if (tree_int_cst_lt (const_op1, integer_zero_node))
4352 if ((complain & tf_warning)
4353 && c_inhibit_evaluation_warnings == 0)
4354 warning (OPT_Wshift_count_negative,
4355 "left shift count is negative");
4357 else if (compare_tree_int (const_op1,
4358 TYPE_PRECISION (type0)) >= 0)
4360 if ((complain & tf_warning)
4361 && c_inhibit_evaluation_warnings == 0)
4362 warning (OPT_Wshift_count_overflow,
4363 "left shift count >= width of type");
4366 /* Avoid converting op1 to result_type later. */
4367 converted = 1;
4369 break;
4371 case RROTATE_EXPR:
4372 case LROTATE_EXPR:
4373 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4375 result_type = type0;
4376 if (TREE_CODE (op1) == INTEGER_CST)
4378 if (tree_int_cst_lt (op1, integer_zero_node))
4380 if (complain & tf_warning)
4381 warning (0, (code == LROTATE_EXPR)
4382 ? G_("left rotate count is negative")
4383 : G_("right rotate count is negative"));
4385 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4387 if (complain & tf_warning)
4388 warning (0, (code == LROTATE_EXPR)
4389 ? G_("left rotate count >= width of type")
4390 : G_("right rotate count >= width of type"));
4393 /* Convert the shift-count to an integer, regardless of
4394 size of value being shifted. */
4395 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4396 op1 = cp_convert (integer_type_node, op1, complain);
4398 break;
4400 case EQ_EXPR:
4401 case NE_EXPR:
4402 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4403 goto vector_compare;
4404 if ((complain & tf_warning)
4405 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4406 warning (OPT_Wfloat_equal,
4407 "comparing floating point with == or != is unsafe");
4408 if ((complain & tf_warning)
4409 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4410 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4411 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4413 build_type = boolean_type_node;
4414 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4415 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4416 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4417 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4418 short_compare = 1;
4419 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4420 && null_ptr_cst_p (op1))
4421 /* Handle, eg, (void*)0 (c++/43906), and more. */
4422 || (code0 == POINTER_TYPE
4423 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4425 if (TYPE_PTR_P (type1))
4426 result_type = composite_pointer_type (type0, type1, op0, op1,
4427 CPO_COMPARISON, complain);
4428 else
4429 result_type = type0;
4431 if (TREE_CODE (op0) == ADDR_EXPR
4432 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4434 if ((complain & tf_warning)
4435 && c_inhibit_evaluation_warnings == 0
4436 && !TREE_NO_WARNING (op0))
4437 warning (OPT_Waddress, "the address of %qD will never be NULL",
4438 TREE_OPERAND (op0, 0));
4441 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4442 && null_ptr_cst_p (op0))
4443 /* Handle, eg, (void*)0 (c++/43906), and more. */
4444 || (code1 == POINTER_TYPE
4445 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4447 if (TYPE_PTR_P (type0))
4448 result_type = composite_pointer_type (type0, type1, op0, op1,
4449 CPO_COMPARISON, complain);
4450 else
4451 result_type = type1;
4453 if (TREE_CODE (op1) == ADDR_EXPR
4454 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4456 if ((complain & tf_warning)
4457 && c_inhibit_evaluation_warnings == 0
4458 && !TREE_NO_WARNING (op1))
4459 warning (OPT_Waddress, "the address of %qD will never be NULL",
4460 TREE_OPERAND (op1, 0));
4463 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4464 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4465 result_type = composite_pointer_type (type0, type1, op0, op1,
4466 CPO_COMPARISON, complain);
4467 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4468 /* One of the operands must be of nullptr_t type. */
4469 result_type = TREE_TYPE (nullptr_node);
4470 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4472 result_type = type0;
4473 if (complain & tf_error)
4474 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4475 else
4476 return error_mark_node;
4478 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4480 result_type = type1;
4481 if (complain & tf_error)
4482 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4483 else
4484 return error_mark_node;
4486 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4488 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4489 == ptrmemfunc_vbit_in_delta)
4491 tree pfn0, delta0, e1, e2;
4493 if (TREE_SIDE_EFFECTS (op0))
4494 op0 = save_expr (op0);
4496 pfn0 = pfn_from_ptrmemfunc (op0);
4497 delta0 = delta_from_ptrmemfunc (op0);
4498 e1 = cp_build_binary_op (location,
4499 EQ_EXPR,
4500 pfn0,
4501 build_zero_cst (TREE_TYPE (pfn0)),
4502 complain);
4503 e2 = cp_build_binary_op (location,
4504 BIT_AND_EXPR,
4505 delta0,
4506 integer_one_node,
4507 complain);
4509 if (complain & tf_warning)
4510 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4512 e2 = cp_build_binary_op (location,
4513 EQ_EXPR, e2, integer_zero_node,
4514 complain);
4515 op0 = cp_build_binary_op (location,
4516 TRUTH_ANDIF_EXPR, e1, e2,
4517 complain);
4518 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4520 else
4522 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4523 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4525 result_type = TREE_TYPE (op0);
4527 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4528 return cp_build_binary_op (location, code, op1, op0, complain);
4529 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4531 tree type;
4532 /* E will be the final comparison. */
4533 tree e;
4534 /* E1 and E2 are for scratch. */
4535 tree e1;
4536 tree e2;
4537 tree pfn0;
4538 tree pfn1;
4539 tree delta0;
4540 tree delta1;
4542 type = composite_pointer_type (type0, type1, op0, op1,
4543 CPO_COMPARISON, complain);
4545 if (!same_type_p (TREE_TYPE (op0), type))
4546 op0 = cp_convert_and_check (type, op0, complain);
4547 if (!same_type_p (TREE_TYPE (op1), type))
4548 op1 = cp_convert_and_check (type, op1, complain);
4550 if (op0 == error_mark_node || op1 == error_mark_node)
4551 return error_mark_node;
4553 if (TREE_SIDE_EFFECTS (op0))
4554 op0 = save_expr (op0);
4555 if (TREE_SIDE_EFFECTS (op1))
4556 op1 = save_expr (op1);
4558 pfn0 = pfn_from_ptrmemfunc (op0);
4559 /* Avoid -Waddress warnings (c++/64877). */
4560 if (TREE_CODE (pfn0) == ADDR_EXPR)
4561 TREE_NO_WARNING (pfn0) = 1;
4562 pfn1 = pfn_from_ptrmemfunc (op1);
4563 delta0 = delta_from_ptrmemfunc (op0);
4564 delta1 = delta_from_ptrmemfunc (op1);
4565 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4566 == ptrmemfunc_vbit_in_delta)
4568 /* We generate:
4570 (op0.pfn == op1.pfn
4571 && ((op0.delta == op1.delta)
4572 || (!op0.pfn && op0.delta & 1 == 0
4573 && op1.delta & 1 == 0))
4575 The reason for the `!op0.pfn' bit is that a NULL
4576 pointer-to-member is any member with a zero PFN and
4577 LSB of the DELTA field is 0. */
4579 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4580 delta0,
4581 integer_one_node,
4582 complain);
4583 e1 = cp_build_binary_op (location,
4584 EQ_EXPR, e1, integer_zero_node,
4585 complain);
4586 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4587 delta1,
4588 integer_one_node,
4589 complain);
4590 e2 = cp_build_binary_op (location,
4591 EQ_EXPR, e2, integer_zero_node,
4592 complain);
4593 e1 = cp_build_binary_op (location,
4594 TRUTH_ANDIF_EXPR, e2, e1,
4595 complain);
4596 e2 = cp_build_binary_op (location, EQ_EXPR,
4597 pfn0,
4598 build_zero_cst (TREE_TYPE (pfn0)),
4599 complain);
4600 e2 = cp_build_binary_op (location,
4601 TRUTH_ANDIF_EXPR, e2, e1, complain);
4602 e1 = cp_build_binary_op (location,
4603 EQ_EXPR, delta0, delta1, complain);
4604 e1 = cp_build_binary_op (location,
4605 TRUTH_ORIF_EXPR, e1, e2, complain);
4607 else
4609 /* We generate:
4611 (op0.pfn == op1.pfn
4612 && (!op0.pfn || op0.delta == op1.delta))
4614 The reason for the `!op0.pfn' bit is that a NULL
4615 pointer-to-member is any member with a zero PFN; the
4616 DELTA field is unspecified. */
4618 e1 = cp_build_binary_op (location,
4619 EQ_EXPR, delta0, delta1, complain);
4620 e2 = cp_build_binary_op (location,
4621 EQ_EXPR,
4622 pfn0,
4623 build_zero_cst (TREE_TYPE (pfn0)),
4624 complain);
4625 e1 = cp_build_binary_op (location,
4626 TRUTH_ORIF_EXPR, e1, e2, complain);
4628 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4629 e = cp_build_binary_op (location,
4630 TRUTH_ANDIF_EXPR, e2, e1, complain);
4631 if (code == EQ_EXPR)
4632 return e;
4633 return cp_build_binary_op (location,
4634 EQ_EXPR, e, integer_zero_node, complain);
4636 else
4638 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4639 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4640 type1));
4641 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4642 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4643 type0));
4646 break;
4648 case MAX_EXPR:
4649 case MIN_EXPR:
4650 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4651 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4652 shorten = 1;
4653 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4654 result_type = composite_pointer_type (type0, type1, op0, op1,
4655 CPO_COMPARISON, complain);
4656 break;
4658 case LE_EXPR:
4659 case GE_EXPR:
4660 case LT_EXPR:
4661 case GT_EXPR:
4662 if (TREE_CODE (orig_op0) == STRING_CST
4663 || TREE_CODE (orig_op1) == STRING_CST)
4665 if (complain & tf_warning)
4666 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4669 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4671 vector_compare:
4672 tree intt;
4673 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4674 TREE_TYPE (type1))
4675 && !vector_types_compatible_elements_p (type0, type1))
4677 if (complain & tf_error)
4679 error_at (location, "comparing vectors with different "
4680 "element types");
4681 inform (location, "operand types are %qT and %qT",
4682 type0, type1);
4684 return error_mark_node;
4687 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4689 if (complain & tf_error)
4691 error_at (location, "comparing vectors with different "
4692 "number of elements");
4693 inform (location, "operand types are %qT and %qT",
4694 type0, type1);
4696 return error_mark_node;
4699 /* Always construct signed integer vector type. */
4700 intt = c_common_type_for_size (GET_MODE_BITSIZE
4701 (TYPE_MODE (TREE_TYPE (type0))), 0);
4702 if (!intt)
4704 if (complain & tf_error)
4705 error_at (location, "could not find an integer type "
4706 "of the same size as %qT", TREE_TYPE (type0));
4707 return error_mark_node;
4709 result_type = build_opaque_vector_type (intt,
4710 TYPE_VECTOR_SUBPARTS (type0));
4711 converted = 1;
4712 break;
4714 build_type = boolean_type_node;
4715 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4716 || code0 == ENUMERAL_TYPE)
4717 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4718 || code1 == ENUMERAL_TYPE))
4719 short_compare = 1;
4720 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4721 result_type = composite_pointer_type (type0, type1, op0, op1,
4722 CPO_COMPARISON, complain);
4723 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4725 result_type = type0;
4726 if (extra_warnings && (complain & tf_warning))
4727 warning (OPT_Wextra,
4728 "ordered comparison of pointer with integer zero");
4730 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4732 result_type = type1;
4733 if (extra_warnings && (complain & tf_warning))
4734 warning (OPT_Wextra,
4735 "ordered comparison of pointer with integer zero");
4737 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4738 /* One of the operands must be of nullptr_t type. */
4739 result_type = TREE_TYPE (nullptr_node);
4740 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4742 result_type = type0;
4743 if (complain & tf_error)
4744 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4745 else
4746 return error_mark_node;
4748 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4750 result_type = type1;
4751 if (complain & tf_error)
4752 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4753 else
4754 return error_mark_node;
4756 break;
4758 case UNORDERED_EXPR:
4759 case ORDERED_EXPR:
4760 case UNLT_EXPR:
4761 case UNLE_EXPR:
4762 case UNGT_EXPR:
4763 case UNGE_EXPR:
4764 case UNEQ_EXPR:
4765 build_type = integer_type_node;
4766 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4768 if (complain & tf_error)
4769 error ("unordered comparison on non-floating point argument");
4770 return error_mark_node;
4772 common = 1;
4773 break;
4775 default:
4776 break;
4779 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4780 || code0 == ENUMERAL_TYPE)
4781 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4782 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4783 arithmetic_types_p = 1;
4784 else
4786 arithmetic_types_p = 0;
4787 /* Vector arithmetic is only allowed when both sides are vectors. */
4788 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4790 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4791 || !vector_types_compatible_elements_p (type0, type1))
4793 if (complain & tf_error)
4794 binary_op_error (location, code, type0, type1);
4795 return error_mark_node;
4797 arithmetic_types_p = 1;
4800 /* Determine the RESULT_TYPE, if it is not already known. */
4801 if (!result_type
4802 && arithmetic_types_p
4803 && (shorten || common || short_compare))
4805 result_type = cp_common_type (type0, type1);
4806 if (complain & tf_warning)
4807 do_warn_double_promotion (result_type, type0, type1,
4808 "implicit conversion from %qT to %qT "
4809 "to match other operand of binary "
4810 "expression",
4811 location);
4814 if (!result_type)
4816 if (complain & tf_error)
4817 error ("invalid operands of types %qT and %qT to binary %qO",
4818 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4819 return error_mark_node;
4822 /* If we're in a template, the only thing we need to know is the
4823 RESULT_TYPE. */
4824 if (processing_template_decl)
4826 /* Since the middle-end checks the type when doing a build2, we
4827 need to build the tree in pieces. This built tree will never
4828 get out of the front-end as we replace it when instantiating
4829 the template. */
4830 tree tmp = build2 (resultcode,
4831 build_type ? build_type : result_type,
4832 NULL_TREE, op1);
4833 TREE_OPERAND (tmp, 0) = op0;
4834 return tmp;
4837 if (arithmetic_types_p)
4839 bool first_complex = (code0 == COMPLEX_TYPE);
4840 bool second_complex = (code1 == COMPLEX_TYPE);
4841 int none_complex = (!first_complex && !second_complex);
4843 /* Adapted from patch for c/24581. */
4844 if (first_complex != second_complex
4845 && (code == PLUS_EXPR
4846 || code == MINUS_EXPR
4847 || code == MULT_EXPR
4848 || (code == TRUNC_DIV_EXPR && first_complex))
4849 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4850 && flag_signed_zeros)
4852 /* An operation on mixed real/complex operands must be
4853 handled specially, but the language-independent code can
4854 more easily optimize the plain complex arithmetic if
4855 -fno-signed-zeros. */
4856 tree real_type = TREE_TYPE (result_type);
4857 tree real, imag;
4858 if (first_complex)
4860 if (TREE_TYPE (op0) != result_type)
4861 op0 = cp_convert_and_check (result_type, op0, complain);
4862 if (TREE_TYPE (op1) != real_type)
4863 op1 = cp_convert_and_check (real_type, op1, complain);
4865 else
4867 if (TREE_TYPE (op0) != real_type)
4868 op0 = cp_convert_and_check (real_type, op0, complain);
4869 if (TREE_TYPE (op1) != result_type)
4870 op1 = cp_convert_and_check (result_type, op1, complain);
4872 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4873 return error_mark_node;
4874 if (first_complex)
4876 op0 = save_expr (op0);
4877 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4878 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4879 switch (code)
4881 case MULT_EXPR:
4882 case TRUNC_DIV_EXPR:
4883 op1 = save_expr (op1);
4884 imag = build2 (resultcode, real_type, imag, op1);
4885 /* Fall through. */
4886 case PLUS_EXPR:
4887 case MINUS_EXPR:
4888 real = build2 (resultcode, real_type, real, op1);
4889 break;
4890 default:
4891 gcc_unreachable();
4894 else
4896 op1 = save_expr (op1);
4897 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4898 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4899 switch (code)
4901 case MULT_EXPR:
4902 op0 = save_expr (op0);
4903 imag = build2 (resultcode, real_type, op0, imag);
4904 /* Fall through. */
4905 case PLUS_EXPR:
4906 real = build2 (resultcode, real_type, op0, real);
4907 break;
4908 case MINUS_EXPR:
4909 real = build2 (resultcode, real_type, op0, real);
4910 imag = build1 (NEGATE_EXPR, real_type, imag);
4911 break;
4912 default:
4913 gcc_unreachable();
4916 real = fold_if_not_in_template (real);
4917 imag = fold_if_not_in_template (imag);
4918 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4919 result = fold_if_not_in_template (result);
4920 return result;
4923 /* For certain operations (which identify themselves by shorten != 0)
4924 if both args were extended from the same smaller type,
4925 do the arithmetic in that type and then extend.
4927 shorten !=0 and !=1 indicates a bitwise operation.
4928 For them, this optimization is safe only if
4929 both args are zero-extended or both are sign-extended.
4930 Otherwise, we might change the result.
4931 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4932 but calculated in (unsigned short) it would be (unsigned short)-1. */
4934 if (shorten && none_complex)
4936 orig_type = result_type;
4937 final_type = result_type;
4938 result_type = shorten_binary_op (result_type, op0, op1,
4939 shorten == -1);
4942 /* Comparison operations are shortened too but differently.
4943 They identify themselves by setting short_compare = 1. */
4945 if (short_compare)
4947 /* Don't write &op0, etc., because that would prevent op0
4948 from being kept in a register.
4949 Instead, make copies of the our local variables and
4950 pass the copies by reference, then copy them back afterward. */
4951 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4952 enum tree_code xresultcode = resultcode;
4953 tree val
4954 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4955 &xresultcode);
4956 if (val != 0)
4957 return cp_convert (boolean_type_node, val, complain);
4958 op0 = xop0, op1 = xop1;
4959 converted = 1;
4960 resultcode = xresultcode;
4963 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4964 && warn_sign_compare
4965 /* Do not warn until the template is instantiated; we cannot
4966 bound the ranges of the arguments until that point. */
4967 && !processing_template_decl
4968 && (complain & tf_warning)
4969 && c_inhibit_evaluation_warnings == 0
4970 /* Even unsigned enum types promote to signed int. We don't
4971 want to issue -Wsign-compare warnings for this case. */
4972 && !enum_cast_to_int (orig_op0)
4973 && !enum_cast_to_int (orig_op1))
4975 tree oop0 = maybe_constant_value (orig_op0);
4976 tree oop1 = maybe_constant_value (orig_op1);
4978 if (TREE_CODE (oop0) != INTEGER_CST)
4979 oop0 = orig_op0;
4980 if (TREE_CODE (oop1) != INTEGER_CST)
4981 oop1 = orig_op1;
4982 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4983 result_type, resultcode);
4987 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4988 Then the expression will be built.
4989 It will be given type FINAL_TYPE if that is nonzero;
4990 otherwise, it will be given type RESULT_TYPE. */
4991 if (! converted)
4993 if (TREE_TYPE (op0) != result_type)
4994 op0 = cp_convert_and_check (result_type, op0, complain);
4995 if (TREE_TYPE (op1) != result_type)
4996 op1 = cp_convert_and_check (result_type, op1, complain);
4998 if (op0 == error_mark_node || op1 == error_mark_node)
4999 return error_mark_node;
5002 if (build_type == NULL_TREE)
5003 build_type = result_type;
5005 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
5006 | SANITIZE_FLOAT_DIVIDE))
5007 && !processing_template_decl
5008 && do_ubsan_in_current_function ()
5009 && (doing_div_or_mod || doing_shift))
5011 /* OP0 and/or OP1 might have side-effects. */
5012 op0 = cp_save_expr (op0);
5013 op1 = cp_save_expr (op1);
5014 op0 = fold_non_dependent_expr (op0);
5015 op1 = fold_non_dependent_expr (op1);
5016 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
5017 | SANITIZE_FLOAT_DIVIDE)))
5019 /* For diagnostics we want to use the promoted types without
5020 shorten_binary_op. So convert the arguments to the
5021 original result_type. */
5022 tree cop0 = op0;
5023 tree cop1 = op1;
5024 if (orig_type != NULL && result_type != orig_type)
5026 cop0 = cp_convert (orig_type, op0, complain);
5027 cop1 = cp_convert (orig_type, op1, complain);
5029 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5031 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5032 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5035 result = build2 (resultcode, build_type, op0, op1);
5036 result = fold_if_not_in_template (result);
5037 if (final_type != 0)
5038 result = cp_convert (final_type, result, complain);
5040 if (TREE_OVERFLOW_P (result)
5041 && !TREE_OVERFLOW_P (op0)
5042 && !TREE_OVERFLOW_P (op1))
5043 overflow_warning (location, result);
5045 if (instrument_expr != NULL)
5046 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
5047 instrument_expr, result);
5049 return result;
5052 /* Build a VEC_PERM_EXPR.
5053 This is a simple wrapper for c_build_vec_perm_expr. */
5054 tree
5055 build_x_vec_perm_expr (location_t loc,
5056 tree arg0, tree arg1, tree arg2,
5057 tsubst_flags_t complain)
5059 tree orig_arg0 = arg0;
5060 tree orig_arg1 = arg1;
5061 tree orig_arg2 = arg2;
5062 if (processing_template_decl)
5064 if (type_dependent_expression_p (arg0)
5065 || type_dependent_expression_p (arg1)
5066 || type_dependent_expression_p (arg2))
5067 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5068 arg0 = build_non_dependent_expr (arg0);
5069 if (arg1)
5070 arg1 = build_non_dependent_expr (arg1);
5071 arg2 = build_non_dependent_expr (arg2);
5073 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5074 if (processing_template_decl && exp != error_mark_node)
5075 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5076 orig_arg1, orig_arg2);
5077 return exp;
5080 /* Return a tree for the sum or difference (RESULTCODE says which)
5081 of pointer PTROP and integer INTOP. */
5083 static tree
5084 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5085 tsubst_flags_t complain)
5087 tree res_type = TREE_TYPE (ptrop);
5089 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5090 in certain circumstance (when it's valid to do so). So we need
5091 to make sure it's complete. We don't need to check here, if we
5092 can actually complete it at all, as those checks will be done in
5093 pointer_int_sum() anyway. */
5094 complete_type (TREE_TYPE (res_type));
5096 return pointer_int_sum (input_location, resultcode, ptrop,
5097 fold_if_not_in_template (intop),
5098 complain & tf_warning_or_error);
5101 /* Return a tree for the difference of pointers OP0 and OP1.
5102 The resulting tree has type int. */
5104 static tree
5105 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5107 tree result;
5108 tree restype = ptrdiff_type_node;
5109 tree target_type = TREE_TYPE (ptrtype);
5111 if (!complete_type_or_else (target_type, NULL_TREE))
5112 return error_mark_node;
5114 if (VOID_TYPE_P (target_type))
5116 if (complain & tf_error)
5117 permerror (input_location, "ISO C++ forbids using pointer of "
5118 "type %<void *%> in subtraction");
5119 else
5120 return error_mark_node;
5122 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5124 if (complain & tf_error)
5125 permerror (input_location, "ISO C++ forbids using pointer to "
5126 "a function in subtraction");
5127 else
5128 return error_mark_node;
5130 if (TREE_CODE (target_type) == METHOD_TYPE)
5132 if (complain & tf_error)
5133 permerror (input_location, "ISO C++ forbids using pointer to "
5134 "a method in subtraction");
5135 else
5136 return error_mark_node;
5139 /* First do the subtraction as integers;
5140 then drop through to build the divide operator. */
5142 op0 = cp_build_binary_op (input_location,
5143 MINUS_EXPR,
5144 cp_convert (restype, op0, complain),
5145 cp_convert (restype, op1, complain),
5146 complain);
5148 /* This generates an error if op1 is a pointer to an incomplete type. */
5149 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5151 if (complain & tf_error)
5152 error ("invalid use of a pointer to an incomplete type in "
5153 "pointer arithmetic");
5154 else
5155 return error_mark_node;
5158 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5160 if (complain & tf_error)
5161 error ("arithmetic on pointer to an empty aggregate");
5162 else
5163 return error_mark_node;
5166 op1 = (TYPE_PTROB_P (ptrtype)
5167 ? size_in_bytes (target_type)
5168 : integer_one_node);
5170 /* Do the division. */
5172 result = build2 (EXACT_DIV_EXPR, restype, op0,
5173 cp_convert (restype, op1, complain));
5174 return fold_if_not_in_template (result);
5177 /* Construct and perhaps optimize a tree representation
5178 for a unary operation. CODE, a tree_code, specifies the operation
5179 and XARG is the operand. */
5181 tree
5182 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5183 tsubst_flags_t complain)
5185 tree orig_expr = xarg;
5186 tree exp;
5187 int ptrmem = 0;
5189 if (processing_template_decl)
5191 if (type_dependent_expression_p (xarg))
5192 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5194 xarg = build_non_dependent_expr (xarg);
5197 exp = NULL_TREE;
5199 /* [expr.unary.op] says:
5201 The address of an object of incomplete type can be taken.
5203 (And is just the ordinary address operator, not an overloaded
5204 "operator &".) However, if the type is a template
5205 specialization, we must complete the type at this point so that
5206 an overloaded "operator &" will be available if required. */
5207 if (code == ADDR_EXPR
5208 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5209 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5210 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5211 || (TREE_CODE (xarg) == OFFSET_REF)))
5212 /* Don't look for a function. */;
5213 else
5214 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5215 NULL_TREE, /*overload=*/NULL, complain);
5216 if (!exp && code == ADDR_EXPR)
5218 if (is_overloaded_fn (xarg))
5220 tree fn = get_first_fn (xarg);
5221 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5223 if (complain & tf_error)
5224 error (DECL_CONSTRUCTOR_P (fn)
5225 ? G_("taking address of constructor %qE")
5226 : G_("taking address of destructor %qE"),
5227 xarg);
5228 return error_mark_node;
5232 /* A pointer to member-function can be formed only by saying
5233 &X::mf. */
5234 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5235 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5237 if (TREE_CODE (xarg) != OFFSET_REF
5238 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5240 if (complain & tf_error)
5242 error ("invalid use of %qE to form a "
5243 "pointer-to-member-function", xarg);
5244 if (TREE_CODE (xarg) != OFFSET_REF)
5245 inform (input_location, " a qualified-id is required");
5247 return error_mark_node;
5249 else
5251 if (complain & tf_error)
5252 error ("parentheses around %qE cannot be used to form a"
5253 " pointer-to-member-function",
5254 xarg);
5255 else
5256 return error_mark_node;
5257 PTRMEM_OK_P (xarg) = 1;
5261 if (TREE_CODE (xarg) == OFFSET_REF)
5263 ptrmem = PTRMEM_OK_P (xarg);
5265 if (!ptrmem && !flag_ms_extensions
5266 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5268 /* A single non-static member, make sure we don't allow a
5269 pointer-to-member. */
5270 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5271 TREE_OPERAND (xarg, 0),
5272 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5273 PTRMEM_OK_P (xarg) = ptrmem;
5277 exp = cp_build_addr_expr_strict (xarg, complain);
5280 if (processing_template_decl && exp != error_mark_node)
5281 exp = build_min_non_dep (code, exp, orig_expr,
5282 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5283 if (TREE_CODE (exp) == ADDR_EXPR)
5284 PTRMEM_OK_P (exp) = ptrmem;
5285 return exp;
5288 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5289 constants, where a null value is represented by an INTEGER_CST of
5290 -1. */
5292 tree
5293 cp_truthvalue_conversion (tree expr)
5295 tree type = TREE_TYPE (expr);
5296 if (TYPE_PTRDATAMEM_P (type)
5297 /* Avoid ICE on invalid use of non-static member function. */
5298 || TREE_CODE (expr) == FUNCTION_DECL)
5299 return build_binary_op (EXPR_LOCATION (expr),
5300 NE_EXPR, expr, nullptr_node, 1);
5301 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5303 /* With -Wzero-as-null-pointer-constant do not warn for an
5304 'if (p)' or a 'while (!p)', where p is a pointer. */
5305 tree ret;
5306 ++c_inhibit_evaluation_warnings;
5307 ret = c_common_truthvalue_conversion (input_location, expr);
5308 --c_inhibit_evaluation_warnings;
5309 return ret;
5311 else
5312 return c_common_truthvalue_conversion (input_location, expr);
5315 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5317 tree
5318 condition_conversion (tree expr)
5320 tree t;
5321 if (processing_template_decl)
5322 return expr;
5323 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5324 tf_warning_or_error, LOOKUP_NORMAL);
5325 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5326 return t;
5329 /* Returns the address of T. This function will fold away
5330 ADDR_EXPR of INDIRECT_REF. */
5332 tree
5333 build_address (tree t)
5335 if (error_operand_p (t) || !cxx_mark_addressable (t))
5336 return error_mark_node;
5337 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5338 t = build_fold_addr_expr (t);
5339 if (TREE_CODE (t) != ADDR_EXPR)
5340 t = rvalue (t);
5341 return t;
5344 /* Return a NOP_EXPR converting EXPR to TYPE. */
5346 tree
5347 build_nop (tree type, tree expr)
5349 if (type == error_mark_node || error_operand_p (expr))
5350 return expr;
5351 return build1 (NOP_EXPR, type, expr);
5354 /* Take the address of ARG, whatever that means under C++ semantics.
5355 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5356 and class rvalues as well.
5358 Nothing should call this function directly; instead, callers should use
5359 cp_build_addr_expr or cp_build_addr_expr_strict. */
5361 static tree
5362 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5364 tree argtype;
5365 tree val;
5367 if (!arg || error_operand_p (arg))
5368 return error_mark_node;
5370 arg = mark_lvalue_use (arg);
5371 argtype = lvalue_type (arg);
5373 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5375 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5376 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5378 /* They're trying to take the address of a unique non-static
5379 member function. This is ill-formed (except in MS-land),
5380 but let's try to DTRT.
5381 Note: We only handle unique functions here because we don't
5382 want to complain if there's a static overload; non-unique
5383 cases will be handled by instantiate_type. But we need to
5384 handle this case here to allow casts on the resulting PMF.
5385 We could defer this in non-MS mode, but it's easier to give
5386 a useful error here. */
5388 /* Inside constant member functions, the `this' pointer
5389 contains an extra const qualifier. TYPE_MAIN_VARIANT
5390 is used here to remove this const from the diagnostics
5391 and the created OFFSET_REF. */
5392 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5393 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5394 mark_used (fn);
5396 if (! flag_ms_extensions)
5398 tree name = DECL_NAME (fn);
5399 if (!(complain & tf_error))
5400 return error_mark_node;
5401 else if (current_class_type
5402 && TREE_OPERAND (arg, 0) == current_class_ref)
5403 /* An expression like &memfn. */
5404 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5405 " or parenthesized non-static member function to form"
5406 " a pointer to member function. Say %<&%T::%D%>",
5407 base, name);
5408 else
5409 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5410 " function to form a pointer to member function."
5411 " Say %<&%T::%D%>",
5412 base, name);
5414 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5417 /* Uninstantiated types are all functions. Taking the
5418 address of a function is a no-op, so just return the
5419 argument. */
5420 if (type_unknown_p (arg))
5421 return build1 (ADDR_EXPR, unknown_type_node, arg);
5423 if (TREE_CODE (arg) == OFFSET_REF)
5424 /* We want a pointer to member; bypass all the code for actually taking
5425 the address of something. */
5426 goto offset_ref;
5428 /* Anything not already handled and not a true memory reference
5429 is an error. */
5430 if (TREE_CODE (argtype) != FUNCTION_TYPE
5431 && TREE_CODE (argtype) != METHOD_TYPE)
5433 cp_lvalue_kind kind = lvalue_kind (arg);
5434 if (kind == clk_none)
5436 if (complain & tf_error)
5437 lvalue_error (input_location, lv_addressof);
5438 return error_mark_node;
5440 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5442 if (!(complain & tf_error))
5443 return error_mark_node;
5444 if (kind & clk_class)
5445 /* Make this a permerror because we used to accept it. */
5446 permerror (input_location, "taking address of temporary");
5447 else
5448 error ("taking address of xvalue (rvalue reference)");
5452 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5454 tree type = build_pointer_type (TREE_TYPE (argtype));
5455 arg = build1 (CONVERT_EXPR, type, arg);
5456 return arg;
5458 else if (pedantic && DECL_MAIN_P (arg))
5460 /* ARM $3.4 */
5461 /* Apparently a lot of autoconf scripts for C++ packages do this,
5462 so only complain if -Wpedantic. */
5463 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5464 pedwarn (input_location, OPT_Wpedantic,
5465 "ISO C++ forbids taking address of function %<::main%>");
5466 else if (flag_pedantic_errors)
5467 return error_mark_node;
5470 /* Let &* cancel out to simplify resulting code. */
5471 if (INDIRECT_REF_P (arg))
5473 /* We don't need to have `current_class_ptr' wrapped in a
5474 NON_LVALUE_EXPR node. */
5475 if (arg == current_class_ref)
5476 return current_class_ptr;
5478 arg = TREE_OPERAND (arg, 0);
5479 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5481 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5482 arg = build1 (CONVERT_EXPR, type, arg);
5484 else
5485 /* Don't let this be an lvalue. */
5486 arg = rvalue (arg);
5487 return arg;
5490 /* ??? Cope with user tricks that amount to offsetof. */
5491 if (TREE_CODE (argtype) != FUNCTION_TYPE
5492 && TREE_CODE (argtype) != METHOD_TYPE
5493 && argtype != unknown_type_node
5494 && (val = get_base_address (arg))
5495 && COMPLETE_TYPE_P (TREE_TYPE (val))
5496 && INDIRECT_REF_P (val)
5497 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5499 tree type = build_pointer_type (argtype);
5500 return fold_convert (type, fold_offsetof_1 (arg));
5503 /* Handle complex lvalues (when permitted)
5504 by reduction to simpler cases. */
5505 val = unary_complex_lvalue (ADDR_EXPR, arg);
5506 if (val != 0)
5507 return val;
5509 switch (TREE_CODE (arg))
5511 CASE_CONVERT:
5512 case FLOAT_EXPR:
5513 case FIX_TRUNC_EXPR:
5514 /* Even if we're not being pedantic, we cannot allow this
5515 extension when we're instantiating in a SFINAE
5516 context. */
5517 if (! lvalue_p (arg) && complain == tf_none)
5519 if (complain & tf_error)
5520 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5521 else
5522 return error_mark_node;
5524 break;
5526 case BASELINK:
5527 arg = BASELINK_FUNCTIONS (arg);
5528 /* Fall through. */
5530 case OVERLOAD:
5531 arg = OVL_CURRENT (arg);
5532 break;
5534 case OFFSET_REF:
5535 offset_ref:
5536 /* Turn a reference to a non-static data member into a
5537 pointer-to-member. */
5539 tree type;
5540 tree t;
5542 gcc_assert (PTRMEM_OK_P (arg));
5544 t = TREE_OPERAND (arg, 1);
5545 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5547 if (complain & tf_error)
5548 error ("cannot create pointer to reference member %qD", t);
5549 return error_mark_node;
5552 type = build_ptrmem_type (context_for_name_lookup (t),
5553 TREE_TYPE (t));
5554 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5555 return t;
5558 default:
5559 break;
5562 if (argtype != error_mark_node)
5563 argtype = build_pointer_type (argtype);
5565 /* In a template, we are processing a non-dependent expression
5566 so we can just form an ADDR_EXPR with the correct type. */
5567 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5569 val = build_address (arg);
5570 if (TREE_CODE (arg) == OFFSET_REF)
5571 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5573 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5575 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5577 /* We can only get here with a single static member
5578 function. */
5579 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5580 && DECL_STATIC_FUNCTION_P (fn));
5581 mark_used (fn);
5582 val = build_address (fn);
5583 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5584 /* Do not lose object's side effects. */
5585 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5586 TREE_OPERAND (arg, 0), val);
5588 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5590 if (complain & tf_error)
5591 error ("attempt to take address of bit-field structure member %qD",
5592 TREE_OPERAND (arg, 1));
5593 return error_mark_node;
5595 else
5597 tree object = TREE_OPERAND (arg, 0);
5598 tree field = TREE_OPERAND (arg, 1);
5599 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5600 (TREE_TYPE (object), decl_type_context (field)));
5601 val = build_address (arg);
5604 if (TYPE_PTR_P (argtype)
5605 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5607 build_ptrmemfunc_type (argtype);
5608 val = build_ptrmemfunc (argtype, val, 0,
5609 /*c_cast_p=*/false,
5610 complain);
5613 return val;
5616 /* Take the address of ARG if it has one, even if it's an rvalue. */
5618 tree
5619 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5621 return cp_build_addr_expr_1 (arg, 0, complain);
5624 /* Take the address of ARG, but only if it's an lvalue. */
5626 static tree
5627 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5629 return cp_build_addr_expr_1 (arg, 1, complain);
5632 /* C++: Must handle pointers to members.
5634 Perhaps type instantiation should be extended to handle conversion
5635 from aggregates to types we don't yet know we want? (Or are those
5636 cases typically errors which should be reported?)
5638 NOCONVERT nonzero suppresses the default promotions
5639 (such as from short to int). */
5641 tree
5642 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5643 tsubst_flags_t complain)
5645 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5646 tree arg = xarg;
5647 tree argtype = 0;
5648 const char *errstring = NULL;
5649 tree val;
5650 const char *invalid_op_diag;
5652 if (!arg || error_operand_p (arg))
5653 return error_mark_node;
5655 if ((invalid_op_diag
5656 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5657 ? CONVERT_EXPR
5658 : code),
5659 TREE_TYPE (xarg))))
5661 if (complain & tf_error)
5662 error (invalid_op_diag);
5663 return error_mark_node;
5666 switch (code)
5668 case UNARY_PLUS_EXPR:
5669 case NEGATE_EXPR:
5671 int flags = WANT_ARITH | WANT_ENUM;
5672 /* Unary plus (but not unary minus) is allowed on pointers. */
5673 if (code == UNARY_PLUS_EXPR)
5674 flags |= WANT_POINTER;
5675 arg = build_expr_type_conversion (flags, arg, true);
5676 if (!arg)
5677 errstring = (code == NEGATE_EXPR
5678 ? _("wrong type argument to unary minus")
5679 : _("wrong type argument to unary plus"));
5680 else
5682 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5683 arg = cp_perform_integral_promotions (arg, complain);
5685 /* Make sure the result is not an lvalue: a unary plus or minus
5686 expression is always a rvalue. */
5687 arg = rvalue (arg);
5690 break;
5692 case BIT_NOT_EXPR:
5693 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5695 code = CONJ_EXPR;
5696 if (!noconvert)
5698 arg = cp_default_conversion (arg, complain);
5699 if (arg == error_mark_node)
5700 return error_mark_node;
5703 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5704 | WANT_VECTOR_OR_COMPLEX,
5705 arg, true)))
5706 errstring = _("wrong type argument to bit-complement");
5707 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5708 arg = cp_perform_integral_promotions (arg, complain);
5709 break;
5711 case ABS_EXPR:
5712 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5713 errstring = _("wrong type argument to abs");
5714 else if (!noconvert)
5716 arg = cp_default_conversion (arg, complain);
5717 if (arg == error_mark_node)
5718 return error_mark_node;
5720 break;
5722 case CONJ_EXPR:
5723 /* Conjugating a real value is a no-op, but allow it anyway. */
5724 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5725 errstring = _("wrong type argument to conjugation");
5726 else if (!noconvert)
5728 arg = cp_default_conversion (arg, complain);
5729 if (arg == error_mark_node)
5730 return error_mark_node;
5732 break;
5734 case TRUTH_NOT_EXPR:
5735 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5736 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5737 build_zero_cst (TREE_TYPE (arg)), complain);
5738 arg = perform_implicit_conversion (boolean_type_node, arg,
5739 complain);
5740 val = invert_truthvalue_loc (input_location, arg);
5741 if (arg != error_mark_node)
5742 return val;
5743 errstring = _("in argument to unary !");
5744 break;
5746 case NOP_EXPR:
5747 break;
5749 case REALPART_EXPR:
5750 case IMAGPART_EXPR:
5751 arg = build_real_imag_expr (input_location, code, arg);
5752 if (arg == error_mark_node)
5753 return arg;
5754 else
5755 return fold_if_not_in_template (arg);
5757 case PREINCREMENT_EXPR:
5758 case POSTINCREMENT_EXPR:
5759 case PREDECREMENT_EXPR:
5760 case POSTDECREMENT_EXPR:
5761 /* Handle complex lvalues (when permitted)
5762 by reduction to simpler cases. */
5764 val = unary_complex_lvalue (code, arg);
5765 if (val != 0)
5766 return val;
5768 arg = mark_lvalue_use (arg);
5770 /* Increment or decrement the real part of the value,
5771 and don't change the imaginary part. */
5772 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5774 tree real, imag;
5776 arg = stabilize_reference (arg);
5777 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5778 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5779 real = cp_build_unary_op (code, real, 1, complain);
5780 if (real == error_mark_node || imag == error_mark_node)
5781 return error_mark_node;
5782 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5783 real, imag);
5786 /* Report invalid types. */
5788 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5789 arg, true)))
5791 if (code == PREINCREMENT_EXPR)
5792 errstring = _("no pre-increment operator for type");
5793 else if (code == POSTINCREMENT_EXPR)
5794 errstring = _("no post-increment operator for type");
5795 else if (code == PREDECREMENT_EXPR)
5796 errstring = _("no pre-decrement operator for type");
5797 else
5798 errstring = _("no post-decrement operator for type");
5799 break;
5801 else if (arg == error_mark_node)
5802 return error_mark_node;
5804 /* Report something read-only. */
5806 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5807 || TREE_READONLY (arg))
5809 if (complain & tf_error)
5810 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5811 || code == POSTINCREMENT_EXPR)
5812 ? lv_increment : lv_decrement));
5813 else
5814 return error_mark_node;
5818 tree inc;
5819 tree declared_type = unlowered_expr_type (arg);
5821 argtype = TREE_TYPE (arg);
5823 /* ARM $5.2.5 last annotation says this should be forbidden. */
5824 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5826 if (complain & tf_error)
5827 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5828 ? G_("ISO C++ forbids incrementing an enum")
5829 : G_("ISO C++ forbids decrementing an enum"));
5830 else
5831 return error_mark_node;
5834 /* Compute the increment. */
5836 if (TYPE_PTR_P (argtype))
5838 tree type = complete_type (TREE_TYPE (argtype));
5840 if (!COMPLETE_OR_VOID_TYPE_P (type))
5842 if (complain & tf_error)
5843 error (((code == PREINCREMENT_EXPR
5844 || code == POSTINCREMENT_EXPR))
5845 ? G_("cannot increment a pointer to incomplete type %qT")
5846 : G_("cannot decrement a pointer to incomplete type %qT"),
5847 TREE_TYPE (argtype));
5848 else
5849 return error_mark_node;
5851 else if (!TYPE_PTROB_P (argtype))
5853 if (complain & tf_error)
5854 pedwarn (input_location, OPT_Wpointer_arith,
5855 (code == PREINCREMENT_EXPR
5856 || code == POSTINCREMENT_EXPR)
5857 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5858 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5859 argtype);
5860 else
5861 return error_mark_node;
5864 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5866 else
5867 inc = VECTOR_TYPE_P (argtype)
5868 ? build_one_cst (argtype)
5869 : integer_one_node;
5871 inc = cp_convert (argtype, inc, complain);
5873 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5874 need to ask Objective-C to build the increment or decrement
5875 expression for it. */
5876 if (objc_is_property_ref (arg))
5877 return objc_build_incr_expr_for_property_ref (input_location, code,
5878 arg, inc);
5880 /* Complain about anything else that is not a true lvalue. */
5881 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5882 || code == POSTINCREMENT_EXPR)
5883 ? lv_increment : lv_decrement),
5884 complain))
5885 return error_mark_node;
5887 /* Forbid using -- on `bool'. */
5888 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5890 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5892 if (complain & tf_error)
5893 error ("invalid use of Boolean expression as operand "
5894 "to %<operator--%>");
5895 return error_mark_node;
5897 val = boolean_increment (code, arg);
5899 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5900 /* An rvalue has no cv-qualifiers. */
5901 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5902 else
5903 val = build2 (code, TREE_TYPE (arg), arg, inc);
5905 TREE_SIDE_EFFECTS (val) = 1;
5906 return val;
5909 case ADDR_EXPR:
5910 /* Note that this operation never does default_conversion
5911 regardless of NOCONVERT. */
5912 return cp_build_addr_expr (arg, complain);
5914 default:
5915 break;
5918 if (!errstring)
5920 if (argtype == 0)
5921 argtype = TREE_TYPE (arg);
5922 return fold_if_not_in_template (build1 (code, argtype, arg));
5925 if (complain & tf_error)
5926 error ("%s", errstring);
5927 return error_mark_node;
5930 /* Hook for the c-common bits that build a unary op. */
5931 tree
5932 build_unary_op (location_t /*location*/,
5933 enum tree_code code, tree xarg, int noconvert)
5935 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5938 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5939 for certain kinds of expressions which are not really lvalues
5940 but which we can accept as lvalues.
5942 If ARG is not a kind of expression we can handle, return
5943 NULL_TREE. */
5945 tree
5946 unary_complex_lvalue (enum tree_code code, tree arg)
5948 /* Inside a template, making these kinds of adjustments is
5949 pointless; we are only concerned with the type of the
5950 expression. */
5951 if (processing_template_decl)
5952 return NULL_TREE;
5954 /* Handle (a, b) used as an "lvalue". */
5955 if (TREE_CODE (arg) == COMPOUND_EXPR)
5957 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5958 tf_warning_or_error);
5959 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5960 TREE_OPERAND (arg, 0), real_result);
5963 /* Handle (a ? b : c) used as an "lvalue". */
5964 if (TREE_CODE (arg) == COND_EXPR
5965 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5966 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5968 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5969 if (TREE_CODE (arg) == MODIFY_EXPR
5970 || TREE_CODE (arg) == PREINCREMENT_EXPR
5971 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5973 tree lvalue = TREE_OPERAND (arg, 0);
5974 if (TREE_SIDE_EFFECTS (lvalue))
5976 lvalue = stabilize_reference (lvalue);
5977 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5978 lvalue, TREE_OPERAND (arg, 1));
5980 return unary_complex_lvalue
5981 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5984 if (code != ADDR_EXPR)
5985 return NULL_TREE;
5987 /* Handle (a = b) used as an "lvalue" for `&'. */
5988 if (TREE_CODE (arg) == MODIFY_EXPR
5989 || TREE_CODE (arg) == INIT_EXPR)
5991 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5992 tf_warning_or_error);
5993 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5994 arg, real_result);
5995 TREE_NO_WARNING (arg) = 1;
5996 return arg;
5999 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6000 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6001 || TREE_CODE (arg) == OFFSET_REF)
6002 return NULL_TREE;
6004 /* We permit compiler to make function calls returning
6005 objects of aggregate type look like lvalues. */
6007 tree targ = arg;
6009 if (TREE_CODE (targ) == SAVE_EXPR)
6010 targ = TREE_OPERAND (targ, 0);
6012 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6014 if (TREE_CODE (arg) == SAVE_EXPR)
6015 targ = arg;
6016 else
6017 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6018 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6021 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6022 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6023 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6026 /* Don't let anything else be handled specially. */
6027 return NULL_TREE;
6030 /* Mark EXP saying that we need to be able to take the
6031 address of it; it should not be allocated in a register.
6032 Value is true if successful.
6034 C++: we do not allow `current_class_ptr' to be addressable. */
6036 bool
6037 cxx_mark_addressable (tree exp)
6039 tree x = exp;
6041 while (1)
6042 switch (TREE_CODE (x))
6044 case ADDR_EXPR:
6045 case COMPONENT_REF:
6046 case ARRAY_REF:
6047 case REALPART_EXPR:
6048 case IMAGPART_EXPR:
6049 x = TREE_OPERAND (x, 0);
6050 break;
6052 case PARM_DECL:
6053 if (x == current_class_ptr)
6055 error ("cannot take the address of %<this%>, which is an rvalue expression");
6056 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6057 return true;
6059 /* Fall through. */
6061 case VAR_DECL:
6062 /* Caller should not be trying to mark initialized
6063 constant fields addressable. */
6064 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6065 || DECL_IN_AGGR_P (x) == 0
6066 || TREE_STATIC (x)
6067 || DECL_EXTERNAL (x));
6068 /* Fall through. */
6070 case RESULT_DECL:
6071 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6072 && !DECL_ARTIFICIAL (x))
6074 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6076 error
6077 ("address of explicit register variable %qD requested", x);
6078 return false;
6080 else if (extra_warnings)
6081 warning
6082 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6084 TREE_ADDRESSABLE (x) = 1;
6085 return true;
6087 case CONST_DECL:
6088 case FUNCTION_DECL:
6089 TREE_ADDRESSABLE (x) = 1;
6090 return true;
6092 case CONSTRUCTOR:
6093 TREE_ADDRESSABLE (x) = 1;
6094 return true;
6096 case TARGET_EXPR:
6097 TREE_ADDRESSABLE (x) = 1;
6098 cxx_mark_addressable (TREE_OPERAND (x, 0));
6099 return true;
6101 default:
6102 return true;
6106 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6108 tree
6109 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6110 tsubst_flags_t complain)
6112 tree orig_ifexp = ifexp;
6113 tree orig_op1 = op1;
6114 tree orig_op2 = op2;
6115 tree expr;
6117 if (processing_template_decl)
6119 /* The standard says that the expression is type-dependent if
6120 IFEXP is type-dependent, even though the eventual type of the
6121 expression doesn't dependent on IFEXP. */
6122 if (type_dependent_expression_p (ifexp)
6123 /* As a GNU extension, the middle operand may be omitted. */
6124 || (op1 && type_dependent_expression_p (op1))
6125 || type_dependent_expression_p (op2))
6126 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6127 ifexp = build_non_dependent_expr (ifexp);
6128 if (op1)
6129 op1 = build_non_dependent_expr (op1);
6130 op2 = build_non_dependent_expr (op2);
6133 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6134 if (processing_template_decl && expr != error_mark_node
6135 && TREE_CODE (expr) != VEC_COND_EXPR)
6137 tree min = build_min_non_dep (COND_EXPR, expr,
6138 orig_ifexp, orig_op1, orig_op2);
6139 /* In C++11, remember that the result is an lvalue or xvalue.
6140 In C++98, lvalue_kind can just assume lvalue in a template. */
6141 if (cxx_dialect >= cxx11
6142 && lvalue_or_rvalue_with_address_p (expr)
6143 && !lvalue_or_rvalue_with_address_p (min))
6144 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6145 !real_lvalue_p (expr));
6146 expr = convert_from_reference (min);
6148 return expr;
6151 /* Given a list of expressions, return a compound expression
6152 that performs them all and returns the value of the last of them. */
6154 tree
6155 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6156 tsubst_flags_t complain)
6158 tree expr = TREE_VALUE (list);
6160 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6161 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6163 if (complain & tf_error)
6164 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6165 "list-initializer for non-class type must not "
6166 "be parenthesized");
6167 else
6168 return error_mark_node;
6171 if (TREE_CHAIN (list))
6173 if (complain & tf_error)
6174 switch (exp)
6176 case ELK_INIT:
6177 permerror (input_location, "expression list treated as compound "
6178 "expression in initializer");
6179 break;
6180 case ELK_MEM_INIT:
6181 permerror (input_location, "expression list treated as compound "
6182 "expression in mem-initializer");
6183 break;
6184 case ELK_FUNC_CAST:
6185 permerror (input_location, "expression list treated as compound "
6186 "expression in functional cast");
6187 break;
6188 default:
6189 gcc_unreachable ();
6191 else
6192 return error_mark_node;
6194 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6195 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6196 expr, TREE_VALUE (list), complain);
6199 return expr;
6202 /* Like build_x_compound_expr_from_list, but using a VEC. */
6204 tree
6205 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6206 tsubst_flags_t complain)
6208 if (vec_safe_is_empty (vec))
6209 return NULL_TREE;
6210 else if (vec->length () == 1)
6211 return (*vec)[0];
6212 else
6214 tree expr;
6215 unsigned int ix;
6216 tree t;
6218 if (msg != NULL)
6220 if (complain & tf_error)
6221 permerror (input_location,
6222 "%s expression list treated as compound expression",
6223 msg);
6224 else
6225 return error_mark_node;
6228 expr = (*vec)[0];
6229 for (ix = 1; vec->iterate (ix, &t); ++ix)
6230 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6231 t, complain);
6233 return expr;
6237 /* Handle overloading of the ',' operator when needed. */
6239 tree
6240 build_x_compound_expr (location_t loc, tree op1, tree op2,
6241 tsubst_flags_t complain)
6243 tree result;
6244 tree orig_op1 = op1;
6245 tree orig_op2 = op2;
6247 if (processing_template_decl)
6249 if (type_dependent_expression_p (op1)
6250 || type_dependent_expression_p (op2))
6251 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6252 op1 = build_non_dependent_expr (op1);
6253 op2 = build_non_dependent_expr (op2);
6256 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6257 NULL_TREE, /*overload=*/NULL, complain);
6258 if (!result)
6259 result = cp_build_compound_expr (op1, op2, complain);
6261 if (processing_template_decl && result != error_mark_node)
6262 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6264 return result;
6267 /* Like cp_build_compound_expr, but for the c-common bits. */
6269 tree
6270 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6272 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6275 /* Build a compound expression. */
6277 tree
6278 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6280 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6282 if (lhs == error_mark_node || rhs == error_mark_node)
6283 return error_mark_node;
6285 if (flag_cilkplus
6286 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6287 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6289 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6290 : EXPR_LOCATION (rhs));
6291 error_at (loc,
6292 "spawned function call cannot be part of a comma expression");
6293 return error_mark_node;
6296 if (TREE_CODE (rhs) == TARGET_EXPR)
6298 /* If the rhs is a TARGET_EXPR, then build the compound
6299 expression inside the target_expr's initializer. This
6300 helps the compiler to eliminate unnecessary temporaries. */
6301 tree init = TREE_OPERAND (rhs, 1);
6303 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6304 TREE_OPERAND (rhs, 1) = init;
6306 return rhs;
6309 if (type_unknown_p (rhs))
6311 if (complain & tf_error)
6312 error ("no context to resolve type of %qE", rhs);
6313 return error_mark_node;
6316 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6319 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6320 casts away constness. CAST gives the type of cast. Returns true
6321 if the cast is ill-formed, false if it is well-formed.
6323 ??? This function warns for casting away any qualifier not just
6324 const. We would like to specify exactly what qualifiers are casted
6325 away.
6328 static bool
6329 check_for_casting_away_constness (tree src_type, tree dest_type,
6330 enum tree_code cast, tsubst_flags_t complain)
6332 /* C-style casts are allowed to cast away constness. With
6333 WARN_CAST_QUAL, we still want to issue a warning. */
6334 if (cast == CAST_EXPR && !warn_cast_qual)
6335 return false;
6337 if (!casts_away_constness (src_type, dest_type, complain))
6338 return false;
6340 switch (cast)
6342 case CAST_EXPR:
6343 if (complain & tf_warning)
6344 warning (OPT_Wcast_qual,
6345 "cast from type %qT to type %qT casts away qualifiers",
6346 src_type, dest_type);
6347 return false;
6349 case STATIC_CAST_EXPR:
6350 if (complain & tf_error)
6351 error ("static_cast from type %qT to type %qT casts away qualifiers",
6352 src_type, dest_type);
6353 return true;
6355 case REINTERPRET_CAST_EXPR:
6356 if (complain & tf_error)
6357 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6358 src_type, dest_type);
6359 return true;
6361 default:
6362 gcc_unreachable();
6367 Warns if the cast from expression EXPR to type TYPE is useless.
6369 void
6370 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6372 if (warn_useless_cast
6373 && complain & tf_warning)
6375 if ((TREE_CODE (type) == REFERENCE_TYPE
6376 && (TYPE_REF_IS_RVALUE (type)
6377 ? xvalue_p (expr) : real_lvalue_p (expr))
6378 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6379 || same_type_p (TREE_TYPE (expr), type))
6380 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6384 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6385 (another pointer-to-member type in the same hierarchy) and return
6386 the converted expression. If ALLOW_INVERSE_P is permitted, a
6387 pointer-to-derived may be converted to pointer-to-base; otherwise,
6388 only the other direction is permitted. If C_CAST_P is true, this
6389 conversion is taking place as part of a C-style cast. */
6391 tree
6392 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6393 bool c_cast_p, tsubst_flags_t complain)
6395 if (TYPE_PTRDATAMEM_P (type))
6397 tree delta;
6399 if (TREE_CODE (expr) == PTRMEM_CST)
6400 expr = cplus_expand_constant (expr);
6401 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6402 TYPE_PTRMEM_CLASS_TYPE (type),
6403 allow_inverse_p,
6404 c_cast_p, complain);
6405 if (delta == error_mark_node)
6406 return error_mark_node;
6408 if (!integer_zerop (delta))
6410 tree cond, op1, op2;
6412 cond = cp_build_binary_op (input_location,
6413 EQ_EXPR,
6414 expr,
6415 build_int_cst (TREE_TYPE (expr), -1),
6416 complain);
6417 op1 = build_nop (ptrdiff_type_node, expr);
6418 op2 = cp_build_binary_op (input_location,
6419 PLUS_EXPR, op1, delta,
6420 complain);
6422 expr = fold_build3_loc (input_location,
6423 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6427 return build_nop (type, expr);
6429 else
6430 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6431 allow_inverse_p, c_cast_p, complain);
6434 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6435 this static_cast is being attempted as one of the possible casts
6436 allowed by a C-style cast. (In that case, accessibility of base
6437 classes is not considered, and it is OK to cast away
6438 constness.) Return the result of the cast. *VALID_P is set to
6439 indicate whether or not the cast was valid. */
6441 static tree
6442 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6443 bool *valid_p, tsubst_flags_t complain)
6445 tree intype;
6446 tree result;
6447 cp_lvalue_kind clk;
6449 /* Assume the cast is valid. */
6450 *valid_p = true;
6452 intype = unlowered_expr_type (expr);
6454 /* Save casted types in the function's used types hash table. */
6455 used_types_insert (type);
6457 /* [expr.static.cast]
6459 An lvalue of type "cv1 B", where B is a class type, can be cast
6460 to type "reference to cv2 D", where D is a class derived (clause
6461 _class.derived_) from B, if a valid standard conversion from
6462 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6463 same cv-qualification as, or greater cv-qualification than, cv1,
6464 and B is not a virtual base class of D. */
6465 /* We check this case before checking the validity of "TYPE t =
6466 EXPR;" below because for this case:
6468 struct B {};
6469 struct D : public B { D(const B&); };
6470 extern B& b;
6471 void f() { static_cast<const D&>(b); }
6473 we want to avoid constructing a new D. The standard is not
6474 completely clear about this issue, but our interpretation is
6475 consistent with other compilers. */
6476 if (TREE_CODE (type) == REFERENCE_TYPE
6477 && CLASS_TYPE_P (TREE_TYPE (type))
6478 && CLASS_TYPE_P (intype)
6479 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6480 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6481 && can_convert_standard (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6482 build_pointer_type (TYPE_MAIN_VARIANT
6483 (TREE_TYPE (type))),
6484 complain)
6485 && (c_cast_p
6486 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6488 tree base;
6490 /* There is a standard conversion from "D*" to "B*" even if "B"
6491 is ambiguous or inaccessible. If this is really a
6492 static_cast, then we check both for inaccessibility and
6493 ambiguity. However, if this is a static_cast being performed
6494 because the user wrote a C-style cast, then accessibility is
6495 not considered. */
6496 base = lookup_base (TREE_TYPE (type), intype,
6497 c_cast_p ? ba_unique : ba_check,
6498 NULL, complain);
6499 expr = build_address (expr);
6501 if (flag_sanitize & SANITIZE_VPTR)
6503 tree ubsan_check
6504 = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
6505 if (ubsan_check)
6506 expr = ubsan_check;
6509 /* Convert from "B*" to "D*". This function will check that "B"
6510 is not a virtual base of "D". */
6511 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6512 complain);
6514 /* Convert the pointer to a reference -- but then remember that
6515 there are no expressions with reference type in C++.
6517 We call rvalue so that there's an actual tree code
6518 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6519 is a variable with the same type, the conversion would get folded
6520 away, leaving just the variable and causing lvalue_kind to give
6521 the wrong answer. */
6522 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6525 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6526 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6527 if (TREE_CODE (type) == REFERENCE_TYPE
6528 && TYPE_REF_IS_RVALUE (type)
6529 && (clk = real_lvalue_p (expr))
6530 && reference_related_p (TREE_TYPE (type), intype)
6531 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6533 if (clk == clk_ordinary)
6535 /* Handle the (non-bit-field) lvalue case here by casting to
6536 lvalue reference and then changing it to an rvalue reference.
6537 Casting an xvalue to rvalue reference will be handled by the
6538 main code path. */
6539 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6540 result = (perform_direct_initialization_if_possible
6541 (lref, expr, c_cast_p, complain));
6542 result = cp_fold_convert (type, result);
6543 /* Make sure we don't fold back down to a named rvalue reference,
6544 because that would be an lvalue. */
6545 if (DECL_P (result))
6546 result = build1 (NON_LVALUE_EXPR, type, result);
6547 return convert_from_reference (result);
6549 else
6550 /* For a bit-field or packed field, bind to a temporary. */
6551 expr = rvalue (expr);
6554 /* Resolve overloaded address here rather than once in
6555 implicit_conversion and again in the inverse code below. */
6556 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6558 expr = instantiate_type (type, expr, complain);
6559 intype = TREE_TYPE (expr);
6562 /* [expr.static.cast]
6564 Any expression can be explicitly converted to type cv void. */
6565 if (VOID_TYPE_P (type))
6566 return convert_to_void (expr, ICV_CAST, complain);
6568 /* [class.abstract]
6569 An abstract class shall not be used ... as the type of an explicit
6570 conversion. */
6571 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6572 return error_mark_node;
6574 /* [expr.static.cast]
6576 An expression e can be explicitly converted to a type T using a
6577 static_cast of the form static_cast<T>(e) if the declaration T
6578 t(e);" is well-formed, for some invented temporary variable
6579 t. */
6580 result = perform_direct_initialization_if_possible (type, expr,
6581 c_cast_p, complain);
6582 if (result)
6584 result = convert_from_reference (result);
6586 /* [expr.static.cast]
6588 If T is a reference type, the result is an lvalue; otherwise,
6589 the result is an rvalue. */
6590 if (TREE_CODE (type) != REFERENCE_TYPE)
6591 result = rvalue (result);
6592 return result;
6595 /* [expr.static.cast]
6597 The inverse of any standard conversion sequence (clause _conv_),
6598 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6599 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6600 (_conv.bool_) conversions, can be performed explicitly using
6601 static_cast subject to the restriction that the explicit
6602 conversion does not cast away constness (_expr.const.cast_), and
6603 the following additional rules for specific cases: */
6604 /* For reference, the conversions not excluded are: integral
6605 promotions, floating point promotion, integral conversions,
6606 floating point conversions, floating-integral conversions,
6607 pointer conversions, and pointer to member conversions. */
6608 /* DR 128
6610 A value of integral _or enumeration_ type can be explicitly
6611 converted to an enumeration type. */
6612 /* The effect of all that is that any conversion between any two
6613 types which are integral, floating, or enumeration types can be
6614 performed. */
6615 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6616 || SCALAR_FLOAT_TYPE_P (type))
6617 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6618 || SCALAR_FLOAT_TYPE_P (intype)))
6619 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6621 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6622 && CLASS_TYPE_P (TREE_TYPE (type))
6623 && CLASS_TYPE_P (TREE_TYPE (intype))
6624 && can_convert_standard (build_pointer_type (TYPE_MAIN_VARIANT
6625 (TREE_TYPE (intype))),
6626 build_pointer_type (TYPE_MAIN_VARIANT
6627 (TREE_TYPE (type))),
6628 complain))
6630 tree base;
6632 if (!c_cast_p
6633 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6634 complain))
6635 return error_mark_node;
6636 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6637 c_cast_p ? ba_unique : ba_check,
6638 NULL, complain);
6639 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6640 complain);
6642 if (flag_sanitize & SANITIZE_VPTR)
6644 tree ubsan_check
6645 = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
6646 if (ubsan_check)
6647 expr = ubsan_check;
6650 return cp_fold_convert (type, expr);
6653 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6654 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6656 tree c1;
6657 tree c2;
6658 tree t1;
6659 tree t2;
6661 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6662 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6664 if (TYPE_PTRDATAMEM_P (type))
6666 t1 = (build_ptrmem_type
6667 (c1,
6668 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6669 t2 = (build_ptrmem_type
6670 (c2,
6671 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6673 else
6675 t1 = intype;
6676 t2 = type;
6678 if (can_convert_standard (t1, t2, complain)
6679 || can_convert_standard (t2, t1, complain))
6681 if (!c_cast_p
6682 && check_for_casting_away_constness (intype, type,
6683 STATIC_CAST_EXPR,
6684 complain))
6685 return error_mark_node;
6686 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6687 c_cast_p, complain);
6691 /* [expr.static.cast]
6693 An rvalue of type "pointer to cv void" can be explicitly
6694 converted to a pointer to object type. A value of type pointer
6695 to object converted to "pointer to cv void" and back to the
6696 original pointer type will have its original value. */
6697 if (TYPE_PTR_P (intype)
6698 && VOID_TYPE_P (TREE_TYPE (intype))
6699 && TYPE_PTROB_P (type))
6701 if (!c_cast_p
6702 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6703 complain))
6704 return error_mark_node;
6705 return build_nop (type, expr);
6708 *valid_p = false;
6709 return error_mark_node;
6712 /* Return an expression representing static_cast<TYPE>(EXPR). */
6714 tree
6715 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6717 tree result;
6718 bool valid_p;
6720 if (type == error_mark_node || expr == error_mark_node)
6721 return error_mark_node;
6723 if (processing_template_decl)
6725 expr = build_min (STATIC_CAST_EXPR, type, expr);
6726 /* We don't know if it will or will not have side effects. */
6727 TREE_SIDE_EFFECTS (expr) = 1;
6728 return convert_from_reference (expr);
6731 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6732 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6733 if (TREE_CODE (type) != REFERENCE_TYPE
6734 && TREE_CODE (expr) == NOP_EXPR
6735 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6736 expr = TREE_OPERAND (expr, 0);
6738 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6739 complain);
6740 if (valid_p)
6742 if (result != error_mark_node)
6743 maybe_warn_about_useless_cast (type, expr, complain);
6744 return result;
6747 if (complain & tf_error)
6748 error ("invalid static_cast from type %qT to type %qT",
6749 TREE_TYPE (expr), type);
6750 return error_mark_node;
6753 /* EXPR is an expression with member function or pointer-to-member
6754 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6755 not permitted by ISO C++, but we accept it in some modes. If we
6756 are not in one of those modes, issue a diagnostic. Return the
6757 converted expression. */
6759 tree
6760 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6762 tree intype;
6763 tree decl;
6765 intype = TREE_TYPE (expr);
6766 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6767 || TREE_CODE (intype) == METHOD_TYPE);
6769 if (!(complain & tf_warning_or_error))
6770 return error_mark_node;
6772 if (pedantic || warn_pmf2ptr)
6773 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6774 "converting from %qT to %qT", intype, type);
6776 if (TREE_CODE (intype) == METHOD_TYPE)
6777 expr = build_addr_func (expr, complain);
6778 else if (TREE_CODE (expr) == PTRMEM_CST)
6779 expr = build_address (PTRMEM_CST_MEMBER (expr));
6780 else
6782 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6783 decl = build_address (decl);
6784 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6787 if (expr == error_mark_node)
6788 return error_mark_node;
6790 return build_nop (type, expr);
6793 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6794 If C_CAST_P is true, this reinterpret cast is being done as part of
6795 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6796 indicate whether or not reinterpret_cast was valid. */
6798 static tree
6799 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6800 bool *valid_p, tsubst_flags_t complain)
6802 tree intype;
6804 /* Assume the cast is invalid. */
6805 if (valid_p)
6806 *valid_p = true;
6808 if (type == error_mark_node || error_operand_p (expr))
6809 return error_mark_node;
6811 intype = TREE_TYPE (expr);
6813 /* Save casted types in the function's used types hash table. */
6814 used_types_insert (type);
6816 /* [expr.reinterpret.cast]
6817 An lvalue expression of type T1 can be cast to the type
6818 "reference to T2" if an expression of type "pointer to T1" can be
6819 explicitly converted to the type "pointer to T2" using a
6820 reinterpret_cast. */
6821 if (TREE_CODE (type) == REFERENCE_TYPE)
6823 if (! real_lvalue_p (expr))
6825 if (complain & tf_error)
6826 error ("invalid cast of an rvalue expression of type "
6827 "%qT to type %qT",
6828 intype, type);
6829 return error_mark_node;
6832 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6833 "B" are related class types; the reinterpret_cast does not
6834 adjust the pointer. */
6835 if (TYPE_PTR_P (intype)
6836 && (complain & tf_warning)
6837 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6838 COMPARE_BASE | COMPARE_DERIVED)))
6839 warning (0, "casting %qT to %qT does not dereference pointer",
6840 intype, type);
6842 expr = cp_build_addr_expr (expr, complain);
6844 if (warn_strict_aliasing > 2)
6845 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6847 if (expr != error_mark_node)
6848 expr = build_reinterpret_cast_1
6849 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6850 valid_p, complain);
6851 if (expr != error_mark_node)
6852 /* cp_build_indirect_ref isn't right for rvalue refs. */
6853 expr = convert_from_reference (fold_convert (type, expr));
6854 return expr;
6857 /* As a G++ extension, we consider conversions from member
6858 functions, and pointers to member functions to
6859 pointer-to-function and pointer-to-void types. If
6860 -Wno-pmf-conversions has not been specified,
6861 convert_member_func_to_ptr will issue an error message. */
6862 if ((TYPE_PTRMEMFUNC_P (intype)
6863 || TREE_CODE (intype) == METHOD_TYPE)
6864 && TYPE_PTR_P (type)
6865 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6866 || VOID_TYPE_P (TREE_TYPE (type))))
6867 return convert_member_func_to_ptr (type, expr, complain);
6869 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6870 array-to-pointer, and function-to-pointer conversions are
6871 performed. */
6872 expr = decay_conversion (expr, complain);
6874 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6875 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6876 if (TREE_CODE (expr) == NOP_EXPR
6877 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6878 expr = TREE_OPERAND (expr, 0);
6880 if (error_operand_p (expr))
6881 return error_mark_node;
6883 intype = TREE_TYPE (expr);
6885 /* [expr.reinterpret.cast]
6886 A pointer can be converted to any integral type large enough to
6887 hold it. ... A value of type std::nullptr_t can be converted to
6888 an integral type; the conversion has the same meaning and
6889 validity as a conversion of (void*)0 to the integral type. */
6890 if (CP_INTEGRAL_TYPE_P (type)
6891 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6893 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6895 if (complain & tf_error)
6896 permerror (input_location, "cast from %qT to %qT loses precision",
6897 intype, type);
6898 else
6899 return error_mark_node;
6901 if (NULLPTR_TYPE_P (intype))
6902 return build_int_cst (type, 0);
6904 /* [expr.reinterpret.cast]
6905 A value of integral or enumeration type can be explicitly
6906 converted to a pointer. */
6907 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6908 /* OK */
6910 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6911 || TYPE_PTR_OR_PTRMEM_P (type))
6912 && same_type_p (type, intype))
6913 /* DR 799 */
6914 return rvalue (expr);
6915 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6916 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6917 return fold_if_not_in_template (build_nop (type, expr));
6918 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6919 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6921 tree sexpr = expr;
6923 if (!c_cast_p
6924 && check_for_casting_away_constness (intype, type,
6925 REINTERPRET_CAST_EXPR,
6926 complain))
6927 return error_mark_node;
6928 /* Warn about possible alignment problems. */
6929 if (STRICT_ALIGNMENT && warn_cast_align
6930 && (complain & tf_warning)
6931 && !VOID_TYPE_P (type)
6932 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6933 && COMPLETE_TYPE_P (TREE_TYPE (type))
6934 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6935 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6936 warning (OPT_Wcast_align, "cast from %qT to %qT "
6937 "increases required alignment of target type", intype, type);
6939 /* We need to strip nops here, because the front end likes to
6940 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6941 STRIP_NOPS (sexpr);
6942 if (warn_strict_aliasing <= 2)
6943 strict_aliasing_warning (intype, type, sexpr);
6945 return fold_if_not_in_template (build_nop (type, expr));
6947 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6948 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6950 if (complain & tf_warning)
6951 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6952 object pointer type or vice versa is conditionally-supported." */
6953 warning (OPT_Wconditionally_supported,
6954 "casting between pointer-to-function and pointer-to-object "
6955 "is conditionally-supported");
6956 return fold_if_not_in_template (build_nop (type, expr));
6958 else if (TREE_CODE (type) == VECTOR_TYPE)
6959 return fold_if_not_in_template (convert_to_vector (type, expr));
6960 else if (TREE_CODE (intype) == VECTOR_TYPE
6961 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6962 return fold_if_not_in_template (convert_to_integer (type, expr));
6963 else
6965 if (valid_p)
6966 *valid_p = false;
6967 if (complain & tf_error)
6968 error ("invalid cast from type %qT to type %qT", intype, type);
6969 return error_mark_node;
6972 return cp_convert (type, expr, complain);
6975 tree
6976 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6978 tree r;
6980 if (type == error_mark_node || expr == error_mark_node)
6981 return error_mark_node;
6983 if (processing_template_decl)
6985 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6987 if (!TREE_SIDE_EFFECTS (t)
6988 && type_dependent_expression_p (expr))
6989 /* There might turn out to be side effects inside expr. */
6990 TREE_SIDE_EFFECTS (t) = 1;
6991 return convert_from_reference (t);
6994 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6995 /*valid_p=*/NULL, complain);
6996 if (r != error_mark_node)
6997 maybe_warn_about_useless_cast (type, expr, complain);
6998 return r;
7001 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7002 return an appropriate expression. Otherwise, return
7003 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7004 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7005 performing a C-style cast, its value upon return will indicate
7006 whether or not the conversion succeeded. */
7008 static tree
7009 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7010 bool *valid_p)
7012 tree src_type;
7013 tree reference_type;
7015 /* Callers are responsible for handling error_mark_node as a
7016 destination type. */
7017 gcc_assert (dst_type != error_mark_node);
7018 /* In a template, callers should be building syntactic
7019 representations of casts, not using this machinery. */
7020 gcc_assert (!processing_template_decl);
7022 /* Assume the conversion is invalid. */
7023 if (valid_p)
7024 *valid_p = false;
7026 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7028 if (complain & tf_error)
7029 error ("invalid use of const_cast with type %qT, "
7030 "which is not a pointer, "
7031 "reference, nor a pointer-to-data-member type", dst_type);
7032 return error_mark_node;
7035 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7037 if (complain & tf_error)
7038 error ("invalid use of const_cast with type %qT, which is a pointer "
7039 "or reference to a function type", dst_type);
7040 return error_mark_node;
7043 /* Save casted types in the function's used types hash table. */
7044 used_types_insert (dst_type);
7046 src_type = TREE_TYPE (expr);
7047 /* Expressions do not really have reference types. */
7048 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7049 src_type = TREE_TYPE (src_type);
7051 /* [expr.const.cast]
7053 For two object types T1 and T2, if a pointer to T1 can be explicitly
7054 converted to the type "pointer to T2" using a const_cast, then the
7055 following conversions can also be made:
7057 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7058 type T2 using the cast const_cast<T2&>;
7060 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7061 type T2 using the cast const_cast<T2&&>; and
7063 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7064 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7066 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7068 reference_type = dst_type;
7069 if (!TYPE_REF_IS_RVALUE (dst_type)
7070 ? real_lvalue_p (expr)
7071 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7072 ? lvalue_p (expr)
7073 : lvalue_or_rvalue_with_address_p (expr)))
7074 /* OK. */;
7075 else
7077 if (complain & tf_error)
7078 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7079 src_type, dst_type);
7080 return error_mark_node;
7082 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7083 src_type = build_pointer_type (src_type);
7085 else
7087 reference_type = NULL_TREE;
7088 /* If the destination type is not a reference type, the
7089 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7090 conversions are performed. */
7091 src_type = type_decays_to (src_type);
7092 if (src_type == error_mark_node)
7093 return error_mark_node;
7096 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7098 if (comp_ptr_ttypes_const (dst_type, src_type))
7100 if (valid_p)
7102 *valid_p = true;
7103 /* This cast is actually a C-style cast. Issue a warning if
7104 the user is making a potentially unsafe cast. */
7105 check_for_casting_away_constness (src_type, dst_type,
7106 CAST_EXPR, complain);
7108 if (reference_type)
7110 expr = cp_build_addr_expr (expr, complain);
7111 if (expr == error_mark_node)
7112 return error_mark_node;
7113 expr = build_nop (reference_type, expr);
7114 return convert_from_reference (expr);
7116 else
7118 expr = decay_conversion (expr, complain);
7119 if (expr == error_mark_node)
7120 return error_mark_node;
7122 /* build_c_cast puts on a NOP_EXPR to make the result not an
7123 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7124 non-lvalue context. */
7125 if (TREE_CODE (expr) == NOP_EXPR
7126 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7127 expr = TREE_OPERAND (expr, 0);
7128 return build_nop (dst_type, expr);
7131 else if (valid_p
7132 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7133 TREE_TYPE (src_type)))
7134 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7135 complain);
7138 if (complain & tf_error)
7139 error ("invalid const_cast from type %qT to type %qT",
7140 src_type, dst_type);
7141 return error_mark_node;
7144 tree
7145 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7147 tree r;
7149 if (type == error_mark_node || error_operand_p (expr))
7150 return error_mark_node;
7152 if (processing_template_decl)
7154 tree t = build_min (CONST_CAST_EXPR, type, expr);
7156 if (!TREE_SIDE_EFFECTS (t)
7157 && type_dependent_expression_p (expr))
7158 /* There might turn out to be side effects inside expr. */
7159 TREE_SIDE_EFFECTS (t) = 1;
7160 return convert_from_reference (t);
7163 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7164 if (r != error_mark_node)
7165 maybe_warn_about_useless_cast (type, expr, complain);
7166 return r;
7169 /* Like cp_build_c_cast, but for the c-common bits. */
7171 tree
7172 build_c_cast (location_t /*loc*/, tree type, tree expr)
7174 return cp_build_c_cast (type, expr, tf_warning_or_error);
7177 /* Build an expression representing an explicit C-style cast to type
7178 TYPE of expression EXPR. */
7180 tree
7181 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7183 tree value = expr;
7184 tree result;
7185 bool valid_p;
7187 if (type == error_mark_node || error_operand_p (expr))
7188 return error_mark_node;
7190 if (processing_template_decl)
7192 tree t = build_min (CAST_EXPR, type,
7193 tree_cons (NULL_TREE, value, NULL_TREE));
7194 /* We don't know if it will or will not have side effects. */
7195 TREE_SIDE_EFFECTS (t) = 1;
7196 return convert_from_reference (t);
7199 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7200 'Class') should always be retained, because this information aids
7201 in method lookup. */
7202 if (objc_is_object_ptr (type)
7203 && objc_is_object_ptr (TREE_TYPE (expr)))
7204 return build_nop (type, expr);
7206 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7207 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7208 if (TREE_CODE (type) != REFERENCE_TYPE
7209 && TREE_CODE (value) == NOP_EXPR
7210 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7211 value = TREE_OPERAND (value, 0);
7213 if (TREE_CODE (type) == ARRAY_TYPE)
7215 /* Allow casting from T1* to T2[] because Cfront allows it.
7216 NIHCL uses it. It is not valid ISO C++ however. */
7217 if (TYPE_PTR_P (TREE_TYPE (expr)))
7219 if (complain & tf_error)
7220 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7221 else
7222 return error_mark_node;
7223 type = build_pointer_type (TREE_TYPE (type));
7225 else
7227 if (complain & tf_error)
7228 error ("ISO C++ forbids casting to an array type %qT", type);
7229 return error_mark_node;
7233 if (TREE_CODE (type) == FUNCTION_TYPE
7234 || TREE_CODE (type) == METHOD_TYPE)
7236 if (complain & tf_error)
7237 error ("invalid cast to function type %qT", type);
7238 return error_mark_node;
7241 if (TYPE_PTR_P (type)
7242 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7243 /* Casting to an integer of smaller size is an error detected elsewhere. */
7244 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7245 /* Don't warn about converting any constant. */
7246 && !TREE_CONSTANT (value))
7247 warning_at (input_location, OPT_Wint_to_pointer_cast,
7248 "cast to pointer from integer of different size");
7250 /* A C-style cast can be a const_cast. */
7251 result = build_const_cast_1 (type, value, complain & tf_warning,
7252 &valid_p);
7253 if (valid_p)
7255 if (result != error_mark_node)
7256 maybe_warn_about_useless_cast (type, value, complain);
7257 return result;
7260 /* Or a static cast. */
7261 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7262 &valid_p, complain);
7263 /* Or a reinterpret_cast. */
7264 if (!valid_p)
7265 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7266 &valid_p, complain);
7267 /* The static_cast or reinterpret_cast may be followed by a
7268 const_cast. */
7269 if (valid_p
7270 /* A valid cast may result in errors if, for example, a
7271 conversion to an ambiguous base class is required. */
7272 && !error_operand_p (result))
7274 tree result_type;
7276 maybe_warn_about_useless_cast (type, value, complain);
7278 /* Non-class rvalues always have cv-unqualified type. */
7279 if (!CLASS_TYPE_P (type))
7280 type = TYPE_MAIN_VARIANT (type);
7281 result_type = TREE_TYPE (result);
7282 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7283 result_type = TYPE_MAIN_VARIANT (result_type);
7284 /* If the type of RESULT does not match TYPE, perform a
7285 const_cast to make it match. If the static_cast or
7286 reinterpret_cast succeeded, we will differ by at most
7287 cv-qualification, so the follow-on const_cast is guaranteed
7288 to succeed. */
7289 if (!same_type_p (non_reference (type), non_reference (result_type)))
7291 result = build_const_cast_1 (type, result, false, &valid_p);
7292 gcc_assert (valid_p);
7294 return result;
7297 return error_mark_node;
7300 /* For use from the C common bits. */
7301 tree
7302 build_modify_expr (location_t /*location*/,
7303 tree lhs, tree /*lhs_origtype*/,
7304 enum tree_code modifycode,
7305 location_t /*rhs_location*/, tree rhs,
7306 tree /*rhs_origtype*/)
7308 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7311 /* Build an assignment expression of lvalue LHS from value RHS.
7312 MODIFYCODE is the code for a binary operator that we use
7313 to combine the old value of LHS with RHS to get the new value.
7314 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7316 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7318 tree
7319 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7320 tsubst_flags_t complain)
7322 tree result;
7323 tree newrhs = rhs;
7324 tree lhstype = TREE_TYPE (lhs);
7325 tree olhstype = lhstype;
7326 bool plain_assign = (modifycode == NOP_EXPR);
7328 /* Avoid duplicate error messages from operands that had errors. */
7329 if (error_operand_p (lhs) || error_operand_p (rhs))
7330 return error_mark_node;
7332 /* Handle control structure constructs used as "lvalues". */
7333 switch (TREE_CODE (lhs))
7335 /* Handle --foo = 5; as these are valid constructs in C++. */
7336 case PREDECREMENT_EXPR:
7337 case PREINCREMENT_EXPR:
7338 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7339 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7340 stabilize_reference (TREE_OPERAND (lhs, 0)),
7341 TREE_OPERAND (lhs, 1));
7342 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7343 modifycode, rhs, complain);
7344 if (newrhs == error_mark_node)
7345 return error_mark_node;
7346 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7348 /* Handle (a, b) used as an "lvalue". */
7349 case COMPOUND_EXPR:
7350 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7351 modifycode, rhs, complain);
7352 if (newrhs == error_mark_node)
7353 return error_mark_node;
7354 return build2 (COMPOUND_EXPR, lhstype,
7355 TREE_OPERAND (lhs, 0), newrhs);
7357 case MODIFY_EXPR:
7358 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7359 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7360 stabilize_reference (TREE_OPERAND (lhs, 0)),
7361 TREE_OPERAND (lhs, 1));
7362 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7363 complain);
7364 if (newrhs == error_mark_node)
7365 return error_mark_node;
7366 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7368 case MIN_EXPR:
7369 case MAX_EXPR:
7370 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7371 when neither operand has side-effects. */
7372 if (!lvalue_or_else (lhs, lv_assign, complain))
7373 return error_mark_node;
7375 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7376 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7378 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7379 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7380 boolean_type_node,
7381 TREE_OPERAND (lhs, 0),
7382 TREE_OPERAND (lhs, 1)),
7383 TREE_OPERAND (lhs, 0),
7384 TREE_OPERAND (lhs, 1));
7385 /* Fall through. */
7387 /* Handle (a ? b : c) used as an "lvalue". */
7388 case COND_EXPR:
7390 /* Produce (a ? (b = rhs) : (c = rhs))
7391 except that the RHS goes through a save-expr
7392 so the code to compute it is only emitted once. */
7393 tree cond;
7394 tree preeval = NULL_TREE;
7396 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7398 if (complain & tf_error)
7399 error ("void value not ignored as it ought to be");
7400 return error_mark_node;
7403 rhs = stabilize_expr (rhs, &preeval);
7405 /* Check this here to avoid odd errors when trying to convert
7406 a throw to the type of the COND_EXPR. */
7407 if (!lvalue_or_else (lhs, lv_assign, complain))
7408 return error_mark_node;
7410 cond = build_conditional_expr
7411 (input_location, TREE_OPERAND (lhs, 0),
7412 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7413 modifycode, rhs, complain),
7414 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7415 modifycode, rhs, complain),
7416 complain);
7418 if (cond == error_mark_node)
7419 return cond;
7420 /* Make sure the code to compute the rhs comes out
7421 before the split. */
7422 if (preeval)
7423 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7424 return cond;
7427 default:
7428 break;
7431 if (modifycode == INIT_EXPR)
7433 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7434 /* Do the default thing. */;
7435 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7437 /* Compound literal. */
7438 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7439 /* Call convert to generate an error; see PR 11063. */
7440 rhs = convert (lhstype, rhs);
7441 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7442 TREE_SIDE_EFFECTS (result) = 1;
7443 return result;
7445 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7446 /* Do the default thing. */;
7447 else
7449 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7450 result = build_special_member_call (lhs, complete_ctor_identifier,
7451 &rhs_vec, lhstype, LOOKUP_NORMAL,
7452 complain);
7453 release_tree_vector (rhs_vec);
7454 if (result == NULL_TREE)
7455 return error_mark_node;
7456 return result;
7459 else
7461 lhs = require_complete_type_sfinae (lhs, complain);
7462 if (lhs == error_mark_node)
7463 return error_mark_node;
7465 if (modifycode == NOP_EXPR)
7467 if (c_dialect_objc ())
7469 result = objc_maybe_build_modify_expr (lhs, rhs);
7470 if (result)
7471 return result;
7474 /* `operator=' is not an inheritable operator. */
7475 if (! MAYBE_CLASS_TYPE_P (lhstype))
7476 /* Do the default thing. */;
7477 else
7479 result = build_new_op (input_location, MODIFY_EXPR,
7480 LOOKUP_NORMAL, lhs, rhs,
7481 make_node (NOP_EXPR), /*overload=*/NULL,
7482 complain);
7483 if (result == NULL_TREE)
7484 return error_mark_node;
7485 return result;
7487 lhstype = olhstype;
7489 else
7491 tree init = NULL_TREE;
7493 /* A binary op has been requested. Combine the old LHS
7494 value with the RHS producing the value we should actually
7495 store into the LHS. */
7496 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7497 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7498 || MAYBE_CLASS_TYPE_P (lhstype)));
7500 /* Preevaluate the RHS to make sure its evaluation is complete
7501 before the lvalue-to-rvalue conversion of the LHS:
7503 [expr.ass] With respect to an indeterminately-sequenced
7504 function call, the operation of a compound assignment is a
7505 single evaluation. [ Note: Therefore, a function call shall
7506 not intervene between the lvalue-to-rvalue conversion and the
7507 side effect associated with any single compound assignment
7508 operator. -- end note ] */
7509 lhs = stabilize_reference (lhs);
7510 rhs = rvalue (rhs);
7511 rhs = stabilize_expr (rhs, &init);
7512 newrhs = cp_build_binary_op (input_location,
7513 modifycode, lhs, rhs,
7514 complain);
7515 if (newrhs == error_mark_node)
7517 if (complain & tf_error)
7518 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7519 TREE_TYPE (lhs), TREE_TYPE (rhs));
7520 return error_mark_node;
7523 if (init)
7524 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7526 /* Now it looks like a plain assignment. */
7527 modifycode = NOP_EXPR;
7528 if (c_dialect_objc ())
7530 result = objc_maybe_build_modify_expr (lhs, newrhs);
7531 if (result)
7532 return result;
7535 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7536 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7539 /* The left-hand side must be an lvalue. */
7540 if (!lvalue_or_else (lhs, lv_assign, complain))
7541 return error_mark_node;
7543 /* Warn about modifying something that is `const'. Don't warn if
7544 this is initialization. */
7545 if (modifycode != INIT_EXPR
7546 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7547 /* Functions are not modifiable, even though they are
7548 lvalues. */
7549 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7550 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7551 /* If it's an aggregate and any field is const, then it is
7552 effectively const. */
7553 || (CLASS_TYPE_P (lhstype)
7554 && C_TYPE_FIELDS_READONLY (lhstype))))
7556 if (complain & tf_error)
7557 cxx_readonly_error (lhs, lv_assign);
7558 else
7559 return error_mark_node;
7562 /* If storing into a structure or union member, it may have been given a
7563 lowered bitfield type. We need to convert to the declared type first,
7564 so retrieve it now. */
7566 olhstype = unlowered_expr_type (lhs);
7568 /* Convert new value to destination type. */
7570 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7572 int from_array;
7574 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7576 if (modifycode != INIT_EXPR)
7578 if (complain & tf_error)
7579 error ("assigning to an array from an initializer list");
7580 return error_mark_node;
7582 if (check_array_initializer (lhs, lhstype, newrhs))
7583 return error_mark_node;
7584 newrhs = digest_init (lhstype, newrhs, complain);
7585 if (newrhs == error_mark_node)
7586 return error_mark_node;
7589 /* C++11 8.5/17: "If the destination type is an array of characters,
7590 an array of char16_t, an array of char32_t, or an array of wchar_t,
7591 and the initializer is a string literal...". */
7592 else if (TREE_CODE (newrhs) == STRING_CST
7593 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7594 && modifycode == INIT_EXPR)
7596 newrhs = digest_init (lhstype, newrhs, complain);
7597 if (newrhs == error_mark_node)
7598 return error_mark_node;
7601 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7602 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7604 if (complain & tf_error)
7605 error ("incompatible types in assignment of %qT to %qT",
7606 TREE_TYPE (rhs), lhstype);
7607 return error_mark_node;
7610 /* Allow array assignment in compiler-generated code. */
7611 else if (!current_function_decl
7612 || !DECL_DEFAULTED_FN (current_function_decl))
7614 /* This routine is used for both initialization and assignment.
7615 Make sure the diagnostic message differentiates the context. */
7616 if (complain & tf_error)
7618 if (modifycode == INIT_EXPR)
7619 error ("array used as initializer");
7620 else
7621 error ("invalid array assignment");
7623 return error_mark_node;
7626 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7627 ? 1 + (modifycode != INIT_EXPR): 0;
7628 return build_vec_init (lhs, NULL_TREE, newrhs,
7629 /*explicit_value_init_p=*/false,
7630 from_array, complain);
7633 if (modifycode == INIT_EXPR)
7634 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7635 LOOKUP_ONLYCONVERTING. */
7636 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7637 ICR_INIT, NULL_TREE, 0,
7638 complain);
7639 else
7640 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7641 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7643 if (!same_type_p (lhstype, olhstype))
7644 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7646 if (modifycode != INIT_EXPR)
7648 if (TREE_CODE (newrhs) == CALL_EXPR
7649 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7650 newrhs = build_cplus_new (lhstype, newrhs, complain);
7652 /* Can't initialize directly from a TARGET_EXPR, since that would
7653 cause the lhs to be constructed twice, and possibly result in
7654 accidental self-initialization. So we force the TARGET_EXPR to be
7655 expanded without a target. */
7656 if (TREE_CODE (newrhs) == TARGET_EXPR)
7657 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7658 TREE_OPERAND (newrhs, 0));
7661 if (newrhs == error_mark_node)
7662 return error_mark_node;
7664 if (c_dialect_objc () && flag_objc_gc)
7666 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7668 if (result)
7669 return result;
7672 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7673 lhstype, lhs, newrhs);
7675 TREE_SIDE_EFFECTS (result) = 1;
7676 if (!plain_assign)
7677 TREE_NO_WARNING (result) = 1;
7679 return result;
7682 tree
7683 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7684 tree rhs, tsubst_flags_t complain)
7686 if (processing_template_decl)
7687 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7688 build_min_nt_loc (loc, modifycode, NULL_TREE,
7689 NULL_TREE), rhs);
7691 if (modifycode != NOP_EXPR)
7693 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7694 make_node (modifycode), /*overload=*/NULL,
7695 complain);
7696 if (rval)
7698 TREE_NO_WARNING (rval) = 1;
7699 return rval;
7702 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7705 /* Helper function for get_delta_difference which assumes FROM is a base
7706 class of TO. Returns a delta for the conversion of pointer-to-member
7707 of FROM to pointer-to-member of TO. If the conversion is invalid and
7708 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7709 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7710 If C_CAST_P is true, this conversion is taking place as part of a
7711 C-style cast. */
7713 static tree
7714 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7715 tsubst_flags_t complain)
7717 tree binfo;
7718 base_kind kind;
7720 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7721 &kind, complain);
7723 if (binfo == error_mark_node)
7725 if (!(complain & tf_error))
7726 return error_mark_node;
7728 error (" in pointer to member function conversion");
7729 return size_zero_node;
7731 else if (binfo)
7733 if (kind != bk_via_virtual)
7734 return BINFO_OFFSET (binfo);
7735 else
7736 /* FROM is a virtual base class of TO. Issue an error or warning
7737 depending on whether or not this is a reinterpret cast. */
7739 if (!(complain & tf_error))
7740 return error_mark_node;
7742 error ("pointer to member conversion via virtual base %qT",
7743 BINFO_TYPE (binfo_from_vbase (binfo)));
7745 return size_zero_node;
7748 else
7749 return NULL_TREE;
7752 /* Get difference in deltas for different pointer to member function
7753 types. If the conversion is invalid and tf_error is not set in
7754 COMPLAIN, returns error_mark_node, otherwise returns an integer
7755 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7756 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7757 conversions as well. If C_CAST_P is true this conversion is taking
7758 place as part of a C-style cast.
7760 Note that the naming of FROM and TO is kind of backwards; the return
7761 value is what we add to a TO in order to get a FROM. They are named
7762 this way because we call this function to find out how to convert from
7763 a pointer to member of FROM to a pointer to member of TO. */
7765 static tree
7766 get_delta_difference (tree from, tree to,
7767 bool allow_inverse_p,
7768 bool c_cast_p, tsubst_flags_t complain)
7770 tree result;
7772 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7773 /* Pointer to member of incomplete class is permitted*/
7774 result = size_zero_node;
7775 else
7776 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7778 if (result == error_mark_node)
7779 return error_mark_node;
7781 if (!result)
7783 if (!allow_inverse_p)
7785 if (!(complain & tf_error))
7786 return error_mark_node;
7788 error_not_base_type (from, to);
7789 error (" in pointer to member conversion");
7790 result = size_zero_node;
7792 else
7794 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7796 if (result == error_mark_node)
7797 return error_mark_node;
7799 if (result)
7800 result = size_diffop_loc (input_location,
7801 size_zero_node, result);
7802 else
7804 if (!(complain & tf_error))
7805 return error_mark_node;
7807 error_not_base_type (from, to);
7808 error (" in pointer to member conversion");
7809 result = size_zero_node;
7814 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7815 result));
7818 /* Return a constructor for the pointer-to-member-function TYPE using
7819 the other components as specified. */
7821 tree
7822 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7824 tree u = NULL_TREE;
7825 tree delta_field;
7826 tree pfn_field;
7827 vec<constructor_elt, va_gc> *v;
7829 /* Pull the FIELD_DECLs out of the type. */
7830 pfn_field = TYPE_FIELDS (type);
7831 delta_field = DECL_CHAIN (pfn_field);
7833 /* Make sure DELTA has the type we want. */
7834 delta = convert_and_check (input_location, delta_type_node, delta);
7836 /* Convert to the correct target type if necessary. */
7837 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7839 /* Finish creating the initializer. */
7840 vec_alloc (v, 2);
7841 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7842 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7843 u = build_constructor (type, v);
7844 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7845 TREE_STATIC (u) = (TREE_CONSTANT (u)
7846 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7847 != NULL_TREE)
7848 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7849 != NULL_TREE));
7850 return u;
7853 /* Build a constructor for a pointer to member function. It can be
7854 used to initialize global variables, local variable, or used
7855 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7856 want to be.
7858 If FORCE is nonzero, then force this conversion, even if
7859 we would rather not do it. Usually set when using an explicit
7860 cast. A C-style cast is being processed iff C_CAST_P is true.
7862 Return error_mark_node, if something goes wrong. */
7864 tree
7865 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7866 tsubst_flags_t complain)
7868 tree fn;
7869 tree pfn_type;
7870 tree to_type;
7872 if (error_operand_p (pfn))
7873 return error_mark_node;
7875 pfn_type = TREE_TYPE (pfn);
7876 to_type = build_ptrmemfunc_type (type);
7878 /* Handle multiple conversions of pointer to member functions. */
7879 if (TYPE_PTRMEMFUNC_P (pfn_type))
7881 tree delta = NULL_TREE;
7882 tree npfn = NULL_TREE;
7883 tree n;
7885 if (!force
7886 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7887 LOOKUP_NORMAL, complain))
7889 if (complain & tf_error)
7890 error ("invalid conversion to type %qT from type %qT",
7891 to_type, pfn_type);
7892 else
7893 return error_mark_node;
7896 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7897 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7898 force,
7899 c_cast_p, complain);
7900 if (n == error_mark_node)
7901 return error_mark_node;
7903 /* We don't have to do any conversion to convert a
7904 pointer-to-member to its own type. But, we don't want to
7905 just return a PTRMEM_CST if there's an explicit cast; that
7906 cast should make the expression an invalid template argument. */
7907 if (TREE_CODE (pfn) != PTRMEM_CST)
7909 if (same_type_p (to_type, pfn_type))
7910 return pfn;
7911 else if (integer_zerop (n))
7912 return build_reinterpret_cast (to_type, pfn,
7913 complain);
7916 if (TREE_SIDE_EFFECTS (pfn))
7917 pfn = save_expr (pfn);
7919 /* Obtain the function pointer and the current DELTA. */
7920 if (TREE_CODE (pfn) == PTRMEM_CST)
7921 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7922 else
7924 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7925 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7928 /* Just adjust the DELTA field. */
7929 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7930 (TREE_TYPE (delta), ptrdiff_type_node));
7931 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7932 n = cp_build_binary_op (input_location,
7933 LSHIFT_EXPR, n, integer_one_node,
7934 complain);
7935 delta = cp_build_binary_op (input_location,
7936 PLUS_EXPR, delta, n, complain);
7937 return build_ptrmemfunc1 (to_type, delta, npfn);
7940 /* Handle null pointer to member function conversions. */
7941 if (null_ptr_cst_p (pfn))
7943 pfn = cp_build_c_cast (type, pfn, complain);
7944 return build_ptrmemfunc1 (to_type,
7945 integer_zero_node,
7946 pfn);
7949 if (type_unknown_p (pfn))
7950 return instantiate_type (type, pfn, complain);
7952 fn = TREE_OPERAND (pfn, 0);
7953 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7954 /* In a template, we will have preserved the
7955 OFFSET_REF. */
7956 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7957 return make_ptrmem_cst (to_type, fn);
7960 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7961 given by CST.
7963 ??? There is no consistency as to the types returned for the above
7964 values. Some code acts as if it were a sizetype and some as if it were
7965 integer_type_node. */
7967 void
7968 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7970 tree type = TREE_TYPE (cst);
7971 tree fn = PTRMEM_CST_MEMBER (cst);
7972 tree ptr_class, fn_class;
7974 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7976 /* The class that the function belongs to. */
7977 fn_class = DECL_CONTEXT (fn);
7979 /* The class that we're creating a pointer to member of. */
7980 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7982 /* First, calculate the adjustment to the function's class. */
7983 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7984 /*c_cast_p=*/0, tf_warning_or_error);
7986 if (!DECL_VIRTUAL_P (fn))
7987 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7988 build_addr_func (fn, tf_warning_or_error));
7989 else
7991 /* If we're dealing with a virtual function, we have to adjust 'this'
7992 again, to point to the base which provides the vtable entry for
7993 fn; the call will do the opposite adjustment. */
7994 tree orig_class = DECL_CONTEXT (fn);
7995 tree binfo = binfo_or_else (orig_class, fn_class);
7996 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7997 *delta, BINFO_OFFSET (binfo));
7998 *delta = fold_if_not_in_template (*delta);
8000 /* We set PFN to the vtable offset at which the function can be
8001 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8002 case delta is shifted left, and then incremented). */
8003 *pfn = DECL_VINDEX (fn);
8004 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
8005 TYPE_SIZE_UNIT (vtable_entry_type));
8006 *pfn = fold_if_not_in_template (*pfn);
8008 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8010 case ptrmemfunc_vbit_in_pfn:
8011 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
8012 integer_one_node);
8013 *pfn = fold_if_not_in_template (*pfn);
8014 break;
8016 case ptrmemfunc_vbit_in_delta:
8017 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8018 *delta, integer_one_node);
8019 *delta = fold_if_not_in_template (*delta);
8020 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
8021 *delta, integer_one_node);
8022 *delta = fold_if_not_in_template (*delta);
8023 break;
8025 default:
8026 gcc_unreachable ();
8029 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8030 *pfn = fold_if_not_in_template (*pfn);
8034 /* Return an expression for PFN from the pointer-to-member function
8035 given by T. */
8037 static tree
8038 pfn_from_ptrmemfunc (tree t)
8040 if (TREE_CODE (t) == PTRMEM_CST)
8042 tree delta;
8043 tree pfn;
8045 expand_ptrmemfunc_cst (t, &delta, &pfn);
8046 if (pfn)
8047 return pfn;
8050 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8053 /* Return an expression for DELTA from the pointer-to-member function
8054 given by T. */
8056 static tree
8057 delta_from_ptrmemfunc (tree t)
8059 if (TREE_CODE (t) == PTRMEM_CST)
8061 tree delta;
8062 tree pfn;
8064 expand_ptrmemfunc_cst (t, &delta, &pfn);
8065 if (delta)
8066 return delta;
8069 return build_ptrmemfunc_access_expr (t, delta_identifier);
8072 /* Convert value RHS to type TYPE as preparation for an assignment to
8073 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8074 implicit conversion is. If FNDECL is non-NULL, we are doing the
8075 conversion in order to pass the PARMNUMth argument of FNDECL.
8076 If FNDECL is NULL, we are doing the conversion in function pointer
8077 argument passing, conversion in initialization, etc. */
8079 static tree
8080 convert_for_assignment (tree type, tree rhs,
8081 impl_conv_rhs errtype, tree fndecl, int parmnum,
8082 tsubst_flags_t complain, int flags)
8084 tree rhstype;
8085 enum tree_code coder;
8087 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8088 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8089 rhs = TREE_OPERAND (rhs, 0);
8091 rhstype = TREE_TYPE (rhs);
8092 coder = TREE_CODE (rhstype);
8094 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8095 && vector_types_convertible_p (type, rhstype, true))
8097 rhs = mark_rvalue_use (rhs);
8098 return convert (type, rhs);
8101 if (rhs == error_mark_node || rhstype == error_mark_node)
8102 return error_mark_node;
8103 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8104 return error_mark_node;
8106 /* The RHS of an assignment cannot have void type. */
8107 if (coder == VOID_TYPE)
8109 if (complain & tf_error)
8110 error ("void value not ignored as it ought to be");
8111 return error_mark_node;
8114 if (c_dialect_objc ())
8116 int parmno;
8117 tree selector;
8118 tree rname = fndecl;
8120 switch (errtype)
8122 case ICR_ASSIGN:
8123 parmno = -1;
8124 break;
8125 case ICR_INIT:
8126 parmno = -2;
8127 break;
8128 default:
8129 selector = objc_message_selector ();
8130 parmno = parmnum;
8131 if (selector && parmno > 1)
8133 rname = selector;
8134 parmno -= 1;
8138 if (objc_compare_types (type, rhstype, parmno, rname))
8140 rhs = mark_rvalue_use (rhs);
8141 return convert (type, rhs);
8145 /* [expr.ass]
8147 The expression is implicitly converted (clause _conv_) to the
8148 cv-unqualified type of the left operand.
8150 We allow bad conversions here because by the time we get to this point
8151 we are committed to doing the conversion. If we end up doing a bad
8152 conversion, convert_like will complain. */
8153 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8155 /* When -Wno-pmf-conversions is use, we just silently allow
8156 conversions from pointers-to-members to plain pointers. If
8157 the conversion doesn't work, cp_convert will complain. */
8158 if (!warn_pmf2ptr
8159 && TYPE_PTR_P (type)
8160 && TYPE_PTRMEMFUNC_P (rhstype))
8161 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8162 else
8164 if (complain & tf_error)
8166 /* If the right-hand side has unknown type, then it is an
8167 overloaded function. Call instantiate_type to get error
8168 messages. */
8169 if (rhstype == unknown_type_node)
8170 instantiate_type (type, rhs, tf_warning_or_error);
8171 else if (fndecl)
8172 error ("cannot convert %qT to %qT for argument %qP to %qD",
8173 rhstype, type, parmnum, fndecl);
8174 else
8175 switch (errtype)
8177 case ICR_DEFAULT_ARGUMENT:
8178 error ("cannot convert %qT to %qT in default argument",
8179 rhstype, type);
8180 break;
8181 case ICR_ARGPASS:
8182 error ("cannot convert %qT to %qT in argument passing",
8183 rhstype, type);
8184 break;
8185 case ICR_CONVERTING:
8186 error ("cannot convert %qT to %qT",
8187 rhstype, type);
8188 break;
8189 case ICR_INIT:
8190 error ("cannot convert %qT to %qT in initialization",
8191 rhstype, type);
8192 break;
8193 case ICR_RETURN:
8194 error ("cannot convert %qT to %qT in return",
8195 rhstype, type);
8196 break;
8197 case ICR_ASSIGN:
8198 error ("cannot convert %qT to %qT in assignment",
8199 rhstype, type);
8200 break;
8201 default:
8202 gcc_unreachable();
8204 if (TYPE_PTR_P (rhstype)
8205 && TYPE_PTR_P (type)
8206 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8207 && CLASS_TYPE_P (TREE_TYPE (type))
8208 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8209 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8210 (TREE_TYPE (rhstype))),
8211 "class type %qT is incomplete", TREE_TYPE (rhstype));
8213 return error_mark_node;
8216 if (warn_suggest_attribute_format)
8218 const enum tree_code codel = TREE_CODE (type);
8219 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8220 && coder == codel
8221 && check_missing_format_attribute (type, rhstype)
8222 && (complain & tf_warning))
8223 switch (errtype)
8225 case ICR_ARGPASS:
8226 case ICR_DEFAULT_ARGUMENT:
8227 if (fndecl)
8228 warning (OPT_Wsuggest_attribute_format,
8229 "parameter %qP of %qD might be a candidate "
8230 "for a format attribute", parmnum, fndecl);
8231 else
8232 warning (OPT_Wsuggest_attribute_format,
8233 "parameter might be a candidate "
8234 "for a format attribute");
8235 break;
8236 case ICR_CONVERTING:
8237 warning (OPT_Wsuggest_attribute_format,
8238 "target of conversion might be a candidate "
8239 "for a format attribute");
8240 break;
8241 case ICR_INIT:
8242 warning (OPT_Wsuggest_attribute_format,
8243 "target of initialization might be a candidate "
8244 "for a format attribute");
8245 break;
8246 case ICR_RETURN:
8247 warning (OPT_Wsuggest_attribute_format,
8248 "return type might be a candidate "
8249 "for a format attribute");
8250 break;
8251 case ICR_ASSIGN:
8252 warning (OPT_Wsuggest_attribute_format,
8253 "left-hand side of assignment might be a candidate "
8254 "for a format attribute");
8255 break;
8256 default:
8257 gcc_unreachable();
8261 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8262 does not. */
8263 if (warn_parentheses
8264 && TREE_CODE (type) == BOOLEAN_TYPE
8265 && TREE_CODE (rhs) == MODIFY_EXPR
8266 && !TREE_NO_WARNING (rhs)
8267 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8268 && (complain & tf_warning))
8270 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8272 warning_at (loc, OPT_Wparentheses,
8273 "suggest parentheses around assignment used as truth value");
8274 TREE_NO_WARNING (rhs) = 1;
8277 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8278 complain, flags);
8281 /* Convert RHS to be of type TYPE.
8282 If EXP is nonzero, it is the target of the initialization.
8283 ERRTYPE indicates what kind of error the implicit conversion is.
8285 Two major differences between the behavior of
8286 `convert_for_assignment' and `convert_for_initialization'
8287 are that references are bashed in the former, while
8288 copied in the latter, and aggregates are assigned in
8289 the former (operator=) while initialized in the
8290 latter (X(X&)).
8292 If using constructor make sure no conversion operator exists, if one does
8293 exist, an ambiguity exists. */
8295 tree
8296 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8297 impl_conv_rhs errtype, tree fndecl, int parmnum,
8298 tsubst_flags_t complain)
8300 enum tree_code codel = TREE_CODE (type);
8301 tree rhstype;
8302 enum tree_code coder;
8304 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8305 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8306 if (TREE_CODE (rhs) == NOP_EXPR
8307 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8308 && codel != REFERENCE_TYPE)
8309 rhs = TREE_OPERAND (rhs, 0);
8311 if (type == error_mark_node
8312 || rhs == error_mark_node
8313 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8314 return error_mark_node;
8316 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8317 && TREE_CODE (type) != ARRAY_TYPE
8318 && (TREE_CODE (type) != REFERENCE_TYPE
8319 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8320 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8321 && !TYPE_REFFN_P (type))
8322 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8323 rhs = decay_conversion (rhs, complain);
8325 rhstype = TREE_TYPE (rhs);
8326 coder = TREE_CODE (rhstype);
8328 if (coder == ERROR_MARK)
8329 return error_mark_node;
8331 /* We accept references to incomplete types, so we can
8332 return here before checking if RHS is of complete type. */
8334 if (codel == REFERENCE_TYPE)
8336 /* This should eventually happen in convert_arguments. */
8337 int savew = 0, savee = 0;
8339 if (fndecl)
8340 savew = warningcount + werrorcount, savee = errorcount;
8341 rhs = initialize_reference (type, rhs, flags, complain);
8343 if (fndecl
8344 && (warningcount + werrorcount > savew || errorcount > savee))
8345 inform (input_location,
8346 "in passing argument %P of %q+D", parmnum, fndecl);
8348 return rhs;
8351 if (exp != 0)
8352 exp = require_complete_type_sfinae (exp, complain);
8353 if (exp == error_mark_node)
8354 return error_mark_node;
8356 rhstype = non_reference (rhstype);
8358 type = complete_type (type);
8360 if (DIRECT_INIT_EXPR_P (type, rhs))
8361 /* Don't try to do copy-initialization if we already have
8362 direct-initialization. */
8363 return rhs;
8365 if (MAYBE_CLASS_TYPE_P (type))
8366 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8368 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8369 complain, flags);
8372 /* If RETVAL is the address of, or a reference to, a local variable or
8373 temporary give an appropriate warning and return true. */
8375 static bool
8376 maybe_warn_about_returning_address_of_local (tree retval)
8378 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8379 tree whats_returned = retval;
8381 for (;;)
8383 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8384 whats_returned = TREE_OPERAND (whats_returned, 1);
8385 else if (CONVERT_EXPR_P (whats_returned)
8386 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8387 whats_returned = TREE_OPERAND (whats_returned, 0);
8388 else
8389 break;
8392 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8393 return false;
8394 whats_returned = TREE_OPERAND (whats_returned, 0);
8396 while (TREE_CODE (whats_returned) == COMPONENT_REF
8397 || TREE_CODE (whats_returned) == ARRAY_REF)
8398 whats_returned = TREE_OPERAND (whats_returned, 0);
8400 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8402 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8403 || TREE_CODE (whats_returned) == TARGET_EXPR)
8405 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8406 return true;
8408 if (VAR_P (whats_returned)
8409 && DECL_NAME (whats_returned)
8410 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8412 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8413 return true;
8417 if (DECL_P (whats_returned)
8418 && DECL_NAME (whats_returned)
8419 && DECL_FUNCTION_SCOPE_P (whats_returned)
8420 && !is_capture_proxy (whats_returned)
8421 && !(TREE_STATIC (whats_returned)
8422 || TREE_PUBLIC (whats_returned)))
8424 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8425 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8426 whats_returned);
8427 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8428 warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
8429 whats_returned);
8430 else
8431 warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
8432 "returned", whats_returned);
8433 return true;
8436 return false;
8439 /* Check that returning RETVAL from the current function is valid.
8440 Return an expression explicitly showing all conversions required to
8441 change RETVAL into the function return type, and to assign it to
8442 the DECL_RESULT for the function. Set *NO_WARNING to true if
8443 code reaches end of non-void function warning shouldn't be issued
8444 on this RETURN_EXPR. */
8446 tree
8447 check_return_expr (tree retval, bool *no_warning)
8449 tree result;
8450 /* The type actually returned by the function. */
8451 tree valtype;
8452 /* The type the function is declared to return, or void if
8453 the declared type is incomplete. */
8454 tree functype;
8455 int fn_returns_value_p;
8456 bool named_return_value_okay_p;
8458 *no_warning = false;
8460 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8462 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8463 "statement is not allowed");
8464 return NULL_TREE;
8467 /* A `volatile' function is one that isn't supposed to return, ever.
8468 (This is a G++ extension, used to get better code for functions
8469 that call the `volatile' function.) */
8470 if (TREE_THIS_VOLATILE (current_function_decl))
8471 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8473 /* Check for various simple errors. */
8474 if (DECL_DESTRUCTOR_P (current_function_decl))
8476 if (retval)
8477 error ("returning a value from a destructor");
8478 return NULL_TREE;
8480 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8482 if (in_function_try_handler)
8483 /* If a return statement appears in a handler of the
8484 function-try-block of a constructor, the program is ill-formed. */
8485 error ("cannot return from a handler of a function-try-block of a constructor");
8486 else if (retval)
8487 /* You can't return a value from a constructor. */
8488 error ("returning a value from a constructor");
8489 return NULL_TREE;
8492 if (processing_template_decl)
8494 current_function_returns_value = 1;
8495 if (check_for_bare_parameter_packs (retval))
8496 retval = error_mark_node;
8497 return retval;
8500 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8502 /* Deduce auto return type from a return statement. */
8503 if (current_function_auto_return_pattern)
8505 tree auto_node;
8506 tree type;
8508 if (!retval && !is_auto (current_function_auto_return_pattern))
8510 /* Give a helpful error message. */
8511 error ("return-statement with no value, in function returning %qT",
8512 current_function_auto_return_pattern);
8513 inform (input_location, "only plain %<auto%> return type can be "
8514 "deduced to %<void%>");
8515 type = error_mark_node;
8517 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8519 error ("returning initializer list");
8520 type = error_mark_node;
8522 else
8524 if (!retval)
8525 retval = void_node;
8526 auto_node = type_uses_auto (current_function_auto_return_pattern);
8527 type = do_auto_deduction (current_function_auto_return_pattern,
8528 retval, auto_node);
8531 if (type == error_mark_node)
8532 /* Leave it. */;
8533 else if (functype == current_function_auto_return_pattern)
8534 apply_deduced_return_type (current_function_decl, type);
8535 else
8536 /* A mismatch should have been diagnosed in do_auto_deduction. */
8537 gcc_assert (same_type_p (type, functype));
8538 functype = type;
8541 /* When no explicit return-value is given in a function with a named
8542 return value, the named return value is used. */
8543 result = DECL_RESULT (current_function_decl);
8544 valtype = TREE_TYPE (result);
8545 gcc_assert (valtype != NULL_TREE);
8546 fn_returns_value_p = !VOID_TYPE_P (valtype);
8547 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8548 retval = result;
8550 /* Check for a return statement with no return value in a function
8551 that's supposed to return a value. */
8552 if (!retval && fn_returns_value_p)
8554 if (functype != error_mark_node)
8555 permerror (input_location, "return-statement with no value, in "
8556 "function returning %qT", valtype);
8557 /* Remember that this function did return. */
8558 current_function_returns_value = 1;
8559 /* And signal caller that TREE_NO_WARNING should be set on the
8560 RETURN_EXPR to avoid control reaches end of non-void function
8561 warnings in tree-cfg.c. */
8562 *no_warning = true;
8564 /* Check for a return statement with a value in a function that
8565 isn't supposed to return a value. */
8566 else if (retval && !fn_returns_value_p)
8568 if (VOID_TYPE_P (TREE_TYPE (retval)))
8569 /* You can return a `void' value from a function of `void'
8570 type. In that case, we have to evaluate the expression for
8571 its side-effects. */
8572 finish_expr_stmt (retval);
8573 else
8574 permerror (input_location, "return-statement with a value, in function "
8575 "returning 'void'");
8576 current_function_returns_null = 1;
8578 /* There's really no value to return, after all. */
8579 return NULL_TREE;
8581 else if (!retval)
8582 /* Remember that this function can sometimes return without a
8583 value. */
8584 current_function_returns_null = 1;
8585 else
8586 /* Remember that this function did return a value. */
8587 current_function_returns_value = 1;
8589 /* Check for erroneous operands -- but after giving ourselves a
8590 chance to provide an error about returning a value from a void
8591 function. */
8592 if (error_operand_p (retval))
8594 current_function_return_value = error_mark_node;
8595 return error_mark_node;
8598 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8599 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8600 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8601 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8602 && ! flag_check_new
8603 && retval && null_ptr_cst_p (retval))
8604 warning (0, "%<operator new%> must not return NULL unless it is "
8605 "declared %<throw()%> (or -fcheck-new is in effect)");
8607 /* Effective C++ rule 15. See also start_function. */
8608 if (warn_ecpp
8609 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8611 bool warn = true;
8613 /* The function return type must be a reference to the current
8614 class. */
8615 if (TREE_CODE (valtype) == REFERENCE_TYPE
8616 && same_type_ignoring_top_level_qualifiers_p
8617 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8619 /* Returning '*this' is obviously OK. */
8620 if (retval == current_class_ref)
8621 warn = false;
8622 /* If we are calling a function whose return type is the same of
8623 the current class reference, it is ok. */
8624 else if (INDIRECT_REF_P (retval)
8625 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8626 warn = false;
8629 if (warn)
8630 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8633 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8635 [...] For a function with a class return type, if the expression
8636 in the return statement is the name of a local object, and the cv-
8637 unqualified type of the local object is the same as the function
8638 return type, an implementation is permitted to omit creating the tem-
8639 porary object to hold the function return value [...]
8641 So, if this is a value-returning function that always returns the same
8642 local variable, remember it.
8644 It might be nice to be more flexible, and choose the first suitable
8645 variable even if the function sometimes returns something else, but
8646 then we run the risk of clobbering the variable we chose if the other
8647 returned expression uses the chosen variable somehow. And people expect
8648 this restriction, anyway. (jason 2000-11-19)
8650 See finish_function and finalize_nrv for the rest of this optimization. */
8652 named_return_value_okay_p =
8653 (retval != NULL_TREE
8654 /* Must be a local, automatic variable. */
8655 && VAR_P (retval)
8656 && DECL_CONTEXT (retval) == current_function_decl
8657 && ! TREE_STATIC (retval)
8658 /* And not a lambda or anonymous union proxy. */
8659 && !DECL_HAS_VALUE_EXPR_P (retval)
8660 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8661 /* The cv-unqualified type of the returned value must be the
8662 same as the cv-unqualified return type of the
8663 function. */
8664 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8665 (TYPE_MAIN_VARIANT (functype)))
8666 /* And the returned value must be non-volatile. */
8667 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8669 if (fn_returns_value_p && flag_elide_constructors)
8671 if (named_return_value_okay_p
8672 && (current_function_return_value == NULL_TREE
8673 || current_function_return_value == retval))
8674 current_function_return_value = retval;
8675 else
8676 current_function_return_value = error_mark_node;
8679 /* We don't need to do any conversions when there's nothing being
8680 returned. */
8681 if (!retval)
8682 return NULL_TREE;
8684 /* Do any required conversions. */
8685 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8686 /* No conversions are required. */
8688 else
8690 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8692 /* The functype's return type will have been set to void, if it
8693 was an incomplete type. Just treat this as 'return;' */
8694 if (VOID_TYPE_P (functype))
8695 return error_mark_node;
8697 /* If we had an id-expression obfuscated by force_paren_expr, we need
8698 to undo it so we can try to treat it as an rvalue below. */
8699 if (cxx_dialect >= cxx14
8700 && INDIRECT_REF_P (retval)
8701 && REF_PARENTHESIZED_P (retval))
8703 retval = TREE_OPERAND (retval, 0);
8704 while (TREE_CODE (retval) == NON_LVALUE_EXPR
8705 || TREE_CODE (retval) == NOP_EXPR)
8706 retval = TREE_OPERAND (retval, 0);
8707 gcc_assert (TREE_CODE (retval) == ADDR_EXPR);
8708 retval = TREE_OPERAND (retval, 0);
8711 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8712 treated as an rvalue for the purposes of overload resolution to
8713 favor move constructors over copy constructors.
8715 Note that these conditions are similar to, but not as strict as,
8716 the conditions for the named return value optimization. */
8717 if ((cxx_dialect != cxx98)
8718 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8719 || TREE_CODE (retval) == PARM_DECL)
8720 && DECL_CONTEXT (retval) == current_function_decl
8721 && !TREE_STATIC (retval)
8722 /* This is only interesting for class type. */
8723 && CLASS_TYPE_P (functype))
8724 flags = flags | LOOKUP_PREFER_RVALUE;
8726 /* First convert the value to the function's return type, then
8727 to the type of return value's location to handle the
8728 case that functype is smaller than the valtype. */
8729 retval = convert_for_initialization
8730 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8731 tf_warning_or_error);
8732 retval = convert (valtype, retval);
8734 /* If the conversion failed, treat this just like `return;'. */
8735 if (retval == error_mark_node)
8736 return retval;
8737 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8738 else if (! cfun->returns_struct
8739 && TREE_CODE (retval) == TARGET_EXPR
8740 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8741 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8742 TREE_OPERAND (retval, 0));
8743 else if (maybe_warn_about_returning_address_of_local (retval))
8744 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8745 build_zero_cst (TREE_TYPE (retval)));
8748 /* Actually copy the value returned into the appropriate location. */
8749 if (retval && retval != result)
8750 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8752 return retval;
8756 /* Returns nonzero if the pointer-type FROM can be converted to the
8757 pointer-type TO via a qualification conversion. If CONSTP is -1,
8758 then we return nonzero if the pointers are similar, and the
8759 cv-qualification signature of FROM is a proper subset of that of TO.
8761 If CONSTP is positive, then all outer pointers have been
8762 const-qualified. */
8764 static int
8765 comp_ptr_ttypes_real (tree to, tree from, int constp)
8767 bool to_more_cv_qualified = false;
8768 bool is_opaque_pointer = false;
8770 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8772 if (TREE_CODE (to) != TREE_CODE (from))
8773 return 0;
8775 if (TREE_CODE (from) == OFFSET_TYPE
8776 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8777 TYPE_OFFSET_BASETYPE (to)))
8778 return 0;
8780 /* Const and volatile mean something different for function types,
8781 so the usual checks are not appropriate. */
8782 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8784 if (!at_least_as_qualified_p (to, from))
8785 return 0;
8787 if (!at_least_as_qualified_p (from, to))
8789 if (constp == 0)
8790 return 0;
8791 to_more_cv_qualified = true;
8794 if (constp > 0)
8795 constp &= TYPE_READONLY (to);
8798 if (TREE_CODE (to) == VECTOR_TYPE)
8799 is_opaque_pointer = vector_targets_convertible_p (to, from);
8801 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8802 return ((constp >= 0 || to_more_cv_qualified)
8803 && (is_opaque_pointer
8804 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8808 /* When comparing, say, char ** to char const **, this function takes
8809 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8810 types to this function. */
8813 comp_ptr_ttypes (tree to, tree from)
8815 return comp_ptr_ttypes_real (to, from, 1);
8818 /* Returns true iff FNTYPE is a non-class type that involves
8819 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8820 if a parameter type is ill-formed. */
8822 bool
8823 error_type_p (const_tree type)
8825 tree t;
8827 switch (TREE_CODE (type))
8829 case ERROR_MARK:
8830 return true;
8832 case POINTER_TYPE:
8833 case REFERENCE_TYPE:
8834 case OFFSET_TYPE:
8835 return error_type_p (TREE_TYPE (type));
8837 case FUNCTION_TYPE:
8838 case METHOD_TYPE:
8839 if (error_type_p (TREE_TYPE (type)))
8840 return true;
8841 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8842 if (error_type_p (TREE_VALUE (t)))
8843 return true;
8844 return false;
8846 case RECORD_TYPE:
8847 if (TYPE_PTRMEMFUNC_P (type))
8848 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8849 return false;
8851 default:
8852 return false;
8856 /* Returns true if to and from are (possibly multi-level) pointers to the same
8857 type or inheritance-related types, regardless of cv-quals. */
8859 bool
8860 ptr_reasonably_similar (const_tree to, const_tree from)
8862 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8864 /* Any target type is similar enough to void. */
8865 if (VOID_TYPE_P (to))
8866 return !error_type_p (from);
8867 if (VOID_TYPE_P (from))
8868 return !error_type_p (to);
8870 if (TREE_CODE (to) != TREE_CODE (from))
8871 return false;
8873 if (TREE_CODE (from) == OFFSET_TYPE
8874 && comptypes (TYPE_OFFSET_BASETYPE (to),
8875 TYPE_OFFSET_BASETYPE (from),
8876 COMPARE_BASE | COMPARE_DERIVED))
8877 continue;
8879 if (TREE_CODE (to) == VECTOR_TYPE
8880 && vector_types_convertible_p (to, from, false))
8881 return true;
8883 if (TREE_CODE (to) == INTEGER_TYPE
8884 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8885 return true;
8887 if (TREE_CODE (to) == FUNCTION_TYPE)
8888 return !error_type_p (to) && !error_type_p (from);
8890 if (!TYPE_PTR_P (to))
8892 /* When either type is incomplete avoid DERIVED_FROM_P,
8893 which may call complete_type (c++/57942). */
8894 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8895 return comptypes
8896 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8897 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8902 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8903 pointer-to-member types) are the same, ignoring cv-qualification at
8904 all levels. */
8906 bool
8907 comp_ptr_ttypes_const (tree to, tree from)
8909 bool is_opaque_pointer = false;
8911 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8913 if (TREE_CODE (to) != TREE_CODE (from))
8914 return false;
8916 if (TREE_CODE (from) == OFFSET_TYPE
8917 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8918 TYPE_OFFSET_BASETYPE (to)))
8919 continue;
8921 if (TREE_CODE (to) == VECTOR_TYPE)
8922 is_opaque_pointer = vector_targets_convertible_p (to, from);
8924 if (!TYPE_PTR_P (to))
8925 return (is_opaque_pointer
8926 || same_type_ignoring_top_level_qualifiers_p (to, from));
8930 /* Returns the type qualifiers for this type, including the qualifiers on the
8931 elements for an array type. */
8934 cp_type_quals (const_tree type)
8936 int quals;
8937 /* This CONST_CAST is okay because strip_array_types returns its
8938 argument unmodified and we assign it to a const_tree. */
8939 type = strip_array_types (CONST_CAST_TREE (type));
8940 if (type == error_mark_node
8941 /* Quals on a FUNCTION_TYPE are memfn quals. */
8942 || TREE_CODE (type) == FUNCTION_TYPE)
8943 return TYPE_UNQUALIFIED;
8944 quals = TYPE_QUALS (type);
8945 /* METHOD and REFERENCE_TYPEs should never have quals. */
8946 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8947 && TREE_CODE (type) != REFERENCE_TYPE)
8948 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8949 == TYPE_UNQUALIFIED));
8950 return quals;
8953 /* Returns the function-ref-qualifier for TYPE */
8955 cp_ref_qualifier
8956 type_memfn_rqual (const_tree type)
8958 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8959 || TREE_CODE (type) == METHOD_TYPE);
8961 if (!FUNCTION_REF_QUALIFIED (type))
8962 return REF_QUAL_NONE;
8963 else if (FUNCTION_RVALUE_QUALIFIED (type))
8964 return REF_QUAL_RVALUE;
8965 else
8966 return REF_QUAL_LVALUE;
8969 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8970 METHOD_TYPE. */
8973 type_memfn_quals (const_tree type)
8975 if (TREE_CODE (type) == FUNCTION_TYPE)
8976 return TYPE_QUALS (type);
8977 else if (TREE_CODE (type) == METHOD_TYPE)
8978 return cp_type_quals (class_of_this_parm (type));
8979 else
8980 gcc_unreachable ();
8983 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8984 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8986 tree
8987 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8989 /* Could handle METHOD_TYPE here if necessary. */
8990 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8991 if (TYPE_QUALS (type) == memfn_quals
8992 && type_memfn_rqual (type) == rqual)
8993 return type;
8995 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8996 complex. */
8997 tree result = build_qualified_type (type, memfn_quals);
8998 if (tree canon = TYPE_CANONICAL (result))
8999 if (canon != result)
9000 /* check_qualified_type doesn't check the ref-qualifier, so make sure
9001 TYPE_CANONICAL is correct. */
9002 TYPE_CANONICAL (result)
9003 = build_ref_qualified_type (canon, type_memfn_rqual (result));
9004 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
9005 return build_ref_qualified_type (result, rqual);
9008 /* Returns nonzero if TYPE is const or volatile. */
9010 bool
9011 cv_qualified_p (const_tree type)
9013 int quals = cp_type_quals (type);
9014 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9017 /* Returns nonzero if the TYPE contains a mutable member. */
9019 bool
9020 cp_has_mutable_p (const_tree type)
9022 /* This CONST_CAST is okay because strip_array_types returns its
9023 argument unmodified and we assign it to a const_tree. */
9024 type = strip_array_types (CONST_CAST_TREE(type));
9026 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9029 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9030 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9031 approximation. In particular, consider:
9033 int f();
9034 struct S { int i; };
9035 const S s = { f(); }
9037 Here, we will make "s" as TREE_READONLY (because it is declared
9038 "const") -- only to reverse ourselves upon seeing that the
9039 initializer is non-constant. */
9041 void
9042 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9044 tree type = TREE_TYPE (decl);
9046 if (type == error_mark_node)
9047 return;
9049 if (TREE_CODE (decl) == TYPE_DECL)
9050 return;
9052 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9053 && type_quals != TYPE_UNQUALIFIED));
9055 /* Avoid setting TREE_READONLY incorrectly. */
9056 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9057 constructor can produce constant init, so rely on cp_finish_decl to
9058 clear TREE_READONLY if the variable has non-constant init. */
9060 /* If the type has (or might have) a mutable component, that component
9061 might be modified. */
9062 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9063 type_quals &= ~TYPE_QUAL_CONST;
9065 c_apply_type_quals_to_decl (type_quals, decl);
9068 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9069 exemplar types such that casting T1 to T2 is casting away constness
9070 if and only if there is no implicit conversion from T1 to T2. */
9072 static void
9073 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9075 int quals1;
9076 int quals2;
9078 /* [expr.const.cast]
9080 For multi-level pointer to members and multi-level mixed pointers
9081 and pointers to members (conv.qual), the "member" aspect of a
9082 pointer to member level is ignored when determining if a const
9083 cv-qualifier has been cast away. */
9084 /* [expr.const.cast]
9086 For two pointer types:
9088 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9089 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9090 K is min(N,M)
9092 casting from X1 to X2 casts away constness if, for a non-pointer
9093 type T there does not exist an implicit conversion (clause
9094 _conv_) from:
9096 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9100 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9101 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9102 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9104 *t1 = cp_build_qualified_type (void_type_node,
9105 cp_type_quals (*t1));
9106 *t2 = cp_build_qualified_type (void_type_node,
9107 cp_type_quals (*t2));
9108 return;
9111 quals1 = cp_type_quals (*t1);
9112 quals2 = cp_type_quals (*t2);
9114 if (TYPE_PTRDATAMEM_P (*t1))
9115 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9116 else
9117 *t1 = TREE_TYPE (*t1);
9118 if (TYPE_PTRDATAMEM_P (*t2))
9119 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9120 else
9121 *t2 = TREE_TYPE (*t2);
9123 casts_away_constness_r (t1, t2, complain);
9124 *t1 = build_pointer_type (*t1);
9125 *t2 = build_pointer_type (*t2);
9126 *t1 = cp_build_qualified_type (*t1, quals1);
9127 *t2 = cp_build_qualified_type (*t2, quals2);
9130 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9131 constness.
9133 ??? This function returns non-zero if casting away qualifiers not
9134 just const. We would like to return to the caller exactly which
9135 qualifiers are casted away to give more accurate diagnostics.
9138 static bool
9139 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9141 if (TREE_CODE (t2) == REFERENCE_TYPE)
9143 /* [expr.const.cast]
9145 Casting from an lvalue of type T1 to an lvalue of type T2
9146 using a reference cast casts away constness if a cast from an
9147 rvalue of type "pointer to T1" to the type "pointer to T2"
9148 casts away constness. */
9149 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9150 return casts_away_constness (build_pointer_type (t1),
9151 build_pointer_type (TREE_TYPE (t2)),
9152 complain);
9155 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9156 /* [expr.const.cast]
9158 Casting from an rvalue of type "pointer to data member of X
9159 of type T1" to the type "pointer to data member of Y of type
9160 T2" casts away constness if a cast from an rvalue of type
9161 "pointer to T1" to the type "pointer to T2" casts away
9162 constness. */
9163 return casts_away_constness
9164 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9165 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9166 complain);
9168 /* Casting away constness is only something that makes sense for
9169 pointer or reference types. */
9170 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9171 return false;
9173 /* Top-level qualifiers don't matter. */
9174 t1 = TYPE_MAIN_VARIANT (t1);
9175 t2 = TYPE_MAIN_VARIANT (t2);
9176 casts_away_constness_r (&t1, &t2, complain);
9177 if (!can_convert_standard (t2, t1, complain))
9178 return true;
9180 return false;
9183 /* If T is a REFERENCE_TYPE return the type to which T refers.
9184 Otherwise, return T itself. */
9186 tree
9187 non_reference (tree t)
9189 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9190 t = TREE_TYPE (t);
9191 return t;
9195 /* Return nonzero if REF is an lvalue valid for this language;
9196 otherwise, print an error message and return zero. USE says
9197 how the lvalue is being used and so selects the error message. */
9200 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9202 cp_lvalue_kind kind = lvalue_kind (ref);
9204 if (kind == clk_none)
9206 if (complain & tf_error)
9207 lvalue_error (input_location, use);
9208 return 0;
9210 else if (kind & (clk_rvalueref|clk_class))
9212 if (!(complain & tf_error))
9213 return 0;
9214 if (kind & clk_class)
9215 /* Make this a permerror because we used to accept it. */
9216 permerror (input_location, "using temporary as lvalue");
9217 else
9218 error ("using xvalue (rvalue reference) as lvalue");
9220 return 1;
9223 /* Return true if a user-defined literal operator is a raw operator. */
9225 bool
9226 check_raw_literal_operator (const_tree decl)
9228 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9229 tree argtype;
9230 int arity;
9231 bool maybe_raw_p = false;
9233 /* Count the number and type of arguments and check for ellipsis. */
9234 for (argtype = argtypes, arity = 0;
9235 argtype && argtype != void_list_node;
9236 ++arity, argtype = TREE_CHAIN (argtype))
9238 tree t = TREE_VALUE (argtype);
9240 if (same_type_p (t, const_string_type_node))
9241 maybe_raw_p = true;
9243 if (!argtype)
9244 return false; /* Found ellipsis. */
9246 if (!maybe_raw_p || arity != 1)
9247 return false;
9249 return true;
9253 /* Return true if a user-defined literal operator has one of the allowed
9254 argument types. */
9256 bool
9257 check_literal_operator_args (const_tree decl,
9258 bool *long_long_unsigned_p, bool *long_double_p)
9260 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9262 *long_long_unsigned_p = false;
9263 *long_double_p = false;
9264 if (processing_template_decl || processing_specialization)
9265 return argtypes == void_list_node;
9266 else
9268 tree argtype;
9269 int arity;
9270 int max_arity = 2;
9272 /* Count the number and type of arguments and check for ellipsis. */
9273 for (argtype = argtypes, arity = 0;
9274 argtype && argtype != void_list_node;
9275 argtype = TREE_CHAIN (argtype))
9277 tree t = TREE_VALUE (argtype);
9278 ++arity;
9280 if (TYPE_PTR_P (t))
9282 bool maybe_raw_p = false;
9283 t = TREE_TYPE (t);
9284 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9285 return false;
9286 t = TYPE_MAIN_VARIANT (t);
9287 if ((maybe_raw_p = same_type_p (t, char_type_node))
9288 || same_type_p (t, wchar_type_node)
9289 || same_type_p (t, char16_type_node)
9290 || same_type_p (t, char32_type_node))
9292 argtype = TREE_CHAIN (argtype);
9293 if (!argtype)
9294 return false;
9295 t = TREE_VALUE (argtype);
9296 if (maybe_raw_p && argtype == void_list_node)
9297 return true;
9298 else if (same_type_p (t, size_type_node))
9300 ++arity;
9301 continue;
9303 else
9304 return false;
9307 else if (same_type_p (t, long_long_unsigned_type_node))
9309 max_arity = 1;
9310 *long_long_unsigned_p = true;
9312 else if (same_type_p (t, long_double_type_node))
9314 max_arity = 1;
9315 *long_double_p = true;
9317 else if (same_type_p (t, char_type_node))
9318 max_arity = 1;
9319 else if (same_type_p (t, wchar_type_node))
9320 max_arity = 1;
9321 else if (same_type_p (t, char16_type_node))
9322 max_arity = 1;
9323 else if (same_type_p (t, char32_type_node))
9324 max_arity = 1;
9325 else
9326 return false;
9328 if (!argtype)
9329 return false; /* Found ellipsis. */
9331 if (arity != max_arity)
9332 return false;
9334 return true;