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 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 PARAMS ((tree, tree, const char *, tree,
50 static tree cp_pointer_int_sum PARAMS ((enum tree_code, tree, tree));
51 static tree rationalize_conditional_expr PARAMS ((enum tree_code, tree));
52 static int comp_target_parms PARAMS ((tree, tree));
53 static int comp_ptr_ttypes_real PARAMS ((tree, tree, int));
54 static int comp_ptr_ttypes_const PARAMS ((tree, tree));
55 static int comp_ptr_ttypes_reinterpret PARAMS ((tree, tree));
56 static int comp_except_types PARAMS ((tree, tree, int));
57 static int comp_array_types PARAMS ((int (*) (tree, tree, int), tree,
59 static tree common_base_type PARAMS ((tree, tree));
60 static tree lookup_anon_field PARAMS ((tree, tree));
61 static tree pointer_diff PARAMS ((tree, tree, tree));
62 static tree qualify_type_recursive PARAMS ((tree, tree));
63 static tree get_delta_difference PARAMS ((tree, tree, int));
64 static int comp_cv_target_types PARAMS ((tree, tree, int));
65 static void casts_away_constness_r PARAMS ((tree *, tree *));
66 static int casts_away_constness PARAMS ((tree, tree));
67 static void maybe_warn_about_returning_address_of_local PARAMS ((tree));
68 static tree strip_all_pointer_quals PARAMS ((tree));
69 static tree lookup_destructor (tree, tree, tree);
71 /* Return the target type of TYPE, which means return T for:
72 T*, T&, T[], T (...), and otherwise, just T. */
78 if (TREE_CODE (type) == REFERENCE_TYPE)
79 type = TREE_TYPE (type);
80 while (TREE_CODE (type) == POINTER_TYPE
81 || TREE_CODE (type) == ARRAY_TYPE
82 || TREE_CODE (type) == FUNCTION_TYPE
83 || TREE_CODE (type) == METHOD_TYPE
84 || TREE_CODE (type) == OFFSET_TYPE)
85 type = TREE_TYPE (type);
89 /* Do `exp = require_complete_type (exp);' to make sure exp
90 does not have an incomplete type. (That includes void types.)
91 Returns the error_mark_node if the VALUE does not have
92 complete type when this function returns. */
95 require_complete_type (value)
100 if (processing_template_decl || value == error_mark_node)
103 if (TREE_CODE (value) == OVERLOAD)
104 type = unknown_type_node;
106 type = TREE_TYPE (value);
108 /* First, detect a valid value with a complete type. */
109 if (COMPLETE_TYPE_P (type))
112 /* If we see X::Y, we build an OFFSET_TYPE which has
113 not been laid out. Try to avoid an error by interpreting
114 it as this->X::Y, if reasonable. */
115 if (TREE_CODE (value) == OFFSET_REF
116 && current_class_ref != 0
117 && TREE_OPERAND (value, 0) == current_class_ref)
119 value = resolve_offset_ref (value);
120 return require_complete_type (value);
123 if (complete_type_or_else (type, value))
126 return error_mark_node;
129 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
130 a template instantiation, do the instantiation. Returns TYPE,
131 whether or not it could be completed, unless something goes
132 horribly wrong, in which case the error_mark_node is returned. */
138 if (type == NULL_TREE)
139 /* Rather than crash, we return something sure to cause an error
141 return error_mark_node;
143 if (type == error_mark_node || COMPLETE_TYPE_P (type))
145 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
147 tree t = complete_type (TREE_TYPE (type));
148 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
150 TYPE_NEEDS_CONSTRUCTING (type)
151 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
152 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
153 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
155 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
156 instantiate_class_template (TYPE_MAIN_VARIANT (type));
161 /* Like complete_type, but issue an error if the TYPE cannot be completed.
162 VALUE is used for informative diagnostics. DIAG_TYPE indicates the type
163 of diagnostic: 0 for an error, 1 for a warning, 2 for a pedwarn.
164 Returns NULL_TREE if the type cannot be made complete. */
167 complete_type_or_diagnostic (type, value, diag_type)
172 type = complete_type (type);
173 if (type == error_mark_node)
174 /* We already issued an error. */
176 else if (!COMPLETE_TYPE_P (type))
178 cxx_incomplete_type_diagnostic (value, type, diag_type);
185 /* Return truthvalue of whether type of EXP is instantiated. */
191 return (TREE_CODE (exp) == OVERLOAD
192 || TREE_CODE (exp) == TREE_LIST
193 || TREE_TYPE (exp) == unknown_type_node
194 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
195 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
198 /* Return a pointer or pointer to member type similar to T1, with a
199 cv-qualification signature that is the union of the cv-qualification
200 signatures of T1 and T2: [expr.rel], [expr.eq]. */
203 qualify_type_recursive (t1, t2)
206 if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
207 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
214 tree attributes = (*targetm.merge_type_attributes) (t1, t2);
216 if (TYPE_PTRMEM_P (t1))
218 b1 = TYPE_PTRMEM_CLASS_TYPE (t1);
219 tt1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
220 tt2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
225 tt1 = TREE_TYPE (t1);
226 tt2 = TREE_TYPE (t2);
229 type_quals = (cp_type_quals (tt1) | cp_type_quals (tt2));
230 tgt = qualify_type_recursive (tt1, tt2);
231 tgt = cp_build_qualified_type (tgt, type_quals);
233 t1 = build_ptrmem_type (b1, tgt);
235 t1 = build_pointer_type (tgt);
236 t1 = build_type_attribute_variant (t1, attributes);
241 /* Return the common type of two parameter lists.
242 We assume that comptypes has already been done and returned 1;
243 if that isn't so, this may crash.
245 As an optimization, free the space we allocate if the parameter
246 lists are already common. */
252 tree oldargs = p1, newargs, n;
256 len = list_length (p1);
257 newargs = tree_last (p1);
259 if (newargs == void_list_node)
268 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
273 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
275 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
277 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
280 else if (! TREE_PURPOSE (p1))
282 if (TREE_PURPOSE (p2))
284 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
290 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
292 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
294 if (TREE_VALUE (p1) != TREE_VALUE (p2))
297 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
300 TREE_VALUE (n) = TREE_VALUE (p1);
308 /* Given a type, perhaps copied for a typedef,
309 find the "original" version of it. */
314 while (TYPE_NAME (t) != NULL_TREE)
316 tree x = TYPE_NAME (t);
317 if (TREE_CODE (x) != TYPE_DECL)
319 x = DECL_ORIGINAL_TYPE (x);
327 /* T1 and T2 are arithmetic or enumeration types. Return the type
328 that will result from the "usual arithmetic conversions" on T1 and
329 T2 as described in [expr]. */
332 type_after_usual_arithmetic_conversions (t1, t2)
336 enum tree_code code1 = TREE_CODE (t1);
337 enum tree_code code2 = TREE_CODE (t2);
340 /* FIXME: Attributes. */
341 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
342 || TREE_CODE (t1) == COMPLEX_TYPE
343 || TREE_CODE (t1) == ENUMERAL_TYPE,
345 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
346 || TREE_CODE (t2) == COMPLEX_TYPE
347 || TREE_CODE (t2) == ENUMERAL_TYPE,
350 /* In what follows, we slightly generalize the rules given in [expr] so
351 as to deal with `long long' and `complex'. First, merge the
353 attributes = (*targetm.merge_type_attributes) (t1, t2);
355 /* If one type is complex, form the common type of the non-complex
356 components, then make that complex. Use T1 or T2 if it is the
358 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
360 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
361 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
363 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
365 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
366 return build_type_attribute_variant (t1, attributes);
367 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
368 return build_type_attribute_variant (t2, attributes);
370 return build_type_attribute_variant (build_complex_type (subtype),
374 /* If only one is real, use it as the result. */
375 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
376 return build_type_attribute_variant (t1, attributes);
377 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
378 return build_type_attribute_variant (t2, attributes);
380 /* Perform the integral promotions. */
381 if (code1 != REAL_TYPE)
383 t1 = type_promotes_to (t1);
384 t2 = type_promotes_to (t2);
387 /* Both real or both integers; use the one with greater precision. */
388 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
389 return build_type_attribute_variant (t1, attributes);
390 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
391 return build_type_attribute_variant (t2, attributes);
393 /* The types are the same; no need to do anything fancy. */
394 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
395 return build_type_attribute_variant (t1, attributes);
397 if (code1 != REAL_TYPE)
399 /* If one is a sizetype, use it so size_binop doesn't blow up. */
400 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
401 return build_type_attribute_variant (t1, attributes);
402 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
403 return build_type_attribute_variant (t2, attributes);
405 /* If one is unsigned long long, then convert the other to unsigned
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
409 return build_type_attribute_variant (long_long_unsigned_type_node,
411 /* If one is a long long, and the other is an unsigned long, and
412 long long can represent all the values of an unsigned long, then
413 convert to a long long. Otherwise, convert to an unsigned long
414 long. Otherwise, if either operand is long long, convert the
417 Since we're here, we know the TYPE_PRECISION is the same;
418 therefore converting to long long cannot represent all the values
419 of an unsigned long, so we choose unsigned long long in that
421 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
422 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
424 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
425 ? long_long_unsigned_type_node
426 : long_long_integer_type_node);
427 return build_type_attribute_variant (t, attributes);
430 /* Go through the same procedure, but for longs. */
431 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
432 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
433 return build_type_attribute_variant (long_unsigned_type_node,
435 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
436 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
438 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
439 ? long_unsigned_type_node : long_integer_type_node);
440 return build_type_attribute_variant (t, attributes);
442 /* Otherwise prefer the unsigned one. */
443 if (TREE_UNSIGNED (t1))
444 return build_type_attribute_variant (t1, attributes);
446 return build_type_attribute_variant (t2, attributes);
450 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
451 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
452 return build_type_attribute_variant (long_double_type_node,
454 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
455 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
456 return build_type_attribute_variant (double_type_node,
458 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
459 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
460 return build_type_attribute_variant (float_type_node,
463 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
464 the standard C++ floating-point types. Logic earlier in this
465 function has already eliminated the possibility that
466 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
467 compelling reason to choose one or the other. */
468 return build_type_attribute_variant (t1, attributes);
472 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
473 ARG1 and ARG2 are the values with those types. The LOCATION is a
474 string describing the current location, in case an error occurs. */
477 composite_pointer_type (t1, t2, arg1, arg2, location)
482 const char* location;
489 If one operand is a null pointer constant, the composite pointer
490 type is the type of the other operand. */
491 if (null_ptr_cst_p (arg1))
493 if (null_ptr_cst_p (arg2))
496 /* Deal with pointer-to-member functions in the same way as we deal
497 with pointers to functions. */
498 if (TYPE_PTRMEMFUNC_P (t1))
499 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
500 if (TYPE_PTRMEMFUNC_P (t2))
501 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
503 /* Merge the attributes. */
504 attributes = (*targetm.merge_type_attributes) (t1, t2);
510 If one of the operands has type "pointer to cv1 void*", then
511 the other has type "pointer to cv2T", and the composite pointer
512 type is "pointer to cv12 void", where cv12 is the union of cv1
515 If either type is a pointer to void, make sure it is T1. */
516 if (VOID_TYPE_P (TREE_TYPE (t2)))
523 /* Now, if T1 is a pointer to void, merge the qualifiers. */
524 if (VOID_TYPE_P (TREE_TYPE (t1)))
526 if (pedantic && TYPE_PTRFN_P (t2))
527 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
530 result_type = cp_build_qualified_type (void_type_node,
532 | cp_type_quals (t2)));
533 result_type = build_pointer_type (result_type);
537 tree full1 = qualify_type_recursive (t1, t2);
538 tree full2 = qualify_type_recursive (t2, t1);
540 int val = comp_target_types (full1, full2, 1);
548 pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
550 result_type = ptr_type_node;
554 return build_type_attribute_variant (result_type, attributes);
557 /* Return the merged type of two types.
558 We assume that comptypes has already been done and returned 1;
559 if that isn't so, this may crash.
561 This just combines attributes and default arguments; any other
562 differences would cause the two types to compare unalike. */
568 register enum tree_code code1;
569 register enum tree_code code2;
572 /* Save time if the two types are the same. */
575 if (original_type (t1) == original_type (t2))
578 /* If one type is nonsense, use the other. */
579 if (t1 == error_mark_node)
581 if (t2 == error_mark_node)
584 /* Merge the attributes. */
585 attributes = (*targetm.merge_type_attributes) (t1, t2);
587 /* Treat an enum type as the unsigned integer type of the same width. */
589 if (TYPE_PTRMEMFUNC_P (t1))
590 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
591 if (TYPE_PTRMEMFUNC_P (t2))
592 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
594 code1 = TREE_CODE (t1);
595 code2 = TREE_CODE (t2);
601 /* For two pointers, do this recursively on the target type. */
603 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
604 int quals = cp_type_quals (t1);
606 if (code1 == POINTER_TYPE)
607 t1 = build_pointer_type (target);
609 t1 = build_reference_type (target);
610 t1 = build_type_attribute_variant (t1, attributes);
611 t1 = cp_build_qualified_type (t1, quals);
613 if (TREE_CODE (target) == METHOD_TYPE)
614 t1 = build_ptrmemfunc_type (t1);
621 tree base = TYPE_OFFSET_BASETYPE (t1);
622 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
623 t1 = build_offset_type (base, target);
629 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
630 /* Save space: see if the result is identical to one of the args. */
631 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
632 return build_type_attribute_variant (t1, attributes);
633 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
634 return build_type_attribute_variant (t2, attributes);
635 /* Merge the element types, and have a size if either arg has one. */
636 t1 = build_cplus_array_type
637 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
642 /* Function types: prefer the one that specified arg types.
643 If both do, merge the arg types. Also merge the return types. */
645 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
646 tree p1 = TYPE_ARG_TYPES (t1);
647 tree p2 = TYPE_ARG_TYPES (t2);
650 /* Save space: see if the result is identical to one of the args. */
651 if (valtype == TREE_TYPE (t1) && ! p2)
652 return build_type_attribute_variant (t1, attributes);
653 if (valtype == TREE_TYPE (t2) && ! p1)
654 return build_type_attribute_variant (t2, attributes);
656 /* Simple way if one arg fails to specify argument types. */
657 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
659 rval = build_function_type (valtype, p2);
660 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
661 rval = build_exception_variant (rval, raises);
662 return build_type_attribute_variant (rval, attributes);
664 raises = TYPE_RAISES_EXCEPTIONS (t1);
665 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
667 rval = build_function_type (valtype, p1);
669 rval = build_exception_variant (rval, raises);
670 return build_type_attribute_variant (rval, attributes);
673 rval = build_function_type (valtype, commonparms (p1, p2));
674 t1 = build_exception_variant (rval, raises);
680 /* Get this value the long way, since TYPE_METHOD_BASETYPE
681 is just the main variant of this. */
682 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
683 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
686 /* If this was a member function type, get back to the
687 original type of type member function (i.e., without
688 the class instance variable up front. */
689 t1 = build_function_type (TREE_TYPE (t1),
690 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
691 t2 = build_function_type (TREE_TYPE (t2),
692 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
693 t3 = merge_types (t1, t2);
694 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
695 TYPE_ARG_TYPES (t3));
696 t1 = build_exception_variant (t3, raises);
702 return build_type_attribute_variant (t1, attributes);
705 /* Return the common type of two types.
706 We assume that comptypes has already been done and returned 1;
707 if that isn't so, this may crash.
709 This is the type for the result of most arithmetic operations
710 if the operands have the given two types. */
716 enum tree_code code1;
717 enum tree_code code2;
719 /* If one type is nonsense, bail. */
720 if (t1 == error_mark_node || t2 == error_mark_node)
721 return error_mark_node;
723 code1 = TREE_CODE (t1);
724 code2 = TREE_CODE (t2);
726 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
727 || code1 == COMPLEX_TYPE)
728 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
729 || code2 == COMPLEX_TYPE))
730 return type_after_usual_arithmetic_conversions (t1, t2);
732 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
733 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
734 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
735 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
742 /* Compare two exception specifier types for exactness or subsetness, if
743 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
745 [except.spec] "If a class X ... objects of class X or any class publicly
746 and unambigously derrived from X. Similarly, if a pointer type Y * ...
747 exceptions of type Y * or that are pointers to any type publicly and
748 unambigously derrived from Y. Otherwise a function only allows exceptions
749 that have the same type ..."
750 This does not mention cv qualifiers and is different to what throw
751 [except.throw] and catch [except.catch] will do. They will ignore the
752 top level cv qualifiers, and allow qualifiers in the pointer to class
755 We implement the letter of the standard. */
758 comp_except_types (a, b, exact)
762 if (same_type_p (a, b))
766 if (cp_type_quals (a) || cp_type_quals (b))
769 if (TREE_CODE (a) == POINTER_TYPE
770 && TREE_CODE (b) == POINTER_TYPE)
774 if (cp_type_quals (a) || cp_type_quals (b))
778 if (TREE_CODE (a) != RECORD_TYPE
779 || TREE_CODE (b) != RECORD_TYPE)
782 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
788 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
789 If EXACT is 0, T2 can be stricter than T1 (according to 15.4/7),
790 otherwise it must be exact. Exception lists are unordered, but
791 we've already filtered out duplicates. Most lists will be in order,
792 we should try to make use of that. */
795 comp_except_specs (t1, t2, exact)
806 if (t1 == NULL_TREE) /* T1 is ... */
807 return t2 == NULL_TREE || !exact;
808 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
809 return t2 != NULL_TREE && !TREE_VALUE (t2);
810 if (t2 == NULL_TREE) /* T2 is ... */
812 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
815 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
816 Count how many we find, to determine exactness. For exact matching and
817 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
819 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
821 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
823 tree a = TREE_VALUE (probe);
824 tree b = TREE_VALUE (t2);
826 if (comp_except_types (a, b, exact))
828 if (probe == base && exact)
829 base = TREE_CHAIN (probe);
834 if (probe == NULL_TREE)
837 return !exact || base == NULL_TREE || length == list_length (t1);
840 /* Compare the array types T1 and T2, using CMP as the type comparison
841 function for the element types. STRICT is as for comptypes. */
844 comp_array_types (cmp, t1, t2, strict)
845 register int (*cmp) PARAMS ((tree, tree, int));
855 /* The type of the array elements must be the same. */
856 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
857 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
858 strict & ~COMPARE_REDECLARATION)))
861 d1 = TYPE_DOMAIN (t1);
862 d2 = TYPE_DOMAIN (t2);
867 /* If one of the arrays is dimensionless, and the other has a
868 dimension, they are of different types. However, it is valid to
876 declarations for an array object can specify
877 array types that differ by the presence or absence of a major
878 array bound (_dcl.array_). */
880 return strict & COMPARE_REDECLARATION;
882 /* Check that the dimensions are the same. */
883 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
885 && cp_tree_equal (TYPE_MAX_VALUE (d1),
886 TYPE_MAX_VALUE (d2)));
889 /* Return 1 if T1 and T2 are compatible types for assignment or
890 various other operations. STRICT is a bitwise-or of the COMPARE_*
894 comptypes (t1, t2, strict)
900 int orig_strict = strict;
902 /* The special exemption for redeclaring array types without an
903 array bound only applies at the top level:
908 is invalid, for example. */
909 strict &= ~COMPARE_REDECLARATION;
911 /* Suppress errors caused by previously reported errors */
915 /* This should never happen. */
916 my_friendly_assert (t1 != error_mark_node, 307);
918 if (t2 == error_mark_node)
921 /* If either type is the internal version of sizetype, return the
923 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
924 && TYPE_DOMAIN (t1) != 0)
925 t1 = TYPE_DOMAIN (t1);
927 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
928 && TYPE_DOMAIN (t2) != 0)
929 t2 = TYPE_DOMAIN (t2);
931 if (strict & COMPARE_RELAXED)
933 /* Treat an enum type as the unsigned integer type of the same width. */
935 if (TREE_CODE (t1) == ENUMERAL_TYPE)
936 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
937 if (TREE_CODE (t2) == ENUMERAL_TYPE)
938 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
944 if (TYPE_PTRMEMFUNC_P (t1))
945 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
946 if (TYPE_PTRMEMFUNC_P (t2))
947 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
949 /* Different classes of types can't be compatible. */
950 if (TREE_CODE (t1) != TREE_CODE (t2))
953 /* Qualifiers must match. */
954 if (cp_type_quals (t1) != cp_type_quals (t2))
956 if (strict == COMPARE_STRICT
957 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
960 /* Allow for two different type nodes which have essentially the same
961 definition. Note that we already checked for equality of the type
962 qualifiers (just above). */
964 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
967 if (strict & COMPARE_NO_ATTRIBUTES)
969 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
970 else if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
973 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
976 switch (TREE_CODE (t1))
978 case TEMPLATE_TEMPLATE_PARM:
979 case BOUND_TEMPLATE_TEMPLATE_PARM:
980 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
981 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
983 if (! comp_template_parms
984 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
985 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
987 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
989 /* Don't check inheritance. */
990 strict = COMPARE_STRICT;
995 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
996 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
997 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
998 val = comp_template_args (TYPE_TI_ARGS (t1),
1001 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1003 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1008 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1009 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1010 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1014 case REFERENCE_TYPE:
1015 t1 = TREE_TYPE (t1);
1016 t2 = TREE_TYPE (t2);
1017 /* first, check whether the referred types match with the
1018 required level of strictness */
1019 val = comptypes (t1, t2, strict);
1022 if (TREE_CODE (t1) == RECORD_TYPE
1023 && TREE_CODE (t2) == RECORD_TYPE)
1029 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1030 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1031 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1035 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1036 here since this is the one place where
1037 COMPARE_REDECLARATION should be used. */
1038 val = comp_array_types (comptypes, t1, t2, orig_strict);
1041 case TEMPLATE_TYPE_PARM:
1042 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1043 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1046 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1047 TYPENAME_TYPE_FULLNAME (t2)) < 1)
1049 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1051 case UNBOUND_CLASS_TEMPLATE:
1052 if (cp_tree_equal (TYPE_IDENTIFIER (t1),
1053 TYPE_IDENTIFIER (t2)) < 1)
1055 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1058 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1063 return attrval == 2 && val == 1 ? 2 : val;
1066 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1067 only in the same direction as the target type. */
1070 comp_cv_target_types (ttl, ttr, nptrs)
1076 if (!at_least_as_qualified_p (ttl, ttr)
1077 && !at_least_as_qualified_p (ttr, ttl))
1078 /* The qualifications are incomparable. */
1081 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1082 return more_qualified_p (ttr, ttl) ? -1 : 1;
1084 t = comp_target_types (ttl, ttr, nptrs);
1085 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1086 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1092 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1093 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1094 converted to TTL. Return -1 means that TTL can be converted to TTR but
1097 NPTRS is the number of pointers we can strip off and keep cool.
1098 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1099 but to not permit B** to convert to A**.
1101 This should go away. Callers should use can_convert or something
1102 similar instead. (jason 17 Apr 1997) */
1105 comp_target_types (ttl, ttr, nptrs)
1109 ttl = TYPE_MAIN_VARIANT (ttl);
1110 ttr = TYPE_MAIN_VARIANT (ttr);
1111 if (same_type_p (ttl, ttr))
1114 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1117 if ((TREE_CODE (ttr) == POINTER_TYPE
1118 || TREE_CODE (ttr) == REFERENCE_TYPE)
1119 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1120 of the type pointed to. This is necessary for reference init
1121 semantics. We won't get here from a previous call with nptrs == 1;
1122 for multi-level pointers we end up in comp_ptr_ttypes. */
1125 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1127 ttl = TREE_TYPE (ttl);
1128 ttr = TREE_TYPE (ttr);
1132 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1133 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1135 else if (TREE_CODE (ttl) == VOID_TYPE
1136 && TREE_CODE (ttr) != FUNCTION_TYPE
1137 && TREE_CODE (ttr) != METHOD_TYPE
1138 && TREE_CODE (ttr) != OFFSET_TYPE)
1140 else if (TREE_CODE (ttr) == VOID_TYPE
1141 && TREE_CODE (ttl) != FUNCTION_TYPE
1142 && TREE_CODE (ttl) != METHOD_TYPE
1143 && TREE_CODE (ttl) != OFFSET_TYPE)
1145 else if (TREE_CODE (ttl) == POINTER_TYPE
1146 || TREE_CODE (ttl) == ARRAY_TYPE)
1148 if (comp_ptr_ttypes (ttl, ttr))
1150 else if (comp_ptr_ttypes (ttr, ttl))
1156 /* Const and volatile mean something different for function types,
1157 so the usual checks are not appropriate. */
1158 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1159 return comp_target_types (ttl, ttr, nptrs - 1);
1161 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1164 if (TREE_CODE (ttr) == ARRAY_TYPE)
1165 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1166 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1173 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1178 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1187 argsl = TYPE_ARG_TYPES (ttl);
1188 argsr = TYPE_ARG_TYPES (ttr);
1190 /* Compare 'this' here, not in comp_target_parms. */
1191 if (TREE_CODE (ttr) == METHOD_TYPE)
1193 tree tl = TYPE_METHOD_BASETYPE (ttl);
1194 tree tr = TYPE_METHOD_BASETYPE (ttr);
1196 if (!same_or_base_type_p (tr, tl))
1198 if (same_or_base_type_p (tl, tr))
1204 argsl = TREE_CHAIN (argsl);
1205 argsr = TREE_CHAIN (argsr);
1208 switch (comp_target_parms (argsl, argsr))
1216 return saw_contra ? -1 : 1;
1219 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1223 /* Contravariance: we can assign a pointer to base member to a pointer
1224 to derived member. Note difference from simple pointer case, where
1225 we can pass a pointer to derived to a pointer to base. */
1226 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1227 TYPE_OFFSET_BASETYPE (ttl)))
1229 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1230 TYPE_OFFSET_BASETYPE (ttr)))
1240 ttl = TREE_TYPE (ttl);
1241 ttr = TREE_TYPE (ttr);
1243 if (TREE_CODE (ttl) == POINTER_TYPE
1244 || TREE_CODE (ttl) == ARRAY_TYPE)
1246 if (comp_ptr_ttypes (ttl, ttr))
1252 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1257 else if (IS_AGGR_TYPE (ttl))
1261 if (same_or_base_type_p (build_pointer_type (ttl),
1262 build_pointer_type (ttr)))
1264 if (same_or_base_type_p (build_pointer_type (ttr),
1265 build_pointer_type (ttl)))
1273 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1276 at_least_as_qualified_p (type1, type2)
1280 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1281 return ((cp_type_quals (type1) & cp_type_quals (type2))
1282 == cp_type_quals (type2));
1285 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1288 more_qualified_p (type1, type2)
1292 return (cp_type_quals (type1) != cp_type_quals (type2)
1293 && at_least_as_qualified_p (type1, type2));
1296 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1297 more cv-qualified that TYPE1, and 0 otherwise. */
1300 comp_cv_qualification (type1, type2)
1304 if (cp_type_quals (type1) == cp_type_quals (type2))
1307 if (at_least_as_qualified_p (type1, type2))
1310 else if (at_least_as_qualified_p (type2, type1))
1316 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1317 subset of the cv-qualification signature of TYPE2, and the types
1318 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1321 comp_cv_qual_signature (type1, type2)
1325 if (comp_ptr_ttypes_real (type2, type1, -1))
1327 else if (comp_ptr_ttypes_real (type1, type2, -1))
1333 /* If two types share a common base type, return that basetype.
1334 If there is not a unique most-derived base type, this function
1335 returns ERROR_MARK_NODE. */
1338 common_base_type (tt1, tt2)
1341 tree best = NULL_TREE;
1344 /* If one is a baseclass of another, that's good enough. */
1345 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1347 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1350 /* Otherwise, try to find a unique baseclass of TT1
1351 that is shared by TT2, and follow that down. */
1352 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1354 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1355 tree trial = common_base_type (basetype, tt2);
1358 if (trial == error_mark_node)
1360 if (best == NULL_TREE)
1362 else if (best != trial)
1363 return error_mark_node;
1368 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1370 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1371 tree trial = common_base_type (tt1, basetype);
1374 if (trial == error_mark_node)
1376 if (best == NULL_TREE)
1378 else if (best != trial)
1379 return error_mark_node;
1385 /* Subroutines of `comptypes'. */
1387 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1388 equivalent in the sense that functions with those parameter types
1389 can have equivalent types. The two lists must be equivalent,
1392 C++: See comment above about TYPE1, TYPE2. */
1395 compparms (parms1, parms2)
1396 tree parms1, parms2;
1398 register tree t1 = parms1, t2 = parms2;
1400 /* An unspecified parmlist matches any specified parmlist
1401 whose argument types don't need default promotions. */
1405 if (t1 == 0 && t2 == 0)
1407 /* If one parmlist is shorter than the other,
1408 they fail to match. */
1409 if (t1 == 0 || t2 == 0)
1411 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1414 t1 = TREE_CHAIN (t1);
1415 t2 = TREE_CHAIN (t2);
1419 /* This really wants return whether or not parameter type lists
1420 would make their owning functions assignment compatible or not.
1422 The return value is like for comp_target_types.
1424 This should go away, possibly with the exception of the empty parmlist
1425 conversion; there are no conversions between function types in C++.
1426 (jason 17 Apr 1997) */
1429 comp_target_parms (parms1, parms2)
1430 tree parms1, parms2;
1432 register tree t1 = parms1, t2 = parms2;
1433 int warn_contravariance = 0;
1435 /* In C, an unspecified parmlist matches any specified parmlist
1436 whose argument types don't need default promotions. This is not
1437 true for C++, but let's do it anyway for unfixed headers. */
1439 if (t1 == 0 && t2 != 0)
1441 pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1443 return self_promoting_args_p (t2);
1446 return self_promoting_args_p (t1);
1448 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1452 /* If one parmlist is shorter than the other,
1453 they fail to match, unless STRICT is <= 0. */
1454 if (t1 == 0 || t2 == 0)
1456 p1 = TREE_VALUE (t1);
1457 p2 = TREE_VALUE (t2);
1458 if (same_type_p (p1, p2))
1464 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1465 || (TREE_CODE (p1) == REFERENCE_TYPE
1466 && TREE_CODE (p2) == REFERENCE_TYPE))
1468 /* The following is wrong for contravariance,
1469 but many programs depend on it. */
1470 if (TREE_TYPE (p1) == void_type_node)
1472 if (TREE_TYPE (p2) == void_type_node)
1474 warn_contravariance = 1;
1477 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1478 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1482 /* Note backwards order due to contravariance. */
1483 if (comp_target_types (p2, p1, 1) <= 0)
1485 if (comp_target_types (p1, p2, 1) > 0)
1487 warn_contravariance = 1;
1493 return warn_contravariance ? -1 : 1;
1497 cxx_sizeof_or_alignof_type (type, op, complain)
1502 enum tree_code type_code;
1504 const char *op_name;
1506 my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
1507 if (processing_template_decl)
1508 return build_min_nt (op, type);
1510 op_name = operator_name_info[(int) op].name;
1512 if (TREE_CODE (type) == REFERENCE_TYPE)
1513 type = TREE_TYPE (type);
1514 type_code = TREE_CODE (type);
1516 if (type_code == METHOD_TYPE)
1518 if (complain && (pedantic || warn_pointer_arith))
1519 pedwarn ("invalid application of `%s' to a member function", op_name);
1520 value = size_one_node;
1522 else if (type_code == OFFSET_TYPE)
1525 error ("invalid application of `%s' to non-static member", op_name);
1526 value = size_zero_node;
1529 value = c_sizeof_or_alignof_type (complete_type (type), op, complain);
1538 if (processing_template_decl)
1539 return build_min_nt (SIZEOF_EXPR, e);
1541 if (TREE_CODE (e) == COMPONENT_REF
1542 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1543 error ("sizeof applied to a bit-field");
1544 if (is_overloaded_fn (e))
1546 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1547 return c_sizeof (char_type_node);
1549 else if (type_unknown_p (e))
1551 cxx_incomplete_type_error (e, TREE_TYPE (e));
1552 return c_sizeof (char_type_node);
1554 /* It's invalid to say `sizeof (X::i)' for `i' a non-static data
1555 member unless you're in a non-static member of X. So hand off to
1556 resolve_offset_ref. [expr.prim] */
1557 else if (TREE_CODE (e) == OFFSET_REF)
1558 e = resolve_offset_ref (e);
1560 if (e == error_mark_node)
1563 return cxx_sizeof (TREE_TYPE (e));
1567 /* Perform the array-to-pointer and function-to-pointer conversions
1570 In addition, references are converted to lvalues and manifest
1571 constants are replaced by their values. */
1574 decay_conversion (exp)
1578 register enum tree_code code;
1580 if (TREE_CODE (exp) == OFFSET_REF)
1581 exp = resolve_offset_ref (exp);
1583 type = TREE_TYPE (exp);
1584 code = TREE_CODE (type);
1586 if (code == REFERENCE_TYPE)
1588 exp = convert_from_reference (exp);
1589 type = TREE_TYPE (exp);
1590 code = TREE_CODE (type);
1593 if (type == error_mark_node)
1594 return error_mark_node;
1596 if (type_unknown_p (exp))
1598 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1599 return error_mark_node;
1602 /* Constants can be used directly unless they're not loadable. */
1603 if (TREE_CODE (exp) == CONST_DECL)
1604 exp = DECL_INITIAL (exp);
1605 /* Replace a nonvolatile const static variable with its value. We
1606 don't do this for arrays, though; we want the address of the
1607 first element of the array, not the address of the first element
1608 of its initializing constant. */
1609 else if (code != ARRAY_TYPE)
1611 exp = decl_constant_value (exp);
1612 type = TREE_TYPE (exp);
1615 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1616 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1618 if (code == VOID_TYPE)
1620 error ("void value not ignored as it ought to be");
1621 return error_mark_node;
1623 if (code == METHOD_TYPE)
1625 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1626 return build_unary_op (ADDR_EXPR, exp, 0);
1627 if (code == ARRAY_TYPE)
1632 if (TREE_CODE (exp) == INDIRECT_REF)
1634 /* Stripping away the INDIRECT_REF is not the right
1635 thing to do for references... */
1636 tree inner = TREE_OPERAND (exp, 0);
1637 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1639 inner = build1 (CONVERT_EXPR,
1640 build_pointer_type (TREE_TYPE
1641 (TREE_TYPE (inner))),
1643 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1645 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1648 if (TREE_CODE (exp) == COMPOUND_EXPR)
1650 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1651 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1652 TREE_OPERAND (exp, 0), op1);
1656 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1658 error ("invalid use of non-lvalue array");
1659 return error_mark_node;
1662 ptrtype = build_pointer_type (TREE_TYPE (type));
1664 if (TREE_CODE (exp) == VAR_DECL)
1666 /* ??? This is not really quite correct
1667 in that the type of the operand of ADDR_EXPR
1668 is not the target type of the type of the ADDR_EXPR itself.
1669 Question is, can this lossage be avoided? */
1670 adr = build1 (ADDR_EXPR, ptrtype, exp);
1671 if (!cxx_mark_addressable (exp))
1672 return error_mark_node;
1673 TREE_CONSTANT (adr) = staticp (exp);
1674 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1677 /* This way is better for a COMPONENT_REF since it can
1678 simplify the offset for a component. */
1679 adr = build_unary_op (ADDR_EXPR, exp, 1);
1680 return cp_convert (ptrtype, adr);
1683 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1684 rvalues always have cv-unqualified types. */
1685 if (! CLASS_TYPE_P (type))
1686 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1692 default_conversion (exp)
1696 enum tree_code code;
1698 exp = decay_conversion (exp);
1700 type = TREE_TYPE (exp);
1701 code = TREE_CODE (type);
1703 if (INTEGRAL_CODE_P (code))
1705 tree t = type_promotes_to (type);
1707 return cp_convert (t, exp);
1713 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1717 inline_conversion (exp)
1720 if (TREE_CODE (exp) == FUNCTION_DECL)
1721 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1726 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1727 decay_conversion to one. */
1730 string_conv_p (totype, exp, warn)
1736 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1739 t = TREE_TYPE (totype);
1740 if (!same_type_p (t, char_type_node)
1741 && !same_type_p (t, wchar_type_node))
1744 if (TREE_CODE (exp) == STRING_CST)
1746 /* Make sure that we don't try to convert between char and wchar_t. */
1747 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1752 /* Is this a string constant which has decayed to 'const char *'? */
1753 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1754 if (!same_type_p (TREE_TYPE (exp), t))
1757 if (TREE_CODE (exp) != ADDR_EXPR
1758 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1762 /* This warning is not very useful, as it complains about printf. */
1763 if (warn && warn_write_strings)
1764 warning ("deprecated conversion from string constant to `%T'", totype);
1769 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1770 can, for example, use as an lvalue. This code used to be in
1771 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1772 expressions, where we're dealing with aggregates. But now it's again only
1773 called from unary_complex_lvalue. The case (in particular) that led to
1774 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1778 rationalize_conditional_expr (code, t)
1779 enum tree_code code;
1782 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1783 the first operand is always the one to be used if both operands
1784 are equal, so we know what conditional expression this used to be. */
1785 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1788 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1789 ? LE_EXPR : GE_EXPR),
1790 TREE_OPERAND (t, 0),
1791 TREE_OPERAND (t, 1)),
1792 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1793 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1797 build_conditional_expr (TREE_OPERAND (t, 0),
1798 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1799 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1802 /* Given the TYPE of an anonymous union field inside T, return the
1803 FIELD_DECL for the field. If not found return NULL_TREE. Because
1804 anonymous unions can nest, we must also search all anonymous unions
1805 that are directly reachable. */
1808 lookup_anon_field (t, type)
1813 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1815 if (TREE_STATIC (field))
1817 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1820 /* If we find it directly, return the field. */
1821 if (DECL_NAME (field) == NULL_TREE
1822 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1827 /* Otherwise, it could be nested, search harder. */
1828 if (DECL_NAME (field) == NULL_TREE
1829 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1831 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1839 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1840 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1841 non-NULL, it indicates the path to the base used to name MEMBER.
1842 If PRESERVE_REFERENCE is true, the expression returned will have
1843 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1844 returned will have the type referred to by the reference.
1846 This function does not perform access control; that is either done
1847 earlier by the parser when the name of MEMBER is resolved to MEMBER
1848 itself, or later when overload resolution selects one of the
1849 functions indicated by MEMBER. */
1852 build_class_member_access_expr (tree object, tree member,
1853 tree access_path, bool preserve_reference)
1857 tree result = NULL_TREE;
1859 if (object == error_mark_node || member == error_mark_node)
1860 return error_mark_node;
1862 if (TREE_CODE (member) == PSEUDO_DTOR_EXPR)
1865 my_friendly_assert (DECL_P (member) || BASELINK_P (member),
1870 The type of the first expression shall be "class object" (of a
1872 object_type = TREE_TYPE (object);
1873 if (!complete_type_or_else (object_type, object))
1874 return error_mark_node;
1875 if (!CLASS_TYPE_P (object_type))
1877 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1878 member, object, object_type);
1879 return error_mark_node;
1882 /* The standard does not seem to actually say that MEMBER must be a
1883 member of OBJECT_TYPE. However, that is clearly what is
1885 if (DECL_P (member))
1887 member_scope = DECL_CLASS_CONTEXT (member);
1889 if (TREE_DEPRECATED (member))
1890 warn_deprecated_use (member);
1893 member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1894 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1895 presently be the anonymous union. Go outwards until we find a
1896 type related to OBJECT_TYPE. */
1897 while (ANON_AGGR_TYPE_P (member_scope)
1898 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1900 member_scope = TYPE_CONTEXT (member_scope);
1901 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1903 error ("`%D' is not a member of `%T'", member, object_type);
1904 return error_mark_node;
1907 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1908 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
1909 in the frontend; only _DECLs and _REFs are lvalues in the backend. */
1911 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1913 object = build_indirect_ref (temp, NULL);
1916 /* In [expr.ref], there is an explicit list of the valid choices for
1917 MEMBER. We check for each of those cases here. */
1918 if (TREE_CODE (member) == VAR_DECL)
1920 /* A static data member. */
1922 /* If OBJECT has side-effects, they are supposed to occur. */
1923 if (TREE_SIDE_EFFECTS (object))
1924 result = build (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1926 else if (TREE_CODE (member) == FIELD_DECL)
1928 /* A non-static data member. */
1933 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1934 && integer_zerop (TREE_OPERAND (object, 0)));
1936 /* Convert OBJECT to the type of MEMBER. */
1937 if (!same_type_p (object_type, member_scope))
1942 binfo = lookup_base (access_path ? access_path : object_type,
1943 member_scope, ba_ignore, &kind);
1944 if (binfo == error_mark_node)
1945 return error_mark_node;
1947 /* It is invalid to use to try to get to a virtual base of a
1948 NULL object. The most common cause is invalid use of
1950 if (null_object_p && kind == bk_via_virtual)
1952 error ("invalid access to non-static data member `%D' of NULL object",
1954 error ("(perhaps the `offsetof' macro was used incorrectly)");
1955 return error_mark_node;
1958 /* Convert to the base. */
1959 object = build_base_path (PLUS_EXPR, object, binfo,
1961 /* If we found the base successfully then we should be able
1962 to convert to it successfully. */
1963 my_friendly_assert (object != error_mark_node,
1967 /* Complain about other invalid uses of offsetof, even though they will
1968 give the right answer. Note that we complain whether or not they
1969 actually used the offsetof macro, since there's no way to know at this
1970 point. So we just give a warning, instead of a pedwarn. */
1971 if (null_object_p && CLASSTYPE_NON_POD_P (object_type))
1973 warning ("invalid access to non-static data member `%D' of NULL object",
1975 warning ("(perhaps the `offsetof' macro was used incorrectly)");
1978 /* If MEMBER is from an anonymous aggregate, we have converted
1979 OBJECT so that it refers to the class containing the
1980 anonymous union. Generate a reference to the anonymous union
1981 itself, and recur to find MEMBER. */
1982 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1983 /* When this code is called from build_field_call, the
1984 object already has the type of the anonymous union.
1985 That is because the COMPONENT_REF was already
1986 constructed, and was then disassembled before calling
1987 build_field_call. After the function-call code is
1988 cleaned up, this waste can be eliminated. */
1989 && (!same_type_ignoring_top_level_qualifiers_p
1990 (TREE_TYPE (object), DECL_CONTEXT (member))))
1992 tree anonymous_union;
1994 anonymous_union = lookup_anon_field (TREE_TYPE (object),
1995 DECL_CONTEXT (member));
1996 object = build_class_member_access_expr (object,
1998 /*access_path=*/NULL_TREE,
1999 preserve_reference);
2002 /* Compute the type of the field, as described in [expr.ref]. */
2003 type_quals = TYPE_UNQUALIFIED;
2004 member_type = TREE_TYPE (member);
2005 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2007 type_quals = (cp_type_quals (member_type)
2008 | cp_type_quals (object_type));
2010 /* A field is const (volatile) if the enclosing object, or the
2011 field itself, is const (volatile). But, a mutable field is
2012 not const, even within a const object. */
2013 if (DECL_MUTABLE_P (member))
2014 type_quals &= ~TYPE_QUAL_CONST;
2015 member_type = cp_build_qualified_type (member_type, type_quals);
2018 result = fold (build (COMPONENT_REF, member_type, object, member));
2020 /* Mark the expression const or volatile, as appropriate. Even
2021 though we've dealt with the type above, we still have to mark the
2022 expression itself. */
2023 if (type_quals & TYPE_QUAL_CONST)
2024 TREE_READONLY (result) = 1;
2025 else if (type_quals & TYPE_QUAL_VOLATILE)
2026 TREE_THIS_VOLATILE (result) = 1;
2028 else if (BASELINK_P (member))
2030 /* The member is a (possibly overloaded) member function. */
2034 /* If the MEMBER is exactly one static member function, then we
2035 know the type of the expression. Otherwise, we must wait
2036 until overload resolution has been performed. */
2037 functions = BASELINK_FUNCTIONS (member);
2038 if (TREE_CODE (functions) == FUNCTION_DECL
2039 && DECL_STATIC_FUNCTION_P (functions))
2040 type = TREE_TYPE (functions);
2042 type = unknown_type_node;
2043 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2044 base. That will happen when the function is called. */
2045 result = build (COMPONENT_REF, type, object, member);
2047 else if (TREE_CODE (member) == CONST_DECL)
2049 /* The member is an enumerator. */
2051 /* If OBJECT has side-effects, they are supposed to occur. */
2052 if (TREE_SIDE_EFFECTS (object))
2053 result = build (COMPOUND_EXPR, TREE_TYPE (result),
2058 error ("invalid use of `%D'", member);
2059 return error_mark_node;
2062 if (!preserve_reference)
2065 If E2 is declared to have type "reference to T", then ... the
2066 type of E1.E2 is T. */
2067 result = convert_from_reference (result);
2072 /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
2073 SCOPE is NULL, by OBJECT.~DTOR_NAME. */
2076 lookup_destructor (tree object, tree scope, tree dtor_name)
2078 tree object_type = TREE_TYPE (object);
2079 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2081 if (scope && !check_dtor_name (scope, dtor_name))
2083 error ("qualified type `%T' does not match destructor name `~%T'",
2085 return error_mark_node;
2087 if (!same_type_p (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2089 error ("destructor name `%T' does not match type `%T' of expression",
2090 dtor_type, object_type);
2091 return error_mark_node;
2093 if (!TYPE_HAS_DESTRUCTOR (object_type))
2094 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope,
2096 return lookup_member (object_type, complete_dtor_identifier,
2097 /*protect=*/1, /*want_type=*/0);
2100 /* This function is called by the parser to process a class member
2101 access expression of the form OBJECT.NAME. NAME is a node used by
2102 the parser to represent a name; it is not yet a DECL. It may,
2103 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2104 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2105 there is no reason to do the lookup twice, so the parser keeps the
2109 finish_class_member_access_expr (tree object, tree name)
2113 tree access_path = NULL_TREE;
2115 if (object == error_mark_node || name == error_mark_node)
2116 return error_mark_node;
2118 if (processing_template_decl)
2119 return build_min_nt (COMPONENT_REF, object, name);
2121 if (TREE_CODE (object) == OFFSET_REF)
2122 object = resolve_offset_ref (object);
2124 object_type = TREE_TYPE (object);
2125 if (TREE_CODE (object_type) == REFERENCE_TYPE)
2127 object = convert_from_reference (object);
2128 object_type = TREE_TYPE (object);
2133 The type of the first expression shall be "class object" (of a
2135 if (!complete_type_or_else (object_type, object))
2136 return error_mark_node;
2137 if (!CLASS_TYPE_P (object_type))
2139 error ("request for member `%D' in `%E', which is of non-class type `%T'",
2140 name, object, object_type);
2141 return error_mark_node;
2144 if (BASELINK_P (name))
2146 /* A member function that has already been looked up. */
2147 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
2148 == TEMPLATE_ID_EXPR),
2154 bool is_template_id = false;
2155 tree template_args = NULL_TREE;
2157 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2159 is_template_id = true;
2160 template_args = TREE_OPERAND (name, 1);
2161 name = TREE_OPERAND (name, 0);
2164 if (TREE_CODE (name) == SCOPE_REF)
2168 /* A qualified name. The qualifying class or namespace `S' has
2169 already been looked up; it is either a TYPE or a
2170 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
2171 or a BIT_NOT_EXPR. */
2172 scope = TREE_OPERAND (name, 0);
2173 name = TREE_OPERAND (name, 1);
2174 my_friendly_assert ((CLASS_TYPE_P (scope)
2175 || TREE_CODE (scope) == NAMESPACE_DECL),
2177 my_friendly_assert ((TREE_CODE (name) == IDENTIFIER_NODE
2178 || TREE_CODE (name) == BIT_NOT_EXPR),
2181 /* If SCOPE is a namespace, then the qualified name does not
2182 name a member of OBJECT_TYPE. */
2183 if (TREE_CODE (scope) == NAMESPACE_DECL)
2185 error ("`%D::%D' is not a member of `%T'",
2186 scope, name, object_type);
2187 return error_mark_node;
2190 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2191 access_path = lookup_base (object_type, scope, ba_check, NULL);
2192 if (!access_path || access_path == error_mark_node)
2193 return error_mark_node;
2195 if (TREE_CODE (name) == BIT_NOT_EXPR)
2196 member = lookup_destructor (object, scope, name);
2199 /* Look up the member. */
2200 member = lookup_member (access_path, name, /*protect=*/1,
2202 if (member == NULL_TREE)
2204 error ("'%D' has no member named '%E'", object_type, name);
2205 return error_mark_node;
2207 if (member == error_mark_node)
2208 return error_mark_node;
2211 else if (TREE_CODE (name) == BIT_NOT_EXPR)
2212 member = lookup_destructor (object, /*scope=*/NULL_TREE, name);
2213 else if (TREE_CODE (name) == IDENTIFIER_NODE)
2215 /* An unqualified name. */
2216 member = lookup_member (object_type, name, /*protect=*/1,
2218 if (member == NULL_TREE)
2220 error ("'%D' has no member named '%E'", object_type, name);
2221 return error_mark_node;
2223 else if (member == error_mark_node)
2224 return error_mark_node;
2228 /* The YACC parser sometimes gives us things that are not names.
2229 These always indicate errors. The recursive-descent parser
2230 does not do this, so this code can go away once that parser
2231 replaces the YACC parser. */
2232 error ("invalid use of `%D'", name);
2233 return error_mark_node;
2238 tree template = member;
2240 if (BASELINK_P (template))
2241 BASELINK_FUNCTIONS (template)
2242 = build_nt (TEMPLATE_ID_EXPR,
2243 BASELINK_FUNCTIONS (template),
2247 error ("`%D' is not a member template function", name);
2248 return error_mark_node;
2253 if (TREE_DEPRECATED (member))
2254 warn_deprecated_use (member);
2256 return build_class_member_access_expr (object, member, access_path,
2257 /*preserve_reference=*/false);
2260 /* Return an expression for the MEMBER_NAME field in the internal
2261 representation of PTRMEM, a pointer-to-member function. (Each
2262 pointer-to-member function type gets its own RECORD_TYPE so it is
2263 more convenient to access the fields by name than by FIELD_DECL.)
2264 This routine converts the NAME to a FIELD_DECL and then creates the
2265 node for the complete expression. */
2268 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2274 /* This code is a stripped down version of
2275 build_class_member_access_expr. It does not work to use that
2276 routine directly because it expects the object to be of class
2278 ptrmem_type = TREE_TYPE (ptrmem);
2279 my_friendly_assert (TYPE_PTRMEMFUNC_P (ptrmem_type), 20020804);
2280 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2282 member_type = cp_build_qualified_type (TREE_TYPE (member),
2283 cp_type_quals (ptrmem_type));
2284 return fold (build (COMPONENT_REF, member_type, ptrmem, member));
2287 /* Given an expression PTR for a pointer, return an expression
2288 for the value pointed to.
2289 ERRORSTRING is the name of the operator to appear in error messages.
2291 This function may need to overload OPERATOR_FNNAME.
2292 Must also handle REFERENCE_TYPEs for C++. */
2295 build_x_indirect_ref (ptr, errorstring)
2297 const char *errorstring;
2301 if (processing_template_decl)
2302 return build_min_nt (INDIRECT_REF, ptr);
2304 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2308 return build_indirect_ref (ptr, errorstring);
2312 build_indirect_ref (ptr, errorstring)
2314 const char *errorstring;
2316 register tree pointer, type;
2318 if (ptr == error_mark_node)
2319 return error_mark_node;
2321 if (ptr == current_class_ptr)
2322 return current_class_ref;
2324 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2325 ? ptr : default_conversion (ptr));
2326 type = TREE_TYPE (pointer);
2328 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2332 If the type of the expression is "pointer to T," the type
2333 of the result is "T."
2335 We must use the canonical variant because certain parts of
2336 the back end, like fold, do pointer comparisons between
2338 tree t = canonical_type_variant (TREE_TYPE (type));
2340 if (VOID_TYPE_P (t))
2342 /* A pointer to incomplete type (other than cv void) can be
2343 dereferenced [expr.unary.op]/1 */
2344 error ("`%T' is not a pointer-to-object type", type);
2345 return error_mark_node;
2347 else if (TREE_CODE (pointer) == ADDR_EXPR
2349 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2350 /* The POINTER was something like `&x'. We simplify `*&x' to
2352 return TREE_OPERAND (pointer, 0);
2355 tree ref = build1 (INDIRECT_REF, t, pointer);
2357 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2358 so that we get the proper error message if the result is used
2359 to assign to. Also, &* is supposed to be a no-op. */
2360 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2361 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2362 TREE_SIDE_EFFECTS (ref)
2363 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2368 /* `pointer' won't be an error_mark_node if we were given a
2369 pointer to member, so it's cool to check for this here. */
2370 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2371 error ("invalid use of `%s' on pointer to member", errorstring);
2372 else if (pointer != error_mark_node)
2375 error ("invalid type argument of `%s'", errorstring);
2377 error ("invalid type argument");
2379 return error_mark_node;
2382 /* This handles expressions of the form "a[i]", which denotes
2385 This is logically equivalent in C to *(a+i), but we may do it differently.
2386 If A is a variable or a member, we generate a primitive ARRAY_REF.
2387 This avoids forcing the array out of registers, and can work on
2388 arrays that are not lvalues (for example, members of structures returned
2391 If INDEX is of some user-defined type, it must be converted to
2392 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2393 will inherit the type of the array, which will be some pointer type. */
2396 build_array_ref (array, idx)
2401 error ("subscript missing in array reference");
2402 return error_mark_node;
2405 if (TREE_TYPE (array) == error_mark_node
2406 || TREE_TYPE (idx) == error_mark_node)
2407 return error_mark_node;
2409 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2411 switch (TREE_CODE (array))
2415 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2416 return build (COMPOUND_EXPR, TREE_TYPE (value),
2417 TREE_OPERAND (array, 0), value);
2421 return build_conditional_expr
2422 (TREE_OPERAND (array, 0),
2423 build_array_ref (TREE_OPERAND (array, 1), idx),
2424 build_array_ref (TREE_OPERAND (array, 2), idx));
2430 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2431 && TREE_CODE (array) != INDIRECT_REF)
2435 /* Subscripting with type char is likely to lose
2436 on a machine where chars are signed.
2437 So warn on any machine, but optionally.
2438 Don't warn for unsigned char since that type is safe.
2439 Don't warn for signed char because anyone who uses that
2440 must have done so deliberately. */
2441 if (warn_char_subscripts
2442 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2443 warning ("array subscript has type `char'");
2445 /* Apply default promotions *after* noticing character types. */
2446 idx = default_conversion (idx);
2448 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2450 error ("array subscript is not an integer");
2451 return error_mark_node;
2454 /* An array that is indexed by a non-constant
2455 cannot be stored in a register; we must be able to do
2456 address arithmetic on its address.
2457 Likewise an array of elements of variable size. */
2458 if (TREE_CODE (idx) != INTEGER_CST
2459 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2460 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2463 if (!cxx_mark_addressable (array))
2464 return error_mark_node;
2467 /* An array that is indexed by a constant value which is not within
2468 the array bounds cannot be stored in a register either; because we
2469 would get a crash in store_bit_field/extract_bit_field when trying
2470 to access a non-existent part of the register. */
2471 if (TREE_CODE (idx) == INTEGER_CST
2472 && TYPE_VALUES (TREE_TYPE (array))
2473 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2475 if (!cxx_mark_addressable (array))
2476 return error_mark_node;
2479 if (pedantic && !lvalue_p (array))
2480 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2482 /* Note in C++ it is valid to subscript a `register' array, since
2483 it is valid to take the address of something with that
2484 storage specification. */
2488 while (TREE_CODE (foo) == COMPONENT_REF)
2489 foo = TREE_OPERAND (foo, 0);
2490 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2491 warning ("subscripting array declared `register'");
2494 type = TREE_TYPE (TREE_TYPE (array));
2495 rval = build (ARRAY_REF, type, array, idx);
2496 /* Array ref is const/volatile if the array elements are
2497 or if the array is.. */
2498 TREE_READONLY (rval)
2499 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2500 TREE_SIDE_EFFECTS (rval)
2501 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2502 TREE_THIS_VOLATILE (rval)
2503 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2504 return require_complete_type (fold (rval));
2508 tree ar = default_conversion (array);
2509 tree ind = default_conversion (idx);
2511 /* Put the integer in IND to simplify error checking. */
2512 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2519 if (ar == error_mark_node)
2522 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2524 error ("subscripted value is neither array nor pointer");
2525 return error_mark_node;
2527 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2529 error ("array subscript is not an integer");
2530 return error_mark_node;
2533 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2538 /* Resolve a pointer to member function. INSTANCE is the object
2539 instance to use, if the member points to a virtual member.
2541 This used to avoid checking for virtual functions if basetype
2542 has no virtual functions, according to an earlier ANSI draft.
2543 With the final ISO C++ rules, such an optimization is
2544 incorrect: A pointer to a derived member can be static_cast
2545 to pointer-to-base-member, as long as the dynamic object
2546 later has the right member. */
2549 get_member_function_from_ptrfunc (instance_ptrptr, function)
2550 tree *instance_ptrptr;
2553 if (TREE_CODE (function) == OFFSET_REF)
2554 function = TREE_OPERAND (function, 1);
2556 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2558 tree idx, delta, e1, e2, e3, vtbl, basetype;
2559 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2561 tree instance_ptr = *instance_ptrptr;
2562 tree instance_save_expr = 0;
2563 if (instance_ptr == error_mark_node)
2565 if (TREE_CODE (function) == PTRMEM_CST)
2567 /* Extracting the function address from a pmf is only
2568 allowed with -Wno-pmf-conversions. It only works for
2570 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2571 e1 = convert (fntype, e1);
2576 error ("object missing in use of `%E'", function);
2577 return error_mark_node;
2581 if (TREE_SIDE_EFFECTS (instance_ptr))
2582 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2584 if (TREE_SIDE_EFFECTS (function))
2585 function = save_expr (function);
2587 /* Start by extracting all the information from the PMF itself. */
2588 e3 = PFN_FROM_PTRMEMFUNC (function);
2589 delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2590 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2591 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2593 case ptrmemfunc_vbit_in_pfn:
2594 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2595 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2598 case ptrmemfunc_vbit_in_delta:
2599 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2600 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2607 /* Convert down to the right base before using the instance. First
2609 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2610 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2611 basetype, ba_check, NULL);
2612 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1);
2613 if (instance_ptr == error_mark_node)
2614 return error_mark_node;
2615 /* ...and then the delta in the PMF. */
2616 instance_ptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2617 instance_ptr, delta);
2619 /* Hand back the adjusted 'this' argument to our caller. */
2620 *instance_ptrptr = instance_ptr;
2622 /* Next extract the vtable pointer from the object. */
2623 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2625 vtbl = build_indirect_ref (vtbl, NULL);
2627 /* Finally, extract the function pointer from the vtable. */
2628 e2 = fold (build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx));
2629 e2 = build_indirect_ref (e2, NULL);
2630 TREE_CONSTANT (e2) = 1;
2632 /* When using function descriptors, the address of the
2633 vtable entry is treated as a function pointer. */
2634 if (TARGET_VTABLE_USES_DESCRIPTORS)
2635 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2636 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2638 TREE_TYPE (e2) = TREE_TYPE (e3);
2639 e1 = build_conditional_expr (e1, e2, e3);
2641 /* Make sure this doesn't get evaluated first inside one of the
2642 branches of the COND_EXPR. */
2643 if (instance_save_expr)
2644 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2645 instance_save_expr, e1);
2653 build_function_call_real (function, params, require_complete, flags)
2654 tree function, params;
2655 int require_complete, flags;
2657 register tree fntype, fndecl;
2658 register tree value_type;
2659 register tree coerced_params;
2661 tree name = NULL_TREE, assembler_name = NULL_TREE;
2663 tree original = function;
2665 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2666 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2667 if (TREE_CODE (function) == NOP_EXPR
2668 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2669 function = TREE_OPERAND (function, 0);
2671 if (TREE_CODE (function) == FUNCTION_DECL)
2673 name = DECL_NAME (function);
2674 assembler_name = DECL_ASSEMBLER_NAME (function);
2676 mark_used (function);
2679 /* Convert anything with function type to a pointer-to-function. */
2680 if (pedantic && DECL_MAIN_P (function))
2681 pedwarn ("ISO C++ forbids calling `::main' from within program");
2683 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2684 (because calling an inline function does not mean the function
2685 needs to be separately compiled). */
2687 if (DECL_INLINE (function))
2688 function = inline_conversion (function);
2690 function = build_addr_func (function);
2696 function = build_addr_func (function);
2699 if (function == error_mark_node)
2700 return error_mark_node;
2702 fntype = TREE_TYPE (function);
2704 if (TYPE_PTRMEMFUNC_P (fntype))
2706 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2708 return error_mark_node;
2711 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2712 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2714 if (!((TREE_CODE (fntype) == POINTER_TYPE
2715 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2717 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2719 error ("`%E' cannot be used as a function", original);
2720 return error_mark_node;
2723 /* fntype now gets the type of function pointed to. */
2724 fntype = TREE_TYPE (fntype);
2726 /* Convert the parameters to the types declared in the
2727 function prototype, or apply default promotions. */
2729 if (flags & LOOKUP_COMPLAIN)
2730 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2731 params, fndecl, LOOKUP_NORMAL);
2733 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2736 if (coerced_params == error_mark_node)
2738 if (flags & LOOKUP_SPECULATIVELY)
2741 return error_mark_node;
2744 /* Check for errors in format strings. */
2747 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
2749 /* Recognize certain built-in functions so we can make tree-codes
2750 other than CALL_EXPR. We do this when it enables fold-const.c
2751 to do something useful. */
2753 if (TREE_CODE (function) == ADDR_EXPR
2754 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2755 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2757 result = expand_tree_builtin (TREE_OPERAND (function, 0),
2758 params, coerced_params);
2763 /* Some built-in function calls will be evaluated at
2764 compile-time in fold (). */
2765 result = fold (build_call (function, coerced_params));
2766 value_type = TREE_TYPE (result);
2768 if (require_complete)
2770 if (TREE_CODE (value_type) == VOID_TYPE)
2772 result = require_complete_type (result);
2774 if (IS_AGGR_TYPE (value_type))
2775 result = build_cplus_new (value_type, result);
2776 return convert_from_reference (result);
2780 build_function_call (function, params)
2781 tree function, params;
2783 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2786 /* Convert the actual parameter expressions in the list VALUES
2787 to the types in the list TYPELIST.
2788 If parmdecls is exhausted, or when an element has NULL as its type,
2789 perform the default conversions.
2791 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2793 This is also where warnings about wrong number of args are generated.
2795 Return a list of expressions for the parameters as converted.
2797 Both VALUES and the returned value are chains of TREE_LIST nodes
2798 with the elements of the list in the TREE_VALUE slots of those nodes.
2800 In C++, unspecified trailing parameters can be filled in with their
2801 default arguments, if such were specified. Do so here. */
2804 convert_arguments (typelist, values, fndecl, flags)
2805 tree typelist, values, fndecl;
2808 register tree typetail, valtail;
2809 register tree result = NULL_TREE;
2810 const char *called_thing = 0;
2813 /* Argument passing is always copy-initialization. */
2814 flags |= LOOKUP_ONLYCONVERTING;
2818 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2820 if (DECL_NAME (fndecl) == NULL_TREE
2821 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2822 called_thing = "constructor";
2824 called_thing = "member function";
2827 called_thing = "function";
2830 for (valtail = values, typetail = typelist;
2832 valtail = TREE_CHAIN (valtail), i++)
2834 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2835 register tree val = TREE_VALUE (valtail);
2837 if (val == error_mark_node)
2838 return error_mark_node;
2840 if (type == void_type_node)
2844 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
2846 error ("at this point in file");
2849 error ("too many arguments to function");
2850 /* In case anybody wants to know if this argument
2853 TREE_TYPE (tree_last (result)) = error_mark_node;
2857 if (TREE_CODE (val) == OFFSET_REF)
2858 val = resolve_offset_ref (val);
2860 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2861 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2862 if (TREE_CODE (val) == NOP_EXPR
2863 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2864 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2865 val = TREE_OPERAND (val, 0);
2867 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2869 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2870 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2871 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2872 val = default_conversion (val);
2875 if (val == error_mark_node)
2876 return error_mark_node;
2880 /* Formal parm type is specified by a function prototype. */
2883 if (!COMPLETE_TYPE_P (complete_type (type)))
2885 error ("parameter type of called function is incomplete");
2890 parmval = convert_for_initialization
2891 (NULL_TREE, type, val, flags,
2892 "argument passing", fndecl, i);
2893 parmval = convert_for_arg_passing (type, parmval);
2896 if (parmval == error_mark_node)
2897 return error_mark_node;
2899 result = tree_cons (NULL_TREE, parmval, result);
2903 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2904 val = convert_from_reference (val);
2906 if (fndecl && DECL_BUILT_IN (fndecl)
2907 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2908 /* Don't do ellipsis conversion for __built_in_constant_p
2909 as this will result in spurious warnings for non-POD
2911 val = require_complete_type (val);
2913 val = convert_arg_to_ellipsis (val);
2915 result = tree_cons (NULL_TREE, val, result);
2919 typetail = TREE_CHAIN (typetail);
2922 if (typetail != 0 && typetail != void_list_node)
2924 /* See if there are default arguments that can be used */
2925 if (TREE_PURPOSE (typetail)
2926 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
2928 for (; typetail != void_list_node; ++i)
2931 = convert_default_arg (TREE_VALUE (typetail),
2932 TREE_PURPOSE (typetail),
2935 if (parmval == error_mark_node)
2936 return error_mark_node;
2938 result = tree_cons (0, parmval, result);
2939 typetail = TREE_CHAIN (typetail);
2940 /* ends with `...'. */
2941 if (typetail == NULL_TREE)
2949 cp_error_at ("too few arguments to %s `%+#D'",
2950 called_thing, fndecl);
2951 error ("at this point in file");
2954 error ("too few arguments to function");
2955 return error_mark_list;
2959 return nreverse (result);
2962 /* Build a binary-operation expression, after performing default
2963 conversions on the operands. CODE is the kind of expression to build. */
2966 build_x_binary_op (code, arg1, arg2)
2967 enum tree_code code;
2970 if (processing_template_decl)
2971 return build_min_nt (code, arg1, arg2);
2973 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2976 /* Build a binary-operation expression without default conversions.
2977 CODE is the kind of expression to build.
2978 This function differs from `build' in several ways:
2979 the data type of the result is computed and recorded in it,
2980 warnings are generated if arg data types are invalid,
2981 special handling for addition and subtraction of pointers is known,
2982 and some optimization is done (operations on narrow ints
2983 are done in the narrower type when that gives the same result).
2984 Constant folding is also done before the result is returned.
2986 Note that the operands will never have enumeral types
2987 because either they have just had the default conversions performed
2988 or they have both just been converted to some other type in which
2989 the arithmetic is to be done.
2991 C++: must do special pointer arithmetic when implementing
2992 multiple inheritance, and deal with pointer to member functions. */
2995 build_binary_op (code, orig_op0, orig_op1, convert_p)
2996 enum tree_code code;
2997 tree orig_op0, orig_op1;
2998 int convert_p ATTRIBUTE_UNUSED;
3001 register enum tree_code code0, code1;
3004 /* Expression code to give to the expression when it is built.
3005 Normally this is CODE, which is what the caller asked for,
3006 but in some special cases we change it. */
3007 register enum tree_code resultcode = code;
3009 /* Data type in which the computation is to be performed.
3010 In the simplest cases this is the common type of the arguments. */
3011 register tree result_type = NULL;
3013 /* Nonzero means operands have already been type-converted
3014 in whatever way is necessary.
3015 Zero means they need to be converted to RESULT_TYPE. */
3018 /* Nonzero means create the expression with this type, rather than
3020 tree build_type = 0;
3022 /* Nonzero means after finally constructing the expression
3023 convert it to this type. */
3024 tree final_type = 0;
3026 /* Nonzero if this is an operation like MIN or MAX which can
3027 safely be computed in short if both args are promoted shorts.
3028 Also implies COMMON.
3029 -1 indicates a bitwise operation; this makes a difference
3030 in the exact conditions for when it is safe to do the operation
3031 in a narrower mode. */
3034 /* Nonzero if this is a comparison operation;
3035 if both args are promoted shorts, compare the original shorts.
3036 Also implies COMMON. */
3037 int short_compare = 0;
3039 /* Nonzero if this is a right-shift operation, which can be computed on the
3040 original short and then promoted if the operand is a promoted short. */
3041 int short_shift = 0;
3043 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3046 /* Apply default conversions. */
3050 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3051 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3052 || code == TRUTH_XOR_EXPR)
3054 if (!really_overloaded_fn (op0))
3055 op0 = decay_conversion (op0);
3056 if (!really_overloaded_fn (op1))
3057 op1 = decay_conversion (op1);
3061 if (!really_overloaded_fn (op0))
3062 op0 = default_conversion (op0);
3063 if (!really_overloaded_fn (op1))
3064 op1 = default_conversion (op1);
3067 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3068 STRIP_TYPE_NOPS (op0);
3069 STRIP_TYPE_NOPS (op1);
3071 /* DTRT if one side is an overloaded function, but complain about it. */
3072 if (type_unknown_p (op0))
3074 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3075 if (t != error_mark_node)
3077 pedwarn ("assuming cast to type `%T' from overloaded function",
3082 if (type_unknown_p (op1))
3084 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3085 if (t != error_mark_node)
3087 pedwarn ("assuming cast to type `%T' from overloaded function",
3093 type0 = TREE_TYPE (op0);
3094 type1 = TREE_TYPE (op1);
3096 /* The expression codes of the data types of the arguments tell us
3097 whether the arguments are integers, floating, pointers, etc. */
3098 code0 = TREE_CODE (type0);
3099 code1 = TREE_CODE (type1);
3101 /* If an error was already reported for one of the arguments,
3102 avoid reporting another error. */
3104 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3105 return error_mark_node;
3110 /* Handle the pointer + int case. */
3111 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3112 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
3113 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3114 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
3120 /* Subtraction of two similar pointers.
3121 We must subtract them as integers, then divide by object size. */
3122 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3123 && comp_target_types (type0, type1, 1))
3124 return pointer_diff (op0, op1, common_type (type0, type1));
3125 /* Handle pointer minus int. Just like pointer plus int. */
3126 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3127 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
3136 case TRUNC_DIV_EXPR:
3138 case FLOOR_DIV_EXPR:
3139 case ROUND_DIV_EXPR:
3140 case EXACT_DIV_EXPR:
3141 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3142 || code0 == COMPLEX_TYPE)
3143 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3144 || code1 == COMPLEX_TYPE))
3146 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3147 warning ("division by zero in `%E / 0'", op0);
3148 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3149 warning ("division by zero in `%E / 0.'", op0);
3151 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3152 resultcode = RDIV_EXPR;
3154 /* When dividing two signed integers, we have to promote to int.
3155 unless we divide by a constant != -1. Note that default
3156 conversion will have been performed on the operands at this
3157 point, so we have to dig out the original type to find out if
3159 shorten = ((TREE_CODE (op0) == NOP_EXPR
3160 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3161 || (TREE_CODE (op1) == INTEGER_CST
3162 && ! integer_all_onesp (op1)));
3169 case BIT_ANDTC_EXPR:
3172 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3176 case TRUNC_MOD_EXPR:
3177 case FLOOR_MOD_EXPR:
3178 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3179 warning ("division by zero in `%E %% 0'", op0);
3180 else if (code1 == REAL_TYPE && real_zerop (op1))
3181 warning ("division by zero in `%E %% 0.'", op0);
3183 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3185 /* Although it would be tempting to shorten always here, that loses
3186 on some targets, since the modulo instruction is undefined if the
3187 quotient can't be represented in the computation mode. We shorten
3188 only if unsigned or if dividing by something we know != -1. */
3189 shorten = ((TREE_CODE (op0) == NOP_EXPR
3190 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3191 || (TREE_CODE (op1) == INTEGER_CST
3192 && ! integer_all_onesp (op1)));
3197 case TRUTH_ANDIF_EXPR:
3198 case TRUTH_ORIF_EXPR:
3199 case TRUTH_AND_EXPR:
3201 result_type = boolean_type_node;
3204 /* Shift operations: result has same type as first operand;
3205 always convert second operand to int.
3206 Also set SHORT_SHIFT if shifting rightward. */
3209 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3211 result_type = type0;
3212 if (TREE_CODE (op1) == INTEGER_CST)
3214 if (tree_int_cst_lt (op1, integer_zero_node))
3215 warning ("right shift count is negative");
3218 if (! integer_zerop (op1))
3220 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3221 warning ("right shift count >= width of type");
3224 /* Convert the shift-count to an integer, regardless of
3225 size of value being shifted. */
3226 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3227 op1 = cp_convert (integer_type_node, op1);
3228 /* Avoid converting op1 to result_type later. */
3234 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3236 result_type = type0;
3237 if (TREE_CODE (op1) == INTEGER_CST)
3239 if (tree_int_cst_lt (op1, integer_zero_node))
3240 warning ("left shift count is negative");
3241 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3242 warning ("left shift count >= width of type");
3244 /* Convert the shift-count to an integer, regardless of
3245 size of value being shifted. */
3246 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3247 op1 = cp_convert (integer_type_node, op1);
3248 /* Avoid converting op1 to result_type later. */
3255 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3257 result_type = type0;
3258 if (TREE_CODE (op1) == INTEGER_CST)
3260 if (tree_int_cst_lt (op1, integer_zero_node))
3261 warning ("%s rotate count is negative",
3262 (code == LROTATE_EXPR) ? "left" : "right");
3263 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3264 warning ("%s rotate count >= width of type",
3265 (code == LROTATE_EXPR) ? "left" : "right");
3267 /* Convert the shift-count to an integer, regardless of
3268 size of value being shifted. */
3269 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3270 op1 = cp_convert (integer_type_node, op1);
3276 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3277 warning ("comparing floating point with == or != is unsafe");
3279 build_type = boolean_type_node;
3280 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3281 || code0 == COMPLEX_TYPE)
3282 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3283 || code1 == COMPLEX_TYPE))
3285 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3286 result_type = composite_pointer_type (type0, type1, op0, op1,
3288 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3289 result_type = type0;
3290 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3291 result_type = type1;
3292 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3294 result_type = type0;
3295 error ("ISO C++ forbids comparison between pointer and integer");
3297 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3299 result_type = type1;
3300 error ("ISO C++ forbids comparison between pointer and integer");
3302 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3304 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3305 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3306 result_type = TREE_TYPE (op0);
3308 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3309 return cp_build_binary_op (code, op1, op0);
3310 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3311 && same_type_p (type0, type1))
3313 /* E will be the final comparison. */
3315 /* E1 and E2 are for scratch. */
3323 if (TREE_SIDE_EFFECTS (op0))
3324 op0 = save_expr (op0);
3325 if (TREE_SIDE_EFFECTS (op1))
3326 op1 = save_expr (op1);
3331 && (!op0.pfn || op0.delta == op1.delta))
3333 The reason for the `!op0.pfn' bit is that a NULL
3334 pointer-to-member is any member with a zero PFN; the
3335 DELTA field is unspecified. */
3336 pfn0 = pfn_from_ptrmemfunc (op0);
3337 pfn1 = pfn_from_ptrmemfunc (op1);
3338 delta0 = build_ptrmemfunc_access_expr (op0,
3340 delta1 = build_ptrmemfunc_access_expr (op1,
3342 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3343 e2 = cp_build_binary_op (EQ_EXPR,
3345 cp_convert (TREE_TYPE (pfn0),
3346 integer_zero_node));
3347 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3348 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3349 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3350 if (code == EQ_EXPR)
3352 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3354 else if ((TYPE_PTRMEMFUNC_P (type0)
3355 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3356 || (TYPE_PTRMEMFUNC_P (type1)
3357 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3363 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3364 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3366 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3367 result_type = composite_pointer_type (type0, type1, op0, op1,
3375 build_type = boolean_type_node;
3376 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3377 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3379 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3380 result_type = composite_pointer_type (type0, type1, op0, op1,
3382 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3383 && integer_zerop (op1))
3384 result_type = type0;
3385 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3386 && integer_zerop (op0))
3387 result_type = type1;
3388 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3390 result_type = type0;
3391 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3393 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3395 result_type = type1;
3396 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3400 case UNORDERED_EXPR:
3407 build_type = integer_type_node;
3408 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3410 error ("unordered comparison on non-floating point argument");
3411 return error_mark_node;
3420 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3422 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3424 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3426 if (shorten || common || short_compare)
3427 result_type = common_type (type0, type1);
3429 /* For certain operations (which identify themselves by shorten != 0)
3430 if both args were extended from the same smaller type,
3431 do the arithmetic in that type and then extend.
3433 shorten !=0 and !=1 indicates a bitwise operation.
3434 For them, this optimization is safe only if
3435 both args are zero-extended or both are sign-extended.
3436 Otherwise, we might change the result.
3437 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3438 but calculated in (unsigned short) it would be (unsigned short)-1. */
3440 if (shorten && none_complex)
3442 int unsigned0, unsigned1;
3443 tree arg0 = get_narrower (op0, &unsigned0);
3444 tree arg1 = get_narrower (op1, &unsigned1);
3445 /* UNS is 1 if the operation to be done is an unsigned one. */
3446 int uns = TREE_UNSIGNED (result_type);
3449 final_type = result_type;
3451 /* Handle the case that OP0 does not *contain* a conversion
3452 but it *requires* conversion to FINAL_TYPE. */
3454 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3455 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3456 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3457 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3459 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3461 /* For bitwise operations, signedness of nominal type
3462 does not matter. Consider only how operands were extended. */
3466 /* Note that in all three cases below we refrain from optimizing
3467 an unsigned operation on sign-extended args.
3468 That would not be valid. */
3470 /* Both args variable: if both extended in same way
3471 from same width, do it in that width.
3472 Do it unsigned if args were zero-extended. */
3473 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3474 < TYPE_PRECISION (result_type))
3475 && (TYPE_PRECISION (TREE_TYPE (arg1))
3476 == TYPE_PRECISION (TREE_TYPE (arg0)))
3477 && unsigned0 == unsigned1
3478 && (unsigned0 || !uns))
3479 result_type = c_common_signed_or_unsigned_type
3480 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3481 else if (TREE_CODE (arg0) == INTEGER_CST
3482 && (unsigned1 || !uns)
3483 && (TYPE_PRECISION (TREE_TYPE (arg1))
3484 < TYPE_PRECISION (result_type))
3485 && (type = c_common_signed_or_unsigned_type
3486 (unsigned1, TREE_TYPE (arg1)),
3487 int_fits_type_p (arg0, type)))
3489 else if (TREE_CODE (arg1) == INTEGER_CST
3490 && (unsigned0 || !uns)
3491 && (TYPE_PRECISION (TREE_TYPE (arg0))
3492 < TYPE_PRECISION (result_type))
3493 && (type = c_common_signed_or_unsigned_type
3494 (unsigned0, TREE_TYPE (arg0)),
3495 int_fits_type_p (arg1, type)))
3499 /* Shifts can be shortened if shifting right. */
3504 tree arg0 = get_narrower (op0, &unsigned_arg);
3506 final_type = result_type;
3508 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3509 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3511 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3512 /* We can shorten only if the shift count is less than the
3513 number of bits in the smaller type size. */
3514 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3515 /* If arg is sign-extended and then unsigned-shifted,
3516 we can simulate this with a signed shift in arg's type
3517 only if the extended result is at least twice as wide
3518 as the arg. Otherwise, the shift could use up all the
3519 ones made by sign-extension and bring in zeros.
3520 We can't optimize that case at all, but in most machines
3521 it never happens because available widths are 2**N. */
3522 && (!TREE_UNSIGNED (final_type)
3524 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3525 <= TYPE_PRECISION (result_type))))
3527 /* Do an unsigned shift if the operand was zero-extended. */
3529 = c_common_signed_or_unsigned_type (unsigned_arg,
3531 /* Convert value-to-be-shifted to that type. */
3532 if (TREE_TYPE (op0) != result_type)
3533 op0 = cp_convert (result_type, op0);
3538 /* Comparison operations are shortened too but differently.
3539 They identify themselves by setting short_compare = 1. */
3543 /* Don't write &op0, etc., because that would prevent op0
3544 from being kept in a register.
3545 Instead, make copies of the our local variables and
3546 pass the copies by reference, then copy them back afterward. */
3547 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3548 enum tree_code xresultcode = resultcode;
3550 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3552 return cp_convert (boolean_type_node, val);
3553 op0 = xop0, op1 = xop1;
3555 resultcode = xresultcode;
3558 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3559 && warn_sign_compare)
3561 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3562 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3564 int unsignedp0, unsignedp1;
3565 tree primop0 = get_narrower (op0, &unsignedp0);
3566 tree primop1 = get_narrower (op1, &unsignedp1);
3568 /* Check for comparison of different enum types. */
3569 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3570 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3571 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3572 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3574 warning ("comparison between types `%#T' and `%#T'",
3575 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3578 /* Give warnings for comparisons between signed and unsigned
3579 quantities that may fail. */
3580 /* Do the checking based on the original operand trees, so that
3581 casts will be considered, but default promotions won't be. */
3583 /* Do not warn if the comparison is being done in a signed type,
3584 since the signed type will only be chosen if it can represent
3585 all the values of the unsigned type. */
3586 if (! TREE_UNSIGNED (result_type))
3588 /* Do not warn if both operands are unsigned. */
3589 else if (op0_signed == op1_signed)
3591 /* Do not warn if the signed quantity is an unsuffixed
3592 integer literal (or some static constant expression
3593 involving such literals or a conditional expression
3594 involving such literals) and it is non-negative. */
3595 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3596 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3598 /* Do not warn if the comparison is an equality operation,
3599 the unsigned quantity is an integral constant and it does
3600 not use the most significant bit of result_type. */
3601 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3602 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3603 && int_fits_type_p (orig_op1, c_common_signed_type
3605 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3606 && int_fits_type_p (orig_op0, c_common_signed_type
3610 warning ("comparison between signed and unsigned integer expressions");
3612 /* Warn if two unsigned values are being compared in a size
3613 larger than their original size, and one (and only one) is the
3614 result of a `~' operator. This comparison will always fail.
3616 Also warn if one operand is a constant, and the constant does not
3617 have all bits set that are set in the ~ operand when it is
3620 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3621 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3623 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3624 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3625 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3626 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3628 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3631 HOST_WIDE_INT constant, mask;
3635 if (host_integerp (primop0, 0))
3638 unsignedp = unsignedp1;
3639 constant = tree_low_cst (primop0, 0);
3644 unsignedp = unsignedp0;
3645 constant = tree_low_cst (primop1, 0);
3648 bits = TYPE_PRECISION (TREE_TYPE (primop));
3649 if (bits < TYPE_PRECISION (result_type)
3650 && bits < HOST_BITS_PER_LONG && unsignedp)
3652 mask = (~ (HOST_WIDE_INT) 0) << bits;
3653 if ((mask & constant) != mask)
3654 warning ("comparison of promoted ~unsigned with constant");
3657 else if (unsignedp0 && unsignedp1
3658 && (TYPE_PRECISION (TREE_TYPE (primop0))
3659 < TYPE_PRECISION (result_type))
3660 && (TYPE_PRECISION (TREE_TYPE (primop1))
3661 < TYPE_PRECISION (result_type)))
3662 warning ("comparison of promoted ~unsigned with unsigned");
3667 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3668 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3669 Then the expression will be built.
3670 It will be given type FINAL_TYPE if that is nonzero;
3671 otherwise, it will be given type RESULT_TYPE. */
3675 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3676 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3677 return error_mark_node;
3680 /* Issue warnings about peculiar, but valid, uses of NULL. */
3681 if (/* It's reasonable to use pointer values as operands of &&
3682 and ||, so NULL is no exception. */
3683 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3684 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3685 (orig_op0 == null_node
3686 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3687 /* Or vice versa. */
3688 || (orig_op1 == null_node
3689 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3690 /* Or, both are NULL and the operation was not a comparison. */
3691 || (orig_op0 == null_node && orig_op1 == null_node
3692 && code != EQ_EXPR && code != NE_EXPR)))
3693 /* Some sort of arithmetic operation involving NULL was
3694 performed. Note that pointer-difference and pointer-addition
3695 have already been handled above, and so we don't end up here in
3697 warning ("NULL used in arithmetic");
3701 if (TREE_TYPE (op0) != result_type)
3702 op0 = cp_convert (result_type, op0);
3703 if (TREE_TYPE (op1) != result_type)
3704 op1 = cp_convert (result_type, op1);
3706 if (op0 == error_mark_node || op1 == error_mark_node)
3707 return error_mark_node;
3710 if (build_type == NULL_TREE)
3711 build_type = result_type;
3714 register tree result = build (resultcode, build_type, op0, op1);
3715 register tree folded;
3717 folded = fold (result);
3718 if (folded == result)
3719 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3720 if (final_type != 0)
3721 return cp_convert (final_type, folded);
3726 /* Return a tree for the sum or difference (RESULTCODE says which)
3727 of pointer PTROP and integer INTOP. */
3730 cp_pointer_int_sum (resultcode, ptrop, intop)
3731 enum tree_code resultcode;
3732 register tree ptrop, intop;
3734 tree res_type = TREE_TYPE (ptrop);
3736 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3737 in certain circumstance (when it's valid to do so). So we need
3738 to make sure it's complete. We don't need to check here, if we
3739 can actually complete it at all, as those checks will be done in
3740 pointer_int_sum() anyway. */
3741 complete_type (TREE_TYPE (res_type));
3743 return pointer_int_sum (resultcode, ptrop, fold (intop));
3746 /* Return a tree for the difference of pointers OP0 and OP1.
3747 The resulting tree has type int. */
3750 pointer_diff (op0, op1, ptrtype)
3751 register tree op0, op1;
3752 register tree ptrtype;
3754 register tree result, folded;
3755 tree restype = ptrdiff_type_node;
3756 tree target_type = TREE_TYPE (ptrtype);
3758 if (!complete_type_or_else (target_type, NULL_TREE))
3759 return error_mark_node;
3761 if (pedantic || warn_pointer_arith)
3763 if (TREE_CODE (target_type) == VOID_TYPE)
3764 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
3765 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3766 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3767 if (TREE_CODE (target_type) == METHOD_TYPE)
3768 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3769 if (TREE_CODE (target_type) == OFFSET_TYPE)
3770 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
3773 /* First do the subtraction as integers;
3774 then drop through to build the divide operator. */
3776 op0 = cp_build_binary_op (MINUS_EXPR,
3777 cp_convert (restype, op0),
3778 cp_convert (restype, op1));
3780 /* This generates an error if op1 is a pointer to an incomplete type. */
3781 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3782 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3784 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3785 || TREE_CODE (target_type) == FUNCTION_TYPE
3786 || TREE_CODE (target_type) == METHOD_TYPE
3787 || TREE_CODE (target_type) == OFFSET_TYPE)
3789 : size_in_bytes (target_type));
3791 /* Do the division. */
3793 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3795 folded = fold (result);
3796 if (folded == result)
3797 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3801 /* Construct and perhaps optimize a tree representation
3802 for a unary operation. CODE, a tree_code, specifies the operation
3803 and XARG is the operand. */
3806 build_x_unary_op (code, xarg)
3807 enum tree_code code;
3813 if (processing_template_decl)
3814 return build_min_nt (code, xarg, NULL_TREE);
3816 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3818 if (code == ADDR_EXPR
3819 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3820 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
3821 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
3822 || (TREE_CODE (xarg) == OFFSET_REF)))
3823 /* don't look for a function */;
3828 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
3829 NULL_TREE, NULL_TREE);
3830 if (rval || code != ADDR_EXPR)
3833 if (code == ADDR_EXPR)
3835 /* A pointer to member-function can be formed only by saying
3837 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
3838 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
3840 if (TREE_CODE (xarg) != OFFSET_REF)
3842 error ("invalid use of '%E' to form a pointer-to-member-function. Use a qualified-id.",
3844 return error_mark_node;
3848 error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function",
3850 PTRMEM_OK_P (xarg) = 1;
3854 if (TREE_CODE (xarg) == OFFSET_REF)
3856 ptrmem = PTRMEM_OK_P (xarg);
3858 if (!ptrmem && !flag_ms_extensions
3859 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
3861 /* A single non-static member, make sure we don't allow a
3862 pointer-to-member. */
3863 xarg = build (OFFSET_REF, TREE_TYPE (xarg),
3864 TREE_OPERAND (xarg, 0),
3865 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
3866 PTRMEM_OK_P (xarg) = ptrmem;
3870 else if (TREE_CODE (xarg) == TARGET_EXPR)
3871 warning ("taking address of temporary");
3873 exp = build_unary_op (code, xarg, 0);
3874 if (TREE_CODE (exp) == ADDR_EXPR)
3875 PTRMEM_OK_P (exp) = ptrmem;
3880 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
3881 constants, where a null value is represented by an INTEGER_CST of
3885 cp_truthvalue_conversion (expr)
3888 tree type = TREE_TYPE (expr);
3889 if (TYPE_PTRMEM_P (type))
3890 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3892 return c_common_truthvalue_conversion (expr);
3895 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3898 condition_conversion (expr)
3902 if (processing_template_decl)
3904 if (TREE_CODE (expr) == OFFSET_REF)
3905 expr = resolve_offset_ref (expr);
3906 t = perform_implicit_conversion (boolean_type_node, expr);
3907 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
3911 /* C++: Must handle pointers to members.
3913 Perhaps type instantiation should be extended to handle conversion
3914 from aggregates to types we don't yet know we want? (Or are those
3915 cases typically errors which should be reported?)
3917 NOCONVERT nonzero suppresses the default promotions
3918 (such as from short to int). */
3921 build_unary_op (code, xarg, noconvert)
3922 enum tree_code code;
3926 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3927 register tree arg = xarg;
3928 register tree argtype = 0;
3929 const char *errstring = NULL;
3932 if (arg == error_mark_node)
3933 return error_mark_node;
3938 /* This is used for unary plus, because a CONVERT_EXPR
3939 is enough to prevent anybody from looking inside for
3940 associativity, but won't generate any code. */
3941 if (!(arg = build_expr_type_conversion
3942 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, true)))
3943 errstring = "wrong type argument to unary plus";
3947 arg = default_conversion (arg);
3948 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
3949 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3954 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3955 errstring = "wrong type argument to unary minus";
3956 else if (!noconvert)
3957 arg = default_conversion (arg);
3961 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3965 arg = default_conversion (arg);
3967 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
3969 errstring = "wrong type argument to bit-complement";
3970 else if (!noconvert)
3971 arg = default_conversion (arg);
3975 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3976 errstring = "wrong type argument to abs";
3977 else if (!noconvert)
3978 arg = default_conversion (arg);
3982 /* Conjugating a real value is a no-op, but allow it anyway. */
3983 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3984 errstring = "wrong type argument to conjugation";
3985 else if (!noconvert)
3986 arg = default_conversion (arg);
3989 case TRUTH_NOT_EXPR:
3990 arg = cp_convert (boolean_type_node, arg);
3991 val = invert_truthvalue (arg);
3992 if (arg != error_mark_node)
3994 errstring = "in argument to unary !";
4001 if (TREE_CODE (arg) == COMPLEX_CST)
4002 return TREE_REALPART (arg);
4003 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4004 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4009 if (TREE_CODE (arg) == COMPLEX_CST)
4010 return TREE_IMAGPART (arg);
4011 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4012 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4014 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4016 case PREINCREMENT_EXPR:
4017 case POSTINCREMENT_EXPR:
4018 case PREDECREMENT_EXPR:
4019 case POSTDECREMENT_EXPR:
4020 /* Handle complex lvalues (when permitted)
4021 by reduction to simpler cases. */
4023 val = unary_complex_lvalue (code, arg);
4027 /* Increment or decrement the real part of the value,
4028 and don't change the imaginary part. */
4029 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4033 arg = stabilize_reference (arg);
4034 real = build_unary_op (REALPART_EXPR, arg, 1);
4035 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4036 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4037 build_unary_op (code, real, 1), imag);
4040 /* Report invalid types. */
4042 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4045 if (code == PREINCREMENT_EXPR)
4046 errstring ="no pre-increment operator for type";
4047 else if (code == POSTINCREMENT_EXPR)
4048 errstring ="no post-increment operator for type";
4049 else if (code == PREDECREMENT_EXPR)
4050 errstring ="no pre-decrement operator for type";
4052 errstring ="no post-decrement operator for type";
4056 /* Report something read-only. */
4058 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4059 || TREE_READONLY (arg))
4060 readonly_error (arg, ((code == PREINCREMENT_EXPR
4061 || code == POSTINCREMENT_EXPR)
4062 ? "increment" : "decrement"),
4067 tree result_type = TREE_TYPE (arg);
4069 arg = get_unwidened (arg, 0);
4070 argtype = TREE_TYPE (arg);
4072 /* ARM $5.2.5 last annotation says this should be forbidden. */
4073 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4074 pedwarn ("ISO C++ forbids %sing an enum",
4075 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4076 ? "increment" : "decrement");
4078 /* Compute the increment. */
4080 if (TREE_CODE (argtype) == POINTER_TYPE)
4082 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4083 tree type = complete_type (TREE_TYPE (argtype));
4085 if (!COMPLETE_OR_VOID_TYPE_P (type))
4086 error ("cannot %s a pointer to incomplete type `%T'",
4087 ((code == PREINCREMENT_EXPR
4088 || code == POSTINCREMENT_EXPR)
4089 ? "increment" : "decrement"), TREE_TYPE (argtype));
4090 else if ((pedantic || warn_pointer_arith)
4091 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4092 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4093 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4094 ((code == PREINCREMENT_EXPR
4095 || code == POSTINCREMENT_EXPR)
4096 ? "increment" : "decrement"), argtype);
4097 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
4100 inc = integer_one_node;
4102 inc = cp_convert (argtype, inc);
4104 /* Handle incrementing a cast-expression. */
4106 switch (TREE_CODE (arg))
4111 case FIX_TRUNC_EXPR:
4112 case FIX_FLOOR_EXPR:
4113 case FIX_ROUND_EXPR:
4116 tree incremented, modify, value, compound;
4117 if (! lvalue_p (arg) && pedantic)
4118 pedwarn ("cast to non-reference type used as lvalue");
4119 arg = stabilize_reference (arg);
4120 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4123 value = save_expr (arg);
4124 incremented = build (((code == PREINCREMENT_EXPR
4125 || code == POSTINCREMENT_EXPR)
4126 ? PLUS_EXPR : MINUS_EXPR),
4127 argtype, value, inc);
4129 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4130 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4132 /* Eliminate warning about unused result of + or -. */
4133 TREE_NO_UNUSED_WARNING (compound) = 1;
4141 /* Complain about anything else that is not a true lvalue. */
4142 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4143 || code == POSTINCREMENT_EXPR)
4144 ? "increment" : "decrement")))
4145 return error_mark_node;
4147 /* Forbid using -- on `bool'. */
4148 if (TREE_TYPE (arg) == boolean_type_node)
4150 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4152 error ("invalid use of `--' on bool variable `%D'", arg);
4153 return error_mark_node;
4156 /* This will only work if someone can convince Kenner to accept
4157 my patch to expand_increment. (jason) */
4158 val = build (code, TREE_TYPE (arg), arg, inc);
4160 val = boolean_increment (code, arg);
4164 val = build (code, TREE_TYPE (arg), arg, inc);
4166 TREE_SIDE_EFFECTS (val) = 1;
4167 return cp_convert (result_type, val);
4171 /* Note that this operation never does default_conversion
4172 regardless of NOCONVERT. */
4174 argtype = lvalue_type (arg);
4175 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4179 build_pointer_type (TREE_TYPE (argtype)), arg);
4180 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4183 else if (pedantic && DECL_MAIN_P (arg))
4185 pedwarn ("ISO C++ forbids taking address of function `::main'");
4187 /* Let &* cancel out to simplify resulting code. */
4188 if (TREE_CODE (arg) == INDIRECT_REF)
4190 /* We don't need to have `current_class_ptr' wrapped in a
4191 NON_LVALUE_EXPR node. */
4192 if (arg == current_class_ref)
4193 return current_class_ptr;
4195 arg = TREE_OPERAND (arg, 0);
4196 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4200 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4201 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4203 else if (lvalue_p (arg))
4204 /* Don't let this be an lvalue. */
4205 return non_lvalue (arg);
4209 /* For &x[y], return x+y */
4210 if (TREE_CODE (arg) == ARRAY_REF)
4212 if (!cxx_mark_addressable (TREE_OPERAND (arg, 0)))
4213 return error_mark_node;
4214 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4215 TREE_OPERAND (arg, 1));
4218 /* Uninstantiated types are all functions. Taking the
4219 address of a function is a no-op, so just return the
4222 if (TREE_CODE (arg) == IDENTIFIER_NODE
4223 && IDENTIFIER_OPNAME_P (arg))
4226 /* We don't know the type yet, so just work around the problem.
4227 We know that this will resolve to an lvalue. */
4228 return build1 (ADDR_EXPR, unknown_type_node, arg);
4231 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4232 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4234 /* They're trying to take the address of a unique non-static
4235 member function. This is ill-formed (except in MS-land),
4236 but let's try to DTRT.
4237 Note: We only handle unique functions here because we don't
4238 want to complain if there's a static overload; non-unique
4239 cases will be handled by instantiate_type. But we need to
4240 handle this case here to allow casts on the resulting PMF.
4241 We could defer this in non-MS mode, but it's easier to give
4242 a useful error here. */
4244 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4245 tree name = DECL_NAME (get_first_fn (TREE_OPERAND (arg, 1)));
4247 if (! flag_ms_extensions)
4249 if (current_class_type
4250 && TREE_OPERAND (arg, 0) == current_class_ref)
4251 /* An expression like &memfn. */
4252 pedwarn ("ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say `&%T::%D'", base, name);
4254 pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base, name);
4256 arg = build_offset_ref (base, name);
4259 if (type_unknown_p (arg))
4260 return build1 (ADDR_EXPR, unknown_type_node, arg);
4262 /* Handle complex lvalues (when permitted)
4263 by reduction to simpler cases. */
4264 val = unary_complex_lvalue (code, arg);
4268 switch (TREE_CODE (arg))
4273 case FIX_TRUNC_EXPR:
4274 case FIX_FLOOR_EXPR:
4275 case FIX_ROUND_EXPR:
4277 if (! lvalue_p (arg) && pedantic)
4278 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4285 /* Allow the address of a constructor if all the elements
4287 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4288 && TREE_CONSTANT (arg))
4290 /* Anything not already handled and not a true memory reference
4292 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4293 && TREE_CODE (argtype) != METHOD_TYPE
4294 && !non_cast_lvalue_or_else (arg, "unary `&'"))
4295 return error_mark_node;
4297 if (argtype != error_mark_node)
4298 argtype = build_pointer_type (argtype);
4300 if (!cxx_mark_addressable (arg))
4301 return error_mark_node;
4306 if (TREE_CODE (arg) == COMPONENT_REF
4307 && TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
4308 arg = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4310 if (TREE_CODE (arg) == COMPONENT_REF
4311 && DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4313 error ("attempt to take address of bit-field structure member `%D'",
4314 TREE_OPERAND (arg, 1));
4315 return error_mark_node;
4317 else if (TREE_CODE (arg) == COMPONENT_REF
4318 && TREE_CODE (TREE_OPERAND (arg, 0)) == INDIRECT_REF
4319 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg, 0), 0))
4322 /* offsetof idiom, fold it. */
4323 tree field = TREE_OPERAND (arg, 1);
4324 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4325 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)),
4326 decl_type_context (field),
4329 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
4330 rval = build1 (NOP_EXPR, argtype, rval);
4331 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4332 addr = fold (build (PLUS_EXPR, argtype, rval,
4333 cp_convert (argtype, byte_position (field))));
4336 addr = build1 (ADDR_EXPR, argtype, arg);
4338 /* Address of a static or external variable or
4339 function counts as a constant */
4341 TREE_CONSTANT (addr) = 1;
4343 if (TREE_CODE (argtype) == POINTER_TYPE
4344 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4346 build_ptrmemfunc_type (argtype);
4347 addr = build_ptrmemfunc (argtype, addr, 0);
4360 argtype = TREE_TYPE (arg);
4361 return fold (build1 (code, argtype, arg));
4364 error ("%s", errstring);
4365 return error_mark_node;
4368 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4369 for certain kinds of expressions which are not really lvalues
4370 but which we can accept as lvalues.
4372 If ARG is not a kind of expression we can handle, return zero. */
4375 unary_complex_lvalue (code, arg)
4376 enum tree_code code;
4379 /* Handle (a, b) used as an "lvalue". */
4380 if (TREE_CODE (arg) == COMPOUND_EXPR)
4382 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4383 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4384 TREE_OPERAND (arg, 0), real_result);
4387 /* Handle (a ? b : c) used as an "lvalue". */
4388 if (TREE_CODE (arg) == COND_EXPR
4389 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4390 return rationalize_conditional_expr (code, arg);
4392 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4393 if (TREE_CODE (arg) == MODIFY_EXPR
4394 || TREE_CODE (arg) == PREINCREMENT_EXPR
4395 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4397 tree lvalue = TREE_OPERAND (arg, 0);
4398 if (TREE_SIDE_EFFECTS (lvalue))
4400 lvalue = stabilize_reference (lvalue);
4401 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4402 lvalue, TREE_OPERAND (arg, 1));
4404 return unary_complex_lvalue
4405 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4408 if (code != ADDR_EXPR)
4411 /* Handle (a = b) used as an "lvalue" for `&'. */
4412 if (TREE_CODE (arg) == MODIFY_EXPR
4413 || TREE_CODE (arg) == INIT_EXPR)
4415 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4416 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4417 TREE_NO_UNUSED_WARNING (arg) = 1;
4421 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4422 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4423 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4425 /* The representation of something of type OFFSET_TYPE
4426 is really the representation of a pointer to it.
4427 Here give the representation its true type. */
4430 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4432 if (TREE_CODE (arg) != OFFSET_REF)
4435 t = TREE_OPERAND (arg, 1);
4437 /* Check all this code for right semantics. */
4438 if (TREE_CODE (t) == FUNCTION_DECL)
4440 if (DECL_DESTRUCTOR_P (t))
4441 error ("taking address of destructor");
4442 return build_unary_op (ADDR_EXPR, t, 0);
4444 if (TREE_CODE (t) == VAR_DECL)
4445 return build_unary_op (ADDR_EXPR, t, 0);
4450 if (TREE_OPERAND (arg, 0)
4451 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4452 && TREE_CODE (t) != FIELD_DECL)
4454 error ("taking address of bound pointer-to-member expression");
4455 return error_mark_node;
4457 if (!PTRMEM_OK_P (arg))
4459 /* This cannot form a pointer to method, so we must
4460 resolve the offset ref, and take the address of the
4461 result. For instance,
4463 arg = resolve_offset_ref (arg);
4465 return build_unary_op (code, arg, 0);
4468 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4470 error ("cannot create pointer to reference member `%D'", t);
4471 return error_mark_node;
4474 type = build_ptrmem_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4475 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4481 /* We permit compiler to make function calls returning
4482 objects of aggregate type look like lvalues. */
4486 if (TREE_CODE (targ) == SAVE_EXPR)
4487 targ = TREE_OPERAND (targ, 0);
4489 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4491 if (TREE_CODE (arg) == SAVE_EXPR)
4494 targ = build_cplus_new (TREE_TYPE (arg), arg);
4495 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4498 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4499 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4500 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4503 /* Don't let anything else be handled specially. */
4507 /* Mark EXP saying that we need to be able to take the
4508 address of it; it should not be allocated in a register.
4509 Value is true if successful.
4511 C++: we do not allow `current_class_ptr' to be addressable. */
4514 cxx_mark_addressable (exp)
4517 register tree x = exp;
4520 switch (TREE_CODE (x))
4527 x = TREE_OPERAND (x, 0);
4531 if (x == current_class_ptr)
4533 error ("cannot take the address of `this', which is an rvalue expression");
4534 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4540 /* Caller should not be trying to mark initialized
4541 constant fields addressable. */
4542 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4543 || DECL_IN_AGGR_P (x) == 0
4545 || DECL_EXTERNAL (x), 314);
4550 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4551 && !DECL_ARTIFICIAL (x) && extra_warnings)
4552 warning ("address requested for `%D', which is declared `register'",
4554 TREE_ADDRESSABLE (x) = 1;
4555 put_var_into_stack (x);
4559 TREE_ADDRESSABLE (x) = 1;
4560 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4564 TREE_ADDRESSABLE (x) = 1;
4568 TREE_ADDRESSABLE (x) = 1;
4569 cxx_mark_addressable (TREE_OPERAND (x, 0));
4577 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4580 build_x_conditional_expr (ifexp, op1, op2)
4581 tree ifexp, op1, op2;
4583 if (processing_template_decl)
4584 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4586 return build_conditional_expr (ifexp, op1, op2);
4589 /* Handle overloading of the ',' operator when needed. Otherwise,
4590 this function just builds an expression list. */
4593 build_x_compound_expr (list)
4596 tree rest = TREE_CHAIN (list);
4599 if (processing_template_decl)
4600 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
4602 if (rest == NULL_TREE)
4603 return build_compound_expr (list);
4605 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
4606 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4608 return build_x_compound_expr (tree_cons (NULL_TREE, result,
4609 TREE_CHAIN (rest)));
4611 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
4613 /* FIXME: This test should be in the implicit cast to void of the LHS. */
4614 /* the left-hand operand of a comma expression is like an expression
4615 statement: we should warn if it doesn't have any side-effects,
4616 unless it was explicitly cast to (void). */
4617 if ((extra_warnings || warn_unused_value)
4618 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
4619 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
4620 warning("left-hand operand of comma expression has no effect");
4622 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4623 else if (warn_unused_value)
4624 warn_if_unused_value (TREE_VALUE(list));
4627 return build_compound_expr
4628 (tree_cons (NULL_TREE, TREE_VALUE (list),
4629 build_tree_list (NULL_TREE,
4630 build_x_compound_expr (rest))));
4633 /* Given a list of expressions, return a compound expression
4634 that performs them all and returns the value of the last of them. */
4637 build_compound_expr (list)
4643 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
4645 if (TREE_CHAIN (list) == 0)
4647 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4648 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4649 if (TREE_CODE (list) == NOP_EXPR
4650 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
4651 list = TREE_OPERAND (list, 0);
4653 return TREE_VALUE (list);
4656 first = TREE_VALUE (list);
4657 first = convert_to_void (first, "left-hand operand of comma");
4658 if (first == error_mark_node)
4659 return error_mark_node;
4661 rest = build_compound_expr (TREE_CHAIN (list));
4662 if (rest == error_mark_node)
4663 return error_mark_node;
4665 /* When pedantic, a compound expression cannot be a constant expression. */
4666 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
4669 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
4673 build_static_cast (type, expr)
4679 if (type == error_mark_node || expr == error_mark_node)
4680 return error_mark_node;
4682 if (TREE_CODE (expr) == OFFSET_REF)
4683 expr = resolve_offset_ref (expr);
4685 if (processing_template_decl)
4687 tree t = build_min (STATIC_CAST_EXPR, type, expr);
4691 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4692 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4693 if (TREE_CODE (type) != REFERENCE_TYPE
4694 && TREE_CODE (expr) == NOP_EXPR
4695 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4696 expr = TREE_OPERAND (expr, 0);
4698 if (TREE_CODE (type) == VOID_TYPE)
4700 expr = convert_to_void (expr, /*implicit=*/NULL);
4704 if (TREE_CODE (type) == REFERENCE_TYPE)
4705 return (convert_from_reference
4706 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
4707 LOOKUP_COMPLAIN, NULL_TREE)));
4709 if (IS_AGGR_TYPE (type))
4710 return build_cplus_new (type, (build_special_member_call
4711 (NULL_TREE, complete_ctor_identifier,
4712 build_tree_list (NULL_TREE, expr),
4713 TYPE_BINFO (type), LOOKUP_NORMAL)));
4715 intype = TREE_TYPE (expr);
4717 /* FIXME handle casting to array type. */
4720 if (IS_AGGR_TYPE (intype)
4721 ? can_convert_arg (type, intype, expr)
4722 : can_convert_arg (strip_all_pointer_quals (type),
4723 strip_all_pointer_quals (intype), expr))
4724 /* This is a standard conversion. */
4726 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
4728 /* They're pointers to objects. They must be aggregates that
4729 are related non-virtually. */
4732 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
4733 && lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
4734 ba_ignore | ba_quiet, &kind)
4735 && kind != bk_via_virtual)
4738 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4740 /* They're pointers to members. The pointed to objects must be
4741 the same (ignoring CV qualifiers), and the containing classes
4742 must be related non-virtually. */
4746 (strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (type))),
4747 strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (intype))))
4748 && (lookup_base (TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)),
4749 TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
4750 ba_ignore | ba_quiet, &kind))
4751 && kind != bk_via_virtual)
4754 else if (TREE_CODE (intype) != BOOLEAN_TYPE
4755 && TREE_CODE (type) != ARRAY_TYPE
4756 && TREE_CODE (type) != FUNCTION_TYPE
4757 && can_convert (intype, strip_all_pointer_quals (type)))
4759 else if (TREE_CODE (intype) == ENUMERAL_TYPE
4760 && TREE_CODE (type) == ENUMERAL_TYPE)
4761 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
4762 converted to an enumeration type."
4763 The integral to enumeration will be accepted by the previous clause.
4764 We need to explicitly check for enumeration to enumeration. */
4767 /* [expr.static.cast]
4769 The static_cast operator shall not be used to cast away
4771 if (ok && casts_away_constness (intype, type))
4773 error ("static_cast from type `%T' to type `%T' casts away constness",
4775 return error_mark_node;
4779 return build_c_cast (type, expr);
4781 error ("invalid static_cast from type `%T' to type `%T'", intype, type);
4782 return error_mark_node;
4786 build_reinterpret_cast (type, expr)
4791 if (type == error_mark_node || expr == error_mark_node)
4792 return error_mark_node;
4794 if (TREE_CODE (expr) == OFFSET_REF)
4795 expr = resolve_offset_ref (expr);
4797 if (processing_template_decl)
4799 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
4803 if (TREE_CODE (type) != REFERENCE_TYPE)
4805 expr = decay_conversion (expr);
4807 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4808 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4809 if (TREE_CODE (expr) == NOP_EXPR
4810 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4811 expr = TREE_OPERAND (expr, 0);
4814 intype = TREE_TYPE (expr);
4816 if (TREE_CODE (type) == REFERENCE_TYPE)
4818 if (! real_lvalue_p (expr))
4820 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
4821 return error_mark_node;
4823 expr = build_unary_op (ADDR_EXPR, expr, 0);
4824 if (expr != error_mark_node)
4825 expr = build_reinterpret_cast
4826 (build_pointer_type (TREE_TYPE (type)), expr);
4827 if (expr != error_mark_node)
4828 expr = build_indirect_ref (expr, 0);
4831 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
4832 return build_static_cast (type, expr);
4834 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
4835 || TREE_CODE (intype) == ENUMERAL_TYPE))
4837 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
4839 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
4840 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
4843 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
4844 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
4846 expr = decl_constant_value (expr);
4847 return fold (build1 (NOP_EXPR, type, expr));
4849 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4850 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
4852 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
4853 pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
4856 expr = decl_constant_value (expr);
4857 return fold (build1 (NOP_EXPR, type, expr));
4859 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
4860 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
4862 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
4863 expr = decl_constant_value (expr);
4864 return fold (build1 (NOP_EXPR, type, expr));
4868 error ("invalid reinterpret_cast from type `%T' to type `%T'",
4870 return error_mark_node;
4873 return cp_convert (type, expr);
4877 build_const_cast (type, expr)
4882 if (type == error_mark_node || expr == error_mark_node)
4883 return error_mark_node;
4885 if (TREE_CODE (expr) == OFFSET_REF)
4886 expr = resolve_offset_ref (expr);
4888 if (processing_template_decl)
4890 tree t = build_min (CONST_CAST_EXPR, type, expr);
4894 if (!POINTER_TYPE_P (type))
4895 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
4896 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4898 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
4899 return error_mark_node;
4902 if (TREE_CODE (type) != REFERENCE_TYPE)
4904 expr = decay_conversion (expr);
4906 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4907 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4908 if (TREE_CODE (expr) == NOP_EXPR
4909 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4910 expr = TREE_OPERAND (expr, 0);
4913 intype = TREE_TYPE (expr);
4915 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
4916 return build_static_cast (type, expr);
4917 else if (TREE_CODE (type) == REFERENCE_TYPE)
4919 if (! real_lvalue_p (expr))
4921 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
4922 return error_mark_node;
4925 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
4927 expr = build_unary_op (ADDR_EXPR, expr, 0);
4928 expr = build1 (NOP_EXPR, type, expr);
4929 return convert_from_reference (expr);
4932 else if (TREE_CODE (type) == POINTER_TYPE
4933 && TREE_CODE (intype) == POINTER_TYPE
4934 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
4935 return cp_convert (type, expr);
4937 error ("invalid const_cast from type `%T' to type `%T'", intype, type);
4938 return error_mark_node;
4941 /* Build an expression representing a cast to type TYPE of expression EXPR.
4943 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
4944 when doing the cast. */
4947 build_c_cast (type, expr)
4950 register tree value = expr;
4953 if (type == error_mark_node || expr == error_mark_node)
4954 return error_mark_node;
4956 if (processing_template_decl)
4958 tree t = build_min (CAST_EXPR, type,
4959 tree_cons (NULL_TREE, value, NULL_TREE));
4963 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4964 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4965 if (TREE_CODE (type) != REFERENCE_TYPE
4966 && TREE_CODE (value) == NOP_EXPR
4967 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
4968 value = TREE_OPERAND (value, 0);
4970 if (TREE_CODE (value) == OFFSET_REF)
4971 value = resolve_offset_ref (value);
4973 if (TREE_CODE (type) == ARRAY_TYPE)
4975 /* Allow casting from T1* to T2[] because Cfront allows it.
4976 NIHCL uses it. It is not valid ISO C++ however. */
4977 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
4979 pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
4980 type = build_pointer_type (TREE_TYPE (type));
4984 error ("ISO C++ forbids casting to an array type `%T'", type);
4985 return error_mark_node;
4989 if (TREE_CODE (type) == FUNCTION_TYPE
4990 || TREE_CODE (type) == METHOD_TYPE)
4992 error ("invalid cast to function type `%T'", type);
4993 return error_mark_node;
4996 if (TREE_CODE (type) == VOID_TYPE)
4998 /* Conversion to void does not cause any of the normal function to
4999 * pointer, array to pointer and lvalue to rvalue decays. */
5001 value = convert_to_void (value, /*implicit=*/NULL);
5004 /* Convert functions and arrays to pointers and
5005 convert references to their expanded types,
5006 but don't convert any other types. If, however, we are
5007 casting to a class type, there's no reason to do this: the
5008 cast will only succeed if there is a converting constructor,
5009 and the default conversions will be done at that point. In
5010 fact, doing the default conversion here is actually harmful
5014 struct S { S(const A&); };
5016 since we don't want the array-to-pointer conversion done. */
5017 if (!IS_AGGR_TYPE (type))
5019 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5020 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5021 /* Don't do the default conversion on a ->* expression. */
5022 && ! (TREE_CODE (type) == POINTER_TYPE
5023 && bound_pmf_p (value)))
5024 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5025 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5026 value = default_conversion (value);
5028 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5029 /* However, even for class types, we still need to strip away
5030 the reference type, since the call to convert_force below
5031 does not expect the input expression to be of reference
5033 value = convert_from_reference (value);
5035 otype = TREE_TYPE (value);
5037 /* Optionally warn about potentially worrisome casts. */
5040 && TREE_CODE (type) == POINTER_TYPE
5041 && TREE_CODE (otype) == POINTER_TYPE
5042 && !at_least_as_qualified_p (TREE_TYPE (type),
5044 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5047 if (TREE_CODE (type) == INTEGER_TYPE
5048 && TREE_CODE (otype) == POINTER_TYPE
5049 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5050 warning ("cast from pointer to integer of different size");
5052 if (TREE_CODE (type) == POINTER_TYPE
5053 && TREE_CODE (otype) == INTEGER_TYPE
5054 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5055 /* Don't warn about converting any constant. */
5056 && !TREE_CONSTANT (value))
5057 warning ("cast to pointer from integer of different size");
5059 if (TREE_CODE (type) == REFERENCE_TYPE)
5060 value = (convert_from_reference
5061 (convert_to_reference (type, value, CONV_C_CAST,
5062 LOOKUP_COMPLAIN, NULL_TREE)));
5067 value = decl_constant_value (value);
5070 value = convert_force (type, value, CONV_C_CAST);
5072 /* Ignore any integer overflow caused by the cast. */
5073 if (TREE_CODE (value) == INTEGER_CST)
5075 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5076 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5080 /* Warn about possible alignment problems. Do this here when we will have
5081 instantiated any necessary template types. */
5082 if (STRICT_ALIGNMENT && warn_cast_align
5083 && TREE_CODE (type) == POINTER_TYPE
5084 && TREE_CODE (otype) == POINTER_TYPE
5085 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5086 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5087 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5088 && COMPLETE_TYPE_P (TREE_TYPE (type))
5089 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5090 warning ("cast from `%T' to `%T' increases required alignment of target type",
5093 /* Always produce some operator for an explicit cast,
5094 so we can tell (for -pedantic) that the cast is no lvalue. */
5095 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5096 && real_lvalue_p (value))
5097 value = non_lvalue (value);
5102 /* Build an assignment expression of lvalue LHS from value RHS.
5103 MODIFYCODE is the code for a binary operator that we use
5104 to combine the old value of LHS with RHS to get the new value.
5105 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5107 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5110 build_modify_expr (lhs, modifycode, rhs)
5112 enum tree_code modifycode;
5115 register tree result;
5117 tree lhstype = TREE_TYPE (lhs);
5118 tree olhstype = lhstype;
5121 /* Avoid duplicate error messages from operands that had errors. */
5122 if (lhs == error_mark_node || rhs == error_mark_node)
5123 return error_mark_node;
5125 /* Handle control structure constructs used as "lvalues". */
5126 switch (TREE_CODE (lhs))
5128 /* Handle --foo = 5; as these are valid constructs in C++ */
5129 case PREDECREMENT_EXPR:
5130 case PREINCREMENT_EXPR:
5131 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5132 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5133 stabilize_reference (TREE_OPERAND (lhs, 0)),
5134 TREE_OPERAND (lhs, 1));
5135 return build (COMPOUND_EXPR, lhstype,
5137 build_modify_expr (TREE_OPERAND (lhs, 0),
5140 /* Handle (a, b) used as an "lvalue". */
5142 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5144 if (newrhs == error_mark_node)
5145 return error_mark_node;
5146 return build (COMPOUND_EXPR, lhstype,
5147 TREE_OPERAND (lhs, 0), newrhs);
5150 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5151 if (newrhs == error_mark_node)
5152 return error_mark_node;
5153 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5155 /* Handle (a ? b : c) used as an "lvalue". */
5158 /* Produce (a ? (b = rhs) : (c = rhs))
5159 except that the RHS goes through a save-expr
5160 so the code to compute it is only emitted once. */
5162 tree preeval = NULL_TREE;
5164 rhs = stabilize_expr (rhs, &preeval);
5166 /* Check this here to avoid odd errors when trying to convert
5167 a throw to the type of the COND_EXPR. */
5168 if (!lvalue_or_else (lhs, "assignment"))
5169 return error_mark_node;
5171 cond = build_conditional_expr
5172 (TREE_OPERAND (lhs, 0),
5173 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5174 TREE_OPERAND (lhs, 1)),
5176 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5177 TREE_OPERAND (lhs, 2)),
5180 if (cond == error_mark_node)
5182 /* Make sure the code to compute the rhs comes out
5183 before the split. */
5184 return build (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
5188 lhs = resolve_offset_ref (lhs);
5189 if (lhs == error_mark_node)
5190 return error_mark_node;
5191 olhstype = lhstype = TREE_TYPE (lhs);
5197 if (modifycode == INIT_EXPR)
5199 if (TREE_CODE (rhs) == CONSTRUCTOR)
5201 my_friendly_assert (same_type_p (TREE_TYPE (rhs), lhstype),
5203 result = build (INIT_EXPR, lhstype, lhs, rhs);
5204 TREE_SIDE_EFFECTS (result) = 1;
5207 else if (! IS_AGGR_TYPE (lhstype))
5208 /* Do the default thing */;
5211 result = build_special_member_call (lhs, complete_ctor_identifier,
5212 build_tree_list (NULL_TREE, rhs),
5213 TYPE_BINFO (lhstype),
5215 if (result == NULL_TREE)
5216 return error_mark_node;
5222 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5224 lhs = convert_from_reference (lhs);
5225 olhstype = lhstype = TREE_TYPE (lhs);
5227 lhs = require_complete_type (lhs);
5228 if (lhs == error_mark_node)
5229 return error_mark_node;
5231 if (modifycode == NOP_EXPR)
5233 /* `operator=' is not an inheritable operator. */
5234 if (! IS_AGGR_TYPE (lhstype))
5235 /* Do the default thing */;
5238 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5239 lhs, rhs, make_node (NOP_EXPR));
5240 if (result == NULL_TREE)
5241 return error_mark_node;
5248 /* A binary op has been requested. Combine the old LHS
5249 value with the RHS producing the value we should actually
5250 store into the LHS. */
5252 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE),
5254 lhs = stabilize_reference (lhs);
5255 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5256 if (newrhs == error_mark_node)
5258 error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5259 TREE_TYPE (lhs), TREE_TYPE (rhs));
5260 return error_mark_node;
5263 /* Now it looks like a plain assignment. */
5264 modifycode = NOP_EXPR;
5266 my_friendly_assert (TREE_CODE (lhstype) != REFERENCE_TYPE, 20011220);
5267 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE,
5271 /* Handle a cast used as an "lvalue".
5272 We have already performed any binary operator using the value as cast.
5273 Now convert the result to the cast type of the lhs,
5274 and then true type of the lhs and store it there;
5275 then convert result back to the cast type to be the value
5276 of the assignment. */
5278 switch (TREE_CODE (lhs))
5283 case FIX_TRUNC_EXPR:
5284 case FIX_FLOOR_EXPR:
5285 case FIX_ROUND_EXPR:
5288 tree inner_lhs = TREE_OPERAND (lhs, 0);
5291 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5292 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5293 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5294 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5295 newrhs = default_conversion (newrhs);
5297 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5298 type, otherwise the result is an rvalue. */
5299 if (! lvalue_p (lhs))
5300 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5302 result = build_modify_expr (inner_lhs, NOP_EXPR,
5303 cp_convert (TREE_TYPE (inner_lhs),
5304 cp_convert (lhstype, newrhs)));
5305 if (result == error_mark_node)
5307 return cp_convert (TREE_TYPE (lhs), result);
5314 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5315 Reject anything strange now. */
5317 if (!lvalue_or_else (lhs, "assignment"))
5318 return error_mark_node;
5320 /* Warn about modifying something that is `const'. Don't warn if
5321 this is initialization. */
5322 if (modifycode != INIT_EXPR
5323 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5324 /* Functions are not modifiable, even though they are
5326 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5327 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5328 /* If it's an aggregate and any field is const, then it is
5329 effectively const. */
5330 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5331 && C_TYPE_FIELDS_READONLY (lhstype))))
5332 readonly_error (lhs, "assignment", 0);
5334 /* If storing into a structure or union member, it has probably been
5335 given type `int'. Compute the type that would go with the actual
5336 amount of storage the member occupies. */
5338 if (TREE_CODE (lhs) == COMPONENT_REF
5339 && (TREE_CODE (lhstype) == INTEGER_TYPE
5340 || TREE_CODE (lhstype) == REAL_TYPE
5341 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5343 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5345 /* If storing in a field that is in actuality a short or narrower
5346 than one, we must store in the field in its actual type. */
5348 if (lhstype != TREE_TYPE (lhs))
5350 lhs = copy_node (lhs);
5351 TREE_TYPE (lhs) = lhstype;
5355 if (TREE_CODE (lhstype) != REFERENCE_TYPE)
5357 if (TREE_SIDE_EFFECTS (lhs))
5358 lhs = stabilize_reference (lhs);
5359 if (TREE_SIDE_EFFECTS (newrhs))
5360 newrhs = stabilize_reference (newrhs);
5363 /* Convert new value to destination type. */
5365 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5369 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5370 TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
5372 error ("incompatible types in assignment of `%T' to `%T'",
5373 TREE_TYPE (rhs), lhstype);
5374 return error_mark_node;
5377 /* Allow array assignment in compiler-generated code. */
5378 if (! DECL_ARTIFICIAL (current_function_decl))
5379 pedwarn ("ISO C++ forbids assignment of arrays");
5381 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5382 ? 1 + (modifycode != INIT_EXPR): 0;
5383 return build_vec_init (lhs, NULL_TREE, newrhs, from_array);
5386 if (modifycode == INIT_EXPR)
5387 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5388 "initialization", NULL_TREE, 0);
5391 /* Avoid warnings on enum bit fields. */
5392 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5393 && TREE_CODE (lhstype) == INTEGER_TYPE)
5395 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5397 newrhs = convert_force (lhstype, newrhs, 0);
5400 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5402 if (TREE_CODE (newrhs) == CALL_EXPR
5403 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5404 newrhs = build_cplus_new (lhstype, newrhs);
5406 /* Can't initialize directly from a TARGET_EXPR, since that would
5407 cause the lhs to be constructed twice, and possibly result in
5408 accidental self-initialization. So we force the TARGET_EXPR to be
5409 expanded without a target. */
5410 if (TREE_CODE (newrhs) == TARGET_EXPR)
5411 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5412 TREE_OPERAND (newrhs, 0));
5415 if (newrhs == error_mark_node)
5416 return error_mark_node;
5418 if (TREE_CODE (newrhs) == COND_EXPR)
5421 tree cond = TREE_OPERAND (newrhs, 0);
5423 if (TREE_SIDE_EFFECTS (lhs))
5424 cond = build_compound_expr (tree_cons
5426 build_tree_list (NULL_TREE, cond)));
5428 /* Cannot have two identical lhs on this one tree (result) as preexpand
5429 calls will rip them out and fill in RTL for them, but when the
5430 rtl is generated, the calls will only be in the first side of the
5431 condition, not on both, or before the conditional jump! (mrs) */
5432 lhs1 = break_out_calls (lhs);
5435 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5436 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5437 lhstype, lhs, newrhs);
5440 tree result_type = TREE_TYPE (newrhs);
5441 /* We have to convert each arm to the proper type because the
5442 types may have been munged by constant folding. */
5444 = build (COND_EXPR, result_type, cond,
5445 build_modify_expr (lhs, modifycode,
5446 cp_convert (result_type,
5447 TREE_OPERAND (newrhs, 1))),
5448 build_modify_expr (lhs1, modifycode,
5449 cp_convert (result_type,
5450 TREE_OPERAND (newrhs, 2))));
5454 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5455 lhstype, lhs, newrhs);
5457 TREE_SIDE_EFFECTS (result) = 1;
5459 /* If we got the LHS in a different type for storing in,
5460 convert the result back to the nominal type of LHS
5461 so that the value we return always has the same type
5462 as the LHS argument. */
5464 if (olhstype == TREE_TYPE (result))
5466 /* Avoid warnings converting integral types back into enums
5467 for enum bit fields. */
5468 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5469 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5471 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5472 TREE_NO_UNUSED_WARNING (result) = 1;
5475 return convert_for_assignment (olhstype, result, "assignment",
5480 build_x_modify_expr (lhs, modifycode, rhs)
5482 enum tree_code modifycode;
5485 if (processing_template_decl)
5486 return build_min_nt (MODOP_EXPR, lhs,
5487 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5489 if (modifycode != NOP_EXPR)
5491 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5492 make_node (modifycode));
5496 return build_modify_expr (lhs, modifycode, rhs);
5500 /* Get difference in deltas for different pointer to member function
5501 types. Return integer_zero_node, if FROM cannot be converted to a
5502 TO type. If FORCE is true, then allow reverse conversions as well.
5504 Note that the naming of FROM and TO is kind of backwards; the return
5505 value is what we add to a TO in order to get a FROM. They are named
5506 this way because we call this function to find out how to convert from
5507 a pointer to member of FROM to a pointer to member of TO. */
5510 get_delta_difference (from, to, force)
5514 tree delta = integer_zero_node;
5519 binfo = lookup_base (to, from, ba_check, &kind);
5520 if (kind == bk_inaccessible || kind == bk_ambig)
5522 error (" in pointer to member function conversion");
5529 error_not_base_type (from, to);
5530 error (" in pointer to member conversion");
5533 binfo = lookup_base (from, to, ba_check, &kind);
5536 virt_binfo = binfo_from_vbase (binfo);
5540 /* This is a reinterpret cast, we choose to do nothing. */
5541 warning ("pointer to member cast via virtual base `%T' of `%T'",
5542 BINFO_TYPE (virt_binfo),
5543 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5546 delta = BINFO_OFFSET (binfo);
5547 delta = cp_convert (ptrdiff_type_node, delta);
5548 delta = cp_build_binary_op (MINUS_EXPR,
5555 virt_binfo = binfo_from_vbase (binfo);
5558 /* This is a reinterpret cast, we choose to do nothing. */
5560 warning ("pointer to member cast via virtual base `%T' of `%T'",
5561 BINFO_TYPE (virt_binfo),
5562 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5564 error ("pointer to member conversion via virtual base `%T' of `%T'",
5565 BINFO_TYPE (virt_binfo),
5566 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5569 delta = BINFO_OFFSET (binfo);
5571 return cp_convert (ptrdiff_type_node, delta);
5574 /* Return a constructor for the pointer-to-member-function TYPE using
5575 the other components as specified. */
5578 build_ptrmemfunc1 (type, delta, pfn)
5579 tree type, delta, pfn;
5585 /* Pull the FIELD_DECLs out of the type. */
5586 pfn_field = TYPE_FIELDS (type);
5587 delta_field = TREE_CHAIN (pfn_field);
5589 /* Make sure DELTA has the type we want. */
5590 delta = convert_and_check (delta_type_node, delta);
5592 /* Finish creating the initializer. */
5593 u = tree_cons (pfn_field, pfn,
5594 build_tree_list (delta_field, delta));
5595 u = build (CONSTRUCTOR, type, NULL_TREE, u);
5596 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
5597 TREE_STATIC (u) = (TREE_CONSTANT (u)
5598 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
5600 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
5605 /* Build a constructor for a pointer to member function. It can be
5606 used to initialize global variables, local variable, or used
5607 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5610 If FORCE is nonzero, then force this conversion, even if
5611 we would rather not do it. Usually set when using an explicit
5614 Return error_mark_node, if something goes wrong. */
5617 build_ptrmemfunc (type, pfn, force)
5622 tree pfn_type = TREE_TYPE (pfn);
5623 tree to_type = build_ptrmemfunc_type (type);
5625 /* Handle multiple conversions of pointer to member functions. */
5626 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
5628 tree delta = NULL_TREE;
5629 tree npfn = NULL_TREE;
5633 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
5634 error ("invalid conversion to type `%T' from type `%T'",
5637 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
5638 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
5641 /* We don't have to do any conversion to convert a
5642 pointer-to-member to its own type. But, we don't want to
5643 just return a PTRMEM_CST if there's an explicit cast; that
5644 cast should make the expression an invalid template argument. */
5645 if (TREE_CODE (pfn) != PTRMEM_CST)
5647 if (same_type_p (to_type, pfn_type))
5649 else if (integer_zerop (n))
5650 return build_reinterpret_cast (to_type, pfn);
5653 if (TREE_SIDE_EFFECTS (pfn))
5654 pfn = save_expr (pfn);
5656 /* Obtain the function pointer and the current DELTA. */
5657 if (TREE_CODE (pfn) == PTRMEM_CST)
5658 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
5661 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
5662 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
5665 /* Just adjust the DELTA field. */
5666 delta = cp_convert (ptrdiff_type_node, delta);
5667 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
5668 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
5669 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
5670 return build_ptrmemfunc1 (to_type, delta, npfn);
5673 /* Handle null pointer to member function conversions. */
5674 if (integer_zerop (pfn))
5676 pfn = build_c_cast (type, integer_zero_node);
5677 return build_ptrmemfunc1 (to_type,
5682 if (type_unknown_p (pfn))
5683 return instantiate_type (type, pfn, tf_error | tf_warning);
5685 fn = TREE_OPERAND (pfn, 0);
5686 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5687 return make_ptrmem_cst (to_type, fn);
5690 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5693 ??? There is no consistency as to the types returned for the above
5694 values. Some code acts as if its a sizetype and some as if its
5695 integer_type_node. */
5698 expand_ptrmemfunc_cst (cst, delta, pfn)
5703 tree type = TREE_TYPE (cst);
5704 tree fn = PTRMEM_CST_MEMBER (cst);
5705 tree ptr_class, fn_class;
5707 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5709 /* The class that the function belongs to. */
5710 fn_class = DECL_CONTEXT (fn);
5712 /* The class that we're creating a pointer to member of. */
5713 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
5715 /* First, calculate the adjustment to the function's class. */
5716 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
5718 if (!DECL_VIRTUAL_P (fn))
5719 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
5722 /* If we're dealing with a virtual function, we have to adjust 'this'
5723 again, to point to the base which provides the vtable entry for
5724 fn; the call will do the opposite adjustment. */
5725 tree orig_class = DECL_CONTEXT (fn);
5726 tree binfo = binfo_or_else (orig_class, fn_class);
5727 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5728 *delta, BINFO_OFFSET (binfo)));
5730 /* We set PFN to the vtable offset at which the function can be
5731 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
5732 case delta is shifted left, and then incremented). */
5733 *pfn = DECL_VINDEX (fn);
5734 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
5735 TYPE_SIZE_UNIT (vtable_entry_type)));
5737 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
5739 case ptrmemfunc_vbit_in_pfn:
5740 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
5744 case ptrmemfunc_vbit_in_delta:
5745 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
5746 *delta, integer_one_node));
5747 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5748 *delta, integer_one_node));
5755 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
5760 /* Return an expression for PFN from the pointer-to-member function
5764 pfn_from_ptrmemfunc (t)
5767 if (TREE_CODE (t) == PTRMEM_CST)
5772 expand_ptrmemfunc_cst (t, &delta, &pfn);
5777 return build_ptrmemfunc_access_expr (t, pfn_identifier);
5780 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
5781 if this is a potentially dangerous thing to do. Returns a possibly
5785 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
5788 const char *errtype;
5792 if (TREE_CODE (type) == REFERENCE_TYPE)
5793 type = TREE_TYPE (type);
5795 /* Issue warnings about peculiar, but valid, uses of NULL. */
5796 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
5799 warning ("passing NULL used for non-pointer %s %P of `%D'",
5800 errtype, parmnum, fndecl);
5802 warning ("%s to non-pointer type `%T' from NULL", errtype, type);
5805 /* Warn about assigning a floating-point type to an integer type. */
5806 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
5807 && TREE_CODE (type) == INTEGER_TYPE)
5810 warning ("passing `%T' for %s %P of `%D'",
5811 TREE_TYPE (expr), errtype, parmnum, fndecl);
5813 warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
5815 /* And warn about assigning a negative value to an unsigned
5817 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
5819 if (TREE_CODE (expr) == INTEGER_CST
5820 && TREE_NEGATED_INT (expr))
5823 warning ("passing negative value `%E' for %s %P of `%D'",
5824 expr, errtype, parmnum, fndecl);
5826 warning ("%s of negative value `%E' to `%T'",
5827 errtype, expr, type);
5830 overflow_warning (expr);
5832 if (TREE_CONSTANT (expr))
5838 /* Convert value RHS to type TYPE as preparation for an assignment to
5839 an lvalue of type TYPE. ERRTYPE is a string to use in error
5840 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
5841 are doing the conversion in order to pass the PARMNUMth argument of
5845 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
5847 const char *errtype;
5851 register enum tree_code codel = TREE_CODE (type);
5852 register tree rhstype;
5853 register enum tree_code coder;
5855 if (codel == OFFSET_TYPE)
5858 if (TREE_CODE (rhs) == OFFSET_REF)
5859 rhs = resolve_offset_ref (rhs);
5861 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
5862 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
5863 rhs = TREE_OPERAND (rhs, 0);
5865 rhstype = TREE_TYPE (rhs);
5866 coder = TREE_CODE (rhstype);
5868 if (rhs == error_mark_node || rhstype == error_mark_node)
5869 return error_mark_node;
5870 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
5871 return error_mark_node;
5873 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
5875 /* The RHS of an assignment cannot have void type. */
5876 if (coder == VOID_TYPE)
5878 error ("void value not ignored as it ought to be");
5879 return error_mark_node;
5882 /* Simplify the RHS if possible. */
5883 if (TREE_CODE (rhs) == CONST_DECL)
5884 rhs = DECL_INITIAL (rhs);
5886 /* We do not use decl_constant_value here because of this case:
5888 const char* const s = "s";
5890 The conversion rules for a string literal are more lax than for a
5891 variable; in particular, a string literal can be converted to a
5892 "char *" but the variable "s" cannot be converted in the same
5893 way. If the conversion is allowed, the optimization should be
5894 performed while creating the converted expression. */
5898 The expression is implicitly converted (clause _conv_) to the
5899 cv-unqualified type of the left operand.
5901 We allow bad conversions here because by the time we get to this point
5902 we are committed to doing the conversion. If we end up doing a bad
5903 conversion, convert_like will complain. */
5904 if (!can_convert_arg_bad (type, rhstype, rhs))
5906 /* When -Wno-pmf-conversions is use, we just silently allow
5907 conversions from pointers-to-members to plain pointers. If
5908 the conversion doesn't work, cp_convert will complain. */
5910 && TYPE_PTR_P (type)
5911 && TYPE_PTRMEMFUNC_P (rhstype))
5912 rhs = cp_convert (strip_top_quals (type), rhs);
5915 /* If the right-hand side has unknown type, then it is an
5916 overloaded function. Call instantiate_type to get error
5918 if (rhstype == unknown_type_node)
5919 instantiate_type (type, rhs, tf_error | tf_warning);
5921 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
5922 rhstype, type, parmnum, fndecl);
5924 error ("cannot convert `%T' to `%T' in %s", rhstype, type,
5926 return error_mark_node;
5929 return perform_implicit_conversion (strip_top_quals (type), rhs);
5932 /* Convert RHS to be of type TYPE.
5933 If EXP is nonzero, it is the target of the initialization.
5934 ERRTYPE is a string to use in error messages.
5936 Two major differences between the behavior of
5937 `convert_for_assignment' and `convert_for_initialization'
5938 are that references are bashed in the former, while
5939 copied in the latter, and aggregates are assigned in
5940 the former (operator=) while initialized in the
5943 If using constructor make sure no conversion operator exists, if one does
5944 exist, an ambiguity exists.
5946 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
5949 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
5950 tree exp, type, rhs;
5952 const char *errtype;
5956 register enum tree_code codel = TREE_CODE (type);
5957 register tree rhstype;
5958 register enum tree_code coder;
5960 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5961 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
5962 if (TREE_CODE (rhs) == NOP_EXPR
5963 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
5964 && codel != REFERENCE_TYPE)
5965 rhs = TREE_OPERAND (rhs, 0);
5967 if (rhs == error_mark_node
5968 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
5969 return error_mark_node;
5971 if (TREE_CODE (rhs) == OFFSET_REF)
5973 rhs = resolve_offset_ref (rhs);
5974 if (rhs == error_mark_node)
5975 return error_mark_node;
5978 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
5979 rhs = convert_from_reference (rhs);
5981 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
5982 && TREE_CODE (type) != ARRAY_TYPE
5983 && (TREE_CODE (type) != REFERENCE_TYPE
5984 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
5985 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
5986 && (TREE_CODE (type) != REFERENCE_TYPE
5987 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
5988 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
5989 rhs = default_conversion (rhs);
5991 rhstype = TREE_TYPE (rhs);
5992 coder = TREE_CODE (rhstype);
5994 if (coder == ERROR_MARK)
5995 return error_mark_node;
5997 /* We accept references to incomplete types, so we can
5998 return here before checking if RHS is of complete type. */
6000 if (codel == REFERENCE_TYPE)
6002 /* This should eventually happen in convert_arguments. */
6003 int savew = 0, savee = 0;
6006 savew = warningcount, savee = errorcount;
6007 rhs = initialize_reference (type, rhs);
6010 if (warningcount > savew)
6011 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6012 else if (errorcount > savee)
6013 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6019 exp = require_complete_type (exp);
6020 if (exp == error_mark_node)
6021 return error_mark_node;
6023 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6024 rhstype = TREE_TYPE (rhstype);
6026 type = complete_type (type);
6028 if (IS_AGGR_TYPE (type))
6029 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6031 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6034 /* Expand an ASM statement with operands, handling output operands
6035 that are not variables or INDIRECT_REFS by transforming such
6036 cases into cases that expand_asm_operands can handle.
6038 Arguments are same as for expand_asm_operands.
6040 We don't do default conversions on all inputs, because it can screw
6041 up operands that are expected to be in memory. */
6044 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6045 tree string, outputs, inputs, clobbers;
6047 const char *filename;
6050 int noutputs = list_length (outputs);
6052 /* o[I] is the place that output number I should be written. */
6053 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6056 /* Record the contents of OUTPUTS before it is modified. */
6057 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6058 o[i] = TREE_VALUE (tail);
6060 /* Generate the ASM_OPERANDS insn;
6061 store into the TREE_VALUEs of OUTPUTS some trees for
6062 where the values were actually stored. */
6063 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6065 /* Copy all the intermediate outputs into the specified outputs. */
6066 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6068 if (o[i] != TREE_VALUE (tail))
6070 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6071 const0_rtx, VOIDmode, EXPAND_NORMAL);
6074 /* Restore the original value so that it's correct the next
6075 time we expand this function. */
6076 TREE_VALUE (tail) = o[i];
6078 /* Detect modification of read-only values.
6079 (Otherwise done by build_modify_expr.) */
6082 tree type = TREE_TYPE (o[i]);
6083 if (type != error_mark_node
6084 && (CP_TYPE_CONST_P (type)
6085 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6086 && C_TYPE_FIELDS_READONLY (type))))
6087 readonly_error (o[i], "modification by `asm'", 1);
6091 /* Those MODIFY_EXPRs could do autoincrements. */
6095 /* If RETVAL is the address of, or a reference to, a local variable or
6096 temporary give an appropraite warning. */
6099 maybe_warn_about_returning_address_of_local (retval)
6102 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6103 tree whats_returned = retval;
6107 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6108 whats_returned = TREE_OPERAND (whats_returned, 1);
6109 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6110 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6111 || TREE_CODE (whats_returned) == NOP_EXPR)
6112 whats_returned = TREE_OPERAND (whats_returned, 0);
6117 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6119 whats_returned = TREE_OPERAND (whats_returned, 0);
6121 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6123 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6124 || TREE_CODE (whats_returned) == TARGET_EXPR)
6126 /* Get the target. */
6127 whats_returned = TREE_OPERAND (whats_returned, 0);
6128 warning ("returning reference to temporary");
6131 if (TREE_CODE (whats_returned) == VAR_DECL
6132 && DECL_NAME (whats_returned)
6133 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6135 warning ("reference to non-lvalue returned");
6140 if (TREE_CODE (whats_returned) == VAR_DECL
6141 && DECL_NAME (whats_returned)
6142 && DECL_FUNCTION_SCOPE_P (whats_returned)
6143 && !(TREE_STATIC (whats_returned)
6144 || TREE_PUBLIC (whats_returned)))
6146 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6147 cp_warning_at ("reference to local variable `%D' returned",
6150 cp_warning_at ("address of local variable `%D' returned",
6156 /* Check that returning RETVAL from the current function is valid.
6157 Return an expression explicitly showing all conversions required to
6158 change RETVAL into the function return type, and to assign it to
6159 the DECL_RESULT for the function. */
6162 check_return_expr (retval)
6166 /* The type actually returned by the function, after any
6169 int fn_returns_value_p;
6171 /* A `volatile' function is one that isn't supposed to return, ever.
6172 (This is a G++ extension, used to get better code for functions
6173 that call the `volatile' function.) */
6174 if (TREE_THIS_VOLATILE (current_function_decl))
6175 warning ("function declared `noreturn' has a `return' statement");
6177 /* Check for various simple errors. */
6178 if (DECL_DESTRUCTOR_P (current_function_decl))
6181 error ("returning a value from a destructor");
6184 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6186 if (in_function_try_handler)
6187 /* If a return statement appears in a handler of the
6188 function-try-block of a constructor, the program is ill-formed. */
6189 error ("cannot return from a handler of a function-try-block of a constructor");
6191 /* You can't return a value from a constructor. */
6192 error ("returning a value from a constructor");
6196 if (processing_template_decl)
6198 current_function_returns_value = 1;
6202 /* When no explicit return-value is given in a function with a named
6203 return value, the named return value is used. */
6204 result = DECL_RESULT (current_function_decl);
6205 valtype = TREE_TYPE (result);
6206 my_friendly_assert (valtype != NULL_TREE, 19990924);
6207 fn_returns_value_p = !VOID_TYPE_P (valtype);
6208 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6211 /* Check for a return statement with no return value in a function
6212 that's supposed to return a value. */
6213 if (!retval && fn_returns_value_p)
6215 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6216 /* Clear this, so finish_function won't say that we reach the
6217 end of a non-void function (which we don't, we gave a
6219 current_function_returns_null = 0;
6221 /* Check for a return statement with a value in a function that
6222 isn't supposed to return a value. */
6223 else if (retval && !fn_returns_value_p)
6225 if (VOID_TYPE_P (TREE_TYPE (retval)))
6226 /* You can return a `void' value from a function of `void'
6227 type. In that case, we have to evaluate the expression for
6228 its side-effects. */
6229 finish_expr_stmt (retval);
6231 pedwarn ("return-statement with a value, in function declared with a void return type");
6233 current_function_returns_null = 1;
6235 /* There's really no value to return, after all. */
6239 /* Remember that this function can sometimes return without a
6241 current_function_returns_null = 1;
6243 /* Remember that this function did return a value. */
6244 current_function_returns_value = 1;
6246 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6247 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6248 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6249 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6251 && null_ptr_cst_p (retval))
6252 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6254 /* Effective C++ rule 15. See also start_function. */
6256 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6257 && retval != current_class_ref)
6258 warning ("`operator=' should return a reference to `*this'");
6260 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6262 [...] For a function with a class return type, if the expression
6263 in the return statement is the name of a local object, and the cv-
6264 unqualified type of the local object is the same as the function
6265 return type, an implementation is permitted to omit creating the tem-
6266 porary object to hold the function return value [...]
6268 So, if this is a value-returning function that always returns the same
6269 local variable, remember it.
6271 It might be nice to be more flexible, and choose the first suitable
6272 variable even if the function sometimes returns something else, but
6273 then we run the risk of clobbering the variable we chose if the other
6274 returned expression uses the chosen variable somehow. And people expect
6275 this restriction, anyway. (jason 2000-11-19)
6277 See finish_function, genrtl_start_function, and declare_return_variable
6278 for other pieces of this optimization. */
6280 if (fn_returns_value_p && flag_elide_constructors)
6282 if (retval != NULL_TREE
6283 && (current_function_return_value == NULL_TREE
6284 || current_function_return_value == retval)
6285 && TREE_CODE (retval) == VAR_DECL
6286 && DECL_CONTEXT (retval) == current_function_decl
6287 && ! TREE_STATIC (retval)
6288 && (DECL_ALIGN (retval)
6289 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6290 && same_type_p ((TYPE_MAIN_VARIANT
6291 (TREE_TYPE (retval))),
6293 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6294 current_function_return_value = retval;
6296 current_function_return_value = error_mark_node;
6299 /* We don't need to do any conversions when there's nothing being
6301 if (!retval || retval == error_mark_node)
6304 /* Do any required conversions. */
6305 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6306 /* No conversions are required. */
6310 /* The type the function is declared to return. */
6311 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6313 /* First convert the value to the function's return type, then
6314 to the type of return value's location to handle the
6315 case that functype is smaller than the valtype. */
6316 retval = convert_for_initialization
6317 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6318 "return", NULL_TREE, 0);
6319 retval = convert (valtype, retval);
6321 /* If the conversion failed, treat this just like `return;'. */
6322 if (retval == error_mark_node)
6324 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6325 else if (! current_function_returns_struct
6326 && TREE_CODE (retval) == TARGET_EXPR
6327 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6328 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6329 TREE_OPERAND (retval, 0));
6331 maybe_warn_about_returning_address_of_local (retval);
6334 /* Actually copy the value returned into the appropriate location. */
6335 if (retval && retval != result)
6336 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6342 /* Returns nonzero if the pointer-type FROM can be converted to the
6343 pointer-type TO via a qualification conversion. If CONSTP is -1,
6344 then we return nonzero if the pointers are similar, and the
6345 cv-qualification signature of FROM is a proper subset of that of TO.
6347 If CONSTP is positive, then all outer pointers have been
6351 comp_ptr_ttypes_real (to, from, constp)
6355 int to_more_cv_qualified = 0;
6357 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6359 if (TREE_CODE (to) != TREE_CODE (from))
6362 if (TREE_CODE (from) == OFFSET_TYPE
6363 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6364 TYPE_OFFSET_BASETYPE (to)))
6367 /* Const and volatile mean something different for function types,
6368 so the usual checks are not appropriate. */
6369 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6371 if (!at_least_as_qualified_p (to, from))
6374 if (!at_least_as_qualified_p (from, to))
6379 ++to_more_cv_qualified;
6383 constp &= TYPE_READONLY (to);
6386 if (TREE_CODE (to) != POINTER_TYPE)
6388 same_type_ignoring_top_level_qualifiers_p (to, from)
6389 && (constp >= 0 || to_more_cv_qualified);
6393 /* When comparing, say, char ** to char const **, this function takes the
6394 'char *' and 'char const *'. Do not pass non-pointer types to this
6398 comp_ptr_ttypes (to, from)
6401 return comp_ptr_ttypes_real (to, from, 1);
6404 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6405 type or inheritance-related types, regardless of cv-quals. */
6408 ptr_reasonably_similar (to, from)
6411 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6413 /* Any target type is similar enough to void. */
6414 if (TREE_CODE (to) == VOID_TYPE
6415 || TREE_CODE (from) == VOID_TYPE)
6418 if (TREE_CODE (to) != TREE_CODE (from))
6421 if (TREE_CODE (from) == OFFSET_TYPE
6422 && comptypes (TYPE_OFFSET_BASETYPE (to),
6423 TYPE_OFFSET_BASETYPE (from),
6424 COMPARE_BASE | COMPARE_RELAXED))
6427 if (TREE_CODE (to) == INTEGER_TYPE
6428 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6431 if (TREE_CODE (to) == FUNCTION_TYPE)
6434 if (TREE_CODE (to) != POINTER_TYPE)
6436 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6437 COMPARE_BASE | COMPARE_RELAXED);
6441 /* Like comp_ptr_ttypes, for const_cast. */
6444 comp_ptr_ttypes_const (to, from)
6447 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6449 if (TREE_CODE (to) != TREE_CODE (from))
6452 if (TREE_CODE (from) == OFFSET_TYPE
6453 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6454 TYPE_OFFSET_BASETYPE (to)))
6457 if (TREE_CODE (to) != POINTER_TYPE)
6458 return same_type_ignoring_top_level_qualifiers_p (to, from);
6462 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6465 comp_ptr_ttypes_reinterpret (to, from)
6470 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6472 if (TREE_CODE (from) == OFFSET_TYPE)
6473 from = TREE_TYPE (from);
6474 if (TREE_CODE (to) == OFFSET_TYPE)
6475 to = TREE_TYPE (to);
6477 /* Const and volatile mean something different for function types,
6478 so the usual checks are not appropriate. */
6479 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6480 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6482 if (!at_least_as_qualified_p (to, from))
6486 && !at_least_as_qualified_p (from, to))
6488 constp &= TYPE_READONLY (to);
6491 if (TREE_CODE (from) != POINTER_TYPE
6492 || TREE_CODE (to) != POINTER_TYPE)
6497 /* Returns the type qualifiers for this type, including the qualifiers on the
6498 elements for an array type. */
6501 cp_type_quals (type)
6504 type = strip_array_types (type);
6505 if (type == error_mark_node)
6506 return TYPE_UNQUALIFIED;
6507 return TYPE_QUALS (type);
6510 /* Returns nonzero if the TYPE contains a mutable member */
6513 cp_has_mutable_p (type)
6516 type = strip_array_types (type);
6518 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6521 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6522 exemplar types such that casting T1 to T2 is casting away castness
6523 if and only if there is no implicit conversion from T1 to T2. */
6526 casts_away_constness_r (t1, t2)
6533 /* [expr.const.cast]
6535 For multi-level pointer to members and multi-level mixed pointers
6536 and pointers to members (conv.qual), the "member" aspect of a
6537 pointer to member level is ignored when determining if a const
6538 cv-qualifier has been cast away. */
6539 if (TYPE_PTRMEM_P (*t1))
6540 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6541 if (TYPE_PTRMEM_P (*t2))
6542 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6544 /* [expr.const.cast]
6546 For two pointer types:
6548 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6549 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6552 casting from X1 to X2 casts away constness if, for a non-pointer
6553 type T there does not exist an implicit conversion (clause
6556 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6560 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6562 if (TREE_CODE (*t1) != POINTER_TYPE
6563 || TREE_CODE (*t2) != POINTER_TYPE)
6565 *t1 = cp_build_qualified_type (void_type_node,
6566 cp_type_quals (*t1));
6567 *t2 = cp_build_qualified_type (void_type_node,
6568 cp_type_quals (*t2));
6572 quals1 = cp_type_quals (*t1);
6573 quals2 = cp_type_quals (*t2);
6574 *t1 = TREE_TYPE (*t1);
6575 *t2 = TREE_TYPE (*t2);
6576 casts_away_constness_r (t1, t2);
6577 *t1 = build_pointer_type (*t1);
6578 *t2 = build_pointer_type (*t2);
6579 *t1 = cp_build_qualified_type (*t1, quals1);
6580 *t2 = cp_build_qualified_type (*t2, quals2);
6583 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
6587 casts_away_constness (t1, t2)
6591 if (TREE_CODE (t2) == REFERENCE_TYPE)
6593 /* [expr.const.cast]
6595 Casting from an lvalue of type T1 to an lvalue of type T2
6596 using a reference cast casts away constness if a cast from an
6597 rvalue of type "pointer to T1" to the type "pointer to T2"
6598 casts away constness. */
6599 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
6600 ? TREE_TYPE (t1) : t1);
6601 return casts_away_constness (build_pointer_type (t1),
6602 build_pointer_type (TREE_TYPE (t2)));
6605 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6606 /* [expr.const.cast]
6608 Casting from an rvalue of type "pointer to data member of X
6609 of type T1" to the type "pointer to data member of Y of type
6610 T2" casts away constness if a cast from an rvalue of type
6611 "pointer to T1" to the type "pointer to T2" casts away
6613 return casts_away_constness
6614 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
6615 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
6617 /* Casting away constness is only something that makes sense for
6618 pointer or reference types. */
6619 if (TREE_CODE (t1) != POINTER_TYPE
6620 || TREE_CODE (t2) != POINTER_TYPE)
6623 /* Top-level qualifiers don't matter. */
6624 t1 = TYPE_MAIN_VARIANT (t1);
6625 t2 = TYPE_MAIN_VARIANT (t2);
6626 casts_away_constness_r (&t1, &t2);
6627 if (!can_convert (t2, t1))
6633 /* Returns TYPE with its cv qualifiers removed
6634 TYPE is T cv* .. *cv where T is not a pointer type,
6635 returns T * .. *. (If T is an array type, then the cv qualifiers
6636 above are those of the array members.) */
6639 strip_all_pointer_quals (type)
6642 if (TREE_CODE (type) == POINTER_TYPE)
6643 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
6644 else if (TREE_CODE (type) == OFFSET_TYPE)
6645 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
6646 strip_all_pointer_quals (TREE_TYPE (type)));
6648 return TYPE_MAIN_VARIANT (type);