* config/darwin.c (darwin_assemble_visibility): Treat
[official-gcc.git] / gcc / cp / typeck.c
blob884f7d0573b69d60430c857eae7a289ff299cf1f
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, delta0, e1, e2;
4164 if (TREE_SIDE_EFFECTS (op0))
4165 op0 = save_expr (op0);
4167 pfn0 = pfn_from_ptrmemfunc (op0);
4168 delta0 = delta_from_ptrmemfunc (op0);
4169 e1 = cp_build_binary_op (location,
4170 EQ_EXPR,
4171 pfn0,
4172 build_zero_cst (TREE_TYPE (pfn0)),
4173 complain);
4174 e2 = cp_build_binary_op (location,
4175 BIT_AND_EXPR,
4176 delta0,
4177 integer_one_node,
4178 complain);
4180 if ((complain & tf_warning)
4181 && c_inhibit_evaluation_warnings == 0
4182 && !NULLPTR_TYPE_P (TREE_TYPE (op1)))
4183 warning (OPT_Wzero_as_null_pointer_constant,
4184 "zero as null pointer constant");
4186 e2 = cp_build_binary_op (location,
4187 EQ_EXPR, e2, integer_zero_node,
4188 complain);
4189 op0 = cp_build_binary_op (location,
4190 TRUTH_ANDIF_EXPR, e1, e2,
4191 complain);
4192 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4194 else
4196 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4197 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4199 result_type = TREE_TYPE (op0);
4201 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4202 return cp_build_binary_op (location, code, op1, op0, complain);
4203 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4205 tree type;
4206 /* E will be the final comparison. */
4207 tree e;
4208 /* E1 and E2 are for scratch. */
4209 tree e1;
4210 tree e2;
4211 tree pfn0;
4212 tree pfn1;
4213 tree delta0;
4214 tree delta1;
4216 type = composite_pointer_type (type0, type1, op0, op1,
4217 CPO_COMPARISON, complain);
4219 if (!same_type_p (TREE_TYPE (op0), type))
4220 op0 = cp_convert_and_check (type, op0, complain);
4221 if (!same_type_p (TREE_TYPE (op1), type))
4222 op1 = cp_convert_and_check (type, op1, complain);
4224 if (op0 == error_mark_node || op1 == error_mark_node)
4225 return error_mark_node;
4227 if (TREE_SIDE_EFFECTS (op0))
4228 op0 = save_expr (op0);
4229 if (TREE_SIDE_EFFECTS (op1))
4230 op1 = save_expr (op1);
4232 pfn0 = pfn_from_ptrmemfunc (op0);
4233 pfn1 = pfn_from_ptrmemfunc (op1);
4234 delta0 = delta_from_ptrmemfunc (op0);
4235 delta1 = delta_from_ptrmemfunc (op1);
4236 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4237 == ptrmemfunc_vbit_in_delta)
4239 /* We generate:
4241 (op0.pfn == op1.pfn
4242 && ((op0.delta == op1.delta)
4243 || (!op0.pfn && op0.delta & 1 == 0
4244 && op1.delta & 1 == 0))
4246 The reason for the `!op0.pfn' bit is that a NULL
4247 pointer-to-member is any member with a zero PFN and
4248 LSB of the DELTA field is 0. */
4250 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4251 delta0,
4252 integer_one_node,
4253 complain);
4254 e1 = cp_build_binary_op (location,
4255 EQ_EXPR, e1, integer_zero_node,
4256 complain);
4257 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4258 delta1,
4259 integer_one_node,
4260 complain);
4261 e2 = cp_build_binary_op (location,
4262 EQ_EXPR, e2, integer_zero_node,
4263 complain);
4264 e1 = cp_build_binary_op (location,
4265 TRUTH_ANDIF_EXPR, e2, e1,
4266 complain);
4267 e2 = cp_build_binary_op (location, EQ_EXPR,
4268 pfn0,
4269 build_zero_cst (TREE_TYPE (pfn0)),
4270 complain);
4271 e2 = cp_build_binary_op (location,
4272 TRUTH_ANDIF_EXPR, e2, e1, complain);
4273 e1 = cp_build_binary_op (location,
4274 EQ_EXPR, delta0, delta1, complain);
4275 e1 = cp_build_binary_op (location,
4276 TRUTH_ORIF_EXPR, e1, e2, complain);
4278 else
4280 /* We generate:
4282 (op0.pfn == op1.pfn
4283 && (!op0.pfn || op0.delta == op1.delta))
4285 The reason for the `!op0.pfn' bit is that a NULL
4286 pointer-to-member is any member with a zero PFN; the
4287 DELTA field is unspecified. */
4289 e1 = cp_build_binary_op (location,
4290 EQ_EXPR, delta0, delta1, complain);
4291 e2 = cp_build_binary_op (location,
4292 EQ_EXPR,
4293 pfn0,
4294 build_zero_cst (TREE_TYPE (pfn0)),
4295 complain);
4296 e1 = cp_build_binary_op (location,
4297 TRUTH_ORIF_EXPR, e1, e2, complain);
4299 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4300 e = cp_build_binary_op (location,
4301 TRUTH_ANDIF_EXPR, e2, e1, complain);
4302 if (code == EQ_EXPR)
4303 return e;
4304 return cp_build_binary_op (location,
4305 EQ_EXPR, e, integer_zero_node, complain);
4307 else
4309 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4310 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4311 type1));
4312 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4313 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4314 type0));
4317 break;
4319 case MAX_EXPR:
4320 case MIN_EXPR:
4321 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4322 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4323 shorten = 1;
4324 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4325 result_type = composite_pointer_type (type0, type1, op0, op1,
4326 CPO_COMPARISON, complain);
4327 break;
4329 case LE_EXPR:
4330 case GE_EXPR:
4331 case LT_EXPR:
4332 case GT_EXPR:
4333 if (TREE_CODE (orig_op0) == STRING_CST
4334 || TREE_CODE (orig_op1) == STRING_CST)
4336 if (complain & tf_warning)
4337 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4340 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4342 vector_compare:
4343 tree intt;
4344 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4345 TREE_TYPE (type1)))
4347 error_at (location, "comparing vectors with different "
4348 "element types");
4349 inform (location, "operand types are %qT and %qT", type0, type1);
4350 return error_mark_node;
4353 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4355 error_at (location, "comparing vectors with different "
4356 "number of elements");
4357 inform (location, "operand types are %qT and %qT", type0, type1);
4358 return error_mark_node;
4361 /* Always construct signed integer vector type. */
4362 intt = c_common_type_for_size (GET_MODE_BITSIZE
4363 (TYPE_MODE (TREE_TYPE (type0))), 0);
4364 result_type = build_opaque_vector_type (intt,
4365 TYPE_VECTOR_SUBPARTS (type0));
4366 converted = 1;
4367 break;
4369 build_type = boolean_type_node;
4370 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4371 || code0 == ENUMERAL_TYPE)
4372 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4373 || code1 == ENUMERAL_TYPE))
4374 short_compare = 1;
4375 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4376 result_type = composite_pointer_type (type0, type1, op0, op1,
4377 CPO_COMPARISON, complain);
4378 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4380 result_type = type0;
4381 if (extra_warnings && (complain & tf_warning))
4382 warning (OPT_Wextra,
4383 "ordered comparison of pointer with integer zero");
4385 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4387 result_type = type1;
4388 if (extra_warnings && (complain & tf_warning))
4389 warning (OPT_Wextra,
4390 "ordered comparison of pointer with integer zero");
4392 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4393 /* One of the operands must be of nullptr_t type. */
4394 result_type = TREE_TYPE (nullptr_node);
4395 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4397 result_type = type0;
4398 if (complain & tf_error)
4399 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4400 else
4401 return error_mark_node;
4403 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4405 result_type = type1;
4406 if (complain & tf_error)
4407 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4408 else
4409 return error_mark_node;
4411 break;
4413 case UNORDERED_EXPR:
4414 case ORDERED_EXPR:
4415 case UNLT_EXPR:
4416 case UNLE_EXPR:
4417 case UNGT_EXPR:
4418 case UNGE_EXPR:
4419 case UNEQ_EXPR:
4420 build_type = integer_type_node;
4421 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4423 if (complain & tf_error)
4424 error ("unordered comparison on non-floating point argument");
4425 return error_mark_node;
4427 common = 1;
4428 break;
4430 default:
4431 break;
4434 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4435 || code0 == ENUMERAL_TYPE)
4436 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4437 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4438 arithmetic_types_p = 1;
4439 else
4441 arithmetic_types_p = 0;
4442 /* Vector arithmetic is only allowed when both sides are vectors. */
4443 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4445 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4446 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4447 TREE_TYPE (type1)))
4449 binary_op_error (location, code, type0, type1);
4450 return error_mark_node;
4452 arithmetic_types_p = 1;
4455 /* Determine the RESULT_TYPE, if it is not already known. */
4456 if (!result_type
4457 && arithmetic_types_p
4458 && (shorten || common || short_compare))
4460 result_type = cp_common_type (type0, type1);
4461 do_warn_double_promotion (result_type, type0, type1,
4462 "implicit conversion from %qT to %qT "
4463 "to match other operand of binary "
4464 "expression",
4465 location);
4468 if (!result_type)
4470 if (complain & tf_error)
4471 error ("invalid operands of types %qT and %qT to binary %qO",
4472 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4473 return error_mark_node;
4476 /* If we're in a template, the only thing we need to know is the
4477 RESULT_TYPE. */
4478 if (processing_template_decl)
4480 /* Since the middle-end checks the type when doing a build2, we
4481 need to build the tree in pieces. This built tree will never
4482 get out of the front-end as we replace it when instantiating
4483 the template. */
4484 tree tmp = build2 (resultcode,
4485 build_type ? build_type : result_type,
4486 NULL_TREE, op1);
4487 TREE_OPERAND (tmp, 0) = op0;
4488 return tmp;
4491 if (arithmetic_types_p)
4493 bool first_complex = (code0 == COMPLEX_TYPE);
4494 bool second_complex = (code1 == COMPLEX_TYPE);
4495 int none_complex = (!first_complex && !second_complex);
4497 /* Adapted from patch for c/24581. */
4498 if (first_complex != second_complex
4499 && (code == PLUS_EXPR
4500 || code == MINUS_EXPR
4501 || code == MULT_EXPR
4502 || (code == TRUNC_DIV_EXPR && first_complex))
4503 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4504 && flag_signed_zeros)
4506 /* An operation on mixed real/complex operands must be
4507 handled specially, but the language-independent code can
4508 more easily optimize the plain complex arithmetic if
4509 -fno-signed-zeros. */
4510 tree real_type = TREE_TYPE (result_type);
4511 tree real, imag;
4512 if (first_complex)
4514 if (TREE_TYPE (op0) != result_type)
4515 op0 = cp_convert_and_check (result_type, op0, complain);
4516 if (TREE_TYPE (op1) != real_type)
4517 op1 = cp_convert_and_check (real_type, op1, complain);
4519 else
4521 if (TREE_TYPE (op0) != real_type)
4522 op0 = cp_convert_and_check (real_type, op0, complain);
4523 if (TREE_TYPE (op1) != result_type)
4524 op1 = cp_convert_and_check (result_type, op1, complain);
4526 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4527 return error_mark_node;
4528 if (first_complex)
4530 op0 = save_expr (op0);
4531 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4532 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4533 switch (code)
4535 case MULT_EXPR:
4536 case TRUNC_DIV_EXPR:
4537 op1 = save_expr (op1);
4538 imag = build2 (resultcode, real_type, imag, op1);
4539 /* Fall through. */
4540 case PLUS_EXPR:
4541 case MINUS_EXPR:
4542 real = build2 (resultcode, real_type, real, op1);
4543 break;
4544 default:
4545 gcc_unreachable();
4548 else
4550 op1 = save_expr (op1);
4551 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4552 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4553 switch (code)
4555 case MULT_EXPR:
4556 op0 = save_expr (op0);
4557 imag = build2 (resultcode, real_type, op0, imag);
4558 /* Fall through. */
4559 case PLUS_EXPR:
4560 real = build2 (resultcode, real_type, op0, real);
4561 break;
4562 case MINUS_EXPR:
4563 real = build2 (resultcode, real_type, op0, real);
4564 imag = build1 (NEGATE_EXPR, real_type, imag);
4565 break;
4566 default:
4567 gcc_unreachable();
4570 real = fold_if_not_in_template (real);
4571 imag = fold_if_not_in_template (imag);
4572 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4573 result = fold_if_not_in_template (result);
4574 return result;
4577 /* For certain operations (which identify themselves by shorten != 0)
4578 if both args were extended from the same smaller type,
4579 do the arithmetic in that type and then extend.
4581 shorten !=0 and !=1 indicates a bitwise operation.
4582 For them, this optimization is safe only if
4583 both args are zero-extended or both are sign-extended.
4584 Otherwise, we might change the result.
4585 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4586 but calculated in (unsigned short) it would be (unsigned short)-1. */
4588 if (shorten && none_complex)
4590 final_type = result_type;
4591 result_type = shorten_binary_op (result_type, op0, op1,
4592 shorten == -1);
4595 /* Comparison operations are shortened too but differently.
4596 They identify themselves by setting short_compare = 1. */
4598 if (short_compare)
4600 /* Don't write &op0, etc., because that would prevent op0
4601 from being kept in a register.
4602 Instead, make copies of the our local variables and
4603 pass the copies by reference, then copy them back afterward. */
4604 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4605 enum tree_code xresultcode = resultcode;
4606 tree val
4607 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4608 if (val != 0)
4609 return cp_convert (boolean_type_node, val, complain);
4610 op0 = xop0, op1 = xop1;
4611 converted = 1;
4612 resultcode = xresultcode;
4615 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4616 && warn_sign_compare
4617 /* Do not warn until the template is instantiated; we cannot
4618 bound the ranges of the arguments until that point. */
4619 && !processing_template_decl
4620 && (complain & tf_warning)
4621 && c_inhibit_evaluation_warnings == 0
4622 /* Even unsigned enum types promote to signed int. We don't
4623 want to issue -Wsign-compare warnings for this case. */
4624 && !enum_cast_to_int (orig_op0)
4625 && !enum_cast_to_int (orig_op1))
4627 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
4628 result_type, resultcode);
4632 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4633 Then the expression will be built.
4634 It will be given type FINAL_TYPE if that is nonzero;
4635 otherwise, it will be given type RESULT_TYPE. */
4636 if (! converted)
4638 if (TREE_TYPE (op0) != result_type)
4639 op0 = cp_convert_and_check (result_type, op0, complain);
4640 if (TREE_TYPE (op1) != result_type)
4641 op1 = cp_convert_and_check (result_type, op1, complain);
4643 if (op0 == error_mark_node || op1 == error_mark_node)
4644 return error_mark_node;
4647 if (build_type == NULL_TREE)
4648 build_type = result_type;
4650 result = build2 (resultcode, build_type, op0, op1);
4651 result = fold_if_not_in_template (result);
4652 if (final_type != 0)
4653 result = cp_convert (final_type, result, complain);
4655 if (TREE_OVERFLOW_P (result)
4656 && !TREE_OVERFLOW_P (op0)
4657 && !TREE_OVERFLOW_P (op1))
4658 overflow_warning (location, result);
4660 return result;
4663 /* Return a tree for the sum or difference (RESULTCODE says which)
4664 of pointer PTROP and integer INTOP. */
4666 static tree
4667 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4669 tree res_type = TREE_TYPE (ptrop);
4671 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4672 in certain circumstance (when it's valid to do so). So we need
4673 to make sure it's complete. We don't need to check here, if we
4674 can actually complete it at all, as those checks will be done in
4675 pointer_int_sum() anyway. */
4676 complete_type (TREE_TYPE (res_type));
4678 return pointer_int_sum (input_location, resultcode, ptrop,
4679 fold_if_not_in_template (intop));
4682 /* Return a tree for the difference of pointers OP0 and OP1.
4683 The resulting tree has type int. */
4685 static tree
4686 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
4688 tree result;
4689 tree restype = ptrdiff_type_node;
4690 tree target_type = TREE_TYPE (ptrtype);
4692 if (!complete_type_or_else (target_type, NULL_TREE))
4693 return error_mark_node;
4695 if (TREE_CODE (target_type) == VOID_TYPE)
4697 if (complain & tf_error)
4698 permerror (input_location, "ISO C++ forbids using pointer of "
4699 "type %<void *%> in subtraction");
4700 else
4701 return error_mark_node;
4703 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4705 if (complain & tf_error)
4706 permerror (input_location, "ISO C++ forbids using pointer to "
4707 "a function in subtraction");
4708 else
4709 return error_mark_node;
4711 if (TREE_CODE (target_type) == METHOD_TYPE)
4713 if (complain & tf_error)
4714 permerror (input_location, "ISO C++ forbids using pointer to "
4715 "a method in subtraction");
4716 else
4717 return error_mark_node;
4720 /* First do the subtraction as integers;
4721 then drop through to build the divide operator. */
4723 op0 = cp_build_binary_op (input_location,
4724 MINUS_EXPR,
4725 cp_convert (restype, op0, complain),
4726 cp_convert (restype, op1, complain),
4727 complain);
4729 /* This generates an error if op1 is a pointer to an incomplete type. */
4730 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4732 if (complain & tf_error)
4733 error ("invalid use of a pointer to an incomplete type in "
4734 "pointer arithmetic");
4735 else
4736 return error_mark_node;
4739 op1 = (TYPE_PTROB_P (ptrtype)
4740 ? size_in_bytes (target_type)
4741 : integer_one_node);
4743 /* Do the division. */
4745 result = build2 (EXACT_DIV_EXPR, restype, op0,
4746 cp_convert (restype, op1, complain));
4747 return fold_if_not_in_template (result);
4750 /* Construct and perhaps optimize a tree representation
4751 for a unary operation. CODE, a tree_code, specifies the operation
4752 and XARG is the operand. */
4754 tree
4755 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
4756 tsubst_flags_t complain)
4758 tree orig_expr = xarg;
4759 tree exp;
4760 int ptrmem = 0;
4762 if (processing_template_decl)
4764 if (type_dependent_expression_p (xarg))
4765 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
4767 xarg = build_non_dependent_expr (xarg);
4770 exp = NULL_TREE;
4772 /* [expr.unary.op] says:
4774 The address of an object of incomplete type can be taken.
4776 (And is just the ordinary address operator, not an overloaded
4777 "operator &".) However, if the type is a template
4778 specialization, we must complete the type at this point so that
4779 an overloaded "operator &" will be available if required. */
4780 if (code == ADDR_EXPR
4781 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4782 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4783 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4784 || (TREE_CODE (xarg) == OFFSET_REF)))
4785 /* Don't look for a function. */;
4786 else
4787 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
4788 NULL_TREE, /*overload=*/NULL, complain);
4789 if (!exp && code == ADDR_EXPR)
4791 if (is_overloaded_fn (xarg))
4793 tree fn = get_first_fn (xarg);
4794 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4796 error (DECL_CONSTRUCTOR_P (fn)
4797 ? G_("taking address of constructor %qE")
4798 : G_("taking address of destructor %qE"),
4799 xarg);
4800 return error_mark_node;
4804 /* A pointer to member-function can be formed only by saying
4805 &X::mf. */
4806 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4807 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4809 if (TREE_CODE (xarg) != OFFSET_REF
4810 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4812 error ("invalid use of %qE to form a pointer-to-member-function",
4813 xarg);
4814 if (TREE_CODE (xarg) != OFFSET_REF)
4815 inform (input_location, " a qualified-id is required");
4816 return error_mark_node;
4818 else
4820 error ("parentheses around %qE cannot be used to form a"
4821 " pointer-to-member-function",
4822 xarg);
4823 PTRMEM_OK_P (xarg) = 1;
4827 if (TREE_CODE (xarg) == OFFSET_REF)
4829 ptrmem = PTRMEM_OK_P (xarg);
4831 if (!ptrmem && !flag_ms_extensions
4832 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4834 /* A single non-static member, make sure we don't allow a
4835 pointer-to-member. */
4836 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4837 TREE_OPERAND (xarg, 0),
4838 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4839 PTRMEM_OK_P (xarg) = ptrmem;
4843 exp = cp_build_addr_expr_strict (xarg, complain);
4846 if (processing_template_decl && exp != error_mark_node)
4847 exp = build_min_non_dep (code, exp, orig_expr,
4848 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4849 if (TREE_CODE (exp) == ADDR_EXPR)
4850 PTRMEM_OK_P (exp) = ptrmem;
4851 return exp;
4854 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4855 constants, where a null value is represented by an INTEGER_CST of
4856 -1. */
4858 tree
4859 cp_truthvalue_conversion (tree expr)
4861 tree type = TREE_TYPE (expr);
4862 if (TYPE_PTRDATAMEM_P (type))
4863 return build_binary_op (EXPR_LOCATION (expr),
4864 NE_EXPR, expr, nullptr_node, 1);
4865 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
4867 /* With -Wzero-as-null-pointer-constant do not warn for an
4868 'if (p)' or a 'while (!p)', where p is a pointer. */
4869 tree ret;
4870 ++c_inhibit_evaluation_warnings;
4871 ret = c_common_truthvalue_conversion (input_location, expr);
4872 --c_inhibit_evaluation_warnings;
4873 return ret;
4875 else
4876 return c_common_truthvalue_conversion (input_location, expr);
4879 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4881 tree
4882 condition_conversion (tree expr)
4884 tree t;
4885 if (processing_template_decl)
4886 return expr;
4887 t = perform_implicit_conversion_flags (boolean_type_node, expr,
4888 tf_warning_or_error, LOOKUP_NORMAL);
4889 t = fold_build_cleanup_point_expr (boolean_type_node, t);
4890 return t;
4893 /* Returns the address of T. This function will fold away
4894 ADDR_EXPR of INDIRECT_REF. */
4896 tree
4897 build_address (tree t)
4899 if (error_operand_p (t) || !cxx_mark_addressable (t))
4900 return error_mark_node;
4901 t = build_fold_addr_expr (t);
4902 if (TREE_CODE (t) != ADDR_EXPR)
4903 t = rvalue (t);
4904 return t;
4907 /* Returns the address of T with type TYPE. */
4909 tree
4910 build_typed_address (tree t, tree type)
4912 if (error_operand_p (t) || !cxx_mark_addressable (t))
4913 return error_mark_node;
4914 t = build_fold_addr_expr_with_type (t, type);
4915 if (TREE_CODE (t) != ADDR_EXPR)
4916 t = rvalue (t);
4917 return t;
4920 /* Return a NOP_EXPR converting EXPR to TYPE. */
4922 tree
4923 build_nop (tree type, tree expr)
4925 if (type == error_mark_node || error_operand_p (expr))
4926 return expr;
4927 return build1 (NOP_EXPR, type, expr);
4930 /* Take the address of ARG, whatever that means under C++ semantics.
4931 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
4932 and class rvalues as well.
4934 Nothing should call this function directly; instead, callers should use
4935 cp_build_addr_expr or cp_build_addr_expr_strict. */
4937 static tree
4938 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
4940 tree argtype;
4941 tree val;
4943 if (!arg || error_operand_p (arg))
4944 return error_mark_node;
4946 arg = mark_lvalue_use (arg);
4947 argtype = lvalue_type (arg);
4949 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4950 || !IDENTIFIER_OPNAME_P (arg));
4952 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4953 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4955 /* They're trying to take the address of a unique non-static
4956 member function. This is ill-formed (except in MS-land),
4957 but let's try to DTRT.
4958 Note: We only handle unique functions here because we don't
4959 want to complain if there's a static overload; non-unique
4960 cases will be handled by instantiate_type. But we need to
4961 handle this case here to allow casts on the resulting PMF.
4962 We could defer this in non-MS mode, but it's easier to give
4963 a useful error here. */
4965 /* Inside constant member functions, the `this' pointer
4966 contains an extra const qualifier. TYPE_MAIN_VARIANT
4967 is used here to remove this const from the diagnostics
4968 and the created OFFSET_REF. */
4969 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4970 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4971 mark_used (fn);
4973 if (! flag_ms_extensions)
4975 tree name = DECL_NAME (fn);
4976 if (!(complain & tf_error))
4977 return error_mark_node;
4978 else if (current_class_type
4979 && TREE_OPERAND (arg, 0) == current_class_ref)
4980 /* An expression like &memfn. */
4981 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
4982 " or parenthesized non-static member function to form"
4983 " a pointer to member function. Say %<&%T::%D%>",
4984 base, name);
4985 else
4986 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
4987 " function to form a pointer to member function."
4988 " Say %<&%T::%D%>",
4989 base, name);
4991 arg = build_offset_ref (base, fn, /*address_p=*/true);
4994 /* Uninstantiated types are all functions. Taking the
4995 address of a function is a no-op, so just return the
4996 argument. */
4997 if (type_unknown_p (arg))
4998 return build1 (ADDR_EXPR, unknown_type_node, arg);
5000 if (TREE_CODE (arg) == OFFSET_REF)
5001 /* We want a pointer to member; bypass all the code for actually taking
5002 the address of something. */
5003 goto offset_ref;
5005 /* Anything not already handled and not a true memory reference
5006 is an error. */
5007 if (TREE_CODE (argtype) != FUNCTION_TYPE
5008 && TREE_CODE (argtype) != METHOD_TYPE)
5010 cp_lvalue_kind kind = lvalue_kind (arg);
5011 if (kind == clk_none)
5013 if (complain & tf_error)
5014 lvalue_error (input_location, lv_addressof);
5015 return error_mark_node;
5017 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5019 if (!(complain & tf_error))
5020 return error_mark_node;
5021 if (kind & clk_class)
5022 /* Make this a permerror because we used to accept it. */
5023 permerror (input_location, "taking address of temporary");
5024 else
5025 error ("taking address of xvalue (rvalue reference)");
5029 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5031 tree type = build_pointer_type (TREE_TYPE (argtype));
5032 arg = build1 (CONVERT_EXPR, type, arg);
5033 return arg;
5035 else if (pedantic && DECL_MAIN_P (arg))
5037 /* ARM $3.4 */
5038 /* Apparently a lot of autoconf scripts for C++ packages do this,
5039 so only complain if -Wpedantic. */
5040 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5041 pedwarn (input_location, OPT_Wpedantic,
5042 "ISO C++ forbids taking address of function %<::main%>");
5043 else if (flag_pedantic_errors)
5044 return error_mark_node;
5047 /* Let &* cancel out to simplify resulting code. */
5048 if (TREE_CODE (arg) == INDIRECT_REF)
5050 /* We don't need to have `current_class_ptr' wrapped in a
5051 NON_LVALUE_EXPR node. */
5052 if (arg == current_class_ref)
5053 return current_class_ptr;
5055 arg = TREE_OPERAND (arg, 0);
5056 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5058 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5059 arg = build1 (CONVERT_EXPR, type, arg);
5061 else
5062 /* Don't let this be an lvalue. */
5063 arg = rvalue (arg);
5064 return arg;
5067 /* ??? Cope with user tricks that amount to offsetof. */
5068 if (TREE_CODE (argtype) != FUNCTION_TYPE
5069 && TREE_CODE (argtype) != METHOD_TYPE
5070 && argtype != unknown_type_node
5071 && (val = get_base_address (arg))
5072 && COMPLETE_TYPE_P (TREE_TYPE (val))
5073 && TREE_CODE (val) == INDIRECT_REF
5074 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5076 tree type = build_pointer_type (argtype);
5077 return fold_convert (type, fold_offsetof_1 (arg));
5080 /* Handle complex lvalues (when permitted)
5081 by reduction to simpler cases. */
5082 val = unary_complex_lvalue (ADDR_EXPR, arg);
5083 if (val != 0)
5084 return val;
5086 switch (TREE_CODE (arg))
5088 CASE_CONVERT:
5089 case FLOAT_EXPR:
5090 case FIX_TRUNC_EXPR:
5091 /* Even if we're not being pedantic, we cannot allow this
5092 extension when we're instantiating in a SFINAE
5093 context. */
5094 if (! lvalue_p (arg) && complain == tf_none)
5096 if (complain & tf_error)
5097 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5098 else
5099 return error_mark_node;
5101 break;
5103 case BASELINK:
5104 arg = BASELINK_FUNCTIONS (arg);
5105 /* Fall through. */
5107 case OVERLOAD:
5108 arg = OVL_CURRENT (arg);
5109 break;
5111 case OFFSET_REF:
5112 offset_ref:
5113 /* Turn a reference to a non-static data member into a
5114 pointer-to-member. */
5116 tree type;
5117 tree t;
5119 gcc_assert (PTRMEM_OK_P (arg));
5121 t = TREE_OPERAND (arg, 1);
5122 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5124 if (complain & tf_error)
5125 error ("cannot create pointer to reference member %qD", t);
5126 return error_mark_node;
5129 type = build_ptrmem_type (context_for_name_lookup (t),
5130 TREE_TYPE (t));
5131 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5132 return t;
5135 default:
5136 break;
5139 if (argtype != error_mark_node)
5140 argtype = build_pointer_type (argtype);
5142 /* In a template, we are processing a non-dependent expression
5143 so we can just form an ADDR_EXPR with the correct type. */
5144 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5146 val = build_address (arg);
5147 if (TREE_CODE (arg) == OFFSET_REF)
5148 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5150 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5152 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5154 /* We can only get here with a single static member
5155 function. */
5156 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5157 && DECL_STATIC_FUNCTION_P (fn));
5158 mark_used (fn);
5159 val = build_address (fn);
5160 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5161 /* Do not lose object's side effects. */
5162 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5163 TREE_OPERAND (arg, 0), val);
5165 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5167 if (complain & tf_error)
5168 error ("attempt to take address of bit-field structure member %qD",
5169 TREE_OPERAND (arg, 1));
5170 return error_mark_node;
5172 else
5174 tree object = TREE_OPERAND (arg, 0);
5175 tree field = TREE_OPERAND (arg, 1);
5176 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5177 (TREE_TYPE (object), decl_type_context (field)));
5178 val = build_address (arg);
5181 if (TREE_CODE (argtype) == POINTER_TYPE
5182 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5184 build_ptrmemfunc_type (argtype);
5185 val = build_ptrmemfunc (argtype, val, 0,
5186 /*c_cast_p=*/false,
5187 complain);
5190 return val;
5193 /* Take the address of ARG if it has one, even if it's an rvalue. */
5195 tree
5196 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5198 return cp_build_addr_expr_1 (arg, 0, complain);
5201 /* Take the address of ARG, but only if it's an lvalue. */
5203 tree
5204 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5206 return cp_build_addr_expr_1 (arg, 1, complain);
5209 /* C++: Must handle pointers to members.
5211 Perhaps type instantiation should be extended to handle conversion
5212 from aggregates to types we don't yet know we want? (Or are those
5213 cases typically errors which should be reported?)
5215 NOCONVERT nonzero suppresses the default promotions
5216 (such as from short to int). */
5218 tree
5219 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5220 tsubst_flags_t complain)
5222 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5223 tree arg = xarg;
5224 tree argtype = 0;
5225 const char *errstring = NULL;
5226 tree val;
5227 const char *invalid_op_diag;
5229 if (!arg || error_operand_p (arg))
5230 return error_mark_node;
5232 if ((invalid_op_diag
5233 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5234 ? CONVERT_EXPR
5235 : code),
5236 TREE_TYPE (xarg))))
5238 error (invalid_op_diag);
5239 return error_mark_node;
5242 switch (code)
5244 case UNARY_PLUS_EXPR:
5245 case NEGATE_EXPR:
5247 int flags = WANT_ARITH | WANT_ENUM;
5248 /* Unary plus (but not unary minus) is allowed on pointers. */
5249 if (code == UNARY_PLUS_EXPR)
5250 flags |= WANT_POINTER;
5251 arg = build_expr_type_conversion (flags, arg, true);
5252 if (!arg)
5253 errstring = (code == NEGATE_EXPR
5254 ? _("wrong type argument to unary minus")
5255 : _("wrong type argument to unary plus"));
5256 else
5258 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5259 arg = cp_perform_integral_promotions (arg, complain);
5261 /* Make sure the result is not an lvalue: a unary plus or minus
5262 expression is always a rvalue. */
5263 arg = rvalue (arg);
5266 break;
5268 case BIT_NOT_EXPR:
5269 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5271 code = CONJ_EXPR;
5272 if (!noconvert)
5274 arg = cp_default_conversion (arg, complain);
5275 if (arg == error_mark_node)
5276 return error_mark_node;
5279 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5280 | WANT_VECTOR_OR_COMPLEX,
5281 arg, true)))
5282 errstring = _("wrong type argument to bit-complement");
5283 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5284 arg = cp_perform_integral_promotions (arg, complain);
5285 break;
5287 case ABS_EXPR:
5288 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5289 errstring = _("wrong type argument to abs");
5290 else if (!noconvert)
5292 arg = cp_default_conversion (arg, complain);
5293 if (arg == error_mark_node)
5294 return error_mark_node;
5296 break;
5298 case CONJ_EXPR:
5299 /* Conjugating a real value is a no-op, but allow it anyway. */
5300 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5301 errstring = _("wrong type argument to conjugation");
5302 else if (!noconvert)
5304 arg = cp_default_conversion (arg, complain);
5305 if (arg == error_mark_node)
5306 return error_mark_node;
5308 break;
5310 case TRUTH_NOT_EXPR:
5311 arg = perform_implicit_conversion (boolean_type_node, arg,
5312 complain);
5313 val = invert_truthvalue_loc (input_location, arg);
5314 if (arg != error_mark_node)
5315 return val;
5316 errstring = _("in argument to unary !");
5317 break;
5319 case NOP_EXPR:
5320 break;
5322 case REALPART_EXPR:
5323 case IMAGPART_EXPR:
5324 arg = build_real_imag_expr (input_location, code, arg);
5325 if (arg == error_mark_node)
5326 return arg;
5327 else
5328 return fold_if_not_in_template (arg);
5330 case PREINCREMENT_EXPR:
5331 case POSTINCREMENT_EXPR:
5332 case PREDECREMENT_EXPR:
5333 case POSTDECREMENT_EXPR:
5334 /* Handle complex lvalues (when permitted)
5335 by reduction to simpler cases. */
5337 val = unary_complex_lvalue (code, arg);
5338 if (val != 0)
5339 return val;
5341 arg = mark_lvalue_use (arg);
5343 /* Increment or decrement the real part of the value,
5344 and don't change the imaginary part. */
5345 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5347 tree real, imag;
5349 arg = stabilize_reference (arg);
5350 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5351 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5352 real = cp_build_unary_op (code, real, 1, complain);
5353 if (real == error_mark_node || imag == error_mark_node)
5354 return error_mark_node;
5355 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5356 real, imag);
5359 /* Report invalid types. */
5361 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5362 arg, true)))
5364 if (code == PREINCREMENT_EXPR)
5365 errstring = _("no pre-increment operator for type");
5366 else if (code == POSTINCREMENT_EXPR)
5367 errstring = _("no post-increment operator for type");
5368 else if (code == PREDECREMENT_EXPR)
5369 errstring = _("no pre-decrement operator for type");
5370 else
5371 errstring = _("no post-decrement operator for type");
5372 break;
5374 else if (arg == error_mark_node)
5375 return error_mark_node;
5377 /* Report something read-only. */
5379 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5380 || TREE_READONLY (arg))
5382 if (complain & tf_error)
5383 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5384 || code == POSTINCREMENT_EXPR)
5385 ? lv_increment : lv_decrement));
5386 else
5387 return error_mark_node;
5391 tree inc;
5392 tree declared_type = unlowered_expr_type (arg);
5394 argtype = TREE_TYPE (arg);
5396 /* ARM $5.2.5 last annotation says this should be forbidden. */
5397 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5399 if (complain & tf_error)
5400 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5401 ? G_("ISO C++ forbids incrementing an enum")
5402 : G_("ISO C++ forbids decrementing an enum"));
5403 else
5404 return error_mark_node;
5407 /* Compute the increment. */
5409 if (TREE_CODE (argtype) == POINTER_TYPE)
5411 tree type = complete_type (TREE_TYPE (argtype));
5413 if (!COMPLETE_OR_VOID_TYPE_P (type))
5415 if (complain & tf_error)
5416 error (((code == PREINCREMENT_EXPR
5417 || code == POSTINCREMENT_EXPR))
5418 ? G_("cannot increment a pointer to incomplete type %qT")
5419 : G_("cannot decrement a pointer to incomplete type %qT"),
5420 TREE_TYPE (argtype));
5421 else
5422 return error_mark_node;
5424 else if ((pedantic || warn_pointer_arith)
5425 && !TYPE_PTROB_P (argtype))
5427 if (complain & tf_error)
5428 permerror (input_location, (code == PREINCREMENT_EXPR
5429 || code == POSTINCREMENT_EXPR)
5430 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5431 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5432 argtype);
5433 else
5434 return error_mark_node;
5437 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5439 else
5440 inc = integer_one_node;
5442 inc = cp_convert (argtype, inc, complain);
5444 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5445 need to ask Objective-C to build the increment or decrement
5446 expression for it. */
5447 if (objc_is_property_ref (arg))
5448 return objc_build_incr_expr_for_property_ref (input_location, code,
5449 arg, inc);
5451 /* Complain about anything else that is not a true lvalue. */
5452 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5453 || code == POSTINCREMENT_EXPR)
5454 ? lv_increment : lv_decrement),
5455 complain))
5456 return error_mark_node;
5458 /* Forbid using -- on `bool'. */
5459 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5461 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5463 if (complain & tf_error)
5464 error ("invalid use of Boolean expression as operand "
5465 "to %<operator--%>");
5466 return error_mark_node;
5468 val = boolean_increment (code, arg);
5470 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5471 /* An rvalue has no cv-qualifiers. */
5472 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5473 else
5474 val = build2 (code, TREE_TYPE (arg), arg, inc);
5476 TREE_SIDE_EFFECTS (val) = 1;
5477 return val;
5480 case ADDR_EXPR:
5481 /* Note that this operation never does default_conversion
5482 regardless of NOCONVERT. */
5483 return cp_build_addr_expr (arg, complain);
5485 default:
5486 break;
5489 if (!errstring)
5491 if (argtype == 0)
5492 argtype = TREE_TYPE (arg);
5493 return fold_if_not_in_template (build1 (code, argtype, arg));
5496 if (complain & tf_error)
5497 error ("%s", errstring);
5498 return error_mark_node;
5501 /* Hook for the c-common bits that build a unary op. */
5502 tree
5503 build_unary_op (location_t /*location*/,
5504 enum tree_code code, tree xarg, int noconvert)
5506 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5509 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5510 for certain kinds of expressions which are not really lvalues
5511 but which we can accept as lvalues.
5513 If ARG is not a kind of expression we can handle, return
5514 NULL_TREE. */
5516 tree
5517 unary_complex_lvalue (enum tree_code code, tree arg)
5519 /* Inside a template, making these kinds of adjustments is
5520 pointless; we are only concerned with the type of the
5521 expression. */
5522 if (processing_template_decl)
5523 return NULL_TREE;
5525 /* Handle (a, b) used as an "lvalue". */
5526 if (TREE_CODE (arg) == COMPOUND_EXPR)
5528 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5529 tf_warning_or_error);
5530 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5531 TREE_OPERAND (arg, 0), real_result);
5534 /* Handle (a ? b : c) used as an "lvalue". */
5535 if (TREE_CODE (arg) == COND_EXPR
5536 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5537 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5539 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5540 if (TREE_CODE (arg) == MODIFY_EXPR
5541 || TREE_CODE (arg) == PREINCREMENT_EXPR
5542 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5544 tree lvalue = TREE_OPERAND (arg, 0);
5545 if (TREE_SIDE_EFFECTS (lvalue))
5547 lvalue = stabilize_reference (lvalue);
5548 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5549 lvalue, TREE_OPERAND (arg, 1));
5551 return unary_complex_lvalue
5552 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5555 if (code != ADDR_EXPR)
5556 return NULL_TREE;
5558 /* Handle (a = b) used as an "lvalue" for `&'. */
5559 if (TREE_CODE (arg) == MODIFY_EXPR
5560 || TREE_CODE (arg) == INIT_EXPR)
5562 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5563 tf_warning_or_error);
5564 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5565 arg, real_result);
5566 TREE_NO_WARNING (arg) = 1;
5567 return arg;
5570 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5571 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5572 || TREE_CODE (arg) == OFFSET_REF)
5573 return NULL_TREE;
5575 /* We permit compiler to make function calls returning
5576 objects of aggregate type look like lvalues. */
5578 tree targ = arg;
5580 if (TREE_CODE (targ) == SAVE_EXPR)
5581 targ = TREE_OPERAND (targ, 0);
5583 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5585 if (TREE_CODE (arg) == SAVE_EXPR)
5586 targ = arg;
5587 else
5588 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5589 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5592 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5593 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5594 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5597 /* Don't let anything else be handled specially. */
5598 return NULL_TREE;
5601 /* Mark EXP saying that we need to be able to take the
5602 address of it; it should not be allocated in a register.
5603 Value is true if successful.
5605 C++: we do not allow `current_class_ptr' to be addressable. */
5607 bool
5608 cxx_mark_addressable (tree exp)
5610 tree x = exp;
5612 while (1)
5613 switch (TREE_CODE (x))
5615 case ADDR_EXPR:
5616 case COMPONENT_REF:
5617 case ARRAY_REF:
5618 case REALPART_EXPR:
5619 case IMAGPART_EXPR:
5620 x = TREE_OPERAND (x, 0);
5621 break;
5623 case PARM_DECL:
5624 if (x == current_class_ptr)
5626 error ("cannot take the address of %<this%>, which is an rvalue expression");
5627 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5628 return true;
5630 /* Fall through. */
5632 case VAR_DECL:
5633 /* Caller should not be trying to mark initialized
5634 constant fields addressable. */
5635 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5636 || DECL_IN_AGGR_P (x) == 0
5637 || TREE_STATIC (x)
5638 || DECL_EXTERNAL (x));
5639 /* Fall through. */
5641 case RESULT_DECL:
5642 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5643 && !DECL_ARTIFICIAL (x))
5645 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
5647 error
5648 ("address of explicit register variable %qD requested", x);
5649 return false;
5651 else if (extra_warnings)
5652 warning
5653 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5655 TREE_ADDRESSABLE (x) = 1;
5656 return true;
5658 case CONST_DECL:
5659 case FUNCTION_DECL:
5660 TREE_ADDRESSABLE (x) = 1;
5661 return true;
5663 case CONSTRUCTOR:
5664 TREE_ADDRESSABLE (x) = 1;
5665 return true;
5667 case TARGET_EXPR:
5668 TREE_ADDRESSABLE (x) = 1;
5669 cxx_mark_addressable (TREE_OPERAND (x, 0));
5670 return true;
5672 default:
5673 return true;
5677 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5679 tree
5680 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
5681 tsubst_flags_t complain)
5683 tree orig_ifexp = ifexp;
5684 tree orig_op1 = op1;
5685 tree orig_op2 = op2;
5686 tree expr;
5688 if (processing_template_decl)
5690 /* The standard says that the expression is type-dependent if
5691 IFEXP is type-dependent, even though the eventual type of the
5692 expression doesn't dependent on IFEXP. */
5693 if (type_dependent_expression_p (ifexp)
5694 /* As a GNU extension, the middle operand may be omitted. */
5695 || (op1 && type_dependent_expression_p (op1))
5696 || type_dependent_expression_p (op2))
5697 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
5698 ifexp = build_non_dependent_expr (ifexp);
5699 if (op1)
5700 op1 = build_non_dependent_expr (op1);
5701 op2 = build_non_dependent_expr (op2);
5704 expr = build_conditional_expr (ifexp, op1, op2, complain);
5705 if (processing_template_decl && expr != error_mark_node)
5707 tree min = build_min_non_dep (COND_EXPR, expr,
5708 orig_ifexp, orig_op1, orig_op2);
5709 /* In C++11, remember that the result is an lvalue or xvalue.
5710 In C++98, lvalue_kind can just assume lvalue in a template. */
5711 if (cxx_dialect >= cxx0x
5712 && lvalue_or_rvalue_with_address_p (expr)
5713 && !lvalue_or_rvalue_with_address_p (min))
5714 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5715 !real_lvalue_p (expr));
5716 expr = convert_from_reference (min);
5718 return expr;
5721 /* Given a list of expressions, return a compound expression
5722 that performs them all and returns the value of the last of them. */
5724 tree
5725 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5726 tsubst_flags_t complain)
5728 tree expr = TREE_VALUE (list);
5730 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5731 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5733 if (complain & tf_error)
5734 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5735 "non-class type must not be parenthesized");
5736 else
5737 return error_mark_node;
5740 if (TREE_CHAIN (list))
5742 if (complain & tf_error)
5743 switch (exp)
5745 case ELK_INIT:
5746 permerror (input_location, "expression list treated as compound "
5747 "expression in initializer");
5748 break;
5749 case ELK_MEM_INIT:
5750 permerror (input_location, "expression list treated as compound "
5751 "expression in mem-initializer");
5752 break;
5753 case ELK_FUNC_CAST:
5754 permerror (input_location, "expression list treated as compound "
5755 "expression in functional cast");
5756 break;
5757 default:
5758 gcc_unreachable ();
5760 else
5761 return error_mark_node;
5763 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5764 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
5765 expr, TREE_VALUE (list), complain);
5768 return expr;
5771 /* Like build_x_compound_expr_from_list, but using a VEC. */
5773 tree
5774 build_x_compound_expr_from_vec (VEC(tree,gc) *vec, const char *msg,
5775 tsubst_flags_t complain)
5777 if (VEC_empty (tree, vec))
5778 return NULL_TREE;
5779 else if (VEC_length (tree, vec) == 1)
5780 return VEC_index (tree, vec, 0);
5781 else
5783 tree expr;
5784 unsigned int ix;
5785 tree t;
5787 if (msg != NULL)
5789 if (complain & tf_error)
5790 permerror (input_location,
5791 "%s expression list treated as compound expression",
5792 msg);
5793 else
5794 return error_mark_node;
5797 expr = VEC_index (tree, vec, 0);
5798 for (ix = 1; VEC_iterate (tree, vec, ix, t); ++ix)
5799 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
5800 t, complain);
5802 return expr;
5806 /* Handle overloading of the ',' operator when needed. */
5808 tree
5809 build_x_compound_expr (location_t loc, tree op1, tree op2,
5810 tsubst_flags_t complain)
5812 tree result;
5813 tree orig_op1 = op1;
5814 tree orig_op2 = op2;
5816 if (processing_template_decl)
5818 if (type_dependent_expression_p (op1)
5819 || type_dependent_expression_p (op2))
5820 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
5821 op1 = build_non_dependent_expr (op1);
5822 op2 = build_non_dependent_expr (op2);
5825 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
5826 NULL_TREE, /*overload=*/NULL, complain);
5827 if (!result)
5828 result = cp_build_compound_expr (op1, op2, complain);
5830 if (processing_template_decl && result != error_mark_node)
5831 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
5833 return result;
5836 /* Like cp_build_compound_expr, but for the c-common bits. */
5838 tree
5839 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
5841 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
5844 /* Build a compound expression. */
5846 tree
5847 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
5849 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
5851 if (lhs == error_mark_node || rhs == error_mark_node)
5852 return error_mark_node;
5854 if (TREE_CODE (rhs) == TARGET_EXPR)
5856 /* If the rhs is a TARGET_EXPR, then build the compound
5857 expression inside the target_expr's initializer. This
5858 helps the compiler to eliminate unnecessary temporaries. */
5859 tree init = TREE_OPERAND (rhs, 1);
5861 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
5862 TREE_OPERAND (rhs, 1) = init;
5864 return rhs;
5867 if (type_unknown_p (rhs))
5869 error ("no context to resolve type of %qE", rhs);
5870 return error_mark_node;
5873 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
5876 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
5877 casts away constness. CAST gives the type of cast. Returns true
5878 if the cast is ill-formed, false if it is well-formed.
5880 ??? This function warns for casting away any qualifier not just
5881 const. We would like to specify exactly what qualifiers are casted
5882 away.
5885 static bool
5886 check_for_casting_away_constness (tree src_type, tree dest_type,
5887 enum tree_code cast, tsubst_flags_t complain)
5889 /* C-style casts are allowed to cast away constness. With
5890 WARN_CAST_QUAL, we still want to issue a warning. */
5891 if (cast == CAST_EXPR && !warn_cast_qual)
5892 return false;
5894 if (!casts_away_constness (src_type, dest_type, complain))
5895 return false;
5897 switch (cast)
5899 case CAST_EXPR:
5900 if (complain & tf_warning)
5901 warning (OPT_Wcast_qual,
5902 "cast from type %qT to type %qT casts away qualifiers",
5903 src_type, dest_type);
5904 return false;
5906 case STATIC_CAST_EXPR:
5907 if (complain & tf_error)
5908 error ("static_cast from type %qT to type %qT casts away qualifiers",
5909 src_type, dest_type);
5910 return true;
5912 case REINTERPRET_CAST_EXPR:
5913 if (complain & tf_error)
5914 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
5915 src_type, dest_type);
5916 return true;
5918 default:
5919 gcc_unreachable();
5924 Warns if the cast from expression EXPR to type TYPE is useless.
5926 void
5927 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
5929 if (warn_useless_cast
5930 && complain & tf_warning
5931 && c_inhibit_evaluation_warnings == 0)
5933 if (REFERENCE_REF_P (expr))
5934 expr = TREE_OPERAND (expr, 0);
5936 if ((TREE_CODE (type) == REFERENCE_TYPE
5937 && (TYPE_REF_IS_RVALUE (type)
5938 ? xvalue_p (expr) : real_lvalue_p (expr))
5939 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
5940 || same_type_p (TREE_TYPE (expr), type))
5941 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
5945 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
5946 (another pointer-to-member type in the same hierarchy) and return
5947 the converted expression. If ALLOW_INVERSE_P is permitted, a
5948 pointer-to-derived may be converted to pointer-to-base; otherwise,
5949 only the other direction is permitted. If C_CAST_P is true, this
5950 conversion is taking place as part of a C-style cast. */
5952 tree
5953 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
5954 bool c_cast_p, tsubst_flags_t complain)
5956 if (TYPE_PTRDATAMEM_P (type))
5958 tree delta;
5960 if (TREE_CODE (expr) == PTRMEM_CST)
5961 expr = cplus_expand_constant (expr);
5962 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
5963 TYPE_PTRMEM_CLASS_TYPE (type),
5964 allow_inverse_p,
5965 c_cast_p, complain);
5966 if (delta == error_mark_node)
5967 return error_mark_node;
5969 if (!integer_zerop (delta))
5971 tree cond, op1, op2;
5973 cond = cp_build_binary_op (input_location,
5974 EQ_EXPR,
5975 expr,
5976 build_int_cst (TREE_TYPE (expr), -1),
5977 complain);
5978 op1 = build_nop (ptrdiff_type_node, expr);
5979 op2 = cp_build_binary_op (input_location,
5980 PLUS_EXPR, op1, delta,
5981 complain);
5983 expr = fold_build3_loc (input_location,
5984 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
5988 return build_nop (type, expr);
5990 else
5991 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
5992 allow_inverse_p, c_cast_p, complain);
5995 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
5996 this static_cast is being attempted as one of the possible casts
5997 allowed by a C-style cast. (In that case, accessibility of base
5998 classes is not considered, and it is OK to cast away
5999 constness.) Return the result of the cast. *VALID_P is set to
6000 indicate whether or not the cast was valid. */
6002 static tree
6003 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6004 bool *valid_p, tsubst_flags_t complain)
6006 tree intype;
6007 tree result;
6008 cp_lvalue_kind clk;
6010 /* Assume the cast is valid. */
6011 *valid_p = true;
6013 intype = unlowered_expr_type (expr);
6015 /* Save casted types in the function's used types hash table. */
6016 used_types_insert (type);
6018 /* [expr.static.cast]
6020 An lvalue of type "cv1 B", where B is a class type, can be cast
6021 to type "reference to cv2 D", where D is a class derived (clause
6022 _class.derived_) from B, if a valid standard conversion from
6023 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6024 same cv-qualification as, or greater cv-qualification than, cv1,
6025 and B is not a virtual base class of D. */
6026 /* We check this case before checking the validity of "TYPE t =
6027 EXPR;" below because for this case:
6029 struct B {};
6030 struct D : public B { D(const B&); };
6031 extern B& b;
6032 void f() { static_cast<const D&>(b); }
6034 we want to avoid constructing a new D. The standard is not
6035 completely clear about this issue, but our interpretation is
6036 consistent with other compilers. */
6037 if (TREE_CODE (type) == REFERENCE_TYPE
6038 && CLASS_TYPE_P (TREE_TYPE (type))
6039 && CLASS_TYPE_P (intype)
6040 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6041 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6042 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6043 build_pointer_type (TYPE_MAIN_VARIANT
6044 (TREE_TYPE (type))),
6045 complain)
6046 && (c_cast_p
6047 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6049 tree base;
6051 /* There is a standard conversion from "D*" to "B*" even if "B"
6052 is ambiguous or inaccessible. If this is really a
6053 static_cast, then we check both for inaccessibility and
6054 ambiguity. However, if this is a static_cast being performed
6055 because the user wrote a C-style cast, then accessibility is
6056 not considered. */
6057 base = lookup_base (TREE_TYPE (type), intype,
6058 c_cast_p ? ba_unique : ba_check,
6059 NULL, complain);
6061 /* Convert from "B*" to "D*". This function will check that "B"
6062 is not a virtual base of "D". */
6063 expr = build_base_path (MINUS_EXPR, build_address (expr),
6064 base, /*nonnull=*/false, complain);
6065 /* Convert the pointer to a reference -- but then remember that
6066 there are no expressions with reference type in C++.
6068 We call rvalue so that there's an actual tree code
6069 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6070 is a variable with the same type, the conversion would get folded
6071 away, leaving just the variable and causing lvalue_kind to give
6072 the wrong answer. */
6073 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6076 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6077 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6078 if (TREE_CODE (type) == REFERENCE_TYPE
6079 && TYPE_REF_IS_RVALUE (type)
6080 && (clk = real_lvalue_p (expr))
6081 && reference_related_p (TREE_TYPE (type), intype)
6082 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6084 if (clk == clk_ordinary)
6086 /* Handle the (non-bit-field) lvalue case here by casting to
6087 lvalue reference and then changing it to an rvalue reference.
6088 Casting an xvalue to rvalue reference will be handled by the
6089 main code path. */
6090 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6091 result = (perform_direct_initialization_if_possible
6092 (lref, expr, c_cast_p, complain));
6093 result = cp_fold_convert (type, result);
6094 /* Make sure we don't fold back down to a named rvalue reference,
6095 because that would be an lvalue. */
6096 if (DECL_P (result))
6097 result = build1 (NON_LVALUE_EXPR, type, result);
6098 return convert_from_reference (result);
6100 else
6101 /* For a bit-field or packed field, bind to a temporary. */
6102 expr = rvalue (expr);
6105 /* Resolve overloaded address here rather than once in
6106 implicit_conversion and again in the inverse code below. */
6107 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6109 expr = instantiate_type (type, expr, complain);
6110 intype = TREE_TYPE (expr);
6113 /* [expr.static.cast]
6115 Any expression can be explicitly converted to type cv void. */
6116 if (TREE_CODE (type) == VOID_TYPE)
6117 return convert_to_void (expr, ICV_CAST, complain);
6119 /* [expr.static.cast]
6121 An expression e can be explicitly converted to a type T using a
6122 static_cast of the form static_cast<T>(e) if the declaration T
6123 t(e);" is well-formed, for some invented temporary variable
6124 t. */
6125 result = perform_direct_initialization_if_possible (type, expr,
6126 c_cast_p, complain);
6127 if (result)
6129 result = convert_from_reference (result);
6131 /* [expr.static.cast]
6133 If T is a reference type, the result is an lvalue; otherwise,
6134 the result is an rvalue. */
6135 if (TREE_CODE (type) != REFERENCE_TYPE)
6136 result = rvalue (result);
6137 return result;
6140 /* [expr.static.cast]
6142 The inverse of any standard conversion sequence (clause _conv_),
6143 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6144 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6145 (_conv.bool_) conversions, can be performed explicitly using
6146 static_cast subject to the restriction that the explicit
6147 conversion does not cast away constness (_expr.const.cast_), and
6148 the following additional rules for specific cases: */
6149 /* For reference, the conversions not excluded are: integral
6150 promotions, floating point promotion, integral conversions,
6151 floating point conversions, floating-integral conversions,
6152 pointer conversions, and pointer to member conversions. */
6153 /* DR 128
6155 A value of integral _or enumeration_ type can be explicitly
6156 converted to an enumeration type. */
6157 /* The effect of all that is that any conversion between any two
6158 types which are integral, floating, or enumeration types can be
6159 performed. */
6160 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6161 || SCALAR_FLOAT_TYPE_P (type))
6162 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6163 || SCALAR_FLOAT_TYPE_P (intype)))
6164 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6166 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6167 && CLASS_TYPE_P (TREE_TYPE (type))
6168 && CLASS_TYPE_P (TREE_TYPE (intype))
6169 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6170 (TREE_TYPE (intype))),
6171 build_pointer_type (TYPE_MAIN_VARIANT
6172 (TREE_TYPE (type))),
6173 complain))
6175 tree base;
6177 if (!c_cast_p
6178 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6179 complain))
6180 return error_mark_node;
6181 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6182 c_cast_p ? ba_unique : ba_check,
6183 NULL, complain);
6184 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6185 complain);
6186 return cp_fold_convert(type, expr);
6189 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6190 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6192 tree c1;
6193 tree c2;
6194 tree t1;
6195 tree t2;
6197 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6198 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6200 if (TYPE_PTRDATAMEM_P (type))
6202 t1 = (build_ptrmem_type
6203 (c1,
6204 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6205 t2 = (build_ptrmem_type
6206 (c2,
6207 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6209 else
6211 t1 = intype;
6212 t2 = type;
6214 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6216 if (!c_cast_p
6217 && check_for_casting_away_constness (intype, type,
6218 STATIC_CAST_EXPR,
6219 complain))
6220 return error_mark_node;
6221 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6222 c_cast_p, complain);
6226 /* [expr.static.cast]
6228 An rvalue of type "pointer to cv void" can be explicitly
6229 converted to a pointer to object type. A value of type pointer
6230 to object converted to "pointer to cv void" and back to the
6231 original pointer type will have its original value. */
6232 if (TREE_CODE (intype) == POINTER_TYPE
6233 && VOID_TYPE_P (TREE_TYPE (intype))
6234 && TYPE_PTROB_P (type))
6236 if (!c_cast_p
6237 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6238 complain))
6239 return error_mark_node;
6240 return build_nop (type, expr);
6243 *valid_p = false;
6244 return error_mark_node;
6247 /* Return an expression representing static_cast<TYPE>(EXPR). */
6249 tree
6250 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6252 tree result;
6253 bool valid_p;
6255 if (type == error_mark_node || expr == error_mark_node)
6256 return error_mark_node;
6258 if (processing_template_decl)
6260 expr = build_min (STATIC_CAST_EXPR, type, expr);
6261 /* We don't know if it will or will not have side effects. */
6262 TREE_SIDE_EFFECTS (expr) = 1;
6263 return convert_from_reference (expr);
6266 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6267 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6268 if (TREE_CODE (type) != REFERENCE_TYPE
6269 && TREE_CODE (expr) == NOP_EXPR
6270 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6271 expr = TREE_OPERAND (expr, 0);
6273 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6274 complain);
6275 if (valid_p)
6277 if (result != error_mark_node)
6278 maybe_warn_about_useless_cast (type, expr, complain);
6279 return result;
6282 if (complain & tf_error)
6283 error ("invalid static_cast from type %qT to type %qT",
6284 TREE_TYPE (expr), type);
6285 return error_mark_node;
6288 /* EXPR is an expression with member function or pointer-to-member
6289 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6290 not permitted by ISO C++, but we accept it in some modes. If we
6291 are not in one of those modes, issue a diagnostic. Return the
6292 converted expression. */
6294 tree
6295 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6297 tree intype;
6298 tree decl;
6300 intype = TREE_TYPE (expr);
6301 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6302 || TREE_CODE (intype) == METHOD_TYPE);
6304 if (!(complain & tf_warning_or_error))
6305 return error_mark_node;
6307 if (pedantic || warn_pmf2ptr)
6308 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6309 "converting from %qT to %qT", intype, type);
6311 if (TREE_CODE (intype) == METHOD_TYPE)
6312 expr = build_addr_func (expr, complain);
6313 else if (TREE_CODE (expr) == PTRMEM_CST)
6314 expr = build_address (PTRMEM_CST_MEMBER (expr));
6315 else
6317 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6318 decl = build_address (decl);
6319 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6322 if (expr == error_mark_node)
6323 return error_mark_node;
6325 return build_nop (type, expr);
6328 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6329 If C_CAST_P is true, this reinterpret cast is being done as part of
6330 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6331 indicate whether or not reinterpret_cast was valid. */
6333 static tree
6334 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6335 bool *valid_p, tsubst_flags_t complain)
6337 tree intype;
6339 /* Assume the cast is invalid. */
6340 if (valid_p)
6341 *valid_p = true;
6343 if (type == error_mark_node || error_operand_p (expr))
6344 return error_mark_node;
6346 intype = TREE_TYPE (expr);
6348 /* Save casted types in the function's used types hash table. */
6349 used_types_insert (type);
6351 /* [expr.reinterpret.cast]
6352 An lvalue expression of type T1 can be cast to the type
6353 "reference to T2" if an expression of type "pointer to T1" can be
6354 explicitly converted to the type "pointer to T2" using a
6355 reinterpret_cast. */
6356 if (TREE_CODE (type) == REFERENCE_TYPE)
6358 if (! real_lvalue_p (expr))
6360 if (complain & tf_error)
6361 error ("invalid cast of an rvalue expression of type "
6362 "%qT to type %qT",
6363 intype, type);
6364 return error_mark_node;
6367 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6368 "B" are related class types; the reinterpret_cast does not
6369 adjust the pointer. */
6370 if (TYPE_PTR_P (intype)
6371 && (complain & tf_warning)
6372 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6373 COMPARE_BASE | COMPARE_DERIVED)))
6374 warning (0, "casting %qT to %qT does not dereference pointer",
6375 intype, type);
6377 expr = cp_build_addr_expr (expr, complain);
6379 if (warn_strict_aliasing > 2)
6380 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6382 if (expr != error_mark_node)
6383 expr = build_reinterpret_cast_1
6384 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6385 valid_p, complain);
6386 if (expr != error_mark_node)
6387 /* cp_build_indirect_ref isn't right for rvalue refs. */
6388 expr = convert_from_reference (fold_convert (type, expr));
6389 return expr;
6392 /* As a G++ extension, we consider conversions from member
6393 functions, and pointers to member functions to
6394 pointer-to-function and pointer-to-void types. If
6395 -Wno-pmf-conversions has not been specified,
6396 convert_member_func_to_ptr will issue an error message. */
6397 if ((TYPE_PTRMEMFUNC_P (intype)
6398 || TREE_CODE (intype) == METHOD_TYPE)
6399 && TYPE_PTR_P (type)
6400 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6401 || VOID_TYPE_P (TREE_TYPE (type))))
6402 return convert_member_func_to_ptr (type, expr, complain);
6404 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6405 array-to-pointer, and function-to-pointer conversions are
6406 performed. */
6407 expr = decay_conversion (expr, complain);
6409 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6410 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6411 if (TREE_CODE (expr) == NOP_EXPR
6412 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6413 expr = TREE_OPERAND (expr, 0);
6415 if (error_operand_p (expr))
6416 return error_mark_node;
6418 intype = TREE_TYPE (expr);
6420 /* [expr.reinterpret.cast]
6421 A pointer can be converted to any integral type large enough to
6422 hold it. ... A value of type std::nullptr_t can be converted to
6423 an integral type; the conversion has the same meaning and
6424 validity as a conversion of (void*)0 to the integral type. */
6425 if (CP_INTEGRAL_TYPE_P (type)
6426 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6428 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6430 if (complain & tf_error)
6431 permerror (input_location, "cast from %qT to %qT loses precision",
6432 intype, type);
6433 else
6434 return error_mark_node;
6436 if (NULLPTR_TYPE_P (intype))
6437 return build_int_cst (type, 0);
6439 /* [expr.reinterpret.cast]
6440 A value of integral or enumeration type can be explicitly
6441 converted to a pointer. */
6442 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6443 /* OK */
6445 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6446 || TYPE_PTR_OR_PTRMEM_P (type))
6447 && same_type_p (type, intype))
6448 /* DR 799 */
6449 return fold_if_not_in_template (build_nop (type, expr));
6450 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6451 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6452 return fold_if_not_in_template (build_nop (type, expr));
6453 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6454 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6456 tree sexpr = expr;
6458 if (!c_cast_p
6459 && check_for_casting_away_constness (intype, type,
6460 REINTERPRET_CAST_EXPR,
6461 complain))
6462 return error_mark_node;
6463 /* Warn about possible alignment problems. */
6464 if (STRICT_ALIGNMENT && warn_cast_align
6465 && (complain & tf_warning)
6466 && !VOID_TYPE_P (type)
6467 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6468 && COMPLETE_TYPE_P (TREE_TYPE (type))
6469 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6470 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6471 warning (OPT_Wcast_align, "cast from %qT to %qT "
6472 "increases required alignment of target type", intype, type);
6474 /* We need to strip nops here, because the front end likes to
6475 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6476 STRIP_NOPS (sexpr);
6477 if (warn_strict_aliasing <= 2)
6478 strict_aliasing_warning (intype, type, sexpr);
6480 return fold_if_not_in_template (build_nop (type, expr));
6482 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6483 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6485 if (pedantic && (complain & tf_warning))
6486 /* Only issue a warning, as we have always supported this
6487 where possible, and it is necessary in some cases. DR 195
6488 addresses this issue, but as of 2004/10/26 is still in
6489 drafting. */
6490 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6491 return fold_if_not_in_template (build_nop (type, expr));
6493 else if (TREE_CODE (type) == VECTOR_TYPE)
6494 return fold_if_not_in_template (convert_to_vector (type, expr));
6495 else if (TREE_CODE (intype) == VECTOR_TYPE
6496 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6497 return fold_if_not_in_template (convert_to_integer (type, expr));
6498 else
6500 if (valid_p)
6501 *valid_p = false;
6502 if (complain & tf_error)
6503 error ("invalid cast from type %qT to type %qT", intype, type);
6504 return error_mark_node;
6507 return cp_convert (type, expr, complain);
6510 tree
6511 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6513 tree r;
6515 if (type == error_mark_node || expr == error_mark_node)
6516 return error_mark_node;
6518 if (processing_template_decl)
6520 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6522 if (!TREE_SIDE_EFFECTS (t)
6523 && type_dependent_expression_p (expr))
6524 /* There might turn out to be side effects inside expr. */
6525 TREE_SIDE_EFFECTS (t) = 1;
6526 return convert_from_reference (t);
6529 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6530 /*valid_p=*/NULL, complain);
6531 if (r != error_mark_node)
6532 maybe_warn_about_useless_cast (type, expr, complain);
6533 return r;
6536 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6537 return an appropriate expression. Otherwise, return
6538 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6539 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6540 performing a C-style cast, its value upon return will indicate
6541 whether or not the conversion succeeded. */
6543 static tree
6544 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6545 bool *valid_p)
6547 tree src_type;
6548 tree reference_type;
6550 /* Callers are responsible for handling error_mark_node as a
6551 destination type. */
6552 gcc_assert (dst_type != error_mark_node);
6553 /* In a template, callers should be building syntactic
6554 representations of casts, not using this machinery. */
6555 gcc_assert (!processing_template_decl);
6557 /* Assume the conversion is invalid. */
6558 if (valid_p)
6559 *valid_p = false;
6561 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6563 if (complain & tf_error)
6564 error ("invalid use of const_cast with type %qT, "
6565 "which is not a pointer, "
6566 "reference, nor a pointer-to-data-member type", dst_type);
6567 return error_mark_node;
6570 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6572 if (complain & tf_error)
6573 error ("invalid use of const_cast with type %qT, which is a pointer "
6574 "or reference to a function type", dst_type);
6575 return error_mark_node;
6578 /* Save casted types in the function's used types hash table. */
6579 used_types_insert (dst_type);
6581 src_type = TREE_TYPE (expr);
6582 /* Expressions do not really have reference types. */
6583 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6584 src_type = TREE_TYPE (src_type);
6586 /* [expr.const.cast]
6588 For two object types T1 and T2, if a pointer to T1 can be explicitly
6589 converted to the type "pointer to T2" using a const_cast, then the
6590 following conversions can also be made:
6592 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6593 type T2 using the cast const_cast<T2&>;
6595 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6596 type T2 using the cast const_cast<T2&&>; and
6598 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6599 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6601 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6603 reference_type = dst_type;
6604 if (!TYPE_REF_IS_RVALUE (dst_type)
6605 ? real_lvalue_p (expr)
6606 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6607 ? lvalue_p (expr)
6608 : lvalue_or_rvalue_with_address_p (expr)))
6609 /* OK. */;
6610 else
6612 if (complain & tf_error)
6613 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6614 src_type, dst_type);
6615 return error_mark_node;
6617 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6618 src_type = build_pointer_type (src_type);
6620 else
6622 reference_type = NULL_TREE;
6623 /* If the destination type is not a reference type, the
6624 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6625 conversions are performed. */
6626 src_type = type_decays_to (src_type);
6627 if (src_type == error_mark_node)
6628 return error_mark_node;
6631 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
6633 if (comp_ptr_ttypes_const (dst_type, src_type))
6635 if (valid_p)
6637 *valid_p = true;
6638 /* This cast is actually a C-style cast. Issue a warning if
6639 the user is making a potentially unsafe cast. */
6640 check_for_casting_away_constness (src_type, dst_type,
6641 CAST_EXPR, complain);
6643 if (reference_type)
6645 expr = cp_build_addr_expr (expr, complain);
6646 if (expr == error_mark_node)
6647 return error_mark_node;
6648 expr = build_nop (reference_type, expr);
6649 return convert_from_reference (expr);
6651 else
6653 expr = decay_conversion (expr, complain);
6654 if (expr == error_mark_node)
6655 return error_mark_node;
6657 /* build_c_cast puts on a NOP_EXPR to make the result not an
6658 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6659 non-lvalue context. */
6660 if (TREE_CODE (expr) == NOP_EXPR
6661 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6662 expr = TREE_OPERAND (expr, 0);
6663 return build_nop (dst_type, expr);
6666 else if (valid_p
6667 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6668 TREE_TYPE (src_type)))
6669 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6670 complain);
6673 if (complain & tf_error)
6674 error ("invalid const_cast from type %qT to type %qT",
6675 src_type, dst_type);
6676 return error_mark_node;
6679 tree
6680 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6682 tree r;
6684 if (type == error_mark_node || error_operand_p (expr))
6685 return error_mark_node;
6687 if (processing_template_decl)
6689 tree t = build_min (CONST_CAST_EXPR, type, expr);
6691 if (!TREE_SIDE_EFFECTS (t)
6692 && type_dependent_expression_p (expr))
6693 /* There might turn out to be side effects inside expr. */
6694 TREE_SIDE_EFFECTS (t) = 1;
6695 return convert_from_reference (t);
6698 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
6699 if (r != error_mark_node)
6700 maybe_warn_about_useless_cast (type, expr, complain);
6701 return r;
6704 /* Like cp_build_c_cast, but for the c-common bits. */
6706 tree
6707 build_c_cast (location_t /*loc*/, tree type, tree expr)
6709 return cp_build_c_cast (type, expr, tf_warning_or_error);
6712 /* Build an expression representing an explicit C-style cast to type
6713 TYPE of expression EXPR. */
6715 tree
6716 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6718 tree value = expr;
6719 tree result;
6720 bool valid_p;
6722 if (type == error_mark_node || error_operand_p (expr))
6723 return error_mark_node;
6725 if (processing_template_decl)
6727 tree t = build_min (CAST_EXPR, type,
6728 tree_cons (NULL_TREE, value, NULL_TREE));
6729 /* We don't know if it will or will not have side effects. */
6730 TREE_SIDE_EFFECTS (t) = 1;
6731 return convert_from_reference (t);
6734 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6735 'Class') should always be retained, because this information aids
6736 in method lookup. */
6737 if (objc_is_object_ptr (type)
6738 && objc_is_object_ptr (TREE_TYPE (expr)))
6739 return build_nop (type, expr);
6741 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6742 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6743 if (TREE_CODE (type) != REFERENCE_TYPE
6744 && TREE_CODE (value) == NOP_EXPR
6745 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6746 value = TREE_OPERAND (value, 0);
6748 if (TREE_CODE (type) == ARRAY_TYPE)
6750 /* Allow casting from T1* to T2[] because Cfront allows it.
6751 NIHCL uses it. It is not valid ISO C++ however. */
6752 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6754 if (complain & tf_error)
6755 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6756 else
6757 return error_mark_node;
6758 type = build_pointer_type (TREE_TYPE (type));
6760 else
6762 if (complain & tf_error)
6763 error ("ISO C++ forbids casting to an array type %qT", type);
6764 return error_mark_node;
6768 if (TREE_CODE (type) == FUNCTION_TYPE
6769 || TREE_CODE (type) == METHOD_TYPE)
6771 if (complain & tf_error)
6772 error ("invalid cast to function type %qT", type);
6773 return error_mark_node;
6776 if (TREE_CODE (type) == POINTER_TYPE
6777 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
6778 /* Casting to an integer of smaller size is an error detected elsewhere. */
6779 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
6780 /* Don't warn about converting any constant. */
6781 && !TREE_CONSTANT (value))
6782 warning_at (input_location, OPT_Wint_to_pointer_cast,
6783 "cast to pointer from integer of different size");
6785 /* A C-style cast can be a const_cast. */
6786 result = build_const_cast_1 (type, value, complain & tf_warning,
6787 &valid_p);
6788 if (valid_p)
6790 if (result != error_mark_node)
6791 maybe_warn_about_useless_cast (type, value, complain);
6792 return result;
6795 /* Or a static cast. */
6796 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
6797 &valid_p, complain);
6798 /* Or a reinterpret_cast. */
6799 if (!valid_p)
6800 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
6801 &valid_p, complain);
6802 /* The static_cast or reinterpret_cast may be followed by a
6803 const_cast. */
6804 if (valid_p
6805 /* A valid cast may result in errors if, for example, a
6806 conversion to an ambiguous base class is required. */
6807 && !error_operand_p (result))
6809 tree result_type;
6811 maybe_warn_about_useless_cast (type, value, complain);
6813 /* Non-class rvalues always have cv-unqualified type. */
6814 if (!CLASS_TYPE_P (type))
6815 type = TYPE_MAIN_VARIANT (type);
6816 result_type = TREE_TYPE (result);
6817 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
6818 result_type = TYPE_MAIN_VARIANT (result_type);
6819 /* If the type of RESULT does not match TYPE, perform a
6820 const_cast to make it match. If the static_cast or
6821 reinterpret_cast succeeded, we will differ by at most
6822 cv-qualification, so the follow-on const_cast is guaranteed
6823 to succeed. */
6824 if (!same_type_p (non_reference (type), non_reference (result_type)))
6826 result = build_const_cast_1 (type, result, false, &valid_p);
6827 gcc_assert (valid_p);
6829 return result;
6832 return error_mark_node;
6835 /* For use from the C common bits. */
6836 tree
6837 build_modify_expr (location_t /*location*/,
6838 tree lhs, tree /*lhs_origtype*/,
6839 enum tree_code modifycode,
6840 location_t /*rhs_location*/, tree rhs,
6841 tree /*rhs_origtype*/)
6843 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
6846 /* Build an assignment expression of lvalue LHS from value RHS.
6847 MODIFYCODE is the code for a binary operator that we use
6848 to combine the old value of LHS with RHS to get the new value.
6849 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6851 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
6853 tree
6854 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6855 tsubst_flags_t complain)
6857 tree result;
6858 tree newrhs = rhs;
6859 tree lhstype = TREE_TYPE (lhs);
6860 tree olhstype = lhstype;
6861 bool plain_assign = (modifycode == NOP_EXPR);
6863 /* Avoid duplicate error messages from operands that had errors. */
6864 if (error_operand_p (lhs) || error_operand_p (rhs))
6865 return error_mark_node;
6867 /* Handle control structure constructs used as "lvalues". */
6868 switch (TREE_CODE (lhs))
6870 /* Handle --foo = 5; as these are valid constructs in C++. */
6871 case PREDECREMENT_EXPR:
6872 case PREINCREMENT_EXPR:
6873 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6874 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6875 stabilize_reference (TREE_OPERAND (lhs, 0)),
6876 TREE_OPERAND (lhs, 1));
6877 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
6878 modifycode, rhs, complain);
6879 if (newrhs == error_mark_node)
6880 return error_mark_node;
6881 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6883 /* Handle (a, b) used as an "lvalue". */
6884 case COMPOUND_EXPR:
6885 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6886 modifycode, rhs, complain);
6887 if (newrhs == error_mark_node)
6888 return error_mark_node;
6889 return build2 (COMPOUND_EXPR, lhstype,
6890 TREE_OPERAND (lhs, 0), newrhs);
6892 case MODIFY_EXPR:
6893 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6894 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6895 stabilize_reference (TREE_OPERAND (lhs, 0)),
6896 TREE_OPERAND (lhs, 1));
6897 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
6898 complain);
6899 if (newrhs == error_mark_node)
6900 return error_mark_node;
6901 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6903 case MIN_EXPR:
6904 case MAX_EXPR:
6905 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
6906 when neither operand has side-effects. */
6907 if (!lvalue_or_else (lhs, lv_assign, complain))
6908 return error_mark_node;
6910 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
6911 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
6913 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
6914 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
6915 boolean_type_node,
6916 TREE_OPERAND (lhs, 0),
6917 TREE_OPERAND (lhs, 1)),
6918 TREE_OPERAND (lhs, 0),
6919 TREE_OPERAND (lhs, 1));
6920 /* Fall through. */
6922 /* Handle (a ? b : c) used as an "lvalue". */
6923 case COND_EXPR:
6925 /* Produce (a ? (b = rhs) : (c = rhs))
6926 except that the RHS goes through a save-expr
6927 so the code to compute it is only emitted once. */
6928 tree cond;
6929 tree preeval = NULL_TREE;
6931 if (VOID_TYPE_P (TREE_TYPE (rhs)))
6933 if (complain & tf_error)
6934 error ("void value not ignored as it ought to be");
6935 return error_mark_node;
6938 rhs = stabilize_expr (rhs, &preeval);
6940 /* Check this here to avoid odd errors when trying to convert
6941 a throw to the type of the COND_EXPR. */
6942 if (!lvalue_or_else (lhs, lv_assign, complain))
6943 return error_mark_node;
6945 cond = build_conditional_expr
6946 (TREE_OPERAND (lhs, 0),
6947 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6948 modifycode, rhs, complain),
6949 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
6950 modifycode, rhs, complain),
6951 complain);
6953 if (cond == error_mark_node)
6954 return cond;
6955 /* Make sure the code to compute the rhs comes out
6956 before the split. */
6957 if (preeval)
6958 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
6959 return cond;
6962 default:
6963 break;
6966 if (modifycode == INIT_EXPR)
6968 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6969 /* Do the default thing. */;
6970 else if (TREE_CODE (rhs) == CONSTRUCTOR)
6972 /* Compound literal. */
6973 if (! same_type_p (TREE_TYPE (rhs), lhstype))
6974 /* Call convert to generate an error; see PR 11063. */
6975 rhs = convert (lhstype, rhs);
6976 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
6977 TREE_SIDE_EFFECTS (result) = 1;
6978 return result;
6980 else if (! MAYBE_CLASS_TYPE_P (lhstype))
6981 /* Do the default thing. */;
6982 else
6984 VEC(tree,gc) *rhs_vec = make_tree_vector_single (rhs);
6985 result = build_special_member_call (lhs, complete_ctor_identifier,
6986 &rhs_vec, lhstype, LOOKUP_NORMAL,
6987 complain);
6988 release_tree_vector (rhs_vec);
6989 if (result == NULL_TREE)
6990 return error_mark_node;
6991 return result;
6994 else
6996 lhs = require_complete_type_sfinae (lhs, complain);
6997 if (lhs == error_mark_node)
6998 return error_mark_node;
7000 if (modifycode == NOP_EXPR)
7002 if (c_dialect_objc ())
7004 result = objc_maybe_build_modify_expr (lhs, rhs);
7005 if (result)
7006 return result;
7009 /* `operator=' is not an inheritable operator. */
7010 if (! MAYBE_CLASS_TYPE_P (lhstype))
7011 /* Do the default thing. */;
7012 else
7014 result = build_new_op (input_location, MODIFY_EXPR,
7015 LOOKUP_NORMAL, lhs, rhs,
7016 make_node (NOP_EXPR), /*overload=*/NULL,
7017 complain);
7018 if (result == NULL_TREE)
7019 return error_mark_node;
7020 return result;
7022 lhstype = olhstype;
7024 else
7026 tree init = NULL_TREE;
7028 /* A binary op has been requested. Combine the old LHS
7029 value with the RHS producing the value we should actually
7030 store into the LHS. */
7031 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7032 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7033 || MAYBE_CLASS_TYPE_P (lhstype)));
7035 /* Preevaluate the RHS to make sure its evaluation is complete
7036 before the lvalue-to-rvalue conversion of the LHS:
7038 [expr.ass] With respect to an indeterminately-sequenced
7039 function call, the operation of a compound assignment is a
7040 single evaluation. [ Note: Therefore, a function call shall
7041 not intervene between the lvalue-to-rvalue conversion and the
7042 side effect associated with any single compound assignment
7043 operator. -- end note ] */
7044 lhs = stabilize_reference (lhs);
7045 if (TREE_SIDE_EFFECTS (rhs))
7046 rhs = mark_rvalue_use (rhs);
7047 rhs = stabilize_expr (rhs, &init);
7048 newrhs = cp_build_binary_op (input_location,
7049 modifycode, lhs, rhs,
7050 complain);
7051 if (newrhs == error_mark_node)
7053 if (complain & tf_error)
7054 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7055 TREE_TYPE (lhs), TREE_TYPE (rhs));
7056 return error_mark_node;
7059 if (init)
7060 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7062 /* Now it looks like a plain assignment. */
7063 modifycode = NOP_EXPR;
7064 if (c_dialect_objc ())
7066 result = objc_maybe_build_modify_expr (lhs, newrhs);
7067 if (result)
7068 return result;
7071 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7072 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7075 /* The left-hand side must be an lvalue. */
7076 if (!lvalue_or_else (lhs, lv_assign, complain))
7077 return error_mark_node;
7079 /* Warn about modifying something that is `const'. Don't warn if
7080 this is initialization. */
7081 if (modifycode != INIT_EXPR
7082 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7083 /* Functions are not modifiable, even though they are
7084 lvalues. */
7085 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7086 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7087 /* If it's an aggregate and any field is const, then it is
7088 effectively const. */
7089 || (CLASS_TYPE_P (lhstype)
7090 && C_TYPE_FIELDS_READONLY (lhstype))))
7092 if (complain & tf_error)
7093 cxx_readonly_error (lhs, lv_assign);
7094 else
7095 return error_mark_node;
7098 /* If storing into a structure or union member, it may have been given a
7099 lowered bitfield type. We need to convert to the declared type first,
7100 so retrieve it now. */
7102 olhstype = unlowered_expr_type (lhs);
7104 /* Convert new value to destination type. */
7106 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7108 int from_array;
7110 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7112 if (modifycode != INIT_EXPR)
7114 if (complain & tf_error)
7115 error ("assigning to an array from an initializer list");
7116 return error_mark_node;
7118 if (check_array_initializer (lhs, lhstype, newrhs))
7119 return error_mark_node;
7120 newrhs = digest_init (lhstype, newrhs, complain);
7121 if (newrhs == error_mark_node)
7122 return error_mark_node;
7125 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7126 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7128 if (complain & tf_error)
7129 error ("incompatible types in assignment of %qT to %qT",
7130 TREE_TYPE (rhs), lhstype);
7131 return error_mark_node;
7134 /* Allow array assignment in compiler-generated code. */
7135 else if (!current_function_decl
7136 || !DECL_DEFAULTED_FN (current_function_decl))
7138 /* This routine is used for both initialization and assignment.
7139 Make sure the diagnostic message differentiates the context. */
7140 if (complain & tf_error)
7142 if (modifycode == INIT_EXPR)
7143 error ("array used as initializer");
7144 else
7145 error ("invalid array assignment");
7147 return error_mark_node;
7150 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7151 ? 1 + (modifycode != INIT_EXPR): 0;
7152 return build_vec_init (lhs, NULL_TREE, newrhs,
7153 /*explicit_value_init_p=*/false,
7154 from_array, complain);
7157 if (modifycode == INIT_EXPR)
7158 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7159 LOOKUP_ONLYCONVERTING. */
7160 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7161 ICR_INIT, NULL_TREE, 0,
7162 complain);
7163 else
7164 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7165 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7167 if (!same_type_p (lhstype, olhstype))
7168 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7170 if (modifycode != INIT_EXPR)
7172 if (TREE_CODE (newrhs) == CALL_EXPR
7173 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7174 newrhs = build_cplus_new (lhstype, newrhs, complain);
7176 /* Can't initialize directly from a TARGET_EXPR, since that would
7177 cause the lhs to be constructed twice, and possibly result in
7178 accidental self-initialization. So we force the TARGET_EXPR to be
7179 expanded without a target. */
7180 if (TREE_CODE (newrhs) == TARGET_EXPR)
7181 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7182 TREE_OPERAND (newrhs, 0));
7185 if (newrhs == error_mark_node)
7186 return error_mark_node;
7188 if (c_dialect_objc () && flag_objc_gc)
7190 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7192 if (result)
7193 return result;
7196 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7197 lhstype, lhs, newrhs);
7199 TREE_SIDE_EFFECTS (result) = 1;
7200 if (!plain_assign)
7201 TREE_NO_WARNING (result) = 1;
7203 return result;
7206 tree
7207 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7208 tree rhs, tsubst_flags_t complain)
7210 if (processing_template_decl)
7211 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7212 build_min_nt_loc (loc, modifycode, NULL_TREE,
7213 NULL_TREE), rhs);
7215 if (modifycode != NOP_EXPR)
7217 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7218 make_node (modifycode), /*overload=*/NULL,
7219 complain);
7220 if (rval)
7222 TREE_NO_WARNING (rval) = 1;
7223 return rval;
7226 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7229 /* Helper function for get_delta_difference which assumes FROM is a base
7230 class of TO. Returns a delta for the conversion of pointer-to-member
7231 of FROM to pointer-to-member of TO. If the conversion is invalid and
7232 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7233 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7234 If C_CAST_P is true, this conversion is taking place as part of a
7235 C-style cast. */
7237 static tree
7238 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7239 tsubst_flags_t complain)
7241 tree binfo;
7242 base_kind kind;
7244 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7245 &kind, complain);
7247 if (binfo == error_mark_node)
7249 if (!(complain & tf_error))
7250 return error_mark_node;
7252 error (" in pointer to member function conversion");
7253 return size_zero_node;
7255 else if (binfo)
7257 if (kind != bk_via_virtual)
7258 return BINFO_OFFSET (binfo);
7259 else
7260 /* FROM is a virtual base class of TO. Issue an error or warning
7261 depending on whether or not this is a reinterpret cast. */
7263 if (!(complain & tf_error))
7264 return error_mark_node;
7266 error ("pointer to member conversion via virtual base %qT",
7267 BINFO_TYPE (binfo_from_vbase (binfo)));
7269 return size_zero_node;
7272 else
7273 return NULL_TREE;
7276 /* Get difference in deltas for different pointer to member function
7277 types. If the conversion is invalid and tf_error is not set in
7278 COMPLAIN, returns error_mark_node, otherwise returns an integer
7279 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7280 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7281 conversions as well. If C_CAST_P is true this conversion is taking
7282 place as part of a C-style cast.
7284 Note that the naming of FROM and TO is kind of backwards; the return
7285 value is what we add to a TO in order to get a FROM. They are named
7286 this way because we call this function to find out how to convert from
7287 a pointer to member of FROM to a pointer to member of TO. */
7289 static tree
7290 get_delta_difference (tree from, tree to,
7291 bool allow_inverse_p,
7292 bool c_cast_p, tsubst_flags_t complain)
7294 tree result;
7296 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7297 /* Pointer to member of incomplete class is permitted*/
7298 result = size_zero_node;
7299 else
7300 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7302 if (result == error_mark_node)
7303 return error_mark_node;
7305 if (!result)
7307 if (!allow_inverse_p)
7309 if (!(complain & tf_error))
7310 return error_mark_node;
7312 error_not_base_type (from, to);
7313 error (" in pointer to member conversion");
7314 result = size_zero_node;
7316 else
7318 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7320 if (result == error_mark_node)
7321 return error_mark_node;
7323 if (result)
7324 result = size_diffop_loc (input_location,
7325 size_zero_node, result);
7326 else
7328 if (!(complain & tf_error))
7329 return error_mark_node;
7331 error_not_base_type (from, to);
7332 error (" in pointer to member conversion");
7333 result = size_zero_node;
7338 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7339 result));
7342 /* Return a constructor for the pointer-to-member-function TYPE using
7343 the other components as specified. */
7345 tree
7346 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7348 tree u = NULL_TREE;
7349 tree delta_field;
7350 tree pfn_field;
7351 VEC(constructor_elt, gc) *v;
7353 /* Pull the FIELD_DECLs out of the type. */
7354 pfn_field = TYPE_FIELDS (type);
7355 delta_field = DECL_CHAIN (pfn_field);
7357 /* Make sure DELTA has the type we want. */
7358 delta = convert_and_check (delta_type_node, delta);
7360 /* Convert to the correct target type if necessary. */
7361 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7363 /* Finish creating the initializer. */
7364 v = VEC_alloc(constructor_elt, gc, 2);
7365 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7366 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7367 u = build_constructor (type, v);
7368 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7369 TREE_STATIC (u) = (TREE_CONSTANT (u)
7370 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7371 != NULL_TREE)
7372 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7373 != NULL_TREE));
7374 return u;
7377 /* Build a constructor for a pointer to member function. It can be
7378 used to initialize global variables, local variable, or used
7379 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7380 want to be.
7382 If FORCE is nonzero, then force this conversion, even if
7383 we would rather not do it. Usually set when using an explicit
7384 cast. A C-style cast is being processed iff C_CAST_P is true.
7386 Return error_mark_node, if something goes wrong. */
7388 tree
7389 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7390 tsubst_flags_t complain)
7392 tree fn;
7393 tree pfn_type;
7394 tree to_type;
7396 if (error_operand_p (pfn))
7397 return error_mark_node;
7399 pfn_type = TREE_TYPE (pfn);
7400 to_type = build_ptrmemfunc_type (type);
7402 /* Handle multiple conversions of pointer to member functions. */
7403 if (TYPE_PTRMEMFUNC_P (pfn_type))
7405 tree delta = NULL_TREE;
7406 tree npfn = NULL_TREE;
7407 tree n;
7409 if (!force
7410 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7411 LOOKUP_NORMAL, complain))
7412 error ("invalid conversion to type %qT from type %qT",
7413 to_type, pfn_type);
7415 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7416 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7417 force,
7418 c_cast_p, complain);
7419 if (n == error_mark_node)
7420 return error_mark_node;
7422 /* We don't have to do any conversion to convert a
7423 pointer-to-member to its own type. But, we don't want to
7424 just return a PTRMEM_CST if there's an explicit cast; that
7425 cast should make the expression an invalid template argument. */
7426 if (TREE_CODE (pfn) != PTRMEM_CST)
7428 if (same_type_p (to_type, pfn_type))
7429 return pfn;
7430 else if (integer_zerop (n))
7431 return build_reinterpret_cast (to_type, pfn,
7432 complain);
7435 if (TREE_SIDE_EFFECTS (pfn))
7436 pfn = save_expr (pfn);
7438 /* Obtain the function pointer and the current DELTA. */
7439 if (TREE_CODE (pfn) == PTRMEM_CST)
7440 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7441 else
7443 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7444 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7447 /* Just adjust the DELTA field. */
7448 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7449 (TREE_TYPE (delta), ptrdiff_type_node));
7450 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7451 n = cp_build_binary_op (input_location,
7452 LSHIFT_EXPR, n, integer_one_node,
7453 complain);
7454 delta = cp_build_binary_op (input_location,
7455 PLUS_EXPR, delta, n, complain);
7456 return build_ptrmemfunc1 (to_type, delta, npfn);
7459 /* Handle null pointer to member function conversions. */
7460 if (null_ptr_cst_p (pfn))
7462 pfn = build_c_cast (input_location, type, nullptr_node);
7463 return build_ptrmemfunc1 (to_type,
7464 integer_zero_node,
7465 pfn);
7468 if (type_unknown_p (pfn))
7469 return instantiate_type (type, pfn, complain);
7471 fn = TREE_OPERAND (pfn, 0);
7472 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7473 /* In a template, we will have preserved the
7474 OFFSET_REF. */
7475 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7476 return make_ptrmem_cst (to_type, fn);
7479 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7480 given by CST.
7482 ??? There is no consistency as to the types returned for the above
7483 values. Some code acts as if it were a sizetype and some as if it were
7484 integer_type_node. */
7486 void
7487 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7489 tree type = TREE_TYPE (cst);
7490 tree fn = PTRMEM_CST_MEMBER (cst);
7491 tree ptr_class, fn_class;
7493 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7495 /* The class that the function belongs to. */
7496 fn_class = DECL_CONTEXT (fn);
7498 /* The class that we're creating a pointer to member of. */
7499 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7501 /* First, calculate the adjustment to the function's class. */
7502 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7503 /*c_cast_p=*/0, tf_warning_or_error);
7505 if (!DECL_VIRTUAL_P (fn))
7506 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7507 build_addr_func (fn, tf_warning_or_error));
7508 else
7510 /* If we're dealing with a virtual function, we have to adjust 'this'
7511 again, to point to the base which provides the vtable entry for
7512 fn; the call will do the opposite adjustment. */
7513 tree orig_class = DECL_CONTEXT (fn);
7514 tree binfo = binfo_or_else (orig_class, fn_class);
7515 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7516 *delta, BINFO_OFFSET (binfo));
7517 *delta = fold_if_not_in_template (*delta);
7519 /* We set PFN to the vtable offset at which the function can be
7520 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7521 case delta is shifted left, and then incremented). */
7522 *pfn = DECL_VINDEX (fn);
7523 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7524 TYPE_SIZE_UNIT (vtable_entry_type));
7525 *pfn = fold_if_not_in_template (*pfn);
7527 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7529 case ptrmemfunc_vbit_in_pfn:
7530 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7531 integer_one_node);
7532 *pfn = fold_if_not_in_template (*pfn);
7533 break;
7535 case ptrmemfunc_vbit_in_delta:
7536 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7537 *delta, integer_one_node);
7538 *delta = fold_if_not_in_template (*delta);
7539 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7540 *delta, integer_one_node);
7541 *delta = fold_if_not_in_template (*delta);
7542 break;
7544 default:
7545 gcc_unreachable ();
7548 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7549 *pfn = fold_if_not_in_template (*pfn);
7553 /* Return an expression for PFN from the pointer-to-member function
7554 given by T. */
7556 static tree
7557 pfn_from_ptrmemfunc (tree t)
7559 if (TREE_CODE (t) == PTRMEM_CST)
7561 tree delta;
7562 tree pfn;
7564 expand_ptrmemfunc_cst (t, &delta, &pfn);
7565 if (pfn)
7566 return pfn;
7569 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7572 /* Return an expression for DELTA from the pointer-to-member function
7573 given by T. */
7575 static tree
7576 delta_from_ptrmemfunc (tree t)
7578 if (TREE_CODE (t) == PTRMEM_CST)
7580 tree delta;
7581 tree pfn;
7583 expand_ptrmemfunc_cst (t, &delta, &pfn);
7584 if (delta)
7585 return delta;
7588 return build_ptrmemfunc_access_expr (t, delta_identifier);
7591 /* Convert value RHS to type TYPE as preparation for an assignment to
7592 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7593 implicit conversion is. If FNDECL is non-NULL, we are doing the
7594 conversion in order to pass the PARMNUMth argument of FNDECL.
7595 If FNDECL is NULL, we are doing the conversion in function pointer
7596 argument passing, conversion in initialization, etc. */
7598 static tree
7599 convert_for_assignment (tree type, tree rhs,
7600 impl_conv_rhs errtype, tree fndecl, int parmnum,
7601 tsubst_flags_t complain, int flags)
7603 tree rhstype;
7604 enum tree_code coder;
7606 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7607 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7608 rhs = TREE_OPERAND (rhs, 0);
7610 rhstype = TREE_TYPE (rhs);
7611 coder = TREE_CODE (rhstype);
7613 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7614 && vector_types_convertible_p (type, rhstype, true))
7616 rhs = mark_rvalue_use (rhs);
7617 return convert (type, rhs);
7620 if (rhs == error_mark_node || rhstype == error_mark_node)
7621 return error_mark_node;
7622 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7623 return error_mark_node;
7625 /* The RHS of an assignment cannot have void type. */
7626 if (coder == VOID_TYPE)
7628 if (complain & tf_error)
7629 error ("void value not ignored as it ought to be");
7630 return error_mark_node;
7633 if (c_dialect_objc ())
7635 int parmno;
7636 tree selector;
7637 tree rname = fndecl;
7639 switch (errtype)
7641 case ICR_ASSIGN:
7642 parmno = -1;
7643 break;
7644 case ICR_INIT:
7645 parmno = -2;
7646 break;
7647 default:
7648 selector = objc_message_selector ();
7649 parmno = parmnum;
7650 if (selector && parmno > 1)
7652 rname = selector;
7653 parmno -= 1;
7657 if (objc_compare_types (type, rhstype, parmno, rname))
7659 rhs = mark_rvalue_use (rhs);
7660 return convert (type, rhs);
7664 /* [expr.ass]
7666 The expression is implicitly converted (clause _conv_) to the
7667 cv-unqualified type of the left operand.
7669 We allow bad conversions here because by the time we get to this point
7670 we are committed to doing the conversion. If we end up doing a bad
7671 conversion, convert_like will complain. */
7672 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
7674 /* When -Wno-pmf-conversions is use, we just silently allow
7675 conversions from pointers-to-members to plain pointers. If
7676 the conversion doesn't work, cp_convert will complain. */
7677 if (!warn_pmf2ptr
7678 && TYPE_PTR_P (type)
7679 && TYPE_PTRMEMFUNC_P (rhstype))
7680 rhs = cp_convert (strip_top_quals (type), rhs, complain);
7681 else
7683 if (complain & tf_error)
7685 /* If the right-hand side has unknown type, then it is an
7686 overloaded function. Call instantiate_type to get error
7687 messages. */
7688 if (rhstype == unknown_type_node)
7689 instantiate_type (type, rhs, tf_warning_or_error);
7690 else if (fndecl)
7691 error ("cannot convert %qT to %qT for argument %qP to %qD",
7692 rhstype, type, parmnum, fndecl);
7693 else
7694 switch (errtype)
7696 case ICR_DEFAULT_ARGUMENT:
7697 error ("cannot convert %qT to %qT in default argument",
7698 rhstype, type);
7699 break;
7700 case ICR_ARGPASS:
7701 error ("cannot convert %qT to %qT in argument passing",
7702 rhstype, type);
7703 break;
7704 case ICR_CONVERTING:
7705 error ("cannot convert %qT to %qT",
7706 rhstype, type);
7707 break;
7708 case ICR_INIT:
7709 error ("cannot convert %qT to %qT in initialization",
7710 rhstype, type);
7711 break;
7712 case ICR_RETURN:
7713 error ("cannot convert %qT to %qT in return",
7714 rhstype, type);
7715 break;
7716 case ICR_ASSIGN:
7717 error ("cannot convert %qT to %qT in assignment",
7718 rhstype, type);
7719 break;
7720 default:
7721 gcc_unreachable();
7724 return error_mark_node;
7727 if (warn_suggest_attribute_format)
7729 const enum tree_code codel = TREE_CODE (type);
7730 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7731 && coder == codel
7732 && check_missing_format_attribute (type, rhstype)
7733 && (complain & tf_warning))
7734 switch (errtype)
7736 case ICR_ARGPASS:
7737 case ICR_DEFAULT_ARGUMENT:
7738 if (fndecl)
7739 warning (OPT_Wsuggest_attribute_format,
7740 "parameter %qP of %qD might be a candidate "
7741 "for a format attribute", parmnum, fndecl);
7742 else
7743 warning (OPT_Wsuggest_attribute_format,
7744 "parameter might be a candidate "
7745 "for a format attribute");
7746 break;
7747 case ICR_CONVERTING:
7748 warning (OPT_Wsuggest_attribute_format,
7749 "target of conversion might be a candidate "
7750 "for a format attribute");
7751 break;
7752 case ICR_INIT:
7753 warning (OPT_Wsuggest_attribute_format,
7754 "target of initialization might be a candidate "
7755 "for a format attribute");
7756 break;
7757 case ICR_RETURN:
7758 warning (OPT_Wsuggest_attribute_format,
7759 "return type might be a candidate "
7760 "for a format attribute");
7761 break;
7762 case ICR_ASSIGN:
7763 warning (OPT_Wsuggest_attribute_format,
7764 "left-hand side of assignment might be a candidate "
7765 "for a format attribute");
7766 break;
7767 default:
7768 gcc_unreachable();
7772 /* If -Wparentheses, warn about a = b = c when a has type bool and b
7773 does not. */
7774 if (warn_parentheses
7775 && TREE_CODE (type) == BOOLEAN_TYPE
7776 && TREE_CODE (rhs) == MODIFY_EXPR
7777 && !TREE_NO_WARNING (rhs)
7778 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7779 && (complain & tf_warning))
7781 location_t loc = EXPR_LOC_OR_HERE (rhs);
7783 warning_at (loc, OPT_Wparentheses,
7784 "suggest parentheses around assignment used as truth value");
7785 TREE_NO_WARNING (rhs) = 1;
7788 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
7789 complain, flags);
7792 /* Convert RHS to be of type TYPE.
7793 If EXP is nonzero, it is the target of the initialization.
7794 ERRTYPE indicates what kind of error the implicit conversion is.
7796 Two major differences between the behavior of
7797 `convert_for_assignment' and `convert_for_initialization'
7798 are that references are bashed in the former, while
7799 copied in the latter, and aggregates are assigned in
7800 the former (operator=) while initialized in the
7801 latter (X(X&)).
7803 If using constructor make sure no conversion operator exists, if one does
7804 exist, an ambiguity exists. */
7806 tree
7807 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
7808 impl_conv_rhs errtype, tree fndecl, int parmnum,
7809 tsubst_flags_t complain)
7811 enum tree_code codel = TREE_CODE (type);
7812 tree rhstype;
7813 enum tree_code coder;
7815 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7816 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7817 if (TREE_CODE (rhs) == NOP_EXPR
7818 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7819 && codel != REFERENCE_TYPE)
7820 rhs = TREE_OPERAND (rhs, 0);
7822 if (type == error_mark_node
7823 || rhs == error_mark_node
7824 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7825 return error_mark_node;
7827 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7828 && TREE_CODE (type) != ARRAY_TYPE
7829 && (TREE_CODE (type) != REFERENCE_TYPE
7830 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7831 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7832 && (TREE_CODE (type) != REFERENCE_TYPE
7833 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7834 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7835 rhs = decay_conversion (rhs, complain);
7837 rhstype = TREE_TYPE (rhs);
7838 coder = TREE_CODE (rhstype);
7840 if (coder == ERROR_MARK)
7841 return error_mark_node;
7843 /* We accept references to incomplete types, so we can
7844 return here before checking if RHS is of complete type. */
7846 if (codel == REFERENCE_TYPE)
7848 /* This should eventually happen in convert_arguments. */
7849 int savew = 0, savee = 0;
7851 if (fndecl)
7852 savew = warningcount, savee = errorcount;
7853 rhs = initialize_reference (type, rhs, flags, complain);
7854 if (fndecl)
7856 if (warningcount > savew)
7857 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
7858 else if (errorcount > savee)
7859 error ("in passing argument %P of %q+D", parmnum, fndecl);
7861 return rhs;
7864 if (exp != 0)
7865 exp = require_complete_type_sfinae (exp, complain);
7866 if (exp == error_mark_node)
7867 return error_mark_node;
7869 rhstype = non_reference (rhstype);
7871 type = complete_type (type);
7873 if (DIRECT_INIT_EXPR_P (type, rhs))
7874 /* Don't try to do copy-initialization if we already have
7875 direct-initialization. */
7876 return rhs;
7878 if (MAYBE_CLASS_TYPE_P (type))
7879 return perform_implicit_conversion_flags (type, rhs, complain, flags);
7881 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
7882 complain, flags);
7885 /* If RETVAL is the address of, or a reference to, a local variable or
7886 temporary give an appropriate warning. */
7888 static void
7889 maybe_warn_about_returning_address_of_local (tree retval)
7891 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
7892 tree whats_returned = retval;
7894 for (;;)
7896 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7897 whats_returned = TREE_OPERAND (whats_returned, 1);
7898 else if (CONVERT_EXPR_P (whats_returned)
7899 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
7900 whats_returned = TREE_OPERAND (whats_returned, 0);
7901 else
7902 break;
7905 if (TREE_CODE (whats_returned) != ADDR_EXPR)
7906 return;
7907 whats_returned = TREE_OPERAND (whats_returned, 0);
7909 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7911 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7912 || TREE_CODE (whats_returned) == TARGET_EXPR)
7914 warning (0, "returning reference to temporary");
7915 return;
7917 if (TREE_CODE (whats_returned) == VAR_DECL
7918 && DECL_NAME (whats_returned)
7919 && TEMP_NAME_P (DECL_NAME (whats_returned)))
7921 warning (0, "reference to non-lvalue returned");
7922 return;
7926 while (TREE_CODE (whats_returned) == COMPONENT_REF
7927 || TREE_CODE (whats_returned) == ARRAY_REF)
7928 whats_returned = TREE_OPERAND (whats_returned, 0);
7930 if (DECL_P (whats_returned)
7931 && DECL_NAME (whats_returned)
7932 && DECL_FUNCTION_SCOPE_P (whats_returned)
7933 && !(TREE_STATIC (whats_returned)
7934 || TREE_PUBLIC (whats_returned)))
7936 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7937 warning (0, "reference to local variable %q+D returned",
7938 whats_returned);
7939 else
7940 warning (0, "address of local variable %q+D returned",
7941 whats_returned);
7942 return;
7946 /* Check that returning RETVAL from the current function is valid.
7947 Return an expression explicitly showing all conversions required to
7948 change RETVAL into the function return type, and to assign it to
7949 the DECL_RESULT for the function. Set *NO_WARNING to true if
7950 code reaches end of non-void function warning shouldn't be issued
7951 on this RETURN_EXPR. */
7953 tree
7954 check_return_expr (tree retval, bool *no_warning)
7956 tree result;
7957 /* The type actually returned by the function. */
7958 tree valtype;
7959 /* The type the function is declared to return, or void if
7960 the declared type is incomplete. */
7961 tree functype;
7962 int fn_returns_value_p;
7963 bool named_return_value_okay_p;
7965 *no_warning = false;
7967 /* A `volatile' function is one that isn't supposed to return, ever.
7968 (This is a G++ extension, used to get better code for functions
7969 that call the `volatile' function.) */
7970 if (TREE_THIS_VOLATILE (current_function_decl))
7971 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7973 /* Check for various simple errors. */
7974 if (DECL_DESTRUCTOR_P (current_function_decl))
7976 if (retval)
7977 error ("returning a value from a destructor");
7978 return NULL_TREE;
7980 else if (DECL_CONSTRUCTOR_P (current_function_decl))
7982 if (in_function_try_handler)
7983 /* If a return statement appears in a handler of the
7984 function-try-block of a constructor, the program is ill-formed. */
7985 error ("cannot return from a handler of a function-try-block of a constructor");
7986 else if (retval)
7987 /* You can't return a value from a constructor. */
7988 error ("returning a value from a constructor");
7989 return NULL_TREE;
7992 if (processing_template_decl)
7994 current_function_returns_value = 1;
7995 if (check_for_bare_parameter_packs (retval))
7996 retval = error_mark_node;
7997 return retval;
8000 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8002 /* Deduce auto return type from a return statement. */
8003 if (current_function_auto_return_pattern)
8005 tree auto_node;
8006 tree type;
8008 if (!retval && !is_auto (current_function_auto_return_pattern))
8010 /* Give a helpful error message. */
8011 error ("return-statement with no value, in function returning %qT",
8012 current_function_auto_return_pattern);
8013 inform (input_location, "only plain %<auto%> return type can be "
8014 "deduced to %<void%>");
8015 type = error_mark_node;
8017 else
8019 if (!retval)
8020 retval = void_zero_node;
8021 auto_node = type_uses_auto (current_function_auto_return_pattern);
8022 type = do_auto_deduction (current_function_auto_return_pattern,
8023 retval, auto_node);
8026 if (type == error_mark_node)
8027 /* Leave it. */;
8028 else if (functype == current_function_auto_return_pattern)
8029 apply_deduced_return_type (current_function_decl, type);
8030 else
8031 /* A mismatch should have been diagnosed in do_auto_deduction. */
8032 gcc_assert (same_type_p (type, functype));
8033 functype = type;
8036 /* When no explicit return-value is given in a function with a named
8037 return value, the named return value is used. */
8038 result = DECL_RESULT (current_function_decl);
8039 valtype = TREE_TYPE (result);
8040 gcc_assert (valtype != NULL_TREE);
8041 fn_returns_value_p = !VOID_TYPE_P (valtype);
8042 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8043 retval = result;
8045 /* Check for a return statement with no return value in a function
8046 that's supposed to return a value. */
8047 if (!retval && fn_returns_value_p)
8049 if (functype != error_mark_node)
8050 permerror (input_location, "return-statement with no value, in "
8051 "function returning %qT", valtype);
8052 /* Remember that this function did return. */
8053 current_function_returns_value = 1;
8054 /* And signal caller that TREE_NO_WARNING should be set on the
8055 RETURN_EXPR to avoid control reaches end of non-void function
8056 warnings in tree-cfg.c. */
8057 *no_warning = true;
8059 /* Check for a return statement with a value in a function that
8060 isn't supposed to return a value. */
8061 else if (retval && !fn_returns_value_p)
8063 if (VOID_TYPE_P (TREE_TYPE (retval)))
8064 /* You can return a `void' value from a function of `void'
8065 type. In that case, we have to evaluate the expression for
8066 its side-effects. */
8067 finish_expr_stmt (retval);
8068 else
8069 permerror (input_location, "return-statement with a value, in function "
8070 "returning 'void'");
8071 current_function_returns_null = 1;
8073 /* There's really no value to return, after all. */
8074 return NULL_TREE;
8076 else if (!retval)
8077 /* Remember that this function can sometimes return without a
8078 value. */
8079 current_function_returns_null = 1;
8080 else
8081 /* Remember that this function did return a value. */
8082 current_function_returns_value = 1;
8084 /* Check for erroneous operands -- but after giving ourselves a
8085 chance to provide an error about returning a value from a void
8086 function. */
8087 if (error_operand_p (retval))
8089 current_function_return_value = error_mark_node;
8090 return error_mark_node;
8093 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8094 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8095 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8096 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8097 && ! flag_check_new
8098 && retval && null_ptr_cst_p (retval))
8099 warning (0, "%<operator new%> must not return NULL unless it is "
8100 "declared %<throw()%> (or -fcheck-new is in effect)");
8102 /* Effective C++ rule 15. See also start_function. */
8103 if (warn_ecpp
8104 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8106 bool warn = true;
8108 /* The function return type must be a reference to the current
8109 class. */
8110 if (TREE_CODE (valtype) == REFERENCE_TYPE
8111 && same_type_ignoring_top_level_qualifiers_p
8112 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8114 /* Returning '*this' is obviously OK. */
8115 if (retval == current_class_ref)
8116 warn = false;
8117 /* If we are calling a function whose return type is the same of
8118 the current class reference, it is ok. */
8119 else if (TREE_CODE (retval) == INDIRECT_REF
8120 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8121 warn = false;
8124 if (warn)
8125 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8128 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8130 [...] For a function with a class return type, if the expression
8131 in the return statement is the name of a local object, and the cv-
8132 unqualified type of the local object is the same as the function
8133 return type, an implementation is permitted to omit creating the tem-
8134 porary object to hold the function return value [...]
8136 So, if this is a value-returning function that always returns the same
8137 local variable, remember it.
8139 It might be nice to be more flexible, and choose the first suitable
8140 variable even if the function sometimes returns something else, but
8141 then we run the risk of clobbering the variable we chose if the other
8142 returned expression uses the chosen variable somehow. And people expect
8143 this restriction, anyway. (jason 2000-11-19)
8145 See finish_function and finalize_nrv for the rest of this optimization. */
8147 named_return_value_okay_p =
8148 (retval != NULL_TREE
8149 /* Must be a local, automatic variable. */
8150 && TREE_CODE (retval) == VAR_DECL
8151 && DECL_CONTEXT (retval) == current_function_decl
8152 && ! TREE_STATIC (retval)
8153 && ! DECL_ANON_UNION_VAR_P (retval)
8154 && (DECL_ALIGN (retval) >= DECL_ALIGN (result))
8155 /* The cv-unqualified type of the returned value must be the
8156 same as the cv-unqualified return type of the
8157 function. */
8158 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8159 (TYPE_MAIN_VARIANT (functype)))
8160 /* And the returned value must be non-volatile. */
8161 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8163 if (fn_returns_value_p && flag_elide_constructors)
8165 if (named_return_value_okay_p
8166 && (current_function_return_value == NULL_TREE
8167 || current_function_return_value == retval))
8168 current_function_return_value = retval;
8169 else
8170 current_function_return_value = error_mark_node;
8173 /* We don't need to do any conversions when there's nothing being
8174 returned. */
8175 if (!retval)
8176 return NULL_TREE;
8178 /* Do any required conversions. */
8179 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8180 /* No conversions are required. */
8182 else
8184 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8186 /* The functype's return type will have been set to void, if it
8187 was an incomplete type. Just treat this as 'return;' */
8188 if (VOID_TYPE_P (functype))
8189 return error_mark_node;
8191 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8192 treated as an rvalue for the purposes of overload resolution to
8193 favor move constructors over copy constructors.
8195 Note that these conditions are similar to, but not as strict as,
8196 the conditions for the named return value optimization. */
8197 if ((cxx_dialect != cxx98)
8198 && (TREE_CODE (retval) == VAR_DECL
8199 || TREE_CODE (retval) == PARM_DECL)
8200 && DECL_CONTEXT (retval) == current_function_decl
8201 && !TREE_STATIC (retval)
8202 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8203 (TYPE_MAIN_VARIANT (functype)))
8204 /* This is only interesting for class type. */
8205 && CLASS_TYPE_P (functype))
8206 flags = flags | LOOKUP_PREFER_RVALUE;
8208 /* First convert the value to the function's return type, then
8209 to the type of return value's location to handle the
8210 case that functype is smaller than the valtype. */
8211 retval = convert_for_initialization
8212 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8213 tf_warning_or_error);
8214 retval = convert (valtype, retval);
8216 /* If the conversion failed, treat this just like `return;'. */
8217 if (retval == error_mark_node)
8218 return retval;
8219 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8220 else if (! cfun->returns_struct
8221 && TREE_CODE (retval) == TARGET_EXPR
8222 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8223 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8224 TREE_OPERAND (retval, 0));
8225 else
8226 maybe_warn_about_returning_address_of_local (retval);
8229 /* Actually copy the value returned into the appropriate location. */
8230 if (retval && retval != result)
8231 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8233 return retval;
8237 /* Returns nonzero if the pointer-type FROM can be converted to the
8238 pointer-type TO via a qualification conversion. If CONSTP is -1,
8239 then we return nonzero if the pointers are similar, and the
8240 cv-qualification signature of FROM is a proper subset of that of TO.
8242 If CONSTP is positive, then all outer pointers have been
8243 const-qualified. */
8245 static int
8246 comp_ptr_ttypes_real (tree to, tree from, int constp)
8248 bool to_more_cv_qualified = false;
8249 bool is_opaque_pointer = false;
8251 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8253 if (TREE_CODE (to) != TREE_CODE (from))
8254 return 0;
8256 if (TREE_CODE (from) == OFFSET_TYPE
8257 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8258 TYPE_OFFSET_BASETYPE (to)))
8259 return 0;
8261 /* Const and volatile mean something different for function types,
8262 so the usual checks are not appropriate. */
8263 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8265 if (!at_least_as_qualified_p (to, from))
8266 return 0;
8268 if (!at_least_as_qualified_p (from, to))
8270 if (constp == 0)
8271 return 0;
8272 to_more_cv_qualified = true;
8275 if (constp > 0)
8276 constp &= TYPE_READONLY (to);
8279 if (TREE_CODE (to) == VECTOR_TYPE)
8280 is_opaque_pointer = vector_targets_convertible_p (to, from);
8282 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRDATAMEM_P (to))
8283 return ((constp >= 0 || to_more_cv_qualified)
8284 && (is_opaque_pointer
8285 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8289 /* When comparing, say, char ** to char const **, this function takes
8290 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8291 types to this function. */
8294 comp_ptr_ttypes (tree to, tree from)
8296 return comp_ptr_ttypes_real (to, from, 1);
8299 /* Returns true iff FNTYPE is a non-class type that involves
8300 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8301 if a parameter type is ill-formed. */
8303 bool
8304 error_type_p (const_tree type)
8306 tree t;
8308 switch (TREE_CODE (type))
8310 case ERROR_MARK:
8311 return true;
8313 case POINTER_TYPE:
8314 case REFERENCE_TYPE:
8315 case OFFSET_TYPE:
8316 return error_type_p (TREE_TYPE (type));
8318 case FUNCTION_TYPE:
8319 case METHOD_TYPE:
8320 if (error_type_p (TREE_TYPE (type)))
8321 return true;
8322 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8323 if (error_type_p (TREE_VALUE (t)))
8324 return true;
8325 return false;
8327 case RECORD_TYPE:
8328 if (TYPE_PTRMEMFUNC_P (type))
8329 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8330 return false;
8332 default:
8333 return false;
8337 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8338 type or inheritance-related types, regardless of cv-quals. */
8341 ptr_reasonably_similar (const_tree to, const_tree from)
8343 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8345 /* Any target type is similar enough to void. */
8346 if (TREE_CODE (to) == VOID_TYPE)
8347 return !error_type_p (from);
8348 if (TREE_CODE (from) == VOID_TYPE)
8349 return !error_type_p (to);
8351 if (TREE_CODE (to) != TREE_CODE (from))
8352 return 0;
8354 if (TREE_CODE (from) == OFFSET_TYPE
8355 && comptypes (TYPE_OFFSET_BASETYPE (to),
8356 TYPE_OFFSET_BASETYPE (from),
8357 COMPARE_BASE | COMPARE_DERIVED))
8358 continue;
8360 if (TREE_CODE (to) == VECTOR_TYPE
8361 && vector_types_convertible_p (to, from, false))
8362 return 1;
8364 if (TREE_CODE (to) == INTEGER_TYPE
8365 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8366 return 1;
8368 if (TREE_CODE (to) == FUNCTION_TYPE)
8369 return !error_type_p (to) && !error_type_p (from);
8371 if (TREE_CODE (to) != POINTER_TYPE)
8372 return comptypes
8373 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8374 COMPARE_BASE | COMPARE_DERIVED);
8378 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8379 pointer-to-member types) are the same, ignoring cv-qualification at
8380 all levels. */
8382 bool
8383 comp_ptr_ttypes_const (tree to, tree from)
8385 bool is_opaque_pointer = false;
8387 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8389 if (TREE_CODE (to) != TREE_CODE (from))
8390 return false;
8392 if (TREE_CODE (from) == OFFSET_TYPE
8393 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8394 TYPE_OFFSET_BASETYPE (to)))
8395 continue;
8397 if (TREE_CODE (to) == VECTOR_TYPE)
8398 is_opaque_pointer = vector_targets_convertible_p (to, from);
8400 if (TREE_CODE (to) != POINTER_TYPE)
8401 return (is_opaque_pointer
8402 || same_type_ignoring_top_level_qualifiers_p (to, from));
8406 /* Returns the type qualifiers for this type, including the qualifiers on the
8407 elements for an array type. */
8410 cp_type_quals (const_tree type)
8412 int quals;
8413 /* This CONST_CAST is okay because strip_array_types returns its
8414 argument unmodified and we assign it to a const_tree. */
8415 type = strip_array_types (CONST_CAST_TREE (type));
8416 if (type == error_mark_node
8417 /* Quals on a FUNCTION_TYPE are memfn quals. */
8418 || TREE_CODE (type) == FUNCTION_TYPE)
8419 return TYPE_UNQUALIFIED;
8420 quals = TYPE_QUALS (type);
8421 /* METHOD and REFERENCE_TYPEs should never have quals. */
8422 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8423 && TREE_CODE (type) != REFERENCE_TYPE)
8424 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8425 == TYPE_UNQUALIFIED));
8426 return quals;
8429 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8430 METHOD_TYPE. */
8433 type_memfn_quals (const_tree type)
8435 if (TREE_CODE (type) == FUNCTION_TYPE)
8436 return TYPE_QUALS (type);
8437 else if (TREE_CODE (type) == METHOD_TYPE)
8438 return cp_type_quals (class_of_this_parm (type));
8439 else
8440 gcc_unreachable ();
8443 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8444 MEMFN_QUALS. */
8446 tree
8447 apply_memfn_quals (tree type, cp_cv_quals memfn_quals)
8449 /* Could handle METHOD_TYPE here if necessary. */
8450 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8451 if (TYPE_QUALS (type) == memfn_quals)
8452 return type;
8453 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8454 complex. */
8455 return build_qualified_type (type, memfn_quals);
8458 /* Returns nonzero if TYPE is const or volatile. */
8460 bool
8461 cv_qualified_p (const_tree type)
8463 int quals = cp_type_quals (type);
8464 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8467 /* Returns nonzero if the TYPE contains a mutable member. */
8469 bool
8470 cp_has_mutable_p (const_tree type)
8472 /* This CONST_CAST is okay because strip_array_types returns its
8473 argument unmodified and we assign it to a const_tree. */
8474 type = strip_array_types (CONST_CAST_TREE(type));
8476 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8479 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8480 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8481 approximation. In particular, consider:
8483 int f();
8484 struct S { int i; };
8485 const S s = { f(); }
8487 Here, we will make "s" as TREE_READONLY (because it is declared
8488 "const") -- only to reverse ourselves upon seeing that the
8489 initializer is non-constant. */
8491 void
8492 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8494 tree type = TREE_TYPE (decl);
8496 if (type == error_mark_node)
8497 return;
8499 if (TREE_CODE (decl) == TYPE_DECL)
8500 return;
8502 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8503 && type_quals != TYPE_UNQUALIFIED));
8505 /* Avoid setting TREE_READONLY incorrectly. */
8506 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8507 constructor can produce constant init, so rely on cp_finish_decl to
8508 clear TREE_READONLY if the variable has non-constant init. */
8510 /* If the type has (or might have) a mutable component, that component
8511 might be modified. */
8512 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8513 type_quals &= ~TYPE_QUAL_CONST;
8515 c_apply_type_quals_to_decl (type_quals, decl);
8518 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8519 exemplar types such that casting T1 to T2 is casting away constness
8520 if and only if there is no implicit conversion from T1 to T2. */
8522 static void
8523 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8525 int quals1;
8526 int quals2;
8528 /* [expr.const.cast]
8530 For multi-level pointer to members and multi-level mixed pointers
8531 and pointers to members (conv.qual), the "member" aspect of a
8532 pointer to member level is ignored when determining if a const
8533 cv-qualifier has been cast away. */
8534 /* [expr.const.cast]
8536 For two pointer types:
8538 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8539 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8540 K is min(N,M)
8542 casting from X1 to X2 casts away constness if, for a non-pointer
8543 type T there does not exist an implicit conversion (clause
8544 _conv_) from:
8546 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8550 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8551 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8552 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8554 *t1 = cp_build_qualified_type (void_type_node,
8555 cp_type_quals (*t1));
8556 *t2 = cp_build_qualified_type (void_type_node,
8557 cp_type_quals (*t2));
8558 return;
8561 quals1 = cp_type_quals (*t1);
8562 quals2 = cp_type_quals (*t2);
8564 if (TYPE_PTRDATAMEM_P (*t1))
8565 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8566 else
8567 *t1 = TREE_TYPE (*t1);
8568 if (TYPE_PTRDATAMEM_P (*t2))
8569 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8570 else
8571 *t2 = TREE_TYPE (*t2);
8573 casts_away_constness_r (t1, t2, complain);
8574 *t1 = build_pointer_type (*t1);
8575 *t2 = build_pointer_type (*t2);
8576 *t1 = cp_build_qualified_type (*t1, quals1);
8577 *t2 = cp_build_qualified_type (*t2, quals2);
8580 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8581 constness.
8583 ??? This function returns non-zero if casting away qualifiers not
8584 just const. We would like to return to the caller exactly which
8585 qualifiers are casted away to give more accurate diagnostics.
8588 static bool
8589 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8591 if (TREE_CODE (t2) == REFERENCE_TYPE)
8593 /* [expr.const.cast]
8595 Casting from an lvalue of type T1 to an lvalue of type T2
8596 using a reference cast casts away constness if a cast from an
8597 rvalue of type "pointer to T1" to the type "pointer to T2"
8598 casts away constness. */
8599 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8600 return casts_away_constness (build_pointer_type (t1),
8601 build_pointer_type (TREE_TYPE (t2)),
8602 complain);
8605 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
8606 /* [expr.const.cast]
8608 Casting from an rvalue of type "pointer to data member of X
8609 of type T1" to the type "pointer to data member of Y of type
8610 T2" casts away constness if a cast from an rvalue of type
8611 "pointer to T1" to the type "pointer to T2" casts away
8612 constness. */
8613 return casts_away_constness
8614 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8615 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
8616 complain);
8618 /* Casting away constness is only something that makes sense for
8619 pointer or reference types. */
8620 if (TREE_CODE (t1) != POINTER_TYPE
8621 || TREE_CODE (t2) != POINTER_TYPE)
8622 return false;
8624 /* Top-level qualifiers don't matter. */
8625 t1 = TYPE_MAIN_VARIANT (t1);
8626 t2 = TYPE_MAIN_VARIANT (t2);
8627 casts_away_constness_r (&t1, &t2, complain);
8628 if (!can_convert (t2, t1, complain))
8629 return true;
8631 return false;
8634 /* If T is a REFERENCE_TYPE return the type to which T refers.
8635 Otherwise, return T itself. */
8637 tree
8638 non_reference (tree t)
8640 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8641 t = TREE_TYPE (t);
8642 return t;
8646 /* Return nonzero if REF is an lvalue valid for this language;
8647 otherwise, print an error message and return zero. USE says
8648 how the lvalue is being used and so selects the error message. */
8651 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8653 cp_lvalue_kind kind = lvalue_kind (ref);
8655 if (kind == clk_none)
8657 if (complain & tf_error)
8658 lvalue_error (input_location, use);
8659 return 0;
8661 else if (kind & (clk_rvalueref|clk_class))
8663 if (!(complain & tf_error))
8664 return 0;
8665 if (kind & clk_class)
8666 /* Make this a permerror because we used to accept it. */
8667 permerror (input_location, "using temporary as lvalue");
8668 else
8669 error ("using xvalue (rvalue reference) as lvalue");
8671 return 1;
8674 /* Return true if a user-defined literal operator is a raw operator. */
8676 bool
8677 check_raw_literal_operator (const_tree decl)
8679 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8680 tree argtype;
8681 int arity;
8682 bool maybe_raw_p = false;
8684 /* Count the number and type of arguments and check for ellipsis. */
8685 for (argtype = argtypes, arity = 0;
8686 argtype && argtype != void_list_node;
8687 ++arity, argtype = TREE_CHAIN (argtype))
8689 tree t = TREE_VALUE (argtype);
8691 if (same_type_p (t, const_string_type_node))
8692 maybe_raw_p = true;
8694 if (!argtype)
8695 return false; /* Found ellipsis. */
8697 if (!maybe_raw_p || arity != 1)
8698 return false;
8700 return true;
8704 /* Return true if a user-defined literal operator has one of the allowed
8705 argument types. */
8707 bool
8708 check_literal_operator_args (const_tree decl,
8709 bool *long_long_unsigned_p, bool *long_double_p)
8711 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8713 *long_long_unsigned_p = false;
8714 *long_double_p = false;
8715 if (processing_template_decl || processing_specialization)
8716 return argtypes == void_list_node;
8717 else
8719 tree argtype;
8720 int arity;
8721 int max_arity = 2;
8723 /* Count the number and type of arguments and check for ellipsis. */
8724 for (argtype = argtypes, arity = 0;
8725 argtype && argtype != void_list_node;
8726 argtype = TREE_CHAIN (argtype))
8728 tree t = TREE_VALUE (argtype);
8729 ++arity;
8731 if (TREE_CODE (t) == POINTER_TYPE)
8733 bool maybe_raw_p = false;
8734 t = TREE_TYPE (t);
8735 if (cp_type_quals (t) != TYPE_QUAL_CONST)
8736 return false;
8737 t = TYPE_MAIN_VARIANT (t);
8738 if ((maybe_raw_p = same_type_p (t, char_type_node))
8739 || same_type_p (t, wchar_type_node)
8740 || same_type_p (t, char16_type_node)
8741 || same_type_p (t, char32_type_node))
8743 argtype = TREE_CHAIN (argtype);
8744 if (!argtype)
8745 return false;
8746 t = TREE_VALUE (argtype);
8747 if (maybe_raw_p && argtype == void_list_node)
8748 return true;
8749 else if (same_type_p (t, size_type_node))
8751 ++arity;
8752 continue;
8754 else
8755 return false;
8758 else if (same_type_p (t, long_long_unsigned_type_node))
8760 max_arity = 1;
8761 *long_long_unsigned_p = true;
8763 else if (same_type_p (t, long_double_type_node))
8765 max_arity = 1;
8766 *long_double_p = true;
8768 else if (same_type_p (t, char_type_node))
8769 max_arity = 1;
8770 else if (same_type_p (t, wchar_type_node))
8771 max_arity = 1;
8772 else if (same_type_p (t, char16_type_node))
8773 max_arity = 1;
8774 else if (same_type_p (t, char32_type_node))
8775 max_arity = 1;
8776 else
8777 return false;
8779 if (!argtype)
8780 return false; /* Found ellipsis. */
8782 if (arity != max_arity)
8783 return false;
8785 return true;