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 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
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"
46 static tree convert_for_assignment (tree, tree, const char *, tree, int);
47 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
48 static tree rationalize_conditional_expr (enum tree_code, tree);
49 static int comp_ptr_ttypes_real (tree, tree, int);
50 static int comp_ptr_ttypes_const (tree, tree);
51 static bool comp_except_types (tree, tree, bool);
52 static bool comp_array_types (tree, tree, bool);
53 static tree common_base_type (tree, tree);
54 static tree pointer_diff (tree, tree, tree);
55 static tree get_delta_difference (tree, tree, bool, bool);
56 static void casts_away_constness_r (tree *, tree *);
57 static bool casts_away_constness (tree, tree);
58 static void maybe_warn_about_returning_address_of_local (tree);
59 static tree lookup_destructor (tree, tree, tree);
61 /* Return the target type of TYPE, which means return T for:
62 T*, T&, T[], T (...), and otherwise, just T. */
65 target_type (tree type)
67 type = non_reference (type);
68 while (TREE_CODE (type) == POINTER_TYPE
69 || TREE_CODE (type) == ARRAY_TYPE
70 || TREE_CODE (type) == FUNCTION_TYPE
71 || TREE_CODE (type) == METHOD_TYPE
72 || TYPE_PTRMEM_P (type))
73 type = TREE_TYPE (type);
77 /* Do `exp = require_complete_type (exp);' to make sure exp
78 does not have an incomplete type. (That includes void types.)
79 Returns the error_mark_node if the VALUE does not have
80 complete type when this function returns. */
83 require_complete_type (tree value)
87 if (processing_template_decl || value == error_mark_node)
90 if (TREE_CODE (value) == OVERLOAD)
91 type = unknown_type_node;
93 type = TREE_TYPE (value);
95 if (type == error_mark_node)
96 return error_mark_node;
98 /* First, detect a valid value with a complete type. */
99 if (COMPLETE_TYPE_P (type))
102 if (complete_type_or_else (type, value))
105 return error_mark_node;
108 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
109 a template instantiation, do the instantiation. Returns TYPE,
110 whether or not it could be completed, unless something goes
111 horribly wrong, in which case the error_mark_node is returned. */
114 complete_type (tree type)
116 if (type == NULL_TREE)
117 /* Rather than crash, we return something sure to cause an error
119 return error_mark_node;
121 if (type == error_mark_node || COMPLETE_TYPE_P (type))
123 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
125 tree t = complete_type (TREE_TYPE (type));
126 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
128 TYPE_NEEDS_CONSTRUCTING (type)
129 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
131 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics. DIAG_TYPE indicates the type
141 of diagnostic: 0 for an error, 1 for a warning, 2 for a pedwarn.
142 Returns NULL_TREE if the type cannot be made complete. */
145 complete_type_or_diagnostic (tree type, tree value, int diag_type)
147 type = complete_type (type);
148 if (type == error_mark_node)
149 /* We already issued an error. */
151 else if (!COMPLETE_TYPE_P (type))
153 cxx_incomplete_type_diagnostic (value, type, diag_type);
160 /* Return truthvalue of whether type of EXP is instantiated. */
163 type_unknown_p (tree exp)
165 return (TREE_CODE (exp) == TREE_LIST
166 || TREE_TYPE (exp) == unknown_type_node);
170 /* Return the common type of two parameter lists.
171 We assume that comptypes has already been done and returned 1;
172 if that isn't so, this may crash.
174 As an optimization, free the space we allocate if the parameter
175 lists are already common. */
178 commonparms (tree p1, tree p2)
180 tree oldargs = p1, newargs, n;
184 len = list_length (p1);
185 newargs = tree_last (p1);
187 if (newargs == void_list_node)
196 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
201 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
203 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
205 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
208 else if (! TREE_PURPOSE (p1))
210 if (TREE_PURPOSE (p2))
212 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
218 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
220 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
222 if (TREE_VALUE (p1) != TREE_VALUE (p2))
225 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
228 TREE_VALUE (n) = TREE_VALUE (p1);
236 /* Given a type, perhaps copied for a typedef,
237 find the "original" version of it. */
239 original_type (tree t)
241 while (TYPE_NAME (t) != NULL_TREE)
243 tree x = TYPE_NAME (t);
244 if (TREE_CODE (x) != TYPE_DECL)
246 x = DECL_ORIGINAL_TYPE (x);
254 /* T1 and T2 are arithmetic or enumeration types. Return the type
255 that will result from the "usual arithmetic conversions" on T1 and
256 T2 as described in [expr]. */
259 type_after_usual_arithmetic_conversions (tree t1, tree t2)
261 enum tree_code code1 = TREE_CODE (t1);
262 enum tree_code code2 = TREE_CODE (t2);
265 /* FIXME: Attributes. */
266 gcc_assert (ARITHMETIC_TYPE_P (t1)
267 || TREE_CODE (t1) == COMPLEX_TYPE
268 || TREE_CODE (t1) == ENUMERAL_TYPE);
269 gcc_assert (ARITHMETIC_TYPE_P (t2)
270 || TREE_CODE (t2) == COMPLEX_TYPE
271 || TREE_CODE (t2) == ENUMERAL_TYPE);
273 /* In what follows, we slightly generalize the rules given in [expr] so
274 as to deal with `long long' and `complex'. First, merge the
276 attributes = (*targetm.merge_type_attributes) (t1, t2);
278 /* If one type is complex, form the common type of the non-complex
279 components, then make that complex. Use T1 or T2 if it is the
281 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
283 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
284 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
286 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
288 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
289 return build_type_attribute_variant (t1, attributes);
290 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
291 return build_type_attribute_variant (t2, attributes);
293 return build_type_attribute_variant (build_complex_type (subtype),
297 /* If only one is real, use it as the result. */
298 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
299 return build_type_attribute_variant (t1, attributes);
300 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
301 return build_type_attribute_variant (t2, attributes);
303 /* Perform the integral promotions. */
304 if (code1 != REAL_TYPE)
306 t1 = type_promotes_to (t1);
307 t2 = type_promotes_to (t2);
310 /* Both real or both integers; use the one with greater precision. */
311 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
312 return build_type_attribute_variant (t1, attributes);
313 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
314 return build_type_attribute_variant (t2, attributes);
316 /* The types are the same; no need to do anything fancy. */
317 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
318 return build_type_attribute_variant (t1, attributes);
320 if (code1 != REAL_TYPE)
322 /* If one is a sizetype, use it so size_binop doesn't blow up. */
323 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
324 return build_type_attribute_variant (t1, attributes);
325 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
326 return build_type_attribute_variant (t2, attributes);
328 /* If one is unsigned long long, then convert the other to unsigned
330 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
331 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
332 return build_type_attribute_variant (long_long_unsigned_type_node,
334 /* If one is a long long, and the other is an unsigned long, and
335 long long can represent all the values of an unsigned long, then
336 convert to a long long. Otherwise, convert to an unsigned long
337 long. Otherwise, if either operand is long long, convert the
340 Since we're here, we know the TYPE_PRECISION is the same;
341 therefore converting to long long cannot represent all the values
342 of an unsigned long, so we choose unsigned long long in that
344 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
345 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
347 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
348 ? long_long_unsigned_type_node
349 : long_long_integer_type_node);
350 return build_type_attribute_variant (t, attributes);
353 /* Go through the same procedure, but for longs. */
354 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
355 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
356 return build_type_attribute_variant (long_unsigned_type_node,
358 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
359 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
361 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
362 ? long_unsigned_type_node : long_integer_type_node);
363 return build_type_attribute_variant (t, attributes);
365 /* Otherwise prefer the unsigned one. */
366 if (TYPE_UNSIGNED (t1))
367 return build_type_attribute_variant (t1, attributes);
369 return build_type_attribute_variant (t2, attributes);
373 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
374 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
375 return build_type_attribute_variant (long_double_type_node,
377 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
378 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
379 return build_type_attribute_variant (double_type_node,
381 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
382 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
383 return build_type_attribute_variant (float_type_node,
386 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
387 the standard C++ floating-point types. Logic earlier in this
388 function has already eliminated the possibility that
389 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
390 compelling reason to choose one or the other. */
391 return build_type_attribute_variant (t1, attributes);
395 /* Subroutine of composite_pointer_type to implement the recursive
396 case. See that function for documentation fo the parameters. */
399 composite_pointer_type_r (tree t1, tree t2, const char* location)
406 /* Determine the types pointed to by T1 and T2. */
407 if (TREE_CODE (t1) == POINTER_TYPE)
409 pointee1 = TREE_TYPE (t1);
410 pointee2 = TREE_TYPE (t2);
414 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
415 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
420 Otherwise, the composite pointer type is a pointer type
421 similar (_conv.qual_) to the type of one of the operands,
422 with a cv-qualification signature (_conv.qual_) that is the
423 union of the cv-qualification signatures of the operand
425 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
426 result_type = pointee1;
427 else if ((TREE_CODE (pointee1) == POINTER_TYPE
428 && TREE_CODE (pointee2) == POINTER_TYPE)
429 || (TYPE_PTR_TO_MEMBER_P (pointee1)
430 && TYPE_PTR_TO_MEMBER_P (pointee2)))
431 result_type = composite_pointer_type_r (pointee1, pointee2, location);
434 pedwarn ("%s between distinct pointer types %qT and %qT "
437 result_type = void_type_node;
439 result_type = cp_build_qualified_type (result_type,
440 (cp_type_quals (pointee1)
441 | cp_type_quals (pointee2)));
442 /* If the original types were pointers to members, so is the
444 if (TYPE_PTR_TO_MEMBER_P (t1))
446 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
447 TYPE_PTRMEM_CLASS_TYPE (t2)))
448 pedwarn ("%s between distinct pointer types %qT and %qT "
451 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
455 result_type = build_pointer_type (result_type);
457 /* Merge the attributes. */
458 attributes = (*targetm.merge_type_attributes) (t1, t2);
459 return build_type_attribute_variant (result_type, attributes);
462 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
463 ARG1 and ARG2 are the values with those types. The LOCATION is a
464 string describing the current location, in case an error occurs.
466 This routine also implements the computation of a common type for
467 pointers-to-members as per [expr.eq]. */
470 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
471 const char* location)
478 If one operand is a null pointer constant, the composite pointer
479 type is the type of the other operand. */
480 if (null_ptr_cst_p (arg1))
482 if (null_ptr_cst_p (arg2))
489 If one of the operands has type "pointer to cv1 void*", then
490 the other has type "pointer to cv2T", and the composite pointer
491 type is "pointer to cv12 void", where cv12 is the union of cv1
494 If either type is a pointer to void, make sure it is T1. */
495 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
503 /* Now, if T1 is a pointer to void, merge the qualifiers. */
504 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
509 if (pedantic && TYPE_PTRFN_P (t2))
510 pedwarn ("ISO C++ forbids %s between pointer of type %<void *%> "
511 "and pointer-to-function", location);
513 = cp_build_qualified_type (void_type_node,
514 (cp_type_quals (TREE_TYPE (t1))
515 | cp_type_quals (TREE_TYPE (t2))));
516 result_type = build_pointer_type (result_type);
517 /* Merge the attributes. */
518 attributes = (*targetm.merge_type_attributes) (t1, t2);
519 return build_type_attribute_variant (result_type, attributes);
522 /* [expr.eq] permits the application of a pointer conversion to
523 bring the pointers to a common type. */
524 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
525 && CLASS_TYPE_P (TREE_TYPE (t1))
526 && CLASS_TYPE_P (TREE_TYPE (t2))
527 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
530 class1 = TREE_TYPE (t1);
531 class2 = TREE_TYPE (t2);
533 if (DERIVED_FROM_P (class1, class2))
534 t2 = (build_pointer_type
535 (cp_build_qualified_type (class1, TYPE_QUALS (class2))));
536 else if (DERIVED_FROM_P (class2, class1))
537 t1 = (build_pointer_type
538 (cp_build_qualified_type (class2, TYPE_QUALS (class1))));
541 error ("%s between distinct pointer types %qT and %qT "
542 "lacks a cast", location, t1, t2);
543 return error_mark_node;
546 /* [expr.eq] permits the application of a pointer-to-member
547 conversion to change the class type of one of the types. */
548 else if (TYPE_PTR_TO_MEMBER_P (t1)
549 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
550 TYPE_PTRMEM_CLASS_TYPE (t2)))
552 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
553 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
555 if (DERIVED_FROM_P (class1, class2))
556 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
557 else if (DERIVED_FROM_P (class2, class1))
558 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
561 error ("%s between distinct pointer-to-member types %qT and %qT "
562 "lacks a cast", location, t1, t2);
563 return error_mark_node;
567 return composite_pointer_type_r (t1, t2, location);
570 /* Return the merged type of two types.
571 We assume that comptypes has already been done and returned 1;
572 if that isn't so, this may crash.
574 This just combines attributes and default arguments; any other
575 differences would cause the two types to compare unalike. */
578 merge_types (tree t1, tree t2)
580 enum tree_code code1;
581 enum tree_code code2;
584 /* Save time if the two types are the same. */
587 if (original_type (t1) == original_type (t2))
590 /* If one type is nonsense, use the other. */
591 if (t1 == error_mark_node)
593 if (t2 == error_mark_node)
596 /* Merge the attributes. */
597 attributes = (*targetm.merge_type_attributes) (t1, t2);
599 if (TYPE_PTRMEMFUNC_P (t1))
600 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
601 if (TYPE_PTRMEMFUNC_P (t2))
602 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
604 code1 = TREE_CODE (t1);
605 code2 = TREE_CODE (t2);
611 /* For two pointers, do this recursively on the target type. */
613 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
614 int quals = cp_type_quals (t1);
616 if (code1 == POINTER_TYPE)
617 t1 = build_pointer_type (target);
619 t1 = build_reference_type (target);
620 t1 = build_type_attribute_variant (t1, attributes);
621 t1 = cp_build_qualified_type (t1, quals);
623 if (TREE_CODE (target) == METHOD_TYPE)
624 t1 = build_ptrmemfunc_type (t1);
633 quals = cp_type_quals (t1);
634 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
635 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
636 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
638 t1 = cp_build_qualified_type (t1, quals);
644 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
645 /* Save space: see if the result is identical to one of the args. */
646 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
647 return build_type_attribute_variant (t1, attributes);
648 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
649 return build_type_attribute_variant (t2, attributes);
650 /* Merge the element types, and have a size if either arg has one. */
651 t1 = build_cplus_array_type
652 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
657 /* Function types: prefer the one that specified arg types.
658 If both do, merge the arg types. Also merge the return types. */
660 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
661 tree p1 = TYPE_ARG_TYPES (t1);
662 tree p2 = TYPE_ARG_TYPES (t2);
665 /* Save space: see if the result is identical to one of the args. */
666 if (valtype == TREE_TYPE (t1) && ! p2)
667 return cp_build_type_attribute_variant (t1, attributes);
668 if (valtype == TREE_TYPE (t2) && ! p1)
669 return cp_build_type_attribute_variant (t2, attributes);
671 /* Simple way if one arg fails to specify argument types. */
672 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
674 rval = build_function_type (valtype, p2);
675 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
676 rval = build_exception_variant (rval, raises);
677 return cp_build_type_attribute_variant (rval, attributes);
679 raises = TYPE_RAISES_EXCEPTIONS (t1);
680 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
682 rval = build_function_type (valtype, p1);
684 rval = build_exception_variant (rval, raises);
685 return cp_build_type_attribute_variant (rval, attributes);
688 rval = build_function_type (valtype, commonparms (p1, p2));
689 t1 = build_exception_variant (rval, raises);
695 /* Get this value the long way, since TYPE_METHOD_BASETYPE
696 is just the main variant of this. */
697 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
698 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
701 /* If this was a member function type, get back to the
702 original type of type member function (i.e., without
703 the class instance variable up front. */
704 t1 = build_function_type (TREE_TYPE (t1),
705 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
706 t2 = build_function_type (TREE_TYPE (t2),
707 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
708 t3 = merge_types (t1, t2);
709 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
710 TYPE_ARG_TYPES (t3));
711 t1 = build_exception_variant (t3, raises);
716 /* There is no need to merge attributes into a TYPENAME_TYPE.
717 When the type is instantiated it will have whatever
718 attributes result from the instantiation. */
723 return cp_build_type_attribute_variant (t1, attributes);
726 /* Return the common type of two types.
727 We assume that comptypes has already been done and returned 1;
728 if that isn't so, this may crash.
730 This is the type for the result of most arithmetic operations
731 if the operands have the given two types. */
734 common_type (tree t1, tree t2)
736 enum tree_code code1;
737 enum tree_code code2;
739 /* If one type is nonsense, bail. */
740 if (t1 == error_mark_node || t2 == error_mark_node)
741 return error_mark_node;
743 code1 = TREE_CODE (t1);
744 code2 = TREE_CODE (t2);
746 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
747 || code1 == COMPLEX_TYPE)
748 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
749 || code2 == COMPLEX_TYPE))
750 return type_after_usual_arithmetic_conversions (t1, t2);
752 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
753 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
754 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
755 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
761 /* Compare two exception specifier types for exactness or subsetness, if
762 allowed. Returns false for mismatch, true for match (same, or
765 [except.spec] "If a class X ... objects of class X or any class publicly
766 and unambiguously derived from X. Similarly, if a pointer type Y * ...
767 exceptions of type Y * or that are pointers to any type publicly and
768 unambiguously derived from Y. Otherwise a function only allows exceptions
769 that have the same type ..."
770 This does not mention cv qualifiers and is different to what throw
771 [except.throw] and catch [except.catch] will do. They will ignore the
772 top level cv qualifiers, and allow qualifiers in the pointer to class
775 We implement the letter of the standard. */
778 comp_except_types (tree a, tree b, bool exact)
780 if (same_type_p (a, b))
784 if (cp_type_quals (a) || cp_type_quals (b))
787 if (TREE_CODE (a) == POINTER_TYPE
788 && TREE_CODE (b) == POINTER_TYPE)
792 if (cp_type_quals (a) || cp_type_quals (b))
796 if (TREE_CODE (a) != RECORD_TYPE
797 || TREE_CODE (b) != RECORD_TYPE)
800 if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
806 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
807 If EXACT is false, T2 can be stricter than T1 (according to 15.4/7),
808 otherwise it must be exact. Exception lists are unordered, but
809 we've already filtered out duplicates. Most lists will be in order,
810 we should try to make use of that. */
813 comp_except_specs (tree t1, tree t2, bool exact)
822 if (t1 == NULL_TREE) /* T1 is ... */
823 return t2 == NULL_TREE || !exact;
824 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
825 return t2 != NULL_TREE && !TREE_VALUE (t2);
826 if (t2 == NULL_TREE) /* T2 is ... */
828 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
831 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
832 Count how many we find, to determine exactness. For exact matching and
833 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
835 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
837 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
839 tree a = TREE_VALUE (probe);
840 tree b = TREE_VALUE (t2);
842 if (comp_except_types (a, b, exact))
844 if (probe == base && exact)
845 base = TREE_CHAIN (probe);
850 if (probe == NULL_TREE)
853 return !exact || base == NULL_TREE || length == list_length (t1);
856 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
857 [] can match [size]. */
860 comp_array_types (tree t1, tree t2, bool allow_redeclaration)
869 /* The type of the array elements must be the same. */
870 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
873 d1 = TYPE_DOMAIN (t1);
874 d2 = TYPE_DOMAIN (t2);
879 /* If one of the arrays is dimensionless, and the other has a
880 dimension, they are of different types. However, it is valid to
888 declarations for an array object can specify
889 array types that differ by the presence or absence of a major
890 array bound (_dcl.array_). */
892 return allow_redeclaration;
894 /* Check that the dimensions are the same. */
896 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
898 max1 = TYPE_MAX_VALUE (d1);
899 max2 = TYPE_MAX_VALUE (d2);
900 if (processing_template_decl && !abi_version_at_least (2)
901 && !value_dependent_expression_p (max1)
902 && !value_dependent_expression_p (max2))
904 /* With abi-1 we do not fold non-dependent array bounds, (and
905 consequently mangle them incorrectly). We must therefore
906 fold them here, to verify the domains have the same
912 if (!cp_tree_equal (max1, max2))
918 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
919 is a bitwise-or of the COMPARE_* flags. */
922 comptypes (tree t1, tree t2, int strict)
929 /* Suppress errors caused by previously reported errors. */
930 if (t1 == error_mark_node || t2 == error_mark_node)
933 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
935 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
936 current instantiation. */
937 if (TREE_CODE (t1) == TYPENAME_TYPE)
939 tree resolved = resolve_typename_type (t1, /*only_current_p=*/true);
941 if (resolved != error_mark_node)
945 if (TREE_CODE (t2) == TYPENAME_TYPE)
947 tree resolved = resolve_typename_type (t2, /*only_current_p=*/true);
949 if (resolved != error_mark_node)
953 /* If either type is the internal version of sizetype, use the
955 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
956 && TYPE_ORIG_SIZE_TYPE (t1))
957 t1 = TYPE_ORIG_SIZE_TYPE (t1);
959 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
960 && TYPE_ORIG_SIZE_TYPE (t2))
961 t2 = TYPE_ORIG_SIZE_TYPE (t2);
963 if (TYPE_PTRMEMFUNC_P (t1))
964 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
965 if (TYPE_PTRMEMFUNC_P (t2))
966 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
968 /* Different classes of types can't be compatible. */
969 if (TREE_CODE (t1) != TREE_CODE (t2))
972 /* Qualifiers must match. For array types, we will check when we
973 recur on the array element types. */
974 if (TREE_CODE (t1) != ARRAY_TYPE
975 && TYPE_QUALS (t1) != TYPE_QUALS (t2))
977 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
980 /* Allow for two different type nodes which have essentially the same
981 definition. Note that we already checked for equality of the type
982 qualifiers (just above). */
984 if (TREE_CODE (t1) != ARRAY_TYPE
985 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
988 if (!(*targetm.comp_type_attributes) (t1, t2))
991 switch (TREE_CODE (t1))
993 case TEMPLATE_TEMPLATE_PARM:
994 case BOUND_TEMPLATE_TEMPLATE_PARM:
995 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
996 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
998 if (!comp_template_parms
999 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1000 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1002 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1004 /* Don't check inheritance. */
1005 strict = COMPARE_STRICT;
1010 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1011 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1012 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1013 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1016 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1018 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1021 /* We may be dealing with Objective-C instances... */
1022 if (TREE_CODE (t1) == RECORD_TYPE
1023 && (retval = objc_comptypes (t1, t2, 0) >= 0))
1025 /* ...but fall through if we are not. */
1030 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1031 strict & ~COMPARE_REDECLARATION))
1036 case REFERENCE_TYPE:
1037 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1041 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1043 return compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1046 /* Target types must match incl. qualifiers. */
1047 return comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION));
1049 case TEMPLATE_TYPE_PARM:
1050 return (TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1051 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2));
1054 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1055 TYPENAME_TYPE_FULLNAME (t2)))
1057 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1059 case UNBOUND_CLASS_TEMPLATE:
1060 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1062 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1065 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1068 return TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1069 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1078 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1081 at_least_as_qualified_p (tree type1, tree type2)
1083 int q1 = cp_type_quals (type1);
1084 int q2 = cp_type_quals (type2);
1086 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1087 return (q1 & q2) == q2;
1090 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1093 more_qualified_p (tree type1, tree type2)
1095 int q1 = cp_type_quals (type1);
1096 int q2 = cp_type_quals (type2);
1098 return q1 != q2 && (q1 & q2) == q2;
1101 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1102 more cv-qualified that TYPE1, and 0 otherwise. */
1105 comp_cv_qualification (tree type1, tree type2)
1107 int q1 = cp_type_quals (type1);
1108 int q2 = cp_type_quals (type2);
1113 if ((q1 & q2) == q2)
1115 else if ((q1 & q2) == q1)
1121 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1122 subset of the cv-qualification signature of TYPE2, and the types
1123 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1126 comp_cv_qual_signature (tree type1, tree type2)
1128 if (comp_ptr_ttypes_real (type2, type1, -1))
1130 else if (comp_ptr_ttypes_real (type1, type2, -1))
1136 /* If two types share a common base type, return that basetype.
1137 If there is not a unique most-derived base type, this function
1138 returns ERROR_MARK_NODE. */
1141 common_base_type (tree tt1, tree tt2)
1143 tree best = NULL_TREE;
1146 /* If one is a baseclass of another, that's good enough. */
1147 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1149 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1152 /* Otherwise, try to find a unique baseclass of TT1
1153 that is shared by TT2, and follow that down. */
1154 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (tt1))-1; i >= 0; i--)
1156 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (tt1), i));
1157 tree trial = common_base_type (basetype, tt2);
1161 if (trial == error_mark_node)
1163 if (best == NULL_TREE)
1165 else if (best != trial)
1166 return error_mark_node;
1171 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (tt2))-1; i >= 0; i--)
1173 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (tt2), i));
1174 tree trial = common_base_type (tt1, basetype);
1178 if (trial == error_mark_node)
1180 if (best == NULL_TREE)
1182 else if (best != trial)
1183 return error_mark_node;
1189 /* Subroutines of `comptypes'. */
1191 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1192 equivalent in the sense that functions with those parameter types
1193 can have equivalent types. The two lists must be equivalent,
1194 element by element. */
1197 compparms (tree parms1, tree parms2)
1201 /* An unspecified parmlist matches any specified parmlist
1202 whose argument types don't need default promotions. */
1204 for (t1 = parms1, t2 = parms2;
1206 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1208 /* If one parmlist is shorter than the other,
1209 they fail to match. */
1212 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1219 /* Process a sizeof or alignof expression where the operand is a
1223 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1225 enum tree_code type_code;
1227 const char *op_name;
1229 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1230 if (type == error_mark_node)
1231 return error_mark_node;
1233 if (processing_template_decl)
1235 value = build_min (op, size_type_node, type);
1236 TREE_READONLY (value) = 1;
1240 op_name = operator_name_info[(int) op].name;
1242 type = non_reference (type);
1243 type_code = TREE_CODE (type);
1245 if (type_code == METHOD_TYPE)
1247 if (complain && (pedantic || warn_pointer_arith))
1248 pedwarn ("invalid application of %qs to a member function", op_name);
1249 value = size_one_node;
1252 value = c_sizeof_or_alignof_type (complete_type (type), op, complain);
1257 /* Process a sizeof or alignof expression where the operand is an
1261 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op)
1263 const char *op_name = operator_name_info[(int) op].name;
1265 if (e == error_mark_node)
1266 return error_mark_node;
1268 if (processing_template_decl)
1270 e = build_min (op, size_type_node, e);
1271 TREE_SIDE_EFFECTS (e) = 0;
1272 TREE_READONLY (e) = 1;
1277 if (TREE_CODE (e) == COMPONENT_REF
1278 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1279 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1281 error ("invalid application of %qs to a bit-field", op_name);
1284 else if (is_overloaded_fn (e))
1286 pedwarn ("ISO C++ forbids applying %qs to an expression of "
1287 "function type", op_name);
1290 else if (type_unknown_p (e))
1292 cxx_incomplete_type_error (e, TREE_TYPE (e));
1298 return cxx_sizeof_or_alignof_type (e, op, true);
1302 /* EXPR is being used in a context that is not a function call.
1307 The expression can be used only as the left-hand operand of a
1308 member function call.
1310 [expr.mptr.operator]
1312 If the result of .* or ->* is a function, then that result can be
1313 used only as the operand for the function call operator ().
1315 by issuing an error message if appropriate. Returns true iff EXPR
1316 violates these rules. */
1319 invalid_nonstatic_memfn_p (tree expr)
1321 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
1323 error ("invalid use of non-static member function");
1329 /* Perform the conversions in [expr] that apply when an lvalue appears
1330 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1331 function-to-pointer conversions.
1333 In addition manifest constants are replaced by their values. */
1336 decay_conversion (tree exp)
1339 enum tree_code code;
1341 type = TREE_TYPE (exp);
1342 code = TREE_CODE (type);
1344 if (code == REFERENCE_TYPE)
1346 exp = convert_from_reference (exp);
1347 type = TREE_TYPE (exp);
1348 code = TREE_CODE (type);
1351 if (type == error_mark_node)
1352 return error_mark_node;
1354 if (type_unknown_p (exp))
1356 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1357 return error_mark_node;
1360 /* Constants can be used directly unless they're not loadable. */
1361 if (TREE_CODE (exp) == CONST_DECL)
1362 exp = DECL_INITIAL (exp);
1363 /* Replace a nonvolatile const static variable with its value. We
1364 don't do this for arrays, though; we want the address of the
1365 first element of the array, not the address of the first element
1366 of its initializing constant. */
1367 else if (code != ARRAY_TYPE)
1369 exp = decl_constant_value (exp);
1370 type = TREE_TYPE (exp);
1373 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1374 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1376 if (code == VOID_TYPE)
1378 error ("void value not ignored as it ought to be");
1379 return error_mark_node;
1381 if (invalid_nonstatic_memfn_p (exp))
1382 return error_mark_node;
1383 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1384 return build_unary_op (ADDR_EXPR, exp, 0);
1385 if (code == ARRAY_TYPE)
1390 if (TREE_CODE (exp) == INDIRECT_REF)
1391 return build_nop (build_pointer_type (TREE_TYPE (type)),
1392 TREE_OPERAND (exp, 0));
1394 if (TREE_CODE (exp) == COMPOUND_EXPR)
1396 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1397 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1398 TREE_OPERAND (exp, 0), op1);
1402 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1404 error ("invalid use of non-lvalue array");
1405 return error_mark_node;
1408 ptrtype = build_pointer_type (TREE_TYPE (type));
1410 if (TREE_CODE (exp) == VAR_DECL)
1412 if (!cxx_mark_addressable (exp))
1413 return error_mark_node;
1414 adr = build_nop (ptrtype, build_address (exp));
1417 /* This way is better for a COMPONENT_REF since it can
1418 simplify the offset for a component. */
1419 adr = build_unary_op (ADDR_EXPR, exp, 1);
1420 return cp_convert (ptrtype, adr);
1423 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1424 rvalues always have cv-unqualified types. */
1425 if (! CLASS_TYPE_P (type))
1426 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1432 default_conversion (tree exp)
1434 exp = decay_conversion (exp);
1436 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1437 exp = perform_integral_promotions (exp);
1442 /* EXPR is an expression with an integral or enumeration type.
1443 Perform the integral promotions in [conv.prom], and return the
1447 perform_integral_promotions (tree expr)
1452 type = TREE_TYPE (expr);
1453 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1454 promoted_type = type_promotes_to (type);
1455 if (type != promoted_type)
1456 expr = cp_convert (promoted_type, expr);
1460 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1464 inline_conversion (tree exp)
1466 if (TREE_CODE (exp) == FUNCTION_DECL)
1467 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1472 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1473 decay_conversion to one. */
1476 string_conv_p (tree totype, tree exp, int warn)
1480 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1483 t = TREE_TYPE (totype);
1484 if (!same_type_p (t, char_type_node)
1485 && !same_type_p (t, wchar_type_node))
1488 if (TREE_CODE (exp) == STRING_CST)
1490 /* Make sure that we don't try to convert between char and wchar_t. */
1491 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1496 /* Is this a string constant which has decayed to 'const char *'? */
1497 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1498 if (!same_type_p (TREE_TYPE (exp), t))
1501 if (TREE_CODE (exp) != ADDR_EXPR
1502 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1506 /* This warning is not very useful, as it complains about printf. */
1507 if (warn && warn_write_strings)
1508 warning ("deprecated conversion from string constant to %qT'", totype);
1513 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1514 can, for example, use as an lvalue. This code used to be in
1515 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1516 expressions, where we're dealing with aggregates. But now it's again only
1517 called from unary_complex_lvalue. The case (in particular) that led to
1518 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1522 rationalize_conditional_expr (enum tree_code code, tree t)
1524 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1525 the first operand is always the one to be used if both operands
1526 are equal, so we know what conditional expression this used to be. */
1527 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1529 /* The following code is incorrect if either operand side-effects. */
1530 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
1531 && !TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)));
1533 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1534 ? LE_EXPR : GE_EXPR),
1535 TREE_OPERAND (t, 0),
1536 TREE_OPERAND (t, 1),
1537 /*overloaded_p=*/NULL),
1538 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1539 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1543 build_conditional_expr (TREE_OPERAND (t, 0),
1544 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1545 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1548 /* Given the TYPE of an anonymous union field inside T, return the
1549 FIELD_DECL for the field. If not found return NULL_TREE. Because
1550 anonymous unions can nest, we must also search all anonymous unions
1551 that are directly reachable. */
1554 lookup_anon_field (tree t, tree type)
1558 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1560 if (TREE_STATIC (field))
1562 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1565 /* If we find it directly, return the field. */
1566 if (DECL_NAME (field) == NULL_TREE
1567 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1572 /* Otherwise, it could be nested, search harder. */
1573 if (DECL_NAME (field) == NULL_TREE
1574 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1576 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1584 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1585 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1586 non-NULL, it indicates the path to the base used to name MEMBER.
1587 If PRESERVE_REFERENCE is true, the expression returned will have
1588 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1589 returned will have the type referred to by the reference.
1591 This function does not perform access control; that is either done
1592 earlier by the parser when the name of MEMBER is resolved to MEMBER
1593 itself, or later when overload resolution selects one of the
1594 functions indicated by MEMBER. */
1597 build_class_member_access_expr (tree object, tree member,
1598 tree access_path, bool preserve_reference)
1602 tree result = NULL_TREE;
1604 if (object == error_mark_node || member == error_mark_node)
1605 return error_mark_node;
1607 if (TREE_CODE (member) == PSEUDO_DTOR_EXPR)
1610 gcc_assert (DECL_P (member) || BASELINK_P (member));
1614 The type of the first expression shall be "class object" (of a
1616 object_type = TREE_TYPE (object);
1617 if (!currently_open_class (object_type)
1618 && !complete_type_or_else (object_type, object))
1619 return error_mark_node;
1620 if (!CLASS_TYPE_P (object_type))
1622 error ("request for member %qD in %qE, which is of non-class type %qT",
1623 member, object, object_type);
1624 return error_mark_node;
1627 /* The standard does not seem to actually say that MEMBER must be a
1628 member of OBJECT_TYPE. However, that is clearly what is
1630 if (DECL_P (member))
1632 member_scope = DECL_CLASS_CONTEXT (member);
1634 if (TREE_DEPRECATED (member))
1635 warn_deprecated_use (member);
1638 member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1639 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1640 presently be the anonymous union. Go outwards until we find a
1641 type related to OBJECT_TYPE. */
1642 while (ANON_AGGR_TYPE_P (member_scope)
1643 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1645 member_scope = TYPE_CONTEXT (member_scope);
1646 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1648 if (TREE_CODE (member) == FIELD_DECL)
1649 error ("invalid use of nonstatic data member %qE", member);
1651 error ("%qD is not a member of %qT", member, object_type);
1652 return error_mark_node;
1655 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1656 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
1657 in the frontend; only _DECLs and _REFs are lvalues in the backend. */
1659 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1661 object = build_indirect_ref (temp, NULL);
1664 /* In [expr.ref], there is an explicit list of the valid choices for
1665 MEMBER. We check for each of those cases here. */
1666 if (TREE_CODE (member) == VAR_DECL)
1668 /* A static data member. */
1670 /* If OBJECT has side-effects, they are supposed to occur. */
1671 if (TREE_SIDE_EFFECTS (object))
1672 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1674 else if (TREE_CODE (member) == FIELD_DECL)
1676 /* A non-static data member. */
1681 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1682 && integer_zerop (TREE_OPERAND (object, 0)));
1684 /* Convert OBJECT to the type of MEMBER. */
1685 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
1686 TYPE_MAIN_VARIANT (member_scope)))
1691 binfo = lookup_base (access_path ? access_path : object_type,
1692 member_scope, ba_unique, &kind);
1693 if (binfo == error_mark_node)
1694 return error_mark_node;
1696 /* It is invalid to try to get to a virtual base of a
1697 NULL object. The most common cause is invalid use of
1699 if (null_object_p && kind == bk_via_virtual)
1701 error ("invalid access to non-static data member %qD of "
1704 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
1705 return error_mark_node;
1708 /* Convert to the base. */
1709 object = build_base_path (PLUS_EXPR, object, binfo,
1711 /* If we found the base successfully then we should be able
1712 to convert to it successfully. */
1713 gcc_assert (object != error_mark_node);
1716 /* Complain about other invalid uses of offsetof, even though they will
1717 give the right answer. Note that we complain whether or not they
1718 actually used the offsetof macro, since there's no way to know at this
1719 point. So we just give a warning, instead of a pedwarn. */
1720 /* Do not produce this warning for base class field references, because
1721 we know for a fact that didn't come from offsetof. This does occur
1722 in various testsuite cases where a null object is passed where a
1723 vtable access is required. */
1724 if (null_object_p && warn_invalid_offsetof
1725 && CLASSTYPE_NON_POD_P (object_type)
1726 && !DECL_FIELD_IS_BASE (member)
1727 && !skip_evaluation)
1729 warning ("invalid access to non-static data member %qD of NULL object",
1731 warning ("(perhaps the %<offsetof%> macro was used incorrectly)");
1734 /* If MEMBER is from an anonymous aggregate, we have converted
1735 OBJECT so that it refers to the class containing the
1736 anonymous union. Generate a reference to the anonymous union
1737 itself, and recur to find MEMBER. */
1738 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1739 /* When this code is called from build_field_call, the
1740 object already has the type of the anonymous union.
1741 That is because the COMPONENT_REF was already
1742 constructed, and was then disassembled before calling
1743 build_field_call. After the function-call code is
1744 cleaned up, this waste can be eliminated. */
1745 && (!same_type_ignoring_top_level_qualifiers_p
1746 (TREE_TYPE (object), DECL_CONTEXT (member))))
1748 tree anonymous_union;
1750 anonymous_union = lookup_anon_field (TREE_TYPE (object),
1751 DECL_CONTEXT (member));
1752 object = build_class_member_access_expr (object,
1754 /*access_path=*/NULL_TREE,
1755 preserve_reference);
1758 /* Compute the type of the field, as described in [expr.ref]. */
1759 type_quals = TYPE_UNQUALIFIED;
1760 member_type = TREE_TYPE (member);
1761 if (TREE_CODE (member_type) != REFERENCE_TYPE)
1763 type_quals = (cp_type_quals (member_type)
1764 | cp_type_quals (object_type));
1766 /* A field is const (volatile) if the enclosing object, or the
1767 field itself, is const (volatile). But, a mutable field is
1768 not const, even within a const object. */
1769 if (DECL_MUTABLE_P (member))
1770 type_quals &= ~TYPE_QUAL_CONST;
1771 member_type = cp_build_qualified_type (member_type, type_quals);
1774 result = build3 (COMPONENT_REF, member_type, object, member,
1776 result = fold_if_not_in_template (result);
1778 /* Mark the expression const or volatile, as appropriate. Even
1779 though we've dealt with the type above, we still have to mark the
1780 expression itself. */
1781 if (type_quals & TYPE_QUAL_CONST)
1782 TREE_READONLY (result) = 1;
1783 else if (type_quals & TYPE_QUAL_VOLATILE)
1784 TREE_THIS_VOLATILE (result) = 1;
1786 else if (BASELINK_P (member))
1788 /* The member is a (possibly overloaded) member function. */
1792 /* If the MEMBER is exactly one static member function, then we
1793 know the type of the expression. Otherwise, we must wait
1794 until overload resolution has been performed. */
1795 functions = BASELINK_FUNCTIONS (member);
1796 if (TREE_CODE (functions) == FUNCTION_DECL
1797 && DECL_STATIC_FUNCTION_P (functions))
1798 type = TREE_TYPE (functions);
1800 type = unknown_type_node;
1801 /* Note that we do not convert OBJECT to the BASELINK_BINFO
1802 base. That will happen when the function is called. */
1803 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
1805 else if (TREE_CODE (member) == CONST_DECL)
1807 /* The member is an enumerator. */
1809 /* If OBJECT has side-effects, they are supposed to occur. */
1810 if (TREE_SIDE_EFFECTS (object))
1811 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
1816 error ("invalid use of %qD", member);
1817 return error_mark_node;
1820 if (!preserve_reference)
1823 If E2 is declared to have type "reference to T", then ... the
1824 type of E1.E2 is T. */
1825 result = convert_from_reference (result);
1830 /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
1831 SCOPE is NULL, by OBJECT.~DTOR_NAME. */
1834 lookup_destructor (tree object, tree scope, tree dtor_name)
1836 tree object_type = TREE_TYPE (object);
1837 tree dtor_type = TREE_OPERAND (dtor_name, 0);
1840 if (scope && !check_dtor_name (scope, dtor_name))
1842 error ("qualified type %qT does not match destructor name ~%qT",
1844 return error_mark_node;
1846 if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
1848 error ("the type being destroyed is %qT, but the destructor refers to %qT",
1849 TYPE_MAIN_VARIANT (object_type), dtor_type);
1850 return error_mark_node;
1852 if (!TYPE_HAS_DESTRUCTOR (dtor_type))
1853 return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope,
1855 expr = lookup_member (dtor_type, complete_dtor_identifier,
1856 /*protect=*/1, /*want_type=*/false);
1857 expr = (adjust_result_of_qualified_name_lookup
1858 (expr, dtor_type, object_type));
1862 /* This function is called by the parser to process a class member
1863 access expression of the form OBJECT.NAME. NAME is a node used by
1864 the parser to represent a name; it is not yet a DECL. It may,
1865 however, be a BASELINK where the BASELINK_FUNCTIONS is a
1866 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
1867 there is no reason to do the lookup twice, so the parser keeps the
1871 finish_class_member_access_expr (tree object, tree name)
1876 tree access_path = NULL_TREE;
1877 tree orig_object = object;
1878 tree orig_name = name;
1880 if (object == error_mark_node || name == error_mark_node)
1881 return error_mark_node;
1883 object_type = TREE_TYPE (object);
1885 if (processing_template_decl)
1887 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
1888 dependent_type_p (object_type)
1889 /* If NAME is just an IDENTIFIER_NODE, then the expression
1891 || TREE_CODE (object) == IDENTIFIER_NODE
1892 /* If NAME is "f<args>", where either 'f' or 'args' is
1893 dependent, then the expression is dependent. */
1894 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
1895 && dependent_template_id_p (TREE_OPERAND (name, 0),
1896 TREE_OPERAND (name, 1)))
1897 /* If NAME is "T::X" where "T" is dependent, then the
1898 expression is dependent. */
1899 || (TREE_CODE (name) == SCOPE_REF
1900 && TYPE_P (TREE_OPERAND (name, 0))
1901 && dependent_type_p (TREE_OPERAND (name, 0))))
1902 return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
1903 object = build_non_dependent_expr (object);
1906 if (TREE_CODE (object_type) == REFERENCE_TYPE)
1908 object = convert_from_reference (object);
1909 object_type = TREE_TYPE (object);
1914 The type of the first expression shall be "class object" (of a
1916 if (!currently_open_class (object_type)
1917 && !complete_type_or_else (object_type, object))
1918 return error_mark_node;
1919 if (!CLASS_TYPE_P (object_type))
1921 error ("request for member %qD in %qE, which is of non-class type %qT",
1922 name, object, object_type);
1923 return error_mark_node;
1926 if (BASELINK_P (name))
1928 /* A member function that has already been looked up. */
1929 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name)) == TEMPLATE_ID_EXPR);
1934 bool is_template_id = false;
1935 tree template_args = NULL_TREE;
1938 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1940 is_template_id = true;
1941 template_args = TREE_OPERAND (name, 1);
1942 name = TREE_OPERAND (name, 0);
1944 if (TREE_CODE (name) == OVERLOAD)
1945 name = DECL_NAME (get_first_fn (name));
1946 else if (DECL_P (name))
1947 name = DECL_NAME (name);
1950 if (TREE_CODE (name) == SCOPE_REF)
1952 /* A qualified name. The qualifying class or namespace `S' has
1953 already been looked up; it is either a TYPE or a
1954 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
1955 or a BIT_NOT_EXPR. */
1956 scope = TREE_OPERAND (name, 0);
1957 name = TREE_OPERAND (name, 1);
1958 gcc_assert (CLASS_TYPE_P (scope)
1959 || TREE_CODE (scope) == NAMESPACE_DECL);
1960 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
1961 || TREE_CODE (name) == BIT_NOT_EXPR);
1963 /* If SCOPE is a namespace, then the qualified name does not
1964 name a member of OBJECT_TYPE. */
1965 if (TREE_CODE (scope) == NAMESPACE_DECL)
1967 error ("%<%D::%D%> is not a member of %qT",
1968 scope, name, object_type);
1969 return error_mark_node;
1972 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
1973 access_path = lookup_base (object_type, scope, ba_check, NULL);
1974 if (access_path == error_mark_node)
1975 return error_mark_node;
1978 error ("%qT is not a base of %qT", scope, object_type);
1979 return error_mark_node;
1985 access_path = object_type;
1988 if (TREE_CODE (name) == BIT_NOT_EXPR)
1989 member = lookup_destructor (object, scope, name);
1992 /* Look up the member. */
1993 member = lookup_member (access_path, name, /*protect=*/1,
1994 /*want_type=*/false);
1995 if (member == NULL_TREE)
1997 error ("%qD has no member named %qE", object_type, name);
1998 return error_mark_node;
2000 if (member == error_mark_node)
2001 return error_mark_node;
2006 tree template = member;
2008 if (BASELINK_P (template))
2009 template = lookup_template_function (template, template_args);
2012 error ("%qD is not a member template function", name);
2013 return error_mark_node;
2018 if (TREE_DEPRECATED (member))
2019 warn_deprecated_use (member);
2021 expr = build_class_member_access_expr (object, member, access_path,
2022 /*preserve_reference=*/false);
2023 if (processing_template_decl && expr != error_mark_node)
2024 return build_min_non_dep (COMPONENT_REF, expr,
2025 orig_object, orig_name, NULL_TREE);
2029 /* Return an expression for the MEMBER_NAME field in the internal
2030 representation of PTRMEM, a pointer-to-member function. (Each
2031 pointer-to-member function type gets its own RECORD_TYPE so it is
2032 more convenient to access the fields by name than by FIELD_DECL.)
2033 This routine converts the NAME to a FIELD_DECL and then creates the
2034 node for the complete expression. */
2037 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2043 /* This code is a stripped down version of
2044 build_class_member_access_expr. It does not work to use that
2045 routine directly because it expects the object to be of class
2047 ptrmem_type = TREE_TYPE (ptrmem);
2048 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2049 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2050 /*want_type=*/false);
2051 member_type = cp_build_qualified_type (TREE_TYPE (member),
2052 cp_type_quals (ptrmem_type));
2053 return fold (build3 (COMPONENT_REF, member_type,
2054 ptrmem, member, NULL_TREE));
2057 /* Given an expression PTR for a pointer, return an expression
2058 for the value pointed to.
2059 ERRORSTRING is the name of the operator to appear in error messages.
2061 This function may need to overload OPERATOR_FNNAME.
2062 Must also handle REFERENCE_TYPEs for C++. */
2065 build_x_indirect_ref (tree expr, const char *errorstring)
2067 tree orig_expr = expr;
2070 if (processing_template_decl)
2072 if (type_dependent_expression_p (expr))
2073 return build_min_nt (INDIRECT_REF, expr);
2074 expr = build_non_dependent_expr (expr);
2077 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2078 NULL_TREE, /*overloaded_p=*/NULL);
2080 rval = build_indirect_ref (expr, errorstring);
2082 if (processing_template_decl && rval != error_mark_node)
2083 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2089 build_indirect_ref (tree ptr, const char *errorstring)
2093 if (ptr == error_mark_node)
2094 return error_mark_node;
2096 if (ptr == current_class_ptr)
2097 return current_class_ref;
2099 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2100 ? ptr : decay_conversion (ptr));
2101 type = TREE_TYPE (pointer);
2103 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2107 If the type of the expression is "pointer to T," the type
2108 of the result is "T."
2110 We must use the canonical variant because certain parts of
2111 the back end, like fold, do pointer comparisons between
2113 tree t = canonical_type_variant (TREE_TYPE (type));
2115 if (VOID_TYPE_P (t))
2117 /* A pointer to incomplete type (other than cv void) can be
2118 dereferenced [expr.unary.op]/1 */
2119 error ("%qT is not a pointer-to-object type", type);
2120 return error_mark_node;
2122 else if (TREE_CODE (pointer) == ADDR_EXPR
2123 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2124 /* The POINTER was something like `&x'. We simplify `*&x' to
2126 return TREE_OPERAND (pointer, 0);
2129 tree ref = build1 (INDIRECT_REF, t, pointer);
2131 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2132 so that we get the proper error message if the result is used
2133 to assign to. Also, &* is supposed to be a no-op. */
2134 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2135 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2136 TREE_SIDE_EFFECTS (ref)
2137 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2141 /* `pointer' won't be an error_mark_node if we were given a
2142 pointer to member, so it's cool to check for this here. */
2143 else if (TYPE_PTR_TO_MEMBER_P (type))
2144 error ("invalid use of %qs on pointer to member", errorstring);
2145 else if (pointer != error_mark_node)
2148 error ("invalid type argument of %qs", errorstring);
2150 error ("invalid type argument");
2152 return error_mark_node;
2155 /* This handles expressions of the form "a[i]", which denotes
2158 This is logically equivalent in C to *(a+i), but we may do it differently.
2159 If A is a variable or a member, we generate a primitive ARRAY_REF.
2160 This avoids forcing the array out of registers, and can work on
2161 arrays that are not lvalues (for example, members of structures returned
2164 If INDEX is of some user-defined type, it must be converted to
2165 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2166 will inherit the type of the array, which will be some pointer type. */
2169 build_array_ref (tree array, tree idx)
2173 error ("subscript missing in array reference");
2174 return error_mark_node;
2177 if (TREE_TYPE (array) == error_mark_node
2178 || TREE_TYPE (idx) == error_mark_node)
2179 return error_mark_node;
2181 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2183 switch (TREE_CODE (array))
2187 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2188 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
2189 TREE_OPERAND (array, 0), value);
2193 return build_conditional_expr
2194 (TREE_OPERAND (array, 0),
2195 build_array_ref (TREE_OPERAND (array, 1), idx),
2196 build_array_ref (TREE_OPERAND (array, 2), idx));
2202 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2206 /* Subscripting with type char is likely to lose
2207 on a machine where chars are signed.
2208 So warn on any machine, but optionally.
2209 Don't warn for unsigned char since that type is safe.
2210 Don't warn for signed char because anyone who uses that
2211 must have done so deliberately. */
2212 if (warn_char_subscripts
2213 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2214 warning ("array subscript has type %<char%>");
2216 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2218 error ("array subscript is not an integer");
2219 return error_mark_node;
2222 /* Apply integral promotions *after* noticing character types.
2223 (It is unclear why we do these promotions -- the standard
2224 does not say that we should. In fact, the natual thing would
2225 seem to be to convert IDX to ptrdiff_t; we're performing
2226 pointer arithmetic.) */
2227 idx = perform_integral_promotions (idx);
2229 /* An array that is indexed by a non-constant
2230 cannot be stored in a register; we must be able to do
2231 address arithmetic on its address.
2232 Likewise an array of elements of variable size. */
2233 if (TREE_CODE (idx) != INTEGER_CST
2234 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2235 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2238 if (!cxx_mark_addressable (array))
2239 return error_mark_node;
2242 /* An array that is indexed by a constant value which is not within
2243 the array bounds cannot be stored in a register either; because we
2244 would get a crash in store_bit_field/extract_bit_field when trying
2245 to access a non-existent part of the register. */
2246 if (TREE_CODE (idx) == INTEGER_CST
2247 && TYPE_DOMAIN (TREE_TYPE (array))
2248 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2250 if (!cxx_mark_addressable (array))
2251 return error_mark_node;
2254 if (pedantic && !lvalue_p (array))
2255 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2257 /* Note in C++ it is valid to subscript a `register' array, since
2258 it is valid to take the address of something with that
2259 storage specification. */
2263 while (TREE_CODE (foo) == COMPONENT_REF)
2264 foo = TREE_OPERAND (foo, 0);
2265 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2266 warning ("subscripting array declared %<register%>");
2269 type = TREE_TYPE (TREE_TYPE (array));
2270 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2271 /* Array ref is const/volatile if the array elements are
2272 or if the array is.. */
2273 TREE_READONLY (rval)
2274 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2275 TREE_SIDE_EFFECTS (rval)
2276 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2277 TREE_THIS_VOLATILE (rval)
2278 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2279 return require_complete_type (fold_if_not_in_template (rval));
2283 tree ar = default_conversion (array);
2284 tree ind = default_conversion (idx);
2286 /* Put the integer in IND to simplify error checking. */
2287 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2294 if (ar == error_mark_node)
2297 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2299 error ("subscripted value is neither array nor pointer");
2300 return error_mark_node;
2302 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2304 error ("array subscript is not an integer");
2305 return error_mark_node;
2308 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2313 /* Resolve a pointer to member function. INSTANCE is the object
2314 instance to use, if the member points to a virtual member.
2316 This used to avoid checking for virtual functions if basetype
2317 has no virtual functions, according to an earlier ANSI draft.
2318 With the final ISO C++ rules, such an optimization is
2319 incorrect: A pointer to a derived member can be static_cast
2320 to pointer-to-base-member, as long as the dynamic object
2321 later has the right member. */
2324 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
2326 if (TREE_CODE (function) == OFFSET_REF)
2327 function = TREE_OPERAND (function, 1);
2329 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2331 tree idx, delta, e1, e2, e3, vtbl, basetype;
2332 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2334 tree instance_ptr = *instance_ptrptr;
2335 tree instance_save_expr = 0;
2336 if (instance_ptr == error_mark_node)
2338 if (TREE_CODE (function) == PTRMEM_CST)
2340 /* Extracting the function address from a pmf is only
2341 allowed with -Wno-pmf-conversions. It only works for
2343 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2344 e1 = convert (fntype, e1);
2349 error ("object missing in use of %qE", function);
2350 return error_mark_node;
2354 if (TREE_SIDE_EFFECTS (instance_ptr))
2355 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2357 if (TREE_SIDE_EFFECTS (function))
2358 function = save_expr (function);
2360 /* Start by extracting all the information from the PMF itself. */
2361 e3 = PFN_FROM_PTRMEMFUNC (function);
2362 delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2363 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2364 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2366 case ptrmemfunc_vbit_in_pfn:
2367 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2368 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2371 case ptrmemfunc_vbit_in_delta:
2372 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2373 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2380 /* Convert down to the right base before using the instance. First
2382 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2383 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2384 basetype, ba_check, NULL);
2385 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1);
2386 if (instance_ptr == error_mark_node)
2387 return error_mark_node;
2388 /* ...and then the delta in the PMF. */
2389 instance_ptr = build2 (PLUS_EXPR, TREE_TYPE (instance_ptr),
2390 instance_ptr, delta);
2392 /* Hand back the adjusted 'this' argument to our caller. */
2393 *instance_ptrptr = instance_ptr;
2395 /* Next extract the vtable pointer from the object. */
2396 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2398 vtbl = build_indirect_ref (vtbl, NULL);
2400 /* Finally, extract the function pointer from the vtable. */
2401 e2 = fold (build2 (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx));
2402 e2 = build_indirect_ref (e2, NULL);
2403 TREE_CONSTANT (e2) = 1;
2404 TREE_INVARIANT (e2) = 1;
2406 /* When using function descriptors, the address of the
2407 vtable entry is treated as a function pointer. */
2408 if (TARGET_VTABLE_USES_DESCRIPTORS)
2409 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2410 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2412 TREE_TYPE (e2) = TREE_TYPE (e3);
2413 e1 = build_conditional_expr (e1, e2, e3);
2415 /* Make sure this doesn't get evaluated first inside one of the
2416 branches of the COND_EXPR. */
2417 if (instance_save_expr)
2418 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
2419 instance_save_expr, e1);
2427 build_function_call (tree function, tree params)
2429 tree fntype, fndecl;
2430 tree coerced_params;
2431 tree name = NULL_TREE;
2433 tree original = function;
2435 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2436 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2437 if (TREE_CODE (function) == NOP_EXPR
2438 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2439 function = TREE_OPERAND (function, 0);
2441 if (TREE_CODE (function) == FUNCTION_DECL)
2443 name = DECL_NAME (function);
2445 mark_used (function);
2448 /* Convert anything with function type to a pointer-to-function. */
2449 if (pedantic && DECL_MAIN_P (function))
2450 pedwarn ("ISO C++ forbids calling %<::main%> from within program");
2452 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2453 (because calling an inline function does not mean the function
2454 needs to be separately compiled). */
2456 if (DECL_INLINE (function))
2457 function = inline_conversion (function);
2459 function = build_addr_func (function);
2465 function = build_addr_func (function);
2468 if (function == error_mark_node)
2469 return error_mark_node;
2471 fntype = TREE_TYPE (function);
2473 if (TYPE_PTRMEMFUNC_P (fntype))
2475 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
2476 "function in %<%E (...)%>",
2478 return error_mark_node;
2481 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2482 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2484 if (!((TREE_CODE (fntype) == POINTER_TYPE
2485 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2487 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2489 error ("%qE cannot be used as a function", original);
2490 return error_mark_node;
2493 /* fntype now gets the type of function pointed to. */
2494 fntype = TREE_TYPE (fntype);
2496 /* Convert the parameters to the types declared in the
2497 function prototype, or apply default promotions. */
2499 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2500 params, fndecl, LOOKUP_NORMAL);
2501 if (coerced_params == error_mark_node)
2502 return error_mark_node;
2504 /* Check for errors in format strings and inappropriately
2507 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
2509 return build_cxx_call (function, coerced_params);
2512 /* Convert the actual parameter expressions in the list VALUES
2513 to the types in the list TYPELIST.
2514 If parmdecls is exhausted, or when an element has NULL as its type,
2515 perform the default conversions.
2517 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2519 This is also where warnings about wrong number of args are generated.
2521 Return a list of expressions for the parameters as converted.
2523 Both VALUES and the returned value are chains of TREE_LIST nodes
2524 with the elements of the list in the TREE_VALUE slots of those nodes.
2526 In C++, unspecified trailing parameters can be filled in with their
2527 default arguments, if such were specified. Do so here. */
2530 convert_arguments (tree typelist, tree values, tree fndecl, int flags)
2532 tree typetail, valtail;
2533 tree result = NULL_TREE;
2534 const char *called_thing = 0;
2537 /* Argument passing is always copy-initialization. */
2538 flags |= LOOKUP_ONLYCONVERTING;
2542 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2544 if (DECL_NAME (fndecl) == NULL_TREE
2545 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2546 called_thing = "constructor";
2548 called_thing = "member function";
2551 called_thing = "function";
2554 for (valtail = values, typetail = typelist;
2556 valtail = TREE_CHAIN (valtail), i++)
2558 tree type = typetail ? TREE_VALUE (typetail) : 0;
2559 tree val = TREE_VALUE (valtail);
2561 if (val == error_mark_node)
2562 return error_mark_node;
2564 if (type == void_type_node)
2568 cp_error_at ("too many arguments to %s %q+#D", called_thing,
2570 error ("at this point in file");
2573 error ("too many arguments to function");
2574 /* In case anybody wants to know if this argument
2577 TREE_TYPE (tree_last (result)) = error_mark_node;
2581 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2582 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2583 if (TREE_CODE (val) == NOP_EXPR
2584 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2585 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2586 val = TREE_OPERAND (val, 0);
2588 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2590 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2591 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2592 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2593 val = decay_conversion (val);
2596 if (val == error_mark_node)
2597 return error_mark_node;
2601 /* Formal parm type is specified by a function prototype. */
2604 if (!COMPLETE_TYPE_P (complete_type (type)))
2607 error ("parameter %P of %qD has incomplete type %qT",
2610 error ("parameter %P has incomplete type %qT", i, type);
2611 parmval = error_mark_node;
2615 parmval = convert_for_initialization
2616 (NULL_TREE, type, val, flags,
2617 "argument passing", fndecl, i);
2618 parmval = convert_for_arg_passing (type, parmval);
2621 if (parmval == error_mark_node)
2622 return error_mark_node;
2624 result = tree_cons (NULL_TREE, parmval, result);
2628 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2629 val = convert_from_reference (val);
2631 if (fndecl && DECL_BUILT_IN (fndecl)
2632 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2633 /* Don't do ellipsis conversion for __built_in_constant_p
2634 as this will result in spurious warnings for non-POD
2636 val = require_complete_type (val);
2638 val = convert_arg_to_ellipsis (val);
2640 result = tree_cons (NULL_TREE, val, result);
2644 typetail = TREE_CHAIN (typetail);
2647 if (typetail != 0 && typetail != void_list_node)
2649 /* See if there are default arguments that can be used. */
2650 if (TREE_PURPOSE (typetail)
2651 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
2653 for (; typetail != void_list_node; ++i)
2656 = convert_default_arg (TREE_VALUE (typetail),
2657 TREE_PURPOSE (typetail),
2660 if (parmval == error_mark_node)
2661 return error_mark_node;
2663 result = tree_cons (0, parmval, result);
2664 typetail = TREE_CHAIN (typetail);
2665 /* ends with `...'. */
2666 if (typetail == NULL_TREE)
2674 cp_error_at ("too few arguments to %s %q+#D",
2675 called_thing, fndecl);
2676 error ("at this point in file");
2679 error ("too few arguments to function");
2680 return error_mark_list;
2684 return nreverse (result);
2687 /* Build a binary-operation expression, after performing default
2688 conversions on the operands. CODE is the kind of expression to build. */
2691 build_x_binary_op (enum tree_code code, tree arg1, tree arg2,
2701 if (processing_template_decl)
2703 if (type_dependent_expression_p (arg1)
2704 || type_dependent_expression_p (arg2))
2705 return build_min_nt (code, arg1, arg2);
2706 arg1 = build_non_dependent_expr (arg1);
2707 arg2 = build_non_dependent_expr (arg2);
2710 if (code == DOTSTAR_EXPR)
2711 expr = build_m_component_ref (arg1, arg2);
2713 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
2716 if (processing_template_decl && expr != error_mark_node)
2717 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
2722 /* Build a binary-operation expression without default conversions.
2723 CODE is the kind of expression to build.
2724 This function differs from `build' in several ways:
2725 the data type of the result is computed and recorded in it,
2726 warnings are generated if arg data types are invalid,
2727 special handling for addition and subtraction of pointers is known,
2728 and some optimization is done (operations on narrow ints
2729 are done in the narrower type when that gives the same result).
2730 Constant folding is also done before the result is returned.
2732 Note that the operands will never have enumeral types
2733 because either they have just had the default conversions performed
2734 or they have both just been converted to some other type in which
2735 the arithmetic is to be done.
2737 C++: must do special pointer arithmetic when implementing
2738 multiple inheritance, and deal with pointer to member functions. */
2741 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
2742 int convert_p ATTRIBUTE_UNUSED)
2745 enum tree_code code0, code1;
2748 /* Expression code to give to the expression when it is built.
2749 Normally this is CODE, which is what the caller asked for,
2750 but in some special cases we change it. */
2751 enum tree_code resultcode = code;
2753 /* Data type in which the computation is to be performed.
2754 In the simplest cases this is the common type of the arguments. */
2755 tree result_type = NULL;
2757 /* Nonzero means operands have already been type-converted
2758 in whatever way is necessary.
2759 Zero means they need to be converted to RESULT_TYPE. */
2762 /* Nonzero means create the expression with this type, rather than
2764 tree build_type = 0;
2766 /* Nonzero means after finally constructing the expression
2767 convert it to this type. */
2768 tree final_type = 0;
2772 /* Nonzero if this is an operation like MIN or MAX which can
2773 safely be computed in short if both args are promoted shorts.
2774 Also implies COMMON.
2775 -1 indicates a bitwise operation; this makes a difference
2776 in the exact conditions for when it is safe to do the operation
2777 in a narrower mode. */
2780 /* Nonzero if this is a comparison operation;
2781 if both args are promoted shorts, compare the original shorts.
2782 Also implies COMMON. */
2783 int short_compare = 0;
2785 /* Nonzero if this is a right-shift operation, which can be computed on the
2786 original short and then promoted if the operand is a promoted short. */
2787 int short_shift = 0;
2789 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2792 /* True if both operands have arithmetic type. */
2793 bool arithmetic_types_p;
2795 /* Apply default conversions. */
2799 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2800 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2801 || code == TRUTH_XOR_EXPR)
2803 if (!really_overloaded_fn (op0))
2804 op0 = decay_conversion (op0);
2805 if (!really_overloaded_fn (op1))
2806 op1 = decay_conversion (op1);
2810 if (!really_overloaded_fn (op0))
2811 op0 = default_conversion (op0);
2812 if (!really_overloaded_fn (op1))
2813 op1 = default_conversion (op1);
2816 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2817 STRIP_TYPE_NOPS (op0);
2818 STRIP_TYPE_NOPS (op1);
2820 /* DTRT if one side is an overloaded function, but complain about it. */
2821 if (type_unknown_p (op0))
2823 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
2824 if (t != error_mark_node)
2826 pedwarn ("assuming cast to type %qT from overloaded function",
2831 if (type_unknown_p (op1))
2833 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
2834 if (t != error_mark_node)
2836 pedwarn ("assuming cast to type %qT from overloaded function",
2842 type0 = TREE_TYPE (op0);
2843 type1 = TREE_TYPE (op1);
2845 /* The expression codes of the data types of the arguments tell us
2846 whether the arguments are integers, floating, pointers, etc. */
2847 code0 = TREE_CODE (type0);
2848 code1 = TREE_CODE (type1);
2850 /* If an error was already reported for one of the arguments,
2851 avoid reporting another error. */
2853 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2854 return error_mark_node;
2859 /* Handle the pointer + int case. */
2860 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2861 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
2862 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2863 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
2869 /* Subtraction of two similar pointers.
2870 We must subtract them as integers, then divide by object size. */
2871 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2872 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
2874 return pointer_diff (op0, op1, common_type (type0, type1));
2875 /* Handle pointer minus int. Just like pointer plus int. */
2876 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2877 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
2886 case TRUNC_DIV_EXPR:
2888 case FLOOR_DIV_EXPR:
2889 case ROUND_DIV_EXPR:
2890 case EXACT_DIV_EXPR:
2891 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2892 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
2893 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2894 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
2896 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
2897 warning ("division by zero in %<%E / 0%>", op0);
2898 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
2899 warning ("division by zero in %<%E / 0.%>", op0);
2901 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
2902 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
2903 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
2904 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
2906 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2907 resultcode = RDIV_EXPR;
2909 /* When dividing two signed integers, we have to promote to int.
2910 unless we divide by a constant != -1. Note that default
2911 conversion will have been performed on the operands at this
2912 point, so we have to dig out the original type to find out if
2914 shorten = ((TREE_CODE (op0) == NOP_EXPR
2915 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2916 || (TREE_CODE (op1) == INTEGER_CST
2917 && ! integer_all_onesp (op1)));
2926 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2930 case TRUNC_MOD_EXPR:
2931 case FLOOR_MOD_EXPR:
2932 if (code1 == INTEGER_TYPE && integer_zerop (op1))
2933 warning ("division by zero in %<%E %% 0%>", op0);
2934 else if (code1 == REAL_TYPE && real_zerop (op1))
2935 warning ("division by zero in %<%E %% 0.%>", op0);
2937 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2939 /* Although it would be tempting to shorten always here, that loses
2940 on some targets, since the modulo instruction is undefined if the
2941 quotient can't be represented in the computation mode. We shorten
2942 only if unsigned or if dividing by something we know != -1. */
2943 shorten = ((TREE_CODE (op0) == NOP_EXPR
2944 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2945 || (TREE_CODE (op1) == INTEGER_CST
2946 && ! integer_all_onesp (op1)));
2951 case TRUTH_ANDIF_EXPR:
2952 case TRUTH_ORIF_EXPR:
2953 case TRUTH_AND_EXPR:
2955 result_type = boolean_type_node;
2958 /* Shift operations: result has same type as first operand;
2959 always convert second operand to int.
2960 Also set SHORT_SHIFT if shifting rightward. */
2963 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2965 result_type = type0;
2966 if (TREE_CODE (op1) == INTEGER_CST)
2968 if (tree_int_cst_lt (op1, integer_zero_node))
2969 warning ("right shift count is negative");
2972 if (! integer_zerop (op1))
2974 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2975 warning ("right shift count >= width of type");
2978 /* Convert the shift-count to an integer, regardless of
2979 size of value being shifted. */
2980 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2981 op1 = cp_convert (integer_type_node, op1);
2982 /* Avoid converting op1 to result_type later. */
2988 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2990 result_type = type0;
2991 if (TREE_CODE (op1) == INTEGER_CST)
2993 if (tree_int_cst_lt (op1, integer_zero_node))
2994 warning ("left shift count is negative");
2995 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2996 warning ("left shift count >= width of type");
2998 /* Convert the shift-count to an integer, regardless of
2999 size of value being shifted. */
3000 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3001 op1 = cp_convert (integer_type_node, op1);
3002 /* Avoid converting op1 to result_type later. */
3009 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3011 result_type = type0;
3012 if (TREE_CODE (op1) == INTEGER_CST)
3014 if (tree_int_cst_lt (op1, integer_zero_node))
3015 warning ("%s rotate count is negative",
3016 (code == LROTATE_EXPR) ? "left" : "right");
3017 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3018 warning ("%s rotate count >= width of type",
3019 (code == LROTATE_EXPR) ? "left" : "right");
3021 /* Convert the shift-count to an integer, regardless of
3022 size of value being shifted. */
3023 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3024 op1 = cp_convert (integer_type_node, op1);
3030 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3031 warning ("comparing floating point with == or != is unsafe");
3033 build_type = boolean_type_node;
3034 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3035 || code0 == COMPLEX_TYPE)
3036 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3037 || code1 == COMPLEX_TYPE))
3039 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3040 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
3041 result_type = composite_pointer_type (type0, type1, op0, op1,
3043 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
3044 && null_ptr_cst_p (op1))
3045 result_type = type0;
3046 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
3047 && null_ptr_cst_p (op0))
3048 result_type = type1;
3049 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3051 result_type = type0;
3052 error ("ISO C++ forbids comparison between pointer and integer");
3054 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3056 result_type = type1;
3057 error ("ISO C++ forbids comparison between pointer and integer");
3059 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3061 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3062 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3063 result_type = TREE_TYPE (op0);
3065 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3066 return cp_build_binary_op (code, op1, op0);
3067 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3068 && same_type_p (type0, type1))
3070 /* E will be the final comparison. */
3072 /* E1 and E2 are for scratch. */
3080 if (TREE_SIDE_EFFECTS (op0))
3081 op0 = save_expr (op0);
3082 if (TREE_SIDE_EFFECTS (op1))
3083 op1 = save_expr (op1);
3088 && (!op0.pfn || op0.delta == op1.delta))
3090 The reason for the `!op0.pfn' bit is that a NULL
3091 pointer-to-member is any member with a zero PFN; the
3092 DELTA field is unspecified. */
3093 pfn0 = pfn_from_ptrmemfunc (op0);
3094 pfn1 = pfn_from_ptrmemfunc (op1);
3095 delta0 = build_ptrmemfunc_access_expr (op0,
3097 delta1 = build_ptrmemfunc_access_expr (op1,
3099 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3100 e2 = cp_build_binary_op (EQ_EXPR,
3102 cp_convert (TREE_TYPE (pfn0),
3103 integer_zero_node));
3104 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3105 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3106 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3107 if (code == EQ_EXPR)
3109 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3113 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
3114 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
3116 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
3117 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
3125 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3126 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3128 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3129 result_type = composite_pointer_type (type0, type1, op0, op1,
3137 build_type = boolean_type_node;
3138 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3139 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3141 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3142 result_type = composite_pointer_type (type0, type1, op0, op1,
3144 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3145 && integer_zerop (op1))
3146 result_type = type0;
3147 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3148 && integer_zerop (op0))
3149 result_type = type1;
3150 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3152 result_type = type0;
3153 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3155 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3157 result_type = type1;
3158 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3162 case UNORDERED_EXPR:
3169 build_type = integer_type_node;
3170 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3172 error ("unordered comparison on non-floating point argument");
3173 return error_mark_node;
3182 arithmetic_types_p =
3183 ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3184 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3185 || code1 == COMPLEX_TYPE));
3186 /* Determine the RESULT_TYPE, if it is not already known. */
3188 && arithmetic_types_p
3189 && (shorten || common || short_compare))
3190 result_type = common_type (type0, type1);
3194 error ("invalid operands of types %qT and %qT to binary %qO",
3195 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3196 return error_mark_node;
3199 /* If we're in a template, the only thing we need to know is the
3201 if (processing_template_decl)
3202 return build2 (resultcode,
3203 build_type ? build_type : result_type,
3206 if (arithmetic_types_p)
3208 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3210 /* For certain operations (which identify themselves by shorten != 0)
3211 if both args were extended from the same smaller type,
3212 do the arithmetic in that type and then extend.
3214 shorten !=0 and !=1 indicates a bitwise operation.
3215 For them, this optimization is safe only if
3216 both args are zero-extended or both are sign-extended.
3217 Otherwise, we might change the result.
3218 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3219 but calculated in (unsigned short) it would be (unsigned short)-1. */
3221 if (shorten && none_complex)
3223 int unsigned0, unsigned1;
3224 tree arg0 = get_narrower (op0, &unsigned0);
3225 tree arg1 = get_narrower (op1, &unsigned1);
3226 /* UNS is 1 if the operation to be done is an unsigned one. */
3227 int uns = TYPE_UNSIGNED (result_type);
3230 final_type = result_type;
3232 /* Handle the case that OP0 does not *contain* a conversion
3233 but it *requires* conversion to FINAL_TYPE. */
3235 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3236 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3237 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3238 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3240 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3242 /* For bitwise operations, signedness of nominal type
3243 does not matter. Consider only how operands were extended. */
3247 /* Note that in all three cases below we refrain from optimizing
3248 an unsigned operation on sign-extended args.
3249 That would not be valid. */
3251 /* Both args variable: if both extended in same way
3252 from same width, do it in that width.
3253 Do it unsigned if args were zero-extended. */
3254 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3255 < TYPE_PRECISION (result_type))
3256 && (TYPE_PRECISION (TREE_TYPE (arg1))
3257 == TYPE_PRECISION (TREE_TYPE (arg0)))
3258 && unsigned0 == unsigned1
3259 && (unsigned0 || !uns))
3260 result_type = c_common_signed_or_unsigned_type
3261 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3262 else if (TREE_CODE (arg0) == INTEGER_CST
3263 && (unsigned1 || !uns)
3264 && (TYPE_PRECISION (TREE_TYPE (arg1))
3265 < TYPE_PRECISION (result_type))
3266 && (type = c_common_signed_or_unsigned_type
3267 (unsigned1, TREE_TYPE (arg1)),
3268 int_fits_type_p (arg0, type)))
3270 else if (TREE_CODE (arg1) == INTEGER_CST
3271 && (unsigned0 || !uns)
3272 && (TYPE_PRECISION (TREE_TYPE (arg0))
3273 < TYPE_PRECISION (result_type))
3274 && (type = c_common_signed_or_unsigned_type
3275 (unsigned0, TREE_TYPE (arg0)),
3276 int_fits_type_p (arg1, type)))
3280 /* Shifts can be shortened if shifting right. */
3285 tree arg0 = get_narrower (op0, &unsigned_arg);
3287 final_type = result_type;
3289 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3290 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
3292 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3293 /* We can shorten only if the shift count is less than the
3294 number of bits in the smaller type size. */
3295 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3296 /* If arg is sign-extended and then unsigned-shifted,
3297 we can simulate this with a signed shift in arg's type
3298 only if the extended result is at least twice as wide
3299 as the arg. Otherwise, the shift could use up all the
3300 ones made by sign-extension and bring in zeros.
3301 We can't optimize that case at all, but in most machines
3302 it never happens because available widths are 2**N. */
3303 && (!TYPE_UNSIGNED (final_type)
3305 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3306 <= TYPE_PRECISION (result_type))))
3308 /* Do an unsigned shift if the operand was zero-extended. */
3310 = c_common_signed_or_unsigned_type (unsigned_arg,
3312 /* Convert value-to-be-shifted to that type. */
3313 if (TREE_TYPE (op0) != result_type)
3314 op0 = cp_convert (result_type, op0);
3319 /* Comparison operations are shortened too but differently.
3320 They identify themselves by setting short_compare = 1. */
3324 /* Don't write &op0, etc., because that would prevent op0
3325 from being kept in a register.
3326 Instead, make copies of the our local variables and
3327 pass the copies by reference, then copy them back afterward. */
3328 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3329 enum tree_code xresultcode = resultcode;
3331 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3333 return cp_convert (boolean_type_node, val);
3334 op0 = xop0, op1 = xop1;
3336 resultcode = xresultcode;
3339 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3340 && warn_sign_compare
3341 /* Do not warn until the template is instantiated; we cannot
3342 bound the ranges of the arguments until that point. */
3343 && !processing_template_decl)
3345 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
3346 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3348 int unsignedp0, unsignedp1;
3349 tree primop0 = get_narrower (op0, &unsignedp0);
3350 tree primop1 = get_narrower (op1, &unsignedp1);
3352 /* Check for comparison of different enum types. */
3353 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3354 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3355 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3356 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3358 warning ("comparison between types %q#T and %q#T",
3359 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3362 /* Give warnings for comparisons between signed and unsigned
3363 quantities that may fail. */
3364 /* Do the checking based on the original operand trees, so that
3365 casts will be considered, but default promotions won't be. */
3367 /* Do not warn if the comparison is being done in a signed type,
3368 since the signed type will only be chosen if it can represent
3369 all the values of the unsigned type. */
3370 if (!TYPE_UNSIGNED (result_type))
3372 /* Do not warn if both operands are unsigned. */
3373 else if (op0_signed == op1_signed)
3375 /* Do not warn if the signed quantity is an unsuffixed
3376 integer literal (or some static constant expression
3377 involving such literals or a conditional expression
3378 involving such literals) and it is non-negative. */
3379 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3380 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3382 /* Do not warn if the comparison is an equality operation,
3383 the unsigned quantity is an integral constant and it does
3384 not use the most significant bit of result_type. */
3385 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3386 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3387 && int_fits_type_p (orig_op1, c_common_signed_type
3389 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3390 && int_fits_type_p (orig_op0, c_common_signed_type
3394 warning ("comparison between signed and unsigned integer expressions");
3396 /* Warn if two unsigned values are being compared in a size
3397 larger than their original size, and one (and only one) is the
3398 result of a `~' operator. This comparison will always fail.
3400 Also warn if one operand is a constant, and the constant does not
3401 have all bits set that are set in the ~ operand when it is
3404 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3405 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3407 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3408 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3409 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3410 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3412 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3415 HOST_WIDE_INT constant, mask;
3419 if (host_integerp (primop0, 0))
3422 unsignedp = unsignedp1;
3423 constant = tree_low_cst (primop0, 0);
3428 unsignedp = unsignedp0;
3429 constant = tree_low_cst (primop1, 0);
3432 bits = TYPE_PRECISION (TREE_TYPE (primop));
3433 if (bits < TYPE_PRECISION (result_type)
3434 && bits < HOST_BITS_PER_LONG && unsignedp)
3436 mask = (~ (HOST_WIDE_INT) 0) << bits;
3437 if ((mask & constant) != mask)
3438 warning ("comparison of promoted ~unsigned with constant");
3441 else if (unsignedp0 && unsignedp1
3442 && (TYPE_PRECISION (TREE_TYPE (primop0))
3443 < TYPE_PRECISION (result_type))
3444 && (TYPE_PRECISION (TREE_TYPE (primop1))
3445 < TYPE_PRECISION (result_type)))
3446 warning ("comparison of promoted ~unsigned with unsigned");
3451 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3452 Then the expression will be built.
3453 It will be given type FINAL_TYPE if that is nonzero;
3454 otherwise, it will be given type RESULT_TYPE. */
3456 /* Issue warnings about peculiar, but valid, uses of NULL. */
3457 if (/* It's reasonable to use pointer values as operands of &&
3458 and ||, so NULL is no exception. */
3459 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3460 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3461 (orig_op0 == null_node
3462 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3463 /* Or vice versa. */
3464 || (orig_op1 == null_node
3465 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3466 /* Or, both are NULL and the operation was not a comparison. */
3467 || (orig_op0 == null_node && orig_op1 == null_node
3468 && code != EQ_EXPR && code != NE_EXPR)))
3469 /* Some sort of arithmetic operation involving NULL was
3470 performed. Note that pointer-difference and pointer-addition
3471 have already been handled above, and so we don't end up here in
3473 warning ("NULL used in arithmetic");
3477 if (TREE_TYPE (op0) != result_type)
3478 op0 = cp_convert (result_type, op0);
3479 if (TREE_TYPE (op1) != result_type)
3480 op1 = cp_convert (result_type, op1);
3482 if (op0 == error_mark_node || op1 == error_mark_node)
3483 return error_mark_node;
3486 if (build_type == NULL_TREE)
3487 build_type = result_type;
3489 result = build2 (resultcode, build_type, op0, op1);
3490 result = fold_if_not_in_template (result);
3491 if (final_type != 0)
3492 result = cp_convert (final_type, result);
3496 /* Return a tree for the sum or difference (RESULTCODE says which)
3497 of pointer PTROP and integer INTOP. */
3500 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
3502 tree res_type = TREE_TYPE (ptrop);
3504 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3505 in certain circumstance (when it's valid to do so). So we need
3506 to make sure it's complete. We don't need to check here, if we
3507 can actually complete it at all, as those checks will be done in
3508 pointer_int_sum() anyway. */
3509 complete_type (TREE_TYPE (res_type));
3511 return pointer_int_sum (resultcode, ptrop,
3512 fold_if_not_in_template (intop));
3515 /* Return a tree for the difference of pointers OP0 and OP1.
3516 The resulting tree has type int. */
3519 pointer_diff (tree op0, tree op1, tree ptrtype)
3522 tree restype = ptrdiff_type_node;
3523 tree target_type = TREE_TYPE (ptrtype);
3525 if (!complete_type_or_else (target_type, NULL_TREE))
3526 return error_mark_node;
3528 if (pedantic || warn_pointer_arith)
3530 if (TREE_CODE (target_type) == VOID_TYPE)
3531 pedwarn ("ISO C++ forbids using pointer of type %<void *%> in subtraction");
3532 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3533 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3534 if (TREE_CODE (target_type) == METHOD_TYPE)
3535 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3538 /* First do the subtraction as integers;
3539 then drop through to build the divide operator. */
3541 op0 = cp_build_binary_op (MINUS_EXPR,
3542 cp_convert (restype, op0),
3543 cp_convert (restype, op1));
3545 /* This generates an error if op1 is a pointer to an incomplete type. */
3546 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3547 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3549 op1 = (TYPE_PTROB_P (ptrtype)
3550 ? size_in_bytes (target_type)
3551 : integer_one_node);
3553 /* Do the division. */
3555 result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3556 return fold_if_not_in_template (result);
3559 /* Construct and perhaps optimize a tree representation
3560 for a unary operation. CODE, a tree_code, specifies the operation
3561 and XARG is the operand. */
3564 build_x_unary_op (enum tree_code code, tree xarg)
3566 tree orig_expr = xarg;
3570 if (processing_template_decl)
3572 if (type_dependent_expression_p (xarg))
3573 return build_min_nt (code, xarg, NULL_TREE);
3575 /* For non-dependent pointer-to-member, the SCOPE_REF will be
3576 processed during template substitution. Just compute the
3577 right type here and build an ADDR_EXPR around it for
3579 if (code == ADDR_EXPR && TREE_CODE (xarg) == SCOPE_REF)
3582 if (TREE_TYPE (xarg) == unknown_type_node)
3583 type = unknown_type_node;
3584 else if (TREE_CODE (TREE_TYPE (xarg)) == FUNCTION_TYPE)
3585 type = build_pointer_type (TREE_TYPE (xarg));
3587 type = build_ptrmem_type (TREE_OPERAND (xarg, 0),
3589 return build_min (code, type, xarg, NULL_TREE);
3592 xarg = build_non_dependent_expr (xarg);
3597 /* [expr.unary.op] says:
3599 The address of an object of incomplete type can be taken.
3601 (And is just the ordinary address operator, not an overloaded
3602 "operator &".) However, if the type is a template
3603 specialization, we must complete the type at this point so that
3604 an overloaded "operator &" will be available if required. */
3605 if (code == ADDR_EXPR
3606 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3607 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
3608 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
3609 || (TREE_CODE (xarg) == OFFSET_REF)))
3610 /* Don't look for a function. */;
3612 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
3613 /*overloaded_p=*/NULL);
3614 if (!exp && code == ADDR_EXPR)
3616 /* A pointer to member-function can be formed only by saying
3618 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
3619 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
3621 if (TREE_CODE (xarg) != OFFSET_REF)
3623 error ("invalid use of %qE to form a pointer-to-member-function."
3624 " Use a qualified-id.",
3626 return error_mark_node;
3630 error ("parenthesis around %qE cannot be used to form a"
3631 " pointer-to-member-function",
3633 PTRMEM_OK_P (xarg) = 1;
3637 if (TREE_CODE (xarg) == OFFSET_REF)
3639 ptrmem = PTRMEM_OK_P (xarg);
3641 if (!ptrmem && !flag_ms_extensions
3642 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
3644 /* A single non-static member, make sure we don't allow a
3645 pointer-to-member. */
3646 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
3647 TREE_OPERAND (xarg, 0),
3648 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
3649 PTRMEM_OK_P (xarg) = ptrmem;
3652 else if (TREE_CODE (xarg) == TARGET_EXPR)
3653 warning ("taking address of temporary");
3654 exp = build_unary_op (ADDR_EXPR, xarg, 0);
3655 if (TREE_CODE (exp) == ADDR_EXPR)
3656 PTRMEM_OK_P (exp) = ptrmem;
3659 if (processing_template_decl && exp != error_mark_node)
3660 return build_min_non_dep (code, exp, orig_expr,
3661 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
3665 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
3666 constants, where a null value is represented by an INTEGER_CST of
3670 cp_truthvalue_conversion (tree expr)
3672 tree type = TREE_TYPE (expr);
3673 if (TYPE_PTRMEM_P (type))
3674 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3676 return c_common_truthvalue_conversion (expr);
3679 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3682 condition_conversion (tree expr)
3685 if (processing_template_decl)
3687 t = perform_implicit_conversion (boolean_type_node, expr);
3688 t = fold_build_cleanup_point_expr (boolean_type_node, t);
3692 /* Return an ADDR_EXPR giving the address of T. This function
3693 attempts no optimizations or simplifications; it is a low-level
3697 build_address (tree t)
3701 if (error_operand_p (t) || !cxx_mark_addressable (t))
3702 return error_mark_node;
3704 addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
3709 /* Return a NOP_EXPR converting EXPR to TYPE. */
3712 build_nop (tree type, tree expr)
3714 if (type == error_mark_node || error_operand_p (expr))
3716 return build1 (NOP_EXPR, type, expr);
3719 /* C++: Must handle pointers to members.
3721 Perhaps type instantiation should be extended to handle conversion
3722 from aggregates to types we don't yet know we want? (Or are those
3723 cases typically errors which should be reported?)
3725 NOCONVERT nonzero suppresses the default promotions
3726 (such as from short to int). */
3729 build_unary_op (enum tree_code code, tree xarg, int noconvert)
3731 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3734 const char *errstring = NULL;
3737 if (arg == error_mark_node)
3738 return error_mark_node;
3743 /* This is used for unary plus, because a CONVERT_EXPR
3744 is enough to prevent anybody from looking inside for
3745 associativity, but won't generate any code. */
3746 if (!(arg = build_expr_type_conversion
3747 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, true)))
3748 errstring = "wrong type argument to unary plus";
3752 arg = default_conversion (arg);
3753 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
3758 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3759 errstring = "wrong type argument to unary minus";
3760 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
3761 arg = perform_integral_promotions (arg);
3765 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3769 arg = default_conversion (arg);
3771 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
3773 errstring = "wrong type argument to bit-complement";
3774 else if (!noconvert)
3775 arg = perform_integral_promotions (arg);
3779 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3780 errstring = "wrong type argument to abs";
3781 else if (!noconvert)
3782 arg = default_conversion (arg);
3786 /* Conjugating a real value is a no-op, but allow it anyway. */
3787 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3788 errstring = "wrong type argument to conjugation";
3789 else if (!noconvert)
3790 arg = default_conversion (arg);
3793 case TRUTH_NOT_EXPR:
3794 arg = perform_implicit_conversion (boolean_type_node, arg);
3795 val = invert_truthvalue (arg);
3796 if (arg != error_mark_node)
3798 errstring = "in argument to unary !";
3805 if (TREE_CODE (arg) == COMPLEX_CST)
3806 return TREE_REALPART (arg);
3807 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3809 arg = build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3810 return fold_if_not_in_template (arg);
3816 if (TREE_CODE (arg) == COMPLEX_CST)
3817 return TREE_IMAGPART (arg);
3818 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3820 arg = build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3821 return fold_if_not_in_template (arg);
3824 return cp_convert (TREE_TYPE (arg), integer_zero_node);
3826 case PREINCREMENT_EXPR:
3827 case POSTINCREMENT_EXPR:
3828 case PREDECREMENT_EXPR:
3829 case POSTDECREMENT_EXPR:
3830 /* Handle complex lvalues (when permitted)
3831 by reduction to simpler cases. */
3833 val = unary_complex_lvalue (code, arg);
3837 /* Increment or decrement the real part of the value,
3838 and don't change the imaginary part. */
3839 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3843 arg = stabilize_reference (arg);
3844 real = build_unary_op (REALPART_EXPR, arg, 1);
3845 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
3846 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3847 build_unary_op (code, real, 1), imag);
3850 /* Report invalid types. */
3852 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
3855 if (code == PREINCREMENT_EXPR)
3856 errstring ="no pre-increment operator for type";
3857 else if (code == POSTINCREMENT_EXPR)
3858 errstring ="no post-increment operator for type";
3859 else if (code == PREDECREMENT_EXPR)
3860 errstring ="no pre-decrement operator for type";
3862 errstring ="no post-decrement operator for type";
3866 /* Report something read-only. */
3868 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
3869 || TREE_READONLY (arg))
3870 readonly_error (arg, ((code == PREINCREMENT_EXPR
3871 || code == POSTINCREMENT_EXPR)
3872 ? "increment" : "decrement"),
3877 tree result_type = TREE_TYPE (arg);
3879 arg = get_unwidened (arg, 0);
3880 argtype = TREE_TYPE (arg);
3882 /* ARM $5.2.5 last annotation says this should be forbidden. */
3883 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
3884 pedwarn ("ISO C++ forbids %sing an enum",
3885 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3886 ? "increment" : "decrement");
3888 /* Compute the increment. */
3890 if (TREE_CODE (argtype) == POINTER_TYPE)
3892 tree type = complete_type (TREE_TYPE (argtype));
3894 if (!COMPLETE_OR_VOID_TYPE_P (type))
3895 error ("cannot %s a pointer to incomplete type %qT",
3896 ((code == PREINCREMENT_EXPR
3897 || code == POSTINCREMENT_EXPR)
3898 ? "increment" : "decrement"), TREE_TYPE (argtype));
3899 else if ((pedantic || warn_pointer_arith)
3900 && !TYPE_PTROB_P (argtype))
3901 pedwarn ("ISO C++ forbids %sing a pointer of type %qT",
3902 ((code == PREINCREMENT_EXPR
3903 || code == POSTINCREMENT_EXPR)
3904 ? "increment" : "decrement"), argtype);
3905 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
3908 inc = integer_one_node;
3910 inc = cp_convert (argtype, inc);
3912 /* Handle incrementing a cast-expression. */
3914 switch (TREE_CODE (arg))
3919 case FIX_TRUNC_EXPR:
3920 case FIX_FLOOR_EXPR:
3921 case FIX_ROUND_EXPR:
3924 tree incremented, modify, value, compound;
3925 if (! lvalue_p (arg) && pedantic)
3926 pedwarn ("cast to non-reference type used as lvalue");
3927 arg = stabilize_reference (arg);
3928 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3931 value = save_expr (arg);
3932 incremented = build2 (((code == PREINCREMENT_EXPR
3933 || code == POSTINCREMENT_EXPR)
3934 ? PLUS_EXPR : MINUS_EXPR),
3935 argtype, value, inc);
3937 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3938 compound = build2 (COMPOUND_EXPR, TREE_TYPE (arg),
3941 /* Eliminate warning about unused result of + or -. */
3942 TREE_NO_WARNING (compound) = 1;
3950 /* Complain about anything else that is not a true lvalue. */
3951 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3952 || code == POSTINCREMENT_EXPR)
3953 ? "increment" : "decrement")))
3954 return error_mark_node;
3956 /* Forbid using -- on `bool'. */
3957 if (TREE_TYPE (arg) == boolean_type_node)
3959 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
3961 error ("invalid use of %<--%> on bool variable %qD", arg);
3962 return error_mark_node;
3964 val = boolean_increment (code, arg);
3967 val = build2 (code, TREE_TYPE (arg), arg, inc);
3969 TREE_SIDE_EFFECTS (val) = 1;
3970 return cp_convert (result_type, val);
3974 /* Note that this operation never does default_conversion
3975 regardless of NOCONVERT. */
3977 argtype = lvalue_type (arg);
3979 if (TREE_CODE (arg) == OFFSET_REF)
3982 if (TREE_CODE (argtype) == REFERENCE_TYPE)
3984 tree type = build_pointer_type (TREE_TYPE (argtype));
3985 arg = build1 (CONVERT_EXPR, type, arg);
3988 else if (pedantic && DECL_MAIN_P (arg))
3990 pedwarn ("ISO C++ forbids taking address of function %<::main%>");
3992 /* Let &* cancel out to simplify resulting code. */
3993 if (TREE_CODE (arg) == INDIRECT_REF)
3995 /* We don't need to have `current_class_ptr' wrapped in a
3996 NON_LVALUE_EXPR node. */
3997 if (arg == current_class_ref)
3998 return current_class_ptr;
4000 arg = TREE_OPERAND (arg, 0);
4001 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4003 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
4004 arg = build1 (CONVERT_EXPR, type, arg);
4006 else if (lvalue_p (arg))
4007 /* Don't let this be an lvalue. */
4008 return non_lvalue (arg);
4012 /* Uninstantiated types are all functions. Taking the
4013 address of a function is a no-op, so just return the
4016 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4017 || !IDENTIFIER_OPNAME_P (arg));
4019 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4020 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4022 /* They're trying to take the address of a unique non-static
4023 member function. This is ill-formed (except in MS-land),
4024 but let's try to DTRT.
4025 Note: We only handle unique functions here because we don't
4026 want to complain if there's a static overload; non-unique
4027 cases will be handled by instantiate_type. But we need to
4028 handle this case here to allow casts on the resulting PMF.
4029 We could defer this in non-MS mode, but it's easier to give
4030 a useful error here. */
4032 /* Inside constant member functions, the `this' pointer
4033 contains an extra const qualifier. TYPE_MAIN_VARIANT
4034 is used here to remove this const from the diagnostics
4035 and the created OFFSET_REF. */
4036 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4037 tree name = DECL_NAME (get_first_fn (TREE_OPERAND (arg, 1)));
4039 if (! flag_ms_extensions)
4041 if (current_class_type
4042 && TREE_OPERAND (arg, 0) == current_class_ref)
4043 /* An expression like &memfn. */
4044 pedwarn ("ISO C++ forbids taking the address of an unqualified"
4045 " or parenthesized non-static member function to form"
4046 " a pointer to member function. Say %<&%T::%D%>",
4049 pedwarn ("ISO C++ forbids taking the address of a bound member"
4050 " function to form a pointer to member function."
4054 arg = build_offset_ref (base, name, /*address_p=*/true);
4058 if (type_unknown_p (arg))
4059 return build1 (ADDR_EXPR, unknown_type_node, arg);
4061 /* Handle complex lvalues (when permitted)
4062 by reduction to simpler cases. */
4063 val = unary_complex_lvalue (code, arg);
4067 switch (TREE_CODE (arg))
4072 case FIX_TRUNC_EXPR:
4073 case FIX_FLOOR_EXPR:
4074 case FIX_ROUND_EXPR:
4076 if (! lvalue_p (arg) && pedantic)
4077 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4081 arg = OVL_CURRENT (arg);
4088 /* Allow the address of a constructor if all the elements
4090 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4091 && TREE_CONSTANT (arg))
4093 /* Anything not already handled and not a true memory reference
4095 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4096 && TREE_CODE (argtype) != METHOD_TYPE
4097 && !lvalue_or_else (arg, "unary %<&$>"))
4098 return error_mark_node;
4100 if (argtype != error_mark_node)
4101 argtype = build_pointer_type (argtype);
4106 if (TREE_CODE (arg) != COMPONENT_REF
4107 /* Inside a template, we are processing a non-dependent
4108 expression so we can just form an ADDR_EXPR with the
4110 || processing_template_decl)
4111 addr = build_address (arg);
4112 else if (TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
4114 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4116 /* We can only get here with a single static member
4118 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4119 && DECL_STATIC_FUNCTION_P (fn));
4121 addr = build_address (fn);
4122 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
4123 /* Do not lose object's side effects. */
4124 addr = build2 (COMPOUND_EXPR, TREE_TYPE (addr),
4125 TREE_OPERAND (arg, 0), addr);
4127 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4129 error ("attempt to take address of bit-field structure member %qD",
4130 TREE_OPERAND (arg, 1));
4131 return error_mark_node;
4135 tree field = TREE_OPERAND (arg, 1);
4136 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4137 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)),
4138 decl_type_context (field),
4141 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
4143 TREE_OPERAND (arg, 0) = build_indirect_ref (rval, NULL);
4144 addr = build_address (arg);
4147 if (TREE_CODE (argtype) == POINTER_TYPE
4148 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4150 build_ptrmemfunc_type (argtype);
4151 addr = build_ptrmemfunc (argtype, addr, 0,
4152 /*c_cast_p=*/false);
4165 argtype = TREE_TYPE (arg);
4166 return fold_if_not_in_template (build1 (code, argtype, arg));
4169 error ("%s", errstring);
4170 return error_mark_node;
4173 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4174 for certain kinds of expressions which are not really lvalues
4175 but which we can accept as lvalues.
4177 If ARG is not a kind of expression we can handle, return zero. */
4180 unary_complex_lvalue (enum tree_code code, tree arg)
4182 /* Handle (a, b) used as an "lvalue". */
4183 if (TREE_CODE (arg) == COMPOUND_EXPR)
4185 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4186 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
4187 TREE_OPERAND (arg, 0), real_result);
4190 /* Handle (a ? b : c) used as an "lvalue". */
4191 if (TREE_CODE (arg) == COND_EXPR
4192 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4193 return rationalize_conditional_expr (code, arg);
4195 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4196 if (TREE_CODE (arg) == MODIFY_EXPR
4197 || TREE_CODE (arg) == PREINCREMENT_EXPR
4198 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4200 tree lvalue = TREE_OPERAND (arg, 0);
4201 if (TREE_SIDE_EFFECTS (lvalue))
4203 lvalue = stabilize_reference (lvalue);
4204 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
4205 lvalue, TREE_OPERAND (arg, 1));
4207 return unary_complex_lvalue
4208 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4211 if (code != ADDR_EXPR)
4214 /* Handle (a = b) used as an "lvalue" for `&'. */
4215 if (TREE_CODE (arg) == MODIFY_EXPR
4216 || TREE_CODE (arg) == INIT_EXPR)
4218 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4219 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
4221 TREE_NO_WARNING (arg) = 1;
4225 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4226 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4227 || TREE_CODE (arg) == OFFSET_REF)
4231 gcc_assert (TREE_CODE (arg) != SCOPE_REF);
4233 if (TREE_CODE (arg) != OFFSET_REF)
4236 t = TREE_OPERAND (arg, 1);
4238 /* Check all this code for right semantics. */
4239 if (TREE_CODE (t) == FUNCTION_DECL)
4241 if (DECL_DESTRUCTOR_P (t))
4242 error ("taking address of destructor");
4243 return build_unary_op (ADDR_EXPR, t, 0);
4245 if (TREE_CODE (t) == VAR_DECL)
4246 return build_unary_op (ADDR_EXPR, t, 0);
4251 if (TREE_OPERAND (arg, 0)
4252 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4253 && TREE_CODE (t) != FIELD_DECL)
4255 error ("taking address of bound pointer-to-member expression");
4256 return error_mark_node;
4258 if (!PTRMEM_OK_P (arg))
4259 return build_unary_op (code, arg, 0);
4261 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4263 error ("cannot create pointer to reference member %qD", t);
4264 return error_mark_node;
4267 type = build_ptrmem_type (context_for_name_lookup (t),
4269 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4275 /* We permit compiler to make function calls returning
4276 objects of aggregate type look like lvalues. */
4280 if (TREE_CODE (targ) == SAVE_EXPR)
4281 targ = TREE_OPERAND (targ, 0);
4283 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4285 if (TREE_CODE (arg) == SAVE_EXPR)
4288 targ = build_cplus_new (TREE_TYPE (arg), arg);
4289 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4292 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4293 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4294 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4297 /* Don't let anything else be handled specially. */
4301 /* Mark EXP saying that we need to be able to take the
4302 address of it; it should not be allocated in a register.
4303 Value is true if successful.
4305 C++: we do not allow `current_class_ptr' to be addressable. */
4308 cxx_mark_addressable (tree exp)
4313 switch (TREE_CODE (x))
4320 x = TREE_OPERAND (x, 0);
4324 if (x == current_class_ptr)
4326 error ("cannot take the address of `this', which is an rvalue expression");
4327 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
4333 /* Caller should not be trying to mark initialized
4334 constant fields addressable. */
4335 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
4336 || DECL_IN_AGGR_P (x) == 0
4338 || DECL_EXTERNAL (x));
4343 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4344 && !DECL_ARTIFICIAL (x) && extra_warnings)
4345 warning ("address requested for %qD, which is declared `register'",
4347 TREE_ADDRESSABLE (x) = 1;
4351 TREE_ADDRESSABLE (x) = 1;
4355 TREE_ADDRESSABLE (x) = 1;
4359 TREE_ADDRESSABLE (x) = 1;
4360 cxx_mark_addressable (TREE_OPERAND (x, 0));
4368 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4371 build_x_conditional_expr (tree ifexp, tree op1, tree op2)
4373 tree orig_ifexp = ifexp;
4374 tree orig_op1 = op1;
4375 tree orig_op2 = op2;
4378 if (processing_template_decl)
4380 /* The standard says that the expression is type-dependent if
4381 IFEXP is type-dependent, even though the eventual type of the
4382 expression doesn't dependent on IFEXP. */
4383 if (type_dependent_expression_p (ifexp)
4384 /* As a GNU extension, the middle operand may be omitted. */
4385 || (op1 && type_dependent_expression_p (op1))
4386 || type_dependent_expression_p (op2))
4387 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4388 ifexp = build_non_dependent_expr (ifexp);
4390 op1 = build_non_dependent_expr (op1);
4391 op2 = build_non_dependent_expr (op2);
4394 expr = build_conditional_expr (ifexp, op1, op2);
4395 if (processing_template_decl && expr != error_mark_node)
4396 return build_min_non_dep (COND_EXPR, expr,
4397 orig_ifexp, orig_op1, orig_op2);
4401 /* Given a list of expressions, return a compound expression
4402 that performs them all and returns the value of the last of them. */
4404 tree build_x_compound_expr_from_list (tree list, const char *msg)
4406 tree expr = TREE_VALUE (list);
4408 if (TREE_CHAIN (list))
4411 pedwarn ("%s expression list treated as compound expression", msg);
4413 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
4414 expr = build_x_compound_expr (expr, TREE_VALUE (list));
4420 /* Handle overloading of the ',' operator when needed. */
4423 build_x_compound_expr (tree op1, tree op2)
4426 tree orig_op1 = op1;
4427 tree orig_op2 = op2;
4429 if (processing_template_decl)
4431 if (type_dependent_expression_p (op1)
4432 || type_dependent_expression_p (op2))
4433 return build_min_nt (COMPOUND_EXPR, op1, op2);
4434 op1 = build_non_dependent_expr (op1);
4435 op2 = build_non_dependent_expr (op2);
4438 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE,
4439 /*overloaded_p=*/NULL);
4441 result = build_compound_expr (op1, op2);
4443 if (processing_template_decl && result != error_mark_node)
4444 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
4449 /* Build a compound expression. */
4452 build_compound_expr (tree lhs, tree rhs)
4454 lhs = decl_constant_value (lhs);
4455 lhs = convert_to_void (lhs, "left-hand operand of comma");
4457 if (lhs == error_mark_node || rhs == error_mark_node)
4458 return error_mark_node;
4460 if (TREE_CODE (rhs) == TARGET_EXPR)
4462 /* If the rhs is a TARGET_EXPR, then build the compound
4463 expression inside the target_expr's initializer. This
4464 helps the compiler to eliminate unnecessary temporaries. */
4465 tree init = TREE_OPERAND (rhs, 1);
4467 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
4468 TREE_OPERAND (rhs, 1) = init;
4473 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
4476 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
4477 casts away constness. DIAG_FN gives the function to call if we
4478 need to issue a diagnostic; if it is NULL, no diagnostic will be
4479 issued. DESCRIPTION explains what operation is taking place. */
4482 check_for_casting_away_constness (tree src_type, tree dest_type,
4483 void (*diag_fn)(const char *, ...),
4484 const char *description)
4486 if (diag_fn && casts_away_constness (src_type, dest_type))
4487 error ("%s from type %qT to type %qT casts away constness",
4488 description, src_type, dest_type);
4491 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
4492 (another pointer-to-member type in the same hierarchy) and return
4493 the converted expression. If ALLOW_INVERSE_P is permitted, a
4494 pointer-to-derived may be converted to pointer-to-base; otherwise,
4495 only the other direction is permitted. If C_CAST_P is true, this
4496 conversion is taking place as part of a C-style cast. */
4499 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
4502 if (TYPE_PTRMEM_P (type))
4506 if (TREE_CODE (expr) == PTRMEM_CST)
4507 expr = cplus_expand_constant (expr);
4508 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
4509 TYPE_PTRMEM_CLASS_TYPE (type),
4512 if (!integer_zerop (delta))
4513 expr = cp_build_binary_op (PLUS_EXPR,
4514 build_nop (ptrdiff_type_node, expr),
4516 return build_nop (type, expr);
4519 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
4520 allow_inverse_p, c_cast_p);
4523 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
4524 this static_cast is being attempted as one of the possible casts
4525 allowed by a C-style cast. (In that case, accessibility of base
4526 classes is not considered, and it is OK to cast away
4527 constness.) Return the result of the cast. *VALID_P is set to
4528 indicate whether or not the cast was valid. */
4531 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
4537 void (*diag_fn)(const char*, ...);
4540 /* Assume the cast is valid. */
4543 intype = TREE_TYPE (expr);
4545 /* Determine what to do when casting away constness. */
4548 /* C-style casts are allowed to cast away constness. With
4549 WARN_CAST_QUAL, we still want to issue a warning. */
4550 diag_fn = warn_cast_qual ? warning : NULL;
4555 /* A static_cast may not cast away constness. */
4557 desc = "static_cast";
4560 /* [expr.static.cast]
4562 An lvalue of type "cv1 B", where B is a class type, can be cast
4563 to type "reference to cv2 D", where D is a class derived (clause
4564 _class.derived_) from B, if a valid standard conversion from
4565 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
4566 same cv-qualification as, or greater cv-qualification than, cv1,
4567 and B is not a virtual base class of D. */
4568 /* We check this case before checking the validity of "TYPE t =
4569 EXPR;" below because for this case:
4572 struct D : public B { D(const B&); };
4574 void f() { static_cast<const D&>(b); }
4576 we want to avoid constructing a new D. The standard is not
4577 completely clear about this issue, but our interpretation is
4578 consistent with other compilers. */
4579 if (TREE_CODE (type) == REFERENCE_TYPE
4580 && CLASS_TYPE_P (TREE_TYPE (type))
4581 && CLASS_TYPE_P (intype)
4582 && real_lvalue_p (expr)
4583 && DERIVED_FROM_P (intype, TREE_TYPE (type))
4584 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
4585 build_pointer_type (TYPE_MAIN_VARIANT
4586 (TREE_TYPE (type))))
4588 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
4592 /* There is a standard conversion from "D*" to "B*" even if "B"
4593 is ambiguous or inaccessible. If this is really a
4594 static_cast, then we check both for inaccessibility and
4595 ambiguity. However, if this is a static_cast being performed
4596 because the user wrote a C-style cast, then accessibility is
4598 base = lookup_base (TREE_TYPE (type), intype,
4599 c_cast_p ? ba_unique : ba_check,
4602 /* Convert from "B*" to "D*". This function will check that "B"
4603 is not a virtual base of "D". */
4604 expr = build_base_path (MINUS_EXPR, build_address (expr),
4605 base, /*nonnull=*/false);
4606 /* Convert the pointer to a reference -- but then remember that
4607 there are no expressions with reference type in C++. */
4608 return convert_from_reference (build_nop (type, expr));
4613 /* [expr.static.cast]
4615 An expression e can be explicitly converted to a type T using a
4616 static_cast of the form static_cast<T>(e) if the declaration T
4617 t(e);" is well-formed, for some invented temporary variable
4619 result = perform_direct_initialization_if_possible (type, expr,
4623 result = convert_from_reference (result);
4625 /* Ignore any integer overflow caused by the cast. */
4626 if (TREE_CODE (result) == INTEGER_CST
4627 && CONSTANT_CLASS_P (orig))
4629 TREE_OVERFLOW (result) = TREE_OVERFLOW (orig);
4630 TREE_CONSTANT_OVERFLOW (result)
4631 = TREE_CONSTANT_OVERFLOW (orig);
4633 /* [expr.static.cast]
4635 If T is a reference type, the result is an lvalue; otherwise,
4636 the result is an rvalue. */
4637 if (TREE_CODE (type) != REFERENCE_TYPE
4638 && real_lvalue_p (result))
4639 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
4643 /* [expr.static.cast]
4645 Any expression can be explicitly converted to type cv void. */
4646 if (TREE_CODE (type) == VOID_TYPE)
4647 return convert_to_void (expr, /*implicit=*/NULL);
4649 /* [expr.static.cast]
4651 The inverse of any standard conversion sequence (clause _conv_),
4652 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
4653 (_conv.array_), function-to-pointer (_conv.func_), and boolean
4654 (_conv.bool_) conversions, can be performed explicitly using
4655 static_cast subject to the restriction that the explicit
4656 conversion does not cast away constness (_expr.const.cast_), and
4657 the following additional rules for specific cases: */
4658 /* For reference, the conversions not excluded are: integral
4659 promotions, floating point promotion, integral conversions,
4660 floating point conversions, floating-integral conversions,
4661 pointer conversions, and pointer to member conversions. */
4662 if ((ARITHMETIC_TYPE_P (type) && ARITHMETIC_TYPE_P (intype))
4665 A value of integral _or enumeration_ type can be explicitly
4666 converted to an enumeration type. */
4667 || (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4668 && INTEGRAL_OR_ENUMERATION_TYPE_P (intype)))
4670 expr = decl_constant_value (expr);
4671 expr = ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
4673 /* Ignore any integer overflow caused by the cast. */
4674 if (TREE_CODE (expr) == INTEGER_CST
4675 && CONSTANT_CLASS_P (orig))
4677 TREE_OVERFLOW (expr) = TREE_OVERFLOW (orig);
4678 TREE_CONSTANT_OVERFLOW (expr) = TREE_CONSTANT_OVERFLOW (orig);
4683 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
4684 && CLASS_TYPE_P (TREE_TYPE (type))
4685 && CLASS_TYPE_P (TREE_TYPE (intype))
4686 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
4687 (TREE_TYPE (intype))),
4688 build_pointer_type (TYPE_MAIN_VARIANT
4689 (TREE_TYPE (type)))))
4694 check_for_casting_away_constness (intype, type, diag_fn, desc);
4695 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
4696 c_cast_p ? ba_unique : ba_check,
4698 return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
4701 if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4702 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
4709 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
4710 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
4712 if (TYPE_PTRMEM_P (type))
4714 t1 = (build_ptrmem_type
4716 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
4717 t2 = (build_ptrmem_type
4719 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
4726 if (can_convert (t1, t2))
4729 check_for_casting_away_constness (intype, type, diag_fn,
4731 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
4736 /* [expr.static.cast]
4738 An rvalue of type "pointer to cv void" can be explicitly
4739 converted to a pointer to object type. A value of type pointer
4740 to object converted to "pointer to cv void" and back to the
4741 original pointer type will have its original value. */
4742 if (TREE_CODE (intype) == POINTER_TYPE
4743 && VOID_TYPE_P (TREE_TYPE (intype))
4744 && TYPE_PTROB_P (type))
4747 check_for_casting_away_constness (intype, type, diag_fn, desc);
4748 return build_nop (type, expr);
4752 return error_mark_node;
4755 /* Return an expression representing static_cast<TYPE>(EXPR). */
4758 build_static_cast (tree type, tree expr)
4763 if (type == error_mark_node || expr == error_mark_node)
4764 return error_mark_node;
4766 if (processing_template_decl)
4768 expr = build_min (STATIC_CAST_EXPR, type, expr);
4769 /* We don't know if it will or will not have side effects. */
4770 TREE_SIDE_EFFECTS (expr) = 1;
4774 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4775 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4776 if (TREE_CODE (type) != REFERENCE_TYPE
4777 && TREE_CODE (expr) == NOP_EXPR
4778 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4779 expr = TREE_OPERAND (expr, 0);
4781 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p);
4785 error ("invalid static_cast from type %qT to type %qT",
4786 TREE_TYPE (expr), type);
4787 return error_mark_node;
4790 /* EXPR is an expression with member function or pointer-to-member
4791 function type. TYPE is a pointer type. Converting EXPR to TYPE is
4792 not permitted by ISO C++, but we accept it in some modes. If we
4793 are not in one of those modes, issue a diagnostic. Return the
4794 converted expression. */
4797 convert_member_func_to_ptr (tree type, tree expr)
4802 intype = TREE_TYPE (expr);
4803 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
4804 || TREE_CODE (intype) == METHOD_TYPE);
4806 if (pedantic || warn_pmf2ptr)
4807 pedwarn ("converting from %qT to %qT", intype, type);
4809 if (TREE_CODE (intype) == METHOD_TYPE)
4810 expr = build_addr_func (expr);
4811 else if (TREE_CODE (expr) == PTRMEM_CST)
4812 expr = build_address (PTRMEM_CST_MEMBER (expr));
4815 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
4816 decl = build_address (decl);
4817 expr = get_member_function_from_ptrfunc (&decl, expr);
4820 return build_nop (type, expr);
4823 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
4824 If C_CAST_P is true, this reinterpret cast is being done as part of
4825 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
4826 indicate whether or not reinterpret_cast was valid. */
4829 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
4834 /* Assume the cast is invalid. */
4838 if (type == error_mark_node || error_operand_p (expr))
4839 return error_mark_node;
4841 intype = TREE_TYPE (expr);
4843 /* [expr.reinterpret.cast]
4844 An lvalue expression of type T1 can be cast to the type
4845 "reference to T2" if an expression of type "pointer to T1" can be
4846 explicitly converted to the type "pointer to T2" using a
4847 reinterpret_cast. */
4848 if (TREE_CODE (type) == REFERENCE_TYPE)
4850 if (! real_lvalue_p (expr))
4852 error ("invalid cast of an rvalue expression of type "
4855 return error_mark_node;
4858 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
4859 "B" are related class types; the reinterpret_cast does not
4860 adjust the pointer. */
4861 if (TYPE_PTR_P (intype)
4862 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
4863 COMPARE_BASE | COMPARE_DERIVED)))
4864 warning ("casting `%T' to `%T' does not dereference pointer",
4867 expr = build_unary_op (ADDR_EXPR, expr, 0);
4868 if (expr != error_mark_node)
4869 expr = build_reinterpret_cast_1
4870 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
4872 if (expr != error_mark_node)
4873 expr = build_indirect_ref (expr, 0);
4877 /* As a G++ extension, we consider conversions from member
4878 functions, and pointers to member functions to
4879 pointer-to-function and pointer-to-void types. If
4880 -Wno-pmf-conversions has not been specified,
4881 convert_member_func_to_ptr will issue an error message. */
4882 if ((TYPE_PTRMEMFUNC_P (intype)
4883 || TREE_CODE (intype) == METHOD_TYPE)
4884 && TYPE_PTR_P (type)
4885 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4886 || VOID_TYPE_P (TREE_TYPE (type))))
4887 return convert_member_func_to_ptr (type, expr);
4889 /* If the cast is not to a reference type, the lvalue-to-rvalue,
4890 array-to-pointer, and function-to-pointer conversions are
4892 expr = decay_conversion (expr);
4894 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4895 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4896 if (TREE_CODE (expr) == NOP_EXPR
4897 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4898 expr = TREE_OPERAND (expr, 0);
4900 if (error_operand_p (expr))
4901 return error_mark_node;
4903 intype = TREE_TYPE (expr);
4905 /* [expr.reinterpret.cast]
4906 A pointer can be converted to any integral type large enough to
4908 if (CP_INTEGRAL_TYPE_P (type) && TYPE_PTR_P (intype))
4910 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
4911 pedwarn ("cast from %qT to %qT loses precision",
4914 /* [expr.reinterpret.cast]
4915 A value of integral or enumeration type can be explicitly
4916 converted to a pointer. */
4917 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
4920 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
4921 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
4923 expr = decl_constant_value (expr);
4924 return fold_if_not_in_template (build_nop (type, expr));
4926 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4927 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
4930 check_for_casting_away_constness (intype, type, error,
4931 "reinterpret_cast");
4932 /* Warn about possible alignment problems. */
4933 if (STRICT_ALIGNMENT && warn_cast_align
4934 && !VOID_TYPE_P (type)
4935 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
4936 && COMPLETE_TYPE_P (TREE_TYPE (type))
4937 && COMPLETE_TYPE_P (TREE_TYPE (intype))
4938 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
4939 warning ("cast from %qT to %qT increases required alignment of "
4942 expr = decl_constant_value (expr);
4943 return fold_if_not_in_template (build_nop (type, expr));
4945 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
4946 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
4949 /* Only issue a warning, as we have always supported this
4950 where possible, and it is necessary in some cases. DR 195
4951 addresses this issue, but as of 2004/10/26 is still in
4953 warning ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
4955 expr = decl_constant_value (expr);
4956 return fold_if_not_in_template (build_nop (type, expr));
4958 else if (TREE_CODE (type) == VECTOR_TYPE)
4959 return fold_if_not_in_template (convert_to_vector (type, expr));
4964 error ("invalid cast from type %qT to type %qT", intype, type);
4965 return error_mark_node;
4968 return cp_convert (type, expr);
4972 build_reinterpret_cast (tree type, tree expr)
4974 if (type == error_mark_node || expr == error_mark_node)
4975 return error_mark_node;
4977 if (processing_template_decl)
4979 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
4981 if (!TREE_SIDE_EFFECTS (t)
4982 && type_dependent_expression_p (expr))
4983 /* There might turn out to be side effects inside expr. */
4984 TREE_SIDE_EFFECTS (t) = 1;
4988 return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
4992 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
4993 return an appropriate expression. Otherwise, return
4994 error_mark_node. If the cast is not valid, and COMPLAIN is true,
4995 then a diagnostic will be issued. If VALID_P is non-NULL, its
4996 value upon return will indicate whether or not the conversion
5000 build_const_cast_1 (tree dst_type, tree expr, bool complain,
5004 tree reference_type;
5006 /* Callers are responsible for handling error_mark_node as a
5007 destination type. */
5008 gcc_assert (dst_type != error_mark_node);
5009 /* In a template, callers should be building syntactic
5010 representations of casts, not using this machinery. */
5011 gcc_assert (!processing_template_decl);
5013 /* Assume the conversion is invalid. */
5017 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type))
5020 error ("invalid use of const_cast with type %qT, "
5021 "which is not a pointer, "
5022 "reference, nor a pointer-to-data-member type", dst_type);
5023 return error_mark_node;
5026 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
5029 error ("invalid use of const_cast with type %qT, which is a pointer "
5030 "or reference to a function type", dst_type);
5031 return error_mark_node;
5034 src_type = TREE_TYPE (expr);
5035 /* Expressions do not really have reference types. */
5036 if (TREE_CODE (src_type) == REFERENCE_TYPE)
5037 src_type = TREE_TYPE (src_type);
5039 /* [expr.const.cast]
5041 An lvalue of type T1 can be explicitly converted to an lvalue of
5042 type T2 using the cast const_cast<T2&> (where T1 and T2 are object
5043 types) if a pointer to T1 can be explicitly converted to the type
5044 pointer to T2 using a const_cast. */
5045 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
5047 reference_type = dst_type;
5048 if (! real_lvalue_p (expr))
5051 error ("invalid const_cast of an rvalue of type %qT to type %qT",
5052 src_type, dst_type);
5053 return error_mark_node;
5055 dst_type = build_pointer_type (TREE_TYPE (dst_type));
5056 src_type = build_pointer_type (src_type);
5060 reference_type = NULL_TREE;
5061 /* If the destination type is not a reference type, the
5062 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
5063 conversions are performed. */
5064 src_type = type_decays_to (src_type);
5065 if (src_type == error_mark_node)
5066 return error_mark_node;
5069 if ((TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
5070 && comp_ptr_ttypes_const (dst_type, src_type))
5076 expr = build_unary_op (ADDR_EXPR, expr, 0);
5077 expr = build_nop (reference_type, expr);
5078 return convert_from_reference (expr);
5082 expr = decay_conversion (expr);
5083 /* build_c_cast puts on a NOP_EXPR to make the result not an
5084 lvalue. Strip such NOP_EXPRs if VALUE is being used in
5085 non-lvalue context. */
5086 if (TREE_CODE (expr) == NOP_EXPR
5087 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5088 expr = TREE_OPERAND (expr, 0);
5089 return build_nop (dst_type, expr);
5094 error ("invalid const_cast from type %qT to type %qT",
5095 src_type, dst_type);
5096 return error_mark_node;
5100 build_const_cast (tree type, tree expr)
5102 if (type == error_mark_node || expr == error_mark_node)
5103 return error_mark_node;
5105 if (processing_template_decl)
5107 tree t = build_min (CONST_CAST_EXPR, type, expr);
5109 if (!TREE_SIDE_EFFECTS (t)
5110 && type_dependent_expression_p (expr))
5111 /* There might turn out to be side effects inside expr. */
5112 TREE_SIDE_EFFECTS (t) = 1;
5116 return build_const_cast_1 (type, expr, /*complain=*/true,
5120 /* Build an expression representing an explicit C-style cast to type
5121 TYPE of expression EXPR. */
5124 build_c_cast (tree type, tree expr)
5130 if (type == error_mark_node || error_operand_p (expr))
5131 return error_mark_node;
5133 if (processing_template_decl)
5135 tree t = build_min (CAST_EXPR, type,
5136 tree_cons (NULL_TREE, value, NULL_TREE));
5137 /* We don't know if it will or will not have side effects. */
5138 TREE_SIDE_EFFECTS (t) = 1;
5142 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
5143 'Class') should always be retained, because this information aids
5144 in method lookup. */
5145 if (objc_is_object_ptr (type)
5146 && objc_is_object_ptr (TREE_TYPE (expr)))
5147 return build_nop (type, expr);
5149 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5150 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5151 if (TREE_CODE (type) != REFERENCE_TYPE
5152 && TREE_CODE (value) == NOP_EXPR
5153 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5154 value = TREE_OPERAND (value, 0);
5156 if (TREE_CODE (type) == ARRAY_TYPE)
5158 /* Allow casting from T1* to T2[] because Cfront allows it.
5159 NIHCL uses it. It is not valid ISO C++ however. */
5160 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5162 pedwarn ("ISO C++ forbids casting to an array type %qT", type);
5163 type = build_pointer_type (TREE_TYPE (type));
5167 error ("ISO C++ forbids casting to an array type %qT", type);
5168 return error_mark_node;
5172 if (TREE_CODE (type) == FUNCTION_TYPE
5173 || TREE_CODE (type) == METHOD_TYPE)
5175 error ("invalid cast to function type %qT", type);
5176 return error_mark_node;
5179 /* A C-style cast can be a const_cast. */
5180 result = build_const_cast_1 (type, value, /*complain=*/false,
5185 /* Or a static cast. */
5186 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
5188 /* Or a reinterpret_cast. */
5190 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
5192 /* The static_cast or reinterpret_cast may be followed by a
5195 /* A valid cast may result in errors if, for example, a
5196 conversion to am ambiguous base class is required. */
5197 && !error_operand_p (result))
5201 /* Non-class rvalues always have cv-unqualified type. */
5202 if (!CLASS_TYPE_P (type))
5203 type = TYPE_MAIN_VARIANT (type);
5204 result_type = TREE_TYPE (result);
5205 if (!CLASS_TYPE_P (result_type))
5206 result_type = TYPE_MAIN_VARIANT (result_type);
5207 /* If the type of RESULT does not match TYPE, perform a
5208 const_cast to make it match. If the static_cast or
5209 reinterpret_cast succeeded, we will differ by at most
5210 cv-qualification, so the follow-on const_cast is guaranteed
5212 if (!same_type_p (non_reference (type), non_reference (result_type)))
5214 result = build_const_cast_1 (type, result, false, &valid_p);
5215 gcc_assert (valid_p);
5220 return error_mark_node;
5223 /* Build an assignment expression of lvalue LHS from value RHS.
5224 MODIFYCODE is the code for a binary operator that we use
5225 to combine the old value of LHS with RHS to get the new value.
5226 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5228 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5231 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5235 tree lhstype = TREE_TYPE (lhs);
5236 tree olhstype = lhstype;
5237 tree olhs = NULL_TREE;
5238 bool plain_assign = (modifycode == NOP_EXPR);
5240 /* Avoid duplicate error messages from operands that had errors. */
5241 if (lhs == error_mark_node || rhs == error_mark_node)
5242 return error_mark_node;
5244 /* Handle control structure constructs used as "lvalues". */
5245 switch (TREE_CODE (lhs))
5247 /* Handle --foo = 5; as these are valid constructs in C++. */
5248 case PREDECREMENT_EXPR:
5249 case PREINCREMENT_EXPR:
5250 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5251 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
5252 stabilize_reference (TREE_OPERAND (lhs, 0)),
5253 TREE_OPERAND (lhs, 1));
5254 return build2 (COMPOUND_EXPR, lhstype,
5256 build_modify_expr (TREE_OPERAND (lhs, 0),
5259 /* Handle (a, b) used as an "lvalue". */
5261 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5263 if (newrhs == error_mark_node)
5264 return error_mark_node;
5265 return build2 (COMPOUND_EXPR, lhstype,
5266 TREE_OPERAND (lhs, 0), newrhs);
5269 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5270 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
5271 stabilize_reference (TREE_OPERAND (lhs, 0)),
5272 TREE_OPERAND (lhs, 1));
5273 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5274 if (newrhs == error_mark_node)
5275 return error_mark_node;
5276 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
5280 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
5281 when neither operand has side-effects. */
5282 if (!lvalue_or_else (lhs, "assignment"))
5283 return error_mark_node;
5285 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
5286 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
5288 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
5289 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
5291 TREE_OPERAND (lhs, 0),
5292 TREE_OPERAND (lhs, 1)),
5293 TREE_OPERAND (lhs, 0),
5294 TREE_OPERAND (lhs, 1));
5297 /* Handle (a ? b : c) used as an "lvalue". */
5300 /* Produce (a ? (b = rhs) : (c = rhs))
5301 except that the RHS goes through a save-expr
5302 so the code to compute it is only emitted once. */
5304 tree preeval = NULL_TREE;
5306 rhs = stabilize_expr (rhs, &preeval);
5308 /* Check this here to avoid odd errors when trying to convert
5309 a throw to the type of the COND_EXPR. */
5310 if (!lvalue_or_else (lhs, "assignment"))
5311 return error_mark_node;
5313 cond = build_conditional_expr
5314 (TREE_OPERAND (lhs, 0),
5315 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5316 TREE_OPERAND (lhs, 1)),
5318 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5319 TREE_OPERAND (lhs, 2)),
5322 if (cond == error_mark_node)
5324 /* Make sure the code to compute the rhs comes out
5325 before the split. */
5327 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
5335 if (modifycode == INIT_EXPR)
5337 if (TREE_CODE (rhs) == CONSTRUCTOR)
5339 if (! same_type_p (TREE_TYPE (rhs), lhstype))
5340 /* Call convert to generate an error; see PR 11063. */
5341 rhs = convert (lhstype, rhs);
5342 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
5343 TREE_SIDE_EFFECTS (result) = 1;
5346 else if (! IS_AGGR_TYPE (lhstype))
5347 /* Do the default thing. */;
5350 result = build_special_member_call (lhs, complete_ctor_identifier,
5351 build_tree_list (NULL_TREE, rhs),
5352 lhstype, LOOKUP_NORMAL);
5353 if (result == NULL_TREE)
5354 return error_mark_node;
5360 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5362 lhs = convert_from_reference (lhs);
5363 olhstype = lhstype = TREE_TYPE (lhs);
5365 lhs = require_complete_type (lhs);
5366 if (lhs == error_mark_node)
5367 return error_mark_node;
5369 if (modifycode == NOP_EXPR)
5371 /* `operator=' is not an inheritable operator. */
5372 if (! IS_AGGR_TYPE (lhstype))
5373 /* Do the default thing. */;
5376 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
5377 lhs, rhs, make_node (NOP_EXPR),
5378 /*overloaded_p=*/NULL);
5379 if (result == NULL_TREE)
5380 return error_mark_node;
5387 /* A binary op has been requested. Combine the old LHS
5388 value with the RHS producing the value we should actually
5389 store into the LHS. */
5391 gcc_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE));
5392 lhs = stabilize_reference (lhs);
5393 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5394 if (newrhs == error_mark_node)
5396 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
5397 TREE_TYPE (lhs), TREE_TYPE (rhs));
5398 return error_mark_node;
5401 /* Now it looks like a plain assignment. */
5402 modifycode = NOP_EXPR;
5404 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
5405 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
5408 /* The left-hand side must be an lvalue. */
5409 if (!lvalue_or_else (lhs, "assignment"))
5410 return error_mark_node;
5412 /* Warn about modifying something that is `const'. Don't warn if
5413 this is initialization. */
5414 if (modifycode != INIT_EXPR
5415 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5416 /* Functions are not modifiable, even though they are
5418 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5419 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5420 /* If it's an aggregate and any field is const, then it is
5421 effectively const. */
5422 || (CLASS_TYPE_P (lhstype)
5423 && C_TYPE_FIELDS_READONLY (lhstype))))
5424 readonly_error (lhs, "assignment", 0);
5426 /* If storing into a structure or union member, it has probably been
5427 given type `int'. Compute the type that would go with the actual
5428 amount of storage the member occupies. */
5430 if (TREE_CODE (lhs) == COMPONENT_REF
5431 && (TREE_CODE (lhstype) == INTEGER_TYPE
5432 || TREE_CODE (lhstype) == REAL_TYPE
5433 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5435 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5437 /* If storing in a field that is in actuality a short or narrower
5438 than one, we must store in the field in its actual type. */
5440 if (lhstype != TREE_TYPE (lhs))
5442 /* Avoid warnings converting integral types back into enums for
5444 if (TREE_CODE (lhstype) == INTEGER_TYPE
5445 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5447 if (TREE_SIDE_EFFECTS (lhs))
5448 lhs = stabilize_reference (lhs);
5451 lhs = copy_node (lhs);
5452 TREE_TYPE (lhs) = lhstype;
5456 /* Convert new value to destination type. */
5458 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5462 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5463 TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
5465 error ("incompatible types in assignment of %qT to %qT",
5466 TREE_TYPE (rhs), lhstype);
5467 return error_mark_node;
5470 /* Allow array assignment in compiler-generated code. */
5471 if (! DECL_ARTIFICIAL (current_function_decl))
5472 pedwarn ("ISO C++ forbids assignment of arrays");
5474 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5475 ? 1 + (modifycode != INIT_EXPR): 0;
5476 return build_vec_init (lhs, NULL_TREE, newrhs, from_array);
5479 if (modifycode == INIT_EXPR)
5480 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5481 "initialization", NULL_TREE, 0);
5484 /* Avoid warnings on enum bit fields. */
5485 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5486 && TREE_CODE (lhstype) == INTEGER_TYPE)
5488 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5490 newrhs = convert_force (lhstype, newrhs, 0);
5493 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5495 if (TREE_CODE (newrhs) == CALL_EXPR
5496 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5497 newrhs = build_cplus_new (lhstype, newrhs);
5499 /* Can't initialize directly from a TARGET_EXPR, since that would
5500 cause the lhs to be constructed twice, and possibly result in
5501 accidental self-initialization. So we force the TARGET_EXPR to be
5502 expanded without a target. */
5503 if (TREE_CODE (newrhs) == TARGET_EXPR)
5504 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5505 TREE_OPERAND (newrhs, 0));
5508 if (newrhs == error_mark_node)
5509 return error_mark_node;
5511 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5512 lhstype, lhs, newrhs);
5514 TREE_SIDE_EFFECTS (result) = 1;
5516 TREE_NO_WARNING (result) = 1;
5518 /* If we got the LHS in a different type for storing in,
5519 convert the result back to the nominal type of LHS
5520 so that the value we return always has the same type
5521 as the LHS argument. */
5523 if (olhstype == TREE_TYPE (result))
5527 result = build2 (COMPOUND_EXPR, olhstype, result, olhs);
5528 TREE_NO_WARNING (result) = 1;
5531 return convert_for_assignment (olhstype, result, "assignment",
5536 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5538 if (processing_template_decl)
5539 return build_min_nt (MODOP_EXPR, lhs,
5540 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5542 if (modifycode != NOP_EXPR)
5544 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5545 make_node (modifycode),
5546 /*overloaded_p=*/NULL);
5549 TREE_NO_WARNING (rval) = 1;
5553 return build_modify_expr (lhs, modifycode, rhs);
5557 /* Get difference in deltas for different pointer to member function
5558 types. Returns an integer constant of type PTRDIFF_TYPE_NODE. If
5559 the conversion is invalid, the constant is zero. If
5560 ALLOW_INVERSE_P is true, then allow reverse conversions as well.
5561 If C_CAST_P is true this conversion is taking place as part of a
5564 Note that the naming of FROM and TO is kind of backwards; the return
5565 value is what we add to a TO in order to get a FROM. They are named
5566 this way because we call this function to find out how to convert from
5567 a pointer to member of FROM to a pointer to member of TO. */
5570 get_delta_difference (tree from, tree to,
5571 bool allow_inverse_p,
5579 /* Assume no conversion is required. */
5580 result = integer_zero_node;
5581 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, &kind);
5582 if (kind == bk_inaccessible || kind == bk_ambig)
5583 error (" in pointer to member function conversion");
5586 if (!allow_inverse_p)
5588 error_not_base_type (from, to);
5589 error (" in pointer to member conversion");
5593 binfo = lookup_base (from, to, c_cast_p ? ba_unique : ba_check,
5597 virt_binfo = binfo_from_vbase (binfo);
5599 /* This is a reinterpret cast, we choose to do nothing. */
5600 warning ("pointer to member cast via virtual base %qT",
5601 BINFO_TYPE (virt_binfo));
5603 result = size_diffop (size_zero_node, BINFO_OFFSET (binfo));
5609 virt_binfo = binfo_from_vbase (binfo);
5611 result = BINFO_OFFSET (binfo);
5614 /* This is a reinterpret cast, we choose to do nothing. */
5615 if (allow_inverse_p)
5616 warning ("pointer to member cast via virtual base %qT",
5617 BINFO_TYPE (virt_binfo));
5619 error ("pointer to member conversion via virtual base %qT",
5620 BINFO_TYPE (virt_binfo));
5624 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
5628 /* Return a constructor for the pointer-to-member-function TYPE using
5629 the other components as specified. */
5632 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
5638 /* Pull the FIELD_DECLs out of the type. */
5639 pfn_field = TYPE_FIELDS (type);
5640 delta_field = TREE_CHAIN (pfn_field);
5642 /* Make sure DELTA has the type we want. */
5643 delta = convert_and_check (delta_type_node, delta);
5645 /* Finish creating the initializer. */
5646 u = tree_cons (pfn_field, pfn,
5647 build_tree_list (delta_field, delta));
5648 u = build_constructor (type, u);
5649 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
5650 TREE_INVARIANT (u) = TREE_INVARIANT (pfn) & TREE_INVARIANT (delta);
5651 TREE_STATIC (u) = (TREE_CONSTANT (u)
5652 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
5654 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
5659 /* Build a constructor for a pointer to member function. It can be
5660 used to initialize global variables, local variable, or used
5661 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5664 If FORCE is nonzero, then force this conversion, even if
5665 we would rather not do it. Usually set when using an explicit
5666 cast. A C-style cast is being processed iff C_CAST_P is true.
5668 Return error_mark_node, if something goes wrong. */
5671 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p)
5677 if (error_operand_p (pfn))
5678 return error_mark_node;
5680 pfn_type = TREE_TYPE (pfn);
5681 to_type = build_ptrmemfunc_type (type);
5683 /* Handle multiple conversions of pointer to member functions. */
5684 if (TYPE_PTRMEMFUNC_P (pfn_type))
5686 tree delta = NULL_TREE;
5687 tree npfn = NULL_TREE;
5691 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
5692 error ("invalid conversion to type %qT from type %qT",
5695 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
5696 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
5700 /* We don't have to do any conversion to convert a
5701 pointer-to-member to its own type. But, we don't want to
5702 just return a PTRMEM_CST if there's an explicit cast; that
5703 cast should make the expression an invalid template argument. */
5704 if (TREE_CODE (pfn) != PTRMEM_CST)
5706 if (same_type_p (to_type, pfn_type))
5708 else if (integer_zerop (n))
5709 return build_reinterpret_cast (to_type, pfn);
5712 if (TREE_SIDE_EFFECTS (pfn))
5713 pfn = save_expr (pfn);
5715 /* Obtain the function pointer and the current DELTA. */
5716 if (TREE_CODE (pfn) == PTRMEM_CST)
5717 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
5720 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
5721 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
5724 /* Just adjust the DELTA field. */
5725 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5726 (TREE_TYPE (delta), ptrdiff_type_node));
5727 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
5728 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
5729 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
5730 return build_ptrmemfunc1 (to_type, delta, npfn);
5733 /* Handle null pointer to member function conversions. */
5734 if (integer_zerop (pfn))
5736 pfn = build_c_cast (type, integer_zero_node);
5737 return build_ptrmemfunc1 (to_type,
5742 if (type_unknown_p (pfn))
5743 return instantiate_type (type, pfn, tf_error | tf_warning);
5745 fn = TREE_OPERAND (pfn, 0);
5746 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
5747 return make_ptrmem_cst (to_type, fn);
5750 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5753 ??? There is no consistency as to the types returned for the above
5754 values. Some code acts as if it were a sizetype and some as if it were
5755 integer_type_node. */
5758 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
5760 tree type = TREE_TYPE (cst);
5761 tree fn = PTRMEM_CST_MEMBER (cst);
5762 tree ptr_class, fn_class;
5764 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
5766 /* The class that the function belongs to. */
5767 fn_class = DECL_CONTEXT (fn);
5769 /* The class that we're creating a pointer to member of. */
5770 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
5772 /* First, calculate the adjustment to the function's class. */
5773 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
5776 if (!DECL_VIRTUAL_P (fn))
5777 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
5780 /* If we're dealing with a virtual function, we have to adjust 'this'
5781 again, to point to the base which provides the vtable entry for
5782 fn; the call will do the opposite adjustment. */
5783 tree orig_class = DECL_CONTEXT (fn);
5784 tree binfo = binfo_or_else (orig_class, fn_class);
5785 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
5786 *delta, BINFO_OFFSET (binfo));
5787 *delta = fold_if_not_in_template (*delta);
5789 /* We set PFN to the vtable offset at which the function can be
5790 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
5791 case delta is shifted left, and then incremented). */
5792 *pfn = DECL_VINDEX (fn);
5793 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
5794 TYPE_SIZE_UNIT (vtable_entry_type));
5795 *pfn = fold_if_not_in_template (*pfn);
5797 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
5799 case ptrmemfunc_vbit_in_pfn:
5800 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
5802 *pfn = fold_if_not_in_template (*pfn);
5805 case ptrmemfunc_vbit_in_delta:
5806 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
5807 *delta, integer_one_node);
5808 *delta = fold_if_not_in_template (*delta);
5809 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
5810 *delta, integer_one_node);
5811 *delta = fold_if_not_in_template (*delta);
5818 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
5819 *pfn = fold_if_not_in_template (*pfn);
5823 /* Return an expression for PFN from the pointer-to-member function
5827 pfn_from_ptrmemfunc (tree t)
5829 if (TREE_CODE (t) == PTRMEM_CST)
5834 expand_ptrmemfunc_cst (t, &delta, &pfn);
5839 return build_ptrmemfunc_access_expr (t, pfn_identifier);
5842 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
5843 if this is a potentially dangerous thing to do. Returns a possibly
5847 dubious_conversion_warnings (tree type, tree expr,
5848 const char *errtype, tree fndecl, int parmnum)
5850 type = non_reference (type);
5852 /* Issue warnings about peculiar, but valid, uses of NULL. */
5853 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
5856 warning ("passing NULL used for non-pointer %s %P of %qD",
5857 errtype, parmnum, fndecl);
5859 warning ("%s to non-pointer type %qT from NULL", errtype, type);
5862 /* Warn about assigning a floating-point type to an integer type. */
5863 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
5864 && TREE_CODE (type) == INTEGER_TYPE)
5867 warning ("passing %qT for %s %P of %qD",
5868 TREE_TYPE (expr), errtype, parmnum, fndecl);
5870 warning ("%s to %qT from %qT", errtype, type, TREE_TYPE (expr));
5872 /* And warn about assigning a negative value to an unsigned
5874 else if (TYPE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
5876 if (TREE_CODE (expr) == INTEGER_CST && TREE_NEGATED_INT (expr))
5879 warning ("passing negative value %qE for %s %P of %qD",
5880 expr, errtype, parmnum, fndecl);
5882 warning ("%s of negative value %qE to %qT", errtype, expr, type);
5885 overflow_warning (expr);
5887 if (TREE_CONSTANT (expr))
5888 expr = fold_if_not_in_template (expr);
5893 /* Convert value RHS to type TYPE as preparation for an assignment to
5894 an lvalue of type TYPE. ERRTYPE is a string to use in error
5895 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
5896 are doing the conversion in order to pass the PARMNUMth argument of
5900 convert_for_assignment (tree type, tree rhs,
5901 const char *errtype, tree fndecl, int parmnum)
5904 enum tree_code coder;
5906 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
5907 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
5908 rhs = TREE_OPERAND (rhs, 0);
5910 rhstype = TREE_TYPE (rhs);
5911 coder = TREE_CODE (rhstype);
5913 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
5914 && vector_types_convertible_p (type, rhstype))
5915 return convert (type, rhs);
5917 if (rhs == error_mark_node || rhstype == error_mark_node)
5918 return error_mark_node;
5919 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
5920 return error_mark_node;
5922 /* The RHS of an assignment cannot have void type. */
5923 if (coder == VOID_TYPE)
5925 error ("void value not ignored as it ought to be");
5926 return error_mark_node;
5929 /* Simplify the RHS if possible. */
5930 if (TREE_CODE (rhs) == CONST_DECL)
5931 rhs = DECL_INITIAL (rhs);
5933 /* We do not use decl_constant_value here because of this case:
5935 const char* const s = "s";
5937 The conversion rules for a string literal are more lax than for a
5938 variable; in particular, a string literal can be converted to a
5939 "char *" but the variable "s" cannot be converted in the same
5940 way. If the conversion is allowed, the optimization should be
5941 performed while creating the converted expression. */
5945 The expression is implicitly converted (clause _conv_) to the
5946 cv-unqualified type of the left operand.
5948 We allow bad conversions here because by the time we get to this point
5949 we are committed to doing the conversion. If we end up doing a bad
5950 conversion, convert_like will complain. */
5951 if (!can_convert_arg_bad (type, rhstype, rhs))
5953 /* When -Wno-pmf-conversions is use, we just silently allow
5954 conversions from pointers-to-members to plain pointers. If
5955 the conversion doesn't work, cp_convert will complain. */
5957 && TYPE_PTR_P (type)
5958 && TYPE_PTRMEMFUNC_P (rhstype))
5959 rhs = cp_convert (strip_top_quals (type), rhs);
5962 /* If the right-hand side has unknown type, then it is an
5963 overloaded function. Call instantiate_type to get error
5965 if (rhstype == unknown_type_node)
5966 instantiate_type (type, rhs, tf_error | tf_warning);
5968 error ("cannot convert %qT to %qT for argument %qP to %qD",
5969 rhstype, type, parmnum, fndecl);
5971 error ("cannot convert %qT to %qT in %s", rhstype, type, errtype);
5972 return error_mark_node;
5975 return perform_implicit_conversion (strip_top_quals (type), rhs);
5978 /* Convert RHS to be of type TYPE.
5979 If EXP is nonzero, it is the target of the initialization.
5980 ERRTYPE is a string to use in error messages.
5982 Two major differences between the behavior of
5983 `convert_for_assignment' and `convert_for_initialization'
5984 are that references are bashed in the former, while
5985 copied in the latter, and aggregates are assigned in
5986 the former (operator=) while initialized in the
5989 If using constructor make sure no conversion operator exists, if one does
5990 exist, an ambiguity exists.
5992 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
5995 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
5996 const char *errtype, tree fndecl, int parmnum)
5998 enum tree_code codel = TREE_CODE (type);
6000 enum tree_code coder;
6002 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6003 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6004 if (TREE_CODE (rhs) == NOP_EXPR
6005 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6006 && codel != REFERENCE_TYPE)
6007 rhs = TREE_OPERAND (rhs, 0);
6009 if (rhs == error_mark_node
6010 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6011 return error_mark_node;
6013 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6014 rhs = convert_from_reference (rhs);
6016 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6017 && TREE_CODE (type) != ARRAY_TYPE
6018 && (TREE_CODE (type) != REFERENCE_TYPE
6019 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6020 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6021 && (TREE_CODE (type) != REFERENCE_TYPE
6022 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6023 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6024 rhs = decay_conversion (rhs);
6026 rhstype = TREE_TYPE (rhs);
6027 coder = TREE_CODE (rhstype);
6029 if (coder == ERROR_MARK)
6030 return error_mark_node;
6032 /* We accept references to incomplete types, so we can
6033 return here before checking if RHS is of complete type. */
6035 if (codel == REFERENCE_TYPE)
6037 /* This should eventually happen in convert_arguments. */
6038 int savew = 0, savee = 0;
6041 savew = warningcount, savee = errorcount;
6042 rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE,
6046 if (warningcount > savew)
6047 cp_warning_at ("in passing argument %P of %q+D", parmnum, fndecl);
6048 else if (errorcount > savee)
6049 cp_error_at ("in passing argument %P of %q+D", parmnum, fndecl);
6055 exp = require_complete_type (exp);
6056 if (exp == error_mark_node)
6057 return error_mark_node;
6059 rhstype = non_reference (rhstype);
6061 type = complete_type (type);
6063 if (IS_AGGR_TYPE (type))
6064 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6066 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6069 /* If RETVAL is the address of, or a reference to, a local variable or
6070 temporary give an appropriate warning. */
6073 maybe_warn_about_returning_address_of_local (tree retval)
6075 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6076 tree whats_returned = retval;
6080 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6081 whats_returned = TREE_OPERAND (whats_returned, 1);
6082 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6083 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6084 || TREE_CODE (whats_returned) == NOP_EXPR)
6085 whats_returned = TREE_OPERAND (whats_returned, 0);
6090 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6092 whats_returned = TREE_OPERAND (whats_returned, 0);
6094 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6096 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6097 || TREE_CODE (whats_returned) == TARGET_EXPR)
6099 warning ("returning reference to temporary");
6102 if (TREE_CODE (whats_returned) == VAR_DECL
6103 && DECL_NAME (whats_returned)
6104 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6106 warning ("reference to non-lvalue returned");
6111 if (DECL_P (whats_returned)
6112 && DECL_NAME (whats_returned)
6113 && DECL_FUNCTION_SCOPE_P (whats_returned)
6114 && !(TREE_STATIC (whats_returned)
6115 || TREE_PUBLIC (whats_returned)))
6117 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6118 cp_warning_at ("reference to local variable %qD returned",
6121 cp_warning_at ("address of local variable %qD returned",
6127 /* Check that returning RETVAL from the current function is valid.
6128 Return an expression explicitly showing all conversions required to
6129 change RETVAL into the function return type, and to assign it to
6130 the DECL_RESULT for the function. */
6133 check_return_expr (tree retval)
6136 /* The type actually returned by the function, after any
6139 int fn_returns_value_p;
6141 /* A `volatile' function is one that isn't supposed to return, ever.
6142 (This is a G++ extension, used to get better code for functions
6143 that call the `volatile' function.) */
6144 if (TREE_THIS_VOLATILE (current_function_decl))
6145 warning ("function declared `noreturn' has a `return' statement");
6147 /* Check for various simple errors. */
6148 if (DECL_DESTRUCTOR_P (current_function_decl))
6151 error ("returning a value from a destructor");
6154 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6156 if (in_function_try_handler)
6157 /* If a return statement appears in a handler of the
6158 function-try-block of a constructor, the program is ill-formed. */
6159 error ("cannot return from a handler of a function-try-block of a constructor");
6161 /* You can't return a value from a constructor. */
6162 error ("returning a value from a constructor");
6166 if (processing_template_decl)
6168 current_function_returns_value = 1;
6172 /* When no explicit return-value is given in a function with a named
6173 return value, the named return value is used. */
6174 result = DECL_RESULT (current_function_decl);
6175 valtype = TREE_TYPE (result);
6176 gcc_assert (valtype != NULL_TREE);
6177 fn_returns_value_p = !VOID_TYPE_P (valtype);
6178 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6181 /* Check for a return statement with no return value in a function
6182 that's supposed to return a value. */
6183 if (!retval && fn_returns_value_p)
6185 pedwarn ("return-statement with no value, in function returning '%T'",
6187 /* Clear this, so finish_function won't say that we reach the
6188 end of a non-void function (which we don't, we gave a
6190 current_function_returns_null = 0;
6192 /* Check for a return statement with a value in a function that
6193 isn't supposed to return a value. */
6194 else if (retval && !fn_returns_value_p)
6196 if (VOID_TYPE_P (TREE_TYPE (retval)))
6197 /* You can return a `void' value from a function of `void'
6198 type. In that case, we have to evaluate the expression for
6199 its side-effects. */
6200 finish_expr_stmt (retval);
6202 pedwarn ("return-statement with a value, in function "
6203 "returning 'void'");
6205 current_function_returns_null = 1;
6207 /* There's really no value to return, after all. */
6211 /* Remember that this function can sometimes return without a
6213 current_function_returns_null = 1;
6215 /* Remember that this function did return a value. */
6216 current_function_returns_value = 1;
6218 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6219 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6220 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6221 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6223 && null_ptr_cst_p (retval))
6224 warning ("%<operator new%> must not return NULL unless it is "
6225 "declared %<throw()%> (or -fcheck-new is in effect)");
6227 /* Effective C++ rule 15. See also start_function. */
6229 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
6233 /* The function return type must be a reference to the current
6235 if (TREE_CODE (valtype) == REFERENCE_TYPE
6236 && same_type_ignoring_top_level_qualifiers_p
6237 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
6239 /* Returning '*this' is obviously OK. */
6240 if (retval == current_class_ref)
6242 /* If we are calling a function whose return type is the same of
6243 the current class reference, it is ok. */
6244 else if (TREE_CODE (retval) == INDIRECT_REF
6245 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
6250 warning ("%<operator=%> should return a reference to %<*this%>");
6253 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6255 [...] For a function with a class return type, if the expression
6256 in the return statement is the name of a local object, and the cv-
6257 unqualified type of the local object is the same as the function
6258 return type, an implementation is permitted to omit creating the tem-
6259 porary object to hold the function return value [...]
6261 So, if this is a value-returning function that always returns the same
6262 local variable, remember it.
6264 It might be nice to be more flexible, and choose the first suitable
6265 variable even if the function sometimes returns something else, but
6266 then we run the risk of clobbering the variable we chose if the other
6267 returned expression uses the chosen variable somehow. And people expect
6268 this restriction, anyway. (jason 2000-11-19)
6270 See finish_function and finalize_nrv for the rest of this optimization. */
6272 if (fn_returns_value_p && flag_elide_constructors)
6274 if (retval != NULL_TREE
6275 && (current_function_return_value == NULL_TREE
6276 || current_function_return_value == retval)
6277 && TREE_CODE (retval) == VAR_DECL
6278 && DECL_CONTEXT (retval) == current_function_decl
6279 && ! TREE_STATIC (retval)
6280 && (DECL_ALIGN (retval)
6281 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6282 && same_type_p ((TYPE_MAIN_VARIANT
6283 (TREE_TYPE (retval))),
6285 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6286 current_function_return_value = retval;
6288 current_function_return_value = error_mark_node;
6291 /* We don't need to do any conversions when there's nothing being
6293 if (!retval || retval == error_mark_node)
6296 /* Do any required conversions. */
6297 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6298 /* No conversions are required. */
6302 /* The type the function is declared to return. */
6303 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6305 /* First convert the value to the function's return type, then
6306 to the type of return value's location to handle the
6307 case that functype is smaller than the valtype. */
6308 retval = convert_for_initialization
6309 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6310 "return", NULL_TREE, 0);
6311 retval = convert (valtype, retval);
6313 /* If the conversion failed, treat this just like `return;'. */
6314 if (retval == error_mark_node)
6316 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6317 else if (! current_function_returns_struct
6318 && TREE_CODE (retval) == TARGET_EXPR
6319 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6320 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6321 TREE_OPERAND (retval, 0));
6323 maybe_warn_about_returning_address_of_local (retval);
6326 /* Actually copy the value returned into the appropriate location. */
6327 if (retval && retval != result)
6328 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
6334 /* Returns nonzero if the pointer-type FROM can be converted to the
6335 pointer-type TO via a qualification conversion. If CONSTP is -1,
6336 then we return nonzero if the pointers are similar, and the
6337 cv-qualification signature of FROM is a proper subset of that of TO.
6339 If CONSTP is positive, then all outer pointers have been
6343 comp_ptr_ttypes_real (tree to, tree from, int constp)
6345 bool to_more_cv_qualified = false;
6347 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6349 if (TREE_CODE (to) != TREE_CODE (from))
6352 if (TREE_CODE (from) == OFFSET_TYPE
6353 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
6354 TYPE_OFFSET_BASETYPE (to)))
6357 /* Const and volatile mean something different for function types,
6358 so the usual checks are not appropriate. */
6359 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6361 if (!at_least_as_qualified_p (to, from))
6364 if (!at_least_as_qualified_p (from, to))
6368 to_more_cv_qualified = true;
6372 constp &= TYPE_READONLY (to);
6375 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
6376 return ((constp >= 0 || to_more_cv_qualified)
6377 && same_type_ignoring_top_level_qualifiers_p (to, from));
6381 /* When comparing, say, char ** to char const **, this function takes
6382 the 'char *' and 'char const *'. Do not pass non-pointer/reference
6383 types to this function. */
6386 comp_ptr_ttypes (tree to, tree from)
6388 return comp_ptr_ttypes_real (to, from, 1);
6391 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6392 type or inheritance-related types, regardless of cv-quals. */
6395 ptr_reasonably_similar (tree to, tree from)
6397 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6399 /* Any target type is similar enough to void. */
6400 if (TREE_CODE (to) == VOID_TYPE
6401 || TREE_CODE (from) == VOID_TYPE)
6404 if (TREE_CODE (to) != TREE_CODE (from))
6407 if (TREE_CODE (from) == OFFSET_TYPE
6408 && comptypes (TYPE_OFFSET_BASETYPE (to),
6409 TYPE_OFFSET_BASETYPE (from),
6410 COMPARE_BASE | COMPARE_DERIVED))
6413 if (TREE_CODE (to) == INTEGER_TYPE
6414 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6417 if (TREE_CODE (to) == FUNCTION_TYPE)
6420 if (TREE_CODE (to) != POINTER_TYPE)
6422 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6423 COMPARE_BASE | COMPARE_DERIVED);
6427 /* Like comp_ptr_ttypes, for const_cast. */
6430 comp_ptr_ttypes_const (tree to, tree from)
6432 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6434 if (TREE_CODE (to) != TREE_CODE (from))
6437 if (TREE_CODE (from) == OFFSET_TYPE
6438 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6439 TYPE_OFFSET_BASETYPE (to)))
6442 if (TREE_CODE (to) != POINTER_TYPE)
6443 return same_type_ignoring_top_level_qualifiers_p (to, from);
6447 /* Returns the type qualifiers for this type, including the qualifiers on the
6448 elements for an array type. */
6451 cp_type_quals (tree type)
6453 type = strip_array_types (type);
6454 if (type == error_mark_node)
6455 return TYPE_UNQUALIFIED;
6456 return TYPE_QUALS (type);
6459 /* Returns nonzero if the TYPE contains a mutable member. */
6462 cp_has_mutable_p (tree type)
6464 type = strip_array_types (type);
6466 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6469 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6470 exemplar types such that casting T1 to T2 is casting away constness
6471 if and only if there is no implicit conversion from T1 to T2. */
6474 casts_away_constness_r (tree *t1, tree *t2)
6479 /* [expr.const.cast]
6481 For multi-level pointer to members and multi-level mixed pointers
6482 and pointers to members (conv.qual), the "member" aspect of a
6483 pointer to member level is ignored when determining if a const
6484 cv-qualifier has been cast away. */
6485 if (TYPE_PTRMEM_P (*t1))
6486 *t1 = build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (*t1));
6487 if (TYPE_PTRMEM_P (*t2))
6488 *t2 = build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (*t2));
6490 /* [expr.const.cast]
6492 For two pointer types:
6494 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6495 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6498 casting from X1 to X2 casts away constness if, for a non-pointer
6499 type T there does not exist an implicit conversion (clause
6502 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6506 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6508 if (TREE_CODE (*t1) != POINTER_TYPE
6509 || TREE_CODE (*t2) != POINTER_TYPE)
6511 *t1 = cp_build_qualified_type (void_type_node,
6512 cp_type_quals (*t1));
6513 *t2 = cp_build_qualified_type (void_type_node,
6514 cp_type_quals (*t2));
6518 quals1 = cp_type_quals (*t1);
6519 quals2 = cp_type_quals (*t2);
6520 *t1 = TREE_TYPE (*t1);
6521 *t2 = TREE_TYPE (*t2);
6522 casts_away_constness_r (t1, t2);
6523 *t1 = build_pointer_type (*t1);
6524 *t2 = build_pointer_type (*t2);
6525 *t1 = cp_build_qualified_type (*t1, quals1);
6526 *t2 = cp_build_qualified_type (*t2, quals2);
6529 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
6533 casts_away_constness (tree t1, tree t2)
6535 if (TREE_CODE (t2) == REFERENCE_TYPE)
6537 /* [expr.const.cast]
6539 Casting from an lvalue of type T1 to an lvalue of type T2
6540 using a reference cast casts away constness if a cast from an
6541 rvalue of type "pointer to T1" to the type "pointer to T2"
6542 casts away constness. */
6543 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
6544 return casts_away_constness (build_pointer_type (t1),
6545 build_pointer_type (TREE_TYPE (t2)));
6548 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6549 /* [expr.const.cast]
6551 Casting from an rvalue of type "pointer to data member of X
6552 of type T1" to the type "pointer to data member of Y of type
6553 T2" casts away constness if a cast from an rvalue of type
6554 "pointer to T1" to the type "pointer to T2" casts away
6556 return casts_away_constness
6557 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
6558 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)));
6560 /* Casting away constness is only something that makes sense for
6561 pointer or reference types. */
6562 if (TREE_CODE (t1) != POINTER_TYPE
6563 || TREE_CODE (t2) != POINTER_TYPE)
6566 /* Top-level qualifiers don't matter. */
6567 t1 = TYPE_MAIN_VARIANT (t1);
6568 t2 = TYPE_MAIN_VARIANT (t2);
6569 casts_away_constness_r (&t1, &t2);
6570 if (!can_convert (t2, t1))
6576 /* If T is a REFERENCE_TYPE return the type to which T refers.
6577 Otherwise, return T itself. */
6580 non_reference (tree t)
6582 if (TREE_CODE (t) == REFERENCE_TYPE)