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 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC 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 GNU CC 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 GNU CC; 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). */
43 #include "diagnostic.h"
46 static tree convert_for_assignment PARAMS ((tree, tree, const char *, tree,
48 static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree));
49 static tree rationalize_conditional_expr PARAMS ((enum tree_code, tree));
50 static int comp_target_parms PARAMS ((tree, tree));
51 static int comp_ptr_ttypes_real PARAMS ((tree, tree, int));
52 static int comp_ptr_ttypes_const PARAMS ((tree, tree));
53 static int comp_ptr_ttypes_reinterpret PARAMS ((tree, tree));
54 static int comp_except_types PARAMS ((tree, tree, int));
55 static int comp_array_types PARAMS ((int (*) (tree, tree, int), tree,
57 static tree common_base_type PARAMS ((tree, tree));
58 static tree lookup_anon_field PARAMS ((tree, tree));
59 static tree pointer_diff PARAMS ((tree, tree, tree));
60 static tree build_component_addr PARAMS ((tree, tree));
61 static tree qualify_type_recursive PARAMS ((tree, tree));
62 static tree get_delta_difference PARAMS ((tree, tree, int));
63 static int comp_cv_target_types PARAMS ((tree, tree, int));
64 static void casts_away_constness_r PARAMS ((tree *, tree *));
65 static int casts_away_constness PARAMS ((tree, tree));
66 static void maybe_warn_about_returning_address_of_local PARAMS ((tree));
67 static tree strip_all_pointer_quals PARAMS ((tree));
69 /* Return the target type of TYPE, which means return T for:
70 T*, T&, T[], T (...), and otherwise, just T. */
76 if (TREE_CODE (type) == REFERENCE_TYPE)
77 type = TREE_TYPE (type);
78 while (TREE_CODE (type) == POINTER_TYPE
79 || TREE_CODE (type) == ARRAY_TYPE
80 || TREE_CODE (type) == FUNCTION_TYPE
81 || TREE_CODE (type) == METHOD_TYPE
82 || TREE_CODE (type) == OFFSET_TYPE)
83 type = TREE_TYPE (type);
87 /* Do `exp = require_complete_type (exp);' to make sure exp
88 does not have an incomplete type. (That includes void types.)
89 Returns the error_mark_node if the VALUE does not have
90 complete type when this function returns. */
93 require_complete_type (value)
98 if (processing_template_decl || value == error_mark_node)
101 if (TREE_CODE (value) == OVERLOAD)
102 type = unknown_type_node;
104 type = TREE_TYPE (value);
106 /* First, detect a valid value with a complete type. */
107 if (COMPLETE_TYPE_P (type))
110 /* If we see X::Y, we build an OFFSET_TYPE which has
111 not been laid out. Try to avoid an error by interpreting
112 it as this->X::Y, if reasonable. */
113 if (TREE_CODE (value) == OFFSET_REF
114 && current_class_ref != 0
115 && TREE_OPERAND (value, 0) == current_class_ref)
117 tree base, member = TREE_OPERAND (value, 1);
118 tree basetype = TYPE_OFFSET_BASETYPE (type);
119 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
120 base = convert_pointer_to (basetype, current_class_ptr);
121 value = build (COMPONENT_REF, TREE_TYPE (member),
122 build_indirect_ref (base, NULL), member);
123 return require_complete_type (value);
126 if (complete_type_or_else (type, value))
129 return error_mark_node;
132 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
133 a template instantiation, do the instantiation. Returns TYPE,
134 whether or not it could be completed, unless something goes
135 horribly wrong, in which case the error_mark_node is returned. */
141 if (type == NULL_TREE)
142 /* Rather than crash, we return something sure to cause an error
144 return error_mark_node;
146 if (type == error_mark_node || COMPLETE_TYPE_P (type))
148 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
150 tree t = complete_type (TREE_TYPE (type));
151 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
153 TYPE_NEEDS_CONSTRUCTING (type)
154 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
155 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
156 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
158 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
159 instantiate_class_template (TYPE_MAIN_VARIANT (type));
164 /* Like complete_type, but issue an error if the TYPE cannot be
165 completed. VALUE is used for informative diagnostics.
166 Returns NULL_TREE if the type cannot be made complete. */
169 complete_type_or_else (type, value)
173 type = complete_type (type);
174 if (type == error_mark_node)
175 /* We already issued an error. */
177 else if (!COMPLETE_TYPE_P (type))
179 incomplete_type_error (value, type);
186 /* Return truthvalue of whether type of EXP is instantiated. */
192 return (TREE_CODE (exp) == OVERLOAD
193 || TREE_CODE (exp) == TREE_LIST
194 || TREE_TYPE (exp) == unknown_type_node
195 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
196 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
199 /* Return a pointer or pointer to member type similar to T1, with a
200 cv-qualification signature that is the union of the cv-qualification
201 signatures of T1 and T2: [expr.rel], [expr.eq]. */
204 qualify_type_recursive (t1, t2)
207 if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
208 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
210 tree tt1 = TREE_TYPE (t1);
211 tree tt2 = TREE_TYPE (t2);
215 tree attributes = (*targetm.merge_type_attributes) (t1, t2);
217 if (TREE_CODE (tt1) == OFFSET_TYPE)
219 b1 = TYPE_OFFSET_BASETYPE (tt1);
220 tt1 = TREE_TYPE (tt1);
221 tt2 = TREE_TYPE (tt2);
226 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
227 tgt = qualify_type_recursive (tt1, tt2);
228 tgt = cp_build_qualified_type (tgt, type_quals);
230 tgt = build_offset_type (b1, tgt);
231 t1 = build_pointer_type (tgt);
232 t1 = build_type_attribute_variant (t1, attributes);
237 /* Return the common type of two parameter lists.
238 We assume that comptypes has already been done and returned 1;
239 if that isn't so, this may crash.
241 As an optimization, free the space we allocate if the parameter
242 lists are already common. */
248 tree oldargs = p1, newargs, n;
252 len = list_length (p1);
253 newargs = tree_last (p1);
255 if (newargs == void_list_node)
264 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
269 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
271 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
273 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
276 else if (! TREE_PURPOSE (p1))
278 if (TREE_PURPOSE (p2))
280 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
286 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
288 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
290 if (TREE_VALUE (p1) != TREE_VALUE (p2))
293 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
296 TREE_VALUE (n) = TREE_VALUE (p1);
304 /* Given a type, perhaps copied for a typedef,
305 find the "original" version of it. */
310 while (TYPE_NAME (t) != NULL_TREE)
312 tree x = TYPE_NAME (t);
313 if (TREE_CODE (x) != TYPE_DECL)
315 x = DECL_ORIGINAL_TYPE (x);
323 /* T1 and T2 are arithmetic or enumeration types. Return the type
324 that will result from the "usual arithmetic converions" on T1 and
325 T2 as described in [expr]. */
328 type_after_usual_arithmetic_conversions (t1, t2)
332 enum tree_code code1 = TREE_CODE (t1);
333 enum tree_code code2 = TREE_CODE (t2);
336 /* FIXME: Attributes. */
337 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
338 || TREE_CODE (t1) == ENUMERAL_TYPE,
340 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
341 || TREE_CODE (t2) == ENUMERAL_TYPE,
344 /* In what follows, we slightly generalize the rules given in [expr]
345 so as to deal with `long long'. First, merge the attributes. */
346 attributes = (*targetm.merge_type_attributes) (t1, t2);
348 /* If only one is real, use it as the result. */
349 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
350 return build_type_attribute_variant (t1, attributes);
351 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
352 return build_type_attribute_variant (t2, attributes);
354 /* Perform the integral promotions. */
355 if (code1 != REAL_TYPE)
357 t1 = type_promotes_to (t1);
358 t2 = type_promotes_to (t2);
361 /* Both real or both integers; use the one with greater precision. */
362 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
363 return build_type_attribute_variant (t1, attributes);
364 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
365 return build_type_attribute_variant (t2, attributes);
367 if (code1 != REAL_TYPE)
369 /* If one is a sizetype, use it so size_binop doesn't blow up. */
370 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
371 return build_type_attribute_variant (t1, attributes);
372 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
373 return build_type_attribute_variant (t2, attributes);
375 /* If one is unsigned long long, then convert the other to unsigned
377 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
378 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
379 return build_type_attribute_variant (long_long_unsigned_type_node,
381 /* If one is a long long, and the other is an unsigned long, and
382 long long can represent all the values of an unsigned long, then
383 convert to a long long. Otherwise, convert to an unsigned long
384 long. Otherwise, if either operand is long long, convert the
387 Since we're here, we know the TYPE_PRECISION is the same;
388 therefore converting to long long cannot represent all the values
389 of an unsigned long, so we choose unsigned long long in that
391 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
392 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
394 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
395 ? long_long_unsigned_type_node
396 : long_long_integer_type_node);
397 return build_type_attribute_variant (t, attributes);
400 /* Go through the same procedure, but for longs. */
401 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
402 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
403 return build_type_attribute_variant (long_unsigned_type_node,
405 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
406 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
408 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
409 ? long_unsigned_type_node : long_integer_type_node);
410 return build_type_attribute_variant (t, attributes);
412 /* Otherwise prefer the unsigned one. */
413 if (TREE_UNSIGNED (t1))
414 return build_type_attribute_variant (t1, attributes);
416 return build_type_attribute_variant (t2, attributes);
420 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
421 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
422 return build_type_attribute_variant (long_double_type_node,
424 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
425 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
426 return build_type_attribute_variant (double_type_node,
429 return build_type_attribute_variant (float_type_node,
434 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
435 ARG1 and ARG2 are the values with those types. The LOCATION is a
436 string describing the current location, in case an error occurs. */
439 composite_pointer_type (t1, t2, arg1, arg2, location)
444 const char* location;
450 If one operand is a null pointer constant, the composite pointer
451 type is the type of the other operand. */
452 if (null_ptr_cst_p (arg1))
454 if (null_ptr_cst_p (arg2))
457 /* Deal with pointer-to-member functions in the same way as we deal
458 with pointers to functions. */
459 if (TYPE_PTRMEMFUNC_P (t1))
460 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
461 if (TYPE_PTRMEMFUNC_P (t2))
462 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
468 If one of the operands has type "pointer to cv1 void*", then
469 the other has type "pointer to cv2T", and the composite pointer
470 type is "pointer to cv12 void", where cv12 is the union of cv1
473 If either type is a pointer to void, make sure it is T1. */
474 if (VOID_TYPE_P (TREE_TYPE (t2)))
481 /* Now, if T1 is a pointer to void, merge the qualifiers. */
482 if (VOID_TYPE_P (TREE_TYPE (t1)))
484 if (pedantic && TYPE_PTRFN_P (t2))
485 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
488 result_type = cp_build_qualified_type (void_type_node,
490 | CP_TYPE_QUALS (t2)));
491 result_type = build_pointer_type (result_type);
495 tree full1 = qualify_type_recursive (t1, t2);
496 tree full2 = qualify_type_recursive (t2, t1);
498 int val = comp_target_types (full1, full2, 1);
506 cp_pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
508 result_type = ptr_type_node;
515 /* Return the common type of two types.
516 We assume that comptypes has already been done and returned 1;
517 if that isn't so, this may crash.
519 This is the type for the result of most arithmetic operations
520 if the operands have the given two types.
522 We do not deal with enumeral types here because they have already been
523 converted to integer types. */
529 register enum tree_code code1;
530 register enum tree_code code2;
533 /* Save time if the two types are the same. */
536 t1 = original_type (t1);
537 t2 = original_type (t2);
541 /* If one type is nonsense, use the other. */
542 if (t1 == error_mark_node)
544 if (t2 == error_mark_node)
547 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
548 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
549 return type_after_usual_arithmetic_conversions (t1, t2);
551 /* Merge the attributes. */
552 attributes = (*targetm.merge_type_attributes) (t1, t2);
554 /* Treat an enum type as the unsigned integer type of the same width. */
556 if (TREE_CODE (t1) == ENUMERAL_TYPE)
557 t1 = type_for_size (TYPE_PRECISION (t1), 1);
558 if (TREE_CODE (t2) == ENUMERAL_TYPE)
559 t2 = type_for_size (TYPE_PRECISION (t2), 1);
561 if (TYPE_PTRMEMFUNC_P (t1))
562 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
563 if (TYPE_PTRMEMFUNC_P (t2))
564 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
566 code1 = TREE_CODE (t1);
567 code2 = TREE_CODE (t2);
569 /* If one type is complex, form the common type of the non-complex
570 components, then make that complex. Use T1 or T2 if it is the
572 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
574 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
575 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
576 tree subtype = common_type (subtype1, subtype2);
578 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
579 return build_type_attribute_variant (t1, attributes);
580 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
581 return build_type_attribute_variant (t2, attributes);
583 return build_type_attribute_variant (build_complex_type (subtype),
591 /* We should have called type_after_usual_arithmetic_conversions
593 my_friendly_abort (19990725);
598 /* For two pointers, do this recursively on the target type,
599 and combine the qualifiers of the two types' targets. */
600 /* This code was turned off; I don't know why.
601 But ISO C++ specifies doing this with the qualifiers.
602 So I turned it on again. */
604 tree tt1 = TREE_TYPE (t1);
605 tree tt2 = TREE_TYPE (t2);
610 if (TREE_CODE (tt1) == OFFSET_TYPE)
612 b1 = TYPE_OFFSET_BASETYPE (tt1);
613 b2 = TYPE_OFFSET_BASETYPE (tt2);
614 tt1 = TREE_TYPE (tt1);
615 tt2 = TREE_TYPE (tt2);
620 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
621 tt1 = TYPE_MAIN_VARIANT (tt1);
622 tt2 = TYPE_MAIN_VARIANT (tt2);
626 else if (VOID_TYPE_P (tt1) || VOID_TYPE_P (tt2))
627 target = void_type_node;
628 else if (tt1 == unknown_type_node)
630 else if (tt2 == unknown_type_node)
633 target = common_type (tt1, tt2);
635 target = cp_build_qualified_type (target, type_quals);
639 if (same_type_p (b1, b2)
640 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
641 target = build_offset_type (b2, target);
642 else if (binfo_or_else (b2, b1))
643 target = build_offset_type (b1, target);
646 if (code1 == POINTER_TYPE)
647 t1 = build_pointer_type (target);
649 t1 = build_reference_type (target);
650 t1 = build_type_attribute_variant (t1, attributes);
652 if (TREE_CODE (target) == METHOD_TYPE)
653 t1 = build_ptrmemfunc_type (t1);
660 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
661 /* Save space: see if the result is identical to one of the args. */
662 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
663 return build_type_attribute_variant (t1, attributes);
664 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
665 return build_type_attribute_variant (t2, attributes);
666 /* Merge the element types, and have a size if either arg has one. */
667 t1 = build_cplus_array_type
668 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
669 return build_type_attribute_variant (t1, attributes);
673 /* Function types: prefer the one that specified arg types.
674 If both do, merge the arg types. Also merge the return types. */
676 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
677 tree p1 = TYPE_ARG_TYPES (t1);
678 tree p2 = TYPE_ARG_TYPES (t2);
681 /* Save space: see if the result is identical to one of the args. */
682 if (valtype == TREE_TYPE (t1) && ! p2)
683 return build_type_attribute_variant (t1, attributes);
684 if (valtype == TREE_TYPE (t2) && ! p1)
685 return build_type_attribute_variant (t2, attributes);
687 /* Simple way if one arg fails to specify argument types. */
688 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
690 rval = build_function_type (valtype, p2);
691 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
692 rval = build_exception_variant (rval, raises);
693 return build_type_attribute_variant (rval, attributes);
695 raises = TYPE_RAISES_EXCEPTIONS (t1);
696 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
698 rval = build_function_type (valtype, p1);
700 rval = build_exception_variant (rval, raises);
701 return build_type_attribute_variant (rval, attributes);
704 rval = build_function_type (valtype, commonparms (p1, p2));
705 rval = build_exception_variant (rval, raises);
706 return build_type_attribute_variant (rval, attributes);
711 t1 = TYPE_MAIN_VARIANT (t1);
712 t2 = TYPE_MAIN_VARIANT (t2);
714 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
715 return build_type_attribute_variant (t1, attributes);
716 else if (binfo_or_else (t2, t1))
717 return build_type_attribute_variant (t2, attributes);
720 compiler_error ("common_type called with uncommon aggregate types");
721 return error_mark_node;
725 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
727 /* Get this value the long way, since TYPE_METHOD_BASETYPE
728 is just the main variant of this. */
732 tree b1 = TYPE_OFFSET_BASETYPE (t1);
733 tree b2 = TYPE_OFFSET_BASETYPE (t2);
735 if (same_type_p (b1, b2)
736 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
737 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
740 if (binfo_or_else (b2, b1) == NULL_TREE)
741 compiler_error ("common_type called with uncommon method types");
742 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
745 raises = TYPE_RAISES_EXCEPTIONS (t1);
747 /* If this was a member function type, get back to the
748 original type of type member function (i.e., without
749 the class instance variable up front. */
750 t1 = build_function_type (TREE_TYPE (t1),
751 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
752 t2 = build_function_type (TREE_TYPE (t2),
753 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
754 t3 = common_type (t1, t2);
755 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
756 TYPE_ARG_TYPES (t3));
757 t1 = build_exception_variant (t3, raises);
760 compiler_error ("common_type called with uncommon method types");
762 return build_type_attribute_variant (t1, attributes);
765 /* Pointers to members should now be handled by the POINTER_TYPE
767 my_friendly_abort (990325);
770 return build_type_attribute_variant (t1, attributes);
774 /* Compare two exception specifier types for exactness or subsetness, if
775 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
777 [except.spec] "If a class X ... objects of class X or any class publicly
778 and unambigously derrived from X. Similarly, if a pointer type Y * ...
779 exceptions of type Y * or that are pointers to any type publicly and
780 unambigously derrived from Y. Otherwise a function only allows exceptions
781 that have the same type ..."
782 This does not mention cv qualifiers and is different to what throw
783 [except.throw] and catch [except.catch] will do. They will ignore the
784 top level cv qualifiers, and allow qualifiers in the pointer to class
787 We implement the letter of the standard. */
790 comp_except_types (a, b, exact)
794 if (same_type_p (a, b))
798 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
801 if (TREE_CODE (a) == POINTER_TYPE
802 && TREE_CODE (b) == POINTER_TYPE)
806 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
810 if (TREE_CODE (a) != RECORD_TYPE
811 || TREE_CODE (b) != RECORD_TYPE)
814 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
820 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
821 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
822 otherwise it must be exact. Exception lists are unordered, but
823 we've already filtered out duplicates. Most lists will be in order,
824 we should try to make use of that. */
827 comp_except_specs (t1, t2, exact)
838 if (t1 == NULL_TREE) /* T1 is ... */
839 return t2 == NULL_TREE || !exact;
840 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
841 return t2 != NULL_TREE && !TREE_VALUE (t2);
842 if (t2 == NULL_TREE) /* T2 is ... */
844 if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
847 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
848 Count how many we find, to determine exactness. For exact matching and
849 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
851 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
853 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
855 tree a = TREE_VALUE (probe);
856 tree b = TREE_VALUE (t2);
858 if (comp_except_types (a, b, exact))
860 if (probe == base && exact)
861 base = TREE_CHAIN (probe);
866 if (probe == NULL_TREE)
869 return !exact || base == NULL_TREE || length == list_length (t1);
872 /* Compare the array types T1 and T2, using CMP as the type comparison
873 function for the element types. STRICT is as for comptypes. */
876 comp_array_types (cmp, t1, t2, strict)
877 register int (*cmp) PARAMS ((tree, tree, int));
887 /* The type of the array elements must be the same. */
888 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
889 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
890 strict & ~COMPARE_REDECLARATION)))
893 d1 = TYPE_DOMAIN (t1);
894 d2 = TYPE_DOMAIN (t2);
899 /* If one of the arrays is dimensionless, and the other has a
900 dimension, they are of different types. However, it is legal to
908 declarations for an array object can specify
909 array types that differ by the presence or absence of a major
910 array bound (_dcl.array_). */
912 return strict & COMPARE_REDECLARATION;
914 /* Check that the dimensions are the same. */
915 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
917 && cp_tree_equal (TYPE_MAX_VALUE (d1),
918 TYPE_MAX_VALUE (d2)));
921 /* Return 1 if T1 and T2 are compatible types for assignment or
922 various other operations. STRICT is a bitwise-or of the COMPARE_*
926 comptypes (t1, t2, strict)
932 int orig_strict = strict;
934 /* The special exemption for redeclaring array types without an
935 array bound only applies at the top level:
940 is not legal, for example. */
941 strict &= ~COMPARE_REDECLARATION;
943 /* Suppress errors caused by previously reported errors */
947 /* This should never happen. */
948 my_friendly_assert (t1 != error_mark_node, 307);
950 if (t2 == error_mark_node)
953 /* If either type is the internal version of sizetype, return the
955 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
956 && TYPE_DOMAIN (t1) != 0)
957 t1 = TYPE_DOMAIN (t1);
959 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
960 && TYPE_DOMAIN (t2) != 0)
961 t2 = TYPE_DOMAIN (t2);
963 if (strict & COMPARE_RELAXED)
965 /* Treat an enum type as the unsigned integer type of the same width. */
967 if (TREE_CODE (t1) == ENUMERAL_TYPE)
968 t1 = type_for_size (TYPE_PRECISION (t1), 1);
969 if (TREE_CODE (t2) == ENUMERAL_TYPE)
970 t2 = type_for_size (TYPE_PRECISION (t2), 1);
976 if (TYPE_PTRMEMFUNC_P (t1))
977 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
978 if (TYPE_PTRMEMFUNC_P (t2))
979 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
981 /* Different classes of types can't be compatible. */
982 if (TREE_CODE (t1) != TREE_CODE (t2))
985 /* Qualifiers must match. */
986 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
988 if (strict == COMPARE_STRICT
989 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
992 /* Allow for two different type nodes which have essentially the same
993 definition. Note that we already checked for equality of the type
994 qualifiers (just above). */
996 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
999 if (strict & COMPARE_NO_ATTRIBUTES)
1001 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1002 else if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
1005 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1008 switch (TREE_CODE (t1))
1010 case TEMPLATE_TEMPLATE_PARM:
1011 case BOUND_TEMPLATE_TEMPLATE_PARM:
1012 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1013 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1015 if (! comp_template_parms
1016 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1017 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1019 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1021 /* Don't check inheritance. */
1022 strict = COMPARE_STRICT;
1027 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1028 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1029 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
1030 val = comp_template_args (TYPE_TI_ARGS (t1),
1033 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1035 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1040 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1041 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1042 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1046 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1047 TYPE_RAISES_EXCEPTIONS (t2), 1))
1050 /* This case is anti-symmetrical!
1051 One can pass a base member (or member function)
1052 to something expecting a derived member (or member function),
1053 but not vice-versa! */
1055 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1056 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1060 case REFERENCE_TYPE:
1061 t1 = TREE_TYPE (t1);
1062 t2 = TREE_TYPE (t2);
1063 /* first, check whether the referred types match with the
1064 required level of strictness */
1065 val = comptypes (t1, t2, strict);
1068 if (TREE_CODE (t1) == RECORD_TYPE
1069 && TREE_CODE (t2) == RECORD_TYPE)
1074 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1075 TYPE_RAISES_EXCEPTIONS (t2), 1))
1078 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1079 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1080 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1084 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1085 here since this is the one place where
1086 COMPARE_REDECLARATION should be used. */
1087 val = comp_array_types (comptypes, t1, t2, orig_strict);
1090 case TEMPLATE_TYPE_PARM:
1091 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1092 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1095 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1096 TYPENAME_TYPE_FULLNAME (t2)) < 1)
1098 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1101 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1106 return attrval == 2 && val == 1 ? 2 : val;
1109 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1110 only in the same direction as the target type. */
1113 comp_cv_target_types (ttl, ttr, nptrs)
1119 if (!at_least_as_qualified_p (ttl, ttr)
1120 && !at_least_as_qualified_p (ttr, ttl))
1121 /* The qualifications are incomparable. */
1124 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1125 return more_qualified_p (ttr, ttl) ? -1 : 1;
1127 t = comp_target_types (ttl, ttr, nptrs);
1128 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1129 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1135 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1136 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1137 converted to TTL. Return -1 means that TTL can be converted to TTR but
1140 NPTRS is the number of pointers we can strip off and keep cool.
1141 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1142 but to not permit B** to convert to A**.
1144 This should go away. Callers should use can_convert or something
1145 similar instead. (jason 17 Apr 1997) */
1148 comp_target_types (ttl, ttr, nptrs)
1152 ttl = TYPE_MAIN_VARIANT (ttl);
1153 ttr = TYPE_MAIN_VARIANT (ttr);
1154 if (same_type_p (ttl, ttr))
1157 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1160 if ((TREE_CODE (ttr) == POINTER_TYPE
1161 || TREE_CODE (ttr) == REFERENCE_TYPE)
1162 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1163 of the type pointed to. This is necessary for reference init
1164 semantics. We won't get here from a previous call with nptrs == 1;
1165 for multi-level pointers we end up in comp_ptr_ttypes. */
1168 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1170 ttl = TREE_TYPE (ttl);
1171 ttr = TREE_TYPE (ttr);
1175 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1176 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1178 else if (TREE_CODE (ttl) == VOID_TYPE
1179 && TREE_CODE (ttr) != FUNCTION_TYPE
1180 && TREE_CODE (ttr) != METHOD_TYPE
1181 && TREE_CODE (ttr) != OFFSET_TYPE)
1183 else if (TREE_CODE (ttr) == VOID_TYPE
1184 && TREE_CODE (ttl) != FUNCTION_TYPE
1185 && TREE_CODE (ttl) != METHOD_TYPE
1186 && TREE_CODE (ttl) != OFFSET_TYPE)
1188 else if (TREE_CODE (ttl) == POINTER_TYPE
1189 || TREE_CODE (ttl) == ARRAY_TYPE)
1191 if (comp_ptr_ttypes (ttl, ttr))
1193 else if (comp_ptr_ttypes (ttr, ttl))
1199 /* Const and volatile mean something different for function types,
1200 so the usual checks are not appropriate. */
1201 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1202 return comp_target_types (ttl, ttr, nptrs - 1);
1204 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1207 if (TREE_CODE (ttr) == ARRAY_TYPE)
1208 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1209 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1216 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1221 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1230 argsl = TYPE_ARG_TYPES (ttl);
1231 argsr = TYPE_ARG_TYPES (ttr);
1233 /* Compare 'this' here, not in comp_target_parms. */
1234 if (TREE_CODE (ttr) == METHOD_TYPE)
1236 tree tl = TYPE_METHOD_BASETYPE (ttl);
1237 tree tr = TYPE_METHOD_BASETYPE (ttr);
1239 if (!same_or_base_type_p (tr, tl))
1241 if (same_or_base_type_p (tl, tr))
1247 argsl = TREE_CHAIN (argsl);
1248 argsr = TREE_CHAIN (argsr);
1251 switch (comp_target_parms (argsl, argsr))
1259 return saw_contra ? -1 : 1;
1262 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1266 /* Contravariance: we can assign a pointer to base member to a pointer
1267 to derived member. Note difference from simple pointer case, where
1268 we can pass a pointer to derived to a pointer to base. */
1269 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1270 TYPE_OFFSET_BASETYPE (ttl)))
1272 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1273 TYPE_OFFSET_BASETYPE (ttr)))
1283 ttl = TREE_TYPE (ttl);
1284 ttr = TREE_TYPE (ttr);
1286 if (TREE_CODE (ttl) == POINTER_TYPE
1287 || TREE_CODE (ttl) == ARRAY_TYPE)
1289 if (comp_ptr_ttypes (ttl, ttr))
1295 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1300 else if (IS_AGGR_TYPE (ttl))
1304 if (same_or_base_type_p (build_pointer_type (ttl),
1305 build_pointer_type (ttr)))
1307 if (same_or_base_type_p (build_pointer_type (ttr),
1308 build_pointer_type (ttl)))
1316 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1319 at_least_as_qualified_p (type1, type2)
1323 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1324 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1325 == CP_TYPE_QUALS (type2));
1328 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1331 more_qualified_p (type1, type2)
1335 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1336 && at_least_as_qualified_p (type1, type2));
1339 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1340 more cv-qualified that TYPE1, and 0 otherwise. */
1343 comp_cv_qualification (type1, type2)
1347 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1350 if (at_least_as_qualified_p (type1, type2))
1353 else if (at_least_as_qualified_p (type2, type1))
1359 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1360 subset of the cv-qualification signature of TYPE2, and the types
1361 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1364 comp_cv_qual_signature (type1, type2)
1368 if (comp_ptr_ttypes_real (type2, type1, -1))
1370 else if (comp_ptr_ttypes_real (type1, type2, -1))
1376 /* If two types share a common base type, return that basetype.
1377 If there is not a unique most-derived base type, this function
1378 returns ERROR_MARK_NODE. */
1381 common_base_type (tt1, tt2)
1384 tree best = NULL_TREE;
1387 /* If one is a baseclass of another, that's good enough. */
1388 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1390 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1393 /* Otherwise, try to find a unique baseclass of TT1
1394 that is shared by TT2, and follow that down. */
1395 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1397 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1398 tree trial = common_base_type (basetype, tt2);
1401 if (trial == error_mark_node)
1403 if (best == NULL_TREE)
1405 else if (best != trial)
1406 return error_mark_node;
1411 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1413 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1414 tree trial = common_base_type (tt1, basetype);
1417 if (trial == error_mark_node)
1419 if (best == NULL_TREE)
1421 else if (best != trial)
1422 return error_mark_node;
1428 /* Subroutines of `comptypes'. */
1430 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1431 equivalent in the sense that functions with those parameter types
1432 can have equivalent types. The two lists must be equivalent,
1435 C++: See comment above about TYPE1, TYPE2. */
1438 compparms (parms1, parms2)
1439 tree parms1, parms2;
1441 register tree t1 = parms1, t2 = parms2;
1443 /* An unspecified parmlist matches any specified parmlist
1444 whose argument types don't need default promotions. */
1448 if (t1 == 0 && t2 == 0)
1450 /* If one parmlist is shorter than the other,
1451 they fail to match. */
1452 if (t1 == 0 || t2 == 0)
1454 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1457 t1 = TREE_CHAIN (t1);
1458 t2 = TREE_CHAIN (t2);
1462 /* This really wants return whether or not parameter type lists
1463 would make their owning functions assignment compatible or not.
1465 The return value is like for comp_target_types.
1467 This should go away, possibly with the exception of the empty parmlist
1468 conversion; there are no conversions between function types in C++.
1469 (jason 17 Apr 1997) */
1472 comp_target_parms (parms1, parms2)
1473 tree parms1, parms2;
1475 register tree t1 = parms1, t2 = parms2;
1476 int warn_contravariance = 0;
1478 /* In C, an unspecified parmlist matches any specified parmlist
1479 whose argument types don't need default promotions. This is not
1480 true for C++, but let's do it anyway for unfixed headers. */
1482 if (t1 == 0 && t2 != 0)
1484 cp_pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1486 return self_promoting_args_p (t2);
1489 return self_promoting_args_p (t1);
1491 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1495 /* If one parmlist is shorter than the other,
1496 they fail to match, unless STRICT is <= 0. */
1497 if (t1 == 0 || t2 == 0)
1499 p1 = TREE_VALUE (t1);
1500 p2 = TREE_VALUE (t2);
1501 if (same_type_p (p1, p2))
1507 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1508 || (TREE_CODE (p1) == REFERENCE_TYPE
1509 && TREE_CODE (p2) == REFERENCE_TYPE))
1511 /* The following is wrong for contravariance,
1512 but many programs depend on it. */
1513 if (TREE_TYPE (p1) == void_type_node)
1515 if (TREE_TYPE (p2) == void_type_node)
1517 warn_contravariance = 1;
1520 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1521 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1525 /* Note backwards order due to contravariance. */
1526 if (comp_target_types (p2, p1, 1) <= 0)
1528 if (comp_target_types (p1, p2, 1) > 0)
1530 warn_contravariance = 1;
1536 return warn_contravariance ? -1 : 1;
1539 /* Compute the value of the `sizeof' operator. */
1545 enum tree_code code = TREE_CODE (type);
1548 if (processing_template_decl)
1549 return build_min (SIZEOF_EXPR, sizetype, type);
1551 if (code == FUNCTION_TYPE)
1553 if (pedantic || warn_pointer_arith)
1554 pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
1555 size = size_one_node;
1557 else if (code == METHOD_TYPE)
1559 if (pedantic || warn_pointer_arith)
1560 pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
1561 size = size_one_node;
1563 else if (code == VOID_TYPE)
1565 if (pedantic || warn_pointer_arith)
1566 pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
1567 size = size_one_node;
1569 else if (code == ERROR_MARK)
1570 size = size_one_node;
1573 /* ARM $5.3.2: ``When applied to a reference, the result is the
1574 size of the referenced object.'' */
1575 if (code == REFERENCE_TYPE)
1576 type = TREE_TYPE (type);
1578 if (code == OFFSET_TYPE)
1580 cp_error ("`sizeof' applied to non-static member");
1581 size = size_zero_node;
1583 else if (!COMPLETE_TYPE_P (complete_type (type)))
1585 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1586 size = size_zero_node;
1589 /* Convert in case a char is more than one unit. */
1590 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1591 size_int (TYPE_PRECISION (char_type_node)
1595 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1596 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1597 never happen. However, this node should really have type
1598 `size_t', which is just a typedef for an ordinary integer type. */
1599 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1600 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1610 if (processing_template_decl)
1611 return build_min (SIZEOF_EXPR, sizetype, e);
1613 if (TREE_CODE (e) == COMPONENT_REF
1614 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1615 error ("sizeof applied to a bit-field");
1616 if (is_overloaded_fn (e))
1618 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1619 return c_sizeof (char_type_node);
1621 else if (type_unknown_p (e))
1623 incomplete_type_error (e, TREE_TYPE (e));
1624 return c_sizeof (char_type_node);
1626 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1627 member unless you're in a non-static member of X. So hand off to
1628 resolve_offset_ref. [expr.prim] */
1629 else if (TREE_CODE (e) == OFFSET_REF)
1630 e = resolve_offset_ref (e);
1632 if (e == error_mark_node)
1635 return c_sizeof (TREE_TYPE (e));
1639 c_sizeof_nowarn (type)
1642 enum tree_code code = TREE_CODE (type);
1645 if (code == FUNCTION_TYPE
1646 || code == METHOD_TYPE
1647 || code == VOID_TYPE
1648 || code == ERROR_MARK)
1649 size = size_one_node;
1652 if (code == REFERENCE_TYPE)
1653 type = TREE_TYPE (type);
1655 if (!COMPLETE_TYPE_P (type))
1656 size = size_zero_node;
1658 /* Convert in case a char is more than one unit. */
1659 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1660 size_int (TYPE_PRECISION (char_type_node)
1664 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1665 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1666 never happen. However, this node should really have type
1667 `size_t', which is just a typedef for an ordinary integer type. */
1668 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1669 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1674 /* Perform the array-to-pointer and function-to-pointer conversions
1677 In addition, references are converted to lvalues and manifest
1678 constants are replaced by their values. */
1681 decay_conversion (exp)
1685 register enum tree_code code;
1687 if (TREE_CODE (exp) == OFFSET_REF)
1688 exp = resolve_offset_ref (exp);
1690 type = TREE_TYPE (exp);
1691 code = TREE_CODE (type);
1693 if (code == REFERENCE_TYPE)
1695 exp = convert_from_reference (exp);
1696 type = TREE_TYPE (exp);
1697 code = TREE_CODE (type);
1700 if (type == error_mark_node)
1701 return error_mark_node;
1703 if (type_unknown_p (exp))
1705 incomplete_type_error (exp, TREE_TYPE (exp));
1706 return error_mark_node;
1709 /* Constants can be used directly unless they're not loadable. */
1710 if (TREE_CODE (exp) == CONST_DECL)
1711 exp = DECL_INITIAL (exp);
1712 /* Replace a nonvolatile const static variable with its value. We
1713 don't do this for arrays, though; we want the address of the
1714 first element of the array, not the address of the first element
1715 of its initializing constant. */
1716 else if (code != ARRAY_TYPE)
1718 exp = decl_constant_value (exp);
1719 type = TREE_TYPE (exp);
1722 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1723 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1725 if (code == VOID_TYPE)
1727 error ("void value not ignored as it ought to be");
1728 return error_mark_node;
1730 if (code == METHOD_TYPE)
1731 my_friendly_abort (990506);
1732 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1733 return build_unary_op (ADDR_EXPR, exp, 0);
1734 if (code == ARRAY_TYPE)
1739 if (TREE_CODE (exp) == INDIRECT_REF)
1741 /* Stripping away the INDIRECT_REF is not the right
1742 thing to do for references... */
1743 tree inner = TREE_OPERAND (exp, 0);
1744 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1746 inner = build1 (CONVERT_EXPR,
1747 build_pointer_type (TREE_TYPE
1748 (TREE_TYPE (inner))),
1750 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1752 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1755 if (TREE_CODE (exp) == COMPOUND_EXPR)
1757 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1758 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1759 TREE_OPERAND (exp, 0), op1);
1763 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1765 error ("invalid use of non-lvalue array");
1766 return error_mark_node;
1769 ptrtype = build_pointer_type (TREE_TYPE (type));
1771 if (TREE_CODE (exp) == VAR_DECL)
1773 /* ??? This is not really quite correct
1774 in that the type of the operand of ADDR_EXPR
1775 is not the target type of the type of the ADDR_EXPR itself.
1776 Question is, can this lossage be avoided? */
1777 adr = build1 (ADDR_EXPR, ptrtype, exp);
1778 if (mark_addressable (exp) == 0)
1779 return error_mark_node;
1780 TREE_CONSTANT (adr) = staticp (exp);
1781 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1784 /* This way is better for a COMPONENT_REF since it can
1785 simplify the offset for a component. */
1786 adr = build_unary_op (ADDR_EXPR, exp, 1);
1787 return cp_convert (ptrtype, adr);
1790 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1791 rvalues always have cv-unqualified types. */
1792 if (! CLASS_TYPE_P (type))
1793 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1799 default_conversion (exp)
1803 enum tree_code code;
1805 exp = decay_conversion (exp);
1807 type = TREE_TYPE (exp);
1808 code = TREE_CODE (type);
1810 if (INTEGRAL_CODE_P (code))
1812 tree t = type_promotes_to (type);
1814 return cp_convert (t, exp);
1820 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1824 inline_conversion (exp)
1827 if (TREE_CODE (exp) == FUNCTION_DECL)
1828 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1833 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1834 decay_conversion to one. */
1837 string_conv_p (totype, exp, warn)
1843 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1846 t = TREE_TYPE (totype);
1847 if (!same_type_p (t, char_type_node)
1848 && !same_type_p (t, wchar_type_node))
1851 if (TREE_CODE (exp) == STRING_CST)
1853 /* Make sure that we don't try to convert between char and wchar_t. */
1854 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1859 /* Is this a string constant which has decayed to 'const char *'? */
1860 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1861 if (!same_type_p (TREE_TYPE (exp), t))
1864 if (TREE_CODE (exp) != ADDR_EXPR
1865 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1869 /* This warning is not very useful, as it complains about printf. */
1870 if (warn && warn_write_strings)
1871 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1877 build_object_ref (datum, basetype, field)
1878 tree datum, basetype, field;
1881 if (datum == error_mark_node)
1882 return error_mark_node;
1884 dtype = TREE_TYPE (datum);
1885 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1886 dtype = TREE_TYPE (dtype);
1887 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1889 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1890 basetype, field, dtype);
1891 return error_mark_node;
1893 else if (is_aggr_type (basetype, 1))
1895 tree binfo = binfo_or_else (basetype, dtype);
1897 return build_x_component_ref (build_scoped_ref (datum, basetype),
1900 return error_mark_node;
1903 /* Like `build_component_ref, but uses an already found field, and converts
1904 from a reference. Must compute access for current_class_ref.
1908 build_component_ref_1 (datum, field, protect)
1912 return convert_from_reference
1913 (build_component_ref (datum, field, NULL_TREE, protect));
1916 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1917 can, for example, use as an lvalue. This code used to be in
1918 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1919 expressions, where we're dealing with aggregates. But now it's again only
1920 called from unary_complex_lvalue. The case (in particular) that led to
1921 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1925 rationalize_conditional_expr (code, t)
1926 enum tree_code code;
1929 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1930 the first operand is always the one to be used if both operands
1931 are equal, so we know what conditional expression this used to be. */
1932 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1935 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1936 ? LE_EXPR : GE_EXPR),
1937 TREE_OPERAND (t, 0),
1938 TREE_OPERAND (t, 1)),
1939 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1940 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1944 build_conditional_expr (TREE_OPERAND (t, 0),
1945 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1946 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1949 /* Given the TYPE of an anonymous union field inside T, return the
1950 FIELD_DECL for the field. If not found return NULL_TREE. Because
1951 anonymous unions can nest, we must also search all anonymous unions
1952 that are directly reachable. */
1955 lookup_anon_field (t, type)
1960 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1962 if (TREE_STATIC (field))
1964 if (TREE_CODE (field) != FIELD_DECL)
1967 /* If we find it directly, return the field. */
1968 if (DECL_NAME (field) == NULL_TREE
1969 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1974 /* Otherwise, it could be nested, search harder. */
1975 if (DECL_NAME (field) == NULL_TREE
1976 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1978 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1986 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1987 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1988 that we are interested in, or it can be a FIELD_DECL. */
1991 build_component_ref (datum, component, basetype_path, protect)
1992 tree datum, component, basetype_path;
1995 register tree basetype;
1996 register enum tree_code code;
1997 register tree field = NULL;
2002 if (processing_template_decl)
2003 return build_min_nt (COMPONENT_REF, datum, component);
2005 if (datum == error_mark_node
2006 || TREE_TYPE (datum) == error_mark_node)
2007 return error_mark_node;
2009 /* BASETYPE holds the type of the class containing the COMPONENT. */
2010 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2012 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2014 switch (TREE_CODE (datum))
2018 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2019 basetype_path, protect);
2020 return build (COMPOUND_EXPR, TREE_TYPE (value),
2021 TREE_OPERAND (datum, 0), value);
2024 return build_conditional_expr
2025 (TREE_OPERAND (datum, 0),
2026 build_component_ref (TREE_OPERAND (datum, 1), component,
2027 basetype_path, protect),
2028 build_component_ref (TREE_OPERAND (datum, 2), component,
2029 basetype_path, protect));
2032 cp_error ("invalid use of %D", datum);
2033 datum = error_mark_node;
2040 code = TREE_CODE (basetype);
2042 if (code == REFERENCE_TYPE)
2044 datum = convert_from_reference (datum);
2045 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2046 code = TREE_CODE (basetype);
2048 if (TREE_CODE (datum) == OFFSET_REF)
2050 datum = resolve_offset_ref (datum);
2051 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2052 code = TREE_CODE (basetype);
2055 /* First, see if there is a field or component with name COMPONENT. */
2056 if (TREE_CODE (component) == TREE_LIST)
2058 /* I could not trigger this code. MvL */
2059 my_friendly_abort (980326);
2061 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2062 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2064 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2067 if (! IS_AGGR_TYPE_CODE (code))
2069 if (code != ERROR_MARK)
2070 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2071 component, datum, basetype);
2072 return error_mark_node;
2075 if (!complete_type_or_else (basetype, datum))
2076 return error_mark_node;
2078 if (TREE_CODE (component) == BIT_NOT_EXPR)
2080 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2082 cp_error ("destructor specifier `%T::~%T' must have matching names",
2083 basetype, TREE_OPERAND (component, 0));
2084 return error_mark_node;
2086 if (! TYPE_HAS_DESTRUCTOR (basetype))
2088 cp_error ("type `%T' has no destructor", basetype);
2089 return error_mark_node;
2091 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2094 /* Look up component name in the structure type definition. */
2095 if (TYPE_VFIELD (basetype)
2096 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2097 /* Special-case this because if we use normal lookups in an ambiguous
2098 hierarchy, the compiler will abort (because vptr lookups are
2099 not supposed to be ambiguous. */
2100 field = TYPE_VFIELD (basetype);
2101 else if (TREE_CODE (component) == FIELD_DECL)
2103 else if (TREE_CODE (component) == TYPE_DECL)
2105 cp_error ("invalid use of type decl `%#D' as expression", component);
2106 return error_mark_node;
2108 else if (TREE_CODE (component) == TEMPLATE_DECL)
2110 cp_error ("invalid use of template `%#D' as expression", component);
2111 return error_mark_node;
2115 tree name = component;
2116 if (TREE_CODE (component) == VAR_DECL)
2117 name = DECL_NAME (component);
2118 if (TREE_CODE (component) == NAMESPACE_DECL)
2119 /* Source is in error, but produce a sensible diagnostic. */
2120 name = DECL_NAME (component);
2121 if (basetype_path == NULL_TREE)
2122 basetype_path = TYPE_BINFO (basetype);
2123 field = lookup_field (basetype_path, name,
2124 protect && !VFIELD_NAME_P (name), 0);
2125 if (field == error_mark_node)
2126 return error_mark_node;
2128 if (field == NULL_TREE)
2130 /* Not found as a data field, look for it as a method. If found,
2131 then if this is the only possible one, return it, else
2132 report ambiguity error. */
2133 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2134 if (fndecls == error_mark_node)
2135 return error_mark_node;
2138 /* If the function is unique and static, we can resolve it
2139 now. Otherwise, we have to wait and see what context it is
2140 used in; a component_ref involving a non-static member
2141 function can only be used in a call (expr.ref). */
2143 if (TREE_CHAIN (fndecls) == NULL_TREE
2144 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2146 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2148 tree fndecl = TREE_VALUE (fndecls);
2149 enforce_access (basetype_path, fndecl);
2155 /* A unique non-static member function. Other parts
2156 of the compiler expect something with
2157 unknown_type_node to be really overloaded, so
2159 TREE_VALUE (fndecls)
2160 = ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2164 ref = build (COMPONENT_REF, unknown_type_node,
2165 datum, TREE_VALUE (fndecls));
2169 cp_error ("`%#T' has no member named `%D'", basetype, name);
2170 return error_mark_node;
2172 else if (TREE_TYPE (field) == error_mark_node)
2173 return error_mark_node;
2175 if (TREE_CODE (field) != FIELD_DECL)
2177 if (TREE_CODE (field) == TYPE_DECL)
2178 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2179 else if (DECL_RTL (field) != 0)
2182 TREE_USED (field) = 1;
2184 /* Do evaluate the object when accessing a static member. */
2185 if (TREE_SIDE_EFFECTS (datum))
2186 field = build (COMPOUND_EXPR, TREE_TYPE (field), datum, field);
2192 /* See if we have to do any conversions so that we pick up the field from the
2194 if (DECL_FIELD_CONTEXT (field) != basetype)
2196 tree context = DECL_FIELD_CONTEXT (field);
2197 tree base = context;
2198 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2199 && ANON_AGGR_TYPE_P (base))
2200 base = TYPE_CONTEXT (base);
2202 /* Handle base classes here... */
2203 if (base != basetype && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
2205 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2206 if (integer_zerop (addr))
2208 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2209 return error_mark_node;
2211 addr = convert_pointer_to (base, addr);
2212 datum = build_indirect_ref (addr, NULL);
2213 if (datum == error_mark_node)
2214 return error_mark_node;
2218 /* Handle things from anon unions here... */
2219 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2221 tree subfield = lookup_anon_field (basetype, context);
2222 tree subdatum = build_component_ref (datum, subfield,
2223 basetype_path, protect);
2224 return build_component_ref (subdatum, field, basetype_path, protect);
2228 /* Compute the type of the field, as described in [expr.ref]. */
2229 type_quals = TYPE_UNQUALIFIED;
2230 field_type = TREE_TYPE (field);
2231 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2232 /* The standard says that the type of the result should be the
2233 type referred to by the reference. But for now, at least, we
2234 do the conversion from reference type later. */
2238 type_quals = (CP_TYPE_QUALS (field_type)
2239 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2241 /* A field is const (volatile) if the enclosing object, or the
2242 field itself, is const (volatile). But, a mutable field is
2243 not const, even within a const object. */
2244 if (DECL_MUTABLE_P (field))
2245 type_quals &= ~TYPE_QUAL_CONST;
2246 field_type = cp_build_qualified_type (field_type, type_quals);
2249 ref = fold (build (COMPONENT_REF, field_type, datum, field));
2251 /* Mark the expression const or volatile, as appropriate. Even
2252 though we've dealt with the type above, we still have to mark the
2253 expression itself. */
2254 if (type_quals & TYPE_QUAL_CONST)
2255 TREE_READONLY (ref) = 1;
2256 else if (type_quals & TYPE_QUAL_VOLATILE)
2257 TREE_THIS_VOLATILE (ref) = 1;
2262 /* Variant of build_component_ref for use in expressions, which should
2263 never have REFERENCE_TYPE. */
2266 build_x_component_ref (datum, component, basetype_path, protect)
2267 tree datum, component, basetype_path;
2270 tree t = build_component_ref (datum, component, basetype_path, protect);
2272 if (! processing_template_decl)
2273 t = convert_from_reference (t);
2278 /* Given an expression PTR for a pointer, return an expression
2279 for the value pointed to.
2280 ERRORSTRING is the name of the operator to appear in error messages.
2282 This function may need to overload OPERATOR_FNNAME.
2283 Must also handle REFERENCE_TYPEs for C++. */
2286 build_x_indirect_ref (ptr, errorstring)
2288 const char *errorstring;
2292 if (processing_template_decl)
2293 return build_min_nt (INDIRECT_REF, ptr);
2295 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2299 return build_indirect_ref (ptr, errorstring);
2303 build_indirect_ref (ptr, errorstring)
2305 const char *errorstring;
2307 register tree pointer, type;
2309 if (ptr == error_mark_node)
2310 return error_mark_node;
2312 if (ptr == current_class_ptr)
2313 return current_class_ref;
2315 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2316 ? ptr : default_conversion (ptr));
2317 type = TREE_TYPE (pointer);
2319 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2323 If the type of the expression is "pointer to T," the type
2324 of the result is "T."
2326 We must use the canonical variant because certain parts of
2327 the back end, like fold, do pointer comparisons between
2329 tree t = canonical_type_variant (TREE_TYPE (type));
2331 if (VOID_TYPE_P (t))
2333 /* A pointer to incomplete type (other than cv void) can be
2334 dereferenced [expr.unary.op]/1 */
2335 cp_error ("`%T' is not a pointer-to-object type", type);
2336 return error_mark_node;
2338 else if (TREE_CODE (pointer) == ADDR_EXPR
2340 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2341 /* The POINTER was something like `&x'. We simplify `*&x' to
2343 return TREE_OPERAND (pointer, 0);
2346 tree ref = build1 (INDIRECT_REF, t, pointer);
2348 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2349 so that we get the proper error message if the result is used
2350 to assign to. Also, &* is supposed to be a no-op. */
2351 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2352 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2353 TREE_SIDE_EFFECTS (ref)
2354 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2359 /* `pointer' won't be an error_mark_node if we were given a
2360 pointer to member, so it's cool to check for this here. */
2361 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2362 error ("invalid use of `%s' on pointer to member", errorstring);
2363 else if (pointer != error_mark_node)
2366 error ("invalid type argument of `%s'", errorstring);
2368 error ("invalid type argument");
2370 return error_mark_node;
2373 /* This handles expressions of the form "a[i]", which denotes
2376 This is logically equivalent in C to *(a+i), but we may do it differently.
2377 If A is a variable or a member, we generate a primitive ARRAY_REF.
2378 This avoids forcing the array out of registers, and can work on
2379 arrays that are not lvalues (for example, members of structures returned
2382 If INDEX is of some user-defined type, it must be converted to
2383 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2384 will inherit the type of the array, which will be some pointer type. */
2387 build_array_ref (array, idx)
2392 error ("subscript missing in array reference");
2393 return error_mark_node;
2396 if (TREE_TYPE (array) == error_mark_node
2397 || TREE_TYPE (idx) == error_mark_node)
2398 return error_mark_node;
2400 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2402 switch (TREE_CODE (array))
2406 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2407 return build (COMPOUND_EXPR, TREE_TYPE (value),
2408 TREE_OPERAND (array, 0), value);
2412 return build_conditional_expr
2413 (TREE_OPERAND (array, 0),
2414 build_array_ref (TREE_OPERAND (array, 1), idx),
2415 build_array_ref (TREE_OPERAND (array, 2), idx));
2421 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2422 && TREE_CODE (array) != INDIRECT_REF)
2426 /* Subscripting with type char is likely to lose
2427 on a machine where chars are signed.
2428 So warn on any machine, but optionally.
2429 Don't warn for unsigned char since that type is safe.
2430 Don't warn for signed char because anyone who uses that
2431 must have done so deliberately. */
2432 if (warn_char_subscripts
2433 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2434 warning ("array subscript has type `char'");
2436 /* Apply default promotions *after* noticing character types. */
2437 idx = default_conversion (idx);
2439 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2441 error ("array subscript is not an integer");
2442 return error_mark_node;
2445 /* An array that is indexed by a non-constant
2446 cannot be stored in a register; we must be able to do
2447 address arithmetic on its address.
2448 Likewise an array of elements of variable size. */
2449 if (TREE_CODE (idx) != INTEGER_CST
2450 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2451 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2454 if (mark_addressable (array) == 0)
2455 return error_mark_node;
2458 /* An array that is indexed by a constant value which is not within
2459 the array bounds cannot be stored in a register either; because we
2460 would get a crash in store_bit_field/extract_bit_field when trying
2461 to access a non-existent part of the register. */
2462 if (TREE_CODE (idx) == INTEGER_CST
2463 && TYPE_VALUES (TREE_TYPE (array))
2464 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2466 if (mark_addressable (array) == 0)
2467 return error_mark_node;
2470 if (pedantic && !lvalue_p (array))
2471 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2473 /* Note in C++ it is valid to subscript a `register' array, since
2474 it is valid to take the address of something with that
2475 storage specification. */
2479 while (TREE_CODE (foo) == COMPONENT_REF)
2480 foo = TREE_OPERAND (foo, 0);
2481 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2482 warning ("subscripting array declared `register'");
2485 type = TREE_TYPE (TREE_TYPE (array));
2486 rval = build (ARRAY_REF, type, array, idx);
2487 /* Array ref is const/volatile if the array elements are
2488 or if the array is.. */
2489 TREE_READONLY (rval)
2490 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2491 TREE_SIDE_EFFECTS (rval)
2492 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2493 TREE_THIS_VOLATILE (rval)
2494 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2495 return require_complete_type (fold (rval));
2499 tree ar = default_conversion (array);
2500 tree ind = default_conversion (idx);
2502 /* Put the integer in IND to simplify error checking. */
2503 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2510 if (ar == error_mark_node)
2513 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2515 error ("subscripted value is neither array nor pointer");
2516 return error_mark_node;
2518 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2520 error ("array subscript is not an integer");
2521 return error_mark_node;
2524 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2529 /* Build a function call to function FUNCTION with parameters PARAMS.
2530 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2531 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2532 not include any object pointer that may be required. FUNCTION's
2533 data type may be a function type or a pointer-to-function.
2535 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2536 is the list of possible methods that FUNCTION could conceivably
2537 be. If the list of methods comes from a class, then it will be
2538 a list of lists (where each element is associated with the class
2539 that produced it), otherwise it will be a simple list (for
2540 functions overloaded in global scope).
2542 In the first case, TREE_VALUE (function) is the head of one of those
2543 lists, and TREE_PURPOSE is the name of the function.
2545 In the second case, TREE_PURPOSE (function) is the function's
2548 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2550 When calling a TEMPLATE_DECL, we don't require a complete return
2554 build_x_function_call (function, params, decl)
2555 tree function, params, decl;
2558 tree template_id = NULL_TREE;
2561 if (function == error_mark_node)
2562 return error_mark_node;
2564 if (processing_template_decl)
2565 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2567 /* Save explicit template arguments if found */
2568 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2570 template_id = function;
2571 function = TREE_OPERAND (function, 0);
2574 type = TREE_TYPE (function);
2576 if (TREE_CODE (type) == OFFSET_TYPE
2577 && TREE_TYPE (type) == unknown_type_node
2578 && TREE_CODE (function) == TREE_LIST
2579 && TREE_CHAIN (function) == NULL_TREE)
2581 /* Undo (Foo:bar)()... */
2582 type = TYPE_OFFSET_BASETYPE (type);
2583 function = TREE_VALUE (function);
2584 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2585 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2586 function = TREE_VALUE (function);
2587 if (TREE_CODE (function) == OVERLOAD)
2588 function = OVL_FUNCTION (function);
2589 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2590 function = DECL_NAME (function);
2591 return build_method_call (decl, function, params,
2592 TYPE_BINFO (type), LOOKUP_NORMAL);
2595 if (TREE_CODE (function) == OFFSET_REF
2596 && TREE_CODE (type) != METHOD_TYPE)
2597 function = resolve_offset_ref (function);
2599 if ((TREE_CODE (function) == FUNCTION_DECL
2600 && DECL_STATIC_FUNCTION_P (function))
2601 || (DECL_FUNCTION_TEMPLATE_P (function)
2602 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function))))
2603 return build_member_call (DECL_CONTEXT (function),
2605 ? template_id : DECL_NAME (function),
2608 is_method = ((TREE_CODE (function) == TREE_LIST
2609 && current_class_type != NULL_TREE
2610 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2612 || (TREE_CODE (function) == OVERLOAD
2613 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2614 || TREE_CODE (function) == IDENTIFIER_NODE
2615 || TREE_CODE (type) == METHOD_TYPE
2616 || TYPE_PTRMEMFUNC_P (type));
2618 /* A friend template. Make it look like a toplevel declaration. */
2619 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2620 function = ovl_cons (function, NULL_TREE);
2622 /* Handle methods, friends, and overloaded functions, respectively. */
2625 tree basetype = NULL_TREE;
2627 if (TREE_CODE (function) == OVERLOAD)
2628 function = OVL_CURRENT (function);
2630 if (TREE_CODE (function) == FUNCTION_DECL
2631 || DECL_FUNCTION_TEMPLATE_P (function))
2633 basetype = DECL_CONTEXT (function);
2635 if (DECL_NAME (function))
2636 function = DECL_NAME (function);
2638 function = TYPE_IDENTIFIER (DECL_CONTEXT (function));
2640 else if (TREE_CODE (function) == TREE_LIST)
2642 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2643 == FUNCTION_DECL, 312);
2644 basetype = DECL_CONTEXT (TREE_VALUE (function));
2645 function = TREE_PURPOSE (function);
2647 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2649 if (TREE_CODE (function) == OFFSET_REF)
2651 if (TREE_OPERAND (function, 0))
2652 decl = TREE_OPERAND (function, 0);
2654 /* Call via a pointer to member function. */
2655 if (decl == NULL_TREE)
2657 error ("pointer to member function called, but not in class scope");
2658 return error_mark_node;
2660 /* What other type of POINTER_TYPE could this be? */
2661 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2662 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2663 && TREE_CODE (function) != OFFSET_REF)
2664 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2669 /* this is an abbreviated method call.
2670 must go through here in case it is a virtual function.
2671 @@ Perhaps this could be optimized. */
2673 if (basetype && (! current_class_type
2674 || ! DERIVED_FROM_P (basetype, current_class_type)))
2675 return build_member_call (basetype, function, params);
2677 if (decl == NULL_TREE)
2679 if (current_class_type == NULL_TREE)
2681 cp_error ("object missing in call to method `%D'", function);
2682 return error_mark_node;
2684 /* Yow: call from a static member function. */
2685 decl = build_dummy_object (current_class_type);
2688 /* Put back explicit template arguments, if any. */
2690 function = template_id;
2691 return build_method_call (decl, function, params,
2692 NULL_TREE, LOOKUP_NORMAL);
2694 else if (TREE_CODE (function) == COMPONENT_REF
2695 && type == unknown_type_node)
2697 /* Undo what we did in build_component_ref. */
2698 decl = TREE_OPERAND (function, 0);
2699 function = TREE_OPERAND (function, 1);
2700 function = DECL_NAME (OVL_CURRENT (function));
2704 TREE_OPERAND (template_id, 0) = function;
2705 function = template_id;
2708 return build_method_call (decl, function, params,
2709 NULL_TREE, LOOKUP_NORMAL);
2711 else if (really_overloaded_fn (function))
2713 if (OVL_FUNCTION (function) == NULL_TREE)
2715 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2716 TREE_PURPOSE (function));
2717 return error_mark_node;
2721 /* Put back explicit template arguments, if any. */
2723 function = template_id;
2724 return build_new_function_call (function, params);
2728 /* Remove a potential OVERLOAD around it */
2729 function = OVL_CURRENT (function);
2732 if (TREE_CODE (function) == OFFSET_REF)
2734 /* If the component is a data element (or a virtual function), we play
2735 games here to make things work. */
2738 if (TREE_OPERAND (function, 0))
2739 decl = TREE_OPERAND (function, 0);
2741 decl = current_class_ref;
2743 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2745 /* Sigh. OFFSET_REFs are being used for too many things.
2746 They're being used both for -> and ->*, and we want to resolve
2747 the -> cases here, but leave the ->*. We could use
2748 resolve_offset_ref for those, too, but it would call
2749 get_member_function_from_ptrfunc and decl_addr wouldn't get
2750 updated properly. Nasty. */
2751 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2752 function = resolve_offset_ref (function);
2754 function = TREE_OPERAND (function, 1);
2756 function = get_member_function_from_ptrfunc (&decl_addr, function);
2757 params = tree_cons (NULL_TREE, decl_addr, params);
2758 return build_function_call (function, params);
2761 type = TREE_TYPE (function);
2762 if (type != error_mark_node)
2764 if (TREE_CODE (type) == REFERENCE_TYPE)
2765 type = TREE_TYPE (type);
2767 if (IS_AGGR_TYPE (type))
2768 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2773 tree fntype = TREE_TYPE (function);
2774 tree ctypeptr = NULL_TREE;
2776 /* Explicitly named method? */
2777 if (TREE_CODE (function) == FUNCTION_DECL)
2778 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2779 /* Expression with ptr-to-method type? It could either be a plain
2780 usage, or it might be a case where the ptr-to-method is being
2781 passed in as an argument. */
2782 else if (TYPE_PTRMEMFUNC_P (fntype))
2784 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2785 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2786 ctypeptr = build_pointer_type (rec);
2788 /* Unexpected node type? */
2790 my_friendly_abort (116);
2791 if (decl == NULL_TREE)
2793 if (current_function_decl
2794 && DECL_STATIC_FUNCTION_P (current_function_decl))
2795 error ("invalid call to member function needing `this' in static member function scope");
2797 error ("pointer to member function called, but not in class scope");
2798 return error_mark_node;
2800 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2801 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2803 decl = build_unary_op (ADDR_EXPR, decl, 0);
2804 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2807 decl = build_c_cast (ctypeptr, decl);
2808 params = tree_cons (NULL_TREE, decl, params);
2811 return build_function_call (function, params);
2814 /* Resolve a pointer to member function. INSTANCE is the object
2815 instance to use, if the member points to a virtual member. */
2818 get_member_function_from_ptrfunc (instance_ptrptr, function)
2819 tree *instance_ptrptr;
2822 if (TREE_CODE (function) == OFFSET_REF)
2824 function = TREE_OPERAND (function, 1);
2827 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2829 tree fntype, idx, e1, delta, delta2, e2, e3, vtbl;
2830 tree instance, basetype;
2832 tree instance_ptr = *instance_ptrptr;
2834 if (instance_ptr == error_mark_node
2835 && TREE_CODE (function) == PTRMEM_CST)
2837 /* Extracting the function address from a pmf is only
2838 allowed with -Wno-pmf-conversions. It only works for
2840 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2841 e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
2845 if (TREE_SIDE_EFFECTS (instance_ptr))
2846 instance_ptr = save_expr (instance_ptr);
2848 if (TREE_SIDE_EFFECTS (function))
2849 function = save_expr (function);
2851 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2852 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2854 /* Convert down to the right base, before using the instance. */
2855 instance = convert_pointer_to_real (basetype, instance_ptr);
2856 if (instance == error_mark_node && instance_ptr != error_mark_node)
2859 e3 = PFN_FROM_PTRMEMFUNC (function);
2861 vtbl = convert_pointer_to (ptr_type_node, instance);
2862 delta = cp_convert (ptrdiff_type_node,
2863 build_component_ref (function, delta_identifier,
2866 /* This used to avoid checking for virtual functions if basetype
2867 has no virtual functions, according to an earlier ANSI draft.
2868 With the final ISO C++ rules, such an optimization is
2869 incorrect: A pointer to a derived member can be static_cast
2870 to pointer-to-base-member, as long as the dynamic object
2871 later has the right member. */
2873 /* Promoting idx before saving it improves performance on RISC
2874 targets. Without promoting, the first compare used
2875 load-with-sign-extend, while the second used normal load then
2876 shift to sign-extend. An optimizer flaw, perhaps, but it's
2877 easier to make this change. */
2878 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2880 case ptrmemfunc_vbit_in_pfn:
2881 idx = cp_build_binary_op (TRUNC_DIV_EXPR,
2882 build1 (NOP_EXPR, vtable_index_type, e3),
2883 TYPE_SIZE_UNIT (vtable_entry_type));
2884 e1 = cp_build_binary_op (BIT_AND_EXPR,
2885 build1 (NOP_EXPR, vtable_index_type, e3),
2889 case ptrmemfunc_vbit_in_delta:
2890 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2891 e1 = cp_build_binary_op (BIT_AND_EXPR,
2892 delta, integer_one_node);
2893 delta = cp_build_binary_op (RSHIFT_EXPR,
2894 build1 (NOP_EXPR, vtable_index_type,
2903 /* DELTA2 is the amount by which to adjust the `this' pointer
2904 to find the vtbl. */
2908 build_pointer_type (build_pointer_type (vtable_entry_type)),
2909 vtbl, cp_convert (ptrdiff_type_node, delta2));
2910 vtbl = build_indirect_ref (vtbl, NULL);
2911 e2 = build_array_ref (vtbl, idx);
2913 /* When using function descriptors, the address of the
2914 vtable entry is treated as a function pointer. */
2915 if (TARGET_VTABLE_USES_DESCRIPTORS)
2916 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2917 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2919 TREE_TYPE (e2) = TREE_TYPE (e3);
2920 e1 = build_conditional_expr (e1, e2, e3);
2922 /* Make sure this doesn't get evaluated first inside one of the
2923 branches of the COND_EXPR. */
2924 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2925 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2928 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2929 instance_ptr, delta);
2931 if (instance_ptr == error_mark_node
2932 && TREE_CODE (e1) != ADDR_EXPR
2933 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2934 cp_error ("object missing in `%E'", function);
2942 build_function_call_real (function, params, require_complete, flags)
2943 tree function, params;
2944 int require_complete, flags;
2946 register tree fntype, fndecl;
2947 register tree value_type;
2948 register tree coerced_params;
2950 tree name = NULL_TREE, assembler_name = NULL_TREE;
2953 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2954 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2955 if (TREE_CODE (function) == NOP_EXPR
2956 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2957 function = TREE_OPERAND (function, 0);
2959 if (TREE_CODE (function) == FUNCTION_DECL)
2961 name = DECL_NAME (function);
2962 assembler_name = DECL_ASSEMBLER_NAME (function);
2964 GNU_xref_call (current_function_decl,
2965 IDENTIFIER_POINTER (name ? name
2966 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2968 mark_used (function);
2971 /* Convert anything with function type to a pointer-to-function. */
2972 if (pedantic && DECL_MAIN_P (function))
2973 pedwarn ("ISO C++ forbids calling `::main' from within program");
2975 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2976 (because calling an inline function does not mean the function
2977 needs to be separately compiled). */
2979 if (DECL_INLINE (function))
2980 function = inline_conversion (function);
2982 function = build_addr_func (function);
2988 function = build_addr_func (function);
2991 if (function == error_mark_node)
2992 return error_mark_node;
2994 fntype = TREE_TYPE (function);
2996 if (TYPE_PTRMEMFUNC_P (fntype))
2998 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3000 return error_mark_node;
3003 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3004 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3006 if (!((TREE_CODE (fntype) == POINTER_TYPE
3007 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3009 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3011 cp_error ("`%E' cannot be used as a function", function);
3012 return error_mark_node;
3015 /* fntype now gets the type of function pointed to. */
3016 fntype = TREE_TYPE (fntype);
3018 /* Convert the parameters to the types declared in the
3019 function prototype, or apply default promotions. */
3021 if (flags & LOOKUP_COMPLAIN)
3022 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3023 params, fndecl, LOOKUP_NORMAL);
3025 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3028 if (coerced_params == error_mark_node)
3030 if (flags & LOOKUP_SPECULATIVELY)
3033 return error_mark_node;
3036 /* Check for errors in format strings. */
3038 if (warn_format && (name || assembler_name))
3039 check_function_format (NULL, name, assembler_name, coerced_params);
3041 /* Recognize certain built-in functions so we can make tree-codes
3042 other than CALL_EXPR. We do this when it enables fold-const.c
3043 to do something useful. */
3045 if (TREE_CODE (function) == ADDR_EXPR
3046 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3047 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3049 result = expand_tree_builtin (TREE_OPERAND (function, 0),
3050 params, coerced_params);
3055 /* Some built-in function calls will be evaluated at
3056 compile-time in fold (). */
3057 result = fold (build_call (function, coerced_params));
3058 value_type = TREE_TYPE (result);
3060 if (require_complete)
3062 if (TREE_CODE (value_type) == VOID_TYPE)
3064 result = require_complete_type (result);
3066 if (IS_AGGR_TYPE (value_type))
3067 result = build_cplus_new (value_type, result);
3068 return convert_from_reference (result);
3072 build_function_call (function, params)
3073 tree function, params;
3075 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3078 /* Convert the actual parameter expressions in the list VALUES
3079 to the types in the list TYPELIST.
3080 If parmdecls is exhausted, or when an element has NULL as its type,
3081 perform the default conversions.
3083 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3085 This is also where warnings about wrong number of args are generated.
3087 Return a list of expressions for the parameters as converted.
3089 Both VALUES and the returned value are chains of TREE_LIST nodes
3090 with the elements of the list in the TREE_VALUE slots of those nodes.
3092 In C++, unspecified trailing parameters can be filled in with their
3093 default arguments, if such were specified. Do so here. */
3096 convert_arguments (typelist, values, fndecl, flags)
3097 tree typelist, values, fndecl;
3100 register tree typetail, valtail;
3101 register tree result = NULL_TREE;
3102 const char *called_thing = 0;
3105 /* Argument passing is always copy-initialization. */
3106 flags |= LOOKUP_ONLYCONVERTING;
3110 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3112 if (DECL_NAME (fndecl) == NULL_TREE
3113 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3114 called_thing = "constructor";
3116 called_thing = "member function";
3119 called_thing = "function";
3122 for (valtail = values, typetail = typelist;
3124 valtail = TREE_CHAIN (valtail), i++)
3126 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3127 register tree val = TREE_VALUE (valtail);
3129 if (val == error_mark_node)
3130 return error_mark_node;
3132 if (type == void_type_node)
3136 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3138 error ("at this point in file");
3141 error ("too many arguments to function");
3142 /* In case anybody wants to know if this argument
3145 TREE_TYPE (tree_last (result)) = error_mark_node;
3149 if (TREE_CODE (val) == OFFSET_REF)
3150 val = resolve_offset_ref (val);
3152 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3153 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3154 if (TREE_CODE (val) == NOP_EXPR
3155 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3156 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3157 val = TREE_OPERAND (val, 0);
3159 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3161 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3162 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3163 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3164 val = default_conversion (val);
3167 if (val == error_mark_node)
3168 return error_mark_node;
3172 /* Formal parm type is specified by a function prototype. */
3175 if (!COMPLETE_TYPE_P (complete_type (type)))
3177 error ("parameter type of called function is incomplete");
3182 parmval = convert_for_initialization
3183 (NULL_TREE, type, val, flags,
3184 "argument passing", fndecl, i);
3185 if (PROMOTE_PROTOTYPES
3186 && INTEGRAL_TYPE_P (type)
3187 && (TYPE_PRECISION (type)
3188 < TYPE_PRECISION (integer_type_node)))
3189 parmval = default_conversion (parmval);
3192 if (parmval == error_mark_node)
3193 return error_mark_node;
3195 result = tree_cons (NULL_TREE, parmval, result);
3199 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3200 val = convert_from_reference (val);
3202 if (fndecl && DECL_BUILT_IN (fndecl)
3203 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3204 /* Don't do ellipsis conversion for __built_in_constant_p
3205 as this will result in spurious warnings for non-POD
3207 val = require_complete_type (val);
3209 val = convert_arg_to_ellipsis (val);
3211 result = tree_cons (NULL_TREE, val, result);
3215 typetail = TREE_CHAIN (typetail);
3218 if (typetail != 0 && typetail != void_list_node)
3220 /* See if there are default arguments that can be used */
3221 if (TREE_PURPOSE (typetail))
3223 for (; typetail != void_list_node; ++i)
3226 = convert_default_arg (TREE_VALUE (typetail),
3227 TREE_PURPOSE (typetail),
3230 if (parmval == error_mark_node)
3231 return error_mark_node;
3233 result = tree_cons (0, parmval, result);
3234 typetail = TREE_CHAIN (typetail);
3235 /* ends with `...'. */
3236 if (typetail == NULL_TREE)
3244 cp_error_at ("too few arguments to %s `%+#D'",
3245 called_thing, fndecl);
3246 error ("at this point in file");
3249 error ("too few arguments to function");
3250 return error_mark_list;
3254 return nreverse (result);
3257 /* Build a binary-operation expression, after performing default
3258 conversions on the operands. CODE is the kind of expression to build. */
3261 build_x_binary_op (code, arg1, arg2)
3262 enum tree_code code;
3265 if (processing_template_decl)
3266 return build_min_nt (code, arg1, arg2);
3268 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3271 /* Build a binary-operation expression without default conversions.
3272 CODE is the kind of expression to build.
3273 This function differs from `build' in several ways:
3274 the data type of the result is computed and recorded in it,
3275 warnings are generated if arg data types are invalid,
3276 special handling for addition and subtraction of pointers is known,
3277 and some optimization is done (operations on narrow ints
3278 are done in the narrower type when that gives the same result).
3279 Constant folding is also done before the result is returned.
3281 Note that the operands will never have enumeral types
3282 because either they have just had the default conversions performed
3283 or they have both just been converted to some other type in which
3284 the arithmetic is to be done.
3286 C++: must do special pointer arithmetic when implementing
3287 multiple inheritance, and deal with pointer to member functions. */
3290 build_binary_op (code, orig_op0, orig_op1, convert_p)
3291 enum tree_code code;
3292 tree orig_op0, orig_op1;
3293 int convert_p ATTRIBUTE_UNUSED;
3296 register enum tree_code code0, code1;
3299 /* Expression code to give to the expression when it is built.
3300 Normally this is CODE, which is what the caller asked for,
3301 but in some special cases we change it. */
3302 register enum tree_code resultcode = code;
3304 /* Data type in which the computation is to be performed.
3305 In the simplest cases this is the common type of the arguments. */
3306 register tree result_type = NULL;
3308 /* Nonzero means operands have already been type-converted
3309 in whatever way is necessary.
3310 Zero means they need to be converted to RESULT_TYPE. */
3313 /* Nonzero means create the expression with this type, rather than
3315 tree build_type = 0;
3317 /* Nonzero means after finally constructing the expression
3318 convert it to this type. */
3319 tree final_type = 0;
3321 /* Nonzero if this is an operation like MIN or MAX which can
3322 safely be computed in short if both args are promoted shorts.
3323 Also implies COMMON.
3324 -1 indicates a bitwise operation; this makes a difference
3325 in the exact conditions for when it is safe to do the operation
3326 in a narrower mode. */
3329 /* Nonzero if this is a comparison operation;
3330 if both args are promoted shorts, compare the original shorts.
3331 Also implies COMMON. */
3332 int short_compare = 0;
3334 /* Nonzero if this is a right-shift operation, which can be computed on the
3335 original short and then promoted if the operand is a promoted short. */
3336 int short_shift = 0;
3338 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3341 /* Apply default conversions. */
3345 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3346 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3347 || code == TRUTH_XOR_EXPR)
3349 if (!really_overloaded_fn (op0))
3350 op0 = decay_conversion (op0);
3351 if (!really_overloaded_fn (op1))
3352 op1 = decay_conversion (op1);
3356 if (!really_overloaded_fn (op0))
3357 op0 = default_conversion (op0);
3358 if (!really_overloaded_fn (op1))
3359 op1 = default_conversion (op1);
3362 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3363 STRIP_TYPE_NOPS (op0);
3364 STRIP_TYPE_NOPS (op1);
3366 /* DTRT if one side is an overloaded function, but complain about it. */
3367 if (type_unknown_p (op0))
3369 tree t = instantiate_type (TREE_TYPE (op1), op0, itf_none);
3370 if (t != error_mark_node)
3372 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3377 if (type_unknown_p (op1))
3379 tree t = instantiate_type (TREE_TYPE (op0), op1, itf_none);
3380 if (t != error_mark_node)
3382 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3388 type0 = TREE_TYPE (op0);
3389 type1 = TREE_TYPE (op1);
3391 /* The expression codes of the data types of the arguments tell us
3392 whether the arguments are integers, floating, pointers, etc. */
3393 code0 = TREE_CODE (type0);
3394 code1 = TREE_CODE (type1);
3396 /* If an error was already reported for one of the arguments,
3397 avoid reporting another error. */
3399 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3400 return error_mark_node;
3405 /* Handle the pointer + int case. */
3406 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3407 return pointer_int_sum (PLUS_EXPR, op0, op1);
3408 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3409 return pointer_int_sum (PLUS_EXPR, op1, op0);
3415 /* Subtraction of two similar pointers.
3416 We must subtract them as integers, then divide by object size. */
3417 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3418 && comp_target_types (type0, type1, 1))
3419 return pointer_diff (op0, op1, common_type (type0, type1));
3420 /* Handle pointer minus int. Just like pointer plus int. */
3421 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3422 return pointer_int_sum (MINUS_EXPR, op0, op1);
3431 case TRUNC_DIV_EXPR:
3433 case FLOOR_DIV_EXPR:
3434 case ROUND_DIV_EXPR:
3435 case EXACT_DIV_EXPR:
3436 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3437 || code0 == COMPLEX_TYPE)
3438 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3439 || code1 == COMPLEX_TYPE))
3441 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3442 cp_warning ("division by zero in `%E / 0'", op0);
3443 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3444 cp_warning ("division by zero in `%E / 0.'", op0);
3446 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3447 resultcode = RDIV_EXPR;
3449 /* When dividing two signed integers, we have to promote to int.
3450 unless we divide by a constant != -1. Note that default
3451 conversion will have been performed on the operands at this
3452 point, so we have to dig out the original type to find out if
3454 shorten = ((TREE_CODE (op0) == NOP_EXPR
3455 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3456 || (TREE_CODE (op1) == INTEGER_CST
3457 && ! integer_all_onesp (op1)));
3464 case BIT_ANDTC_EXPR:
3467 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3469 /* If one operand is a constant, and the other is a short type
3470 that has been converted to an int,
3471 really do the work in the short type and then convert the
3472 result to int. If we are lucky, the constant will be 0 or 1
3473 in the short type, making the entire operation go away. */
3474 if (TREE_CODE (op0) == INTEGER_CST
3475 && TREE_CODE (op1) == NOP_EXPR
3476 && (TYPE_PRECISION (type1)
3477 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3478 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3480 final_type = result_type;
3481 op1 = TREE_OPERAND (op1, 0);
3482 result_type = TREE_TYPE (op1);
3484 if (TREE_CODE (op1) == INTEGER_CST
3485 && TREE_CODE (op0) == NOP_EXPR
3486 && (TYPE_PRECISION (type0)
3487 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3488 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3490 final_type = result_type;
3491 op0 = TREE_OPERAND (op0, 0);
3492 result_type = TREE_TYPE (op0);
3496 case TRUNC_MOD_EXPR:
3497 case FLOOR_MOD_EXPR:
3498 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3499 cp_warning ("division by zero in `%E %% 0'", op0);
3500 else if (code1 == REAL_TYPE && real_zerop (op1))
3501 cp_warning ("division by zero in `%E %% 0.'", op0);
3503 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3505 /* Although it would be tempting to shorten always here, that loses
3506 on some targets, since the modulo instruction is undefined if the
3507 quotient can't be represented in the computation mode. We shorten
3508 only if unsigned or if dividing by something we know != -1. */
3509 shorten = ((TREE_CODE (op0) == NOP_EXPR
3510 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3511 || (TREE_CODE (op1) == INTEGER_CST
3512 && ! integer_all_onesp (op1)));
3517 case TRUTH_ANDIF_EXPR:
3518 case TRUTH_ORIF_EXPR:
3519 case TRUTH_AND_EXPR:
3521 result_type = boolean_type_node;
3524 /* Shift operations: result has same type as first operand;
3525 always convert second operand to int.
3526 Also set SHORT_SHIFT if shifting rightward. */
3529 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3531 result_type = type0;
3532 if (TREE_CODE (op1) == INTEGER_CST)
3534 if (tree_int_cst_lt (op1, integer_zero_node))
3535 warning ("right shift count is negative");
3538 if (! integer_zerop (op1))
3540 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3541 warning ("right shift count >= width of type");
3544 /* Convert the shift-count to an integer, regardless of
3545 size of value being shifted. */
3546 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3547 op1 = cp_convert (integer_type_node, op1);
3548 /* Avoid converting op1 to result_type later. */
3554 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3556 result_type = type0;
3557 if (TREE_CODE (op1) == INTEGER_CST)
3559 if (tree_int_cst_lt (op1, integer_zero_node))
3560 warning ("left shift count is negative");
3561 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3562 warning ("left shift count >= width of type");
3564 /* Convert the shift-count to an integer, regardless of
3565 size of value being shifted. */
3566 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3567 op1 = cp_convert (integer_type_node, op1);
3568 /* Avoid converting op1 to result_type later. */
3575 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3577 result_type = type0;
3578 if (TREE_CODE (op1) == INTEGER_CST)
3580 if (tree_int_cst_lt (op1, integer_zero_node))
3581 warning ("%s rotate count is negative",
3582 (code == LROTATE_EXPR) ? "left" : "right");
3583 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3584 warning ("%s rotate count >= width of type",
3585 (code == LROTATE_EXPR) ? "left" : "right");
3587 /* Convert the shift-count to an integer, regardless of
3588 size of value being shifted. */
3589 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3590 op1 = cp_convert (integer_type_node, op1);
3596 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3597 warning ("comparing floating point with == or != is unsafe");
3599 build_type = boolean_type_node;
3600 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3601 || code0 == COMPLEX_TYPE)
3602 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3603 || code1 == COMPLEX_TYPE))
3605 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3606 result_type = composite_pointer_type (type0, type1, op0, op1,
3608 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3609 result_type = type0;
3610 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3611 result_type = type1;
3612 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3614 result_type = type0;
3615 error ("ISO C++ forbids comparison between pointer and integer");
3617 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3619 result_type = type1;
3620 error ("ISO C++ forbids comparison between pointer and integer");
3622 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3624 op0 = build_component_ref (op0, pfn_identifier, NULL_TREE, 0);
3625 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3626 result_type = TREE_TYPE (op0);
3628 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3629 return cp_build_binary_op (code, op1, op0);
3630 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3631 && same_type_p (type0, type1))
3633 /* E will be the final comparison. */
3635 /* E1 and E2 are for scratch. */
3643 if (TREE_SIDE_EFFECTS (op0))
3644 op0 = save_expr (op0);
3645 if (TREE_SIDE_EFFECTS (op1))
3646 op1 = save_expr (op1);
3651 && (!op0.pfn || op0.delta == op1.delta))
3653 The reason for the `!op0.pfn' bit is that a NULL
3654 pointer-to-member is any member with a zero PFN; the
3655 DELTA field is unspecified. */
3656 pfn0 = pfn_from_ptrmemfunc (op0);
3657 pfn1 = pfn_from_ptrmemfunc (op1);
3658 delta0 = build_component_ref (op0, delta_identifier,
3660 delta1 = build_component_ref (op1, delta_identifier,
3662 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3663 e2 = cp_build_binary_op (EQ_EXPR,
3665 cp_convert (TREE_TYPE (pfn0),
3666 integer_zero_node));
3667 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3668 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3669 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3670 if (code == EQ_EXPR)
3672 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3674 else if ((TYPE_PTRMEMFUNC_P (type0)
3675 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3676 || (TYPE_PTRMEMFUNC_P (type1)
3677 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3678 my_friendly_abort (20000221);
3683 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3684 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3686 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3687 result_type = composite_pointer_type (type0, type1, op0, op1,
3695 build_type = boolean_type_node;
3696 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3697 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3699 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3700 result_type = composite_pointer_type (type0, type1, op0, op1,
3702 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3703 && integer_zerop (op1))
3704 result_type = type0;
3705 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3706 && integer_zerop (op0))
3707 result_type = type1;
3708 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3710 result_type = type0;
3711 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3713 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3715 result_type = type1;
3716 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3720 case UNORDERED_EXPR:
3727 build_type = integer_type_node;
3728 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3730 error ("unordered comparison on non-floating point argument");
3731 return error_mark_node;
3740 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3742 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3744 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3746 if (shorten || common || short_compare)
3747 result_type = common_type (type0, type1);
3749 /* For certain operations (which identify themselves by shorten != 0)
3750 if both args were extended from the same smaller type,
3751 do the arithmetic in that type and then extend.
3753 shorten !=0 and !=1 indicates a bitwise operation.
3754 For them, this optimization is safe only if
3755 both args are zero-extended or both are sign-extended.
3756 Otherwise, we might change the result.
3757 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3758 but calculated in (unsigned short) it would be (unsigned short)-1. */
3760 if (shorten && none_complex)
3762 int unsigned0, unsigned1;
3763 tree arg0 = get_narrower (op0, &unsigned0);
3764 tree arg1 = get_narrower (op1, &unsigned1);
3765 /* UNS is 1 if the operation to be done is an unsigned one. */
3766 int uns = TREE_UNSIGNED (result_type);
3769 final_type = result_type;
3771 /* Handle the case that OP0 does not *contain* a conversion
3772 but it *requires* conversion to FINAL_TYPE. */
3774 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3775 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3776 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3777 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3779 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3781 /* For bitwise operations, signedness of nominal type
3782 does not matter. Consider only how operands were extended. */
3786 /* Note that in all three cases below we refrain from optimizing
3787 an unsigned operation on sign-extended args.
3788 That would not be valid. */
3790 /* Both args variable: if both extended in same way
3791 from same width, do it in that width.
3792 Do it unsigned if args were zero-extended. */
3793 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3794 < TYPE_PRECISION (result_type))
3795 && (TYPE_PRECISION (TREE_TYPE (arg1))
3796 == TYPE_PRECISION (TREE_TYPE (arg0)))
3797 && unsigned0 == unsigned1
3798 && (unsigned0 || !uns))
3800 = signed_or_unsigned_type (unsigned0,
3801 common_type (TREE_TYPE (arg0),
3803 else if (TREE_CODE (arg0) == INTEGER_CST
3804 && (unsigned1 || !uns)
3805 && (TYPE_PRECISION (TREE_TYPE (arg1))
3806 < TYPE_PRECISION (result_type))
3807 && (type = signed_or_unsigned_type (unsigned1,
3809 int_fits_type_p (arg0, type)))
3811 else if (TREE_CODE (arg1) == INTEGER_CST
3812 && (unsigned0 || !uns)
3813 && (TYPE_PRECISION (TREE_TYPE (arg0))
3814 < TYPE_PRECISION (result_type))
3815 && (type = signed_or_unsigned_type (unsigned0,
3817 int_fits_type_p (arg1, type)))
3821 /* Shifts can be shortened if shifting right. */
3826 tree arg0 = get_narrower (op0, &unsigned_arg);
3828 final_type = result_type;
3830 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3831 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3833 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3834 /* We can shorten only if the shift count is less than the
3835 number of bits in the smaller type size. */
3836 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3837 /* If arg is sign-extended and then unsigned-shifted,
3838 we can simulate this with a signed shift in arg's type
3839 only if the extended result is at least twice as wide
3840 as the arg. Otherwise, the shift could use up all the
3841 ones made by sign-extension and bring in zeros.
3842 We can't optimize that case at all, but in most machines
3843 it never happens because available widths are 2**N. */
3844 && (!TREE_UNSIGNED (final_type)
3846 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3847 <= TYPE_PRECISION (result_type))))
3849 /* Do an unsigned shift if the operand was zero-extended. */
3851 = signed_or_unsigned_type (unsigned_arg,
3853 /* Convert value-to-be-shifted to that type. */
3854 if (TREE_TYPE (op0) != result_type)
3855 op0 = cp_convert (result_type, op0);
3860 /* Comparison operations are shortened too but differently.
3861 They identify themselves by setting short_compare = 1. */
3865 /* Don't write &op0, etc., because that would prevent op0
3866 from being kept in a register.
3867 Instead, make copies of the our local variables and
3868 pass the copies by reference, then copy them back afterward. */
3869 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3870 enum tree_code xresultcode = resultcode;
3872 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3874 return cp_convert (boolean_type_node, val);
3875 op0 = xop0, op1 = xop1;
3877 resultcode = xresultcode;
3880 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3881 && warn_sign_compare)
3883 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3884 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3886 int unsignedp0, unsignedp1;
3887 tree primop0 = get_narrower (op0, &unsignedp0);
3888 tree primop1 = get_narrower (op1, &unsignedp1);
3890 /* Check for comparison of different enum types. */
3891 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3892 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3893 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3894 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3896 cp_warning ("comparison between types `%#T' and `%#T'",
3897 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3900 /* Give warnings for comparisons between signed and unsigned
3901 quantities that may fail. */
3902 /* Do the checking based on the original operand trees, so that
3903 casts will be considered, but default promotions won't be. */
3905 /* Do not warn if the comparison is being done in a signed type,
3906 since the signed type will only be chosen if it can represent
3907 all the values of the unsigned type. */
3908 if (! TREE_UNSIGNED (result_type))
3910 /* Do not warn if both operands are unsigned. */
3911 else if (op0_signed == op1_signed)
3913 /* Do not warn if the signed quantity is an unsuffixed
3914 integer literal (or some static constant expression
3915 involving such literals or a conditional expression
3916 involving such literals) and it is non-negative. */
3917 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3918 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3920 /* Do not warn if the comparison is an equality operation,
3921 the unsigned quantity is an integral constant and it does
3922 not use the most significant bit of result_type. */
3923 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3924 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3925 && int_fits_type_p (orig_op1,
3926 signed_type (result_type)))
3927 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3928 && int_fits_type_p (orig_op0,
3929 signed_type (result_type)))))
3932 warning ("comparison between signed and unsigned integer expressions");
3934 /* Warn if two unsigned values are being compared in a size
3935 larger than their original size, and one (and only one) is the
3936 result of a `~' operator. This comparison will always fail.
3938 Also warn if one operand is a constant, and the constant does not
3939 have all bits set that are set in the ~ operand when it is
3942 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3943 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3945 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3946 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3947 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3948 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3950 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3953 HOST_WIDE_INT constant, mask;
3957 if (host_integerp (primop0, 0))
3960 unsignedp = unsignedp1;
3961 constant = tree_low_cst (primop0, 0);
3966 unsignedp = unsignedp0;
3967 constant = tree_low_cst (primop1, 0);
3970 bits = TYPE_PRECISION (TREE_TYPE (primop));
3971 if (bits < TYPE_PRECISION (result_type)
3972 && bits < HOST_BITS_PER_LONG && unsignedp)
3974 mask = (~ (HOST_WIDE_INT) 0) << bits;
3975 if ((mask & constant) != mask)
3976 warning ("comparison of promoted ~unsigned with constant");
3979 else if (unsignedp0 && unsignedp1
3980 && (TYPE_PRECISION (TREE_TYPE (primop0))
3981 < TYPE_PRECISION (result_type))
3982 && (TYPE_PRECISION (TREE_TYPE (primop1))
3983 < TYPE_PRECISION (result_type)))
3984 warning ("comparison of promoted ~unsigned with unsigned");
3989 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3990 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3991 Then the expression will be built.
3992 It will be given type FINAL_TYPE if that is nonzero;
3993 otherwise, it will be given type RESULT_TYPE. */
3997 cp_error ("invalid operands of types `%T' and `%T' to binary `%O'",
3998 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3999 return error_mark_node;
4002 /* Issue warnings about peculiar, but legal, uses of NULL. */
4003 if (/* It's reasonable to use pointer values as operands of &&
4004 and ||, so NULL is no exception. */
4005 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4006 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4007 (orig_op0 == null_node
4008 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4009 /* Or vice versa. */
4010 || (orig_op1 == null_node
4011 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4012 /* Or, both are NULL and the operation was not a comparison. */
4013 || (orig_op0 == null_node && orig_op1 == null_node
4014 && code != EQ_EXPR && code != NE_EXPR)))
4015 /* Some sort of arithmetic operation involving NULL was
4016 performed. Note that pointer-difference and pointer-addition
4017 have already been handled above, and so we don't end up here in
4019 cp_warning ("NULL used in arithmetic");
4023 if (TREE_TYPE (op0) != result_type)
4024 op0 = cp_convert (result_type, op0);
4025 if (TREE_TYPE (op1) != result_type)
4026 op1 = cp_convert (result_type, op1);
4028 if (op0 == error_mark_node || op1 == error_mark_node)
4029 return error_mark_node;
4032 if (build_type == NULL_TREE)
4033 build_type = result_type;
4036 register tree result = build (resultcode, build_type, op0, op1);
4037 register tree folded;
4039 folded = fold (result);
4040 if (folded == result)
4041 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4042 if (final_type != 0)
4043 return cp_convert (final_type, folded);
4048 /* Return a tree for the sum or difference (RESULTCODE says which)
4049 of pointer PTROP and integer INTOP. */
4052 pointer_int_sum (resultcode, ptrop, intop)
4053 enum tree_code resultcode;
4054 register tree ptrop, intop;
4058 register tree result;
4059 register tree folded = fold (intop);
4061 /* The result is a pointer of the same type that is being added. */
4063 register tree result_type = TREE_TYPE (ptrop);
4065 if (!complete_type_or_else (result_type, ptrop))
4066 return error_mark_node;
4068 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4070 if (pedantic || warn_pointer_arith)
4071 pedwarn ("ISO C++ forbids using pointer of type `void *' in pointer arithmetic");
4072 size_exp = integer_one_node;
4074 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4076 if (pedantic || warn_pointer_arith)
4077 pedwarn ("ISO C++ forbids using a pointer-to-function in pointer arithmetic");
4078 size_exp = integer_one_node;
4080 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4082 if (pedantic || warn_pointer_arith)
4083 pedwarn ("ISO C++ forbids using a pointer to member function in pointer arithmetic");
4084 size_exp = integer_one_node;
4086 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4088 if (pedantic || warn_pointer_arith)
4089 pedwarn ("ISO C++ forbids using pointer to a member in pointer arithmetic");
4090 size_exp = integer_one_node;
4093 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4095 /* Needed to make OOPS V2R3 work. */
4097 if (integer_zerop (intop))
4100 /* If what we are about to multiply by the size of the elements
4101 contains a constant term, apply distributive law
4102 and multiply that constant term separately.
4103 This helps produce common subexpressions. */
4105 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4106 && ! TREE_CONSTANT (intop)
4107 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4108 && TREE_CONSTANT (size_exp))
4110 enum tree_code subcode = resultcode;
4111 if (TREE_CODE (intop) == MINUS_EXPR)
4112 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4113 ptrop = cp_build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4114 intop = TREE_OPERAND (intop, 0);
4117 /* Convert the integer argument to a type the same size as sizetype
4118 so the multiply won't overflow spuriously. */
4120 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4121 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4123 /* Replace the integer argument with a suitable product by the object size.
4124 Do this multiplication as signed, then convert to the appropriate
4125 pointer type (actually unsigned integral). */
4127 intop = cp_convert (result_type,
4128 cp_build_binary_op (MULT_EXPR, intop,
4129 cp_convert (TREE_TYPE (intop),
4132 /* Create the sum or difference. */
4134 result = build (resultcode, result_type, ptrop, intop);
4136 folded = fold (result);
4137 if (folded == result)
4138 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4142 /* Return a tree for the difference of pointers OP0 and OP1.
4143 The resulting tree has type int. */
4146 pointer_diff (op0, op1, ptrtype)
4147 register tree op0, op1;
4148 register tree ptrtype;
4150 register tree result, folded;
4151 tree restype = ptrdiff_type_node;
4152 tree target_type = TREE_TYPE (ptrtype);
4154 if (!complete_type_or_else (target_type, NULL_TREE))
4155 return error_mark_node;
4157 if (pedantic || warn_pointer_arith)
4159 if (TREE_CODE (target_type) == VOID_TYPE)
4160 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4161 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4162 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4163 if (TREE_CODE (target_type) == METHOD_TYPE)
4164 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4165 if (TREE_CODE (target_type) == OFFSET_TYPE)
4166 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4169 /* First do the subtraction as integers;
4170 then drop through to build the divide operator. */
4172 op0 = cp_build_binary_op (MINUS_EXPR,
4173 cp_convert (restype, op0),
4174 cp_convert (restype, op1));
4176 /* This generates an error if op1 is a pointer to an incomplete type. */
4177 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4178 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4180 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4181 || TREE_CODE (target_type) == FUNCTION_TYPE
4182 || TREE_CODE (target_type) == METHOD_TYPE
4183 || TREE_CODE (target_type) == OFFSET_TYPE)
4185 : size_in_bytes (target_type));
4187 /* Do the division. */
4189 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4191 folded = fold (result);
4192 if (folded == result)
4193 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4197 /* Handle the case of taking the address of a COMPONENT_REF.
4198 Called by `build_unary_op'.
4200 ARG is the COMPONENT_REF whose address we want.
4201 ARGTYPE is the pointer type that this address should have. */
4204 build_component_addr (arg, argtype)
4207 tree field = TREE_OPERAND (arg, 1);
4208 tree basetype = decl_type_context (field);
4209 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4211 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4213 if (DECL_C_BIT_FIELD (field))
4215 cp_error ("attempt to take address of bit-field structure member `%D'",
4217 return error_mark_node;
4220 if (TREE_CODE (field) == FIELD_DECL
4221 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
4223 /* Can't convert directly to ARGTYPE, since that
4224 may have the same pointer type as one of our
4226 rval = build1 (NOP_EXPR, argtype,
4227 convert_pointer_to (basetype, rval));
4228 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4231 /* This conversion is harmless. */
4232 rval = convert_force (argtype, rval, 0);
4234 return fold (build (PLUS_EXPR, argtype, rval,
4235 cp_convert (argtype, byte_position (field))));
4238 /* Construct and perhaps optimize a tree representation
4239 for a unary operation. CODE, a tree_code, specifies the operation
4240 and XARG is the operand. */
4243 build_x_unary_op (code, xarg)
4244 enum tree_code code;
4250 if (processing_template_decl)
4251 return build_min_nt (code, xarg, NULL_TREE);
4253 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4255 if (code == ADDR_EXPR
4256 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4257 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4258 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
4259 || (TREE_CODE (xarg) == OFFSET_REF)))
4260 /* don't look for a function */;
4265 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4266 NULL_TREE, NULL_TREE);
4267 if (rval || code != ADDR_EXPR)
4270 if (code == ADDR_EXPR)
4272 if (TREE_CODE (xarg) == OFFSET_REF)
4274 ptrmem = PTRMEM_OK_P (xarg);
4276 if (!ptrmem && !flag_ms_extensions
4277 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4278 /* A single non-static member, make sure we don't allow a
4279 pointer-to-member. */
4280 xarg = ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE);
4282 else if (TREE_CODE (xarg) == TARGET_EXPR)
4283 warning ("taking address of temporary");
4285 exp = build_unary_op (code, xarg, 0);
4286 if (TREE_CODE (exp) == ADDR_EXPR)
4287 PTRMEM_OK_P (exp) = ptrmem;
4292 /* Like truthvalue_conversion, but handle pointer-to-member constants, where
4293 a null value is represented by an INTEGER_CST of -1. */
4296 cp_truthvalue_conversion (expr)
4299 tree type = TREE_TYPE (expr);
4300 if (TYPE_PTRMEM_P (type))
4301 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4303 return truthvalue_conversion (expr);
4306 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4309 condition_conversion (expr)
4313 if (processing_template_decl)
4315 if (TREE_CODE (expr) == OFFSET_REF)
4316 expr = resolve_offset_ref (expr);
4317 t = perform_implicit_conversion (boolean_type_node, expr);
4318 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4322 /* C++: Must handle pointers to members.
4324 Perhaps type instantiation should be extended to handle conversion
4325 from aggregates to types we don't yet know we want? (Or are those
4326 cases typically errors which should be reported?)
4328 NOCONVERT nonzero suppresses the default promotions
4329 (such as from short to int). */
4332 build_unary_op (code, xarg, noconvert)
4333 enum tree_code code;
4337 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4338 register tree arg = xarg;
4339 register tree argtype = 0;
4340 const char *errstring = NULL;
4343 if (arg == error_mark_node)
4344 return error_mark_node;
4349 /* This is used for unary plus, because a CONVERT_EXPR
4350 is enough to prevent anybody from looking inside for
4351 associativity, but won't generate any code. */
4352 if (!(arg = build_expr_type_conversion
4353 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4354 errstring = "wrong type argument to unary plus";
4358 arg = default_conversion (arg);
4359 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4360 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4365 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4366 errstring = "wrong type argument to unary minus";
4367 else if (!noconvert)
4368 arg = default_conversion (arg);
4372 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4376 arg = default_conversion (arg);
4378 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4380 errstring = "wrong type argument to bit-complement";
4381 else if (!noconvert)
4382 arg = default_conversion (arg);
4386 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4387 errstring = "wrong type argument to abs";
4388 else if (!noconvert)
4389 arg = default_conversion (arg);
4393 /* Conjugating a real value is a no-op, but allow it anyway. */
4394 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4395 errstring = "wrong type argument to conjugation";
4396 else if (!noconvert)
4397 arg = default_conversion (arg);
4400 case TRUTH_NOT_EXPR:
4401 arg = cp_convert (boolean_type_node, arg);
4402 val = invert_truthvalue (arg);
4403 if (arg != error_mark_node)
4405 errstring = "in argument to unary !";
4412 if (TREE_CODE (arg) == COMPLEX_CST)
4413 return TREE_REALPART (arg);
4414 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4415 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4420 if (TREE_CODE (arg) == COMPLEX_CST)
4421 return TREE_IMAGPART (arg);
4422 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4423 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4425 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4427 case PREINCREMENT_EXPR:
4428 case POSTINCREMENT_EXPR:
4429 case PREDECREMENT_EXPR:
4430 case POSTDECREMENT_EXPR:
4431 /* Handle complex lvalues (when permitted)
4432 by reduction to simpler cases. */
4434 val = unary_complex_lvalue (code, arg);
4438 /* Increment or decrement the real part of the value,
4439 and don't change the imaginary part. */
4440 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4444 arg = stabilize_reference (arg);
4445 real = build_unary_op (REALPART_EXPR, arg, 1);
4446 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4447 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4448 build_unary_op (code, real, 1), imag);
4451 /* Report invalid types. */
4453 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4456 if (code == PREINCREMENT_EXPR)
4457 errstring ="no pre-increment operator for type";
4458 else if (code == POSTINCREMENT_EXPR)
4459 errstring ="no post-increment operator for type";
4460 else if (code == PREDECREMENT_EXPR)
4461 errstring ="no pre-decrement operator for type";
4463 errstring ="no post-decrement operator for type";
4467 /* Report something read-only. */
4469 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4470 || TREE_READONLY (arg))
4471 readonly_error (arg, ((code == PREINCREMENT_EXPR
4472 || code == POSTINCREMENT_EXPR)
4473 ? "increment" : "decrement"),
4478 tree result_type = TREE_TYPE (arg);
4480 arg = get_unwidened (arg, 0);
4481 argtype = TREE_TYPE (arg);
4483 /* ARM $5.2.5 last annotation says this should be forbidden. */
4484 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4485 pedwarn ("ISO C++ forbids %sing an enum",
4486 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4487 ? "increment" : "decrement");
4489 /* Compute the increment. */
4491 if (TREE_CODE (argtype) == POINTER_TYPE)
4493 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4494 tree type = complete_type (TREE_TYPE (argtype));
4496 if (!COMPLETE_OR_VOID_TYPE_P (type))
4497 cp_error ("cannot %s a pointer to incomplete type `%T'",
4498 ((code == PREINCREMENT_EXPR
4499 || code == POSTINCREMENT_EXPR)
4500 ? "increment" : "decrement"), TREE_TYPE (argtype));
4501 else if ((pedantic || warn_pointer_arith)
4502 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4503 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4504 cp_pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4505 ((code == PREINCREMENT_EXPR
4506 || code == POSTINCREMENT_EXPR)
4507 ? "increment" : "decrement"), argtype);
4508 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4511 inc = integer_one_node;
4513 inc = cp_convert (argtype, inc);
4515 /* Handle incrementing a cast-expression. */
4517 switch (TREE_CODE (arg))
4522 case FIX_TRUNC_EXPR:
4523 case FIX_FLOOR_EXPR:
4524 case FIX_ROUND_EXPR:
4527 tree incremented, modify, value, compound;
4528 if (! lvalue_p (arg) && pedantic)
4529 pedwarn ("cast to non-reference type used as lvalue");
4530 arg = stabilize_reference (arg);
4531 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4534 value = save_expr (arg);
4535 incremented = build (((code == PREINCREMENT_EXPR
4536 || code == POSTINCREMENT_EXPR)
4537 ? PLUS_EXPR : MINUS_EXPR),
4538 argtype, value, inc);
4540 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4541 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4543 /* Eliminate warning about unused result of + or -. */
4544 TREE_NO_UNUSED_WARNING (compound) = 1;
4552 /* Complain about anything else that is not a true lvalue. */
4553 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4554 || code == POSTINCREMENT_EXPR)
4555 ? "increment" : "decrement")))
4556 return error_mark_node;
4558 /* Forbid using -- on `bool'. */
4559 if (TREE_TYPE (arg) == boolean_type_node)
4561 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4563 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4564 return error_mark_node;
4567 /* This will only work if someone can convince Kenner to accept
4568 my patch to expand_increment. (jason) */
4569 val = build (code, TREE_TYPE (arg), arg, inc);
4571 val = boolean_increment (code, arg);
4575 val = build (code, TREE_TYPE (arg), arg, inc);
4577 TREE_SIDE_EFFECTS (val) = 1;
4578 return cp_convert (result_type, val);
4582 /* Note that this operation never does default_conversion
4583 regardless of NOCONVERT. */
4585 argtype = lvalue_type (arg);
4586 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4590 build_pointer_type (TREE_TYPE (argtype)), arg);
4591 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4594 else if (pedantic && DECL_MAIN_P (arg))
4596 pedwarn ("ISO C++ forbids taking address of function `::main'");
4598 /* Let &* cancel out to simplify resulting code. */
4599 if (TREE_CODE (arg) == INDIRECT_REF)
4601 /* We don't need to have `current_class_ptr' wrapped in a
4602 NON_LVALUE_EXPR node. */
4603 if (arg == current_class_ref)
4604 return current_class_ptr;
4606 arg = TREE_OPERAND (arg, 0);
4607 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4611 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4612 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4614 else if (lvalue_p (arg))
4615 /* Don't let this be an lvalue. */
4616 return non_lvalue (arg);
4620 /* For &x[y], return x+y */
4621 if (TREE_CODE (arg) == ARRAY_REF)
4623 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4624 return error_mark_node;
4625 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4626 TREE_OPERAND (arg, 1));
4629 /* Uninstantiated types are all functions. Taking the
4630 address of a function is a no-op, so just return the
4633 if (TREE_CODE (arg) == IDENTIFIER_NODE
4634 && IDENTIFIER_OPNAME_P (arg))
4636 my_friendly_abort (117);
4637 /* We don't know the type yet, so just work around the problem.
4638 We know that this will resolve to an lvalue. */
4639 return build1 (ADDR_EXPR, unknown_type_node, arg);
4642 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4643 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4645 /* They're trying to take the address of a unique non-static
4646 member function. This is ill-formed (except in MS-land),
4647 but let's try to DTRT.
4648 Note: We only handle unique functions here because we don't
4649 want to complain if there's a static overload; non-unique
4650 cases will be handled by instantiate_type. But we need to
4651 handle this case here to allow casts on the resulting PMF.
4652 We could defer this in non-MS mode, but it's easier to give
4653 a useful error here. */
4655 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4656 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4658 if (! flag_ms_extensions)
4660 if (current_class_type
4661 && TREE_OPERAND (arg, 0) == current_class_ref)
4662 /* An expression like &memfn. */
4663 cp_pedwarn ("ISO C++ forbids taking the address of an unqualified non-static member function to form a pointer to member function. Say `&%T::%D'", base, name);
4665 cp_pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base, name);
4667 arg = build_offset_ref (base, name);
4670 if (type_unknown_p (arg))
4671 return build1 (ADDR_EXPR, unknown_type_node, arg);
4673 /* Handle complex lvalues (when permitted)
4674 by reduction to simpler cases. */
4675 val = unary_complex_lvalue (code, arg);
4679 switch (TREE_CODE (arg))
4684 case FIX_TRUNC_EXPR:
4685 case FIX_FLOOR_EXPR:
4686 case FIX_ROUND_EXPR:
4688 if (! lvalue_p (arg) && pedantic)
4689 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4696 /* Allow the address of a constructor if all the elements
4698 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4699 && TREE_CONSTANT (arg))
4701 /* Anything not already handled and not a true memory reference
4703 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4704 && TREE_CODE (argtype) != METHOD_TYPE
4705 && !lvalue_or_else (arg, "unary `&'"))
4706 return error_mark_node;
4708 if (argtype != error_mark_node)
4709 argtype = build_pointer_type (argtype);
4711 if (mark_addressable (arg) == 0)
4712 return error_mark_node;
4717 if (TREE_CODE (arg) == COMPONENT_REF)
4718 addr = build_component_addr (arg, argtype);
4720 addr = build1 (ADDR_EXPR, argtype, arg);
4722 /* Address of a static or external variable or
4723 function counts as a constant */
4725 TREE_CONSTANT (addr) = 1;
4727 if (TREE_CODE (argtype) == POINTER_TYPE
4728 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4730 build_ptrmemfunc_type (argtype);
4731 addr = build_ptrmemfunc (argtype, addr, 0);
4744 argtype = TREE_TYPE (arg);
4745 return fold (build1 (code, argtype, arg));
4748 error ("%s", errstring);
4749 return error_mark_node;
4752 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4753 for certain kinds of expressions which are not really lvalues
4754 but which we can accept as lvalues.
4756 If ARG is not a kind of expression we can handle, return zero. */
4759 unary_complex_lvalue (code, arg)
4760 enum tree_code code;
4763 /* Handle (a, b) used as an "lvalue". */
4764 if (TREE_CODE (arg) == COMPOUND_EXPR)
4766 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4767 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4768 TREE_OPERAND (arg, 0), real_result);
4771 /* Handle (a ? b : c) used as an "lvalue". */
4772 if (TREE_CODE (arg) == COND_EXPR
4773 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4774 return rationalize_conditional_expr (code, arg);
4776 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4777 if (TREE_CODE (arg) == MODIFY_EXPR
4778 || TREE_CODE (arg) == PREINCREMENT_EXPR
4779 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4781 tree lvalue = TREE_OPERAND (arg, 0);
4782 if (TREE_SIDE_EFFECTS (lvalue))
4784 lvalue = stabilize_reference (lvalue);
4785 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4786 lvalue, TREE_OPERAND (arg, 1));
4788 return unary_complex_lvalue
4789 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4792 if (code != ADDR_EXPR)
4795 /* Handle (a = b) used as an "lvalue" for `&'. */
4796 if (TREE_CODE (arg) == MODIFY_EXPR
4797 || TREE_CODE (arg) == INIT_EXPR)
4799 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4800 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4801 TREE_NO_UNUSED_WARNING (arg) = 1;
4805 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4806 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4807 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4809 /* The representation of something of type OFFSET_TYPE
4810 is really the representation of a pointer to it.
4811 Here give the representation its true type. */
4814 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4816 if (TREE_CODE (arg) != OFFSET_REF)
4819 t = TREE_OPERAND (arg, 1);
4821 /* Check all this code for right semantics. */
4822 if (TREE_CODE (t) == FUNCTION_DECL)
4824 if (DECL_DESTRUCTOR_P (t))
4825 cp_error ("taking address of destructor");
4826 return build_unary_op (ADDR_EXPR, t, 0);
4828 if (TREE_CODE (t) == VAR_DECL)
4829 return build_unary_op (ADDR_EXPR, t, 0);
4834 if (TREE_OPERAND (arg, 0)
4835 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4836 && TREE_CODE (t) != FIELD_DECL)
4838 cp_error ("taking address of bound pointer-to-member expression");
4839 return error_mark_node;
4842 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4843 type = build_pointer_type (type);
4845 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4851 /* We permit compiler to make function calls returning
4852 objects of aggregate type look like lvalues. */
4856 if (TREE_CODE (targ) == SAVE_EXPR)
4857 targ = TREE_OPERAND (targ, 0);
4859 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4861 if (TREE_CODE (arg) == SAVE_EXPR)
4864 targ = build_cplus_new (TREE_TYPE (arg), arg);
4865 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4868 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4869 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4870 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4873 /* Don't let anything else be handled specially. */
4877 /* Mark EXP saying that we need to be able to take the
4878 address of it; it should not be allocated in a register.
4879 Value is 1 if successful.
4881 C++: we do not allow `current_class_ptr' to be addressable. */
4884 mark_addressable (exp)
4887 register tree x = exp;
4889 if (TREE_ADDRESSABLE (x) == 1)
4893 switch (TREE_CODE (x))
4900 x = TREE_OPERAND (x, 0);
4904 if (x == current_class_ptr)
4906 error ("cannot take the address of `this', which is an rvalue expression");
4907 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4911 /* Caller should not be trying to mark initialized
4912 constant fields addressable. */
4913 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4914 || DECL_IN_AGGR_P (x) == 0
4916 || DECL_EXTERNAL (x), 314);
4920 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4921 && !DECL_ARTIFICIAL (x) && extra_warnings)
4922 cp_warning ("address requested for `%D', which is declared `register'",
4924 TREE_ADDRESSABLE (x) = 1;
4928 TREE_ADDRESSABLE (x) = 1;
4929 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4933 TREE_ADDRESSABLE (x) = 1;
4937 TREE_ADDRESSABLE (x) = 1;
4938 mark_addressable (TREE_OPERAND (x, 0));
4946 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4949 build_x_conditional_expr (ifexp, op1, op2)
4950 tree ifexp, op1, op2;
4952 if (processing_template_decl)
4953 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4955 return build_conditional_expr (ifexp, op1, op2);
4958 /* Handle overloading of the ',' operator when needed. Otherwise,
4959 this function just builds an expression list. */
4962 build_x_compound_expr (list)
4965 tree rest = TREE_CHAIN (list);
4968 if (processing_template_decl)
4969 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
4971 if (rest == NULL_TREE)
4972 return build_compound_expr (list);
4974 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
4975 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4977 return build_x_compound_expr (tree_cons (NULL_TREE, result,
4978 TREE_CHAIN (rest)));
4980 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
4982 /* FIXME: This test should be in the implicit cast to void of the LHS. */
4983 /* the left-hand operand of a comma expression is like an expression
4984 statement: we should warn if it doesn't have any side-effects,
4985 unless it was explicitly cast to (void). */
4986 if ((extra_warnings || warn_unused_value)
4987 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
4988 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
4989 warning("left-hand operand of comma expression has no effect");
4991 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4992 else if (warn_unused_value)
4993 warn_if_unused_value (TREE_VALUE(list));
4996 return build_compound_expr
4997 (tree_cons (NULL_TREE, TREE_VALUE (list),
4998 build_tree_list (NULL_TREE,
4999 build_x_compound_expr (rest))));
5002 /* Given a list of expressions, return a compound expression
5003 that performs them all and returns the value of the last of them. */
5006 build_compound_expr (list)
5012 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5014 if (TREE_CHAIN (list) == 0)
5016 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5017 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5018 if (TREE_CODE (list) == NOP_EXPR
5019 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5020 list = TREE_OPERAND (list, 0);
5022 return TREE_VALUE (list);
5025 first = TREE_VALUE (list);
5026 first = convert_to_void (first, "left-hand operand of comma");
5027 if (first == error_mark_node)
5028 return error_mark_node;
5030 rest = build_compound_expr (TREE_CHAIN (list));
5031 if (rest == error_mark_node)
5032 return error_mark_node;
5034 /* When pedantic, a compound expression cannot be a constant expression. */
5035 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5038 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
5042 build_static_cast (type, expr)
5048 if (type == error_mark_node || expr == error_mark_node)
5049 return error_mark_node;
5051 if (TREE_CODE (expr) == OFFSET_REF)
5052 expr = resolve_offset_ref (expr);
5054 if (processing_template_decl)
5056 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5060 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5061 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5062 if (TREE_CODE (type) != REFERENCE_TYPE
5063 && TREE_CODE (expr) == NOP_EXPR
5064 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5065 expr = TREE_OPERAND (expr, 0);
5067 if (TREE_CODE (type) == VOID_TYPE)
5069 expr = convert_to_void (expr, /*implicit=*/NULL);
5073 if (TREE_CODE (type) == REFERENCE_TYPE)
5074 return (convert_from_reference
5075 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5076 LOOKUP_COMPLAIN, NULL_TREE)));
5078 if (IS_AGGR_TYPE (type))
5079 return build_cplus_new (type, (build_method_call
5080 (NULL_TREE, complete_ctor_identifier,
5081 build_tree_list (NULL_TREE, expr),
5082 TYPE_BINFO (type), LOOKUP_NORMAL)));
5084 intype = TREE_TYPE (expr);
5086 /* FIXME handle casting to array type. */
5089 if (IS_AGGR_TYPE (intype)
5090 ? can_convert_arg (type, intype, expr)
5091 : can_convert_arg (strip_all_pointer_quals (type),
5092 strip_all_pointer_quals (intype), expr))
5093 /* This is a standard conversion. */
5095 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5097 /* They're pointers to objects. They must be aggregates that
5098 are related non-virtually. */
5102 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5103 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5104 && !binfo_from_vbase (binfo))
5107 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5108 && TREE_CODE (type) != ARRAY_TYPE
5109 && TREE_CODE (type) != FUNCTION_TYPE
5110 && can_convert (intype, strip_all_pointer_quals (type)))
5112 else if (TREE_CODE (intype) == ENUMERAL_TYPE
5113 && TREE_CODE (type) == ENUMERAL_TYPE)
5114 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
5115 converted to an enumeration type."
5116 The integral to enumeration will be accepted by the previous clause.
5117 We need to explicitly check for enumeration to enumeration. */
5120 /* [expr.static.cast]
5122 The static_cast operator shall not be used to cast away
5124 if (ok && casts_away_constness (intype, type))
5126 cp_error ("static_cast from type `%T' to type `%T' casts away constness",
5128 return error_mark_node;
5132 return build_c_cast (type, expr);
5134 cp_error ("invalid static_cast from type `%T' to type `%T'", intype, type);
5135 return error_mark_node;
5139 build_reinterpret_cast (type, expr)
5144 if (type == error_mark_node || expr == error_mark_node)
5145 return error_mark_node;
5147 if (TREE_CODE (expr) == OFFSET_REF)
5148 expr = resolve_offset_ref (expr);
5150 if (processing_template_decl)
5152 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5156 if (TREE_CODE (type) != REFERENCE_TYPE)
5158 expr = decay_conversion (expr);
5160 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5161 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5162 if (TREE_CODE (expr) == NOP_EXPR
5163 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5164 expr = TREE_OPERAND (expr, 0);
5167 intype = TREE_TYPE (expr);
5169 if (TREE_CODE (type) == REFERENCE_TYPE)
5171 if (! real_lvalue_p (expr))
5173 cp_error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
5174 return error_mark_node;
5176 expr = build_unary_op (ADDR_EXPR, expr, 0);
5177 if (expr != error_mark_node)
5178 expr = build_reinterpret_cast
5179 (build_pointer_type (TREE_TYPE (type)), expr);
5180 if (expr != error_mark_node)
5181 expr = build_indirect_ref (expr, 0);
5184 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5185 return build_static_cast (type, expr);
5187 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5188 || TREE_CODE (intype) == ENUMERAL_TYPE))
5190 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5192 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5193 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5196 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5197 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5199 expr = decl_constant_value (expr);
5200 return fold (build1 (NOP_EXPR, type, expr));
5202 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5203 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5205 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5206 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5209 expr = decl_constant_value (expr);
5210 return fold (build1 (NOP_EXPR, type, expr));
5212 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5213 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5215 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5216 expr = decl_constant_value (expr);
5217 return fold (build1 (NOP_EXPR, type, expr));
5221 cp_error ("invalid reinterpret_cast from type `%T' to type `%T'",
5223 return error_mark_node;
5226 return cp_convert (type, expr);
5230 build_const_cast (type, expr)
5235 if (type == error_mark_node || expr == error_mark_node)
5236 return error_mark_node;
5238 if (TREE_CODE (expr) == OFFSET_REF)
5239 expr = resolve_offset_ref (expr);
5241 if (processing_template_decl)
5243 tree t = build_min (CONST_CAST_EXPR, type, expr);
5247 if (!POINTER_TYPE_P (type))
5248 cp_error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5249 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5251 cp_error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5252 return error_mark_node;
5255 if (TREE_CODE (type) != REFERENCE_TYPE)
5257 expr = decay_conversion (expr);
5259 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5260 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5261 if (TREE_CODE (expr) == NOP_EXPR
5262 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5263 expr = TREE_OPERAND (expr, 0);
5266 intype = TREE_TYPE (expr);
5268 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5269 return build_static_cast (type, expr);
5270 else if (TREE_CODE (type) == REFERENCE_TYPE)
5272 if (! real_lvalue_p (expr))
5274 cp_error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5275 return error_mark_node;
5278 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5280 expr = build_unary_op (ADDR_EXPR, expr, 0);
5281 expr = build1 (NOP_EXPR, type, expr);
5282 return convert_from_reference (expr);
5285 else if (TREE_CODE (type) == POINTER_TYPE
5286 && TREE_CODE (intype) == POINTER_TYPE
5287 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5288 return cp_convert (type, expr);
5290 cp_error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5291 return error_mark_node;
5294 /* Build an expression representing a cast to type TYPE of expression EXPR.
5296 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5297 when doing the cast. */
5300 build_c_cast (type, expr)
5303 register tree value = expr;
5306 if (type == error_mark_node || expr == error_mark_node)
5307 return error_mark_node;
5309 if (processing_template_decl)
5311 tree t = build_min (CAST_EXPR, type,
5312 tree_cons (NULL_TREE, value, NULL_TREE));
5316 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5317 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5318 if (TREE_CODE (type) != REFERENCE_TYPE
5319 && TREE_CODE (value) == NOP_EXPR
5320 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5321 value = TREE_OPERAND (value, 0);
5323 if (TREE_CODE (value) == OFFSET_REF)
5324 value = resolve_offset_ref (value);
5326 if (TREE_CODE (type) == ARRAY_TYPE)
5328 /* Allow casting from T1* to T2[] because Cfront allows it.
5329 NIHCL uses it. It is not valid ISO C++ however. */
5330 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5332 cp_pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5333 type = build_pointer_type (TREE_TYPE (type));
5337 cp_error ("ISO C++ forbids casting to an array type `%T'", type);
5338 return error_mark_node;
5342 if (TREE_CODE (type) == FUNCTION_TYPE
5343 || TREE_CODE (type) == METHOD_TYPE)
5345 cp_error ("invalid cast to function type `%T'", type);
5346 return error_mark_node;
5349 if (TREE_CODE (type) == VOID_TYPE)
5351 /* Conversion to void does not cause any of the normal function to
5352 * pointer, array to pointer and lvalue to rvalue decays. */
5354 value = convert_to_void (value, /*implicit=*/NULL);
5357 /* Convert functions and arrays to pointers and
5358 convert references to their expanded types,
5359 but don't convert any other types. If, however, we are
5360 casting to a class type, there's no reason to do this: the
5361 cast will only succeed if there is a converting constructor,
5362 and the default conversions will be done at that point. In
5363 fact, doing the default conversion here is actually harmful
5367 struct S { S(const A&); };
5369 since we don't want the array-to-pointer conversion done. */
5370 if (!IS_AGGR_TYPE (type))
5372 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5373 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5374 /* Don't do the default conversion on a ->* expression. */
5375 && ! (TREE_CODE (type) == POINTER_TYPE
5376 && bound_pmf_p (value)))
5377 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5378 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5379 value = default_conversion (value);
5381 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5382 /* However, even for class types, we still need to strip away
5383 the reference type, since the call to convert_force below
5384 does not expect the input expression to be of reference
5386 value = convert_from_reference (value);
5388 otype = TREE_TYPE (value);
5390 /* Optionally warn about potentially worrisome casts. */
5393 && TREE_CODE (type) == POINTER_TYPE
5394 && TREE_CODE (otype) == POINTER_TYPE
5395 && !at_least_as_qualified_p (TREE_TYPE (type),
5397 cp_warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5400 if (TREE_CODE (type) == INTEGER_TYPE
5401 && TREE_CODE (otype) == POINTER_TYPE
5402 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5403 warning ("cast from pointer to integer of different size");
5405 if (TREE_CODE (type) == POINTER_TYPE
5406 && TREE_CODE (otype) == INTEGER_TYPE
5407 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5408 /* Don't warn about converting any constant. */
5409 && !TREE_CONSTANT (value))
5410 warning ("cast to pointer from integer of different size");
5412 if (TREE_CODE (type) == REFERENCE_TYPE)
5413 value = (convert_from_reference
5414 (convert_to_reference (type, value, CONV_C_CAST,
5415 LOOKUP_COMPLAIN, NULL_TREE)));
5420 value = decl_constant_value (value);
5423 value = convert_force (type, value, CONV_C_CAST);
5425 /* Ignore any integer overflow caused by the cast. */
5426 if (TREE_CODE (value) == INTEGER_CST)
5428 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5429 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5433 /* Warn about possible alignment problems. Do this here when we will have
5434 instantiated any necessary template types. */
5435 if (STRICT_ALIGNMENT && warn_cast_align
5436 && TREE_CODE (type) == POINTER_TYPE
5437 && TREE_CODE (otype) == POINTER_TYPE
5438 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5439 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5440 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5441 && COMPLETE_TYPE_P (TREE_TYPE (type))
5442 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5443 cp_warning ("cast from `%T' to `%T' increases required alignment of target type",
5446 /* Always produce some operator for an explicit cast,
5447 so we can tell (for -pedantic) that the cast is no lvalue. */
5448 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5449 && real_lvalue_p (value))
5450 value = non_lvalue (value);
5455 /* Build an assignment expression of lvalue LHS from value RHS.
5456 MODIFYCODE is the code for a binary operator that we use
5457 to combine the old value of LHS with RHS to get the new value.
5458 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5460 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5463 build_modify_expr (lhs, modifycode, rhs)
5465 enum tree_code modifycode;
5468 register tree result;
5470 tree lhstype = TREE_TYPE (lhs);
5471 tree olhstype = lhstype;
5474 /* Avoid duplicate error messages from operands that had errors. */
5475 if (lhs == error_mark_node || rhs == error_mark_node)
5476 return error_mark_node;
5478 /* Types that aren't fully specified cannot be used in assignments. */
5479 lhs = require_complete_type (lhs);
5483 /* Handle control structure constructs used as "lvalues". */
5485 switch (TREE_CODE (lhs))
5487 /* Handle --foo = 5; as these are valid constructs in C++ */
5488 case PREDECREMENT_EXPR:
5489 case PREINCREMENT_EXPR:
5490 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5491 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5492 stabilize_reference (TREE_OPERAND (lhs, 0)),
5493 TREE_OPERAND (lhs, 1));
5494 return build (COMPOUND_EXPR, lhstype,
5496 build_modify_expr (TREE_OPERAND (lhs, 0),
5499 /* Handle (a, b) used as an "lvalue". */
5501 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5503 if (newrhs == error_mark_node)
5504 return error_mark_node;
5505 return build (COMPOUND_EXPR, lhstype,
5506 TREE_OPERAND (lhs, 0), newrhs);
5509 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5510 if (newrhs == error_mark_node)
5511 return error_mark_node;
5512 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5514 /* Handle (a ? b : c) used as an "lvalue". */
5516 rhs = save_expr (rhs);
5518 /* Produce (a ? (b = rhs) : (c = rhs))
5519 except that the RHS goes through a save-expr
5520 so the code to compute it is only emitted once. */
5523 /* Check this here to avoid odd errors when trying to convert
5524 a throw to the type of the COND_EXPR. */
5525 if (!lvalue_or_else (lhs, "assignment"))
5526 return error_mark_node;
5528 cond = build_conditional_expr
5529 (TREE_OPERAND (lhs, 0),
5530 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5531 TREE_OPERAND (lhs, 1)),
5533 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5534 TREE_OPERAND (lhs, 2)),
5537 if (cond == error_mark_node)
5539 /* Make sure the code to compute the rhs comes out
5540 before the split. */
5541 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5542 /* Case to void to suppress warning
5543 from warn_if_unused_value. */
5544 cp_convert (void_type_node, rhs), cond);
5551 if (TREE_CODE (lhs) == OFFSET_REF)
5553 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5555 /* Static class member? */
5556 tree member = TREE_OPERAND (lhs, 1);
5557 if (TREE_CODE (member) == VAR_DECL)
5561 compiler_error ("invalid static class member");
5562 return error_mark_node;
5566 lhs = resolve_offset_ref (lhs);
5568 olhstype = lhstype = TREE_TYPE (lhs);
5571 if (lhs == error_mark_node)
5574 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5575 && modifycode != INIT_EXPR)
5577 lhs = convert_from_reference (lhs);
5578 olhstype = lhstype = TREE_TYPE (lhs);
5581 /* If a binary op has been requested, combine the old LHS value with the RHS
5582 producing the value we should actually store into the LHS. */
5584 if (modifycode == INIT_EXPR)
5586 if (TREE_CODE (rhs) == CONSTRUCTOR)
5588 if (! same_type_p (TREE_TYPE (rhs), lhstype))
5590 result = build (INIT_EXPR, lhstype, lhs, rhs);
5591 TREE_SIDE_EFFECTS (result) = 1;
5594 else if (! IS_AGGR_TYPE (lhstype))
5595 /* Do the default thing */;
5598 result = build_method_call (lhs, complete_ctor_identifier,
5599 build_tree_list (NULL_TREE, rhs),
5600 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5601 if (result == NULL_TREE)
5602 return error_mark_node;
5606 else if (modifycode == NOP_EXPR)
5608 /* `operator=' is not an inheritable operator. */
5609 if (! IS_AGGR_TYPE (lhstype))
5610 /* Do the default thing */;
5613 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5614 lhs, rhs, make_node (NOP_EXPR));
5615 if (result == NULL_TREE)
5616 return error_mark_node;
5621 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5623 my_friendly_abort (978652);
5627 lhs = stabilize_reference (lhs);
5628 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5629 if (newrhs == error_mark_node)
5631 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5632 TREE_TYPE (lhs), TREE_TYPE (rhs));
5633 return error_mark_node;
5637 /* Handle a cast used as an "lvalue".
5638 We have already performed any binary operator using the value as cast.
5639 Now convert the result to the cast type of the lhs,
5640 and then true type of the lhs and store it there;
5641 then convert result back to the cast type to be the value
5642 of the assignment. */
5644 switch (TREE_CODE (lhs))
5649 case FIX_TRUNC_EXPR:
5650 case FIX_FLOOR_EXPR:
5651 case FIX_ROUND_EXPR:
5653 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5654 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5655 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5656 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5657 newrhs = default_conversion (newrhs);
5659 tree inner_lhs = TREE_OPERAND (lhs, 0);
5662 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5663 type, otherwise the result is an rvalue. */
5664 if (! lvalue_p (lhs))
5665 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5667 result = build_modify_expr (inner_lhs, NOP_EXPR,
5668 cp_convert (TREE_TYPE (inner_lhs),
5669 cp_convert (lhstype, newrhs)));
5670 if (result == error_mark_node)
5672 return cp_convert (TREE_TYPE (lhs), result);
5679 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5680 Reject anything strange now. */
5682 if (!lvalue_or_else (lhs, "assignment"))
5683 return error_mark_node;
5685 GNU_xref_assign (lhs);
5687 /* Warn about storing in something that is `const'. */
5688 /* For C++, don't warn if this is initialization. */
5689 if (modifycode != INIT_EXPR
5690 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5691 /* Functions are not modifiable, even though they are
5693 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5694 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5695 && C_TYPE_FIELDS_READONLY (lhstype))
5696 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5697 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5698 readonly_error (lhs, "assignment", 0);
5700 /* If storing into a structure or union member,
5701 it has probably been given type `int'.
5702 Compute the type that would go with
5703 the actual amount of storage the member occupies. */
5705 if (TREE_CODE (lhs) == COMPONENT_REF
5706 && (TREE_CODE (lhstype) == INTEGER_TYPE
5707 || TREE_CODE (lhstype) == REAL_TYPE
5708 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5710 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5712 /* If storing in a field that is in actuality a short or narrower
5713 than one, we must store in the field in its actual type. */
5715 if (lhstype != TREE_TYPE (lhs))
5717 lhs = copy_node (lhs);
5718 TREE_TYPE (lhs) = lhstype;
5722 if (modifycode != INIT_EXPR)
5724 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5725 modifycode = NOP_EXPR;
5726 /* Reference-bashing */
5727 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5729 tree tmp = convert_from_reference (lhs);
5730 lhstype = TREE_TYPE (tmp);
5731 if (!COMPLETE_TYPE_P (lhstype))
5733 incomplete_type_error (lhs, lhstype);
5734 return error_mark_node;
5739 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5741 tree tmp = convert_from_reference (newrhs);
5742 if (!COMPLETE_TYPE_P (TREE_TYPE (tmp)))
5744 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5745 return error_mark_node;
5751 if (TREE_SIDE_EFFECTS (lhs))
5752 lhs = stabilize_reference (lhs);
5753 if (TREE_SIDE_EFFECTS (newrhs))
5754 newrhs = stabilize_reference (newrhs);
5756 /* Convert new value to destination type. */
5758 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5762 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5764 cp_error ("incompatible types in assignment of `%T' to `%T'",
5765 TREE_TYPE (rhs), lhstype);
5766 return error_mark_node;
5769 /* Allow array assignment in compiler-generated code. */
5770 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5771 pedwarn ("ISO C++ forbids assignment of arrays");
5773 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5774 ? 1 + (modifycode != INIT_EXPR): 0;
5775 return build_vec_init (lhs, newrhs, from_array);
5778 if (modifycode == INIT_EXPR)
5780 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5781 "initialization", NULL_TREE, 0);
5782 if (current_function_decl &&
5783 lhs == DECL_RESULT (current_function_decl))
5785 if (DECL_INITIAL (lhs))
5786 warning ("return value from function receives multiple initializations");
5787 DECL_INITIAL (lhs) = newrhs;
5792 /* Avoid warnings on enum bit fields. */
5793 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5794 && TREE_CODE (lhstype) == INTEGER_TYPE)
5796 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5798 newrhs = convert_force (lhstype, newrhs, 0);
5801 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5803 if (TREE_CODE (newrhs) == CALL_EXPR
5804 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5805 newrhs = build_cplus_new (lhstype, newrhs);
5807 /* Can't initialize directly from a TARGET_EXPR, since that would
5808 cause the lhs to be constructed twice, and possibly result in
5809 accidental self-initialization. So we force the TARGET_EXPR to be
5810 expanded without a target. */
5811 if (TREE_CODE (newrhs) == TARGET_EXPR)
5812 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5813 TREE_OPERAND (newrhs, 0));
5816 if (newrhs == error_mark_node)
5817 return error_mark_node;
5819 if (TREE_CODE (newrhs) == COND_EXPR)
5822 tree cond = TREE_OPERAND (newrhs, 0);
5824 if (TREE_SIDE_EFFECTS (lhs))
5825 cond = build_compound_expr (tree_cons
5827 build_tree_list (NULL_TREE, cond)));
5829 /* Cannot have two identical lhs on this one tree (result) as preexpand
5830 calls will rip them out and fill in RTL for them, but when the
5831 rtl is generated, the calls will only be in the first side of the
5832 condition, not on both, or before the conditional jump! (mrs) */
5833 lhs1 = break_out_calls (lhs);
5836 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5837 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5838 lhstype, lhs, newrhs);
5841 tree result_type = TREE_TYPE (newrhs);
5842 /* We have to convert each arm to the proper type because the
5843 types may have been munged by constant folding. */
5845 = build (COND_EXPR, result_type, cond,
5846 build_modify_expr (lhs, modifycode,
5847 cp_convert (result_type,
5848 TREE_OPERAND (newrhs, 1))),
5849 build_modify_expr (lhs1, modifycode,
5850 cp_convert (result_type,
5851 TREE_OPERAND (newrhs, 2))));
5855 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5856 lhstype, lhs, newrhs);
5858 TREE_SIDE_EFFECTS (result) = 1;
5860 /* If we got the LHS in a different type for storing in,
5861 convert the result back to the nominal type of LHS
5862 so that the value we return always has the same type
5863 as the LHS argument. */
5865 if (olhstype == TREE_TYPE (result))
5867 /* Avoid warnings converting integral types back into enums
5868 for enum bit fields. */
5869 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5870 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5872 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5873 TREE_NO_UNUSED_WARNING (result) = 1;
5876 return convert_for_assignment (olhstype, result, "assignment",
5881 build_x_modify_expr (lhs, modifycode, rhs)
5883 enum tree_code modifycode;
5886 if (processing_template_decl)
5887 return build_min_nt (MODOP_EXPR, lhs,
5888 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5890 if (modifycode != NOP_EXPR)
5892 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5893 make_node (modifycode));
5897 return build_modify_expr (lhs, modifycode, rhs);
5901 /* Get difference in deltas for different pointer to member function
5902 types. Return integer_zero_node, if FROM cannot be converted to a
5903 TO type. If FORCE is true, then allow reverse conversions as well.
5905 Note that the naming of FROM and TO is kind of backwards; the return
5906 value is what we add to a TO in order to get a FROM. They are named
5907 this way because we call this function to find out how to convert from
5908 a pointer to member of FROM to a pointer to member of TO. */
5911 get_delta_difference (from, to, force)
5915 tree delta = integer_zero_node;
5922 /* Should get_base_distance here, so we can check if any thing along
5923 the path is virtual, and we need to make sure we stay inside the
5924 real binfos when going through virtual bases. Maybe we should
5925 replace virtual bases with BINFO_FOR_VBASE ... (mrs) */
5926 binfo = get_binfo (from, to, 1);
5927 if (binfo == error_mark_node)
5929 error (" in pointer to member function conversion");
5936 error_not_base_type (from, to);
5937 error (" in pointer to member conversion");
5940 binfo = get_binfo (to, from, 1);
5941 if (binfo == 0 || binfo == error_mark_node)
5943 virt_binfo = binfo_from_vbase (binfo);
5947 /* This is a reinterpret cast, we choose to do nothing. */
5948 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
5949 BINFO_TYPE (virt_binfo),
5950 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5953 delta = BINFO_OFFSET (binfo);
5954 delta = cp_convert (ptrdiff_type_node, delta);
5955 delta = cp_build_binary_op (MINUS_EXPR,
5962 virt_binfo = binfo_from_vbase (binfo);
5965 /* This is a reinterpret cast, we choose to do nothing. */
5967 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
5968 BINFO_TYPE (virt_binfo),
5969 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5971 cp_error ("pointer to member conversion via virtual base `%T' of `%T'",
5972 BINFO_TYPE (virt_binfo),
5973 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5976 delta = BINFO_OFFSET (binfo);
5978 return cp_convert (ptrdiff_type_node, delta);
5981 /* Return a constructor for the pointer-to-member-function TYPE using
5982 the other components as specified. */
5985 build_ptrmemfunc1 (type, delta, pfn)
5986 tree type, delta, pfn;
5992 /* Pull the FIELD_DECLs out of the type. */
5993 pfn_field = TYPE_FIELDS (type);
5994 delta_field = TREE_CHAIN (pfn_field);
5996 /* Make sure DELTA has the type we want. */
5997 delta = convert_and_check (delta_type_node, delta);
5999 /* Finish creating the initializer. */
6000 u = tree_cons (pfn_field, pfn,
6001 build_tree_list (delta_field, delta));
6002 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6003 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
6004 TREE_STATIC (u) = (TREE_CONSTANT (u)
6005 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6007 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
6012 /* Build a constructor for a pointer to member function. It can be
6013 used to initialize global variables, local variable, or used
6014 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6017 If FORCE is non-zero, then force this conversion, even if
6018 we would rather not do it. Usually set when using an explicit
6021 Return error_mark_node, if something goes wrong. */
6024 build_ptrmemfunc (type, pfn, force)
6029 tree pfn_type = TREE_TYPE (pfn);
6030 tree to_type = build_ptrmemfunc_type (type);
6032 /* Handle multiple conversions of pointer to member functions. */
6033 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6035 tree delta = NULL_TREE;
6036 tree npfn = NULL_TREE;
6040 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6041 cp_error ("invalid conversion to type `%T' from type `%T'",
6044 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6045 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6048 /* We don't have to do any conversion to convert a
6049 pointer-to-member to its own type. But, we don't want to
6050 just return a PTRMEM_CST if there's an explicit cast; that
6051 cast should make the expression an invalid template argument. */
6052 if (TREE_CODE (pfn) != PTRMEM_CST)
6054 if (same_type_p (to_type, pfn_type))
6056 else if (integer_zerop (n))
6057 return build_reinterpret_cast (to_type, pfn);
6060 if (TREE_SIDE_EFFECTS (pfn))
6061 pfn = save_expr (pfn);
6063 /* Obtain the function pointer and the current DELTA. */
6064 if (TREE_CODE (pfn) == PTRMEM_CST)
6065 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
6068 npfn = build_component_ref (pfn, pfn_identifier, NULL_TREE, 0);
6069 delta = build_component_ref (pfn, delta_identifier, NULL_TREE, 0);
6072 /* Just adjust the DELTA field. */
6073 delta = cp_convert (ptrdiff_type_node, delta);
6074 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
6075 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
6076 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
6077 return build_ptrmemfunc1 (to_type, delta, npfn);
6080 /* Handle null pointer to member function conversions. */
6081 if (integer_zerop (pfn))
6083 pfn = build_c_cast (type, integer_zero_node);
6084 return build_ptrmemfunc1 (to_type,
6089 if (type_unknown_p (pfn))
6090 return instantiate_type (type, pfn, itf_complain);
6092 fn = TREE_OPERAND (pfn, 0);
6093 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6094 return make_ptrmem_cst (to_type, fn);
6097 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6100 ??? There is no consistency as to the types returned for the above
6101 values. Some code acts as if its a sizetype and some as if its
6102 integer_type_node. */
6105 expand_ptrmemfunc_cst (cst, delta, pfn)
6110 tree type = TREE_TYPE (cst);
6111 tree fn = PTRMEM_CST_MEMBER (cst);
6112 tree ptr_class, fn_class;
6114 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6116 /* The class that the function belongs to. */
6117 fn_class = DECL_CONTEXT (fn);
6119 /* The class that we're creating a pointer to member of. */
6120 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6122 /* First, calculate the adjustment to the function's class. */
6123 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6125 if (!DECL_VIRTUAL_P (fn))
6126 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6129 /* If we're dealing with a virtual function, we have to adjust 'this'
6130 again, to point to the base which provides the vtable entry for
6131 fn; the call will do the opposite adjustment. */
6132 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6133 tree binfo = binfo_or_else (orig_class, fn_class);
6134 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6135 *delta, BINFO_OFFSET (binfo)));
6137 /* We set PFN to the vtable offset at which the function can be
6138 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
6139 case delta is shifted left, and then incremented). */
6140 *pfn = DECL_VINDEX (fn);
6141 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
6142 TYPE_SIZE_UNIT (vtable_entry_type)));
6144 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
6146 case ptrmemfunc_vbit_in_pfn:
6147 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
6151 case ptrmemfunc_vbit_in_delta:
6152 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
6153 *delta, integer_one_node));
6154 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6155 *delta, integer_one_node));
6162 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
6167 /* Return an expression for PFN from the pointer-to-member function
6171 pfn_from_ptrmemfunc (t)
6174 if (TREE_CODE (t) == PTRMEM_CST)
6179 expand_ptrmemfunc_cst (t, &delta, &pfn);
6184 return build_component_ref (t, pfn_identifier, NULL_TREE, 0);
6187 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6188 if this is a potentially dangerous thing to do. Returns a possibly
6192 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
6195 const char *errtype;
6199 if (TREE_CODE (type) == REFERENCE_TYPE)
6200 type = TREE_TYPE (type);
6202 /* Issue warnings about peculiar, but legal, uses of NULL. */
6203 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
6206 cp_warning ("passing NULL used for non-pointer %s %P of `%D'",
6207 errtype, parmnum, fndecl);
6209 cp_warning ("%s to non-pointer type `%T' from NULL", errtype, type);
6212 /* Warn about assigning a floating-point type to an integer type. */
6213 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
6214 && TREE_CODE (type) == INTEGER_TYPE)
6217 cp_warning ("passing `%T' for %s %P of `%D'",
6218 TREE_TYPE (expr), errtype, parmnum, fndecl);
6220 cp_warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
6222 /* And warn about assigning a negative value to an unsigned
6224 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
6226 if (TREE_CODE (expr) == INTEGER_CST
6227 && TREE_NEGATED_INT (expr))
6230 cp_warning ("passing negative value `%E' for %s %P of `%D'",
6231 expr, errtype, parmnum, fndecl);
6233 cp_warning ("%s of negative value `%E' to `%T'",
6234 errtype, expr, type);
6237 overflow_warning (expr);
6239 if (TREE_CONSTANT (expr))
6245 /* Convert value RHS to type TYPE as preparation for an assignment to
6246 an lvalue of type TYPE. ERRTYPE is a string to use in error
6247 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6248 are doing the conversion in order to pass the PARMNUMth argument of
6252 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6254 const char *errtype;
6258 register enum tree_code codel = TREE_CODE (type);
6259 register tree rhstype;
6260 register enum tree_code coder;
6262 if (codel == OFFSET_TYPE)
6263 my_friendly_abort (990505);
6265 if (TREE_CODE (rhs) == OFFSET_REF)
6266 rhs = resolve_offset_ref (rhs);
6268 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6269 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6270 rhs = TREE_OPERAND (rhs, 0);
6272 rhstype = TREE_TYPE (rhs);
6273 coder = TREE_CODE (rhstype);
6275 if (rhs == error_mark_node || rhstype == error_mark_node)
6276 return error_mark_node;
6277 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6278 return error_mark_node;
6280 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6282 /* The RHS of an assignment cannot have void type. */
6283 if (coder == VOID_TYPE)
6285 error ("void value not ignored as it ought to be");
6286 return error_mark_node;
6289 /* Simplify the RHS if possible. */
6290 if (TREE_CODE (rhs) == CONST_DECL)
6291 rhs = DECL_INITIAL (rhs);
6292 else if (coder != ARRAY_TYPE)
6293 rhs = decl_constant_value (rhs);
6297 The expression is implicitly converted (clause _conv_) to the
6298 cv-unqualified type of the left operand. */
6299 if (!can_convert_arg (type, rhstype, rhs))
6301 /* When -Wno-pmf-conversions is use, we just silently allow
6302 conversions from pointers-to-members to plain pointers. If
6303 the conversion doesn't work, cp_convert will complain. */
6305 && TYPE_PTR_P (type)
6306 && TYPE_PTRMEMFUNC_P (rhstype))
6307 rhs = cp_convert (strip_top_quals (type), rhs);
6310 /* If the right-hand side has unknown type, then it is an
6311 overloaded function. Call instantiate_type to get error
6313 if (rhstype == unknown_type_node)
6314 instantiate_type (type, rhs, itf_complain);
6316 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6317 rhstype, type, parmnum, fndecl);
6319 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6321 return error_mark_node;
6324 return perform_implicit_conversion (strip_top_quals (type), rhs);
6327 /* Convert RHS to be of type TYPE.
6328 If EXP is non-zero, it is the target of the initialization.
6329 ERRTYPE is a string to use in error messages.
6331 Two major differences between the behavior of
6332 `convert_for_assignment' and `convert_for_initialization'
6333 are that references are bashed in the former, while
6334 copied in the latter, and aggregates are assigned in
6335 the former (operator=) while initialized in the
6338 If using constructor make sure no conversion operator exists, if one does
6339 exist, an ambiguity exists.
6341 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6344 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6345 tree exp, type, rhs;
6347 const char *errtype;
6351 register enum tree_code codel = TREE_CODE (type);
6352 register tree rhstype;
6353 register enum tree_code coder;
6355 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6356 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6357 if (TREE_CODE (rhs) == NOP_EXPR
6358 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6359 && codel != REFERENCE_TYPE)
6360 rhs = TREE_OPERAND (rhs, 0);
6362 if (rhs == error_mark_node
6363 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6364 return error_mark_node;
6366 if (TREE_CODE (rhs) == OFFSET_REF)
6368 rhs = resolve_offset_ref (rhs);
6369 if (rhs == error_mark_node)
6370 return error_mark_node;
6373 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6374 rhs = convert_from_reference (rhs);
6376 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6377 && TREE_CODE (type) != ARRAY_TYPE
6378 && (TREE_CODE (type) != REFERENCE_TYPE
6379 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6380 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6381 && (TREE_CODE (type) != REFERENCE_TYPE
6382 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6383 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6384 rhs = default_conversion (rhs);
6386 rhstype = TREE_TYPE (rhs);
6387 coder = TREE_CODE (rhstype);
6389 if (coder == ERROR_MARK)
6390 return error_mark_node;
6392 /* We accept references to incomplete types, so we can
6393 return here before checking if RHS is of complete type. */
6395 if (codel == REFERENCE_TYPE)
6397 /* This should eventually happen in convert_arguments. */
6398 int savew = 0, savee = 0;
6401 savew = warningcount, savee = errorcount;
6402 rhs = initialize_reference (type, rhs);
6405 if (warningcount > savew)
6406 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6407 else if (errorcount > savee)
6408 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6414 exp = require_complete_type (exp);
6415 if (exp == error_mark_node)
6416 return error_mark_node;
6418 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6419 rhstype = TREE_TYPE (rhstype);
6421 type = complete_type (type);
6423 if (IS_AGGR_TYPE (type))
6424 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6426 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6429 /* Expand an ASM statement with operands, handling output operands
6430 that are not variables or INDIRECT_REFS by transforming such
6431 cases into cases that expand_asm_operands can handle.
6433 Arguments are same as for expand_asm_operands.
6435 We don't do default conversions on all inputs, because it can screw
6436 up operands that are expected to be in memory. */
6439 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6440 tree string, outputs, inputs, clobbers;
6442 const char *filename;
6445 int noutputs = list_length (outputs);
6447 /* o[I] is the place that output number I should be written. */
6448 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6451 /* Record the contents of OUTPUTS before it is modified. */
6452 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6453 o[i] = TREE_VALUE (tail);
6455 /* Generate the ASM_OPERANDS insn;
6456 store into the TREE_VALUEs of OUTPUTS some trees for
6457 where the values were actually stored. */
6458 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6460 /* Copy all the intermediate outputs into the specified outputs. */
6461 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6463 if (o[i] != TREE_VALUE (tail))
6465 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6466 const0_rtx, VOIDmode, EXPAND_NORMAL);
6469 /* Restore the original value so that it's correct the next
6470 time we expand this function. */
6471 TREE_VALUE (tail) = o[i];
6473 /* Detect modification of read-only values.
6474 (Otherwise done by build_modify_expr.) */
6477 tree type = TREE_TYPE (o[i]);
6478 if (CP_TYPE_CONST_P (type)
6479 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6480 && C_TYPE_FIELDS_READONLY (type)))
6481 readonly_error (o[i], "modification by `asm'", 1);
6485 /* Those MODIFY_EXPRs could do autoincrements. */
6489 /* If RETVAL is the address of, or a reference to, a local variable or
6490 temporary give an appropraite warning. */
6493 maybe_warn_about_returning_address_of_local (retval)
6496 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6497 tree whats_returned = retval;
6501 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6502 whats_returned = TREE_OPERAND (whats_returned, 1);
6503 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6504 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6505 || TREE_CODE (whats_returned) == NOP_EXPR)
6506 whats_returned = TREE_OPERAND (whats_returned, 0);
6511 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6513 whats_returned = TREE_OPERAND (whats_returned, 0);
6515 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6517 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6518 || TREE_CODE (whats_returned) == TARGET_EXPR)
6520 /* Get the target. */
6521 whats_returned = TREE_OPERAND (whats_returned, 0);
6522 warning ("returning reference to temporary");
6525 if (TREE_CODE (whats_returned) == VAR_DECL
6526 && DECL_NAME (whats_returned)
6527 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6529 warning ("reference to non-lvalue returned");
6534 if (TREE_CODE (whats_returned) == VAR_DECL
6535 && DECL_NAME (whats_returned)
6536 && DECL_FUNCTION_SCOPE_P (whats_returned)
6537 && !(TREE_STATIC (whats_returned)
6538 || TREE_PUBLIC (whats_returned)))
6540 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6541 cp_warning_at ("reference to local variable `%D' returned",
6544 cp_warning_at ("address of local variable `%D' returned",
6550 /* Check that returning RETVAL from the current function is legal.
6551 Return an expression explicitly showing all conversions required to
6552 change RETVAL into the function return type, and to assign it to
6553 the DECL_RESULT for the function. */
6556 check_return_expr (retval)
6560 /* The type actually returned by the function, after any
6563 int fn_returns_value_p;
6565 /* A `volatile' function is one that isn't supposed to return, ever.
6566 (This is a G++ extension, used to get better code for functions
6567 that call the `volatile' function.) */
6568 if (TREE_THIS_VOLATILE (current_function_decl))
6569 warning ("function declared `noreturn' has a `return' statement");
6571 /* Check for various simple errors. */
6575 error ("returning a value from a destructor");
6578 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6580 if (in_function_try_handler)
6581 /* If a return statement appears in a handler of the
6582 function-try-block of a constructor, the program is ill-formed. */
6583 error ("cannot return from a handler of a function-try-block of a constructor");
6585 /* You can't return a value from a constructor. */
6586 error ("returning a value from a constructor");
6590 /* When no explicit return-value is given in a function with a named
6591 return value, the named return value is used. */
6592 result = DECL_RESULT (current_function_decl);
6593 valtype = TREE_TYPE (result);
6594 my_friendly_assert (valtype != NULL_TREE, 19990924);
6595 fn_returns_value_p = !VOID_TYPE_P (valtype);
6596 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6599 /* Check for a return statement with no return value in a function
6600 that's supposed to return a value. */
6601 if (!retval && fn_returns_value_p)
6603 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6604 /* Clear this, so finish_function won't say that we reach the
6605 end of a non-void function (which we don't, we gave a
6607 current_function_returns_null = 0;
6609 /* Check for a return statement with a value in a function that
6610 isn't supposed to return a value. */
6611 else if (retval && !fn_returns_value_p)
6613 if (VOID_TYPE_P (TREE_TYPE (retval)))
6614 /* You can return a `void' value from a function of `void'
6615 type. In that case, we have to evaluate the expression for
6616 its side-effects. */
6617 finish_expr_stmt (retval);
6619 pedwarn ("return-statement with a value, in function declared with a void return type");
6621 current_function_returns_null = 1;
6623 /* There's really no value to return, after all. */
6627 /* Remember that this function can sometimes return without a
6629 current_function_returns_null = 1;
6631 /* Remember that this function did return a value. */
6632 current_function_returns_value = 1;
6634 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6635 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6636 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6637 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6639 && null_ptr_cst_p (retval))
6640 cp_warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6642 /* Effective C++ rule 15. See also start_function. */
6644 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6645 && retval != current_class_ref)
6646 cp_warning ("`operator=' should return a reference to `*this'");
6648 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6650 [...] For a function with a class return type, if the expression
6651 in the return statement is the name of a local object, and the cv-
6652 unqualified type of the local object is the same as the function
6653 return type, an implementation is permitted to omit creating the tem-
6654 porary object to hold the function return value [...]
6656 So, if this is a value-returning function that always returns the same
6657 local variable, remember it.
6659 It might be nice to be more flexible, and choose the first suitable
6660 variable even if the function sometimes returns something else, but
6661 then we run the risk of clobbering the variable we chose if the other
6662 returned expression uses the chosen variable somehow. And people expect
6663 this restriction, anyway. (jason 2000-11-19)
6665 See finish_function, genrtl_start_function, and declare_return_variable
6666 for other pieces of this optimization. */
6668 if (fn_returns_value_p && flag_elide_constructors)
6670 if (retval != NULL_TREE
6671 && (current_function_return_value == NULL_TREE
6672 || current_function_return_value == retval)
6673 && TREE_CODE (retval) == VAR_DECL
6674 && DECL_CONTEXT (retval) == current_function_decl
6675 && ! TREE_STATIC (retval)
6676 && (DECL_ALIGN (retval)
6677 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6678 && same_type_p ((TYPE_MAIN_VARIANT
6679 (TREE_TYPE (retval))),
6681 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6682 current_function_return_value = retval;
6684 current_function_return_value = error_mark_node;
6687 /* We don't need to do any conversions when there's nothing being
6689 if (!retval || retval == error_mark_node)
6692 /* Do any required conversions. */
6693 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6694 /* No conversions are required. */
6698 /* The type the function is declared to return. */
6699 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6701 /* First convert the value to the function's return type, then
6702 to the type of return value's location to handle the
6703 case that functype is smaller than the valtype. */
6704 retval = convert_for_initialization
6705 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6706 "return", NULL_TREE, 0);
6707 retval = convert (valtype, retval);
6709 /* If the conversion failed, treat this just like `return;'. */
6710 if (retval == error_mark_node)
6712 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6713 else if (! current_function_returns_struct
6714 && TREE_CODE (retval) == TARGET_EXPR
6715 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6716 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6717 TREE_OPERAND (retval, 0));
6719 maybe_warn_about_returning_address_of_local (retval);
6722 /* Actually copy the value returned into the appropriate location. */
6723 if (retval && retval != result)
6724 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6730 /* Returns non-zero if the pointer-type FROM can be converted to the
6731 pointer-type TO via a qualification conversion. If CONSTP is -1,
6732 then we return non-zero if the pointers are similar, and the
6733 cv-qualification signature of FROM is a proper subset of that of TO.
6735 If CONSTP is positive, then all outer pointers have been
6739 comp_ptr_ttypes_real (to, from, constp)
6743 int to_more_cv_qualified = 0;
6745 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6747 if (TREE_CODE (to) != TREE_CODE (from))
6750 if (TREE_CODE (from) == OFFSET_TYPE
6751 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6752 TYPE_OFFSET_BASETYPE (to)))
6755 /* Const and volatile mean something different for function types,
6756 so the usual checks are not appropriate. */
6757 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6759 if (!at_least_as_qualified_p (to, from))
6762 if (!at_least_as_qualified_p (from, to))
6767 ++to_more_cv_qualified;
6771 constp &= TYPE_READONLY (to);
6774 if (TREE_CODE (to) != POINTER_TYPE)
6776 same_type_ignoring_top_level_qualifiers_p (to, from)
6777 && (constp >= 0 || to_more_cv_qualified);
6781 /* When comparing, say, char ** to char const **, this function takes the
6782 'char *' and 'char const *'. Do not pass non-pointer types to this
6786 comp_ptr_ttypes (to, from)
6789 return comp_ptr_ttypes_real (to, from, 1);
6792 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6793 type or inheritance-related types, regardless of cv-quals. */
6796 ptr_reasonably_similar (to, from)
6799 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6801 if (TREE_CODE (to) != TREE_CODE (from))
6804 if (TREE_CODE (from) == OFFSET_TYPE
6805 && comptypes (TYPE_OFFSET_BASETYPE (to),
6806 TYPE_OFFSET_BASETYPE (from),
6807 COMPARE_BASE | COMPARE_RELAXED))
6810 if (TREE_CODE (to) != POINTER_TYPE)
6812 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6813 COMPARE_BASE | COMPARE_RELAXED);
6817 /* Like comp_ptr_ttypes, for const_cast. */
6820 comp_ptr_ttypes_const (to, from)
6823 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6825 if (TREE_CODE (to) != TREE_CODE (from))
6828 if (TREE_CODE (from) == OFFSET_TYPE
6829 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6830 TYPE_OFFSET_BASETYPE (to)))
6833 if (TREE_CODE (to) != POINTER_TYPE)
6834 return same_type_ignoring_top_level_qualifiers_p (to, from);
6838 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6841 comp_ptr_ttypes_reinterpret (to, from)
6846 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6848 if (TREE_CODE (from) == OFFSET_TYPE)
6849 from = TREE_TYPE (from);
6850 if (TREE_CODE (to) == OFFSET_TYPE)
6851 to = TREE_TYPE (to);
6853 /* Const and volatile mean something different for function types,
6854 so the usual checks are not appropriate. */
6855 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6856 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6858 if (!at_least_as_qualified_p (to, from))
6862 && !at_least_as_qualified_p (from, to))
6864 constp &= TYPE_READONLY (to);
6867 if (TREE_CODE (from) != POINTER_TYPE
6868 || TREE_CODE (to) != POINTER_TYPE)
6873 /* Returns the type-qualifier set corresponding to TYPE. */
6876 cp_type_quals (type)
6879 type = strip_array_types (type);
6880 return TYPE_QUALS (type);
6883 /* Returns non-zero if the TYPE contains a mutable member */
6886 cp_has_mutable_p (type)
6889 type = strip_array_types (type);
6891 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6894 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6895 exemplar types such that casting T1 to T2 is casting away castness
6896 if and only if there is no implicit conversion from T1 to T2. */
6899 casts_away_constness_r (t1, t2)
6906 /* [expr.const.cast]
6908 For multi-level pointer to members and multi-level mixed pointers
6909 and pointers to members (conv.qual), the "member" aspect of a
6910 pointer to member level is ignored when determining if a const
6911 cv-qualifier has been cast away. */
6912 if (TYPE_PTRMEM_P (*t1))
6913 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6914 if (TYPE_PTRMEM_P (*t2))
6915 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6917 /* [expr.const.cast]
6919 For two pointer types:
6921 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6922 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6925 casting from X1 to X2 casts away constness if, for a non-pointer
6926 type T there does not exist an implicit conversion (clause
6929 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6933 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6935 if (TREE_CODE (*t1) != POINTER_TYPE
6936 || TREE_CODE (*t2) != POINTER_TYPE)
6938 *t1 = cp_build_qualified_type (void_type_node,
6939 CP_TYPE_QUALS (*t1));
6940 *t2 = cp_build_qualified_type (void_type_node,
6941 CP_TYPE_QUALS (*t2));
6945 quals1 = CP_TYPE_QUALS (*t1);
6946 quals2 = CP_TYPE_QUALS (*t2);
6947 *t1 = TREE_TYPE (*t1);
6948 *t2 = TREE_TYPE (*t2);
6949 casts_away_constness_r (t1, t2);
6950 *t1 = build_pointer_type (*t1);
6951 *t2 = build_pointer_type (*t2);
6952 *t1 = cp_build_qualified_type (*t1, quals1);
6953 *t2 = cp_build_qualified_type (*t2, quals2);
6956 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
6960 casts_away_constness (t1, t2)
6964 if (TREE_CODE (t2) == REFERENCE_TYPE)
6966 /* [expr.const.cast]
6968 Casting from an lvalue of type T1 to an lvalue of type T2
6969 using a reference cast casts away constness if a cast from an
6970 rvalue of type "pointer to T1" to the type "pointer to T2"
6971 casts away constness. */
6972 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
6973 ? TREE_TYPE (t1) : t1);
6974 return casts_away_constness (build_pointer_type (t1),
6975 build_pointer_type (TREE_TYPE (t2)));
6978 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6979 /* [expr.const.cast]
6981 Casting from an rvalue of type "pointer to data member of X
6982 of type T1" to the type "pointer to data member of Y of type
6983 T2" casts away constness if a cast from an rvalue of type
6984 "poitner to T1" to the type "pointer to T2" casts away
6986 return casts_away_constness
6987 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
6988 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
6990 /* Casting away constness is only something that makes sense for
6991 pointer or reference types. */
6992 if (TREE_CODE (t1) != POINTER_TYPE
6993 || TREE_CODE (t2) != POINTER_TYPE)
6996 /* Top-level qualifiers don't matter. */
6997 t1 = TYPE_MAIN_VARIANT (t1);
6998 t2 = TYPE_MAIN_VARIANT (t2);
6999 casts_away_constness_r (&t1, &t2);
7000 if (!can_convert (t2, t1))
7006 /* Returns TYPE with its cv qualifiers removed
7007 TYPE is T cv* .. *cv where T is not a pointer type,
7008 returns T * .. *. (If T is an array type, then the cv qualifiers
7009 above are those of the array members.) */
7012 strip_all_pointer_quals (type)
7015 if (TREE_CODE (type) == POINTER_TYPE)
7016 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
7017 else if (TREE_CODE (type) == OFFSET_TYPE)
7018 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
7019 strip_all_pointer_quals (TREE_TYPE (type)));
7021 return TYPE_MAIN_VARIANT (type);