1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization.
29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30 and to process initializations in declarations (since they work
31 like a strange sort of assignment). */
43 #include "diagnostic.h"
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));
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));
57 static tree lookup_anon_field PARAMS ((tree, tree));
58 static tree pointer_diff PARAMS ((tree, tree, tree));
59 static tree build_component_addr PARAMS ((tree, tree));
60 static tree qualify_type_recursive PARAMS ((tree, tree));
61 static tree get_delta_difference PARAMS ((tree, tree, int));
62 static int comp_cv_target_types PARAMS ((tree, tree, int));
63 static void casts_away_constness_r PARAMS ((tree *, tree *));
64 static int casts_away_constness PARAMS ((tree, tree));
65 static void maybe_warn_about_returning_address_of_local PARAMS ((tree));
66 static tree strip_all_pointer_quals PARAMS ((tree));
68 /* Return the target type of TYPE, which means return T for:
69 T*, T&, T[], T (...), and otherwise, just T. */
75 if (TREE_CODE (type) == REFERENCE_TYPE)
76 type = TREE_TYPE (type);
77 while (TREE_CODE (type) == POINTER_TYPE
78 || TREE_CODE (type) == ARRAY_TYPE
79 || TREE_CODE (type) == FUNCTION_TYPE
80 || TREE_CODE (type) == METHOD_TYPE
81 || TREE_CODE (type) == OFFSET_TYPE)
82 type = TREE_TYPE (type);
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.)
88 Returns the error_mark_node if the VALUE does not have
89 complete type when this function returns. */
92 require_complete_type (value)
97 if (processing_template_decl || value == error_mark_node)
100 if (TREE_CODE (value) == OVERLOAD)
101 type = unknown_type_node;
103 type = TREE_TYPE (value);
105 /* First, detect a valid value with a complete type. */
106 if (COMPLETE_TYPE_P (type))
109 /* If we see X::Y, we build an OFFSET_TYPE which has
110 not been laid out. Try to avoid an error by interpreting
111 it as this->X::Y, if reasonable. */
112 if (TREE_CODE (value) == OFFSET_REF
113 && current_class_ref != 0
114 && TREE_OPERAND (value, 0) == current_class_ref)
116 tree base, member = TREE_OPERAND (value, 1);
117 tree basetype = TYPE_OFFSET_BASETYPE (type);
118 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
119 base = convert_pointer_to (basetype, current_class_ptr);
120 value = build (COMPONENT_REF, TREE_TYPE (member),
121 build_indirect_ref (base, NULL), member);
122 return require_complete_type (value);
125 if (complete_type_or_else (type, value))
128 return error_mark_node;
131 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
132 a template instantiation, do the instantiation. Returns TYPE,
133 whether or not it could be completed, unless something goes
134 horribly wrong, in which case the error_mark_node is returned. */
140 if (type == NULL_TREE)
141 /* Rather than crash, we return something sure to cause an error
143 return error_mark_node;
145 if (type == error_mark_node || COMPLETE_TYPE_P (type))
147 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
149 tree t = complete_type (TREE_TYPE (type));
150 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
152 TYPE_NEEDS_CONSTRUCTING (type)
153 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
154 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
155 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
157 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
158 instantiate_class_template (TYPE_MAIN_VARIANT (type));
163 /* Like complete_type, but issue an error if the TYPE cannot be
164 completed. VALUE is used for informative diagnostics.
165 Returns NULL_TREE if the type cannot be made complete. */
168 complete_type_or_else (type, value)
172 type = complete_type (type);
173 if (type == error_mark_node)
174 /* We already issued an error. */
176 else if (!COMPLETE_TYPE_P (type))
178 incomplete_type_error (value, type);
185 /* Return truthvalue of whether type of EXP is instantiated. */
191 return (TREE_CODE (exp) == OVERLOAD
192 || TREE_CODE (exp) == TREE_LIST
193 || TREE_TYPE (exp) == unknown_type_node
194 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
195 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
198 /* Return a pointer or pointer to member type similar to T1, with a
199 cv-qualification signature that is the union of the cv-qualification
200 signatures of T1 and T2: [expr.rel], [expr.eq]. */
203 qualify_type_recursive (t1, t2)
206 if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
207 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
209 tree tt1 = TREE_TYPE (t1);
210 tree tt2 = TREE_TYPE (t2);
214 tree attributes = merge_machine_type_attributes (t1, t2);
216 if (TREE_CODE (tt1) == OFFSET_TYPE)
218 b1 = TYPE_OFFSET_BASETYPE (tt1);
219 tt1 = TREE_TYPE (tt1);
220 tt2 = TREE_TYPE (tt2);
225 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
226 target = qualify_type_recursive (tt1, tt2);
227 target = cp_build_qualified_type (target, type_quals);
229 target = build_offset_type (b1, target);
230 t1 = build_pointer_type (target);
231 t1 = build_type_attribute_variant (t1, attributes);
236 /* Return the common type of two parameter lists.
237 We assume that comptypes has already been done and returned 1;
238 if that isn't so, this may crash.
240 As an optimization, free the space we allocate if the parameter
241 lists are already common. */
247 tree oldargs = p1, newargs, n;
251 len = list_length (p1);
252 newargs = tree_last (p1);
254 if (newargs == void_list_node)
263 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
268 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
270 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
272 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
275 else if (! TREE_PURPOSE (p1))
277 if (TREE_PURPOSE (p2))
279 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
285 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
287 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
289 if (TREE_VALUE (p1) != TREE_VALUE (p2))
292 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
295 TREE_VALUE (n) = TREE_VALUE (p1);
303 /* Given a type, perhaps copied for a typedef,
304 find the "original" version of it. */
309 while (TYPE_NAME (t) != NULL_TREE)
311 tree x = TYPE_NAME (t);
312 if (TREE_CODE (x) != TYPE_DECL)
314 x = DECL_ORIGINAL_TYPE (x);
322 /* T1 and T2 are arithmetic or enumeration types. Return the type
323 that will result from the "usual arithmetic converions" on T1 and
324 T2 as described in [expr]. */
327 type_after_usual_arithmetic_conversions (t1, t2)
331 enum tree_code code1 = TREE_CODE (t1);
332 enum tree_code code2 = TREE_CODE (t2);
335 /* FIXME: Attributes. */
336 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
337 || TREE_CODE (t1) == ENUMERAL_TYPE,
339 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
340 || TREE_CODE (t2) == ENUMERAL_TYPE,
343 /* In what follows, we slightly generalize the rules given in [expr]
344 so as to deal with `long long'. First, merge the attributes. */
345 attributes = merge_machine_type_attributes (t1, t2);
347 /* If only one is real, use it as the result. */
348 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
349 return build_type_attribute_variant (t1, attributes);
350 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
351 return build_type_attribute_variant (t2, attributes);
353 /* Perform the integral promotions. */
354 if (code1 != REAL_TYPE)
356 t1 = type_promotes_to (t1);
357 t2 = type_promotes_to (t2);
360 /* Both real or both integers; use the one with greater precision. */
361 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
362 return build_type_attribute_variant (t1, attributes);
363 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
364 return build_type_attribute_variant (t2, attributes);
366 if (code1 != REAL_TYPE)
368 /* If one is a sizetype, use it so size_binop doesn't blow up. */
369 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
370 return build_type_attribute_variant (t1, attributes);
371 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
372 return build_type_attribute_variant (t2, attributes);
374 /* If one is unsigned long long, then convert the other to unsigned
376 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
377 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
378 return build_type_attribute_variant (long_long_unsigned_type_node,
380 /* If one is a long long, and the other is an unsigned long, and
381 long long can represent all the values of an unsigned long, then
382 convert to a long long. Otherwise, convert to an unsigned long
383 long. Otherwise, if either operand is long long, convert the
386 Since we're here, we know the TYPE_PRECISION is the same;
387 therefore converting to long long cannot represent all the values
388 of an unsigned long, so we choose unsigned long long in that
390 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
391 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
393 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
394 ? long_long_unsigned_type_node
395 : long_long_integer_type_node);
396 return build_type_attribute_variant (t, attributes);
399 /* Go through the same procedure, but for longs. */
400 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
401 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
402 return build_type_attribute_variant (long_unsigned_type_node,
404 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
405 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
407 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
408 ? long_unsigned_type_node : long_integer_type_node);
409 return build_type_attribute_variant (t, attributes);
411 /* Otherwise prefer the unsigned one. */
412 if (TREE_UNSIGNED (t1))
413 return build_type_attribute_variant (t1, attributes);
415 return build_type_attribute_variant (t2, attributes);
419 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
420 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
421 return build_type_attribute_variant (long_double_type_node,
423 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
424 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
425 return build_type_attribute_variant (double_type_node,
428 return build_type_attribute_variant (float_type_node,
433 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
434 ARG1 and ARG2 are the values with those types. The LOCATION is a
435 string describing the current location, in case an error occurs. */
438 composite_pointer_type (t1, t2, arg1, arg2, location)
443 const char* location;
449 If one operand is a null pointer constant, the composite pointer
450 type is the type of the other operand. */
451 if (null_ptr_cst_p (arg1))
453 if (null_ptr_cst_p (arg2))
456 /* Deal with pointer-to-member functions in the same way as we deal
457 with pointers to functions. */
458 if (TYPE_PTRMEMFUNC_P (t1))
459 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
460 if (TYPE_PTRMEMFUNC_P (t2))
461 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
467 If one of the operands has type "pointer to cv1 void*", then
468 the other has type "pointer to cv2T", and the composite pointer
469 type is "pointer to cv12 void", where cv12 is the union of cv1
472 If either type is a pointer to void, make sure it is T1. */
473 if (VOID_TYPE_P (TREE_TYPE (t2)))
480 /* Now, if T1 is a pointer to void, merge the qualifiers. */
481 if (VOID_TYPE_P (TREE_TYPE (t1)))
483 if (pedantic && TYPE_PTRFN_P (t2))
484 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
487 result_type = cp_build_qualified_type (void_type_node,
489 | CP_TYPE_QUALS (t2)));
490 result_type = build_pointer_type (result_type);
494 tree full1 = qualify_type_recursive (t1, t2);
495 tree full2 = qualify_type_recursive (t2, t1);
497 int val = comp_target_types (full1, full2, 1);
505 cp_pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
507 result_type = ptr_type_node;
514 /* Return the common type of two types.
515 We assume that comptypes has already been done and returned 1;
516 if that isn't so, this may crash.
518 This is the type for the result of most arithmetic operations
519 if the operands have the given two types.
521 We do not deal with enumeral types here because they have already been
522 converted to integer types. */
528 register enum tree_code code1;
529 register enum tree_code code2;
532 /* Save time if the two types are the same. */
535 t1 = original_type (t1);
536 t2 = original_type (t2);
540 /* If one type is nonsense, use the other. */
541 if (t1 == error_mark_node)
543 if (t2 == error_mark_node)
546 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
547 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
548 return type_after_usual_arithmetic_conversions (t1, t2);
550 /* Merge the attributes. */
551 attributes = merge_machine_type_attributes (t1, t2);
553 /* Treat an enum type as the unsigned integer type of the same width. */
555 if (TREE_CODE (t1) == ENUMERAL_TYPE)
556 t1 = type_for_size (TYPE_PRECISION (t1), 1);
557 if (TREE_CODE (t2) == ENUMERAL_TYPE)
558 t2 = type_for_size (TYPE_PRECISION (t2), 1);
560 if (TYPE_PTRMEMFUNC_P (t1))
561 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
562 if (TYPE_PTRMEMFUNC_P (t2))
563 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
565 code1 = TREE_CODE (t1);
566 code2 = TREE_CODE (t2);
568 /* If one type is complex, form the common type of the non-complex
569 components, then make that complex. Use T1 or T2 if it is the
571 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
573 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
574 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
575 tree subtype = common_type (subtype1, subtype2);
577 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
578 return build_type_attribute_variant (t1, attributes);
579 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
580 return build_type_attribute_variant (t2, attributes);
582 return build_type_attribute_variant (build_complex_type (subtype),
590 /* We should have called type_after_usual_arithmetic_conversions
592 my_friendly_abort (19990725);
597 /* For two pointers, do this recursively on the target type,
598 and combine the qualifiers of the two types' targets. */
599 /* This code was turned off; I don't know why.
600 But ISO C++ specifies doing this with the qualifiers.
601 So I turned it on again. */
603 tree tt1 = TREE_TYPE (t1);
604 tree tt2 = TREE_TYPE (t2);
609 if (TREE_CODE (tt1) == OFFSET_TYPE)
611 b1 = TYPE_OFFSET_BASETYPE (tt1);
612 b2 = TYPE_OFFSET_BASETYPE (tt2);
613 tt1 = TREE_TYPE (tt1);
614 tt2 = TREE_TYPE (tt2);
619 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
620 tt1 = TYPE_MAIN_VARIANT (tt1);
621 tt2 = TYPE_MAIN_VARIANT (tt2);
625 else if (VOID_TYPE_P (tt1) || VOID_TYPE_P (tt2))
626 target = void_type_node;
627 else if (tt1 == unknown_type_node)
629 else if (tt2 == unknown_type_node)
632 target = common_type (tt1, tt2);
634 target = cp_build_qualified_type (target, type_quals);
638 if (same_type_p (b1, b2)
639 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
640 target = build_offset_type (b2, target);
641 else if (binfo_or_else (b2, b1))
642 target = build_offset_type (b1, target);
645 if (code1 == POINTER_TYPE)
646 t1 = build_pointer_type (target);
648 t1 = build_reference_type (target);
649 t1 = build_type_attribute_variant (t1, attributes);
651 if (TREE_CODE (target) == METHOD_TYPE)
652 t1 = build_ptrmemfunc_type (t1);
659 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
660 /* Save space: see if the result is identical to one of the args. */
661 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
662 return build_type_attribute_variant (t1, attributes);
663 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
664 return build_type_attribute_variant (t2, attributes);
665 /* Merge the element types, and have a size if either arg has one. */
666 t1 = build_cplus_array_type
667 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
668 return build_type_attribute_variant (t1, attributes);
672 /* Function types: prefer the one that specified arg types.
673 If both do, merge the arg types. Also merge the return types. */
675 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
676 tree p1 = TYPE_ARG_TYPES (t1);
677 tree p2 = TYPE_ARG_TYPES (t2);
680 /* Save space: see if the result is identical to one of the args. */
681 if (valtype == TREE_TYPE (t1) && ! p2)
682 return build_type_attribute_variant (t1, attributes);
683 if (valtype == TREE_TYPE (t2) && ! p1)
684 return build_type_attribute_variant (t2, attributes);
686 /* Simple way if one arg fails to specify argument types. */
687 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
689 rval = build_function_type (valtype, p2);
690 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
691 rval = build_exception_variant (rval, raises);
692 return build_type_attribute_variant (rval, attributes);
694 raises = TYPE_RAISES_EXCEPTIONS (t1);
695 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
697 rval = build_function_type (valtype, p1);
699 rval = build_exception_variant (rval, raises);
700 return build_type_attribute_variant (rval, attributes);
703 rval = build_function_type (valtype, commonparms (p1, p2));
704 rval = build_exception_variant (rval, raises);
705 return build_type_attribute_variant (rval, attributes);
710 t1 = TYPE_MAIN_VARIANT (t1);
711 t2 = TYPE_MAIN_VARIANT (t2);
713 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
714 return build_type_attribute_variant (t1, attributes);
715 else if (binfo_or_else (t2, t1))
716 return build_type_attribute_variant (t2, attributes);
719 compiler_error ("common_type called with uncommon aggregate types");
720 return error_mark_node;
724 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
726 /* Get this value the long way, since TYPE_METHOD_BASETYPE
727 is just the main variant of this. */
731 tree b1 = TYPE_OFFSET_BASETYPE (t1);
732 tree b2 = TYPE_OFFSET_BASETYPE (t2);
734 if (same_type_p (b1, b2)
735 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
736 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
739 if (binfo_or_else (b2, b1) == NULL_TREE)
740 compiler_error ("common_type called with uncommon method types");
741 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
744 raises = TYPE_RAISES_EXCEPTIONS (t1);
746 /* If this was a member function type, get back to the
747 original type of type member function (i.e., without
748 the class instance variable up front. */
749 t1 = build_function_type (TREE_TYPE (t1),
750 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
751 t2 = build_function_type (TREE_TYPE (t2),
752 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
753 t3 = common_type (t1, t2);
754 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
755 TYPE_ARG_TYPES (t3));
756 t1 = build_exception_variant (t3, raises);
759 compiler_error ("common_type called with uncommon method types");
761 return build_type_attribute_variant (t1, attributes);
764 /* Pointers to members should now be handled by the POINTER_TYPE
766 my_friendly_abort (990325);
769 return build_type_attribute_variant (t1, attributes);
773 /* Compare two exception specifier types for exactness or subsetness, if
774 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
776 [except.spec] "If a class X ... objects of class X or any class publicly
777 and unambigously derrived from X. Similarly, if a pointer type Y * ...
778 exceptions of type Y * or that are pointers to any type publicly and
779 unambigously derrived from Y. Otherwise a function only allows exceptions
780 that have the same type ..."
781 This does not mention cv qualifiers and is different to what throw
782 [except.throw] and catch [except.catch] will do. They will ignore the
783 top level cv qualifiers, and allow qualifiers in the pointer to class
786 We implement the letter of the standard. */
789 comp_except_types (a, b, exact)
793 if (same_type_p (a, b))
797 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
800 if (TREE_CODE (a) == POINTER_TYPE
801 && TREE_CODE (b) == POINTER_TYPE)
805 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
809 if (TREE_CODE (a) != RECORD_TYPE
810 || TREE_CODE (b) != RECORD_TYPE)
813 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
819 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
820 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
821 otherwise it must be exact. Exception lists are unordered, but
822 we've already filtered out duplicates. Most lists will be in order,
823 we should try to make use of that. */
826 comp_except_specs (t1, t2, exact)
837 if (t1 == NULL_TREE) /* T1 is ... */
838 return t2 == NULL_TREE || !exact;
839 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
840 return t2 != NULL_TREE && !TREE_VALUE (t2);
841 if (t2 == NULL_TREE) /* T2 is ... */
843 if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
846 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
847 Count how many we find, to determine exactness. For exact matching and
848 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
850 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
852 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
854 tree a = TREE_VALUE (probe);
855 tree b = TREE_VALUE (t2);
857 if (comp_except_types (a, b, exact))
859 if (probe == base && exact)
860 base = TREE_CHAIN (probe);
865 if (probe == NULL_TREE)
868 return !exact || base == NULL_TREE || length == list_length (t1);
871 /* Compare the array types T1 and T2, using CMP as the type comparison
872 function for the element types. STRICT is as for comptypes. */
875 comp_array_types (cmp, t1, t2, strict)
876 register int (*cmp) PARAMS ((tree, tree, int));
886 /* The type of the array elements must be the same. */
887 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
888 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
889 strict & ~COMPARE_REDECLARATION)))
892 d1 = TYPE_DOMAIN (t1);
893 d2 = TYPE_DOMAIN (t2);
898 /* If one of the arrays is dimensionless, and the other has a
899 dimension, they are of different types. However, it is legal to
907 declarations for an array object can specify
908 array types that differ by the presence or absence of a major
909 array bound (_dcl.array_). */
911 return strict & COMPARE_REDECLARATION;
913 /* Check that the dimensions are the same. */
914 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
916 && cp_tree_equal (TYPE_MAX_VALUE (d1),
917 TYPE_MAX_VALUE (d2)));
920 /* Return 1 if T1 and T2 are compatible types for assignment or
921 various other operations. STRICT is a bitwise-or of the COMPARE_*
925 comptypes (t1, t2, strict)
931 int orig_strict = strict;
933 /* The special exemption for redeclaring array types without an
934 array bound only applies at the top level:
939 is not legal, for example. */
940 strict &= ~COMPARE_REDECLARATION;
942 /* Suppress errors caused by previously reported errors */
946 /* This should never happen. */
947 my_friendly_assert (t1 != error_mark_node, 307);
949 if (t2 == error_mark_node)
952 /* If either type is the internal version of sizetype, return the
954 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
955 && TYPE_DOMAIN (t1) != 0)
956 t1 = TYPE_DOMAIN (t1);
958 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
959 && TYPE_DOMAIN (t2) != 0)
960 t2 = TYPE_DOMAIN (t2);
962 if (strict & COMPARE_RELAXED)
964 /* Treat an enum type as the unsigned integer type of the same width. */
966 if (TREE_CODE (t1) == ENUMERAL_TYPE)
967 t1 = type_for_size (TYPE_PRECISION (t1), 1);
968 if (TREE_CODE (t2) == ENUMERAL_TYPE)
969 t2 = type_for_size (TYPE_PRECISION (t2), 1);
975 if (TYPE_PTRMEMFUNC_P (t1))
976 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
977 if (TYPE_PTRMEMFUNC_P (t2))
978 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
980 /* Different classes of types can't be compatible. */
981 if (TREE_CODE (t1) != TREE_CODE (t2))
984 /* Qualifiers must match. */
985 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
987 if (strict == COMPARE_STRICT
988 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
991 /* Allow for two different type nodes which have essentially the same
992 definition. Note that we already checked for equality of the type
993 qualifiers (just above). */
995 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
998 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
999 attribute is its own main variant (`val' will remain 0). */
1000 #ifndef COMP_TYPE_ATTRIBUTES
1001 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
1004 if (strict & COMPARE_NO_ATTRIBUTES)
1006 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1007 else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
1010 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1013 switch (TREE_CODE (t1))
1015 case TEMPLATE_TEMPLATE_PARM:
1016 case BOUND_TEMPLATE_TEMPLATE_PARM:
1017 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1018 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1020 if (! comp_template_parms
1021 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1022 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1024 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1026 /* Don't check inheritance. */
1027 strict = COMPARE_STRICT;
1032 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1033 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1034 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
1035 val = comp_template_args (TYPE_TI_ARGS (t1),
1038 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1040 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1045 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1046 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1047 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1051 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1052 TYPE_RAISES_EXCEPTIONS (t2), 1))
1055 /* This case is anti-symmetrical!
1056 One can pass a base member (or member function)
1057 to something expecting a derived member (or member function),
1058 but not vice-versa! */
1060 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1061 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1065 case REFERENCE_TYPE:
1066 t1 = TREE_TYPE (t1);
1067 t2 = TREE_TYPE (t2);
1068 /* first, check whether the referred types match with the
1069 required level of strictness */
1070 val = comptypes (t1, t2, strict);
1073 if (TREE_CODE (t1) == RECORD_TYPE
1074 && TREE_CODE (t2) == RECORD_TYPE)
1079 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1080 TYPE_RAISES_EXCEPTIONS (t2), 1))
1083 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1084 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1085 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1089 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1090 here since this is the one place where
1091 COMPARE_REDECLARATION should be used. */
1092 val = comp_array_types (comptypes, t1, t2, orig_strict);
1095 case TEMPLATE_TYPE_PARM:
1096 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1097 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1100 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1101 TYPENAME_TYPE_FULLNAME (t2)) < 1)
1103 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1106 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1111 return attrval == 2 && val == 1 ? 2 : val;
1114 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1115 only in the same direction as the target type. */
1118 comp_cv_target_types (ttl, ttr, nptrs)
1124 if (!at_least_as_qualified_p (ttl, ttr)
1125 && !at_least_as_qualified_p (ttr, ttl))
1126 /* The qualifications are incomparable. */
1129 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1130 return more_qualified_p (ttr, ttl) ? -1 : 1;
1132 t = comp_target_types (ttl, ttr, nptrs);
1133 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1134 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1140 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1141 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1142 converted to TTL. Return -1 means that TTL can be converted to TTR but
1145 NPTRS is the number of pointers we can strip off and keep cool.
1146 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1147 but to not permit B** to convert to A**.
1149 This should go away. Callers should use can_convert or something
1150 similar instead. (jason 17 Apr 1997) */
1153 comp_target_types (ttl, ttr, nptrs)
1157 ttl = TYPE_MAIN_VARIANT (ttl);
1158 ttr = TYPE_MAIN_VARIANT (ttr);
1159 if (same_type_p (ttl, ttr))
1162 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1165 if ((TREE_CODE (ttr) == POINTER_TYPE
1166 || TREE_CODE (ttr) == REFERENCE_TYPE)
1167 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1168 of the type pointed to. This is necessary for reference init
1169 semantics. We won't get here from a previous call with nptrs == 1;
1170 for multi-level pointers we end up in comp_ptr_ttypes. */
1173 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1175 ttl = TREE_TYPE (ttl);
1176 ttr = TREE_TYPE (ttr);
1180 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1181 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1183 else if (TREE_CODE (ttl) == VOID_TYPE
1184 && TREE_CODE (ttr) != FUNCTION_TYPE
1185 && TREE_CODE (ttr) != METHOD_TYPE
1186 && TREE_CODE (ttr) != OFFSET_TYPE)
1188 else if (TREE_CODE (ttr) == VOID_TYPE
1189 && TREE_CODE (ttl) != FUNCTION_TYPE
1190 && TREE_CODE (ttl) != METHOD_TYPE
1191 && TREE_CODE (ttl) != OFFSET_TYPE)
1193 else if (TREE_CODE (ttl) == POINTER_TYPE
1194 || TREE_CODE (ttl) == ARRAY_TYPE)
1196 if (comp_ptr_ttypes (ttl, ttr))
1198 else if (comp_ptr_ttypes (ttr, ttl))
1204 /* Const and volatile mean something different for function types,
1205 so the usual checks are not appropriate. */
1206 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1207 return comp_target_types (ttl, ttr, nptrs - 1);
1209 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1212 if (TREE_CODE (ttr) == ARRAY_TYPE)
1213 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1214 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1221 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1226 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1235 argsl = TYPE_ARG_TYPES (ttl);
1236 argsr = TYPE_ARG_TYPES (ttr);
1238 /* Compare 'this' here, not in comp_target_parms. */
1239 if (TREE_CODE (ttr) == METHOD_TYPE)
1241 tree tl = TYPE_METHOD_BASETYPE (ttl);
1242 tree tr = TYPE_METHOD_BASETYPE (ttr);
1244 if (!same_or_base_type_p (tr, tl))
1246 if (same_or_base_type_p (tl, tr))
1252 argsl = TREE_CHAIN (argsl);
1253 argsr = TREE_CHAIN (argsr);
1256 switch (comp_target_parms (argsl, argsr))
1264 return saw_contra ? -1 : 1;
1267 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1271 /* Contravariance: we can assign a pointer to base member to a pointer
1272 to derived member. Note difference from simple pointer case, where
1273 we can pass a pointer to derived to a pointer to base. */
1274 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1275 TYPE_OFFSET_BASETYPE (ttl)))
1277 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1278 TYPE_OFFSET_BASETYPE (ttr)))
1288 ttl = TREE_TYPE (ttl);
1289 ttr = TREE_TYPE (ttr);
1291 if (TREE_CODE (ttl) == POINTER_TYPE
1292 || TREE_CODE (ttl) == ARRAY_TYPE)
1294 if (comp_ptr_ttypes (ttl, ttr))
1300 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1305 else if (IS_AGGR_TYPE (ttl))
1309 if (same_or_base_type_p (build_pointer_type (ttl),
1310 build_pointer_type (ttr)))
1312 if (same_or_base_type_p (build_pointer_type (ttr),
1313 build_pointer_type (ttl)))
1321 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1324 at_least_as_qualified_p (type1, type2)
1328 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1329 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1330 == CP_TYPE_QUALS (type2));
1333 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1336 more_qualified_p (type1, type2)
1340 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1341 && at_least_as_qualified_p (type1, type2));
1344 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1345 more cv-qualified that TYPE1, and 0 otherwise. */
1348 comp_cv_qualification (type1, type2)
1352 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1355 if (at_least_as_qualified_p (type1, type2))
1358 else if (at_least_as_qualified_p (type2, type1))
1364 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1365 subset of the cv-qualification signature of TYPE2, and the types
1366 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1369 comp_cv_qual_signature (type1, type2)
1373 if (comp_ptr_ttypes_real (type2, type1, -1))
1375 else if (comp_ptr_ttypes_real (type1, type2, -1))
1381 /* If two types share a common base type, return that basetype.
1382 If there is not a unique most-derived base type, this function
1383 returns ERROR_MARK_NODE. */
1386 common_base_type (tt1, tt2)
1389 tree best = NULL_TREE;
1392 /* If one is a baseclass of another, that's good enough. */
1393 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1395 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1398 /* Otherwise, try to find a unique baseclass of TT1
1399 that is shared by TT2, and follow that down. */
1400 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1402 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1403 tree trial = common_base_type (basetype, tt2);
1406 if (trial == error_mark_node)
1408 if (best == NULL_TREE)
1410 else if (best != trial)
1411 return error_mark_node;
1416 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1418 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1419 tree trial = common_base_type (tt1, basetype);
1422 if (trial == error_mark_node)
1424 if (best == NULL_TREE)
1426 else if (best != trial)
1427 return error_mark_node;
1433 /* Subroutines of `comptypes'. */
1435 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1436 equivalent in the sense that functions with those parameter types
1437 can have equivalent types. The two lists must be equivalent,
1440 C++: See comment above about TYPE1, TYPE2. */
1443 compparms (parms1, parms2)
1444 tree parms1, parms2;
1446 register tree t1 = parms1, t2 = parms2;
1448 /* An unspecified parmlist matches any specified parmlist
1449 whose argument types don't need default promotions. */
1453 if (t1 == 0 && t2 == 0)
1455 /* If one parmlist is shorter than the other,
1456 they fail to match. */
1457 if (t1 == 0 || t2 == 0)
1459 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1462 t1 = TREE_CHAIN (t1);
1463 t2 = TREE_CHAIN (t2);
1467 /* This really wants return whether or not parameter type lists
1468 would make their owning functions assignment compatible or not.
1470 The return value is like for comp_target_types.
1472 This should go away, possibly with the exception of the empty parmlist
1473 conversion; there are no conversions between function types in C++.
1474 (jason 17 Apr 1997) */
1477 comp_target_parms (parms1, parms2)
1478 tree parms1, parms2;
1480 register tree t1 = parms1, t2 = parms2;
1481 int warn_contravariance = 0;
1483 /* In C, an unspecified parmlist matches any specified parmlist
1484 whose argument types don't need default promotions. This is not
1485 true for C++, but let's do it anyway for unfixed headers. */
1487 if (t1 == 0 && t2 != 0)
1489 cp_pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1491 return self_promoting_args_p (t2);
1494 return self_promoting_args_p (t1);
1496 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1500 /* If one parmlist is shorter than the other,
1501 they fail to match, unless STRICT is <= 0. */
1502 if (t1 == 0 || t2 == 0)
1504 p1 = TREE_VALUE (t1);
1505 p2 = TREE_VALUE (t2);
1506 if (same_type_p (p1, p2))
1512 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1513 || (TREE_CODE (p1) == REFERENCE_TYPE
1514 && TREE_CODE (p2) == REFERENCE_TYPE))
1516 /* The following is wrong for contravariance,
1517 but many programs depend on it. */
1518 if (TREE_TYPE (p1) == void_type_node)
1520 if (TREE_TYPE (p2) == void_type_node)
1522 warn_contravariance = 1;
1525 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1526 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1530 /* Note backwards order due to contravariance. */
1531 if (comp_target_types (p2, p1, 1) <= 0)
1533 if (comp_target_types (p1, p2, 1) > 0)
1535 warn_contravariance = 1;
1541 return warn_contravariance ? -1 : 1;
1544 /* Compute the value of the `sizeof' operator. */
1550 enum tree_code code = TREE_CODE (type);
1553 if (processing_template_decl)
1554 return build_min (SIZEOF_EXPR, sizetype, type);
1556 if (code == FUNCTION_TYPE)
1558 if (pedantic || warn_pointer_arith)
1559 pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
1560 size = size_one_node;
1562 else if (code == METHOD_TYPE)
1564 if (pedantic || warn_pointer_arith)
1565 pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
1566 size = size_one_node;
1568 else if (code == VOID_TYPE)
1570 if (pedantic || warn_pointer_arith)
1571 pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
1572 size = size_one_node;
1574 else if (code == ERROR_MARK)
1575 size = size_one_node;
1578 /* ARM $5.3.2: ``When applied to a reference, the result is the
1579 size of the referenced object.'' */
1580 if (code == REFERENCE_TYPE)
1581 type = TREE_TYPE (type);
1583 if (code == OFFSET_TYPE)
1585 cp_error ("`sizeof' applied to non-static member");
1586 size = size_zero_node;
1588 else if (!COMPLETE_TYPE_P (complete_type (type)))
1590 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1591 size = size_zero_node;
1594 /* Convert in case a char is more than one unit. */
1595 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1596 size_int (TYPE_PRECISION (char_type_node)
1600 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1601 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1602 never happen. However, this node should really have type
1603 `size_t', which is just a typedef for an ordinary integer type. */
1604 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1605 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1615 if (processing_template_decl)
1616 return build_min (SIZEOF_EXPR, sizetype, e);
1618 if (TREE_CODE (e) == COMPONENT_REF
1619 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1620 error ("sizeof applied to a bit-field");
1621 if (is_overloaded_fn (e))
1623 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1624 return c_sizeof (char_type_node);
1626 else if (type_unknown_p (e))
1628 incomplete_type_error (e, TREE_TYPE (e));
1629 return c_sizeof (char_type_node);
1631 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1632 member unless you're in a non-static member of X. So hand off to
1633 resolve_offset_ref. [expr.prim] */
1634 else if (TREE_CODE (e) == OFFSET_REF)
1635 e = resolve_offset_ref (e);
1637 if (e == error_mark_node)
1640 return c_sizeof (TREE_TYPE (e));
1644 c_sizeof_nowarn (type)
1647 enum tree_code code = TREE_CODE (type);
1650 if (code == FUNCTION_TYPE
1651 || code == METHOD_TYPE
1652 || code == VOID_TYPE
1653 || code == ERROR_MARK)
1654 size = size_one_node;
1657 if (code == REFERENCE_TYPE)
1658 type = TREE_TYPE (type);
1660 if (!COMPLETE_TYPE_P (type))
1661 size = size_zero_node;
1663 /* Convert in case a char is more than one unit. */
1664 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1665 size_int (TYPE_PRECISION (char_type_node)
1669 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1670 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1671 never happen. However, this node should really have type
1672 `size_t', which is just a typedef for an ordinary integer type. */
1673 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1674 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1679 /* Implement the __alignof keyword: Return the minimum required
1680 alignment of TYPE, measured in bytes. */
1686 enum tree_code code = TREE_CODE (type);
1689 if (processing_template_decl)
1690 return build_min (ALIGNOF_EXPR, sizetype, type);
1692 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1693 t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1694 else if (code == VOID_TYPE || code == ERROR_MARK)
1698 /* Similar to sizeof, __alignof applies to the referant. */
1699 if (code == REFERENCE_TYPE)
1700 type = TREE_TYPE (type);
1702 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1705 return fold (build1 (NOP_EXPR, c_size_type_node, t));
1708 /* Perform the array-to-pointer and function-to-pointer conversions
1711 In addition, references are converted to lvalues and manifest
1712 constants are replaced by their values. */
1715 decay_conversion (exp)
1719 register enum tree_code code;
1721 if (TREE_CODE (exp) == OFFSET_REF)
1722 exp = resolve_offset_ref (exp);
1724 type = TREE_TYPE (exp);
1725 code = TREE_CODE (type);
1727 if (code == REFERENCE_TYPE)
1729 exp = convert_from_reference (exp);
1730 type = TREE_TYPE (exp);
1731 code = TREE_CODE (type);
1734 if (type == error_mark_node)
1735 return error_mark_node;
1737 /* Constants can be used directly unless they're not loadable. */
1738 if (TREE_CODE (exp) == CONST_DECL)
1739 exp = DECL_INITIAL (exp);
1740 /* Replace a nonvolatile const static variable with its value. We
1741 don't do this for arrays, though; we want the address of the
1742 first element of the array, not the address of the first element
1743 of its initializing constant. */
1744 else if (code != ARRAY_TYPE)
1746 exp = decl_constant_value (exp);
1747 type = TREE_TYPE (exp);
1750 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1751 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1753 if (code == VOID_TYPE)
1755 error ("void value not ignored as it ought to be");
1756 return error_mark_node;
1758 if (code == METHOD_TYPE)
1759 my_friendly_abort (990506);
1760 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1761 return build_unary_op (ADDR_EXPR, exp, 0);
1762 if (code == ARRAY_TYPE)
1767 if (TREE_CODE (exp) == INDIRECT_REF)
1769 /* Stripping away the INDIRECT_REF is not the right
1770 thing to do for references... */
1771 tree inner = TREE_OPERAND (exp, 0);
1772 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1774 inner = build1 (CONVERT_EXPR,
1775 build_pointer_type (TREE_TYPE
1776 (TREE_TYPE (inner))),
1778 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1780 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1783 if (TREE_CODE (exp) == COMPOUND_EXPR)
1785 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1786 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1787 TREE_OPERAND (exp, 0), op1);
1791 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1793 error ("invalid use of non-lvalue array");
1794 return error_mark_node;
1797 ptrtype = build_pointer_type (TREE_TYPE (type));
1799 if (TREE_CODE (exp) == VAR_DECL)
1801 /* ??? This is not really quite correct
1802 in that the type of the operand of ADDR_EXPR
1803 is not the target type of the type of the ADDR_EXPR itself.
1804 Question is, can this lossage be avoided? */
1805 adr = build1 (ADDR_EXPR, ptrtype, exp);
1806 if (mark_addressable (exp) == 0)
1807 return error_mark_node;
1808 TREE_CONSTANT (adr) = staticp (exp);
1809 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1812 /* This way is better for a COMPONENT_REF since it can
1813 simplify the offset for a component. */
1814 adr = build_unary_op (ADDR_EXPR, exp, 1);
1815 return cp_convert (ptrtype, adr);
1818 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1819 rvalues always have cv-unqualified types. */
1820 if (! CLASS_TYPE_P (type))
1821 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1827 default_conversion (exp)
1831 enum tree_code code;
1833 exp = decay_conversion (exp);
1835 type = TREE_TYPE (exp);
1836 code = TREE_CODE (type);
1838 if (INTEGRAL_CODE_P (code))
1840 tree t = type_promotes_to (type);
1842 return cp_convert (t, exp);
1848 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1852 inline_conversion (exp)
1855 if (TREE_CODE (exp) == FUNCTION_DECL)
1856 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1861 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1862 decay_conversion to one. */
1865 string_conv_p (totype, exp, warn)
1871 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1874 t = TREE_TYPE (totype);
1875 if (!same_type_p (t, char_type_node)
1876 && !same_type_p (t, wchar_type_node))
1879 if (TREE_CODE (exp) == STRING_CST)
1881 /* Make sure that we don't try to convert between char and wchar_t. */
1882 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1887 /* Is this a string constant which has decayed to 'const char *'? */
1888 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1889 if (!same_type_p (TREE_TYPE (exp), t))
1892 if (TREE_CODE (exp) != ADDR_EXPR
1893 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1897 /* This warning is not very useful, as it complains about printf. */
1898 if (warn && warn_write_strings)
1899 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1905 build_object_ref (datum, basetype, field)
1906 tree datum, basetype, field;
1909 if (datum == error_mark_node)
1910 return error_mark_node;
1912 dtype = TREE_TYPE (datum);
1913 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1914 dtype = TREE_TYPE (dtype);
1915 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1917 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1918 basetype, field, dtype);
1919 return error_mark_node;
1921 else if (is_aggr_type (basetype, 1))
1923 tree binfo = binfo_or_else (basetype, dtype);
1925 return build_x_component_ref (build_scoped_ref (datum, basetype),
1928 return error_mark_node;
1931 /* Like `build_component_ref, but uses an already found field, and converts
1932 from a reference. Must compute access for current_class_ref.
1936 build_component_ref_1 (datum, field, protect)
1940 return convert_from_reference
1941 (build_component_ref (datum, field, NULL_TREE, protect));
1944 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1945 can, for example, use as an lvalue. This code used to be in
1946 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1947 expressions, where we're dealing with aggregates. But now it's again only
1948 called from unary_complex_lvalue. The case (in particular) that led to
1949 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1953 rationalize_conditional_expr (code, t)
1954 enum tree_code code;
1957 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1958 the first operand is always the one to be used if both operands
1959 are equal, so we know what conditional expression this used to be. */
1960 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1963 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1964 ? LE_EXPR : GE_EXPR),
1965 TREE_OPERAND (t, 0),
1966 TREE_OPERAND (t, 1)),
1967 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1968 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1972 build_conditional_expr (TREE_OPERAND (t, 0),
1973 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1974 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1977 /* Given the TYPE of an anonymous union field inside T, return the
1978 FIELD_DECL for the field. If not found return NULL_TREE. Because
1979 anonymous unions can nest, we must also search all anonymous unions
1980 that are directly reachable. */
1983 lookup_anon_field (t, type)
1988 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1990 if (TREE_STATIC (field))
1992 if (TREE_CODE (field) != FIELD_DECL)
1995 /* If we find it directly, return the field. */
1996 if (DECL_NAME (field) == NULL_TREE
1997 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2002 /* Otherwise, it could be nested, search harder. */
2003 if (DECL_NAME (field) == NULL_TREE
2004 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2006 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2014 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2015 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2016 that we are interested in, or it can be a FIELD_DECL. */
2019 build_component_ref (datum, component, basetype_path, protect)
2020 tree datum, component, basetype_path;
2023 register tree basetype;
2024 register enum tree_code code;
2025 register tree field = NULL;
2030 if (processing_template_decl)
2031 return build_min_nt (COMPONENT_REF, datum, component);
2033 if (datum == error_mark_node
2034 || TREE_TYPE (datum) == error_mark_node)
2035 return error_mark_node;
2037 /* BASETYPE holds the type of the class containing the COMPONENT. */
2038 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2040 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2042 switch (TREE_CODE (datum))
2046 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2047 basetype_path, protect);
2048 return build (COMPOUND_EXPR, TREE_TYPE (value),
2049 TREE_OPERAND (datum, 0), value);
2052 return build_conditional_expr
2053 (TREE_OPERAND (datum, 0),
2054 build_component_ref (TREE_OPERAND (datum, 1), component,
2055 basetype_path, protect),
2056 build_component_ref (TREE_OPERAND (datum, 2), component,
2057 basetype_path, protect));
2060 cp_error ("invalid use of %D", datum);
2061 datum = error_mark_node;
2068 code = TREE_CODE (basetype);
2070 if (code == REFERENCE_TYPE)
2072 datum = convert_from_reference (datum);
2073 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2074 code = TREE_CODE (basetype);
2076 if (TREE_CODE (datum) == OFFSET_REF)
2078 datum = resolve_offset_ref (datum);
2079 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2080 code = TREE_CODE (basetype);
2083 /* First, see if there is a field or component with name COMPONENT. */
2084 if (TREE_CODE (component) == TREE_LIST)
2086 /* I could not trigger this code. MvL */
2087 my_friendly_abort (980326);
2089 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2090 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2092 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2095 if (! IS_AGGR_TYPE_CODE (code))
2097 if (code != ERROR_MARK)
2098 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2099 component, datum, basetype);
2100 return error_mark_node;
2103 if (!complete_type_or_else (basetype, datum))
2104 return error_mark_node;
2106 if (TREE_CODE (component) == BIT_NOT_EXPR)
2108 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2110 cp_error ("destructor specifier `%T::~%T' must have matching names",
2111 basetype, TREE_OPERAND (component, 0));
2112 return error_mark_node;
2114 if (! TYPE_HAS_DESTRUCTOR (basetype))
2116 cp_error ("type `%T' has no destructor", basetype);
2117 return error_mark_node;
2119 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2122 /* Look up component name in the structure type definition. */
2123 if (TYPE_VFIELD (basetype)
2124 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2125 /* Special-case this because if we use normal lookups in an ambiguous
2126 hierarchy, the compiler will abort (because vptr lookups are
2127 not supposed to be ambiguous. */
2128 field = TYPE_VFIELD (basetype);
2129 else if (TREE_CODE (component) == FIELD_DECL)
2131 else if (TREE_CODE (component) == TYPE_DECL)
2133 cp_error ("invalid use of type decl `%#D' as expression", component);
2134 return error_mark_node;
2136 else if (TREE_CODE (component) == TEMPLATE_DECL)
2138 cp_error ("invalid use of template `%#D' as expression", component);
2139 return error_mark_node;
2143 tree name = component;
2144 if (TREE_CODE (component) == VAR_DECL)
2145 name = DECL_NAME (component);
2146 if (TREE_CODE (component) == NAMESPACE_DECL)
2147 /* Source is in error, but produce a sensible diagnostic. */
2148 name = DECL_NAME (component);
2149 if (basetype_path == NULL_TREE)
2150 basetype_path = TYPE_BINFO (basetype);
2151 field = lookup_field (basetype_path, name,
2152 protect && !VFIELD_NAME_P (name), 0);
2153 if (field == error_mark_node)
2154 return error_mark_node;
2156 if (field == NULL_TREE)
2158 /* Not found as a data field, look for it as a method. If found,
2159 then if this is the only possible one, return it, else
2160 report ambiguity error. */
2161 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2162 if (fndecls == error_mark_node)
2163 return error_mark_node;
2166 /* If the function is unique and static, we can resolve it
2167 now. Otherwise, we have to wait and see what context it is
2168 used in; a component_ref involving a non-static member
2169 function can only be used in a call (expr.ref). */
2171 if (TREE_CHAIN (fndecls) == NULL_TREE
2172 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2174 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2176 tree fndecl = TREE_VALUE (fndecls);
2177 enforce_access (basetype_path, fndecl);
2183 /* A unique non-static member function. Other parts
2184 of the compiler expect something with
2185 unknown_type_node to be really overloaded, so
2187 TREE_VALUE (fndecls)
2188 = ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2192 ref = build (COMPONENT_REF, unknown_type_node,
2193 datum, TREE_VALUE (fndecls));
2197 cp_error ("`%#T' has no member named `%D'", basetype, name);
2198 return error_mark_node;
2200 else if (TREE_TYPE (field) == error_mark_node)
2201 return error_mark_node;
2203 if (TREE_CODE (field) != FIELD_DECL)
2205 if (TREE_CODE (field) == TYPE_DECL)
2206 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2207 else if (DECL_RTL (field) != 0)
2210 TREE_USED (field) = 1;
2212 /* Do evaluate the object when accessing a static member. */
2213 if (TREE_SIDE_EFFECTS (datum))
2214 field = build (COMPOUND_EXPR, TREE_TYPE (field), datum, field);
2220 /* See if we have to do any conversions so that we pick up the field from the
2222 if (DECL_FIELD_CONTEXT (field) != basetype)
2224 tree context = DECL_FIELD_CONTEXT (field);
2225 tree base = context;
2226 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2227 && ANON_AGGR_TYPE_P (base))
2228 base = TYPE_CONTEXT (base);
2230 /* Handle base classes here... */
2231 if (base != basetype && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
2233 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2234 if (integer_zerop (addr))
2236 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2237 return error_mark_node;
2239 if (VBASE_NAME_P (DECL_NAME (field)))
2241 /* It doesn't matter which vbase pointer we grab, just
2242 find one of them. */
2243 tree binfo = get_binfo (base,
2244 TREE_TYPE (TREE_TYPE (addr)), 0);
2245 addr = convert_pointer_to_real (binfo, addr);
2248 addr = convert_pointer_to (base, addr);
2249 datum = build_indirect_ref (addr, NULL);
2250 if (datum == error_mark_node)
2251 return error_mark_node;
2255 /* Handle things from anon unions here... */
2256 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2258 tree subfield = lookup_anon_field (basetype, context);
2259 tree subdatum = build_component_ref (datum, subfield,
2260 basetype_path, protect);
2261 return build_component_ref (subdatum, field, basetype_path, protect);
2265 /* Compute the type of the field, as described in [expr.ref]. */
2266 type_quals = TYPE_UNQUALIFIED;
2267 field_type = TREE_TYPE (field);
2268 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2269 /* The standard says that the type of the result should be the
2270 type referred to by the reference. But for now, at least, we
2271 do the conversion from reference type later. */
2275 type_quals = (CP_TYPE_QUALS (field_type)
2276 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2278 /* A field is const (volatile) if the enclosing object, or the
2279 field itself, is const (volatile). But, a mutable field is
2280 not const, even within a const object. */
2281 if (DECL_MUTABLE_P (field))
2282 type_quals &= ~TYPE_QUAL_CONST;
2283 field_type = cp_build_qualified_type (field_type, type_quals);
2286 ref = fold (build (COMPONENT_REF, field_type, datum, field));
2288 /* Mark the expression const or volatile, as appropriate. Even
2289 though we've dealt with the type above, we still have to mark the
2290 expression itself. */
2291 if (type_quals & TYPE_QUAL_CONST)
2292 TREE_READONLY (ref) = 1;
2293 else if (type_quals & TYPE_QUAL_VOLATILE)
2294 TREE_THIS_VOLATILE (ref) = 1;
2299 /* Variant of build_component_ref for use in expressions, which should
2300 never have REFERENCE_TYPE. */
2303 build_x_component_ref (datum, component, basetype_path, protect)
2304 tree datum, component, basetype_path;
2307 tree t = build_component_ref (datum, component, basetype_path, protect);
2309 if (! processing_template_decl)
2310 t = convert_from_reference (t);
2315 /* Given an expression PTR for a pointer, return an expression
2316 for the value pointed to.
2317 ERRORSTRING is the name of the operator to appear in error messages.
2319 This function may need to overload OPERATOR_FNNAME.
2320 Must also handle REFERENCE_TYPEs for C++. */
2323 build_x_indirect_ref (ptr, errorstring)
2325 const char *errorstring;
2329 if (processing_template_decl)
2330 return build_min_nt (INDIRECT_REF, ptr);
2332 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2336 return build_indirect_ref (ptr, errorstring);
2340 build_indirect_ref (ptr, errorstring)
2342 const char *errorstring;
2344 register tree pointer, type;
2346 if (ptr == error_mark_node)
2347 return error_mark_node;
2349 if (ptr == current_class_ptr)
2350 return current_class_ref;
2352 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2353 ? ptr : default_conversion (ptr));
2354 type = TREE_TYPE (pointer);
2356 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2360 If the type of the expression is "pointer to T," the type
2361 of the result is "T."
2363 We must use the canonical variant because certain parts of
2364 the back end, like fold, do pointer comparisons between
2366 tree t = canonical_type_variant (TREE_TYPE (type));
2368 if (VOID_TYPE_P (t))
2370 /* A pointer to incomplete type (other than cv void) can be
2371 dereferenced [expr.unary.op]/1 */
2372 cp_error ("`%T' is not a pointer-to-object type", type);
2373 return error_mark_node;
2375 else if (TREE_CODE (pointer) == ADDR_EXPR
2377 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2378 /* The POINTER was something like `&x'. We simplify `*&x' to
2380 return TREE_OPERAND (pointer, 0);
2383 tree ref = build1 (INDIRECT_REF, t, pointer);
2385 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2386 so that we get the proper error message if the result is used
2387 to assign to. Also, &* is supposed to be a no-op. */
2388 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2389 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2390 TREE_SIDE_EFFECTS (ref)
2391 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2396 /* `pointer' won't be an error_mark_node if we were given a
2397 pointer to member, so it's cool to check for this here. */
2398 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2399 error ("invalid use of `%s' on pointer to member", errorstring);
2400 else if (pointer != error_mark_node)
2403 error ("invalid type argument of `%s'", errorstring);
2405 error ("invalid type argument");
2407 return error_mark_node;
2410 /* This handles expressions of the form "a[i]", which denotes
2413 This is logically equivalent in C to *(a+i), but we may do it differently.
2414 If A is a variable or a member, we generate a primitive ARRAY_REF.
2415 This avoids forcing the array out of registers, and can work on
2416 arrays that are not lvalues (for example, members of structures returned
2419 If INDEX is of some user-defined type, it must be converted to
2420 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2421 will inherit the type of the array, which will be some pointer type. */
2424 build_array_ref (array, idx)
2429 error ("subscript missing in array reference");
2430 return error_mark_node;
2433 if (TREE_TYPE (array) == error_mark_node
2434 || TREE_TYPE (idx) == error_mark_node)
2435 return error_mark_node;
2437 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2439 switch (TREE_CODE (array))
2443 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2444 return build (COMPOUND_EXPR, TREE_TYPE (value),
2445 TREE_OPERAND (array, 0), value);
2449 return build_conditional_expr
2450 (TREE_OPERAND (array, 0),
2451 build_array_ref (TREE_OPERAND (array, 1), idx),
2452 build_array_ref (TREE_OPERAND (array, 2), idx));
2458 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2459 && TREE_CODE (array) != INDIRECT_REF)
2463 /* Subscripting with type char is likely to lose
2464 on a machine where chars are signed.
2465 So warn on any machine, but optionally.
2466 Don't warn for unsigned char since that type is safe.
2467 Don't warn for signed char because anyone who uses that
2468 must have done so deliberately. */
2469 if (warn_char_subscripts
2470 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2471 warning ("array subscript has type `char'");
2473 /* Apply default promotions *after* noticing character types. */
2474 idx = default_conversion (idx);
2476 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2478 error ("array subscript is not an integer");
2479 return error_mark_node;
2482 /* An array that is indexed by a non-constant
2483 cannot be stored in a register; we must be able to do
2484 address arithmetic on its address.
2485 Likewise an array of elements of variable size. */
2486 if (TREE_CODE (idx) != INTEGER_CST
2487 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2488 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2491 if (mark_addressable (array) == 0)
2492 return error_mark_node;
2495 /* An array that is indexed by a constant value which is not within
2496 the array bounds cannot be stored in a register either; because we
2497 would get a crash in store_bit_field/extract_bit_field when trying
2498 to access a non-existent part of the register. */
2499 if (TREE_CODE (idx) == INTEGER_CST
2500 && TYPE_VALUES (TREE_TYPE (array))
2501 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2503 if (mark_addressable (array) == 0)
2504 return error_mark_node;
2507 if (pedantic && !lvalue_p (array))
2508 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2510 /* Note in C++ it is valid to subscript a `register' array, since
2511 it is valid to take the address of something with that
2512 storage specification. */
2516 while (TREE_CODE (foo) == COMPONENT_REF)
2517 foo = TREE_OPERAND (foo, 0);
2518 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2519 warning ("subscripting array declared `register'");
2522 type = TREE_TYPE (TREE_TYPE (array));
2523 rval = build (ARRAY_REF, type, array, idx);
2524 /* Array ref is const/volatile if the array elements are
2525 or if the array is.. */
2526 TREE_READONLY (rval)
2527 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2528 TREE_SIDE_EFFECTS (rval)
2529 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2530 TREE_THIS_VOLATILE (rval)
2531 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2532 return require_complete_type (fold (rval));
2536 tree ar = default_conversion (array);
2537 tree ind = default_conversion (idx);
2539 /* Put the integer in IND to simplify error checking. */
2540 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2547 if (ar == error_mark_node)
2550 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2552 error ("subscripted value is neither array nor pointer");
2553 return error_mark_node;
2555 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2557 error ("array subscript is not an integer");
2558 return error_mark_node;
2561 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2566 /* Build a function call to function FUNCTION with parameters PARAMS.
2567 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2568 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2569 not include any object pointer that may be required. FUNCTION's
2570 data type may be a function type or a pointer-to-function.
2572 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2573 is the list of possible methods that FUNCTION could conceivably
2574 be. If the list of methods comes from a class, then it will be
2575 a list of lists (where each element is associated with the class
2576 that produced it), otherwise it will be a simple list (for
2577 functions overloaded in global scope).
2579 In the first case, TREE_VALUE (function) is the head of one of those
2580 lists, and TREE_PURPOSE is the name of the function.
2582 In the second case, TREE_PURPOSE (function) is the function's
2585 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2587 When calling a TEMPLATE_DECL, we don't require a complete return
2591 build_x_function_call (function, params, decl)
2592 tree function, params, decl;
2595 tree template_id = NULL_TREE;
2598 if (function == error_mark_node)
2599 return error_mark_node;
2601 if (processing_template_decl)
2602 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2604 /* Save explicit template arguments if found */
2605 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2607 template_id = function;
2608 function = TREE_OPERAND (function, 0);
2611 type = TREE_TYPE (function);
2613 if (TREE_CODE (type) == OFFSET_TYPE
2614 && TREE_TYPE (type) == unknown_type_node
2615 && TREE_CODE (function) == TREE_LIST
2616 && TREE_CHAIN (function) == NULL_TREE)
2618 /* Undo (Foo:bar)()... */
2619 type = TYPE_OFFSET_BASETYPE (type);
2620 function = TREE_VALUE (function);
2621 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2622 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2623 function = TREE_VALUE (function);
2624 if (TREE_CODE (function) == OVERLOAD)
2625 function = OVL_FUNCTION (function);
2626 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2627 function = DECL_NAME (function);
2628 return build_method_call (decl, function, params,
2629 TYPE_BINFO (type), LOOKUP_NORMAL);
2632 if (TREE_CODE (function) == OFFSET_REF
2633 && TREE_CODE (type) != METHOD_TYPE)
2634 function = resolve_offset_ref (function);
2636 if ((TREE_CODE (function) == FUNCTION_DECL
2637 && DECL_STATIC_FUNCTION_P (function))
2638 || (DECL_FUNCTION_TEMPLATE_P (function)
2639 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function))))
2640 return build_member_call (DECL_CONTEXT (function),
2642 ? template_id : DECL_NAME (function),
2645 is_method = ((TREE_CODE (function) == TREE_LIST
2646 && current_class_type != NULL_TREE
2647 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2649 || (TREE_CODE (function) == OVERLOAD
2650 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2651 || TREE_CODE (function) == IDENTIFIER_NODE
2652 || TREE_CODE (type) == METHOD_TYPE
2653 || TYPE_PTRMEMFUNC_P (type));
2655 /* A friend template. Make it look like a toplevel declaration. */
2656 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2657 function = ovl_cons (function, NULL_TREE);
2659 /* Handle methods, friends, and overloaded functions, respectively. */
2662 tree basetype = NULL_TREE;
2664 if (TREE_CODE (function) == OVERLOAD)
2665 function = OVL_CURRENT (function);
2667 if (TREE_CODE (function) == FUNCTION_DECL
2668 || DECL_FUNCTION_TEMPLATE_P (function))
2670 basetype = DECL_CONTEXT (function);
2672 if (DECL_NAME (function))
2673 function = DECL_NAME (function);
2675 function = TYPE_IDENTIFIER (DECL_CONTEXT (function));
2677 else if (TREE_CODE (function) == TREE_LIST)
2679 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2680 == FUNCTION_DECL, 312);
2681 basetype = DECL_CONTEXT (TREE_VALUE (function));
2682 function = TREE_PURPOSE (function);
2684 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2686 if (TREE_CODE (function) == OFFSET_REF)
2688 if (TREE_OPERAND (function, 0))
2689 decl = TREE_OPERAND (function, 0);
2691 /* Call via a pointer to member function. */
2692 if (decl == NULL_TREE)
2694 error ("pointer to member function called, but not in class scope");
2695 return error_mark_node;
2697 /* What other type of POINTER_TYPE could this be? */
2698 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2699 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2700 && TREE_CODE (function) != OFFSET_REF)
2701 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2706 /* this is an abbreviated method call.
2707 must go through here in case it is a virtual function.
2708 @@ Perhaps this could be optimized. */
2710 if (basetype && (! current_class_type
2711 || ! DERIVED_FROM_P (basetype, current_class_type)))
2712 return build_member_call (basetype, function, params);
2714 if (decl == NULL_TREE)
2716 if (current_class_type == NULL_TREE)
2718 cp_error ("object missing in call to method `%D'", function);
2719 return error_mark_node;
2721 /* Yow: call from a static member function. */
2722 decl = build_dummy_object (current_class_type);
2725 /* Put back explicit template arguments, if any. */
2727 function = template_id;
2728 return build_method_call (decl, function, params,
2729 NULL_TREE, LOOKUP_NORMAL);
2731 else if (TREE_CODE (function) == COMPONENT_REF
2732 && type == unknown_type_node)
2734 /* Undo what we did in build_component_ref. */
2735 decl = TREE_OPERAND (function, 0);
2736 function = TREE_OPERAND (function, 1);
2737 function = DECL_NAME (OVL_CURRENT (function));
2741 TREE_OPERAND (template_id, 0) = function;
2742 function = template_id;
2745 return build_method_call (decl, function, params,
2746 NULL_TREE, LOOKUP_NORMAL);
2748 else if (really_overloaded_fn (function))
2750 if (OVL_FUNCTION (function) == NULL_TREE)
2752 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2753 TREE_PURPOSE (function));
2754 return error_mark_node;
2758 /* Put back explicit template arguments, if any. */
2760 function = template_id;
2761 return build_new_function_call (function, params);
2765 /* Remove a potential OVERLOAD around it */
2766 function = OVL_CURRENT (function);
2769 if (TREE_CODE (function) == OFFSET_REF)
2771 /* If the component is a data element (or a virtual function), we play
2772 games here to make things work. */
2775 if (TREE_OPERAND (function, 0))
2776 decl = TREE_OPERAND (function, 0);
2778 decl = current_class_ref;
2780 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2782 /* Sigh. OFFSET_REFs are being used for too many things.
2783 They're being used both for -> and ->*, and we want to resolve
2784 the -> cases here, but leave the ->*. We could use
2785 resolve_offset_ref for those, too, but it would call
2786 get_member_function_from_ptrfunc and decl_addr wouldn't get
2787 updated properly. Nasty. */
2788 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2789 function = resolve_offset_ref (function);
2791 function = TREE_OPERAND (function, 1);
2793 function = get_member_function_from_ptrfunc (&decl_addr, function);
2794 params = tree_cons (NULL_TREE, decl_addr, params);
2795 return build_function_call (function, params);
2798 type = TREE_TYPE (function);
2799 if (type != error_mark_node)
2801 if (TREE_CODE (type) == REFERENCE_TYPE)
2802 type = TREE_TYPE (type);
2804 if (IS_AGGR_TYPE (type))
2805 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2810 tree fntype = TREE_TYPE (function);
2811 tree ctypeptr = NULL_TREE;
2813 /* Explicitly named method? */
2814 if (TREE_CODE (function) == FUNCTION_DECL)
2815 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2816 /* Expression with ptr-to-method type? It could either be a plain
2817 usage, or it might be a case where the ptr-to-method is being
2818 passed in as an argument. */
2819 else if (TYPE_PTRMEMFUNC_P (fntype))
2821 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2822 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2823 ctypeptr = build_pointer_type (rec);
2825 /* Unexpected node type? */
2827 my_friendly_abort (116);
2828 if (decl == NULL_TREE)
2830 if (current_function_decl
2831 && DECL_STATIC_FUNCTION_P (current_function_decl))
2832 error ("invalid call to member function needing `this' in static member function scope");
2834 error ("pointer to member function called, but not in class scope");
2835 return error_mark_node;
2837 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2838 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2840 decl = build_unary_op (ADDR_EXPR, decl, 0);
2841 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2844 decl = build_c_cast (ctypeptr, decl);
2845 params = tree_cons (NULL_TREE, decl, params);
2848 return build_function_call (function, params);
2851 /* Resolve a pointer to member function. INSTANCE is the object
2852 instance to use, if the member points to a virtual member. */
2855 get_member_function_from_ptrfunc (instance_ptrptr, function)
2856 tree *instance_ptrptr;
2859 if (TREE_CODE (function) == OFFSET_REF)
2861 function = TREE_OPERAND (function, 1);
2864 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2866 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2867 tree instance, basetype;
2869 tree instance_ptr = *instance_ptrptr;
2871 if (instance_ptr == error_mark_node
2872 && TREE_CODE (function) == PTRMEM_CST)
2874 /* Extracting the function address from a pmf is only
2875 allowed with -Wno-pmf-conversions. It only works for
2877 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2878 e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
2882 if (TREE_SIDE_EFFECTS (instance_ptr))
2883 instance_ptr = save_expr (instance_ptr);
2885 if (TREE_SIDE_EFFECTS (function))
2886 function = save_expr (function);
2888 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2889 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2891 /* Convert down to the right base, before using the instance. */
2892 instance = convert_pointer_to_real (basetype, instance_ptr);
2893 if (instance == error_mark_node && instance_ptr != error_mark_node)
2896 e3 = PFN_FROM_PTRMEMFUNC (function);
2898 vtbl = convert_pointer_to (ptr_type_node, instance);
2899 delta = cp_convert (ptrdiff_type_node,
2900 build_component_ref (function, delta_identifier,
2903 /* This used to avoid checking for virtual functions if basetype
2904 has no virtual functions, according to an earlier ANSI draft.
2905 With the final ISO C++ rules, such an optimization is
2906 incorrect: A pointer to a derived member can be static_cast
2907 to pointer-to-base-member, as long as the dynamic object
2908 later has the right member. */
2910 /* Promoting idx before saving it improves performance on RISC
2911 targets. Without promoting, the first compare used
2912 load-with-sign-extend, while the second used normal load then
2913 shift to sign-extend. An optimizer flaw, perhaps, but it's
2914 easier to make this change. */
2915 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2917 case ptrmemfunc_vbit_in_pfn:
2918 idx = cp_build_binary_op (TRUNC_DIV_EXPR,
2919 build1 (NOP_EXPR, vtable_index_type, e3),
2920 TYPE_SIZE_UNIT (vtable_entry_type));
2921 e1 = cp_build_binary_op (BIT_AND_EXPR,
2922 build1 (NOP_EXPR, vtable_index_type, e3),
2926 case ptrmemfunc_vbit_in_delta:
2927 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2928 e1 = cp_build_binary_op (BIT_AND_EXPR,
2929 delta, integer_one_node);
2930 delta = cp_build_binary_op (RSHIFT_EXPR,
2931 build1 (NOP_EXPR, vtable_index_type,
2940 delta = cp_convert (ptrdiff_type_node,
2941 build_component_ref (function, delta_identifier,
2943 /* DELTA2 is the amount by which to adjust the `this' pointer
2944 to find the vtbl. */
2948 build_pointer_type (build_pointer_type (vtable_entry_type)),
2949 vtbl, cp_convert (ptrdiff_type_node, delta2));
2950 vtbl = build_indirect_ref (vtbl, NULL);
2951 aref = build_array_ref (vtbl, idx);
2953 if (! flag_vtable_thunks)
2955 aref = save_expr (aref);
2957 delta = cp_build_binary_op
2959 build_conditional_expr (e1,
2960 build_component_ref (aref,
2967 if (flag_vtable_thunks)
2970 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2971 TREE_TYPE (e2) = TREE_TYPE (e3);
2972 e1 = build_conditional_expr (e1, e2, e3);
2974 /* Make sure this doesn't get evaluated first inside one of the
2975 branches of the COND_EXPR. */
2976 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2977 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2980 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2981 instance_ptr, delta);
2983 if (instance_ptr == error_mark_node
2984 && TREE_CODE (e1) != ADDR_EXPR
2985 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2986 cp_error ("object missing in `%E'", function);
2994 build_function_call_real (function, params, require_complete, flags)
2995 tree function, params;
2996 int require_complete, flags;
2998 register tree fntype, fndecl;
2999 register tree value_type;
3000 register tree coerced_params;
3002 tree name = NULL_TREE, assembler_name = NULL_TREE;
3005 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3006 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3007 if (TREE_CODE (function) == NOP_EXPR
3008 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3009 function = TREE_OPERAND (function, 0);
3011 if (TREE_CODE (function) == FUNCTION_DECL)
3013 name = DECL_NAME (function);
3014 assembler_name = DECL_ASSEMBLER_NAME (function);
3016 GNU_xref_call (current_function_decl,
3017 IDENTIFIER_POINTER (name ? name
3018 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
3020 mark_used (function);
3023 /* Convert anything with function type to a pointer-to-function. */
3024 if (pedantic && DECL_MAIN_P (function))
3025 pedwarn ("ISO C++ forbids calling `::main' from within program");
3027 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
3028 (because calling an inline function does not mean the function
3029 needs to be separately compiled). */
3031 if (DECL_INLINE (function))
3032 function = inline_conversion (function);
3034 function = build_addr_func (function);
3040 function = build_addr_func (function);
3043 if (function == error_mark_node)
3044 return error_mark_node;
3046 fntype = TREE_TYPE (function);
3048 if (TYPE_PTRMEMFUNC_P (fntype))
3050 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3052 return error_mark_node;
3055 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3056 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3058 if (!((TREE_CODE (fntype) == POINTER_TYPE
3059 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3061 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3063 cp_error ("`%E' cannot be used as a function", function);
3064 return error_mark_node;
3067 /* fntype now gets the type of function pointed to. */
3068 fntype = TREE_TYPE (fntype);
3070 /* Convert the parameters to the types declared in the
3071 function prototype, or apply default promotions. */
3073 if (flags & LOOKUP_COMPLAIN)
3074 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3075 params, fndecl, LOOKUP_NORMAL);
3077 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3080 if (coerced_params == error_mark_node)
3082 if (flags & LOOKUP_SPECULATIVELY)
3085 return error_mark_node;
3088 /* Check for errors in format strings. */
3090 if (warn_format && (name || assembler_name))
3091 check_function_format (NULL, name, assembler_name, coerced_params);
3093 /* Recognize certain built-in functions so we can make tree-codes
3094 other than CALL_EXPR. We do this when it enables fold-const.c
3095 to do something useful. */
3097 if (TREE_CODE (function) == ADDR_EXPR
3098 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3099 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3101 result = expand_tree_builtin (TREE_OPERAND (function, 0),
3102 params, coerced_params);
3107 /* Some built-in function calls will be evaluated at
3108 compile-time in fold (). */
3109 result = fold (build_call (function, coerced_params));
3110 value_type = TREE_TYPE (result);
3112 if (require_complete)
3114 if (TREE_CODE (value_type) == VOID_TYPE)
3116 result = require_complete_type (result);
3118 if (IS_AGGR_TYPE (value_type))
3119 result = build_cplus_new (value_type, result);
3120 return convert_from_reference (result);
3124 build_function_call (function, params)
3125 tree function, params;
3127 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3130 /* Convert the actual parameter expressions in the list VALUES
3131 to the types in the list TYPELIST.
3132 If parmdecls is exhausted, or when an element has NULL as its type,
3133 perform the default conversions.
3135 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3137 This is also where warnings about wrong number of args are generated.
3139 Return a list of expressions for the parameters as converted.
3141 Both VALUES and the returned value are chains of TREE_LIST nodes
3142 with the elements of the list in the TREE_VALUE slots of those nodes.
3144 In C++, unspecified trailing parameters can be filled in with their
3145 default arguments, if such were specified. Do so here. */
3148 convert_arguments (typelist, values, fndecl, flags)
3149 tree typelist, values, fndecl;
3152 register tree typetail, valtail;
3153 register tree result = NULL_TREE;
3154 const char *called_thing = 0;
3157 /* Argument passing is always copy-initialization. */
3158 flags |= LOOKUP_ONLYCONVERTING;
3162 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3164 if (DECL_NAME (fndecl) == NULL_TREE
3165 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3166 called_thing = "constructor";
3168 called_thing = "member function";
3171 called_thing = "function";
3174 for (valtail = values, typetail = typelist;
3176 valtail = TREE_CHAIN (valtail), i++)
3178 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3179 register tree val = TREE_VALUE (valtail);
3181 if (val == error_mark_node)
3182 return error_mark_node;
3184 if (type == void_type_node)
3188 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3190 error ("at this point in file");
3193 error ("too many arguments to function");
3194 /* In case anybody wants to know if this argument
3197 TREE_TYPE (tree_last (result)) = error_mark_node;
3201 if (TREE_CODE (val) == OFFSET_REF)
3202 val = resolve_offset_ref (val);
3204 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3205 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3206 if (TREE_CODE (val) == NOP_EXPR
3207 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3208 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3209 val = TREE_OPERAND (val, 0);
3211 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3213 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3214 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3215 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3216 val = default_conversion (val);
3219 if (val == error_mark_node)
3220 return error_mark_node;
3224 /* Formal parm type is specified by a function prototype. */
3227 if (!COMPLETE_TYPE_P (complete_type (type)))
3229 error ("parameter type of called function is incomplete");
3234 parmval = convert_for_initialization
3235 (NULL_TREE, type, val, flags,
3236 "argument passing", fndecl, i);
3237 if (PROMOTE_PROTOTYPES
3238 && INTEGRAL_TYPE_P (type)
3239 && (TYPE_PRECISION (type)
3240 < TYPE_PRECISION (integer_type_node)))
3241 parmval = default_conversion (parmval);
3244 if (parmval == error_mark_node)
3245 return error_mark_node;
3247 result = tree_cons (NULL_TREE, parmval, result);
3251 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3252 val = convert_from_reference (val);
3254 if (fndecl && DECL_BUILT_IN (fndecl)
3255 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3256 /* Don't do ellipsis conversion for __built_in_constant_p
3257 as this will result in spurious warnings for non-POD
3259 val = require_complete_type (val);
3261 val = convert_arg_to_ellipsis (val);
3263 result = tree_cons (NULL_TREE, val, result);
3267 typetail = TREE_CHAIN (typetail);
3270 if (typetail != 0 && typetail != void_list_node)
3272 /* See if there are default arguments that can be used */
3273 if (TREE_PURPOSE (typetail))
3275 for (; typetail != void_list_node; ++i)
3278 = convert_default_arg (TREE_VALUE (typetail),
3279 TREE_PURPOSE (typetail),
3282 if (parmval == error_mark_node)
3283 return error_mark_node;
3285 result = tree_cons (0, parmval, result);
3286 typetail = TREE_CHAIN (typetail);
3287 /* ends with `...'. */
3288 if (typetail == NULL_TREE)
3296 cp_error_at ("too few arguments to %s `%+#D'",
3297 called_thing, fndecl);
3298 error ("at this point in file");
3301 error ("too few arguments to function");
3302 return error_mark_list;
3306 return nreverse (result);
3309 /* Build a binary-operation expression, after performing default
3310 conversions on the operands. CODE is the kind of expression to build. */
3313 build_x_binary_op (code, arg1, arg2)
3314 enum tree_code code;
3317 if (processing_template_decl)
3318 return build_min_nt (code, arg1, arg2);
3320 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3323 /* Build a binary-operation expression without default conversions.
3324 CODE is the kind of expression to build.
3325 This function differs from `build' in several ways:
3326 the data type of the result is computed and recorded in it,
3327 warnings are generated if arg data types are invalid,
3328 special handling for addition and subtraction of pointers is known,
3329 and some optimization is done (operations on narrow ints
3330 are done in the narrower type when that gives the same result).
3331 Constant folding is also done before the result is returned.
3333 Note that the operands will never have enumeral types
3334 because either they have just had the default conversions performed
3335 or they have both just been converted to some other type in which
3336 the arithmetic is to be done.
3338 C++: must do special pointer arithmetic when implementing
3339 multiple inheritance, and deal with pointer to member functions. */
3342 build_binary_op (code, orig_op0, orig_op1, convert_p)
3343 enum tree_code code;
3344 tree orig_op0, orig_op1;
3345 int convert_p ATTRIBUTE_UNUSED;
3348 register enum tree_code code0, code1;
3351 /* Expression code to give to the expression when it is built.
3352 Normally this is CODE, which is what the caller asked for,
3353 but in some special cases we change it. */
3354 register enum tree_code resultcode = code;
3356 /* Data type in which the computation is to be performed.
3357 In the simplest cases this is the common type of the arguments. */
3358 register tree result_type = NULL;
3360 /* Nonzero means operands have already been type-converted
3361 in whatever way is necessary.
3362 Zero means they need to be converted to RESULT_TYPE. */
3365 /* Nonzero means create the expression with this type, rather than
3367 tree build_type = 0;
3369 /* Nonzero means after finally constructing the expression
3370 convert it to this type. */
3371 tree final_type = 0;
3373 /* Nonzero if this is an operation like MIN or MAX which can
3374 safely be computed in short if both args are promoted shorts.
3375 Also implies COMMON.
3376 -1 indicates a bitwise operation; this makes a difference
3377 in the exact conditions for when it is safe to do the operation
3378 in a narrower mode. */
3381 /* Nonzero if this is a comparison operation;
3382 if both args are promoted shorts, compare the original shorts.
3383 Also implies COMMON. */
3384 int short_compare = 0;
3386 /* Nonzero if this is a right-shift operation, which can be computed on the
3387 original short and then promoted if the operand is a promoted short. */
3388 int short_shift = 0;
3390 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3393 /* Apply default conversions. */
3394 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3395 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3396 || code == TRUTH_XOR_EXPR)
3398 op0 = decay_conversion (orig_op0);
3399 op1 = decay_conversion (orig_op1);
3403 op0 = default_conversion (orig_op0);
3404 op1 = default_conversion (orig_op1);
3407 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3408 STRIP_TYPE_NOPS (op0);
3409 STRIP_TYPE_NOPS (op1);
3411 /* DTRT if one side is an overloaded function, but complain about it. */
3412 if (type_unknown_p (op0))
3414 tree t = instantiate_type (TREE_TYPE (op1), op0, itf_none);
3415 if (t != error_mark_node)
3417 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3422 if (type_unknown_p (op1))
3424 tree t = instantiate_type (TREE_TYPE (op0), op1, itf_none);
3425 if (t != error_mark_node)
3427 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3433 type0 = TREE_TYPE (op0);
3434 type1 = TREE_TYPE (op1);
3436 /* The expression codes of the data types of the arguments tell us
3437 whether the arguments are integers, floating, pointers, etc. */
3438 code0 = TREE_CODE (type0);
3439 code1 = TREE_CODE (type1);
3441 /* If an error was already reported for one of the arguments,
3442 avoid reporting another error. */
3444 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3445 return error_mark_node;
3450 /* Handle the pointer + int case. */
3451 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3452 return pointer_int_sum (PLUS_EXPR, op0, op1);
3453 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3454 return pointer_int_sum (PLUS_EXPR, op1, op0);
3460 /* Subtraction of two similar pointers.
3461 We must subtract them as integers, then divide by object size. */
3462 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3463 && comp_target_types (type0, type1, 1))
3464 return pointer_diff (op0, op1, common_type (type0, type1));
3465 /* Handle pointer minus int. Just like pointer plus int. */
3466 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3467 return pointer_int_sum (MINUS_EXPR, op0, op1);
3476 case TRUNC_DIV_EXPR:
3478 case FLOOR_DIV_EXPR:
3479 case ROUND_DIV_EXPR:
3480 case EXACT_DIV_EXPR:
3481 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3482 || code0 == COMPLEX_TYPE)
3483 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3484 || code1 == COMPLEX_TYPE))
3486 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3487 cp_warning ("division by zero in `%E / 0'", op0);
3488 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3489 cp_warning ("division by zero in `%E / 0.'", op0);
3491 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3492 resultcode = RDIV_EXPR;
3494 /* When dividing two signed integers, we have to promote to int.
3495 unless we divide by a constant != -1. Note that default
3496 conversion will have been performed on the operands at this
3497 point, so we have to dig out the original type to find out if
3499 shorten = ((TREE_CODE (op0) == NOP_EXPR
3500 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3501 || (TREE_CODE (op1) == INTEGER_CST
3502 && ! integer_all_onesp (op1)));
3509 case BIT_ANDTC_EXPR:
3512 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3514 /* If one operand is a constant, and the other is a short type
3515 that has been converted to an int,
3516 really do the work in the short type and then convert the
3517 result to int. If we are lucky, the constant will be 0 or 1
3518 in the short type, making the entire operation go away. */
3519 if (TREE_CODE (op0) == INTEGER_CST
3520 && TREE_CODE (op1) == NOP_EXPR
3521 && (TYPE_PRECISION (type1)
3522 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3523 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3525 final_type = result_type;
3526 op1 = TREE_OPERAND (op1, 0);
3527 result_type = TREE_TYPE (op1);
3529 if (TREE_CODE (op1) == INTEGER_CST
3530 && TREE_CODE (op0) == NOP_EXPR
3531 && (TYPE_PRECISION (type0)
3532 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3533 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3535 final_type = result_type;
3536 op0 = TREE_OPERAND (op0, 0);
3537 result_type = TREE_TYPE (op0);
3541 case TRUNC_MOD_EXPR:
3542 case FLOOR_MOD_EXPR:
3543 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3544 cp_warning ("division by zero in `%E %% 0'", op0);
3545 else if (code1 == REAL_TYPE && real_zerop (op1))
3546 cp_warning ("division by zero in `%E %% 0.'", op0);
3548 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3550 /* Although it would be tempting to shorten always here, that loses
3551 on some targets, since the modulo instruction is undefined if the
3552 quotient can't be represented in the computation mode. We shorten
3553 only if unsigned or if dividing by something we know != -1. */
3554 shorten = ((TREE_CODE (op0) == NOP_EXPR
3555 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3556 || (TREE_CODE (op1) == INTEGER_CST
3557 && ! integer_all_onesp (op1)));
3562 case TRUTH_ANDIF_EXPR:
3563 case TRUTH_ORIF_EXPR:
3564 case TRUTH_AND_EXPR:
3566 result_type = boolean_type_node;
3569 /* Shift operations: result has same type as first operand;
3570 always convert second operand to int.
3571 Also set SHORT_SHIFT if shifting rightward. */
3574 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3576 result_type = type0;
3577 if (TREE_CODE (op1) == INTEGER_CST)
3579 if (tree_int_cst_lt (op1, integer_zero_node))
3580 warning ("right shift count is negative");
3583 if (! integer_zerop (op1))
3585 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3586 warning ("right shift count >= width of type");
3589 /* Convert the shift-count to an integer, regardless of
3590 size of value being shifted. */
3591 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3592 op1 = cp_convert (integer_type_node, op1);
3593 /* Avoid converting op1 to result_type later. */
3599 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3601 result_type = type0;
3602 if (TREE_CODE (op1) == INTEGER_CST)
3604 if (tree_int_cst_lt (op1, integer_zero_node))
3605 warning ("left shift count is negative");
3606 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3607 warning ("left shift count >= width of type");
3609 /* Convert the shift-count to an integer, regardless of
3610 size of value being shifted. */
3611 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3612 op1 = cp_convert (integer_type_node, op1);
3613 /* Avoid converting op1 to result_type later. */
3620 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3622 result_type = type0;
3623 if (TREE_CODE (op1) == INTEGER_CST)
3625 if (tree_int_cst_lt (op1, integer_zero_node))
3626 warning ("%s rotate count is negative",
3627 (code == LROTATE_EXPR) ? "left" : "right");
3628 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3629 warning ("%s rotate count >= width of type",
3630 (code == LROTATE_EXPR) ? "left" : "right");
3632 /* Convert the shift-count to an integer, regardless of
3633 size of value being shifted. */
3634 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3635 op1 = cp_convert (integer_type_node, op1);
3641 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3642 warning ("comparing floating point with == or != is unsafe");
3644 build_type = boolean_type_node;
3645 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3646 || code0 == COMPLEX_TYPE)
3647 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3648 || code1 == COMPLEX_TYPE))
3650 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3651 result_type = composite_pointer_type (type0, type1, op0, op1,
3653 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3654 result_type = type0;
3655 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3656 result_type = type1;
3657 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3659 result_type = type0;
3660 error ("ISO C++ forbids comparison between pointer and integer");
3662 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3664 result_type = type1;
3665 error ("ISO C++ forbids comparison between pointer and integer");
3667 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3669 op0 = build_component_ref (op0, pfn_identifier, NULL_TREE, 0);
3670 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3671 result_type = TREE_TYPE (op0);
3673 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3674 return cp_build_binary_op (code, op1, op0);
3675 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3676 && same_type_p (type0, type1))
3678 /* E will be the final comparison. */
3680 /* E1 and E2 are for scratch. */
3688 if (TREE_SIDE_EFFECTS (op0))
3689 op0 = save_expr (op0);
3690 if (TREE_SIDE_EFFECTS (op1))
3691 op1 = save_expr (op1);
3696 && (!op0.pfn || op0.delta == op1.delta))
3698 The reason for the `!op0.pfn' bit is that a NULL
3699 pointer-to-member is any member with a zero PFN; the
3700 DELTA field is unspecified. */
3701 pfn0 = pfn_from_ptrmemfunc (op0);
3702 pfn1 = pfn_from_ptrmemfunc (op1);
3703 delta0 = build_component_ref (op0, delta_identifier,
3705 delta1 = build_component_ref (op1, delta_identifier,
3707 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3708 e2 = cp_build_binary_op (EQ_EXPR,
3710 cp_convert (TREE_TYPE (pfn0),
3711 integer_zero_node));
3712 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3713 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3714 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3715 if (code == EQ_EXPR)
3717 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3719 else if ((TYPE_PTRMEMFUNC_P (type0)
3720 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3721 || (TYPE_PTRMEMFUNC_P (type1)
3722 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3723 my_friendly_abort (20000221);
3728 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3729 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3731 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3732 result_type = composite_pointer_type (type0, type1, op0, op1,
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)
3745 result_type = composite_pointer_type (type0, type1, op0, op1,
3747 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3748 && integer_zerop (op1))
3749 result_type = type0;
3750 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3751 && integer_zerop (op0))
3752 result_type = type1;
3753 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3755 result_type = type0;
3756 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3758 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3760 result_type = type1;
3761 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3765 case UNORDERED_EXPR:
3772 build_type = integer_type_node;
3773 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3775 error ("unordered comparison on non-floating point argument");
3776 return error_mark_node;
3785 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3787 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3789 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3791 if (shorten || common || short_compare)
3792 result_type = common_type (type0, type1);
3794 /* For certain operations (which identify themselves by shorten != 0)
3795 if both args were extended from the same smaller type,
3796 do the arithmetic in that type and then extend.
3798 shorten !=0 and !=1 indicates a bitwise operation.
3799 For them, this optimization is safe only if
3800 both args are zero-extended or both are sign-extended.
3801 Otherwise, we might change the result.
3802 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3803 but calculated in (unsigned short) it would be (unsigned short)-1. */
3805 if (shorten && none_complex)
3807 int unsigned0, unsigned1;
3808 tree arg0 = get_narrower (op0, &unsigned0);
3809 tree arg1 = get_narrower (op1, &unsigned1);
3810 /* UNS is 1 if the operation to be done is an unsigned one. */
3811 int uns = TREE_UNSIGNED (result_type);
3814 final_type = result_type;
3816 /* Handle the case that OP0 does not *contain* a conversion
3817 but it *requires* conversion to FINAL_TYPE. */
3819 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3820 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3821 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3822 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3824 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3826 /* For bitwise operations, signedness of nominal type
3827 does not matter. Consider only how operands were extended. */
3831 /* Note that in all three cases below we refrain from optimizing
3832 an unsigned operation on sign-extended args.
3833 That would not be valid. */
3835 /* Both args variable: if both extended in same way
3836 from same width, do it in that width.
3837 Do it unsigned if args were zero-extended. */
3838 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3839 < TYPE_PRECISION (result_type))
3840 && (TYPE_PRECISION (TREE_TYPE (arg1))
3841 == TYPE_PRECISION (TREE_TYPE (arg0)))
3842 && unsigned0 == unsigned1
3843 && (unsigned0 || !uns))
3845 = signed_or_unsigned_type (unsigned0,
3846 common_type (TREE_TYPE (arg0),
3848 else if (TREE_CODE (arg0) == INTEGER_CST
3849 && (unsigned1 || !uns)
3850 && (TYPE_PRECISION (TREE_TYPE (arg1))
3851 < TYPE_PRECISION (result_type))
3852 && (type = signed_or_unsigned_type (unsigned1,
3854 int_fits_type_p (arg0, type)))
3856 else if (TREE_CODE (arg1) == INTEGER_CST
3857 && (unsigned0 || !uns)
3858 && (TYPE_PRECISION (TREE_TYPE (arg0))
3859 < TYPE_PRECISION (result_type))
3860 && (type = signed_or_unsigned_type (unsigned0,
3862 int_fits_type_p (arg1, type)))
3866 /* Shifts can be shortened if shifting right. */
3871 tree arg0 = get_narrower (op0, &unsigned_arg);
3873 final_type = result_type;
3875 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3876 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3878 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3879 /* We can shorten only if the shift count is less than the
3880 number of bits in the smaller type size. */
3881 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3882 /* If arg is sign-extended and then unsigned-shifted,
3883 we can simulate this with a signed shift in arg's type
3884 only if the extended result is at least twice as wide
3885 as the arg. Otherwise, the shift could use up all the
3886 ones made by sign-extension and bring in zeros.
3887 We can't optimize that case at all, but in most machines
3888 it never happens because available widths are 2**N. */
3889 && (!TREE_UNSIGNED (final_type)
3891 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3892 <= TYPE_PRECISION (result_type))))
3894 /* Do an unsigned shift if the operand was zero-extended. */
3896 = signed_or_unsigned_type (unsigned_arg,
3898 /* Convert value-to-be-shifted to that type. */
3899 if (TREE_TYPE (op0) != result_type)
3900 op0 = cp_convert (result_type, op0);
3905 /* Comparison operations are shortened too but differently.
3906 They identify themselves by setting short_compare = 1. */
3910 /* Don't write &op0, etc., because that would prevent op0
3911 from being kept in a register.
3912 Instead, make copies of the our local variables and
3913 pass the copies by reference, then copy them back afterward. */
3914 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3915 enum tree_code xresultcode = resultcode;
3917 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3919 return cp_convert (boolean_type_node, val);
3920 op0 = xop0, op1 = xop1;
3922 resultcode = xresultcode;
3925 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3926 && warn_sign_compare)
3928 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3929 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3931 int unsignedp0, unsignedp1;
3932 tree primop0 = get_narrower (op0, &unsignedp0);
3933 tree primop1 = get_narrower (op1, &unsignedp1);
3935 /* Check for comparison of different enum types. */
3936 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3937 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3938 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3939 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3941 cp_warning ("comparison between types `%#T' and `%#T'",
3942 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3945 /* Give warnings for comparisons between signed and unsigned
3946 quantities that may fail. */
3947 /* Do the checking based on the original operand trees, so that
3948 casts will be considered, but default promotions won't be. */
3950 /* Do not warn if the comparison is being done in a signed type,
3951 since the signed type will only be chosen if it can represent
3952 all the values of the unsigned type. */
3953 if (! TREE_UNSIGNED (result_type))
3955 /* Do not warn if both operands are unsigned. */
3956 else if (op0_signed == op1_signed)
3958 /* Do not warn if the signed quantity is an unsuffixed
3959 integer literal (or some static constant expression
3960 involving such literals or a conditional expression
3961 involving such literals) and it is non-negative. */
3962 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3963 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3965 /* Do not warn if the comparison is an equality operation,
3966 the unsigned quantity is an integral constant and it does
3967 not use the most significant bit of result_type. */
3968 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3969 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3970 && int_fits_type_p (orig_op1,
3971 signed_type (result_type)))
3972 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3973 && int_fits_type_p (orig_op0,
3974 signed_type (result_type)))))
3977 warning ("comparison between signed and unsigned integer expressions");
3979 /* Warn if two unsigned values are being compared in a size
3980 larger than their original size, and one (and only one) is the
3981 result of a `~' operator. This comparison will always fail.
3983 Also warn if one operand is a constant, and the constant does not
3984 have all bits set that are set in the ~ operand when it is
3987 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3988 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3990 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3991 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3992 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3993 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3995 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3998 HOST_WIDE_INT constant, mask;
4002 if (host_integerp (primop0, 0))
4005 unsignedp = unsignedp1;
4006 constant = tree_low_cst (primop0, 0);
4011 unsignedp = unsignedp0;
4012 constant = tree_low_cst (primop1, 0);
4015 bits = TYPE_PRECISION (TREE_TYPE (primop));
4016 if (bits < TYPE_PRECISION (result_type)
4017 && bits < HOST_BITS_PER_LONG && unsignedp)
4019 mask = (~ (HOST_WIDE_INT) 0) << bits;
4020 if ((mask & constant) != mask)
4021 warning ("comparison of promoted ~unsigned with constant");
4024 else if (unsignedp0 && unsignedp1
4025 && (TYPE_PRECISION (TREE_TYPE (primop0))
4026 < TYPE_PRECISION (result_type))
4027 && (TYPE_PRECISION (TREE_TYPE (primop1))
4028 < TYPE_PRECISION (result_type)))
4029 warning ("comparison of promoted ~unsigned with unsigned");
4034 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4035 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4036 Then the expression will be built.
4037 It will be given type FINAL_TYPE if that is nonzero;
4038 otherwise, it will be given type RESULT_TYPE. */
4042 cp_error ("invalid operands of types `%T' and `%T' to binary `%O'",
4043 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4044 return error_mark_node;
4047 /* Issue warnings about peculiar, but legal, uses of NULL. */
4048 if (/* It's reasonable to use pointer values as operands of &&
4049 and ||, so NULL is no exception. */
4050 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4051 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4052 (orig_op0 == null_node
4053 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4054 /* Or vice versa. */
4055 || (orig_op1 == null_node
4056 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4057 /* Or, both are NULL and the operation was not a comparison. */
4058 || (orig_op0 == null_node && orig_op1 == null_node
4059 && code != EQ_EXPR && code != NE_EXPR)))
4060 /* Some sort of arithmetic operation involving NULL was
4061 performed. Note that pointer-difference and pointer-addition
4062 have already been handled above, and so we don't end up here in
4064 cp_warning ("NULL used in arithmetic");
4068 if (TREE_TYPE (op0) != result_type)
4069 op0 = cp_convert (result_type, op0);
4070 if (TREE_TYPE (op1) != result_type)
4071 op1 = cp_convert (result_type, op1);
4073 if (op0 == error_mark_node || op1 == error_mark_node)
4074 return error_mark_node;
4077 if (build_type == NULL_TREE)
4078 build_type = result_type;
4081 register tree result = build (resultcode, build_type, op0, op1);
4082 register tree folded;
4084 folded = fold (result);
4085 if (folded == result)
4086 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4087 if (final_type != 0)
4088 return cp_convert (final_type, folded);
4093 /* Return a tree for the sum or difference (RESULTCODE says which)
4094 of pointer PTROP and integer INTOP. */
4097 pointer_int_sum (resultcode, ptrop, intop)
4098 enum tree_code resultcode;
4099 register tree ptrop, intop;
4103 register tree result;
4104 register tree folded = fold (intop);
4106 /* The result is a pointer of the same type that is being added. */
4108 register tree result_type = TREE_TYPE (ptrop);
4110 if (!complete_type_or_else (result_type, ptrop))
4111 return error_mark_node;
4113 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4115 if (pedantic || warn_pointer_arith)
4116 pedwarn ("ISO C++ forbids using pointer of type `void *' in pointer arithmetic");
4117 size_exp = integer_one_node;
4119 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4121 if (pedantic || warn_pointer_arith)
4122 pedwarn ("ISO C++ forbids using a pointer-to-function in pointer arithmetic");
4123 size_exp = integer_one_node;
4125 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4127 if (pedantic || warn_pointer_arith)
4128 pedwarn ("ISO C++ forbids using a pointer to member function in pointer arithmetic");
4129 size_exp = integer_one_node;
4131 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4133 if (pedantic || warn_pointer_arith)
4134 pedwarn ("ISO C++ forbids using pointer to a member in pointer arithmetic");
4135 size_exp = integer_one_node;
4138 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4140 /* Needed to make OOPS V2R3 work. */
4142 if (integer_zerop (intop))
4145 /* If what we are about to multiply by the size of the elements
4146 contains a constant term, apply distributive law
4147 and multiply that constant term separately.
4148 This helps produce common subexpressions. */
4150 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4151 && ! TREE_CONSTANT (intop)
4152 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4153 && TREE_CONSTANT (size_exp))
4155 enum tree_code subcode = resultcode;
4156 if (TREE_CODE (intop) == MINUS_EXPR)
4157 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4158 ptrop = cp_build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4159 intop = TREE_OPERAND (intop, 0);
4162 /* Convert the integer argument to a type the same size as sizetype
4163 so the multiply won't overflow spuriously. */
4165 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4166 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4168 /* Replace the integer argument with a suitable product by the object size.
4169 Do this multiplication as signed, then convert to the appropriate
4170 pointer type (actually unsigned integral). */
4172 intop = cp_convert (result_type,
4173 cp_build_binary_op (MULT_EXPR, intop,
4174 cp_convert (TREE_TYPE (intop),
4177 /* Create the sum or difference. */
4179 result = build (resultcode, result_type, ptrop, intop);
4181 folded = fold (result);
4182 if (folded == result)
4183 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4187 /* Return a tree for the difference of pointers OP0 and OP1.
4188 The resulting tree has type int. */
4191 pointer_diff (op0, op1, ptrtype)
4192 register tree op0, op1;
4193 register tree ptrtype;
4195 register tree result, folded;
4196 tree restype = ptrdiff_type_node;
4197 tree target_type = TREE_TYPE (ptrtype);
4199 if (!complete_type_or_else (target_type, NULL_TREE))
4200 return error_mark_node;
4202 if (pedantic || warn_pointer_arith)
4204 if (TREE_CODE (target_type) == VOID_TYPE)
4205 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4206 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4207 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4208 if (TREE_CODE (target_type) == METHOD_TYPE)
4209 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4210 if (TREE_CODE (target_type) == OFFSET_TYPE)
4211 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4214 /* First do the subtraction as integers;
4215 then drop through to build the divide operator. */
4217 op0 = cp_build_binary_op (MINUS_EXPR,
4218 cp_convert (restype, op0),
4219 cp_convert (restype, op1));
4221 /* This generates an error if op1 is a pointer to an incomplete type. */
4222 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4223 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4225 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4226 || TREE_CODE (target_type) == FUNCTION_TYPE
4227 || TREE_CODE (target_type) == METHOD_TYPE
4228 || TREE_CODE (target_type) == OFFSET_TYPE)
4230 : size_in_bytes (target_type));
4232 /* Do the division. */
4234 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4236 folded = fold (result);
4237 if (folded == result)
4238 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4242 /* Handle the case of taking the address of a COMPONENT_REF.
4243 Called by `build_unary_op'.
4245 ARG is the COMPONENT_REF whose address we want.
4246 ARGTYPE is the pointer type that this address should have. */
4249 build_component_addr (arg, argtype)
4252 tree field = TREE_OPERAND (arg, 1);
4253 tree basetype = decl_type_context (field);
4254 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4256 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4258 if (DECL_C_BIT_FIELD (field))
4260 cp_error ("attempt to take address of bit-field structure member `%D'",
4262 return error_mark_node;
4265 if (TREE_CODE (field) == FIELD_DECL
4266 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
4268 /* Can't convert directly to ARGTYPE, since that
4269 may have the same pointer type as one of our
4271 rval = build1 (NOP_EXPR, argtype,
4272 convert_pointer_to (basetype, rval));
4273 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4276 /* This conversion is harmless. */
4277 rval = convert_force (argtype, rval, 0);
4279 return fold (build (PLUS_EXPR, argtype, rval,
4280 cp_convert (argtype, byte_position (field))));
4283 /* Construct and perhaps optimize a tree representation
4284 for a unary operation. CODE, a tree_code, specifies the operation
4285 and XARG is the operand. */
4288 build_x_unary_op (code, xarg)
4289 enum tree_code code;
4295 if (processing_template_decl)
4296 return build_min_nt (code, xarg, NULL_TREE);
4298 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4300 if (code == ADDR_EXPR
4301 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4302 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4303 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
4304 || (TREE_CODE (xarg) == OFFSET_REF)))
4305 /* don't look for a function */;
4310 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4311 NULL_TREE, NULL_TREE);
4312 if (rval || code != ADDR_EXPR)
4315 if (code == ADDR_EXPR)
4317 if (TREE_CODE (xarg) == OFFSET_REF)
4319 ptrmem = PTRMEM_OK_P (xarg);
4321 if (!ptrmem && !flag_ms_extensions
4322 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4323 /* A single non-static member, make sure we don't allow a
4324 pointer-to-member. */
4325 xarg = ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE);
4327 else if (TREE_CODE (xarg) == TARGET_EXPR)
4328 warning ("taking address of temporary");
4330 exp = build_unary_op (code, xarg, 0);
4331 if (TREE_CODE (exp) == ADDR_EXPR)
4332 PTRMEM_OK_P (exp) = ptrmem;
4337 /* Like truthvalue_conversion, but handle pointer-to-member constants, where
4338 a null value is represented by an INTEGER_CST of -1. */
4341 cp_truthvalue_conversion (expr)
4344 tree type = TREE_TYPE (expr);
4345 if (TYPE_PTRMEM_P (type))
4346 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4348 return truthvalue_conversion (expr);
4351 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4354 condition_conversion (expr)
4358 if (processing_template_decl)
4360 t = perform_implicit_conversion (boolean_type_node, expr);
4361 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4365 /* C++: Must handle pointers to members.
4367 Perhaps type instantiation should be extended to handle conversion
4368 from aggregates to types we don't yet know we want? (Or are those
4369 cases typically errors which should be reported?)
4371 NOCONVERT nonzero suppresses the default promotions
4372 (such as from short to int). */
4375 build_unary_op (code, xarg, noconvert)
4376 enum tree_code code;
4380 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4381 register tree arg = xarg;
4382 register tree argtype = 0;
4383 const char *errstring = NULL;
4386 if (arg == error_mark_node)
4387 return error_mark_node;
4392 /* This is used for unary plus, because a CONVERT_EXPR
4393 is enough to prevent anybody from looking inside for
4394 associativity, but won't generate any code. */
4395 if (!(arg = build_expr_type_conversion
4396 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4397 errstring = "wrong type argument to unary plus";
4401 arg = default_conversion (arg);
4402 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4403 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4408 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4409 errstring = "wrong type argument to unary minus";
4410 else if (!noconvert)
4411 arg = default_conversion (arg);
4415 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4419 arg = default_conversion (arg);
4421 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4423 errstring = "wrong type argument to bit-complement";
4424 else if (!noconvert)
4425 arg = default_conversion (arg);
4429 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4430 errstring = "wrong type argument to abs";
4431 else if (!noconvert)
4432 arg = default_conversion (arg);
4436 /* Conjugating a real value is a no-op, but allow it anyway. */
4437 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4438 errstring = "wrong type argument to conjugation";
4439 else if (!noconvert)
4440 arg = default_conversion (arg);
4443 case TRUTH_NOT_EXPR:
4444 arg = cp_convert (boolean_type_node, arg);
4445 val = invert_truthvalue (arg);
4446 if (arg != error_mark_node)
4448 errstring = "in argument to unary !";
4455 if (TREE_CODE (arg) == COMPLEX_CST)
4456 return TREE_REALPART (arg);
4457 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4458 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4463 if (TREE_CODE (arg) == COMPLEX_CST)
4464 return TREE_IMAGPART (arg);
4465 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4466 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4468 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4470 case PREINCREMENT_EXPR:
4471 case POSTINCREMENT_EXPR:
4472 case PREDECREMENT_EXPR:
4473 case POSTDECREMENT_EXPR:
4474 /* Handle complex lvalues (when permitted)
4475 by reduction to simpler cases. */
4477 val = unary_complex_lvalue (code, arg);
4481 /* Increment or decrement the real part of the value,
4482 and don't change the imaginary part. */
4483 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4487 arg = stabilize_reference (arg);
4488 real = build_unary_op (REALPART_EXPR, arg, 1);
4489 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4490 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4491 build_unary_op (code, real, 1), imag);
4494 /* Report invalid types. */
4496 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4499 if (code == PREINCREMENT_EXPR)
4500 errstring ="no pre-increment operator for type";
4501 else if (code == POSTINCREMENT_EXPR)
4502 errstring ="no post-increment operator for type";
4503 else if (code == PREDECREMENT_EXPR)
4504 errstring ="no pre-decrement operator for type";
4506 errstring ="no post-decrement operator for type";
4510 /* Report something read-only. */
4512 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4513 || TREE_READONLY (arg))
4514 readonly_error (arg, ((code == PREINCREMENT_EXPR
4515 || code == POSTINCREMENT_EXPR)
4516 ? "increment" : "decrement"),
4521 tree result_type = TREE_TYPE (arg);
4523 arg = get_unwidened (arg, 0);
4524 argtype = TREE_TYPE (arg);
4526 /* ARM $5.2.5 last annotation says this should be forbidden. */
4527 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4528 pedwarn ("ISO C++ forbids %sing an enum",
4529 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4530 ? "increment" : "decrement");
4532 /* Compute the increment. */
4534 if (TREE_CODE (argtype) == POINTER_TYPE)
4536 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4537 tree type = complete_type (TREE_TYPE (argtype));
4539 if (!COMPLETE_OR_VOID_TYPE_P (type))
4540 cp_error ("cannot %s a pointer to incomplete type `%T'",
4541 ((code == PREINCREMENT_EXPR
4542 || code == POSTINCREMENT_EXPR)
4543 ? "increment" : "decrement"), TREE_TYPE (argtype));
4544 else if ((pedantic || warn_pointer_arith)
4545 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4546 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4547 cp_pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4548 ((code == PREINCREMENT_EXPR
4549 || code == POSTINCREMENT_EXPR)
4550 ? "increment" : "decrement"), argtype);
4551 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4554 inc = integer_one_node;
4556 inc = cp_convert (argtype, inc);
4558 /* Handle incrementing a cast-expression. */
4560 switch (TREE_CODE (arg))
4565 case FIX_TRUNC_EXPR:
4566 case FIX_FLOOR_EXPR:
4567 case FIX_ROUND_EXPR:
4570 tree incremented, modify, value, compound;
4571 if (! lvalue_p (arg) && pedantic)
4572 pedwarn ("cast to non-reference type used as lvalue");
4573 arg = stabilize_reference (arg);
4574 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4577 value = save_expr (arg);
4578 incremented = build (((code == PREINCREMENT_EXPR
4579 || code == POSTINCREMENT_EXPR)
4580 ? PLUS_EXPR : MINUS_EXPR),
4581 argtype, value, inc);
4583 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4584 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4586 /* Eliminate warning about unused result of + or -. */
4587 TREE_NO_UNUSED_WARNING (compound) = 1;
4595 /* Complain about anything else that is not a true lvalue. */
4596 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4597 || code == POSTINCREMENT_EXPR)
4598 ? "increment" : "decrement")))
4599 return error_mark_node;
4601 /* Forbid using -- on `bool'. */
4602 if (TREE_TYPE (arg) == boolean_type_node)
4604 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4606 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4607 return error_mark_node;
4610 /* This will only work if someone can convince Kenner to accept
4611 my patch to expand_increment. (jason) */
4612 val = build (code, TREE_TYPE (arg), arg, inc);
4614 val = boolean_increment (code, arg);
4618 val = build (code, TREE_TYPE (arg), arg, inc);
4620 TREE_SIDE_EFFECTS (val) = 1;
4621 return cp_convert (result_type, val);
4625 /* Note that this operation never does default_conversion
4626 regardless of NOCONVERT. */
4628 argtype = lvalue_type (arg);
4629 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4633 build_pointer_type (TREE_TYPE (argtype)), arg);
4634 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4637 else if (pedantic && DECL_MAIN_P (arg))
4639 pedwarn ("ISO C++ forbids taking address of function `::main'");
4641 /* Let &* cancel out to simplify resulting code. */
4642 if (TREE_CODE (arg) == INDIRECT_REF)
4644 /* We don't need to have `current_class_ptr' wrapped in a
4645 NON_LVALUE_EXPR node. */
4646 if (arg == current_class_ref)
4647 return current_class_ptr;
4649 arg = TREE_OPERAND (arg, 0);
4650 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4654 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4655 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4657 else if (lvalue_p (arg))
4658 /* Don't let this be an lvalue. */
4659 return non_lvalue (arg);
4663 /* For &x[y], return x+y */
4664 if (TREE_CODE (arg) == ARRAY_REF)
4666 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4667 return error_mark_node;
4668 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4669 TREE_OPERAND (arg, 1));
4672 /* Uninstantiated types are all functions. Taking the
4673 address of a function is a no-op, so just return the
4676 if (TREE_CODE (arg) == IDENTIFIER_NODE
4677 && IDENTIFIER_OPNAME_P (arg))
4679 my_friendly_abort (117);
4680 /* We don't know the type yet, so just work around the problem.
4681 We know that this will resolve to an lvalue. */
4682 return build1 (ADDR_EXPR, unknown_type_node, arg);
4685 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4686 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4688 /* They're trying to take the address of a unique non-static
4689 member function. This is ill-formed (except in MS-land),
4690 but let's try to DTRT.
4691 Note: We only handle unique functions here because we don't
4692 want to complain if there's a static overload; non-unique
4693 cases will be handled by instantiate_type. But we need to
4694 handle this case here to allow casts on the resulting PMF.
4695 We could defer this in non-MS mode, but it's easier to give
4696 a useful error here. */
4698 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4699 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4701 if (! flag_ms_extensions)
4703 if (current_class_type
4704 && TREE_OPERAND (arg, 0) == current_class_ref)
4705 /* An expression like &memfn. */
4706 cp_pedwarn ("ISO C++ forbids taking the address of an unqualified non-static member function to form a pointer to member function. Say `&%T::%D'", base, name);
4708 cp_pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base, name);
4710 arg = build_offset_ref (base, name);
4713 if (type_unknown_p (arg))
4714 return build1 (ADDR_EXPR, unknown_type_node, arg);
4716 /* Handle complex lvalues (when permitted)
4717 by reduction to simpler cases. */
4718 val = unary_complex_lvalue (code, arg);
4722 switch (TREE_CODE (arg))
4727 case FIX_TRUNC_EXPR:
4728 case FIX_FLOOR_EXPR:
4729 case FIX_ROUND_EXPR:
4731 if (! lvalue_p (arg) && pedantic)
4732 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4739 /* Allow the address of a constructor if all the elements
4741 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4742 && TREE_CONSTANT (arg))
4744 /* Anything not already handled and not a true memory reference
4746 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4747 && TREE_CODE (argtype) != METHOD_TYPE
4748 && !lvalue_or_else (arg, "unary `&'"))
4749 return error_mark_node;
4751 if (argtype != error_mark_node)
4752 argtype = build_pointer_type (argtype);
4754 if (mark_addressable (arg) == 0)
4755 return error_mark_node;
4760 if (TREE_CODE (arg) == COMPONENT_REF)
4761 addr = build_component_addr (arg, argtype);
4763 addr = build1 (ADDR_EXPR, argtype, arg);
4765 /* Address of a static or external variable or
4766 function counts as a constant */
4768 TREE_CONSTANT (addr) = 1;
4770 if (TREE_CODE (argtype) == POINTER_TYPE
4771 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4773 build_ptrmemfunc_type (argtype);
4774 addr = build_ptrmemfunc (argtype, addr, 0);
4787 argtype = TREE_TYPE (arg);
4788 return fold (build1 (code, argtype, arg));
4791 error ("%s", errstring);
4792 return error_mark_node;
4795 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4796 for certain kinds of expressions which are not really lvalues
4797 but which we can accept as lvalues.
4799 If ARG is not a kind of expression we can handle, return zero. */
4802 unary_complex_lvalue (code, arg)
4803 enum tree_code code;
4806 /* Handle (a, b) used as an "lvalue". */
4807 if (TREE_CODE (arg) == COMPOUND_EXPR)
4809 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4810 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4811 TREE_OPERAND (arg, 0), real_result);
4814 /* Handle (a ? b : c) used as an "lvalue". */
4815 if (TREE_CODE (arg) == COND_EXPR
4816 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4817 return rationalize_conditional_expr (code, arg);
4819 if (TREE_CODE (arg) == MODIFY_EXPR
4820 || TREE_CODE (arg) == PREINCREMENT_EXPR
4821 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4822 return unary_complex_lvalue
4823 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4824 arg, TREE_OPERAND (arg, 0)));
4826 if (code != ADDR_EXPR)
4829 /* Handle (a = b) used as an "lvalue" for `&'. */
4830 if (TREE_CODE (arg) == MODIFY_EXPR
4831 || TREE_CODE (arg) == INIT_EXPR)
4833 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4834 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4835 TREE_NO_UNUSED_WARNING (arg) = 1;
4839 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4840 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4841 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4843 /* The representation of something of type OFFSET_TYPE
4844 is really the representation of a pointer to it.
4845 Here give the representation its true type. */
4848 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4850 if (TREE_CODE (arg) != OFFSET_REF)
4853 t = TREE_OPERAND (arg, 1);
4855 /* Check all this code for right semantics. */
4856 if (TREE_CODE (t) == FUNCTION_DECL)
4858 if (DECL_DESTRUCTOR_P (t))
4859 cp_error ("taking address of destructor");
4860 return build_unary_op (ADDR_EXPR, t, 0);
4862 if (TREE_CODE (t) == VAR_DECL)
4863 return build_unary_op (ADDR_EXPR, t, 0);
4868 if (TREE_OPERAND (arg, 0)
4869 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4870 && TREE_CODE (t) != FIELD_DECL)
4872 cp_error ("taking address of bound pointer-to-member expression");
4873 return error_mark_node;
4876 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4877 type = build_pointer_type (type);
4879 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4885 /* We permit compiler to make function calls returning
4886 objects of aggregate type look like lvalues. */
4890 if (TREE_CODE (targ) == SAVE_EXPR)
4891 targ = TREE_OPERAND (targ, 0);
4893 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4895 if (TREE_CODE (arg) == SAVE_EXPR)
4898 targ = build_cplus_new (TREE_TYPE (arg), arg);
4899 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4902 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4903 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4904 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4907 /* Don't let anything else be handled specially. */
4911 /* Mark EXP saying that we need to be able to take the
4912 address of it; it should not be allocated in a register.
4913 Value is 1 if successful.
4915 C++: we do not allow `current_class_ptr' to be addressable. */
4918 mark_addressable (exp)
4921 register tree x = exp;
4923 if (TREE_ADDRESSABLE (x) == 1)
4927 switch (TREE_CODE (x))
4934 x = TREE_OPERAND (x, 0);
4938 if (x == current_class_ptr)
4940 error ("cannot take the address of `this', which is an rvalue expression");
4941 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4945 /* Caller should not be trying to mark initialized
4946 constant fields addressable. */
4947 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4948 || DECL_IN_AGGR_P (x) == 0
4950 || DECL_EXTERNAL (x), 314);
4954 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4955 && !DECL_ARTIFICIAL (x) && extra_warnings)
4956 cp_warning ("address requested for `%D', which is declared `register'",
4958 TREE_ADDRESSABLE (x) = 1;
4962 TREE_ADDRESSABLE (x) = 1;
4963 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4967 TREE_ADDRESSABLE (x) = 1;
4971 TREE_ADDRESSABLE (x) = 1;
4972 mark_addressable (TREE_OPERAND (x, 0));
4980 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4983 build_x_conditional_expr (ifexp, op1, op2)
4984 tree ifexp, op1, op2;
4986 if (processing_template_decl)
4987 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4989 return build_conditional_expr (ifexp, op1, op2);
4992 /* Handle overloading of the ',' operator when needed. Otherwise,
4993 this function just builds an expression list. */
4996 build_x_compound_expr (list)
4999 tree rest = TREE_CHAIN (list);
5002 if (processing_template_decl)
5003 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5005 if (rest == NULL_TREE)
5006 return build_compound_expr (list);
5008 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5009 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5011 return build_x_compound_expr (tree_cons (NULL_TREE, result,
5012 TREE_CHAIN (rest)));
5014 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5016 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5017 /* the left-hand operand of a comma expression is like an expression
5018 statement: we should warn if it doesn't have any side-effects,
5019 unless it was explicitly cast to (void). */
5020 if ((extra_warnings || warn_unused_value)
5021 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5022 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
5023 warning("left-hand operand of comma expression has no effect");
5025 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5026 else if (warn_unused_value)
5027 warn_if_unused_value (TREE_VALUE(list));
5030 return build_compound_expr
5031 (tree_cons (NULL_TREE, TREE_VALUE (list),
5032 build_tree_list (NULL_TREE,
5033 build_x_compound_expr (rest))));
5036 /* Given a list of expressions, return a compound expression
5037 that performs them all and returns the value of the last of them. */
5040 build_compound_expr (list)
5046 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5048 if (TREE_CHAIN (list) == 0)
5050 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5051 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5052 if (TREE_CODE (list) == NOP_EXPR
5053 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5054 list = TREE_OPERAND (list, 0);
5056 return TREE_VALUE (list);
5059 first = TREE_VALUE (list);
5060 first = convert_to_void (first, "left-hand operand of comma");
5061 if (first == error_mark_node)
5062 return error_mark_node;
5064 rest = build_compound_expr (TREE_CHAIN (list));
5065 if (rest == error_mark_node)
5066 return error_mark_node;
5068 /* When pedantic, a compound expression cannot be a constant expression. */
5069 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5072 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
5076 build_static_cast (type, expr)
5082 if (type == error_mark_node || expr == error_mark_node)
5083 return error_mark_node;
5085 if (TREE_CODE (expr) == OFFSET_REF)
5086 expr = resolve_offset_ref (expr);
5088 if (processing_template_decl)
5090 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5094 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5095 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5096 if (TREE_CODE (type) != REFERENCE_TYPE
5097 && TREE_CODE (expr) == NOP_EXPR
5098 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5099 expr = TREE_OPERAND (expr, 0);
5101 if (TREE_CODE (type) == VOID_TYPE)
5103 expr = convert_to_void (expr, /*implicit=*/NULL);
5107 if (TREE_CODE (type) == REFERENCE_TYPE)
5108 return (convert_from_reference
5109 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5110 LOOKUP_COMPLAIN, NULL_TREE)));
5112 if (IS_AGGR_TYPE (type))
5113 return build_cplus_new (type, (build_method_call
5114 (NULL_TREE, complete_ctor_identifier,
5115 build_tree_list (NULL_TREE, expr),
5116 TYPE_BINFO (type), LOOKUP_NORMAL)));
5118 expr = decay_conversion (expr);
5119 intype = TREE_TYPE (expr);
5121 /* FIXME handle casting to array type. */
5124 if (IS_AGGR_TYPE (intype)
5125 ? can_convert_arg (type, intype, expr)
5126 : can_convert_arg (strip_all_pointer_quals (type),
5127 strip_all_pointer_quals (intype), expr))
5128 /* This is a standard conversion. */
5130 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5132 /* They're pointers to objects. They must be aggregates that
5133 are related non-virtually. */
5137 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5138 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5139 && !binfo_from_vbase (binfo))
5142 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5143 && TREE_CODE (type) != ARRAY_TYPE
5144 && TREE_CODE (type) != FUNCTION_TYPE
5145 && can_convert (intype, strip_all_pointer_quals (type)))
5147 else if (TREE_CODE (intype) == ENUMERAL_TYPE
5148 && TREE_CODE (type) == ENUMERAL_TYPE)
5149 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
5150 converted to an enumeration type."
5151 The integral to enumeration will be accepted by the previous clause.
5152 We need to explicitly check for enumeration to enumeration. */
5155 /* [expr.static.cast]
5157 The static_cast operator shall not be used to cast away
5159 if (ok && casts_away_constness (intype, type))
5161 cp_error ("static_cast from type `%T' to type `%T' casts away constness",
5163 return error_mark_node;
5167 return build_c_cast (type, expr);
5169 cp_error ("invalid static_cast from type `%T' to type `%T'", intype, type);
5170 return error_mark_node;
5174 build_reinterpret_cast (type, expr)
5179 if (type == error_mark_node || expr == error_mark_node)
5180 return error_mark_node;
5182 if (TREE_CODE (expr) == OFFSET_REF)
5183 expr = resolve_offset_ref (expr);
5185 if (processing_template_decl)
5187 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5191 if (TREE_CODE (type) != REFERENCE_TYPE)
5193 expr = decay_conversion (expr);
5195 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5196 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5197 if (TREE_CODE (expr) == NOP_EXPR
5198 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5199 expr = TREE_OPERAND (expr, 0);
5202 intype = TREE_TYPE (expr);
5204 if (TREE_CODE (type) == REFERENCE_TYPE)
5206 if (! real_lvalue_p (expr))
5208 cp_error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
5209 return error_mark_node;
5211 expr = build_unary_op (ADDR_EXPR, expr, 0);
5212 if (expr != error_mark_node)
5213 expr = build_reinterpret_cast
5214 (build_pointer_type (TREE_TYPE (type)), expr);
5215 if (expr != error_mark_node)
5216 expr = build_indirect_ref (expr, 0);
5219 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5220 return build_static_cast (type, expr);
5222 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5223 || TREE_CODE (intype) == ENUMERAL_TYPE))
5225 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5227 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5228 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5231 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5232 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5234 expr = decl_constant_value (expr);
5235 return fold (build1 (NOP_EXPR, type, expr));
5237 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5238 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5240 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5241 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5244 expr = decl_constant_value (expr);
5245 return fold (build1 (NOP_EXPR, type, expr));
5247 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5248 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5250 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5251 expr = decl_constant_value (expr);
5252 return fold (build1 (NOP_EXPR, type, expr));
5256 cp_error ("invalid reinterpret_cast from type `%T' to type `%T'",
5258 return error_mark_node;
5261 return cp_convert (type, expr);
5265 build_const_cast (type, expr)
5270 if (type == error_mark_node || expr == error_mark_node)
5271 return error_mark_node;
5273 if (TREE_CODE (expr) == OFFSET_REF)
5274 expr = resolve_offset_ref (expr);
5276 if (processing_template_decl)
5278 tree t = build_min (CONST_CAST_EXPR, type, expr);
5282 if (!POINTER_TYPE_P (type))
5283 cp_error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5284 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5286 cp_error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5287 return error_mark_node;
5290 if (TREE_CODE (type) != REFERENCE_TYPE)
5292 expr = decay_conversion (expr);
5294 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5295 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5296 if (TREE_CODE (expr) == NOP_EXPR
5297 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5298 expr = TREE_OPERAND (expr, 0);
5301 intype = TREE_TYPE (expr);
5303 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5304 return build_static_cast (type, expr);
5305 else if (TREE_CODE (type) == REFERENCE_TYPE)
5307 if (! real_lvalue_p (expr))
5309 cp_error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5310 return error_mark_node;
5313 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5315 expr = build_unary_op (ADDR_EXPR, expr, 0);
5316 expr = build1 (NOP_EXPR, type, expr);
5317 return convert_from_reference (expr);
5320 else if (TREE_CODE (type) == POINTER_TYPE
5321 && TREE_CODE (intype) == POINTER_TYPE
5322 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5323 return cp_convert (type, expr);
5325 cp_error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5326 return error_mark_node;
5329 /* Build an expression representing a cast to type TYPE of expression EXPR.
5331 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5332 when doing the cast. */
5335 build_c_cast (type, expr)
5338 register tree value = expr;
5341 if (type == error_mark_node || expr == error_mark_node)
5342 return error_mark_node;
5344 if (processing_template_decl)
5346 tree t = build_min (CAST_EXPR, type,
5347 tree_cons (NULL_TREE, value, NULL_TREE));
5351 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5352 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5353 if (TREE_CODE (type) != REFERENCE_TYPE
5354 && TREE_CODE (value) == NOP_EXPR
5355 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5356 value = TREE_OPERAND (value, 0);
5358 if (TREE_CODE (value) == OFFSET_REF)
5359 value = resolve_offset_ref (value);
5361 if (TREE_CODE (type) == ARRAY_TYPE)
5363 /* Allow casting from T1* to T2[] because Cfront allows it.
5364 NIHCL uses it. It is not valid ISO C++ however. */
5365 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5367 cp_pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5368 type = build_pointer_type (TREE_TYPE (type));
5372 cp_error ("ISO C++ forbids casting to an array type `%T'", type);
5373 return error_mark_node;
5377 if (TREE_CODE (type) == FUNCTION_TYPE
5378 || TREE_CODE (type) == METHOD_TYPE)
5380 cp_error ("invalid cast to function type `%T'", type);
5381 return error_mark_node;
5384 if (TREE_CODE (type) == VOID_TYPE)
5386 /* Conversion to void does not cause any of the normal function to
5387 * pointer, array to pointer and lvalue to rvalue decays. */
5389 value = convert_to_void (value, /*implicit=*/NULL);
5392 /* Convert functions and arrays to pointers and
5393 convert references to their expanded types,
5394 but don't convert any other types. If, however, we are
5395 casting to a class type, there's no reason to do this: the
5396 cast will only succeed if there is a converting constructor,
5397 and the default conversions will be done at that point. In
5398 fact, doing the default conversion here is actually harmful
5402 struct S { S(const A&); };
5404 since we don't want the array-to-pointer conversion done. */
5405 if (!IS_AGGR_TYPE (type))
5407 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5408 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5409 /* Don't do the default conversion on a ->* expression. */
5410 && ! (TREE_CODE (type) == POINTER_TYPE
5411 && bound_pmf_p (value)))
5412 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5413 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5414 value = default_conversion (value);
5416 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5417 /* However, even for class types, we still need to strip away
5418 the reference type, since the call to convert_force below
5419 does not expect the input expression to be of reference
5421 value = convert_from_reference (value);
5423 otype = TREE_TYPE (value);
5425 /* Optionally warn about potentially worrisome casts. */
5428 && TREE_CODE (type) == POINTER_TYPE
5429 && TREE_CODE (otype) == POINTER_TYPE
5430 && !at_least_as_qualified_p (TREE_TYPE (type),
5432 cp_warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5435 if (TREE_CODE (type) == INTEGER_TYPE
5436 && TREE_CODE (otype) == POINTER_TYPE
5437 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5438 warning ("cast from pointer to integer of different size");
5440 if (TREE_CODE (type) == POINTER_TYPE
5441 && TREE_CODE (otype) == INTEGER_TYPE
5442 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5443 /* Don't warn about converting any constant. */
5444 && !TREE_CONSTANT (value))
5445 warning ("cast to pointer from integer of different size");
5447 if (TREE_CODE (type) == REFERENCE_TYPE)
5448 value = (convert_from_reference
5449 (convert_to_reference (type, value, CONV_C_CAST,
5450 LOOKUP_COMPLAIN, NULL_TREE)));
5455 value = decl_constant_value (value);
5458 value = convert_force (type, value, CONV_C_CAST);
5460 /* Ignore any integer overflow caused by the cast. */
5461 if (TREE_CODE (value) == INTEGER_CST)
5463 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5464 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5468 /* Warn about possible alignment problems. Do this here when we will have
5469 instantiated any necessary template types. */
5470 if (STRICT_ALIGNMENT && warn_cast_align
5471 && TREE_CODE (type) == POINTER_TYPE
5472 && TREE_CODE (otype) == POINTER_TYPE
5473 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5474 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5475 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5476 && COMPLETE_TYPE_P (TREE_TYPE (type))
5477 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5478 cp_warning ("cast from `%T' to `%T' increases required alignment of target type",
5481 /* Always produce some operator for an explicit cast,
5482 so we can tell (for -pedantic) that the cast is no lvalue. */
5483 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5484 && real_lvalue_p (value))
5485 value = non_lvalue (value);
5490 /* Build an assignment expression of lvalue LHS from value RHS.
5491 MODIFYCODE is the code for a binary operator that we use
5492 to combine the old value of LHS with RHS to get the new value.
5493 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5495 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5498 build_modify_expr (lhs, modifycode, rhs)
5500 enum tree_code modifycode;
5503 register tree result;
5505 tree lhstype = TREE_TYPE (lhs);
5506 tree olhstype = lhstype;
5509 /* Avoid duplicate error messages from operands that had errors. */
5510 if (lhs == error_mark_node || rhs == error_mark_node)
5511 return error_mark_node;
5513 /* Types that aren't fully specified cannot be used in assignments. */
5514 lhs = require_complete_type (lhs);
5518 /* Handle control structure constructs used as "lvalues". */
5520 switch (TREE_CODE (lhs))
5522 /* Handle --foo = 5; as these are valid constructs in C++ */
5523 case PREDECREMENT_EXPR:
5524 case PREINCREMENT_EXPR:
5525 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5526 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5527 stabilize_reference (TREE_OPERAND (lhs, 0)),
5528 TREE_OPERAND (lhs, 1));
5529 return build (COMPOUND_EXPR, lhstype,
5531 build_modify_expr (TREE_OPERAND (lhs, 0),
5534 /* Handle (a, b) used as an "lvalue". */
5536 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5538 if (newrhs == error_mark_node)
5539 return error_mark_node;
5540 return build (COMPOUND_EXPR, lhstype,
5541 TREE_OPERAND (lhs, 0), newrhs);
5544 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5545 if (newrhs == error_mark_node)
5546 return error_mark_node;
5547 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5549 /* Handle (a ? b : c) used as an "lvalue". */
5551 rhs = save_expr (rhs);
5553 /* Produce (a ? (b = rhs) : (c = rhs))
5554 except that the RHS goes through a save-expr
5555 so the code to compute it is only emitted once. */
5558 /* Check this here to avoid odd errors when trying to convert
5559 a throw to the type of the COND_EXPR. */
5560 if (!lvalue_or_else (lhs, "assignment"))
5561 return error_mark_node;
5563 cond = build_conditional_expr
5564 (TREE_OPERAND (lhs, 0),
5565 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5566 TREE_OPERAND (lhs, 1)),
5568 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5569 TREE_OPERAND (lhs, 2)),
5572 if (cond == error_mark_node)
5574 /* Make sure the code to compute the rhs comes out
5575 before the split. */
5576 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5577 /* Case to void to suppress warning
5578 from warn_if_unused_value. */
5579 cp_convert (void_type_node, rhs), cond);
5586 if (TREE_CODE (lhs) == OFFSET_REF)
5588 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5590 /* Static class member? */
5591 tree member = TREE_OPERAND (lhs, 1);
5592 if (TREE_CODE (member) == VAR_DECL)
5596 compiler_error ("invalid static class member");
5597 return error_mark_node;
5601 lhs = resolve_offset_ref (lhs);
5603 olhstype = lhstype = TREE_TYPE (lhs);
5606 if (lhs == error_mark_node)
5609 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5610 && modifycode != INIT_EXPR)
5612 lhs = convert_from_reference (lhs);
5613 olhstype = lhstype = TREE_TYPE (lhs);
5616 /* If a binary op has been requested, combine the old LHS value with the RHS
5617 producing the value we should actually store into the LHS. */
5619 if (modifycode == INIT_EXPR)
5621 if (TREE_CODE (rhs) == CONSTRUCTOR)
5623 if (! same_type_p (TREE_TYPE (rhs), lhstype))
5625 result = build (INIT_EXPR, lhstype, lhs, rhs);
5626 TREE_SIDE_EFFECTS (result) = 1;
5629 else if (! IS_AGGR_TYPE (lhstype))
5630 /* Do the default thing */;
5633 result = build_method_call (lhs, complete_ctor_identifier,
5634 build_tree_list (NULL_TREE, rhs),
5635 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5636 if (result == NULL_TREE)
5637 return error_mark_node;
5641 else if (modifycode == NOP_EXPR)
5643 /* `operator=' is not an inheritable operator. */
5644 if (! IS_AGGR_TYPE (lhstype))
5645 /* Do the default thing */;
5648 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5649 lhs, rhs, make_node (NOP_EXPR));
5650 if (result == NULL_TREE)
5651 return error_mark_node;
5656 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5658 my_friendly_abort (978652);
5662 lhs = stabilize_reference (lhs);
5663 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5664 if (newrhs == error_mark_node)
5666 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5667 TREE_TYPE (lhs), TREE_TYPE (rhs));
5668 return error_mark_node;
5672 /* Handle a cast used as an "lvalue".
5673 We have already performed any binary operator using the value as cast.
5674 Now convert the result to the cast type of the lhs,
5675 and then true type of the lhs and store it there;
5676 then convert result back to the cast type to be the value
5677 of the assignment. */
5679 switch (TREE_CODE (lhs))
5684 case FIX_TRUNC_EXPR:
5685 case FIX_FLOOR_EXPR:
5686 case FIX_ROUND_EXPR:
5688 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5689 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5690 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5691 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5692 newrhs = default_conversion (newrhs);
5694 tree inner_lhs = TREE_OPERAND (lhs, 0);
5697 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5698 type, otherwise the result is an rvalue. */
5699 if (! lvalue_p (lhs))
5700 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5702 result = build_modify_expr (inner_lhs, NOP_EXPR,
5703 cp_convert (TREE_TYPE (inner_lhs),
5704 cp_convert (lhstype, newrhs)));
5705 if (result == error_mark_node)
5707 return cp_convert (TREE_TYPE (lhs), result);
5714 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5715 Reject anything strange now. */
5717 if (!lvalue_or_else (lhs, "assignment"))
5718 return error_mark_node;
5720 GNU_xref_assign (lhs);
5722 /* Warn about storing in something that is `const'. */
5723 /* For C++, don't warn if this is initialization. */
5724 if (modifycode != INIT_EXPR
5725 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5726 /* Functions are not modifiable, even though they are
5728 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5729 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5730 && C_TYPE_FIELDS_READONLY (lhstype))
5731 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5732 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5733 readonly_error (lhs, "assignment", 0);
5735 /* If storing into a structure or union member,
5736 it has probably been given type `int'.
5737 Compute the type that would go with
5738 the actual amount of storage the member occupies. */
5740 if (TREE_CODE (lhs) == COMPONENT_REF
5741 && (TREE_CODE (lhstype) == INTEGER_TYPE
5742 || TREE_CODE (lhstype) == REAL_TYPE
5743 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5745 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5747 /* If storing in a field that is in actuality a short or narrower
5748 than one, we must store in the field in its actual type. */
5750 if (lhstype != TREE_TYPE (lhs))
5752 lhs = copy_node (lhs);
5753 TREE_TYPE (lhs) = lhstype;
5757 if (modifycode != INIT_EXPR)
5759 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5760 modifycode = NOP_EXPR;
5761 /* Reference-bashing */
5762 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5764 tree tmp = convert_from_reference (lhs);
5765 lhstype = TREE_TYPE (tmp);
5766 if (!COMPLETE_TYPE_P (lhstype))
5768 incomplete_type_error (lhs, lhstype);
5769 return error_mark_node;
5774 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5776 tree tmp = convert_from_reference (newrhs);
5777 if (!COMPLETE_TYPE_P (TREE_TYPE (tmp)))
5779 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5780 return error_mark_node;
5786 if (TREE_SIDE_EFFECTS (lhs))
5787 lhs = stabilize_reference (lhs);
5788 if (TREE_SIDE_EFFECTS (newrhs))
5789 newrhs = stabilize_reference (newrhs);
5791 /* Convert new value to destination type. */
5793 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5797 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5799 cp_error ("incompatible types in assignment of `%T' to `%T'",
5800 TREE_TYPE (rhs), lhstype);
5801 return error_mark_node;
5804 /* Allow array assignment in compiler-generated code. */
5805 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5806 pedwarn ("ISO C++ forbids assignment of arrays");
5808 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5809 ? 1 + (modifycode != INIT_EXPR): 0;
5810 return build_vec_init (lhs, newrhs, from_array);
5813 if (modifycode == INIT_EXPR)
5815 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5816 "initialization", NULL_TREE, 0);
5817 if (current_function_decl &&
5818 lhs == DECL_RESULT (current_function_decl))
5820 if (DECL_INITIAL (lhs))
5821 warning ("return value from function receives multiple initializations");
5822 DECL_INITIAL (lhs) = newrhs;
5827 /* Avoid warnings on enum bit fields. */
5828 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5829 && TREE_CODE (lhstype) == INTEGER_TYPE)
5831 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5833 newrhs = convert_force (lhstype, newrhs, 0);
5836 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5838 if (TREE_CODE (newrhs) == CALL_EXPR
5839 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5840 newrhs = build_cplus_new (lhstype, newrhs);
5842 /* Can't initialize directly from a TARGET_EXPR, since that would
5843 cause the lhs to be constructed twice, and possibly result in
5844 accidental self-initialization. So we force the TARGET_EXPR to be
5845 expanded without a target. */
5846 if (TREE_CODE (newrhs) == TARGET_EXPR)
5847 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5848 TREE_OPERAND (newrhs, 0));
5851 if (newrhs == error_mark_node)
5852 return error_mark_node;
5854 if (TREE_CODE (newrhs) == COND_EXPR)
5857 tree cond = TREE_OPERAND (newrhs, 0);
5859 if (TREE_SIDE_EFFECTS (lhs))
5860 cond = build_compound_expr (tree_cons
5862 build_tree_list (NULL_TREE, cond)));
5864 /* Cannot have two identical lhs on this one tree (result) as preexpand
5865 calls will rip them out and fill in RTL for them, but when the
5866 rtl is generated, the calls will only be in the first side of the
5867 condition, not on both, or before the conditional jump! (mrs) */
5868 lhs1 = break_out_calls (lhs);
5871 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5872 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5873 lhstype, lhs, newrhs);
5876 tree result_type = TREE_TYPE (newrhs);
5877 /* We have to convert each arm to the proper type because the
5878 types may have been munged by constant folding. */
5880 = build (COND_EXPR, result_type, cond,
5881 build_modify_expr (lhs, modifycode,
5882 cp_convert (result_type,
5883 TREE_OPERAND (newrhs, 1))),
5884 build_modify_expr (lhs1, modifycode,
5885 cp_convert (result_type,
5886 TREE_OPERAND (newrhs, 2))));
5890 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5891 lhstype, lhs, newrhs);
5893 TREE_SIDE_EFFECTS (result) = 1;
5895 /* If we got the LHS in a different type for storing in,
5896 convert the result back to the nominal type of LHS
5897 so that the value we return always has the same type
5898 as the LHS argument. */
5900 if (olhstype == TREE_TYPE (result))
5902 /* Avoid warnings converting integral types back into enums
5903 for enum bit fields. */
5904 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5905 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5907 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5908 TREE_NO_UNUSED_WARNING (result) = 1;
5911 return convert_for_assignment (olhstype, result, "assignment",
5916 build_x_modify_expr (lhs, modifycode, rhs)
5918 enum tree_code modifycode;
5921 if (processing_template_decl)
5922 return build_min_nt (MODOP_EXPR, lhs,
5923 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5925 if (modifycode != NOP_EXPR)
5927 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5928 make_node (modifycode));
5932 return build_modify_expr (lhs, modifycode, rhs);
5936 /* Get difference in deltas for different pointer to member function
5937 types. Return integer_zero_node, if FROM cannot be converted to a
5938 TO type. If FORCE is true, then allow reverse conversions as well.
5940 Note that the naming of FROM and TO is kind of backwards; the return
5941 value is what we add to a TO in order to get a FROM. They are named
5942 this way because we call this function to find out how to convert from
5943 a pointer to member of FROM to a pointer to member of TO. */
5946 get_delta_difference (from, to, force)
5950 tree delta = integer_zero_node;
5957 /* Should get_base_distance here, so we can check if any thing along
5958 the path is virtual, and we need to make sure we stay inside the
5959 real binfos when going through virtual bases. Maybe we should
5960 replace virtual bases with BINFO_FOR_VBASE ... (mrs) */
5961 binfo = get_binfo (from, to, 1);
5962 if (binfo == error_mark_node)
5964 error (" in pointer to member function conversion");
5971 error_not_base_type (from, to);
5972 error (" in pointer to member conversion");
5975 binfo = get_binfo (to, from, 1);
5976 if (binfo == 0 || binfo == error_mark_node)
5978 virt_binfo = binfo_from_vbase (binfo);
5982 /* This is a reinterpret cast, we choose to do nothing. */
5983 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
5984 BINFO_TYPE (virt_binfo),
5985 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5988 delta = BINFO_OFFSET (binfo);
5989 delta = cp_convert (ptrdiff_type_node, delta);
5990 delta = cp_build_binary_op (MINUS_EXPR,
5997 virt_binfo = binfo_from_vbase (binfo);
6000 /* This is a reinterpret cast, we choose to do nothing. */
6002 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
6003 BINFO_TYPE (virt_binfo),
6004 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
6006 cp_error ("pointer to member conversion via virtual base `%T' of `%T'",
6007 BINFO_TYPE (virt_binfo),
6008 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
6011 delta = BINFO_OFFSET (binfo);
6013 return cp_convert (ptrdiff_type_node, delta);
6016 /* Return a constructor for the pointer-to-member-function TYPE using
6017 the other components as specified. */
6020 build_ptrmemfunc1 (type, delta, pfn)
6021 tree type, delta, pfn;
6027 /* Pull the FIELD_DECLs out of the type. */
6028 pfn_field = TYPE_FIELDS (type);
6029 delta_field = TREE_CHAIN (pfn_field);
6031 /* Make sure DELTA has the type we want. */
6032 delta = convert_and_check (delta_type_node, delta);
6034 /* Finish creating the initializer. */
6035 u = tree_cons (pfn_field, pfn,
6036 build_tree_list (delta_field, delta));
6037 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6038 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
6039 TREE_STATIC (u) = (TREE_CONSTANT (u)
6040 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6042 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
6047 /* Build a constructor for a pointer to member function. It can be
6048 used to initialize global variables, local variable, or used
6049 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6052 If FORCE is non-zero, then force this conversion, even if
6053 we would rather not do it. Usually set when using an explicit
6056 Return error_mark_node, if something goes wrong. */
6059 build_ptrmemfunc (type, pfn, force)
6064 tree pfn_type = TREE_TYPE (pfn);
6065 tree to_type = build_ptrmemfunc_type (type);
6067 /* Handle multiple conversions of pointer to member functions. */
6068 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6070 tree delta = NULL_TREE;
6071 tree npfn = NULL_TREE;
6075 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6076 cp_error ("invalid conversion to type `%T' from type `%T'",
6079 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6080 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6083 /* We don't have to do any conversion to convert a
6084 pointer-to-member to its own type. But, we don't want to
6085 just return a PTRMEM_CST if there's an explicit cast; that
6086 cast should make the expression an invalid template argument. */
6087 if (TREE_CODE (pfn) != PTRMEM_CST)
6089 if (same_type_p (to_type, pfn_type))
6091 else if (integer_zerop (n))
6092 return build_reinterpret_cast (to_type, pfn);
6095 if (TREE_SIDE_EFFECTS (pfn))
6096 pfn = save_expr (pfn);
6098 /* Obtain the function pointer and the current DELTA. */
6099 if (TREE_CODE (pfn) == PTRMEM_CST)
6100 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
6103 npfn = build_component_ref (pfn, pfn_identifier, NULL_TREE, 0);
6104 delta = build_component_ref (pfn, delta_identifier, NULL_TREE, 0);
6107 /* Under the new ABI, the conversion is easy. Just adjust
6109 delta = cp_convert (ptrdiff_type_node, delta);
6110 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
6111 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
6112 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
6113 return build_ptrmemfunc1 (to_type, delta, npfn);
6116 /* Handle null pointer to member function conversions. */
6117 if (integer_zerop (pfn))
6119 pfn = build_c_cast (type, integer_zero_node);
6120 return build_ptrmemfunc1 (to_type,
6125 if (type_unknown_p (pfn))
6126 return instantiate_type (type, pfn, itf_complain);
6128 fn = TREE_OPERAND (pfn, 0);
6129 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6130 return make_ptrmem_cst (to_type, fn);
6133 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6136 ??? There is no consistency as to the types returned for the above
6137 values. Some code acts as if its a sizetype and some as if its
6138 integer_type_node. */
6141 expand_ptrmemfunc_cst (cst, delta, pfn)
6146 tree type = TREE_TYPE (cst);
6147 tree fn = PTRMEM_CST_MEMBER (cst);
6148 tree ptr_class, fn_class;
6150 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6152 /* The class that the function belongs to. */
6153 fn_class = DECL_CONTEXT (fn);
6155 /* The class that we're creating a pointer to member of. */
6156 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6158 /* First, calculate the adjustment to the function's class. */
6159 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6161 if (!DECL_VIRTUAL_P (fn))
6162 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6165 /* If we're dealing with a virtual function, we have to adjust 'this'
6166 again, to point to the base which provides the vtable entry for
6167 fn; the call will do the opposite adjustment. */
6168 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6169 tree binfo = binfo_or_else (orig_class, fn_class);
6170 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6171 *delta, BINFO_OFFSET (binfo)));
6173 /* Under the new ABI, we set PFN to the vtable offset at
6174 which the function can be found, plus one (unless
6175 ptrmemfunc_vbit_in_delta, in which case delta is shifted
6176 left, and then incremented). */
6177 *pfn = DECL_VINDEX (fn);
6179 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
6181 case ptrmemfunc_vbit_in_pfn:
6182 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
6183 TYPE_SIZE_UNIT (vtable_entry_type)));
6184 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
6188 case ptrmemfunc_vbit_in_delta:
6189 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
6190 *delta, integer_one_node));
6191 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6192 *delta, integer_one_node));
6199 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
6204 /* Return an expression for PFN from the pointer-to-member function
6208 pfn_from_ptrmemfunc (t)
6211 if (TREE_CODE (t) == PTRMEM_CST)
6216 expand_ptrmemfunc_cst (t, &delta, &pfn);
6221 return build_component_ref (t, pfn_identifier, NULL_TREE, 0);
6224 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6225 if this is a potentially dangerous thing to do. Returns a possibly
6229 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
6232 const char *errtype;
6236 if (TREE_CODE (type) == REFERENCE_TYPE)
6237 type = TREE_TYPE (type);
6239 /* Issue warnings about peculiar, but legal, uses of NULL. */
6240 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
6243 cp_warning ("passing NULL used for non-pointer %s %P of `%D'",
6244 errtype, parmnum, fndecl);
6246 cp_warning ("%s to non-pointer type `%T' from NULL", errtype, type);
6249 /* Warn about assigning a floating-point type to an integer type. */
6250 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
6251 && TREE_CODE (type) == INTEGER_TYPE)
6254 cp_warning ("passing `%T' for %s %P of `%D'",
6255 TREE_TYPE (expr), errtype, parmnum, fndecl);
6257 cp_warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
6259 /* And warn about assigning a negative value to an unsigned
6261 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
6263 if (TREE_CODE (expr) == INTEGER_CST
6264 && TREE_NEGATED_INT (expr))
6267 cp_warning ("passing negative value `%E' for %s %P of `%D'",
6268 expr, errtype, parmnum, fndecl);
6270 cp_warning ("%s of negative value `%E' to `%T'",
6271 errtype, expr, type);
6274 overflow_warning (expr);
6276 if (TREE_CONSTANT (expr))
6282 /* Convert value RHS to type TYPE as preparation for an assignment to
6283 an lvalue of type TYPE. ERRTYPE is a string to use in error
6284 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6285 are doing the conversion in order to pass the PARMNUMth argument of
6289 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6291 const char *errtype;
6295 register enum tree_code codel = TREE_CODE (type);
6296 register tree rhstype;
6297 register enum tree_code coder;
6299 if (codel == OFFSET_TYPE)
6300 my_friendly_abort (990505);
6302 if (TREE_CODE (rhs) == OFFSET_REF)
6303 rhs = resolve_offset_ref (rhs);
6305 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6306 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6307 rhs = TREE_OPERAND (rhs, 0);
6309 rhstype = TREE_TYPE (rhs);
6310 coder = TREE_CODE (rhstype);
6312 if (rhs == error_mark_node || rhstype == error_mark_node)
6313 return error_mark_node;
6314 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6315 return error_mark_node;
6317 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6319 /* The RHS of an assignment cannot have void type. */
6320 if (coder == VOID_TYPE)
6322 error ("void value not ignored as it ought to be");
6323 return error_mark_node;
6326 /* Simplify the RHS if possible. */
6327 if (TREE_CODE (rhs) == CONST_DECL)
6328 rhs = DECL_INITIAL (rhs);
6329 else if (coder != ARRAY_TYPE)
6330 rhs = decl_constant_value (rhs);
6334 The expression is implicitly converted (clause _conv_) to the
6335 cv-unqualified type of the left operand. */
6336 if (!can_convert_arg (type, rhstype, rhs))
6338 /* When -Wno-pmf-conversions is use, we just silently allow
6339 conversions from pointers-to-members to plain pointers. If
6340 the conversion doesn't work, cp_convert will complain. */
6342 && TYPE_PTR_P (type)
6343 && TYPE_PTRMEMFUNC_P (rhstype))
6344 rhs = cp_convert (strip_top_quals (type), rhs);
6347 /* If the right-hand side has unknown type, then it is an
6348 overloaded function. Call instantiate_type to get error
6350 if (rhstype == unknown_type_node)
6351 instantiate_type (type, rhs, itf_complain);
6353 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6354 rhstype, type, parmnum, fndecl);
6356 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6358 return error_mark_node;
6361 return perform_implicit_conversion (strip_top_quals (type), rhs);
6364 /* Convert RHS to be of type TYPE.
6365 If EXP is non-zero, it is the target of the initialization.
6366 ERRTYPE is a string to use in error messages.
6368 Two major differences between the behavior of
6369 `convert_for_assignment' and `convert_for_initialization'
6370 are that references are bashed in the former, while
6371 copied in the latter, and aggregates are assigned in
6372 the former (operator=) while initialized in the
6375 If using constructor make sure no conversion operator exists, if one does
6376 exist, an ambiguity exists.
6378 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6381 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6382 tree exp, type, rhs;
6384 const char *errtype;
6388 register enum tree_code codel = TREE_CODE (type);
6389 register tree rhstype;
6390 register enum tree_code coder;
6392 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6393 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6394 if (TREE_CODE (rhs) == NOP_EXPR
6395 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6396 && codel != REFERENCE_TYPE)
6397 rhs = TREE_OPERAND (rhs, 0);
6399 if (rhs == error_mark_node
6400 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6401 return error_mark_node;
6403 if (TREE_CODE (rhs) == OFFSET_REF)
6405 rhs = resolve_offset_ref (rhs);
6406 if (rhs == error_mark_node)
6407 return error_mark_node;
6410 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6411 rhs = convert_from_reference (rhs);
6413 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6414 && TREE_CODE (type) != ARRAY_TYPE
6415 && (TREE_CODE (type) != REFERENCE_TYPE
6416 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6417 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6418 && (TREE_CODE (type) != REFERENCE_TYPE
6419 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6420 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6421 rhs = default_conversion (rhs);
6423 rhstype = TREE_TYPE (rhs);
6424 coder = TREE_CODE (rhstype);
6426 if (coder == ERROR_MARK)
6427 return error_mark_node;
6429 /* We accept references to incomplete types, so we can
6430 return here before checking if RHS is of complete type. */
6432 if (codel == REFERENCE_TYPE)
6434 /* This should eventually happen in convert_arguments. */
6435 int savew = 0, savee = 0;
6438 savew = warningcount, savee = errorcount;
6439 rhs = initialize_reference (type, rhs);
6442 if (warningcount > savew)
6443 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6444 else if (errorcount > savee)
6445 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6451 exp = require_complete_type (exp);
6452 if (exp == error_mark_node)
6453 return error_mark_node;
6455 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6456 rhstype = TREE_TYPE (rhstype);
6458 type = complete_type (type);
6460 if (IS_AGGR_TYPE (type))
6461 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6463 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6466 /* Expand an ASM statement with operands, handling output operands
6467 that are not variables or INDIRECT_REFS by transforming such
6468 cases into cases that expand_asm_operands can handle.
6470 Arguments are same as for expand_asm_operands.
6472 We don't do default conversions on all inputs, because it can screw
6473 up operands that are expected to be in memory. */
6476 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6477 tree string, outputs, inputs, clobbers;
6479 const char *filename;
6482 int noutputs = list_length (outputs);
6484 /* o[I] is the place that output number I should be written. */
6485 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6488 /* Record the contents of OUTPUTS before it is modified. */
6489 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6490 o[i] = TREE_VALUE (tail);
6492 /* Generate the ASM_OPERANDS insn;
6493 store into the TREE_VALUEs of OUTPUTS some trees for
6494 where the values were actually stored. */
6495 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6497 /* Copy all the intermediate outputs into the specified outputs. */
6498 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6500 if (o[i] != TREE_VALUE (tail))
6502 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6503 const0_rtx, VOIDmode, EXPAND_NORMAL);
6506 /* Restore the original value so that it's correct the next
6507 time we expand this function. */
6508 TREE_VALUE (tail) = o[i];
6510 /* Detect modification of read-only values.
6511 (Otherwise done by build_modify_expr.) */
6514 tree type = TREE_TYPE (o[i]);
6515 if (CP_TYPE_CONST_P (type)
6516 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6517 && C_TYPE_FIELDS_READONLY (type)))
6518 readonly_error (o[i], "modification by `asm'", 1);
6522 /* Those MODIFY_EXPRs could do autoincrements. */
6526 /* If RETVAL is the address of, or a reference to, a local variable or
6527 temporary give an appropraite warning. */
6530 maybe_warn_about_returning_address_of_local (retval)
6533 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6534 tree whats_returned = retval;
6538 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6539 whats_returned = TREE_OPERAND (whats_returned, 1);
6540 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6541 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6542 || TREE_CODE (whats_returned) == NOP_EXPR)
6543 whats_returned = TREE_OPERAND (whats_returned, 0);
6548 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6550 whats_returned = TREE_OPERAND (whats_returned, 0);
6552 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6554 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6555 || TREE_CODE (whats_returned) == TARGET_EXPR)
6557 /* Get the target. */
6558 whats_returned = TREE_OPERAND (whats_returned, 0);
6559 warning ("returning reference to temporary");
6562 if (TREE_CODE (whats_returned) == VAR_DECL
6563 && DECL_NAME (whats_returned)
6564 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6566 warning ("reference to non-lvalue returned");
6571 if (TREE_CODE (whats_returned) == VAR_DECL
6572 && DECL_NAME (whats_returned)
6573 && DECL_FUNCTION_SCOPE_P (whats_returned)
6574 && !(TREE_STATIC (whats_returned)
6575 || TREE_PUBLIC (whats_returned)))
6577 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6578 cp_warning_at ("reference to local variable `%D' returned",
6581 cp_warning_at ("address of local variable `%D' returned",
6587 /* Check that returning RETVAL from the current function is legal.
6588 Return an expression explicitly showing all conversions required to
6589 change RETVAL into the function return type, and to assign it to
6590 the DECL_RESULT for the function. */
6593 check_return_expr (retval)
6597 /* The type actually returned by the function, after any
6600 int fn_returns_value_p;
6602 /* A `volatile' function is one that isn't supposed to return, ever.
6603 (This is a G++ extension, used to get better code for functions
6604 that call the `volatile' function.) */
6605 if (TREE_THIS_VOLATILE (current_function_decl))
6606 warning ("function declared `noreturn' has a `return' statement");
6608 /* Check for various simple errors. */
6612 error ("returning a value from a destructor");
6615 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6617 if (in_function_try_handler)
6618 /* If a return statement appears in a handler of the
6619 function-try-block of a constructor, the program is ill-formed. */
6620 error ("cannot return from a handler of a function-try-block of a constructor");
6622 /* You can't return a value from a constructor. */
6623 error ("returning a value from a constructor");
6627 /* When no explicit return-value is given in a function with a named
6628 return value, the named return value is used. */
6629 result = DECL_RESULT (current_function_decl);
6630 valtype = TREE_TYPE (result);
6631 my_friendly_assert (valtype != NULL_TREE, 19990924);
6632 fn_returns_value_p = !VOID_TYPE_P (valtype);
6633 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6636 /* Check for a return statement with no return value in a function
6637 that's supposed to return a value. */
6638 if (!retval && fn_returns_value_p)
6640 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6641 /* Clear this, so finish_function won't say that we reach the
6642 end of a non-void function (which we don't, we gave a
6644 current_function_returns_null = 0;
6646 /* Check for a return statement with a value in a function that
6647 isn't supposed to return a value. */
6648 else if (retval && !fn_returns_value_p)
6650 if (VOID_TYPE_P (TREE_TYPE (retval)))
6651 /* You can return a `void' value from a function of `void'
6652 type. In that case, we have to evaluate the expression for
6653 its side-effects. */
6654 finish_expr_stmt (retval);
6656 pedwarn ("return-statement with a value, in function declared with a void return type");
6658 current_function_returns_null = 1;
6660 /* There's really no value to return, after all. */
6664 /* Remember that this function can sometimes return without a
6666 current_function_returns_null = 1;
6668 /* Remember that this function did return a value. */
6669 current_function_returns_value = 1;
6671 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6672 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6673 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6674 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6676 && null_ptr_cst_p (retval))
6677 cp_warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6679 /* Effective C++ rule 15. See also start_function. */
6681 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6682 && retval != current_class_ref)
6683 cp_warning ("`operator=' should return a reference to `*this'");
6685 /* We don't need to do any conversions when there's nothing being
6687 if (!retval || retval == error_mark_node)
6690 /* Do any required conversions. */
6691 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6692 /* No conversions are required. */
6696 /* The type the function is declared to return. */
6697 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6699 /* First convert the value to the function's return type, then
6700 to the type of return value's location to handle the
6701 case that functype is smaller than the valtype. */
6702 retval = convert_for_initialization
6703 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6704 "return", NULL_TREE, 0);
6705 retval = convert (valtype, retval);
6707 /* If the conversion failed, treat this just like `return;'. */
6708 if (retval == error_mark_node)
6710 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6711 else if (! current_function_returns_struct
6712 && TREE_CODE (retval) == TARGET_EXPR
6713 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6714 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6715 TREE_OPERAND (retval, 0));
6717 maybe_warn_about_returning_address_of_local (retval);
6720 /* Actually copy the value returned into the appropriate location. */
6721 if (retval && retval != result)
6722 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6728 /* Returns non-zero if the pointer-type FROM can be converted to the
6729 pointer-type TO via a qualification conversion. If CONSTP is -1,
6730 then we return non-zero if the pointers are similar, and the
6731 cv-qualification signature of FROM is a proper subset of that of TO.
6733 If CONSTP is positive, then all outer pointers have been
6737 comp_ptr_ttypes_real (to, from, constp)
6741 int to_more_cv_qualified = 0;
6743 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6745 if (TREE_CODE (to) != TREE_CODE (from))
6748 if (TREE_CODE (from) == OFFSET_TYPE
6749 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6750 TYPE_OFFSET_BASETYPE (to)))
6753 /* Const and volatile mean something different for function types,
6754 so the usual checks are not appropriate. */
6755 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6757 if (!at_least_as_qualified_p (to, from))
6760 if (!at_least_as_qualified_p (from, to))
6765 ++to_more_cv_qualified;
6769 constp &= TYPE_READONLY (to);
6772 if (TREE_CODE (to) != POINTER_TYPE)
6774 same_type_ignoring_top_level_qualifiers_p (to, from)
6775 && (constp >= 0 || to_more_cv_qualified);
6779 /* When comparing, say, char ** to char const **, this function takes the
6780 'char *' and 'char const *'. Do not pass non-pointer types to this
6784 comp_ptr_ttypes (to, from)
6787 return comp_ptr_ttypes_real (to, from, 1);
6790 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6791 type or inheritance-related types, regardless of cv-quals. */
6794 ptr_reasonably_similar (to, from)
6797 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6799 if (TREE_CODE (to) != TREE_CODE (from))
6802 if (TREE_CODE (from) == OFFSET_TYPE
6803 && comptypes (TYPE_OFFSET_BASETYPE (to),
6804 TYPE_OFFSET_BASETYPE (from),
6805 COMPARE_BASE | COMPARE_RELAXED))
6808 if (TREE_CODE (to) != POINTER_TYPE)
6810 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6811 COMPARE_BASE | COMPARE_RELAXED);
6815 /* Like comp_ptr_ttypes, for const_cast. */
6818 comp_ptr_ttypes_const (to, from)
6821 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6823 if (TREE_CODE (to) != TREE_CODE (from))
6826 if (TREE_CODE (from) == OFFSET_TYPE
6827 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6828 TYPE_OFFSET_BASETYPE (to)))
6831 if (TREE_CODE (to) != POINTER_TYPE)
6832 return same_type_ignoring_top_level_qualifiers_p (to, from);
6836 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6839 comp_ptr_ttypes_reinterpret (to, from)
6844 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6846 if (TREE_CODE (from) == OFFSET_TYPE)
6847 from = TREE_TYPE (from);
6848 if (TREE_CODE (to) == OFFSET_TYPE)
6849 to = TREE_TYPE (to);
6851 /* Const and volatile mean something different for function types,
6852 so the usual checks are not appropriate. */
6853 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6854 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6856 if (!at_least_as_qualified_p (to, from))
6860 && !at_least_as_qualified_p (from, to))
6862 constp &= TYPE_READONLY (to);
6865 if (TREE_CODE (from) != POINTER_TYPE
6866 || TREE_CODE (to) != POINTER_TYPE)
6871 /* Returns the type-qualifier set corresponding to TYPE. */
6874 cp_type_quals (type)
6877 type = strip_array_types (type);
6878 return TYPE_QUALS (type);
6881 /* Returns non-zero if the TYPE contains a mutable member */
6884 cp_has_mutable_p (type)
6887 type = strip_array_types (type);
6889 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6892 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6893 exemplar types such that casting T1 to T2 is casting away castness
6894 if and only if there is no implicit conversion from T1 to T2. */
6897 casts_away_constness_r (t1, t2)
6904 /* [expr.const.cast]
6906 For multi-level pointer to members and multi-level mixed pointers
6907 and pointers to members (conv.qual), the "member" aspect of a
6908 pointer to member level is ignored when determining if a const
6909 cv-qualifier has been cast away. */
6910 if (TYPE_PTRMEM_P (*t1))
6911 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6912 if (TYPE_PTRMEM_P (*t2))
6913 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6915 /* [expr.const.cast]
6917 For two pointer types:
6919 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6920 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6923 casting from X1 to X2 casts away constness if, for a non-pointer
6924 type T there does not exist an implicit conversion (clause
6927 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6931 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6933 if (TREE_CODE (*t1) != POINTER_TYPE
6934 || TREE_CODE (*t2) != POINTER_TYPE)
6936 *t1 = cp_build_qualified_type (void_type_node,
6937 CP_TYPE_QUALS (*t1));
6938 *t2 = cp_build_qualified_type (void_type_node,
6939 CP_TYPE_QUALS (*t2));
6943 quals1 = CP_TYPE_QUALS (*t1);
6944 quals2 = CP_TYPE_QUALS (*t2);
6945 *t1 = TREE_TYPE (*t1);
6946 *t2 = TREE_TYPE (*t2);
6947 casts_away_constness_r (t1, t2);
6948 *t1 = build_pointer_type (*t1);
6949 *t2 = build_pointer_type (*t2);
6950 *t1 = cp_build_qualified_type (*t1, quals1);
6951 *t2 = cp_build_qualified_type (*t2, quals2);
6954 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
6958 casts_away_constness (t1, t2)
6962 if (TREE_CODE (t2) == REFERENCE_TYPE)
6964 /* [expr.const.cast]
6966 Casting from an lvalue of type T1 to an lvalue of type T2
6967 using a reference cast casts away constness if a cast from an
6968 rvalue of type "pointer to T1" to the type "pointer to T2"
6969 casts away constness. */
6970 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
6971 ? TREE_TYPE (t1) : t1);
6972 return casts_away_constness (build_pointer_type (t1),
6973 build_pointer_type (TREE_TYPE (t2)));
6976 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6977 /* [expr.const.cast]
6979 Casting from an rvalue of type "pointer to data member of X
6980 of type T1" to the type "pointer to data member of Y of type
6981 T2" casts away constness if a cast from an rvalue of type
6982 "poitner to T1" to the type "pointer to T2" casts away
6984 return casts_away_constness
6985 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
6986 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
6988 /* Casting away constness is only something that makes sense for
6989 pointer or reference types. */
6990 if (TREE_CODE (t1) != POINTER_TYPE
6991 || TREE_CODE (t2) != POINTER_TYPE)
6994 /* Top-level qualifiers don't matter. */
6995 t1 = TYPE_MAIN_VARIANT (t1);
6996 t2 = TYPE_MAIN_VARIANT (t2);
6997 casts_away_constness_r (&t1, &t2);
6998 if (!can_convert (t2, t1))
7004 /* Returns TYPE with its cv qualifiers removed
7005 TYPE is T cv* .. *cv where T is not a pointer type,
7006 returns T * .. *. (If T is an array type, then the cv qualifiers
7007 above are those of the array members.) */
7010 strip_all_pointer_quals (type)
7013 if (TREE_CODE (type) == POINTER_TYPE)
7014 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
7015 else if (TREE_CODE (type) == OFFSET_TYPE)
7016 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
7017 strip_all_pointer_quals (TREE_TYPE (type)));
7019 return TYPE_MAIN_VARIANT (type);