1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
43 static tree convert_for_assignment PROTO((tree, tree, const char *, tree,
45 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
46 static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
47 static int comp_target_parms PROTO((tree, tree, int));
48 static int comp_ptr_ttypes_real PROTO((tree, tree, int));
49 static int comp_ptr_ttypes_const PROTO((tree, tree));
50 static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
51 static int comp_except_types PROTO((tree, tree, int));
52 static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
54 static tree common_base_type PROTO((tree, tree));
56 static tree convert_sequence PROTO((tree, tree));
58 static tree lookup_anon_field PROTO((tree, tree));
59 static tree pointer_diff PROTO((tree, tree, tree));
60 static tree build_component_addr PROTO((tree, tree));
61 static tree qualify_type PROTO((tree, tree));
62 static tree get_delta_difference PROTO((tree, tree, int));
63 static int comp_cv_target_types PROTO((tree, tree, int));
64 static void casts_away_constness_r PROTO((tree *, tree *));
65 static int casts_away_constness PROTO ((tree, tree));
67 /* Return the target type of TYPE, which means return T for:
68 T*, T&, T[], T (...), and otherwise, just T. */
74 if (TREE_CODE (type) == REFERENCE_TYPE)
75 type = TREE_TYPE (type);
76 while (TREE_CODE (type) == POINTER_TYPE
77 || TREE_CODE (type) == ARRAY_TYPE
78 || TREE_CODE (type) == FUNCTION_TYPE
79 || TREE_CODE (type) == METHOD_TYPE
80 || TREE_CODE (type) == OFFSET_TYPE)
81 type = TREE_TYPE (type);
85 /* Do `exp = require_complete_type (exp);' to make sure exp
86 does not have an incomplete type. (That includes void types.)
87 Returns the error_mark_node if the VALUE does not have
88 complete type when this function returns. */
91 require_complete_type (value)
96 if (processing_template_decl || value == error_mark_node)
99 if (TREE_CODE (value) == OVERLOAD)
100 type = unknown_type_node;
102 type = TREE_TYPE (value);
104 /* First, detect a valid value with a complete type. */
105 if (TYPE_SIZE (type) != 0
106 && TYPE_SIZE (type) != size_zero_node)
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_PTR), 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 || TYPE_SIZE (type) != NULL_TREE)
147 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
149 tree t = complete_type (TREE_TYPE (type));
150 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
152 TYPE_NEEDS_CONSTRUCTING (type)
153 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
154 TYPE_NEEDS_DESTRUCTOR (type)
155 = TYPE_NEEDS_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 (!TYPE_SIZE (type) || TYPE_SIZE (type) == size_zero_node)
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 truthvalue of whether T is function (or pfn) type. */
204 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
205 || (TREE_CODE (t) == POINTER_TYPE
206 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
207 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
210 /* Return a variant of TYPE which has all the type qualifiers of LIKE
211 as well as those of TYPE. */
214 qualify_type (type, like)
217 /* @@ Must do member pointers here. */
218 return cp_build_qualified_type (type, (CP_TYPE_QUALS (type)
219 | CP_TYPE_QUALS (like)));
222 /* Return the common type of two parameter lists.
223 We assume that comptypes has already been done and returned 1;
224 if that isn't so, this may crash.
226 As an optimization, free the space we allocate if the parameter
227 lists are already common. */
233 tree oldargs = p1, newargs, n;
236 char *first_obj = (char *) oballoc (0);
238 len = list_length (p1);
239 newargs = tree_last (p1);
241 if (newargs == void_list_node)
250 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
255 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
257 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
259 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
262 else if (! TREE_PURPOSE (p1))
264 if (TREE_PURPOSE (p2))
266 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
272 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
274 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
276 if (TREE_VALUE (p1) != TREE_VALUE (p2))
279 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
282 TREE_VALUE (n) = TREE_VALUE (p1);
293 /* Given a type, perhaps copied for a typedef,
294 find the "original" version of it. */
299 while (TYPE_NAME (t) != NULL_TREE)
301 tree x = TYPE_NAME (t);
302 if (TREE_CODE (x) != TYPE_DECL)
304 x = DECL_ORIGINAL_TYPE (x);
312 /* T1 and T2 are arithmetic or enumeration types. Return the type
313 that will result from the "usual arithmetic converions" on T1 and
314 T2 as described in [expr]. */
317 type_after_usual_arithmetic_conversions (t1, t2)
321 enum tree_code code1 = TREE_CODE (t1);
322 enum tree_code code2 = TREE_CODE (t2);
325 /* FIXME: Attributes. */
326 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
327 || TREE_CODE (t1) == ENUMERAL_TYPE,
329 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
330 || TREE_CODE (t2) == ENUMERAL_TYPE,
333 /* In what follows, we slightly generalize the rules given in [expr]
334 so as to deal with `long long'. First, merge the attributes. */
335 attributes = merge_machine_type_attributes (t1, t2);
337 /* If only one is real, use it as the result. */
338 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
339 return build_type_attribute_variant (t1, attributes);
340 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
341 return build_type_attribute_variant (t2, attributes);
343 /* Perform the integral promotions. */
344 if (code1 != REAL_TYPE)
346 t1 = type_promotes_to (t1);
347 t2 = type_promotes_to (t2);
350 /* Both real or both integers; use the one with greater precision. */
351 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
352 return build_type_attribute_variant (t1, attributes);
353 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
354 return build_type_attribute_variant (t2, attributes);
356 if (code1 != REAL_TYPE)
358 /* If one is unsigned long long, then convert the other to unsigned
360 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
361 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
362 return build_type_attribute_variant (long_long_unsigned_type_node,
364 /* If one is a long long, and the other is an unsigned long, and
365 long long can represent all the values of an unsigned long, then
366 convert to a long long. Otherwise, convert to an unsigned long
367 long. Otherwise, if either operand is long long, convert the
370 Since we're here, we know the TYPE_PRECISION is the same;
371 therefore converting to long long cannot represent all the values
372 of an unsigned long, so we choose unsigned long long in that
374 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
375 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
377 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
378 ? long_long_unsigned_type_node
379 : long_long_integer_type_node);
380 return build_type_attribute_variant (t, attributes);
383 /* Go through the same procedure, but for longs. */
384 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
386 return build_type_attribute_variant (long_unsigned_type_node,
388 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
389 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
391 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
392 ? long_unsigned_type_node : long_integer_type_node);
393 return build_type_attribute_variant (t, attributes);
395 /* Otherwise prefer the unsigned one. */
396 if (TREE_UNSIGNED (t1))
397 return build_type_attribute_variant (t1, attributes);
399 return build_type_attribute_variant (t2, attributes);
403 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
405 return build_type_attribute_variant (long_double_type_node,
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
409 return build_type_attribute_variant (double_type_node,
412 return build_type_attribute_variant (float_type_node,
417 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
418 ARG1 and ARG2 are the values with those types. The LOCATION is a
419 string describing the current location, in case an error occurs. */
422 composite_pointer_type (t1, t2, arg1, arg2, location)
427 const char* location;
433 If one operand is a null pointer constant, the composite pointer
434 type is the type of the other operand. */
435 if (null_ptr_cst_p (arg1))
437 if (null_ptr_cst_p (arg2))
440 /* Deal with pointer-to-member functions in the same way as we deal
441 with pointers to functions. */
442 if (TYPE_PTRMEMFUNC_P (t1))
443 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
444 if (TYPE_PTRMEMFUNC_P (t2))
445 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
447 if (comp_target_types (t1, t2, 1))
448 result_type = common_type (t1, t2);
449 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t1)) == void_type_node)
451 if (pedantic && TREE_CODE (t2) == FUNCTION_TYPE)
452 pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
454 result_type = qualify_type (t1, t2);
456 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t2)) == void_type_node)
458 if (pedantic && TREE_CODE (t1) == FUNCTION_TYPE)
459 pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
461 result_type = qualify_type (t2, t1);
464 else if (same_or_base_type_p (t2, t1))
466 else if (IS_AGGR_TYPE (TREE_TYPE (t1))
467 && IS_AGGR_TYPE (TREE_TYPE (t2))
468 && (result_type = common_base_type (TREE_TYPE (t1),
471 if (result_type == error_mark_node)
473 cp_error ("common base type of types `%T' and `%T' is ambiguous",
474 TREE_TYPE (t1), TREE_TYPE (t2));
475 result_type = ptr_type_node;
480 && result_type != TREE_TYPE (t1)
481 && result_type != TREE_TYPE (t2))
482 cp_pedwarn ("`%T' and `%T' converted to `%T *' in %s",
483 t1, t2, result_type, location);
485 result_type = build_pointer_type (result_type);
490 cp_pedwarn ("pointer type mismatch in %s", location);
491 result_type = ptr_type_node;
497 /* Return the common type of two types.
498 We assume that comptypes has already been done and returned 1;
499 if that isn't so, this may crash.
501 This is the type for the result of most arithmetic operations
502 if the operands have the given two types.
504 We do not deal with enumeral types here because they have already been
505 converted to integer types. */
511 register enum tree_code code1;
512 register enum tree_code code2;
515 /* Save time if the two types are the same. */
518 t1 = original_type (t1);
519 t2 = original_type (t2);
523 /* If one type is nonsense, use the other. */
524 if (t1 == error_mark_node)
526 if (t2 == error_mark_node)
529 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
530 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
531 return type_after_usual_arithmetic_conversions (t1, t2);
533 /* Merge the attributes. */
534 attributes = merge_machine_type_attributes (t1, t2);
536 /* Treat an enum type as the unsigned integer type of the same width. */
538 if (TREE_CODE (t1) == ENUMERAL_TYPE)
539 t1 = type_for_size (TYPE_PRECISION (t1), 1);
540 if (TREE_CODE (t2) == ENUMERAL_TYPE)
541 t2 = type_for_size (TYPE_PRECISION (t2), 1);
543 if (TYPE_PTRMEMFUNC_P (t1))
544 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
545 if (TYPE_PTRMEMFUNC_P (t2))
546 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
548 code1 = TREE_CODE (t1);
549 code2 = TREE_CODE (t2);
551 /* If one type is complex, form the common type of the non-complex
552 components, then make that complex. Use T1 or T2 if it is the
554 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
556 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
557 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
558 tree subtype = common_type (subtype1, subtype2);
560 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
561 return build_type_attribute_variant (t1, attributes);
562 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
563 return build_type_attribute_variant (t2, attributes);
565 return build_type_attribute_variant (build_complex_type (subtype),
573 /* We should have called type_after_usual_arithmetic_conversions
575 my_friendly_abort (19990725);
580 /* For two pointers, do this recursively on the target type,
581 and combine the qualifiers of the two types' targets. */
582 /* This code was turned off; I don't know why.
583 But ANSI C++ specifies doing this with the qualifiers.
584 So I turned it on again. */
586 tree tt1 = TREE_TYPE (t1);
587 tree tt2 = TREE_TYPE (t2);
592 if (TREE_CODE (tt1) == OFFSET_TYPE)
594 b1 = TYPE_OFFSET_BASETYPE (tt1);
595 b2 = TYPE_OFFSET_BASETYPE (tt2);
596 tt1 = TREE_TYPE (tt1);
597 tt2 = TREE_TYPE (tt2);
602 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
603 tt1 = TYPE_MAIN_VARIANT (tt1);
604 tt2 = TYPE_MAIN_VARIANT (tt2);
608 else if (tt1 == void_type_node || tt2 == void_type_node)
609 target = void_type_node;
610 else if (tt1 == unknown_type_node)
612 else if (tt2 == unknown_type_node)
615 target = common_type (tt1, tt2);
617 target = cp_build_qualified_type (target, type_quals);
621 if (same_type_p (b1, b2)
622 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
623 target = build_offset_type (b2, target);
624 else if (binfo_or_else (b2, b1))
625 target = build_offset_type (b1, target);
628 if (code1 == POINTER_TYPE)
629 t1 = build_pointer_type (target);
631 t1 = build_reference_type (target);
632 t1 = build_type_attribute_variant (t1, attributes);
634 if (TREE_CODE (target) == METHOD_TYPE)
635 t1 = build_ptrmemfunc_type (t1);
642 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
643 /* Save space: see if the result is identical to one of the args. */
644 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
645 return build_type_attribute_variant (t1, attributes);
646 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
647 return build_type_attribute_variant (t2, attributes);
648 /* Merge the element types, and have a size if either arg has one. */
649 t1 = build_cplus_array_type
650 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
651 return build_type_attribute_variant (t1, attributes);
655 /* Function types: prefer the one that specified arg types.
656 If both do, merge the arg types. Also merge the return types. */
658 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
659 tree p1 = TYPE_ARG_TYPES (t1);
660 tree p2 = TYPE_ARG_TYPES (t2);
663 /* Save space: see if the result is identical to one of the args. */
664 if (valtype == TREE_TYPE (t1) && ! p2)
665 return build_type_attribute_variant (t1, attributes);
666 if (valtype == TREE_TYPE (t2) && ! p1)
667 return build_type_attribute_variant (t2, attributes);
669 /* Simple way if one arg fails to specify argument types. */
670 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
672 rval = build_function_type (valtype, p2);
673 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
674 rval = build_exception_variant (rval, raises);
675 return build_type_attribute_variant (rval, attributes);
677 raises = TYPE_RAISES_EXCEPTIONS (t1);
678 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
680 rval = build_function_type (valtype, p1);
682 rval = build_exception_variant (rval, raises);
683 return build_type_attribute_variant (rval, attributes);
686 rval = build_function_type (valtype, commonparms (p1, p2));
687 rval = build_exception_variant (rval, raises);
688 return build_type_attribute_variant (rval, attributes);
693 t1 = TYPE_MAIN_VARIANT (t1);
694 t2 = TYPE_MAIN_VARIANT (t2);
696 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
697 return build_type_attribute_variant (t1, attributes);
698 else if (binfo_or_else (t2, t1))
699 return build_type_attribute_variant (t2, attributes);
702 compiler_error ("common_type called with uncommon aggregate types");
703 return error_mark_node;
707 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
709 /* Get this value the long way, since TYPE_METHOD_BASETYPE
710 is just the main variant of this. */
714 tree b1 = TYPE_OFFSET_BASETYPE (t1);
715 tree b2 = TYPE_OFFSET_BASETYPE (t2);
717 if (same_type_p (b1, b2)
718 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
719 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
722 if (binfo_or_else (b2, b1) == NULL_TREE)
723 compiler_error ("common_type called with uncommon method types");
724 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
727 raises = TYPE_RAISES_EXCEPTIONS (t1);
729 /* If this was a member function type, get back to the
730 original type of type member function (i.e., without
731 the class instance variable up front. */
732 t1 = build_function_type (TREE_TYPE (t1),
733 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
734 t2 = build_function_type (TREE_TYPE (t2),
735 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
736 t3 = common_type (t1, t2);
737 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
738 TYPE_ARG_TYPES (t3));
739 t1 = build_exception_variant (t3, raises);
742 compiler_error ("common_type called with uncommon method types");
744 return build_type_attribute_variant (t1, attributes);
747 /* Pointers to members should now be handled by the POINTER_TYPE
749 my_friendly_abort (990325);
752 return build_type_attribute_variant (t1, attributes);
756 /* Compare two exception specifier types for exactness or subsetness, if
757 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
759 [except.spec] "If a class X ... objects of class X or any class publicly
760 and unambigously derrived from X. Similarly, if a pointer type Y * ...
761 exceptions of type Y * or that are pointers to any type publicly and
762 unambigously derrived from Y. Otherwise a function only allows exceptions
763 that have the same type ..."
764 This does not mention cv qualifiers and is different to what throw
765 [except.throw] and catch [except.catch] will do. They will ignore the
766 top level cv qualifiers, and allow qualifiers in the pointer to class
769 We implement the letter of the standard. */
772 comp_except_types (a, b, exact)
776 if (same_type_p (a, b))
780 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
783 if (TREE_CODE (a) == POINTER_TYPE
784 && TREE_CODE (b) == POINTER_TYPE)
788 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
792 if (TREE_CODE (a) != RECORD_TYPE
793 || TREE_CODE (b) != RECORD_TYPE)
796 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
802 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
803 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
804 otherwise it must be exact. Exception lists are unordered, but
805 we've already filtered out duplicates. Most lists will be in order,
806 we should try to make use of that. */
809 comp_except_specs (t1, t2, exact)
820 if (t1 == NULL_TREE) /* T1 is ... */
821 return t2 == NULL_TREE || !exact;
822 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
823 return t2 != NULL_TREE && !TREE_VALUE (t2);
824 if (t2 == NULL_TREE) /* T2 is ... */
826 if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
829 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
830 Count how many we find, to determine exactness. For exact matching and
831 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
833 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
835 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
837 tree a = TREE_VALUE (probe);
838 tree b = TREE_VALUE (t2);
840 if (comp_except_types (a, b, exact))
842 if (probe == base && exact)
843 base = TREE_CHAIN (probe);
848 if (probe == NULL_TREE)
851 return !exact || base == NULL_TREE || length == list_length (t1);
854 /* Compare the array types T1 and T2, using CMP as the type comparison
855 function for the element types. STRICT is as for comptypes. */
858 comp_array_types (cmp, t1, t2, strict)
859 register int (*cmp) PROTO((tree, tree, int));
869 /* The type of the array elements must be the same. */
870 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
871 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
872 strict & ~COMPARE_REDECLARATION)))
875 d1 = TYPE_DOMAIN (t1);
876 d2 = TYPE_DOMAIN (t2);
881 /* If one of the arrays is dimensionless, and the other has a
882 dimension, they are of different types. However, it is legal to
890 declarations for an array object can specify
891 array types that differ by the presence or absence of a major
892 array bound (_dcl.array_). */
894 return strict & COMPARE_REDECLARATION;
896 /* Check that the dimensions are the same. */
897 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
899 && cp_tree_equal (TYPE_MAX_VALUE (d1),
900 TYPE_MAX_VALUE (d2)));
903 /* Return 1 if T1 and T2 are compatible types for assignment or
904 various other operations. STRICT is a bitwise-or of the COMPARE_*
908 comptypes (t1, t2, strict)
914 int orig_strict = strict;
916 /* The special exemption for redeclaring array types without an
917 array bound only applies at the top level:
922 is not legal, for example. */
923 strict &= ~COMPARE_REDECLARATION;
925 /* Suppress errors caused by previously reported errors */
929 /* This should never happen. */
930 my_friendly_assert (t1 != error_mark_node, 307);
932 if (t2 == error_mark_node)
935 if (strict & COMPARE_RELAXED)
937 /* Treat an enum type as the unsigned integer type of the same width. */
939 if (TREE_CODE (t1) == ENUMERAL_TYPE)
940 t1 = type_for_size (TYPE_PRECISION (t1), 1);
941 if (TREE_CODE (t2) == ENUMERAL_TYPE)
942 t2 = type_for_size (TYPE_PRECISION (t2), 1);
948 if (TYPE_PTRMEMFUNC_P (t1))
949 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
950 if (TYPE_PTRMEMFUNC_P (t2))
951 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
953 /* Different classes of types can't be compatible. */
954 if (TREE_CODE (t1) != TREE_CODE (t2))
957 /* Qualifiers must match. */
958 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
960 if (strict == COMPARE_STRICT
961 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
964 /* Allow for two different type nodes which have essentially the same
965 definition. Note that we already checked for equality of the type
966 qualifiers (just above). */
968 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
971 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
972 attribute is its own main variant (`val' will remain 0). */
973 #ifndef COMP_TYPE_ATTRIBUTES
974 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
977 if (strict & COMPARE_NO_ATTRIBUTES)
979 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
980 else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
983 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
986 switch (TREE_CODE (t1))
988 case TEMPLATE_TEMPLATE_PARM:
989 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
990 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
992 if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1)),
993 DECL_TEMPLATE_PARMS (TYPE_NAME (t2))))
995 if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1)
996 && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2))
998 /* Don't check inheritance. */
999 strict = COMPARE_STRICT;
1004 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1005 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1006 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
1007 val = comp_template_args (TYPE_TI_ARGS (t1),
1010 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1012 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1017 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1018 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1019 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1023 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1024 TYPE_RAISES_EXCEPTIONS (t2), 1))
1027 /* This case is anti-symmetrical!
1028 One can pass a base member (or member function)
1029 to something expecting a derived member (or member function),
1030 but not vice-versa! */
1032 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1033 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1037 case REFERENCE_TYPE:
1038 t1 = TREE_TYPE (t1);
1039 t2 = TREE_TYPE (t2);
1040 /* first, check whether the referred types match with the
1041 required level of strictness */
1042 val = comptypes (t1, t2, strict);
1045 if (TREE_CODE (t1) == RECORD_TYPE
1046 && TREE_CODE (t2) == RECORD_TYPE)
1051 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1052 TYPE_RAISES_EXCEPTIONS (t2), 1))
1055 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1056 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1057 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1061 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1062 here since this is the one place where
1063 COMPARE_REDECLARATION should be used. */
1064 val = comp_array_types (comptypes, t1, t2, orig_strict);
1067 case TEMPLATE_TYPE_PARM:
1068 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1069 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1072 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
1074 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1077 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1082 return attrval == 2 && val == 1 ? 2 : val;
1085 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1086 only in the same direction as the target type. */
1089 comp_cv_target_types (ttl, ttr, nptrs)
1095 if (!at_least_as_qualified_p (ttl, ttr)
1096 && !at_least_as_qualified_p (ttr, ttl))
1097 /* The qualifications are incomparable. */
1100 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1101 return more_qualified_p (ttr, ttl) ? -1 : 1;
1103 t = comp_target_types (ttl, ttr, nptrs);
1104 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1105 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1111 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1112 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1113 converted to TTL. Return -1 means that TTL can be converted to TTR but
1116 NPTRS is the number of pointers we can strip off and keep cool.
1117 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1118 but to not permit B** to convert to A**.
1120 This should go away. Callers should use can_convert or something
1121 similar instead. (jason 17 Apr 1997) */
1124 comp_target_types (ttl, ttr, nptrs)
1128 ttl = TYPE_MAIN_VARIANT (ttl);
1129 ttr = TYPE_MAIN_VARIANT (ttr);
1130 if (same_type_p (ttl, ttr))
1133 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1136 if ((TREE_CODE (ttr) == POINTER_TYPE
1137 || TREE_CODE (ttr) == REFERENCE_TYPE)
1138 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1139 of the type pointed to. This is necessary for reference init
1140 semantics. We won't get here from a previous call with nptrs == 1;
1141 for multi-level pointers we end up in comp_ptr_ttypes. */
1144 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1146 ttl = TREE_TYPE (ttl);
1147 ttr = TREE_TYPE (ttr);
1151 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1152 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1154 else if (TREE_CODE (ttl) == VOID_TYPE
1155 && TREE_CODE (ttr) != FUNCTION_TYPE
1156 && TREE_CODE (ttr) != METHOD_TYPE
1157 && TREE_CODE (ttr) != OFFSET_TYPE)
1159 else if (TREE_CODE (ttr) == VOID_TYPE
1160 && TREE_CODE (ttl) != FUNCTION_TYPE
1161 && TREE_CODE (ttl) != METHOD_TYPE
1162 && TREE_CODE (ttl) != OFFSET_TYPE)
1164 else if (TREE_CODE (ttl) == POINTER_TYPE
1165 || TREE_CODE (ttl) == ARRAY_TYPE)
1167 if (comp_ptr_ttypes (ttl, ttr))
1169 else if (comp_ptr_ttypes (ttr, ttl))
1175 /* Const and volatile mean something different for function types,
1176 so the usual checks are not appropriate. */
1177 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1178 return comp_target_types (ttl, ttr, nptrs - 1);
1180 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1183 if (TREE_CODE (ttr) == ARRAY_TYPE)
1184 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1185 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1192 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1197 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1206 argsl = TYPE_ARG_TYPES (ttl);
1207 argsr = TYPE_ARG_TYPES (ttr);
1209 /* Compare 'this' here, not in comp_target_parms. */
1210 if (TREE_CODE (ttr) == METHOD_TYPE)
1212 tree tl = TYPE_METHOD_BASETYPE (ttl);
1213 tree tr = TYPE_METHOD_BASETYPE (ttr);
1215 if (!same_or_base_type_p (tr, tl))
1217 if (same_or_base_type_p (tl, tr))
1223 argsl = TREE_CHAIN (argsl);
1224 argsr = TREE_CHAIN (argsr);
1227 switch (comp_target_parms (argsl, argsr, 1))
1235 return saw_contra ? -1 : 1;
1238 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1242 /* Contravariance: we can assign a pointer to base member to a pointer
1243 to derived member. Note difference from simple pointer case, where
1244 we can pass a pointer to derived to a pointer to base. */
1245 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1246 TYPE_OFFSET_BASETYPE (ttl)))
1248 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1249 TYPE_OFFSET_BASETYPE (ttr)))
1259 ttl = TREE_TYPE (ttl);
1260 ttr = TREE_TYPE (ttr);
1262 if (TREE_CODE (ttl) == POINTER_TYPE
1263 || TREE_CODE (ttl) == ARRAY_TYPE)
1265 if (comp_ptr_ttypes (ttl, ttr))
1271 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1276 else if (IS_AGGR_TYPE (ttl))
1280 if (same_or_base_type_p (build_pointer_type (ttl),
1281 build_pointer_type (ttr)))
1283 if (same_or_base_type_p (build_pointer_type (ttr),
1284 build_pointer_type (ttl)))
1292 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1295 at_least_as_qualified_p (type1, type2)
1299 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1300 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1301 == CP_TYPE_QUALS (type2));
1304 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1307 more_qualified_p (type1, type2)
1311 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1312 && at_least_as_qualified_p (type1, type2));
1315 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1316 more cv-qualified that TYPE1, and 0 otherwise. */
1319 comp_cv_qualification (type1, type2)
1323 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1326 if (at_least_as_qualified_p (type1, type2))
1329 else if (at_least_as_qualified_p (type2, type1))
1335 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1336 subset of the cv-qualification signature of TYPE2, and the types
1337 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1340 comp_cv_qual_signature (type1, type2)
1344 if (comp_ptr_ttypes_real (type2, type1, -1))
1346 else if (comp_ptr_ttypes_real (type1, type2, -1))
1352 /* If two types share a common base type, return that basetype.
1353 If there is not a unique most-derived base type, this function
1354 returns ERROR_MARK_NODE. */
1357 common_base_type (tt1, tt2)
1360 tree best = NULL_TREE;
1363 /* If one is a baseclass of another, that's good enough. */
1364 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1366 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1369 /* Otherwise, try to find a unique baseclass of TT1
1370 that is shared by TT2, and follow that down. */
1371 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1373 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1374 tree trial = common_base_type (basetype, tt2);
1377 if (trial == error_mark_node)
1379 if (best == NULL_TREE)
1381 else if (best != trial)
1382 return error_mark_node;
1387 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1389 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1390 tree trial = common_base_type (tt1, basetype);
1393 if (trial == error_mark_node)
1395 if (best == NULL_TREE)
1397 else if (best != trial)
1398 return error_mark_node;
1404 /* Subroutines of `comptypes'. */
1406 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1407 equivalent in the sense that functions with those parameter types
1408 can have equivalent types. The two lists must be equivalent,
1411 C++: See comment above about TYPE1, TYPE2. */
1414 compparms (parms1, parms2)
1415 tree parms1, parms2;
1417 register tree t1 = parms1, t2 = parms2;
1419 /* An unspecified parmlist matches any specified parmlist
1420 whose argument types don't need default promotions. */
1424 if (t1 == 0 && t2 == 0)
1426 /* If one parmlist is shorter than the other,
1427 they fail to match. */
1428 if (t1 == 0 || t2 == 0)
1430 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1433 t1 = TREE_CHAIN (t1);
1434 t2 = TREE_CHAIN (t2);
1438 /* This really wants return whether or not parameter type lists
1439 would make their owning functions assignment compatible or not.
1441 The return value is like for comp_target_types.
1443 This should go away, possibly with the exception of the empty parmlist
1444 conversion; there are no conversions between function types in C++.
1445 (jason 17 Apr 1997) */
1448 comp_target_parms (parms1, parms2, strict)
1449 tree parms1, parms2;
1452 register tree t1 = parms1, t2 = parms2;
1453 int warn_contravariance = 0;
1455 /* In C, an unspecified parmlist matches any specified parmlist
1456 whose argument types don't need default promotions. This is not
1457 true for C++, but let's do it anyway for unfixed headers. */
1459 if (t1 == 0 && t2 != 0)
1461 if (! flag_strict_prototype && t2 == void_list_node)
1462 /* t1 might be the arglist of a function pointer in extern "C"
1463 declared to take (), which we fudged to (...). Don't make the
1464 user pay for our mistake. */;
1466 cp_pedwarn ("ANSI C++ prohibits conversion from `%#T' to `(...)'",
1468 return self_promoting_args_p (t2);
1471 return self_promoting_args_p (t1);
1473 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1477 /* If one parmlist is shorter than the other,
1478 they fail to match, unless STRICT is <= 0. */
1479 if (t1 == 0 || t2 == 0)
1484 return 1 + warn_contravariance;
1485 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1487 p1 = TREE_VALUE (t1);
1488 p2 = TREE_VALUE (t2);
1489 if (same_type_p (p1, p2))
1495 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1496 || (TREE_CODE (p1) == REFERENCE_TYPE
1497 && TREE_CODE (p2) == REFERENCE_TYPE))
1500 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1501 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1504 /* The following is wrong for contravariance,
1505 but many programs depend on it. */
1506 if (TREE_TYPE (p1) == void_type_node)
1508 if (TREE_TYPE (p2) == void_type_node)
1510 warn_contravariance = 1;
1513 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1514 && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (p1)),
1515 TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1518 /* Note backwards order due to contravariance. */
1519 if (comp_target_types (p2, p1, 1) <= 0)
1521 if (comp_target_types (p1, p2, 1) > 0)
1523 warn_contravariance = 1;
1530 return warn_contravariance ? -1 : 1;
1533 /* Compute the value of the `sizeof' operator. */
1539 enum tree_code code = TREE_CODE (type);
1542 if (processing_template_decl)
1543 return build_min (SIZEOF_EXPR, sizetype, type);
1545 if (code == FUNCTION_TYPE)
1547 if (pedantic || warn_pointer_arith)
1548 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1549 return size_int (1);
1551 if (code == METHOD_TYPE)
1553 if (pedantic || warn_pointer_arith)
1554 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1555 return size_int (1);
1557 if (code == VOID_TYPE)
1559 if (pedantic || warn_pointer_arith)
1560 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1561 return size_int (1);
1563 if (code == ERROR_MARK)
1564 return size_int (1);
1566 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1567 referenced object.'' */
1568 if (code == REFERENCE_TYPE)
1569 type = TREE_TYPE (type);
1571 if (code == OFFSET_TYPE)
1573 cp_error ("`sizeof' applied to non-static member");
1574 return size_int (0);
1577 if (TYPE_SIZE (complete_type (type)) == 0)
1579 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1580 return size_int (0);
1583 /* Convert in case a char is more than one unit. */
1584 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1585 size_int (TYPE_PRECISION (char_type_node)));
1586 t = convert (sizetype, t);
1587 /* size_binop does not put the constant in range, so do it now. */
1588 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1589 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1597 if (processing_template_decl)
1598 return build_min (SIZEOF_EXPR, sizetype, e);
1600 if (TREE_CODE (e) == COMPONENT_REF
1601 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1602 error ("sizeof applied to a bit-field");
1603 if (is_overloaded_fn (e))
1605 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1606 return size_int (1);
1608 else if (type_unknown_p (e))
1610 incomplete_type_error (e, TREE_TYPE (e));
1611 return size_int (1);
1613 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1614 member unless you're in a non-static member of X. So hand off to
1615 resolve_offset_ref. [expr.prim] */
1616 else if (TREE_CODE (e) == OFFSET_REF)
1617 e = resolve_offset_ref (e);
1619 if (e == error_mark_node)
1622 return c_sizeof (TREE_TYPE (e));
1626 c_sizeof_nowarn (type)
1629 enum tree_code code = TREE_CODE (type);
1632 if (code == FUNCTION_TYPE
1633 || code == METHOD_TYPE
1634 || code == VOID_TYPE
1635 || code == ERROR_MARK)
1636 return size_int (1);
1637 if (code == REFERENCE_TYPE)
1638 type = TREE_TYPE (type);
1640 if (TYPE_SIZE (type) == 0)
1641 return size_int (0);
1643 /* Convert in case a char is more than one unit. */
1644 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1645 size_int (TYPE_PRECISION (char_type_node)));
1646 t = convert (sizetype, t);
1647 force_fit_type (t, 0);
1651 /* Implement the __alignof keyword: Return the minimum required
1652 alignment of TYPE, measured in bytes. */
1658 enum tree_code code = TREE_CODE (type);
1661 if (processing_template_decl)
1662 return build_min (ALIGNOF_EXPR, sizetype, type);
1664 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1665 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1667 if (code == VOID_TYPE || code == ERROR_MARK)
1668 return size_int (1);
1670 /* C++: this is really correct! */
1671 if (code == REFERENCE_TYPE)
1672 type = TREE_TYPE (type);
1674 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1675 force_fit_type (t, 0);
1679 /* Perform the array-to-pointer and function-to-pointer conversions
1682 In addition, references are converted to rvalues and manifest
1683 constants are replaced by their values. */
1686 decay_conversion (exp)
1690 register enum tree_code code;
1692 if (TREE_CODE (exp) == OFFSET_REF)
1693 exp = resolve_offset_ref (exp);
1695 type = TREE_TYPE (exp);
1696 code = TREE_CODE (type);
1698 if (code == REFERENCE_TYPE)
1700 exp = convert_from_reference (exp);
1701 type = TREE_TYPE (exp);
1702 code = TREE_CODE (type);
1705 /* Constants can be used directly unless they're not loadable. */
1706 if (TREE_CODE (exp) == CONST_DECL)
1707 exp = DECL_INITIAL (exp);
1708 /* Replace a nonvolatile const static variable with its value. We
1709 don't do this for arrays, though; we want the address of the
1710 first element of the array, not the address of the first element
1711 of its initializing constant. We *do* replace variables that the
1712 user isn't really supposed to know about; this is a hack to deal
1713 with __PRETTY_FUNCTION__ and the like. */
1714 else if (TREE_READONLY_DECL_P (exp)
1715 && (code != ARRAY_TYPE
1716 || (TREE_CODE (exp) == VAR_DECL && DECL_IGNORED_P (exp))))
1718 exp = decl_constant_value (exp);
1719 type = TREE_TYPE (exp);
1722 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1723 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1725 if (code == VOID_TYPE)
1727 error ("void value not ignored as it ought to be");
1728 return error_mark_node;
1730 if (code == METHOD_TYPE)
1731 my_friendly_abort (990506);
1732 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1733 return build_unary_op (ADDR_EXPR, exp, 0);
1734 if (code == ARRAY_TYPE)
1739 if (TREE_CODE (exp) == INDIRECT_REF)
1741 /* Stripping away the INDIRECT_REF is not the right
1742 thing to do for references... */
1743 tree inner = TREE_OPERAND (exp, 0);
1744 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1746 inner = build1 (CONVERT_EXPR,
1747 build_pointer_type (TREE_TYPE
1748 (TREE_TYPE (inner))),
1750 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1752 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1755 if (TREE_CODE (exp) == COMPOUND_EXPR)
1757 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1758 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1759 TREE_OPERAND (exp, 0), op1);
1763 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1765 error ("invalid use of non-lvalue array");
1766 return error_mark_node;
1769 ptrtype = build_pointer_type (TREE_TYPE (type));
1771 if (TREE_CODE (exp) == VAR_DECL)
1773 /* ??? This is not really quite correct
1774 in that the type of the operand of ADDR_EXPR
1775 is not the target type of the type of the ADDR_EXPR itself.
1776 Question is, can this lossage be avoided? */
1777 adr = build1 (ADDR_EXPR, ptrtype, exp);
1778 if (mark_addressable (exp) == 0)
1779 return error_mark_node;
1780 TREE_CONSTANT (adr) = staticp (exp);
1781 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1784 /* This way is better for a COMPONENT_REF since it can
1785 simplify the offset for a component. */
1786 adr = build_unary_op (ADDR_EXPR, exp, 1);
1787 return cp_convert (ptrtype, adr);
1794 default_conversion (exp)
1798 enum tree_code code;
1800 exp = decay_conversion (exp);
1802 type = TREE_TYPE (exp);
1803 code = TREE_CODE (type);
1805 if (INTEGRAL_CODE_P (code))
1807 tree t = type_promotes_to (type);
1809 return cp_convert (t, exp);
1815 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1819 inline_conversion (exp)
1822 if (TREE_CODE (exp) == FUNCTION_DECL)
1823 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1828 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1829 decay_conversion to one. */
1832 string_conv_p (totype, exp, warn)
1838 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1841 t = TREE_TYPE (totype);
1842 if (!same_type_p (t, char_type_node)
1843 && !same_type_p (t, wchar_type_node))
1846 if (TREE_CODE (exp) == STRING_CST)
1848 /* Make sure that we don't try to convert between char and wchar_t. */
1849 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1854 /* Is this a string constant which has decayed to 'const char *'? */
1855 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1856 if (!same_type_p (TREE_TYPE (exp), t))
1859 if (TREE_CODE (exp) != ADDR_EXPR
1860 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1864 /* This warning is not very useful, as it complains about printf. */
1865 if (warn && warn_write_strings)
1866 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1872 build_object_ref (datum, basetype, field)
1873 tree datum, basetype, field;
1876 if (datum == error_mark_node)
1877 return error_mark_node;
1879 dtype = TREE_TYPE (datum);
1880 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1881 dtype = TREE_TYPE (dtype);
1882 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1884 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1885 basetype, field, dtype);
1886 return error_mark_node;
1888 else if (is_aggr_type (basetype, 1))
1890 tree binfo = binfo_or_else (basetype, dtype);
1892 return build_x_component_ref (build_scoped_ref (datum, basetype),
1895 return error_mark_node;
1898 /* Like `build_component_ref, but uses an already found field, and converts
1899 from a reference. Must compute access for current_class_ref.
1903 build_component_ref_1 (datum, field, protect)
1907 return convert_from_reference
1908 (build_component_ref (datum, field, NULL_TREE, protect));
1911 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1912 can, for example, use as an lvalue. This code used to be in
1913 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1914 expressions, where we're dealing with aggregates. But now it's again only
1915 called from unary_complex_lvalue. The case (in particular) that led to
1916 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1920 rationalize_conditional_expr (code, t)
1921 enum tree_code code;
1924 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1925 the first operand is always the one to be used if both operands
1926 are equal, so we know what conditional expression this used to be. */
1927 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1930 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1931 ? LE_EXPR : GE_EXPR),
1932 TREE_OPERAND (t, 0),
1933 TREE_OPERAND (t, 1)),
1934 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1935 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1939 build_conditional_expr (TREE_OPERAND (t, 0),
1940 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1941 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1944 /* Given the TYPE of an anonymous union field inside T, return the
1945 FIELD_DECL for the field. If not found return NULL_TREE. Because
1946 anonymous unions can nest, we must also search all anonymous unions
1947 that are directly reachable. */
1950 lookup_anon_field (t, type)
1955 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1957 if (TREE_STATIC (field))
1959 if (TREE_CODE (field) != FIELD_DECL)
1962 /* If we find it directly, return the field. */
1963 if (DECL_NAME (field) == NULL_TREE
1964 && type == TREE_TYPE (field))
1969 /* Otherwise, it could be nested, search harder. */
1970 if (DECL_NAME (field) == NULL_TREE
1971 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1973 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1981 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1982 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1983 that we are interested in, or it can be a FIELD_DECL. */
1986 build_component_ref (datum, component, basetype_path, protect)
1987 tree datum, component, basetype_path;
1990 register tree basetype;
1991 register enum tree_code code;
1992 register tree field = NULL;
1997 if (processing_template_decl)
1998 return build_min_nt (COMPONENT_REF, datum, component);
2000 if (datum == error_mark_node
2001 || TREE_TYPE (datum) == error_mark_node)
2002 return error_mark_node;
2004 /* BASETYPE holds the type of the class containing the COMPONENT. */
2005 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2007 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2009 switch (TREE_CODE (datum))
2013 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2014 basetype_path, protect);
2015 return build (COMPOUND_EXPR, TREE_TYPE (value),
2016 TREE_OPERAND (datum, 0), value);
2019 return build_conditional_expr
2020 (TREE_OPERAND (datum, 0),
2021 build_component_ref (TREE_OPERAND (datum, 1), component,
2022 basetype_path, protect),
2023 build_component_ref (TREE_OPERAND (datum, 2), component,
2024 basetype_path, protect));
2027 cp_error ("invalid use of %D", datum);
2028 datum = error_mark_node;
2035 code = TREE_CODE (basetype);
2037 if (code == REFERENCE_TYPE)
2039 datum = convert_from_reference (datum);
2040 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2041 code = TREE_CODE (basetype);
2043 if (TREE_CODE (datum) == OFFSET_REF)
2045 datum = resolve_offset_ref (datum);
2046 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2047 code = TREE_CODE (basetype);
2050 /* First, see if there is a field or component with name COMPONENT. */
2051 if (TREE_CODE (component) == TREE_LIST)
2053 /* I could not trigger this code. MvL */
2054 my_friendly_abort (980326);
2056 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2057 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2059 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2062 if (! IS_AGGR_TYPE_CODE (code))
2064 if (code != ERROR_MARK)
2065 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2066 component, datum, basetype);
2067 return error_mark_node;
2070 if (!complete_type_or_else (basetype, datum))
2071 return error_mark_node;
2073 if (TREE_CODE (component) == BIT_NOT_EXPR)
2075 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2077 cp_error ("destructor specifier `%T::~%T' must have matching names",
2078 basetype, TREE_OPERAND (component, 0));
2079 return error_mark_node;
2081 if (! TYPE_HAS_DESTRUCTOR (basetype))
2083 cp_error ("type `%T' has no destructor", basetype);
2084 return error_mark_node;
2086 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2089 /* Look up component name in the structure type definition. */
2090 if (CLASSTYPE_VFIELD (basetype)
2091 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
2092 /* Special-case this because if we use normal lookups in an ambiguous
2093 hierarchy, the compiler will abort (because vptr lookups are
2094 not supposed to be ambiguous. */
2095 field = CLASSTYPE_VFIELD (basetype);
2096 else if (TREE_CODE (component) == FIELD_DECL)
2098 else if (TREE_CODE (component) == TYPE_DECL)
2100 cp_error ("invalid use of type decl `%#D' as expression", component);
2101 return error_mark_node;
2103 else if (TREE_CODE (component) == TEMPLATE_DECL)
2105 cp_error ("invalid use of template `%#D' as expression", component);
2106 return error_mark_node;
2110 tree name = component;
2111 if (TREE_CODE (component) == VAR_DECL)
2112 name = DECL_NAME (component);
2113 if (TREE_CODE (component) == NAMESPACE_DECL)
2114 /* Source is in error, but produce a sensible diagnostic. */
2115 name = DECL_NAME (component);
2116 if (basetype_path == NULL_TREE)
2117 basetype_path = TYPE_BINFO (basetype);
2118 field = lookup_field (basetype_path, name,
2119 protect && !VFIELD_NAME_P (name), 0);
2120 if (field == error_mark_node)
2121 return error_mark_node;
2123 if (field == NULL_TREE)
2125 /* Not found as a data field, look for it as a method. If found,
2126 then if this is the only possible one, return it, else
2127 report ambiguity error. */
2128 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2129 if (fndecls == error_mark_node)
2130 return error_mark_node;
2133 /* If the function is unique and static, we can resolve it
2134 now. Otherwise, we have to wait and see what context it is
2135 used in; a component_ref involving a non-static member
2136 function can only be used in a call (expr.ref). */
2138 if (TREE_CHAIN (fndecls) == NULL_TREE
2139 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2141 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2143 tree fndecl = TREE_VALUE (fndecls);
2144 enforce_access (TREE_PURPOSE (fndecls), fndecl);
2150 /* A unique non-static member function. Other parts
2151 of the compiler expect something with
2152 unknown_type_node to be really overloaded, so
2154 TREE_VALUE (fndecls)
2155 = scratch_ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2159 ref = build (COMPONENT_REF, unknown_type_node,
2160 datum, TREE_VALUE (fndecls));
2164 cp_error ("`%#T' has no member named `%D'", basetype, name);
2165 return error_mark_node;
2167 else if (TREE_TYPE (field) == error_mark_node)
2168 return error_mark_node;
2170 if (TREE_CODE (field) != FIELD_DECL)
2172 if (TREE_CODE (field) == TYPE_DECL)
2173 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2174 else if (DECL_RTL (field) != 0)
2177 TREE_USED (field) = 1;
2182 /* See if we have to do any conversions so that we pick up the field from the
2184 if (DECL_FIELD_CONTEXT (field) != basetype)
2186 tree context = DECL_FIELD_CONTEXT (field);
2187 tree base = context;
2188 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2189 && ANON_AGGR_TYPE_P (base))
2191 base = TYPE_CONTEXT (base);
2194 /* Handle base classes here... */
2195 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2197 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2198 if (integer_zerop (addr))
2200 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2201 return error_mark_node;
2203 if (VBASE_NAME_P (DECL_NAME (field)))
2205 /* It doesn't matter which vbase pointer we grab, just
2206 find one of them. */
2207 tree binfo = get_binfo (base,
2208 TREE_TYPE (TREE_TYPE (addr)), 0);
2209 addr = convert_pointer_to_real (binfo, addr);
2212 addr = convert_pointer_to (base, addr);
2213 datum = build_indirect_ref (addr, NULL_PTR);
2214 my_friendly_assert (datum != error_mark_node, 311);
2218 /* Handle things from anon unions here... */
2219 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2221 tree subfield = lookup_anon_field (basetype, context);
2222 tree subdatum = build_component_ref (datum, subfield,
2223 basetype_path, protect);
2224 return build_component_ref (subdatum, field, basetype_path, protect);
2228 /* Compute the type of the field, as described in [expr.ref]. */
2229 type_quals = TYPE_UNQUALIFIED;
2230 field_type = TREE_TYPE (field);
2231 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2232 /* The standard says that the type of the result should be the
2233 type referred to by the reference. But for now, at least, we
2234 do the conversion from reference type later. */
2238 type_quals = (CP_TYPE_QUALS (field_type)
2239 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2241 /* A field is const (volatile) if the enclosing object, or the
2242 field itself, is const (volatile). But, a mutable field is
2243 not const, even within a const object. */
2244 if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2245 type_quals &= ~TYPE_QUAL_CONST;
2246 field_type = cp_build_qualified_type (field_type, type_quals);
2249 ref = fold (build (COMPONENT_REF, field_type,
2250 break_out_cleanups (datum), field));
2252 /* Mark the expression const or volatile, as appropriate. Even
2253 though we've dealt with the type above, we still have to mark the
2254 expression itself. */
2255 if (type_quals & TYPE_QUAL_CONST)
2256 TREE_READONLY (ref) = 1;
2257 else if (type_quals & TYPE_QUAL_VOLATILE)
2258 TREE_THIS_VOLATILE (ref) = 1;
2263 /* Variant of build_component_ref for use in expressions, which should
2264 never have REFERENCE_TYPE. */
2267 build_x_component_ref (datum, component, basetype_path, protect)
2268 tree datum, component, basetype_path;
2271 tree t = build_component_ref (datum, component, basetype_path, protect);
2273 if (! processing_template_decl)
2274 t = convert_from_reference (t);
2279 /* Given an expression PTR for a pointer, return an expression
2280 for the value pointed to.
2281 ERRORSTRING is the name of the operator to appear in error messages.
2283 This function may need to overload OPERATOR_FNNAME.
2284 Must also handle REFERENCE_TYPEs for C++. */
2287 build_x_indirect_ref (ptr, errorstring)
2289 const char *errorstring;
2293 if (processing_template_decl)
2294 return build_min_nt (INDIRECT_REF, ptr);
2296 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2300 return build_indirect_ref (ptr, errorstring);
2304 build_indirect_ref (ptr, errorstring)
2306 const char *errorstring;
2308 register tree pointer, type;
2310 if (ptr == error_mark_node)
2311 return error_mark_node;
2313 if (ptr == current_class_ptr)
2314 return current_class_ref;
2316 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2317 ? ptr : default_conversion (ptr));
2318 type = TREE_TYPE (pointer);
2320 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2324 If the type of the expression is "pointer to T," the type
2325 of the result is "T."
2327 We must use the canonical variant because certain parts of
2328 the back end, like fold, do pointer comparisons between
2330 tree t = canonical_type_variant (TREE_TYPE (type));
2332 if (same_type_p (TYPE_MAIN_VARIANT (t), void_type_node))
2334 /* A pointer to incomplete type (other than cv void) can be
2335 dereferenced [expr.unary.op]/1 */
2336 cp_error ("`%T' is not a pointer-to-object type", type);
2337 return error_mark_node;
2339 else if (TREE_CODE (pointer) == ADDR_EXPR
2341 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2342 /* The POINTER was something like `&x'. We simplify `*&x' to
2344 return TREE_OPERAND (pointer, 0);
2347 tree ref = build1 (INDIRECT_REF, t, pointer);
2349 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2350 so that we get the proper error message if the result is used
2351 to assign to. Also, &* is supposed to be a no-op. */
2352 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2353 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2354 TREE_SIDE_EFFECTS (ref)
2355 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2360 /* `pointer' won't be an error_mark_node if we were given a
2361 pointer to member, so it's cool to check for this here. */
2362 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2363 error ("invalid use of `%s' on pointer to member", errorstring);
2364 else if (pointer != error_mark_node)
2367 error ("invalid type argument of `%s'", errorstring);
2369 error ("invalid type argument");
2371 return error_mark_node;
2374 /* This handles expressions of the form "a[i]", which denotes
2377 This is logically equivalent in C to *(a+i), but we may do it differently.
2378 If A is a variable or a member, we generate a primitive ARRAY_REF.
2379 This avoids forcing the array out of registers, and can work on
2380 arrays that are not lvalues (for example, members of structures returned
2383 If INDEX is of some user-defined type, it must be converted to
2384 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2385 will inherit the type of the array, which will be some pointer type. */
2388 build_array_ref (array, idx)
2393 error ("subscript missing in array reference");
2394 return error_mark_node;
2397 if (TREE_TYPE (array) == error_mark_node
2398 || TREE_TYPE (idx) == error_mark_node)
2399 return error_mark_node;
2401 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2402 && TREE_CODE (array) != INDIRECT_REF)
2406 /* Subscripting with type char is likely to lose
2407 on a machine where chars are signed.
2408 So warn on any machine, but optionally.
2409 Don't warn for unsigned char since that type is safe.
2410 Don't warn for signed char because anyone who uses that
2411 must have done so deliberately. */
2412 if (warn_char_subscripts
2413 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2414 warning ("array subscript has type `char'");
2416 /* Apply default promotions *after* noticing character types. */
2417 idx = default_conversion (idx);
2419 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2421 error ("array subscript is not an integer");
2422 return error_mark_node;
2425 /* An array that is indexed by a non-constant
2426 cannot be stored in a register; we must be able to do
2427 address arithmetic on its address.
2428 Likewise an array of elements of variable size. */
2429 if (TREE_CODE (idx) != INTEGER_CST
2430 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2431 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2434 if (mark_addressable (array) == 0)
2435 return error_mark_node;
2437 /* An array that is indexed by a constant value which is not within
2438 the array bounds cannot be stored in a register either; because we
2439 would get a crash in store_bit_field/extract_bit_field when trying
2440 to access a non-existent part of the register. */
2441 if (TREE_CODE (idx) == INTEGER_CST
2442 && TYPE_VALUES (TREE_TYPE (array))
2443 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2445 if (mark_addressable (array) == 0)
2446 return error_mark_node;
2449 if (pedantic && !lvalue_p (array))
2450 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2452 /* Note in C++ it is valid to subscript a `register' array, since
2453 it is valid to take the address of something with that
2454 storage specification. */
2458 while (TREE_CODE (foo) == COMPONENT_REF)
2459 foo = TREE_OPERAND (foo, 0);
2460 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2461 warning ("subscripting array declared `register'");
2464 type = TREE_TYPE (TREE_TYPE (array));
2465 rval = build (ARRAY_REF, type, array, idx);
2466 /* Array ref is const/volatile if the array elements are
2467 or if the array is.. */
2468 TREE_READONLY (rval)
2469 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2470 TREE_SIDE_EFFECTS (rval)
2471 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2472 TREE_THIS_VOLATILE (rval)
2473 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2474 return require_complete_type (fold (rval));
2478 tree ar = default_conversion (array);
2479 tree ind = default_conversion (idx);
2481 /* Put the integer in IND to simplify error checking. */
2482 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2489 if (ar == error_mark_node)
2492 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2494 error ("subscripted value is neither array nor pointer");
2495 return error_mark_node;
2497 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2499 error ("array subscript is not an integer");
2500 return error_mark_node;
2503 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar,
2509 /* Build a function call to function FUNCTION with parameters PARAMS.
2510 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2511 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2512 not include any object pointer that may be required. FUNCTION's
2513 data type may be a function type or a pointer-to-function.
2515 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2516 is the list of possible methods that FUNCTION could conceivably
2517 be. If the list of methods comes from a class, then it will be
2518 a list of lists (where each element is associated with the class
2519 that produced it), otherwise it will be a simple list (for
2520 functions overloaded in global scope).
2522 In the first case, TREE_VALUE (function) is the head of one of those
2523 lists, and TREE_PURPOSE is the name of the function.
2525 In the second case, TREE_PURPOSE (function) is the function's
2528 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2530 When calling a TEMPLATE_DECL, we don't require a complete return
2534 build_x_function_call (function, params, decl)
2535 tree function, params, decl;
2538 tree template_id = NULL_TREE;
2541 if (function == error_mark_node)
2542 return error_mark_node;
2544 if (processing_template_decl)
2545 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2547 /* Save explicit template arguments if found */
2548 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2550 template_id = function;
2551 function = TREE_OPERAND (function, 0);
2554 type = TREE_TYPE (function);
2556 if (TREE_CODE (type) == OFFSET_TYPE
2557 && TREE_TYPE (type) == unknown_type_node
2558 && TREE_CODE (function) == TREE_LIST
2559 && TREE_CHAIN (function) == NULL_TREE)
2561 /* Undo (Foo:bar)()... */
2562 type = TYPE_OFFSET_BASETYPE (type);
2563 function = TREE_VALUE (function);
2564 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2565 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2566 function = TREE_VALUE (function);
2567 if (TREE_CODE (function) == OVERLOAD)
2568 function = OVL_FUNCTION (function);
2569 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2570 function = DECL_NAME (function);
2571 return build_method_call (decl, function, params,
2572 TYPE_BINFO (type), LOOKUP_NORMAL);
2575 if ((TREE_CODE (function) == FUNCTION_DECL
2576 && DECL_STATIC_FUNCTION_P (function))
2577 || (TREE_CODE (function) == TEMPLATE_DECL
2578 && DECL_STATIC_FUNCTION_P (DECL_RESULT (function))))
2579 return build_member_call(DECL_CONTEXT (function),
2581 ? template_id : DECL_NAME (function),
2584 is_method = ((TREE_CODE (function) == TREE_LIST
2585 && current_class_type != NULL_TREE
2586 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2588 || (TREE_CODE (function) == OVERLOAD
2589 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2590 || TREE_CODE (function) == IDENTIFIER_NODE
2591 || TREE_CODE (type) == METHOD_TYPE
2592 || TYPE_PTRMEMFUNC_P (type));
2594 /* A friend template. Make it look like a toplevel declaration. */
2595 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2596 function = scratch_ovl_cons (function, NULL_TREE);
2598 /* Handle methods, friends, and overloaded functions, respectively. */
2601 tree basetype = NULL_TREE;
2603 if (TREE_CODE (function) == OVERLOAD)
2604 function = OVL_CURRENT (function);
2606 if (TREE_CODE (function) == FUNCTION_DECL
2607 || DECL_FUNCTION_TEMPLATE_P (function))
2609 basetype = DECL_CLASS_CONTEXT (function);
2611 if (DECL_NAME (function))
2612 function = DECL_NAME (function);
2614 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2616 else if (TREE_CODE (function) == TREE_LIST)
2618 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2619 == FUNCTION_DECL, 312);
2620 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2621 function = TREE_PURPOSE (function);
2623 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2625 if (TREE_CODE (function) == OFFSET_REF)
2627 if (TREE_OPERAND (function, 0))
2628 decl = TREE_OPERAND (function, 0);
2630 /* Call via a pointer to member function. */
2631 if (decl == NULL_TREE)
2633 error ("pointer to member function called, but not in class scope");
2634 return error_mark_node;
2636 /* What other type of POINTER_TYPE could this be? */
2637 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2638 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2639 && TREE_CODE (function) != OFFSET_REF)
2640 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2645 /* this is an abbreviated method call.
2646 must go through here in case it is a virtual function.
2647 @@ Perhaps this could be optimized. */
2649 if (basetype && (! current_class_type
2650 || ! DERIVED_FROM_P (basetype, current_class_type)))
2651 return build_member_call (basetype, function, params);
2653 if (decl == NULL_TREE)
2655 if (current_class_type == NULL_TREE)
2657 cp_error ("object missing in call to method `%D'", function);
2658 return error_mark_node;
2660 /* Yow: call from a static member function. */
2661 decl = build_dummy_object (current_class_type);
2664 /* Put back explicit template arguments, if any. */
2666 function = template_id;
2667 return build_method_call (decl, function, params,
2668 NULL_TREE, LOOKUP_NORMAL);
2670 else if (TREE_CODE (function) == COMPONENT_REF
2671 && type == unknown_type_node)
2673 /* Undo what we did in build_component_ref. */
2674 decl = TREE_OPERAND (function, 0);
2675 function = TREE_OPERAND (function, 1);
2676 function = DECL_NAME (OVL_CURRENT (function));
2680 TREE_OPERAND (template_id, 0) = function;
2681 function = template_id;
2684 return build_method_call (decl, function, params,
2685 NULL_TREE, LOOKUP_NORMAL);
2687 else if (really_overloaded_fn (function))
2689 if (OVL_FUNCTION (function) == NULL_TREE)
2691 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2692 TREE_PURPOSE (function));
2693 return error_mark_node;
2697 /* Put back explicit template arguments, if any. */
2699 function = template_id;
2700 return build_new_function_call (function, params);
2704 /* Remove a potential OVERLOAD around it */
2705 function = OVL_CURRENT (function);
2708 if (TREE_CODE (function) == OFFSET_REF)
2710 /* If the component is a data element (or a virtual function), we play
2711 games here to make things work. */
2714 if (TREE_OPERAND (function, 0))
2715 decl = TREE_OPERAND (function, 0);
2717 decl = current_class_ref;
2719 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2721 /* Sigh. OFFSET_REFs are being used for too many things.
2722 They're being used both for -> and ->*, and we want to resolve
2723 the -> cases here, but leave the ->*. We could use
2724 resolve_offset_ref for those, too, but it would call
2725 get_member_function_from_ptrfunc and decl_addr wouldn't get
2726 updated properly. Nasty. */
2727 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2728 function = resolve_offset_ref (function);
2730 function = TREE_OPERAND (function, 1);
2732 function = get_member_function_from_ptrfunc (&decl_addr, function);
2733 params = tree_cons (NULL_TREE, decl_addr, params);
2734 return build_function_call (function, params);
2737 type = TREE_TYPE (function);
2738 if (type != error_mark_node)
2740 if (TREE_CODE (type) == REFERENCE_TYPE)
2741 type = TREE_TYPE (type);
2743 if (IS_AGGR_TYPE (type))
2744 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2749 tree fntype = TREE_TYPE (function);
2750 tree ctypeptr = NULL_TREE;
2752 /* Explicitly named method? */
2753 if (TREE_CODE (function) == FUNCTION_DECL)
2754 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2755 /* Expression with ptr-to-method type? It could either be a plain
2756 usage, or it might be a case where the ptr-to-method is being
2757 passed in as an argument. */
2758 else if (TYPE_PTRMEMFUNC_P (fntype))
2760 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2761 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2762 ctypeptr = build_pointer_type (rec);
2764 /* Unexpected node type? */
2766 my_friendly_abort (116);
2767 if (decl == NULL_TREE)
2769 if (current_function_decl
2770 && DECL_STATIC_FUNCTION_P (current_function_decl))
2771 error ("invalid call to member function needing `this' in static member function scope");
2773 error ("pointer to member function called, but not in class scope");
2774 return error_mark_node;
2776 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2777 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2779 decl = build_unary_op (ADDR_EXPR, decl, 0);
2780 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2783 decl = build_c_cast (ctypeptr, decl);
2784 params = tree_cons (NULL_TREE, decl, params);
2787 return build_function_call (function, params);
2790 /* Resolve a pointer to member function. INSTANCE is the object
2791 instance to use, if the member points to a virtual member. */
2794 get_member_function_from_ptrfunc (instance_ptrptr, function)
2795 tree *instance_ptrptr;
2798 if (TREE_CODE (function) == OFFSET_REF)
2800 function = TREE_OPERAND (function, 1);
2803 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2805 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2806 tree instance, basetype;
2808 tree instance_ptr = *instance_ptrptr;
2810 if (TREE_SIDE_EFFECTS (instance_ptr))
2811 instance_ptr = save_expr (instance_ptr);
2813 if (TREE_SIDE_EFFECTS (function))
2814 function = save_expr (function);
2816 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2817 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2819 delta = cp_convert (ptrdiff_type_node,
2820 build_component_ref (function, delta_identifier,
2822 e3 = PFN_FROM_PTRMEMFUNC (function);
2824 /* This used to avoid checking for virtual functions if basetype
2825 has no virtual functions, according to an earlier ANSI draft.
2826 With the final ISO C++ rules, such an optimization is
2827 incorrect: A pointer to a derived member can be static_cast
2828 to pointer-to-base-member, as long as the dynamic object
2829 later has the right member. */
2831 /* Promoting idx before saving it improves performance on RISC
2832 targets. Without promoting, the first compare used
2833 load-with-sign-extend, while the second used normal load then
2834 shift to sign-extend. An optimizer flaw, perhaps, but it's
2835 easier to make this change. */
2836 idx = save_expr (default_conversion
2837 (build_component_ref (function,
2840 e1 = build_binary_op (GE_EXPR, idx, integer_zero_node);
2842 /* Convert down to the right base, before using the instance. */
2843 instance = convert_pointer_to_real (basetype, instance_ptr);
2844 if (instance == error_mark_node && instance_ptr != error_mark_node)
2847 vtbl = convert_pointer_to (ptr_type_node, instance);
2848 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2851 build_pointer_type (build_pointer_type (vtable_entry_type)),
2852 vtbl, cp_convert (ptrdiff_type_node, delta2));
2853 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2854 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2857 if (! flag_vtable_thunks)
2859 aref = save_expr (aref);
2861 delta = build_binary_op
2863 build_conditional_expr (e1,
2864 build_component_ref (aref,
2871 if (flag_vtable_thunks)
2874 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2875 TREE_TYPE (e2) = TREE_TYPE (e3);
2876 e1 = build_conditional_expr (e1, e2, e3);
2878 /* Make sure this doesn't get evaluated first inside one of the
2879 branches of the COND_EXPR. */
2880 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2881 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2884 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2885 instance_ptr, delta);
2887 if (instance_ptr == error_mark_node
2888 && TREE_CODE (e1) != ADDR_EXPR
2889 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2890 cp_error ("object missing in `%E'", function);
2898 build_function_call_real (function, params, require_complete, flags)
2899 tree function, params;
2900 int require_complete, flags;
2902 register tree fntype, fndecl;
2903 register tree value_type;
2904 register tree coerced_params;
2905 tree name = NULL_TREE, assembler_name = NULL_TREE;
2908 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2909 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2910 if (TREE_CODE (function) == NOP_EXPR
2911 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2912 function = TREE_OPERAND (function, 0);
2914 if (TREE_CODE (function) == FUNCTION_DECL)
2916 name = DECL_NAME (function);
2917 assembler_name = DECL_ASSEMBLER_NAME (function);
2919 GNU_xref_call (current_function_decl,
2920 IDENTIFIER_POINTER (name ? name
2921 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2923 mark_used (function);
2926 /* Convert anything with function type to a pointer-to-function. */
2927 if (pedantic && DECL_MAIN_P (function))
2928 pedwarn ("ANSI C++ forbids calling `main' from within program");
2930 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2931 (because calling an inline function does not mean the function
2932 needs to be separately compiled). */
2934 if (DECL_INLINE (function))
2935 function = inline_conversion (function);
2937 function = build_addr_func (function);
2943 function = build_addr_func (function);
2946 if (function == error_mark_node)
2947 return error_mark_node;
2949 fntype = TREE_TYPE (function);
2951 if (TYPE_PTRMEMFUNC_P (fntype))
2953 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2955 return error_mark_node;
2958 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2959 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2961 if (!((TREE_CODE (fntype) == POINTER_TYPE
2962 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2964 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2966 cp_error ("`%E' cannot be used as a function", function);
2967 return error_mark_node;
2970 /* fntype now gets the type of function pointed to. */
2971 fntype = TREE_TYPE (fntype);
2973 /* Convert the parameters to the types declared in the
2974 function prototype, or apply default promotions. */
2976 if (flags & LOOKUP_COMPLAIN)
2977 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2978 params, fndecl, LOOKUP_NORMAL);
2980 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2983 if (coerced_params == error_mark_node)
2985 if (flags & LOOKUP_SPECULATIVELY)
2988 return error_mark_node;
2991 /* Check for errors in format strings. */
2993 if (warn_format && (name || assembler_name))
2994 check_function_format (name, assembler_name, coerced_params);
2996 /* Recognize certain built-in functions so we can make tree-codes
2997 other than CALL_EXPR. We do this when it enables fold-const.c
2998 to do something useful. */
3000 if (TREE_CODE (function) == ADDR_EXPR
3001 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3002 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3003 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
3008 if (coerced_params == 0)
3009 return integer_zero_node;
3010 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3017 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3019 register tree result
3020 = build_call (function, value_type, coerced_params);
3022 if (require_complete)
3024 if (TREE_CODE (value_type) == VOID_TYPE)
3026 result = require_complete_type (result);
3028 if (IS_AGGR_TYPE (value_type))
3029 result = build_cplus_new (value_type, result);
3030 return convert_from_reference (result);
3035 build_function_call (function, params)
3036 tree function, params;
3038 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3041 /* Convert the actual parameter expressions in the list VALUES
3042 to the types in the list TYPELIST.
3043 If parmdecls is exhausted, or when an element has NULL as its type,
3044 perform the default conversions.
3046 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3048 This is also where warnings about wrong number of args are generated.
3050 Return a list of expressions for the parameters as converted.
3052 Both VALUES and the returned value are chains of TREE_LIST nodes
3053 with the elements of the list in the TREE_VALUE slots of those nodes.
3055 In C++, unspecified trailing parameters can be filled in with their
3056 default arguments, if such were specified. Do so here. */
3059 convert_arguments (typelist, values, fndecl, flags)
3060 tree typelist, values, fndecl;
3063 register tree typetail, valtail;
3064 register tree result = NULL_TREE;
3065 const char *called_thing = 0;
3068 /* Argument passing is always copy-initialization. */
3069 flags |= LOOKUP_ONLYCONVERTING;
3073 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3075 if (DECL_NAME (fndecl) == NULL_TREE
3076 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3077 called_thing = "constructor";
3079 called_thing = "member function";
3082 called_thing = "function";
3085 for (valtail = values, typetail = typelist;
3087 valtail = TREE_CHAIN (valtail), i++)
3089 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3090 register tree val = TREE_VALUE (valtail);
3092 if (val == error_mark_node)
3093 return error_mark_node;
3095 if (type == void_type_node)
3099 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3101 error ("at this point in file");
3104 error ("too many arguments to function");
3105 /* In case anybody wants to know if this argument
3108 TREE_TYPE (tree_last (result)) = error_mark_node;
3112 if (TREE_CODE (val) == OFFSET_REF)
3113 val = resolve_offset_ref (val);
3115 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3116 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3117 if (TREE_CODE (val) == NOP_EXPR
3118 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3119 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3120 val = TREE_OPERAND (val, 0);
3122 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3124 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3125 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3126 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3127 val = default_conversion (val);
3130 if (val == error_mark_node)
3131 return error_mark_node;
3135 /* Formal parm type is specified by a function prototype. */
3138 if (TYPE_SIZE (complete_type (type)) == 0)
3140 error ("parameter type of called function is incomplete");
3145 parmval = convert_for_initialization
3146 (NULL_TREE, type, val, flags,
3147 "argument passing", fndecl, i);
3148 if (PROMOTE_PROTOTYPES
3149 && (TREE_CODE (type) == INTEGER_TYPE
3150 || TREE_CODE (type) == ENUMERAL_TYPE)
3151 && (TYPE_PRECISION (type)
3152 < TYPE_PRECISION (integer_type_node)))
3153 parmval = default_conversion (parmval);
3156 if (parmval == error_mark_node)
3157 return error_mark_node;
3159 result = tree_cons (NULL_TREE, parmval, result);
3163 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3164 val = convert_from_reference (val);
3166 result = tree_cons (NULL_TREE,
3167 convert_arg_to_ellipsis (val),
3172 typetail = TREE_CHAIN (typetail);
3175 if (typetail != 0 && typetail != void_list_node)
3177 /* See if there are default arguments that can be used */
3178 if (TREE_PURPOSE (typetail))
3180 for (; typetail != void_list_node; ++i)
3183 = convert_default_arg (TREE_VALUE (typetail),
3184 TREE_PURPOSE (typetail),
3187 if (parmval == error_mark_node)
3188 return error_mark_node;
3190 result = tree_cons (0, parmval, result);
3191 typetail = TREE_CHAIN (typetail);
3192 /* ends with `...'. */
3193 if (typetail == NULL_TREE)
3201 cp_error_at ("too few arguments to %s `%+#D'",
3202 called_thing, fndecl);
3203 error ("at this point in file");
3206 error ("too few arguments to function");
3207 return error_mark_list;
3211 return nreverse (result);
3214 /* Build a binary-operation expression, after performing default
3215 conversions on the operands. CODE is the kind of expression to build. */
3218 build_x_binary_op (code, arg1, arg2)
3219 enum tree_code code;
3222 if (processing_template_decl)
3223 return build_min_nt (code, arg1, arg2);
3225 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3229 build_binary_op (code, arg1, arg2)
3230 enum tree_code code;
3233 return build_binary_op_nodefault (code, arg1, arg2, code);
3236 /* Build a binary-operation expression without default conversions.
3237 CODE is the kind of expression to build.
3238 This function differs from `build' in several ways:
3239 the data type of the result is computed and recorded in it,
3240 warnings are generated if arg data types are invalid,
3241 special handling for addition and subtraction of pointers is known,
3242 and some optimization is done (operations on narrow ints
3243 are done in the narrower type when that gives the same result).
3244 Constant folding is also done before the result is returned.
3246 ERROR_CODE is the code that determines what to say in error messages.
3247 It is usually, but not always, the same as CODE.
3249 Note that the operands will never have enumeral types
3250 because either they have just had the default conversions performed
3251 or they have both just been converted to some other type in which
3252 the arithmetic is to be done.
3254 C++: must do special pointer arithmetic when implementing
3255 multiple inheritance, and deal with pointer to member functions. */
3258 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3259 enum tree_code code;
3260 tree orig_op0, orig_op1;
3261 enum tree_code error_code;
3264 register enum tree_code code0, code1;
3267 /* Expression code to give to the expression when it is built.
3268 Normally this is CODE, which is what the caller asked for,
3269 but in some special cases we change it. */
3270 register enum tree_code resultcode = code;
3272 /* Data type in which the computation is to be performed.
3273 In the simplest cases this is the common type of the arguments. */
3274 register tree result_type = NULL;
3276 /* Nonzero means operands have already been type-converted
3277 in whatever way is necessary.
3278 Zero means they need to be converted to RESULT_TYPE. */
3281 /* Nonzero means create the expression with this type, rather than
3283 tree build_type = 0;
3285 /* Nonzero means after finally constructing the expression
3286 convert it to this type. */
3287 tree final_type = 0;
3289 /* Nonzero if this is an operation like MIN or MAX which can
3290 safely be computed in short if both args are promoted shorts.
3291 Also implies COMMON.
3292 -1 indicates a bitwise operation; this makes a difference
3293 in the exact conditions for when it is safe to do the operation
3294 in a narrower mode. */
3297 /* Nonzero if this is a comparison operation;
3298 if both args are promoted shorts, compare the original shorts.
3299 Also implies COMMON. */
3300 int short_compare = 0;
3302 /* Nonzero if this is a right-shift operation, which can be computed on the
3303 original short and then promoted if the operand is a promoted short. */
3304 int short_shift = 0;
3306 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3309 /* Apply default conversions. */
3310 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3311 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3312 || code == TRUTH_XOR_EXPR)
3314 op0 = decay_conversion (orig_op0);
3315 op1 = decay_conversion (orig_op1);
3319 op0 = default_conversion (orig_op0);
3320 op1 = default_conversion (orig_op1);
3323 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3324 STRIP_TYPE_NOPS (op0);
3325 STRIP_TYPE_NOPS (op1);
3327 /* DTRT if one side is an overloaded function, but complain about it. */
3328 if (type_unknown_p (op0))
3330 tree t = instantiate_type (TREE_TYPE (op1), op0, 0);
3331 if (t != error_mark_node)
3333 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3338 if (type_unknown_p (op1))
3340 tree t = instantiate_type (TREE_TYPE (op0), op1, 0);
3341 if (t != error_mark_node)
3343 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3349 type0 = TREE_TYPE (op0);
3350 type1 = TREE_TYPE (op1);
3352 /* The expression codes of the data types of the arguments tell us
3353 whether the arguments are integers, floating, pointers, etc. */
3354 code0 = TREE_CODE (type0);
3355 code1 = TREE_CODE (type1);
3357 /* If an error was already reported for one of the arguments,
3358 avoid reporting another error. */
3360 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3361 return error_mark_node;
3366 /* Handle the pointer + int case. */
3367 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3368 return pointer_int_sum (PLUS_EXPR, op0, op1);
3369 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3370 return pointer_int_sum (PLUS_EXPR, op1, op0);
3376 /* Subtraction of two similar pointers.
3377 We must subtract them as integers, then divide by object size. */
3378 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3379 && comp_target_types (type0, type1, 1))
3380 return pointer_diff (op0, op1, common_type (type0, type1));
3381 /* Handle pointer minus int. Just like pointer plus int. */
3382 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3383 return pointer_int_sum (MINUS_EXPR, op0, op1);
3392 case TRUNC_DIV_EXPR:
3394 case FLOOR_DIV_EXPR:
3395 case ROUND_DIV_EXPR:
3396 case EXACT_DIV_EXPR:
3397 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3398 || code0 == COMPLEX_TYPE)
3399 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3400 || code1 == COMPLEX_TYPE))
3402 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3403 cp_warning ("division by zero in `%E / 0'", op0);
3404 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3405 cp_warning ("division by zero in `%E / 0.'", op0);
3407 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3408 resultcode = RDIV_EXPR;
3410 /* When dividing two signed integers, we have to promote to int.
3411 unless we divide by a constant != -1. Note that default
3412 conversion will have been performed on the operands at this
3413 point, so we have to dig out the original type to find out if
3415 shorten = ((TREE_CODE (op0) == NOP_EXPR
3416 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3417 || (TREE_CODE (op1) == INTEGER_CST
3418 && (TREE_INT_CST_LOW (op1) != -1
3419 || TREE_INT_CST_HIGH (op1) != -1)));
3425 case BIT_ANDTC_EXPR:
3428 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3430 /* If one operand is a constant, and the other is a short type
3431 that has been converted to an int,
3432 really do the work in the short type and then convert the
3433 result to int. If we are lucky, the constant will be 0 or 1
3434 in the short type, making the entire operation go away. */
3435 if (TREE_CODE (op0) == INTEGER_CST
3436 && TREE_CODE (op1) == NOP_EXPR
3437 && (TYPE_PRECISION (type1)
3438 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3439 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3441 final_type = result_type;
3442 op1 = TREE_OPERAND (op1, 0);
3443 result_type = TREE_TYPE (op1);
3445 if (TREE_CODE (op1) == INTEGER_CST
3446 && TREE_CODE (op0) == NOP_EXPR
3447 && (TYPE_PRECISION (type0)
3448 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3449 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3451 final_type = result_type;
3452 op0 = TREE_OPERAND (op0, 0);
3453 result_type = TREE_TYPE (op0);
3457 case TRUNC_MOD_EXPR:
3458 case FLOOR_MOD_EXPR:
3459 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3460 cp_warning ("division by zero in `%E %% 0'", op0);
3461 else if (code1 == REAL_TYPE && real_zerop (op1))
3462 cp_warning ("division by zero in `%E %% 0.'", op0);
3464 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3466 /* Although it would be tempting to shorten always here, that loses
3467 on some targets, since the modulo instruction is undefined if the
3468 quotient can't be represented in the computation mode. We shorten
3469 only if unsigned or if dividing by something we know != -1. */
3470 shorten = ((TREE_CODE (op0) == NOP_EXPR
3471 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3472 || (TREE_CODE (op1) == INTEGER_CST
3473 && (TREE_INT_CST_LOW (op1) != -1
3474 || TREE_INT_CST_HIGH (op1) != -1)));
3479 case TRUTH_ANDIF_EXPR:
3480 case TRUTH_ORIF_EXPR:
3481 case TRUTH_AND_EXPR:
3483 result_type = boolean_type_node;
3486 /* Shift operations: result has same type as first operand;
3487 always convert second operand to int.
3488 Also set SHORT_SHIFT if shifting rightward. */
3491 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3493 result_type = type0;
3494 if (TREE_CODE (op1) == INTEGER_CST)
3496 if (tree_int_cst_lt (op1, integer_zero_node))
3497 warning ("right shift count is negative");
3500 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3502 if (TREE_INT_CST_HIGH (op1) != 0
3503 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3504 >= TYPE_PRECISION (type0)))
3505 warning ("right shift count >= width of type");
3508 /* Convert the shift-count to an integer, regardless of
3509 size of value being shifted. */
3510 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3511 op1 = cp_convert (integer_type_node, op1);
3512 /* Avoid converting op1 to result_type later. */
3518 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3520 result_type = type0;
3521 if (TREE_CODE (op1) == INTEGER_CST)
3523 if (tree_int_cst_lt (op1, integer_zero_node))
3524 warning ("left shift count is negative");
3525 else if (TREE_INT_CST_HIGH (op1) != 0
3526 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3527 >= TYPE_PRECISION (type0)))
3528 warning ("left shift count >= width of type");
3530 /* Convert the shift-count to an integer, regardless of
3531 size of value being shifted. */
3532 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3533 op1 = cp_convert (integer_type_node, op1);
3534 /* Avoid converting op1 to result_type later. */
3541 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3543 result_type = type0;
3544 if (TREE_CODE (op1) == INTEGER_CST)
3546 if (tree_int_cst_lt (op1, integer_zero_node))
3547 warning ("%s rotate count is negative",
3548 (code == LROTATE_EXPR) ? "left" : "right");
3549 else if (TREE_INT_CST_HIGH (op1) != 0
3550 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3551 >= TYPE_PRECISION (type0)))
3552 warning ("%s rotate count >= width of type",
3553 (code == LROTATE_EXPR) ? "left" : "right");
3555 /* Convert the shift-count to an integer, regardless of
3556 size of value being shifted. */
3557 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3558 op1 = cp_convert (integer_type_node, op1);
3564 build_type = boolean_type_node;
3565 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3566 || code0 == COMPLEX_TYPE)
3567 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3568 || code1 == COMPLEX_TYPE))
3570 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3572 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3573 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3575 if (comp_target_types (type0, type1, 1))
3576 result_type = common_type (type0, type1);
3577 else if (tt0 == void_type_node)
3579 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3580 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3581 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3582 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3583 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3585 else if (tt1 == void_type_node)
3587 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3588 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3589 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3592 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3595 if (result_type == NULL_TREE)
3596 result_type = ptr_type_node;
3598 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3599 result_type = type0;
3600 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3601 result_type = type1;
3602 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3604 result_type = type0;
3605 error ("ANSI C++ forbids comparison between pointer and integer");
3607 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3609 result_type = type1;
3610 error ("ANSI C++ forbids comparison between pointer and integer");
3612 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3614 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3615 op1 = integer_zero_node;
3616 result_type = TREE_TYPE (op0);
3618 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3620 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3621 op1 = integer_zero_node;
3622 result_type = TREE_TYPE (op0);
3624 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3625 && same_type_p (type0, type1))
3627 /* The code we generate for the test is:
3629 (op0.index == op1.index
3630 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3631 || op0.pfn == op1.pfn)) */
3633 tree index0 = build_component_ref (op0, index_identifier,
3635 tree index1 = save_expr (build_component_ref (op1, index_identifier,
3637 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3638 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3639 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3640 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3642 tree integer_neg_one_node
3643 = build_binary_op (MINUS_EXPR, integer_zero_node,
3645 e1 = build_binary_op (EQ_EXPR, index0, index1);
3646 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3647 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3648 build_binary_op (EQ_EXPR, delta20, delta21));
3649 /* We can't use build_binary_op for this cmp because it would get
3650 confused by the ptr to method types and think we want pmfs. */
3651 e3 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3652 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3653 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3654 if (code == EQ_EXPR)
3656 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3658 else if (TYPE_PTRMEMFUNC_P (type0)
3659 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3661 tree index0 = build_component_ref (op0, index_identifier,
3664 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3665 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3666 tree delta21 = integer_zero_node;
3668 tree integer_neg_one_node
3669 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node);
3670 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3671 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3673 /* Map everything down one to make room for
3674 the null pointer to member. */
3675 index1 = size_binop (PLUS_EXPR,
3676 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3678 op1 = integer_zero_node;
3679 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3680 (TREE_TYPE (type1)));
3681 delta21 = DECL_FIELD_BITPOS (delta21);
3682 delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3683 size_int (BITS_PER_UNIT));
3684 delta21 = convert (sizetype, delta21);
3687 index1 = integer_neg_one_node;
3689 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3691 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3694 e1 = build_binary_op (EQ_EXPR, index0, index1);
3695 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3696 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3697 build_binary_op (EQ_EXPR, delta20, delta21));
3698 /* We can't use build_binary_op for this cmp because it would get
3699 confused by the ptr to method types and think we want pmfs. */
3700 e3 = build (EQ_EXPR, boolean_type_node, pfn0, op1);
3701 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3702 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3703 if (code == EQ_EXPR)
3705 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3707 else if (TYPE_PTRMEMFUNC_P (type1)
3708 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0))
3709 return build_binary_op (code, op1, op0);
3714 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3715 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3717 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3719 if (comp_target_types (type0, type1, 1))
3720 result_type = common_type (type0, type1);
3723 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3725 result_type = ptr_type_node;
3734 build_type = boolean_type_node;
3735 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3736 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3738 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3740 if (comp_target_types (type0, type1, 1))
3741 result_type = common_type (type0, type1);
3744 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3746 result_type = ptr_type_node;
3749 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3750 && integer_zerop (op1))
3751 result_type = type0;
3752 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3753 && integer_zerop (op0))
3754 result_type = type1;
3755 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3757 result_type = type0;
3758 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3760 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3762 result_type = type1;
3763 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3771 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3773 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3775 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3777 if (shorten || common || short_compare)
3778 result_type = common_type (type0, type1);
3780 /* For certain operations (which identify themselves by shorten != 0)
3781 if both args were extended from the same smaller type,
3782 do the arithmetic in that type and then extend.
3784 shorten !=0 and !=1 indicates a bitwise operation.
3785 For them, this optimization is safe only if
3786 both args are zero-extended or both are sign-extended.
3787 Otherwise, we might change the result.
3788 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3789 but calculated in (unsigned short) it would be (unsigned short)-1. */
3791 if (shorten && none_complex)
3793 int unsigned0, unsigned1;
3794 tree arg0 = get_narrower (op0, &unsigned0);
3795 tree arg1 = get_narrower (op1, &unsigned1);
3796 /* UNS is 1 if the operation to be done is an unsigned one. */
3797 int uns = TREE_UNSIGNED (result_type);
3800 final_type = result_type;
3802 /* Handle the case that OP0 does not *contain* a conversion
3803 but it *requires* conversion to FINAL_TYPE. */
3805 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3806 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3807 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3808 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3810 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3812 /* For bitwise operations, signedness of nominal type
3813 does not matter. Consider only how operands were extended. */
3817 /* Note that in all three cases below we refrain from optimizing
3818 an unsigned operation on sign-extended args.
3819 That would not be valid. */
3821 /* Both args variable: if both extended in same way
3822 from same width, do it in that width.
3823 Do it unsigned if args were zero-extended. */
3824 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3825 < TYPE_PRECISION (result_type))
3826 && (TYPE_PRECISION (TREE_TYPE (arg1))
3827 == TYPE_PRECISION (TREE_TYPE (arg0)))
3828 && unsigned0 == unsigned1
3829 && (unsigned0 || !uns))
3831 = signed_or_unsigned_type (unsigned0,
3832 common_type (TREE_TYPE (arg0),
3834 else if (TREE_CODE (arg0) == INTEGER_CST
3835 && (unsigned1 || !uns)
3836 && (TYPE_PRECISION (TREE_TYPE (arg1))
3837 < TYPE_PRECISION (result_type))
3838 && (type = signed_or_unsigned_type (unsigned1,
3840 int_fits_type_p (arg0, type)))
3842 else if (TREE_CODE (arg1) == INTEGER_CST
3843 && (unsigned0 || !uns)
3844 && (TYPE_PRECISION (TREE_TYPE (arg0))
3845 < TYPE_PRECISION (result_type))
3846 && (type = signed_or_unsigned_type (unsigned0,
3848 int_fits_type_p (arg1, type)))
3852 /* Shifts can be shortened if shifting right. */
3857 tree arg0 = get_narrower (op0, &unsigned_arg);
3859 final_type = result_type;
3861 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3862 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3864 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3865 /* We can shorten only if the shift count is less than the
3866 number of bits in the smaller type size. */
3867 && TREE_INT_CST_HIGH (op1) == 0
3868 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3869 /* If arg is sign-extended and then unsigned-shifted,
3870 we can simulate this with a signed shift in arg's type
3871 only if the extended result is at least twice as wide
3872 as the arg. Otherwise, the shift could use up all the
3873 ones made by sign-extension and bring in zeros.
3874 We can't optimize that case at all, but in most machines
3875 it never happens because available widths are 2**N. */
3876 && (!TREE_UNSIGNED (final_type)
3878 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3879 <= TYPE_PRECISION (result_type))))
3881 /* Do an unsigned shift if the operand was zero-extended. */
3883 = signed_or_unsigned_type (unsigned_arg,
3885 /* Convert value-to-be-shifted to that type. */
3886 if (TREE_TYPE (op0) != result_type)
3887 op0 = cp_convert (result_type, op0);
3892 /* Comparison operations are shortened too but differently.
3893 They identify themselves by setting short_compare = 1. */
3897 /* Don't write &op0, etc., because that would prevent op0
3898 from being kept in a register.
3899 Instead, make copies of the our local variables and
3900 pass the copies by reference, then copy them back afterward. */
3901 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3902 enum tree_code xresultcode = resultcode;
3904 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3906 return cp_convert (boolean_type_node, val);
3907 op0 = xop0, op1 = xop1;
3909 resultcode = xresultcode;
3912 if (short_compare && warn_sign_compare)
3914 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3915 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3917 int unsignedp0, unsignedp1;
3918 tree primop0 = get_narrower (op0, &unsignedp0);
3919 tree primop1 = get_narrower (op1, &unsignedp1);
3921 /* Check for comparison of different enum types. */
3922 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3923 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3924 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3925 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3927 cp_warning ("comparison between `%#T' and `%#T'",
3928 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3931 /* Give warnings for comparisons between signed and unsigned
3932 quantities that may fail. */
3933 /* Do the checking based on the original operand trees, so that
3934 casts will be considered, but default promotions won't be. */
3936 /* Do not warn if the comparison is being done in a signed type,
3937 since the signed type will only be chosen if it can represent
3938 all the values of the unsigned type. */
3939 if (! TREE_UNSIGNED (result_type))
3941 /* Do not warn if both operands are unsigned. */
3942 else if (op0_signed == op1_signed)
3944 /* Do not warn if the signed quantity is an unsuffixed
3945 integer literal (or some static constant expression
3946 involving such literals) and it is non-negative. */
3947 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3948 && tree_int_cst_sgn (orig_op0) >= 0)
3949 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3950 && tree_int_cst_sgn (orig_op1) >= 0))
3952 /* Do not warn if the comparison is an equality operation,
3953 the unsigned quantity is an integral constant and it does
3954 not use the most significant bit of result_type. */
3955 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3956 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3957 && int_fits_type_p (orig_op1,
3958 signed_type (result_type)))
3959 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3960 && int_fits_type_p (orig_op0,
3961 signed_type (result_type)))))
3964 warning ("comparison between signed and unsigned");
3966 /* Warn if two unsigned values are being compared in a size
3967 larger than their original size, and one (and only one) is the
3968 result of a `~' operator. This comparison will always fail.
3970 Also warn if one operand is a constant, and the constant does not
3971 have all bits set that are set in the ~ operand when it is
3974 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3975 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3977 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3978 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3979 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3980 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3982 if (TREE_CODE (primop0) == INTEGER_CST
3983 || TREE_CODE (primop1) == INTEGER_CST)
3986 HOST_WIDE_INT constant, mask;
3990 if (TREE_CODE (primop0) == INTEGER_CST)
3993 unsignedp = unsignedp1;
3994 constant = TREE_INT_CST_LOW (primop0);
3999 unsignedp = unsignedp0;
4000 constant = TREE_INT_CST_LOW (primop1);
4003 bits = TYPE_PRECISION (TREE_TYPE (primop));
4004 if (bits < TYPE_PRECISION (result_type)
4005 && bits < HOST_BITS_PER_LONG && unsignedp)
4007 mask = (~ (HOST_WIDE_INT) 0) << bits;
4008 if ((mask & constant) != mask)
4009 warning ("comparison of promoted ~unsigned with constant");
4012 else if (unsignedp0 && unsignedp1
4013 && (TYPE_PRECISION (TREE_TYPE (primop0))
4014 < TYPE_PRECISION (result_type))
4015 && (TYPE_PRECISION (TREE_TYPE (primop1))
4016 < TYPE_PRECISION (result_type)))
4017 warning ("comparison of promoted ~unsigned with unsigned");
4022 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4023 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4024 Then the expression will be built.
4025 It will be given type FINAL_TYPE if that is nonzero;
4026 otherwise, it will be given type RESULT_TYPE. */
4030 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
4031 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
4032 return error_mark_node;
4035 /* Issue warnings about peculiar, but legal, uses of NULL. */
4036 if (/* It's reasonable to use pointer values as operands of &&
4037 and ||, so NULL is no exception. */
4038 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4039 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4040 (orig_op0 == null_node
4041 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4042 /* Or vice versa. */
4043 || (orig_op1 == null_node
4044 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4045 /* Or, both are NULL and the operation was not a comparison. */
4046 || (orig_op0 == null_node && orig_op1 == null_node
4047 && code != EQ_EXPR && code != NE_EXPR)))
4048 /* Some sort of arithmetic operation involving NULL was
4049 performed. Note that pointer-difference and pointer-addition
4050 have already been handled above, and so we don't end up here in
4052 cp_warning ("NULL used in arithmetic");
4056 if (TREE_TYPE (op0) != result_type)
4057 op0 = cp_convert (result_type, op0);
4058 if (TREE_TYPE (op1) != result_type)
4059 op1 = cp_convert (result_type, op1);
4061 if (op0 == error_mark_node || op1 == error_mark_node)
4062 return error_mark_node;
4065 if (build_type == NULL_TREE)
4066 build_type = result_type;
4069 register tree result = build (resultcode, build_type, op0, op1);
4070 register tree folded;
4072 folded = fold (result);
4073 if (folded == result)
4074 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4075 if (final_type != 0)
4076 return cp_convert (final_type, folded);
4081 /* Return a tree for the sum or difference (RESULTCODE says which)
4082 of pointer PTROP and integer INTOP. */
4085 pointer_int_sum (resultcode, ptrop, intop)
4086 enum tree_code resultcode;
4087 register tree ptrop, intop;
4091 register tree result;
4092 register tree folded = fold (intop);
4094 /* The result is a pointer of the same type that is being added. */
4096 register tree result_type = TREE_TYPE (ptrop);
4098 if (!complete_type_or_else (result_type, ptrop))
4099 return error_mark_node;
4101 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4103 if (pedantic || warn_pointer_arith)
4104 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
4105 size_exp = integer_one_node;
4107 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4109 if (pedantic || warn_pointer_arith)
4110 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
4111 size_exp = integer_one_node;
4113 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4115 if (pedantic || warn_pointer_arith)
4116 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4117 size_exp = integer_one_node;
4119 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4121 if (pedantic || warn_pointer_arith)
4122 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4123 size_exp = integer_one_node;
4126 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4128 /* Needed to make OOPS V2R3 work. */
4130 if (TREE_CODE (intop) == INTEGER_CST
4131 && TREE_INT_CST_LOW (intop) == 0
4132 && TREE_INT_CST_HIGH (intop) == 0)
4135 /* If what we are about to multiply by the size of the elements
4136 contains a constant term, apply distributive law
4137 and multiply that constant term separately.
4138 This helps produce common subexpressions. */
4140 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4141 && ! TREE_CONSTANT (intop)
4142 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4143 && TREE_CONSTANT (size_exp))
4145 enum tree_code subcode = resultcode;
4146 if (TREE_CODE (intop) == MINUS_EXPR)
4147 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4148 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4149 intop = TREE_OPERAND (intop, 0);
4152 /* Convert the integer argument to a type the same size as sizetype
4153 so the multiply won't overflow spuriously. */
4155 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4156 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4158 /* Replace the integer argument with a suitable product by the object size.
4159 Do this multiplication as signed, then convert to the appropriate
4160 pointer type (actually unsigned integral). */
4162 intop = cp_convert (result_type,
4163 build_binary_op (MULT_EXPR, intop,
4164 cp_convert (TREE_TYPE (intop),
4167 /* Create the sum or difference. */
4169 result = build (resultcode, result_type, ptrop, intop);
4171 folded = fold (result);
4172 if (folded == result)
4173 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4177 /* Return a tree for the difference of pointers OP0 and OP1.
4178 The resulting tree has type int. */
4181 pointer_diff (op0, op1, ptrtype)
4182 register tree op0, op1;
4183 register tree ptrtype;
4185 register tree result, folded;
4186 tree restype = ptrdiff_type_node;
4187 tree target_type = TREE_TYPE (ptrtype);
4189 if (!complete_type_or_else (target_type, NULL_TREE))
4190 return error_mark_node;
4192 if (pedantic || warn_pointer_arith)
4194 if (TREE_CODE (target_type) == VOID_TYPE)
4195 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4196 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4197 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4198 if (TREE_CODE (target_type) == METHOD_TYPE)
4199 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4200 if (TREE_CODE (target_type) == OFFSET_TYPE)
4201 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4204 /* First do the subtraction as integers;
4205 then drop through to build the divide operator. */
4207 op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4208 cp_convert (restype, op1));
4210 /* This generates an error if op1 is a pointer to an incomplete type. */
4211 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4212 error ("arithmetic on pointer to an incomplete type");
4214 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4215 || TREE_CODE (target_type) == FUNCTION_TYPE
4216 || TREE_CODE (target_type) == METHOD_TYPE
4217 || TREE_CODE (target_type) == OFFSET_TYPE)
4219 : size_in_bytes (target_type));
4221 /* Do the division. */
4223 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4225 folded = fold (result);
4226 if (folded == result)
4227 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4231 /* Handle the case of taking the address of a COMPONENT_REF.
4232 Called by `build_unary_op'.
4234 ARG is the COMPONENT_REF whose address we want.
4235 ARGTYPE is the pointer type that this address should have. */
4238 build_component_addr (arg, argtype)
4241 tree field = TREE_OPERAND (arg, 1);
4242 tree basetype = decl_type_context (field);
4243 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4245 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4247 if (DECL_C_BIT_FIELD (field))
4249 cp_error ("attempt to take address of bit-field structure member `%D'",
4251 return error_mark_node;
4254 if (TREE_CODE (field) == FIELD_DECL
4255 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4257 /* Can't convert directly to ARGTYPE, since that
4258 may have the same pointer type as one of our
4260 rval = build1 (NOP_EXPR, argtype,
4261 convert_pointer_to (basetype, rval));
4262 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4265 /* This conversion is harmless. */
4266 rval = convert_force (argtype, rval, 0);
4268 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4270 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4271 size_int (BITS_PER_UNIT));
4272 int flag = TREE_CONSTANT (rval);
4273 offset = convert (sizetype, offset);
4274 rval = fold (build (PLUS_EXPR, argtype,
4275 rval, cp_convert (argtype, offset)));
4276 TREE_CONSTANT (rval) = flag;
4281 /* Construct and perhaps optimize a tree representation
4282 for a unary operation. CODE, a tree_code, specifies the operation
4283 and XARG is the operand. */
4286 build_x_unary_op (code, xarg)
4287 enum tree_code code;
4290 if (processing_template_decl)
4291 return build_min_nt (code, xarg, NULL_TREE);
4293 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4295 if (code == ADDR_EXPR
4296 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4297 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4298 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4299 || (TREE_CODE (xarg) == OFFSET_REF)))
4300 /* don't look for a function */;
4305 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4306 NULL_TREE, NULL_TREE);
4307 if (rval || code != ADDR_EXPR)
4311 if (code == ADDR_EXPR)
4313 if (TREE_CODE (xarg) == TARGET_EXPR)
4314 warning ("taking address of temporary");
4317 return build_unary_op (code, xarg, 0);
4320 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4323 condition_conversion (expr)
4327 if (processing_template_decl)
4329 t = perform_implicit_conversion (boolean_type_node, expr);
4330 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4334 /* C++: Must handle pointers to members.
4336 Perhaps type instantiation should be extended to handle conversion
4337 from aggregates to types we don't yet know we want? (Or are those
4338 cases typically errors which should be reported?)
4340 NOCONVERT nonzero suppresses the default promotions
4341 (such as from short to int). */
4344 build_unary_op (code, xarg, noconvert)
4345 enum tree_code code;
4349 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4350 register tree arg = xarg;
4351 register tree argtype = 0;
4352 const char *errstring = NULL;
4355 if (arg == error_mark_node)
4356 return error_mark_node;
4361 /* This is used for unary plus, because a CONVERT_EXPR
4362 is enough to prevent anybody from looking inside for
4363 associativity, but won't generate any code. */
4364 if (!(arg = build_expr_type_conversion
4365 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4366 errstring = "wrong type argument to unary plus";
4370 arg = default_conversion (arg);
4371 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4372 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4377 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4378 errstring = "wrong type argument to unary minus";
4379 else if (!noconvert)
4380 arg = default_conversion (arg);
4384 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4388 arg = default_conversion (arg);
4390 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4392 errstring = "wrong type argument to bit-complement";
4393 else if (!noconvert)
4394 arg = default_conversion (arg);
4398 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4399 errstring = "wrong type argument to abs";
4400 else if (!noconvert)
4401 arg = default_conversion (arg);
4405 /* Conjugating a real value is a no-op, but allow it anyway. */
4406 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4407 errstring = "wrong type argument to conjugation";
4408 else if (!noconvert)
4409 arg = default_conversion (arg);
4412 case TRUTH_NOT_EXPR:
4413 arg = cp_convert (boolean_type_node, arg);
4414 val = invert_truthvalue (arg);
4415 if (arg != error_mark_node)
4417 errstring = "in argument to unary !";
4424 if (TREE_CODE (arg) == COMPLEX_CST)
4425 return TREE_REALPART (arg);
4426 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4427 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4432 if (TREE_CODE (arg) == COMPLEX_CST)
4433 return TREE_IMAGPART (arg);
4434 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4435 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4437 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4439 case PREINCREMENT_EXPR:
4440 case POSTINCREMENT_EXPR:
4441 case PREDECREMENT_EXPR:
4442 case POSTDECREMENT_EXPR:
4443 /* Handle complex lvalues (when permitted)
4444 by reduction to simpler cases. */
4446 val = unary_complex_lvalue (code, arg);
4450 /* Increment or decrement the real part of the value,
4451 and don't change the imaginary part. */
4452 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4456 arg = stabilize_reference (arg);
4457 real = build_unary_op (REALPART_EXPR, arg, 1);
4458 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4459 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4460 build_unary_op (code, real, 1), imag);
4463 /* Report invalid types. */
4465 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4468 if (code == PREINCREMENT_EXPR)
4469 errstring ="no pre-increment operator for type";
4470 else if (code == POSTINCREMENT_EXPR)
4471 errstring ="no post-increment operator for type";
4472 else if (code == PREDECREMENT_EXPR)
4473 errstring ="no pre-decrement operator for type";
4475 errstring ="no post-decrement operator for type";
4479 /* Report something read-only. */
4481 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4482 || TREE_READONLY (arg))
4483 readonly_error (arg, ((code == PREINCREMENT_EXPR
4484 || code == POSTINCREMENT_EXPR)
4485 ? "increment" : "decrement"),
4490 tree result_type = TREE_TYPE (arg);
4492 arg = get_unwidened (arg, 0);
4493 argtype = TREE_TYPE (arg);
4495 /* ARM $5.2.5 last annotation says this should be forbidden. */
4496 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4497 pedwarn ("ANSI C++ forbids %sing an enum",
4498 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4499 ? "increment" : "decrement");
4501 /* Compute the increment. */
4503 if (TREE_CODE (argtype) == POINTER_TYPE)
4505 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4506 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4507 cp_error ("cannot %s a pointer to incomplete type `%T'",
4508 ((code == PREINCREMENT_EXPR
4509 || code == POSTINCREMENT_EXPR)
4510 ? "increment" : "decrement"), TREE_TYPE (argtype));
4511 else if ((pedantic || warn_pointer_arith)
4512 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4513 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4514 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4515 ((code == PREINCREMENT_EXPR
4516 || code == POSTINCREMENT_EXPR)
4517 ? "increment" : "decrement"), argtype);
4518 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4521 inc = integer_one_node;
4523 inc = cp_convert (argtype, inc);
4525 /* Handle incrementing a cast-expression. */
4527 switch (TREE_CODE (arg))
4532 case FIX_TRUNC_EXPR:
4533 case FIX_FLOOR_EXPR:
4534 case FIX_ROUND_EXPR:
4537 tree incremented, modify, value, compound;
4538 if (! lvalue_p (arg) && pedantic)
4539 pedwarn ("cast to non-reference type used as lvalue");
4540 arg = stabilize_reference (arg);
4541 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4544 value = save_expr (arg);
4545 incremented = build (((code == PREINCREMENT_EXPR
4546 || code == POSTINCREMENT_EXPR)
4547 ? PLUS_EXPR : MINUS_EXPR),
4548 argtype, value, inc);
4549 TREE_SIDE_EFFECTS (incremented) = 1;
4551 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4552 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4554 /* Eliminate warning about unused result of + or -. */
4555 TREE_NO_UNUSED_WARNING (compound) = 1;
4563 /* Complain about anything else that is not a true lvalue. */
4564 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4565 || code == POSTINCREMENT_EXPR)
4566 ? "increment" : "decrement")))
4567 return error_mark_node;
4569 /* Forbid using -- on `bool'. */
4570 if (TREE_TYPE (arg) == boolean_type_node)
4572 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4574 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4575 return error_mark_node;
4578 /* This will only work if someone can convince Kenner to accept
4579 my patch to expand_increment. (jason) */
4580 val = build (code, TREE_TYPE (arg), arg, inc);
4582 if (code == POSTINCREMENT_EXPR)
4584 arg = stabilize_reference (arg);
4585 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4587 TREE_SIDE_EFFECTS (val) = 1;
4588 arg = save_expr (arg);
4589 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4590 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4593 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4598 val = build (code, TREE_TYPE (arg), arg, inc);
4600 TREE_SIDE_EFFECTS (val) = 1;
4601 return cp_convert (result_type, val);
4605 /* Note that this operation never does default_conversion
4606 regardless of NOCONVERT. */
4608 argtype = lvalue_type (arg);
4609 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4613 build_pointer_type (TREE_TYPE (argtype)), arg);
4614 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4617 else if (pedantic && DECL_MAIN_P (arg))
4619 pedwarn ("taking address of function `main'");
4621 /* Let &* cancel out to simplify resulting code. */
4622 if (TREE_CODE (arg) == INDIRECT_REF)
4624 /* We don't need to have `current_class_ptr' wrapped in a
4625 NON_LVALUE_EXPR node. */
4626 if (arg == current_class_ref)
4627 return current_class_ptr;
4629 arg = TREE_OPERAND (arg, 0);
4630 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4634 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4635 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4637 else if (lvalue_p (arg))
4638 /* Don't let this be an lvalue. */
4639 return non_lvalue (arg);
4643 /* For &x[y], return x+y */
4644 if (TREE_CODE (arg) == ARRAY_REF)
4646 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4647 return error_mark_node;
4648 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4649 TREE_OPERAND (arg, 1));
4652 /* Uninstantiated types are all functions. Taking the
4653 address of a function is a no-op, so just return the
4656 if (TREE_CODE (arg) == IDENTIFIER_NODE
4657 && IDENTIFIER_OPNAME_P (arg))
4659 my_friendly_abort (117);
4660 /* We don't know the type yet, so just work around the problem.
4661 We know that this will resolve to an lvalue. */
4662 return build1 (ADDR_EXPR, unknown_type_node, arg);
4665 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4666 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4668 /* They're trying to take the address of a unique non-static
4669 member function. This is ill-formed, but let's try to DTRT.
4670 Note: We only handle unique functions here because we don't
4671 want to complain if there's a static overload; non-unique
4672 cases will be handled by instantiate_type. But we need to
4673 handle this case here to allow casts on the resulting PMF. */
4675 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4676 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4678 if (! flag_ms_extensions)
4680 if (current_class_type
4681 && TREE_OPERAND (arg, 0) == current_class_ref)
4682 /* An expression like &memfn. */
4683 pedwarn ("taking the address of a non-static member function");
4685 pedwarn ("taking the address of a bound member function");
4688 (" to form a pointer to member function, say `&%T::%D'",
4692 arg = build_offset_ref (base, name);
4695 if (type_unknown_p (arg))
4696 return build1 (ADDR_EXPR, unknown_type_node, arg);
4698 /* Handle complex lvalues (when permitted)
4699 by reduction to simpler cases. */
4700 val = unary_complex_lvalue (code, arg);
4704 switch (TREE_CODE (arg))
4709 case FIX_TRUNC_EXPR:
4710 case FIX_FLOOR_EXPR:
4711 case FIX_ROUND_EXPR:
4713 if (! lvalue_p (arg) && pedantic)
4714 pedwarn ("taking the address of a cast to non-reference type");
4721 /* Allow the address of a constructor if all the elements
4723 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4724 && TREE_CONSTANT (arg))
4726 /* Anything not already handled and not a true memory reference
4728 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4729 && TREE_CODE (argtype) != METHOD_TYPE
4730 && !lvalue_or_else (arg, "unary `&'"))
4731 return error_mark_node;
4733 if (argtype != error_mark_node)
4734 argtype = build_pointer_type (argtype);
4736 if (mark_addressable (arg) == 0)
4737 return error_mark_node;
4742 if (TREE_CODE (arg) == COMPONENT_REF)
4743 addr = build_component_addr (arg, argtype);
4745 addr = build1 (ADDR_EXPR, argtype, arg);
4747 /* Address of a static or external variable or
4748 function counts as a constant */
4750 TREE_CONSTANT (addr) = 1;
4752 if (TREE_CODE (argtype) == POINTER_TYPE
4753 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4755 build_ptrmemfunc_type (argtype);
4756 addr = build_ptrmemfunc (argtype, addr, 0);
4769 argtype = TREE_TYPE (arg);
4770 return fold (build1 (code, argtype, arg));
4774 return error_mark_node;
4778 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4779 convert ARG with the same conversions in the same order
4780 and return the result. */
4783 convert_sequence (conversions, arg)
4787 switch (TREE_CODE (conversions))
4792 case FIX_TRUNC_EXPR:
4793 case FIX_FLOOR_EXPR:
4794 case FIX_ROUND_EXPR:
4796 return cp_convert (TREE_TYPE (conversions),
4797 convert_sequence (TREE_OPERAND (conversions, 0),
4806 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4807 for certain kinds of expressions which are not really lvalues
4808 but which we can accept as lvalues.
4810 If ARG is not a kind of expression we can handle, return zero. */
4813 unary_complex_lvalue (code, arg)
4814 enum tree_code code;
4817 /* Handle (a, b) used as an "lvalue". */
4818 if (TREE_CODE (arg) == COMPOUND_EXPR)
4820 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4821 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4822 TREE_OPERAND (arg, 0), real_result);
4825 /* Handle (a ? b : c) used as an "lvalue". */
4826 if (TREE_CODE (arg) == COND_EXPR
4827 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4828 return rationalize_conditional_expr (code, arg);
4830 if (TREE_CODE (arg) == MODIFY_EXPR
4831 || TREE_CODE (arg) == PREINCREMENT_EXPR
4832 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4833 return unary_complex_lvalue
4834 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4835 arg, TREE_OPERAND (arg, 0)));
4837 if (code != ADDR_EXPR)
4840 /* Handle (a = b) used as an "lvalue" for `&'. */
4841 if (TREE_CODE (arg) == MODIFY_EXPR
4842 || TREE_CODE (arg) == INIT_EXPR)
4844 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4845 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4846 TREE_NO_UNUSED_WARNING (arg) = 1;
4850 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4851 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4852 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4854 /* The representation of something of type OFFSET_TYPE
4855 is really the representation of a pointer to it.
4856 Here give the representation its true type. */
4859 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4861 if (TREE_CODE (arg) != OFFSET_REF)
4864 t = TREE_OPERAND (arg, 1);
4866 /* Check all this code for right semantics. */
4867 if (TREE_CODE (t) == FUNCTION_DECL)
4869 if (DECL_DESTRUCTOR_P (t))
4870 cp_error ("taking address of destructor");
4871 return build_unary_op (ADDR_EXPR, t, 0);
4873 if (TREE_CODE (t) == VAR_DECL)
4874 return build_unary_op (ADDR_EXPR, t, 0);
4879 if (TREE_OPERAND (arg, 0)
4880 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4881 && TREE_CODE (t) != FIELD_DECL)
4883 cp_error ("taking address of bound pointer-to-member expression");
4884 return error_mark_node;
4887 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4888 type = build_pointer_type (type);
4890 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4896 /* We permit compiler to make function calls returning
4897 objects of aggregate type look like lvalues. */
4901 if (TREE_CODE (targ) == SAVE_EXPR)
4902 targ = TREE_OPERAND (targ, 0);
4904 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4906 if (TREE_CODE (arg) == SAVE_EXPR)
4909 targ = build_cplus_new (TREE_TYPE (arg), arg);
4910 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4913 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4914 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4915 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4918 /* Don't let anything else be handled specially. */
4922 /* Mark EXP saying that we need to be able to take the
4923 address of it; it should not be allocated in a register.
4924 Value is 1 if successful.
4926 C++: we do not allow `current_class_ptr' to be addressable. */
4929 mark_addressable (exp)
4932 register tree x = exp;
4934 if (TREE_ADDRESSABLE (x) == 1)
4938 switch (TREE_CODE (x))
4945 x = TREE_OPERAND (x, 0);
4949 if (x == current_class_ptr)
4951 if (! flag_this_is_variable)
4952 error ("address of `this' not available");
4953 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4954 put_var_into_stack (x);
4958 if (TREE_STATIC (x) && TREE_READONLY (x)
4959 && DECL_RTL (x) != 0
4960 && ! DECL_IN_MEMORY_P (x))
4962 /* We thought this would make a good constant variable,
4963 but we were wrong. */
4964 push_permanent_obstack ();
4966 TREE_ASM_WRITTEN (x) = 0;
4968 rest_of_decl_compilation (x, 0,
4969 !DECL_FUNCTION_SCOPE_P (x),
4971 TREE_ADDRESSABLE (x) = 1;
4977 /* Caller should not be trying to mark initialized
4978 constant fields addressable. */
4979 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4980 || DECL_IN_AGGR_P (x) == 0
4982 || DECL_EXTERNAL (x), 314);
4986 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4987 && !DECL_ARTIFICIAL (x) && extra_warnings)
4988 cp_warning ("address requested for `%D', which is declared `register'",
4990 put_var_into_stack (x);
4991 TREE_ADDRESSABLE (x) = 1;
4995 if (DECL_LANG_SPECIFIC (x) != 0)
4997 x = DECL_MAIN_VARIANT (x);
4998 /* We have to test both conditions here. The first may be
4999 non-zero in the case of processing a default function. The
5000 second may be non-zero in the case of a template function. */
5001 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
5004 TREE_ADDRESSABLE (x) = 1;
5006 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
5010 TREE_ADDRESSABLE (x) = 1;
5014 TREE_ADDRESSABLE (x) = 1;
5015 mark_addressable (TREE_OPERAND (x, 0));
5023 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5026 build_x_conditional_expr (ifexp, op1, op2)
5027 tree ifexp, op1, op2;
5029 if (processing_template_decl)
5030 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5032 return build_conditional_expr (ifexp, op1, op2);
5035 /* Handle overloading of the ',' operator when needed. Otherwise,
5036 this function just builds an expression list. */
5039 build_x_compound_expr (list)
5042 tree rest = TREE_CHAIN (list);
5045 if (processing_template_decl)
5046 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5048 if (rest == NULL_TREE)
5049 return build_compound_expr (list);
5051 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5052 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5054 return build_x_compound_expr (tree_cons (NULL_TREE, result,
5055 TREE_CHAIN (rest)));
5057 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5059 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5060 /* the left-hand operand of a comma expression is like an expression
5061 statement: we should warn if it doesn't have any side-effects,
5062 unless it was explicitly cast to (void). */
5063 if ((extra_warnings || warn_unused)
5064 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5065 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5066 warning("left-hand operand of comma expression has no effect");
5068 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5069 else if (warn_unused)
5070 warn_if_unused_value (TREE_VALUE(list));
5073 return build_compound_expr
5074 (tree_cons (NULL_TREE, TREE_VALUE (list),
5075 build_expr_list (NULL_TREE,
5076 build_x_compound_expr (rest))));
5079 /* Given a list of expressions, return a compound expression
5080 that performs them all and returns the value of the last of them. */
5083 build_compound_expr (list)
5089 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5090 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5092 if (TREE_CHAIN (list) == 0)
5094 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5095 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5096 if (TREE_CODE (list) == NOP_EXPR
5097 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5098 list = TREE_OPERAND (list, 0);
5100 return TREE_VALUE (list);
5103 first = TREE_VALUE (list);
5104 first = convert_to_void (first, "lhs of comma");
5105 if (first == error_mark_node)
5106 return error_mark_node;
5108 rest = build_compound_expr (TREE_CHAIN (list));
5109 if (rest == error_mark_node)
5110 return error_mark_node;
5112 /* When pedantic, a compound expression cannot be a constant expression. */
5113 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5116 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5117 break_out_cleanups (first), rest);
5121 build_static_cast (type, expr)
5127 if (type == error_mark_node || expr == error_mark_node)
5128 return error_mark_node;
5130 if (TREE_CODE (expr) == OFFSET_REF)
5131 expr = resolve_offset_ref (expr);
5133 if (processing_template_decl)
5135 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5139 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5140 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5141 if (TREE_CODE (type) != REFERENCE_TYPE
5142 && TREE_CODE (expr) == NOP_EXPR
5143 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5144 expr = TREE_OPERAND (expr, 0);
5146 if (TREE_CODE (type) == VOID_TYPE)
5148 expr = convert_to_void (expr, /*implicit=*/NULL);
5152 if (TREE_CODE (type) == REFERENCE_TYPE)
5153 return (convert_from_reference
5154 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5155 LOOKUP_COMPLAIN, NULL_TREE)));
5157 if (IS_AGGR_TYPE (type))
5158 return build_cplus_new
5159 (type, (build_method_call
5160 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5161 TYPE_BINFO (type), LOOKUP_NORMAL)));
5163 expr = decay_conversion (expr);
5164 intype = TREE_TYPE (expr);
5166 /* FIXME handle casting to array type. */
5169 if (can_convert_arg (type, intype, expr))
5171 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5174 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5175 && at_least_as_qualified_p (TREE_TYPE (type),
5177 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5178 && ! TREE_VIA_VIRTUAL (binfo))
5181 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5183 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5184 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))))
5185 && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type)),
5186 TREE_TYPE (TREE_TYPE (intype)))
5187 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5188 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5189 && ! TREE_VIA_VIRTUAL (binfo))
5192 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5193 && TREE_CODE (type) != ARRAY_TYPE
5194 && TREE_CODE (type) != FUNCTION_TYPE
5195 && can_convert (intype, type))
5198 /* [expr.static.cast]
5200 The static_cast operator shall not be used to cast away
5202 if (ok && casts_away_constness (intype, type))
5204 cp_error ("static_cast from `%T' to `%T' casts away constness",
5206 return error_mark_node;
5210 return build_c_cast (type, expr);
5212 cp_error ("static_cast from `%T' to `%T'", intype, type);
5213 return error_mark_node;
5217 build_reinterpret_cast (type, expr)
5222 if (type == error_mark_node || expr == error_mark_node)
5223 return error_mark_node;
5225 if (TREE_CODE (expr) == OFFSET_REF)
5226 expr = resolve_offset_ref (expr);
5228 if (processing_template_decl)
5230 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5234 if (TREE_CODE (type) != REFERENCE_TYPE)
5236 expr = decay_conversion (expr);
5238 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5239 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5240 if (TREE_CODE (expr) == NOP_EXPR
5241 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5242 expr = TREE_OPERAND (expr, 0);
5245 intype = TREE_TYPE (expr);
5247 if (TREE_CODE (type) == REFERENCE_TYPE)
5249 if (! real_lvalue_p (expr))
5251 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5252 return error_mark_node;
5254 expr = build_unary_op (ADDR_EXPR, expr, 0);
5255 if (expr != error_mark_node)
5256 expr = build_reinterpret_cast
5257 (build_pointer_type (TREE_TYPE (type)), expr);
5258 if (expr != error_mark_node)
5259 expr = build_indirect_ref (expr, 0);
5262 else if (same_type_p (TYPE_MAIN_VARIANT (intype),
5263 TYPE_MAIN_VARIANT (type)))
5264 return build_static_cast (type, expr);
5266 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5267 || TREE_CODE (intype) == ENUMERAL_TYPE))
5269 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5271 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5272 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5275 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5276 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5278 if (TREE_READONLY_DECL_P (expr))
5279 expr = decl_constant_value (expr);
5280 return fold (build1 (NOP_EXPR, type, expr));
5282 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5283 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5285 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5286 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5289 if (TREE_READONLY_DECL_P (expr))
5290 expr = decl_constant_value (expr);
5291 return fold (build1 (NOP_EXPR, type, expr));
5293 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5294 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5296 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5297 if (TREE_READONLY_DECL_P (expr))
5298 expr = decl_constant_value (expr);
5299 return fold (build1 (NOP_EXPR, type, expr));
5303 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5304 return error_mark_node;
5307 return cp_convert (type, expr);
5311 build_const_cast (type, expr)
5316 if (type == error_mark_node || expr == error_mark_node)
5317 return error_mark_node;
5319 if (TREE_CODE (expr) == OFFSET_REF)
5320 expr = resolve_offset_ref (expr);
5322 if (processing_template_decl)
5324 tree t = build_min (CONST_CAST_EXPR, type, expr);
5328 if (!POINTER_TYPE_P (type))
5330 cp_error ("`%T' is not a pointer, reference, or pointer-to-data-member type",
5332 cp_error ("as required by const_cast");
5334 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5336 cp_error ("`%T' is a pointer or reference to a function type",
5338 cp_error ("which is forbidden by const_cast");
5339 return error_mark_node;
5342 if (TREE_CODE (type) != REFERENCE_TYPE)
5344 expr = decay_conversion (expr);
5346 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5347 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5348 if (TREE_CODE (expr) == NOP_EXPR
5349 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5350 expr = TREE_OPERAND (expr, 0);
5353 intype = TREE_TYPE (expr);
5355 if (same_type_p (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type)))
5356 return build_static_cast (type, expr);
5357 else if (TREE_CODE (type) == REFERENCE_TYPE)
5359 if (! real_lvalue_p (expr))
5361 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5362 return error_mark_node;
5365 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5367 expr = build_unary_op (ADDR_EXPR, expr, 0);
5368 expr = build1 (NOP_EXPR, type, expr);
5369 return convert_from_reference (expr);
5372 else if (TREE_CODE (type) == POINTER_TYPE
5373 && TREE_CODE (intype) == POINTER_TYPE
5374 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5375 return cp_convert (type, expr);
5377 cp_error ("const_cast from `%T' to `%T'", intype, type);
5378 return error_mark_node;
5381 /* Build an expression representing a cast to type TYPE of expression EXPR.
5383 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5384 when doing the cast. */
5387 build_c_cast (type, expr)
5390 register tree value = expr;
5393 if (type == error_mark_node || expr == error_mark_node)
5394 return error_mark_node;
5396 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5397 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5398 if (TREE_CODE (type) != REFERENCE_TYPE
5399 && TREE_CODE (value) == NOP_EXPR
5400 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5401 value = TREE_OPERAND (value, 0);
5403 if (TREE_CODE (value) == OFFSET_REF)
5404 value = resolve_offset_ref (value);
5406 if (TREE_CODE (type) == ARRAY_TYPE)
5408 /* Allow casting from T1* to T2[] because Cfront allows it.
5409 NIHCL uses it. It is not valid ANSI C however, and hence, not
5411 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5414 pedwarn ("ANSI C++ forbids casting to an array type");
5415 type = build_pointer_type (TREE_TYPE (type));
5419 error ("ANSI C++ forbids casting to an array type");
5420 return error_mark_node;
5424 if (TREE_CODE (type) == FUNCTION_TYPE
5425 || TREE_CODE (type) == METHOD_TYPE)
5427 cp_error ("casting to function type `%T'", type);
5428 return error_mark_node;
5431 if (processing_template_decl)
5433 tree t = build_min (CAST_EXPR, type,
5434 min_tree_cons (NULL_TREE, value, NULL_TREE));
5438 if (TREE_CODE (type) == VOID_TYPE)
5440 /* Conversion to void does not cause any of the normal function to
5441 * pointer, array to pointer and lvalue to rvalue decays. */
5443 value = convert_to_void (value, /*implicit=*/NULL);
5446 /* Convert functions and arrays to pointers and
5447 convert references to their expanded types,
5448 but don't convert any other types. If, however, we are
5449 casting to a class type, there's no reason to do this: the
5450 cast will only succeed if there is a converting constructor,
5451 and the default conversions will be done at that point. In
5452 fact, doing the default conversion here is actually harmful
5456 struct S { S(const A&); };
5458 since we don't want the array-to-pointer conversion done. */
5459 if (!IS_AGGR_TYPE (type))
5461 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5462 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5463 /* Don't do the default conversion on a ->* expression. */
5464 && ! (TREE_CODE (type) == POINTER_TYPE
5465 && bound_pmf_p (value)))
5466 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5467 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5468 value = default_conversion (value);
5470 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5471 /* However, even for class types, we still need to strip away
5472 the reference type, since the call to convert_force below
5473 does not expect the input expression to be of reference
5475 value = convert_from_reference (value);
5477 otype = TREE_TYPE (value);
5479 /* Optionally warn about potentially worrisome casts. */
5482 && TREE_CODE (type) == POINTER_TYPE
5483 && TREE_CODE (otype) == POINTER_TYPE
5484 && !at_least_as_qualified_p (TREE_TYPE (type),
5486 cp_warning ("cast discards qualifiers from pointer target type");
5488 /* Warn about possible alignment problems. */
5489 if (STRICT_ALIGNMENT && warn_cast_align
5490 && TREE_CODE (type) == POINTER_TYPE
5491 && TREE_CODE (otype) == POINTER_TYPE
5492 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5493 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5494 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5495 warning ("cast increases required alignment of target type");
5498 /* We should see about re-enabling these, they seem useful to
5500 if (TREE_CODE (type) == INTEGER_TYPE
5501 && TREE_CODE (otype) == POINTER_TYPE
5502 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5503 warning ("cast from pointer to integer of different size");
5505 if (TREE_CODE (type) == POINTER_TYPE
5506 && TREE_CODE (otype) == INTEGER_TYPE
5507 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5508 /* Don't warn about converting 0 to pointer,
5509 provided the 0 was explicit--not cast or made by folding. */
5510 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5511 warning ("cast to pointer from integer of different size");
5514 if (TREE_CODE (type) == REFERENCE_TYPE)
5515 value = (convert_from_reference
5516 (convert_to_reference (type, value, CONV_C_CAST,
5517 LOOKUP_COMPLAIN, NULL_TREE)));
5522 if (TREE_READONLY_DECL_P (value))
5523 value = decl_constant_value (value);
5526 value = convert_force (type, value, CONV_C_CAST);
5528 /* Ignore any integer overflow caused by the cast. */
5529 if (TREE_CODE (value) == INTEGER_CST)
5531 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5532 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5536 /* Always produce some operator for an explicit cast,
5537 so we can tell (for -pedantic) that the cast is no lvalue. */
5538 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5539 && real_lvalue_p (value))
5540 value = non_lvalue (value);
5545 /* Build an assignment expression of lvalue LHS from value RHS.
5546 MODIFYCODE is the code for a binary operator that we use
5547 to combine the old value of LHS with RHS to get the new value.
5548 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5550 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5553 build_modify_expr (lhs, modifycode, rhs)
5555 enum tree_code modifycode;
5558 register tree result;
5560 tree lhstype = TREE_TYPE (lhs);
5561 tree olhstype = lhstype;
5564 /* Avoid duplicate error messages from operands that had errors. */
5565 if (lhs == error_mark_node || rhs == error_mark_node)
5566 return error_mark_node;
5568 /* Types that aren't fully specified cannot be used in assignments. */
5569 lhs = require_complete_type (lhs);
5573 /* Handle control structure constructs used as "lvalues". */
5575 switch (TREE_CODE (lhs))
5577 /* Handle --foo = 5; as these are valid constructs in C++ */
5578 case PREDECREMENT_EXPR:
5579 case PREINCREMENT_EXPR:
5580 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5581 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5582 stabilize_reference (TREE_OPERAND (lhs, 0)),
5583 TREE_OPERAND (lhs, 1));
5584 return build (COMPOUND_EXPR, lhstype,
5586 build_modify_expr (TREE_OPERAND (lhs, 0),
5589 /* Handle (a, b) used as an "lvalue". */
5591 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5593 if (newrhs == error_mark_node)
5594 return error_mark_node;
5595 return build (COMPOUND_EXPR, lhstype,
5596 TREE_OPERAND (lhs, 0), newrhs);
5599 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5600 if (newrhs == error_mark_node)
5601 return error_mark_node;
5602 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5604 /* Handle (a ? b : c) used as an "lvalue". */
5606 rhs = save_expr (rhs);
5608 /* Produce (a ? (b = rhs) : (c = rhs))
5609 except that the RHS goes through a save-expr
5610 so the code to compute it is only emitted once. */
5613 /* Check this here to avoid odd errors when trying to convert
5614 a throw to the type of the COND_EXPR. */
5615 if (!lvalue_or_else (lhs, "assignment"))
5616 return error_mark_node;
5618 cond = build_conditional_expr
5619 (TREE_OPERAND (lhs, 0),
5620 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5621 TREE_OPERAND (lhs, 1)),
5623 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5624 TREE_OPERAND (lhs, 2)),
5627 if (cond == error_mark_node)
5629 /* Make sure the code to compute the rhs comes out
5630 before the split. */
5631 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5632 /* Case to void to suppress warning
5633 from warn_if_unused_value. */
5634 cp_convert (void_type_node, rhs), cond);
5641 if (TREE_CODE (lhs) == OFFSET_REF)
5643 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5645 /* Static class member? */
5646 tree member = TREE_OPERAND (lhs, 1);
5647 if (TREE_CODE (member) == VAR_DECL)
5651 compiler_error ("invalid static class member");
5652 return error_mark_node;
5656 lhs = resolve_offset_ref (lhs);
5658 olhstype = lhstype = TREE_TYPE (lhs);
5661 if (lhs == error_mark_node)
5664 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5665 && modifycode != INIT_EXPR)
5667 lhs = convert_from_reference (lhs);
5668 olhstype = lhstype = TREE_TYPE (lhs);
5671 /* If a binary op has been requested, combine the old LHS value with the RHS
5672 producing the value we should actually store into the LHS. */
5674 if (modifycode == INIT_EXPR)
5676 if (! IS_AGGR_TYPE (lhstype))
5677 /* Do the default thing */;
5680 result = build_method_call (lhs, ctor_identifier,
5681 build_expr_list (NULL_TREE, rhs),
5682 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5683 if (result == NULL_TREE)
5684 return error_mark_node;
5688 else if (modifycode == NOP_EXPR)
5690 /* `operator=' is not an inheritable operator. */
5691 if (! IS_AGGR_TYPE (lhstype))
5692 /* Do the default thing */;
5695 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5696 lhs, rhs, make_node (NOP_EXPR));
5697 if (result == NULL_TREE)
5698 return error_mark_node;
5703 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5705 my_friendly_abort (978652);
5709 lhs = stabilize_reference (lhs);
5710 newrhs = build_binary_op (modifycode, lhs, rhs);
5711 if (newrhs == error_mark_node)
5713 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5714 TREE_TYPE (lhs), TREE_TYPE (rhs));
5715 return error_mark_node;
5719 /* Handle a cast used as an "lvalue".
5720 We have already performed any binary operator using the value as cast.
5721 Now convert the result to the cast type of the lhs,
5722 and then true type of the lhs and store it there;
5723 then convert result back to the cast type to be the value
5724 of the assignment. */
5726 switch (TREE_CODE (lhs))
5731 case FIX_TRUNC_EXPR:
5732 case FIX_FLOOR_EXPR:
5733 case FIX_ROUND_EXPR:
5735 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5736 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5737 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5738 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5739 newrhs = default_conversion (newrhs);
5741 tree inner_lhs = TREE_OPERAND (lhs, 0);
5744 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5745 otherwise the result is an rvalue. */
5746 if (! lvalue_p (lhs))
5747 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5749 result = build_modify_expr (inner_lhs, NOP_EXPR,
5750 cp_convert (TREE_TYPE (inner_lhs),
5751 cp_convert (lhstype, newrhs)));
5752 if (result == error_mark_node)
5754 return cp_convert (TREE_TYPE (lhs), result);
5761 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5762 Reject anything strange now. */
5764 if (!lvalue_or_else (lhs, "assignment"))
5765 return error_mark_node;
5767 GNU_xref_assign (lhs);
5769 /* Warn about storing in something that is `const'. */
5770 /* For C++, don't warn if this is initialization. */
5771 if (modifycode != INIT_EXPR
5772 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5773 /* Functions are not modifiable, even though they are
5775 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5776 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5777 && C_TYPE_FIELDS_READONLY (lhstype))
5778 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5779 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5780 readonly_error (lhs, "assignment", 0);
5782 /* If storing into a structure or union member,
5783 it has probably been given type `int'.
5784 Compute the type that would go with
5785 the actual amount of storage the member occupies. */
5787 if (TREE_CODE (lhs) == COMPONENT_REF
5788 && (TREE_CODE (lhstype) == INTEGER_TYPE
5789 || TREE_CODE (lhstype) == REAL_TYPE
5790 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5792 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5794 /* If storing in a field that is in actuality a short or narrower
5795 than one, we must store in the field in its actual type. */
5797 if (lhstype != TREE_TYPE (lhs))
5799 lhs = copy_node (lhs);
5800 TREE_TYPE (lhs) = lhstype;
5804 /* check to see if there is an assignment to `this' */
5805 if (lhs == current_class_ptr)
5807 if (flag_this_is_variable > 0
5808 && DECL_NAME (current_function_decl) != NULL_TREE
5809 && (DECL_NAME (current_function_decl)
5810 != constructor_name (current_class_type)))
5811 warning ("assignment to `this' not in constructor or destructor");
5812 current_function_just_assigned_this = 1;
5815 if (modifycode != INIT_EXPR)
5817 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5818 modifycode = NOP_EXPR;
5819 /* Reference-bashing */
5820 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5822 tree tmp = convert_from_reference (lhs);
5823 lhstype = TREE_TYPE (tmp);
5824 if (TYPE_SIZE (lhstype) == 0)
5826 incomplete_type_error (lhs, lhstype);
5827 return error_mark_node;
5832 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5834 tree tmp = convert_from_reference (newrhs);
5835 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5837 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5838 return error_mark_node;
5844 if (TREE_SIDE_EFFECTS (lhs))
5845 lhs = stabilize_reference (lhs);
5846 if (TREE_SIDE_EFFECTS (newrhs))
5847 newrhs = stabilize_reference (newrhs);
5849 /* Convert new value to destination type. */
5851 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5855 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5857 cp_error ("incompatible types in assignment of `%T' to `%T'",
5858 TREE_TYPE (rhs), lhstype);
5859 return error_mark_node;
5862 /* Allow array assignment in compiler-generated code. */
5863 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5864 pedwarn ("ANSI C++ forbids assignment of arrays");
5866 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5867 ? 1 + (modifycode != INIT_EXPR): 0;
5868 return (build_vec_init
5869 (lhs, lhs, array_type_nelts (lhstype), newrhs,
5873 if (modifycode == INIT_EXPR)
5875 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5876 "assignment", NULL_TREE, 0);
5877 if (lhs == DECL_RESULT (current_function_decl))
5879 if (DECL_INITIAL (lhs))
5880 warning ("return value from function receives multiple initializations");
5881 DECL_INITIAL (lhs) = newrhs;
5886 /* Avoid warnings on enum bit fields. */
5887 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5888 && TREE_CODE (lhstype) == INTEGER_TYPE)
5890 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5892 newrhs = convert_force (lhstype, newrhs, 0);
5895 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5897 if (TREE_CODE (newrhs) == CALL_EXPR
5898 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5899 newrhs = build_cplus_new (lhstype, newrhs);
5901 /* Can't initialize directly from a TARGET_EXPR, since that would
5902 cause the lhs to be constructed twice, and possibly result in
5903 accidental self-initialization. So we force the TARGET_EXPR to be
5904 expanded without a target. */
5905 if (TREE_CODE (newrhs) == TARGET_EXPR)
5906 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5907 TREE_OPERAND (newrhs, 0));
5910 if (newrhs == error_mark_node)
5911 return error_mark_node;
5913 if (TREE_CODE (newrhs) == COND_EXPR)
5916 tree cond = TREE_OPERAND (newrhs, 0);
5918 if (TREE_SIDE_EFFECTS (lhs))
5919 cond = build_compound_expr (tree_cons
5921 build_expr_list (NULL_TREE, cond)));
5923 /* Cannot have two identical lhs on this one tree (result) as preexpand
5924 calls will rip them out and fill in RTL for them, but when the
5925 rtl is generated, the calls will only be in the first side of the
5926 condition, not on both, or before the conditional jump! (mrs) */
5927 lhs1 = break_out_calls (lhs);
5930 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5931 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5932 lhstype, lhs, newrhs);
5935 tree result_type = TREE_TYPE (newrhs);
5936 /* We have to convert each arm to the proper type because the
5937 types may have been munged by constant folding. */
5939 = build (COND_EXPR, result_type, cond,
5940 build_modify_expr (lhs, modifycode,
5941 cp_convert (result_type,
5942 TREE_OPERAND (newrhs, 1))),
5943 build_modify_expr (lhs1, modifycode,
5944 cp_convert (result_type,
5945 TREE_OPERAND (newrhs, 2))));
5949 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5950 lhstype, lhs, newrhs);
5952 TREE_SIDE_EFFECTS (result) = 1;
5954 /* If we got the LHS in a different type for storing in,
5955 convert the result back to the nominal type of LHS
5956 so that the value we return always has the same type
5957 as the LHS argument. */
5959 if (olhstype == TREE_TYPE (result))
5961 /* Avoid warnings converting integral types back into enums
5962 for enum bit fields. */
5963 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5964 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5966 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5967 TREE_NO_UNUSED_WARNING (result) = 1;
5970 return convert_for_assignment (olhstype, result, "assignment",
5975 build_x_modify_expr (lhs, modifycode, rhs)
5977 enum tree_code modifycode;
5980 if (processing_template_decl)
5981 return build_min_nt (MODOP_EXPR, lhs,
5982 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5984 if (modifycode != NOP_EXPR)
5986 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5987 make_node (modifycode));
5991 return build_modify_expr (lhs, modifycode, rhs);
5995 /* Get difference in deltas for different pointer to member function
5996 types. Return integer_zero_node, if FROM cannot be converted to a
5997 TO type. If FORCE is true, then allow reverse conversions as well.
5999 Note that the naming of FROM and TO is kind of backwards; the return
6000 value is what we add to a TO in order to get a FROM. They are named
6001 this way because we call this function to find out how to convert from
6002 a pointer to member of FROM to a pointer to member of TO. */
6005 get_delta_difference (from, to, force)
6009 tree delta = integer_zero_node;
6015 /* Should get_base_distance here, so we can check if any thing along the
6016 path is virtual, and we need to make sure we stay
6017 inside the real binfos when going through virtual bases.
6018 Maybe we should replace virtual bases with
6019 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6020 binfo = get_binfo (from, to, 1);
6021 if (binfo == error_mark_node)
6023 error (" in pointer to member function conversion");
6030 error_not_base_type (from, to);
6031 error (" in pointer to member conversion");
6034 binfo = get_binfo (to, from, 1);
6035 if (binfo == 0 || binfo == error_mark_node)
6037 if (binfo_from_vbase (binfo))
6039 binfo = binfo_member (BINFO_TYPE (binfo),
6040 CLASSTYPE_VBASECLASSES (from));
6041 cp_warning ("pointer to member cast to virtual base `%T'",
6042 BINFO_TYPE (binfo));
6043 warning (" will only work if you are very careful");
6045 delta = BINFO_OFFSET (binfo);
6046 delta = cp_convert (ptrdiff_type_node, delta);
6048 return build_binary_op (MINUS_EXPR,
6053 if (binfo_from_vbase (binfo))
6057 cp_warning ("pointer to member cast from virtual base `%T'",
6058 BINFO_TYPE (binfo));
6059 warning (" will only work if you are very careful");
6062 cp_error ("pointer to member conversion from virtual base `%T'",
6063 BINFO_TYPE (binfo));
6066 return BINFO_OFFSET (binfo);
6070 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6071 tree type, delta, idx, pfn, delta2;
6076 /* This is the old way we did it. We want to avoid calling
6077 digest_init, so that it can give an error if we use { } when
6078 initializing a pointer to member function. */
6082 u = build_nt (CONSTRUCTOR, NULL_TREE,
6083 tree_cons (pfn_identifier, pfn, NULL_TREE));
6087 u = build_nt (CONSTRUCTOR, NULL_TREE,
6088 tree_cons (delta2_identifier, delta2, NULL_TREE));
6091 u = build_nt (CONSTRUCTOR, NULL_TREE,
6092 tree_cons (NULL_TREE, delta,
6093 tree_cons (NULL_TREE, idx,
6094 tree_cons (NULL_TREE, u, NULL_TREE))));
6096 return digest_init (type, u, (tree*)0);
6098 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6100 int allconstant, allsimple;
6102 delta_field = TYPE_FIELDS (type);
6103 idx_field = TREE_CHAIN (delta_field);
6104 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6105 subtype = TREE_TYPE (pfn_or_delta2_field);
6106 pfn_field = TYPE_FIELDS (subtype);
6107 delta2_field = TREE_CHAIN (pfn_field);
6111 allconstant = TREE_CONSTANT (pfn);
6112 allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6113 u = tree_cons (pfn_field, pfn, NULL_TREE);
6117 delta2 = convert_and_check (delta_type_node, delta2);
6118 allconstant = TREE_CONSTANT (delta2);
6119 allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6120 u = tree_cons (delta2_field, delta2, NULL_TREE);
6123 delta = convert_and_check (delta_type_node, delta);
6124 idx = convert_and_check (delta_type_node, idx);
6126 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6127 allsimple = allsimple
6128 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6129 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6131 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6132 u = tree_cons (delta_field, delta,
6133 tree_cons (idx_field, idx,
6134 tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6135 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6136 TREE_CONSTANT (u) = allconstant;
6137 TREE_STATIC (u) = allconstant && allsimple;
6142 /* Build a constructor for a pointer to member function. It can be
6143 used to initialize global variables, local variable, or used
6144 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6147 If FORCE is non-zero, then force this conversion, even if
6148 we would rather not do it. Usually set when using an explicit
6151 Return error_mark_node, if something goes wrong. */
6154 build_ptrmemfunc (type, pfn, force)
6159 tree pfn_type = TREE_TYPE (pfn);
6160 tree to_type = build_ptrmemfunc_type (type);
6162 /* Handle multiple conversions of pointer to member functions. */
6163 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6165 tree idx = integer_zero_node;
6166 tree delta = integer_zero_node;
6167 tree delta2 = integer_zero_node;
6168 tree npfn = NULL_TREE;
6169 tree ndelta, ndelta2;
6173 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6174 cp_error ("conversion to `%T' from `%T'",
6177 if (TREE_CODE (pfn) == PTRMEM_CST)
6179 /* We could just build the resulting CONSTRUCTOR now, but we
6180 don't, relying on the general machinery below, together
6181 with constant-folding, to do the right thing. We don't
6182 want to return a PTRMEM_CST here, since a
6183 pointer-to-member constant is no longer a valid template
6184 argument once it is cast to any type, including its
6186 expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
6188 /* This constant points to a non-virtual function.
6189 NDELTA2 will be NULL, but it's value doesn't really
6190 matter since we won't use it anyhow. */
6191 ndelta2 = integer_zero_node;
6193 else if (same_type_p (to_type, pfn_type))
6194 /* We don't have to do any conversion. Note that we do this
6195 after checking for a PTRMEM_CST so that a PTRMEM_CST, cast
6196 to its own type, will not be considered a legal non-type
6197 template argument. */
6201 ndelta = cp_convert (ptrdiff_type_node,
6202 build_component_ref (pfn,
6205 ndelta2 = cp_convert (ptrdiff_type_node,
6206 DELTA2_FROM_PTRMEMFUNC (pfn));
6207 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6210 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6211 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6213 delta = build_binary_op (PLUS_EXPR, ndelta, n);
6214 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n);
6215 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6217 /* If it's a virtual function, this is what we want. */
6218 e2 = build_ptrmemfunc1 (to_type, delta, idx,
6221 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6222 npfn = build1 (NOP_EXPR, type, pfn);
6223 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6225 /* But if it's a non-virtual function, or NULL, we use this
6227 e3 = build_ptrmemfunc1 (to_type, delta,
6228 idx, npfn, NULL_TREE);
6229 return build_conditional_expr (e1, e2, e3);
6232 /* Handle null pointer to member function conversions. */
6233 if (integer_zerop (pfn))
6235 pfn = build_c_cast (type, integer_zero_node);
6236 return build_ptrmemfunc1 (to_type,
6237 integer_zero_node, integer_zero_node,
6241 if (type_unknown_p (pfn))
6242 return instantiate_type (type, pfn, 1);
6244 fn = TREE_OPERAND (pfn, 0);
6245 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6246 return make_ptrmem_cst (to_type, fn);
6249 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6253 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6260 tree type = TREE_TYPE (cst);
6261 tree fn = PTRMEM_CST_MEMBER (cst);
6262 tree ptr_class, fn_class;
6264 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6266 /* The class that the function belongs to. */
6267 fn_class = DECL_CLASS_CONTEXT (fn);
6269 /* The class that we're creating a pointer to member of. */
6270 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6272 /* First, calculate the adjustment to the function's class. */
6273 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6275 if (!DECL_VIRTUAL_P (fn))
6277 *idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6278 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6279 *delta2 = NULL_TREE;
6283 /* If we're dealing with a virtual function, we have to adjust 'this'
6284 again, to point to the base which provides the vtable entry for
6285 fn; the call will do the opposite adjustment. */
6286 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6287 tree binfo = binfo_or_else (orig_class, fn_class);
6288 *delta = size_binop (PLUS_EXPR, *delta, BINFO_OFFSET (binfo));
6290 /* Map everything down one to make room for the null PMF. */
6291 *idx = size_binop (PLUS_EXPR, DECL_VINDEX (fn), integer_one_node);
6294 /* Offset from an object of PTR_CLASS to the vptr for ORIG_CLASS. */
6295 *delta2 = size_binop (PLUS_EXPR, *delta,
6296 get_vfield_offset (TYPE_BINFO (orig_class)));
6300 /* Return an expression for DELTA2 from the pointer-to-member function
6304 delta2_from_ptrmemfunc (t)
6307 if (TREE_CODE (t) == PTRMEM_CST)
6314 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6319 return (build_component_ref
6320 (build_component_ref (t,
6321 pfn_or_delta2_identifier, NULL_TREE,
6323 delta2_identifier, NULL_TREE, 0));
6326 /* Return an expression for PFN from the pointer-to-member function
6330 pfn_from_ptrmemfunc (t)
6333 if (TREE_CODE (t) == PTRMEM_CST)
6340 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6345 return (build_component_ref
6346 (build_component_ref (t,
6347 pfn_or_delta2_identifier, NULL_TREE,
6349 pfn_identifier, NULL_TREE, 0));
6352 /* Convert value RHS to type TYPE as preparation for an assignment to
6353 an lvalue of type TYPE. ERRTYPE is a string to use in error
6354 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6355 are doing the conversion in order to pass the PARMNUMth argument of
6359 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6361 const char *errtype;
6365 register enum tree_code codel = TREE_CODE (type);
6366 register tree rhstype;
6367 register enum tree_code coder;
6369 if (codel == OFFSET_TYPE)
6370 my_friendly_abort (990505);
6372 if (TREE_CODE (rhs) == OFFSET_REF)
6373 rhs = resolve_offset_ref (rhs);
6375 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6376 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6377 rhs = TREE_OPERAND (rhs, 0);
6379 rhstype = TREE_TYPE (rhs);
6380 coder = TREE_CODE (rhstype);
6382 if (rhs == error_mark_node || rhstype == error_mark_node)
6383 return error_mark_node;
6384 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6385 return error_mark_node;
6387 /* Issue warnings about peculiar, but legal, uses of NULL. */
6388 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6389 cp_warning ("converting NULL to non-pointer type");
6391 /* The RHS of an assignment cannot have void type. */
6392 if (coder == VOID_TYPE)
6394 error ("void value not ignored as it ought to be");
6395 return error_mark_node;
6398 /* Simplify the RHS if possible. */
6399 if (TREE_CODE (rhs) == CONST_DECL)
6400 rhs = DECL_INITIAL (rhs);
6401 else if (TREE_READONLY_DECL_P (rhs))
6402 rhs = decl_constant_value (rhs);
6404 /* Warn about assigning a floating-point type to an integer type. */
6405 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6408 cp_warning ("`%T' used for argument %P of `%D'",
6409 rhstype, parmnum, fndecl);
6411 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6413 /* And warn about assigning a negative value to an unsigned
6415 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6417 if (TREE_CODE (rhs) == INTEGER_CST
6418 && TREE_NEGATED_INT (rhs))
6421 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6422 rhs, parmnum, fndecl);
6424 cp_warning ("%s of negative value `%E' to `%T'",
6425 errtype, rhs, type);
6427 overflow_warning (rhs);
6428 if (TREE_CONSTANT (rhs))
6434 The expression is implicitly converted (clause _conv_) to the
6435 cv-unqualified type of the left operand. */
6436 if (!can_convert_arg (type, rhstype, rhs))
6438 /* When -Wno-pmf-converions is use, we just silently allow
6439 conversions from pointers-to-members to plain pointers. If
6440 the conversion doesn't work, cp_convert will complain. */
6442 && TYPE_PTR_P (type)
6443 && TYPE_PTRMEMFUNC_P (rhstype))
6444 rhs = cp_convert (strip_top_quals (type), rhs);
6445 /* If the right-hand side has unknown type, then it is an
6446 overloaded function. Call instantiate_type to get error
6448 else if (rhstype == unknown_type_node)
6449 instantiate_type (type, rhs, 1);
6451 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6452 rhstype, type, parmnum, fndecl);
6454 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6456 return error_mark_node;
6458 return perform_implicit_conversion (strip_top_quals (type), rhs);
6461 /* Convert RHS to be of type TYPE.
6462 If EXP is non-zero, it is the target of the initialization.
6463 ERRTYPE is a string to use in error messages.
6465 Two major differences between the behavior of
6466 `convert_for_assignment' and `convert_for_initialization'
6467 are that references are bashed in the former, while
6468 copied in the latter, and aggregates are assigned in
6469 the former (operator=) while initialized in the
6472 If using constructor make sure no conversion operator exists, if one does
6473 exist, an ambiguity exists.
6475 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6478 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6479 tree exp, type, rhs;
6481 const char *errtype;
6485 register enum tree_code codel = TREE_CODE (type);
6486 register tree rhstype;
6487 register enum tree_code coder;
6489 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6490 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6491 if (TREE_CODE (rhs) == NOP_EXPR
6492 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6493 && codel != REFERENCE_TYPE)
6494 rhs = TREE_OPERAND (rhs, 0);
6496 if (rhs == error_mark_node
6497 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6498 return error_mark_node;
6500 if (TREE_CODE (rhs) == OFFSET_REF)
6502 rhs = resolve_offset_ref (rhs);
6503 if (rhs == error_mark_node)
6504 return error_mark_node;
6507 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6508 rhs = convert_from_reference (rhs);
6510 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6511 && TREE_CODE (type) != ARRAY_TYPE
6512 && (TREE_CODE (type) != REFERENCE_TYPE
6513 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6514 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6515 && (TREE_CODE (type) != REFERENCE_TYPE
6516 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6517 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6518 rhs = default_conversion (rhs);
6520 rhstype = TREE_TYPE (rhs);
6521 coder = TREE_CODE (rhstype);
6523 if (coder == ERROR_MARK)
6524 return error_mark_node;
6526 /* We accept references to incomplete types, so we can
6527 return here before checking if RHS is of complete type. */
6529 if (codel == REFERENCE_TYPE)
6531 /* This should eventually happen in convert_arguments. */
6532 extern int warningcount, errorcount;
6533 int savew = 0, savee = 0;
6536 savew = warningcount, savee = errorcount;
6537 rhs = initialize_reference (type, rhs);
6540 if (warningcount > savew)
6541 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6542 else if (errorcount > savee)
6543 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6549 exp = require_complete_type (exp);
6550 if (exp == error_mark_node)
6551 return error_mark_node;
6553 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6554 rhstype = TREE_TYPE (rhstype);
6556 type = complete_type (type);
6558 if (IS_AGGR_TYPE (type))
6559 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6561 if (type == TREE_TYPE (rhs))
6563 /* Issue warnings about peculiar, but legal, uses of NULL. We
6564 do this *before* the call to decl_constant_value so as to
6565 avoid duplicate warnings on code like `const int I = NULL;
6567 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6568 cp_warning ("converting NULL to non-pointer type");
6570 if (TREE_READONLY_DECL_P (rhs))
6571 rhs = decl_constant_value (rhs);
6576 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6579 /* Expand an ASM statement with operands, handling output operands
6580 that are not variables or INDIRECT_REFS by transforming such
6581 cases into cases that expand_asm_operands can handle.
6583 Arguments are same as for expand_asm_operands.
6585 We don't do default conversions on all inputs, because it can screw
6586 up operands that are expected to be in memory. */
6589 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6590 tree string, outputs, inputs, clobbers;
6595 int noutputs = list_length (outputs);
6597 /* o[I] is the place that output number I should be written. */
6598 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6601 /* Record the contents of OUTPUTS before it is modified. */
6602 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6603 o[i] = TREE_VALUE (tail);
6605 /* Generate the ASM_OPERANDS insn;
6606 store into the TREE_VALUEs of OUTPUTS some trees for
6607 where the values were actually stored. */
6608 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6610 /* Copy all the intermediate outputs into the specified outputs. */
6611 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6613 if (o[i] != TREE_VALUE (tail))
6615 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6616 const0_rtx, VOIDmode, EXPAND_NORMAL);
6619 /* Detect modification of read-only values.
6620 (Otherwise done by build_modify_expr.) */
6623 tree type = TREE_TYPE (o[i]);
6624 if (CP_TYPE_CONST_P (type)
6625 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6626 && C_TYPE_FIELDS_READONLY (type)))
6627 readonly_error (o[i], "modification by `asm'", 1);
6631 /* Those MODIFY_EXPRs could do autoincrements. */
6635 /* Expand a C `return' statement.
6636 RETVAL is the expression for what to return,
6637 or a null pointer for `return;' with no value.
6639 C++: upon seeing a `return', we must call destructors on all
6640 variables in scope which had constructors called on them.
6641 This means that if in a destructor, the base class destructors
6642 must be called before returning.
6644 The RETURN statement in C++ has initialization semantics. */
6647 c_expand_return (retval)
6650 tree result = DECL_RESULT (current_function_decl);
6651 tree valtype = TREE_TYPE (result);
6653 if (TREE_THIS_VOLATILE (current_function_decl))
6654 warning ("function declared `noreturn' has a `return' statement");
6656 if (retval == error_mark_node)
6658 current_function_returns_null = 1;
6665 error ("returning a value from a destructor");
6667 /* Can't just return from a destructor. */
6668 expand_goto (dtor_label);
6671 else if (in_function_try_handler
6672 && DECL_CONSTRUCTOR_P (current_function_decl))
6674 /* If a return statement appears in a handler of the
6675 function-try-block of a constructor, the program is ill-formed. */
6676 error ("cannot return from a handler of a function-try-block of a constructor");
6680 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6681 if ((DECL_NAME (current_function_decl) == ansi_opname[(int) NEW_EXPR]
6682 || DECL_NAME (current_function_decl) == ansi_opname[(int) VEC_NEW_EXPR])
6683 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6684 && null_ptr_cst_p (retval))
6685 cp_warning ("operator new should throw an exception, not return NULL");
6687 if (retval == NULL_TREE)
6689 /* A non-named return value does not count. */
6691 if (DECL_CONSTRUCTOR_P (current_function_decl))
6692 retval = current_class_ptr;
6693 else if (DECL_NAME (result) != NULL_TREE
6694 && TREE_CODE (valtype) != VOID_TYPE)
6698 current_function_returns_null = 1;
6700 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
6702 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
6704 pedwarn ("`return' with no value, in function returning non-void");
6705 /* Clear this, so finish_function won't say that we
6706 reach the end of a non-void function (which we don't,
6707 we gave a return!). */
6708 current_function_returns_null = 0;
6712 expand_null_return ();
6716 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6718 if (flag_this_is_variable)
6719 error ("return from a constructor: use `this = ...' instead");
6721 error ("returning a value from a constructor");
6722 retval = current_class_ptr;
6725 /* Effective C++ rule 15. See also start_function. */
6727 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
6728 && retval != current_class_ref)
6729 cp_warning ("`operator=' should return a reference to `*this'");
6731 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
6733 current_function_returns_null = 1;
6734 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6735 pedwarn ("`return' with a value, in function returning void");
6736 expand_return (retval);
6740 /* Now deal with possible C++ hair:
6741 (1) Compute the return value.
6742 (2) If there are aggregate values with destructors which
6743 must be cleaned up, clean them (taking care
6744 not to clobber the return value).
6745 (3) If an X(X&) constructor is defined, the return
6746 value must be returned via that. */
6748 if (retval == result
6749 || DECL_CONSTRUCTOR_P (current_function_decl))
6750 /* It's already done for us. */;
6751 else if (TREE_CODE (TREE_TYPE (retval)) == VOID_TYPE)
6753 pedwarn ("return of void value in function returning non-void");
6754 expand_expr_stmt (retval);
6759 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6761 /* First convert the value to the function's return type, then
6762 to the type of return value's location to handle the
6763 case that functype is thiner than the valtype. */
6765 retval = convert_for_initialization
6766 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6767 "return", NULL_TREE, 0);
6769 retval = convert (valtype, retval);
6771 if (retval == error_mark_node)
6773 /* Avoid warning about control reaching end of function. */
6774 expand_null_return ();
6778 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6779 else if (! current_function_returns_struct
6780 && TREE_CODE (retval) == TARGET_EXPR
6781 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6782 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6783 TREE_OPERAND (retval, 0));
6785 /* Add some useful error checking for C++. */
6786 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
6788 tree whats_returned;
6790 /* Sort through common things to see what it is
6791 we are returning. */
6792 whats_returned = retval;
6793 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6795 whats_returned = TREE_OPERAND (whats_returned, 1);
6796 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6797 whats_returned = TREE_OPERAND (whats_returned, 0);
6799 while (TREE_CODE (whats_returned) == CONVERT_EXPR
6800 || TREE_CODE (whats_returned) == NOP_EXPR)
6801 whats_returned = TREE_OPERAND (whats_returned, 0);
6802 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6804 whats_returned = TREE_OPERAND (whats_returned, 0);
6805 while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6806 || TREE_CODE (whats_returned) == TARGET_EXPR)
6808 /* Get the target. */
6809 whats_returned = TREE_OPERAND (whats_returned, 0);
6810 warning ("returning reference to temporary");
6814 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
6816 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
6817 warning ("reference to non-lvalue returned");
6818 else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
6819 && DECL_FUNCTION_SCOPE_P (whats_returned)
6820 && !(TREE_STATIC (whats_returned)
6821 || TREE_PUBLIC (whats_returned)))
6822 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
6825 else if (TREE_CODE (retval) == ADDR_EXPR)
6827 tree whats_returned = TREE_OPERAND (retval, 0);
6829 if (TREE_CODE (whats_returned) == VAR_DECL
6830 && DECL_NAME (whats_returned)
6831 && DECL_FUNCTION_SCOPE_P (whats_returned)
6832 && !(TREE_STATIC (whats_returned)
6833 || TREE_PUBLIC (whats_returned)))
6834 cp_warning_at ("address of local variable `%D' returned", whats_returned);
6838 if (retval != NULL_TREE
6839 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
6840 && ! in_control_zone_p ())
6841 current_function_return_value = retval;
6843 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
6845 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
6846 expand_goto (ctor_label);
6849 if (retval && retval != result)
6851 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6852 TREE_SIDE_EFFECTS (result) = 1;
6855 expand_start_target_temps ();
6857 expand_return (result);
6859 expand_end_target_temps ();
6861 current_function_returns_value = 1;
6864 /* Start a C switch statement, testing expression EXP.
6865 Return EXP if it is valid, an error node otherwise. */
6868 c_expand_start_case (exp)
6873 exp = build_expr_type_conversion (WANT_INT | WANT_ENUM, exp, 1);
6874 if (exp == NULL_TREE)
6876 error ("switch quantity not an integer");
6877 exp = error_mark_node;
6879 if (exp == error_mark_node)
6880 return error_mark_node;
6882 exp = default_conversion (exp);
6883 type = TREE_TYPE (exp);
6884 idx = get_unwidened (exp, 0);
6885 /* We can't strip a conversion from a signed type to an unsigned,
6886 because if we did, int_fits_type_p would do the wrong thing
6887 when checking case values for being in range,
6888 and it's too hard to do the right thing. */
6889 if (TREE_UNSIGNED (TREE_TYPE (exp)) == TREE_UNSIGNED (TREE_TYPE (idx)))
6893 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
6894 type, "switch statement");
6899 /* Returns non-zero if the pointer-type FROM can be converted to the
6900 pointer-type TO via a qualification conversion. If CONSTP is -1,
6901 then we return non-zero if the pointers are similar, and the
6902 cv-qualification signature of FROM is a proper subset of that of TO.
6904 If CONSTP is positive, then all outer pointers have been
6908 comp_ptr_ttypes_real (to, from, constp)
6912 int to_more_cv_qualified = 0;
6914 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6916 if (TREE_CODE (to) != TREE_CODE (from))
6919 if (TREE_CODE (from) == OFFSET_TYPE
6920 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6921 TYPE_OFFSET_BASETYPE (to)))
6924 /* Const and volatile mean something different for function types,
6925 so the usual checks are not appropriate. */
6926 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6928 if (!at_least_as_qualified_p (to, from))
6931 if (!at_least_as_qualified_p (from, to))
6936 ++to_more_cv_qualified;
6940 constp &= TYPE_READONLY (to);
6943 if (TREE_CODE (to) != POINTER_TYPE)
6945 same_type_p (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from))
6946 && (constp >= 0 || to_more_cv_qualified);
6950 /* When comparing, say, char ** to char const **, this function takes the
6951 'char *' and 'char const *'. Do not pass non-pointer types to this
6955 comp_ptr_ttypes (to, from)
6958 return comp_ptr_ttypes_real (to, from, 1);
6961 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6962 type or inheritance-related types, regardless of cv-quals. */
6965 ptr_reasonably_similar (to, from)
6968 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6970 if (TREE_CODE (to) != TREE_CODE (from))
6973 if (TREE_CODE (from) == OFFSET_TYPE
6974 && comptypes (TYPE_OFFSET_BASETYPE (to),
6975 TYPE_OFFSET_BASETYPE (from),
6976 COMPARE_BASE | COMPARE_RELAXED))
6979 if (TREE_CODE (to) != POINTER_TYPE)
6981 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6982 COMPARE_BASE | COMPARE_RELAXED);
6986 /* Like comp_ptr_ttypes, for const_cast. */
6989 comp_ptr_ttypes_const (to, from)
6992 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6994 if (TREE_CODE (to) != TREE_CODE (from))
6997 if (TREE_CODE (from) == OFFSET_TYPE
6998 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6999 TYPE_OFFSET_BASETYPE (to)))
7002 if (TREE_CODE (to) != POINTER_TYPE)
7003 return same_type_p (TYPE_MAIN_VARIANT (to),
7004 TYPE_MAIN_VARIANT (from));
7008 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7011 comp_ptr_ttypes_reinterpret (to, from)
7016 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7018 if (TREE_CODE (from) == OFFSET_TYPE)
7019 from = TREE_TYPE (from);
7020 if (TREE_CODE (to) == OFFSET_TYPE)
7021 to = TREE_TYPE (to);
7023 /* Const and volatile mean something different for function types,
7024 so the usual checks are not appropriate. */
7025 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7026 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7028 if (!at_least_as_qualified_p (to, from))
7032 && !at_least_as_qualified_p (from, to))
7034 constp &= TYPE_READONLY (to);
7037 if (TREE_CODE (from) != POINTER_TYPE
7038 || TREE_CODE (to) != POINTER_TYPE)
7043 /* Recursively examines the array elements of TYPE, until a non-array
7044 element type is found. */
7047 strip_array_types (type)
7050 while (TREE_CODE (type) == ARRAY_TYPE)
7051 type = TREE_TYPE (type);
7056 /* Returns the type-qualifier set corresponding to TYPE. */
7059 cp_type_quals (type)
7062 return TYPE_QUALS (strip_array_types (type));
7065 /* Returns non-zero if the TYPE contains a mutable member */
7068 cp_has_mutable_p (type)
7071 while (TREE_CODE (type) == ARRAY_TYPE)
7072 type = TREE_TYPE (type);
7074 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7077 /* Subroutine of casts_away_constness. Make T1 and T2 point at
7078 exemplar types such that casting T1 to T2 is casting away castness
7079 if and only if there is no implicit conversion from T1 to T2. */
7082 casts_away_constness_r (t1, t2)
7089 /* [expr.const.cast]
7091 For multi-level pointer to members and multi-level mixed pointers
7092 and pointers to members (conv.qual), the "member" aspect of a
7093 pointer to member level is ignored when determining if a const
7094 cv-qualifier has been cast away. */
7095 if (TYPE_PTRMEM_P (*t1))
7096 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
7097 if (TYPE_PTRMEM_P (*t2))
7098 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
7100 /* [expr.const.cast]
7102 For two pointer types:
7104 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
7105 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
7108 casting from X1 to X2 casts away constness if, for a non-pointer
7109 type T there does not exist an implicit conversion (clause
7112 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7116 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
7118 if (TREE_CODE (*t1) != POINTER_TYPE
7119 || TREE_CODE (*t2) != POINTER_TYPE)
7121 *t1 = cp_build_qualified_type (void_type_node,
7122 CP_TYPE_QUALS (*t1));
7123 *t2 = cp_build_qualified_type (void_type_node,
7124 CP_TYPE_QUALS (*t2));
7128 quals1 = CP_TYPE_QUALS (*t1);
7129 quals2 = CP_TYPE_QUALS (*t2);
7130 *t1 = TREE_TYPE (*t1);
7131 *t2 = TREE_TYPE (*t2);
7132 casts_away_constness_r (t1, t2);
7133 *t1 = build_pointer_type (*t1);
7134 *t2 = build_pointer_type (*t2);
7135 *t1 = cp_build_qualified_type (*t1, quals1);
7136 *t2 = cp_build_qualified_type (*t2, quals2);
7139 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
7143 casts_away_constness (t1, t2)
7147 if (TREE_CODE (t2) == REFERENCE_TYPE)
7149 /* [expr.const.cast]
7151 Casting from an lvalue of type T1 to an lvalue of type T2
7152 using a reference cast casts away constness if a cast from an
7153 rvalue of type "pointer to T1" to the type "pointer to T2"
7154 casts away constness. */
7155 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
7156 ? TREE_TYPE (t1) : t1);
7157 return casts_away_constness (build_pointer_type (t1),
7158 build_pointer_type (TREE_TYPE (t2)));
7161 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7162 /* [expr.const.cast]
7164 Casting from an rvalue of type "pointer to data member of X
7165 of type T1" to the type "pointer to data member of Y of type
7166 T2" casts away constness if a cast from an rvalue of type
7167 "poitner to T1" to the type "pointer to T2" casts away
7169 return casts_away_constness
7170 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7171 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7173 /* Casting away constness is only something that makes sense for
7174 pointer or reference types. */
7175 if (TREE_CODE (t1) != POINTER_TYPE
7176 || TREE_CODE (t2) != POINTER_TYPE)
7179 /* Top-level qualifiers don't matter. */
7180 t1 = TYPE_MAIN_VARIANT (t1);
7181 t2 = TYPE_MAIN_VARIANT (t2);
7182 casts_away_constness_r (&t1, &t2);
7183 if (!can_convert (t2, t1))