2012-09-14 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / cp / typeck.c
blobad4b0903d6725f3e5480afb94f1ce6081159d6c9
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, complain);
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,
2634 NULL, complain);
2635 if (access_path == error_mark_node)
2636 return error_mark_node;
2637 if (!access_path)
2639 if (complain & tf_error)
2640 error ("%qT is not a base of %qT", scope, object_type);
2641 return error_mark_node;
2644 else
2646 scope = NULL_TREE;
2647 access_path = object_type;
2650 if (TREE_CODE (name) == BIT_NOT_EXPR)
2651 member = lookup_destructor (object, scope, name);
2652 else
2654 /* Look up the member. */
2655 member = lookup_member (access_path, name, /*protect=*/1,
2656 /*want_type=*/false, complain);
2657 if (member == NULL_TREE)
2659 if (complain & tf_error)
2660 error ("%qD has no member named %qE",
2661 TREE_CODE (access_path) == TREE_BINFO
2662 ? TREE_TYPE (access_path) : object_type, name);
2663 return error_mark_node;
2665 if (member == error_mark_node)
2666 return error_mark_node;
2669 if (is_template_id)
2671 tree templ = member;
2673 if (BASELINK_P (templ))
2674 templ = lookup_template_function (templ, template_args);
2675 else
2677 if (complain & tf_error)
2678 error ("%qD is not a member template function", name);
2679 return error_mark_node;
2684 if (TREE_DEPRECATED (member))
2685 warn_deprecated_use (member, NULL_TREE);
2687 if (template_p)
2688 check_template_keyword (member);
2690 expr = build_class_member_access_expr (object, member, access_path,
2691 /*preserve_reference=*/false,
2692 complain);
2693 if (processing_template_decl && expr != error_mark_node)
2695 if (BASELINK_P (member))
2697 if (TREE_CODE (orig_name) == SCOPE_REF)
2698 BASELINK_QUALIFIED_P (member) = 1;
2699 orig_name = member;
2701 return build_min_non_dep (COMPONENT_REF, expr,
2702 orig_object, orig_name,
2703 NULL_TREE);
2706 return expr;
2709 /* Return an expression for the MEMBER_NAME field in the internal
2710 representation of PTRMEM, a pointer-to-member function. (Each
2711 pointer-to-member function type gets its own RECORD_TYPE so it is
2712 more convenient to access the fields by name than by FIELD_DECL.)
2713 This routine converts the NAME to a FIELD_DECL and then creates the
2714 node for the complete expression. */
2716 tree
2717 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2719 tree ptrmem_type;
2720 tree member;
2721 tree member_type;
2723 /* This code is a stripped down version of
2724 build_class_member_access_expr. It does not work to use that
2725 routine directly because it expects the object to be of class
2726 type. */
2727 ptrmem_type = TREE_TYPE (ptrmem);
2728 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2729 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2730 /*want_type=*/false, tf_warning_or_error);
2731 member_type = cp_build_qualified_type (TREE_TYPE (member),
2732 cp_type_quals (ptrmem_type));
2733 return fold_build3_loc (input_location,
2734 COMPONENT_REF, member_type,
2735 ptrmem, member, NULL_TREE);
2738 /* Given an expression PTR for a pointer, return an expression
2739 for the value pointed to.
2740 ERRORSTRING is the name of the operator to appear in error messages.
2742 This function may need to overload OPERATOR_FNNAME.
2743 Must also handle REFERENCE_TYPEs for C++. */
2745 tree
2746 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2747 tsubst_flags_t complain)
2749 tree orig_expr = expr;
2750 tree rval;
2752 if (processing_template_decl)
2754 /* Retain the type if we know the operand is a pointer. */
2755 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2756 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2757 if (type_dependent_expression_p (expr))
2758 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2759 expr = build_non_dependent_expr (expr);
2762 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2763 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2764 if (!rval)
2765 rval = cp_build_indirect_ref (expr, errorstring, complain);
2767 if (processing_template_decl && rval != error_mark_node)
2768 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2769 else
2770 return rval;
2773 /* Helper function called from c-common. */
2774 tree
2775 build_indirect_ref (location_t /*loc*/,
2776 tree ptr, ref_operator errorstring)
2778 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2781 tree
2782 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2783 tsubst_flags_t complain)
2785 tree pointer, type;
2787 if (ptr == current_class_ptr)
2788 return current_class_ref;
2790 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2791 ? ptr : decay_conversion (ptr, complain));
2792 if (pointer == error_mark_node)
2793 return error_mark_node;
2795 type = TREE_TYPE (pointer);
2797 if (POINTER_TYPE_P (type))
2799 /* [expr.unary.op]
2801 If the type of the expression is "pointer to T," the type
2802 of the result is "T." */
2803 tree t = TREE_TYPE (type);
2805 if (CONVERT_EXPR_P (ptr)
2806 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2808 /* If a warning is issued, mark it to avoid duplicates from
2809 the backend. This only needs to be done at
2810 warn_strict_aliasing > 2. */
2811 if (warn_strict_aliasing > 2)
2812 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2813 type, TREE_OPERAND (ptr, 0)))
2814 TREE_NO_WARNING (ptr) = 1;
2817 if (VOID_TYPE_P (t))
2819 /* A pointer to incomplete type (other than cv void) can be
2820 dereferenced [expr.unary.op]/1 */
2821 if (complain & tf_error)
2822 error ("%qT is not a pointer-to-object type", type);
2823 return error_mark_node;
2825 else if (TREE_CODE (pointer) == ADDR_EXPR
2826 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2827 /* The POINTER was something like `&x'. We simplify `*&x' to
2828 `x'. */
2829 return TREE_OPERAND (pointer, 0);
2830 else
2832 tree ref = build1 (INDIRECT_REF, t, pointer);
2834 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2835 so that we get the proper error message if the result is used
2836 to assign to. Also, &* is supposed to be a no-op. */
2837 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2838 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2839 TREE_SIDE_EFFECTS (ref)
2840 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2841 return ref;
2844 else if (!(complain & tf_error))
2845 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2847 /* `pointer' won't be an error_mark_node if we were given a
2848 pointer to member, so it's cool to check for this here. */
2849 else if (TYPE_PTRMEM_P (type))
2850 switch (errorstring)
2852 case RO_ARRAY_INDEXING:
2853 error ("invalid use of array indexing on pointer to member");
2854 break;
2855 case RO_UNARY_STAR:
2856 error ("invalid use of unary %<*%> on pointer to member");
2857 break;
2858 case RO_IMPLICIT_CONVERSION:
2859 error ("invalid use of implicit conversion on pointer to member");
2860 break;
2861 default:
2862 gcc_unreachable ();
2864 else if (pointer != error_mark_node)
2865 invalid_indirection_error (input_location, type, errorstring);
2867 return error_mark_node;
2870 /* This handles expressions of the form "a[i]", which denotes
2871 an array reference.
2873 This is logically equivalent in C to *(a+i), but we may do it differently.
2874 If A is a variable or a member, we generate a primitive ARRAY_REF.
2875 This avoids forcing the array out of registers, and can work on
2876 arrays that are not lvalues (for example, members of structures returned
2877 by functions).
2879 If INDEX is of some user-defined type, it must be converted to
2880 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2881 will inherit the type of the array, which will be some pointer type.
2883 LOC is the location to use in building the array reference. */
2885 tree
2886 cp_build_array_ref (location_t loc, tree array, tree idx,
2887 tsubst_flags_t complain)
2889 tree ret;
2891 if (idx == 0)
2893 if (complain & tf_error)
2894 error_at (loc, "subscript missing in array reference");
2895 return error_mark_node;
2898 if (TREE_TYPE (array) == error_mark_node
2899 || TREE_TYPE (idx) == error_mark_node)
2900 return error_mark_node;
2902 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2903 inside it. */
2904 switch (TREE_CODE (array))
2906 case COMPOUND_EXPR:
2908 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2909 complain);
2910 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2911 TREE_OPERAND (array, 0), value);
2912 SET_EXPR_LOCATION (ret, loc);
2913 return ret;
2916 case COND_EXPR:
2917 ret = build_conditional_expr
2918 (TREE_OPERAND (array, 0),
2919 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2920 complain),
2921 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2922 complain),
2923 complain);
2924 protected_set_expr_location (ret, loc);
2925 return ret;
2927 default:
2928 break;
2931 convert_vector_to_pointer_for_subscript (loc, &array, idx);
2933 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2935 tree rval, type;
2937 warn_array_subscript_with_type_char (idx);
2939 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2941 if (complain & tf_error)
2942 error_at (loc, "array subscript is not an integer");
2943 return error_mark_node;
2946 /* Apply integral promotions *after* noticing character types.
2947 (It is unclear why we do these promotions -- the standard
2948 does not say that we should. In fact, the natural thing would
2949 seem to be to convert IDX to ptrdiff_t; we're performing
2950 pointer arithmetic.) */
2951 idx = cp_perform_integral_promotions (idx, complain);
2953 /* An array that is indexed by a non-constant
2954 cannot be stored in a register; we must be able to do
2955 address arithmetic on its address.
2956 Likewise an array of elements of variable size. */
2957 if (TREE_CODE (idx) != INTEGER_CST
2958 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2959 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2960 != INTEGER_CST)))
2962 if (!cxx_mark_addressable (array))
2963 return error_mark_node;
2966 /* An array that is indexed by a constant value which is not within
2967 the array bounds cannot be stored in a register either; because we
2968 would get a crash in store_bit_field/extract_bit_field when trying
2969 to access a non-existent part of the register. */
2970 if (TREE_CODE (idx) == INTEGER_CST
2971 && TYPE_DOMAIN (TREE_TYPE (array))
2972 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2974 if (!cxx_mark_addressable (array))
2975 return error_mark_node;
2978 if (!lvalue_p (array) && (complain & tf_error))
2979 pedwarn (loc, OPT_Wpedantic,
2980 "ISO C++ forbids subscripting non-lvalue array");
2982 /* Note in C++ it is valid to subscript a `register' array, since
2983 it is valid to take the address of something with that
2984 storage specification. */
2985 if (extra_warnings)
2987 tree foo = array;
2988 while (TREE_CODE (foo) == COMPONENT_REF)
2989 foo = TREE_OPERAND (foo, 0);
2990 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
2991 && (complain & tf_warning))
2992 warning_at (loc, OPT_Wextra,
2993 "subscripting array declared %<register%>");
2996 type = TREE_TYPE (TREE_TYPE (array));
2997 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2998 /* Array ref is const/volatile if the array elements are
2999 or if the array is.. */
3000 TREE_READONLY (rval)
3001 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3002 TREE_SIDE_EFFECTS (rval)
3003 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3004 TREE_THIS_VOLATILE (rval)
3005 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3006 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3007 complain);
3008 protected_set_expr_location (ret, loc);
3009 return ret;
3013 tree ar = cp_default_conversion (array, complain);
3014 tree ind = cp_default_conversion (idx, complain);
3016 /* Put the integer in IND to simplify error checking. */
3017 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3019 tree temp = ar;
3020 ar = ind;
3021 ind = temp;
3024 if (ar == error_mark_node || ind == error_mark_node)
3025 return error_mark_node;
3027 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
3029 if (complain & tf_error)
3030 error_at (loc, "subscripted value is neither array nor pointer");
3031 return error_mark_node;
3033 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3035 if (complain & tf_error)
3036 error_at (loc, "array subscript is not an integer");
3037 return error_mark_node;
3040 warn_array_subscript_with_type_char (idx);
3042 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3043 PLUS_EXPR, ar, ind,
3044 complain),
3045 RO_ARRAY_INDEXING,
3046 complain);
3047 protected_set_expr_location (ret, loc);
3048 return ret;
3052 /* Entry point for Obj-C++. */
3054 tree
3055 build_array_ref (location_t loc, tree array, tree idx)
3057 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3060 /* Resolve a pointer to member function. INSTANCE is the object
3061 instance to use, if the member points to a virtual member.
3063 This used to avoid checking for virtual functions if basetype
3064 has no virtual functions, according to an earlier ANSI draft.
3065 With the final ISO C++ rules, such an optimization is
3066 incorrect: A pointer to a derived member can be static_cast
3067 to pointer-to-base-member, as long as the dynamic object
3068 later has the right member. */
3070 tree
3071 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3072 tsubst_flags_t complain)
3074 if (TREE_CODE (function) == OFFSET_REF)
3075 function = TREE_OPERAND (function, 1);
3077 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3079 tree idx, delta, e1, e2, e3, vtbl, basetype;
3080 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3082 tree instance_ptr = *instance_ptrptr;
3083 tree instance_save_expr = 0;
3084 if (instance_ptr == error_mark_node)
3086 if (TREE_CODE (function) == PTRMEM_CST)
3088 /* Extracting the function address from a pmf is only
3089 allowed with -Wno-pmf-conversions. It only works for
3090 pmf constants. */
3091 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3092 e1 = convert (fntype, e1);
3093 return e1;
3095 else
3097 if (complain & tf_error)
3098 error ("object missing in use of %qE", function);
3099 return error_mark_node;
3103 if (TREE_SIDE_EFFECTS (instance_ptr))
3104 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3106 if (TREE_SIDE_EFFECTS (function))
3107 function = save_expr (function);
3109 /* Start by extracting all the information from the PMF itself. */
3110 e3 = pfn_from_ptrmemfunc (function);
3111 delta = delta_from_ptrmemfunc (function);
3112 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3113 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3115 case ptrmemfunc_vbit_in_pfn:
3116 e1 = cp_build_binary_op (input_location,
3117 BIT_AND_EXPR, idx, integer_one_node,
3118 complain);
3119 idx = cp_build_binary_op (input_location,
3120 MINUS_EXPR, idx, integer_one_node,
3121 complain);
3122 if (idx == error_mark_node)
3123 return error_mark_node;
3124 break;
3126 case ptrmemfunc_vbit_in_delta:
3127 e1 = cp_build_binary_op (input_location,
3128 BIT_AND_EXPR, delta, integer_one_node,
3129 complain);
3130 delta = cp_build_binary_op (input_location,
3131 RSHIFT_EXPR, delta, integer_one_node,
3132 complain);
3133 if (delta == error_mark_node)
3134 return error_mark_node;
3135 break;
3137 default:
3138 gcc_unreachable ();
3141 if (e1 == error_mark_node)
3142 return error_mark_node;
3144 /* Convert down to the right base before using the instance. A
3145 special case is that in a pointer to member of class C, C may
3146 be incomplete. In that case, the function will of course be
3147 a member of C, and no conversion is required. In fact,
3148 lookup_base will fail in that case, because incomplete
3149 classes do not have BINFOs. */
3150 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3151 if (!same_type_ignoring_top_level_qualifiers_p
3152 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3154 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3155 basetype, ba_check, NULL, complain);
3156 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3157 1, complain);
3158 if (instance_ptr == error_mark_node)
3159 return error_mark_node;
3161 /* ...and then the delta in the PMF. */
3162 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3164 /* Hand back the adjusted 'this' argument to our caller. */
3165 *instance_ptrptr = instance_ptr;
3167 /* Next extract the vtable pointer from the object. */
3168 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3169 instance_ptr);
3170 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3171 if (vtbl == error_mark_node)
3172 return error_mark_node;
3174 /* If the object is not dynamic the access invokes undefined
3175 behavior. As it is not executed in this case silence the
3176 spurious warnings it may provoke. */
3177 TREE_NO_WARNING (vtbl) = 1;
3179 /* Finally, extract the function pointer from the vtable. */
3180 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3181 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3182 if (e2 == error_mark_node)
3183 return error_mark_node;
3184 TREE_CONSTANT (e2) = 1;
3186 /* When using function descriptors, the address of the
3187 vtable entry is treated as a function pointer. */
3188 if (TARGET_VTABLE_USES_DESCRIPTORS)
3189 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3190 cp_build_addr_expr (e2, complain));
3192 e2 = fold_convert (TREE_TYPE (e3), e2);
3193 e1 = build_conditional_expr (e1, e2, e3, complain);
3194 if (e1 == error_mark_node)
3195 return error_mark_node;
3197 /* Make sure this doesn't get evaluated first inside one of the
3198 branches of the COND_EXPR. */
3199 if (instance_save_expr)
3200 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3201 instance_save_expr, e1);
3203 function = e1;
3205 return function;
3208 /* Used by the C-common bits. */
3209 tree
3210 build_function_call (location_t /*loc*/,
3211 tree function, tree params)
3213 return cp_build_function_call (function, params, tf_warning_or_error);
3216 /* Used by the C-common bits. */
3217 tree
3218 build_function_call_vec (location_t /*loc*/,
3219 tree function, VEC(tree,gc) *params,
3220 VEC(tree,gc) * /*origtypes*/)
3222 VEC(tree,gc) *orig_params = params;
3223 tree ret = cp_build_function_call_vec (function, &params,
3224 tf_warning_or_error);
3226 /* cp_build_function_call_vec can reallocate PARAMS by adding
3227 default arguments. That should never happen here. Verify
3228 that. */
3229 gcc_assert (params == orig_params);
3231 return ret;
3234 /* Build a function call using a tree list of arguments. */
3236 tree
3237 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3239 VEC(tree,gc) *vec;
3240 tree ret;
3242 vec = make_tree_vector ();
3243 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3244 VEC_safe_push (tree, gc, vec, TREE_VALUE (params));
3245 ret = cp_build_function_call_vec (function, &vec, complain);
3246 release_tree_vector (vec);
3247 return ret;
3250 /* Build a function call using varargs. */
3252 tree
3253 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3255 VEC(tree,gc) *vec;
3256 va_list args;
3257 tree ret, t;
3259 vec = make_tree_vector ();
3260 va_start (args, complain);
3261 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3262 VEC_safe_push (tree, gc, vec, t);
3263 va_end (args);
3264 ret = cp_build_function_call_vec (function, &vec, complain);
3265 release_tree_vector (vec);
3266 return ret;
3269 /* Build a function call using a vector of arguments. PARAMS may be
3270 NULL if there are no parameters. This changes the contents of
3271 PARAMS. */
3273 tree
3274 cp_build_function_call_vec (tree function, VEC(tree,gc) **params,
3275 tsubst_flags_t complain)
3277 tree fntype, fndecl;
3278 int is_method;
3279 tree original = function;
3280 int nargs;
3281 tree *argarray;
3282 tree parm_types;
3283 VEC(tree,gc) *allocated = NULL;
3284 tree ret;
3286 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3287 expressions, like those used for ObjC messenger dispatches. */
3288 if (params != NULL && !VEC_empty (tree, *params))
3289 function = objc_rewrite_function_call (function,
3290 VEC_index (tree, *params, 0));
3292 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3293 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3294 if (TREE_CODE (function) == NOP_EXPR
3295 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3296 function = TREE_OPERAND (function, 0);
3298 if (TREE_CODE (function) == FUNCTION_DECL)
3300 mark_used (function);
3301 fndecl = function;
3303 /* Convert anything with function type to a pointer-to-function. */
3304 if (DECL_MAIN_P (function) && (complain & tf_error))
3305 pedwarn (input_location, OPT_Wpedantic,
3306 "ISO C++ forbids calling %<::main%> from within program");
3308 function = build_addr_func (function, complain);
3310 else
3312 fndecl = NULL_TREE;
3314 function = build_addr_func (function, complain);
3317 if (function == error_mark_node)
3318 return error_mark_node;
3320 fntype = TREE_TYPE (function);
3322 if (TYPE_PTRMEMFUNC_P (fntype))
3324 if (complain & tf_error)
3325 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3326 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3327 original, original);
3328 return error_mark_node;
3331 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3332 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3334 if (!((TREE_CODE (fntype) == POINTER_TYPE
3335 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3336 || is_method
3337 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3339 if (complain & tf_error)
3341 if (!flag_diagnostics_show_caret)
3342 error_at (input_location,
3343 "%qE cannot be used as a function", original);
3344 else if (DECL_P (original))
3345 error_at (input_location,
3346 "%qD cannot be used as a function", original);
3347 else
3348 error_at (input_location,
3349 "expression cannot be used as a function");
3352 return error_mark_node;
3355 /* fntype now gets the type of function pointed to. */
3356 fntype = TREE_TYPE (fntype);
3357 parm_types = TYPE_ARG_TYPES (fntype);
3359 if (params == NULL)
3361 allocated = make_tree_vector ();
3362 params = &allocated;
3365 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3366 complain);
3367 if (nargs < 0)
3368 return error_mark_node;
3370 argarray = VEC_address (tree, *params);
3372 /* Check for errors in format strings and inappropriately
3373 null parameters. */
3374 check_function_arguments (fntype, nargs, argarray);
3376 ret = build_cxx_call (function, nargs, argarray, complain);
3378 if (allocated != NULL)
3379 release_tree_vector (allocated);
3381 return ret;
3384 /* Subroutine of convert_arguments.
3385 Warn about wrong number of args are genereted. */
3387 static void
3388 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3390 if (fndecl)
3392 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3394 if (DECL_NAME (fndecl) == NULL_TREE
3395 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3396 error_at (loc,
3397 too_many_p
3398 ? G_("too many arguments to constructor %q#D")
3399 : G_("too few arguments to constructor %q#D"),
3400 fndecl);
3401 else
3402 error_at (loc,
3403 too_many_p
3404 ? G_("too many arguments to member function %q#D")
3405 : G_("too few arguments to member function %q#D"),
3406 fndecl);
3408 else
3409 error_at (loc,
3410 too_many_p
3411 ? G_("too many arguments to function %q#D")
3412 : G_("too few arguments to function %q#D"),
3413 fndecl);
3414 inform (DECL_SOURCE_LOCATION (fndecl),
3415 "declared here");
3417 else
3419 if (c_dialect_objc () && objc_message_selector ())
3420 error_at (loc,
3421 too_many_p
3422 ? G_("too many arguments to method %q#D")
3423 : G_("too few arguments to method %q#D"),
3424 objc_message_selector ());
3425 else
3426 error_at (loc, too_many_p ? G_("too many arguments to function")
3427 : G_("too few arguments to function"));
3431 /* Convert the actual parameter expressions in the list VALUES to the
3432 types in the list TYPELIST. The converted expressions are stored
3433 back in the VALUES vector.
3434 If parmdecls is exhausted, or when an element has NULL as its type,
3435 perform the default conversions.
3437 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3439 This is also where warnings about wrong number of args are generated.
3441 Returns the actual number of arguments processed (which might be less
3442 than the length of the vector), or -1 on error.
3444 In C++, unspecified trailing parameters can be filled in with their
3445 default arguments, if such were specified. Do so here. */
3447 static int
3448 convert_arguments (tree typelist, VEC(tree,gc) **values, tree fndecl,
3449 int flags, tsubst_flags_t complain)
3451 tree typetail;
3452 unsigned int i;
3454 /* Argument passing is always copy-initialization. */
3455 flags |= LOOKUP_ONLYCONVERTING;
3457 for (i = 0, typetail = typelist;
3458 i < VEC_length (tree, *values);
3459 i++)
3461 tree type = typetail ? TREE_VALUE (typetail) : 0;
3462 tree val = VEC_index (tree, *values, i);
3464 if (val == error_mark_node || type == error_mark_node)
3465 return -1;
3467 if (type == void_type_node)
3469 if (complain & tf_error)
3471 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3472 return i;
3474 else
3475 return -1;
3478 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3479 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3480 if (TREE_CODE (val) == NOP_EXPR
3481 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3482 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3483 val = TREE_OPERAND (val, 0);
3485 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3487 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3488 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3489 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3490 val = decay_conversion (val, complain);
3493 if (val == error_mark_node)
3494 return -1;
3496 if (type != 0)
3498 /* Formal parm type is specified by a function prototype. */
3499 tree parmval;
3501 if (!COMPLETE_TYPE_P (complete_type (type)))
3503 if (complain & tf_error)
3505 if (fndecl)
3506 error ("parameter %P of %qD has incomplete type %qT",
3507 i, fndecl, type);
3508 else
3509 error ("parameter %P has incomplete type %qT", i, type);
3511 parmval = error_mark_node;
3513 else
3515 parmval = convert_for_initialization
3516 (NULL_TREE, type, val, flags,
3517 ICR_ARGPASS, fndecl, i, complain);
3518 parmval = convert_for_arg_passing (type, parmval, complain);
3521 if (parmval == error_mark_node)
3522 return -1;
3524 VEC_replace (tree, *values, i, parmval);
3526 else
3528 if (fndecl && DECL_BUILT_IN (fndecl)
3529 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3530 /* Don't do ellipsis conversion for __built_in_constant_p
3531 as this will result in spurious errors for non-trivial
3532 types. */
3533 val = require_complete_type_sfinae (val, complain);
3534 else
3535 val = convert_arg_to_ellipsis (val, complain);
3537 VEC_replace (tree, *values, i, val);
3540 if (typetail)
3541 typetail = TREE_CHAIN (typetail);
3544 if (typetail != 0 && typetail != void_list_node)
3546 /* See if there are default arguments that can be used. Because
3547 we hold default arguments in the FUNCTION_TYPE (which is so
3548 wrong), we can see default parameters here from deduced
3549 contexts (and via typeof) for indirect function calls.
3550 Fortunately we know whether we have a function decl to
3551 provide default arguments in a language conformant
3552 manner. */
3553 if (fndecl && TREE_PURPOSE (typetail)
3554 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3556 for (; typetail != void_list_node; ++i)
3558 tree parmval
3559 = convert_default_arg (TREE_VALUE (typetail),
3560 TREE_PURPOSE (typetail),
3561 fndecl, i, complain);
3563 if (parmval == error_mark_node)
3564 return -1;
3566 VEC_safe_push (tree, gc, *values, parmval);
3567 typetail = TREE_CHAIN (typetail);
3568 /* ends with `...'. */
3569 if (typetail == NULL_TREE)
3570 break;
3573 else
3575 if (complain & tf_error)
3576 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3577 return -1;
3581 return (int) i;
3584 /* Build a binary-operation expression, after performing default
3585 conversions on the operands. CODE is the kind of expression to
3586 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3587 are the tree codes which correspond to ARG1 and ARG2 when issuing
3588 warnings about possibly misplaced parentheses. They may differ
3589 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3590 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3591 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3592 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3593 ARG2_CODE as ERROR_MARK. */
3595 tree
3596 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3597 enum tree_code arg1_code, tree arg2,
3598 enum tree_code arg2_code, tree *overload,
3599 tsubst_flags_t complain)
3601 tree orig_arg1;
3602 tree orig_arg2;
3603 tree expr;
3605 orig_arg1 = arg1;
3606 orig_arg2 = arg2;
3608 if (processing_template_decl)
3610 if (type_dependent_expression_p (arg1)
3611 || type_dependent_expression_p (arg2))
3612 return build_min_nt_loc (loc, code, arg1, arg2);
3613 arg1 = build_non_dependent_expr (arg1);
3614 arg2 = build_non_dependent_expr (arg2);
3617 if (code == DOTSTAR_EXPR)
3618 expr = build_m_component_ref (arg1, arg2, complain);
3619 else
3620 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3621 overload, complain);
3623 /* Check for cases such as x+y<<z which users are likely to
3624 misinterpret. But don't warn about obj << x + y, since that is a
3625 common idiom for I/O. */
3626 if (warn_parentheses
3627 && (complain & tf_warning)
3628 && !processing_template_decl
3629 && !error_operand_p (arg1)
3630 && !error_operand_p (arg2)
3631 && (code != LSHIFT_EXPR
3632 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3633 warn_about_parentheses (code, arg1_code, orig_arg1, arg2_code, orig_arg2);
3635 if (processing_template_decl && expr != error_mark_node)
3636 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3638 return expr;
3641 /* Build and return an ARRAY_REF expression. */
3643 tree
3644 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3645 tsubst_flags_t complain)
3647 tree orig_arg1 = arg1;
3648 tree orig_arg2 = arg2;
3649 tree expr;
3651 if (processing_template_decl)
3653 if (type_dependent_expression_p (arg1)
3654 || type_dependent_expression_p (arg2))
3655 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3656 NULL_TREE, NULL_TREE);
3657 arg1 = build_non_dependent_expr (arg1);
3658 arg2 = build_non_dependent_expr (arg2);
3661 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3662 NULL_TREE, /*overload=*/NULL, complain);
3664 if (processing_template_decl && expr != error_mark_node)
3665 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3666 NULL_TREE, NULL_TREE);
3667 return expr;
3670 /* Return whether OP is an expression of enum type cast to integer
3671 type. In C++ even unsigned enum types are cast to signed integer
3672 types. We do not want to issue warnings about comparisons between
3673 signed and unsigned types when one of the types is an enum type.
3674 Those warnings are always false positives in practice. */
3676 static bool
3677 enum_cast_to_int (tree op)
3679 if (TREE_CODE (op) == NOP_EXPR
3680 && TREE_TYPE (op) == integer_type_node
3681 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3682 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3683 return true;
3685 /* The cast may have been pushed into a COND_EXPR. */
3686 if (TREE_CODE (op) == COND_EXPR)
3687 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3688 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3690 return false;
3693 /* For the c-common bits. */
3694 tree
3695 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3696 int /*convert_p*/)
3698 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3702 /* Build a binary-operation expression without default conversions.
3703 CODE is the kind of expression to build.
3704 LOCATION is the location_t of the operator in the source code.
3705 This function differs from `build' in several ways:
3706 the data type of the result is computed and recorded in it,
3707 warnings are generated if arg data types are invalid,
3708 special handling for addition and subtraction of pointers is known,
3709 and some optimization is done (operations on narrow ints
3710 are done in the narrower type when that gives the same result).
3711 Constant folding is also done before the result is returned.
3713 Note that the operands will never have enumeral types
3714 because either they have just had the default conversions performed
3715 or they have both just been converted to some other type in which
3716 the arithmetic is to be done.
3718 C++: must do special pointer arithmetic when implementing
3719 multiple inheritance, and deal with pointer to member functions. */
3721 tree
3722 cp_build_binary_op (location_t location,
3723 enum tree_code code, tree orig_op0, tree orig_op1,
3724 tsubst_flags_t complain)
3726 tree op0, op1;
3727 enum tree_code code0, code1;
3728 tree type0, type1;
3729 const char *invalid_op_diag;
3731 /* Expression code to give to the expression when it is built.
3732 Normally this is CODE, which is what the caller asked for,
3733 but in some special cases we change it. */
3734 enum tree_code resultcode = code;
3736 /* Data type in which the computation is to be performed.
3737 In the simplest cases this is the common type of the arguments. */
3738 tree result_type = NULL;
3740 /* Nonzero means operands have already been type-converted
3741 in whatever way is necessary.
3742 Zero means they need to be converted to RESULT_TYPE. */
3743 int converted = 0;
3745 /* Nonzero means create the expression with this type, rather than
3746 RESULT_TYPE. */
3747 tree build_type = 0;
3749 /* Nonzero means after finally constructing the expression
3750 convert it to this type. */
3751 tree final_type = 0;
3753 tree result;
3755 /* Nonzero if this is an operation like MIN or MAX which can
3756 safely be computed in short if both args are promoted shorts.
3757 Also implies COMMON.
3758 -1 indicates a bitwise operation; this makes a difference
3759 in the exact conditions for when it is safe to do the operation
3760 in a narrower mode. */
3761 int shorten = 0;
3763 /* Nonzero if this is a comparison operation;
3764 if both args are promoted shorts, compare the original shorts.
3765 Also implies COMMON. */
3766 int short_compare = 0;
3768 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3769 int common = 0;
3771 /* True if both operands have arithmetic type. */
3772 bool arithmetic_types_p;
3774 /* Apply default conversions. */
3775 op0 = orig_op0;
3776 op1 = orig_op1;
3778 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3779 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3780 || code == TRUTH_XOR_EXPR)
3782 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3783 op0 = decay_conversion (op0, complain);
3784 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3785 op1 = decay_conversion (op1, complain);
3787 else
3789 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3790 op0 = cp_default_conversion (op0, complain);
3791 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3792 op1 = cp_default_conversion (op1, complain);
3795 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3796 STRIP_TYPE_NOPS (op0);
3797 STRIP_TYPE_NOPS (op1);
3799 /* DTRT if one side is an overloaded function, but complain about it. */
3800 if (type_unknown_p (op0))
3802 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3803 if (t != error_mark_node)
3805 if (complain & tf_error)
3806 permerror (input_location, "assuming cast to type %qT from overloaded function",
3807 TREE_TYPE (t));
3808 op0 = t;
3811 if (type_unknown_p (op1))
3813 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3814 if (t != error_mark_node)
3816 if (complain & tf_error)
3817 permerror (input_location, "assuming cast to type %qT from overloaded function",
3818 TREE_TYPE (t));
3819 op1 = t;
3823 type0 = TREE_TYPE (op0);
3824 type1 = TREE_TYPE (op1);
3826 /* The expression codes of the data types of the arguments tell us
3827 whether the arguments are integers, floating, pointers, etc. */
3828 code0 = TREE_CODE (type0);
3829 code1 = TREE_CODE (type1);
3831 /* If an error was already reported for one of the arguments,
3832 avoid reporting another error. */
3833 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3834 return error_mark_node;
3836 if ((invalid_op_diag
3837 = targetm.invalid_binary_op (code, type0, type1)))
3839 error (invalid_op_diag);
3840 return error_mark_node;
3843 /* Issue warnings about peculiar, but valid, uses of NULL. */
3844 if ((orig_op0 == null_node || orig_op1 == null_node)
3845 /* It's reasonable to use pointer values as operands of &&
3846 and ||, so NULL is no exception. */
3847 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3848 && ( /* Both are NULL (or 0) and the operation was not a
3849 comparison or a pointer subtraction. */
3850 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3851 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3852 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3853 || (!null_ptr_cst_p (orig_op0)
3854 && !TYPE_PTR_OR_PTRMEM_P (type0))
3855 || (!null_ptr_cst_p (orig_op1)
3856 && !TYPE_PTR_OR_PTRMEM_P (type1)))
3857 && (complain & tf_warning))
3859 source_location loc =
3860 expansion_point_location_if_in_system_header (input_location);
3862 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
3865 switch (code)
3867 case MINUS_EXPR:
3868 /* Subtraction of two similar pointers.
3869 We must subtract them as integers, then divide by object size. */
3870 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3871 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3872 TREE_TYPE (type1)))
3873 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
3874 complain);
3875 /* In all other cases except pointer - int, the usual arithmetic
3876 rules apply. */
3877 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3879 common = 1;
3880 break;
3882 /* The pointer - int case is just like pointer + int; fall
3883 through. */
3884 case PLUS_EXPR:
3885 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3886 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3888 tree ptr_operand;
3889 tree int_operand;
3890 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3891 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3892 if (processing_template_decl)
3894 result_type = TREE_TYPE (ptr_operand);
3895 break;
3897 return cp_pointer_int_sum (code,
3898 ptr_operand,
3899 int_operand);
3901 common = 1;
3902 break;
3904 case MULT_EXPR:
3905 common = 1;
3906 break;
3908 case TRUNC_DIV_EXPR:
3909 case CEIL_DIV_EXPR:
3910 case FLOOR_DIV_EXPR:
3911 case ROUND_DIV_EXPR:
3912 case EXACT_DIV_EXPR:
3913 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3914 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3915 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3916 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
3918 enum tree_code tcode0 = code0, tcode1 = code1;
3920 warn_for_div_by_zero (location, op1);
3922 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
3923 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3924 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
3925 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3927 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
3928 resultcode = RDIV_EXPR;
3929 else
3930 /* When dividing two signed integers, we have to promote to int.
3931 unless we divide by a constant != -1. Note that default
3932 conversion will have been performed on the operands at this
3933 point, so we have to dig out the original type to find out if
3934 it was unsigned. */
3935 shorten = ((TREE_CODE (op0) == NOP_EXPR
3936 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3937 || (TREE_CODE (op1) == INTEGER_CST
3938 && ! integer_all_onesp (op1)));
3940 common = 1;
3942 break;
3944 case BIT_AND_EXPR:
3945 case BIT_IOR_EXPR:
3946 case BIT_XOR_EXPR:
3947 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3948 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3949 && !VECTOR_FLOAT_TYPE_P (type0)
3950 && !VECTOR_FLOAT_TYPE_P (type1)))
3951 shorten = -1;
3952 break;
3954 case TRUNC_MOD_EXPR:
3955 case FLOOR_MOD_EXPR:
3956 warn_for_div_by_zero (location, op1);
3958 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3959 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
3960 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
3961 common = 1;
3962 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3964 /* Although it would be tempting to shorten always here, that loses
3965 on some targets, since the modulo instruction is undefined if the
3966 quotient can't be represented in the computation mode. We shorten
3967 only if unsigned or if dividing by something we know != -1. */
3968 shorten = ((TREE_CODE (op0) == NOP_EXPR
3969 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3970 || (TREE_CODE (op1) == INTEGER_CST
3971 && ! integer_all_onesp (op1)));
3972 common = 1;
3974 break;
3976 case TRUTH_ANDIF_EXPR:
3977 case TRUTH_ORIF_EXPR:
3978 case TRUTH_AND_EXPR:
3979 case TRUTH_OR_EXPR:
3980 result_type = boolean_type_node;
3981 break;
3983 /* Shift operations: result has same type as first operand;
3984 always convert second operand to int.
3985 Also set SHORT_SHIFT if shifting rightward. */
3987 case RSHIFT_EXPR:
3988 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3989 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
3990 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
3991 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
3993 result_type = type0;
3994 converted = 1;
3996 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3998 result_type = type0;
3999 if (TREE_CODE (op1) == INTEGER_CST)
4001 if (tree_int_cst_lt (op1, integer_zero_node))
4003 if ((complain & tf_warning)
4004 && c_inhibit_evaluation_warnings == 0)
4005 warning (0, "right shift count is negative");
4007 else
4009 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0
4010 && (complain & tf_warning)
4011 && c_inhibit_evaluation_warnings == 0)
4012 warning (0, "right shift count >= width of type");
4015 /* Convert the shift-count to an integer, regardless of
4016 size of value being shifted. */
4017 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4018 op1 = cp_convert (integer_type_node, op1, complain);
4019 /* Avoid converting op1 to result_type later. */
4020 converted = 1;
4022 break;
4024 case LSHIFT_EXPR:
4025 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4026 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4027 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4028 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4030 result_type = type0;
4031 converted = 1;
4033 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4035 result_type = type0;
4036 if (TREE_CODE (op1) == INTEGER_CST)
4038 if (tree_int_cst_lt (op1, integer_zero_node))
4040 if ((complain & tf_warning)
4041 && c_inhibit_evaluation_warnings == 0)
4042 warning (0, "left shift count is negative");
4044 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4046 if ((complain & tf_warning)
4047 && c_inhibit_evaluation_warnings == 0)
4048 warning (0, "left shift count >= width of type");
4051 /* Convert the shift-count to an integer, regardless of
4052 size of value being shifted. */
4053 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4054 op1 = cp_convert (integer_type_node, op1, complain);
4055 /* Avoid converting op1 to result_type later. */
4056 converted = 1;
4058 break;
4060 case RROTATE_EXPR:
4061 case LROTATE_EXPR:
4062 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4064 result_type = type0;
4065 if (TREE_CODE (op1) == INTEGER_CST)
4067 if (tree_int_cst_lt (op1, integer_zero_node))
4069 if (complain & tf_warning)
4070 warning (0, (code == LROTATE_EXPR)
4071 ? G_("left rotate count is negative")
4072 : G_("right rotate count is negative"));
4074 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4076 if (complain & tf_warning)
4077 warning (0, (code == LROTATE_EXPR)
4078 ? G_("left rotate count >= width of type")
4079 : G_("right rotate count >= width of type"));
4082 /* Convert the shift-count to an integer, regardless of
4083 size of value being shifted. */
4084 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4085 op1 = cp_convert (integer_type_node, op1, complain);
4087 break;
4089 case EQ_EXPR:
4090 case NE_EXPR:
4091 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4092 goto vector_compare;
4093 if ((complain & tf_warning)
4094 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4095 warning (OPT_Wfloat_equal,
4096 "comparing floating point with == or != is unsafe");
4097 if ((complain & tf_warning)
4098 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4099 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4100 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4102 build_type = boolean_type_node;
4103 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4104 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4105 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4106 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4107 short_compare = 1;
4108 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4109 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4110 result_type = composite_pointer_type (type0, type1, op0, op1,
4111 CPO_COMPARISON, complain);
4112 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4113 && null_ptr_cst_p (op1))
4115 if (TREE_CODE (op0) == ADDR_EXPR
4116 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4118 if ((complain & tf_warning)
4119 && c_inhibit_evaluation_warnings == 0)
4120 warning (OPT_Waddress, "the address of %qD will never be NULL",
4121 TREE_OPERAND (op0, 0));
4123 result_type = type0;
4125 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4126 && null_ptr_cst_p (op0))
4128 if (TREE_CODE (op1) == ADDR_EXPR
4129 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4131 if ((complain & tf_warning)
4132 && c_inhibit_evaluation_warnings == 0)
4133 warning (OPT_Waddress, "the address of %qD will never be NULL",
4134 TREE_OPERAND (op1, 0));
4136 result_type = type1;
4138 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4139 /* One of the operands must be of nullptr_t type. */
4140 result_type = TREE_TYPE (nullptr_node);
4141 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4143 result_type = type0;
4144 if (complain & tf_error)
4145 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4146 else
4147 return error_mark_node;
4149 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4151 result_type = type1;
4152 if (complain & tf_error)
4153 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4154 else
4155 return error_mark_node;
4157 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4159 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4160 == ptrmemfunc_vbit_in_delta)
4162 tree pfn0 = pfn_from_ptrmemfunc (op0);
4163 tree delta0 = delta_from_ptrmemfunc (op0);
4164 tree e1 = cp_build_binary_op (location,
4165 EQ_EXPR,
4166 pfn0,
4167 build_zero_cst (TREE_TYPE (pfn0)),
4168 complain);
4169 tree e2 = cp_build_binary_op (location,
4170 BIT_AND_EXPR,
4171 delta0,
4172 integer_one_node,
4173 complain);
4175 if ((complain & tf_warning)
4176 && c_inhibit_evaluation_warnings == 0
4177 && !NULLPTR_TYPE_P (TREE_TYPE (op1)))
4178 warning (OPT_Wzero_as_null_pointer_constant,
4179 "zero as null pointer constant");
4181 e2 = cp_build_binary_op (location,
4182 EQ_EXPR, e2, integer_zero_node,
4183 complain);
4184 op0 = cp_build_binary_op (location,
4185 TRUTH_ANDIF_EXPR, e1, e2,
4186 complain);
4187 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4189 else
4191 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4192 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4194 result_type = TREE_TYPE (op0);
4196 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4197 return cp_build_binary_op (location, code, op1, op0, complain);
4198 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4200 tree type;
4201 /* E will be the final comparison. */
4202 tree e;
4203 /* E1 and E2 are for scratch. */
4204 tree e1;
4205 tree e2;
4206 tree pfn0;
4207 tree pfn1;
4208 tree delta0;
4209 tree delta1;
4211 type = composite_pointer_type (type0, type1, op0, op1,
4212 CPO_COMPARISON, complain);
4214 if (!same_type_p (TREE_TYPE (op0), type))
4215 op0 = cp_convert_and_check (type, op0, complain);
4216 if (!same_type_p (TREE_TYPE (op1), type))
4217 op1 = cp_convert_and_check (type, op1, complain);
4219 if (op0 == error_mark_node || op1 == error_mark_node)
4220 return error_mark_node;
4222 if (TREE_SIDE_EFFECTS (op0))
4223 op0 = save_expr (op0);
4224 if (TREE_SIDE_EFFECTS (op1))
4225 op1 = save_expr (op1);
4227 pfn0 = pfn_from_ptrmemfunc (op0);
4228 pfn1 = pfn_from_ptrmemfunc (op1);
4229 delta0 = delta_from_ptrmemfunc (op0);
4230 delta1 = delta_from_ptrmemfunc (op1);
4231 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4232 == ptrmemfunc_vbit_in_delta)
4234 /* We generate:
4236 (op0.pfn == op1.pfn
4237 && ((op0.delta == op1.delta)
4238 || (!op0.pfn && op0.delta & 1 == 0
4239 && op1.delta & 1 == 0))
4241 The reason for the `!op0.pfn' bit is that a NULL
4242 pointer-to-member is any member with a zero PFN and
4243 LSB of the DELTA field is 0. */
4245 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4246 delta0,
4247 integer_one_node,
4248 complain);
4249 e1 = cp_build_binary_op (location,
4250 EQ_EXPR, e1, integer_zero_node,
4251 complain);
4252 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4253 delta1,
4254 integer_one_node,
4255 complain);
4256 e2 = cp_build_binary_op (location,
4257 EQ_EXPR, e2, integer_zero_node,
4258 complain);
4259 e1 = cp_build_binary_op (location,
4260 TRUTH_ANDIF_EXPR, e2, e1,
4261 complain);
4262 e2 = cp_build_binary_op (location, EQ_EXPR,
4263 pfn0,
4264 build_zero_cst (TREE_TYPE (pfn0)),
4265 complain);
4266 e2 = cp_build_binary_op (location,
4267 TRUTH_ANDIF_EXPR, e2, e1, complain);
4268 e1 = cp_build_binary_op (location,
4269 EQ_EXPR, delta0, delta1, complain);
4270 e1 = cp_build_binary_op (location,
4271 TRUTH_ORIF_EXPR, e1, e2, complain);
4273 else
4275 /* We generate:
4277 (op0.pfn == op1.pfn
4278 && (!op0.pfn || op0.delta == op1.delta))
4280 The reason for the `!op0.pfn' bit is that a NULL
4281 pointer-to-member is any member with a zero PFN; the
4282 DELTA field is unspecified. */
4284 e1 = cp_build_binary_op (location,
4285 EQ_EXPR, delta0, delta1, complain);
4286 e2 = cp_build_binary_op (location,
4287 EQ_EXPR,
4288 pfn0,
4289 build_zero_cst (TREE_TYPE (pfn0)),
4290 complain);
4291 e1 = cp_build_binary_op (location,
4292 TRUTH_ORIF_EXPR, e1, e2, complain);
4294 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4295 e = cp_build_binary_op (location,
4296 TRUTH_ANDIF_EXPR, e2, e1, complain);
4297 if (code == EQ_EXPR)
4298 return e;
4299 return cp_build_binary_op (location,
4300 EQ_EXPR, e, integer_zero_node, complain);
4302 else
4304 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4305 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4306 type1));
4307 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4308 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4309 type0));
4312 break;
4314 case MAX_EXPR:
4315 case MIN_EXPR:
4316 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4317 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4318 shorten = 1;
4319 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4320 result_type = composite_pointer_type (type0, type1, op0, op1,
4321 CPO_COMPARISON, complain);
4322 break;
4324 case LE_EXPR:
4325 case GE_EXPR:
4326 case LT_EXPR:
4327 case GT_EXPR:
4328 if (TREE_CODE (orig_op0) == STRING_CST
4329 || TREE_CODE (orig_op1) == STRING_CST)
4331 if (complain & tf_warning)
4332 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4335 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4337 vector_compare:
4338 tree intt;
4339 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4340 TREE_TYPE (type1)))
4342 error_at (location, "comparing vectors with different "
4343 "element types");
4344 inform (location, "operand types are %qT and %qT", type0, type1);
4345 return error_mark_node;
4348 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4350 error_at (location, "comparing vectors with different "
4351 "number of elements");
4352 inform (location, "operand types are %qT and %qT", type0, type1);
4353 return error_mark_node;
4356 /* Always construct signed integer vector type. */
4357 intt = c_common_type_for_size (GET_MODE_BITSIZE
4358 (TYPE_MODE (TREE_TYPE (type0))), 0);
4359 result_type = build_opaque_vector_type (intt,
4360 TYPE_VECTOR_SUBPARTS (type0));
4361 converted = 1;
4362 break;
4364 build_type = boolean_type_node;
4365 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4366 || code0 == ENUMERAL_TYPE)
4367 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4368 || code1 == ENUMERAL_TYPE))
4369 short_compare = 1;
4370 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4371 result_type = composite_pointer_type (type0, type1, op0, op1,
4372 CPO_COMPARISON, complain);
4373 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4375 result_type = type0;
4376 if (extra_warnings && (complain & tf_warning))
4377 warning (OPT_Wextra,
4378 "ordered comparison of pointer with integer zero");
4380 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4382 result_type = type1;
4383 if (extra_warnings && (complain & tf_warning))
4384 warning (OPT_Wextra,
4385 "ordered comparison of pointer with integer zero");
4387 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4388 /* One of the operands must be of nullptr_t type. */
4389 result_type = TREE_TYPE (nullptr_node);
4390 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4392 result_type = type0;
4393 if (complain & tf_error)
4394 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4395 else
4396 return error_mark_node;
4398 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4400 result_type = type1;
4401 if (complain & tf_error)
4402 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4403 else
4404 return error_mark_node;
4406 break;
4408 case UNORDERED_EXPR:
4409 case ORDERED_EXPR:
4410 case UNLT_EXPR:
4411 case UNLE_EXPR:
4412 case UNGT_EXPR:
4413 case UNGE_EXPR:
4414 case UNEQ_EXPR:
4415 build_type = integer_type_node;
4416 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4418 if (complain & tf_error)
4419 error ("unordered comparison on non-floating point argument");
4420 return error_mark_node;
4422 common = 1;
4423 break;
4425 default:
4426 break;
4429 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4430 || code0 == ENUMERAL_TYPE)
4431 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4432 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4433 arithmetic_types_p = 1;
4434 else
4436 arithmetic_types_p = 0;
4437 /* Vector arithmetic is only allowed when both sides are vectors. */
4438 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4440 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4441 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4442 TREE_TYPE (type1)))
4444 binary_op_error (location, code, type0, type1);
4445 return error_mark_node;
4447 arithmetic_types_p = 1;
4450 /* Determine the RESULT_TYPE, if it is not already known. */
4451 if (!result_type
4452 && arithmetic_types_p
4453 && (shorten || common || short_compare))
4455 result_type = cp_common_type (type0, type1);
4456 do_warn_double_promotion (result_type, type0, type1,
4457 "implicit conversion from %qT to %qT "
4458 "to match other operand of binary "
4459 "expression",
4460 location);
4463 if (!result_type)
4465 if (complain & tf_error)
4466 error ("invalid operands of types %qT and %qT to binary %qO",
4467 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4468 return error_mark_node;
4471 /* If we're in a template, the only thing we need to know is the
4472 RESULT_TYPE. */
4473 if (processing_template_decl)
4475 /* Since the middle-end checks the type when doing a build2, we
4476 need to build the tree in pieces. This built tree will never
4477 get out of the front-end as we replace it when instantiating
4478 the template. */
4479 tree tmp = build2 (resultcode,
4480 build_type ? build_type : result_type,
4481 NULL_TREE, op1);
4482 TREE_OPERAND (tmp, 0) = op0;
4483 return tmp;
4486 if (arithmetic_types_p)
4488 bool first_complex = (code0 == COMPLEX_TYPE);
4489 bool second_complex = (code1 == COMPLEX_TYPE);
4490 int none_complex = (!first_complex && !second_complex);
4492 /* Adapted from patch for c/24581. */
4493 if (first_complex != second_complex
4494 && (code == PLUS_EXPR
4495 || code == MINUS_EXPR
4496 || code == MULT_EXPR
4497 || (code == TRUNC_DIV_EXPR && first_complex))
4498 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4499 && flag_signed_zeros)
4501 /* An operation on mixed real/complex operands must be
4502 handled specially, but the language-independent code can
4503 more easily optimize the plain complex arithmetic if
4504 -fno-signed-zeros. */
4505 tree real_type = TREE_TYPE (result_type);
4506 tree real, imag;
4507 if (first_complex)
4509 if (TREE_TYPE (op0) != result_type)
4510 op0 = cp_convert_and_check (result_type, op0, complain);
4511 if (TREE_TYPE (op1) != real_type)
4512 op1 = cp_convert_and_check (real_type, op1, complain);
4514 else
4516 if (TREE_TYPE (op0) != real_type)
4517 op0 = cp_convert_and_check (real_type, op0, complain);
4518 if (TREE_TYPE (op1) != result_type)
4519 op1 = cp_convert_and_check (result_type, op1, complain);
4521 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4522 return error_mark_node;
4523 if (first_complex)
4525 op0 = save_expr (op0);
4526 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4527 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4528 switch (code)
4530 case MULT_EXPR:
4531 case TRUNC_DIV_EXPR:
4532 op1 = save_expr (op1);
4533 imag = build2 (resultcode, real_type, imag, op1);
4534 /* Fall through. */
4535 case PLUS_EXPR:
4536 case MINUS_EXPR:
4537 real = build2 (resultcode, real_type, real, op1);
4538 break;
4539 default:
4540 gcc_unreachable();
4543 else
4545 op1 = save_expr (op1);
4546 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4547 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4548 switch (code)
4550 case MULT_EXPR:
4551 op0 = save_expr (op0);
4552 imag = build2 (resultcode, real_type, op0, imag);
4553 /* Fall through. */
4554 case PLUS_EXPR:
4555 real = build2 (resultcode, real_type, op0, real);
4556 break;
4557 case MINUS_EXPR:
4558 real = build2 (resultcode, real_type, op0, real);
4559 imag = build1 (NEGATE_EXPR, real_type, imag);
4560 break;
4561 default:
4562 gcc_unreachable();
4565 real = fold_if_not_in_template (real);
4566 imag = fold_if_not_in_template (imag);
4567 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4568 result = fold_if_not_in_template (result);
4569 return result;
4572 /* For certain operations (which identify themselves by shorten != 0)
4573 if both args were extended from the same smaller type,
4574 do the arithmetic in that type and then extend.
4576 shorten !=0 and !=1 indicates a bitwise operation.
4577 For them, this optimization is safe only if
4578 both args are zero-extended or both are sign-extended.
4579 Otherwise, we might change the result.
4580 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4581 but calculated in (unsigned short) it would be (unsigned short)-1. */
4583 if (shorten && none_complex)
4585 final_type = result_type;
4586 result_type = shorten_binary_op (result_type, op0, op1,
4587 shorten == -1);
4590 /* Comparison operations are shortened too but differently.
4591 They identify themselves by setting short_compare = 1. */
4593 if (short_compare)
4595 /* Don't write &op0, etc., because that would prevent op0
4596 from being kept in a register.
4597 Instead, make copies of the our local variables and
4598 pass the copies by reference, then copy them back afterward. */
4599 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4600 enum tree_code xresultcode = resultcode;
4601 tree val
4602 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4603 if (val != 0)
4604 return cp_convert (boolean_type_node, val, complain);
4605 op0 = xop0, op1 = xop1;
4606 converted = 1;
4607 resultcode = xresultcode;
4610 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4611 && warn_sign_compare
4612 /* Do not warn until the template is instantiated; we cannot
4613 bound the ranges of the arguments until that point. */
4614 && !processing_template_decl
4615 && (complain & tf_warning)
4616 && c_inhibit_evaluation_warnings == 0
4617 /* Even unsigned enum types promote to signed int. We don't
4618 want to issue -Wsign-compare warnings for this case. */
4619 && !enum_cast_to_int (orig_op0)
4620 && !enum_cast_to_int (orig_op1))
4622 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
4623 result_type, resultcode);
4627 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4628 Then the expression will be built.
4629 It will be given type FINAL_TYPE if that is nonzero;
4630 otherwise, it will be given type RESULT_TYPE. */
4631 if (! converted)
4633 if (TREE_TYPE (op0) != result_type)
4634 op0 = cp_convert_and_check (result_type, op0, complain);
4635 if (TREE_TYPE (op1) != result_type)
4636 op1 = cp_convert_and_check (result_type, op1, complain);
4638 if (op0 == error_mark_node || op1 == error_mark_node)
4639 return error_mark_node;
4642 if (build_type == NULL_TREE)
4643 build_type = result_type;
4645 result = build2 (resultcode, build_type, op0, op1);
4646 result = fold_if_not_in_template (result);
4647 if (final_type != 0)
4648 result = cp_convert (final_type, result, complain);
4650 if (TREE_OVERFLOW_P (result)
4651 && !TREE_OVERFLOW_P (op0)
4652 && !TREE_OVERFLOW_P (op1))
4653 overflow_warning (location, result);
4655 return result;
4658 /* Return a tree for the sum or difference (RESULTCODE says which)
4659 of pointer PTROP and integer INTOP. */
4661 static tree
4662 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4664 tree res_type = TREE_TYPE (ptrop);
4666 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4667 in certain circumstance (when it's valid to do so). So we need
4668 to make sure it's complete. We don't need to check here, if we
4669 can actually complete it at all, as those checks will be done in
4670 pointer_int_sum() anyway. */
4671 complete_type (TREE_TYPE (res_type));
4673 return pointer_int_sum (input_location, resultcode, ptrop,
4674 fold_if_not_in_template (intop));
4677 /* Return a tree for the difference of pointers OP0 and OP1.
4678 The resulting tree has type int. */
4680 static tree
4681 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
4683 tree result;
4684 tree restype = ptrdiff_type_node;
4685 tree target_type = TREE_TYPE (ptrtype);
4687 if (!complete_type_or_else (target_type, NULL_TREE))
4688 return error_mark_node;
4690 if (TREE_CODE (target_type) == VOID_TYPE)
4692 if (complain & tf_error)
4693 permerror (input_location, "ISO C++ forbids using pointer of "
4694 "type %<void *%> in subtraction");
4695 else
4696 return error_mark_node;
4698 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4700 if (complain & tf_error)
4701 permerror (input_location, "ISO C++ forbids using pointer to "
4702 "a function in subtraction");
4703 else
4704 return error_mark_node;
4706 if (TREE_CODE (target_type) == METHOD_TYPE)
4708 if (complain & tf_error)
4709 permerror (input_location, "ISO C++ forbids using pointer to "
4710 "a method in subtraction");
4711 else
4712 return error_mark_node;
4715 /* First do the subtraction as integers;
4716 then drop through to build the divide operator. */
4718 op0 = cp_build_binary_op (input_location,
4719 MINUS_EXPR,
4720 cp_convert (restype, op0, complain),
4721 cp_convert (restype, op1, complain),
4722 complain);
4724 /* This generates an error if op1 is a pointer to an incomplete type. */
4725 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4727 if (complain & tf_error)
4728 error ("invalid use of a pointer to an incomplete type in "
4729 "pointer arithmetic");
4730 else
4731 return error_mark_node;
4734 op1 = (TYPE_PTROB_P (ptrtype)
4735 ? size_in_bytes (target_type)
4736 : integer_one_node);
4738 /* Do the division. */
4740 result = build2 (EXACT_DIV_EXPR, restype, op0,
4741 cp_convert (restype, op1, complain));
4742 return fold_if_not_in_template (result);
4745 /* Construct and perhaps optimize a tree representation
4746 for a unary operation. CODE, a tree_code, specifies the operation
4747 and XARG is the operand. */
4749 tree
4750 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
4751 tsubst_flags_t complain)
4753 tree orig_expr = xarg;
4754 tree exp;
4755 int ptrmem = 0;
4757 if (processing_template_decl)
4759 if (type_dependent_expression_p (xarg))
4760 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
4762 xarg = build_non_dependent_expr (xarg);
4765 exp = NULL_TREE;
4767 /* [expr.unary.op] says:
4769 The address of an object of incomplete type can be taken.
4771 (And is just the ordinary address operator, not an overloaded
4772 "operator &".) However, if the type is a template
4773 specialization, we must complete the type at this point so that
4774 an overloaded "operator &" will be available if required. */
4775 if (code == ADDR_EXPR
4776 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4777 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4778 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4779 || (TREE_CODE (xarg) == OFFSET_REF)))
4780 /* Don't look for a function. */;
4781 else
4782 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
4783 NULL_TREE, /*overload=*/NULL, complain);
4784 if (!exp && code == ADDR_EXPR)
4786 if (is_overloaded_fn (xarg))
4788 tree fn = get_first_fn (xarg);
4789 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4791 error (DECL_CONSTRUCTOR_P (fn)
4792 ? G_("taking address of constructor %qE")
4793 : G_("taking address of destructor %qE"),
4794 xarg);
4795 return error_mark_node;
4799 /* A pointer to member-function can be formed only by saying
4800 &X::mf. */
4801 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4802 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4804 if (TREE_CODE (xarg) != OFFSET_REF
4805 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4807 error ("invalid use of %qE to form a pointer-to-member-function",
4808 xarg);
4809 if (TREE_CODE (xarg) != OFFSET_REF)
4810 inform (input_location, " a qualified-id is required");
4811 return error_mark_node;
4813 else
4815 error ("parentheses around %qE cannot be used to form a"
4816 " pointer-to-member-function",
4817 xarg);
4818 PTRMEM_OK_P (xarg) = 1;
4822 if (TREE_CODE (xarg) == OFFSET_REF)
4824 ptrmem = PTRMEM_OK_P (xarg);
4826 if (!ptrmem && !flag_ms_extensions
4827 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4829 /* A single non-static member, make sure we don't allow a
4830 pointer-to-member. */
4831 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4832 TREE_OPERAND (xarg, 0),
4833 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4834 PTRMEM_OK_P (xarg) = ptrmem;
4838 exp = cp_build_addr_expr_strict (xarg, complain);
4841 if (processing_template_decl && exp != error_mark_node)
4842 exp = build_min_non_dep (code, exp, orig_expr,
4843 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4844 if (TREE_CODE (exp) == ADDR_EXPR)
4845 PTRMEM_OK_P (exp) = ptrmem;
4846 return exp;
4849 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4850 constants, where a null value is represented by an INTEGER_CST of
4851 -1. */
4853 tree
4854 cp_truthvalue_conversion (tree expr)
4856 tree type = TREE_TYPE (expr);
4857 if (TYPE_PTRDATAMEM_P (type))
4858 return build_binary_op (EXPR_LOCATION (expr),
4859 NE_EXPR, expr, nullptr_node, 1);
4860 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
4862 /* With -Wzero-as-null-pointer-constant do not warn for an
4863 'if (p)' or a 'while (!p)', where p is a pointer. */
4864 tree ret;
4865 ++c_inhibit_evaluation_warnings;
4866 ret = c_common_truthvalue_conversion (input_location, expr);
4867 --c_inhibit_evaluation_warnings;
4868 return ret;
4870 else
4871 return c_common_truthvalue_conversion (input_location, expr);
4874 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4876 tree
4877 condition_conversion (tree expr)
4879 tree t;
4880 if (processing_template_decl)
4881 return expr;
4882 t = perform_implicit_conversion_flags (boolean_type_node, expr,
4883 tf_warning_or_error, LOOKUP_NORMAL);
4884 t = fold_build_cleanup_point_expr (boolean_type_node, t);
4885 return t;
4888 /* Returns the address of T. This function will fold away
4889 ADDR_EXPR of INDIRECT_REF. */
4891 tree
4892 build_address (tree t)
4894 if (error_operand_p (t) || !cxx_mark_addressable (t))
4895 return error_mark_node;
4896 t = build_fold_addr_expr (t);
4897 if (TREE_CODE (t) != ADDR_EXPR)
4898 t = rvalue (t);
4899 return t;
4902 /* Returns the address of T with type TYPE. */
4904 tree
4905 build_typed_address (tree t, tree type)
4907 if (error_operand_p (t) || !cxx_mark_addressable (t))
4908 return error_mark_node;
4909 t = build_fold_addr_expr_with_type (t, type);
4910 if (TREE_CODE (t) != ADDR_EXPR)
4911 t = rvalue (t);
4912 return t;
4915 /* Return a NOP_EXPR converting EXPR to TYPE. */
4917 tree
4918 build_nop (tree type, tree expr)
4920 if (type == error_mark_node || error_operand_p (expr))
4921 return expr;
4922 return build1 (NOP_EXPR, type, expr);
4925 /* Take the address of ARG, whatever that means under C++ semantics.
4926 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
4927 and class rvalues as well.
4929 Nothing should call this function directly; instead, callers should use
4930 cp_build_addr_expr or cp_build_addr_expr_strict. */
4932 static tree
4933 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
4935 tree argtype;
4936 tree val;
4938 if (!arg || error_operand_p (arg))
4939 return error_mark_node;
4941 arg = mark_lvalue_use (arg);
4942 argtype = lvalue_type (arg);
4944 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4945 || !IDENTIFIER_OPNAME_P (arg));
4947 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4948 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4950 /* They're trying to take the address of a unique non-static
4951 member function. This is ill-formed (except in MS-land),
4952 but let's try to DTRT.
4953 Note: We only handle unique functions here because we don't
4954 want to complain if there's a static overload; non-unique
4955 cases will be handled by instantiate_type. But we need to
4956 handle this case here to allow casts on the resulting PMF.
4957 We could defer this in non-MS mode, but it's easier to give
4958 a useful error here. */
4960 /* Inside constant member functions, the `this' pointer
4961 contains an extra const qualifier. TYPE_MAIN_VARIANT
4962 is used here to remove this const from the diagnostics
4963 and the created OFFSET_REF. */
4964 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4965 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4966 mark_used (fn);
4968 if (! flag_ms_extensions)
4970 tree name = DECL_NAME (fn);
4971 if (!(complain & tf_error))
4972 return error_mark_node;
4973 else if (current_class_type
4974 && TREE_OPERAND (arg, 0) == current_class_ref)
4975 /* An expression like &memfn. */
4976 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
4977 " or parenthesized non-static member function to form"
4978 " a pointer to member function. Say %<&%T::%D%>",
4979 base, name);
4980 else
4981 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
4982 " function to form a pointer to member function."
4983 " Say %<&%T::%D%>",
4984 base, name);
4986 arg = build_offset_ref (base, fn, /*address_p=*/true);
4989 /* Uninstantiated types are all functions. Taking the
4990 address of a function is a no-op, so just return the
4991 argument. */
4992 if (type_unknown_p (arg))
4993 return build1 (ADDR_EXPR, unknown_type_node, arg);
4995 if (TREE_CODE (arg) == OFFSET_REF)
4996 /* We want a pointer to member; bypass all the code for actually taking
4997 the address of something. */
4998 goto offset_ref;
5000 /* Anything not already handled and not a true memory reference
5001 is an error. */
5002 if (TREE_CODE (argtype) != FUNCTION_TYPE
5003 && TREE_CODE (argtype) != METHOD_TYPE)
5005 cp_lvalue_kind kind = lvalue_kind (arg);
5006 if (kind == clk_none)
5008 if (complain & tf_error)
5009 lvalue_error (input_location, lv_addressof);
5010 return error_mark_node;
5012 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5014 if (!(complain & tf_error))
5015 return error_mark_node;
5016 if (kind & clk_class)
5017 /* Make this a permerror because we used to accept it. */
5018 permerror (input_location, "taking address of temporary");
5019 else
5020 error ("taking address of xvalue (rvalue reference)");
5024 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5026 tree type = build_pointer_type (TREE_TYPE (argtype));
5027 arg = build1 (CONVERT_EXPR, type, arg);
5028 return arg;
5030 else if (pedantic && DECL_MAIN_P (arg))
5032 /* ARM $3.4 */
5033 /* Apparently a lot of autoconf scripts for C++ packages do this,
5034 so only complain if -Wpedantic. */
5035 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5036 pedwarn (input_location, OPT_Wpedantic,
5037 "ISO C++ forbids taking address of function %<::main%>");
5038 else if (flag_pedantic_errors)
5039 return error_mark_node;
5042 /* Let &* cancel out to simplify resulting code. */
5043 if (TREE_CODE (arg) == INDIRECT_REF)
5045 /* We don't need to have `current_class_ptr' wrapped in a
5046 NON_LVALUE_EXPR node. */
5047 if (arg == current_class_ref)
5048 return current_class_ptr;
5050 arg = TREE_OPERAND (arg, 0);
5051 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5053 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5054 arg = build1 (CONVERT_EXPR, type, arg);
5056 else
5057 /* Don't let this be an lvalue. */
5058 arg = rvalue (arg);
5059 return arg;
5062 /* ??? Cope with user tricks that amount to offsetof. */
5063 if (TREE_CODE (argtype) != FUNCTION_TYPE
5064 && TREE_CODE (argtype) != METHOD_TYPE
5065 && argtype != unknown_type_node
5066 && (val = get_base_address (arg))
5067 && COMPLETE_TYPE_P (TREE_TYPE (val))
5068 && TREE_CODE (val) == INDIRECT_REF
5069 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5071 tree type = build_pointer_type (argtype);
5072 return fold_convert (type, fold_offsetof_1 (arg));
5075 /* Handle complex lvalues (when permitted)
5076 by reduction to simpler cases. */
5077 val = unary_complex_lvalue (ADDR_EXPR, arg);
5078 if (val != 0)
5079 return val;
5081 switch (TREE_CODE (arg))
5083 CASE_CONVERT:
5084 case FLOAT_EXPR:
5085 case FIX_TRUNC_EXPR:
5086 /* Even if we're not being pedantic, we cannot allow this
5087 extension when we're instantiating in a SFINAE
5088 context. */
5089 if (! lvalue_p (arg) && complain == tf_none)
5091 if (complain & tf_error)
5092 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5093 else
5094 return error_mark_node;
5096 break;
5098 case BASELINK:
5099 arg = BASELINK_FUNCTIONS (arg);
5100 /* Fall through. */
5102 case OVERLOAD:
5103 arg = OVL_CURRENT (arg);
5104 break;
5106 case OFFSET_REF:
5107 offset_ref:
5108 /* Turn a reference to a non-static data member into a
5109 pointer-to-member. */
5111 tree type;
5112 tree t;
5114 gcc_assert (PTRMEM_OK_P (arg));
5116 t = TREE_OPERAND (arg, 1);
5117 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5119 if (complain & tf_error)
5120 error ("cannot create pointer to reference member %qD", t);
5121 return error_mark_node;
5124 type = build_ptrmem_type (context_for_name_lookup (t),
5125 TREE_TYPE (t));
5126 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5127 return t;
5130 default:
5131 break;
5134 if (argtype != error_mark_node)
5135 argtype = build_pointer_type (argtype);
5137 /* In a template, we are processing a non-dependent expression
5138 so we can just form an ADDR_EXPR with the correct type. */
5139 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5141 val = build_address (arg);
5142 if (TREE_CODE (arg) == OFFSET_REF)
5143 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5145 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5147 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5149 /* We can only get here with a single static member
5150 function. */
5151 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5152 && DECL_STATIC_FUNCTION_P (fn));
5153 mark_used (fn);
5154 val = build_address (fn);
5155 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5156 /* Do not lose object's side effects. */
5157 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5158 TREE_OPERAND (arg, 0), val);
5160 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5162 if (complain & tf_error)
5163 error ("attempt to take address of bit-field structure member %qD",
5164 TREE_OPERAND (arg, 1));
5165 return error_mark_node;
5167 else
5169 tree object = TREE_OPERAND (arg, 0);
5170 tree field = TREE_OPERAND (arg, 1);
5171 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5172 (TREE_TYPE (object), decl_type_context (field)));
5173 val = build_address (arg);
5176 if (TREE_CODE (argtype) == POINTER_TYPE
5177 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5179 build_ptrmemfunc_type (argtype);
5180 val = build_ptrmemfunc (argtype, val, 0,
5181 /*c_cast_p=*/false,
5182 complain);
5185 return val;
5188 /* Take the address of ARG if it has one, even if it's an rvalue. */
5190 tree
5191 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5193 return cp_build_addr_expr_1 (arg, 0, complain);
5196 /* Take the address of ARG, but only if it's an lvalue. */
5198 tree
5199 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5201 return cp_build_addr_expr_1 (arg, 1, complain);
5204 /* C++: Must handle pointers to members.
5206 Perhaps type instantiation should be extended to handle conversion
5207 from aggregates to types we don't yet know we want? (Or are those
5208 cases typically errors which should be reported?)
5210 NOCONVERT nonzero suppresses the default promotions
5211 (such as from short to int). */
5213 tree
5214 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5215 tsubst_flags_t complain)
5217 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5218 tree arg = xarg;
5219 tree argtype = 0;
5220 const char *errstring = NULL;
5221 tree val;
5222 const char *invalid_op_diag;
5224 if (!arg || error_operand_p (arg))
5225 return error_mark_node;
5227 if ((invalid_op_diag
5228 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5229 ? CONVERT_EXPR
5230 : code),
5231 TREE_TYPE (xarg))))
5233 error (invalid_op_diag);
5234 return error_mark_node;
5237 switch (code)
5239 case UNARY_PLUS_EXPR:
5240 case NEGATE_EXPR:
5242 int flags = WANT_ARITH | WANT_ENUM;
5243 /* Unary plus (but not unary minus) is allowed on pointers. */
5244 if (code == UNARY_PLUS_EXPR)
5245 flags |= WANT_POINTER;
5246 arg = build_expr_type_conversion (flags, arg, true);
5247 if (!arg)
5248 errstring = (code == NEGATE_EXPR
5249 ? _("wrong type argument to unary minus")
5250 : _("wrong type argument to unary plus"));
5251 else
5253 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5254 arg = cp_perform_integral_promotions (arg, complain);
5256 /* Make sure the result is not an lvalue: a unary plus or minus
5257 expression is always a rvalue. */
5258 arg = rvalue (arg);
5261 break;
5263 case BIT_NOT_EXPR:
5264 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5266 code = CONJ_EXPR;
5267 if (!noconvert)
5269 arg = cp_default_conversion (arg, complain);
5270 if (arg == error_mark_node)
5271 return error_mark_node;
5274 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5275 | WANT_VECTOR_OR_COMPLEX,
5276 arg, true)))
5277 errstring = _("wrong type argument to bit-complement");
5278 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5279 arg = cp_perform_integral_promotions (arg, complain);
5280 break;
5282 case ABS_EXPR:
5283 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5284 errstring = _("wrong type argument to abs");
5285 else if (!noconvert)
5287 arg = cp_default_conversion (arg, complain);
5288 if (arg == error_mark_node)
5289 return error_mark_node;
5291 break;
5293 case CONJ_EXPR:
5294 /* Conjugating a real value is a no-op, but allow it anyway. */
5295 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5296 errstring = _("wrong type argument to conjugation");
5297 else if (!noconvert)
5299 arg = cp_default_conversion (arg, complain);
5300 if (arg == error_mark_node)
5301 return error_mark_node;
5303 break;
5305 case TRUTH_NOT_EXPR:
5306 arg = perform_implicit_conversion (boolean_type_node, arg,
5307 complain);
5308 val = invert_truthvalue_loc (input_location, arg);
5309 if (arg != error_mark_node)
5310 return val;
5311 errstring = _("in argument to unary !");
5312 break;
5314 case NOP_EXPR:
5315 break;
5317 case REALPART_EXPR:
5318 case IMAGPART_EXPR:
5319 arg = build_real_imag_expr (input_location, code, arg);
5320 if (arg == error_mark_node)
5321 return arg;
5322 else
5323 return fold_if_not_in_template (arg);
5325 case PREINCREMENT_EXPR:
5326 case POSTINCREMENT_EXPR:
5327 case PREDECREMENT_EXPR:
5328 case POSTDECREMENT_EXPR:
5329 /* Handle complex lvalues (when permitted)
5330 by reduction to simpler cases. */
5332 val = unary_complex_lvalue (code, arg);
5333 if (val != 0)
5334 return val;
5336 arg = mark_lvalue_use (arg);
5338 /* Increment or decrement the real part of the value,
5339 and don't change the imaginary part. */
5340 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5342 tree real, imag;
5344 arg = stabilize_reference (arg);
5345 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5346 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5347 real = cp_build_unary_op (code, real, 1, complain);
5348 if (real == error_mark_node || imag == error_mark_node)
5349 return error_mark_node;
5350 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5351 real, imag);
5354 /* Report invalid types. */
5356 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5357 arg, true)))
5359 if (code == PREINCREMENT_EXPR)
5360 errstring = _("no pre-increment operator for type");
5361 else if (code == POSTINCREMENT_EXPR)
5362 errstring = _("no post-increment operator for type");
5363 else if (code == PREDECREMENT_EXPR)
5364 errstring = _("no pre-decrement operator for type");
5365 else
5366 errstring = _("no post-decrement operator for type");
5367 break;
5369 else if (arg == error_mark_node)
5370 return error_mark_node;
5372 /* Report something read-only. */
5374 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5375 || TREE_READONLY (arg))
5377 if (complain & tf_error)
5378 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5379 || code == POSTINCREMENT_EXPR)
5380 ? lv_increment : lv_decrement));
5381 else
5382 return error_mark_node;
5386 tree inc;
5387 tree declared_type = unlowered_expr_type (arg);
5389 argtype = TREE_TYPE (arg);
5391 /* ARM $5.2.5 last annotation says this should be forbidden. */
5392 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5394 if (complain & tf_error)
5395 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5396 ? G_("ISO C++ forbids incrementing an enum")
5397 : G_("ISO C++ forbids decrementing an enum"));
5398 else
5399 return error_mark_node;
5402 /* Compute the increment. */
5404 if (TREE_CODE (argtype) == POINTER_TYPE)
5406 tree type = complete_type (TREE_TYPE (argtype));
5408 if (!COMPLETE_OR_VOID_TYPE_P (type))
5410 if (complain & tf_error)
5411 error (((code == PREINCREMENT_EXPR
5412 || code == POSTINCREMENT_EXPR))
5413 ? G_("cannot increment a pointer to incomplete type %qT")
5414 : G_("cannot decrement a pointer to incomplete type %qT"),
5415 TREE_TYPE (argtype));
5416 else
5417 return error_mark_node;
5419 else if ((pedantic || warn_pointer_arith)
5420 && !TYPE_PTROB_P (argtype))
5422 if (complain & tf_error)
5423 permerror (input_location, (code == PREINCREMENT_EXPR
5424 || code == POSTINCREMENT_EXPR)
5425 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5426 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5427 argtype);
5428 else
5429 return error_mark_node;
5432 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5434 else
5435 inc = integer_one_node;
5437 inc = cp_convert (argtype, inc, complain);
5439 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5440 need to ask Objective-C to build the increment or decrement
5441 expression for it. */
5442 if (objc_is_property_ref (arg))
5443 return objc_build_incr_expr_for_property_ref (input_location, code,
5444 arg, inc);
5446 /* Complain about anything else that is not a true lvalue. */
5447 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5448 || code == POSTINCREMENT_EXPR)
5449 ? lv_increment : lv_decrement),
5450 complain))
5451 return error_mark_node;
5453 /* Forbid using -- on `bool'. */
5454 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5456 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5458 if (complain & tf_error)
5459 error ("invalid use of Boolean expression as operand "
5460 "to %<operator--%>");
5461 return error_mark_node;
5463 val = boolean_increment (code, arg);
5465 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5466 /* An rvalue has no cv-qualifiers. */
5467 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5468 else
5469 val = build2 (code, TREE_TYPE (arg), arg, inc);
5471 TREE_SIDE_EFFECTS (val) = 1;
5472 return val;
5475 case ADDR_EXPR:
5476 /* Note that this operation never does default_conversion
5477 regardless of NOCONVERT. */
5478 return cp_build_addr_expr (arg, complain);
5480 default:
5481 break;
5484 if (!errstring)
5486 if (argtype == 0)
5487 argtype = TREE_TYPE (arg);
5488 return fold_if_not_in_template (build1 (code, argtype, arg));
5491 if (complain & tf_error)
5492 error ("%s", errstring);
5493 return error_mark_node;
5496 /* Hook for the c-common bits that build a unary op. */
5497 tree
5498 build_unary_op (location_t /*location*/,
5499 enum tree_code code, tree xarg, int noconvert)
5501 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5504 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5505 for certain kinds of expressions which are not really lvalues
5506 but which we can accept as lvalues.
5508 If ARG is not a kind of expression we can handle, return
5509 NULL_TREE. */
5511 tree
5512 unary_complex_lvalue (enum tree_code code, tree arg)
5514 /* Inside a template, making these kinds of adjustments is
5515 pointless; we are only concerned with the type of the
5516 expression. */
5517 if (processing_template_decl)
5518 return NULL_TREE;
5520 /* Handle (a, b) used as an "lvalue". */
5521 if (TREE_CODE (arg) == COMPOUND_EXPR)
5523 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5524 tf_warning_or_error);
5525 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5526 TREE_OPERAND (arg, 0), real_result);
5529 /* Handle (a ? b : c) used as an "lvalue". */
5530 if (TREE_CODE (arg) == COND_EXPR
5531 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5532 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5534 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5535 if (TREE_CODE (arg) == MODIFY_EXPR
5536 || TREE_CODE (arg) == PREINCREMENT_EXPR
5537 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5539 tree lvalue = TREE_OPERAND (arg, 0);
5540 if (TREE_SIDE_EFFECTS (lvalue))
5542 lvalue = stabilize_reference (lvalue);
5543 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5544 lvalue, TREE_OPERAND (arg, 1));
5546 return unary_complex_lvalue
5547 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5550 if (code != ADDR_EXPR)
5551 return NULL_TREE;
5553 /* Handle (a = b) used as an "lvalue" for `&'. */
5554 if (TREE_CODE (arg) == MODIFY_EXPR
5555 || TREE_CODE (arg) == INIT_EXPR)
5557 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5558 tf_warning_or_error);
5559 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5560 arg, real_result);
5561 TREE_NO_WARNING (arg) = 1;
5562 return arg;
5565 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5566 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5567 || TREE_CODE (arg) == OFFSET_REF)
5568 return NULL_TREE;
5570 /* We permit compiler to make function calls returning
5571 objects of aggregate type look like lvalues. */
5573 tree targ = arg;
5575 if (TREE_CODE (targ) == SAVE_EXPR)
5576 targ = TREE_OPERAND (targ, 0);
5578 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5580 if (TREE_CODE (arg) == SAVE_EXPR)
5581 targ = arg;
5582 else
5583 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5584 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5587 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5588 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5589 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5592 /* Don't let anything else be handled specially. */
5593 return NULL_TREE;
5596 /* Mark EXP saying that we need to be able to take the
5597 address of it; it should not be allocated in a register.
5598 Value is true if successful.
5600 C++: we do not allow `current_class_ptr' to be addressable. */
5602 bool
5603 cxx_mark_addressable (tree exp)
5605 tree x = exp;
5607 while (1)
5608 switch (TREE_CODE (x))
5610 case ADDR_EXPR:
5611 case COMPONENT_REF:
5612 case ARRAY_REF:
5613 case REALPART_EXPR:
5614 case IMAGPART_EXPR:
5615 x = TREE_OPERAND (x, 0);
5616 break;
5618 case PARM_DECL:
5619 if (x == current_class_ptr)
5621 error ("cannot take the address of %<this%>, which is an rvalue expression");
5622 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5623 return true;
5625 /* Fall through. */
5627 case VAR_DECL:
5628 /* Caller should not be trying to mark initialized
5629 constant fields addressable. */
5630 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5631 || DECL_IN_AGGR_P (x) == 0
5632 || TREE_STATIC (x)
5633 || DECL_EXTERNAL (x));
5634 /* Fall through. */
5636 case RESULT_DECL:
5637 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5638 && !DECL_ARTIFICIAL (x))
5640 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
5642 error
5643 ("address of explicit register variable %qD requested", x);
5644 return false;
5646 else if (extra_warnings)
5647 warning
5648 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5650 TREE_ADDRESSABLE (x) = 1;
5651 return true;
5653 case CONST_DECL:
5654 case FUNCTION_DECL:
5655 TREE_ADDRESSABLE (x) = 1;
5656 return true;
5658 case CONSTRUCTOR:
5659 TREE_ADDRESSABLE (x) = 1;
5660 return true;
5662 case TARGET_EXPR:
5663 TREE_ADDRESSABLE (x) = 1;
5664 cxx_mark_addressable (TREE_OPERAND (x, 0));
5665 return true;
5667 default:
5668 return true;
5672 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5674 tree
5675 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
5676 tsubst_flags_t complain)
5678 tree orig_ifexp = ifexp;
5679 tree orig_op1 = op1;
5680 tree orig_op2 = op2;
5681 tree expr;
5683 if (processing_template_decl)
5685 /* The standard says that the expression is type-dependent if
5686 IFEXP is type-dependent, even though the eventual type of the
5687 expression doesn't dependent on IFEXP. */
5688 if (type_dependent_expression_p (ifexp)
5689 /* As a GNU extension, the middle operand may be omitted. */
5690 || (op1 && type_dependent_expression_p (op1))
5691 || type_dependent_expression_p (op2))
5692 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
5693 ifexp = build_non_dependent_expr (ifexp);
5694 if (op1)
5695 op1 = build_non_dependent_expr (op1);
5696 op2 = build_non_dependent_expr (op2);
5699 expr = build_conditional_expr (ifexp, op1, op2, complain);
5700 if (processing_template_decl && expr != error_mark_node)
5702 tree min = build_min_non_dep (COND_EXPR, expr,
5703 orig_ifexp, orig_op1, orig_op2);
5704 /* In C++11, remember that the result is an lvalue or xvalue.
5705 In C++98, lvalue_kind can just assume lvalue in a template. */
5706 if (cxx_dialect >= cxx0x
5707 && lvalue_or_rvalue_with_address_p (expr)
5708 && !lvalue_or_rvalue_with_address_p (min))
5709 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5710 !real_lvalue_p (expr));
5711 expr = convert_from_reference (min);
5713 return expr;
5716 /* Given a list of expressions, return a compound expression
5717 that performs them all and returns the value of the last of them. */
5719 tree
5720 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5721 tsubst_flags_t complain)
5723 tree expr = TREE_VALUE (list);
5725 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5726 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5728 if (complain & tf_error)
5729 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5730 "non-class type must not be parenthesized");
5731 else
5732 return error_mark_node;
5735 if (TREE_CHAIN (list))
5737 if (complain & tf_error)
5738 switch (exp)
5740 case ELK_INIT:
5741 permerror (input_location, "expression list treated as compound "
5742 "expression in initializer");
5743 break;
5744 case ELK_MEM_INIT:
5745 permerror (input_location, "expression list treated as compound "
5746 "expression in mem-initializer");
5747 break;
5748 case ELK_FUNC_CAST:
5749 permerror (input_location, "expression list treated as compound "
5750 "expression in functional cast");
5751 break;
5752 default:
5753 gcc_unreachable ();
5755 else
5756 return error_mark_node;
5758 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5759 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
5760 expr, TREE_VALUE (list), complain);
5763 return expr;
5766 /* Like build_x_compound_expr_from_list, but using a VEC. */
5768 tree
5769 build_x_compound_expr_from_vec (VEC(tree,gc) *vec, const char *msg,
5770 tsubst_flags_t complain)
5772 if (VEC_empty (tree, vec))
5773 return NULL_TREE;
5774 else if (VEC_length (tree, vec) == 1)
5775 return VEC_index (tree, vec, 0);
5776 else
5778 tree expr;
5779 unsigned int ix;
5780 tree t;
5782 if (msg != NULL)
5784 if (complain & tf_error)
5785 permerror (input_location,
5786 "%s expression list treated as compound expression",
5787 msg);
5788 else
5789 return error_mark_node;
5792 expr = VEC_index (tree, vec, 0);
5793 for (ix = 1; VEC_iterate (tree, vec, ix, t); ++ix)
5794 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
5795 t, complain);
5797 return expr;
5801 /* Handle overloading of the ',' operator when needed. */
5803 tree
5804 build_x_compound_expr (location_t loc, tree op1, tree op2,
5805 tsubst_flags_t complain)
5807 tree result;
5808 tree orig_op1 = op1;
5809 tree orig_op2 = op2;
5811 if (processing_template_decl)
5813 if (type_dependent_expression_p (op1)
5814 || type_dependent_expression_p (op2))
5815 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
5816 op1 = build_non_dependent_expr (op1);
5817 op2 = build_non_dependent_expr (op2);
5820 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
5821 NULL_TREE, /*overload=*/NULL, complain);
5822 if (!result)
5823 result = cp_build_compound_expr (op1, op2, complain);
5825 if (processing_template_decl && result != error_mark_node)
5826 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
5828 return result;
5831 /* Like cp_build_compound_expr, but for the c-common bits. */
5833 tree
5834 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
5836 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
5839 /* Build a compound expression. */
5841 tree
5842 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
5844 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
5846 if (lhs == error_mark_node || rhs == error_mark_node)
5847 return error_mark_node;
5849 if (TREE_CODE (rhs) == TARGET_EXPR)
5851 /* If the rhs is a TARGET_EXPR, then build the compound
5852 expression inside the target_expr's initializer. This
5853 helps the compiler to eliminate unnecessary temporaries. */
5854 tree init = TREE_OPERAND (rhs, 1);
5856 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
5857 TREE_OPERAND (rhs, 1) = init;
5859 return rhs;
5862 if (type_unknown_p (rhs))
5864 error ("no context to resolve type of %qE", rhs);
5865 return error_mark_node;
5868 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
5871 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
5872 casts away constness. CAST gives the type of cast. Returns true
5873 if the cast is ill-formed, false if it is well-formed.
5875 ??? This function warns for casting away any qualifier not just
5876 const. We would like to specify exactly what qualifiers are casted
5877 away.
5880 static bool
5881 check_for_casting_away_constness (tree src_type, tree dest_type,
5882 enum tree_code cast, tsubst_flags_t complain)
5884 /* C-style casts are allowed to cast away constness. With
5885 WARN_CAST_QUAL, we still want to issue a warning. */
5886 if (cast == CAST_EXPR && !warn_cast_qual)
5887 return false;
5889 if (!casts_away_constness (src_type, dest_type, complain))
5890 return false;
5892 switch (cast)
5894 case CAST_EXPR:
5895 if (complain & tf_warning)
5896 warning (OPT_Wcast_qual,
5897 "cast from type %qT to type %qT casts away qualifiers",
5898 src_type, dest_type);
5899 return false;
5901 case STATIC_CAST_EXPR:
5902 if (complain & tf_error)
5903 error ("static_cast from type %qT to type %qT casts away qualifiers",
5904 src_type, dest_type);
5905 return true;
5907 case REINTERPRET_CAST_EXPR:
5908 if (complain & tf_error)
5909 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
5910 src_type, dest_type);
5911 return true;
5913 default:
5914 gcc_unreachable();
5919 Warns if the cast from expression EXPR to type TYPE is useless.
5921 void
5922 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
5924 if (warn_useless_cast
5925 && complain & tf_warning
5926 && c_inhibit_evaluation_warnings == 0)
5928 if (REFERENCE_REF_P (expr))
5929 expr = TREE_OPERAND (expr, 0);
5931 if ((TREE_CODE (type) == REFERENCE_TYPE
5932 && (TYPE_REF_IS_RVALUE (type)
5933 ? xvalue_p (expr) : real_lvalue_p (expr))
5934 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
5935 || same_type_p (TREE_TYPE (expr), type))
5936 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
5940 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
5941 (another pointer-to-member type in the same hierarchy) and return
5942 the converted expression. If ALLOW_INVERSE_P is permitted, a
5943 pointer-to-derived may be converted to pointer-to-base; otherwise,
5944 only the other direction is permitted. If C_CAST_P is true, this
5945 conversion is taking place as part of a C-style cast. */
5947 tree
5948 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
5949 bool c_cast_p, tsubst_flags_t complain)
5951 if (TYPE_PTRDATAMEM_P (type))
5953 tree delta;
5955 if (TREE_CODE (expr) == PTRMEM_CST)
5956 expr = cplus_expand_constant (expr);
5957 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
5958 TYPE_PTRMEM_CLASS_TYPE (type),
5959 allow_inverse_p,
5960 c_cast_p, complain);
5961 if (delta == error_mark_node)
5962 return error_mark_node;
5964 if (!integer_zerop (delta))
5966 tree cond, op1, op2;
5968 cond = cp_build_binary_op (input_location,
5969 EQ_EXPR,
5970 expr,
5971 build_int_cst (TREE_TYPE (expr), -1),
5972 complain);
5973 op1 = build_nop (ptrdiff_type_node, expr);
5974 op2 = cp_build_binary_op (input_location,
5975 PLUS_EXPR, op1, delta,
5976 complain);
5978 expr = fold_build3_loc (input_location,
5979 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
5983 return build_nop (type, expr);
5985 else
5986 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
5987 allow_inverse_p, c_cast_p, complain);
5990 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
5991 this static_cast is being attempted as one of the possible casts
5992 allowed by a C-style cast. (In that case, accessibility of base
5993 classes is not considered, and it is OK to cast away
5994 constness.) Return the result of the cast. *VALID_P is set to
5995 indicate whether or not the cast was valid. */
5997 static tree
5998 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
5999 bool *valid_p, tsubst_flags_t complain)
6001 tree intype;
6002 tree result;
6003 cp_lvalue_kind clk;
6005 /* Assume the cast is valid. */
6006 *valid_p = true;
6008 intype = unlowered_expr_type (expr);
6010 /* Save casted types in the function's used types hash table. */
6011 used_types_insert (type);
6013 /* [expr.static.cast]
6015 An lvalue of type "cv1 B", where B is a class type, can be cast
6016 to type "reference to cv2 D", where D is a class derived (clause
6017 _class.derived_) from B, if a valid standard conversion from
6018 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6019 same cv-qualification as, or greater cv-qualification than, cv1,
6020 and B is not a virtual base class of D. */
6021 /* We check this case before checking the validity of "TYPE t =
6022 EXPR;" below because for this case:
6024 struct B {};
6025 struct D : public B { D(const B&); };
6026 extern B& b;
6027 void f() { static_cast<const D&>(b); }
6029 we want to avoid constructing a new D. The standard is not
6030 completely clear about this issue, but our interpretation is
6031 consistent with other compilers. */
6032 if (TREE_CODE (type) == REFERENCE_TYPE
6033 && CLASS_TYPE_P (TREE_TYPE (type))
6034 && CLASS_TYPE_P (intype)
6035 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6036 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6037 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6038 build_pointer_type (TYPE_MAIN_VARIANT
6039 (TREE_TYPE (type))),
6040 complain)
6041 && (c_cast_p
6042 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6044 tree base;
6046 /* There is a standard conversion from "D*" to "B*" even if "B"
6047 is ambiguous or inaccessible. If this is really a
6048 static_cast, then we check both for inaccessibility and
6049 ambiguity. However, if this is a static_cast being performed
6050 because the user wrote a C-style cast, then accessibility is
6051 not considered. */
6052 base = lookup_base (TREE_TYPE (type), intype,
6053 c_cast_p ? ba_unique : ba_check,
6054 NULL, complain);
6056 /* Convert from "B*" to "D*". This function will check that "B"
6057 is not a virtual base of "D". */
6058 expr = build_base_path (MINUS_EXPR, build_address (expr),
6059 base, /*nonnull=*/false, complain);
6060 /* Convert the pointer to a reference -- but then remember that
6061 there are no expressions with reference type in C++.
6063 We call rvalue so that there's an actual tree code
6064 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6065 is a variable with the same type, the conversion would get folded
6066 away, leaving just the variable and causing lvalue_kind to give
6067 the wrong answer. */
6068 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6071 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6072 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6073 if (TREE_CODE (type) == REFERENCE_TYPE
6074 && TYPE_REF_IS_RVALUE (type)
6075 && (clk = real_lvalue_p (expr))
6076 && reference_related_p (TREE_TYPE (type), intype)
6077 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6079 if (clk == clk_ordinary)
6081 /* Handle the (non-bit-field) lvalue case here by casting to
6082 lvalue reference and then changing it to an rvalue reference.
6083 Casting an xvalue to rvalue reference will be handled by the
6084 main code path. */
6085 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6086 result = (perform_direct_initialization_if_possible
6087 (lref, expr, c_cast_p, complain));
6088 result = cp_fold_convert (type, result);
6089 /* Make sure we don't fold back down to a named rvalue reference,
6090 because that would be an lvalue. */
6091 if (DECL_P (result))
6092 result = build1 (NON_LVALUE_EXPR, type, result);
6093 return convert_from_reference (result);
6095 else
6096 /* For a bit-field or packed field, bind to a temporary. */
6097 expr = rvalue (expr);
6100 /* Resolve overloaded address here rather than once in
6101 implicit_conversion and again in the inverse code below. */
6102 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6104 expr = instantiate_type (type, expr, complain);
6105 intype = TREE_TYPE (expr);
6108 /* [expr.static.cast]
6110 Any expression can be explicitly converted to type cv void. */
6111 if (TREE_CODE (type) == VOID_TYPE)
6112 return convert_to_void (expr, ICV_CAST, complain);
6114 /* [expr.static.cast]
6116 An expression e can be explicitly converted to a type T using a
6117 static_cast of the form static_cast<T>(e) if the declaration T
6118 t(e);" is well-formed, for some invented temporary variable
6119 t. */
6120 result = perform_direct_initialization_if_possible (type, expr,
6121 c_cast_p, complain);
6122 if (result)
6124 result = convert_from_reference (result);
6126 /* [expr.static.cast]
6128 If T is a reference type, the result is an lvalue; otherwise,
6129 the result is an rvalue. */
6130 if (TREE_CODE (type) != REFERENCE_TYPE)
6131 result = rvalue (result);
6132 return result;
6135 /* [expr.static.cast]
6137 The inverse of any standard conversion sequence (clause _conv_),
6138 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6139 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6140 (_conv.bool_) conversions, can be performed explicitly using
6141 static_cast subject to the restriction that the explicit
6142 conversion does not cast away constness (_expr.const.cast_), and
6143 the following additional rules for specific cases: */
6144 /* For reference, the conversions not excluded are: integral
6145 promotions, floating point promotion, integral conversions,
6146 floating point conversions, floating-integral conversions,
6147 pointer conversions, and pointer to member conversions. */
6148 /* DR 128
6150 A value of integral _or enumeration_ type can be explicitly
6151 converted to an enumeration type. */
6152 /* The effect of all that is that any conversion between any two
6153 types which are integral, floating, or enumeration types can be
6154 performed. */
6155 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6156 || SCALAR_FLOAT_TYPE_P (type))
6157 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6158 || SCALAR_FLOAT_TYPE_P (intype)))
6159 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6161 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6162 && CLASS_TYPE_P (TREE_TYPE (type))
6163 && CLASS_TYPE_P (TREE_TYPE (intype))
6164 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6165 (TREE_TYPE (intype))),
6166 build_pointer_type (TYPE_MAIN_VARIANT
6167 (TREE_TYPE (type))),
6168 complain))
6170 tree base;
6172 if (!c_cast_p
6173 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6174 complain))
6175 return error_mark_node;
6176 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6177 c_cast_p ? ba_unique : ba_check,
6178 NULL, complain);
6179 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6180 complain);
6181 return cp_fold_convert(type, expr);
6184 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6185 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6187 tree c1;
6188 tree c2;
6189 tree t1;
6190 tree t2;
6192 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6193 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6195 if (TYPE_PTRDATAMEM_P (type))
6197 t1 = (build_ptrmem_type
6198 (c1,
6199 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6200 t2 = (build_ptrmem_type
6201 (c2,
6202 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6204 else
6206 t1 = intype;
6207 t2 = type;
6209 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6211 if (!c_cast_p
6212 && check_for_casting_away_constness (intype, type,
6213 STATIC_CAST_EXPR,
6214 complain))
6215 return error_mark_node;
6216 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6217 c_cast_p, complain);
6221 /* [expr.static.cast]
6223 An rvalue of type "pointer to cv void" can be explicitly
6224 converted to a pointer to object type. A value of type pointer
6225 to object converted to "pointer to cv void" and back to the
6226 original pointer type will have its original value. */
6227 if (TREE_CODE (intype) == POINTER_TYPE
6228 && VOID_TYPE_P (TREE_TYPE (intype))
6229 && TYPE_PTROB_P (type))
6231 if (!c_cast_p
6232 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6233 complain))
6234 return error_mark_node;
6235 return build_nop (type, expr);
6238 *valid_p = false;
6239 return error_mark_node;
6242 /* Return an expression representing static_cast<TYPE>(EXPR). */
6244 tree
6245 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6247 tree result;
6248 bool valid_p;
6250 if (type == error_mark_node || expr == error_mark_node)
6251 return error_mark_node;
6253 if (processing_template_decl)
6255 expr = build_min (STATIC_CAST_EXPR, type, expr);
6256 /* We don't know if it will or will not have side effects. */
6257 TREE_SIDE_EFFECTS (expr) = 1;
6258 return convert_from_reference (expr);
6261 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6262 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6263 if (TREE_CODE (type) != REFERENCE_TYPE
6264 && TREE_CODE (expr) == NOP_EXPR
6265 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6266 expr = TREE_OPERAND (expr, 0);
6268 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6269 complain);
6270 if (valid_p)
6272 if (result != error_mark_node)
6273 maybe_warn_about_useless_cast (type, expr, complain);
6274 return result;
6277 if (complain & tf_error)
6278 error ("invalid static_cast from type %qT to type %qT",
6279 TREE_TYPE (expr), type);
6280 return error_mark_node;
6283 /* EXPR is an expression with member function or pointer-to-member
6284 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6285 not permitted by ISO C++, but we accept it in some modes. If we
6286 are not in one of those modes, issue a diagnostic. Return the
6287 converted expression. */
6289 tree
6290 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6292 tree intype;
6293 tree decl;
6295 intype = TREE_TYPE (expr);
6296 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6297 || TREE_CODE (intype) == METHOD_TYPE);
6299 if (!(complain & tf_warning_or_error))
6300 return error_mark_node;
6302 if (pedantic || warn_pmf2ptr)
6303 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6304 "converting from %qT to %qT", intype, type);
6306 if (TREE_CODE (intype) == METHOD_TYPE)
6307 expr = build_addr_func (expr, complain);
6308 else if (TREE_CODE (expr) == PTRMEM_CST)
6309 expr = build_address (PTRMEM_CST_MEMBER (expr));
6310 else
6312 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6313 decl = build_address (decl);
6314 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6317 if (expr == error_mark_node)
6318 return error_mark_node;
6320 return build_nop (type, expr);
6323 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6324 If C_CAST_P is true, this reinterpret cast is being done as part of
6325 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6326 indicate whether or not reinterpret_cast was valid. */
6328 static tree
6329 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6330 bool *valid_p, tsubst_flags_t complain)
6332 tree intype;
6334 /* Assume the cast is invalid. */
6335 if (valid_p)
6336 *valid_p = true;
6338 if (type == error_mark_node || error_operand_p (expr))
6339 return error_mark_node;
6341 intype = TREE_TYPE (expr);
6343 /* Save casted types in the function's used types hash table. */
6344 used_types_insert (type);
6346 /* [expr.reinterpret.cast]
6347 An lvalue expression of type T1 can be cast to the type
6348 "reference to T2" if an expression of type "pointer to T1" can be
6349 explicitly converted to the type "pointer to T2" using a
6350 reinterpret_cast. */
6351 if (TREE_CODE (type) == REFERENCE_TYPE)
6353 if (! real_lvalue_p (expr))
6355 if (complain & tf_error)
6356 error ("invalid cast of an rvalue expression of type "
6357 "%qT to type %qT",
6358 intype, type);
6359 return error_mark_node;
6362 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6363 "B" are related class types; the reinterpret_cast does not
6364 adjust the pointer. */
6365 if (TYPE_PTR_P (intype)
6366 && (complain & tf_warning)
6367 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6368 COMPARE_BASE | COMPARE_DERIVED)))
6369 warning (0, "casting %qT to %qT does not dereference pointer",
6370 intype, type);
6372 expr = cp_build_addr_expr (expr, complain);
6374 if (warn_strict_aliasing > 2)
6375 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6377 if (expr != error_mark_node)
6378 expr = build_reinterpret_cast_1
6379 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6380 valid_p, complain);
6381 if (expr != error_mark_node)
6382 /* cp_build_indirect_ref isn't right for rvalue refs. */
6383 expr = convert_from_reference (fold_convert (type, expr));
6384 return expr;
6387 /* As a G++ extension, we consider conversions from member
6388 functions, and pointers to member functions to
6389 pointer-to-function and pointer-to-void types. If
6390 -Wno-pmf-conversions has not been specified,
6391 convert_member_func_to_ptr will issue an error message. */
6392 if ((TYPE_PTRMEMFUNC_P (intype)
6393 || TREE_CODE (intype) == METHOD_TYPE)
6394 && TYPE_PTR_P (type)
6395 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6396 || VOID_TYPE_P (TREE_TYPE (type))))
6397 return convert_member_func_to_ptr (type, expr, complain);
6399 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6400 array-to-pointer, and function-to-pointer conversions are
6401 performed. */
6402 expr = decay_conversion (expr, complain);
6404 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6405 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6406 if (TREE_CODE (expr) == NOP_EXPR
6407 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6408 expr = TREE_OPERAND (expr, 0);
6410 if (error_operand_p (expr))
6411 return error_mark_node;
6413 intype = TREE_TYPE (expr);
6415 /* [expr.reinterpret.cast]
6416 A pointer can be converted to any integral type large enough to
6417 hold it. ... A value of type std::nullptr_t can be converted to
6418 an integral type; the conversion has the same meaning and
6419 validity as a conversion of (void*)0 to the integral type. */
6420 if (CP_INTEGRAL_TYPE_P (type)
6421 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6423 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6425 if (complain & tf_error)
6426 permerror (input_location, "cast from %qT to %qT loses precision",
6427 intype, type);
6428 else
6429 return error_mark_node;
6431 if (NULLPTR_TYPE_P (intype))
6432 return build_int_cst (type, 0);
6434 /* [expr.reinterpret.cast]
6435 A value of integral or enumeration type can be explicitly
6436 converted to a pointer. */
6437 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6438 /* OK */
6440 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6441 || TYPE_PTR_OR_PTRMEM_P (type))
6442 && same_type_p (type, intype))
6443 /* DR 799 */
6444 return fold_if_not_in_template (build_nop (type, expr));
6445 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6446 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6447 return fold_if_not_in_template (build_nop (type, expr));
6448 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6449 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6451 tree sexpr = expr;
6453 if (!c_cast_p
6454 && check_for_casting_away_constness (intype, type,
6455 REINTERPRET_CAST_EXPR,
6456 complain))
6457 return error_mark_node;
6458 /* Warn about possible alignment problems. */
6459 if (STRICT_ALIGNMENT && warn_cast_align
6460 && (complain & tf_warning)
6461 && !VOID_TYPE_P (type)
6462 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6463 && COMPLETE_TYPE_P (TREE_TYPE (type))
6464 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6465 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6466 warning (OPT_Wcast_align, "cast from %qT to %qT "
6467 "increases required alignment of target type", intype, type);
6469 /* We need to strip nops here, because the front end likes to
6470 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6471 STRIP_NOPS (sexpr);
6472 if (warn_strict_aliasing <= 2)
6473 strict_aliasing_warning (intype, type, sexpr);
6475 return fold_if_not_in_template (build_nop (type, expr));
6477 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6478 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6480 if (pedantic && (complain & tf_warning))
6481 /* Only issue a warning, as we have always supported this
6482 where possible, and it is necessary in some cases. DR 195
6483 addresses this issue, but as of 2004/10/26 is still in
6484 drafting. */
6485 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6486 return fold_if_not_in_template (build_nop (type, expr));
6488 else if (TREE_CODE (type) == VECTOR_TYPE)
6489 return fold_if_not_in_template (convert_to_vector (type, expr));
6490 else if (TREE_CODE (intype) == VECTOR_TYPE
6491 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6492 return fold_if_not_in_template (convert_to_integer (type, expr));
6493 else
6495 if (valid_p)
6496 *valid_p = false;
6497 if (complain & tf_error)
6498 error ("invalid cast from type %qT to type %qT", intype, type);
6499 return error_mark_node;
6502 return cp_convert (type, expr, complain);
6505 tree
6506 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6508 tree r;
6510 if (type == error_mark_node || expr == error_mark_node)
6511 return error_mark_node;
6513 if (processing_template_decl)
6515 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6517 if (!TREE_SIDE_EFFECTS (t)
6518 && type_dependent_expression_p (expr))
6519 /* There might turn out to be side effects inside expr. */
6520 TREE_SIDE_EFFECTS (t) = 1;
6521 return convert_from_reference (t);
6524 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6525 /*valid_p=*/NULL, complain);
6526 if (r != error_mark_node)
6527 maybe_warn_about_useless_cast (type, expr, complain);
6528 return r;
6531 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6532 return an appropriate expression. Otherwise, return
6533 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6534 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6535 performing a C-style cast, its value upon return will indicate
6536 whether or not the conversion succeeded. */
6538 static tree
6539 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6540 bool *valid_p)
6542 tree src_type;
6543 tree reference_type;
6545 /* Callers are responsible for handling error_mark_node as a
6546 destination type. */
6547 gcc_assert (dst_type != error_mark_node);
6548 /* In a template, callers should be building syntactic
6549 representations of casts, not using this machinery. */
6550 gcc_assert (!processing_template_decl);
6552 /* Assume the conversion is invalid. */
6553 if (valid_p)
6554 *valid_p = false;
6556 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6558 if (complain & tf_error)
6559 error ("invalid use of const_cast with type %qT, "
6560 "which is not a pointer, "
6561 "reference, nor a pointer-to-data-member type", dst_type);
6562 return error_mark_node;
6565 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6567 if (complain & tf_error)
6568 error ("invalid use of const_cast with type %qT, which is a pointer "
6569 "or reference to a function type", dst_type);
6570 return error_mark_node;
6573 /* Save casted types in the function's used types hash table. */
6574 used_types_insert (dst_type);
6576 src_type = TREE_TYPE (expr);
6577 /* Expressions do not really have reference types. */
6578 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6579 src_type = TREE_TYPE (src_type);
6581 /* [expr.const.cast]
6583 For two object types T1 and T2, if a pointer to T1 can be explicitly
6584 converted to the type "pointer to T2" using a const_cast, then the
6585 following conversions can also be made:
6587 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6588 type T2 using the cast const_cast<T2&>;
6590 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6591 type T2 using the cast const_cast<T2&&>; and
6593 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6594 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6596 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6598 reference_type = dst_type;
6599 if (!TYPE_REF_IS_RVALUE (dst_type)
6600 ? real_lvalue_p (expr)
6601 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6602 ? lvalue_p (expr)
6603 : lvalue_or_rvalue_with_address_p (expr)))
6604 /* OK. */;
6605 else
6607 if (complain & tf_error)
6608 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6609 src_type, dst_type);
6610 return error_mark_node;
6612 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6613 src_type = build_pointer_type (src_type);
6615 else
6617 reference_type = NULL_TREE;
6618 /* If the destination type is not a reference type, the
6619 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6620 conversions are performed. */
6621 src_type = type_decays_to (src_type);
6622 if (src_type == error_mark_node)
6623 return error_mark_node;
6626 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
6628 if (comp_ptr_ttypes_const (dst_type, src_type))
6630 if (valid_p)
6632 *valid_p = true;
6633 /* This cast is actually a C-style cast. Issue a warning if
6634 the user is making a potentially unsafe cast. */
6635 check_for_casting_away_constness (src_type, dst_type,
6636 CAST_EXPR, complain);
6638 if (reference_type)
6640 expr = cp_build_addr_expr (expr, complain);
6641 if (expr == error_mark_node)
6642 return error_mark_node;
6643 expr = build_nop (reference_type, expr);
6644 return convert_from_reference (expr);
6646 else
6648 expr = decay_conversion (expr, complain);
6649 if (expr == error_mark_node)
6650 return error_mark_node;
6652 /* build_c_cast puts on a NOP_EXPR to make the result not an
6653 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6654 non-lvalue context. */
6655 if (TREE_CODE (expr) == NOP_EXPR
6656 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6657 expr = TREE_OPERAND (expr, 0);
6658 return build_nop (dst_type, expr);
6661 else if (valid_p
6662 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6663 TREE_TYPE (src_type)))
6664 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6665 complain);
6668 if (complain & tf_error)
6669 error ("invalid const_cast from type %qT to type %qT",
6670 src_type, dst_type);
6671 return error_mark_node;
6674 tree
6675 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6677 tree r;
6679 if (type == error_mark_node || error_operand_p (expr))
6680 return error_mark_node;
6682 if (processing_template_decl)
6684 tree t = build_min (CONST_CAST_EXPR, type, expr);
6686 if (!TREE_SIDE_EFFECTS (t)
6687 && type_dependent_expression_p (expr))
6688 /* There might turn out to be side effects inside expr. */
6689 TREE_SIDE_EFFECTS (t) = 1;
6690 return convert_from_reference (t);
6693 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
6694 if (r != error_mark_node)
6695 maybe_warn_about_useless_cast (type, expr, complain);
6696 return r;
6699 /* Like cp_build_c_cast, but for the c-common bits. */
6701 tree
6702 build_c_cast (location_t /*loc*/, tree type, tree expr)
6704 return cp_build_c_cast (type, expr, tf_warning_or_error);
6707 /* Build an expression representing an explicit C-style cast to type
6708 TYPE of expression EXPR. */
6710 tree
6711 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6713 tree value = expr;
6714 tree result;
6715 bool valid_p;
6717 if (type == error_mark_node || error_operand_p (expr))
6718 return error_mark_node;
6720 if (processing_template_decl)
6722 tree t = build_min (CAST_EXPR, type,
6723 tree_cons (NULL_TREE, value, NULL_TREE));
6724 /* We don't know if it will or will not have side effects. */
6725 TREE_SIDE_EFFECTS (t) = 1;
6726 return convert_from_reference (t);
6729 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6730 'Class') should always be retained, because this information aids
6731 in method lookup. */
6732 if (objc_is_object_ptr (type)
6733 && objc_is_object_ptr (TREE_TYPE (expr)))
6734 return build_nop (type, expr);
6736 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6737 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6738 if (TREE_CODE (type) != REFERENCE_TYPE
6739 && TREE_CODE (value) == NOP_EXPR
6740 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6741 value = TREE_OPERAND (value, 0);
6743 if (TREE_CODE (type) == ARRAY_TYPE)
6745 /* Allow casting from T1* to T2[] because Cfront allows it.
6746 NIHCL uses it. It is not valid ISO C++ however. */
6747 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6749 if (complain & tf_error)
6750 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6751 else
6752 return error_mark_node;
6753 type = build_pointer_type (TREE_TYPE (type));
6755 else
6757 if (complain & tf_error)
6758 error ("ISO C++ forbids casting to an array type %qT", type);
6759 return error_mark_node;
6763 if (TREE_CODE (type) == FUNCTION_TYPE
6764 || TREE_CODE (type) == METHOD_TYPE)
6766 if (complain & tf_error)
6767 error ("invalid cast to function type %qT", type);
6768 return error_mark_node;
6771 if (TREE_CODE (type) == POINTER_TYPE
6772 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
6773 /* Casting to an integer of smaller size is an error detected elsewhere. */
6774 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
6775 /* Don't warn about converting any constant. */
6776 && !TREE_CONSTANT (value))
6777 warning_at (input_location, OPT_Wint_to_pointer_cast,
6778 "cast to pointer from integer of different size");
6780 /* A C-style cast can be a const_cast. */
6781 result = build_const_cast_1 (type, value, complain & tf_warning,
6782 &valid_p);
6783 if (valid_p)
6785 if (result != error_mark_node)
6786 maybe_warn_about_useless_cast (type, value, complain);
6787 return result;
6790 /* Or a static cast. */
6791 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
6792 &valid_p, complain);
6793 /* Or a reinterpret_cast. */
6794 if (!valid_p)
6795 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
6796 &valid_p, complain);
6797 /* The static_cast or reinterpret_cast may be followed by a
6798 const_cast. */
6799 if (valid_p
6800 /* A valid cast may result in errors if, for example, a
6801 conversion to an ambiguous base class is required. */
6802 && !error_operand_p (result))
6804 tree result_type;
6806 maybe_warn_about_useless_cast (type, value, complain);
6808 /* Non-class rvalues always have cv-unqualified type. */
6809 if (!CLASS_TYPE_P (type))
6810 type = TYPE_MAIN_VARIANT (type);
6811 result_type = TREE_TYPE (result);
6812 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
6813 result_type = TYPE_MAIN_VARIANT (result_type);
6814 /* If the type of RESULT does not match TYPE, perform a
6815 const_cast to make it match. If the static_cast or
6816 reinterpret_cast succeeded, we will differ by at most
6817 cv-qualification, so the follow-on const_cast is guaranteed
6818 to succeed. */
6819 if (!same_type_p (non_reference (type), non_reference (result_type)))
6821 result = build_const_cast_1 (type, result, false, &valid_p);
6822 gcc_assert (valid_p);
6824 return result;
6827 return error_mark_node;
6830 /* For use from the C common bits. */
6831 tree
6832 build_modify_expr (location_t /*location*/,
6833 tree lhs, tree /*lhs_origtype*/,
6834 enum tree_code modifycode,
6835 location_t /*rhs_location*/, tree rhs,
6836 tree /*rhs_origtype*/)
6838 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
6841 /* Build an assignment expression of lvalue LHS from value RHS.
6842 MODIFYCODE is the code for a binary operator that we use
6843 to combine the old value of LHS with RHS to get the new value.
6844 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6846 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
6848 tree
6849 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6850 tsubst_flags_t complain)
6852 tree result;
6853 tree newrhs = rhs;
6854 tree lhstype = TREE_TYPE (lhs);
6855 tree olhstype = lhstype;
6856 bool plain_assign = (modifycode == NOP_EXPR);
6858 /* Avoid duplicate error messages from operands that had errors. */
6859 if (error_operand_p (lhs) || error_operand_p (rhs))
6860 return error_mark_node;
6862 /* Handle control structure constructs used as "lvalues". */
6863 switch (TREE_CODE (lhs))
6865 /* Handle --foo = 5; as these are valid constructs in C++. */
6866 case PREDECREMENT_EXPR:
6867 case PREINCREMENT_EXPR:
6868 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6869 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6870 stabilize_reference (TREE_OPERAND (lhs, 0)),
6871 TREE_OPERAND (lhs, 1));
6872 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
6873 modifycode, rhs, complain);
6874 if (newrhs == error_mark_node)
6875 return error_mark_node;
6876 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6878 /* Handle (a, b) used as an "lvalue". */
6879 case COMPOUND_EXPR:
6880 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6881 modifycode, rhs, complain);
6882 if (newrhs == error_mark_node)
6883 return error_mark_node;
6884 return build2 (COMPOUND_EXPR, lhstype,
6885 TREE_OPERAND (lhs, 0), newrhs);
6887 case MODIFY_EXPR:
6888 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6889 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6890 stabilize_reference (TREE_OPERAND (lhs, 0)),
6891 TREE_OPERAND (lhs, 1));
6892 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
6893 complain);
6894 if (newrhs == error_mark_node)
6895 return error_mark_node;
6896 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6898 case MIN_EXPR:
6899 case MAX_EXPR:
6900 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
6901 when neither operand has side-effects. */
6902 if (!lvalue_or_else (lhs, lv_assign, complain))
6903 return error_mark_node;
6905 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
6906 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
6908 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
6909 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
6910 boolean_type_node,
6911 TREE_OPERAND (lhs, 0),
6912 TREE_OPERAND (lhs, 1)),
6913 TREE_OPERAND (lhs, 0),
6914 TREE_OPERAND (lhs, 1));
6915 /* Fall through. */
6917 /* Handle (a ? b : c) used as an "lvalue". */
6918 case COND_EXPR:
6920 /* Produce (a ? (b = rhs) : (c = rhs))
6921 except that the RHS goes through a save-expr
6922 so the code to compute it is only emitted once. */
6923 tree cond;
6924 tree preeval = NULL_TREE;
6926 if (VOID_TYPE_P (TREE_TYPE (rhs)))
6928 if (complain & tf_error)
6929 error ("void value not ignored as it ought to be");
6930 return error_mark_node;
6933 rhs = stabilize_expr (rhs, &preeval);
6935 /* Check this here to avoid odd errors when trying to convert
6936 a throw to the type of the COND_EXPR. */
6937 if (!lvalue_or_else (lhs, lv_assign, complain))
6938 return error_mark_node;
6940 cond = build_conditional_expr
6941 (TREE_OPERAND (lhs, 0),
6942 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6943 modifycode, rhs, complain),
6944 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
6945 modifycode, rhs, complain),
6946 complain);
6948 if (cond == error_mark_node)
6949 return cond;
6950 /* Make sure the code to compute the rhs comes out
6951 before the split. */
6952 if (preeval)
6953 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
6954 return cond;
6957 default:
6958 break;
6961 if (modifycode == INIT_EXPR)
6963 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6964 /* Do the default thing. */;
6965 else if (TREE_CODE (rhs) == CONSTRUCTOR)
6967 /* Compound literal. */
6968 if (! same_type_p (TREE_TYPE (rhs), lhstype))
6969 /* Call convert to generate an error; see PR 11063. */
6970 rhs = convert (lhstype, rhs);
6971 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
6972 TREE_SIDE_EFFECTS (result) = 1;
6973 return result;
6975 else if (! MAYBE_CLASS_TYPE_P (lhstype))
6976 /* Do the default thing. */;
6977 else
6979 VEC(tree,gc) *rhs_vec = make_tree_vector_single (rhs);
6980 result = build_special_member_call (lhs, complete_ctor_identifier,
6981 &rhs_vec, lhstype, LOOKUP_NORMAL,
6982 complain);
6983 release_tree_vector (rhs_vec);
6984 if (result == NULL_TREE)
6985 return error_mark_node;
6986 return result;
6989 else
6991 lhs = require_complete_type_sfinae (lhs, complain);
6992 if (lhs == error_mark_node)
6993 return error_mark_node;
6995 if (modifycode == NOP_EXPR)
6997 if (c_dialect_objc ())
6999 result = objc_maybe_build_modify_expr (lhs, rhs);
7000 if (result)
7001 return result;
7004 /* `operator=' is not an inheritable operator. */
7005 if (! MAYBE_CLASS_TYPE_P (lhstype))
7006 /* Do the default thing. */;
7007 else
7009 result = build_new_op (input_location, MODIFY_EXPR,
7010 LOOKUP_NORMAL, lhs, rhs,
7011 make_node (NOP_EXPR), /*overload=*/NULL,
7012 complain);
7013 if (result == NULL_TREE)
7014 return error_mark_node;
7015 return result;
7017 lhstype = olhstype;
7019 else
7021 tree init = NULL_TREE;
7023 /* A binary op has been requested. Combine the old LHS
7024 value with the RHS producing the value we should actually
7025 store into the LHS. */
7026 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7027 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7028 || MAYBE_CLASS_TYPE_P (lhstype)));
7030 /* Preevaluate the RHS to make sure its evaluation is complete
7031 before the lvalue-to-rvalue conversion of the LHS:
7033 [expr.ass] With respect to an indeterminately-sequenced
7034 function call, the operation of a compound assignment is a
7035 single evaluation. [ Note: Therefore, a function call shall
7036 not intervene between the lvalue-to-rvalue conversion and the
7037 side effect associated with any single compound assignment
7038 operator. -- end note ] */
7039 lhs = stabilize_reference (lhs);
7040 if (TREE_SIDE_EFFECTS (rhs))
7041 rhs = mark_rvalue_use (rhs);
7042 rhs = stabilize_expr (rhs, &init);
7043 newrhs = cp_build_binary_op (input_location,
7044 modifycode, lhs, rhs,
7045 complain);
7046 if (newrhs == error_mark_node)
7048 if (complain & tf_error)
7049 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7050 TREE_TYPE (lhs), TREE_TYPE (rhs));
7051 return error_mark_node;
7054 if (init)
7055 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7057 /* Now it looks like a plain assignment. */
7058 modifycode = NOP_EXPR;
7059 if (c_dialect_objc ())
7061 result = objc_maybe_build_modify_expr (lhs, newrhs);
7062 if (result)
7063 return result;
7066 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7067 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7070 /* The left-hand side must be an lvalue. */
7071 if (!lvalue_or_else (lhs, lv_assign, complain))
7072 return error_mark_node;
7074 /* Warn about modifying something that is `const'. Don't warn if
7075 this is initialization. */
7076 if (modifycode != INIT_EXPR
7077 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7078 /* Functions are not modifiable, even though they are
7079 lvalues. */
7080 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7081 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7082 /* If it's an aggregate and any field is const, then it is
7083 effectively const. */
7084 || (CLASS_TYPE_P (lhstype)
7085 && C_TYPE_FIELDS_READONLY (lhstype))))
7087 if (complain & tf_error)
7088 cxx_readonly_error (lhs, lv_assign);
7089 else
7090 return error_mark_node;
7093 /* If storing into a structure or union member, it may have been given a
7094 lowered bitfield type. We need to convert to the declared type first,
7095 so retrieve it now. */
7097 olhstype = unlowered_expr_type (lhs);
7099 /* Convert new value to destination type. */
7101 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7103 int from_array;
7105 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7107 if (modifycode != INIT_EXPR)
7109 if (complain & tf_error)
7110 error ("assigning to an array from an initializer list");
7111 return error_mark_node;
7113 if (check_array_initializer (lhs, lhstype, newrhs))
7114 return error_mark_node;
7115 newrhs = digest_init (lhstype, newrhs, complain);
7116 if (newrhs == error_mark_node)
7117 return error_mark_node;
7120 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7121 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7123 if (complain & tf_error)
7124 error ("incompatible types in assignment of %qT to %qT",
7125 TREE_TYPE (rhs), lhstype);
7126 return error_mark_node;
7129 /* Allow array assignment in compiler-generated code. */
7130 else if (!current_function_decl
7131 || !DECL_DEFAULTED_FN (current_function_decl))
7133 /* This routine is used for both initialization and assignment.
7134 Make sure the diagnostic message differentiates the context. */
7135 if (complain & tf_error)
7137 if (modifycode == INIT_EXPR)
7138 error ("array used as initializer");
7139 else
7140 error ("invalid array assignment");
7142 return error_mark_node;
7145 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7146 ? 1 + (modifycode != INIT_EXPR): 0;
7147 return build_vec_init (lhs, NULL_TREE, newrhs,
7148 /*explicit_value_init_p=*/false,
7149 from_array, complain);
7152 if (modifycode == INIT_EXPR)
7153 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7154 LOOKUP_ONLYCONVERTING. */
7155 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7156 ICR_INIT, NULL_TREE, 0,
7157 complain);
7158 else
7159 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7160 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7162 if (!same_type_p (lhstype, olhstype))
7163 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7165 if (modifycode != INIT_EXPR)
7167 if (TREE_CODE (newrhs) == CALL_EXPR
7168 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7169 newrhs = build_cplus_new (lhstype, newrhs, complain);
7171 /* Can't initialize directly from a TARGET_EXPR, since that would
7172 cause the lhs to be constructed twice, and possibly result in
7173 accidental self-initialization. So we force the TARGET_EXPR to be
7174 expanded without a target. */
7175 if (TREE_CODE (newrhs) == TARGET_EXPR)
7176 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7177 TREE_OPERAND (newrhs, 0));
7180 if (newrhs == error_mark_node)
7181 return error_mark_node;
7183 if (c_dialect_objc () && flag_objc_gc)
7185 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7187 if (result)
7188 return result;
7191 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7192 lhstype, lhs, newrhs);
7194 TREE_SIDE_EFFECTS (result) = 1;
7195 if (!plain_assign)
7196 TREE_NO_WARNING (result) = 1;
7198 return result;
7201 tree
7202 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7203 tree rhs, tsubst_flags_t complain)
7205 if (processing_template_decl)
7206 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7207 build_min_nt_loc (loc, modifycode, NULL_TREE,
7208 NULL_TREE), rhs);
7210 if (modifycode != NOP_EXPR)
7212 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7213 make_node (modifycode), /*overload=*/NULL,
7214 complain);
7215 if (rval)
7217 TREE_NO_WARNING (rval) = 1;
7218 return rval;
7221 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7224 /* Helper function for get_delta_difference which assumes FROM is a base
7225 class of TO. Returns a delta for the conversion of pointer-to-member
7226 of FROM to pointer-to-member of TO. If the conversion is invalid and
7227 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7228 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7229 If C_CAST_P is true, this conversion is taking place as part of a
7230 C-style cast. */
7232 static tree
7233 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7234 tsubst_flags_t complain)
7236 tree binfo;
7237 base_kind kind;
7239 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7240 &kind, complain);
7242 if (binfo == error_mark_node)
7244 if (!(complain & tf_error))
7245 return error_mark_node;
7247 error (" in pointer to member function conversion");
7248 return size_zero_node;
7250 else if (binfo)
7252 if (kind != bk_via_virtual)
7253 return BINFO_OFFSET (binfo);
7254 else
7255 /* FROM is a virtual base class of TO. Issue an error or warning
7256 depending on whether or not this is a reinterpret cast. */
7258 if (!(complain & tf_error))
7259 return error_mark_node;
7261 error ("pointer to member conversion via virtual base %qT",
7262 BINFO_TYPE (binfo_from_vbase (binfo)));
7264 return size_zero_node;
7267 else
7268 return NULL_TREE;
7271 /* Get difference in deltas for different pointer to member function
7272 types. If the conversion is invalid and tf_error is not set in
7273 COMPLAIN, returns error_mark_node, otherwise returns an integer
7274 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7275 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7276 conversions as well. If C_CAST_P is true this conversion is taking
7277 place as part of a C-style cast.
7279 Note that the naming of FROM and TO is kind of backwards; the return
7280 value is what we add to a TO in order to get a FROM. They are named
7281 this way because we call this function to find out how to convert from
7282 a pointer to member of FROM to a pointer to member of TO. */
7284 static tree
7285 get_delta_difference (tree from, tree to,
7286 bool allow_inverse_p,
7287 bool c_cast_p, tsubst_flags_t complain)
7289 tree result;
7291 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7292 /* Pointer to member of incomplete class is permitted*/
7293 result = size_zero_node;
7294 else
7295 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7297 if (result == error_mark_node)
7298 return error_mark_node;
7300 if (!result)
7302 if (!allow_inverse_p)
7304 if (!(complain & tf_error))
7305 return error_mark_node;
7307 error_not_base_type (from, to);
7308 error (" in pointer to member conversion");
7309 result = size_zero_node;
7311 else
7313 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7315 if (result == error_mark_node)
7316 return error_mark_node;
7318 if (result)
7319 result = size_diffop_loc (input_location,
7320 size_zero_node, result);
7321 else
7323 if (!(complain & tf_error))
7324 return error_mark_node;
7326 error_not_base_type (from, to);
7327 error (" in pointer to member conversion");
7328 result = size_zero_node;
7333 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7334 result));
7337 /* Return a constructor for the pointer-to-member-function TYPE using
7338 the other components as specified. */
7340 tree
7341 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7343 tree u = NULL_TREE;
7344 tree delta_field;
7345 tree pfn_field;
7346 VEC(constructor_elt, gc) *v;
7348 /* Pull the FIELD_DECLs out of the type. */
7349 pfn_field = TYPE_FIELDS (type);
7350 delta_field = DECL_CHAIN (pfn_field);
7352 /* Make sure DELTA has the type we want. */
7353 delta = convert_and_check (delta_type_node, delta);
7355 /* Convert to the correct target type if necessary. */
7356 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7358 /* Finish creating the initializer. */
7359 v = VEC_alloc(constructor_elt, gc, 2);
7360 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7361 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7362 u = build_constructor (type, v);
7363 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7364 TREE_STATIC (u) = (TREE_CONSTANT (u)
7365 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7366 != NULL_TREE)
7367 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7368 != NULL_TREE));
7369 return u;
7372 /* Build a constructor for a pointer to member function. It can be
7373 used to initialize global variables, local variable, or used
7374 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7375 want to be.
7377 If FORCE is nonzero, then force this conversion, even if
7378 we would rather not do it. Usually set when using an explicit
7379 cast. A C-style cast is being processed iff C_CAST_P is true.
7381 Return error_mark_node, if something goes wrong. */
7383 tree
7384 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7385 tsubst_flags_t complain)
7387 tree fn;
7388 tree pfn_type;
7389 tree to_type;
7391 if (error_operand_p (pfn))
7392 return error_mark_node;
7394 pfn_type = TREE_TYPE (pfn);
7395 to_type = build_ptrmemfunc_type (type);
7397 /* Handle multiple conversions of pointer to member functions. */
7398 if (TYPE_PTRMEMFUNC_P (pfn_type))
7400 tree delta = NULL_TREE;
7401 tree npfn = NULL_TREE;
7402 tree n;
7404 if (!force
7405 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7406 LOOKUP_NORMAL, complain))
7407 error ("invalid conversion to type %qT from type %qT",
7408 to_type, pfn_type);
7410 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7411 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7412 force,
7413 c_cast_p, complain);
7414 if (n == error_mark_node)
7415 return error_mark_node;
7417 /* We don't have to do any conversion to convert a
7418 pointer-to-member to its own type. But, we don't want to
7419 just return a PTRMEM_CST if there's an explicit cast; that
7420 cast should make the expression an invalid template argument. */
7421 if (TREE_CODE (pfn) != PTRMEM_CST)
7423 if (same_type_p (to_type, pfn_type))
7424 return pfn;
7425 else if (integer_zerop (n))
7426 return build_reinterpret_cast (to_type, pfn,
7427 complain);
7430 if (TREE_SIDE_EFFECTS (pfn))
7431 pfn = save_expr (pfn);
7433 /* Obtain the function pointer and the current DELTA. */
7434 if (TREE_CODE (pfn) == PTRMEM_CST)
7435 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7436 else
7438 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7439 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7442 /* Just adjust the DELTA field. */
7443 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7444 (TREE_TYPE (delta), ptrdiff_type_node));
7445 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7446 n = cp_build_binary_op (input_location,
7447 LSHIFT_EXPR, n, integer_one_node,
7448 complain);
7449 delta = cp_build_binary_op (input_location,
7450 PLUS_EXPR, delta, n, complain);
7451 return build_ptrmemfunc1 (to_type, delta, npfn);
7454 /* Handle null pointer to member function conversions. */
7455 if (null_ptr_cst_p (pfn))
7457 pfn = build_c_cast (input_location, type, nullptr_node);
7458 return build_ptrmemfunc1 (to_type,
7459 integer_zero_node,
7460 pfn);
7463 if (type_unknown_p (pfn))
7464 return instantiate_type (type, pfn, complain);
7466 fn = TREE_OPERAND (pfn, 0);
7467 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7468 /* In a template, we will have preserved the
7469 OFFSET_REF. */
7470 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7471 return make_ptrmem_cst (to_type, fn);
7474 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7475 given by CST.
7477 ??? There is no consistency as to the types returned for the above
7478 values. Some code acts as if it were a sizetype and some as if it were
7479 integer_type_node. */
7481 void
7482 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7484 tree type = TREE_TYPE (cst);
7485 tree fn = PTRMEM_CST_MEMBER (cst);
7486 tree ptr_class, fn_class;
7488 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7490 /* The class that the function belongs to. */
7491 fn_class = DECL_CONTEXT (fn);
7493 /* The class that we're creating a pointer to member of. */
7494 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7496 /* First, calculate the adjustment to the function's class. */
7497 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7498 /*c_cast_p=*/0, tf_warning_or_error);
7500 if (!DECL_VIRTUAL_P (fn))
7501 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7502 build_addr_func (fn, tf_warning_or_error));
7503 else
7505 /* If we're dealing with a virtual function, we have to adjust 'this'
7506 again, to point to the base which provides the vtable entry for
7507 fn; the call will do the opposite adjustment. */
7508 tree orig_class = DECL_CONTEXT (fn);
7509 tree binfo = binfo_or_else (orig_class, fn_class);
7510 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7511 *delta, BINFO_OFFSET (binfo));
7512 *delta = fold_if_not_in_template (*delta);
7514 /* We set PFN to the vtable offset at which the function can be
7515 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7516 case delta is shifted left, and then incremented). */
7517 *pfn = DECL_VINDEX (fn);
7518 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7519 TYPE_SIZE_UNIT (vtable_entry_type));
7520 *pfn = fold_if_not_in_template (*pfn);
7522 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7524 case ptrmemfunc_vbit_in_pfn:
7525 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7526 integer_one_node);
7527 *pfn = fold_if_not_in_template (*pfn);
7528 break;
7530 case ptrmemfunc_vbit_in_delta:
7531 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7532 *delta, integer_one_node);
7533 *delta = fold_if_not_in_template (*delta);
7534 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7535 *delta, integer_one_node);
7536 *delta = fold_if_not_in_template (*delta);
7537 break;
7539 default:
7540 gcc_unreachable ();
7543 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7544 *pfn = fold_if_not_in_template (*pfn);
7548 /* Return an expression for PFN from the pointer-to-member function
7549 given by T. */
7551 static tree
7552 pfn_from_ptrmemfunc (tree t)
7554 if (TREE_CODE (t) == PTRMEM_CST)
7556 tree delta;
7557 tree pfn;
7559 expand_ptrmemfunc_cst (t, &delta, &pfn);
7560 if (pfn)
7561 return pfn;
7564 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7567 /* Return an expression for DELTA from the pointer-to-member function
7568 given by T. */
7570 static tree
7571 delta_from_ptrmemfunc (tree t)
7573 if (TREE_CODE (t) == PTRMEM_CST)
7575 tree delta;
7576 tree pfn;
7578 expand_ptrmemfunc_cst (t, &delta, &pfn);
7579 if (delta)
7580 return delta;
7583 return build_ptrmemfunc_access_expr (t, delta_identifier);
7586 /* Convert value RHS to type TYPE as preparation for an assignment to
7587 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7588 implicit conversion is. If FNDECL is non-NULL, we are doing the
7589 conversion in order to pass the PARMNUMth argument of FNDECL.
7590 If FNDECL is NULL, we are doing the conversion in function pointer
7591 argument passing, conversion in initialization, etc. */
7593 static tree
7594 convert_for_assignment (tree type, tree rhs,
7595 impl_conv_rhs errtype, tree fndecl, int parmnum,
7596 tsubst_flags_t complain, int flags)
7598 tree rhstype;
7599 enum tree_code coder;
7601 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7602 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7603 rhs = TREE_OPERAND (rhs, 0);
7605 rhstype = TREE_TYPE (rhs);
7606 coder = TREE_CODE (rhstype);
7608 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7609 && vector_types_convertible_p (type, rhstype, true))
7611 rhs = mark_rvalue_use (rhs);
7612 return convert (type, rhs);
7615 if (rhs == error_mark_node || rhstype == error_mark_node)
7616 return error_mark_node;
7617 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7618 return error_mark_node;
7620 /* The RHS of an assignment cannot have void type. */
7621 if (coder == VOID_TYPE)
7623 if (complain & tf_error)
7624 error ("void value not ignored as it ought to be");
7625 return error_mark_node;
7628 if (c_dialect_objc ())
7630 int parmno;
7631 tree selector;
7632 tree rname = fndecl;
7634 switch (errtype)
7636 case ICR_ASSIGN:
7637 parmno = -1;
7638 break;
7639 case ICR_INIT:
7640 parmno = -2;
7641 break;
7642 default:
7643 selector = objc_message_selector ();
7644 parmno = parmnum;
7645 if (selector && parmno > 1)
7647 rname = selector;
7648 parmno -= 1;
7652 if (objc_compare_types (type, rhstype, parmno, rname))
7654 rhs = mark_rvalue_use (rhs);
7655 return convert (type, rhs);
7659 /* [expr.ass]
7661 The expression is implicitly converted (clause _conv_) to the
7662 cv-unqualified type of the left operand.
7664 We allow bad conversions here because by the time we get to this point
7665 we are committed to doing the conversion. If we end up doing a bad
7666 conversion, convert_like will complain. */
7667 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
7669 /* When -Wno-pmf-conversions is use, we just silently allow
7670 conversions from pointers-to-members to plain pointers. If
7671 the conversion doesn't work, cp_convert will complain. */
7672 if (!warn_pmf2ptr
7673 && TYPE_PTR_P (type)
7674 && TYPE_PTRMEMFUNC_P (rhstype))
7675 rhs = cp_convert (strip_top_quals (type), rhs, complain);
7676 else
7678 if (complain & tf_error)
7680 /* If the right-hand side has unknown type, then it is an
7681 overloaded function. Call instantiate_type to get error
7682 messages. */
7683 if (rhstype == unknown_type_node)
7684 instantiate_type (type, rhs, tf_warning_or_error);
7685 else if (fndecl)
7686 error ("cannot convert %qT to %qT for argument %qP to %qD",
7687 rhstype, type, parmnum, fndecl);
7688 else
7689 switch (errtype)
7691 case ICR_DEFAULT_ARGUMENT:
7692 error ("cannot convert %qT to %qT in default argument",
7693 rhstype, type);
7694 break;
7695 case ICR_ARGPASS:
7696 error ("cannot convert %qT to %qT in argument passing",
7697 rhstype, type);
7698 break;
7699 case ICR_CONVERTING:
7700 error ("cannot convert %qT to %qT",
7701 rhstype, type);
7702 break;
7703 case ICR_INIT:
7704 error ("cannot convert %qT to %qT in initialization",
7705 rhstype, type);
7706 break;
7707 case ICR_RETURN:
7708 error ("cannot convert %qT to %qT in return",
7709 rhstype, type);
7710 break;
7711 case ICR_ASSIGN:
7712 error ("cannot convert %qT to %qT in assignment",
7713 rhstype, type);
7714 break;
7715 default:
7716 gcc_unreachable();
7719 return error_mark_node;
7722 if (warn_suggest_attribute_format)
7724 const enum tree_code codel = TREE_CODE (type);
7725 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7726 && coder == codel
7727 && check_missing_format_attribute (type, rhstype)
7728 && (complain & tf_warning))
7729 switch (errtype)
7731 case ICR_ARGPASS:
7732 case ICR_DEFAULT_ARGUMENT:
7733 if (fndecl)
7734 warning (OPT_Wsuggest_attribute_format,
7735 "parameter %qP of %qD might be a candidate "
7736 "for a format attribute", parmnum, fndecl);
7737 else
7738 warning (OPT_Wsuggest_attribute_format,
7739 "parameter might be a candidate "
7740 "for a format attribute");
7741 break;
7742 case ICR_CONVERTING:
7743 warning (OPT_Wsuggest_attribute_format,
7744 "target of conversion might be a candidate "
7745 "for a format attribute");
7746 break;
7747 case ICR_INIT:
7748 warning (OPT_Wsuggest_attribute_format,
7749 "target of initialization might be a candidate "
7750 "for a format attribute");
7751 break;
7752 case ICR_RETURN:
7753 warning (OPT_Wsuggest_attribute_format,
7754 "return type might be a candidate "
7755 "for a format attribute");
7756 break;
7757 case ICR_ASSIGN:
7758 warning (OPT_Wsuggest_attribute_format,
7759 "left-hand side of assignment might be a candidate "
7760 "for a format attribute");
7761 break;
7762 default:
7763 gcc_unreachable();
7767 /* If -Wparentheses, warn about a = b = c when a has type bool and b
7768 does not. */
7769 if (warn_parentheses
7770 && TREE_CODE (type) == BOOLEAN_TYPE
7771 && TREE_CODE (rhs) == MODIFY_EXPR
7772 && !TREE_NO_WARNING (rhs)
7773 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7774 && (complain & tf_warning))
7776 location_t loc = EXPR_LOC_OR_HERE (rhs);
7778 warning_at (loc, OPT_Wparentheses,
7779 "suggest parentheses around assignment used as truth value");
7780 TREE_NO_WARNING (rhs) = 1;
7783 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
7784 complain, flags);
7787 /* Convert RHS to be of type TYPE.
7788 If EXP is nonzero, it is the target of the initialization.
7789 ERRTYPE indicates what kind of error the implicit conversion is.
7791 Two major differences between the behavior of
7792 `convert_for_assignment' and `convert_for_initialization'
7793 are that references are bashed in the former, while
7794 copied in the latter, and aggregates are assigned in
7795 the former (operator=) while initialized in the
7796 latter (X(X&)).
7798 If using constructor make sure no conversion operator exists, if one does
7799 exist, an ambiguity exists. */
7801 tree
7802 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
7803 impl_conv_rhs errtype, tree fndecl, int parmnum,
7804 tsubst_flags_t complain)
7806 enum tree_code codel = TREE_CODE (type);
7807 tree rhstype;
7808 enum tree_code coder;
7810 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7811 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7812 if (TREE_CODE (rhs) == NOP_EXPR
7813 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7814 && codel != REFERENCE_TYPE)
7815 rhs = TREE_OPERAND (rhs, 0);
7817 if (type == error_mark_node
7818 || rhs == error_mark_node
7819 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7820 return error_mark_node;
7822 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7823 && TREE_CODE (type) != ARRAY_TYPE
7824 && (TREE_CODE (type) != REFERENCE_TYPE
7825 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7826 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7827 && (TREE_CODE (type) != REFERENCE_TYPE
7828 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7829 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7830 rhs = decay_conversion (rhs, complain);
7832 rhstype = TREE_TYPE (rhs);
7833 coder = TREE_CODE (rhstype);
7835 if (coder == ERROR_MARK)
7836 return error_mark_node;
7838 /* We accept references to incomplete types, so we can
7839 return here before checking if RHS is of complete type. */
7841 if (codel == REFERENCE_TYPE)
7843 /* This should eventually happen in convert_arguments. */
7844 int savew = 0, savee = 0;
7846 if (fndecl)
7847 savew = warningcount, savee = errorcount;
7848 rhs = initialize_reference (type, rhs, flags, complain);
7849 if (fndecl)
7851 if (warningcount > savew)
7852 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
7853 else if (errorcount > savee)
7854 error ("in passing argument %P of %q+D", parmnum, fndecl);
7856 return rhs;
7859 if (exp != 0)
7860 exp = require_complete_type_sfinae (exp, complain);
7861 if (exp == error_mark_node)
7862 return error_mark_node;
7864 rhstype = non_reference (rhstype);
7866 type = complete_type (type);
7868 if (DIRECT_INIT_EXPR_P (type, rhs))
7869 /* Don't try to do copy-initialization if we already have
7870 direct-initialization. */
7871 return rhs;
7873 if (MAYBE_CLASS_TYPE_P (type))
7874 return perform_implicit_conversion_flags (type, rhs, complain, flags);
7876 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
7877 complain, flags);
7880 /* If RETVAL is the address of, or a reference to, a local variable or
7881 temporary give an appropriate warning. */
7883 static void
7884 maybe_warn_about_returning_address_of_local (tree retval)
7886 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
7887 tree whats_returned = retval;
7889 for (;;)
7891 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7892 whats_returned = TREE_OPERAND (whats_returned, 1);
7893 else if (CONVERT_EXPR_P (whats_returned)
7894 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
7895 whats_returned = TREE_OPERAND (whats_returned, 0);
7896 else
7897 break;
7900 if (TREE_CODE (whats_returned) != ADDR_EXPR)
7901 return;
7902 whats_returned = TREE_OPERAND (whats_returned, 0);
7904 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7906 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7907 || TREE_CODE (whats_returned) == TARGET_EXPR)
7909 warning (0, "returning reference to temporary");
7910 return;
7912 if (TREE_CODE (whats_returned) == VAR_DECL
7913 && DECL_NAME (whats_returned)
7914 && TEMP_NAME_P (DECL_NAME (whats_returned)))
7916 warning (0, "reference to non-lvalue returned");
7917 return;
7921 while (TREE_CODE (whats_returned) == COMPONENT_REF
7922 || TREE_CODE (whats_returned) == ARRAY_REF)
7923 whats_returned = TREE_OPERAND (whats_returned, 0);
7925 if (DECL_P (whats_returned)
7926 && DECL_NAME (whats_returned)
7927 && DECL_FUNCTION_SCOPE_P (whats_returned)
7928 && !(TREE_STATIC (whats_returned)
7929 || TREE_PUBLIC (whats_returned)))
7931 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7932 warning (0, "reference to local variable %q+D returned",
7933 whats_returned);
7934 else
7935 warning (0, "address of local variable %q+D returned",
7936 whats_returned);
7937 return;
7941 /* Check that returning RETVAL from the current function is valid.
7942 Return an expression explicitly showing all conversions required to
7943 change RETVAL into the function return type, and to assign it to
7944 the DECL_RESULT for the function. Set *NO_WARNING to true if
7945 code reaches end of non-void function warning shouldn't be issued
7946 on this RETURN_EXPR. */
7948 tree
7949 check_return_expr (tree retval, bool *no_warning)
7951 tree result;
7952 /* The type actually returned by the function. */
7953 tree valtype;
7954 /* The type the function is declared to return, or void if
7955 the declared type is incomplete. */
7956 tree functype;
7957 int fn_returns_value_p;
7958 bool named_return_value_okay_p;
7960 *no_warning = false;
7962 /* A `volatile' function is one that isn't supposed to return, ever.
7963 (This is a G++ extension, used to get better code for functions
7964 that call the `volatile' function.) */
7965 if (TREE_THIS_VOLATILE (current_function_decl))
7966 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7968 /* Check for various simple errors. */
7969 if (DECL_DESTRUCTOR_P (current_function_decl))
7971 if (retval)
7972 error ("returning a value from a destructor");
7973 return NULL_TREE;
7975 else if (DECL_CONSTRUCTOR_P (current_function_decl))
7977 if (in_function_try_handler)
7978 /* If a return statement appears in a handler of the
7979 function-try-block of a constructor, the program is ill-formed. */
7980 error ("cannot return from a handler of a function-try-block of a constructor");
7981 else if (retval)
7982 /* You can't return a value from a constructor. */
7983 error ("returning a value from a constructor");
7984 return NULL_TREE;
7987 if (processing_template_decl)
7989 current_function_returns_value = 1;
7990 if (check_for_bare_parameter_packs (retval))
7991 retval = error_mark_node;
7992 return retval;
7995 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
7997 /* Deduce auto return type from a return statement. */
7998 if (current_function_auto_return_pattern)
8000 tree auto_node;
8001 tree type;
8003 if (!retval && !is_auto (current_function_auto_return_pattern))
8005 /* Give a helpful error message. */
8006 error ("return-statement with no value, in function returning %qT",
8007 current_function_auto_return_pattern);
8008 inform (input_location, "only plain %<auto%> return type can be "
8009 "deduced to %<void%>");
8010 type = error_mark_node;
8012 else
8014 if (!retval)
8015 retval = void_zero_node;
8016 auto_node = type_uses_auto (current_function_auto_return_pattern);
8017 type = do_auto_deduction (current_function_auto_return_pattern,
8018 retval, auto_node);
8021 if (type == error_mark_node)
8022 /* Leave it. */;
8023 else if (functype == current_function_auto_return_pattern)
8024 apply_deduced_return_type (current_function_decl, type);
8025 else
8026 /* A mismatch should have been diagnosed in do_auto_deduction. */
8027 gcc_assert (same_type_p (type, functype));
8028 functype = type;
8031 /* When no explicit return-value is given in a function with a named
8032 return value, the named return value is used. */
8033 result = DECL_RESULT (current_function_decl);
8034 valtype = TREE_TYPE (result);
8035 gcc_assert (valtype != NULL_TREE);
8036 fn_returns_value_p = !VOID_TYPE_P (valtype);
8037 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8038 retval = result;
8040 /* Check for a return statement with no return value in a function
8041 that's supposed to return a value. */
8042 if (!retval && fn_returns_value_p)
8044 if (functype != error_mark_node)
8045 permerror (input_location, "return-statement with no value, in "
8046 "function returning %qT", valtype);
8047 /* Remember that this function did return. */
8048 current_function_returns_value = 1;
8049 /* And signal caller that TREE_NO_WARNING should be set on the
8050 RETURN_EXPR to avoid control reaches end of non-void function
8051 warnings in tree-cfg.c. */
8052 *no_warning = true;
8054 /* Check for a return statement with a value in a function that
8055 isn't supposed to return a value. */
8056 else if (retval && !fn_returns_value_p)
8058 if (VOID_TYPE_P (TREE_TYPE (retval)))
8059 /* You can return a `void' value from a function of `void'
8060 type. In that case, we have to evaluate the expression for
8061 its side-effects. */
8062 finish_expr_stmt (retval);
8063 else
8064 permerror (input_location, "return-statement with a value, in function "
8065 "returning 'void'");
8066 current_function_returns_null = 1;
8068 /* There's really no value to return, after all. */
8069 return NULL_TREE;
8071 else if (!retval)
8072 /* Remember that this function can sometimes return without a
8073 value. */
8074 current_function_returns_null = 1;
8075 else
8076 /* Remember that this function did return a value. */
8077 current_function_returns_value = 1;
8079 /* Check for erroneous operands -- but after giving ourselves a
8080 chance to provide an error about returning a value from a void
8081 function. */
8082 if (error_operand_p (retval))
8084 current_function_return_value = error_mark_node;
8085 return error_mark_node;
8088 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8089 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8090 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8091 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8092 && ! flag_check_new
8093 && retval && null_ptr_cst_p (retval))
8094 warning (0, "%<operator new%> must not return NULL unless it is "
8095 "declared %<throw()%> (or -fcheck-new is in effect)");
8097 /* Effective C++ rule 15. See also start_function. */
8098 if (warn_ecpp
8099 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8101 bool warn = true;
8103 /* The function return type must be a reference to the current
8104 class. */
8105 if (TREE_CODE (valtype) == REFERENCE_TYPE
8106 && same_type_ignoring_top_level_qualifiers_p
8107 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8109 /* Returning '*this' is obviously OK. */
8110 if (retval == current_class_ref)
8111 warn = false;
8112 /* If we are calling a function whose return type is the same of
8113 the current class reference, it is ok. */
8114 else if (TREE_CODE (retval) == INDIRECT_REF
8115 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8116 warn = false;
8119 if (warn)
8120 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8123 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8125 [...] For a function with a class return type, if the expression
8126 in the return statement is the name of a local object, and the cv-
8127 unqualified type of the local object is the same as the function
8128 return type, an implementation is permitted to omit creating the tem-
8129 porary object to hold the function return value [...]
8131 So, if this is a value-returning function that always returns the same
8132 local variable, remember it.
8134 It might be nice to be more flexible, and choose the first suitable
8135 variable even if the function sometimes returns something else, but
8136 then we run the risk of clobbering the variable we chose if the other
8137 returned expression uses the chosen variable somehow. And people expect
8138 this restriction, anyway. (jason 2000-11-19)
8140 See finish_function and finalize_nrv for the rest of this optimization. */
8142 named_return_value_okay_p =
8143 (retval != NULL_TREE
8144 /* Must be a local, automatic variable. */
8145 && TREE_CODE (retval) == VAR_DECL
8146 && DECL_CONTEXT (retval) == current_function_decl
8147 && ! TREE_STATIC (retval)
8148 && ! DECL_ANON_UNION_VAR_P (retval)
8149 && (DECL_ALIGN (retval) >= DECL_ALIGN (result))
8150 /* The cv-unqualified type of the returned value must be the
8151 same as the cv-unqualified return type of the
8152 function. */
8153 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8154 (TYPE_MAIN_VARIANT (functype)))
8155 /* And the returned value must be non-volatile. */
8156 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8158 if (fn_returns_value_p && flag_elide_constructors)
8160 if (named_return_value_okay_p
8161 && (current_function_return_value == NULL_TREE
8162 || current_function_return_value == retval))
8163 current_function_return_value = retval;
8164 else
8165 current_function_return_value = error_mark_node;
8168 /* We don't need to do any conversions when there's nothing being
8169 returned. */
8170 if (!retval)
8171 return NULL_TREE;
8173 /* Do any required conversions. */
8174 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8175 /* No conversions are required. */
8177 else
8179 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8181 /* The functype's return type will have been set to void, if it
8182 was an incomplete type. Just treat this as 'return;' */
8183 if (VOID_TYPE_P (functype))
8184 return error_mark_node;
8186 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8187 treated as an rvalue for the purposes of overload resolution to
8188 favor move constructors over copy constructors.
8190 Note that these conditions are similar to, but not as strict as,
8191 the conditions for the named return value optimization. */
8192 if ((cxx_dialect != cxx98)
8193 && (TREE_CODE (retval) == VAR_DECL
8194 || TREE_CODE (retval) == PARM_DECL)
8195 && DECL_CONTEXT (retval) == current_function_decl
8196 && !TREE_STATIC (retval)
8197 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8198 (TYPE_MAIN_VARIANT (functype)))
8199 /* This is only interesting for class type. */
8200 && CLASS_TYPE_P (functype))
8201 flags = flags | LOOKUP_PREFER_RVALUE;
8203 /* First convert the value to the function's return type, then
8204 to the type of return value's location to handle the
8205 case that functype is smaller than the valtype. */
8206 retval = convert_for_initialization
8207 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8208 tf_warning_or_error);
8209 retval = convert (valtype, retval);
8211 /* If the conversion failed, treat this just like `return;'. */
8212 if (retval == error_mark_node)
8213 return retval;
8214 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8215 else if (! cfun->returns_struct
8216 && TREE_CODE (retval) == TARGET_EXPR
8217 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8218 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8219 TREE_OPERAND (retval, 0));
8220 else
8221 maybe_warn_about_returning_address_of_local (retval);
8224 /* Actually copy the value returned into the appropriate location. */
8225 if (retval && retval != result)
8226 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8228 return retval;
8232 /* Returns nonzero if the pointer-type FROM can be converted to the
8233 pointer-type TO via a qualification conversion. If CONSTP is -1,
8234 then we return nonzero if the pointers are similar, and the
8235 cv-qualification signature of FROM is a proper subset of that of TO.
8237 If CONSTP is positive, then all outer pointers have been
8238 const-qualified. */
8240 static int
8241 comp_ptr_ttypes_real (tree to, tree from, int constp)
8243 bool to_more_cv_qualified = false;
8244 bool is_opaque_pointer = false;
8246 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8248 if (TREE_CODE (to) != TREE_CODE (from))
8249 return 0;
8251 if (TREE_CODE (from) == OFFSET_TYPE
8252 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8253 TYPE_OFFSET_BASETYPE (to)))
8254 return 0;
8256 /* Const and volatile mean something different for function types,
8257 so the usual checks are not appropriate. */
8258 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8260 if (!at_least_as_qualified_p (to, from))
8261 return 0;
8263 if (!at_least_as_qualified_p (from, to))
8265 if (constp == 0)
8266 return 0;
8267 to_more_cv_qualified = true;
8270 if (constp > 0)
8271 constp &= TYPE_READONLY (to);
8274 if (TREE_CODE (to) == VECTOR_TYPE)
8275 is_opaque_pointer = vector_targets_convertible_p (to, from);
8277 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRDATAMEM_P (to))
8278 return ((constp >= 0 || to_more_cv_qualified)
8279 && (is_opaque_pointer
8280 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8284 /* When comparing, say, char ** to char const **, this function takes
8285 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8286 types to this function. */
8289 comp_ptr_ttypes (tree to, tree from)
8291 return comp_ptr_ttypes_real (to, from, 1);
8294 /* Returns true iff FNTYPE is a non-class type that involves
8295 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8296 if a parameter type is ill-formed. */
8298 bool
8299 error_type_p (const_tree type)
8301 tree t;
8303 switch (TREE_CODE (type))
8305 case ERROR_MARK:
8306 return true;
8308 case POINTER_TYPE:
8309 case REFERENCE_TYPE:
8310 case OFFSET_TYPE:
8311 return error_type_p (TREE_TYPE (type));
8313 case FUNCTION_TYPE:
8314 case METHOD_TYPE:
8315 if (error_type_p (TREE_TYPE (type)))
8316 return true;
8317 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8318 if (error_type_p (TREE_VALUE (t)))
8319 return true;
8320 return false;
8322 case RECORD_TYPE:
8323 if (TYPE_PTRMEMFUNC_P (type))
8324 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8325 return false;
8327 default:
8328 return false;
8332 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8333 type or inheritance-related types, regardless of cv-quals. */
8336 ptr_reasonably_similar (const_tree to, const_tree from)
8338 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8340 /* Any target type is similar enough to void. */
8341 if (TREE_CODE (to) == VOID_TYPE)
8342 return !error_type_p (from);
8343 if (TREE_CODE (from) == VOID_TYPE)
8344 return !error_type_p (to);
8346 if (TREE_CODE (to) != TREE_CODE (from))
8347 return 0;
8349 if (TREE_CODE (from) == OFFSET_TYPE
8350 && comptypes (TYPE_OFFSET_BASETYPE (to),
8351 TYPE_OFFSET_BASETYPE (from),
8352 COMPARE_BASE | COMPARE_DERIVED))
8353 continue;
8355 if (TREE_CODE (to) == VECTOR_TYPE
8356 && vector_types_convertible_p (to, from, false))
8357 return 1;
8359 if (TREE_CODE (to) == INTEGER_TYPE
8360 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8361 return 1;
8363 if (TREE_CODE (to) == FUNCTION_TYPE)
8364 return !error_type_p (to) && !error_type_p (from);
8366 if (TREE_CODE (to) != POINTER_TYPE)
8367 return comptypes
8368 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8369 COMPARE_BASE | COMPARE_DERIVED);
8373 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8374 pointer-to-member types) are the same, ignoring cv-qualification at
8375 all levels. */
8377 bool
8378 comp_ptr_ttypes_const (tree to, tree from)
8380 bool is_opaque_pointer = false;
8382 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8384 if (TREE_CODE (to) != TREE_CODE (from))
8385 return false;
8387 if (TREE_CODE (from) == OFFSET_TYPE
8388 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8389 TYPE_OFFSET_BASETYPE (to)))
8390 continue;
8392 if (TREE_CODE (to) == VECTOR_TYPE)
8393 is_opaque_pointer = vector_targets_convertible_p (to, from);
8395 if (TREE_CODE (to) != POINTER_TYPE)
8396 return (is_opaque_pointer
8397 || same_type_ignoring_top_level_qualifiers_p (to, from));
8401 /* Returns the type qualifiers for this type, including the qualifiers on the
8402 elements for an array type. */
8405 cp_type_quals (const_tree type)
8407 int quals;
8408 /* This CONST_CAST is okay because strip_array_types returns its
8409 argument unmodified and we assign it to a const_tree. */
8410 type = strip_array_types (CONST_CAST_TREE (type));
8411 if (type == error_mark_node
8412 /* Quals on a FUNCTION_TYPE are memfn quals. */
8413 || TREE_CODE (type) == FUNCTION_TYPE)
8414 return TYPE_UNQUALIFIED;
8415 quals = TYPE_QUALS (type);
8416 /* METHOD and REFERENCE_TYPEs should never have quals. */
8417 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8418 && TREE_CODE (type) != REFERENCE_TYPE)
8419 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8420 == TYPE_UNQUALIFIED));
8421 return quals;
8424 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8425 METHOD_TYPE. */
8428 type_memfn_quals (const_tree type)
8430 if (TREE_CODE (type) == FUNCTION_TYPE)
8431 return TYPE_QUALS (type);
8432 else if (TREE_CODE (type) == METHOD_TYPE)
8433 return cp_type_quals (class_of_this_parm (type));
8434 else
8435 gcc_unreachable ();
8438 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8439 MEMFN_QUALS. */
8441 tree
8442 apply_memfn_quals (tree type, cp_cv_quals memfn_quals)
8444 /* Could handle METHOD_TYPE here if necessary. */
8445 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8446 if (TYPE_QUALS (type) == memfn_quals)
8447 return type;
8448 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8449 complex. */
8450 return build_qualified_type (type, memfn_quals);
8453 /* Returns nonzero if TYPE is const or volatile. */
8455 bool
8456 cv_qualified_p (const_tree type)
8458 int quals = cp_type_quals (type);
8459 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8462 /* Returns nonzero if the TYPE contains a mutable member. */
8464 bool
8465 cp_has_mutable_p (const_tree type)
8467 /* This CONST_CAST is okay because strip_array_types returns its
8468 argument unmodified and we assign it to a const_tree. */
8469 type = strip_array_types (CONST_CAST_TREE(type));
8471 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8474 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8475 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8476 approximation. In particular, consider:
8478 int f();
8479 struct S { int i; };
8480 const S s = { f(); }
8482 Here, we will make "s" as TREE_READONLY (because it is declared
8483 "const") -- only to reverse ourselves upon seeing that the
8484 initializer is non-constant. */
8486 void
8487 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8489 tree type = TREE_TYPE (decl);
8491 if (type == error_mark_node)
8492 return;
8494 if (TREE_CODE (decl) == TYPE_DECL)
8495 return;
8497 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8498 && type_quals != TYPE_UNQUALIFIED));
8500 /* Avoid setting TREE_READONLY incorrectly. */
8501 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8502 constructor can produce constant init, so rely on cp_finish_decl to
8503 clear TREE_READONLY if the variable has non-constant init. */
8505 /* If the type has (or might have) a mutable component, that component
8506 might be modified. */
8507 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8508 type_quals &= ~TYPE_QUAL_CONST;
8510 c_apply_type_quals_to_decl (type_quals, decl);
8513 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8514 exemplar types such that casting T1 to T2 is casting away constness
8515 if and only if there is no implicit conversion from T1 to T2. */
8517 static void
8518 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8520 int quals1;
8521 int quals2;
8523 /* [expr.const.cast]
8525 For multi-level pointer to members and multi-level mixed pointers
8526 and pointers to members (conv.qual), the "member" aspect of a
8527 pointer to member level is ignored when determining if a const
8528 cv-qualifier has been cast away. */
8529 /* [expr.const.cast]
8531 For two pointer types:
8533 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8534 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8535 K is min(N,M)
8537 casting from X1 to X2 casts away constness if, for a non-pointer
8538 type T there does not exist an implicit conversion (clause
8539 _conv_) from:
8541 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8545 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8546 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8547 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8549 *t1 = cp_build_qualified_type (void_type_node,
8550 cp_type_quals (*t1));
8551 *t2 = cp_build_qualified_type (void_type_node,
8552 cp_type_quals (*t2));
8553 return;
8556 quals1 = cp_type_quals (*t1);
8557 quals2 = cp_type_quals (*t2);
8559 if (TYPE_PTRDATAMEM_P (*t1))
8560 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8561 else
8562 *t1 = TREE_TYPE (*t1);
8563 if (TYPE_PTRDATAMEM_P (*t2))
8564 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8565 else
8566 *t2 = TREE_TYPE (*t2);
8568 casts_away_constness_r (t1, t2, complain);
8569 *t1 = build_pointer_type (*t1);
8570 *t2 = build_pointer_type (*t2);
8571 *t1 = cp_build_qualified_type (*t1, quals1);
8572 *t2 = cp_build_qualified_type (*t2, quals2);
8575 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8576 constness.
8578 ??? This function returns non-zero if casting away qualifiers not
8579 just const. We would like to return to the caller exactly which
8580 qualifiers are casted away to give more accurate diagnostics.
8583 static bool
8584 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8586 if (TREE_CODE (t2) == REFERENCE_TYPE)
8588 /* [expr.const.cast]
8590 Casting from an lvalue of type T1 to an lvalue of type T2
8591 using a reference cast casts away constness if a cast from an
8592 rvalue of type "pointer to T1" to the type "pointer to T2"
8593 casts away constness. */
8594 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8595 return casts_away_constness (build_pointer_type (t1),
8596 build_pointer_type (TREE_TYPE (t2)),
8597 complain);
8600 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
8601 /* [expr.const.cast]
8603 Casting from an rvalue of type "pointer to data member of X
8604 of type T1" to the type "pointer to data member of Y of type
8605 T2" casts away constness if a cast from an rvalue of type
8606 "pointer to T1" to the type "pointer to T2" casts away
8607 constness. */
8608 return casts_away_constness
8609 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8610 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
8611 complain);
8613 /* Casting away constness is only something that makes sense for
8614 pointer or reference types. */
8615 if (TREE_CODE (t1) != POINTER_TYPE
8616 || TREE_CODE (t2) != POINTER_TYPE)
8617 return false;
8619 /* Top-level qualifiers don't matter. */
8620 t1 = TYPE_MAIN_VARIANT (t1);
8621 t2 = TYPE_MAIN_VARIANT (t2);
8622 casts_away_constness_r (&t1, &t2, complain);
8623 if (!can_convert (t2, t1, complain))
8624 return true;
8626 return false;
8629 /* If T is a REFERENCE_TYPE return the type to which T refers.
8630 Otherwise, return T itself. */
8632 tree
8633 non_reference (tree t)
8635 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8636 t = TREE_TYPE (t);
8637 return t;
8641 /* Return nonzero if REF is an lvalue valid for this language;
8642 otherwise, print an error message and return zero. USE says
8643 how the lvalue is being used and so selects the error message. */
8646 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8648 cp_lvalue_kind kind = lvalue_kind (ref);
8650 if (kind == clk_none)
8652 if (complain & tf_error)
8653 lvalue_error (input_location, use);
8654 return 0;
8656 else if (kind & (clk_rvalueref|clk_class))
8658 if (!(complain & tf_error))
8659 return 0;
8660 if (kind & clk_class)
8661 /* Make this a permerror because we used to accept it. */
8662 permerror (input_location, "using temporary as lvalue");
8663 else
8664 error ("using xvalue (rvalue reference) as lvalue");
8666 return 1;
8669 /* Return true if a user-defined literal operator is a raw operator. */
8671 bool
8672 check_raw_literal_operator (const_tree decl)
8674 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8675 tree argtype;
8676 int arity;
8677 bool maybe_raw_p = false;
8679 /* Count the number and type of arguments and check for ellipsis. */
8680 for (argtype = argtypes, arity = 0;
8681 argtype && argtype != void_list_node;
8682 ++arity, argtype = TREE_CHAIN (argtype))
8684 tree t = TREE_VALUE (argtype);
8686 if (same_type_p (t, const_string_type_node))
8687 maybe_raw_p = true;
8689 if (!argtype)
8690 return false; /* Found ellipsis. */
8692 if (!maybe_raw_p || arity != 1)
8693 return false;
8695 return true;
8699 /* Return true if a user-defined literal operator has one of the allowed
8700 argument types. */
8702 bool
8703 check_literal_operator_args (const_tree decl,
8704 bool *long_long_unsigned_p, bool *long_double_p)
8706 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8708 *long_long_unsigned_p = false;
8709 *long_double_p = false;
8710 if (processing_template_decl || processing_specialization)
8711 return argtypes == void_list_node;
8712 else
8714 tree argtype;
8715 int arity;
8716 int max_arity = 2;
8718 /* Count the number and type of arguments and check for ellipsis. */
8719 for (argtype = argtypes, arity = 0;
8720 argtype && argtype != void_list_node;
8721 argtype = TREE_CHAIN (argtype))
8723 tree t = TREE_VALUE (argtype);
8724 ++arity;
8726 if (TREE_CODE (t) == POINTER_TYPE)
8728 bool maybe_raw_p = false;
8729 t = TREE_TYPE (t);
8730 if (cp_type_quals (t) != TYPE_QUAL_CONST)
8731 return false;
8732 t = TYPE_MAIN_VARIANT (t);
8733 if ((maybe_raw_p = same_type_p (t, char_type_node))
8734 || same_type_p (t, wchar_type_node)
8735 || same_type_p (t, char16_type_node)
8736 || same_type_p (t, char32_type_node))
8738 argtype = TREE_CHAIN (argtype);
8739 if (!argtype)
8740 return false;
8741 t = TREE_VALUE (argtype);
8742 if (maybe_raw_p && argtype == void_list_node)
8743 return true;
8744 else if (same_type_p (t, size_type_node))
8746 ++arity;
8747 continue;
8749 else
8750 return false;
8753 else if (same_type_p (t, long_long_unsigned_type_node))
8755 max_arity = 1;
8756 *long_long_unsigned_p = true;
8758 else if (same_type_p (t, long_double_type_node))
8760 max_arity = 1;
8761 *long_double_p = true;
8763 else if (same_type_p (t, char_type_node))
8764 max_arity = 1;
8765 else if (same_type_p (t, wchar_type_node))
8766 max_arity = 1;
8767 else if (same_type_p (t, char16_type_node))
8768 max_arity = 1;
8769 else if (same_type_p (t, char32_type_node))
8770 max_arity = 1;
8771 else
8772 return false;
8774 if (!argtype)
8775 return false; /* Found ellipsis. */
8777 if (arity != max_arity)
8778 return false;
8780 return true;