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 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). */
45 static tree convert_for_assignment PARAMS ((tree, tree, const char *, tree,
47 static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree));
48 static tree rationalize_conditional_expr PARAMS ((enum tree_code, tree));
49 static int comp_target_parms PARAMS ((tree, tree, int));
50 static int comp_ptr_ttypes_real PARAMS ((tree, tree, int));
51 static int comp_ptr_ttypes_const PARAMS ((tree, tree));
52 static int comp_ptr_ttypes_reinterpret PARAMS ((tree, tree));
53 static int comp_except_types PARAMS ((tree, tree, int));
54 static int comp_array_types PARAMS ((int (*) (tree, tree, int), tree,
56 static tree common_base_type PARAMS ((tree, tree));
58 static tree convert_sequence PARAMS ((tree, tree));
60 static tree lookup_anon_field PARAMS ((tree, tree));
61 static tree pointer_diff PARAMS ((tree, tree, tree));
62 static tree build_component_addr PARAMS ((tree, tree));
63 static tree qualify_type PARAMS ((tree, tree));
64 static tree get_delta_difference PARAMS ((tree, tree, int));
65 static int comp_cv_target_types PARAMS ((tree, tree, int));
66 static void casts_away_constness_r PARAMS ((tree *, tree *));
67 static int casts_away_constness PARAMS ((tree, tree));
68 static void maybe_warn_about_returning_address_of_local PARAMS ((tree));
69 static tree strip_all_pointer_quals PARAMS ((tree));
71 /* Return the target type of TYPE, which means return T for:
72 T*, T&, T[], T (...), and otherwise, just T. */
78 if (TREE_CODE (type) == REFERENCE_TYPE)
79 type = TREE_TYPE (type);
80 while (TREE_CODE (type) == POINTER_TYPE
81 || TREE_CODE (type) == ARRAY_TYPE
82 || TREE_CODE (type) == FUNCTION_TYPE
83 || TREE_CODE (type) == METHOD_TYPE
84 || TREE_CODE (type) == OFFSET_TYPE)
85 type = TREE_TYPE (type);
89 /* Do `exp = require_complete_type (exp);' to make sure exp
90 does not have an incomplete type. (That includes void types.)
91 Returns the error_mark_node if the VALUE does not have
92 complete type when this function returns. */
95 require_complete_type (value)
100 if (processing_template_decl || value == error_mark_node)
103 if (TREE_CODE (value) == OVERLOAD)
104 type = unknown_type_node;
106 type = TREE_TYPE (value);
108 /* First, detect a valid value with a complete type. */
109 if (COMPLETE_TYPE_P (type))
112 /* If we see X::Y, we build an OFFSET_TYPE which has
113 not been laid out. Try to avoid an error by interpreting
114 it as this->X::Y, if reasonable. */
115 if (TREE_CODE (value) == OFFSET_REF
116 && current_class_ref != 0
117 && TREE_OPERAND (value, 0) == current_class_ref)
119 tree base, member = TREE_OPERAND (value, 1);
120 tree basetype = TYPE_OFFSET_BASETYPE (type);
121 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
122 base = convert_pointer_to (basetype, current_class_ptr);
123 value = build (COMPONENT_REF, TREE_TYPE (member),
124 build_indirect_ref (base, NULL_PTR), member);
125 return require_complete_type (value);
128 if (complete_type_or_else (type, value))
131 return error_mark_node;
134 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
135 a template instantiation, do the instantiation. Returns TYPE,
136 whether or not it could be completed, unless something goes
137 horribly wrong, in which case the error_mark_node is returned. */
143 if (type == NULL_TREE)
144 /* Rather than crash, we return something sure to cause an error
146 return error_mark_node;
148 if (type == error_mark_node || COMPLETE_TYPE_P (type))
150 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
152 tree t = complete_type (TREE_TYPE (type));
153 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
155 TYPE_NEEDS_CONSTRUCTING (type)
156 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
157 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
158 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
160 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
161 instantiate_class_template (TYPE_MAIN_VARIANT (type));
166 /* Like complete_type, but issue an error if the TYPE cannot be
167 completed. VALUE is used for informative diagnostics.
168 Returns NULL_TREE if the type cannot be made complete. */
171 complete_type_or_else (type, value)
175 type = complete_type (type);
176 if (type == error_mark_node)
177 /* We already issued an error. */
179 else if (!COMPLETE_TYPE_P (type))
181 incomplete_type_error (value, type);
188 /* Return truthvalue of whether type of EXP is instantiated. */
194 return (TREE_CODE (exp) == OVERLOAD
195 || TREE_CODE (exp) == TREE_LIST
196 || TREE_TYPE (exp) == unknown_type_node
197 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
198 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
201 /* Return a variant of TYPE which has all the type qualifiers of LIKE
202 as well as those of TYPE. */
205 qualify_type (type, like)
208 /* @@ Must do member pointers here. */
209 return cp_build_qualified_type (type, (CP_TYPE_QUALS (type)
210 | CP_TYPE_QUALS (like)));
213 /* Return the common type of two parameter lists.
214 We assume that comptypes has already been done and returned 1;
215 if that isn't so, this may crash.
217 As an optimization, free the space we allocate if the parameter
218 lists are already common. */
224 tree oldargs = p1, newargs, n;
227 char *first_obj = (char *) oballoc (0);
229 len = list_length (p1);
230 newargs = tree_last (p1);
232 if (newargs == void_list_node)
241 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
246 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
248 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
250 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
253 else if (! TREE_PURPOSE (p1))
255 if (TREE_PURPOSE (p2))
257 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
263 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
265 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
267 if (TREE_VALUE (p1) != TREE_VALUE (p2))
270 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
273 TREE_VALUE (n) = TREE_VALUE (p1);
284 /* Given a type, perhaps copied for a typedef,
285 find the "original" version of it. */
290 while (TYPE_NAME (t) != NULL_TREE)
292 tree x = TYPE_NAME (t);
293 if (TREE_CODE (x) != TYPE_DECL)
295 x = DECL_ORIGINAL_TYPE (x);
303 /* T1 and T2 are arithmetic or enumeration types. Return the type
304 that will result from the "usual arithmetic converions" on T1 and
305 T2 as described in [expr]. */
308 type_after_usual_arithmetic_conversions (t1, t2)
312 enum tree_code code1 = TREE_CODE (t1);
313 enum tree_code code2 = TREE_CODE (t2);
316 /* FIXME: Attributes. */
317 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
318 || TREE_CODE (t1) == ENUMERAL_TYPE,
320 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
321 || TREE_CODE (t2) == ENUMERAL_TYPE,
324 /* In what follows, we slightly generalize the rules given in [expr]
325 so as to deal with `long long'. First, merge the attributes. */
326 attributes = merge_machine_type_attributes (t1, t2);
328 /* If only one is real, use it as the result. */
329 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
330 return build_type_attribute_variant (t1, attributes);
331 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
332 return build_type_attribute_variant (t2, attributes);
334 /* Perform the integral promotions. */
335 if (code1 != REAL_TYPE)
337 t1 = type_promotes_to (t1);
338 t2 = type_promotes_to (t2);
341 /* Both real or both integers; use the one with greater precision. */
342 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
343 return build_type_attribute_variant (t1, attributes);
344 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
345 return build_type_attribute_variant (t2, attributes);
347 if (code1 != REAL_TYPE)
349 /* If one is a sizetype, use it so size_binop doesn't blow up. */
350 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
351 return build_type_attribute_variant (t1, attributes);
352 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
353 return build_type_attribute_variant (t2, attributes);
355 /* If one is unsigned long long, then convert the other to unsigned
357 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
358 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
359 return build_type_attribute_variant (long_long_unsigned_type_node,
361 /* If one is a long long, and the other is an unsigned long, and
362 long long can represent all the values of an unsigned long, then
363 convert to a long long. Otherwise, convert to an unsigned long
364 long. Otherwise, if either operand is long long, convert the
367 Since we're here, we know the TYPE_PRECISION is the same;
368 therefore converting to long long cannot represent all the values
369 of an unsigned long, so we choose unsigned long long in that
371 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
372 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
374 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
375 ? long_long_unsigned_type_node
376 : long_long_integer_type_node);
377 return build_type_attribute_variant (t, attributes);
380 /* Go through the same procedure, but for longs. */
381 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
382 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
383 return build_type_attribute_variant (long_unsigned_type_node,
385 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
386 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
388 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
389 ? long_unsigned_type_node : long_integer_type_node);
390 return build_type_attribute_variant (t, attributes);
392 /* Otherwise prefer the unsigned one. */
393 if (TREE_UNSIGNED (t1))
394 return build_type_attribute_variant (t1, attributes);
396 return build_type_attribute_variant (t2, attributes);
400 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
401 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
402 return build_type_attribute_variant (long_double_type_node,
404 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
405 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
406 return build_type_attribute_variant (double_type_node,
409 return build_type_attribute_variant (float_type_node,
414 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
415 ARG1 and ARG2 are the values with those types. The LOCATION is a
416 string describing the current location, in case an error occurs. */
419 composite_pointer_type (t1, t2, arg1, arg2, location)
424 const char* location;
430 If one operand is a null pointer constant, the composite pointer
431 type is the type of the other operand. */
432 if (null_ptr_cst_p (arg1))
434 if (null_ptr_cst_p (arg2))
437 /* Deal with pointer-to-member functions in the same way as we deal
438 with pointers to functions. */
439 if (TYPE_PTRMEMFUNC_P (t1))
440 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
441 if (TYPE_PTRMEMFUNC_P (t2))
442 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
444 if (comp_target_types (t1, t2, 1))
445 result_type = common_type (t1, t2);
446 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t1)) == void_type_node)
448 if (pedantic && TREE_CODE (t2) == FUNCTION_TYPE)
449 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
450 result_type = qualify_type (t1, t2);
452 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t2)) == void_type_node)
454 if (pedantic && TREE_CODE (t1) == FUNCTION_TYPE)
455 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
456 result_type = qualify_type (t2, t1);
459 else if (same_or_base_type_p (t2, t1))
461 else if (IS_AGGR_TYPE (TREE_TYPE (t1))
462 && IS_AGGR_TYPE (TREE_TYPE (t2))
463 && (result_type = common_base_type (TREE_TYPE (t1),
466 if (result_type == error_mark_node)
468 cp_error ("common base type of types `%T' and `%T' is ambiguous",
469 TREE_TYPE (t1), TREE_TYPE (t2));
470 result_type = ptr_type_node;
475 && result_type != TREE_TYPE (t1)
476 && result_type != TREE_TYPE (t2))
477 cp_pedwarn ("types `%T' and `%T' converted to `%T *' in %s",
478 t1, t2, result_type, location);
480 result_type = build_pointer_type (result_type);
485 cp_pedwarn ("pointer type mismatch in %s", location);
486 result_type = ptr_type_node;
492 /* Return the common type of two types.
493 We assume that comptypes has already been done and returned 1;
494 if that isn't so, this may crash.
496 This is the type for the result of most arithmetic operations
497 if the operands have the given two types.
499 We do not deal with enumeral types here because they have already been
500 converted to integer types. */
506 register enum tree_code code1;
507 register enum tree_code code2;
510 /* Save time if the two types are the same. */
513 t1 = original_type (t1);
514 t2 = original_type (t2);
518 /* If one type is nonsense, use the other. */
519 if (t1 == error_mark_node)
521 if (t2 == error_mark_node)
524 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
525 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
526 return type_after_usual_arithmetic_conversions (t1, t2);
528 /* Merge the attributes. */
529 attributes = merge_machine_type_attributes (t1, t2);
531 /* Treat an enum type as the unsigned integer type of the same width. */
533 if (TREE_CODE (t1) == ENUMERAL_TYPE)
534 t1 = type_for_size (TYPE_PRECISION (t1), 1);
535 if (TREE_CODE (t2) == ENUMERAL_TYPE)
536 t2 = type_for_size (TYPE_PRECISION (t2), 1);
538 if (TYPE_PTRMEMFUNC_P (t1))
539 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
540 if (TYPE_PTRMEMFUNC_P (t2))
541 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
543 code1 = TREE_CODE (t1);
544 code2 = TREE_CODE (t2);
546 /* If one type is complex, form the common type of the non-complex
547 components, then make that complex. Use T1 or T2 if it is the
549 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
551 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
552 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
553 tree subtype = common_type (subtype1, subtype2);
555 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
556 return build_type_attribute_variant (t1, attributes);
557 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
558 return build_type_attribute_variant (t2, attributes);
560 return build_type_attribute_variant (build_complex_type (subtype),
568 /* We should have called type_after_usual_arithmetic_conversions
570 my_friendly_abort (19990725);
575 /* For two pointers, do this recursively on the target type,
576 and combine the qualifiers of the two types' targets. */
577 /* This code was turned off; I don't know why.
578 But ISO C++ specifies doing this with the qualifiers.
579 So I turned it on again. */
581 tree tt1 = TREE_TYPE (t1);
582 tree tt2 = TREE_TYPE (t2);
587 if (TREE_CODE (tt1) == OFFSET_TYPE)
589 b1 = TYPE_OFFSET_BASETYPE (tt1);
590 b2 = TYPE_OFFSET_BASETYPE (tt2);
591 tt1 = TREE_TYPE (tt1);
592 tt2 = TREE_TYPE (tt2);
597 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
598 tt1 = TYPE_MAIN_VARIANT (tt1);
599 tt2 = TYPE_MAIN_VARIANT (tt2);
603 else if (tt1 == void_type_node || tt2 == void_type_node)
604 target = void_type_node;
605 else if (tt1 == unknown_type_node)
607 else if (tt2 == unknown_type_node)
610 target = common_type (tt1, tt2);
612 target = cp_build_qualified_type (target, type_quals);
616 if (same_type_p (b1, b2)
617 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
618 target = build_offset_type (b2, target);
619 else if (binfo_or_else (b2, b1))
620 target = build_offset_type (b1, target);
623 if (code1 == POINTER_TYPE)
624 t1 = build_pointer_type (target);
626 t1 = build_reference_type (target);
627 t1 = build_type_attribute_variant (t1, attributes);
629 if (TREE_CODE (target) == METHOD_TYPE)
630 t1 = build_ptrmemfunc_type (t1);
637 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
638 /* Save space: see if the result is identical to one of the args. */
639 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
640 return build_type_attribute_variant (t1, attributes);
641 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
642 return build_type_attribute_variant (t2, attributes);
643 /* Merge the element types, and have a size if either arg has one. */
644 t1 = build_cplus_array_type
645 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
646 return build_type_attribute_variant (t1, attributes);
650 /* Function types: prefer the one that specified arg types.
651 If both do, merge the arg types. Also merge the return types. */
653 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
654 tree p1 = TYPE_ARG_TYPES (t1);
655 tree p2 = TYPE_ARG_TYPES (t2);
658 /* Save space: see if the result is identical to one of the args. */
659 if (valtype == TREE_TYPE (t1) && ! p2)
660 return build_type_attribute_variant (t1, attributes);
661 if (valtype == TREE_TYPE (t2) && ! p1)
662 return build_type_attribute_variant (t2, attributes);
664 /* Simple way if one arg fails to specify argument types. */
665 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
667 rval = build_function_type (valtype, p2);
668 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
669 rval = build_exception_variant (rval, raises);
670 return build_type_attribute_variant (rval, attributes);
672 raises = TYPE_RAISES_EXCEPTIONS (t1);
673 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
675 rval = build_function_type (valtype, p1);
677 rval = build_exception_variant (rval, raises);
678 return build_type_attribute_variant (rval, attributes);
681 rval = build_function_type (valtype, commonparms (p1, p2));
682 rval = build_exception_variant (rval, raises);
683 return build_type_attribute_variant (rval, attributes);
688 t1 = TYPE_MAIN_VARIANT (t1);
689 t2 = TYPE_MAIN_VARIANT (t2);
691 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
692 return build_type_attribute_variant (t1, attributes);
693 else if (binfo_or_else (t2, t1))
694 return build_type_attribute_variant (t2, attributes);
697 compiler_error ("common_type called with uncommon aggregate types");
698 return error_mark_node;
702 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
704 /* Get this value the long way, since TYPE_METHOD_BASETYPE
705 is just the main variant of this. */
709 tree b1 = TYPE_OFFSET_BASETYPE (t1);
710 tree b2 = TYPE_OFFSET_BASETYPE (t2);
712 if (same_type_p (b1, b2)
713 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
714 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
717 if (binfo_or_else (b2, b1) == NULL_TREE)
718 compiler_error ("common_type called with uncommon method types");
719 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
722 raises = TYPE_RAISES_EXCEPTIONS (t1);
724 /* If this was a member function type, get back to the
725 original type of type member function (i.e., without
726 the class instance variable up front. */
727 t1 = build_function_type (TREE_TYPE (t1),
728 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
729 t2 = build_function_type (TREE_TYPE (t2),
730 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
731 t3 = common_type (t1, t2);
732 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
733 TYPE_ARG_TYPES (t3));
734 t1 = build_exception_variant (t3, raises);
737 compiler_error ("common_type called with uncommon method types");
739 return build_type_attribute_variant (t1, attributes);
742 /* Pointers to members should now be handled by the POINTER_TYPE
744 my_friendly_abort (990325);
747 return build_type_attribute_variant (t1, attributes);
751 /* Compare two exception specifier types for exactness or subsetness, if
752 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
754 [except.spec] "If a class X ... objects of class X or any class publicly
755 and unambigously derrived from X. Similarly, if a pointer type Y * ...
756 exceptions of type Y * or that are pointers to any type publicly and
757 unambigously derrived from Y. Otherwise a function only allows exceptions
758 that have the same type ..."
759 This does not mention cv qualifiers and is different to what throw
760 [except.throw] and catch [except.catch] will do. They will ignore the
761 top level cv qualifiers, and allow qualifiers in the pointer to class
764 We implement the letter of the standard. */
767 comp_except_types (a, b, exact)
771 if (same_type_p (a, b))
775 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
778 if (TREE_CODE (a) == POINTER_TYPE
779 && TREE_CODE (b) == POINTER_TYPE)
783 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
787 if (TREE_CODE (a) != RECORD_TYPE
788 || TREE_CODE (b) != RECORD_TYPE)
791 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
797 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
798 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
799 otherwise it must be exact. Exception lists are unordered, but
800 we've already filtered out duplicates. Most lists will be in order,
801 we should try to make use of that. */
804 comp_except_specs (t1, t2, exact)
815 if (t1 == NULL_TREE) /* T1 is ... */
816 return t2 == NULL_TREE || !exact;
817 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
818 return t2 != NULL_TREE && !TREE_VALUE (t2);
819 if (t2 == NULL_TREE) /* T2 is ... */
821 if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
824 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
825 Count how many we find, to determine exactness. For exact matching and
826 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
828 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
830 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
832 tree a = TREE_VALUE (probe);
833 tree b = TREE_VALUE (t2);
835 if (comp_except_types (a, b, exact))
837 if (probe == base && exact)
838 base = TREE_CHAIN (probe);
843 if (probe == NULL_TREE)
846 return !exact || base == NULL_TREE || length == list_length (t1);
849 /* Compare the array types T1 and T2, using CMP as the type comparison
850 function for the element types. STRICT is as for comptypes. */
853 comp_array_types (cmp, t1, t2, strict)
854 register int (*cmp) PARAMS ((tree, tree, int));
864 /* The type of the array elements must be the same. */
865 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
866 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
867 strict & ~COMPARE_REDECLARATION)))
870 d1 = TYPE_DOMAIN (t1);
871 d2 = TYPE_DOMAIN (t2);
876 /* If one of the arrays is dimensionless, and the other has a
877 dimension, they are of different types. However, it is legal to
885 declarations for an array object can specify
886 array types that differ by the presence or absence of a major
887 array bound (_dcl.array_). */
889 return strict & COMPARE_REDECLARATION;
891 /* Check that the dimensions are the same. */
892 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
894 && cp_tree_equal (TYPE_MAX_VALUE (d1),
895 TYPE_MAX_VALUE (d2)));
898 /* Return 1 if T1 and T2 are compatible types for assignment or
899 various other operations. STRICT is a bitwise-or of the COMPARE_*
903 comptypes (t1, t2, strict)
909 int orig_strict = strict;
911 /* The special exemption for redeclaring array types without an
912 array bound only applies at the top level:
917 is not legal, for example. */
918 strict &= ~COMPARE_REDECLARATION;
920 /* Suppress errors caused by previously reported errors */
924 /* This should never happen. */
925 my_friendly_assert (t1 != error_mark_node, 307);
927 if (t2 == error_mark_node)
930 /* If either type is the internal version of sizetype, return the
932 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
933 && TYPE_DOMAIN (t1) != 0)
934 t1 = TYPE_DOMAIN (t1);
936 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
937 && TYPE_DOMAIN (t2) != 0)
938 t2 = TYPE_DOMAIN (t2);
940 if (strict & COMPARE_RELAXED)
942 /* Treat an enum type as the unsigned integer type of the same width. */
944 if (TREE_CODE (t1) == ENUMERAL_TYPE)
945 t1 = type_for_size (TYPE_PRECISION (t1), 1);
946 if (TREE_CODE (t2) == ENUMERAL_TYPE)
947 t2 = type_for_size (TYPE_PRECISION (t2), 1);
953 if (TYPE_PTRMEMFUNC_P (t1))
954 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
955 if (TYPE_PTRMEMFUNC_P (t2))
956 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
958 /* Different classes of types can't be compatible. */
959 if (TREE_CODE (t1) != TREE_CODE (t2))
962 /* Qualifiers must match. */
963 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
965 if (strict == COMPARE_STRICT
966 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
969 /* Allow for two different type nodes which have essentially the same
970 definition. Note that we already checked for equality of the type
971 qualifiers (just above). */
973 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
976 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
977 attribute is its own main variant (`val' will remain 0). */
978 #ifndef COMP_TYPE_ATTRIBUTES
979 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
982 if (strict & COMPARE_NO_ATTRIBUTES)
984 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
985 else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
988 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
991 switch (TREE_CODE (t1))
993 case TEMPLATE_TEMPLATE_PARM:
994 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
995 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
997 if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1)),
998 DECL_TEMPLATE_PARMS (TYPE_NAME (t2))))
1000 if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1)
1001 && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2))
1003 /* Don't check inheritance. */
1004 strict = COMPARE_STRICT;
1009 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1010 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1011 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
1012 val = comp_template_args (TYPE_TI_ARGS (t1),
1015 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1017 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1022 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1023 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1024 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1028 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1029 TYPE_RAISES_EXCEPTIONS (t2), 1))
1032 /* This case is anti-symmetrical!
1033 One can pass a base member (or member function)
1034 to something expecting a derived member (or member function),
1035 but not vice-versa! */
1037 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1038 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1042 case REFERENCE_TYPE:
1043 t1 = TREE_TYPE (t1);
1044 t2 = TREE_TYPE (t2);
1045 /* first, check whether the referred types match with the
1046 required level of strictness */
1047 val = comptypes (t1, t2, strict);
1050 if (TREE_CODE (t1) == RECORD_TYPE
1051 && TREE_CODE (t2) == RECORD_TYPE)
1056 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1057 TYPE_RAISES_EXCEPTIONS (t2), 1))
1060 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1061 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1062 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1066 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1067 here since this is the one place where
1068 COMPARE_REDECLARATION should be used. */
1069 val = comp_array_types (comptypes, t1, t2, orig_strict);
1072 case TEMPLATE_TYPE_PARM:
1073 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1074 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1077 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
1079 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1082 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1087 return attrval == 2 && val == 1 ? 2 : val;
1090 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1091 only in the same direction as the target type. */
1094 comp_cv_target_types (ttl, ttr, nptrs)
1100 if (!at_least_as_qualified_p (ttl, ttr)
1101 && !at_least_as_qualified_p (ttr, ttl))
1102 /* The qualifications are incomparable. */
1105 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1106 return more_qualified_p (ttr, ttl) ? -1 : 1;
1108 t = comp_target_types (ttl, ttr, nptrs);
1109 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1110 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1116 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1117 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1118 converted to TTL. Return -1 means that TTL can be converted to TTR but
1121 NPTRS is the number of pointers we can strip off and keep cool.
1122 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1123 but to not permit B** to convert to A**.
1125 This should go away. Callers should use can_convert or something
1126 similar instead. (jason 17 Apr 1997) */
1129 comp_target_types (ttl, ttr, nptrs)
1133 ttl = TYPE_MAIN_VARIANT (ttl);
1134 ttr = TYPE_MAIN_VARIANT (ttr);
1135 if (same_type_p (ttl, ttr))
1138 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1141 if ((TREE_CODE (ttr) == POINTER_TYPE
1142 || TREE_CODE (ttr) == REFERENCE_TYPE)
1143 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1144 of the type pointed to. This is necessary for reference init
1145 semantics. We won't get here from a previous call with nptrs == 1;
1146 for multi-level pointers we end up in comp_ptr_ttypes. */
1149 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1151 ttl = TREE_TYPE (ttl);
1152 ttr = TREE_TYPE (ttr);
1156 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1157 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1159 else if (TREE_CODE (ttl) == VOID_TYPE
1160 && TREE_CODE (ttr) != FUNCTION_TYPE
1161 && TREE_CODE (ttr) != METHOD_TYPE
1162 && TREE_CODE (ttr) != OFFSET_TYPE)
1164 else if (TREE_CODE (ttr) == VOID_TYPE
1165 && TREE_CODE (ttl) != FUNCTION_TYPE
1166 && TREE_CODE (ttl) != METHOD_TYPE
1167 && TREE_CODE (ttl) != OFFSET_TYPE)
1169 else if (TREE_CODE (ttl) == POINTER_TYPE
1170 || TREE_CODE (ttl) == ARRAY_TYPE)
1172 if (comp_ptr_ttypes (ttl, ttr))
1174 else if (comp_ptr_ttypes (ttr, ttl))
1180 /* Const and volatile mean something different for function types,
1181 so the usual checks are not appropriate. */
1182 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1183 return comp_target_types (ttl, ttr, nptrs - 1);
1185 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1188 if (TREE_CODE (ttr) == ARRAY_TYPE)
1189 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1190 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1197 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1202 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1211 argsl = TYPE_ARG_TYPES (ttl);
1212 argsr = TYPE_ARG_TYPES (ttr);
1214 /* Compare 'this' here, not in comp_target_parms. */
1215 if (TREE_CODE (ttr) == METHOD_TYPE)
1217 tree tl = TYPE_METHOD_BASETYPE (ttl);
1218 tree tr = TYPE_METHOD_BASETYPE (ttr);
1220 if (!same_or_base_type_p (tr, tl))
1222 if (same_or_base_type_p (tl, tr))
1228 argsl = TREE_CHAIN (argsl);
1229 argsr = TREE_CHAIN (argsr);
1232 switch (comp_target_parms (argsl, argsr, 1))
1240 return saw_contra ? -1 : 1;
1243 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1247 /* Contravariance: we can assign a pointer to base member to a pointer
1248 to derived member. Note difference from simple pointer case, where
1249 we can pass a pointer to derived to a pointer to base. */
1250 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1251 TYPE_OFFSET_BASETYPE (ttl)))
1253 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1254 TYPE_OFFSET_BASETYPE (ttr)))
1264 ttl = TREE_TYPE (ttl);
1265 ttr = TREE_TYPE (ttr);
1267 if (TREE_CODE (ttl) == POINTER_TYPE
1268 || TREE_CODE (ttl) == ARRAY_TYPE)
1270 if (comp_ptr_ttypes (ttl, ttr))
1276 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1281 else if (IS_AGGR_TYPE (ttl))
1285 if (same_or_base_type_p (build_pointer_type (ttl),
1286 build_pointer_type (ttr)))
1288 if (same_or_base_type_p (build_pointer_type (ttr),
1289 build_pointer_type (ttl)))
1297 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1300 at_least_as_qualified_p (type1, type2)
1304 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1305 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1306 == CP_TYPE_QUALS (type2));
1309 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1312 more_qualified_p (type1, type2)
1316 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1317 && at_least_as_qualified_p (type1, type2));
1320 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1321 more cv-qualified that TYPE1, and 0 otherwise. */
1324 comp_cv_qualification (type1, type2)
1328 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1331 if (at_least_as_qualified_p (type1, type2))
1334 else if (at_least_as_qualified_p (type2, type1))
1340 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1341 subset of the cv-qualification signature of TYPE2, and the types
1342 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1345 comp_cv_qual_signature (type1, type2)
1349 if (comp_ptr_ttypes_real (type2, type1, -1))
1351 else if (comp_ptr_ttypes_real (type1, type2, -1))
1357 /* If two types share a common base type, return that basetype.
1358 If there is not a unique most-derived base type, this function
1359 returns ERROR_MARK_NODE. */
1362 common_base_type (tt1, tt2)
1365 tree best = NULL_TREE;
1368 /* If one is a baseclass of another, that's good enough. */
1369 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1371 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1374 /* Otherwise, try to find a unique baseclass of TT1
1375 that is shared by TT2, and follow that down. */
1376 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1378 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1379 tree trial = common_base_type (basetype, tt2);
1382 if (trial == error_mark_node)
1384 if (best == NULL_TREE)
1386 else if (best != trial)
1387 return error_mark_node;
1392 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1394 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1395 tree trial = common_base_type (tt1, basetype);
1398 if (trial == error_mark_node)
1400 if (best == NULL_TREE)
1402 else if (best != trial)
1403 return error_mark_node;
1409 /* Subroutines of `comptypes'. */
1411 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1412 equivalent in the sense that functions with those parameter types
1413 can have equivalent types. The two lists must be equivalent,
1416 C++: See comment above about TYPE1, TYPE2. */
1419 compparms (parms1, parms2)
1420 tree parms1, parms2;
1422 register tree t1 = parms1, t2 = parms2;
1424 /* An unspecified parmlist matches any specified parmlist
1425 whose argument types don't need default promotions. */
1429 if (t1 == 0 && t2 == 0)
1431 /* If one parmlist is shorter than the other,
1432 they fail to match. */
1433 if (t1 == 0 || t2 == 0)
1435 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1438 t1 = TREE_CHAIN (t1);
1439 t2 = TREE_CHAIN (t2);
1443 /* This really wants return whether or not parameter type lists
1444 would make their owning functions assignment compatible or not.
1446 The return value is like for comp_target_types.
1448 This should go away, possibly with the exception of the empty parmlist
1449 conversion; there are no conversions between function types in C++.
1450 (jason 17 Apr 1997) */
1453 comp_target_parms (parms1, parms2, strict)
1454 tree parms1, parms2;
1457 register tree t1 = parms1, t2 = parms2;
1458 int warn_contravariance = 0;
1460 /* In C, an unspecified parmlist matches any specified parmlist
1461 whose argument types don't need default promotions. This is not
1462 true for C++, but let's do it anyway for unfixed headers. */
1464 if (t1 == 0 && t2 != 0)
1466 if (! flag_strict_prototype && t2 == void_list_node)
1467 /* t1 might be the arglist of a function pointer in extern "C"
1468 declared to take (), which we fudged to (...). Don't make the
1469 user pay for our mistake. */;
1471 cp_pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1473 return self_promoting_args_p (t2);
1476 return self_promoting_args_p (t1);
1478 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1482 /* If one parmlist is shorter than the other,
1483 they fail to match, unless STRICT is <= 0. */
1484 if (t1 == 0 || t2 == 0)
1489 return 1 + warn_contravariance;
1490 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1492 p1 = TREE_VALUE (t1);
1493 p2 = TREE_VALUE (t2);
1494 if (same_type_p (p1, p2))
1500 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1501 || (TREE_CODE (p1) == REFERENCE_TYPE
1502 && TREE_CODE (p2) == REFERENCE_TYPE))
1505 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1506 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1509 /* The following is wrong for contravariance,
1510 but many programs depend on it. */
1511 if (TREE_TYPE (p1) == void_type_node)
1513 if (TREE_TYPE (p2) == void_type_node)
1515 warn_contravariance = 1;
1518 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1519 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1523 /* Note backwards order due to contravariance. */
1524 if (comp_target_types (p2, p1, 1) <= 0)
1526 if (comp_target_types (p1, p2, 1) > 0)
1528 warn_contravariance = 1;
1535 return warn_contravariance ? -1 : 1;
1538 /* Compute the value of the `sizeof' operator. */
1544 enum tree_code code = TREE_CODE (type);
1546 if (processing_template_decl)
1547 return build_min (SIZEOF_EXPR, sizetype, type);
1549 if (code == FUNCTION_TYPE)
1551 if (pedantic || warn_pointer_arith)
1552 pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
1553 return size_one_node;
1555 if (code == METHOD_TYPE)
1557 if (pedantic || warn_pointer_arith)
1558 pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
1559 return size_one_node;
1561 if (code == VOID_TYPE)
1563 if (pedantic || warn_pointer_arith)
1564 pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
1565 return size_one_node;
1567 if (code == ERROR_MARK)
1568 return size_one_node;
1570 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1571 referenced object.'' */
1572 if (code == REFERENCE_TYPE)
1573 type = TREE_TYPE (type);
1575 if (code == OFFSET_TYPE)
1577 cp_error ("`sizeof' applied to non-static member");
1578 return size_zero_node;
1581 if (!COMPLETE_TYPE_P (complete_type (type)))
1583 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1584 return size_zero_node;
1587 /* Convert in case a char is more than one unit. */
1588 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1589 size_int (TYPE_PRECISION (char_type_node)
1597 if (processing_template_decl)
1598 return build_min (SIZEOF_EXPR, sizetype, e);
1600 if (TREE_CODE (e) == COMPONENT_REF
1601 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1602 error ("sizeof applied to a bit-field");
1603 if (is_overloaded_fn (e))
1605 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1606 return size_one_node;
1608 else if (type_unknown_p (e))
1610 incomplete_type_error (e, TREE_TYPE (e));
1611 return size_one_node;
1613 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1614 member unless you're in a non-static member of X. So hand off to
1615 resolve_offset_ref. [expr.prim] */
1616 else if (TREE_CODE (e) == OFFSET_REF)
1617 e = resolve_offset_ref (e);
1619 if (e == error_mark_node)
1622 return c_sizeof (TREE_TYPE (e));
1626 c_sizeof_nowarn (type)
1629 enum tree_code code = TREE_CODE (type);
1631 if (code == FUNCTION_TYPE
1632 || code == METHOD_TYPE
1633 || code == VOID_TYPE
1634 || code == ERROR_MARK)
1635 return size_one_node;
1637 if (code == REFERENCE_TYPE)
1638 type = TREE_TYPE (type);
1640 if (!COMPLETE_TYPE_P (type))
1641 return size_zero_node;
1643 /* Convert in case a char is more than one unit. */
1644 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1645 size_int (TYPE_PRECISION (char_type_node)
1649 /* Implement the __alignof keyword: Return the minimum required
1650 alignment of TYPE, measured in bytes. */
1656 enum tree_code code = TREE_CODE (type);
1659 if (processing_template_decl)
1660 return build_min (ALIGNOF_EXPR, sizetype, type);
1662 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1663 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1665 if (code == VOID_TYPE || code == ERROR_MARK)
1666 return size_one_node;
1668 /* C++: this is really correct! */
1669 if (code == REFERENCE_TYPE)
1670 type = TREE_TYPE (type);
1672 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1673 force_fit_type (t, 0);
1677 /* Perform the array-to-pointer and function-to-pointer conversions
1680 In addition, references are converted to lvalues and manifest
1681 constants are replaced by their values. */
1684 decay_conversion (exp)
1688 register enum tree_code code;
1690 if (TREE_CODE (exp) == OFFSET_REF)
1691 exp = resolve_offset_ref (exp);
1693 type = TREE_TYPE (exp);
1694 code = TREE_CODE (type);
1696 if (code == REFERENCE_TYPE)
1698 exp = convert_from_reference (exp);
1699 type = TREE_TYPE (exp);
1700 code = TREE_CODE (type);
1703 if (type == error_mark_node)
1704 return error_mark_node;
1706 /* Constants can be used directly unless they're not loadable. */
1707 if (TREE_CODE (exp) == CONST_DECL)
1708 exp = DECL_INITIAL (exp);
1709 /* Replace a nonvolatile const static variable with its value. We
1710 don't do this for arrays, though; we want the address of the
1711 first element of the array, not the address of the first element
1712 of its initializing constant. */
1713 else if (code != ARRAY_TYPE)
1715 exp = decl_constant_value (exp);
1716 type = TREE_TYPE (exp);
1719 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1720 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1722 if (code == VOID_TYPE)
1724 error ("void value not ignored as it ought to be");
1725 return error_mark_node;
1727 if (code == METHOD_TYPE)
1728 my_friendly_abort (990506);
1729 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1730 return build_unary_op (ADDR_EXPR, exp, 0);
1731 if (code == ARRAY_TYPE)
1736 if (TREE_CODE (exp) == INDIRECT_REF)
1738 /* Stripping away the INDIRECT_REF is not the right
1739 thing to do for references... */
1740 tree inner = TREE_OPERAND (exp, 0);
1741 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1743 inner = build1 (CONVERT_EXPR,
1744 build_pointer_type (TREE_TYPE
1745 (TREE_TYPE (inner))),
1747 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1749 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1752 if (TREE_CODE (exp) == COMPOUND_EXPR)
1754 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1755 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1756 TREE_OPERAND (exp, 0), op1);
1760 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1762 error ("invalid use of non-lvalue array");
1763 return error_mark_node;
1766 ptrtype = build_pointer_type (TREE_TYPE (type));
1768 if (TREE_CODE (exp) == VAR_DECL)
1770 /* ??? This is not really quite correct
1771 in that the type of the operand of ADDR_EXPR
1772 is not the target type of the type of the ADDR_EXPR itself.
1773 Question is, can this lossage be avoided? */
1774 adr = build1 (ADDR_EXPR, ptrtype, exp);
1775 if (mark_addressable (exp) == 0)
1776 return error_mark_node;
1777 TREE_CONSTANT (adr) = staticp (exp);
1778 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1781 /* This way is better for a COMPONENT_REF since it can
1782 simplify the offset for a component. */
1783 adr = build_unary_op (ADDR_EXPR, exp, 1);
1784 return cp_convert (ptrtype, adr);
1787 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1788 rvalues always have cv-unqualified types. */
1789 if (! CLASS_TYPE_P (type))
1790 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1796 default_conversion (exp)
1800 enum tree_code code;
1802 exp = decay_conversion (exp);
1804 type = TREE_TYPE (exp);
1805 code = TREE_CODE (type);
1807 if (INTEGRAL_CODE_P (code))
1809 tree t = type_promotes_to (type);
1811 return cp_convert (t, exp);
1817 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1821 inline_conversion (exp)
1824 if (TREE_CODE (exp) == FUNCTION_DECL)
1825 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1830 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1831 decay_conversion to one. */
1834 string_conv_p (totype, exp, warn)
1840 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1843 t = TREE_TYPE (totype);
1844 if (!same_type_p (t, char_type_node)
1845 && !same_type_p (t, wchar_type_node))
1848 if (TREE_CODE (exp) == STRING_CST)
1850 /* Make sure that we don't try to convert between char and wchar_t. */
1851 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1856 /* Is this a string constant which has decayed to 'const char *'? */
1857 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1858 if (!same_type_p (TREE_TYPE (exp), t))
1861 if (TREE_CODE (exp) != ADDR_EXPR
1862 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1866 /* This warning is not very useful, as it complains about printf. */
1867 if (warn && warn_write_strings)
1868 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1874 build_object_ref (datum, basetype, field)
1875 tree datum, basetype, field;
1878 if (datum == error_mark_node)
1879 return error_mark_node;
1881 dtype = TREE_TYPE (datum);
1882 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1883 dtype = TREE_TYPE (dtype);
1884 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1886 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1887 basetype, field, dtype);
1888 return error_mark_node;
1890 else if (is_aggr_type (basetype, 1))
1892 tree binfo = binfo_or_else (basetype, dtype);
1894 return build_x_component_ref (build_scoped_ref (datum, basetype),
1897 return error_mark_node;
1900 /* Like `build_component_ref, but uses an already found field, and converts
1901 from a reference. Must compute access for current_class_ref.
1905 build_component_ref_1 (datum, field, protect)
1909 return convert_from_reference
1910 (build_component_ref (datum, field, NULL_TREE, protect));
1913 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1914 can, for example, use as an lvalue. This code used to be in
1915 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1916 expressions, where we're dealing with aggregates. But now it's again only
1917 called from unary_complex_lvalue. The case (in particular) that led to
1918 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1922 rationalize_conditional_expr (code, t)
1923 enum tree_code code;
1926 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1927 the first operand is always the one to be used if both operands
1928 are equal, so we know what conditional expression this used to be. */
1929 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1932 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1933 ? LE_EXPR : GE_EXPR),
1934 TREE_OPERAND (t, 0),
1935 TREE_OPERAND (t, 1)),
1936 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1937 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1941 build_conditional_expr (TREE_OPERAND (t, 0),
1942 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1943 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1946 /* Given the TYPE of an anonymous union field inside T, return the
1947 FIELD_DECL for the field. If not found return NULL_TREE. Because
1948 anonymous unions can nest, we must also search all anonymous unions
1949 that are directly reachable. */
1952 lookup_anon_field (t, type)
1957 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1959 if (TREE_STATIC (field))
1961 if (TREE_CODE (field) != FIELD_DECL)
1964 /* If we find it directly, return the field. */
1965 if (DECL_NAME (field) == NULL_TREE
1966 && type == TREE_TYPE (field))
1971 /* Otherwise, it could be nested, search harder. */
1972 if (DECL_NAME (field) == NULL_TREE
1973 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1975 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1983 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1984 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1985 that we are interested in, or it can be a FIELD_DECL. */
1988 build_component_ref (datum, component, basetype_path, protect)
1989 tree datum, component, basetype_path;
1992 register tree basetype;
1993 register enum tree_code code;
1994 register tree field = NULL;
1999 if (processing_template_decl)
2000 return build_min_nt (COMPONENT_REF, datum, component);
2002 if (datum == error_mark_node
2003 || TREE_TYPE (datum) == error_mark_node)
2004 return error_mark_node;
2006 /* BASETYPE holds the type of the class containing the COMPONENT. */
2007 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2009 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2011 switch (TREE_CODE (datum))
2015 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2016 basetype_path, protect);
2017 return build (COMPOUND_EXPR, TREE_TYPE (value),
2018 TREE_OPERAND (datum, 0), value);
2021 return build_conditional_expr
2022 (TREE_OPERAND (datum, 0),
2023 build_component_ref (TREE_OPERAND (datum, 1), component,
2024 basetype_path, protect),
2025 build_component_ref (TREE_OPERAND (datum, 2), component,
2026 basetype_path, protect));
2029 cp_error ("invalid use of %D", datum);
2030 datum = error_mark_node;
2037 code = TREE_CODE (basetype);
2039 if (code == REFERENCE_TYPE)
2041 datum = convert_from_reference (datum);
2042 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2043 code = TREE_CODE (basetype);
2045 if (TREE_CODE (datum) == OFFSET_REF)
2047 datum = resolve_offset_ref (datum);
2048 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2049 code = TREE_CODE (basetype);
2052 /* First, see if there is a field or component with name COMPONENT. */
2053 if (TREE_CODE (component) == TREE_LIST)
2055 /* I could not trigger this code. MvL */
2056 my_friendly_abort (980326);
2058 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2059 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2061 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2064 if (! IS_AGGR_TYPE_CODE (code))
2066 if (code != ERROR_MARK)
2067 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2068 component, datum, basetype);
2069 return error_mark_node;
2072 if (!complete_type_or_else (basetype, datum))
2073 return error_mark_node;
2075 if (TREE_CODE (component) == BIT_NOT_EXPR)
2077 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2079 cp_error ("destructor specifier `%T::~%T' must have matching names",
2080 basetype, TREE_OPERAND (component, 0));
2081 return error_mark_node;
2083 if (! TYPE_HAS_DESTRUCTOR (basetype))
2085 cp_error ("type `%T' has no destructor", basetype);
2086 return error_mark_node;
2088 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2091 /* Look up component name in the structure type definition. */
2092 if (TYPE_VFIELD (basetype)
2093 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2094 /* Special-case this because if we use normal lookups in an ambiguous
2095 hierarchy, the compiler will abort (because vptr lookups are
2096 not supposed to be ambiguous. */
2097 field = TYPE_VFIELD (basetype);
2098 else if (TREE_CODE (component) == FIELD_DECL)
2100 else if (TREE_CODE (component) == TYPE_DECL)
2102 cp_error ("invalid use of type decl `%#D' as expression", component);
2103 return error_mark_node;
2105 else if (TREE_CODE (component) == TEMPLATE_DECL)
2107 cp_error ("invalid use of template `%#D' as expression", component);
2108 return error_mark_node;
2112 tree name = component;
2113 if (TREE_CODE (component) == VAR_DECL)
2114 name = DECL_NAME (component);
2115 if (TREE_CODE (component) == NAMESPACE_DECL)
2116 /* Source is in error, but produce a sensible diagnostic. */
2117 name = DECL_NAME (component);
2118 if (basetype_path == NULL_TREE)
2119 basetype_path = TYPE_BINFO (basetype);
2120 field = lookup_field (basetype_path, name,
2121 protect && !VFIELD_NAME_P (name), 0);
2122 if (field == error_mark_node)
2123 return error_mark_node;
2125 if (field == NULL_TREE)
2127 /* Not found as a data field, look for it as a method. If found,
2128 then if this is the only possible one, return it, else
2129 report ambiguity error. */
2130 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2131 if (fndecls == error_mark_node)
2132 return error_mark_node;
2135 /* If the function is unique and static, we can resolve it
2136 now. Otherwise, we have to wait and see what context it is
2137 used in; a component_ref involving a non-static member
2138 function can only be used in a call (expr.ref). */
2140 if (TREE_CHAIN (fndecls) == NULL_TREE
2141 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2143 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2145 tree fndecl = TREE_VALUE (fndecls);
2146 enforce_access (basetype_path, fndecl);
2152 /* A unique non-static member function. Other parts
2153 of the compiler expect something with
2154 unknown_type_node to be really overloaded, so
2156 TREE_VALUE (fndecls)
2157 = ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2161 ref = build (COMPONENT_REF, unknown_type_node,
2162 datum, TREE_VALUE (fndecls));
2166 cp_error ("`%#T' has no member named `%D'", basetype, name);
2167 return error_mark_node;
2169 else if (TREE_TYPE (field) == error_mark_node)
2170 return error_mark_node;
2172 if (TREE_CODE (field) != FIELD_DECL)
2174 if (TREE_CODE (field) == TYPE_DECL)
2175 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2176 else if (DECL_RTL (field) != 0)
2179 TREE_USED (field) = 1;
2184 /* See if we have to do any conversions so that we pick up the field from the
2186 if (DECL_FIELD_CONTEXT (field) != basetype)
2188 tree context = DECL_FIELD_CONTEXT (field);
2189 tree base = context;
2190 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2191 && ANON_AGGR_TYPE_P (base))
2193 base = TYPE_CONTEXT (base);
2196 /* Handle base classes here... */
2197 if (base != basetype && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
2199 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2200 if (integer_zerop (addr))
2202 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2203 return error_mark_node;
2205 if (VBASE_NAME_P (DECL_NAME (field)))
2207 /* It doesn't matter which vbase pointer we grab, just
2208 find one of them. */
2209 tree binfo = get_binfo (base,
2210 TREE_TYPE (TREE_TYPE (addr)), 0);
2211 addr = convert_pointer_to_real (binfo, addr);
2214 addr = convert_pointer_to (base, addr);
2215 datum = build_indirect_ref (addr, NULL_PTR);
2216 if (datum == error_mark_node)
2217 return error_mark_node;
2221 /* Handle things from anon unions here... */
2222 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2224 tree subfield = lookup_anon_field (basetype, context);
2225 tree subdatum = build_component_ref (datum, subfield,
2226 basetype_path, protect);
2227 return build_component_ref (subdatum, field, basetype_path, protect);
2231 /* Compute the type of the field, as described in [expr.ref]. */
2232 type_quals = TYPE_UNQUALIFIED;
2233 field_type = TREE_TYPE (field);
2234 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2235 /* The standard says that the type of the result should be the
2236 type referred to by the reference. But for now, at least, we
2237 do the conversion from reference type later. */
2241 type_quals = (CP_TYPE_QUALS (field_type)
2242 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2244 /* A field is const (volatile) if the enclosing object, or the
2245 field itself, is const (volatile). But, a mutable field is
2246 not const, even within a const object. */
2247 if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2248 type_quals &= ~TYPE_QUAL_CONST;
2249 field_type = cp_build_qualified_type (field_type, type_quals);
2252 ref = fold (build (COMPONENT_REF, field_type,
2253 break_out_cleanups (datum), field));
2255 /* Mark the expression const or volatile, as appropriate. Even
2256 though we've dealt with the type above, we still have to mark the
2257 expression itself. */
2258 if (type_quals & TYPE_QUAL_CONST)
2259 TREE_READONLY (ref) = 1;
2260 else if (type_quals & TYPE_QUAL_VOLATILE)
2261 TREE_THIS_VOLATILE (ref) = 1;
2266 /* Variant of build_component_ref for use in expressions, which should
2267 never have REFERENCE_TYPE. */
2270 build_x_component_ref (datum, component, basetype_path, protect)
2271 tree datum, component, basetype_path;
2274 tree t = build_component_ref (datum, component, basetype_path, protect);
2276 if (! processing_template_decl)
2277 t = convert_from_reference (t);
2282 /* Given an expression PTR for a pointer, return an expression
2283 for the value pointed to.
2284 ERRORSTRING is the name of the operator to appear in error messages.
2286 This function may need to overload OPERATOR_FNNAME.
2287 Must also handle REFERENCE_TYPEs for C++. */
2290 build_x_indirect_ref (ptr, errorstring)
2292 const char *errorstring;
2296 if (processing_template_decl)
2297 return build_min_nt (INDIRECT_REF, ptr);
2299 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2303 return build_indirect_ref (ptr, errorstring);
2307 build_indirect_ref (ptr, errorstring)
2309 const char *errorstring;
2311 register tree pointer, type;
2313 if (ptr == error_mark_node)
2314 return error_mark_node;
2316 if (ptr == current_class_ptr)
2317 return current_class_ref;
2319 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2320 ? ptr : default_conversion (ptr));
2321 type = TREE_TYPE (pointer);
2323 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2327 If the type of the expression is "pointer to T," the type
2328 of the result is "T."
2330 We must use the canonical variant because certain parts of
2331 the back end, like fold, do pointer comparisons between
2333 tree t = canonical_type_variant (TREE_TYPE (type));
2335 if (same_type_p (TYPE_MAIN_VARIANT (t), void_type_node))
2337 /* A pointer to incomplete type (other than cv void) can be
2338 dereferenced [expr.unary.op]/1 */
2339 cp_error ("`%T' is not a pointer-to-object type", type);
2340 return error_mark_node;
2342 else if (TREE_CODE (pointer) == ADDR_EXPR
2344 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2345 /* The POINTER was something like `&x'. We simplify `*&x' to
2347 return TREE_OPERAND (pointer, 0);
2350 tree ref = build1 (INDIRECT_REF, t, pointer);
2352 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2353 so that we get the proper error message if the result is used
2354 to assign to. Also, &* is supposed to be a no-op. */
2355 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2356 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2357 TREE_SIDE_EFFECTS (ref)
2358 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2363 /* `pointer' won't be an error_mark_node if we were given a
2364 pointer to member, so it's cool to check for this here. */
2365 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2366 error ("invalid use of `%s' on pointer to member", errorstring);
2367 else if (pointer != error_mark_node)
2370 error ("invalid type argument of `%s'", errorstring);
2372 error ("invalid type argument");
2374 return error_mark_node;
2377 /* This handles expressions of the form "a[i]", which denotes
2380 This is logically equivalent in C to *(a+i), but we may do it differently.
2381 If A is a variable or a member, we generate a primitive ARRAY_REF.
2382 This avoids forcing the array out of registers, and can work on
2383 arrays that are not lvalues (for example, members of structures returned
2386 If INDEX is of some user-defined type, it must be converted to
2387 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2388 will inherit the type of the array, which will be some pointer type. */
2391 build_array_ref (array, idx)
2396 error ("subscript missing in array reference");
2397 return error_mark_node;
2400 if (TREE_TYPE (array) == error_mark_node
2401 || TREE_TYPE (idx) == error_mark_node)
2402 return error_mark_node;
2404 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2405 && TREE_CODE (array) != INDIRECT_REF)
2409 /* Subscripting with type char is likely to lose
2410 on a machine where chars are signed.
2411 So warn on any machine, but optionally.
2412 Don't warn for unsigned char since that type is safe.
2413 Don't warn for signed char because anyone who uses that
2414 must have done so deliberately. */
2415 if (warn_char_subscripts
2416 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2417 warning ("array subscript has type `char'");
2419 /* Apply default promotions *after* noticing character types. */
2420 idx = default_conversion (idx);
2422 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2424 error ("array subscript is not an integer");
2425 return error_mark_node;
2428 /* An array that is indexed by a non-constant
2429 cannot be stored in a register; we must be able to do
2430 address arithmetic on its address.
2431 Likewise an array of elements of variable size. */
2432 if (TREE_CODE (idx) != INTEGER_CST
2433 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2434 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2437 if (mark_addressable (array) == 0)
2438 return error_mark_node;
2440 /* An array that is indexed by a constant value which is not within
2441 the array bounds cannot be stored in a register either; because we
2442 would get a crash in store_bit_field/extract_bit_field when trying
2443 to access a non-existent part of the register. */
2444 if (TREE_CODE (idx) == INTEGER_CST
2445 && TYPE_VALUES (TREE_TYPE (array))
2446 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2448 if (mark_addressable (array) == 0)
2449 return error_mark_node;
2452 if (pedantic && !lvalue_p (array))
2453 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2455 /* Note in C++ it is valid to subscript a `register' array, since
2456 it is valid to take the address of something with that
2457 storage specification. */
2461 while (TREE_CODE (foo) == COMPONENT_REF)
2462 foo = TREE_OPERAND (foo, 0);
2463 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2464 warning ("subscripting array declared `register'");
2467 type = TREE_TYPE (TREE_TYPE (array));
2468 rval = build (ARRAY_REF, type, array, idx);
2469 /* Array ref is const/volatile if the array elements are
2470 or if the array is.. */
2471 TREE_READONLY (rval)
2472 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2473 TREE_SIDE_EFFECTS (rval)
2474 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2475 TREE_THIS_VOLATILE (rval)
2476 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2477 return require_complete_type (fold (rval));
2481 tree ar = default_conversion (array);
2482 tree ind = default_conversion (idx);
2484 /* Put the integer in IND to simplify error checking. */
2485 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2492 if (ar == error_mark_node)
2495 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2497 error ("subscripted value is neither array nor pointer");
2498 return error_mark_node;
2500 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2502 error ("array subscript is not an integer");
2503 return error_mark_node;
2506 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind),
2511 /* Build a function call to function FUNCTION with parameters PARAMS.
2512 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2513 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2514 not include any object pointer that may be required. FUNCTION's
2515 data type may be a function type or a pointer-to-function.
2517 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2518 is the list of possible methods that FUNCTION could conceivably
2519 be. If the list of methods comes from a class, then it will be
2520 a list of lists (where each element is associated with the class
2521 that produced it), otherwise it will be a simple list (for
2522 functions overloaded in global scope).
2524 In the first case, TREE_VALUE (function) is the head of one of those
2525 lists, and TREE_PURPOSE is the name of the function.
2527 In the second case, TREE_PURPOSE (function) is the function's
2530 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2532 When calling a TEMPLATE_DECL, we don't require a complete return
2536 build_x_function_call (function, params, decl)
2537 tree function, params, decl;
2540 tree template_id = NULL_TREE;
2543 if (function == error_mark_node)
2544 return error_mark_node;
2546 if (processing_template_decl)
2547 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2549 /* Save explicit template arguments if found */
2550 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2552 template_id = function;
2553 function = TREE_OPERAND (function, 0);
2556 type = TREE_TYPE (function);
2558 if (TREE_CODE (type) == OFFSET_TYPE
2559 && TREE_TYPE (type) == unknown_type_node
2560 && TREE_CODE (function) == TREE_LIST
2561 && TREE_CHAIN (function) == NULL_TREE)
2563 /* Undo (Foo:bar)()... */
2564 type = TYPE_OFFSET_BASETYPE (type);
2565 function = TREE_VALUE (function);
2566 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2567 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2568 function = TREE_VALUE (function);
2569 if (TREE_CODE (function) == OVERLOAD)
2570 function = OVL_FUNCTION (function);
2571 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2572 function = DECL_NAME (function);
2573 return build_method_call (decl, function, params,
2574 TYPE_BINFO (type), LOOKUP_NORMAL);
2577 if (TREE_CODE (function) == OFFSET_REF
2578 && TREE_CODE (type) != METHOD_TYPE)
2579 function = resolve_offset_ref (function);
2581 if ((TREE_CODE (function) == FUNCTION_DECL
2582 && DECL_STATIC_FUNCTION_P (function))
2583 || (TREE_CODE (function) == TEMPLATE_DECL
2584 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function))))
2585 return build_member_call (DECL_CONTEXT (function),
2587 ? template_id : DECL_NAME (function),
2590 is_method = ((TREE_CODE (function) == TREE_LIST
2591 && current_class_type != NULL_TREE
2592 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2594 || (TREE_CODE (function) == OVERLOAD
2595 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2596 || TREE_CODE (function) == IDENTIFIER_NODE
2597 || TREE_CODE (type) == METHOD_TYPE
2598 || TYPE_PTRMEMFUNC_P (type));
2600 /* A friend template. Make it look like a toplevel declaration. */
2601 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2602 function = ovl_cons (function, NULL_TREE);
2604 /* Handle methods, friends, and overloaded functions, respectively. */
2607 tree basetype = NULL_TREE;
2609 if (TREE_CODE (function) == OVERLOAD)
2610 function = OVL_CURRENT (function);
2612 if (TREE_CODE (function) == FUNCTION_DECL
2613 || DECL_FUNCTION_TEMPLATE_P (function))
2615 basetype = DECL_CONTEXT (function);
2617 if (DECL_NAME (function))
2618 function = DECL_NAME (function);
2620 function = TYPE_IDENTIFIER (DECL_CONTEXT (function));
2622 else if (TREE_CODE (function) == TREE_LIST)
2624 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2625 == FUNCTION_DECL, 312);
2626 basetype = DECL_CONTEXT (TREE_VALUE (function));
2627 function = TREE_PURPOSE (function);
2629 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2631 if (TREE_CODE (function) == OFFSET_REF)
2633 if (TREE_OPERAND (function, 0))
2634 decl = TREE_OPERAND (function, 0);
2636 /* Call via a pointer to member function. */
2637 if (decl == NULL_TREE)
2639 error ("pointer to member function called, but not in class scope");
2640 return error_mark_node;
2642 /* What other type of POINTER_TYPE could this be? */
2643 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2644 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2645 && TREE_CODE (function) != OFFSET_REF)
2646 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2651 /* this is an abbreviated method call.
2652 must go through here in case it is a virtual function.
2653 @@ Perhaps this could be optimized. */
2655 if (basetype && (! current_class_type
2656 || ! DERIVED_FROM_P (basetype, current_class_type)))
2657 return build_member_call (basetype, function, params);
2659 if (decl == NULL_TREE)
2661 if (current_class_type == NULL_TREE)
2663 cp_error ("object missing in call to method `%D'", function);
2664 return error_mark_node;
2666 /* Yow: call from a static member function. */
2667 decl = build_dummy_object (current_class_type);
2670 /* Put back explicit template arguments, if any. */
2672 function = template_id;
2673 return build_method_call (decl, function, params,
2674 NULL_TREE, LOOKUP_NORMAL);
2676 else if (TREE_CODE (function) == COMPONENT_REF
2677 && type == unknown_type_node)
2679 /* Undo what we did in build_component_ref. */
2680 decl = TREE_OPERAND (function, 0);
2681 function = TREE_OPERAND (function, 1);
2682 function = DECL_NAME (OVL_CURRENT (function));
2686 TREE_OPERAND (template_id, 0) = function;
2687 function = template_id;
2690 return build_method_call (decl, function, params,
2691 NULL_TREE, LOOKUP_NORMAL);
2693 else if (really_overloaded_fn (function))
2695 if (OVL_FUNCTION (function) == NULL_TREE)
2697 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2698 TREE_PURPOSE (function));
2699 return error_mark_node;
2703 /* Put back explicit template arguments, if any. */
2705 function = template_id;
2706 return build_new_function_call (function, params);
2710 /* Remove a potential OVERLOAD around it */
2711 function = OVL_CURRENT (function);
2714 if (TREE_CODE (function) == OFFSET_REF)
2716 /* If the component is a data element (or a virtual function), we play
2717 games here to make things work. */
2720 if (TREE_OPERAND (function, 0))
2721 decl = TREE_OPERAND (function, 0);
2723 decl = current_class_ref;
2725 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2727 /* Sigh. OFFSET_REFs are being used for too many things.
2728 They're being used both for -> and ->*, and we want to resolve
2729 the -> cases here, but leave the ->*. We could use
2730 resolve_offset_ref for those, too, but it would call
2731 get_member_function_from_ptrfunc and decl_addr wouldn't get
2732 updated properly. Nasty. */
2733 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2734 function = resolve_offset_ref (function);
2736 function = TREE_OPERAND (function, 1);
2738 function = get_member_function_from_ptrfunc (&decl_addr, function);
2739 params = tree_cons (NULL_TREE, decl_addr, params);
2740 return build_function_call (function, params);
2743 type = TREE_TYPE (function);
2744 if (type != error_mark_node)
2746 if (TREE_CODE (type) == REFERENCE_TYPE)
2747 type = TREE_TYPE (type);
2749 if (IS_AGGR_TYPE (type))
2750 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2755 tree fntype = TREE_TYPE (function);
2756 tree ctypeptr = NULL_TREE;
2758 /* Explicitly named method? */
2759 if (TREE_CODE (function) == FUNCTION_DECL)
2760 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2761 /* Expression with ptr-to-method type? It could either be a plain
2762 usage, or it might be a case where the ptr-to-method is being
2763 passed in as an argument. */
2764 else if (TYPE_PTRMEMFUNC_P (fntype))
2766 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2767 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2768 ctypeptr = build_pointer_type (rec);
2770 /* Unexpected node type? */
2772 my_friendly_abort (116);
2773 if (decl == NULL_TREE)
2775 if (current_function_decl
2776 && DECL_STATIC_FUNCTION_P (current_function_decl))
2777 error ("invalid call to member function needing `this' in static member function scope");
2779 error ("pointer to member function called, but not in class scope");
2780 return error_mark_node;
2782 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2783 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2785 decl = build_unary_op (ADDR_EXPR, decl, 0);
2786 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2789 decl = build_c_cast (ctypeptr, decl);
2790 params = tree_cons (NULL_TREE, decl, params);
2793 return build_function_call (function, params);
2796 /* Resolve a pointer to member function. INSTANCE is the object
2797 instance to use, if the member points to a virtual member. */
2800 get_member_function_from_ptrfunc (instance_ptrptr, function)
2801 tree *instance_ptrptr;
2804 if (TREE_CODE (function) == OFFSET_REF)
2806 function = TREE_OPERAND (function, 1);
2809 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2811 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2812 tree instance, basetype;
2814 tree instance_ptr = *instance_ptrptr;
2816 if (instance_ptr == error_mark_node
2817 && TREE_CODE (function) == PTRMEM_CST)
2819 /* Extracting the function address from a pmf is only
2820 allowed with -Wno-pmf-conversions. It only works for
2822 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2823 e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
2827 if (TREE_SIDE_EFFECTS (instance_ptr))
2828 instance_ptr = save_expr (instance_ptr);
2830 if (TREE_SIDE_EFFECTS (function))
2831 function = save_expr (function);
2833 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2834 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2836 /* Convert down to the right base, before using the instance. */
2837 instance = convert_pointer_to_real (basetype, instance_ptr);
2838 if (instance == error_mark_node && instance_ptr != error_mark_node)
2841 e3 = PFN_FROM_PTRMEMFUNC (function);
2843 /* This used to avoid checking for virtual functions if basetype
2844 has no virtual functions, according to an earlier ANSI draft.
2845 With the final ISO C++ rules, such an optimization is
2846 incorrect: A pointer to a derived member can be static_cast
2847 to pointer-to-base-member, as long as the dynamic object
2848 later has the right member. */
2850 /* Promoting idx before saving it improves performance on RISC
2851 targets. Without promoting, the first compare used
2852 load-with-sign-extend, while the second used normal load then
2853 shift to sign-extend. An optimizer flaw, perhaps, but it's
2854 easier to make this change. */
2857 idx = build_binary_op (TRUNC_DIV_EXPR,
2858 build1 (NOP_EXPR, vtable_index_type, e3),
2859 TYPE_SIZE_UNIT (vtable_entry_type));
2860 e1 = build_binary_op (BIT_AND_EXPR,
2861 build1 (NOP_EXPR, vtable_index_type, e3),
2866 idx = save_expr (default_conversion
2867 (build_component_ref (function,
2870 e1 = build_binary_op (GE_EXPR, idx, integer_zero_node);
2871 idx = build_binary_op (MINUS_EXPR, idx, integer_one_node);
2874 vtbl = convert_pointer_to (ptr_type_node, instance);
2875 delta = cp_convert (ptrdiff_type_node,
2876 build_component_ref (function, delta_identifier,
2879 /* DELTA2 is the amount by which to adjust the `this' pointer
2880 to find the vtbl. */
2883 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2886 build_pointer_type (build_pointer_type (vtable_entry_type)),
2887 vtbl, cp_convert (ptrdiff_type_node, delta2));
2888 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2889 aref = build_array_ref (vtbl, idx);
2891 if (! flag_vtable_thunks)
2893 aref = save_expr (aref);
2895 delta = build_binary_op
2897 build_conditional_expr (e1,
2898 build_component_ref (aref,
2905 if (flag_vtable_thunks)
2908 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2909 TREE_TYPE (e2) = TREE_TYPE (e3);
2910 e1 = build_conditional_expr (e1, e2, e3);
2912 /* Make sure this doesn't get evaluated first inside one of the
2913 branches of the COND_EXPR. */
2914 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2915 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2918 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2919 instance_ptr, delta);
2921 if (instance_ptr == error_mark_node
2922 && TREE_CODE (e1) != ADDR_EXPR
2923 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2924 cp_error ("object missing in `%E'", function);
2932 build_function_call_real (function, params, require_complete, flags)
2933 tree function, params;
2934 int require_complete, flags;
2936 register tree fntype, fndecl;
2937 register tree value_type;
2938 register tree coerced_params;
2940 tree name = NULL_TREE, assembler_name = NULL_TREE;
2943 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2944 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2945 if (TREE_CODE (function) == NOP_EXPR
2946 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2947 function = TREE_OPERAND (function, 0);
2949 if (TREE_CODE (function) == FUNCTION_DECL)
2951 name = DECL_NAME (function);
2952 assembler_name = DECL_ASSEMBLER_NAME (function);
2954 GNU_xref_call (current_function_decl,
2955 IDENTIFIER_POINTER (name ? name
2956 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2958 mark_used (function);
2961 /* Convert anything with function type to a pointer-to-function. */
2962 if (pedantic && DECL_MAIN_P (function))
2963 pedwarn ("ISO C++ forbids calling `::main' from within program");
2965 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2966 (because calling an inline function does not mean the function
2967 needs to be separately compiled). */
2969 if (DECL_INLINE (function))
2970 function = inline_conversion (function);
2972 function = build_addr_func (function);
2978 function = build_addr_func (function);
2981 if (function == error_mark_node)
2982 return error_mark_node;
2984 fntype = TREE_TYPE (function);
2986 if (TYPE_PTRMEMFUNC_P (fntype))
2988 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2990 return error_mark_node;
2993 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2994 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2996 if (!((TREE_CODE (fntype) == POINTER_TYPE
2997 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2999 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3001 cp_error ("`%E' cannot be used as a function", function);
3002 return error_mark_node;
3005 /* fntype now gets the type of function pointed to. */
3006 fntype = TREE_TYPE (fntype);
3008 /* Convert the parameters to the types declared in the
3009 function prototype, or apply default promotions. */
3011 if (flags & LOOKUP_COMPLAIN)
3012 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3013 params, fndecl, LOOKUP_NORMAL);
3015 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3018 if (coerced_params == error_mark_node)
3020 if (flags & LOOKUP_SPECULATIVELY)
3023 return error_mark_node;
3026 /* Check for errors in format strings. */
3028 if (warn_format && (name || assembler_name))
3029 check_function_format (name, assembler_name, coerced_params);
3031 /* Recognize certain built-in functions so we can make tree-codes
3032 other than CALL_EXPR. We do this when it enables fold-const.c
3033 to do something useful. */
3035 if (TREE_CODE (function) == ADDR_EXPR
3036 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3037 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3039 result = expand_tree_builtin (TREE_OPERAND (function, 0),
3040 params, coerced_params);
3046 result = build_call (function, coerced_params);
3047 value_type = TREE_TYPE (result);
3049 if (require_complete)
3051 if (TREE_CODE (value_type) == VOID_TYPE)
3053 result = require_complete_type (result);
3055 if (IS_AGGR_TYPE (value_type))
3056 result = build_cplus_new (value_type, result);
3057 return convert_from_reference (result);
3061 build_function_call (function, params)
3062 tree function, params;
3064 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3067 /* Convert the actual parameter expressions in the list VALUES
3068 to the types in the list TYPELIST.
3069 If parmdecls is exhausted, or when an element has NULL as its type,
3070 perform the default conversions.
3072 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3074 This is also where warnings about wrong number of args are generated.
3076 Return a list of expressions for the parameters as converted.
3078 Both VALUES and the returned value are chains of TREE_LIST nodes
3079 with the elements of the list in the TREE_VALUE slots of those nodes.
3081 In C++, unspecified trailing parameters can be filled in with their
3082 default arguments, if such were specified. Do so here. */
3085 convert_arguments (typelist, values, fndecl, flags)
3086 tree typelist, values, fndecl;
3089 register tree typetail, valtail;
3090 register tree result = NULL_TREE;
3091 const char *called_thing = 0;
3094 /* Argument passing is always copy-initialization. */
3095 flags |= LOOKUP_ONLYCONVERTING;
3099 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3101 if (DECL_NAME (fndecl) == NULL_TREE
3102 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3103 called_thing = "constructor";
3105 called_thing = "member function";
3108 called_thing = "function";
3111 for (valtail = values, typetail = typelist;
3113 valtail = TREE_CHAIN (valtail), i++)
3115 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3116 register tree val = TREE_VALUE (valtail);
3118 if (val == error_mark_node)
3119 return error_mark_node;
3121 if (type == void_type_node)
3125 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3127 error ("at this point in file");
3130 error ("too many arguments to function");
3131 /* In case anybody wants to know if this argument
3134 TREE_TYPE (tree_last (result)) = error_mark_node;
3138 if (TREE_CODE (val) == OFFSET_REF)
3139 val = resolve_offset_ref (val);
3141 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3142 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3143 if (TREE_CODE (val) == NOP_EXPR
3144 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3145 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3146 val = TREE_OPERAND (val, 0);
3148 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3150 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3151 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3152 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3153 val = default_conversion (val);
3156 if (val == error_mark_node)
3157 return error_mark_node;
3161 /* Formal parm type is specified by a function prototype. */
3164 if (!COMPLETE_TYPE_P (complete_type (type)))
3166 error ("parameter type of called function is incomplete");
3171 parmval = convert_for_initialization
3172 (NULL_TREE, type, val, flags,
3173 "argument passing", fndecl, i);
3174 if (PROMOTE_PROTOTYPES
3175 && (TREE_CODE (type) == INTEGER_TYPE
3176 || TREE_CODE (type) == ENUMERAL_TYPE)
3177 && (TYPE_PRECISION (type)
3178 < TYPE_PRECISION (integer_type_node)))
3179 parmval = default_conversion (parmval);
3182 if (parmval == error_mark_node)
3183 return error_mark_node;
3185 result = tree_cons (NULL_TREE, parmval, result);
3189 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3190 val = convert_from_reference (val);
3192 result = tree_cons (NULL_TREE,
3193 convert_arg_to_ellipsis (val),
3198 typetail = TREE_CHAIN (typetail);
3201 if (typetail != 0 && typetail != void_list_node)
3203 /* See if there are default arguments that can be used */
3204 if (TREE_PURPOSE (typetail))
3206 for (; typetail != void_list_node; ++i)
3209 = convert_default_arg (TREE_VALUE (typetail),
3210 TREE_PURPOSE (typetail),
3213 if (parmval == error_mark_node)
3214 return error_mark_node;
3216 result = tree_cons (0, parmval, result);
3217 typetail = TREE_CHAIN (typetail);
3218 /* ends with `...'. */
3219 if (typetail == NULL_TREE)
3227 cp_error_at ("too few arguments to %s `%+#D'",
3228 called_thing, fndecl);
3229 error ("at this point in file");
3232 error ("too few arguments to function");
3233 return error_mark_list;
3237 return nreverse (result);
3240 /* Build a binary-operation expression, after performing default
3241 conversions on the operands. CODE is the kind of expression to build. */
3244 build_x_binary_op (code, arg1, arg2)
3245 enum tree_code code;
3248 if (processing_template_decl)
3249 return build_min_nt (code, arg1, arg2);
3251 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3254 /* Build a binary-operation expression without default conversions.
3255 CODE is the kind of expression to build.
3256 This function differs from `build' in several ways:
3257 the data type of the result is computed and recorded in it,
3258 warnings are generated if arg data types are invalid,
3259 special handling for addition and subtraction of pointers is known,
3260 and some optimization is done (operations on narrow ints
3261 are done in the narrower type when that gives the same result).
3262 Constant folding is also done before the result is returned.
3264 Note that the operands will never have enumeral types
3265 because either they have just had the default conversions performed
3266 or they have both just been converted to some other type in which
3267 the arithmetic is to be done.
3269 C++: must do special pointer arithmetic when implementing
3270 multiple inheritance, and deal with pointer to member functions. */
3273 build_binary_op (code, orig_op0, orig_op1)
3274 enum tree_code code;
3275 tree orig_op0, orig_op1;
3278 register enum tree_code code0, code1;
3281 /* Expression code to give to the expression when it is built.
3282 Normally this is CODE, which is what the caller asked for,
3283 but in some special cases we change it. */
3284 register enum tree_code resultcode = code;
3286 /* Data type in which the computation is to be performed.
3287 In the simplest cases this is the common type of the arguments. */
3288 register tree result_type = NULL;
3290 /* Nonzero means operands have already been type-converted
3291 in whatever way is necessary.
3292 Zero means they need to be converted to RESULT_TYPE. */
3295 /* Nonzero means create the expression with this type, rather than
3297 tree build_type = 0;
3299 /* Nonzero means after finally constructing the expression
3300 convert it to this type. */
3301 tree final_type = 0;
3303 /* Nonzero if this is an operation like MIN or MAX which can
3304 safely be computed in short if both args are promoted shorts.
3305 Also implies COMMON.
3306 -1 indicates a bitwise operation; this makes a difference
3307 in the exact conditions for when it is safe to do the operation
3308 in a narrower mode. */
3311 /* Nonzero if this is a comparison operation;
3312 if both args are promoted shorts, compare the original shorts.
3313 Also implies COMMON. */
3314 int short_compare = 0;
3316 /* Nonzero if this is a right-shift operation, which can be computed on the
3317 original short and then promoted if the operand is a promoted short. */
3318 int short_shift = 0;
3320 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3323 /* Apply default conversions. */
3324 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3325 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3326 || code == TRUTH_XOR_EXPR)
3328 op0 = decay_conversion (orig_op0);
3329 op1 = decay_conversion (orig_op1);
3333 op0 = default_conversion (orig_op0);
3334 op1 = default_conversion (orig_op1);
3337 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3338 STRIP_TYPE_NOPS (op0);
3339 STRIP_TYPE_NOPS (op1);
3341 /* DTRT if one side is an overloaded function, but complain about it. */
3342 if (type_unknown_p (op0))
3344 tree t = instantiate_type (TREE_TYPE (op1), op0, 0);
3345 if (t != error_mark_node)
3347 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3352 if (type_unknown_p (op1))
3354 tree t = instantiate_type (TREE_TYPE (op0), op1, 0);
3355 if (t != error_mark_node)
3357 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3363 type0 = TREE_TYPE (op0);
3364 type1 = TREE_TYPE (op1);
3366 /* The expression codes of the data types of the arguments tell us
3367 whether the arguments are integers, floating, pointers, etc. */
3368 code0 = TREE_CODE (type0);
3369 code1 = TREE_CODE (type1);
3371 /* If an error was already reported for one of the arguments,
3372 avoid reporting another error. */
3374 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3375 return error_mark_node;
3380 /* Handle the pointer + int case. */
3381 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3382 return pointer_int_sum (PLUS_EXPR, op0, op1);
3383 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3384 return pointer_int_sum (PLUS_EXPR, op1, op0);
3390 /* Subtraction of two similar pointers.
3391 We must subtract them as integers, then divide by object size. */
3392 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3393 && comp_target_types (type0, type1, 1))
3394 return pointer_diff (op0, op1, common_type (type0, type1));
3395 /* Handle pointer minus int. Just like pointer plus int. */
3396 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3397 return pointer_int_sum (MINUS_EXPR, op0, op1);
3406 case TRUNC_DIV_EXPR:
3408 case FLOOR_DIV_EXPR:
3409 case ROUND_DIV_EXPR:
3410 case EXACT_DIV_EXPR:
3411 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3412 || code0 == COMPLEX_TYPE)
3413 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3414 || code1 == COMPLEX_TYPE))
3416 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3417 cp_warning ("division by zero in `%E / 0'", op0);
3418 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3419 cp_warning ("division by zero in `%E / 0.'", op0);
3421 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3422 resultcode = RDIV_EXPR;
3424 /* When dividing two signed integers, we have to promote to int.
3425 unless we divide by a constant != -1. Note that default
3426 conversion will have been performed on the operands at this
3427 point, so we have to dig out the original type to find out if
3429 shorten = ((TREE_CODE (op0) == NOP_EXPR
3430 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3431 || (TREE_CODE (op1) == INTEGER_CST
3432 && ! integer_all_onesp (op1)));
3439 case BIT_ANDTC_EXPR:
3442 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3444 /* If one operand is a constant, and the other is a short type
3445 that has been converted to an int,
3446 really do the work in the short type and then convert the
3447 result to int. If we are lucky, the constant will be 0 or 1
3448 in the short type, making the entire operation go away. */
3449 if (TREE_CODE (op0) == INTEGER_CST
3450 && TREE_CODE (op1) == NOP_EXPR
3451 && (TYPE_PRECISION (type1)
3452 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3453 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3455 final_type = result_type;
3456 op1 = TREE_OPERAND (op1, 0);
3457 result_type = TREE_TYPE (op1);
3459 if (TREE_CODE (op1) == INTEGER_CST
3460 && TREE_CODE (op0) == NOP_EXPR
3461 && (TYPE_PRECISION (type0)
3462 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3463 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3465 final_type = result_type;
3466 op0 = TREE_OPERAND (op0, 0);
3467 result_type = TREE_TYPE (op0);
3471 case TRUNC_MOD_EXPR:
3472 case FLOOR_MOD_EXPR:
3473 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3474 cp_warning ("division by zero in `%E %% 0'", op0);
3475 else if (code1 == REAL_TYPE && real_zerop (op1))
3476 cp_warning ("division by zero in `%E %% 0.'", op0);
3478 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3480 /* Although it would be tempting to shorten always here, that loses
3481 on some targets, since the modulo instruction is undefined if the
3482 quotient can't be represented in the computation mode. We shorten
3483 only if unsigned or if dividing by something we know != -1. */
3484 shorten = ((TREE_CODE (op0) == NOP_EXPR
3485 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3486 || (TREE_CODE (op1) == INTEGER_CST
3487 && ! integer_all_onesp (op1)));
3492 case TRUTH_ANDIF_EXPR:
3493 case TRUTH_ORIF_EXPR:
3494 case TRUTH_AND_EXPR:
3496 result_type = boolean_type_node;
3499 /* Shift operations: result has same type as first operand;
3500 always convert second operand to int.
3501 Also set SHORT_SHIFT if shifting rightward. */
3504 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3506 result_type = type0;
3507 if (TREE_CODE (op1) == INTEGER_CST)
3509 if (tree_int_cst_lt (op1, integer_zero_node))
3510 warning ("right shift count is negative");
3513 if (! integer_zerop (op1))
3515 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3516 warning ("right shift count >= width of type");
3519 /* Convert the shift-count to an integer, regardless of
3520 size of value being shifted. */
3521 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3522 op1 = cp_convert (integer_type_node, op1);
3523 /* Avoid converting op1 to result_type later. */
3529 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3531 result_type = type0;
3532 if (TREE_CODE (op1) == INTEGER_CST)
3534 if (tree_int_cst_lt (op1, integer_zero_node))
3535 warning ("left shift count is negative");
3536 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3537 warning ("left shift count >= width of type");
3539 /* Convert the shift-count to an integer, regardless of
3540 size of value being shifted. */
3541 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3542 op1 = cp_convert (integer_type_node, op1);
3543 /* Avoid converting op1 to result_type later. */
3550 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3552 result_type = type0;
3553 if (TREE_CODE (op1) == INTEGER_CST)
3555 if (tree_int_cst_lt (op1, integer_zero_node))
3556 warning ("%s rotate count is negative",
3557 (code == LROTATE_EXPR) ? "left" : "right");
3558 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3559 warning ("%s rotate count >= width of type",
3560 (code == LROTATE_EXPR) ? "left" : "right");
3562 /* Convert the shift-count to an integer, regardless of
3563 size of value being shifted. */
3564 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3565 op1 = cp_convert (integer_type_node, op1);
3571 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3572 warning ("comparing floating point with == or != is unsafe");
3574 build_type = boolean_type_node;
3575 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3576 || code0 == COMPLEX_TYPE)
3577 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3578 || code1 == COMPLEX_TYPE))
3580 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3582 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3583 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3585 if (comp_target_types (type0, type1, 1))
3586 result_type = common_type (type0, type1);
3587 else if (tt0 == void_type_node)
3589 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3590 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3591 pedwarn ("ISO C++ forbids comparison of `void *' with function pointer");
3592 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3593 pedwarn ("ISO C++ forbids conversion of a pointer to member to `void *'");
3595 else if (tt1 == void_type_node)
3597 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3598 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3599 pedwarn ("ISO C++ forbids comparison of `void *' with function pointer");
3602 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3605 if (result_type == NULL_TREE)
3606 result_type = ptr_type_node;
3608 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3609 result_type = type0;
3610 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3611 result_type = type1;
3612 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3614 result_type = type0;
3615 error ("ISO C++ forbids comparison between pointer and integer");
3617 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3619 result_type = type1;
3620 error ("ISO C++ forbids comparison between pointer and integer");
3622 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3626 op0 = build_component_ref (op0, pfn_identifier, NULL_TREE, 0);
3627 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3631 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3632 op1 = integer_zero_node;
3634 result_type = TREE_TYPE (op0);
3636 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3637 return build_binary_op (code, op1, op0);
3638 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3639 && same_type_p (type0, type1))
3641 /* E will be the final comparison. */
3643 /* E1 and E2 are for scratch. */
3652 && (!op0.pfn || op0.delta == op1.delta))
3654 The reason for the `!op0.pfn' bit is that a NULL
3655 pointer-to-member is any member with a zero PFN; the
3656 DELTA field is unspecified. */
3662 pfn0 = pfn_from_ptrmemfunc (op0);
3663 pfn1 = pfn_from_ptrmemfunc (op1);
3664 delta0 = build_component_ref (op0, delta_identifier,
3666 delta1 = build_component_ref (op1, delta_identifier,
3668 e1 = build_binary_op (EQ_EXPR, delta0, delta1);
3669 e2 = build_binary_op (NE_EXPR,
3671 cp_convert (TREE_TYPE (pfn0),
3672 integer_zero_node));
3673 e1 = build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3674 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3675 e = build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3679 /* The code we generate for the test is:
3681 (op0.index == op1.index
3682 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3683 || op0.pfn == op1.pfn)) */
3685 tree index0 = build_component_ref (op0, index_identifier,
3687 tree index1 = save_expr (build_component_ref (op1, index_identifier,
3689 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3690 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3691 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3692 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3694 tree integer_neg_one_node
3695 = build_binary_op (MINUS_EXPR, integer_zero_node,
3697 e1 = build_binary_op (EQ_EXPR, index0, index1);
3698 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3699 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3700 build_binary_op (EQ_EXPR, delta20, delta21));
3701 /* We can't use build_binary_op for this cmp because it would get
3702 confused by the ptr to method types and think we want pmfs. */
3703 e3 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3704 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3705 e = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3707 if (code == EQ_EXPR)
3709 return 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)
3725 if (comp_target_types (type0, type1, 1))
3726 result_type = common_type (type0, type1);
3729 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3731 result_type = ptr_type_node;
3740 build_type = boolean_type_node;
3741 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3742 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3744 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3746 if (comp_target_types (type0, type1, 1))
3747 result_type = common_type (type0, type1);
3750 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3752 result_type = ptr_type_node;
3755 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3756 && integer_zerop (op1))
3757 result_type = type0;
3758 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3759 && integer_zerop (op0))
3760 result_type = type1;
3761 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3763 result_type = type0;
3764 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3766 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3768 result_type = type1;
3769 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3773 case UNORDERED_EXPR:
3780 build_type = integer_type_node;
3781 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3783 error ("unordered comparison on non-floating point argument");
3784 return error_mark_node;
3793 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3795 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3797 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3799 if (shorten || common || short_compare)
3800 result_type = common_type (type0, type1);
3802 /* For certain operations (which identify themselves by shorten != 0)
3803 if both args were extended from the same smaller type,
3804 do the arithmetic in that type and then extend.
3806 shorten !=0 and !=1 indicates a bitwise operation.
3807 For them, this optimization is safe only if
3808 both args are zero-extended or both are sign-extended.
3809 Otherwise, we might change the result.
3810 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3811 but calculated in (unsigned short) it would be (unsigned short)-1. */
3813 if (shorten && none_complex)
3815 int unsigned0, unsigned1;
3816 tree arg0 = get_narrower (op0, &unsigned0);
3817 tree arg1 = get_narrower (op1, &unsigned1);
3818 /* UNS is 1 if the operation to be done is an unsigned one. */
3819 int uns = TREE_UNSIGNED (result_type);
3822 final_type = result_type;
3824 /* Handle the case that OP0 does not *contain* a conversion
3825 but it *requires* conversion to FINAL_TYPE. */
3827 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3828 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3829 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3830 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3832 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3834 /* For bitwise operations, signedness of nominal type
3835 does not matter. Consider only how operands were extended. */
3839 /* Note that in all three cases below we refrain from optimizing
3840 an unsigned operation on sign-extended args.
3841 That would not be valid. */
3843 /* Both args variable: if both extended in same way
3844 from same width, do it in that width.
3845 Do it unsigned if args were zero-extended. */
3846 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3847 < TYPE_PRECISION (result_type))
3848 && (TYPE_PRECISION (TREE_TYPE (arg1))
3849 == TYPE_PRECISION (TREE_TYPE (arg0)))
3850 && unsigned0 == unsigned1
3851 && (unsigned0 || !uns))
3853 = signed_or_unsigned_type (unsigned0,
3854 common_type (TREE_TYPE (arg0),
3856 else if (TREE_CODE (arg0) == INTEGER_CST
3857 && (unsigned1 || !uns)
3858 && (TYPE_PRECISION (TREE_TYPE (arg1))
3859 < TYPE_PRECISION (result_type))
3860 && (type = signed_or_unsigned_type (unsigned1,
3862 int_fits_type_p (arg0, type)))
3864 else if (TREE_CODE (arg1) == INTEGER_CST
3865 && (unsigned0 || !uns)
3866 && (TYPE_PRECISION (TREE_TYPE (arg0))
3867 < TYPE_PRECISION (result_type))
3868 && (type = signed_or_unsigned_type (unsigned0,
3870 int_fits_type_p (arg1, type)))
3874 /* Shifts can be shortened if shifting right. */
3879 tree arg0 = get_narrower (op0, &unsigned_arg);
3881 final_type = result_type;
3883 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3884 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3886 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3887 /* We can shorten only if the shift count is less than the
3888 number of bits in the smaller type size. */
3889 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3890 /* If arg is sign-extended and then unsigned-shifted,
3891 we can simulate this with a signed shift in arg's type
3892 only if the extended result is at least twice as wide
3893 as the arg. Otherwise, the shift could use up all the
3894 ones made by sign-extension and bring in zeros.
3895 We can't optimize that case at all, but in most machines
3896 it never happens because available widths are 2**N. */
3897 && (!TREE_UNSIGNED (final_type)
3899 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3900 <= TYPE_PRECISION (result_type))))
3902 /* Do an unsigned shift if the operand was zero-extended. */
3904 = signed_or_unsigned_type (unsigned_arg,
3906 /* Convert value-to-be-shifted to that type. */
3907 if (TREE_TYPE (op0) != result_type)
3908 op0 = cp_convert (result_type, op0);
3913 /* Comparison operations are shortened too but differently.
3914 They identify themselves by setting short_compare = 1. */
3918 /* Don't write &op0, etc., because that would prevent op0
3919 from being kept in a register.
3920 Instead, make copies of the our local variables and
3921 pass the copies by reference, then copy them back afterward. */
3922 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3923 enum tree_code xresultcode = resultcode;
3925 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3927 return cp_convert (boolean_type_node, val);
3928 op0 = xop0, op1 = xop1;
3930 resultcode = xresultcode;
3933 if (short_compare && warn_sign_compare)
3935 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3936 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3938 int unsignedp0, unsignedp1;
3939 tree primop0 = get_narrower (op0, &unsignedp0);
3940 tree primop1 = get_narrower (op1, &unsignedp1);
3942 /* Check for comparison of different enum types. */
3943 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3944 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3945 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3946 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3948 cp_warning ("comparison between types `%#T' and `%#T'",
3949 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3952 /* Give warnings for comparisons between signed and unsigned
3953 quantities that may fail. */
3954 /* Do the checking based on the original operand trees, so that
3955 casts will be considered, but default promotions won't be. */
3957 /* Do not warn if the comparison is being done in a signed type,
3958 since the signed type will only be chosen if it can represent
3959 all the values of the unsigned type. */
3960 if (! TREE_UNSIGNED (result_type))
3962 /* Do not warn if both operands are unsigned. */
3963 else if (op0_signed == op1_signed)
3965 /* Do not warn if the signed quantity is an unsuffixed
3966 integer literal (or some static constant expression
3967 involving such literals or a conditional expression
3968 involving such literals) and it is non-negative. */
3969 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3970 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3972 /* Do not warn if the comparison is an equality operation,
3973 the unsigned quantity is an integral constant and it does
3974 not use the most significant bit of result_type. */
3975 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3976 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3977 && int_fits_type_p (orig_op1,
3978 signed_type (result_type)))
3979 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3980 && int_fits_type_p (orig_op0,
3981 signed_type (result_type)))))
3984 warning ("comparison between a signed and an unsigned integer expressions");
3986 /* Warn if two unsigned values are being compared in a size
3987 larger than their original size, and one (and only one) is the
3988 result of a `~' operator. This comparison will always fail.
3990 Also warn if one operand is a constant, and the constant does not
3991 have all bits set that are set in the ~ operand when it is
3994 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3995 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3997 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3998 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3999 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
4000 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
4002 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
4005 HOST_WIDE_INT constant, mask;
4009 if (host_integerp (primop0, 0))
4012 unsignedp = unsignedp1;
4013 constant = tree_low_cst (primop0, 0);
4018 unsignedp = unsignedp0;
4019 constant = tree_low_cst (primop1, 0);
4022 bits = TYPE_PRECISION (TREE_TYPE (primop));
4023 if (bits < TYPE_PRECISION (result_type)
4024 && bits < HOST_BITS_PER_LONG && unsignedp)
4026 mask = (~ (HOST_WIDE_INT) 0) << bits;
4027 if ((mask & constant) != mask)
4028 warning ("comparison of promoted ~unsigned with constant");
4031 else if (unsignedp0 && unsignedp1
4032 && (TYPE_PRECISION (TREE_TYPE (primop0))
4033 < TYPE_PRECISION (result_type))
4034 && (TYPE_PRECISION (TREE_TYPE (primop1))
4035 < TYPE_PRECISION (result_type)))
4036 warning ("comparison of promoted ~unsigned with unsigned");
4041 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4042 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4043 Then the expression will be built.
4044 It will be given type FINAL_TYPE if that is nonzero;
4045 otherwise, it will be given type RESULT_TYPE. */
4049 cp_error ("invalid operands of types `%T' and `%T' to binary `%O'",
4050 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4051 return error_mark_node;
4054 /* Issue warnings about peculiar, but legal, uses of NULL. */
4055 if (/* It's reasonable to use pointer values as operands of &&
4056 and ||, so NULL is no exception. */
4057 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4058 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4059 (orig_op0 == null_node
4060 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4061 /* Or vice versa. */
4062 || (orig_op1 == null_node
4063 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4064 /* Or, both are NULL and the operation was not a comparison. */
4065 || (orig_op0 == null_node && orig_op1 == null_node
4066 && code != EQ_EXPR && code != NE_EXPR)))
4067 /* Some sort of arithmetic operation involving NULL was
4068 performed. Note that pointer-difference and pointer-addition
4069 have already been handled above, and so we don't end up here in
4071 cp_warning ("NULL used in arithmetic");
4075 if (TREE_TYPE (op0) != result_type)
4076 op0 = cp_convert (result_type, op0);
4077 if (TREE_TYPE (op1) != result_type)
4078 op1 = cp_convert (result_type, op1);
4080 if (op0 == error_mark_node || op1 == error_mark_node)
4081 return error_mark_node;
4084 if (build_type == NULL_TREE)
4085 build_type = result_type;
4088 register tree result = build (resultcode, build_type, op0, op1);
4089 register tree folded;
4091 folded = fold (result);
4092 if (folded == result)
4093 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4094 if (final_type != 0)
4095 return cp_convert (final_type, folded);
4100 /* Return a tree for the sum or difference (RESULTCODE says which)
4101 of pointer PTROP and integer INTOP. */
4104 pointer_int_sum (resultcode, ptrop, intop)
4105 enum tree_code resultcode;
4106 register tree ptrop, intop;
4110 register tree result;
4111 register tree folded = fold (intop);
4113 /* The result is a pointer of the same type that is being added. */
4115 register tree result_type = TREE_TYPE (ptrop);
4117 if (!complete_type_or_else (result_type, ptrop))
4118 return error_mark_node;
4120 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4122 if (pedantic || warn_pointer_arith)
4123 pedwarn ("ISO C++ forbids using pointer of type `void *' in pointer arithmetic");
4124 size_exp = integer_one_node;
4126 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4128 if (pedantic || warn_pointer_arith)
4129 pedwarn ("ISO C++ forbids using a pointer-to-function in pointer arithmetic");
4130 size_exp = integer_one_node;
4132 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4134 if (pedantic || warn_pointer_arith)
4135 pedwarn ("ISO C++ forbids using a pointer to member function in pointer arithmetic");
4136 size_exp = integer_one_node;
4138 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4140 if (pedantic || warn_pointer_arith)
4141 pedwarn ("ISO C++ forbids using pointer to a member in pointer arithmetic");
4142 size_exp = integer_one_node;
4145 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4147 /* Needed to make OOPS V2R3 work. */
4149 if (integer_zerop (intop))
4152 /* If what we are about to multiply by the size of the elements
4153 contains a constant term, apply distributive law
4154 and multiply that constant term separately.
4155 This helps produce common subexpressions. */
4157 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4158 && ! TREE_CONSTANT (intop)
4159 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4160 && TREE_CONSTANT (size_exp))
4162 enum tree_code subcode = resultcode;
4163 if (TREE_CODE (intop) == MINUS_EXPR)
4164 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4165 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4166 intop = TREE_OPERAND (intop, 0);
4169 /* Convert the integer argument to a type the same size as sizetype
4170 so the multiply won't overflow spuriously. */
4172 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4173 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4175 /* Replace the integer argument with a suitable product by the object size.
4176 Do this multiplication as signed, then convert to the appropriate
4177 pointer type (actually unsigned integral). */
4179 intop = cp_convert (result_type,
4180 build_binary_op (MULT_EXPR, intop,
4181 cp_convert (TREE_TYPE (intop),
4184 /* Create the sum or difference. */
4186 result = build (resultcode, result_type, ptrop, intop);
4188 folded = fold (result);
4189 if (folded == result)
4190 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4194 /* Return a tree for the difference of pointers OP0 and OP1.
4195 The resulting tree has type int. */
4198 pointer_diff (op0, op1, ptrtype)
4199 register tree op0, op1;
4200 register tree ptrtype;
4202 register tree result, folded;
4203 tree restype = ptrdiff_type_node;
4204 tree target_type = TREE_TYPE (ptrtype);
4206 if (!complete_type_or_else (target_type, NULL_TREE))
4207 return error_mark_node;
4209 if (pedantic || warn_pointer_arith)
4211 if (TREE_CODE (target_type) == VOID_TYPE)
4212 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4213 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4214 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4215 if (TREE_CODE (target_type) == METHOD_TYPE)
4216 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4217 if (TREE_CODE (target_type) == OFFSET_TYPE)
4218 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4221 /* First do the subtraction as integers;
4222 then drop through to build the divide operator. */
4224 op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4225 cp_convert (restype, op1));
4227 /* This generates an error if op1 is a pointer to an incomplete type. */
4228 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4229 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4231 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4232 || TREE_CODE (target_type) == FUNCTION_TYPE
4233 || TREE_CODE (target_type) == METHOD_TYPE
4234 || TREE_CODE (target_type) == OFFSET_TYPE)
4236 : size_in_bytes (target_type));
4238 /* Do the division. */
4240 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4242 folded = fold (result);
4243 if (folded == result)
4244 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4248 /* Handle the case of taking the address of a COMPONENT_REF.
4249 Called by `build_unary_op'.
4251 ARG is the COMPONENT_REF whose address we want.
4252 ARGTYPE is the pointer type that this address should have. */
4255 build_component_addr (arg, argtype)
4258 tree field = TREE_OPERAND (arg, 1);
4259 tree basetype = decl_type_context (field);
4260 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4262 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4264 if (DECL_C_BIT_FIELD (field))
4266 cp_error ("attempt to take address of bit-field structure member `%D'",
4268 return error_mark_node;
4271 if (TREE_CODE (field) == FIELD_DECL
4272 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
4274 /* Can't convert directly to ARGTYPE, since that
4275 may have the same pointer type as one of our
4277 rval = build1 (NOP_EXPR, argtype,
4278 convert_pointer_to (basetype, rval));
4279 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4282 /* This conversion is harmless. */
4283 rval = convert_force (argtype, rval, 0);
4285 return fold (build (PLUS_EXPR, argtype, rval,
4286 cp_convert (argtype, byte_position (field))));
4289 /* Construct and perhaps optimize a tree representation
4290 for a unary operation. CODE, a tree_code, specifies the operation
4291 and XARG is the operand. */
4294 build_x_unary_op (code, xarg)
4295 enum tree_code code;
4298 if (processing_template_decl)
4299 return build_min_nt (code, xarg, NULL_TREE);
4301 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4303 if (code == ADDR_EXPR
4304 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4305 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4306 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
4307 || (TREE_CODE (xarg) == OFFSET_REF)))
4308 /* don't look for a function */;
4313 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4314 NULL_TREE, NULL_TREE);
4315 if (rval || code != ADDR_EXPR)
4319 if (code == ADDR_EXPR)
4321 if (TREE_CODE (xarg) == TARGET_EXPR)
4322 warning ("taking address of temporary");
4325 return build_unary_op (code, xarg, 0);
4328 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4331 condition_conversion (expr)
4335 if (processing_template_decl)
4337 t = perform_implicit_conversion (boolean_type_node, expr);
4338 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4342 /* C++: Must handle pointers to members.
4344 Perhaps type instantiation should be extended to handle conversion
4345 from aggregates to types we don't yet know we want? (Or are those
4346 cases typically errors which should be reported?)
4348 NOCONVERT nonzero suppresses the default promotions
4349 (such as from short to int). */
4352 build_unary_op (code, xarg, noconvert)
4353 enum tree_code code;
4357 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4358 register tree arg = xarg;
4359 register tree argtype = 0;
4360 const char *errstring = NULL;
4363 if (arg == error_mark_node)
4364 return error_mark_node;
4369 /* This is used for unary plus, because a CONVERT_EXPR
4370 is enough to prevent anybody from looking inside for
4371 associativity, but won't generate any code. */
4372 if (!(arg = build_expr_type_conversion
4373 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4374 errstring = "wrong type argument to unary plus";
4378 arg = default_conversion (arg);
4379 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4380 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4385 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4386 errstring = "wrong type argument to unary minus";
4387 else if (!noconvert)
4388 arg = default_conversion (arg);
4392 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4396 arg = default_conversion (arg);
4398 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4400 errstring = "wrong type argument to bit-complement";
4401 else if (!noconvert)
4402 arg = default_conversion (arg);
4406 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4407 errstring = "wrong type argument to abs";
4408 else if (!noconvert)
4409 arg = default_conversion (arg);
4413 /* Conjugating a real value is a no-op, but allow it anyway. */
4414 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4415 errstring = "wrong type argument to conjugation";
4416 else if (!noconvert)
4417 arg = default_conversion (arg);
4420 case TRUTH_NOT_EXPR:
4421 arg = cp_convert (boolean_type_node, arg);
4422 val = invert_truthvalue (arg);
4423 if (arg != error_mark_node)
4425 errstring = "in argument to unary !";
4432 if (TREE_CODE (arg) == COMPLEX_CST)
4433 return TREE_REALPART (arg);
4434 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4435 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4440 if (TREE_CODE (arg) == COMPLEX_CST)
4441 return TREE_IMAGPART (arg);
4442 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4443 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4445 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4447 case PREINCREMENT_EXPR:
4448 case POSTINCREMENT_EXPR:
4449 case PREDECREMENT_EXPR:
4450 case POSTDECREMENT_EXPR:
4451 /* Handle complex lvalues (when permitted)
4452 by reduction to simpler cases. */
4454 val = unary_complex_lvalue (code, arg);
4458 /* Increment or decrement the real part of the value,
4459 and don't change the imaginary part. */
4460 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4464 arg = stabilize_reference (arg);
4465 real = build_unary_op (REALPART_EXPR, arg, 1);
4466 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4467 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4468 build_unary_op (code, real, 1), imag);
4471 /* Report invalid types. */
4473 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4476 if (code == PREINCREMENT_EXPR)
4477 errstring ="no pre-increment operator for type";
4478 else if (code == POSTINCREMENT_EXPR)
4479 errstring ="no post-increment operator for type";
4480 else if (code == PREDECREMENT_EXPR)
4481 errstring ="no pre-decrement operator for type";
4483 errstring ="no post-decrement operator for type";
4487 /* Report something read-only. */
4489 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4490 || TREE_READONLY (arg))
4491 readonly_error (arg, ((code == PREINCREMENT_EXPR
4492 || code == POSTINCREMENT_EXPR)
4493 ? "increment" : "decrement"),
4498 tree result_type = TREE_TYPE (arg);
4500 arg = get_unwidened (arg, 0);
4501 argtype = TREE_TYPE (arg);
4503 /* ARM $5.2.5 last annotation says this should be forbidden. */
4504 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4505 pedwarn ("ISO C++ forbids %sing an enum",
4506 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4507 ? "increment" : "decrement");
4509 /* Compute the increment. */
4511 if (TREE_CODE (argtype) == POINTER_TYPE)
4513 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4514 tree type = complete_type (TREE_TYPE (argtype));
4516 if (!COMPLETE_OR_VOID_TYPE_P (type))
4517 cp_error ("cannot %s a pointer to incomplete type `%T'",
4518 ((code == PREINCREMENT_EXPR
4519 || code == POSTINCREMENT_EXPR)
4520 ? "increment" : "decrement"), TREE_TYPE (argtype));
4521 else if ((pedantic || warn_pointer_arith)
4522 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4523 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4524 cp_pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4525 ((code == PREINCREMENT_EXPR
4526 || code == POSTINCREMENT_EXPR)
4527 ? "increment" : "decrement"), argtype);
4528 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4531 inc = integer_one_node;
4533 inc = cp_convert (argtype, inc);
4535 /* Handle incrementing a cast-expression. */
4537 switch (TREE_CODE (arg))
4542 case FIX_TRUNC_EXPR:
4543 case FIX_FLOOR_EXPR:
4544 case FIX_ROUND_EXPR:
4547 tree incremented, modify, value, compound;
4548 if (! lvalue_p (arg) && pedantic)
4549 pedwarn ("cast to non-reference type used as lvalue");
4550 arg = stabilize_reference (arg);
4551 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4554 value = save_expr (arg);
4555 incremented = build (((code == PREINCREMENT_EXPR
4556 || code == POSTINCREMENT_EXPR)
4557 ? PLUS_EXPR : MINUS_EXPR),
4558 argtype, value, inc);
4560 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4561 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4563 /* Eliminate warning about unused result of + or -. */
4564 TREE_NO_UNUSED_WARNING (compound) = 1;
4572 /* Complain about anything else that is not a true lvalue. */
4573 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4574 || code == POSTINCREMENT_EXPR)
4575 ? "increment" : "decrement")))
4576 return error_mark_node;
4578 /* Forbid using -- on `bool'. */
4579 if (TREE_TYPE (arg) == boolean_type_node)
4581 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4583 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4584 return error_mark_node;
4587 /* This will only work if someone can convince Kenner to accept
4588 my patch to expand_increment. (jason) */
4589 val = build (code, TREE_TYPE (arg), arg, inc);
4591 if (code == POSTINCREMENT_EXPR)
4593 arg = stabilize_reference (arg);
4594 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4596 arg = save_expr (arg);
4597 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4598 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4601 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4606 val = build (code, TREE_TYPE (arg), arg, inc);
4608 TREE_SIDE_EFFECTS (val) = 1;
4609 return cp_convert (result_type, val);
4613 /* Note that this operation never does default_conversion
4614 regardless of NOCONVERT. */
4616 argtype = lvalue_type (arg);
4617 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4621 build_pointer_type (TREE_TYPE (argtype)), arg);
4622 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4625 else if (pedantic && DECL_MAIN_P (arg))
4627 pedwarn ("ISO C++ forbids taking address of function `::main'");
4629 /* Let &* cancel out to simplify resulting code. */
4630 if (TREE_CODE (arg) == INDIRECT_REF)
4632 /* We don't need to have `current_class_ptr' wrapped in a
4633 NON_LVALUE_EXPR node. */
4634 if (arg == current_class_ref)
4635 return current_class_ptr;
4637 arg = TREE_OPERAND (arg, 0);
4638 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4642 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4643 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4645 else if (lvalue_p (arg))
4646 /* Don't let this be an lvalue. */
4647 return non_lvalue (arg);
4651 /* For &x[y], return x+y */
4652 if (TREE_CODE (arg) == ARRAY_REF)
4654 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4655 return error_mark_node;
4656 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4657 TREE_OPERAND (arg, 1));
4660 /* Uninstantiated types are all functions. Taking the
4661 address of a function is a no-op, so just return the
4664 if (TREE_CODE (arg) == IDENTIFIER_NODE
4665 && IDENTIFIER_OPNAME_P (arg))
4667 my_friendly_abort (117);
4668 /* We don't know the type yet, so just work around the problem.
4669 We know that this will resolve to an lvalue. */
4670 return build1 (ADDR_EXPR, unknown_type_node, arg);
4673 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4674 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4676 /* They're trying to take the address of a unique non-static
4677 member function. This is ill-formed, but let's try to DTRT.
4678 Note: We only handle unique functions here because we don't
4679 want to complain if there's a static overload; non-unique
4680 cases will be handled by instantiate_type. But we need to
4681 handle this case here to allow casts on the resulting PMF. */
4683 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4684 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4686 if (! flag_ms_extensions)
4688 if (current_class_type
4689 && TREE_OPERAND (arg, 0) == current_class_ref)
4690 /* An expression like &memfn. */
4691 cp_pedwarn ("ISO C++ forbids taking the address of a non-static member function to form a pointer to member function. Say `&%T::%D'", base, name);
4693 cp_pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function", base, name);
4696 arg = build_offset_ref (base, name);
4699 if (type_unknown_p (arg))
4700 return build1 (ADDR_EXPR, unknown_type_node, arg);
4702 /* Handle complex lvalues (when permitted)
4703 by reduction to simpler cases. */
4704 val = unary_complex_lvalue (code, arg);
4708 switch (TREE_CODE (arg))
4713 case FIX_TRUNC_EXPR:
4714 case FIX_FLOOR_EXPR:
4715 case FIX_ROUND_EXPR:
4717 if (! lvalue_p (arg) && pedantic)
4718 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4725 /* Allow the address of a constructor if all the elements
4727 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4728 && TREE_CONSTANT (arg))
4730 /* Anything not already handled and not a true memory reference
4732 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4733 && TREE_CODE (argtype) != METHOD_TYPE
4734 && !lvalue_or_else (arg, "unary `&'"))
4735 return error_mark_node;
4737 if (argtype != error_mark_node)
4738 argtype = build_pointer_type (argtype);
4740 if (mark_addressable (arg) == 0)
4741 return error_mark_node;
4746 if (TREE_CODE (arg) == COMPONENT_REF)
4747 addr = build_component_addr (arg, argtype);
4749 addr = build1 (ADDR_EXPR, argtype, arg);
4751 /* Address of a static or external variable or
4752 function counts as a constant */
4754 TREE_CONSTANT (addr) = 1;
4756 if (TREE_CODE (argtype) == POINTER_TYPE
4757 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4759 build_ptrmemfunc_type (argtype);
4760 addr = build_ptrmemfunc (argtype, addr, 0);
4773 argtype = TREE_TYPE (arg);
4774 return fold (build1 (code, argtype, arg));
4777 error ("%s", errstring);
4778 return error_mark_node;
4782 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4783 convert ARG with the same conversions in the same order
4784 and return the result. */
4787 convert_sequence (conversions, arg)
4791 switch (TREE_CODE (conversions))
4796 case FIX_TRUNC_EXPR:
4797 case FIX_FLOOR_EXPR:
4798 case FIX_ROUND_EXPR:
4800 return cp_convert (TREE_TYPE (conversions),
4801 convert_sequence (TREE_OPERAND (conversions, 0),
4810 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4811 for certain kinds of expressions which are not really lvalues
4812 but which we can accept as lvalues.
4814 If ARG is not a kind of expression we can handle, return zero. */
4817 unary_complex_lvalue (code, arg)
4818 enum tree_code code;
4821 /* Handle (a, b) used as an "lvalue". */
4822 if (TREE_CODE (arg) == COMPOUND_EXPR)
4824 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4825 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4826 TREE_OPERAND (arg, 0), real_result);
4829 /* Handle (a ? b : c) used as an "lvalue". */
4830 if (TREE_CODE (arg) == COND_EXPR
4831 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4832 return rationalize_conditional_expr (code, arg);
4834 if (TREE_CODE (arg) == MODIFY_EXPR
4835 || TREE_CODE (arg) == PREINCREMENT_EXPR
4836 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4837 return unary_complex_lvalue
4838 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4839 arg, TREE_OPERAND (arg, 0)));
4841 if (code != ADDR_EXPR)
4844 /* Handle (a = b) used as an "lvalue" for `&'. */
4845 if (TREE_CODE (arg) == MODIFY_EXPR
4846 || TREE_CODE (arg) == INIT_EXPR)
4848 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4849 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4850 TREE_NO_UNUSED_WARNING (arg) = 1;
4854 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4855 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4856 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4858 /* The representation of something of type OFFSET_TYPE
4859 is really the representation of a pointer to it.
4860 Here give the representation its true type. */
4863 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4865 if (TREE_CODE (arg) != OFFSET_REF)
4868 t = TREE_OPERAND (arg, 1);
4870 /* Check all this code for right semantics. */
4871 if (TREE_CODE (t) == FUNCTION_DECL)
4873 if (DECL_DESTRUCTOR_P (t))
4874 cp_error ("taking address of destructor");
4875 return build_unary_op (ADDR_EXPR, t, 0);
4877 if (TREE_CODE (t) == VAR_DECL)
4878 return build_unary_op (ADDR_EXPR, t, 0);
4883 if (TREE_OPERAND (arg, 0)
4884 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4885 && TREE_CODE (t) != FIELD_DECL)
4887 cp_error ("taking address of bound pointer-to-member expression");
4888 return error_mark_node;
4891 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4892 type = build_pointer_type (type);
4894 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4900 /* We permit compiler to make function calls returning
4901 objects of aggregate type look like lvalues. */
4905 if (TREE_CODE (targ) == SAVE_EXPR)
4906 targ = TREE_OPERAND (targ, 0);
4908 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4910 if (TREE_CODE (arg) == SAVE_EXPR)
4913 targ = build_cplus_new (TREE_TYPE (arg), arg);
4914 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4917 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4918 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4919 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4922 /* Don't let anything else be handled specially. */
4926 /* Mark EXP saying that we need to be able to take the
4927 address of it; it should not be allocated in a register.
4928 Value is 1 if successful.
4930 C++: we do not allow `current_class_ptr' to be addressable. */
4933 mark_addressable (exp)
4936 register tree x = exp;
4938 if (TREE_ADDRESSABLE (x) == 1)
4942 switch (TREE_CODE (x))
4949 x = TREE_OPERAND (x, 0);
4953 if (x == current_class_ptr)
4955 if (! flag_this_is_variable)
4956 error ("cannot take the address of `this', which is an ravlue expression");
4957 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4958 put_var_into_stack (x);
4962 if (TREE_STATIC (x) && TREE_READONLY (x)
4963 && DECL_RTL (x) != 0
4964 && ! DECL_IN_MEMORY_P (x))
4966 TREE_ASM_WRITTEN (x) = 0;
4968 rest_of_decl_compilation (x, 0,
4969 !DECL_FUNCTION_SCOPE_P (x),
4971 TREE_ADDRESSABLE (x) = 1;
4975 /* Caller should not be trying to mark initialized
4976 constant fields addressable. */
4977 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4978 || DECL_IN_AGGR_P (x) == 0
4980 || DECL_EXTERNAL (x), 314);
4984 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4985 && !DECL_ARTIFICIAL (x) && extra_warnings)
4986 cp_warning ("address requested for `%D', which is declared `register'",
4988 TREE_ADDRESSABLE (x) = 1;
4990 if (cfun && expanding_p)
4991 put_var_into_stack (x);
4995 /* We have to test both conditions here. The first may be
4996 non-zero in the case of processing a default function. The
4997 second may be non-zero in the case of a template function. */
4998 if (DECL_LANG_SPECIFIC (x)
4999 && DECL_TEMPLATE_INFO (x)
5000 && !DECL_TEMPLATE_SPECIALIZATION (x))
5002 TREE_ADDRESSABLE (x) = 1;
5004 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
5008 TREE_ADDRESSABLE (x) = 1;
5012 TREE_ADDRESSABLE (x) = 1;
5013 mark_addressable (TREE_OPERAND (x, 0));
5021 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5024 build_x_conditional_expr (ifexp, op1, op2)
5025 tree ifexp, op1, op2;
5027 if (processing_template_decl)
5028 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5030 return build_conditional_expr (ifexp, op1, op2);
5033 /* Handle overloading of the ',' operator when needed. Otherwise,
5034 this function just builds an expression list. */
5037 build_x_compound_expr (list)
5040 tree rest = TREE_CHAIN (list);
5043 if (processing_template_decl)
5044 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5046 if (rest == NULL_TREE)
5047 return build_compound_expr (list);
5049 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5050 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5052 return build_x_compound_expr (tree_cons (NULL_TREE, result,
5053 TREE_CHAIN (rest)));
5055 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5057 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5058 /* the left-hand operand of a comma expression is like an expression
5059 statement: we should warn if it doesn't have any side-effects,
5060 unless it was explicitly cast to (void). */
5061 if ((extra_warnings || warn_unused)
5062 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5063 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5064 warning("left-hand operand of comma expression has no effect");
5066 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5067 else if (warn_unused)
5068 warn_if_unused_value (TREE_VALUE(list));
5071 return build_compound_expr
5072 (tree_cons (NULL_TREE, TREE_VALUE (list),
5073 build_tree_list (NULL_TREE,
5074 build_x_compound_expr (rest))));
5077 /* Given a list of expressions, return a compound expression
5078 that performs them all and returns the value of the last of them. */
5081 build_compound_expr (list)
5087 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5089 if (TREE_CHAIN (list) == 0)
5091 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5092 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5093 if (TREE_CODE (list) == NOP_EXPR
5094 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5095 list = TREE_OPERAND (list, 0);
5097 return TREE_VALUE (list);
5100 first = TREE_VALUE (list);
5101 first = convert_to_void (first, "left-hand operand of comma");
5102 if (first == error_mark_node)
5103 return error_mark_node;
5105 rest = build_compound_expr (TREE_CHAIN (list));
5106 if (rest == error_mark_node)
5107 return error_mark_node;
5109 /* When pedantic, a compound expression cannot be a constant expression. */
5110 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5113 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5114 break_out_cleanups (first), rest);
5118 build_static_cast (type, expr)
5124 if (type == error_mark_node || expr == error_mark_node)
5125 return error_mark_node;
5127 if (TREE_CODE (expr) == OFFSET_REF)
5128 expr = resolve_offset_ref (expr);
5130 if (processing_template_decl)
5132 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5136 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5137 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5138 if (TREE_CODE (type) != REFERENCE_TYPE
5139 && TREE_CODE (expr) == NOP_EXPR
5140 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5141 expr = TREE_OPERAND (expr, 0);
5143 if (TREE_CODE (type) == VOID_TYPE)
5145 expr = convert_to_void (expr, /*implicit=*/NULL);
5149 if (TREE_CODE (type) == REFERENCE_TYPE)
5150 return (convert_from_reference
5151 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5152 LOOKUP_COMPLAIN, NULL_TREE)));
5154 if (IS_AGGR_TYPE (type))
5155 return build_cplus_new (type, (build_method_call
5156 (NULL_TREE, complete_ctor_identifier,
5157 build_tree_list (NULL_TREE, expr),
5158 TYPE_BINFO (type), LOOKUP_NORMAL)));
5160 expr = decay_conversion (expr);
5161 intype = TREE_TYPE (expr);
5163 /* FIXME handle casting to array type. */
5166 if (IS_AGGR_TYPE (intype)
5167 ? can_convert_arg (type, intype, expr)
5168 : can_convert_arg (strip_all_pointer_quals (type),
5169 strip_all_pointer_quals (intype), expr))
5171 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5174 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5175 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5176 && ! TREE_VIA_VIRTUAL (binfo))
5179 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5181 if (same_type_ignoring_top_level_qualifiers_p
5182 (TREE_TYPE (TREE_TYPE (type)),
5183 TREE_TYPE (TREE_TYPE (intype)))
5184 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5185 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5186 && ! TREE_VIA_VIRTUAL (binfo))
5189 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5190 && TREE_CODE (type) != ARRAY_TYPE
5191 && TREE_CODE (type) != FUNCTION_TYPE
5192 && can_convert (intype, strip_all_pointer_quals (type)))
5195 /* [expr.static.cast]
5197 The static_cast operator shall not be used to cast away
5199 if (ok && casts_away_constness (intype, type))
5201 cp_error ("static_cast from type `%T' to type `%T' casts away constness",
5203 return error_mark_node;
5207 return build_c_cast (type, expr);
5209 cp_error ("invalid static_cast from type `%T' to type `%T'", intype, type);
5210 return error_mark_node;
5214 build_reinterpret_cast (type, expr)
5219 if (type == error_mark_node || expr == error_mark_node)
5220 return error_mark_node;
5222 if (TREE_CODE (expr) == OFFSET_REF)
5223 expr = resolve_offset_ref (expr);
5225 if (processing_template_decl)
5227 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5231 if (TREE_CODE (type) != REFERENCE_TYPE)
5233 expr = decay_conversion (expr);
5235 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5236 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5237 if (TREE_CODE (expr) == NOP_EXPR
5238 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5239 expr = TREE_OPERAND (expr, 0);
5242 intype = TREE_TYPE (expr);
5244 if (TREE_CODE (type) == REFERENCE_TYPE)
5246 if (! real_lvalue_p (expr))
5248 cp_error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
5249 return error_mark_node;
5251 expr = build_unary_op (ADDR_EXPR, expr, 0);
5252 if (expr != error_mark_node)
5253 expr = build_reinterpret_cast
5254 (build_pointer_type (TREE_TYPE (type)), expr);
5255 if (expr != error_mark_node)
5256 expr = build_indirect_ref (expr, 0);
5259 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5260 return build_static_cast (type, expr);
5262 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5263 || TREE_CODE (intype) == ENUMERAL_TYPE))
5265 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5267 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5268 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5271 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5272 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5274 expr = decl_constant_value (expr);
5275 return fold (build1 (NOP_EXPR, type, expr));
5277 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5278 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5280 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5281 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5284 expr = decl_constant_value (expr);
5285 return fold (build1 (NOP_EXPR, type, expr));
5287 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5288 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5290 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5291 expr = decl_constant_value (expr);
5292 return fold (build1 (NOP_EXPR, type, expr));
5296 cp_error ("invalid reinterpret_cast from type `%T' to type `%T'",
5298 return error_mark_node;
5301 return cp_convert (type, expr);
5305 build_const_cast (type, expr)
5310 if (type == error_mark_node || expr == error_mark_node)
5311 return error_mark_node;
5313 if (TREE_CODE (expr) == OFFSET_REF)
5314 expr = resolve_offset_ref (expr);
5316 if (processing_template_decl)
5318 tree t = build_min (CONST_CAST_EXPR, type, expr);
5322 if (!POINTER_TYPE_P (type))
5323 cp_error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5324 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5326 cp_error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5327 return error_mark_node;
5330 if (TREE_CODE (type) != REFERENCE_TYPE)
5332 expr = decay_conversion (expr);
5334 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5335 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5336 if (TREE_CODE (expr) == NOP_EXPR
5337 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5338 expr = TREE_OPERAND (expr, 0);
5341 intype = TREE_TYPE (expr);
5343 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5344 return build_static_cast (type, expr);
5345 else if (TREE_CODE (type) == REFERENCE_TYPE)
5347 if (! real_lvalue_p (expr))
5349 cp_error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5350 return error_mark_node;
5353 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5355 expr = build_unary_op (ADDR_EXPR, expr, 0);
5356 expr = build1 (NOP_EXPR, type, expr);
5357 return convert_from_reference (expr);
5360 else if (TREE_CODE (type) == POINTER_TYPE
5361 && TREE_CODE (intype) == POINTER_TYPE
5362 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5363 return cp_convert (type, expr);
5365 cp_error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5366 return error_mark_node;
5369 /* Build an expression representing a cast to type TYPE of expression EXPR.
5371 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5372 when doing the cast. */
5375 build_c_cast (type, expr)
5378 register tree value = expr;
5381 if (type == error_mark_node || expr == error_mark_node)
5382 return error_mark_node;
5384 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5385 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5386 if (TREE_CODE (type) != REFERENCE_TYPE
5387 && TREE_CODE (value) == NOP_EXPR
5388 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5389 value = TREE_OPERAND (value, 0);
5391 if (TREE_CODE (value) == OFFSET_REF)
5392 value = resolve_offset_ref (value);
5394 if (TREE_CODE (type) == ARRAY_TYPE)
5396 /* Allow casting from T1* to T2[] because Cfront allows it.
5397 NIHCL uses it. It is not valid ISO C++ however. */
5398 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5401 pedwarn ("ISO C++ forbids casting to an array type");
5402 type = build_pointer_type (TREE_TYPE (type));
5406 error ("ISO C++ forbids casting to an array type");
5407 return error_mark_node;
5411 if (TREE_CODE (type) == FUNCTION_TYPE
5412 || TREE_CODE (type) == METHOD_TYPE)
5414 cp_error ("invalid cast to function type `%T'", type);
5415 return error_mark_node;
5418 if (processing_template_decl)
5420 tree t = build_min (CAST_EXPR, type,
5421 tree_cons (NULL_TREE, value, NULL_TREE));
5425 if (TREE_CODE (type) == VOID_TYPE)
5427 /* Conversion to void does not cause any of the normal function to
5428 * pointer, array to pointer and lvalue to rvalue decays. */
5430 value = convert_to_void (value, /*implicit=*/NULL);
5433 /* Convert functions and arrays to pointers and
5434 convert references to their expanded types,
5435 but don't convert any other types. If, however, we are
5436 casting to a class type, there's no reason to do this: the
5437 cast will only succeed if there is a converting constructor,
5438 and the default conversions will be done at that point. In
5439 fact, doing the default conversion here is actually harmful
5443 struct S { S(const A&); };
5445 since we don't want the array-to-pointer conversion done. */
5446 if (!IS_AGGR_TYPE (type))
5448 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5449 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5450 /* Don't do the default conversion on a ->* expression. */
5451 && ! (TREE_CODE (type) == POINTER_TYPE
5452 && bound_pmf_p (value)))
5453 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5454 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5455 value = default_conversion (value);
5457 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5458 /* However, even for class types, we still need to strip away
5459 the reference type, since the call to convert_force below
5460 does not expect the input expression to be of reference
5462 value = convert_from_reference (value);
5464 otype = TREE_TYPE (value);
5466 /* Optionally warn about potentially worrisome casts. */
5469 && TREE_CODE (type) == POINTER_TYPE
5470 && TREE_CODE (otype) == POINTER_TYPE
5471 && !at_least_as_qualified_p (TREE_TYPE (type),
5473 cp_warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5476 if (TREE_CODE (type) == INTEGER_TYPE
5477 && TREE_CODE (otype) == POINTER_TYPE
5478 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5479 warning ("cast from pointer to integer of different size");
5481 if (TREE_CODE (type) == POINTER_TYPE
5482 && TREE_CODE (otype) == INTEGER_TYPE
5483 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5484 /* Don't warn about converting 0 to pointer,
5485 provided the 0 was explicit--not cast or made by folding. */
5486 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5487 warning ("cast to pointer from integer of different size");
5489 if (TREE_CODE (type) == REFERENCE_TYPE)
5490 value = (convert_from_reference
5491 (convert_to_reference (type, value, CONV_C_CAST,
5492 LOOKUP_COMPLAIN, NULL_TREE)));
5497 value = decl_constant_value (value);
5500 value = convert_force (type, value, CONV_C_CAST);
5502 /* Ignore any integer overflow caused by the cast. */
5503 if (TREE_CODE (value) == INTEGER_CST)
5505 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5506 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5510 /* Warn about possible alignment problems. Do this here when we will have
5511 instantiated any necessary template types. */
5512 if (STRICT_ALIGNMENT && warn_cast_align
5513 && TREE_CODE (type) == POINTER_TYPE
5514 && TREE_CODE (otype) == POINTER_TYPE
5515 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5516 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5517 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5518 && COMPLETE_TYPE_P (TREE_TYPE (type))
5519 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5520 cp_warning ("cast from `%T' to `%T' increases required alignment of target type",
5523 /* Always produce some operator for an explicit cast,
5524 so we can tell (for -pedantic) that the cast is no lvalue. */
5525 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5526 && real_lvalue_p (value))
5527 value = non_lvalue (value);
5532 /* Build an assignment expression of lvalue LHS from value RHS.
5533 MODIFYCODE is the code for a binary operator that we use
5534 to combine the old value of LHS with RHS to get the new value.
5535 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5537 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5540 build_modify_expr (lhs, modifycode, rhs)
5542 enum tree_code modifycode;
5545 register tree result;
5547 tree lhstype = TREE_TYPE (lhs);
5548 tree olhstype = lhstype;
5551 /* Avoid duplicate error messages from operands that had errors. */
5552 if (lhs == error_mark_node || rhs == error_mark_node)
5553 return error_mark_node;
5555 /* Types that aren't fully specified cannot be used in assignments. */
5556 lhs = require_complete_type (lhs);
5560 /* Handle control structure constructs used as "lvalues". */
5562 switch (TREE_CODE (lhs))
5564 /* Handle --foo = 5; as these are valid constructs in C++ */
5565 case PREDECREMENT_EXPR:
5566 case PREINCREMENT_EXPR:
5567 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5568 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5569 stabilize_reference (TREE_OPERAND (lhs, 0)),
5570 TREE_OPERAND (lhs, 1));
5571 return build (COMPOUND_EXPR, lhstype,
5573 build_modify_expr (TREE_OPERAND (lhs, 0),
5576 /* Handle (a, b) used as an "lvalue". */
5578 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5580 if (newrhs == error_mark_node)
5581 return error_mark_node;
5582 return build (COMPOUND_EXPR, lhstype,
5583 TREE_OPERAND (lhs, 0), newrhs);
5586 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5587 if (newrhs == error_mark_node)
5588 return error_mark_node;
5589 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5591 /* Handle (a ? b : c) used as an "lvalue". */
5593 rhs = save_expr (rhs);
5595 /* Produce (a ? (b = rhs) : (c = rhs))
5596 except that the RHS goes through a save-expr
5597 so the code to compute it is only emitted once. */
5600 /* Check this here to avoid odd errors when trying to convert
5601 a throw to the type of the COND_EXPR. */
5602 if (!lvalue_or_else (lhs, "assignment"))
5603 return error_mark_node;
5605 cond = build_conditional_expr
5606 (TREE_OPERAND (lhs, 0),
5607 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5608 TREE_OPERAND (lhs, 1)),
5610 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5611 TREE_OPERAND (lhs, 2)),
5614 if (cond == error_mark_node)
5616 /* Make sure the code to compute the rhs comes out
5617 before the split. */
5618 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5619 /* Case to void to suppress warning
5620 from warn_if_unused_value. */
5621 cp_convert (void_type_node, rhs), cond);
5628 if (TREE_CODE (lhs) == OFFSET_REF)
5630 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5632 /* Static class member? */
5633 tree member = TREE_OPERAND (lhs, 1);
5634 if (TREE_CODE (member) == VAR_DECL)
5638 compiler_error ("invalid static class member");
5639 return error_mark_node;
5643 lhs = resolve_offset_ref (lhs);
5645 olhstype = lhstype = TREE_TYPE (lhs);
5648 if (lhs == error_mark_node)
5651 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5652 && modifycode != INIT_EXPR)
5654 lhs = convert_from_reference (lhs);
5655 olhstype = lhstype = TREE_TYPE (lhs);
5658 /* If a binary op has been requested, combine the old LHS value with the RHS
5659 producing the value we should actually store into the LHS. */
5661 if (modifycode == INIT_EXPR)
5663 if (! IS_AGGR_TYPE (lhstype))
5664 /* Do the default thing */;
5667 result = build_method_call (lhs, complete_ctor_identifier,
5668 build_tree_list (NULL_TREE, rhs),
5669 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5670 if (result == NULL_TREE)
5671 return error_mark_node;
5675 else if (modifycode == NOP_EXPR)
5677 /* `operator=' is not an inheritable operator. */
5678 if (! IS_AGGR_TYPE (lhstype))
5679 /* Do the default thing */;
5682 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5683 lhs, rhs, make_node (NOP_EXPR));
5684 if (result == NULL_TREE)
5685 return error_mark_node;
5690 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5692 my_friendly_abort (978652);
5696 lhs = stabilize_reference (lhs);
5697 newrhs = build_binary_op (modifycode, lhs, rhs);
5698 if (newrhs == error_mark_node)
5700 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5701 TREE_TYPE (lhs), TREE_TYPE (rhs));
5702 return error_mark_node;
5706 /* Handle a cast used as an "lvalue".
5707 We have already performed any binary operator using the value as cast.
5708 Now convert the result to the cast type of the lhs,
5709 and then true type of the lhs and store it there;
5710 then convert result back to the cast type to be the value
5711 of the assignment. */
5713 switch (TREE_CODE (lhs))
5718 case FIX_TRUNC_EXPR:
5719 case FIX_FLOOR_EXPR:
5720 case FIX_ROUND_EXPR:
5722 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5723 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5724 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5725 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5726 newrhs = default_conversion (newrhs);
5728 tree inner_lhs = TREE_OPERAND (lhs, 0);
5731 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5732 type, otherwise the result is an rvalue. */
5733 if (! lvalue_p (lhs))
5734 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5736 result = build_modify_expr (inner_lhs, NOP_EXPR,
5737 cp_convert (TREE_TYPE (inner_lhs),
5738 cp_convert (lhstype, newrhs)));
5739 if (result == error_mark_node)
5741 return cp_convert (TREE_TYPE (lhs), result);
5748 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5749 Reject anything strange now. */
5751 if (!lvalue_or_else (lhs, "assignment"))
5752 return error_mark_node;
5754 GNU_xref_assign (lhs);
5756 /* Warn about storing in something that is `const'. */
5757 /* For C++, don't warn if this is initialization. */
5758 if (modifycode != INIT_EXPR
5759 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5760 /* Functions are not modifiable, even though they are
5762 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5763 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5764 && C_TYPE_FIELDS_READONLY (lhstype))
5765 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5766 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5767 readonly_error (lhs, "assignment", 0);
5769 /* If storing into a structure or union member,
5770 it has probably been given type `int'.
5771 Compute the type that would go with
5772 the actual amount of storage the member occupies. */
5774 if (TREE_CODE (lhs) == COMPONENT_REF
5775 && (TREE_CODE (lhstype) == INTEGER_TYPE
5776 || TREE_CODE (lhstype) == REAL_TYPE
5777 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5779 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5781 /* If storing in a field that is in actuality a short or narrower
5782 than one, we must store in the field in its actual type. */
5784 if (lhstype != TREE_TYPE (lhs))
5786 lhs = copy_node (lhs);
5787 TREE_TYPE (lhs) = lhstype;
5791 if (modifycode != INIT_EXPR)
5793 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5794 modifycode = NOP_EXPR;
5795 /* Reference-bashing */
5796 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5798 tree tmp = convert_from_reference (lhs);
5799 lhstype = TREE_TYPE (tmp);
5800 if (!COMPLETE_TYPE_P (lhstype))
5802 incomplete_type_error (lhs, lhstype);
5803 return error_mark_node;
5808 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5810 tree tmp = convert_from_reference (newrhs);
5811 if (!COMPLETE_TYPE_P (TREE_TYPE (tmp)))
5813 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5814 return error_mark_node;
5820 if (TREE_SIDE_EFFECTS (lhs))
5821 lhs = stabilize_reference (lhs);
5822 if (TREE_SIDE_EFFECTS (newrhs))
5823 newrhs = stabilize_reference (newrhs);
5825 /* Convert new value to destination type. */
5827 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5831 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5833 cp_error ("incompatible types in assignment of `%T' to `%T'",
5834 TREE_TYPE (rhs), lhstype);
5835 return error_mark_node;
5838 /* Allow array assignment in compiler-generated code. */
5839 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5840 pedwarn ("ISO C++ forbids assignment of arrays");
5842 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5843 ? 1 + (modifycode != INIT_EXPR): 0;
5844 return (build_vec_init
5845 (lhs, lhs, array_type_nelts (lhstype), newrhs,
5849 if (modifycode == INIT_EXPR)
5851 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5852 "assignment", NULL_TREE, 0);
5853 if (current_function_decl &&
5854 lhs == DECL_RESULT (current_function_decl))
5856 if (DECL_INITIAL (lhs))
5857 warning ("return value from function receives multiple initializations");
5858 DECL_INITIAL (lhs) = newrhs;
5863 /* Avoid warnings on enum bit fields. */
5864 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5865 && TREE_CODE (lhstype) == INTEGER_TYPE)
5867 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5869 newrhs = convert_force (lhstype, newrhs, 0);
5872 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5874 if (TREE_CODE (newrhs) == CALL_EXPR
5875 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5876 newrhs = build_cplus_new (lhstype, newrhs);
5878 /* Can't initialize directly from a TARGET_EXPR, since that would
5879 cause the lhs to be constructed twice, and possibly result in
5880 accidental self-initialization. So we force the TARGET_EXPR to be
5881 expanded without a target. */
5882 if (TREE_CODE (newrhs) == TARGET_EXPR)
5883 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5884 TREE_OPERAND (newrhs, 0));
5887 if (newrhs == error_mark_node)
5888 return error_mark_node;
5890 if (TREE_CODE (newrhs) == COND_EXPR)
5893 tree cond = TREE_OPERAND (newrhs, 0);
5895 if (TREE_SIDE_EFFECTS (lhs))
5896 cond = build_compound_expr (tree_cons
5898 build_tree_list (NULL_TREE, cond)));
5900 /* Cannot have two identical lhs on this one tree (result) as preexpand
5901 calls will rip them out and fill in RTL for them, but when the
5902 rtl is generated, the calls will only be in the first side of the
5903 condition, not on both, or before the conditional jump! (mrs) */
5904 lhs1 = break_out_calls (lhs);
5907 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5908 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5909 lhstype, lhs, newrhs);
5912 tree result_type = TREE_TYPE (newrhs);
5913 /* We have to convert each arm to the proper type because the
5914 types may have been munged by constant folding. */
5916 = build (COND_EXPR, result_type, cond,
5917 build_modify_expr (lhs, modifycode,
5918 cp_convert (result_type,
5919 TREE_OPERAND (newrhs, 1))),
5920 build_modify_expr (lhs1, modifycode,
5921 cp_convert (result_type,
5922 TREE_OPERAND (newrhs, 2))));
5926 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5927 lhstype, lhs, newrhs);
5929 TREE_SIDE_EFFECTS (result) = 1;
5931 /* If we got the LHS in a different type for storing in,
5932 convert the result back to the nominal type of LHS
5933 so that the value we return always has the same type
5934 as the LHS argument. */
5936 if (olhstype == TREE_TYPE (result))
5938 /* Avoid warnings converting integral types back into enums
5939 for enum bit fields. */
5940 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5941 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5943 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5944 TREE_NO_UNUSED_WARNING (result) = 1;
5947 return convert_for_assignment (olhstype, result, "assignment",
5952 build_x_modify_expr (lhs, modifycode, rhs)
5954 enum tree_code modifycode;
5957 if (processing_template_decl)
5958 return build_min_nt (MODOP_EXPR, lhs,
5959 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5961 if (modifycode != NOP_EXPR)
5963 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5964 make_node (modifycode));
5968 return build_modify_expr (lhs, modifycode, rhs);
5972 /* Get difference in deltas for different pointer to member function
5973 types. Return integer_zero_node, if FROM cannot be converted to a
5974 TO type. If FORCE is true, then allow reverse conversions as well.
5976 Note that the naming of FROM and TO is kind of backwards; the return
5977 value is what we add to a TO in order to get a FROM. They are named
5978 this way because we call this function to find out how to convert from
5979 a pointer to member of FROM to a pointer to member of TO. */
5982 get_delta_difference (from, to, force)
5986 tree delta = integer_zero_node;
5992 /* Should get_base_distance here, so we can check if any thing along the
5993 path is virtual, and we need to make sure we stay
5994 inside the real binfos when going through virtual bases.
5995 Maybe we should replace virtual bases with
5996 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
5997 binfo = get_binfo (from, to, 1);
5998 if (binfo == error_mark_node)
6000 error (" in pointer to member function conversion");
6007 error_not_base_type (from, to);
6008 error (" in pointer to member conversion");
6011 binfo = get_binfo (to, from, 1);
6012 if (binfo == 0 || binfo == error_mark_node)
6014 if (binfo_from_vbase (binfo))
6016 binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), from);
6017 cp_warning ("pointer to member cast to virtual base `%T' will only work if you are very careful", BINFO_TYPE (binfo));
6019 delta = BINFO_OFFSET (binfo);
6020 delta = cp_convert (ptrdiff_type_node, delta);
6022 return build_binary_op (MINUS_EXPR,
6027 if (binfo_from_vbase (binfo))
6031 cp_warning ("pointer to member cast from virtual base `%T' will only wokr if you are very careful", BINFO_TYPE (binfo));
6034 cp_error ("pointer to member conversion from virtual base `%T'",
6035 BINFO_TYPE (binfo));
6038 return BINFO_OFFSET (binfo);
6041 /* Return a constructor for the pointer-to-member-function TYPE using
6042 the other components as specified. */
6045 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6046 tree type, delta, idx, pfn, delta2;
6051 tree pfn_or_delta2_field;
6055 int allconstant, allsimple;
6057 /* Pull the FIELD_DECLs out of the type. */
6060 pfn_field = TYPE_FIELDS (type);
6061 delta_field = TREE_CHAIN (pfn_field);
6062 idx_field = NULL_TREE;
6063 pfn_or_delta2_field = NULL_TREE;
6064 delta2_field = NULL_TREE;
6065 subtype = NULL_TREE;
6069 delta_field = TYPE_FIELDS (type);
6070 idx_field = TREE_CHAIN (delta_field);
6071 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6072 subtype = TREE_TYPE (pfn_or_delta2_field);
6073 pfn_field = TYPE_FIELDS (subtype);
6074 delta2_field = TREE_CHAIN (pfn_field);
6077 /* Make sure DELTA has the type we want. */
6078 delta = convert_and_check (delta_type_node, delta);
6080 /* Keep track of whether the initializer is a) constant, and b) can
6081 be done statically. */
6082 allconstant = TREE_CONSTANT (delta);
6083 allsimple = (initializer_constant_valid_p (delta, TREE_TYPE (delta))
6088 /* A non-virtual function. */
6090 u = build_tree_list (pfn_field, pfn);
6092 allconstant &= TREE_CONSTANT (pfn);
6093 allsimple &= (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6098 /* A virtual function. */
6101 allconstant &= TREE_CONSTANT (pfn);
6102 allsimple &= (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6107 idx = convert_and_check (delta_type_node, idx);
6108 u = build_tree_list (delta2_field, delta2);
6110 allconstant &= TREE_CONSTANT (idx) && TREE_CONSTANT (delta2);
6111 allsimple &= ((initializer_constant_valid_p (idx, TREE_TYPE (idx))
6113 && (initializer_constant_valid_p (delta2,
6119 /* Finish creating the initializer. */
6121 u = tree_cons (pfn_field, pfn,
6122 build_tree_list (delta_field, delta));
6125 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6126 u = tree_cons (delta_field, delta,
6127 tree_cons (idx_field,
6129 build_tree_list (pfn_or_delta2_field,
6132 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6133 TREE_CONSTANT (u) = allconstant;
6134 TREE_STATIC (u) = allconstant && allsimple;
6138 /* Build a constructor for a pointer to member function. It can be
6139 used to initialize global variables, local variable, or used
6140 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6143 If FORCE is non-zero, then force this conversion, even if
6144 we would rather not do it. Usually set when using an explicit
6147 Return error_mark_node, if something goes wrong. */
6150 build_ptrmemfunc (type, pfn, force)
6155 tree pfn_type = TREE_TYPE (pfn);
6156 tree to_type = build_ptrmemfunc_type (type);
6158 /* Handle multiple conversions of pointer to member functions. */
6159 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6161 tree idx = integer_zero_node;
6162 tree delta = integer_zero_node;
6163 tree delta2 = integer_zero_node;
6164 tree npfn = NULL_TREE;
6165 tree ndelta, ndelta2;
6169 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6170 cp_error ("invalid conversion to type `%T' from type `%T'",
6173 /* We don't have to do any conversion to convert a
6174 pointer-to-member to its own type. But, we don't want to
6175 just return a PTRMEM_CST if there's an explicit cast; that
6176 cast should make the expression an invalid template argument. */
6177 if (TREE_CODE (pfn) != PTRMEM_CST && same_type_p (to_type, pfn_type))
6182 /* Under the new ABI, the conversion is easy. Just adjust
6184 npfn = build_component_ref (pfn, pfn_identifier, NULL_TREE, 0);
6185 delta = build_component_ref (pfn, delta_identifier, NULL_TREE, 0);
6186 delta = cp_convert (ptrdiff_type_node, delta);
6187 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6188 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6190 delta = build_binary_op (PLUS_EXPR, delta, n);
6191 return build_ptrmemfunc1 (to_type, delta, NULL_TREE, npfn,
6195 if (TREE_CODE (pfn) == PTRMEM_CST)
6197 /* We could just build the resulting CONSTRUCTOR now, but we
6198 don't, relying on the general machinery below, together
6199 with constant-folding, to do the right thing. */
6200 expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
6202 /* This constant points to a non-virtual function.
6203 NDELTA2 will be NULL, but it's value doesn't really
6204 matter since we won't use it anyhow. */
6205 ndelta2 = integer_zero_node;
6209 ndelta = cp_convert (ptrdiff_type_node,
6210 build_component_ref (pfn,
6213 ndelta2 = cp_convert (ptrdiff_type_node,
6214 DELTA2_FROM_PTRMEMFUNC (pfn));
6215 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6218 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6219 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6221 delta = build_binary_op (PLUS_EXPR, ndelta, n);
6222 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n);
6223 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6225 /* If it's a virtual function, this is what we want. */
6226 e2 = build_ptrmemfunc1 (to_type, delta, idx, NULL_TREE, delta2);
6228 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6229 npfn = build1 (NOP_EXPR, type, pfn);
6230 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6232 /* But if it's a non-virtual function, or NULL, we use this
6234 e3 = build_ptrmemfunc1 (to_type, delta, idx, npfn, NULL_TREE);
6235 return build_conditional_expr (e1, e2, e3);
6238 /* Handle null pointer to member function conversions. */
6239 if (integer_zerop (pfn))
6241 pfn = build_c_cast (type, integer_zero_node);
6242 return build_ptrmemfunc1 (to_type,
6243 integer_zero_node, integer_zero_node,
6247 if (type_unknown_p (pfn))
6248 return instantiate_type (type, pfn, 1);
6250 fn = TREE_OPERAND (pfn, 0);
6251 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6252 return make_ptrmem_cst (to_type, fn);
6255 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6258 ??? There is no consistency as to the types returned for the above
6259 values. Some code acts as if its a sizetype and some as if its
6260 integer_type_node. */
6263 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6270 tree type = TREE_TYPE (cst);
6271 tree fn = PTRMEM_CST_MEMBER (cst);
6272 tree ptr_class, fn_class;
6274 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6276 /* The class that the function belongs to. */
6277 fn_class = DECL_CONTEXT (fn);
6279 /* The class that we're creating a pointer to member of. */
6280 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6282 /* First, calculate the adjustment to the function's class. */
6283 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6285 if (!DECL_VIRTUAL_P (fn))
6288 *idx = build_int_2 (-1, -1);
6291 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6292 *delta2 = NULL_TREE;
6296 /* If we're dealing with a virtual function, we have to adjust 'this'
6297 again, to point to the base which provides the vtable entry for
6298 fn; the call will do the opposite adjustment. */
6299 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6300 tree binfo = binfo_or_else (orig_class, fn_class);
6301 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6302 *delta, BINFO_OFFSET (binfo)));
6306 /* Map everything down one to make room for the null PMF. */
6307 *idx = fold (build (PLUS_EXPR, integer_type_node,
6308 DECL_VINDEX (fn), integer_one_node));
6313 /* Under the new ABI, we set PFN to the vtable offset, plus
6314 one, at which the function can be found. */
6316 *pfn = fold (build (MULT_EXPR, integer_type_node,
6318 TYPE_SIZE_UNIT (vtable_entry_type)));
6319 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
6321 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
6325 /* Offset from an object of PTR_CLASS to the vptr for ORIG_CLASS. */
6326 *delta2 = fold (build (PLUS_EXPR, integer_type_node, *delta,
6327 get_vfield_offset (TYPE_BINFO (orig_class))));
6331 /* Return an expression for DELTA2 from the pointer-to-member function
6335 delta2_from_ptrmemfunc (t)
6338 my_friendly_assert (!flag_new_abi, 20000221);
6340 if (TREE_CODE (t) == PTRMEM_CST)
6347 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6352 return (build_component_ref
6353 (build_component_ref (t,
6354 pfn_or_delta2_identifier, NULL_TREE,
6356 delta2_identifier, NULL_TREE, 0));
6359 /* Return an expression for PFN from the pointer-to-member function
6363 pfn_from_ptrmemfunc (t)
6366 if (TREE_CODE (t) == PTRMEM_CST)
6373 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6379 return build_component_ref (t, pfn_identifier, NULL_TREE, 0);
6381 return (build_component_ref
6382 (build_component_ref (t,
6383 pfn_or_delta2_identifier, NULL_TREE,
6385 pfn_identifier, NULL_TREE, 0));
6388 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6389 if this is a potentially dangerous thing to do. Returns a possibly
6393 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
6396 const char *errtype;
6400 if (TREE_CODE (type) == REFERENCE_TYPE)
6401 type = TREE_TYPE (type);
6403 /* Issue warnings about peculiar, but legal, uses of NULL. */
6404 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
6407 cp_warning ("passing NULL used for non-pointer %s %P of `%D'",
6408 errtype, parmnum, fndecl);
6410 cp_warning ("%s to non-pointer type `%T' from NULL", errtype, type);
6413 /* Warn about assigning a floating-point type to an integer type. */
6414 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
6415 && TREE_CODE (type) == INTEGER_TYPE)
6418 cp_warning ("passing `%T' for %s %P of `%D'",
6419 TREE_TYPE (expr), errtype, parmnum, fndecl);
6421 cp_warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
6423 /* And warn about assigning a negative value to an unsigned
6425 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
6427 if (TREE_CODE (expr) == INTEGER_CST
6428 && TREE_NEGATED_INT (expr))
6431 cp_warning ("passing negative value `%E' for %s %P of `%D'",
6432 expr, errtype, parmnum, fndecl);
6434 cp_warning ("%s of negative value `%E' to `%T'",
6435 errtype, expr, type);
6437 overflow_warning (expr);
6438 if (TREE_CONSTANT (expr))
6444 /* Convert value RHS to type TYPE as preparation for an assignment to
6445 an lvalue of type TYPE. ERRTYPE is a string to use in error
6446 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6447 are doing the conversion in order to pass the PARMNUMth argument of
6451 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6453 const char *errtype;
6457 register enum tree_code codel = TREE_CODE (type);
6458 register tree rhstype;
6459 register enum tree_code coder;
6461 if (codel == OFFSET_TYPE)
6462 my_friendly_abort (990505);
6464 if (TREE_CODE (rhs) == OFFSET_REF)
6465 rhs = resolve_offset_ref (rhs);
6467 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6468 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6469 rhs = TREE_OPERAND (rhs, 0);
6471 rhstype = TREE_TYPE (rhs);
6472 coder = TREE_CODE (rhstype);
6474 if (rhs == error_mark_node || rhstype == error_mark_node)
6475 return error_mark_node;
6476 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6477 return error_mark_node;
6479 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6481 /* The RHS of an assignment cannot have void type. */
6482 if (coder == VOID_TYPE)
6484 error ("void value not ignored as it ought to be");
6485 return error_mark_node;
6488 /* Simplify the RHS if possible. */
6489 if (TREE_CODE (rhs) == CONST_DECL)
6490 rhs = DECL_INITIAL (rhs);
6491 else if (coder != ARRAY_TYPE)
6492 rhs = decl_constant_value (rhs);
6496 The expression is implicitly converted (clause _conv_) to the
6497 cv-unqualified type of the left operand. */
6498 if (!can_convert_arg (type, rhstype, rhs))
6500 /* When -Wno-pmf-conversions is use, we just silently allow
6501 conversions from pointers-to-members to plain pointers. If
6502 the conversion doesn't work, cp_convert will complain. */
6504 && TYPE_PTR_P (type)
6505 && TYPE_PTRMEMFUNC_P (rhstype))
6506 rhs = cp_convert (strip_top_quals (type), rhs);
6509 /* If the right-hand side has unknown type, then it is an
6510 overloaded function. Call instantiate_type to get error
6512 if (rhstype == unknown_type_node)
6513 instantiate_type (type, rhs, 1);
6515 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6516 rhstype, type, parmnum, fndecl);
6518 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6520 return error_mark_node;
6523 return perform_implicit_conversion (strip_top_quals (type), rhs);
6526 /* Convert RHS to be of type TYPE.
6527 If EXP is non-zero, it is the target of the initialization.
6528 ERRTYPE is a string to use in error messages.
6530 Two major differences between the behavior of
6531 `convert_for_assignment' and `convert_for_initialization'
6532 are that references are bashed in the former, while
6533 copied in the latter, and aggregates are assigned in
6534 the former (operator=) while initialized in the
6537 If using constructor make sure no conversion operator exists, if one does
6538 exist, an ambiguity exists.
6540 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6543 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6544 tree exp, type, rhs;
6546 const char *errtype;
6550 register enum tree_code codel = TREE_CODE (type);
6551 register tree rhstype;
6552 register enum tree_code coder;
6554 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6555 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6556 if (TREE_CODE (rhs) == NOP_EXPR
6557 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6558 && codel != REFERENCE_TYPE)
6559 rhs = TREE_OPERAND (rhs, 0);
6561 if (rhs == error_mark_node
6562 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6563 return error_mark_node;
6565 if (TREE_CODE (rhs) == OFFSET_REF)
6567 rhs = resolve_offset_ref (rhs);
6568 if (rhs == error_mark_node)
6569 return error_mark_node;
6572 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6573 rhs = convert_from_reference (rhs);
6575 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6576 && TREE_CODE (type) != ARRAY_TYPE
6577 && (TREE_CODE (type) != REFERENCE_TYPE
6578 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6579 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6580 && (TREE_CODE (type) != REFERENCE_TYPE
6581 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6582 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6583 rhs = default_conversion (rhs);
6585 rhstype = TREE_TYPE (rhs);
6586 coder = TREE_CODE (rhstype);
6588 if (coder == ERROR_MARK)
6589 return error_mark_node;
6591 /* We accept references to incomplete types, so we can
6592 return here before checking if RHS is of complete type. */
6594 if (codel == REFERENCE_TYPE)
6596 /* This should eventually happen in convert_arguments. */
6597 extern int warningcount, errorcount;
6598 int savew = 0, savee = 0;
6601 savew = warningcount, savee = errorcount;
6602 rhs = initialize_reference (type, rhs);
6605 if (warningcount > savew)
6606 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6607 else if (errorcount > savee)
6608 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6614 exp = require_complete_type (exp);
6615 if (exp == error_mark_node)
6616 return error_mark_node;
6618 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6619 rhstype = TREE_TYPE (rhstype);
6621 type = complete_type (type);
6623 if (IS_AGGR_TYPE (type))
6624 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6626 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6629 /* Expand an ASM statement with operands, handling output operands
6630 that are not variables or INDIRECT_REFS by transforming such
6631 cases into cases that expand_asm_operands can handle.
6633 Arguments are same as for expand_asm_operands.
6635 We don't do default conversions on all inputs, because it can screw
6636 up operands that are expected to be in memory. */
6639 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6640 tree string, outputs, inputs, clobbers;
6645 int noutputs = list_length (outputs);
6647 /* o[I] is the place that output number I should be written. */
6648 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6651 /* Record the contents of OUTPUTS before it is modified. */
6652 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6653 o[i] = TREE_VALUE (tail);
6655 /* Generate the ASM_OPERANDS insn;
6656 store into the TREE_VALUEs of OUTPUTS some trees for
6657 where the values were actually stored. */
6658 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6660 /* Copy all the intermediate outputs into the specified outputs. */
6661 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6663 if (o[i] != TREE_VALUE (tail))
6665 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6666 const0_rtx, VOIDmode, EXPAND_NORMAL);
6669 /* Restore the original value so that it's correct the next
6670 time we expand this function. */
6671 TREE_VALUE (tail) = o[i];
6673 /* Detect modification of read-only values.
6674 (Otherwise done by build_modify_expr.) */
6677 tree type = TREE_TYPE (o[i]);
6678 if (CP_TYPE_CONST_P (type)
6679 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6680 && C_TYPE_FIELDS_READONLY (type)))
6681 readonly_error (o[i], "modification by `asm'", 1);
6685 /* Those MODIFY_EXPRs could do autoincrements. */
6689 /* If RETVAL is the address of, or a reference to, a local variable or
6690 temporary give an appropraite warning. */
6693 maybe_warn_about_returning_address_of_local (retval)
6696 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6697 tree whats_returned = retval;
6701 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6702 whats_returned = TREE_OPERAND (whats_returned, 1);
6703 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6704 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6705 || TREE_CODE (whats_returned) == NOP_EXPR)
6706 whats_returned = TREE_OPERAND (whats_returned, 0);
6711 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6713 whats_returned = TREE_OPERAND (whats_returned, 0);
6715 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6717 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6718 || TREE_CODE (whats_returned) == TARGET_EXPR)
6720 /* Get the target. */
6721 whats_returned = TREE_OPERAND (whats_returned, 0);
6722 warning ("returning reference to temporary");
6725 if (TREE_CODE (whats_returned) == VAR_DECL
6726 && DECL_NAME (whats_returned)
6727 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6729 warning ("reference to non-lvalue returned");
6734 if (TREE_CODE (whats_returned) == VAR_DECL
6735 && DECL_NAME (whats_returned)
6736 && DECL_FUNCTION_SCOPE_P (whats_returned)
6737 && !(TREE_STATIC (whats_returned)
6738 || TREE_PUBLIC (whats_returned)))
6740 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6741 cp_warning_at ("reference to local variable `%D' returned",
6744 cp_warning_at ("address of local variable `%D' returned",
6750 /* Check that returning RETVAL from the current function is legal.
6751 Return an expression explicitly showing all conversions required to
6752 change RETVAL into the function return type, and to assign it to
6753 the DECL_RESULT for the function. */
6756 check_return_expr (retval)
6760 /* The type actually returned by the function, after any
6763 int fn_returns_value_p;
6765 /* A `volatile' function is one that isn't supposed to return, ever.
6766 (This is a G++ extension, used to get better code for functions
6767 that call the `volatile' function.) */
6768 if (TREE_THIS_VOLATILE (current_function_decl))
6769 warning ("function declared `noreturn' has a `return' statement");
6771 /* Check for various simple errors. */
6772 if (retval == error_mark_node)
6774 /* If an error occurred, there's nothing to do. */
6775 current_function_returns_null = 1;
6776 return error_mark_node;
6778 else if (dtor_label)
6781 error ("returning a value from a destructor");
6784 else if (in_function_try_handler
6785 && DECL_CONSTRUCTOR_P (current_function_decl))
6787 /* If a return statement appears in a handler of the
6788 function-try-block of a constructor, the program is ill-formed. */
6789 error ("cannot return from a handler of a function-try-block of a constructor");
6790 return error_mark_node;
6792 else if (retval && DECL_CONSTRUCTOR_P (current_function_decl))
6793 /* You can't return a value from a constructor. */
6794 error ("returning a value from a constructor");
6796 /* Under the old ABI, constructors actually always return `this',
6797 even though in C++ you can't return a value from a constructor. */
6798 if (!flag_new_abi && DECL_CONSTRUCTOR_P (current_function_decl))
6799 retval = current_class_ptr;
6801 /* When no explicit return-value is given in a function with a named
6802 return value, the named return value is used. */
6803 result = DECL_RESULT (current_function_decl);
6804 valtype = TREE_TYPE (result);
6805 my_friendly_assert (valtype != NULL_TREE, 19990924);
6806 fn_returns_value_p = !same_type_p (valtype, void_type_node);
6807 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6810 /* Check for a return statement with no return value in a function
6811 that's supposed to return a value. */
6812 if (!retval && fn_returns_value_p)
6814 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6815 /* Clear this, so finish_function won't say that we reach the
6816 end of a non-void function (which we don't, we gave a
6818 current_function_returns_null = 0;
6820 /* Check for a return statement with a value in a function that
6821 isn't supposed to return a value. */
6822 else if (retval && !fn_returns_value_p)
6824 if (same_type_p (TREE_TYPE (retval), void_type_node))
6825 /* You can return a `void' value from a function of `void'
6826 type. In that case, we have to evaluate the expression for
6827 its side-effects. */
6828 finish_expr_stmt (retval);
6830 pedwarn ("return-statement with a value, in function declared with a void return type");
6832 current_function_returns_null = 1;
6834 /* There's really no value to return, after all. */
6838 /* Remember that this function can sometimes return without a
6840 current_function_returns_null = 1;
6842 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6843 if ((DECL_NAME (current_function_decl) == ansi_opname[(int) NEW_EXPR]
6844 || DECL_NAME (current_function_decl) == ansi_opname[(int) VEC_NEW_EXPR])
6845 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6846 && null_ptr_cst_p (retval))
6847 cp_warning ("`operator new' should throw an exception, not return NULL");
6849 /* Effective C++ rule 15. See also start_function. */
6851 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
6852 && retval != current_class_ref)
6853 cp_warning ("`operator=' should return a reference to `*this'");
6855 /* We don't need to do any conversions when there's nothing being
6860 /* Do any required conversions. */
6861 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6862 /* No conversions are required. */
6866 /* The type the function is declared to return. */
6867 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6869 /* First convert the value to the function's return type, then
6870 to the type of return value's location to handle the
6871 case that functype is thiner than the valtype. */
6872 retval = convert_for_initialization
6873 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6874 "return", NULL_TREE, 0);
6875 retval = convert (valtype, retval);
6877 /* If the conversion failed, treat this just like `return;'. */
6878 if (retval == error_mark_node)
6880 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6881 else if (! current_function_returns_struct
6882 && TREE_CODE (retval) == TARGET_EXPR
6883 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6884 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6885 TREE_OPERAND (retval, 0));
6887 maybe_warn_about_returning_address_of_local (retval);
6890 /* Actually copy the value returned into the appropriate location. */
6891 if (retval && retval != result)
6892 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6894 /* All done. Remember that this function did return a value. */
6895 current_function_returns_value = 1;
6899 /* Expand a C `return' statement.
6900 RETVAL is the expression for what to return,
6901 or a null pointer for `return;' with no value.
6903 C++: upon seeing a `return', we must call destructors on all
6904 variables in scope which had constructors called on them.
6905 This means that if in a destructor, the base class destructors
6906 must be called before returning.
6908 The RETURN statement in C++ has initialization semantics. */
6911 c_expand_return (retval)
6915 expand_null_return ();
6918 expand_start_target_temps ();
6919 expand_return (retval);
6920 expand_end_target_temps ();
6924 /* Start a C switch statement, testing expression EXP.
6925 Return EXP if it is valid, an error node otherwise. */
6928 c_expand_start_case (exp)
6931 expand_start_case (1, exp, TREE_TYPE (exp), "switch statement");
6936 /* Returns non-zero if the pointer-type FROM can be converted to the
6937 pointer-type TO via a qualification conversion. If CONSTP is -1,
6938 then we return non-zero if the pointers are similar, and the
6939 cv-qualification signature of FROM is a proper subset of that of TO.
6941 If CONSTP is positive, then all outer pointers have been
6945 comp_ptr_ttypes_real (to, from, constp)
6949 int to_more_cv_qualified = 0;
6951 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6953 if (TREE_CODE (to) != TREE_CODE (from))
6956 if (TREE_CODE (from) == OFFSET_TYPE
6957 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6958 TYPE_OFFSET_BASETYPE (to)))
6961 /* Const and volatile mean something different for function types,
6962 so the usual checks are not appropriate. */
6963 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6965 if (!at_least_as_qualified_p (to, from))
6968 if (!at_least_as_qualified_p (from, to))
6973 ++to_more_cv_qualified;
6977 constp &= TYPE_READONLY (to);
6980 if (TREE_CODE (to) != POINTER_TYPE)
6982 same_type_ignoring_top_level_qualifiers_p (to, from)
6983 && (constp >= 0 || to_more_cv_qualified);
6987 /* When comparing, say, char ** to char const **, this function takes the
6988 'char *' and 'char const *'. Do not pass non-pointer types to this
6992 comp_ptr_ttypes (to, from)
6995 return comp_ptr_ttypes_real (to, from, 1);
6998 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6999 type or inheritance-related types, regardless of cv-quals. */
7002 ptr_reasonably_similar (to, from)
7005 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7007 if (TREE_CODE (to) != TREE_CODE (from))
7010 if (TREE_CODE (from) == OFFSET_TYPE
7011 && comptypes (TYPE_OFFSET_BASETYPE (to),
7012 TYPE_OFFSET_BASETYPE (from),
7013 COMPARE_BASE | COMPARE_RELAXED))
7016 if (TREE_CODE (to) != POINTER_TYPE)
7018 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
7019 COMPARE_BASE | COMPARE_RELAXED);
7023 /* Like comp_ptr_ttypes, for const_cast. */
7026 comp_ptr_ttypes_const (to, from)
7029 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7031 if (TREE_CODE (to) != TREE_CODE (from))
7034 if (TREE_CODE (from) == OFFSET_TYPE
7035 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7036 TYPE_OFFSET_BASETYPE (to)))
7039 if (TREE_CODE (to) != POINTER_TYPE)
7040 return same_type_ignoring_top_level_qualifiers_p (to, from);
7044 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7047 comp_ptr_ttypes_reinterpret (to, from)
7052 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7054 if (TREE_CODE (from) == OFFSET_TYPE)
7055 from = TREE_TYPE (from);
7056 if (TREE_CODE (to) == OFFSET_TYPE)
7057 to = TREE_TYPE (to);
7059 /* Const and volatile mean something different for function types,
7060 so the usual checks are not appropriate. */
7061 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7062 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7064 if (!at_least_as_qualified_p (to, from))
7068 && !at_least_as_qualified_p (from, to))
7070 constp &= TYPE_READONLY (to);
7073 if (TREE_CODE (from) != POINTER_TYPE
7074 || TREE_CODE (to) != POINTER_TYPE)
7079 /* Recursively examines the array elements of TYPE, until a non-array
7080 element type is found. */
7083 strip_array_types (type)
7086 while (TREE_CODE (type) == ARRAY_TYPE)
7087 type = TREE_TYPE (type);
7092 /* Returns the type-qualifier set corresponding to TYPE. */
7095 cp_type_quals (type)
7098 return TYPE_QUALS (strip_array_types (type));
7101 /* Returns non-zero if the TYPE contains a mutable member */
7104 cp_has_mutable_p (type)
7107 while (TREE_CODE (type) == ARRAY_TYPE)
7108 type = TREE_TYPE (type);
7110 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7113 /* Subroutine of casts_away_constness. Make T1 and T2 point at
7114 exemplar types such that casting T1 to T2 is casting away castness
7115 if and only if there is no implicit conversion from T1 to T2. */
7118 casts_away_constness_r (t1, t2)
7125 /* [expr.const.cast]
7127 For multi-level pointer to members and multi-level mixed pointers
7128 and pointers to members (conv.qual), the "member" aspect of a
7129 pointer to member level is ignored when determining if a const
7130 cv-qualifier has been cast away. */
7131 if (TYPE_PTRMEM_P (*t1))
7132 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
7133 if (TYPE_PTRMEM_P (*t2))
7134 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
7136 /* [expr.const.cast]
7138 For two pointer types:
7140 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
7141 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
7144 casting from X1 to X2 casts away constness if, for a non-pointer
7145 type T there does not exist an implicit conversion (clause
7148 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7152 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
7154 if (TREE_CODE (*t1) != POINTER_TYPE
7155 || TREE_CODE (*t2) != POINTER_TYPE)
7157 *t1 = cp_build_qualified_type (void_type_node,
7158 CP_TYPE_QUALS (*t1));
7159 *t2 = cp_build_qualified_type (void_type_node,
7160 CP_TYPE_QUALS (*t2));
7164 quals1 = CP_TYPE_QUALS (*t1);
7165 quals2 = CP_TYPE_QUALS (*t2);
7166 *t1 = TREE_TYPE (*t1);
7167 *t2 = TREE_TYPE (*t2);
7168 casts_away_constness_r (t1, t2);
7169 *t1 = build_pointer_type (*t1);
7170 *t2 = build_pointer_type (*t2);
7171 *t1 = cp_build_qualified_type (*t1, quals1);
7172 *t2 = cp_build_qualified_type (*t2, quals2);
7175 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
7179 casts_away_constness (t1, t2)
7183 if (TREE_CODE (t2) == REFERENCE_TYPE)
7185 /* [expr.const.cast]
7187 Casting from an lvalue of type T1 to an lvalue of type T2
7188 using a reference cast casts away constness if a cast from an
7189 rvalue of type "pointer to T1" to the type "pointer to T2"
7190 casts away constness. */
7191 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
7192 ? TREE_TYPE (t1) : t1);
7193 return casts_away_constness (build_pointer_type (t1),
7194 build_pointer_type (TREE_TYPE (t2)));
7197 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7198 /* [expr.const.cast]
7200 Casting from an rvalue of type "pointer to data member of X
7201 of type T1" to the type "pointer to data member of Y of type
7202 T2" casts away constness if a cast from an rvalue of type
7203 "poitner to T1" to the type "pointer to T2" casts away
7205 return casts_away_constness
7206 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7207 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7209 /* Casting away constness is only something that makes sense for
7210 pointer or reference types. */
7211 if (TREE_CODE (t1) != POINTER_TYPE
7212 || TREE_CODE (t2) != POINTER_TYPE)
7215 /* Top-level qualifiers don't matter. */
7216 t1 = TYPE_MAIN_VARIANT (t1);
7217 t2 = TYPE_MAIN_VARIANT (t2);
7218 casts_away_constness_r (&t1, &t2);
7219 if (!can_convert (t2, t1))
7225 /* Returns TYPE with its cv qualifiers removed
7226 TYPE is T cv* .. *cv where T is not a pointer type,
7227 returns T * .. *. (If T is an array type, then the cv qualifiers
7228 above are those of the array members.) */
7231 strip_all_pointer_quals (type)
7234 if (TREE_CODE (type) == POINTER_TYPE)
7235 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
7237 return TYPE_MAIN_VARIANT (type);