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 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
888 current instantiation. */
889 if (TREE_CODE (t1) == TYPENAME_TYPE)
891 tree resolved = resolve_typename_type (t1, /*only_current_p=*/true);
893 if (resolved != error_mark_node)
897 if (TREE_CODE (t2) == TYPENAME_TYPE)
899 tree resolved = resolve_typename_type (t2, /*only_current_p=*/true);
901 if (resolved != error_mark_node)
905 /* If either type is the internal version of sizetype, use the
907 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
909 t1 = TYPE_DOMAIN (t1);
911 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
913 t2 = TYPE_DOMAIN (t2);
915 if (TYPE_PTRMEMFUNC_P (t1))
916 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
917 if (TYPE_PTRMEMFUNC_P (t2))
918 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
920 /* Different classes of types can't be compatible. */
921 if (TREE_CODE (t1) != TREE_CODE (t2))
924 /* Qualifiers must match. */
925 if (cp_type_quals (t1) != cp_type_quals (t2))
927 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
930 /* Allow for two different type nodes which have essentially the same
931 definition. Note that we already checked for equality of the type
932 qualifiers (just above). */
934 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
937 if (!(*targetm.comp_type_attributes) (t1, t2))
940 switch (TREE_CODE (t1))
942 case TEMPLATE_TEMPLATE_PARM:
943 case BOUND_TEMPLATE_TEMPLATE_PARM:
944 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
945 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
947 if (!comp_template_parms
948 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
949 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
951 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
953 /* Don't check inheritance. */
954 strict = COMPARE_STRICT;
959 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
960 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
961 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
962 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
965 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
967 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
973 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
974 strict & ~COMPARE_REDECLARATION))
980 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
984 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
986 return compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
989 /* Target types must match incl. qualifiers. */
990 return comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION));
992 case TEMPLATE_TYPE_PARM:
993 return (TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
994 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2));
997 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
998 TYPENAME_TYPE_FULLNAME (t2)))
1000 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1002 case UNBOUND_CLASS_TEMPLATE:
1003 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1005 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1008 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1016 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1017 only in the same direction as the target type. */
1020 comp_cv_target_types (tree ttl, tree ttr, int nptrs)
1024 if (!at_least_as_qualified_p (ttl, ttr)
1025 && !at_least_as_qualified_p (ttr, ttl))
1026 /* The qualifications are incomparable. */
1029 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1030 return more_qualified_p (ttr, ttl) ? -1 : 1;
1032 t = comp_target_types (ttl, ttr, nptrs);
1033 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1034 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1040 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1041 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1042 converted to TTL. Return -1 means that TTL can be converted to TTR but
1045 NPTRS is the number of pointers we can strip off and keep cool.
1046 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1047 but to not permit B** to convert to A**.
1049 This should go away. Callers should use can_convert or something
1050 similar instead. (jason 17 Apr 1997) */
1053 comp_target_types (tree ttl, tree ttr, int nptrs)
1055 ttl = TYPE_MAIN_VARIANT (ttl);
1056 ttr = TYPE_MAIN_VARIANT (ttr);
1057 if (same_type_p (ttl, ttr))
1060 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1063 if ((TREE_CODE (ttr) == POINTER_TYPE
1064 || TREE_CODE (ttr) == REFERENCE_TYPE)
1065 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1066 of the type pointed to. This is necessary for reference init
1067 semantics. We won't get here from a previous call with nptrs == 1;
1068 for multi-level pointers we end up in comp_ptr_ttypes. */
1071 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1073 ttl = TREE_TYPE (ttl);
1074 ttr = TREE_TYPE (ttr);
1078 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1079 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1081 else if (TREE_CODE (ttl) == VOID_TYPE
1082 && TREE_CODE (ttr) != FUNCTION_TYPE
1083 && TREE_CODE (ttr) != METHOD_TYPE
1084 && TREE_CODE (ttr) != OFFSET_TYPE)
1086 else if (TREE_CODE (ttr) == VOID_TYPE
1087 && TREE_CODE (ttl) != FUNCTION_TYPE
1088 && TREE_CODE (ttl) != METHOD_TYPE
1089 && TREE_CODE (ttl) != OFFSET_TYPE)
1091 else if (TREE_CODE (ttl) == POINTER_TYPE
1092 || TREE_CODE (ttl) == ARRAY_TYPE)
1094 if (comp_ptr_ttypes (ttl, ttr))
1096 else if (comp_ptr_ttypes (ttr, ttl))
1102 /* Const and volatile mean something different for function types,
1103 so the usual checks are not appropriate. */
1104 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1105 return comp_target_types (ttl, ttr, nptrs - 1);
1107 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1110 my_friendly_assert (TREE_CODE (ttr) != ARRAY_TYPE, 20030617);
1111 if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1118 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1123 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1132 argsl = TYPE_ARG_TYPES (ttl);
1133 argsr = TYPE_ARG_TYPES (ttr);
1135 /* Compare 'this' here, not in comp_target_parms. */
1136 if (TREE_CODE (ttr) == METHOD_TYPE)
1138 tree tl = TYPE_METHOD_BASETYPE (ttl);
1139 tree tr = TYPE_METHOD_BASETYPE (ttr);
1141 if (!same_or_base_type_p (tr, tl))
1143 if (same_or_base_type_p (tl, tr))
1149 argsl = TREE_CHAIN (argsl);
1150 argsr = TREE_CHAIN (argsr);
1153 switch (comp_target_parms (argsl, argsr))
1161 return saw_contra ? -1 : 1;
1164 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1168 /* Contravariance: we can assign a pointer to base member to a pointer
1169 to derived member. Note difference from simple pointer case, where
1170 we can pass a pointer to derived to a pointer to base. */
1171 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1172 TYPE_OFFSET_BASETYPE (ttl)))
1174 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1175 TYPE_OFFSET_BASETYPE (ttr)))
1185 ttl = TREE_TYPE (ttl);
1186 ttr = TREE_TYPE (ttr);
1188 if (TREE_CODE (ttl) == POINTER_TYPE
1189 || TREE_CODE (ttl) == ARRAY_TYPE)
1191 if (comp_ptr_ttypes (ttl, ttr))
1197 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1202 else if (IS_AGGR_TYPE (ttl))
1206 if (same_or_base_type_p (ttl, ttr))
1208 if (same_or_base_type_p (ttr, ttl))
1216 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1219 at_least_as_qualified_p (tree type1, tree type2)
1221 int q1 = cp_type_quals (type1);
1222 int q2 = cp_type_quals (type2);
1224 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1225 return (q1 & q2) == q2;
1228 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1231 more_qualified_p (tree type1, tree type2)
1233 int q1 = cp_type_quals (type1);
1234 int q2 = cp_type_quals (type2);
1236 return q1 != q2 && (q1 & q2) == q2;
1239 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1240 more cv-qualified that TYPE1, and 0 otherwise. */
1243 comp_cv_qualification (tree type1, tree type2)
1245 int q1 = cp_type_quals (type1);
1246 int q2 = cp_type_quals (type2);
1251 if ((q1 & q2) == q2)
1253 else if ((q1 & q2) == q1)
1259 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1260 subset of the cv-qualification signature of TYPE2, and the types
1261 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1264 comp_cv_qual_signature (tree type1, tree type2)
1266 if (comp_ptr_ttypes_real (type2, type1, -1))
1268 else if (comp_ptr_ttypes_real (type1, type2, -1))
1274 /* If two types share a common base type, return that basetype.
1275 If there is not a unique most-derived base type, this function
1276 returns ERROR_MARK_NODE. */
1279 common_base_type (tree tt1, tree tt2)
1281 tree best = NULL_TREE;
1284 /* If one is a baseclass of another, that's good enough. */
1285 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1287 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1290 /* Otherwise, try to find a unique baseclass of TT1
1291 that is shared by TT2, and follow that down. */
1292 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1294 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1295 tree trial = common_base_type (basetype, tt2);
1298 if (trial == error_mark_node)
1300 if (best == NULL_TREE)
1302 else if (best != trial)
1303 return error_mark_node;
1308 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1310 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1311 tree trial = common_base_type (tt1, basetype);
1314 if (trial == error_mark_node)
1316 if (best == NULL_TREE)
1318 else if (best != trial)
1319 return error_mark_node;
1325 /* Subroutines of `comptypes'. */
1327 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1328 equivalent in the sense that functions with those parameter types
1329 can have equivalent types. The two lists must be equivalent,
1330 element by element. */
1333 compparms (tree parms1, tree parms2)
1337 /* An unspecified parmlist matches any specified parmlist
1338 whose argument types don't need default promotions. */
1340 for (t1 = parms1, t2 = parms2;
1342 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1344 /* If one parmlist is shorter than the other,
1345 they fail to match. */
1348 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1354 /* This really wants return whether or not parameter type lists
1355 would make their owning functions assignment compatible or not.
1357 The return value is like for comp_target_types.
1359 This should go away, possibly with the exception of the empty parmlist
1360 conversion; there are no conversions between function types in C++.
1361 (jason 17 Apr 1997) */
1364 comp_target_parms (tree parms1, tree parms2)
1366 register tree t1 = parms1, t2 = parms2;
1367 int warn_contravariance = 0;
1369 /* In C, an unspecified parmlist matches any specified parmlist
1370 whose argument types don't need default promotions. This is not
1371 true for C++, but let's do it anyway for unfixed headers. */
1373 if (t1 == 0 && t2 != 0)
1375 pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1377 return self_promoting_args_p (t2);
1380 return self_promoting_args_p (t1);
1382 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1386 /* If one parmlist is shorter than the other, they fail to match. */
1389 p1 = TREE_VALUE (t1);
1390 p2 = TREE_VALUE (t2);
1391 if (same_type_p (p1, p2))
1397 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1398 || (TREE_CODE (p1) == REFERENCE_TYPE
1399 && TREE_CODE (p2) == REFERENCE_TYPE))
1401 /* The following is wrong for contravariance,
1402 but many programs depend on it. */
1403 if (TREE_TYPE (p1) == void_type_node)
1405 if (TREE_TYPE (p2) == void_type_node)
1407 warn_contravariance = 1;
1410 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1411 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1415 /* Note backwards order due to contravariance. */
1416 if (comp_target_types (p2, p1, 1) <= 0)
1418 if (comp_target_types (p1, p2, 1) > 0)
1420 warn_contravariance = 1;
1426 return warn_contravariance ? -1 : 1;
1430 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, int complain)
1432 enum tree_code type_code;
1434 const char *op_name;
1436 my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
1437 if (processing_template_decl)
1438 return build_min_nt (op, type);
1440 op_name = operator_name_info[(int) op].name;
1442 if (TREE_CODE (type) == REFERENCE_TYPE)
1443 type = TREE_TYPE (type);
1444 type_code = TREE_CODE (type);
1446 if (type_code == METHOD_TYPE)
1448 if (complain && (pedantic || warn_pointer_arith))
1449 pedwarn ("invalid application of `%s' to a member function", op_name);
1450 value = size_one_node;
1452 else if (type_code == OFFSET_TYPE)
1455 error ("invalid application of `%s' to non-static member", op_name);
1456 value = size_zero_node;
1459 value = c_sizeof_or_alignof_type (complete_type (type), op, complain);
1465 expr_sizeof (tree e)
1467 if (processing_template_decl)
1468 return build_min_nt (SIZEOF_EXPR, e);
1470 if (TREE_CODE (e) == COMPONENT_REF
1471 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1472 error ("sizeof applied to a bit-field");
1473 if (is_overloaded_fn (e))
1475 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1476 return c_sizeof (char_type_node);
1478 else if (type_unknown_p (e))
1480 cxx_incomplete_type_error (e, TREE_TYPE (e));
1481 return c_sizeof (char_type_node);
1483 /* It's invalid to say `sizeof (X::i)' for `i' a non-static data
1484 member unless you're in a non-static member of X. So hand off to
1485 resolve_offset_ref. [expr.prim] */
1486 else if (TREE_CODE (e) == OFFSET_REF)
1487 e = resolve_offset_ref (e);
1489 if (e == error_mark_node)
1492 return cxx_sizeof (TREE_TYPE (e));
1496 /* Perform the array-to-pointer and function-to-pointer conversions
1499 In addition, references are converted to lvalues and manifest
1500 constants are replaced by their values. */
1503 decay_conversion (tree exp)
1506 register enum tree_code code;
1508 if (TREE_CODE (exp) == OFFSET_REF)
1509 exp = resolve_offset_ref (exp);
1511 type = TREE_TYPE (exp);
1512 code = TREE_CODE (type);
1514 if (code == REFERENCE_TYPE)
1516 exp = convert_from_reference (exp);
1517 type = TREE_TYPE (exp);
1518 code = TREE_CODE (type);
1521 if (type == error_mark_node)
1522 return error_mark_node;
1524 if (type_unknown_p (exp))
1526 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1527 return error_mark_node;
1530 /* Constants can be used directly unless they're not loadable. */
1531 if (TREE_CODE (exp) == CONST_DECL)
1532 exp = DECL_INITIAL (exp);
1533 /* Replace a nonvolatile const static variable with its value. We
1534 don't do this for arrays, though; we want the address of the
1535 first element of the array, not the address of the first element
1536 of its initializing constant. */
1537 else if (code != ARRAY_TYPE)
1539 exp = decl_constant_value (exp);
1540 type = TREE_TYPE (exp);
1543 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1544 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1546 if (code == VOID_TYPE)
1548 error ("void value not ignored as it ought to be");
1549 return error_mark_node;
1551 if (code == METHOD_TYPE)
1553 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1554 return build_unary_op (ADDR_EXPR, exp, 0);
1555 if (code == ARRAY_TYPE)
1560 if (TREE_CODE (exp) == INDIRECT_REF)
1562 /* Stripping away the INDIRECT_REF is not the right
1563 thing to do for references... */
1564 tree inner = TREE_OPERAND (exp, 0);
1565 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1567 inner = build1 (CONVERT_EXPR,
1568 build_pointer_type (TREE_TYPE
1569 (TREE_TYPE (inner))),
1571 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1573 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1576 if (TREE_CODE (exp) == COMPOUND_EXPR)
1578 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1579 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1580 TREE_OPERAND (exp, 0), op1);
1584 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1586 error ("invalid use of non-lvalue array");
1587 return error_mark_node;
1590 ptrtype = build_pointer_type (TREE_TYPE (type));
1592 if (TREE_CODE (exp) == VAR_DECL)
1594 /* ??? This is not really quite correct
1595 in that the type of the operand of ADDR_EXPR
1596 is not the target type of the type of the ADDR_EXPR itself.
1597 Question is, can this lossage be avoided? */
1598 adr = build1 (ADDR_EXPR, ptrtype, exp);
1599 if (!cxx_mark_addressable (exp))
1600 return error_mark_node;
1601 TREE_CONSTANT (adr) = staticp (exp);
1602 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1605 /* This way is better for a COMPONENT_REF since it can
1606 simplify the offset for a component. */
1607 adr = build_unary_op (ADDR_EXPR, exp, 1);
1608 return cp_convert (ptrtype, adr);
1611 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1612 rvalues always have cv-unqualified types. */
1613 if (! CLASS_TYPE_P (type))
1614 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1620 default_conversion (tree exp)
1623 enum tree_code code;
1625 exp = decay_conversion (exp);
1627 type = TREE_TYPE (exp);
1628 code = TREE_CODE (type);
1630 if (INTEGRAL_CODE_P (code))
1632 tree t = type_promotes_to (type);
1634 return cp_convert (t, exp);
1640 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1644 inline_conversion (tree exp)
1646 if (TREE_CODE (exp) == FUNCTION_DECL)
1647 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1652 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1653 decay_conversion to one. */
1656 string_conv_p (tree totype, tree exp, int warn)
1660 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1663 t = TREE_TYPE (totype);
1664 if (!same_type_p (t, char_type_node)
1665 && !same_type_p (t, wchar_type_node))
1668 if (TREE_CODE (exp) == STRING_CST)
1670 /* Make sure that we don't try to convert between char and wchar_t. */
1671 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1676 /* Is this a string constant which has decayed to 'const char *'? */
1677 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1678 if (!same_type_p (TREE_TYPE (exp), t))
1681 if (TREE_CODE (exp) != ADDR_EXPR
1682 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1686 /* This warning is not very useful, as it complains about printf. */
1687 if (warn && warn_write_strings)
1688 warning ("deprecated conversion from string constant to `%T'", totype);
1693 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1694 can, for example, use as an lvalue. This code used to be in
1695 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1696 expressions, where we're dealing with aggregates. But now it's again only
1697 called from unary_complex_lvalue. The case (in particular) that led to
1698 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1702 rationalize_conditional_expr (enum tree_code code, tree t)
1704 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1705 the first operand is always the one to be used if both operands
1706 are equal, so we know what conditional expression this used to be. */
1707 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1710 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1711 ? LE_EXPR : GE_EXPR),
1712 TREE_OPERAND (t, 0),
1713 TREE_OPERAND (t, 1)),
1714 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1715 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1719 build_conditional_expr (TREE_OPERAND (t, 0),
1720 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1721 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1724 /* Given the TYPE of an anonymous union field inside T, return the
1725 FIELD_DECL for the field. If not found return NULL_TREE. Because
1726 anonymous unions can nest, we must also search all anonymous unions
1727 that are directly reachable. */
1730 lookup_anon_field (tree t, tree type)
1734 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1736 if (TREE_STATIC (field))
1738 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1741 /* If we find it directly, return the field. */
1742 if (DECL_NAME (field) == NULL_TREE
1743 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1748 /* Otherwise, it could be nested, search harder. */
1749 if (DECL_NAME (field) == NULL_TREE
1750 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1752 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1760 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1761 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1762 non-NULL, it indicates the path to the base used to name MEMBER.
1763 If PRESERVE_REFERENCE is true, the expression returned will have
1764 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1765 returned will have the type referred to by the reference.
1767 This function does not perform access control; that is either done
1768 earlier by the parser when the name of MEMBER is resolved to MEMBER
1769 itself, or later when overload resolution selects one of the
1770 functions indicated by MEMBER. */
1773 build_class_member_access_expr (tree object, tree member,
1774 tree access_path, bool preserve_reference)
1778 tree result = NULL_TREE;
1780 if (object == error_mark_node || member == error_mark_node)
1781 return error_mark_node;
1783 if (TREE_CODE (member) == PSEUDO_DTOR_EXPR)
1786 my_friendly_assert (DECL_P (member) || BASELINK_P (member),
1791 The type of the first expression shall be "class object" (of a
1793 object_type = TREE_TYPE (object);
1794 if (!complete_type_or_else (object_type, object))
1795 return error_mark_node;
1796 if (!CLASS_TYPE_P (object_type))
1798 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1799 member, object, object_type);
1800 return error_mark_node;
1803 /* The standard does not seem to actually say that MEMBER must be a
1804 member of OBJECT_TYPE. However, that is clearly what is
1806 if (DECL_P (member))
1808 member_scope = DECL_CLASS_CONTEXT (member);
1810 if (TREE_DEPRECATED (member))
1811 warn_deprecated_use (member);
1814 member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1815 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1816 presently be the anonymous union. Go outwards until we find a
1817 type related to OBJECT_TYPE. */
1818 while (ANON_AGGR_TYPE_P (member_scope)
1819 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1821 member_scope = TYPE_CONTEXT (member_scope);
1822 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1824 error ("`%D' is not a member of `%T'", member, object_type);
1825 return error_mark_node;
1828 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1829 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
1830 in the frontend; only _DECLs and _REFs are lvalues in the backend. */
1832 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1834 object = build_indirect_ref (temp, NULL);
1837 /* In [expr.ref], there is an explicit list of the valid choices for
1838 MEMBER. We check for each of those cases here. */
1839 if (TREE_CODE (member) == VAR_DECL)
1841 /* A static data member. */
1843 /* If OBJECT has side-effects, they are supposed to occur. */
1844 if (TREE_SIDE_EFFECTS (object))
1845 result = build (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1847 else if (TREE_CODE (member) == FIELD_DECL)
1849 /* A non-static data member. */
1854 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1855 && integer_zerop (TREE_OPERAND (object, 0)));
1857 /* Convert OBJECT to the type of MEMBER. */
1858 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
1859 TYPE_MAIN_VARIANT (member_scope)))
1864 binfo = lookup_base (access_path ? access_path : object_type,
1865 member_scope, ba_ignore, &kind);
1866 if (binfo == error_mark_node)
1867 return error_mark_node;
1869 /* It is invalid to try to get to a virtual base of a
1870 NULL object. The most common cause is invalid use of
1872 if (null_object_p && kind == bk_via_virtual)
1874 error ("invalid access to non-static data member `%D' of NULL object",
1876 error ("(perhaps the `offsetof' macro was used incorrectly)");
1877 return error_mark_node;
1880 /* Convert to the base. */
1881 object = build_base_path (PLUS_EXPR, object, binfo,
1883 /* If we found the base successfully then we should be able
1884 to convert to it successfully. */
1885 my_friendly_assert (object != error_mark_node,
1889 /* Complain about other invalid uses of offsetof, even though they will
1890 give the right answer. Note that we complain whether or not they
1891 actually used the offsetof macro, since there's no way to know at this
1892 point. So we just give a warning, instead of a pedwarn. */
1893 if (null_object_p && warn_invalid_offsetof
1894 && CLASSTYPE_NON_POD_P (object_type))
1896 warning ("invalid access to non-static data member `%D' of NULL object",
1898 warning ("(perhaps the `offsetof' macro was used incorrectly)");
1901 /* If MEMBER is from an anonymous aggregate, we have converted
1902 OBJECT so that it refers to the class containing the
1903 anonymous union. Generate a reference to the anonymous union
1904 itself, and recur to find MEMBER. */
1905 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1906 /* When this code is called from build_field_call, the
1907 object already has the type of the anonymous union.
1908 That is because the COMPONENT_REF was already
1909 constructed, and was then disassembled before calling
1910 build_field_call. After the function-call code is
1911 cleaned up, this waste can be eliminated. */
1912 && (!same_type_ignoring_top_level_qualifiers_p
1913 (TREE_TYPE (object), DECL_CONTEXT (member))))
1915 tree anonymous_union;
1917 anonymous_union = lookup_anon_field (TREE_TYPE (object),
1918 DECL_CONTEXT (member));
1919 object = build_class_member_access_expr (object,
1921 /*access_path=*/NULL_TREE,
1922 preserve_reference);
1925 /* Compute the type of the field, as described in [expr.ref]. */
1926 type_quals = TYPE_UNQUALIFIED;
1927 member_type = TREE_TYPE (member);
1928 if (TREE_CODE (member_type) != REFERENCE_TYPE)
1930 type_quals = (cp_type_quals (member_type)
1931 | cp_type_quals (object_type));
1933 /* A field is const (volatile) if the enclosing object, or the
1934 field itself, is const (volatile). But, a mutable field is
1935 not const, even within a const object. */
1936 if (DECL_MUTABLE_P (member))
1937 type_quals &= ~TYPE_QUAL_CONST;
1938 member_type = cp_build_qualified_type (member_type, type_quals);
1941 result = fold (build (COMPONENT_REF, member_type, object, member));
1943 /* Mark the expression const or volatile, as appropriate. Even
1944 though we've dealt with the type above, we still have to mark the
1945 expression itself. */
1946 if (type_quals & TYPE_QUAL_CONST)
1947 TREE_READONLY (result) = 1;
1948 else if (type_quals & TYPE_QUAL_VOLATILE)
1949 TREE_THIS_VOLATILE (result) = 1;
1951 else if (BASELINK_P (member))
1953 /* The member is a (possibly overloaded) member function. */
1957 /* If the MEMBER is exactly one static member function, then we
1958 know the type of the expression. Otherwise, we must wait
1959 until overload resolution has been performed. */
1960 functions = BASELINK_FUNCTIONS (member);
1961 if (TREE_CODE (functions) == FUNCTION_DECL
1962 && DECL_STATIC_FUNCTION_P (functions))
1963 type = TREE_TYPE (functions);
1965 type = unknown_type_node;
1966 /* Note that we do not convert OBJECT to the BASELINK_BINFO
1967 base. That will happen when the function is called. */
1968 result = build (COMPONENT_REF, type, object, member);
1970 else if (TREE_CODE (member) == CONST_DECL)
1972 /* The member is an enumerator. */
1974 /* If OBJECT has side-effects, they are supposed to occur. */
1975 if (TREE_SIDE_EFFECTS (object))
1976 result = build (COMPOUND_EXPR, TREE_TYPE (result),
1981 error ("invalid use of `%D'", member);
1982 return error_mark_node;
1985 if (!preserve_reference)
1988 If E2 is declared to have type "reference to T", then ... the
1989 type of E1.E2 is T. */
1990 result = convert_from_reference (result);
1995 /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
1996 SCOPE is NULL, by OBJECT.~DTOR_NAME. */
1999 lookup_destructor (tree object, tree scope, tree dtor_name)
2001 tree object_type = TREE_TYPE (object);
2002 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2004 if (scope && !check_dtor_name (scope, dtor_name))
2006 error ("qualified type `%T' does not match destructor name `~%T'",
2008 return error_mark_node;
2010 if (!same_type_p (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2012 error ("destructor name `%T' does not match type `%T' of expression",
2013 dtor_type, object_type);
2014 return error_mark_node;
2016 if (!TYPE_HAS_DESTRUCTOR (object_type))
2017 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope,
2019 return lookup_member (object_type, complete_dtor_identifier,
2020 /*protect=*/1, /*want_type=*/false);
2023 /* This function is called by the parser to process a class member
2024 access expression of the form OBJECT.NAME. NAME is a node used by
2025 the parser to represent a name; it is not yet a DECL. It may,
2026 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2027 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2028 there is no reason to do the lookup twice, so the parser keeps the
2032 finish_class_member_access_expr (tree object, tree name)
2036 tree access_path = NULL_TREE;
2038 if (object == error_mark_node || name == error_mark_node)
2039 return error_mark_node;
2041 if (processing_template_decl)
2042 return build_min_nt (COMPONENT_REF, object, name);
2044 if (TREE_CODE (object) == OFFSET_REF)
2045 object = resolve_offset_ref (object);
2047 object_type = TREE_TYPE (object);
2048 if (TREE_CODE (object_type) == REFERENCE_TYPE)
2050 object = convert_from_reference (object);
2051 object_type = TREE_TYPE (object);
2056 The type of the first expression shall be "class object" (of a
2058 if (!complete_type_or_else (object_type, object))
2059 return error_mark_node;
2060 if (!CLASS_TYPE_P (object_type))
2062 error ("request for member `%D' in `%E', which is of non-class type `%T'",
2063 name, object, object_type);
2064 return error_mark_node;
2067 if (BASELINK_P (name))
2069 /* A member function that has already been looked up. */
2070 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
2071 == TEMPLATE_ID_EXPR),
2077 bool is_template_id = false;
2078 tree template_args = NULL_TREE;
2080 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2082 is_template_id = true;
2083 template_args = TREE_OPERAND (name, 1);
2084 name = TREE_OPERAND (name, 0);
2087 if (TREE_CODE (name) == SCOPE_REF)
2091 /* A qualified name. The qualifying class or namespace `S' has
2092 already been looked up; it is either a TYPE or a
2093 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
2094 or a BIT_NOT_EXPR. */
2095 scope = TREE_OPERAND (name, 0);
2096 name = TREE_OPERAND (name, 1);
2097 my_friendly_assert ((CLASS_TYPE_P (scope)
2098 || TREE_CODE (scope) == NAMESPACE_DECL),
2100 my_friendly_assert ((TREE_CODE (name) == IDENTIFIER_NODE
2101 || TREE_CODE (name) == BIT_NOT_EXPR),
2104 /* If SCOPE is a namespace, then the qualified name does not
2105 name a member of OBJECT_TYPE. */
2106 if (TREE_CODE (scope) == NAMESPACE_DECL)
2108 error ("`%D::%D' is not a member of `%T'",
2109 scope, name, object_type);
2110 return error_mark_node;
2113 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2114 access_path = lookup_base (object_type, scope, ba_check, NULL);
2115 if (!access_path || access_path == error_mark_node)
2116 return error_mark_node;
2118 if (TREE_CODE (name) == BIT_NOT_EXPR)
2119 member = lookup_destructor (object, scope, name);
2122 /* Look up the member. */
2123 member = lookup_member (access_path, name, /*protect=*/1,
2124 /*want_type=*/false);
2125 if (member == NULL_TREE)
2127 error ("'%D' has no member named '%E'", object_type, name);
2128 return error_mark_node;
2130 if (member == error_mark_node)
2131 return error_mark_node;
2134 else if (TREE_CODE (name) == BIT_NOT_EXPR)
2135 member = lookup_destructor (object, /*scope=*/NULL_TREE, name);
2136 else if (TREE_CODE (name) == IDENTIFIER_NODE)
2138 /* An unqualified name. */
2139 member = lookup_member (object_type, name, /*protect=*/1,
2140 /*want_type=*/false);
2141 if (member == NULL_TREE)
2143 error ("'%D' has no member named '%E'", object_type, name);
2144 return error_mark_node;
2146 else if (member == error_mark_node)
2147 return error_mark_node;
2151 /* The YACC parser sometimes gives us things that are not names.
2152 These always indicate errors. The recursive-descent parser
2153 does not do this, so this code can go away once that parser
2154 replaces the YACC parser. */
2155 error ("invalid use of `%D'", name);
2156 return error_mark_node;
2161 tree template = member;
2163 if (BASELINK_P (template))
2164 BASELINK_FUNCTIONS (template)
2165 = build_nt (TEMPLATE_ID_EXPR,
2166 BASELINK_FUNCTIONS (template),
2170 error ("`%D' is not a member template function", name);
2171 return error_mark_node;
2176 if (TREE_DEPRECATED (member))
2177 warn_deprecated_use (member);
2179 return build_class_member_access_expr (object, member, access_path,
2180 /*preserve_reference=*/false);
2183 /* Return an expression for the MEMBER_NAME field in the internal
2184 representation of PTRMEM, a pointer-to-member function. (Each
2185 pointer-to-member function type gets its own RECORD_TYPE so it is
2186 more convenient to access the fields by name than by FIELD_DECL.)
2187 This routine converts the NAME to a FIELD_DECL and then creates the
2188 node for the complete expression. */
2191 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2197 /* This code is a stripped down version of
2198 build_class_member_access_expr. It does not work to use that
2199 routine directly because it expects the object to be of class
2201 ptrmem_type = TREE_TYPE (ptrmem);
2202 my_friendly_assert (TYPE_PTRMEMFUNC_P (ptrmem_type), 20020804);
2203 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2204 /*want_type=*/false);
2205 member_type = cp_build_qualified_type (TREE_TYPE (member),
2206 cp_type_quals (ptrmem_type));
2207 return fold (build (COMPONENT_REF, member_type, ptrmem, member));
2210 /* Given an expression PTR for a pointer, return an expression
2211 for the value pointed to.
2212 ERRORSTRING is the name of the operator to appear in error messages.
2214 This function may need to overload OPERATOR_FNNAME.
2215 Must also handle REFERENCE_TYPEs for C++. */
2218 build_x_indirect_ref (ptr, errorstring)
2220 const char *errorstring;
2224 if (processing_template_decl)
2225 return build_min_nt (INDIRECT_REF, ptr);
2227 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2231 return build_indirect_ref (ptr, errorstring);
2235 build_indirect_ref (ptr, errorstring)
2237 const char *errorstring;
2239 register tree pointer, type;
2241 if (ptr == error_mark_node)
2242 return error_mark_node;
2244 if (ptr == current_class_ptr)
2245 return current_class_ref;
2247 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2248 ? ptr : default_conversion (ptr));
2249 type = TREE_TYPE (pointer);
2251 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2255 If the type of the expression is "pointer to T," the type
2256 of the result is "T."
2258 We must use the canonical variant because certain parts of
2259 the back end, like fold, do pointer comparisons between
2261 tree t = canonical_type_variant (TREE_TYPE (type));
2263 if (VOID_TYPE_P (t))
2265 /* A pointer to incomplete type (other than cv void) can be
2266 dereferenced [expr.unary.op]/1 */
2267 error ("`%T' is not a pointer-to-object type", type);
2268 return error_mark_node;
2270 else if (TREE_CODE (pointer) == ADDR_EXPR
2271 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2272 /* The POINTER was something like `&x'. We simplify `*&x' to
2274 return TREE_OPERAND (pointer, 0);
2277 tree ref = build1 (INDIRECT_REF, t, pointer);
2279 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2280 so that we get the proper error message if the result is used
2281 to assign to. Also, &* is supposed to be a no-op. */
2282 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2283 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2284 TREE_SIDE_EFFECTS (ref)
2285 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2289 /* `pointer' won't be an error_mark_node if we were given a
2290 pointer to member, so it's cool to check for this here. */
2291 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2292 error ("invalid use of `%s' on pointer to member", errorstring);
2293 else if (pointer != error_mark_node)
2296 error ("invalid type argument of `%s'", errorstring);
2298 error ("invalid type argument");
2300 return error_mark_node;
2303 /* This handles expressions of the form "a[i]", which denotes
2306 This is logically equivalent in C to *(a+i), but we may do it differently.
2307 If A is a variable or a member, we generate a primitive ARRAY_REF.
2308 This avoids forcing the array out of registers, and can work on
2309 arrays that are not lvalues (for example, members of structures returned
2312 If INDEX is of some user-defined type, it must be converted to
2313 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2314 will inherit the type of the array, which will be some pointer type. */
2317 build_array_ref (array, idx)
2322 error ("subscript missing in array reference");
2323 return error_mark_node;
2326 if (TREE_TYPE (array) == error_mark_node
2327 || TREE_TYPE (idx) == error_mark_node)
2328 return error_mark_node;
2330 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2332 switch (TREE_CODE (array))
2336 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2337 return build (COMPOUND_EXPR, TREE_TYPE (value),
2338 TREE_OPERAND (array, 0), value);
2342 return build_conditional_expr
2343 (TREE_OPERAND (array, 0),
2344 build_array_ref (TREE_OPERAND (array, 1), idx),
2345 build_array_ref (TREE_OPERAND (array, 2), idx));
2351 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2352 && TREE_CODE (array) != INDIRECT_REF)
2356 /* Subscripting with type char is likely to lose
2357 on a machine where chars are signed.
2358 So warn on any machine, but optionally.
2359 Don't warn for unsigned char since that type is safe.
2360 Don't warn for signed char because anyone who uses that
2361 must have done so deliberately. */
2362 if (warn_char_subscripts
2363 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2364 warning ("array subscript has type `char'");
2366 /* Apply default promotions *after* noticing character types. */
2367 idx = default_conversion (idx);
2369 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2371 error ("array subscript is not an integer");
2372 return error_mark_node;
2375 /* An array that is indexed by a non-constant
2376 cannot be stored in a register; we must be able to do
2377 address arithmetic on its address.
2378 Likewise an array of elements of variable size. */
2379 if (TREE_CODE (idx) != INTEGER_CST
2380 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2381 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2384 if (!cxx_mark_addressable (array))
2385 return error_mark_node;
2388 /* An array that is indexed by a constant value which is not within
2389 the array bounds cannot be stored in a register either; because we
2390 would get a crash in store_bit_field/extract_bit_field when trying
2391 to access a non-existent part of the register. */
2392 if (TREE_CODE (idx) == INTEGER_CST
2393 && TYPE_VALUES (TREE_TYPE (array))
2394 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2396 if (!cxx_mark_addressable (array))
2397 return error_mark_node;
2400 if (pedantic && !lvalue_p (array))
2401 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2403 /* Note in C++ it is valid to subscript a `register' array, since
2404 it is valid to take the address of something with that
2405 storage specification. */
2409 while (TREE_CODE (foo) == COMPONENT_REF)
2410 foo = TREE_OPERAND (foo, 0);
2411 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2412 warning ("subscripting array declared `register'");
2415 type = TREE_TYPE (TREE_TYPE (array));
2416 rval = build (ARRAY_REF, type, array, idx);
2417 /* Array ref is const/volatile if the array elements are
2418 or if the array is.. */
2419 TREE_READONLY (rval)
2420 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2421 TREE_SIDE_EFFECTS (rval)
2422 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2423 TREE_THIS_VOLATILE (rval)
2424 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2425 return require_complete_type (fold (rval));
2429 tree ar = default_conversion (array);
2430 tree ind = default_conversion (idx);
2432 /* Put the integer in IND to simplify error checking. */
2433 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2440 if (ar == error_mark_node)
2443 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2445 error ("subscripted value is neither array nor pointer");
2446 return error_mark_node;
2448 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2450 error ("array subscript is not an integer");
2451 return error_mark_node;
2454 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2459 /* Resolve a pointer to member function. INSTANCE is the object
2460 instance to use, if the member points to a virtual member.
2462 This used to avoid checking for virtual functions if basetype
2463 has no virtual functions, according to an earlier ANSI draft.
2464 With the final ISO C++ rules, such an optimization is
2465 incorrect: A pointer to a derived member can be static_cast
2466 to pointer-to-base-member, as long as the dynamic object
2467 later has the right member. */
2470 get_member_function_from_ptrfunc (instance_ptrptr, function)
2471 tree *instance_ptrptr;
2474 if (TREE_CODE (function) == OFFSET_REF)
2475 function = TREE_OPERAND (function, 1);
2477 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2479 tree idx, delta, e1, e2, e3, vtbl, basetype;
2480 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2482 tree instance_ptr = *instance_ptrptr;
2483 tree instance_save_expr = 0;
2484 if (instance_ptr == error_mark_node)
2486 if (TREE_CODE (function) == PTRMEM_CST)
2488 /* Extracting the function address from a pmf is only
2489 allowed with -Wno-pmf-conversions. It only works for
2491 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2492 e1 = convert (fntype, e1);
2497 error ("object missing in use of `%E'", function);
2498 return error_mark_node;
2502 if (TREE_SIDE_EFFECTS (instance_ptr))
2503 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2505 if (TREE_SIDE_EFFECTS (function))
2506 function = save_expr (function);
2508 /* Start by extracting all the information from the PMF itself. */
2509 e3 = PFN_FROM_PTRMEMFUNC (function);
2510 delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2511 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2512 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2514 case ptrmemfunc_vbit_in_pfn:
2515 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2516 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2519 case ptrmemfunc_vbit_in_delta:
2520 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2521 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2528 /* Convert down to the right base before using the instance. First
2530 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2531 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2532 basetype, ba_check, NULL);
2533 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1);
2534 if (instance_ptr == error_mark_node)
2535 return error_mark_node;
2536 /* ...and then the delta in the PMF. */
2537 instance_ptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2538 instance_ptr, delta);
2540 /* Hand back the adjusted 'this' argument to our caller. */
2541 *instance_ptrptr = instance_ptr;
2543 /* Next extract the vtable pointer from the object. */
2544 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2546 vtbl = build_indirect_ref (vtbl, NULL);
2548 /* Finally, extract the function pointer from the vtable. */
2549 e2 = fold (build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx));
2550 e2 = build_indirect_ref (e2, NULL);
2551 TREE_CONSTANT (e2) = 1;
2553 /* When using function descriptors, the address of the
2554 vtable entry is treated as a function pointer. */
2555 if (TARGET_VTABLE_USES_DESCRIPTORS)
2556 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2557 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2559 TREE_TYPE (e2) = TREE_TYPE (e3);
2560 e1 = build_conditional_expr (e1, e2, e3);
2562 /* Make sure this doesn't get evaluated first inside one of the
2563 branches of the COND_EXPR. */
2564 if (instance_save_expr)
2565 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2566 instance_save_expr, e1);
2574 build_function_call (function, params)
2575 tree function, params;
2577 register tree fntype, fndecl;
2578 register tree coerced_params;
2580 tree name = NULL_TREE, assembler_name = NULL_TREE;
2582 tree original = function;
2584 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2585 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2586 if (TREE_CODE (function) == NOP_EXPR
2587 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2588 function = TREE_OPERAND (function, 0);
2590 if (TREE_CODE (function) == FUNCTION_DECL)
2592 name = DECL_NAME (function);
2593 assembler_name = DECL_ASSEMBLER_NAME (function);
2595 mark_used (function);
2598 /* Convert anything with function type to a pointer-to-function. */
2599 if (pedantic && DECL_MAIN_P (function))
2600 pedwarn ("ISO C++ forbids calling `::main' from within program");
2602 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2603 (because calling an inline function does not mean the function
2604 needs to be separately compiled). */
2606 if (DECL_INLINE (function))
2607 function = inline_conversion (function);
2609 function = build_addr_func (function);
2615 function = build_addr_func (function);
2618 if (function == error_mark_node)
2619 return error_mark_node;
2621 fntype = TREE_TYPE (function);
2623 if (TYPE_PTRMEMFUNC_P (fntype))
2625 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2627 return error_mark_node;
2630 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2631 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2633 if (!((TREE_CODE (fntype) == POINTER_TYPE
2634 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2636 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2638 error ("`%E' cannot be used as a function", original);
2639 return error_mark_node;
2642 /* fntype now gets the type of function pointed to. */
2643 fntype = TREE_TYPE (fntype);
2645 /* Convert the parameters to the types declared in the
2646 function prototype, or apply default promotions. */
2648 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2649 params, fndecl, LOOKUP_NORMAL);
2650 if (coerced_params == error_mark_node)
2651 return error_mark_node;
2653 /* Check for errors in format strings. */
2656 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
2658 /* Recognize certain built-in functions so we can make tree-codes
2659 other than CALL_EXPR. We do this when it enables fold-const.c
2660 to do something useful. */
2662 if (TREE_CODE (function) == ADDR_EXPR
2663 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2664 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2666 result = expand_tree_builtin (TREE_OPERAND (function, 0),
2667 params, coerced_params);
2672 return build_cxx_call (function, params, coerced_params);
2675 /* Convert the actual parameter expressions in the list VALUES
2676 to the types in the list TYPELIST.
2677 If parmdecls is exhausted, or when an element has NULL as its type,
2678 perform the default conversions.
2680 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2682 This is also where warnings about wrong number of args are generated.
2684 Return a list of expressions for the parameters as converted.
2686 Both VALUES and the returned value are chains of TREE_LIST nodes
2687 with the elements of the list in the TREE_VALUE slots of those nodes.
2689 In C++, unspecified trailing parameters can be filled in with their
2690 default arguments, if such were specified. Do so here. */
2693 convert_arguments (typelist, values, fndecl, flags)
2694 tree typelist, values, fndecl;
2697 register tree typetail, valtail;
2698 register tree result = NULL_TREE;
2699 const char *called_thing = 0;
2702 /* Argument passing is always copy-initialization. */
2703 flags |= LOOKUP_ONLYCONVERTING;
2707 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2709 if (DECL_NAME (fndecl) == NULL_TREE
2710 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2711 called_thing = "constructor";
2713 called_thing = "member function";
2716 called_thing = "function";
2719 for (valtail = values, typetail = typelist;
2721 valtail = TREE_CHAIN (valtail), i++)
2723 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2724 register tree val = TREE_VALUE (valtail);
2726 if (val == error_mark_node)
2727 return error_mark_node;
2729 if (type == void_type_node)
2733 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
2735 error ("at this point in file");
2738 error ("too many arguments to function");
2739 /* In case anybody wants to know if this argument
2742 TREE_TYPE (tree_last (result)) = error_mark_node;
2746 if (TREE_CODE (val) == OFFSET_REF)
2747 val = resolve_offset_ref (val);
2749 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2750 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2751 if (TREE_CODE (val) == NOP_EXPR
2752 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2753 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2754 val = TREE_OPERAND (val, 0);
2756 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2758 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2759 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2760 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2761 val = default_conversion (val);
2764 if (val == error_mark_node)
2765 return error_mark_node;
2769 /* Formal parm type is specified by a function prototype. */
2772 if (!COMPLETE_TYPE_P (complete_type (type)))
2774 error ("parameter type of called function is incomplete");
2779 parmval = convert_for_initialization
2780 (NULL_TREE, type, val, flags,
2781 "argument passing", fndecl, i);
2782 parmval = convert_for_arg_passing (type, parmval);
2785 if (parmval == error_mark_node)
2786 return error_mark_node;
2788 result = tree_cons (NULL_TREE, parmval, result);
2792 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2793 val = convert_from_reference (val);
2795 if (fndecl && DECL_BUILT_IN (fndecl)
2796 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2797 /* Don't do ellipsis conversion for __built_in_constant_p
2798 as this will result in spurious warnings for non-POD
2800 val = require_complete_type (val);
2802 val = convert_arg_to_ellipsis (val);
2804 result = tree_cons (NULL_TREE, val, result);
2808 typetail = TREE_CHAIN (typetail);
2811 if (typetail != 0 && typetail != void_list_node)
2813 /* See if there are default arguments that can be used */
2814 if (TREE_PURPOSE (typetail)
2815 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
2817 for (; typetail != void_list_node; ++i)
2820 = convert_default_arg (TREE_VALUE (typetail),
2821 TREE_PURPOSE (typetail),
2824 if (parmval == error_mark_node)
2825 return error_mark_node;
2827 result = tree_cons (0, parmval, result);
2828 typetail = TREE_CHAIN (typetail);
2829 /* ends with `...'. */
2830 if (typetail == NULL_TREE)
2838 cp_error_at ("too few arguments to %s `%+#D'",
2839 called_thing, fndecl);
2840 error ("at this point in file");
2843 error ("too few arguments to function");
2844 return error_mark_list;
2848 return nreverse (result);
2851 /* Build a binary-operation expression, after performing default
2852 conversions on the operands. CODE is the kind of expression to build. */
2855 build_x_binary_op (code, arg1, arg2)
2856 enum tree_code code;
2859 if (processing_template_decl)
2860 return build_min_nt (code, arg1, arg2);
2862 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2868 build_template_expr (enum tree_code code, tree op0, tree op1, tree op2)
2872 /* If any of the operands is erroneous the result is erroneous too. */
2873 if (error_operand_p (op0)
2874 || (op1 && error_operand_p (op1))
2875 || (op2 && error_operand_p (op2)))
2876 return error_mark_node;
2878 if (dependent_type_p (TREE_TYPE (op0))
2879 || (op1 && dependent_type_p (TREE_TYPE (op1)))
2880 || (op2 && dependent_type_p (TREE_TYPE (op2))))
2881 /* If at least one operand has a dependent type, we cannot
2882 determine the type of the expression until instantiation time. */
2886 struct z_candidate *cand;
2891 /* None of the operands is dependent, so we can compute the type
2892 of the expression at this point. We must compute the type so
2893 that in things like:
2896 void f() { S<sizeof(I + 3)> s; ... }
2898 we can tell that the type of "s" is non-dependent.
2900 If we're processing a template argument, we do not want to
2901 actually change the operands in any way. Adding conversions,
2902 performing constant folding, etc., would all change mangled
2903 names. For example, in:
2906 void f(S<sizeof(3 + 4 + I)>);
2908 we need to determine that "3 + 4 + I" has type "int", without
2909 actually turning the expression into "7 + I". */
2910 cand = find_overloaded_op (code, op0, op1, op2);
2912 /* If an overloaded operator was found, the expression will
2913 have the type returned by the function. */
2914 type = non_reference (TREE_TYPE (cand->fn));
2917 /* There is no overloaded operator so we can just use the
2918 default rules for determining the type of the operand. */
2919 op0_type = TREE_TYPE (op0);
2920 op1_type = op1 ? TREE_TYPE (op1) : NULL_TREE;
2921 op2_type = op2 ? TREE_TYPE (op2) : NULL_TREE;
2929 The result of the assignment operation is the value
2930 stored in the left operand. */
2934 /* Implement this case. */
2936 case POSTINCREMENT_EXPR:
2937 case POSTDECREMENT_EXPR:
2940 The type of the result is the cv-unqualified version
2941 of the type of the operand. */
2942 type = TYPE_MAIN_VARIANT (op0_type);
2944 case PREINCREMENT_EXPR:
2945 case PREDECREMENT_EXPR:
2948 The value is the new value of the operand. */
2954 If the type of the expression is "pointer to T", the
2955 type of the result is "T". */
2956 type = TREE_TYPE (op0_type);
2961 If the type of the expression is "T", the type of the
2962 result is "pointer to T". */
2963 /* FIXME: Handle the pointer-to-member case. */
2966 /* FIXME: Implement this case. */
2972 The type of the result is that of the promoted left
2977 /* FIXME: Be careful of special pointer-arithmetic
2982 /* These are GNU extensions; the result type is computed
2983 as it would be for other arithmetic operators. */
2989 case TRUNC_DIV_EXPR:
2990 case TRUNC_MOD_EXPR:
2991 /* [expr.bit.and], [expr.xor], [expr.or], [expr.mul]
2993 The usual arithmetic conversions are performed on the
2994 operands and determine the type of the result. */
2995 /* FIXME: Check that this is possible. */
2996 type = type_after_usual_arithmetic_conversions (t1, t2);
3006 The type of the result is bool. */
3007 type = boolean_type_node;
3009 case TRUTH_ANDIF_EXPR:
3010 case TRUTH_ORIF_EXPR:
3011 /* [expr.log.and], [expr.log.org]
3013 The result is a bool. */
3014 type = boolean_type_node;
3017 /* FIXME: Handle special rules for conditioanl
3026 /* If the type of the expression could not be determined,
3027 something is wrong. */
3030 /* If the type is erroneous, the expression is erroneous
3032 if (type == error_mark_node)
3033 return error_mark_node;
3037 return build_min (code, type, op0, op1, op2, NULL_TREE);
3042 /* Build a binary-operation expression without default conversions.
3043 CODE is the kind of expression to build.
3044 This function differs from `build' in several ways:
3045 the data type of the result is computed and recorded in it,
3046 warnings are generated if arg data types are invalid,
3047 special handling for addition and subtraction of pointers is known,
3048 and some optimization is done (operations on narrow ints
3049 are done in the narrower type when that gives the same result).
3050 Constant folding is also done before the result is returned.
3052 Note that the operands will never have enumeral types
3053 because either they have just had the default conversions performed
3054 or they have both just been converted to some other type in which
3055 the arithmetic is to be done.
3057 C++: must do special pointer arithmetic when implementing
3058 multiple inheritance, and deal with pointer to member functions. */
3061 build_binary_op (code, orig_op0, orig_op1, convert_p)
3062 enum tree_code code;
3063 tree orig_op0, orig_op1;
3064 int convert_p ATTRIBUTE_UNUSED;
3067 register enum tree_code code0, code1;
3070 /* Expression code to give to the expression when it is built.
3071 Normally this is CODE, which is what the caller asked for,
3072 but in some special cases we change it. */
3073 register enum tree_code resultcode = code;
3075 /* Data type in which the computation is to be performed.
3076 In the simplest cases this is the common type of the arguments. */
3077 register tree result_type = NULL;
3079 /* Nonzero means operands have already been type-converted
3080 in whatever way is necessary.
3081 Zero means they need to be converted to RESULT_TYPE. */
3084 /* Nonzero means create the expression with this type, rather than
3086 tree build_type = 0;
3088 /* Nonzero means after finally constructing the expression
3089 convert it to this type. */
3090 tree final_type = 0;
3092 /* Nonzero if this is an operation like MIN or MAX which can
3093 safely be computed in short if both args are promoted shorts.
3094 Also implies COMMON.
3095 -1 indicates a bitwise operation; this makes a difference
3096 in the exact conditions for when it is safe to do the operation
3097 in a narrower mode. */
3100 /* Nonzero if this is a comparison operation;
3101 if both args are promoted shorts, compare the original shorts.
3102 Also implies COMMON. */
3103 int short_compare = 0;
3105 /* Nonzero if this is a right-shift operation, which can be computed on the
3106 original short and then promoted if the operand is a promoted short. */
3107 int short_shift = 0;
3109 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3112 /* Apply default conversions. */
3116 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3117 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3118 || code == TRUTH_XOR_EXPR)
3120 if (!really_overloaded_fn (op0))
3121 op0 = decay_conversion (op0);
3122 if (!really_overloaded_fn (op1))
3123 op1 = decay_conversion (op1);
3127 if (!really_overloaded_fn (op0))
3128 op0 = default_conversion (op0);
3129 if (!really_overloaded_fn (op1))
3130 op1 = default_conversion (op1);
3133 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3134 STRIP_TYPE_NOPS (op0);
3135 STRIP_TYPE_NOPS (op1);
3137 /* DTRT if one side is an overloaded function, but complain about it. */
3138 if (type_unknown_p (op0))
3140 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3141 if (t != error_mark_node)
3143 pedwarn ("assuming cast to type `%T' from overloaded function",
3148 if (type_unknown_p (op1))
3150 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3151 if (t != error_mark_node)
3153 pedwarn ("assuming cast to type `%T' from overloaded function",
3159 type0 = TREE_TYPE (op0);
3160 type1 = TREE_TYPE (op1);
3162 /* The expression codes of the data types of the arguments tell us
3163 whether the arguments are integers, floating, pointers, etc. */
3164 code0 = TREE_CODE (type0);
3165 code1 = TREE_CODE (type1);
3167 /* If an error was already reported for one of the arguments,
3168 avoid reporting another error. */
3170 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3171 return error_mark_node;
3176 /* Handle the pointer + int case. */
3177 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3178 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
3179 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3180 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
3186 /* Subtraction of two similar pointers.
3187 We must subtract them as integers, then divide by object size. */
3188 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3189 && comp_target_types (type0, type1, 1))
3190 return pointer_diff (op0, op1, common_type (type0, type1));
3191 /* Handle pointer minus int. Just like pointer plus int. */
3192 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3193 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
3202 case TRUNC_DIV_EXPR:
3204 case FLOOR_DIV_EXPR:
3205 case ROUND_DIV_EXPR:
3206 case EXACT_DIV_EXPR:
3207 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3208 || code0 == COMPLEX_TYPE)
3209 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3210 || code1 == COMPLEX_TYPE))
3212 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3213 warning ("division by zero in `%E / 0'", op0);
3214 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3215 warning ("division by zero in `%E / 0.'", op0);
3217 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3218 resultcode = RDIV_EXPR;
3220 /* When dividing two signed integers, we have to promote to int.
3221 unless we divide by a constant != -1. Note that default
3222 conversion will have been performed on the operands at this
3223 point, so we have to dig out the original type to find out if
3225 shorten = ((TREE_CODE (op0) == NOP_EXPR
3226 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3227 || (TREE_CODE (op1) == INTEGER_CST
3228 && ! integer_all_onesp (op1)));
3235 case BIT_ANDTC_EXPR:
3238 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3242 case TRUNC_MOD_EXPR:
3243 case FLOOR_MOD_EXPR:
3244 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3245 warning ("division by zero in `%E %% 0'", op0);
3246 else if (code1 == REAL_TYPE && real_zerop (op1))
3247 warning ("division by zero in `%E %% 0.'", op0);
3249 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3251 /* Although it would be tempting to shorten always here, that loses
3252 on some targets, since the modulo instruction is undefined if the
3253 quotient can't be represented in the computation mode. We shorten
3254 only if unsigned or if dividing by something we know != -1. */
3255 shorten = ((TREE_CODE (op0) == NOP_EXPR
3256 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3257 || (TREE_CODE (op1) == INTEGER_CST
3258 && ! integer_all_onesp (op1)));
3263 case TRUTH_ANDIF_EXPR:
3264 case TRUTH_ORIF_EXPR:
3265 case TRUTH_AND_EXPR:
3267 result_type = boolean_type_node;
3270 /* Shift operations: result has same type as first operand;
3271 always convert second operand to int.
3272 Also set SHORT_SHIFT if shifting rightward. */
3275 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3277 result_type = type0;
3278 if (TREE_CODE (op1) == INTEGER_CST)
3280 if (tree_int_cst_lt (op1, integer_zero_node))
3281 warning ("right shift count is negative");
3284 if (! integer_zerop (op1))
3286 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3287 warning ("right shift count >= width of type");
3290 /* Convert the shift-count to an integer, regardless of
3291 size of value being shifted. */
3292 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3293 op1 = cp_convert (integer_type_node, op1);
3294 /* Avoid converting op1 to result_type later. */
3300 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3302 result_type = type0;
3303 if (TREE_CODE (op1) == INTEGER_CST)
3305 if (tree_int_cst_lt (op1, integer_zero_node))
3306 warning ("left shift count is negative");
3307 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3308 warning ("left shift count >= width of type");
3310 /* Convert the shift-count to an integer, regardless of
3311 size of value being shifted. */
3312 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3313 op1 = cp_convert (integer_type_node, op1);
3314 /* Avoid converting op1 to result_type later. */
3321 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3323 result_type = type0;
3324 if (TREE_CODE (op1) == INTEGER_CST)
3326 if (tree_int_cst_lt (op1, integer_zero_node))
3327 warning ("%s rotate count is negative",
3328 (code == LROTATE_EXPR) ? "left" : "right");
3329 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3330 warning ("%s rotate count >= width of type",
3331 (code == LROTATE_EXPR) ? "left" : "right");
3333 /* Convert the shift-count to an integer, regardless of
3334 size of value being shifted. */
3335 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3336 op1 = cp_convert (integer_type_node, op1);
3342 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3343 warning ("comparing floating point with == or != is unsafe");
3345 build_type = boolean_type_node;
3346 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3347 || code0 == COMPLEX_TYPE)
3348 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3349 || code1 == COMPLEX_TYPE))
3351 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3352 result_type = composite_pointer_type (type0, type1, op0, op1,
3354 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3355 result_type = type0;
3356 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3357 result_type = type1;
3358 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3360 result_type = type0;
3361 error ("ISO C++ forbids comparison between pointer and integer");
3363 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3365 result_type = type1;
3366 error ("ISO C++ forbids comparison between pointer and integer");
3368 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3370 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3371 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3372 result_type = TREE_TYPE (op0);
3374 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3375 return cp_build_binary_op (code, op1, op0);
3376 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3377 && same_type_p (type0, type1))
3379 /* E will be the final comparison. */
3381 /* E1 and E2 are for scratch. */
3389 if (TREE_SIDE_EFFECTS (op0))
3390 op0 = save_expr (op0);
3391 if (TREE_SIDE_EFFECTS (op1))
3392 op1 = save_expr (op1);
3397 && (!op0.pfn || op0.delta == op1.delta))
3399 The reason for the `!op0.pfn' bit is that a NULL
3400 pointer-to-member is any member with a zero PFN; the
3401 DELTA field is unspecified. */
3402 pfn0 = pfn_from_ptrmemfunc (op0);
3403 pfn1 = pfn_from_ptrmemfunc (op1);
3404 delta0 = build_ptrmemfunc_access_expr (op0,
3406 delta1 = build_ptrmemfunc_access_expr (op1,
3408 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3409 e2 = cp_build_binary_op (EQ_EXPR,
3411 cp_convert (TREE_TYPE (pfn0),
3412 integer_zero_node));
3413 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3414 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3415 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3416 if (code == EQ_EXPR)
3418 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3420 else if ((TYPE_PTRMEMFUNC_P (type0)
3421 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3422 || (TYPE_PTRMEMFUNC_P (type1)
3423 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3429 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3430 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3432 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3433 result_type = composite_pointer_type (type0, type1, op0, op1,
3441 build_type = boolean_type_node;
3442 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3443 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3445 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3446 result_type = composite_pointer_type (type0, type1, op0, op1,
3448 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3449 && integer_zerop (op1))
3450 result_type = type0;
3451 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3452 && integer_zerop (op0))
3453 result_type = type1;
3454 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3456 result_type = type0;
3457 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3459 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3461 result_type = type1;
3462 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3466 case UNORDERED_EXPR:
3473 build_type = integer_type_node;
3474 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3476 error ("unordered comparison on non-floating point argument");
3477 return error_mark_node;
3486 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3488 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3490 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3492 if (shorten || common || short_compare)
3493 result_type = common_type (type0, type1);
3495 /* For certain operations (which identify themselves by shorten != 0)
3496 if both args were extended from the same smaller type,
3497 do the arithmetic in that type and then extend.
3499 shorten !=0 and !=1 indicates a bitwise operation.
3500 For them, this optimization is safe only if
3501 both args are zero-extended or both are sign-extended.
3502 Otherwise, we might change the result.
3503 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3504 but calculated in (unsigned short) it would be (unsigned short)-1. */
3506 if (shorten && none_complex)
3508 int unsigned0, unsigned1;
3509 tree arg0 = get_narrower (op0, &unsigned0);
3510 tree arg1 = get_narrower (op1, &unsigned1);
3511 /* UNS is 1 if the operation to be done is an unsigned one. */
3512 int uns = TREE_UNSIGNED (result_type);
3515 final_type = result_type;
3517 /* Handle the case that OP0 does not *contain* a conversion
3518 but it *requires* conversion to FINAL_TYPE. */
3520 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3521 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3522 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3523 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3525 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3527 /* For bitwise operations, signedness of nominal type
3528 does not matter. Consider only how operands were extended. */
3532 /* Note that in all three cases below we refrain from optimizing
3533 an unsigned operation on sign-extended args.
3534 That would not be valid. */
3536 /* Both args variable: if both extended in same way
3537 from same width, do it in that width.
3538 Do it unsigned if args were zero-extended. */
3539 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3540 < TYPE_PRECISION (result_type))
3541 && (TYPE_PRECISION (TREE_TYPE (arg1))
3542 == TYPE_PRECISION (TREE_TYPE (arg0)))
3543 && unsigned0 == unsigned1
3544 && (unsigned0 || !uns))
3545 result_type = c_common_signed_or_unsigned_type
3546 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3547 else if (TREE_CODE (arg0) == INTEGER_CST
3548 && (unsigned1 || !uns)
3549 && (TYPE_PRECISION (TREE_TYPE (arg1))
3550 < TYPE_PRECISION (result_type))
3551 && (type = c_common_signed_or_unsigned_type
3552 (unsigned1, TREE_TYPE (arg1)),
3553 int_fits_type_p (arg0, type)))
3555 else if (TREE_CODE (arg1) == INTEGER_CST
3556 && (unsigned0 || !uns)
3557 && (TYPE_PRECISION (TREE_TYPE (arg0))
3558 < TYPE_PRECISION (result_type))
3559 && (type = c_common_signed_or_unsigned_type
3560 (unsigned0, TREE_TYPE (arg0)),
3561 int_fits_type_p (arg1, type)))
3565 /* Shifts can be shortened if shifting right. */
3570 tree arg0 = get_narrower (op0, &unsigned_arg);
3572 final_type = result_type;
3574 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3575 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3577 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3578 /* We can shorten only if the shift count is less than the
3579 number of bits in the smaller type size. */
3580 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3581 /* If arg is sign-extended and then unsigned-shifted,
3582 we can simulate this with a signed shift in arg's type
3583 only if the extended result is at least twice as wide
3584 as the arg. Otherwise, the shift could use up all the
3585 ones made by sign-extension and bring in zeros.
3586 We can't optimize that case at all, but in most machines
3587 it never happens because available widths are 2**N. */
3588 && (!TREE_UNSIGNED (final_type)
3590 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3591 <= TYPE_PRECISION (result_type))))
3593 /* Do an unsigned shift if the operand was zero-extended. */
3595 = c_common_signed_or_unsigned_type (unsigned_arg,
3597 /* Convert value-to-be-shifted to that type. */
3598 if (TREE_TYPE (op0) != result_type)
3599 op0 = cp_convert (result_type, op0);
3604 /* Comparison operations are shortened too but differently.
3605 They identify themselves by setting short_compare = 1. */
3609 /* Don't write &op0, etc., because that would prevent op0
3610 from being kept in a register.
3611 Instead, make copies of the our local variables and
3612 pass the copies by reference, then copy them back afterward. */
3613 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3614 enum tree_code xresultcode = resultcode;
3616 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3618 return cp_convert (boolean_type_node, val);
3619 op0 = xop0, op1 = xop1;
3621 resultcode = xresultcode;
3624 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3625 && warn_sign_compare)
3627 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3628 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3630 int unsignedp0, unsignedp1;
3631 tree primop0 = get_narrower (op0, &unsignedp0);
3632 tree primop1 = get_narrower (op1, &unsignedp1);
3634 /* Check for comparison of different enum types. */
3635 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3636 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3637 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3638 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3640 warning ("comparison between types `%#T' and `%#T'",
3641 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3644 /* Give warnings for comparisons between signed and unsigned
3645 quantities that may fail. */
3646 /* Do the checking based on the original operand trees, so that
3647 casts will be considered, but default promotions won't be. */
3649 /* Do not warn if the comparison is being done in a signed type,
3650 since the signed type will only be chosen if it can represent
3651 all the values of the unsigned type. */
3652 if (! TREE_UNSIGNED (result_type))
3654 /* Do not warn if both operands are unsigned. */
3655 else if (op0_signed == op1_signed)
3657 /* Do not warn if the signed quantity is an unsuffixed
3658 integer literal (or some static constant expression
3659 involving such literals or a conditional expression
3660 involving such literals) and it is non-negative. */
3661 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3662 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3664 /* Do not warn if the comparison is an equality operation,
3665 the unsigned quantity is an integral constant and it does
3666 not use the most significant bit of result_type. */
3667 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3668 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3669 && int_fits_type_p (orig_op1, c_common_signed_type
3671 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3672 && int_fits_type_p (orig_op0, c_common_signed_type
3676 warning ("comparison between signed and unsigned integer expressions");
3678 /* Warn if two unsigned values are being compared in a size
3679 larger than their original size, and one (and only one) is the
3680 result of a `~' operator. This comparison will always fail.
3682 Also warn if one operand is a constant, and the constant does not
3683 have all bits set that are set in the ~ operand when it is
3686 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3687 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3689 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3690 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3691 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3692 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3694 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3697 HOST_WIDE_INT constant, mask;
3701 if (host_integerp (primop0, 0))
3704 unsignedp = unsignedp1;
3705 constant = tree_low_cst (primop0, 0);
3710 unsignedp = unsignedp0;
3711 constant = tree_low_cst (primop1, 0);
3714 bits = TYPE_PRECISION (TREE_TYPE (primop));
3715 if (bits < TYPE_PRECISION (result_type)
3716 && bits < HOST_BITS_PER_LONG && unsignedp)
3718 mask = (~ (HOST_WIDE_INT) 0) << bits;
3719 if ((mask & constant) != mask)
3720 warning ("comparison of promoted ~unsigned with constant");
3723 else if (unsignedp0 && unsignedp1
3724 && (TYPE_PRECISION (TREE_TYPE (primop0))
3725 < TYPE_PRECISION (result_type))
3726 && (TYPE_PRECISION (TREE_TYPE (primop1))
3727 < TYPE_PRECISION (result_type)))
3728 warning ("comparison of promoted ~unsigned with unsigned");
3733 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3734 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3735 Then the expression will be built.
3736 It will be given type FINAL_TYPE if that is nonzero;
3737 otherwise, it will be given type RESULT_TYPE. */
3741 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3742 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3743 return error_mark_node;
3746 /* Issue warnings about peculiar, but valid, uses of NULL. */
3747 if (/* It's reasonable to use pointer values as operands of &&
3748 and ||, so NULL is no exception. */
3749 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3750 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3751 (orig_op0 == null_node
3752 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3753 /* Or vice versa. */
3754 || (orig_op1 == null_node
3755 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3756 /* Or, both are NULL and the operation was not a comparison. */
3757 || (orig_op0 == null_node && orig_op1 == null_node
3758 && code != EQ_EXPR && code != NE_EXPR)))
3759 /* Some sort of arithmetic operation involving NULL was
3760 performed. Note that pointer-difference and pointer-addition
3761 have already been handled above, and so we don't end up here in
3763 warning ("NULL used in arithmetic");
3767 if (TREE_TYPE (op0) != result_type)
3768 op0 = cp_convert (result_type, op0);
3769 if (TREE_TYPE (op1) != result_type)
3770 op1 = cp_convert (result_type, op1);
3772 if (op0 == error_mark_node || op1 == error_mark_node)
3773 return error_mark_node;
3776 if (build_type == NULL_TREE)
3777 build_type = result_type;
3780 register tree result = build (resultcode, build_type, op0, op1);
3781 register tree folded;
3783 folded = fold (result);
3784 if (folded == result)
3785 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3786 if (final_type != 0)
3787 return cp_convert (final_type, folded);
3792 /* Return a tree for the sum or difference (RESULTCODE says which)
3793 of pointer PTROP and integer INTOP. */
3796 cp_pointer_int_sum (resultcode, ptrop, intop)
3797 enum tree_code resultcode;
3798 register tree ptrop, intop;
3800 tree res_type = TREE_TYPE (ptrop);
3802 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3803 in certain circumstance (when it's valid to do so). So we need
3804 to make sure it's complete. We don't need to check here, if we
3805 can actually complete it at all, as those checks will be done in
3806 pointer_int_sum() anyway. */
3807 complete_type (TREE_TYPE (res_type));
3809 return pointer_int_sum (resultcode, ptrop, fold (intop));
3812 /* Return a tree for the difference of pointers OP0 and OP1.
3813 The resulting tree has type int. */
3816 pointer_diff (op0, op1, ptrtype)
3817 register tree op0, op1;
3818 register tree ptrtype;
3820 register tree result, folded;
3821 tree restype = ptrdiff_type_node;
3822 tree target_type = TREE_TYPE (ptrtype);
3824 if (!complete_type_or_else (target_type, NULL_TREE))
3825 return error_mark_node;
3827 if (pedantic || warn_pointer_arith)
3829 if (TREE_CODE (target_type) == VOID_TYPE)
3830 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
3831 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3832 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3833 if (TREE_CODE (target_type) == METHOD_TYPE)
3834 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3835 if (TREE_CODE (target_type) == OFFSET_TYPE)
3836 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
3839 /* First do the subtraction as integers;
3840 then drop through to build the divide operator. */
3842 op0 = cp_build_binary_op (MINUS_EXPR,
3843 cp_convert (restype, op0),
3844 cp_convert (restype, op1));
3846 /* This generates an error if op1 is a pointer to an incomplete type. */
3847 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3848 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3850 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3851 || TREE_CODE (target_type) == FUNCTION_TYPE
3852 || TREE_CODE (target_type) == METHOD_TYPE
3853 || TREE_CODE (target_type) == OFFSET_TYPE)
3855 : size_in_bytes (target_type));
3857 /* Do the division. */
3859 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3861 folded = fold (result);
3862 if (folded == result)
3863 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3867 /* Construct and perhaps optimize a tree representation
3868 for a unary operation. CODE, a tree_code, specifies the operation
3869 and XARG is the operand. */
3872 build_x_unary_op (code, xarg)
3873 enum tree_code code;
3879 if (processing_template_decl)
3880 return build_min_nt (code, xarg, NULL_TREE);
3882 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3884 if (code == ADDR_EXPR
3885 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3886 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
3887 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
3888 || (TREE_CODE (xarg) == OFFSET_REF)))
3889 /* don't look for a function */;
3894 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
3895 NULL_TREE, NULL_TREE);
3896 if (rval || code != ADDR_EXPR)
3899 if (code == ADDR_EXPR)
3901 /* A pointer to member-function can be formed only by saying
3903 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
3904 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
3906 if (TREE_CODE (xarg) != OFFSET_REF)
3908 error ("invalid use of '%E' to form a pointer-to-member-function. Use a qualified-id.",
3910 return error_mark_node;
3914 error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function",
3916 PTRMEM_OK_P (xarg) = 1;
3920 if (TREE_CODE (xarg) == OFFSET_REF)
3922 ptrmem = PTRMEM_OK_P (xarg);
3924 if (!ptrmem && !flag_ms_extensions
3925 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
3927 /* A single non-static member, make sure we don't allow a
3928 pointer-to-member. */
3929 xarg = build (OFFSET_REF, TREE_TYPE (xarg),
3930 TREE_OPERAND (xarg, 0),
3931 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
3932 PTRMEM_OK_P (xarg) = ptrmem;
3936 else if (TREE_CODE (xarg) == TARGET_EXPR)
3937 warning ("taking address of temporary");
3939 exp = build_unary_op (code, xarg, 0);
3940 if (TREE_CODE (exp) == ADDR_EXPR)
3941 PTRMEM_OK_P (exp) = ptrmem;
3946 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
3947 constants, where a null value is represented by an INTEGER_CST of
3951 cp_truthvalue_conversion (expr)
3954 tree type = TREE_TYPE (expr);
3955 if (TYPE_PTRMEM_P (type))
3956 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3958 return c_common_truthvalue_conversion (expr);
3961 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3964 condition_conversion (expr)
3968 if (processing_template_decl)
3970 if (TREE_CODE (expr) == OFFSET_REF)
3971 expr = resolve_offset_ref (expr);
3972 t = perform_implicit_conversion (boolean_type_node, expr);
3973 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
3977 /* Return an ADDR_EXPR giving the address of T. This function
3978 attempts no optimizations or simplifications; it is a low-level
3982 build_address (tree t)
3986 if (error_operand_p (t) || !cxx_mark_addressable (t))
3987 return error_mark_node;
3989 addr = build1 (ADDR_EXPR,
3990 build_pointer_type (TREE_TYPE (t)),
3993 TREE_CONSTANT (addr) = 1;
3998 /* Return a NOP_EXPR converting EXPR to TYPE. */
4001 build_nop (tree type, tree expr)
4005 if (type == error_mark_node || error_operand_p (expr))
4008 nop = build1 (NOP_EXPR, type, expr);
4009 if (TREE_CONSTANT (expr))
4010 TREE_CONSTANT (nop) = 1;
4015 /* C++: Must handle pointers to members.
4017 Perhaps type instantiation should be extended to handle conversion
4018 from aggregates to types we don't yet know we want? (Or are those
4019 cases typically errors which should be reported?)
4021 NOCONVERT nonzero suppresses the default promotions
4022 (such as from short to int). */
4025 build_unary_op (enum tree_code code, tree xarg, int noconvert)
4027 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4028 register tree arg = xarg;
4029 register tree argtype = 0;
4030 const char *errstring = NULL;
4033 if (arg == error_mark_node)
4034 return error_mark_node;
4039 /* This is used for unary plus, because a CONVERT_EXPR
4040 is enough to prevent anybody from looking inside for
4041 associativity, but won't generate any code. */
4042 if (!(arg = build_expr_type_conversion
4043 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, true)))
4044 errstring = "wrong type argument to unary plus";
4048 arg = default_conversion (arg);
4049 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4050 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4055 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4056 errstring = "wrong type argument to unary minus";
4057 else if (!noconvert)
4058 arg = default_conversion (arg);
4062 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4066 arg = default_conversion (arg);
4068 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4070 errstring = "wrong type argument to bit-complement";
4071 else if (!noconvert)
4072 arg = default_conversion (arg);
4076 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4077 errstring = "wrong type argument to abs";
4078 else if (!noconvert)
4079 arg = default_conversion (arg);
4083 /* Conjugating a real value is a no-op, but allow it anyway. */
4084 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4085 errstring = "wrong type argument to conjugation";
4086 else if (!noconvert)
4087 arg = default_conversion (arg);
4090 case TRUTH_NOT_EXPR:
4091 arg = cp_convert (boolean_type_node, arg);
4092 val = invert_truthvalue (arg);
4093 if (arg != error_mark_node)
4095 errstring = "in argument to unary !";
4102 if (TREE_CODE (arg) == COMPLEX_CST)
4103 return TREE_REALPART (arg);
4104 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4105 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4110 if (TREE_CODE (arg) == COMPLEX_CST)
4111 return TREE_IMAGPART (arg);
4112 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4113 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4115 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4117 case PREINCREMENT_EXPR:
4118 case POSTINCREMENT_EXPR:
4119 case PREDECREMENT_EXPR:
4120 case POSTDECREMENT_EXPR:
4121 /* Handle complex lvalues (when permitted)
4122 by reduction to simpler cases. */
4124 val = unary_complex_lvalue (code, arg);
4128 /* Increment or decrement the real part of the value,
4129 and don't change the imaginary part. */
4130 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4134 arg = stabilize_reference (arg);
4135 real = build_unary_op (REALPART_EXPR, arg, 1);
4136 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4137 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4138 build_unary_op (code, real, 1), imag);
4141 /* Report invalid types. */
4143 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4146 if (code == PREINCREMENT_EXPR)
4147 errstring ="no pre-increment operator for type";
4148 else if (code == POSTINCREMENT_EXPR)
4149 errstring ="no post-increment operator for type";
4150 else if (code == PREDECREMENT_EXPR)
4151 errstring ="no pre-decrement operator for type";
4153 errstring ="no post-decrement operator for type";
4157 /* Report something read-only. */
4159 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4160 || TREE_READONLY (arg))
4161 readonly_error (arg, ((code == PREINCREMENT_EXPR
4162 || code == POSTINCREMENT_EXPR)
4163 ? "increment" : "decrement"),
4168 tree result_type = TREE_TYPE (arg);
4170 arg = get_unwidened (arg, 0);
4171 argtype = TREE_TYPE (arg);
4173 /* ARM $5.2.5 last annotation says this should be forbidden. */
4174 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4175 pedwarn ("ISO C++ forbids %sing an enum",
4176 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4177 ? "increment" : "decrement");
4179 /* Compute the increment. */
4181 if (TREE_CODE (argtype) == POINTER_TYPE)
4183 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4184 tree type = complete_type (TREE_TYPE (argtype));
4186 if (!COMPLETE_OR_VOID_TYPE_P (type))
4187 error ("cannot %s a pointer to incomplete type `%T'",
4188 ((code == PREINCREMENT_EXPR
4189 || code == POSTINCREMENT_EXPR)
4190 ? "increment" : "decrement"), TREE_TYPE (argtype));
4191 else if ((pedantic || warn_pointer_arith)
4192 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4193 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4194 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4195 ((code == PREINCREMENT_EXPR
4196 || code == POSTINCREMENT_EXPR)
4197 ? "increment" : "decrement"), argtype);
4198 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
4201 inc = integer_one_node;
4203 inc = cp_convert (argtype, inc);
4205 /* Handle incrementing a cast-expression. */
4207 switch (TREE_CODE (arg))
4212 case FIX_TRUNC_EXPR:
4213 case FIX_FLOOR_EXPR:
4214 case FIX_ROUND_EXPR:
4217 tree incremented, modify, value, compound;
4218 if (! lvalue_p (arg) && pedantic)
4219 pedwarn ("cast to non-reference type used as lvalue");
4220 arg = stabilize_reference (arg);
4221 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4224 value = save_expr (arg);
4225 incremented = build (((code == PREINCREMENT_EXPR
4226 || code == POSTINCREMENT_EXPR)
4227 ? PLUS_EXPR : MINUS_EXPR),
4228 argtype, value, inc);
4230 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4231 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4233 /* Eliminate warning about unused result of + or -. */
4234 TREE_NO_UNUSED_WARNING (compound) = 1;
4242 /* Complain about anything else that is not a true lvalue. */
4243 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4244 || code == POSTINCREMENT_EXPR)
4245 ? "increment" : "decrement")))
4246 return error_mark_node;
4248 /* Forbid using -- on `bool'. */
4249 if (TREE_TYPE (arg) == boolean_type_node)
4251 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4253 error ("invalid use of `--' on bool variable `%D'", arg);
4254 return error_mark_node;
4257 /* This will only work if someone can convince Kenner to accept
4258 my patch to expand_increment. (jason) */
4259 val = build (code, TREE_TYPE (arg), arg, inc);
4261 val = boolean_increment (code, arg);
4265 val = build (code, TREE_TYPE (arg), arg, inc);
4267 TREE_SIDE_EFFECTS (val) = 1;
4268 return cp_convert (result_type, val);
4272 /* Note that this operation never does default_conversion
4273 regardless of NOCONVERT. */
4275 argtype = lvalue_type (arg);
4276 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4280 build_pointer_type (TREE_TYPE (argtype)), arg);
4281 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4284 else if (pedantic && DECL_MAIN_P (arg))
4286 pedwarn ("ISO C++ forbids taking address of function `::main'");
4288 /* Let &* cancel out to simplify resulting code. */
4289 if (TREE_CODE (arg) == INDIRECT_REF)
4291 /* We don't need to have `current_class_ptr' wrapped in a
4292 NON_LVALUE_EXPR node. */
4293 if (arg == current_class_ref)
4294 return current_class_ptr;
4296 arg = TREE_OPERAND (arg, 0);
4297 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4301 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4302 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4304 else if (lvalue_p (arg))
4305 /* Don't let this be an lvalue. */
4306 return non_lvalue (arg);
4310 /* For &x[y], return x+y */
4311 if (TREE_CODE (arg) == ARRAY_REF)
4313 if (!cxx_mark_addressable (TREE_OPERAND (arg, 0)))
4314 return error_mark_node;
4315 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4316 TREE_OPERAND (arg, 1));
4319 /* Uninstantiated types are all functions. Taking the
4320 address of a function is a no-op, so just return the
4323 if (TREE_CODE (arg) == IDENTIFIER_NODE
4324 && IDENTIFIER_OPNAME_P (arg))
4327 /* We don't know the type yet, so just work around the problem.
4328 We know that this will resolve to an lvalue. */
4329 return build1 (ADDR_EXPR, unknown_type_node, arg);
4332 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4333 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4335 /* They're trying to take the address of a unique non-static
4336 member function. This is ill-formed (except in MS-land),
4337 but let's try to DTRT.
4338 Note: We only handle unique functions here because we don't
4339 want to complain if there's a static overload; non-unique
4340 cases will be handled by instantiate_type. But we need to
4341 handle this case here to allow casts on the resulting PMF.
4342 We could defer this in non-MS mode, but it's easier to give
4343 a useful error here. */
4345 /* Inside constant member functions, the `this' pointer
4346 contains an extra const qualifier. TYPE_MAIN_VARIANT
4347 is used here to remove this const from the diagnostics
4348 and the created OFFSET_REF. */
4349 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4350 tree name = DECL_NAME (get_first_fn (TREE_OPERAND (arg, 1)));
4352 if (! flag_ms_extensions)
4354 if (current_class_type
4355 && TREE_OPERAND (arg, 0) == current_class_ref)
4356 /* An expression like &memfn. */
4357 pedwarn ("ISO C++ forbids taking the address of an unqualified"
4358 " or parenthesized non-static member function to form"
4359 " a pointer to member function. Say `&%T::%D'",
4362 pedwarn ("ISO C++ forbids taking the address of a bound member"
4363 " function to form a pointer to member function."
4367 arg = build_offset_ref (base, name);
4370 if (type_unknown_p (arg))
4371 return build1 (ADDR_EXPR, unknown_type_node, arg);
4373 /* Handle complex lvalues (when permitted)
4374 by reduction to simpler cases. */
4375 val = unary_complex_lvalue (code, arg);
4379 switch (TREE_CODE (arg))
4384 case FIX_TRUNC_EXPR:
4385 case FIX_FLOOR_EXPR:
4386 case FIX_ROUND_EXPR:
4388 if (! lvalue_p (arg) && pedantic)
4389 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4396 /* Allow the address of a constructor if all the elements
4398 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4399 && TREE_CONSTANT (arg))
4401 /* Anything not already handled and not a true memory reference
4403 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4404 && TREE_CODE (argtype) != METHOD_TYPE
4405 && !non_cast_lvalue_or_else (arg, "unary `&'"))
4406 return error_mark_node;
4408 if (argtype != error_mark_node)
4409 argtype = build_pointer_type (argtype);
4414 if (TREE_CODE (arg) == COMPONENT_REF
4415 && TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
4416 arg = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4418 if (TREE_CODE (arg) != COMPONENT_REF)
4419 addr = build_address (arg);
4420 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4422 error ("attempt to take address of bit-field structure member `%D'",
4423 TREE_OPERAND (arg, 1));
4424 return error_mark_node;
4428 /* Unfortunately we cannot just build an address
4429 expression here, because we would not handle
4430 address-constant-expressions or offsetof correctly. */
4431 tree field = TREE_OPERAND (arg, 1);
4432 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4433 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)),
4434 decl_type_context (field),
4437 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
4438 rval = build1 (NOP_EXPR, argtype, rval);
4439 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4440 addr = fold (build (PLUS_EXPR, argtype, rval,
4441 cp_convert (argtype, byte_position (field))));
4444 if (TREE_CODE (argtype) == POINTER_TYPE
4445 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4447 build_ptrmemfunc_type (argtype);
4448 addr = build_ptrmemfunc (argtype, addr, 0);
4461 argtype = TREE_TYPE (arg);
4462 return fold (build1 (code, argtype, arg));
4465 error ("%s", errstring);
4466 return error_mark_node;
4469 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4470 for certain kinds of expressions which are not really lvalues
4471 but which we can accept as lvalues.
4473 If ARG is not a kind of expression we can handle, return zero. */
4476 unary_complex_lvalue (enum tree_code code, tree arg)
4478 /* Handle (a, b) used as an "lvalue". */
4479 if (TREE_CODE (arg) == COMPOUND_EXPR)
4481 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4482 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4483 TREE_OPERAND (arg, 0), real_result);
4486 /* Handle (a ? b : c) used as an "lvalue". */
4487 if (TREE_CODE (arg) == COND_EXPR
4488 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4489 return rationalize_conditional_expr (code, arg);
4491 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4492 if (TREE_CODE (arg) == MODIFY_EXPR
4493 || TREE_CODE (arg) == PREINCREMENT_EXPR
4494 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4496 tree lvalue = TREE_OPERAND (arg, 0);
4497 if (TREE_SIDE_EFFECTS (lvalue))
4499 lvalue = stabilize_reference (lvalue);
4500 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4501 lvalue, TREE_OPERAND (arg, 1));
4503 return unary_complex_lvalue
4504 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4507 if (code != ADDR_EXPR)
4510 /* Handle (a = b) used as an "lvalue" for `&'. */
4511 if (TREE_CODE (arg) == MODIFY_EXPR
4512 || TREE_CODE (arg) == INIT_EXPR)
4514 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4515 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4516 TREE_NO_UNUSED_WARNING (arg) = 1;
4520 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4521 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4522 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4524 /* The representation of something of type OFFSET_TYPE
4525 is really the representation of a pointer to it.
4526 Here give the representation its true type. */
4529 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4531 if (TREE_CODE (arg) != OFFSET_REF)
4534 t = TREE_OPERAND (arg, 1);
4536 /* Check all this code for right semantics. */
4537 if (TREE_CODE (t) == FUNCTION_DECL)
4539 if (DECL_DESTRUCTOR_P (t))
4540 error ("taking address of destructor");
4541 return build_unary_op (ADDR_EXPR, t, 0);
4543 if (TREE_CODE (t) == VAR_DECL)
4544 return build_unary_op (ADDR_EXPR, t, 0);
4549 if (TREE_OPERAND (arg, 0)
4550 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4551 && TREE_CODE (t) != FIELD_DECL)
4553 error ("taking address of bound pointer-to-member expression");
4554 return error_mark_node;
4556 if (!PTRMEM_OK_P (arg))
4558 /* This cannot form a pointer to method, so we must
4559 resolve the offset ref, and take the address of the
4560 result. For instance,
4562 arg = resolve_offset_ref (arg);
4564 return build_unary_op (code, arg, 0);
4567 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4569 error ("cannot create pointer to reference member `%D'", t);
4570 return error_mark_node;
4573 type = build_ptrmem_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4574 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4580 /* We permit compiler to make function calls returning
4581 objects of aggregate type look like lvalues. */
4585 if (TREE_CODE (targ) == SAVE_EXPR)
4586 targ = TREE_OPERAND (targ, 0);
4588 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4590 if (TREE_CODE (arg) == SAVE_EXPR)
4593 targ = build_cplus_new (TREE_TYPE (arg), arg);
4594 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4597 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4598 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4599 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4602 /* Don't let anything else be handled specially. */
4606 /* Mark EXP saying that we need to be able to take the
4607 address of it; it should not be allocated in a register.
4608 Value is true if successful.
4610 C++: we do not allow `current_class_ptr' to be addressable. */
4613 cxx_mark_addressable (tree exp)
4615 register tree x = exp;
4618 switch (TREE_CODE (x))
4625 x = TREE_OPERAND (x, 0);
4629 if (x == current_class_ptr)
4631 error ("cannot take the address of `this', which is an rvalue expression");
4632 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4638 /* Caller should not be trying to mark initialized
4639 constant fields addressable. */
4640 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4641 || DECL_IN_AGGR_P (x) == 0
4643 || DECL_EXTERNAL (x), 314);
4648 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4649 && !DECL_ARTIFICIAL (x) && extra_warnings)
4650 warning ("address requested for `%D', which is declared `register'",
4652 TREE_ADDRESSABLE (x) = 1;
4653 put_var_into_stack (x, /*rescan=*/true);
4657 TREE_ADDRESSABLE (x) = 1;
4658 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4662 TREE_ADDRESSABLE (x) = 1;
4666 TREE_ADDRESSABLE (x) = 1;
4667 cxx_mark_addressable (TREE_OPERAND (x, 0));
4675 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4678 build_x_conditional_expr (tree ifexp, tree op1, tree op2)
4680 if (processing_template_decl)
4681 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4683 return build_conditional_expr (ifexp, op1, op2);
4686 /* Handle overloading of the ',' operator when needed. Otherwise,
4687 this function just builds an expression list. */
4690 build_x_compound_expr (tree list)
4692 tree rest = TREE_CHAIN (list);
4695 if (processing_template_decl)
4696 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
4698 if (rest == NULL_TREE)
4699 return build_compound_expr (list);
4701 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL,
4702 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4704 return build_x_compound_expr (tree_cons (NULL_TREE, result,
4705 TREE_CHAIN (rest)));
4707 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
4709 /* FIXME: This test should be in the implicit cast to void of the LHS. */
4710 /* the left-hand operand of a comma expression is like an expression
4711 statement: we should warn if it doesn't have any side-effects,
4712 unless it was explicitly cast to (void). */
4713 if (warn_unused_value
4714 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
4715 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
4716 warning("left-hand operand of comma expression has no effect");
4718 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4719 else if (warn_unused_value)
4720 warn_if_unused_value (TREE_VALUE(list));
4723 return build_compound_expr
4724 (tree_cons (NULL_TREE, TREE_VALUE (list),
4725 build_tree_list (NULL_TREE,
4726 build_x_compound_expr (rest))));
4729 /* Given a list of expressions, return a compound expression
4730 that performs them all and returns the value of the last of them. */
4733 build_compound_expr (tree list)
4738 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
4740 if (TREE_CHAIN (list) == 0)
4742 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4743 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4744 if (TREE_CODE (list) == NOP_EXPR
4745 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
4746 list = TREE_OPERAND (list, 0);
4748 return TREE_VALUE (list);
4751 first = TREE_VALUE (list);
4752 first = convert_to_void (first, "left-hand operand of comma");
4753 if (first == error_mark_node)
4754 return error_mark_node;
4756 rest = build_compound_expr (TREE_CHAIN (list));
4757 if (rest == error_mark_node)
4758 return error_mark_node;
4760 /* When pedantic, a compound expression cannot be a constant expression. */
4761 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
4764 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
4768 build_static_cast (tree type, tree expr)
4773 if (type == error_mark_node || expr == error_mark_node)
4774 return error_mark_node;
4776 if (TREE_CODE (expr) == OFFSET_REF)
4777 expr = resolve_offset_ref (expr);
4779 if (processing_template_decl)
4781 tree t = build_min (STATIC_CAST_EXPR, type, expr);
4785 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4786 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4787 if (TREE_CODE (type) != REFERENCE_TYPE
4788 && TREE_CODE (expr) == NOP_EXPR
4789 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4790 expr = TREE_OPERAND (expr, 0);
4792 if (TREE_CODE (type) == VOID_TYPE)
4794 expr = convert_to_void (expr, /*implicit=*/NULL);
4798 if (TREE_CODE (type) == REFERENCE_TYPE)
4799 return (convert_from_reference
4800 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
4801 LOOKUP_COMPLAIN, NULL_TREE)));
4803 if (IS_AGGR_TYPE (type))
4804 return build_cplus_new (type, (build_special_member_call
4805 (NULL_TREE, complete_ctor_identifier,
4806 build_tree_list (NULL_TREE, expr),
4807 TYPE_BINFO (type), LOOKUP_NORMAL)));
4809 intype = TREE_TYPE (expr);
4811 /* FIXME handle casting to array type. */
4814 if (IS_AGGR_TYPE (intype)
4815 ? can_convert_arg (type, intype, expr)
4816 : can_convert_arg (strip_all_pointer_quals (type),
4817 strip_all_pointer_quals (intype), expr))
4818 /* This is a standard conversion. */
4820 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
4822 /* They're pointers to objects. They must be aggregates that
4823 are related non-virtually. */
4826 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
4827 && lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
4828 ba_ignore | ba_quiet, &kind)
4829 && kind != bk_via_virtual)
4832 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4834 /* They're pointers to members. The pointed to objects must be
4835 the same (ignoring CV qualifiers), and the containing classes
4836 must be related non-virtually. */
4840 (strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (type))),
4841 strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (intype))))
4842 && (lookup_base (TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)),
4843 TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
4844 ba_ignore | ba_quiet, &kind))
4845 && kind != bk_via_virtual)
4848 else if (TREE_CODE (intype) != BOOLEAN_TYPE
4849 && TREE_CODE (type) != ARRAY_TYPE
4850 && TREE_CODE (type) != FUNCTION_TYPE
4851 && can_convert (intype, strip_all_pointer_quals (type)))
4853 else if (TREE_CODE (intype) == ENUMERAL_TYPE
4854 && TREE_CODE (type) == ENUMERAL_TYPE)
4855 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
4856 converted to an enumeration type."
4857 The integral to enumeration will be accepted by the previous clause.
4858 We need to explicitly check for enumeration to enumeration. */
4861 /* [expr.static.cast]
4863 The static_cast operator shall not be used to cast away
4865 if (ok && casts_away_constness (intype, type))
4867 error ("static_cast from type `%T' to type `%T' casts away constness",
4869 return error_mark_node;
4873 return build_c_cast (type, expr);
4875 error ("invalid static_cast from type `%T' to type `%T'", intype, type);
4876 return error_mark_node;
4880 build_reinterpret_cast (tree type, tree expr)
4884 if (type == error_mark_node || expr == error_mark_node)
4885 return error_mark_node;
4887 if (TREE_CODE (expr) == OFFSET_REF)
4888 expr = resolve_offset_ref (expr);
4890 if (processing_template_decl)
4892 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
4896 if (TREE_CODE (type) != REFERENCE_TYPE)
4898 expr = decay_conversion (expr);
4900 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4901 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4902 if (TREE_CODE (expr) == NOP_EXPR
4903 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4904 expr = TREE_OPERAND (expr, 0);
4907 intype = TREE_TYPE (expr);
4909 if (TREE_CODE (type) == REFERENCE_TYPE)
4911 if (! real_lvalue_p (expr))
4913 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
4914 return error_mark_node;
4916 expr = build_unary_op (ADDR_EXPR, expr, 0);
4917 if (expr != error_mark_node)
4918 expr = build_reinterpret_cast
4919 (build_pointer_type (TREE_TYPE (type)), expr);
4920 if (expr != error_mark_node)
4921 expr = build_indirect_ref (expr, 0);
4924 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
4925 return build_static_cast (type, expr);
4927 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
4928 || TREE_CODE (intype) == ENUMERAL_TYPE))
4930 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
4932 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
4933 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
4936 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
4937 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
4939 expr = decl_constant_value (expr);
4940 return fold (build1 (NOP_EXPR, type, expr));
4942 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4943 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
4945 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
4946 pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
4949 expr = decl_constant_value (expr);
4950 return fold (build1 (NOP_EXPR, type, expr));
4952 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
4953 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
4955 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
4956 expr = decl_constant_value (expr);
4957 return fold (build1 (NOP_EXPR, type, expr));
4961 error ("invalid reinterpret_cast from type `%T' to type `%T'",
4963 return error_mark_node;
4966 return cp_convert (type, expr);
4970 build_const_cast (tree type, tree expr)
4974 if (type == error_mark_node || expr == error_mark_node)
4975 return error_mark_node;
4977 if (TREE_CODE (expr) == OFFSET_REF)
4978 expr = resolve_offset_ref (expr);
4980 if (processing_template_decl)
4982 tree t = build_min (CONST_CAST_EXPR, type, expr);
4986 if (!POINTER_TYPE_P (type))
4987 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
4988 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4990 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
4991 return error_mark_node;
4994 if (TREE_CODE (type) != REFERENCE_TYPE)
4996 expr = decay_conversion (expr);
4998 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4999 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5000 if (TREE_CODE (expr) == NOP_EXPR
5001 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5002 expr = TREE_OPERAND (expr, 0);
5005 intype = TREE_TYPE (expr);
5007 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5008 return build_static_cast (type, expr);
5009 else if (TREE_CODE (type) == REFERENCE_TYPE)
5011 if (! real_lvalue_p (expr))
5013 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5014 return error_mark_node;
5017 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5019 expr = build_unary_op (ADDR_EXPR, expr, 0);
5020 expr = build1 (NOP_EXPR, type, expr);
5021 return convert_from_reference (expr);
5024 else if (TREE_CODE (type) == POINTER_TYPE
5025 && TREE_CODE (intype) == POINTER_TYPE
5026 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5027 return cp_convert (type, expr);
5029 error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5030 return error_mark_node;
5033 /* Build an expression representing a cast to type TYPE of expression EXPR.
5035 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5036 when doing the cast. */
5039 build_c_cast (tree type, tree expr)
5041 register tree value = expr;
5044 if (type == error_mark_node || expr == error_mark_node)
5045 return error_mark_node;
5047 if (processing_template_decl)
5049 tree t = build_min (CAST_EXPR, type,
5050 tree_cons (NULL_TREE, value, NULL_TREE));
5054 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5055 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5056 if (TREE_CODE (type) != REFERENCE_TYPE
5057 && TREE_CODE (value) == NOP_EXPR
5058 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5059 value = TREE_OPERAND (value, 0);
5061 if (TREE_CODE (value) == OFFSET_REF)
5062 value = resolve_offset_ref (value);
5064 if (TREE_CODE (type) == ARRAY_TYPE)
5066 /* Allow casting from T1* to T2[] because Cfront allows it.
5067 NIHCL uses it. It is not valid ISO C++ however. */
5068 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5070 pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5071 type = build_pointer_type (TREE_TYPE (type));
5075 error ("ISO C++ forbids casting to an array type `%T'", type);
5076 return error_mark_node;
5080 if (TREE_CODE (type) == FUNCTION_TYPE
5081 || TREE_CODE (type) == METHOD_TYPE)
5083 error ("invalid cast to function type `%T'", type);
5084 return error_mark_node;
5087 if (TREE_CODE (type) == VOID_TYPE)
5089 /* Conversion to void does not cause any of the normal function to
5090 * pointer, array to pointer and lvalue to rvalue decays. */
5092 value = convert_to_void (value, /*implicit=*/NULL);
5096 if (!complete_type_or_else (type, NULL_TREE))
5097 return error_mark_node;
5099 /* Convert functions and arrays to pointers and
5100 convert references to their expanded types,
5101 but don't convert any other types. If, however, we are
5102 casting to a class type, there's no reason to do this: the
5103 cast will only succeed if there is a converting constructor,
5104 and the default conversions will be done at that point. In
5105 fact, doing the default conversion here is actually harmful
5109 struct S { S(const A&); };
5111 since we don't want the array-to-pointer conversion done. */
5112 if (!IS_AGGR_TYPE (type))
5114 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5115 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5116 /* Don't do the default conversion on a ->* expression. */
5117 && ! (TREE_CODE (type) == POINTER_TYPE
5118 && bound_pmf_p (value)))
5119 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5120 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5121 value = default_conversion (value);
5123 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5124 /* However, even for class types, we still need to strip away
5125 the reference type, since the call to convert_force below
5126 does not expect the input expression to be of reference
5128 value = convert_from_reference (value);
5130 otype = TREE_TYPE (value);
5132 /* Optionally warn about potentially worrisome casts. */
5135 && TREE_CODE (type) == POINTER_TYPE
5136 && TREE_CODE (otype) == POINTER_TYPE
5137 && !at_least_as_qualified_p (TREE_TYPE (type),
5139 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5142 if (TREE_CODE (type) == INTEGER_TYPE
5143 && TREE_CODE (otype) == POINTER_TYPE
5144 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5145 warning ("cast from pointer to integer of different size");
5147 if (TREE_CODE (type) == POINTER_TYPE
5148 && TREE_CODE (otype) == INTEGER_TYPE
5149 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5150 /* Don't warn about converting any constant. */
5151 && !TREE_CONSTANT (value))
5152 warning ("cast to pointer from integer of different size");
5154 if (TREE_CODE (type) == REFERENCE_TYPE)
5155 value = (convert_from_reference
5156 (convert_to_reference (type, value, CONV_C_CAST,
5157 LOOKUP_COMPLAIN, NULL_TREE)));
5162 value = decl_constant_value (value);
5165 value = convert_force (type, value, CONV_C_CAST);
5167 /* Ignore any integer overflow caused by the cast. */
5168 if (TREE_CODE (value) == INTEGER_CST)
5170 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5171 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5175 /* Warn about possible alignment problems. Do this here when we will have
5176 instantiated any necessary template types. */
5177 if (STRICT_ALIGNMENT && warn_cast_align
5178 && TREE_CODE (type) == POINTER_TYPE
5179 && TREE_CODE (otype) == POINTER_TYPE
5180 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5181 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5182 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5183 && COMPLETE_TYPE_P (TREE_TYPE (type))
5184 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5185 warning ("cast from `%T' to `%T' increases required alignment of target type",
5188 /* Always produce some operator for an explicit cast,
5189 so we can tell (for -pedantic) that the cast is no lvalue. */
5190 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5191 && real_lvalue_p (value))
5192 value = non_lvalue (value);
5197 /* Build an assignment expression of lvalue LHS from value RHS.
5198 MODIFYCODE is the code for a binary operator that we use
5199 to combine the old value of LHS with RHS to get the new value.
5200 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5202 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5205 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5207 register tree result;
5209 tree lhstype = TREE_TYPE (lhs);
5210 tree olhstype = lhstype;
5213 /* Avoid duplicate error messages from operands that had errors. */
5214 if (lhs == error_mark_node || rhs == error_mark_node)
5215 return error_mark_node;
5217 /* Handle control structure constructs used as "lvalues". */
5218 switch (TREE_CODE (lhs))
5220 /* Handle --foo = 5; as these are valid constructs in C++ */
5221 case PREDECREMENT_EXPR:
5222 case PREINCREMENT_EXPR:
5223 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5224 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5225 stabilize_reference (TREE_OPERAND (lhs, 0)),
5226 TREE_OPERAND (lhs, 1));
5227 return build (COMPOUND_EXPR, lhstype,
5229 build_modify_expr (TREE_OPERAND (lhs, 0),
5232 /* Handle (a, b) used as an "lvalue". */
5234 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5236 if (newrhs == error_mark_node)
5237 return error_mark_node;
5238 return build (COMPOUND_EXPR, lhstype,
5239 TREE_OPERAND (lhs, 0), newrhs);
5242 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5243 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5244 stabilize_reference (TREE_OPERAND (lhs, 0)),
5245 TREE_OPERAND (lhs, 1));
5246 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5247 if (newrhs == error_mark_node)
5248 return error_mark_node;
5249 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5251 /* Handle (a ? b : c) used as an "lvalue". */
5254 /* Produce (a ? (b = rhs) : (c = rhs))
5255 except that the RHS goes through a save-expr
5256 so the code to compute it is only emitted once. */
5258 tree preeval = NULL_TREE;
5260 rhs = stabilize_expr (rhs, &preeval);
5262 /* Check this here to avoid odd errors when trying to convert
5263 a throw to the type of the COND_EXPR. */
5264 if (!lvalue_or_else (lhs, "assignment"))
5265 return error_mark_node;
5267 cond = build_conditional_expr
5268 (TREE_OPERAND (lhs, 0),
5269 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5270 TREE_OPERAND (lhs, 1)),
5272 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5273 TREE_OPERAND (lhs, 2)),
5276 if (cond == error_mark_node)
5278 /* Make sure the code to compute the rhs comes out
5279 before the split. */
5280 return build (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
5284 lhs = resolve_offset_ref (lhs);
5285 if (lhs == error_mark_node)
5286 return error_mark_node;
5287 olhstype = lhstype = TREE_TYPE (lhs);
5293 if (modifycode == INIT_EXPR)
5295 if (TREE_CODE (rhs) == CONSTRUCTOR)
5297 my_friendly_assert (same_type_p (TREE_TYPE (rhs), lhstype),
5299 result = build (INIT_EXPR, lhstype, lhs, rhs);
5300 TREE_SIDE_EFFECTS (result) = 1;
5303 else if (! IS_AGGR_TYPE (lhstype))
5304 /* Do the default thing */;
5307 result = build_special_member_call (lhs, complete_ctor_identifier,
5308 build_tree_list (NULL_TREE, rhs),
5309 TYPE_BINFO (lhstype),
5311 if (result == NULL_TREE)
5312 return error_mark_node;
5318 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5320 lhs = convert_from_reference (lhs);
5321 olhstype = lhstype = TREE_TYPE (lhs);
5323 lhs = require_complete_type (lhs);
5324 if (lhs == error_mark_node)
5325 return error_mark_node;
5327 if (modifycode == NOP_EXPR)
5329 /* `operator=' is not an inheritable operator. */
5330 if (! IS_AGGR_TYPE (lhstype))
5331 /* Do the default thing */;
5334 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
5335 lhs, rhs, make_node (NOP_EXPR));
5336 if (result == NULL_TREE)
5337 return error_mark_node;
5344 /* A binary op has been requested. Combine the old LHS
5345 value with the RHS producing the value we should actually
5346 store into the LHS. */
5348 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE),
5350 lhs = stabilize_reference (lhs);
5351 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5352 if (newrhs == error_mark_node)
5354 error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5355 TREE_TYPE (lhs), TREE_TYPE (rhs));
5356 return error_mark_node;
5359 /* Now it looks like a plain assignment. */
5360 modifycode = NOP_EXPR;
5362 my_friendly_assert (TREE_CODE (lhstype) != REFERENCE_TYPE, 20011220);
5363 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE,
5367 /* Handle a cast used as an "lvalue".
5368 We have already performed any binary operator using the value as cast.
5369 Now convert the result to the cast type of the lhs,
5370 and then true type of the lhs and store it there;
5371 then convert result back to the cast type to be the value
5372 of the assignment. */
5374 switch (TREE_CODE (lhs))
5379 case FIX_TRUNC_EXPR:
5380 case FIX_FLOOR_EXPR:
5381 case FIX_ROUND_EXPR:
5384 tree inner_lhs = TREE_OPERAND (lhs, 0);
5387 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5388 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5389 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5390 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5391 newrhs = default_conversion (newrhs);
5393 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5394 type, otherwise the result is an rvalue. */
5395 if (! lvalue_p (lhs))
5396 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5398 result = build_modify_expr (inner_lhs, NOP_EXPR,
5399 cp_convert (TREE_TYPE (inner_lhs),
5400 cp_convert (lhstype, newrhs)));
5401 if (result == error_mark_node)
5403 return cp_convert (TREE_TYPE (lhs), result);
5410 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5411 Reject anything strange now. */
5413 if (!lvalue_or_else (lhs, "assignment"))
5414 return error_mark_node;
5416 /* Warn about modifying something that is `const'. Don't warn if
5417 this is initialization. */
5418 if (modifycode != INIT_EXPR
5419 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5420 /* Functions are not modifiable, even though they are
5422 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5423 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5424 /* If it's an aggregate and any field is const, then it is
5425 effectively const. */
5426 || (CLASS_TYPE_P (lhstype)
5427 && C_TYPE_FIELDS_READONLY (lhstype))))
5428 readonly_error (lhs, "assignment", 0);
5430 /* If storing into a structure or union member, it has probably been
5431 given type `int'. Compute the type that would go with the actual
5432 amount of storage the member occupies. */
5434 if (TREE_CODE (lhs) == COMPONENT_REF
5435 && (TREE_CODE (lhstype) == INTEGER_TYPE
5436 || TREE_CODE (lhstype) == REAL_TYPE
5437 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5439 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5441 /* If storing in a field that is in actuality a short or narrower
5442 than one, we must store in the field in its actual type. */
5444 if (lhstype != TREE_TYPE (lhs))
5446 lhs = copy_node (lhs);
5447 TREE_TYPE (lhs) = lhstype;
5451 /* Convert new value to destination type. */
5453 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5457 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5458 TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
5460 error ("incompatible types in assignment of `%T' to `%T'",
5461 TREE_TYPE (rhs), lhstype);
5462 return error_mark_node;
5465 /* Allow array assignment in compiler-generated code. */
5466 if (! DECL_ARTIFICIAL (current_function_decl))
5467 pedwarn ("ISO C++ forbids assignment of arrays");
5469 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5470 ? 1 + (modifycode != INIT_EXPR): 0;
5471 return build_vec_init (lhs, NULL_TREE, newrhs, from_array);
5474 if (modifycode == INIT_EXPR)
5475 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5476 "initialization", NULL_TREE, 0);
5479 /* Avoid warnings on enum bit fields. */
5480 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5481 && TREE_CODE (lhstype) == INTEGER_TYPE)
5483 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5485 newrhs = convert_force (lhstype, newrhs, 0);
5488 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5490 if (TREE_CODE (newrhs) == CALL_EXPR
5491 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5492 newrhs = build_cplus_new (lhstype, newrhs);
5494 /* Can't initialize directly from a TARGET_EXPR, since that would
5495 cause the lhs to be constructed twice, and possibly result in
5496 accidental self-initialization. So we force the TARGET_EXPR to be
5497 expanded without a target. */
5498 if (TREE_CODE (newrhs) == TARGET_EXPR)
5499 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5500 TREE_OPERAND (newrhs, 0));
5503 if (newrhs == error_mark_node)
5504 return error_mark_node;
5506 if (TREE_CODE (newrhs) == COND_EXPR)
5509 tree cond = TREE_OPERAND (newrhs, 0);
5511 if (TREE_SIDE_EFFECTS (lhs))
5512 cond = build_compound_expr (tree_cons
5514 build_tree_list (NULL_TREE, cond)));
5516 /* Cannot have two identical lhs on this one tree (result) as preexpand
5517 calls will rip them out and fill in RTL for them, but when the
5518 rtl is generated, the calls will only be in the first side of the
5519 condition, not on both, or before the conditional jump! (mrs) */
5520 lhs1 = break_out_calls (lhs);
5523 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5524 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5525 lhstype, lhs, newrhs);
5528 tree result_type = TREE_TYPE (newrhs);
5529 /* We have to convert each arm to the proper type because the
5530 types may have been munged by constant folding. */
5532 = build (COND_EXPR, result_type, cond,
5533 build_modify_expr (lhs, modifycode,
5534 cp_convert (result_type,
5535 TREE_OPERAND (newrhs, 1))),
5536 build_modify_expr (lhs1, modifycode,
5537 cp_convert (result_type,
5538 TREE_OPERAND (newrhs, 2))));
5542 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5543 lhstype, lhs, newrhs);
5545 TREE_SIDE_EFFECTS (result) = 1;
5547 /* If we got the LHS in a different type for storing in,
5548 convert the result back to the nominal type of LHS
5549 so that the value we return always has the same type
5550 as the LHS argument. */
5552 if (olhstype == TREE_TYPE (result))
5554 /* Avoid warnings converting integral types back into enums
5555 for enum bit fields. */
5556 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5557 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5559 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5560 TREE_NO_UNUSED_WARNING (result) = 1;
5563 return convert_for_assignment (olhstype, result, "assignment",
5568 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5570 if (processing_template_decl)
5571 return build_min_nt (MODOP_EXPR, lhs,
5572 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5574 if (modifycode != NOP_EXPR)
5576 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5577 make_node (modifycode));
5581 return build_modify_expr (lhs, modifycode, rhs);
5585 /* Get difference in deltas for different pointer to member function
5586 types. Return integer_zero_node, if FROM cannot be converted to a
5587 TO type. If FORCE is true, then allow reverse conversions as well.
5589 Note that the naming of FROM and TO is kind of backwards; the return
5590 value is what we add to a TO in order to get a FROM. They are named
5591 this way because we call this function to find out how to convert from
5592 a pointer to member of FROM to a pointer to member of TO. */
5595 get_delta_difference (tree from, tree to, int force)
5597 tree delta = integer_zero_node;
5602 binfo = lookup_base (to, from, ba_check, &kind);
5603 if (kind == bk_inaccessible || kind == bk_ambig)
5605 error (" in pointer to member function conversion");
5612 error_not_base_type (from, to);
5613 error (" in pointer to member conversion");
5616 binfo = lookup_base (from, to, ba_check, &kind);
5619 virt_binfo = binfo_from_vbase (binfo);
5623 /* This is a reinterpret cast, we choose to do nothing. */
5624 warning ("pointer to member cast via virtual base `%T'",
5625 BINFO_TYPE (virt_binfo));
5628 delta = BINFO_OFFSET (binfo);
5629 delta = cp_convert (ptrdiff_type_node, delta);
5630 delta = cp_build_binary_op (MINUS_EXPR,
5637 virt_binfo = binfo_from_vbase (binfo);
5640 /* This is a reinterpret cast, we choose to do nothing. */
5642 warning ("pointer to member cast via virtual base `%T'",
5643 BINFO_TYPE (virt_binfo));
5645 error ("pointer to member conversion via virtual base `%T'",
5646 BINFO_TYPE (virt_binfo));
5649 delta = BINFO_OFFSET (binfo);
5651 return cp_convert (ptrdiff_type_node, delta);
5654 /* Return a constructor for the pointer-to-member-function TYPE using
5655 the other components as specified. */
5658 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
5664 /* Pull the FIELD_DECLs out of the type. */
5665 pfn_field = TYPE_FIELDS (type);
5666 delta_field = TREE_CHAIN (pfn_field);
5668 /* Make sure DELTA has the type we want. */
5669 delta = convert_and_check (delta_type_node, delta);
5671 /* Finish creating the initializer. */
5672 u = tree_cons (pfn_field, pfn,
5673 build_tree_list (delta_field, delta));
5674 u = build_constructor (type, u);
5675 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
5676 TREE_STATIC (u) = (TREE_CONSTANT (u)
5677 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
5679 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
5684 /* Build a constructor for a pointer to member function. It can be
5685 used to initialize global variables, local variable, or used
5686 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5689 If FORCE is nonzero, then force this conversion, even if
5690 we would rather not do it. Usually set when using an explicit
5693 Return error_mark_node, if something goes wrong. */
5696 build_ptrmemfunc (tree type, tree pfn, int force)
5702 if (error_operand_p (pfn))
5703 return error_mark_node;
5705 pfn_type = TREE_TYPE (pfn);
5706 to_type = build_ptrmemfunc_type (type);
5708 /* Handle multiple conversions of pointer to member functions. */
5709 if (TYPE_PTRMEMFUNC_P (pfn_type))
5711 tree delta = NULL_TREE;
5712 tree npfn = NULL_TREE;
5716 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
5717 error ("invalid conversion to type `%T' from type `%T'",
5720 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
5721 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
5724 /* We don't have to do any conversion to convert a
5725 pointer-to-member to its own type. But, we don't want to
5726 just return a PTRMEM_CST if there's an explicit cast; that
5727 cast should make the expression an invalid template argument. */
5728 if (TREE_CODE (pfn) != PTRMEM_CST)
5730 if (same_type_p (to_type, pfn_type))
5732 else if (integer_zerop (n))
5733 return build_reinterpret_cast (to_type, pfn);
5736 if (TREE_SIDE_EFFECTS (pfn))
5737 pfn = save_expr (pfn);
5739 /* Obtain the function pointer and the current DELTA. */
5740 if (TREE_CODE (pfn) == PTRMEM_CST)
5741 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
5744 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
5745 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
5748 /* Just adjust the DELTA field. */
5749 delta = cp_convert (ptrdiff_type_node, delta);
5750 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
5751 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
5752 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
5753 return build_ptrmemfunc1 (to_type, delta, npfn);
5756 /* Handle null pointer to member function conversions. */
5757 if (integer_zerop (pfn))
5759 pfn = build_c_cast (type, integer_zero_node);
5760 return build_ptrmemfunc1 (to_type,
5765 if (type_unknown_p (pfn))
5766 return instantiate_type (type, pfn, tf_error | tf_warning);
5768 fn = TREE_OPERAND (pfn, 0);
5769 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5770 return make_ptrmem_cst (to_type, fn);
5773 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5776 ??? There is no consistency as to the types returned for the above
5777 values. Some code acts as if its a sizetype and some as if its
5778 integer_type_node. */
5781 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
5783 tree type = TREE_TYPE (cst);
5784 tree fn = PTRMEM_CST_MEMBER (cst);
5785 tree ptr_class, fn_class;
5787 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5789 /* The class that the function belongs to. */
5790 fn_class = DECL_CONTEXT (fn);
5792 /* The class that we're creating a pointer to member of. */
5793 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
5795 /* First, calculate the adjustment to the function's class. */
5796 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
5798 if (!DECL_VIRTUAL_P (fn))
5799 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
5802 /* If we're dealing with a virtual function, we have to adjust 'this'
5803 again, to point to the base which provides the vtable entry for
5804 fn; the call will do the opposite adjustment. */
5805 tree orig_class = DECL_CONTEXT (fn);
5806 tree binfo = binfo_or_else (orig_class, fn_class);
5807 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5808 *delta, BINFO_OFFSET (binfo)));
5810 /* We set PFN to the vtable offset at which the function can be
5811 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
5812 case delta is shifted left, and then incremented). */
5813 *pfn = DECL_VINDEX (fn);
5814 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
5815 TYPE_SIZE_UNIT (vtable_entry_type)));
5817 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
5819 case ptrmemfunc_vbit_in_pfn:
5820 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
5824 case ptrmemfunc_vbit_in_delta:
5825 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
5826 *delta, integer_one_node));
5827 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5828 *delta, integer_one_node));
5835 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
5840 /* Return an expression for PFN from the pointer-to-member function
5844 pfn_from_ptrmemfunc (tree t)
5846 if (TREE_CODE (t) == PTRMEM_CST)
5851 expand_ptrmemfunc_cst (t, &delta, &pfn);
5856 return build_ptrmemfunc_access_expr (t, pfn_identifier);
5859 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
5860 if this is a potentially dangerous thing to do. Returns a possibly
5864 dubious_conversion_warnings (tree type, tree expr,
5865 const char *errtype, tree fndecl, int parmnum)
5867 if (TREE_CODE (type) == REFERENCE_TYPE)
5868 type = TREE_TYPE (type);
5870 /* Issue warnings about peculiar, but valid, uses of NULL. */
5871 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
5874 warning ("passing NULL used for non-pointer %s %P of `%D'",
5875 errtype, parmnum, fndecl);
5877 warning ("%s to non-pointer type `%T' from NULL", errtype, type);
5880 /* Warn about assigning a floating-point type to an integer type. */
5881 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
5882 && TREE_CODE (type) == INTEGER_TYPE)
5885 warning ("passing `%T' for %s %P of `%D'",
5886 TREE_TYPE (expr), errtype, parmnum, fndecl);
5888 warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
5890 /* And warn about assigning a negative value to an unsigned
5892 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
5894 if (TREE_CODE (expr) == INTEGER_CST
5895 && TREE_NEGATED_INT (expr))
5898 warning ("passing negative value `%E' for %s %P of `%D'",
5899 expr, errtype, parmnum, fndecl);
5901 warning ("%s of negative value `%E' to `%T'",
5902 errtype, expr, type);
5905 overflow_warning (expr);
5907 if (TREE_CONSTANT (expr))
5913 /* Convert value RHS to type TYPE as preparation for an assignment to
5914 an lvalue of type TYPE. ERRTYPE is a string to use in error
5915 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
5916 are doing the conversion in order to pass the PARMNUMth argument of
5920 convert_for_assignment (tree type, tree rhs,
5921 const char *errtype, tree fndecl, int parmnum)
5923 register enum tree_code codel = TREE_CODE (type);
5924 register tree rhstype;
5925 register enum tree_code coder;
5927 if (codel == OFFSET_TYPE)
5930 if (TREE_CODE (rhs) == OFFSET_REF)
5931 rhs = resolve_offset_ref (rhs);
5933 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
5934 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
5935 rhs = TREE_OPERAND (rhs, 0);
5937 rhstype = TREE_TYPE (rhs);
5938 coder = TREE_CODE (rhstype);
5940 if (rhs == error_mark_node || rhstype == error_mark_node)
5941 return error_mark_node;
5942 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
5943 return error_mark_node;
5945 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
5947 /* The RHS of an assignment cannot have void type. */
5948 if (coder == VOID_TYPE)
5950 error ("void value not ignored as it ought to be");
5951 return error_mark_node;
5954 /* Simplify the RHS if possible. */
5955 if (TREE_CODE (rhs) == CONST_DECL)
5956 rhs = DECL_INITIAL (rhs);
5958 /* We do not use decl_constant_value here because of this case:
5960 const char* const s = "s";
5962 The conversion rules for a string literal are more lax than for a
5963 variable; in particular, a string literal can be converted to a
5964 "char *" but the variable "s" cannot be converted in the same
5965 way. If the conversion is allowed, the optimization should be
5966 performed while creating the converted expression. */
5970 The expression is implicitly converted (clause _conv_) to the
5971 cv-unqualified type of the left operand.
5973 We allow bad conversions here because by the time we get to this point
5974 we are committed to doing the conversion. If we end up doing a bad
5975 conversion, convert_like will complain. */
5976 if (!can_convert_arg_bad (type, rhstype, rhs))
5978 /* When -Wno-pmf-conversions is use, we just silently allow
5979 conversions from pointers-to-members to plain pointers. If
5980 the conversion doesn't work, cp_convert will complain. */
5982 && TYPE_PTR_P (type)
5983 && TYPE_PTRMEMFUNC_P (rhstype))
5984 rhs = cp_convert (strip_top_quals (type), rhs);
5987 /* If the right-hand side has unknown type, then it is an
5988 overloaded function. Call instantiate_type to get error
5990 if (rhstype == unknown_type_node)
5991 instantiate_type (type, rhs, tf_error | tf_warning);
5993 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
5994 rhstype, type, parmnum, fndecl);
5996 error ("cannot convert `%T' to `%T' in %s", rhstype, type,
5998 return error_mark_node;
6001 return perform_implicit_conversion (strip_top_quals (type), rhs);
6004 /* Convert RHS to be of type TYPE.
6005 If EXP is nonzero, it is the target of the initialization.
6006 ERRTYPE is a string to use in error messages.
6008 Two major differences between the behavior of
6009 `convert_for_assignment' and `convert_for_initialization'
6010 are that references are bashed in the former, while
6011 copied in the latter, and aggregates are assigned in
6012 the former (operator=) while initialized in the
6015 If using constructor make sure no conversion operator exists, if one does
6016 exist, an ambiguity exists.
6018 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6021 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
6022 const char *errtype, tree fndecl, int parmnum)
6024 register enum tree_code codel = TREE_CODE (type);
6025 register tree rhstype;
6026 register enum tree_code coder;
6028 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6029 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6030 if (TREE_CODE (rhs) == NOP_EXPR
6031 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6032 && codel != REFERENCE_TYPE)
6033 rhs = TREE_OPERAND (rhs, 0);
6035 if (rhs == error_mark_node
6036 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6037 return error_mark_node;
6039 if (TREE_CODE (rhs) == OFFSET_REF)
6041 rhs = resolve_offset_ref (rhs);
6042 if (rhs == error_mark_node)
6043 return error_mark_node;
6046 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6047 rhs = convert_from_reference (rhs);
6049 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6050 && TREE_CODE (type) != ARRAY_TYPE
6051 && (TREE_CODE (type) != REFERENCE_TYPE
6052 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6053 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6054 && (TREE_CODE (type) != REFERENCE_TYPE
6055 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6056 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6057 rhs = default_conversion (rhs);
6059 rhstype = TREE_TYPE (rhs);
6060 coder = TREE_CODE (rhstype);
6062 if (coder == ERROR_MARK)
6063 return error_mark_node;
6065 /* We accept references to incomplete types, so we can
6066 return here before checking if RHS is of complete type. */
6068 if (codel == REFERENCE_TYPE)
6070 /* This should eventually happen in convert_arguments. */
6071 int savew = 0, savee = 0;
6074 savew = warningcount, savee = errorcount;
6075 rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE);
6078 if (warningcount > savew)
6079 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6080 else if (errorcount > savee)
6081 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6087 exp = require_complete_type (exp);
6088 if (exp == error_mark_node)
6089 return error_mark_node;
6091 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6092 rhstype = TREE_TYPE (rhstype);
6094 type = complete_type (type);
6096 if (IS_AGGR_TYPE (type))
6097 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6099 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6102 /* Expand an ASM statement with operands, handling output operands
6103 that are not variables or INDIRECT_REFS by transforming such
6104 cases into cases that expand_asm_operands can handle.
6106 Arguments are same as for expand_asm_operands.
6108 We don't do default conversions on all inputs, because it can screw
6109 up operands that are expected to be in memory. */
6112 c_expand_asm_operands (tree string, tree outputs, tree inputs, tree clobbers,
6113 int vol, const char *filename, int line)
6115 int noutputs = list_length (outputs);
6117 /* o[I] is the place that output number I should be written. */
6118 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6121 /* Record the contents of OUTPUTS before it is modified. */
6122 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6123 o[i] = TREE_VALUE (tail);
6125 /* Generate the ASM_OPERANDS insn;
6126 store into the TREE_VALUEs of OUTPUTS some trees for
6127 where the values were actually stored. */
6128 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6130 /* Copy all the intermediate outputs into the specified outputs. */
6131 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6133 if (o[i] != TREE_VALUE (tail))
6135 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6136 const0_rtx, VOIDmode, EXPAND_NORMAL);
6139 /* Restore the original value so that it's correct the next
6140 time we expand this function. */
6141 TREE_VALUE (tail) = o[i];
6143 /* Detect modification of read-only values.
6144 (Otherwise done by build_modify_expr.) */
6147 tree type = TREE_TYPE (o[i]);
6148 if (type != error_mark_node
6149 && (CP_TYPE_CONST_P (type)
6150 || (CLASS_TYPE_P (type) && C_TYPE_FIELDS_READONLY (type))))
6151 readonly_error (o[i], "modification by `asm'", 1);
6155 /* Those MODIFY_EXPRs could do autoincrements. */
6159 /* If RETVAL is the address of, or a reference to, a local variable or
6160 temporary give an appropraite warning. */
6163 maybe_warn_about_returning_address_of_local (tree retval)
6165 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6166 tree whats_returned = retval;
6170 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6171 whats_returned = TREE_OPERAND (whats_returned, 1);
6172 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6173 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6174 || TREE_CODE (whats_returned) == NOP_EXPR)
6175 whats_returned = TREE_OPERAND (whats_returned, 0);
6180 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6182 whats_returned = TREE_OPERAND (whats_returned, 0);
6184 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6186 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6187 || TREE_CODE (whats_returned) == TARGET_EXPR)
6189 warning ("returning reference to temporary");
6192 if (TREE_CODE (whats_returned) == VAR_DECL
6193 && DECL_NAME (whats_returned)
6194 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6196 warning ("reference to non-lvalue returned");
6201 if (TREE_CODE (whats_returned) == VAR_DECL
6202 && DECL_NAME (whats_returned)
6203 && DECL_FUNCTION_SCOPE_P (whats_returned)
6204 && !(TREE_STATIC (whats_returned)
6205 || TREE_PUBLIC (whats_returned)))
6207 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6208 cp_warning_at ("reference to local variable `%D' returned",
6211 cp_warning_at ("address of local variable `%D' returned",
6217 /* Check that returning RETVAL from the current function is valid.
6218 Return an expression explicitly showing all conversions required to
6219 change RETVAL into the function return type, and to assign it to
6220 the DECL_RESULT for the function. */
6223 check_return_expr (tree retval)
6226 /* The type actually returned by the function, after any
6229 int fn_returns_value_p;
6231 /* A `volatile' function is one that isn't supposed to return, ever.
6232 (This is a G++ extension, used to get better code for functions
6233 that call the `volatile' function.) */
6234 if (TREE_THIS_VOLATILE (current_function_decl))
6235 warning ("function declared `noreturn' has a `return' statement");
6237 /* Check for various simple errors. */
6238 if (DECL_DESTRUCTOR_P (current_function_decl))
6241 error ("returning a value from a destructor");
6244 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6246 if (in_function_try_handler)
6247 /* If a return statement appears in a handler of the
6248 function-try-block of a constructor, the program is ill-formed. */
6249 error ("cannot return from a handler of a function-try-block of a constructor");
6251 /* You can't return a value from a constructor. */
6252 error ("returning a value from a constructor");
6256 if (processing_template_decl)
6258 current_function_returns_value = 1;
6262 /* When no explicit return-value is given in a function with a named
6263 return value, the named return value is used. */
6264 result = DECL_RESULT (current_function_decl);
6265 valtype = TREE_TYPE (result);
6266 my_friendly_assert (valtype != NULL_TREE, 19990924);
6267 fn_returns_value_p = !VOID_TYPE_P (valtype);
6268 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6271 /* Check for a return statement with no return value in a function
6272 that's supposed to return a value. */
6273 if (!retval && fn_returns_value_p)
6275 pedwarn ("return-statement with no value, in function returning `%D'",
6277 /* Clear this, so finish_function won't say that we reach the
6278 end of a non-void function (which we don't, we gave a
6280 current_function_returns_null = 0;
6282 /* Check for a return statement with a value in a function that
6283 isn't supposed to return a value. */
6284 else if (retval && !fn_returns_value_p)
6286 if (VOID_TYPE_P (TREE_TYPE (retval)))
6287 /* You can return a `void' value from a function of `void'
6288 type. In that case, we have to evaluate the expression for
6289 its side-effects. */
6290 finish_expr_stmt (retval);
6292 pedwarn ("return-statement with a value, in function returning `%D'",
6295 current_function_returns_null = 1;
6297 /* There's really no value to return, after all. */
6301 /* Remember that this function can sometimes return without a
6303 current_function_returns_null = 1;
6305 /* Remember that this function did return a value. */
6306 current_function_returns_value = 1;
6308 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6309 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6310 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6311 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6313 && null_ptr_cst_p (retval))
6314 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6316 /* Effective C++ rule 15. See also start_function. */
6318 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6319 && retval != current_class_ref)
6320 warning ("`operator=' should return a reference to `*this'");
6322 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6324 [...] For a function with a class return type, if the expression
6325 in the return statement is the name of a local object, and the cv-
6326 unqualified type of the local object is the same as the function
6327 return type, an implementation is permitted to omit creating the tem-
6328 porary object to hold the function return value [...]
6330 So, if this is a value-returning function that always returns the same
6331 local variable, remember it.
6333 It might be nice to be more flexible, and choose the first suitable
6334 variable even if the function sometimes returns something else, but
6335 then we run the risk of clobbering the variable we chose if the other
6336 returned expression uses the chosen variable somehow. And people expect
6337 this restriction, anyway. (jason 2000-11-19)
6339 See finish_function, genrtl_start_function, and declare_return_variable
6340 for other pieces of this optimization. */
6342 if (fn_returns_value_p && flag_elide_constructors)
6344 if (retval != NULL_TREE
6345 && (current_function_return_value == NULL_TREE
6346 || current_function_return_value == retval)
6347 && TREE_CODE (retval) == VAR_DECL
6348 && DECL_CONTEXT (retval) == current_function_decl
6349 && ! TREE_STATIC (retval)
6350 && (DECL_ALIGN (retval)
6351 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6352 && same_type_p ((TYPE_MAIN_VARIANT
6353 (TREE_TYPE (retval))),
6355 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6356 current_function_return_value = retval;
6358 current_function_return_value = error_mark_node;
6361 /* We don't need to do any conversions when there's nothing being
6363 if (!retval || retval == error_mark_node)
6366 /* Do any required conversions. */
6367 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6368 /* No conversions are required. */
6372 /* The type the function is declared to return. */
6373 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6375 /* First convert the value to the function's return type, then
6376 to the type of return value's location to handle the
6377 case that functype is smaller than the valtype. */
6378 retval = convert_for_initialization
6379 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6380 "return", NULL_TREE, 0);
6381 retval = convert (valtype, retval);
6383 /* If the conversion failed, treat this just like `return;'. */
6384 if (retval == error_mark_node)
6386 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6387 else if (! current_function_returns_struct
6388 && TREE_CODE (retval) == TARGET_EXPR
6389 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6390 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6391 TREE_OPERAND (retval, 0));
6393 maybe_warn_about_returning_address_of_local (retval);
6396 /* Actually copy the value returned into the appropriate location. */
6397 if (retval && retval != result)
6398 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6404 /* Returns nonzero if the pointer-type FROM can be converted to the
6405 pointer-type TO via a qualification conversion. If CONSTP is -1,
6406 then we return nonzero if the pointers are similar, and the
6407 cv-qualification signature of FROM is a proper subset of that of TO.
6409 If CONSTP is positive, then all outer pointers have been
6413 comp_ptr_ttypes_real (tree to, tree from, int constp)
6415 int to_more_cv_qualified = 0;
6417 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6419 if (TREE_CODE (to) != TREE_CODE (from))
6422 if (TREE_CODE (from) == OFFSET_TYPE
6423 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6424 TYPE_OFFSET_BASETYPE (to)))
6427 /* Const and volatile mean something different for function types,
6428 so the usual checks are not appropriate. */
6429 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6431 if (!at_least_as_qualified_p (to, from))
6434 if (!at_least_as_qualified_p (from, to))
6439 ++to_more_cv_qualified;
6443 constp &= TYPE_READONLY (to);
6446 if (TREE_CODE (to) != POINTER_TYPE)
6447 return ((constp >= 0 || to_more_cv_qualified)
6448 && same_type_ignoring_top_level_qualifiers_p (to, from));
6452 /* When comparing, say, char ** to char const **, this function takes
6453 the 'char *' and 'char const *'. Do not pass non-pointer/reference
6454 types to this function. */
6457 comp_ptr_ttypes (tree to, tree from)
6459 return comp_ptr_ttypes_real (to, from, 1);
6462 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6463 type or inheritance-related types, regardless of cv-quals. */
6466 ptr_reasonably_similar (tree to, tree from)
6468 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6470 /* Any target type is similar enough to void. */
6471 if (TREE_CODE (to) == VOID_TYPE
6472 || TREE_CODE (from) == VOID_TYPE)
6475 if (TREE_CODE (to) != TREE_CODE (from))
6478 if (TREE_CODE (from) == OFFSET_TYPE
6479 && comptypes (TYPE_OFFSET_BASETYPE (to),
6480 TYPE_OFFSET_BASETYPE (from),
6481 COMPARE_BASE | COMPARE_DERIVED))
6484 if (TREE_CODE (to) == INTEGER_TYPE
6485 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6488 if (TREE_CODE (to) == FUNCTION_TYPE)
6491 if (TREE_CODE (to) != POINTER_TYPE)
6493 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6494 COMPARE_BASE | COMPARE_DERIVED);
6498 /* Like comp_ptr_ttypes, for const_cast. */
6501 comp_ptr_ttypes_const (tree to, tree from)
6503 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6505 if (TREE_CODE (to) != TREE_CODE (from))
6508 if (TREE_CODE (from) == OFFSET_TYPE
6509 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6510 TYPE_OFFSET_BASETYPE (to)))
6513 if (TREE_CODE (to) != POINTER_TYPE)
6514 return same_type_ignoring_top_level_qualifiers_p (to, from);
6518 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6521 comp_ptr_ttypes_reinterpret (tree to, tree from)
6525 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6527 if (TREE_CODE (from) == OFFSET_TYPE)
6528 from = TREE_TYPE (from);
6529 if (TREE_CODE (to) == OFFSET_TYPE)
6530 to = TREE_TYPE (to);
6532 /* Const and volatile mean something different for function types,
6533 so the usual checks are not appropriate. */
6534 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6535 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6537 if (!at_least_as_qualified_p (to, from))
6541 && !at_least_as_qualified_p (from, to))
6543 constp &= TYPE_READONLY (to);
6546 if (TREE_CODE (from) != POINTER_TYPE
6547 || TREE_CODE (to) != POINTER_TYPE)
6552 /* Returns the type qualifiers for this type, including the qualifiers on the
6553 elements for an array type. */
6556 cp_type_quals (tree type)
6558 type = strip_array_types (type);
6559 if (type == error_mark_node)
6560 return TYPE_UNQUALIFIED;
6561 return TYPE_QUALS (type);
6564 /* Returns nonzero if the TYPE contains a mutable member */
6567 cp_has_mutable_p (tree type)
6569 type = strip_array_types (type);
6571 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6574 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6575 exemplar types such that casting T1 to T2 is casting away castness
6576 if and only if there is no implicit conversion from T1 to T2. */
6579 casts_away_constness_r (tree *t1, tree *t2)
6584 /* [expr.const.cast]
6586 For multi-level pointer to members and multi-level mixed pointers
6587 and pointers to members (conv.qual), the "member" aspect of a
6588 pointer to member level is ignored when determining if a const
6589 cv-qualifier has been cast away. */
6590 if (TYPE_PTRMEM_P (*t1))
6591 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6592 if (TYPE_PTRMEM_P (*t2))
6593 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6595 /* [expr.const.cast]
6597 For two pointer types:
6599 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6600 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6603 casting from X1 to X2 casts away constness if, for a non-pointer
6604 type T there does not exist an implicit conversion (clause
6607 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6611 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6613 if (TREE_CODE (*t1) != POINTER_TYPE
6614 || TREE_CODE (*t2) != POINTER_TYPE)
6616 *t1 = cp_build_qualified_type (void_type_node,
6617 cp_type_quals (*t1));
6618 *t2 = cp_build_qualified_type (void_type_node,
6619 cp_type_quals (*t2));
6623 quals1 = cp_type_quals (*t1);
6624 quals2 = cp_type_quals (*t2);
6625 *t1 = TREE_TYPE (*t1);
6626 *t2 = TREE_TYPE (*t2);
6627 casts_away_constness_r (t1, t2);
6628 *t1 = build_pointer_type (*t1);
6629 *t2 = build_pointer_type (*t2);
6630 *t1 = cp_build_qualified_type (*t1, quals1);
6631 *t2 = cp_build_qualified_type (*t2, quals2);
6634 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
6638 casts_away_constness (tree t1, tree t2)
6640 if (TREE_CODE (t2) == REFERENCE_TYPE)
6642 /* [expr.const.cast]
6644 Casting from an lvalue of type T1 to an lvalue of type T2
6645 using a reference cast casts away constness if a cast from an
6646 rvalue of type "pointer to T1" to the type "pointer to T2"
6647 casts away constness. */
6648 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
6649 return casts_away_constness (build_pointer_type (t1),
6650 build_pointer_type (TREE_TYPE (t2)));
6653 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6654 /* [expr.const.cast]
6656 Casting from an rvalue of type "pointer to data member of X
6657 of type T1" to the type "pointer to data member of Y of type
6658 T2" casts away constness if a cast from an rvalue of type
6659 "pointer to T1" to the type "pointer to T2" casts away
6661 return casts_away_constness
6662 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
6663 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
6665 /* Casting away constness is only something that makes sense for
6666 pointer or reference types. */
6667 if (TREE_CODE (t1) != POINTER_TYPE
6668 || TREE_CODE (t2) != POINTER_TYPE)
6671 /* Top-level qualifiers don't matter. */
6672 t1 = TYPE_MAIN_VARIANT (t1);
6673 t2 = TYPE_MAIN_VARIANT (t2);
6674 casts_away_constness_r (&t1, &t2);
6675 if (!can_convert (t2, t1))
6681 /* Returns TYPE with its cv qualifiers removed
6682 TYPE is T cv* .. *cv where T is not a pointer type,
6683 returns T * .. *. (If T is an array type, then the cv qualifiers
6684 above are those of the array members.) */
6687 strip_all_pointer_quals (tree type)
6689 if (TREE_CODE (type) == POINTER_TYPE)
6690 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
6691 else if (TREE_CODE (type) == OFFSET_TYPE)
6692 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
6693 strip_all_pointer_quals (TREE_TYPE (type)));
6695 return TYPE_MAIN_VARIANT (type);