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). */
42 static tree convert_for_assignment PROTO((tree, tree, const char *, tree,
44 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
45 static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
46 static int comp_target_parms PROTO((tree, tree, int));
47 static int comp_ptr_ttypes_real PROTO((tree, tree, int));
48 static int comp_ptr_ttypes_const PROTO((tree, tree));
49 static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
50 static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
52 static tree common_base_type PROTO((tree, tree));
54 static tree convert_sequence PROTO((tree, tree));
56 static tree lookup_anon_field PROTO((tree, tree));
57 static tree pointer_diff PROTO((tree, tree, tree));
58 static tree build_component_addr PROTO((tree, tree));
59 static tree qualify_type PROTO((tree, tree));
60 static tree get_delta_difference PROTO((tree, tree, int));
61 static int comp_cv_target_types PROTO((tree, tree, int));
63 /* Return the target type of TYPE, which meas return T for:
64 T*, T&, T[], T (...), and otherwise, just T. */
70 if (TREE_CODE (type) == REFERENCE_TYPE)
71 type = TREE_TYPE (type);
72 while (TREE_CODE (type) == POINTER_TYPE
73 || TREE_CODE (type) == ARRAY_TYPE
74 || TREE_CODE (type) == FUNCTION_TYPE
75 || TREE_CODE (type) == METHOD_TYPE
76 || TREE_CODE (type) == OFFSET_TYPE)
77 type = TREE_TYPE (type);
81 /* Do `exp = require_complete_type (exp);' to make sure exp
82 does not have an incomplete type. (That includes void types.)
83 Returns the error_mark_node if the VALUE does not have
84 complete type when this function returns. */
87 require_complete_type (value)
92 if (processing_template_decl || value == error_mark_node)
95 if (TREE_CODE (value) == OVERLOAD)
96 type = unknown_type_node;
98 type = TREE_TYPE (value);
100 /* First, detect a valid value with a complete type. */
101 if (TYPE_SIZE (type) != 0
102 && TYPE_SIZE (type) != size_zero_node
103 && ! (TYPE_LANG_SPECIFIC (type)
104 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
105 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
108 /* If we see X::Y, we build an OFFSET_TYPE which has
109 not been laid out. Try to avoid an error by interpreting
110 it as this->X::Y, if reasonable. */
111 if (TREE_CODE (value) == OFFSET_REF
112 && current_class_ref != 0
113 && TREE_OPERAND (value, 0) == current_class_ref)
115 tree base, member = TREE_OPERAND (value, 1);
116 tree basetype = TYPE_OFFSET_BASETYPE (type);
117 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
118 base = convert_pointer_to (basetype, current_class_ptr);
119 value = build (COMPONENT_REF, TREE_TYPE (member),
120 build_indirect_ref (base, NULL_PTR), member);
121 return require_complete_type (value);
124 if (complete_type_or_else (type, value))
127 return error_mark_node;
130 /* Makes sure EXPR is a complete type when used in a void context, like a
131 whole expression, or lhs of a comma operator. Issue a diagnostic and
132 return error_mark_node on failure. This is a little tricky, because some
133 valid void types look stunningly similar to invalid void types. We err on
134 the side of caution */
137 require_complete_type_in_void (expr)
140 switch (TREE_CODE (expr))
146 op = TREE_OPERAND (expr,2);
147 op = require_complete_type_in_void (op);
148 TREE_OPERAND (expr,2) = op;
149 if (op == error_mark_node)
162 op = TREE_OPERAND (expr,1);
163 op = require_complete_type_in_void (op);
164 TREE_OPERAND (expr,1) = op;
165 if (op == error_mark_node)
174 case NON_LVALUE_EXPR:
179 op = TREE_OPERAND (expr,0);
180 op = require_complete_type_in_void (op);
181 TREE_OPERAND (expr,0) = op;
182 if (op == error_mark_node)
190 case CALL_EXPR: /* function call return can be ignored */
191 case RTL_EXPR: /* RTL nodes have no value */
192 case DELETE_EXPR: /* delete expressions have no type */
193 case VEC_DELETE_EXPR:
194 case INTEGER_CST: /* used for null pointer */
195 case EXIT_EXPR: /* have no return */
196 case LOOP_EXPR: /* have no return */
197 case BIND_EXPR: /* have no return */
198 case THROW_EXPR: /* have no return */
199 case MODIFY_EXPR: /* sometimes this has a void type, but that's ok */
200 case CONVERT_EXPR: /* sometimes has a void type */
205 tree op = TREE_OPERAND (expr,0);
207 /* Calling a function returning a reference has an implicit
208 dereference applied. We don't want to make that an error. */
209 if (TREE_CODE (op) == CALL_EXPR
210 && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
212 /* else fallthrough */
216 expr = require_complete_type (expr);
223 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
224 a template instantiation, do the instantiation. Returns TYPE,
225 whether or not it could be completed, unless something goes
226 horribly wrong, in which case the error_mark_node is returned. */
232 if (type == NULL_TREE)
233 /* Rather than crash, we return something sure to cause an error
235 return error_mark_node;
237 if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
239 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
241 tree t = complete_type (TREE_TYPE (type));
242 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
244 TYPE_NEEDS_CONSTRUCTING (type)
245 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
246 TYPE_NEEDS_DESTRUCTOR (type)
247 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
249 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
250 instantiate_class_template (TYPE_MAIN_VARIANT (type));
255 /* Like complete_type, but issue an error if the TYPE cannot be
256 completed. VALUE is used for informative diagnostics.
257 Returns NULL_TREE if the type cannot be made complete. */
260 complete_type_or_else (type, value)
264 type = complete_type (type);
265 if (type == error_mark_node)
266 /* We already issued an error. */
268 else if (!TYPE_SIZE (type) || TYPE_SIZE (type) == size_zero_node)
270 incomplete_type_error (value, type);
277 /* Return truthvalue of whether type of EXP is instantiated. */
283 return (TREE_CODE (exp) == OVERLOAD
284 || TREE_CODE (exp) == TREE_LIST
285 || TREE_TYPE (exp) == unknown_type_node
286 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
287 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
290 /* Return truthvalue of whether T is function (or pfn) type. */
296 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
297 || (TREE_CODE (t) == POINTER_TYPE
298 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
299 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
302 /* Return a variant of TYPE which has all the type qualifiers of LIKE
303 as well as those of TYPE. */
306 qualify_type (type, like)
309 /* @@ Must do member pointers here. */
310 return cp_build_qualified_type (type, (CP_TYPE_QUALS (type)
311 | CP_TYPE_QUALS (like)));
314 /* Return the common type of two parameter lists.
315 We assume that comptypes has already been done and returned 1;
316 if that isn't so, this may crash.
318 As an optimization, free the space we allocate if the parameter
319 lists are already common. */
325 tree oldargs = p1, newargs, n;
328 char *first_obj = (char *) oballoc (0);
330 len = list_length (p1);
331 newargs = tree_last (p1);
333 if (newargs == void_list_node)
342 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
347 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
349 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
351 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
354 else if (! TREE_PURPOSE (p1))
356 if (TREE_PURPOSE (p2))
358 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
364 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
366 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
368 if (TREE_VALUE (p1) != TREE_VALUE (p2))
371 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
374 TREE_VALUE (n) = TREE_VALUE (p1);
385 /* Given a type, perhaps copied for a typedef,
386 find the "original" version of it. */
391 while (TYPE_NAME (t) != NULL_TREE)
393 tree x = TYPE_NAME (t);
394 if (TREE_CODE (x) != TYPE_DECL)
396 x = DECL_ORIGINAL_TYPE (x);
404 /* Return the common type of two types.
405 We assume that comptypes has already been done and returned 1;
406 if that isn't so, this may crash.
408 This is the type for the result of most arithmetic operations
409 if the operands have the given two types.
411 We do not deal with enumeral types here because they have already been
412 converted to integer types. */
418 register enum tree_code code1;
419 register enum tree_code code2;
422 /* Save time if the two types are the same. */
425 t1 = original_type (t1);
426 t2 = original_type (t2);
430 /* If one type is nonsense, use the other. */
431 if (t1 == error_mark_node)
433 if (t2 == error_mark_node)
436 /* Merge the attributes. */
437 attributes = merge_machine_type_attributes (t1, t2);
439 { register tree a1, a2;
440 a1 = TYPE_ATTRIBUTES (t1);
441 a2 = TYPE_ATTRIBUTES (t2);
443 /* Either one unset? Take the set one. */
445 if (!(attributes = a1))
448 /* One that completely contains the other? Take it. */
450 else if (a2 && !attribute_list_contained (a1, a2))
452 if (attribute_list_contained (a2, a1))
456 /* Pick the longest list, and hang on the other list. */
457 /* ??? For the moment we punt on the issue of attrs with args. */
459 if (list_length (a1) < list_length (a2))
460 attributes = a2, a2 = a1;
462 for (; a2; a2 = TREE_CHAIN (a2))
463 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
464 attributes) == NULL_TREE)
467 TREE_CHAIN (a1) = attributes;
474 /* Treat an enum type as the unsigned integer type of the same width. */
476 if (TREE_CODE (t1) == ENUMERAL_TYPE)
477 t1 = type_for_size (TYPE_PRECISION (t1), 1);
478 if (TREE_CODE (t2) == ENUMERAL_TYPE)
479 t2 = type_for_size (TYPE_PRECISION (t2), 1);
481 if (TYPE_PTRMEMFUNC_P (t1))
482 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
483 if (TYPE_PTRMEMFUNC_P (t2))
484 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
486 code1 = TREE_CODE (t1);
487 code2 = TREE_CODE (t2);
489 /* If one type is complex, form the common type of the non-complex
490 components, then make that complex. Use T1 or T2 if it is the
492 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
494 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
495 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
496 tree subtype = common_type (subtype1, subtype2);
498 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
499 return build_type_attribute_variant (t1, attributes);
500 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
501 return build_type_attribute_variant (t2, attributes);
503 return build_type_attribute_variant (build_complex_type (subtype),
511 /* If only one is real, use it as the result. */
513 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
514 return build_type_attribute_variant (t1, attributes);
516 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
517 return build_type_attribute_variant (t2, attributes);
519 /* Both real or both integers; use the one with greater precision. */
521 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
522 return build_type_attribute_variant (t1, attributes);
523 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
524 return build_type_attribute_variant (t2, attributes);
526 /* Same precision. Prefer longs to ints even when same size. */
528 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
529 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
530 return build_type_attribute_variant (long_unsigned_type_node,
533 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
534 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
536 /* But preserve unsignedness from the other type,
537 since long cannot hold all the values of an unsigned int. */
538 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
539 t1 = long_unsigned_type_node;
541 t1 = long_integer_type_node;
542 return build_type_attribute_variant (t1, attributes);
545 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
546 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
547 return build_type_attribute_variant (long_double_type_node,
550 /* Otherwise prefer the unsigned one. */
552 if (TREE_UNSIGNED (t1))
553 return build_type_attribute_variant (t1, attributes);
555 return build_type_attribute_variant (t2, attributes);
559 /* For two pointers, do this recursively on the target type,
560 and combine the qualifiers of the two types' targets. */
561 /* This code was turned off; I don't know why.
562 But ANSI C++ specifies doing this with the qualifiers.
563 So I turned it on again. */
565 tree tt1 = TREE_TYPE (t1);
566 tree tt2 = TREE_TYPE (t2);
571 if (TREE_CODE (tt1) == OFFSET_TYPE)
573 b1 = TYPE_OFFSET_BASETYPE (tt1);
574 b2 = TYPE_OFFSET_BASETYPE (tt2);
575 tt1 = TREE_TYPE (tt1);
576 tt2 = TREE_TYPE (tt2);
581 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
582 tt1 = TYPE_MAIN_VARIANT (tt1);
583 tt2 = TYPE_MAIN_VARIANT (tt2);
589 compiler_error ("common_type called with uncommon member types");
592 else if (tt1 == void_type_node || tt2 == void_type_node)
593 target = void_type_node;
594 else if (tt1 == unknown_type_node)
596 else if (tt2 == unknown_type_node)
599 target = common_type (tt1, tt2);
601 target = cp_build_qualified_type (target, type_quals);
605 if (same_type_p (b1, b2)
606 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
607 target = build_offset_type (b2, target);
608 else if (binfo_or_else (b2, b1))
609 target = build_offset_type (b1, target);
612 if (code1 == POINTER_TYPE)
613 t1 = build_pointer_type (target);
615 t1 = build_reference_type (target);
616 t1 = build_type_attribute_variant (t1, attributes);
618 if (TREE_CODE (target) == METHOD_TYPE)
619 t1 = build_ptrmemfunc_type (t1);
626 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
627 /* Save space: see if the result is identical to one of the args. */
628 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
629 return build_type_attribute_variant (t1, attributes);
630 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
631 return build_type_attribute_variant (t2, attributes);
632 /* Merge the element types, and have a size if either arg has one. */
633 t1 = build_cplus_array_type
634 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
635 return build_type_attribute_variant (t1, attributes);
639 /* Function types: prefer the one that specified arg types.
640 If both do, merge the arg types. Also merge the return types. */
642 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
643 tree p1 = TYPE_ARG_TYPES (t1);
644 tree p2 = TYPE_ARG_TYPES (t2);
647 /* Save space: see if the result is identical to one of the args. */
648 if (valtype == TREE_TYPE (t1) && ! p2)
649 return build_type_attribute_variant (t1, attributes);
650 if (valtype == TREE_TYPE (t2) && ! p1)
651 return build_type_attribute_variant (t2, attributes);
653 /* Simple way if one arg fails to specify argument types. */
654 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
656 rval = build_function_type (valtype, p2);
657 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
658 rval = build_exception_variant (rval, raises);
659 return build_type_attribute_variant (rval, attributes);
661 raises = TYPE_RAISES_EXCEPTIONS (t1);
662 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
664 rval = build_function_type (valtype, p1);
666 rval = build_exception_variant (rval, raises);
667 return build_type_attribute_variant (rval, attributes);
670 rval = build_function_type (valtype, commonparms (p1, p2));
671 rval = build_exception_variant (rval, raises);
672 return build_type_attribute_variant (rval, attributes);
677 t1 = TYPE_MAIN_VARIANT (t1);
678 t2 = TYPE_MAIN_VARIANT (t2);
680 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
681 return build_type_attribute_variant (t1, attributes);
682 else if (binfo_or_else (t2, t1))
683 return build_type_attribute_variant (t2, attributes);
686 compiler_error ("common_type called with uncommon aggregate types");
687 return error_mark_node;
691 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
693 /* Get this value the long way, since TYPE_METHOD_BASETYPE
694 is just the main variant of this. */
698 tree b1 = TYPE_OFFSET_BASETYPE (t1);
699 tree b2 = TYPE_OFFSET_BASETYPE (t2);
701 if (same_type_p (b1, b2)
702 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
703 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
706 if (binfo_or_else (b2, b1) == NULL_TREE)
707 compiler_error ("common_type called with uncommon method types");
708 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
711 raises = TYPE_RAISES_EXCEPTIONS (t1);
713 /* If this was a member function type, get back to the
714 original type of type member function (i.e., without
715 the class instance variable up front. */
716 t1 = build_function_type (TREE_TYPE (t1),
717 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
718 t2 = build_function_type (TREE_TYPE (t2),
719 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
720 t3 = common_type (t1, t2);
721 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
722 TYPE_ARG_TYPES (t3));
723 t1 = build_exception_variant (t3, raises);
726 compiler_error ("common_type called with uncommon method types");
728 return build_type_attribute_variant (t1, attributes);
731 /* Pointers to members should now be handled by the POINTER_TYPE
733 my_friendly_abort (990325);
736 return build_type_attribute_variant (t1, attributes);
740 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
743 compexcepttypes (t1, t2)
746 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
749 /* Compare the array types T1 and T2, using CMP as the type comparison
750 function for the element types. STRICT is as for comptypes. */
753 comp_array_types (cmp, t1, t2, strict)
754 register int (*cmp) PROTO((tree, tree, int));
764 /* The type of the array elements must be the same. */
765 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
766 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
767 strict & ~COMPARE_REDECLARATION)))
770 d1 = TYPE_DOMAIN (t1);
771 d2 = TYPE_DOMAIN (t2);
776 /* If one of the arrays is dimensionless, and the other has a
777 dimension, they are of different types. However, it is legal to
785 declarations for an array object can specify
786 array types that differ by the presence or absence of a major
787 array bound (_dcl.array_). */
789 return strict & COMPARE_REDECLARATION;
791 /* Check that the dimensions are the same. */
792 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
794 && cp_tree_equal (TYPE_MAX_VALUE (d1),
795 TYPE_MAX_VALUE (d2)));
798 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
799 or various other operations. STRICT is a bitwise-or of the
803 comptypes (type1, type2, strict)
807 register tree t1 = type1;
808 register tree t2 = type2;
810 int orig_strict = strict;
812 /* The special exemption for redeclaring array types without an
813 array bound only applies at the top level:
818 is not legal, for example. */
819 strict &= ~COMPARE_REDECLARATION;
821 /* Suppress errors caused by previously reported errors */
825 /* This should never happen. */
826 my_friendly_assert (t1 != error_mark_node, 307);
828 if (t2 == error_mark_node)
831 if (strict & COMPARE_RELAXED)
833 /* Treat an enum type as the unsigned integer type of the same width. */
835 if (TREE_CODE (t1) == ENUMERAL_TYPE)
836 t1 = type_for_size (TYPE_PRECISION (t1), 1);
837 if (TREE_CODE (t2) == ENUMERAL_TYPE)
838 t2 = type_for_size (TYPE_PRECISION (t2), 1);
844 if (TYPE_PTRMEMFUNC_P (t1))
845 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
846 if (TYPE_PTRMEMFUNC_P (t2))
847 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
849 /* Different classes of types can't be compatible. */
850 if (TREE_CODE (t1) != TREE_CODE (t2))
853 /* Qualifiers must match. */
854 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
856 if (strict == COMPARE_STRICT
857 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
860 /* Allow for two different type nodes which have essentially the same
861 definition. Note that we already checked for equality of the type
862 qualifiers (just above). */
864 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
867 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
868 attribute is its own main variant (`val' will remain 0). */
869 #ifndef COMP_TYPE_ATTRIBUTES
870 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
873 if (strict & COMPARE_NO_ATTRIBUTES)
875 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
876 else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
879 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
882 switch (TREE_CODE (t1))
884 case TEMPLATE_TEMPLATE_PARM:
885 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
886 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
888 if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1)),
889 DECL_TEMPLATE_PARMS (TYPE_NAME (t2))))
891 if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1)
892 && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2))
894 /* Don't check inheritance. */
895 strict = COMPARE_STRICT;
900 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
901 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
902 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
903 val = comp_template_args (TYPE_TI_ARGS (t1),
906 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
911 if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
919 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
920 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
921 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
925 if (! compexcepttypes (t1, t2))
928 /* This case is anti-symmetrical!
929 One can pass a base member (or member function)
930 to something expecting a derived member (or member function),
931 but not vice-versa! */
933 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
934 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
941 /* first, check whether the referred types match with the
942 required level of strictness */
943 val = comptypes (t1, t2, strict);
946 if (TREE_CODE (t1) == RECORD_TYPE
947 && TREE_CODE (t2) == RECORD_TYPE)
952 if (! compexcepttypes (t1, t2))
955 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
956 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
957 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
961 /* Target types must match incl. qualifiers. We use ORIG_STRICT
962 here since this is the one place where
963 COMPARE_REDECLARATION should be used. */
964 val = comp_array_types (comptypes, t1, t2, orig_strict);
967 case TEMPLATE_TYPE_PARM:
968 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
969 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
972 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
974 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
979 return attrval == 2 && val == 1 ? 2 : val;
982 /* Subroutine of comp_target-types. Make sure that the cv-quals change
983 only in the same direction as the target type. */
986 comp_cv_target_types (ttl, ttr, nptrs)
992 if (!at_least_as_qualified_p (ttl, ttr)
993 && !at_least_as_qualified_p (ttr, ttl))
994 /* The qualifications are incomparable. */
997 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
998 return more_qualified_p (ttr, ttl) ? -1 : 1;
1000 t = comp_target_types (ttl, ttr, nptrs);
1001 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1002 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1008 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1009 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1010 converted to TTL. Return -1 means that TTL can be converted to TTR but
1013 NPTRS is the number of pointers we can strip off and keep cool.
1014 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1015 but to not permit B** to convert to A**.
1017 This should go away. Callers should use can_convert or something
1018 similar instead. (jason 17 Apr 1997) */
1021 comp_target_types (ttl, ttr, nptrs)
1025 ttl = TYPE_MAIN_VARIANT (ttl);
1026 ttr = TYPE_MAIN_VARIANT (ttr);
1027 if (same_type_p (ttl, ttr))
1030 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1033 if ((TREE_CODE (ttr) == POINTER_TYPE
1034 || TREE_CODE (ttr) == REFERENCE_TYPE)
1035 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1036 of the type pointed to. This is necessary for reference init
1037 semantics. We won't get here from a previous call with nptrs == 1;
1038 for multi-level pointers we end up in comp_ptr_ttypes. */
1041 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1043 ttl = TREE_TYPE (ttl);
1044 ttr = TREE_TYPE (ttr);
1048 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1049 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1051 else if (TREE_CODE (ttl) == VOID_TYPE
1052 && TREE_CODE (ttr) != FUNCTION_TYPE
1053 && TREE_CODE (ttr) != METHOD_TYPE
1054 && TREE_CODE (ttr) != OFFSET_TYPE)
1056 else if (TREE_CODE (ttr) == VOID_TYPE
1057 && TREE_CODE (ttl) != FUNCTION_TYPE
1058 && TREE_CODE (ttl) != METHOD_TYPE
1059 && TREE_CODE (ttl) != OFFSET_TYPE)
1061 else if (TREE_CODE (ttl) == POINTER_TYPE
1062 || TREE_CODE (ttl) == ARRAY_TYPE)
1064 if (comp_ptr_ttypes (ttl, ttr))
1066 else if (comp_ptr_ttypes (ttr, ttl))
1072 /* Const and volatile mean something different for function types,
1073 so the usual checks are not appropriate. */
1074 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1075 return comp_target_types (ttl, ttr, nptrs - 1);
1077 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1080 if (TREE_CODE (ttr) == ARRAY_TYPE)
1081 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1082 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1089 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1094 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1103 argsl = TYPE_ARG_TYPES (ttl);
1104 argsr = TYPE_ARG_TYPES (ttr);
1106 /* Compare 'this' here, not in comp_target_parms. */
1107 if (TREE_CODE (ttr) == METHOD_TYPE)
1109 tree tl = TYPE_METHOD_BASETYPE (ttl);
1110 tree tr = TYPE_METHOD_BASETYPE (ttr);
1112 if (!same_or_base_type_p (tr, tl))
1114 if (same_or_base_type_p (tl, tr))
1120 argsl = TREE_CHAIN (argsl);
1121 argsr = TREE_CHAIN (argsr);
1124 switch (comp_target_parms (argsl, argsr, 1))
1132 return saw_contra ? -1 : 1;
1135 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1139 /* Contravariance: we can assign a pointer to base member to a pointer
1140 to derived member. Note difference from simple pointer case, where
1141 we can pass a pointer to derived to a pointer to base. */
1142 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1143 TYPE_OFFSET_BASETYPE (ttl)))
1145 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1146 TYPE_OFFSET_BASETYPE (ttr)))
1156 ttl = TREE_TYPE (ttl);
1157 ttr = TREE_TYPE (ttr);
1159 if (TREE_CODE (ttl) == POINTER_TYPE
1160 || TREE_CODE (ttl) == ARRAY_TYPE)
1162 if (comp_ptr_ttypes (ttl, ttr))
1168 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1173 else if (IS_AGGR_TYPE (ttl))
1177 if (same_or_base_type_p (build_pointer_type (ttl),
1178 build_pointer_type (ttr)))
1180 if (same_or_base_type_p (build_pointer_type (ttr),
1181 build_pointer_type (ttl)))
1189 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1192 at_least_as_qualified_p (type1, type2)
1196 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1197 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1198 == CP_TYPE_QUALS (type2));
1201 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1204 more_qualified_p (type1, type2)
1208 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1209 && at_least_as_qualified_p (type1, type2));
1212 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1213 more cv-qualified that TYPE1, and 0 otherwise. */
1216 comp_cv_qualification (type1, type2)
1220 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1223 if (at_least_as_qualified_p (type1, type2))
1226 else if (at_least_as_qualified_p (type2, type1))
1232 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1233 subset of the cv-qualification signature of TYPE2, and the types
1234 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1237 comp_cv_qual_signature (type1, type2)
1241 if (comp_ptr_ttypes_real (type2, type1, -1))
1243 else if (comp_ptr_ttypes_real (type1, type2, -1))
1249 /* If two types share a common base type, return that basetype.
1250 If there is not a unique most-derived base type, this function
1251 returns ERROR_MARK_NODE. */
1254 common_base_type (tt1, tt2)
1257 tree best = NULL_TREE;
1260 /* If one is a baseclass of another, that's good enough. */
1261 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1263 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1266 /* Otherwise, try to find a unique baseclass of TT1
1267 that is shared by TT2, and follow that down. */
1268 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1270 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1271 tree trial = common_base_type (basetype, tt2);
1274 if (trial == error_mark_node)
1276 if (best == NULL_TREE)
1278 else if (best != trial)
1279 return error_mark_node;
1284 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1286 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1287 tree trial = common_base_type (tt1, basetype);
1290 if (trial == error_mark_node)
1292 if (best == NULL_TREE)
1294 else if (best != trial)
1295 return error_mark_node;
1301 /* Subroutines of `comptypes'. */
1303 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1304 equivalent in the sense that functions with those parameter types
1305 can have equivalent types. The two lists must be equivalent,
1308 C++: See comment above about TYPE1, TYPE2. */
1311 compparms (parms1, parms2)
1312 tree parms1, parms2;
1314 register tree t1 = parms1, t2 = parms2;
1316 /* An unspecified parmlist matches any specified parmlist
1317 whose argument types don't need default promotions. */
1321 if (t1 == 0 && t2 == 0)
1323 /* If one parmlist is shorter than the other,
1324 they fail to match. */
1325 if (t1 == 0 || t2 == 0)
1327 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1330 t1 = TREE_CHAIN (t1);
1331 t2 = TREE_CHAIN (t2);
1335 /* This really wants return whether or not parameter type lists
1336 would make their owning functions assignment compatible or not.
1338 The return value is like for comp_target_types.
1340 This should go away, possibly with the exception of the empty parmlist
1341 conversion; there are no conversions between function types in C++.
1342 (jason 17 Apr 1997) */
1345 comp_target_parms (parms1, parms2, strict)
1346 tree parms1, parms2;
1349 register tree t1 = parms1, t2 = parms2;
1350 int warn_contravariance = 0;
1352 /* In C, an unspecified parmlist matches any specified parmlist
1353 whose argument types don't need default promotions. This is not
1354 true for C++, but let's do it anyway for unfixed headers. */
1356 if (t1 == 0 && t2 != 0)
1358 if (! flag_strict_prototype && t2 == void_list_node)
1359 /* t1 might be the arglist of a function pointer in extern "C"
1360 declared to take (), which we fudged to (...). Don't make the
1361 user pay for our mistake. */;
1363 cp_pedwarn ("ANSI C++ prohibits conversion from `%#T' to `(...)'",
1365 return self_promoting_args_p (t2);
1368 return self_promoting_args_p (t1);
1370 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1374 /* If one parmlist is shorter than the other,
1375 they fail to match, unless STRICT is <= 0. */
1376 if (t1 == 0 || t2 == 0)
1381 return 1 + warn_contravariance;
1382 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1384 p1 = TREE_VALUE (t1);
1385 p2 = TREE_VALUE (t2);
1386 if (same_type_p (p1, p2))
1392 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1393 || (TREE_CODE (p1) == REFERENCE_TYPE
1394 && TREE_CODE (p2) == REFERENCE_TYPE))
1397 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1398 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1401 /* The following is wrong for contravariance,
1402 but many programs depend on it. */
1403 if (TREE_TYPE (p1) == void_type_node)
1405 if (TREE_TYPE (p2) == void_type_node)
1407 warn_contravariance = 1;
1410 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1411 && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (p1)),
1412 TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1415 /* Note backwards order due to contravariance. */
1416 if (comp_target_types (p2, p1, 1) <= 0)
1418 if (comp_target_types (p1, p2, 1) > 0)
1420 warn_contravariance = 1;
1427 return warn_contravariance ? -1 : 1;
1430 /* Return 1 if PARMS specifies a fixed number of parameters
1431 and none of their types is affected by default promotions. */
1434 self_promoting_args_p (parms)
1438 for (t = parms; t; t = TREE_CHAIN (t))
1440 register tree type = TREE_VALUE (t);
1442 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1448 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1451 if (C_PROMOTING_INTEGER_TYPE_P (type))
1457 /* Return an unsigned type the same as TYPE in other respects.
1459 C++: must make these work for type variants as well. */
1462 unsigned_type (type)
1465 tree type1 = TYPE_MAIN_VARIANT (type);
1466 if (type1 == signed_char_type_node || type1 == char_type_node)
1467 return unsigned_char_type_node;
1468 if (type1 == integer_type_node)
1469 return unsigned_type_node;
1470 if (type1 == short_integer_type_node)
1471 return short_unsigned_type_node;
1472 if (type1 == long_integer_type_node)
1473 return long_unsigned_type_node;
1474 if (type1 == long_long_integer_type_node)
1475 return long_long_unsigned_type_node;
1476 #if HOST_BITS_PER_WIDE_INT >= 64
1477 if (type1 == intTI_type_node)
1478 return unsigned_intTI_type_node;
1480 if (type1 == intDI_type_node)
1481 return unsigned_intDI_type_node;
1482 if (type1 == intSI_type_node)
1483 return unsigned_intSI_type_node;
1484 if (type1 == intHI_type_node)
1485 return unsigned_intHI_type_node;
1486 if (type1 == intQI_type_node)
1487 return unsigned_intQI_type_node;
1489 return signed_or_unsigned_type (1, type);
1492 /* Return a signed type the same as TYPE in other respects. */
1498 tree type1 = TYPE_MAIN_VARIANT (type);
1499 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1500 return signed_char_type_node;
1501 if (type1 == unsigned_type_node)
1502 return integer_type_node;
1503 if (type1 == short_unsigned_type_node)
1504 return short_integer_type_node;
1505 if (type1 == long_unsigned_type_node)
1506 return long_integer_type_node;
1507 if (type1 == long_long_unsigned_type_node)
1508 return long_long_integer_type_node;
1509 #if HOST_BITS_PER_WIDE_INT >= 64
1510 if (type1 == unsigned_intTI_type_node)
1511 return intTI_type_node;
1513 if (type1 == unsigned_intDI_type_node)
1514 return intDI_type_node;
1515 if (type1 == unsigned_intSI_type_node)
1516 return intSI_type_node;
1517 if (type1 == unsigned_intHI_type_node)
1518 return intHI_type_node;
1519 if (type1 == unsigned_intQI_type_node)
1520 return intQI_type_node;
1522 return signed_or_unsigned_type (0, type);
1525 /* Return a type the same as TYPE except unsigned or
1526 signed according to UNSIGNEDP. */
1529 signed_or_unsigned_type (unsignedp, type)
1533 if (! INTEGRAL_TYPE_P (type)
1534 || TREE_UNSIGNED (type) == unsignedp)
1537 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1538 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1539 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1540 return unsignedp ? unsigned_type_node : integer_type_node;
1541 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1542 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1543 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1544 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1545 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1546 return (unsignedp ? long_long_unsigned_type_node
1547 : long_long_integer_type_node);
1551 /* Compute the value of the `sizeof' operator. */
1557 enum tree_code code = TREE_CODE (type);
1560 if (processing_template_decl)
1561 return build_min (SIZEOF_EXPR, sizetype, type);
1563 if (code == FUNCTION_TYPE)
1565 if (pedantic || warn_pointer_arith)
1566 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1567 return size_int (1);
1569 if (code == METHOD_TYPE)
1571 if (pedantic || warn_pointer_arith)
1572 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1573 return size_int (1);
1575 if (code == VOID_TYPE)
1577 if (pedantic || warn_pointer_arith)
1578 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1579 return size_int (1);
1581 if (code == ERROR_MARK)
1582 return size_int (1);
1584 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1585 referenced object.'' */
1586 if (code == REFERENCE_TYPE)
1587 type = TREE_TYPE (type);
1589 /* We couldn't find anything in the ARM or the draft standard that says,
1590 one way or the other, if doing sizeof on something that doesn't have
1591 an object associated with it is correct or incorrect. For example, if
1592 you declare `struct S { char str[16]; };', and in your program do
1593 a `sizeof (S::str)', should we flag that as an error or should we give
1594 the size of it? Since it seems like a reasonable thing to do, we'll go
1595 with giving the value. */
1596 if (code == OFFSET_TYPE)
1597 type = TREE_TYPE (type);
1599 /* @@ This also produces an error for a signature ref.
1600 In that case we should be able to do better. */
1601 if (IS_SIGNATURE (type))
1603 error ("`sizeof' applied to a signature type");
1604 return size_int (0);
1607 if (TYPE_SIZE (complete_type (type)) == 0)
1609 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1610 return size_int (0);
1613 /* Convert in case a char is more than one unit. */
1614 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1615 size_int (TYPE_PRECISION (char_type_node)));
1616 t = convert (sizetype, t);
1617 /* size_binop does not put the constant in range, so do it now. */
1618 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1619 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1627 if (processing_template_decl)
1628 return build_min (SIZEOF_EXPR, sizetype, e);
1630 if (TREE_CODE (e) == COMPONENT_REF
1631 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1632 error ("sizeof applied to a bit-field");
1633 /* ANSI says arrays and functions are converted inside comma.
1634 But we can't really convert them in build_compound_expr
1635 because that would break commas in lvalues.
1636 So do the conversion here if operand was a comma. */
1637 if (TREE_CODE (e) == COMPOUND_EXPR
1638 && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1639 || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1640 e = default_conversion (e);
1641 else if (is_overloaded_fn (e))
1643 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1644 return size_int (1);
1646 else if (type_unknown_p (e))
1648 incomplete_type_error (e, TREE_TYPE (e));
1649 return size_int (1);
1652 return c_sizeof (TREE_TYPE (e));
1656 c_sizeof_nowarn (type)
1659 enum tree_code code = TREE_CODE (type);
1662 if (code == FUNCTION_TYPE
1663 || code == METHOD_TYPE
1664 || code == VOID_TYPE
1665 || code == ERROR_MARK)
1666 return size_int (1);
1667 if (code == REFERENCE_TYPE)
1668 type = TREE_TYPE (type);
1670 if (TYPE_SIZE (type) == 0)
1671 return size_int (0);
1673 /* Convert in case a char is more than one unit. */
1674 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1675 size_int (TYPE_PRECISION (char_type_node)));
1676 t = convert (sizetype, t);
1677 force_fit_type (t, 0);
1681 /* Implement the __alignof keyword: Return the minimum required
1682 alignment of TYPE, measured in bytes. */
1688 enum tree_code code = TREE_CODE (type);
1691 if (processing_template_decl)
1692 return build_min (ALIGNOF_EXPR, sizetype, type);
1694 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1695 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1697 if (code == VOID_TYPE || code == ERROR_MARK)
1698 return size_int (1);
1700 /* C++: this is really correct! */
1701 if (code == REFERENCE_TYPE)
1702 type = TREE_TYPE (type);
1704 /* @@ This also produces an error for a signature ref.
1705 In that case we should be able to do better. */
1706 if (IS_SIGNATURE (type))
1708 error ("`__alignof' applied to a signature type");
1709 return size_int (1);
1712 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1713 force_fit_type (t, 0);
1717 /* Perform the array-to-pointer and function-to-pointer conversions
1720 In addition, references are converted to rvalues and manifest
1721 constants are replaced by their values. */
1724 decay_conversion (exp)
1728 register enum tree_code code;
1730 if (TREE_CODE (exp) == OFFSET_REF)
1731 exp = resolve_offset_ref (exp);
1733 type = TREE_TYPE (exp);
1734 code = TREE_CODE (type);
1736 if (code == REFERENCE_TYPE)
1738 exp = convert_from_reference (exp);
1739 type = TREE_TYPE (exp);
1740 code = TREE_CODE (type);
1743 /* Constants can be used directly unless they're not loadable. */
1744 if (TREE_CODE (exp) == CONST_DECL)
1745 exp = DECL_INITIAL (exp);
1746 /* Replace a nonvolatile const static variable with its value. We
1747 don't do this for arrays, though; we want the address of the
1748 first element of the array, not the address of the first element
1749 of its initializing constant. We *do* replace variables that the
1750 user isn't really supposed to know about; this is a hack to deal
1751 with __PRETTY_FUNCTION__ and the like. */
1752 else if (TREE_READONLY_DECL_P (exp)
1753 && (code != ARRAY_TYPE
1754 || (TREE_CODE (exp) == VAR_DECL && DECL_IGNORED_P (exp))))
1756 exp = decl_constant_value (exp);
1757 type = TREE_TYPE (exp);
1760 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1761 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1763 if (code == VOID_TYPE)
1765 error ("void value not ignored as it ought to be");
1766 return error_mark_node;
1768 if (code == METHOD_TYPE)
1769 my_friendly_abort (990506);
1770 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1771 return build_unary_op (ADDR_EXPR, exp, 0);
1772 if (code == ARRAY_TYPE)
1777 if (TREE_CODE (exp) == INDIRECT_REF)
1779 /* Stripping away the INDIRECT_REF is not the right
1780 thing to do for references... */
1781 tree inner = TREE_OPERAND (exp, 0);
1782 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1784 inner = build1 (CONVERT_EXPR,
1785 build_pointer_type (TREE_TYPE
1786 (TREE_TYPE (inner))),
1788 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1790 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1793 if (TREE_CODE (exp) == COMPOUND_EXPR)
1795 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1796 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1797 TREE_OPERAND (exp, 0), op1);
1801 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1803 error ("invalid use of non-lvalue array");
1804 return error_mark_node;
1807 ptrtype = build_pointer_type (TREE_TYPE (type));
1809 if (TREE_CODE (exp) == VAR_DECL)
1811 /* ??? This is not really quite correct
1812 in that the type of the operand of ADDR_EXPR
1813 is not the target type of the type of the ADDR_EXPR itself.
1814 Question is, can this lossage be avoided? */
1815 adr = build1 (ADDR_EXPR, ptrtype, exp);
1816 if (mark_addressable (exp) == 0)
1817 return error_mark_node;
1818 TREE_CONSTANT (adr) = staticp (exp);
1819 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1822 /* This way is better for a COMPONENT_REF since it can
1823 simplify the offset for a component. */
1824 adr = build_unary_op (ADDR_EXPR, exp, 1);
1825 return cp_convert (ptrtype, adr);
1832 default_conversion (exp)
1836 enum tree_code code;
1838 exp = decay_conversion (exp);
1840 type = TREE_TYPE (exp);
1841 code = TREE_CODE (type);
1843 if (INTEGRAL_CODE_P (code))
1845 tree t = type_promotes_to (type);
1847 return cp_convert (t, exp);
1853 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1857 inline_conversion (exp)
1860 if (TREE_CODE (exp) == FUNCTION_DECL)
1861 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1866 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1867 decay_conversion to one. */
1870 string_conv_p (totype, exp, warn)
1876 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1879 t = TREE_TYPE (totype);
1880 if (!same_type_p (t, char_type_node)
1881 && !same_type_p (t, wchar_type_node))
1884 if (TREE_CODE (exp) == STRING_CST)
1886 /* Make sure that we don't try to convert between char and wchar_t. */
1887 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1892 /* Is this a string constant which has decayed to 'const char *'? */
1893 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1894 if (!same_type_p (TREE_TYPE (exp), t))
1897 if (TREE_CODE (exp) != ADDR_EXPR
1898 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1902 /* This warning is not very useful, as it complains about printf. */
1903 if (warn && warn_write_strings)
1904 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1910 build_object_ref (datum, basetype, field)
1911 tree datum, basetype, field;
1914 if (datum == error_mark_node)
1915 return error_mark_node;
1917 dtype = TREE_TYPE (datum);
1918 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1919 dtype = TREE_TYPE (dtype);
1920 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1922 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1923 basetype, field, dtype);
1924 return error_mark_node;
1926 else if (IS_SIGNATURE (basetype))
1928 warning ("signature name in scope resolution ignored");
1929 return build_component_ref (datum, field, NULL_TREE, 1);
1931 else if (is_aggr_type (basetype, 1))
1933 tree binfo = binfo_or_else (basetype, dtype);
1935 return build_x_component_ref (build_scoped_ref (datum, basetype),
1938 return error_mark_node;
1941 /* Like `build_component_ref, but uses an already found field, and converts
1942 from a reference. Must compute access for current_class_ref.
1946 build_component_ref_1 (datum, field, protect)
1950 return convert_from_reference
1951 (build_component_ref (datum, field, NULL_TREE, protect));
1954 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1955 can, for example, use as an lvalue. This code used to be in
1956 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1957 expressions, where we're dealing with aggregates. But now it's again only
1958 called from unary_complex_lvalue. The case (in particular) that led to
1959 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1963 rationalize_conditional_expr (code, t)
1964 enum tree_code code;
1967 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1968 the first operand is always the one to be used if both operands
1969 are equal, so we know what conditional expression this used to be. */
1970 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1973 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1974 ? LE_EXPR : GE_EXPR),
1975 TREE_OPERAND (t, 0),
1976 TREE_OPERAND (t, 1)),
1977 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1978 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1982 build_conditional_expr (TREE_OPERAND (t, 0),
1983 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1984 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1987 /* Given the TYPE of an anonymous union field inside T, return the
1988 FIELD_DECL for the field. If not found return NULL_TREE. Because
1989 anonymous unions can nest, we must also search all anonymous unions
1990 that are directly reachable. */
1993 lookup_anon_field (t, type)
1998 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2000 if (TREE_STATIC (field))
2002 if (TREE_CODE (field) != FIELD_DECL)
2005 /* If we find it directly, return the field. */
2006 if (DECL_NAME (field) == NULL_TREE
2007 && type == TREE_TYPE (field))
2012 /* Otherwise, it could be nested, search harder. */
2013 if (DECL_NAME (field) == NULL_TREE
2014 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2016 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2024 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2025 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2026 that we are interested in, or it can be a FIELD_DECL. */
2029 build_component_ref (datum, component, basetype_path, protect)
2030 tree datum, component, basetype_path;
2033 register tree basetype;
2034 register enum tree_code code;
2035 register tree field = NULL;
2040 if (processing_template_decl)
2041 return build_min_nt (COMPONENT_REF, datum, component);
2043 if (datum == error_mark_node
2044 || TREE_TYPE (datum) == error_mark_node)
2045 return error_mark_node;
2047 /* BASETYPE holds the type of the class containing the COMPONENT. */
2048 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2050 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2052 switch (TREE_CODE (datum))
2056 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2057 basetype_path, protect);
2058 return build (COMPOUND_EXPR, TREE_TYPE (value),
2059 TREE_OPERAND (datum, 0), value);
2062 return build_conditional_expr
2063 (TREE_OPERAND (datum, 0),
2064 build_component_ref (TREE_OPERAND (datum, 1), component,
2065 basetype_path, protect),
2066 build_component_ref (TREE_OPERAND (datum, 2), component,
2067 basetype_path, protect));
2070 cp_error ("invalid use of %D", datum);
2071 datum = error_mark_node;
2078 code = TREE_CODE (basetype);
2080 if (code == REFERENCE_TYPE)
2082 datum = convert_from_reference (datum);
2083 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2084 code = TREE_CODE (basetype);
2086 if (TREE_CODE (datum) == OFFSET_REF)
2088 datum = resolve_offset_ref (datum);
2089 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2090 code = TREE_CODE (basetype);
2093 /* First, see if there is a field or component with name COMPONENT. */
2094 if (TREE_CODE (component) == TREE_LIST)
2096 /* I could not trigger this code. MvL */
2097 my_friendly_abort (980326);
2099 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2100 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2102 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2105 if (! IS_AGGR_TYPE_CODE (code))
2107 if (code != ERROR_MARK)
2108 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2109 component, datum, basetype);
2110 return error_mark_node;
2113 if (!complete_type_or_else (basetype, datum))
2114 return error_mark_node;
2116 if (TREE_CODE (component) == BIT_NOT_EXPR)
2118 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2120 cp_error ("destructor specifier `%T::~%T' must have matching names",
2121 basetype, TREE_OPERAND (component, 0));
2122 return error_mark_node;
2124 if (! TYPE_HAS_DESTRUCTOR (basetype))
2126 cp_error ("type `%T' has no destructor", basetype);
2127 return error_mark_node;
2129 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2132 /* Look up component name in the structure type definition. */
2133 if (CLASSTYPE_VFIELD (basetype)
2134 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
2135 /* Special-case this because if we use normal lookups in an ambiguous
2136 hierarchy, the compiler will abort (because vptr lookups are
2137 not supposed to be ambiguous. */
2138 field = CLASSTYPE_VFIELD (basetype);
2139 else if (TREE_CODE (component) == FIELD_DECL)
2141 else if (TREE_CODE (component) == TYPE_DECL)
2143 cp_error ("invalid use of type decl `%#D' as expression", component);
2144 return error_mark_node;
2148 tree name = component;
2149 if (TREE_CODE (component) == VAR_DECL)
2150 name = DECL_NAME (component);
2151 if (basetype_path == NULL_TREE)
2152 basetype_path = TYPE_BINFO (basetype);
2153 field = lookup_field (basetype_path, name,
2154 protect && !VFIELD_NAME_P (name), 0);
2155 if (field == error_mark_node)
2156 return error_mark_node;
2158 if (field == NULL_TREE)
2160 /* Not found as a data field, look for it as a method. If found,
2161 then if this is the only possible one, return it, else
2162 report ambiguity error. */
2163 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2164 if (fndecls == error_mark_node)
2165 return error_mark_node;
2168 /* If the function is unique and static, we can resolve it
2169 now. Otherwise, we have to wait and see what context it is
2170 used in; a component_ref involving a non-static member
2171 function can only be used in a call (expr.ref). */
2173 if (TREE_CHAIN (fndecls) == NULL_TREE
2174 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2176 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2178 tree fndecl = TREE_VALUE (fndecls);
2179 enforce_access (TREE_PURPOSE (fndecls), fndecl);
2185 /* A unique non-static member function. Other parts
2186 of the compiler expect something with
2187 unknown_type_node to be really overloaded, so
2189 TREE_VALUE (fndecls)
2190 = scratch_ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2194 ref = build (COMPONENT_REF, unknown_type_node,
2195 datum, TREE_VALUE (fndecls));
2199 cp_error ("`%#T' has no member named `%D'", basetype, name);
2200 return error_mark_node;
2202 else if (TREE_TYPE (field) == error_mark_node)
2203 return error_mark_node;
2205 if (TREE_CODE (field) != FIELD_DECL)
2207 if (TREE_CODE (field) == TYPE_DECL)
2208 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2209 else if (DECL_RTL (field) != 0)
2212 TREE_USED (field) = 1;
2217 /* See if we have to do any conversions so that we pick up the field from the
2219 if (DECL_FIELD_CONTEXT (field) != basetype)
2221 tree context = DECL_FIELD_CONTEXT (field);
2222 tree base = context;
2223 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2224 && ANON_UNION_TYPE_P (base))
2226 base = TYPE_CONTEXT (base);
2229 /* Handle base classes here... */
2230 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2232 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2233 if (integer_zerop (addr))
2235 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2236 return error_mark_node;
2238 if (VBASE_NAME_P (DECL_NAME (field)))
2240 /* It doesn't matter which vbase pointer we grab, just
2241 find one of them. */
2242 tree binfo = get_binfo (base,
2243 TREE_TYPE (TREE_TYPE (addr)), 0);
2244 addr = convert_pointer_to_real (binfo, addr);
2247 addr = convert_pointer_to (base, addr);
2248 datum = build_indirect_ref (addr, NULL_PTR);
2249 my_friendly_assert (datum != error_mark_node, 311);
2253 /* Handle things from anon unions here... */
2254 if (TYPE_NAME (context) && ANON_UNION_TYPE_P (context))
2256 tree subfield = lookup_anon_field (basetype, context);
2257 tree subdatum = build_component_ref (datum, subfield,
2258 basetype_path, protect);
2259 return build_component_ref (subdatum, field, basetype_path, protect);
2263 /* Compute the type of the field, as described in [expr.ref]. */
2264 type_quals = TYPE_UNQUALIFIED;
2265 field_type = TREE_TYPE (field);
2266 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2267 /* The standard says that the type of the result should be the
2268 type referred to by the reference. But for now, at least, we
2269 do the conversion from reference type later. */
2273 type_quals = (CP_TYPE_QUALS (field_type)
2274 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2276 /* A field is const (volatile) if the enclosing object, or the
2277 field itself, is const (volatile). But, a mutable field is
2278 not const, even within a const object. */
2279 if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2280 type_quals &= ~TYPE_QUAL_CONST;
2281 if (!IS_SIGNATURE (field_type))
2282 field_type = cp_build_qualified_type (field_type, type_quals);
2285 ref = fold (build (COMPONENT_REF, field_type,
2286 break_out_cleanups (datum), field));
2288 /* Mark the expression const or volatile, as appropriate. Even
2289 though we've dealt with the type above, we still have to mark the
2290 expression itself. */
2291 if (type_quals & TYPE_QUAL_CONST)
2292 TREE_READONLY (ref) = 1;
2293 else if (type_quals & TYPE_QUAL_VOLATILE)
2294 TREE_THIS_VOLATILE (ref) = 1;
2299 /* Variant of build_component_ref for use in expressions, which should
2300 never have REFERENCE_TYPE. */
2303 build_x_component_ref (datum, component, basetype_path, protect)
2304 tree datum, component, basetype_path;
2307 tree t = build_component_ref (datum, component, basetype_path, protect);
2309 if (! processing_template_decl)
2310 t = convert_from_reference (t);
2315 /* Given an expression PTR for a pointer, return an expression
2316 for the value pointed to.
2317 ERRORSTRING is the name of the operator to appear in error messages.
2319 This function may need to overload OPERATOR_FNNAME.
2320 Must also handle REFERENCE_TYPEs for C++. */
2323 build_x_indirect_ref (ptr, errorstring)
2325 const char *errorstring;
2329 if (processing_template_decl)
2330 return build_min_nt (INDIRECT_REF, ptr);
2332 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2336 return build_indirect_ref (ptr, errorstring);
2340 build_indirect_ref (ptr, errorstring)
2342 const char *errorstring;
2344 register tree pointer, type;
2346 if (ptr == error_mark_node)
2347 return error_mark_node;
2349 if (ptr == current_class_ptr)
2350 return current_class_ref;
2352 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2353 ? ptr : default_conversion (ptr));
2354 type = TREE_TYPE (pointer);
2356 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2360 If the type of the expression is "pointer to T," the type
2361 of the result is "T."
2363 We must use the canonical variant because certain parts of
2364 the back end, like fold, do pointer comparisons between
2366 tree t = canonical_type_variant (TREE_TYPE (type));
2368 if (TREE_CODE (pointer) == ADDR_EXPR
2370 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2371 /* The POINTER was something like `&x'. We simplify `*&x' to
2373 return TREE_OPERAND (pointer, 0);
2376 tree ref = build1 (INDIRECT_REF, t, pointer);
2378 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2379 so that we get the proper error message if the result is used
2380 to assign to. Also, &* is supposed to be a no-op. */
2381 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2382 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2383 TREE_SIDE_EFFECTS (ref)
2384 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2389 /* `pointer' won't be an error_mark_node if we were given a
2390 pointer to member, so it's cool to check for this here. */
2391 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2392 error ("invalid use of `%s' on pointer to member", errorstring);
2393 else if (TREE_CODE (type) == RECORD_TYPE
2394 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2395 error ("cannot dereference signature pointer/reference");
2396 else if (pointer != error_mark_node)
2399 error ("invalid type argument of `%s'", errorstring);
2401 error ("invalid type argument");
2403 return error_mark_node;
2406 /* This handles expressions of the form "a[i]", which denotes
2409 This is logically equivalent in C to *(a+i), but we may do it differently.
2410 If A is a variable or a member, we generate a primitive ARRAY_REF.
2411 This avoids forcing the array out of registers, and can work on
2412 arrays that are not lvalues (for example, members of structures returned
2415 If INDEX is of some user-defined type, it must be converted to
2416 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2417 will inherit the type of the array, which will be some pointer type. */
2420 build_array_ref (array, idx)
2425 error ("subscript missing in array reference");
2426 return error_mark_node;
2429 if (TREE_TYPE (array) == error_mark_node
2430 || TREE_TYPE (idx) == error_mark_node)
2431 return error_mark_node;
2433 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2434 && TREE_CODE (array) != INDIRECT_REF)
2438 /* Subscripting with type char is likely to lose
2439 on a machine where chars are signed.
2440 So warn on any machine, but optionally.
2441 Don't warn for unsigned char since that type is safe.
2442 Don't warn for signed char because anyone who uses that
2443 must have done so deliberately. */
2444 if (warn_char_subscripts
2445 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2446 warning ("array subscript has type `char'");
2448 /* Apply default promotions *after* noticing character types. */
2449 idx = default_conversion (idx);
2451 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2453 error ("array subscript is not an integer");
2454 return error_mark_node;
2457 /* An array that is indexed by a non-constant
2458 cannot be stored in a register; we must be able to do
2459 address arithmetic on its address.
2460 Likewise an array of elements of variable size. */
2461 if (TREE_CODE (idx) != INTEGER_CST
2462 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2463 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2466 if (mark_addressable (array) == 0)
2467 return error_mark_node;
2469 /* An array that is indexed by a constant value which is not within
2470 the array bounds cannot be stored in a register either; because we
2471 would get a crash in store_bit_field/extract_bit_field when trying
2472 to access a non-existent part of the register. */
2473 if (TREE_CODE (idx) == INTEGER_CST
2474 && TYPE_VALUES (TREE_TYPE (array))
2475 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2477 if (mark_addressable (array) == 0)
2478 return error_mark_node;
2481 if (pedantic && !lvalue_p (array))
2482 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2484 /* Note in C++ it is valid to subscript a `register' array, since
2485 it is valid to take the address of something with that
2486 storage specification. */
2490 while (TREE_CODE (foo) == COMPONENT_REF)
2491 foo = TREE_OPERAND (foo, 0);
2492 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2493 warning ("subscripting array declared `register'");
2496 type = TREE_TYPE (TREE_TYPE (array));
2497 rval = build (ARRAY_REF, type, array, idx);
2498 /* Array ref is const/volatile if the array elements are
2499 or if the array is.. */
2500 TREE_READONLY (rval)
2501 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2502 TREE_SIDE_EFFECTS (rval)
2503 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2504 TREE_THIS_VOLATILE (rval)
2505 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2506 return require_complete_type (fold (rval));
2510 tree ar = default_conversion (array);
2511 tree ind = default_conversion (idx);
2513 /* Put the integer in IND to simplify error checking. */
2514 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2521 if (ar == error_mark_node)
2524 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2526 error ("subscripted value is neither array nor pointer");
2527 return error_mark_node;
2529 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2531 error ("array subscript is not an integer");
2532 return error_mark_node;
2535 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar,
2541 /* Build a function call to function FUNCTION with parameters PARAMS.
2542 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2543 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2544 not include any object pointer that may be required. FUNCTION's
2545 data type may be a function type or a pointer-to-function.
2547 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2548 is the list of possible methods that FUNCTION could conceivably
2549 be. If the list of methods comes from a class, then it will be
2550 a list of lists (where each element is associated with the class
2551 that produced it), otherwise it will be a simple list (for
2552 functions overloaded in global scope).
2554 In the first case, TREE_VALUE (function) is the head of one of those
2555 lists, and TREE_PURPOSE is the name of the function.
2557 In the second case, TREE_PURPOSE (function) is the function's
2560 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2562 When calling a TEMPLATE_DECL, we don't require a complete return
2566 build_x_function_call (function, params, decl)
2567 tree function, params, decl;
2570 tree template_id = NULL_TREE;
2573 if (function == error_mark_node)
2574 return error_mark_node;
2576 if (processing_template_decl)
2577 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2579 /* Save explicit template arguments if found */
2580 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2582 template_id = function;
2583 function = TREE_OPERAND (function, 0);
2586 type = TREE_TYPE (function);
2588 if (TREE_CODE (type) == OFFSET_TYPE
2589 && TREE_TYPE (type) == unknown_type_node
2590 && TREE_CODE (function) == TREE_LIST
2591 && TREE_CHAIN (function) == NULL_TREE)
2593 /* Undo (Foo:bar)()... */
2594 type = TYPE_OFFSET_BASETYPE (type);
2595 function = TREE_VALUE (function);
2596 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2597 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2598 function = TREE_VALUE (function);
2599 if (TREE_CODE (function) == OVERLOAD)
2600 function = OVL_FUNCTION (function);
2601 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2602 function = DECL_NAME (function);
2603 return build_method_call (decl, function, params,
2604 TYPE_BINFO (type), LOOKUP_NORMAL);
2607 if ((TREE_CODE (function) == FUNCTION_DECL
2608 && DECL_STATIC_FUNCTION_P (function))
2609 || (TREE_CODE (function) == TEMPLATE_DECL
2610 && DECL_STATIC_FUNCTION_P (DECL_RESULT (function))))
2611 return build_member_call(DECL_CONTEXT (function),
2613 ? template_id : DECL_NAME (function),
2616 is_method = ((TREE_CODE (function) == TREE_LIST
2617 && current_class_type != NULL_TREE
2618 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2620 || (TREE_CODE (function) == OVERLOAD
2621 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2622 || TREE_CODE (function) == IDENTIFIER_NODE
2623 || TREE_CODE (type) == METHOD_TYPE
2624 || TYPE_PTRMEMFUNC_P (type));
2626 /* A friend template. Make it look like a toplevel declaration. */
2627 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2628 function = scratch_ovl_cons (function, NULL_TREE);
2630 /* Handle methods, friends, and overloaded functions, respectively. */
2633 tree basetype = NULL_TREE;
2635 if (TREE_CODE (function) == OVERLOAD)
2636 function = OVL_CURRENT (function);
2638 if (TREE_CODE (function) == FUNCTION_DECL
2639 || DECL_FUNCTION_TEMPLATE_P (function))
2641 basetype = DECL_CLASS_CONTEXT (function);
2643 if (DECL_NAME (function))
2644 function = DECL_NAME (function);
2646 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2648 else if (TREE_CODE (function) == TREE_LIST)
2650 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2651 == FUNCTION_DECL, 312);
2652 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2653 function = TREE_PURPOSE (function);
2655 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2657 if (TREE_CODE (function) == OFFSET_REF)
2659 if (TREE_OPERAND (function, 0))
2660 decl = TREE_OPERAND (function, 0);
2662 /* Call via a pointer to member function. */
2663 if (decl == NULL_TREE)
2665 error ("pointer to member function called, but not in class scope");
2666 return error_mark_node;
2668 /* What other type of POINTER_TYPE could this be? */
2669 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2670 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2671 && TREE_CODE (function) != OFFSET_REF)
2672 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2677 /* this is an abbreviated method call.
2678 must go through here in case it is a virtual function.
2679 @@ Perhaps this could be optimized. */
2681 if (basetype && (! current_class_type
2682 || ! DERIVED_FROM_P (basetype, current_class_type)))
2683 return build_member_call (basetype, function, params);
2685 if (decl == NULL_TREE)
2687 if (current_class_type == NULL_TREE)
2689 cp_error ("object missing in call to method `%D'", function);
2690 return error_mark_node;
2692 /* Yow: call from a static member function. */
2693 decl = build_dummy_object (current_class_type);
2696 /* Put back explicit template arguments, if any. */
2698 function = template_id;
2699 return build_method_call (decl, function, params,
2700 NULL_TREE, LOOKUP_NORMAL);
2702 else if (TREE_CODE (function) == COMPONENT_REF
2703 && type == unknown_type_node)
2705 /* Undo what we did in build_component_ref. */
2706 decl = TREE_OPERAND (function, 0);
2707 function = TREE_OPERAND (function, 1);
2708 function = DECL_NAME (OVL_CURRENT (function));
2712 TREE_OPERAND (template_id, 0) = function;
2713 function = template_id;
2716 return build_method_call (decl, function, params,
2717 NULL_TREE, LOOKUP_NORMAL);
2719 else if (really_overloaded_fn (function))
2721 if (OVL_FUNCTION (function) == NULL_TREE)
2723 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2724 TREE_PURPOSE (function));
2725 return error_mark_node;
2729 /* Put back explicit template arguments, if any. */
2731 function = template_id;
2732 return build_new_function_call (function, params);
2736 /* Remove a potential OVERLOAD around it */
2737 function = OVL_CURRENT (function);
2740 if (TREE_CODE (function) == OFFSET_REF)
2742 /* If the component is a data element (or a virtual function), we play
2743 games here to make things work. */
2746 if (TREE_OPERAND (function, 0))
2747 decl = TREE_OPERAND (function, 0);
2749 decl = current_class_ref;
2751 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2753 /* Sigh. OFFSET_REFs are being used for too many things.
2754 They're being used both for -> and ->*, and we want to resolve
2755 the -> cases here, but leave the ->*. We could use
2756 resolve_offset_ref for those, too, but it would call
2757 get_member_function_from_ptrfunc and decl_addr wouldn't get
2758 updated properly. Nasty. */
2759 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2760 function = resolve_offset_ref (function);
2762 function = TREE_OPERAND (function, 1);
2764 function = get_member_function_from_ptrfunc (&decl_addr, function);
2765 params = expr_tree_cons (NULL_TREE, decl_addr, params);
2766 return build_function_call (function, params);
2769 type = TREE_TYPE (function);
2770 if (type != error_mark_node)
2772 if (TREE_CODE (type) == REFERENCE_TYPE)
2773 type = TREE_TYPE (type);
2775 if (IS_AGGR_TYPE (type))
2776 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2781 tree fntype = TREE_TYPE (function);
2782 tree ctypeptr = NULL_TREE;
2784 /* Explicitly named method? */
2785 if (TREE_CODE (function) == FUNCTION_DECL)
2786 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2787 /* Expression with ptr-to-method type? It could either be a plain
2788 usage, or it might be a case where the ptr-to-method is being
2789 passed in as an argument. */
2790 else if (TYPE_PTRMEMFUNC_P (fntype))
2792 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2793 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2794 ctypeptr = build_pointer_type (rec);
2796 /* Unexpected node type? */
2798 my_friendly_abort (116);
2799 if (decl == NULL_TREE)
2801 if (current_function_decl
2802 && DECL_STATIC_FUNCTION_P (current_function_decl))
2803 error ("invalid call to member function needing `this' in static member function scope");
2805 error ("pointer to member function called, but not in class scope");
2806 return error_mark_node;
2808 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2809 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2811 decl = build_unary_op (ADDR_EXPR, decl, 0);
2812 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2815 decl = build_c_cast (ctypeptr, decl);
2816 params = expr_tree_cons (NULL_TREE, decl, params);
2819 return build_function_call (function, params);
2822 /* Resolve a pointer to member function. INSTANCE is the object
2823 instance to use, if the member points to a virtual member. */
2826 get_member_function_from_ptrfunc (instance_ptrptr, function)
2827 tree *instance_ptrptr;
2830 if (TREE_CODE (function) == OFFSET_REF)
2832 function = TREE_OPERAND (function, 1);
2835 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2837 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2838 tree instance, basetype;
2840 tree instance_ptr = *instance_ptrptr;
2842 if (TREE_SIDE_EFFECTS (instance_ptr))
2843 instance_ptr = save_expr (instance_ptr);
2845 if (TREE_SIDE_EFFECTS (function))
2846 function = save_expr (function);
2848 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2849 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2851 delta = cp_convert (ptrdiff_type_node,
2852 build_component_ref (function, delta_identifier,
2854 e3 = PFN_FROM_PTRMEMFUNC (function);
2856 if (TYPE_SIZE (basetype) != NULL_TREE
2857 && ! TYPE_VIRTUAL_P (basetype))
2858 /* If basetype doesn't have virtual functions, don't emit code to
2859 handle that case. */
2863 /* Promoting idx before saving it improves performance on RISC
2864 targets. Without promoting, the first compare used
2865 load-with-sign-extend, while the second used normal load then
2866 shift to sign-extend. An optimizer flaw, perhaps, but it's
2867 easier to make this change. */
2868 idx = save_expr (default_conversion
2869 (build_component_ref (function,
2872 e1 = build_binary_op (GE_EXPR, idx, integer_zero_node);
2874 /* Convert down to the right base, before using the instance. */
2875 instance = convert_pointer_to_real (basetype, instance_ptr);
2876 if (instance == error_mark_node && instance_ptr != error_mark_node)
2879 vtbl = convert_pointer_to (ptr_type_node, instance);
2880 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2883 build_pointer_type (build_pointer_type (vtable_entry_type)),
2884 vtbl, cp_convert (ptrdiff_type_node, delta2));
2885 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2886 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2889 if (! flag_vtable_thunks)
2891 aref = save_expr (aref);
2893 delta = build_binary_op
2895 build_conditional_expr (e1,
2896 build_component_ref (aref,
2903 if (flag_vtable_thunks)
2906 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2907 TREE_TYPE (e2) = TREE_TYPE (e3);
2908 e1 = build_conditional_expr (e1, e2, e3);
2910 /* Make sure this doesn't get evaluated first inside one of the
2911 branches of the COND_EXPR. */
2912 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2913 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2917 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2918 instance_ptr, delta);
2920 if (instance_ptr == error_mark_node
2921 && TREE_CODE (e1) != ADDR_EXPR
2922 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2923 cp_error ("object missing in `%E'", function);
2931 build_function_call_real (function, params, require_complete, flags)
2932 tree function, params;
2933 int require_complete, flags;
2935 register tree fntype, fndecl;
2936 register tree value_type;
2937 register tree coerced_params;
2938 tree name = NULL_TREE, assembler_name = NULL_TREE;
2941 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2942 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2943 if (TREE_CODE (function) == NOP_EXPR
2944 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2945 function = TREE_OPERAND (function, 0);
2947 if (TREE_CODE (function) == FUNCTION_DECL)
2949 name = DECL_NAME (function);
2950 assembler_name = DECL_ASSEMBLER_NAME (function);
2952 GNU_xref_call (current_function_decl,
2953 IDENTIFIER_POINTER (name ? name
2954 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2956 mark_used (function);
2959 /* Convert anything with function type to a pointer-to-function. */
2960 if (pedantic && DECL_MAIN_P (function))
2961 pedwarn ("ANSI C++ forbids calling `main' from within program");
2963 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2964 (because calling an inline function does not mean the function
2965 needs to be separately compiled). */
2967 if (DECL_INLINE (function))
2968 function = inline_conversion (function);
2970 function = build_addr_func (function);
2976 function = build_addr_func (function);
2979 if (function == error_mark_node)
2980 return error_mark_node;
2982 fntype = TREE_TYPE (function);
2984 if (TYPE_PTRMEMFUNC_P (fntype))
2986 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2988 return error_mark_node;
2991 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2992 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2994 if (!((TREE_CODE (fntype) == POINTER_TYPE
2995 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2997 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2999 cp_error ("`%E' cannot be used as a function", function);
3000 return error_mark_node;
3003 /* fntype now gets the type of function pointed to. */
3004 fntype = TREE_TYPE (fntype);
3006 /* Convert the parameters to the types declared in the
3007 function prototype, or apply default promotions. */
3009 if (flags & LOOKUP_COMPLAIN)
3010 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3011 params, fndecl, LOOKUP_NORMAL);
3013 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3016 if (coerced_params == error_mark_node)
3018 if (flags & LOOKUP_SPECULATIVELY)
3021 return error_mark_node;
3024 /* Check for errors in format strings. */
3026 if (warn_format && (name || assembler_name))
3027 check_function_format (name, assembler_name, coerced_params);
3029 /* Recognize certain built-in functions so we can make tree-codes
3030 other than CALL_EXPR. We do this when it enables fold-const.c
3031 to do something useful. */
3033 if (TREE_CODE (function) == ADDR_EXPR
3034 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3035 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3036 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
3041 if (coerced_params == 0)
3042 return integer_zero_node;
3043 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3050 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3052 register tree result
3053 = build_call (function, value_type, coerced_params);
3055 if (require_complete)
3057 if (TREE_CODE (value_type) == VOID_TYPE)
3059 result = require_complete_type (result);
3061 if (IS_AGGR_TYPE (value_type))
3062 result = build_cplus_new (value_type, result);
3063 return convert_from_reference (result);
3068 build_function_call (function, params)
3069 tree function, params;
3071 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3074 /* Convert the actual parameter expressions in the list VALUES
3075 to the types in the list TYPELIST.
3076 If parmdecls is exhausted, or when an element has NULL as its type,
3077 perform the default conversions.
3079 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3081 This is also where warnings about wrong number of args are generated.
3083 Return a list of expressions for the parameters as converted.
3085 Both VALUES and the returned value are chains of TREE_LIST nodes
3086 with the elements of the list in the TREE_VALUE slots of those nodes.
3088 In C++, unspecified trailing parameters can be filled in with their
3089 default arguments, if such were specified. Do so here. */
3092 convert_arguments (typelist, values, fndecl, flags)
3093 tree typelist, values, fndecl;
3096 register tree typetail, valtail;
3097 register tree result = NULL_TREE;
3098 const char *called_thing = 0;
3101 /* Argument passing is always copy-initialization. */
3102 flags |= LOOKUP_ONLYCONVERTING;
3106 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3108 if (DECL_NAME (fndecl) == NULL_TREE
3109 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3110 called_thing = "constructor";
3112 called_thing = "member function";
3115 called_thing = "function";
3118 for (valtail = values, typetail = typelist;
3120 valtail = TREE_CHAIN (valtail), i++)
3122 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3123 register tree val = TREE_VALUE (valtail);
3125 if (val == error_mark_node)
3126 return error_mark_node;
3128 if (type == void_type_node)
3132 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3134 error ("at this point in file");
3137 error ("too many arguments to function");
3138 /* In case anybody wants to know if this argument
3141 TREE_TYPE (tree_last (result)) = error_mark_node;
3145 if (TREE_CODE (val) == OFFSET_REF)
3146 val = resolve_offset_ref (val);
3148 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3149 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3150 if (TREE_CODE (val) == NOP_EXPR
3151 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3152 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3153 val = TREE_OPERAND (val, 0);
3155 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3157 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3158 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3159 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3160 val = default_conversion (val);
3163 if (val == error_mark_node)
3164 return error_mark_node;
3168 /* Formal parm type is specified by a function prototype. */
3171 if (TYPE_SIZE (complete_type (type)) == 0)
3173 error ("parameter type of called function is incomplete");
3178 parmval = convert_for_initialization
3179 (NULL_TREE, type, val, flags,
3180 "argument passing", fndecl, i);
3181 #ifdef PROMOTE_PROTOTYPES
3182 if ((TREE_CODE (type) == INTEGER_TYPE
3183 || TREE_CODE (type) == ENUMERAL_TYPE)
3184 && (TYPE_PRECISION (type)
3185 < TYPE_PRECISION (integer_type_node)))
3186 parmval = default_conversion (parmval);
3190 if (parmval == error_mark_node)
3191 return error_mark_node;
3193 result = expr_tree_cons (NULL_TREE, parmval, result);
3197 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3198 val = convert_from_reference (val);
3200 result = expr_tree_cons (NULL_TREE,
3201 convert_arg_to_ellipsis (val),
3206 typetail = TREE_CHAIN (typetail);
3209 if (typetail != 0 && typetail != void_list_node)
3211 /* See if there are default arguments that can be used */
3212 if (TREE_PURPOSE (typetail))
3214 for (; typetail != void_list_node; ++i)
3217 = convert_default_arg (TREE_VALUE (typetail),
3218 TREE_PURPOSE (typetail),
3221 if (parmval == error_mark_node)
3222 return error_mark_node;
3224 result = expr_tree_cons (0, parmval, result);
3225 typetail = TREE_CHAIN (typetail);
3226 /* ends with `...'. */
3227 if (typetail == NULL_TREE)
3235 cp_error_at ("too few arguments to %s `%+#D'",
3236 called_thing, fndecl);
3237 error ("at this point in file");
3240 error ("too few arguments to function");
3241 return error_mark_list;
3245 return nreverse (result);
3248 /* Build a binary-operation expression, after performing default
3249 conversions on the operands. CODE is the kind of expression to build. */
3252 build_x_binary_op (code, arg1, arg2)
3253 enum tree_code code;
3256 if (processing_template_decl)
3257 return build_min_nt (code, arg1, arg2);
3259 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3263 build_binary_op (code, arg1, arg2)
3264 enum tree_code code;
3267 return build_binary_op_nodefault (code, arg1, arg2, code);
3270 /* Build a binary-operation expression without default conversions.
3271 CODE is the kind of expression to build.
3272 This function differs from `build' in several ways:
3273 the data type of the result is computed and recorded in it,
3274 warnings are generated if arg data types are invalid,
3275 special handling for addition and subtraction of pointers is known,
3276 and some optimization is done (operations on narrow ints
3277 are done in the narrower type when that gives the same result).
3278 Constant folding is also done before the result is returned.
3280 ERROR_CODE is the code that determines what to say in error messages.
3281 It is usually, but not always, the same as CODE.
3283 Note that the operands will never have enumeral types
3284 because either they have just had the default conversions performed
3285 or they have both just been converted to some other type in which
3286 the arithmetic is to be done.
3288 C++: must do special pointer arithmetic when implementing
3289 multiple inheritance, and deal with pointer to member functions. */
3292 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3293 enum tree_code code;
3294 tree orig_op0, orig_op1;
3295 enum tree_code error_code;
3298 register enum tree_code code0, code1;
3301 /* Expression code to give to the expression when it is built.
3302 Normally this is CODE, which is what the caller asked for,
3303 but in some special cases we change it. */
3304 register enum tree_code resultcode = code;
3306 /* Data type in which the computation is to be performed.
3307 In the simplest cases this is the common type of the arguments. */
3308 register tree result_type = NULL;
3310 /* Nonzero means operands have already been type-converted
3311 in whatever way is necessary.
3312 Zero means they need to be converted to RESULT_TYPE. */
3315 /* Nonzero means create the expression with this type, rather than
3317 tree build_type = 0;
3319 /* Nonzero means after finally constructing the expression
3320 convert it to this type. */
3321 tree final_type = 0;
3323 /* Nonzero if this is an operation like MIN or MAX which can
3324 safely be computed in short if both args are promoted shorts.
3325 Also implies COMMON.
3326 -1 indicates a bitwise operation; this makes a difference
3327 in the exact conditions for when it is safe to do the operation
3328 in a narrower mode. */
3331 /* Nonzero if this is a comparison operation;
3332 if both args are promoted shorts, compare the original shorts.
3333 Also implies COMMON. */
3334 int short_compare = 0;
3336 /* Nonzero if this is a right-shift operation, which can be computed on the
3337 original short and then promoted if the operand is a promoted short. */
3338 int short_shift = 0;
3340 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3343 /* Apply default conversions. */
3344 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3345 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3346 || code == TRUTH_XOR_EXPR)
3348 op0 = decay_conversion (orig_op0);
3349 op1 = decay_conversion (orig_op1);
3353 op0 = default_conversion (orig_op0);
3354 op1 = default_conversion (orig_op1);
3357 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3358 STRIP_TYPE_NOPS (op0);
3359 STRIP_TYPE_NOPS (op1);
3361 /* DTRT if one side is an overloaded function, but complain about it. */
3362 if (type_unknown_p (op0))
3364 tree t = instantiate_type (TREE_TYPE (op1), op0, 0);
3365 if (t != error_mark_node)
3367 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3372 if (type_unknown_p (op1))
3374 tree t = instantiate_type (TREE_TYPE (op0), op1, 0);
3375 if (t != error_mark_node)
3377 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3383 type0 = TREE_TYPE (op0);
3384 type1 = TREE_TYPE (op1);
3386 /* The expression codes of the data types of the arguments tell us
3387 whether the arguments are integers, floating, pointers, etc. */
3388 code0 = TREE_CODE (type0);
3389 code1 = TREE_CODE (type1);
3391 /* If an error was already reported for one of the arguments,
3392 avoid reporting another error. */
3394 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3395 return error_mark_node;
3400 /* Handle the pointer + int case. */
3401 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3402 return pointer_int_sum (PLUS_EXPR, op0, op1);
3403 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3404 return pointer_int_sum (PLUS_EXPR, op1, op0);
3410 /* Subtraction of two similar pointers.
3411 We must subtract them as integers, then divide by object size. */
3412 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3413 && comp_target_types (type0, type1, 1))
3414 return pointer_diff (op0, op1, common_type (type0, type1));
3415 /* Handle pointer minus int. Just like pointer plus int. */
3416 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3417 return pointer_int_sum (MINUS_EXPR, op0, op1);
3426 case TRUNC_DIV_EXPR:
3428 case FLOOR_DIV_EXPR:
3429 case ROUND_DIV_EXPR:
3430 case EXACT_DIV_EXPR:
3431 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3432 || code0 == COMPLEX_TYPE)
3433 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3434 || code1 == COMPLEX_TYPE))
3436 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3437 cp_warning ("division by zero in `%E / 0'", op0);
3438 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3439 cp_warning ("division by zero in `%E / 0.'", op0);
3441 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3442 resultcode = RDIV_EXPR;
3444 /* When dividing two signed integers, we have to promote to int.
3445 unless we divide by a constant != -1. Note that default
3446 conversion will have been performed on the operands at this
3447 point, so we have to dig out the original type to find out if
3449 shorten = ((TREE_CODE (op0) == NOP_EXPR
3450 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3451 || (TREE_CODE (op1) == INTEGER_CST
3452 && (TREE_INT_CST_LOW (op1) != -1
3453 || TREE_INT_CST_HIGH (op1) != -1)));
3459 case BIT_ANDTC_EXPR:
3462 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3464 /* If one operand is a constant, and the other is a short type
3465 that has been converted to an int,
3466 really do the work in the short type and then convert the
3467 result to int. If we are lucky, the constant will be 0 or 1
3468 in the short type, making the entire operation go away. */
3469 if (TREE_CODE (op0) == INTEGER_CST
3470 && TREE_CODE (op1) == NOP_EXPR
3471 && (TYPE_PRECISION (type1)
3472 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3473 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3475 final_type = result_type;
3476 op1 = TREE_OPERAND (op1, 0);
3477 result_type = TREE_TYPE (op1);
3479 if (TREE_CODE (op1) == INTEGER_CST
3480 && TREE_CODE (op0) == NOP_EXPR
3481 && (TYPE_PRECISION (type0)
3482 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3483 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3485 final_type = result_type;
3486 op0 = TREE_OPERAND (op0, 0);
3487 result_type = TREE_TYPE (op0);
3491 case TRUNC_MOD_EXPR:
3492 case FLOOR_MOD_EXPR:
3493 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3494 cp_warning ("division by zero in `%E %% 0'", op0);
3495 else if (code1 == REAL_TYPE && real_zerop (op1))
3496 cp_warning ("division by zero in `%E %% 0.'", op0);
3498 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3500 /* Although it would be tempting to shorten always here, that loses
3501 on some targets, since the modulo instruction is undefined if the
3502 quotient can't be represented in the computation mode. We shorten
3503 only if unsigned or if dividing by something we know != -1. */
3504 shorten = ((TREE_CODE (op0) == NOP_EXPR
3505 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3506 || (TREE_CODE (op1) == INTEGER_CST
3507 && (TREE_INT_CST_LOW (op1) != -1
3508 || TREE_INT_CST_HIGH (op1) != -1)));
3513 case TRUTH_ANDIF_EXPR:
3514 case TRUTH_ORIF_EXPR:
3515 case TRUTH_AND_EXPR:
3517 result_type = boolean_type_node;
3520 /* Shift operations: result has same type as first operand;
3521 always convert second operand to int.
3522 Also set SHORT_SHIFT if shifting rightward. */
3525 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3527 result_type = type0;
3528 if (TREE_CODE (op1) == INTEGER_CST)
3530 if (tree_int_cst_lt (op1, integer_zero_node))
3531 warning ("right shift count is negative");
3534 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3536 if (TREE_INT_CST_HIGH (op1) != 0
3537 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3538 >= TYPE_PRECISION (type0)))
3539 warning ("right shift count >= width of type");
3542 /* Convert the shift-count to an integer, regardless of
3543 size of value being shifted. */
3544 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3545 op1 = cp_convert (integer_type_node, op1);
3546 /* Avoid converting op1 to result_type later. */
3552 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3554 result_type = type0;
3555 if (TREE_CODE (op1) == INTEGER_CST)
3557 if (tree_int_cst_lt (op1, integer_zero_node))
3558 warning ("left shift count is negative");
3559 else if (TREE_INT_CST_HIGH (op1) != 0
3560 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3561 >= TYPE_PRECISION (type0)))
3562 warning ("left shift count >= width of type");
3564 /* Convert the shift-count to an integer, regardless of
3565 size of value being shifted. */
3566 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3567 op1 = cp_convert (integer_type_node, op1);
3568 /* Avoid converting op1 to result_type later. */
3575 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3577 result_type = type0;
3578 if (TREE_CODE (op1) == INTEGER_CST)
3580 if (tree_int_cst_lt (op1, integer_zero_node))
3581 warning ("%s rotate count is negative",
3582 (code == LROTATE_EXPR) ? "left" : "right");
3583 else if (TREE_INT_CST_HIGH (op1) != 0
3584 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3585 >= TYPE_PRECISION (type0)))
3586 warning ("%s rotate count >= width of type",
3587 (code == LROTATE_EXPR) ? "left" : "right");
3589 /* Convert the shift-count to an integer, regardless of
3590 size of value being shifted. */
3591 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3592 op1 = cp_convert (integer_type_node, op1);
3598 build_type = boolean_type_node;
3599 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3600 || code0 == COMPLEX_TYPE)
3601 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3602 || code1 == COMPLEX_TYPE))
3604 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3606 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3607 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3609 if (comp_target_types (type0, type1, 1))
3610 result_type = common_type (type0, type1);
3611 else if (tt0 == void_type_node)
3613 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3614 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3615 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3616 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3617 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3619 else if (tt1 == void_type_node)
3621 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3622 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3623 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3626 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3629 if (result_type == NULL_TREE)
3630 result_type = ptr_type_node;
3632 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3633 && integer_zerop (op1))
3634 result_type = type0;
3635 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3636 && integer_zerop (op0))
3637 result_type = type1;
3638 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3640 result_type = type0;
3641 error ("ANSI C++ forbids comparison between pointer and integer");
3643 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3645 result_type = type1;
3646 error ("ANSI C++ forbids comparison between pointer and integer");
3648 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3649 && integer_zerop (op1))
3651 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3652 op1 = integer_zero_node;
3653 result_type = TREE_TYPE (op0);
3655 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3656 && integer_zerop (op0))
3658 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3659 op1 = integer_zero_node;
3660 result_type = TREE_TYPE (op0);
3662 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3663 && same_type_p (type0, type1))
3665 /* The code we generate for the test is:
3667 (op0.index == op1.index
3668 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3669 || op0.pfn == op1.pfn)) */
3671 tree index0 = build_component_ref (op0, index_identifier,
3673 tree index1 = save_expr (build_component_ref (op1, index_identifier,
3675 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3676 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3677 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3678 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3680 tree integer_neg_one_node
3681 = build_binary_op (MINUS_EXPR, integer_zero_node,
3683 e1 = build_binary_op (EQ_EXPR, index0, index1);
3684 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3685 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3686 build_binary_op (EQ_EXPR, delta20, delta21));
3687 /* We can't use build_binary_op for this cmp because it would get
3688 confused by the ptr to method types and think we want pmfs. */
3689 e3 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3690 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3691 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3692 if (code == EQ_EXPR)
3694 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3696 else if (TYPE_PTRMEMFUNC_P (type0)
3697 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3699 tree index0 = build_component_ref (op0, index_identifier,
3702 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3703 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3704 tree delta21 = integer_zero_node;
3706 tree integer_neg_one_node
3707 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node);
3708 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3709 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3711 /* Map everything down one to make room for
3712 the null pointer to member. */
3713 index1 = size_binop (PLUS_EXPR,
3714 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3716 op1 = integer_zero_node;
3717 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3718 (TREE_TYPE (type1)));
3719 delta21 = DECL_FIELD_BITPOS (delta21);
3720 delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3721 size_int (BITS_PER_UNIT));
3722 delta21 = convert (sizetype, delta21);
3725 index1 = integer_neg_one_node;
3727 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3729 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3732 e1 = build_binary_op (EQ_EXPR, index0, index1);
3733 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3734 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3735 build_binary_op (EQ_EXPR, delta20, delta21));
3736 /* We can't use build_binary_op for this cmp because it would get
3737 confused by the ptr to method types and think we want pmfs. */
3738 e3 = build (EQ_EXPR, boolean_type_node, pfn0, op1);
3739 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3740 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3741 if (code == EQ_EXPR)
3743 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3745 else if (TYPE_PTRMEMFUNC_P (type1)
3746 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0))
3747 return build_binary_op (code, op1, op0);
3752 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3753 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3755 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3757 if (comp_target_types (type0, type1, 1))
3758 result_type = common_type (type0, type1);
3761 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3763 result_type = ptr_type_node;
3772 build_type = boolean_type_node;
3773 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3774 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3776 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3778 if (comp_target_types (type0, type1, 1))
3779 result_type = common_type (type0, type1);
3782 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3784 result_type = ptr_type_node;
3787 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3788 && integer_zerop (op1))
3789 result_type = type0;
3790 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3791 && integer_zerop (op0))
3792 result_type = type1;
3793 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3795 result_type = type0;
3796 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3798 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3800 result_type = type1;
3801 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3809 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3811 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3813 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3815 if (shorten || common || short_compare)
3816 result_type = common_type (type0, type1);
3818 /* For certain operations (which identify themselves by shorten != 0)
3819 if both args were extended from the same smaller type,
3820 do the arithmetic in that type and then extend.
3822 shorten !=0 and !=1 indicates a bitwise operation.
3823 For them, this optimization is safe only if
3824 both args are zero-extended or both are sign-extended.
3825 Otherwise, we might change the result.
3826 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3827 but calculated in (unsigned short) it would be (unsigned short)-1. */
3829 if (shorten && none_complex)
3831 int unsigned0, unsigned1;
3832 tree arg0 = get_narrower (op0, &unsigned0);
3833 tree arg1 = get_narrower (op1, &unsigned1);
3834 /* UNS is 1 if the operation to be done is an unsigned one. */
3835 int uns = TREE_UNSIGNED (result_type);
3838 final_type = result_type;
3840 /* Handle the case that OP0 does not *contain* a conversion
3841 but it *requires* conversion to FINAL_TYPE. */
3843 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3844 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3845 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3846 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3848 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3850 /* For bitwise operations, signedness of nominal type
3851 does not matter. Consider only how operands were extended. */
3855 /* Note that in all three cases below we refrain from optimizing
3856 an unsigned operation on sign-extended args.
3857 That would not be valid. */
3859 /* Both args variable: if both extended in same way
3860 from same width, do it in that width.
3861 Do it unsigned if args were zero-extended. */
3862 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3863 < TYPE_PRECISION (result_type))
3864 && (TYPE_PRECISION (TREE_TYPE (arg1))
3865 == TYPE_PRECISION (TREE_TYPE (arg0)))
3866 && unsigned0 == unsigned1
3867 && (unsigned0 || !uns))
3869 = signed_or_unsigned_type (unsigned0,
3870 common_type (TREE_TYPE (arg0),
3872 else if (TREE_CODE (arg0) == INTEGER_CST
3873 && (unsigned1 || !uns)
3874 && (TYPE_PRECISION (TREE_TYPE (arg1))
3875 < TYPE_PRECISION (result_type))
3876 && (type = signed_or_unsigned_type (unsigned1,
3878 int_fits_type_p (arg0, type)))
3880 else if (TREE_CODE (arg1) == INTEGER_CST
3881 && (unsigned0 || !uns)
3882 && (TYPE_PRECISION (TREE_TYPE (arg0))
3883 < TYPE_PRECISION (result_type))
3884 && (type = signed_or_unsigned_type (unsigned0,
3886 int_fits_type_p (arg1, type)))
3890 /* Shifts can be shortened if shifting right. */
3895 tree arg0 = get_narrower (op0, &unsigned_arg);
3897 final_type = result_type;
3899 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3900 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3902 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3903 /* We can shorten only if the shift count is less than the
3904 number of bits in the smaller type size. */
3905 && TREE_INT_CST_HIGH (op1) == 0
3906 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3907 /* If arg is sign-extended and then unsigned-shifted,
3908 we can simulate this with a signed shift in arg's type
3909 only if the extended result is at least twice as wide
3910 as the arg. Otherwise, the shift could use up all the
3911 ones made by sign-extension and bring in zeros.
3912 We can't optimize that case at all, but in most machines
3913 it never happens because available widths are 2**N. */
3914 && (!TREE_UNSIGNED (final_type)
3916 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3917 <= TYPE_PRECISION (result_type))))
3919 /* Do an unsigned shift if the operand was zero-extended. */
3921 = signed_or_unsigned_type (unsigned_arg,
3923 /* Convert value-to-be-shifted to that type. */
3924 if (TREE_TYPE (op0) != result_type)
3925 op0 = cp_convert (result_type, op0);
3930 /* Comparison operations are shortened too but differently.
3931 They identify themselves by setting short_compare = 1. */
3935 /* Don't write &op0, etc., because that would prevent op0
3936 from being kept in a register.
3937 Instead, make copies of the our local variables and
3938 pass the copies by reference, then copy them back afterward. */
3939 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3940 enum tree_code xresultcode = resultcode;
3942 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3944 return cp_convert (boolean_type_node, val);
3945 op0 = xop0, op1 = xop1;
3947 resultcode = xresultcode;
3950 if (short_compare && warn_sign_compare)
3952 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3953 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3955 int unsignedp0, unsignedp1;
3956 tree primop0 = get_narrower (op0, &unsignedp0);
3957 tree primop1 = get_narrower (op1, &unsignedp1);
3959 /* Check for comparison of different enum types. */
3960 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3961 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3962 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3963 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3965 cp_warning ("comparison between `%#T' and `%#T'",
3966 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3969 /* Give warnings for comparisons between signed and unsigned
3970 quantities that may fail. */
3971 /* Do the checking based on the original operand trees, so that
3972 casts will be considered, but default promotions won't be. */
3974 /* Do not warn if the comparison is being done in a signed type,
3975 since the signed type will only be chosen if it can represent
3976 all the values of the unsigned type. */
3977 if (! TREE_UNSIGNED (result_type))
3979 /* Do not warn if both operands are unsigned. */
3980 else if (op0_signed == op1_signed)
3982 /* Do not warn if the signed quantity is an unsuffixed
3983 integer literal (or some static constant expression
3984 involving such literals) and it is non-negative. */
3985 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3986 && tree_int_cst_sgn (orig_op0) >= 0)
3987 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3988 && tree_int_cst_sgn (orig_op1) >= 0))
3990 /* Do not warn if the comparison is an equality operation,
3991 the unsigned quantity is an integral constant and it does
3992 not use the most significant bit of result_type. */
3993 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3994 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3995 && int_fits_type_p (orig_op1,
3996 signed_type (result_type)))
3997 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3998 && int_fits_type_p (orig_op0,
3999 signed_type (result_type)))))
4002 warning ("comparison between signed and unsigned");
4004 /* Warn if two unsigned values are being compared in a size
4005 larger than their original size, and one (and only one) is the
4006 result of a `~' operator. This comparison will always fail.
4008 Also warn if one operand is a constant, and the constant does not
4009 have all bits set that are set in the ~ operand when it is
4012 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
4013 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
4015 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
4016 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
4017 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
4018 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
4020 if (TREE_CODE (primop0) == INTEGER_CST
4021 || TREE_CODE (primop1) == INTEGER_CST)
4024 HOST_WIDE_INT constant, mask;
4028 if (TREE_CODE (primop0) == INTEGER_CST)
4031 unsignedp = unsignedp1;
4032 constant = TREE_INT_CST_LOW (primop0);
4037 unsignedp = unsignedp0;
4038 constant = TREE_INT_CST_LOW (primop1);
4041 bits = TYPE_PRECISION (TREE_TYPE (primop));
4042 if (bits < TYPE_PRECISION (result_type)
4043 && bits < HOST_BITS_PER_LONG && unsignedp)
4045 mask = (~ (HOST_WIDE_INT) 0) << bits;
4046 if ((mask & constant) != mask)
4047 warning ("comparison of promoted ~unsigned with constant");
4050 else if (unsignedp0 && unsignedp1
4051 && (TYPE_PRECISION (TREE_TYPE (primop0))
4052 < TYPE_PRECISION (result_type))
4053 && (TYPE_PRECISION (TREE_TYPE (primop1))
4054 < TYPE_PRECISION (result_type)))
4055 warning ("comparison of promoted ~unsigned with unsigned");
4060 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4061 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4062 Then the expression will be built.
4063 It will be given type FINAL_TYPE if that is nonzero;
4064 otherwise, it will be given type RESULT_TYPE. */
4068 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
4069 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
4070 return error_mark_node;
4073 /* Issue warnings about peculiar, but legal, uses of NULL. */
4074 if (/* It's reasonable to use pointer values as operands of &&
4075 and ||, so NULL is no exception. */
4076 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4077 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4078 (orig_op0 == null_node
4079 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4080 /* Or vice versa. */
4081 || (orig_op1 == null_node
4082 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4083 /* Or, both are NULL and the operation was not a comparison. */
4084 || (orig_op0 == null_node && orig_op1 == null_node
4085 && code != EQ_EXPR && code != NE_EXPR)))
4086 /* Some sort of arithmetic operation involving NULL was
4087 performed. Note that pointer-difference and pointer-addition
4088 have already been handled above, and so we don't end up here in
4090 cp_warning ("NULL used in arithmetic");
4094 if (TREE_TYPE (op0) != result_type)
4095 op0 = cp_convert (result_type, op0);
4096 if (TREE_TYPE (op1) != result_type)
4097 op1 = cp_convert (result_type, op1);
4099 if (op0 == error_mark_node || op1 == error_mark_node)
4100 return error_mark_node;
4103 if (build_type == NULL_TREE)
4104 build_type = result_type;
4107 register tree result = build (resultcode, build_type, op0, op1);
4108 register tree folded;
4110 folded = fold (result);
4111 if (folded == result)
4112 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4113 if (final_type != 0)
4114 return cp_convert (final_type, folded);
4119 /* Return a tree for the sum or difference (RESULTCODE says which)
4120 of pointer PTROP and integer INTOP. */
4123 pointer_int_sum (resultcode, ptrop, intop)
4124 enum tree_code resultcode;
4125 register tree ptrop, intop;
4129 register tree result;
4130 register tree folded = fold (intop);
4132 /* The result is a pointer of the same type that is being added. */
4134 register tree result_type = TREE_TYPE (ptrop);
4136 if (!complete_type_or_else (result_type, ptrop))
4137 return error_mark_node;
4139 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4141 if (pedantic || warn_pointer_arith)
4142 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
4143 size_exp = integer_one_node;
4145 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4147 if (pedantic || warn_pointer_arith)
4148 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
4149 size_exp = integer_one_node;
4151 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4153 if (pedantic || warn_pointer_arith)
4154 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4155 size_exp = integer_one_node;
4157 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4159 if (pedantic || warn_pointer_arith)
4160 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4161 size_exp = integer_one_node;
4164 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4166 /* Needed to make OOPS V2R3 work. */
4168 if (TREE_CODE (intop) == INTEGER_CST
4169 && TREE_INT_CST_LOW (intop) == 0
4170 && TREE_INT_CST_HIGH (intop) == 0)
4173 /* If what we are about to multiply by the size of the elements
4174 contains a constant term, apply distributive law
4175 and multiply that constant term separately.
4176 This helps produce common subexpressions. */
4178 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4179 && ! TREE_CONSTANT (intop)
4180 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4181 && TREE_CONSTANT (size_exp))
4183 enum tree_code subcode = resultcode;
4184 if (TREE_CODE (intop) == MINUS_EXPR)
4185 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4186 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4187 intop = TREE_OPERAND (intop, 0);
4190 /* Convert the integer argument to a type the same size as sizetype
4191 so the multiply won't overflow spuriously. */
4193 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4194 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4196 /* Replace the integer argument with a suitable product by the object size.
4197 Do this multiplication as signed, then convert to the appropriate
4198 pointer type (actually unsigned integral). */
4200 intop = cp_convert (result_type,
4201 build_binary_op (MULT_EXPR, intop,
4202 cp_convert (TREE_TYPE (intop),
4205 /* Create the sum or difference. */
4207 result = build (resultcode, result_type, ptrop, intop);
4209 folded = fold (result);
4210 if (folded == result)
4211 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4215 /* Return a tree for the difference of pointers OP0 and OP1.
4216 The resulting tree has type int. */
4219 pointer_diff (op0, op1, ptrtype)
4220 register tree op0, op1;
4221 register tree ptrtype;
4223 register tree result, folded;
4224 tree restype = ptrdiff_type_node;
4225 tree target_type = TREE_TYPE (ptrtype);
4227 if (!complete_type_or_else (target_type, NULL_TREE))
4228 return error_mark_node;
4230 if (pedantic || warn_pointer_arith)
4232 if (TREE_CODE (target_type) == VOID_TYPE)
4233 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4234 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4235 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4236 if (TREE_CODE (target_type) == METHOD_TYPE)
4237 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4238 if (TREE_CODE (target_type) == OFFSET_TYPE)
4239 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4242 /* First do the subtraction as integers;
4243 then drop through to build the divide operator. */
4245 op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4246 cp_convert (restype, op1));
4248 /* This generates an error if op1 is a pointer to an incomplete type. */
4249 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4250 error ("arithmetic on pointer to an incomplete type");
4252 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4253 || TREE_CODE (target_type) == FUNCTION_TYPE
4254 || TREE_CODE (target_type) == METHOD_TYPE
4255 || TREE_CODE (target_type) == OFFSET_TYPE)
4257 : size_in_bytes (target_type));
4259 /* Do the division. */
4261 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4263 folded = fold (result);
4264 if (folded == result)
4265 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4269 /* Handle the case of taking the address of a COMPONENT_REF.
4270 Called by `build_unary_op'.
4272 ARG is the COMPONENT_REF whose address we want.
4273 ARGTYPE is the pointer type that this address should have. */
4276 build_component_addr (arg, argtype)
4279 tree field = TREE_OPERAND (arg, 1);
4280 tree basetype = decl_type_context (field);
4281 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4283 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4285 if (DECL_C_BIT_FIELD (field))
4287 cp_error ("attempt to take address of bit-field structure member `%D'",
4289 return error_mark_node;
4292 if (TREE_CODE (field) == FIELD_DECL
4293 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4295 /* Can't convert directly to ARGTYPE, since that
4296 may have the same pointer type as one of our
4298 rval = build1 (NOP_EXPR, argtype,
4299 convert_pointer_to (basetype, rval));
4300 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4303 /* This conversion is harmless. */
4304 rval = convert_force (argtype, rval, 0);
4306 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4308 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4309 size_int (BITS_PER_UNIT));
4310 int flag = TREE_CONSTANT (rval);
4311 offset = convert (sizetype, offset);
4312 rval = fold (build (PLUS_EXPR, argtype,
4313 rval, cp_convert (argtype, offset)));
4314 TREE_CONSTANT (rval) = flag;
4319 /* Construct and perhaps optimize a tree representation
4320 for a unary operation. CODE, a tree_code, specifies the operation
4321 and XARG is the operand. */
4324 build_x_unary_op (code, xarg)
4325 enum tree_code code;
4328 if (processing_template_decl)
4329 return build_min_nt (code, xarg, NULL_TREE);
4331 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4333 if (code == ADDR_EXPR
4334 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4335 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4336 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4337 || (TREE_CODE (xarg) == OFFSET_REF)))
4338 /* don't look for a function */;
4343 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4344 NULL_TREE, NULL_TREE);
4345 if (rval || code != ADDR_EXPR)
4349 if (code == ADDR_EXPR)
4351 if (TREE_CODE (xarg) == TARGET_EXPR)
4352 warning ("taking address of temporary");
4355 return build_unary_op (code, xarg, 0);
4358 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4361 condition_conversion (expr)
4365 if (processing_template_decl)
4367 t = cp_convert (boolean_type_node, expr);
4368 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4372 /* C++: Must handle pointers to members.
4374 Perhaps type instantiation should be extended to handle conversion
4375 from aggregates to types we don't yet know we want? (Or are those
4376 cases typically errors which should be reported?)
4378 NOCONVERT nonzero suppresses the default promotions
4379 (such as from short to int). */
4382 build_unary_op (code, xarg, noconvert)
4383 enum tree_code code;
4387 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4388 register tree arg = xarg;
4389 register tree argtype = 0;
4390 const char *errstring = NULL;
4393 if (arg == error_mark_node)
4394 return error_mark_node;
4399 /* This is used for unary plus, because a CONVERT_EXPR
4400 is enough to prevent anybody from looking inside for
4401 associativity, but won't generate any code. */
4402 if (!(arg = build_expr_type_conversion
4403 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4404 errstring = "wrong type argument to unary plus";
4408 arg = default_conversion (arg);
4409 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4410 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4415 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4416 errstring = "wrong type argument to unary minus";
4417 else if (!noconvert)
4418 arg = default_conversion (arg);
4422 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4426 arg = default_conversion (arg);
4428 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4430 errstring = "wrong type argument to bit-complement";
4431 else if (!noconvert)
4432 arg = default_conversion (arg);
4436 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4437 errstring = "wrong type argument to abs";
4438 else if (!noconvert)
4439 arg = default_conversion (arg);
4443 /* Conjugating a real value is a no-op, but allow it anyway. */
4444 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4445 errstring = "wrong type argument to conjugation";
4446 else if (!noconvert)
4447 arg = default_conversion (arg);
4450 case TRUTH_NOT_EXPR:
4451 arg = cp_convert (boolean_type_node, arg);
4452 val = invert_truthvalue (arg);
4453 if (arg != error_mark_node)
4455 errstring = "in argument to unary !";
4462 if (TREE_CODE (arg) == COMPLEX_CST)
4463 return TREE_REALPART (arg);
4464 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4465 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4470 if (TREE_CODE (arg) == COMPLEX_CST)
4471 return TREE_IMAGPART (arg);
4472 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4473 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4475 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4477 case PREINCREMENT_EXPR:
4478 case POSTINCREMENT_EXPR:
4479 case PREDECREMENT_EXPR:
4480 case POSTDECREMENT_EXPR:
4481 /* Handle complex lvalues (when permitted)
4482 by reduction to simpler cases. */
4484 val = unary_complex_lvalue (code, arg);
4488 /* Increment or decrement the real part of the value,
4489 and don't change the imaginary part. */
4490 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4494 arg = stabilize_reference (arg);
4495 real = build_unary_op (REALPART_EXPR, arg, 1);
4496 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4497 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4498 build_unary_op (code, real, 1), imag);
4501 /* Report invalid types. */
4503 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4506 if (code == PREINCREMENT_EXPR)
4507 errstring ="no pre-increment operator for type";
4508 else if (code == POSTINCREMENT_EXPR)
4509 errstring ="no post-increment operator for type";
4510 else if (code == PREDECREMENT_EXPR)
4511 errstring ="no pre-decrement operator for type";
4513 errstring ="no post-decrement operator for type";
4517 /* Report something read-only. */
4519 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4520 || TREE_READONLY (arg))
4521 readonly_error (arg, ((code == PREINCREMENT_EXPR
4522 || code == POSTINCREMENT_EXPR)
4523 ? "increment" : "decrement"),
4528 tree result_type = TREE_TYPE (arg);
4530 arg = get_unwidened (arg, 0);
4531 argtype = TREE_TYPE (arg);
4533 /* ARM $5.2.5 last annotation says this should be forbidden. */
4534 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4535 pedwarn ("ANSI C++ forbids %sing an enum",
4536 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4537 ? "increment" : "decrement");
4539 /* Compute the increment. */
4541 if (TREE_CODE (argtype) == POINTER_TYPE)
4543 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4544 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4545 cp_error ("cannot %s a pointer to incomplete type `%T'",
4546 ((code == PREINCREMENT_EXPR
4547 || code == POSTINCREMENT_EXPR)
4548 ? "increment" : "decrement"), TREE_TYPE (argtype));
4549 else if ((pedantic || warn_pointer_arith)
4550 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4551 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4552 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4553 ((code == PREINCREMENT_EXPR
4554 || code == POSTINCREMENT_EXPR)
4555 ? "increment" : "decrement"), argtype);
4556 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4559 inc = integer_one_node;
4561 inc = cp_convert (argtype, inc);
4563 /* Handle incrementing a cast-expression. */
4565 switch (TREE_CODE (arg))
4570 case FIX_TRUNC_EXPR:
4571 case FIX_FLOOR_EXPR:
4572 case FIX_ROUND_EXPR:
4575 tree incremented, modify, value, compound;
4576 if (! lvalue_p (arg) && pedantic)
4577 pedwarn ("cast to non-reference type used as lvalue");
4578 arg = stabilize_reference (arg);
4579 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4582 value = save_expr (arg);
4583 incremented = build (((code == PREINCREMENT_EXPR
4584 || code == POSTINCREMENT_EXPR)
4585 ? PLUS_EXPR : MINUS_EXPR),
4586 argtype, value, inc);
4587 TREE_SIDE_EFFECTS (incremented) = 1;
4589 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4590 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4592 /* Eliminate warning about unused result of + or -. */
4593 TREE_NO_UNUSED_WARNING (compound) = 1;
4601 /* Complain about anything else that is not a true lvalue. */
4602 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4603 || code == POSTINCREMENT_EXPR)
4604 ? "increment" : "decrement")))
4605 return error_mark_node;
4607 /* Forbid using -- on `bool'. */
4608 if (TREE_TYPE (arg) == boolean_type_node)
4610 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4612 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4613 return error_mark_node;
4616 /* This will only work if someone can convince Kenner to accept
4617 my patch to expand_increment. (jason) */
4618 val = build (code, TREE_TYPE (arg), arg, inc);
4620 if (code == POSTINCREMENT_EXPR)
4622 arg = stabilize_reference (arg);
4623 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4625 TREE_SIDE_EFFECTS (val) = 1;
4626 arg = save_expr (arg);
4627 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4628 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4631 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4636 val = build (code, TREE_TYPE (arg), arg, inc);
4638 TREE_SIDE_EFFECTS (val) = 1;
4639 return cp_convert (result_type, val);
4643 /* Note that this operation never does default_conversion
4644 regardless of NOCONVERT. */
4646 argtype = lvalue_type (arg);
4647 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4651 build_pointer_type (TREE_TYPE (argtype)), arg);
4652 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4655 else if (pedantic && DECL_MAIN_P (arg))
4657 pedwarn ("taking address of function `main'");
4659 /* Let &* cancel out to simplify resulting code. */
4660 if (TREE_CODE (arg) == INDIRECT_REF)
4662 /* We don't need to have `current_class_ptr' wrapped in a
4663 NON_LVALUE_EXPR node. */
4664 if (arg == current_class_ref)
4665 return current_class_ptr;
4667 arg = TREE_OPERAND (arg, 0);
4668 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4672 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4673 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4675 else if (lvalue_p (arg))
4676 /* Don't let this be an lvalue. */
4677 return non_lvalue (arg);
4681 /* For &x[y], return x+y */
4682 if (TREE_CODE (arg) == ARRAY_REF)
4684 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4685 return error_mark_node;
4686 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4687 TREE_OPERAND (arg, 1));
4690 /* Uninstantiated types are all functions. Taking the
4691 address of a function is a no-op, so just return the
4694 if (TREE_CODE (arg) == IDENTIFIER_NODE
4695 && IDENTIFIER_OPNAME_P (arg))
4697 my_friendly_abort (117);
4698 /* We don't know the type yet, so just work around the problem.
4699 We know that this will resolve to an lvalue. */
4700 return build1 (ADDR_EXPR, unknown_type_node, arg);
4703 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4704 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4706 /* They're trying to take the address of a unique non-static
4707 member function. This is ill-formed, but let's try to DTRT. */
4710 if (current_class_type
4711 && TREE_OPERAND (arg, 0) == current_class_ref)
4712 /* An expression like &memfn. */
4713 pedwarn ("taking the address of a non-static member function");
4715 pedwarn ("taking the address of a bound member function");
4717 base = TREE_TYPE (TREE_OPERAND (arg, 0));
4718 name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4720 cp_pedwarn (" to form a pointer to member function, say `&%T::%D'",
4722 arg = build_offset_ref (base, name);
4725 if (type_unknown_p (arg))
4726 return build1 (ADDR_EXPR, unknown_type_node, arg);
4728 /* Handle complex lvalues (when permitted)
4729 by reduction to simpler cases. */
4730 val = unary_complex_lvalue (code, arg);
4734 switch (TREE_CODE (arg))
4739 case FIX_TRUNC_EXPR:
4740 case FIX_FLOOR_EXPR:
4741 case FIX_ROUND_EXPR:
4743 if (! lvalue_p (arg) && pedantic)
4744 pedwarn ("taking the address of a cast to non-reference type");
4751 /* Allow the address of a constructor if all the elements
4753 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4754 && TREE_CONSTANT (arg))
4756 /* Anything not already handled and not a true memory reference
4758 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4759 && TREE_CODE (argtype) != METHOD_TYPE
4760 && !lvalue_or_else (arg, "unary `&'"))
4761 return error_mark_node;
4763 if (argtype != error_mark_node)
4764 argtype = build_pointer_type (argtype);
4766 if (mark_addressable (arg) == 0)
4767 return error_mark_node;
4772 if (TREE_CODE (arg) == COMPONENT_REF)
4773 addr = build_component_addr (arg, argtype);
4775 addr = build1 (ADDR_EXPR, argtype, arg);
4777 /* Address of a static or external variable or
4778 function counts as a constant */
4780 TREE_CONSTANT (addr) = 1;
4782 if (TREE_CODE (argtype) == POINTER_TYPE
4783 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4785 build_ptrmemfunc_type (argtype);
4786 addr = build_ptrmemfunc (argtype, addr, 0);
4799 argtype = TREE_TYPE (arg);
4800 return fold (build1 (code, argtype, arg));
4804 return error_mark_node;
4808 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4809 convert ARG with the same conversions in the same order
4810 and return the result. */
4813 convert_sequence (conversions, arg)
4817 switch (TREE_CODE (conversions))
4822 case FIX_TRUNC_EXPR:
4823 case FIX_FLOOR_EXPR:
4824 case FIX_ROUND_EXPR:
4826 return cp_convert (TREE_TYPE (conversions),
4827 convert_sequence (TREE_OPERAND (conversions, 0),
4836 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4837 for certain kinds of expressions which are not really lvalues
4838 but which we can accept as lvalues.
4840 If ARG is not a kind of expression we can handle, return zero. */
4843 unary_complex_lvalue (code, arg)
4844 enum tree_code code;
4847 /* Handle (a, b) used as an "lvalue". */
4848 if (TREE_CODE (arg) == COMPOUND_EXPR)
4850 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4851 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4852 TREE_OPERAND (arg, 0), real_result);
4855 /* Handle (a ? b : c) used as an "lvalue". */
4856 if (TREE_CODE (arg) == COND_EXPR
4857 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4858 return rationalize_conditional_expr (code, arg);
4860 if (TREE_CODE (arg) == MODIFY_EXPR
4861 || TREE_CODE (arg) == PREINCREMENT_EXPR
4862 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4863 return unary_complex_lvalue
4864 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4865 arg, TREE_OPERAND (arg, 0)));
4867 if (code != ADDR_EXPR)
4870 /* Handle (a = b) used as an "lvalue" for `&'. */
4871 if (TREE_CODE (arg) == MODIFY_EXPR
4872 || TREE_CODE (arg) == INIT_EXPR)
4874 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4875 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4876 TREE_NO_UNUSED_WARNING (arg) = 1;
4880 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4881 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4882 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4884 /* The representation of something of type OFFSET_TYPE
4885 is really the representation of a pointer to it.
4886 Here give the representation its true type. */
4889 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4891 if (TREE_CODE (arg) != OFFSET_REF)
4894 t = TREE_OPERAND (arg, 1);
4896 /* Check all this code for right semantics. */
4897 if (TREE_CODE (t) == FUNCTION_DECL)
4899 if (DECL_DESTRUCTOR_P (t))
4900 cp_error ("taking address of destructor");
4901 return build_unary_op (ADDR_EXPR, t, 0);
4903 if (TREE_CODE (t) == VAR_DECL)
4904 return build_unary_op (ADDR_EXPR, t, 0);
4909 if (TREE_OPERAND (arg, 0)
4910 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4911 && TREE_CODE (t) != FIELD_DECL)
4913 cp_error ("taking address of bound pointer-to-member expression");
4914 return error_mark_node;
4917 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4918 type = build_pointer_type (type);
4920 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4926 /* We permit compiler to make function calls returning
4927 objects of aggregate type look like lvalues. */
4931 if (TREE_CODE (targ) == SAVE_EXPR)
4932 targ = TREE_OPERAND (targ, 0);
4934 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4936 if (TREE_CODE (arg) == SAVE_EXPR)
4939 targ = build_cplus_new (TREE_TYPE (arg), arg);
4940 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4943 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4944 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4945 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4948 /* Don't let anything else be handled specially. */
4952 /* Mark EXP saying that we need to be able to take the
4953 address of it; it should not be allocated in a register.
4954 Value is 1 if successful.
4956 C++: we do not allow `current_class_ptr' to be addressable. */
4959 mark_addressable (exp)
4962 register tree x = exp;
4964 if (TREE_ADDRESSABLE (x) == 1)
4968 switch (TREE_CODE (x))
4975 x = TREE_OPERAND (x, 0);
4979 if (x == current_class_ptr)
4981 if (! flag_this_is_variable)
4982 error ("address of `this' not available");
4983 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4984 put_var_into_stack (x);
4988 if (TREE_STATIC (x) && TREE_READONLY (x)
4989 && DECL_RTL (x) != 0
4990 && ! DECL_IN_MEMORY_P (x))
4992 /* We thought this would make a good constant variable,
4993 but we were wrong. */
4994 push_obstacks_nochange ();
4995 end_temporary_allocation ();
4997 TREE_ASM_WRITTEN (x) = 0;
4999 rest_of_decl_compilation (x, 0,
5000 !DECL_FUNCTION_SCOPE_P (x),
5002 TREE_ADDRESSABLE (x) = 1;
5008 /* Caller should not be trying to mark initialized
5009 constant fields addressable. */
5010 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
5011 || DECL_IN_AGGR_P (x) == 0
5013 || DECL_EXTERNAL (x), 314);
5017 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5018 && !DECL_ARTIFICIAL (x) && extra_warnings)
5019 cp_warning ("address requested for `%D', which is declared `register'",
5021 put_var_into_stack (x);
5022 TREE_ADDRESSABLE (x) = 1;
5026 if (DECL_LANG_SPECIFIC (x) != 0)
5028 x = DECL_MAIN_VARIANT (x);
5029 /* We have to test both conditions here. The first may be
5030 non-zero in the case of processing a default function. The
5031 second may be non-zero in the case of a template function. */
5032 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
5035 TREE_ADDRESSABLE (x) = 1;
5037 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
5041 TREE_ADDRESSABLE (x) = 1;
5045 TREE_ADDRESSABLE (x) = 1;
5046 mark_addressable (TREE_OPERAND (x, 0));
5054 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5057 build_x_conditional_expr (ifexp, op1, op2)
5058 tree ifexp, op1, op2;
5060 if (processing_template_decl)
5061 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5063 return build_new_op (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
5067 build_conditional_expr (ifexp, op1, op2)
5068 tree ifexp, op1, op2;
5070 register tree type1;
5071 register tree type2;
5072 register enum tree_code code1;
5073 register enum tree_code code2;
5074 register tree result_type = NULL_TREE;
5076 /* If second operand is omitted, it is the same as the first one;
5077 make sure it is calculated only once. */
5081 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
5082 ifexp = op1 = save_expr (ifexp);
5085 type1 = TREE_TYPE (op1);
5086 code1 = TREE_CODE (type1);
5087 type2 = TREE_TYPE (op2);
5088 code2 = TREE_CODE (type2);
5089 if (op1 == error_mark_node || op2 == error_mark_node
5090 || type1 == error_mark_node || type2 == error_mark_node)
5091 return error_mark_node;
5093 ifexp = cp_convert (boolean_type_node, ifexp);
5095 if (TREE_CODE (ifexp) == ERROR_MARK)
5096 return error_mark_node;
5098 /* C++: REFERENCE_TYPES must be dereferenced. */
5099 if (code1 == REFERENCE_TYPE)
5101 op1 = convert_from_reference (op1);
5102 type1 = TREE_TYPE (op1);
5103 code1 = TREE_CODE (type1);
5105 if (code2 == REFERENCE_TYPE)
5107 op2 = convert_from_reference (op2);
5108 type2 = TREE_TYPE (op2);
5109 code2 = TREE_CODE (type2);
5112 /* Don't promote the operands separately if they promote
5113 the same way. Return the unpromoted type and let the combined
5114 value get promoted if necessary. */
5116 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
5117 && code2 != ARRAY_TYPE
5118 && code2 != FUNCTION_TYPE
5119 && code2 != METHOD_TYPE)
5123 if (TREE_CONSTANT (ifexp)
5124 && (TREE_CODE (ifexp) == INTEGER_CST
5125 || TREE_CODE (ifexp) == ADDR_EXPR))
5126 return (integer_zerop (ifexp) ? op2 : op1);
5128 if (TREE_CODE (op1) == CONST_DECL)
5129 op1 = DECL_INITIAL (op1);
5130 else if (TREE_READONLY_DECL_P (op1))
5131 op1 = decl_constant_value (op1);
5132 if (TREE_CODE (op2) == CONST_DECL)
5133 op2 = DECL_INITIAL (op2);
5134 else if (TREE_READONLY_DECL_P (op2))
5135 op2 = decl_constant_value (op2);
5137 type1 = cp_build_qualified_type
5138 (type1, (CP_TYPE_QUALS (TREE_TYPE (op1))
5139 | CP_TYPE_QUALS (TREE_TYPE (op2))));
5140 /* ??? This is a kludge to deal with the fact that
5141 we don't sort out integers and enums properly, yet. */
5142 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
5143 if (TREE_TYPE (result) != type1)
5144 result = build1 (NOP_EXPR, type1, result);
5145 /* Expand both sides into the same slot,
5146 hopefully the target of the ?: expression. */
5147 if (TREE_CODE (op1) == TARGET_EXPR && TREE_CODE (op2) == TARGET_EXPR)
5149 tree slot = build (VAR_DECL, TREE_TYPE (result));
5150 layout_decl (slot, 0);
5151 result = build (TARGET_EXPR, TREE_TYPE (result),
5152 slot, result, NULL_TREE, NULL_TREE);
5157 /* They don't match; promote them both and then try to reconcile them.
5158 But don't permit mismatching enum types. */
5159 if (code1 == ENUMERAL_TYPE)
5161 if (code2 == ENUMERAL_TYPE)
5163 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'",
5165 return error_mark_node;
5167 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2)
5168 && type2 != type_promotes_to (type1))
5169 warning ("enumeral and non-enumeral type in conditional expression");
5171 else if (extra_warnings
5172 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1)
5173 && type1 != type_promotes_to (type2))
5174 warning ("enumeral and non-enumeral type in conditional expression");
5176 if (code1 != VOID_TYPE)
5178 op1 = default_conversion (op1);
5179 type1 = TREE_TYPE (op1);
5180 if (TYPE_PTRMEMFUNC_P (type1))
5181 type1 = TYPE_PTRMEMFUNC_FN_TYPE (type1);
5182 code1 = TREE_CODE (type1);
5184 if (code2 != VOID_TYPE)
5186 op2 = default_conversion (op2);
5187 type2 = TREE_TYPE (op2);
5188 if (TYPE_PTRMEMFUNC_P (type2))
5189 type2 = TYPE_PTRMEMFUNC_FN_TYPE (type2);
5190 code2 = TREE_CODE (type2);
5193 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE
5194 && real_lvalue_p (op1) && real_lvalue_p (op2)
5195 && comptypes (type1, type2, COMPARE_BASE | COMPARE_RELAXED))
5197 type1 = build_reference_type (type1);
5198 type2 = build_reference_type (type2);
5199 result_type = common_type (type1, type2);
5200 op1 = convert_to_reference (result_type, op1, CONV_IMPLICIT,
5201 LOOKUP_NORMAL, NULL_TREE);
5202 op2 = convert_to_reference (result_type, op2, CONV_IMPLICIT,
5203 LOOKUP_NORMAL, NULL_TREE);
5205 /* Quickly detect the usual case where op1 and op2 have the same type
5207 else if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5210 result_type = type1;
5213 cp_build_qualified_type (type1,
5214 CP_TYPE_QUALS (TREE_TYPE (op1))
5215 | CP_TYPE_QUALS (TREE_TYPE (op2)));
5217 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
5218 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
5220 result_type = common_type (type1, type2);
5222 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5224 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
5225 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
5226 result_type = void_type_node;
5228 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op2))
5229 result_type = qualify_type (type1, type2);
5230 else if (code2 == POINTER_TYPE && null_ptr_cst_p (op1))
5231 result_type = qualify_type (type2, type1);
5232 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5234 if (comp_target_types (type1, type2, 1))
5235 result_type = common_type (type1, type2);
5236 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
5238 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
5239 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5240 result_type = qualify_type (type1, type2);
5242 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
5244 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
5245 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5246 result_type = qualify_type (type2, type1);
5249 else if (same_or_base_type_p (type2, type1))
5250 result_type = type2;
5251 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
5252 && IS_AGGR_TYPE (TREE_TYPE (type2))
5253 && (result_type = common_base_type (TREE_TYPE (type1),
5254 TREE_TYPE (type2))))
5256 if (result_type == error_mark_node)
5258 cp_error ("common base type of types `%T' and `%T' is ambiguous",
5259 TREE_TYPE (type1), TREE_TYPE (type2));
5260 result_type = ptr_type_node;
5265 && result_type != TREE_TYPE (type1)
5266 && result_type != TREE_TYPE (type2))
5267 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
5268 type1, type2, result_type);
5270 result_type = build_pointer_type (result_type);
5275 pedwarn ("pointer type mismatch in conditional expression");
5276 result_type = ptr_type_node;
5279 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5281 pedwarn ("pointer/integer type mismatch in conditional expression");
5282 result_type = type1;
5284 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5286 pedwarn ("pointer/integer type mismatch in conditional expression");
5287 result_type = type2;
5289 if (type2 == unknown_type_node)
5290 result_type = type1;
5291 else if (type1 == unknown_type_node)
5292 result_type = type2;
5296 /* The match does not look good. If either is
5297 an aggregate value, try converting to a scalar type. */
5298 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
5300 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'",
5302 return error_mark_node;
5304 /* Warning: this code assumes that conversion between cv-variants of
5305 a type is done using NOP_EXPRs. */
5306 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
5308 /* There are other types besides pointers and records. */
5310 if (code2 == POINTER_TYPE)
5311 tmp = build_pointer_type
5312 (cp_build_qualified_type (TREE_TYPE (type2),
5314 | TYPE_QUAL_VOLATILE
5315 | TYPE_QUAL_RESTRICT));
5318 tmp = build_type_conversion (tmp, op1, 0);
5319 if (tmp == NULL_TREE)
5321 cp_error ("incompatible types `%T' and `%T' in `?:'",
5323 return error_mark_node;
5325 if (tmp == error_mark_node)
5326 error ("ambiguous pointer conversion");
5329 result_type = common_type (type2, TREE_TYPE (tmp));
5332 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
5335 if (code1 == POINTER_TYPE)
5336 tmp = build_pointer_type
5337 (cp_build_qualified_type (TREE_TYPE (type1),
5339 | TYPE_QUAL_VOLATILE
5340 | TYPE_QUAL_RESTRICT));
5344 tmp = build_type_conversion (tmp, op2, 0);
5345 if (tmp == NULL_TREE)
5347 cp_error ("incompatible types `%T' and `%T' in `?:'",
5349 return error_mark_node;
5351 if (tmp == error_mark_node)
5352 error ("ambiguous pointer conversion");
5355 result_type = common_type (type1, TREE_TYPE (tmp));
5358 else if (flag_cond_mismatch)
5359 result_type = void_type_node;
5362 error ("type mismatch in conditional expression");
5363 return error_mark_node;
5367 if (TREE_CODE (result_type) == POINTER_TYPE
5368 && TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
5369 result_type = build_ptrmemfunc_type (result_type);
5371 if (result_type != TREE_TYPE (op1))
5372 op1 = convert_for_initialization
5373 (NULL_TREE, result_type, op1, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5374 if (result_type != TREE_TYPE (op2))
5375 op2 = convert_for_initialization
5376 (NULL_TREE, result_type, op2, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5378 if (TREE_CODE (ifexp) == INTEGER_CST)
5379 return integer_zerop (ifexp) ? op2 : op1;
5381 return convert_from_reference
5382 (fold (build (COND_EXPR, result_type, ifexp, op1, op2)));
5385 /* Handle overloading of the ',' operator when needed. Otherwise,
5386 this function just builds an expression list. */
5389 build_x_compound_expr (list)
5392 tree rest = TREE_CHAIN (list);
5395 if (processing_template_decl)
5396 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5398 if (rest == NULL_TREE)
5399 return build_compound_expr (list);
5401 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5402 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5404 return build_x_compound_expr (expr_tree_cons (NULL_TREE, result,
5405 TREE_CHAIN (rest)));
5407 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5409 /* the left-hand operand of a comma expression is like an expression
5410 statement: we should warn if it doesn't have any side-effects,
5411 unless it was explicitly cast to (void). */
5412 if ((extra_warnings || warn_unused)
5413 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5414 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5415 warning("left-hand operand of comma expression has no effect");
5417 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5418 else if (warn_unused)
5419 warn_if_unused_value (TREE_VALUE(list));
5422 return build_compound_expr
5423 (expr_tree_cons (NULL_TREE, TREE_VALUE (list),
5424 build_expr_list (NULL_TREE,
5425 build_x_compound_expr (rest))));
5428 /* Given a list of expressions, return a compound expression
5429 that performs them all and returns the value of the last of them. */
5432 build_compound_expr (list)
5438 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5439 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5441 if (TREE_CHAIN (list) == 0)
5443 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5444 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5445 if (TREE_CODE (list) == NOP_EXPR
5446 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5447 list = TREE_OPERAND (list, 0);
5449 /* Convert arrays to pointers. */
5450 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5451 return default_conversion (TREE_VALUE (list));
5453 return TREE_VALUE (list);
5456 first = TREE_VALUE (list);
5457 first = require_complete_type_in_void (first);
5458 if (first == error_mark_node)
5459 return error_mark_node;
5461 rest = build_compound_expr (TREE_CHAIN (list));
5462 if (rest == error_mark_node)
5463 return error_mark_node;
5465 /* When pedantic, a compound expression cannot be a constant expression. */
5466 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5469 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5470 break_out_cleanups (first), rest);
5474 build_static_cast (type, expr)
5480 if (type == error_mark_node || expr == error_mark_node)
5481 return error_mark_node;
5483 if (TREE_CODE (expr) == OFFSET_REF)
5484 expr = resolve_offset_ref (expr);
5486 if (processing_template_decl)
5488 tree t = build_min (STATIC_CAST_EXPR, copy_to_permanent (type),
5493 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5494 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5495 if (TREE_CODE (type) != REFERENCE_TYPE
5496 && TREE_CODE (expr) == NOP_EXPR
5497 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5498 expr = TREE_OPERAND (expr, 0);
5500 if (TREE_CODE (type) == VOID_TYPE)
5501 return build1 (CONVERT_EXPR, type, expr);
5503 if (TREE_CODE (type) == REFERENCE_TYPE)
5504 return (convert_from_reference
5505 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5506 LOOKUP_COMPLAIN, NULL_TREE)));
5508 if (IS_AGGR_TYPE (type))
5509 return build_cplus_new
5510 (type, (build_method_call
5511 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5512 TYPE_BINFO (type), LOOKUP_NORMAL)));
5514 expr = decay_conversion (expr);
5515 intype = TREE_TYPE (expr);
5517 /* FIXME handle casting to array type. */
5520 if (can_convert_arg (type, intype, expr))
5522 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5525 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5526 && at_least_as_qualified_p (TREE_TYPE (type),
5528 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5529 && ! TREE_VIA_VIRTUAL (binfo))
5532 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5534 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5535 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))))
5536 && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type)),
5537 TREE_TYPE (TREE_TYPE (intype)))
5538 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5539 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5540 && ! TREE_VIA_VIRTUAL (binfo))
5543 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5544 && TREE_CODE (type) != ARRAY_TYPE
5545 && TREE_CODE (type) != FUNCTION_TYPE
5546 && can_convert (intype, type))
5550 return build_c_cast (type, expr);
5552 cp_error ("static_cast from `%T' to `%T'", intype, type);
5553 return error_mark_node;
5557 build_reinterpret_cast (type, expr)
5562 if (type == error_mark_node || expr == error_mark_node)
5563 return error_mark_node;
5565 if (TREE_CODE (expr) == OFFSET_REF)
5566 expr = resolve_offset_ref (expr);
5568 if (processing_template_decl)
5570 tree t = build_min (REINTERPRET_CAST_EXPR,
5571 copy_to_permanent (type), expr);
5575 if (TREE_CODE (type) != REFERENCE_TYPE)
5577 expr = decay_conversion (expr);
5579 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5580 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5581 if (TREE_CODE (expr) == NOP_EXPR
5582 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5583 expr = TREE_OPERAND (expr, 0);
5586 intype = TREE_TYPE (expr);
5588 if (TREE_CODE (type) == REFERENCE_TYPE)
5590 if (! real_lvalue_p (expr))
5592 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5593 return error_mark_node;
5595 expr = build_unary_op (ADDR_EXPR, expr, 0);
5596 if (expr != error_mark_node)
5597 expr = build_reinterpret_cast
5598 (build_pointer_type (TREE_TYPE (type)), expr);
5599 if (expr != error_mark_node)
5600 expr = build_indirect_ref (expr, 0);
5603 else if (same_type_p (TYPE_MAIN_VARIANT (intype),
5604 TYPE_MAIN_VARIANT (type)))
5605 return build_static_cast (type, expr);
5607 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5608 || TREE_CODE (intype) == ENUMERAL_TYPE))
5610 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5612 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5613 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5616 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5617 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5619 if (TREE_READONLY_DECL_P (expr))
5620 expr = decl_constant_value (expr);
5621 return fold (build1 (NOP_EXPR, type, expr));
5623 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5624 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5626 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5627 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5630 if (TREE_READONLY_DECL_P (expr))
5631 expr = decl_constant_value (expr);
5632 return fold (build1 (NOP_EXPR, type, expr));
5634 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5635 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5637 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5638 if (TREE_READONLY_DECL_P (expr))
5639 expr = decl_constant_value (expr);
5640 return fold (build1 (NOP_EXPR, type, expr));
5644 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5645 return error_mark_node;
5648 return cp_convert (type, expr);
5652 build_const_cast (type, expr)
5657 if (type == error_mark_node || expr == error_mark_node)
5658 return error_mark_node;
5660 if (TREE_CODE (expr) == OFFSET_REF)
5661 expr = resolve_offset_ref (expr);
5663 if (processing_template_decl)
5665 tree t = build_min (CONST_CAST_EXPR, copy_to_permanent (type),
5670 if (!POINTER_TYPE_P (type))
5672 cp_error ("`%T' is not a pointer, reference, or pointer-to-data-member type",
5674 cp_error ("as required by const_cast");
5676 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5678 cp_error ("`%T' is a pointer or reference to a function type",
5680 cp_error ("which is forbidden by const_cast");
5681 return error_mark_node;
5684 if (TREE_CODE (type) != REFERENCE_TYPE)
5686 expr = decay_conversion (expr);
5688 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5689 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5690 if (TREE_CODE (expr) == NOP_EXPR
5691 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5692 expr = TREE_OPERAND (expr, 0);
5695 intype = TREE_TYPE (expr);
5697 if (same_type_p (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type)))
5698 return build_static_cast (type, expr);
5699 else if (TREE_CODE (type) == REFERENCE_TYPE)
5701 if (! real_lvalue_p (expr))
5703 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5704 return error_mark_node;
5707 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5709 expr = build_unary_op (ADDR_EXPR, expr, 0);
5710 expr = build1 (NOP_EXPR, type, expr);
5711 return convert_from_reference (expr);
5714 else if (TREE_CODE (type) == POINTER_TYPE
5715 && TREE_CODE (intype) == POINTER_TYPE
5716 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5717 return cp_convert (type, expr);
5719 cp_error ("const_cast from `%T' to `%T'", intype, type);
5720 return error_mark_node;
5723 /* Build an expression representing a cast to type TYPE of expression EXPR.
5725 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5726 when doing the cast. */
5729 build_c_cast (type, expr)
5732 register tree value = expr;
5735 if (type == error_mark_node || expr == error_mark_node)
5736 return error_mark_node;
5738 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5739 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5740 if (TREE_CODE (type) != REFERENCE_TYPE
5741 && TREE_CODE (value) == NOP_EXPR
5742 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5743 value = TREE_OPERAND (value, 0);
5745 if (TREE_CODE (value) == OFFSET_REF)
5746 value = resolve_offset_ref (value);
5748 if (TREE_CODE (type) == ARRAY_TYPE)
5750 /* Allow casting from T1* to T2[] because Cfront allows it.
5751 NIHCL uses it. It is not valid ANSI C however, and hence, not
5753 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5756 pedwarn ("ANSI C++ forbids casting to an array type");
5757 type = build_pointer_type (TREE_TYPE (type));
5761 error ("ANSI C++ forbids casting to an array type");
5762 return error_mark_node;
5766 if (TREE_CODE (type) == FUNCTION_TYPE
5767 || TREE_CODE (type) == METHOD_TYPE)
5769 cp_error ("casting to function type `%T'", type);
5770 return error_mark_node;
5773 if (IS_SIGNATURE (type))
5775 error ("cast specifies signature type");
5776 return error_mark_node;
5779 if (processing_template_decl)
5781 tree t = build_min (CAST_EXPR, type,
5782 min_tree_cons (NULL_TREE, value, NULL_TREE));
5786 /* Convert functions and arrays to pointers and
5787 convert references to their expanded types,
5788 but don't convert any other types. If, however, we are
5789 casting to a class type, there's no reason to do this: the
5790 cast will only succeed if there is a converting constructor,
5791 and the default conversions will be done at that point. In
5792 fact, doing the default conversion here is actually harmful
5796 struct S { S(const A&); };
5798 since we don't want the array-to-pointer conversion done. */
5799 if (!IS_AGGR_TYPE (type))
5801 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5802 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5803 /* Don't do the default conversion on a ->* expression. */
5804 && ! (TREE_CODE (type) == POINTER_TYPE
5805 && bound_pmf_p (value)))
5806 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5807 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5808 value = default_conversion (value);
5810 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5811 /* However, even for class types, we still need to strip away
5812 the reference type, since the call to convert_force below
5813 does not expect the input expression to be of reference
5815 value = convert_from_reference (value);
5817 otype = TREE_TYPE (value);
5819 /* Optionally warn about potentially worrisome casts. */
5822 && TREE_CODE (type) == POINTER_TYPE
5823 && TREE_CODE (otype) == POINTER_TYPE
5824 && !at_least_as_qualified_p (TREE_TYPE (type),
5826 cp_warning ("cast discards qualifiers from pointer target type");
5828 /* Warn about possible alignment problems. */
5829 if (STRICT_ALIGNMENT && warn_cast_align
5830 && TREE_CODE (type) == POINTER_TYPE
5831 && TREE_CODE (otype) == POINTER_TYPE
5832 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5833 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5834 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5835 warning ("cast increases required alignment of target type");
5838 /* We should see about re-enabling these, they seem useful to
5840 if (TREE_CODE (type) == INTEGER_TYPE
5841 && TREE_CODE (otype) == POINTER_TYPE
5842 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5843 warning ("cast from pointer to integer of different size");
5845 if (TREE_CODE (type) == POINTER_TYPE
5846 && TREE_CODE (otype) == INTEGER_TYPE
5847 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5848 /* Don't warn about converting 0 to pointer,
5849 provided the 0 was explicit--not cast or made by folding. */
5850 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5851 warning ("cast to pointer from integer of different size");
5854 if (TREE_CODE (type) == VOID_TYPE)
5856 value = require_complete_type_in_void (value);
5857 if (value != error_mark_node)
5858 value = build1 (CONVERT_EXPR, void_type_node, value);
5860 else if (TREE_CODE (type) == REFERENCE_TYPE)
5861 value = (convert_from_reference
5862 (convert_to_reference (type, value, CONV_C_CAST,
5863 LOOKUP_COMPLAIN, NULL_TREE)));
5868 if (TREE_READONLY_DECL_P (value))
5869 value = decl_constant_value (value);
5872 value = convert_force (type, value, CONV_C_CAST);
5874 /* Ignore any integer overflow caused by the cast. */
5875 if (TREE_CODE (value) == INTEGER_CST)
5877 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5878 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5882 /* Always produce some operator for an explicit cast,
5883 so we can tell (for -pedantic) that the cast is no lvalue. */
5884 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5885 && real_lvalue_p (value))
5886 value = non_lvalue (value);
5891 /* Build an assignment expression of lvalue LHS from value RHS.
5892 MODIFYCODE is the code for a binary operator that we use
5893 to combine the old value of LHS with RHS to get the new value.
5894 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5896 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5899 build_modify_expr (lhs, modifycode, rhs)
5901 enum tree_code modifycode;
5904 register tree result;
5906 tree lhstype = TREE_TYPE (lhs);
5907 tree olhstype = lhstype;
5910 /* Avoid duplicate error messages from operands that had errors. */
5911 if (lhs == error_mark_node || rhs == error_mark_node)
5912 return error_mark_node;
5914 /* Types that aren't fully specified cannot be used in assignments. */
5915 lhs = require_complete_type (lhs);
5919 /* Handle assignment to signature pointers/refs. */
5921 if (TYPE_LANG_SPECIFIC (lhstype)
5922 && (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5924 return build_signature_pointer_constructor (lhs, rhs);
5927 /* Handle control structure constructs used as "lvalues". */
5929 switch (TREE_CODE (lhs))
5931 /* Handle --foo = 5; as these are valid constructs in C++ */
5932 case PREDECREMENT_EXPR:
5933 case PREINCREMENT_EXPR:
5934 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5935 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5936 stabilize_reference (TREE_OPERAND (lhs, 0)),
5937 TREE_OPERAND (lhs, 1));
5938 return build (COMPOUND_EXPR, lhstype,
5940 build_modify_expr (TREE_OPERAND (lhs, 0),
5943 /* Handle (a, b) used as an "lvalue". */
5945 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5947 if (newrhs == error_mark_node)
5948 return error_mark_node;
5949 return build (COMPOUND_EXPR, lhstype,
5950 TREE_OPERAND (lhs, 0), newrhs);
5953 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5954 if (newrhs == error_mark_node)
5955 return error_mark_node;
5956 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5958 /* Handle (a ? b : c) used as an "lvalue". */
5960 rhs = save_expr (rhs);
5962 /* Produce (a ? (b = rhs) : (c = rhs))
5963 except that the RHS goes through a save-expr
5964 so the code to compute it is only emitted once. */
5966 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5967 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5969 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5971 if (cond == error_mark_node)
5973 /* Make sure the code to compute the rhs comes out
5974 before the split. */
5975 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5976 /* Case to void to suppress warning
5977 from warn_if_unused_value. */
5978 cp_convert (void_type_node, rhs), cond);
5985 if (TREE_CODE (lhs) == OFFSET_REF)
5987 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5989 /* Static class member? */
5990 tree member = TREE_OPERAND (lhs, 1);
5991 if (TREE_CODE (member) == VAR_DECL)
5995 compiler_error ("invalid static class member");
5996 return error_mark_node;
6000 lhs = resolve_offset_ref (lhs);
6002 olhstype = lhstype = TREE_TYPE (lhs);
6005 if (lhs == error_mark_node)
6008 if (TREE_CODE (lhstype) == REFERENCE_TYPE
6009 && modifycode != INIT_EXPR)
6011 lhs = convert_from_reference (lhs);
6012 olhstype = lhstype = TREE_TYPE (lhs);
6015 /* If a binary op has been requested, combine the old LHS value with the RHS
6016 producing the value we should actually store into the LHS. */
6018 if (modifycode == INIT_EXPR)
6020 if (! IS_AGGR_TYPE (lhstype))
6021 /* Do the default thing */;
6024 result = build_method_call (lhs, ctor_identifier,
6025 build_expr_list (NULL_TREE, rhs),
6026 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
6027 if (result == NULL_TREE)
6028 return error_mark_node;
6032 else if (modifycode == NOP_EXPR)
6034 /* `operator=' is not an inheritable operator. */
6035 if (! IS_AGGR_TYPE (lhstype))
6036 /* Do the default thing */;
6039 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
6040 lhs, rhs, make_node (NOP_EXPR));
6041 if (result == NULL_TREE)
6042 return error_mark_node;
6047 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
6049 my_friendly_abort (978652);
6053 lhs = stabilize_reference (lhs);
6054 newrhs = build_binary_op (modifycode, lhs, rhs);
6055 if (newrhs == error_mark_node)
6057 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
6058 TREE_TYPE (lhs), TREE_TYPE (rhs));
6059 return error_mark_node;
6063 /* Handle a cast used as an "lvalue".
6064 We have already performed any binary operator using the value as cast.
6065 Now convert the result to the cast type of the lhs,
6066 and then true type of the lhs and store it there;
6067 then convert result back to the cast type to be the value
6068 of the assignment. */
6070 switch (TREE_CODE (lhs))
6075 case FIX_TRUNC_EXPR:
6076 case FIX_FLOOR_EXPR:
6077 case FIX_ROUND_EXPR:
6079 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6080 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
6081 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
6082 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
6083 newrhs = default_conversion (newrhs);
6085 tree inner_lhs = TREE_OPERAND (lhs, 0);
6088 /* WP 5.4.1: The result is an lvalue if T is a reference type,
6089 otherwise the result is an rvalue. */
6090 if (! lvalue_p (lhs))
6091 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
6093 result = build_modify_expr (inner_lhs, NOP_EXPR,
6094 cp_convert (TREE_TYPE (inner_lhs),
6095 cp_convert (lhstype, newrhs)));
6096 if (result == error_mark_node)
6098 return cp_convert (TREE_TYPE (lhs), result);
6105 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
6106 Reject anything strange now. */
6108 if (!lvalue_or_else (lhs, "assignment"))
6109 return error_mark_node;
6111 GNU_xref_assign (lhs);
6113 /* Warn about storing in something that is `const'. */
6114 /* For C++, don't warn if this is initialization. */
6115 if (modifycode != INIT_EXPR
6116 /* For assignment to `const' signature pointer/reference fields,
6117 don't warn either, we already printed a better message before. */
6118 && ! (TREE_CODE (lhs) == COMPONENT_REF
6119 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
6120 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
6121 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
6122 /* Functions are not modifiable, even though they are
6124 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
6125 || ((TREE_CODE (lhstype) == RECORD_TYPE
6126 || TREE_CODE (lhstype) == UNION_TYPE)
6127 && C_TYPE_FIELDS_READONLY (lhstype))
6128 || (TREE_CODE (lhstype) == REFERENCE_TYPE
6129 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
6130 readonly_error (lhs, "assignment", 0);
6132 /* If storing into a structure or union member,
6133 it has probably been given type `int'.
6134 Compute the type that would go with
6135 the actual amount of storage the member occupies. */
6137 if (TREE_CODE (lhs) == COMPONENT_REF
6138 && (TREE_CODE (lhstype) == INTEGER_TYPE
6139 || TREE_CODE (lhstype) == REAL_TYPE
6140 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6142 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6144 /* If storing in a field that is in actuality a short or narrower
6145 than one, we must store in the field in its actual type. */
6147 if (lhstype != TREE_TYPE (lhs))
6149 lhs = copy_node (lhs);
6150 TREE_TYPE (lhs) = lhstype;
6154 /* check to see if there is an assignment to `this' */
6155 if (lhs == current_class_ptr)
6157 if (flag_this_is_variable > 0
6158 && DECL_NAME (current_function_decl) != NULL_TREE
6159 && (DECL_NAME (current_function_decl)
6160 != constructor_name (current_class_type)))
6161 warning ("assignment to `this' not in constructor or destructor");
6162 current_function_just_assigned_this = 1;
6165 if (modifycode != INIT_EXPR)
6167 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
6168 modifycode = NOP_EXPR;
6169 /* Reference-bashing */
6170 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
6172 tree tmp = convert_from_reference (lhs);
6173 lhstype = TREE_TYPE (tmp);
6174 if (TYPE_SIZE (lhstype) == 0)
6176 incomplete_type_error (lhs, lhstype);
6177 return error_mark_node;
6182 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
6184 tree tmp = convert_from_reference (newrhs);
6185 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
6187 incomplete_type_error (newrhs, TREE_TYPE (tmp));
6188 return error_mark_node;
6194 if (TREE_SIDE_EFFECTS (lhs))
6195 lhs = stabilize_reference (lhs);
6196 if (TREE_SIDE_EFFECTS (newrhs))
6197 newrhs = stabilize_reference (newrhs);
6199 /* Convert new value to destination type. */
6201 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6205 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
6207 cp_error ("incompatible types in assignment of `%T' to `%T'",
6208 TREE_TYPE (rhs), lhstype);
6209 return error_mark_node;
6212 /* Allow array assignment in compiler-generated code. */
6213 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
6214 pedwarn ("ANSI C++ forbids assignment of arrays");
6216 /* Have to wrap this in RTL_EXPR for two cases:
6217 in base or member initialization and if we
6218 are a branch of a ?: operator. Since we
6219 can't easily know the latter, just do it always. */
6221 result = make_node (RTL_EXPR);
6223 TREE_TYPE (result) = void_type_node;
6224 do_pending_stack_adjust ();
6225 start_sequence_for_rtl_expr (result);
6227 /* As a matter of principle, `start_sequence' should do this. */
6230 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6231 ? 1 + (modifycode != INIT_EXPR): 0;
6232 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
6235 do_pending_stack_adjust ();
6237 TREE_SIDE_EFFECTS (result) = 1;
6238 RTL_EXPR_SEQUENCE (result) = get_insns ();
6239 RTL_EXPR_RTL (result) = const0_rtx;
6244 if (modifycode == INIT_EXPR)
6246 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
6247 "assignment", NULL_TREE, 0);
6248 if (lhs == DECL_RESULT (current_function_decl))
6250 if (DECL_INITIAL (lhs))
6251 warning ("return value from function receives multiple initializations");
6252 DECL_INITIAL (lhs) = newrhs;
6257 /* Avoid warnings on enum bit fields. */
6258 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
6259 && TREE_CODE (lhstype) == INTEGER_TYPE)
6261 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
6263 newrhs = convert_force (lhstype, newrhs, 0);
6266 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
6268 if (TREE_CODE (newrhs) == CALL_EXPR
6269 && TYPE_NEEDS_CONSTRUCTING (lhstype))
6270 newrhs = build_cplus_new (lhstype, newrhs);
6272 /* Can't initialize directly from a TARGET_EXPR, since that would
6273 cause the lhs to be constructed twice, and possibly result in
6274 accidental self-initialization. So we force the TARGET_EXPR to be
6275 expanded without a target. */
6276 if (TREE_CODE (newrhs) == TARGET_EXPR)
6277 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6278 TREE_OPERAND (newrhs, 0));
6281 if (newrhs == error_mark_node)
6282 return error_mark_node;
6284 if (TREE_CODE (newrhs) == COND_EXPR)
6287 tree cond = TREE_OPERAND (newrhs, 0);
6289 if (TREE_SIDE_EFFECTS (lhs))
6290 cond = build_compound_expr (tree_cons
6292 build_expr_list (NULL_TREE, cond)));
6294 /* Cannot have two identical lhs on this one tree (result) as preexpand
6295 calls will rip them out and fill in RTL for them, but when the
6296 rtl is generated, the calls will only be in the first side of the
6297 condition, not on both, or before the conditional jump! (mrs) */
6298 lhs1 = break_out_calls (lhs);
6301 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6302 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6303 lhstype, lhs, newrhs);
6306 tree result_type = TREE_TYPE (newrhs);
6307 /* We have to convert each arm to the proper type because the
6308 types may have been munged by constant folding. */
6310 = build (COND_EXPR, result_type, cond,
6311 build_modify_expr (lhs, modifycode,
6312 cp_convert (result_type,
6313 TREE_OPERAND (newrhs, 1))),
6314 build_modify_expr (lhs1, modifycode,
6315 cp_convert (result_type,
6316 TREE_OPERAND (newrhs, 2))));
6320 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6321 lhstype, lhs, newrhs);
6323 TREE_SIDE_EFFECTS (result) = 1;
6325 /* If we got the LHS in a different type for storing in,
6326 convert the result back to the nominal type of LHS
6327 so that the value we return always has the same type
6328 as the LHS argument. */
6330 if (olhstype == TREE_TYPE (result))
6332 /* Avoid warnings converting integral types back into enums
6333 for enum bit fields. */
6334 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6335 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6337 result = build (COMPOUND_EXPR, olhstype, result, olhs);
6338 TREE_NO_UNUSED_WARNING (result) = 1;
6341 return convert_for_assignment (olhstype, result, "assignment",
6346 build_x_modify_expr (lhs, modifycode, rhs)
6348 enum tree_code modifycode;
6351 if (processing_template_decl)
6352 return build_min_nt (MODOP_EXPR, lhs,
6353 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6355 if (modifycode != NOP_EXPR)
6357 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6358 make_node (modifycode));
6362 return build_modify_expr (lhs, modifycode, rhs);
6366 /* Get difference in deltas for different pointer to member function
6367 types. Return integer_zero_node, if FROM cannot be converted to a
6368 TO type. If FORCE is true, then allow reverse conversions as well.
6370 Note that the naming of FROM and TO is kind of backwards; the return
6371 value is what we add to a TO in order to get a FROM. They are named
6372 this way because we call this function to find out how to convert from
6373 a pointer to member of FROM to a pointer to member of TO. */
6376 get_delta_difference (from, to, force)
6380 tree delta = integer_zero_node;
6386 /* Should get_base_distance here, so we can check if any thing along the
6387 path is virtual, and we need to make sure we stay
6388 inside the real binfos when going through virtual bases.
6389 Maybe we should replace virtual bases with
6390 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6391 binfo = get_binfo (from, to, 1);
6392 if (binfo == error_mark_node)
6394 error (" in pointer to member function conversion");
6401 error_not_base_type (from, to);
6402 error (" in pointer to member conversion");
6405 binfo = get_binfo (to, from, 1);
6406 if (binfo == 0 || binfo == error_mark_node)
6408 if (TREE_VIA_VIRTUAL (binfo))
6410 binfo = binfo_member (BINFO_TYPE (binfo),
6411 CLASSTYPE_VBASECLASSES (from));
6412 cp_warning ("pointer to member cast to virtual base `%T'",
6413 BINFO_TYPE (binfo));
6414 warning (" will only work if you are very careful");
6416 delta = BINFO_OFFSET (binfo);
6417 delta = cp_convert (ptrdiff_type_node, delta);
6419 return build_binary_op (MINUS_EXPR,
6424 if (TREE_VIA_VIRTUAL (binfo))
6428 cp_warning ("pointer to member cast from virtual base `%T'",
6429 BINFO_TYPE (binfo));
6430 warning (" will only work if you are very careful");
6433 cp_error ("pointer to member conversion from virtual base `%T'",
6434 BINFO_TYPE (binfo));
6437 return BINFO_OFFSET (binfo);
6441 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6442 tree type, delta, idx, pfn, delta2;
6447 /* This is the old way we did it. We want to avoid calling
6448 digest_init, so that it can give an error if we use { } when
6449 initializing a pointer to member function. */
6453 u = build_nt (CONSTRUCTOR, NULL_TREE,
6454 expr_tree_cons (pfn_identifier, pfn, NULL_TREE));
6458 u = build_nt (CONSTRUCTOR, NULL_TREE,
6459 expr_tree_cons (delta2_identifier, delta2, NULL_TREE));
6462 u = build_nt (CONSTRUCTOR, NULL_TREE,
6463 expr_tree_cons (NULL_TREE, delta,
6464 expr_tree_cons (NULL_TREE, idx,
6465 expr_tree_cons (NULL_TREE, u, NULL_TREE))));
6467 return digest_init (type, u, (tree*)0);
6469 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6471 int allconstant, allsimple;
6473 delta_field = TYPE_FIELDS (type);
6474 idx_field = TREE_CHAIN (delta_field);
6475 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6476 subtype = TREE_TYPE (pfn_or_delta2_field);
6477 pfn_field = TYPE_FIELDS (subtype);
6478 delta2_field = TREE_CHAIN (pfn_field);
6482 allconstant = TREE_CONSTANT (pfn);
6483 allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6484 u = expr_tree_cons (pfn_field, pfn, NULL_TREE);
6488 delta2 = convert_and_check (delta_type_node, delta2);
6489 allconstant = TREE_CONSTANT (delta2);
6490 allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6491 u = expr_tree_cons (delta2_field, delta2, NULL_TREE);
6494 delta = convert_and_check (delta_type_node, delta);
6495 idx = convert_and_check (delta_type_node, idx);
6497 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6498 allsimple = allsimple
6499 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6500 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6502 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6503 u = expr_tree_cons (delta_field, delta,
6504 expr_tree_cons (idx_field, idx,
6505 expr_tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6506 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6507 TREE_CONSTANT (u) = allconstant;
6508 TREE_STATIC (u) = allconstant && allsimple;
6513 /* Build a constructor for a pointer to member function. It can be
6514 used to initialize global variables, local variable, or used
6515 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6518 If FORCE is non-zero, then force this conversion, even if
6519 we would rather not do it. Usually set when using an explicit
6522 Return error_mark_node, if something goes wrong. */
6525 build_ptrmemfunc (type, pfn, force)
6531 /* Handle multiple conversions of pointer to member functions. */
6532 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6534 tree idx = integer_zero_node;
6535 tree delta = integer_zero_node;
6536 tree delta2 = integer_zero_node;
6537 tree npfn = NULL_TREE;
6538 tree ndelta, ndelta2;
6542 /* Is is already the right type? */
6543 if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
6546 pfn_type = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn));
6548 && comp_target_types (type, pfn_type, 1) != 1)
6549 cp_error ("conversion to `%T' from `%T'", type, pfn_type);
6551 if (TREE_CODE (pfn) == PTRMEM_CST)
6553 /* We could just build the resulting CONSTRUCTOR now, but we
6554 don't, relying on the general machinery below, together
6555 with constant-folding, to do the right thing. We don't
6556 want to return a PTRMEM_CST here, even though we could,
6557 because a pointer-to-member constant ceases to be a
6558 constant (from the point of view of the language) when it
6559 is cast to another type. */
6561 expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
6563 /* This constant points to a non-virtual function.
6564 NDELTA2 will be NULL, but it's value doesn't really
6565 matter since we won't use it anyhow. */
6566 ndelta2 = integer_zero_node;
6570 ndelta = cp_convert (ptrdiff_type_node,
6571 build_component_ref (pfn,
6574 ndelta2 = cp_convert (ptrdiff_type_node,
6575 DELTA2_FROM_PTRMEMFUNC (pfn));
6576 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6579 n = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (pfn_type)),
6580 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6582 delta = build_binary_op (PLUS_EXPR, ndelta, n);
6583 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n);
6584 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6586 /* If it's a virtual function, this is what we want. */
6587 e2 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx,
6590 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6591 npfn = build1 (NOP_EXPR, type, pfn);
6592 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6594 /* But if it's a non-virtual function, or NULL, we use this
6596 e3 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta,
6597 idx, npfn, NULL_TREE);
6598 return build_conditional_expr (e1, e2, e3);
6601 /* Handle null pointer to member function conversions. */
6602 if (integer_zerop (pfn))
6604 pfn = build_c_cast (type, integer_zero_node);
6605 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type),
6606 integer_zero_node, integer_zero_node,
6610 if (type_unknown_p (pfn))
6611 return instantiate_type (type, pfn, 1);
6613 fn = TREE_OPERAND (pfn, 0);
6614 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6615 return make_ptrmem_cst (build_ptrmemfunc_type (type), fn);
6618 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6622 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6629 tree type = TREE_TYPE (cst);
6630 tree fn = PTRMEM_CST_MEMBER (cst);
6631 tree ptr_class, fn_class;
6633 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6635 /* The class that the function belongs to. */
6636 fn_class = DECL_CLASS_CONTEXT (fn);
6638 /* The class that we're creating a pointer to member of. */
6639 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6641 /* First, calculate the adjustment to the function's class. */
6642 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6644 if (!DECL_VIRTUAL_P (fn))
6646 *idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6647 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6648 *delta2 = NULL_TREE;
6652 /* If we're dealing with a virtual function, we have to adjust 'this'
6653 again, to point to the base which provides the vtable entry for
6654 fn; the call will do the opposite adjustment. */
6655 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6656 tree binfo = binfo_or_else (orig_class, fn_class);
6657 *delta = size_binop (PLUS_EXPR, *delta, BINFO_OFFSET (binfo));
6659 /* Map everything down one to make room for the null PMF. */
6660 *idx = size_binop (PLUS_EXPR, DECL_VINDEX (fn), integer_one_node);
6663 /* Offset from an object of PTR_CLASS to the vptr for ORIG_CLASS. */
6664 *delta2 = size_binop (PLUS_EXPR, *delta,
6665 get_vfield_offset (TYPE_BINFO (orig_class)));
6669 /* Return an expression for DELTA2 from the pointer-to-member function
6673 delta2_from_ptrmemfunc (t)
6676 if (TREE_CODE (t) == PTRMEM_CST)
6683 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6688 return (build_component_ref
6689 (build_component_ref (t,
6690 pfn_or_delta2_identifier, NULL_TREE,
6692 delta2_identifier, NULL_TREE, 0));
6695 /* Return an expression for PFN from the pointer-to-member function
6699 pfn_from_ptrmemfunc (t)
6702 if (TREE_CODE (t) == PTRMEM_CST)
6709 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6714 return (build_component_ref
6715 (build_component_ref (t,
6716 pfn_or_delta2_identifier, NULL_TREE,
6718 pfn_identifier, NULL_TREE, 0));
6721 /* Convert value RHS to type TYPE as preparation for an assignment
6722 to an lvalue of type TYPE.
6723 The real work of conversion is done by `convert'.
6724 The purpose of this function is to generate error messages
6725 for assignments that are not allowed in C.
6726 ERRTYPE is a string to use in error messages:
6727 "assignment", "return", etc.
6729 C++: attempts to allow `convert' to find conversions involving
6730 implicit type conversion between aggregate and scalar types
6731 as per 8.5.6 of C++ manual. Does not randomly dereference
6732 pointers to aggregates! */
6735 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6737 const char *errtype;
6741 register enum tree_code codel = TREE_CODE (type);
6742 register tree rhstype;
6743 register enum tree_code coder;
6745 if (codel == OFFSET_TYPE)
6746 my_friendly_abort (990505);
6748 if (TREE_CODE (rhs) == OFFSET_REF)
6749 rhs = resolve_offset_ref (rhs);
6751 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6752 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6753 rhs = TREE_OPERAND (rhs, 0);
6755 if (rhs == error_mark_node || TREE_TYPE (rhs) == error_mark_node)
6756 return error_mark_node;
6757 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6758 return error_mark_node;
6760 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6761 || is_overloaded_fn (rhs))
6762 rhs = default_conversion (rhs);
6763 else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6764 rhs = convert_from_reference (rhs);
6766 /* If rhs is some sort of overloaded function, ocp_convert will either
6767 do the right thing or complain; we don't need to check anything else.
6768 So just hand off. */
6769 if (type_unknown_p (rhs))
6770 return ocp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6772 rhstype = TREE_TYPE (rhs);
6773 coder = TREE_CODE (rhstype);
6775 /* Issue warnings about peculiar, but legal, uses of NULL. */
6776 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6777 cp_warning ("converting NULL to non-pointer type");
6779 /* This should no longer change types on us. */
6780 if (TREE_CODE (rhs) == CONST_DECL)
6781 rhs = DECL_INITIAL (rhs);
6782 else if (TREE_READONLY_DECL_P (rhs))
6783 rhs = decl_constant_value (rhs);
6785 if (same_type_p (type, rhstype))
6787 overflow_warning (rhs);
6791 if (coder == VOID_TYPE)
6793 error ("void value not ignored as it ought to be");
6794 return error_mark_node;
6796 /* Arithmetic types all interconvert. */
6797 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == BOOLEAN_TYPE
6798 || codel == COMPLEX_TYPE)
6799 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == BOOLEAN_TYPE
6800 || coder == COMPLEX_TYPE))
6802 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6803 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6806 cp_warning ("`%T' used for argument %P of `%D'",
6807 rhstype, parmnum, fndecl);
6809 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6811 /* And we should warn if assigning a negative value to
6812 an unsigned variable. */
6813 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6815 if (TREE_CODE (rhs) == INTEGER_CST
6816 && TREE_NEGATED_INT (rhs))
6819 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6820 rhs, parmnum, fndecl);
6822 cp_warning ("%s of negative value `%E' to `%T'",
6823 errtype, rhs, type);
6825 overflow_warning (rhs);
6826 if (TREE_CONSTANT (rhs))
6830 return convert_and_check (type, rhs);
6832 /* Conversions involving enums. */
6833 else if ((codel == ENUMERAL_TYPE
6834 && (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE))
6835 || (coder == ENUMERAL_TYPE
6836 && (INTEGRAL_CODE_P (codel) || codel == REAL_TYPE)))
6838 return ocp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6840 /* Conversions among pointers */
6841 else if (codel == POINTER_TYPE
6842 && (coder == POINTER_TYPE
6843 || (coder == RECORD_TYPE
6844 && (IS_SIGNATURE_POINTER (rhstype)
6845 || IS_SIGNATURE_REFERENCE (rhstype)))))
6847 register tree ttl = TREE_TYPE (type);
6851 if (coder == RECORD_TYPE)
6853 rhs = build_optr_ref (rhs);
6854 rhstype = TREE_TYPE (rhs);
6856 ttr = TREE_TYPE (rhstype);
6858 /* If both pointers are of aggregate type, then we
6859 can give better error messages, and save some work
6861 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6865 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6866 || type == class_star_type_node
6867 || rhstype == class_star_type_node)
6868 binfo = TYPE_BINFO (ttl);
6870 binfo = get_binfo (ttl, ttr, 1);
6872 if (binfo == error_mark_node)
6873 return error_mark_node;
6875 return error_not_base_type (ttl, ttr);
6877 if (!at_least_as_qualified_p (ttl, ttr))
6880 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6881 rhstype, parmnum, fndecl);
6883 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6884 errtype, type, rhstype);
6888 /* Any non-function converts to a [const][volatile] void *
6889 and vice versa; otherwise, targets must be the same.
6890 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6891 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6892 || TYPE_MAIN_VARIANT (ttr) == void_type_node
6893 || (ctt = comp_target_types (type, rhstype, 1))
6894 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6895 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6897 /* ARM $4.8, commentary on p39. */
6898 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6899 && TREE_CODE (ttr) == OFFSET_TYPE)
6901 cp_error ("no standard conversion from `%T' to `void *'", ttr);
6902 return error_mark_node;
6905 if (ctt < 0 && TYPE_MAIN_VARIANT (ttl) != TYPE_MAIN_VARIANT (ttr))
6906 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6909 if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6910 && TYPE_MAIN_VARIANT (ttr) == void_type_node
6911 && ! null_ptr_cst_p (rhs))
6913 if (coder == RECORD_TYPE)
6914 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6917 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6920 /* Const and volatile mean something different for function types,
6921 so the usual warnings are not appropriate. */
6922 else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6923 || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6925 if (TREE_CODE (ttl) == OFFSET_TYPE
6926 && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6927 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6929 error ("%s between pointer to members converting across virtual baseclasses", errtype);
6930 return error_mark_node;
6932 else if (!at_least_as_qualified_p (ttl, ttr))
6934 if (string_conv_p (type, rhs, 1))
6935 /* converting from string constant to char *, OK. */;
6937 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6938 rhstype, parmnum, fndecl);
6940 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6941 errtype, type, rhstype);
6943 else if (TREE_CODE (ttl) == TREE_CODE (ttr)
6944 && ! comp_target_types (type, rhstype, 1))
6947 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6948 rhstype, parmnum, fndecl);
6950 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6951 errtype, type, rhstype);
6958 int drops_quals = 0;
6960 int unsigned_parity;
6963 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6964 for (; ; ttl = TREE_TYPE (ttl), ttr = TREE_TYPE (ttr))
6967 drops_quals |= !at_least_as_qualified_p (ttl, ttr);
6970 && !at_least_as_qualified_p (ttr, ttl))
6972 left_const &= TYPE_READONLY (ttl);
6974 if (TREE_CODE (ttl) != POINTER_TYPE
6975 || TREE_CODE (ttr) != POINTER_TYPE)
6978 unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6979 if (unsigned_parity)
6981 if (TREE_UNSIGNED (ttl))
6982 ttr = unsigned_type (ttr);
6984 ttl = unsigned_type (ttl);
6987 if (comp_target_types (ttl, ttr, nptrs) > 0)
6992 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6993 rhstype, parmnum, fndecl);
6995 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6996 errtype, type, rhstype);
7001 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
7002 rhstype, parmnum, fndecl);
7004 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
7005 errtype, type, rhstype);
7007 if (unsigned_parity > 0)
7010 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
7011 rhstype, parmnum, fndecl);
7013 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
7014 errtype, type, rhstype);
7016 else if (unsigned_parity < 0)
7019 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
7020 rhstype, parmnum, fndecl);
7022 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
7023 errtype, type, rhstype);
7026 /* C++ is not so friendly about converting function and
7027 member function pointers as C. Emit warnings here. */
7028 if (TREE_CODE (ttl) == FUNCTION_TYPE
7029 || TREE_CODE (ttl) == METHOD_TYPE)
7030 if (!same_or_base_type_p (ttl, ttr))
7032 warning ("conflicting function types in %s:", errtype);
7033 cp_warning ("\t`%T' != `%T'", type, rhstype);
7039 cp_error ("passing `%T' as argument %P of `%D'",
7040 rhstype, parmnum, fndecl);
7042 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
7043 return error_mark_node;
7046 return cp_convert (type, rhs);
7048 else if (codel == POINTER_TYPE
7049 && (coder == INTEGER_TYPE
7050 || coder == BOOLEAN_TYPE))
7052 /* An explicit constant 0 can convert to a pointer,
7053 but not a 0 that results from casting or folding. */
7054 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
7057 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
7058 rhstype, parmnum, fndecl);
7060 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
7061 errtype, type, rhstype);
7063 return cp_convert (type, rhs);
7065 else if (codel == INTEGER_TYPE
7066 && (coder == POINTER_TYPE
7067 || (coder == RECORD_TYPE
7068 && (IS_SIGNATURE_POINTER (rhstype)
7069 || TYPE_PTRMEMFUNC_FLAG (rhstype)
7070 || IS_SIGNATURE_REFERENCE (rhstype)))))
7073 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
7074 rhstype, parmnum, fndecl);
7076 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
7077 errtype, type, rhstype);
7078 return cp_convert (type, rhs);
7080 else if (codel == BOOLEAN_TYPE
7081 && (coder == POINTER_TYPE
7082 || (coder == RECORD_TYPE
7083 && (IS_SIGNATURE_POINTER (rhstype)
7084 || TYPE_PTRMEMFUNC_FLAG (rhstype)
7085 || IS_SIGNATURE_REFERENCE (rhstype)))))
7086 return cp_convert (type, rhs);
7089 else if (((coder == POINTER_TYPE
7090 && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
7091 || integer_zerop (rhs)
7092 || TYPE_PTRMEMFUNC_P (rhstype))
7093 && TYPE_PTRMEMFUNC_P (type))
7095 tree ttl = TYPE_PTRMEMFUNC_FN_TYPE (type);
7096 tree ttr = (TYPE_PTRMEMFUNC_P (rhstype)
7097 ? TYPE_PTRMEMFUNC_FN_TYPE (rhstype)
7099 int ctt = (TREE_CODE (rhstype) == INTEGER_TYPE ? 1
7100 : comp_target_types (ttl, ttr, 1));
7103 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
7106 cp_error ("%s to `%T' from `%T'", errtype, ttl, ttr);
7108 /* compatible pointer to member functions. */
7109 return build_ptrmemfunc (ttl, rhs, 0);
7111 else if (codel == ERROR_MARK || coder == ERROR_MARK)
7112 return error_mark_node;
7114 /* This should no longer happen. References are initialized via
7115 `convert_for_initialization'. They should otherwise be
7116 bashed before coming here. */
7117 else if (codel == REFERENCE_TYPE)
7118 my_friendly_abort (317);
7119 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
7121 tree nrhs = build1 (NOP_EXPR, type, rhs);
7122 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
7125 else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
7126 return cp_convert (type, rhs);
7127 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
7128 else if (TREE_CODE (type) == POINTER_TYPE
7129 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7130 || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
7132 && TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7133 return cp_convert (type, rhs);
7135 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
7136 return error_mark_node;
7139 /* Convert RHS to be of type TYPE.
7140 If EXP is non-zero, it is the target of the initialization.
7141 ERRTYPE is a string to use in error messages.
7143 Two major differences between the behavior of
7144 `convert_for_assignment' and `convert_for_initialization'
7145 are that references are bashed in the former, while
7146 copied in the latter, and aggregates are assigned in
7147 the former (operator=) while initialized in the
7150 If using constructor make sure no conversion operator exists, if one does
7151 exist, an ambiguity exists.
7153 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
7156 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
7157 tree exp, type, rhs;
7159 const char *errtype;
7163 register enum tree_code codel = TREE_CODE (type);
7164 register tree rhstype;
7165 register enum tree_code coder;
7167 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7168 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7169 if (TREE_CODE (rhs) == NOP_EXPR
7170 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7171 && codel != REFERENCE_TYPE)
7172 rhs = TREE_OPERAND (rhs, 0);
7174 if (rhs == error_mark_node
7175 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7176 return error_mark_node;
7178 if (TREE_CODE (rhs) == OFFSET_REF)
7180 rhs = resolve_offset_ref (rhs);
7181 if (rhs == error_mark_node)
7182 return error_mark_node;
7185 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
7186 rhs = convert_from_reference (rhs);
7188 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7189 && TREE_CODE (type) != ARRAY_TYPE
7190 && (TREE_CODE (type) != REFERENCE_TYPE
7191 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7192 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7193 && (TREE_CODE (type) != REFERENCE_TYPE
7194 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7195 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7196 rhs = default_conversion (rhs);
7198 rhstype = TREE_TYPE (rhs);
7199 coder = TREE_CODE (rhstype);
7201 if (coder == ERROR_MARK)
7202 return error_mark_node;
7204 /* We accept references to incomplete types, so we can
7205 return here before checking if RHS is of complete type. */
7207 if (codel == REFERENCE_TYPE)
7209 /* This should eventually happen in convert_arguments. */
7210 extern int warningcount, errorcount;
7211 int savew = 0, savee = 0;
7214 savew = warningcount, savee = errorcount;
7215 rhs = convert_to_reference (type, rhs, CONV_IMPLICIT, flags,
7216 exp ? exp : error_mark_node);
7219 if (warningcount > savew)
7220 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
7221 else if (errorcount > savee)
7222 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
7228 exp = require_complete_type (exp);
7229 if (exp == error_mark_node)
7230 return error_mark_node;
7232 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
7233 rhstype = TREE_TYPE (rhstype);
7235 type = complete_type (type);
7237 if (TYPE_LANG_SPECIFIC (type)
7238 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
7239 return build_signature_pointer_constructor (type, rhs);
7241 if (IS_AGGR_TYPE (type))
7242 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
7244 if (type == TREE_TYPE (rhs))
7246 /* Issue warnings about peculiar, but legal, uses of NULL. We
7247 do this *before* the call to decl_constant_value so as to
7248 avoid duplicate warnings on code like `const int I = NULL;
7250 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
7251 cp_warning ("converting NULL to non-pointer type");
7253 if (TREE_READONLY_DECL_P (rhs))
7254 rhs = decl_constant_value (rhs);
7259 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
7262 /* Expand an ASM statement with operands, handling output operands
7263 that are not variables or INDIRECT_REFS by transforming such
7264 cases into cases that expand_asm_operands can handle.
7266 Arguments are same as for expand_asm_operands.
7268 We don't do default conversions on all inputs, because it can screw
7269 up operands that are expected to be in memory. */
7272 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
7273 tree string, outputs, inputs, clobbers;
7278 int noutputs = list_length (outputs);
7280 /* o[I] is the place that output number I should be written. */
7281 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
7284 /* Record the contents of OUTPUTS before it is modified. */
7285 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7286 o[i] = TREE_VALUE (tail);
7288 /* Generate the ASM_OPERANDS insn;
7289 store into the TREE_VALUEs of OUTPUTS some trees for
7290 where the values were actually stored. */
7291 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
7293 /* Copy all the intermediate outputs into the specified outputs. */
7294 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7296 if (o[i] != TREE_VALUE (tail))
7298 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
7299 const0_rtx, VOIDmode, EXPAND_NORMAL);
7302 /* Detect modification of read-only values.
7303 (Otherwise done by build_modify_expr.) */
7306 tree type = TREE_TYPE (o[i]);
7307 if (CP_TYPE_CONST_P (type)
7308 || ((TREE_CODE (type) == RECORD_TYPE
7309 || TREE_CODE (type) == UNION_TYPE)
7310 && C_TYPE_FIELDS_READONLY (type)))
7311 readonly_error (o[i], "modification by `asm'", 1);
7315 /* Those MODIFY_EXPRs could do autoincrements. */
7319 /* Expand a C `return' statement.
7320 RETVAL is the expression for what to return,
7321 or a null pointer for `return;' with no value.
7323 C++: upon seeing a `return', we must call destructors on all
7324 variables in scope which had constructors called on them.
7325 This means that if in a destructor, the base class destructors
7326 must be called before returning.
7328 The RETURN statement in C++ has initialization semantics. */
7331 c_expand_return (retval)
7334 extern struct nesting *cond_stack, *loop_stack, *case_stack;
7335 extern tree dtor_label, ctor_label;
7336 tree result = DECL_RESULT (current_function_decl);
7337 tree valtype = TREE_TYPE (result);
7339 if (TREE_THIS_VOLATILE (current_function_decl))
7340 warning ("function declared `noreturn' has a `return' statement");
7342 if (retval == error_mark_node)
7344 current_function_returns_null = 1;
7348 if (processing_template_decl)
7350 add_tree (build_min_nt (RETURN_STMT, retval));
7357 error ("returning a value from a destructor");
7359 /* Can't just return from a destructor. */
7360 expand_goto (dtor_label);
7364 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
7365 if ((DECL_NAME (current_function_decl) == ansi_opname[(int) NEW_EXPR]
7366 || DECL_NAME (current_function_decl) == ansi_opname[(int) VEC_NEW_EXPR])
7367 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
7368 && null_ptr_cst_p (retval))
7369 cp_warning ("operator new should throw an exception, not return NULL");
7371 if (retval == NULL_TREE)
7373 /* A non-named return value does not count. */
7375 if (DECL_CONSTRUCTOR_P (current_function_decl))
7376 retval = current_class_ptr;
7377 else if (DECL_NAME (result) != NULL_TREE
7378 && TREE_CODE (valtype) != VOID_TYPE)
7382 current_function_returns_null = 1;
7384 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
7386 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
7388 pedwarn ("`return' with no value, in function returning non-void");
7389 /* Clear this, so finish_function won't say that we
7390 reach the end of a non-void function (which we don't,
7391 we gave a return!). */
7392 current_function_returns_null = 0;
7396 expand_null_return ();
7400 else if (DECL_CONSTRUCTOR_P (current_function_decl))
7402 if (flag_this_is_variable)
7403 error ("return from a constructor: use `this = ...' instead");
7405 error ("returning a value from a constructor");
7406 retval = current_class_ptr;
7409 /* Effective C++ rule 15. See also start_function. */
7411 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
7412 && retval != current_class_ref)
7413 cp_warning ("`operator=' should return a reference to `*this'");
7415 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
7417 current_function_returns_null = 1;
7418 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7419 pedwarn ("`return' with a value, in function returning void");
7420 expand_return (retval);
7424 /* Now deal with possible C++ hair:
7425 (1) Compute the return value.
7426 (2) If there are aggregate values with destructors which
7427 must be cleaned up, clean them (taking care
7428 not to clobber the return value).
7429 (3) If an X(X&) constructor is defined, the return
7430 value must be returned via that. */
7432 if (retval == result
7433 || DECL_CONSTRUCTOR_P (current_function_decl))
7434 /* It's already done for us. */;
7435 else if (TREE_CODE (TREE_TYPE (retval)) == VOID_TYPE)
7437 pedwarn ("return of void value in function returning non-void");
7438 expand_expr_stmt (retval);
7443 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
7445 /* First convert the value to the function's return type, then
7446 to the type of return value's location to handle the
7447 case that functype is thiner than the valtype. */
7449 retval = convert_for_initialization
7450 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
7451 "return", NULL_TREE, 0);
7453 retval = convert (valtype, retval);
7455 if (retval == error_mark_node)
7457 /* Avoid warning about control reaching end of function. */
7458 expand_null_return ();
7462 /* We can't initialize a register from a AGGR_INIT_EXPR. */
7463 else if (! current_function_returns_struct
7464 && TREE_CODE (retval) == TARGET_EXPR
7465 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
7466 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
7467 TREE_OPERAND (retval, 0));
7469 /* Add some useful error checking for C++. */
7470 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
7472 tree whats_returned;
7474 /* Sort through common things to see what it is
7475 we are returning. */
7476 whats_returned = retval;
7477 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7479 whats_returned = TREE_OPERAND (whats_returned, 1);
7480 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7481 whats_returned = TREE_OPERAND (whats_returned, 0);
7483 while (TREE_CODE (whats_returned) == CONVERT_EXPR
7484 || TREE_CODE (whats_returned) == NOP_EXPR)
7485 whats_returned = TREE_OPERAND (whats_returned, 0);
7486 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7488 whats_returned = TREE_OPERAND (whats_returned, 0);
7489 while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7490 || TREE_CODE (whats_returned) == TARGET_EXPR)
7492 /* Get the target. */
7493 whats_returned = TREE_OPERAND (whats_returned, 0);
7494 warning ("returning reference to temporary");
7498 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
7500 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
7501 warning ("reference to non-lvalue returned");
7502 else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
7503 && DECL_FUNCTION_SCOPE_P (whats_returned)
7504 && !(TREE_STATIC (whats_returned)
7505 || TREE_PUBLIC (whats_returned)))
7506 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
7509 else if (TREE_CODE (retval) == ADDR_EXPR)
7511 tree whats_returned = TREE_OPERAND (retval, 0);
7513 if (TREE_CODE (whats_returned) == VAR_DECL
7514 && DECL_NAME (whats_returned)
7515 && DECL_FUNCTION_SCOPE_P (whats_returned)
7516 && !(TREE_STATIC (whats_returned)
7517 || TREE_PUBLIC (whats_returned)))
7518 cp_warning_at ("address of local variable `%D' returned", whats_returned);
7522 if (retval != NULL_TREE
7523 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
7524 && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
7525 current_function_return_value = retval;
7527 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7529 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
7530 expand_goto (ctor_label);
7533 if (retval && retval != result)
7535 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7536 TREE_SIDE_EFFECTS (result) = 1;
7539 expand_start_target_temps ();
7541 expand_return (result);
7543 expand_end_target_temps ();
7545 current_function_returns_value = 1;
7548 /* Start a C switch statement, testing expression EXP.
7549 Return EXP if it is valid, an error node otherwise. */
7552 c_expand_start_case (exp)
7557 exp = build_expr_type_conversion (WANT_INT | WANT_ENUM, exp, 1);
7558 if (exp == NULL_TREE)
7560 error ("switch quantity not an integer");
7561 exp = error_mark_node;
7563 if (exp == error_mark_node)
7564 return error_mark_node;
7566 exp = default_conversion (exp);
7567 type = TREE_TYPE (exp);
7568 idx = get_unwidened (exp, 0);
7569 /* We can't strip a conversion from a signed type to an unsigned,
7570 because if we did, int_fits_type_p would do the wrong thing
7571 when checking case values for being in range,
7572 and it's too hard to do the right thing. */
7573 if (TREE_UNSIGNED (TREE_TYPE (exp)) == TREE_UNSIGNED (TREE_TYPE (idx)))
7577 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7578 type, "switch statement");
7583 /* Returns non-zero if the pointer-type FROM can be converted to the
7584 pointer-type TO via a qualification conversion. If CONSTP is -1,
7585 then we return non-zero if the pointers are similar, and the
7586 cv-qualification signature of FROM is a proper subset of that of TO.
7588 If CONSTP is positive, then all outer pointers have been
7592 comp_ptr_ttypes_real (to, from, constp)
7596 int to_more_cv_qualified = 0;
7598 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7600 if (TREE_CODE (to) != TREE_CODE (from))
7603 if (TREE_CODE (from) == OFFSET_TYPE
7604 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7605 TYPE_OFFSET_BASETYPE (to)))
7608 /* Const and volatile mean something different for function types,
7609 so the usual checks are not appropriate. */
7610 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7612 if (!at_least_as_qualified_p (to, from))
7615 if (!at_least_as_qualified_p (from, to))
7620 ++to_more_cv_qualified;
7624 constp &= TYPE_READONLY (to);
7627 if (TREE_CODE (to) != POINTER_TYPE)
7629 same_type_p (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from))
7630 && (constp >= 0 || to_more_cv_qualified);
7634 /* When comparing, say, char ** to char const **, this function takes the
7635 'char *' and 'char const *'. Do not pass non-pointer types to this
7639 comp_ptr_ttypes (to, from)
7642 return comp_ptr_ttypes_real (to, from, 1);
7645 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7646 type or inheritance-related types, regardless of cv-quals. */
7649 ptr_reasonably_similar (to, from)
7652 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7654 if (TREE_CODE (to) != TREE_CODE (from))
7657 if (TREE_CODE (from) == OFFSET_TYPE
7658 && comptypes (TYPE_OFFSET_BASETYPE (to),
7659 TYPE_OFFSET_BASETYPE (from),
7660 COMPARE_BASE | COMPARE_RELAXED))
7663 if (TREE_CODE (to) != POINTER_TYPE)
7665 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
7666 COMPARE_BASE | COMPARE_RELAXED);
7670 /* Like comp_ptr_ttypes, for const_cast. */
7673 comp_ptr_ttypes_const (to, from)
7676 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7678 if (TREE_CODE (to) != TREE_CODE (from))
7681 if (TREE_CODE (from) == OFFSET_TYPE
7682 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7683 TYPE_OFFSET_BASETYPE (to)))
7686 if (TREE_CODE (to) != POINTER_TYPE)
7687 return same_type_p (TYPE_MAIN_VARIANT (to),
7688 TYPE_MAIN_VARIANT (from));
7692 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7695 comp_ptr_ttypes_reinterpret (to, from)
7700 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7702 if (TREE_CODE (from) == OFFSET_TYPE)
7703 from = TREE_TYPE (from);
7704 if (TREE_CODE (to) == OFFSET_TYPE)
7705 to = TREE_TYPE (to);
7707 /* Const and volatile mean something different for function types,
7708 so the usual checks are not appropriate. */
7709 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7710 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7712 if (!at_least_as_qualified_p (to, from))
7716 && !at_least_as_qualified_p (from, to))
7718 constp &= TYPE_READONLY (to);
7721 if (TREE_CODE (from) != POINTER_TYPE
7722 || TREE_CODE (to) != POINTER_TYPE)
7727 /* Returns the type-qualifier set corresponding to TYPE. */
7730 cp_type_quals (type)
7733 while (TREE_CODE (type) == ARRAY_TYPE)
7734 type = TREE_TYPE (type);
7736 return TYPE_QUALS (type);
7739 /* Returns non-zero if the TYPE contains a mutable member */
7742 cp_has_mutable_p (type)
7745 while (TREE_CODE (type) == ARRAY_TYPE)
7746 type = TREE_TYPE (type);
7748 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);