2012-08-01 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / cp / typeck.c
blobd7a719fcf44be5a874326324f4d056b8afd81d1e
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011, 2012
5 Free Software Foundation, Inc.
6 Hacked by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* This file is part of the C++ front end.
26 It contains routines to build C++ expressions given their operands,
27 including computing the types of the result, C and C++ specific error
28 checks, and some optimization. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "tree.h"
35 #include "cp-tree.h"
36 #include "flags.h"
37 #include "diagnostic.h"
38 #include "intl.h"
39 #include "target.h"
40 #include "convert.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
43 #include "params.h"
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
48 tsubst_flags_t, int);
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
50 static tree rationalize_conditional_expr (enum tree_code, tree,
51 tsubst_flags_t);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
58 static bool casts_away_constness (tree, tree, tsubst_flags_t);
59 static void maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, VEC(tree,gc) **, tree, int,
63 tsubst_flags_t);
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
70 tree
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
73 tree type;
75 if (processing_template_decl || value == error_mark_node)
76 return value;
78 if (TREE_CODE (value) == OVERLOAD)
79 type = unknown_type_node;
80 else
81 type = TREE_TYPE (value);
83 if (type == error_mark_node)
84 return error_mark_node;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type))
88 return value;
90 if (complete_type_or_maybe_complain (type, value, complain))
91 return value;
92 else
93 return error_mark_node;
96 tree
97 require_complete_type (tree value)
99 return require_complete_type_sfinae (value, tf_warning_or_error);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
107 tree
108 complete_type (tree type)
110 if (type == NULL_TREE)
111 /* Rather than crash, we return something sure to cause an error
112 at some point. */
113 return error_mark_node;
115 if (type == error_mark_node || COMPLETE_TYPE_P (type))
117 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
119 tree t = complete_type (TREE_TYPE (type));
120 unsigned int needs_constructing, has_nontrivial_dtor;
121 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
122 layout_type (type);
123 needs_constructing
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
125 has_nontrivial_dtor
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
129 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
136 return type;
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
143 tree
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
146 type = complete_type (type);
147 if (type == error_mark_node)
148 /* We already issued an error. */
149 return NULL_TREE;
150 else if (!COMPLETE_TYPE_P (type))
152 if (complain & tf_error)
153 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
154 return NULL_TREE;
156 else
157 return type;
160 tree
161 complete_type_or_else (tree type, tree value)
163 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
166 /* Return truthvalue of whether type of EXP is instantiated. */
169 type_unknown_p (const_tree exp)
171 return (TREE_CODE (exp) == TREE_LIST
172 || TREE_TYPE (exp) == unknown_type_node);
176 /* Return the common type of two parameter lists.
177 We assume that comptypes has already been done and returned 1;
178 if that isn't so, this may crash.
180 As an optimization, free the space we allocate if the parameter
181 lists are already common. */
183 static tree
184 commonparms (tree p1, tree p2)
186 tree oldargs = p1, newargs, n;
187 int i, len;
188 int any_change = 0;
190 len = list_length (p1);
191 newargs = tree_last (p1);
193 if (newargs == void_list_node)
194 i = 1;
195 else
197 i = 0;
198 newargs = 0;
201 for (; i < len; i++)
202 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
204 n = newargs;
206 for (i = 0; p1;
207 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
209 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
212 any_change = 1;
214 else if (! TREE_PURPOSE (p1))
216 if (TREE_PURPOSE (p2))
218 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219 any_change = 1;
222 else
224 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
225 any_change = 1;
226 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
228 if (TREE_VALUE (p1) != TREE_VALUE (p2))
230 any_change = 1;
231 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
233 else
234 TREE_VALUE (n) = TREE_VALUE (p1);
236 if (! any_change)
237 return oldargs;
239 return newargs;
242 /* Given a type, perhaps copied for a typedef,
243 find the "original" version of it. */
244 static tree
245 original_type (tree t)
247 int quals = cp_type_quals (t);
248 while (t != error_mark_node
249 && TYPE_NAME (t) != NULL_TREE)
251 tree x = TYPE_NAME (t);
252 if (TREE_CODE (x) != TYPE_DECL)
253 break;
254 x = DECL_ORIGINAL_TYPE (x);
255 if (x == NULL_TREE)
256 break;
257 t = x;
259 return cp_build_qualified_type (t, quals);
262 /* Return the common type for two arithmetic types T1 and T2 under the
263 usual arithmetic conversions. The default conversions have already
264 been applied, and enumerated types converted to their compatible
265 integer types. */
267 static tree
268 cp_common_type (tree t1, tree t2)
270 enum tree_code code1 = TREE_CODE (t1);
271 enum tree_code code2 = TREE_CODE (t2);
272 tree attributes;
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
277 attributes. */
278 attributes = (*targetm.merge_type_attributes) (t1, t2);
280 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
282 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283 return build_type_attribute_variant (t1, attributes);
284 else
285 return NULL_TREE;
288 /* FIXME: Attributes. */
289 gcc_assert (ARITHMETIC_TYPE_P (t1)
290 || TREE_CODE (t1) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t1));
292 gcc_assert (ARITHMETIC_TYPE_P (t2)
293 || TREE_CODE (t2) == VECTOR_TYPE
294 || UNSCOPED_ENUM_P (t2));
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
298 required type. */
299 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
301 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
303 tree subtype
304 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
306 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307 return build_type_attribute_variant (t1, attributes);
308 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309 return build_type_attribute_variant (t2, attributes);
310 else
311 return build_type_attribute_variant (build_complex_type (subtype),
312 attributes);
315 if (code1 == VECTOR_TYPE)
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1))
320 return build_type_attribute_variant (t1, attributes);
321 else
322 return build_type_attribute_variant (t2, attributes);
325 /* If only one is real, use it as the result. */
326 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327 return build_type_attribute_variant (t1, attributes);
328 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329 return build_type_attribute_variant (t2, attributes);
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333 return build_type_attribute_variant (t1, attributes);
334 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335 return build_type_attribute_variant (t2, attributes);
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339 return build_type_attribute_variant (t1, attributes);
341 if (code1 != REAL_TYPE)
343 /* If one is unsigned long long, then convert the other to unsigned
344 long long. */
345 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347 return build_type_attribute_variant (long_long_unsigned_type_node,
348 attributes);
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
353 other to long long.
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
358 case. */
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
362 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363 ? long_long_unsigned_type_node
364 : long_long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
367 if (int128_integer_type_node != NULL_TREE
368 && (same_type_p (TYPE_MAIN_VARIANT (t1),
369 int128_integer_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2),
371 int128_integer_type_node)))
373 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
374 ? int128_unsigned_type_node
375 : int128_integer_type_node);
376 return build_type_attribute_variant (t, attributes);
379 /* Go through the same procedure, but for longs. */
380 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
381 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
382 return build_type_attribute_variant (long_unsigned_type_node,
383 attributes);
384 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? long_unsigned_type_node : long_integer_type_node);
389 return build_type_attribute_variant (t, attributes);
391 /* Otherwise prefer the unsigned one. */
392 if (TYPE_UNSIGNED (t1))
393 return build_type_attribute_variant (t1, attributes);
394 else
395 return build_type_attribute_variant (t2, attributes);
397 else
399 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
400 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
401 return build_type_attribute_variant (long_double_type_node,
402 attributes);
403 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
405 return build_type_attribute_variant (double_type_node,
406 attributes);
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
409 return build_type_attribute_variant (float_type_node,
410 attributes);
412 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
413 the standard C++ floating-point types. Logic earlier in this
414 function has already eliminated the possibility that
415 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
416 compelling reason to choose one or the other. */
417 return build_type_attribute_variant (t1, attributes);
421 /* T1 and T2 are arithmetic or enumeration types. Return the type
422 that will result from the "usual arithmetic conversions" on T1 and
423 T2 as described in [expr]. */
425 tree
426 type_after_usual_arithmetic_conversions (tree t1, tree t2)
428 gcc_assert (ARITHMETIC_TYPE_P (t1)
429 || TREE_CODE (t1) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t1));
431 gcc_assert (ARITHMETIC_TYPE_P (t2)
432 || TREE_CODE (t2) == VECTOR_TYPE
433 || UNSCOPED_ENUM_P (t2));
435 /* Perform the integral promotions. We do not promote real types here. */
436 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
437 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
439 t1 = type_promotes_to (t1);
440 t2 = type_promotes_to (t2);
443 return cp_common_type (t1, t2);
446 static void
447 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
448 composite_pointer_operation operation)
450 switch (operation)
452 case CPO_COMPARISON:
453 emit_diagnostic (kind, input_location, 0,
454 "comparison between "
455 "distinct pointer types %qT and %qT lacks a cast",
456 t1, t2);
457 break;
458 case CPO_CONVERSION:
459 emit_diagnostic (kind, input_location, 0,
460 "conversion between "
461 "distinct pointer types %qT and %qT lacks a cast",
462 t1, t2);
463 break;
464 case CPO_CONDITIONAL_EXPR:
465 emit_diagnostic (kind, input_location, 0,
466 "conditional expression between "
467 "distinct pointer types %qT and %qT lacks a cast",
468 t1, t2);
469 break;
470 default:
471 gcc_unreachable ();
475 /* Subroutine of composite_pointer_type to implement the recursive
476 case. See that function for documentation of the parameters. */
478 static tree
479 composite_pointer_type_r (tree t1, tree t2,
480 composite_pointer_operation operation,
481 tsubst_flags_t complain)
483 tree pointee1;
484 tree pointee2;
485 tree result_type;
486 tree attributes;
488 /* Determine the types pointed to by T1 and T2. */
489 if (TREE_CODE (t1) == POINTER_TYPE)
491 pointee1 = TREE_TYPE (t1);
492 pointee2 = TREE_TYPE (t2);
494 else
496 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
497 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
500 /* [expr.rel]
502 Otherwise, the composite pointer type is a pointer type
503 similar (_conv.qual_) to the type of one of the operands,
504 with a cv-qualification signature (_conv.qual_) that is the
505 union of the cv-qualification signatures of the operand
506 types. */
507 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
508 result_type = pointee1;
509 else if ((TREE_CODE (pointee1) == POINTER_TYPE
510 && TREE_CODE (pointee2) == POINTER_TYPE)
511 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
513 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
514 complain);
515 if (result_type == error_mark_node)
516 return error_mark_node;
518 else
520 if (complain & tf_error)
521 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
522 else
523 return error_mark_node;
524 result_type = void_type_node;
526 result_type = cp_build_qualified_type (result_type,
527 (cp_type_quals (pointee1)
528 | cp_type_quals (pointee2)));
529 /* If the original types were pointers to members, so is the
530 result. */
531 if (TYPE_PTRMEM_P (t1))
533 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
534 TYPE_PTRMEM_CLASS_TYPE (t2)))
536 if (complain & tf_error)
537 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
538 else
539 return error_mark_node;
541 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
542 result_type);
544 else
545 result_type = build_pointer_type (result_type);
547 /* Merge the attributes. */
548 attributes = (*targetm.merge_type_attributes) (t1, t2);
549 return build_type_attribute_variant (result_type, attributes);
552 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
553 ARG1 and ARG2 are the values with those types. The OPERATION is to
554 describe the operation between the pointer types,
555 in case an error occurs.
557 This routine also implements the computation of a common type for
558 pointers-to-members as per [expr.eq]. */
560 tree
561 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
562 composite_pointer_operation operation,
563 tsubst_flags_t complain)
565 tree class1;
566 tree class2;
568 /* [expr.rel]
570 If one operand is a null pointer constant, the composite pointer
571 type is the type of the other operand. */
572 if (null_ptr_cst_p (arg1))
573 return t2;
574 if (null_ptr_cst_p (arg2))
575 return t1;
577 /* We have:
579 [expr.rel]
581 If one of the operands has type "pointer to cv1 void*", then
582 the other has type "pointer to cv2T", and the composite pointer
583 type is "pointer to cv12 void", where cv12 is the union of cv1
584 and cv2.
586 If either type is a pointer to void, make sure it is T1. */
587 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
589 tree t;
590 t = t1;
591 t1 = t2;
592 t2 = t;
595 /* Now, if T1 is a pointer to void, merge the qualifiers. */
596 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
598 tree attributes;
599 tree result_type;
601 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
603 switch (operation)
605 case CPO_COMPARISON:
606 pedwarn (input_location, OPT_Wpedantic,
607 "ISO C++ forbids comparison between "
608 "pointer of type %<void *%> and pointer-to-function");
609 break;
610 case CPO_CONVERSION:
611 pedwarn (input_location, OPT_Wpedantic,
612 "ISO C++ forbids conversion between "
613 "pointer of type %<void *%> and pointer-to-function");
614 break;
615 case CPO_CONDITIONAL_EXPR:
616 pedwarn (input_location, OPT_Wpedantic,
617 "ISO C++ forbids conditional expression between "
618 "pointer of type %<void *%> and pointer-to-function");
619 break;
620 default:
621 gcc_unreachable ();
624 result_type
625 = cp_build_qualified_type (void_type_node,
626 (cp_type_quals (TREE_TYPE (t1))
627 | cp_type_quals (TREE_TYPE (t2))));
628 result_type = build_pointer_type (result_type);
629 /* Merge the attributes. */
630 attributes = (*targetm.merge_type_attributes) (t1, t2);
631 return build_type_attribute_variant (result_type, attributes);
634 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
635 && TREE_CODE (t2) == POINTER_TYPE)
637 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
638 return objc_common_type (t1, t2);
641 /* [expr.eq] permits the application of a pointer conversion to
642 bring the pointers to a common type. */
643 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
644 && CLASS_TYPE_P (TREE_TYPE (t1))
645 && CLASS_TYPE_P (TREE_TYPE (t2))
646 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
647 TREE_TYPE (t2)))
649 class1 = TREE_TYPE (t1);
650 class2 = TREE_TYPE (t2);
652 if (DERIVED_FROM_P (class1, class2))
653 t2 = (build_pointer_type
654 (cp_build_qualified_type (class1, cp_type_quals (class2))));
655 else if (DERIVED_FROM_P (class2, class1))
656 t1 = (build_pointer_type
657 (cp_build_qualified_type (class2, cp_type_quals (class1))));
658 else
660 if (complain & tf_error)
661 composite_pointer_error (DK_ERROR, t1, t2, operation);
662 return error_mark_node;
665 /* [expr.eq] permits the application of a pointer-to-member
666 conversion to change the class type of one of the types. */
667 else if (TYPE_PTRMEM_P (t1)
668 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
669 TYPE_PTRMEM_CLASS_TYPE (t2)))
671 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
672 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
674 if (DERIVED_FROM_P (class1, class2))
675 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
676 else if (DERIVED_FROM_P (class2, class1))
677 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
678 else
680 if (complain & tf_error)
681 switch (operation)
683 case CPO_COMPARISON:
684 error ("comparison between distinct "
685 "pointer-to-member types %qT and %qT lacks a cast",
686 t1, t2);
687 break;
688 case CPO_CONVERSION:
689 error ("conversion between distinct "
690 "pointer-to-member types %qT and %qT lacks a cast",
691 t1, t2);
692 break;
693 case CPO_CONDITIONAL_EXPR:
694 error ("conditional expression between distinct "
695 "pointer-to-member types %qT and %qT lacks a cast",
696 t1, t2);
697 break;
698 default:
699 gcc_unreachable ();
701 return error_mark_node;
705 return composite_pointer_type_r (t1, t2, operation, complain);
708 /* Return the merged type of two types.
709 We assume that comptypes has already been done and returned 1;
710 if that isn't so, this may crash.
712 This just combines attributes and default arguments; any other
713 differences would cause the two types to compare unalike. */
715 tree
716 merge_types (tree t1, tree t2)
718 enum tree_code code1;
719 enum tree_code code2;
720 tree attributes;
722 /* Save time if the two types are the same. */
723 if (t1 == t2)
724 return t1;
725 if (original_type (t1) == original_type (t2))
726 return t1;
728 /* If one type is nonsense, use the other. */
729 if (t1 == error_mark_node)
730 return t2;
731 if (t2 == error_mark_node)
732 return t1;
734 /* Handle merging an auto redeclaration with a previous deduced
735 return type. */
736 if (is_auto (t1))
737 return t2;
739 /* Merge the attributes. */
740 attributes = (*targetm.merge_type_attributes) (t1, t2);
742 if (TYPE_PTRMEMFUNC_P (t1))
743 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
744 if (TYPE_PTRMEMFUNC_P (t2))
745 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
747 code1 = TREE_CODE (t1);
748 code2 = TREE_CODE (t2);
749 if (code1 != code2)
751 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
752 if (code1 == TYPENAME_TYPE)
754 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
755 code1 = TREE_CODE (t1);
757 else
759 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
760 code2 = TREE_CODE (t2);
764 switch (code1)
766 case POINTER_TYPE:
767 case REFERENCE_TYPE:
768 /* For two pointers, do this recursively on the target type. */
770 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
771 int quals = cp_type_quals (t1);
773 if (code1 == POINTER_TYPE)
774 t1 = build_pointer_type (target);
775 else
776 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
777 t1 = build_type_attribute_variant (t1, attributes);
778 t1 = cp_build_qualified_type (t1, quals);
780 if (TREE_CODE (target) == METHOD_TYPE)
781 t1 = build_ptrmemfunc_type (t1);
783 return t1;
786 case OFFSET_TYPE:
788 int quals;
789 tree pointee;
790 quals = cp_type_quals (t1);
791 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
792 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
793 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
794 pointee);
795 t1 = cp_build_qualified_type (t1, quals);
796 break;
799 case ARRAY_TYPE:
801 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
802 /* Save space: see if the result is identical to one of the args. */
803 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
804 return build_type_attribute_variant (t1, attributes);
805 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
806 return build_type_attribute_variant (t2, attributes);
807 /* Merge the element types, and have a size if either arg has one. */
808 t1 = build_cplus_array_type
809 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
810 break;
813 case FUNCTION_TYPE:
814 /* Function types: prefer the one that specified arg types.
815 If both do, merge the arg types. Also merge the return types. */
817 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
818 tree p1 = TYPE_ARG_TYPES (t1);
819 tree p2 = TYPE_ARG_TYPES (t2);
820 tree parms;
821 tree rval, raises;
823 /* Save space: see if the result is identical to one of the args. */
824 if (valtype == TREE_TYPE (t1) && ! p2)
825 return cp_build_type_attribute_variant (t1, attributes);
826 if (valtype == TREE_TYPE (t2) && ! p1)
827 return cp_build_type_attribute_variant (t2, attributes);
829 /* Simple way if one arg fails to specify argument types. */
830 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
831 parms = p2;
832 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
833 parms = p1;
834 else
835 parms = commonparms (p1, p2);
837 rval = build_function_type (valtype, parms);
838 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
839 rval = apply_memfn_quals (rval, type_memfn_quals (t1));
840 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
841 TYPE_RAISES_EXCEPTIONS (t2),
842 NULL_TREE);
843 t1 = build_exception_variant (rval, raises);
844 break;
847 case METHOD_TYPE:
849 /* Get this value the long way, since TYPE_METHOD_BASETYPE
850 is just the main variant of this. */
851 tree basetype = class_of_this_parm (t2);
852 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
853 TYPE_RAISES_EXCEPTIONS (t2),
854 NULL_TREE);
855 tree t3;
857 /* If this was a member function type, get back to the
858 original type of type member function (i.e., without
859 the class instance variable up front. */
860 t1 = build_function_type (TREE_TYPE (t1),
861 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
862 t2 = build_function_type (TREE_TYPE (t2),
863 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
864 t3 = merge_types (t1, t2);
865 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
866 TYPE_ARG_TYPES (t3));
867 t1 = build_exception_variant (t3, raises);
868 break;
871 case TYPENAME_TYPE:
872 /* There is no need to merge attributes into a TYPENAME_TYPE.
873 When the type is instantiated it will have whatever
874 attributes result from the instantiation. */
875 return t1;
877 default:;
880 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
881 return t1;
882 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
883 return t2;
884 else
885 return cp_build_type_attribute_variant (t1, attributes);
888 /* Return the ARRAY_TYPE type without its domain. */
890 tree
891 strip_array_domain (tree type)
893 tree t2;
894 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
895 if (TYPE_DOMAIN (type) == NULL_TREE)
896 return type;
897 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
898 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
901 /* Wrapper around cp_common_type that is used by c-common.c and other
902 front end optimizations that remove promotions.
904 Return the common type for two arithmetic types T1 and T2 under the
905 usual arithmetic conversions. The default conversions have already
906 been applied, and enumerated types converted to their compatible
907 integer types. */
909 tree
910 common_type (tree t1, tree t2)
912 /* If one type is nonsense, use the other */
913 if (t1 == error_mark_node)
914 return t2;
915 if (t2 == error_mark_node)
916 return t1;
918 return cp_common_type (t1, t2);
921 /* Return the common type of two pointer types T1 and T2. This is the
922 type for the result of most arithmetic operations if the operands
923 have the given two types.
925 We assume that comp_target_types has already been done and returned
926 nonzero; if that isn't so, this may crash. */
928 tree
929 common_pointer_type (tree t1, tree t2)
931 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
932 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
933 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
935 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
936 CPO_CONVERSION, tf_warning_or_error);
939 /* Compare two exception specifier types for exactness or subsetness, if
940 allowed. Returns false for mismatch, true for match (same, or
941 derived and !exact).
943 [except.spec] "If a class X ... objects of class X or any class publicly
944 and unambiguously derived from X. Similarly, if a pointer type Y * ...
945 exceptions of type Y * or that are pointers to any type publicly and
946 unambiguously derived from Y. Otherwise a function only allows exceptions
947 that have the same type ..."
948 This does not mention cv qualifiers and is different to what throw
949 [except.throw] and catch [except.catch] will do. They will ignore the
950 top level cv qualifiers, and allow qualifiers in the pointer to class
951 example.
953 We implement the letter of the standard. */
955 static bool
956 comp_except_types (tree a, tree b, bool exact)
958 if (same_type_p (a, b))
959 return true;
960 else if (!exact)
962 if (cp_type_quals (a) || cp_type_quals (b))
963 return false;
965 if (TREE_CODE (a) == POINTER_TYPE
966 && TREE_CODE (b) == POINTER_TYPE)
968 a = TREE_TYPE (a);
969 b = TREE_TYPE (b);
970 if (cp_type_quals (a) || cp_type_quals (b))
971 return false;
974 if (TREE_CODE (a) != RECORD_TYPE
975 || TREE_CODE (b) != RECORD_TYPE)
976 return false;
978 if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
979 return true;
981 return false;
984 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
985 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
986 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
987 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
988 are unordered, but we've already filtered out duplicates. Most lists will
989 be in order, we should try to make use of that. */
991 bool
992 comp_except_specs (const_tree t1, const_tree t2, int exact)
994 const_tree probe;
995 const_tree base;
996 int length = 0;
998 if (t1 == t2)
999 return true;
1001 /* First handle noexcept. */
1002 if (exact < ce_exact)
1004 /* noexcept(false) is compatible with no exception-specification,
1005 and stricter than any spec. */
1006 if (t1 == noexcept_false_spec)
1007 return t2 == NULL_TREE || exact == ce_derived;
1008 /* Even a derived noexcept(false) is compatible with no
1009 exception-specification. */
1010 if (t2 == noexcept_false_spec)
1011 return t1 == NULL_TREE;
1013 /* Otherwise, if we aren't looking for an exact match, noexcept is
1014 equivalent to throw(). */
1015 if (t1 == noexcept_true_spec)
1016 t1 = empty_except_spec;
1017 if (t2 == noexcept_true_spec)
1018 t2 = empty_except_spec;
1021 /* If any noexcept is left, it is only comparable to itself;
1022 either we're looking for an exact match or we're redeclaring a
1023 template with dependent noexcept. */
1024 if ((t1 && TREE_PURPOSE (t1))
1025 || (t2 && TREE_PURPOSE (t2)))
1026 return (t1 && t2
1027 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1029 if (t1 == NULL_TREE) /* T1 is ... */
1030 return t2 == NULL_TREE || exact == ce_derived;
1031 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1032 return t2 != NULL_TREE && !TREE_VALUE (t2);
1033 if (t2 == NULL_TREE) /* T2 is ... */
1034 return false;
1035 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1036 return exact == ce_derived;
1038 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1039 Count how many we find, to determine exactness. For exact matching and
1040 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1041 O(nm). */
1042 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1044 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1046 tree a = TREE_VALUE (probe);
1047 tree b = TREE_VALUE (t2);
1049 if (comp_except_types (a, b, exact))
1051 if (probe == base && exact > ce_derived)
1052 base = TREE_CHAIN (probe);
1053 length++;
1054 break;
1057 if (probe == NULL_TREE)
1058 return false;
1060 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1063 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1064 [] can match [size]. */
1066 static bool
1067 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1069 tree d1;
1070 tree d2;
1071 tree max1, max2;
1073 if (t1 == t2)
1074 return true;
1076 /* The type of the array elements must be the same. */
1077 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1078 return false;
1080 d1 = TYPE_DOMAIN (t1);
1081 d2 = TYPE_DOMAIN (t2);
1083 if (d1 == d2)
1084 return true;
1086 /* If one of the arrays is dimensionless, and the other has a
1087 dimension, they are of different types. However, it is valid to
1088 write:
1090 extern int a[];
1091 int a[3];
1093 by [basic.link]:
1095 declarations for an array object can specify
1096 array types that differ by the presence or absence of a major
1097 array bound (_dcl.array_). */
1098 if (!d1 || !d2)
1099 return allow_redeclaration;
1101 /* Check that the dimensions are the same. */
1103 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1104 return false;
1105 max1 = TYPE_MAX_VALUE (d1);
1106 max2 = TYPE_MAX_VALUE (d2);
1107 if (processing_template_decl && !abi_version_at_least (2)
1108 && !value_dependent_expression_p (max1)
1109 && !value_dependent_expression_p (max2))
1111 /* With abi-1 we do not fold non-dependent array bounds, (and
1112 consequently mangle them incorrectly). We must therefore
1113 fold them here, to verify the domains have the same
1114 value. */
1115 max1 = fold (max1);
1116 max2 = fold (max2);
1119 if (!cp_tree_equal (max1, max2))
1120 return false;
1122 return true;
1125 /* Compare the relative position of T1 and T2 into their respective
1126 template parameter list.
1127 T1 and T2 must be template parameter types.
1128 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1130 static bool
1131 comp_template_parms_position (tree t1, tree t2)
1133 tree index1, index2;
1134 gcc_assert (t1 && t2
1135 && TREE_CODE (t1) == TREE_CODE (t2)
1136 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1137 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1138 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1140 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1141 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1143 /* Then compare their relative position. */
1144 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1145 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1146 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1147 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1148 return false;
1150 return true;
1153 /* Subroutine in comptypes. */
1155 static bool
1156 structural_comptypes (tree t1, tree t2, int strict)
1158 if (t1 == t2)
1159 return true;
1161 /* Suppress errors caused by previously reported errors. */
1162 if (t1 == error_mark_node || t2 == error_mark_node)
1163 return false;
1165 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1167 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1168 current instantiation. */
1169 if (TREE_CODE (t1) == TYPENAME_TYPE)
1170 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1172 if (TREE_CODE (t2) == TYPENAME_TYPE)
1173 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1175 if (TYPE_PTRMEMFUNC_P (t1))
1176 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1177 if (TYPE_PTRMEMFUNC_P (t2))
1178 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1180 /* Different classes of types can't be compatible. */
1181 if (TREE_CODE (t1) != TREE_CODE (t2))
1182 return false;
1184 /* Qualifiers must match. For array types, we will check when we
1185 recur on the array element types. */
1186 if (TREE_CODE (t1) != ARRAY_TYPE
1187 && cp_type_quals (t1) != cp_type_quals (t2))
1188 return false;
1189 if (TREE_CODE (t1) == FUNCTION_TYPE
1190 && type_memfn_quals (t1) != type_memfn_quals (t2))
1191 return false;
1192 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1193 return false;
1195 /* Allow for two different type nodes which have essentially the same
1196 definition. Note that we already checked for equality of the type
1197 qualifiers (just above). */
1199 if (TREE_CODE (t1) != ARRAY_TYPE
1200 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1201 return true;
1204 /* Compare the types. Break out if they could be the same. */
1205 switch (TREE_CODE (t1))
1207 case VOID_TYPE:
1208 case BOOLEAN_TYPE:
1209 /* All void and bool types are the same. */
1210 break;
1212 case INTEGER_TYPE:
1213 case FIXED_POINT_TYPE:
1214 case REAL_TYPE:
1215 /* With these nodes, we can't determine type equivalence by
1216 looking at what is stored in the nodes themselves, because
1217 two nodes might have different TYPE_MAIN_VARIANTs but still
1218 represent the same type. For example, wchar_t and int could
1219 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1220 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1221 and are distinct types. On the other hand, int and the
1222 following typedef
1224 typedef int INT __attribute((may_alias));
1226 have identical properties, different TYPE_MAIN_VARIANTs, but
1227 represent the same type. The canonical type system keeps
1228 track of equivalence in this case, so we fall back on it. */
1229 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1231 case TEMPLATE_TEMPLATE_PARM:
1232 case BOUND_TEMPLATE_TEMPLATE_PARM:
1233 if (!comp_template_parms_position (t1, t2))
1234 return false;
1235 if (!comp_template_parms
1236 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1237 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1238 return false;
1239 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1240 break;
1241 /* Don't check inheritance. */
1242 strict = COMPARE_STRICT;
1243 /* Fall through. */
1245 case RECORD_TYPE:
1246 case UNION_TYPE:
1247 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1248 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1249 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1250 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1251 break;
1253 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1254 break;
1255 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1256 break;
1258 return false;
1260 case OFFSET_TYPE:
1261 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1262 strict & ~COMPARE_REDECLARATION))
1263 return false;
1264 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1265 return false;
1266 break;
1268 case REFERENCE_TYPE:
1269 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1270 return false;
1271 /* fall through to checks for pointer types */
1273 case POINTER_TYPE:
1274 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1275 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1276 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1277 return false;
1278 break;
1280 case METHOD_TYPE:
1281 case FUNCTION_TYPE:
1282 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1283 return false;
1284 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1285 return false;
1286 break;
1288 case ARRAY_TYPE:
1289 /* Target types must match incl. qualifiers. */
1290 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1291 return false;
1292 break;
1294 case TEMPLATE_TYPE_PARM:
1295 /* If T1 and T2 don't have the same relative position in their
1296 template parameters set, they can't be equal. */
1297 if (!comp_template_parms_position (t1, t2))
1298 return false;
1299 break;
1301 case TYPENAME_TYPE:
1302 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1303 TYPENAME_TYPE_FULLNAME (t2)))
1304 return false;
1305 /* Qualifiers don't matter on scopes. */
1306 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1307 TYPE_CONTEXT (t2)))
1308 return false;
1309 break;
1311 case UNBOUND_CLASS_TEMPLATE:
1312 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1313 return false;
1314 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1315 return false;
1316 break;
1318 case COMPLEX_TYPE:
1319 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1320 return false;
1321 break;
1323 case VECTOR_TYPE:
1324 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1325 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1326 return false;
1327 break;
1329 case TYPE_PACK_EXPANSION:
1330 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1331 PACK_EXPANSION_PATTERN (t2))
1332 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1333 PACK_EXPANSION_EXTRA_ARGS (t2)));
1335 case DECLTYPE_TYPE:
1336 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1337 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1338 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1339 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1340 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1341 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1342 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1343 DECLTYPE_TYPE_EXPR (t2)))
1344 return false;
1345 break;
1347 case UNDERLYING_TYPE:
1348 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1349 UNDERLYING_TYPE_TYPE (t2));
1351 default:
1352 return false;
1355 /* If we get here, we know that from a target independent POV the
1356 types are the same. Make sure the target attributes are also
1357 the same. */
1358 return comp_type_attributes (t1, t2);
1361 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1362 is a bitwise-or of the COMPARE_* flags. */
1364 bool
1365 comptypes (tree t1, tree t2, int strict)
1367 if (strict == COMPARE_STRICT)
1369 if (t1 == t2)
1370 return true;
1372 if (t1 == error_mark_node || t2 == error_mark_node)
1373 return false;
1375 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1376 /* At least one of the types requires structural equality, so
1377 perform a deep check. */
1378 return structural_comptypes (t1, t2, strict);
1380 #ifdef ENABLE_CHECKING
1381 if (USE_CANONICAL_TYPES)
1383 bool result = structural_comptypes (t1, t2, strict);
1385 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1386 /* The two types are structurally equivalent, but their
1387 canonical types were different. This is a failure of the
1388 canonical type propagation code.*/
1389 internal_error
1390 ("canonical types differ for identical types %T and %T",
1391 t1, t2);
1392 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1393 /* Two types are structurally different, but the canonical
1394 types are the same. This means we were over-eager in
1395 assigning canonical types. */
1396 internal_error
1397 ("same canonical type node for different types %T and %T",
1398 t1, t2);
1400 return result;
1402 #else
1403 if (USE_CANONICAL_TYPES)
1404 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1405 #endif
1406 else
1407 return structural_comptypes (t1, t2, strict);
1409 else if (strict == COMPARE_STRUCTURAL)
1410 return structural_comptypes (t1, t2, COMPARE_STRICT);
1411 else
1412 return structural_comptypes (t1, t2, strict);
1415 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1416 top-level qualifiers. */
1418 bool
1419 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1421 if (type1 == error_mark_node || type2 == error_mark_node)
1422 return false;
1424 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1427 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1429 bool
1430 at_least_as_qualified_p (const_tree type1, const_tree type2)
1432 int q1 = cp_type_quals (type1);
1433 int q2 = cp_type_quals (type2);
1435 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1436 return (q1 & q2) == q2;
1439 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1440 more cv-qualified that TYPE1, and 0 otherwise. */
1443 comp_cv_qualification (const_tree type1, const_tree type2)
1445 int q1 = cp_type_quals (type1);
1446 int q2 = cp_type_quals (type2);
1448 if (q1 == q2)
1449 return 0;
1451 if ((q1 & q2) == q2)
1452 return 1;
1453 else if ((q1 & q2) == q1)
1454 return -1;
1456 return 0;
1459 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1460 subset of the cv-qualification signature of TYPE2, and the types
1461 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1464 comp_cv_qual_signature (tree type1, tree type2)
1466 if (comp_ptr_ttypes_real (type2, type1, -1))
1467 return 1;
1468 else if (comp_ptr_ttypes_real (type1, type2, -1))
1469 return -1;
1470 else
1471 return 0;
1474 /* Subroutines of `comptypes'. */
1476 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1477 equivalent in the sense that functions with those parameter types
1478 can have equivalent types. The two lists must be equivalent,
1479 element by element. */
1481 bool
1482 compparms (const_tree parms1, const_tree parms2)
1484 const_tree t1, t2;
1486 /* An unspecified parmlist matches any specified parmlist
1487 whose argument types don't need default promotions. */
1489 for (t1 = parms1, t2 = parms2;
1490 t1 || t2;
1491 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1493 /* If one parmlist is shorter than the other,
1494 they fail to match. */
1495 if (!t1 || !t2)
1496 return false;
1497 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1498 return false;
1500 return true;
1504 /* Process a sizeof or alignof expression where the operand is a
1505 type. */
1507 tree
1508 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1510 tree value;
1511 bool dependent_p;
1513 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1514 if (type == error_mark_node)
1515 return error_mark_node;
1517 type = non_reference (type);
1518 if (TREE_CODE (type) == METHOD_TYPE)
1520 if (complain)
1521 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
1522 "invalid application of %qs to a member function",
1523 operator_name_info[(int) op].name);
1524 value = size_one_node;
1527 dependent_p = dependent_type_p (type);
1528 if (!dependent_p)
1529 complete_type (type);
1530 if (dependent_p
1531 /* VLA types will have a non-constant size. In the body of an
1532 uninstantiated template, we don't need to try to compute the
1533 value, because the sizeof expression is not an integral
1534 constant expression in that case. And, if we do try to
1535 compute the value, we'll likely end up with SAVE_EXPRs, which
1536 the template substitution machinery does not expect to see. */
1537 || (processing_template_decl
1538 && COMPLETE_TYPE_P (type)
1539 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1541 value = build_min (op, size_type_node, type);
1542 TREE_READONLY (value) = 1;
1543 return value;
1546 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1547 op == SIZEOF_EXPR,
1548 complain);
1551 /* Return the size of the type, without producing any warnings for
1552 types whose size cannot be taken. This routine should be used only
1553 in some other routine that has already produced a diagnostic about
1554 using the size of such a type. */
1555 tree
1556 cxx_sizeof_nowarn (tree type)
1558 if (TREE_CODE (type) == FUNCTION_TYPE
1559 || TREE_CODE (type) == VOID_TYPE
1560 || TREE_CODE (type) == ERROR_MARK)
1561 return size_one_node;
1562 else if (!COMPLETE_TYPE_P (type))
1563 return size_zero_node;
1564 else
1565 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1568 /* Process a sizeof expression where the operand is an expression. */
1570 static tree
1571 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1573 if (e == error_mark_node)
1574 return error_mark_node;
1576 if (processing_template_decl)
1578 e = build_min (SIZEOF_EXPR, size_type_node, e);
1579 TREE_SIDE_EFFECTS (e) = 0;
1580 TREE_READONLY (e) = 1;
1582 return e;
1585 /* To get the size of a static data member declared as an array of
1586 unknown bound, we need to instantiate it. */
1587 if (TREE_CODE (e) == VAR_DECL
1588 && VAR_HAD_UNKNOWN_BOUND (e)
1589 && DECL_TEMPLATE_INSTANTIATION (e))
1590 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1592 e = mark_type_use (e);
1594 if (TREE_CODE (e) == COMPONENT_REF
1595 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1596 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1598 if (complain & tf_error)
1599 error ("invalid application of %<sizeof%> to a bit-field");
1600 else
1601 return error_mark_node;
1602 e = char_type_node;
1604 else if (is_overloaded_fn (e))
1606 if (complain & tf_error)
1607 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1608 "function type");
1609 else
1610 return error_mark_node;
1611 e = char_type_node;
1613 else if (type_unknown_p (e))
1615 if (complain & tf_error)
1616 cxx_incomplete_type_error (e, TREE_TYPE (e));
1617 else
1618 return error_mark_node;
1619 e = char_type_node;
1621 else
1622 e = TREE_TYPE (e);
1624 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1627 /* Implement the __alignof keyword: Return the minimum required
1628 alignment of E, measured in bytes. For VAR_DECL's and
1629 FIELD_DECL's return DECL_ALIGN (which can be set from an
1630 "aligned" __attribute__ specification). */
1632 static tree
1633 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1635 tree t;
1637 if (e == error_mark_node)
1638 return error_mark_node;
1640 if (processing_template_decl)
1642 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1643 TREE_SIDE_EFFECTS (e) = 0;
1644 TREE_READONLY (e) = 1;
1646 return e;
1649 e = mark_type_use (e);
1651 if (TREE_CODE (e) == VAR_DECL)
1652 t = size_int (DECL_ALIGN_UNIT (e));
1653 else if (TREE_CODE (e) == COMPONENT_REF
1654 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1655 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1657 if (complain & tf_error)
1658 error ("invalid application of %<__alignof%> to a bit-field");
1659 else
1660 return error_mark_node;
1661 t = size_one_node;
1663 else if (TREE_CODE (e) == COMPONENT_REF
1664 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1665 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1666 else if (is_overloaded_fn (e))
1668 if (complain & tf_error)
1669 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1670 "function type");
1671 else
1672 return error_mark_node;
1673 if (TREE_CODE (e) == FUNCTION_DECL)
1674 t = size_int (DECL_ALIGN_UNIT (e));
1675 else
1676 t = size_one_node;
1678 else if (type_unknown_p (e))
1680 if (complain & tf_error)
1681 cxx_incomplete_type_error (e, TREE_TYPE (e));
1682 else
1683 return error_mark_node;
1684 t = size_one_node;
1686 else
1687 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1688 complain & tf_error);
1690 return fold_convert (size_type_node, t);
1693 /* Process a sizeof or alignof expression E with code OP where the operand
1694 is an expression. */
1696 tree
1697 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1699 if (op == SIZEOF_EXPR)
1700 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1701 else
1702 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1705 /* EXPR is being used in a context that is not a function call.
1706 Enforce:
1708 [expr.ref]
1710 The expression can be used only as the left-hand operand of a
1711 member function call.
1713 [expr.mptr.operator]
1715 If the result of .* or ->* is a function, then that result can be
1716 used only as the operand for the function call operator ().
1718 by issuing an error message if appropriate. Returns true iff EXPR
1719 violates these rules. */
1721 bool
1722 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1724 if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1726 if (complain & tf_error)
1727 error ("invalid use of non-static member function");
1728 return true;
1730 return false;
1733 /* If EXP is a reference to a bitfield, and the type of EXP does not
1734 match the declared type of the bitfield, return the declared type
1735 of the bitfield. Otherwise, return NULL_TREE. */
1737 tree
1738 is_bitfield_expr_with_lowered_type (const_tree exp)
1740 switch (TREE_CODE (exp))
1742 case COND_EXPR:
1743 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1744 ? TREE_OPERAND (exp, 1)
1745 : TREE_OPERAND (exp, 0)))
1746 return NULL_TREE;
1747 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1749 case COMPOUND_EXPR:
1750 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1752 case MODIFY_EXPR:
1753 case SAVE_EXPR:
1754 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1756 case COMPONENT_REF:
1758 tree field;
1760 field = TREE_OPERAND (exp, 1);
1761 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1762 return NULL_TREE;
1763 if (same_type_ignoring_top_level_qualifiers_p
1764 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1765 return NULL_TREE;
1766 return DECL_BIT_FIELD_TYPE (field);
1769 CASE_CONVERT:
1770 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1771 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1772 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1773 /* Fallthrough. */
1775 default:
1776 return NULL_TREE;
1780 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1781 bitfield with a lowered type, the type of EXP is returned, rather
1782 than NULL_TREE. */
1784 tree
1785 unlowered_expr_type (const_tree exp)
1787 tree type;
1788 tree etype = TREE_TYPE (exp);
1790 type = is_bitfield_expr_with_lowered_type (exp);
1791 if (type)
1792 type = cp_build_qualified_type (type, cp_type_quals (etype));
1793 else
1794 type = etype;
1796 return type;
1799 /* Perform the conversions in [expr] that apply when an lvalue appears
1800 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1801 function-to-pointer conversions. In addition, manifest constants
1802 are replaced by their values, and bitfield references are converted
1803 to their declared types. Note that this function does not perform the
1804 lvalue-to-rvalue conversion for class types. If you need that conversion
1805 to for class types, then you probably need to use force_rvalue.
1807 Although the returned value is being used as an rvalue, this
1808 function does not wrap the returned expression in a
1809 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1810 that the return value is no longer an lvalue. */
1812 tree
1813 decay_conversion (tree exp, tsubst_flags_t complain)
1815 tree type;
1816 enum tree_code code;
1817 location_t loc = EXPR_LOC_OR_HERE (exp);
1819 type = TREE_TYPE (exp);
1820 if (type == error_mark_node)
1821 return error_mark_node;
1823 exp = mark_rvalue_use (exp);
1825 exp = resolve_nondeduced_context (exp);
1826 if (type_unknown_p (exp))
1828 if (complain & tf_error)
1829 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1830 return error_mark_node;
1833 /* FIXME remove? at least need to remember that this isn't really a
1834 constant expression if EXP isn't decl_constant_var_p, like with
1835 C_MAYBE_CONST_EXPR. */
1836 exp = decl_constant_value_safe (exp);
1837 if (error_operand_p (exp))
1838 return error_mark_node;
1840 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1841 return nullptr_node;
1843 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1844 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1845 code = TREE_CODE (type);
1846 if (code == VOID_TYPE)
1848 if (complain & tf_error)
1849 error_at (loc, "void value not ignored as it ought to be");
1850 return error_mark_node;
1852 if (invalid_nonstatic_memfn_p (exp, complain))
1853 return error_mark_node;
1854 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1855 return cp_build_addr_expr (exp, complain);
1856 if (code == ARRAY_TYPE)
1858 tree adr;
1859 tree ptrtype;
1861 if (TREE_CODE (exp) == INDIRECT_REF)
1862 return build_nop (build_pointer_type (TREE_TYPE (type)),
1863 TREE_OPERAND (exp, 0));
1865 if (TREE_CODE (exp) == COMPOUND_EXPR)
1867 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1868 if (op1 == error_mark_node)
1869 return error_mark_node;
1870 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1871 TREE_OPERAND (exp, 0), op1);
1874 if (!lvalue_p (exp)
1875 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1877 if (complain & tf_error)
1878 error_at (loc, "invalid use of non-lvalue array");
1879 return error_mark_node;
1882 /* Don't let an array compound literal decay to a pointer. It can
1883 still be used to initialize an array or bind to a reference. */
1884 if (TREE_CODE (exp) == TARGET_EXPR)
1886 if (complain & tf_error)
1887 error_at (loc, "taking address of temporary array");
1888 return error_mark_node;
1891 ptrtype = build_pointer_type (TREE_TYPE (type));
1893 if (TREE_CODE (exp) == VAR_DECL)
1895 if (!cxx_mark_addressable (exp))
1896 return error_mark_node;
1897 adr = build_nop (ptrtype, build_address (exp));
1898 return adr;
1900 /* This way is better for a COMPONENT_REF since it can
1901 simplify the offset for a component. */
1902 adr = cp_build_addr_expr (exp, complain);
1903 return cp_convert (ptrtype, adr, complain);
1906 /* If a bitfield is used in a context where integral promotion
1907 applies, then the caller is expected to have used
1908 default_conversion. That function promotes bitfields correctly
1909 before calling this function. At this point, if we have a
1910 bitfield referenced, we may assume that is not subject to
1911 promotion, and that, therefore, the type of the resulting rvalue
1912 is the declared type of the bitfield. */
1913 exp = convert_bitfield_to_declared_type (exp);
1915 /* We do not call rvalue() here because we do not want to wrap EXP
1916 in a NON_LVALUE_EXPR. */
1918 /* [basic.lval]
1920 Non-class rvalues always have cv-unqualified types. */
1921 type = TREE_TYPE (exp);
1922 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1923 exp = build_nop (cv_unqualified (type), exp);
1925 return exp;
1928 /* Perform preparatory conversions, as part of the "usual arithmetic
1929 conversions". In particular, as per [expr]:
1931 Whenever an lvalue expression appears as an operand of an
1932 operator that expects the rvalue for that operand, the
1933 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1934 standard conversions are applied to convert the expression to an
1935 rvalue.
1937 In addition, we perform integral promotions here, as those are
1938 applied to both operands to a binary operator before determining
1939 what additional conversions should apply. */
1941 static tree
1942 cp_default_conversion (tree exp, tsubst_flags_t complain)
1944 /* Check for target-specific promotions. */
1945 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1946 if (promoted_type)
1947 exp = cp_convert (promoted_type, exp, complain);
1948 /* Perform the integral promotions first so that bitfield
1949 expressions (which may promote to "int", even if the bitfield is
1950 declared "unsigned") are promoted correctly. */
1951 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1952 exp = cp_perform_integral_promotions (exp, complain);
1953 /* Perform the other conversions. */
1954 exp = decay_conversion (exp, complain);
1956 return exp;
1959 /* C version. */
1961 tree
1962 default_conversion (tree exp)
1964 return cp_default_conversion (exp, tf_warning_or_error);
1967 /* EXPR is an expression with an integral or enumeration type.
1968 Perform the integral promotions in [conv.prom], and return the
1969 converted value. */
1971 tree
1972 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
1974 tree type;
1975 tree promoted_type;
1977 expr = mark_rvalue_use (expr);
1979 /* [conv.prom]
1981 If the bitfield has an enumerated type, it is treated as any
1982 other value of that type for promotion purposes. */
1983 type = is_bitfield_expr_with_lowered_type (expr);
1984 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1985 type = TREE_TYPE (expr);
1986 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1987 /* Scoped enums don't promote. */
1988 if (SCOPED_ENUM_P (type))
1989 return expr;
1990 promoted_type = type_promotes_to (type);
1991 if (type != promoted_type)
1992 expr = cp_convert (promoted_type, expr, complain);
1993 return expr;
1996 /* C version. */
1998 tree
1999 perform_integral_promotions (tree expr)
2001 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2004 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2005 decay_conversion to one. */
2008 string_conv_p (const_tree totype, const_tree exp, int warn)
2010 tree t;
2012 if (TREE_CODE (totype) != POINTER_TYPE)
2013 return 0;
2015 t = TREE_TYPE (totype);
2016 if (!same_type_p (t, char_type_node)
2017 && !same_type_p (t, char16_type_node)
2018 && !same_type_p (t, char32_type_node)
2019 && !same_type_p (t, wchar_type_node))
2020 return 0;
2022 if (TREE_CODE (exp) == STRING_CST)
2024 /* Make sure that we don't try to convert between char and wide chars. */
2025 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2026 return 0;
2028 else
2030 /* Is this a string constant which has decayed to 'const char *'? */
2031 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2032 if (!same_type_p (TREE_TYPE (exp), t))
2033 return 0;
2034 STRIP_NOPS (exp);
2035 if (TREE_CODE (exp) != ADDR_EXPR
2036 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2037 return 0;
2040 /* This warning is not very useful, as it complains about printf. */
2041 if (warn)
2042 warning (OPT_Wwrite_strings,
2043 "deprecated conversion from string constant to %qT",
2044 totype);
2046 return 1;
2049 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2050 can, for example, use as an lvalue. This code used to be in
2051 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2052 expressions, where we're dealing with aggregates. But now it's again only
2053 called from unary_complex_lvalue. The case (in particular) that led to
2054 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2055 get it there. */
2057 static tree
2058 rationalize_conditional_expr (enum tree_code code, tree t,
2059 tsubst_flags_t complain)
2061 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2062 the first operand is always the one to be used if both operands
2063 are equal, so we know what conditional expression this used to be. */
2064 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2066 tree op0 = TREE_OPERAND (t, 0);
2067 tree op1 = TREE_OPERAND (t, 1);
2069 /* The following code is incorrect if either operand side-effects. */
2070 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2071 && !TREE_SIDE_EFFECTS (op1));
2072 return
2073 build_conditional_expr (build_x_binary_op (input_location,
2074 (TREE_CODE (t) == MIN_EXPR
2075 ? LE_EXPR : GE_EXPR),
2076 op0, TREE_CODE (op0),
2077 op1, TREE_CODE (op1),
2078 /*overload=*/NULL,
2079 complain),
2080 cp_build_unary_op (code, op0, 0, complain),
2081 cp_build_unary_op (code, op1, 0, complain),
2082 complain);
2085 return
2086 build_conditional_expr (TREE_OPERAND (t, 0),
2087 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2088 complain),
2089 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2090 complain),
2091 complain);
2094 /* Given the TYPE of an anonymous union field inside T, return the
2095 FIELD_DECL for the field. If not found return NULL_TREE. Because
2096 anonymous unions can nest, we must also search all anonymous unions
2097 that are directly reachable. */
2099 tree
2100 lookup_anon_field (tree t, tree type)
2102 tree field;
2104 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2106 if (TREE_STATIC (field))
2107 continue;
2108 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2109 continue;
2111 /* If we find it directly, return the field. */
2112 if (DECL_NAME (field) == NULL_TREE
2113 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2115 return field;
2118 /* Otherwise, it could be nested, search harder. */
2119 if (DECL_NAME (field) == NULL_TREE
2120 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2122 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2123 if (subfield)
2124 return subfield;
2127 return NULL_TREE;
2130 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2131 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2132 non-NULL, it indicates the path to the base used to name MEMBER.
2133 If PRESERVE_REFERENCE is true, the expression returned will have
2134 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2135 returned will have the type referred to by the reference.
2137 This function does not perform access control; that is either done
2138 earlier by the parser when the name of MEMBER is resolved to MEMBER
2139 itself, or later when overload resolution selects one of the
2140 functions indicated by MEMBER. */
2142 tree
2143 build_class_member_access_expr (tree object, tree member,
2144 tree access_path, bool preserve_reference,
2145 tsubst_flags_t complain)
2147 tree object_type;
2148 tree member_scope;
2149 tree result = NULL_TREE;
2150 tree using_decl = NULL_TREE;
2152 if (error_operand_p (object) || error_operand_p (member))
2153 return error_mark_node;
2155 gcc_assert (DECL_P (member) || BASELINK_P (member));
2157 /* [expr.ref]
2159 The type of the first expression shall be "class object" (of a
2160 complete type). */
2161 object_type = TREE_TYPE (object);
2162 if (!currently_open_class (object_type)
2163 && !complete_type_or_maybe_complain (object_type, object, complain))
2164 return error_mark_node;
2165 if (!CLASS_TYPE_P (object_type))
2167 if (complain & tf_error)
2169 if (POINTER_TYPE_P (object_type)
2170 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2171 error ("request for member %qD in %qE, which is of pointer "
2172 "type %qT (maybe you meant to use %<->%> ?)",
2173 member, object, object_type);
2174 else
2175 error ("request for member %qD in %qE, which is of non-class "
2176 "type %qT", member, object, object_type);
2178 return error_mark_node;
2181 /* The standard does not seem to actually say that MEMBER must be a
2182 member of OBJECT_TYPE. However, that is clearly what is
2183 intended. */
2184 if (DECL_P (member))
2186 member_scope = DECL_CLASS_CONTEXT (member);
2187 mark_used (member);
2188 if (TREE_DEPRECATED (member))
2189 warn_deprecated_use (member, NULL_TREE);
2191 else
2192 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2193 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2194 presently be the anonymous union. Go outwards until we find a
2195 type related to OBJECT_TYPE. */
2196 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2197 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2198 object_type))
2199 member_scope = TYPE_CONTEXT (member_scope);
2200 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2202 if (complain & tf_error)
2204 if (TREE_CODE (member) == FIELD_DECL)
2205 error ("invalid use of nonstatic data member %qE", member);
2206 else
2207 error ("%qD is not a member of %qT", member, object_type);
2209 return error_mark_node;
2212 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2213 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2214 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2216 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2217 if (temp)
2218 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2221 /* In [expr.ref], there is an explicit list of the valid choices for
2222 MEMBER. We check for each of those cases here. */
2223 if (TREE_CODE (member) == VAR_DECL)
2225 /* A static data member. */
2226 result = member;
2227 mark_exp_read (object);
2228 /* If OBJECT has side-effects, they are supposed to occur. */
2229 if (TREE_SIDE_EFFECTS (object))
2230 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2232 else if (TREE_CODE (member) == FIELD_DECL)
2234 /* A non-static data member. */
2235 bool null_object_p;
2236 int type_quals;
2237 tree member_type;
2239 null_object_p = (TREE_CODE (object) == INDIRECT_REF
2240 && integer_zerop (TREE_OPERAND (object, 0)));
2242 /* Convert OBJECT to the type of MEMBER. */
2243 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2244 TYPE_MAIN_VARIANT (member_scope)))
2246 tree binfo;
2247 base_kind kind;
2249 binfo = lookup_base (access_path ? access_path : object_type,
2250 member_scope, ba_unique, &kind);
2251 if (binfo == error_mark_node)
2252 return error_mark_node;
2254 /* It is invalid to try to get to a virtual base of a
2255 NULL object. The most common cause is invalid use of
2256 offsetof macro. */
2257 if (null_object_p && kind == bk_via_virtual)
2259 if (complain & tf_error)
2261 error ("invalid access to non-static data member %qD of "
2262 "NULL object",
2263 member);
2264 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2266 return error_mark_node;
2269 /* Convert to the base. */
2270 object = build_base_path (PLUS_EXPR, object, binfo,
2271 /*nonnull=*/1, complain);
2272 /* If we found the base successfully then we should be able
2273 to convert to it successfully. */
2274 gcc_assert (object != error_mark_node);
2277 /* Complain about other invalid uses of offsetof, even though they will
2278 give the right answer. Note that we complain whether or not they
2279 actually used the offsetof macro, since there's no way to know at this
2280 point. So we just give a warning, instead of a pedwarn. */
2281 /* Do not produce this warning for base class field references, because
2282 we know for a fact that didn't come from offsetof. This does occur
2283 in various testsuite cases where a null object is passed where a
2284 vtable access is required. */
2285 if (null_object_p && warn_invalid_offsetof
2286 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2287 && !DECL_FIELD_IS_BASE (member)
2288 && cp_unevaluated_operand == 0
2289 && (complain & tf_warning))
2291 warning (OPT_Winvalid_offsetof,
2292 "invalid access to non-static data member %qD "
2293 " of NULL object", member);
2294 warning (OPT_Winvalid_offsetof,
2295 "(perhaps the %<offsetof%> macro was used incorrectly)");
2298 /* If MEMBER is from an anonymous aggregate, we have converted
2299 OBJECT so that it refers to the class containing the
2300 anonymous union. Generate a reference to the anonymous union
2301 itself, and recur to find MEMBER. */
2302 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2303 /* When this code is called from build_field_call, the
2304 object already has the type of the anonymous union.
2305 That is because the COMPONENT_REF was already
2306 constructed, and was then disassembled before calling
2307 build_field_call. After the function-call code is
2308 cleaned up, this waste can be eliminated. */
2309 && (!same_type_ignoring_top_level_qualifiers_p
2310 (TREE_TYPE (object), DECL_CONTEXT (member))))
2312 tree anonymous_union;
2314 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2315 DECL_CONTEXT (member));
2316 object = build_class_member_access_expr (object,
2317 anonymous_union,
2318 /*access_path=*/NULL_TREE,
2319 preserve_reference,
2320 complain);
2323 /* Compute the type of the field, as described in [expr.ref]. */
2324 type_quals = TYPE_UNQUALIFIED;
2325 member_type = TREE_TYPE (member);
2326 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2328 type_quals = (cp_type_quals (member_type)
2329 | cp_type_quals (object_type));
2331 /* A field is const (volatile) if the enclosing object, or the
2332 field itself, is const (volatile). But, a mutable field is
2333 not const, even within a const object. */
2334 if (DECL_MUTABLE_P (member))
2335 type_quals &= ~TYPE_QUAL_CONST;
2336 member_type = cp_build_qualified_type (member_type, type_quals);
2339 result = build3 (COMPONENT_REF, member_type, object, member,
2340 NULL_TREE);
2341 result = fold_if_not_in_template (result);
2343 /* Mark the expression const or volatile, as appropriate. Even
2344 though we've dealt with the type above, we still have to mark the
2345 expression itself. */
2346 if (type_quals & TYPE_QUAL_CONST)
2347 TREE_READONLY (result) = 1;
2348 if (type_quals & TYPE_QUAL_VOLATILE)
2349 TREE_THIS_VOLATILE (result) = 1;
2351 else if (BASELINK_P (member))
2353 /* The member is a (possibly overloaded) member function. */
2354 tree functions;
2355 tree type;
2357 /* If the MEMBER is exactly one static member function, then we
2358 know the type of the expression. Otherwise, we must wait
2359 until overload resolution has been performed. */
2360 functions = BASELINK_FUNCTIONS (member);
2361 if (TREE_CODE (functions) == FUNCTION_DECL
2362 && DECL_STATIC_FUNCTION_P (functions))
2363 type = TREE_TYPE (functions);
2364 else
2365 type = unknown_type_node;
2366 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2367 base. That will happen when the function is called. */
2368 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2370 else if (TREE_CODE (member) == CONST_DECL)
2372 /* The member is an enumerator. */
2373 result = member;
2374 /* If OBJECT has side-effects, they are supposed to occur. */
2375 if (TREE_SIDE_EFFECTS (object))
2376 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2377 object, result);
2379 else if ((using_decl = strip_using_decl (member)) != member)
2380 result = build_class_member_access_expr (object,
2381 using_decl,
2382 access_path, preserve_reference,
2383 complain);
2384 else
2386 if (complain & tf_error)
2387 error ("invalid use of %qD", member);
2388 return error_mark_node;
2391 if (!preserve_reference)
2392 /* [expr.ref]
2394 If E2 is declared to have type "reference to T", then ... the
2395 type of E1.E2 is T. */
2396 result = convert_from_reference (result);
2398 return result;
2401 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2402 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2404 static tree
2405 lookup_destructor (tree object, tree scope, tree dtor_name)
2407 tree object_type = TREE_TYPE (object);
2408 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2409 tree expr;
2411 if (scope && !check_dtor_name (scope, dtor_type))
2413 error ("qualified type %qT does not match destructor name ~%qT",
2414 scope, dtor_type);
2415 return error_mark_node;
2417 if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2419 /* In a template, names we can't find a match for are still accepted
2420 destructor names, and we check them here. */
2421 if (check_dtor_name (object_type, dtor_type))
2422 dtor_type = object_type;
2423 else
2425 error ("object type %qT does not match destructor name ~%qT",
2426 object_type, dtor_type);
2427 return error_mark_node;
2431 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2433 error ("the type being destroyed is %qT, but the destructor refers to %qT",
2434 TYPE_MAIN_VARIANT (object_type), dtor_type);
2435 return error_mark_node;
2437 expr = lookup_member (dtor_type, complete_dtor_identifier,
2438 /*protect=*/1, /*want_type=*/false,
2439 tf_warning_or_error);
2440 expr = (adjust_result_of_qualified_name_lookup
2441 (expr, dtor_type, object_type));
2442 if (scope == NULL_TREE)
2443 /* We need to call adjust_result_of_qualified_name_lookup in case the
2444 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2445 that we still get virtual function binding. */
2446 BASELINK_QUALIFIED_P (expr) = false;
2447 return expr;
2450 /* An expression of the form "A::template B" has been resolved to
2451 DECL. Issue a diagnostic if B is not a template or template
2452 specialization. */
2454 void
2455 check_template_keyword (tree decl)
2457 /* The standard says:
2459 [temp.names]
2461 If a name prefixed by the keyword template is not a member
2462 template, the program is ill-formed.
2464 DR 228 removed the restriction that the template be a member
2465 template.
2467 DR 96, if accepted would add the further restriction that explicit
2468 template arguments must be provided if the template keyword is
2469 used, but, as of 2005-10-16, that DR is still in "drafting". If
2470 this DR is accepted, then the semantic checks here can be
2471 simplified, as the entity named must in fact be a template
2472 specialization, rather than, as at present, a set of overloaded
2473 functions containing at least one template function. */
2474 if (TREE_CODE (decl) != TEMPLATE_DECL
2475 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2477 if (!is_overloaded_fn (decl))
2478 permerror (input_location, "%qD is not a template", decl);
2479 else
2481 tree fns;
2482 fns = decl;
2483 if (BASELINK_P (fns))
2484 fns = BASELINK_FUNCTIONS (fns);
2485 while (fns)
2487 tree fn = OVL_CURRENT (fns);
2488 if (TREE_CODE (fn) == TEMPLATE_DECL
2489 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2490 break;
2491 if (TREE_CODE (fn) == FUNCTION_DECL
2492 && DECL_USE_TEMPLATE (fn)
2493 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2494 break;
2495 fns = OVL_NEXT (fns);
2497 if (!fns)
2498 permerror (input_location, "%qD is not a template", decl);
2503 /* This function is called by the parser to process a class member
2504 access expression of the form OBJECT.NAME. NAME is a node used by
2505 the parser to represent a name; it is not yet a DECL. It may,
2506 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2507 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2508 there is no reason to do the lookup twice, so the parser keeps the
2509 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2510 be a template via the use of the "A::template B" syntax. */
2512 tree
2513 finish_class_member_access_expr (tree object, tree name, bool template_p,
2514 tsubst_flags_t complain)
2516 tree expr;
2517 tree object_type;
2518 tree member;
2519 tree access_path = NULL_TREE;
2520 tree orig_object = object;
2521 tree orig_name = name;
2523 if (object == error_mark_node || name == error_mark_node)
2524 return error_mark_node;
2526 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2527 if (!objc_is_public (object, name))
2528 return error_mark_node;
2530 object_type = TREE_TYPE (object);
2532 if (processing_template_decl)
2534 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2535 dependent_type_p (object_type)
2536 /* If NAME is just an IDENTIFIER_NODE, then the expression
2537 is dependent. */
2538 || TREE_CODE (object) == IDENTIFIER_NODE
2539 /* If NAME is "f<args>", where either 'f' or 'args' is
2540 dependent, then the expression is dependent. */
2541 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2542 && dependent_template_id_p (TREE_OPERAND (name, 0),
2543 TREE_OPERAND (name, 1)))
2544 /* If NAME is "T::X" where "T" is dependent, then the
2545 expression is dependent. */
2546 || (TREE_CODE (name) == SCOPE_REF
2547 && TYPE_P (TREE_OPERAND (name, 0))
2548 && dependent_type_p (TREE_OPERAND (name, 0))))
2549 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2550 object, name, NULL_TREE);
2551 object = build_non_dependent_expr (object);
2553 else if (c_dialect_objc ()
2554 && TREE_CODE (name) == IDENTIFIER_NODE
2555 && (expr = objc_maybe_build_component_ref (object, name)))
2556 return expr;
2558 /* [expr.ref]
2560 The type of the first expression shall be "class object" (of a
2561 complete type). */
2562 if (!currently_open_class (object_type)
2563 && !complete_type_or_maybe_complain (object_type, object, complain))
2564 return error_mark_node;
2565 if (!CLASS_TYPE_P (object_type))
2567 if (complain & tf_error)
2569 if (POINTER_TYPE_P (object_type)
2570 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2571 error ("request for member %qD in %qE, which is of pointer "
2572 "type %qT (maybe you meant to use %<->%> ?)",
2573 name, object, object_type);
2574 else
2575 error ("request for member %qD in %qE, which is of non-class "
2576 "type %qT", name, object, object_type);
2578 return error_mark_node;
2581 if (BASELINK_P (name))
2582 /* A member function that has already been looked up. */
2583 member = name;
2584 else
2586 bool is_template_id = false;
2587 tree template_args = NULL_TREE;
2588 tree scope;
2590 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2592 is_template_id = true;
2593 template_args = TREE_OPERAND (name, 1);
2594 name = TREE_OPERAND (name, 0);
2596 if (TREE_CODE (name) == OVERLOAD)
2597 name = DECL_NAME (get_first_fn (name));
2598 else if (DECL_P (name))
2599 name = DECL_NAME (name);
2602 if (TREE_CODE (name) == SCOPE_REF)
2604 /* A qualified name. The qualifying class or namespace `S'
2605 has already been looked up; it is either a TYPE or a
2606 NAMESPACE_DECL. */
2607 scope = TREE_OPERAND (name, 0);
2608 name = TREE_OPERAND (name, 1);
2610 /* If SCOPE is a namespace, then the qualified name does not
2611 name a member of OBJECT_TYPE. */
2612 if (TREE_CODE (scope) == NAMESPACE_DECL)
2614 if (complain & tf_error)
2615 error ("%<%D::%D%> is not a member of %qT",
2616 scope, name, object_type);
2617 return error_mark_node;
2620 gcc_assert (CLASS_TYPE_P (scope));
2621 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2622 || TREE_CODE (name) == BIT_NOT_EXPR);
2624 if (constructor_name_p (name, scope))
2626 if (complain & tf_error)
2627 error ("cannot call constructor %<%T::%D%> directly",
2628 scope, name);
2629 return error_mark_node;
2632 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2633 access_path = lookup_base (object_type, scope, ba_check, NULL);
2634 if (access_path == error_mark_node)
2635 return error_mark_node;
2636 if (!access_path)
2638 if (complain & tf_error)
2639 error ("%qT is not a base of %qT", scope, object_type);
2640 return error_mark_node;
2643 else
2645 scope = NULL_TREE;
2646 access_path = object_type;
2649 if (TREE_CODE (name) == BIT_NOT_EXPR)
2650 member = lookup_destructor (object, scope, name);
2651 else
2653 /* Look up the member. */
2654 member = lookup_member (access_path, name, /*protect=*/1,
2655 /*want_type=*/false, complain);
2656 if (member == NULL_TREE)
2658 if (complain & tf_error)
2659 error ("%qD has no member named %qE",
2660 TREE_CODE (access_path) == TREE_BINFO
2661 ? TREE_TYPE (access_path) : object_type, name);
2662 return error_mark_node;
2664 if (member == error_mark_node)
2665 return error_mark_node;
2668 if (is_template_id)
2670 tree templ = member;
2672 if (BASELINK_P (templ))
2673 templ = lookup_template_function (templ, template_args);
2674 else
2676 if (complain & tf_error)
2677 error ("%qD is not a member template function", name);
2678 return error_mark_node;
2683 if (TREE_DEPRECATED (member))
2684 warn_deprecated_use (member, NULL_TREE);
2686 if (template_p)
2687 check_template_keyword (member);
2689 expr = build_class_member_access_expr (object, member, access_path,
2690 /*preserve_reference=*/false,
2691 complain);
2692 if (processing_template_decl && expr != error_mark_node)
2694 if (BASELINK_P (member))
2696 if (TREE_CODE (orig_name) == SCOPE_REF)
2697 BASELINK_QUALIFIED_P (member) = 1;
2698 orig_name = member;
2700 return build_min_non_dep (COMPONENT_REF, expr,
2701 orig_object, orig_name,
2702 NULL_TREE);
2705 return expr;
2708 /* Return an expression for the MEMBER_NAME field in the internal
2709 representation of PTRMEM, a pointer-to-member function. (Each
2710 pointer-to-member function type gets its own RECORD_TYPE so it is
2711 more convenient to access the fields by name than by FIELD_DECL.)
2712 This routine converts the NAME to a FIELD_DECL and then creates the
2713 node for the complete expression. */
2715 tree
2716 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2718 tree ptrmem_type;
2719 tree member;
2720 tree member_type;
2722 /* This code is a stripped down version of
2723 build_class_member_access_expr. It does not work to use that
2724 routine directly because it expects the object to be of class
2725 type. */
2726 ptrmem_type = TREE_TYPE (ptrmem);
2727 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2728 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2729 /*want_type=*/false, tf_warning_or_error);
2730 member_type = cp_build_qualified_type (TREE_TYPE (member),
2731 cp_type_quals (ptrmem_type));
2732 return fold_build3_loc (input_location,
2733 COMPONENT_REF, member_type,
2734 ptrmem, member, NULL_TREE);
2737 /* Given an expression PTR for a pointer, return an expression
2738 for the value pointed to.
2739 ERRORSTRING is the name of the operator to appear in error messages.
2741 This function may need to overload OPERATOR_FNNAME.
2742 Must also handle REFERENCE_TYPEs for C++. */
2744 tree
2745 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2746 tsubst_flags_t complain)
2748 tree orig_expr = expr;
2749 tree rval;
2751 if (processing_template_decl)
2753 /* Retain the type if we know the operand is a pointer. */
2754 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2755 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2756 if (type_dependent_expression_p (expr))
2757 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2758 expr = build_non_dependent_expr (expr);
2761 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2762 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2763 if (!rval)
2764 rval = cp_build_indirect_ref (expr, errorstring, complain);
2766 if (processing_template_decl && rval != error_mark_node)
2767 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2768 else
2769 return rval;
2772 /* Helper function called from c-common. */
2773 tree
2774 build_indirect_ref (location_t loc ATTRIBUTE_UNUSED,
2775 tree ptr, ref_operator errorstring)
2777 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2780 tree
2781 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2782 tsubst_flags_t complain)
2784 tree pointer, type;
2786 if (ptr == current_class_ptr)
2787 return current_class_ref;
2789 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2790 ? ptr : decay_conversion (ptr, complain));
2791 if (pointer == error_mark_node)
2792 return error_mark_node;
2794 type = TREE_TYPE (pointer);
2796 if (POINTER_TYPE_P (type))
2798 /* [expr.unary.op]
2800 If the type of the expression is "pointer to T," the type
2801 of the result is "T." */
2802 tree t = TREE_TYPE (type);
2804 if (CONVERT_EXPR_P (ptr)
2805 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2807 /* If a warning is issued, mark it to avoid duplicates from
2808 the backend. This only needs to be done at
2809 warn_strict_aliasing > 2. */
2810 if (warn_strict_aliasing > 2)
2811 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2812 type, TREE_OPERAND (ptr, 0)))
2813 TREE_NO_WARNING (ptr) = 1;
2816 if (VOID_TYPE_P (t))
2818 /* A pointer to incomplete type (other than cv void) can be
2819 dereferenced [expr.unary.op]/1 */
2820 if (complain & tf_error)
2821 error ("%qT is not a pointer-to-object type", type);
2822 return error_mark_node;
2824 else if (TREE_CODE (pointer) == ADDR_EXPR
2825 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2826 /* The POINTER was something like `&x'. We simplify `*&x' to
2827 `x'. */
2828 return TREE_OPERAND (pointer, 0);
2829 else
2831 tree ref = build1 (INDIRECT_REF, t, pointer);
2833 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2834 so that we get the proper error message if the result is used
2835 to assign to. Also, &* is supposed to be a no-op. */
2836 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2837 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2838 TREE_SIDE_EFFECTS (ref)
2839 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2840 return ref;
2843 else if (!(complain & tf_error))
2844 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2846 /* `pointer' won't be an error_mark_node if we were given a
2847 pointer to member, so it's cool to check for this here. */
2848 else if (TYPE_PTRMEM_P (type))
2849 switch (errorstring)
2851 case RO_ARRAY_INDEXING:
2852 error ("invalid use of array indexing on pointer to member");
2853 break;
2854 case RO_UNARY_STAR:
2855 error ("invalid use of unary %<*%> on pointer to member");
2856 break;
2857 case RO_IMPLICIT_CONVERSION:
2858 error ("invalid use of implicit conversion on pointer to member");
2859 break;
2860 default:
2861 gcc_unreachable ();
2863 else if (pointer != error_mark_node)
2864 invalid_indirection_error (input_location, type, errorstring);
2866 return error_mark_node;
2869 /* This handles expressions of the form "a[i]", which denotes
2870 an array reference.
2872 This is logically equivalent in C to *(a+i), but we may do it differently.
2873 If A is a variable or a member, we generate a primitive ARRAY_REF.
2874 This avoids forcing the array out of registers, and can work on
2875 arrays that are not lvalues (for example, members of structures returned
2876 by functions).
2878 If INDEX is of some user-defined type, it must be converted to
2879 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2880 will inherit the type of the array, which will be some pointer type.
2882 LOC is the location to use in building the array reference. */
2884 tree
2885 cp_build_array_ref (location_t loc, tree array, tree idx,
2886 tsubst_flags_t complain)
2888 tree ret;
2890 if (idx == 0)
2892 if (complain & tf_error)
2893 error_at (loc, "subscript missing in array reference");
2894 return error_mark_node;
2897 if (TREE_TYPE (array) == error_mark_node
2898 || TREE_TYPE (idx) == error_mark_node)
2899 return error_mark_node;
2901 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2902 inside it. */
2903 switch (TREE_CODE (array))
2905 case COMPOUND_EXPR:
2907 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2908 complain);
2909 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2910 TREE_OPERAND (array, 0), value);
2911 SET_EXPR_LOCATION (ret, loc);
2912 return ret;
2915 case COND_EXPR:
2916 ret = build_conditional_expr
2917 (TREE_OPERAND (array, 0),
2918 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2919 complain),
2920 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2921 complain),
2922 complain);
2923 protected_set_expr_location (ret, loc);
2924 return ret;
2926 default:
2927 break;
2930 convert_vector_to_pointer_for_subscript (loc, &array, idx);
2932 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2934 tree rval, type;
2936 warn_array_subscript_with_type_char (idx);
2938 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2940 if (complain & tf_error)
2941 error_at (loc, "array subscript is not an integer");
2942 return error_mark_node;
2945 /* Apply integral promotions *after* noticing character types.
2946 (It is unclear why we do these promotions -- the standard
2947 does not say that we should. In fact, the natural thing would
2948 seem to be to convert IDX to ptrdiff_t; we're performing
2949 pointer arithmetic.) */
2950 idx = cp_perform_integral_promotions (idx, complain);
2952 /* An array that is indexed by a non-constant
2953 cannot be stored in a register; we must be able to do
2954 address arithmetic on its address.
2955 Likewise an array of elements of variable size. */
2956 if (TREE_CODE (idx) != INTEGER_CST
2957 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2958 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2959 != INTEGER_CST)))
2961 if (!cxx_mark_addressable (array))
2962 return error_mark_node;
2965 /* An array that is indexed by a constant value which is not within
2966 the array bounds cannot be stored in a register either; because we
2967 would get a crash in store_bit_field/extract_bit_field when trying
2968 to access a non-existent part of the register. */
2969 if (TREE_CODE (idx) == INTEGER_CST
2970 && TYPE_DOMAIN (TREE_TYPE (array))
2971 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2973 if (!cxx_mark_addressable (array))
2974 return error_mark_node;
2977 if (!lvalue_p (array) && (complain & tf_error))
2978 pedwarn (loc, OPT_Wpedantic,
2979 "ISO C++ forbids subscripting non-lvalue array");
2981 /* Note in C++ it is valid to subscript a `register' array, since
2982 it is valid to take the address of something with that
2983 storage specification. */
2984 if (extra_warnings)
2986 tree foo = array;
2987 while (TREE_CODE (foo) == COMPONENT_REF)
2988 foo = TREE_OPERAND (foo, 0);
2989 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
2990 && (complain & tf_warning))
2991 warning_at (loc, OPT_Wextra,
2992 "subscripting array declared %<register%>");
2995 type = TREE_TYPE (TREE_TYPE (array));
2996 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2997 /* Array ref is const/volatile if the array elements are
2998 or if the array is.. */
2999 TREE_READONLY (rval)
3000 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3001 TREE_SIDE_EFFECTS (rval)
3002 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3003 TREE_THIS_VOLATILE (rval)
3004 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3005 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3006 complain);
3007 protected_set_expr_location (ret, loc);
3008 return ret;
3012 tree ar = cp_default_conversion (array, complain);
3013 tree ind = cp_default_conversion (idx, complain);
3015 /* Put the integer in IND to simplify error checking. */
3016 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3018 tree temp = ar;
3019 ar = ind;
3020 ind = temp;
3023 if (ar == error_mark_node || ind == error_mark_node)
3024 return error_mark_node;
3026 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
3028 if (complain & tf_error)
3029 error_at (loc, "subscripted value is neither array nor pointer");
3030 return error_mark_node;
3032 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3034 if (complain & tf_error)
3035 error_at (loc, "array subscript is not an integer");
3036 return error_mark_node;
3039 warn_array_subscript_with_type_char (idx);
3041 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3042 PLUS_EXPR, ar, ind,
3043 complain),
3044 RO_ARRAY_INDEXING,
3045 complain);
3046 protected_set_expr_location (ret, loc);
3047 return ret;
3051 /* Entry point for Obj-C++. */
3053 tree
3054 build_array_ref (location_t loc, tree array, tree idx)
3056 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3059 /* Resolve a pointer to member function. INSTANCE is the object
3060 instance to use, if the member points to a virtual member.
3062 This used to avoid checking for virtual functions if basetype
3063 has no virtual functions, according to an earlier ANSI draft.
3064 With the final ISO C++ rules, such an optimization is
3065 incorrect: A pointer to a derived member can be static_cast
3066 to pointer-to-base-member, as long as the dynamic object
3067 later has the right member. */
3069 tree
3070 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3071 tsubst_flags_t complain)
3073 if (TREE_CODE (function) == OFFSET_REF)
3074 function = TREE_OPERAND (function, 1);
3076 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3078 tree idx, delta, e1, e2, e3, vtbl, basetype;
3079 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3081 tree instance_ptr = *instance_ptrptr;
3082 tree instance_save_expr = 0;
3083 if (instance_ptr == error_mark_node)
3085 if (TREE_CODE (function) == PTRMEM_CST)
3087 /* Extracting the function address from a pmf is only
3088 allowed with -Wno-pmf-conversions. It only works for
3089 pmf constants. */
3090 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3091 e1 = convert (fntype, e1);
3092 return e1;
3094 else
3096 if (complain & tf_error)
3097 error ("object missing in use of %qE", function);
3098 return error_mark_node;
3102 if (TREE_SIDE_EFFECTS (instance_ptr))
3103 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3105 if (TREE_SIDE_EFFECTS (function))
3106 function = save_expr (function);
3108 /* Start by extracting all the information from the PMF itself. */
3109 e3 = pfn_from_ptrmemfunc (function);
3110 delta = delta_from_ptrmemfunc (function);
3111 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3112 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3114 case ptrmemfunc_vbit_in_pfn:
3115 e1 = cp_build_binary_op (input_location,
3116 BIT_AND_EXPR, idx, integer_one_node,
3117 complain);
3118 idx = cp_build_binary_op (input_location,
3119 MINUS_EXPR, idx, integer_one_node,
3120 complain);
3121 if (idx == error_mark_node)
3122 return error_mark_node;
3123 break;
3125 case ptrmemfunc_vbit_in_delta:
3126 e1 = cp_build_binary_op (input_location,
3127 BIT_AND_EXPR, delta, integer_one_node,
3128 complain);
3129 delta = cp_build_binary_op (input_location,
3130 RSHIFT_EXPR, delta, integer_one_node,
3131 complain);
3132 if (delta == error_mark_node)
3133 return error_mark_node;
3134 break;
3136 default:
3137 gcc_unreachable ();
3140 if (e1 == error_mark_node)
3141 return error_mark_node;
3143 /* Convert down to the right base before using the instance. A
3144 special case is that in a pointer to member of class C, C may
3145 be incomplete. In that case, the function will of course be
3146 a member of C, and no conversion is required. In fact,
3147 lookup_base will fail in that case, because incomplete
3148 classes do not have BINFOs. */
3149 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3150 if (!same_type_ignoring_top_level_qualifiers_p
3151 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3153 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3154 basetype, ba_check, NULL);
3155 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3156 1, complain);
3157 if (instance_ptr == error_mark_node)
3158 return error_mark_node;
3160 /* ...and then the delta in the PMF. */
3161 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3163 /* Hand back the adjusted 'this' argument to our caller. */
3164 *instance_ptrptr = instance_ptr;
3166 /* Next extract the vtable pointer from the object. */
3167 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3168 instance_ptr);
3169 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3170 if (vtbl == error_mark_node)
3171 return error_mark_node;
3173 /* If the object is not dynamic the access invokes undefined
3174 behavior. As it is not executed in this case silence the
3175 spurious warnings it may provoke. */
3176 TREE_NO_WARNING (vtbl) = 1;
3178 /* Finally, extract the function pointer from the vtable. */
3179 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3180 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3181 if (e2 == error_mark_node)
3182 return error_mark_node;
3183 TREE_CONSTANT (e2) = 1;
3185 /* When using function descriptors, the address of the
3186 vtable entry is treated as a function pointer. */
3187 if (TARGET_VTABLE_USES_DESCRIPTORS)
3188 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3189 cp_build_addr_expr (e2, complain));
3191 e2 = fold_convert (TREE_TYPE (e3), e2);
3192 e1 = build_conditional_expr (e1, e2, e3, complain);
3193 if (e1 == error_mark_node)
3194 return error_mark_node;
3196 /* Make sure this doesn't get evaluated first inside one of the
3197 branches of the COND_EXPR. */
3198 if (instance_save_expr)
3199 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3200 instance_save_expr, e1);
3202 function = e1;
3204 return function;
3207 /* Used by the C-common bits. */
3208 tree
3209 build_function_call (location_t loc ATTRIBUTE_UNUSED,
3210 tree function, tree params)
3212 return cp_build_function_call (function, params, tf_warning_or_error);
3215 /* Used by the C-common bits. */
3216 tree
3217 build_function_call_vec (location_t loc ATTRIBUTE_UNUSED,
3218 tree function, VEC(tree,gc) *params,
3219 VEC(tree,gc) *origtypes ATTRIBUTE_UNUSED)
3221 VEC(tree,gc) *orig_params = params;
3222 tree ret = cp_build_function_call_vec (function, &params,
3223 tf_warning_or_error);
3225 /* cp_build_function_call_vec can reallocate PARAMS by adding
3226 default arguments. That should never happen here. Verify
3227 that. */
3228 gcc_assert (params == orig_params);
3230 return ret;
3233 /* Build a function call using a tree list of arguments. */
3235 tree
3236 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3238 VEC(tree,gc) *vec;
3239 tree ret;
3241 vec = make_tree_vector ();
3242 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3243 VEC_safe_push (tree, gc, vec, TREE_VALUE (params));
3244 ret = cp_build_function_call_vec (function, &vec, complain);
3245 release_tree_vector (vec);
3246 return ret;
3249 /* Build a function call using varargs. */
3251 tree
3252 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3254 VEC(tree,gc) *vec;
3255 va_list args;
3256 tree ret, t;
3258 vec = make_tree_vector ();
3259 va_start (args, complain);
3260 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3261 VEC_safe_push (tree, gc, vec, t);
3262 va_end (args);
3263 ret = cp_build_function_call_vec (function, &vec, complain);
3264 release_tree_vector (vec);
3265 return ret;
3268 /* Build a function call using a vector of arguments. PARAMS may be
3269 NULL if there are no parameters. This changes the contents of
3270 PARAMS. */
3272 tree
3273 cp_build_function_call_vec (tree function, VEC(tree,gc) **params,
3274 tsubst_flags_t complain)
3276 tree fntype, fndecl;
3277 int is_method;
3278 tree original = function;
3279 int nargs;
3280 tree *argarray;
3281 tree parm_types;
3282 VEC(tree,gc) *allocated = NULL;
3283 tree ret;
3285 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3286 expressions, like those used for ObjC messenger dispatches. */
3287 if (params != NULL && !VEC_empty (tree, *params))
3288 function = objc_rewrite_function_call (function,
3289 VEC_index (tree, *params, 0));
3291 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3292 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3293 if (TREE_CODE (function) == NOP_EXPR
3294 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3295 function = TREE_OPERAND (function, 0);
3297 if (TREE_CODE (function) == FUNCTION_DECL)
3299 mark_used (function);
3300 fndecl = function;
3302 /* Convert anything with function type to a pointer-to-function. */
3303 if (DECL_MAIN_P (function) && (complain & tf_error))
3304 pedwarn (input_location, OPT_Wpedantic,
3305 "ISO C++ forbids calling %<::main%> from within program");
3307 function = build_addr_func (function, complain);
3309 else
3311 fndecl = NULL_TREE;
3313 function = build_addr_func (function, complain);
3316 if (function == error_mark_node)
3317 return error_mark_node;
3319 fntype = TREE_TYPE (function);
3321 if (TYPE_PTRMEMFUNC_P (fntype))
3323 if (complain & tf_error)
3324 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3325 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3326 original, original);
3327 return error_mark_node;
3330 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3331 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3333 if (!((TREE_CODE (fntype) == POINTER_TYPE
3334 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3335 || is_method
3336 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3338 if (complain & tf_error)
3340 if (!flag_diagnostics_show_caret)
3341 error_at (input_location,
3342 "%qE cannot be used as a function", original);
3343 else if (DECL_P (original))
3344 error_at (input_location,
3345 "%qD cannot be used as a function", original);
3346 else
3347 error_at (input_location,
3348 "expression cannot be used as a function");
3351 return error_mark_node;
3354 /* fntype now gets the type of function pointed to. */
3355 fntype = TREE_TYPE (fntype);
3356 parm_types = TYPE_ARG_TYPES (fntype);
3358 if (params == NULL)
3360 allocated = make_tree_vector ();
3361 params = &allocated;
3364 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3365 complain);
3366 if (nargs < 0)
3367 return error_mark_node;
3369 argarray = VEC_address (tree, *params);
3371 /* Check for errors in format strings and inappropriately
3372 null parameters. */
3373 check_function_arguments (fntype, nargs, argarray);
3375 ret = build_cxx_call (function, nargs, argarray);
3377 if (allocated != NULL)
3378 release_tree_vector (allocated);
3380 return ret;
3383 /* Subroutine of convert_arguments.
3384 Warn about wrong number of args are genereted. */
3386 static void
3387 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3389 if (fndecl)
3391 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3393 if (DECL_NAME (fndecl) == NULL_TREE
3394 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3395 error_at (loc,
3396 too_many_p
3397 ? G_("too many arguments to constructor %q#D")
3398 : G_("too few arguments to constructor %q#D"),
3399 fndecl);
3400 else
3401 error_at (loc,
3402 too_many_p
3403 ? G_("too many arguments to member function %q#D")
3404 : G_("too few arguments to member function %q#D"),
3405 fndecl);
3407 else
3408 error_at (loc,
3409 too_many_p
3410 ? G_("too many arguments to function %q#D")
3411 : G_("too few arguments to function %q#D"),
3412 fndecl);
3413 inform (DECL_SOURCE_LOCATION (fndecl),
3414 "declared here");
3416 else
3418 if (c_dialect_objc () && objc_message_selector ())
3419 error_at (loc,
3420 too_many_p
3421 ? G_("too many arguments to method %q#D")
3422 : G_("too few arguments to method %q#D"),
3423 objc_message_selector ());
3424 else
3425 error_at (loc, too_many_p ? G_("too many arguments to function")
3426 : G_("too few arguments to function"));
3430 /* Convert the actual parameter expressions in the list VALUES to the
3431 types in the list TYPELIST. The converted expressions are stored
3432 back in the VALUES vector.
3433 If parmdecls is exhausted, or when an element has NULL as its type,
3434 perform the default conversions.
3436 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3438 This is also where warnings about wrong number of args are generated.
3440 Returns the actual number of arguments processed (which might be less
3441 than the length of the vector), or -1 on error.
3443 In C++, unspecified trailing parameters can be filled in with their
3444 default arguments, if such were specified. Do so here. */
3446 static int
3447 convert_arguments (tree typelist, VEC(tree,gc) **values, tree fndecl,
3448 int flags, tsubst_flags_t complain)
3450 tree typetail;
3451 unsigned int i;
3453 /* Argument passing is always copy-initialization. */
3454 flags |= LOOKUP_ONLYCONVERTING;
3456 for (i = 0, typetail = typelist;
3457 i < VEC_length (tree, *values);
3458 i++)
3460 tree type = typetail ? TREE_VALUE (typetail) : 0;
3461 tree val = VEC_index (tree, *values, i);
3463 if (val == error_mark_node || type == error_mark_node)
3464 return -1;
3466 if (type == void_type_node)
3468 if (complain & tf_error)
3470 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3471 return i;
3473 else
3474 return -1;
3477 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3478 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3479 if (TREE_CODE (val) == NOP_EXPR
3480 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3481 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3482 val = TREE_OPERAND (val, 0);
3484 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3486 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3487 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3488 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3489 val = decay_conversion (val, complain);
3492 if (val == error_mark_node)
3493 return -1;
3495 if (type != 0)
3497 /* Formal parm type is specified by a function prototype. */
3498 tree parmval;
3500 if (!COMPLETE_TYPE_P (complete_type (type)))
3502 if (complain & tf_error)
3504 if (fndecl)
3505 error ("parameter %P of %qD has incomplete type %qT",
3506 i, fndecl, type);
3507 else
3508 error ("parameter %P has incomplete type %qT", i, type);
3510 parmval = error_mark_node;
3512 else
3514 parmval = convert_for_initialization
3515 (NULL_TREE, type, val, flags,
3516 ICR_ARGPASS, fndecl, i, complain);
3517 parmval = convert_for_arg_passing (type, parmval, complain);
3520 if (parmval == error_mark_node)
3521 return -1;
3523 VEC_replace (tree, *values, i, parmval);
3525 else
3527 if (fndecl && DECL_BUILT_IN (fndecl)
3528 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3529 /* Don't do ellipsis conversion for __built_in_constant_p
3530 as this will result in spurious errors for non-trivial
3531 types. */
3532 val = require_complete_type_sfinae (val, complain);
3533 else
3534 val = convert_arg_to_ellipsis (val, complain);
3536 VEC_replace (tree, *values, i, val);
3539 if (typetail)
3540 typetail = TREE_CHAIN (typetail);
3543 if (typetail != 0 && typetail != void_list_node)
3545 /* See if there are default arguments that can be used. Because
3546 we hold default arguments in the FUNCTION_TYPE (which is so
3547 wrong), we can see default parameters here from deduced
3548 contexts (and via typeof) for indirect function calls.
3549 Fortunately we know whether we have a function decl to
3550 provide default arguments in a language conformant
3551 manner. */
3552 if (fndecl && TREE_PURPOSE (typetail)
3553 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3555 for (; typetail != void_list_node; ++i)
3557 tree parmval
3558 = convert_default_arg (TREE_VALUE (typetail),
3559 TREE_PURPOSE (typetail),
3560 fndecl, i, complain);
3562 if (parmval == error_mark_node)
3563 return -1;
3565 VEC_safe_push (tree, gc, *values, parmval);
3566 typetail = TREE_CHAIN (typetail);
3567 /* ends with `...'. */
3568 if (typetail == NULL_TREE)
3569 break;
3572 else
3574 if (complain & tf_error)
3575 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3576 return -1;
3580 return (int) i;
3583 /* Build a binary-operation expression, after performing default
3584 conversions on the operands. CODE is the kind of expression to
3585 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3586 are the tree codes which correspond to ARG1 and ARG2 when issuing
3587 warnings about possibly misplaced parentheses. They may differ
3588 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3589 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3590 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3591 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3592 ARG2_CODE as ERROR_MARK. */
3594 tree
3595 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3596 enum tree_code arg1_code, tree arg2,
3597 enum tree_code arg2_code, tree *overload,
3598 tsubst_flags_t complain)
3600 tree orig_arg1;
3601 tree orig_arg2;
3602 tree expr;
3604 orig_arg1 = arg1;
3605 orig_arg2 = arg2;
3607 if (processing_template_decl)
3609 if (type_dependent_expression_p (arg1)
3610 || type_dependent_expression_p (arg2))
3611 return build_min_nt_loc (loc, code, arg1, arg2);
3612 arg1 = build_non_dependent_expr (arg1);
3613 arg2 = build_non_dependent_expr (arg2);
3616 if (code == DOTSTAR_EXPR)
3617 expr = build_m_component_ref (arg1, arg2, complain);
3618 else
3619 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3620 overload, complain);
3622 /* Check for cases such as x+y<<z which users are likely to
3623 misinterpret. But don't warn about obj << x + y, since that is a
3624 common idiom for I/O. */
3625 if (warn_parentheses
3626 && (complain & tf_warning)
3627 && !processing_template_decl
3628 && !error_operand_p (arg1)
3629 && !error_operand_p (arg2)
3630 && (code != LSHIFT_EXPR
3631 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3632 warn_about_parentheses (code, arg1_code, orig_arg1, arg2_code, orig_arg2);
3634 if (processing_template_decl && expr != error_mark_node)
3635 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3637 return expr;
3640 /* Build and return an ARRAY_REF expression. */
3642 tree
3643 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3644 tsubst_flags_t complain)
3646 tree orig_arg1 = arg1;
3647 tree orig_arg2 = arg2;
3648 tree expr;
3650 if (processing_template_decl)
3652 if (type_dependent_expression_p (arg1)
3653 || type_dependent_expression_p (arg2))
3654 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3655 NULL_TREE, NULL_TREE);
3656 arg1 = build_non_dependent_expr (arg1);
3657 arg2 = build_non_dependent_expr (arg2);
3660 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3661 NULL_TREE, /*overload=*/NULL, complain);
3663 if (processing_template_decl && expr != error_mark_node)
3664 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3665 NULL_TREE, NULL_TREE);
3666 return expr;
3669 /* Return whether OP is an expression of enum type cast to integer
3670 type. In C++ even unsigned enum types are cast to signed integer
3671 types. We do not want to issue warnings about comparisons between
3672 signed and unsigned types when one of the types is an enum type.
3673 Those warnings are always false positives in practice. */
3675 static bool
3676 enum_cast_to_int (tree op)
3678 if (TREE_CODE (op) == NOP_EXPR
3679 && TREE_TYPE (op) == integer_type_node
3680 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3681 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3682 return true;
3684 /* The cast may have been pushed into a COND_EXPR. */
3685 if (TREE_CODE (op) == COND_EXPR)
3686 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3687 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3689 return false;
3692 /* For the c-common bits. */
3693 tree
3694 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3695 int convert_p ATTRIBUTE_UNUSED)
3697 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3701 /* Build a binary-operation expression without default conversions.
3702 CODE is the kind of expression to build.
3703 LOCATION is the location_t of the operator in the source code.
3704 This function differs from `build' in several ways:
3705 the data type of the result is computed and recorded in it,
3706 warnings are generated if arg data types are invalid,
3707 special handling for addition and subtraction of pointers is known,
3708 and some optimization is done (operations on narrow ints
3709 are done in the narrower type when that gives the same result).
3710 Constant folding is also done before the result is returned.
3712 Note that the operands will never have enumeral types
3713 because either they have just had the default conversions performed
3714 or they have both just been converted to some other type in which
3715 the arithmetic is to be done.
3717 C++: must do special pointer arithmetic when implementing
3718 multiple inheritance, and deal with pointer to member functions. */
3720 tree
3721 cp_build_binary_op (location_t location,
3722 enum tree_code code, tree orig_op0, tree orig_op1,
3723 tsubst_flags_t complain)
3725 tree op0, op1;
3726 enum tree_code code0, code1;
3727 tree type0, type1;
3728 const char *invalid_op_diag;
3730 /* Expression code to give to the expression when it is built.
3731 Normally this is CODE, which is what the caller asked for,
3732 but in some special cases we change it. */
3733 enum tree_code resultcode = code;
3735 /* Data type in which the computation is to be performed.
3736 In the simplest cases this is the common type of the arguments. */
3737 tree result_type = NULL;
3739 /* Nonzero means operands have already been type-converted
3740 in whatever way is necessary.
3741 Zero means they need to be converted to RESULT_TYPE. */
3742 int converted = 0;
3744 /* Nonzero means create the expression with this type, rather than
3745 RESULT_TYPE. */
3746 tree build_type = 0;
3748 /* Nonzero means after finally constructing the expression
3749 convert it to this type. */
3750 tree final_type = 0;
3752 tree result;
3754 /* Nonzero if this is an operation like MIN or MAX which can
3755 safely be computed in short if both args are promoted shorts.
3756 Also implies COMMON.
3757 -1 indicates a bitwise operation; this makes a difference
3758 in the exact conditions for when it is safe to do the operation
3759 in a narrower mode. */
3760 int shorten = 0;
3762 /* Nonzero if this is a comparison operation;
3763 if both args are promoted shorts, compare the original shorts.
3764 Also implies COMMON. */
3765 int short_compare = 0;
3767 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3768 int common = 0;
3770 /* True if both operands have arithmetic type. */
3771 bool arithmetic_types_p;
3773 /* Apply default conversions. */
3774 op0 = orig_op0;
3775 op1 = orig_op1;
3777 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3778 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3779 || code == TRUTH_XOR_EXPR)
3781 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3782 op0 = decay_conversion (op0, complain);
3783 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3784 op1 = decay_conversion (op1, complain);
3786 else
3788 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3789 op0 = cp_default_conversion (op0, complain);
3790 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3791 op1 = cp_default_conversion (op1, complain);
3794 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3795 STRIP_TYPE_NOPS (op0);
3796 STRIP_TYPE_NOPS (op1);
3798 /* DTRT if one side is an overloaded function, but complain about it. */
3799 if (type_unknown_p (op0))
3801 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3802 if (t != error_mark_node)
3804 if (complain & tf_error)
3805 permerror (input_location, "assuming cast to type %qT from overloaded function",
3806 TREE_TYPE (t));
3807 op0 = t;
3810 if (type_unknown_p (op1))
3812 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3813 if (t != error_mark_node)
3815 if (complain & tf_error)
3816 permerror (input_location, "assuming cast to type %qT from overloaded function",
3817 TREE_TYPE (t));
3818 op1 = t;
3822 type0 = TREE_TYPE (op0);
3823 type1 = TREE_TYPE (op1);
3825 /* The expression codes of the data types of the arguments tell us
3826 whether the arguments are integers, floating, pointers, etc. */
3827 code0 = TREE_CODE (type0);
3828 code1 = TREE_CODE (type1);
3830 /* If an error was already reported for one of the arguments,
3831 avoid reporting another error. */
3832 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3833 return error_mark_node;
3835 if ((invalid_op_diag
3836 = targetm.invalid_binary_op (code, type0, type1)))
3838 error (invalid_op_diag);
3839 return error_mark_node;
3842 /* Issue warnings about peculiar, but valid, uses of NULL. */
3843 if ((orig_op0 == null_node || orig_op1 == null_node)
3844 /* It's reasonable to use pointer values as operands of &&
3845 and ||, so NULL is no exception. */
3846 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3847 && ( /* Both are NULL (or 0) and the operation was not a
3848 comparison or a pointer subtraction. */
3849 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3850 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3851 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3852 || (!null_ptr_cst_p (orig_op0)
3853 && !TYPE_PTR_OR_PTRMEM_P (type0))
3854 || (!null_ptr_cst_p (orig_op1)
3855 && !TYPE_PTR_OR_PTRMEM_P (type1)))
3856 && (complain & tf_warning))
3858 source_location loc =
3859 expansion_point_location_if_in_system_header (input_location);
3861 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
3864 switch (code)
3866 case MINUS_EXPR:
3867 /* Subtraction of two similar pointers.
3868 We must subtract them as integers, then divide by object size. */
3869 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3870 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3871 TREE_TYPE (type1)))
3872 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
3873 complain);
3874 /* In all other cases except pointer - int, the usual arithmetic
3875 rules apply. */
3876 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3878 common = 1;
3879 break;
3881 /* The pointer - int case is just like pointer + int; fall
3882 through. */
3883 case PLUS_EXPR:
3884 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3885 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3887 tree ptr_operand;
3888 tree int_operand;
3889 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3890 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3891 if (processing_template_decl)
3893 result_type = TREE_TYPE (ptr_operand);
3894 break;
3896 return cp_pointer_int_sum (code,
3897 ptr_operand,
3898 int_operand);
3900 common = 1;
3901 break;
3903 case MULT_EXPR:
3904 common = 1;
3905 break;
3907 case TRUNC_DIV_EXPR:
3908 case CEIL_DIV_EXPR:
3909 case FLOOR_DIV_EXPR:
3910 case ROUND_DIV_EXPR:
3911 case EXACT_DIV_EXPR:
3912 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3913 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3914 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3915 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
3917 enum tree_code tcode0 = code0, tcode1 = code1;
3919 warn_for_div_by_zero (location, op1);
3921 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
3922 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3923 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
3924 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3926 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
3927 resultcode = RDIV_EXPR;
3928 else
3929 /* When dividing two signed integers, we have to promote to int.
3930 unless we divide by a constant != -1. Note that default
3931 conversion will have been performed on the operands at this
3932 point, so we have to dig out the original type to find out if
3933 it was unsigned. */
3934 shorten = ((TREE_CODE (op0) == NOP_EXPR
3935 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3936 || (TREE_CODE (op1) == INTEGER_CST
3937 && ! integer_all_onesp (op1)));
3939 common = 1;
3941 break;
3943 case BIT_AND_EXPR:
3944 case BIT_IOR_EXPR:
3945 case BIT_XOR_EXPR:
3946 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3947 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3948 && !VECTOR_FLOAT_TYPE_P (type0)
3949 && !VECTOR_FLOAT_TYPE_P (type1)))
3950 shorten = -1;
3951 break;
3953 case TRUNC_MOD_EXPR:
3954 case FLOOR_MOD_EXPR:
3955 warn_for_div_by_zero (location, op1);
3957 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3958 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
3959 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
3960 common = 1;
3961 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3963 /* Although it would be tempting to shorten always here, that loses
3964 on some targets, since the modulo instruction is undefined if the
3965 quotient can't be represented in the computation mode. We shorten
3966 only if unsigned or if dividing by something we know != -1. */
3967 shorten = ((TREE_CODE (op0) == NOP_EXPR
3968 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3969 || (TREE_CODE (op1) == INTEGER_CST
3970 && ! integer_all_onesp (op1)));
3971 common = 1;
3973 break;
3975 case TRUTH_ANDIF_EXPR:
3976 case TRUTH_ORIF_EXPR:
3977 case TRUTH_AND_EXPR:
3978 case TRUTH_OR_EXPR:
3979 result_type = boolean_type_node;
3980 break;
3982 /* Shift operations: result has same type as first operand;
3983 always convert second operand to int.
3984 Also set SHORT_SHIFT if shifting rightward. */
3986 case RSHIFT_EXPR:
3987 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3989 result_type = type0;
3990 if (TREE_CODE (op1) == INTEGER_CST)
3992 if (tree_int_cst_lt (op1, integer_zero_node))
3994 if ((complain & tf_warning)
3995 && c_inhibit_evaluation_warnings == 0)
3996 warning (0, "right shift count is negative");
3998 else
4000 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0
4001 && (complain & tf_warning)
4002 && c_inhibit_evaluation_warnings == 0)
4003 warning (0, "right shift count >= width of type");
4006 /* Convert the shift-count to an integer, regardless of
4007 size of value being shifted. */
4008 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4009 op1 = cp_convert (integer_type_node, op1, complain);
4010 /* Avoid converting op1 to result_type later. */
4011 converted = 1;
4013 break;
4015 case LSHIFT_EXPR:
4016 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4018 result_type = type0;
4019 if (TREE_CODE (op1) == INTEGER_CST)
4021 if (tree_int_cst_lt (op1, integer_zero_node))
4023 if ((complain & tf_warning)
4024 && c_inhibit_evaluation_warnings == 0)
4025 warning (0, "left shift count is negative");
4027 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4029 if ((complain & tf_warning)
4030 && c_inhibit_evaluation_warnings == 0)
4031 warning (0, "left shift count >= width of type");
4034 /* Convert the shift-count to an integer, regardless of
4035 size of value being shifted. */
4036 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4037 op1 = cp_convert (integer_type_node, op1, complain);
4038 /* Avoid converting op1 to result_type later. */
4039 converted = 1;
4041 break;
4043 case RROTATE_EXPR:
4044 case LROTATE_EXPR:
4045 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4047 result_type = type0;
4048 if (TREE_CODE (op1) == INTEGER_CST)
4050 if (tree_int_cst_lt (op1, integer_zero_node))
4052 if (complain & tf_warning)
4053 warning (0, (code == LROTATE_EXPR)
4054 ? G_("left rotate count is negative")
4055 : G_("right rotate count is negative"));
4057 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4059 if (complain & tf_warning)
4060 warning (0, (code == LROTATE_EXPR)
4061 ? G_("left rotate count >= width of type")
4062 : G_("right rotate count >= width of type"));
4065 /* Convert the shift-count to an integer, regardless of
4066 size of value being shifted. */
4067 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4068 op1 = cp_convert (integer_type_node, op1, complain);
4070 break;
4072 case EQ_EXPR:
4073 case NE_EXPR:
4074 if ((complain & tf_warning)
4075 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4076 warning (OPT_Wfloat_equal,
4077 "comparing floating point with == or != is unsafe");
4078 if ((complain & tf_warning)
4079 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4080 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4081 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4083 build_type = boolean_type_node;
4084 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4085 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4086 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4087 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4088 short_compare = 1;
4089 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4090 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4091 result_type = composite_pointer_type (type0, type1, op0, op1,
4092 CPO_COMPARISON, complain);
4093 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4094 && null_ptr_cst_p (op1))
4096 if (TREE_CODE (op0) == ADDR_EXPR
4097 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4099 if ((complain & tf_warning)
4100 && c_inhibit_evaluation_warnings == 0)
4101 warning (OPT_Waddress, "the address of %qD will never be NULL",
4102 TREE_OPERAND (op0, 0));
4104 result_type = type0;
4106 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4107 && null_ptr_cst_p (op0))
4109 if (TREE_CODE (op1) == ADDR_EXPR
4110 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4112 if ((complain & tf_warning)
4113 && c_inhibit_evaluation_warnings == 0)
4114 warning (OPT_Waddress, "the address of %qD will never be NULL",
4115 TREE_OPERAND (op1, 0));
4117 result_type = type1;
4119 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4120 /* One of the operands must be of nullptr_t type. */
4121 result_type = TREE_TYPE (nullptr_node);
4122 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4124 result_type = type0;
4125 if (complain & tf_error)
4126 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4127 else
4128 return error_mark_node;
4130 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4132 result_type = type1;
4133 if (complain & tf_error)
4134 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4135 else
4136 return error_mark_node;
4138 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4140 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4141 == ptrmemfunc_vbit_in_delta)
4143 tree pfn0 = pfn_from_ptrmemfunc (op0);
4144 tree delta0 = delta_from_ptrmemfunc (op0);
4145 tree e1 = cp_build_binary_op (location,
4146 EQ_EXPR,
4147 pfn0,
4148 build_zero_cst (TREE_TYPE (pfn0)),
4149 complain);
4150 tree e2 = cp_build_binary_op (location,
4151 BIT_AND_EXPR,
4152 delta0,
4153 integer_one_node,
4154 complain);
4156 if ((complain & tf_warning)
4157 && c_inhibit_evaluation_warnings == 0
4158 && !NULLPTR_TYPE_P (TREE_TYPE (op1)))
4159 warning (OPT_Wzero_as_null_pointer_constant,
4160 "zero as null pointer constant");
4162 e2 = cp_build_binary_op (location,
4163 EQ_EXPR, e2, integer_zero_node,
4164 complain);
4165 op0 = cp_build_binary_op (location,
4166 TRUTH_ANDIF_EXPR, e1, e2,
4167 complain);
4168 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4170 else
4172 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4173 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4175 result_type = TREE_TYPE (op0);
4177 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4178 return cp_build_binary_op (location, code, op1, op0, complain);
4179 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4181 tree type;
4182 /* E will be the final comparison. */
4183 tree e;
4184 /* E1 and E2 are for scratch. */
4185 tree e1;
4186 tree e2;
4187 tree pfn0;
4188 tree pfn1;
4189 tree delta0;
4190 tree delta1;
4192 type = composite_pointer_type (type0, type1, op0, op1,
4193 CPO_COMPARISON, complain);
4195 if (!same_type_p (TREE_TYPE (op0), type))
4196 op0 = cp_convert_and_check (type, op0, complain);
4197 if (!same_type_p (TREE_TYPE (op1), type))
4198 op1 = cp_convert_and_check (type, op1, complain);
4200 if (op0 == error_mark_node || op1 == error_mark_node)
4201 return error_mark_node;
4203 if (TREE_SIDE_EFFECTS (op0))
4204 op0 = save_expr (op0);
4205 if (TREE_SIDE_EFFECTS (op1))
4206 op1 = save_expr (op1);
4208 pfn0 = pfn_from_ptrmemfunc (op0);
4209 pfn1 = pfn_from_ptrmemfunc (op1);
4210 delta0 = delta_from_ptrmemfunc (op0);
4211 delta1 = delta_from_ptrmemfunc (op1);
4212 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4213 == ptrmemfunc_vbit_in_delta)
4215 /* We generate:
4217 (op0.pfn == op1.pfn
4218 && ((op0.delta == op1.delta)
4219 || (!op0.pfn && op0.delta & 1 == 0
4220 && op1.delta & 1 == 0))
4222 The reason for the `!op0.pfn' bit is that a NULL
4223 pointer-to-member is any member with a zero PFN and
4224 LSB of the DELTA field is 0. */
4226 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4227 delta0,
4228 integer_one_node,
4229 complain);
4230 e1 = cp_build_binary_op (location,
4231 EQ_EXPR, e1, integer_zero_node,
4232 complain);
4233 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4234 delta1,
4235 integer_one_node,
4236 complain);
4237 e2 = cp_build_binary_op (location,
4238 EQ_EXPR, e2, integer_zero_node,
4239 complain);
4240 e1 = cp_build_binary_op (location,
4241 TRUTH_ANDIF_EXPR, e2, e1,
4242 complain);
4243 e2 = cp_build_binary_op (location, EQ_EXPR,
4244 pfn0,
4245 build_zero_cst (TREE_TYPE (pfn0)),
4246 complain);
4247 e2 = cp_build_binary_op (location,
4248 TRUTH_ANDIF_EXPR, e2, e1, complain);
4249 e1 = cp_build_binary_op (location,
4250 EQ_EXPR, delta0, delta1, complain);
4251 e1 = cp_build_binary_op (location,
4252 TRUTH_ORIF_EXPR, e1, e2, complain);
4254 else
4256 /* We generate:
4258 (op0.pfn == op1.pfn
4259 && (!op0.pfn || op0.delta == op1.delta))
4261 The reason for the `!op0.pfn' bit is that a NULL
4262 pointer-to-member is any member with a zero PFN; the
4263 DELTA field is unspecified. */
4265 e1 = cp_build_binary_op (location,
4266 EQ_EXPR, delta0, delta1, complain);
4267 e2 = cp_build_binary_op (location,
4268 EQ_EXPR,
4269 pfn0,
4270 build_zero_cst (TREE_TYPE (pfn0)),
4271 complain);
4272 e1 = cp_build_binary_op (location,
4273 TRUTH_ORIF_EXPR, e1, e2, complain);
4275 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4276 e = cp_build_binary_op (location,
4277 TRUTH_ANDIF_EXPR, e2, e1, complain);
4278 if (code == EQ_EXPR)
4279 return e;
4280 return cp_build_binary_op (location,
4281 EQ_EXPR, e, integer_zero_node, complain);
4283 else
4285 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4286 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4287 type1));
4288 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4289 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4290 type0));
4293 break;
4295 case MAX_EXPR:
4296 case MIN_EXPR:
4297 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4298 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4299 shorten = 1;
4300 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4301 result_type = composite_pointer_type (type0, type1, op0, op1,
4302 CPO_COMPARISON, complain);
4303 break;
4305 case LE_EXPR:
4306 case GE_EXPR:
4307 case LT_EXPR:
4308 case GT_EXPR:
4309 if (TREE_CODE (orig_op0) == STRING_CST
4310 || TREE_CODE (orig_op1) == STRING_CST)
4312 if (complain & tf_warning)
4313 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4316 build_type = boolean_type_node;
4317 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4318 || code0 == ENUMERAL_TYPE)
4319 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4320 || code1 == ENUMERAL_TYPE))
4321 short_compare = 1;
4322 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4323 result_type = composite_pointer_type (type0, type1, op0, op1,
4324 CPO_COMPARISON, complain);
4325 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4327 result_type = type0;
4328 if (extra_warnings && (complain & tf_warning))
4329 warning (OPT_Wextra,
4330 "ordered comparison of pointer with integer zero");
4332 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4334 result_type = type1;
4335 if (extra_warnings && (complain & tf_warning))
4336 warning (OPT_Wextra,
4337 "ordered comparison of pointer with integer zero");
4339 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4340 /* One of the operands must be of nullptr_t type. */
4341 result_type = TREE_TYPE (nullptr_node);
4342 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4344 result_type = type0;
4345 if (complain & tf_error)
4346 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4347 else
4348 return error_mark_node;
4350 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4352 result_type = type1;
4353 if (complain & tf_error)
4354 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4355 else
4356 return error_mark_node;
4358 break;
4360 case UNORDERED_EXPR:
4361 case ORDERED_EXPR:
4362 case UNLT_EXPR:
4363 case UNLE_EXPR:
4364 case UNGT_EXPR:
4365 case UNGE_EXPR:
4366 case UNEQ_EXPR:
4367 build_type = integer_type_node;
4368 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4370 if (complain & tf_error)
4371 error ("unordered comparison on non-floating point argument");
4372 return error_mark_node;
4374 common = 1;
4375 break;
4377 default:
4378 break;
4381 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4382 || code0 == ENUMERAL_TYPE)
4383 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4384 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4385 arithmetic_types_p = 1;
4386 else
4388 arithmetic_types_p = 0;
4389 /* Vector arithmetic is only allowed when both sides are vectors. */
4390 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4392 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4393 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4394 TREE_TYPE (type1)))
4396 binary_op_error (location, code, type0, type1);
4397 return error_mark_node;
4399 arithmetic_types_p = 1;
4402 /* Determine the RESULT_TYPE, if it is not already known. */
4403 if (!result_type
4404 && arithmetic_types_p
4405 && (shorten || common || short_compare))
4407 result_type = cp_common_type (type0, type1);
4408 do_warn_double_promotion (result_type, type0, type1,
4409 "implicit conversion from %qT to %qT "
4410 "to match other operand of binary "
4411 "expression",
4412 location);
4415 if (!result_type)
4417 if (complain & tf_error)
4418 error ("invalid operands of types %qT and %qT to binary %qO",
4419 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4420 return error_mark_node;
4423 /* If we're in a template, the only thing we need to know is the
4424 RESULT_TYPE. */
4425 if (processing_template_decl)
4427 /* Since the middle-end checks the type when doing a build2, we
4428 need to build the tree in pieces. This built tree will never
4429 get out of the front-end as we replace it when instantiating
4430 the template. */
4431 tree tmp = build2 (resultcode,
4432 build_type ? build_type : result_type,
4433 NULL_TREE, op1);
4434 TREE_OPERAND (tmp, 0) = op0;
4435 return tmp;
4438 if (arithmetic_types_p)
4440 bool first_complex = (code0 == COMPLEX_TYPE);
4441 bool second_complex = (code1 == COMPLEX_TYPE);
4442 int none_complex = (!first_complex && !second_complex);
4444 /* Adapted from patch for c/24581. */
4445 if (first_complex != second_complex
4446 && (code == PLUS_EXPR
4447 || code == MINUS_EXPR
4448 || code == MULT_EXPR
4449 || (code == TRUNC_DIV_EXPR && first_complex))
4450 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4451 && flag_signed_zeros)
4453 /* An operation on mixed real/complex operands must be
4454 handled specially, but the language-independent code can
4455 more easily optimize the plain complex arithmetic if
4456 -fno-signed-zeros. */
4457 tree real_type = TREE_TYPE (result_type);
4458 tree real, imag;
4459 if (first_complex)
4461 if (TREE_TYPE (op0) != result_type)
4462 op0 = cp_convert_and_check (result_type, op0, complain);
4463 if (TREE_TYPE (op1) != real_type)
4464 op1 = cp_convert_and_check (real_type, op1, complain);
4466 else
4468 if (TREE_TYPE (op0) != real_type)
4469 op0 = cp_convert_and_check (real_type, op0, complain);
4470 if (TREE_TYPE (op1) != result_type)
4471 op1 = cp_convert_and_check (result_type, op1, complain);
4473 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4474 return error_mark_node;
4475 if (first_complex)
4477 op0 = save_expr (op0);
4478 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4479 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4480 switch (code)
4482 case MULT_EXPR:
4483 case TRUNC_DIV_EXPR:
4484 op1 = save_expr (op1);
4485 imag = build2 (resultcode, real_type, imag, op1);
4486 /* Fall through. */
4487 case PLUS_EXPR:
4488 case MINUS_EXPR:
4489 real = build2 (resultcode, real_type, real, op1);
4490 break;
4491 default:
4492 gcc_unreachable();
4495 else
4497 op1 = save_expr (op1);
4498 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4499 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4500 switch (code)
4502 case MULT_EXPR:
4503 op0 = save_expr (op0);
4504 imag = build2 (resultcode, real_type, op0, imag);
4505 /* Fall through. */
4506 case PLUS_EXPR:
4507 real = build2 (resultcode, real_type, op0, real);
4508 break;
4509 case MINUS_EXPR:
4510 real = build2 (resultcode, real_type, op0, real);
4511 imag = build1 (NEGATE_EXPR, real_type, imag);
4512 break;
4513 default:
4514 gcc_unreachable();
4517 real = fold_if_not_in_template (real);
4518 imag = fold_if_not_in_template (imag);
4519 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4520 result = fold_if_not_in_template (result);
4521 return result;
4524 /* For certain operations (which identify themselves by shorten != 0)
4525 if both args were extended from the same smaller type,
4526 do the arithmetic in that type and then extend.
4528 shorten !=0 and !=1 indicates a bitwise operation.
4529 For them, this optimization is safe only if
4530 both args are zero-extended or both are sign-extended.
4531 Otherwise, we might change the result.
4532 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4533 but calculated in (unsigned short) it would be (unsigned short)-1. */
4535 if (shorten && none_complex)
4537 final_type = result_type;
4538 result_type = shorten_binary_op (result_type, op0, op1,
4539 shorten == -1);
4542 /* Comparison operations are shortened too but differently.
4543 They identify themselves by setting short_compare = 1. */
4545 if (short_compare)
4547 /* Don't write &op0, etc., because that would prevent op0
4548 from being kept in a register.
4549 Instead, make copies of the our local variables and
4550 pass the copies by reference, then copy them back afterward. */
4551 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4552 enum tree_code xresultcode = resultcode;
4553 tree val
4554 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4555 if (val != 0)
4556 return cp_convert (boolean_type_node, val, complain);
4557 op0 = xop0, op1 = xop1;
4558 converted = 1;
4559 resultcode = xresultcode;
4562 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4563 && warn_sign_compare
4564 /* Do not warn until the template is instantiated; we cannot
4565 bound the ranges of the arguments until that point. */
4566 && !processing_template_decl
4567 && (complain & tf_warning)
4568 && c_inhibit_evaluation_warnings == 0
4569 /* Even unsigned enum types promote to signed int. We don't
4570 want to issue -Wsign-compare warnings for this case. */
4571 && !enum_cast_to_int (orig_op0)
4572 && !enum_cast_to_int (orig_op1))
4574 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
4575 result_type, resultcode);
4579 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4580 Then the expression will be built.
4581 It will be given type FINAL_TYPE if that is nonzero;
4582 otherwise, it will be given type RESULT_TYPE. */
4583 if (! converted)
4585 if (TREE_TYPE (op0) != result_type)
4586 op0 = cp_convert_and_check (result_type, op0, complain);
4587 if (TREE_TYPE (op1) != result_type)
4588 op1 = cp_convert_and_check (result_type, op1, complain);
4590 if (op0 == error_mark_node || op1 == error_mark_node)
4591 return error_mark_node;
4594 if (build_type == NULL_TREE)
4595 build_type = result_type;
4597 result = build2 (resultcode, build_type, op0, op1);
4598 result = fold_if_not_in_template (result);
4599 if (final_type != 0)
4600 result = cp_convert (final_type, result, complain);
4602 if (TREE_OVERFLOW_P (result)
4603 && !TREE_OVERFLOW_P (op0)
4604 && !TREE_OVERFLOW_P (op1))
4605 overflow_warning (location, result);
4607 return result;
4610 /* Return a tree for the sum or difference (RESULTCODE says which)
4611 of pointer PTROP and integer INTOP. */
4613 static tree
4614 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4616 tree res_type = TREE_TYPE (ptrop);
4618 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4619 in certain circumstance (when it's valid to do so). So we need
4620 to make sure it's complete. We don't need to check here, if we
4621 can actually complete it at all, as those checks will be done in
4622 pointer_int_sum() anyway. */
4623 complete_type (TREE_TYPE (res_type));
4625 return pointer_int_sum (input_location, resultcode, ptrop,
4626 fold_if_not_in_template (intop));
4629 /* Return a tree for the difference of pointers OP0 and OP1.
4630 The resulting tree has type int. */
4632 static tree
4633 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
4635 tree result;
4636 tree restype = ptrdiff_type_node;
4637 tree target_type = TREE_TYPE (ptrtype);
4639 if (!complete_type_or_else (target_type, NULL_TREE))
4640 return error_mark_node;
4642 if (TREE_CODE (target_type) == VOID_TYPE)
4644 if (complain & tf_error)
4645 permerror (input_location, "ISO C++ forbids using pointer of "
4646 "type %<void *%> in subtraction");
4647 else
4648 return error_mark_node;
4650 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4652 if (complain & tf_error)
4653 permerror (input_location, "ISO C++ forbids using pointer to "
4654 "a function in subtraction");
4655 else
4656 return error_mark_node;
4658 if (TREE_CODE (target_type) == METHOD_TYPE)
4660 if (complain & tf_error)
4661 permerror (input_location, "ISO C++ forbids using pointer to "
4662 "a method in subtraction");
4663 else
4664 return error_mark_node;
4667 /* First do the subtraction as integers;
4668 then drop through to build the divide operator. */
4670 op0 = cp_build_binary_op (input_location,
4671 MINUS_EXPR,
4672 cp_convert (restype, op0, complain),
4673 cp_convert (restype, op1, complain),
4674 complain);
4676 /* This generates an error if op1 is a pointer to an incomplete type. */
4677 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4679 if (complain & tf_error)
4680 error ("invalid use of a pointer to an incomplete type in "
4681 "pointer arithmetic");
4682 else
4683 return error_mark_node;
4686 op1 = (TYPE_PTROB_P (ptrtype)
4687 ? size_in_bytes (target_type)
4688 : integer_one_node);
4690 /* Do the division. */
4692 result = build2 (EXACT_DIV_EXPR, restype, op0,
4693 cp_convert (restype, op1, complain));
4694 return fold_if_not_in_template (result);
4697 /* Construct and perhaps optimize a tree representation
4698 for a unary operation. CODE, a tree_code, specifies the operation
4699 and XARG is the operand. */
4701 tree
4702 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
4703 tsubst_flags_t complain)
4705 tree orig_expr = xarg;
4706 tree exp;
4707 int ptrmem = 0;
4709 if (processing_template_decl)
4711 if (type_dependent_expression_p (xarg))
4712 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
4714 xarg = build_non_dependent_expr (xarg);
4717 exp = NULL_TREE;
4719 /* [expr.unary.op] says:
4721 The address of an object of incomplete type can be taken.
4723 (And is just the ordinary address operator, not an overloaded
4724 "operator &".) However, if the type is a template
4725 specialization, we must complete the type at this point so that
4726 an overloaded "operator &" will be available if required. */
4727 if (code == ADDR_EXPR
4728 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4729 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4730 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4731 || (TREE_CODE (xarg) == OFFSET_REF)))
4732 /* Don't look for a function. */;
4733 else
4734 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
4735 NULL_TREE, /*overload=*/NULL, complain);
4736 if (!exp && code == ADDR_EXPR)
4738 if (is_overloaded_fn (xarg))
4740 tree fn = get_first_fn (xarg);
4741 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4743 error (DECL_CONSTRUCTOR_P (fn)
4744 ? G_("taking address of constructor %qE")
4745 : G_("taking address of destructor %qE"),
4746 xarg);
4747 return error_mark_node;
4751 /* A pointer to member-function can be formed only by saying
4752 &X::mf. */
4753 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4754 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4756 if (TREE_CODE (xarg) != OFFSET_REF
4757 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4759 error ("invalid use of %qE to form a pointer-to-member-function",
4760 xarg);
4761 if (TREE_CODE (xarg) != OFFSET_REF)
4762 inform (input_location, " a qualified-id is required");
4763 return error_mark_node;
4765 else
4767 error ("parentheses around %qE cannot be used to form a"
4768 " pointer-to-member-function",
4769 xarg);
4770 PTRMEM_OK_P (xarg) = 1;
4774 if (TREE_CODE (xarg) == OFFSET_REF)
4776 ptrmem = PTRMEM_OK_P (xarg);
4778 if (!ptrmem && !flag_ms_extensions
4779 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4781 /* A single non-static member, make sure we don't allow a
4782 pointer-to-member. */
4783 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4784 TREE_OPERAND (xarg, 0),
4785 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4786 PTRMEM_OK_P (xarg) = ptrmem;
4790 exp = cp_build_addr_expr_strict (xarg, complain);
4793 if (processing_template_decl && exp != error_mark_node)
4794 exp = build_min_non_dep (code, exp, orig_expr,
4795 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4796 if (TREE_CODE (exp) == ADDR_EXPR)
4797 PTRMEM_OK_P (exp) = ptrmem;
4798 return exp;
4801 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4802 constants, where a null value is represented by an INTEGER_CST of
4803 -1. */
4805 tree
4806 cp_truthvalue_conversion (tree expr)
4808 tree type = TREE_TYPE (expr);
4809 if (TYPE_PTRDATAMEM_P (type))
4810 return build_binary_op (EXPR_LOCATION (expr),
4811 NE_EXPR, expr, nullptr_node, 1);
4812 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
4814 /* With -Wzero-as-null-pointer-constant do not warn for an
4815 'if (p)' or a 'while (!p)', where p is a pointer. */
4816 tree ret;
4817 ++c_inhibit_evaluation_warnings;
4818 ret = c_common_truthvalue_conversion (input_location, expr);
4819 --c_inhibit_evaluation_warnings;
4820 return ret;
4822 else
4823 return c_common_truthvalue_conversion (input_location, expr);
4826 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4828 tree
4829 condition_conversion (tree expr)
4831 tree t;
4832 if (processing_template_decl)
4833 return expr;
4834 t = perform_implicit_conversion_flags (boolean_type_node, expr,
4835 tf_warning_or_error, LOOKUP_NORMAL);
4836 t = fold_build_cleanup_point_expr (boolean_type_node, t);
4837 return t;
4840 /* Returns the address of T. This function will fold away
4841 ADDR_EXPR of INDIRECT_REF. */
4843 tree
4844 build_address (tree t)
4846 if (error_operand_p (t) || !cxx_mark_addressable (t))
4847 return error_mark_node;
4848 t = build_fold_addr_expr (t);
4849 if (TREE_CODE (t) != ADDR_EXPR)
4850 t = rvalue (t);
4851 return t;
4854 /* Returns the address of T with type TYPE. */
4856 tree
4857 build_typed_address (tree t, tree type)
4859 if (error_operand_p (t) || !cxx_mark_addressable (t))
4860 return error_mark_node;
4861 t = build_fold_addr_expr_with_type (t, type);
4862 if (TREE_CODE (t) != ADDR_EXPR)
4863 t = rvalue (t);
4864 return t;
4867 /* Return a NOP_EXPR converting EXPR to TYPE. */
4869 tree
4870 build_nop (tree type, tree expr)
4872 if (type == error_mark_node || error_operand_p (expr))
4873 return expr;
4874 return build1 (NOP_EXPR, type, expr);
4877 /* Take the address of ARG, whatever that means under C++ semantics.
4878 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
4879 and class rvalues as well.
4881 Nothing should call this function directly; instead, callers should use
4882 cp_build_addr_expr or cp_build_addr_expr_strict. */
4884 static tree
4885 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
4887 tree argtype;
4888 tree val;
4890 if (!arg || error_operand_p (arg))
4891 return error_mark_node;
4893 arg = mark_lvalue_use (arg);
4894 argtype = lvalue_type (arg);
4896 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4897 || !IDENTIFIER_OPNAME_P (arg));
4899 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4900 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4902 /* They're trying to take the address of a unique non-static
4903 member function. This is ill-formed (except in MS-land),
4904 but let's try to DTRT.
4905 Note: We only handle unique functions here because we don't
4906 want to complain if there's a static overload; non-unique
4907 cases will be handled by instantiate_type. But we need to
4908 handle this case here to allow casts on the resulting PMF.
4909 We could defer this in non-MS mode, but it's easier to give
4910 a useful error here. */
4912 /* Inside constant member functions, the `this' pointer
4913 contains an extra const qualifier. TYPE_MAIN_VARIANT
4914 is used here to remove this const from the diagnostics
4915 and the created OFFSET_REF. */
4916 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4917 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4918 mark_used (fn);
4920 if (! flag_ms_extensions)
4922 tree name = DECL_NAME (fn);
4923 if (!(complain & tf_error))
4924 return error_mark_node;
4925 else if (current_class_type
4926 && TREE_OPERAND (arg, 0) == current_class_ref)
4927 /* An expression like &memfn. */
4928 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
4929 " or parenthesized non-static member function to form"
4930 " a pointer to member function. Say %<&%T::%D%>",
4931 base, name);
4932 else
4933 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
4934 " function to form a pointer to member function."
4935 " Say %<&%T::%D%>",
4936 base, name);
4938 arg = build_offset_ref (base, fn, /*address_p=*/true);
4941 /* Uninstantiated types are all functions. Taking the
4942 address of a function is a no-op, so just return the
4943 argument. */
4944 if (type_unknown_p (arg))
4945 return build1 (ADDR_EXPR, unknown_type_node, arg);
4947 if (TREE_CODE (arg) == OFFSET_REF)
4948 /* We want a pointer to member; bypass all the code for actually taking
4949 the address of something. */
4950 goto offset_ref;
4952 /* Anything not already handled and not a true memory reference
4953 is an error. */
4954 if (TREE_CODE (argtype) != FUNCTION_TYPE
4955 && TREE_CODE (argtype) != METHOD_TYPE)
4957 cp_lvalue_kind kind = lvalue_kind (arg);
4958 if (kind == clk_none)
4960 if (complain & tf_error)
4961 lvalue_error (input_location, lv_addressof);
4962 return error_mark_node;
4964 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
4966 if (!(complain & tf_error))
4967 return error_mark_node;
4968 if (kind & clk_class)
4969 /* Make this a permerror because we used to accept it. */
4970 permerror (input_location, "taking address of temporary");
4971 else
4972 error ("taking address of xvalue (rvalue reference)");
4976 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4978 tree type = build_pointer_type (TREE_TYPE (argtype));
4979 arg = build1 (CONVERT_EXPR, type, arg);
4980 return arg;
4982 else if (pedantic && DECL_MAIN_P (arg))
4984 /* ARM $3.4 */
4985 /* Apparently a lot of autoconf scripts for C++ packages do this,
4986 so only complain if -Wpedantic. */
4987 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
4988 pedwarn (input_location, OPT_Wpedantic,
4989 "ISO C++ forbids taking address of function %<::main%>");
4990 else if (flag_pedantic_errors)
4991 return error_mark_node;
4994 /* Let &* cancel out to simplify resulting code. */
4995 if (TREE_CODE (arg) == INDIRECT_REF)
4997 /* We don't need to have `current_class_ptr' wrapped in a
4998 NON_LVALUE_EXPR node. */
4999 if (arg == current_class_ref)
5000 return current_class_ptr;
5002 arg = TREE_OPERAND (arg, 0);
5003 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5005 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5006 arg = build1 (CONVERT_EXPR, type, arg);
5008 else
5009 /* Don't let this be an lvalue. */
5010 arg = rvalue (arg);
5011 return arg;
5014 /* ??? Cope with user tricks that amount to offsetof. */
5015 if (TREE_CODE (argtype) != FUNCTION_TYPE
5016 && TREE_CODE (argtype) != METHOD_TYPE
5017 && argtype != unknown_type_node
5018 && (val = get_base_address (arg))
5019 && COMPLETE_TYPE_P (TREE_TYPE (val))
5020 && TREE_CODE (val) == INDIRECT_REF
5021 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5023 tree type = build_pointer_type (argtype);
5024 return fold_convert (type, fold_offsetof_1 (arg));
5027 /* Handle complex lvalues (when permitted)
5028 by reduction to simpler cases. */
5029 val = unary_complex_lvalue (ADDR_EXPR, arg);
5030 if (val != 0)
5031 return val;
5033 switch (TREE_CODE (arg))
5035 CASE_CONVERT:
5036 case FLOAT_EXPR:
5037 case FIX_TRUNC_EXPR:
5038 /* Even if we're not being pedantic, we cannot allow this
5039 extension when we're instantiating in a SFINAE
5040 context. */
5041 if (! lvalue_p (arg) && complain == tf_none)
5043 if (complain & tf_error)
5044 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5045 else
5046 return error_mark_node;
5048 break;
5050 case BASELINK:
5051 arg = BASELINK_FUNCTIONS (arg);
5052 /* Fall through. */
5054 case OVERLOAD:
5055 arg = OVL_CURRENT (arg);
5056 break;
5058 case OFFSET_REF:
5059 offset_ref:
5060 /* Turn a reference to a non-static data member into a
5061 pointer-to-member. */
5063 tree type;
5064 tree t;
5066 gcc_assert (PTRMEM_OK_P (arg));
5068 t = TREE_OPERAND (arg, 1);
5069 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5071 if (complain & tf_error)
5072 error ("cannot create pointer to reference member %qD", t);
5073 return error_mark_node;
5076 type = build_ptrmem_type (context_for_name_lookup (t),
5077 TREE_TYPE (t));
5078 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5079 return t;
5082 default:
5083 break;
5086 if (argtype != error_mark_node)
5087 argtype = build_pointer_type (argtype);
5089 /* In a template, we are processing a non-dependent expression
5090 so we can just form an ADDR_EXPR with the correct type. */
5091 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5093 val = build_address (arg);
5094 if (TREE_CODE (arg) == OFFSET_REF)
5095 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5097 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5099 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5101 /* We can only get here with a single static member
5102 function. */
5103 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5104 && DECL_STATIC_FUNCTION_P (fn));
5105 mark_used (fn);
5106 val = build_address (fn);
5107 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5108 /* Do not lose object's side effects. */
5109 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5110 TREE_OPERAND (arg, 0), val);
5112 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5114 if (complain & tf_error)
5115 error ("attempt to take address of bit-field structure member %qD",
5116 TREE_OPERAND (arg, 1));
5117 return error_mark_node;
5119 else
5121 tree object = TREE_OPERAND (arg, 0);
5122 tree field = TREE_OPERAND (arg, 1);
5123 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5124 (TREE_TYPE (object), decl_type_context (field)));
5125 val = build_address (arg);
5128 if (TREE_CODE (argtype) == POINTER_TYPE
5129 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5131 build_ptrmemfunc_type (argtype);
5132 val = build_ptrmemfunc (argtype, val, 0,
5133 /*c_cast_p=*/false,
5134 complain);
5137 return val;
5140 /* Take the address of ARG if it has one, even if it's an rvalue. */
5142 tree
5143 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5145 return cp_build_addr_expr_1 (arg, 0, complain);
5148 /* Take the address of ARG, but only if it's an lvalue. */
5150 tree
5151 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5153 return cp_build_addr_expr_1 (arg, 1, complain);
5156 /* C++: Must handle pointers to members.
5158 Perhaps type instantiation should be extended to handle conversion
5159 from aggregates to types we don't yet know we want? (Or are those
5160 cases typically errors which should be reported?)
5162 NOCONVERT nonzero suppresses the default promotions
5163 (such as from short to int). */
5165 tree
5166 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5167 tsubst_flags_t complain)
5169 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5170 tree arg = xarg;
5171 tree argtype = 0;
5172 const char *errstring = NULL;
5173 tree val;
5174 const char *invalid_op_diag;
5176 if (!arg || error_operand_p (arg))
5177 return error_mark_node;
5179 if ((invalid_op_diag
5180 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5181 ? CONVERT_EXPR
5182 : code),
5183 TREE_TYPE (xarg))))
5185 error (invalid_op_diag);
5186 return error_mark_node;
5189 switch (code)
5191 case UNARY_PLUS_EXPR:
5192 case NEGATE_EXPR:
5194 int flags = WANT_ARITH | WANT_ENUM;
5195 /* Unary plus (but not unary minus) is allowed on pointers. */
5196 if (code == UNARY_PLUS_EXPR)
5197 flags |= WANT_POINTER;
5198 arg = build_expr_type_conversion (flags, arg, true);
5199 if (!arg)
5200 errstring = (code == NEGATE_EXPR
5201 ? _("wrong type argument to unary minus")
5202 : _("wrong type argument to unary plus"));
5203 else
5205 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5206 arg = cp_perform_integral_promotions (arg, complain);
5208 /* Make sure the result is not an lvalue: a unary plus or minus
5209 expression is always a rvalue. */
5210 arg = rvalue (arg);
5213 break;
5215 case BIT_NOT_EXPR:
5216 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5218 code = CONJ_EXPR;
5219 if (!noconvert)
5221 arg = cp_default_conversion (arg, complain);
5222 if (arg == error_mark_node)
5223 return error_mark_node;
5226 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5227 | WANT_VECTOR_OR_COMPLEX,
5228 arg, true)))
5229 errstring = _("wrong type argument to bit-complement");
5230 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5231 arg = cp_perform_integral_promotions (arg, complain);
5232 break;
5234 case ABS_EXPR:
5235 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5236 errstring = _("wrong type argument to abs");
5237 else if (!noconvert)
5239 arg = cp_default_conversion (arg, complain);
5240 if (arg == error_mark_node)
5241 return error_mark_node;
5243 break;
5245 case CONJ_EXPR:
5246 /* Conjugating a real value is a no-op, but allow it anyway. */
5247 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5248 errstring = _("wrong type argument to conjugation");
5249 else if (!noconvert)
5251 arg = cp_default_conversion (arg, complain);
5252 if (arg == error_mark_node)
5253 return error_mark_node;
5255 break;
5257 case TRUTH_NOT_EXPR:
5258 arg = perform_implicit_conversion (boolean_type_node, arg,
5259 complain);
5260 val = invert_truthvalue_loc (input_location, arg);
5261 if (arg != error_mark_node)
5262 return val;
5263 errstring = _("in argument to unary !");
5264 break;
5266 case NOP_EXPR:
5267 break;
5269 case REALPART_EXPR:
5270 case IMAGPART_EXPR:
5271 arg = build_real_imag_expr (input_location, code, arg);
5272 if (arg == error_mark_node)
5273 return arg;
5274 else
5275 return fold_if_not_in_template (arg);
5277 case PREINCREMENT_EXPR:
5278 case POSTINCREMENT_EXPR:
5279 case PREDECREMENT_EXPR:
5280 case POSTDECREMENT_EXPR:
5281 /* Handle complex lvalues (when permitted)
5282 by reduction to simpler cases. */
5284 val = unary_complex_lvalue (code, arg);
5285 if (val != 0)
5286 return val;
5288 arg = mark_lvalue_use (arg);
5290 /* Increment or decrement the real part of the value,
5291 and don't change the imaginary part. */
5292 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5294 tree real, imag;
5296 arg = stabilize_reference (arg);
5297 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5298 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5299 real = cp_build_unary_op (code, real, 1, complain);
5300 if (real == error_mark_node || imag == error_mark_node)
5301 return error_mark_node;
5302 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5303 real, imag);
5306 /* Report invalid types. */
5308 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5309 arg, true)))
5311 if (code == PREINCREMENT_EXPR)
5312 errstring = _("no pre-increment operator for type");
5313 else if (code == POSTINCREMENT_EXPR)
5314 errstring = _("no post-increment operator for type");
5315 else if (code == PREDECREMENT_EXPR)
5316 errstring = _("no pre-decrement operator for type");
5317 else
5318 errstring = _("no post-decrement operator for type");
5319 break;
5321 else if (arg == error_mark_node)
5322 return error_mark_node;
5324 /* Report something read-only. */
5326 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5327 || TREE_READONLY (arg))
5329 if (complain & tf_error)
5330 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5331 || code == POSTINCREMENT_EXPR)
5332 ? lv_increment : lv_decrement));
5333 else
5334 return error_mark_node;
5338 tree inc;
5339 tree declared_type = unlowered_expr_type (arg);
5341 argtype = TREE_TYPE (arg);
5343 /* ARM $5.2.5 last annotation says this should be forbidden. */
5344 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5346 if (complain & tf_error)
5347 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5348 ? G_("ISO C++ forbids incrementing an enum")
5349 : G_("ISO C++ forbids decrementing an enum"));
5350 else
5351 return error_mark_node;
5354 /* Compute the increment. */
5356 if (TREE_CODE (argtype) == POINTER_TYPE)
5358 tree type = complete_type (TREE_TYPE (argtype));
5360 if (!COMPLETE_OR_VOID_TYPE_P (type))
5362 if (complain & tf_error)
5363 error (((code == PREINCREMENT_EXPR
5364 || code == POSTINCREMENT_EXPR))
5365 ? G_("cannot increment a pointer to incomplete type %qT")
5366 : G_("cannot decrement a pointer to incomplete type %qT"),
5367 TREE_TYPE (argtype));
5368 else
5369 return error_mark_node;
5371 else if ((pedantic || warn_pointer_arith)
5372 && !TYPE_PTROB_P (argtype))
5374 if (complain & tf_error)
5375 permerror (input_location, (code == PREINCREMENT_EXPR
5376 || code == POSTINCREMENT_EXPR)
5377 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5378 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5379 argtype);
5380 else
5381 return error_mark_node;
5384 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5386 else
5387 inc = integer_one_node;
5389 inc = cp_convert (argtype, inc, complain);
5391 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5392 need to ask Objective-C to build the increment or decrement
5393 expression for it. */
5394 if (objc_is_property_ref (arg))
5395 return objc_build_incr_expr_for_property_ref (input_location, code,
5396 arg, inc);
5398 /* Complain about anything else that is not a true lvalue. */
5399 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5400 || code == POSTINCREMENT_EXPR)
5401 ? lv_increment : lv_decrement),
5402 complain))
5403 return error_mark_node;
5405 /* Forbid using -- on `bool'. */
5406 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5408 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5410 if (complain & tf_error)
5411 error ("invalid use of Boolean expression as operand "
5412 "to %<operator--%>");
5413 return error_mark_node;
5415 val = boolean_increment (code, arg);
5417 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5418 /* An rvalue has no cv-qualifiers. */
5419 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5420 else
5421 val = build2 (code, TREE_TYPE (arg), arg, inc);
5423 TREE_SIDE_EFFECTS (val) = 1;
5424 return val;
5427 case ADDR_EXPR:
5428 /* Note that this operation never does default_conversion
5429 regardless of NOCONVERT. */
5430 return cp_build_addr_expr (arg, complain);
5432 default:
5433 break;
5436 if (!errstring)
5438 if (argtype == 0)
5439 argtype = TREE_TYPE (arg);
5440 return fold_if_not_in_template (build1 (code, argtype, arg));
5443 if (complain & tf_error)
5444 error ("%s", errstring);
5445 return error_mark_node;
5448 /* Hook for the c-common bits that build a unary op. */
5449 tree
5450 build_unary_op (location_t location ATTRIBUTE_UNUSED,
5451 enum tree_code code, tree xarg, int noconvert)
5453 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5456 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5457 for certain kinds of expressions which are not really lvalues
5458 but which we can accept as lvalues.
5460 If ARG is not a kind of expression we can handle, return
5461 NULL_TREE. */
5463 tree
5464 unary_complex_lvalue (enum tree_code code, tree arg)
5466 /* Inside a template, making these kinds of adjustments is
5467 pointless; we are only concerned with the type of the
5468 expression. */
5469 if (processing_template_decl)
5470 return NULL_TREE;
5472 /* Handle (a, b) used as an "lvalue". */
5473 if (TREE_CODE (arg) == COMPOUND_EXPR)
5475 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5476 tf_warning_or_error);
5477 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5478 TREE_OPERAND (arg, 0), real_result);
5481 /* Handle (a ? b : c) used as an "lvalue". */
5482 if (TREE_CODE (arg) == COND_EXPR
5483 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5484 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5486 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5487 if (TREE_CODE (arg) == MODIFY_EXPR
5488 || TREE_CODE (arg) == PREINCREMENT_EXPR
5489 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5491 tree lvalue = TREE_OPERAND (arg, 0);
5492 if (TREE_SIDE_EFFECTS (lvalue))
5494 lvalue = stabilize_reference (lvalue);
5495 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5496 lvalue, TREE_OPERAND (arg, 1));
5498 return unary_complex_lvalue
5499 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5502 if (code != ADDR_EXPR)
5503 return NULL_TREE;
5505 /* Handle (a = b) used as an "lvalue" for `&'. */
5506 if (TREE_CODE (arg) == MODIFY_EXPR
5507 || TREE_CODE (arg) == INIT_EXPR)
5509 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5510 tf_warning_or_error);
5511 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5512 arg, real_result);
5513 TREE_NO_WARNING (arg) = 1;
5514 return arg;
5517 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5518 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5519 || TREE_CODE (arg) == OFFSET_REF)
5520 return NULL_TREE;
5522 /* We permit compiler to make function calls returning
5523 objects of aggregate type look like lvalues. */
5525 tree targ = arg;
5527 if (TREE_CODE (targ) == SAVE_EXPR)
5528 targ = TREE_OPERAND (targ, 0);
5530 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5532 if (TREE_CODE (arg) == SAVE_EXPR)
5533 targ = arg;
5534 else
5535 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5536 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5539 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5540 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5541 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5544 /* Don't let anything else be handled specially. */
5545 return NULL_TREE;
5548 /* Mark EXP saying that we need to be able to take the
5549 address of it; it should not be allocated in a register.
5550 Value is true if successful.
5552 C++: we do not allow `current_class_ptr' to be addressable. */
5554 bool
5555 cxx_mark_addressable (tree exp)
5557 tree x = exp;
5559 while (1)
5560 switch (TREE_CODE (x))
5562 case ADDR_EXPR:
5563 case COMPONENT_REF:
5564 case ARRAY_REF:
5565 case REALPART_EXPR:
5566 case IMAGPART_EXPR:
5567 x = TREE_OPERAND (x, 0);
5568 break;
5570 case PARM_DECL:
5571 if (x == current_class_ptr)
5573 error ("cannot take the address of %<this%>, which is an rvalue expression");
5574 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5575 return true;
5577 /* Fall through. */
5579 case VAR_DECL:
5580 /* Caller should not be trying to mark initialized
5581 constant fields addressable. */
5582 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5583 || DECL_IN_AGGR_P (x) == 0
5584 || TREE_STATIC (x)
5585 || DECL_EXTERNAL (x));
5586 /* Fall through. */
5588 case RESULT_DECL:
5589 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5590 && !DECL_ARTIFICIAL (x))
5592 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
5594 error
5595 ("address of explicit register variable %qD requested", x);
5596 return false;
5598 else if (extra_warnings)
5599 warning
5600 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5602 TREE_ADDRESSABLE (x) = 1;
5603 return true;
5605 case CONST_DECL:
5606 case FUNCTION_DECL:
5607 TREE_ADDRESSABLE (x) = 1;
5608 return true;
5610 case CONSTRUCTOR:
5611 TREE_ADDRESSABLE (x) = 1;
5612 return true;
5614 case TARGET_EXPR:
5615 TREE_ADDRESSABLE (x) = 1;
5616 cxx_mark_addressable (TREE_OPERAND (x, 0));
5617 return true;
5619 default:
5620 return true;
5624 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5626 tree
5627 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
5628 tsubst_flags_t complain)
5630 tree orig_ifexp = ifexp;
5631 tree orig_op1 = op1;
5632 tree orig_op2 = op2;
5633 tree expr;
5635 if (processing_template_decl)
5637 /* The standard says that the expression is type-dependent if
5638 IFEXP is type-dependent, even though the eventual type of the
5639 expression doesn't dependent on IFEXP. */
5640 if (type_dependent_expression_p (ifexp)
5641 /* As a GNU extension, the middle operand may be omitted. */
5642 || (op1 && type_dependent_expression_p (op1))
5643 || type_dependent_expression_p (op2))
5644 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
5645 ifexp = build_non_dependent_expr (ifexp);
5646 if (op1)
5647 op1 = build_non_dependent_expr (op1);
5648 op2 = build_non_dependent_expr (op2);
5651 expr = build_conditional_expr (ifexp, op1, op2, complain);
5652 if (processing_template_decl && expr != error_mark_node)
5654 tree min = build_min_non_dep (COND_EXPR, expr,
5655 orig_ifexp, orig_op1, orig_op2);
5656 /* In C++11, remember that the result is an lvalue or xvalue.
5657 In C++98, lvalue_kind can just assume lvalue in a template. */
5658 if (cxx_dialect >= cxx0x
5659 && lvalue_or_rvalue_with_address_p (expr)
5660 && !lvalue_or_rvalue_with_address_p (min))
5661 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5662 !real_lvalue_p (expr));
5663 expr = convert_from_reference (min);
5665 return expr;
5668 /* Given a list of expressions, return a compound expression
5669 that performs them all and returns the value of the last of them. */
5671 tree
5672 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5673 tsubst_flags_t complain)
5675 tree expr = TREE_VALUE (list);
5677 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5678 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5680 if (complain & tf_error)
5681 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5682 "non-class type must not be parenthesized");
5683 else
5684 return error_mark_node;
5687 if (TREE_CHAIN (list))
5689 if (complain & tf_error)
5690 switch (exp)
5692 case ELK_INIT:
5693 permerror (input_location, "expression list treated as compound "
5694 "expression in initializer");
5695 break;
5696 case ELK_MEM_INIT:
5697 permerror (input_location, "expression list treated as compound "
5698 "expression in mem-initializer");
5699 break;
5700 case ELK_FUNC_CAST:
5701 permerror (input_location, "expression list treated as compound "
5702 "expression in functional cast");
5703 break;
5704 default:
5705 gcc_unreachable ();
5707 else
5708 return error_mark_node;
5710 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5711 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
5712 expr, TREE_VALUE (list), complain);
5715 return expr;
5718 /* Like build_x_compound_expr_from_list, but using a VEC. */
5720 tree
5721 build_x_compound_expr_from_vec (VEC(tree,gc) *vec, const char *msg)
5723 if (VEC_empty (tree, vec))
5724 return NULL_TREE;
5725 else if (VEC_length (tree, vec) == 1)
5726 return VEC_index (tree, vec, 0);
5727 else
5729 tree expr;
5730 unsigned int ix;
5731 tree t;
5733 if (msg != NULL)
5734 permerror (input_location,
5735 "%s expression list treated as compound expression",
5736 msg);
5738 expr = VEC_index (tree, vec, 0);
5739 for (ix = 1; VEC_iterate (tree, vec, ix, t); ++ix)
5740 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
5741 t, tf_warning_or_error);
5743 return expr;
5747 /* Handle overloading of the ',' operator when needed. */
5749 tree
5750 build_x_compound_expr (location_t loc, tree op1, tree op2,
5751 tsubst_flags_t complain)
5753 tree result;
5754 tree orig_op1 = op1;
5755 tree orig_op2 = op2;
5757 if (processing_template_decl)
5759 if (type_dependent_expression_p (op1)
5760 || type_dependent_expression_p (op2))
5761 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
5762 op1 = build_non_dependent_expr (op1);
5763 op2 = build_non_dependent_expr (op2);
5766 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
5767 NULL_TREE, /*overload=*/NULL, complain);
5768 if (!result)
5769 result = cp_build_compound_expr (op1, op2, complain);
5771 if (processing_template_decl && result != error_mark_node)
5772 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
5774 return result;
5777 /* Like cp_build_compound_expr, but for the c-common bits. */
5779 tree
5780 build_compound_expr (location_t loc ATTRIBUTE_UNUSED, tree lhs, tree rhs)
5782 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
5785 /* Build a compound expression. */
5787 tree
5788 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
5790 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
5792 if (lhs == error_mark_node || rhs == error_mark_node)
5793 return error_mark_node;
5795 if (TREE_CODE (rhs) == TARGET_EXPR)
5797 /* If the rhs is a TARGET_EXPR, then build the compound
5798 expression inside the target_expr's initializer. This
5799 helps the compiler to eliminate unnecessary temporaries. */
5800 tree init = TREE_OPERAND (rhs, 1);
5802 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
5803 TREE_OPERAND (rhs, 1) = init;
5805 return rhs;
5808 if (type_unknown_p (rhs))
5810 error ("no context to resolve type of %qE", rhs);
5811 return error_mark_node;
5814 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
5817 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
5818 casts away constness. CAST gives the type of cast. Returns true
5819 if the cast is ill-formed, false if it is well-formed.
5821 ??? This function warns for casting away any qualifier not just
5822 const. We would like to specify exactly what qualifiers are casted
5823 away.
5826 static bool
5827 check_for_casting_away_constness (tree src_type, tree dest_type,
5828 enum tree_code cast, tsubst_flags_t complain)
5830 /* C-style casts are allowed to cast away constness. With
5831 WARN_CAST_QUAL, we still want to issue a warning. */
5832 if (cast == CAST_EXPR && !warn_cast_qual)
5833 return false;
5835 if (!casts_away_constness (src_type, dest_type, complain))
5836 return false;
5838 switch (cast)
5840 case CAST_EXPR:
5841 if (complain & tf_warning)
5842 warning (OPT_Wcast_qual,
5843 "cast from type %qT to type %qT casts away qualifiers",
5844 src_type, dest_type);
5845 return false;
5847 case STATIC_CAST_EXPR:
5848 if (complain & tf_error)
5849 error ("static_cast from type %qT to type %qT casts away qualifiers",
5850 src_type, dest_type);
5851 return true;
5853 case REINTERPRET_CAST_EXPR:
5854 if (complain & tf_error)
5855 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
5856 src_type, dest_type);
5857 return true;
5859 default:
5860 gcc_unreachable();
5865 Warns if the cast from expression EXPR to type TYPE is useless.
5867 void
5868 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
5870 if (warn_useless_cast
5871 && complain & tf_warning
5872 && c_inhibit_evaluation_warnings == 0)
5874 if (REFERENCE_REF_P (expr))
5875 expr = TREE_OPERAND (expr, 0);
5877 if ((TREE_CODE (type) == REFERENCE_TYPE
5878 && (TYPE_REF_IS_RVALUE (type)
5879 ? xvalue_p (expr) : real_lvalue_p (expr))
5880 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
5881 || same_type_p (TREE_TYPE (expr), type))
5882 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
5886 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
5887 (another pointer-to-member type in the same hierarchy) and return
5888 the converted expression. If ALLOW_INVERSE_P is permitted, a
5889 pointer-to-derived may be converted to pointer-to-base; otherwise,
5890 only the other direction is permitted. If C_CAST_P is true, this
5891 conversion is taking place as part of a C-style cast. */
5893 tree
5894 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
5895 bool c_cast_p, tsubst_flags_t complain)
5897 if (TYPE_PTRDATAMEM_P (type))
5899 tree delta;
5901 if (TREE_CODE (expr) == PTRMEM_CST)
5902 expr = cplus_expand_constant (expr);
5903 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
5904 TYPE_PTRMEM_CLASS_TYPE (type),
5905 allow_inverse_p,
5906 c_cast_p, complain);
5907 if (delta == error_mark_node)
5908 return error_mark_node;
5910 if (!integer_zerop (delta))
5912 tree cond, op1, op2;
5914 cond = cp_build_binary_op (input_location,
5915 EQ_EXPR,
5916 expr,
5917 build_int_cst (TREE_TYPE (expr), -1),
5918 complain);
5919 op1 = build_nop (ptrdiff_type_node, expr);
5920 op2 = cp_build_binary_op (input_location,
5921 PLUS_EXPR, op1, delta,
5922 complain);
5924 expr = fold_build3_loc (input_location,
5925 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
5929 return build_nop (type, expr);
5931 else
5932 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
5933 allow_inverse_p, c_cast_p, complain);
5936 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
5937 this static_cast is being attempted as one of the possible casts
5938 allowed by a C-style cast. (In that case, accessibility of base
5939 classes is not considered, and it is OK to cast away
5940 constness.) Return the result of the cast. *VALID_P is set to
5941 indicate whether or not the cast was valid. */
5943 static tree
5944 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
5945 bool *valid_p, tsubst_flags_t complain)
5947 tree intype;
5948 tree result;
5949 cp_lvalue_kind clk;
5951 /* Assume the cast is valid. */
5952 *valid_p = true;
5954 intype = unlowered_expr_type (expr);
5956 /* Save casted types in the function's used types hash table. */
5957 used_types_insert (type);
5959 /* [expr.static.cast]
5961 An lvalue of type "cv1 B", where B is a class type, can be cast
5962 to type "reference to cv2 D", where D is a class derived (clause
5963 _class.derived_) from B, if a valid standard conversion from
5964 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
5965 same cv-qualification as, or greater cv-qualification than, cv1,
5966 and B is not a virtual base class of D. */
5967 /* We check this case before checking the validity of "TYPE t =
5968 EXPR;" below because for this case:
5970 struct B {};
5971 struct D : public B { D(const B&); };
5972 extern B& b;
5973 void f() { static_cast<const D&>(b); }
5975 we want to avoid constructing a new D. The standard is not
5976 completely clear about this issue, but our interpretation is
5977 consistent with other compilers. */
5978 if (TREE_CODE (type) == REFERENCE_TYPE
5979 && CLASS_TYPE_P (TREE_TYPE (type))
5980 && CLASS_TYPE_P (intype)
5981 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
5982 && DERIVED_FROM_P (intype, TREE_TYPE (type))
5983 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
5984 build_pointer_type (TYPE_MAIN_VARIANT
5985 (TREE_TYPE (type))),
5986 complain)
5987 && (c_cast_p
5988 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5990 tree base;
5992 /* There is a standard conversion from "D*" to "B*" even if "B"
5993 is ambiguous or inaccessible. If this is really a
5994 static_cast, then we check both for inaccessibility and
5995 ambiguity. However, if this is a static_cast being performed
5996 because the user wrote a C-style cast, then accessibility is
5997 not considered. */
5998 base = lookup_base (TREE_TYPE (type), intype,
5999 c_cast_p ? ba_unique : ba_check,
6000 NULL);
6002 /* Convert from "B*" to "D*". This function will check that "B"
6003 is not a virtual base of "D". */
6004 expr = build_base_path (MINUS_EXPR, build_address (expr),
6005 base, /*nonnull=*/false, complain);
6006 /* Convert the pointer to a reference -- but then remember that
6007 there are no expressions with reference type in C++.
6009 We call rvalue so that there's an actual tree code
6010 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6011 is a variable with the same type, the conversion would get folded
6012 away, leaving just the variable and causing lvalue_kind to give
6013 the wrong answer. */
6014 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6017 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6018 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6019 if (TREE_CODE (type) == REFERENCE_TYPE
6020 && TYPE_REF_IS_RVALUE (type)
6021 && (clk = real_lvalue_p (expr))
6022 && reference_related_p (TREE_TYPE (type), intype)
6023 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6025 if (clk == clk_ordinary)
6027 /* Handle the (non-bit-field) lvalue case here by casting to
6028 lvalue reference and then changing it to an rvalue reference.
6029 Casting an xvalue to rvalue reference will be handled by the
6030 main code path. */
6031 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6032 result = (perform_direct_initialization_if_possible
6033 (lref, expr, c_cast_p, complain));
6034 result = cp_fold_convert (type, result);
6035 /* Make sure we don't fold back down to a named rvalue reference,
6036 because that would be an lvalue. */
6037 if (DECL_P (result))
6038 result = build1 (NON_LVALUE_EXPR, type, result);
6039 return convert_from_reference (result);
6041 else
6042 /* For a bit-field or packed field, bind to a temporary. */
6043 expr = rvalue (expr);
6046 /* Resolve overloaded address here rather than once in
6047 implicit_conversion and again in the inverse code below. */
6048 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6050 expr = instantiate_type (type, expr, complain);
6051 intype = TREE_TYPE (expr);
6054 /* [expr.static.cast]
6056 An expression e can be explicitly converted to a type T using a
6057 static_cast of the form static_cast<T>(e) if the declaration T
6058 t(e);" is well-formed, for some invented temporary variable
6059 t. */
6060 result = perform_direct_initialization_if_possible (type, expr,
6061 c_cast_p, complain);
6062 if (result)
6064 result = convert_from_reference (result);
6066 /* [expr.static.cast]
6068 If T is a reference type, the result is an lvalue; otherwise,
6069 the result is an rvalue. */
6070 if (TREE_CODE (type) != REFERENCE_TYPE)
6071 result = rvalue (result);
6072 return result;
6075 /* [expr.static.cast]
6077 Any expression can be explicitly converted to type cv void. */
6078 if (TREE_CODE (type) == VOID_TYPE)
6079 return convert_to_void (expr, ICV_CAST, complain);
6081 /* [expr.static.cast]
6083 The inverse of any standard conversion sequence (clause _conv_),
6084 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6085 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6086 (_conv.bool_) conversions, can be performed explicitly using
6087 static_cast subject to the restriction that the explicit
6088 conversion does not cast away constness (_expr.const.cast_), and
6089 the following additional rules for specific cases: */
6090 /* For reference, the conversions not excluded are: integral
6091 promotions, floating point promotion, integral conversions,
6092 floating point conversions, floating-integral conversions,
6093 pointer conversions, and pointer to member conversions. */
6094 /* DR 128
6096 A value of integral _or enumeration_ type can be explicitly
6097 converted to an enumeration type. */
6098 /* The effect of all that is that any conversion between any two
6099 types which are integral, floating, or enumeration types can be
6100 performed. */
6101 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6102 || SCALAR_FLOAT_TYPE_P (type))
6103 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6104 || SCALAR_FLOAT_TYPE_P (intype)))
6105 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6107 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6108 && CLASS_TYPE_P (TREE_TYPE (type))
6109 && CLASS_TYPE_P (TREE_TYPE (intype))
6110 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6111 (TREE_TYPE (intype))),
6112 build_pointer_type (TYPE_MAIN_VARIANT
6113 (TREE_TYPE (type))),
6114 complain))
6116 tree base;
6118 if (!c_cast_p
6119 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6120 complain))
6121 return error_mark_node;
6122 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6123 c_cast_p ? ba_unique : ba_check,
6124 NULL);
6125 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6126 complain);
6127 return cp_fold_convert(type, expr);
6130 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6131 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6133 tree c1;
6134 tree c2;
6135 tree t1;
6136 tree t2;
6138 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6139 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6141 if (TYPE_PTRDATAMEM_P (type))
6143 t1 = (build_ptrmem_type
6144 (c1,
6145 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6146 t2 = (build_ptrmem_type
6147 (c2,
6148 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6150 else
6152 t1 = intype;
6153 t2 = type;
6155 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6157 if (!c_cast_p
6158 && check_for_casting_away_constness (intype, type,
6159 STATIC_CAST_EXPR,
6160 complain))
6161 return error_mark_node;
6162 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6163 c_cast_p, complain);
6167 /* [expr.static.cast]
6169 An rvalue of type "pointer to cv void" can be explicitly
6170 converted to a pointer to object type. A value of type pointer
6171 to object converted to "pointer to cv void" and back to the
6172 original pointer type will have its original value. */
6173 if (TREE_CODE (intype) == POINTER_TYPE
6174 && VOID_TYPE_P (TREE_TYPE (intype))
6175 && TYPE_PTROB_P (type))
6177 if (!c_cast_p
6178 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6179 complain))
6180 return error_mark_node;
6181 return build_nop (type, expr);
6184 *valid_p = false;
6185 return error_mark_node;
6188 /* Return an expression representing static_cast<TYPE>(EXPR). */
6190 tree
6191 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6193 tree result;
6194 bool valid_p;
6196 if (type == error_mark_node || expr == error_mark_node)
6197 return error_mark_node;
6199 if (processing_template_decl)
6201 expr = build_min (STATIC_CAST_EXPR, type, expr);
6202 /* We don't know if it will or will not have side effects. */
6203 TREE_SIDE_EFFECTS (expr) = 1;
6204 return convert_from_reference (expr);
6207 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6208 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6209 if (TREE_CODE (type) != REFERENCE_TYPE
6210 && TREE_CODE (expr) == NOP_EXPR
6211 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6212 expr = TREE_OPERAND (expr, 0);
6214 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6215 complain);
6216 if (valid_p)
6218 if (result != error_mark_node)
6219 maybe_warn_about_useless_cast (type, expr, complain);
6220 return result;
6223 if (complain & tf_error)
6224 error ("invalid static_cast from type %qT to type %qT",
6225 TREE_TYPE (expr), type);
6226 return error_mark_node;
6229 /* EXPR is an expression with member function or pointer-to-member
6230 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6231 not permitted by ISO C++, but we accept it in some modes. If we
6232 are not in one of those modes, issue a diagnostic. Return the
6233 converted expression. */
6235 tree
6236 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6238 tree intype;
6239 tree decl;
6241 intype = TREE_TYPE (expr);
6242 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6243 || TREE_CODE (intype) == METHOD_TYPE);
6245 if (!(complain & tf_warning_or_error))
6246 return error_mark_node;
6248 if (pedantic || warn_pmf2ptr)
6249 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6250 "converting from %qT to %qT", intype, type);
6252 if (TREE_CODE (intype) == METHOD_TYPE)
6253 expr = build_addr_func (expr, complain);
6254 else if (TREE_CODE (expr) == PTRMEM_CST)
6255 expr = build_address (PTRMEM_CST_MEMBER (expr));
6256 else
6258 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6259 decl = build_address (decl);
6260 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6263 if (expr == error_mark_node)
6264 return error_mark_node;
6266 return build_nop (type, expr);
6269 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6270 If C_CAST_P is true, this reinterpret cast is being done as part of
6271 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6272 indicate whether or not reinterpret_cast was valid. */
6274 static tree
6275 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6276 bool *valid_p, tsubst_flags_t complain)
6278 tree intype;
6280 /* Assume the cast is invalid. */
6281 if (valid_p)
6282 *valid_p = true;
6284 if (type == error_mark_node || error_operand_p (expr))
6285 return error_mark_node;
6287 intype = TREE_TYPE (expr);
6289 /* Save casted types in the function's used types hash table. */
6290 used_types_insert (type);
6292 /* [expr.reinterpret.cast]
6293 An lvalue expression of type T1 can be cast to the type
6294 "reference to T2" if an expression of type "pointer to T1" can be
6295 explicitly converted to the type "pointer to T2" using a
6296 reinterpret_cast. */
6297 if (TREE_CODE (type) == REFERENCE_TYPE)
6299 if (! real_lvalue_p (expr))
6301 if (complain & tf_error)
6302 error ("invalid cast of an rvalue expression of type "
6303 "%qT to type %qT",
6304 intype, type);
6305 return error_mark_node;
6308 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6309 "B" are related class types; the reinterpret_cast does not
6310 adjust the pointer. */
6311 if (TYPE_PTR_P (intype)
6312 && (complain & tf_warning)
6313 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6314 COMPARE_BASE | COMPARE_DERIVED)))
6315 warning (0, "casting %qT to %qT does not dereference pointer",
6316 intype, type);
6318 expr = cp_build_addr_expr (expr, complain);
6320 if (warn_strict_aliasing > 2)
6321 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6323 if (expr != error_mark_node)
6324 expr = build_reinterpret_cast_1
6325 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6326 valid_p, complain);
6327 if (expr != error_mark_node)
6328 /* cp_build_indirect_ref isn't right for rvalue refs. */
6329 expr = convert_from_reference (fold_convert (type, expr));
6330 return expr;
6333 /* As a G++ extension, we consider conversions from member
6334 functions, and pointers to member functions to
6335 pointer-to-function and pointer-to-void types. If
6336 -Wno-pmf-conversions has not been specified,
6337 convert_member_func_to_ptr will issue an error message. */
6338 if ((TYPE_PTRMEMFUNC_P (intype)
6339 || TREE_CODE (intype) == METHOD_TYPE)
6340 && TYPE_PTR_P (type)
6341 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6342 || VOID_TYPE_P (TREE_TYPE (type))))
6343 return convert_member_func_to_ptr (type, expr, complain);
6345 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6346 array-to-pointer, and function-to-pointer conversions are
6347 performed. */
6348 expr = decay_conversion (expr, complain);
6350 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6351 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6352 if (TREE_CODE (expr) == NOP_EXPR
6353 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6354 expr = TREE_OPERAND (expr, 0);
6356 if (error_operand_p (expr))
6357 return error_mark_node;
6359 intype = TREE_TYPE (expr);
6361 /* [expr.reinterpret.cast]
6362 A pointer can be converted to any integral type large enough to
6363 hold it. ... A value of type std::nullptr_t can be converted to
6364 an integral type; the conversion has the same meaning and
6365 validity as a conversion of (void*)0 to the integral type. */
6366 if (CP_INTEGRAL_TYPE_P (type)
6367 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6369 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6371 if (complain & tf_error)
6372 permerror (input_location, "cast from %qT to %qT loses precision",
6373 intype, type);
6374 else
6375 return error_mark_node;
6377 if (NULLPTR_TYPE_P (intype))
6378 return build_int_cst (type, 0);
6380 /* [expr.reinterpret.cast]
6381 A value of integral or enumeration type can be explicitly
6382 converted to a pointer. */
6383 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6384 /* OK */
6386 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6387 || TYPE_PTR_OR_PTRMEM_P (type))
6388 && same_type_p (type, intype))
6389 /* DR 799 */
6390 return fold_if_not_in_template (build_nop (type, expr));
6391 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6392 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6393 return fold_if_not_in_template (build_nop (type, expr));
6394 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6395 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6397 tree sexpr = expr;
6399 if (!c_cast_p
6400 && check_for_casting_away_constness (intype, type,
6401 REINTERPRET_CAST_EXPR,
6402 complain))
6403 return error_mark_node;
6404 /* Warn about possible alignment problems. */
6405 if (STRICT_ALIGNMENT && warn_cast_align
6406 && (complain & tf_warning)
6407 && !VOID_TYPE_P (type)
6408 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6409 && COMPLETE_TYPE_P (TREE_TYPE (type))
6410 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6411 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6412 warning (OPT_Wcast_align, "cast from %qT to %qT "
6413 "increases required alignment of target type", intype, type);
6415 /* We need to strip nops here, because the front end likes to
6416 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6417 STRIP_NOPS (sexpr);
6418 if (warn_strict_aliasing <= 2)
6419 strict_aliasing_warning (intype, type, sexpr);
6421 return fold_if_not_in_template (build_nop (type, expr));
6423 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6424 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6426 if (pedantic && (complain & tf_warning))
6427 /* Only issue a warning, as we have always supported this
6428 where possible, and it is necessary in some cases. DR 195
6429 addresses this issue, but as of 2004/10/26 is still in
6430 drafting. */
6431 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6432 return fold_if_not_in_template (build_nop (type, expr));
6434 else if (TREE_CODE (type) == VECTOR_TYPE)
6435 return fold_if_not_in_template (convert_to_vector (type, expr));
6436 else if (TREE_CODE (intype) == VECTOR_TYPE
6437 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6438 return fold_if_not_in_template (convert_to_integer (type, expr));
6439 else
6441 if (valid_p)
6442 *valid_p = false;
6443 if (complain & tf_error)
6444 error ("invalid cast from type %qT to type %qT", intype, type);
6445 return error_mark_node;
6448 return cp_convert (type, expr, complain);
6451 tree
6452 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6454 tree r;
6456 if (type == error_mark_node || expr == error_mark_node)
6457 return error_mark_node;
6459 if (processing_template_decl)
6461 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6463 if (!TREE_SIDE_EFFECTS (t)
6464 && type_dependent_expression_p (expr))
6465 /* There might turn out to be side effects inside expr. */
6466 TREE_SIDE_EFFECTS (t) = 1;
6467 return convert_from_reference (t);
6470 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6471 /*valid_p=*/NULL, complain);
6472 if (r != error_mark_node)
6473 maybe_warn_about_useless_cast (type, expr, complain);
6474 return r;
6477 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6478 return an appropriate expression. Otherwise, return
6479 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6480 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6481 performing a C-style cast, its value upon return will indicate
6482 whether or not the conversion succeeded. */
6484 static tree
6485 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6486 bool *valid_p)
6488 tree src_type;
6489 tree reference_type;
6491 /* Callers are responsible for handling error_mark_node as a
6492 destination type. */
6493 gcc_assert (dst_type != error_mark_node);
6494 /* In a template, callers should be building syntactic
6495 representations of casts, not using this machinery. */
6496 gcc_assert (!processing_template_decl);
6498 /* Assume the conversion is invalid. */
6499 if (valid_p)
6500 *valid_p = false;
6502 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6504 if (complain & tf_error)
6505 error ("invalid use of const_cast with type %qT, "
6506 "which is not a pointer, "
6507 "reference, nor a pointer-to-data-member type", dst_type);
6508 return error_mark_node;
6511 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6513 if (complain & tf_error)
6514 error ("invalid use of const_cast with type %qT, which is a pointer "
6515 "or reference to a function type", dst_type);
6516 return error_mark_node;
6519 /* Save casted types in the function's used types hash table. */
6520 used_types_insert (dst_type);
6522 src_type = TREE_TYPE (expr);
6523 /* Expressions do not really have reference types. */
6524 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6525 src_type = TREE_TYPE (src_type);
6527 /* [expr.const.cast]
6529 For two object types T1 and T2, if a pointer to T1 can be explicitly
6530 converted to the type "pointer to T2" using a const_cast, then the
6531 following conversions can also be made:
6533 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6534 type T2 using the cast const_cast<T2&>;
6536 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6537 type T2 using the cast const_cast<T2&&>; and
6539 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6540 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6542 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6544 reference_type = dst_type;
6545 if (!TYPE_REF_IS_RVALUE (dst_type)
6546 ? real_lvalue_p (expr)
6547 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6548 ? lvalue_p (expr)
6549 : lvalue_or_rvalue_with_address_p (expr)))
6550 /* OK. */;
6551 else
6553 if (complain & tf_error)
6554 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6555 src_type, dst_type);
6556 return error_mark_node;
6558 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6559 src_type = build_pointer_type (src_type);
6561 else
6563 reference_type = NULL_TREE;
6564 /* If the destination type is not a reference type, the
6565 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6566 conversions are performed. */
6567 src_type = type_decays_to (src_type);
6568 if (src_type == error_mark_node)
6569 return error_mark_node;
6572 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
6574 if (comp_ptr_ttypes_const (dst_type, src_type))
6576 if (valid_p)
6578 *valid_p = true;
6579 /* This cast is actually a C-style cast. Issue a warning if
6580 the user is making a potentially unsafe cast. */
6581 check_for_casting_away_constness (src_type, dst_type,
6582 CAST_EXPR, complain);
6584 if (reference_type)
6586 expr = cp_build_addr_expr (expr, complain);
6587 if (expr == error_mark_node)
6588 return error_mark_node;
6589 expr = build_nop (reference_type, expr);
6590 return convert_from_reference (expr);
6592 else
6594 expr = decay_conversion (expr, complain);
6595 if (expr == error_mark_node)
6596 return error_mark_node;
6598 /* build_c_cast puts on a NOP_EXPR to make the result not an
6599 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6600 non-lvalue context. */
6601 if (TREE_CODE (expr) == NOP_EXPR
6602 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6603 expr = TREE_OPERAND (expr, 0);
6604 return build_nop (dst_type, expr);
6607 else if (valid_p
6608 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6609 TREE_TYPE (src_type)))
6610 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6611 complain);
6614 if (complain & tf_error)
6615 error ("invalid const_cast from type %qT to type %qT",
6616 src_type, dst_type);
6617 return error_mark_node;
6620 tree
6621 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6623 tree r;
6625 if (type == error_mark_node || error_operand_p (expr))
6626 return error_mark_node;
6628 if (processing_template_decl)
6630 tree t = build_min (CONST_CAST_EXPR, type, expr);
6632 if (!TREE_SIDE_EFFECTS (t)
6633 && type_dependent_expression_p (expr))
6634 /* There might turn out to be side effects inside expr. */
6635 TREE_SIDE_EFFECTS (t) = 1;
6636 return convert_from_reference (t);
6639 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
6640 if (r != error_mark_node)
6641 maybe_warn_about_useless_cast (type, expr, complain);
6642 return r;
6645 /* Like cp_build_c_cast, but for the c-common bits. */
6647 tree
6648 build_c_cast (location_t loc ATTRIBUTE_UNUSED, tree type, tree expr)
6650 return cp_build_c_cast (type, expr, tf_warning_or_error);
6653 /* Build an expression representing an explicit C-style cast to type
6654 TYPE of expression EXPR. */
6656 tree
6657 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6659 tree value = expr;
6660 tree result;
6661 bool valid_p;
6663 if (type == error_mark_node || error_operand_p (expr))
6664 return error_mark_node;
6666 if (processing_template_decl)
6668 tree t = build_min (CAST_EXPR, type,
6669 tree_cons (NULL_TREE, value, NULL_TREE));
6670 /* We don't know if it will or will not have side effects. */
6671 TREE_SIDE_EFFECTS (t) = 1;
6672 return convert_from_reference (t);
6675 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6676 'Class') should always be retained, because this information aids
6677 in method lookup. */
6678 if (objc_is_object_ptr (type)
6679 && objc_is_object_ptr (TREE_TYPE (expr)))
6680 return build_nop (type, expr);
6682 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6683 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6684 if (TREE_CODE (type) != REFERENCE_TYPE
6685 && TREE_CODE (value) == NOP_EXPR
6686 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6687 value = TREE_OPERAND (value, 0);
6689 if (TREE_CODE (type) == ARRAY_TYPE)
6691 /* Allow casting from T1* to T2[] because Cfront allows it.
6692 NIHCL uses it. It is not valid ISO C++ however. */
6693 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6695 if (complain & tf_error)
6696 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6697 else
6698 return error_mark_node;
6699 type = build_pointer_type (TREE_TYPE (type));
6701 else
6703 if (complain & tf_error)
6704 error ("ISO C++ forbids casting to an array type %qT", type);
6705 return error_mark_node;
6709 if (TREE_CODE (type) == FUNCTION_TYPE
6710 || TREE_CODE (type) == METHOD_TYPE)
6712 if (complain & tf_error)
6713 error ("invalid cast to function type %qT", type);
6714 return error_mark_node;
6717 if (TREE_CODE (type) == POINTER_TYPE
6718 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
6719 /* Casting to an integer of smaller size is an error detected elsewhere. */
6720 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
6721 /* Don't warn about converting any constant. */
6722 && !TREE_CONSTANT (value))
6723 warning_at (input_location, OPT_Wint_to_pointer_cast,
6724 "cast to pointer from integer of different size");
6726 /* A C-style cast can be a const_cast. */
6727 result = build_const_cast_1 (type, value, complain & tf_warning,
6728 &valid_p);
6729 if (valid_p)
6731 if (result != error_mark_node)
6732 maybe_warn_about_useless_cast (type, value, complain);
6733 return result;
6736 /* Or a static cast. */
6737 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
6738 &valid_p, complain);
6739 /* Or a reinterpret_cast. */
6740 if (!valid_p)
6741 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
6742 &valid_p, complain);
6743 /* The static_cast or reinterpret_cast may be followed by a
6744 const_cast. */
6745 if (valid_p
6746 /* A valid cast may result in errors if, for example, a
6747 conversion to an ambiguous base class is required. */
6748 && !error_operand_p (result))
6750 tree result_type;
6752 maybe_warn_about_useless_cast (type, value, complain);
6754 /* Non-class rvalues always have cv-unqualified type. */
6755 if (!CLASS_TYPE_P (type))
6756 type = TYPE_MAIN_VARIANT (type);
6757 result_type = TREE_TYPE (result);
6758 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
6759 result_type = TYPE_MAIN_VARIANT (result_type);
6760 /* If the type of RESULT does not match TYPE, perform a
6761 const_cast to make it match. If the static_cast or
6762 reinterpret_cast succeeded, we will differ by at most
6763 cv-qualification, so the follow-on const_cast is guaranteed
6764 to succeed. */
6765 if (!same_type_p (non_reference (type), non_reference (result_type)))
6767 result = build_const_cast_1 (type, result, false, &valid_p);
6768 gcc_assert (valid_p);
6770 return result;
6773 return error_mark_node;
6776 /* For use from the C common bits. */
6777 tree
6778 build_modify_expr (location_t location ATTRIBUTE_UNUSED,
6779 tree lhs, tree lhs_origtype ATTRIBUTE_UNUSED,
6780 enum tree_code modifycode,
6781 location_t rhs_location ATTRIBUTE_UNUSED, tree rhs,
6782 tree rhs_origtype ATTRIBUTE_UNUSED)
6784 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
6787 /* Build an assignment expression of lvalue LHS from value RHS.
6788 MODIFYCODE is the code for a binary operator that we use
6789 to combine the old value of LHS with RHS to get the new value.
6790 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6792 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
6794 tree
6795 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6796 tsubst_flags_t complain)
6798 tree result;
6799 tree newrhs = rhs;
6800 tree lhstype = TREE_TYPE (lhs);
6801 tree olhstype = lhstype;
6802 bool plain_assign = (modifycode == NOP_EXPR);
6804 /* Avoid duplicate error messages from operands that had errors. */
6805 if (error_operand_p (lhs) || error_operand_p (rhs))
6806 return error_mark_node;
6808 /* Handle control structure constructs used as "lvalues". */
6809 switch (TREE_CODE (lhs))
6811 /* Handle --foo = 5; as these are valid constructs in C++. */
6812 case PREDECREMENT_EXPR:
6813 case PREINCREMENT_EXPR:
6814 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6815 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6816 stabilize_reference (TREE_OPERAND (lhs, 0)),
6817 TREE_OPERAND (lhs, 1));
6818 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
6819 modifycode, rhs, complain);
6820 if (newrhs == error_mark_node)
6821 return error_mark_node;
6822 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6824 /* Handle (a, b) used as an "lvalue". */
6825 case COMPOUND_EXPR:
6826 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6827 modifycode, rhs, complain);
6828 if (newrhs == error_mark_node)
6829 return error_mark_node;
6830 return build2 (COMPOUND_EXPR, lhstype,
6831 TREE_OPERAND (lhs, 0), newrhs);
6833 case MODIFY_EXPR:
6834 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6835 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6836 stabilize_reference (TREE_OPERAND (lhs, 0)),
6837 TREE_OPERAND (lhs, 1));
6838 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
6839 complain);
6840 if (newrhs == error_mark_node)
6841 return error_mark_node;
6842 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6844 case MIN_EXPR:
6845 case MAX_EXPR:
6846 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
6847 when neither operand has side-effects. */
6848 if (!lvalue_or_else (lhs, lv_assign, complain))
6849 return error_mark_node;
6851 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
6852 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
6854 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
6855 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
6856 boolean_type_node,
6857 TREE_OPERAND (lhs, 0),
6858 TREE_OPERAND (lhs, 1)),
6859 TREE_OPERAND (lhs, 0),
6860 TREE_OPERAND (lhs, 1));
6861 /* Fall through. */
6863 /* Handle (a ? b : c) used as an "lvalue". */
6864 case COND_EXPR:
6866 /* Produce (a ? (b = rhs) : (c = rhs))
6867 except that the RHS goes through a save-expr
6868 so the code to compute it is only emitted once. */
6869 tree cond;
6870 tree preeval = NULL_TREE;
6872 if (VOID_TYPE_P (TREE_TYPE (rhs)))
6874 if (complain & tf_error)
6875 error ("void value not ignored as it ought to be");
6876 return error_mark_node;
6879 rhs = stabilize_expr (rhs, &preeval);
6881 /* Check this here to avoid odd errors when trying to convert
6882 a throw to the type of the COND_EXPR. */
6883 if (!lvalue_or_else (lhs, lv_assign, complain))
6884 return error_mark_node;
6886 cond = build_conditional_expr
6887 (TREE_OPERAND (lhs, 0),
6888 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6889 modifycode, rhs, complain),
6890 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
6891 modifycode, rhs, complain),
6892 complain);
6894 if (cond == error_mark_node)
6895 return cond;
6896 /* Make sure the code to compute the rhs comes out
6897 before the split. */
6898 if (preeval)
6899 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
6900 return cond;
6903 default:
6904 break;
6907 if (modifycode == INIT_EXPR)
6909 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6910 /* Do the default thing. */;
6911 else if (TREE_CODE (rhs) == CONSTRUCTOR)
6913 /* Compound literal. */
6914 if (! same_type_p (TREE_TYPE (rhs), lhstype))
6915 /* Call convert to generate an error; see PR 11063. */
6916 rhs = convert (lhstype, rhs);
6917 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
6918 TREE_SIDE_EFFECTS (result) = 1;
6919 return result;
6921 else if (! MAYBE_CLASS_TYPE_P (lhstype))
6922 /* Do the default thing. */;
6923 else
6925 VEC(tree,gc) *rhs_vec = make_tree_vector_single (rhs);
6926 result = build_special_member_call (lhs, complete_ctor_identifier,
6927 &rhs_vec, lhstype, LOOKUP_NORMAL,
6928 complain);
6929 release_tree_vector (rhs_vec);
6930 if (result == NULL_TREE)
6931 return error_mark_node;
6932 return result;
6935 else
6937 lhs = require_complete_type_sfinae (lhs, complain);
6938 if (lhs == error_mark_node)
6939 return error_mark_node;
6941 if (modifycode == NOP_EXPR)
6943 if (c_dialect_objc ())
6945 result = objc_maybe_build_modify_expr (lhs, rhs);
6946 if (result)
6947 return result;
6950 /* `operator=' is not an inheritable operator. */
6951 if (! MAYBE_CLASS_TYPE_P (lhstype))
6952 /* Do the default thing. */;
6953 else
6955 result = build_new_op (input_location, MODIFY_EXPR,
6956 LOOKUP_NORMAL, lhs, rhs,
6957 make_node (NOP_EXPR), /*overload=*/NULL,
6958 complain);
6959 if (result == NULL_TREE)
6960 return error_mark_node;
6961 return result;
6963 lhstype = olhstype;
6965 else
6967 tree init = NULL_TREE;
6969 /* A binary op has been requested. Combine the old LHS
6970 value with the RHS producing the value we should actually
6971 store into the LHS. */
6972 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
6973 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
6974 || MAYBE_CLASS_TYPE_P (lhstype)));
6976 /* Preevaluate the RHS to make sure its evaluation is complete
6977 before the lvalue-to-rvalue conversion of the LHS:
6979 [expr.ass] With respect to an indeterminately-sequenced
6980 function call, the operation of a compound assignment is a
6981 single evaluation. [ Note: Therefore, a function call shall
6982 not intervene between the lvalue-to-rvalue conversion and the
6983 side effect associated with any single compound assignment
6984 operator. -- end note ] */
6985 lhs = stabilize_reference (lhs);
6986 if (TREE_SIDE_EFFECTS (rhs))
6987 rhs = mark_rvalue_use (rhs);
6988 rhs = stabilize_expr (rhs, &init);
6989 newrhs = cp_build_binary_op (input_location,
6990 modifycode, lhs, rhs,
6991 complain);
6992 if (newrhs == error_mark_node)
6994 if (complain & tf_error)
6995 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
6996 TREE_TYPE (lhs), TREE_TYPE (rhs));
6997 return error_mark_node;
7000 if (init)
7001 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7003 /* Now it looks like a plain assignment. */
7004 modifycode = NOP_EXPR;
7005 if (c_dialect_objc ())
7007 result = objc_maybe_build_modify_expr (lhs, newrhs);
7008 if (result)
7009 return result;
7012 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7013 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7016 /* The left-hand side must be an lvalue. */
7017 if (!lvalue_or_else (lhs, lv_assign, complain))
7018 return error_mark_node;
7020 /* Warn about modifying something that is `const'. Don't warn if
7021 this is initialization. */
7022 if (modifycode != INIT_EXPR
7023 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7024 /* Functions are not modifiable, even though they are
7025 lvalues. */
7026 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7027 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7028 /* If it's an aggregate and any field is const, then it is
7029 effectively const. */
7030 || (CLASS_TYPE_P (lhstype)
7031 && C_TYPE_FIELDS_READONLY (lhstype))))
7033 if (complain & tf_error)
7034 cxx_readonly_error (lhs, lv_assign);
7035 else
7036 return error_mark_node;
7039 /* If storing into a structure or union member, it may have been given a
7040 lowered bitfield type. We need to convert to the declared type first,
7041 so retrieve it now. */
7043 olhstype = unlowered_expr_type (lhs);
7045 /* Convert new value to destination type. */
7047 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7049 int from_array;
7051 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7053 if (modifycode != INIT_EXPR)
7055 if (complain & tf_error)
7056 error ("assigning to an array from an initializer list");
7057 return error_mark_node;
7059 if (check_array_initializer (lhs, lhstype, newrhs))
7060 return error_mark_node;
7061 newrhs = digest_init (lhstype, newrhs, complain);
7062 if (newrhs == error_mark_node)
7063 return error_mark_node;
7066 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7067 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7069 if (complain & tf_error)
7070 error ("incompatible types in assignment of %qT to %qT",
7071 TREE_TYPE (rhs), lhstype);
7072 return error_mark_node;
7075 /* Allow array assignment in compiler-generated code. */
7076 else if (!current_function_decl
7077 || !DECL_DEFAULTED_FN (current_function_decl))
7079 /* This routine is used for both initialization and assignment.
7080 Make sure the diagnostic message differentiates the context. */
7081 if (complain & tf_error)
7083 if (modifycode == INIT_EXPR)
7084 error ("array used as initializer");
7085 else
7086 error ("invalid array assignment");
7088 return error_mark_node;
7091 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7092 ? 1 + (modifycode != INIT_EXPR): 0;
7093 return build_vec_init (lhs, NULL_TREE, newrhs,
7094 /*explicit_value_init_p=*/false,
7095 from_array, complain);
7098 if (modifycode == INIT_EXPR)
7099 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7100 LOOKUP_ONLYCONVERTING. */
7101 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7102 ICR_INIT, NULL_TREE, 0,
7103 complain);
7104 else
7105 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7106 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7108 if (!same_type_p (lhstype, olhstype))
7109 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7111 if (modifycode != INIT_EXPR)
7113 if (TREE_CODE (newrhs) == CALL_EXPR
7114 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7115 newrhs = build_cplus_new (lhstype, newrhs, complain);
7117 /* Can't initialize directly from a TARGET_EXPR, since that would
7118 cause the lhs to be constructed twice, and possibly result in
7119 accidental self-initialization. So we force the TARGET_EXPR to be
7120 expanded without a target. */
7121 if (TREE_CODE (newrhs) == TARGET_EXPR)
7122 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7123 TREE_OPERAND (newrhs, 0));
7126 if (newrhs == error_mark_node)
7127 return error_mark_node;
7129 if (c_dialect_objc () && flag_objc_gc)
7131 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7133 if (result)
7134 return result;
7137 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7138 lhstype, lhs, newrhs);
7140 TREE_SIDE_EFFECTS (result) = 1;
7141 if (!plain_assign)
7142 TREE_NO_WARNING (result) = 1;
7144 return result;
7147 tree
7148 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7149 tree rhs, tsubst_flags_t complain)
7151 if (processing_template_decl)
7152 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7153 build_min_nt_loc (loc, modifycode, NULL_TREE,
7154 NULL_TREE), rhs);
7156 if (modifycode != NOP_EXPR)
7158 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7159 make_node (modifycode), /*overload=*/NULL,
7160 complain);
7161 if (rval)
7163 TREE_NO_WARNING (rval) = 1;
7164 return rval;
7167 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7170 /* Helper function for get_delta_difference which assumes FROM is a base
7171 class of TO. Returns a delta for the conversion of pointer-to-member
7172 of FROM to pointer-to-member of TO. If the conversion is invalid and
7173 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7174 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7175 If C_CAST_P is true, this conversion is taking place as part of a
7176 C-style cast. */
7178 static tree
7179 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7180 tsubst_flags_t complain)
7182 tree binfo;
7183 base_kind kind;
7184 base_access access = c_cast_p ? ba_unique : ba_check;
7186 /* Note: ba_quiet does not distinguish between access control and
7187 ambiguity. */
7188 if (!(complain & tf_error))
7189 access |= ba_quiet;
7191 binfo = lookup_base (to, from, access, &kind);
7193 if (kind == bk_inaccessible || kind == bk_ambig)
7195 if (!(complain & tf_error))
7196 return error_mark_node;
7198 error (" in pointer to member function conversion");
7199 return size_zero_node;
7201 else if (binfo)
7203 if (kind != bk_via_virtual)
7204 return BINFO_OFFSET (binfo);
7205 else
7206 /* FROM is a virtual base class of TO. Issue an error or warning
7207 depending on whether or not this is a reinterpret cast. */
7209 if (!(complain & tf_error))
7210 return error_mark_node;
7212 error ("pointer to member conversion via virtual base %qT",
7213 BINFO_TYPE (binfo_from_vbase (binfo)));
7215 return size_zero_node;
7218 else
7219 return NULL_TREE;
7222 /* Get difference in deltas for different pointer to member function
7223 types. If the conversion is invalid and tf_error is not set in
7224 COMPLAIN, returns error_mark_node, otherwise returns an integer
7225 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7226 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7227 conversions as well. If C_CAST_P is true this conversion is taking
7228 place as part of a C-style cast.
7230 Note that the naming of FROM and TO is kind of backwards; the return
7231 value is what we add to a TO in order to get a FROM. They are named
7232 this way because we call this function to find out how to convert from
7233 a pointer to member of FROM to a pointer to member of TO. */
7235 static tree
7236 get_delta_difference (tree from, tree to,
7237 bool allow_inverse_p,
7238 bool c_cast_p, tsubst_flags_t complain)
7240 tree result;
7242 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7243 /* Pointer to member of incomplete class is permitted*/
7244 result = size_zero_node;
7245 else
7246 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7248 if (result == error_mark_node)
7249 return error_mark_node;
7251 if (!result)
7253 if (!allow_inverse_p)
7255 if (!(complain & tf_error))
7256 return error_mark_node;
7258 error_not_base_type (from, to);
7259 error (" in pointer to member conversion");
7260 result = size_zero_node;
7262 else
7264 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7266 if (result == error_mark_node)
7267 return error_mark_node;
7269 if (result)
7270 result = size_diffop_loc (input_location,
7271 size_zero_node, result);
7272 else
7274 if (!(complain & tf_error))
7275 return error_mark_node;
7277 error_not_base_type (from, to);
7278 error (" in pointer to member conversion");
7279 result = size_zero_node;
7284 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7285 result));
7288 /* Return a constructor for the pointer-to-member-function TYPE using
7289 the other components as specified. */
7291 tree
7292 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7294 tree u = NULL_TREE;
7295 tree delta_field;
7296 tree pfn_field;
7297 VEC(constructor_elt, gc) *v;
7299 /* Pull the FIELD_DECLs out of the type. */
7300 pfn_field = TYPE_FIELDS (type);
7301 delta_field = DECL_CHAIN (pfn_field);
7303 /* Make sure DELTA has the type we want. */
7304 delta = convert_and_check (delta_type_node, delta);
7306 /* Convert to the correct target type if necessary. */
7307 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7309 /* Finish creating the initializer. */
7310 v = VEC_alloc(constructor_elt, gc, 2);
7311 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7312 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7313 u = build_constructor (type, v);
7314 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7315 TREE_STATIC (u) = (TREE_CONSTANT (u)
7316 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7317 != NULL_TREE)
7318 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7319 != NULL_TREE));
7320 return u;
7323 /* Build a constructor for a pointer to member function. It can be
7324 used to initialize global variables, local variable, or used
7325 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7326 want to be.
7328 If FORCE is nonzero, then force this conversion, even if
7329 we would rather not do it. Usually set when using an explicit
7330 cast. A C-style cast is being processed iff C_CAST_P is true.
7332 Return error_mark_node, if something goes wrong. */
7334 tree
7335 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7336 tsubst_flags_t complain)
7338 tree fn;
7339 tree pfn_type;
7340 tree to_type;
7342 if (error_operand_p (pfn))
7343 return error_mark_node;
7345 pfn_type = TREE_TYPE (pfn);
7346 to_type = build_ptrmemfunc_type (type);
7348 /* Handle multiple conversions of pointer to member functions. */
7349 if (TYPE_PTRMEMFUNC_P (pfn_type))
7351 tree delta = NULL_TREE;
7352 tree npfn = NULL_TREE;
7353 tree n;
7355 if (!force
7356 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7357 LOOKUP_NORMAL, complain))
7358 error ("invalid conversion to type %qT from type %qT",
7359 to_type, pfn_type);
7361 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7362 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7363 force,
7364 c_cast_p, complain);
7365 if (n == error_mark_node)
7366 return error_mark_node;
7368 /* We don't have to do any conversion to convert a
7369 pointer-to-member to its own type. But, we don't want to
7370 just return a PTRMEM_CST if there's an explicit cast; that
7371 cast should make the expression an invalid template argument. */
7372 if (TREE_CODE (pfn) != PTRMEM_CST)
7374 if (same_type_p (to_type, pfn_type))
7375 return pfn;
7376 else if (integer_zerop (n))
7377 return build_reinterpret_cast (to_type, pfn,
7378 complain);
7381 if (TREE_SIDE_EFFECTS (pfn))
7382 pfn = save_expr (pfn);
7384 /* Obtain the function pointer and the current DELTA. */
7385 if (TREE_CODE (pfn) == PTRMEM_CST)
7386 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7387 else
7389 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7390 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7393 /* Just adjust the DELTA field. */
7394 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7395 (TREE_TYPE (delta), ptrdiff_type_node));
7396 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7397 n = cp_build_binary_op (input_location,
7398 LSHIFT_EXPR, n, integer_one_node,
7399 complain);
7400 delta = cp_build_binary_op (input_location,
7401 PLUS_EXPR, delta, n, complain);
7402 return build_ptrmemfunc1 (to_type, delta, npfn);
7405 /* Handle null pointer to member function conversions. */
7406 if (null_ptr_cst_p (pfn))
7408 pfn = build_c_cast (input_location, type, nullptr_node);
7409 return build_ptrmemfunc1 (to_type,
7410 integer_zero_node,
7411 pfn);
7414 if (type_unknown_p (pfn))
7415 return instantiate_type (type, pfn, complain);
7417 fn = TREE_OPERAND (pfn, 0);
7418 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7419 /* In a template, we will have preserved the
7420 OFFSET_REF. */
7421 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7422 return make_ptrmem_cst (to_type, fn);
7425 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7426 given by CST.
7428 ??? There is no consistency as to the types returned for the above
7429 values. Some code acts as if it were a sizetype and some as if it were
7430 integer_type_node. */
7432 void
7433 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7435 tree type = TREE_TYPE (cst);
7436 tree fn = PTRMEM_CST_MEMBER (cst);
7437 tree ptr_class, fn_class;
7439 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7441 /* The class that the function belongs to. */
7442 fn_class = DECL_CONTEXT (fn);
7444 /* The class that we're creating a pointer to member of. */
7445 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7447 /* First, calculate the adjustment to the function's class. */
7448 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7449 /*c_cast_p=*/0, tf_warning_or_error);
7451 if (!DECL_VIRTUAL_P (fn))
7452 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7453 build_addr_func (fn, tf_warning_or_error));
7454 else
7456 /* If we're dealing with a virtual function, we have to adjust 'this'
7457 again, to point to the base which provides the vtable entry for
7458 fn; the call will do the opposite adjustment. */
7459 tree orig_class = DECL_CONTEXT (fn);
7460 tree binfo = binfo_or_else (orig_class, fn_class);
7461 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7462 *delta, BINFO_OFFSET (binfo));
7463 *delta = fold_if_not_in_template (*delta);
7465 /* We set PFN to the vtable offset at which the function can be
7466 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7467 case delta is shifted left, and then incremented). */
7468 *pfn = DECL_VINDEX (fn);
7469 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7470 TYPE_SIZE_UNIT (vtable_entry_type));
7471 *pfn = fold_if_not_in_template (*pfn);
7473 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7475 case ptrmemfunc_vbit_in_pfn:
7476 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7477 integer_one_node);
7478 *pfn = fold_if_not_in_template (*pfn);
7479 break;
7481 case ptrmemfunc_vbit_in_delta:
7482 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7483 *delta, integer_one_node);
7484 *delta = fold_if_not_in_template (*delta);
7485 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7486 *delta, integer_one_node);
7487 *delta = fold_if_not_in_template (*delta);
7488 break;
7490 default:
7491 gcc_unreachable ();
7494 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7495 *pfn = fold_if_not_in_template (*pfn);
7499 /* Return an expression for PFN from the pointer-to-member function
7500 given by T. */
7502 static tree
7503 pfn_from_ptrmemfunc (tree t)
7505 if (TREE_CODE (t) == PTRMEM_CST)
7507 tree delta;
7508 tree pfn;
7510 expand_ptrmemfunc_cst (t, &delta, &pfn);
7511 if (pfn)
7512 return pfn;
7515 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7518 /* Return an expression for DELTA from the pointer-to-member function
7519 given by T. */
7521 static tree
7522 delta_from_ptrmemfunc (tree t)
7524 if (TREE_CODE (t) == PTRMEM_CST)
7526 tree delta;
7527 tree pfn;
7529 expand_ptrmemfunc_cst (t, &delta, &pfn);
7530 if (delta)
7531 return delta;
7534 return build_ptrmemfunc_access_expr (t, delta_identifier);
7537 /* Convert value RHS to type TYPE as preparation for an assignment to
7538 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7539 implicit conversion is. If FNDECL is non-NULL, we are doing the
7540 conversion in order to pass the PARMNUMth argument of FNDECL.
7541 If FNDECL is NULL, we are doing the conversion in function pointer
7542 argument passing, conversion in initialization, etc. */
7544 static tree
7545 convert_for_assignment (tree type, tree rhs,
7546 impl_conv_rhs errtype, tree fndecl, int parmnum,
7547 tsubst_flags_t complain, int flags)
7549 tree rhstype;
7550 enum tree_code coder;
7552 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7553 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7554 rhs = TREE_OPERAND (rhs, 0);
7556 rhstype = TREE_TYPE (rhs);
7557 coder = TREE_CODE (rhstype);
7559 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7560 && vector_types_convertible_p (type, rhstype, true))
7562 rhs = mark_rvalue_use (rhs);
7563 return convert (type, rhs);
7566 if (rhs == error_mark_node || rhstype == error_mark_node)
7567 return error_mark_node;
7568 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7569 return error_mark_node;
7571 /* The RHS of an assignment cannot have void type. */
7572 if (coder == VOID_TYPE)
7574 if (complain & tf_error)
7575 error ("void value not ignored as it ought to be");
7576 return error_mark_node;
7579 if (c_dialect_objc ())
7581 int parmno;
7582 tree selector;
7583 tree rname = fndecl;
7585 switch (errtype)
7587 case ICR_ASSIGN:
7588 parmno = -1;
7589 break;
7590 case ICR_INIT:
7591 parmno = -2;
7592 break;
7593 default:
7594 selector = objc_message_selector ();
7595 parmno = parmnum;
7596 if (selector && parmno > 1)
7598 rname = selector;
7599 parmno -= 1;
7603 if (objc_compare_types (type, rhstype, parmno, rname))
7605 rhs = mark_rvalue_use (rhs);
7606 return convert (type, rhs);
7610 /* [expr.ass]
7612 The expression is implicitly converted (clause _conv_) to the
7613 cv-unqualified type of the left operand.
7615 We allow bad conversions here because by the time we get to this point
7616 we are committed to doing the conversion. If we end up doing a bad
7617 conversion, convert_like will complain. */
7618 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
7620 /* When -Wno-pmf-conversions is use, we just silently allow
7621 conversions from pointers-to-members to plain pointers. If
7622 the conversion doesn't work, cp_convert will complain. */
7623 if (!warn_pmf2ptr
7624 && TYPE_PTR_P (type)
7625 && TYPE_PTRMEMFUNC_P (rhstype))
7626 rhs = cp_convert (strip_top_quals (type), rhs, complain);
7627 else
7629 if (complain & tf_error)
7631 /* If the right-hand side has unknown type, then it is an
7632 overloaded function. Call instantiate_type to get error
7633 messages. */
7634 if (rhstype == unknown_type_node)
7635 instantiate_type (type, rhs, tf_warning_or_error);
7636 else if (fndecl)
7637 error ("cannot convert %qT to %qT for argument %qP to %qD",
7638 rhstype, type, parmnum, fndecl);
7639 else
7640 switch (errtype)
7642 case ICR_DEFAULT_ARGUMENT:
7643 error ("cannot convert %qT to %qT in default argument",
7644 rhstype, type);
7645 break;
7646 case ICR_ARGPASS:
7647 error ("cannot convert %qT to %qT in argument passing",
7648 rhstype, type);
7649 break;
7650 case ICR_CONVERTING:
7651 error ("cannot convert %qT to %qT",
7652 rhstype, type);
7653 break;
7654 case ICR_INIT:
7655 error ("cannot convert %qT to %qT in initialization",
7656 rhstype, type);
7657 break;
7658 case ICR_RETURN:
7659 error ("cannot convert %qT to %qT in return",
7660 rhstype, type);
7661 break;
7662 case ICR_ASSIGN:
7663 error ("cannot convert %qT to %qT in assignment",
7664 rhstype, type);
7665 break;
7666 default:
7667 gcc_unreachable();
7670 return error_mark_node;
7673 if (warn_suggest_attribute_format)
7675 const enum tree_code codel = TREE_CODE (type);
7676 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7677 && coder == codel
7678 && check_missing_format_attribute (type, rhstype)
7679 && (complain & tf_warning))
7680 switch (errtype)
7682 case ICR_ARGPASS:
7683 case ICR_DEFAULT_ARGUMENT:
7684 if (fndecl)
7685 warning (OPT_Wsuggest_attribute_format,
7686 "parameter %qP of %qD might be a candidate "
7687 "for a format attribute", parmnum, fndecl);
7688 else
7689 warning (OPT_Wsuggest_attribute_format,
7690 "parameter might be a candidate "
7691 "for a format attribute");
7692 break;
7693 case ICR_CONVERTING:
7694 warning (OPT_Wsuggest_attribute_format,
7695 "target of conversion might be a candidate "
7696 "for a format attribute");
7697 break;
7698 case ICR_INIT:
7699 warning (OPT_Wsuggest_attribute_format,
7700 "target of initialization might be a candidate "
7701 "for a format attribute");
7702 break;
7703 case ICR_RETURN:
7704 warning (OPT_Wsuggest_attribute_format,
7705 "return type might be a candidate "
7706 "for a format attribute");
7707 break;
7708 case ICR_ASSIGN:
7709 warning (OPT_Wsuggest_attribute_format,
7710 "left-hand side of assignment might be a candidate "
7711 "for a format attribute");
7712 break;
7713 default:
7714 gcc_unreachable();
7718 /* If -Wparentheses, warn about a = b = c when a has type bool and b
7719 does not. */
7720 if (warn_parentheses
7721 && TREE_CODE (type) == BOOLEAN_TYPE
7722 && TREE_CODE (rhs) == MODIFY_EXPR
7723 && !TREE_NO_WARNING (rhs)
7724 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7725 && (complain & tf_warning))
7727 location_t loc = EXPR_LOC_OR_HERE (rhs);
7729 warning_at (loc, OPT_Wparentheses,
7730 "suggest parentheses around assignment used as truth value");
7731 TREE_NO_WARNING (rhs) = 1;
7734 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
7735 complain, flags);
7738 /* Convert RHS to be of type TYPE.
7739 If EXP is nonzero, it is the target of the initialization.
7740 ERRTYPE indicates what kind of error the implicit conversion is.
7742 Two major differences between the behavior of
7743 `convert_for_assignment' and `convert_for_initialization'
7744 are that references are bashed in the former, while
7745 copied in the latter, and aggregates are assigned in
7746 the former (operator=) while initialized in the
7747 latter (X(X&)).
7749 If using constructor make sure no conversion operator exists, if one does
7750 exist, an ambiguity exists. */
7752 tree
7753 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
7754 impl_conv_rhs errtype, tree fndecl, int parmnum,
7755 tsubst_flags_t complain)
7757 enum tree_code codel = TREE_CODE (type);
7758 tree rhstype;
7759 enum tree_code coder;
7761 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7762 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7763 if (TREE_CODE (rhs) == NOP_EXPR
7764 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7765 && codel != REFERENCE_TYPE)
7766 rhs = TREE_OPERAND (rhs, 0);
7768 if (type == error_mark_node
7769 || rhs == error_mark_node
7770 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7771 return error_mark_node;
7773 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7774 && TREE_CODE (type) != ARRAY_TYPE
7775 && (TREE_CODE (type) != REFERENCE_TYPE
7776 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7777 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7778 && (TREE_CODE (type) != REFERENCE_TYPE
7779 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7780 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7781 rhs = decay_conversion (rhs, complain);
7783 rhstype = TREE_TYPE (rhs);
7784 coder = TREE_CODE (rhstype);
7786 if (coder == ERROR_MARK)
7787 return error_mark_node;
7789 /* We accept references to incomplete types, so we can
7790 return here before checking if RHS is of complete type. */
7792 if (codel == REFERENCE_TYPE)
7794 /* This should eventually happen in convert_arguments. */
7795 int savew = 0, savee = 0;
7797 if (fndecl)
7798 savew = warningcount, savee = errorcount;
7799 rhs = initialize_reference (type, rhs, flags, complain);
7800 if (fndecl)
7802 if (warningcount > savew)
7803 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
7804 else if (errorcount > savee)
7805 error ("in passing argument %P of %q+D", parmnum, fndecl);
7807 return rhs;
7810 if (exp != 0)
7811 exp = require_complete_type_sfinae (exp, complain);
7812 if (exp == error_mark_node)
7813 return error_mark_node;
7815 rhstype = non_reference (rhstype);
7817 type = complete_type (type);
7819 if (DIRECT_INIT_EXPR_P (type, rhs))
7820 /* Don't try to do copy-initialization if we already have
7821 direct-initialization. */
7822 return rhs;
7824 if (MAYBE_CLASS_TYPE_P (type))
7825 return perform_implicit_conversion_flags (type, rhs, complain, flags);
7827 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
7828 complain, flags);
7831 /* If RETVAL is the address of, or a reference to, a local variable or
7832 temporary give an appropriate warning. */
7834 static void
7835 maybe_warn_about_returning_address_of_local (tree retval)
7837 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
7838 tree whats_returned = retval;
7840 for (;;)
7842 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7843 whats_returned = TREE_OPERAND (whats_returned, 1);
7844 else if (CONVERT_EXPR_P (whats_returned)
7845 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
7846 whats_returned = TREE_OPERAND (whats_returned, 0);
7847 else
7848 break;
7851 if (TREE_CODE (whats_returned) != ADDR_EXPR)
7852 return;
7853 whats_returned = TREE_OPERAND (whats_returned, 0);
7855 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7857 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7858 || TREE_CODE (whats_returned) == TARGET_EXPR)
7860 warning (0, "returning reference to temporary");
7861 return;
7863 if (TREE_CODE (whats_returned) == VAR_DECL
7864 && DECL_NAME (whats_returned)
7865 && TEMP_NAME_P (DECL_NAME (whats_returned)))
7867 warning (0, "reference to non-lvalue returned");
7868 return;
7872 while (TREE_CODE (whats_returned) == COMPONENT_REF
7873 || TREE_CODE (whats_returned) == ARRAY_REF)
7874 whats_returned = TREE_OPERAND (whats_returned, 0);
7876 if (DECL_P (whats_returned)
7877 && DECL_NAME (whats_returned)
7878 && DECL_FUNCTION_SCOPE_P (whats_returned)
7879 && !(TREE_STATIC (whats_returned)
7880 || TREE_PUBLIC (whats_returned)))
7882 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7883 warning (0, "reference to local variable %q+D returned",
7884 whats_returned);
7885 else
7886 warning (0, "address of local variable %q+D returned",
7887 whats_returned);
7888 return;
7892 /* Check that returning RETVAL from the current function is valid.
7893 Return an expression explicitly showing all conversions required to
7894 change RETVAL into the function return type, and to assign it to
7895 the DECL_RESULT for the function. Set *NO_WARNING to true if
7896 code reaches end of non-void function warning shouldn't be issued
7897 on this RETURN_EXPR. */
7899 tree
7900 check_return_expr (tree retval, bool *no_warning)
7902 tree result;
7903 /* The type actually returned by the function. */
7904 tree valtype;
7905 /* The type the function is declared to return, or void if
7906 the declared type is incomplete. */
7907 tree functype;
7908 int fn_returns_value_p;
7909 bool named_return_value_okay_p;
7911 *no_warning = false;
7913 /* A `volatile' function is one that isn't supposed to return, ever.
7914 (This is a G++ extension, used to get better code for functions
7915 that call the `volatile' function.) */
7916 if (TREE_THIS_VOLATILE (current_function_decl))
7917 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7919 /* Check for various simple errors. */
7920 if (DECL_DESTRUCTOR_P (current_function_decl))
7922 if (retval)
7923 error ("returning a value from a destructor");
7924 return NULL_TREE;
7926 else if (DECL_CONSTRUCTOR_P (current_function_decl))
7928 if (in_function_try_handler)
7929 /* If a return statement appears in a handler of the
7930 function-try-block of a constructor, the program is ill-formed. */
7931 error ("cannot return from a handler of a function-try-block of a constructor");
7932 else if (retval)
7933 /* You can't return a value from a constructor. */
7934 error ("returning a value from a constructor");
7935 return NULL_TREE;
7938 if (processing_template_decl)
7940 current_function_returns_value = 1;
7941 if (check_for_bare_parameter_packs (retval))
7942 retval = error_mark_node;
7943 return retval;
7946 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
7948 /* Deduce auto return type from a return statement. */
7949 if (current_function_auto_return_pattern)
7951 tree auto_node;
7952 tree type;
7954 if (!retval && !is_auto (current_function_auto_return_pattern))
7956 /* Give a helpful error message. */
7957 error ("return-statement with no value, in function returning %qT",
7958 current_function_auto_return_pattern);
7959 inform (input_location, "only plain %<auto%> return type can be "
7960 "deduced to %<void%>");
7961 type = error_mark_node;
7963 else
7965 if (!retval)
7966 retval = void_zero_node;
7967 auto_node = type_uses_auto (current_function_auto_return_pattern);
7968 type = do_auto_deduction (current_function_auto_return_pattern,
7969 retval, auto_node);
7972 if (type == error_mark_node)
7973 /* Leave it. */;
7974 else if (functype == current_function_auto_return_pattern)
7975 apply_deduced_return_type (current_function_decl, type);
7976 else
7977 /* A mismatch should have been diagnosed in do_auto_deduction. */
7978 gcc_assert (same_type_p (type, functype));
7979 functype = type;
7982 /* When no explicit return-value is given in a function with a named
7983 return value, the named return value is used. */
7984 result = DECL_RESULT (current_function_decl);
7985 valtype = TREE_TYPE (result);
7986 gcc_assert (valtype != NULL_TREE);
7987 fn_returns_value_p = !VOID_TYPE_P (valtype);
7988 if (!retval && DECL_NAME (result) && fn_returns_value_p)
7989 retval = result;
7991 /* Check for a return statement with no return value in a function
7992 that's supposed to return a value. */
7993 if (!retval && fn_returns_value_p)
7995 if (functype != error_mark_node)
7996 permerror (input_location, "return-statement with no value, in "
7997 "function returning %qT", valtype);
7998 /* Remember that this function did return. */
7999 current_function_returns_value = 1;
8000 /* And signal caller that TREE_NO_WARNING should be set on the
8001 RETURN_EXPR to avoid control reaches end of non-void function
8002 warnings in tree-cfg.c. */
8003 *no_warning = true;
8005 /* Check for a return statement with a value in a function that
8006 isn't supposed to return a value. */
8007 else if (retval && !fn_returns_value_p)
8009 if (VOID_TYPE_P (TREE_TYPE (retval)))
8010 /* You can return a `void' value from a function of `void'
8011 type. In that case, we have to evaluate the expression for
8012 its side-effects. */
8013 finish_expr_stmt (retval);
8014 else
8015 permerror (input_location, "return-statement with a value, in function "
8016 "returning 'void'");
8017 current_function_returns_null = 1;
8019 /* There's really no value to return, after all. */
8020 return NULL_TREE;
8022 else if (!retval)
8023 /* Remember that this function can sometimes return without a
8024 value. */
8025 current_function_returns_null = 1;
8026 else
8027 /* Remember that this function did return a value. */
8028 current_function_returns_value = 1;
8030 /* Check for erroneous operands -- but after giving ourselves a
8031 chance to provide an error about returning a value from a void
8032 function. */
8033 if (error_operand_p (retval))
8035 current_function_return_value = error_mark_node;
8036 return error_mark_node;
8039 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8040 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8041 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8042 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8043 && ! flag_check_new
8044 && retval && null_ptr_cst_p (retval))
8045 warning (0, "%<operator new%> must not return NULL unless it is "
8046 "declared %<throw()%> (or -fcheck-new is in effect)");
8048 /* Effective C++ rule 15. See also start_function. */
8049 if (warn_ecpp
8050 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8052 bool warn = true;
8054 /* The function return type must be a reference to the current
8055 class. */
8056 if (TREE_CODE (valtype) == REFERENCE_TYPE
8057 && same_type_ignoring_top_level_qualifiers_p
8058 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8060 /* Returning '*this' is obviously OK. */
8061 if (retval == current_class_ref)
8062 warn = false;
8063 /* If we are calling a function whose return type is the same of
8064 the current class reference, it is ok. */
8065 else if (TREE_CODE (retval) == INDIRECT_REF
8066 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8067 warn = false;
8070 if (warn)
8071 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8074 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8076 [...] For a function with a class return type, if the expression
8077 in the return statement is the name of a local object, and the cv-
8078 unqualified type of the local object is the same as the function
8079 return type, an implementation is permitted to omit creating the tem-
8080 porary object to hold the function return value [...]
8082 So, if this is a value-returning function that always returns the same
8083 local variable, remember it.
8085 It might be nice to be more flexible, and choose the first suitable
8086 variable even if the function sometimes returns something else, but
8087 then we run the risk of clobbering the variable we chose if the other
8088 returned expression uses the chosen variable somehow. And people expect
8089 this restriction, anyway. (jason 2000-11-19)
8091 See finish_function and finalize_nrv for the rest of this optimization. */
8093 named_return_value_okay_p =
8094 (retval != NULL_TREE
8095 /* Must be a local, automatic variable. */
8096 && TREE_CODE (retval) == VAR_DECL
8097 && DECL_CONTEXT (retval) == current_function_decl
8098 && ! TREE_STATIC (retval)
8099 && ! DECL_ANON_UNION_VAR_P (retval)
8100 && (DECL_ALIGN (retval) >= DECL_ALIGN (result))
8101 /* The cv-unqualified type of the returned value must be the
8102 same as the cv-unqualified return type of the
8103 function. */
8104 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8105 (TYPE_MAIN_VARIANT (functype)))
8106 /* And the returned value must be non-volatile. */
8107 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8109 if (fn_returns_value_p && flag_elide_constructors)
8111 if (named_return_value_okay_p
8112 && (current_function_return_value == NULL_TREE
8113 || current_function_return_value == retval))
8114 current_function_return_value = retval;
8115 else
8116 current_function_return_value = error_mark_node;
8119 /* We don't need to do any conversions when there's nothing being
8120 returned. */
8121 if (!retval)
8122 return NULL_TREE;
8124 /* Do any required conversions. */
8125 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8126 /* No conversions are required. */
8128 else
8130 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8132 /* The functype's return type will have been set to void, if it
8133 was an incomplete type. Just treat this as 'return;' */
8134 if (VOID_TYPE_P (functype))
8135 return error_mark_node;
8137 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8138 treated as an rvalue for the purposes of overload resolution to
8139 favor move constructors over copy constructors.
8141 Note that these conditions are similar to, but not as strict as,
8142 the conditions for the named return value optimization. */
8143 if ((cxx_dialect != cxx98)
8144 && (TREE_CODE (retval) == VAR_DECL
8145 || TREE_CODE (retval) == PARM_DECL)
8146 && DECL_CONTEXT (retval) == current_function_decl
8147 && !TREE_STATIC (retval)
8148 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8149 (TYPE_MAIN_VARIANT (functype)))
8150 /* This is only interesting for class type. */
8151 && CLASS_TYPE_P (functype))
8152 flags = flags | LOOKUP_PREFER_RVALUE;
8154 /* First convert the value to the function's return type, then
8155 to the type of return value's location to handle the
8156 case that functype is smaller than the valtype. */
8157 retval = convert_for_initialization
8158 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8159 tf_warning_or_error);
8160 retval = convert (valtype, retval);
8162 /* If the conversion failed, treat this just like `return;'. */
8163 if (retval == error_mark_node)
8164 return retval;
8165 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8166 else if (! cfun->returns_struct
8167 && TREE_CODE (retval) == TARGET_EXPR
8168 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8169 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8170 TREE_OPERAND (retval, 0));
8171 else
8172 maybe_warn_about_returning_address_of_local (retval);
8175 /* Actually copy the value returned into the appropriate location. */
8176 if (retval && retval != result)
8177 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8179 return retval;
8183 /* Returns nonzero if the pointer-type FROM can be converted to the
8184 pointer-type TO via a qualification conversion. If CONSTP is -1,
8185 then we return nonzero if the pointers are similar, and the
8186 cv-qualification signature of FROM is a proper subset of that of TO.
8188 If CONSTP is positive, then all outer pointers have been
8189 const-qualified. */
8191 static int
8192 comp_ptr_ttypes_real (tree to, tree from, int constp)
8194 bool to_more_cv_qualified = false;
8195 bool is_opaque_pointer = false;
8197 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8199 if (TREE_CODE (to) != TREE_CODE (from))
8200 return 0;
8202 if (TREE_CODE (from) == OFFSET_TYPE
8203 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8204 TYPE_OFFSET_BASETYPE (to)))
8205 return 0;
8207 /* Const and volatile mean something different for function types,
8208 so the usual checks are not appropriate. */
8209 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8211 if (!at_least_as_qualified_p (to, from))
8212 return 0;
8214 if (!at_least_as_qualified_p (from, to))
8216 if (constp == 0)
8217 return 0;
8218 to_more_cv_qualified = true;
8221 if (constp > 0)
8222 constp &= TYPE_READONLY (to);
8225 if (TREE_CODE (to) == VECTOR_TYPE)
8226 is_opaque_pointer = vector_targets_convertible_p (to, from);
8228 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRDATAMEM_P (to))
8229 return ((constp >= 0 || to_more_cv_qualified)
8230 && (is_opaque_pointer
8231 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8235 /* When comparing, say, char ** to char const **, this function takes
8236 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8237 types to this function. */
8240 comp_ptr_ttypes (tree to, tree from)
8242 return comp_ptr_ttypes_real (to, from, 1);
8245 /* Returns true iff FNTYPE is a non-class type that involves
8246 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8247 if a parameter type is ill-formed. */
8249 bool
8250 error_type_p (const_tree type)
8252 tree t;
8254 switch (TREE_CODE (type))
8256 case ERROR_MARK:
8257 return true;
8259 case POINTER_TYPE:
8260 case REFERENCE_TYPE:
8261 case OFFSET_TYPE:
8262 return error_type_p (TREE_TYPE (type));
8264 case FUNCTION_TYPE:
8265 case METHOD_TYPE:
8266 if (error_type_p (TREE_TYPE (type)))
8267 return true;
8268 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8269 if (error_type_p (TREE_VALUE (t)))
8270 return true;
8271 return false;
8273 case RECORD_TYPE:
8274 if (TYPE_PTRMEMFUNC_P (type))
8275 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8276 return false;
8278 default:
8279 return false;
8283 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8284 type or inheritance-related types, regardless of cv-quals. */
8287 ptr_reasonably_similar (const_tree to, const_tree from)
8289 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8291 /* Any target type is similar enough to void. */
8292 if (TREE_CODE (to) == VOID_TYPE)
8293 return !error_type_p (from);
8294 if (TREE_CODE (from) == VOID_TYPE)
8295 return !error_type_p (to);
8297 if (TREE_CODE (to) != TREE_CODE (from))
8298 return 0;
8300 if (TREE_CODE (from) == OFFSET_TYPE
8301 && comptypes (TYPE_OFFSET_BASETYPE (to),
8302 TYPE_OFFSET_BASETYPE (from),
8303 COMPARE_BASE | COMPARE_DERIVED))
8304 continue;
8306 if (TREE_CODE (to) == VECTOR_TYPE
8307 && vector_types_convertible_p (to, from, false))
8308 return 1;
8310 if (TREE_CODE (to) == INTEGER_TYPE
8311 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8312 return 1;
8314 if (TREE_CODE (to) == FUNCTION_TYPE)
8315 return !error_type_p (to) && !error_type_p (from);
8317 if (TREE_CODE (to) != POINTER_TYPE)
8318 return comptypes
8319 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8320 COMPARE_BASE | COMPARE_DERIVED);
8324 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8325 pointer-to-member types) are the same, ignoring cv-qualification at
8326 all levels. */
8328 bool
8329 comp_ptr_ttypes_const (tree to, tree from)
8331 bool is_opaque_pointer = false;
8333 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8335 if (TREE_CODE (to) != TREE_CODE (from))
8336 return false;
8338 if (TREE_CODE (from) == OFFSET_TYPE
8339 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8340 TYPE_OFFSET_BASETYPE (to)))
8341 continue;
8343 if (TREE_CODE (to) == VECTOR_TYPE)
8344 is_opaque_pointer = vector_targets_convertible_p (to, from);
8346 if (TREE_CODE (to) != POINTER_TYPE)
8347 return (is_opaque_pointer
8348 || same_type_ignoring_top_level_qualifiers_p (to, from));
8352 /* Returns the type qualifiers for this type, including the qualifiers on the
8353 elements for an array type. */
8356 cp_type_quals (const_tree type)
8358 int quals;
8359 /* This CONST_CAST is okay because strip_array_types returns its
8360 argument unmodified and we assign it to a const_tree. */
8361 type = strip_array_types (CONST_CAST_TREE (type));
8362 if (type == error_mark_node
8363 /* Quals on a FUNCTION_TYPE are memfn quals. */
8364 || TREE_CODE (type) == FUNCTION_TYPE)
8365 return TYPE_UNQUALIFIED;
8366 quals = TYPE_QUALS (type);
8367 /* METHOD and REFERENCE_TYPEs should never have quals. */
8368 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8369 && TREE_CODE (type) != REFERENCE_TYPE)
8370 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8371 == TYPE_UNQUALIFIED));
8372 return quals;
8375 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8376 METHOD_TYPE. */
8379 type_memfn_quals (const_tree type)
8381 if (TREE_CODE (type) == FUNCTION_TYPE)
8382 return TYPE_QUALS (type);
8383 else if (TREE_CODE (type) == METHOD_TYPE)
8384 return cp_type_quals (class_of_this_parm (type));
8385 else
8386 gcc_unreachable ();
8389 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8390 MEMFN_QUALS. */
8392 tree
8393 apply_memfn_quals (tree type, cp_cv_quals memfn_quals)
8395 /* Could handle METHOD_TYPE here if necessary. */
8396 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8397 if (TYPE_QUALS (type) == memfn_quals)
8398 return type;
8399 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8400 complex. */
8401 return build_qualified_type (type, memfn_quals);
8404 /* Returns nonzero if TYPE is const or volatile. */
8406 bool
8407 cv_qualified_p (const_tree type)
8409 int quals = cp_type_quals (type);
8410 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8413 /* Returns nonzero if the TYPE contains a mutable member. */
8415 bool
8416 cp_has_mutable_p (const_tree type)
8418 /* This CONST_CAST is okay because strip_array_types returns its
8419 argument unmodified and we assign it to a const_tree. */
8420 type = strip_array_types (CONST_CAST_TREE(type));
8422 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8425 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8426 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8427 approximation. In particular, consider:
8429 int f();
8430 struct S { int i; };
8431 const S s = { f(); }
8433 Here, we will make "s" as TREE_READONLY (because it is declared
8434 "const") -- only to reverse ourselves upon seeing that the
8435 initializer is non-constant. */
8437 void
8438 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8440 tree type = TREE_TYPE (decl);
8442 if (type == error_mark_node)
8443 return;
8445 if (TREE_CODE (decl) == TYPE_DECL)
8446 return;
8448 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8449 && type_quals != TYPE_UNQUALIFIED));
8451 /* Avoid setting TREE_READONLY incorrectly. */
8452 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8453 constructor can produce constant init, so rely on cp_finish_decl to
8454 clear TREE_READONLY if the variable has non-constant init. */
8456 /* If the type has (or might have) a mutable component, that component
8457 might be modified. */
8458 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8459 type_quals &= ~TYPE_QUAL_CONST;
8461 c_apply_type_quals_to_decl (type_quals, decl);
8464 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8465 exemplar types such that casting T1 to T2 is casting away constness
8466 if and only if there is no implicit conversion from T1 to T2. */
8468 static void
8469 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8471 int quals1;
8472 int quals2;
8474 /* [expr.const.cast]
8476 For multi-level pointer to members and multi-level mixed pointers
8477 and pointers to members (conv.qual), the "member" aspect of a
8478 pointer to member level is ignored when determining if a const
8479 cv-qualifier has been cast away. */
8480 /* [expr.const.cast]
8482 For two pointer types:
8484 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8485 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8486 K is min(N,M)
8488 casting from X1 to X2 casts away constness if, for a non-pointer
8489 type T there does not exist an implicit conversion (clause
8490 _conv_) from:
8492 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8496 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8497 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8498 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8500 *t1 = cp_build_qualified_type (void_type_node,
8501 cp_type_quals (*t1));
8502 *t2 = cp_build_qualified_type (void_type_node,
8503 cp_type_quals (*t2));
8504 return;
8507 quals1 = cp_type_quals (*t1);
8508 quals2 = cp_type_quals (*t2);
8510 if (TYPE_PTRDATAMEM_P (*t1))
8511 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8512 else
8513 *t1 = TREE_TYPE (*t1);
8514 if (TYPE_PTRDATAMEM_P (*t2))
8515 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8516 else
8517 *t2 = TREE_TYPE (*t2);
8519 casts_away_constness_r (t1, t2, complain);
8520 *t1 = build_pointer_type (*t1);
8521 *t2 = build_pointer_type (*t2);
8522 *t1 = cp_build_qualified_type (*t1, quals1);
8523 *t2 = cp_build_qualified_type (*t2, quals2);
8526 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8527 constness.
8529 ??? This function returns non-zero if casting away qualifiers not
8530 just const. We would like to return to the caller exactly which
8531 qualifiers are casted away to give more accurate diagnostics.
8534 static bool
8535 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8537 if (TREE_CODE (t2) == REFERENCE_TYPE)
8539 /* [expr.const.cast]
8541 Casting from an lvalue of type T1 to an lvalue of type T2
8542 using a reference cast casts away constness if a cast from an
8543 rvalue of type "pointer to T1" to the type "pointer to T2"
8544 casts away constness. */
8545 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8546 return casts_away_constness (build_pointer_type (t1),
8547 build_pointer_type (TREE_TYPE (t2)),
8548 complain);
8551 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
8552 /* [expr.const.cast]
8554 Casting from an rvalue of type "pointer to data member of X
8555 of type T1" to the type "pointer to data member of Y of type
8556 T2" casts away constness if a cast from an rvalue of type
8557 "pointer to T1" to the type "pointer to T2" casts away
8558 constness. */
8559 return casts_away_constness
8560 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8561 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
8562 complain);
8564 /* Casting away constness is only something that makes sense for
8565 pointer or reference types. */
8566 if (TREE_CODE (t1) != POINTER_TYPE
8567 || TREE_CODE (t2) != POINTER_TYPE)
8568 return false;
8570 /* Top-level qualifiers don't matter. */
8571 t1 = TYPE_MAIN_VARIANT (t1);
8572 t2 = TYPE_MAIN_VARIANT (t2);
8573 casts_away_constness_r (&t1, &t2, complain);
8574 if (!can_convert (t2, t1, complain))
8575 return true;
8577 return false;
8580 /* If T is a REFERENCE_TYPE return the type to which T refers.
8581 Otherwise, return T itself. */
8583 tree
8584 non_reference (tree t)
8586 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8587 t = TREE_TYPE (t);
8588 return t;
8592 /* Return nonzero if REF is an lvalue valid for this language;
8593 otherwise, print an error message and return zero. USE says
8594 how the lvalue is being used and so selects the error message. */
8597 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8599 cp_lvalue_kind kind = lvalue_kind (ref);
8601 if (kind == clk_none)
8603 if (complain & tf_error)
8604 lvalue_error (input_location, use);
8605 return 0;
8607 else if (kind & (clk_rvalueref|clk_class))
8609 if (!(complain & tf_error))
8610 return 0;
8611 if (kind & clk_class)
8612 /* Make this a permerror because we used to accept it. */
8613 permerror (input_location, "using temporary as lvalue");
8614 else
8615 error ("using xvalue (rvalue reference) as lvalue");
8617 return 1;
8620 /* Return true if a user-defined literal operator is a raw operator. */
8622 bool
8623 check_raw_literal_operator (const_tree decl)
8625 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8626 tree argtype;
8627 int arity;
8628 bool maybe_raw_p = false;
8630 /* Count the number and type of arguments and check for ellipsis. */
8631 for (argtype = argtypes, arity = 0;
8632 argtype && argtype != void_list_node;
8633 ++arity, argtype = TREE_CHAIN (argtype))
8635 tree t = TREE_VALUE (argtype);
8637 if (same_type_p (t, const_string_type_node))
8638 maybe_raw_p = true;
8640 if (!argtype)
8641 return false; /* Found ellipsis. */
8643 if (!maybe_raw_p || arity != 1)
8644 return false;
8646 return true;
8650 /* Return true if a user-defined literal operator has one of the allowed
8651 argument types. */
8653 bool
8654 check_literal_operator_args (const_tree decl,
8655 bool *long_long_unsigned_p, bool *long_double_p)
8657 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8659 *long_long_unsigned_p = false;
8660 *long_double_p = false;
8661 if (processing_template_decl || processing_specialization)
8662 return argtypes == void_list_node;
8663 else
8665 tree argtype;
8666 int arity;
8667 int max_arity = 2;
8669 /* Count the number and type of arguments and check for ellipsis. */
8670 for (argtype = argtypes, arity = 0;
8671 argtype && argtype != void_list_node;
8672 argtype = TREE_CHAIN (argtype))
8674 tree t = TREE_VALUE (argtype);
8675 ++arity;
8677 if (TREE_CODE (t) == POINTER_TYPE)
8679 bool maybe_raw_p = false;
8680 t = TREE_TYPE (t);
8681 if (cp_type_quals (t) != TYPE_QUAL_CONST)
8682 return false;
8683 t = TYPE_MAIN_VARIANT (t);
8684 if ((maybe_raw_p = same_type_p (t, char_type_node))
8685 || same_type_p (t, wchar_type_node)
8686 || same_type_p (t, char16_type_node)
8687 || same_type_p (t, char32_type_node))
8689 argtype = TREE_CHAIN (argtype);
8690 if (!argtype)
8691 return false;
8692 t = TREE_VALUE (argtype);
8693 if (maybe_raw_p && argtype == void_list_node)
8694 return true;
8695 else if (same_type_p (t, size_type_node))
8697 ++arity;
8698 continue;
8700 else
8701 return false;
8704 else if (same_type_p (t, long_long_unsigned_type_node))
8706 max_arity = 1;
8707 *long_long_unsigned_p = true;
8709 else if (same_type_p (t, long_double_type_node))
8711 max_arity = 1;
8712 *long_double_p = true;
8714 else if (same_type_p (t, char_type_node))
8715 max_arity = 1;
8716 else if (same_type_p (t, wchar_type_node))
8717 max_arity = 1;
8718 else if (same_type_p (t, char16_type_node))
8719 max_arity = 1;
8720 else if (same_type_p (t, char32_type_node))
8721 max_arity = 1;
8722 else
8723 return false;
8725 if (!argtype)
8726 return false; /* Found ellipsis. */
8728 if (arity != max_arity)
8729 return false;
8731 return true;