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 /* Implement the __alignof keyword: Return the minimum required
1675 alignment of TYPE, measured in bytes. */
1681 enum tree_code code = TREE_CODE (type);
1684 if (processing_template_decl)
1685 return build_min (ALIGNOF_EXPR, sizetype, type);
1687 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1688 t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1689 else if (code == VOID_TYPE || code == ERROR_MARK)
1693 /* Similar to sizeof, __alignof applies to the referant. */
1694 if (code == REFERENCE_TYPE)
1695 type = TREE_TYPE (type);
1697 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1700 return fold (build1 (NOP_EXPR, c_size_type_node, t));
1703 /* Perform the array-to-pointer and function-to-pointer conversions
1706 In addition, references are converted to lvalues and manifest
1707 constants are replaced by their values. */
1710 decay_conversion (exp)
1714 register enum tree_code code;
1716 if (TREE_CODE (exp) == OFFSET_REF)
1717 exp = resolve_offset_ref (exp);
1719 type = TREE_TYPE (exp);
1720 code = TREE_CODE (type);
1722 if (code == REFERENCE_TYPE)
1724 exp = convert_from_reference (exp);
1725 type = TREE_TYPE (exp);
1726 code = TREE_CODE (type);
1729 if (type == error_mark_node)
1730 return error_mark_node;
1732 /* Constants can be used directly unless they're not loadable. */
1733 if (TREE_CODE (exp) == CONST_DECL)
1734 exp = DECL_INITIAL (exp);
1735 /* Replace a nonvolatile const static variable with its value. We
1736 don't do this for arrays, though; we want the address of the
1737 first element of the array, not the address of the first element
1738 of its initializing constant. */
1739 else if (code != ARRAY_TYPE)
1741 exp = decl_constant_value (exp);
1742 type = TREE_TYPE (exp);
1745 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1746 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1748 if (code == VOID_TYPE)
1750 error ("void value not ignored as it ought to be");
1751 return error_mark_node;
1753 if (code == METHOD_TYPE)
1754 my_friendly_abort (990506);
1755 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1756 return build_unary_op (ADDR_EXPR, exp, 0);
1757 if (code == ARRAY_TYPE)
1762 if (TREE_CODE (exp) == INDIRECT_REF)
1764 /* Stripping away the INDIRECT_REF is not the right
1765 thing to do for references... */
1766 tree inner = TREE_OPERAND (exp, 0);
1767 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1769 inner = build1 (CONVERT_EXPR,
1770 build_pointer_type (TREE_TYPE
1771 (TREE_TYPE (inner))),
1773 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1775 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1778 if (TREE_CODE (exp) == COMPOUND_EXPR)
1780 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1781 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1782 TREE_OPERAND (exp, 0), op1);
1786 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1788 error ("invalid use of non-lvalue array");
1789 return error_mark_node;
1792 ptrtype = build_pointer_type (TREE_TYPE (type));
1794 if (TREE_CODE (exp) == VAR_DECL)
1796 /* ??? This is not really quite correct
1797 in that the type of the operand of ADDR_EXPR
1798 is not the target type of the type of the ADDR_EXPR itself.
1799 Question is, can this lossage be avoided? */
1800 adr = build1 (ADDR_EXPR, ptrtype, exp);
1801 if (mark_addressable (exp) == 0)
1802 return error_mark_node;
1803 TREE_CONSTANT (adr) = staticp (exp);
1804 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1807 /* This way is better for a COMPONENT_REF since it can
1808 simplify the offset for a component. */
1809 adr = build_unary_op (ADDR_EXPR, exp, 1);
1810 return cp_convert (ptrtype, adr);
1813 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1814 rvalues always have cv-unqualified types. */
1815 if (! CLASS_TYPE_P (type))
1816 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1822 default_conversion (exp)
1826 enum tree_code code;
1828 exp = decay_conversion (exp);
1830 type = TREE_TYPE (exp);
1831 code = TREE_CODE (type);
1833 if (INTEGRAL_CODE_P (code))
1835 tree t = type_promotes_to (type);
1837 return cp_convert (t, exp);
1843 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1847 inline_conversion (exp)
1850 if (TREE_CODE (exp) == FUNCTION_DECL)
1851 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1856 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1857 decay_conversion to one. */
1860 string_conv_p (totype, exp, warn)
1866 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1869 t = TREE_TYPE (totype);
1870 if (!same_type_p (t, char_type_node)
1871 && !same_type_p (t, wchar_type_node))
1874 if (TREE_CODE (exp) == STRING_CST)
1876 /* Make sure that we don't try to convert between char and wchar_t. */
1877 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1882 /* Is this a string constant which has decayed to 'const char *'? */
1883 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1884 if (!same_type_p (TREE_TYPE (exp), t))
1887 if (TREE_CODE (exp) != ADDR_EXPR
1888 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1892 /* This warning is not very useful, as it complains about printf. */
1893 if (warn && warn_write_strings)
1894 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1900 build_object_ref (datum, basetype, field)
1901 tree datum, basetype, field;
1904 if (datum == error_mark_node)
1905 return error_mark_node;
1907 dtype = TREE_TYPE (datum);
1908 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1909 dtype = TREE_TYPE (dtype);
1910 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1912 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1913 basetype, field, dtype);
1914 return error_mark_node;
1916 else if (is_aggr_type (basetype, 1))
1918 tree binfo = binfo_or_else (basetype, dtype);
1920 return build_x_component_ref (build_scoped_ref (datum, basetype),
1923 return error_mark_node;
1926 /* Like `build_component_ref, but uses an already found field, and converts
1927 from a reference. Must compute access for current_class_ref.
1931 build_component_ref_1 (datum, field, protect)
1935 return convert_from_reference
1936 (build_component_ref (datum, field, NULL_TREE, protect));
1939 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1940 can, for example, use as an lvalue. This code used to be in
1941 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1942 expressions, where we're dealing with aggregates. But now it's again only
1943 called from unary_complex_lvalue. The case (in particular) that led to
1944 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1948 rationalize_conditional_expr (code, t)
1949 enum tree_code code;
1952 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1953 the first operand is always the one to be used if both operands
1954 are equal, so we know what conditional expression this used to be. */
1955 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1958 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1959 ? LE_EXPR : GE_EXPR),
1960 TREE_OPERAND (t, 0),
1961 TREE_OPERAND (t, 1)),
1962 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1963 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1967 build_conditional_expr (TREE_OPERAND (t, 0),
1968 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1969 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1972 /* Given the TYPE of an anonymous union field inside T, return the
1973 FIELD_DECL for the field. If not found return NULL_TREE. Because
1974 anonymous unions can nest, we must also search all anonymous unions
1975 that are directly reachable. */
1978 lookup_anon_field (t, type)
1983 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1985 if (TREE_STATIC (field))
1987 if (TREE_CODE (field) != FIELD_DECL)
1990 /* If we find it directly, return the field. */
1991 if (DECL_NAME (field) == NULL_TREE
1992 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1997 /* Otherwise, it could be nested, search harder. */
1998 if (DECL_NAME (field) == NULL_TREE
1999 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2001 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2009 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2010 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2011 that we are interested in, or it can be a FIELD_DECL. */
2014 build_component_ref (datum, component, basetype_path, protect)
2015 tree datum, component, basetype_path;
2018 register tree basetype;
2019 register enum tree_code code;
2020 register tree field = NULL;
2025 if (processing_template_decl)
2026 return build_min_nt (COMPONENT_REF, datum, component);
2028 if (datum == error_mark_node
2029 || TREE_TYPE (datum) == error_mark_node)
2030 return error_mark_node;
2032 /* BASETYPE holds the type of the class containing the COMPONENT. */
2033 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2035 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2037 switch (TREE_CODE (datum))
2041 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2042 basetype_path, protect);
2043 return build (COMPOUND_EXPR, TREE_TYPE (value),
2044 TREE_OPERAND (datum, 0), value);
2047 return build_conditional_expr
2048 (TREE_OPERAND (datum, 0),
2049 build_component_ref (TREE_OPERAND (datum, 1), component,
2050 basetype_path, protect),
2051 build_component_ref (TREE_OPERAND (datum, 2), component,
2052 basetype_path, protect));
2055 cp_error ("invalid use of %D", datum);
2056 datum = error_mark_node;
2063 code = TREE_CODE (basetype);
2065 if (code == REFERENCE_TYPE)
2067 datum = convert_from_reference (datum);
2068 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2069 code = TREE_CODE (basetype);
2071 if (TREE_CODE (datum) == OFFSET_REF)
2073 datum = resolve_offset_ref (datum);
2074 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2075 code = TREE_CODE (basetype);
2078 /* First, see if there is a field or component with name COMPONENT. */
2079 if (TREE_CODE (component) == TREE_LIST)
2081 /* I could not trigger this code. MvL */
2082 my_friendly_abort (980326);
2084 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2085 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2087 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2090 if (! IS_AGGR_TYPE_CODE (code))
2092 if (code != ERROR_MARK)
2093 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2094 component, datum, basetype);
2095 return error_mark_node;
2098 if (!complete_type_or_else (basetype, datum))
2099 return error_mark_node;
2101 if (TREE_CODE (component) == BIT_NOT_EXPR)
2103 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2105 cp_error ("destructor specifier `%T::~%T' must have matching names",
2106 basetype, TREE_OPERAND (component, 0));
2107 return error_mark_node;
2109 if (! TYPE_HAS_DESTRUCTOR (basetype))
2111 cp_error ("type `%T' has no destructor", basetype);
2112 return error_mark_node;
2114 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2117 /* Look up component name in the structure type definition. */
2118 if (TYPE_VFIELD (basetype)
2119 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2120 /* Special-case this because if we use normal lookups in an ambiguous
2121 hierarchy, the compiler will abort (because vptr lookups are
2122 not supposed to be ambiguous. */
2123 field = TYPE_VFIELD (basetype);
2124 else if (TREE_CODE (component) == FIELD_DECL)
2126 else if (TREE_CODE (component) == TYPE_DECL)
2128 cp_error ("invalid use of type decl `%#D' as expression", component);
2129 return error_mark_node;
2131 else if (TREE_CODE (component) == TEMPLATE_DECL)
2133 cp_error ("invalid use of template `%#D' as expression", component);
2134 return error_mark_node;
2138 tree name = component;
2139 if (TREE_CODE (component) == VAR_DECL)
2140 name = DECL_NAME (component);
2141 if (TREE_CODE (component) == NAMESPACE_DECL)
2142 /* Source is in error, but produce a sensible diagnostic. */
2143 name = DECL_NAME (component);
2144 if (basetype_path == NULL_TREE)
2145 basetype_path = TYPE_BINFO (basetype);
2146 field = lookup_field (basetype_path, name,
2147 protect && !VFIELD_NAME_P (name), 0);
2148 if (field == error_mark_node)
2149 return error_mark_node;
2151 if (field == NULL_TREE)
2153 /* Not found as a data field, look for it as a method. If found,
2154 then if this is the only possible one, return it, else
2155 report ambiguity error. */
2156 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2157 if (fndecls == error_mark_node)
2158 return error_mark_node;
2161 /* If the function is unique and static, we can resolve it
2162 now. Otherwise, we have to wait and see what context it is
2163 used in; a component_ref involving a non-static member
2164 function can only be used in a call (expr.ref). */
2166 if (TREE_CHAIN (fndecls) == NULL_TREE
2167 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2169 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2171 tree fndecl = TREE_VALUE (fndecls);
2172 enforce_access (basetype_path, fndecl);
2178 /* A unique non-static member function. Other parts
2179 of the compiler expect something with
2180 unknown_type_node to be really overloaded, so
2182 TREE_VALUE (fndecls)
2183 = ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2187 ref = build (COMPONENT_REF, unknown_type_node,
2188 datum, TREE_VALUE (fndecls));
2192 cp_error ("`%#T' has no member named `%D'", basetype, name);
2193 return error_mark_node;
2195 else if (TREE_TYPE (field) == error_mark_node)
2196 return error_mark_node;
2198 if (TREE_CODE (field) != FIELD_DECL)
2200 if (TREE_CODE (field) == TYPE_DECL)
2201 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2202 else if (DECL_RTL (field) != 0)
2205 TREE_USED (field) = 1;
2207 /* Do evaluate the object when accessing a static member. */
2208 if (TREE_SIDE_EFFECTS (datum))
2209 field = build (COMPOUND_EXPR, TREE_TYPE (field), datum, field);
2215 /* See if we have to do any conversions so that we pick up the field from the
2217 if (DECL_FIELD_CONTEXT (field) != basetype)
2219 tree context = DECL_FIELD_CONTEXT (field);
2220 tree base = context;
2221 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2222 && ANON_AGGR_TYPE_P (base))
2223 base = TYPE_CONTEXT (base);
2225 /* Handle base classes here... */
2226 if (base != basetype && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
2228 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2229 if (integer_zerop (addr))
2231 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2232 return error_mark_node;
2234 if (VBASE_NAME_P (DECL_NAME (field)))
2236 /* It doesn't matter which vbase pointer we grab, just
2237 find one of them. */
2238 tree binfo = get_binfo (base,
2239 TREE_TYPE (TREE_TYPE (addr)), 0);
2240 addr = convert_pointer_to_real (binfo, addr);
2243 addr = convert_pointer_to (base, addr);
2244 datum = build_indirect_ref (addr, NULL);
2245 if (datum == error_mark_node)
2246 return error_mark_node;
2250 /* Handle things from anon unions here... */
2251 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2253 tree subfield = lookup_anon_field (basetype, context);
2254 tree subdatum = build_component_ref (datum, subfield,
2255 basetype_path, protect);
2256 return build_component_ref (subdatum, field, basetype_path, protect);
2260 /* Compute the type of the field, as described in [expr.ref]. */
2261 type_quals = TYPE_UNQUALIFIED;
2262 field_type = TREE_TYPE (field);
2263 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2264 /* The standard says that the type of the result should be the
2265 type referred to by the reference. But for now, at least, we
2266 do the conversion from reference type later. */
2270 type_quals = (CP_TYPE_QUALS (field_type)
2271 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2273 /* A field is const (volatile) if the enclosing object, or the
2274 field itself, is const (volatile). But, a mutable field is
2275 not const, even within a const object. */
2276 if (DECL_MUTABLE_P (field))
2277 type_quals &= ~TYPE_QUAL_CONST;
2278 field_type = cp_build_qualified_type (field_type, type_quals);
2281 ref = fold (build (COMPONENT_REF, field_type, datum, field));
2283 /* Mark the expression const or volatile, as appropriate. Even
2284 though we've dealt with the type above, we still have to mark the
2285 expression itself. */
2286 if (type_quals & TYPE_QUAL_CONST)
2287 TREE_READONLY (ref) = 1;
2288 else if (type_quals & TYPE_QUAL_VOLATILE)
2289 TREE_THIS_VOLATILE (ref) = 1;
2294 /* Variant of build_component_ref for use in expressions, which should
2295 never have REFERENCE_TYPE. */
2298 build_x_component_ref (datum, component, basetype_path, protect)
2299 tree datum, component, basetype_path;
2302 tree t = build_component_ref (datum, component, basetype_path, protect);
2304 if (! processing_template_decl)
2305 t = convert_from_reference (t);
2310 /* Given an expression PTR for a pointer, return an expression
2311 for the value pointed to.
2312 ERRORSTRING is the name of the operator to appear in error messages.
2314 This function may need to overload OPERATOR_FNNAME.
2315 Must also handle REFERENCE_TYPEs for C++. */
2318 build_x_indirect_ref (ptr, errorstring)
2320 const char *errorstring;
2324 if (processing_template_decl)
2325 return build_min_nt (INDIRECT_REF, ptr);
2327 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2331 return build_indirect_ref (ptr, errorstring);
2335 build_indirect_ref (ptr, errorstring)
2337 const char *errorstring;
2339 register tree pointer, type;
2341 if (ptr == error_mark_node)
2342 return error_mark_node;
2344 if (ptr == current_class_ptr)
2345 return current_class_ref;
2347 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2348 ? ptr : default_conversion (ptr));
2349 type = TREE_TYPE (pointer);
2351 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2355 If the type of the expression is "pointer to T," the type
2356 of the result is "T."
2358 We must use the canonical variant because certain parts of
2359 the back end, like fold, do pointer comparisons between
2361 tree t = canonical_type_variant (TREE_TYPE (type));
2363 if (VOID_TYPE_P (t))
2365 /* A pointer to incomplete type (other than cv void) can be
2366 dereferenced [expr.unary.op]/1 */
2367 cp_error ("`%T' is not a pointer-to-object type", type);
2368 return error_mark_node;
2370 else if (TREE_CODE (pointer) == ADDR_EXPR
2372 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2373 /* The POINTER was something like `&x'. We simplify `*&x' to
2375 return TREE_OPERAND (pointer, 0);
2378 tree ref = build1 (INDIRECT_REF, t, pointer);
2380 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2381 so that we get the proper error message if the result is used
2382 to assign to. Also, &* is supposed to be a no-op. */
2383 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2384 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2385 TREE_SIDE_EFFECTS (ref)
2386 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2391 /* `pointer' won't be an error_mark_node if we were given a
2392 pointer to member, so it's cool to check for this here. */
2393 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2394 error ("invalid use of `%s' on pointer to member", errorstring);
2395 else if (pointer != error_mark_node)
2398 error ("invalid type argument of `%s'", errorstring);
2400 error ("invalid type argument");
2402 return error_mark_node;
2405 /* This handles expressions of the form "a[i]", which denotes
2408 This is logically equivalent in C to *(a+i), but we may do it differently.
2409 If A is a variable or a member, we generate a primitive ARRAY_REF.
2410 This avoids forcing the array out of registers, and can work on
2411 arrays that are not lvalues (for example, members of structures returned
2414 If INDEX is of some user-defined type, it must be converted to
2415 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2416 will inherit the type of the array, which will be some pointer type. */
2419 build_array_ref (array, idx)
2424 error ("subscript missing in array reference");
2425 return error_mark_node;
2428 if (TREE_TYPE (array) == error_mark_node
2429 || TREE_TYPE (idx) == error_mark_node)
2430 return error_mark_node;
2432 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2434 switch (TREE_CODE (array))
2438 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2439 return build (COMPOUND_EXPR, TREE_TYPE (value),
2440 TREE_OPERAND (array, 0), value);
2444 return build_conditional_expr
2445 (TREE_OPERAND (array, 0),
2446 build_array_ref (TREE_OPERAND (array, 1), idx),
2447 build_array_ref (TREE_OPERAND (array, 2), idx));
2453 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2454 && TREE_CODE (array) != INDIRECT_REF)
2458 /* Subscripting with type char is likely to lose
2459 on a machine where chars are signed.
2460 So warn on any machine, but optionally.
2461 Don't warn for unsigned char since that type is safe.
2462 Don't warn for signed char because anyone who uses that
2463 must have done so deliberately. */
2464 if (warn_char_subscripts
2465 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2466 warning ("array subscript has type `char'");
2468 /* Apply default promotions *after* noticing character types. */
2469 idx = default_conversion (idx);
2471 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2473 error ("array subscript is not an integer");
2474 return error_mark_node;
2477 /* An array that is indexed by a non-constant
2478 cannot be stored in a register; we must be able to do
2479 address arithmetic on its address.
2480 Likewise an array of elements of variable size. */
2481 if (TREE_CODE (idx) != INTEGER_CST
2482 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2483 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2486 if (mark_addressable (array) == 0)
2487 return error_mark_node;
2490 /* An array that is indexed by a constant value which is not within
2491 the array bounds cannot be stored in a register either; because we
2492 would get a crash in store_bit_field/extract_bit_field when trying
2493 to access a non-existent part of the register. */
2494 if (TREE_CODE (idx) == INTEGER_CST
2495 && TYPE_VALUES (TREE_TYPE (array))
2496 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2498 if (mark_addressable (array) == 0)
2499 return error_mark_node;
2502 if (pedantic && !lvalue_p (array))
2503 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2505 /* Note in C++ it is valid to subscript a `register' array, since
2506 it is valid to take the address of something with that
2507 storage specification. */
2511 while (TREE_CODE (foo) == COMPONENT_REF)
2512 foo = TREE_OPERAND (foo, 0);
2513 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2514 warning ("subscripting array declared `register'");
2517 type = TREE_TYPE (TREE_TYPE (array));
2518 rval = build (ARRAY_REF, type, array, idx);
2519 /* Array ref is const/volatile if the array elements are
2520 or if the array is.. */
2521 TREE_READONLY (rval)
2522 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2523 TREE_SIDE_EFFECTS (rval)
2524 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2525 TREE_THIS_VOLATILE (rval)
2526 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2527 return require_complete_type (fold (rval));
2531 tree ar = default_conversion (array);
2532 tree ind = default_conversion (idx);
2534 /* Put the integer in IND to simplify error checking. */
2535 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2542 if (ar == error_mark_node)
2545 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2547 error ("subscripted value is neither array nor pointer");
2548 return error_mark_node;
2550 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2552 error ("array subscript is not an integer");
2553 return error_mark_node;
2556 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2561 /* Build a function call to function FUNCTION with parameters PARAMS.
2562 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2563 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2564 not include any object pointer that may be required. FUNCTION's
2565 data type may be a function type or a pointer-to-function.
2567 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2568 is the list of possible methods that FUNCTION could conceivably
2569 be. If the list of methods comes from a class, then it will be
2570 a list of lists (where each element is associated with the class
2571 that produced it), otherwise it will be a simple list (for
2572 functions overloaded in global scope).
2574 In the first case, TREE_VALUE (function) is the head of one of those
2575 lists, and TREE_PURPOSE is the name of the function.
2577 In the second case, TREE_PURPOSE (function) is the function's
2580 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2582 When calling a TEMPLATE_DECL, we don't require a complete return
2586 build_x_function_call (function, params, decl)
2587 tree function, params, decl;
2590 tree template_id = NULL_TREE;
2593 if (function == error_mark_node)
2594 return error_mark_node;
2596 if (processing_template_decl)
2597 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2599 /* Save explicit template arguments if found */
2600 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2602 template_id = function;
2603 function = TREE_OPERAND (function, 0);
2606 type = TREE_TYPE (function);
2608 if (TREE_CODE (type) == OFFSET_TYPE
2609 && TREE_TYPE (type) == unknown_type_node
2610 && TREE_CODE (function) == TREE_LIST
2611 && TREE_CHAIN (function) == NULL_TREE)
2613 /* Undo (Foo:bar)()... */
2614 type = TYPE_OFFSET_BASETYPE (type);
2615 function = TREE_VALUE (function);
2616 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2617 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2618 function = TREE_VALUE (function);
2619 if (TREE_CODE (function) == OVERLOAD)
2620 function = OVL_FUNCTION (function);
2621 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2622 function = DECL_NAME (function);
2623 return build_method_call (decl, function, params,
2624 TYPE_BINFO (type), LOOKUP_NORMAL);
2627 if (TREE_CODE (function) == OFFSET_REF
2628 && TREE_CODE (type) != METHOD_TYPE)
2629 function = resolve_offset_ref (function);
2631 if ((TREE_CODE (function) == FUNCTION_DECL
2632 && DECL_STATIC_FUNCTION_P (function))
2633 || (DECL_FUNCTION_TEMPLATE_P (function)
2634 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function))))
2635 return build_member_call (DECL_CONTEXT (function),
2637 ? template_id : DECL_NAME (function),
2640 is_method = ((TREE_CODE (function) == TREE_LIST
2641 && current_class_type != NULL_TREE
2642 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2644 || (TREE_CODE (function) == OVERLOAD
2645 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2646 || TREE_CODE (function) == IDENTIFIER_NODE
2647 || TREE_CODE (type) == METHOD_TYPE
2648 || TYPE_PTRMEMFUNC_P (type));
2650 /* A friend template. Make it look like a toplevel declaration. */
2651 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2652 function = ovl_cons (function, NULL_TREE);
2654 /* Handle methods, friends, and overloaded functions, respectively. */
2657 tree basetype = NULL_TREE;
2659 if (TREE_CODE (function) == OVERLOAD)
2660 function = OVL_CURRENT (function);
2662 if (TREE_CODE (function) == FUNCTION_DECL
2663 || DECL_FUNCTION_TEMPLATE_P (function))
2665 basetype = DECL_CONTEXT (function);
2667 if (DECL_NAME (function))
2668 function = DECL_NAME (function);
2670 function = TYPE_IDENTIFIER (DECL_CONTEXT (function));
2672 else if (TREE_CODE (function) == TREE_LIST)
2674 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2675 == FUNCTION_DECL, 312);
2676 basetype = DECL_CONTEXT (TREE_VALUE (function));
2677 function = TREE_PURPOSE (function);
2679 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2681 if (TREE_CODE (function) == OFFSET_REF)
2683 if (TREE_OPERAND (function, 0))
2684 decl = TREE_OPERAND (function, 0);
2686 /* Call via a pointer to member function. */
2687 if (decl == NULL_TREE)
2689 error ("pointer to member function called, but not in class scope");
2690 return error_mark_node;
2692 /* What other type of POINTER_TYPE could this be? */
2693 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2694 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2695 && TREE_CODE (function) != OFFSET_REF)
2696 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2701 /* this is an abbreviated method call.
2702 must go through here in case it is a virtual function.
2703 @@ Perhaps this could be optimized. */
2705 if (basetype && (! current_class_type
2706 || ! DERIVED_FROM_P (basetype, current_class_type)))
2707 return build_member_call (basetype, function, params);
2709 if (decl == NULL_TREE)
2711 if (current_class_type == NULL_TREE)
2713 cp_error ("object missing in call to method `%D'", function);
2714 return error_mark_node;
2716 /* Yow: call from a static member function. */
2717 decl = build_dummy_object (current_class_type);
2720 /* Put back explicit template arguments, if any. */
2722 function = template_id;
2723 return build_method_call (decl, function, params,
2724 NULL_TREE, LOOKUP_NORMAL);
2726 else if (TREE_CODE (function) == COMPONENT_REF
2727 && type == unknown_type_node)
2729 /* Undo what we did in build_component_ref. */
2730 decl = TREE_OPERAND (function, 0);
2731 function = TREE_OPERAND (function, 1);
2732 function = DECL_NAME (OVL_CURRENT (function));
2736 TREE_OPERAND (template_id, 0) = function;
2737 function = template_id;
2740 return build_method_call (decl, function, params,
2741 NULL_TREE, LOOKUP_NORMAL);
2743 else if (really_overloaded_fn (function))
2745 if (OVL_FUNCTION (function) == NULL_TREE)
2747 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2748 TREE_PURPOSE (function));
2749 return error_mark_node;
2753 /* Put back explicit template arguments, if any. */
2755 function = template_id;
2756 return build_new_function_call (function, params);
2760 /* Remove a potential OVERLOAD around it */
2761 function = OVL_CURRENT (function);
2764 if (TREE_CODE (function) == OFFSET_REF)
2766 /* If the component is a data element (or a virtual function), we play
2767 games here to make things work. */
2770 if (TREE_OPERAND (function, 0))
2771 decl = TREE_OPERAND (function, 0);
2773 decl = current_class_ref;
2775 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2777 /* Sigh. OFFSET_REFs are being used for too many things.
2778 They're being used both for -> and ->*, and we want to resolve
2779 the -> cases here, but leave the ->*. We could use
2780 resolve_offset_ref for those, too, but it would call
2781 get_member_function_from_ptrfunc and decl_addr wouldn't get
2782 updated properly. Nasty. */
2783 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2784 function = resolve_offset_ref (function);
2786 function = TREE_OPERAND (function, 1);
2788 function = get_member_function_from_ptrfunc (&decl_addr, function);
2789 params = tree_cons (NULL_TREE, decl_addr, params);
2790 return build_function_call (function, params);
2793 type = TREE_TYPE (function);
2794 if (type != error_mark_node)
2796 if (TREE_CODE (type) == REFERENCE_TYPE)
2797 type = TREE_TYPE (type);
2799 if (IS_AGGR_TYPE (type))
2800 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2805 tree fntype = TREE_TYPE (function);
2806 tree ctypeptr = NULL_TREE;
2808 /* Explicitly named method? */
2809 if (TREE_CODE (function) == FUNCTION_DECL)
2810 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2811 /* Expression with ptr-to-method type? It could either be a plain
2812 usage, or it might be a case where the ptr-to-method is being
2813 passed in as an argument. */
2814 else if (TYPE_PTRMEMFUNC_P (fntype))
2816 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2817 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2818 ctypeptr = build_pointer_type (rec);
2820 /* Unexpected node type? */
2822 my_friendly_abort (116);
2823 if (decl == NULL_TREE)
2825 if (current_function_decl
2826 && DECL_STATIC_FUNCTION_P (current_function_decl))
2827 error ("invalid call to member function needing `this' in static member function scope");
2829 error ("pointer to member function called, but not in class scope");
2830 return error_mark_node;
2832 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2833 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2835 decl = build_unary_op (ADDR_EXPR, decl, 0);
2836 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2839 decl = build_c_cast (ctypeptr, decl);
2840 params = tree_cons (NULL_TREE, decl, params);
2843 return build_function_call (function, params);
2846 /* Resolve a pointer to member function. INSTANCE is the object
2847 instance to use, if the member points to a virtual member. */
2850 get_member_function_from_ptrfunc (instance_ptrptr, function)
2851 tree *instance_ptrptr;
2854 if (TREE_CODE (function) == OFFSET_REF)
2856 function = TREE_OPERAND (function, 1);
2859 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2861 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2862 tree instance, basetype;
2864 tree instance_ptr = *instance_ptrptr;
2866 if (instance_ptr == error_mark_node
2867 && TREE_CODE (function) == PTRMEM_CST)
2869 /* Extracting the function address from a pmf is only
2870 allowed with -Wno-pmf-conversions. It only works for
2872 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2873 e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
2877 if (TREE_SIDE_EFFECTS (instance_ptr))
2878 instance_ptr = save_expr (instance_ptr);
2880 if (TREE_SIDE_EFFECTS (function))
2881 function = save_expr (function);
2883 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2884 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2886 /* Convert down to the right base, before using the instance. */
2887 instance = convert_pointer_to_real (basetype, instance_ptr);
2888 if (instance == error_mark_node && instance_ptr != error_mark_node)
2891 e3 = PFN_FROM_PTRMEMFUNC (function);
2893 vtbl = convert_pointer_to (ptr_type_node, instance);
2894 delta = cp_convert (ptrdiff_type_node,
2895 build_component_ref (function, delta_identifier,
2898 /* This used to avoid checking for virtual functions if basetype
2899 has no virtual functions, according to an earlier ANSI draft.
2900 With the final ISO C++ rules, such an optimization is
2901 incorrect: A pointer to a derived member can be static_cast
2902 to pointer-to-base-member, as long as the dynamic object
2903 later has the right member. */
2905 /* Promoting idx before saving it improves performance on RISC
2906 targets. Without promoting, the first compare used
2907 load-with-sign-extend, while the second used normal load then
2908 shift to sign-extend. An optimizer flaw, perhaps, but it's
2909 easier to make this change. */
2910 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2912 case ptrmemfunc_vbit_in_pfn:
2913 idx = cp_build_binary_op (TRUNC_DIV_EXPR,
2914 build1 (NOP_EXPR, vtable_index_type, e3),
2915 TYPE_SIZE_UNIT (vtable_entry_type));
2916 e1 = cp_build_binary_op (BIT_AND_EXPR,
2917 build1 (NOP_EXPR, vtable_index_type, e3),
2921 case ptrmemfunc_vbit_in_delta:
2922 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2923 e1 = cp_build_binary_op (BIT_AND_EXPR,
2924 delta, integer_one_node);
2925 delta = cp_build_binary_op (RSHIFT_EXPR,
2926 build1 (NOP_EXPR, vtable_index_type,
2935 /* DELTA2 is the amount by which to adjust the `this' pointer
2936 to find the vtbl. */
2940 build_pointer_type (build_pointer_type (vtable_entry_type)),
2941 vtbl, cp_convert (ptrdiff_type_node, delta2));
2942 vtbl = build_indirect_ref (vtbl, NULL);
2943 aref = build_array_ref (vtbl, idx);
2945 if (! flag_vtable_thunks)
2947 aref = save_expr (aref);
2949 delta = cp_build_binary_op
2951 build_conditional_expr (e1,
2952 build_component_ref (aref,
2959 if (flag_vtable_thunks)
2962 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2963 TREE_TYPE (e2) = TREE_TYPE (e3);
2964 e1 = build_conditional_expr (e1, e2, e3);
2966 /* Make sure this doesn't get evaluated first inside one of the
2967 branches of the COND_EXPR. */
2968 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2969 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2972 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2973 instance_ptr, delta);
2975 if (instance_ptr == error_mark_node
2976 && TREE_CODE (e1) != ADDR_EXPR
2977 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2978 cp_error ("object missing in `%E'", function);
2986 build_function_call_real (function, params, require_complete, flags)
2987 tree function, params;
2988 int require_complete, flags;
2990 register tree fntype, fndecl;
2991 register tree value_type;
2992 register tree coerced_params;
2994 tree name = NULL_TREE, assembler_name = NULL_TREE;
2997 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2998 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2999 if (TREE_CODE (function) == NOP_EXPR
3000 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3001 function = TREE_OPERAND (function, 0);
3003 if (TREE_CODE (function) == FUNCTION_DECL)
3005 name = DECL_NAME (function);
3006 assembler_name = DECL_ASSEMBLER_NAME (function);
3008 GNU_xref_call (current_function_decl,
3009 IDENTIFIER_POINTER (name ? name
3010 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
3012 mark_used (function);
3015 /* Convert anything with function type to a pointer-to-function. */
3016 if (pedantic && DECL_MAIN_P (function))
3017 pedwarn ("ISO C++ forbids calling `::main' from within program");
3019 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
3020 (because calling an inline function does not mean the function
3021 needs to be separately compiled). */
3023 if (DECL_INLINE (function))
3024 function = inline_conversion (function);
3026 function = build_addr_func (function);
3032 function = build_addr_func (function);
3035 if (function == error_mark_node)
3036 return error_mark_node;
3038 fntype = TREE_TYPE (function);
3040 if (TYPE_PTRMEMFUNC_P (fntype))
3042 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3044 return error_mark_node;
3047 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3048 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3050 if (!((TREE_CODE (fntype) == POINTER_TYPE
3051 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3053 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3055 cp_error ("`%E' cannot be used as a function", function);
3056 return error_mark_node;
3059 /* fntype now gets the type of function pointed to. */
3060 fntype = TREE_TYPE (fntype);
3062 /* Convert the parameters to the types declared in the
3063 function prototype, or apply default promotions. */
3065 if (flags & LOOKUP_COMPLAIN)
3066 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3067 params, fndecl, LOOKUP_NORMAL);
3069 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3072 if (coerced_params == error_mark_node)
3074 if (flags & LOOKUP_SPECULATIVELY)
3077 return error_mark_node;
3080 /* Check for errors in format strings. */
3082 if (warn_format && (name || assembler_name))
3083 check_function_format (NULL, name, assembler_name, coerced_params);
3085 /* Recognize certain built-in functions so we can make tree-codes
3086 other than CALL_EXPR. We do this when it enables fold-const.c
3087 to do something useful. */
3089 if (TREE_CODE (function) == ADDR_EXPR
3090 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3091 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3093 result = expand_tree_builtin (TREE_OPERAND (function, 0),
3094 params, coerced_params);
3099 /* Some built-in function calls will be evaluated at
3100 compile-time in fold (). */
3101 result = fold (build_call (function, coerced_params));
3102 value_type = TREE_TYPE (result);
3104 if (require_complete)
3106 if (TREE_CODE (value_type) == VOID_TYPE)
3108 result = require_complete_type (result);
3110 if (IS_AGGR_TYPE (value_type))
3111 result = build_cplus_new (value_type, result);
3112 return convert_from_reference (result);
3116 build_function_call (function, params)
3117 tree function, params;
3119 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3122 /* Convert the actual parameter expressions in the list VALUES
3123 to the types in the list TYPELIST.
3124 If parmdecls is exhausted, or when an element has NULL as its type,
3125 perform the default conversions.
3127 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3129 This is also where warnings about wrong number of args are generated.
3131 Return a list of expressions for the parameters as converted.
3133 Both VALUES and the returned value are chains of TREE_LIST nodes
3134 with the elements of the list in the TREE_VALUE slots of those nodes.
3136 In C++, unspecified trailing parameters can be filled in with their
3137 default arguments, if such were specified. Do so here. */
3140 convert_arguments (typelist, values, fndecl, flags)
3141 tree typelist, values, fndecl;
3144 register tree typetail, valtail;
3145 register tree result = NULL_TREE;
3146 const char *called_thing = 0;
3149 /* Argument passing is always copy-initialization. */
3150 flags |= LOOKUP_ONLYCONVERTING;
3154 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3156 if (DECL_NAME (fndecl) == NULL_TREE
3157 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3158 called_thing = "constructor";
3160 called_thing = "member function";
3163 called_thing = "function";
3166 for (valtail = values, typetail = typelist;
3168 valtail = TREE_CHAIN (valtail), i++)
3170 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3171 register tree val = TREE_VALUE (valtail);
3173 if (val == error_mark_node)
3174 return error_mark_node;
3176 if (type == void_type_node)
3180 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3182 error ("at this point in file");
3185 error ("too many arguments to function");
3186 /* In case anybody wants to know if this argument
3189 TREE_TYPE (tree_last (result)) = error_mark_node;
3193 if (TREE_CODE (val) == OFFSET_REF)
3194 val = resolve_offset_ref (val);
3196 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3197 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3198 if (TREE_CODE (val) == NOP_EXPR
3199 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3200 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3201 val = TREE_OPERAND (val, 0);
3203 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3205 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3206 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3207 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3208 val = default_conversion (val);
3211 if (val == error_mark_node)
3212 return error_mark_node;
3216 /* Formal parm type is specified by a function prototype. */
3219 if (!COMPLETE_TYPE_P (complete_type (type)))
3221 error ("parameter type of called function is incomplete");
3226 parmval = convert_for_initialization
3227 (NULL_TREE, type, val, flags,
3228 "argument passing", fndecl, i);
3229 if (PROMOTE_PROTOTYPES
3230 && INTEGRAL_TYPE_P (type)
3231 && (TYPE_PRECISION (type)
3232 < TYPE_PRECISION (integer_type_node)))
3233 parmval = default_conversion (parmval);
3236 if (parmval == error_mark_node)
3237 return error_mark_node;
3239 result = tree_cons (NULL_TREE, parmval, result);
3243 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3244 val = convert_from_reference (val);
3246 if (fndecl && DECL_BUILT_IN (fndecl)
3247 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3248 /* Don't do ellipsis conversion for __built_in_constant_p
3249 as this will result in spurious warnings for non-POD
3251 val = require_complete_type (val);
3253 val = convert_arg_to_ellipsis (val);
3255 result = tree_cons (NULL_TREE, val, result);
3259 typetail = TREE_CHAIN (typetail);
3262 if (typetail != 0 && typetail != void_list_node)
3264 /* See if there are default arguments that can be used */
3265 if (TREE_PURPOSE (typetail))
3267 for (; typetail != void_list_node; ++i)
3270 = convert_default_arg (TREE_VALUE (typetail),
3271 TREE_PURPOSE (typetail),
3274 if (parmval == error_mark_node)
3275 return error_mark_node;
3277 result = tree_cons (0, parmval, result);
3278 typetail = TREE_CHAIN (typetail);
3279 /* ends with `...'. */
3280 if (typetail == NULL_TREE)
3288 cp_error_at ("too few arguments to %s `%+#D'",
3289 called_thing, fndecl);
3290 error ("at this point in file");
3293 error ("too few arguments to function");
3294 return error_mark_list;
3298 return nreverse (result);
3301 /* Build a binary-operation expression, after performing default
3302 conversions on the operands. CODE is the kind of expression to build. */
3305 build_x_binary_op (code, arg1, arg2)
3306 enum tree_code code;
3309 if (processing_template_decl)
3310 return build_min_nt (code, arg1, arg2);
3312 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3315 /* Build a binary-operation expression without default conversions.
3316 CODE is the kind of expression to build.
3317 This function differs from `build' in several ways:
3318 the data type of the result is computed and recorded in it,
3319 warnings are generated if arg data types are invalid,
3320 special handling for addition and subtraction of pointers is known,
3321 and some optimization is done (operations on narrow ints
3322 are done in the narrower type when that gives the same result).
3323 Constant folding is also done before the result is returned.
3325 Note that the operands will never have enumeral types
3326 because either they have just had the default conversions performed
3327 or they have both just been converted to some other type in which
3328 the arithmetic is to be done.
3330 C++: must do special pointer arithmetic when implementing
3331 multiple inheritance, and deal with pointer to member functions. */
3334 build_binary_op (code, orig_op0, orig_op1, convert_p)
3335 enum tree_code code;
3336 tree orig_op0, orig_op1;
3337 int convert_p ATTRIBUTE_UNUSED;
3340 register enum tree_code code0, code1;
3343 /* Expression code to give to the expression when it is built.
3344 Normally this is CODE, which is what the caller asked for,
3345 but in some special cases we change it. */
3346 register enum tree_code resultcode = code;
3348 /* Data type in which the computation is to be performed.
3349 In the simplest cases this is the common type of the arguments. */
3350 register tree result_type = NULL;
3352 /* Nonzero means operands have already been type-converted
3353 in whatever way is necessary.
3354 Zero means they need to be converted to RESULT_TYPE. */
3357 /* Nonzero means create the expression with this type, rather than
3359 tree build_type = 0;
3361 /* Nonzero means after finally constructing the expression
3362 convert it to this type. */
3363 tree final_type = 0;
3365 /* Nonzero if this is an operation like MIN or MAX which can
3366 safely be computed in short if both args are promoted shorts.
3367 Also implies COMMON.
3368 -1 indicates a bitwise operation; this makes a difference
3369 in the exact conditions for when it is safe to do the operation
3370 in a narrower mode. */
3373 /* Nonzero if this is a comparison operation;
3374 if both args are promoted shorts, compare the original shorts.
3375 Also implies COMMON. */
3376 int short_compare = 0;
3378 /* Nonzero if this is a right-shift operation, which can be computed on the
3379 original short and then promoted if the operand is a promoted short. */
3380 int short_shift = 0;
3382 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3385 /* Apply default conversions. */
3386 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3387 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3388 || code == TRUTH_XOR_EXPR)
3390 op0 = decay_conversion (orig_op0);
3391 op1 = decay_conversion (orig_op1);
3395 op0 = default_conversion (orig_op0);
3396 op1 = default_conversion (orig_op1);
3399 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3400 STRIP_TYPE_NOPS (op0);
3401 STRIP_TYPE_NOPS (op1);
3403 /* DTRT if one side is an overloaded function, but complain about it. */
3404 if (type_unknown_p (op0))
3406 tree t = instantiate_type (TREE_TYPE (op1), op0, itf_none);
3407 if (t != error_mark_node)
3409 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3414 if (type_unknown_p (op1))
3416 tree t = instantiate_type (TREE_TYPE (op0), op1, itf_none);
3417 if (t != error_mark_node)
3419 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3425 type0 = TREE_TYPE (op0);
3426 type1 = TREE_TYPE (op1);
3428 /* The expression codes of the data types of the arguments tell us
3429 whether the arguments are integers, floating, pointers, etc. */
3430 code0 = TREE_CODE (type0);
3431 code1 = TREE_CODE (type1);
3433 /* If an error was already reported for one of the arguments,
3434 avoid reporting another error. */
3436 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3437 return error_mark_node;
3442 /* Handle the pointer + int case. */
3443 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3444 return pointer_int_sum (PLUS_EXPR, op0, op1);
3445 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3446 return pointer_int_sum (PLUS_EXPR, op1, op0);
3452 /* Subtraction of two similar pointers.
3453 We must subtract them as integers, then divide by object size. */
3454 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3455 && comp_target_types (type0, type1, 1))
3456 return pointer_diff (op0, op1, common_type (type0, type1));
3457 /* Handle pointer minus int. Just like pointer plus int. */
3458 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3459 return pointer_int_sum (MINUS_EXPR, op0, op1);
3468 case TRUNC_DIV_EXPR:
3470 case FLOOR_DIV_EXPR:
3471 case ROUND_DIV_EXPR:
3472 case EXACT_DIV_EXPR:
3473 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3474 || code0 == COMPLEX_TYPE)
3475 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3476 || code1 == COMPLEX_TYPE))
3478 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3479 cp_warning ("division by zero in `%E / 0'", op0);
3480 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3481 cp_warning ("division by zero in `%E / 0.'", op0);
3483 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3484 resultcode = RDIV_EXPR;
3486 /* When dividing two signed integers, we have to promote to int.
3487 unless we divide by a constant != -1. Note that default
3488 conversion will have been performed on the operands at this
3489 point, so we have to dig out the original type to find out if
3491 shorten = ((TREE_CODE (op0) == NOP_EXPR
3492 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3493 || (TREE_CODE (op1) == INTEGER_CST
3494 && ! integer_all_onesp (op1)));
3501 case BIT_ANDTC_EXPR:
3504 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3506 /* If one operand is a constant, and the other is a short type
3507 that has been converted to an int,
3508 really do the work in the short type and then convert the
3509 result to int. If we are lucky, the constant will be 0 or 1
3510 in the short type, making the entire operation go away. */
3511 if (TREE_CODE (op0) == INTEGER_CST
3512 && TREE_CODE (op1) == NOP_EXPR
3513 && (TYPE_PRECISION (type1)
3514 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3515 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3517 final_type = result_type;
3518 op1 = TREE_OPERAND (op1, 0);
3519 result_type = TREE_TYPE (op1);
3521 if (TREE_CODE (op1) == INTEGER_CST
3522 && TREE_CODE (op0) == NOP_EXPR
3523 && (TYPE_PRECISION (type0)
3524 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3525 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3527 final_type = result_type;
3528 op0 = TREE_OPERAND (op0, 0);
3529 result_type = TREE_TYPE (op0);
3533 case TRUNC_MOD_EXPR:
3534 case FLOOR_MOD_EXPR:
3535 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3536 cp_warning ("division by zero in `%E %% 0'", op0);
3537 else if (code1 == REAL_TYPE && real_zerop (op1))
3538 cp_warning ("division by zero in `%E %% 0.'", op0);
3540 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3542 /* Although it would be tempting to shorten always here, that loses
3543 on some targets, since the modulo instruction is undefined if the
3544 quotient can't be represented in the computation mode. We shorten
3545 only if unsigned or if dividing by something we know != -1. */
3546 shorten = ((TREE_CODE (op0) == NOP_EXPR
3547 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3548 || (TREE_CODE (op1) == INTEGER_CST
3549 && ! integer_all_onesp (op1)));
3554 case TRUTH_ANDIF_EXPR:
3555 case TRUTH_ORIF_EXPR:
3556 case TRUTH_AND_EXPR:
3558 result_type = boolean_type_node;
3561 /* Shift operations: result has same type as first operand;
3562 always convert second operand to int.
3563 Also set SHORT_SHIFT if shifting rightward. */
3566 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3568 result_type = type0;
3569 if (TREE_CODE (op1) == INTEGER_CST)
3571 if (tree_int_cst_lt (op1, integer_zero_node))
3572 warning ("right shift count is negative");
3575 if (! integer_zerop (op1))
3577 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3578 warning ("right shift count >= width of type");
3581 /* Convert the shift-count to an integer, regardless of
3582 size of value being shifted. */
3583 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3584 op1 = cp_convert (integer_type_node, op1);
3585 /* Avoid converting op1 to result_type later. */
3591 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3593 result_type = type0;
3594 if (TREE_CODE (op1) == INTEGER_CST)
3596 if (tree_int_cst_lt (op1, integer_zero_node))
3597 warning ("left shift count is negative");
3598 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3599 warning ("left shift count >= width of type");
3601 /* Convert the shift-count to an integer, regardless of
3602 size of value being shifted. */
3603 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3604 op1 = cp_convert (integer_type_node, op1);
3605 /* Avoid converting op1 to result_type later. */
3612 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3614 result_type = type0;
3615 if (TREE_CODE (op1) == INTEGER_CST)
3617 if (tree_int_cst_lt (op1, integer_zero_node))
3618 warning ("%s rotate count is negative",
3619 (code == LROTATE_EXPR) ? "left" : "right");
3620 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3621 warning ("%s rotate count >= width of type",
3622 (code == LROTATE_EXPR) ? "left" : "right");
3624 /* Convert the shift-count to an integer, regardless of
3625 size of value being shifted. */
3626 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3627 op1 = cp_convert (integer_type_node, op1);
3633 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3634 warning ("comparing floating point with == or != is unsafe");
3636 build_type = boolean_type_node;
3637 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3638 || code0 == COMPLEX_TYPE)
3639 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3640 || code1 == COMPLEX_TYPE))
3642 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3643 result_type = composite_pointer_type (type0, type1, op0, op1,
3645 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3646 result_type = type0;
3647 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3648 result_type = type1;
3649 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3651 result_type = type0;
3652 error ("ISO C++ forbids comparison between pointer and integer");
3654 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3656 result_type = type1;
3657 error ("ISO C++ forbids comparison between pointer and integer");
3659 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3661 op0 = build_component_ref (op0, pfn_identifier, NULL_TREE, 0);
3662 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3663 result_type = TREE_TYPE (op0);
3665 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3666 return cp_build_binary_op (code, op1, op0);
3667 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3668 && same_type_p (type0, type1))
3670 /* E will be the final comparison. */
3672 /* E1 and E2 are for scratch. */
3680 if (TREE_SIDE_EFFECTS (op0))
3681 op0 = save_expr (op0);
3682 if (TREE_SIDE_EFFECTS (op1))
3683 op1 = save_expr (op1);
3688 && (!op0.pfn || op0.delta == op1.delta))
3690 The reason for the `!op0.pfn' bit is that a NULL
3691 pointer-to-member is any member with a zero PFN; the
3692 DELTA field is unspecified. */
3693 pfn0 = pfn_from_ptrmemfunc (op0);
3694 pfn1 = pfn_from_ptrmemfunc (op1);
3695 delta0 = build_component_ref (op0, delta_identifier,
3697 delta1 = build_component_ref (op1, delta_identifier,
3699 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3700 e2 = cp_build_binary_op (EQ_EXPR,
3702 cp_convert (TREE_TYPE (pfn0),
3703 integer_zero_node));
3704 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3705 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3706 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3707 if (code == EQ_EXPR)
3709 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3711 else if ((TYPE_PTRMEMFUNC_P (type0)
3712 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3713 || (TYPE_PTRMEMFUNC_P (type1)
3714 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3715 my_friendly_abort (20000221);
3720 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3721 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3723 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3724 result_type = composite_pointer_type (type0, type1, op0, op1,
3732 build_type = boolean_type_node;
3733 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3734 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3736 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3737 result_type = composite_pointer_type (type0, type1, op0, op1,
3739 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3740 && integer_zerop (op1))
3741 result_type = type0;
3742 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3743 && integer_zerop (op0))
3744 result_type = type1;
3745 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3747 result_type = type0;
3748 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3750 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3752 result_type = type1;
3753 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3757 case UNORDERED_EXPR:
3764 build_type = integer_type_node;
3765 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3767 error ("unordered comparison on non-floating point argument");
3768 return error_mark_node;
3777 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3779 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3781 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3783 if (shorten || common || short_compare)
3784 result_type = common_type (type0, type1);
3786 /* For certain operations (which identify themselves by shorten != 0)
3787 if both args were extended from the same smaller type,
3788 do the arithmetic in that type and then extend.
3790 shorten !=0 and !=1 indicates a bitwise operation.
3791 For them, this optimization is safe only if
3792 both args are zero-extended or both are sign-extended.
3793 Otherwise, we might change the result.
3794 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3795 but calculated in (unsigned short) it would be (unsigned short)-1. */
3797 if (shorten && none_complex)
3799 int unsigned0, unsigned1;
3800 tree arg0 = get_narrower (op0, &unsigned0);
3801 tree arg1 = get_narrower (op1, &unsigned1);
3802 /* UNS is 1 if the operation to be done is an unsigned one. */
3803 int uns = TREE_UNSIGNED (result_type);
3806 final_type = result_type;
3808 /* Handle the case that OP0 does not *contain* a conversion
3809 but it *requires* conversion to FINAL_TYPE. */
3811 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3812 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3813 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3814 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3816 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3818 /* For bitwise operations, signedness of nominal type
3819 does not matter. Consider only how operands were extended. */
3823 /* Note that in all three cases below we refrain from optimizing
3824 an unsigned operation on sign-extended args.
3825 That would not be valid. */
3827 /* Both args variable: if both extended in same way
3828 from same width, do it in that width.
3829 Do it unsigned if args were zero-extended. */
3830 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3831 < TYPE_PRECISION (result_type))
3832 && (TYPE_PRECISION (TREE_TYPE (arg1))
3833 == TYPE_PRECISION (TREE_TYPE (arg0)))
3834 && unsigned0 == unsigned1
3835 && (unsigned0 || !uns))
3837 = signed_or_unsigned_type (unsigned0,
3838 common_type (TREE_TYPE (arg0),
3840 else if (TREE_CODE (arg0) == INTEGER_CST
3841 && (unsigned1 || !uns)
3842 && (TYPE_PRECISION (TREE_TYPE (arg1))
3843 < TYPE_PRECISION (result_type))
3844 && (type = signed_or_unsigned_type (unsigned1,
3846 int_fits_type_p (arg0, type)))
3848 else if (TREE_CODE (arg1) == INTEGER_CST
3849 && (unsigned0 || !uns)
3850 && (TYPE_PRECISION (TREE_TYPE (arg0))
3851 < TYPE_PRECISION (result_type))
3852 && (type = signed_or_unsigned_type (unsigned0,
3854 int_fits_type_p (arg1, type)))
3858 /* Shifts can be shortened if shifting right. */
3863 tree arg0 = get_narrower (op0, &unsigned_arg);
3865 final_type = result_type;
3867 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3868 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3870 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3871 /* We can shorten only if the shift count is less than the
3872 number of bits in the smaller type size. */
3873 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3874 /* If arg is sign-extended and then unsigned-shifted,
3875 we can simulate this with a signed shift in arg's type
3876 only if the extended result is at least twice as wide
3877 as the arg. Otherwise, the shift could use up all the
3878 ones made by sign-extension and bring in zeros.
3879 We can't optimize that case at all, but in most machines
3880 it never happens because available widths are 2**N. */
3881 && (!TREE_UNSIGNED (final_type)
3883 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3884 <= TYPE_PRECISION (result_type))))
3886 /* Do an unsigned shift if the operand was zero-extended. */
3888 = signed_or_unsigned_type (unsigned_arg,
3890 /* Convert value-to-be-shifted to that type. */
3891 if (TREE_TYPE (op0) != result_type)
3892 op0 = cp_convert (result_type, op0);
3897 /* Comparison operations are shortened too but differently.
3898 They identify themselves by setting short_compare = 1. */
3902 /* Don't write &op0, etc., because that would prevent op0
3903 from being kept in a register.
3904 Instead, make copies of the our local variables and
3905 pass the copies by reference, then copy them back afterward. */
3906 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3907 enum tree_code xresultcode = resultcode;
3909 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3911 return cp_convert (boolean_type_node, val);
3912 op0 = xop0, op1 = xop1;
3914 resultcode = xresultcode;
3917 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3918 && warn_sign_compare)
3920 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3921 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3923 int unsignedp0, unsignedp1;
3924 tree primop0 = get_narrower (op0, &unsignedp0);
3925 tree primop1 = get_narrower (op1, &unsignedp1);
3927 /* Check for comparison of different enum types. */
3928 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3929 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3930 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3931 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3933 cp_warning ("comparison between types `%#T' and `%#T'",
3934 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3937 /* Give warnings for comparisons between signed and unsigned
3938 quantities that may fail. */
3939 /* Do the checking based on the original operand trees, so that
3940 casts will be considered, but default promotions won't be. */
3942 /* Do not warn if the comparison is being done in a signed type,
3943 since the signed type will only be chosen if it can represent
3944 all the values of the unsigned type. */
3945 if (! TREE_UNSIGNED (result_type))
3947 /* Do not warn if both operands are unsigned. */
3948 else if (op0_signed == op1_signed)
3950 /* Do not warn if the signed quantity is an unsuffixed
3951 integer literal (or some static constant expression
3952 involving such literals or a conditional expression
3953 involving such literals) and it is non-negative. */
3954 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3955 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3957 /* Do not warn if the comparison is an equality operation,
3958 the unsigned quantity is an integral constant and it does
3959 not use the most significant bit of result_type. */
3960 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3961 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3962 && int_fits_type_p (orig_op1,
3963 signed_type (result_type)))
3964 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3965 && int_fits_type_p (orig_op0,
3966 signed_type (result_type)))))
3969 warning ("comparison between signed and unsigned integer expressions");
3971 /* Warn if two unsigned values are being compared in a size
3972 larger than their original size, and one (and only one) is the
3973 result of a `~' operator. This comparison will always fail.
3975 Also warn if one operand is a constant, and the constant does not
3976 have all bits set that are set in the ~ operand when it is
3979 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3980 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3982 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3983 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3984 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3985 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3987 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3990 HOST_WIDE_INT constant, mask;
3994 if (host_integerp (primop0, 0))
3997 unsignedp = unsignedp1;
3998 constant = tree_low_cst (primop0, 0);
4003 unsignedp = unsignedp0;
4004 constant = tree_low_cst (primop1, 0);
4007 bits = TYPE_PRECISION (TREE_TYPE (primop));
4008 if (bits < TYPE_PRECISION (result_type)
4009 && bits < HOST_BITS_PER_LONG && unsignedp)
4011 mask = (~ (HOST_WIDE_INT) 0) << bits;
4012 if ((mask & constant) != mask)
4013 warning ("comparison of promoted ~unsigned with constant");
4016 else if (unsignedp0 && unsignedp1
4017 && (TYPE_PRECISION (TREE_TYPE (primop0))
4018 < TYPE_PRECISION (result_type))
4019 && (TYPE_PRECISION (TREE_TYPE (primop1))
4020 < TYPE_PRECISION (result_type)))
4021 warning ("comparison of promoted ~unsigned with unsigned");
4026 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4027 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4028 Then the expression will be built.
4029 It will be given type FINAL_TYPE if that is nonzero;
4030 otherwise, it will be given type RESULT_TYPE. */
4034 cp_error ("invalid operands of types `%T' and `%T' to binary `%O'",
4035 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4036 return error_mark_node;
4039 /* Issue warnings about peculiar, but legal, uses of NULL. */
4040 if (/* It's reasonable to use pointer values as operands of &&
4041 and ||, so NULL is no exception. */
4042 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4043 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4044 (orig_op0 == null_node
4045 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4046 /* Or vice versa. */
4047 || (orig_op1 == null_node
4048 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4049 /* Or, both are NULL and the operation was not a comparison. */
4050 || (orig_op0 == null_node && orig_op1 == null_node
4051 && code != EQ_EXPR && code != NE_EXPR)))
4052 /* Some sort of arithmetic operation involving NULL was
4053 performed. Note that pointer-difference and pointer-addition
4054 have already been handled above, and so we don't end up here in
4056 cp_warning ("NULL used in arithmetic");
4060 if (TREE_TYPE (op0) != result_type)
4061 op0 = cp_convert (result_type, op0);
4062 if (TREE_TYPE (op1) != result_type)
4063 op1 = cp_convert (result_type, op1);
4065 if (op0 == error_mark_node || op1 == error_mark_node)
4066 return error_mark_node;
4069 if (build_type == NULL_TREE)
4070 build_type = result_type;
4073 register tree result = build (resultcode, build_type, op0, op1);
4074 register tree folded;
4076 folded = fold (result);
4077 if (folded == result)
4078 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4079 if (final_type != 0)
4080 return cp_convert (final_type, folded);
4085 /* Return a tree for the sum or difference (RESULTCODE says which)
4086 of pointer PTROP and integer INTOP. */
4089 pointer_int_sum (resultcode, ptrop, intop)
4090 enum tree_code resultcode;
4091 register tree ptrop, intop;
4095 register tree result;
4096 register tree folded = fold (intop);
4098 /* The result is a pointer of the same type that is being added. */
4100 register tree result_type = TREE_TYPE (ptrop);
4102 if (!complete_type_or_else (result_type, ptrop))
4103 return error_mark_node;
4105 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4107 if (pedantic || warn_pointer_arith)
4108 pedwarn ("ISO C++ forbids using pointer of type `void *' in pointer arithmetic");
4109 size_exp = integer_one_node;
4111 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4113 if (pedantic || warn_pointer_arith)
4114 pedwarn ("ISO C++ forbids using a pointer-to-function in pointer arithmetic");
4115 size_exp = integer_one_node;
4117 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4119 if (pedantic || warn_pointer_arith)
4120 pedwarn ("ISO C++ forbids using a pointer to member function in pointer arithmetic");
4121 size_exp = integer_one_node;
4123 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4125 if (pedantic || warn_pointer_arith)
4126 pedwarn ("ISO C++ forbids using pointer to a member in pointer arithmetic");
4127 size_exp = integer_one_node;
4130 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4132 /* Needed to make OOPS V2R3 work. */
4134 if (integer_zerop (intop))
4137 /* If what we are about to multiply by the size of the elements
4138 contains a constant term, apply distributive law
4139 and multiply that constant term separately.
4140 This helps produce common subexpressions. */
4142 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4143 && ! TREE_CONSTANT (intop)
4144 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4145 && TREE_CONSTANT (size_exp))
4147 enum tree_code subcode = resultcode;
4148 if (TREE_CODE (intop) == MINUS_EXPR)
4149 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4150 ptrop = cp_build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4151 intop = TREE_OPERAND (intop, 0);
4154 /* Convert the integer argument to a type the same size as sizetype
4155 so the multiply won't overflow spuriously. */
4157 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4158 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4160 /* Replace the integer argument with a suitable product by the object size.
4161 Do this multiplication as signed, then convert to the appropriate
4162 pointer type (actually unsigned integral). */
4164 intop = cp_convert (result_type,
4165 cp_build_binary_op (MULT_EXPR, intop,
4166 cp_convert (TREE_TYPE (intop),
4169 /* Create the sum or difference. */
4171 result = build (resultcode, result_type, ptrop, intop);
4173 folded = fold (result);
4174 if (folded == result)
4175 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4179 /* Return a tree for the difference of pointers OP0 and OP1.
4180 The resulting tree has type int. */
4183 pointer_diff (op0, op1, ptrtype)
4184 register tree op0, op1;
4185 register tree ptrtype;
4187 register tree result, folded;
4188 tree restype = ptrdiff_type_node;
4189 tree target_type = TREE_TYPE (ptrtype);
4191 if (!complete_type_or_else (target_type, NULL_TREE))
4192 return error_mark_node;
4194 if (pedantic || warn_pointer_arith)
4196 if (TREE_CODE (target_type) == VOID_TYPE)
4197 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4198 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4199 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4200 if (TREE_CODE (target_type) == METHOD_TYPE)
4201 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4202 if (TREE_CODE (target_type) == OFFSET_TYPE)
4203 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4206 /* First do the subtraction as integers;
4207 then drop through to build the divide operator. */
4209 op0 = cp_build_binary_op (MINUS_EXPR,
4210 cp_convert (restype, op0),
4211 cp_convert (restype, op1));
4213 /* This generates an error if op1 is a pointer to an incomplete type. */
4214 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4215 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4217 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4218 || TREE_CODE (target_type) == FUNCTION_TYPE
4219 || TREE_CODE (target_type) == METHOD_TYPE
4220 || TREE_CODE (target_type) == OFFSET_TYPE)
4222 : size_in_bytes (target_type));
4224 /* Do the division. */
4226 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4228 folded = fold (result);
4229 if (folded == result)
4230 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4234 /* Handle the case of taking the address of a COMPONENT_REF.
4235 Called by `build_unary_op'.
4237 ARG is the COMPONENT_REF whose address we want.
4238 ARGTYPE is the pointer type that this address should have. */
4241 build_component_addr (arg, argtype)
4244 tree field = TREE_OPERAND (arg, 1);
4245 tree basetype = decl_type_context (field);
4246 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4248 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4250 if (DECL_C_BIT_FIELD (field))
4252 cp_error ("attempt to take address of bit-field structure member `%D'",
4254 return error_mark_node;
4257 if (TREE_CODE (field) == FIELD_DECL
4258 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
4260 /* Can't convert directly to ARGTYPE, since that
4261 may have the same pointer type as one of our
4263 rval = build1 (NOP_EXPR, argtype,
4264 convert_pointer_to (basetype, rval));
4265 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4268 /* This conversion is harmless. */
4269 rval = convert_force (argtype, rval, 0);
4271 return fold (build (PLUS_EXPR, argtype, rval,
4272 cp_convert (argtype, byte_position (field))));
4275 /* Construct and perhaps optimize a tree representation
4276 for a unary operation. CODE, a tree_code, specifies the operation
4277 and XARG is the operand. */
4280 build_x_unary_op (code, xarg)
4281 enum tree_code code;
4287 if (processing_template_decl)
4288 return build_min_nt (code, xarg, NULL_TREE);
4290 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4292 if (code == ADDR_EXPR
4293 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4294 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4295 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
4296 || (TREE_CODE (xarg) == OFFSET_REF)))
4297 /* don't look for a function */;
4302 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4303 NULL_TREE, NULL_TREE);
4304 if (rval || code != ADDR_EXPR)
4307 if (code == ADDR_EXPR)
4309 if (TREE_CODE (xarg) == OFFSET_REF)
4311 ptrmem = PTRMEM_OK_P (xarg);
4313 if (!ptrmem && !flag_ms_extensions
4314 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4315 /* A single non-static member, make sure we don't allow a
4316 pointer-to-member. */
4317 xarg = ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE);
4319 else if (TREE_CODE (xarg) == TARGET_EXPR)
4320 warning ("taking address of temporary");
4322 exp = build_unary_op (code, xarg, 0);
4323 if (TREE_CODE (exp) == ADDR_EXPR)
4324 PTRMEM_OK_P (exp) = ptrmem;
4329 /* Like truthvalue_conversion, but handle pointer-to-member constants, where
4330 a null value is represented by an INTEGER_CST of -1. */
4333 cp_truthvalue_conversion (expr)
4336 tree type = TREE_TYPE (expr);
4337 if (TYPE_PTRMEM_P (type))
4338 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4340 return truthvalue_conversion (expr);
4343 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4346 condition_conversion (expr)
4350 if (processing_template_decl)
4352 t = perform_implicit_conversion (boolean_type_node, expr);
4353 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4357 /* C++: Must handle pointers to members.
4359 Perhaps type instantiation should be extended to handle conversion
4360 from aggregates to types we don't yet know we want? (Or are those
4361 cases typically errors which should be reported?)
4363 NOCONVERT nonzero suppresses the default promotions
4364 (such as from short to int). */
4367 build_unary_op (code, xarg, noconvert)
4368 enum tree_code code;
4372 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4373 register tree arg = xarg;
4374 register tree argtype = 0;
4375 const char *errstring = NULL;
4378 if (arg == error_mark_node)
4379 return error_mark_node;
4384 /* This is used for unary plus, because a CONVERT_EXPR
4385 is enough to prevent anybody from looking inside for
4386 associativity, but won't generate any code. */
4387 if (!(arg = build_expr_type_conversion
4388 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4389 errstring = "wrong type argument to unary plus";
4393 arg = default_conversion (arg);
4394 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4395 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4400 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4401 errstring = "wrong type argument to unary minus";
4402 else if (!noconvert)
4403 arg = default_conversion (arg);
4407 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4411 arg = default_conversion (arg);
4413 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4415 errstring = "wrong type argument to bit-complement";
4416 else if (!noconvert)
4417 arg = default_conversion (arg);
4421 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4422 errstring = "wrong type argument to abs";
4423 else if (!noconvert)
4424 arg = default_conversion (arg);
4428 /* Conjugating a real value is a no-op, but allow it anyway. */
4429 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4430 errstring = "wrong type argument to conjugation";
4431 else if (!noconvert)
4432 arg = default_conversion (arg);
4435 case TRUTH_NOT_EXPR:
4436 arg = cp_convert (boolean_type_node, arg);
4437 val = invert_truthvalue (arg);
4438 if (arg != error_mark_node)
4440 errstring = "in argument to unary !";
4447 if (TREE_CODE (arg) == COMPLEX_CST)
4448 return TREE_REALPART (arg);
4449 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4450 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4455 if (TREE_CODE (arg) == COMPLEX_CST)
4456 return TREE_IMAGPART (arg);
4457 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4458 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4460 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4462 case PREINCREMENT_EXPR:
4463 case POSTINCREMENT_EXPR:
4464 case PREDECREMENT_EXPR:
4465 case POSTDECREMENT_EXPR:
4466 /* Handle complex lvalues (when permitted)
4467 by reduction to simpler cases. */
4469 val = unary_complex_lvalue (code, arg);
4473 /* Increment or decrement the real part of the value,
4474 and don't change the imaginary part. */
4475 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4479 arg = stabilize_reference (arg);
4480 real = build_unary_op (REALPART_EXPR, arg, 1);
4481 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4482 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4483 build_unary_op (code, real, 1), imag);
4486 /* Report invalid types. */
4488 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4491 if (code == PREINCREMENT_EXPR)
4492 errstring ="no pre-increment operator for type";
4493 else if (code == POSTINCREMENT_EXPR)
4494 errstring ="no post-increment operator for type";
4495 else if (code == PREDECREMENT_EXPR)
4496 errstring ="no pre-decrement operator for type";
4498 errstring ="no post-decrement operator for type";
4502 /* Report something read-only. */
4504 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4505 || TREE_READONLY (arg))
4506 readonly_error (arg, ((code == PREINCREMENT_EXPR
4507 || code == POSTINCREMENT_EXPR)
4508 ? "increment" : "decrement"),
4513 tree result_type = TREE_TYPE (arg);
4515 arg = get_unwidened (arg, 0);
4516 argtype = TREE_TYPE (arg);
4518 /* ARM $5.2.5 last annotation says this should be forbidden. */
4519 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4520 pedwarn ("ISO C++ forbids %sing an enum",
4521 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4522 ? "increment" : "decrement");
4524 /* Compute the increment. */
4526 if (TREE_CODE (argtype) == POINTER_TYPE)
4528 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4529 tree type = complete_type (TREE_TYPE (argtype));
4531 if (!COMPLETE_OR_VOID_TYPE_P (type))
4532 cp_error ("cannot %s a pointer to incomplete type `%T'",
4533 ((code == PREINCREMENT_EXPR
4534 || code == POSTINCREMENT_EXPR)
4535 ? "increment" : "decrement"), TREE_TYPE (argtype));
4536 else if ((pedantic || warn_pointer_arith)
4537 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4538 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4539 cp_pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4540 ((code == PREINCREMENT_EXPR
4541 || code == POSTINCREMENT_EXPR)
4542 ? "increment" : "decrement"), argtype);
4543 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4546 inc = integer_one_node;
4548 inc = cp_convert (argtype, inc);
4550 /* Handle incrementing a cast-expression. */
4552 switch (TREE_CODE (arg))
4557 case FIX_TRUNC_EXPR:
4558 case FIX_FLOOR_EXPR:
4559 case FIX_ROUND_EXPR:
4562 tree incremented, modify, value, compound;
4563 if (! lvalue_p (arg) && pedantic)
4564 pedwarn ("cast to non-reference type used as lvalue");
4565 arg = stabilize_reference (arg);
4566 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4569 value = save_expr (arg);
4570 incremented = build (((code == PREINCREMENT_EXPR
4571 || code == POSTINCREMENT_EXPR)
4572 ? PLUS_EXPR : MINUS_EXPR),
4573 argtype, value, inc);
4575 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4576 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4578 /* Eliminate warning about unused result of + or -. */
4579 TREE_NO_UNUSED_WARNING (compound) = 1;
4587 /* Complain about anything else that is not a true lvalue. */
4588 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4589 || code == POSTINCREMENT_EXPR)
4590 ? "increment" : "decrement")))
4591 return error_mark_node;
4593 /* Forbid using -- on `bool'. */
4594 if (TREE_TYPE (arg) == boolean_type_node)
4596 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4598 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4599 return error_mark_node;
4602 /* This will only work if someone can convince Kenner to accept
4603 my patch to expand_increment. (jason) */
4604 val = build (code, TREE_TYPE (arg), arg, inc);
4606 val = boolean_increment (code, arg);
4610 val = build (code, TREE_TYPE (arg), arg, inc);
4612 TREE_SIDE_EFFECTS (val) = 1;
4613 return cp_convert (result_type, val);
4617 /* Note that this operation never does default_conversion
4618 regardless of NOCONVERT. */
4620 argtype = lvalue_type (arg);
4621 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4625 build_pointer_type (TREE_TYPE (argtype)), arg);
4626 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4629 else if (pedantic && DECL_MAIN_P (arg))
4631 pedwarn ("ISO C++ forbids taking address of function `::main'");
4633 /* Let &* cancel out to simplify resulting code. */
4634 if (TREE_CODE (arg) == INDIRECT_REF)
4636 /* We don't need to have `current_class_ptr' wrapped in a
4637 NON_LVALUE_EXPR node. */
4638 if (arg == current_class_ref)
4639 return current_class_ptr;
4641 arg = TREE_OPERAND (arg, 0);
4642 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4646 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4647 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4649 else if (lvalue_p (arg))
4650 /* Don't let this be an lvalue. */
4651 return non_lvalue (arg);
4655 /* For &x[y], return x+y */
4656 if (TREE_CODE (arg) == ARRAY_REF)
4658 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4659 return error_mark_node;
4660 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4661 TREE_OPERAND (arg, 1));
4664 /* Uninstantiated types are all functions. Taking the
4665 address of a function is a no-op, so just return the
4668 if (TREE_CODE (arg) == IDENTIFIER_NODE
4669 && IDENTIFIER_OPNAME_P (arg))
4671 my_friendly_abort (117);
4672 /* We don't know the type yet, so just work around the problem.
4673 We know that this will resolve to an lvalue. */
4674 return build1 (ADDR_EXPR, unknown_type_node, arg);
4677 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4678 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4680 /* They're trying to take the address of a unique non-static
4681 member function. This is ill-formed (except in MS-land),
4682 but let's try to DTRT.
4683 Note: We only handle unique functions here because we don't
4684 want to complain if there's a static overload; non-unique
4685 cases will be handled by instantiate_type. But we need to
4686 handle this case here to allow casts on the resulting PMF.
4687 We could defer this in non-MS mode, but it's easier to give
4688 a useful error here. */
4690 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4691 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4693 if (! flag_ms_extensions)
4695 if (current_class_type
4696 && TREE_OPERAND (arg, 0) == current_class_ref)
4697 /* An expression like &memfn. */
4698 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);
4700 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);
4702 arg = build_offset_ref (base, name);
4705 if (type_unknown_p (arg))
4706 return build1 (ADDR_EXPR, unknown_type_node, arg);
4708 /* Handle complex lvalues (when permitted)
4709 by reduction to simpler cases. */
4710 val = unary_complex_lvalue (code, arg);
4714 switch (TREE_CODE (arg))
4719 case FIX_TRUNC_EXPR:
4720 case FIX_FLOOR_EXPR:
4721 case FIX_ROUND_EXPR:
4723 if (! lvalue_p (arg) && pedantic)
4724 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4731 /* Allow the address of a constructor if all the elements
4733 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4734 && TREE_CONSTANT (arg))
4736 /* Anything not already handled and not a true memory reference
4738 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4739 && TREE_CODE (argtype) != METHOD_TYPE
4740 && !lvalue_or_else (arg, "unary `&'"))
4741 return error_mark_node;
4743 if (argtype != error_mark_node)
4744 argtype = build_pointer_type (argtype);
4746 if (mark_addressable (arg) == 0)
4747 return error_mark_node;
4752 if (TREE_CODE (arg) == COMPONENT_REF)
4753 addr = build_component_addr (arg, argtype);
4755 addr = build1 (ADDR_EXPR, argtype, arg);
4757 /* Address of a static or external variable or
4758 function counts as a constant */
4760 TREE_CONSTANT (addr) = 1;
4762 if (TREE_CODE (argtype) == POINTER_TYPE
4763 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4765 build_ptrmemfunc_type (argtype);
4766 addr = build_ptrmemfunc (argtype, addr, 0);
4779 argtype = TREE_TYPE (arg);
4780 return fold (build1 (code, argtype, arg));
4783 error ("%s", errstring);
4784 return error_mark_node;
4787 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4788 for certain kinds of expressions which are not really lvalues
4789 but which we can accept as lvalues.
4791 If ARG is not a kind of expression we can handle, return zero. */
4794 unary_complex_lvalue (code, arg)
4795 enum tree_code code;
4798 /* Handle (a, b) used as an "lvalue". */
4799 if (TREE_CODE (arg) == COMPOUND_EXPR)
4801 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4802 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4803 TREE_OPERAND (arg, 0), real_result);
4806 /* Handle (a ? b : c) used as an "lvalue". */
4807 if (TREE_CODE (arg) == COND_EXPR
4808 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4809 return rationalize_conditional_expr (code, arg);
4811 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4812 if (TREE_CODE (arg) == MODIFY_EXPR
4813 || TREE_CODE (arg) == PREINCREMENT_EXPR
4814 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4816 tree lvalue = TREE_OPERAND (arg, 0);
4817 if (TREE_SIDE_EFFECTS (lvalue))
4819 lvalue = stabilize_reference (lvalue);
4820 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4821 lvalue, TREE_OPERAND (arg, 1));
4823 return unary_complex_lvalue
4824 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4827 if (code != ADDR_EXPR)
4830 /* Handle (a = b) used as an "lvalue" for `&'. */
4831 if (TREE_CODE (arg) == MODIFY_EXPR
4832 || TREE_CODE (arg) == INIT_EXPR)
4834 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4835 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4836 TREE_NO_UNUSED_WARNING (arg) = 1;
4840 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4841 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4842 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4844 /* The representation of something of type OFFSET_TYPE
4845 is really the representation of a pointer to it.
4846 Here give the representation its true type. */
4849 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4851 if (TREE_CODE (arg) != OFFSET_REF)
4854 t = TREE_OPERAND (arg, 1);
4856 /* Check all this code for right semantics. */
4857 if (TREE_CODE (t) == FUNCTION_DECL)
4859 if (DECL_DESTRUCTOR_P (t))
4860 cp_error ("taking address of destructor");
4861 return build_unary_op (ADDR_EXPR, t, 0);
4863 if (TREE_CODE (t) == VAR_DECL)
4864 return build_unary_op (ADDR_EXPR, t, 0);
4869 if (TREE_OPERAND (arg, 0)
4870 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4871 && TREE_CODE (t) != FIELD_DECL)
4873 cp_error ("taking address of bound pointer-to-member expression");
4874 return error_mark_node;
4877 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4878 type = build_pointer_type (type);
4880 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4886 /* We permit compiler to make function calls returning
4887 objects of aggregate type look like lvalues. */
4891 if (TREE_CODE (targ) == SAVE_EXPR)
4892 targ = TREE_OPERAND (targ, 0);
4894 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4896 if (TREE_CODE (arg) == SAVE_EXPR)
4899 targ = build_cplus_new (TREE_TYPE (arg), arg);
4900 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4903 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4904 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4905 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4908 /* Don't let anything else be handled specially. */
4912 /* Mark EXP saying that we need to be able to take the
4913 address of it; it should not be allocated in a register.
4914 Value is 1 if successful.
4916 C++: we do not allow `current_class_ptr' to be addressable. */
4919 mark_addressable (exp)
4922 register tree x = exp;
4924 if (TREE_ADDRESSABLE (x) == 1)
4928 switch (TREE_CODE (x))
4935 x = TREE_OPERAND (x, 0);
4939 if (x == current_class_ptr)
4941 error ("cannot take the address of `this', which is an rvalue expression");
4942 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4946 /* Caller should not be trying to mark initialized
4947 constant fields addressable. */
4948 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4949 || DECL_IN_AGGR_P (x) == 0
4951 || DECL_EXTERNAL (x), 314);
4955 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4956 && !DECL_ARTIFICIAL (x) && extra_warnings)
4957 cp_warning ("address requested for `%D', which is declared `register'",
4959 TREE_ADDRESSABLE (x) = 1;
4963 TREE_ADDRESSABLE (x) = 1;
4964 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4968 TREE_ADDRESSABLE (x) = 1;
4972 TREE_ADDRESSABLE (x) = 1;
4973 mark_addressable (TREE_OPERAND (x, 0));
4981 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4984 build_x_conditional_expr (ifexp, op1, op2)
4985 tree ifexp, op1, op2;
4987 if (processing_template_decl)
4988 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4990 return build_conditional_expr (ifexp, op1, op2);
4993 /* Handle overloading of the ',' operator when needed. Otherwise,
4994 this function just builds an expression list. */
4997 build_x_compound_expr (list)
5000 tree rest = TREE_CHAIN (list);
5003 if (processing_template_decl)
5004 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5006 if (rest == NULL_TREE)
5007 return build_compound_expr (list);
5009 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5010 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5012 return build_x_compound_expr (tree_cons (NULL_TREE, result,
5013 TREE_CHAIN (rest)));
5015 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5017 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5018 /* the left-hand operand of a comma expression is like an expression
5019 statement: we should warn if it doesn't have any side-effects,
5020 unless it was explicitly cast to (void). */
5021 if ((extra_warnings || warn_unused_value)
5022 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5023 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
5024 warning("left-hand operand of comma expression has no effect");
5026 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5027 else if (warn_unused_value)
5028 warn_if_unused_value (TREE_VALUE(list));
5031 return build_compound_expr
5032 (tree_cons (NULL_TREE, TREE_VALUE (list),
5033 build_tree_list (NULL_TREE,
5034 build_x_compound_expr (rest))));
5037 /* Given a list of expressions, return a compound expression
5038 that performs them all and returns the value of the last of them. */
5041 build_compound_expr (list)
5047 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5049 if (TREE_CHAIN (list) == 0)
5051 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5052 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5053 if (TREE_CODE (list) == NOP_EXPR
5054 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5055 list = TREE_OPERAND (list, 0);
5057 return TREE_VALUE (list);
5060 first = TREE_VALUE (list);
5061 first = convert_to_void (first, "left-hand operand of comma");
5062 if (first == error_mark_node)
5063 return error_mark_node;
5065 rest = build_compound_expr (TREE_CHAIN (list));
5066 if (rest == error_mark_node)
5067 return error_mark_node;
5069 /* When pedantic, a compound expression cannot be a constant expression. */
5070 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5073 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
5077 build_static_cast (type, expr)
5083 if (type == error_mark_node || expr == error_mark_node)
5084 return error_mark_node;
5086 if (TREE_CODE (expr) == OFFSET_REF)
5087 expr = resolve_offset_ref (expr);
5089 if (processing_template_decl)
5091 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5095 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5096 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5097 if (TREE_CODE (type) != REFERENCE_TYPE
5098 && TREE_CODE (expr) == NOP_EXPR
5099 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5100 expr = TREE_OPERAND (expr, 0);
5102 if (TREE_CODE (type) == VOID_TYPE)
5104 expr = convert_to_void (expr, /*implicit=*/NULL);
5108 if (TREE_CODE (type) == REFERENCE_TYPE)
5109 return (convert_from_reference
5110 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5111 LOOKUP_COMPLAIN, NULL_TREE)));
5113 if (IS_AGGR_TYPE (type))
5114 return build_cplus_new (type, (build_method_call
5115 (NULL_TREE, complete_ctor_identifier,
5116 build_tree_list (NULL_TREE, expr),
5117 TYPE_BINFO (type), LOOKUP_NORMAL)));
5119 expr = decay_conversion (expr);
5120 intype = TREE_TYPE (expr);
5122 /* FIXME handle casting to array type. */
5125 if (IS_AGGR_TYPE (intype)
5126 ? can_convert_arg (type, intype, expr)
5127 : can_convert_arg (strip_all_pointer_quals (type),
5128 strip_all_pointer_quals (intype), expr))
5129 /* This is a standard conversion. */
5131 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5133 /* They're pointers to objects. They must be aggregates that
5134 are related non-virtually. */
5138 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5139 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5140 && !binfo_from_vbase (binfo))
5143 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5144 && TREE_CODE (type) != ARRAY_TYPE
5145 && TREE_CODE (type) != FUNCTION_TYPE
5146 && can_convert (intype, strip_all_pointer_quals (type)))
5148 else if (TREE_CODE (intype) == ENUMERAL_TYPE
5149 && TREE_CODE (type) == ENUMERAL_TYPE)
5150 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
5151 converted to an enumeration type."
5152 The integral to enumeration will be accepted by the previous clause.
5153 We need to explicitly check for enumeration to enumeration. */
5156 /* [expr.static.cast]
5158 The static_cast operator shall not be used to cast away
5160 if (ok && casts_away_constness (intype, type))
5162 cp_error ("static_cast from type `%T' to type `%T' casts away constness",
5164 return error_mark_node;
5168 return build_c_cast (type, expr);
5170 cp_error ("invalid static_cast from type `%T' to type `%T'", intype, type);
5171 return error_mark_node;
5175 build_reinterpret_cast (type, expr)
5180 if (type == error_mark_node || expr == error_mark_node)
5181 return error_mark_node;
5183 if (TREE_CODE (expr) == OFFSET_REF)
5184 expr = resolve_offset_ref (expr);
5186 if (processing_template_decl)
5188 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5192 if (TREE_CODE (type) != REFERENCE_TYPE)
5194 expr = decay_conversion (expr);
5196 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5197 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5198 if (TREE_CODE (expr) == NOP_EXPR
5199 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5200 expr = TREE_OPERAND (expr, 0);
5203 intype = TREE_TYPE (expr);
5205 if (TREE_CODE (type) == REFERENCE_TYPE)
5207 if (! real_lvalue_p (expr))
5209 cp_error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
5210 return error_mark_node;
5212 expr = build_unary_op (ADDR_EXPR, expr, 0);
5213 if (expr != error_mark_node)
5214 expr = build_reinterpret_cast
5215 (build_pointer_type (TREE_TYPE (type)), expr);
5216 if (expr != error_mark_node)
5217 expr = build_indirect_ref (expr, 0);
5220 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5221 return build_static_cast (type, expr);
5223 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5224 || TREE_CODE (intype) == ENUMERAL_TYPE))
5226 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5228 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5229 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5232 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5233 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5235 expr = decl_constant_value (expr);
5236 return fold (build1 (NOP_EXPR, type, expr));
5238 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5239 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5241 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5242 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5245 expr = decl_constant_value (expr);
5246 return fold (build1 (NOP_EXPR, type, expr));
5248 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5249 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5251 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5252 expr = decl_constant_value (expr);
5253 return fold (build1 (NOP_EXPR, type, expr));
5257 cp_error ("invalid reinterpret_cast from type `%T' to type `%T'",
5259 return error_mark_node;
5262 return cp_convert (type, expr);
5266 build_const_cast (type, expr)
5271 if (type == error_mark_node || expr == error_mark_node)
5272 return error_mark_node;
5274 if (TREE_CODE (expr) == OFFSET_REF)
5275 expr = resolve_offset_ref (expr);
5277 if (processing_template_decl)
5279 tree t = build_min (CONST_CAST_EXPR, type, expr);
5283 if (!POINTER_TYPE_P (type))
5284 cp_error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5285 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5287 cp_error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5288 return error_mark_node;
5291 if (TREE_CODE (type) != REFERENCE_TYPE)
5293 expr = decay_conversion (expr);
5295 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5296 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5297 if (TREE_CODE (expr) == NOP_EXPR
5298 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5299 expr = TREE_OPERAND (expr, 0);
5302 intype = TREE_TYPE (expr);
5304 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5305 return build_static_cast (type, expr);
5306 else if (TREE_CODE (type) == REFERENCE_TYPE)
5308 if (! real_lvalue_p (expr))
5310 cp_error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5311 return error_mark_node;
5314 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5316 expr = build_unary_op (ADDR_EXPR, expr, 0);
5317 expr = build1 (NOP_EXPR, type, expr);
5318 return convert_from_reference (expr);
5321 else if (TREE_CODE (type) == POINTER_TYPE
5322 && TREE_CODE (intype) == POINTER_TYPE
5323 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5324 return cp_convert (type, expr);
5326 cp_error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5327 return error_mark_node;
5330 /* Build an expression representing a cast to type TYPE of expression EXPR.
5332 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5333 when doing the cast. */
5336 build_c_cast (type, expr)
5339 register tree value = expr;
5342 if (type == error_mark_node || expr == error_mark_node)
5343 return error_mark_node;
5345 if (processing_template_decl)
5347 tree t = build_min (CAST_EXPR, type,
5348 tree_cons (NULL_TREE, value, NULL_TREE));
5352 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5353 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5354 if (TREE_CODE (type) != REFERENCE_TYPE
5355 && TREE_CODE (value) == NOP_EXPR
5356 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5357 value = TREE_OPERAND (value, 0);
5359 if (TREE_CODE (value) == OFFSET_REF)
5360 value = resolve_offset_ref (value);
5362 if (TREE_CODE (type) == ARRAY_TYPE)
5364 /* Allow casting from T1* to T2[] because Cfront allows it.
5365 NIHCL uses it. It is not valid ISO C++ however. */
5366 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5368 cp_pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5369 type = build_pointer_type (TREE_TYPE (type));
5373 cp_error ("ISO C++ forbids casting to an array type `%T'", type);
5374 return error_mark_node;
5378 if (TREE_CODE (type) == FUNCTION_TYPE
5379 || TREE_CODE (type) == METHOD_TYPE)
5381 cp_error ("invalid cast to function type `%T'", type);
5382 return error_mark_node;
5385 if (TREE_CODE (type) == VOID_TYPE)
5387 /* Conversion to void does not cause any of the normal function to
5388 * pointer, array to pointer and lvalue to rvalue decays. */
5390 value = convert_to_void (value, /*implicit=*/NULL);
5393 /* Convert functions and arrays to pointers and
5394 convert references to their expanded types,
5395 but don't convert any other types. If, however, we are
5396 casting to a class type, there's no reason to do this: the
5397 cast will only succeed if there is a converting constructor,
5398 and the default conversions will be done at that point. In
5399 fact, doing the default conversion here is actually harmful
5403 struct S { S(const A&); };
5405 since we don't want the array-to-pointer conversion done. */
5406 if (!IS_AGGR_TYPE (type))
5408 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5409 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5410 /* Don't do the default conversion on a ->* expression. */
5411 && ! (TREE_CODE (type) == POINTER_TYPE
5412 && bound_pmf_p (value)))
5413 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5414 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5415 value = default_conversion (value);
5417 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5418 /* However, even for class types, we still need to strip away
5419 the reference type, since the call to convert_force below
5420 does not expect the input expression to be of reference
5422 value = convert_from_reference (value);
5424 otype = TREE_TYPE (value);
5426 /* Optionally warn about potentially worrisome casts. */
5429 && TREE_CODE (type) == POINTER_TYPE
5430 && TREE_CODE (otype) == POINTER_TYPE
5431 && !at_least_as_qualified_p (TREE_TYPE (type),
5433 cp_warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5436 if (TREE_CODE (type) == INTEGER_TYPE
5437 && TREE_CODE (otype) == POINTER_TYPE
5438 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5439 warning ("cast from pointer to integer of different size");
5441 if (TREE_CODE (type) == POINTER_TYPE
5442 && TREE_CODE (otype) == INTEGER_TYPE
5443 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5444 /* Don't warn about converting any constant. */
5445 && !TREE_CONSTANT (value))
5446 warning ("cast to pointer from integer of different size");
5448 if (TREE_CODE (type) == REFERENCE_TYPE)
5449 value = (convert_from_reference
5450 (convert_to_reference (type, value, CONV_C_CAST,
5451 LOOKUP_COMPLAIN, NULL_TREE)));
5456 value = decl_constant_value (value);
5459 value = convert_force (type, value, CONV_C_CAST);
5461 /* Ignore any integer overflow caused by the cast. */
5462 if (TREE_CODE (value) == INTEGER_CST)
5464 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5465 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5469 /* Warn about possible alignment problems. Do this here when we will have
5470 instantiated any necessary template types. */
5471 if (STRICT_ALIGNMENT && warn_cast_align
5472 && TREE_CODE (type) == POINTER_TYPE
5473 && TREE_CODE (otype) == POINTER_TYPE
5474 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5475 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5476 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5477 && COMPLETE_TYPE_P (TREE_TYPE (type))
5478 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5479 cp_warning ("cast from `%T' to `%T' increases required alignment of target type",
5482 /* Always produce some operator for an explicit cast,
5483 so we can tell (for -pedantic) that the cast is no lvalue. */
5484 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5485 && real_lvalue_p (value))
5486 value = non_lvalue (value);
5491 /* Build an assignment expression of lvalue LHS from value RHS.
5492 MODIFYCODE is the code for a binary operator that we use
5493 to combine the old value of LHS with RHS to get the new value.
5494 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5496 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5499 build_modify_expr (lhs, modifycode, rhs)
5501 enum tree_code modifycode;
5504 register tree result;
5506 tree lhstype = TREE_TYPE (lhs);
5507 tree olhstype = lhstype;
5510 /* Avoid duplicate error messages from operands that had errors. */
5511 if (lhs == error_mark_node || rhs == error_mark_node)
5512 return error_mark_node;
5514 /* Types that aren't fully specified cannot be used in assignments. */
5515 lhs = require_complete_type (lhs);
5519 /* Handle control structure constructs used as "lvalues". */
5521 switch (TREE_CODE (lhs))
5523 /* Handle --foo = 5; as these are valid constructs in C++ */
5524 case PREDECREMENT_EXPR:
5525 case PREINCREMENT_EXPR:
5526 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5527 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5528 stabilize_reference (TREE_OPERAND (lhs, 0)),
5529 TREE_OPERAND (lhs, 1));
5530 return build (COMPOUND_EXPR, lhstype,
5532 build_modify_expr (TREE_OPERAND (lhs, 0),
5535 /* Handle (a, b) used as an "lvalue". */
5537 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5539 if (newrhs == error_mark_node)
5540 return error_mark_node;
5541 return build (COMPOUND_EXPR, lhstype,
5542 TREE_OPERAND (lhs, 0), newrhs);
5545 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5546 if (newrhs == error_mark_node)
5547 return error_mark_node;
5548 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5550 /* Handle (a ? b : c) used as an "lvalue". */
5552 rhs = save_expr (rhs);
5554 /* Produce (a ? (b = rhs) : (c = rhs))
5555 except that the RHS goes through a save-expr
5556 so the code to compute it is only emitted once. */
5559 /* Check this here to avoid odd errors when trying to convert
5560 a throw to the type of the COND_EXPR. */
5561 if (!lvalue_or_else (lhs, "assignment"))
5562 return error_mark_node;
5564 cond = build_conditional_expr
5565 (TREE_OPERAND (lhs, 0),
5566 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5567 TREE_OPERAND (lhs, 1)),
5569 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5570 TREE_OPERAND (lhs, 2)),
5573 if (cond == error_mark_node)
5575 /* Make sure the code to compute the rhs comes out
5576 before the split. */
5577 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5578 /* Case to void to suppress warning
5579 from warn_if_unused_value. */
5580 cp_convert (void_type_node, rhs), cond);
5587 if (TREE_CODE (lhs) == OFFSET_REF)
5589 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5591 /* Static class member? */
5592 tree member = TREE_OPERAND (lhs, 1);
5593 if (TREE_CODE (member) == VAR_DECL)
5597 compiler_error ("invalid static class member");
5598 return error_mark_node;
5602 lhs = resolve_offset_ref (lhs);
5604 olhstype = lhstype = TREE_TYPE (lhs);
5607 if (lhs == error_mark_node)
5610 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5611 && modifycode != INIT_EXPR)
5613 lhs = convert_from_reference (lhs);
5614 olhstype = lhstype = TREE_TYPE (lhs);
5617 /* If a binary op has been requested, combine the old LHS value with the RHS
5618 producing the value we should actually store into the LHS. */
5620 if (modifycode == INIT_EXPR)
5622 if (TREE_CODE (rhs) == CONSTRUCTOR)
5624 if (! same_type_p (TREE_TYPE (rhs), lhstype))
5626 result = build (INIT_EXPR, lhstype, lhs, rhs);
5627 TREE_SIDE_EFFECTS (result) = 1;
5630 else if (! IS_AGGR_TYPE (lhstype))
5631 /* Do the default thing */;
5634 result = build_method_call (lhs, complete_ctor_identifier,
5635 build_tree_list (NULL_TREE, rhs),
5636 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5637 if (result == NULL_TREE)
5638 return error_mark_node;
5642 else if (modifycode == NOP_EXPR)
5644 /* `operator=' is not an inheritable operator. */
5645 if (! IS_AGGR_TYPE (lhstype))
5646 /* Do the default thing */;
5649 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5650 lhs, rhs, make_node (NOP_EXPR));
5651 if (result == NULL_TREE)
5652 return error_mark_node;
5657 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5659 my_friendly_abort (978652);
5663 lhs = stabilize_reference (lhs);
5664 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5665 if (newrhs == error_mark_node)
5667 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5668 TREE_TYPE (lhs), TREE_TYPE (rhs));
5669 return error_mark_node;
5673 /* Handle a cast used as an "lvalue".
5674 We have already performed any binary operator using the value as cast.
5675 Now convert the result to the cast type of the lhs,
5676 and then true type of the lhs and store it there;
5677 then convert result back to the cast type to be the value
5678 of the assignment. */
5680 switch (TREE_CODE (lhs))
5685 case FIX_TRUNC_EXPR:
5686 case FIX_FLOOR_EXPR:
5687 case FIX_ROUND_EXPR:
5689 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5690 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5691 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5692 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5693 newrhs = default_conversion (newrhs);
5695 tree inner_lhs = TREE_OPERAND (lhs, 0);
5698 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5699 type, otherwise the result is an rvalue. */
5700 if (! lvalue_p (lhs))
5701 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5703 result = build_modify_expr (inner_lhs, NOP_EXPR,
5704 cp_convert (TREE_TYPE (inner_lhs),
5705 cp_convert (lhstype, newrhs)));
5706 if (result == error_mark_node)
5708 return cp_convert (TREE_TYPE (lhs), result);
5715 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5716 Reject anything strange now. */
5718 if (!lvalue_or_else (lhs, "assignment"))
5719 return error_mark_node;
5721 GNU_xref_assign (lhs);
5723 /* Warn about storing in something that is `const'. */
5724 /* For C++, don't warn if this is initialization. */
5725 if (modifycode != INIT_EXPR
5726 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5727 /* Functions are not modifiable, even though they are
5729 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5730 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5731 && C_TYPE_FIELDS_READONLY (lhstype))
5732 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5733 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5734 readonly_error (lhs, "assignment", 0);
5736 /* If storing into a structure or union member,
5737 it has probably been given type `int'.
5738 Compute the type that would go with
5739 the actual amount of storage the member occupies. */
5741 if (TREE_CODE (lhs) == COMPONENT_REF
5742 && (TREE_CODE (lhstype) == INTEGER_TYPE
5743 || TREE_CODE (lhstype) == REAL_TYPE
5744 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5746 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5748 /* If storing in a field that is in actuality a short or narrower
5749 than one, we must store in the field in its actual type. */
5751 if (lhstype != TREE_TYPE (lhs))
5753 lhs = copy_node (lhs);
5754 TREE_TYPE (lhs) = lhstype;
5758 if (modifycode != INIT_EXPR)
5760 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5761 modifycode = NOP_EXPR;
5762 /* Reference-bashing */
5763 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5765 tree tmp = convert_from_reference (lhs);
5766 lhstype = TREE_TYPE (tmp);
5767 if (!COMPLETE_TYPE_P (lhstype))
5769 incomplete_type_error (lhs, lhstype);
5770 return error_mark_node;
5775 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5777 tree tmp = convert_from_reference (newrhs);
5778 if (!COMPLETE_TYPE_P (TREE_TYPE (tmp)))
5780 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5781 return error_mark_node;
5787 if (TREE_SIDE_EFFECTS (lhs))
5788 lhs = stabilize_reference (lhs);
5789 if (TREE_SIDE_EFFECTS (newrhs))
5790 newrhs = stabilize_reference (newrhs);
5792 /* Convert new value to destination type. */
5794 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5798 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5800 cp_error ("incompatible types in assignment of `%T' to `%T'",
5801 TREE_TYPE (rhs), lhstype);
5802 return error_mark_node;
5805 /* Allow array assignment in compiler-generated code. */
5806 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5807 pedwarn ("ISO C++ forbids assignment of arrays");
5809 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5810 ? 1 + (modifycode != INIT_EXPR): 0;
5811 return build_vec_init (lhs, newrhs, from_array);
5814 if (modifycode == INIT_EXPR)
5816 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5817 "initialization", NULL_TREE, 0);
5818 if (current_function_decl &&
5819 lhs == DECL_RESULT (current_function_decl))
5821 if (DECL_INITIAL (lhs))
5822 warning ("return value from function receives multiple initializations");
5823 DECL_INITIAL (lhs) = newrhs;
5828 /* Avoid warnings on enum bit fields. */
5829 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5830 && TREE_CODE (lhstype) == INTEGER_TYPE)
5832 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5834 newrhs = convert_force (lhstype, newrhs, 0);
5837 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5839 if (TREE_CODE (newrhs) == CALL_EXPR
5840 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5841 newrhs = build_cplus_new (lhstype, newrhs);
5843 /* Can't initialize directly from a TARGET_EXPR, since that would
5844 cause the lhs to be constructed twice, and possibly result in
5845 accidental self-initialization. So we force the TARGET_EXPR to be
5846 expanded without a target. */
5847 if (TREE_CODE (newrhs) == TARGET_EXPR)
5848 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5849 TREE_OPERAND (newrhs, 0));
5852 if (newrhs == error_mark_node)
5853 return error_mark_node;
5855 if (TREE_CODE (newrhs) == COND_EXPR)
5858 tree cond = TREE_OPERAND (newrhs, 0);
5860 if (TREE_SIDE_EFFECTS (lhs))
5861 cond = build_compound_expr (tree_cons
5863 build_tree_list (NULL_TREE, cond)));
5865 /* Cannot have two identical lhs on this one tree (result) as preexpand
5866 calls will rip them out and fill in RTL for them, but when the
5867 rtl is generated, the calls will only be in the first side of the
5868 condition, not on both, or before the conditional jump! (mrs) */
5869 lhs1 = break_out_calls (lhs);
5872 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5873 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5874 lhstype, lhs, newrhs);
5877 tree result_type = TREE_TYPE (newrhs);
5878 /* We have to convert each arm to the proper type because the
5879 types may have been munged by constant folding. */
5881 = build (COND_EXPR, result_type, cond,
5882 build_modify_expr (lhs, modifycode,
5883 cp_convert (result_type,
5884 TREE_OPERAND (newrhs, 1))),
5885 build_modify_expr (lhs1, modifycode,
5886 cp_convert (result_type,
5887 TREE_OPERAND (newrhs, 2))));
5891 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5892 lhstype, lhs, newrhs);
5894 TREE_SIDE_EFFECTS (result) = 1;
5896 /* If we got the LHS in a different type for storing in,
5897 convert the result back to the nominal type of LHS
5898 so that the value we return always has the same type
5899 as the LHS argument. */
5901 if (olhstype == TREE_TYPE (result))
5903 /* Avoid warnings converting integral types back into enums
5904 for enum bit fields. */
5905 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5906 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5908 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5909 TREE_NO_UNUSED_WARNING (result) = 1;
5912 return convert_for_assignment (olhstype, result, "assignment",
5917 build_x_modify_expr (lhs, modifycode, rhs)
5919 enum tree_code modifycode;
5922 if (processing_template_decl)
5923 return build_min_nt (MODOP_EXPR, lhs,
5924 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5926 if (modifycode != NOP_EXPR)
5928 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5929 make_node (modifycode));
5933 return build_modify_expr (lhs, modifycode, rhs);
5937 /* Get difference in deltas for different pointer to member function
5938 types. Return integer_zero_node, if FROM cannot be converted to a
5939 TO type. If FORCE is true, then allow reverse conversions as well.
5941 Note that the naming of FROM and TO is kind of backwards; the return
5942 value is what we add to a TO in order to get a FROM. They are named
5943 this way because we call this function to find out how to convert from
5944 a pointer to member of FROM to a pointer to member of TO. */
5947 get_delta_difference (from, to, force)
5951 tree delta = integer_zero_node;
5958 /* Should get_base_distance here, so we can check if any thing along
5959 the path is virtual, and we need to make sure we stay inside the
5960 real binfos when going through virtual bases. Maybe we should
5961 replace virtual bases with BINFO_FOR_VBASE ... (mrs) */
5962 binfo = get_binfo (from, to, 1);
5963 if (binfo == error_mark_node)
5965 error (" in pointer to member function conversion");
5972 error_not_base_type (from, to);
5973 error (" in pointer to member conversion");
5976 binfo = get_binfo (to, from, 1);
5977 if (binfo == 0 || binfo == error_mark_node)
5979 virt_binfo = binfo_from_vbase (binfo);
5983 /* This is a reinterpret cast, we choose to do nothing. */
5984 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
5985 BINFO_TYPE (virt_binfo),
5986 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5989 delta = BINFO_OFFSET (binfo);
5990 delta = cp_convert (ptrdiff_type_node, delta);
5991 delta = cp_build_binary_op (MINUS_EXPR,
5998 virt_binfo = binfo_from_vbase (binfo);
6001 /* This is a reinterpret cast, we choose to do nothing. */
6003 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
6004 BINFO_TYPE (virt_binfo),
6005 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
6007 cp_error ("pointer to member conversion via virtual base `%T' of `%T'",
6008 BINFO_TYPE (virt_binfo),
6009 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
6012 delta = BINFO_OFFSET (binfo);
6014 return cp_convert (ptrdiff_type_node, delta);
6017 /* Return a constructor for the pointer-to-member-function TYPE using
6018 the other components as specified. */
6021 build_ptrmemfunc1 (type, delta, pfn)
6022 tree type, delta, pfn;
6028 /* Pull the FIELD_DECLs out of the type. */
6029 pfn_field = TYPE_FIELDS (type);
6030 delta_field = TREE_CHAIN (pfn_field);
6032 /* Make sure DELTA has the type we want. */
6033 delta = convert_and_check (delta_type_node, delta);
6035 /* Finish creating the initializer. */
6036 u = tree_cons (pfn_field, pfn,
6037 build_tree_list (delta_field, delta));
6038 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6039 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
6040 TREE_STATIC (u) = (TREE_CONSTANT (u)
6041 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6043 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
6048 /* Build a constructor for a pointer to member function. It can be
6049 used to initialize global variables, local variable, or used
6050 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6053 If FORCE is non-zero, then force this conversion, even if
6054 we would rather not do it. Usually set when using an explicit
6057 Return error_mark_node, if something goes wrong. */
6060 build_ptrmemfunc (type, pfn, force)
6065 tree pfn_type = TREE_TYPE (pfn);
6066 tree to_type = build_ptrmemfunc_type (type);
6068 /* Handle multiple conversions of pointer to member functions. */
6069 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6071 tree delta = NULL_TREE;
6072 tree npfn = NULL_TREE;
6076 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6077 cp_error ("invalid conversion to type `%T' from type `%T'",
6080 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6081 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6084 /* We don't have to do any conversion to convert a
6085 pointer-to-member to its own type. But, we don't want to
6086 just return a PTRMEM_CST if there's an explicit cast; that
6087 cast should make the expression an invalid template argument. */
6088 if (TREE_CODE (pfn) != PTRMEM_CST)
6090 if (same_type_p (to_type, pfn_type))
6092 else if (integer_zerop (n))
6093 return build_reinterpret_cast (to_type, pfn);
6096 if (TREE_SIDE_EFFECTS (pfn))
6097 pfn = save_expr (pfn);
6099 /* Obtain the function pointer and the current DELTA. */
6100 if (TREE_CODE (pfn) == PTRMEM_CST)
6101 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
6104 npfn = build_component_ref (pfn, pfn_identifier, NULL_TREE, 0);
6105 delta = build_component_ref (pfn, delta_identifier, NULL_TREE, 0);
6108 /* Just adjust the DELTA field. */
6109 delta = cp_convert (ptrdiff_type_node, delta);
6110 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
6111 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
6112 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
6113 return build_ptrmemfunc1 (to_type, delta, npfn);
6116 /* Handle null pointer to member function conversions. */
6117 if (integer_zerop (pfn))
6119 pfn = build_c_cast (type, integer_zero_node);
6120 return build_ptrmemfunc1 (to_type,
6125 if (type_unknown_p (pfn))
6126 return instantiate_type (type, pfn, itf_complain);
6128 fn = TREE_OPERAND (pfn, 0);
6129 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6130 return make_ptrmem_cst (to_type, fn);
6133 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6136 ??? There is no consistency as to the types returned for the above
6137 values. Some code acts as if its a sizetype and some as if its
6138 integer_type_node. */
6141 expand_ptrmemfunc_cst (cst, delta, pfn)
6146 tree type = TREE_TYPE (cst);
6147 tree fn = PTRMEM_CST_MEMBER (cst);
6148 tree ptr_class, fn_class;
6150 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6152 /* The class that the function belongs to. */
6153 fn_class = DECL_CONTEXT (fn);
6155 /* The class that we're creating a pointer to member of. */
6156 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6158 /* First, calculate the adjustment to the function's class. */
6159 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6161 if (!DECL_VIRTUAL_P (fn))
6162 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6165 /* If we're dealing with a virtual function, we have to adjust 'this'
6166 again, to point to the base which provides the vtable entry for
6167 fn; the call will do the opposite adjustment. */
6168 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6169 tree binfo = binfo_or_else (orig_class, fn_class);
6170 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6171 *delta, BINFO_OFFSET (binfo)));
6173 /* We set PFN to the vtable offset at which the function can be
6174 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
6175 case delta is shifted left, and then incremented). */
6176 *pfn = DECL_VINDEX (fn);
6177 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
6178 TYPE_SIZE_UNIT (vtable_entry_type)));
6180 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
6182 case ptrmemfunc_vbit_in_pfn:
6183 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
6187 case ptrmemfunc_vbit_in_delta:
6188 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
6189 *delta, integer_one_node));
6190 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6191 *delta, integer_one_node));
6198 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
6203 /* Return an expression for PFN from the pointer-to-member function
6207 pfn_from_ptrmemfunc (t)
6210 if (TREE_CODE (t) == PTRMEM_CST)
6215 expand_ptrmemfunc_cst (t, &delta, &pfn);
6220 return build_component_ref (t, pfn_identifier, NULL_TREE, 0);
6223 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6224 if this is a potentially dangerous thing to do. Returns a possibly
6228 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
6231 const char *errtype;
6235 if (TREE_CODE (type) == REFERENCE_TYPE)
6236 type = TREE_TYPE (type);
6238 /* Issue warnings about peculiar, but legal, uses of NULL. */
6239 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
6242 cp_warning ("passing NULL used for non-pointer %s %P of `%D'",
6243 errtype, parmnum, fndecl);
6245 cp_warning ("%s to non-pointer type `%T' from NULL", errtype, type);
6248 /* Warn about assigning a floating-point type to an integer type. */
6249 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
6250 && TREE_CODE (type) == INTEGER_TYPE)
6253 cp_warning ("passing `%T' for %s %P of `%D'",
6254 TREE_TYPE (expr), errtype, parmnum, fndecl);
6256 cp_warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
6258 /* And warn about assigning a negative value to an unsigned
6260 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
6262 if (TREE_CODE (expr) == INTEGER_CST
6263 && TREE_NEGATED_INT (expr))
6266 cp_warning ("passing negative value `%E' for %s %P of `%D'",
6267 expr, errtype, parmnum, fndecl);
6269 cp_warning ("%s of negative value `%E' to `%T'",
6270 errtype, expr, type);
6273 overflow_warning (expr);
6275 if (TREE_CONSTANT (expr))
6281 /* Convert value RHS to type TYPE as preparation for an assignment to
6282 an lvalue of type TYPE. ERRTYPE is a string to use in error
6283 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6284 are doing the conversion in order to pass the PARMNUMth argument of
6288 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6290 const char *errtype;
6294 register enum tree_code codel = TREE_CODE (type);
6295 register tree rhstype;
6296 register enum tree_code coder;
6298 if (codel == OFFSET_TYPE)
6299 my_friendly_abort (990505);
6301 if (TREE_CODE (rhs) == OFFSET_REF)
6302 rhs = resolve_offset_ref (rhs);
6304 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6305 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6306 rhs = TREE_OPERAND (rhs, 0);
6308 rhstype = TREE_TYPE (rhs);
6309 coder = TREE_CODE (rhstype);
6311 if (rhs == error_mark_node || rhstype == error_mark_node)
6312 return error_mark_node;
6313 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6314 return error_mark_node;
6316 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6318 /* The RHS of an assignment cannot have void type. */
6319 if (coder == VOID_TYPE)
6321 error ("void value not ignored as it ought to be");
6322 return error_mark_node;
6325 /* Simplify the RHS if possible. */
6326 if (TREE_CODE (rhs) == CONST_DECL)
6327 rhs = DECL_INITIAL (rhs);
6328 else if (coder != ARRAY_TYPE)
6329 rhs = decl_constant_value (rhs);
6333 The expression is implicitly converted (clause _conv_) to the
6334 cv-unqualified type of the left operand. */
6335 if (!can_convert_arg (type, rhstype, rhs))
6337 /* When -Wno-pmf-conversions is use, we just silently allow
6338 conversions from pointers-to-members to plain pointers. If
6339 the conversion doesn't work, cp_convert will complain. */
6341 && TYPE_PTR_P (type)
6342 && TYPE_PTRMEMFUNC_P (rhstype))
6343 rhs = cp_convert (strip_top_quals (type), rhs);
6346 /* If the right-hand side has unknown type, then it is an
6347 overloaded function. Call instantiate_type to get error
6349 if (rhstype == unknown_type_node)
6350 instantiate_type (type, rhs, itf_complain);
6352 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6353 rhstype, type, parmnum, fndecl);
6355 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6357 return error_mark_node;
6360 return perform_implicit_conversion (strip_top_quals (type), rhs);
6363 /* Convert RHS to be of type TYPE.
6364 If EXP is non-zero, it is the target of the initialization.
6365 ERRTYPE is a string to use in error messages.
6367 Two major differences between the behavior of
6368 `convert_for_assignment' and `convert_for_initialization'
6369 are that references are bashed in the former, while
6370 copied in the latter, and aggregates are assigned in
6371 the former (operator=) while initialized in the
6374 If using constructor make sure no conversion operator exists, if one does
6375 exist, an ambiguity exists.
6377 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6380 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6381 tree exp, type, rhs;
6383 const char *errtype;
6387 register enum tree_code codel = TREE_CODE (type);
6388 register tree rhstype;
6389 register enum tree_code coder;
6391 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6392 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6393 if (TREE_CODE (rhs) == NOP_EXPR
6394 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6395 && codel != REFERENCE_TYPE)
6396 rhs = TREE_OPERAND (rhs, 0);
6398 if (rhs == error_mark_node
6399 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6400 return error_mark_node;
6402 if (TREE_CODE (rhs) == OFFSET_REF)
6404 rhs = resolve_offset_ref (rhs);
6405 if (rhs == error_mark_node)
6406 return error_mark_node;
6409 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6410 rhs = convert_from_reference (rhs);
6412 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6413 && TREE_CODE (type) != ARRAY_TYPE
6414 && (TREE_CODE (type) != REFERENCE_TYPE
6415 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6416 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6417 && (TREE_CODE (type) != REFERENCE_TYPE
6418 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6419 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6420 rhs = default_conversion (rhs);
6422 rhstype = TREE_TYPE (rhs);
6423 coder = TREE_CODE (rhstype);
6425 if (coder == ERROR_MARK)
6426 return error_mark_node;
6428 /* We accept references to incomplete types, so we can
6429 return here before checking if RHS is of complete type. */
6431 if (codel == REFERENCE_TYPE)
6433 /* This should eventually happen in convert_arguments. */
6434 int savew = 0, savee = 0;
6437 savew = warningcount, savee = errorcount;
6438 rhs = initialize_reference (type, rhs);
6441 if (warningcount > savew)
6442 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6443 else if (errorcount > savee)
6444 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6450 exp = require_complete_type (exp);
6451 if (exp == error_mark_node)
6452 return error_mark_node;
6454 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6455 rhstype = TREE_TYPE (rhstype);
6457 type = complete_type (type);
6459 if (IS_AGGR_TYPE (type))
6460 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6462 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6465 /* Expand an ASM statement with operands, handling output operands
6466 that are not variables or INDIRECT_REFS by transforming such
6467 cases into cases that expand_asm_operands can handle.
6469 Arguments are same as for expand_asm_operands.
6471 We don't do default conversions on all inputs, because it can screw
6472 up operands that are expected to be in memory. */
6475 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6476 tree string, outputs, inputs, clobbers;
6478 const char *filename;
6481 int noutputs = list_length (outputs);
6483 /* o[I] is the place that output number I should be written. */
6484 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6487 /* Record the contents of OUTPUTS before it is modified. */
6488 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6489 o[i] = TREE_VALUE (tail);
6491 /* Generate the ASM_OPERANDS insn;
6492 store into the TREE_VALUEs of OUTPUTS some trees for
6493 where the values were actually stored. */
6494 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6496 /* Copy all the intermediate outputs into the specified outputs. */
6497 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6499 if (o[i] != TREE_VALUE (tail))
6501 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6502 const0_rtx, VOIDmode, EXPAND_NORMAL);
6505 /* Restore the original value so that it's correct the next
6506 time we expand this function. */
6507 TREE_VALUE (tail) = o[i];
6509 /* Detect modification of read-only values.
6510 (Otherwise done by build_modify_expr.) */
6513 tree type = TREE_TYPE (o[i]);
6514 if (CP_TYPE_CONST_P (type)
6515 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6516 && C_TYPE_FIELDS_READONLY (type)))
6517 readonly_error (o[i], "modification by `asm'", 1);
6521 /* Those MODIFY_EXPRs could do autoincrements. */
6525 /* If RETVAL is the address of, or a reference to, a local variable or
6526 temporary give an appropraite warning. */
6529 maybe_warn_about_returning_address_of_local (retval)
6532 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6533 tree whats_returned = retval;
6537 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6538 whats_returned = TREE_OPERAND (whats_returned, 1);
6539 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6540 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6541 || TREE_CODE (whats_returned) == NOP_EXPR)
6542 whats_returned = TREE_OPERAND (whats_returned, 0);
6547 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6549 whats_returned = TREE_OPERAND (whats_returned, 0);
6551 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6553 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6554 || TREE_CODE (whats_returned) == TARGET_EXPR)
6556 /* Get the target. */
6557 whats_returned = TREE_OPERAND (whats_returned, 0);
6558 warning ("returning reference to temporary");
6561 if (TREE_CODE (whats_returned) == VAR_DECL
6562 && DECL_NAME (whats_returned)
6563 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6565 warning ("reference to non-lvalue returned");
6570 if (TREE_CODE (whats_returned) == VAR_DECL
6571 && DECL_NAME (whats_returned)
6572 && DECL_FUNCTION_SCOPE_P (whats_returned)
6573 && !(TREE_STATIC (whats_returned)
6574 || TREE_PUBLIC (whats_returned)))
6576 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6577 cp_warning_at ("reference to local variable `%D' returned",
6580 cp_warning_at ("address of local variable `%D' returned",
6586 /* Check that returning RETVAL from the current function is legal.
6587 Return an expression explicitly showing all conversions required to
6588 change RETVAL into the function return type, and to assign it to
6589 the DECL_RESULT for the function. */
6592 check_return_expr (retval)
6596 /* The type actually returned by the function, after any
6599 int fn_returns_value_p;
6601 /* A `volatile' function is one that isn't supposed to return, ever.
6602 (This is a G++ extension, used to get better code for functions
6603 that call the `volatile' function.) */
6604 if (TREE_THIS_VOLATILE (current_function_decl))
6605 warning ("function declared `noreturn' has a `return' statement");
6607 /* Check for various simple errors. */
6611 error ("returning a value from a destructor");
6614 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6616 if (in_function_try_handler)
6617 /* If a return statement appears in a handler of the
6618 function-try-block of a constructor, the program is ill-formed. */
6619 error ("cannot return from a handler of a function-try-block of a constructor");
6621 /* You can't return a value from a constructor. */
6622 error ("returning a value from a constructor");
6626 /* When no explicit return-value is given in a function with a named
6627 return value, the named return value is used. */
6628 result = DECL_RESULT (current_function_decl);
6629 valtype = TREE_TYPE (result);
6630 my_friendly_assert (valtype != NULL_TREE, 19990924);
6631 fn_returns_value_p = !VOID_TYPE_P (valtype);
6632 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6635 /* Check for a return statement with no return value in a function
6636 that's supposed to return a value. */
6637 if (!retval && fn_returns_value_p)
6639 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6640 /* Clear this, so finish_function won't say that we reach the
6641 end of a non-void function (which we don't, we gave a
6643 current_function_returns_null = 0;
6645 /* Check for a return statement with a value in a function that
6646 isn't supposed to return a value. */
6647 else if (retval && !fn_returns_value_p)
6649 if (VOID_TYPE_P (TREE_TYPE (retval)))
6650 /* You can return a `void' value from a function of `void'
6651 type. In that case, we have to evaluate the expression for
6652 its side-effects. */
6653 finish_expr_stmt (retval);
6655 pedwarn ("return-statement with a value, in function declared with a void return type");
6657 current_function_returns_null = 1;
6659 /* There's really no value to return, after all. */
6663 /* Remember that this function can sometimes return without a
6665 current_function_returns_null = 1;
6667 /* Remember that this function did return a value. */
6668 current_function_returns_value = 1;
6670 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6671 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6672 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6673 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6675 && null_ptr_cst_p (retval))
6676 cp_warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6678 /* Effective C++ rule 15. See also start_function. */
6680 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6681 && retval != current_class_ref)
6682 cp_warning ("`operator=' should return a reference to `*this'");
6684 /* The fabled Named Return Value optimization: If this is a
6685 value-returning function that always returns the same local
6686 variable, remember it.
6688 It might be nice to be more flexible, and choose the first suitable
6689 variable even if the function sometimes returns something else, but
6690 then we run the risk of clobbering the variable we chose if the other
6691 returned expression uses the chosen variable somehow. And people expect
6692 this restriction, anyway. (jason 2000-11-19) */
6694 if (fn_returns_value_p && flag_elide_constructors)
6696 if (retval != NULL_TREE
6697 && (current_function_return_value == NULL_TREE
6698 || current_function_return_value == retval)
6699 && TREE_CODE (retval) == VAR_DECL
6700 && DECL_CONTEXT (retval) == current_function_decl
6701 && ! TREE_STATIC (retval)
6702 && (DECL_ALIGN (retval)
6703 == DECL_ALIGN (DECL_RESULT (current_function_decl)))
6704 && same_type_p (TREE_TYPE (retval),
6705 TREE_TYPE (TREE_TYPE (current_function_decl))))
6706 current_function_return_value = retval;
6708 current_function_return_value = error_mark_node;
6711 /* We don't need to do any conversions when there's nothing being
6713 if (!retval || retval == error_mark_node)
6716 /* Do any required conversions. */
6717 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6718 /* No conversions are required. */
6722 /* The type the function is declared to return. */
6723 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6725 /* First convert the value to the function's return type, then
6726 to the type of return value's location to handle the
6727 case that functype is smaller than the valtype. */
6728 retval = convert_for_initialization
6729 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6730 "return", NULL_TREE, 0);
6731 retval = convert (valtype, retval);
6733 /* If the conversion failed, treat this just like `return;'. */
6734 if (retval == error_mark_node)
6736 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6737 else if (! current_function_returns_struct
6738 && TREE_CODE (retval) == TARGET_EXPR
6739 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6740 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6741 TREE_OPERAND (retval, 0));
6743 maybe_warn_about_returning_address_of_local (retval);
6746 /* Actually copy the value returned into the appropriate location. */
6747 if (retval && retval != result)
6748 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6754 /* Returns non-zero if the pointer-type FROM can be converted to the
6755 pointer-type TO via a qualification conversion. If CONSTP is -1,
6756 then we return non-zero if the pointers are similar, and the
6757 cv-qualification signature of FROM is a proper subset of that of TO.
6759 If CONSTP is positive, then all outer pointers have been
6763 comp_ptr_ttypes_real (to, from, constp)
6767 int to_more_cv_qualified = 0;
6769 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6771 if (TREE_CODE (to) != TREE_CODE (from))
6774 if (TREE_CODE (from) == OFFSET_TYPE
6775 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6776 TYPE_OFFSET_BASETYPE (to)))
6779 /* Const and volatile mean something different for function types,
6780 so the usual checks are not appropriate. */
6781 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6783 if (!at_least_as_qualified_p (to, from))
6786 if (!at_least_as_qualified_p (from, to))
6791 ++to_more_cv_qualified;
6795 constp &= TYPE_READONLY (to);
6798 if (TREE_CODE (to) != POINTER_TYPE)
6800 same_type_ignoring_top_level_qualifiers_p (to, from)
6801 && (constp >= 0 || to_more_cv_qualified);
6805 /* When comparing, say, char ** to char const **, this function takes the
6806 'char *' and 'char const *'. Do not pass non-pointer types to this
6810 comp_ptr_ttypes (to, from)
6813 return comp_ptr_ttypes_real (to, from, 1);
6816 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6817 type or inheritance-related types, regardless of cv-quals. */
6820 ptr_reasonably_similar (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 && comptypes (TYPE_OFFSET_BASETYPE (to),
6830 TYPE_OFFSET_BASETYPE (from),
6831 COMPARE_BASE | COMPARE_RELAXED))
6834 if (TREE_CODE (to) != POINTER_TYPE)
6836 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6837 COMPARE_BASE | COMPARE_RELAXED);
6841 /* Like comp_ptr_ttypes, for const_cast. */
6844 comp_ptr_ttypes_const (to, from)
6847 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6849 if (TREE_CODE (to) != TREE_CODE (from))
6852 if (TREE_CODE (from) == OFFSET_TYPE
6853 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6854 TYPE_OFFSET_BASETYPE (to)))
6857 if (TREE_CODE (to) != POINTER_TYPE)
6858 return same_type_ignoring_top_level_qualifiers_p (to, from);
6862 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6865 comp_ptr_ttypes_reinterpret (to, from)
6870 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6872 if (TREE_CODE (from) == OFFSET_TYPE)
6873 from = TREE_TYPE (from);
6874 if (TREE_CODE (to) == OFFSET_TYPE)
6875 to = TREE_TYPE (to);
6877 /* Const and volatile mean something different for function types,
6878 so the usual checks are not appropriate. */
6879 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6880 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6882 if (!at_least_as_qualified_p (to, from))
6886 && !at_least_as_qualified_p (from, to))
6888 constp &= TYPE_READONLY (to);
6891 if (TREE_CODE (from) != POINTER_TYPE
6892 || TREE_CODE (to) != POINTER_TYPE)
6897 /* Returns the type-qualifier set corresponding to TYPE. */
6900 cp_type_quals (type)
6903 type = strip_array_types (type);
6904 return TYPE_QUALS (type);
6907 /* Returns non-zero if the TYPE contains a mutable member */
6910 cp_has_mutable_p (type)
6913 type = strip_array_types (type);
6915 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6918 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6919 exemplar types such that casting T1 to T2 is casting away castness
6920 if and only if there is no implicit conversion from T1 to T2. */
6923 casts_away_constness_r (t1, t2)
6930 /* [expr.const.cast]
6932 For multi-level pointer to members and multi-level mixed pointers
6933 and pointers to members (conv.qual), the "member" aspect of a
6934 pointer to member level is ignored when determining if a const
6935 cv-qualifier has been cast away. */
6936 if (TYPE_PTRMEM_P (*t1))
6937 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6938 if (TYPE_PTRMEM_P (*t2))
6939 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6941 /* [expr.const.cast]
6943 For two pointer types:
6945 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6946 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6949 casting from X1 to X2 casts away constness if, for a non-pointer
6950 type T there does not exist an implicit conversion (clause
6953 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6957 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6959 if (TREE_CODE (*t1) != POINTER_TYPE
6960 || TREE_CODE (*t2) != POINTER_TYPE)
6962 *t1 = cp_build_qualified_type (void_type_node,
6963 CP_TYPE_QUALS (*t1));
6964 *t2 = cp_build_qualified_type (void_type_node,
6965 CP_TYPE_QUALS (*t2));
6969 quals1 = CP_TYPE_QUALS (*t1);
6970 quals2 = CP_TYPE_QUALS (*t2);
6971 *t1 = TREE_TYPE (*t1);
6972 *t2 = TREE_TYPE (*t2);
6973 casts_away_constness_r (t1, t2);
6974 *t1 = build_pointer_type (*t1);
6975 *t2 = build_pointer_type (*t2);
6976 *t1 = cp_build_qualified_type (*t1, quals1);
6977 *t2 = cp_build_qualified_type (*t2, quals2);
6980 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
6984 casts_away_constness (t1, t2)
6988 if (TREE_CODE (t2) == REFERENCE_TYPE)
6990 /* [expr.const.cast]
6992 Casting from an lvalue of type T1 to an lvalue of type T2
6993 using a reference cast casts away constness if a cast from an
6994 rvalue of type "pointer to T1" to the type "pointer to T2"
6995 casts away constness. */
6996 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
6997 ? TREE_TYPE (t1) : t1);
6998 return casts_away_constness (build_pointer_type (t1),
6999 build_pointer_type (TREE_TYPE (t2)));
7002 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7003 /* [expr.const.cast]
7005 Casting from an rvalue of type "pointer to data member of X
7006 of type T1" to the type "pointer to data member of Y of type
7007 T2" casts away constness if a cast from an rvalue of type
7008 "poitner to T1" to the type "pointer to T2" casts away
7010 return casts_away_constness
7011 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7012 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7014 /* Casting away constness is only something that makes sense for
7015 pointer or reference types. */
7016 if (TREE_CODE (t1) != POINTER_TYPE
7017 || TREE_CODE (t2) != POINTER_TYPE)
7020 /* Top-level qualifiers don't matter. */
7021 t1 = TYPE_MAIN_VARIANT (t1);
7022 t2 = TYPE_MAIN_VARIANT (t2);
7023 casts_away_constness_r (&t1, &t2);
7024 if (!can_convert (t2, t1))
7030 /* Returns TYPE with its cv qualifiers removed
7031 TYPE is T cv* .. *cv where T is not a pointer type,
7032 returns T * .. *. (If T is an array type, then the cv qualifiers
7033 above are those of the array members.) */
7036 strip_all_pointer_quals (type)
7039 if (TREE_CODE (type) == POINTER_TYPE)
7040 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
7041 else if (TREE_CODE (type) == OFFSET_TYPE)
7042 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
7043 strip_all_pointer_quals (TREE_TYPE (type)));
7045 return TYPE_MAIN_VARIANT (type);