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 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.
29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30 and to process initializations in declarations (since they work
31 like a strange sort of assignment). */
35 #include "coretypes.h"
45 #include "diagnostic.h"
48 static tree convert_for_assignment (tree, tree, const char *, tree, int);
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
50 static tree rationalize_conditional_expr (enum tree_code, tree);
51 static int comp_target_parms (tree, tree);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static int comp_ptr_ttypes_const (tree, tree);
54 static int comp_ptr_ttypes_reinterpret (tree, tree);
55 static bool comp_except_types (tree, tree, bool);
56 static bool comp_array_types (tree, tree, bool);
57 static tree common_base_type (tree, tree);
58 static tree lookup_anon_field (tree, tree);
59 static tree pointer_diff (tree, tree, tree);
60 static tree qualify_type_recursive (tree, tree);
61 static tree get_delta_difference (tree, tree, int);
62 static int comp_cv_target_types (tree, tree, int);
63 static void casts_away_constness_r (tree *, tree *);
64 static bool casts_away_constness (tree, tree);
65 static void maybe_warn_about_returning_address_of_local (tree);
66 static tree strip_all_pointer_quals (tree);
67 static tree lookup_destructor (tree, tree, tree);
69 /* Return the target type of TYPE, which means return T for:
70 T*, T&, T[], T (...), and otherwise, just T. */
73 target_type (tree type)
75 if (TREE_CODE (type) == REFERENCE_TYPE)
76 type = TREE_TYPE (type);
77 while (TREE_CODE (type) == POINTER_TYPE
78 || TREE_CODE (type) == ARRAY_TYPE
79 || TREE_CODE (type) == FUNCTION_TYPE
80 || TREE_CODE (type) == METHOD_TYPE
81 || TREE_CODE (type) == OFFSET_TYPE)
82 type = TREE_TYPE (type);
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.)
88 Returns the error_mark_node if the VALUE does not have
89 complete type when this function returns. */
92 require_complete_type (tree value)
96 if (processing_template_decl || value == error_mark_node)
99 if (TREE_CODE (value) == OVERLOAD)
100 type = unknown_type_node;
102 type = TREE_TYPE (value);
104 /* First, detect a valid value with a complete type. */
105 if (COMPLETE_TYPE_P (type))
108 /* If we see X::Y, we build an OFFSET_TYPE which has
109 not been laid out. Try to avoid an error by interpreting
110 it as this->X::Y, if reasonable. */
111 if (TREE_CODE (value) == OFFSET_REF
112 && current_class_ref != 0
113 && TREE_OPERAND (value, 0) == current_class_ref)
115 value = resolve_offset_ref (value);
116 return require_complete_type (value);
119 if (complete_type_or_else (type, value))
122 return error_mark_node;
125 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
126 a template instantiation, do the instantiation. Returns TYPE,
127 whether or not it could be completed, unless something goes
128 horribly wrong, in which case the error_mark_node is returned. */
131 complete_type (tree type)
133 if (type == NULL_TREE)
134 /* Rather than crash, we return something sure to cause an error
136 return error_mark_node;
138 if (type == error_mark_node || COMPLETE_TYPE_P (type))
140 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
142 tree t = complete_type (TREE_TYPE (type));
143 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
145 TYPE_NEEDS_CONSTRUCTING (type)
146 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
147 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
148 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
150 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
151 instantiate_class_template (TYPE_MAIN_VARIANT (type));
156 /* Like complete_type, but issue an error if the TYPE cannot be completed.
157 VALUE is used for informative diagnostics. DIAG_TYPE indicates the type
158 of diagnostic: 0 for an error, 1 for a warning, 2 for a pedwarn.
159 Returns NULL_TREE if the type cannot be made complete. */
162 complete_type_or_diagnostic (tree type, tree value, int diag_type)
164 type = complete_type (type);
165 if (type == error_mark_node)
166 /* We already issued an error. */
168 else if (!COMPLETE_TYPE_P (type))
170 cxx_incomplete_type_diagnostic (value, type, diag_type);
177 /* Return truthvalue of whether type of EXP is instantiated. */
183 return (TREE_CODE (exp) == OVERLOAD
184 || TREE_CODE (exp) == TREE_LIST
185 || TREE_TYPE (exp) == unknown_type_node
186 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
187 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
190 /* Return a pointer or pointer to member type similar to T1, with a
191 cv-qualification signature that is the union of the cv-qualification
192 signatures of T1 and T2: [expr.rel], [expr.eq]. */
195 qualify_type_recursive (t1, t2)
198 if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
199 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
206 tree attributes = (*targetm.merge_type_attributes) (t1, t2);
208 if (TYPE_PTRMEM_P (t1))
210 b1 = TYPE_PTRMEM_CLASS_TYPE (t1);
211 tt1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
212 tt2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
217 tt1 = TREE_TYPE (t1);
218 tt2 = TREE_TYPE (t2);
221 type_quals = (cp_type_quals (tt1) | cp_type_quals (tt2));
222 tgt = qualify_type_recursive (tt1, tt2);
223 tgt = cp_build_qualified_type (tgt, type_quals);
225 t1 = build_ptrmem_type (b1, tgt);
227 t1 = build_pointer_type (tgt);
228 t1 = build_type_attribute_variant (t1, attributes);
233 /* Return the common type of two parameter lists.
234 We assume that comptypes has already been done and returned 1;
235 if that isn't so, this may crash.
237 As an optimization, free the space we allocate if the parameter
238 lists are already common. */
244 tree oldargs = p1, newargs, n;
248 len = list_length (p1);
249 newargs = tree_last (p1);
251 if (newargs == void_list_node)
260 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
265 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
267 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
269 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
272 else if (! TREE_PURPOSE (p1))
274 if (TREE_PURPOSE (p2))
276 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
282 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
284 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
286 if (TREE_VALUE (p1) != TREE_VALUE (p2))
289 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
292 TREE_VALUE (n) = TREE_VALUE (p1);
300 /* Given a type, perhaps copied for a typedef,
301 find the "original" version of it. */
306 while (TYPE_NAME (t) != NULL_TREE)
308 tree x = TYPE_NAME (t);
309 if (TREE_CODE (x) != TYPE_DECL)
311 x = DECL_ORIGINAL_TYPE (x);
319 /* T1 and T2 are arithmetic or enumeration types. Return the type
320 that will result from the "usual arithmetic conversions" on T1 and
321 T2 as described in [expr]. */
324 type_after_usual_arithmetic_conversions (t1, t2)
328 enum tree_code code1 = TREE_CODE (t1);
329 enum tree_code code2 = TREE_CODE (t2);
332 /* FIXME: Attributes. */
333 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
334 || TREE_CODE (t1) == COMPLEX_TYPE
335 || TREE_CODE (t1) == ENUMERAL_TYPE,
337 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
338 || TREE_CODE (t2) == COMPLEX_TYPE
339 || TREE_CODE (t2) == ENUMERAL_TYPE,
342 /* In what follows, we slightly generalize the rules given in [expr] so
343 as to deal with `long long' and `complex'. First, merge the
345 attributes = (*targetm.merge_type_attributes) (t1, t2);
347 /* If one type is complex, form the common type of the non-complex
348 components, then make that complex. Use T1 or T2 if it is the
350 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
352 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
353 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
355 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
357 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
358 return build_type_attribute_variant (t1, attributes);
359 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
360 return build_type_attribute_variant (t2, attributes);
362 return build_type_attribute_variant (build_complex_type (subtype),
366 /* If only one is real, use it as the result. */
367 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
368 return build_type_attribute_variant (t1, attributes);
369 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
370 return build_type_attribute_variant (t2, attributes);
372 /* Perform the integral promotions. */
373 if (code1 != REAL_TYPE)
375 t1 = type_promotes_to (t1);
376 t2 = type_promotes_to (t2);
379 /* Both real or both integers; use the one with greater precision. */
380 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
381 return build_type_attribute_variant (t1, attributes);
382 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
383 return build_type_attribute_variant (t2, attributes);
385 /* The types are the same; no need to do anything fancy. */
386 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
387 return build_type_attribute_variant (t1, attributes);
389 if (code1 != REAL_TYPE)
391 /* If one is a sizetype, use it so size_binop doesn't blow up. */
392 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
393 return build_type_attribute_variant (t1, attributes);
394 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
395 return build_type_attribute_variant (t2, attributes);
397 /* If one is unsigned long long, then convert the other to unsigned
399 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
400 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
401 return build_type_attribute_variant (long_long_unsigned_type_node,
403 /* If one is a long long, and the other is an unsigned long, and
404 long long can represent all the values of an unsigned long, then
405 convert to a long long. Otherwise, convert to an unsigned long
406 long. Otherwise, if either operand is long long, convert the
409 Since we're here, we know the TYPE_PRECISION is the same;
410 therefore converting to long long cannot represent all the values
411 of an unsigned long, so we choose unsigned long long in that
413 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
414 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
416 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
417 ? long_long_unsigned_type_node
418 : long_long_integer_type_node);
419 return build_type_attribute_variant (t, attributes);
422 /* Go through the same procedure, but for longs. */
423 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
424 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
425 return build_type_attribute_variant (long_unsigned_type_node,
427 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
428 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
430 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
431 ? long_unsigned_type_node : long_integer_type_node);
432 return build_type_attribute_variant (t, attributes);
434 /* Otherwise prefer the unsigned one. */
435 if (TREE_UNSIGNED (t1))
436 return build_type_attribute_variant (t1, attributes);
438 return build_type_attribute_variant (t2, attributes);
442 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
443 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
444 return build_type_attribute_variant (long_double_type_node,
446 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
447 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
448 return build_type_attribute_variant (double_type_node,
450 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
451 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
452 return build_type_attribute_variant (float_type_node,
455 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
456 the standard C++ floating-point types. Logic earlier in this
457 function has already eliminated the possibility that
458 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
459 compelling reason to choose one or the other. */
460 return build_type_attribute_variant (t1, attributes);
464 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
465 ARG1 and ARG2 are the values with those types. The LOCATION is a
466 string describing the current location, in case an error occurs. */
469 composite_pointer_type (t1, t2, arg1, arg2, location)
474 const char* location;
481 If one operand is a null pointer constant, the composite pointer
482 type is the type of the other operand. */
483 if (null_ptr_cst_p (arg1))
485 if (null_ptr_cst_p (arg2))
488 /* Deal with pointer-to-member functions in the same way as we deal
489 with pointers to functions. */
490 if (TYPE_PTRMEMFUNC_P (t1))
491 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
492 if (TYPE_PTRMEMFUNC_P (t2))
493 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
495 /* Merge the attributes. */
496 attributes = (*targetm.merge_type_attributes) (t1, t2);
502 If one of the operands has type "pointer to cv1 void*", then
503 the other has type "pointer to cv2T", and the composite pointer
504 type is "pointer to cv12 void", where cv12 is the union of cv1
507 If either type is a pointer to void, make sure it is T1. */
508 if (VOID_TYPE_P (TREE_TYPE (t2)))
515 /* Now, if T1 is a pointer to void, merge the qualifiers. */
516 if (VOID_TYPE_P (TREE_TYPE (t1)))
518 if (pedantic && TYPE_PTRFN_P (t2))
519 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
522 result_type = cp_build_qualified_type (void_type_node,
524 | cp_type_quals (t2)));
525 result_type = build_pointer_type (result_type);
529 tree full1 = qualify_type_recursive (t1, t2);
530 tree full2 = qualify_type_recursive (t2, t1);
532 int val = comp_target_types (full1, full2, 1);
540 pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
542 result_type = ptr_type_node;
546 return build_type_attribute_variant (result_type, attributes);
549 /* Return the merged type of two types.
550 We assume that comptypes has already been done and returned 1;
551 if that isn't so, this may crash.
553 This just combines attributes and default arguments; any other
554 differences would cause the two types to compare unalike. */
560 register enum tree_code code1;
561 register enum tree_code code2;
564 /* Save time if the two types are the same. */
567 if (original_type (t1) == original_type (t2))
570 /* If one type is nonsense, use the other. */
571 if (t1 == error_mark_node)
573 if (t2 == error_mark_node)
576 /* Merge the attributes. */
577 attributes = (*targetm.merge_type_attributes) (t1, t2);
579 /* Treat an enum type as the unsigned integer type of the same width. */
581 if (TYPE_PTRMEMFUNC_P (t1))
582 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
583 if (TYPE_PTRMEMFUNC_P (t2))
584 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
586 code1 = TREE_CODE (t1);
587 code2 = TREE_CODE (t2);
593 /* For two pointers, do this recursively on the target type. */
595 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
596 int quals = cp_type_quals (t1);
598 if (code1 == POINTER_TYPE)
599 t1 = build_pointer_type (target);
601 t1 = build_reference_type (target);
602 t1 = build_type_attribute_variant (t1, attributes);
603 t1 = cp_build_qualified_type (t1, quals);
605 if (TREE_CODE (target) == METHOD_TYPE)
606 t1 = build_ptrmemfunc_type (t1);
613 tree base = TYPE_OFFSET_BASETYPE (t1);
614 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
615 t1 = build_offset_type (base, target);
621 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
622 /* Save space: see if the result is identical to one of the args. */
623 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
624 return build_type_attribute_variant (t1, attributes);
625 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
626 return build_type_attribute_variant (t2, attributes);
627 /* Merge the element types, and have a size if either arg has one. */
628 t1 = build_cplus_array_type
629 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
634 /* Function types: prefer the one that specified arg types.
635 If both do, merge the arg types. Also merge the return types. */
637 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
638 tree p1 = TYPE_ARG_TYPES (t1);
639 tree p2 = TYPE_ARG_TYPES (t2);
642 /* Save space: see if the result is identical to one of the args. */
643 if (valtype == TREE_TYPE (t1) && ! p2)
644 return build_type_attribute_variant (t1, attributes);
645 if (valtype == TREE_TYPE (t2) && ! p1)
646 return build_type_attribute_variant (t2, attributes);
648 /* Simple way if one arg fails to specify argument types. */
649 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
651 rval = build_function_type (valtype, p2);
652 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
653 rval = build_exception_variant (rval, raises);
654 return build_type_attribute_variant (rval, attributes);
656 raises = TYPE_RAISES_EXCEPTIONS (t1);
657 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
659 rval = build_function_type (valtype, p1);
661 rval = build_exception_variant (rval, raises);
662 return build_type_attribute_variant (rval, attributes);
665 rval = build_function_type (valtype, commonparms (p1, p2));
666 t1 = build_exception_variant (rval, raises);
672 /* Get this value the long way, since TYPE_METHOD_BASETYPE
673 is just the main variant of this. */
674 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
675 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
678 /* If this was a member function type, get back to the
679 original type of type member function (i.e., without
680 the class instance variable up front. */
681 t1 = build_function_type (TREE_TYPE (t1),
682 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
683 t2 = build_function_type (TREE_TYPE (t2),
684 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
685 t3 = merge_types (t1, t2);
686 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
687 TYPE_ARG_TYPES (t3));
688 t1 = build_exception_variant (t3, raises);
694 return build_type_attribute_variant (t1, attributes);
697 /* Return the common type of two types.
698 We assume that comptypes has already been done and returned 1;
699 if that isn't so, this may crash.
701 This is the type for the result of most arithmetic operations
702 if the operands have the given two types. */
708 enum tree_code code1;
709 enum tree_code code2;
711 /* If one type is nonsense, bail. */
712 if (t1 == error_mark_node || t2 == error_mark_node)
713 return error_mark_node;
715 code1 = TREE_CODE (t1);
716 code2 = TREE_CODE (t2);
718 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
719 || code1 == COMPLEX_TYPE)
720 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
721 || code2 == COMPLEX_TYPE))
722 return type_after_usual_arithmetic_conversions (t1, t2);
724 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
725 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
726 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
727 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
734 /* Compare two exception specifier types for exactness or subsetness, if
735 allowed. Returns false for mismatch, true for match (same, or
738 [except.spec] "If a class X ... objects of class X or any class publicly
739 and unambigously derrived from X. Similarly, if a pointer type Y * ...
740 exceptions of type Y * or that are pointers to any type publicly and
741 unambigously derrived from Y. Otherwise a function only allows exceptions
742 that have the same type ..."
743 This does not mention cv qualifiers and is different to what throw
744 [except.throw] and catch [except.catch] will do. They will ignore the
745 top level cv qualifiers, and allow qualifiers in the pointer to class
748 We implement the letter of the standard. */
751 comp_except_types (tree a, tree b, bool exact)
753 if (same_type_p (a, b))
757 if (cp_type_quals (a) || cp_type_quals (b))
760 if (TREE_CODE (a) == POINTER_TYPE
761 && TREE_CODE (b) == POINTER_TYPE)
765 if (cp_type_quals (a) || cp_type_quals (b))
769 if (TREE_CODE (a) != RECORD_TYPE
770 || TREE_CODE (b) != RECORD_TYPE)
773 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
779 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
780 If EXACT is false, T2 can be stricter than T1 (according to 15.4/7),
781 otherwise it must be exact. Exception lists are unordered, but
782 we've already filtered out duplicates. Most lists will be in order,
783 we should try to make use of that. */
786 comp_except_specs (tree t1, tree t2, bool exact)
795 if (t1 == NULL_TREE) /* T1 is ... */
796 return t2 == NULL_TREE || !exact;
797 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
798 return t2 != NULL_TREE && !TREE_VALUE (t2);
799 if (t2 == NULL_TREE) /* T2 is ... */
801 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
804 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
805 Count how many we find, to determine exactness. For exact matching and
806 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
808 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
810 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
812 tree a = TREE_VALUE (probe);
813 tree b = TREE_VALUE (t2);
815 if (comp_except_types (a, b, exact))
817 if (probe == base && exact)
818 base = TREE_CHAIN (probe);
823 if (probe == NULL_TREE)
826 return !exact || base == NULL_TREE || length == list_length (t1);
829 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
830 [] can match [size]. */
833 comp_array_types (tree t1, tree t2, bool allow_redeclaration)
841 /* The type of the array elements must be the same. */
842 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
845 d1 = TYPE_DOMAIN (t1);
846 d2 = TYPE_DOMAIN (t2);
851 /* If one of the arrays is dimensionless, and the other has a
852 dimension, they are of different types. However, it is valid to
860 declarations for an array object can specify
861 array types that differ by the presence or absence of a major
862 array bound (_dcl.array_). */
864 return allow_redeclaration;
866 /* Check that the dimensions are the same. */
867 return (cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
868 && cp_tree_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)));
871 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
872 is a bitwise-or of the COMPARE_* flags. */
875 comptypes (tree t1, tree t2, int strict)
880 /* This should never happen. */
881 my_friendly_assert (t1 != error_mark_node, 307);
883 /* Suppress errors caused by previously reported errors */
884 if (t2 == error_mark_node)
887 my_friendly_assert (TYPE_P (t1) && TYPE_P (t2), 20030623);
889 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
890 current instantiation. */
891 if (TREE_CODE (t1) == TYPENAME_TYPE)
893 tree resolved = resolve_typename_type (t1, /*only_current_p=*/true);
895 if (resolved != error_mark_node)
899 if (TREE_CODE (t2) == TYPENAME_TYPE)
901 tree resolved = resolve_typename_type (t2, /*only_current_p=*/true);
903 if (resolved != error_mark_node)
907 /* If either type is the internal version of sizetype, use the
909 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
911 t1 = TYPE_DOMAIN (t1);
913 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
915 t2 = TYPE_DOMAIN (t2);
917 if (TYPE_PTRMEMFUNC_P (t1))
918 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
919 if (TYPE_PTRMEMFUNC_P (t2))
920 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
922 /* Different classes of types can't be compatible. */
923 if (TREE_CODE (t1) != TREE_CODE (t2))
926 /* Qualifiers must match. */
927 if (cp_type_quals (t1) != cp_type_quals (t2))
929 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
932 /* Allow for two different type nodes which have essentially the same
933 definition. Note that we already checked for equality of the type
934 qualifiers (just above). */
936 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
939 if (!(*targetm.comp_type_attributes) (t1, t2))
942 switch (TREE_CODE (t1))
944 case TEMPLATE_TEMPLATE_PARM:
945 case BOUND_TEMPLATE_TEMPLATE_PARM:
946 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
947 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
949 if (!comp_template_parms
950 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
951 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
953 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
955 /* Don't check inheritance. */
956 strict = COMPARE_STRICT;
961 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
962 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
963 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
964 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
967 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
969 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
975 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
976 strict & ~COMPARE_REDECLARATION))
982 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
986 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
988 return compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
991 /* Target types must match incl. qualifiers. */
992 return comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION));
994 case TEMPLATE_TYPE_PARM:
995 return (TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
996 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2));
999 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1000 TYPENAME_TYPE_FULLNAME (t2)))
1002 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1004 case UNBOUND_CLASS_TEMPLATE:
1005 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1007 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1010 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1018 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1019 only in the same direction as the target type. */
1022 comp_cv_target_types (tree ttl, tree ttr, int nptrs)
1026 if (!at_least_as_qualified_p (ttl, ttr)
1027 && !at_least_as_qualified_p (ttr, ttl))
1028 /* The qualifications are incomparable. */
1031 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1032 return more_qualified_p (ttr, ttl) ? -1 : 1;
1034 t = comp_target_types (ttl, ttr, nptrs);
1035 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1036 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1042 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1043 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1044 converted to TTL. Return -1 means that TTL can be converted to TTR but
1047 NPTRS is the number of pointers we can strip off and keep cool.
1048 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1049 but to not permit B** to convert to A**.
1051 This should go away. Callers should use can_convert or something
1052 similar instead. (jason 17 Apr 1997) */
1055 comp_target_types (tree ttl, tree ttr, int nptrs)
1057 ttl = TYPE_MAIN_VARIANT (ttl);
1058 ttr = TYPE_MAIN_VARIANT (ttr);
1059 if (same_type_p (ttl, ttr))
1062 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1065 if ((TREE_CODE (ttr) == POINTER_TYPE
1066 || TREE_CODE (ttr) == REFERENCE_TYPE)
1067 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1068 of the type pointed to. This is necessary for reference init
1069 semantics. We won't get here from a previous call with nptrs == 1;
1070 for multi-level pointers we end up in comp_ptr_ttypes. */
1073 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1075 ttl = TREE_TYPE (ttl);
1076 ttr = TREE_TYPE (ttr);
1080 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1081 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1083 else if (TREE_CODE (ttl) == VOID_TYPE
1084 && TREE_CODE (ttr) != FUNCTION_TYPE
1085 && TREE_CODE (ttr) != METHOD_TYPE
1086 && TREE_CODE (ttr) != OFFSET_TYPE)
1088 else if (TREE_CODE (ttr) == VOID_TYPE
1089 && TREE_CODE (ttl) != FUNCTION_TYPE
1090 && TREE_CODE (ttl) != METHOD_TYPE
1091 && TREE_CODE (ttl) != OFFSET_TYPE)
1093 else if (TREE_CODE (ttl) == POINTER_TYPE
1094 || TREE_CODE (ttl) == ARRAY_TYPE)
1096 if (comp_ptr_ttypes (ttl, ttr))
1098 else if (comp_ptr_ttypes (ttr, ttl))
1104 /* Const and volatile mean something different for function types,
1105 so the usual checks are not appropriate. */
1106 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1107 return comp_target_types (ttl, ttr, nptrs - 1);
1109 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1112 my_friendly_assert (TREE_CODE (ttr) != ARRAY_TYPE, 20030617);
1113 if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1120 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1125 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1134 argsl = TYPE_ARG_TYPES (ttl);
1135 argsr = TYPE_ARG_TYPES (ttr);
1137 /* Compare 'this' here, not in comp_target_parms. */
1138 if (TREE_CODE (ttr) == METHOD_TYPE)
1140 tree tl = TYPE_METHOD_BASETYPE (ttl);
1141 tree tr = TYPE_METHOD_BASETYPE (ttr);
1143 if (!same_or_base_type_p (tr, tl))
1145 if (same_or_base_type_p (tl, tr))
1151 argsl = TREE_CHAIN (argsl);
1152 argsr = TREE_CHAIN (argsr);
1155 switch (comp_target_parms (argsl, argsr))
1163 return saw_contra ? -1 : 1;
1166 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1170 /* Contravariance: we can assign a pointer to base member to a pointer
1171 to derived member. Note difference from simple pointer case, where
1172 we can pass a pointer to derived to a pointer to base. */
1173 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1174 TYPE_OFFSET_BASETYPE (ttl)))
1176 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1177 TYPE_OFFSET_BASETYPE (ttr)))
1187 ttl = TREE_TYPE (ttl);
1188 ttr = TREE_TYPE (ttr);
1190 if (TREE_CODE (ttl) == POINTER_TYPE
1191 || TREE_CODE (ttl) == ARRAY_TYPE)
1193 if (comp_ptr_ttypes (ttl, ttr))
1199 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1204 else if (IS_AGGR_TYPE (ttl))
1208 if (same_or_base_type_p (ttl, ttr))
1210 if (same_or_base_type_p (ttr, ttl))
1218 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1221 at_least_as_qualified_p (tree type1, tree type2)
1223 int q1 = cp_type_quals (type1);
1224 int q2 = cp_type_quals (type2);
1226 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1227 return (q1 & q2) == q2;
1230 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1233 more_qualified_p (tree type1, tree type2)
1235 int q1 = cp_type_quals (type1);
1236 int q2 = cp_type_quals (type2);
1238 return q1 != q2 && (q1 & q2) == q2;
1241 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1242 more cv-qualified that TYPE1, and 0 otherwise. */
1245 comp_cv_qualification (tree type1, tree type2)
1247 int q1 = cp_type_quals (type1);
1248 int q2 = cp_type_quals (type2);
1253 if ((q1 & q2) == q2)
1255 else if ((q1 & q2) == q1)
1261 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1262 subset of the cv-qualification signature of TYPE2, and the types
1263 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1266 comp_cv_qual_signature (tree type1, tree type2)
1268 if (comp_ptr_ttypes_real (type2, type1, -1))
1270 else if (comp_ptr_ttypes_real (type1, type2, -1))
1276 /* If two types share a common base type, return that basetype.
1277 If there is not a unique most-derived base type, this function
1278 returns ERROR_MARK_NODE. */
1281 common_base_type (tree tt1, tree tt2)
1283 tree best = NULL_TREE;
1286 /* If one is a baseclass of another, that's good enough. */
1287 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1289 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1292 /* Otherwise, try to find a unique baseclass of TT1
1293 that is shared by TT2, and follow that down. */
1294 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1296 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1297 tree trial = common_base_type (basetype, tt2);
1300 if (trial == error_mark_node)
1302 if (best == NULL_TREE)
1304 else if (best != trial)
1305 return error_mark_node;
1310 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1312 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1313 tree trial = common_base_type (tt1, basetype);
1316 if (trial == error_mark_node)
1318 if (best == NULL_TREE)
1320 else if (best != trial)
1321 return error_mark_node;
1327 /* Subroutines of `comptypes'. */
1329 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1330 equivalent in the sense that functions with those parameter types
1331 can have equivalent types. The two lists must be equivalent,
1332 element by element. */
1335 compparms (tree parms1, tree parms2)
1339 /* An unspecified parmlist matches any specified parmlist
1340 whose argument types don't need default promotions. */
1342 for (t1 = parms1, t2 = parms2;
1344 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1346 /* If one parmlist is shorter than the other,
1347 they fail to match. */
1350 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1356 /* This really wants return whether or not parameter type lists
1357 would make their owning functions assignment compatible or not.
1359 The return value is like for comp_target_types.
1361 This should go away, possibly with the exception of the empty parmlist
1362 conversion; there are no conversions between function types in C++.
1363 (jason 17 Apr 1997) */
1366 comp_target_parms (tree parms1, tree parms2)
1368 register tree t1 = parms1, t2 = parms2;
1369 int warn_contravariance = 0;
1371 /* In C, an unspecified parmlist matches any specified parmlist
1372 whose argument types don't need default promotions. This is not
1373 true for C++, but let's do it anyway for unfixed headers. */
1375 if (t1 == 0 && t2 != 0)
1377 pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1379 return self_promoting_args_p (t2);
1382 return self_promoting_args_p (t1);
1384 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1388 /* If one parmlist is shorter than the other, they fail to match. */
1391 p1 = TREE_VALUE (t1);
1392 p2 = TREE_VALUE (t2);
1393 if (same_type_p (p1, p2))
1399 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1400 || (TREE_CODE (p1) == REFERENCE_TYPE
1401 && TREE_CODE (p2) == REFERENCE_TYPE))
1403 /* The following is wrong for contravariance,
1404 but many programs depend on it. */
1405 if (TREE_TYPE (p1) == void_type_node)
1407 if (TREE_TYPE (p2) == void_type_node)
1409 warn_contravariance = 1;
1412 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1413 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1417 /* Note backwards order due to contravariance. */
1418 if (comp_target_types (p2, p1, 1) <= 0)
1420 if (comp_target_types (p1, p2, 1) > 0)
1422 warn_contravariance = 1;
1428 return warn_contravariance ? -1 : 1;
1432 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, int complain)
1434 enum tree_code type_code;
1436 const char *op_name;
1438 my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
1439 if (processing_template_decl)
1440 return build_min_nt (op, type);
1442 op_name = operator_name_info[(int) op].name;
1444 if (TREE_CODE (type) == REFERENCE_TYPE)
1445 type = TREE_TYPE (type);
1446 type_code = TREE_CODE (type);
1448 if (type_code == METHOD_TYPE)
1450 if (complain && (pedantic || warn_pointer_arith))
1451 pedwarn ("invalid application of `%s' to a member function", op_name);
1452 value = size_one_node;
1454 else if (type_code == OFFSET_TYPE)
1457 error ("invalid application of `%s' to non-static member", op_name);
1458 value = size_zero_node;
1461 value = c_sizeof_or_alignof_type (complete_type (type), op, complain);
1467 expr_sizeof (tree e)
1469 if (processing_template_decl)
1470 return build_min_nt (SIZEOF_EXPR, e);
1472 if (TREE_CODE (e) == COMPONENT_REF
1473 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1474 error ("sizeof applied to a bit-field");
1475 if (is_overloaded_fn (e))
1477 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1478 return c_sizeof (char_type_node);
1480 else if (type_unknown_p (e))
1482 cxx_incomplete_type_error (e, TREE_TYPE (e));
1483 return c_sizeof (char_type_node);
1485 /* It's invalid to say `sizeof (X::i)' for `i' a non-static data
1486 member unless you're in a non-static member of X. So hand off to
1487 resolve_offset_ref. [expr.prim] */
1488 else if (TREE_CODE (e) == OFFSET_REF)
1489 e = resolve_offset_ref (e);
1491 if (e == error_mark_node)
1494 return cxx_sizeof (TREE_TYPE (e));
1498 /* Perform the array-to-pointer and function-to-pointer conversions
1501 In addition, references are converted to lvalues and manifest
1502 constants are replaced by their values. */
1505 decay_conversion (tree exp)
1508 register enum tree_code code;
1510 if (TREE_CODE (exp) == OFFSET_REF)
1511 exp = resolve_offset_ref (exp);
1513 type = TREE_TYPE (exp);
1514 code = TREE_CODE (type);
1516 if (code == REFERENCE_TYPE)
1518 exp = convert_from_reference (exp);
1519 type = TREE_TYPE (exp);
1520 code = TREE_CODE (type);
1523 if (type == error_mark_node)
1524 return error_mark_node;
1526 if (type_unknown_p (exp))
1528 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1529 return error_mark_node;
1532 /* Constants can be used directly unless they're not loadable. */
1533 if (TREE_CODE (exp) == CONST_DECL)
1534 exp = DECL_INITIAL (exp);
1535 /* Replace a nonvolatile const static variable with its value. We
1536 don't do this for arrays, though; we want the address of the
1537 first element of the array, not the address of the first element
1538 of its initializing constant. */
1539 else if (code != ARRAY_TYPE)
1541 exp = decl_constant_value (exp);
1542 type = TREE_TYPE (exp);
1545 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1546 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1548 if (code == VOID_TYPE)
1550 error ("void value not ignored as it ought to be");
1551 return error_mark_node;
1553 if (code == METHOD_TYPE)
1555 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1556 return build_unary_op (ADDR_EXPR, exp, 0);
1557 if (code == ARRAY_TYPE)
1562 if (TREE_CODE (exp) == INDIRECT_REF)
1564 /* Stripping away the INDIRECT_REF is not the right
1565 thing to do for references... */
1566 tree inner = TREE_OPERAND (exp, 0);
1567 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1569 inner = build1 (CONVERT_EXPR,
1570 build_pointer_type (TREE_TYPE
1571 (TREE_TYPE (inner))),
1573 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1575 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1578 if (TREE_CODE (exp) == COMPOUND_EXPR)
1580 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1581 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1582 TREE_OPERAND (exp, 0), op1);
1586 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1588 error ("invalid use of non-lvalue array");
1589 return error_mark_node;
1592 ptrtype = build_pointer_type (TREE_TYPE (type));
1594 if (TREE_CODE (exp) == VAR_DECL)
1596 /* ??? This is not really quite correct
1597 in that the type of the operand of ADDR_EXPR
1598 is not the target type of the type of the ADDR_EXPR itself.
1599 Question is, can this lossage be avoided? */
1600 adr = build1 (ADDR_EXPR, ptrtype, exp);
1601 if (!cxx_mark_addressable (exp))
1602 return error_mark_node;
1603 TREE_CONSTANT (adr) = staticp (exp);
1604 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1607 /* This way is better for a COMPONENT_REF since it can
1608 simplify the offset for a component. */
1609 adr = build_unary_op (ADDR_EXPR, exp, 1);
1610 return cp_convert (ptrtype, adr);
1613 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1614 rvalues always have cv-unqualified types. */
1615 if (! CLASS_TYPE_P (type))
1616 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1622 default_conversion (tree exp)
1625 enum tree_code code;
1627 exp = decay_conversion (exp);
1629 type = TREE_TYPE (exp);
1630 code = TREE_CODE (type);
1632 if (INTEGRAL_CODE_P (code))
1634 tree t = type_promotes_to (type);
1636 return cp_convert (t, exp);
1642 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1646 inline_conversion (tree exp)
1648 if (TREE_CODE (exp) == FUNCTION_DECL)
1649 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1654 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1655 decay_conversion to one. */
1658 string_conv_p (tree totype, tree exp, int warn)
1662 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1665 t = TREE_TYPE (totype);
1666 if (!same_type_p (t, char_type_node)
1667 && !same_type_p (t, wchar_type_node))
1670 if (TREE_CODE (exp) == STRING_CST)
1672 /* Make sure that we don't try to convert between char and wchar_t. */
1673 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1678 /* Is this a string constant which has decayed to 'const char *'? */
1679 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1680 if (!same_type_p (TREE_TYPE (exp), t))
1683 if (TREE_CODE (exp) != ADDR_EXPR
1684 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1688 /* This warning is not very useful, as it complains about printf. */
1689 if (warn && warn_write_strings)
1690 warning ("deprecated conversion from string constant to `%T'", totype);
1695 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1696 can, for example, use as an lvalue. This code used to be in
1697 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1698 expressions, where we're dealing with aggregates. But now it's again only
1699 called from unary_complex_lvalue. The case (in particular) that led to
1700 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1704 rationalize_conditional_expr (enum tree_code code, tree t)
1706 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1707 the first operand is always the one to be used if both operands
1708 are equal, so we know what conditional expression this used to be. */
1709 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1712 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1713 ? LE_EXPR : GE_EXPR),
1714 TREE_OPERAND (t, 0),
1715 TREE_OPERAND (t, 1)),
1716 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1717 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1721 build_conditional_expr (TREE_OPERAND (t, 0),
1722 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1723 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1726 /* Given the TYPE of an anonymous union field inside T, return the
1727 FIELD_DECL for the field. If not found return NULL_TREE. Because
1728 anonymous unions can nest, we must also search all anonymous unions
1729 that are directly reachable. */
1732 lookup_anon_field (tree t, tree type)
1736 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1738 if (TREE_STATIC (field))
1740 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1743 /* If we find it directly, return the field. */
1744 if (DECL_NAME (field) == NULL_TREE
1745 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1750 /* Otherwise, it could be nested, search harder. */
1751 if (DECL_NAME (field) == NULL_TREE
1752 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1754 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1762 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1763 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1764 non-NULL, it indicates the path to the base used to name MEMBER.
1765 If PRESERVE_REFERENCE is true, the expression returned will have
1766 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1767 returned will have the type referred to by the reference.
1769 This function does not perform access control; that is either done
1770 earlier by the parser when the name of MEMBER is resolved to MEMBER
1771 itself, or later when overload resolution selects one of the
1772 functions indicated by MEMBER. */
1775 build_class_member_access_expr (tree object, tree member,
1776 tree access_path, bool preserve_reference)
1780 tree result = NULL_TREE;
1782 if (object == error_mark_node || member == error_mark_node)
1783 return error_mark_node;
1785 if (TREE_CODE (member) == PSEUDO_DTOR_EXPR)
1788 my_friendly_assert (DECL_P (member) || BASELINK_P (member),
1793 The type of the first expression shall be "class object" (of a
1795 object_type = TREE_TYPE (object);
1796 if (!complete_type_or_else (object_type, object))
1797 return error_mark_node;
1798 if (!CLASS_TYPE_P (object_type))
1800 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1801 member, object, object_type);
1802 return error_mark_node;
1805 /* The standard does not seem to actually say that MEMBER must be a
1806 member of OBJECT_TYPE. However, that is clearly what is
1808 if (DECL_P (member))
1810 member_scope = DECL_CLASS_CONTEXT (member);
1812 if (TREE_DEPRECATED (member))
1813 warn_deprecated_use (member);
1816 member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1817 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1818 presently be the anonymous union. Go outwards until we find a
1819 type related to OBJECT_TYPE. */
1820 while (ANON_AGGR_TYPE_P (member_scope)
1821 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1823 member_scope = TYPE_CONTEXT (member_scope);
1824 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1826 error ("`%D' is not a member of `%T'", member, object_type);
1827 return error_mark_node;
1830 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1831 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
1832 in the frontend; only _DECLs and _REFs are lvalues in the backend. */
1834 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1836 object = build_indirect_ref (temp, NULL);
1839 /* In [expr.ref], there is an explicit list of the valid choices for
1840 MEMBER. We check for each of those cases here. */
1841 if (TREE_CODE (member) == VAR_DECL)
1843 /* A static data member. */
1845 /* If OBJECT has side-effects, they are supposed to occur. */
1846 if (TREE_SIDE_EFFECTS (object))
1847 result = build (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1849 else if (TREE_CODE (member) == FIELD_DECL)
1851 /* A non-static data member. */
1856 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1857 && integer_zerop (TREE_OPERAND (object, 0)));
1859 /* Convert OBJECT to the type of MEMBER. */
1860 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
1861 TYPE_MAIN_VARIANT (member_scope)))
1866 binfo = lookup_base (access_path ? access_path : object_type,
1867 member_scope, ba_ignore, &kind);
1868 if (binfo == error_mark_node)
1869 return error_mark_node;
1871 /* It is invalid to try to get to a virtual base of a
1872 NULL object. The most common cause is invalid use of
1874 if (null_object_p && kind == bk_via_virtual)
1876 error ("invalid access to non-static data member `%D' of NULL object",
1878 error ("(perhaps the `offsetof' macro was used incorrectly)");
1879 return error_mark_node;
1882 /* Convert to the base. */
1883 object = build_base_path (PLUS_EXPR, object, binfo,
1885 /* If we found the base successfully then we should be able
1886 to convert to it successfully. */
1887 my_friendly_assert (object != error_mark_node,
1891 /* Complain about other invalid uses of offsetof, even though they will
1892 give the right answer. Note that we complain whether or not they
1893 actually used the offsetof macro, since there's no way to know at this
1894 point. So we just give a warning, instead of a pedwarn. */
1895 if (null_object_p && warn_invalid_offsetof
1896 && CLASSTYPE_NON_POD_P (object_type))
1898 warning ("invalid access to non-static data member `%D' of NULL object",
1900 warning ("(perhaps the `offsetof' macro was used incorrectly)");
1903 /* If MEMBER is from an anonymous aggregate, we have converted
1904 OBJECT so that it refers to the class containing the
1905 anonymous union. Generate a reference to the anonymous union
1906 itself, and recur to find MEMBER. */
1907 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1908 /* When this code is called from build_field_call, the
1909 object already has the type of the anonymous union.
1910 That is because the COMPONENT_REF was already
1911 constructed, and was then disassembled before calling
1912 build_field_call. After the function-call code is
1913 cleaned up, this waste can be eliminated. */
1914 && (!same_type_ignoring_top_level_qualifiers_p
1915 (TREE_TYPE (object), DECL_CONTEXT (member))))
1917 tree anonymous_union;
1919 anonymous_union = lookup_anon_field (TREE_TYPE (object),
1920 DECL_CONTEXT (member));
1921 object = build_class_member_access_expr (object,
1923 /*access_path=*/NULL_TREE,
1924 preserve_reference);
1927 /* Compute the type of the field, as described in [expr.ref]. */
1928 type_quals = TYPE_UNQUALIFIED;
1929 member_type = TREE_TYPE (member);
1930 if (TREE_CODE (member_type) != REFERENCE_TYPE)
1932 type_quals = (cp_type_quals (member_type)
1933 | cp_type_quals (object_type));
1935 /* A field is const (volatile) if the enclosing object, or the
1936 field itself, is const (volatile). But, a mutable field is
1937 not const, even within a const object. */
1938 if (DECL_MUTABLE_P (member))
1939 type_quals &= ~TYPE_QUAL_CONST;
1940 member_type = cp_build_qualified_type (member_type, type_quals);
1943 result = fold (build (COMPONENT_REF, member_type, object, member));
1945 /* Mark the expression const or volatile, as appropriate. Even
1946 though we've dealt with the type above, we still have to mark the
1947 expression itself. */
1948 if (type_quals & TYPE_QUAL_CONST)
1949 TREE_READONLY (result) = 1;
1950 else if (type_quals & TYPE_QUAL_VOLATILE)
1951 TREE_THIS_VOLATILE (result) = 1;
1953 else if (BASELINK_P (member))
1955 /* The member is a (possibly overloaded) member function. */
1959 /* If the MEMBER is exactly one static member function, then we
1960 know the type of the expression. Otherwise, we must wait
1961 until overload resolution has been performed. */
1962 functions = BASELINK_FUNCTIONS (member);
1963 if (TREE_CODE (functions) == FUNCTION_DECL
1964 && DECL_STATIC_FUNCTION_P (functions))
1965 type = TREE_TYPE (functions);
1967 type = unknown_type_node;
1968 /* Note that we do not convert OBJECT to the BASELINK_BINFO
1969 base. That will happen when the function is called. */
1970 result = build (COMPONENT_REF, type, object, member);
1972 else if (TREE_CODE (member) == CONST_DECL)
1974 /* The member is an enumerator. */
1976 /* If OBJECT has side-effects, they are supposed to occur. */
1977 if (TREE_SIDE_EFFECTS (object))
1978 result = build (COMPOUND_EXPR, TREE_TYPE (result),
1983 error ("invalid use of `%D'", member);
1984 return error_mark_node;
1987 if (!preserve_reference)
1990 If E2 is declared to have type "reference to T", then ... the
1991 type of E1.E2 is T. */
1992 result = convert_from_reference (result);
1997 /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
1998 SCOPE is NULL, by OBJECT.~DTOR_NAME. */
2001 lookup_destructor (tree object, tree scope, tree dtor_name)
2003 tree object_type = TREE_TYPE (object);
2004 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2006 if (scope && !check_dtor_name (scope, dtor_name))
2008 error ("qualified type `%T' does not match destructor name `~%T'",
2010 return error_mark_node;
2012 if (!same_type_p (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2014 error ("destructor name `%T' does not match type `%T' of expression",
2015 dtor_type, object_type);
2016 return error_mark_node;
2018 if (!TYPE_HAS_DESTRUCTOR (object_type))
2019 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope,
2021 return lookup_member (object_type, complete_dtor_identifier,
2022 /*protect=*/1, /*want_type=*/false);
2025 /* This function is called by the parser to process a class member
2026 access expression of the form OBJECT.NAME. NAME is a node used by
2027 the parser to represent a name; it is not yet a DECL. It may,
2028 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2029 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2030 there is no reason to do the lookup twice, so the parser keeps the
2034 finish_class_member_access_expr (tree object, tree name)
2038 tree access_path = NULL_TREE;
2040 if (object == error_mark_node || name == error_mark_node)
2041 return error_mark_node;
2043 if (processing_template_decl)
2044 return build_min_nt (COMPONENT_REF, object, name);
2046 if (TREE_CODE (object) == OFFSET_REF)
2047 object = resolve_offset_ref (object);
2049 object_type = TREE_TYPE (object);
2050 if (TREE_CODE (object_type) == REFERENCE_TYPE)
2052 object = convert_from_reference (object);
2053 object_type = TREE_TYPE (object);
2058 The type of the first expression shall be "class object" (of a
2060 if (!complete_type_or_else (object_type, object))
2061 return error_mark_node;
2062 if (!CLASS_TYPE_P (object_type))
2064 error ("request for member `%D' in `%E', which is of non-class type `%T'",
2065 name, object, object_type);
2066 return error_mark_node;
2069 if (BASELINK_P (name))
2071 /* A member function that has already been looked up. */
2072 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
2073 == TEMPLATE_ID_EXPR),
2079 bool is_template_id = false;
2080 tree template_args = NULL_TREE;
2082 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2084 is_template_id = true;
2085 template_args = TREE_OPERAND (name, 1);
2086 name = TREE_OPERAND (name, 0);
2089 if (TREE_CODE (name) == SCOPE_REF)
2093 /* A qualified name. The qualifying class or namespace `S' has
2094 already been looked up; it is either a TYPE or a
2095 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
2096 or a BIT_NOT_EXPR. */
2097 scope = TREE_OPERAND (name, 0);
2098 name = TREE_OPERAND (name, 1);
2099 my_friendly_assert ((CLASS_TYPE_P (scope)
2100 || TREE_CODE (scope) == NAMESPACE_DECL),
2102 my_friendly_assert ((TREE_CODE (name) == IDENTIFIER_NODE
2103 || TREE_CODE (name) == BIT_NOT_EXPR),
2106 /* If SCOPE is a namespace, then the qualified name does not
2107 name a member of OBJECT_TYPE. */
2108 if (TREE_CODE (scope) == NAMESPACE_DECL)
2110 error ("`%D::%D' is not a member of `%T'",
2111 scope, name, object_type);
2112 return error_mark_node;
2115 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2116 access_path = lookup_base (object_type, scope, ba_check, NULL);
2117 if (!access_path || access_path == error_mark_node)
2118 return error_mark_node;
2120 if (TREE_CODE (name) == BIT_NOT_EXPR)
2121 member = lookup_destructor (object, scope, name);
2124 /* Look up the member. */
2125 member = lookup_member (access_path, name, /*protect=*/1,
2126 /*want_type=*/false);
2127 if (member == NULL_TREE)
2129 error ("'%D' has no member named '%E'", object_type, name);
2130 return error_mark_node;
2132 if (member == error_mark_node)
2133 return error_mark_node;
2136 else if (TREE_CODE (name) == BIT_NOT_EXPR)
2137 member = lookup_destructor (object, /*scope=*/NULL_TREE, name);
2138 else if (TREE_CODE (name) == IDENTIFIER_NODE)
2140 /* An unqualified name. */
2141 member = lookup_member (object_type, name, /*protect=*/1,
2142 /*want_type=*/false);
2143 if (member == NULL_TREE)
2145 error ("'%D' has no member named '%E'", object_type, name);
2146 return error_mark_node;
2148 else if (member == error_mark_node)
2149 return error_mark_node;
2153 /* The YACC parser sometimes gives us things that are not names.
2154 These always indicate errors. The recursive-descent parser
2155 does not do this, so this code can go away once that parser
2156 replaces the YACC parser. */
2157 error ("invalid use of `%D'", name);
2158 return error_mark_node;
2163 tree template = member;
2165 if (BASELINK_P (template))
2166 BASELINK_FUNCTIONS (template)
2167 = build_nt (TEMPLATE_ID_EXPR,
2168 BASELINK_FUNCTIONS (template),
2172 error ("`%D' is not a member template function", name);
2173 return error_mark_node;
2178 if (TREE_DEPRECATED (member))
2179 warn_deprecated_use (member);
2181 return build_class_member_access_expr (object, member, access_path,
2182 /*preserve_reference=*/false);
2185 /* Return an expression for the MEMBER_NAME field in the internal
2186 representation of PTRMEM, a pointer-to-member function. (Each
2187 pointer-to-member function type gets its own RECORD_TYPE so it is
2188 more convenient to access the fields by name than by FIELD_DECL.)
2189 This routine converts the NAME to a FIELD_DECL and then creates the
2190 node for the complete expression. */
2193 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2199 /* This code is a stripped down version of
2200 build_class_member_access_expr. It does not work to use that
2201 routine directly because it expects the object to be of class
2203 ptrmem_type = TREE_TYPE (ptrmem);
2204 my_friendly_assert (TYPE_PTRMEMFUNC_P (ptrmem_type), 20020804);
2205 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2206 /*want_type=*/false);
2207 member_type = cp_build_qualified_type (TREE_TYPE (member),
2208 cp_type_quals (ptrmem_type));
2209 return fold (build (COMPONENT_REF, member_type, ptrmem, member));
2212 /* Given an expression PTR for a pointer, return an expression
2213 for the value pointed to.
2214 ERRORSTRING is the name of the operator to appear in error messages.
2216 This function may need to overload OPERATOR_FNNAME.
2217 Must also handle REFERENCE_TYPEs for C++. */
2220 build_x_indirect_ref (ptr, errorstring)
2222 const char *errorstring;
2226 if (processing_template_decl)
2227 return build_min_nt (INDIRECT_REF, ptr);
2229 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2233 return build_indirect_ref (ptr, errorstring);
2237 build_indirect_ref (ptr, errorstring)
2239 const char *errorstring;
2241 register tree pointer, type;
2243 if (ptr == error_mark_node)
2244 return error_mark_node;
2246 if (ptr == current_class_ptr)
2247 return current_class_ref;
2249 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2250 ? ptr : default_conversion (ptr));
2251 type = TREE_TYPE (pointer);
2253 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2257 If the type of the expression is "pointer to T," the type
2258 of the result is "T."
2260 We must use the canonical variant because certain parts of
2261 the back end, like fold, do pointer comparisons between
2263 tree t = canonical_type_variant (TREE_TYPE (type));
2265 if (VOID_TYPE_P (t))
2267 /* A pointer to incomplete type (other than cv void) can be
2268 dereferenced [expr.unary.op]/1 */
2269 error ("`%T' is not a pointer-to-object type", type);
2270 return error_mark_node;
2272 else if (TREE_CODE (pointer) == ADDR_EXPR
2273 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2274 /* The POINTER was something like `&x'. We simplify `*&x' to
2276 return TREE_OPERAND (pointer, 0);
2279 tree ref = build1 (INDIRECT_REF, t, pointer);
2281 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2282 so that we get the proper error message if the result is used
2283 to assign to. Also, &* is supposed to be a no-op. */
2284 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2285 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2286 TREE_SIDE_EFFECTS (ref)
2287 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2291 /* `pointer' won't be an error_mark_node if we were given a
2292 pointer to member, so it's cool to check for this here. */
2293 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2294 error ("invalid use of `%s' on pointer to member", errorstring);
2295 else if (pointer != error_mark_node)
2298 error ("invalid type argument of `%s'", errorstring);
2300 error ("invalid type argument");
2302 return error_mark_node;
2305 /* This handles expressions of the form "a[i]", which denotes
2308 This is logically equivalent in C to *(a+i), but we may do it differently.
2309 If A is a variable or a member, we generate a primitive ARRAY_REF.
2310 This avoids forcing the array out of registers, and can work on
2311 arrays that are not lvalues (for example, members of structures returned
2314 If INDEX is of some user-defined type, it must be converted to
2315 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2316 will inherit the type of the array, which will be some pointer type. */
2319 build_array_ref (array, idx)
2324 error ("subscript missing in array reference");
2325 return error_mark_node;
2328 if (TREE_TYPE (array) == error_mark_node
2329 || TREE_TYPE (idx) == error_mark_node)
2330 return error_mark_node;
2332 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2334 switch (TREE_CODE (array))
2338 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2339 return build (COMPOUND_EXPR, TREE_TYPE (value),
2340 TREE_OPERAND (array, 0), value);
2344 return build_conditional_expr
2345 (TREE_OPERAND (array, 0),
2346 build_array_ref (TREE_OPERAND (array, 1), idx),
2347 build_array_ref (TREE_OPERAND (array, 2), idx));
2353 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2354 && TREE_CODE (array) != INDIRECT_REF)
2358 /* Subscripting with type char is likely to lose
2359 on a machine where chars are signed.
2360 So warn on any machine, but optionally.
2361 Don't warn for unsigned char since that type is safe.
2362 Don't warn for signed char because anyone who uses that
2363 must have done so deliberately. */
2364 if (warn_char_subscripts
2365 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2366 warning ("array subscript has type `char'");
2368 /* Apply default promotions *after* noticing character types. */
2369 idx = default_conversion (idx);
2371 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2373 error ("array subscript is not an integer");
2374 return error_mark_node;
2377 /* An array that is indexed by a non-constant
2378 cannot be stored in a register; we must be able to do
2379 address arithmetic on its address.
2380 Likewise an array of elements of variable size. */
2381 if (TREE_CODE (idx) != INTEGER_CST
2382 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2383 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2386 if (!cxx_mark_addressable (array))
2387 return error_mark_node;
2390 /* An array that is indexed by a constant value which is not within
2391 the array bounds cannot be stored in a register either; because we
2392 would get a crash in store_bit_field/extract_bit_field when trying
2393 to access a non-existent part of the register. */
2394 if (TREE_CODE (idx) == INTEGER_CST
2395 && TYPE_VALUES (TREE_TYPE (array))
2396 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2398 if (!cxx_mark_addressable (array))
2399 return error_mark_node;
2402 if (pedantic && !lvalue_p (array))
2403 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2405 /* Note in C++ it is valid to subscript a `register' array, since
2406 it is valid to take the address of something with that
2407 storage specification. */
2411 while (TREE_CODE (foo) == COMPONENT_REF)
2412 foo = TREE_OPERAND (foo, 0);
2413 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2414 warning ("subscripting array declared `register'");
2417 type = TREE_TYPE (TREE_TYPE (array));
2418 rval = build (ARRAY_REF, type, array, idx);
2419 /* Array ref is const/volatile if the array elements are
2420 or if the array is.. */
2421 TREE_READONLY (rval)
2422 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2423 TREE_SIDE_EFFECTS (rval)
2424 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2425 TREE_THIS_VOLATILE (rval)
2426 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2427 return require_complete_type (fold (rval));
2431 tree ar = default_conversion (array);
2432 tree ind = default_conversion (idx);
2434 /* Put the integer in IND to simplify error checking. */
2435 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2442 if (ar == error_mark_node)
2445 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2447 error ("subscripted value is neither array nor pointer");
2448 return error_mark_node;
2450 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2452 error ("array subscript is not an integer");
2453 return error_mark_node;
2456 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2461 /* Resolve a pointer to member function. INSTANCE is the object
2462 instance to use, if the member points to a virtual member.
2464 This used to avoid checking for virtual functions if basetype
2465 has no virtual functions, according to an earlier ANSI draft.
2466 With the final ISO C++ rules, such an optimization is
2467 incorrect: A pointer to a derived member can be static_cast
2468 to pointer-to-base-member, as long as the dynamic object
2469 later has the right member. */
2472 get_member_function_from_ptrfunc (instance_ptrptr, function)
2473 tree *instance_ptrptr;
2476 if (TREE_CODE (function) == OFFSET_REF)
2477 function = TREE_OPERAND (function, 1);
2479 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2481 tree idx, delta, e1, e2, e3, vtbl, basetype;
2482 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2484 tree instance_ptr = *instance_ptrptr;
2485 tree instance_save_expr = 0;
2486 if (instance_ptr == error_mark_node)
2488 if (TREE_CODE (function) == PTRMEM_CST)
2490 /* Extracting the function address from a pmf is only
2491 allowed with -Wno-pmf-conversions. It only works for
2493 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2494 e1 = convert (fntype, e1);
2499 error ("object missing in use of `%E'", function);
2500 return error_mark_node;
2504 if (TREE_SIDE_EFFECTS (instance_ptr))
2505 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2507 if (TREE_SIDE_EFFECTS (function))
2508 function = save_expr (function);
2510 /* Start by extracting all the information from the PMF itself. */
2511 e3 = PFN_FROM_PTRMEMFUNC (function);
2512 delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2513 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2514 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2516 case ptrmemfunc_vbit_in_pfn:
2517 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2518 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2521 case ptrmemfunc_vbit_in_delta:
2522 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2523 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2530 /* Convert down to the right base before using the instance. First
2532 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2533 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2534 basetype, ba_check, NULL);
2535 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1);
2536 if (instance_ptr == error_mark_node)
2537 return error_mark_node;
2538 /* ...and then the delta in the PMF. */
2539 instance_ptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2540 instance_ptr, delta);
2542 /* Hand back the adjusted 'this' argument to our caller. */
2543 *instance_ptrptr = instance_ptr;
2545 /* Next extract the vtable pointer from the object. */
2546 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2548 vtbl = build_indirect_ref (vtbl, NULL);
2550 /* Finally, extract the function pointer from the vtable. */
2551 e2 = fold (build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx));
2552 e2 = build_indirect_ref (e2, NULL);
2553 TREE_CONSTANT (e2) = 1;
2555 /* When using function descriptors, the address of the
2556 vtable entry is treated as a function pointer. */
2557 if (TARGET_VTABLE_USES_DESCRIPTORS)
2558 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2559 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2561 TREE_TYPE (e2) = TREE_TYPE (e3);
2562 e1 = build_conditional_expr (e1, e2, e3);
2564 /* Make sure this doesn't get evaluated first inside one of the
2565 branches of the COND_EXPR. */
2566 if (instance_save_expr)
2567 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2568 instance_save_expr, e1);
2576 build_function_call (function, params)
2577 tree function, params;
2579 register tree fntype, fndecl;
2580 register tree coerced_params;
2582 tree name = NULL_TREE, assembler_name = NULL_TREE;
2584 tree original = function;
2586 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2587 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2588 if (TREE_CODE (function) == NOP_EXPR
2589 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2590 function = TREE_OPERAND (function, 0);
2592 if (TREE_CODE (function) == FUNCTION_DECL)
2594 name = DECL_NAME (function);
2595 assembler_name = DECL_ASSEMBLER_NAME (function);
2597 mark_used (function);
2600 /* Convert anything with function type to a pointer-to-function. */
2601 if (pedantic && DECL_MAIN_P (function))
2602 pedwarn ("ISO C++ forbids calling `::main' from within program");
2604 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2605 (because calling an inline function does not mean the function
2606 needs to be separately compiled). */
2608 if (DECL_INLINE (function))
2609 function = inline_conversion (function);
2611 function = build_addr_func (function);
2617 function = build_addr_func (function);
2620 if (function == error_mark_node)
2621 return error_mark_node;
2623 fntype = TREE_TYPE (function);
2625 if (TYPE_PTRMEMFUNC_P (fntype))
2627 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2629 return error_mark_node;
2632 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2633 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2635 if (!((TREE_CODE (fntype) == POINTER_TYPE
2636 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2638 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2640 error ("`%E' cannot be used as a function", original);
2641 return error_mark_node;
2644 /* fntype now gets the type of function pointed to. */
2645 fntype = TREE_TYPE (fntype);
2647 /* Convert the parameters to the types declared in the
2648 function prototype, or apply default promotions. */
2650 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2651 params, fndecl, LOOKUP_NORMAL);
2652 if (coerced_params == error_mark_node)
2653 return error_mark_node;
2655 /* Check for errors in format strings. */
2658 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
2660 /* Recognize certain built-in functions so we can make tree-codes
2661 other than CALL_EXPR. We do this when it enables fold-const.c
2662 to do something useful. */
2664 if (TREE_CODE (function) == ADDR_EXPR
2665 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2666 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2668 result = expand_tree_builtin (TREE_OPERAND (function, 0),
2669 params, coerced_params);
2674 return build_cxx_call (function, params, coerced_params);
2677 /* Convert the actual parameter expressions in the list VALUES
2678 to the types in the list TYPELIST.
2679 If parmdecls is exhausted, or when an element has NULL as its type,
2680 perform the default conversions.
2682 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2684 This is also where warnings about wrong number of args are generated.
2686 Return a list of expressions for the parameters as converted.
2688 Both VALUES and the returned value are chains of TREE_LIST nodes
2689 with the elements of the list in the TREE_VALUE slots of those nodes.
2691 In C++, unspecified trailing parameters can be filled in with their
2692 default arguments, if such were specified. Do so here. */
2695 convert_arguments (typelist, values, fndecl, flags)
2696 tree typelist, values, fndecl;
2699 register tree typetail, valtail;
2700 register tree result = NULL_TREE;
2701 const char *called_thing = 0;
2704 /* Argument passing is always copy-initialization. */
2705 flags |= LOOKUP_ONLYCONVERTING;
2709 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2711 if (DECL_NAME (fndecl) == NULL_TREE
2712 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2713 called_thing = "constructor";
2715 called_thing = "member function";
2718 called_thing = "function";
2721 for (valtail = values, typetail = typelist;
2723 valtail = TREE_CHAIN (valtail), i++)
2725 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2726 register tree val = TREE_VALUE (valtail);
2728 if (val == error_mark_node)
2729 return error_mark_node;
2731 if (type == void_type_node)
2735 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
2737 error ("at this point in file");
2740 error ("too many arguments to function");
2741 /* In case anybody wants to know if this argument
2744 TREE_TYPE (tree_last (result)) = error_mark_node;
2748 if (TREE_CODE (val) == OFFSET_REF)
2749 val = resolve_offset_ref (val);
2751 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2752 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2753 if (TREE_CODE (val) == NOP_EXPR
2754 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2755 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2756 val = TREE_OPERAND (val, 0);
2758 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2760 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2761 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2762 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2763 val = default_conversion (val);
2766 if (val == error_mark_node)
2767 return error_mark_node;
2771 /* Formal parm type is specified by a function prototype. */
2774 if (!COMPLETE_TYPE_P (complete_type (type)))
2776 error ("parameter type of called function is incomplete");
2781 parmval = convert_for_initialization
2782 (NULL_TREE, type, val, flags,
2783 "argument passing", fndecl, i);
2784 parmval = convert_for_arg_passing (type, parmval);
2787 if (parmval == error_mark_node)
2788 return error_mark_node;
2790 result = tree_cons (NULL_TREE, parmval, result);
2794 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2795 val = convert_from_reference (val);
2797 if (fndecl && DECL_BUILT_IN (fndecl)
2798 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2799 /* Don't do ellipsis conversion for __built_in_constant_p
2800 as this will result in spurious warnings for non-POD
2802 val = require_complete_type (val);
2804 val = convert_arg_to_ellipsis (val);
2806 result = tree_cons (NULL_TREE, val, result);
2810 typetail = TREE_CHAIN (typetail);
2813 if (typetail != 0 && typetail != void_list_node)
2815 /* See if there are default arguments that can be used */
2816 if (TREE_PURPOSE (typetail)
2817 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
2819 for (; typetail != void_list_node; ++i)
2822 = convert_default_arg (TREE_VALUE (typetail),
2823 TREE_PURPOSE (typetail),
2826 if (parmval == error_mark_node)
2827 return error_mark_node;
2829 result = tree_cons (0, parmval, result);
2830 typetail = TREE_CHAIN (typetail);
2831 /* ends with `...'. */
2832 if (typetail == NULL_TREE)
2840 cp_error_at ("too few arguments to %s `%+#D'",
2841 called_thing, fndecl);
2842 error ("at this point in file");
2845 error ("too few arguments to function");
2846 return error_mark_list;
2850 return nreverse (result);
2853 /* Build a binary-operation expression, after performing default
2854 conversions on the operands. CODE is the kind of expression to build. */
2857 build_x_binary_op (code, arg1, arg2)
2858 enum tree_code code;
2861 if (processing_template_decl)
2862 return build_min_nt (code, arg1, arg2);
2864 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2870 build_template_expr (enum tree_code code, tree op0, tree op1, tree op2)
2874 /* If any of the operands is erroneous the result is erroneous too. */
2875 if (error_operand_p (op0)
2876 || (op1 && error_operand_p (op1))
2877 || (op2 && error_operand_p (op2)))
2878 return error_mark_node;
2880 if (dependent_type_p (TREE_TYPE (op0))
2881 || (op1 && dependent_type_p (TREE_TYPE (op1)))
2882 || (op2 && dependent_type_p (TREE_TYPE (op2))))
2883 /* If at least one operand has a dependent type, we cannot
2884 determine the type of the expression until instantiation time. */
2888 struct z_candidate *cand;
2893 /* None of the operands is dependent, so we can compute the type
2894 of the expression at this point. We must compute the type so
2895 that in things like:
2898 void f() { S<sizeof(I + 3)> s; ... }
2900 we can tell that the type of "s" is non-dependent.
2902 If we're processing a template argument, we do not want to
2903 actually change the operands in any way. Adding conversions,
2904 performing constant folding, etc., would all change mangled
2905 names. For example, in:
2908 void f(S<sizeof(3 + 4 + I)>);
2910 we need to determine that "3 + 4 + I" has type "int", without
2911 actually turning the expression into "7 + I". */
2912 cand = find_overloaded_op (code, op0, op1, op2);
2914 /* If an overloaded operator was found, the expression will
2915 have the type returned by the function. */
2916 type = non_reference (TREE_TYPE (cand->fn));
2919 /* There is no overloaded operator so we can just use the
2920 default rules for determining the type of the operand. */
2921 op0_type = TREE_TYPE (op0);
2922 op1_type = op1 ? TREE_TYPE (op1) : NULL_TREE;
2923 op2_type = op2 ? TREE_TYPE (op2) : NULL_TREE;
2931 The result of the assignment operation is the value
2932 stored in the left operand. */
2936 /* Implement this case. */
2938 case POSTINCREMENT_EXPR:
2939 case POSTDECREMENT_EXPR:
2942 The type of the result is the cv-unqualified version
2943 of the type of the operand. */
2944 type = TYPE_MAIN_VARIANT (op0_type);
2946 case PREINCREMENT_EXPR:
2947 case PREDECREMENT_EXPR:
2950 The value is the new value of the operand. */
2956 If the type of the expression is "pointer to T", the
2957 type of the result is "T". */
2958 type = TREE_TYPE (op0_type);
2963 If the type of the expression is "T", the type of the
2964 result is "pointer to T". */
2965 /* FIXME: Handle the pointer-to-member case. */
2968 /* FIXME: Implement this case. */
2974 The type of the result is that of the promoted left
2979 /* FIXME: Be careful of special pointer-arithmetic
2984 /* These are GNU extensions; the result type is computed
2985 as it would be for other arithmetic operators. */
2991 case TRUNC_DIV_EXPR:
2992 case TRUNC_MOD_EXPR:
2993 /* [expr.bit.and], [expr.xor], [expr.or], [expr.mul]
2995 The usual arithmetic conversions are performed on the
2996 operands and determine the type of the result. */
2997 /* FIXME: Check that this is possible. */
2998 type = type_after_usual_arithmetic_conversions (t1, t2);
3008 The type of the result is bool. */
3009 type = boolean_type_node;
3011 case TRUTH_ANDIF_EXPR:
3012 case TRUTH_ORIF_EXPR:
3013 /* [expr.log.and], [expr.log.org]
3015 The result is a bool. */
3016 type = boolean_type_node;
3019 /* FIXME: Handle special rules for conditioanl
3028 /* If the type of the expression could not be determined,
3029 something is wrong. */
3032 /* If the type is erroneous, the expression is erroneous
3034 if (type == error_mark_node)
3035 return error_mark_node;
3039 return build_min (code, type, op0, op1, op2, NULL_TREE);
3044 /* Build a binary-operation expression without default conversions.
3045 CODE is the kind of expression to build.
3046 This function differs from `build' in several ways:
3047 the data type of the result is computed and recorded in it,
3048 warnings are generated if arg data types are invalid,
3049 special handling for addition and subtraction of pointers is known,
3050 and some optimization is done (operations on narrow ints
3051 are done in the narrower type when that gives the same result).
3052 Constant folding is also done before the result is returned.
3054 Note that the operands will never have enumeral types
3055 because either they have just had the default conversions performed
3056 or they have both just been converted to some other type in which
3057 the arithmetic is to be done.
3059 C++: must do special pointer arithmetic when implementing
3060 multiple inheritance, and deal with pointer to member functions. */
3063 build_binary_op (code, orig_op0, orig_op1, convert_p)
3064 enum tree_code code;
3065 tree orig_op0, orig_op1;
3066 int convert_p ATTRIBUTE_UNUSED;
3069 register enum tree_code code0, code1;
3072 /* Expression code to give to the expression when it is built.
3073 Normally this is CODE, which is what the caller asked for,
3074 but in some special cases we change it. */
3075 register enum tree_code resultcode = code;
3077 /* Data type in which the computation is to be performed.
3078 In the simplest cases this is the common type of the arguments. */
3079 register tree result_type = NULL;
3081 /* Nonzero means operands have already been type-converted
3082 in whatever way is necessary.
3083 Zero means they need to be converted to RESULT_TYPE. */
3086 /* Nonzero means create the expression with this type, rather than
3088 tree build_type = 0;
3090 /* Nonzero means after finally constructing the expression
3091 convert it to this type. */
3092 tree final_type = 0;
3094 /* Nonzero if this is an operation like MIN or MAX which can
3095 safely be computed in short if both args are promoted shorts.
3096 Also implies COMMON.
3097 -1 indicates a bitwise operation; this makes a difference
3098 in the exact conditions for when it is safe to do the operation
3099 in a narrower mode. */
3102 /* Nonzero if this is a comparison operation;
3103 if both args are promoted shorts, compare the original shorts.
3104 Also implies COMMON. */
3105 int short_compare = 0;
3107 /* Nonzero if this is a right-shift operation, which can be computed on the
3108 original short and then promoted if the operand is a promoted short. */
3109 int short_shift = 0;
3111 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3114 /* Apply default conversions. */
3118 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3119 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3120 || code == TRUTH_XOR_EXPR)
3122 if (!really_overloaded_fn (op0))
3123 op0 = decay_conversion (op0);
3124 if (!really_overloaded_fn (op1))
3125 op1 = decay_conversion (op1);
3129 if (!really_overloaded_fn (op0))
3130 op0 = default_conversion (op0);
3131 if (!really_overloaded_fn (op1))
3132 op1 = default_conversion (op1);
3135 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3136 STRIP_TYPE_NOPS (op0);
3137 STRIP_TYPE_NOPS (op1);
3139 /* DTRT if one side is an overloaded function, but complain about it. */
3140 if (type_unknown_p (op0))
3142 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3143 if (t != error_mark_node)
3145 pedwarn ("assuming cast to type `%T' from overloaded function",
3150 if (type_unknown_p (op1))
3152 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3153 if (t != error_mark_node)
3155 pedwarn ("assuming cast to type `%T' from overloaded function",
3161 type0 = TREE_TYPE (op0);
3162 type1 = TREE_TYPE (op1);
3164 /* The expression codes of the data types of the arguments tell us
3165 whether the arguments are integers, floating, pointers, etc. */
3166 code0 = TREE_CODE (type0);
3167 code1 = TREE_CODE (type1);
3169 /* If an error was already reported for one of the arguments,
3170 avoid reporting another error. */
3172 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3173 return error_mark_node;
3178 /* Handle the pointer + int case. */
3179 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3180 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
3181 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3182 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
3188 /* Subtraction of two similar pointers.
3189 We must subtract them as integers, then divide by object size. */
3190 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3191 && comp_target_types (type0, type1, 1))
3192 return pointer_diff (op0, op1, common_type (type0, type1));
3193 /* Handle pointer minus int. Just like pointer plus int. */
3194 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3195 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
3204 case TRUNC_DIV_EXPR:
3206 case FLOOR_DIV_EXPR:
3207 case ROUND_DIV_EXPR:
3208 case EXACT_DIV_EXPR:
3209 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3210 || code0 == COMPLEX_TYPE)
3211 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3212 || code1 == COMPLEX_TYPE))
3214 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3215 warning ("division by zero in `%E / 0'", op0);
3216 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3217 warning ("division by zero in `%E / 0.'", op0);
3219 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3220 resultcode = RDIV_EXPR;
3222 /* When dividing two signed integers, we have to promote to int.
3223 unless we divide by a constant != -1. Note that default
3224 conversion will have been performed on the operands at this
3225 point, so we have to dig out the original type to find out if
3227 shorten = ((TREE_CODE (op0) == NOP_EXPR
3228 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3229 || (TREE_CODE (op1) == INTEGER_CST
3230 && ! integer_all_onesp (op1)));
3237 case BIT_ANDTC_EXPR:
3240 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3244 case TRUNC_MOD_EXPR:
3245 case FLOOR_MOD_EXPR:
3246 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3247 warning ("division by zero in `%E %% 0'", op0);
3248 else if (code1 == REAL_TYPE && real_zerop (op1))
3249 warning ("division by zero in `%E %% 0.'", op0);
3251 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3253 /* Although it would be tempting to shorten always here, that loses
3254 on some targets, since the modulo instruction is undefined if the
3255 quotient can't be represented in the computation mode. We shorten
3256 only if unsigned or if dividing by something we know != -1. */
3257 shorten = ((TREE_CODE (op0) == NOP_EXPR
3258 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3259 || (TREE_CODE (op1) == INTEGER_CST
3260 && ! integer_all_onesp (op1)));
3265 case TRUTH_ANDIF_EXPR:
3266 case TRUTH_ORIF_EXPR:
3267 case TRUTH_AND_EXPR:
3269 result_type = boolean_type_node;
3272 /* Shift operations: result has same type as first operand;
3273 always convert second operand to int.
3274 Also set SHORT_SHIFT if shifting rightward. */
3277 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3279 result_type = type0;
3280 if (TREE_CODE (op1) == INTEGER_CST)
3282 if (tree_int_cst_lt (op1, integer_zero_node))
3283 warning ("right shift count is negative");
3286 if (! integer_zerop (op1))
3288 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3289 warning ("right shift count >= width of type");
3292 /* Convert the shift-count to an integer, regardless of
3293 size of value being shifted. */
3294 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3295 op1 = cp_convert (integer_type_node, op1);
3296 /* Avoid converting op1 to result_type later. */
3302 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3304 result_type = type0;
3305 if (TREE_CODE (op1) == INTEGER_CST)
3307 if (tree_int_cst_lt (op1, integer_zero_node))
3308 warning ("left shift count is negative");
3309 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3310 warning ("left shift count >= width of type");
3312 /* Convert the shift-count to an integer, regardless of
3313 size of value being shifted. */
3314 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3315 op1 = cp_convert (integer_type_node, op1);
3316 /* Avoid converting op1 to result_type later. */
3323 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3325 result_type = type0;
3326 if (TREE_CODE (op1) == INTEGER_CST)
3328 if (tree_int_cst_lt (op1, integer_zero_node))
3329 warning ("%s rotate count is negative",
3330 (code == LROTATE_EXPR) ? "left" : "right");
3331 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3332 warning ("%s rotate count >= width of type",
3333 (code == LROTATE_EXPR) ? "left" : "right");
3335 /* Convert the shift-count to an integer, regardless of
3336 size of value being shifted. */
3337 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3338 op1 = cp_convert (integer_type_node, op1);
3344 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3345 warning ("comparing floating point with == or != is unsafe");
3347 build_type = boolean_type_node;
3348 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3349 || code0 == COMPLEX_TYPE)
3350 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3351 || code1 == COMPLEX_TYPE))
3353 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3354 result_type = composite_pointer_type (type0, type1, op0, op1,
3356 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3357 result_type = type0;
3358 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3359 result_type = type1;
3360 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3362 result_type = type0;
3363 error ("ISO C++ forbids comparison between pointer and integer");
3365 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3367 result_type = type1;
3368 error ("ISO C++ forbids comparison between pointer and integer");
3370 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3372 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3373 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3374 result_type = TREE_TYPE (op0);
3376 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3377 return cp_build_binary_op (code, op1, op0);
3378 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3379 && same_type_p (type0, type1))
3381 /* E will be the final comparison. */
3383 /* E1 and E2 are for scratch. */
3391 if (TREE_SIDE_EFFECTS (op0))
3392 op0 = save_expr (op0);
3393 if (TREE_SIDE_EFFECTS (op1))
3394 op1 = save_expr (op1);
3399 && (!op0.pfn || op0.delta == op1.delta))
3401 The reason for the `!op0.pfn' bit is that a NULL
3402 pointer-to-member is any member with a zero PFN; the
3403 DELTA field is unspecified. */
3404 pfn0 = pfn_from_ptrmemfunc (op0);
3405 pfn1 = pfn_from_ptrmemfunc (op1);
3406 delta0 = build_ptrmemfunc_access_expr (op0,
3408 delta1 = build_ptrmemfunc_access_expr (op1,
3410 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3411 e2 = cp_build_binary_op (EQ_EXPR,
3413 cp_convert (TREE_TYPE (pfn0),
3414 integer_zero_node));
3415 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3416 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3417 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3418 if (code == EQ_EXPR)
3420 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3422 else if ((TYPE_PTRMEMFUNC_P (type0)
3423 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3424 || (TYPE_PTRMEMFUNC_P (type1)
3425 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3431 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3432 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3434 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3435 result_type = composite_pointer_type (type0, type1, op0, op1,
3443 build_type = boolean_type_node;
3444 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3445 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3447 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3448 result_type = composite_pointer_type (type0, type1, op0, op1,
3450 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3451 && integer_zerop (op1))
3452 result_type = type0;
3453 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3454 && integer_zerop (op0))
3455 result_type = type1;
3456 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3458 result_type = type0;
3459 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3461 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3463 result_type = type1;
3464 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3468 case UNORDERED_EXPR:
3475 build_type = integer_type_node;
3476 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3478 error ("unordered comparison on non-floating point argument");
3479 return error_mark_node;
3488 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3490 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3492 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3494 if (shorten || common || short_compare)
3495 result_type = common_type (type0, type1);
3497 /* For certain operations (which identify themselves by shorten != 0)
3498 if both args were extended from the same smaller type,
3499 do the arithmetic in that type and then extend.
3501 shorten !=0 and !=1 indicates a bitwise operation.
3502 For them, this optimization is safe only if
3503 both args are zero-extended or both are sign-extended.
3504 Otherwise, we might change the result.
3505 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3506 but calculated in (unsigned short) it would be (unsigned short)-1. */
3508 if (shorten && none_complex)
3510 int unsigned0, unsigned1;
3511 tree arg0 = get_narrower (op0, &unsigned0);
3512 tree arg1 = get_narrower (op1, &unsigned1);
3513 /* UNS is 1 if the operation to be done is an unsigned one. */
3514 int uns = TREE_UNSIGNED (result_type);
3517 final_type = result_type;
3519 /* Handle the case that OP0 does not *contain* a conversion
3520 but it *requires* conversion to FINAL_TYPE. */
3522 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3523 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3524 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3525 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3527 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3529 /* For bitwise operations, signedness of nominal type
3530 does not matter. Consider only how operands were extended. */
3534 /* Note that in all three cases below we refrain from optimizing
3535 an unsigned operation on sign-extended args.
3536 That would not be valid. */
3538 /* Both args variable: if both extended in same way
3539 from same width, do it in that width.
3540 Do it unsigned if args were zero-extended. */
3541 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3542 < TYPE_PRECISION (result_type))
3543 && (TYPE_PRECISION (TREE_TYPE (arg1))
3544 == TYPE_PRECISION (TREE_TYPE (arg0)))
3545 && unsigned0 == unsigned1
3546 && (unsigned0 || !uns))
3547 result_type = c_common_signed_or_unsigned_type
3548 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3549 else if (TREE_CODE (arg0) == INTEGER_CST
3550 && (unsigned1 || !uns)
3551 && (TYPE_PRECISION (TREE_TYPE (arg1))
3552 < TYPE_PRECISION (result_type))
3553 && (type = c_common_signed_or_unsigned_type
3554 (unsigned1, TREE_TYPE (arg1)),
3555 int_fits_type_p (arg0, type)))
3557 else if (TREE_CODE (arg1) == INTEGER_CST
3558 && (unsigned0 || !uns)
3559 && (TYPE_PRECISION (TREE_TYPE (arg0))
3560 < TYPE_PRECISION (result_type))
3561 && (type = c_common_signed_or_unsigned_type
3562 (unsigned0, TREE_TYPE (arg0)),
3563 int_fits_type_p (arg1, type)))
3567 /* Shifts can be shortened if shifting right. */
3572 tree arg0 = get_narrower (op0, &unsigned_arg);
3574 final_type = result_type;
3576 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3577 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3579 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3580 /* We can shorten only if the shift count is less than the
3581 number of bits in the smaller type size. */
3582 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3583 /* If arg is sign-extended and then unsigned-shifted,
3584 we can simulate this with a signed shift in arg's type
3585 only if the extended result is at least twice as wide
3586 as the arg. Otherwise, the shift could use up all the
3587 ones made by sign-extension and bring in zeros.
3588 We can't optimize that case at all, but in most machines
3589 it never happens because available widths are 2**N. */
3590 && (!TREE_UNSIGNED (final_type)
3592 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3593 <= TYPE_PRECISION (result_type))))
3595 /* Do an unsigned shift if the operand was zero-extended. */
3597 = c_common_signed_or_unsigned_type (unsigned_arg,
3599 /* Convert value-to-be-shifted to that type. */
3600 if (TREE_TYPE (op0) != result_type)
3601 op0 = cp_convert (result_type, op0);
3606 /* Comparison operations are shortened too but differently.
3607 They identify themselves by setting short_compare = 1. */
3611 /* Don't write &op0, etc., because that would prevent op0
3612 from being kept in a register.
3613 Instead, make copies of the our local variables and
3614 pass the copies by reference, then copy them back afterward. */
3615 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3616 enum tree_code xresultcode = resultcode;
3618 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3620 return cp_convert (boolean_type_node, val);
3621 op0 = xop0, op1 = xop1;
3623 resultcode = xresultcode;
3626 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3627 && warn_sign_compare)
3629 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3630 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3632 int unsignedp0, unsignedp1;
3633 tree primop0 = get_narrower (op0, &unsignedp0);
3634 tree primop1 = get_narrower (op1, &unsignedp1);
3636 /* Check for comparison of different enum types. */
3637 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3638 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3639 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3640 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3642 warning ("comparison between types `%#T' and `%#T'",
3643 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3646 /* Give warnings for comparisons between signed and unsigned
3647 quantities that may fail. */
3648 /* Do the checking based on the original operand trees, so that
3649 casts will be considered, but default promotions won't be. */
3651 /* Do not warn if the comparison is being done in a signed type,
3652 since the signed type will only be chosen if it can represent
3653 all the values of the unsigned type. */
3654 if (! TREE_UNSIGNED (result_type))
3656 /* Do not warn if both operands are unsigned. */
3657 else if (op0_signed == op1_signed)
3659 /* Do not warn if the signed quantity is an unsuffixed
3660 integer literal (or some static constant expression
3661 involving such literals or a conditional expression
3662 involving such literals) and it is non-negative. */
3663 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3664 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3666 /* Do not warn if the comparison is an equality operation,
3667 the unsigned quantity is an integral constant and it does
3668 not use the most significant bit of result_type. */
3669 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3670 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3671 && int_fits_type_p (orig_op1, c_common_signed_type
3673 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3674 && int_fits_type_p (orig_op0, c_common_signed_type
3678 warning ("comparison between signed and unsigned integer expressions");
3680 /* Warn if two unsigned values are being compared in a size
3681 larger than their original size, and one (and only one) is the
3682 result of a `~' operator. This comparison will always fail.
3684 Also warn if one operand is a constant, and the constant does not
3685 have all bits set that are set in the ~ operand when it is
3688 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3689 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3691 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3692 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3693 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3694 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3696 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3699 HOST_WIDE_INT constant, mask;
3703 if (host_integerp (primop0, 0))
3706 unsignedp = unsignedp1;
3707 constant = tree_low_cst (primop0, 0);
3712 unsignedp = unsignedp0;
3713 constant = tree_low_cst (primop1, 0);
3716 bits = TYPE_PRECISION (TREE_TYPE (primop));
3717 if (bits < TYPE_PRECISION (result_type)
3718 && bits < HOST_BITS_PER_LONG && unsignedp)
3720 mask = (~ (HOST_WIDE_INT) 0) << bits;
3721 if ((mask & constant) != mask)
3722 warning ("comparison of promoted ~unsigned with constant");
3725 else if (unsignedp0 && unsignedp1
3726 && (TYPE_PRECISION (TREE_TYPE (primop0))
3727 < TYPE_PRECISION (result_type))
3728 && (TYPE_PRECISION (TREE_TYPE (primop1))
3729 < TYPE_PRECISION (result_type)))
3730 warning ("comparison of promoted ~unsigned with unsigned");
3735 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3736 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3737 Then the expression will be built.
3738 It will be given type FINAL_TYPE if that is nonzero;
3739 otherwise, it will be given type RESULT_TYPE. */
3743 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3744 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3745 return error_mark_node;
3748 /* Issue warnings about peculiar, but valid, uses of NULL. */
3749 if (/* It's reasonable to use pointer values as operands of &&
3750 and ||, so NULL is no exception. */
3751 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3752 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3753 (orig_op0 == null_node
3754 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3755 /* Or vice versa. */
3756 || (orig_op1 == null_node
3757 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3758 /* Or, both are NULL and the operation was not a comparison. */
3759 || (orig_op0 == null_node && orig_op1 == null_node
3760 && code != EQ_EXPR && code != NE_EXPR)))
3761 /* Some sort of arithmetic operation involving NULL was
3762 performed. Note that pointer-difference and pointer-addition
3763 have already been handled above, and so we don't end up here in
3765 warning ("NULL used in arithmetic");
3769 if (TREE_TYPE (op0) != result_type)
3770 op0 = cp_convert (result_type, op0);
3771 if (TREE_TYPE (op1) != result_type)
3772 op1 = cp_convert (result_type, op1);
3774 if (op0 == error_mark_node || op1 == error_mark_node)
3775 return error_mark_node;
3778 if (build_type == NULL_TREE)
3779 build_type = result_type;
3782 register tree result = build (resultcode, build_type, op0, op1);
3783 register tree folded;
3785 folded = fold (result);
3786 if (folded == result)
3787 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3788 if (final_type != 0)
3789 return cp_convert (final_type, folded);
3794 /* Return a tree for the sum or difference (RESULTCODE says which)
3795 of pointer PTROP and integer INTOP. */
3798 cp_pointer_int_sum (resultcode, ptrop, intop)
3799 enum tree_code resultcode;
3800 register tree ptrop, intop;
3802 tree res_type = TREE_TYPE (ptrop);
3804 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3805 in certain circumstance (when it's valid to do so). So we need
3806 to make sure it's complete. We don't need to check here, if we
3807 can actually complete it at all, as those checks will be done in
3808 pointer_int_sum() anyway. */
3809 complete_type (TREE_TYPE (res_type));
3811 return pointer_int_sum (resultcode, ptrop, fold (intop));
3814 /* Return a tree for the difference of pointers OP0 and OP1.
3815 The resulting tree has type int. */
3818 pointer_diff (op0, op1, ptrtype)
3819 register tree op0, op1;
3820 register tree ptrtype;
3822 register tree result, folded;
3823 tree restype = ptrdiff_type_node;
3824 tree target_type = TREE_TYPE (ptrtype);
3826 if (!complete_type_or_else (target_type, NULL_TREE))
3827 return error_mark_node;
3829 if (pedantic || warn_pointer_arith)
3831 if (TREE_CODE (target_type) == VOID_TYPE)
3832 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
3833 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3834 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3835 if (TREE_CODE (target_type) == METHOD_TYPE)
3836 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3837 if (TREE_CODE (target_type) == OFFSET_TYPE)
3838 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
3841 /* First do the subtraction as integers;
3842 then drop through to build the divide operator. */
3844 op0 = cp_build_binary_op (MINUS_EXPR,
3845 cp_convert (restype, op0),
3846 cp_convert (restype, op1));
3848 /* This generates an error if op1 is a pointer to an incomplete type. */
3849 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3850 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3852 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3853 || TREE_CODE (target_type) == FUNCTION_TYPE
3854 || TREE_CODE (target_type) == METHOD_TYPE
3855 || TREE_CODE (target_type) == OFFSET_TYPE)
3857 : size_in_bytes (target_type));
3859 /* Do the division. */
3861 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3863 folded = fold (result);
3864 if (folded == result)
3865 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3869 /* Construct and perhaps optimize a tree representation
3870 for a unary operation. CODE, a tree_code, specifies the operation
3871 and XARG is the operand. */
3874 build_x_unary_op (code, xarg)
3875 enum tree_code code;
3881 if (processing_template_decl)
3882 return build_min_nt (code, xarg, NULL_TREE);
3884 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3886 if (code == ADDR_EXPR
3887 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3888 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
3889 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
3890 || (TREE_CODE (xarg) == OFFSET_REF)))
3891 /* don't look for a function */;
3896 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
3897 NULL_TREE, NULL_TREE);
3898 if (rval || code != ADDR_EXPR)
3901 if (code == ADDR_EXPR)
3903 /* A pointer to member-function can be formed only by saying
3905 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
3906 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
3908 if (TREE_CODE (xarg) != OFFSET_REF)
3910 error ("invalid use of '%E' to form a pointer-to-member-function. Use a qualified-id.",
3912 return error_mark_node;
3916 error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function",
3918 PTRMEM_OK_P (xarg) = 1;
3922 if (TREE_CODE (xarg) == OFFSET_REF)
3924 ptrmem = PTRMEM_OK_P (xarg);
3926 if (!ptrmem && !flag_ms_extensions
3927 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
3929 /* A single non-static member, make sure we don't allow a
3930 pointer-to-member. */
3931 xarg = build (OFFSET_REF, TREE_TYPE (xarg),
3932 TREE_OPERAND (xarg, 0),
3933 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
3934 PTRMEM_OK_P (xarg) = ptrmem;
3938 else if (TREE_CODE (xarg) == TARGET_EXPR)
3939 warning ("taking address of temporary");
3941 exp = build_unary_op (code, xarg, 0);
3942 if (TREE_CODE (exp) == ADDR_EXPR)
3943 PTRMEM_OK_P (exp) = ptrmem;
3948 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
3949 constants, where a null value is represented by an INTEGER_CST of
3953 cp_truthvalue_conversion (expr)
3956 tree type = TREE_TYPE (expr);
3957 if (TYPE_PTRMEM_P (type))
3958 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3960 return c_common_truthvalue_conversion (expr);
3963 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3966 condition_conversion (expr)
3970 if (processing_template_decl)
3972 if (TREE_CODE (expr) == OFFSET_REF)
3973 expr = resolve_offset_ref (expr);
3974 t = perform_implicit_conversion (boolean_type_node, expr);
3975 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
3979 /* Return an ADDR_EXPR giving the address of T. This function
3980 attempts no optimizations or simplifications; it is a low-level
3984 build_address (tree t)
3988 if (error_operand_p (t) || !cxx_mark_addressable (t))
3989 return error_mark_node;
3991 addr = build1 (ADDR_EXPR,
3992 build_pointer_type (TREE_TYPE (t)),
3995 TREE_CONSTANT (addr) = 1;
4000 /* Return a NOP_EXPR converting EXPR to TYPE. */
4003 build_nop (tree type, tree expr)
4007 if (type == error_mark_node || error_operand_p (expr))
4010 nop = build1 (NOP_EXPR, type, expr);
4011 if (TREE_CONSTANT (expr))
4012 TREE_CONSTANT (nop) = 1;
4017 /* C++: Must handle pointers to members.
4019 Perhaps type instantiation should be extended to handle conversion
4020 from aggregates to types we don't yet know we want? (Or are those
4021 cases typically errors which should be reported?)
4023 NOCONVERT nonzero suppresses the default promotions
4024 (such as from short to int). */
4027 build_unary_op (enum tree_code code, tree xarg, int noconvert)
4029 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4030 register tree arg = xarg;
4031 register tree argtype = 0;
4032 const char *errstring = NULL;
4035 if (arg == error_mark_node)
4036 return error_mark_node;
4041 /* This is used for unary plus, because a CONVERT_EXPR
4042 is enough to prevent anybody from looking inside for
4043 associativity, but won't generate any code. */
4044 if (!(arg = build_expr_type_conversion
4045 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, true)))
4046 errstring = "wrong type argument to unary plus";
4050 arg = default_conversion (arg);
4051 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4052 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4057 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4058 errstring = "wrong type argument to unary minus";
4059 else if (!noconvert)
4060 arg = default_conversion (arg);
4064 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4068 arg = default_conversion (arg);
4070 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4072 errstring = "wrong type argument to bit-complement";
4073 else if (!noconvert)
4074 arg = default_conversion (arg);
4078 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4079 errstring = "wrong type argument to abs";
4080 else if (!noconvert)
4081 arg = default_conversion (arg);
4085 /* Conjugating a real value is a no-op, but allow it anyway. */
4086 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4087 errstring = "wrong type argument to conjugation";
4088 else if (!noconvert)
4089 arg = default_conversion (arg);
4092 case TRUTH_NOT_EXPR:
4093 arg = cp_convert (boolean_type_node, arg);
4094 val = invert_truthvalue (arg);
4095 if (arg != error_mark_node)
4097 errstring = "in argument to unary !";
4104 if (TREE_CODE (arg) == COMPLEX_CST)
4105 return TREE_REALPART (arg);
4106 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4107 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4112 if (TREE_CODE (arg) == COMPLEX_CST)
4113 return TREE_IMAGPART (arg);
4114 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4115 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4117 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4119 case PREINCREMENT_EXPR:
4120 case POSTINCREMENT_EXPR:
4121 case PREDECREMENT_EXPR:
4122 case POSTDECREMENT_EXPR:
4123 /* Handle complex lvalues (when permitted)
4124 by reduction to simpler cases. */
4126 val = unary_complex_lvalue (code, arg);
4130 /* Increment or decrement the real part of the value,
4131 and don't change the imaginary part. */
4132 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4136 arg = stabilize_reference (arg);
4137 real = build_unary_op (REALPART_EXPR, arg, 1);
4138 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4139 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4140 build_unary_op (code, real, 1), imag);
4143 /* Report invalid types. */
4145 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4148 if (code == PREINCREMENT_EXPR)
4149 errstring ="no pre-increment operator for type";
4150 else if (code == POSTINCREMENT_EXPR)
4151 errstring ="no post-increment operator for type";
4152 else if (code == PREDECREMENT_EXPR)
4153 errstring ="no pre-decrement operator for type";
4155 errstring ="no post-decrement operator for type";
4159 /* Report something read-only. */
4161 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4162 || TREE_READONLY (arg))
4163 readonly_error (arg, ((code == PREINCREMENT_EXPR
4164 || code == POSTINCREMENT_EXPR)
4165 ? "increment" : "decrement"),
4170 tree result_type = TREE_TYPE (arg);
4172 arg = get_unwidened (arg, 0);
4173 argtype = TREE_TYPE (arg);
4175 /* ARM $5.2.5 last annotation says this should be forbidden. */
4176 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4177 pedwarn ("ISO C++ forbids %sing an enum",
4178 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4179 ? "increment" : "decrement");
4181 /* Compute the increment. */
4183 if (TREE_CODE (argtype) == POINTER_TYPE)
4185 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4186 tree type = complete_type (TREE_TYPE (argtype));
4188 if (!COMPLETE_OR_VOID_TYPE_P (type))
4189 error ("cannot %s a pointer to incomplete type `%T'",
4190 ((code == PREINCREMENT_EXPR
4191 || code == POSTINCREMENT_EXPR)
4192 ? "increment" : "decrement"), TREE_TYPE (argtype));
4193 else if ((pedantic || warn_pointer_arith)
4194 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4195 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4196 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4197 ((code == PREINCREMENT_EXPR
4198 || code == POSTINCREMENT_EXPR)
4199 ? "increment" : "decrement"), argtype);
4200 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
4203 inc = integer_one_node;
4205 inc = cp_convert (argtype, inc);
4207 /* Handle incrementing a cast-expression. */
4209 switch (TREE_CODE (arg))
4214 case FIX_TRUNC_EXPR:
4215 case FIX_FLOOR_EXPR:
4216 case FIX_ROUND_EXPR:
4219 tree incremented, modify, value, compound;
4220 if (! lvalue_p (arg) && pedantic)
4221 pedwarn ("cast to non-reference type used as lvalue");
4222 arg = stabilize_reference (arg);
4223 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4226 value = save_expr (arg);
4227 incremented = build (((code == PREINCREMENT_EXPR
4228 || code == POSTINCREMENT_EXPR)
4229 ? PLUS_EXPR : MINUS_EXPR),
4230 argtype, value, inc);
4232 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4233 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4235 /* Eliminate warning about unused result of + or -. */
4236 TREE_NO_UNUSED_WARNING (compound) = 1;
4244 /* Complain about anything else that is not a true lvalue. */
4245 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4246 || code == POSTINCREMENT_EXPR)
4247 ? "increment" : "decrement")))
4248 return error_mark_node;
4250 /* Forbid using -- on `bool'. */
4251 if (TREE_TYPE (arg) == boolean_type_node)
4253 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4255 error ("invalid use of `--' on bool variable `%D'", arg);
4256 return error_mark_node;
4259 /* This will only work if someone can convince Kenner to accept
4260 my patch to expand_increment. (jason) */
4261 val = build (code, TREE_TYPE (arg), arg, inc);
4263 val = boolean_increment (code, arg);
4267 val = build (code, TREE_TYPE (arg), arg, inc);
4269 TREE_SIDE_EFFECTS (val) = 1;
4270 return cp_convert (result_type, val);
4274 /* Note that this operation never does default_conversion
4275 regardless of NOCONVERT. */
4277 argtype = lvalue_type (arg);
4278 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4282 build_pointer_type (TREE_TYPE (argtype)), arg);
4283 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4286 else if (pedantic && DECL_MAIN_P (arg))
4288 pedwarn ("ISO C++ forbids taking address of function `::main'");
4290 /* Let &* cancel out to simplify resulting code. */
4291 if (TREE_CODE (arg) == INDIRECT_REF)
4293 /* We don't need to have `current_class_ptr' wrapped in a
4294 NON_LVALUE_EXPR node. */
4295 if (arg == current_class_ref)
4296 return current_class_ptr;
4298 arg = TREE_OPERAND (arg, 0);
4299 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4303 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4304 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4306 else if (lvalue_p (arg))
4307 /* Don't let this be an lvalue. */
4308 return non_lvalue (arg);
4312 /* For &x[y], return x+y */
4313 if (TREE_CODE (arg) == ARRAY_REF)
4315 if (!cxx_mark_addressable (TREE_OPERAND (arg, 0)))
4316 return error_mark_node;
4317 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4318 TREE_OPERAND (arg, 1));
4321 /* Uninstantiated types are all functions. Taking the
4322 address of a function is a no-op, so just return the
4325 if (TREE_CODE (arg) == IDENTIFIER_NODE
4326 && IDENTIFIER_OPNAME_P (arg))
4329 /* We don't know the type yet, so just work around the problem.
4330 We know that this will resolve to an lvalue. */
4331 return build1 (ADDR_EXPR, unknown_type_node, arg);
4334 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4335 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4337 /* They're trying to take the address of a unique non-static
4338 member function. This is ill-formed (except in MS-land),
4339 but let's try to DTRT.
4340 Note: We only handle unique functions here because we don't
4341 want to complain if there's a static overload; non-unique
4342 cases will be handled by instantiate_type. But we need to
4343 handle this case here to allow casts on the resulting PMF.
4344 We could defer this in non-MS mode, but it's easier to give
4345 a useful error here. */
4347 /* Inside constant member functions, the `this' pointer
4348 contains an extra const qualifier. TYPE_MAIN_VARIANT
4349 is used here to remove this const from the diagnostics
4350 and the created OFFSET_REF. */
4351 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4352 tree name = DECL_NAME (get_first_fn (TREE_OPERAND (arg, 1)));
4354 if (! flag_ms_extensions)
4356 if (current_class_type
4357 && TREE_OPERAND (arg, 0) == current_class_ref)
4358 /* An expression like &memfn. */
4359 pedwarn ("ISO C++ forbids taking the address of an unqualified"
4360 " or parenthesized non-static member function to form"
4361 " a pointer to member function. Say `&%T::%D'",
4364 pedwarn ("ISO C++ forbids taking the address of a bound member"
4365 " function to form a pointer to member function."
4369 arg = build_offset_ref (base, name);
4372 if (type_unknown_p (arg))
4373 return build1 (ADDR_EXPR, unknown_type_node, arg);
4375 /* Handle complex lvalues (when permitted)
4376 by reduction to simpler cases. */
4377 val = unary_complex_lvalue (code, arg);
4381 switch (TREE_CODE (arg))
4386 case FIX_TRUNC_EXPR:
4387 case FIX_FLOOR_EXPR:
4388 case FIX_ROUND_EXPR:
4390 if (! lvalue_p (arg) && pedantic)
4391 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4398 /* Allow the address of a constructor if all the elements
4400 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4401 && TREE_CONSTANT (arg))
4403 /* Anything not already handled and not a true memory reference
4405 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4406 && TREE_CODE (argtype) != METHOD_TYPE
4407 && !non_cast_lvalue_or_else (arg, "unary `&'"))
4408 return error_mark_node;
4410 if (argtype != error_mark_node)
4411 argtype = build_pointer_type (argtype);
4416 if (TREE_CODE (arg) == COMPONENT_REF
4417 && TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
4418 arg = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4420 if (TREE_CODE (arg) != COMPONENT_REF)
4421 addr = build_address (arg);
4422 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4424 error ("attempt to take address of bit-field structure member `%D'",
4425 TREE_OPERAND (arg, 1));
4426 return error_mark_node;
4430 /* Unfortunately we cannot just build an address
4431 expression here, because we would not handle
4432 address-constant-expressions or offsetof correctly. */
4433 tree field = TREE_OPERAND (arg, 1);
4434 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4435 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)),
4436 decl_type_context (field),
4439 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
4440 rval = build1 (NOP_EXPR, argtype, rval);
4441 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4442 addr = fold (build (PLUS_EXPR, argtype, rval,
4443 cp_convert (argtype, byte_position (field))));
4446 if (TREE_CODE (argtype) == POINTER_TYPE
4447 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4449 build_ptrmemfunc_type (argtype);
4450 addr = build_ptrmemfunc (argtype, addr, 0);
4463 argtype = TREE_TYPE (arg);
4464 return fold (build1 (code, argtype, arg));
4467 error ("%s", errstring);
4468 return error_mark_node;
4471 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4472 for certain kinds of expressions which are not really lvalues
4473 but which we can accept as lvalues.
4475 If ARG is not a kind of expression we can handle, return zero. */
4478 unary_complex_lvalue (enum tree_code code, tree arg)
4480 /* Handle (a, b) used as an "lvalue". */
4481 if (TREE_CODE (arg) == COMPOUND_EXPR)
4483 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4484 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4485 TREE_OPERAND (arg, 0), real_result);
4488 /* Handle (a ? b : c) used as an "lvalue". */
4489 if (TREE_CODE (arg) == COND_EXPR
4490 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4491 return rationalize_conditional_expr (code, arg);
4493 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4494 if (TREE_CODE (arg) == MODIFY_EXPR
4495 || TREE_CODE (arg) == PREINCREMENT_EXPR
4496 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4498 tree lvalue = TREE_OPERAND (arg, 0);
4499 if (TREE_SIDE_EFFECTS (lvalue))
4501 lvalue = stabilize_reference (lvalue);
4502 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4503 lvalue, TREE_OPERAND (arg, 1));
4505 return unary_complex_lvalue
4506 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4509 if (code != ADDR_EXPR)
4512 /* Handle (a = b) used as an "lvalue" for `&'. */
4513 if (TREE_CODE (arg) == MODIFY_EXPR
4514 || TREE_CODE (arg) == INIT_EXPR)
4516 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4517 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4518 TREE_NO_UNUSED_WARNING (arg) = 1;
4522 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4523 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4524 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4526 /* The representation of something of type OFFSET_TYPE
4527 is really the representation of a pointer to it.
4528 Here give the representation its true type. */
4531 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4533 if (TREE_CODE (arg) != OFFSET_REF)
4536 t = TREE_OPERAND (arg, 1);
4538 /* Check all this code for right semantics. */
4539 if (TREE_CODE (t) == FUNCTION_DECL)
4541 if (DECL_DESTRUCTOR_P (t))
4542 error ("taking address of destructor");
4543 return build_unary_op (ADDR_EXPR, t, 0);
4545 if (TREE_CODE (t) == VAR_DECL)
4546 return build_unary_op (ADDR_EXPR, t, 0);
4551 if (TREE_OPERAND (arg, 0)
4552 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4553 && TREE_CODE (t) != FIELD_DECL)
4555 error ("taking address of bound pointer-to-member expression");
4556 return error_mark_node;
4558 if (!PTRMEM_OK_P (arg))
4560 /* This cannot form a pointer to method, so we must
4561 resolve the offset ref, and take the address of the
4562 result. For instance,
4564 arg = resolve_offset_ref (arg);
4566 return build_unary_op (code, arg, 0);
4569 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4571 error ("cannot create pointer to reference member `%D'", t);
4572 return error_mark_node;
4575 type = build_ptrmem_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4576 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4582 /* We permit compiler to make function calls returning
4583 objects of aggregate type look like lvalues. */
4587 if (TREE_CODE (targ) == SAVE_EXPR)
4588 targ = TREE_OPERAND (targ, 0);
4590 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4592 if (TREE_CODE (arg) == SAVE_EXPR)
4595 targ = build_cplus_new (TREE_TYPE (arg), arg);
4596 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4599 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4600 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4601 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4604 /* Don't let anything else be handled specially. */
4608 /* Mark EXP saying that we need to be able to take the
4609 address of it; it should not be allocated in a register.
4610 Value is true if successful.
4612 C++: we do not allow `current_class_ptr' to be addressable. */
4615 cxx_mark_addressable (tree exp)
4617 register tree x = exp;
4620 switch (TREE_CODE (x))
4627 x = TREE_OPERAND (x, 0);
4631 if (x == current_class_ptr)
4633 error ("cannot take the address of `this', which is an rvalue expression");
4634 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4640 /* Caller should not be trying to mark initialized
4641 constant fields addressable. */
4642 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4643 || DECL_IN_AGGR_P (x) == 0
4645 || DECL_EXTERNAL (x), 314);
4650 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4651 && !DECL_ARTIFICIAL (x) && extra_warnings)
4652 warning ("address requested for `%D', which is declared `register'",
4654 TREE_ADDRESSABLE (x) = 1;
4655 put_var_into_stack (x, /*rescan=*/true);
4659 TREE_ADDRESSABLE (x) = 1;
4660 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4664 TREE_ADDRESSABLE (x) = 1;
4668 TREE_ADDRESSABLE (x) = 1;
4669 cxx_mark_addressable (TREE_OPERAND (x, 0));
4677 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4680 build_x_conditional_expr (tree ifexp, tree op1, tree op2)
4682 if (processing_template_decl)
4683 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4685 return build_conditional_expr (ifexp, op1, op2);
4688 /* Handle overloading of the ',' operator when needed. Otherwise,
4689 this function just builds an expression list. */
4692 build_x_compound_expr (tree list)
4694 tree rest = TREE_CHAIN (list);
4697 if (processing_template_decl)
4698 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
4700 if (rest == NULL_TREE)
4701 return build_compound_expr (list);
4703 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL,
4704 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4706 return build_x_compound_expr (tree_cons (NULL_TREE, result,
4707 TREE_CHAIN (rest)));
4709 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
4711 /* FIXME: This test should be in the implicit cast to void of the LHS. */
4712 /* the left-hand operand of a comma expression is like an expression
4713 statement: we should warn if it doesn't have any side-effects,
4714 unless it was explicitly cast to (void). */
4715 if (warn_unused_value
4716 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
4717 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
4718 warning("left-hand operand of comma expression has no effect");
4720 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4721 else if (warn_unused_value)
4722 warn_if_unused_value (TREE_VALUE(list));
4725 return build_compound_expr
4726 (tree_cons (NULL_TREE, TREE_VALUE (list),
4727 build_tree_list (NULL_TREE,
4728 build_x_compound_expr (rest))));
4731 /* Given a list of expressions, return a compound expression
4732 that performs them all and returns the value of the last of them. */
4735 build_compound_expr (tree list)
4740 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
4742 if (TREE_CHAIN (list) == 0)
4744 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4745 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4746 if (TREE_CODE (list) == NOP_EXPR
4747 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
4748 list = TREE_OPERAND (list, 0);
4750 return TREE_VALUE (list);
4753 first = TREE_VALUE (list);
4754 first = convert_to_void (first, "left-hand operand of comma");
4755 if (first == error_mark_node)
4756 return error_mark_node;
4758 rest = build_compound_expr (TREE_CHAIN (list));
4759 if (rest == error_mark_node)
4760 return error_mark_node;
4762 /* When pedantic, a compound expression cannot be a constant expression. */
4763 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
4766 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
4769 /* Issue an error message if casting from SRC_TYPE to DEST_TYPE casts
4773 check_for_casting_away_constness (tree src_type, tree dest_type)
4775 if (casts_away_constness (src_type, dest_type))
4776 error ("static_cast from type `%T' to type `%T' casts away constness",
4777 src_type, dest_type);
4780 /* Return an expression representing static_cast<TYPE>(EXPR). */
4783 build_static_cast (tree type, tree expr)
4788 if (type == error_mark_node || expr == error_mark_node)
4789 return error_mark_node;
4791 if (TREE_CODE (expr) == OFFSET_REF)
4792 expr = resolve_offset_ref (expr);
4794 if (processing_template_decl)
4796 tree t = build_min (STATIC_CAST_EXPR, type, expr);
4800 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4801 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4802 if (TREE_CODE (type) != REFERENCE_TYPE
4803 && TREE_CODE (expr) == NOP_EXPR
4804 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4805 expr = TREE_OPERAND (expr, 0);
4807 intype = TREE_TYPE (expr);
4809 /* [expr.static.cast]
4811 An expression e can be explicitly converted to a type T using a
4812 static_cast of the form static_cast<T>(e) if the declaration T
4813 t(e);" is well-formed, for some invented temporary variable
4815 result = perform_direct_initialization_if_possible (type, expr);
4819 /* [expr.static.cast]
4821 Any expression can be explicitly converted to type cv void. */
4822 if (TREE_CODE (type) == VOID_TYPE)
4823 return convert_to_void (expr, /*implicit=*/NULL);
4825 /* [expr.static.cast]
4827 An lvalue of type "cv1 B", where B is a class type, can be cast
4828 to type "reference to cv2 D", where D is a class derived (clause
4829 _class.derived_) from B, if a valid standard conversion from
4830 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
4831 same cv-qualification as, or greater cv-qualification than, cv1,
4832 and B is not a virtual base class of D. */
4833 if (TREE_CODE (type) == REFERENCE_TYPE
4834 && CLASS_TYPE_P (TREE_TYPE (type))
4835 && CLASS_TYPE_P (intype)
4836 && real_non_cast_lvalue_p (expr)
4837 && DERIVED_FROM_P (intype, TREE_TYPE (type))
4838 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
4839 build_pointer_type (TYPE_MAIN_VARIANT
4840 (TREE_TYPE (type))))
4841 && at_least_as_qualified_p (TREE_TYPE (type), intype))
4843 /* At this point we have checked all of the conditions except
4844 that B is not a virtual base class of D. That will be
4845 checked by build_base_path. */
4846 tree base = lookup_base (TREE_TYPE (type), intype, ba_any, NULL);
4848 /* Convert from B* to D*. */
4849 expr = build_base_path (MINUS_EXPR, build_address (expr),
4850 base, /*nonnull=*/false);
4851 /* Convert the pointer to a reference. */
4852 return build_nop (type, expr);
4855 /* [expr.static.cast]
4857 The inverse of any standard conversion sequence (clause _conv_),
4858 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
4859 (_conv.array_), function-to-pointer (_conv.func_), and boolean
4860 (_conv.bool_) conversions, can be performed explicitly using
4861 static_cast subject to the restriction that the explicit
4862 conversion does not cast away constness (_expr.const.cast_), and
4863 the following additional rules for specific cases: */
4864 /* For reference, the conversions not excluded are: integral
4865 promotions, floating point promotion, integral conversions,
4866 floating point conversions, floating-integral conversions,
4867 pointer conversions, and pointer to member conversions. */
4868 if ((ARITHMETIC_TYPE_P (type) && ARITHMETIC_TYPE_P (intype))
4871 A value of integral _or enumeration_ type can be explicitly
4872 converted to an enumeration type. */
4873 || (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4874 && INTEGRAL_OR_ENUMERATION_TYPE_P (intype)))
4875 /* Really, build_c_cast should defer to this function rather
4876 than the other way around. */
4877 return build_c_cast (type, expr);
4878 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
4879 && CLASS_TYPE_P (TREE_TYPE (type))
4880 && CLASS_TYPE_P (TREE_TYPE (intype))
4881 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
4882 (TREE_TYPE (intype))),
4883 build_pointer_type (TYPE_MAIN_VARIANT
4884 (TREE_TYPE (type)))))
4888 check_for_casting_away_constness (intype, type);
4889 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
4890 ba_check | ba_quiet, NULL);
4891 return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
4893 if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4894 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
4901 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
4902 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
4904 if (TYPE_PTRMEM_P (type))
4906 t1 = (build_ptrmem_type
4908 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
4909 t2 = (build_ptrmem_type
4911 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
4918 if (can_convert (t1, t2))
4920 check_for_casting_away_constness (intype, type);
4921 if (TYPE_PTRMEM_P (type))
4923 if (TREE_CODE (expr) == PTRMEM_CST)
4924 expr = cplus_expand_constant (expr);
4925 expr = cp_build_binary_op (PLUS_EXPR,
4926 cp_convert (ptrdiff_type_node, expr),
4927 get_delta_difference (c1, c2,
4929 return build_nop (type, expr);
4932 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
4937 /* [expr.static.cast]
4939 An rvalue of type "pointer to cv void" can be explicitly
4940 converted to a pointer to object type. A value of type pointer
4941 to object converted to "pointer to cv void" and back to the
4942 original pointer type will have its original value. */
4943 if (TREE_CODE (intype) == POINTER_TYPE
4944 && VOID_TYPE_P (TREE_TYPE (intype))
4945 && TYPE_PTROB_P (type))
4947 check_for_casting_away_constness (intype, type);
4948 return build_nop (type, expr);
4951 error ("invalid static_cast from type `%T' to type `%T'", intype, type);
4952 return error_mark_node;
4956 build_reinterpret_cast (tree type, tree expr)
4960 if (type == error_mark_node || expr == error_mark_node)
4961 return error_mark_node;
4963 if (TREE_CODE (expr) == OFFSET_REF)
4964 expr = resolve_offset_ref (expr);
4966 if (processing_template_decl)
4968 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
4972 if (TREE_CODE (type) != REFERENCE_TYPE)
4974 expr = decay_conversion (expr);
4976 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4977 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4978 if (TREE_CODE (expr) == NOP_EXPR
4979 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4980 expr = TREE_OPERAND (expr, 0);
4983 intype = TREE_TYPE (expr);
4985 if (TREE_CODE (type) == REFERENCE_TYPE)
4987 if (! real_lvalue_p (expr))
4989 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
4990 return error_mark_node;
4992 expr = build_unary_op (ADDR_EXPR, expr, 0);
4993 if (expr != error_mark_node)
4994 expr = build_reinterpret_cast
4995 (build_pointer_type (TREE_TYPE (type)), expr);
4996 if (expr != error_mark_node)
4997 expr = build_indirect_ref (expr, 0);
5000 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5001 return build_static_cast (type, expr);
5003 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5004 || TREE_CODE (intype) == ENUMERAL_TYPE))
5006 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5008 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5009 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5012 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5013 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5015 expr = decl_constant_value (expr);
5016 return fold (build1 (NOP_EXPR, type, expr));
5018 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5019 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5021 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5022 pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5025 expr = decl_constant_value (expr);
5026 return fold (build1 (NOP_EXPR, type, expr));
5028 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5029 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5031 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5032 expr = decl_constant_value (expr);
5033 return fold (build1 (NOP_EXPR, type, expr));
5037 error ("invalid reinterpret_cast from type `%T' to type `%T'",
5039 return error_mark_node;
5042 return cp_convert (type, expr);
5046 build_const_cast (tree type, tree expr)
5050 if (type == error_mark_node || expr == error_mark_node)
5051 return error_mark_node;
5053 if (TREE_CODE (expr) == OFFSET_REF)
5054 expr = resolve_offset_ref (expr);
5056 if (processing_template_decl)
5058 tree t = build_min (CONST_CAST_EXPR, type, expr);
5062 if (!POINTER_TYPE_P (type))
5063 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5064 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5066 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5067 return error_mark_node;
5070 if (TREE_CODE (type) != REFERENCE_TYPE)
5072 expr = decay_conversion (expr);
5074 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5075 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5076 if (TREE_CODE (expr) == NOP_EXPR
5077 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5078 expr = TREE_OPERAND (expr, 0);
5081 intype = TREE_TYPE (expr);
5083 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5084 return build_static_cast (type, expr);
5085 else if (TREE_CODE (type) == REFERENCE_TYPE)
5087 if (! real_lvalue_p (expr))
5089 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5090 return error_mark_node;
5093 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5095 expr = build_unary_op (ADDR_EXPR, expr, 0);
5096 expr = build1 (NOP_EXPR, type, expr);
5097 return convert_from_reference (expr);
5100 else if (TREE_CODE (type) == POINTER_TYPE
5101 && TREE_CODE (intype) == POINTER_TYPE
5102 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5103 return cp_convert (type, expr);
5105 error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5106 return error_mark_node;
5109 /* Build an expression representing a cast to type TYPE of expression EXPR.
5111 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5112 when doing the cast. */
5115 build_c_cast (tree type, tree expr)
5117 register tree value = expr;
5120 if (type == error_mark_node || expr == error_mark_node)
5121 return error_mark_node;
5123 if (processing_template_decl)
5125 tree t = build_min (CAST_EXPR, type,
5126 tree_cons (NULL_TREE, value, NULL_TREE));
5130 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5131 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5132 if (TREE_CODE (type) != REFERENCE_TYPE
5133 && TREE_CODE (value) == NOP_EXPR
5134 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5135 value = TREE_OPERAND (value, 0);
5137 if (TREE_CODE (value) == OFFSET_REF)
5138 value = resolve_offset_ref (value);
5140 if (TREE_CODE (type) == ARRAY_TYPE)
5142 /* Allow casting from T1* to T2[] because Cfront allows it.
5143 NIHCL uses it. It is not valid ISO C++ however. */
5144 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5146 pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5147 type = build_pointer_type (TREE_TYPE (type));
5151 error ("ISO C++ forbids casting to an array type `%T'", type);
5152 return error_mark_node;
5156 if (TREE_CODE (type) == FUNCTION_TYPE
5157 || TREE_CODE (type) == METHOD_TYPE)
5159 error ("invalid cast to function type `%T'", type);
5160 return error_mark_node;
5163 if (TREE_CODE (type) == VOID_TYPE)
5165 /* Conversion to void does not cause any of the normal function to
5166 * pointer, array to pointer and lvalue to rvalue decays. */
5168 value = convert_to_void (value, /*implicit=*/NULL);
5172 if (!complete_type_or_else (type, NULL_TREE))
5173 return error_mark_node;
5175 /* Convert functions and arrays to pointers and
5176 convert references to their expanded types,
5177 but don't convert any other types. If, however, we are
5178 casting to a class type, there's no reason to do this: the
5179 cast will only succeed if there is a converting constructor,
5180 and the default conversions will be done at that point. In
5181 fact, doing the default conversion here is actually harmful
5185 struct S { S(const A&); };
5187 since we don't want the array-to-pointer conversion done. */
5188 if (!IS_AGGR_TYPE (type))
5190 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5191 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5192 /* Don't do the default conversion on a ->* expression. */
5193 && ! (TREE_CODE (type) == POINTER_TYPE
5194 && bound_pmf_p (value)))
5195 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5196 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5197 value = default_conversion (value);
5199 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5200 /* However, even for class types, we still need to strip away
5201 the reference type, since the call to convert_force below
5202 does not expect the input expression to be of reference
5204 value = convert_from_reference (value);
5206 otype = TREE_TYPE (value);
5208 /* Optionally warn about potentially worrisome casts. */
5211 && TREE_CODE (type) == POINTER_TYPE
5212 && TREE_CODE (otype) == POINTER_TYPE
5213 && !at_least_as_qualified_p (TREE_TYPE (type),
5215 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5218 if (TREE_CODE (type) == INTEGER_TYPE
5219 && TREE_CODE (otype) == POINTER_TYPE
5220 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5221 warning ("cast from pointer to integer of different size");
5223 if (TREE_CODE (type) == POINTER_TYPE
5224 && TREE_CODE (otype) == INTEGER_TYPE
5225 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5226 /* Don't warn about converting any constant. */
5227 && !TREE_CONSTANT (value))
5228 warning ("cast to pointer from integer of different size");
5230 if (TREE_CODE (type) == REFERENCE_TYPE)
5231 value = (convert_from_reference
5232 (convert_to_reference (type, value, CONV_C_CAST,
5233 LOOKUP_COMPLAIN, NULL_TREE)));
5238 value = decl_constant_value (value);
5241 value = convert_force (type, value, CONV_C_CAST);
5243 /* Ignore any integer overflow caused by the cast. */
5244 if (TREE_CODE (value) == INTEGER_CST)
5246 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5247 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5251 /* Warn about possible alignment problems. Do this here when we will have
5252 instantiated any necessary template types. */
5253 if (STRICT_ALIGNMENT && warn_cast_align
5254 && TREE_CODE (type) == POINTER_TYPE
5255 && TREE_CODE (otype) == POINTER_TYPE
5256 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5257 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5258 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5259 && COMPLETE_TYPE_P (TREE_TYPE (type))
5260 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5261 warning ("cast from `%T' to `%T' increases required alignment of target type",
5264 /* Always produce some operator for an explicit cast,
5265 so we can tell (for -pedantic) that the cast is no lvalue. */
5266 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5267 && real_lvalue_p (value))
5268 value = non_lvalue (value);
5273 /* Build an assignment expression of lvalue LHS from value RHS.
5274 MODIFYCODE is the code for a binary operator that we use
5275 to combine the old value of LHS with RHS to get the new value.
5276 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5278 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5281 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5283 register tree result;
5285 tree lhstype = TREE_TYPE (lhs);
5286 tree olhstype = lhstype;
5289 /* Avoid duplicate error messages from operands that had errors. */
5290 if (lhs == error_mark_node || rhs == error_mark_node)
5291 return error_mark_node;
5293 /* Handle control structure constructs used as "lvalues". */
5294 switch (TREE_CODE (lhs))
5296 /* Handle --foo = 5; as these are valid constructs in C++ */
5297 case PREDECREMENT_EXPR:
5298 case PREINCREMENT_EXPR:
5299 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5300 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5301 stabilize_reference (TREE_OPERAND (lhs, 0)),
5302 TREE_OPERAND (lhs, 1));
5303 return build (COMPOUND_EXPR, lhstype,
5305 build_modify_expr (TREE_OPERAND (lhs, 0),
5308 /* Handle (a, b) used as an "lvalue". */
5310 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5312 if (newrhs == error_mark_node)
5313 return error_mark_node;
5314 return build (COMPOUND_EXPR, lhstype,
5315 TREE_OPERAND (lhs, 0), newrhs);
5318 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5319 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5320 stabilize_reference (TREE_OPERAND (lhs, 0)),
5321 TREE_OPERAND (lhs, 1));
5322 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5323 if (newrhs == error_mark_node)
5324 return error_mark_node;
5325 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5327 /* Handle (a ? b : c) used as an "lvalue". */
5330 /* Produce (a ? (b = rhs) : (c = rhs))
5331 except that the RHS goes through a save-expr
5332 so the code to compute it is only emitted once. */
5334 tree preeval = NULL_TREE;
5336 rhs = stabilize_expr (rhs, &preeval);
5338 /* Check this here to avoid odd errors when trying to convert
5339 a throw to the type of the COND_EXPR. */
5340 if (!lvalue_or_else (lhs, "assignment"))
5341 return error_mark_node;
5343 cond = build_conditional_expr
5344 (TREE_OPERAND (lhs, 0),
5345 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5346 TREE_OPERAND (lhs, 1)),
5348 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5349 TREE_OPERAND (lhs, 2)),
5352 if (cond == error_mark_node)
5354 /* Make sure the code to compute the rhs comes out
5355 before the split. */
5356 return build (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
5360 lhs = resolve_offset_ref (lhs);
5361 if (lhs == error_mark_node)
5362 return error_mark_node;
5363 olhstype = lhstype = TREE_TYPE (lhs);
5369 if (modifycode == INIT_EXPR)
5371 if (TREE_CODE (rhs) == CONSTRUCTOR)
5373 my_friendly_assert (same_type_p (TREE_TYPE (rhs), lhstype),
5375 result = build (INIT_EXPR, lhstype, lhs, rhs);
5376 TREE_SIDE_EFFECTS (result) = 1;
5379 else if (! IS_AGGR_TYPE (lhstype))
5380 /* Do the default thing */;
5383 result = build_special_member_call (lhs, complete_ctor_identifier,
5384 build_tree_list (NULL_TREE, rhs),
5385 TYPE_BINFO (lhstype),
5387 if (result == NULL_TREE)
5388 return error_mark_node;
5394 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5396 lhs = convert_from_reference (lhs);
5397 olhstype = lhstype = TREE_TYPE (lhs);
5399 lhs = require_complete_type (lhs);
5400 if (lhs == error_mark_node)
5401 return error_mark_node;
5403 if (modifycode == NOP_EXPR)
5405 /* `operator=' is not an inheritable operator. */
5406 if (! IS_AGGR_TYPE (lhstype))
5407 /* Do the default thing */;
5410 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
5411 lhs, rhs, make_node (NOP_EXPR));
5412 if (result == NULL_TREE)
5413 return error_mark_node;
5420 /* A binary op has been requested. Combine the old LHS
5421 value with the RHS producing the value we should actually
5422 store into the LHS. */
5424 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE),
5426 lhs = stabilize_reference (lhs);
5427 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5428 if (newrhs == error_mark_node)
5430 error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5431 TREE_TYPE (lhs), TREE_TYPE (rhs));
5432 return error_mark_node;
5435 /* Now it looks like a plain assignment. */
5436 modifycode = NOP_EXPR;
5438 my_friendly_assert (TREE_CODE (lhstype) != REFERENCE_TYPE, 20011220);
5439 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE,
5443 /* Handle a cast used as an "lvalue".
5444 We have already performed any binary operator using the value as cast.
5445 Now convert the result to the cast type of the lhs,
5446 and then true type of the lhs and store it there;
5447 then convert result back to the cast type to be the value
5448 of the assignment. */
5450 switch (TREE_CODE (lhs))
5455 case FIX_TRUNC_EXPR:
5456 case FIX_FLOOR_EXPR:
5457 case FIX_ROUND_EXPR:
5460 tree inner_lhs = TREE_OPERAND (lhs, 0);
5463 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5464 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5465 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5466 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5467 newrhs = default_conversion (newrhs);
5469 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5470 type, otherwise the result is an rvalue. */
5471 if (! lvalue_p (lhs))
5472 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5474 result = build_modify_expr (inner_lhs, NOP_EXPR,
5475 cp_convert (TREE_TYPE (inner_lhs),
5476 cp_convert (lhstype, newrhs)));
5477 if (result == error_mark_node)
5479 return cp_convert (TREE_TYPE (lhs), result);
5486 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5487 Reject anything strange now. */
5489 if (!lvalue_or_else (lhs, "assignment"))
5490 return error_mark_node;
5492 /* Warn about modifying something that is `const'. Don't warn if
5493 this is initialization. */
5494 if (modifycode != INIT_EXPR
5495 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5496 /* Functions are not modifiable, even though they are
5498 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5499 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5500 /* If it's an aggregate and any field is const, then it is
5501 effectively const. */
5502 || (CLASS_TYPE_P (lhstype)
5503 && C_TYPE_FIELDS_READONLY (lhstype))))
5504 readonly_error (lhs, "assignment", 0);
5506 /* If storing into a structure or union member, it has probably been
5507 given type `int'. Compute the type that would go with the actual
5508 amount of storage the member occupies. */
5510 if (TREE_CODE (lhs) == COMPONENT_REF
5511 && (TREE_CODE (lhstype) == INTEGER_TYPE
5512 || TREE_CODE (lhstype) == REAL_TYPE
5513 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5515 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5517 /* If storing in a field that is in actuality a short or narrower
5518 than one, we must store in the field in its actual type. */
5520 if (lhstype != TREE_TYPE (lhs))
5522 lhs = copy_node (lhs);
5523 TREE_TYPE (lhs) = lhstype;
5527 /* Convert new value to destination type. */
5529 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5533 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5534 TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
5536 error ("incompatible types in assignment of `%T' to `%T'",
5537 TREE_TYPE (rhs), lhstype);
5538 return error_mark_node;
5541 /* Allow array assignment in compiler-generated code. */
5542 if (! DECL_ARTIFICIAL (current_function_decl))
5543 pedwarn ("ISO C++ forbids assignment of arrays");
5545 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5546 ? 1 + (modifycode != INIT_EXPR): 0;
5547 return build_vec_init (lhs, NULL_TREE, newrhs, from_array);
5550 if (modifycode == INIT_EXPR)
5551 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5552 "initialization", NULL_TREE, 0);
5555 /* Avoid warnings on enum bit fields. */
5556 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5557 && TREE_CODE (lhstype) == INTEGER_TYPE)
5559 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5561 newrhs = convert_force (lhstype, newrhs, 0);
5564 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5566 if (TREE_CODE (newrhs) == CALL_EXPR
5567 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5568 newrhs = build_cplus_new (lhstype, newrhs);
5570 /* Can't initialize directly from a TARGET_EXPR, since that would
5571 cause the lhs to be constructed twice, and possibly result in
5572 accidental self-initialization. So we force the TARGET_EXPR to be
5573 expanded without a target. */
5574 if (TREE_CODE (newrhs) == TARGET_EXPR)
5575 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5576 TREE_OPERAND (newrhs, 0));
5579 if (newrhs == error_mark_node)
5580 return error_mark_node;
5582 if (TREE_CODE (newrhs) == COND_EXPR)
5585 tree cond = TREE_OPERAND (newrhs, 0);
5587 if (TREE_SIDE_EFFECTS (lhs))
5588 cond = build_compound_expr (tree_cons
5590 build_tree_list (NULL_TREE, cond)));
5592 /* Cannot have two identical lhs on this one tree (result) as preexpand
5593 calls will rip them out and fill in RTL for them, but when the
5594 rtl is generated, the calls will only be in the first side of the
5595 condition, not on both, or before the conditional jump! (mrs) */
5596 lhs1 = break_out_calls (lhs);
5599 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5600 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5601 lhstype, lhs, newrhs);
5604 tree result_type = TREE_TYPE (newrhs);
5605 /* We have to convert each arm to the proper type because the
5606 types may have been munged by constant folding. */
5608 = build (COND_EXPR, result_type, cond,
5609 build_modify_expr (lhs, modifycode,
5610 cp_convert (result_type,
5611 TREE_OPERAND (newrhs, 1))),
5612 build_modify_expr (lhs1, modifycode,
5613 cp_convert (result_type,
5614 TREE_OPERAND (newrhs, 2))));
5618 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5619 lhstype, lhs, newrhs);
5621 TREE_SIDE_EFFECTS (result) = 1;
5623 /* If we got the LHS in a different type for storing in,
5624 convert the result back to the nominal type of LHS
5625 so that the value we return always has the same type
5626 as the LHS argument. */
5628 if (olhstype == TREE_TYPE (result))
5630 /* Avoid warnings converting integral types back into enums
5631 for enum bit fields. */
5632 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5633 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5635 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5636 TREE_NO_UNUSED_WARNING (result) = 1;
5639 return convert_for_assignment (olhstype, result, "assignment",
5644 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5646 if (processing_template_decl)
5647 return build_min_nt (MODOP_EXPR, lhs,
5648 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5650 if (modifycode != NOP_EXPR)
5652 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5653 make_node (modifycode));
5657 return build_modify_expr (lhs, modifycode, rhs);
5661 /* Get difference in deltas for different pointer to member function
5662 types. Return integer_zero_node, if FROM cannot be converted to a
5663 TO type. If FORCE is true, then allow reverse conversions as well.
5665 Note that the naming of FROM and TO is kind of backwards; the return
5666 value is what we add to a TO in order to get a FROM. They are named
5667 this way because we call this function to find out how to convert from
5668 a pointer to member of FROM to a pointer to member of TO. */
5671 get_delta_difference (tree from, tree to, int force)
5673 tree delta = integer_zero_node;
5678 binfo = lookup_base (to, from, ba_check, &kind);
5679 if (kind == bk_inaccessible || kind == bk_ambig)
5681 error (" in pointer to member function conversion");
5688 error_not_base_type (from, to);
5689 error (" in pointer to member conversion");
5692 binfo = lookup_base (from, to, ba_check, &kind);
5695 virt_binfo = binfo_from_vbase (binfo);
5699 /* This is a reinterpret cast, we choose to do nothing. */
5700 warning ("pointer to member cast via virtual base `%T'",
5701 BINFO_TYPE (virt_binfo));
5704 delta = BINFO_OFFSET (binfo);
5705 delta = cp_convert (ptrdiff_type_node, delta);
5706 delta = cp_build_binary_op (MINUS_EXPR,
5713 virt_binfo = binfo_from_vbase (binfo);
5716 /* This is a reinterpret cast, we choose to do nothing. */
5718 warning ("pointer to member cast via virtual base `%T'",
5719 BINFO_TYPE (virt_binfo));
5721 error ("pointer to member conversion via virtual base `%T'",
5722 BINFO_TYPE (virt_binfo));
5725 delta = BINFO_OFFSET (binfo);
5727 return cp_convert (ptrdiff_type_node, delta);
5730 /* Return a constructor for the pointer-to-member-function TYPE using
5731 the other components as specified. */
5734 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
5740 /* Pull the FIELD_DECLs out of the type. */
5741 pfn_field = TYPE_FIELDS (type);
5742 delta_field = TREE_CHAIN (pfn_field);
5744 /* Make sure DELTA has the type we want. */
5745 delta = convert_and_check (delta_type_node, delta);
5747 /* Finish creating the initializer. */
5748 u = tree_cons (pfn_field, pfn,
5749 build_tree_list (delta_field, delta));
5750 u = build_constructor (type, u);
5751 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
5752 TREE_STATIC (u) = (TREE_CONSTANT (u)
5753 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
5755 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
5760 /* Build a constructor for a pointer to member function. It can be
5761 used to initialize global variables, local variable, or used
5762 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5765 If FORCE is nonzero, then force this conversion, even if
5766 we would rather not do it. Usually set when using an explicit
5769 Return error_mark_node, if something goes wrong. */
5772 build_ptrmemfunc (tree type, tree pfn, int force)
5778 if (error_operand_p (pfn))
5779 return error_mark_node;
5781 pfn_type = TREE_TYPE (pfn);
5782 to_type = build_ptrmemfunc_type (type);
5784 /* Handle multiple conversions of pointer to member functions. */
5785 if (TYPE_PTRMEMFUNC_P (pfn_type))
5787 tree delta = NULL_TREE;
5788 tree npfn = NULL_TREE;
5792 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
5793 error ("invalid conversion to type `%T' from type `%T'",
5796 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
5797 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
5800 /* We don't have to do any conversion to convert a
5801 pointer-to-member to its own type. But, we don't want to
5802 just return a PTRMEM_CST if there's an explicit cast; that
5803 cast should make the expression an invalid template argument. */
5804 if (TREE_CODE (pfn) != PTRMEM_CST)
5806 if (same_type_p (to_type, pfn_type))
5808 else if (integer_zerop (n))
5809 return build_reinterpret_cast (to_type, pfn);
5812 if (TREE_SIDE_EFFECTS (pfn))
5813 pfn = save_expr (pfn);
5815 /* Obtain the function pointer and the current DELTA. */
5816 if (TREE_CODE (pfn) == PTRMEM_CST)
5817 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
5820 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
5821 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
5824 /* Just adjust the DELTA field. */
5825 delta = cp_convert (ptrdiff_type_node, delta);
5826 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
5827 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
5828 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
5829 return build_ptrmemfunc1 (to_type, delta, npfn);
5832 /* Handle null pointer to member function conversions. */
5833 if (integer_zerop (pfn))
5835 pfn = build_c_cast (type, integer_zero_node);
5836 return build_ptrmemfunc1 (to_type,
5841 if (type_unknown_p (pfn))
5842 return instantiate_type (type, pfn, tf_error | tf_warning);
5844 fn = TREE_OPERAND (pfn, 0);
5845 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5846 return make_ptrmem_cst (to_type, fn);
5849 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5852 ??? There is no consistency as to the types returned for the above
5853 values. Some code acts as if its a sizetype and some as if its
5854 integer_type_node. */
5857 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
5859 tree type = TREE_TYPE (cst);
5860 tree fn = PTRMEM_CST_MEMBER (cst);
5861 tree ptr_class, fn_class;
5863 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5865 /* The class that the function belongs to. */
5866 fn_class = DECL_CONTEXT (fn);
5868 /* The class that we're creating a pointer to member of. */
5869 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
5871 /* First, calculate the adjustment to the function's class. */
5872 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
5874 if (!DECL_VIRTUAL_P (fn))
5875 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
5878 /* If we're dealing with a virtual function, we have to adjust 'this'
5879 again, to point to the base which provides the vtable entry for
5880 fn; the call will do the opposite adjustment. */
5881 tree orig_class = DECL_CONTEXT (fn);
5882 tree binfo = binfo_or_else (orig_class, fn_class);
5883 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5884 *delta, BINFO_OFFSET (binfo)));
5886 /* We set PFN to the vtable offset at which the function can be
5887 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
5888 case delta is shifted left, and then incremented). */
5889 *pfn = DECL_VINDEX (fn);
5890 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
5891 TYPE_SIZE_UNIT (vtable_entry_type)));
5893 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
5895 case ptrmemfunc_vbit_in_pfn:
5896 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
5900 case ptrmemfunc_vbit_in_delta:
5901 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
5902 *delta, integer_one_node));
5903 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5904 *delta, integer_one_node));
5911 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
5916 /* Return an expression for PFN from the pointer-to-member function
5920 pfn_from_ptrmemfunc (tree t)
5922 if (TREE_CODE (t) == PTRMEM_CST)
5927 expand_ptrmemfunc_cst (t, &delta, &pfn);
5932 return build_ptrmemfunc_access_expr (t, pfn_identifier);
5935 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
5936 if this is a potentially dangerous thing to do. Returns a possibly
5940 dubious_conversion_warnings (tree type, tree expr,
5941 const char *errtype, tree fndecl, int parmnum)
5943 if (TREE_CODE (type) == REFERENCE_TYPE)
5944 type = TREE_TYPE (type);
5946 /* Issue warnings about peculiar, but valid, uses of NULL. */
5947 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
5950 warning ("passing NULL used for non-pointer %s %P of `%D'",
5951 errtype, parmnum, fndecl);
5953 warning ("%s to non-pointer type `%T' from NULL", errtype, type);
5956 /* Warn about assigning a floating-point type to an integer type. */
5957 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
5958 && TREE_CODE (type) == INTEGER_TYPE)
5961 warning ("passing `%T' for %s %P of `%D'",
5962 TREE_TYPE (expr), errtype, parmnum, fndecl);
5964 warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
5966 /* And warn about assigning a negative value to an unsigned
5968 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
5970 if (TREE_CODE (expr) == INTEGER_CST
5971 && TREE_NEGATED_INT (expr))
5974 warning ("passing negative value `%E' for %s %P of `%D'",
5975 expr, errtype, parmnum, fndecl);
5977 warning ("%s of negative value `%E' to `%T'",
5978 errtype, expr, type);
5981 overflow_warning (expr);
5983 if (TREE_CONSTANT (expr))
5989 /* Convert value RHS to type TYPE as preparation for an assignment to
5990 an lvalue of type TYPE. ERRTYPE is a string to use in error
5991 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
5992 are doing the conversion in order to pass the PARMNUMth argument of
5996 convert_for_assignment (tree type, tree rhs,
5997 const char *errtype, tree fndecl, int parmnum)
5999 register enum tree_code codel = TREE_CODE (type);
6000 register tree rhstype;
6001 register enum tree_code coder;
6003 if (codel == OFFSET_TYPE)
6006 if (TREE_CODE (rhs) == OFFSET_REF)
6007 rhs = resolve_offset_ref (rhs);
6009 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6010 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6011 rhs = TREE_OPERAND (rhs, 0);
6013 rhstype = TREE_TYPE (rhs);
6014 coder = TREE_CODE (rhstype);
6016 if (rhs == error_mark_node || rhstype == error_mark_node)
6017 return error_mark_node;
6018 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6019 return error_mark_node;
6021 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6023 /* The RHS of an assignment cannot have void type. */
6024 if (coder == VOID_TYPE)
6026 error ("void value not ignored as it ought to be");
6027 return error_mark_node;
6030 /* Simplify the RHS if possible. */
6031 if (TREE_CODE (rhs) == CONST_DECL)
6032 rhs = DECL_INITIAL (rhs);
6034 /* We do not use decl_constant_value here because of this case:
6036 const char* const s = "s";
6038 The conversion rules for a string literal are more lax than for a
6039 variable; in particular, a string literal can be converted to a
6040 "char *" but the variable "s" cannot be converted in the same
6041 way. If the conversion is allowed, the optimization should be
6042 performed while creating the converted expression. */
6046 The expression is implicitly converted (clause _conv_) to the
6047 cv-unqualified type of the left operand.
6049 We allow bad conversions here because by the time we get to this point
6050 we are committed to doing the conversion. If we end up doing a bad
6051 conversion, convert_like will complain. */
6052 if (!can_convert_arg_bad (type, rhstype, rhs))
6054 /* When -Wno-pmf-conversions is use, we just silently allow
6055 conversions from pointers-to-members to plain pointers. If
6056 the conversion doesn't work, cp_convert will complain. */
6058 && TYPE_PTR_P (type)
6059 && TYPE_PTRMEMFUNC_P (rhstype))
6060 rhs = cp_convert (strip_top_quals (type), rhs);
6063 /* If the right-hand side has unknown type, then it is an
6064 overloaded function. Call instantiate_type to get error
6066 if (rhstype == unknown_type_node)
6067 instantiate_type (type, rhs, tf_error | tf_warning);
6069 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6070 rhstype, type, parmnum, fndecl);
6072 error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6074 return error_mark_node;
6077 return perform_implicit_conversion (strip_top_quals (type), rhs);
6080 /* Convert RHS to be of type TYPE.
6081 If EXP is nonzero, it is the target of the initialization.
6082 ERRTYPE is a string to use in error messages.
6084 Two major differences between the behavior of
6085 `convert_for_assignment' and `convert_for_initialization'
6086 are that references are bashed in the former, while
6087 copied in the latter, and aggregates are assigned in
6088 the former (operator=) while initialized in the
6091 If using constructor make sure no conversion operator exists, if one does
6092 exist, an ambiguity exists.
6094 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6097 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
6098 const char *errtype, tree fndecl, int parmnum)
6100 register enum tree_code codel = TREE_CODE (type);
6101 register tree rhstype;
6102 register enum tree_code coder;
6104 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6105 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6106 if (TREE_CODE (rhs) == NOP_EXPR
6107 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6108 && codel != REFERENCE_TYPE)
6109 rhs = TREE_OPERAND (rhs, 0);
6111 if (rhs == error_mark_node
6112 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6113 return error_mark_node;
6115 if (TREE_CODE (rhs) == OFFSET_REF)
6117 rhs = resolve_offset_ref (rhs);
6118 if (rhs == error_mark_node)
6119 return error_mark_node;
6122 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6123 rhs = convert_from_reference (rhs);
6125 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6126 && TREE_CODE (type) != ARRAY_TYPE
6127 && (TREE_CODE (type) != REFERENCE_TYPE
6128 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6129 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6130 && (TREE_CODE (type) != REFERENCE_TYPE
6131 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6132 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6133 rhs = default_conversion (rhs);
6135 rhstype = TREE_TYPE (rhs);
6136 coder = TREE_CODE (rhstype);
6138 if (coder == ERROR_MARK)
6139 return error_mark_node;
6141 /* We accept references to incomplete types, so we can
6142 return here before checking if RHS is of complete type. */
6144 if (codel == REFERENCE_TYPE)
6146 /* This should eventually happen in convert_arguments. */
6147 int savew = 0, savee = 0;
6150 savew = warningcount, savee = errorcount;
6151 rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE);
6154 if (warningcount > savew)
6155 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6156 else if (errorcount > savee)
6157 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6163 exp = require_complete_type (exp);
6164 if (exp == error_mark_node)
6165 return error_mark_node;
6167 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6168 rhstype = TREE_TYPE (rhstype);
6170 type = complete_type (type);
6172 if (IS_AGGR_TYPE (type))
6173 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6175 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6178 /* Expand an ASM statement with operands, handling output operands
6179 that are not variables or INDIRECT_REFS by transforming such
6180 cases into cases that expand_asm_operands can handle.
6182 Arguments are same as for expand_asm_operands.
6184 We don't do default conversions on all inputs, because it can screw
6185 up operands that are expected to be in memory. */
6188 c_expand_asm_operands (tree string, tree outputs, tree inputs, tree clobbers,
6189 int vol, const char *filename, int line)
6191 int noutputs = list_length (outputs);
6193 /* o[I] is the place that output number I should be written. */
6194 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6197 /* Record the contents of OUTPUTS before it is modified. */
6198 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6199 o[i] = TREE_VALUE (tail);
6201 /* Generate the ASM_OPERANDS insn;
6202 store into the TREE_VALUEs of OUTPUTS some trees for
6203 where the values were actually stored. */
6204 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6206 /* Copy all the intermediate outputs into the specified outputs. */
6207 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6209 if (o[i] != TREE_VALUE (tail))
6211 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6212 const0_rtx, VOIDmode, EXPAND_NORMAL);
6215 /* Restore the original value so that it's correct the next
6216 time we expand this function. */
6217 TREE_VALUE (tail) = o[i];
6219 /* Detect modification of read-only values.
6220 (Otherwise done by build_modify_expr.) */
6223 tree type = TREE_TYPE (o[i]);
6224 if (type != error_mark_node
6225 && (CP_TYPE_CONST_P (type)
6226 || (CLASS_TYPE_P (type) && C_TYPE_FIELDS_READONLY (type))))
6227 readonly_error (o[i], "modification by `asm'", 1);
6231 /* Those MODIFY_EXPRs could do autoincrements. */
6235 /* If RETVAL is the address of, or a reference to, a local variable or
6236 temporary give an appropraite warning. */
6239 maybe_warn_about_returning_address_of_local (tree retval)
6241 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6242 tree whats_returned = retval;
6246 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6247 whats_returned = TREE_OPERAND (whats_returned, 1);
6248 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6249 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6250 || TREE_CODE (whats_returned) == NOP_EXPR)
6251 whats_returned = TREE_OPERAND (whats_returned, 0);
6256 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6258 whats_returned = TREE_OPERAND (whats_returned, 0);
6260 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6262 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6263 || TREE_CODE (whats_returned) == TARGET_EXPR)
6265 warning ("returning reference to temporary");
6268 if (TREE_CODE (whats_returned) == VAR_DECL
6269 && DECL_NAME (whats_returned)
6270 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6272 warning ("reference to non-lvalue returned");
6277 if (TREE_CODE (whats_returned) == VAR_DECL
6278 && DECL_NAME (whats_returned)
6279 && DECL_FUNCTION_SCOPE_P (whats_returned)
6280 && !(TREE_STATIC (whats_returned)
6281 || TREE_PUBLIC (whats_returned)))
6283 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6284 cp_warning_at ("reference to local variable `%D' returned",
6287 cp_warning_at ("address of local variable `%D' returned",
6293 /* Check that returning RETVAL from the current function is valid.
6294 Return an expression explicitly showing all conversions required to
6295 change RETVAL into the function return type, and to assign it to
6296 the DECL_RESULT for the function. */
6299 check_return_expr (tree retval)
6302 /* The type actually returned by the function, after any
6305 int fn_returns_value_p;
6307 /* A `volatile' function is one that isn't supposed to return, ever.
6308 (This is a G++ extension, used to get better code for functions
6309 that call the `volatile' function.) */
6310 if (TREE_THIS_VOLATILE (current_function_decl))
6311 warning ("function declared `noreturn' has a `return' statement");
6313 /* Check for various simple errors. */
6314 if (DECL_DESTRUCTOR_P (current_function_decl))
6317 error ("returning a value from a destructor");
6320 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6322 if (in_function_try_handler)
6323 /* If a return statement appears in a handler of the
6324 function-try-block of a constructor, the program is ill-formed. */
6325 error ("cannot return from a handler of a function-try-block of a constructor");
6327 /* You can't return a value from a constructor. */
6328 error ("returning a value from a constructor");
6332 if (processing_template_decl)
6334 current_function_returns_value = 1;
6338 /* When no explicit return-value is given in a function with a named
6339 return value, the named return value is used. */
6340 result = DECL_RESULT (current_function_decl);
6341 valtype = TREE_TYPE (result);
6342 my_friendly_assert (valtype != NULL_TREE, 19990924);
6343 fn_returns_value_p = !VOID_TYPE_P (valtype);
6344 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6347 /* Check for a return statement with no return value in a function
6348 that's supposed to return a value. */
6349 if (!retval && fn_returns_value_p)
6351 pedwarn ("return-statement with no value, in function returning `%D'",
6353 /* Clear this, so finish_function won't say that we reach the
6354 end of a non-void function (which we don't, we gave a
6356 current_function_returns_null = 0;
6358 /* Check for a return statement with a value in a function that
6359 isn't supposed to return a value. */
6360 else if (retval && !fn_returns_value_p)
6362 if (VOID_TYPE_P (TREE_TYPE (retval)))
6363 /* You can return a `void' value from a function of `void'
6364 type. In that case, we have to evaluate the expression for
6365 its side-effects. */
6366 finish_expr_stmt (retval);
6368 pedwarn ("return-statement with a value, in function returning `%D'",
6371 current_function_returns_null = 1;
6373 /* There's really no value to return, after all. */
6377 /* Remember that this function can sometimes return without a
6379 current_function_returns_null = 1;
6381 /* Remember that this function did return a value. */
6382 current_function_returns_value = 1;
6384 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6385 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6386 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6387 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6389 && null_ptr_cst_p (retval))
6390 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6392 /* Effective C++ rule 15. See also start_function. */
6394 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6395 && retval != current_class_ref)
6396 warning ("`operator=' should return a reference to `*this'");
6398 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6400 [...] For a function with a class return type, if the expression
6401 in the return statement is the name of a local object, and the cv-
6402 unqualified type of the local object is the same as the function
6403 return type, an implementation is permitted to omit creating the tem-
6404 porary object to hold the function return value [...]
6406 So, if this is a value-returning function that always returns the same
6407 local variable, remember it.
6409 It might be nice to be more flexible, and choose the first suitable
6410 variable even if the function sometimes returns something else, but
6411 then we run the risk of clobbering the variable we chose if the other
6412 returned expression uses the chosen variable somehow. And people expect
6413 this restriction, anyway. (jason 2000-11-19)
6415 See finish_function, genrtl_start_function, and declare_return_variable
6416 for other pieces of this optimization. */
6418 if (fn_returns_value_p && flag_elide_constructors)
6420 if (retval != NULL_TREE
6421 && (current_function_return_value == NULL_TREE
6422 || current_function_return_value == retval)
6423 && TREE_CODE (retval) == VAR_DECL
6424 && DECL_CONTEXT (retval) == current_function_decl
6425 && ! TREE_STATIC (retval)
6426 && (DECL_ALIGN (retval)
6427 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6428 && same_type_p ((TYPE_MAIN_VARIANT
6429 (TREE_TYPE (retval))),
6431 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6432 current_function_return_value = retval;
6434 current_function_return_value = error_mark_node;
6437 /* We don't need to do any conversions when there's nothing being
6439 if (!retval || retval == error_mark_node)
6442 /* Do any required conversions. */
6443 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6444 /* No conversions are required. */
6448 /* The type the function is declared to return. */
6449 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6451 /* First convert the value to the function's return type, then
6452 to the type of return value's location to handle the
6453 case that functype is smaller than the valtype. */
6454 retval = convert_for_initialization
6455 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6456 "return", NULL_TREE, 0);
6457 retval = convert (valtype, retval);
6459 /* If the conversion failed, treat this just like `return;'. */
6460 if (retval == error_mark_node)
6462 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6463 else if (! current_function_returns_struct
6464 && TREE_CODE (retval) == TARGET_EXPR
6465 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6466 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6467 TREE_OPERAND (retval, 0));
6469 maybe_warn_about_returning_address_of_local (retval);
6472 /* Actually copy the value returned into the appropriate location. */
6473 if (retval && retval != result)
6474 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6480 /* Returns nonzero if the pointer-type FROM can be converted to the
6481 pointer-type TO via a qualification conversion. If CONSTP is -1,
6482 then we return nonzero if the pointers are similar, and the
6483 cv-qualification signature of FROM is a proper subset of that of TO.
6485 If CONSTP is positive, then all outer pointers have been
6489 comp_ptr_ttypes_real (tree to, tree from, int constp)
6491 int to_more_cv_qualified = 0;
6493 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6495 if (TREE_CODE (to) != TREE_CODE (from))
6498 if (TREE_CODE (from) == OFFSET_TYPE
6499 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6500 TYPE_OFFSET_BASETYPE (to)))
6503 /* Const and volatile mean something different for function types,
6504 so the usual checks are not appropriate. */
6505 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6507 if (!at_least_as_qualified_p (to, from))
6510 if (!at_least_as_qualified_p (from, to))
6515 ++to_more_cv_qualified;
6519 constp &= TYPE_READONLY (to);
6522 if (TREE_CODE (to) != POINTER_TYPE)
6523 return ((constp >= 0 || to_more_cv_qualified)
6524 && same_type_ignoring_top_level_qualifiers_p (to, from));
6528 /* When comparing, say, char ** to char const **, this function takes
6529 the 'char *' and 'char const *'. Do not pass non-pointer/reference
6530 types to this function. */
6533 comp_ptr_ttypes (tree to, tree from)
6535 return comp_ptr_ttypes_real (to, from, 1);
6538 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6539 type or inheritance-related types, regardless of cv-quals. */
6542 ptr_reasonably_similar (tree to, tree from)
6544 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6546 /* Any target type is similar enough to void. */
6547 if (TREE_CODE (to) == VOID_TYPE
6548 || TREE_CODE (from) == VOID_TYPE)
6551 if (TREE_CODE (to) != TREE_CODE (from))
6554 if (TREE_CODE (from) == OFFSET_TYPE
6555 && comptypes (TYPE_OFFSET_BASETYPE (to),
6556 TYPE_OFFSET_BASETYPE (from),
6557 COMPARE_BASE | COMPARE_DERIVED))
6560 if (TREE_CODE (to) == INTEGER_TYPE
6561 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6564 if (TREE_CODE (to) == FUNCTION_TYPE)
6567 if (TREE_CODE (to) != POINTER_TYPE)
6569 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6570 COMPARE_BASE | COMPARE_DERIVED);
6574 /* Like comp_ptr_ttypes, for const_cast. */
6577 comp_ptr_ttypes_const (tree to, tree from)
6579 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6581 if (TREE_CODE (to) != TREE_CODE (from))
6584 if (TREE_CODE (from) == OFFSET_TYPE
6585 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6586 TYPE_OFFSET_BASETYPE (to)))
6589 if (TREE_CODE (to) != POINTER_TYPE)
6590 return same_type_ignoring_top_level_qualifiers_p (to, from);
6594 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6597 comp_ptr_ttypes_reinterpret (tree to, tree from)
6601 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6603 if (TREE_CODE (from) == OFFSET_TYPE)
6604 from = TREE_TYPE (from);
6605 if (TREE_CODE (to) == OFFSET_TYPE)
6606 to = TREE_TYPE (to);
6608 /* Const and volatile mean something different for function types,
6609 so the usual checks are not appropriate. */
6610 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6611 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6613 if (!at_least_as_qualified_p (to, from))
6617 && !at_least_as_qualified_p (from, to))
6619 constp &= TYPE_READONLY (to);
6622 if (TREE_CODE (from) != POINTER_TYPE
6623 || TREE_CODE (to) != POINTER_TYPE)
6628 /* Returns the type qualifiers for this type, including the qualifiers on the
6629 elements for an array type. */
6632 cp_type_quals (tree type)
6634 type = strip_array_types (type);
6635 if (type == error_mark_node)
6636 return TYPE_UNQUALIFIED;
6637 return TYPE_QUALS (type);
6640 /* Returns nonzero if the TYPE contains a mutable member */
6643 cp_has_mutable_p (tree type)
6645 type = strip_array_types (type);
6647 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6650 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6651 exemplar types such that casting T1 to T2 is casting away castness
6652 if and only if there is no implicit conversion from T1 to T2. */
6655 casts_away_constness_r (tree *t1, tree *t2)
6660 /* [expr.const.cast]
6662 For multi-level pointer to members and multi-level mixed pointers
6663 and pointers to members (conv.qual), the "member" aspect of a
6664 pointer to member level is ignored when determining if a const
6665 cv-qualifier has been cast away. */
6666 if (TYPE_PTRMEM_P (*t1))
6667 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6668 if (TYPE_PTRMEM_P (*t2))
6669 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6671 /* [expr.const.cast]
6673 For two pointer types:
6675 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6676 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6679 casting from X1 to X2 casts away constness if, for a non-pointer
6680 type T there does not exist an implicit conversion (clause
6683 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6687 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6689 if (TREE_CODE (*t1) != POINTER_TYPE
6690 || TREE_CODE (*t2) != POINTER_TYPE)
6692 *t1 = cp_build_qualified_type (void_type_node,
6693 cp_type_quals (*t1));
6694 *t2 = cp_build_qualified_type (void_type_node,
6695 cp_type_quals (*t2));
6699 quals1 = cp_type_quals (*t1);
6700 quals2 = cp_type_quals (*t2);
6701 *t1 = TREE_TYPE (*t1);
6702 *t2 = TREE_TYPE (*t2);
6703 casts_away_constness_r (t1, t2);
6704 *t1 = build_pointer_type (*t1);
6705 *t2 = build_pointer_type (*t2);
6706 *t1 = cp_build_qualified_type (*t1, quals1);
6707 *t2 = cp_build_qualified_type (*t2, quals2);
6710 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
6714 casts_away_constness (tree t1, tree t2)
6716 if (TREE_CODE (t2) == REFERENCE_TYPE)
6718 /* [expr.const.cast]
6720 Casting from an lvalue of type T1 to an lvalue of type T2
6721 using a reference cast casts away constness if a cast from an
6722 rvalue of type "pointer to T1" to the type "pointer to T2"
6723 casts away constness. */
6724 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
6725 return casts_away_constness (build_pointer_type (t1),
6726 build_pointer_type (TREE_TYPE (t2)));
6729 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6730 /* [expr.const.cast]
6732 Casting from an rvalue of type "pointer to data member of X
6733 of type T1" to the type "pointer to data member of Y of type
6734 T2" casts away constness if a cast from an rvalue of type
6735 "pointer to T1" to the type "pointer to T2" casts away
6737 return casts_away_constness
6738 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
6739 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
6741 /* Casting away constness is only something that makes sense for
6742 pointer or reference types. */
6743 if (TREE_CODE (t1) != POINTER_TYPE
6744 || TREE_CODE (t2) != POINTER_TYPE)
6747 /* Top-level qualifiers don't matter. */
6748 t1 = TYPE_MAIN_VARIANT (t1);
6749 t2 = TYPE_MAIN_VARIANT (t2);
6750 casts_away_constness_r (&t1, &t2);
6751 if (!can_convert (t2, t1))
6757 /* Returns TYPE with its cv qualifiers removed
6758 TYPE is T cv* .. *cv where T is not a pointer type,
6759 returns T * .. *. (If T is an array type, then the cv qualifiers
6760 above are those of the array members.) */
6763 strip_all_pointer_quals (tree type)
6765 if (TREE_CODE (type) == POINTER_TYPE)
6766 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
6767 else if (TREE_CODE (type) == OFFSET_TYPE)
6768 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
6769 strip_all_pointer_quals (TREE_TYPE (type)));
6771 return TYPE_MAIN_VARIANT (type);