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 qualify_type PROTO((tree, tree));
59 static tree get_delta_difference PROTO((tree, tree, int));
60 static int comp_cv_target_types PROTO((tree, tree, int));
62 /* Return the target type of TYPE, which meas return T for:
63 T*, T&, T[], T (...), and otherwise, just T. */
69 if (TREE_CODE (type) == REFERENCE_TYPE)
70 type = TREE_TYPE (type);
71 while (TREE_CODE (type) == POINTER_TYPE
72 || TREE_CODE (type) == ARRAY_TYPE
73 || TREE_CODE (type) == FUNCTION_TYPE
74 || TREE_CODE (type) == METHOD_TYPE
75 || TREE_CODE (type) == OFFSET_TYPE)
76 type = TREE_TYPE (type);
80 /* Do `exp = require_complete_type (exp);' to make sure exp
81 does not have an incomplete type. (That includes void types.)
82 Returns the error_mark_node if the VALUE does not have
83 complete type when this function returns. */
86 require_complete_type (value)
91 if (processing_template_decl || value == error_mark_node)
94 if (TREE_CODE (value) == OVERLOAD)
95 type = unknown_type_node;
97 type = TREE_TYPE (value);
99 /* First, detect a valid value with a complete type. */
100 if (TYPE_SIZE (type) != 0
101 && TYPE_SIZE (type) != size_zero_node
102 && ! (TYPE_LANG_SPECIFIC (type)
103 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
104 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
107 /* If we see X::Y, we build an OFFSET_TYPE which has
108 not been laid out. Try to avoid an error by interpreting
109 it as this->X::Y, if reasonable. */
110 if (TREE_CODE (value) == OFFSET_REF
111 && current_class_ref != 0
112 && TREE_OPERAND (value, 0) == current_class_ref)
114 tree base, member = TREE_OPERAND (value, 1);
115 tree basetype = TYPE_OFFSET_BASETYPE (type);
116 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
117 base = convert_pointer_to (basetype, current_class_ptr);
118 value = build (COMPONENT_REF, TREE_TYPE (member),
119 build_indirect_ref (base, NULL_PTR), member);
120 return require_complete_type (value);
123 if (complete_type_or_else (type, value))
126 return error_mark_node;
129 /* Makes sure EXPR is a complete type when used in a void context, like a
130 whole expression, or lhs of a comma operator. Issue a diagnostic and
131 return error_mark_node on failure. This is a little tricky, because some
132 valid void types look stunningly similar to invalid void types. We err on
133 the side of caution */
136 require_complete_type_in_void (expr)
139 switch (TREE_CODE (expr))
145 op = TREE_OPERAND (expr,2);
146 op = require_complete_type_in_void (op);
147 TREE_OPERAND (expr,2) = op;
148 if (op == error_mark_node)
161 op = TREE_OPERAND (expr,1);
162 op = require_complete_type_in_void (op);
163 TREE_OPERAND (expr,1) = op;
164 if (op == error_mark_node)
173 case NON_LVALUE_EXPR:
178 op = TREE_OPERAND (expr,0);
179 op = require_complete_type_in_void (op);
180 TREE_OPERAND (expr,0) = op;
181 if (op == error_mark_node)
189 case CALL_EXPR: /* function call return can be ignored */
190 case RTL_EXPR: /* RTL nodes have no value */
191 case DELETE_EXPR: /* delete expressions have no type */
192 case VEC_DELETE_EXPR:
193 case INTEGER_CST: /* used for null pointer */
194 case EXIT_EXPR: /* have no return */
195 case LOOP_EXPR: /* have no return */
196 case BIND_EXPR: /* have no return */
197 case THROW_EXPR: /* have no return */
198 case MODIFY_EXPR: /* sometimes this has a void type, but that's ok */
199 case CONVERT_EXPR: /* sometimes has a void type */
204 tree op = TREE_OPERAND (expr,0);
206 /* Calling a function returning a reference has an implicit
207 dereference applied. We don't want to make that an error. */
208 if (TREE_CODE (op) == CALL_EXPR
209 && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
211 /* else fallthrough */
215 expr = require_complete_type (expr);
222 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
223 a template instantiation, do the instantiation. Returns TYPE,
224 whether or not it could be completed, unless something goes
225 horribly wrong, in which case the error_mark_node is returned. */
231 if (type == NULL_TREE)
232 /* Rather than crash, we return something sure to cause an error
234 return error_mark_node;
236 if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
238 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
240 tree t = complete_type (TREE_TYPE (type));
241 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
243 TYPE_NEEDS_CONSTRUCTING (type)
244 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
245 TYPE_NEEDS_DESTRUCTOR (type)
246 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
248 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
249 instantiate_class_template (TYPE_MAIN_VARIANT (type));
254 /* Like complete_type, but issue an error if the TYPE cannot be
255 completed. VALUE is used for informative diagnostics.
256 Returns NULL_TREE if the type cannot be made complete. */
259 complete_type_or_else (type, value)
263 type = complete_type (type);
264 if (type == error_mark_node)
265 /* We already issued an error. */
267 else if (!TYPE_SIZE (type) || TYPE_SIZE (type) == size_zero_node)
269 incomplete_type_error (value, type);
276 /* Return truthvalue of whether type of EXP is instantiated. */
282 return (TREE_CODE (exp) == OVERLOAD
283 || TREE_CODE (exp) == TREE_LIST
284 || TREE_TYPE (exp) == unknown_type_node
285 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
286 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
289 /* Return truthvalue of whether T is function (or pfn) type. */
295 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
296 || (TREE_CODE (t) == POINTER_TYPE
297 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
298 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
301 /* Return a variant of TYPE which has all the type qualifiers of LIKE
302 as well as those of TYPE. */
305 qualify_type (type, like)
308 /* @@ Must do member pointers here. */
309 return cp_build_qualified_type (type, (CP_TYPE_QUALS (type)
310 | CP_TYPE_QUALS (like)));
313 /* Return the common type of two parameter lists.
314 We assume that comptypes has already been done and returned 1;
315 if that isn't so, this may crash.
317 As an optimization, free the space we allocate if the parameter
318 lists are already common. */
324 tree oldargs = p1, newargs, n;
327 char *first_obj = (char *) oballoc (0);
329 len = list_length (p1);
330 newargs = tree_last (p1);
332 if (newargs == void_list_node)
341 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
346 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
348 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
350 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
353 else if (! TREE_PURPOSE (p1))
355 if (TREE_PURPOSE (p2))
357 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
363 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
365 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
367 if (TREE_VALUE (p1) != TREE_VALUE (p2))
370 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
373 TREE_VALUE (n) = TREE_VALUE (p1);
384 /* Given a type, perhaps copied for a typedef,
385 find the "original" version of it. */
390 while (TYPE_NAME (t) != NULL_TREE)
392 tree x = TYPE_NAME (t);
393 if (TREE_CODE (x) != TYPE_DECL)
395 x = DECL_ORIGINAL_TYPE (x);
403 /* Return the common type of two types.
404 We assume that comptypes has already been done and returned 1;
405 if that isn't so, this may crash.
407 This is the type for the result of most arithmetic operations
408 if the operands have the given two types.
410 We do not deal with enumeral types here because they have already been
411 converted to integer types. */
417 register enum tree_code code1;
418 register enum tree_code code2;
421 /* Save time if the two types are the same. */
424 t1 = original_type (t1);
425 t2 = original_type (t2);
429 /* If one type is nonsense, use the other. */
430 if (t1 == error_mark_node)
432 if (t2 == error_mark_node)
435 /* Merge the attributes. */
436 attributes = merge_machine_type_attributes (t1, t2);
438 { register tree a1, a2;
439 a1 = TYPE_ATTRIBUTES (t1);
440 a2 = TYPE_ATTRIBUTES (t2);
442 /* Either one unset? Take the set one. */
444 if (!(attributes = a1))
447 /* One that completely contains the other? Take it. */
449 else if (a2 && !attribute_list_contained (a1, a2))
451 if (attribute_list_contained (a2, a1))
455 /* Pick the longest list, and hang on the other list. */
456 /* ??? For the moment we punt on the issue of attrs with args. */
458 if (list_length (a1) < list_length (a2))
459 attributes = a2, a2 = a1;
461 for (; a2; a2 = TREE_CHAIN (a2))
462 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
463 attributes) == NULL_TREE)
466 TREE_CHAIN (a1) = attributes;
473 /* Treat an enum type as the unsigned integer type of the same width. */
475 if (TREE_CODE (t1) == ENUMERAL_TYPE)
476 t1 = type_for_size (TYPE_PRECISION (t1), 1);
477 if (TREE_CODE (t2) == ENUMERAL_TYPE)
478 t2 = type_for_size (TYPE_PRECISION (t2), 1);
480 if (TYPE_PTRMEMFUNC_P (t1))
481 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
482 if (TYPE_PTRMEMFUNC_P (t2))
483 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
485 code1 = TREE_CODE (t1);
486 code2 = TREE_CODE (t2);
488 /* If one type is complex, form the common type of the non-complex
489 components, then make that complex. Use T1 or T2 if it is the
491 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
493 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
494 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
495 tree subtype = common_type (subtype1, subtype2);
497 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
498 return build_type_attribute_variant (t1, attributes);
499 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
500 return build_type_attribute_variant (t2, attributes);
502 return build_type_attribute_variant (build_complex_type (subtype),
510 /* If only one is real, use it as the result. */
512 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
513 return build_type_attribute_variant (t1, attributes);
515 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
516 return build_type_attribute_variant (t2, attributes);
518 /* Both real or both integers; use the one with greater precision. */
520 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
521 return build_type_attribute_variant (t1, attributes);
522 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
523 return build_type_attribute_variant (t2, attributes);
525 /* Same precision. Prefer longs to ints even when same size. */
527 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
528 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
529 return build_type_attribute_variant (long_unsigned_type_node,
532 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
533 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
535 /* But preserve unsignedness from the other type,
536 since long cannot hold all the values of an unsigned int. */
537 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
538 t1 = long_unsigned_type_node;
540 t1 = long_integer_type_node;
541 return build_type_attribute_variant (t1, attributes);
544 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
545 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
546 return build_type_attribute_variant (long_double_type_node,
549 /* Otherwise prefer the unsigned one. */
551 if (TREE_UNSIGNED (t1))
552 return build_type_attribute_variant (t1, attributes);
554 return build_type_attribute_variant (t2, attributes);
558 /* For two pointers, do this recursively on the target type,
559 and combine the qualifiers of the two types' targets. */
560 /* This code was turned off; I don't know why.
561 But ANSI C++ specifies doing this with the qualifiers.
562 So I turned it on again. */
564 tree tt1 = TREE_TYPE (t1);
565 tree tt2 = TREE_TYPE (t2);
570 if (TREE_CODE (tt1) == OFFSET_TYPE)
572 b1 = TYPE_OFFSET_BASETYPE (tt1);
573 b2 = TYPE_OFFSET_BASETYPE (tt2);
574 tt1 = TREE_TYPE (tt1);
575 tt2 = TREE_TYPE (tt2);
580 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
581 tt1 = TYPE_MAIN_VARIANT (tt1);
582 tt2 = TYPE_MAIN_VARIANT (tt2);
588 compiler_error ("common_type called with uncommon member types");
591 else if (tt1 == void_type_node || tt2 == void_type_node)
592 target = void_type_node;
593 else if (tt1 == unknown_type_node)
595 else if (tt2 == unknown_type_node)
598 target = common_type (tt1, tt2);
600 target = cp_build_qualified_type (target, type_quals);
604 if (same_type_p (b1, b2)
605 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
606 target = build_offset_type (b2, target);
607 else if (binfo_or_else (b2, b1))
608 target = build_offset_type (b1, target);
611 if (code1 == POINTER_TYPE)
612 t1 = build_pointer_type (target);
614 t1 = build_reference_type (target);
615 t1 = build_type_attribute_variant (t1, attributes);
617 if (TREE_CODE (target) == METHOD_TYPE)
618 t1 = build_ptrmemfunc_type (t1);
625 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
626 /* Save space: see if the result is identical to one of the args. */
627 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
628 return build_type_attribute_variant (t1, attributes);
629 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
630 return build_type_attribute_variant (t2, attributes);
631 /* Merge the element types, and have a size if either arg has one. */
632 t1 = build_cplus_array_type
633 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
634 return build_type_attribute_variant (t1, attributes);
638 /* Function types: prefer the one that specified arg types.
639 If both do, merge the arg types. Also merge the return types. */
641 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
642 tree p1 = TYPE_ARG_TYPES (t1);
643 tree p2 = TYPE_ARG_TYPES (t2);
646 /* Save space: see if the result is identical to one of the args. */
647 if (valtype == TREE_TYPE (t1) && ! p2)
648 return build_type_attribute_variant (t1, attributes);
649 if (valtype == TREE_TYPE (t2) && ! p1)
650 return build_type_attribute_variant (t2, attributes);
652 /* Simple way if one arg fails to specify argument types. */
653 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
655 rval = build_function_type (valtype, p2);
656 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
657 rval = build_exception_variant (rval, raises);
658 return build_type_attribute_variant (rval, attributes);
660 raises = TYPE_RAISES_EXCEPTIONS (t1);
661 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
663 rval = build_function_type (valtype, p1);
665 rval = build_exception_variant (rval, raises);
666 return build_type_attribute_variant (rval, attributes);
669 rval = build_function_type (valtype, commonparms (p1, p2));
670 rval = build_exception_variant (rval, raises);
671 return build_type_attribute_variant (rval, attributes);
676 t1 = TYPE_MAIN_VARIANT (t1);
677 t2 = TYPE_MAIN_VARIANT (t2);
679 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
680 return build_type_attribute_variant (t1, attributes);
681 else if (binfo_or_else (t2, t1))
682 return build_type_attribute_variant (t2, attributes);
685 compiler_error ("common_type called with uncommon aggregate types");
686 return error_mark_node;
690 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
692 /* Get this value the long way, since TYPE_METHOD_BASETYPE
693 is just the main variant of this. */
697 tree b1 = TYPE_OFFSET_BASETYPE (t1);
698 tree b2 = TYPE_OFFSET_BASETYPE (t2);
700 if (same_type_p (b1, b2)
701 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
702 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
705 if (binfo_or_else (b2, b1) == NULL_TREE)
706 compiler_error ("common_type called with uncommon method types");
707 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
710 raises = TYPE_RAISES_EXCEPTIONS (t1);
712 /* If this was a member function type, get back to the
713 original type of type member function (i.e., without
714 the class instance variable up front. */
715 t1 = build_function_type (TREE_TYPE (t1),
716 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
717 t2 = build_function_type (TREE_TYPE (t2),
718 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
719 t3 = common_type (t1, t2);
720 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
721 TYPE_ARG_TYPES (t3));
722 t1 = build_exception_variant (t3, raises);
725 compiler_error ("common_type called with uncommon method types");
727 return build_type_attribute_variant (t1, attributes);
730 /* Pointers to members should now be handled by the POINTER_TYPE
732 my_friendly_abort (990325);
735 return build_type_attribute_variant (t1, attributes);
739 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
742 compexcepttypes (t1, t2)
745 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
748 /* Compare the array types T1 and T2, using CMP as the type comparison
749 function for the element types. STRICT is as for comptypes. */
752 comp_array_types (cmp, t1, t2, strict)
753 register int (*cmp) PROTO((tree, tree, int));
763 /* The type of the array elements must be the same. */
764 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
765 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
766 strict & ~COMPARE_REDECLARATION)))
769 d1 = TYPE_DOMAIN (t1);
770 d2 = TYPE_DOMAIN (t2);
775 /* If one of the arrays is dimensionless, and the other has a
776 dimension, they are of different types. However, it is legal to
784 declarations for an array object can specify
785 array types that differ by the presence or absence of a major
786 array bound (_dcl.array_). */
788 return strict & COMPARE_REDECLARATION;
790 /* Check that the dimensions are the same. */
791 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
793 && cp_tree_equal (TYPE_MAX_VALUE (d1),
794 TYPE_MAX_VALUE (d2)));
797 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
798 or various other operations. STRICT is a bitwise-or of the
802 comptypes (type1, type2, strict)
806 register tree t1 = type1;
807 register tree t2 = type2;
809 int orig_strict = strict;
811 /* The special exemption for redeclaring array types without an
812 array bound only applies at the top level:
817 is not legal, for example. */
818 strict &= ~COMPARE_REDECLARATION;
820 /* Suppress errors caused by previously reported errors */
824 /* This should never happen. */
825 my_friendly_assert (t1 != error_mark_node, 307);
827 if (t2 == error_mark_node)
830 if (strict & COMPARE_RELAXED)
832 /* Treat an enum type as the unsigned integer type of the same width. */
834 if (TREE_CODE (t1) == ENUMERAL_TYPE)
835 t1 = type_for_size (TYPE_PRECISION (t1), 1);
836 if (TREE_CODE (t2) == ENUMERAL_TYPE)
837 t2 = type_for_size (TYPE_PRECISION (t2), 1);
843 if (TYPE_PTRMEMFUNC_P (t1))
844 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
845 if (TYPE_PTRMEMFUNC_P (t2))
846 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
848 /* Different classes of types can't be compatible. */
849 if (TREE_CODE (t1) != TREE_CODE (t2))
852 /* Qualifiers must match. */
853 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
855 if (strict == COMPARE_STRICT
856 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
859 /* Allow for two different type nodes which have essentially the same
860 definition. Note that we already checked for equality of the type
861 qualifiers (just above). */
863 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
866 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
867 attribute is its own main variant (`val' will remain 0). */
868 #ifndef COMP_TYPE_ATTRIBUTES
869 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
872 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
873 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
876 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
879 switch (TREE_CODE (t1))
881 case TEMPLATE_TEMPLATE_PARM:
882 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
883 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
885 if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1)),
886 DECL_TEMPLATE_PARMS (TYPE_NAME (t2))))
888 if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1)
889 && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2))
891 /* Don't check inheritance. */
892 strict = COMPARE_STRICT;
897 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
898 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
899 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
900 val = comp_template_args (TYPE_TI_ARGS (t1),
903 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
908 if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
916 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
917 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
918 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
922 if (! compexcepttypes (t1, t2))
925 /* This case is anti-symmetrical!
926 One can pass a base member (or member function)
927 to something expecting a derived member (or member function),
928 but not vice-versa! */
930 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
931 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
938 /* first, check whether the referred types match with the
939 required level of strictness */
940 val = comptypes (t1, t2, strict);
943 if (TREE_CODE (t1) == RECORD_TYPE
944 && TREE_CODE (t2) == RECORD_TYPE)
949 if (! compexcepttypes (t1, t2))
952 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
953 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
954 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
958 /* Target types must match incl. qualifiers. We use ORIG_STRICT
959 here since this is the one place where
960 COMPARE_REDECLARATION should be used. */
961 val = comp_array_types (comptypes, t1, t2, orig_strict);
964 case TEMPLATE_TYPE_PARM:
965 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
966 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
969 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
971 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
976 return attrval == 2 && val == 1 ? 2 : val;
979 /* Subroutine of comp_target-types. Make sure that the cv-quals change
980 only in the same direction as the target type. */
983 comp_cv_target_types (ttl, ttr, nptrs)
989 if (!at_least_as_qualified_p (ttl, ttr)
990 && !at_least_as_qualified_p (ttr, ttl))
991 /* The qualifications are incomparable. */
994 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
995 return more_qualified_p (ttr, ttl) ? -1 : 1;
997 t = comp_target_types (ttl, ttr, nptrs);
998 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
999 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1005 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1006 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1007 converted to TTL. Return -1 means that TTL can be converted to TTR but
1010 NPTRS is the number of pointers we can strip off and keep cool.
1011 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1012 but to not permit B** to convert to A**.
1014 This should go away. Callers should use can_convert or something
1015 similar instead. (jason 17 Apr 1997) */
1018 comp_target_types (ttl, ttr, nptrs)
1022 ttl = TYPE_MAIN_VARIANT (ttl);
1023 ttr = TYPE_MAIN_VARIANT (ttr);
1027 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1030 if ((TREE_CODE (ttr) == POINTER_TYPE
1031 || TREE_CODE (ttr) == REFERENCE_TYPE)
1032 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1033 of the type pointed to. This is necessary for reference init
1034 semantics. We won't get here from a previous call with nptrs == 1;
1035 for multi-level pointers we end up in comp_ptr_ttypes. */
1038 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1040 ttl = TREE_TYPE (ttl);
1041 ttr = TREE_TYPE (ttr);
1045 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1046 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1048 else if (TREE_CODE (ttl) == VOID_TYPE
1049 && TREE_CODE (ttr) != FUNCTION_TYPE
1050 && TREE_CODE (ttr) != METHOD_TYPE
1051 && TREE_CODE (ttr) != OFFSET_TYPE)
1053 else if (TREE_CODE (ttr) == VOID_TYPE
1054 && TREE_CODE (ttl) != FUNCTION_TYPE
1055 && TREE_CODE (ttl) != METHOD_TYPE
1056 && TREE_CODE (ttl) != OFFSET_TYPE)
1058 else if (TREE_CODE (ttl) == POINTER_TYPE
1059 || TREE_CODE (ttl) == ARRAY_TYPE)
1061 if (comp_ptr_ttypes (ttl, ttr))
1063 else if (comp_ptr_ttypes (ttr, ttl))
1069 /* Const and volatile mean something different for function types,
1070 so the usual checks are not appropriate. */
1071 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1072 return comp_target_types (ttl, ttr, nptrs - 1);
1074 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1077 if (TREE_CODE (ttr) == ARRAY_TYPE)
1078 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1079 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1086 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1091 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1100 argsl = TYPE_ARG_TYPES (ttl);
1101 argsr = TYPE_ARG_TYPES (ttr);
1103 /* Compare 'this' here, not in comp_target_parms. */
1104 if (TREE_CODE (ttr) == METHOD_TYPE)
1106 tree tl = TYPE_METHOD_BASETYPE (ttl);
1107 tree tr = TYPE_METHOD_BASETYPE (ttr);
1109 if (!same_or_base_type_p (tr, tl))
1111 if (same_or_base_type_p (tl, tr))
1117 argsl = TREE_CHAIN (argsl);
1118 argsr = TREE_CHAIN (argsr);
1121 switch (comp_target_parms (argsl, argsr, 1))
1129 return saw_contra ? -1 : 1;
1132 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1136 /* Contravariance: we can assign a pointer to base member to a pointer
1137 to derived member. Note difference from simple pointer case, where
1138 we can pass a pointer to derived to a pointer to base. */
1139 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1140 TYPE_OFFSET_BASETYPE (ttl)))
1142 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1143 TYPE_OFFSET_BASETYPE (ttr)))
1153 ttl = TREE_TYPE (ttl);
1154 ttr = TREE_TYPE (ttr);
1156 if (TREE_CODE (ttl) == POINTER_TYPE
1157 || TREE_CODE (ttl) == ARRAY_TYPE)
1159 if (comp_ptr_ttypes (ttl, ttr))
1165 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1170 else if (IS_AGGR_TYPE (ttl))
1174 if (same_or_base_type_p (build_pointer_type (ttl),
1175 build_pointer_type (ttr)))
1177 if (same_or_base_type_p (build_pointer_type (ttr),
1178 build_pointer_type (ttl)))
1186 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1189 at_least_as_qualified_p (type1, type2)
1193 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1194 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1195 == CP_TYPE_QUALS (type2));
1198 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1201 more_qualified_p (type1, type2)
1205 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1206 && at_least_as_qualified_p (type1, type2));
1209 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1210 more cv-qualified that TYPE1, and 0 otherwise. */
1213 comp_cv_qualification (type1, type2)
1217 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1220 if (at_least_as_qualified_p (type1, type2))
1223 else if (at_least_as_qualified_p (type2, type1))
1229 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1230 subset of the cv-qualification signature of TYPE2, and the types
1231 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1234 comp_cv_qual_signature (type1, type2)
1238 if (comp_ptr_ttypes_real (type2, type1, -1))
1240 else if (comp_ptr_ttypes_real (type1, type2, -1))
1246 /* If two types share a common base type, return that basetype.
1247 If there is not a unique most-derived base type, this function
1248 returns ERROR_MARK_NODE. */
1251 common_base_type (tt1, tt2)
1254 tree best = NULL_TREE;
1257 /* If one is a baseclass of another, that's good enough. */
1258 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1260 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1263 /* Otherwise, try to find a unique baseclass of TT1
1264 that is shared by TT2, and follow that down. */
1265 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1267 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1268 tree trial = common_base_type (basetype, tt2);
1271 if (trial == error_mark_node)
1273 if (best == NULL_TREE)
1275 else if (best != trial)
1276 return error_mark_node;
1281 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1283 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1284 tree trial = common_base_type (tt1, basetype);
1287 if (trial == error_mark_node)
1289 if (best == NULL_TREE)
1291 else if (best != trial)
1292 return error_mark_node;
1298 /* Subroutines of `comptypes'. */
1300 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1301 equivalent in the sense that functions with those parameter types
1302 can have equivalent types. The two lists must be equivalent,
1305 C++: See comment above about TYPE1, TYPE2. */
1308 compparms (parms1, parms2)
1309 tree parms1, parms2;
1311 register tree t1 = parms1, t2 = parms2;
1313 /* An unspecified parmlist matches any specified parmlist
1314 whose argument types don't need default promotions. */
1318 if (t1 == 0 && t2 == 0)
1320 /* If one parmlist is shorter than the other,
1321 they fail to match. */
1322 if (t1 == 0 || t2 == 0)
1324 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1327 t1 = TREE_CHAIN (t1);
1328 t2 = TREE_CHAIN (t2);
1332 /* This really wants return whether or not parameter type lists
1333 would make their owning functions assignment compatible or not.
1335 The return value is like for comp_target_types.
1337 This should go away, possibly with the exception of the empty parmlist
1338 conversion; there are no conversions between function types in C++.
1339 (jason 17 Apr 1997) */
1342 comp_target_parms (parms1, parms2, strict)
1343 tree parms1, parms2;
1346 register tree t1 = parms1, t2 = parms2;
1347 int warn_contravariance = 0;
1349 /* In C, an unspecified parmlist matches any specified parmlist
1350 whose argument types don't need default promotions. This is not
1351 true for C++, but let's do it anyway for unfixed headers. */
1353 if (t1 == 0 && t2 != 0)
1355 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1357 return self_promoting_args_p (t2);
1360 return self_promoting_args_p (t1);
1362 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1366 /* If one parmlist is shorter than the other,
1367 they fail to match, unless STRICT is <= 0. */
1368 if (t1 == 0 || t2 == 0)
1373 return 1 + warn_contravariance;
1374 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1376 p1 = TREE_VALUE (t1);
1377 p2 = TREE_VALUE (t2);
1378 if (same_type_p (p1, p2))
1384 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1385 || (TREE_CODE (p1) == REFERENCE_TYPE
1386 && TREE_CODE (p2) == REFERENCE_TYPE))
1389 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1390 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1393 /* The following is wrong for contravariance,
1394 but many programs depend on it. */
1395 if (TREE_TYPE (p1) == void_type_node)
1397 if (TREE_TYPE (p2) == void_type_node)
1399 warn_contravariance = 1;
1402 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1403 && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (p1)),
1404 TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1407 /* Note backwards order due to contravariance. */
1408 if (comp_target_types (p2, p1, 1) <= 0)
1410 if (comp_target_types (p1, p2, 1) > 0)
1412 warn_contravariance = 1;
1419 return warn_contravariance ? -1 : 1;
1422 /* Return 1 if PARMS specifies a fixed number of parameters
1423 and none of their types is affected by default promotions. */
1426 self_promoting_args_p (parms)
1430 for (t = parms; t; t = TREE_CHAIN (t))
1432 register tree type = TREE_VALUE (t);
1434 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1440 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1443 if (C_PROMOTING_INTEGER_TYPE_P (type))
1449 /* Return an unsigned type the same as TYPE in other respects.
1451 C++: must make these work for type variants as well. */
1454 unsigned_type (type)
1457 tree type1 = TYPE_MAIN_VARIANT (type);
1458 if (type1 == signed_char_type_node || type1 == char_type_node)
1459 return unsigned_char_type_node;
1460 if (type1 == integer_type_node)
1461 return unsigned_type_node;
1462 if (type1 == short_integer_type_node)
1463 return short_unsigned_type_node;
1464 if (type1 == long_integer_type_node)
1465 return long_unsigned_type_node;
1466 if (type1 == long_long_integer_type_node)
1467 return long_long_unsigned_type_node;
1468 #if HOST_BITS_PER_WIDE_INT >= 64
1469 if (type1 == intTI_type_node)
1470 return unsigned_intTI_type_node;
1472 if (type1 == intDI_type_node)
1473 return unsigned_intDI_type_node;
1474 if (type1 == intSI_type_node)
1475 return unsigned_intSI_type_node;
1476 if (type1 == intHI_type_node)
1477 return unsigned_intHI_type_node;
1478 if (type1 == intQI_type_node)
1479 return unsigned_intQI_type_node;
1481 return signed_or_unsigned_type (1, type);
1484 /* Return a signed type the same as TYPE in other respects. */
1490 tree type1 = TYPE_MAIN_VARIANT (type);
1491 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1492 return signed_char_type_node;
1493 if (type1 == unsigned_type_node)
1494 return integer_type_node;
1495 if (type1 == short_unsigned_type_node)
1496 return short_integer_type_node;
1497 if (type1 == long_unsigned_type_node)
1498 return long_integer_type_node;
1499 if (type1 == long_long_unsigned_type_node)
1500 return long_long_integer_type_node;
1501 #if HOST_BITS_PER_WIDE_INT >= 64
1502 if (type1 == unsigned_intTI_type_node)
1503 return intTI_type_node;
1505 if (type1 == unsigned_intDI_type_node)
1506 return intDI_type_node;
1507 if (type1 == unsigned_intSI_type_node)
1508 return intSI_type_node;
1509 if (type1 == unsigned_intHI_type_node)
1510 return intHI_type_node;
1511 if (type1 == unsigned_intQI_type_node)
1512 return intQI_type_node;
1514 return signed_or_unsigned_type (0, type);
1517 /* Return a type the same as TYPE except unsigned or
1518 signed according to UNSIGNEDP. */
1521 signed_or_unsigned_type (unsignedp, type)
1525 if (! INTEGRAL_TYPE_P (type)
1526 || TREE_UNSIGNED (type) == unsignedp)
1529 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1530 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1531 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1532 return unsignedp ? unsigned_type_node : integer_type_node;
1533 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1534 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1535 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1536 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1537 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1538 return (unsignedp ? long_long_unsigned_type_node
1539 : long_long_integer_type_node);
1543 /* Compute the value of the `sizeof' operator. */
1549 enum tree_code code = TREE_CODE (type);
1552 if (processing_template_decl)
1553 return build_min (SIZEOF_EXPR, sizetype, type);
1555 if (code == FUNCTION_TYPE)
1557 if (pedantic || warn_pointer_arith)
1558 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1559 return size_int (1);
1561 if (code == METHOD_TYPE)
1563 if (pedantic || warn_pointer_arith)
1564 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1565 return size_int (1);
1567 if (code == VOID_TYPE)
1569 if (pedantic || warn_pointer_arith)
1570 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1571 return size_int (1);
1573 if (code == ERROR_MARK)
1574 return size_int (1);
1576 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1577 referenced object.'' */
1578 if (code == REFERENCE_TYPE)
1579 type = TREE_TYPE (type);
1581 /* We couldn't find anything in the ARM or the draft standard that says,
1582 one way or the other, if doing sizeof on something that doesn't have
1583 an object associated with it is correct or incorrect. For example, if
1584 you declare `struct S { char str[16]; };', and in your program do
1585 a `sizeof (S::str)', should we flag that as an error or should we give
1586 the size of it? Since it seems like a reasonable thing to do, we'll go
1587 with giving the value. */
1588 if (code == OFFSET_TYPE)
1589 type = TREE_TYPE (type);
1591 /* @@ This also produces an error for a signature ref.
1592 In that case we should be able to do better. */
1593 if (IS_SIGNATURE (type))
1595 error ("`sizeof' applied to a signature type");
1596 return size_int (0);
1599 if (TYPE_SIZE (complete_type (type)) == 0)
1601 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1602 return size_int (0);
1605 /* Convert in case a char is more than one unit. */
1606 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1607 size_int (TYPE_PRECISION (char_type_node)));
1608 t = convert (sizetype, t);
1609 /* size_binop does not put the constant in range, so do it now. */
1610 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1611 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1619 if (processing_template_decl)
1620 return build_min (SIZEOF_EXPR, sizetype, e);
1622 if (TREE_CODE (e) == COMPONENT_REF
1623 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1624 error ("sizeof applied to a bit-field");
1625 /* ANSI says arrays and functions are converted inside comma.
1626 But we can't really convert them in build_compound_expr
1627 because that would break commas in lvalues.
1628 So do the conversion here if operand was a comma. */
1629 if (TREE_CODE (e) == COMPOUND_EXPR
1630 && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1631 || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1632 e = default_conversion (e);
1633 else if (is_overloaded_fn (e))
1635 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1636 return size_int (1);
1639 return c_sizeof (TREE_TYPE (e));
1643 c_sizeof_nowarn (type)
1646 enum tree_code code = TREE_CODE (type);
1649 if (code == FUNCTION_TYPE
1650 || code == METHOD_TYPE
1651 || code == VOID_TYPE
1652 || code == ERROR_MARK)
1653 return size_int (1);
1654 if (code == REFERENCE_TYPE)
1655 type = TREE_TYPE (type);
1657 if (TYPE_SIZE (type) == 0)
1658 return size_int (0);
1660 /* Convert in case a char is more than one unit. */
1661 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1662 size_int (TYPE_PRECISION (char_type_node)));
1663 t = convert (sizetype, t);
1664 force_fit_type (t, 0);
1668 /* Implement the __alignof keyword: Return the minimum required
1669 alignment of TYPE, measured in bytes. */
1675 enum tree_code code = TREE_CODE (type);
1678 if (processing_template_decl)
1679 return build_min (ALIGNOF_EXPR, sizetype, type);
1681 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1682 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1684 if (code == VOID_TYPE || code == ERROR_MARK)
1685 return size_int (1);
1687 /* C++: this is really correct! */
1688 if (code == REFERENCE_TYPE)
1689 type = TREE_TYPE (type);
1691 /* @@ This also produces an error for a signature ref.
1692 In that case we should be able to do better. */
1693 if (IS_SIGNATURE (type))
1695 error ("`__alignof' applied to a signature type");
1696 return size_int (1);
1699 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1700 force_fit_type (t, 0);
1704 /* Perform the array-to-pointer and function-to-pointer conversions
1707 In addition, references are converted to rvalues and manifest
1708 constants are replaced by their values. */
1711 decay_conversion (exp)
1714 register tree type = TREE_TYPE (exp);
1715 register enum tree_code code = TREE_CODE (type);
1717 if (code == OFFSET_TYPE)
1719 if (TREE_CODE (exp) == OFFSET_REF)
1720 return decay_conversion (resolve_offset_ref (exp));
1722 type = TREE_TYPE (type);
1723 code = TREE_CODE (type);
1725 if (type == unknown_type_node)
1727 cp_pedwarn ("assuming & on overloaded member function");
1728 return build_unary_op (ADDR_EXPR, exp, 0);
1732 if (code == REFERENCE_TYPE)
1734 exp = convert_from_reference (exp);
1735 type = TREE_TYPE (exp);
1736 code = TREE_CODE (type);
1739 /* Constants can be used directly unless they're not loadable. */
1740 if (TREE_CODE (exp) == CONST_DECL)
1741 exp = DECL_INITIAL (exp);
1742 /* Replace a nonvolatile const static variable with its value. We
1743 don't do this for arrays, though; we want the address of the
1744 first element of the array, not the address of the first element
1745 of its initializing constant. We *do* replace variables that the
1746 user isn't really supposed to know about; this is a hack to deal
1747 with __PRETTY_FUNCTION__ and the like. */
1748 else if (TREE_READONLY_DECL_P (exp)
1749 && (code != ARRAY_TYPE
1750 || (TREE_CODE (exp) == VAR_DECL && DECL_IGNORED_P (exp))))
1752 exp = decl_constant_value (exp);
1753 type = TREE_TYPE (exp);
1756 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1757 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1759 if (code == VOID_TYPE)
1761 error ("void value not ignored as it ought to be");
1762 return error_mark_node;
1764 if (code == METHOD_TYPE)
1766 cp_pedwarn ("assuming & on `%E'", exp);
1767 return build_unary_op (ADDR_EXPR, exp, 0);
1769 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1770 return build_unary_op (ADDR_EXPR, exp, 0);
1771 if (code == ARRAY_TYPE)
1776 if (TREE_CODE (exp) == INDIRECT_REF)
1778 /* Stripping away the INDIRECT_REF is not the right
1779 thing to do for references... */
1780 tree inner = TREE_OPERAND (exp, 0);
1781 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1783 inner = build1 (CONVERT_EXPR,
1784 build_pointer_type (TREE_TYPE
1785 (TREE_TYPE (inner))),
1787 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1789 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1792 if (TREE_CODE (exp) == COMPOUND_EXPR)
1794 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1795 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1796 TREE_OPERAND (exp, 0), op1);
1800 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1802 error ("invalid use of non-lvalue array");
1803 return error_mark_node;
1806 ptrtype = build_pointer_type (TREE_TYPE (type));
1808 if (TREE_CODE (exp) == VAR_DECL)
1810 /* ??? This is not really quite correct
1811 in that the type of the operand of ADDR_EXPR
1812 is not the target type of the type of the ADDR_EXPR itself.
1813 Question is, can this lossage be avoided? */
1814 adr = build1 (ADDR_EXPR, ptrtype, exp);
1815 if (mark_addressable (exp) == 0)
1816 return error_mark_node;
1817 TREE_CONSTANT (adr) = staticp (exp);
1818 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1821 /* This way is better for a COMPONENT_REF since it can
1822 simplify the offset for a component. */
1823 adr = build_unary_op (ADDR_EXPR, exp, 1);
1824 return cp_convert (ptrtype, adr);
1831 default_conversion (exp)
1835 enum tree_code code;
1837 exp = decay_conversion (exp);
1839 type = TREE_TYPE (exp);
1840 code = TREE_CODE (type);
1842 if (INTEGRAL_CODE_P (code))
1844 tree t = type_promotes_to (type);
1846 return cp_convert (t, exp);
1852 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1856 inline_conversion (exp)
1859 if (TREE_CODE (exp) == FUNCTION_DECL)
1860 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1865 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1866 decay_conversion to one. */
1869 string_conv_p (totype, exp, warn)
1875 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1878 t = TREE_TYPE (totype);
1879 if (!same_type_p (t, char_type_node)
1880 && !same_type_p (t, wchar_type_node))
1883 if (TREE_CODE (exp) == STRING_CST)
1885 /* Make sure that we don't try to convert between char and wchar_t. */
1886 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))) != t)
1891 /* Is this a string constant which has decayed to 'const char *'? */
1892 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1893 if (!same_type_p (TREE_TYPE (exp), t))
1896 if (TREE_CODE (exp) != ADDR_EXPR
1897 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1901 /* This warning is not very useful, as it complains about printf. */
1902 if (warn && warn_write_strings)
1903 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1909 build_object_ref (datum, basetype, field)
1910 tree datum, basetype, field;
1913 if (datum == error_mark_node)
1914 return error_mark_node;
1916 dtype = TREE_TYPE (datum);
1917 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1918 dtype = TREE_TYPE (dtype);
1919 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1921 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1922 basetype, field, dtype);
1923 return error_mark_node;
1925 else if (IS_SIGNATURE (basetype))
1927 warning ("signature name in scope resolution ignored");
1928 return build_component_ref (datum, field, NULL_TREE, 1);
1930 else if (is_aggr_type (basetype, 1))
1932 tree binfo = binfo_or_else (basetype, dtype);
1934 return build_x_component_ref (build_scoped_ref (datum, basetype),
1937 return error_mark_node;
1940 /* Like `build_component_ref, but uses an already found field, and converts
1941 from a reference. Must compute access for current_class_ref.
1945 build_component_ref_1 (datum, field, protect)
1949 return convert_from_reference
1950 (build_component_ref (datum, field, NULL_TREE, protect));
1953 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1954 can, for example, use as an lvalue. This code used to be in
1955 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1956 expressions, where we're dealing with aggregates. But now it's again only
1957 called from unary_complex_lvalue. The case (in particular) that led to
1958 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1962 rationalize_conditional_expr (code, t)
1963 enum tree_code code;
1966 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1967 the first operand is always the one to be used if both operands
1968 are equal, so we know what conditional expression this used to be. */
1969 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1972 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1973 ? LE_EXPR : GE_EXPR),
1974 TREE_OPERAND (t, 0),
1975 TREE_OPERAND (t, 1)),
1976 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1977 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1981 build_conditional_expr (TREE_OPERAND (t, 0),
1982 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1983 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1986 /* Given the TYPE of an anonymous union field inside T, return the
1987 FIELD_DECL for the field. If not found return NULL_TREE. Because
1988 anonymous unions can nest, we must also search all anonymous unions
1989 that are directly reachable. */
1992 lookup_anon_field (t, type)
1997 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1999 if (TREE_STATIC (field))
2001 if (TREE_CODE (field) != FIELD_DECL)
2004 /* If we find it directly, return the field. */
2005 if (DECL_NAME (field) == NULL_TREE
2006 && type == TREE_TYPE (field))
2011 /* Otherwise, it could be nested, search harder. */
2012 if (DECL_NAME (field) == NULL_TREE
2013 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2015 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2023 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2024 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2025 that we are interested in, or it can be a FIELD_DECL. */
2028 build_component_ref (datum, component, basetype_path, protect)
2029 tree datum, component, basetype_path;
2032 register tree basetype;
2033 register enum tree_code code;
2034 register tree field = NULL;
2039 if (processing_template_decl)
2040 return build_min_nt (COMPONENT_REF, datum, component);
2042 if (datum == error_mark_node
2043 || TREE_TYPE (datum) == error_mark_node)
2044 return error_mark_node;
2046 /* BASETYPE holds the type of the class containing the COMPONENT. */
2047 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2049 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2051 switch (TREE_CODE (datum))
2055 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2056 basetype_path, protect);
2057 return build (COMPOUND_EXPR, TREE_TYPE (value),
2058 TREE_OPERAND (datum, 0), value);
2061 return build_conditional_expr
2062 (TREE_OPERAND (datum, 0),
2063 build_component_ref (TREE_OPERAND (datum, 1), component,
2064 basetype_path, protect),
2065 build_component_ref (TREE_OPERAND (datum, 2), component,
2066 basetype_path, protect));
2069 cp_error ("invalid use of %D", datum);
2070 datum = error_mark_node;
2077 code = TREE_CODE (basetype);
2079 if (code == REFERENCE_TYPE)
2081 datum = convert_from_reference (datum);
2082 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2083 code = TREE_CODE (basetype);
2085 if (TREE_CODE (datum) == OFFSET_REF)
2087 datum = resolve_offset_ref (datum);
2088 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2089 code = TREE_CODE (basetype);
2092 /* First, see if there is a field or component with name COMPONENT. */
2093 if (TREE_CODE (component) == TREE_LIST)
2095 /* I could not trigger this code. MvL */
2096 my_friendly_abort (980326);
2098 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2099 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2101 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2104 if (! IS_AGGR_TYPE_CODE (code))
2106 if (code != ERROR_MARK)
2107 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2108 component, datum, basetype);
2109 return error_mark_node;
2112 if (!complete_type_or_else (basetype, datum))
2113 return error_mark_node;
2115 if (TREE_CODE (component) == BIT_NOT_EXPR)
2117 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2119 cp_error ("destructor specifier `%T::~%T' must have matching names",
2120 basetype, TREE_OPERAND (component, 0));
2121 return error_mark_node;
2123 if (! TYPE_HAS_DESTRUCTOR (basetype))
2125 cp_error ("type `%T' has no destructor", basetype);
2126 return error_mark_node;
2128 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2131 /* Look up component name in the structure type definition. */
2132 if (CLASSTYPE_VFIELD (basetype)
2133 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
2134 /* Special-case this because if we use normal lookups in an ambiguous
2135 hierarchy, the compiler will abort (because vptr lookups are
2136 not supposed to be ambiguous. */
2137 field = CLASSTYPE_VFIELD (basetype);
2138 else if (TREE_CODE (component) == FIELD_DECL)
2140 else if (TREE_CODE (component) == TYPE_DECL)
2142 cp_error ("invalid use of type decl `%#D' as expression", component);
2143 return error_mark_node;
2147 tree name = component;
2148 if (TREE_CODE (component) == VAR_DECL)
2149 name = DECL_NAME (component);
2150 if (basetype_path == NULL_TREE)
2151 basetype_path = TYPE_BINFO (basetype);
2152 field = lookup_field (basetype_path, name,
2153 protect && !VFIELD_NAME_P (name), 0);
2154 if (field == error_mark_node)
2155 return error_mark_node;
2157 if (field == NULL_TREE)
2159 /* Not found as a data field, look for it as a method. If found,
2160 then if this is the only possible one, return it, else
2161 report ambiguity error. */
2162 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2163 if (fndecls == error_mark_node)
2164 return error_mark_node;
2167 /* If the function is unique and static, we can resolve it
2168 now. Otherwise, we have to wait and see what context it is
2169 used in; a component_ref involving a non-static member
2170 function can only be used in a call (expr.ref). */
2172 if (TREE_CHAIN (fndecls) == NULL_TREE
2173 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2175 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2177 tree fndecl = TREE_VALUE (fndecls);
2178 enforce_access (TREE_PURPOSE (fndecls), fndecl);
2184 /* A unique non-static member function. Other parts
2185 of the compiler expect something with
2186 unknown_type_node to be really overloaded, so
2188 TREE_VALUE (fndecls)
2189 = scratch_ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2193 ref = build (COMPONENT_REF, unknown_type_node,
2198 cp_error ("`%#T' has no member named `%D'", basetype, name);
2199 return error_mark_node;
2201 else if (TREE_TYPE (field) == error_mark_node)
2202 return error_mark_node;
2204 if (TREE_CODE (field) != FIELD_DECL)
2206 if (TREE_CODE (field) == TYPE_DECL)
2207 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2208 else if (DECL_RTL (field) != 0)
2211 TREE_USED (field) = 1;
2216 /* See if we have to do any conversions so that we pick up the field from the
2218 if (DECL_FIELD_CONTEXT (field) != basetype)
2220 tree context = DECL_FIELD_CONTEXT (field);
2221 tree base = context;
2222 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2223 && ANON_UNION_TYPE_P (base))
2225 base = TYPE_CONTEXT (base);
2228 /* Handle base classes here... */
2229 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2231 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2232 if (integer_zerop (addr))
2234 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2235 return error_mark_node;
2237 if (VBASE_NAME_P (DECL_NAME (field)))
2239 /* It doesn't matter which vbase pointer we grab, just
2240 find one of them. */
2241 tree binfo = get_binfo (base,
2242 TREE_TYPE (TREE_TYPE (addr)), 0);
2243 addr = convert_pointer_to_real (binfo, addr);
2246 addr = convert_pointer_to (base, addr);
2247 datum = build_indirect_ref (addr, NULL_PTR);
2248 my_friendly_assert (datum != error_mark_node, 311);
2252 /* Handle things from anon unions here... */
2253 if (TYPE_NAME (context) && ANON_UNION_TYPE_P (context))
2255 tree subfield = lookup_anon_field (basetype, context);
2256 tree subdatum = build_component_ref (datum, subfield,
2257 basetype_path, protect);
2258 return build_component_ref (subdatum, field, basetype_path, protect);
2262 /* Compute the type of the field, as described in [expr.ref]. */
2263 type_quals = TYPE_UNQUALIFIED;
2264 field_type = TREE_TYPE (field);
2265 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2266 /* The standard says that the type of the result should be the
2267 type referred to by the reference. But for now, at least, we
2268 do the conversion from reference type later. */
2272 type_quals = (CP_TYPE_QUALS (field_type)
2273 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2275 /* A field is const (volatile) if the enclosing object, or the
2276 field itself, is const (volatile). But, a mutable field is
2277 not const, even within a const object. */
2278 if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2279 type_quals &= ~TYPE_QUAL_CONST;
2280 if (!IS_SIGNATURE (field_type))
2281 field_type = cp_build_qualified_type (field_type, type_quals);
2284 ref = fold (build (COMPONENT_REF, field_type,
2285 break_out_cleanups (datum), field));
2287 /* Mark the expression const or volatile, as appropriate. Even
2288 though we've dealt with the type above, we still have to mark the
2289 expression itself. */
2290 if (type_quals & TYPE_QUAL_CONST)
2291 TREE_READONLY (ref) = 1;
2292 else if (type_quals & TYPE_QUAL_VOLATILE)
2293 TREE_THIS_VOLATILE (ref) = 1;
2298 /* Variant of build_component_ref for use in expressions, which should
2299 never have REFERENCE_TYPE. */
2302 build_x_component_ref (datum, component, basetype_path, protect)
2303 tree datum, component, basetype_path;
2306 tree t = build_component_ref (datum, component, basetype_path, protect);
2308 if (! processing_template_decl)
2309 t = convert_from_reference (t);
2314 /* Given an expression PTR for a pointer, return an expression
2315 for the value pointed to.
2316 ERRORSTRING is the name of the operator to appear in error messages.
2318 This function may need to overload OPERATOR_FNNAME.
2319 Must also handle REFERENCE_TYPEs for C++. */
2322 build_x_indirect_ref (ptr, errorstring)
2324 const char *errorstring;
2328 if (processing_template_decl)
2329 return build_min_nt (INDIRECT_REF, ptr);
2331 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2335 return build_indirect_ref (ptr, errorstring);
2339 build_indirect_ref (ptr, errorstring)
2341 const char *errorstring;
2343 register tree pointer, type;
2345 if (ptr == error_mark_node)
2346 return error_mark_node;
2348 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2349 ? ptr : default_conversion (ptr));
2350 type = TREE_TYPE (pointer);
2352 if (ptr == current_class_ptr)
2353 return current_class_ref;
2355 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2359 If the type of the expression is "pointer to T," the type
2360 of the result is "T."
2362 We must use the canonical variant because certain parts of
2363 the back end, like fold, do pointer comparisons between
2365 tree t = canonical_type_variant (TREE_TYPE (type));
2367 if (TREE_CODE (pointer) == ADDR_EXPR
2369 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2370 /* The POINTER was something like `&x'. We simplify `*&x' to
2372 return TREE_OPERAND (pointer, 0);
2375 tree ref = build1 (INDIRECT_REF, t, pointer);
2377 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2378 so that we get the proper error message if the result is used
2379 to assign to. Also, &* is supposed to be a no-op. */
2380 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2381 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2382 TREE_SIDE_EFFECTS (ref)
2383 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2388 /* `pointer' won't be an error_mark_node if we were given a
2389 pointer to member, so it's cool to check for this here. */
2390 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2391 error ("invalid use of `%s' on pointer to member", errorstring);
2392 else if (TREE_CODE (type) == RECORD_TYPE
2393 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2394 error ("cannot dereference signature pointer/reference");
2395 else if (pointer != error_mark_node)
2398 error ("invalid type argument of `%s'", errorstring);
2400 error ("invalid type argument");
2402 return error_mark_node;
2405 /* This handles expressions of the form "a[i]", which denotes
2408 This is logically equivalent in C to *(a+i), but we may do it differently.
2409 If A is a variable or a member, we generate a primitive ARRAY_REF.
2410 This avoids forcing the array out of registers, and can work on
2411 arrays that are not lvalues (for example, members of structures returned
2414 If INDEX is of some user-defined type, it must be converted to
2415 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2416 will inherit the type of the array, which will be some pointer type. */
2419 build_array_ref (array, idx)
2424 error ("subscript missing in array reference");
2425 return error_mark_node;
2428 if (TREE_TYPE (array) == error_mark_node
2429 || TREE_TYPE (idx) == error_mark_node)
2430 return error_mark_node;
2432 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2433 && TREE_CODE (array) != INDIRECT_REF)
2437 /* Subscripting with type char is likely to lose
2438 on a machine where chars are signed.
2439 So warn on any machine, but optionally.
2440 Don't warn for unsigned char since that type is safe.
2441 Don't warn for signed char because anyone who uses that
2442 must have done so deliberately. */
2443 if (warn_char_subscripts
2444 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2445 warning ("array subscript has type `char'");
2447 /* Apply default promotions *after* noticing character types. */
2448 idx = default_conversion (idx);
2450 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2452 error ("array subscript is not an integer");
2453 return error_mark_node;
2456 /* An array that is indexed by a non-constant
2457 cannot be stored in a register; we must be able to do
2458 address arithmetic on its address.
2459 Likewise an array of elements of variable size. */
2460 if (TREE_CODE (idx) != INTEGER_CST
2461 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2462 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2465 if (mark_addressable (array) == 0)
2466 return error_mark_node;
2468 /* An array that is indexed by a constant value which is not within
2469 the array bounds cannot be stored in a register either; because we
2470 would get a crash in store_bit_field/extract_bit_field when trying
2471 to access a non-existent part of the register. */
2472 if (TREE_CODE (idx) == INTEGER_CST
2473 && TYPE_VALUES (TREE_TYPE (array))
2474 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2476 if (mark_addressable (array) == 0)
2477 return error_mark_node;
2480 if (pedantic && !lvalue_p (array))
2481 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2483 /* Note in C++ it is valid to subscript a `register' array, since
2484 it is valid to take the address of something with that
2485 storage specification. */
2489 while (TREE_CODE (foo) == COMPONENT_REF)
2490 foo = TREE_OPERAND (foo, 0);
2491 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2492 warning ("subscripting array declared `register'");
2495 type = TREE_TYPE (TREE_TYPE (array));
2496 rval = build (ARRAY_REF, type, array, idx);
2497 /* Array ref is const/volatile if the array elements are
2498 or if the array is.. */
2499 TREE_READONLY (rval)
2500 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2501 TREE_SIDE_EFFECTS (rval)
2502 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2503 TREE_THIS_VOLATILE (rval)
2504 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2505 return require_complete_type (fold (rval));
2509 tree ar = default_conversion (array);
2510 tree ind = default_conversion (idx);
2512 /* Put the integer in IND to simplify error checking. */
2513 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2520 if (ar == error_mark_node)
2523 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2525 error ("subscripted value is neither array nor pointer");
2526 return error_mark_node;
2528 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2530 error ("array subscript is not an integer");
2531 return error_mark_node;
2534 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar,
2540 /* Build a function call to function FUNCTION with parameters PARAMS.
2541 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2542 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2543 not include any object pointer that may be required. FUNCTION's
2544 data type may be a function type or a pointer-to-function.
2546 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2547 is the list of possible methods that FUNCTION could conceivably
2548 be. If the list of methods comes from a class, then it will be
2549 a list of lists (where each element is associated with the class
2550 that produced it), otherwise it will be a simple list (for
2551 functions overloaded in global scope).
2553 In the first case, TREE_VALUE (function) is the head of one of those
2554 lists, and TREE_PURPOSE is the name of the function.
2556 In the second case, TREE_PURPOSE (function) is the function's
2559 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2561 When calling a TEMPLATE_DECL, we don't require a complete return
2565 build_x_function_call (function, params, decl)
2566 tree function, params, decl;
2569 tree template_id = NULL_TREE;
2572 if (function == error_mark_node)
2573 return error_mark_node;
2575 if (processing_template_decl)
2576 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2578 /* Save explicit template arguments if found */
2579 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2581 template_id = function;
2582 function = TREE_OPERAND (function, 0);
2585 type = TREE_TYPE (function);
2587 if (TREE_CODE (type) == OFFSET_TYPE
2588 && TREE_TYPE (type) == unknown_type_node
2589 && TREE_CODE (function) == TREE_LIST
2590 && TREE_CHAIN (function) == NULL_TREE)
2592 /* Undo (Foo:bar)()... */
2593 type = TYPE_OFFSET_BASETYPE (type);
2594 function = TREE_VALUE (function);
2595 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2596 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2597 function = TREE_VALUE (function);
2598 if (TREE_CODE (function) == OVERLOAD)
2599 function = OVL_FUNCTION (function);
2600 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2601 function = DECL_NAME (function);
2602 return build_method_call (decl, function, params,
2603 TYPE_BINFO (type), LOOKUP_NORMAL);
2606 if ((TREE_CODE (function) == FUNCTION_DECL
2607 && DECL_STATIC_FUNCTION_P (function))
2608 || (TREE_CODE (function) == TEMPLATE_DECL
2609 && DECL_STATIC_FUNCTION_P (DECL_RESULT (function))))
2610 return build_member_call(DECL_CONTEXT (function),
2612 ? template_id : DECL_NAME (function),
2615 is_method = ((TREE_CODE (function) == TREE_LIST
2616 && current_class_type != NULL_TREE
2617 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2619 || TREE_CODE (function) == IDENTIFIER_NODE
2620 || TREE_CODE (type) == METHOD_TYPE
2621 || TYPE_PTRMEMFUNC_P (type));
2623 /* A friend template. Make it look like a toplevel declaration. */
2624 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2625 function = scratch_ovl_cons (function, NULL_TREE);
2627 /* Handle methods, friends, and overloaded functions, respectively. */
2630 tree basetype = NULL_TREE;
2632 if (TREE_CODE (function) == FUNCTION_DECL
2633 || DECL_FUNCTION_TEMPLATE_P (function))
2635 basetype = DECL_CLASS_CONTEXT (function);
2637 if (DECL_NAME (function))
2638 function = DECL_NAME (function);
2640 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2642 else if (TREE_CODE (function) == TREE_LIST)
2644 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2645 == FUNCTION_DECL, 312);
2646 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2647 function = TREE_PURPOSE (function);
2649 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2651 if (TREE_CODE (function) == OFFSET_REF)
2653 if (TREE_OPERAND (function, 0))
2654 decl = TREE_OPERAND (function, 0);
2656 /* Call via a pointer to member function. */
2657 if (decl == NULL_TREE)
2659 error ("pointer to member function called, but not in class scope");
2660 return error_mark_node;
2662 /* What other type of POINTER_TYPE could this be? */
2663 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2664 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2665 && TREE_CODE (function) != OFFSET_REF)
2666 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2671 /* this is an abbreviated method call.
2672 must go through here in case it is a virtual function.
2673 @@ Perhaps this could be optimized. */
2675 if (basetype && (! current_class_type
2676 || ! DERIVED_FROM_P (basetype, current_class_type)))
2677 return build_member_call (basetype, function, params);
2679 if (decl == NULL_TREE)
2681 if (current_class_type == NULL_TREE)
2683 error ("object missing in call to method `%s'",
2684 IDENTIFIER_POINTER (function));
2685 return error_mark_node;
2687 /* Yow: call from a static member function. */
2688 decl = build_dummy_object (current_class_type);
2691 /* Put back explicit template arguments, if any. */
2693 function = template_id;
2694 return build_method_call (decl, function, params,
2695 NULL_TREE, LOOKUP_NORMAL);
2697 else if (TREE_CODE (function) == COMPONENT_REF
2698 && type == unknown_type_node)
2700 /* Undo what we did in build_component_ref. */
2701 decl = TREE_OPERAND (function, 0);
2702 function = TREE_OPERAND (function, 1);
2703 function = DECL_NAME (OVL_CURRENT (TREE_VALUE (function)));
2704 return build_method_call (decl, function, params,
2705 NULL_TREE, LOOKUP_NORMAL);
2707 else if (really_overloaded_fn (function))
2709 if (OVL_FUNCTION (function) == NULL_TREE)
2711 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2712 TREE_PURPOSE (function));
2713 return error_mark_node;
2717 /* Put back explicit template arguments, if any. */
2719 function = template_id;
2720 return build_new_function_call (function, params);
2724 /* Remove a potential OVERLOAD around it */
2725 function = OVL_CURRENT (function);
2728 if (TREE_CODE (function) == OFFSET_REF)
2730 /* If the component is a data element (or a virtual function), we play
2731 games here to make things work. */
2734 if (TREE_OPERAND (function, 0))
2735 decl = TREE_OPERAND (function, 0);
2737 decl = current_class_ref;
2739 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2741 /* Sigh. OFFSET_REFs are being used for too many things.
2742 They're being used both for -> and ->*, and we want to resolve
2743 the -> cases here, but leave the ->*. We could use
2744 resolve_offset_ref for those, too, but it would call
2745 get_member_function_from_ptrfunc and decl_addr wouldn't get
2746 updated properly. Nasty. */
2747 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2748 function = resolve_offset_ref (function);
2750 function = TREE_OPERAND (function, 1);
2752 function = get_member_function_from_ptrfunc (&decl_addr, function);
2753 params = expr_tree_cons (NULL_TREE, decl_addr, params);
2754 return build_function_call (function, params);
2757 type = TREE_TYPE (function);
2758 if (type != error_mark_node)
2760 if (TREE_CODE (type) == REFERENCE_TYPE)
2761 type = TREE_TYPE (type);
2763 if (IS_AGGR_TYPE (type))
2764 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2769 tree fntype = TREE_TYPE (function);
2770 tree ctypeptr = NULL_TREE;
2772 /* Explicitly named method? */
2773 if (TREE_CODE (function) == FUNCTION_DECL)
2774 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2775 /* Expression with ptr-to-method type? It could either be a plain
2776 usage, or it might be a case where the ptr-to-method is being
2777 passed in as an argument. */
2778 else if (TYPE_PTRMEMFUNC_P (fntype))
2780 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2781 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2782 ctypeptr = build_pointer_type (rec);
2784 /* Unexpected node type? */
2786 my_friendly_abort (116);
2787 if (decl == NULL_TREE)
2789 if (current_function_decl
2790 && DECL_STATIC_FUNCTION_P (current_function_decl))
2791 error ("invalid call to member function needing `this' in static member function scope");
2793 error ("pointer to member function called, but not in class scope");
2794 return error_mark_node;
2796 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2797 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2799 decl = build_unary_op (ADDR_EXPR, decl, 0);
2800 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2803 decl = build_c_cast (ctypeptr, decl);
2804 params = expr_tree_cons (NULL_TREE, decl, params);
2807 return build_function_call (function, params);
2810 /* Resolve a pointer to member function. INSTANCE is the object
2811 instance to use, if the member points to a virtual member. */
2814 get_member_function_from_ptrfunc (instance_ptrptr, function)
2815 tree *instance_ptrptr;
2818 if (TREE_CODE (function) == OFFSET_REF)
2820 function = TREE_OPERAND (function, 1);
2823 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2825 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2826 tree instance, basetype;
2828 tree instance_ptr = *instance_ptrptr;
2830 if (TREE_SIDE_EFFECTS (instance_ptr))
2831 instance_ptr = save_expr (instance_ptr);
2833 if (TREE_SIDE_EFFECTS (function))
2834 function = save_expr (function);
2836 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2837 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2839 delta = cp_convert (ptrdiff_type_node,
2840 build_component_ref (function, delta_identifier,
2842 e3 = PFN_FROM_PTRMEMFUNC (function);
2844 if (TYPE_SIZE (basetype) != NULL_TREE
2845 && ! TYPE_VIRTUAL_P (basetype))
2846 /* If basetype doesn't have virtual functions, don't emit code to
2847 handle that case. */
2851 /* Promoting idx before saving it improves performance on RISC
2852 targets. Without promoting, the first compare used
2853 load-with-sign-extend, while the second used normal load then
2854 shift to sign-extend. An optimizer flaw, perhaps, but it's
2855 easier to make this change. */
2856 idx = save_expr (default_conversion
2857 (build_component_ref (function,
2860 e1 = build_binary_op (GT_EXPR, idx, integer_zero_node, 1);
2862 /* Convert down to the right base, before using the instance. */
2863 instance = convert_pointer_to_real (basetype, instance_ptr);
2864 if (instance == error_mark_node && instance_ptr != error_mark_node)
2867 vtbl = convert_pointer_to (ptr_type_node, instance);
2868 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2871 build_pointer_type (build_pointer_type (vtable_entry_type)),
2872 vtbl, cp_convert (ptrdiff_type_node, delta2));
2873 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2874 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2876 integer_one_node, 1));
2877 if (! flag_vtable_thunks)
2879 aref = save_expr (aref);
2881 delta = build_binary_op
2883 build_conditional_expr (e1,
2884 build_component_ref (aref,
2891 if (flag_vtable_thunks)
2894 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2895 TREE_TYPE (e2) = TREE_TYPE (e3);
2896 e1 = build_conditional_expr (e1, e2, e3);
2898 /* Make sure this doesn't get evaluated first inside one of the
2899 branches of the COND_EXPR. */
2900 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2901 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2905 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2906 instance_ptr, delta);
2908 if (instance_ptr == error_mark_node
2909 && TREE_CODE (e1) != ADDR_EXPR
2910 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2911 cp_error ("object missing in `%E'", function);
2919 build_function_call_real (function, params, require_complete, flags)
2920 tree function, params;
2921 int require_complete, flags;
2923 register tree fntype, fndecl;
2924 register tree value_type;
2925 register tree coerced_params;
2926 tree name = NULL_TREE, assembler_name = NULL_TREE;
2929 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2930 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2931 if (TREE_CODE (function) == NOP_EXPR
2932 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2933 function = TREE_OPERAND (function, 0);
2935 if (TREE_CODE (function) == FUNCTION_DECL)
2937 name = DECL_NAME (function);
2938 assembler_name = DECL_ASSEMBLER_NAME (function);
2940 GNU_xref_call (current_function_decl,
2941 IDENTIFIER_POINTER (name ? name
2942 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2944 mark_used (function);
2947 /* Convert anything with function type to a pointer-to-function. */
2948 if (pedantic && DECL_MAIN_P (function))
2949 pedwarn ("ANSI C++ forbids calling `main' from within program");
2951 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2952 (because calling an inline function does not mean the function
2953 needs to be separately compiled). */
2955 if (DECL_INLINE (function))
2956 function = inline_conversion (function);
2958 function = build_addr_func (function);
2964 function = build_addr_func (function);
2967 if (function == error_mark_node)
2968 return error_mark_node;
2970 fntype = TREE_TYPE (function);
2972 if (TYPE_PTRMEMFUNC_P (fntype))
2974 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2976 return error_mark_node;
2979 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2980 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2982 if (!((TREE_CODE (fntype) == POINTER_TYPE
2983 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2985 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2987 cp_error ("`%E' cannot be used as a function", function);
2988 return error_mark_node;
2991 /* fntype now gets the type of function pointed to. */
2992 fntype = TREE_TYPE (fntype);
2994 /* Convert the parameters to the types declared in the
2995 function prototype, or apply default promotions. */
2997 if (flags & LOOKUP_COMPLAIN)
2998 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2999 params, fndecl, LOOKUP_NORMAL);
3001 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3004 if (coerced_params == error_mark_node)
3006 if (flags & LOOKUP_SPECULATIVELY)
3009 return error_mark_node;
3012 /* Check for errors in format strings. */
3014 if (warn_format && (name || assembler_name))
3015 check_function_format (name, assembler_name, coerced_params);
3017 /* Recognize certain built-in functions so we can make tree-codes
3018 other than CALL_EXPR. We do this when it enables fold-const.c
3019 to do something useful. */
3021 if (TREE_CODE (function) == ADDR_EXPR
3022 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3023 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3024 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
3029 if (coerced_params == 0)
3030 return integer_zero_node;
3031 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3038 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3040 register tree result
3041 = build_call (function, value_type, coerced_params);
3043 if (require_complete)
3045 if (TREE_CODE (value_type) == VOID_TYPE)
3047 result = require_complete_type (result);
3049 if (IS_AGGR_TYPE (value_type))
3050 result = build_cplus_new (value_type, result);
3051 return convert_from_reference (result);
3056 build_function_call (function, params)
3057 tree function, params;
3059 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3062 /* Convert the actual parameter expressions in the list VALUES
3063 to the types in the list TYPELIST.
3064 If parmdecls is exhausted, or when an element has NULL as its type,
3065 perform the default conversions.
3067 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3069 This is also where warnings about wrong number of args are generated.
3071 Return a list of expressions for the parameters as converted.
3073 Both VALUES and the returned value are chains of TREE_LIST nodes
3074 with the elements of the list in the TREE_VALUE slots of those nodes.
3076 In C++, unspecified trailing parameters can be filled in with their
3077 default arguments, if such were specified. Do so here. */
3080 convert_arguments (typelist, values, fndecl, flags)
3081 tree typelist, values, fndecl;
3084 register tree typetail, valtail;
3085 register tree result = NULL_TREE;
3086 const char *called_thing = 0;
3089 /* Argument passing is always copy-initialization. */
3090 flags |= LOOKUP_ONLYCONVERTING;
3094 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3096 if (DECL_NAME (fndecl) == NULL_TREE
3097 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3098 called_thing = "constructor";
3100 called_thing = "member function";
3103 called_thing = "function";
3106 for (valtail = values, typetail = typelist;
3108 valtail = TREE_CHAIN (valtail), i++)
3110 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3111 register tree val = TREE_VALUE (valtail);
3113 if (val == error_mark_node)
3114 return error_mark_node;
3116 if (type == void_type_node)
3120 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3122 error ("at this point in file");
3125 error ("too many arguments to function");
3126 /* In case anybody wants to know if this argument
3129 TREE_TYPE (tree_last (result)) = error_mark_node;
3133 if (TREE_CODE (val) == OFFSET_REF)
3134 val = resolve_offset_ref (val);
3136 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3137 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3138 if (TREE_CODE (val) == NOP_EXPR
3139 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3140 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3141 val = TREE_OPERAND (val, 0);
3143 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3145 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3146 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3147 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3148 val = default_conversion (val);
3151 if (val == error_mark_node)
3152 return error_mark_node;
3156 /* Formal parm type is specified by a function prototype. */
3159 if (TYPE_SIZE (complete_type (type)) == 0)
3161 error ("parameter type of called function is incomplete");
3166 parmval = convert_for_initialization
3167 (NULL_TREE, type, val, flags,
3168 "argument passing", fndecl, i);
3169 #ifdef PROMOTE_PROTOTYPES
3170 if ((TREE_CODE (type) == INTEGER_TYPE
3171 || TREE_CODE (type) == ENUMERAL_TYPE)
3172 && (TYPE_PRECISION (type)
3173 < TYPE_PRECISION (integer_type_node)))
3174 parmval = default_conversion (parmval);
3178 if (parmval == error_mark_node)
3179 return error_mark_node;
3181 result = expr_tree_cons (NULL_TREE, parmval, result);
3185 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3186 val = convert_from_reference (val);
3188 result = expr_tree_cons (NULL_TREE,
3189 convert_arg_to_ellipsis (val),
3194 typetail = TREE_CHAIN (typetail);
3197 if (typetail != 0 && typetail != void_list_node)
3199 /* See if there are default arguments that can be used */
3200 if (TREE_PURPOSE (typetail))
3202 for (; typetail != void_list_node; ++i)
3205 = convert_default_arg (TREE_VALUE (typetail),
3206 TREE_PURPOSE (typetail),
3209 if (parmval == error_mark_node)
3210 return error_mark_node;
3212 result = expr_tree_cons (0, parmval, result);
3213 typetail = TREE_CHAIN (typetail);
3214 /* ends with `...'. */
3215 if (typetail == NULL_TREE)
3223 cp_error_at ("too few arguments to %s `%+#D'",
3224 called_thing, fndecl);
3225 error ("at this point in file");
3228 error ("too few arguments to function");
3229 return error_mark_list;
3233 return nreverse (result);
3236 /* Build a binary-operation expression, after performing default
3237 conversions on the operands. CODE is the kind of expression to build. */
3240 build_x_binary_op (code, arg1, arg2)
3241 enum tree_code code;
3244 if (processing_template_decl)
3245 return build_min_nt (code, arg1, arg2);
3247 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3251 build_binary_op (code, arg1, arg2, convert_p)
3252 enum tree_code code;
3254 int convert_p ATTRIBUTE_UNUSED;
3256 return build_binary_op_nodefault (code, arg1, arg2, code);
3259 /* Build a binary-operation expression without default conversions.
3260 CODE is the kind of expression to build.
3261 This function differs from `build' in several ways:
3262 the data type of the result is computed and recorded in it,
3263 warnings are generated if arg data types are invalid,
3264 special handling for addition and subtraction of pointers is known,
3265 and some optimization is done (operations on narrow ints
3266 are done in the narrower type when that gives the same result).
3267 Constant folding is also done before the result is returned.
3269 ERROR_CODE is the code that determines what to say in error messages.
3270 It is usually, but not always, the same as CODE.
3272 Note that the operands will never have enumeral types
3273 because either they have just had the default conversions performed
3274 or they have both just been converted to some other type in which
3275 the arithmetic is to be done.
3277 C++: must do special pointer arithmetic when implementing
3278 multiple inheritance, and deal with pointer to member functions. */
3281 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3282 enum tree_code code;
3283 tree orig_op0, orig_op1;
3284 enum tree_code error_code;
3287 register enum tree_code code0, code1;
3290 /* Expression code to give to the expression when it is built.
3291 Normally this is CODE, which is what the caller asked for,
3292 but in some special cases we change it. */
3293 register enum tree_code resultcode = code;
3295 /* Data type in which the computation is to be performed.
3296 In the simplest cases this is the common type of the arguments. */
3297 register tree result_type = NULL;
3299 /* Nonzero means operands have already been type-converted
3300 in whatever way is necessary.
3301 Zero means they need to be converted to RESULT_TYPE. */
3304 /* Nonzero means create the expression with this type, rather than
3306 tree build_type = 0;
3308 /* Nonzero means after finally constructing the expression
3309 convert it to this type. */
3310 tree final_type = 0;
3312 /* Nonzero if this is an operation like MIN or MAX which can
3313 safely be computed in short if both args are promoted shorts.
3314 Also implies COMMON.
3315 -1 indicates a bitwise operation; this makes a difference
3316 in the exact conditions for when it is safe to do the operation
3317 in a narrower mode. */
3320 /* Nonzero if this is a comparison operation;
3321 if both args are promoted shorts, compare the original shorts.
3322 Also implies COMMON. */
3323 int short_compare = 0;
3325 /* Nonzero if this is a right-shift operation, which can be computed on the
3326 original short and then promoted if the operand is a promoted short. */
3327 int short_shift = 0;
3329 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3332 /* Apply default conversions. */
3333 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3334 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3335 || code == TRUTH_XOR_EXPR)
3337 op0 = decay_conversion (orig_op0);
3338 op1 = decay_conversion (orig_op1);
3342 op0 = default_conversion (orig_op0);
3343 op1 = default_conversion (orig_op1);
3346 type0 = TREE_TYPE (op0);
3347 type1 = TREE_TYPE (op1);
3349 /* The expression codes of the data types of the arguments tell us
3350 whether the arguments are integers, floating, pointers, etc. */
3351 code0 = TREE_CODE (type0);
3352 code1 = TREE_CODE (type1);
3354 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3355 STRIP_TYPE_NOPS (op0);
3356 STRIP_TYPE_NOPS (op1);
3358 /* If an error was already reported for one of the arguments,
3359 avoid reporting another error. */
3361 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3362 return error_mark_node;
3367 /* Handle the pointer + int case. */
3368 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3369 return pointer_int_sum (PLUS_EXPR, op0, op1);
3370 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3371 return pointer_int_sum (PLUS_EXPR, op1, op0);
3377 /* Subtraction of two similar pointers.
3378 We must subtract them as integers, then divide by object size. */
3379 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3380 && comp_target_types (type0, type1, 1))
3381 return pointer_diff (op0, op1, common_type (type0, type1));
3382 /* Handle pointer minus int. Just like pointer plus int. */
3383 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3384 return pointer_int_sum (MINUS_EXPR, op0, op1);
3393 case TRUNC_DIV_EXPR:
3395 case FLOOR_DIV_EXPR:
3396 case ROUND_DIV_EXPR:
3397 case EXACT_DIV_EXPR:
3398 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3399 || code0 == COMPLEX_TYPE)
3400 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3401 || code1 == COMPLEX_TYPE))
3403 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3404 cp_warning ("division by zero in `%E / 0'", op0);
3405 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3406 cp_warning ("division by zero in `%E / 0.'", op0);
3408 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3409 resultcode = RDIV_EXPR;
3411 /* When dividing two signed integers, we have to promote to int.
3412 unless we divide by a constant != -1. Note that default
3413 conversion will have been performed on the operands at this
3414 point, so we have to dig out the original type to find out if
3416 shorten = ((TREE_CODE (op0) == NOP_EXPR
3417 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3418 || (TREE_CODE (op1) == INTEGER_CST
3419 && (TREE_INT_CST_LOW (op1) != -1
3420 || TREE_INT_CST_HIGH (op1) != -1)));
3426 case BIT_ANDTC_EXPR:
3429 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3431 /* If one operand is a constant, and the other is a short type
3432 that has been converted to an int,
3433 really do the work in the short type and then convert the
3434 result to int. If we are lucky, the constant will be 0 or 1
3435 in the short type, making the entire operation go away. */
3436 if (TREE_CODE (op0) == INTEGER_CST
3437 && TREE_CODE (op1) == NOP_EXPR
3438 && (TYPE_PRECISION (type1)
3439 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3440 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3442 final_type = result_type;
3443 op1 = TREE_OPERAND (op1, 0);
3444 result_type = TREE_TYPE (op1);
3446 if (TREE_CODE (op1) == INTEGER_CST
3447 && TREE_CODE (op0) == NOP_EXPR
3448 && (TYPE_PRECISION (type0)
3449 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3450 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3452 final_type = result_type;
3453 op0 = TREE_OPERAND (op0, 0);
3454 result_type = TREE_TYPE (op0);
3458 case TRUNC_MOD_EXPR:
3459 case FLOOR_MOD_EXPR:
3460 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3461 cp_warning ("division by zero in `%E %% 0'", op0);
3462 else if (code1 == REAL_TYPE && real_zerop (op1))
3463 cp_warning ("division by zero in `%E %% 0.'", op0);
3465 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3467 /* Although it would be tempting to shorten always here, that loses
3468 on some targets, since the modulo instruction is undefined if the
3469 quotient can't be represented in the computation mode. We shorten
3470 only if unsigned or if dividing by something we know != -1. */
3471 shorten = ((TREE_CODE (op0) == NOP_EXPR
3472 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3473 || (TREE_CODE (op1) == INTEGER_CST
3474 && (TREE_INT_CST_LOW (op1) != -1
3475 || TREE_INT_CST_HIGH (op1) != -1)));
3480 case TRUTH_ANDIF_EXPR:
3481 case TRUTH_ORIF_EXPR:
3482 case TRUTH_AND_EXPR:
3484 result_type = boolean_type_node;
3487 /* Shift operations: result has same type as first operand;
3488 always convert second operand to int.
3489 Also set SHORT_SHIFT if shifting rightward. */
3492 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3494 result_type = type0;
3495 if (TREE_CODE (op1) == INTEGER_CST)
3497 if (tree_int_cst_lt (op1, integer_zero_node))
3498 warning ("right shift count is negative");
3501 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3503 if (TREE_INT_CST_HIGH (op1) != 0
3504 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3505 >= TYPE_PRECISION (type0)))
3506 warning ("right shift count >= width of type");
3509 /* Convert the shift-count to an integer, regardless of
3510 size of value being shifted. */
3511 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3512 op1 = cp_convert (integer_type_node, op1);
3513 /* Avoid converting op1 to result_type later. */
3519 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3521 result_type = type0;
3522 if (TREE_CODE (op1) == INTEGER_CST)
3524 if (tree_int_cst_lt (op1, integer_zero_node))
3525 warning ("left shift count is negative");
3526 else if (TREE_INT_CST_HIGH (op1) != 0
3527 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3528 >= TYPE_PRECISION (type0)))
3529 warning ("left shift count >= width of type");
3531 /* Convert the shift-count to an integer, regardless of
3532 size of value being shifted. */
3533 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3534 op1 = cp_convert (integer_type_node, op1);
3535 /* Avoid converting op1 to result_type later. */
3542 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3544 result_type = type0;
3545 if (TREE_CODE (op1) == INTEGER_CST)
3547 if (tree_int_cst_lt (op1, integer_zero_node))
3548 warning ("%s rotate count is negative",
3549 (code == LROTATE_EXPR) ? "left" : "right");
3550 else if (TREE_INT_CST_HIGH (op1) != 0
3551 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3552 >= TYPE_PRECISION (type0)))
3553 warning ("%s rotate count >= width of type",
3554 (code == LROTATE_EXPR) ? "left" : "right");
3556 /* Convert the shift-count to an integer, regardless of
3557 size of value being shifted. */
3558 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3559 op1 = cp_convert (integer_type_node, op1);
3565 build_type = boolean_type_node;
3566 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3567 || code0 == COMPLEX_TYPE)
3568 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3569 || code1 == COMPLEX_TYPE))
3571 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3573 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3574 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3576 if (comp_target_types (type0, type1, 1))
3577 result_type = common_type (type0, type1);
3578 else if (tt0 == void_type_node)
3580 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3581 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3582 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3583 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3584 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3586 else if (tt1 == void_type_node)
3588 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3589 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3590 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3593 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3596 if (result_type == NULL_TREE)
3597 result_type = ptr_type_node;
3599 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3600 && integer_zerop (op1))
3601 result_type = type0;
3602 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3603 && integer_zerop (op0))
3604 result_type = type1;
3605 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3607 result_type = type0;
3608 error ("ANSI C++ forbids comparison between pointer and integer");
3610 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3612 result_type = type1;
3613 error ("ANSI C++ forbids comparison between pointer and integer");
3615 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3616 && integer_zerop (op1))
3618 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3619 op1 = integer_zero_node;
3620 result_type = TREE_TYPE (op0);
3622 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3623 && integer_zerop (op0))
3625 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3626 op1 = integer_zero_node;
3627 result_type = TREE_TYPE (op0);
3629 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3630 && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3631 == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3633 /* The code we generate for the test is:
3635 (op0.index == op1.index
3636 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3637 || op0.pfn == op1.pfn)) */
3639 tree index0 = build_component_ref (op0, index_identifier,
3641 tree index1 = save_expr (build_component_ref (op1, index_identifier,
3643 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3644 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3645 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3646 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3648 tree integer_neg_one_node
3649 = build_binary_op (MINUS_EXPR, integer_zero_node,
3650 integer_one_node, 1);
3651 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3652 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3653 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3654 build_binary_op (EQ_EXPR, delta20, delta21, 1),
3656 e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3657 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3658 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3659 if (code == EQ_EXPR)
3661 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3663 else if (TYPE_PTRMEMFUNC_P (type0)
3664 && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3666 tree index0 = build_component_ref (op0, index_identifier,
3669 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3670 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3671 tree delta21 = integer_zero_node;
3673 tree integer_neg_one_node
3674 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3675 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3676 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3678 /* Map everything down one to make room for
3679 the null pointer to member. */
3680 index1 = size_binop (PLUS_EXPR,
3681 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3683 op1 = integer_zero_node;
3684 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3685 (TREE_TYPE (type1)));
3686 delta21 = DECL_FIELD_BITPOS (delta21);
3687 delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3688 size_int (BITS_PER_UNIT));
3689 delta21 = convert (sizetype, delta21);
3692 index1 = integer_neg_one_node;
3694 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3696 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3699 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3700 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3701 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3702 build_binary_op (EQ_EXPR, delta20, delta21, 1),
3704 e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3705 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3706 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3707 if (code == EQ_EXPR)
3709 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3711 else if (TYPE_PTRMEMFUNC_P (type1)
3712 && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3714 return build_binary_op (code, op1, op0, 1);
3720 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3721 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3723 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3725 if (comp_target_types (type0, type1, 1))
3726 result_type = common_type (type0, type1);
3729 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3731 result_type = ptr_type_node;
3740 build_type = boolean_type_node;
3741 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3742 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3744 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3746 if (comp_target_types (type0, type1, 1))
3747 result_type = common_type (type0, type1);
3750 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3752 result_type = ptr_type_node;
3755 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3756 && integer_zerop (op1))
3757 result_type = type0;
3758 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3759 && integer_zerop (op0))
3760 result_type = type1;
3761 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3763 result_type = type0;
3764 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3766 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3768 result_type = type1;
3769 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3777 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3779 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3781 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3783 if (shorten || common || short_compare)
3784 result_type = common_type (type0, type1);
3786 /* For certain operations (which identify themselves by shorten != 0)
3787 if both args were extended from the same smaller type,
3788 do the arithmetic in that type and then extend.
3790 shorten !=0 and !=1 indicates a bitwise operation.
3791 For them, this optimization is safe only if
3792 both args are zero-extended or both are sign-extended.
3793 Otherwise, we might change the result.
3794 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3795 but calculated in (unsigned short) it would be (unsigned short)-1. */
3797 if (shorten && none_complex)
3799 int unsigned0, unsigned1;
3800 tree arg0 = get_narrower (op0, &unsigned0);
3801 tree arg1 = get_narrower (op1, &unsigned1);
3802 /* UNS is 1 if the operation to be done is an unsigned one. */
3803 int uns = TREE_UNSIGNED (result_type);
3806 final_type = result_type;
3808 /* Handle the case that OP0 does not *contain* a conversion
3809 but it *requires* conversion to FINAL_TYPE. */
3811 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3812 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3813 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3814 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3816 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3818 /* For bitwise operations, signedness of nominal type
3819 does not matter. Consider only how operands were extended. */
3823 /* Note that in all three cases below we refrain from optimizing
3824 an unsigned operation on sign-extended args.
3825 That would not be valid. */
3827 /* Both args variable: if both extended in same way
3828 from same width, do it in that width.
3829 Do it unsigned if args were zero-extended. */
3830 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3831 < TYPE_PRECISION (result_type))
3832 && (TYPE_PRECISION (TREE_TYPE (arg1))
3833 == TYPE_PRECISION (TREE_TYPE (arg0)))
3834 && unsigned0 == unsigned1
3835 && (unsigned0 || !uns))
3837 = signed_or_unsigned_type (unsigned0,
3838 common_type (TREE_TYPE (arg0),
3840 else if (TREE_CODE (arg0) == INTEGER_CST
3841 && (unsigned1 || !uns)
3842 && (TYPE_PRECISION (TREE_TYPE (arg1))
3843 < TYPE_PRECISION (result_type))
3844 && (type = signed_or_unsigned_type (unsigned1,
3846 int_fits_type_p (arg0, type)))
3848 else if (TREE_CODE (arg1) == INTEGER_CST
3849 && (unsigned0 || !uns)
3850 && (TYPE_PRECISION (TREE_TYPE (arg0))
3851 < TYPE_PRECISION (result_type))
3852 && (type = signed_or_unsigned_type (unsigned0,
3854 int_fits_type_p (arg1, type)))
3858 /* Shifts can be shortened if shifting right. */
3863 tree arg0 = get_narrower (op0, &unsigned_arg);
3865 final_type = result_type;
3867 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3868 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3870 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3871 /* We can shorten only if the shift count is less than the
3872 number of bits in the smaller type size. */
3873 && TREE_INT_CST_HIGH (op1) == 0
3874 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3875 /* If arg is sign-extended and then unsigned-shifted,
3876 we can simulate this with a signed shift in arg's type
3877 only if the extended result is at least twice as wide
3878 as the arg. Otherwise, the shift could use up all the
3879 ones made by sign-extension and bring in zeros.
3880 We can't optimize that case at all, but in most machines
3881 it never happens because available widths are 2**N. */
3882 && (!TREE_UNSIGNED (final_type)
3884 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3885 <= TYPE_PRECISION (result_type))))
3887 /* Do an unsigned shift if the operand was zero-extended. */
3889 = signed_or_unsigned_type (unsigned_arg,
3891 /* Convert value-to-be-shifted to that type. */
3892 if (TREE_TYPE (op0) != result_type)
3893 op0 = cp_convert (result_type, op0);
3898 /* Comparison operations are shortened too but differently.
3899 They identify themselves by setting short_compare = 1. */
3903 /* Don't write &op0, etc., because that would prevent op0
3904 from being kept in a register.
3905 Instead, make copies of the our local variables and
3906 pass the copies by reference, then copy them back afterward. */
3907 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3908 enum tree_code xresultcode = resultcode;
3910 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3912 return cp_convert (boolean_type_node, val);
3913 op0 = xop0, op1 = xop1;
3915 resultcode = xresultcode;
3918 if (short_compare && warn_sign_compare)
3920 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3921 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3923 int unsignedp0, unsignedp1;
3924 tree primop0 = get_narrower (op0, &unsignedp0);
3925 tree primop1 = get_narrower (op1, &unsignedp1);
3927 /* Check for comparison of different enum types. */
3928 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3929 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3930 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3931 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3933 cp_warning ("comparison between `%#T' and `%#T'",
3934 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3937 /* Give warnings for comparisons between signed and unsigned
3938 quantities that may fail. */
3939 /* Do the checking based on the original operand trees, so that
3940 casts will be considered, but default promotions won't be. */
3942 /* Do not warn if the comparison is being done in a signed type,
3943 since the signed type will only be chosen if it can represent
3944 all the values of the unsigned type. */
3945 if (! TREE_UNSIGNED (result_type))
3947 /* Do not warn if both operands are unsigned. */
3948 else if (op0_signed == op1_signed)
3950 /* Do not warn if the signed quantity is an unsuffixed
3951 integer literal (or some static constant expression
3952 involving such literals) and it is non-negative. */
3953 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3954 && tree_int_cst_sgn (orig_op0) >= 0)
3955 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3956 && tree_int_cst_sgn (orig_op1) >= 0))
3958 /* Do not warn if the comparison is an equality operation,
3959 the unsigned quantity is an integral constant and it does
3960 not use the most significant bit of result_type. */
3961 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3962 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3963 && int_fits_type_p (orig_op1,
3964 signed_type (result_type)))
3965 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3966 && int_fits_type_p (orig_op0,
3967 signed_type (result_type)))))
3970 warning ("comparison between signed and unsigned");
3972 /* Warn if two unsigned values are being compared in a size
3973 larger than their original size, and one (and only one) is the
3974 result of a `~' operator. This comparison will always fail.
3976 Also warn if one operand is a constant, and the constant does not
3977 have all bits set that are set in the ~ operand when it is
3980 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3981 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3983 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3984 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3985 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3986 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3988 if (TREE_CODE (primop0) == INTEGER_CST
3989 || TREE_CODE (primop1) == INTEGER_CST)
3992 HOST_WIDE_INT constant, mask;
3996 if (TREE_CODE (primop0) == INTEGER_CST)
3999 unsignedp = unsignedp1;
4000 constant = TREE_INT_CST_LOW (primop0);
4005 unsignedp = unsignedp0;
4006 constant = TREE_INT_CST_LOW (primop1);
4009 bits = TYPE_PRECISION (TREE_TYPE (primop));
4010 if (bits < TYPE_PRECISION (result_type)
4011 && bits < HOST_BITS_PER_LONG && unsignedp)
4013 mask = (~ (HOST_WIDE_INT) 0) << bits;
4014 if ((mask & constant) != mask)
4015 warning ("comparison of promoted ~unsigned with constant");
4018 else if (unsignedp0 && unsignedp1
4019 && (TYPE_PRECISION (TREE_TYPE (primop0))
4020 < TYPE_PRECISION (result_type))
4021 && (TYPE_PRECISION (TREE_TYPE (primop1))
4022 < TYPE_PRECISION (result_type)))
4023 warning ("comparison of promoted ~unsigned with unsigned");
4028 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4029 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4030 Then the expression will be built.
4031 It will be given type FINAL_TYPE if that is nonzero;
4032 otherwise, it will be given type RESULT_TYPE. */
4036 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
4037 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
4038 return error_mark_node;
4041 /* Issue warnings about peculiar, but legal, uses of NULL. */
4042 if (/* It's reasonable to use pointer values as operands of &&
4043 and ||, so NULL is no exception. */
4044 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4045 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4046 (orig_op0 == null_node
4047 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4048 /* Or vice versa. */
4049 || (orig_op1 == null_node
4050 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4051 /* Or, both are NULL and the operation was not a comparison. */
4052 || (orig_op0 == null_node && orig_op1 == null_node
4053 && code != EQ_EXPR && code != NE_EXPR)))
4054 /* Some sort of arithmetic operation involving NULL was
4055 performed. Note that pointer-difference and pointer-addition
4056 have already been handled above, and so we don't end up here in
4058 cp_warning ("NULL used in arithmetic");
4062 if (TREE_TYPE (op0) != result_type)
4063 op0 = cp_convert (result_type, op0);
4064 if (TREE_TYPE (op1) != result_type)
4065 op1 = cp_convert (result_type, op1);
4067 if (op0 == error_mark_node || op1 == error_mark_node)
4068 return error_mark_node;
4071 if (build_type == NULL_TREE)
4072 build_type = result_type;
4075 register tree result = build (resultcode, build_type, op0, op1);
4076 register tree folded;
4078 folded = fold (result);
4079 if (folded == result)
4080 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4081 if (final_type != 0)
4082 return cp_convert (final_type, folded);
4087 /* Return a tree for the sum or difference (RESULTCODE says which)
4088 of pointer PTROP and integer INTOP. */
4091 pointer_int_sum (resultcode, ptrop, intop)
4092 enum tree_code resultcode;
4093 register tree ptrop, intop;
4097 register tree result;
4098 register tree folded = fold (intop);
4100 /* The result is a pointer of the same type that is being added. */
4102 register tree result_type = TREE_TYPE (ptrop);
4104 if (!complete_type_or_else (result_type, ptrop))
4105 return error_mark_node;
4107 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4109 if (pedantic || warn_pointer_arith)
4110 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
4111 size_exp = integer_one_node;
4113 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4115 if (pedantic || warn_pointer_arith)
4116 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
4117 size_exp = integer_one_node;
4119 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4121 if (pedantic || warn_pointer_arith)
4122 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4123 size_exp = integer_one_node;
4125 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4127 if (pedantic || warn_pointer_arith)
4128 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4129 size_exp = integer_one_node;
4132 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4134 /* Needed to make OOPS V2R3 work. */
4136 if (TREE_CODE (intop) == INTEGER_CST
4137 && TREE_INT_CST_LOW (intop) == 0
4138 && TREE_INT_CST_HIGH (intop) == 0)
4141 /* If what we are about to multiply by the size of the elements
4142 contains a constant term, apply distributive law
4143 and multiply that constant term separately.
4144 This helps produce common subexpressions. */
4146 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4147 && ! TREE_CONSTANT (intop)
4148 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4149 && TREE_CONSTANT (size_exp))
4151 enum tree_code subcode = resultcode;
4152 if (TREE_CODE (intop) == MINUS_EXPR)
4153 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4154 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
4155 intop = TREE_OPERAND (intop, 0);
4158 /* Convert the integer argument to a type the same size as sizetype
4159 so the multiply won't overflow spuriously. */
4161 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4162 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4164 /* Replace the integer argument with a suitable product by the object size.
4165 Do this multiplication as signed, then convert to the appropriate
4166 pointer type (actually unsigned integral). */
4168 intop = cp_convert (result_type,
4169 build_binary_op (MULT_EXPR, intop,
4170 cp_convert (TREE_TYPE (intop),
4174 /* Create the sum or difference. */
4176 result = build (resultcode, result_type, ptrop, intop);
4178 folded = fold (result);
4179 if (folded == result)
4180 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4184 /* Return a tree for the difference of pointers OP0 and OP1.
4185 The resulting tree has type int. */
4188 pointer_diff (op0, op1, ptrtype)
4189 register tree op0, op1;
4190 register tree ptrtype;
4192 register tree result, folded;
4193 tree restype = ptrdiff_type_node;
4194 tree target_type = TREE_TYPE (ptrtype);
4196 if (!complete_type_or_else (target_type, NULL_TREE))
4197 return error_mark_node;
4199 if (pedantic || warn_pointer_arith)
4201 if (TREE_CODE (target_type) == VOID_TYPE)
4202 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4203 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4204 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4205 if (TREE_CODE (target_type) == METHOD_TYPE)
4206 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4207 if (TREE_CODE (target_type) == OFFSET_TYPE)
4208 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4211 /* First do the subtraction as integers;
4212 then drop through to build the divide operator. */
4214 op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4215 cp_convert (restype, op1), 1);
4217 /* This generates an error if op1 is a pointer to an incomplete type. */
4218 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4219 error ("arithmetic on pointer to an incomplete type");
4221 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4222 || TREE_CODE (target_type) == FUNCTION_TYPE
4223 || TREE_CODE (target_type) == METHOD_TYPE
4224 || TREE_CODE (target_type) == OFFSET_TYPE)
4226 : size_in_bytes (target_type));
4228 /* Do the division. */
4230 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4232 folded = fold (result);
4233 if (folded == result)
4234 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4238 /* Handle the case of taking the address of a COMPONENT_REF.
4239 Called by `build_unary_op' and `build_up_reference'.
4241 ARG is the COMPONENT_REF whose address we want.
4242 ARGTYPE is the pointer type that this address should have.
4243 MSG is an error message to print if this COMPONENT_REF is not
4244 addressable (such as a bitfield). */
4247 build_component_addr (arg, argtype, msg)
4251 tree field = TREE_OPERAND (arg, 1);
4252 tree basetype = decl_type_context (field);
4253 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4255 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4257 if (DECL_C_BIT_FIELD (field))
4259 error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
4260 return error_mark_node;
4263 if (TREE_CODE (field) == FIELD_DECL
4264 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4266 /* Can't convert directly to ARGTYPE, since that
4267 may have the same pointer type as one of our
4269 rval = build1 (NOP_EXPR, argtype,
4270 convert_pointer_to (basetype, rval));
4271 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4274 /* This conversion is harmless. */
4275 rval = convert_force (argtype, rval, 0);
4277 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4279 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4280 size_int (BITS_PER_UNIT));
4281 int flag = TREE_CONSTANT (rval);
4282 offset = convert (sizetype, offset);
4283 rval = fold (build (PLUS_EXPR, argtype,
4284 rval, cp_convert (argtype, offset)));
4285 TREE_CONSTANT (rval) = flag;
4290 /* Construct and perhaps optimize a tree representation
4291 for a unary operation. CODE, a tree_code, specifies the operation
4292 and XARG is the operand. */
4295 build_x_unary_op (code, xarg)
4296 enum tree_code code;
4299 if (processing_template_decl)
4300 return build_min_nt (code, xarg, NULL_TREE);
4302 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4304 if (code == ADDR_EXPR
4305 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4306 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4307 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4308 || (TREE_CODE (xarg) == OFFSET_REF)))
4309 /* don't look for a function */;
4314 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4315 NULL_TREE, NULL_TREE);
4316 if (rval || code != ADDR_EXPR)
4320 if (code == ADDR_EXPR)
4322 if (TREE_CODE (xarg) == TARGET_EXPR)
4323 warning ("taking address of temporary");
4326 return build_unary_op (code, xarg, 0);
4329 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4332 condition_conversion (expr)
4336 if (processing_template_decl)
4338 t = cp_convert (boolean_type_node, expr);
4339 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4343 /* C++: Must handle pointers to members.
4345 Perhaps type instantiation should be extended to handle conversion
4346 from aggregates to types we don't yet know we want? (Or are those
4347 cases typically errors which should be reported?)
4349 NOCONVERT nonzero suppresses the default promotions
4350 (such as from short to int). */
4353 build_unary_op (code, xarg, noconvert)
4354 enum tree_code code;
4358 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4359 register tree arg = xarg;
4360 register tree argtype = 0;
4361 const char *errstring = NULL;
4364 if (arg == error_mark_node)
4365 return error_mark_node;
4370 /* This is used for unary plus, because a CONVERT_EXPR
4371 is enough to prevent anybody from looking inside for
4372 associativity, but won't generate any code. */
4373 if (!(arg = build_expr_type_conversion
4374 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4375 errstring = "wrong type argument to unary plus";
4379 arg = default_conversion (arg);
4380 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4381 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4386 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4387 errstring = "wrong type argument to unary minus";
4388 else if (!noconvert)
4389 arg = default_conversion (arg);
4393 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4397 arg = default_conversion (arg);
4399 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4401 errstring = "wrong type argument to bit-complement";
4402 else if (!noconvert)
4403 arg = default_conversion (arg);
4407 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4408 errstring = "wrong type argument to abs";
4409 else if (!noconvert)
4410 arg = default_conversion (arg);
4414 /* Conjugating a real value is a no-op, but allow it anyway. */
4415 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4416 errstring = "wrong type argument to conjugation";
4417 else if (!noconvert)
4418 arg = default_conversion (arg);
4421 case TRUTH_NOT_EXPR:
4422 arg = cp_convert (boolean_type_node, arg);
4423 val = invert_truthvalue (arg);
4424 if (arg != error_mark_node)
4426 errstring = "in argument to unary !";
4433 if (TREE_CODE (arg) == COMPLEX_CST)
4434 return TREE_REALPART (arg);
4435 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4436 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4441 if (TREE_CODE (arg) == COMPLEX_CST)
4442 return TREE_IMAGPART (arg);
4443 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4444 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4446 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4448 case PREINCREMENT_EXPR:
4449 case POSTINCREMENT_EXPR:
4450 case PREDECREMENT_EXPR:
4451 case POSTDECREMENT_EXPR:
4452 /* Handle complex lvalues (when permitted)
4453 by reduction to simpler cases. */
4455 val = unary_complex_lvalue (code, arg);
4459 /* Increment or decrement the real part of the value,
4460 and don't change the imaginary part. */
4461 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4465 arg = stabilize_reference (arg);
4466 real = build_unary_op (REALPART_EXPR, arg, 1);
4467 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4468 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4469 build_unary_op (code, real, 1), imag);
4472 /* Report invalid types. */
4474 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4477 if (code == PREINCREMENT_EXPR)
4478 errstring ="no pre-increment operator for type";
4479 else if (code == POSTINCREMENT_EXPR)
4480 errstring ="no post-increment operator for type";
4481 else if (code == PREDECREMENT_EXPR)
4482 errstring ="no pre-decrement operator for type";
4484 errstring ="no post-decrement operator for type";
4488 /* Report something read-only. */
4490 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4491 || TREE_READONLY (arg))
4492 readonly_error (arg, ((code == PREINCREMENT_EXPR
4493 || code == POSTINCREMENT_EXPR)
4494 ? "increment" : "decrement"),
4499 tree result_type = TREE_TYPE (arg);
4501 arg = get_unwidened (arg, 0);
4502 argtype = TREE_TYPE (arg);
4504 /* ARM $5.2.5 last annotation says this should be forbidden. */
4505 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4506 pedwarn ("ANSI C++ forbids %sing an enum",
4507 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4508 ? "increment" : "decrement");
4510 /* Compute the increment. */
4512 if (TREE_CODE (argtype) == POINTER_TYPE)
4514 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4515 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4516 cp_error ("cannot %s a pointer to incomplete type `%T'",
4517 ((code == PREINCREMENT_EXPR
4518 || code == POSTINCREMENT_EXPR)
4519 ? "increment" : "decrement"), TREE_TYPE (argtype));
4520 else if ((pedantic || warn_pointer_arith)
4521 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4522 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4523 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4524 ((code == PREINCREMENT_EXPR
4525 || code == POSTINCREMENT_EXPR)
4526 ? "increment" : "decrement"), argtype);
4527 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4530 inc = integer_one_node;
4532 inc = cp_convert (argtype, inc);
4534 /* Handle incrementing a cast-expression. */
4536 switch (TREE_CODE (arg))
4541 case FIX_TRUNC_EXPR:
4542 case FIX_FLOOR_EXPR:
4543 case FIX_ROUND_EXPR:
4546 tree incremented, modify, value, compound;
4547 if (! lvalue_p (arg) && pedantic)
4548 pedwarn ("cast to non-reference type used as lvalue");
4549 arg = stabilize_reference (arg);
4550 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4553 value = save_expr (arg);
4554 incremented = build (((code == PREINCREMENT_EXPR
4555 || code == POSTINCREMENT_EXPR)
4556 ? PLUS_EXPR : MINUS_EXPR),
4557 argtype, value, inc);
4558 TREE_SIDE_EFFECTS (incremented) = 1;
4560 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4561 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4563 /* Eliminate warning about unused result of + or -. */
4564 TREE_NO_UNUSED_WARNING (compound) = 1;
4572 /* Complain about anything else that is not a true lvalue. */
4573 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4574 || code == POSTINCREMENT_EXPR)
4575 ? "increment" : "decrement")))
4576 return error_mark_node;
4578 /* Forbid using -- on `bool'. */
4579 if (TREE_TYPE (arg) == boolean_type_node)
4581 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4583 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4584 return error_mark_node;
4587 /* This will only work if someone can convince Kenner to accept
4588 my patch to expand_increment. (jason) */
4589 val = build (code, TREE_TYPE (arg), arg, inc);
4591 if (code == POSTINCREMENT_EXPR)
4593 arg = stabilize_reference (arg);
4594 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4596 TREE_SIDE_EFFECTS (val) = 1;
4597 arg = save_expr (arg);
4598 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4599 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4602 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4607 val = build (code, TREE_TYPE (arg), arg, inc);
4609 TREE_SIDE_EFFECTS (val) = 1;
4610 return cp_convert (result_type, val);
4614 /* Note that this operation never does default_conversion
4615 regardless of NOCONVERT. */
4617 argtype = lvalue_type (arg);
4618 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4622 build_pointer_type (TREE_TYPE (argtype)), arg);
4623 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4626 else if (pedantic && DECL_MAIN_P (arg))
4628 pedwarn ("taking address of function `main'");
4630 /* Let &* cancel out to simplify resulting code. */
4631 if (TREE_CODE (arg) == INDIRECT_REF)
4633 /* We don't need to have `current_class_ptr' wrapped in a
4634 NON_LVALUE_EXPR node. */
4635 if (arg == current_class_ref)
4636 return current_class_ptr;
4638 arg = TREE_OPERAND (arg, 0);
4639 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4643 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4644 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4646 else if (lvalue_p (arg))
4647 /* Don't let this be an lvalue. */
4648 return non_lvalue (arg);
4652 /* For &x[y], return x+y */
4653 if (TREE_CODE (arg) == ARRAY_REF)
4655 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4656 return error_mark_node;
4657 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4658 TREE_OPERAND (arg, 1), 1);
4661 /* Uninstantiated types are all functions. Taking the
4662 address of a function is a no-op, so just return the
4665 if (TREE_CODE (arg) == IDENTIFIER_NODE
4666 && IDENTIFIER_OPNAME_P (arg))
4668 my_friendly_abort (117);
4669 /* We don't know the type yet, so just work around the problem.
4670 We know that this will resolve to an lvalue. */
4671 return build1 (ADDR_EXPR, unknown_type_node, arg);
4674 if (type_unknown_p (arg))
4675 return build1 (ADDR_EXPR, unknown_type_node, arg);
4677 /* Handle complex lvalues (when permitted)
4678 by reduction to simpler cases. */
4679 val = unary_complex_lvalue (code, arg);
4683 switch (TREE_CODE (arg))
4688 case FIX_TRUNC_EXPR:
4689 case FIX_FLOOR_EXPR:
4690 case FIX_ROUND_EXPR:
4692 if (! lvalue_p (arg) && pedantic)
4693 pedwarn ("taking the address of a cast to non-reference type");
4700 /* Allow the address of a constructor if all the elements
4702 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4703 && TREE_CONSTANT (arg))
4705 /* Anything not already handled and not a true memory reference
4707 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4708 && TREE_CODE (argtype) != METHOD_TYPE
4709 && !lvalue_or_else (arg, "unary `&'"))
4710 return error_mark_node;
4712 if (argtype != error_mark_node)
4713 argtype = build_pointer_type (argtype);
4715 if (mark_addressable (arg) == 0)
4716 return error_mark_node;
4721 if (TREE_CODE (arg) == COMPONENT_REF)
4722 addr = build_component_addr
4724 "attempt to take address of bit-field structure member `%s'");
4726 addr = build1 (ADDR_EXPR, argtype, arg);
4728 /* Address of a static or external variable or
4729 function counts as a constant */
4731 TREE_CONSTANT (addr) = 1;
4733 if (TREE_CODE (argtype) == POINTER_TYPE
4734 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4736 build_ptrmemfunc_type (argtype);
4737 addr = build_ptrmemfunc (argtype, addr, 0);
4750 argtype = TREE_TYPE (arg);
4751 return fold (build1 (code, argtype, arg));
4755 return error_mark_node;
4759 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4760 convert ARG with the same conversions in the same order
4761 and return the result. */
4764 convert_sequence (conversions, arg)
4768 switch (TREE_CODE (conversions))
4773 case FIX_TRUNC_EXPR:
4774 case FIX_FLOOR_EXPR:
4775 case FIX_ROUND_EXPR:
4777 return cp_convert (TREE_TYPE (conversions),
4778 convert_sequence (TREE_OPERAND (conversions, 0),
4787 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4788 for certain kinds of expressions which are not really lvalues
4789 but which we can accept as lvalues.
4791 If ARG is not a kind of expression we can handle, return zero. */
4794 unary_complex_lvalue (code, arg)
4795 enum tree_code code;
4798 /* Handle (a, b) used as an "lvalue". */
4799 if (TREE_CODE (arg) == COMPOUND_EXPR)
4801 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4802 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4803 TREE_OPERAND (arg, 0), real_result);
4806 /* Handle (a ? b : c) used as an "lvalue". */
4807 if (TREE_CODE (arg) == COND_EXPR
4808 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4809 return rationalize_conditional_expr (code, arg);
4811 if (TREE_CODE (arg) == MODIFY_EXPR
4812 || TREE_CODE (arg) == PREINCREMENT_EXPR
4813 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4814 return unary_complex_lvalue
4815 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4816 arg, TREE_OPERAND (arg, 0)));
4818 if (code != ADDR_EXPR)
4821 /* Handle (a = b) used as an "lvalue" for `&'. */
4822 if (TREE_CODE (arg) == MODIFY_EXPR
4823 || TREE_CODE (arg) == INIT_EXPR)
4825 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4826 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4827 TREE_NO_UNUSED_WARNING (arg) = 1;
4831 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4832 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4833 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4835 /* The representation of something of type OFFSET_TYPE
4836 is really the representation of a pointer to it.
4837 Here give the representation its true type. */
4840 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4842 if (TREE_CODE (arg) != OFFSET_REF)
4845 t = TREE_OPERAND (arg, 1);
4847 /* Check all this code for right semantics. */
4848 if (TREE_CODE (t) == FUNCTION_DECL)
4850 if (DECL_DESTRUCTOR_P (t))
4851 cp_error ("taking address of destructor");
4852 return build_unary_op (ADDR_EXPR, t, 0);
4854 if (TREE_CODE (t) == VAR_DECL)
4855 return build_unary_op (ADDR_EXPR, t, 0);
4860 if (TREE_OPERAND (arg, 0)
4861 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4862 && TREE_CODE (t) != FIELD_DECL)
4864 cp_error ("taking address of bound pointer-to-member expression");
4865 return error_mark_node;
4868 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4869 type = build_pointer_type (type);
4871 t = make_node (PTRMEM_CST);
4872 TREE_TYPE (t) = type;
4873 PTRMEM_CST_MEMBER (t) = TREE_OPERAND (arg, 1);
4879 /* We permit compiler to make function calls returning
4880 objects of aggregate type look like lvalues. */
4884 if (TREE_CODE (targ) == SAVE_EXPR)
4885 targ = TREE_OPERAND (targ, 0);
4887 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4889 if (TREE_CODE (arg) == SAVE_EXPR)
4892 targ = build_cplus_new (TREE_TYPE (arg), arg);
4893 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4896 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4897 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4898 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4901 /* Don't let anything else be handled specially. */
4905 /* Mark EXP saying that we need to be able to take the
4906 address of it; it should not be allocated in a register.
4907 Value is 1 if successful.
4909 C++: we do not allow `current_class_ptr' to be addressable. */
4912 mark_addressable (exp)
4915 register tree x = exp;
4917 if (TREE_ADDRESSABLE (x) == 1)
4921 switch (TREE_CODE (x))
4928 x = TREE_OPERAND (x, 0);
4932 if (x == current_class_ptr)
4934 if (! flag_this_is_variable)
4935 error ("address of `this' not available");
4936 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4937 put_var_into_stack (x);
4941 if (TREE_STATIC (x) && TREE_READONLY (x)
4942 && DECL_RTL (x) != 0
4943 && ! DECL_IN_MEMORY_P (x))
4945 /* We thought this would make a good constant variable,
4946 but we were wrong. */
4947 push_obstacks_nochange ();
4948 end_temporary_allocation ();
4950 TREE_ASM_WRITTEN (x) = 0;
4952 rest_of_decl_compilation (x, 0,
4953 !DECL_FUNCTION_SCOPE_P (x),
4955 TREE_ADDRESSABLE (x) = 1;
4961 /* Caller should not be trying to mark initialized
4962 constant fields addressable. */
4963 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4964 || DECL_IN_AGGR_P (x) == 0
4966 || DECL_EXTERNAL (x), 314);
4970 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4971 && !DECL_ARTIFICIAL (x) && extra_warnings)
4972 cp_warning ("address requested for `%D', which is declared `register'",
4974 put_var_into_stack (x);
4975 TREE_ADDRESSABLE (x) = 1;
4979 if (DECL_LANG_SPECIFIC (x) != 0)
4981 x = DECL_MAIN_VARIANT (x);
4982 /* We have to test both conditions here. The first may be
4983 non-zero in the case of processing a default function. The
4984 second may be non-zero in the case of a template function. */
4985 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
4988 TREE_ADDRESSABLE (x) = 1;
4990 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4994 TREE_ADDRESSABLE (x) = 1;
4998 TREE_ADDRESSABLE (x) = 1;
4999 mark_addressable (TREE_OPERAND (x, 0));
5007 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5010 build_x_conditional_expr (ifexp, op1, op2)
5011 tree ifexp, op1, op2;
5013 if (processing_template_decl)
5014 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5016 return build_new_op (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
5020 build_conditional_expr (ifexp, op1, op2)
5021 tree ifexp, op1, op2;
5023 register tree type1;
5024 register tree type2;
5025 register enum tree_code code1;
5026 register enum tree_code code2;
5027 register tree result_type = NULL_TREE;
5029 /* If second operand is omitted, it is the same as the first one;
5030 make sure it is calculated only once. */
5034 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
5035 ifexp = op1 = save_expr (ifexp);
5038 ifexp = cp_convert (boolean_type_node, ifexp);
5040 if (TREE_CODE (ifexp) == ERROR_MARK)
5041 return error_mark_node;
5043 /* C++: REFERENCE_TYPES must be dereferenced. */
5044 type1 = TREE_TYPE (op1);
5045 code1 = TREE_CODE (type1);
5046 type2 = TREE_TYPE (op2);
5047 code2 = TREE_CODE (type2);
5049 if (code1 == REFERENCE_TYPE)
5051 op1 = convert_from_reference (op1);
5052 type1 = TREE_TYPE (op1);
5053 code1 = TREE_CODE (type1);
5055 if (code2 == REFERENCE_TYPE)
5057 op2 = convert_from_reference (op2);
5058 type2 = TREE_TYPE (op2);
5059 code2 = TREE_CODE (type2);
5062 /* Don't promote the operands separately if they promote
5063 the same way. Return the unpromoted type and let the combined
5064 value get promoted if necessary. */
5066 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
5067 && code2 != ARRAY_TYPE
5068 && code2 != FUNCTION_TYPE
5069 && code2 != METHOD_TYPE)
5073 if (TREE_CONSTANT (ifexp)
5074 && (TREE_CODE (ifexp) == INTEGER_CST
5075 || TREE_CODE (ifexp) == ADDR_EXPR))
5076 return (integer_zerop (ifexp) ? op2 : op1);
5078 if (TREE_CODE (op1) == CONST_DECL)
5079 op1 = DECL_INITIAL (op1);
5080 else if (TREE_READONLY_DECL_P (op1))
5081 op1 = decl_constant_value (op1);
5082 if (TREE_CODE (op2) == CONST_DECL)
5083 op2 = DECL_INITIAL (op2);
5084 else if (TREE_READONLY_DECL_P (op2))
5085 op2 = decl_constant_value (op2);
5087 type1 = cp_build_qualified_type
5088 (type1, (CP_TYPE_QUALS (TREE_TYPE (op1))
5089 | CP_TYPE_QUALS (TREE_TYPE (op2))));
5090 /* ??? This is a kludge to deal with the fact that
5091 we don't sort out integers and enums properly, yet. */
5092 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
5093 if (TREE_TYPE (result) != type1)
5094 result = build1 (NOP_EXPR, type1, result);
5095 /* Expand both sides into the same slot,
5096 hopefully the target of the ?: expression. */
5097 if (TREE_CODE (op1) == TARGET_EXPR && TREE_CODE (op2) == TARGET_EXPR)
5099 tree slot = build (VAR_DECL, TREE_TYPE (result));
5100 layout_decl (slot, 0);
5101 result = build (TARGET_EXPR, TREE_TYPE (result),
5102 slot, result, NULL_TREE, NULL_TREE);
5107 /* They don't match; promote them both and then try to reconcile them.
5108 But don't permit mismatching enum types. */
5109 if (code1 == ENUMERAL_TYPE)
5111 if (code2 == ENUMERAL_TYPE)
5113 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'",
5115 return error_mark_node;
5117 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2)
5118 && type2 != type_promotes_to (type1))
5119 warning ("enumeral and non-enumeral type in conditional expression");
5121 else if (extra_warnings
5122 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1)
5123 && type1 != type_promotes_to (type2))
5124 warning ("enumeral and non-enumeral type in conditional expression");
5126 if (code1 != VOID_TYPE)
5128 op1 = default_conversion (op1);
5129 type1 = TREE_TYPE (op1);
5130 if (TYPE_PTRMEMFUNC_P (type1))
5131 type1 = TYPE_PTRMEMFUNC_FN_TYPE (type1);
5132 code1 = TREE_CODE (type1);
5134 if (code2 != VOID_TYPE)
5136 op2 = default_conversion (op2);
5137 type2 = TREE_TYPE (op2);
5138 if (TYPE_PTRMEMFUNC_P (type2))
5139 type2 = TYPE_PTRMEMFUNC_FN_TYPE (type2);
5140 code2 = TREE_CODE (type2);
5143 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE
5144 && real_lvalue_p (op1) && real_lvalue_p (op2)
5145 && comptypes (type1, type2, COMPARE_BASE | COMPARE_RELAXED))
5147 type1 = build_reference_type (type1);
5148 type2 = build_reference_type (type2);
5149 result_type = common_type (type1, type2);
5150 op1 = convert_to_reference (result_type, op1, CONV_IMPLICIT,
5151 LOOKUP_NORMAL, NULL_TREE);
5152 op2 = convert_to_reference (result_type, op2, CONV_IMPLICIT,
5153 LOOKUP_NORMAL, NULL_TREE);
5155 /* Quickly detect the usual case where op1 and op2 have the same type
5157 else if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5160 result_type = type1;
5163 cp_build_qualified_type (type1,
5164 CP_TYPE_QUALS (TREE_TYPE (op1))
5165 | CP_TYPE_QUALS (TREE_TYPE (op2)));
5167 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
5168 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
5170 result_type = common_type (type1, type2);
5172 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5174 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
5175 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
5176 result_type = void_type_node;
5178 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op2))
5179 result_type = qualify_type (type1, type2);
5180 else if (code2 == POINTER_TYPE && null_ptr_cst_p (op1))
5181 result_type = qualify_type (type2, type1);
5182 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5184 if (comp_target_types (type1, type2, 1))
5185 result_type = common_type (type1, type2);
5186 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
5188 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
5189 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5190 result_type = qualify_type (type1, type2);
5192 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
5194 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
5195 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5196 result_type = qualify_type (type2, type1);
5199 else if (same_or_base_type_p (type2, type1))
5200 result_type = type2;
5201 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
5202 && IS_AGGR_TYPE (TREE_TYPE (type2))
5203 && (result_type = common_base_type (TREE_TYPE (type1),
5204 TREE_TYPE (type2))))
5206 if (result_type == error_mark_node)
5208 cp_error ("common base type of types `%T' and `%T' is ambiguous",
5209 TREE_TYPE (type1), TREE_TYPE (type2));
5210 result_type = ptr_type_node;
5215 && result_type != TREE_TYPE (type1)
5216 && result_type != TREE_TYPE (type2))
5217 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
5218 type1, type2, result_type);
5220 result_type = build_pointer_type (result_type);
5225 pedwarn ("pointer type mismatch in conditional expression");
5226 result_type = ptr_type_node;
5229 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5231 pedwarn ("pointer/integer type mismatch in conditional expression");
5232 result_type = type1;
5234 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5236 pedwarn ("pointer/integer type mismatch in conditional expression");
5237 result_type = type2;
5239 if (type2 == unknown_type_node)
5240 result_type = type1;
5241 else if (type1 == unknown_type_node)
5242 result_type = type2;
5246 /* The match does not look good. If either is
5247 an aggregate value, try converting to a scalar type. */
5248 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
5250 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'",
5252 return error_mark_node;
5254 /* Warning: this code assumes that conversion between cv-variants of
5255 a type is done using NOP_EXPRs. */
5256 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
5258 /* There are other types besides pointers and records. */
5260 if (code2 == POINTER_TYPE)
5261 tmp = build_pointer_type
5262 (cp_build_qualified_type (TREE_TYPE (type2),
5264 | TYPE_QUAL_VOLATILE
5265 | TYPE_QUAL_RESTRICT));
5268 tmp = build_type_conversion (tmp, op1, 0);
5269 if (tmp == NULL_TREE)
5271 cp_error ("incompatible types `%T' and `%T' in `?:'",
5273 return error_mark_node;
5275 if (tmp == error_mark_node)
5276 error ("ambiguous pointer conversion");
5279 result_type = common_type (type2, TREE_TYPE (tmp));
5282 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
5285 if (code1 == POINTER_TYPE)
5286 tmp = build_pointer_type
5287 (cp_build_qualified_type (TREE_TYPE (type1),
5289 | TYPE_QUAL_VOLATILE
5290 | TYPE_QUAL_RESTRICT));
5294 tmp = build_type_conversion (tmp, op2, 0);
5295 if (tmp == NULL_TREE)
5297 cp_error ("incompatible types `%T' and `%T' in `?:'",
5299 return error_mark_node;
5301 if (tmp == error_mark_node)
5302 error ("ambiguous pointer conversion");
5305 result_type = common_type (type1, TREE_TYPE (tmp));
5308 else if (flag_cond_mismatch)
5309 result_type = void_type_node;
5312 error ("type mismatch in conditional expression");
5313 return error_mark_node;
5317 if (TREE_CODE (result_type) == POINTER_TYPE
5318 && TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
5319 result_type = build_ptrmemfunc_type (result_type);
5321 if (result_type != TREE_TYPE (op1))
5322 op1 = convert_for_initialization
5323 (NULL_TREE, result_type, op1, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5324 if (result_type != TREE_TYPE (op2))
5325 op2 = convert_for_initialization
5326 (NULL_TREE, result_type, op2, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5328 if (TREE_CODE (ifexp) == INTEGER_CST)
5329 return integer_zerop (ifexp) ? op2 : op1;
5331 return convert_from_reference
5332 (fold (build (COND_EXPR, result_type, ifexp, op1, op2)));
5335 /* Handle overloading of the ',' operator when needed. Otherwise,
5336 this function just builds an expression list. */
5339 build_x_compound_expr (list)
5342 tree rest = TREE_CHAIN (list);
5345 if (processing_template_decl)
5346 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5348 if (rest == NULL_TREE)
5349 return build_compound_expr (list);
5351 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5352 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5354 return build_x_compound_expr (expr_tree_cons (NULL_TREE, result,
5355 TREE_CHAIN (rest)));
5357 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5359 /* the left-hand operand of a comma expression is like an expression
5360 statement: we should warn if it doesn't have any side-effects,
5361 unless it was explicitly cast to (void). */
5362 if ((extra_warnings || warn_unused)
5363 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5364 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5365 warning("left-hand operand of comma expression has no effect");
5367 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5368 else if (warn_unused)
5369 warn_if_unused_value (TREE_VALUE(list));
5372 return build_compound_expr
5373 (expr_tree_cons (NULL_TREE, TREE_VALUE (list),
5374 build_expr_list (NULL_TREE,
5375 build_x_compound_expr (rest))));
5378 /* Given a list of expressions, return a compound expression
5379 that performs them all and returns the value of the last of them. */
5382 build_compound_expr (list)
5388 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5389 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5391 if (TREE_CHAIN (list) == 0)
5393 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5394 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5395 if (TREE_CODE (list) == NOP_EXPR
5396 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5397 list = TREE_OPERAND (list, 0);
5399 /* Convert arrays to pointers. */
5400 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5401 return default_conversion (TREE_VALUE (list));
5403 return TREE_VALUE (list);
5406 first = TREE_VALUE (list);
5407 first = require_complete_type_in_void (first);
5408 if (first == error_mark_node)
5409 return error_mark_node;
5411 rest = build_compound_expr (TREE_CHAIN (list));
5412 if (rest == error_mark_node)
5413 return error_mark_node;
5415 /* When pedantic, a compound expression cannot be a constant expression. */
5416 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5419 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5420 break_out_cleanups (first), rest);
5424 build_static_cast (type, expr)
5430 if (type == error_mark_node || expr == error_mark_node)
5431 return error_mark_node;
5433 if (TREE_CODE (expr) == OFFSET_REF)
5434 expr = resolve_offset_ref (expr);
5436 if (processing_template_decl)
5438 tree t = build_min (STATIC_CAST_EXPR, copy_to_permanent (type),
5443 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5444 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5445 if (TREE_CODE (type) != REFERENCE_TYPE
5446 && TREE_CODE (expr) == NOP_EXPR
5447 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5448 expr = TREE_OPERAND (expr, 0);
5450 if (TREE_CODE (type) == VOID_TYPE)
5451 return build1 (CONVERT_EXPR, type, expr);
5453 if (TREE_CODE (type) == REFERENCE_TYPE)
5454 return (convert_from_reference
5455 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5456 LOOKUP_COMPLAIN, NULL_TREE)));
5458 if (IS_AGGR_TYPE (type))
5459 return build_cplus_new
5460 (type, (build_method_call
5461 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5462 TYPE_BINFO (type), LOOKUP_NORMAL)));
5464 expr = decay_conversion (expr);
5465 intype = TREE_TYPE (expr);
5467 /* FIXME handle casting to array type. */
5470 if (can_convert_arg (type, intype, expr))
5472 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5475 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5476 && at_least_as_qualified_p (TREE_TYPE (type),
5478 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5479 && ! TREE_VIA_VIRTUAL (binfo))
5482 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5484 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5485 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))))
5486 && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type)),
5487 TREE_TYPE (TREE_TYPE (intype)))
5488 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5489 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5490 && ! TREE_VIA_VIRTUAL (binfo))
5493 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5494 && TREE_CODE (type) != ARRAY_TYPE
5495 && TREE_CODE (type) != FUNCTION_TYPE
5496 && can_convert (intype, type))
5500 return build_c_cast (type, expr);
5502 cp_error ("static_cast from `%T' to `%T'", intype, type);
5503 return error_mark_node;
5507 build_reinterpret_cast (type, expr)
5512 if (type == error_mark_node || expr == error_mark_node)
5513 return error_mark_node;
5515 if (TREE_CODE (expr) == OFFSET_REF)
5516 expr = resolve_offset_ref (expr);
5518 if (processing_template_decl)
5520 tree t = build_min (REINTERPRET_CAST_EXPR,
5521 copy_to_permanent (type), expr);
5525 if (TREE_CODE (type) != REFERENCE_TYPE)
5527 expr = decay_conversion (expr);
5529 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5530 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5531 if (TREE_CODE (expr) == NOP_EXPR
5532 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5533 expr = TREE_OPERAND (expr, 0);
5536 intype = TREE_TYPE (expr);
5538 if (TREE_CODE (type) == REFERENCE_TYPE)
5540 if (! real_lvalue_p (expr))
5542 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5543 return error_mark_node;
5545 expr = build_unary_op (ADDR_EXPR, expr, 0);
5546 if (expr != error_mark_node)
5547 expr = build_reinterpret_cast
5548 (build_pointer_type (TREE_TYPE (type)), expr);
5549 if (expr != error_mark_node)
5550 expr = build_indirect_ref (expr, 0);
5553 else if (same_type_p (TYPE_MAIN_VARIANT (intype),
5554 TYPE_MAIN_VARIANT (type)))
5555 return build_static_cast (type, expr);
5557 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5558 || TREE_CODE (intype) == ENUMERAL_TYPE))
5560 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5562 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5563 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5566 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5567 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5569 if (TREE_READONLY_DECL_P (expr))
5570 expr = decl_constant_value (expr);
5571 return fold (build1 (NOP_EXPR, type, expr));
5573 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5574 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5576 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5577 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5580 if (TREE_READONLY_DECL_P (expr))
5581 expr = decl_constant_value (expr);
5582 return fold (build1 (NOP_EXPR, type, expr));
5584 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5585 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5587 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5588 if (TREE_READONLY_DECL_P (expr))
5589 expr = decl_constant_value (expr);
5590 return fold (build1 (NOP_EXPR, type, expr));
5594 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5595 return error_mark_node;
5598 return cp_convert (type, expr);
5602 build_const_cast (type, expr)
5607 if (type == error_mark_node || expr == error_mark_node)
5608 return error_mark_node;
5610 if (TREE_CODE (expr) == OFFSET_REF)
5611 expr = resolve_offset_ref (expr);
5613 if (processing_template_decl)
5615 tree t = build_min (CONST_CAST_EXPR, copy_to_permanent (type),
5620 if (TREE_CODE (type) != REFERENCE_TYPE)
5622 expr = decay_conversion (expr);
5624 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5625 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5626 if (TREE_CODE (expr) == NOP_EXPR
5627 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5628 expr = TREE_OPERAND (expr, 0);
5631 intype = TREE_TYPE (expr);
5633 if (same_type_p (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type)))
5634 return build_static_cast (type, expr);
5635 else if (TREE_CODE (type) == REFERENCE_TYPE)
5637 if (! real_lvalue_p (expr))
5639 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5640 return error_mark_node;
5643 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5645 expr = build_unary_op (ADDR_EXPR, expr, 0);
5646 expr = build1 (NOP_EXPR, type, expr);
5647 return convert_from_reference (expr);
5650 else if (TREE_CODE (type) == POINTER_TYPE
5651 && TREE_CODE (intype) == POINTER_TYPE
5652 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5653 return cp_convert (type, expr);
5655 cp_error ("const_cast from `%T' to `%T'", intype, type);
5656 return error_mark_node;
5659 /* Build an expression representing a cast to type TYPE of expression EXPR.
5661 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5662 when doing the cast. */
5665 build_c_cast (type, expr)
5668 register tree value = expr;
5671 if (type == error_mark_node || expr == error_mark_node)
5672 return error_mark_node;
5674 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5675 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5676 if (TREE_CODE (type) != REFERENCE_TYPE
5677 && TREE_CODE (value) == NOP_EXPR
5678 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5679 value = TREE_OPERAND (value, 0);
5681 if (TREE_TYPE (expr)
5682 && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
5683 && TREE_CODE (type) != OFFSET_TYPE)
5684 value = resolve_offset_ref (value);
5686 if (TREE_CODE (type) == ARRAY_TYPE)
5688 /* Allow casting from T1* to T2[] because Cfront allows it.
5689 NIHCL uses it. It is not valid ANSI C however, and hence, not
5691 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5694 pedwarn ("ANSI C++ forbids casting to an array type");
5695 type = build_pointer_type (TREE_TYPE (type));
5699 error ("ANSI C++ forbids casting to an array type");
5700 return error_mark_node;
5704 if (TREE_CODE (type) == FUNCTION_TYPE
5705 || TREE_CODE (type) == METHOD_TYPE)
5707 cp_error ("casting to function type `%T'", type);
5708 return error_mark_node;
5711 if (IS_SIGNATURE (type))
5713 error ("cast specifies signature type");
5714 return error_mark_node;
5717 if (processing_template_decl)
5719 tree t = build_min (CAST_EXPR, type,
5720 min_tree_cons (NULL_TREE, value, NULL_TREE));
5724 /* Convert functions and arrays to pointers and
5725 convert references to their expanded types,
5726 but don't convert any other types. If, however, we are
5727 casting to a class type, there's no reason to do this: the
5728 cast will only succeed if there is a converting constructor,
5729 and the default conversions will be done at that point. In
5730 fact, doing the default conversion here is actually harmful
5734 struct S { S(const A&); };
5736 since we don't want the array-to-pointer conversion done. */
5737 if (!IS_AGGR_TYPE (type))
5739 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5740 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5741 /* Don't do the default conversion on a ->* expression. */
5742 && ! (TREE_CODE (type) == POINTER_TYPE
5743 && bound_pmf_p (value)))
5744 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5745 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5746 value = default_conversion (value);
5748 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5749 /* However, even for class types, we still need to strip away
5750 the reference type, since the call to convert_force below
5751 does not expect the input expression to be of reference
5753 value = convert_from_reference (value);
5755 otype = TREE_TYPE (value);
5757 /* Optionally warn about potentially worrisome casts. */
5760 && TREE_CODE (type) == POINTER_TYPE
5761 && TREE_CODE (otype) == POINTER_TYPE
5762 && !at_least_as_qualified_p (TREE_TYPE (type),
5764 cp_warning ("cast discards qualifiers from pointer target type");
5766 /* Warn about possible alignment problems. */
5767 if (STRICT_ALIGNMENT && warn_cast_align
5768 && TREE_CODE (type) == POINTER_TYPE
5769 && TREE_CODE (otype) == POINTER_TYPE
5770 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5771 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5772 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5773 warning ("cast increases required alignment of target type");
5776 /* We should see about re-enabling these, they seem useful to
5778 if (TREE_CODE (type) == INTEGER_TYPE
5779 && TREE_CODE (otype) == POINTER_TYPE
5780 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5781 warning ("cast from pointer to integer of different size");
5783 if (TREE_CODE (type) == POINTER_TYPE
5784 && TREE_CODE (otype) == INTEGER_TYPE
5785 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5786 /* Don't warn about converting 0 to pointer,
5787 provided the 0 was explicit--not cast or made by folding. */
5788 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5789 warning ("cast to pointer from integer of different size");
5792 if (TREE_CODE (type) == VOID_TYPE)
5794 value = require_complete_type_in_void (value);
5795 if (value != error_mark_node)
5796 value = build1 (CONVERT_EXPR, void_type_node, value);
5798 else if (TREE_CODE (type) == REFERENCE_TYPE)
5799 value = (convert_from_reference
5800 (convert_to_reference (type, value, CONV_C_CAST,
5801 LOOKUP_COMPLAIN, NULL_TREE)));
5806 if (TREE_READONLY_DECL_P (value))
5807 value = decl_constant_value (value);
5810 value = convert_force (type, value, CONV_C_CAST);
5812 /* Ignore any integer overflow caused by the cast. */
5813 if (TREE_CODE (value) == INTEGER_CST)
5815 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5816 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5820 /* Always produce some operator for an explicit cast,
5821 so we can tell (for -pedantic) that the cast is no lvalue. */
5822 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5823 && real_lvalue_p (value))
5824 value = non_lvalue (value);
5829 /* Build an assignment expression of lvalue LHS from value RHS.
5830 MODIFYCODE is the code for a binary operator that we use
5831 to combine the old value of LHS with RHS to get the new value.
5832 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5834 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5837 build_modify_expr (lhs, modifycode, rhs)
5839 enum tree_code modifycode;
5842 register tree result;
5844 tree lhstype = TREE_TYPE (lhs);
5845 tree olhstype = lhstype;
5848 /* Avoid duplicate error messages from operands that had errors. */
5849 if (lhs == error_mark_node || rhs == error_mark_node)
5850 return error_mark_node;
5852 /* Types that aren't fully specified cannot be used in assignments. */
5853 lhs = require_complete_type (lhs);
5857 /* Handle assignment to signature pointers/refs. */
5859 if (TYPE_LANG_SPECIFIC (lhstype)
5860 && (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5862 return build_signature_pointer_constructor (lhs, rhs);
5865 /* Handle control structure constructs used as "lvalues". */
5867 switch (TREE_CODE (lhs))
5869 /* Handle --foo = 5; as these are valid constructs in C++ */
5870 case PREDECREMENT_EXPR:
5871 case PREINCREMENT_EXPR:
5872 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5873 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5874 stabilize_reference (TREE_OPERAND (lhs, 0)),
5875 TREE_OPERAND (lhs, 1));
5876 return build (COMPOUND_EXPR, lhstype,
5878 build_modify_expr (TREE_OPERAND (lhs, 0),
5881 /* Handle (a, b) used as an "lvalue". */
5883 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5885 if (newrhs == error_mark_node)
5886 return error_mark_node;
5887 return build (COMPOUND_EXPR, lhstype,
5888 TREE_OPERAND (lhs, 0), newrhs);
5891 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5892 if (newrhs == error_mark_node)
5893 return error_mark_node;
5894 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5896 /* Handle (a ? b : c) used as an "lvalue". */
5898 rhs = save_expr (rhs);
5900 /* Produce (a ? (b = rhs) : (c = rhs))
5901 except that the RHS goes through a save-expr
5902 so the code to compute it is only emitted once. */
5904 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5905 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5907 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5909 if (cond == error_mark_node)
5911 /* Make sure the code to compute the rhs comes out
5912 before the split. */
5913 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5914 /* Case to void to suppress warning
5915 from warn_if_unused_value. */
5916 cp_convert (void_type_node, rhs), cond);
5923 if (TREE_CODE (lhs) == OFFSET_REF)
5925 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5927 /* Static class member? */
5928 tree member = TREE_OPERAND (lhs, 1);
5929 if (TREE_CODE (member) == VAR_DECL)
5933 compiler_error ("invalid static class member");
5934 return error_mark_node;
5938 lhs = resolve_offset_ref (lhs);
5940 olhstype = lhstype = TREE_TYPE (lhs);
5943 if (lhs == error_mark_node)
5946 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5947 && modifycode != INIT_EXPR)
5949 lhs = convert_from_reference (lhs);
5950 olhstype = lhstype = TREE_TYPE (lhs);
5953 /* If a binary op has been requested, combine the old LHS value with the RHS
5954 producing the value we should actually store into the LHS. */
5956 if (modifycode == INIT_EXPR)
5958 if (! IS_AGGR_TYPE (lhstype))
5959 /* Do the default thing */;
5962 result = build_method_call (lhs, ctor_identifier,
5963 build_expr_list (NULL_TREE, rhs),
5964 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5965 if (result == NULL_TREE)
5966 return error_mark_node;
5970 else if (modifycode == NOP_EXPR)
5972 /* `operator=' is not an inheritable operator. */
5973 if (! IS_AGGR_TYPE (lhstype))
5974 /* Do the default thing */;
5977 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5978 lhs, rhs, make_node (NOP_EXPR));
5979 if (result == NULL_TREE)
5980 return error_mark_node;
5985 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5987 my_friendly_abort (978652);
5991 lhs = stabilize_reference (lhs);
5992 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5993 if (newrhs == error_mark_node)
5995 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5996 TREE_TYPE (lhs), TREE_TYPE (rhs));
5997 return error_mark_node;
6001 /* Handle a cast used as an "lvalue".
6002 We have already performed any binary operator using the value as cast.
6003 Now convert the result to the cast type of the lhs,
6004 and then true type of the lhs and store it there;
6005 then convert result back to the cast type to be the value
6006 of the assignment. */
6008 switch (TREE_CODE (lhs))
6013 case FIX_TRUNC_EXPR:
6014 case FIX_FLOOR_EXPR:
6015 case FIX_ROUND_EXPR:
6017 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6018 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
6019 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
6020 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
6021 newrhs = default_conversion (newrhs);
6023 tree inner_lhs = TREE_OPERAND (lhs, 0);
6026 /* WP 5.4.1: The result is an lvalue if T is a reference type,
6027 otherwise the result is an rvalue. */
6028 if (! lvalue_p (lhs))
6029 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
6031 result = build_modify_expr (inner_lhs, NOP_EXPR,
6032 cp_convert (TREE_TYPE (inner_lhs),
6033 cp_convert (lhstype, newrhs)));
6034 if (result == error_mark_node)
6036 return cp_convert (TREE_TYPE (lhs), result);
6043 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
6044 Reject anything strange now. */
6046 if (!lvalue_or_else (lhs, "assignment"))
6047 return error_mark_node;
6049 GNU_xref_assign (lhs);
6051 /* Warn about storing in something that is `const'. */
6052 /* For C++, don't warn if this is initialization. */
6053 if (modifycode != INIT_EXPR
6054 /* For assignment to `const' signature pointer/reference fields,
6055 don't warn either, we already printed a better message before. */
6056 && ! (TREE_CODE (lhs) == COMPONENT_REF
6057 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
6058 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
6059 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
6060 /* Functions are not modifiable, even though they are
6062 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
6063 || ((TREE_CODE (lhstype) == RECORD_TYPE
6064 || TREE_CODE (lhstype) == UNION_TYPE)
6065 && C_TYPE_FIELDS_READONLY (lhstype))
6066 || (TREE_CODE (lhstype) == REFERENCE_TYPE
6067 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
6068 readonly_error (lhs, "assignment", 0);
6070 /* If storing into a structure or union member,
6071 it has probably been given type `int'.
6072 Compute the type that would go with
6073 the actual amount of storage the member occupies. */
6075 if (TREE_CODE (lhs) == COMPONENT_REF
6076 && (TREE_CODE (lhstype) == INTEGER_TYPE
6077 || TREE_CODE (lhstype) == REAL_TYPE
6078 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6080 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6082 /* If storing in a field that is in actuality a short or narrower
6083 than one, we must store in the field in its actual type. */
6085 if (lhstype != TREE_TYPE (lhs))
6087 lhs = copy_node (lhs);
6088 TREE_TYPE (lhs) = lhstype;
6092 /* check to see if there is an assignment to `this' */
6093 if (lhs == current_class_ptr)
6095 if (flag_this_is_variable > 0
6096 && DECL_NAME (current_function_decl) != NULL_TREE
6097 && (DECL_NAME (current_function_decl)
6098 != constructor_name (current_class_type)))
6099 warning ("assignment to `this' not in constructor or destructor");
6100 current_function_just_assigned_this = 1;
6103 if (modifycode != INIT_EXPR)
6105 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
6106 modifycode = NOP_EXPR;
6107 /* Reference-bashing */
6108 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
6110 tree tmp = convert_from_reference (lhs);
6111 lhstype = TREE_TYPE (tmp);
6112 if (TYPE_SIZE (lhstype) == 0)
6114 incomplete_type_error (lhs, lhstype);
6115 return error_mark_node;
6120 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
6122 tree tmp = convert_from_reference (newrhs);
6123 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
6125 incomplete_type_error (newrhs, TREE_TYPE (tmp));
6126 return error_mark_node;
6132 if (TREE_SIDE_EFFECTS (lhs))
6133 lhs = stabilize_reference (lhs);
6134 if (TREE_SIDE_EFFECTS (newrhs))
6135 newrhs = stabilize_reference (newrhs);
6137 /* Convert new value to destination type. */
6139 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6143 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
6145 cp_error ("incompatible types in assignment of `%T' to `%T'",
6146 TREE_TYPE (rhs), lhstype);
6147 return error_mark_node;
6150 /* Allow array assignment in compiler-generated code. */
6151 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
6152 pedwarn ("ANSI C++ forbids assignment of arrays");
6154 /* Have to wrap this in RTL_EXPR for two cases:
6155 in base or member initialization and if we
6156 are a branch of a ?: operator. Since we
6157 can't easily know the latter, just do it always. */
6159 result = make_node (RTL_EXPR);
6161 TREE_TYPE (result) = void_type_node;
6162 do_pending_stack_adjust ();
6163 start_sequence_for_rtl_expr (result);
6165 /* As a matter of principle, `start_sequence' should do this. */
6168 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6169 ? 1 + (modifycode != INIT_EXPR): 0;
6170 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
6173 do_pending_stack_adjust ();
6175 TREE_SIDE_EFFECTS (result) = 1;
6176 RTL_EXPR_SEQUENCE (result) = get_insns ();
6177 RTL_EXPR_RTL (result) = const0_rtx;
6182 if (modifycode == INIT_EXPR)
6184 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
6185 "assignment", NULL_TREE, 0);
6186 if (lhs == DECL_RESULT (current_function_decl))
6188 if (DECL_INITIAL (lhs))
6189 warning ("return value from function receives multiple initializations");
6190 DECL_INITIAL (lhs) = newrhs;
6195 /* Avoid warnings on enum bit fields. */
6196 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
6197 && TREE_CODE (lhstype) == INTEGER_TYPE)
6199 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
6201 newrhs = convert_force (lhstype, newrhs, 0);
6204 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
6206 if (TREE_CODE (newrhs) == CALL_EXPR
6207 && TYPE_NEEDS_CONSTRUCTING (lhstype))
6208 newrhs = build_cplus_new (lhstype, newrhs);
6210 /* Can't initialize directly from a TARGET_EXPR, since that would
6211 cause the lhs to be constructed twice, and possibly result in
6212 accidental self-initialization. So we force the TARGET_EXPR to be
6213 expanded without a target. */
6214 if (TREE_CODE (newrhs) == TARGET_EXPR)
6215 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6216 TREE_OPERAND (newrhs, 0));
6219 if (newrhs == error_mark_node)
6220 return error_mark_node;
6222 if (TREE_CODE (newrhs) == COND_EXPR)
6225 tree cond = TREE_OPERAND (newrhs, 0);
6227 if (TREE_SIDE_EFFECTS (lhs))
6228 cond = build_compound_expr (tree_cons
6230 build_expr_list (NULL_TREE, cond)));
6232 /* Cannot have two identical lhs on this one tree (result) as preexpand
6233 calls will rip them out and fill in RTL for them, but when the
6234 rtl is generated, the calls will only be in the first side of the
6235 condition, not on both, or before the conditional jump! (mrs) */
6236 lhs1 = break_out_calls (lhs);
6239 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6240 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6241 lhstype, lhs, newrhs);
6244 tree result_type = TREE_TYPE (newrhs);
6245 /* We have to convert each arm to the proper type because the
6246 types may have been munged by constant folding. */
6248 = build (COND_EXPR, result_type, cond,
6249 build_modify_expr (lhs, modifycode,
6250 cp_convert (result_type,
6251 TREE_OPERAND (newrhs, 1))),
6252 build_modify_expr (lhs1, modifycode,
6253 cp_convert (result_type,
6254 TREE_OPERAND (newrhs, 2))));
6258 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6259 lhstype, lhs, newrhs);
6261 TREE_SIDE_EFFECTS (result) = 1;
6263 /* If we got the LHS in a different type for storing in,
6264 convert the result back to the nominal type of LHS
6265 so that the value we return always has the same type
6266 as the LHS argument. */
6268 if (olhstype == TREE_TYPE (result))
6270 /* Avoid warnings converting integral types back into enums
6271 for enum bit fields. */
6272 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6273 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6275 result = build (COMPOUND_EXPR, olhstype, result, olhs);
6276 TREE_NO_UNUSED_WARNING (result) = 1;
6279 return convert_for_assignment (olhstype, result, "assignment",
6284 build_x_modify_expr (lhs, modifycode, rhs)
6286 enum tree_code modifycode;
6289 if (processing_template_decl)
6290 return build_min_nt (MODOP_EXPR, lhs,
6291 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6293 if (modifycode != NOP_EXPR)
6295 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6296 make_node (modifycode));
6300 return build_modify_expr (lhs, modifycode, rhs);
6304 /* Get difference in deltas for different pointer to member function
6305 types. Return integer_zero_node, if FROM cannot be converted to a
6306 TO type. If FORCE is true, then allow reverse conversions as well. */
6309 get_delta_difference (from, to, force)
6313 tree delta = integer_zero_node;
6319 /* Should get_base_distance here, so we can check if any thing along the
6320 path is virtual, and we need to make sure we stay
6321 inside the real binfos when going through virtual bases.
6322 Maybe we should replace virtual bases with
6323 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6324 binfo = get_binfo (from, to, 1);
6325 if (binfo == error_mark_node)
6327 error (" in pointer to member function conversion");
6334 error_not_base_type (from, to);
6335 error (" in pointer to member conversion");
6338 binfo = get_binfo (to, from, 1);
6339 if (binfo == 0 || binfo == error_mark_node)
6341 if (TREE_VIA_VIRTUAL (binfo))
6343 binfo = binfo_member (BINFO_TYPE (binfo),
6344 CLASSTYPE_VBASECLASSES (from));
6345 cp_warning ("pointer to member cast to virtual base `%T'",
6346 BINFO_TYPE (binfo));
6347 warning (" will only work if you are very careful");
6349 delta = BINFO_OFFSET (binfo);
6350 delta = cp_convert (ptrdiff_type_node, delta);
6352 return build_binary_op (MINUS_EXPR,
6357 if (TREE_VIA_VIRTUAL (binfo))
6361 cp_warning ("pointer to member cast from virtual base `%T'",
6362 BINFO_TYPE (binfo));
6363 warning (" will only work if you are very careful");
6366 cp_error ("pointer to member conversion from virtual base `%T'",
6367 BINFO_TYPE (binfo));
6370 return BINFO_OFFSET (binfo);
6374 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6375 tree type, delta, idx, pfn, delta2;
6380 /* This is the old way we did it. We want to avoid calling
6381 digest_init, so that it can give an error if we use { } when
6382 initializing a pointer to member function. */
6386 u = build_nt (CONSTRUCTOR, NULL_TREE,
6387 expr_tree_cons (pfn_identifier, pfn, NULL_TREE));
6391 u = build_nt (CONSTRUCTOR, NULL_TREE,
6392 expr_tree_cons (delta2_identifier, delta2, NULL_TREE));
6395 u = build_nt (CONSTRUCTOR, NULL_TREE,
6396 expr_tree_cons (NULL_TREE, delta,
6397 expr_tree_cons (NULL_TREE, idx,
6398 expr_tree_cons (NULL_TREE, u, NULL_TREE))));
6400 return digest_init (type, u, (tree*)0);
6402 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6404 int allconstant, allsimple;
6406 delta_field = TYPE_FIELDS (type);
6407 idx_field = TREE_CHAIN (delta_field);
6408 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6409 subtype = TREE_TYPE (pfn_or_delta2_field);
6410 pfn_field = TYPE_FIELDS (subtype);
6411 delta2_field = TREE_CHAIN (pfn_field);
6415 allconstant = TREE_CONSTANT (pfn);
6416 allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6417 u = expr_tree_cons (pfn_field, pfn, NULL_TREE);
6421 delta2 = convert_and_check (delta_type_node, delta2);
6422 allconstant = TREE_CONSTANT (delta2);
6423 allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6424 u = expr_tree_cons (delta2_field, delta2, NULL_TREE);
6427 delta = convert_and_check (delta_type_node, delta);
6428 idx = convert_and_check (delta_type_node, idx);
6430 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6431 allsimple = allsimple
6432 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6433 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6435 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6436 u = expr_tree_cons (delta_field, delta,
6437 expr_tree_cons (idx_field, idx,
6438 expr_tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6439 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6440 TREE_CONSTANT (u) = allconstant;
6441 TREE_STATIC (u) = allconstant && allsimple;
6446 /* Build a constructor for a pointer to member function. It can be
6447 used to initialize global variables, local variable, or used
6448 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6451 If FORCE is non-zero, then force this conversion, even if
6452 we would rather not do it. Usually set when using an explicit
6455 Return error_mark_node, if something goes wrong. */
6458 build_ptrmemfunc (type, pfn, force)
6462 tree idx = integer_zero_node;
6463 tree delta = integer_zero_node;
6464 tree delta2 = integer_zero_node;
6465 tree npfn = NULL_TREE;
6468 /* Handle multiple conversions of pointer to member functions. */
6469 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6471 tree ndelta, ndelta2;
6475 /* Is is already the right type? */
6476 if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
6479 pfn_type = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn));
6481 && comp_target_types (type, pfn_type, 1) != 1)
6482 cp_error ("conversion to `%T' from `%T'", type, pfn_type);
6484 ndelta = cp_convert (ptrdiff_type_node, build_component_ref (pfn, delta_identifier, NULL_TREE, 0));
6485 ndelta2 = cp_convert (ptrdiff_type_node, DELTA2_FROM_PTRMEMFUNC (pfn));
6486 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6488 n = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (pfn_type)),
6489 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6492 delta = build_binary_op (PLUS_EXPR, ndelta, n, 1);
6493 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n, 1);
6494 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6496 e2 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx,
6499 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6500 npfn = build1 (NOP_EXPR, type, pfn);
6501 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6503 e3 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx, npfn,
6505 return build_conditional_expr (e1, e2, e3);
6508 /* Handle null pointer to member function conversions. */
6509 if (integer_zerop (pfn))
6511 pfn = build_c_cast (type, integer_zero_node);
6512 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type),
6513 integer_zero_node, integer_zero_node,
6517 if (type_unknown_p (pfn))
6518 return instantiate_type (type, pfn, 1);
6520 fn = TREE_OPERAND (pfn, 0);
6521 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6522 npfn = make_node (PTRMEM_CST);
6523 TREE_TYPE (npfn) = build_ptrmemfunc_type (type);
6524 PTRMEM_CST_MEMBER (npfn) = fn;
6528 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6532 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6539 tree type = TREE_TYPE (cst);
6540 tree fn = PTRMEM_CST_MEMBER (cst);
6542 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6545 = get_delta_difference (TYPE_METHOD_BASETYPE
6547 TYPE_PTRMEMFUNC_OBJECT_TYPE (type),
6549 if (!DECL_VIRTUAL_P (fn))
6551 *idx = size_binop (MINUS_EXPR, integer_zero_node,
6553 *pfn = build_addr_func (fn);
6554 if (!same_type_p (TYPE_METHOD_BASETYPE (TREE_TYPE (fn)),
6555 TYPE_PTRMEMFUNC_OBJECT_TYPE (type)))
6556 *pfn = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
6558 *delta2 = NULL_TREE;
6562 *idx = size_binop (PLUS_EXPR, DECL_VINDEX (fn),
6565 *delta2 = get_binfo (DECL_CONTEXT (fn),
6566 DECL_CLASS_CONTEXT (fn),
6568 *delta2 = get_vfield_offset (*delta2);
6569 *delta2 = size_binop (PLUS_EXPR, *delta2,
6570 build_binary_op (PLUS_EXPR,
6577 /* Return an expression for DELTA2 from the pointer-to-member function
6581 delta2_from_ptrmemfunc (t)
6584 if (TREE_CODE (t) == PTRMEM_CST)
6591 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6596 return (build_component_ref
6597 (build_component_ref (t,
6598 pfn_or_delta2_identifier, NULL_TREE,
6600 delta2_identifier, NULL_TREE, 0));
6603 /* Return an expression for PFN from the pointer-to-member function
6607 pfn_from_ptrmemfunc (t)
6610 if (TREE_CODE (t) == PTRMEM_CST)
6617 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6622 return (build_component_ref
6623 (build_component_ref (t,
6624 pfn_or_delta2_identifier, NULL_TREE,
6626 pfn_identifier, NULL_TREE, 0));
6629 /* Convert value RHS to type TYPE as preparation for an assignment
6630 to an lvalue of type TYPE.
6631 The real work of conversion is done by `convert'.
6632 The purpose of this function is to generate error messages
6633 for assignments that are not allowed in C.
6634 ERRTYPE is a string to use in error messages:
6635 "assignment", "return", etc.
6637 C++: attempts to allow `convert' to find conversions involving
6638 implicit type conversion between aggregate and scalar types
6639 as per 8.5.6 of C++ manual. Does not randomly dereference
6640 pointers to aggregates! */
6643 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6645 const char *errtype;
6649 register enum tree_code codel = TREE_CODE (type);
6650 register tree rhstype;
6651 register enum tree_code coder = TREE_CODE (TREE_TYPE (rhs));
6653 /* Issue warnings about peculiar, but legal, uses of NULL. */
6654 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6655 cp_warning ("converting NULL to non-pointer type");
6657 if (coder == ERROR_MARK)
6658 return error_mark_node;
6660 if (codel == OFFSET_TYPE)
6662 type = TREE_TYPE (type);
6663 codel = TREE_CODE (type);
6666 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6667 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6668 rhs = TREE_OPERAND (rhs, 0);
6670 if (rhs == error_mark_node)
6671 return error_mark_node;
6673 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6674 return error_mark_node;
6676 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6678 rhs = resolve_offset_ref (rhs);
6679 if (rhs == error_mark_node)
6680 return error_mark_node;
6681 rhstype = TREE_TYPE (rhs);
6682 coder = TREE_CODE (rhstype);
6685 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6686 || is_overloaded_fn (rhs))
6687 rhs = default_conversion (rhs);
6688 else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6689 rhs = convert_from_reference (rhs);
6691 /* If rhs is some sort of overloaded function, ocp_convert will either
6692 do the right thing or complain; we don't need to check anything else.
6693 So just hand off. */
6694 if (type_unknown_p (rhs))
6695 return ocp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6697 rhstype = TREE_TYPE (rhs);
6698 coder = TREE_CODE (rhstype);
6700 /* This should no longer change types on us. */
6701 if (TREE_CODE (rhs) == CONST_DECL)
6702 rhs = DECL_INITIAL (rhs);
6703 else if (TREE_READONLY_DECL_P (rhs))
6704 rhs = decl_constant_value (rhs);
6706 if (same_type_p (type, rhstype))
6708 overflow_warning (rhs);
6712 if (coder == VOID_TYPE)
6714 error ("void value not ignored as it ought to be");
6715 return error_mark_node;
6717 /* Arithmetic types all interconvert. */
6718 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == BOOLEAN_TYPE
6719 || codel == COMPLEX_TYPE)
6720 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == BOOLEAN_TYPE
6721 || coder == COMPLEX_TYPE))
6723 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6724 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6727 cp_warning ("`%T' used for argument %P of `%D'",
6728 rhstype, parmnum, fndecl);
6730 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6732 /* And we should warn if assigning a negative value to
6733 an unsigned variable. */
6734 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6736 if (TREE_CODE (rhs) == INTEGER_CST
6737 && TREE_NEGATED_INT (rhs))
6740 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6741 rhs, parmnum, fndecl);
6743 cp_warning ("%s of negative value `%E' to `%T'",
6744 errtype, rhs, type);
6746 overflow_warning (rhs);
6747 if (TREE_CONSTANT (rhs))
6751 return convert_and_check (type, rhs);
6753 /* Conversions involving enums. */
6754 else if ((codel == ENUMERAL_TYPE
6755 && (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE))
6756 || (coder == ENUMERAL_TYPE
6757 && (INTEGRAL_CODE_P (codel) || codel == REAL_TYPE)))
6759 return ocp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6761 /* Conversions among pointers */
6762 else if (codel == POINTER_TYPE
6763 && (coder == POINTER_TYPE
6764 || (coder == RECORD_TYPE
6765 && (IS_SIGNATURE_POINTER (rhstype)
6766 || IS_SIGNATURE_REFERENCE (rhstype)))))
6768 register tree ttl = TREE_TYPE (type);
6772 if (coder == RECORD_TYPE)
6774 rhs = build_optr_ref (rhs);
6775 rhstype = TREE_TYPE (rhs);
6777 ttr = TREE_TYPE (rhstype);
6779 /* If both pointers are of aggregate type, then we
6780 can give better error messages, and save some work
6782 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6786 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6787 || type == class_star_type_node
6788 || rhstype == class_star_type_node)
6789 binfo = TYPE_BINFO (ttl);
6791 binfo = get_binfo (ttl, ttr, 1);
6793 if (binfo == error_mark_node)
6794 return error_mark_node;
6796 return error_not_base_type (ttl, ttr);
6798 if (!at_least_as_qualified_p (ttl, ttr))
6801 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6802 rhstype, parmnum, fndecl);
6804 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6805 errtype, type, rhstype);
6809 /* Any non-function converts to a [const][volatile] void *
6810 and vice versa; otherwise, targets must be the same.
6811 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6812 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6813 || TYPE_MAIN_VARIANT (ttr) == void_type_node
6814 || (ctt = comp_target_types (type, rhstype, 1))
6815 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6816 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6818 /* ARM $4.8, commentary on p39. */
6819 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6820 && TREE_CODE (ttr) == OFFSET_TYPE)
6822 cp_error ("no standard conversion from `%T' to `void *'", ttr);
6823 return error_mark_node;
6826 if (ctt < 0 && TYPE_MAIN_VARIANT (ttl) != TYPE_MAIN_VARIANT (ttr))
6827 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6830 if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6831 && TYPE_MAIN_VARIANT (ttr) == void_type_node
6832 && ! null_ptr_cst_p (rhs))
6834 if (coder == RECORD_TYPE)
6835 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6838 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6841 /* Const and volatile mean something different for function types,
6842 so the usual warnings are not appropriate. */
6843 else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6844 || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6846 if (TREE_CODE (ttl) == OFFSET_TYPE
6847 && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6848 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6850 error ("%s between pointer to members converting across virtual baseclasses", errtype);
6851 return error_mark_node;
6853 else if (!at_least_as_qualified_p (ttl, ttr))
6855 if (string_conv_p (type, rhs, 1))
6856 /* converting from string constant to char *, OK. */;
6858 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6859 rhstype, parmnum, fndecl);
6861 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6862 errtype, type, rhstype);
6864 else if (TREE_CODE (ttl) == TREE_CODE (ttr)
6865 && ! comp_target_types (type, rhstype, 1))
6868 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6869 rhstype, parmnum, fndecl);
6871 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6872 errtype, type, rhstype);
6879 int drops_quals = 0;
6881 int unsigned_parity;
6884 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6885 for (; ; ttl = TREE_TYPE (ttl), ttr = TREE_TYPE (ttr))
6888 drops_quals |= !at_least_as_qualified_p (ttl, ttr);
6891 && !at_least_as_qualified_p (ttr, ttl))
6893 left_const &= TYPE_READONLY (ttl);
6895 if (TREE_CODE (ttl) != POINTER_TYPE
6896 || TREE_CODE (ttr) != POINTER_TYPE)
6899 unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6900 if (unsigned_parity)
6902 if (TREE_UNSIGNED (ttl))
6903 ttr = unsigned_type (ttr);
6905 ttl = unsigned_type (ttl);
6908 if (comp_target_types (ttl, ttr, nptrs) > 0)
6913 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6914 rhstype, parmnum, fndecl);
6916 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6917 errtype, type, rhstype);
6922 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6923 rhstype, parmnum, fndecl);
6925 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6926 errtype, type, rhstype);
6928 if (unsigned_parity > 0)
6931 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6932 rhstype, parmnum, fndecl);
6934 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6935 errtype, type, rhstype);
6937 else if (unsigned_parity < 0)
6940 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6941 rhstype, parmnum, fndecl);
6943 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6944 errtype, type, rhstype);
6947 /* C++ is not so friendly about converting function and
6948 member function pointers as C. Emit warnings here. */
6949 if (TREE_CODE (ttl) == FUNCTION_TYPE
6950 || TREE_CODE (ttl) == METHOD_TYPE)
6951 if (!same_or_base_type_p (ttl, ttr))
6953 warning ("conflicting function types in %s:", errtype);
6954 cp_warning ("\t`%T' != `%T'", type, rhstype);
6960 cp_error ("passing `%T' as argument %P of `%D'",
6961 rhstype, parmnum, fndecl);
6963 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6964 return error_mark_node;
6967 return cp_convert (type, rhs);
6969 else if (codel == POINTER_TYPE
6970 && (coder == INTEGER_TYPE
6971 || coder == BOOLEAN_TYPE))
6973 /* An explicit constant 0 can convert to a pointer,
6974 but not a 0 that results from casting or folding. */
6975 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
6978 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6979 rhstype, parmnum, fndecl);
6981 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6982 errtype, type, rhstype);
6984 return cp_convert (type, rhs);
6986 else if (codel == INTEGER_TYPE
6987 && (coder == POINTER_TYPE
6988 || (coder == RECORD_TYPE
6989 && (IS_SIGNATURE_POINTER (rhstype)
6990 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6991 || IS_SIGNATURE_REFERENCE (rhstype)))))
6994 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6995 rhstype, parmnum, fndecl);
6997 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6998 errtype, type, rhstype);
6999 return cp_convert (type, rhs);
7001 else if (codel == BOOLEAN_TYPE
7002 && (coder == POINTER_TYPE
7003 || (coder == RECORD_TYPE
7004 && (IS_SIGNATURE_POINTER (rhstype)
7005 || TYPE_PTRMEMFUNC_FLAG (rhstype)
7006 || IS_SIGNATURE_REFERENCE (rhstype)))))
7007 return cp_convert (type, rhs);
7010 else if (((coder == POINTER_TYPE
7011 && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
7012 || integer_zerop (rhs)
7013 || TYPE_PTRMEMFUNC_P (rhstype))
7014 && TYPE_PTRMEMFUNC_P (type))
7016 tree ttl = TYPE_PTRMEMFUNC_FN_TYPE (type);
7017 tree ttr = (TYPE_PTRMEMFUNC_P (rhstype)
7018 ? TYPE_PTRMEMFUNC_FN_TYPE (rhstype)
7020 int ctt = (TREE_CODE (rhstype) == INTEGER_TYPE ? 1
7021 : comp_target_types (ttl, ttr, 1));
7024 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
7027 cp_error ("%s to `%T' from `%T'", errtype, ttl, ttr);
7029 /* compatible pointer to member functions. */
7030 return build_ptrmemfunc (ttl, rhs, 0);
7032 else if (codel == ERROR_MARK || coder == ERROR_MARK)
7033 return error_mark_node;
7035 /* This should no longer happen. References are initialized via
7036 `convert_for_initialization'. They should otherwise be
7037 bashed before coming here. */
7038 else if (codel == REFERENCE_TYPE)
7039 my_friendly_abort (317);
7040 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
7042 tree nrhs = build1 (NOP_EXPR, type, rhs);
7043 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
7046 else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
7047 return cp_convert (type, rhs);
7048 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
7049 else if (TREE_CODE (type) == POINTER_TYPE
7050 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7051 || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
7053 && TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7054 return cp_convert (type, rhs);
7056 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
7057 return error_mark_node;
7060 /* Convert RHS to be of type TYPE.
7061 If EXP is non-zero, it is the target of the initialization.
7062 ERRTYPE is a string to use in error messages.
7064 Two major differences between the behavior of
7065 `convert_for_assignment' and `convert_for_initialization'
7066 are that references are bashed in the former, while
7067 copied in the latter, and aggregates are assigned in
7068 the former (operator=) while initialized in the
7071 If using constructor make sure no conversion operator exists, if one does
7072 exist, an ambiguity exists.
7074 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
7077 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
7078 tree exp, type, rhs;
7080 const char *errtype;
7084 register enum tree_code codel = TREE_CODE (type);
7085 register tree rhstype;
7086 register enum tree_code coder;
7088 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7089 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7090 if (TREE_CODE (rhs) == NOP_EXPR
7091 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7092 && codel != REFERENCE_TYPE)
7093 rhs = TREE_OPERAND (rhs, 0);
7095 if (rhs == error_mark_node
7096 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7097 return error_mark_node;
7099 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
7101 rhs = resolve_offset_ref (rhs);
7102 if (rhs == error_mark_node)
7103 return error_mark_node;
7106 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
7107 rhs = convert_from_reference (rhs);
7109 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7110 && TREE_CODE (type) != ARRAY_TYPE
7111 && (TREE_CODE (type) != REFERENCE_TYPE
7112 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7113 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7114 && (TREE_CODE (type) != REFERENCE_TYPE
7115 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7116 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7117 rhs = default_conversion (rhs);
7119 rhstype = TREE_TYPE (rhs);
7120 coder = TREE_CODE (rhstype);
7122 if (coder == ERROR_MARK)
7123 return error_mark_node;
7125 /* We accept references to incomplete types, so we can
7126 return here before checking if RHS is of complete type. */
7128 if (codel == REFERENCE_TYPE)
7130 /* This should eventually happen in convert_arguments. */
7131 extern int warningcount, errorcount;
7132 int savew = 0, savee = 0;
7135 savew = warningcount, savee = errorcount;
7136 rhs = convert_to_reference (type, rhs, CONV_IMPLICIT, flags,
7137 exp ? exp : error_mark_node);
7140 if (warningcount > savew)
7141 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
7142 else if (errorcount > savee)
7143 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
7149 exp = require_complete_type (exp);
7150 if (exp == error_mark_node)
7151 return error_mark_node;
7153 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
7154 rhstype = TREE_TYPE (rhstype);
7156 type = complete_type (type);
7158 if (TYPE_LANG_SPECIFIC (type)
7159 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
7160 return build_signature_pointer_constructor (type, rhs);
7162 if (IS_AGGR_TYPE (type))
7163 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
7165 if (type == TREE_TYPE (rhs))
7167 /* Issue warnings about peculiar, but legal, uses of NULL. We
7168 do this *before* the call to decl_constant_value so as to
7169 avoid duplicate warnings on code like `const int I = NULL;
7171 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
7172 cp_warning ("converting NULL to non-pointer type");
7174 if (TREE_READONLY_DECL_P (rhs))
7175 rhs = decl_constant_value (rhs);
7180 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
7183 /* Expand an ASM statement with operands, handling output operands
7184 that are not variables or INDIRECT_REFS by transforming such
7185 cases into cases that expand_asm_operands can handle.
7187 Arguments are same as for expand_asm_operands.
7189 We don't do default conversions on all inputs, because it can screw
7190 up operands that are expected to be in memory. */
7193 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
7194 tree string, outputs, inputs, clobbers;
7199 int noutputs = list_length (outputs);
7201 /* o[I] is the place that output number I should be written. */
7202 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
7205 /* Record the contents of OUTPUTS before it is modified. */
7206 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7207 o[i] = TREE_VALUE (tail);
7209 /* Generate the ASM_OPERANDS insn;
7210 store into the TREE_VALUEs of OUTPUTS some trees for
7211 where the values were actually stored. */
7212 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
7214 /* Copy all the intermediate outputs into the specified outputs. */
7215 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7217 if (o[i] != TREE_VALUE (tail))
7219 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
7220 const0_rtx, VOIDmode, EXPAND_NORMAL);
7223 /* Detect modification of read-only values.
7224 (Otherwise done by build_modify_expr.) */
7227 tree type = TREE_TYPE (o[i]);
7228 if (CP_TYPE_CONST_P (type)
7229 || ((TREE_CODE (type) == RECORD_TYPE
7230 || TREE_CODE (type) == UNION_TYPE)
7231 && C_TYPE_FIELDS_READONLY (type)))
7232 readonly_error (o[i], "modification by `asm'", 1);
7236 /* Those MODIFY_EXPRs could do autoincrements. */
7240 /* Expand a C `return' statement.
7241 RETVAL is the expression for what to return,
7242 or a null pointer for `return;' with no value.
7244 C++: upon seeing a `return', we must call destructors on all
7245 variables in scope which had constructors called on them.
7246 This means that if in a destructor, the base class destructors
7247 must be called before returning.
7249 The RETURN statement in C++ has initialization semantics. */
7252 c_expand_return (retval)
7255 extern struct nesting *cond_stack, *loop_stack, *case_stack;
7256 extern tree dtor_label, ctor_label;
7257 tree result = DECL_RESULT (current_function_decl);
7258 tree valtype = TREE_TYPE (result);
7260 if (TREE_THIS_VOLATILE (current_function_decl))
7261 warning ("function declared `noreturn' has a `return' statement");
7263 if (retval == error_mark_node)
7265 current_function_returns_null = 1;
7269 if (processing_template_decl)
7271 add_tree (build_min_nt (RETURN_STMT, retval));
7278 error ("returning a value from a destructor");
7280 /* Can't just return from a destructor. */
7281 expand_goto (dtor_label);
7285 if (retval == NULL_TREE)
7287 /* A non-named return value does not count. */
7289 if (DECL_CONSTRUCTOR_P (current_function_decl))
7290 retval = current_class_ptr;
7291 else if (DECL_NAME (result) != NULL_TREE
7292 && TREE_CODE (valtype) != VOID_TYPE)
7296 current_function_returns_null = 1;
7298 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
7300 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
7302 pedwarn ("`return' with no value, in function returning non-void");
7303 /* Clear this, so finish_function won't say that we
7304 reach the end of a non-void function (which we don't,
7305 we gave a return!). */
7306 current_function_returns_null = 0;
7310 expand_null_return ();
7314 else if (DECL_CONSTRUCTOR_P (current_function_decl))
7316 if (flag_this_is_variable)
7317 error ("return from a constructor: use `this = ...' instead");
7319 error ("returning a value from a constructor");
7320 retval = current_class_ptr;
7323 /* Effective C++ rule 15. See also start_function. */
7325 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
7326 && retval != current_class_ref)
7327 cp_warning ("`operator=' should return a reference to `*this'");
7329 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
7331 current_function_returns_null = 1;
7332 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7333 pedwarn ("`return' with a value, in function returning void");
7334 expand_return (retval);
7338 /* Now deal with possible C++ hair:
7339 (1) Compute the return value.
7340 (2) If there are aggregate values with destructors which
7341 must be cleaned up, clean them (taking care
7342 not to clobber the return value).
7343 (3) If an X(X&) constructor is defined, the return
7344 value must be returned via that. */
7346 if (retval == result
7347 || DECL_CONSTRUCTOR_P (current_function_decl))
7348 /* It's already done for us. */;
7349 else if (TREE_CODE (TREE_TYPE (retval)) == VOID_TYPE)
7351 pedwarn ("return of void value in function returning non-void");
7352 expand_expr_stmt (retval);
7357 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
7359 /* First convert the value to the function's return type, then
7360 to the type of return value's location to handle the
7361 case that functype is thiner than the valtype. */
7363 retval = convert_for_initialization
7364 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
7365 "return", NULL_TREE, 0);
7367 retval = convert (valtype, retval);
7369 if (retval == error_mark_node)
7371 /* Avoid warning about control reaching end of function. */
7372 expand_null_return ();
7376 /* We can't initialize a register from a AGGR_INIT_EXPR. */
7377 else if (! current_function_returns_struct
7378 && TREE_CODE (retval) == TARGET_EXPR
7379 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
7380 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
7381 TREE_OPERAND (retval, 0));
7383 /* Add some useful error checking for C++. */
7384 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
7386 tree whats_returned;
7388 /* Sort through common things to see what it is
7389 we are returning. */
7390 whats_returned = retval;
7391 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7393 whats_returned = TREE_OPERAND (whats_returned, 1);
7394 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7395 whats_returned = TREE_OPERAND (whats_returned, 0);
7397 while (TREE_CODE (whats_returned) == CONVERT_EXPR
7398 || TREE_CODE (whats_returned) == NOP_EXPR)
7399 whats_returned = TREE_OPERAND (whats_returned, 0);
7400 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7402 whats_returned = TREE_OPERAND (whats_returned, 0);
7403 while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7404 || TREE_CODE (whats_returned) == TARGET_EXPR)
7406 /* Get the target. */
7407 whats_returned = TREE_OPERAND (whats_returned, 0);
7408 warning ("returning reference to temporary");
7412 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
7414 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
7415 warning ("reference to non-lvalue returned");
7416 else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
7417 && DECL_FUNCTION_SCOPE_P (whats_returned)
7418 && !(TREE_STATIC (whats_returned)
7419 || TREE_PUBLIC (whats_returned)))
7420 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
7423 else if (TREE_CODE (retval) == ADDR_EXPR)
7425 tree whats_returned = TREE_OPERAND (retval, 0);
7427 if (TREE_CODE (whats_returned) == VAR_DECL
7428 && DECL_NAME (whats_returned)
7429 && DECL_FUNCTION_SCOPE_P (whats_returned)
7430 && !(TREE_STATIC (whats_returned)
7431 || TREE_PUBLIC (whats_returned)))
7432 cp_warning_at ("address of local variable `%D' returned", whats_returned);
7436 if (retval != NULL_TREE
7437 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
7438 && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
7439 current_function_return_value = retval;
7441 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7443 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
7444 expand_goto (ctor_label);
7447 if (retval && retval != result)
7449 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7450 TREE_SIDE_EFFECTS (result) = 1;
7453 expand_start_target_temps ();
7455 expand_return (result);
7457 expand_end_target_temps ();
7459 current_function_returns_value = 1;
7462 /* Start a C switch statement, testing expression EXP.
7463 Return EXP if it is valid, an error node otherwise. */
7466 c_expand_start_case (exp)
7471 exp = build_expr_type_conversion (WANT_INT | WANT_ENUM, exp, 1);
7472 if (exp == NULL_TREE)
7474 error ("switch quantity not an integer");
7475 exp = error_mark_node;
7477 if (exp == error_mark_node)
7478 return error_mark_node;
7480 exp = default_conversion (exp);
7481 type = TREE_TYPE (exp);
7482 idx = get_unwidened (exp, 0);
7483 /* We can't strip a conversion from a signed type to an unsigned,
7484 because if we did, int_fits_type_p would do the wrong thing
7485 when checking case values for being in range,
7486 and it's too hard to do the right thing. */
7487 if (TREE_UNSIGNED (TREE_TYPE (exp)) == TREE_UNSIGNED (TREE_TYPE (idx)))
7491 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7492 type, "switch statement");
7497 /* Returns non-zero if the pointer-type FROM can be converted to the
7498 pointer-type TO via a qualification conversion. If CONSTP is -1,
7499 then we return non-zero if the pointers are similar, and the
7500 cv-qualification signature of FROM is a proper subset of that of TO.
7502 If CONSTP is positive, then all outer pointers have been
7506 comp_ptr_ttypes_real (to, from, constp)
7510 int to_more_cv_qualified = 0;
7512 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7514 if (TREE_CODE (to) != TREE_CODE (from))
7517 if (TREE_CODE (from) == OFFSET_TYPE
7518 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7519 TYPE_OFFSET_BASETYPE (to)))
7522 /* Const and volatile mean something different for function types,
7523 so the usual checks are not appropriate. */
7524 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7526 if (!at_least_as_qualified_p (to, from))
7529 if (!at_least_as_qualified_p (from, to))
7534 ++to_more_cv_qualified;
7538 constp &= TYPE_READONLY (to);
7541 if (TREE_CODE (to) != POINTER_TYPE)
7543 same_type_p (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from))
7544 && (constp >= 0 || to_more_cv_qualified);
7548 /* When comparing, say, char ** to char const **, this function takes the
7549 'char *' and 'char const *'. Do not pass non-pointer types to this
7553 comp_ptr_ttypes (to, from)
7556 return comp_ptr_ttypes_real (to, from, 1);
7559 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7560 type or inheritance-related types, regardless of cv-quals. */
7563 ptr_reasonably_similar (to, from)
7566 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7568 if (TREE_CODE (to) != TREE_CODE (from))
7571 if (TREE_CODE (from) == OFFSET_TYPE
7572 && comptypes (TYPE_OFFSET_BASETYPE (to),
7573 TYPE_OFFSET_BASETYPE (from),
7574 COMPARE_BASE | COMPARE_RELAXED))
7577 if (TREE_CODE (to) != POINTER_TYPE)
7579 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
7580 COMPARE_BASE | COMPARE_RELAXED);
7584 /* Like comp_ptr_ttypes, for const_cast. */
7587 comp_ptr_ttypes_const (to, from)
7590 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7592 if (TREE_CODE (to) != TREE_CODE (from))
7595 if (TREE_CODE (from) == OFFSET_TYPE
7596 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7597 TYPE_OFFSET_BASETYPE (to)))
7600 if (TREE_CODE (to) != POINTER_TYPE)
7601 return same_type_p (TYPE_MAIN_VARIANT (to),
7602 TYPE_MAIN_VARIANT (from));
7606 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7609 comp_ptr_ttypes_reinterpret (to, from)
7614 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7616 if (TREE_CODE (from) == OFFSET_TYPE)
7617 from = TREE_TYPE (from);
7618 if (TREE_CODE (to) == OFFSET_TYPE)
7619 to = TREE_TYPE (to);
7621 /* Const and volatile mean something different for function types,
7622 so the usual checks are not appropriate. */
7623 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7624 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7626 if (!at_least_as_qualified_p (to, from))
7630 && !at_least_as_qualified_p (from, to))
7632 constp &= TYPE_READONLY (to);
7635 if (TREE_CODE (from) != POINTER_TYPE
7636 || TREE_CODE (to) != POINTER_TYPE)
7641 /* Returns the type-qualifier set corresponding to TYPE. */
7644 cp_type_quals (type)
7647 while (TREE_CODE (type) == ARRAY_TYPE)
7648 type = TREE_TYPE (type);
7650 return TYPE_QUALS (type);
7653 /* Returns non-zero if the TYPE contains a mutable member */
7656 cp_has_mutable_p (type)
7659 while (TREE_CODE (type) == ARRAY_TYPE)
7660 type = TREE_TYPE (type);
7662 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);