ChangeLog entry:
[official-gcc.git] / gcc / cp / typeck.c
blobb59741c6471023a4b31b7aa7980a245e0fd02bb6
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 "output.h"
38 #include "diagnostic.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "convert.h"
42 #include "c-family/c-common.h"
43 #include "c-family/c-objc.h"
44 #include "params.h"
46 static tree pfn_from_ptrmemfunc (tree);
47 static tree delta_from_ptrmemfunc (tree);
48 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 tsubst_flags_t, int);
50 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
51 static tree rationalize_conditional_expr (enum tree_code, tree,
52 tsubst_flags_t);
53 static int comp_ptr_ttypes_real (tree, tree, int);
54 static bool comp_except_types (tree, tree, bool);
55 static bool comp_array_types (const_tree, const_tree, bool);
56 static tree pointer_diff (tree, tree, tree);
57 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
58 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
59 static bool casts_away_constness (tree, tree, tsubst_flags_t);
60 static void maybe_warn_about_returning_address_of_local (tree);
61 static tree lookup_destructor (tree, tree, tree);
62 static void warn_args_num (location_t, tree, bool);
63 static int convert_arguments (tree, VEC(tree,gc) **, tree, int,
64 tsubst_flags_t);
66 /* Do `exp = require_complete_type (exp);' to make sure exp
67 does not have an incomplete type. (That includes void types.)
68 Returns error_mark_node if the VALUE does not have
69 complete type when this function returns. */
71 tree
72 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
74 tree type;
76 if (processing_template_decl || value == error_mark_node)
77 return value;
79 if (TREE_CODE (value) == OVERLOAD)
80 type = unknown_type_node;
81 else
82 type = TREE_TYPE (value);
84 if (type == error_mark_node)
85 return error_mark_node;
87 /* First, detect a valid value with a complete type. */
88 if (COMPLETE_TYPE_P (type))
89 return value;
91 if (complete_type_or_maybe_complain (type, value, complain))
92 return value;
93 else
94 return error_mark_node;
97 tree
98 require_complete_type (tree value)
100 return require_complete_type_sfinae (value, tf_warning_or_error);
103 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
104 a template instantiation, do the instantiation. Returns TYPE,
105 whether or not it could be completed, unless something goes
106 horribly wrong, in which case the error_mark_node is returned. */
108 tree
109 complete_type (tree type)
111 if (type == NULL_TREE)
112 /* Rather than crash, we return something sure to cause an error
113 at some point. */
114 return error_mark_node;
116 if (type == error_mark_node || COMPLETE_TYPE_P (type))
118 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
120 tree t = complete_type (TREE_TYPE (type));
121 unsigned int needs_constructing, has_nontrivial_dtor;
122 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
123 layout_type (type);
124 needs_constructing
125 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
126 has_nontrivial_dtor
127 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
128 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
130 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
131 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
134 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
135 instantiate_class_template (TYPE_MAIN_VARIANT (type));
137 return type;
140 /* Like complete_type, but issue an error if the TYPE cannot be completed.
141 VALUE is used for informative diagnostics.
142 Returns NULL_TREE if the type cannot be made complete. */
144 tree
145 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
147 type = complete_type (type);
148 if (type == error_mark_node)
149 /* We already issued an error. */
150 return NULL_TREE;
151 else if (!COMPLETE_TYPE_P (type))
153 if (complain & tf_error)
154 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
155 return NULL_TREE;
157 else
158 return type;
161 tree
162 complete_type_or_else (tree type, tree value)
164 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
167 /* Return truthvalue of whether type of EXP is instantiated. */
170 type_unknown_p (const_tree exp)
172 return (TREE_CODE (exp) == TREE_LIST
173 || TREE_TYPE (exp) == unknown_type_node);
177 /* Return the common type of two parameter lists.
178 We assume that comptypes has already been done and returned 1;
179 if that isn't so, this may crash.
181 As an optimization, free the space we allocate if the parameter
182 lists are already common. */
184 static tree
185 commonparms (tree p1, tree p2)
187 tree oldargs = p1, newargs, n;
188 int i, len;
189 int any_change = 0;
191 len = list_length (p1);
192 newargs = tree_last (p1);
194 if (newargs == void_list_node)
195 i = 1;
196 else
198 i = 0;
199 newargs = 0;
202 for (; i < len; i++)
203 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
205 n = newargs;
207 for (i = 0; p1;
208 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
210 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
212 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
213 any_change = 1;
215 else if (! TREE_PURPOSE (p1))
217 if (TREE_PURPOSE (p2))
219 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
220 any_change = 1;
223 else
225 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
226 any_change = 1;
227 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
229 if (TREE_VALUE (p1) != TREE_VALUE (p2))
231 any_change = 1;
232 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
234 else
235 TREE_VALUE (n) = TREE_VALUE (p1);
237 if (! any_change)
238 return oldargs;
240 return newargs;
243 /* Given a type, perhaps copied for a typedef,
244 find the "original" version of it. */
245 static tree
246 original_type (tree t)
248 int quals = cp_type_quals (t);
249 while (t != error_mark_node
250 && TYPE_NAME (t) != NULL_TREE)
252 tree x = TYPE_NAME (t);
253 if (TREE_CODE (x) != TYPE_DECL)
254 break;
255 x = DECL_ORIGINAL_TYPE (x);
256 if (x == NULL_TREE)
257 break;
258 t = x;
260 return cp_build_qualified_type (t, quals);
263 /* Return the common type for two arithmetic types T1 and T2 under the
264 usual arithmetic conversions. The default conversions have already
265 been applied, and enumerated types converted to their compatible
266 integer types. */
268 static tree
269 cp_common_type (tree t1, tree t2)
271 enum tree_code code1 = TREE_CODE (t1);
272 enum tree_code code2 = TREE_CODE (t2);
273 tree attributes;
276 /* In what follows, we slightly generalize the rules given in [expr] so
277 as to deal with `long long' and `complex'. First, merge the
278 attributes. */
279 attributes = (*targetm.merge_type_attributes) (t1, t2);
281 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
283 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
284 return build_type_attribute_variant (t1, attributes);
285 else
286 return NULL_TREE;
289 /* FIXME: Attributes. */
290 gcc_assert (ARITHMETIC_TYPE_P (t1)
291 || TREE_CODE (t1) == VECTOR_TYPE
292 || UNSCOPED_ENUM_P (t1));
293 gcc_assert (ARITHMETIC_TYPE_P (t2)
294 || TREE_CODE (t2) == VECTOR_TYPE
295 || UNSCOPED_ENUM_P (t2));
297 /* If one type is complex, form the common type of the non-complex
298 components, then make that complex. Use T1 or T2 if it is the
299 required type. */
300 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
302 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
303 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
304 tree subtype
305 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
307 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
308 return build_type_attribute_variant (t1, attributes);
309 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
310 return build_type_attribute_variant (t2, attributes);
311 else
312 return build_type_attribute_variant (build_complex_type (subtype),
313 attributes);
316 if (code1 == VECTOR_TYPE)
318 /* When we get here we should have two vectors of the same size.
319 Just prefer the unsigned one if present. */
320 if (TYPE_UNSIGNED (t1))
321 return build_type_attribute_variant (t1, attributes);
322 else
323 return build_type_attribute_variant (t2, attributes);
326 /* If only one is real, use it as the result. */
327 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
328 return build_type_attribute_variant (t1, attributes);
329 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
330 return build_type_attribute_variant (t2, attributes);
332 /* Both real or both integers; use the one with greater precision. */
333 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
334 return build_type_attribute_variant (t1, attributes);
335 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
336 return build_type_attribute_variant (t2, attributes);
338 /* The types are the same; no need to do anything fancy. */
339 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
340 return build_type_attribute_variant (t1, attributes);
342 if (code1 != REAL_TYPE)
344 /* If one is unsigned long long, then convert the other to unsigned
345 long long. */
346 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
347 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
348 return build_type_attribute_variant (long_long_unsigned_type_node,
349 attributes);
350 /* If one is a long long, and the other is an unsigned long, and
351 long long can represent all the values of an unsigned long, then
352 convert to a long long. Otherwise, convert to an unsigned long
353 long. Otherwise, if either operand is long long, convert the
354 other to long long.
356 Since we're here, we know the TYPE_PRECISION is the same;
357 therefore converting to long long cannot represent all the values
358 of an unsigned long, so we choose unsigned long long in that
359 case. */
360 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
361 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
363 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
364 ? long_long_unsigned_type_node
365 : long_long_integer_type_node);
366 return build_type_attribute_variant (t, attributes);
368 if (int128_integer_type_node != NULL_TREE
369 && (same_type_p (TYPE_MAIN_VARIANT (t1),
370 int128_integer_type_node)
371 || same_type_p (TYPE_MAIN_VARIANT (t2),
372 int128_integer_type_node)))
374 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
375 ? int128_unsigned_type_node
376 : int128_integer_type_node);
377 return build_type_attribute_variant (t, attributes);
380 /* Go through the same procedure, but for longs. */
381 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
382 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
383 return build_type_attribute_variant (long_unsigned_type_node,
384 attributes);
385 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
386 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
388 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
389 ? long_unsigned_type_node : long_integer_type_node);
390 return build_type_attribute_variant (t, attributes);
392 /* Otherwise prefer the unsigned one. */
393 if (TYPE_UNSIGNED (t1))
394 return build_type_attribute_variant (t1, attributes);
395 else
396 return build_type_attribute_variant (t2, attributes);
398 else
400 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
401 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
402 return build_type_attribute_variant (long_double_type_node,
403 attributes);
404 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
405 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
406 return build_type_attribute_variant (double_type_node,
407 attributes);
408 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
409 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
410 return build_type_attribute_variant (float_type_node,
411 attributes);
413 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
414 the standard C++ floating-point types. Logic earlier in this
415 function has already eliminated the possibility that
416 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
417 compelling reason to choose one or the other. */
418 return build_type_attribute_variant (t1, attributes);
422 /* T1 and T2 are arithmetic or enumeration types. Return the type
423 that will result from the "usual arithmetic conversions" on T1 and
424 T2 as described in [expr]. */
426 tree
427 type_after_usual_arithmetic_conversions (tree t1, tree t2)
429 gcc_assert (ARITHMETIC_TYPE_P (t1)
430 || TREE_CODE (t1) == VECTOR_TYPE
431 || UNSCOPED_ENUM_P (t1));
432 gcc_assert (ARITHMETIC_TYPE_P (t2)
433 || TREE_CODE (t2) == VECTOR_TYPE
434 || UNSCOPED_ENUM_P (t2));
436 /* Perform the integral promotions. We do not promote real types here. */
437 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
438 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
440 t1 = type_promotes_to (t1);
441 t2 = type_promotes_to (t2);
444 return cp_common_type (t1, t2);
447 static void
448 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
449 composite_pointer_operation operation)
451 switch (operation)
453 case CPO_COMPARISON:
454 emit_diagnostic (kind, input_location, 0,
455 "comparison between "
456 "distinct pointer types %qT and %qT lacks a cast",
457 t1, t2);
458 break;
459 case CPO_CONVERSION:
460 emit_diagnostic (kind, input_location, 0,
461 "conversion between "
462 "distinct pointer types %qT and %qT lacks a cast",
463 t1, t2);
464 break;
465 case CPO_CONDITIONAL_EXPR:
466 emit_diagnostic (kind, input_location, 0,
467 "conditional expression between "
468 "distinct pointer types %qT and %qT lacks a cast",
469 t1, t2);
470 break;
471 default:
472 gcc_unreachable ();
476 /* Subroutine of composite_pointer_type to implement the recursive
477 case. See that function for documentation of the parameters. */
479 static tree
480 composite_pointer_type_r (tree t1, tree t2,
481 composite_pointer_operation operation,
482 tsubst_flags_t complain)
484 tree pointee1;
485 tree pointee2;
486 tree result_type;
487 tree attributes;
489 /* Determine the types pointed to by T1 and T2. */
490 if (TREE_CODE (t1) == POINTER_TYPE)
492 pointee1 = TREE_TYPE (t1);
493 pointee2 = TREE_TYPE (t2);
495 else
497 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
498 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
501 /* [expr.rel]
503 Otherwise, the composite pointer type is a pointer type
504 similar (_conv.qual_) to the type of one of the operands,
505 with a cv-qualification signature (_conv.qual_) that is the
506 union of the cv-qualification signatures of the operand
507 types. */
508 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
509 result_type = pointee1;
510 else if ((TREE_CODE (pointee1) == POINTER_TYPE
511 && TREE_CODE (pointee2) == POINTER_TYPE)
512 || (TYPE_PTR_TO_MEMBER_P (pointee1)
513 && TYPE_PTR_TO_MEMBER_P (pointee2)))
515 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
516 complain);
517 if (result_type == error_mark_node)
518 return error_mark_node;
520 else
522 if (complain & tf_error)
523 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
524 else
525 return error_mark_node;
526 result_type = void_type_node;
528 result_type = cp_build_qualified_type (result_type,
529 (cp_type_quals (pointee1)
530 | cp_type_quals (pointee2)));
531 /* If the original types were pointers to members, so is the
532 result. */
533 if (TYPE_PTR_TO_MEMBER_P (t1))
535 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
536 TYPE_PTRMEM_CLASS_TYPE (t2)))
538 if (complain & tf_error)
539 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
540 else
541 return error_mark_node;
543 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
544 result_type);
546 else
547 result_type = build_pointer_type (result_type);
549 /* Merge the attributes. */
550 attributes = (*targetm.merge_type_attributes) (t1, t2);
551 return build_type_attribute_variant (result_type, attributes);
554 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
555 ARG1 and ARG2 are the values with those types. The OPERATION is to
556 describe the operation between the pointer types,
557 in case an error occurs.
559 This routine also implements the computation of a common type for
560 pointers-to-members as per [expr.eq]. */
562 tree
563 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
564 composite_pointer_operation operation,
565 tsubst_flags_t complain)
567 tree class1;
568 tree class2;
570 /* [expr.rel]
572 If one operand is a null pointer constant, the composite pointer
573 type is the type of the other operand. */
574 if (null_ptr_cst_p (arg1))
575 return t2;
576 if (null_ptr_cst_p (arg2))
577 return t1;
579 /* We have:
581 [expr.rel]
583 If one of the operands has type "pointer to cv1 void*", then
584 the other has type "pointer to cv2T", and the composite pointer
585 type is "pointer to cv12 void", where cv12 is the union of cv1
586 and cv2.
588 If either type is a pointer to void, make sure it is T1. */
589 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
591 tree t;
592 t = t1;
593 t1 = t2;
594 t2 = t;
597 /* Now, if T1 is a pointer to void, merge the qualifiers. */
598 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
600 tree attributes;
601 tree result_type;
603 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
605 switch (operation)
607 case CPO_COMPARISON:
608 pedwarn (input_location, OPT_Wpedantic,
609 "ISO C++ forbids comparison between "
610 "pointer of type %<void *%> and pointer-to-function");
611 break;
612 case CPO_CONVERSION:
613 pedwarn (input_location, OPT_Wpedantic,
614 "ISO C++ forbids conversion between "
615 "pointer of type %<void *%> and pointer-to-function");
616 break;
617 case CPO_CONDITIONAL_EXPR:
618 pedwarn (input_location, OPT_Wpedantic,
619 "ISO C++ forbids conditional expression between "
620 "pointer of type %<void *%> and pointer-to-function");
621 break;
622 default:
623 gcc_unreachable ();
626 result_type
627 = cp_build_qualified_type (void_type_node,
628 (cp_type_quals (TREE_TYPE (t1))
629 | cp_type_quals (TREE_TYPE (t2))));
630 result_type = build_pointer_type (result_type);
631 /* Merge the attributes. */
632 attributes = (*targetm.merge_type_attributes) (t1, t2);
633 return build_type_attribute_variant (result_type, attributes);
636 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
637 && TREE_CODE (t2) == POINTER_TYPE)
639 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
640 return objc_common_type (t1, t2);
643 /* [expr.eq] permits the application of a pointer conversion to
644 bring the pointers to a common type. */
645 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
646 && CLASS_TYPE_P (TREE_TYPE (t1))
647 && CLASS_TYPE_P (TREE_TYPE (t2))
648 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
649 TREE_TYPE (t2)))
651 class1 = TREE_TYPE (t1);
652 class2 = TREE_TYPE (t2);
654 if (DERIVED_FROM_P (class1, class2))
655 t2 = (build_pointer_type
656 (cp_build_qualified_type (class1, cp_type_quals (class2))));
657 else if (DERIVED_FROM_P (class2, class1))
658 t1 = (build_pointer_type
659 (cp_build_qualified_type (class2, cp_type_quals (class1))));
660 else
662 if (complain & tf_error)
663 composite_pointer_error (DK_ERROR, t1, t2, operation);
664 return error_mark_node;
667 /* [expr.eq] permits the application of a pointer-to-member
668 conversion to change the class type of one of the types. */
669 else if (TYPE_PTR_TO_MEMBER_P (t1)
670 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
671 TYPE_PTRMEM_CLASS_TYPE (t2)))
673 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
674 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
676 if (DERIVED_FROM_P (class1, class2))
677 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
678 else if (DERIVED_FROM_P (class2, class1))
679 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
680 else
682 if (complain & tf_error)
683 switch (operation)
685 case CPO_COMPARISON:
686 error ("comparison between distinct "
687 "pointer-to-member types %qT and %qT lacks a cast",
688 t1, t2);
689 break;
690 case CPO_CONVERSION:
691 error ("conversion between distinct "
692 "pointer-to-member types %qT and %qT lacks a cast",
693 t1, t2);
694 break;
695 case CPO_CONDITIONAL_EXPR:
696 error ("conditional expression between distinct "
697 "pointer-to-member types %qT and %qT lacks a cast",
698 t1, t2);
699 break;
700 default:
701 gcc_unreachable ();
703 return error_mark_node;
707 return composite_pointer_type_r (t1, t2, operation, complain);
710 /* Return the merged type of two types.
711 We assume that comptypes has already been done and returned 1;
712 if that isn't so, this may crash.
714 This just combines attributes and default arguments; any other
715 differences would cause the two types to compare unalike. */
717 tree
718 merge_types (tree t1, tree t2)
720 enum tree_code code1;
721 enum tree_code code2;
722 tree attributes;
724 /* Save time if the two types are the same. */
725 if (t1 == t2)
726 return t1;
727 if (original_type (t1) == original_type (t2))
728 return t1;
730 /* If one type is nonsense, use the other. */
731 if (t1 == error_mark_node)
732 return t2;
733 if (t2 == error_mark_node)
734 return t1;
736 /* Handle merging an auto redeclaration with a previous deduced
737 return type. */
738 if (is_auto (t1))
739 return t2;
741 /* Merge the attributes. */
742 attributes = (*targetm.merge_type_attributes) (t1, t2);
744 if (TYPE_PTRMEMFUNC_P (t1))
745 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
746 if (TYPE_PTRMEMFUNC_P (t2))
747 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
749 code1 = TREE_CODE (t1);
750 code2 = TREE_CODE (t2);
751 if (code1 != code2)
753 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
754 if (code1 == TYPENAME_TYPE)
756 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
757 code1 = TREE_CODE (t1);
759 else
761 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
762 code2 = TREE_CODE (t2);
766 switch (code1)
768 case POINTER_TYPE:
769 case REFERENCE_TYPE:
770 /* For two pointers, do this recursively on the target type. */
772 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
773 int quals = cp_type_quals (t1);
775 if (code1 == POINTER_TYPE)
776 t1 = build_pointer_type (target);
777 else
778 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
779 t1 = build_type_attribute_variant (t1, attributes);
780 t1 = cp_build_qualified_type (t1, quals);
782 if (TREE_CODE (target) == METHOD_TYPE)
783 t1 = build_ptrmemfunc_type (t1);
785 return t1;
788 case OFFSET_TYPE:
790 int quals;
791 tree pointee;
792 quals = cp_type_quals (t1);
793 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
794 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
795 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
796 pointee);
797 t1 = cp_build_qualified_type (t1, quals);
798 break;
801 case ARRAY_TYPE:
803 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
804 /* Save space: see if the result is identical to one of the args. */
805 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
806 return build_type_attribute_variant (t1, attributes);
807 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
808 return build_type_attribute_variant (t2, attributes);
809 /* Merge the element types, and have a size if either arg has one. */
810 t1 = build_cplus_array_type
811 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
812 break;
815 case FUNCTION_TYPE:
816 /* Function types: prefer the one that specified arg types.
817 If both do, merge the arg types. Also merge the return types. */
819 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
820 tree p1 = TYPE_ARG_TYPES (t1);
821 tree p2 = TYPE_ARG_TYPES (t2);
822 tree parms;
823 tree rval, raises;
825 /* Save space: see if the result is identical to one of the args. */
826 if (valtype == TREE_TYPE (t1) && ! p2)
827 return cp_build_type_attribute_variant (t1, attributes);
828 if (valtype == TREE_TYPE (t2) && ! p1)
829 return cp_build_type_attribute_variant (t2, attributes);
831 /* Simple way if one arg fails to specify argument types. */
832 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
833 parms = p2;
834 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
835 parms = p1;
836 else
837 parms = commonparms (p1, p2);
839 rval = build_function_type (valtype, parms);
840 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
841 rval = apply_memfn_quals (rval, type_memfn_quals (t1));
842 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
843 TYPE_RAISES_EXCEPTIONS (t2),
844 NULL_TREE);
845 t1 = build_exception_variant (rval, raises);
846 break;
849 case METHOD_TYPE:
851 /* Get this value the long way, since TYPE_METHOD_BASETYPE
852 is just the main variant of this. */
853 tree basetype = class_of_this_parm (t2);
854 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
855 TYPE_RAISES_EXCEPTIONS (t2),
856 NULL_TREE);
857 tree t3;
859 /* If this was a member function type, get back to the
860 original type of type member function (i.e., without
861 the class instance variable up front. */
862 t1 = build_function_type (TREE_TYPE (t1),
863 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
864 t2 = build_function_type (TREE_TYPE (t2),
865 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
866 t3 = merge_types (t1, t2);
867 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
868 TYPE_ARG_TYPES (t3));
869 t1 = build_exception_variant (t3, raises);
870 break;
873 case TYPENAME_TYPE:
874 /* There is no need to merge attributes into a TYPENAME_TYPE.
875 When the type is instantiated it will have whatever
876 attributes result from the instantiation. */
877 return t1;
879 default:;
882 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
883 return t1;
884 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
885 return t2;
886 else
887 return cp_build_type_attribute_variant (t1, attributes);
890 /* Return the ARRAY_TYPE type without its domain. */
892 tree
893 strip_array_domain (tree type)
895 tree t2;
896 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
897 if (TYPE_DOMAIN (type) == NULL_TREE)
898 return type;
899 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
900 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
903 /* Wrapper around cp_common_type that is used by c-common.c and other
904 front end optimizations that remove promotions.
906 Return the common type for two arithmetic types T1 and T2 under the
907 usual arithmetic conversions. The default conversions have already
908 been applied, and enumerated types converted to their compatible
909 integer types. */
911 tree
912 common_type (tree t1, tree t2)
914 /* If one type is nonsense, use the other */
915 if (t1 == error_mark_node)
916 return t2;
917 if (t2 == error_mark_node)
918 return t1;
920 return cp_common_type (t1, t2);
923 /* Return the common type of two pointer types T1 and T2. This is the
924 type for the result of most arithmetic operations if the operands
925 have the given two types.
927 We assume that comp_target_types has already been done and returned
928 nonzero; if that isn't so, this may crash. */
930 tree
931 common_pointer_type (tree t1, tree t2)
933 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
934 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
935 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
937 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
938 CPO_CONVERSION, tf_warning_or_error);
941 /* Compare two exception specifier types for exactness or subsetness, if
942 allowed. Returns false for mismatch, true for match (same, or
943 derived and !exact).
945 [except.spec] "If a class X ... objects of class X or any class publicly
946 and unambiguously derived from X. Similarly, if a pointer type Y * ...
947 exceptions of type Y * or that are pointers to any type publicly and
948 unambiguously derived from Y. Otherwise a function only allows exceptions
949 that have the same type ..."
950 This does not mention cv qualifiers and is different to what throw
951 [except.throw] and catch [except.catch] will do. They will ignore the
952 top level cv qualifiers, and allow qualifiers in the pointer to class
953 example.
955 We implement the letter of the standard. */
957 static bool
958 comp_except_types (tree a, tree b, bool exact)
960 if (same_type_p (a, b))
961 return true;
962 else if (!exact)
964 if (cp_type_quals (a) || cp_type_quals (b))
965 return false;
967 if (TREE_CODE (a) == POINTER_TYPE
968 && TREE_CODE (b) == POINTER_TYPE)
970 a = TREE_TYPE (a);
971 b = TREE_TYPE (b);
972 if (cp_type_quals (a) || cp_type_quals (b))
973 return false;
976 if (TREE_CODE (a) != RECORD_TYPE
977 || TREE_CODE (b) != RECORD_TYPE)
978 return false;
980 if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
981 return true;
983 return false;
986 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
987 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
988 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
989 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
990 are unordered, but we've already filtered out duplicates. Most lists will
991 be in order, we should try to make use of that. */
993 bool
994 comp_except_specs (const_tree t1, const_tree t2, int exact)
996 const_tree probe;
997 const_tree base;
998 int length = 0;
1000 if (t1 == t2)
1001 return true;
1003 /* First handle noexcept. */
1004 if (exact < ce_exact)
1006 /* noexcept(false) is compatible with no exception-specification,
1007 and stricter than any spec. */
1008 if (t1 == noexcept_false_spec)
1009 return t2 == NULL_TREE || exact == ce_derived;
1010 /* Even a derived noexcept(false) is compatible with no
1011 exception-specification. */
1012 if (t2 == noexcept_false_spec)
1013 return t1 == NULL_TREE;
1015 /* Otherwise, if we aren't looking for an exact match, noexcept is
1016 equivalent to throw(). */
1017 if (t1 == noexcept_true_spec)
1018 t1 = empty_except_spec;
1019 if (t2 == noexcept_true_spec)
1020 t2 = empty_except_spec;
1023 /* If any noexcept is left, it is only comparable to itself;
1024 either we're looking for an exact match or we're redeclaring a
1025 template with dependent noexcept. */
1026 if ((t1 && TREE_PURPOSE (t1))
1027 || (t2 && TREE_PURPOSE (t2)))
1028 return (t1 && t2
1029 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1031 if (t1 == NULL_TREE) /* T1 is ... */
1032 return t2 == NULL_TREE || exact == ce_derived;
1033 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1034 return t2 != NULL_TREE && !TREE_VALUE (t2);
1035 if (t2 == NULL_TREE) /* T2 is ... */
1036 return false;
1037 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1038 return exact == ce_derived;
1040 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1041 Count how many we find, to determine exactness. For exact matching and
1042 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1043 O(nm). */
1044 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1046 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1048 tree a = TREE_VALUE (probe);
1049 tree b = TREE_VALUE (t2);
1051 if (comp_except_types (a, b, exact))
1053 if (probe == base && exact > ce_derived)
1054 base = TREE_CHAIN (probe);
1055 length++;
1056 break;
1059 if (probe == NULL_TREE)
1060 return false;
1062 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1065 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1066 [] can match [size]. */
1068 static bool
1069 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1071 tree d1;
1072 tree d2;
1073 tree max1, max2;
1075 if (t1 == t2)
1076 return true;
1078 /* The type of the array elements must be the same. */
1079 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1080 return false;
1082 d1 = TYPE_DOMAIN (t1);
1083 d2 = TYPE_DOMAIN (t2);
1085 if (d1 == d2)
1086 return true;
1088 /* If one of the arrays is dimensionless, and the other has a
1089 dimension, they are of different types. However, it is valid to
1090 write:
1092 extern int a[];
1093 int a[3];
1095 by [basic.link]:
1097 declarations for an array object can specify
1098 array types that differ by the presence or absence of a major
1099 array bound (_dcl.array_). */
1100 if (!d1 || !d2)
1101 return allow_redeclaration;
1103 /* Check that the dimensions are the same. */
1105 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1106 return false;
1107 max1 = TYPE_MAX_VALUE (d1);
1108 max2 = TYPE_MAX_VALUE (d2);
1109 if (processing_template_decl && !abi_version_at_least (2)
1110 && !value_dependent_expression_p (max1)
1111 && !value_dependent_expression_p (max2))
1113 /* With abi-1 we do not fold non-dependent array bounds, (and
1114 consequently mangle them incorrectly). We must therefore
1115 fold them here, to verify the domains have the same
1116 value. */
1117 max1 = fold (max1);
1118 max2 = fold (max2);
1121 if (!cp_tree_equal (max1, max2))
1122 return false;
1124 return true;
1127 /* Compare the relative position of T1 and T2 into their respective
1128 template parameter list.
1129 T1 and T2 must be template parameter types.
1130 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1132 static bool
1133 comp_template_parms_position (tree t1, tree t2)
1135 tree index1, index2;
1136 gcc_assert (t1 && t2
1137 && TREE_CODE (t1) == TREE_CODE (t2)
1138 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1139 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1140 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1142 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1143 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1145 /* If T1 and T2 belong to template parm lists of different size,
1146 let's assume they are different. */
1147 if (TEMPLATE_PARM_NUM_SIBLINGS (index1)
1148 != TEMPLATE_PARM_NUM_SIBLINGS (index2))
1149 return false;
1151 /* Then compare their relative position. */
1152 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1153 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1154 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1155 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1156 return false;
1158 return true;
1161 /* Subroutine in comptypes. */
1163 static bool
1164 structural_comptypes (tree t1, tree t2, int strict)
1166 if (t1 == t2)
1167 return true;
1169 /* Suppress errors caused by previously reported errors. */
1170 if (t1 == error_mark_node || t2 == error_mark_node)
1171 return false;
1173 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1175 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1176 current instantiation. */
1177 if (TREE_CODE (t1) == TYPENAME_TYPE)
1178 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1180 if (TREE_CODE (t2) == TYPENAME_TYPE)
1181 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1183 if (TYPE_PTRMEMFUNC_P (t1))
1184 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1185 if (TYPE_PTRMEMFUNC_P (t2))
1186 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1188 /* Different classes of types can't be compatible. */
1189 if (TREE_CODE (t1) != TREE_CODE (t2))
1190 return false;
1192 /* Qualifiers must match. For array types, we will check when we
1193 recur on the array element types. */
1194 if (TREE_CODE (t1) != ARRAY_TYPE
1195 && cp_type_quals (t1) != cp_type_quals (t2))
1196 return false;
1197 if (TREE_CODE (t1) == FUNCTION_TYPE
1198 && type_memfn_quals (t1) != type_memfn_quals (t2))
1199 return false;
1200 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1201 return false;
1203 /* Allow for two different type nodes which have essentially the same
1204 definition. Note that we already checked for equality of the type
1205 qualifiers (just above). */
1207 if (TREE_CODE (t1) != ARRAY_TYPE
1208 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1209 return true;
1212 /* Compare the types. Break out if they could be the same. */
1213 switch (TREE_CODE (t1))
1215 case VOID_TYPE:
1216 case BOOLEAN_TYPE:
1217 /* All void and bool types are the same. */
1218 break;
1220 case INTEGER_TYPE:
1221 case FIXED_POINT_TYPE:
1222 case REAL_TYPE:
1223 /* With these nodes, we can't determine type equivalence by
1224 looking at what is stored in the nodes themselves, because
1225 two nodes might have different TYPE_MAIN_VARIANTs but still
1226 represent the same type. For example, wchar_t and int could
1227 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1228 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1229 and are distinct types. On the other hand, int and the
1230 following typedef
1232 typedef int INT __attribute((may_alias));
1234 have identical properties, different TYPE_MAIN_VARIANTs, but
1235 represent the same type. The canonical type system keeps
1236 track of equivalence in this case, so we fall back on it. */
1237 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1239 case TEMPLATE_TEMPLATE_PARM:
1240 case BOUND_TEMPLATE_TEMPLATE_PARM:
1241 if (!comp_template_parms_position (t1, t2))
1242 return false;
1243 if (!comp_template_parms
1244 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1245 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1246 return false;
1247 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1248 break;
1249 /* Don't check inheritance. */
1250 strict = COMPARE_STRICT;
1251 /* Fall through. */
1253 case RECORD_TYPE:
1254 case UNION_TYPE:
1255 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1256 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1257 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1258 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1259 break;
1261 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1262 break;
1263 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1264 break;
1266 return false;
1268 case OFFSET_TYPE:
1269 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1270 strict & ~COMPARE_REDECLARATION))
1271 return false;
1272 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1273 return false;
1274 break;
1276 case REFERENCE_TYPE:
1277 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1278 return false;
1279 /* fall through to checks for pointer types */
1281 case POINTER_TYPE:
1282 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1283 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1284 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1285 return false;
1286 break;
1288 case METHOD_TYPE:
1289 case FUNCTION_TYPE:
1290 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1291 return false;
1292 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1293 return false;
1294 break;
1296 case ARRAY_TYPE:
1297 /* Target types must match incl. qualifiers. */
1298 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1299 return false;
1300 break;
1302 case TEMPLATE_TYPE_PARM:
1303 /* If T1 and T2 don't have the same relative position in their
1304 template parameters set, they can't be equal. */
1305 if (!comp_template_parms_position (t1, t2))
1306 return false;
1307 break;
1309 case TYPENAME_TYPE:
1310 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1311 TYPENAME_TYPE_FULLNAME (t2)))
1312 return false;
1313 /* Qualifiers don't matter on scopes. */
1314 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1315 TYPE_CONTEXT (t2)))
1316 return false;
1317 break;
1319 case UNBOUND_CLASS_TEMPLATE:
1320 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1321 return false;
1322 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1323 return false;
1324 break;
1326 case COMPLEX_TYPE:
1327 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1328 return false;
1329 break;
1331 case VECTOR_TYPE:
1332 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1333 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1334 return false;
1335 break;
1337 case TYPE_PACK_EXPANSION:
1338 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1339 PACK_EXPANSION_PATTERN (t2))
1340 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1341 PACK_EXPANSION_EXTRA_ARGS (t2)));
1343 case DECLTYPE_TYPE:
1344 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1345 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1346 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1347 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1348 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1349 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1350 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1351 DECLTYPE_TYPE_EXPR (t2)))
1352 return false;
1353 break;
1355 case UNDERLYING_TYPE:
1356 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1357 UNDERLYING_TYPE_TYPE (t2));
1359 default:
1360 return false;
1363 /* If we get here, we know that from a target independent POV the
1364 types are the same. Make sure the target attributes are also
1365 the same. */
1366 return comp_type_attributes (t1, t2);
1369 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1370 is a bitwise-or of the COMPARE_* flags. */
1372 bool
1373 comptypes (tree t1, tree t2, int strict)
1375 if (strict == COMPARE_STRICT)
1377 if (t1 == t2)
1378 return true;
1380 if (t1 == error_mark_node || t2 == error_mark_node)
1381 return false;
1383 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1384 /* At least one of the types requires structural equality, so
1385 perform a deep check. */
1386 return structural_comptypes (t1, t2, strict);
1388 #ifdef ENABLE_CHECKING
1389 if (USE_CANONICAL_TYPES)
1391 bool result = structural_comptypes (t1, t2, strict);
1393 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1394 /* The two types are structurally equivalent, but their
1395 canonical types were different. This is a failure of the
1396 canonical type propagation code.*/
1397 internal_error
1398 ("canonical types differ for identical types %T and %T",
1399 t1, t2);
1400 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1401 /* Two types are structurally different, but the canonical
1402 types are the same. This means we were over-eager in
1403 assigning canonical types. */
1404 internal_error
1405 ("same canonical type node for different types %T and %T",
1406 t1, t2);
1408 return result;
1410 #else
1411 if (USE_CANONICAL_TYPES)
1412 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1413 #endif
1414 else
1415 return structural_comptypes (t1, t2, strict);
1417 else if (strict == COMPARE_STRUCTURAL)
1418 return structural_comptypes (t1, t2, COMPARE_STRICT);
1419 else
1420 return structural_comptypes (t1, t2, strict);
1423 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1424 top-level qualifiers. */
1426 bool
1427 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1429 if (type1 == error_mark_node || type2 == error_mark_node)
1430 return false;
1432 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1435 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1437 bool
1438 at_least_as_qualified_p (const_tree type1, const_tree type2)
1440 int q1 = cp_type_quals (type1);
1441 int q2 = cp_type_quals (type2);
1443 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1444 return (q1 & q2) == q2;
1447 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1448 more cv-qualified that TYPE1, and 0 otherwise. */
1451 comp_cv_qualification (const_tree type1, const_tree type2)
1453 int q1 = cp_type_quals (type1);
1454 int q2 = cp_type_quals (type2);
1456 if (q1 == q2)
1457 return 0;
1459 if ((q1 & q2) == q2)
1460 return 1;
1461 else if ((q1 & q2) == q1)
1462 return -1;
1464 return 0;
1467 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1468 subset of the cv-qualification signature of TYPE2, and the types
1469 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1472 comp_cv_qual_signature (tree type1, tree type2)
1474 if (comp_ptr_ttypes_real (type2, type1, -1))
1475 return 1;
1476 else if (comp_ptr_ttypes_real (type1, type2, -1))
1477 return -1;
1478 else
1479 return 0;
1482 /* Subroutines of `comptypes'. */
1484 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1485 equivalent in the sense that functions with those parameter types
1486 can have equivalent types. The two lists must be equivalent,
1487 element by element. */
1489 bool
1490 compparms (const_tree parms1, const_tree parms2)
1492 const_tree t1, t2;
1494 /* An unspecified parmlist matches any specified parmlist
1495 whose argument types don't need default promotions. */
1497 for (t1 = parms1, t2 = parms2;
1498 t1 || t2;
1499 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1501 /* If one parmlist is shorter than the other,
1502 they fail to match. */
1503 if (!t1 || !t2)
1504 return false;
1505 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1506 return false;
1508 return true;
1512 /* Process a sizeof or alignof expression where the operand is a
1513 type. */
1515 tree
1516 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1518 tree value;
1519 bool dependent_p;
1521 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1522 if (type == error_mark_node)
1523 return error_mark_node;
1525 type = non_reference (type);
1526 if (TREE_CODE (type) == METHOD_TYPE)
1528 if (complain)
1529 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
1530 "invalid application of %qs to a member function",
1531 operator_name_info[(int) op].name);
1532 value = size_one_node;
1535 dependent_p = dependent_type_p (type);
1536 if (!dependent_p)
1537 complete_type (type);
1538 if (dependent_p
1539 /* VLA types will have a non-constant size. In the body of an
1540 uninstantiated template, we don't need to try to compute the
1541 value, because the sizeof expression is not an integral
1542 constant expression in that case. And, if we do try to
1543 compute the value, we'll likely end up with SAVE_EXPRs, which
1544 the template substitution machinery does not expect to see. */
1545 || (processing_template_decl
1546 && COMPLETE_TYPE_P (type)
1547 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1549 value = build_min (op, size_type_node, type);
1550 TREE_READONLY (value) = 1;
1551 return value;
1554 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1555 op == SIZEOF_EXPR,
1556 complain);
1559 /* Return the size of the type, without producing any warnings for
1560 types whose size cannot be taken. This routine should be used only
1561 in some other routine that has already produced a diagnostic about
1562 using the size of such a type. */
1563 tree
1564 cxx_sizeof_nowarn (tree type)
1566 if (TREE_CODE (type) == FUNCTION_TYPE
1567 || TREE_CODE (type) == VOID_TYPE
1568 || TREE_CODE (type) == ERROR_MARK)
1569 return size_one_node;
1570 else if (!COMPLETE_TYPE_P (type))
1571 return size_zero_node;
1572 else
1573 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1576 /* Process a sizeof expression where the operand is an expression. */
1578 static tree
1579 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1581 if (e == error_mark_node)
1582 return error_mark_node;
1584 if (processing_template_decl)
1586 e = build_min (SIZEOF_EXPR, size_type_node, e);
1587 TREE_SIDE_EFFECTS (e) = 0;
1588 TREE_READONLY (e) = 1;
1590 return e;
1593 /* To get the size of a static data member declared as an array of
1594 unknown bound, we need to instantiate it. */
1595 if (TREE_CODE (e) == VAR_DECL
1596 && VAR_HAD_UNKNOWN_BOUND (e)
1597 && DECL_TEMPLATE_INSTANTIATION (e))
1598 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1600 e = mark_type_use (e);
1602 if (TREE_CODE (e) == COMPONENT_REF
1603 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1604 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1606 if (complain & tf_error)
1607 error ("invalid application of %<sizeof%> to a bit-field");
1608 else
1609 return error_mark_node;
1610 e = char_type_node;
1612 else if (is_overloaded_fn (e))
1614 if (complain & tf_error)
1615 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1616 "function type");
1617 else
1618 return error_mark_node;
1619 e = char_type_node;
1621 else if (type_unknown_p (e))
1623 if (complain & tf_error)
1624 cxx_incomplete_type_error (e, TREE_TYPE (e));
1625 else
1626 return error_mark_node;
1627 e = char_type_node;
1629 else
1630 e = TREE_TYPE (e);
1632 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1635 /* Implement the __alignof keyword: Return the minimum required
1636 alignment of E, measured in bytes. For VAR_DECL's and
1637 FIELD_DECL's return DECL_ALIGN (which can be set from an
1638 "aligned" __attribute__ specification). */
1640 static tree
1641 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1643 tree t;
1645 if (e == error_mark_node)
1646 return error_mark_node;
1648 if (processing_template_decl)
1650 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1651 TREE_SIDE_EFFECTS (e) = 0;
1652 TREE_READONLY (e) = 1;
1654 return e;
1657 e = mark_type_use (e);
1659 if (TREE_CODE (e) == VAR_DECL)
1660 t = size_int (DECL_ALIGN_UNIT (e));
1661 else if (TREE_CODE (e) == COMPONENT_REF
1662 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1663 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1665 if (complain & tf_error)
1666 error ("invalid application of %<__alignof%> to a bit-field");
1667 else
1668 return error_mark_node;
1669 t = size_one_node;
1671 else if (TREE_CODE (e) == COMPONENT_REF
1672 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1673 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1674 else if (is_overloaded_fn (e))
1676 if (complain & tf_error)
1677 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1678 "function type");
1679 else
1680 return error_mark_node;
1681 if (TREE_CODE (e) == FUNCTION_DECL)
1682 t = size_int (DECL_ALIGN_UNIT (e));
1683 else
1684 t = size_one_node;
1686 else if (type_unknown_p (e))
1688 if (complain & tf_error)
1689 cxx_incomplete_type_error (e, TREE_TYPE (e));
1690 else
1691 return error_mark_node;
1692 t = size_one_node;
1694 else
1695 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1696 complain & tf_error);
1698 return fold_convert (size_type_node, t);
1701 /* Process a sizeof or alignof expression E with code OP where the operand
1702 is an expression. */
1704 tree
1705 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1707 if (op == SIZEOF_EXPR)
1708 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1709 else
1710 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1713 /* EXPR is being used in a context that is not a function call.
1714 Enforce:
1716 [expr.ref]
1718 The expression can be used only as the left-hand operand of a
1719 member function call.
1721 [expr.mptr.operator]
1723 If the result of .* or ->* is a function, then that result can be
1724 used only as the operand for the function call operator ().
1726 by issuing an error message if appropriate. Returns true iff EXPR
1727 violates these rules. */
1729 bool
1730 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1732 if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1734 if (complain & tf_error)
1735 error ("invalid use of non-static member function");
1736 return true;
1738 return false;
1741 /* If EXP is a reference to a bitfield, and the type of EXP does not
1742 match the declared type of the bitfield, return the declared type
1743 of the bitfield. Otherwise, return NULL_TREE. */
1745 tree
1746 is_bitfield_expr_with_lowered_type (const_tree exp)
1748 switch (TREE_CODE (exp))
1750 case COND_EXPR:
1751 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1752 ? TREE_OPERAND (exp, 1)
1753 : TREE_OPERAND (exp, 0)))
1754 return NULL_TREE;
1755 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1757 case COMPOUND_EXPR:
1758 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1760 case MODIFY_EXPR:
1761 case SAVE_EXPR:
1762 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1764 case COMPONENT_REF:
1766 tree field;
1768 field = TREE_OPERAND (exp, 1);
1769 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1770 return NULL_TREE;
1771 if (same_type_ignoring_top_level_qualifiers_p
1772 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1773 return NULL_TREE;
1774 return DECL_BIT_FIELD_TYPE (field);
1777 CASE_CONVERT:
1778 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1779 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1780 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1781 /* Fallthrough. */
1783 default:
1784 return NULL_TREE;
1788 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1789 bitfield with a lowered type, the type of EXP is returned, rather
1790 than NULL_TREE. */
1792 tree
1793 unlowered_expr_type (const_tree exp)
1795 tree type;
1796 tree etype = TREE_TYPE (exp);
1798 type = is_bitfield_expr_with_lowered_type (exp);
1799 if (type)
1800 type = cp_build_qualified_type (type, cp_type_quals (etype));
1801 else
1802 type = etype;
1804 return type;
1807 /* Perform the conversions in [expr] that apply when an lvalue appears
1808 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1809 function-to-pointer conversions. In addition, manifest constants
1810 are replaced by their values, and bitfield references are converted
1811 to their declared types. Note that this function does not perform the
1812 lvalue-to-rvalue conversion for class types. If you need that conversion
1813 to for class types, then you probably need to use force_rvalue.
1815 Although the returned value is being used as an rvalue, this
1816 function does not wrap the returned expression in a
1817 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1818 that the return value is no longer an lvalue. */
1820 tree
1821 decay_conversion (tree exp, tsubst_flags_t complain)
1823 tree type;
1824 enum tree_code code;
1826 type = TREE_TYPE (exp);
1827 if (type == error_mark_node)
1828 return error_mark_node;
1830 exp = mark_rvalue_use (exp);
1832 exp = resolve_nondeduced_context (exp);
1833 if (type_unknown_p (exp))
1835 if (complain & tf_error)
1836 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1837 return error_mark_node;
1840 /* FIXME remove? at least need to remember that this isn't really a
1841 constant expression if EXP isn't decl_constant_var_p, like with
1842 C_MAYBE_CONST_EXPR. */
1843 exp = decl_constant_value_safe (exp);
1844 if (error_operand_p (exp))
1845 return error_mark_node;
1847 if (NULLPTR_TYPE_P (type))
1848 return nullptr_node;
1850 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1851 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1852 code = TREE_CODE (type);
1853 if (code == VOID_TYPE)
1855 if (complain & tf_error)
1856 error ("void value not ignored as it ought to be");
1857 return error_mark_node;
1859 if (invalid_nonstatic_memfn_p (exp, complain))
1860 return error_mark_node;
1861 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1862 return cp_build_addr_expr (exp, complain);
1863 if (code == ARRAY_TYPE)
1865 tree adr;
1866 tree ptrtype;
1868 if (TREE_CODE (exp) == INDIRECT_REF)
1869 return build_nop (build_pointer_type (TREE_TYPE (type)),
1870 TREE_OPERAND (exp, 0));
1872 if (TREE_CODE (exp) == COMPOUND_EXPR)
1874 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1875 if (op1 == error_mark_node)
1876 return error_mark_node;
1877 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1878 TREE_OPERAND (exp, 0), op1);
1881 if (!lvalue_p (exp)
1882 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1884 if (complain & tf_error)
1885 error ("invalid use of non-lvalue array");
1886 return error_mark_node;
1889 ptrtype = build_pointer_type (TREE_TYPE (type));
1891 if (TREE_CODE (exp) == VAR_DECL)
1893 if (!cxx_mark_addressable (exp))
1894 return error_mark_node;
1895 adr = build_nop (ptrtype, build_address (exp));
1896 return adr;
1898 /* This way is better for a COMPONENT_REF since it can
1899 simplify the offset for a component. */
1900 adr = cp_build_addr_expr (exp, complain);
1901 return cp_convert (ptrtype, adr);
1904 /* If a bitfield is used in a context where integral promotion
1905 applies, then the caller is expected to have used
1906 default_conversion. That function promotes bitfields correctly
1907 before calling this function. At this point, if we have a
1908 bitfield referenced, we may assume that is not subject to
1909 promotion, and that, therefore, the type of the resulting rvalue
1910 is the declared type of the bitfield. */
1911 exp = convert_bitfield_to_declared_type (exp);
1913 /* We do not call rvalue() here because we do not want to wrap EXP
1914 in a NON_LVALUE_EXPR. */
1916 /* [basic.lval]
1918 Non-class rvalues always have cv-unqualified types. */
1919 type = TREE_TYPE (exp);
1920 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1921 exp = build_nop (cv_unqualified (type), exp);
1923 return exp;
1926 /* Perform preparatory conversions, as part of the "usual arithmetic
1927 conversions". In particular, as per [expr]:
1929 Whenever an lvalue expression appears as an operand of an
1930 operator that expects the rvalue for that operand, the
1931 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1932 standard conversions are applied to convert the expression to an
1933 rvalue.
1935 In addition, we perform integral promotions here, as those are
1936 applied to both operands to a binary operator before determining
1937 what additional conversions should apply. */
1939 static tree
1940 cp_default_conversion (tree exp, tsubst_flags_t complain)
1942 /* Check for target-specific promotions. */
1943 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1944 if (promoted_type)
1945 exp = cp_convert (promoted_type, exp);
1946 /* Perform the integral promotions first so that bitfield
1947 expressions (which may promote to "int", even if the bitfield is
1948 declared "unsigned") are promoted correctly. */
1949 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1950 exp = perform_integral_promotions (exp);
1951 /* Perform the other conversions. */
1952 exp = decay_conversion (exp, complain);
1954 return exp;
1957 /* C version. */
1959 tree
1960 default_conversion (tree exp)
1962 return cp_default_conversion (exp, tf_warning_or_error);
1965 /* EXPR is an expression with an integral or enumeration type.
1966 Perform the integral promotions in [conv.prom], and return the
1967 converted value. */
1969 tree
1970 perform_integral_promotions (tree expr)
1972 tree type;
1973 tree promoted_type;
1975 expr = mark_rvalue_use (expr);
1977 /* [conv.prom]
1979 If the bitfield has an enumerated type, it is treated as any
1980 other value of that type for promotion purposes. */
1981 type = is_bitfield_expr_with_lowered_type (expr);
1982 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1983 type = TREE_TYPE (expr);
1984 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1985 /* Scoped enums don't promote. */
1986 if (SCOPED_ENUM_P (type))
1987 return expr;
1988 promoted_type = type_promotes_to (type);
1989 if (type != promoted_type)
1990 expr = cp_convert (promoted_type, expr);
1991 return expr;
1994 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1995 decay_conversion to one. */
1998 string_conv_p (const_tree totype, const_tree exp, int warn)
2000 tree t;
2002 if (TREE_CODE (totype) != POINTER_TYPE)
2003 return 0;
2005 t = TREE_TYPE (totype);
2006 if (!same_type_p (t, char_type_node)
2007 && !same_type_p (t, char16_type_node)
2008 && !same_type_p (t, char32_type_node)
2009 && !same_type_p (t, wchar_type_node))
2010 return 0;
2012 if (TREE_CODE (exp) == STRING_CST)
2014 /* Make sure that we don't try to convert between char and wide chars. */
2015 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2016 return 0;
2018 else
2020 /* Is this a string constant which has decayed to 'const char *'? */
2021 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2022 if (!same_type_p (TREE_TYPE (exp), t))
2023 return 0;
2024 STRIP_NOPS (exp);
2025 if (TREE_CODE (exp) != ADDR_EXPR
2026 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2027 return 0;
2030 /* This warning is not very useful, as it complains about printf. */
2031 if (warn)
2032 warning (OPT_Wwrite_strings,
2033 "deprecated conversion from string constant to %qT",
2034 totype);
2036 return 1;
2039 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2040 can, for example, use as an lvalue. This code used to be in
2041 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2042 expressions, where we're dealing with aggregates. But now it's again only
2043 called from unary_complex_lvalue. The case (in particular) that led to
2044 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2045 get it there. */
2047 static tree
2048 rationalize_conditional_expr (enum tree_code code, tree t,
2049 tsubst_flags_t complain)
2051 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2052 the first operand is always the one to be used if both operands
2053 are equal, so we know what conditional expression this used to be. */
2054 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2056 tree op0 = TREE_OPERAND (t, 0);
2057 tree op1 = TREE_OPERAND (t, 1);
2059 /* The following code is incorrect if either operand side-effects. */
2060 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2061 && !TREE_SIDE_EFFECTS (op1));
2062 return
2063 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
2064 ? LE_EXPR : GE_EXPR),
2065 op0, TREE_CODE (op0),
2066 op1, TREE_CODE (op1),
2067 /*overload=*/NULL,
2068 complain),
2069 cp_build_unary_op (code, op0, 0, complain),
2070 cp_build_unary_op (code, op1, 0, complain),
2071 complain);
2074 return
2075 build_conditional_expr (TREE_OPERAND (t, 0),
2076 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2077 complain),
2078 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2079 complain),
2080 complain);
2083 /* Given the TYPE of an anonymous union field inside T, return the
2084 FIELD_DECL for the field. If not found return NULL_TREE. Because
2085 anonymous unions can nest, we must also search all anonymous unions
2086 that are directly reachable. */
2088 tree
2089 lookup_anon_field (tree t, tree type)
2091 tree field;
2093 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2095 if (TREE_STATIC (field))
2096 continue;
2097 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2098 continue;
2100 /* If we find it directly, return the field. */
2101 if (DECL_NAME (field) == NULL_TREE
2102 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2104 return field;
2107 /* Otherwise, it could be nested, search harder. */
2108 if (DECL_NAME (field) == NULL_TREE
2109 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2111 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2112 if (subfield)
2113 return subfield;
2116 return NULL_TREE;
2119 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2120 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2121 non-NULL, it indicates the path to the base used to name MEMBER.
2122 If PRESERVE_REFERENCE is true, the expression returned will have
2123 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2124 returned will have the type referred to by the reference.
2126 This function does not perform access control; that is either done
2127 earlier by the parser when the name of MEMBER is resolved to MEMBER
2128 itself, or later when overload resolution selects one of the
2129 functions indicated by MEMBER. */
2131 tree
2132 build_class_member_access_expr (tree object, tree member,
2133 tree access_path, bool preserve_reference,
2134 tsubst_flags_t complain)
2136 tree object_type;
2137 tree member_scope;
2138 tree result = NULL_TREE;
2139 tree using_decl = NULL_TREE;
2141 if (error_operand_p (object) || error_operand_p (member))
2142 return error_mark_node;
2144 gcc_assert (DECL_P (member) || BASELINK_P (member));
2146 /* [expr.ref]
2148 The type of the first expression shall be "class object" (of a
2149 complete type). */
2150 object_type = TREE_TYPE (object);
2151 if (!currently_open_class (object_type)
2152 && !complete_type_or_maybe_complain (object_type, object, complain))
2153 return error_mark_node;
2154 if (!CLASS_TYPE_P (object_type))
2156 if (complain & tf_error)
2158 if (POINTER_TYPE_P (object_type)
2159 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2160 error ("request for member %qD in %qE, which is of pointer "
2161 "type %qT (maybe you meant to use %<->%> ?)",
2162 member, object, object_type);
2163 else
2164 error ("request for member %qD in %qE, which is of non-class "
2165 "type %qT", member, object, object_type);
2167 return error_mark_node;
2170 /* The standard does not seem to actually say that MEMBER must be a
2171 member of OBJECT_TYPE. However, that is clearly what is
2172 intended. */
2173 if (DECL_P (member))
2175 member_scope = DECL_CLASS_CONTEXT (member);
2176 mark_used (member);
2177 if (TREE_DEPRECATED (member))
2178 warn_deprecated_use (member, NULL_TREE);
2180 else
2181 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2182 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2183 presently be the anonymous union. Go outwards until we find a
2184 type related to OBJECT_TYPE. */
2185 while (ANON_AGGR_TYPE_P (member_scope)
2186 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2187 object_type))
2188 member_scope = TYPE_CONTEXT (member_scope);
2189 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2191 if (complain & tf_error)
2193 if (TREE_CODE (member) == FIELD_DECL)
2194 error ("invalid use of nonstatic data member %qE", member);
2195 else
2196 error ("%qD is not a member of %qT", member, object_type);
2198 return error_mark_node;
2201 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2202 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2203 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2205 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2206 if (temp)
2207 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2210 /* In [expr.ref], there is an explicit list of the valid choices for
2211 MEMBER. We check for each of those cases here. */
2212 if (TREE_CODE (member) == VAR_DECL)
2214 /* A static data member. */
2215 result = member;
2216 mark_exp_read (object);
2217 /* If OBJECT has side-effects, they are supposed to occur. */
2218 if (TREE_SIDE_EFFECTS (object))
2219 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2221 else if (TREE_CODE (member) == FIELD_DECL)
2223 /* A non-static data member. */
2224 bool null_object_p;
2225 int type_quals;
2226 tree member_type;
2228 null_object_p = (TREE_CODE (object) == INDIRECT_REF
2229 && integer_zerop (TREE_OPERAND (object, 0)));
2231 /* Convert OBJECT to the type of MEMBER. */
2232 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2233 TYPE_MAIN_VARIANT (member_scope)))
2235 tree binfo;
2236 base_kind kind;
2238 binfo = lookup_base (access_path ? access_path : object_type,
2239 member_scope, ba_unique, &kind);
2240 if (binfo == error_mark_node)
2241 return error_mark_node;
2243 /* It is invalid to try to get to a virtual base of a
2244 NULL object. The most common cause is invalid use of
2245 offsetof macro. */
2246 if (null_object_p && kind == bk_via_virtual)
2248 if (complain & tf_error)
2250 error ("invalid access to non-static data member %qD of "
2251 "NULL object",
2252 member);
2253 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2255 return error_mark_node;
2258 /* Convert to the base. */
2259 object = build_base_path (PLUS_EXPR, object, binfo,
2260 /*nonnull=*/1, complain);
2261 /* If we found the base successfully then we should be able
2262 to convert to it successfully. */
2263 gcc_assert (object != error_mark_node);
2266 /* Complain about other invalid uses of offsetof, even though they will
2267 give the right answer. Note that we complain whether or not they
2268 actually used the offsetof macro, since there's no way to know at this
2269 point. So we just give a warning, instead of a pedwarn. */
2270 /* Do not produce this warning for base class field references, because
2271 we know for a fact that didn't come from offsetof. This does occur
2272 in various testsuite cases where a null object is passed where a
2273 vtable access is required. */
2274 if (null_object_p && warn_invalid_offsetof
2275 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2276 && !DECL_FIELD_IS_BASE (member)
2277 && cp_unevaluated_operand == 0
2278 && (complain & tf_warning))
2280 warning (OPT_Winvalid_offsetof,
2281 "invalid access to non-static data member %qD "
2282 " of NULL object", member);
2283 warning (OPT_Winvalid_offsetof,
2284 "(perhaps the %<offsetof%> macro was used incorrectly)");
2287 /* If MEMBER is from an anonymous aggregate, we have converted
2288 OBJECT so that it refers to the class containing the
2289 anonymous union. Generate a reference to the anonymous union
2290 itself, and recur to find MEMBER. */
2291 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2292 /* When this code is called from build_field_call, the
2293 object already has the type of the anonymous union.
2294 That is because the COMPONENT_REF was already
2295 constructed, and was then disassembled before calling
2296 build_field_call. After the function-call code is
2297 cleaned up, this waste can be eliminated. */
2298 && (!same_type_ignoring_top_level_qualifiers_p
2299 (TREE_TYPE (object), DECL_CONTEXT (member))))
2301 tree anonymous_union;
2303 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2304 DECL_CONTEXT (member));
2305 object = build_class_member_access_expr (object,
2306 anonymous_union,
2307 /*access_path=*/NULL_TREE,
2308 preserve_reference,
2309 complain);
2312 /* Compute the type of the field, as described in [expr.ref]. */
2313 type_quals = TYPE_UNQUALIFIED;
2314 member_type = TREE_TYPE (member);
2315 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2317 type_quals = (cp_type_quals (member_type)
2318 | cp_type_quals (object_type));
2320 /* A field is const (volatile) if the enclosing object, or the
2321 field itself, is const (volatile). But, a mutable field is
2322 not const, even within a const object. */
2323 if (DECL_MUTABLE_P (member))
2324 type_quals &= ~TYPE_QUAL_CONST;
2325 member_type = cp_build_qualified_type (member_type, type_quals);
2328 result = build3 (COMPONENT_REF, member_type, object, member,
2329 NULL_TREE);
2330 result = fold_if_not_in_template (result);
2332 /* Mark the expression const or volatile, as appropriate. Even
2333 though we've dealt with the type above, we still have to mark the
2334 expression itself. */
2335 if (type_quals & TYPE_QUAL_CONST)
2336 TREE_READONLY (result) = 1;
2337 if (type_quals & TYPE_QUAL_VOLATILE)
2338 TREE_THIS_VOLATILE (result) = 1;
2340 else if (BASELINK_P (member))
2342 /* The member is a (possibly overloaded) member function. */
2343 tree functions;
2344 tree type;
2346 /* If the MEMBER is exactly one static member function, then we
2347 know the type of the expression. Otherwise, we must wait
2348 until overload resolution has been performed. */
2349 functions = BASELINK_FUNCTIONS (member);
2350 if (TREE_CODE (functions) == FUNCTION_DECL
2351 && DECL_STATIC_FUNCTION_P (functions))
2352 type = TREE_TYPE (functions);
2353 else
2354 type = unknown_type_node;
2355 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2356 base. That will happen when the function is called. */
2357 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2359 else if (TREE_CODE (member) == CONST_DECL)
2361 /* The member is an enumerator. */
2362 result = member;
2363 /* If OBJECT has side-effects, they are supposed to occur. */
2364 if (TREE_SIDE_EFFECTS (object))
2365 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2366 object, result);
2368 else if ((using_decl = strip_using_decl (member)) != member)
2369 result = build_class_member_access_expr (object,
2370 using_decl,
2371 access_path, preserve_reference,
2372 complain);
2373 else
2375 if (complain & tf_error)
2376 error ("invalid use of %qD", member);
2377 return error_mark_node;
2380 if (!preserve_reference)
2381 /* [expr.ref]
2383 If E2 is declared to have type "reference to T", then ... the
2384 type of E1.E2 is T. */
2385 result = convert_from_reference (result);
2387 return result;
2390 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2391 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2393 static tree
2394 lookup_destructor (tree object, tree scope, tree dtor_name)
2396 tree object_type = TREE_TYPE (object);
2397 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2398 tree expr;
2400 if (scope && !check_dtor_name (scope, dtor_type))
2402 error ("qualified type %qT does not match destructor name ~%qT",
2403 scope, dtor_type);
2404 return error_mark_node;
2406 if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2408 /* In a template, names we can't find a match for are still accepted
2409 destructor names, and we check them here. */
2410 if (check_dtor_name (object_type, dtor_type))
2411 dtor_type = object_type;
2412 else
2414 error ("object type %qT does not match destructor name ~%qT",
2415 object_type, dtor_type);
2416 return error_mark_node;
2420 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2422 error ("the type being destroyed is %qT, but the destructor refers to %qT",
2423 TYPE_MAIN_VARIANT (object_type), dtor_type);
2424 return error_mark_node;
2426 expr = lookup_member (dtor_type, complete_dtor_identifier,
2427 /*protect=*/1, /*want_type=*/false,
2428 tf_warning_or_error);
2429 expr = (adjust_result_of_qualified_name_lookup
2430 (expr, dtor_type, object_type));
2431 if (scope == NULL_TREE)
2432 /* We need to call adjust_result_of_qualified_name_lookup in case the
2433 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2434 that we still get virtual function binding. */
2435 BASELINK_QUALIFIED_P (expr) = false;
2436 return expr;
2439 /* An expression of the form "A::template B" has been resolved to
2440 DECL. Issue a diagnostic if B is not a template or template
2441 specialization. */
2443 void
2444 check_template_keyword (tree decl)
2446 /* The standard says:
2448 [temp.names]
2450 If a name prefixed by the keyword template is not a member
2451 template, the program is ill-formed.
2453 DR 228 removed the restriction that the template be a member
2454 template.
2456 DR 96, if accepted would add the further restriction that explicit
2457 template arguments must be provided if the template keyword is
2458 used, but, as of 2005-10-16, that DR is still in "drafting". If
2459 this DR is accepted, then the semantic checks here can be
2460 simplified, as the entity named must in fact be a template
2461 specialization, rather than, as at present, a set of overloaded
2462 functions containing at least one template function. */
2463 if (TREE_CODE (decl) != TEMPLATE_DECL
2464 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2466 if (!is_overloaded_fn (decl))
2467 permerror (input_location, "%qD is not a template", decl);
2468 else
2470 tree fns;
2471 fns = decl;
2472 if (BASELINK_P (fns))
2473 fns = BASELINK_FUNCTIONS (fns);
2474 while (fns)
2476 tree fn = OVL_CURRENT (fns);
2477 if (TREE_CODE (fn) == TEMPLATE_DECL
2478 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2479 break;
2480 if (TREE_CODE (fn) == FUNCTION_DECL
2481 && DECL_USE_TEMPLATE (fn)
2482 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2483 break;
2484 fns = OVL_NEXT (fns);
2486 if (!fns)
2487 permerror (input_location, "%qD is not a template", decl);
2492 /* This function is called by the parser to process a class member
2493 access expression of the form OBJECT.NAME. NAME is a node used by
2494 the parser to represent a name; it is not yet a DECL. It may,
2495 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2496 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2497 there is no reason to do the lookup twice, so the parser keeps the
2498 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2499 be a template via the use of the "A::template B" syntax. */
2501 tree
2502 finish_class_member_access_expr (tree object, tree name, bool template_p,
2503 tsubst_flags_t complain)
2505 tree expr;
2506 tree object_type;
2507 tree member;
2508 tree access_path = NULL_TREE;
2509 tree orig_object = object;
2510 tree orig_name = name;
2512 if (object == error_mark_node || name == error_mark_node)
2513 return error_mark_node;
2515 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2516 if (!objc_is_public (object, name))
2517 return error_mark_node;
2519 object_type = TREE_TYPE (object);
2521 if (processing_template_decl)
2523 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2524 dependent_type_p (object_type)
2525 /* If NAME is just an IDENTIFIER_NODE, then the expression
2526 is dependent. */
2527 || TREE_CODE (object) == IDENTIFIER_NODE
2528 /* If NAME is "f<args>", where either 'f' or 'args' is
2529 dependent, then the expression is dependent. */
2530 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2531 && dependent_template_id_p (TREE_OPERAND (name, 0),
2532 TREE_OPERAND (name, 1)))
2533 /* If NAME is "T::X" where "T" is dependent, then the
2534 expression is dependent. */
2535 || (TREE_CODE (name) == SCOPE_REF
2536 && TYPE_P (TREE_OPERAND (name, 0))
2537 && dependent_type_p (TREE_OPERAND (name, 0))))
2538 return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2539 object = build_non_dependent_expr (object);
2541 else if (c_dialect_objc ()
2542 && TREE_CODE (name) == IDENTIFIER_NODE
2543 && (expr = objc_maybe_build_component_ref (object, name)))
2544 return expr;
2546 /* [expr.ref]
2548 The type of the first expression shall be "class object" (of a
2549 complete type). */
2550 if (!currently_open_class (object_type)
2551 && !complete_type_or_maybe_complain (object_type, object, complain))
2552 return error_mark_node;
2553 if (!CLASS_TYPE_P (object_type))
2555 if (complain & tf_error)
2557 if (POINTER_TYPE_P (object_type)
2558 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2559 error ("request for member %qD in %qE, which is of pointer "
2560 "type %qT (maybe you meant to use %<->%> ?)",
2561 name, object, object_type);
2562 else
2563 error ("request for member %qD in %qE, which is of non-class "
2564 "type %qT", name, object, object_type);
2566 return error_mark_node;
2569 if (BASELINK_P (name))
2570 /* A member function that has already been looked up. */
2571 member = name;
2572 else
2574 bool is_template_id = false;
2575 tree template_args = NULL_TREE;
2576 tree scope;
2578 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2580 is_template_id = true;
2581 template_args = TREE_OPERAND (name, 1);
2582 name = TREE_OPERAND (name, 0);
2584 if (TREE_CODE (name) == OVERLOAD)
2585 name = DECL_NAME (get_first_fn (name));
2586 else if (DECL_P (name))
2587 name = DECL_NAME (name);
2590 if (TREE_CODE (name) == SCOPE_REF)
2592 /* A qualified name. The qualifying class or namespace `S'
2593 has already been looked up; it is either a TYPE or a
2594 NAMESPACE_DECL. */
2595 scope = TREE_OPERAND (name, 0);
2596 name = TREE_OPERAND (name, 1);
2598 /* If SCOPE is a namespace, then the qualified name does not
2599 name a member of OBJECT_TYPE. */
2600 if (TREE_CODE (scope) == NAMESPACE_DECL)
2602 if (complain & tf_error)
2603 error ("%<%D::%D%> is not a member of %qT",
2604 scope, name, object_type);
2605 return error_mark_node;
2608 gcc_assert (CLASS_TYPE_P (scope));
2609 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2610 || TREE_CODE (name) == BIT_NOT_EXPR);
2612 if (constructor_name_p (name, scope))
2614 if (complain & tf_error)
2615 error ("cannot call constructor %<%T::%D%> directly",
2616 scope, name);
2617 return error_mark_node;
2620 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2621 access_path = lookup_base (object_type, scope, ba_check, NULL);
2622 if (access_path == error_mark_node)
2623 return error_mark_node;
2624 if (!access_path)
2626 if (complain & tf_error)
2627 error ("%qT is not a base of %qT", scope, object_type);
2628 return error_mark_node;
2631 else
2633 scope = NULL_TREE;
2634 access_path = object_type;
2637 if (TREE_CODE (name) == BIT_NOT_EXPR)
2638 member = lookup_destructor (object, scope, name);
2639 else
2641 /* Look up the member. */
2642 member = lookup_member (access_path, name, /*protect=*/1,
2643 /*want_type=*/false, complain);
2644 if (member == NULL_TREE)
2646 if (complain & tf_error)
2647 error ("%qD has no member named %qE",
2648 TREE_CODE (access_path) == TREE_BINFO
2649 ? TREE_TYPE (access_path) : object_type, name);
2650 return error_mark_node;
2652 if (member == error_mark_node)
2653 return error_mark_node;
2656 if (is_template_id)
2658 tree templ = member;
2660 if (BASELINK_P (templ))
2661 templ = lookup_template_function (templ, template_args);
2662 else
2664 if (complain & tf_error)
2665 error ("%qD is not a member template function", name);
2666 return error_mark_node;
2671 if (TREE_DEPRECATED (member))
2672 warn_deprecated_use (member, NULL_TREE);
2674 if (template_p)
2675 check_template_keyword (member);
2677 expr = build_class_member_access_expr (object, member, access_path,
2678 /*preserve_reference=*/false,
2679 complain);
2680 if (processing_template_decl && expr != error_mark_node)
2682 if (BASELINK_P (member))
2684 if (TREE_CODE (orig_name) == SCOPE_REF)
2685 BASELINK_QUALIFIED_P (member) = 1;
2686 orig_name = member;
2688 return build_min_non_dep (COMPONENT_REF, expr,
2689 orig_object, orig_name,
2690 NULL_TREE);
2693 return expr;
2696 /* Return an expression for the MEMBER_NAME field in the internal
2697 representation of PTRMEM, a pointer-to-member function. (Each
2698 pointer-to-member function type gets its own RECORD_TYPE so it is
2699 more convenient to access the fields by name than by FIELD_DECL.)
2700 This routine converts the NAME to a FIELD_DECL and then creates the
2701 node for the complete expression. */
2703 tree
2704 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2706 tree ptrmem_type;
2707 tree member;
2708 tree member_type;
2710 /* This code is a stripped down version of
2711 build_class_member_access_expr. It does not work to use that
2712 routine directly because it expects the object to be of class
2713 type. */
2714 ptrmem_type = TREE_TYPE (ptrmem);
2715 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2716 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2717 /*want_type=*/false, tf_warning_or_error);
2718 member_type = cp_build_qualified_type (TREE_TYPE (member),
2719 cp_type_quals (ptrmem_type));
2720 return fold_build3_loc (input_location,
2721 COMPONENT_REF, member_type,
2722 ptrmem, member, NULL_TREE);
2725 /* Given an expression PTR for a pointer, return an expression
2726 for the value pointed to.
2727 ERRORSTRING is the name of the operator to appear in error messages.
2729 This function may need to overload OPERATOR_FNNAME.
2730 Must also handle REFERENCE_TYPEs for C++. */
2732 tree
2733 build_x_indirect_ref (tree expr, ref_operator errorstring,
2734 tsubst_flags_t complain)
2736 tree orig_expr = expr;
2737 tree rval;
2739 if (processing_template_decl)
2741 /* Retain the type if we know the operand is a pointer. */
2742 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2743 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2744 if (type_dependent_expression_p (expr))
2745 return build_min_nt (INDIRECT_REF, expr);
2746 expr = build_non_dependent_expr (expr);
2749 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2750 NULL_TREE, /*overload=*/NULL, complain);
2751 if (!rval)
2752 rval = cp_build_indirect_ref (expr, errorstring, complain);
2754 if (processing_template_decl && rval != error_mark_node)
2755 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2756 else
2757 return rval;
2760 /* Helper function called from c-common. */
2761 tree
2762 build_indirect_ref (location_t loc ATTRIBUTE_UNUSED,
2763 tree ptr, ref_operator errorstring)
2765 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2768 tree
2769 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2770 tsubst_flags_t complain)
2772 tree pointer, type;
2774 if (ptr == current_class_ptr)
2775 return current_class_ref;
2777 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2778 ? ptr : decay_conversion (ptr, complain));
2779 if (pointer == error_mark_node)
2780 return error_mark_node;
2782 type = TREE_TYPE (pointer);
2784 if (POINTER_TYPE_P (type))
2786 /* [expr.unary.op]
2788 If the type of the expression is "pointer to T," the type
2789 of the result is "T." */
2790 tree t = TREE_TYPE (type);
2792 if (CONVERT_EXPR_P (ptr)
2793 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2795 /* If a warning is issued, mark it to avoid duplicates from
2796 the backend. This only needs to be done at
2797 warn_strict_aliasing > 2. */
2798 if (warn_strict_aliasing > 2)
2799 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2800 type, TREE_OPERAND (ptr, 0)))
2801 TREE_NO_WARNING (ptr) = 1;
2804 if (VOID_TYPE_P (t))
2806 /* A pointer to incomplete type (other than cv void) can be
2807 dereferenced [expr.unary.op]/1 */
2808 if (complain & tf_error)
2809 error ("%qT is not a pointer-to-object type", type);
2810 return error_mark_node;
2812 else if (TREE_CODE (pointer) == ADDR_EXPR
2813 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2814 /* The POINTER was something like `&x'. We simplify `*&x' to
2815 `x'. */
2816 return TREE_OPERAND (pointer, 0);
2817 else
2819 tree ref = build1 (INDIRECT_REF, t, pointer);
2821 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2822 so that we get the proper error message if the result is used
2823 to assign to. Also, &* is supposed to be a no-op. */
2824 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2825 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2826 TREE_SIDE_EFFECTS (ref)
2827 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2828 return ref;
2831 else if (!(complain & tf_error))
2832 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2834 /* `pointer' won't be an error_mark_node if we were given a
2835 pointer to member, so it's cool to check for this here. */
2836 else if (TYPE_PTR_TO_MEMBER_P (type))
2837 switch (errorstring)
2839 case RO_ARRAY_INDEXING:
2840 error ("invalid use of array indexing on pointer to member");
2841 break;
2842 case RO_UNARY_STAR:
2843 error ("invalid use of unary %<*%> on pointer to member");
2844 break;
2845 case RO_IMPLICIT_CONVERSION:
2846 error ("invalid use of implicit conversion on pointer to member");
2847 break;
2848 default:
2849 gcc_unreachable ();
2851 else if (pointer != error_mark_node)
2852 invalid_indirection_error (input_location, type, errorstring);
2854 return error_mark_node;
2857 /* This handles expressions of the form "a[i]", which denotes
2858 an array reference.
2860 This is logically equivalent in C to *(a+i), but we may do it differently.
2861 If A is a variable or a member, we generate a primitive ARRAY_REF.
2862 This avoids forcing the array out of registers, and can work on
2863 arrays that are not lvalues (for example, members of structures returned
2864 by functions).
2866 If INDEX is of some user-defined type, it must be converted to
2867 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2868 will inherit the type of the array, which will be some pointer type.
2870 LOC is the location to use in building the array reference. */
2872 tree
2873 cp_build_array_ref (location_t loc, tree array, tree idx,
2874 tsubst_flags_t complain)
2876 tree ret;
2878 if (idx == 0)
2880 if (complain & tf_error)
2881 error_at (loc, "subscript missing in array reference");
2882 return error_mark_node;
2885 if (TREE_TYPE (array) == error_mark_node
2886 || TREE_TYPE (idx) == error_mark_node)
2887 return error_mark_node;
2889 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2890 inside it. */
2891 switch (TREE_CODE (array))
2893 case COMPOUND_EXPR:
2895 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2896 complain);
2897 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2898 TREE_OPERAND (array, 0), value);
2899 SET_EXPR_LOCATION (ret, loc);
2900 return ret;
2903 case COND_EXPR:
2904 ret = build_conditional_expr
2905 (TREE_OPERAND (array, 0),
2906 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2907 complain),
2908 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2909 complain),
2910 complain);
2911 protected_set_expr_location (ret, loc);
2912 return ret;
2914 default:
2915 break;
2918 convert_vector_to_pointer_for_subscript (loc, &array, idx);
2920 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2922 tree rval, type;
2924 warn_array_subscript_with_type_char (idx);
2926 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2928 if (complain & tf_error)
2929 error_at (loc, "array subscript is not an integer");
2930 return error_mark_node;
2933 /* Apply integral promotions *after* noticing character types.
2934 (It is unclear why we do these promotions -- the standard
2935 does not say that we should. In fact, the natural thing would
2936 seem to be to convert IDX to ptrdiff_t; we're performing
2937 pointer arithmetic.) */
2938 idx = perform_integral_promotions (idx);
2940 /* An array that is indexed by a non-constant
2941 cannot be stored in a register; we must be able to do
2942 address arithmetic on its address.
2943 Likewise an array of elements of variable size. */
2944 if (TREE_CODE (idx) != INTEGER_CST
2945 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2946 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2947 != INTEGER_CST)))
2949 if (!cxx_mark_addressable (array))
2950 return error_mark_node;
2953 /* An array that is indexed by a constant value which is not within
2954 the array bounds cannot be stored in a register either; because we
2955 would get a crash in store_bit_field/extract_bit_field when trying
2956 to access a non-existent part of the register. */
2957 if (TREE_CODE (idx) == INTEGER_CST
2958 && TYPE_DOMAIN (TREE_TYPE (array))
2959 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2961 if (!cxx_mark_addressable (array))
2962 return error_mark_node;
2965 if (!lvalue_p (array) && (complain & tf_error))
2966 pedwarn (loc, OPT_Wpedantic,
2967 "ISO C++ forbids subscripting non-lvalue array");
2969 /* Note in C++ it is valid to subscript a `register' array, since
2970 it is valid to take the address of something with that
2971 storage specification. */
2972 if (extra_warnings)
2974 tree foo = array;
2975 while (TREE_CODE (foo) == COMPONENT_REF)
2976 foo = TREE_OPERAND (foo, 0);
2977 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
2978 && (complain & tf_warning))
2979 warning_at (loc, OPT_Wextra,
2980 "subscripting array declared %<register%>");
2983 type = TREE_TYPE (TREE_TYPE (array));
2984 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2985 /* Array ref is const/volatile if the array elements are
2986 or if the array is.. */
2987 TREE_READONLY (rval)
2988 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2989 TREE_SIDE_EFFECTS (rval)
2990 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2991 TREE_THIS_VOLATILE (rval)
2992 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2993 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
2994 complain);
2995 protected_set_expr_location (ret, loc);
2996 return ret;
3000 tree ar = cp_default_conversion (array, complain);
3001 tree ind = cp_default_conversion (idx, complain);
3003 /* Put the integer in IND to simplify error checking. */
3004 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3006 tree temp = ar;
3007 ar = ind;
3008 ind = temp;
3011 if (ar == error_mark_node || ind == error_mark_node)
3012 return error_mark_node;
3014 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
3016 if (complain & tf_error)
3017 error_at (loc, "subscripted value is neither array nor pointer");
3018 return error_mark_node;
3020 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3022 if (complain & tf_error)
3023 error_at (loc, "array subscript is not an integer");
3024 return error_mark_node;
3027 warn_array_subscript_with_type_char (idx);
3029 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3030 PLUS_EXPR, ar, ind,
3031 complain),
3032 RO_ARRAY_INDEXING,
3033 complain);
3034 protected_set_expr_location (ret, loc);
3035 return ret;
3039 /* Entry point for Obj-C++. */
3041 tree
3042 build_array_ref (location_t loc, tree array, tree idx)
3044 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3047 /* Resolve a pointer to member function. INSTANCE is the object
3048 instance to use, if the member points to a virtual member.
3050 This used to avoid checking for virtual functions if basetype
3051 has no virtual functions, according to an earlier ANSI draft.
3052 With the final ISO C++ rules, such an optimization is
3053 incorrect: A pointer to a derived member can be static_cast
3054 to pointer-to-base-member, as long as the dynamic object
3055 later has the right member. */
3057 tree
3058 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3059 tsubst_flags_t complain)
3061 if (TREE_CODE (function) == OFFSET_REF)
3062 function = TREE_OPERAND (function, 1);
3064 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3066 tree idx, delta, e1, e2, e3, vtbl, basetype;
3067 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3069 tree instance_ptr = *instance_ptrptr;
3070 tree instance_save_expr = 0;
3071 if (instance_ptr == error_mark_node)
3073 if (TREE_CODE (function) == PTRMEM_CST)
3075 /* Extracting the function address from a pmf is only
3076 allowed with -Wno-pmf-conversions. It only works for
3077 pmf constants. */
3078 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3079 e1 = convert (fntype, e1);
3080 return e1;
3082 else
3084 if (complain & tf_error)
3085 error ("object missing in use of %qE", function);
3086 return error_mark_node;
3090 if (TREE_SIDE_EFFECTS (instance_ptr))
3091 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3093 if (TREE_SIDE_EFFECTS (function))
3094 function = save_expr (function);
3096 /* Start by extracting all the information from the PMF itself. */
3097 e3 = pfn_from_ptrmemfunc (function);
3098 delta = delta_from_ptrmemfunc (function);
3099 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3100 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3102 case ptrmemfunc_vbit_in_pfn:
3103 e1 = cp_build_binary_op (input_location,
3104 BIT_AND_EXPR, idx, integer_one_node,
3105 complain);
3106 idx = cp_build_binary_op (input_location,
3107 MINUS_EXPR, idx, integer_one_node,
3108 complain);
3109 if (idx == error_mark_node)
3110 return error_mark_node;
3111 break;
3113 case ptrmemfunc_vbit_in_delta:
3114 e1 = cp_build_binary_op (input_location,
3115 BIT_AND_EXPR, delta, integer_one_node,
3116 complain);
3117 delta = cp_build_binary_op (input_location,
3118 RSHIFT_EXPR, delta, integer_one_node,
3119 complain);
3120 if (delta == error_mark_node)
3121 return error_mark_node;
3122 break;
3124 default:
3125 gcc_unreachable ();
3128 if (e1 == error_mark_node)
3129 return error_mark_node;
3131 /* Convert down to the right base before using the instance. A
3132 special case is that in a pointer to member of class C, C may
3133 be incomplete. In that case, the function will of course be
3134 a member of C, and no conversion is required. In fact,
3135 lookup_base will fail in that case, because incomplete
3136 classes do not have BINFOs. */
3137 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3138 if (!same_type_ignoring_top_level_qualifiers_p
3139 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3141 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3142 basetype, ba_check, NULL);
3143 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3144 1, complain);
3145 if (instance_ptr == error_mark_node)
3146 return error_mark_node;
3148 /* ...and then the delta in the PMF. */
3149 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3151 /* Hand back the adjusted 'this' argument to our caller. */
3152 *instance_ptrptr = instance_ptr;
3154 /* Next extract the vtable pointer from the object. */
3155 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3156 instance_ptr);
3157 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3158 if (vtbl == error_mark_node)
3159 return error_mark_node;
3161 /* If the object is not dynamic the access invokes undefined
3162 behavior. As it is not executed in this case silence the
3163 spurious warnings it may provoke. */
3164 TREE_NO_WARNING (vtbl) = 1;
3166 /* Finally, extract the function pointer from the vtable. */
3167 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3168 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3169 if (e2 == error_mark_node)
3170 return error_mark_node;
3171 TREE_CONSTANT (e2) = 1;
3173 /* When using function descriptors, the address of the
3174 vtable entry is treated as a function pointer. */
3175 if (TARGET_VTABLE_USES_DESCRIPTORS)
3176 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3177 cp_build_addr_expr (e2, complain));
3179 e2 = fold_convert (TREE_TYPE (e3), e2);
3180 e1 = build_conditional_expr (e1, e2, e3, complain);
3181 if (e1 == error_mark_node)
3182 return error_mark_node;
3184 /* Make sure this doesn't get evaluated first inside one of the
3185 branches of the COND_EXPR. */
3186 if (instance_save_expr)
3187 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3188 instance_save_expr, e1);
3190 function = e1;
3192 return function;
3195 /* Used by the C-common bits. */
3196 tree
3197 build_function_call (location_t loc ATTRIBUTE_UNUSED,
3198 tree function, tree params)
3200 return cp_build_function_call (function, params, tf_warning_or_error);
3203 /* Used by the C-common bits. */
3204 tree
3205 build_function_call_vec (location_t loc ATTRIBUTE_UNUSED,
3206 tree function, VEC(tree,gc) *params,
3207 VEC(tree,gc) *origtypes ATTRIBUTE_UNUSED)
3209 VEC(tree,gc) *orig_params = params;
3210 tree ret = cp_build_function_call_vec (function, &params,
3211 tf_warning_or_error);
3213 /* cp_build_function_call_vec can reallocate PARAMS by adding
3214 default arguments. That should never happen here. Verify
3215 that. */
3216 gcc_assert (params == orig_params);
3218 return ret;
3221 /* Build a function call using a tree list of arguments. */
3223 tree
3224 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3226 VEC(tree,gc) *vec;
3227 tree ret;
3229 vec = make_tree_vector ();
3230 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3231 VEC_safe_push (tree, gc, vec, TREE_VALUE (params));
3232 ret = cp_build_function_call_vec (function, &vec, complain);
3233 release_tree_vector (vec);
3234 return ret;
3237 /* Build a function call using varargs. */
3239 tree
3240 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3242 VEC(tree,gc) *vec;
3243 va_list args;
3244 tree ret, t;
3246 vec = make_tree_vector ();
3247 va_start (args, complain);
3248 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3249 VEC_safe_push (tree, gc, vec, t);
3250 va_end (args);
3251 ret = cp_build_function_call_vec (function, &vec, complain);
3252 release_tree_vector (vec);
3253 return ret;
3256 /* Build a function call using a vector of arguments. PARAMS may be
3257 NULL if there are no parameters. This changes the contents of
3258 PARAMS. */
3260 tree
3261 cp_build_function_call_vec (tree function, VEC(tree,gc) **params,
3262 tsubst_flags_t complain)
3264 tree fntype, fndecl;
3265 int is_method;
3266 tree original = function;
3267 int nargs;
3268 tree *argarray;
3269 tree parm_types;
3270 VEC(tree,gc) *allocated = NULL;
3271 tree ret;
3273 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3274 expressions, like those used for ObjC messenger dispatches. */
3275 if (params != NULL && !VEC_empty (tree, *params))
3276 function = objc_rewrite_function_call (function,
3277 VEC_index (tree, *params, 0));
3279 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3280 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3281 if (TREE_CODE (function) == NOP_EXPR
3282 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3283 function = TREE_OPERAND (function, 0);
3285 if (TREE_CODE (function) == FUNCTION_DECL)
3287 mark_used (function);
3288 fndecl = function;
3290 /* Convert anything with function type to a pointer-to-function. */
3291 if (DECL_MAIN_P (function) && (complain & tf_error))
3292 pedwarn (input_location, OPT_Wpedantic,
3293 "ISO C++ forbids calling %<::main%> from within program");
3295 function = build_addr_func (function, complain);
3297 else
3299 fndecl = NULL_TREE;
3301 function = build_addr_func (function, complain);
3304 if (function == error_mark_node)
3305 return error_mark_node;
3307 fntype = TREE_TYPE (function);
3309 if (TYPE_PTRMEMFUNC_P (fntype))
3311 if (complain & tf_error)
3312 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3313 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3314 original, original);
3315 return error_mark_node;
3318 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3319 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3321 if (!((TREE_CODE (fntype) == POINTER_TYPE
3322 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3323 || is_method
3324 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3326 if (complain & tf_error)
3328 if (!flag_diagnostics_show_caret)
3329 error_at (input_location,
3330 "%qE cannot be used as a function", original);
3331 else if (DECL_P (original))
3332 error_at (input_location,
3333 "%qD cannot be used as a function", original);
3334 else
3335 error_at (input_location,
3336 "expression cannot be used as a function");
3339 return error_mark_node;
3342 /* fntype now gets the type of function pointed to. */
3343 fntype = TREE_TYPE (fntype);
3344 parm_types = TYPE_ARG_TYPES (fntype);
3346 if (params == NULL)
3348 allocated = make_tree_vector ();
3349 params = &allocated;
3352 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3353 complain);
3354 if (nargs < 0)
3355 return error_mark_node;
3357 argarray = VEC_address (tree, *params);
3359 /* Check for errors in format strings and inappropriately
3360 null parameters. */
3361 check_function_arguments (fntype, nargs, argarray);
3363 ret = build_cxx_call (function, nargs, argarray);
3365 if (allocated != NULL)
3366 release_tree_vector (allocated);
3368 return ret;
3371 /* Subroutine of convert_arguments.
3372 Warn about wrong number of args are genereted. */
3374 static void
3375 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3377 if (fndecl)
3379 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3381 if (DECL_NAME (fndecl) == NULL_TREE
3382 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3383 error_at (loc,
3384 too_many_p
3385 ? G_("too many arguments to constructor %q#D")
3386 : G_("too few arguments to constructor %q#D"),
3387 fndecl);
3388 else
3389 error_at (loc,
3390 too_many_p
3391 ? G_("too many arguments to member function %q#D")
3392 : G_("too few arguments to member function %q#D"),
3393 fndecl);
3395 else
3396 error_at (loc,
3397 too_many_p
3398 ? G_("too many arguments to function %q#D")
3399 : G_("too few arguments to function %q#D"),
3400 fndecl);
3401 inform (DECL_SOURCE_LOCATION (fndecl),
3402 "declared here");
3404 else
3406 if (c_dialect_objc () && objc_message_selector ())
3407 error_at (loc,
3408 too_many_p
3409 ? G_("too many arguments to method %q#D")
3410 : G_("too few arguments to method %q#D"),
3411 objc_message_selector ());
3412 else
3413 error_at (loc, too_many_p ? G_("too many arguments to function")
3414 : G_("too few arguments to function"));
3418 /* Convert the actual parameter expressions in the list VALUES to the
3419 types in the list TYPELIST. The converted expressions are stored
3420 back in the VALUES vector.
3421 If parmdecls is exhausted, or when an element has NULL as its type,
3422 perform the default conversions.
3424 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3426 This is also where warnings about wrong number of args are generated.
3428 Returns the actual number of arguments processed (which might be less
3429 than the length of the vector), or -1 on error.
3431 In C++, unspecified trailing parameters can be filled in with their
3432 default arguments, if such were specified. Do so here. */
3434 static int
3435 convert_arguments (tree typelist, VEC(tree,gc) **values, tree fndecl,
3436 int flags, tsubst_flags_t complain)
3438 tree typetail;
3439 unsigned int i;
3441 /* Argument passing is always copy-initialization. */
3442 flags |= LOOKUP_ONLYCONVERTING;
3444 for (i = 0, typetail = typelist;
3445 i < VEC_length (tree, *values);
3446 i++)
3448 tree type = typetail ? TREE_VALUE (typetail) : 0;
3449 tree val = VEC_index (tree, *values, i);
3451 if (val == error_mark_node || type == error_mark_node)
3452 return -1;
3454 if (type == void_type_node)
3456 if (complain & tf_error)
3458 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3459 return i;
3461 else
3462 return -1;
3465 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3466 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3467 if (TREE_CODE (val) == NOP_EXPR
3468 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3469 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3470 val = TREE_OPERAND (val, 0);
3472 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3474 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3475 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3476 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3477 val = decay_conversion (val, complain);
3480 if (val == error_mark_node)
3481 return -1;
3483 if (type != 0)
3485 /* Formal parm type is specified by a function prototype. */
3486 tree parmval;
3488 if (!COMPLETE_TYPE_P (complete_type (type)))
3490 if (complain & tf_error)
3492 if (fndecl)
3493 error ("parameter %P of %qD has incomplete type %qT",
3494 i, fndecl, type);
3495 else
3496 error ("parameter %P has incomplete type %qT", i, type);
3498 parmval = error_mark_node;
3500 else
3502 parmval = convert_for_initialization
3503 (NULL_TREE, type, val, flags,
3504 ICR_ARGPASS, fndecl, i, complain);
3505 parmval = convert_for_arg_passing (type, parmval, complain);
3508 if (parmval == error_mark_node)
3509 return -1;
3511 VEC_replace (tree, *values, i, parmval);
3513 else
3515 if (fndecl && DECL_BUILT_IN (fndecl)
3516 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3517 /* Don't do ellipsis conversion for __built_in_constant_p
3518 as this will result in spurious errors for non-trivial
3519 types. */
3520 val = require_complete_type_sfinae (val, complain);
3521 else
3522 val = convert_arg_to_ellipsis (val, complain);
3524 VEC_replace (tree, *values, i, val);
3527 if (typetail)
3528 typetail = TREE_CHAIN (typetail);
3531 if (typetail != 0 && typetail != void_list_node)
3533 /* See if there are default arguments that can be used. Because
3534 we hold default arguments in the FUNCTION_TYPE (which is so
3535 wrong), we can see default parameters here from deduced
3536 contexts (and via typeof) for indirect function calls.
3537 Fortunately we know whether we have a function decl to
3538 provide default arguments in a language conformant
3539 manner. */
3540 if (fndecl && TREE_PURPOSE (typetail)
3541 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3543 for (; typetail != void_list_node; ++i)
3545 tree parmval
3546 = convert_default_arg (TREE_VALUE (typetail),
3547 TREE_PURPOSE (typetail),
3548 fndecl, i, complain);
3550 if (parmval == error_mark_node)
3551 return -1;
3553 VEC_safe_push (tree, gc, *values, parmval);
3554 typetail = TREE_CHAIN (typetail);
3555 /* ends with `...'. */
3556 if (typetail == NULL_TREE)
3557 break;
3560 else
3562 if (complain & tf_error)
3563 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3564 return -1;
3568 return (int) i;
3571 /* Build a binary-operation expression, after performing default
3572 conversions on the operands. CODE is the kind of expression to
3573 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3574 are the tree codes which correspond to ARG1 and ARG2 when issuing
3575 warnings about possibly misplaced parentheses. They may differ
3576 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3577 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3578 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3579 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3580 ARG2_CODE as ERROR_MARK. */
3582 tree
3583 build_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code,
3584 tree arg2, enum tree_code arg2_code, tree *overload,
3585 tsubst_flags_t complain)
3587 tree orig_arg1;
3588 tree orig_arg2;
3589 tree expr;
3591 orig_arg1 = arg1;
3592 orig_arg2 = arg2;
3594 if (processing_template_decl)
3596 if (type_dependent_expression_p (arg1)
3597 || type_dependent_expression_p (arg2))
3598 return build_min_nt (code, arg1, arg2);
3599 arg1 = build_non_dependent_expr (arg1);
3600 arg2 = build_non_dependent_expr (arg2);
3603 if (code == DOTSTAR_EXPR)
3604 expr = build_m_component_ref (arg1, arg2, complain);
3605 else
3606 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3607 overload, complain);
3609 /* Check for cases such as x+y<<z which users are likely to
3610 misinterpret. But don't warn about obj << x + y, since that is a
3611 common idiom for I/O. */
3612 if (warn_parentheses
3613 && (complain & tf_warning)
3614 && !processing_template_decl
3615 && !error_operand_p (arg1)
3616 && !error_operand_p (arg2)
3617 && (code != LSHIFT_EXPR
3618 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3619 warn_about_parentheses (code, arg1_code, orig_arg1, arg2_code, orig_arg2);
3621 if (processing_template_decl && expr != error_mark_node)
3622 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3624 return expr;
3627 /* Build and return an ARRAY_REF expression. */
3629 tree
3630 build_x_array_ref (tree arg1, tree arg2, tsubst_flags_t complain)
3632 tree orig_arg1 = arg1;
3633 tree orig_arg2 = arg2;
3634 tree expr;
3636 if (processing_template_decl)
3638 if (type_dependent_expression_p (arg1)
3639 || type_dependent_expression_p (arg2))
3640 return build_min_nt (ARRAY_REF, arg1, arg2,
3641 NULL_TREE, NULL_TREE);
3642 arg1 = build_non_dependent_expr (arg1);
3643 arg2 = build_non_dependent_expr (arg2);
3646 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3647 /*overload=*/NULL, complain);
3649 if (processing_template_decl && expr != error_mark_node)
3650 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3651 NULL_TREE, NULL_TREE);
3652 return expr;
3655 /* Return whether OP is an expression of enum type cast to integer
3656 type. In C++ even unsigned enum types are cast to signed integer
3657 types. We do not want to issue warnings about comparisons between
3658 signed and unsigned types when one of the types is an enum type.
3659 Those warnings are always false positives in practice. */
3661 static bool
3662 enum_cast_to_int (tree op)
3664 if (TREE_CODE (op) == NOP_EXPR
3665 && TREE_TYPE (op) == integer_type_node
3666 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3667 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3668 return true;
3670 /* The cast may have been pushed into a COND_EXPR. */
3671 if (TREE_CODE (op) == COND_EXPR)
3672 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3673 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3675 return false;
3678 /* For the c-common bits. */
3679 tree
3680 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3681 int convert_p ATTRIBUTE_UNUSED)
3683 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3687 /* Build a binary-operation expression without default conversions.
3688 CODE is the kind of expression to build.
3689 LOCATION is the location_t of the operator in the source code.
3690 This function differs from `build' in several ways:
3691 the data type of the result is computed and recorded in it,
3692 warnings are generated if arg data types are invalid,
3693 special handling for addition and subtraction of pointers is known,
3694 and some optimization is done (operations on narrow ints
3695 are done in the narrower type when that gives the same result).
3696 Constant folding is also done before the result is returned.
3698 Note that the operands will never have enumeral types
3699 because either they have just had the default conversions performed
3700 or they have both just been converted to some other type in which
3701 the arithmetic is to be done.
3703 C++: must do special pointer arithmetic when implementing
3704 multiple inheritance, and deal with pointer to member functions. */
3706 tree
3707 cp_build_binary_op (location_t location,
3708 enum tree_code code, tree orig_op0, tree orig_op1,
3709 tsubst_flags_t complain)
3711 tree op0, op1;
3712 enum tree_code code0, code1;
3713 tree type0, type1;
3714 const char *invalid_op_diag;
3716 /* Expression code to give to the expression when it is built.
3717 Normally this is CODE, which is what the caller asked for,
3718 but in some special cases we change it. */
3719 enum tree_code resultcode = code;
3721 /* Data type in which the computation is to be performed.
3722 In the simplest cases this is the common type of the arguments. */
3723 tree result_type = NULL;
3725 /* Nonzero means operands have already been type-converted
3726 in whatever way is necessary.
3727 Zero means they need to be converted to RESULT_TYPE. */
3728 int converted = 0;
3730 /* Nonzero means create the expression with this type, rather than
3731 RESULT_TYPE. */
3732 tree build_type = 0;
3734 /* Nonzero means after finally constructing the expression
3735 convert it to this type. */
3736 tree final_type = 0;
3738 tree result;
3740 /* Nonzero if this is an operation like MIN or MAX which can
3741 safely be computed in short if both args are promoted shorts.
3742 Also implies COMMON.
3743 -1 indicates a bitwise operation; this makes a difference
3744 in the exact conditions for when it is safe to do the operation
3745 in a narrower mode. */
3746 int shorten = 0;
3748 /* Nonzero if this is a comparison operation;
3749 if both args are promoted shorts, compare the original shorts.
3750 Also implies COMMON. */
3751 int short_compare = 0;
3753 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3754 int common = 0;
3756 /* True if both operands have arithmetic type. */
3757 bool arithmetic_types_p;
3759 /* Apply default conversions. */
3760 op0 = orig_op0;
3761 op1 = orig_op1;
3763 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3764 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3765 || code == TRUTH_XOR_EXPR)
3767 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3768 op0 = decay_conversion (op0, complain);
3769 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3770 op1 = decay_conversion (op1, complain);
3772 else
3774 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3775 op0 = cp_default_conversion (op0, complain);
3776 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3777 op1 = cp_default_conversion (op1, complain);
3780 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3781 STRIP_TYPE_NOPS (op0);
3782 STRIP_TYPE_NOPS (op1);
3784 /* DTRT if one side is an overloaded function, but complain about it. */
3785 if (type_unknown_p (op0))
3787 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3788 if (t != error_mark_node)
3790 if (complain & tf_error)
3791 permerror (input_location, "assuming cast to type %qT from overloaded function",
3792 TREE_TYPE (t));
3793 op0 = t;
3796 if (type_unknown_p (op1))
3798 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3799 if (t != error_mark_node)
3801 if (complain & tf_error)
3802 permerror (input_location, "assuming cast to type %qT from overloaded function",
3803 TREE_TYPE (t));
3804 op1 = t;
3808 type0 = TREE_TYPE (op0);
3809 type1 = TREE_TYPE (op1);
3811 /* The expression codes of the data types of the arguments tell us
3812 whether the arguments are integers, floating, pointers, etc. */
3813 code0 = TREE_CODE (type0);
3814 code1 = TREE_CODE (type1);
3816 /* If an error was already reported for one of the arguments,
3817 avoid reporting another error. */
3818 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3819 return error_mark_node;
3821 if ((invalid_op_diag
3822 = targetm.invalid_binary_op (code, type0, type1)))
3824 error (invalid_op_diag);
3825 return error_mark_node;
3828 /* Issue warnings about peculiar, but valid, uses of NULL. */
3829 if ((orig_op0 == null_node || orig_op1 == null_node)
3830 /* It's reasonable to use pointer values as operands of &&
3831 and ||, so NULL is no exception. */
3832 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3833 && ( /* Both are NULL (or 0) and the operation was not a
3834 comparison or a pointer subtraction. */
3835 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3836 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3837 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3838 || (!null_ptr_cst_p (orig_op0)
3839 && !TYPE_PTR_P (type0) && !TYPE_PTR_TO_MEMBER_P (type0))
3840 || (!null_ptr_cst_p (orig_op1)
3841 && !TYPE_PTR_P (type1) && !TYPE_PTR_TO_MEMBER_P (type1)))
3842 && (complain & tf_warning))
3844 source_location loc =
3845 expansion_point_location_if_in_system_header (input_location);
3847 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
3850 switch (code)
3852 case MINUS_EXPR:
3853 /* Subtraction of two similar pointers.
3854 We must subtract them as integers, then divide by object size. */
3855 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3856 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3857 TREE_TYPE (type1)))
3858 return pointer_diff (op0, op1, common_pointer_type (type0, type1));
3859 /* In all other cases except pointer - int, the usual arithmetic
3860 rules apply. */
3861 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3863 common = 1;
3864 break;
3866 /* The pointer - int case is just like pointer + int; fall
3867 through. */
3868 case PLUS_EXPR:
3869 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3870 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3872 tree ptr_operand;
3873 tree int_operand;
3874 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3875 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3876 if (processing_template_decl)
3878 result_type = TREE_TYPE (ptr_operand);
3879 break;
3881 return cp_pointer_int_sum (code,
3882 ptr_operand,
3883 int_operand);
3885 common = 1;
3886 break;
3888 case MULT_EXPR:
3889 common = 1;
3890 break;
3892 case TRUNC_DIV_EXPR:
3893 case CEIL_DIV_EXPR:
3894 case FLOOR_DIV_EXPR:
3895 case ROUND_DIV_EXPR:
3896 case EXACT_DIV_EXPR:
3897 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3898 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3899 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3900 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
3902 enum tree_code tcode0 = code0, tcode1 = code1;
3904 warn_for_div_by_zero (location, op1);
3906 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
3907 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3908 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
3909 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3911 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
3912 resultcode = RDIV_EXPR;
3913 else
3914 /* When dividing two signed integers, we have to promote to int.
3915 unless we divide by a constant != -1. Note that default
3916 conversion will have been performed on the operands at this
3917 point, so we have to dig out the original type to find out if
3918 it was unsigned. */
3919 shorten = ((TREE_CODE (op0) == NOP_EXPR
3920 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3921 || (TREE_CODE (op1) == INTEGER_CST
3922 && ! integer_all_onesp (op1)));
3924 common = 1;
3926 break;
3928 case BIT_AND_EXPR:
3929 case BIT_IOR_EXPR:
3930 case BIT_XOR_EXPR:
3931 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3932 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3933 && !VECTOR_FLOAT_TYPE_P (type0)
3934 && !VECTOR_FLOAT_TYPE_P (type1)))
3935 shorten = -1;
3936 break;
3938 case TRUNC_MOD_EXPR:
3939 case FLOOR_MOD_EXPR:
3940 warn_for_div_by_zero (location, op1);
3942 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3943 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
3944 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
3945 common = 1;
3946 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3948 /* Although it would be tempting to shorten always here, that loses
3949 on some targets, since the modulo instruction is undefined if the
3950 quotient can't be represented in the computation mode. We shorten
3951 only if unsigned or if dividing by something we know != -1. */
3952 shorten = ((TREE_CODE (op0) == NOP_EXPR
3953 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3954 || (TREE_CODE (op1) == INTEGER_CST
3955 && ! integer_all_onesp (op1)));
3956 common = 1;
3958 break;
3960 case TRUTH_ANDIF_EXPR:
3961 case TRUTH_ORIF_EXPR:
3962 case TRUTH_AND_EXPR:
3963 case TRUTH_OR_EXPR:
3964 result_type = boolean_type_node;
3965 break;
3967 /* Shift operations: result has same type as first operand;
3968 always convert second operand to int.
3969 Also set SHORT_SHIFT if shifting rightward. */
3971 case RSHIFT_EXPR:
3972 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3974 result_type = type0;
3975 if (TREE_CODE (op1) == INTEGER_CST)
3977 if (tree_int_cst_lt (op1, integer_zero_node))
3979 if ((complain & tf_warning)
3980 && c_inhibit_evaluation_warnings == 0)
3981 warning (0, "right shift count is negative");
3983 else
3985 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0
3986 && (complain & tf_warning)
3987 && c_inhibit_evaluation_warnings == 0)
3988 warning (0, "right shift count >= width of type");
3991 /* Convert the shift-count to an integer, regardless of
3992 size of value being shifted. */
3993 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3994 op1 = cp_convert (integer_type_node, op1);
3995 /* Avoid converting op1 to result_type later. */
3996 converted = 1;
3998 break;
4000 case LSHIFT_EXPR:
4001 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4003 result_type = type0;
4004 if (TREE_CODE (op1) == INTEGER_CST)
4006 if (tree_int_cst_lt (op1, integer_zero_node))
4008 if ((complain & tf_warning)
4009 && c_inhibit_evaluation_warnings == 0)
4010 warning (0, "left shift count is negative");
4012 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4014 if ((complain & tf_warning)
4015 && c_inhibit_evaluation_warnings == 0)
4016 warning (0, "left shift count >= width of type");
4019 /* Convert the shift-count to an integer, regardless of
4020 size of value being shifted. */
4021 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4022 op1 = cp_convert (integer_type_node, op1);
4023 /* Avoid converting op1 to result_type later. */
4024 converted = 1;
4026 break;
4028 case RROTATE_EXPR:
4029 case LROTATE_EXPR:
4030 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4032 result_type = type0;
4033 if (TREE_CODE (op1) == INTEGER_CST)
4035 if (tree_int_cst_lt (op1, integer_zero_node))
4037 if (complain & tf_warning)
4038 warning (0, (code == LROTATE_EXPR)
4039 ? G_("left rotate count is negative")
4040 : G_("right rotate count is negative"));
4042 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4044 if (complain & tf_warning)
4045 warning (0, (code == LROTATE_EXPR)
4046 ? G_("left rotate count >= width of type")
4047 : G_("right rotate count >= width of type"));
4050 /* Convert the shift-count to an integer, regardless of
4051 size of value being shifted. */
4052 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4053 op1 = cp_convert (integer_type_node, op1);
4055 break;
4057 case EQ_EXPR:
4058 case NE_EXPR:
4059 if ((complain & tf_warning)
4060 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4061 warning (OPT_Wfloat_equal,
4062 "comparing floating point with == or != is unsafe");
4063 if ((complain & tf_warning)
4064 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4065 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4066 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4068 build_type = boolean_type_node;
4069 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4070 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4071 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4072 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4073 short_compare = 1;
4074 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4075 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
4076 result_type = composite_pointer_type (type0, type1, op0, op1,
4077 CPO_COMPARISON, complain);
4078 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
4079 && null_ptr_cst_p (op1))
4081 if (TREE_CODE (op0) == ADDR_EXPR
4082 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4084 if (complain & tf_warning)
4085 warning (OPT_Waddress, "the address of %qD will never be NULL",
4086 TREE_OPERAND (op0, 0));
4088 result_type = type0;
4090 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
4091 && null_ptr_cst_p (op0))
4093 if (TREE_CODE (op1) == ADDR_EXPR
4094 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4096 if (complain & tf_warning)
4097 warning (OPT_Waddress, "the address of %qD will never be NULL",
4098 TREE_OPERAND (op1, 0));
4100 result_type = type1;
4102 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4103 /* One of the operands must be of nullptr_t type. */
4104 result_type = TREE_TYPE (nullptr_node);
4105 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4107 result_type = type0;
4108 if (complain & tf_error)
4109 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4110 else
4111 return error_mark_node;
4113 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4115 result_type = type1;
4116 if (complain & tf_error)
4117 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4118 else
4119 return error_mark_node;
4121 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4123 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4124 == ptrmemfunc_vbit_in_delta)
4126 tree pfn0 = pfn_from_ptrmemfunc (op0);
4127 tree delta0 = delta_from_ptrmemfunc (op0);
4128 tree e1 = cp_build_binary_op (location,
4129 EQ_EXPR,
4130 pfn0,
4131 build_zero_cst (TREE_TYPE (pfn0)),
4132 complain);
4133 tree e2 = cp_build_binary_op (location,
4134 BIT_AND_EXPR,
4135 delta0,
4136 integer_one_node,
4137 complain);
4139 if ((complain & tf_warning)
4140 && c_inhibit_evaluation_warnings == 0
4141 && !NULLPTR_TYPE_P (TREE_TYPE (op1)))
4142 warning (OPT_Wzero_as_null_pointer_constant,
4143 "zero as null pointer constant");
4145 e2 = cp_build_binary_op (location,
4146 EQ_EXPR, e2, integer_zero_node,
4147 complain);
4148 op0 = cp_build_binary_op (location,
4149 TRUTH_ANDIF_EXPR, e1, e2,
4150 complain);
4151 op1 = cp_convert (TREE_TYPE (op0), integer_one_node);
4153 else
4155 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4156 op1 = cp_convert (TREE_TYPE (op0), op1);
4158 result_type = TREE_TYPE (op0);
4160 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4161 return cp_build_binary_op (location, code, op1, op0, complain);
4162 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4164 tree type;
4165 /* E will be the final comparison. */
4166 tree e;
4167 /* E1 and E2 are for scratch. */
4168 tree e1;
4169 tree e2;
4170 tree pfn0;
4171 tree pfn1;
4172 tree delta0;
4173 tree delta1;
4175 type = composite_pointer_type (type0, type1, op0, op1,
4176 CPO_COMPARISON, complain);
4178 if (!same_type_p (TREE_TYPE (op0), type))
4179 op0 = cp_convert_and_check (type, op0);
4180 if (!same_type_p (TREE_TYPE (op1), type))
4181 op1 = cp_convert_and_check (type, op1);
4183 if (op0 == error_mark_node || op1 == error_mark_node)
4184 return error_mark_node;
4186 if (TREE_SIDE_EFFECTS (op0))
4187 op0 = save_expr (op0);
4188 if (TREE_SIDE_EFFECTS (op1))
4189 op1 = save_expr (op1);
4191 pfn0 = pfn_from_ptrmemfunc (op0);
4192 pfn1 = pfn_from_ptrmemfunc (op1);
4193 delta0 = delta_from_ptrmemfunc (op0);
4194 delta1 = delta_from_ptrmemfunc (op1);
4195 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4196 == ptrmemfunc_vbit_in_delta)
4198 /* We generate:
4200 (op0.pfn == op1.pfn
4201 && ((op0.delta == op1.delta)
4202 || (!op0.pfn && op0.delta & 1 == 0
4203 && op1.delta & 1 == 0))
4205 The reason for the `!op0.pfn' bit is that a NULL
4206 pointer-to-member is any member with a zero PFN and
4207 LSB of the DELTA field is 0. */
4209 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4210 delta0,
4211 integer_one_node,
4212 complain);
4213 e1 = cp_build_binary_op (location,
4214 EQ_EXPR, e1, integer_zero_node,
4215 complain);
4216 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4217 delta1,
4218 integer_one_node,
4219 complain);
4220 e2 = cp_build_binary_op (location,
4221 EQ_EXPR, e2, integer_zero_node,
4222 complain);
4223 e1 = cp_build_binary_op (location,
4224 TRUTH_ANDIF_EXPR, e2, e1,
4225 complain);
4226 e2 = cp_build_binary_op (location, EQ_EXPR,
4227 pfn0,
4228 build_zero_cst (TREE_TYPE (pfn0)),
4229 complain);
4230 e2 = cp_build_binary_op (location,
4231 TRUTH_ANDIF_EXPR, e2, e1, complain);
4232 e1 = cp_build_binary_op (location,
4233 EQ_EXPR, delta0, delta1, complain);
4234 e1 = cp_build_binary_op (location,
4235 TRUTH_ORIF_EXPR, e1, e2, complain);
4237 else
4239 /* We generate:
4241 (op0.pfn == op1.pfn
4242 && (!op0.pfn || op0.delta == op1.delta))
4244 The reason for the `!op0.pfn' bit is that a NULL
4245 pointer-to-member is any member with a zero PFN; the
4246 DELTA field is unspecified. */
4248 e1 = cp_build_binary_op (location,
4249 EQ_EXPR, delta0, delta1, complain);
4250 e2 = cp_build_binary_op (location,
4251 EQ_EXPR,
4252 pfn0,
4253 build_zero_cst (TREE_TYPE (pfn0)),
4254 complain);
4255 e1 = cp_build_binary_op (location,
4256 TRUTH_ORIF_EXPR, e1, e2, complain);
4258 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4259 e = cp_build_binary_op (location,
4260 TRUTH_ANDIF_EXPR, e2, e1, complain);
4261 if (code == EQ_EXPR)
4262 return e;
4263 return cp_build_binary_op (location,
4264 EQ_EXPR, e, integer_zero_node, complain);
4266 else
4268 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4269 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4270 type1));
4271 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4272 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4273 type0));
4276 break;
4278 case MAX_EXPR:
4279 case MIN_EXPR:
4280 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4281 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4282 shorten = 1;
4283 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4284 result_type = composite_pointer_type (type0, type1, op0, op1,
4285 CPO_COMPARISON, complain);
4286 break;
4288 case LE_EXPR:
4289 case GE_EXPR:
4290 case LT_EXPR:
4291 case GT_EXPR:
4292 if (TREE_CODE (orig_op0) == STRING_CST
4293 || TREE_CODE (orig_op1) == STRING_CST)
4295 if (complain & tf_warning)
4296 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4299 build_type = boolean_type_node;
4300 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4301 || code0 == ENUMERAL_TYPE)
4302 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4303 || code1 == ENUMERAL_TYPE))
4304 short_compare = 1;
4305 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4306 result_type = composite_pointer_type (type0, type1, op0, op1,
4307 CPO_COMPARISON, complain);
4308 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4310 result_type = type0;
4311 if (extra_warnings && (complain & tf_warning))
4312 warning (OPT_Wextra,
4313 "ordered comparison of pointer with integer zero");
4315 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4317 result_type = type1;
4318 if (extra_warnings && (complain & tf_warning))
4319 warning (OPT_Wextra,
4320 "ordered comparison of pointer with integer zero");
4322 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4323 /* One of the operands must be of nullptr_t type. */
4324 result_type = TREE_TYPE (nullptr_node);
4325 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4327 result_type = type0;
4328 if (complain & tf_error)
4329 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4330 else
4331 return error_mark_node;
4333 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4335 result_type = type1;
4336 if (complain & tf_error)
4337 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4338 else
4339 return error_mark_node;
4341 break;
4343 case UNORDERED_EXPR:
4344 case ORDERED_EXPR:
4345 case UNLT_EXPR:
4346 case UNLE_EXPR:
4347 case UNGT_EXPR:
4348 case UNGE_EXPR:
4349 case UNEQ_EXPR:
4350 build_type = integer_type_node;
4351 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4353 if (complain & tf_error)
4354 error ("unordered comparison on non-floating point argument");
4355 return error_mark_node;
4357 common = 1;
4358 break;
4360 default:
4361 break;
4364 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4365 || code0 == ENUMERAL_TYPE)
4366 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4367 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4368 arithmetic_types_p = 1;
4369 else
4371 arithmetic_types_p = 0;
4372 /* Vector arithmetic is only allowed when both sides are vectors. */
4373 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4375 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4376 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4377 TREE_TYPE (type1)))
4379 binary_op_error (location, code, type0, type1);
4380 return error_mark_node;
4382 arithmetic_types_p = 1;
4385 /* Determine the RESULT_TYPE, if it is not already known. */
4386 if (!result_type
4387 && arithmetic_types_p
4388 && (shorten || common || short_compare))
4390 result_type = cp_common_type (type0, type1);
4391 do_warn_double_promotion (result_type, type0, type1,
4392 "implicit conversion from %qT to %qT "
4393 "to match other operand of binary "
4394 "expression",
4395 location);
4398 if (!result_type)
4400 if (complain & tf_error)
4401 error ("invalid operands of types %qT and %qT to binary %qO",
4402 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4403 return error_mark_node;
4406 /* If we're in a template, the only thing we need to know is the
4407 RESULT_TYPE. */
4408 if (processing_template_decl)
4410 /* Since the middle-end checks the type when doing a build2, we
4411 need to build the tree in pieces. This built tree will never
4412 get out of the front-end as we replace it when instantiating
4413 the template. */
4414 tree tmp = build2 (resultcode,
4415 build_type ? build_type : result_type,
4416 NULL_TREE, op1);
4417 TREE_OPERAND (tmp, 0) = op0;
4418 return tmp;
4421 if (arithmetic_types_p)
4423 bool first_complex = (code0 == COMPLEX_TYPE);
4424 bool second_complex = (code1 == COMPLEX_TYPE);
4425 int none_complex = (!first_complex && !second_complex);
4427 /* Adapted from patch for c/24581. */
4428 if (first_complex != second_complex
4429 && (code == PLUS_EXPR
4430 || code == MINUS_EXPR
4431 || code == MULT_EXPR
4432 || (code == TRUNC_DIV_EXPR && first_complex))
4433 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4434 && flag_signed_zeros)
4436 /* An operation on mixed real/complex operands must be
4437 handled specially, but the language-independent code can
4438 more easily optimize the plain complex arithmetic if
4439 -fno-signed-zeros. */
4440 tree real_type = TREE_TYPE (result_type);
4441 tree real, imag;
4442 if (first_complex)
4444 if (TREE_TYPE (op0) != result_type)
4445 op0 = cp_convert_and_check (result_type, op0);
4446 if (TREE_TYPE (op1) != real_type)
4447 op1 = cp_convert_and_check (real_type, op1);
4449 else
4451 if (TREE_TYPE (op0) != real_type)
4452 op0 = cp_convert_and_check (real_type, op0);
4453 if (TREE_TYPE (op1) != result_type)
4454 op1 = cp_convert_and_check (result_type, op1);
4456 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4457 return error_mark_node;
4458 if (first_complex)
4460 op0 = save_expr (op0);
4461 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4462 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4463 switch (code)
4465 case MULT_EXPR:
4466 case TRUNC_DIV_EXPR:
4467 op1 = save_expr (op1);
4468 imag = build2 (resultcode, real_type, imag, op1);
4469 /* Fall through. */
4470 case PLUS_EXPR:
4471 case MINUS_EXPR:
4472 real = build2 (resultcode, real_type, real, op1);
4473 break;
4474 default:
4475 gcc_unreachable();
4478 else
4480 op1 = save_expr (op1);
4481 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4482 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4483 switch (code)
4485 case MULT_EXPR:
4486 op0 = save_expr (op0);
4487 imag = build2 (resultcode, real_type, op0, imag);
4488 /* Fall through. */
4489 case PLUS_EXPR:
4490 real = build2 (resultcode, real_type, op0, real);
4491 break;
4492 case MINUS_EXPR:
4493 real = build2 (resultcode, real_type, op0, real);
4494 imag = build1 (NEGATE_EXPR, real_type, imag);
4495 break;
4496 default:
4497 gcc_unreachable();
4500 real = fold_if_not_in_template (real);
4501 imag = fold_if_not_in_template (imag);
4502 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4503 result = fold_if_not_in_template (result);
4504 return result;
4507 /* For certain operations (which identify themselves by shorten != 0)
4508 if both args were extended from the same smaller type,
4509 do the arithmetic in that type and then extend.
4511 shorten !=0 and !=1 indicates a bitwise operation.
4512 For them, this optimization is safe only if
4513 both args are zero-extended or both are sign-extended.
4514 Otherwise, we might change the result.
4515 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4516 but calculated in (unsigned short) it would be (unsigned short)-1. */
4518 if (shorten && none_complex)
4520 final_type = result_type;
4521 result_type = shorten_binary_op (result_type, op0, op1,
4522 shorten == -1);
4525 /* Comparison operations are shortened too but differently.
4526 They identify themselves by setting short_compare = 1. */
4528 if (short_compare)
4530 /* Don't write &op0, etc., because that would prevent op0
4531 from being kept in a register.
4532 Instead, make copies of the our local variables and
4533 pass the copies by reference, then copy them back afterward. */
4534 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4535 enum tree_code xresultcode = resultcode;
4536 tree val
4537 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4538 if (val != 0)
4539 return cp_convert (boolean_type_node, val);
4540 op0 = xop0, op1 = xop1;
4541 converted = 1;
4542 resultcode = xresultcode;
4545 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4546 && warn_sign_compare
4547 /* Do not warn until the template is instantiated; we cannot
4548 bound the ranges of the arguments until that point. */
4549 && !processing_template_decl
4550 && (complain & tf_warning)
4551 && c_inhibit_evaluation_warnings == 0
4552 /* Even unsigned enum types promote to signed int. We don't
4553 want to issue -Wsign-compare warnings for this case. */
4554 && !enum_cast_to_int (orig_op0)
4555 && !enum_cast_to_int (orig_op1))
4557 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
4558 result_type, resultcode);
4562 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4563 Then the expression will be built.
4564 It will be given type FINAL_TYPE if that is nonzero;
4565 otherwise, it will be given type RESULT_TYPE. */
4566 if (! converted)
4568 if (TREE_TYPE (op0) != result_type)
4569 op0 = cp_convert_and_check (result_type, op0);
4570 if (TREE_TYPE (op1) != result_type)
4571 op1 = cp_convert_and_check (result_type, op1);
4573 if (op0 == error_mark_node || op1 == error_mark_node)
4574 return error_mark_node;
4577 if (build_type == NULL_TREE)
4578 build_type = result_type;
4580 result = build2 (resultcode, build_type, op0, op1);
4581 result = fold_if_not_in_template (result);
4582 if (final_type != 0)
4583 result = cp_convert (final_type, result);
4585 if (TREE_OVERFLOW_P (result)
4586 && !TREE_OVERFLOW_P (op0)
4587 && !TREE_OVERFLOW_P (op1))
4588 overflow_warning (location, result);
4590 return result;
4593 /* Return a tree for the sum or difference (RESULTCODE says which)
4594 of pointer PTROP and integer INTOP. */
4596 static tree
4597 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4599 tree res_type = TREE_TYPE (ptrop);
4601 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4602 in certain circumstance (when it's valid to do so). So we need
4603 to make sure it's complete. We don't need to check here, if we
4604 can actually complete it at all, as those checks will be done in
4605 pointer_int_sum() anyway. */
4606 complete_type (TREE_TYPE (res_type));
4608 return pointer_int_sum (input_location, resultcode, ptrop,
4609 fold_if_not_in_template (intop));
4612 /* Return a tree for the difference of pointers OP0 and OP1.
4613 The resulting tree has type int. */
4615 static tree
4616 pointer_diff (tree op0, tree op1, tree ptrtype)
4618 tree result;
4619 tree restype = ptrdiff_type_node;
4620 tree target_type = TREE_TYPE (ptrtype);
4622 if (!complete_type_or_else (target_type, NULL_TREE))
4623 return error_mark_node;
4625 if (TREE_CODE (target_type) == VOID_TYPE)
4626 permerror (input_location, "ISO C++ forbids using pointer of type %<void *%> in subtraction");
4627 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4628 permerror (input_location, "ISO C++ forbids using pointer to a function in subtraction");
4629 if (TREE_CODE (target_type) == METHOD_TYPE)
4630 permerror (input_location, "ISO C++ forbids using pointer to a method in subtraction");
4632 /* First do the subtraction as integers;
4633 then drop through to build the divide operator. */
4635 op0 = cp_build_binary_op (input_location,
4636 MINUS_EXPR,
4637 cp_convert (restype, op0),
4638 cp_convert (restype, op1),
4639 tf_warning_or_error);
4641 /* This generates an error if op1 is a pointer to an incomplete type. */
4642 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4643 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4645 op1 = (TYPE_PTROB_P (ptrtype)
4646 ? size_in_bytes (target_type)
4647 : integer_one_node);
4649 /* Do the division. */
4651 result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4652 return fold_if_not_in_template (result);
4655 /* Construct and perhaps optimize a tree representation
4656 for a unary operation. CODE, a tree_code, specifies the operation
4657 and XARG is the operand. */
4659 tree
4660 build_x_unary_op (enum tree_code code, tree xarg, tsubst_flags_t complain)
4662 tree orig_expr = xarg;
4663 tree exp;
4664 int ptrmem = 0;
4666 if (processing_template_decl)
4668 if (type_dependent_expression_p (xarg))
4669 return build_min_nt (code, xarg, NULL_TREE);
4671 xarg = build_non_dependent_expr (xarg);
4674 exp = NULL_TREE;
4676 /* [expr.unary.op] says:
4678 The address of an object of incomplete type can be taken.
4680 (And is just the ordinary address operator, not an overloaded
4681 "operator &".) However, if the type is a template
4682 specialization, we must complete the type at this point so that
4683 an overloaded "operator &" will be available if required. */
4684 if (code == ADDR_EXPR
4685 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4686 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4687 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4688 || (TREE_CODE (xarg) == OFFSET_REF)))
4689 /* Don't look for a function. */;
4690 else
4691 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
4692 /*overload=*/NULL, complain);
4693 if (!exp && code == ADDR_EXPR)
4695 if (is_overloaded_fn (xarg))
4697 tree fn = get_first_fn (xarg);
4698 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4700 error (DECL_CONSTRUCTOR_P (fn)
4701 ? G_("taking address of constructor %qE")
4702 : G_("taking address of destructor %qE"),
4703 xarg);
4704 return error_mark_node;
4708 /* A pointer to member-function can be formed only by saying
4709 &X::mf. */
4710 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4711 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4713 if (TREE_CODE (xarg) != OFFSET_REF
4714 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4716 error ("invalid use of %qE to form a pointer-to-member-function",
4717 xarg);
4718 if (TREE_CODE (xarg) != OFFSET_REF)
4719 inform (input_location, " a qualified-id is required");
4720 return error_mark_node;
4722 else
4724 error ("parentheses around %qE cannot be used to form a"
4725 " pointer-to-member-function",
4726 xarg);
4727 PTRMEM_OK_P (xarg) = 1;
4731 if (TREE_CODE (xarg) == OFFSET_REF)
4733 ptrmem = PTRMEM_OK_P (xarg);
4735 if (!ptrmem && !flag_ms_extensions
4736 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4738 /* A single non-static member, make sure we don't allow a
4739 pointer-to-member. */
4740 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4741 TREE_OPERAND (xarg, 0),
4742 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4743 PTRMEM_OK_P (xarg) = ptrmem;
4747 exp = cp_build_addr_expr_strict (xarg, complain);
4750 if (processing_template_decl && exp != error_mark_node)
4751 exp = build_min_non_dep (code, exp, orig_expr,
4752 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4753 if (TREE_CODE (exp) == ADDR_EXPR)
4754 PTRMEM_OK_P (exp) = ptrmem;
4755 return exp;
4758 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4759 constants, where a null value is represented by an INTEGER_CST of
4760 -1. */
4762 tree
4763 cp_truthvalue_conversion (tree expr)
4765 tree type = TREE_TYPE (expr);
4766 if (TYPE_PTRMEM_P (type))
4767 return build_binary_op (EXPR_LOCATION (expr),
4768 NE_EXPR, expr, nullptr_node, 1);
4769 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
4771 /* With -Wzero-as-null-pointer-constant do not warn for an
4772 'if (p)' or a 'while (!p)', where p is a pointer. */
4773 tree ret;
4774 ++c_inhibit_evaluation_warnings;
4775 ret = c_common_truthvalue_conversion (input_location, expr);
4776 --c_inhibit_evaluation_warnings;
4777 return ret;
4779 else
4780 return c_common_truthvalue_conversion (input_location, expr);
4783 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4785 tree
4786 condition_conversion (tree expr)
4788 tree t;
4789 if (processing_template_decl)
4790 return expr;
4791 t = perform_implicit_conversion_flags (boolean_type_node, expr,
4792 tf_warning_or_error, LOOKUP_NORMAL);
4793 t = fold_build_cleanup_point_expr (boolean_type_node, t);
4794 return t;
4797 /* Returns the address of T. This function will fold away
4798 ADDR_EXPR of INDIRECT_REF. */
4800 tree
4801 build_address (tree t)
4803 if (error_operand_p (t) || !cxx_mark_addressable (t))
4804 return error_mark_node;
4805 t = build_fold_addr_expr (t);
4806 if (TREE_CODE (t) != ADDR_EXPR)
4807 t = rvalue (t);
4808 return t;
4811 /* Returns the address of T with type TYPE. */
4813 tree
4814 build_typed_address (tree t, tree type)
4816 if (error_operand_p (t) || !cxx_mark_addressable (t))
4817 return error_mark_node;
4818 t = build_fold_addr_expr_with_type (t, type);
4819 if (TREE_CODE (t) != ADDR_EXPR)
4820 t = rvalue (t);
4821 return t;
4824 /* Return a NOP_EXPR converting EXPR to TYPE. */
4826 tree
4827 build_nop (tree type, tree expr)
4829 if (type == error_mark_node || error_operand_p (expr))
4830 return expr;
4831 return build1 (NOP_EXPR, type, expr);
4834 /* Take the address of ARG, whatever that means under C++ semantics.
4835 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
4836 and class rvalues as well.
4838 Nothing should call this function directly; instead, callers should use
4839 cp_build_addr_expr or cp_build_addr_expr_strict. */
4841 static tree
4842 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
4844 tree argtype;
4845 tree val;
4847 if (!arg || error_operand_p (arg))
4848 return error_mark_node;
4850 arg = mark_lvalue_use (arg);
4851 argtype = lvalue_type (arg);
4853 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4854 || !IDENTIFIER_OPNAME_P (arg));
4856 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4857 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4859 /* They're trying to take the address of a unique non-static
4860 member function. This is ill-formed (except in MS-land),
4861 but let's try to DTRT.
4862 Note: We only handle unique functions here because we don't
4863 want to complain if there's a static overload; non-unique
4864 cases will be handled by instantiate_type. But we need to
4865 handle this case here to allow casts on the resulting PMF.
4866 We could defer this in non-MS mode, but it's easier to give
4867 a useful error here. */
4869 /* Inside constant member functions, the `this' pointer
4870 contains an extra const qualifier. TYPE_MAIN_VARIANT
4871 is used here to remove this const from the diagnostics
4872 and the created OFFSET_REF. */
4873 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4874 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4875 mark_used (fn);
4877 if (! flag_ms_extensions)
4879 tree name = DECL_NAME (fn);
4880 if (!(complain & tf_error))
4881 return error_mark_node;
4882 else if (current_class_type
4883 && TREE_OPERAND (arg, 0) == current_class_ref)
4884 /* An expression like &memfn. */
4885 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
4886 " or parenthesized non-static member function to form"
4887 " a pointer to member function. Say %<&%T::%D%>",
4888 base, name);
4889 else
4890 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
4891 " function to form a pointer to member function."
4892 " Say %<&%T::%D%>",
4893 base, name);
4895 arg = build_offset_ref (base, fn, /*address_p=*/true);
4898 /* Uninstantiated types are all functions. Taking the
4899 address of a function is a no-op, so just return the
4900 argument. */
4901 if (type_unknown_p (arg))
4902 return build1 (ADDR_EXPR, unknown_type_node, arg);
4904 if (TREE_CODE (arg) == OFFSET_REF)
4905 /* We want a pointer to member; bypass all the code for actually taking
4906 the address of something. */
4907 goto offset_ref;
4909 /* Anything not already handled and not a true memory reference
4910 is an error. */
4911 if (TREE_CODE (argtype) != FUNCTION_TYPE
4912 && TREE_CODE (argtype) != METHOD_TYPE)
4914 cp_lvalue_kind kind = lvalue_kind (arg);
4915 if (kind == clk_none)
4917 if (complain & tf_error)
4918 lvalue_error (input_location, lv_addressof);
4919 return error_mark_node;
4921 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
4923 if (!(complain & tf_error))
4924 return error_mark_node;
4925 if (kind & clk_class)
4926 /* Make this a permerror because we used to accept it. */
4927 permerror (input_location, "taking address of temporary");
4928 else
4929 error ("taking address of xvalue (rvalue reference)");
4933 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4935 tree type = build_pointer_type (TREE_TYPE (argtype));
4936 arg = build1 (CONVERT_EXPR, type, arg);
4937 return arg;
4939 else if (pedantic && DECL_MAIN_P (arg))
4941 /* ARM $3.4 */
4942 /* Apparently a lot of autoconf scripts for C++ packages do this,
4943 so only complain if -Wpedantic. */
4944 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
4945 pedwarn (input_location, OPT_Wpedantic,
4946 "ISO C++ forbids taking address of function %<::main%>");
4947 else if (flag_pedantic_errors)
4948 return error_mark_node;
4951 /* Let &* cancel out to simplify resulting code. */
4952 if (TREE_CODE (arg) == INDIRECT_REF)
4954 /* We don't need to have `current_class_ptr' wrapped in a
4955 NON_LVALUE_EXPR node. */
4956 if (arg == current_class_ref)
4957 return current_class_ptr;
4959 arg = TREE_OPERAND (arg, 0);
4960 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4962 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
4963 arg = build1 (CONVERT_EXPR, type, arg);
4965 else
4966 /* Don't let this be an lvalue. */
4967 arg = rvalue (arg);
4968 return arg;
4971 /* ??? Cope with user tricks that amount to offsetof. */
4972 if (TREE_CODE (argtype) != FUNCTION_TYPE
4973 && TREE_CODE (argtype) != METHOD_TYPE
4974 && argtype != unknown_type_node
4975 && (val = get_base_address (arg))
4976 && COMPLETE_TYPE_P (TREE_TYPE (val))
4977 && TREE_CODE (val) == INDIRECT_REF
4978 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4980 tree type = build_pointer_type (argtype);
4981 return fold_convert (type, fold_offsetof_1 (arg));
4984 /* Handle complex lvalues (when permitted)
4985 by reduction to simpler cases. */
4986 val = unary_complex_lvalue (ADDR_EXPR, arg);
4987 if (val != 0)
4988 return val;
4990 switch (TREE_CODE (arg))
4992 CASE_CONVERT:
4993 case FLOAT_EXPR:
4994 case FIX_TRUNC_EXPR:
4995 /* Even if we're not being pedantic, we cannot allow this
4996 extension when we're instantiating in a SFINAE
4997 context. */
4998 if (! lvalue_p (arg) && complain == tf_none)
5000 if (complain & tf_error)
5001 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5002 else
5003 return error_mark_node;
5005 break;
5007 case BASELINK:
5008 arg = BASELINK_FUNCTIONS (arg);
5009 /* Fall through. */
5011 case OVERLOAD:
5012 arg = OVL_CURRENT (arg);
5013 break;
5015 case OFFSET_REF:
5016 offset_ref:
5017 /* Turn a reference to a non-static data member into a
5018 pointer-to-member. */
5020 tree type;
5021 tree t;
5023 gcc_assert (PTRMEM_OK_P (arg));
5025 t = TREE_OPERAND (arg, 1);
5026 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5028 if (complain & tf_error)
5029 error ("cannot create pointer to reference member %qD", t);
5030 return error_mark_node;
5033 type = build_ptrmem_type (context_for_name_lookup (t),
5034 TREE_TYPE (t));
5035 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5036 return t;
5039 default:
5040 break;
5043 if (argtype != error_mark_node)
5044 argtype = build_pointer_type (argtype);
5046 /* In a template, we are processing a non-dependent expression
5047 so we can just form an ADDR_EXPR with the correct type. */
5048 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5050 val = build_address (arg);
5051 if (TREE_CODE (arg) == OFFSET_REF)
5052 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5054 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5056 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5058 /* We can only get here with a single static member
5059 function. */
5060 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5061 && DECL_STATIC_FUNCTION_P (fn));
5062 mark_used (fn);
5063 val = build_address (fn);
5064 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5065 /* Do not lose object's side effects. */
5066 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5067 TREE_OPERAND (arg, 0), val);
5069 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5071 if (complain & tf_error)
5072 error ("attempt to take address of bit-field structure member %qD",
5073 TREE_OPERAND (arg, 1));
5074 return error_mark_node;
5076 else
5078 tree object = TREE_OPERAND (arg, 0);
5079 tree field = TREE_OPERAND (arg, 1);
5080 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5081 (TREE_TYPE (object), decl_type_context (field)));
5082 val = build_address (arg);
5085 if (TREE_CODE (argtype) == POINTER_TYPE
5086 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5088 build_ptrmemfunc_type (argtype);
5089 val = build_ptrmemfunc (argtype, val, 0,
5090 /*c_cast_p=*/false,
5091 complain);
5094 return val;
5097 /* Take the address of ARG if it has one, even if it's an rvalue. */
5099 tree
5100 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5102 return cp_build_addr_expr_1 (arg, 0, complain);
5105 /* Take the address of ARG, but only if it's an lvalue. */
5107 tree
5108 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5110 return cp_build_addr_expr_1 (arg, 1, complain);
5113 /* C++: Must handle pointers to members.
5115 Perhaps type instantiation should be extended to handle conversion
5116 from aggregates to types we don't yet know we want? (Or are those
5117 cases typically errors which should be reported?)
5119 NOCONVERT nonzero suppresses the default promotions
5120 (such as from short to int). */
5122 tree
5123 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5124 tsubst_flags_t complain)
5126 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5127 tree arg = xarg;
5128 tree argtype = 0;
5129 const char *errstring = NULL;
5130 tree val;
5131 const char *invalid_op_diag;
5133 if (!arg || error_operand_p (arg))
5134 return error_mark_node;
5136 if ((invalid_op_diag
5137 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5138 ? CONVERT_EXPR
5139 : code),
5140 TREE_TYPE (xarg))))
5142 error (invalid_op_diag);
5143 return error_mark_node;
5146 switch (code)
5148 case UNARY_PLUS_EXPR:
5149 case NEGATE_EXPR:
5151 int flags = WANT_ARITH | WANT_ENUM;
5152 /* Unary plus (but not unary minus) is allowed on pointers. */
5153 if (code == UNARY_PLUS_EXPR)
5154 flags |= WANT_POINTER;
5155 arg = build_expr_type_conversion (flags, arg, true);
5156 if (!arg)
5157 errstring = (code == NEGATE_EXPR
5158 ? _("wrong type argument to unary minus")
5159 : _("wrong type argument to unary plus"));
5160 else
5162 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5163 arg = perform_integral_promotions (arg);
5165 /* Make sure the result is not an lvalue: a unary plus or minus
5166 expression is always a rvalue. */
5167 arg = rvalue (arg);
5170 break;
5172 case BIT_NOT_EXPR:
5173 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5175 code = CONJ_EXPR;
5176 if (!noconvert)
5178 arg = cp_default_conversion (arg, complain);
5179 if (arg == error_mark_node)
5180 return error_mark_node;
5183 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5184 | WANT_VECTOR_OR_COMPLEX,
5185 arg, true)))
5186 errstring = _("wrong type argument to bit-complement");
5187 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5188 arg = perform_integral_promotions (arg);
5189 break;
5191 case ABS_EXPR:
5192 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5193 errstring = _("wrong type argument to abs");
5194 else if (!noconvert)
5196 arg = cp_default_conversion (arg, complain);
5197 if (arg == error_mark_node)
5198 return error_mark_node;
5200 break;
5202 case CONJ_EXPR:
5203 /* Conjugating a real value is a no-op, but allow it anyway. */
5204 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5205 errstring = _("wrong type argument to conjugation");
5206 else if (!noconvert)
5208 arg = cp_default_conversion (arg, complain);
5209 if (arg == error_mark_node)
5210 return error_mark_node;
5212 break;
5214 case TRUTH_NOT_EXPR:
5215 arg = perform_implicit_conversion (boolean_type_node, arg,
5216 complain);
5217 val = invert_truthvalue_loc (input_location, arg);
5218 if (arg != error_mark_node)
5219 return val;
5220 errstring = _("in argument to unary !");
5221 break;
5223 case NOP_EXPR:
5224 break;
5226 case REALPART_EXPR:
5227 case IMAGPART_EXPR:
5228 arg = build_real_imag_expr (input_location, code, arg);
5229 if (arg == error_mark_node)
5230 return arg;
5231 else
5232 return fold_if_not_in_template (arg);
5234 case PREINCREMENT_EXPR:
5235 case POSTINCREMENT_EXPR:
5236 case PREDECREMENT_EXPR:
5237 case POSTDECREMENT_EXPR:
5238 /* Handle complex lvalues (when permitted)
5239 by reduction to simpler cases. */
5241 val = unary_complex_lvalue (code, arg);
5242 if (val != 0)
5243 return val;
5245 arg = mark_lvalue_use (arg);
5247 /* Increment or decrement the real part of the value,
5248 and don't change the imaginary part. */
5249 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5251 tree real, imag;
5253 arg = stabilize_reference (arg);
5254 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5255 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5256 real = cp_build_unary_op (code, real, 1, complain);
5257 if (real == error_mark_node || imag == error_mark_node)
5258 return error_mark_node;
5259 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5260 real, imag);
5263 /* Report invalid types. */
5265 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5266 arg, true)))
5268 if (code == PREINCREMENT_EXPR)
5269 errstring = _("no pre-increment operator for type");
5270 else if (code == POSTINCREMENT_EXPR)
5271 errstring = _("no post-increment operator for type");
5272 else if (code == PREDECREMENT_EXPR)
5273 errstring = _("no pre-decrement operator for type");
5274 else
5275 errstring = _("no post-decrement operator for type");
5276 break;
5278 else if (arg == error_mark_node)
5279 return error_mark_node;
5281 /* Report something read-only. */
5283 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5284 || TREE_READONLY (arg))
5286 if (complain & tf_error)
5287 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5288 || code == POSTINCREMENT_EXPR)
5289 ? lv_increment : lv_decrement));
5290 else
5291 return error_mark_node;
5295 tree inc;
5296 tree declared_type = unlowered_expr_type (arg);
5298 argtype = TREE_TYPE (arg);
5300 /* ARM $5.2.5 last annotation says this should be forbidden. */
5301 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5303 if (complain & tf_error)
5304 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5305 ? G_("ISO C++ forbids incrementing an enum")
5306 : G_("ISO C++ forbids decrementing an enum"));
5307 else
5308 return error_mark_node;
5311 /* Compute the increment. */
5313 if (TREE_CODE (argtype) == POINTER_TYPE)
5315 tree type = complete_type (TREE_TYPE (argtype));
5317 if (!COMPLETE_OR_VOID_TYPE_P (type))
5319 if (complain & tf_error)
5320 error (((code == PREINCREMENT_EXPR
5321 || code == POSTINCREMENT_EXPR))
5322 ? G_("cannot increment a pointer to incomplete type %qT")
5323 : G_("cannot decrement a pointer to incomplete type %qT"),
5324 TREE_TYPE (argtype));
5325 else
5326 return error_mark_node;
5328 else if ((pedantic || warn_pointer_arith)
5329 && !TYPE_PTROB_P (argtype))
5331 if (complain & tf_error)
5332 permerror (input_location, (code == PREINCREMENT_EXPR
5333 || code == POSTINCREMENT_EXPR)
5334 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5335 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5336 argtype);
5337 else
5338 return error_mark_node;
5341 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5343 else
5344 inc = integer_one_node;
5346 inc = cp_convert (argtype, inc);
5348 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5349 need to ask Objective-C to build the increment or decrement
5350 expression for it. */
5351 if (objc_is_property_ref (arg))
5352 return objc_build_incr_expr_for_property_ref (input_location, code,
5353 arg, inc);
5355 /* Complain about anything else that is not a true lvalue. */
5356 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5357 || code == POSTINCREMENT_EXPR)
5358 ? lv_increment : lv_decrement),
5359 complain))
5360 return error_mark_node;
5362 /* Forbid using -- on `bool'. */
5363 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5365 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5367 if (complain & tf_error)
5368 error ("invalid use of Boolean expression as operand "
5369 "to %<operator--%>");
5370 return error_mark_node;
5372 val = boolean_increment (code, arg);
5374 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5375 /* An rvalue has no cv-qualifiers. */
5376 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5377 else
5378 val = build2 (code, TREE_TYPE (arg), arg, inc);
5380 TREE_SIDE_EFFECTS (val) = 1;
5381 return val;
5384 case ADDR_EXPR:
5385 /* Note that this operation never does default_conversion
5386 regardless of NOCONVERT. */
5387 return cp_build_addr_expr (arg, complain);
5389 default:
5390 break;
5393 if (!errstring)
5395 if (argtype == 0)
5396 argtype = TREE_TYPE (arg);
5397 return fold_if_not_in_template (build1 (code, argtype, arg));
5400 if (complain & tf_error)
5401 error ("%s", errstring);
5402 return error_mark_node;
5405 /* Hook for the c-common bits that build a unary op. */
5406 tree
5407 build_unary_op (location_t location ATTRIBUTE_UNUSED,
5408 enum tree_code code, tree xarg, int noconvert)
5410 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5413 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5414 for certain kinds of expressions which are not really lvalues
5415 but which we can accept as lvalues.
5417 If ARG is not a kind of expression we can handle, return
5418 NULL_TREE. */
5420 tree
5421 unary_complex_lvalue (enum tree_code code, tree arg)
5423 /* Inside a template, making these kinds of adjustments is
5424 pointless; we are only concerned with the type of the
5425 expression. */
5426 if (processing_template_decl)
5427 return NULL_TREE;
5429 /* Handle (a, b) used as an "lvalue". */
5430 if (TREE_CODE (arg) == COMPOUND_EXPR)
5432 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5433 tf_warning_or_error);
5434 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5435 TREE_OPERAND (arg, 0), real_result);
5438 /* Handle (a ? b : c) used as an "lvalue". */
5439 if (TREE_CODE (arg) == COND_EXPR
5440 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5441 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5443 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5444 if (TREE_CODE (arg) == MODIFY_EXPR
5445 || TREE_CODE (arg) == PREINCREMENT_EXPR
5446 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5448 tree lvalue = TREE_OPERAND (arg, 0);
5449 if (TREE_SIDE_EFFECTS (lvalue))
5451 lvalue = stabilize_reference (lvalue);
5452 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5453 lvalue, TREE_OPERAND (arg, 1));
5455 return unary_complex_lvalue
5456 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5459 if (code != ADDR_EXPR)
5460 return NULL_TREE;
5462 /* Handle (a = b) used as an "lvalue" for `&'. */
5463 if (TREE_CODE (arg) == MODIFY_EXPR
5464 || TREE_CODE (arg) == INIT_EXPR)
5466 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5467 tf_warning_or_error);
5468 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5469 arg, real_result);
5470 TREE_NO_WARNING (arg) = 1;
5471 return arg;
5474 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5475 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5476 || TREE_CODE (arg) == OFFSET_REF)
5477 return NULL_TREE;
5479 /* We permit compiler to make function calls returning
5480 objects of aggregate type look like lvalues. */
5482 tree targ = arg;
5484 if (TREE_CODE (targ) == SAVE_EXPR)
5485 targ = TREE_OPERAND (targ, 0);
5487 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5489 if (TREE_CODE (arg) == SAVE_EXPR)
5490 targ = arg;
5491 else
5492 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5493 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5496 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5497 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5498 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5501 /* Don't let anything else be handled specially. */
5502 return NULL_TREE;
5505 /* Mark EXP saying that we need to be able to take the
5506 address of it; it should not be allocated in a register.
5507 Value is true if successful.
5509 C++: we do not allow `current_class_ptr' to be addressable. */
5511 bool
5512 cxx_mark_addressable (tree exp)
5514 tree x = exp;
5516 while (1)
5517 switch (TREE_CODE (x))
5519 case ADDR_EXPR:
5520 case COMPONENT_REF:
5521 case ARRAY_REF:
5522 case REALPART_EXPR:
5523 case IMAGPART_EXPR:
5524 x = TREE_OPERAND (x, 0);
5525 break;
5527 case PARM_DECL:
5528 if (x == current_class_ptr)
5530 error ("cannot take the address of %<this%>, which is an rvalue expression");
5531 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5532 return true;
5534 /* Fall through. */
5536 case VAR_DECL:
5537 /* Caller should not be trying to mark initialized
5538 constant fields addressable. */
5539 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5540 || DECL_IN_AGGR_P (x) == 0
5541 || TREE_STATIC (x)
5542 || DECL_EXTERNAL (x));
5543 /* Fall through. */
5545 case RESULT_DECL:
5546 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5547 && !DECL_ARTIFICIAL (x))
5549 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
5551 error
5552 ("address of explicit register variable %qD requested", x);
5553 return false;
5555 else if (extra_warnings)
5556 warning
5557 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5559 TREE_ADDRESSABLE (x) = 1;
5560 return true;
5562 case CONST_DECL:
5563 case FUNCTION_DECL:
5564 TREE_ADDRESSABLE (x) = 1;
5565 return true;
5567 case CONSTRUCTOR:
5568 TREE_ADDRESSABLE (x) = 1;
5569 return true;
5571 case TARGET_EXPR:
5572 TREE_ADDRESSABLE (x) = 1;
5573 cxx_mark_addressable (TREE_OPERAND (x, 0));
5574 return true;
5576 default:
5577 return true;
5581 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5583 tree
5584 build_x_conditional_expr (tree ifexp, tree op1, tree op2,
5585 tsubst_flags_t complain)
5587 tree orig_ifexp = ifexp;
5588 tree orig_op1 = op1;
5589 tree orig_op2 = op2;
5590 tree expr;
5592 if (processing_template_decl)
5594 /* The standard says that the expression is type-dependent if
5595 IFEXP is type-dependent, even though the eventual type of the
5596 expression doesn't dependent on IFEXP. */
5597 if (type_dependent_expression_p (ifexp)
5598 /* As a GNU extension, the middle operand may be omitted. */
5599 || (op1 && type_dependent_expression_p (op1))
5600 || type_dependent_expression_p (op2))
5601 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5602 ifexp = build_non_dependent_expr (ifexp);
5603 if (op1)
5604 op1 = build_non_dependent_expr (op1);
5605 op2 = build_non_dependent_expr (op2);
5608 expr = build_conditional_expr (ifexp, op1, op2, complain);
5609 if (processing_template_decl && expr != error_mark_node)
5611 tree min = build_min_non_dep (COND_EXPR, expr,
5612 orig_ifexp, orig_op1, orig_op2);
5613 /* In C++11, remember that the result is an lvalue or xvalue.
5614 In C++98, lvalue_kind can just assume lvalue in a template. */
5615 if (cxx_dialect >= cxx0x
5616 && lvalue_or_rvalue_with_address_p (expr)
5617 && !lvalue_or_rvalue_with_address_p (min))
5618 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5619 !real_lvalue_p (expr));
5620 expr = convert_from_reference (min);
5622 return expr;
5625 /* Given a list of expressions, return a compound expression
5626 that performs them all and returns the value of the last of them. */
5628 tree
5629 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5630 tsubst_flags_t complain)
5632 tree expr = TREE_VALUE (list);
5634 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5635 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5637 if (complain & tf_error)
5638 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5639 "non-class type must not be parenthesized");
5640 else
5641 return error_mark_node;
5644 if (TREE_CHAIN (list))
5646 if (complain & tf_error)
5647 switch (exp)
5649 case ELK_INIT:
5650 permerror (input_location, "expression list treated as compound "
5651 "expression in initializer");
5652 break;
5653 case ELK_MEM_INIT:
5654 permerror (input_location, "expression list treated as compound "
5655 "expression in mem-initializer");
5656 break;
5657 case ELK_FUNC_CAST:
5658 permerror (input_location, "expression list treated as compound "
5659 "expression in functional cast");
5660 break;
5661 default:
5662 gcc_unreachable ();
5664 else
5665 return error_mark_node;
5667 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5668 expr = build_x_compound_expr (expr, TREE_VALUE (list),
5669 complain);
5672 return expr;
5675 /* Like build_x_compound_expr_from_list, but using a VEC. */
5677 tree
5678 build_x_compound_expr_from_vec (VEC(tree,gc) *vec, const char *msg)
5680 if (VEC_empty (tree, vec))
5681 return NULL_TREE;
5682 else if (VEC_length (tree, vec) == 1)
5683 return VEC_index (tree, vec, 0);
5684 else
5686 tree expr;
5687 unsigned int ix;
5688 tree t;
5690 if (msg != NULL)
5691 permerror (input_location,
5692 "%s expression list treated as compound expression",
5693 msg);
5695 expr = VEC_index (tree, vec, 0);
5696 for (ix = 1; VEC_iterate (tree, vec, ix, t); ++ix)
5697 expr = build_x_compound_expr (expr, t, tf_warning_or_error);
5699 return expr;
5703 /* Handle overloading of the ',' operator when needed. */
5705 tree
5706 build_x_compound_expr (tree op1, tree op2, tsubst_flags_t complain)
5708 tree result;
5709 tree orig_op1 = op1;
5710 tree orig_op2 = op2;
5712 if (processing_template_decl)
5714 if (type_dependent_expression_p (op1)
5715 || type_dependent_expression_p (op2))
5716 return build_min_nt (COMPOUND_EXPR, op1, op2);
5717 op1 = build_non_dependent_expr (op1);
5718 op2 = build_non_dependent_expr (op2);
5721 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE,
5722 /*overload=*/NULL, complain);
5723 if (!result)
5724 result = cp_build_compound_expr (op1, op2, complain);
5726 if (processing_template_decl && result != error_mark_node)
5727 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
5729 return result;
5732 /* Like cp_build_compound_expr, but for the c-common bits. */
5734 tree
5735 build_compound_expr (location_t loc ATTRIBUTE_UNUSED, tree lhs, tree rhs)
5737 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
5740 /* Build a compound expression. */
5742 tree
5743 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
5745 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
5747 if (lhs == error_mark_node || rhs == error_mark_node)
5748 return error_mark_node;
5750 if (TREE_CODE (rhs) == TARGET_EXPR)
5752 /* If the rhs is a TARGET_EXPR, then build the compound
5753 expression inside the target_expr's initializer. This
5754 helps the compiler to eliminate unnecessary temporaries. */
5755 tree init = TREE_OPERAND (rhs, 1);
5757 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
5758 TREE_OPERAND (rhs, 1) = init;
5760 return rhs;
5763 if (type_unknown_p (rhs))
5765 error ("no context to resolve type of %qE", rhs);
5766 return error_mark_node;
5769 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
5772 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
5773 casts away constness. CAST gives the type of cast. Returns true
5774 if the cast is ill-formed, false if it is well-formed.
5776 ??? This function warns for casting away any qualifier not just
5777 const. We would like to specify exactly what qualifiers are casted
5778 away.
5781 static bool
5782 check_for_casting_away_constness (tree src_type, tree dest_type,
5783 enum tree_code cast, tsubst_flags_t complain)
5785 /* C-style casts are allowed to cast away constness. With
5786 WARN_CAST_QUAL, we still want to issue a warning. */
5787 if (cast == CAST_EXPR && !warn_cast_qual)
5788 return false;
5790 if (!casts_away_constness (src_type, dest_type, complain))
5791 return false;
5793 switch (cast)
5795 case CAST_EXPR:
5796 if (complain & tf_warning)
5797 warning (OPT_Wcast_qual,
5798 "cast from type %qT to type %qT casts away qualifiers",
5799 src_type, dest_type);
5800 return false;
5802 case STATIC_CAST_EXPR:
5803 if (complain & tf_error)
5804 error ("static_cast from type %qT to type %qT casts away qualifiers",
5805 src_type, dest_type);
5806 return true;
5808 case REINTERPRET_CAST_EXPR:
5809 if (complain & tf_error)
5810 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
5811 src_type, dest_type);
5812 return true;
5814 default:
5815 gcc_unreachable();
5820 Warns if the cast from expression EXPR to type TYPE is useless.
5822 void
5823 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
5825 if (warn_useless_cast
5826 && complain & tf_warning
5827 && c_inhibit_evaluation_warnings == 0)
5829 if (REFERENCE_REF_P (expr))
5830 expr = TREE_OPERAND (expr, 0);
5832 if ((TREE_CODE (type) == REFERENCE_TYPE
5833 && (TYPE_REF_IS_RVALUE (type)
5834 ? xvalue_p (expr) : real_lvalue_p (expr))
5835 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
5836 || same_type_p (TREE_TYPE (expr), type))
5837 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
5841 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
5842 (another pointer-to-member type in the same hierarchy) and return
5843 the converted expression. If ALLOW_INVERSE_P is permitted, a
5844 pointer-to-derived may be converted to pointer-to-base; otherwise,
5845 only the other direction is permitted. If C_CAST_P is true, this
5846 conversion is taking place as part of a C-style cast. */
5848 tree
5849 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
5850 bool c_cast_p, tsubst_flags_t complain)
5852 if (TYPE_PTRMEM_P (type))
5854 tree delta;
5856 if (TREE_CODE (expr) == PTRMEM_CST)
5857 expr = cplus_expand_constant (expr);
5858 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
5859 TYPE_PTRMEM_CLASS_TYPE (type),
5860 allow_inverse_p,
5861 c_cast_p, complain);
5862 if (delta == error_mark_node)
5863 return error_mark_node;
5865 if (!integer_zerop (delta))
5867 tree cond, op1, op2;
5869 cond = cp_build_binary_op (input_location,
5870 EQ_EXPR,
5871 expr,
5872 build_int_cst (TREE_TYPE (expr), -1),
5873 complain);
5874 op1 = build_nop (ptrdiff_type_node, expr);
5875 op2 = cp_build_binary_op (input_location,
5876 PLUS_EXPR, op1, delta,
5877 complain);
5879 expr = fold_build3_loc (input_location,
5880 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
5884 return build_nop (type, expr);
5886 else
5887 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
5888 allow_inverse_p, c_cast_p, complain);
5891 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
5892 this static_cast is being attempted as one of the possible casts
5893 allowed by a C-style cast. (In that case, accessibility of base
5894 classes is not considered, and it is OK to cast away
5895 constness.) Return the result of the cast. *VALID_P is set to
5896 indicate whether or not the cast was valid. */
5898 static tree
5899 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
5900 bool *valid_p, tsubst_flags_t complain)
5902 tree intype;
5903 tree result;
5904 cp_lvalue_kind clk;
5906 /* Assume the cast is valid. */
5907 *valid_p = true;
5909 intype = unlowered_expr_type (expr);
5911 /* Save casted types in the function's used types hash table. */
5912 used_types_insert (type);
5914 /* [expr.static.cast]
5916 An lvalue of type "cv1 B", where B is a class type, can be cast
5917 to type "reference to cv2 D", where D is a class derived (clause
5918 _class.derived_) from B, if a valid standard conversion from
5919 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
5920 same cv-qualification as, or greater cv-qualification than, cv1,
5921 and B is not a virtual base class of D. */
5922 /* We check this case before checking the validity of "TYPE t =
5923 EXPR;" below because for this case:
5925 struct B {};
5926 struct D : public B { D(const B&); };
5927 extern B& b;
5928 void f() { static_cast<const D&>(b); }
5930 we want to avoid constructing a new D. The standard is not
5931 completely clear about this issue, but our interpretation is
5932 consistent with other compilers. */
5933 if (TREE_CODE (type) == REFERENCE_TYPE
5934 && CLASS_TYPE_P (TREE_TYPE (type))
5935 && CLASS_TYPE_P (intype)
5936 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
5937 && DERIVED_FROM_P (intype, TREE_TYPE (type))
5938 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
5939 build_pointer_type (TYPE_MAIN_VARIANT
5940 (TREE_TYPE (type))),
5941 complain)
5942 && (c_cast_p
5943 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5945 tree base;
5947 /* There is a standard conversion from "D*" to "B*" even if "B"
5948 is ambiguous or inaccessible. If this is really a
5949 static_cast, then we check both for inaccessibility and
5950 ambiguity. However, if this is a static_cast being performed
5951 because the user wrote a C-style cast, then accessibility is
5952 not considered. */
5953 base = lookup_base (TREE_TYPE (type), intype,
5954 c_cast_p ? ba_unique : ba_check,
5955 NULL);
5957 /* Convert from "B*" to "D*". This function will check that "B"
5958 is not a virtual base of "D". */
5959 expr = build_base_path (MINUS_EXPR, build_address (expr),
5960 base, /*nonnull=*/false, complain);
5961 /* Convert the pointer to a reference -- but then remember that
5962 there are no expressions with reference type in C++.
5964 We call rvalue so that there's an actual tree code
5965 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
5966 is a variable with the same type, the conversion would get folded
5967 away, leaving just the variable and causing lvalue_kind to give
5968 the wrong answer. */
5969 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
5972 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
5973 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
5974 if (TREE_CODE (type) == REFERENCE_TYPE
5975 && TYPE_REF_IS_RVALUE (type)
5976 && (clk = real_lvalue_p (expr))
5977 && reference_related_p (TREE_TYPE (type), intype)
5978 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5980 if (clk == clk_ordinary)
5982 /* Handle the (non-bit-field) lvalue case here by casting to
5983 lvalue reference and then changing it to an rvalue reference.
5984 Casting an xvalue to rvalue reference will be handled by the
5985 main code path. */
5986 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
5987 result = (perform_direct_initialization_if_possible
5988 (lref, expr, c_cast_p, complain));
5989 result = cp_fold_convert (type, result);
5990 /* Make sure we don't fold back down to a named rvalue reference,
5991 because that would be an lvalue. */
5992 if (DECL_P (result))
5993 result = build1 (NON_LVALUE_EXPR, type, result);
5994 return convert_from_reference (result);
5996 else
5997 /* For a bit-field or packed field, bind to a temporary. */
5998 expr = rvalue (expr);
6001 /* Resolve overloaded address here rather than once in
6002 implicit_conversion and again in the inverse code below. */
6003 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6005 expr = instantiate_type (type, expr, complain);
6006 intype = TREE_TYPE (expr);
6009 /* [expr.static.cast]
6011 An expression e can be explicitly converted to a type T using a
6012 static_cast of the form static_cast<T>(e) if the declaration T
6013 t(e);" is well-formed, for some invented temporary variable
6014 t. */
6015 result = perform_direct_initialization_if_possible (type, expr,
6016 c_cast_p, complain);
6017 if (result)
6019 result = convert_from_reference (result);
6021 /* [expr.static.cast]
6023 If T is a reference type, the result is an lvalue; otherwise,
6024 the result is an rvalue. */
6025 if (TREE_CODE (type) != REFERENCE_TYPE)
6026 result = rvalue (result);
6027 return result;
6030 /* [expr.static.cast]
6032 Any expression can be explicitly converted to type cv void. */
6033 if (TREE_CODE (type) == VOID_TYPE)
6034 return convert_to_void (expr, ICV_CAST, complain);
6036 /* [expr.static.cast]
6038 The inverse of any standard conversion sequence (clause _conv_),
6039 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6040 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6041 (_conv.bool_) conversions, can be performed explicitly using
6042 static_cast subject to the restriction that the explicit
6043 conversion does not cast away constness (_expr.const.cast_), and
6044 the following additional rules for specific cases: */
6045 /* For reference, the conversions not excluded are: integral
6046 promotions, floating point promotion, integral conversions,
6047 floating point conversions, floating-integral conversions,
6048 pointer conversions, and pointer to member conversions. */
6049 /* DR 128
6051 A value of integral _or enumeration_ type can be explicitly
6052 converted to an enumeration type. */
6053 /* The effect of all that is that any conversion between any two
6054 types which are integral, floating, or enumeration types can be
6055 performed. */
6056 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6057 || SCALAR_FLOAT_TYPE_P (type))
6058 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6059 || SCALAR_FLOAT_TYPE_P (intype)))
6060 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
6062 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6063 && CLASS_TYPE_P (TREE_TYPE (type))
6064 && CLASS_TYPE_P (TREE_TYPE (intype))
6065 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6066 (TREE_TYPE (intype))),
6067 build_pointer_type (TYPE_MAIN_VARIANT
6068 (TREE_TYPE (type))),
6069 complain))
6071 tree base;
6073 if (!c_cast_p
6074 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6075 complain))
6076 return error_mark_node;
6077 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6078 c_cast_p ? ba_unique : ba_check,
6079 NULL);
6080 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6081 complain);
6082 return cp_fold_convert(type, expr);
6085 if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
6086 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6088 tree c1;
6089 tree c2;
6090 tree t1;
6091 tree t2;
6093 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6094 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6096 if (TYPE_PTRMEM_P (type))
6098 t1 = (build_ptrmem_type
6099 (c1,
6100 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6101 t2 = (build_ptrmem_type
6102 (c2,
6103 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6105 else
6107 t1 = intype;
6108 t2 = type;
6110 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6112 if (!c_cast_p
6113 && check_for_casting_away_constness (intype, type,
6114 STATIC_CAST_EXPR,
6115 complain))
6116 return error_mark_node;
6117 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6118 c_cast_p, complain);
6122 /* [expr.static.cast]
6124 An rvalue of type "pointer to cv void" can be explicitly
6125 converted to a pointer to object type. A value of type pointer
6126 to object converted to "pointer to cv void" and back to the
6127 original pointer type will have its original value. */
6128 if (TREE_CODE (intype) == POINTER_TYPE
6129 && VOID_TYPE_P (TREE_TYPE (intype))
6130 && TYPE_PTROB_P (type))
6132 if (!c_cast_p
6133 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6134 complain))
6135 return error_mark_node;
6136 return build_nop (type, expr);
6139 *valid_p = false;
6140 return error_mark_node;
6143 /* Return an expression representing static_cast<TYPE>(EXPR). */
6145 tree
6146 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6148 tree result;
6149 bool valid_p;
6151 if (type == error_mark_node || expr == error_mark_node)
6152 return error_mark_node;
6154 if (processing_template_decl)
6156 expr = build_min (STATIC_CAST_EXPR, type, expr);
6157 /* We don't know if it will or will not have side effects. */
6158 TREE_SIDE_EFFECTS (expr) = 1;
6159 return convert_from_reference (expr);
6162 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6163 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6164 if (TREE_CODE (type) != REFERENCE_TYPE
6165 && TREE_CODE (expr) == NOP_EXPR
6166 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6167 expr = TREE_OPERAND (expr, 0);
6169 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6170 complain);
6171 if (valid_p)
6173 if (result != error_mark_node)
6174 maybe_warn_about_useless_cast (type, expr, complain);
6175 return result;
6178 if (complain & tf_error)
6179 error ("invalid static_cast from type %qT to type %qT",
6180 TREE_TYPE (expr), type);
6181 return error_mark_node;
6184 /* EXPR is an expression with member function or pointer-to-member
6185 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6186 not permitted by ISO C++, but we accept it in some modes. If we
6187 are not in one of those modes, issue a diagnostic. Return the
6188 converted expression. */
6190 tree
6191 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6193 tree intype;
6194 tree decl;
6196 intype = TREE_TYPE (expr);
6197 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6198 || TREE_CODE (intype) == METHOD_TYPE);
6200 if (!(complain & tf_warning_or_error))
6201 return error_mark_node;
6203 if (pedantic || warn_pmf2ptr)
6204 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6205 "converting from %qT to %qT", intype, type);
6207 if (TREE_CODE (intype) == METHOD_TYPE)
6208 expr = build_addr_func (expr, complain);
6209 else if (TREE_CODE (expr) == PTRMEM_CST)
6210 expr = build_address (PTRMEM_CST_MEMBER (expr));
6211 else
6213 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6214 decl = build_address (decl);
6215 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6218 if (expr == error_mark_node)
6219 return error_mark_node;
6221 return build_nop (type, expr);
6224 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6225 If C_CAST_P is true, this reinterpret cast is being done as part of
6226 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6227 indicate whether or not reinterpret_cast was valid. */
6229 static tree
6230 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6231 bool *valid_p, tsubst_flags_t complain)
6233 tree intype;
6235 /* Assume the cast is invalid. */
6236 if (valid_p)
6237 *valid_p = true;
6239 if (type == error_mark_node || error_operand_p (expr))
6240 return error_mark_node;
6242 intype = TREE_TYPE (expr);
6244 /* Save casted types in the function's used types hash table. */
6245 used_types_insert (type);
6247 /* [expr.reinterpret.cast]
6248 An lvalue expression of type T1 can be cast to the type
6249 "reference to T2" if an expression of type "pointer to T1" can be
6250 explicitly converted to the type "pointer to T2" using a
6251 reinterpret_cast. */
6252 if (TREE_CODE (type) == REFERENCE_TYPE)
6254 if (! real_lvalue_p (expr))
6256 if (complain & tf_error)
6257 error ("invalid cast of an rvalue expression of type "
6258 "%qT to type %qT",
6259 intype, type);
6260 return error_mark_node;
6263 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6264 "B" are related class types; the reinterpret_cast does not
6265 adjust the pointer. */
6266 if (TYPE_PTR_P (intype)
6267 && (complain & tf_warning)
6268 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6269 COMPARE_BASE | COMPARE_DERIVED)))
6270 warning (0, "casting %qT to %qT does not dereference pointer",
6271 intype, type);
6273 expr = cp_build_addr_expr (expr, complain);
6275 if (warn_strict_aliasing > 2)
6276 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6278 if (expr != error_mark_node)
6279 expr = build_reinterpret_cast_1
6280 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6281 valid_p, complain);
6282 if (expr != error_mark_node)
6283 /* cp_build_indirect_ref isn't right for rvalue refs. */
6284 expr = convert_from_reference (fold_convert (type, expr));
6285 return expr;
6288 /* As a G++ extension, we consider conversions from member
6289 functions, and pointers to member functions to
6290 pointer-to-function and pointer-to-void types. If
6291 -Wno-pmf-conversions has not been specified,
6292 convert_member_func_to_ptr will issue an error message. */
6293 if ((TYPE_PTRMEMFUNC_P (intype)
6294 || TREE_CODE (intype) == METHOD_TYPE)
6295 && TYPE_PTR_P (type)
6296 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6297 || VOID_TYPE_P (TREE_TYPE (type))))
6298 return convert_member_func_to_ptr (type, expr, complain);
6300 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6301 array-to-pointer, and function-to-pointer conversions are
6302 performed. */
6303 expr = decay_conversion (expr, complain);
6305 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6306 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6307 if (TREE_CODE (expr) == NOP_EXPR
6308 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6309 expr = TREE_OPERAND (expr, 0);
6311 if (error_operand_p (expr))
6312 return error_mark_node;
6314 intype = TREE_TYPE (expr);
6316 /* [expr.reinterpret.cast]
6317 A pointer can be converted to any integral type large enough to
6318 hold it. ... A value of type std::nullptr_t can be converted to
6319 an integral type; the conversion has the same meaning and
6320 validity as a conversion of (void*)0 to the integral type. */
6321 if (CP_INTEGRAL_TYPE_P (type)
6322 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6324 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6326 if (complain & tf_error)
6327 permerror (input_location, "cast from %qT to %qT loses precision",
6328 intype, type);
6329 else
6330 return error_mark_node;
6332 if (NULLPTR_TYPE_P (intype))
6333 return build_int_cst (type, 0);
6335 /* [expr.reinterpret.cast]
6336 A value of integral or enumeration type can be explicitly
6337 converted to a pointer. */
6338 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6339 /* OK */
6341 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6342 || TYPE_PTR_P (type) || TYPE_PTR_TO_MEMBER_P (type))
6343 && same_type_p (type, intype))
6344 /* DR 799 */
6345 return fold_if_not_in_template (build_nop (type, expr));
6346 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6347 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6348 return fold_if_not_in_template (build_nop (type, expr));
6349 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
6350 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6352 tree sexpr = expr;
6354 if (!c_cast_p
6355 && check_for_casting_away_constness (intype, type,
6356 REINTERPRET_CAST_EXPR,
6357 complain))
6358 return error_mark_node;
6359 /* Warn about possible alignment problems. */
6360 if (STRICT_ALIGNMENT && warn_cast_align
6361 && (complain & tf_warning)
6362 && !VOID_TYPE_P (type)
6363 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6364 && COMPLETE_TYPE_P (TREE_TYPE (type))
6365 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6366 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6367 warning (OPT_Wcast_align, "cast from %qT to %qT "
6368 "increases required alignment of target type", intype, type);
6370 /* We need to strip nops here, because the front end likes to
6371 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6372 STRIP_NOPS (sexpr);
6373 if (warn_strict_aliasing <= 2)
6374 strict_aliasing_warning (intype, type, sexpr);
6376 return fold_if_not_in_template (build_nop (type, expr));
6378 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6379 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6381 if (pedantic && (complain & tf_warning))
6382 /* Only issue a warning, as we have always supported this
6383 where possible, and it is necessary in some cases. DR 195
6384 addresses this issue, but as of 2004/10/26 is still in
6385 drafting. */
6386 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6387 return fold_if_not_in_template (build_nop (type, expr));
6389 else if (TREE_CODE (type) == VECTOR_TYPE)
6390 return fold_if_not_in_template (convert_to_vector (type, expr));
6391 else if (TREE_CODE (intype) == VECTOR_TYPE
6392 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6393 return fold_if_not_in_template (convert_to_integer (type, expr));
6394 else
6396 if (valid_p)
6397 *valid_p = false;
6398 if (complain & tf_error)
6399 error ("invalid cast from type %qT to type %qT", intype, type);
6400 return error_mark_node;
6403 return cp_convert (type, expr);
6406 tree
6407 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6409 tree r;
6411 if (type == error_mark_node || expr == error_mark_node)
6412 return error_mark_node;
6414 if (processing_template_decl)
6416 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6418 if (!TREE_SIDE_EFFECTS (t)
6419 && type_dependent_expression_p (expr))
6420 /* There might turn out to be side effects inside expr. */
6421 TREE_SIDE_EFFECTS (t) = 1;
6422 return convert_from_reference (t);
6425 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6426 /*valid_p=*/NULL, complain);
6427 if (r != error_mark_node)
6428 maybe_warn_about_useless_cast (type, expr, complain);
6429 return r;
6432 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6433 return an appropriate expression. Otherwise, return
6434 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6435 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6436 performing a C-style cast, its value upon return will indicate
6437 whether or not the conversion succeeded. */
6439 static tree
6440 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6441 bool *valid_p)
6443 tree src_type;
6444 tree reference_type;
6446 /* Callers are responsible for handling error_mark_node as a
6447 destination type. */
6448 gcc_assert (dst_type != error_mark_node);
6449 /* In a template, callers should be building syntactic
6450 representations of casts, not using this machinery. */
6451 gcc_assert (!processing_template_decl);
6453 /* Assume the conversion is invalid. */
6454 if (valid_p)
6455 *valid_p = false;
6457 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type))
6459 if (complain & tf_error)
6460 error ("invalid use of const_cast with type %qT, "
6461 "which is not a pointer, "
6462 "reference, nor a pointer-to-data-member type", dst_type);
6463 return error_mark_node;
6466 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6468 if (complain & tf_error)
6469 error ("invalid use of const_cast with type %qT, which is a pointer "
6470 "or reference to a function type", dst_type);
6471 return error_mark_node;
6474 /* Save casted types in the function's used types hash table. */
6475 used_types_insert (dst_type);
6477 src_type = TREE_TYPE (expr);
6478 /* Expressions do not really have reference types. */
6479 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6480 src_type = TREE_TYPE (src_type);
6482 /* [expr.const.cast]
6484 For two object types T1 and T2, if a pointer to T1 can be explicitly
6485 converted to the type "pointer to T2" using a const_cast, then the
6486 following conversions can also be made:
6488 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6489 type T2 using the cast const_cast<T2&>;
6491 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6492 type T2 using the cast const_cast<T2&&>; and
6494 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6495 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6497 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6499 reference_type = dst_type;
6500 if (!TYPE_REF_IS_RVALUE (dst_type)
6501 ? real_lvalue_p (expr)
6502 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6503 ? lvalue_p (expr)
6504 : lvalue_or_rvalue_with_address_p (expr)))
6505 /* OK. */;
6506 else
6508 if (complain & tf_error)
6509 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6510 src_type, dst_type);
6511 return error_mark_node;
6513 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6514 src_type = build_pointer_type (src_type);
6516 else
6518 reference_type = NULL_TREE;
6519 /* If the destination type is not a reference type, the
6520 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6521 conversions are performed. */
6522 src_type = type_decays_to (src_type);
6523 if (src_type == error_mark_node)
6524 return error_mark_node;
6527 if (TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
6529 if (comp_ptr_ttypes_const (dst_type, src_type))
6531 if (valid_p)
6533 *valid_p = true;
6534 /* This cast is actually a C-style cast. Issue a warning if
6535 the user is making a potentially unsafe cast. */
6536 check_for_casting_away_constness (src_type, dst_type,
6537 CAST_EXPR, complain);
6539 if (reference_type)
6541 expr = cp_build_addr_expr (expr, complain);
6542 if (expr == error_mark_node)
6543 return error_mark_node;
6544 expr = build_nop (reference_type, expr);
6545 return convert_from_reference (expr);
6547 else
6549 expr = decay_conversion (expr, complain);
6550 if (expr == error_mark_node)
6551 return error_mark_node;
6553 /* build_c_cast puts on a NOP_EXPR to make the result not an
6554 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6555 non-lvalue context. */
6556 if (TREE_CODE (expr) == NOP_EXPR
6557 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6558 expr = TREE_OPERAND (expr, 0);
6559 return build_nop (dst_type, expr);
6562 else if (valid_p
6563 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6564 TREE_TYPE (src_type)))
6565 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6566 complain);
6569 if (complain & tf_error)
6570 error ("invalid const_cast from type %qT to type %qT",
6571 src_type, dst_type);
6572 return error_mark_node;
6575 tree
6576 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6578 tree r;
6580 if (type == error_mark_node || error_operand_p (expr))
6581 return error_mark_node;
6583 if (processing_template_decl)
6585 tree t = build_min (CONST_CAST_EXPR, type, expr);
6587 if (!TREE_SIDE_EFFECTS (t)
6588 && type_dependent_expression_p (expr))
6589 /* There might turn out to be side effects inside expr. */
6590 TREE_SIDE_EFFECTS (t) = 1;
6591 return convert_from_reference (t);
6594 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
6595 if (r != error_mark_node)
6596 maybe_warn_about_useless_cast (type, expr, complain);
6597 return r;
6600 /* Like cp_build_c_cast, but for the c-common bits. */
6602 tree
6603 build_c_cast (location_t loc ATTRIBUTE_UNUSED, tree type, tree expr)
6605 return cp_build_c_cast (type, expr, tf_warning_or_error);
6608 /* Build an expression representing an explicit C-style cast to type
6609 TYPE of expression EXPR. */
6611 tree
6612 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6614 tree value = expr;
6615 tree result;
6616 bool valid_p;
6618 if (type == error_mark_node || error_operand_p (expr))
6619 return error_mark_node;
6621 if (processing_template_decl)
6623 tree t = build_min (CAST_EXPR, type,
6624 tree_cons (NULL_TREE, value, NULL_TREE));
6625 /* We don't know if it will or will not have side effects. */
6626 TREE_SIDE_EFFECTS (t) = 1;
6627 return convert_from_reference (t);
6630 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6631 'Class') should always be retained, because this information aids
6632 in method lookup. */
6633 if (objc_is_object_ptr (type)
6634 && objc_is_object_ptr (TREE_TYPE (expr)))
6635 return build_nop (type, expr);
6637 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6638 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6639 if (TREE_CODE (type) != REFERENCE_TYPE
6640 && TREE_CODE (value) == NOP_EXPR
6641 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6642 value = TREE_OPERAND (value, 0);
6644 if (TREE_CODE (type) == ARRAY_TYPE)
6646 /* Allow casting from T1* to T2[] because Cfront allows it.
6647 NIHCL uses it. It is not valid ISO C++ however. */
6648 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6650 if (complain & tf_error)
6651 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6652 else
6653 return error_mark_node;
6654 type = build_pointer_type (TREE_TYPE (type));
6656 else
6658 if (complain & tf_error)
6659 error ("ISO C++ forbids casting to an array type %qT", type);
6660 return error_mark_node;
6664 if (TREE_CODE (type) == FUNCTION_TYPE
6665 || TREE_CODE (type) == METHOD_TYPE)
6667 if (complain & tf_error)
6668 error ("invalid cast to function type %qT", type);
6669 return error_mark_node;
6672 if (TREE_CODE (type) == POINTER_TYPE
6673 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
6674 /* Casting to an integer of smaller size is an error detected elsewhere. */
6675 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
6676 /* Don't warn about converting any constant. */
6677 && !TREE_CONSTANT (value))
6678 warning_at (input_location, OPT_Wint_to_pointer_cast,
6679 "cast to pointer from integer of different size");
6681 /* A C-style cast can be a const_cast. */
6682 result = build_const_cast_1 (type, value, complain & tf_warning,
6683 &valid_p);
6684 if (valid_p)
6686 if (result != error_mark_node)
6687 maybe_warn_about_useless_cast (type, value, complain);
6688 return result;
6691 /* Or a static cast. */
6692 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
6693 &valid_p, complain);
6694 /* Or a reinterpret_cast. */
6695 if (!valid_p)
6696 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
6697 &valid_p, complain);
6698 /* The static_cast or reinterpret_cast may be followed by a
6699 const_cast. */
6700 if (valid_p
6701 /* A valid cast may result in errors if, for example, a
6702 conversion to an ambiguous base class is required. */
6703 && !error_operand_p (result))
6705 tree result_type;
6707 maybe_warn_about_useless_cast (type, value, complain);
6709 /* Non-class rvalues always have cv-unqualified type. */
6710 if (!CLASS_TYPE_P (type))
6711 type = TYPE_MAIN_VARIANT (type);
6712 result_type = TREE_TYPE (result);
6713 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
6714 result_type = TYPE_MAIN_VARIANT (result_type);
6715 /* If the type of RESULT does not match TYPE, perform a
6716 const_cast to make it match. If the static_cast or
6717 reinterpret_cast succeeded, we will differ by at most
6718 cv-qualification, so the follow-on const_cast is guaranteed
6719 to succeed. */
6720 if (!same_type_p (non_reference (type), non_reference (result_type)))
6722 result = build_const_cast_1 (type, result, false, &valid_p);
6723 gcc_assert (valid_p);
6725 return result;
6728 return error_mark_node;
6731 /* For use from the C common bits. */
6732 tree
6733 build_modify_expr (location_t location ATTRIBUTE_UNUSED,
6734 tree lhs, tree lhs_origtype ATTRIBUTE_UNUSED,
6735 enum tree_code modifycode,
6736 location_t rhs_location ATTRIBUTE_UNUSED, tree rhs,
6737 tree rhs_origtype ATTRIBUTE_UNUSED)
6739 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
6742 /* Build an assignment expression of lvalue LHS from value RHS.
6743 MODIFYCODE is the code for a binary operator that we use
6744 to combine the old value of LHS with RHS to get the new value.
6745 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6747 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
6749 tree
6750 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6751 tsubst_flags_t complain)
6753 tree result;
6754 tree newrhs = rhs;
6755 tree lhstype = TREE_TYPE (lhs);
6756 tree olhstype = lhstype;
6757 bool plain_assign = (modifycode == NOP_EXPR);
6759 /* Avoid duplicate error messages from operands that had errors. */
6760 if (error_operand_p (lhs) || error_operand_p (rhs))
6761 return error_mark_node;
6763 /* Handle control structure constructs used as "lvalues". */
6764 switch (TREE_CODE (lhs))
6766 /* Handle --foo = 5; as these are valid constructs in C++. */
6767 case PREDECREMENT_EXPR:
6768 case PREINCREMENT_EXPR:
6769 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6770 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6771 stabilize_reference (TREE_OPERAND (lhs, 0)),
6772 TREE_OPERAND (lhs, 1));
6773 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
6774 modifycode, rhs, complain);
6775 if (newrhs == error_mark_node)
6776 return error_mark_node;
6777 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6779 /* Handle (a, b) used as an "lvalue". */
6780 case COMPOUND_EXPR:
6781 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6782 modifycode, rhs, complain);
6783 if (newrhs == error_mark_node)
6784 return error_mark_node;
6785 return build2 (COMPOUND_EXPR, lhstype,
6786 TREE_OPERAND (lhs, 0), newrhs);
6788 case MODIFY_EXPR:
6789 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6790 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6791 stabilize_reference (TREE_OPERAND (lhs, 0)),
6792 TREE_OPERAND (lhs, 1));
6793 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
6794 complain);
6795 if (newrhs == error_mark_node)
6796 return error_mark_node;
6797 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6799 case MIN_EXPR:
6800 case MAX_EXPR:
6801 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
6802 when neither operand has side-effects. */
6803 if (!lvalue_or_else (lhs, lv_assign, complain))
6804 return error_mark_node;
6806 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
6807 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
6809 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
6810 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
6811 boolean_type_node,
6812 TREE_OPERAND (lhs, 0),
6813 TREE_OPERAND (lhs, 1)),
6814 TREE_OPERAND (lhs, 0),
6815 TREE_OPERAND (lhs, 1));
6816 /* Fall through. */
6818 /* Handle (a ? b : c) used as an "lvalue". */
6819 case COND_EXPR:
6821 /* Produce (a ? (b = rhs) : (c = rhs))
6822 except that the RHS goes through a save-expr
6823 so the code to compute it is only emitted once. */
6824 tree cond;
6825 tree preeval = NULL_TREE;
6827 if (VOID_TYPE_P (TREE_TYPE (rhs)))
6829 if (complain & tf_error)
6830 error ("void value not ignored as it ought to be");
6831 return error_mark_node;
6834 rhs = stabilize_expr (rhs, &preeval);
6836 /* Check this here to avoid odd errors when trying to convert
6837 a throw to the type of the COND_EXPR. */
6838 if (!lvalue_or_else (lhs, lv_assign, complain))
6839 return error_mark_node;
6841 cond = build_conditional_expr
6842 (TREE_OPERAND (lhs, 0),
6843 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6844 modifycode, rhs, complain),
6845 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
6846 modifycode, rhs, complain),
6847 complain);
6849 if (cond == error_mark_node)
6850 return cond;
6851 /* Make sure the code to compute the rhs comes out
6852 before the split. */
6853 if (preeval)
6854 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
6855 return cond;
6858 default:
6859 break;
6862 if (modifycode == INIT_EXPR)
6864 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6865 /* Do the default thing. */;
6866 else if (TREE_CODE (rhs) == CONSTRUCTOR)
6868 /* Compound literal. */
6869 if (! same_type_p (TREE_TYPE (rhs), lhstype))
6870 /* Call convert to generate an error; see PR 11063. */
6871 rhs = convert (lhstype, rhs);
6872 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
6873 TREE_SIDE_EFFECTS (result) = 1;
6874 return result;
6876 else if (! MAYBE_CLASS_TYPE_P (lhstype))
6877 /* Do the default thing. */;
6878 else
6880 VEC(tree,gc) *rhs_vec = make_tree_vector_single (rhs);
6881 result = build_special_member_call (lhs, complete_ctor_identifier,
6882 &rhs_vec, lhstype, LOOKUP_NORMAL,
6883 complain);
6884 release_tree_vector (rhs_vec);
6885 if (result == NULL_TREE)
6886 return error_mark_node;
6887 return result;
6890 else
6892 lhs = require_complete_type_sfinae (lhs, complain);
6893 if (lhs == error_mark_node)
6894 return error_mark_node;
6896 if (modifycode == NOP_EXPR)
6898 if (c_dialect_objc ())
6900 result = objc_maybe_build_modify_expr (lhs, rhs);
6901 if (result)
6902 return result;
6905 /* `operator=' is not an inheritable operator. */
6906 if (! MAYBE_CLASS_TYPE_P (lhstype))
6907 /* Do the default thing. */;
6908 else
6910 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
6911 lhs, rhs, make_node (NOP_EXPR),
6912 /*overload=*/NULL,
6913 complain);
6914 if (result == NULL_TREE)
6915 return error_mark_node;
6916 return result;
6918 lhstype = olhstype;
6920 else
6922 tree init = NULL_TREE;
6924 /* A binary op has been requested. Combine the old LHS
6925 value with the RHS producing the value we should actually
6926 store into the LHS. */
6927 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
6928 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
6929 || MAYBE_CLASS_TYPE_P (lhstype)));
6931 /* Preevaluate the RHS to make sure its evaluation is complete
6932 before the lvalue-to-rvalue conversion of the LHS:
6934 [expr.ass] With respect to an indeterminately-sequenced
6935 function call, the operation of a compound assignment is a
6936 single evaluation. [ Note: Therefore, a function call shall
6937 not intervene between the lvalue-to-rvalue conversion and the
6938 side effect associated with any single compound assignment
6939 operator. -- end note ] */
6940 lhs = stabilize_reference (lhs);
6941 if (TREE_SIDE_EFFECTS (rhs))
6942 rhs = mark_rvalue_use (rhs);
6943 rhs = stabilize_expr (rhs, &init);
6944 newrhs = cp_build_binary_op (input_location,
6945 modifycode, lhs, rhs,
6946 complain);
6947 if (newrhs == error_mark_node)
6949 if (complain & tf_error)
6950 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
6951 TREE_TYPE (lhs), TREE_TYPE (rhs));
6952 return error_mark_node;
6955 if (init)
6956 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
6958 /* Now it looks like a plain assignment. */
6959 modifycode = NOP_EXPR;
6960 if (c_dialect_objc ())
6962 result = objc_maybe_build_modify_expr (lhs, newrhs);
6963 if (result)
6964 return result;
6967 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
6968 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
6971 /* The left-hand side must be an lvalue. */
6972 if (!lvalue_or_else (lhs, lv_assign, complain))
6973 return error_mark_node;
6975 /* Warn about modifying something that is `const'. Don't warn if
6976 this is initialization. */
6977 if (modifycode != INIT_EXPR
6978 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
6979 /* Functions are not modifiable, even though they are
6980 lvalues. */
6981 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
6982 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
6983 /* If it's an aggregate and any field is const, then it is
6984 effectively const. */
6985 || (CLASS_TYPE_P (lhstype)
6986 && C_TYPE_FIELDS_READONLY (lhstype))))
6988 if (complain & tf_error)
6989 cxx_readonly_error (lhs, lv_assign);
6990 else
6991 return error_mark_node;
6994 /* If storing into a structure or union member, it may have been given a
6995 lowered bitfield type. We need to convert to the declared type first,
6996 so retrieve it now. */
6998 olhstype = unlowered_expr_type (lhs);
7000 /* Convert new value to destination type. */
7002 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7004 int from_array;
7006 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7008 if (modifycode != INIT_EXPR)
7010 if (complain & tf_error)
7011 error ("assigning to an array from an initializer list");
7012 return error_mark_node;
7014 if (check_array_initializer (lhs, lhstype, newrhs))
7015 return error_mark_node;
7016 newrhs = digest_init (lhstype, newrhs, complain);
7017 if (newrhs == error_mark_node)
7018 return error_mark_node;
7021 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7022 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7024 if (complain & tf_error)
7025 error ("incompatible types in assignment of %qT to %qT",
7026 TREE_TYPE (rhs), lhstype);
7027 return error_mark_node;
7030 /* Allow array assignment in compiler-generated code. */
7031 else if (!current_function_decl
7032 || !DECL_DEFAULTED_FN (current_function_decl))
7034 /* This routine is used for both initialization and assignment.
7035 Make sure the diagnostic message differentiates the context. */
7036 if (complain & tf_error)
7038 if (modifycode == INIT_EXPR)
7039 error ("array used as initializer");
7040 else
7041 error ("invalid array assignment");
7043 return error_mark_node;
7046 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7047 ? 1 + (modifycode != INIT_EXPR): 0;
7048 return build_vec_init (lhs, NULL_TREE, newrhs,
7049 /*explicit_value_init_p=*/false,
7050 from_array, complain);
7053 if (modifycode == INIT_EXPR)
7054 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7055 LOOKUP_ONLYCONVERTING. */
7056 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7057 ICR_INIT, NULL_TREE, 0,
7058 complain);
7059 else
7060 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7061 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7063 if (!same_type_p (lhstype, olhstype))
7064 newrhs = cp_convert_and_check (lhstype, newrhs);
7066 if (modifycode != INIT_EXPR)
7068 if (TREE_CODE (newrhs) == CALL_EXPR
7069 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7070 newrhs = build_cplus_new (lhstype, newrhs, complain);
7072 /* Can't initialize directly from a TARGET_EXPR, since that would
7073 cause the lhs to be constructed twice, and possibly result in
7074 accidental self-initialization. So we force the TARGET_EXPR to be
7075 expanded without a target. */
7076 if (TREE_CODE (newrhs) == TARGET_EXPR)
7077 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7078 TREE_OPERAND (newrhs, 0));
7081 if (newrhs == error_mark_node)
7082 return error_mark_node;
7084 if (c_dialect_objc () && flag_objc_gc)
7086 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7088 if (result)
7089 return result;
7092 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7093 lhstype, lhs, newrhs);
7095 TREE_SIDE_EFFECTS (result) = 1;
7096 if (!plain_assign)
7097 TREE_NO_WARNING (result) = 1;
7099 return result;
7102 tree
7103 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7104 tsubst_flags_t complain)
7106 if (processing_template_decl)
7107 return build_min_nt (MODOP_EXPR, lhs,
7108 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
7110 if (modifycode != NOP_EXPR)
7112 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7113 make_node (modifycode),
7114 /*overload=*/NULL,
7115 complain);
7116 if (rval)
7118 TREE_NO_WARNING (rval) = 1;
7119 return rval;
7122 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7125 /* Helper function for get_delta_difference which assumes FROM is a base
7126 class of TO. Returns a delta for the conversion of pointer-to-member
7127 of FROM to pointer-to-member of TO. If the conversion is invalid and
7128 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7129 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7130 If C_CAST_P is true, this conversion is taking place as part of a
7131 C-style cast. */
7133 static tree
7134 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7135 tsubst_flags_t complain)
7137 tree binfo;
7138 base_kind kind;
7139 base_access access = c_cast_p ? ba_unique : ba_check;
7141 /* Note: ba_quiet does not distinguish between access control and
7142 ambiguity. */
7143 if (!(complain & tf_error))
7144 access |= ba_quiet;
7146 binfo = lookup_base (to, from, access, &kind);
7148 if (kind == bk_inaccessible || kind == bk_ambig)
7150 if (!(complain & tf_error))
7151 return error_mark_node;
7153 error (" in pointer to member function conversion");
7154 return size_zero_node;
7156 else if (binfo)
7158 if (kind != bk_via_virtual)
7159 return BINFO_OFFSET (binfo);
7160 else
7161 /* FROM is a virtual base class of TO. Issue an error or warning
7162 depending on whether or not this is a reinterpret cast. */
7164 if (!(complain & tf_error))
7165 return error_mark_node;
7167 error ("pointer to member conversion via virtual base %qT",
7168 BINFO_TYPE (binfo_from_vbase (binfo)));
7170 return size_zero_node;
7173 else
7174 return NULL_TREE;
7177 /* Get difference in deltas for different pointer to member function
7178 types. If the conversion is invalid and tf_error is not set in
7179 COMPLAIN, returns error_mark_node, otherwise returns an integer
7180 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7181 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7182 conversions as well. If C_CAST_P is true this conversion is taking
7183 place as part of a C-style cast.
7185 Note that the naming of FROM and TO is kind of backwards; the return
7186 value is what we add to a TO in order to get a FROM. They are named
7187 this way because we call this function to find out how to convert from
7188 a pointer to member of FROM to a pointer to member of TO. */
7190 static tree
7191 get_delta_difference (tree from, tree to,
7192 bool allow_inverse_p,
7193 bool c_cast_p, tsubst_flags_t complain)
7195 tree result;
7197 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7198 /* Pointer to member of incomplete class is permitted*/
7199 result = size_zero_node;
7200 else
7201 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7203 if (result == error_mark_node)
7204 return error_mark_node;
7206 if (!result)
7208 if (!allow_inverse_p)
7210 if (!(complain & tf_error))
7211 return error_mark_node;
7213 error_not_base_type (from, to);
7214 error (" in pointer to member conversion");
7215 result = size_zero_node;
7217 else
7219 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7221 if (result == error_mark_node)
7222 return error_mark_node;
7224 if (result)
7225 result = size_diffop_loc (input_location,
7226 size_zero_node, result);
7227 else
7229 if (!(complain & tf_error))
7230 return error_mark_node;
7232 error_not_base_type (from, to);
7233 error (" in pointer to member conversion");
7234 result = size_zero_node;
7239 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7240 result));
7243 /* Return a constructor for the pointer-to-member-function TYPE using
7244 the other components as specified. */
7246 tree
7247 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7249 tree u = NULL_TREE;
7250 tree delta_field;
7251 tree pfn_field;
7252 VEC(constructor_elt, gc) *v;
7254 /* Pull the FIELD_DECLs out of the type. */
7255 pfn_field = TYPE_FIELDS (type);
7256 delta_field = DECL_CHAIN (pfn_field);
7258 /* Make sure DELTA has the type we want. */
7259 delta = convert_and_check (delta_type_node, delta);
7261 /* Convert to the correct target type if necessary. */
7262 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7264 /* Finish creating the initializer. */
7265 v = VEC_alloc(constructor_elt, gc, 2);
7266 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7267 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7268 u = build_constructor (type, v);
7269 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7270 TREE_STATIC (u) = (TREE_CONSTANT (u)
7271 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7272 != NULL_TREE)
7273 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7274 != NULL_TREE));
7275 return u;
7278 /* Build a constructor for a pointer to member function. It can be
7279 used to initialize global variables, local variable, or used
7280 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7281 want to be.
7283 If FORCE is nonzero, then force this conversion, even if
7284 we would rather not do it. Usually set when using an explicit
7285 cast. A C-style cast is being processed iff C_CAST_P is true.
7287 Return error_mark_node, if something goes wrong. */
7289 tree
7290 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7291 tsubst_flags_t complain)
7293 tree fn;
7294 tree pfn_type;
7295 tree to_type;
7297 if (error_operand_p (pfn))
7298 return error_mark_node;
7300 pfn_type = TREE_TYPE (pfn);
7301 to_type = build_ptrmemfunc_type (type);
7303 /* Handle multiple conversions of pointer to member functions. */
7304 if (TYPE_PTRMEMFUNC_P (pfn_type))
7306 tree delta = NULL_TREE;
7307 tree npfn = NULL_TREE;
7308 tree n;
7310 if (!force
7311 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7312 LOOKUP_NORMAL, complain))
7313 error ("invalid conversion to type %qT from type %qT",
7314 to_type, pfn_type);
7316 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7317 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7318 force,
7319 c_cast_p, complain);
7320 if (n == error_mark_node)
7321 return error_mark_node;
7323 /* We don't have to do any conversion to convert a
7324 pointer-to-member to its own type. But, we don't want to
7325 just return a PTRMEM_CST if there's an explicit cast; that
7326 cast should make the expression an invalid template argument. */
7327 if (TREE_CODE (pfn) != PTRMEM_CST)
7329 if (same_type_p (to_type, pfn_type))
7330 return pfn;
7331 else if (integer_zerop (n))
7332 return build_reinterpret_cast (to_type, pfn,
7333 complain);
7336 if (TREE_SIDE_EFFECTS (pfn))
7337 pfn = save_expr (pfn);
7339 /* Obtain the function pointer and the current DELTA. */
7340 if (TREE_CODE (pfn) == PTRMEM_CST)
7341 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7342 else
7344 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7345 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7348 /* Just adjust the DELTA field. */
7349 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7350 (TREE_TYPE (delta), ptrdiff_type_node));
7351 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7352 n = cp_build_binary_op (input_location,
7353 LSHIFT_EXPR, n, integer_one_node,
7354 complain);
7355 delta = cp_build_binary_op (input_location,
7356 PLUS_EXPR, delta, n, complain);
7357 return build_ptrmemfunc1 (to_type, delta, npfn);
7360 /* Handle null pointer to member function conversions. */
7361 if (null_ptr_cst_p (pfn))
7363 pfn = build_c_cast (input_location, type, nullptr_node);
7364 return build_ptrmemfunc1 (to_type,
7365 integer_zero_node,
7366 pfn);
7369 if (type_unknown_p (pfn))
7370 return instantiate_type (type, pfn, complain);
7372 fn = TREE_OPERAND (pfn, 0);
7373 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7374 /* In a template, we will have preserved the
7375 OFFSET_REF. */
7376 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7377 return make_ptrmem_cst (to_type, fn);
7380 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7381 given by CST.
7383 ??? There is no consistency as to the types returned for the above
7384 values. Some code acts as if it were a sizetype and some as if it were
7385 integer_type_node. */
7387 void
7388 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7390 tree type = TREE_TYPE (cst);
7391 tree fn = PTRMEM_CST_MEMBER (cst);
7392 tree ptr_class, fn_class;
7394 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7396 /* The class that the function belongs to. */
7397 fn_class = DECL_CONTEXT (fn);
7399 /* The class that we're creating a pointer to member of. */
7400 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7402 /* First, calculate the adjustment to the function's class. */
7403 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7404 /*c_cast_p=*/0, tf_warning_or_error);
7406 if (!DECL_VIRTUAL_P (fn))
7407 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7408 build_addr_func (fn, tf_warning_or_error));
7409 else
7411 /* If we're dealing with a virtual function, we have to adjust 'this'
7412 again, to point to the base which provides the vtable entry for
7413 fn; the call will do the opposite adjustment. */
7414 tree orig_class = DECL_CONTEXT (fn);
7415 tree binfo = binfo_or_else (orig_class, fn_class);
7416 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7417 *delta, BINFO_OFFSET (binfo));
7418 *delta = fold_if_not_in_template (*delta);
7420 /* We set PFN to the vtable offset at which the function can be
7421 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7422 case delta is shifted left, and then incremented). */
7423 *pfn = DECL_VINDEX (fn);
7424 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7425 TYPE_SIZE_UNIT (vtable_entry_type));
7426 *pfn = fold_if_not_in_template (*pfn);
7428 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7430 case ptrmemfunc_vbit_in_pfn:
7431 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7432 integer_one_node);
7433 *pfn = fold_if_not_in_template (*pfn);
7434 break;
7436 case ptrmemfunc_vbit_in_delta:
7437 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7438 *delta, integer_one_node);
7439 *delta = fold_if_not_in_template (*delta);
7440 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7441 *delta, integer_one_node);
7442 *delta = fold_if_not_in_template (*delta);
7443 break;
7445 default:
7446 gcc_unreachable ();
7449 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7450 *pfn = fold_if_not_in_template (*pfn);
7454 /* Return an expression for PFN from the pointer-to-member function
7455 given by T. */
7457 static tree
7458 pfn_from_ptrmemfunc (tree t)
7460 if (TREE_CODE (t) == PTRMEM_CST)
7462 tree delta;
7463 tree pfn;
7465 expand_ptrmemfunc_cst (t, &delta, &pfn);
7466 if (pfn)
7467 return pfn;
7470 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7473 /* Return an expression for DELTA from the pointer-to-member function
7474 given by T. */
7476 static tree
7477 delta_from_ptrmemfunc (tree t)
7479 if (TREE_CODE (t) == PTRMEM_CST)
7481 tree delta;
7482 tree pfn;
7484 expand_ptrmemfunc_cst (t, &delta, &pfn);
7485 if (delta)
7486 return delta;
7489 return build_ptrmemfunc_access_expr (t, delta_identifier);
7492 /* Convert value RHS to type TYPE as preparation for an assignment to
7493 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7494 implicit conversion is. If FNDECL is non-NULL, we are doing the
7495 conversion in order to pass the PARMNUMth argument of FNDECL.
7496 If FNDECL is NULL, we are doing the conversion in function pointer
7497 argument passing, conversion in initialization, etc. */
7499 static tree
7500 convert_for_assignment (tree type, tree rhs,
7501 impl_conv_rhs errtype, tree fndecl, int parmnum,
7502 tsubst_flags_t complain, int flags)
7504 tree rhstype;
7505 enum tree_code coder;
7507 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7508 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7509 rhs = TREE_OPERAND (rhs, 0);
7511 rhstype = TREE_TYPE (rhs);
7512 coder = TREE_CODE (rhstype);
7514 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7515 && vector_types_convertible_p (type, rhstype, true))
7517 rhs = mark_rvalue_use (rhs);
7518 return convert (type, rhs);
7521 if (rhs == error_mark_node || rhstype == error_mark_node)
7522 return error_mark_node;
7523 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7524 return error_mark_node;
7526 /* The RHS of an assignment cannot have void type. */
7527 if (coder == VOID_TYPE)
7529 if (complain & tf_error)
7530 error ("void value not ignored as it ought to be");
7531 return error_mark_node;
7534 /* Simplify the RHS if possible. */
7535 if (TREE_CODE (rhs) == CONST_DECL)
7536 rhs = DECL_INITIAL (rhs);
7538 if (c_dialect_objc ())
7540 int parmno;
7541 tree selector;
7542 tree rname = fndecl;
7544 switch (errtype)
7546 case ICR_ASSIGN:
7547 parmno = -1;
7548 break;
7549 case ICR_INIT:
7550 parmno = -2;
7551 break;
7552 default:
7553 selector = objc_message_selector ();
7554 parmno = parmnum;
7555 if (selector && parmno > 1)
7557 rname = selector;
7558 parmno -= 1;
7562 if (objc_compare_types (type, rhstype, parmno, rname))
7564 rhs = mark_rvalue_use (rhs);
7565 return convert (type, rhs);
7569 /* [expr.ass]
7571 The expression is implicitly converted (clause _conv_) to the
7572 cv-unqualified type of the left operand.
7574 We allow bad conversions here because by the time we get to this point
7575 we are committed to doing the conversion. If we end up doing a bad
7576 conversion, convert_like will complain. */
7577 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
7579 /* When -Wno-pmf-conversions is use, we just silently allow
7580 conversions from pointers-to-members to plain pointers. If
7581 the conversion doesn't work, cp_convert will complain. */
7582 if (!warn_pmf2ptr
7583 && TYPE_PTR_P (type)
7584 && TYPE_PTRMEMFUNC_P (rhstype))
7585 rhs = cp_convert (strip_top_quals (type), rhs);
7586 else
7588 if (complain & tf_error)
7590 /* If the right-hand side has unknown type, then it is an
7591 overloaded function. Call instantiate_type to get error
7592 messages. */
7593 if (rhstype == unknown_type_node)
7594 instantiate_type (type, rhs, tf_warning_or_error);
7595 else if (fndecl)
7596 error ("cannot convert %qT to %qT for argument %qP to %qD",
7597 rhstype, type, parmnum, fndecl);
7598 else
7599 switch (errtype)
7601 case ICR_DEFAULT_ARGUMENT:
7602 error ("cannot convert %qT to %qT in default argument",
7603 rhstype, type);
7604 break;
7605 case ICR_ARGPASS:
7606 error ("cannot convert %qT to %qT in argument passing",
7607 rhstype, type);
7608 break;
7609 case ICR_CONVERTING:
7610 error ("cannot convert %qT to %qT",
7611 rhstype, type);
7612 break;
7613 case ICR_INIT:
7614 error ("cannot convert %qT to %qT in initialization",
7615 rhstype, type);
7616 break;
7617 case ICR_RETURN:
7618 error ("cannot convert %qT to %qT in return",
7619 rhstype, type);
7620 break;
7621 case ICR_ASSIGN:
7622 error ("cannot convert %qT to %qT in assignment",
7623 rhstype, type);
7624 break;
7625 default:
7626 gcc_unreachable();
7629 return error_mark_node;
7632 if (warn_suggest_attribute_format)
7634 const enum tree_code codel = TREE_CODE (type);
7635 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7636 && coder == codel
7637 && check_missing_format_attribute (type, rhstype)
7638 && (complain & tf_warning))
7639 switch (errtype)
7641 case ICR_ARGPASS:
7642 case ICR_DEFAULT_ARGUMENT:
7643 if (fndecl)
7644 warning (OPT_Wsuggest_attribute_format,
7645 "parameter %qP of %qD might be a candidate "
7646 "for a format attribute", parmnum, fndecl);
7647 else
7648 warning (OPT_Wsuggest_attribute_format,
7649 "parameter might be a candidate "
7650 "for a format attribute");
7651 break;
7652 case ICR_CONVERTING:
7653 warning (OPT_Wsuggest_attribute_format,
7654 "target of conversion might be a candidate "
7655 "for a format attribute");
7656 break;
7657 case ICR_INIT:
7658 warning (OPT_Wsuggest_attribute_format,
7659 "target of initialization might be a candidate "
7660 "for a format attribute");
7661 break;
7662 case ICR_RETURN:
7663 warning (OPT_Wsuggest_attribute_format,
7664 "return type might be a candidate "
7665 "for a format attribute");
7666 break;
7667 case ICR_ASSIGN:
7668 warning (OPT_Wsuggest_attribute_format,
7669 "left-hand side of assignment might be a candidate "
7670 "for a format attribute");
7671 break;
7672 default:
7673 gcc_unreachable();
7677 /* If -Wparentheses, warn about a = b = c when a has type bool and b
7678 does not. */
7679 if (warn_parentheses
7680 && TREE_CODE (type) == BOOLEAN_TYPE
7681 && TREE_CODE (rhs) == MODIFY_EXPR
7682 && !TREE_NO_WARNING (rhs)
7683 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7684 && (complain & tf_warning))
7686 location_t loc = EXPR_LOC_OR_HERE (rhs);
7688 warning_at (loc, OPT_Wparentheses,
7689 "suggest parentheses around assignment used as truth value");
7690 TREE_NO_WARNING (rhs) = 1;
7693 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
7694 complain, flags);
7697 /* Convert RHS to be of type TYPE.
7698 If EXP is nonzero, it is the target of the initialization.
7699 ERRTYPE indicates what kind of error the implicit conversion is.
7701 Two major differences between the behavior of
7702 `convert_for_assignment' and `convert_for_initialization'
7703 are that references are bashed in the former, while
7704 copied in the latter, and aggregates are assigned in
7705 the former (operator=) while initialized in the
7706 latter (X(X&)).
7708 If using constructor make sure no conversion operator exists, if one does
7709 exist, an ambiguity exists.
7711 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
7713 tree
7714 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
7715 impl_conv_rhs errtype, tree fndecl, int parmnum,
7716 tsubst_flags_t complain)
7718 enum tree_code codel = TREE_CODE (type);
7719 tree rhstype;
7720 enum tree_code coder;
7722 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7723 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7724 if (TREE_CODE (rhs) == NOP_EXPR
7725 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7726 && codel != REFERENCE_TYPE)
7727 rhs = TREE_OPERAND (rhs, 0);
7729 if (type == error_mark_node
7730 || rhs == error_mark_node
7731 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7732 return error_mark_node;
7734 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7735 && TREE_CODE (type) != ARRAY_TYPE
7736 && (TREE_CODE (type) != REFERENCE_TYPE
7737 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7738 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7739 && (TREE_CODE (type) != REFERENCE_TYPE
7740 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7741 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7742 rhs = decay_conversion (rhs, complain);
7744 rhstype = TREE_TYPE (rhs);
7745 coder = TREE_CODE (rhstype);
7747 if (coder == ERROR_MARK)
7748 return error_mark_node;
7750 /* We accept references to incomplete types, so we can
7751 return here before checking if RHS is of complete type. */
7753 if (codel == REFERENCE_TYPE)
7755 /* This should eventually happen in convert_arguments. */
7756 int savew = 0, savee = 0;
7758 if (fndecl)
7759 savew = warningcount, savee = errorcount;
7760 rhs = initialize_reference (type, rhs, flags, complain);
7761 if (fndecl)
7763 if (warningcount > savew)
7764 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
7765 else if (errorcount > savee)
7766 error ("in passing argument %P of %q+D", parmnum, fndecl);
7768 return rhs;
7771 if (exp != 0)
7772 exp = require_complete_type_sfinae (exp, complain);
7773 if (exp == error_mark_node)
7774 return error_mark_node;
7776 rhstype = non_reference (rhstype);
7778 type = complete_type (type);
7780 if (DIRECT_INIT_EXPR_P (type, rhs))
7781 /* Don't try to do copy-initialization if we already have
7782 direct-initialization. */
7783 return rhs;
7785 if (MAYBE_CLASS_TYPE_P (type))
7786 return perform_implicit_conversion_flags (type, rhs, complain, flags);
7788 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
7789 complain, flags);
7792 /* If RETVAL is the address of, or a reference to, a local variable or
7793 temporary give an appropriate warning. */
7795 static void
7796 maybe_warn_about_returning_address_of_local (tree retval)
7798 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
7799 tree whats_returned = retval;
7801 for (;;)
7803 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7804 whats_returned = TREE_OPERAND (whats_returned, 1);
7805 else if (CONVERT_EXPR_P (whats_returned)
7806 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
7807 whats_returned = TREE_OPERAND (whats_returned, 0);
7808 else
7809 break;
7812 if (TREE_CODE (whats_returned) != ADDR_EXPR)
7813 return;
7814 whats_returned = TREE_OPERAND (whats_returned, 0);
7816 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7818 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7819 || TREE_CODE (whats_returned) == TARGET_EXPR)
7821 warning (0, "returning reference to temporary");
7822 return;
7824 if (TREE_CODE (whats_returned) == VAR_DECL
7825 && DECL_NAME (whats_returned)
7826 && TEMP_NAME_P (DECL_NAME (whats_returned)))
7828 warning (0, "reference to non-lvalue returned");
7829 return;
7833 while (TREE_CODE (whats_returned) == COMPONENT_REF
7834 || TREE_CODE (whats_returned) == ARRAY_REF)
7835 whats_returned = TREE_OPERAND (whats_returned, 0);
7837 if (DECL_P (whats_returned)
7838 && DECL_NAME (whats_returned)
7839 && DECL_FUNCTION_SCOPE_P (whats_returned)
7840 && !(TREE_STATIC (whats_returned)
7841 || TREE_PUBLIC (whats_returned)))
7843 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7844 warning (0, "reference to local variable %q+D returned",
7845 whats_returned);
7846 else
7847 warning (0, "address of local variable %q+D returned",
7848 whats_returned);
7849 return;
7853 /* Check that returning RETVAL from the current function is valid.
7854 Return an expression explicitly showing all conversions required to
7855 change RETVAL into the function return type, and to assign it to
7856 the DECL_RESULT for the function. Set *NO_WARNING to true if
7857 code reaches end of non-void function warning shouldn't be issued
7858 on this RETURN_EXPR. */
7860 tree
7861 check_return_expr (tree retval, bool *no_warning)
7863 tree result;
7864 /* The type actually returned by the function. */
7865 tree valtype;
7866 /* The type the function is declared to return, or void if
7867 the declared type is incomplete. */
7868 tree functype;
7869 int fn_returns_value_p;
7870 bool named_return_value_okay_p;
7872 *no_warning = false;
7874 /* A `volatile' function is one that isn't supposed to return, ever.
7875 (This is a G++ extension, used to get better code for functions
7876 that call the `volatile' function.) */
7877 if (TREE_THIS_VOLATILE (current_function_decl))
7878 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7880 /* Check for various simple errors. */
7881 if (DECL_DESTRUCTOR_P (current_function_decl))
7883 if (retval)
7884 error ("returning a value from a destructor");
7885 return NULL_TREE;
7887 else if (DECL_CONSTRUCTOR_P (current_function_decl))
7889 if (in_function_try_handler)
7890 /* If a return statement appears in a handler of the
7891 function-try-block of a constructor, the program is ill-formed. */
7892 error ("cannot return from a handler of a function-try-block of a constructor");
7893 else if (retval)
7894 /* You can't return a value from a constructor. */
7895 error ("returning a value from a constructor");
7896 return NULL_TREE;
7899 if (processing_template_decl)
7901 current_function_returns_value = 1;
7902 if (check_for_bare_parameter_packs (retval))
7903 retval = error_mark_node;
7904 return retval;
7907 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
7909 /* Deduce auto return type from a return statement. */
7910 if (current_function_auto_return_pattern)
7912 tree auto_node;
7913 tree type;
7915 if (!retval && !is_auto (current_function_auto_return_pattern))
7917 /* Give a helpful error message. */
7918 error ("return-statement with no value, in function returning %qT",
7919 current_function_auto_return_pattern);
7920 inform (input_location, "only plain %<auto%> return type can be "
7921 "deduced to %<void%>");
7922 type = error_mark_node;
7924 else
7926 if (!retval)
7927 retval = void_zero_node;
7928 auto_node = type_uses_auto (current_function_auto_return_pattern);
7929 type = do_auto_deduction (current_function_auto_return_pattern,
7930 retval, auto_node);
7933 if (type == error_mark_node)
7934 /* Leave it. */;
7935 else if (functype == current_function_auto_return_pattern)
7936 apply_deduced_return_type (current_function_decl, type);
7937 else
7938 /* A mismatch should have been diagnosed in do_auto_deduction. */
7939 gcc_assert (same_type_p (type, functype));
7940 functype = type;
7943 /* When no explicit return-value is given in a function with a named
7944 return value, the named return value is used. */
7945 result = DECL_RESULT (current_function_decl);
7946 valtype = TREE_TYPE (result);
7947 gcc_assert (valtype != NULL_TREE);
7948 fn_returns_value_p = !VOID_TYPE_P (valtype);
7949 if (!retval && DECL_NAME (result) && fn_returns_value_p)
7950 retval = result;
7952 /* Check for a return statement with no return value in a function
7953 that's supposed to return a value. */
7954 if (!retval && fn_returns_value_p)
7956 if (functype != error_mark_node)
7957 permerror (input_location, "return-statement with no value, in "
7958 "function returning %qT", valtype);
7959 /* Remember that this function did return. */
7960 current_function_returns_value = 1;
7961 /* And signal caller that TREE_NO_WARNING should be set on the
7962 RETURN_EXPR to avoid control reaches end of non-void function
7963 warnings in tree-cfg.c. */
7964 *no_warning = true;
7966 /* Check for a return statement with a value in a function that
7967 isn't supposed to return a value. */
7968 else if (retval && !fn_returns_value_p)
7970 if (VOID_TYPE_P (TREE_TYPE (retval)))
7971 /* You can return a `void' value from a function of `void'
7972 type. In that case, we have to evaluate the expression for
7973 its side-effects. */
7974 finish_expr_stmt (retval);
7975 else
7976 permerror (input_location, "return-statement with a value, in function "
7977 "returning 'void'");
7978 current_function_returns_null = 1;
7980 /* There's really no value to return, after all. */
7981 return NULL_TREE;
7983 else if (!retval)
7984 /* Remember that this function can sometimes return without a
7985 value. */
7986 current_function_returns_null = 1;
7987 else
7988 /* Remember that this function did return a value. */
7989 current_function_returns_value = 1;
7991 /* Check for erroneous operands -- but after giving ourselves a
7992 chance to provide an error about returning a value from a void
7993 function. */
7994 if (error_operand_p (retval))
7996 current_function_return_value = error_mark_node;
7997 return error_mark_node;
8000 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8001 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8002 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8003 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8004 && ! flag_check_new
8005 && retval && null_ptr_cst_p (retval))
8006 warning (0, "%<operator new%> must not return NULL unless it is "
8007 "declared %<throw()%> (or -fcheck-new is in effect)");
8009 /* Effective C++ rule 15. See also start_function. */
8010 if (warn_ecpp
8011 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8013 bool warn = true;
8015 /* The function return type must be a reference to the current
8016 class. */
8017 if (TREE_CODE (valtype) == REFERENCE_TYPE
8018 && same_type_ignoring_top_level_qualifiers_p
8019 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8021 /* Returning '*this' is obviously OK. */
8022 if (retval == current_class_ref)
8023 warn = false;
8024 /* If we are calling a function whose return type is the same of
8025 the current class reference, it is ok. */
8026 else if (TREE_CODE (retval) == INDIRECT_REF
8027 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8028 warn = false;
8031 if (warn)
8032 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8035 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8037 [...] For a function with a class return type, if the expression
8038 in the return statement is the name of a local object, and the cv-
8039 unqualified type of the local object is the same as the function
8040 return type, an implementation is permitted to omit creating the tem-
8041 porary object to hold the function return value [...]
8043 So, if this is a value-returning function that always returns the same
8044 local variable, remember it.
8046 It might be nice to be more flexible, and choose the first suitable
8047 variable even if the function sometimes returns something else, but
8048 then we run the risk of clobbering the variable we chose if the other
8049 returned expression uses the chosen variable somehow. And people expect
8050 this restriction, anyway. (jason 2000-11-19)
8052 See finish_function and finalize_nrv for the rest of this optimization. */
8054 named_return_value_okay_p =
8055 (retval != NULL_TREE
8056 /* Must be a local, automatic variable. */
8057 && TREE_CODE (retval) == VAR_DECL
8058 && DECL_CONTEXT (retval) == current_function_decl
8059 && ! TREE_STATIC (retval)
8060 && ! DECL_ANON_UNION_VAR_P (retval)
8061 && (DECL_ALIGN (retval) >= DECL_ALIGN (result))
8062 /* The cv-unqualified type of the returned value must be the
8063 same as the cv-unqualified return type of the
8064 function. */
8065 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8066 (TYPE_MAIN_VARIANT (functype)))
8067 /* And the returned value must be non-volatile. */
8068 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8070 if (fn_returns_value_p && flag_elide_constructors)
8072 if (named_return_value_okay_p
8073 && (current_function_return_value == NULL_TREE
8074 || current_function_return_value == retval))
8075 current_function_return_value = retval;
8076 else
8077 current_function_return_value = error_mark_node;
8080 /* We don't need to do any conversions when there's nothing being
8081 returned. */
8082 if (!retval)
8083 return NULL_TREE;
8085 /* Do any required conversions. */
8086 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8087 /* No conversions are required. */
8089 else
8091 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8093 /* The functype's return type will have been set to void, if it
8094 was an incomplete type. Just treat this as 'return;' */
8095 if (VOID_TYPE_P (functype))
8096 return error_mark_node;
8098 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8099 treated as an rvalue for the purposes of overload resolution to
8100 favor move constructors over copy constructors.
8102 Note that these conditions are similar to, but not as strict as,
8103 the conditions for the named return value optimization. */
8104 if ((cxx_dialect != cxx98)
8105 && (TREE_CODE (retval) == VAR_DECL
8106 || TREE_CODE (retval) == PARM_DECL)
8107 && DECL_CONTEXT (retval) == current_function_decl
8108 && !TREE_STATIC (retval)
8109 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8110 (TYPE_MAIN_VARIANT (functype)))
8111 /* This is only interesting for class type. */
8112 && CLASS_TYPE_P (functype))
8113 flags = flags | LOOKUP_PREFER_RVALUE;
8115 /* First convert the value to the function's return type, then
8116 to the type of return value's location to handle the
8117 case that functype is smaller than the valtype. */
8118 retval = convert_for_initialization
8119 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8120 tf_warning_or_error);
8121 retval = convert (valtype, retval);
8123 /* If the conversion failed, treat this just like `return;'. */
8124 if (retval == error_mark_node)
8125 return retval;
8126 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8127 else if (! cfun->returns_struct
8128 && TREE_CODE (retval) == TARGET_EXPR
8129 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8130 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8131 TREE_OPERAND (retval, 0));
8132 else
8133 maybe_warn_about_returning_address_of_local (retval);
8136 /* Actually copy the value returned into the appropriate location. */
8137 if (retval && retval != result)
8138 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8140 return retval;
8144 /* Returns nonzero if the pointer-type FROM can be converted to the
8145 pointer-type TO via a qualification conversion. If CONSTP is -1,
8146 then we return nonzero if the pointers are similar, and the
8147 cv-qualification signature of FROM is a proper subset of that of TO.
8149 If CONSTP is positive, then all outer pointers have been
8150 const-qualified. */
8152 static int
8153 comp_ptr_ttypes_real (tree to, tree from, int constp)
8155 bool to_more_cv_qualified = false;
8156 bool is_opaque_pointer = false;
8158 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8160 if (TREE_CODE (to) != TREE_CODE (from))
8161 return 0;
8163 if (TREE_CODE (from) == OFFSET_TYPE
8164 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8165 TYPE_OFFSET_BASETYPE (to)))
8166 return 0;
8168 /* Const and volatile mean something different for function types,
8169 so the usual checks are not appropriate. */
8170 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8172 if (!at_least_as_qualified_p (to, from))
8173 return 0;
8175 if (!at_least_as_qualified_p (from, to))
8177 if (constp == 0)
8178 return 0;
8179 to_more_cv_qualified = true;
8182 if (constp > 0)
8183 constp &= TYPE_READONLY (to);
8186 if (TREE_CODE (to) == VECTOR_TYPE)
8187 is_opaque_pointer = vector_targets_convertible_p (to, from);
8189 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
8190 return ((constp >= 0 || to_more_cv_qualified)
8191 && (is_opaque_pointer
8192 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8196 /* When comparing, say, char ** to char const **, this function takes
8197 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8198 types to this function. */
8201 comp_ptr_ttypes (tree to, tree from)
8203 return comp_ptr_ttypes_real (to, from, 1);
8206 /* Returns true iff FNTYPE is a non-class type that involves
8207 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8208 if a parameter type is ill-formed. */
8210 bool
8211 error_type_p (const_tree type)
8213 tree t;
8215 switch (TREE_CODE (type))
8217 case ERROR_MARK:
8218 return true;
8220 case POINTER_TYPE:
8221 case REFERENCE_TYPE:
8222 case OFFSET_TYPE:
8223 return error_type_p (TREE_TYPE (type));
8225 case FUNCTION_TYPE:
8226 case METHOD_TYPE:
8227 if (error_type_p (TREE_TYPE (type)))
8228 return true;
8229 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8230 if (error_type_p (TREE_VALUE (t)))
8231 return true;
8232 return false;
8234 case RECORD_TYPE:
8235 if (TYPE_PTRMEMFUNC_P (type))
8236 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8237 return false;
8239 default:
8240 return false;
8244 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8245 type or inheritance-related types, regardless of cv-quals. */
8248 ptr_reasonably_similar (const_tree to, const_tree from)
8250 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8252 /* Any target type is similar enough to void. */
8253 if (TREE_CODE (to) == VOID_TYPE)
8254 return !error_type_p (from);
8255 if (TREE_CODE (from) == VOID_TYPE)
8256 return !error_type_p (to);
8258 if (TREE_CODE (to) != TREE_CODE (from))
8259 return 0;
8261 if (TREE_CODE (from) == OFFSET_TYPE
8262 && comptypes (TYPE_OFFSET_BASETYPE (to),
8263 TYPE_OFFSET_BASETYPE (from),
8264 COMPARE_BASE | COMPARE_DERIVED))
8265 continue;
8267 if (TREE_CODE (to) == VECTOR_TYPE
8268 && vector_types_convertible_p (to, from, false))
8269 return 1;
8271 if (TREE_CODE (to) == INTEGER_TYPE
8272 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8273 return 1;
8275 if (TREE_CODE (to) == FUNCTION_TYPE)
8276 return !error_type_p (to) && !error_type_p (from);
8278 if (TREE_CODE (to) != POINTER_TYPE)
8279 return comptypes
8280 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8281 COMPARE_BASE | COMPARE_DERIVED);
8285 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8286 pointer-to-member types) are the same, ignoring cv-qualification at
8287 all levels. */
8289 bool
8290 comp_ptr_ttypes_const (tree to, tree from)
8292 bool is_opaque_pointer = false;
8294 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8296 if (TREE_CODE (to) != TREE_CODE (from))
8297 return false;
8299 if (TREE_CODE (from) == OFFSET_TYPE
8300 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8301 TYPE_OFFSET_BASETYPE (to)))
8302 continue;
8304 if (TREE_CODE (to) == VECTOR_TYPE)
8305 is_opaque_pointer = vector_targets_convertible_p (to, from);
8307 if (TREE_CODE (to) != POINTER_TYPE)
8308 return (is_opaque_pointer
8309 || same_type_ignoring_top_level_qualifiers_p (to, from));
8313 /* Returns the type qualifiers for this type, including the qualifiers on the
8314 elements for an array type. */
8317 cp_type_quals (const_tree type)
8319 int quals;
8320 /* This CONST_CAST is okay because strip_array_types returns its
8321 argument unmodified and we assign it to a const_tree. */
8322 type = strip_array_types (CONST_CAST_TREE (type));
8323 if (type == error_mark_node
8324 /* Quals on a FUNCTION_TYPE are memfn quals. */
8325 || TREE_CODE (type) == FUNCTION_TYPE)
8326 return TYPE_UNQUALIFIED;
8327 quals = TYPE_QUALS (type);
8328 /* METHOD and REFERENCE_TYPEs should never have quals. */
8329 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8330 && TREE_CODE (type) != REFERENCE_TYPE)
8331 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8332 == TYPE_UNQUALIFIED));
8333 return quals;
8336 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8337 METHOD_TYPE. */
8340 type_memfn_quals (const_tree type)
8342 if (TREE_CODE (type) == FUNCTION_TYPE)
8343 return TYPE_QUALS (type);
8344 else if (TREE_CODE (type) == METHOD_TYPE)
8345 return cp_type_quals (class_of_this_parm (type));
8346 else
8347 gcc_unreachable ();
8350 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8351 MEMFN_QUALS. */
8353 tree
8354 apply_memfn_quals (tree type, cp_cv_quals memfn_quals)
8356 /* Could handle METHOD_TYPE here if necessary. */
8357 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8358 if (TYPE_QUALS (type) == memfn_quals)
8359 return type;
8360 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8361 complex. */
8362 return build_qualified_type (type, memfn_quals);
8365 /* Returns nonzero if TYPE is const or volatile. */
8367 bool
8368 cv_qualified_p (const_tree type)
8370 int quals = cp_type_quals (type);
8371 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8374 /* Returns nonzero if the TYPE contains a mutable member. */
8376 bool
8377 cp_has_mutable_p (const_tree type)
8379 /* This CONST_CAST is okay because strip_array_types returns its
8380 argument unmodified and we assign it to a const_tree. */
8381 type = strip_array_types (CONST_CAST_TREE(type));
8383 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8386 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8387 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8388 approximation. In particular, consider:
8390 int f();
8391 struct S { int i; };
8392 const S s = { f(); }
8394 Here, we will make "s" as TREE_READONLY (because it is declared
8395 "const") -- only to reverse ourselves upon seeing that the
8396 initializer is non-constant. */
8398 void
8399 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8401 tree type = TREE_TYPE (decl);
8403 if (type == error_mark_node)
8404 return;
8406 if (TREE_CODE (decl) == TYPE_DECL)
8407 return;
8409 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8410 && type_quals != TYPE_UNQUALIFIED));
8412 /* Avoid setting TREE_READONLY incorrectly. */
8413 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8414 constructor can produce constant init, so rely on cp_finish_decl to
8415 clear TREE_READONLY if the variable has non-constant init. */
8417 /* If the type has a mutable component, that component might be
8418 modified. */
8419 if (TYPE_HAS_MUTABLE_P (type))
8420 type_quals &= ~TYPE_QUAL_CONST;
8422 c_apply_type_quals_to_decl (type_quals, decl);
8425 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8426 exemplar types such that casting T1 to T2 is casting away constness
8427 if and only if there is no implicit conversion from T1 to T2. */
8429 static void
8430 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8432 int quals1;
8433 int quals2;
8435 /* [expr.const.cast]
8437 For multi-level pointer to members and multi-level mixed pointers
8438 and pointers to members (conv.qual), the "member" aspect of a
8439 pointer to member level is ignored when determining if a const
8440 cv-qualifier has been cast away. */
8441 /* [expr.const.cast]
8443 For two pointer types:
8445 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8446 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8447 K is min(N,M)
8449 casting from X1 to X2 casts away constness if, for a non-pointer
8450 type T there does not exist an implicit conversion (clause
8451 _conv_) from:
8453 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8457 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8458 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1))
8459 || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2)))
8461 *t1 = cp_build_qualified_type (void_type_node,
8462 cp_type_quals (*t1));
8463 *t2 = cp_build_qualified_type (void_type_node,
8464 cp_type_quals (*t2));
8465 return;
8468 quals1 = cp_type_quals (*t1);
8469 quals2 = cp_type_quals (*t2);
8471 if (TYPE_PTRMEM_P (*t1))
8472 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8473 else
8474 *t1 = TREE_TYPE (*t1);
8475 if (TYPE_PTRMEM_P (*t2))
8476 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8477 else
8478 *t2 = TREE_TYPE (*t2);
8480 casts_away_constness_r (t1, t2, complain);
8481 *t1 = build_pointer_type (*t1);
8482 *t2 = build_pointer_type (*t2);
8483 *t1 = cp_build_qualified_type (*t1, quals1);
8484 *t2 = cp_build_qualified_type (*t2, quals2);
8487 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8488 constness.
8490 ??? This function returns non-zero if casting away qualifiers not
8491 just const. We would like to return to the caller exactly which
8492 qualifiers are casted away to give more accurate diagnostics.
8495 static bool
8496 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8498 if (TREE_CODE (t2) == REFERENCE_TYPE)
8500 /* [expr.const.cast]
8502 Casting from an lvalue of type T1 to an lvalue of type T2
8503 using a reference cast casts away constness if a cast from an
8504 rvalue of type "pointer to T1" to the type "pointer to T2"
8505 casts away constness. */
8506 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8507 return casts_away_constness (build_pointer_type (t1),
8508 build_pointer_type (TREE_TYPE (t2)),
8509 complain);
8512 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
8513 /* [expr.const.cast]
8515 Casting from an rvalue of type "pointer to data member of X
8516 of type T1" to the type "pointer to data member of Y of type
8517 T2" casts away constness if a cast from an rvalue of type
8518 "pointer to T1" to the type "pointer to T2" casts away
8519 constness. */
8520 return casts_away_constness
8521 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8522 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
8523 complain);
8525 /* Casting away constness is only something that makes sense for
8526 pointer or reference types. */
8527 if (TREE_CODE (t1) != POINTER_TYPE
8528 || TREE_CODE (t2) != POINTER_TYPE)
8529 return false;
8531 /* Top-level qualifiers don't matter. */
8532 t1 = TYPE_MAIN_VARIANT (t1);
8533 t2 = TYPE_MAIN_VARIANT (t2);
8534 casts_away_constness_r (&t1, &t2, complain);
8535 if (!can_convert (t2, t1, complain))
8536 return true;
8538 return false;
8541 /* If T is a REFERENCE_TYPE return the type to which T refers.
8542 Otherwise, return T itself. */
8544 tree
8545 non_reference (tree t)
8547 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8548 t = TREE_TYPE (t);
8549 return t;
8553 /* Return nonzero if REF is an lvalue valid for this language;
8554 otherwise, print an error message and return zero. USE says
8555 how the lvalue is being used and so selects the error message. */
8558 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8560 cp_lvalue_kind kind = lvalue_kind (ref);
8562 if (kind == clk_none)
8564 if (complain & tf_error)
8565 lvalue_error (input_location, use);
8566 return 0;
8568 else if (kind & (clk_rvalueref|clk_class))
8570 if (!(complain & tf_error))
8571 return 0;
8572 if (kind & clk_class)
8573 /* Make this a permerror because we used to accept it. */
8574 permerror (input_location, "using temporary as lvalue");
8575 else
8576 error ("using xvalue (rvalue reference) as lvalue");
8578 return 1;
8581 /* Return true if a user-defined literal operator is a raw operator. */
8583 bool
8584 check_raw_literal_operator (const_tree decl)
8586 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8587 tree argtype;
8588 int arity;
8589 bool maybe_raw_p = false;
8591 /* Count the number and type of arguments and check for ellipsis. */
8592 for (argtype = argtypes, arity = 0;
8593 argtype && argtype != void_list_node;
8594 ++arity, argtype = TREE_CHAIN (argtype))
8596 tree t = TREE_VALUE (argtype);
8598 if (same_type_p (t, const_string_type_node))
8599 maybe_raw_p = true;
8601 if (!argtype)
8602 return false; /* Found ellipsis. */
8604 if (!maybe_raw_p || arity != 1)
8605 return false;
8607 return true;
8611 /* Return true if a user-defined literal operator has one of the allowed
8612 argument types. */
8614 bool
8615 check_literal_operator_args (const_tree decl,
8616 bool *long_long_unsigned_p, bool *long_double_p)
8618 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8620 *long_long_unsigned_p = false;
8621 *long_double_p = false;
8622 if (processing_template_decl || processing_specialization)
8623 return argtypes == void_list_node;
8624 else
8626 tree argtype;
8627 int arity;
8628 int max_arity = 2;
8630 /* Count the number and type of arguments and check for ellipsis. */
8631 for (argtype = argtypes, arity = 0;
8632 argtype && argtype != void_list_node;
8633 argtype = TREE_CHAIN (argtype))
8635 tree t = TREE_VALUE (argtype);
8636 ++arity;
8638 if (TREE_CODE (t) == POINTER_TYPE)
8640 bool maybe_raw_p = false;
8641 t = TREE_TYPE (t);
8642 if (cp_type_quals (t) != TYPE_QUAL_CONST)
8643 return false;
8644 t = TYPE_MAIN_VARIANT (t);
8645 if ((maybe_raw_p = same_type_p (t, char_type_node))
8646 || same_type_p (t, wchar_type_node)
8647 || same_type_p (t, char16_type_node)
8648 || same_type_p (t, char32_type_node))
8650 argtype = TREE_CHAIN (argtype);
8651 if (!argtype)
8652 return false;
8653 t = TREE_VALUE (argtype);
8654 if (maybe_raw_p && argtype == void_list_node)
8655 return true;
8656 else if (same_type_p (t, size_type_node))
8658 ++arity;
8659 continue;
8661 else
8662 return false;
8665 else if (same_type_p (t, long_long_unsigned_type_node))
8667 max_arity = 1;
8668 *long_long_unsigned_p = true;
8670 else if (same_type_p (t, long_double_type_node))
8672 max_arity = 1;
8673 *long_double_p = true;
8675 else if (same_type_p (t, char_type_node))
8676 max_arity = 1;
8677 else if (same_type_p (t, wchar_type_node))
8678 max_arity = 1;
8679 else if (same_type_p (t, char16_type_node))
8680 max_arity = 1;
8681 else if (same_type_p (t, char32_type_node))
8682 max_arity = 1;
8683 else
8684 return false;
8686 if (!argtype)
8687 return false; /* Found ellipsis. */
8689 if (arity != max_arity)
8690 return false;
8692 return true;