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
4 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization. */
31 #include "coretypes.h"
41 #include "diagnostic.h"
48 static tree pfn_from_ptrmemfunc (tree);
49 static tree delta_from_ptrmemfunc (tree);
50 static tree convert_for_assignment (tree, tree, const char *, tree, int);
51 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
52 static tree rationalize_conditional_expr (enum tree_code, tree);
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);
58 static void casts_away_constness_r (tree *, tree *);
59 static bool casts_away_constness (tree, tree);
60 static void maybe_warn_about_returning_address_of_local (tree);
61 static tree lookup_destructor (tree, tree, tree);
62 static int convert_arguments (int, tree *, tree, tree, tree, int);
64 /* Do `exp = require_complete_type (exp);' to make sure exp
65 does not have an incomplete type. (That includes void types.)
66 Returns the error_mark_node if the VALUE does not have
67 complete type when this function returns. */
70 require_complete_type (tree value)
74 if (processing_template_decl || value == error_mark_node)
77 if (TREE_CODE (value) == OVERLOAD)
78 type = unknown_type_node;
80 type = TREE_TYPE (value);
82 if (type == error_mark_node)
83 return error_mark_node;
85 /* First, detect a valid value with a complete type. */
86 if (COMPLETE_TYPE_P (type))
89 if (complete_type_or_else (type, value))
92 return error_mark_node;
95 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
96 a template instantiation, do the instantiation. Returns TYPE,
97 whether or not it could be completed, unless something goes
98 horribly wrong, in which case the error_mark_node is returned. */
101 complete_type (tree type)
103 if (type == NULL_TREE)
104 /* Rather than crash, we return something sure to cause an error
106 return error_mark_node;
108 if (type == error_mark_node || COMPLETE_TYPE_P (type))
110 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
112 tree t = complete_type (TREE_TYPE (type));
113 unsigned int needs_constructing, has_nontrivial_dtor;
114 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
117 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
119 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
120 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
122 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
123 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
126 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
127 instantiate_class_template (TYPE_MAIN_VARIANT (type));
132 /* Like complete_type, but issue an error if the TYPE cannot be completed.
133 VALUE is used for informative diagnostics.
134 Returns NULL_TREE if the type cannot be made complete. */
137 complete_type_or_else (tree type, tree value)
139 type = complete_type (type);
140 if (type == error_mark_node)
141 /* We already issued an error. */
143 else if (!COMPLETE_TYPE_P (type))
145 cxx_incomplete_type_diagnostic (value, type, 0);
152 /* Return truthvalue of whether type of EXP is instantiated. */
155 type_unknown_p (const_tree exp)
157 return (TREE_CODE (exp) == TREE_LIST
158 || TREE_TYPE (exp) == unknown_type_node);
162 /* Return the common type of two parameter lists.
163 We assume that cp_comptypes has already been done and returned 1;
164 if that isn't so, this may crash.
166 As an optimization, free the space we allocate if the parameter
167 lists are already common. */
170 commonparms (tree p1, tree p2)
172 tree oldargs = p1, newargs, n;
176 len = list_length (p1);
177 newargs = tree_last (p1);
179 if (newargs == void_list_node)
188 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
193 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
195 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
197 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
200 else if (! TREE_PURPOSE (p1))
202 if (TREE_PURPOSE (p2))
204 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
210 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
212 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
214 if (TREE_VALUE (p1) != TREE_VALUE (p2))
217 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
220 TREE_VALUE (n) = TREE_VALUE (p1);
228 /* Given a type, perhaps copied for a typedef,
229 find the "original" version of it. */
231 original_type (tree t)
233 int quals = cp_type_quals (t);
234 while (t != error_mark_node
235 && TYPE_NAME (t) != NULL_TREE)
237 tree x = TYPE_NAME (t);
238 if (TREE_CODE (x) != TYPE_DECL)
240 x = DECL_ORIGINAL_TYPE (x);
245 return cp_build_qualified_type (t, quals);
248 /* T1 and T2 are arithmetic or enumeration types. Return the type
249 that will result from the "usual arithmetic conversions" on T1 and
250 T2 as described in [expr]. */
253 type_after_usual_arithmetic_conversions (tree t1, tree t2)
255 enum tree_code code1 = TREE_CODE (t1);
256 enum tree_code code2 = TREE_CODE (t2);
259 /* FIXME: Attributes. */
260 gcc_assert (ARITHMETIC_TYPE_P (t1)
261 || TREE_CODE (t1) == VECTOR_TYPE
262 || TREE_CODE (t1) == ENUMERAL_TYPE);
263 gcc_assert (ARITHMETIC_TYPE_P (t2)
264 || TREE_CODE (t2) == VECTOR_TYPE
265 || TREE_CODE (t2) == ENUMERAL_TYPE);
267 /* In what follows, we slightly generalize the rules given in [expr] so
268 as to deal with `long long' and `complex'. First, merge the
270 attributes = (*targetm.merge_type_attributes) (t1, t2);
272 /* If one type is complex, form the common type of the non-complex
273 components, then make that complex. Use T1 or T2 if it is the
275 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
277 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
278 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
280 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
282 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
283 return build_type_attribute_variant (t1, attributes);
284 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
285 return build_type_attribute_variant (t2, attributes);
287 return build_type_attribute_variant (build_complex_type (subtype),
291 if (code1 == VECTOR_TYPE)
293 /* When we get here we should have two vectors of the same size.
294 Just prefer the unsigned one if present. */
295 if (TYPE_UNSIGNED (t1))
296 return build_type_attribute_variant (t1, attributes);
298 return build_type_attribute_variant (t2, attributes);
301 /* If only one is real, use it as the result. */
302 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
303 return build_type_attribute_variant (t1, attributes);
304 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
305 return build_type_attribute_variant (t2, attributes);
307 /* Perform the integral promotions. */
308 if (code1 != REAL_TYPE)
310 t1 = type_promotes_to (t1);
311 t2 = type_promotes_to (t2);
314 /* Both real or both integers; use the one with greater precision. */
315 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
316 return build_type_attribute_variant (t1, attributes);
317 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
318 return build_type_attribute_variant (t2, attributes);
320 /* The types are the same; no need to do anything fancy. */
321 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
322 return build_type_attribute_variant (t1, attributes);
324 if (code1 != REAL_TYPE)
326 /* If one is unsigned long long, then convert the other to unsigned
328 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
329 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
330 return build_type_attribute_variant (long_long_unsigned_type_node,
332 /* If one is a long long, and the other is an unsigned long, and
333 long long can represent all the values of an unsigned long, then
334 convert to a long long. Otherwise, convert to an unsigned long
335 long. Otherwise, if either operand is long long, convert the
338 Since we're here, we know the TYPE_PRECISION is the same;
339 therefore converting to long long cannot represent all the values
340 of an unsigned long, so we choose unsigned long long in that
342 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
343 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
345 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
346 ? long_long_unsigned_type_node
347 : long_long_integer_type_node);
348 return build_type_attribute_variant (t, attributes);
351 /* Go through the same procedure, but for longs. */
352 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
353 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
354 return build_type_attribute_variant (long_unsigned_type_node,
356 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
357 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
359 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
360 ? long_unsigned_type_node : long_integer_type_node);
361 return build_type_attribute_variant (t, attributes);
363 /* Otherwise prefer the unsigned one. */
364 if (TYPE_UNSIGNED (t1))
365 return build_type_attribute_variant (t1, attributes);
367 return build_type_attribute_variant (t2, attributes);
371 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
372 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
373 return build_type_attribute_variant (long_double_type_node,
375 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
376 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
377 return build_type_attribute_variant (double_type_node,
379 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
380 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
381 return build_type_attribute_variant (float_type_node,
384 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
385 the standard C++ floating-point types. Logic earlier in this
386 function has already eliminated the possibility that
387 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
388 compelling reason to choose one or the other. */
389 return build_type_attribute_variant (t1, attributes);
393 /* Subroutine of composite_pointer_type to implement the recursive
394 case. See that function for documentation fo the parameters. */
397 composite_pointer_type_r (tree t1, tree t2, const char* location)
404 /* Determine the types pointed to by T1 and T2. */
405 if (TREE_CODE (t1) == POINTER_TYPE)
407 pointee1 = TREE_TYPE (t1);
408 pointee2 = TREE_TYPE (t2);
412 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
413 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
418 Otherwise, the composite pointer type is a pointer type
419 similar (_conv.qual_) to the type of one of the operands,
420 with a cv-qualification signature (_conv.qual_) that is the
421 union of the cv-qualification signatures of the operand
423 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
424 result_type = pointee1;
425 else if ((TREE_CODE (pointee1) == POINTER_TYPE
426 && TREE_CODE (pointee2) == POINTER_TYPE)
427 || (TYPE_PTR_TO_MEMBER_P (pointee1)
428 && TYPE_PTR_TO_MEMBER_P (pointee2)))
429 result_type = composite_pointer_type_r (pointee1, pointee2, location);
432 pedwarn ("%s between distinct pointer types %qT and %qT "
435 result_type = void_type_node;
437 result_type = cp_build_qualified_type (result_type,
438 (cp_type_quals (pointee1)
439 | cp_type_quals (pointee2)));
440 /* If the original types were pointers to members, so is the
442 if (TYPE_PTR_TO_MEMBER_P (t1))
444 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
445 TYPE_PTRMEM_CLASS_TYPE (t2)))
446 pedwarn ("%s between distinct pointer types %qT and %qT "
449 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
453 result_type = build_pointer_type (result_type);
455 /* Merge the attributes. */
456 attributes = (*targetm.merge_type_attributes) (t1, t2);
457 return build_type_attribute_variant (result_type, attributes);
460 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
461 ARG1 and ARG2 are the values with those types. The LOCATION is a
462 string describing the current location, in case an error occurs.
464 This routine also implements the computation of a common type for
465 pointers-to-members as per [expr.eq]. */
468 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
469 const char* location)
476 If one operand is a null pointer constant, the composite pointer
477 type is the type of the other operand. */
478 if (null_ptr_cst_p (arg1))
480 if (null_ptr_cst_p (arg2))
487 If one of the operands has type "pointer to cv1 void*", then
488 the other has type "pointer to cv2T", and the composite pointer
489 type is "pointer to cv12 void", where cv12 is the union of cv1
492 If either type is a pointer to void, make sure it is T1. */
493 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
501 /* Now, if T1 is a pointer to void, merge the qualifiers. */
502 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
507 if (pedantic && TYPE_PTRFN_P (t2))
508 pedwarn ("ISO C++ forbids %s between pointer of type %<void *%> "
509 "and pointer-to-function", location);
511 = cp_build_qualified_type (void_type_node,
512 (cp_type_quals (TREE_TYPE (t1))
513 | cp_type_quals (TREE_TYPE (t2))));
514 result_type = build_pointer_type (result_type);
515 /* Merge the attributes. */
516 attributes = (*targetm.merge_type_attributes) (t1, t2);
517 return build_type_attribute_variant (result_type, attributes);
520 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
521 && TREE_CODE (t2) == POINTER_TYPE)
523 if (objc_compare_types (t1, t2, -3, NULL_TREE))
527 /* [expr.eq] permits the application of a pointer conversion to
528 bring the pointers to a common type. */
529 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
530 && CLASS_TYPE_P (TREE_TYPE (t1))
531 && CLASS_TYPE_P (TREE_TYPE (t2))
532 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
535 class1 = TREE_TYPE (t1);
536 class2 = TREE_TYPE (t2);
538 if (DERIVED_FROM_P (class1, class2))
539 t2 = (build_pointer_type
540 (cp_build_qualified_type (class1, TYPE_QUALS (class2))));
541 else if (DERIVED_FROM_P (class2, class1))
542 t1 = (build_pointer_type
543 (cp_build_qualified_type (class2, TYPE_QUALS (class1))));
546 error ("%s between distinct pointer types %qT and %qT "
547 "lacks a cast", location, t1, t2);
548 return error_mark_node;
551 /* [expr.eq] permits the application of a pointer-to-member
552 conversion to change the class type of one of the types. */
553 else if (TYPE_PTR_TO_MEMBER_P (t1)
554 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
555 TYPE_PTRMEM_CLASS_TYPE (t2)))
557 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
558 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
560 if (DERIVED_FROM_P (class1, class2))
561 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
562 else if (DERIVED_FROM_P (class2, class1))
563 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
566 error ("%s between distinct pointer-to-member types %qT and %qT "
567 "lacks a cast", location, t1, t2);
568 return error_mark_node;
572 return composite_pointer_type_r (t1, t2, location);
575 /* Return the merged type of two types.
576 We assume that cp_comptypes has already been done and returned 1;
577 if that isn't so, this may crash.
579 This just combines attributes and default arguments; any other
580 differences would cause the two types to compare unalike. */
583 merge_types (tree t1, tree t2)
585 enum tree_code code1;
586 enum tree_code code2;
589 /* Save time if the two types are the same. */
592 if (original_type (t1) == original_type (t2))
595 /* If one type is nonsense, use the other. */
596 if (t1 == error_mark_node)
598 if (t2 == error_mark_node)
601 /* Merge the attributes. */
602 attributes = (*targetm.merge_type_attributes) (t1, t2);
604 if (TYPE_PTRMEMFUNC_P (t1))
605 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
606 if (TYPE_PTRMEMFUNC_P (t2))
607 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
609 code1 = TREE_CODE (t1);
610 code2 = TREE_CODE (t2);
616 /* For two pointers, do this recursively on the target type. */
618 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
619 int quals = cp_type_quals (t1);
621 if (code1 == POINTER_TYPE)
622 t1 = build_pointer_type (target);
624 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
625 t1 = build_type_attribute_variant (t1, attributes);
626 t1 = cp_build_qualified_type (t1, quals);
628 if (TREE_CODE (target) == METHOD_TYPE)
629 t1 = build_ptrmemfunc_type (t1);
638 quals = cp_type_quals (t1);
639 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
640 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
641 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
643 t1 = cp_build_qualified_type (t1, quals);
649 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
650 /* Save space: see if the result is identical to one of the args. */
651 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
652 return build_type_attribute_variant (t1, attributes);
653 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
654 return build_type_attribute_variant (t2, attributes);
655 /* Merge the element types, and have a size if either arg has one. */
656 t1 = build_cplus_array_type
657 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
662 /* Function types: prefer the one that specified arg types.
663 If both do, merge the arg types. Also merge the return types. */
665 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
666 tree p1 = TYPE_ARG_TYPES (t1);
667 tree p2 = TYPE_ARG_TYPES (t2);
670 /* Save space: see if the result is identical to one of the args. */
671 if (valtype == TREE_TYPE (t1) && ! p2)
672 return cp_build_type_attribute_variant (t1, attributes);
673 if (valtype == TREE_TYPE (t2) && ! p1)
674 return cp_build_type_attribute_variant (t2, attributes);
676 /* Simple way if one arg fails to specify argument types. */
677 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
679 rval = build_function_type (valtype, p2);
680 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
681 rval = build_exception_variant (rval, raises);
682 return cp_build_type_attribute_variant (rval, attributes);
684 raises = TYPE_RAISES_EXCEPTIONS (t1);
685 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
687 rval = build_function_type (valtype, p1);
689 rval = build_exception_variant (rval, raises);
690 return cp_build_type_attribute_variant (rval, attributes);
693 rval = build_function_type (valtype, commonparms (p1, p2));
694 t1 = build_exception_variant (rval, raises);
700 /* Get this value the long way, since TYPE_METHOD_BASETYPE
701 is just the main variant of this. */
702 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
703 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
706 /* If this was a member function type, get back to the
707 original type of type member function (i.e., without
708 the class instance variable up front. */
709 t1 = build_function_type (TREE_TYPE (t1),
710 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
711 t2 = build_function_type (TREE_TYPE (t2),
712 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
713 t3 = merge_types (t1, t2);
714 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
715 TYPE_ARG_TYPES (t3));
716 t1 = build_exception_variant (t3, raises);
721 /* There is no need to merge attributes into a TYPENAME_TYPE.
722 When the type is instantiated it will have whatever
723 attributes result from the instantiation. */
729 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
731 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
734 return cp_build_type_attribute_variant (t1, attributes);
737 /* Return the common type of two types.
738 We assume that cp_comptypes has already been done and returned 1;
739 if that isn't so, this may crash.
741 This is the type for the result of most arithmetic operations
742 if the operands have the given two types. */
745 common_type (tree t1, tree t2)
747 enum tree_code code1;
748 enum tree_code code2;
750 /* If one type is nonsense, bail. */
751 if (t1 == error_mark_node || t2 == error_mark_node)
752 return error_mark_node;
754 code1 = TREE_CODE (t1);
755 code2 = TREE_CODE (t2);
757 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
758 || code1 == VECTOR_TYPE)
759 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
760 || code2 == VECTOR_TYPE))
761 return type_after_usual_arithmetic_conversions (t1, t2);
763 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
764 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
765 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
766 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
772 /* Compare two exception specifier types for exactness or subsetness, if
773 allowed. Returns false for mismatch, true for match (same, or
776 [except.spec] "If a class X ... objects of class X or any class publicly
777 and unambiguously derived from X. Similarly, if a pointer type Y * ...
778 exceptions of type Y * or that are pointers to any type publicly and
779 unambiguously derived from Y. Otherwise a function only allows exceptions
780 that have the same type ..."
781 This does not mention cv qualifiers and is different to what throw
782 [except.throw] and catch [except.catch] will do. They will ignore the
783 top level cv qualifiers, and allow qualifiers in the pointer to class
786 We implement the letter of the standard. */
789 comp_except_types (tree a, tree b, bool exact)
791 if (same_type_p (a, b))
795 if (cp_type_quals (a) || cp_type_quals (b))
798 if (TREE_CODE (a) == POINTER_TYPE
799 && TREE_CODE (b) == POINTER_TYPE)
803 if (cp_type_quals (a) || cp_type_quals (b))
807 if (TREE_CODE (a) != RECORD_TYPE
808 || TREE_CODE (b) != RECORD_TYPE)
811 if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
817 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
818 If EXACT is false, T2 can be stricter than T1 (according to 15.4/7),
819 otherwise it must be exact. Exception lists are unordered, but
820 we've already filtered out duplicates. Most lists will be in order,
821 we should try to make use of that. */
824 comp_except_specs (const_tree t1, const_tree t2, bool exact)
833 if (t1 == NULL_TREE) /* T1 is ... */
834 return t2 == NULL_TREE || !exact;
835 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
836 return t2 != NULL_TREE && !TREE_VALUE (t2);
837 if (t2 == NULL_TREE) /* T2 is ... */
839 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
842 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
843 Count how many we find, to determine exactness. For exact matching and
844 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
846 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
848 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
850 tree a = TREE_VALUE (probe);
851 tree b = TREE_VALUE (t2);
853 if (comp_except_types (a, b, exact))
855 if (probe == base && exact)
856 base = TREE_CHAIN (probe);
861 if (probe == NULL_TREE)
864 return !exact || base == NULL_TREE || length == list_length (t1);
867 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
868 [] can match [size]. */
871 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
880 /* The type of the array elements must be the same. */
881 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
884 d1 = TYPE_DOMAIN (t1);
885 d2 = TYPE_DOMAIN (t2);
890 /* If one of the arrays is dimensionless, and the other has a
891 dimension, they are of different types. However, it is valid to
899 declarations for an array object can specify
900 array types that differ by the presence or absence of a major
901 array bound (_dcl.array_). */
903 return allow_redeclaration;
905 /* Check that the dimensions are the same. */
907 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
909 max1 = TYPE_MAX_VALUE (d1);
910 max2 = TYPE_MAX_VALUE (d2);
911 if (processing_template_decl && !abi_version_at_least (2)
912 && !value_dependent_expression_p (max1)
913 && !value_dependent_expression_p (max2))
915 /* With abi-1 we do not fold non-dependent array bounds, (and
916 consequently mangle them incorrectly). We must therefore
917 fold them here, to verify the domains have the same
923 if (!cp_tree_equal (max1, max2))
929 /* Subroutine in cp_comptypes. */
932 structural_comptypes (tree t1, tree t2, int strict)
937 /* Suppress errors caused by previously reported errors. */
938 if (t1 == error_mark_node || t2 == error_mark_node)
941 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
943 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
944 current instantiation. */
945 if (TREE_CODE (t1) == TYPENAME_TYPE)
946 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
948 if (TREE_CODE (t2) == TYPENAME_TYPE)
949 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
951 if (TYPE_PTRMEMFUNC_P (t1))
952 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
953 if (TYPE_PTRMEMFUNC_P (t2))
954 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
956 /* Different classes of types can't be compatible. */
957 if (TREE_CODE (t1) != TREE_CODE (t2))
960 /* Qualifiers must match. For array types, we will check when we
961 recur on the array element types. */
962 if (TREE_CODE (t1) != ARRAY_TYPE
963 && TYPE_QUALS (t1) != TYPE_QUALS (t2))
966 /* Allow for two different type nodes which have essentially the same
967 definition. Note that we already checked for equality of the type
968 qualifiers (just above). */
970 if (TREE_CODE (t1) != ARRAY_TYPE
971 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
974 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
977 /* Compare the types. Break out if they could be the same. */
978 switch (TREE_CODE (t1))
982 /* All void and bool types are the same. */
986 case FIXED_POINT_TYPE:
988 /* With these nodes, we can't determine type equivalence by
989 looking at what is stored in the nodes themselves, because
990 two nodes might have different TYPE_MAIN_VARIANTs but still
991 represent the same type. For example, wchar_t and int could
992 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
993 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
994 and are distinct types. On the other hand, int and the
997 typedef int INT __attribute((may_alias));
999 have identical properties, different TYPE_MAIN_VARIANTs, but
1000 represent the same type. The canonical type system keeps
1001 track of equivalence in this case, so we fall back on it. */
1002 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1004 case TEMPLATE_TEMPLATE_PARM:
1005 case BOUND_TEMPLATE_TEMPLATE_PARM:
1006 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1007 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2)
1008 || (TEMPLATE_TYPE_PARAMETER_PACK (t1)
1009 != TEMPLATE_TYPE_PARAMETER_PACK (t2)))
1011 if (!comp_template_parms
1012 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1013 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1015 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1017 /* Don't check inheritance. */
1018 strict = COMPARE_STRICT;
1023 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1024 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1025 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1026 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1029 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1031 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1037 if (!cp_comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1038 strict & ~COMPARE_REDECLARATION))
1040 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1044 case REFERENCE_TYPE:
1045 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1047 /* fall through to checks for pointer types */
1050 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1051 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1052 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1058 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1060 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1065 /* Target types must match incl. qualifiers. */
1066 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1070 case TEMPLATE_TYPE_PARM:
1071 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1072 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2)
1073 || (TEMPLATE_TYPE_PARAMETER_PACK (t1)
1074 != TEMPLATE_TYPE_PARAMETER_PACK (t2)))
1079 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1080 TYPENAME_TYPE_FULLNAME (t2)))
1082 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1086 case UNBOUND_CLASS_TEMPLATE:
1087 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1089 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1094 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1099 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1100 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1104 case TYPE_PACK_EXPANSION:
1105 return same_type_p (PACK_EXPANSION_PATTERN (t1),
1106 PACK_EXPANSION_PATTERN (t2));
1109 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1110 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1111 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1112 DECLTYPE_TYPE_EXPR (t2)))
1120 /* If we get here, we know that from a target independent POV the
1121 types are the same. Make sure the target attributes are also
1123 return targetm.comp_type_attributes (t1, t2);
1126 extern int comptypes (tree, tree);
1128 /* Type comparison function that matches the signature of comptypes
1129 from c-tree.h, which is used by the C front end and some of the
1130 C/C++ common bits. */
1132 comptypes (tree t1, tree t2)
1134 return cp_comptypes (t1, t2, COMPARE_STRICT);
1137 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1138 is a bitwise-or of the COMPARE_* flags. */
1141 cp_comptypes (tree t1, tree t2, int strict)
1143 if (strict == COMPARE_STRICT)
1148 if (t1 == error_mark_node || t2 == error_mark_node)
1151 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1152 /* At least one of the types requires structural equality, so
1153 perform a deep check. */
1154 return structural_comptypes (t1, t2, strict);
1156 #ifdef ENABLE_CHECKING
1157 if (USE_CANONICAL_TYPES)
1159 bool result = structural_comptypes (t1, t2, strict);
1161 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1162 /* The two types are structurally equivalent, but their
1163 canonical types were different. This is a failure of the
1164 canonical type propagation code.*/
1166 ("canonical types differ for identical types %T and %T",
1168 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1169 /* Two types are structurally different, but the canonical
1170 types are the same. This means we were over-eager in
1171 assigning canonical types. */
1173 ("same canonical type node for different types %T and %T",
1179 if (USE_CANONICAL_TYPES)
1180 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1183 return structural_comptypes (t1, t2, strict);
1185 else if (strict == COMPARE_STRUCTURAL)
1186 return structural_comptypes (t1, t2, COMPARE_STRICT);
1188 return structural_comptypes (t1, t2, strict);
1191 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1194 at_least_as_qualified_p (const_tree type1, const_tree type2)
1196 int q1 = cp_type_quals (type1);
1197 int q2 = cp_type_quals (type2);
1199 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1200 return (q1 & q2) == q2;
1203 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1204 more cv-qualified that TYPE1, and 0 otherwise. */
1207 comp_cv_qualification (const_tree type1, const_tree type2)
1209 int q1 = cp_type_quals (type1);
1210 int q2 = cp_type_quals (type2);
1215 if ((q1 & q2) == q2)
1217 else if ((q1 & q2) == q1)
1223 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1224 subset of the cv-qualification signature of TYPE2, and the types
1225 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1228 comp_cv_qual_signature (tree type1, tree type2)
1230 if (comp_ptr_ttypes_real (type2, type1, -1))
1232 else if (comp_ptr_ttypes_real (type1, type2, -1))
1238 /* Subroutines of `cp_comptypes'. */
1240 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1241 equivalent in the sense that functions with those parameter types
1242 can have equivalent types. The two lists must be equivalent,
1243 element by element. */
1246 compparms (const_tree parms1, const_tree parms2)
1250 /* An unspecified parmlist matches any specified parmlist
1251 whose argument types don't need default promotions. */
1253 for (t1 = parms1, t2 = parms2;
1255 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1257 /* If one parmlist is shorter than the other,
1258 they fail to match. */
1261 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1268 /* Process a sizeof or alignof expression where the operand is a
1272 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1277 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1278 if (type == error_mark_node)
1279 return error_mark_node;
1281 type = non_reference (type);
1282 if (TREE_CODE (type) == METHOD_TYPE)
1284 if (complain && (pedantic || warn_pointer_arith))
1285 pedwarn ("invalid application of %qs to a member function",
1286 operator_name_info[(int) op].name);
1287 value = size_one_node;
1290 dependent_p = dependent_type_p (type);
1292 complete_type (type);
1294 /* VLA types will have a non-constant size. In the body of an
1295 uninstantiated template, we don't need to try to compute the
1296 value, because the sizeof expression is not an integral
1297 constant expression in that case. And, if we do try to
1298 compute the value, we'll likely end up with SAVE_EXPRs, which
1299 the template substitution machinery does not expect to see. */
1300 || (processing_template_decl
1301 && COMPLETE_TYPE_P (type)
1302 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1304 value = build_min (op, size_type_node, type);
1305 TREE_READONLY (value) = 1;
1309 return c_sizeof_or_alignof_type (complete_type (type),
1314 /* Process a sizeof expression where the operand is an expression. */
1317 cxx_sizeof_expr (tree e)
1319 if (e == error_mark_node)
1320 return error_mark_node;
1322 if (processing_template_decl)
1324 e = build_min (SIZEOF_EXPR, size_type_node, e);
1325 TREE_SIDE_EFFECTS (e) = 0;
1326 TREE_READONLY (e) = 1;
1331 if (TREE_CODE (e) == COMPONENT_REF
1332 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1333 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1335 error ("invalid application of %<sizeof%> to a bit-field");
1338 else if (is_overloaded_fn (e))
1340 pedwarn ("ISO C++ forbids applying %<sizeof%> to an expression of "
1344 else if (type_unknown_p (e))
1346 cxx_incomplete_type_error (e, TREE_TYPE (e));
1352 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, true);
1355 /* Implement the __alignof keyword: Return the minimum required
1356 alignment of E, measured in bytes. For VAR_DECL's and
1357 FIELD_DECL's return DECL_ALIGN (which can be set from an
1358 "aligned" __attribute__ specification). */
1361 cxx_alignof_expr (tree e)
1365 if (e == error_mark_node)
1366 return error_mark_node;
1368 if (processing_template_decl)
1370 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1371 TREE_SIDE_EFFECTS (e) = 0;
1372 TREE_READONLY (e) = 1;
1377 if (TREE_CODE (e) == VAR_DECL)
1378 t = size_int (DECL_ALIGN_UNIT (e));
1379 else if (TREE_CODE (e) == COMPONENT_REF
1380 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1381 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1383 error ("invalid application of %<__alignof%> to a bit-field");
1386 else if (TREE_CODE (e) == COMPONENT_REF
1387 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1388 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1389 else if (is_overloaded_fn (e))
1391 pedwarn ("ISO C++ forbids applying %<__alignof%> to an expression of "
1393 if (TREE_CODE (e) == FUNCTION_DECL)
1394 t = size_int (DECL_ALIGN_UNIT (e));
1398 else if (type_unknown_p (e))
1400 cxx_incomplete_type_error (e, TREE_TYPE (e));
1404 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, true);
1406 return fold_convert (size_type_node, t);
1409 /* Process a sizeof or alignof expression E with code OP where the operand
1410 is an expression. */
1413 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op)
1415 if (op == SIZEOF_EXPR)
1416 return cxx_sizeof_expr (e);
1418 return cxx_alignof_expr (e);
1421 /* EXPR is being used in a context that is not a function call.
1426 The expression can be used only as the left-hand operand of a
1427 member function call.
1429 [expr.mptr.operator]
1431 If the result of .* or ->* is a function, then that result can be
1432 used only as the operand for the function call operator ().
1434 by issuing an error message if appropriate. Returns true iff EXPR
1435 violates these rules. */
1438 invalid_nonstatic_memfn_p (const_tree expr)
1440 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
1442 error ("invalid use of non-static member function");
1448 /* If EXP is a reference to a bitfield, and the type of EXP does not
1449 match the declared type of the bitfield, return the declared type
1450 of the bitfield. Otherwise, return NULL_TREE. */
1453 is_bitfield_expr_with_lowered_type (const_tree exp)
1455 switch (TREE_CODE (exp))
1458 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1459 ? TREE_OPERAND (exp, 1)
1460 : TREE_OPERAND (exp, 0)))
1462 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1465 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1469 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1475 field = TREE_OPERAND (exp, 1);
1476 if (TREE_CODE (field) != FIELD_DECL || !DECL_C_BIT_FIELD (field))
1478 if (same_type_ignoring_top_level_qualifiers_p
1479 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1481 return DECL_BIT_FIELD_TYPE (field);
1486 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1487 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1488 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1496 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1497 bitfield with a lowered type, the type of EXP is returned, rather
1501 unlowered_expr_type (const_tree exp)
1505 type = is_bitfield_expr_with_lowered_type (exp);
1507 type = TREE_TYPE (exp);
1512 /* Perform the conversions in [expr] that apply when an lvalue appears
1513 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1514 function-to-pointer conversions. In addition, manifest constants
1515 are replaced by their values, and bitfield references are converted
1516 to their declared types.
1518 Although the returned value is being used as an rvalue, this
1519 function does not wrap the returned expression in a
1520 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1521 that the return value is no longer an lvalue. */
1524 decay_conversion (tree exp)
1527 enum tree_code code;
1529 type = TREE_TYPE (exp);
1530 if (type == error_mark_node)
1531 return error_mark_node;
1533 if (type_unknown_p (exp))
1535 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1536 return error_mark_node;
1539 exp = decl_constant_value (exp);
1540 if (error_operand_p (exp))
1541 return error_mark_node;
1543 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1544 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1545 code = TREE_CODE (type);
1546 if (code == VOID_TYPE)
1548 error ("void value not ignored as it ought to be");
1549 return error_mark_node;
1551 if (invalid_nonstatic_memfn_p (exp))
1552 return error_mark_node;
1553 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1554 return build_unary_op (ADDR_EXPR, exp, 0);
1555 if (code == ARRAY_TYPE)
1560 if (TREE_CODE (exp) == INDIRECT_REF)
1561 return build_nop (build_pointer_type (TREE_TYPE (type)),
1562 TREE_OPERAND (exp, 0));
1564 if (TREE_CODE (exp) == COMPOUND_EXPR)
1566 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1567 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1568 TREE_OPERAND (exp, 0), op1);
1572 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1574 error ("invalid use of non-lvalue array");
1575 return error_mark_node;
1578 ptrtype = build_pointer_type (TREE_TYPE (type));
1580 if (TREE_CODE (exp) == VAR_DECL)
1582 if (!cxx_mark_addressable (exp))
1583 return error_mark_node;
1584 adr = build_nop (ptrtype, build_address (exp));
1587 /* This way is better for a COMPONENT_REF since it can
1588 simplify the offset for a component. */
1589 adr = build_unary_op (ADDR_EXPR, exp, 1);
1590 return cp_convert (ptrtype, adr);
1593 /* If a bitfield is used in a context where integral promotion
1594 applies, then the caller is expected to have used
1595 default_conversion. That function promotes bitfields correctly
1596 before calling this function. At this point, if we have a
1597 bitfield referenced, we may assume that is not subject to
1598 promotion, and that, therefore, the type of the resulting rvalue
1599 is the declared type of the bitfield. */
1600 exp = convert_bitfield_to_declared_type (exp);
1602 /* We do not call rvalue() here because we do not want to wrap EXP
1603 in a NON_LVALUE_EXPR. */
1607 Non-class rvalues always have cv-unqualified types. */
1608 type = TREE_TYPE (exp);
1609 if (!CLASS_TYPE_P (type) && cp_type_quals (type))
1610 exp = build_nop (TYPE_MAIN_VARIANT (type), exp);
1615 /* Perform prepatory conversions, as part of the "usual arithmetic
1616 conversions". In particular, as per [expr]:
1618 Whenever an lvalue expression appears as an operand of an
1619 operator that expects the rvalue for that operand, the
1620 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1621 standard conversions are applied to convert the expression to an
1624 In addition, we perform integral promotions here, as those are
1625 applied to both operands to a binary operator before determining
1626 what additional conversions should apply. */
1629 default_conversion (tree exp)
1631 /* Perform the integral promotions first so that bitfield
1632 expressions (which may promote to "int", even if the bitfield is
1633 declared "unsigned") are promoted correctly. */
1634 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1635 exp = perform_integral_promotions (exp);
1636 /* Perform the other conversions. */
1637 exp = decay_conversion (exp);
1642 /* EXPR is an expression with an integral or enumeration type.
1643 Perform the integral promotions in [conv.prom], and return the
1647 perform_integral_promotions (tree expr)
1654 If the bitfield has an enumerated type, it is treated as any
1655 other value of that type for promotion purposes. */
1656 type = is_bitfield_expr_with_lowered_type (expr);
1657 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1658 type = TREE_TYPE (expr);
1659 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1660 promoted_type = type_promotes_to (type);
1661 if (type != promoted_type)
1662 expr = cp_convert (promoted_type, expr);
1666 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1670 inline_conversion (tree exp)
1672 if (TREE_CODE (exp) == FUNCTION_DECL)
1673 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1678 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1679 decay_conversion to one. */
1682 string_conv_p (const_tree totype, const_tree exp, int warn)
1686 if (TREE_CODE (totype) != POINTER_TYPE)
1689 t = TREE_TYPE (totype);
1690 if (!same_type_p (t, char_type_node)
1691 && !same_type_p (t, wchar_type_node))
1694 if (TREE_CODE (exp) == STRING_CST)
1696 /* Make sure that we don't try to convert between char and wchar_t. */
1697 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1702 /* Is this a string constant which has decayed to 'const char *'? */
1703 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1704 if (!same_type_p (TREE_TYPE (exp), t))
1707 if (TREE_CODE (exp) != ADDR_EXPR
1708 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1712 /* This warning is not very useful, as it complains about printf. */
1714 warning (OPT_Wwrite_strings,
1715 "deprecated conversion from string constant to %qT",
1721 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1722 can, for example, use as an lvalue. This code used to be in
1723 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1724 expressions, where we're dealing with aggregates. But now it's again only
1725 called from unary_complex_lvalue. The case (in particular) that led to
1726 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1730 rationalize_conditional_expr (enum tree_code code, tree t)
1732 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1733 the first operand is always the one to be used if both operands
1734 are equal, so we know what conditional expression this used to be. */
1735 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1737 tree op0 = TREE_OPERAND (t, 0);
1738 tree op1 = TREE_OPERAND (t, 1);
1740 /* The following code is incorrect if either operand side-effects. */
1741 gcc_assert (!TREE_SIDE_EFFECTS (op0)
1742 && !TREE_SIDE_EFFECTS (op1));
1744 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1745 ? LE_EXPR : GE_EXPR),
1746 op0, TREE_CODE (op0),
1747 op1, TREE_CODE (op1),
1748 /*overloaded_p=*/NULL),
1749 build_unary_op (code, op0, 0),
1750 build_unary_op (code, op1, 0));
1754 build_conditional_expr (TREE_OPERAND (t, 0),
1755 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1756 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1759 /* Given the TYPE of an anonymous union field inside T, return the
1760 FIELD_DECL for the field. If not found return NULL_TREE. Because
1761 anonymous unions can nest, we must also search all anonymous unions
1762 that are directly reachable. */
1765 lookup_anon_field (tree t, tree type)
1769 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1771 if (TREE_STATIC (field))
1773 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1776 /* If we find it directly, return the field. */
1777 if (DECL_NAME (field) == NULL_TREE
1778 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1783 /* Otherwise, it could be nested, search harder. */
1784 if (DECL_NAME (field) == NULL_TREE
1785 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1787 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1795 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1796 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1797 non-NULL, it indicates the path to the base used to name MEMBER.
1798 If PRESERVE_REFERENCE is true, the expression returned will have
1799 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1800 returned will have the type referred to by the reference.
1802 This function does not perform access control; that is either done
1803 earlier by the parser when the name of MEMBER is resolved to MEMBER
1804 itself, or later when overload resolution selects one of the
1805 functions indicated by MEMBER. */
1808 build_class_member_access_expr (tree object, tree member,
1809 tree access_path, bool preserve_reference)
1813 tree result = NULL_TREE;
1815 if (error_operand_p (object) || error_operand_p (member))
1816 return error_mark_node;
1818 gcc_assert (DECL_P (member) || BASELINK_P (member));
1822 The type of the first expression shall be "class object" (of a
1824 object_type = TREE_TYPE (object);
1825 if (!currently_open_class (object_type)
1826 && !complete_type_or_else (object_type, object))
1827 return error_mark_node;
1828 if (!CLASS_TYPE_P (object_type))
1830 error ("request for member %qD in %qE, which is of non-class type %qT",
1831 member, object, object_type);
1832 return error_mark_node;
1835 /* The standard does not seem to actually say that MEMBER must be a
1836 member of OBJECT_TYPE. However, that is clearly what is
1838 if (DECL_P (member))
1840 member_scope = DECL_CLASS_CONTEXT (member);
1842 if (TREE_DEPRECATED (member))
1843 warn_deprecated_use (member);
1846 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
1847 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1848 presently be the anonymous union. Go outwards until we find a
1849 type related to OBJECT_TYPE. */
1850 while (ANON_AGGR_TYPE_P (member_scope)
1851 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1853 member_scope = TYPE_CONTEXT (member_scope);
1854 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1856 if (TREE_CODE (member) == FIELD_DECL)
1857 error ("invalid use of nonstatic data member %qE", member);
1859 error ("%qD is not a member of %qT", member, object_type);
1860 return error_mark_node;
1863 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1864 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
1865 in the front end; only _DECLs and _REFs are lvalues in the back end. */
1867 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1869 object = build_indirect_ref (temp, NULL);
1872 /* In [expr.ref], there is an explicit list of the valid choices for
1873 MEMBER. We check for each of those cases here. */
1874 if (TREE_CODE (member) == VAR_DECL)
1876 /* A static data member. */
1878 /* If OBJECT has side-effects, they are supposed to occur. */
1879 if (TREE_SIDE_EFFECTS (object))
1880 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1882 else if (TREE_CODE (member) == FIELD_DECL)
1884 /* A non-static data member. */
1889 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1890 && integer_zerop (TREE_OPERAND (object, 0)));
1892 /* Convert OBJECT to the type of MEMBER. */
1893 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
1894 TYPE_MAIN_VARIANT (member_scope)))
1899 binfo = lookup_base (access_path ? access_path : object_type,
1900 member_scope, ba_unique, &kind);
1901 if (binfo == error_mark_node)
1902 return error_mark_node;
1904 /* It is invalid to try to get to a virtual base of a
1905 NULL object. The most common cause is invalid use of
1907 if (null_object_p && kind == bk_via_virtual)
1909 error ("invalid access to non-static data member %qD of "
1912 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
1913 return error_mark_node;
1916 /* Convert to the base. */
1917 object = build_base_path (PLUS_EXPR, object, binfo,
1919 /* If we found the base successfully then we should be able
1920 to convert to it successfully. */
1921 gcc_assert (object != error_mark_node);
1924 /* Complain about other invalid uses of offsetof, even though they will
1925 give the right answer. Note that we complain whether or not they
1926 actually used the offsetof macro, since there's no way to know at this
1927 point. So we just give a warning, instead of a pedwarn. */
1928 /* Do not produce this warning for base class field references, because
1929 we know for a fact that didn't come from offsetof. This does occur
1930 in various testsuite cases where a null object is passed where a
1931 vtable access is required. */
1932 if (null_object_p && warn_invalid_offsetof
1933 && CLASSTYPE_NON_POD_P (object_type)
1934 && !DECL_FIELD_IS_BASE (member)
1935 && !skip_evaluation)
1937 warning (OPT_Winvalid_offsetof,
1938 "invalid access to non-static data member %qD "
1939 " of NULL object", member);
1940 warning (OPT_Winvalid_offsetof,
1941 "(perhaps the %<offsetof%> macro was used incorrectly)");
1944 /* If MEMBER is from an anonymous aggregate, we have converted
1945 OBJECT so that it refers to the class containing the
1946 anonymous union. Generate a reference to the anonymous union
1947 itself, and recur to find MEMBER. */
1948 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1949 /* When this code is called from build_field_call, the
1950 object already has the type of the anonymous union.
1951 That is because the COMPONENT_REF was already
1952 constructed, and was then disassembled before calling
1953 build_field_call. After the function-call code is
1954 cleaned up, this waste can be eliminated. */
1955 && (!same_type_ignoring_top_level_qualifiers_p
1956 (TREE_TYPE (object), DECL_CONTEXT (member))))
1958 tree anonymous_union;
1960 anonymous_union = lookup_anon_field (TREE_TYPE (object),
1961 DECL_CONTEXT (member));
1962 object = build_class_member_access_expr (object,
1964 /*access_path=*/NULL_TREE,
1965 preserve_reference);
1968 /* Compute the type of the field, as described in [expr.ref]. */
1969 type_quals = TYPE_UNQUALIFIED;
1970 member_type = TREE_TYPE (member);
1971 if (TREE_CODE (member_type) != REFERENCE_TYPE)
1973 type_quals = (cp_type_quals (member_type)
1974 | cp_type_quals (object_type));
1976 /* A field is const (volatile) if the enclosing object, or the
1977 field itself, is const (volatile). But, a mutable field is
1978 not const, even within a const object. */
1979 if (DECL_MUTABLE_P (member))
1980 type_quals &= ~TYPE_QUAL_CONST;
1981 member_type = cp_build_qualified_type (member_type, type_quals);
1984 result = build3 (COMPONENT_REF, member_type, object, member,
1986 result = fold_if_not_in_template (result);
1988 /* Mark the expression const or volatile, as appropriate. Even
1989 though we've dealt with the type above, we still have to mark the
1990 expression itself. */
1991 if (type_quals & TYPE_QUAL_CONST)
1992 TREE_READONLY (result) = 1;
1993 if (type_quals & TYPE_QUAL_VOLATILE)
1994 TREE_THIS_VOLATILE (result) = 1;
1996 else if (BASELINK_P (member))
1998 /* The member is a (possibly overloaded) member function. */
2002 /* If the MEMBER is exactly one static member function, then we
2003 know the type of the expression. Otherwise, we must wait
2004 until overload resolution has been performed. */
2005 functions = BASELINK_FUNCTIONS (member);
2006 if (TREE_CODE (functions) == FUNCTION_DECL
2007 && DECL_STATIC_FUNCTION_P (functions))
2008 type = TREE_TYPE (functions);
2010 type = unknown_type_node;
2011 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2012 base. That will happen when the function is called. */
2013 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2015 else if (TREE_CODE (member) == CONST_DECL)
2017 /* The member is an enumerator. */
2019 /* If OBJECT has side-effects, they are supposed to occur. */
2020 if (TREE_SIDE_EFFECTS (object))
2021 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2026 error ("invalid use of %qD", member);
2027 return error_mark_node;
2030 if (!preserve_reference)
2033 If E2 is declared to have type "reference to T", then ... the
2034 type of E1.E2 is T. */
2035 result = convert_from_reference (result);
2040 /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
2041 SCOPE is NULL, by OBJECT.~DTOR_NAME. */
2044 lookup_destructor (tree object, tree scope, tree dtor_name)
2046 tree object_type = TREE_TYPE (object);
2047 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2050 if (scope && !check_dtor_name (scope, dtor_type))
2052 error ("qualified type %qT does not match destructor name ~%qT",
2054 return error_mark_node;
2056 if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2058 error ("the type being destroyed is %qT, but the destructor refers to %qT",
2059 TYPE_MAIN_VARIANT (object_type), dtor_type);
2060 return error_mark_node;
2062 expr = lookup_member (dtor_type, complete_dtor_identifier,
2063 /*protect=*/1, /*want_type=*/false);
2064 expr = (adjust_result_of_qualified_name_lookup
2065 (expr, dtor_type, object_type));
2069 /* An expression of the form "A::template B" has been resolved to
2070 DECL. Issue a diagnostic if B is not a template or template
2074 check_template_keyword (tree decl)
2076 /* The standard says:
2080 If a name prefixed by the keyword template is not a member
2081 template, the program is ill-formed.
2083 DR 228 removed the restriction that the template be a member
2086 DR 96, if accepted would add the further restriction that explicit
2087 template arguments must be provided if the template keyword is
2088 used, but, as of 2005-10-16, that DR is still in "drafting". If
2089 this DR is accepted, then the semantic checks here can be
2090 simplified, as the entity named must in fact be a template
2091 specialization, rather than, as at present, a set of overloaded
2092 functions containing at least one template function. */
2093 if (TREE_CODE (decl) != TEMPLATE_DECL
2094 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2096 if (!is_overloaded_fn (decl))
2097 pedwarn ("%qD is not a template", decl);
2102 if (BASELINK_P (fns))
2103 fns = BASELINK_FUNCTIONS (fns);
2106 tree fn = OVL_CURRENT (fns);
2107 if (TREE_CODE (fn) == TEMPLATE_DECL
2108 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2110 if (TREE_CODE (fn) == FUNCTION_DECL
2111 && DECL_USE_TEMPLATE (fn)
2112 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2114 fns = OVL_NEXT (fns);
2117 pedwarn ("%qD is not a template", decl);
2122 /* This function is called by the parser to process a class member
2123 access expression of the form OBJECT.NAME. NAME is a node used by
2124 the parser to represent a name; it is not yet a DECL. It may,
2125 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2126 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2127 there is no reason to do the lookup twice, so the parser keeps the
2128 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2129 be a template via the use of the "A::template B" syntax. */
2132 finish_class_member_access_expr (tree object, tree name, bool template_p)
2137 tree access_path = NULL_TREE;
2138 tree orig_object = object;
2139 tree orig_name = name;
2141 if (object == error_mark_node || name == error_mark_node)
2142 return error_mark_node;
2144 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2145 if (!objc_is_public (object, name))
2146 return error_mark_node;
2148 object_type = TREE_TYPE (object);
2150 if (processing_template_decl)
2152 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2153 dependent_type_p (object_type)
2154 /* If NAME is just an IDENTIFIER_NODE, then the expression
2156 || TREE_CODE (object) == IDENTIFIER_NODE
2157 /* If NAME is "f<args>", where either 'f' or 'args' is
2158 dependent, then the expression is dependent. */
2159 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2160 && dependent_template_id_p (TREE_OPERAND (name, 0),
2161 TREE_OPERAND (name, 1)))
2162 /* If NAME is "T::X" where "T" is dependent, then the
2163 expression is dependent. */
2164 || (TREE_CODE (name) == SCOPE_REF
2165 && TYPE_P (TREE_OPERAND (name, 0))
2166 && dependent_type_p (TREE_OPERAND (name, 0))))
2167 return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2168 object = build_non_dependent_expr (object);
2173 The type of the first expression shall be "class object" (of a
2175 if (!currently_open_class (object_type)
2176 && !complete_type_or_else (object_type, object))
2177 return error_mark_node;
2178 if (!CLASS_TYPE_P (object_type))
2180 error ("request for member %qD in %qE, which is of non-class type %qT",
2181 name, object, object_type);
2182 return error_mark_node;
2185 if (BASELINK_P (name))
2186 /* A member function that has already been looked up. */
2190 bool is_template_id = false;
2191 tree template_args = NULL_TREE;
2194 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2196 is_template_id = true;
2197 template_args = TREE_OPERAND (name, 1);
2198 name = TREE_OPERAND (name, 0);
2200 if (TREE_CODE (name) == OVERLOAD)
2201 name = DECL_NAME (get_first_fn (name));
2202 else if (DECL_P (name))
2203 name = DECL_NAME (name);
2206 if (TREE_CODE (name) == SCOPE_REF)
2208 /* A qualified name. The qualifying class or namespace `S'
2209 has already been looked up; it is either a TYPE or a
2211 scope = TREE_OPERAND (name, 0);
2212 name = TREE_OPERAND (name, 1);
2214 /* If SCOPE is a namespace, then the qualified name does not
2215 name a member of OBJECT_TYPE. */
2216 if (TREE_CODE (scope) == NAMESPACE_DECL)
2218 error ("%<%D::%D%> is not a member of %qT",
2219 scope, name, object_type);
2220 return error_mark_node;
2223 gcc_assert (CLASS_TYPE_P (scope));
2224 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2225 || TREE_CODE (name) == BIT_NOT_EXPR);
2227 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2228 access_path = lookup_base (object_type, scope, ba_check, NULL);
2229 if (access_path == error_mark_node)
2230 return error_mark_node;
2233 error ("%qT is not a base of %qT", scope, object_type);
2234 return error_mark_node;
2240 access_path = object_type;
2243 if (TREE_CODE (name) == BIT_NOT_EXPR)
2244 member = lookup_destructor (object, scope, name);
2247 /* Look up the member. */
2248 member = lookup_member (access_path, name, /*protect=*/1,
2249 /*want_type=*/false);
2250 if (member == NULL_TREE)
2252 error ("%qD has no member named %qE", object_type, name);
2253 return error_mark_node;
2255 if (member == error_mark_node)
2256 return error_mark_node;
2261 tree template = member;
2263 if (BASELINK_P (template))
2264 template = lookup_template_function (template, template_args);
2267 error ("%qD is not a member template function", name);
2268 return error_mark_node;
2273 if (TREE_DEPRECATED (member))
2274 warn_deprecated_use (member);
2277 check_template_keyword (member);
2279 expr = build_class_member_access_expr (object, member, access_path,
2280 /*preserve_reference=*/false);
2281 if (processing_template_decl && expr != error_mark_node)
2283 if (BASELINK_P (member))
2285 if (TREE_CODE (orig_name) == SCOPE_REF)
2286 BASELINK_QUALIFIED_P (member) = 1;
2289 return build_min_non_dep (COMPONENT_REF, expr,
2290 orig_object, orig_name,
2297 /* Return an expression for the MEMBER_NAME field in the internal
2298 representation of PTRMEM, a pointer-to-member function. (Each
2299 pointer-to-member function type gets its own RECORD_TYPE so it is
2300 more convenient to access the fields by name than by FIELD_DECL.)
2301 This routine converts the NAME to a FIELD_DECL and then creates the
2302 node for the complete expression. */
2305 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2311 /* This code is a stripped down version of
2312 build_class_member_access_expr. It does not work to use that
2313 routine directly because it expects the object to be of class
2315 ptrmem_type = TREE_TYPE (ptrmem);
2316 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2317 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2318 /*want_type=*/false);
2319 member_type = cp_build_qualified_type (TREE_TYPE (member),
2320 cp_type_quals (ptrmem_type));
2321 return fold_build3 (COMPONENT_REF, member_type,
2322 ptrmem, member, NULL_TREE);
2325 /* Given an expression PTR for a pointer, return an expression
2326 for the value pointed to.
2327 ERRORSTRING is the name of the operator to appear in error messages.
2329 This function may need to overload OPERATOR_FNNAME.
2330 Must also handle REFERENCE_TYPEs for C++. */
2333 build_x_indirect_ref (tree expr, const char *errorstring)
2335 tree orig_expr = expr;
2338 if (processing_template_decl)
2340 if (type_dependent_expression_p (expr))
2341 return build_min_nt (INDIRECT_REF, expr);
2342 expr = build_non_dependent_expr (expr);
2345 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2346 NULL_TREE, /*overloaded_p=*/NULL);
2348 rval = build_indirect_ref (expr, errorstring);
2350 if (processing_template_decl && rval != error_mark_node)
2351 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2357 build_indirect_ref (tree ptr, const char *errorstring)
2361 if (ptr == error_mark_node)
2362 return error_mark_node;
2364 if (ptr == current_class_ptr)
2365 return current_class_ref;
2367 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2368 ? ptr : decay_conversion (ptr));
2369 type = TREE_TYPE (pointer);
2371 if (POINTER_TYPE_P (type))
2375 If the type of the expression is "pointer to T," the type
2376 of the result is "T."
2378 We must use the canonical variant because certain parts of
2379 the back end, like fold, do pointer comparisons between
2381 tree t = canonical_type_variant (TREE_TYPE (type));
2383 if (TREE_CODE (ptr) == CONVERT_EXPR
2384 || TREE_CODE (ptr) == NOP_EXPR
2385 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2387 /* If a warning is issued, mark it to avoid duplicates from
2388 the backend. This only needs to be done at
2389 warn_strict_aliasing > 2. */
2390 if (warn_strict_aliasing > 2)
2391 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2392 type, TREE_OPERAND (ptr, 0)))
2393 TREE_NO_WARNING (ptr) = 1;
2396 if (VOID_TYPE_P (t))
2398 /* A pointer to incomplete type (other than cv void) can be
2399 dereferenced [expr.unary.op]/1 */
2400 error ("%qT is not a pointer-to-object type", type);
2401 return error_mark_node;
2403 else if (TREE_CODE (pointer) == ADDR_EXPR
2404 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2405 /* The POINTER was something like `&x'. We simplify `*&x' to
2407 return TREE_OPERAND (pointer, 0);
2410 tree ref = build1 (INDIRECT_REF, t, pointer);
2412 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2413 so that we get the proper error message if the result is used
2414 to assign to. Also, &* is supposed to be a no-op. */
2415 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2416 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2417 TREE_SIDE_EFFECTS (ref)
2418 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2422 /* `pointer' won't be an error_mark_node if we were given a
2423 pointer to member, so it's cool to check for this here. */
2424 else if (TYPE_PTR_TO_MEMBER_P (type))
2425 error ("invalid use of %qs on pointer to member", errorstring);
2426 else if (pointer != error_mark_node)
2429 error ("invalid type argument of %qs", errorstring);
2431 error ("invalid type argument");
2433 return error_mark_node;
2436 /* This handles expressions of the form "a[i]", which denotes
2439 This is logically equivalent in C to *(a+i), but we may do it differently.
2440 If A is a variable or a member, we generate a primitive ARRAY_REF.
2441 This avoids forcing the array out of registers, and can work on
2442 arrays that are not lvalues (for example, members of structures returned
2445 If INDEX is of some user-defined type, it must be converted to
2446 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2447 will inherit the type of the array, which will be some pointer type. */
2450 build_array_ref (tree array, tree idx)
2454 error ("subscript missing in array reference");
2455 return error_mark_node;
2458 if (TREE_TYPE (array) == error_mark_node
2459 || TREE_TYPE (idx) == error_mark_node)
2460 return error_mark_node;
2462 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2464 switch (TREE_CODE (array))
2468 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2469 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
2470 TREE_OPERAND (array, 0), value);
2474 return build_conditional_expr
2475 (TREE_OPERAND (array, 0),
2476 build_array_ref (TREE_OPERAND (array, 1), idx),
2477 build_array_ref (TREE_OPERAND (array, 2), idx));
2483 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2487 warn_array_subscript_with_type_char (idx);
2489 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2491 error ("array subscript is not an integer");
2492 return error_mark_node;
2495 /* Apply integral promotions *after* noticing character types.
2496 (It is unclear why we do these promotions -- the standard
2497 does not say that we should. In fact, the natural thing would
2498 seem to be to convert IDX to ptrdiff_t; we're performing
2499 pointer arithmetic.) */
2500 idx = perform_integral_promotions (idx);
2502 /* An array that is indexed by a non-constant
2503 cannot be stored in a register; we must be able to do
2504 address arithmetic on its address.
2505 Likewise an array of elements of variable size. */
2506 if (TREE_CODE (idx) != INTEGER_CST
2507 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2508 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2511 if (!cxx_mark_addressable (array))
2512 return error_mark_node;
2515 /* An array that is indexed by a constant value which is not within
2516 the array bounds cannot be stored in a register either; because we
2517 would get a crash in store_bit_field/extract_bit_field when trying
2518 to access a non-existent part of the register. */
2519 if (TREE_CODE (idx) == INTEGER_CST
2520 && TYPE_DOMAIN (TREE_TYPE (array))
2521 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2523 if (!cxx_mark_addressable (array))
2524 return error_mark_node;
2527 if (pedantic && !lvalue_p (array))
2528 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2530 /* Note in C++ it is valid to subscript a `register' array, since
2531 it is valid to take the address of something with that
2532 storage specification. */
2536 while (TREE_CODE (foo) == COMPONENT_REF)
2537 foo = TREE_OPERAND (foo, 0);
2538 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2539 warning (OPT_Wextra, "subscripting array declared %<register%>");
2542 type = TREE_TYPE (TREE_TYPE (array));
2543 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2544 /* Array ref is const/volatile if the array elements are
2545 or if the array is.. */
2546 TREE_READONLY (rval)
2547 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2548 TREE_SIDE_EFFECTS (rval)
2549 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2550 TREE_THIS_VOLATILE (rval)
2551 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2552 return require_complete_type (fold_if_not_in_template (rval));
2556 tree ar = default_conversion (array);
2557 tree ind = default_conversion (idx);
2559 /* Put the integer in IND to simplify error checking. */
2560 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2567 if (ar == error_mark_node)
2570 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2572 error ("subscripted value is neither array nor pointer");
2573 return error_mark_node;
2575 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2577 error ("array subscript is not an integer");
2578 return error_mark_node;
2581 warn_array_subscript_with_type_char (idx);
2583 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2588 /* Resolve a pointer to member function. INSTANCE is the object
2589 instance to use, if the member points to a virtual member.
2591 This used to avoid checking for virtual functions if basetype
2592 has no virtual functions, according to an earlier ANSI draft.
2593 With the final ISO C++ rules, such an optimization is
2594 incorrect: A pointer to a derived member can be static_cast
2595 to pointer-to-base-member, as long as the dynamic object
2596 later has the right member. */
2599 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
2601 if (TREE_CODE (function) == OFFSET_REF)
2602 function = TREE_OPERAND (function, 1);
2604 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2606 tree idx, delta, e1, e2, e3, vtbl, basetype;
2607 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2609 tree instance_ptr = *instance_ptrptr;
2610 tree instance_save_expr = 0;
2611 if (instance_ptr == error_mark_node)
2613 if (TREE_CODE (function) == PTRMEM_CST)
2615 /* Extracting the function address from a pmf is only
2616 allowed with -Wno-pmf-conversions. It only works for
2618 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2619 e1 = convert (fntype, e1);
2624 error ("object missing in use of %qE", function);
2625 return error_mark_node;
2629 if (TREE_SIDE_EFFECTS (instance_ptr))
2630 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2632 if (TREE_SIDE_EFFECTS (function))
2633 function = save_expr (function);
2635 /* Start by extracting all the information from the PMF itself. */
2636 e3 = pfn_from_ptrmemfunc (function);
2637 delta = delta_from_ptrmemfunc (function);
2638 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2639 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2641 case ptrmemfunc_vbit_in_pfn:
2642 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2643 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2646 case ptrmemfunc_vbit_in_delta:
2647 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2648 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2655 /* Convert down to the right base before using the instance. A
2656 special case is that in a pointer to member of class C, C may
2657 be incomplete. In that case, the function will of course be
2658 a member of C, and no conversion is required. In fact,
2659 lookup_base will fail in that case, because incomplete
2660 classes do not have BINFOs. */
2661 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2662 if (!same_type_ignoring_top_level_qualifiers_p
2663 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
2665 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2666 basetype, ba_check, NULL);
2667 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
2669 if (instance_ptr == error_mark_node)
2670 return error_mark_node;
2672 /* ...and then the delta in the PMF. */
2673 instance_ptr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (instance_ptr),
2674 instance_ptr, fold_convert (sizetype, delta));
2676 /* Hand back the adjusted 'this' argument to our caller. */
2677 *instance_ptrptr = instance_ptr;
2679 /* Next extract the vtable pointer from the object. */
2680 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2682 vtbl = build_indirect_ref (vtbl, NULL);
2684 /* Finally, extract the function pointer from the vtable. */
2685 e2 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
2686 fold_convert (sizetype, idx));
2687 e2 = build_indirect_ref (e2, NULL);
2688 TREE_CONSTANT (e2) = 1;
2689 TREE_INVARIANT (e2) = 1;
2691 /* When using function descriptors, the address of the
2692 vtable entry is treated as a function pointer. */
2693 if (TARGET_VTABLE_USES_DESCRIPTORS)
2694 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2695 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2697 e2 = fold_convert (TREE_TYPE (e3), e2);
2698 e1 = build_conditional_expr (e1, e2, e3);
2700 /* Make sure this doesn't get evaluated first inside one of the
2701 branches of the COND_EXPR. */
2702 if (instance_save_expr)
2703 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
2704 instance_save_expr, e1);
2712 build_function_call (tree function, tree params)
2714 tree fntype, fndecl;
2715 tree name = NULL_TREE;
2717 tree original = function;
2718 int nargs, parm_types_len;
2722 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2723 expressions, like those used for ObjC messenger dispatches. */
2724 function = objc_rewrite_function_call (function, params);
2726 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2727 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2728 if (TREE_CODE (function) == NOP_EXPR
2729 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2730 function = TREE_OPERAND (function, 0);
2732 if (TREE_CODE (function) == FUNCTION_DECL)
2734 name = DECL_NAME (function);
2736 mark_used (function);
2739 /* Convert anything with function type to a pointer-to-function. */
2740 if (pedantic && DECL_MAIN_P (function))
2741 pedwarn ("ISO C++ forbids calling %<::main%> from within program");
2743 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2744 (because calling an inline function does not mean the function
2745 needs to be separately compiled). */
2747 if (DECL_INLINE (function))
2748 function = inline_conversion (function);
2750 function = build_addr_func (function);
2756 function = build_addr_func (function);
2759 if (function == error_mark_node)
2760 return error_mark_node;
2762 fntype = TREE_TYPE (function);
2764 if (TYPE_PTRMEMFUNC_P (fntype))
2766 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
2767 "function in %<%E (...)%>",
2769 return error_mark_node;
2772 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2773 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2775 if (!((TREE_CODE (fntype) == POINTER_TYPE
2776 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2778 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2780 error ("%qE cannot be used as a function", original);
2781 return error_mark_node;
2784 /* fntype now gets the type of function pointed to. */
2785 fntype = TREE_TYPE (fntype);
2786 parm_types = TYPE_ARG_TYPES (fntype);
2788 /* Allocate storage for converted arguments. */
2789 parm_types_len = list_length (parm_types);
2790 nargs = list_length (params);
2791 if (parm_types_len > nargs)
2792 nargs = parm_types_len;
2793 argarray = (tree *) alloca (nargs * sizeof (tree));
2795 /* Convert the parameters to the types declared in the
2796 function prototype, or apply default promotions. */
2797 nargs = convert_arguments (nargs, argarray, parm_types,
2798 params, fndecl, LOOKUP_NORMAL);
2800 return error_mark_node;
2802 /* Check for errors in format strings and inappropriately
2805 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2808 return build_cxx_call (function, nargs, argarray);
2811 /* Convert the actual parameter expressions in the list VALUES
2812 to the types in the list TYPELIST.
2813 If parmdecls is exhausted, or when an element has NULL as its type,
2814 perform the default conversions.
2816 Store the converted arguments in ARGARRAY. NARGS is the size of this array.
2818 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2820 This is also where warnings about wrong number of args are generated.
2822 Returns the actual number of arguments processed (which might be less
2823 than NARGS), or -1 on error.
2825 VALUES is a chain of TREE_LIST nodes with the elements of the list
2826 in the TREE_VALUE slots of those nodes.
2828 In C++, unspecified trailing parameters can be filled in with their
2829 default arguments, if such were specified. Do so here. */
2832 convert_arguments (int nargs, tree *argarray,
2833 tree typelist, tree values, tree fndecl, int flags)
2835 tree typetail, valtail;
2836 const char *called_thing = 0;
2839 /* Argument passing is always copy-initialization. */
2840 flags |= LOOKUP_ONLYCONVERTING;
2844 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2846 if (DECL_NAME (fndecl) == NULL_TREE
2847 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2848 called_thing = "constructor";
2850 called_thing = "member function";
2853 called_thing = "function";
2856 for (valtail = values, typetail = typelist;
2858 valtail = TREE_CHAIN (valtail), i++)
2860 tree type = typetail ? TREE_VALUE (typetail) : 0;
2861 tree val = TREE_VALUE (valtail);
2863 if (val == error_mark_node || type == error_mark_node)
2866 if (type == void_type_node)
2870 error ("too many arguments to %s %q+#D", called_thing, fndecl);
2871 error ("at this point in file");
2874 error ("too many arguments to function");
2878 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2879 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2880 if (TREE_CODE (val) == NOP_EXPR
2881 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2882 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2883 val = TREE_OPERAND (val, 0);
2885 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2887 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2888 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2889 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2890 val = decay_conversion (val);
2893 if (val == error_mark_node)
2898 /* Formal parm type is specified by a function prototype. */
2901 if (!COMPLETE_TYPE_P (complete_type (type)))
2904 error ("parameter %P of %qD has incomplete type %qT",
2907 error ("parameter %P has incomplete type %qT", i, type);
2908 parmval = error_mark_node;
2912 parmval = convert_for_initialization
2913 (NULL_TREE, type, val, flags,
2914 "argument passing", fndecl, i);
2915 parmval = convert_for_arg_passing (type, parmval);
2918 if (parmval == error_mark_node)
2921 argarray[i] = parmval;
2925 if (fndecl && DECL_BUILT_IN (fndecl)
2926 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2927 /* Don't do ellipsis conversion for __built_in_constant_p
2928 as this will result in spurious warnings for non-POD
2930 val = require_complete_type (val);
2932 val = convert_arg_to_ellipsis (val);
2938 typetail = TREE_CHAIN (typetail);
2941 if (typetail != 0 && typetail != void_list_node)
2943 /* See if there are default arguments that can be used. Because
2944 we hold default arguments in the FUNCTION_TYPE (which is so
2945 wrong), we can see default parameters here from deduced
2946 contexts (and via typeof) for indirect function calls.
2947 Fortunately we know whether we have a function decl to
2948 provide default arguments in a language conformant
2950 if (fndecl && TREE_PURPOSE (typetail)
2951 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
2953 for (; typetail != void_list_node; ++i)
2956 = convert_default_arg (TREE_VALUE (typetail),
2957 TREE_PURPOSE (typetail),
2960 if (parmval == error_mark_node)
2963 argarray[i] = parmval;
2964 typetail = TREE_CHAIN (typetail);
2965 /* ends with `...'. */
2966 if (typetail == NULL_TREE)
2974 error ("too few arguments to %s %q+#D", called_thing, fndecl);
2975 error ("at this point in file");
2978 error ("too few arguments to function");
2983 gcc_assert (i <= nargs);
2987 /* Build a binary-operation expression, after performing default
2988 conversions on the operands. CODE is the kind of expression to
2989 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
2990 are the tree codes which correspond to ARG1 and ARG2 when issuing
2991 warnings about possibly misplaced parentheses. They may differ
2992 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
2993 folding (e.g., if the parser sees "a | 1 + 1", it may call this
2994 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
2995 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
2996 ARG2_CODE as ERROR_MARK. */
2999 build_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code,
3000 tree arg2, enum tree_code arg2_code, bool *overloaded_p)
3009 if (processing_template_decl)
3011 if (type_dependent_expression_p (arg1)
3012 || type_dependent_expression_p (arg2))
3013 return build_min_nt (code, arg1, arg2);
3014 arg1 = build_non_dependent_expr (arg1);
3015 arg2 = build_non_dependent_expr (arg2);
3018 if (code == DOTSTAR_EXPR)
3019 expr = build_m_component_ref (arg1, arg2);
3021 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3024 /* Check for cases such as x+y<<z which users are likely to
3025 misinterpret. But don't warn about obj << x + y, since that is a
3026 common idiom for I/O. */
3027 if (warn_parentheses
3028 && !processing_template_decl
3029 && !error_operand_p (arg1)
3030 && !error_operand_p (arg2)
3031 && (code != LSHIFT_EXPR
3032 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3033 warn_about_parentheses (code, arg1_code, arg2_code);
3035 if (processing_template_decl && expr != error_mark_node)
3036 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3041 /* Build a binary-operation expression without default conversions.
3042 CODE is the kind of expression to build.
3043 This function differs from `build' in several ways:
3044 the data type of the result is computed and recorded in it,
3045 warnings are generated if arg data types are invalid,
3046 special handling for addition and subtraction of pointers is known,
3047 and some optimization is done (operations on narrow ints
3048 are done in the narrower type when that gives the same result).
3049 Constant folding is also done before the result is returned.
3051 Note that the operands will never have enumeral types
3052 because either they have just had the default conversions performed
3053 or they have both just been converted to some other type in which
3054 the arithmetic is to be done.
3056 C++: must do special pointer arithmetic when implementing
3057 multiple inheritance, and deal with pointer to member functions. */
3060 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
3061 int convert_p ATTRIBUTE_UNUSED)
3064 enum tree_code code0, code1;
3066 const char *invalid_op_diag;
3068 /* Expression code to give to the expression when it is built.
3069 Normally this is CODE, which is what the caller asked for,
3070 but in some special cases we change it. */
3071 enum tree_code resultcode = code;
3073 /* Data type in which the computation is to be performed.
3074 In the simplest cases this is the common type of the arguments. */
3075 tree result_type = NULL;
3077 /* Nonzero means operands have already been type-converted
3078 in whatever way is necessary.
3079 Zero means they need to be converted to RESULT_TYPE. */
3082 /* Nonzero means create the expression with this type, rather than
3084 tree build_type = 0;
3086 /* Nonzero means after finally constructing the expression
3087 convert it to this type. */
3088 tree final_type = 0;
3092 /* Nonzero if this is an operation like MIN or MAX which can
3093 safely be computed in short if both args are promoted shorts.
3094 Also implies COMMON.
3095 -1 indicates a bitwise operation; this makes a difference
3096 in the exact conditions for when it is safe to do the operation
3097 in a narrower mode. */
3100 /* Nonzero if this is a comparison operation;
3101 if both args are promoted shorts, compare the original shorts.
3102 Also implies COMMON. */
3103 int short_compare = 0;
3105 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3108 /* True if both operands have arithmetic type. */
3109 bool arithmetic_types_p;
3111 /* Apply default conversions. */
3115 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3116 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3117 || code == TRUTH_XOR_EXPR)
3119 if (!really_overloaded_fn (op0))
3120 op0 = decay_conversion (op0);
3121 if (!really_overloaded_fn (op1))
3122 op1 = decay_conversion (op1);
3126 if (!really_overloaded_fn (op0))
3127 op0 = default_conversion (op0);
3128 if (!really_overloaded_fn (op1))
3129 op1 = default_conversion (op1);
3132 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3133 STRIP_TYPE_NOPS (op0);
3134 STRIP_TYPE_NOPS (op1);
3136 /* DTRT if one side is an overloaded function, but complain about it. */
3137 if (type_unknown_p (op0))
3139 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3140 if (t != error_mark_node)
3142 pedwarn ("assuming cast to type %qT from overloaded function",
3147 if (type_unknown_p (op1))
3149 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3150 if (t != error_mark_node)
3152 pedwarn ("assuming cast to type %qT from overloaded function",
3158 type0 = TREE_TYPE (op0);
3159 type1 = TREE_TYPE (op1);
3161 /* The expression codes of the data types of the arguments tell us
3162 whether the arguments are integers, floating, pointers, etc. */
3163 code0 = TREE_CODE (type0);
3164 code1 = TREE_CODE (type1);
3166 /* If an error was already reported for one of the arguments,
3167 avoid reporting another error. */
3169 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3170 return error_mark_node;
3172 if ((invalid_op_diag
3173 = targetm.invalid_binary_op (code, type0, type1)))
3175 error (invalid_op_diag);
3176 return error_mark_node;
3182 /* Subtraction of two similar pointers.
3183 We must subtract them as integers, then divide by object size. */
3184 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3185 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3187 return pointer_diff (op0, op1, common_type (type0, type1));
3188 /* In all other cases except pointer - int, the usual arithmetic
3190 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3195 /* The pointer - int case is just like pointer + int; fall
3198 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3199 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3203 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3204 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3205 if (processing_template_decl)
3207 result_type = TREE_TYPE (ptr_operand);
3210 return cp_pointer_int_sum (code,
3221 case TRUNC_DIV_EXPR:
3223 case FLOOR_DIV_EXPR:
3224 case ROUND_DIV_EXPR:
3225 case EXACT_DIV_EXPR:
3226 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3227 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3228 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3229 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
3231 enum tree_code tcode0 = code0, tcode1 = code1;
3233 warn_for_div_by_zero (op1);
3235 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
3236 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3237 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
3238 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3240 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
3241 resultcode = RDIV_EXPR;
3243 /* When dividing two signed integers, we have to promote to int.
3244 unless we divide by a constant != -1. Note that default
3245 conversion will have been performed on the operands at this
3246 point, so we have to dig out the original type to find out if
3248 shorten = ((TREE_CODE (op0) == NOP_EXPR
3249 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3250 || (TREE_CODE (op1) == INTEGER_CST
3251 && ! integer_all_onesp (op1)));
3260 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3261 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3262 && !VECTOR_FLOAT_TYPE_P (type0)
3263 && !VECTOR_FLOAT_TYPE_P (type1)))
3267 case TRUNC_MOD_EXPR:
3268 case FLOOR_MOD_EXPR:
3269 warn_for_div_by_zero (op1);
3271 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3273 /* Although it would be tempting to shorten always here, that loses
3274 on some targets, since the modulo instruction is undefined if the
3275 quotient can't be represented in the computation mode. We shorten
3276 only if unsigned or if dividing by something we know != -1. */
3277 shorten = ((TREE_CODE (op0) == NOP_EXPR
3278 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3279 || (TREE_CODE (op1) == INTEGER_CST
3280 && ! integer_all_onesp (op1)));
3285 case TRUTH_ANDIF_EXPR:
3286 case TRUTH_ORIF_EXPR:
3287 case TRUTH_AND_EXPR:
3289 result_type = boolean_type_node;
3292 /* Shift operations: result has same type as first operand;
3293 always convert second operand to int.
3294 Also set SHORT_SHIFT if shifting rightward. */
3297 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3299 result_type = type0;
3300 if (TREE_CODE (op1) == INTEGER_CST)
3302 if (tree_int_cst_lt (op1, integer_zero_node))
3303 warning (0, "right shift count is negative");
3306 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3307 warning (0, "right shift count >= width of type");
3310 /* Convert the shift-count to an integer, regardless of
3311 size of value being shifted. */
3312 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3313 op1 = cp_convert (integer_type_node, op1);
3314 /* Avoid converting op1 to result_type later. */
3320 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3322 result_type = type0;
3323 if (TREE_CODE (op1) == INTEGER_CST)
3325 if (tree_int_cst_lt (op1, integer_zero_node))
3326 warning (0, "left shift count is negative");
3327 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3328 warning (0, "left shift count >= width of type");
3330 /* Convert the shift-count to an integer, regardless of
3331 size of value being shifted. */
3332 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3333 op1 = cp_convert (integer_type_node, op1);
3334 /* Avoid converting op1 to result_type later. */
3341 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3343 result_type = type0;
3344 if (TREE_CODE (op1) == INTEGER_CST)
3346 if (tree_int_cst_lt (op1, integer_zero_node))
3347 warning (0, (code == LROTATE_EXPR)
3348 ? G_("left rotate count is negative")
3349 : G_("right rotate count is negative"));
3350 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3351 warning (0, (code == LROTATE_EXPR)
3352 ? G_("left rotate count >= width of type")
3353 : G_("right rotate count >= width of type"));
3355 /* Convert the shift-count to an integer, regardless of
3356 size of value being shifted. */
3357 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3358 op1 = cp_convert (integer_type_node, op1);
3364 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
3365 warning (OPT_Wfloat_equal,
3366 "comparing floating point with == or != is unsafe");
3367 if ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
3368 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0)))
3369 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
3371 build_type = boolean_type_node;
3372 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3373 || code0 == COMPLEX_TYPE)
3374 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3375 || code1 == COMPLEX_TYPE))
3377 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3378 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
3379 result_type = composite_pointer_type (type0, type1, op0, op1,
3381 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
3382 && null_ptr_cst_p (op1))
3384 if (TREE_CODE (op0) == ADDR_EXPR
3385 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
3386 warning (OPT_Waddress, "the address of %qD will never be NULL",
3387 TREE_OPERAND (op0, 0));
3388 result_type = type0;
3390 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
3391 && null_ptr_cst_p (op0))
3393 if (TREE_CODE (op1) == ADDR_EXPR
3394 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
3395 warning (OPT_Waddress, "the address of %qD will never be NULL",
3396 TREE_OPERAND (op1, 0));
3397 result_type = type1;
3399 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3401 result_type = type0;
3402 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3404 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3406 result_type = type1;
3407 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3409 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3411 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
3412 == ptrmemfunc_vbit_in_delta)
3414 tree pfn0 = pfn_from_ptrmemfunc (op0);
3415 tree delta0 = delta_from_ptrmemfunc (op0);
3416 tree e1 = cp_build_binary_op (EQ_EXPR,
3418 fold_convert (TREE_TYPE (pfn0),
3419 integer_zero_node));
3420 tree e2 = cp_build_binary_op (BIT_AND_EXPR,
3423 e2 = cp_build_binary_op (EQ_EXPR, e2, integer_zero_node);
3424 op0 = cp_build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3425 op1 = cp_convert (TREE_TYPE (op0), integer_one_node);
3429 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3430 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3432 result_type = TREE_TYPE (op0);
3434 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3435 return cp_build_binary_op (code, op1, op0);
3436 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
3439 /* E will be the final comparison. */
3441 /* E1 and E2 are for scratch. */
3449 type = composite_pointer_type (type0, type1, op0, op1, "comparison");
3451 if (!same_type_p (TREE_TYPE (op0), type))
3452 op0 = cp_convert_and_check (type, op0);
3453 if (!same_type_p (TREE_TYPE (op1), type))
3454 op1 = cp_convert_and_check (type, op1);
3456 if (op0 == error_mark_node || op1 == error_mark_node)
3457 return error_mark_node;
3459 if (TREE_SIDE_EFFECTS (op0))
3460 op0 = save_expr (op0);
3461 if (TREE_SIDE_EFFECTS (op1))
3462 op1 = save_expr (op1);
3464 pfn0 = pfn_from_ptrmemfunc (op0);
3465 pfn1 = pfn_from_ptrmemfunc (op1);
3466 delta0 = delta_from_ptrmemfunc (op0);
3467 delta1 = delta_from_ptrmemfunc (op1);
3468 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
3469 == ptrmemfunc_vbit_in_delta)
3474 && ((op0.delta == op1.delta)
3475 || (!op0.pfn && op0.delta & 1 == 0
3476 && op1.delta & 1 == 0))
3478 The reason for the `!op0.pfn' bit is that a NULL
3479 pointer-to-member is any member with a zero PFN and
3480 LSB of the DELTA field is 0. */
3482 e1 = cp_build_binary_op (BIT_AND_EXPR,
3485 e1 = cp_build_binary_op (EQ_EXPR, e1, integer_zero_node);
3486 e2 = cp_build_binary_op (BIT_AND_EXPR,
3489 e2 = cp_build_binary_op (EQ_EXPR, e2, integer_zero_node);
3490 e1 = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3491 e2 = cp_build_binary_op (EQ_EXPR,
3493 fold_convert (TREE_TYPE (pfn0),
3494 integer_zero_node));
3495 e2 = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3496 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3497 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3504 && (!op0.pfn || op0.delta == op1.delta))
3506 The reason for the `!op0.pfn' bit is that a NULL
3507 pointer-to-member is any member with a zero PFN; the
3508 DELTA field is unspecified. */
3510 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3511 e2 = cp_build_binary_op (EQ_EXPR,
3513 fold_convert (TREE_TYPE (pfn0),
3514 integer_zero_node));
3515 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3517 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3518 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3519 if (code == EQ_EXPR)
3521 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3525 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
3526 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
3528 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
3529 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
3537 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3538 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3540 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3541 result_type = composite_pointer_type (type0, type1, op0, op1,
3549 if (TREE_CODE (orig_op0) == STRING_CST
3550 || TREE_CODE (orig_op1) == STRING_CST)
3551 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
3553 build_type = boolean_type_node;
3554 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3555 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3557 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3558 result_type = composite_pointer_type (type0, type1, op0, op1,
3560 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3561 && integer_zerop (op1))
3562 result_type = type0;
3563 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3564 && integer_zerop (op0))
3565 result_type = type1;
3566 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3568 result_type = type0;
3569 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3571 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3573 result_type = type1;
3574 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3578 case UNORDERED_EXPR:
3585 build_type = integer_type_node;
3586 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3588 error ("unordered comparison on non-floating point argument");
3589 return error_mark_node;
3598 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3599 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3600 || code1 == COMPLEX_TYPE)))
3601 arithmetic_types_p = 1;
3604 arithmetic_types_p = 0;
3605 /* Vector arithmetic is only allowed when both sides are vectors. */
3606 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
3608 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
3609 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
3612 binary_op_error (code, type0, type1);
3613 return error_mark_node;
3615 arithmetic_types_p = 1;
3618 /* Determine the RESULT_TYPE, if it is not already known. */
3620 && arithmetic_types_p
3621 && (shorten || common || short_compare))
3622 result_type = common_type (type0, type1);
3626 error ("invalid operands of types %qT and %qT to binary %qO",
3627 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3628 return error_mark_node;
3631 /* If we're in a template, the only thing we need to know is the
3633 if (processing_template_decl)
3635 /* Since the middle-end checks the type when doing a build2, we
3636 need to build the tree in pieces. This built tree will never
3637 get out of the front-end as we replace it when instantiating
3639 tree tmp = build2 (resultcode,
3640 build_type ? build_type : result_type,
3642 TREE_OPERAND (tmp, 0) = op0;
3646 if (arithmetic_types_p)
3648 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3650 /* For certain operations (which identify themselves by shorten != 0)
3651 if both args were extended from the same smaller type,
3652 do the arithmetic in that type and then extend.
3654 shorten !=0 and !=1 indicates a bitwise operation.
3655 For them, this optimization is safe only if
3656 both args are zero-extended or both are sign-extended.
3657 Otherwise, we might change the result.
3658 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3659 but calculated in (unsigned short) it would be (unsigned short)-1. */
3661 if (shorten && none_complex)
3663 int unsigned0, unsigned1;
3664 tree arg0 = get_narrower (op0, &unsigned0);
3665 tree arg1 = get_narrower (op1, &unsigned1);
3666 /* UNS is 1 if the operation to be done is an unsigned one. */
3667 int uns = TYPE_UNSIGNED (result_type);
3670 final_type = result_type;
3672 /* Handle the case that OP0 does not *contain* a conversion
3673 but it *requires* conversion to FINAL_TYPE. */
3675 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3676 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3677 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3678 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3680 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3682 /* For bitwise operations, signedness of nominal type
3683 does not matter. Consider only how operands were extended. */
3687 /* Note that in all three cases below we refrain from optimizing
3688 an unsigned operation on sign-extended args.
3689 That would not be valid. */
3691 /* Both args variable: if both extended in same way
3692 from same width, do it in that width.
3693 Do it unsigned if args were zero-extended. */
3694 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3695 < TYPE_PRECISION (result_type))
3696 && (TYPE_PRECISION (TREE_TYPE (arg1))
3697 == TYPE_PRECISION (TREE_TYPE (arg0)))
3698 && unsigned0 == unsigned1
3699 && (unsigned0 || !uns))
3700 result_type = c_common_signed_or_unsigned_type
3701 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3702 else if (TREE_CODE (arg0) == INTEGER_CST
3703 && (unsigned1 || !uns)
3704 && (TYPE_PRECISION (TREE_TYPE (arg1))
3705 < TYPE_PRECISION (result_type))
3706 && (type = c_common_signed_or_unsigned_type
3707 (unsigned1, TREE_TYPE (arg1)),
3708 int_fits_type_p (arg0, type)))
3710 else if (TREE_CODE (arg1) == INTEGER_CST
3711 && (unsigned0 || !uns)
3712 && (TYPE_PRECISION (TREE_TYPE (arg0))
3713 < TYPE_PRECISION (result_type))
3714 && (type = c_common_signed_or_unsigned_type
3715 (unsigned0, TREE_TYPE (arg0)),
3716 int_fits_type_p (arg1, type)))
3720 /* Comparison operations are shortened too but differently.
3721 They identify themselves by setting short_compare = 1. */
3725 /* Don't write &op0, etc., because that would prevent op0
3726 from being kept in a register.
3727 Instead, make copies of the our local variables and
3728 pass the copies by reference, then copy them back afterward. */
3729 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3730 enum tree_code xresultcode = resultcode;
3732 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3734 return cp_convert (boolean_type_node, val);
3735 op0 = xop0, op1 = xop1;
3737 resultcode = xresultcode;
3740 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3741 && warn_sign_compare
3742 /* Do not warn until the template is instantiated; we cannot
3743 bound the ranges of the arguments until that point. */
3744 && !processing_template_decl)
3746 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
3747 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3749 int unsignedp0, unsignedp1;
3750 tree primop0 = get_narrower (op0, &unsignedp0);
3751 tree primop1 = get_narrower (op1, &unsignedp1);
3753 /* Check for comparison of different enum types. */
3754 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3755 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3756 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3757 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3759 warning (OPT_Wsign_compare, "comparison between types %q#T and %q#T",
3760 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3763 /* Give warnings for comparisons between signed and unsigned
3764 quantities that may fail. */
3765 /* Do the checking based on the original operand trees, so that
3766 casts will be considered, but default promotions won't be. */
3768 /* Do not warn if the comparison is being done in a signed type,
3769 since the signed type will only be chosen if it can represent
3770 all the values of the unsigned type. */
3771 if (!TYPE_UNSIGNED (result_type))
3773 /* Do not warn if both operands are unsigned. */
3774 else if (op0_signed == op1_signed)
3776 /* Do not warn if the signed quantity is an unsuffixed
3777 integer literal (or some static constant expression
3778 involving such literals or a conditional expression
3779 involving such literals) and it is non-negative. */
3780 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3781 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3783 /* Do not warn if the comparison is an equality operation,
3784 the unsigned quantity is an integral constant and it does
3785 not use the most significant bit of result_type. */
3786 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3787 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3788 && int_fits_type_p (orig_op1, c_common_signed_type
3790 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3791 && int_fits_type_p (orig_op0, c_common_signed_type
3795 warning (OPT_Wsign_compare,
3796 "comparison between signed and unsigned integer expressions");
3798 /* Warn if two unsigned values are being compared in a size
3799 larger than their original size, and one (and only one) is the
3800 result of a `~' operator. This comparison will always fail.
3802 Also warn if one operand is a constant, and the constant does not
3803 have all bits set that are set in the ~ operand when it is
3806 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3807 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3809 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3810 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3811 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3812 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3814 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3817 HOST_WIDE_INT constant, mask;
3821 if (host_integerp (primop0, 0))
3824 unsignedp = unsignedp1;
3825 constant = tree_low_cst (primop0, 0);
3830 unsignedp = unsignedp0;
3831 constant = tree_low_cst (primop1, 0);
3834 bits = TYPE_PRECISION (TREE_TYPE (primop));
3835 if (bits < TYPE_PRECISION (result_type)
3836 && bits < HOST_BITS_PER_LONG && unsignedp)
3838 mask = (~ (HOST_WIDE_INT) 0) << bits;
3839 if ((mask & constant) != mask)
3840 warning (OPT_Wsign_compare, "comparison of promoted ~unsigned with constant");
3843 else if (unsignedp0 && unsignedp1
3844 && (TYPE_PRECISION (TREE_TYPE (primop0))
3845 < TYPE_PRECISION (result_type))
3846 && (TYPE_PRECISION (TREE_TYPE (primop1))
3847 < TYPE_PRECISION (result_type)))
3848 warning (OPT_Wsign_compare, "comparison of promoted ~unsigned with unsigned");
3853 /* Issue warnings about peculiar, but valid, uses of NULL. */
3854 if ((orig_op0 == null_node || orig_op1 == null_node)
3855 /* It's reasonable to use pointer values as operands of &&
3856 and ||, so NULL is no exception. */
3857 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3858 && ( /* Both are NULL (or 0) and the operation was not a comparison. */
3859 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3860 && code != EQ_EXPR && code != NE_EXPR)
3861 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3862 || (!null_ptr_cst_p (orig_op0) && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3863 || (!null_ptr_cst_p (orig_op1) && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)))
3864 /* Some sort of arithmetic operation involving NULL was
3865 performed. Note that pointer-difference and pointer-addition
3866 have already been handled above, and so we don't end up here in
3868 warning (OPT_Wpointer_arith, "NULL used in arithmetic");
3871 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3872 Then the expression will be built.
3873 It will be given type FINAL_TYPE if that is nonzero;
3874 otherwise, it will be given type RESULT_TYPE. */
3877 if (TREE_TYPE (op0) != result_type)
3878 op0 = cp_convert_and_check (result_type, op0);
3879 if (TREE_TYPE (op1) != result_type)
3880 op1 = cp_convert_and_check (result_type, op1);
3882 if (op0 == error_mark_node || op1 == error_mark_node)
3883 return error_mark_node;
3886 if (build_type == NULL_TREE)
3887 build_type = result_type;
3889 result = build2 (resultcode, build_type, op0, op1);
3890 result = fold_if_not_in_template (result);
3891 if (final_type != 0)
3892 result = cp_convert (final_type, result);
3894 if (TREE_OVERFLOW_P (result)
3895 && !TREE_OVERFLOW_P (op0)
3896 && !TREE_OVERFLOW_P (op1))
3897 overflow_warning (result);
3902 /* Return a tree for the sum or difference (RESULTCODE says which)
3903 of pointer PTROP and integer INTOP. */
3906 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
3908 tree res_type = TREE_TYPE (ptrop);
3910 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3911 in certain circumstance (when it's valid to do so). So we need
3912 to make sure it's complete. We don't need to check here, if we
3913 can actually complete it at all, as those checks will be done in
3914 pointer_int_sum() anyway. */
3915 complete_type (TREE_TYPE (res_type));
3917 return pointer_int_sum (resultcode, ptrop,
3918 fold_if_not_in_template (intop));
3921 /* Return a tree for the difference of pointers OP0 and OP1.
3922 The resulting tree has type int. */
3925 pointer_diff (tree op0, tree op1, tree ptrtype)
3928 tree restype = ptrdiff_type_node;
3929 tree target_type = TREE_TYPE (ptrtype);
3931 if (!complete_type_or_else (target_type, NULL_TREE))
3932 return error_mark_node;
3934 if (pedantic || warn_pointer_arith)
3936 if (TREE_CODE (target_type) == VOID_TYPE)
3937 pedwarn ("ISO C++ forbids using pointer of type %<void *%> in subtraction");
3938 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3939 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3940 if (TREE_CODE (target_type) == METHOD_TYPE)
3941 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3944 /* First do the subtraction as integers;
3945 then drop through to build the divide operator. */
3947 op0 = cp_build_binary_op (MINUS_EXPR,
3948 cp_convert (restype, op0),
3949 cp_convert (restype, op1));
3951 /* This generates an error if op1 is a pointer to an incomplete type. */
3952 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3953 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3955 op1 = (TYPE_PTROB_P (ptrtype)
3956 ? size_in_bytes (target_type)
3957 : integer_one_node);
3959 /* Do the division. */
3961 result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3962 return fold_if_not_in_template (result);
3965 /* Construct and perhaps optimize a tree representation
3966 for a unary operation. CODE, a tree_code, specifies the operation
3967 and XARG is the operand. */
3970 build_x_unary_op (enum tree_code code, tree xarg)
3972 tree orig_expr = xarg;
3976 if (processing_template_decl)
3978 if (type_dependent_expression_p (xarg))
3979 return build_min_nt (code, xarg, NULL_TREE);
3981 xarg = build_non_dependent_expr (xarg);
3986 /* [expr.unary.op] says:
3988 The address of an object of incomplete type can be taken.
3990 (And is just the ordinary address operator, not an overloaded
3991 "operator &".) However, if the type is a template
3992 specialization, we must complete the type at this point so that
3993 an overloaded "operator &" will be available if required. */
3994 if (code == ADDR_EXPR
3995 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3996 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
3997 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
3998 || (TREE_CODE (xarg) == OFFSET_REF)))
3999 /* Don't look for a function. */;
4001 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
4002 /*overloaded_p=*/NULL);
4003 if (!exp && code == ADDR_EXPR)
4005 /* A pointer to member-function can be formed only by saying
4007 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4008 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4010 if (TREE_CODE (xarg) != OFFSET_REF
4011 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4013 error ("invalid use of %qE to form a pointer-to-member-function",
4015 if (TREE_CODE (xarg) != OFFSET_REF)
4016 inform (" a qualified-id is required");
4017 return error_mark_node;
4021 error ("parentheses around %qE cannot be used to form a"
4022 " pointer-to-member-function",
4024 PTRMEM_OK_P (xarg) = 1;
4028 if (TREE_CODE (xarg) == OFFSET_REF)
4030 ptrmem = PTRMEM_OK_P (xarg);
4032 if (!ptrmem && !flag_ms_extensions
4033 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4035 /* A single non-static member, make sure we don't allow a
4036 pointer-to-member. */
4037 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4038 TREE_OPERAND (xarg, 0),
4039 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4040 PTRMEM_OK_P (xarg) = ptrmem;
4043 else if (TREE_CODE (xarg) == TARGET_EXPR)
4044 warning (0, "taking address of temporary");
4045 exp = build_unary_op (ADDR_EXPR, xarg, 0);
4048 if (processing_template_decl && exp != error_mark_node)
4049 exp = build_min_non_dep (code, exp, orig_expr,
4050 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4051 if (TREE_CODE (exp) == ADDR_EXPR)
4052 PTRMEM_OK_P (exp) = ptrmem;
4056 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4057 constants, where a null value is represented by an INTEGER_CST of
4061 cp_truthvalue_conversion (tree expr)
4063 tree type = TREE_TYPE (expr);
4064 if (TYPE_PTRMEM_P (type))
4065 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4067 return c_common_truthvalue_conversion (expr);
4070 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4073 condition_conversion (tree expr)
4076 if (processing_template_decl)
4078 t = perform_implicit_conversion (boolean_type_node, expr);
4079 t = fold_build_cleanup_point_expr (boolean_type_node, t);
4083 /* Return an ADDR_EXPR giving the address of T. This function
4084 attempts no optimizations or simplifications; it is a low-level
4088 build_address (tree t)
4092 if (error_operand_p (t) || !cxx_mark_addressable (t))
4093 return error_mark_node;
4095 addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
4100 /* Return a NOP_EXPR converting EXPR to TYPE. */
4103 build_nop (tree type, tree expr)
4105 if (type == error_mark_node || error_operand_p (expr))
4107 return build1 (NOP_EXPR, type, expr);
4110 /* C++: Must handle pointers to members.
4112 Perhaps type instantiation should be extended to handle conversion
4113 from aggregates to types we don't yet know we want? (Or are those
4114 cases typically errors which should be reported?)
4116 NOCONVERT nonzero suppresses the default promotions
4117 (such as from short to int). */
4120 build_unary_op (enum tree_code code, tree xarg, int noconvert)
4122 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4125 const char *errstring = NULL;
4127 const char *invalid_op_diag;
4129 if (arg == error_mark_node)
4130 return error_mark_node;
4132 if ((invalid_op_diag
4133 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
4138 error (invalid_op_diag);
4139 return error_mark_node;
4144 case UNARY_PLUS_EXPR:
4147 int flags = WANT_ARITH | WANT_ENUM;
4148 /* Unary plus (but not unary minus) is allowed on pointers. */
4149 if (code == UNARY_PLUS_EXPR)
4150 flags |= WANT_POINTER;
4151 arg = build_expr_type_conversion (flags, arg, true);
4153 errstring = (code == NEGATE_EXPR
4154 ? "wrong type argument to unary minus"
4155 : "wrong type argument to unary plus");
4158 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
4159 arg = perform_integral_promotions (arg);
4161 /* Make sure the result is not an lvalue: a unary plus or minus
4162 expression is always a rvalue. */
4169 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4173 arg = default_conversion (arg);
4175 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
4178 errstring = "wrong type argument to bit-complement";
4179 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
4180 arg = perform_integral_promotions (arg);
4184 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4185 errstring = "wrong type argument to abs";
4186 else if (!noconvert)
4187 arg = default_conversion (arg);
4191 /* Conjugating a real value is a no-op, but allow it anyway. */
4192 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4193 errstring = "wrong type argument to conjugation";
4194 else if (!noconvert)
4195 arg = default_conversion (arg);
4198 case TRUTH_NOT_EXPR:
4199 arg = perform_implicit_conversion (boolean_type_node, arg);
4200 val = invert_truthvalue (arg);
4201 if (arg != error_mark_node)
4203 errstring = "in argument to unary !";
4210 if (TREE_CODE (arg) == COMPLEX_CST)
4211 return TREE_REALPART (arg);
4212 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4214 arg = build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
4215 return fold_if_not_in_template (arg);
4221 if (TREE_CODE (arg) == COMPLEX_CST)
4222 return TREE_IMAGPART (arg);
4223 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4225 arg = build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
4226 return fold_if_not_in_template (arg);
4229 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4231 case PREINCREMENT_EXPR:
4232 case POSTINCREMENT_EXPR:
4233 case PREDECREMENT_EXPR:
4234 case POSTDECREMENT_EXPR:
4235 /* Handle complex lvalues (when permitted)
4236 by reduction to simpler cases. */
4238 val = unary_complex_lvalue (code, arg);
4242 /* Increment or decrement the real part of the value,
4243 and don't change the imaginary part. */
4244 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4248 arg = stabilize_reference (arg);
4249 real = build_unary_op (REALPART_EXPR, arg, 1);
4250 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4251 real = build_unary_op (code, real, 1);
4252 if (real == error_mark_node || imag == error_mark_node)
4253 return error_mark_node;
4254 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4258 /* Report invalid types. */
4260 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4263 if (code == PREINCREMENT_EXPR)
4264 errstring ="no pre-increment operator for type";
4265 else if (code == POSTINCREMENT_EXPR)
4266 errstring ="no post-increment operator for type";
4267 else if (code == PREDECREMENT_EXPR)
4268 errstring ="no pre-decrement operator for type";
4270 errstring ="no post-decrement operator for type";
4273 else if (arg == error_mark_node)
4274 return error_mark_node;
4276 /* Report something read-only. */
4278 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4279 || TREE_READONLY (arg))
4280 readonly_error (arg, ((code == PREINCREMENT_EXPR
4281 || code == POSTINCREMENT_EXPR)
4282 ? "increment" : "decrement"));
4287 tree result_type = TREE_TYPE (arg);
4289 declared_type = unlowered_expr_type (arg);
4291 arg = get_unwidened (arg, 0);
4292 argtype = TREE_TYPE (arg);
4294 /* ARM $5.2.5 last annotation says this should be forbidden. */
4295 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4296 pedwarn ((code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4297 ? G_("ISO C++ forbids incrementing an enum")
4298 : G_("ISO C++ forbids decrementing an enum"));
4300 /* Compute the increment. */
4302 if (TREE_CODE (argtype) == POINTER_TYPE)
4304 tree type = complete_type (TREE_TYPE (argtype));
4306 if (!COMPLETE_OR_VOID_TYPE_P (type))
4307 error (((code == PREINCREMENT_EXPR
4308 || code == POSTINCREMENT_EXPR))
4309 ? G_("cannot increment a pointer to incomplete type %qT")
4310 : G_("cannot decrement a pointer to incomplete type %qT"),
4311 TREE_TYPE (argtype));
4312 else if ((pedantic || warn_pointer_arith)
4313 && !TYPE_PTROB_P (argtype))
4314 pedwarn ((code == PREINCREMENT_EXPR
4315 || code == POSTINCREMENT_EXPR)
4316 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
4317 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
4319 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
4322 inc = integer_one_node;
4324 inc = cp_convert (argtype, inc);
4326 /* Complain about anything else that is not a true lvalue. */
4327 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4328 || code == POSTINCREMENT_EXPR)
4329 ? lv_increment : lv_decrement)))
4330 return error_mark_node;
4332 /* Forbid using -- on `bool'. */
4333 if (same_type_p (declared_type, boolean_type_node))
4335 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4337 error ("invalid use of Boolean expression as operand "
4338 "to %<operator--%>");
4339 return error_mark_node;
4341 val = boolean_increment (code, arg);
4344 val = build2 (code, TREE_TYPE (arg), arg, inc);
4346 TREE_SIDE_EFFECTS (val) = 1;
4347 return cp_convert (result_type, val);
4351 /* Note that this operation never does default_conversion
4352 regardless of NOCONVERT. */
4354 argtype = lvalue_type (arg);
4356 if (TREE_CODE (arg) == OFFSET_REF)
4359 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4361 tree type = build_pointer_type (TREE_TYPE (argtype));
4362 arg = build1 (CONVERT_EXPR, type, arg);
4365 else if (pedantic && DECL_MAIN_P (arg))
4367 pedwarn ("ISO C++ forbids taking address of function %<::main%>");
4369 /* Let &* cancel out to simplify resulting code. */
4370 if (TREE_CODE (arg) == INDIRECT_REF)
4372 /* We don't need to have `current_class_ptr' wrapped in a
4373 NON_LVALUE_EXPR node. */
4374 if (arg == current_class_ref)
4375 return current_class_ptr;
4377 arg = TREE_OPERAND (arg, 0);
4378 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4380 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
4381 arg = build1 (CONVERT_EXPR, type, arg);
4384 /* Don't let this be an lvalue. */
4389 /* Uninstantiated types are all functions. Taking the
4390 address of a function is a no-op, so just return the
4393 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4394 || !IDENTIFIER_OPNAME_P (arg));
4396 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4397 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4399 /* They're trying to take the address of a unique non-static
4400 member function. This is ill-formed (except in MS-land),
4401 but let's try to DTRT.
4402 Note: We only handle unique functions here because we don't
4403 want to complain if there's a static overload; non-unique
4404 cases will be handled by instantiate_type. But we need to
4405 handle this case here to allow casts on the resulting PMF.
4406 We could defer this in non-MS mode, but it's easier to give
4407 a useful error here. */
4409 /* Inside constant member functions, the `this' pointer
4410 contains an extra const qualifier. TYPE_MAIN_VARIANT
4411 is used here to remove this const from the diagnostics
4412 and the created OFFSET_REF. */
4413 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4414 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4417 if (! flag_ms_extensions)
4419 tree name = DECL_NAME (fn);
4420 if (current_class_type
4421 && TREE_OPERAND (arg, 0) == current_class_ref)
4422 /* An expression like &memfn. */
4423 pedwarn ("ISO C++ forbids taking the address of an unqualified"
4424 " or parenthesized non-static member function to form"
4425 " a pointer to member function. Say %<&%T::%D%>",
4428 pedwarn ("ISO C++ forbids taking the address of a bound member"
4429 " function to form a pointer to member function."
4433 arg = build_offset_ref (base, fn, /*address_p=*/true);
4437 if (type_unknown_p (arg))
4438 return build1 (ADDR_EXPR, unknown_type_node, arg);
4440 /* Handle complex lvalues (when permitted)
4441 by reduction to simpler cases. */
4442 val = unary_complex_lvalue (code, arg);
4446 switch (TREE_CODE (arg))
4451 case FIX_TRUNC_EXPR:
4452 if (! lvalue_p (arg) && pedantic)
4453 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4457 arg = BASELINK_FUNCTIONS (arg);
4461 arg = OVL_CURRENT (arg);
4465 /* Turn a reference to a non-static data member into a
4466 pointer-to-member. */
4471 if (!PTRMEM_OK_P (arg))
4472 return build_unary_op (code, arg, 0);
4474 t = TREE_OPERAND (arg, 1);
4475 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4477 error ("cannot create pointer to reference member %qD", t);
4478 return error_mark_node;
4481 type = build_ptrmem_type (context_for_name_lookup (t),
4483 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4491 /* Anything not already handled and not a true memory reference
4493 if (TREE_CODE (argtype) != FUNCTION_TYPE
4494 && TREE_CODE (argtype) != METHOD_TYPE
4495 && TREE_CODE (arg) != OFFSET_REF
4496 && !lvalue_or_else (arg, lv_addressof))
4497 return error_mark_node;
4499 if (argtype != error_mark_node)
4500 argtype = build_pointer_type (argtype);
4502 /* In a template, we are processing a non-dependent expression
4503 so we can just form an ADDR_EXPR with the correct type. */
4504 if (processing_template_decl)
4506 val = build_address (arg);
4507 if (TREE_CODE (arg) == OFFSET_REF)
4508 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
4512 if (TREE_CODE (arg) != COMPONENT_REF)
4514 val = build_address (arg);
4515 if (TREE_CODE (arg) == OFFSET_REF)
4516 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
4518 else if (TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
4520 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4522 /* We can only get here with a single static member
4524 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4525 && DECL_STATIC_FUNCTION_P (fn));
4527 val = build_address (fn);
4528 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
4529 /* Do not lose object's side effects. */
4530 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
4531 TREE_OPERAND (arg, 0), val);
4533 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4535 error ("attempt to take address of bit-field structure member %qD",
4536 TREE_OPERAND (arg, 1));
4537 return error_mark_node;
4541 tree object = TREE_OPERAND (arg, 0);
4542 tree field = TREE_OPERAND (arg, 1);
4543 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4544 (TREE_TYPE (object), decl_type_context (field)));
4545 val = build_address (arg);
4548 if (TREE_CODE (argtype) == POINTER_TYPE
4549 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4551 build_ptrmemfunc_type (argtype);
4552 val = build_ptrmemfunc (argtype, val, 0,
4553 /*c_cast_p=*/false);
4565 argtype = TREE_TYPE (arg);
4566 return fold_if_not_in_template (build1 (code, argtype, arg));
4569 error ("%s", errstring);
4570 return error_mark_node;
4573 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4574 for certain kinds of expressions which are not really lvalues
4575 but which we can accept as lvalues.
4577 If ARG is not a kind of expression we can handle, return
4581 unary_complex_lvalue (enum tree_code code, tree arg)
4583 /* Inside a template, making these kinds of adjustments is
4584 pointless; we are only concerned with the type of the
4586 if (processing_template_decl)
4589 /* Handle (a, b) used as an "lvalue". */
4590 if (TREE_CODE (arg) == COMPOUND_EXPR)
4592 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4593 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
4594 TREE_OPERAND (arg, 0), real_result);
4597 /* Handle (a ? b : c) used as an "lvalue". */
4598 if (TREE_CODE (arg) == COND_EXPR
4599 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4600 return rationalize_conditional_expr (code, arg);
4602 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4603 if (TREE_CODE (arg) == MODIFY_EXPR
4604 || TREE_CODE (arg) == PREINCREMENT_EXPR
4605 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4607 tree lvalue = TREE_OPERAND (arg, 0);
4608 if (TREE_SIDE_EFFECTS (lvalue))
4610 lvalue = stabilize_reference (lvalue);
4611 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
4612 lvalue, TREE_OPERAND (arg, 1));
4614 return unary_complex_lvalue
4615 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4618 if (code != ADDR_EXPR)
4621 /* Handle (a = b) used as an "lvalue" for `&'. */
4622 if (TREE_CODE (arg) == MODIFY_EXPR
4623 || TREE_CODE (arg) == INIT_EXPR)
4625 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4626 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
4628 TREE_NO_WARNING (arg) = 1;
4632 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4633 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4634 || TREE_CODE (arg) == OFFSET_REF)
4637 /* We permit compiler to make function calls returning
4638 objects of aggregate type look like lvalues. */
4642 if (TREE_CODE (targ) == SAVE_EXPR)
4643 targ = TREE_OPERAND (targ, 0);
4645 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4647 if (TREE_CODE (arg) == SAVE_EXPR)
4650 targ = build_cplus_new (TREE_TYPE (arg), arg);
4651 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4654 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4655 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4656 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4659 /* Don't let anything else be handled specially. */
4663 /* Mark EXP saying that we need to be able to take the
4664 address of it; it should not be allocated in a register.
4665 Value is true if successful.
4667 C++: we do not allow `current_class_ptr' to be addressable. */
4670 cxx_mark_addressable (tree exp)
4675 switch (TREE_CODE (x))
4682 x = TREE_OPERAND (x, 0);
4686 if (x == current_class_ptr)
4688 error ("cannot take the address of %<this%>, which is an rvalue expression");
4689 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
4695 /* Caller should not be trying to mark initialized
4696 constant fields addressable. */
4697 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
4698 || DECL_IN_AGGR_P (x) == 0
4700 || DECL_EXTERNAL (x));
4705 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4706 && !DECL_ARTIFICIAL (x))
4708 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
4711 ("address of explicit register variable %qD requested", x);
4714 else if (extra_warnings)
4716 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
4718 TREE_ADDRESSABLE (x) = 1;
4722 TREE_ADDRESSABLE (x) = 1;
4726 TREE_ADDRESSABLE (x) = 1;
4730 TREE_ADDRESSABLE (x) = 1;
4731 cxx_mark_addressable (TREE_OPERAND (x, 0));
4739 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4742 build_x_conditional_expr (tree ifexp, tree op1, tree op2)
4744 tree orig_ifexp = ifexp;
4745 tree orig_op1 = op1;
4746 tree orig_op2 = op2;
4749 if (processing_template_decl)
4751 /* The standard says that the expression is type-dependent if
4752 IFEXP is type-dependent, even though the eventual type of the
4753 expression doesn't dependent on IFEXP. */
4754 if (type_dependent_expression_p (ifexp)
4755 /* As a GNU extension, the middle operand may be omitted. */
4756 || (op1 && type_dependent_expression_p (op1))
4757 || type_dependent_expression_p (op2))
4758 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4759 ifexp = build_non_dependent_expr (ifexp);
4761 op1 = build_non_dependent_expr (op1);
4762 op2 = build_non_dependent_expr (op2);
4765 expr = build_conditional_expr (ifexp, op1, op2);
4766 if (processing_template_decl && expr != error_mark_node)
4767 return build_min_non_dep (COND_EXPR, expr,
4768 orig_ifexp, orig_op1, orig_op2);
4772 /* Given a list of expressions, return a compound expression
4773 that performs them all and returns the value of the last of them. */
4775 tree build_x_compound_expr_from_list (tree list, const char *msg)
4777 tree expr = TREE_VALUE (list);
4779 if (TREE_CHAIN (list))
4782 pedwarn ("%s expression list treated as compound expression", msg);
4784 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
4785 expr = build_x_compound_expr (expr, TREE_VALUE (list));
4791 /* Handle overloading of the ',' operator when needed. */
4794 build_x_compound_expr (tree op1, tree op2)
4797 tree orig_op1 = op1;
4798 tree orig_op2 = op2;
4800 if (processing_template_decl)
4802 if (type_dependent_expression_p (op1)
4803 || type_dependent_expression_p (op2))
4804 return build_min_nt (COMPOUND_EXPR, op1, op2);
4805 op1 = build_non_dependent_expr (op1);
4806 op2 = build_non_dependent_expr (op2);
4809 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE,
4810 /*overloaded_p=*/NULL);
4812 result = build_compound_expr (op1, op2);
4814 if (processing_template_decl && result != error_mark_node)
4815 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
4820 /* Build a compound expression. */
4823 build_compound_expr (tree lhs, tree rhs)
4825 lhs = convert_to_void (lhs, "left-hand operand of comma");
4827 if (lhs == error_mark_node || rhs == error_mark_node)
4828 return error_mark_node;
4830 if (TREE_CODE (rhs) == TARGET_EXPR)
4832 /* If the rhs is a TARGET_EXPR, then build the compound
4833 expression inside the target_expr's initializer. This
4834 helps the compiler to eliminate unnecessary temporaries. */
4835 tree init = TREE_OPERAND (rhs, 1);
4837 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
4838 TREE_OPERAND (rhs, 1) = init;
4843 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
4846 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
4847 casts away constness. CAST gives the type of cast. */
4850 check_for_casting_away_constness (tree src_type, tree dest_type,
4851 enum tree_code cast)
4853 /* C-style casts are allowed to cast away constness. With
4854 WARN_CAST_QUAL, we still want to issue a warning. */
4855 if (cast == CAST_EXPR && !warn_cast_qual)
4858 if (casts_away_constness (src_type, dest_type))
4862 warning (OPT_Wcast_qual,
4863 "cast from type %qT to type %qT casts away constness",
4864 src_type, dest_type);
4867 case STATIC_CAST_EXPR:
4868 error ("static_cast from type %qT to type %qT casts away constness",
4869 src_type, dest_type);
4872 case REINTERPRET_CAST_EXPR:
4873 error ("reinterpret_cast from type %qT to type %qT casts away constness",
4874 src_type, dest_type);
4881 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
4882 (another pointer-to-member type in the same hierarchy) and return
4883 the converted expression. If ALLOW_INVERSE_P is permitted, a
4884 pointer-to-derived may be converted to pointer-to-base; otherwise,
4885 only the other direction is permitted. If C_CAST_P is true, this
4886 conversion is taking place as part of a C-style cast. */
4889 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
4892 if (TYPE_PTRMEM_P (type))
4896 if (TREE_CODE (expr) == PTRMEM_CST)
4897 expr = cplus_expand_constant (expr);
4898 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
4899 TYPE_PTRMEM_CLASS_TYPE (type),
4902 if (!integer_zerop (delta))
4904 tree cond, op1, op2;
4906 cond = cp_build_binary_op (EQ_EXPR,
4908 build_int_cst (TREE_TYPE (expr), -1));
4909 op1 = build_nop (ptrdiff_type_node, expr);
4910 op2 = cp_build_binary_op (PLUS_EXPR, op1, delta);
4912 expr = fold_build3 (COND_EXPR, ptrdiff_type_node, cond, op1, op2);
4916 return build_nop (type, expr);
4919 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
4920 allow_inverse_p, c_cast_p);
4923 /* If EXPR is an INTEGER_CST and ORIG is an arithmetic constant, return
4924 a version of EXPR that has TREE_OVERFLOW set if it is set in ORIG.
4925 Otherwise, return EXPR unchanged. */
4928 ignore_overflows (tree expr, tree orig)
4930 if (TREE_CODE (expr) == INTEGER_CST
4931 && CONSTANT_CLASS_P (orig)
4932 && TREE_CODE (orig) != STRING_CST
4933 && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig))
4935 if (!TREE_OVERFLOW (orig))
4936 /* Ensure constant sharing. */
4937 expr = build_int_cst_wide (TREE_TYPE (expr),
4938 TREE_INT_CST_LOW (expr),
4939 TREE_INT_CST_HIGH (expr));
4942 /* Avoid clobbering a shared constant. */
4943 expr = copy_node (expr);
4944 TREE_OVERFLOW (expr) = TREE_OVERFLOW (orig);
4950 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
4951 this static_cast is being attempted as one of the possible casts
4952 allowed by a C-style cast. (In that case, accessibility of base
4953 classes is not considered, and it is OK to cast away
4954 constness.) Return the result of the cast. *VALID_P is set to
4955 indicate whether or not the cast was valid. */
4958 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
4965 /* Assume the cast is valid. */
4968 intype = TREE_TYPE (expr);
4970 /* Save casted types in the function's used types hash table. */
4971 used_types_insert (type);
4973 /* [expr.static.cast]
4975 An lvalue of type "cv1 B", where B is a class type, can be cast
4976 to type "reference to cv2 D", where D is a class derived (clause
4977 _class.derived_) from B, if a valid standard conversion from
4978 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
4979 same cv-qualification as, or greater cv-qualification than, cv1,
4980 and B is not a virtual base class of D. */
4981 /* We check this case before checking the validity of "TYPE t =
4982 EXPR;" below because for this case:
4985 struct D : public B { D(const B&); };
4987 void f() { static_cast<const D&>(b); }
4989 we want to avoid constructing a new D. The standard is not
4990 completely clear about this issue, but our interpretation is
4991 consistent with other compilers. */
4992 if (TREE_CODE (type) == REFERENCE_TYPE
4993 && CLASS_TYPE_P (TREE_TYPE (type))
4994 && CLASS_TYPE_P (intype)
4995 && real_lvalue_p (expr)
4996 && DERIVED_FROM_P (intype, TREE_TYPE (type))
4997 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
4998 build_pointer_type (TYPE_MAIN_VARIANT
4999 (TREE_TYPE (type))))
5001 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5005 /* There is a standard conversion from "D*" to "B*" even if "B"
5006 is ambiguous or inaccessible. If this is really a
5007 static_cast, then we check both for inaccessibility and
5008 ambiguity. However, if this is a static_cast being performed
5009 because the user wrote a C-style cast, then accessibility is
5011 base = lookup_base (TREE_TYPE (type), intype,
5012 c_cast_p ? ba_unique : ba_check,
5015 /* Convert from "B*" to "D*". This function will check that "B"
5016 is not a virtual base of "D". */
5017 expr = build_base_path (MINUS_EXPR, build_address (expr),
5018 base, /*nonnull=*/false);
5019 /* Convert the pointer to a reference -- but then remember that
5020 there are no expressions with reference type in C++. */
5021 return convert_from_reference (build_nop (type, expr));
5026 /* [expr.static.cast]
5028 An expression e can be explicitly converted to a type T using a
5029 static_cast of the form static_cast<T>(e) if the declaration T
5030 t(e);" is well-formed, for some invented temporary variable
5032 result = perform_direct_initialization_if_possible (type, expr,
5036 result = convert_from_reference (result);
5038 /* Ignore any integer overflow caused by the cast. */
5039 result = ignore_overflows (result, orig);
5041 /* [expr.static.cast]
5043 If T is a reference type, the result is an lvalue; otherwise,
5044 the result is an rvalue. */
5045 if (TREE_CODE (type) != REFERENCE_TYPE)
5046 result = rvalue (result);
5050 /* [expr.static.cast]
5052 Any expression can be explicitly converted to type cv void. */
5053 if (TREE_CODE (type) == VOID_TYPE)
5054 return convert_to_void (expr, /*implicit=*/NULL);
5056 /* [expr.static.cast]
5058 The inverse of any standard conversion sequence (clause _conv_),
5059 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
5060 (_conv.array_), function-to-pointer (_conv.func_), and boolean
5061 (_conv.bool_) conversions, can be performed explicitly using
5062 static_cast subject to the restriction that the explicit
5063 conversion does not cast away constness (_expr.const.cast_), and
5064 the following additional rules for specific cases: */
5065 /* For reference, the conversions not excluded are: integral
5066 promotions, floating point promotion, integral conversions,
5067 floating point conversions, floating-integral conversions,
5068 pointer conversions, and pointer to member conversions. */
5071 A value of integral _or enumeration_ type can be explicitly
5072 converted to an enumeration type. */
5073 /* The effect of all that is that any conversion between any two
5074 types which are integral, floating, or enumeration types can be
5076 if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
5077 && (INTEGRAL_TYPE_P (intype) || SCALAR_FLOAT_TYPE_P (intype)))
5079 expr = ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
5081 /* Ignore any integer overflow caused by the cast. */
5082 expr = ignore_overflows (expr, orig);
5086 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
5087 && CLASS_TYPE_P (TREE_TYPE (type))
5088 && CLASS_TYPE_P (TREE_TYPE (intype))
5089 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
5090 (TREE_TYPE (intype))),
5091 build_pointer_type (TYPE_MAIN_VARIANT
5092 (TREE_TYPE (type)))))
5097 check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
5098 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
5099 c_cast_p ? ba_unique : ba_check,
5101 return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
5104 if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5105 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5112 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
5113 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
5115 if (TYPE_PTRMEM_P (type))
5117 t1 = (build_ptrmem_type
5119 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
5120 t2 = (build_ptrmem_type
5122 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
5129 if (can_convert (t1, t2) || can_convert (t2, t1))
5132 check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
5133 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
5138 /* [expr.static.cast]
5140 An rvalue of type "pointer to cv void" can be explicitly
5141 converted to a pointer to object type. A value of type pointer
5142 to object converted to "pointer to cv void" and back to the
5143 original pointer type will have its original value. */
5144 if (TREE_CODE (intype) == POINTER_TYPE
5145 && VOID_TYPE_P (TREE_TYPE (intype))
5146 && TYPE_PTROB_P (type))
5149 check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
5150 return build_nop (type, expr);
5154 return error_mark_node;
5157 /* Return an expression representing static_cast<TYPE>(EXPR). */
5160 build_static_cast (tree type, tree expr)
5165 if (type == error_mark_node || expr == error_mark_node)
5166 return error_mark_node;
5168 if (processing_template_decl)
5170 expr = build_min (STATIC_CAST_EXPR, type, expr);
5171 /* We don't know if it will or will not have side effects. */
5172 TREE_SIDE_EFFECTS (expr) = 1;
5173 return convert_from_reference (expr);
5176 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5177 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5178 if (TREE_CODE (type) != REFERENCE_TYPE
5179 && TREE_CODE (expr) == NOP_EXPR
5180 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5181 expr = TREE_OPERAND (expr, 0);
5183 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p);
5187 error ("invalid static_cast from type %qT to type %qT",
5188 TREE_TYPE (expr), type);
5189 return error_mark_node;
5192 /* EXPR is an expression with member function or pointer-to-member
5193 function type. TYPE is a pointer type. Converting EXPR to TYPE is
5194 not permitted by ISO C++, but we accept it in some modes. If we
5195 are not in one of those modes, issue a diagnostic. Return the
5196 converted expression. */
5199 convert_member_func_to_ptr (tree type, tree expr)
5204 intype = TREE_TYPE (expr);
5205 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
5206 || TREE_CODE (intype) == METHOD_TYPE);
5208 if (pedantic || warn_pmf2ptr)
5209 pedwarn ("converting from %qT to %qT", intype, type);
5211 if (TREE_CODE (intype) == METHOD_TYPE)
5212 expr = build_addr_func (expr);
5213 else if (TREE_CODE (expr) == PTRMEM_CST)
5214 expr = build_address (PTRMEM_CST_MEMBER (expr));
5217 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
5218 decl = build_address (decl);
5219 expr = get_member_function_from_ptrfunc (&decl, expr);
5222 return build_nop (type, expr);
5225 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
5226 If C_CAST_P is true, this reinterpret cast is being done as part of
5227 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
5228 indicate whether or not reinterpret_cast was valid. */
5231 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
5236 /* Assume the cast is invalid. */
5240 if (type == error_mark_node || error_operand_p (expr))
5241 return error_mark_node;
5243 intype = TREE_TYPE (expr);
5245 /* Save casted types in the function's used types hash table. */
5246 used_types_insert (type);
5248 /* [expr.reinterpret.cast]
5249 An lvalue expression of type T1 can be cast to the type
5250 "reference to T2" if an expression of type "pointer to T1" can be
5251 explicitly converted to the type "pointer to T2" using a
5252 reinterpret_cast. */
5253 if (TREE_CODE (type) == REFERENCE_TYPE)
5255 if (! real_lvalue_p (expr))
5257 error ("invalid cast of an rvalue expression of type "
5260 return error_mark_node;
5263 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
5264 "B" are related class types; the reinterpret_cast does not
5265 adjust the pointer. */
5266 if (TYPE_PTR_P (intype)
5267 && (cp_comptypes (TREE_TYPE (intype), TREE_TYPE (type),
5268 COMPARE_BASE | COMPARE_DERIVED)))
5269 warning (0, "casting %qT to %qT does not dereference pointer",
5272 expr = build_unary_op (ADDR_EXPR, expr, 0);
5273 if (expr != error_mark_node)
5274 expr = build_reinterpret_cast_1
5275 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
5277 if (expr != error_mark_node)
5278 expr = build_indirect_ref (expr, 0);
5282 /* As a G++ extension, we consider conversions from member
5283 functions, and pointers to member functions to
5284 pointer-to-function and pointer-to-void types. If
5285 -Wno-pmf-conversions has not been specified,
5286 convert_member_func_to_ptr will issue an error message. */
5287 if ((TYPE_PTRMEMFUNC_P (intype)
5288 || TREE_CODE (intype) == METHOD_TYPE)
5289 && TYPE_PTR_P (type)
5290 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5291 || VOID_TYPE_P (TREE_TYPE (type))))
5292 return convert_member_func_to_ptr (type, expr);
5294 /* If the cast is not to a reference type, the lvalue-to-rvalue,
5295 array-to-pointer, and function-to-pointer conversions are
5297 expr = decay_conversion (expr);
5299 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5300 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5301 if (TREE_CODE (expr) == NOP_EXPR
5302 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5303 expr = TREE_OPERAND (expr, 0);
5305 if (error_operand_p (expr))
5306 return error_mark_node;
5308 intype = TREE_TYPE (expr);
5310 /* [expr.reinterpret.cast]
5311 A pointer can be converted to any integral type large enough to
5313 if (CP_INTEGRAL_TYPE_P (type) && TYPE_PTR_P (intype))
5315 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5316 pedwarn ("cast from %qT to %qT loses precision",
5319 /* [expr.reinterpret.cast]
5320 A value of integral or enumeration type can be explicitly
5321 converted to a pointer. */
5322 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
5325 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5326 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5327 return fold_if_not_in_template (build_nop (type, expr));
5328 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5329 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5334 check_for_casting_away_constness (intype, type, REINTERPRET_CAST_EXPR);
5335 /* Warn about possible alignment problems. */
5336 if (STRICT_ALIGNMENT && warn_cast_align
5337 && !VOID_TYPE_P (type)
5338 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
5339 && COMPLETE_TYPE_P (TREE_TYPE (type))
5340 && COMPLETE_TYPE_P (TREE_TYPE (intype))
5341 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
5342 warning (OPT_Wcast_align, "cast from %qT to %qT "
5343 "increases required alignment of target type", intype, type);
5345 /* We need to strip nops here, because the front end likes to
5346 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
5348 if (warn_strict_aliasing <= 2)
5349 strict_aliasing_warning (intype, type, sexpr);
5351 return fold_if_not_in_template (build_nop (type, expr));
5353 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5354 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5357 /* Only issue a warning, as we have always supported this
5358 where possible, and it is necessary in some cases. DR 195
5359 addresses this issue, but as of 2004/10/26 is still in
5361 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5362 return fold_if_not_in_template (build_nop (type, expr));
5364 else if (TREE_CODE (type) == VECTOR_TYPE)
5365 return fold_if_not_in_template (convert_to_vector (type, expr));
5366 else if (TREE_CODE (intype) == VECTOR_TYPE && INTEGRAL_TYPE_P (type))
5367 return fold_if_not_in_template (convert_to_integer (type, expr));
5372 error ("invalid cast from type %qT to type %qT", intype, type);
5373 return error_mark_node;
5376 return cp_convert (type, expr);
5380 build_reinterpret_cast (tree type, tree expr)
5382 if (type == error_mark_node || expr == error_mark_node)
5383 return error_mark_node;
5385 if (processing_template_decl)
5387 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5389 if (!TREE_SIDE_EFFECTS (t)
5390 && type_dependent_expression_p (expr))
5391 /* There might turn out to be side effects inside expr. */
5392 TREE_SIDE_EFFECTS (t) = 1;
5393 return convert_from_reference (t);
5396 return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
5400 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
5401 return an appropriate expression. Otherwise, return
5402 error_mark_node. If the cast is not valid, and COMPLAIN is true,
5403 then a diagnostic will be issued. If VALID_P is non-NULL, we are
5404 performing a C-style cast, its value upon return will indicate
5405 whether or not the conversion succeeded. */
5408 build_const_cast_1 (tree dst_type, tree expr, bool complain,
5412 tree reference_type;
5414 /* Callers are responsible for handling error_mark_node as a
5415 destination type. */
5416 gcc_assert (dst_type != error_mark_node);
5417 /* In a template, callers should be building syntactic
5418 representations of casts, not using this machinery. */
5419 gcc_assert (!processing_template_decl);
5421 /* Assume the conversion is invalid. */
5425 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type))
5428 error ("invalid use of const_cast with type %qT, "
5429 "which is not a pointer, "
5430 "reference, nor a pointer-to-data-member type", dst_type);
5431 return error_mark_node;
5434 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
5437 error ("invalid use of const_cast with type %qT, which is a pointer "
5438 "or reference to a function type", dst_type);
5439 return error_mark_node;
5442 /* Save casted types in the function's used types hash table. */
5443 used_types_insert (dst_type);
5445 src_type = TREE_TYPE (expr);
5446 /* Expressions do not really have reference types. */
5447 if (TREE_CODE (src_type) == REFERENCE_TYPE)
5448 src_type = TREE_TYPE (src_type);
5450 /* [expr.const.cast]
5452 An lvalue of type T1 can be explicitly converted to an lvalue of
5453 type T2 using the cast const_cast<T2&> (where T1 and T2 are object
5454 types) if a pointer to T1 can be explicitly converted to the type
5455 pointer to T2 using a const_cast. */
5456 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
5458 reference_type = dst_type;
5459 if (! real_lvalue_p (expr))
5462 error ("invalid const_cast of an rvalue of type %qT to type %qT",
5463 src_type, dst_type);
5464 return error_mark_node;
5466 dst_type = build_pointer_type (TREE_TYPE (dst_type));
5467 src_type = build_pointer_type (src_type);
5471 reference_type = NULL_TREE;
5472 /* If the destination type is not a reference type, the
5473 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
5474 conversions are performed. */
5475 src_type = type_decays_to (src_type);
5476 if (src_type == error_mark_node)
5477 return error_mark_node;
5480 if ((TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
5481 && comp_ptr_ttypes_const (dst_type, src_type))
5486 /* This cast is actually a C-style cast. Issue a warning if
5487 the user is making a potentially unsafe cast. */
5488 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR);
5492 expr = build_unary_op (ADDR_EXPR, expr, 0);
5493 expr = build_nop (reference_type, expr);
5494 return convert_from_reference (expr);
5498 expr = decay_conversion (expr);
5499 /* build_c_cast puts on a NOP_EXPR to make the result not an
5500 lvalue. Strip such NOP_EXPRs if VALUE is being used in
5501 non-lvalue context. */
5502 if (TREE_CODE (expr) == NOP_EXPR
5503 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5504 expr = TREE_OPERAND (expr, 0);
5505 return build_nop (dst_type, expr);
5510 error ("invalid const_cast from type %qT to type %qT",
5511 src_type, dst_type);
5512 return error_mark_node;
5516 build_const_cast (tree type, tree expr)
5518 if (type == error_mark_node || error_operand_p (expr))
5519 return error_mark_node;
5521 if (processing_template_decl)
5523 tree t = build_min (CONST_CAST_EXPR, type, expr);
5525 if (!TREE_SIDE_EFFECTS (t)
5526 && type_dependent_expression_p (expr))
5527 /* There might turn out to be side effects inside expr. */
5528 TREE_SIDE_EFFECTS (t) = 1;
5529 return convert_from_reference (t);
5532 return build_const_cast_1 (type, expr, /*complain=*/true,
5536 /* Build an expression representing an explicit C-style cast to type
5537 TYPE of expression EXPR. */
5540 build_c_cast (tree type, tree expr)
5546 if (type == error_mark_node || error_operand_p (expr))
5547 return error_mark_node;
5549 if (processing_template_decl)
5551 tree t = build_min (CAST_EXPR, type,
5552 tree_cons (NULL_TREE, value, NULL_TREE));
5553 /* We don't know if it will or will not have side effects. */
5554 TREE_SIDE_EFFECTS (t) = 1;
5555 return convert_from_reference (t);
5558 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
5559 'Class') should always be retained, because this information aids
5560 in method lookup. */
5561 if (objc_is_object_ptr (type)
5562 && objc_is_object_ptr (TREE_TYPE (expr)))
5563 return build_nop (type, expr);
5565 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5566 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5567 if (TREE_CODE (type) != REFERENCE_TYPE
5568 && TREE_CODE (value) == NOP_EXPR
5569 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5570 value = TREE_OPERAND (value, 0);
5572 if (TREE_CODE (type) == ARRAY_TYPE)
5574 /* Allow casting from T1* to T2[] because Cfront allows it.
5575 NIHCL uses it. It is not valid ISO C++ however. */
5576 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5578 pedwarn ("ISO C++ forbids casting to an array type %qT", type);
5579 type = build_pointer_type (TREE_TYPE (type));
5583 error ("ISO C++ forbids casting to an array type %qT", type);
5584 return error_mark_node;
5588 if (TREE_CODE (type) == FUNCTION_TYPE
5589 || TREE_CODE (type) == METHOD_TYPE)
5591 error ("invalid cast to function type %qT", type);
5592 return error_mark_node;
5595 /* A C-style cast can be a const_cast. */
5596 result = build_const_cast_1 (type, value, /*complain=*/false,
5601 /* Or a static cast. */
5602 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
5604 /* Or a reinterpret_cast. */
5606 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
5608 /* The static_cast or reinterpret_cast may be followed by a
5611 /* A valid cast may result in errors if, for example, a
5612 conversion to am ambiguous base class is required. */
5613 && !error_operand_p (result))
5617 /* Non-class rvalues always have cv-unqualified type. */
5618 if (!CLASS_TYPE_P (type))
5619 type = TYPE_MAIN_VARIANT (type);
5620 result_type = TREE_TYPE (result);
5621 if (!CLASS_TYPE_P (result_type))
5622 result_type = TYPE_MAIN_VARIANT (result_type);
5623 /* If the type of RESULT does not match TYPE, perform a
5624 const_cast to make it match. If the static_cast or
5625 reinterpret_cast succeeded, we will differ by at most
5626 cv-qualification, so the follow-on const_cast is guaranteed
5628 if (!same_type_p (non_reference (type), non_reference (result_type)))
5630 result = build_const_cast_1 (type, result, false, &valid_p);
5631 gcc_assert (valid_p);
5636 return error_mark_node;
5639 /* Build an assignment expression of lvalue LHS from value RHS.
5640 MODIFYCODE is the code for a binary operator that we use
5641 to combine the old value of LHS with RHS to get the new value.
5642 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5644 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5647 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5651 tree lhstype = TREE_TYPE (lhs);
5652 tree olhstype = lhstype;
5653 tree olhs = NULL_TREE;
5654 bool plain_assign = (modifycode == NOP_EXPR);
5656 /* Avoid duplicate error messages from operands that had errors. */
5657 if (error_operand_p (lhs) || error_operand_p (rhs))
5658 return error_mark_node;
5660 /* Handle control structure constructs used as "lvalues". */
5661 switch (TREE_CODE (lhs))
5663 /* Handle --foo = 5; as these are valid constructs in C++. */
5664 case PREDECREMENT_EXPR:
5665 case PREINCREMENT_EXPR:
5666 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5667 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
5668 stabilize_reference (TREE_OPERAND (lhs, 0)),
5669 TREE_OPERAND (lhs, 1));
5670 return build2 (COMPOUND_EXPR, lhstype,
5672 build_modify_expr (TREE_OPERAND (lhs, 0),
5675 /* Handle (a, b) used as an "lvalue". */
5677 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5679 if (newrhs == error_mark_node)
5680 return error_mark_node;
5681 return build2 (COMPOUND_EXPR, lhstype,
5682 TREE_OPERAND (lhs, 0), newrhs);
5685 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5686 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
5687 stabilize_reference (TREE_OPERAND (lhs, 0)),
5688 TREE_OPERAND (lhs, 1));
5689 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5690 if (newrhs == error_mark_node)
5691 return error_mark_node;
5692 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
5696 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
5697 when neither operand has side-effects. */
5698 if (!lvalue_or_else (lhs, lv_assign))
5699 return error_mark_node;
5701 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
5702 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
5704 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
5705 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
5707 TREE_OPERAND (lhs, 0),
5708 TREE_OPERAND (lhs, 1)),
5709 TREE_OPERAND (lhs, 0),
5710 TREE_OPERAND (lhs, 1));
5713 /* Handle (a ? b : c) used as an "lvalue". */
5716 /* Produce (a ? (b = rhs) : (c = rhs))
5717 except that the RHS goes through a save-expr
5718 so the code to compute it is only emitted once. */
5720 tree preeval = NULL_TREE;
5722 if (VOID_TYPE_P (TREE_TYPE (rhs)))
5724 error ("void value not ignored as it ought to be");
5725 return error_mark_node;
5728 rhs = stabilize_expr (rhs, &preeval);
5730 /* Check this here to avoid odd errors when trying to convert
5731 a throw to the type of the COND_EXPR. */
5732 if (!lvalue_or_else (lhs, lv_assign))
5733 return error_mark_node;
5735 cond = build_conditional_expr
5736 (TREE_OPERAND (lhs, 0),
5737 build_modify_expr (TREE_OPERAND (lhs, 1),
5739 build_modify_expr (TREE_OPERAND (lhs, 2),
5742 if (cond == error_mark_node)
5744 /* Make sure the code to compute the rhs comes out
5745 before the split. */
5747 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
5755 if (modifycode == INIT_EXPR)
5757 if (TREE_CODE (rhs) == CONSTRUCTOR)
5759 if (! same_type_p (TREE_TYPE (rhs), lhstype))
5760 /* Call convert to generate an error; see PR 11063. */
5761 rhs = convert (lhstype, rhs);
5762 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
5763 TREE_SIDE_EFFECTS (result) = 1;
5766 else if (! IS_AGGR_TYPE (lhstype))
5767 /* Do the default thing. */;
5770 result = build_special_member_call (lhs, complete_ctor_identifier,
5771 build_tree_list (NULL_TREE, rhs),
5772 lhstype, LOOKUP_NORMAL);
5773 if (result == NULL_TREE)
5774 return error_mark_node;
5780 lhs = require_complete_type (lhs);
5781 if (lhs == error_mark_node)
5782 return error_mark_node;
5784 if (modifycode == NOP_EXPR)
5786 /* `operator=' is not an inheritable operator. */
5787 if (! IS_AGGR_TYPE (lhstype))
5788 /* Do the default thing. */;
5791 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
5792 lhs, rhs, make_node (NOP_EXPR),
5793 /*overloaded_p=*/NULL);
5794 if (result == NULL_TREE)
5795 return error_mark_node;
5802 /* A binary op has been requested. Combine the old LHS
5803 value with the RHS producing the value we should actually
5804 store into the LHS. */
5806 gcc_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE));
5807 lhs = stabilize_reference (lhs);
5808 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5809 if (newrhs == error_mark_node)
5811 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
5812 TREE_TYPE (lhs), TREE_TYPE (rhs));
5813 return error_mark_node;
5816 /* Now it looks like a plain assignment. */
5817 modifycode = NOP_EXPR;
5819 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
5820 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
5823 /* The left-hand side must be an lvalue. */
5824 if (!lvalue_or_else (lhs, lv_assign))
5825 return error_mark_node;
5827 /* Warn about modifying something that is `const'. Don't warn if
5828 this is initialization. */
5829 if (modifycode != INIT_EXPR
5830 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5831 /* Functions are not modifiable, even though they are
5833 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5834 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5835 /* If it's an aggregate and any field is const, then it is
5836 effectively const. */
5837 || (CLASS_TYPE_P (lhstype)
5838 && C_TYPE_FIELDS_READONLY (lhstype))))
5839 readonly_error (lhs, "assignment");
5841 /* If storing into a structure or union member, it has probably been
5842 given type `int'. Compute the type that would go with the actual
5843 amount of storage the member occupies. */
5845 if (TREE_CODE (lhs) == COMPONENT_REF
5846 && (TREE_CODE (lhstype) == INTEGER_TYPE
5847 || TREE_CODE (lhstype) == REAL_TYPE
5848 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5850 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5852 /* If storing in a field that is in actuality a short or narrower
5853 than one, we must store in the field in its actual type. */
5855 if (lhstype != TREE_TYPE (lhs))
5857 /* Avoid warnings converting integral types back into enums for
5859 if (TREE_CODE (lhstype) == INTEGER_TYPE
5860 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5862 if (TREE_SIDE_EFFECTS (lhs))
5863 lhs = stabilize_reference (lhs);
5866 lhs = copy_node (lhs);
5867 TREE_TYPE (lhs) = lhstype;
5871 /* Convert new value to destination type. */
5873 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5877 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5878 TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
5880 error ("incompatible types in assignment of %qT to %qT",
5881 TREE_TYPE (rhs), lhstype);
5882 return error_mark_node;
5885 /* Allow array assignment in compiler-generated code. */
5886 if (! DECL_ARTIFICIAL (current_function_decl))
5888 /* This routine is used for both initialization and assignment.
5889 Make sure the diagnostic message differentiates the context. */
5890 if (modifycode == INIT_EXPR)
5891 error ("array used as initializer");
5893 error ("invalid array assignment");
5894 return error_mark_node;
5897 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5898 ? 1 + (modifycode != INIT_EXPR): 0;
5899 return build_vec_init (lhs, NULL_TREE, newrhs,
5900 /*explicit_default_init_p=*/false,
5904 if (modifycode == INIT_EXPR)
5905 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5906 "initialization", NULL_TREE, 0);
5909 /* Avoid warnings on enum bit fields. */
5910 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5911 && TREE_CODE (lhstype) == INTEGER_TYPE)
5913 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5915 newrhs = convert_force (lhstype, newrhs, 0);
5918 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5920 if (TREE_CODE (newrhs) == CALL_EXPR
5921 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5922 newrhs = build_cplus_new (lhstype, newrhs);
5924 /* Can't initialize directly from a TARGET_EXPR, since that would
5925 cause the lhs to be constructed twice, and possibly result in
5926 accidental self-initialization. So we force the TARGET_EXPR to be
5927 expanded without a target. */
5928 if (TREE_CODE (newrhs) == TARGET_EXPR)
5929 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5930 TREE_OPERAND (newrhs, 0));
5933 if (newrhs == error_mark_node)
5934 return error_mark_node;
5936 if (c_dialect_objc () && flag_objc_gc)
5938 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5944 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5945 lhstype, lhs, newrhs);
5947 TREE_SIDE_EFFECTS (result) = 1;
5949 TREE_NO_WARNING (result) = 1;
5951 /* If we got the LHS in a different type for storing in,
5952 convert the result back to the nominal type of LHS
5953 so that the value we return always has the same type
5954 as the LHS argument. */
5956 if (olhstype == TREE_TYPE (result))
5960 result = build2 (COMPOUND_EXPR, olhstype, result, olhs);
5961 TREE_NO_WARNING (result) = 1;
5964 return convert_for_assignment (olhstype, result, "assignment",
5969 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5971 if (processing_template_decl)
5972 return build_min_nt (MODOP_EXPR, lhs,
5973 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5975 if (modifycode != NOP_EXPR)
5977 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5978 make_node (modifycode),
5979 /*overloaded_p=*/NULL);
5982 TREE_NO_WARNING (rval) = 1;
5986 return build_modify_expr (lhs, modifycode, rhs);
5989 /* Helper function for get_delta_difference which assumes FROM is a base
5990 class of TO. Returns a delta for the conversion of pointer-to-member
5991 of FROM to pointer-to-member of TO. If the conversion is invalid,
5992 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
5993 If C_CAST_P is true, this conversion is taking place as part of a C-style
5997 get_delta_difference_1 (tree from, tree to, bool c_cast_p)
6002 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, &kind);
6003 if (kind == bk_inaccessible || kind == bk_ambig)
6005 error (" in pointer to member function conversion");
6006 return size_zero_node;
6010 if (kind != bk_via_virtual)
6011 return BINFO_OFFSET (binfo);
6013 /* FROM is a virtual base class of TO. Issue an error or warning
6014 depending on whether or not this is a reinterpret cast. */
6016 error ("pointer to member conversion via virtual base %qT",
6017 BINFO_TYPE (binfo_from_vbase (binfo)));
6019 return size_zero_node;
6026 /* Get difference in deltas for different pointer to member function
6027 types. Returns an integer constant of type PTRDIFF_TYPE_NODE. If
6028 the conversion is invalid, the constant is zero. If
6029 ALLOW_INVERSE_P is true, then allow reverse conversions as well.
6030 If C_CAST_P is true this conversion is taking place as part of a
6033 Note that the naming of FROM and TO is kind of backwards; the return
6034 value is what we add to a TO in order to get a FROM. They are named
6035 this way because we call this function to find out how to convert from
6036 a pointer to member of FROM to a pointer to member of TO. */
6039 get_delta_difference (tree from, tree to,
6040 bool allow_inverse_p,
6045 if (same_type_ignoring_top_level_qualifiers_p (from, to))
6046 /* Pointer to member of incomplete class is permitted*/
6047 result = size_zero_node;
6049 result = get_delta_difference_1 (from, to, c_cast_p);
6053 if (!allow_inverse_p)
6055 error_not_base_type (from, to);
6056 error (" in pointer to member conversion");
6057 result = size_zero_node;
6061 result = get_delta_difference_1 (to, from, c_cast_p);
6064 result = size_diffop (size_zero_node, result);
6067 error_not_base_type (from, to);
6068 error (" in pointer to member conversion");
6069 result = size_zero_node;
6074 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
6078 /* Return a constructor for the pointer-to-member-function TYPE using
6079 the other components as specified. */
6082 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
6087 VEC(constructor_elt, gc) *v;
6089 /* Pull the FIELD_DECLs out of the type. */
6090 pfn_field = TYPE_FIELDS (type);
6091 delta_field = TREE_CHAIN (pfn_field);
6093 /* Make sure DELTA has the type we want. */
6094 delta = convert_and_check (delta_type_node, delta);
6096 /* Convert to the correct target type if necessary. */
6097 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
6099 /* Finish creating the initializer. */
6100 v = VEC_alloc(constructor_elt, gc, 2);
6101 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
6102 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
6103 u = build_constructor (type, v);
6104 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
6105 TREE_INVARIANT (u) = TREE_INVARIANT (pfn) & TREE_INVARIANT (delta);
6106 TREE_STATIC (u) = (TREE_CONSTANT (u)
6107 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6109 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
6114 /* Build a constructor for a pointer to member function. It can be
6115 used to initialize global variables, local variable, or used
6116 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6119 If FORCE is nonzero, then force this conversion, even if
6120 we would rather not do it. Usually set when using an explicit
6121 cast. A C-style cast is being processed iff C_CAST_P is true.
6123 Return error_mark_node, if something goes wrong. */
6126 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p)
6132 if (error_operand_p (pfn))
6133 return error_mark_node;
6135 pfn_type = TREE_TYPE (pfn);
6136 to_type = build_ptrmemfunc_type (type);
6138 /* Handle multiple conversions of pointer to member functions. */
6139 if (TYPE_PTRMEMFUNC_P (pfn_type))
6141 tree delta = NULL_TREE;
6142 tree npfn = NULL_TREE;
6146 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, LOOKUP_NORMAL))
6147 error ("invalid conversion to type %qT from type %qT",
6150 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6151 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6155 /* We don't have to do any conversion to convert a
6156 pointer-to-member to its own type. But, we don't want to
6157 just return a PTRMEM_CST if there's an explicit cast; that
6158 cast should make the expression an invalid template argument. */
6159 if (TREE_CODE (pfn) != PTRMEM_CST)
6161 if (same_type_p (to_type, pfn_type))
6163 else if (integer_zerop (n))
6164 return build_reinterpret_cast (to_type, pfn);
6167 if (TREE_SIDE_EFFECTS (pfn))
6168 pfn = save_expr (pfn);
6170 /* Obtain the function pointer and the current DELTA. */
6171 if (TREE_CODE (pfn) == PTRMEM_CST)
6172 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
6175 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
6176 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
6179 /* Just adjust the DELTA field. */
6180 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6181 (TREE_TYPE (delta), ptrdiff_type_node));
6182 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
6183 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
6184 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
6185 return build_ptrmemfunc1 (to_type, delta, npfn);
6188 /* Handle null pointer to member function conversions. */
6189 if (integer_zerop (pfn))
6191 pfn = build_c_cast (type, integer_zero_node);
6192 return build_ptrmemfunc1 (to_type,
6197 if (type_unknown_p (pfn))
6198 return instantiate_type (type, pfn, tf_warning_or_error);
6200 fn = TREE_OPERAND (pfn, 0);
6201 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6202 /* In a template, we will have preserved the
6204 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
6205 return make_ptrmem_cst (to_type, fn);
6208 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6211 ??? There is no consistency as to the types returned for the above
6212 values. Some code acts as if it were a sizetype and some as if it were
6213 integer_type_node. */
6216 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
6218 tree type = TREE_TYPE (cst);
6219 tree fn = PTRMEM_CST_MEMBER (cst);
6220 tree ptr_class, fn_class;
6222 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6224 /* The class that the function belongs to. */
6225 fn_class = DECL_CONTEXT (fn);
6227 /* The class that we're creating a pointer to member of. */
6228 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6230 /* First, calculate the adjustment to the function's class. */
6231 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
6234 if (!DECL_VIRTUAL_P (fn))
6235 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6238 /* If we're dealing with a virtual function, we have to adjust 'this'
6239 again, to point to the base which provides the vtable entry for
6240 fn; the call will do the opposite adjustment. */
6241 tree orig_class = DECL_CONTEXT (fn);
6242 tree binfo = binfo_or_else (orig_class, fn_class);
6243 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
6244 *delta, BINFO_OFFSET (binfo));
6245 *delta = fold_if_not_in_template (*delta);
6247 /* We set PFN to the vtable offset at which the function can be
6248 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
6249 case delta is shifted left, and then incremented). */
6250 *pfn = DECL_VINDEX (fn);
6251 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
6252 TYPE_SIZE_UNIT (vtable_entry_type));
6253 *pfn = fold_if_not_in_template (*pfn);
6255 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
6257 case ptrmemfunc_vbit_in_pfn:
6258 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
6260 *pfn = fold_if_not_in_template (*pfn);
6263 case ptrmemfunc_vbit_in_delta:
6264 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
6265 *delta, integer_one_node);
6266 *delta = fold_if_not_in_template (*delta);
6267 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
6268 *delta, integer_one_node);
6269 *delta = fold_if_not_in_template (*delta);
6276 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
6277 *pfn = fold_if_not_in_template (*pfn);
6281 /* Return an expression for PFN from the pointer-to-member function
6285 pfn_from_ptrmemfunc (tree t)
6287 if (TREE_CODE (t) == PTRMEM_CST)
6292 expand_ptrmemfunc_cst (t, &delta, &pfn);
6297 return build_ptrmemfunc_access_expr (t, pfn_identifier);
6300 /* Return an expression for DELTA from the pointer-to-member function
6304 delta_from_ptrmemfunc (tree t)
6306 if (TREE_CODE (t) == PTRMEM_CST)
6311 expand_ptrmemfunc_cst (t, &delta, &pfn);
6316 return build_ptrmemfunc_access_expr (t, delta_identifier);
6319 /* Convert value RHS to type TYPE as preparation for an assignment to
6320 an lvalue of type TYPE. ERRTYPE is a string to use in error
6321 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6322 are doing the conversion in order to pass the PARMNUMth argument of
6326 convert_for_assignment (tree type, tree rhs,
6327 const char *errtype, tree fndecl, int parmnum)
6330 enum tree_code coder;
6332 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6333 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6334 rhs = TREE_OPERAND (rhs, 0);
6336 rhstype = TREE_TYPE (rhs);
6337 coder = TREE_CODE (rhstype);
6339 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
6340 && vector_types_convertible_p (type, rhstype, true))
6341 return convert (type, rhs);
6343 if (rhs == error_mark_node || rhstype == error_mark_node)
6344 return error_mark_node;
6345 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6346 return error_mark_node;
6348 /* The RHS of an assignment cannot have void type. */
6349 if (coder == VOID_TYPE)
6351 error ("void value not ignored as it ought to be");
6352 return error_mark_node;
6355 /* Simplify the RHS if possible. */
6356 if (TREE_CODE (rhs) == CONST_DECL)
6357 rhs = DECL_INITIAL (rhs);
6359 if (c_dialect_objc ())
6362 tree rname = fndecl;
6364 if (!strcmp (errtype, "assignment"))
6366 else if (!strcmp (errtype, "initialization"))
6370 tree selector = objc_message_selector ();
6374 if (selector && parmno > 1)
6381 if (objc_compare_types (type, rhstype, parmno, rname))
6382 return convert (type, rhs);
6387 The expression is implicitly converted (clause _conv_) to the
6388 cv-unqualified type of the left operand.
6390 We allow bad conversions here because by the time we get to this point
6391 we are committed to doing the conversion. If we end up doing a bad
6392 conversion, convert_like will complain. */
6393 if (!can_convert_arg_bad (type, rhstype, rhs))
6395 /* When -Wno-pmf-conversions is use, we just silently allow
6396 conversions from pointers-to-members to plain pointers. If
6397 the conversion doesn't work, cp_convert will complain. */
6399 && TYPE_PTR_P (type)
6400 && TYPE_PTRMEMFUNC_P (rhstype))
6401 rhs = cp_convert (strip_top_quals (type), rhs);
6404 /* If the right-hand side has unknown type, then it is an
6405 overloaded function. Call instantiate_type to get error
6407 if (rhstype == unknown_type_node)
6408 instantiate_type (type, rhs, tf_warning_or_error);
6410 error ("cannot convert %qT to %qT for argument %qP to %qD",
6411 rhstype, type, parmnum, fndecl);
6413 error ("cannot convert %qT to %qT in %s", rhstype, type, errtype);
6414 return error_mark_node;
6417 if (warn_missing_format_attribute)
6419 const enum tree_code codel = TREE_CODE (type);
6420 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6422 && check_missing_format_attribute (type, rhstype))
6423 warning (OPT_Wmissing_format_attribute,
6424 "%s might be a candidate for a format attribute",
6428 /* If -Wparentheses, warn about a = b = c when a has type bool and b
6430 if (warn_parentheses
6431 && type == boolean_type_node
6432 && TREE_CODE (rhs) == MODIFY_EXPR
6433 && !TREE_NO_WARNING (rhs)
6434 && TREE_TYPE (rhs) != boolean_type_node)
6436 warning (OPT_Wparentheses,
6437 "suggest parentheses around assignment used as truth value");
6438 TREE_NO_WARNING (rhs) = 1;
6441 return perform_implicit_conversion (strip_top_quals (type), rhs);
6444 /* Convert RHS to be of type TYPE.
6445 If EXP is nonzero, it is the target of the initialization.
6446 ERRTYPE is a string to use in error messages.
6448 Two major differences between the behavior of
6449 `convert_for_assignment' and `convert_for_initialization'
6450 are that references are bashed in the former, while
6451 copied in the latter, and aggregates are assigned in
6452 the former (operator=) while initialized in the
6455 If using constructor make sure no conversion operator exists, if one does
6456 exist, an ambiguity exists.
6458 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6461 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
6462 const char *errtype, tree fndecl, int parmnum)
6464 enum tree_code codel = TREE_CODE (type);
6466 enum tree_code coder;
6468 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6469 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6470 if (TREE_CODE (rhs) == NOP_EXPR
6471 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6472 && codel != REFERENCE_TYPE)
6473 rhs = TREE_OPERAND (rhs, 0);
6475 if (type == error_mark_node
6476 || rhs == error_mark_node
6477 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6478 return error_mark_node;
6480 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6481 && TREE_CODE (type) != ARRAY_TYPE
6482 && (TREE_CODE (type) != REFERENCE_TYPE
6483 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6484 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6485 && (TREE_CODE (type) != REFERENCE_TYPE
6486 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6487 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6488 rhs = decay_conversion (rhs);
6490 rhstype = TREE_TYPE (rhs);
6491 coder = TREE_CODE (rhstype);
6493 if (coder == ERROR_MARK)
6494 return error_mark_node;
6496 /* We accept references to incomplete types, so we can
6497 return here before checking if RHS is of complete type. */
6499 if (codel == REFERENCE_TYPE)
6501 /* This should eventually happen in convert_arguments. */
6502 int savew = 0, savee = 0;
6505 savew = warningcount, savee = errorcount;
6506 rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE,
6510 if (warningcount > savew)
6511 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
6512 else if (errorcount > savee)
6513 error ("in passing argument %P of %q+D", parmnum, fndecl);
6519 exp = require_complete_type (exp);
6520 if (exp == error_mark_node)
6521 return error_mark_node;
6523 rhstype = non_reference (rhstype);
6525 type = complete_type (type);
6527 if (IS_AGGR_TYPE (type))
6528 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6530 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6533 /* If RETVAL is the address of, or a reference to, a local variable or
6534 temporary give an appropriate warning. */
6537 maybe_warn_about_returning_address_of_local (tree retval)
6539 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6540 tree whats_returned = retval;
6544 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6545 whats_returned = TREE_OPERAND (whats_returned, 1);
6546 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6547 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6548 || TREE_CODE (whats_returned) == NOP_EXPR)
6549 whats_returned = TREE_OPERAND (whats_returned, 0);
6554 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6556 whats_returned = TREE_OPERAND (whats_returned, 0);
6558 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6560 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6561 || TREE_CODE (whats_returned) == TARGET_EXPR)
6563 warning (0, "returning reference to temporary");
6566 if (TREE_CODE (whats_returned) == VAR_DECL
6567 && DECL_NAME (whats_returned)
6568 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6570 warning (0, "reference to non-lvalue returned");
6575 while (TREE_CODE (whats_returned) == COMPONENT_REF
6576 || TREE_CODE (whats_returned) == ARRAY_REF)
6577 whats_returned = TREE_OPERAND (whats_returned, 0);
6579 if (DECL_P (whats_returned)
6580 && DECL_NAME (whats_returned)
6581 && DECL_FUNCTION_SCOPE_P (whats_returned)
6582 && !(TREE_STATIC (whats_returned)
6583 || TREE_PUBLIC (whats_returned)))
6585 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6586 warning (0, "reference to local variable %q+D returned",
6589 warning (0, "address of local variable %q+D returned",
6595 /* Check that returning RETVAL from the current function is valid.
6596 Return an expression explicitly showing all conversions required to
6597 change RETVAL into the function return type, and to assign it to
6598 the DECL_RESULT for the function. Set *NO_WARNING to true if
6599 code reaches end of non-void function warning shouldn't be issued
6600 on this RETURN_EXPR. */
6603 check_return_expr (tree retval, bool *no_warning)
6606 /* The type actually returned by the function, after any
6609 int fn_returns_value_p;
6610 bool named_return_value_okay_p;
6612 *no_warning = false;
6614 /* A `volatile' function is one that isn't supposed to return, ever.
6615 (This is a G++ extension, used to get better code for functions
6616 that call the `volatile' function.) */
6617 if (TREE_THIS_VOLATILE (current_function_decl))
6618 warning (0, "function declared %<noreturn%> has a %<return%> statement");
6620 /* Check for various simple errors. */
6621 if (DECL_DESTRUCTOR_P (current_function_decl))
6624 error ("returning a value from a destructor");
6627 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6629 if (in_function_try_handler)
6630 /* If a return statement appears in a handler of the
6631 function-try-block of a constructor, the program is ill-formed. */
6632 error ("cannot return from a handler of a function-try-block of a constructor");
6634 /* You can't return a value from a constructor. */
6635 error ("returning a value from a constructor");
6639 if (processing_template_decl)
6641 current_function_returns_value = 1;
6642 if (check_for_bare_parameter_packs (retval))
6643 retval = error_mark_node;
6647 /* When no explicit return-value is given in a function with a named
6648 return value, the named return value is used. */
6649 result = DECL_RESULT (current_function_decl);
6650 valtype = TREE_TYPE (result);
6651 gcc_assert (valtype != NULL_TREE);
6652 fn_returns_value_p = !VOID_TYPE_P (valtype);
6653 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6656 /* Check for a return statement with no return value in a function
6657 that's supposed to return a value. */
6658 if (!retval && fn_returns_value_p)
6660 pedwarn ("return-statement with no value, in function returning %qT",
6662 /* Clear this, so finish_function won't say that we reach the
6663 end of a non-void function (which we don't, we gave a
6665 current_function_returns_null = 0;
6666 /* And signal caller that TREE_NO_WARNING should be set on the
6667 RETURN_EXPR to avoid control reaches end of non-void function
6668 warnings in tree-cfg.c. */
6671 /* Check for a return statement with a value in a function that
6672 isn't supposed to return a value. */
6673 else if (retval && !fn_returns_value_p)
6675 if (VOID_TYPE_P (TREE_TYPE (retval)))
6676 /* You can return a `void' value from a function of `void'
6677 type. In that case, we have to evaluate the expression for
6678 its side-effects. */
6679 finish_expr_stmt (retval);
6681 pedwarn ("return-statement with a value, in function "
6682 "returning 'void'");
6684 current_function_returns_null = 1;
6686 /* There's really no value to return, after all. */
6690 /* Remember that this function can sometimes return without a
6692 current_function_returns_null = 1;
6694 /* Remember that this function did return a value. */
6695 current_function_returns_value = 1;
6697 /* Check for erroneous operands -- but after giving ourselves a
6698 chance to provide an error about returning a value from a void
6700 if (error_operand_p (retval))
6702 current_function_return_value = error_mark_node;
6703 return error_mark_node;
6706 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6707 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6708 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6709 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6711 && retval && null_ptr_cst_p (retval))
6712 warning (0, "%<operator new%> must not return NULL unless it is "
6713 "declared %<throw()%> (or -fcheck-new is in effect)");
6715 /* Effective C++ rule 15. See also start_function. */
6717 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
6721 /* The function return type must be a reference to the current
6723 if (TREE_CODE (valtype) == REFERENCE_TYPE
6724 && same_type_ignoring_top_level_qualifiers_p
6725 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
6727 /* Returning '*this' is obviously OK. */
6728 if (retval == current_class_ref)
6730 /* If we are calling a function whose return type is the same of
6731 the current class reference, it is ok. */
6732 else if (TREE_CODE (retval) == INDIRECT_REF
6733 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
6738 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
6741 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6743 [...] For a function with a class return type, if the expression
6744 in the return statement is the name of a local object, and the cv-
6745 unqualified type of the local object is the same as the function
6746 return type, an implementation is permitted to omit creating the tem-
6747 porary object to hold the function return value [...]
6749 So, if this is a value-returning function that always returns the same
6750 local variable, remember it.
6752 It might be nice to be more flexible, and choose the first suitable
6753 variable even if the function sometimes returns something else, but
6754 then we run the risk of clobbering the variable we chose if the other
6755 returned expression uses the chosen variable somehow. And people expect
6756 this restriction, anyway. (jason 2000-11-19)
6758 See finish_function and finalize_nrv for the rest of this optimization. */
6760 named_return_value_okay_p =
6761 (retval != NULL_TREE
6762 /* Must be a local, automatic variable. */
6763 && TREE_CODE (retval) == VAR_DECL
6764 && DECL_CONTEXT (retval) == current_function_decl
6765 && ! TREE_STATIC (retval)
6766 && ! DECL_ANON_UNION_VAR_P (retval)
6767 && (DECL_ALIGN (retval)
6768 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6769 /* The cv-unqualified type of the returned value must be the
6770 same as the cv-unqualified return type of the
6772 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
6774 (TREE_TYPE (TREE_TYPE (current_function_decl)))))
6775 /* And the returned value must be non-volatile. */
6776 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
6778 if (fn_returns_value_p && flag_elide_constructors)
6780 if (named_return_value_okay_p
6781 && (current_function_return_value == NULL_TREE
6782 || current_function_return_value == retval))
6783 current_function_return_value = retval;
6785 current_function_return_value = error_mark_node;
6788 /* We don't need to do any conversions when there's nothing being
6793 /* Do any required conversions. */
6794 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6795 /* No conversions are required. */
6799 /* The type the function is declared to return. */
6800 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6801 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
6803 /* The functype's return type will have been set to void, if it
6804 was an incomplete type. Just treat this as 'return;' */
6805 if (VOID_TYPE_P (functype))
6806 return error_mark_node;
6808 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
6809 treated as an rvalue for the purposes of overload resolution to
6810 favor move constructors over copy constructors. */
6811 if ((cxx_dialect != cxx98)
6812 && named_return_value_okay_p
6813 /* The variable must not have the `volatile' qualifier. */
6814 && !(cp_type_quals (TREE_TYPE (retval)) & TYPE_QUAL_VOLATILE)
6815 /* The return type must be a class type. */
6816 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
6817 flags = flags | LOOKUP_PREFER_RVALUE;
6819 /* First convert the value to the function's return type, then
6820 to the type of return value's location to handle the
6821 case that functype is smaller than the valtype. */
6822 retval = convert_for_initialization
6823 (NULL_TREE, functype, retval, flags, "return", NULL_TREE, 0);
6824 retval = convert (valtype, retval);
6826 /* If the conversion failed, treat this just like `return;'. */
6827 if (retval == error_mark_node)
6829 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6830 else if (! current_function_returns_struct
6831 && TREE_CODE (retval) == TARGET_EXPR
6832 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6833 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6834 TREE_OPERAND (retval, 0));
6836 maybe_warn_about_returning_address_of_local (retval);
6839 /* Actually copy the value returned into the appropriate location. */
6840 if (retval && retval != result)
6841 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
6847 /* Returns nonzero if the pointer-type FROM can be converted to the
6848 pointer-type TO via a qualification conversion. If CONSTP is -1,
6849 then we return nonzero if the pointers are similar, and the
6850 cv-qualification signature of FROM is a proper subset of that of TO.
6852 If CONSTP is positive, then all outer pointers have been
6856 comp_ptr_ttypes_real (tree to, tree from, int constp)
6858 bool to_more_cv_qualified = false;
6860 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6862 if (TREE_CODE (to) != TREE_CODE (from))
6865 if (TREE_CODE (from) == OFFSET_TYPE
6866 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
6867 TYPE_OFFSET_BASETYPE (to)))
6870 /* Const and volatile mean something different for function types,
6871 so the usual checks are not appropriate. */
6872 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6874 /* In Objective-C++, some types may have been 'volatilized' by
6875 the compiler for EH; when comparing them here, the volatile
6876 qualification must be ignored. */
6877 bool objc_quals_match = objc_type_quals_match (to, from);
6879 if (!at_least_as_qualified_p (to, from) && !objc_quals_match)
6882 if (!at_least_as_qualified_p (from, to) && !objc_quals_match)
6886 to_more_cv_qualified = true;
6890 constp &= TYPE_READONLY (to);
6893 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
6894 return ((constp >= 0 || to_more_cv_qualified)
6895 && same_type_ignoring_top_level_qualifiers_p (to, from));
6899 /* When comparing, say, char ** to char const **, this function takes
6900 the 'char *' and 'char const *'. Do not pass non-pointer/reference
6901 types to this function. */
6904 comp_ptr_ttypes (tree to, tree from)
6906 return comp_ptr_ttypes_real (to, from, 1);
6909 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6910 type or inheritance-related types, regardless of cv-quals. */
6913 ptr_reasonably_similar (const_tree to, const_tree from)
6915 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6917 /* Any target type is similar enough to void. */
6918 if (TREE_CODE (to) == VOID_TYPE
6919 || TREE_CODE (from) == VOID_TYPE)
6922 if (TREE_CODE (to) != TREE_CODE (from))
6925 if (TREE_CODE (from) == OFFSET_TYPE
6926 && cp_comptypes (TYPE_OFFSET_BASETYPE (to),
6927 TYPE_OFFSET_BASETYPE (from),
6928 COMPARE_BASE | COMPARE_DERIVED))
6931 if (TREE_CODE (to) == VECTOR_TYPE
6932 && vector_types_convertible_p (to, from, false))
6935 if (TREE_CODE (to) == INTEGER_TYPE
6936 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6939 if (TREE_CODE (to) == FUNCTION_TYPE)
6942 if (TREE_CODE (to) != POINTER_TYPE)
6944 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6945 COMPARE_BASE | COMPARE_DERIVED);
6949 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
6950 pointer-to-member types) are the same, ignoring cv-qualification at
6954 comp_ptr_ttypes_const (tree to, tree from)
6956 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6958 if (TREE_CODE (to) != TREE_CODE (from))
6961 if (TREE_CODE (from) == OFFSET_TYPE
6962 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6963 TYPE_OFFSET_BASETYPE (to)))
6966 if (TREE_CODE (to) != POINTER_TYPE)
6967 return same_type_ignoring_top_level_qualifiers_p (to, from);
6971 /* Returns the type qualifiers for this type, including the qualifiers on the
6972 elements for an array type. */
6975 cp_type_quals (const_tree type)
6977 /* This CONST_CAST is okay because strip_array_types returns it's
6978 argument unmodified and we assign it to a const_tree. */
6979 type = strip_array_types (CONST_CAST_TREE(type));
6980 if (type == error_mark_node)
6981 return TYPE_UNQUALIFIED;
6982 return TYPE_QUALS (type);
6985 /* Returns nonzero if the TYPE is const from a C++ perspective: look inside
6989 cp_type_readonly (const_tree type)
6991 /* This CONST_CAST is okay because strip_array_types returns it's
6992 argument unmodified and we assign it to a const_tree. */
6993 type = strip_array_types (CONST_CAST_TREE(type));
6994 return TYPE_READONLY (type);
6997 /* Returns nonzero if the TYPE contains a mutable member. */
7000 cp_has_mutable_p (const_tree type)
7002 /* This CONST_CAST is okay because strip_array_types returns it's
7003 argument unmodified and we assign it to a const_tree. */
7004 type = strip_array_types (CONST_CAST_TREE(type));
7006 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7009 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
7010 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
7011 approximation. In particular, consider:
7014 struct S { int i; };
7015 const S s = { f(); }
7017 Here, we will make "s" as TREE_READONLY (because it is declared
7018 "const") -- only to reverse ourselves upon seeing that the
7019 initializer is non-constant. */
7022 cp_apply_type_quals_to_decl (int type_quals, tree decl)
7024 tree type = TREE_TYPE (decl);
7026 if (type == error_mark_node)
7029 if (TREE_CODE (type) == FUNCTION_TYPE
7030 && type_quals != TYPE_UNQUALIFIED)
7032 /* This was an error in C++98 (cv-qualifiers cannot be added to
7033 a function type), but DR 295 makes the code well-formed by
7034 dropping the extra qualifiers. */
7037 tree bad_type = build_qualified_type (type, type_quals);
7038 pedwarn ("ignoring %qV qualifiers added to function type %qT",
7042 TREE_TYPE (decl) = TYPE_MAIN_VARIANT (type);
7046 /* Avoid setting TREE_READONLY incorrectly. */
7047 if (/* If the object has a constructor, the constructor may modify
7049 TYPE_NEEDS_CONSTRUCTING (type)
7050 /* If the type isn't complete, we don't know yet if it will need
7052 || !COMPLETE_TYPE_P (type)
7053 /* If the type has a mutable component, that component might be
7055 || TYPE_HAS_MUTABLE_P (type))
7056 type_quals &= ~TYPE_QUAL_CONST;
7058 c_apply_type_quals_to_decl (type_quals, decl);
7061 /* Subroutine of casts_away_constness. Make T1 and T2 point at
7062 exemplar types such that casting T1 to T2 is casting away constness
7063 if and only if there is no implicit conversion from T1 to T2. */
7066 casts_away_constness_r (tree *t1, tree *t2)
7071 /* [expr.const.cast]
7073 For multi-level pointer to members and multi-level mixed pointers
7074 and pointers to members (conv.qual), the "member" aspect of a
7075 pointer to member level is ignored when determining if a const
7076 cv-qualifier has been cast away. */
7077 /* [expr.const.cast]
7079 For two pointer types:
7081 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
7082 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
7085 casting from X1 to X2 casts away constness if, for a non-pointer
7086 type T there does not exist an implicit conversion (clause
7089 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7093 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
7094 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1))
7095 || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2)))
7097 *t1 = cp_build_qualified_type (void_type_node,
7098 cp_type_quals (*t1));
7099 *t2 = cp_build_qualified_type (void_type_node,
7100 cp_type_quals (*t2));
7104 quals1 = cp_type_quals (*t1);
7105 quals2 = cp_type_quals (*t2);
7107 if (TYPE_PTRMEM_P (*t1))
7108 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
7110 *t1 = TREE_TYPE (*t1);
7111 if (TYPE_PTRMEM_P (*t2))
7112 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
7114 *t2 = TREE_TYPE (*t2);
7116 casts_away_constness_r (t1, t2);
7117 *t1 = build_pointer_type (*t1);
7118 *t2 = build_pointer_type (*t2);
7119 *t1 = cp_build_qualified_type (*t1, quals1);
7120 *t2 = cp_build_qualified_type (*t2, quals2);
7123 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
7127 casts_away_constness (tree t1, tree t2)
7129 if (TREE_CODE (t2) == REFERENCE_TYPE)
7131 /* [expr.const.cast]
7133 Casting from an lvalue of type T1 to an lvalue of type T2
7134 using a reference cast casts away constness if a cast from an
7135 rvalue of type "pointer to T1" to the type "pointer to T2"
7136 casts away constness. */
7137 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
7138 return casts_away_constness (build_pointer_type (t1),
7139 build_pointer_type (TREE_TYPE (t2)));
7142 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7143 /* [expr.const.cast]
7145 Casting from an rvalue of type "pointer to data member of X
7146 of type T1" to the type "pointer to data member of Y of type
7147 T2" casts away constness if a cast from an rvalue of type
7148 "pointer to T1" to the type "pointer to T2" casts away
7150 return casts_away_constness
7151 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
7152 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)));
7154 /* Casting away constness is only something that makes sense for
7155 pointer or reference types. */
7156 if (TREE_CODE (t1) != POINTER_TYPE
7157 || TREE_CODE (t2) != POINTER_TYPE)
7160 /* Top-level qualifiers don't matter. */
7161 t1 = TYPE_MAIN_VARIANT (t1);
7162 t2 = TYPE_MAIN_VARIANT (t2);
7163 casts_away_constness_r (&t1, &t2);
7164 if (!can_convert (t2, t1))
7170 /* If T is a REFERENCE_TYPE return the type to which T refers.
7171 Otherwise, return T itself. */
7174 non_reference (tree t)
7176 if (TREE_CODE (t) == REFERENCE_TYPE)
7182 /* Return nonzero if REF is an lvalue valid for this language;
7183 otherwise, print an error message and return zero. USE says
7184 how the lvalue is being used and so selects the error message. */
7187 lvalue_or_else (const_tree ref, enum lvalue_use use)
7189 int win = lvalue_p (ref);