1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
43 static tree convert_for_assignment PROTO((tree, tree, const char *, tree,
45 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
46 static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
47 static int comp_target_parms PROTO((tree, tree, int));
48 static int comp_ptr_ttypes_real PROTO((tree, tree, int));
49 static int comp_ptr_ttypes_const PROTO((tree, tree));
50 static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
51 static int comp_except_types PROTO((tree, tree, int));
52 static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
54 static tree common_base_type PROTO((tree, tree));
56 static tree convert_sequence PROTO((tree, tree));
58 static tree lookup_anon_field PROTO((tree, tree));
59 static tree pointer_diff PROTO((tree, tree, tree));
60 static tree build_component_addr PROTO((tree, tree));
61 static tree qualify_type PROTO((tree, tree));
62 static tree get_delta_difference PROTO((tree, tree, int));
63 static int comp_cv_target_types PROTO((tree, tree, int));
64 static void casts_away_constness_r PROTO((tree *, tree *));
65 static int casts_away_constness PROTO ((tree, tree));
67 /* Return the target type of TYPE, which meas return T for:
68 T*, T&, T[], T (...), and otherwise, just T. */
74 if (TREE_CODE (type) == REFERENCE_TYPE)
75 type = TREE_TYPE (type);
76 while (TREE_CODE (type) == POINTER_TYPE
77 || TREE_CODE (type) == ARRAY_TYPE
78 || TREE_CODE (type) == FUNCTION_TYPE
79 || TREE_CODE (type) == METHOD_TYPE
80 || TREE_CODE (type) == OFFSET_TYPE)
81 type = TREE_TYPE (type);
85 /* Do `exp = require_complete_type (exp);' to make sure exp
86 does not have an incomplete type. (That includes void types.)
87 Returns the error_mark_node if the VALUE does not have
88 complete type when this function returns. */
91 require_complete_type (value)
96 if (processing_template_decl || value == error_mark_node)
99 if (TREE_CODE (value) == OVERLOAD)
100 type = unknown_type_node;
102 type = TREE_TYPE (value);
104 /* First, detect a valid value with a complete type. */
105 if (TYPE_SIZE (type) != 0
106 && TYPE_SIZE (type) != size_zero_node
107 && ! (TYPE_LANG_SPECIFIC (type)
108 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
109 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
112 /* If we see X::Y, we build an OFFSET_TYPE which has
113 not been laid out. Try to avoid an error by interpreting
114 it as this->X::Y, if reasonable. */
115 if (TREE_CODE (value) == OFFSET_REF
116 && current_class_ref != 0
117 && TREE_OPERAND (value, 0) == current_class_ref)
119 tree base, member = TREE_OPERAND (value, 1);
120 tree basetype = TYPE_OFFSET_BASETYPE (type);
121 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
122 base = convert_pointer_to (basetype, current_class_ptr);
123 value = build (COMPONENT_REF, TREE_TYPE (member),
124 build_indirect_ref (base, NULL_PTR), member);
125 return require_complete_type (value);
128 if (complete_type_or_else (type, value))
131 return error_mark_node;
134 /* Makes sure EXPR is a complete type when used in a void context, like a
135 whole expression, or lhs of a comma operator. Issue a diagnostic and
136 return error_mark_node on failure. This is a little tricky, because some
137 valid void types look stunningly similar to invalid void types. We err on
138 the side of caution */
141 require_complete_type_in_void (expr)
144 switch (TREE_CODE (expr))
150 op = TREE_OPERAND (expr,2);
151 op = require_complete_type_in_void (op);
152 TREE_OPERAND (expr,2) = op;
153 if (op == error_mark_node)
166 op = TREE_OPERAND (expr,1);
167 op = require_complete_type_in_void (op);
168 TREE_OPERAND (expr,1) = op;
169 if (op == error_mark_node)
178 case NON_LVALUE_EXPR:
183 op = TREE_OPERAND (expr,0);
184 op = require_complete_type_in_void (op);
185 TREE_OPERAND (expr,0) = op;
186 if (op == error_mark_node)
194 case CALL_EXPR: /* function call return can be ignored */
195 case RTL_EXPR: /* RTL nodes have no value */
196 case DELETE_EXPR: /* delete expressions have no type */
197 case VEC_DELETE_EXPR:
198 case INTEGER_CST: /* used for null pointer */
199 case EXIT_EXPR: /* have no return */
200 case LOOP_EXPR: /* have no return */
201 case BIND_EXPR: /* have no return */
202 case THROW_EXPR: /* have no return */
203 case MODIFY_EXPR: /* sometimes this has a void type, but that's ok */
204 case CONVERT_EXPR: /* sometimes has a void type */
209 tree op = TREE_OPERAND (expr,0);
211 /* Calling a function returning a reference has an implicit
212 dereference applied. We don't want to make that an error. */
213 if (TREE_CODE (op) == CALL_EXPR
214 && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
216 /* else fallthrough */
220 expr = require_complete_type (expr);
227 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
228 a template instantiation, do the instantiation. Returns TYPE,
229 whether or not it could be completed, unless something goes
230 horribly wrong, in which case the error_mark_node is returned. */
236 if (type == NULL_TREE)
237 /* Rather than crash, we return something sure to cause an error
239 return error_mark_node;
241 if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
243 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
245 tree t = complete_type (TREE_TYPE (type));
246 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
248 TYPE_NEEDS_CONSTRUCTING (type)
249 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
250 TYPE_NEEDS_DESTRUCTOR (type)
251 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
253 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
254 instantiate_class_template (TYPE_MAIN_VARIANT (type));
259 /* Like complete_type, but issue an error if the TYPE cannot be
260 completed. VALUE is used for informative diagnostics.
261 Returns NULL_TREE if the type cannot be made complete. */
264 complete_type_or_else (type, value)
268 type = complete_type (type);
269 if (type == error_mark_node)
270 /* We already issued an error. */
272 else if (!TYPE_SIZE (type) || TYPE_SIZE (type) == size_zero_node)
274 incomplete_type_error (value, type);
281 /* Return truthvalue of whether type of EXP is instantiated. */
287 return (TREE_CODE (exp) == OVERLOAD
288 || TREE_CODE (exp) == TREE_LIST
289 || TREE_TYPE (exp) == unknown_type_node
290 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
291 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
294 /* Return truthvalue of whether T is function (or pfn) type. */
300 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
301 || (TREE_CODE (t) == POINTER_TYPE
302 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
303 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
306 /* Return a variant of TYPE which has all the type qualifiers of LIKE
307 as well as those of TYPE. */
310 qualify_type (type, like)
313 /* @@ Must do member pointers here. */
314 return cp_build_qualified_type (type, (CP_TYPE_QUALS (type)
315 | CP_TYPE_QUALS (like)));
318 /* Return the common type of two parameter lists.
319 We assume that comptypes has already been done and returned 1;
320 if that isn't so, this may crash.
322 As an optimization, free the space we allocate if the parameter
323 lists are already common. */
329 tree oldargs = p1, newargs, n;
332 char *first_obj = (char *) oballoc (0);
334 len = list_length (p1);
335 newargs = tree_last (p1);
337 if (newargs == void_list_node)
346 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
351 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
353 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
355 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
358 else if (! TREE_PURPOSE (p1))
360 if (TREE_PURPOSE (p2))
362 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
368 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
370 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
372 if (TREE_VALUE (p1) != TREE_VALUE (p2))
375 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
378 TREE_VALUE (n) = TREE_VALUE (p1);
389 /* Given a type, perhaps copied for a typedef,
390 find the "original" version of it. */
395 while (TYPE_NAME (t) != NULL_TREE)
397 tree x = TYPE_NAME (t);
398 if (TREE_CODE (x) != TYPE_DECL)
400 x = DECL_ORIGINAL_TYPE (x);
408 /* T1 and T2 are arithmetic or enumeration types. Return the type
409 that will result from the "usual arithmetic converions" on T1 and
410 T2 as described in [expr]. */
413 type_after_usual_arithmetic_conversions (t1, t2)
417 enum tree_code code1 = TREE_CODE (t1);
418 enum tree_code code2 = TREE_CODE (t2);
421 /* FIXME: Attributes. */
422 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
423 || TREE_CODE (t1) == ENUMERAL_TYPE,
425 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
426 || TREE_CODE (t2) == ENUMERAL_TYPE,
429 /* In what follows, we slightly generalize the rules given in [expr]
430 so as to deal with `long long'. First, merge the attributes. */
431 attributes = merge_machine_type_attributes (t1, t2);
433 /* If only one is real, use it as the result. */
434 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
435 return build_type_attribute_variant (t1, attributes);
436 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
437 return build_type_attribute_variant (t2, attributes);
439 /* Perform the integral promotions. */
440 if (code1 != REAL_TYPE)
442 t1 = type_promotes_to (t1);
443 t2 = type_promotes_to (t2);
446 /* Both real or both integers; use the one with greater precision. */
447 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
448 return build_type_attribute_variant (t1, attributes);
449 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
450 return build_type_attribute_variant (t2, attributes);
452 if (code1 != REAL_TYPE)
454 /* If one is unsigned long long, then convert the other to unsigned
456 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
457 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
458 return build_type_attribute_variant (long_long_unsigned_type_node,
460 /* If one is a long long, and the other is an unsigned long, and
461 long long can represent all the values of an unsigned long, then
462 convert to a long long. Otherwise, convert to an unsigned long
463 long. Otherwise, if either operand is long long, convert the
466 Since we're here, we know the TYPE_PRECISION is the same;
467 therefore converting to long long cannot represent all the values
468 of an unsigned long, so we choose unsigned long long in that
470 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
471 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
473 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
474 ? long_long_unsigned_type_node
475 : long_long_integer_type_node);
476 return build_type_attribute_variant (t, attributes);
479 /* Go through the same procedure, but for longs. */
480 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
481 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
482 return build_type_attribute_variant (long_unsigned_type_node,
484 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
485 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
487 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
488 ? long_unsigned_type_node : long_integer_type_node);
489 return build_type_attribute_variant (t, attributes);
491 /* Otherwise prefer the unsigned one. */
492 if (TREE_UNSIGNED (t1))
493 return build_type_attribute_variant (t1, attributes);
495 return build_type_attribute_variant (t2, attributes);
499 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
500 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
501 return build_type_attribute_variant (long_double_type_node,
503 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
504 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
505 return build_type_attribute_variant (double_type_node,
508 return build_type_attribute_variant (float_type_node,
513 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
514 ARG1 and ARG2 are the values with those types. The LOCATION is a
515 string describing the current location, in case an error occurs. */
518 composite_pointer_type (t1, t2, arg1, arg2, location)
529 If one operand is a null pointer constant, the composite pointer
530 type is the type of the other operand. */
531 if (null_ptr_cst_p (arg1))
533 if (null_ptr_cst_p (arg2))
536 /* Deal with pointer-to-member functions in the same way as we deal
537 with pointers to functions. */
538 if (TYPE_PTRMEMFUNC_P (t1))
539 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
540 if (TYPE_PTRMEMFUNC_P (t2))
541 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
543 if (comp_target_types (t1, t2, 1))
544 result_type = common_type (t1, t2);
545 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t1)) == void_type_node)
547 if (pedantic && TREE_CODE (t2) == FUNCTION_TYPE)
548 pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
550 result_type = qualify_type (t1, t2);
552 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t2)) == void_type_node)
554 if (pedantic && TREE_CODE (t1) == FUNCTION_TYPE)
555 pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
557 result_type = qualify_type (t2, t1);
560 else if (same_or_base_type_p (t2, t1))
562 else if (IS_AGGR_TYPE (TREE_TYPE (t1))
563 && IS_AGGR_TYPE (TREE_TYPE (t2))
564 && (result_type = common_base_type (TREE_TYPE (t1),
567 if (result_type == error_mark_node)
569 cp_error ("common base type of types `%T' and `%T' is ambiguous",
570 TREE_TYPE (t1), TREE_TYPE (t2));
571 result_type = ptr_type_node;
576 && result_type != TREE_TYPE (t1)
577 && result_type != TREE_TYPE (t2))
578 cp_pedwarn ("`%T' and `%T' converted to `%T *' in %s",
579 t1, t2, result_type, location);
581 result_type = build_pointer_type (result_type);
586 cp_pedwarn ("pointer type mismatch in %s", location);
587 result_type = ptr_type_node;
593 /* Return the common type of two types.
594 We assume that comptypes has already been done and returned 1;
595 if that isn't so, this may crash.
597 This is the type for the result of most arithmetic operations
598 if the operands have the given two types.
600 We do not deal with enumeral types here because they have already been
601 converted to integer types. */
607 register enum tree_code code1;
608 register enum tree_code code2;
611 /* Save time if the two types are the same. */
614 t1 = original_type (t1);
615 t2 = original_type (t2);
619 /* If one type is nonsense, use the other. */
620 if (t1 == error_mark_node)
622 if (t2 == error_mark_node)
625 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
626 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
627 return type_after_usual_arithmetic_conversions (t1, t2);
629 /* Merge the attributes. */
630 attributes = merge_machine_type_attributes (t1, t2);
632 /* Treat an enum type as the unsigned integer type of the same width. */
634 if (TREE_CODE (t1) == ENUMERAL_TYPE)
635 t1 = type_for_size (TYPE_PRECISION (t1), 1);
636 if (TREE_CODE (t2) == ENUMERAL_TYPE)
637 t2 = type_for_size (TYPE_PRECISION (t2), 1);
639 if (TYPE_PTRMEMFUNC_P (t1))
640 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
641 if (TYPE_PTRMEMFUNC_P (t2))
642 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
644 code1 = TREE_CODE (t1);
645 code2 = TREE_CODE (t2);
647 /* If one type is complex, form the common type of the non-complex
648 components, then make that complex. Use T1 or T2 if it is the
650 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
652 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
653 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
654 tree subtype = common_type (subtype1, subtype2);
656 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
657 return build_type_attribute_variant (t1, attributes);
658 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
659 return build_type_attribute_variant (t2, attributes);
661 return build_type_attribute_variant (build_complex_type (subtype),
669 /* We should have called type_after_usual_arithmetic_conversions
671 my_friendly_abort (19990725);
676 /* For two pointers, do this recursively on the target type,
677 and combine the qualifiers of the two types' targets. */
678 /* This code was turned off; I don't know why.
679 But ANSI C++ specifies doing this with the qualifiers.
680 So I turned it on again. */
682 tree tt1 = TREE_TYPE (t1);
683 tree tt2 = TREE_TYPE (t2);
688 if (TREE_CODE (tt1) == OFFSET_TYPE)
690 b1 = TYPE_OFFSET_BASETYPE (tt1);
691 b2 = TYPE_OFFSET_BASETYPE (tt2);
692 tt1 = TREE_TYPE (tt1);
693 tt2 = TREE_TYPE (tt2);
698 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
699 tt1 = TYPE_MAIN_VARIANT (tt1);
700 tt2 = TYPE_MAIN_VARIANT (tt2);
706 compiler_error ("common_type called with uncommon member types");
709 else if (tt1 == void_type_node || tt2 == void_type_node)
710 target = void_type_node;
711 else if (tt1 == unknown_type_node)
713 else if (tt2 == unknown_type_node)
716 target = common_type (tt1, tt2);
718 target = cp_build_qualified_type (target, type_quals);
722 if (same_type_p (b1, b2)
723 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
724 target = build_offset_type (b2, target);
725 else if (binfo_or_else (b2, b1))
726 target = build_offset_type (b1, target);
729 if (code1 == POINTER_TYPE)
730 t1 = build_pointer_type (target);
732 t1 = build_reference_type (target);
733 t1 = build_type_attribute_variant (t1, attributes);
735 if (TREE_CODE (target) == METHOD_TYPE)
736 t1 = build_ptrmemfunc_type (t1);
743 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
744 /* Save space: see if the result is identical to one of the args. */
745 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
746 return build_type_attribute_variant (t1, attributes);
747 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
748 return build_type_attribute_variant (t2, attributes);
749 /* Merge the element types, and have a size if either arg has one. */
750 t1 = build_cplus_array_type
751 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
752 return build_type_attribute_variant (t1, attributes);
756 /* Function types: prefer the one that specified arg types.
757 If both do, merge the arg types. Also merge the return types. */
759 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
760 tree p1 = TYPE_ARG_TYPES (t1);
761 tree p2 = TYPE_ARG_TYPES (t2);
764 /* Save space: see if the result is identical to one of the args. */
765 if (valtype == TREE_TYPE (t1) && ! p2)
766 return build_type_attribute_variant (t1, attributes);
767 if (valtype == TREE_TYPE (t2) && ! p1)
768 return build_type_attribute_variant (t2, attributes);
770 /* Simple way if one arg fails to specify argument types. */
771 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
773 rval = build_function_type (valtype, p2);
774 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
775 rval = build_exception_variant (rval, raises);
776 return build_type_attribute_variant (rval, attributes);
778 raises = TYPE_RAISES_EXCEPTIONS (t1);
779 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
781 rval = build_function_type (valtype, p1);
783 rval = build_exception_variant (rval, raises);
784 return build_type_attribute_variant (rval, attributes);
787 rval = build_function_type (valtype, commonparms (p1, p2));
788 rval = build_exception_variant (rval, raises);
789 return build_type_attribute_variant (rval, attributes);
794 t1 = TYPE_MAIN_VARIANT (t1);
795 t2 = TYPE_MAIN_VARIANT (t2);
797 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
798 return build_type_attribute_variant (t1, attributes);
799 else if (binfo_or_else (t2, t1))
800 return build_type_attribute_variant (t2, attributes);
803 compiler_error ("common_type called with uncommon aggregate types");
804 return error_mark_node;
808 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
810 /* Get this value the long way, since TYPE_METHOD_BASETYPE
811 is just the main variant of this. */
815 tree b1 = TYPE_OFFSET_BASETYPE (t1);
816 tree b2 = TYPE_OFFSET_BASETYPE (t2);
818 if (same_type_p (b1, b2)
819 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
820 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
823 if (binfo_or_else (b2, b1) == NULL_TREE)
824 compiler_error ("common_type called with uncommon method types");
825 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
828 raises = TYPE_RAISES_EXCEPTIONS (t1);
830 /* If this was a member function type, get back to the
831 original type of type member function (i.e., without
832 the class instance variable up front. */
833 t1 = build_function_type (TREE_TYPE (t1),
834 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
835 t2 = build_function_type (TREE_TYPE (t2),
836 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
837 t3 = common_type (t1, t2);
838 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
839 TYPE_ARG_TYPES (t3));
840 t1 = build_exception_variant (t3, raises);
843 compiler_error ("common_type called with uncommon method types");
845 return build_type_attribute_variant (t1, attributes);
848 /* Pointers to members should now be handled by the POINTER_TYPE
850 my_friendly_abort (990325);
853 return build_type_attribute_variant (t1, attributes);
857 /* Compare two exception specifier types for exactness or subsetness, if
858 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
860 [except.spec] "If a class X ... objects of class X or any class publicly
861 and unambigously derrived from X. Similarly, if a pointer type Y * ...
862 exceptions of type Y * or that are pointers to any type publicly and
863 unambigously derrived from Y. Otherwise a function only allows exceptions
864 that have the same type ..."
865 This does not mention cv qualifiers and is different to what throw
866 [except.throw] and catch [except.catch] will do. They will ignore the
867 top level cv qualifiers, and allow qualifiers in the pointer to class
870 We implement the letter of the standard. */
873 comp_except_types (a, b, exact)
877 if (same_type_p (a, b))
881 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
884 if (TREE_CODE (a) == POINTER_TYPE
885 && TREE_CODE (b) == POINTER_TYPE)
889 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
893 if (TREE_CODE (a) != RECORD_TYPE
894 || TREE_CODE (b) != RECORD_TYPE)
897 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
903 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
904 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
905 otherwise it must be exact. Exception lists are unordered, but
906 we've already filtered out duplicates. Most lists will be in order,
907 we should try to make use of that. */
910 comp_except_specs (t1, t2, exact)
921 if (t1 == NULL_TREE) /* T1 is ... */
922 return t2 == NULL_TREE || !exact;
923 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
924 return t2 != NULL_TREE && !TREE_VALUE (t2);
925 if (t2 == NULL_TREE) /* T2 is ... */
927 if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
930 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
931 Count how many we find, to determine exactness. For exact matching and
932 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
934 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
936 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
938 tree a = TREE_VALUE (probe);
939 tree b = TREE_VALUE (t2);
941 if (comp_except_types (a, b, exact))
943 if (probe == base && exact)
944 base = TREE_CHAIN (probe);
949 if (probe == NULL_TREE)
952 return !exact || base == NULL_TREE || length == list_length (t1);
955 /* Compare the array types T1 and T2, using CMP as the type comparison
956 function for the element types. STRICT is as for comptypes. */
959 comp_array_types (cmp, t1, t2, strict)
960 register int (*cmp) PROTO((tree, tree, int));
970 /* The type of the array elements must be the same. */
971 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
972 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
973 strict & ~COMPARE_REDECLARATION)))
976 d1 = TYPE_DOMAIN (t1);
977 d2 = TYPE_DOMAIN (t2);
982 /* If one of the arrays is dimensionless, and the other has a
983 dimension, they are of different types. However, it is legal to
991 declarations for an array object can specify
992 array types that differ by the presence or absence of a major
993 array bound (_dcl.array_). */
995 return strict & COMPARE_REDECLARATION;
997 /* Check that the dimensions are the same. */
998 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
1000 && cp_tree_equal (TYPE_MAX_VALUE (d1),
1001 TYPE_MAX_VALUE (d2)));
1004 /* Return 1 if T1 and T2 are compatible types for assignment or
1005 various other operations. STRICT is a bitwise-or of the COMPARE_*
1009 comptypes (t1, t2, strict)
1015 int orig_strict = strict;
1017 /* The special exemption for redeclaring array types without an
1018 array bound only applies at the top level:
1023 is not legal, for example. */
1024 strict &= ~COMPARE_REDECLARATION;
1026 /* Suppress errors caused by previously reported errors */
1030 /* This should never happen. */
1031 my_friendly_assert (t1 != error_mark_node, 307);
1033 if (t2 == error_mark_node)
1036 if (strict & COMPARE_RELAXED)
1038 /* Treat an enum type as the unsigned integer type of the same width. */
1040 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1041 t1 = type_for_size (TYPE_PRECISION (t1), 1);
1042 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1043 t2 = type_for_size (TYPE_PRECISION (t2), 1);
1049 if (TYPE_PTRMEMFUNC_P (t1))
1050 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1051 if (TYPE_PTRMEMFUNC_P (t2))
1052 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1054 /* Different classes of types can't be compatible. */
1055 if (TREE_CODE (t1) != TREE_CODE (t2))
1058 /* Qualifiers must match. */
1059 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
1061 if (strict == COMPARE_STRICT
1062 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1065 /* Allow for two different type nodes which have essentially the same
1066 definition. Note that we already checked for equality of the type
1067 qualifiers (just above). */
1069 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1072 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
1073 attribute is its own main variant (`val' will remain 0). */
1074 #ifndef COMP_TYPE_ATTRIBUTES
1075 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
1078 if (strict & COMPARE_NO_ATTRIBUTES)
1080 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1081 else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
1084 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1087 switch (TREE_CODE (t1))
1089 case TEMPLATE_TEMPLATE_PARM:
1090 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1091 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1093 if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1)),
1094 DECL_TEMPLATE_PARMS (TYPE_NAME (t2))))
1096 if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1)
1097 && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2))
1099 /* Don't check inheritance. */
1100 strict = COMPARE_STRICT;
1105 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1106 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1107 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
1108 val = comp_template_args (TYPE_TI_ARGS (t1),
1111 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1113 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1118 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1119 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1120 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1124 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1125 TYPE_RAISES_EXCEPTIONS (t2), 1))
1128 /* This case is anti-symmetrical!
1129 One can pass a base member (or member function)
1130 to something expecting a derived member (or member function),
1131 but not vice-versa! */
1133 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1134 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1138 case REFERENCE_TYPE:
1139 t1 = TREE_TYPE (t1);
1140 t2 = TREE_TYPE (t2);
1141 /* first, check whether the referred types match with the
1142 required level of strictness */
1143 val = comptypes (t1, t2, strict);
1146 if (TREE_CODE (t1) == RECORD_TYPE
1147 && TREE_CODE (t2) == RECORD_TYPE)
1152 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1153 TYPE_RAISES_EXCEPTIONS (t2), 1))
1156 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1157 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1158 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1162 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1163 here since this is the one place where
1164 COMPARE_REDECLARATION should be used. */
1165 val = comp_array_types (comptypes, t1, t2, orig_strict);
1168 case TEMPLATE_TYPE_PARM:
1169 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1170 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1173 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
1175 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1178 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1183 return attrval == 2 && val == 1 ? 2 : val;
1186 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1187 only in the same direction as the target type. */
1190 comp_cv_target_types (ttl, ttr, nptrs)
1196 if (!at_least_as_qualified_p (ttl, ttr)
1197 && !at_least_as_qualified_p (ttr, ttl))
1198 /* The qualifications are incomparable. */
1201 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1202 return more_qualified_p (ttr, ttl) ? -1 : 1;
1204 t = comp_target_types (ttl, ttr, nptrs);
1205 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1206 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1212 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1213 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1214 converted to TTL. Return -1 means that TTL can be converted to TTR but
1217 NPTRS is the number of pointers we can strip off and keep cool.
1218 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1219 but to not permit B** to convert to A**.
1221 This should go away. Callers should use can_convert or something
1222 similar instead. (jason 17 Apr 1997) */
1225 comp_target_types (ttl, ttr, nptrs)
1229 ttl = TYPE_MAIN_VARIANT (ttl);
1230 ttr = TYPE_MAIN_VARIANT (ttr);
1231 if (same_type_p (ttl, ttr))
1234 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1237 if ((TREE_CODE (ttr) == POINTER_TYPE
1238 || TREE_CODE (ttr) == REFERENCE_TYPE)
1239 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1240 of the type pointed to. This is necessary for reference init
1241 semantics. We won't get here from a previous call with nptrs == 1;
1242 for multi-level pointers we end up in comp_ptr_ttypes. */
1245 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1247 ttl = TREE_TYPE (ttl);
1248 ttr = TREE_TYPE (ttr);
1252 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1253 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1255 else if (TREE_CODE (ttl) == VOID_TYPE
1256 && TREE_CODE (ttr) != FUNCTION_TYPE
1257 && TREE_CODE (ttr) != METHOD_TYPE
1258 && TREE_CODE (ttr) != OFFSET_TYPE)
1260 else if (TREE_CODE (ttr) == VOID_TYPE
1261 && TREE_CODE (ttl) != FUNCTION_TYPE
1262 && TREE_CODE (ttl) != METHOD_TYPE
1263 && TREE_CODE (ttl) != OFFSET_TYPE)
1265 else if (TREE_CODE (ttl) == POINTER_TYPE
1266 || TREE_CODE (ttl) == ARRAY_TYPE)
1268 if (comp_ptr_ttypes (ttl, ttr))
1270 else if (comp_ptr_ttypes (ttr, ttl))
1276 /* Const and volatile mean something different for function types,
1277 so the usual checks are not appropriate. */
1278 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1279 return comp_target_types (ttl, ttr, nptrs - 1);
1281 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1284 if (TREE_CODE (ttr) == ARRAY_TYPE)
1285 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1286 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1293 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1298 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1307 argsl = TYPE_ARG_TYPES (ttl);
1308 argsr = TYPE_ARG_TYPES (ttr);
1310 /* Compare 'this' here, not in comp_target_parms. */
1311 if (TREE_CODE (ttr) == METHOD_TYPE)
1313 tree tl = TYPE_METHOD_BASETYPE (ttl);
1314 tree tr = TYPE_METHOD_BASETYPE (ttr);
1316 if (!same_or_base_type_p (tr, tl))
1318 if (same_or_base_type_p (tl, tr))
1324 argsl = TREE_CHAIN (argsl);
1325 argsr = TREE_CHAIN (argsr);
1328 switch (comp_target_parms (argsl, argsr, 1))
1336 return saw_contra ? -1 : 1;
1339 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1343 /* Contravariance: we can assign a pointer to base member to a pointer
1344 to derived member. Note difference from simple pointer case, where
1345 we can pass a pointer to derived to a pointer to base. */
1346 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1347 TYPE_OFFSET_BASETYPE (ttl)))
1349 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1350 TYPE_OFFSET_BASETYPE (ttr)))
1360 ttl = TREE_TYPE (ttl);
1361 ttr = TREE_TYPE (ttr);
1363 if (TREE_CODE (ttl) == POINTER_TYPE
1364 || TREE_CODE (ttl) == ARRAY_TYPE)
1366 if (comp_ptr_ttypes (ttl, ttr))
1372 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1377 else if (IS_AGGR_TYPE (ttl))
1381 if (same_or_base_type_p (build_pointer_type (ttl),
1382 build_pointer_type (ttr)))
1384 if (same_or_base_type_p (build_pointer_type (ttr),
1385 build_pointer_type (ttl)))
1393 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1396 at_least_as_qualified_p (type1, type2)
1400 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1401 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1402 == CP_TYPE_QUALS (type2));
1405 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1408 more_qualified_p (type1, type2)
1412 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1413 && at_least_as_qualified_p (type1, type2));
1416 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1417 more cv-qualified that TYPE1, and 0 otherwise. */
1420 comp_cv_qualification (type1, type2)
1424 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1427 if (at_least_as_qualified_p (type1, type2))
1430 else if (at_least_as_qualified_p (type2, type1))
1436 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1437 subset of the cv-qualification signature of TYPE2, and the types
1438 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1441 comp_cv_qual_signature (type1, type2)
1445 if (comp_ptr_ttypes_real (type2, type1, -1))
1447 else if (comp_ptr_ttypes_real (type1, type2, -1))
1453 /* If two types share a common base type, return that basetype.
1454 If there is not a unique most-derived base type, this function
1455 returns ERROR_MARK_NODE. */
1458 common_base_type (tt1, tt2)
1461 tree best = NULL_TREE;
1464 /* If one is a baseclass of another, that's good enough. */
1465 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1467 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1470 /* Otherwise, try to find a unique baseclass of TT1
1471 that is shared by TT2, and follow that down. */
1472 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1474 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1475 tree trial = common_base_type (basetype, tt2);
1478 if (trial == error_mark_node)
1480 if (best == NULL_TREE)
1482 else if (best != trial)
1483 return error_mark_node;
1488 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1490 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1491 tree trial = common_base_type (tt1, basetype);
1494 if (trial == error_mark_node)
1496 if (best == NULL_TREE)
1498 else if (best != trial)
1499 return error_mark_node;
1505 /* Subroutines of `comptypes'. */
1507 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1508 equivalent in the sense that functions with those parameter types
1509 can have equivalent types. The two lists must be equivalent,
1512 C++: See comment above about TYPE1, TYPE2. */
1515 compparms (parms1, parms2)
1516 tree parms1, parms2;
1518 register tree t1 = parms1, t2 = parms2;
1520 /* An unspecified parmlist matches any specified parmlist
1521 whose argument types don't need default promotions. */
1525 if (t1 == 0 && t2 == 0)
1527 /* If one parmlist is shorter than the other,
1528 they fail to match. */
1529 if (t1 == 0 || t2 == 0)
1531 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1534 t1 = TREE_CHAIN (t1);
1535 t2 = TREE_CHAIN (t2);
1539 /* This really wants return whether or not parameter type lists
1540 would make their owning functions assignment compatible or not.
1542 The return value is like for comp_target_types.
1544 This should go away, possibly with the exception of the empty parmlist
1545 conversion; there are no conversions between function types in C++.
1546 (jason 17 Apr 1997) */
1549 comp_target_parms (parms1, parms2, strict)
1550 tree parms1, parms2;
1553 register tree t1 = parms1, t2 = parms2;
1554 int warn_contravariance = 0;
1556 /* In C, an unspecified parmlist matches any specified parmlist
1557 whose argument types don't need default promotions. This is not
1558 true for C++, but let's do it anyway for unfixed headers. */
1560 if (t1 == 0 && t2 != 0)
1562 if (! flag_strict_prototype && t2 == void_list_node)
1563 /* t1 might be the arglist of a function pointer in extern "C"
1564 declared to take (), which we fudged to (...). Don't make the
1565 user pay for our mistake. */;
1567 cp_pedwarn ("ANSI C++ prohibits conversion from `%#T' to `(...)'",
1569 return self_promoting_args_p (t2);
1572 return self_promoting_args_p (t1);
1574 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1578 /* If one parmlist is shorter than the other,
1579 they fail to match, unless STRICT is <= 0. */
1580 if (t1 == 0 || t2 == 0)
1585 return 1 + warn_contravariance;
1586 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1588 p1 = TREE_VALUE (t1);
1589 p2 = TREE_VALUE (t2);
1590 if (same_type_p (p1, p2))
1596 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1597 || (TREE_CODE (p1) == REFERENCE_TYPE
1598 && TREE_CODE (p2) == REFERENCE_TYPE))
1601 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1602 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1605 /* The following is wrong for contravariance,
1606 but many programs depend on it. */
1607 if (TREE_TYPE (p1) == void_type_node)
1609 if (TREE_TYPE (p2) == void_type_node)
1611 warn_contravariance = 1;
1614 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1615 && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (p1)),
1616 TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1619 /* Note backwards order due to contravariance. */
1620 if (comp_target_types (p2, p1, 1) <= 0)
1622 if (comp_target_types (p1, p2, 1) > 0)
1624 warn_contravariance = 1;
1631 return warn_contravariance ? -1 : 1;
1634 /* Return 1 if PARMS specifies a fixed number of parameters
1635 and none of their types is affected by default promotions. */
1638 self_promoting_args_p (parms)
1642 for (t = parms; t; t = TREE_CHAIN (t))
1644 register tree type = TREE_VALUE (t);
1646 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1652 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1655 if (C_PROMOTING_INTEGER_TYPE_P (type))
1661 /* Compute the value of the `sizeof' operator. */
1667 enum tree_code code = TREE_CODE (type);
1670 if (processing_template_decl)
1671 return build_min (SIZEOF_EXPR, sizetype, type);
1673 if (code == FUNCTION_TYPE)
1675 if (pedantic || warn_pointer_arith)
1676 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1677 return size_int (1);
1679 if (code == METHOD_TYPE)
1681 if (pedantic || warn_pointer_arith)
1682 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1683 return size_int (1);
1685 if (code == VOID_TYPE)
1687 if (pedantic || warn_pointer_arith)
1688 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1689 return size_int (1);
1691 if (code == ERROR_MARK)
1692 return size_int (1);
1694 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1695 referenced object.'' */
1696 if (code == REFERENCE_TYPE)
1697 type = TREE_TYPE (type);
1699 /* @@ This also produces an error for a signature ref.
1700 In that case we should be able to do better. */
1701 if (IS_SIGNATURE (type))
1703 error ("`sizeof' applied to a signature type");
1704 return size_int (0);
1706 else if (code == OFFSET_TYPE)
1708 cp_error ("`sizeof' applied to non-static member");
1709 return size_int (0);
1712 if (TYPE_SIZE (complete_type (type)) == 0)
1714 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1715 return size_int (0);
1718 /* Convert in case a char is more than one unit. */
1719 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1720 size_int (TYPE_PRECISION (char_type_node)));
1721 t = convert (sizetype, t);
1722 /* size_binop does not put the constant in range, so do it now. */
1723 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1724 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1732 if (processing_template_decl)
1733 return build_min (SIZEOF_EXPR, sizetype, e);
1735 if (TREE_CODE (e) == COMPONENT_REF
1736 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1737 error ("sizeof applied to a bit-field");
1738 /* ANSI says arrays and functions are converted inside comma.
1739 But we can't really convert them in build_compound_expr
1740 because that would break commas in lvalues.
1741 So do the conversion here if operand was a comma. */
1742 if (TREE_CODE (e) == COMPOUND_EXPR
1743 && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1744 || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1745 e = default_conversion (e);
1746 else if (is_overloaded_fn (e))
1748 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1749 return size_int (1);
1751 else if (type_unknown_p (e))
1753 incomplete_type_error (e, TREE_TYPE (e));
1754 return size_int (1);
1756 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1757 member unless you're in a non-static member of X. But, we used
1758 to support this usage, so we still permit it unless we're being
1760 else if (TREE_CODE (e) == OFFSET_REF)
1761 e = resolve_offset_ref (e);
1763 if (e == error_mark_node)
1766 return c_sizeof (TREE_TYPE (e));
1770 c_sizeof_nowarn (type)
1773 enum tree_code code = TREE_CODE (type);
1776 if (code == FUNCTION_TYPE
1777 || code == METHOD_TYPE
1778 || code == VOID_TYPE
1779 || code == ERROR_MARK)
1780 return size_int (1);
1781 if (code == REFERENCE_TYPE)
1782 type = TREE_TYPE (type);
1784 if (TYPE_SIZE (type) == 0)
1785 return size_int (0);
1787 /* Convert in case a char is more than one unit. */
1788 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1789 size_int (TYPE_PRECISION (char_type_node)));
1790 t = convert (sizetype, t);
1791 force_fit_type (t, 0);
1795 /* Implement the __alignof keyword: Return the minimum required
1796 alignment of TYPE, measured in bytes. */
1802 enum tree_code code = TREE_CODE (type);
1805 if (processing_template_decl)
1806 return build_min (ALIGNOF_EXPR, sizetype, type);
1808 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1809 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1811 if (code == VOID_TYPE || code == ERROR_MARK)
1812 return size_int (1);
1814 /* C++: this is really correct! */
1815 if (code == REFERENCE_TYPE)
1816 type = TREE_TYPE (type);
1818 /* @@ This also produces an error for a signature ref.
1819 In that case we should be able to do better. */
1820 if (IS_SIGNATURE (type))
1822 error ("`__alignof' applied to a signature type");
1823 return size_int (1);
1826 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1827 force_fit_type (t, 0);
1831 /* Perform the array-to-pointer and function-to-pointer conversions
1834 In addition, references are converted to rvalues and manifest
1835 constants are replaced by their values. */
1838 decay_conversion (exp)
1842 register enum tree_code code;
1844 if (TREE_CODE (exp) == OFFSET_REF)
1845 exp = resolve_offset_ref (exp);
1847 type = TREE_TYPE (exp);
1848 code = TREE_CODE (type);
1850 if (code == REFERENCE_TYPE)
1852 exp = convert_from_reference (exp);
1853 type = TREE_TYPE (exp);
1854 code = TREE_CODE (type);
1857 /* Constants can be used directly unless they're not loadable. */
1858 if (TREE_CODE (exp) == CONST_DECL)
1859 exp = DECL_INITIAL (exp);
1860 /* Replace a nonvolatile const static variable with its value. We
1861 don't do this for arrays, though; we want the address of the
1862 first element of the array, not the address of the first element
1863 of its initializing constant. We *do* replace variables that the
1864 user isn't really supposed to know about; this is a hack to deal
1865 with __PRETTY_FUNCTION__ and the like. */
1866 else if (TREE_READONLY_DECL_P (exp)
1867 && (code != ARRAY_TYPE
1868 || (TREE_CODE (exp) == VAR_DECL && DECL_IGNORED_P (exp))))
1870 exp = decl_constant_value (exp);
1871 type = TREE_TYPE (exp);
1874 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1875 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1877 if (code == VOID_TYPE)
1879 error ("void value not ignored as it ought to be");
1880 return error_mark_node;
1882 if (code == METHOD_TYPE)
1883 my_friendly_abort (990506);
1884 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1885 return build_unary_op (ADDR_EXPR, exp, 0);
1886 if (code == ARRAY_TYPE)
1891 if (TREE_CODE (exp) == INDIRECT_REF)
1893 /* Stripping away the INDIRECT_REF is not the right
1894 thing to do for references... */
1895 tree inner = TREE_OPERAND (exp, 0);
1896 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1898 inner = build1 (CONVERT_EXPR,
1899 build_pointer_type (TREE_TYPE
1900 (TREE_TYPE (inner))),
1902 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1904 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1907 if (TREE_CODE (exp) == COMPOUND_EXPR)
1909 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1910 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1911 TREE_OPERAND (exp, 0), op1);
1915 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1917 error ("invalid use of non-lvalue array");
1918 return error_mark_node;
1921 ptrtype = build_pointer_type (TREE_TYPE (type));
1923 if (TREE_CODE (exp) == VAR_DECL)
1925 /* ??? This is not really quite correct
1926 in that the type of the operand of ADDR_EXPR
1927 is not the target type of the type of the ADDR_EXPR itself.
1928 Question is, can this lossage be avoided? */
1929 adr = build1 (ADDR_EXPR, ptrtype, exp);
1930 if (mark_addressable (exp) == 0)
1931 return error_mark_node;
1932 TREE_CONSTANT (adr) = staticp (exp);
1933 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1936 /* This way is better for a COMPONENT_REF since it can
1937 simplify the offset for a component. */
1938 adr = build_unary_op (ADDR_EXPR, exp, 1);
1939 return cp_convert (ptrtype, adr);
1946 default_conversion (exp)
1950 enum tree_code code;
1952 exp = decay_conversion (exp);
1954 type = TREE_TYPE (exp);
1955 code = TREE_CODE (type);
1957 if (INTEGRAL_CODE_P (code))
1959 tree t = type_promotes_to (type);
1961 return cp_convert (t, exp);
1967 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1971 inline_conversion (exp)
1974 if (TREE_CODE (exp) == FUNCTION_DECL)
1975 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1980 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1981 decay_conversion to one. */
1984 string_conv_p (totype, exp, warn)
1990 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1993 t = TREE_TYPE (totype);
1994 if (!same_type_p (t, char_type_node)
1995 && !same_type_p (t, wchar_type_node))
1998 if (TREE_CODE (exp) == STRING_CST)
2000 /* Make sure that we don't try to convert between char and wchar_t. */
2001 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2006 /* Is this a string constant which has decayed to 'const char *'? */
2007 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
2008 if (!same_type_p (TREE_TYPE (exp), t))
2011 if (TREE_CODE (exp) != ADDR_EXPR
2012 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2016 /* This warning is not very useful, as it complains about printf. */
2017 if (warn && warn_write_strings)
2018 cp_warning ("deprecated conversion from string constant to `%T'", totype);
2024 build_object_ref (datum, basetype, field)
2025 tree datum, basetype, field;
2028 if (datum == error_mark_node)
2029 return error_mark_node;
2031 dtype = TREE_TYPE (datum);
2032 if (TREE_CODE (dtype) == REFERENCE_TYPE)
2033 dtype = TREE_TYPE (dtype);
2034 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
2036 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
2037 basetype, field, dtype);
2038 return error_mark_node;
2040 else if (IS_SIGNATURE (basetype))
2042 warning ("signature name in scope resolution ignored");
2043 return build_component_ref (datum, field, NULL_TREE, 1);
2045 else if (is_aggr_type (basetype, 1))
2047 tree binfo = binfo_or_else (basetype, dtype);
2049 return build_x_component_ref (build_scoped_ref (datum, basetype),
2052 return error_mark_node;
2055 /* Like `build_component_ref, but uses an already found field, and converts
2056 from a reference. Must compute access for current_class_ref.
2060 build_component_ref_1 (datum, field, protect)
2064 return convert_from_reference
2065 (build_component_ref (datum, field, NULL_TREE, protect));
2068 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2069 can, for example, use as an lvalue. This code used to be in
2070 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2071 expressions, where we're dealing with aggregates. But now it's again only
2072 called from unary_complex_lvalue. The case (in particular) that led to
2073 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2077 rationalize_conditional_expr (code, t)
2078 enum tree_code code;
2081 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2082 the first operand is always the one to be used if both operands
2083 are equal, so we know what conditional expression this used to be. */
2084 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2087 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
2088 ? LE_EXPR : GE_EXPR),
2089 TREE_OPERAND (t, 0),
2090 TREE_OPERAND (t, 1)),
2091 build_unary_op (code, TREE_OPERAND (t, 0), 0),
2092 build_unary_op (code, TREE_OPERAND (t, 1), 0));
2096 build_conditional_expr (TREE_OPERAND (t, 0),
2097 build_unary_op (code, TREE_OPERAND (t, 1), 0),
2098 build_unary_op (code, TREE_OPERAND (t, 2), 0));
2101 /* Given the TYPE of an anonymous union field inside T, return the
2102 FIELD_DECL for the field. If not found return NULL_TREE. Because
2103 anonymous unions can nest, we must also search all anonymous unions
2104 that are directly reachable. */
2107 lookup_anon_field (t, type)
2112 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2114 if (TREE_STATIC (field))
2116 if (TREE_CODE (field) != FIELD_DECL)
2119 /* If we find it directly, return the field. */
2120 if (DECL_NAME (field) == NULL_TREE
2121 && type == TREE_TYPE (field))
2126 /* Otherwise, it could be nested, search harder. */
2127 if (DECL_NAME (field) == NULL_TREE
2128 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2130 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2138 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2139 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2140 that we are interested in, or it can be a FIELD_DECL. */
2143 build_component_ref (datum, component, basetype_path, protect)
2144 tree datum, component, basetype_path;
2147 register tree basetype;
2148 register enum tree_code code;
2149 register tree field = NULL;
2154 if (processing_template_decl)
2155 return build_min_nt (COMPONENT_REF, datum, component);
2157 if (datum == error_mark_node
2158 || TREE_TYPE (datum) == error_mark_node)
2159 return error_mark_node;
2161 /* BASETYPE holds the type of the class containing the COMPONENT. */
2162 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2164 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2166 switch (TREE_CODE (datum))
2170 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2171 basetype_path, protect);
2172 return build (COMPOUND_EXPR, TREE_TYPE (value),
2173 TREE_OPERAND (datum, 0), value);
2176 return build_conditional_expr
2177 (TREE_OPERAND (datum, 0),
2178 build_component_ref (TREE_OPERAND (datum, 1), component,
2179 basetype_path, protect),
2180 build_component_ref (TREE_OPERAND (datum, 2), component,
2181 basetype_path, protect));
2184 cp_error ("invalid use of %D", datum);
2185 datum = error_mark_node;
2192 code = TREE_CODE (basetype);
2194 if (code == REFERENCE_TYPE)
2196 datum = convert_from_reference (datum);
2197 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2198 code = TREE_CODE (basetype);
2200 if (TREE_CODE (datum) == OFFSET_REF)
2202 datum = resolve_offset_ref (datum);
2203 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2204 code = TREE_CODE (basetype);
2207 /* First, see if there is a field or component with name COMPONENT. */
2208 if (TREE_CODE (component) == TREE_LIST)
2210 /* I could not trigger this code. MvL */
2211 my_friendly_abort (980326);
2213 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2214 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2216 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2219 if (! IS_AGGR_TYPE_CODE (code))
2221 if (code != ERROR_MARK)
2222 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2223 component, datum, basetype);
2224 return error_mark_node;
2227 if (!complete_type_or_else (basetype, datum))
2228 return error_mark_node;
2230 if (TREE_CODE (component) == BIT_NOT_EXPR)
2232 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2234 cp_error ("destructor specifier `%T::~%T' must have matching names",
2235 basetype, TREE_OPERAND (component, 0));
2236 return error_mark_node;
2238 if (! TYPE_HAS_DESTRUCTOR (basetype))
2240 cp_error ("type `%T' has no destructor", basetype);
2241 return error_mark_node;
2243 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2246 /* Look up component name in the structure type definition. */
2247 if (CLASSTYPE_VFIELD (basetype)
2248 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
2249 /* Special-case this because if we use normal lookups in an ambiguous
2250 hierarchy, the compiler will abort (because vptr lookups are
2251 not supposed to be ambiguous. */
2252 field = CLASSTYPE_VFIELD (basetype);
2253 else if (TREE_CODE (component) == FIELD_DECL)
2255 else if (TREE_CODE (component) == TYPE_DECL)
2257 cp_error ("invalid use of type decl `%#D' as expression", component);
2258 return error_mark_node;
2260 else if (TREE_CODE (component) == TEMPLATE_DECL)
2262 cp_error ("invalid use of template `%#D' as expression", component);
2263 return error_mark_node;
2267 tree name = component;
2268 if (TREE_CODE (component) == VAR_DECL)
2269 name = DECL_NAME (component);
2270 if (TREE_CODE (component) == NAMESPACE_DECL)
2271 /* Source is in error, but produce a sensible diagnostic. */
2272 name = DECL_NAME (component);
2273 if (basetype_path == NULL_TREE)
2274 basetype_path = TYPE_BINFO (basetype);
2275 field = lookup_field (basetype_path, name,
2276 protect && !VFIELD_NAME_P (name), 0);
2277 if (field == error_mark_node)
2278 return error_mark_node;
2280 if (field == NULL_TREE)
2282 /* Not found as a data field, look for it as a method. If found,
2283 then if this is the only possible one, return it, else
2284 report ambiguity error. */
2285 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2286 if (fndecls == error_mark_node)
2287 return error_mark_node;
2290 /* If the function is unique and static, we can resolve it
2291 now. Otherwise, we have to wait and see what context it is
2292 used in; a component_ref involving a non-static member
2293 function can only be used in a call (expr.ref). */
2295 if (TREE_CHAIN (fndecls) == NULL_TREE
2296 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2298 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2300 tree fndecl = TREE_VALUE (fndecls);
2301 enforce_access (TREE_PURPOSE (fndecls), fndecl);
2307 /* A unique non-static member function. Other parts
2308 of the compiler expect something with
2309 unknown_type_node to be really overloaded, so
2311 TREE_VALUE (fndecls)
2312 = scratch_ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2316 ref = build (COMPONENT_REF, unknown_type_node,
2317 datum, TREE_VALUE (fndecls));
2321 cp_error ("`%#T' has no member named `%D'", basetype, name);
2322 return error_mark_node;
2324 else if (TREE_TYPE (field) == error_mark_node)
2325 return error_mark_node;
2327 if (TREE_CODE (field) != FIELD_DECL)
2329 if (TREE_CODE (field) == TYPE_DECL)
2330 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2331 else if (DECL_RTL (field) != 0)
2334 TREE_USED (field) = 1;
2339 /* See if we have to do any conversions so that we pick up the field from the
2341 if (DECL_FIELD_CONTEXT (field) != basetype)
2343 tree context = DECL_FIELD_CONTEXT (field);
2344 tree base = context;
2345 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2346 && ANON_AGGR_TYPE_P (base))
2348 base = TYPE_CONTEXT (base);
2351 /* Handle base classes here... */
2352 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2354 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2355 if (integer_zerop (addr))
2357 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2358 return error_mark_node;
2360 if (VBASE_NAME_P (DECL_NAME (field)))
2362 /* It doesn't matter which vbase pointer we grab, just
2363 find one of them. */
2364 tree binfo = get_binfo (base,
2365 TREE_TYPE (TREE_TYPE (addr)), 0);
2366 addr = convert_pointer_to_real (binfo, addr);
2369 addr = convert_pointer_to (base, addr);
2370 datum = build_indirect_ref (addr, NULL_PTR);
2371 my_friendly_assert (datum != error_mark_node, 311);
2375 /* Handle things from anon unions here... */
2376 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2378 tree subfield = lookup_anon_field (basetype, context);
2379 tree subdatum = build_component_ref (datum, subfield,
2380 basetype_path, protect);
2381 return build_component_ref (subdatum, field, basetype_path, protect);
2385 /* Compute the type of the field, as described in [expr.ref]. */
2386 type_quals = TYPE_UNQUALIFIED;
2387 field_type = TREE_TYPE (field);
2388 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2389 /* The standard says that the type of the result should be the
2390 type referred to by the reference. But for now, at least, we
2391 do the conversion from reference type later. */
2395 type_quals = (CP_TYPE_QUALS (field_type)
2396 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2398 /* A field is const (volatile) if the enclosing object, or the
2399 field itself, is const (volatile). But, a mutable field is
2400 not const, even within a const object. */
2401 if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2402 type_quals &= ~TYPE_QUAL_CONST;
2403 if (!IS_SIGNATURE (field_type))
2404 field_type = cp_build_qualified_type (field_type, type_quals);
2407 ref = fold (build (COMPONENT_REF, field_type,
2408 break_out_cleanups (datum), field));
2410 /* Mark the expression const or volatile, as appropriate. Even
2411 though we've dealt with the type above, we still have to mark the
2412 expression itself. */
2413 if (type_quals & TYPE_QUAL_CONST)
2414 TREE_READONLY (ref) = 1;
2415 else if (type_quals & TYPE_QUAL_VOLATILE)
2416 TREE_THIS_VOLATILE (ref) = 1;
2421 /* Variant of build_component_ref for use in expressions, which should
2422 never have REFERENCE_TYPE. */
2425 build_x_component_ref (datum, component, basetype_path, protect)
2426 tree datum, component, basetype_path;
2429 tree t = build_component_ref (datum, component, basetype_path, protect);
2431 if (! processing_template_decl)
2432 t = convert_from_reference (t);
2437 /* Given an expression PTR for a pointer, return an expression
2438 for the value pointed to.
2439 ERRORSTRING is the name of the operator to appear in error messages.
2441 This function may need to overload OPERATOR_FNNAME.
2442 Must also handle REFERENCE_TYPEs for C++. */
2445 build_x_indirect_ref (ptr, errorstring)
2447 const char *errorstring;
2451 if (processing_template_decl)
2452 return build_min_nt (INDIRECT_REF, ptr);
2454 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2458 return build_indirect_ref (ptr, errorstring);
2462 build_indirect_ref (ptr, errorstring)
2464 const char *errorstring;
2466 register tree pointer, type;
2468 if (ptr == error_mark_node)
2469 return error_mark_node;
2471 if (ptr == current_class_ptr)
2472 return current_class_ref;
2474 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2475 ? ptr : default_conversion (ptr));
2476 type = TREE_TYPE (pointer);
2478 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2482 If the type of the expression is "pointer to T," the type
2483 of the result is "T."
2485 We must use the canonical variant because certain parts of
2486 the back end, like fold, do pointer comparisons between
2488 tree t = canonical_type_variant (TREE_TYPE (type));
2490 if (TREE_CODE (pointer) == ADDR_EXPR
2492 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2493 /* The POINTER was something like `&x'. We simplify `*&x' to
2495 return TREE_OPERAND (pointer, 0);
2498 tree ref = build1 (INDIRECT_REF, t, pointer);
2500 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2501 so that we get the proper error message if the result is used
2502 to assign to. Also, &* is supposed to be a no-op. */
2503 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2504 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2505 TREE_SIDE_EFFECTS (ref)
2506 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2511 /* `pointer' won't be an error_mark_node if we were given a
2512 pointer to member, so it's cool to check for this here. */
2513 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2514 error ("invalid use of `%s' on pointer to member", errorstring);
2515 else if (TREE_CODE (type) == RECORD_TYPE
2516 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2517 error ("cannot dereference signature pointer/reference");
2518 else if (pointer != error_mark_node)
2521 error ("invalid type argument of `%s'", errorstring);
2523 error ("invalid type argument");
2525 return error_mark_node;
2528 /* This handles expressions of the form "a[i]", which denotes
2531 This is logically equivalent in C to *(a+i), but we may do it differently.
2532 If A is a variable or a member, we generate a primitive ARRAY_REF.
2533 This avoids forcing the array out of registers, and can work on
2534 arrays that are not lvalues (for example, members of structures returned
2537 If INDEX is of some user-defined type, it must be converted to
2538 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2539 will inherit the type of the array, which will be some pointer type. */
2542 build_array_ref (array, idx)
2547 error ("subscript missing in array reference");
2548 return error_mark_node;
2551 if (TREE_TYPE (array) == error_mark_node
2552 || TREE_TYPE (idx) == error_mark_node)
2553 return error_mark_node;
2555 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2556 && TREE_CODE (array) != INDIRECT_REF)
2560 /* Subscripting with type char is likely to lose
2561 on a machine where chars are signed.
2562 So warn on any machine, but optionally.
2563 Don't warn for unsigned char since that type is safe.
2564 Don't warn for signed char because anyone who uses that
2565 must have done so deliberately. */
2566 if (warn_char_subscripts
2567 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2568 warning ("array subscript has type `char'");
2570 /* Apply default promotions *after* noticing character types. */
2571 idx = default_conversion (idx);
2573 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2575 error ("array subscript is not an integer");
2576 return error_mark_node;
2579 /* An array that is indexed by a non-constant
2580 cannot be stored in a register; we must be able to do
2581 address arithmetic on its address.
2582 Likewise an array of elements of variable size. */
2583 if (TREE_CODE (idx) != INTEGER_CST
2584 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2585 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2588 if (mark_addressable (array) == 0)
2589 return error_mark_node;
2591 /* An array that is indexed by a constant value which is not within
2592 the array bounds cannot be stored in a register either; because we
2593 would get a crash in store_bit_field/extract_bit_field when trying
2594 to access a non-existent part of the register. */
2595 if (TREE_CODE (idx) == INTEGER_CST
2596 && TYPE_VALUES (TREE_TYPE (array))
2597 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2599 if (mark_addressable (array) == 0)
2600 return error_mark_node;
2603 if (pedantic && !lvalue_p (array))
2604 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2606 /* Note in C++ it is valid to subscript a `register' array, since
2607 it is valid to take the address of something with that
2608 storage specification. */
2612 while (TREE_CODE (foo) == COMPONENT_REF)
2613 foo = TREE_OPERAND (foo, 0);
2614 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2615 warning ("subscripting array declared `register'");
2618 type = TREE_TYPE (TREE_TYPE (array));
2619 rval = build (ARRAY_REF, type, array, idx);
2620 /* Array ref is const/volatile if the array elements are
2621 or if the array is.. */
2622 TREE_READONLY (rval)
2623 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2624 TREE_SIDE_EFFECTS (rval)
2625 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2626 TREE_THIS_VOLATILE (rval)
2627 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2628 return require_complete_type (fold (rval));
2632 tree ar = default_conversion (array);
2633 tree ind = default_conversion (idx);
2635 /* Put the integer in IND to simplify error checking. */
2636 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2643 if (ar == error_mark_node)
2646 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2648 error ("subscripted value is neither array nor pointer");
2649 return error_mark_node;
2651 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2653 error ("array subscript is not an integer");
2654 return error_mark_node;
2657 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar,
2663 /* Build a function call to function FUNCTION with parameters PARAMS.
2664 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2665 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2666 not include any object pointer that may be required. FUNCTION's
2667 data type may be a function type or a pointer-to-function.
2669 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2670 is the list of possible methods that FUNCTION could conceivably
2671 be. If the list of methods comes from a class, then it will be
2672 a list of lists (where each element is associated with the class
2673 that produced it), otherwise it will be a simple list (for
2674 functions overloaded in global scope).
2676 In the first case, TREE_VALUE (function) is the head of one of those
2677 lists, and TREE_PURPOSE is the name of the function.
2679 In the second case, TREE_PURPOSE (function) is the function's
2682 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2684 When calling a TEMPLATE_DECL, we don't require a complete return
2688 build_x_function_call (function, params, decl)
2689 tree function, params, decl;
2692 tree template_id = NULL_TREE;
2695 if (function == error_mark_node)
2696 return error_mark_node;
2698 if (processing_template_decl)
2699 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2701 /* Save explicit template arguments if found */
2702 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2704 template_id = function;
2705 function = TREE_OPERAND (function, 0);
2708 type = TREE_TYPE (function);
2710 if (TREE_CODE (type) == OFFSET_TYPE
2711 && TREE_TYPE (type) == unknown_type_node
2712 && TREE_CODE (function) == TREE_LIST
2713 && TREE_CHAIN (function) == NULL_TREE)
2715 /* Undo (Foo:bar)()... */
2716 type = TYPE_OFFSET_BASETYPE (type);
2717 function = TREE_VALUE (function);
2718 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2719 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2720 function = TREE_VALUE (function);
2721 if (TREE_CODE (function) == OVERLOAD)
2722 function = OVL_FUNCTION (function);
2723 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2724 function = DECL_NAME (function);
2725 return build_method_call (decl, function, params,
2726 TYPE_BINFO (type), LOOKUP_NORMAL);
2729 if ((TREE_CODE (function) == FUNCTION_DECL
2730 && DECL_STATIC_FUNCTION_P (function))
2731 || (TREE_CODE (function) == TEMPLATE_DECL
2732 && DECL_STATIC_FUNCTION_P (DECL_RESULT (function))))
2733 return build_member_call(DECL_CONTEXT (function),
2735 ? template_id : DECL_NAME (function),
2738 is_method = ((TREE_CODE (function) == TREE_LIST
2739 && current_class_type != NULL_TREE
2740 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2742 || (TREE_CODE (function) == OVERLOAD
2743 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2744 || TREE_CODE (function) == IDENTIFIER_NODE
2745 || TREE_CODE (type) == METHOD_TYPE
2746 || TYPE_PTRMEMFUNC_P (type));
2748 /* A friend template. Make it look like a toplevel declaration. */
2749 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2750 function = scratch_ovl_cons (function, NULL_TREE);
2752 /* Handle methods, friends, and overloaded functions, respectively. */
2755 tree basetype = NULL_TREE;
2757 if (TREE_CODE (function) == OVERLOAD)
2758 function = OVL_CURRENT (function);
2760 if (TREE_CODE (function) == FUNCTION_DECL
2761 || DECL_FUNCTION_TEMPLATE_P (function))
2763 basetype = DECL_CLASS_CONTEXT (function);
2765 if (DECL_NAME (function))
2766 function = DECL_NAME (function);
2768 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2770 else if (TREE_CODE (function) == TREE_LIST)
2772 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2773 == FUNCTION_DECL, 312);
2774 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2775 function = TREE_PURPOSE (function);
2777 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2779 if (TREE_CODE (function) == OFFSET_REF)
2781 if (TREE_OPERAND (function, 0))
2782 decl = TREE_OPERAND (function, 0);
2784 /* Call via a pointer to member function. */
2785 if (decl == NULL_TREE)
2787 error ("pointer to member function called, but not in class scope");
2788 return error_mark_node;
2790 /* What other type of POINTER_TYPE could this be? */
2791 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2792 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2793 && TREE_CODE (function) != OFFSET_REF)
2794 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2799 /* this is an abbreviated method call.
2800 must go through here in case it is a virtual function.
2801 @@ Perhaps this could be optimized. */
2803 if (basetype && (! current_class_type
2804 || ! DERIVED_FROM_P (basetype, current_class_type)))
2805 return build_member_call (basetype, function, params);
2807 if (decl == NULL_TREE)
2809 if (current_class_type == NULL_TREE)
2811 cp_error ("object missing in call to method `%D'", function);
2812 return error_mark_node;
2814 /* Yow: call from a static member function. */
2815 decl = build_dummy_object (current_class_type);
2818 /* Put back explicit template arguments, if any. */
2820 function = template_id;
2821 return build_method_call (decl, function, params,
2822 NULL_TREE, LOOKUP_NORMAL);
2824 else if (TREE_CODE (function) == COMPONENT_REF
2825 && type == unknown_type_node)
2827 /* Undo what we did in build_component_ref. */
2828 decl = TREE_OPERAND (function, 0);
2829 function = TREE_OPERAND (function, 1);
2830 function = DECL_NAME (OVL_CURRENT (function));
2834 TREE_OPERAND (template_id, 0) = function;
2835 function = template_id;
2838 return build_method_call (decl, function, params,
2839 NULL_TREE, LOOKUP_NORMAL);
2841 else if (really_overloaded_fn (function))
2843 if (OVL_FUNCTION (function) == NULL_TREE)
2845 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2846 TREE_PURPOSE (function));
2847 return error_mark_node;
2851 /* Put back explicit template arguments, if any. */
2853 function = template_id;
2854 return build_new_function_call (function, params);
2858 /* Remove a potential OVERLOAD around it */
2859 function = OVL_CURRENT (function);
2862 if (TREE_CODE (function) == OFFSET_REF)
2864 /* If the component is a data element (or a virtual function), we play
2865 games here to make things work. */
2868 if (TREE_OPERAND (function, 0))
2869 decl = TREE_OPERAND (function, 0);
2871 decl = current_class_ref;
2873 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2875 /* Sigh. OFFSET_REFs are being used for too many things.
2876 They're being used both for -> and ->*, and we want to resolve
2877 the -> cases here, but leave the ->*. We could use
2878 resolve_offset_ref for those, too, but it would call
2879 get_member_function_from_ptrfunc and decl_addr wouldn't get
2880 updated properly. Nasty. */
2881 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2882 function = resolve_offset_ref (function);
2884 function = TREE_OPERAND (function, 1);
2886 function = get_member_function_from_ptrfunc (&decl_addr, function);
2887 params = expr_tree_cons (NULL_TREE, decl_addr, params);
2888 return build_function_call (function, params);
2891 type = TREE_TYPE (function);
2892 if (type != error_mark_node)
2894 if (TREE_CODE (type) == REFERENCE_TYPE)
2895 type = TREE_TYPE (type);
2897 if (IS_AGGR_TYPE (type))
2898 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2903 tree fntype = TREE_TYPE (function);
2904 tree ctypeptr = NULL_TREE;
2906 /* Explicitly named method? */
2907 if (TREE_CODE (function) == FUNCTION_DECL)
2908 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2909 /* Expression with ptr-to-method type? It could either be a plain
2910 usage, or it might be a case where the ptr-to-method is being
2911 passed in as an argument. */
2912 else if (TYPE_PTRMEMFUNC_P (fntype))
2914 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2915 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2916 ctypeptr = build_pointer_type (rec);
2918 /* Unexpected node type? */
2920 my_friendly_abort (116);
2921 if (decl == NULL_TREE)
2923 if (current_function_decl
2924 && DECL_STATIC_FUNCTION_P (current_function_decl))
2925 error ("invalid call to member function needing `this' in static member function scope");
2927 error ("pointer to member function called, but not in class scope");
2928 return error_mark_node;
2930 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2931 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2933 decl = build_unary_op (ADDR_EXPR, decl, 0);
2934 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2937 decl = build_c_cast (ctypeptr, decl);
2938 params = expr_tree_cons (NULL_TREE, decl, params);
2941 return build_function_call (function, params);
2944 /* Resolve a pointer to member function. INSTANCE is the object
2945 instance to use, if the member points to a virtual member. */
2948 get_member_function_from_ptrfunc (instance_ptrptr, function)
2949 tree *instance_ptrptr;
2952 if (TREE_CODE (function) == OFFSET_REF)
2954 function = TREE_OPERAND (function, 1);
2957 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2959 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2960 tree instance, basetype;
2962 tree instance_ptr = *instance_ptrptr;
2964 if (TREE_SIDE_EFFECTS (instance_ptr))
2965 instance_ptr = save_expr (instance_ptr);
2967 if (TREE_SIDE_EFFECTS (function))
2968 function = save_expr (function);
2970 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2971 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2973 delta = cp_convert (ptrdiff_type_node,
2974 build_component_ref (function, delta_identifier,
2976 e3 = PFN_FROM_PTRMEMFUNC (function);
2978 if (TYPE_SIZE (basetype) != NULL_TREE
2979 && ! TYPE_VIRTUAL_P (basetype))
2980 /* If basetype doesn't have virtual functions, don't emit code to
2981 handle that case. */
2985 /* Promoting idx before saving it improves performance on RISC
2986 targets. Without promoting, the first compare used
2987 load-with-sign-extend, while the second used normal load then
2988 shift to sign-extend. An optimizer flaw, perhaps, but it's
2989 easier to make this change. */
2990 idx = save_expr (default_conversion
2991 (build_component_ref (function,
2994 e1 = build_binary_op (GE_EXPR, idx, integer_zero_node);
2996 /* Convert down to the right base, before using the instance. */
2997 instance = convert_pointer_to_real (basetype, instance_ptr);
2998 if (instance == error_mark_node && instance_ptr != error_mark_node)
3001 vtbl = convert_pointer_to (ptr_type_node, instance);
3002 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
3005 build_pointer_type (build_pointer_type (vtable_entry_type)),
3006 vtbl, cp_convert (ptrdiff_type_node, delta2));
3007 vtbl = build_indirect_ref (vtbl, NULL_PTR);
3008 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
3011 if (! flag_vtable_thunks)
3013 aref = save_expr (aref);
3015 delta = build_binary_op
3017 build_conditional_expr (e1,
3018 build_component_ref (aref,
3025 if (flag_vtable_thunks)
3028 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
3029 TREE_TYPE (e2) = TREE_TYPE (e3);
3030 e1 = build_conditional_expr (e1, e2, e3);
3032 /* Make sure this doesn't get evaluated first inside one of the
3033 branches of the COND_EXPR. */
3034 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
3035 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
3039 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
3040 instance_ptr, delta);
3042 if (instance_ptr == error_mark_node
3043 && TREE_CODE (e1) != ADDR_EXPR
3044 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
3045 cp_error ("object missing in `%E'", function);
3053 build_function_call_real (function, params, require_complete, flags)
3054 tree function, params;
3055 int require_complete, flags;
3057 register tree fntype, fndecl;
3058 register tree value_type;
3059 register tree coerced_params;
3060 tree name = NULL_TREE, assembler_name = NULL_TREE;
3063 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3064 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3065 if (TREE_CODE (function) == NOP_EXPR
3066 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3067 function = TREE_OPERAND (function, 0);
3069 if (TREE_CODE (function) == FUNCTION_DECL)
3071 name = DECL_NAME (function);
3072 assembler_name = DECL_ASSEMBLER_NAME (function);
3074 GNU_xref_call (current_function_decl,
3075 IDENTIFIER_POINTER (name ? name
3076 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
3078 mark_used (function);
3081 /* Convert anything with function type to a pointer-to-function. */
3082 if (pedantic && DECL_MAIN_P (function))
3083 pedwarn ("ANSI C++ forbids calling `main' from within program");
3085 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
3086 (because calling an inline function does not mean the function
3087 needs to be separately compiled). */
3089 if (DECL_INLINE (function))
3090 function = inline_conversion (function);
3092 function = build_addr_func (function);
3098 function = build_addr_func (function);
3101 if (function == error_mark_node)
3102 return error_mark_node;
3104 fntype = TREE_TYPE (function);
3106 if (TYPE_PTRMEMFUNC_P (fntype))
3108 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3110 return error_mark_node;
3113 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3114 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3116 if (!((TREE_CODE (fntype) == POINTER_TYPE
3117 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3119 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3121 cp_error ("`%E' cannot be used as a function", function);
3122 return error_mark_node;
3125 /* fntype now gets the type of function pointed to. */
3126 fntype = TREE_TYPE (fntype);
3128 /* Convert the parameters to the types declared in the
3129 function prototype, or apply default promotions. */
3131 if (flags & LOOKUP_COMPLAIN)
3132 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3133 params, fndecl, LOOKUP_NORMAL);
3135 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3138 if (coerced_params == error_mark_node)
3140 if (flags & LOOKUP_SPECULATIVELY)
3143 return error_mark_node;
3146 /* Check for errors in format strings. */
3148 if (warn_format && (name || assembler_name))
3149 check_function_format (name, assembler_name, coerced_params);
3151 /* Recognize certain built-in functions so we can make tree-codes
3152 other than CALL_EXPR. We do this when it enables fold-const.c
3153 to do something useful. */
3155 if (TREE_CODE (function) == ADDR_EXPR
3156 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3157 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3158 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
3163 if (coerced_params == 0)
3164 return integer_zero_node;
3165 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3172 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3174 register tree result
3175 = build_call (function, value_type, coerced_params);
3177 if (require_complete)
3179 if (TREE_CODE (value_type) == VOID_TYPE)
3181 result = require_complete_type (result);
3183 if (IS_AGGR_TYPE (value_type))
3184 result = build_cplus_new (value_type, result);
3185 return convert_from_reference (result);
3190 build_function_call (function, params)
3191 tree function, params;
3193 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3196 /* Convert the actual parameter expressions in the list VALUES
3197 to the types in the list TYPELIST.
3198 If parmdecls is exhausted, or when an element has NULL as its type,
3199 perform the default conversions.
3201 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3203 This is also where warnings about wrong number of args are generated.
3205 Return a list of expressions for the parameters as converted.
3207 Both VALUES and the returned value are chains of TREE_LIST nodes
3208 with the elements of the list in the TREE_VALUE slots of those nodes.
3210 In C++, unspecified trailing parameters can be filled in with their
3211 default arguments, if such were specified. Do so here. */
3214 convert_arguments (typelist, values, fndecl, flags)
3215 tree typelist, values, fndecl;
3218 register tree typetail, valtail;
3219 register tree result = NULL_TREE;
3220 const char *called_thing = 0;
3223 /* Argument passing is always copy-initialization. */
3224 flags |= LOOKUP_ONLYCONVERTING;
3228 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3230 if (DECL_NAME (fndecl) == NULL_TREE
3231 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3232 called_thing = "constructor";
3234 called_thing = "member function";
3237 called_thing = "function";
3240 for (valtail = values, typetail = typelist;
3242 valtail = TREE_CHAIN (valtail), i++)
3244 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3245 register tree val = TREE_VALUE (valtail);
3247 if (val == error_mark_node)
3248 return error_mark_node;
3250 if (type == void_type_node)
3254 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3256 error ("at this point in file");
3259 error ("too many arguments to function");
3260 /* In case anybody wants to know if this argument
3263 TREE_TYPE (tree_last (result)) = error_mark_node;
3267 if (TREE_CODE (val) == OFFSET_REF)
3268 val = resolve_offset_ref (val);
3270 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3271 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3272 if (TREE_CODE (val) == NOP_EXPR
3273 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3274 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3275 val = TREE_OPERAND (val, 0);
3277 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3279 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3280 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3281 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3282 val = default_conversion (val);
3285 if (val == error_mark_node)
3286 return error_mark_node;
3290 /* Formal parm type is specified by a function prototype. */
3293 if (TYPE_SIZE (complete_type (type)) == 0)
3295 error ("parameter type of called function is incomplete");
3300 parmval = convert_for_initialization
3301 (NULL_TREE, type, val, flags,
3302 "argument passing", fndecl, i);
3303 if (PROMOTE_PROTOTYPES
3304 && (TREE_CODE (type) == INTEGER_TYPE
3305 || TREE_CODE (type) == ENUMERAL_TYPE)
3306 && (TYPE_PRECISION (type)
3307 < TYPE_PRECISION (integer_type_node)))
3308 parmval = default_conversion (parmval);
3311 if (parmval == error_mark_node)
3312 return error_mark_node;
3314 result = expr_tree_cons (NULL_TREE, parmval, result);
3318 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3319 val = convert_from_reference (val);
3321 result = expr_tree_cons (NULL_TREE,
3322 convert_arg_to_ellipsis (val),
3327 typetail = TREE_CHAIN (typetail);
3330 if (typetail != 0 && typetail != void_list_node)
3332 /* See if there are default arguments that can be used */
3333 if (TREE_PURPOSE (typetail))
3335 for (; typetail != void_list_node; ++i)
3338 = convert_default_arg (TREE_VALUE (typetail),
3339 TREE_PURPOSE (typetail),
3342 if (parmval == error_mark_node)
3343 return error_mark_node;
3345 result = expr_tree_cons (0, parmval, result);
3346 typetail = TREE_CHAIN (typetail);
3347 /* ends with `...'. */
3348 if (typetail == NULL_TREE)
3356 cp_error_at ("too few arguments to %s `%+#D'",
3357 called_thing, fndecl);
3358 error ("at this point in file");
3361 error ("too few arguments to function");
3362 return error_mark_list;
3366 return nreverse (result);
3369 /* Build a binary-operation expression, after performing default
3370 conversions on the operands. CODE is the kind of expression to build. */
3373 build_x_binary_op (code, arg1, arg2)
3374 enum tree_code code;
3377 if (processing_template_decl)
3378 return build_min_nt (code, arg1, arg2);
3380 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3384 build_binary_op (code, arg1, arg2)
3385 enum tree_code code;
3388 return build_binary_op_nodefault (code, arg1, arg2, code);
3391 /* Build a binary-operation expression without default conversions.
3392 CODE is the kind of expression to build.
3393 This function differs from `build' in several ways:
3394 the data type of the result is computed and recorded in it,
3395 warnings are generated if arg data types are invalid,
3396 special handling for addition and subtraction of pointers is known,
3397 and some optimization is done (operations on narrow ints
3398 are done in the narrower type when that gives the same result).
3399 Constant folding is also done before the result is returned.
3401 ERROR_CODE is the code that determines what to say in error messages.
3402 It is usually, but not always, the same as CODE.
3404 Note that the operands will never have enumeral types
3405 because either they have just had the default conversions performed
3406 or they have both just been converted to some other type in which
3407 the arithmetic is to be done.
3409 C++: must do special pointer arithmetic when implementing
3410 multiple inheritance, and deal with pointer to member functions. */
3413 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3414 enum tree_code code;
3415 tree orig_op0, orig_op1;
3416 enum tree_code error_code;
3419 register enum tree_code code0, code1;
3422 /* Expression code to give to the expression when it is built.
3423 Normally this is CODE, which is what the caller asked for,
3424 but in some special cases we change it. */
3425 register enum tree_code resultcode = code;
3427 /* Data type in which the computation is to be performed.
3428 In the simplest cases this is the common type of the arguments. */
3429 register tree result_type = NULL;
3431 /* Nonzero means operands have already been type-converted
3432 in whatever way is necessary.
3433 Zero means they need to be converted to RESULT_TYPE. */
3436 /* Nonzero means create the expression with this type, rather than
3438 tree build_type = 0;
3440 /* Nonzero means after finally constructing the expression
3441 convert it to this type. */
3442 tree final_type = 0;
3444 /* Nonzero if this is an operation like MIN or MAX which can
3445 safely be computed in short if both args are promoted shorts.
3446 Also implies COMMON.
3447 -1 indicates a bitwise operation; this makes a difference
3448 in the exact conditions for when it is safe to do the operation
3449 in a narrower mode. */
3452 /* Nonzero if this is a comparison operation;
3453 if both args are promoted shorts, compare the original shorts.
3454 Also implies COMMON. */
3455 int short_compare = 0;
3457 /* Nonzero if this is a right-shift operation, which can be computed on the
3458 original short and then promoted if the operand is a promoted short. */
3459 int short_shift = 0;
3461 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3464 /* Apply default conversions. */
3465 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3466 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3467 || code == TRUTH_XOR_EXPR)
3469 op0 = decay_conversion (orig_op0);
3470 op1 = decay_conversion (orig_op1);
3474 op0 = default_conversion (orig_op0);
3475 op1 = default_conversion (orig_op1);
3478 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3479 STRIP_TYPE_NOPS (op0);
3480 STRIP_TYPE_NOPS (op1);
3482 /* DTRT if one side is an overloaded function, but complain about it. */
3483 if (type_unknown_p (op0))
3485 tree t = instantiate_type (TREE_TYPE (op1), op0, 0);
3486 if (t != error_mark_node)
3488 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3493 if (type_unknown_p (op1))
3495 tree t = instantiate_type (TREE_TYPE (op0), op1, 0);
3496 if (t != error_mark_node)
3498 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3504 type0 = TREE_TYPE (op0);
3505 type1 = TREE_TYPE (op1);
3507 /* The expression codes of the data types of the arguments tell us
3508 whether the arguments are integers, floating, pointers, etc. */
3509 code0 = TREE_CODE (type0);
3510 code1 = TREE_CODE (type1);
3512 /* If an error was already reported for one of the arguments,
3513 avoid reporting another error. */
3515 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3516 return error_mark_node;
3521 /* Handle the pointer + int case. */
3522 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3523 return pointer_int_sum (PLUS_EXPR, op0, op1);
3524 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3525 return pointer_int_sum (PLUS_EXPR, op1, op0);
3531 /* Subtraction of two similar pointers.
3532 We must subtract them as integers, then divide by object size. */
3533 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3534 && comp_target_types (type0, type1, 1))
3535 return pointer_diff (op0, op1, common_type (type0, type1));
3536 /* Handle pointer minus int. Just like pointer plus int. */
3537 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3538 return pointer_int_sum (MINUS_EXPR, op0, op1);
3547 case TRUNC_DIV_EXPR:
3549 case FLOOR_DIV_EXPR:
3550 case ROUND_DIV_EXPR:
3551 case EXACT_DIV_EXPR:
3552 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3553 || code0 == COMPLEX_TYPE)
3554 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3555 || code1 == COMPLEX_TYPE))
3557 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3558 cp_warning ("division by zero in `%E / 0'", op0);
3559 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3560 cp_warning ("division by zero in `%E / 0.'", op0);
3562 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3563 resultcode = RDIV_EXPR;
3565 /* When dividing two signed integers, we have to promote to int.
3566 unless we divide by a constant != -1. Note that default
3567 conversion will have been performed on the operands at this
3568 point, so we have to dig out the original type to find out if
3570 shorten = ((TREE_CODE (op0) == NOP_EXPR
3571 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3572 || (TREE_CODE (op1) == INTEGER_CST
3573 && (TREE_INT_CST_LOW (op1) != -1
3574 || TREE_INT_CST_HIGH (op1) != -1)));
3580 case BIT_ANDTC_EXPR:
3583 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3585 /* If one operand is a constant, and the other is a short type
3586 that has been converted to an int,
3587 really do the work in the short type and then convert the
3588 result to int. If we are lucky, the constant will be 0 or 1
3589 in the short type, making the entire operation go away. */
3590 if (TREE_CODE (op0) == INTEGER_CST
3591 && TREE_CODE (op1) == NOP_EXPR
3592 && (TYPE_PRECISION (type1)
3593 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3594 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3596 final_type = result_type;
3597 op1 = TREE_OPERAND (op1, 0);
3598 result_type = TREE_TYPE (op1);
3600 if (TREE_CODE (op1) == INTEGER_CST
3601 && TREE_CODE (op0) == NOP_EXPR
3602 && (TYPE_PRECISION (type0)
3603 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3604 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3606 final_type = result_type;
3607 op0 = TREE_OPERAND (op0, 0);
3608 result_type = TREE_TYPE (op0);
3612 case TRUNC_MOD_EXPR:
3613 case FLOOR_MOD_EXPR:
3614 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3615 cp_warning ("division by zero in `%E %% 0'", op0);
3616 else if (code1 == REAL_TYPE && real_zerop (op1))
3617 cp_warning ("division by zero in `%E %% 0.'", op0);
3619 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3621 /* Although it would be tempting to shorten always here, that loses
3622 on some targets, since the modulo instruction is undefined if the
3623 quotient can't be represented in the computation mode. We shorten
3624 only if unsigned or if dividing by something we know != -1. */
3625 shorten = ((TREE_CODE (op0) == NOP_EXPR
3626 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3627 || (TREE_CODE (op1) == INTEGER_CST
3628 && (TREE_INT_CST_LOW (op1) != -1
3629 || TREE_INT_CST_HIGH (op1) != -1)));
3634 case TRUTH_ANDIF_EXPR:
3635 case TRUTH_ORIF_EXPR:
3636 case TRUTH_AND_EXPR:
3638 result_type = boolean_type_node;
3641 /* Shift operations: result has same type as first operand;
3642 always convert second operand to int.
3643 Also set SHORT_SHIFT if shifting rightward. */
3646 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3648 result_type = type0;
3649 if (TREE_CODE (op1) == INTEGER_CST)
3651 if (tree_int_cst_lt (op1, integer_zero_node))
3652 warning ("right shift count is negative");
3655 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3657 if (TREE_INT_CST_HIGH (op1) != 0
3658 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3659 >= TYPE_PRECISION (type0)))
3660 warning ("right shift count >= width of type");
3663 /* Convert the shift-count to an integer, regardless of
3664 size of value being shifted. */
3665 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3666 op1 = cp_convert (integer_type_node, op1);
3667 /* Avoid converting op1 to result_type later. */
3673 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3675 result_type = type0;
3676 if (TREE_CODE (op1) == INTEGER_CST)
3678 if (tree_int_cst_lt (op1, integer_zero_node))
3679 warning ("left shift count is negative");
3680 else if (TREE_INT_CST_HIGH (op1) != 0
3681 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3682 >= TYPE_PRECISION (type0)))
3683 warning ("left shift count >= width of type");
3685 /* Convert the shift-count to an integer, regardless of
3686 size of value being shifted. */
3687 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3688 op1 = cp_convert (integer_type_node, op1);
3689 /* Avoid converting op1 to result_type later. */
3696 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3698 result_type = type0;
3699 if (TREE_CODE (op1) == INTEGER_CST)
3701 if (tree_int_cst_lt (op1, integer_zero_node))
3702 warning ("%s rotate count is negative",
3703 (code == LROTATE_EXPR) ? "left" : "right");
3704 else if (TREE_INT_CST_HIGH (op1) != 0
3705 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3706 >= TYPE_PRECISION (type0)))
3707 warning ("%s rotate count >= width of type",
3708 (code == LROTATE_EXPR) ? "left" : "right");
3710 /* Convert the shift-count to an integer, regardless of
3711 size of value being shifted. */
3712 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3713 op1 = cp_convert (integer_type_node, op1);
3719 build_type = boolean_type_node;
3720 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3721 || code0 == COMPLEX_TYPE)
3722 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3723 || code1 == COMPLEX_TYPE))
3725 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3727 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3728 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3730 if (comp_target_types (type0, type1, 1))
3731 result_type = common_type (type0, type1);
3732 else if (tt0 == void_type_node)
3734 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3735 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3736 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3737 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3738 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3740 else if (tt1 == void_type_node)
3742 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3743 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3744 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3747 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3750 if (result_type == NULL_TREE)
3751 result_type = ptr_type_node;
3753 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3754 result_type = type0;
3755 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3756 result_type = type1;
3757 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3759 result_type = type0;
3760 error ("ANSI C++ forbids comparison between pointer and integer");
3762 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3764 result_type = type1;
3765 error ("ANSI C++ forbids comparison between pointer and integer");
3767 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3769 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3770 op1 = integer_zero_node;
3771 result_type = TREE_TYPE (op0);
3773 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3775 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3776 op1 = integer_zero_node;
3777 result_type = TREE_TYPE (op0);
3779 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3780 && same_type_p (type0, type1))
3782 /* The code we generate for the test is:
3784 (op0.index == op1.index
3785 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3786 || op0.pfn == op1.pfn)) */
3788 tree index0 = build_component_ref (op0, index_identifier,
3790 tree index1 = save_expr (build_component_ref (op1, index_identifier,
3792 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3793 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3794 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3795 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3797 tree integer_neg_one_node
3798 = build_binary_op (MINUS_EXPR, integer_zero_node,
3800 e1 = build_binary_op (EQ_EXPR, index0, index1);
3801 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3802 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3803 build_binary_op (EQ_EXPR, delta20, delta21));
3804 /* We can't use build_binary_op for this cmp because it would get
3805 confused by the ptr to method types and think we want pmfs. */
3806 e3 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3807 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3808 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3809 if (code == EQ_EXPR)
3811 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3813 else if (TYPE_PTRMEMFUNC_P (type0)
3814 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3816 tree index0 = build_component_ref (op0, index_identifier,
3819 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3820 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3821 tree delta21 = integer_zero_node;
3823 tree integer_neg_one_node
3824 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node);
3825 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3826 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3828 /* Map everything down one to make room for
3829 the null pointer to member. */
3830 index1 = size_binop (PLUS_EXPR,
3831 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3833 op1 = integer_zero_node;
3834 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3835 (TREE_TYPE (type1)));
3836 delta21 = DECL_FIELD_BITPOS (delta21);
3837 delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3838 size_int (BITS_PER_UNIT));
3839 delta21 = convert (sizetype, delta21);
3842 index1 = integer_neg_one_node;
3844 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3846 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3849 e1 = build_binary_op (EQ_EXPR, index0, index1);
3850 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3851 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3852 build_binary_op (EQ_EXPR, delta20, delta21));
3853 /* We can't use build_binary_op for this cmp because it would get
3854 confused by the ptr to method types and think we want pmfs. */
3855 e3 = build (EQ_EXPR, boolean_type_node, pfn0, op1);
3856 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3857 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3858 if (code == EQ_EXPR)
3860 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3862 else if (TYPE_PTRMEMFUNC_P (type1)
3863 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0))
3864 return build_binary_op (code, op1, op0);
3869 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3870 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3872 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3874 if (comp_target_types (type0, type1, 1))
3875 result_type = common_type (type0, type1);
3878 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3880 result_type = ptr_type_node;
3889 build_type = boolean_type_node;
3890 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3891 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3893 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3895 if (comp_target_types (type0, type1, 1))
3896 result_type = common_type (type0, type1);
3899 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3901 result_type = ptr_type_node;
3904 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3905 && integer_zerop (op1))
3906 result_type = type0;
3907 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3908 && integer_zerop (op0))
3909 result_type = type1;
3910 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3912 result_type = type0;
3913 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3915 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3917 result_type = type1;
3918 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3926 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3928 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3930 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3932 if (shorten || common || short_compare)
3933 result_type = common_type (type0, type1);
3935 /* For certain operations (which identify themselves by shorten != 0)
3936 if both args were extended from the same smaller type,
3937 do the arithmetic in that type and then extend.
3939 shorten !=0 and !=1 indicates a bitwise operation.
3940 For them, this optimization is safe only if
3941 both args are zero-extended or both are sign-extended.
3942 Otherwise, we might change the result.
3943 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3944 but calculated in (unsigned short) it would be (unsigned short)-1. */
3946 if (shorten && none_complex)
3948 int unsigned0, unsigned1;
3949 tree arg0 = get_narrower (op0, &unsigned0);
3950 tree arg1 = get_narrower (op1, &unsigned1);
3951 /* UNS is 1 if the operation to be done is an unsigned one. */
3952 int uns = TREE_UNSIGNED (result_type);
3955 final_type = result_type;
3957 /* Handle the case that OP0 does not *contain* a conversion
3958 but it *requires* conversion to FINAL_TYPE. */
3960 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3961 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3962 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3963 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3965 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3967 /* For bitwise operations, signedness of nominal type
3968 does not matter. Consider only how operands were extended. */
3972 /* Note that in all three cases below we refrain from optimizing
3973 an unsigned operation on sign-extended args.
3974 That would not be valid. */
3976 /* Both args variable: if both extended in same way
3977 from same width, do it in that width.
3978 Do it unsigned if args were zero-extended. */
3979 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3980 < TYPE_PRECISION (result_type))
3981 && (TYPE_PRECISION (TREE_TYPE (arg1))
3982 == TYPE_PRECISION (TREE_TYPE (arg0)))
3983 && unsigned0 == unsigned1
3984 && (unsigned0 || !uns))
3986 = signed_or_unsigned_type (unsigned0,
3987 common_type (TREE_TYPE (arg0),
3989 else if (TREE_CODE (arg0) == INTEGER_CST
3990 && (unsigned1 || !uns)
3991 && (TYPE_PRECISION (TREE_TYPE (arg1))
3992 < TYPE_PRECISION (result_type))
3993 && (type = signed_or_unsigned_type (unsigned1,
3995 int_fits_type_p (arg0, type)))
3997 else if (TREE_CODE (arg1) == INTEGER_CST
3998 && (unsigned0 || !uns)
3999 && (TYPE_PRECISION (TREE_TYPE (arg0))
4000 < TYPE_PRECISION (result_type))
4001 && (type = signed_or_unsigned_type (unsigned0,
4003 int_fits_type_p (arg1, type)))
4007 /* Shifts can be shortened if shifting right. */
4012 tree arg0 = get_narrower (op0, &unsigned_arg);
4014 final_type = result_type;
4016 if (arg0 == op0 && final_type == TREE_TYPE (op0))
4017 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
4019 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
4020 /* We can shorten only if the shift count is less than the
4021 number of bits in the smaller type size. */
4022 && TREE_INT_CST_HIGH (op1) == 0
4023 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
4024 /* If arg is sign-extended and then unsigned-shifted,
4025 we can simulate this with a signed shift in arg's type
4026 only if the extended result is at least twice as wide
4027 as the arg. Otherwise, the shift could use up all the
4028 ones made by sign-extension and bring in zeros.
4029 We can't optimize that case at all, but in most machines
4030 it never happens because available widths are 2**N. */
4031 && (!TREE_UNSIGNED (final_type)
4033 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
4034 <= TYPE_PRECISION (result_type))))
4036 /* Do an unsigned shift if the operand was zero-extended. */
4038 = signed_or_unsigned_type (unsigned_arg,
4040 /* Convert value-to-be-shifted to that type. */
4041 if (TREE_TYPE (op0) != result_type)
4042 op0 = cp_convert (result_type, op0);
4047 /* Comparison operations are shortened too but differently.
4048 They identify themselves by setting short_compare = 1. */
4052 /* Don't write &op0, etc., because that would prevent op0
4053 from being kept in a register.
4054 Instead, make copies of the our local variables and
4055 pass the copies by reference, then copy them back afterward. */
4056 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4057 enum tree_code xresultcode = resultcode;
4059 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4061 return cp_convert (boolean_type_node, val);
4062 op0 = xop0, op1 = xop1;
4064 resultcode = xresultcode;
4067 if (short_compare && warn_sign_compare)
4069 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
4070 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
4072 int unsignedp0, unsignedp1;
4073 tree primop0 = get_narrower (op0, &unsignedp0);
4074 tree primop1 = get_narrower (op1, &unsignedp1);
4076 /* Check for comparison of different enum types. */
4077 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
4078 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
4079 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
4080 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
4082 cp_warning ("comparison between `%#T' and `%#T'",
4083 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
4086 /* Give warnings for comparisons between signed and unsigned
4087 quantities that may fail. */
4088 /* Do the checking based on the original operand trees, so that
4089 casts will be considered, but default promotions won't be. */
4091 /* Do not warn if the comparison is being done in a signed type,
4092 since the signed type will only be chosen if it can represent
4093 all the values of the unsigned type. */
4094 if (! TREE_UNSIGNED (result_type))
4096 /* Do not warn if both operands are unsigned. */
4097 else if (op0_signed == op1_signed)
4099 /* Do not warn if the signed quantity is an unsuffixed
4100 integer literal (or some static constant expression
4101 involving such literals) and it is non-negative. */
4102 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
4103 && tree_int_cst_sgn (orig_op0) >= 0)
4104 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
4105 && tree_int_cst_sgn (orig_op1) >= 0))
4107 /* Do not warn if the comparison is an equality operation,
4108 the unsigned quantity is an integral constant and it does
4109 not use the most significant bit of result_type. */
4110 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
4111 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
4112 && int_fits_type_p (orig_op1,
4113 signed_type (result_type)))
4114 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
4115 && int_fits_type_p (orig_op0,
4116 signed_type (result_type)))))
4119 warning ("comparison between signed and unsigned");
4121 /* Warn if two unsigned values are being compared in a size
4122 larger than their original size, and one (and only one) is the
4123 result of a `~' operator. This comparison will always fail.
4125 Also warn if one operand is a constant, and the constant does not
4126 have all bits set that are set in the ~ operand when it is
4129 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
4130 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
4132 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
4133 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
4134 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
4135 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
4137 if (TREE_CODE (primop0) == INTEGER_CST
4138 || TREE_CODE (primop1) == INTEGER_CST)
4141 HOST_WIDE_INT constant, mask;
4145 if (TREE_CODE (primop0) == INTEGER_CST)
4148 unsignedp = unsignedp1;
4149 constant = TREE_INT_CST_LOW (primop0);
4154 unsignedp = unsignedp0;
4155 constant = TREE_INT_CST_LOW (primop1);
4158 bits = TYPE_PRECISION (TREE_TYPE (primop));
4159 if (bits < TYPE_PRECISION (result_type)
4160 && bits < HOST_BITS_PER_LONG && unsignedp)
4162 mask = (~ (HOST_WIDE_INT) 0) << bits;
4163 if ((mask & constant) != mask)
4164 warning ("comparison of promoted ~unsigned with constant");
4167 else if (unsignedp0 && unsignedp1
4168 && (TYPE_PRECISION (TREE_TYPE (primop0))
4169 < TYPE_PRECISION (result_type))
4170 && (TYPE_PRECISION (TREE_TYPE (primop1))
4171 < TYPE_PRECISION (result_type)))
4172 warning ("comparison of promoted ~unsigned with unsigned");
4177 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4178 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4179 Then the expression will be built.
4180 It will be given type FINAL_TYPE if that is nonzero;
4181 otherwise, it will be given type RESULT_TYPE. */
4185 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
4186 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
4187 return error_mark_node;
4190 /* Issue warnings about peculiar, but legal, uses of NULL. */
4191 if (/* It's reasonable to use pointer values as operands of &&
4192 and ||, so NULL is no exception. */
4193 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4194 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4195 (orig_op0 == null_node
4196 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4197 /* Or vice versa. */
4198 || (orig_op1 == null_node
4199 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4200 /* Or, both are NULL and the operation was not a comparison. */
4201 || (orig_op0 == null_node && orig_op1 == null_node
4202 && code != EQ_EXPR && code != NE_EXPR)))
4203 /* Some sort of arithmetic operation involving NULL was
4204 performed. Note that pointer-difference and pointer-addition
4205 have already been handled above, and so we don't end up here in
4207 cp_warning ("NULL used in arithmetic");
4211 if (TREE_TYPE (op0) != result_type)
4212 op0 = cp_convert (result_type, op0);
4213 if (TREE_TYPE (op1) != result_type)
4214 op1 = cp_convert (result_type, op1);
4216 if (op0 == error_mark_node || op1 == error_mark_node)
4217 return error_mark_node;
4220 if (build_type == NULL_TREE)
4221 build_type = result_type;
4224 register tree result = build (resultcode, build_type, op0, op1);
4225 register tree folded;
4227 folded = fold (result);
4228 if (folded == result)
4229 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4230 if (final_type != 0)
4231 return cp_convert (final_type, folded);
4236 /* Return a tree for the sum or difference (RESULTCODE says which)
4237 of pointer PTROP and integer INTOP. */
4240 pointer_int_sum (resultcode, ptrop, intop)
4241 enum tree_code resultcode;
4242 register tree ptrop, intop;
4246 register tree result;
4247 register tree folded = fold (intop);
4249 /* The result is a pointer of the same type that is being added. */
4251 register tree result_type = TREE_TYPE (ptrop);
4253 if (!complete_type_or_else (result_type, ptrop))
4254 return error_mark_node;
4256 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4258 if (pedantic || warn_pointer_arith)
4259 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
4260 size_exp = integer_one_node;
4262 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4264 if (pedantic || warn_pointer_arith)
4265 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
4266 size_exp = integer_one_node;
4268 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4270 if (pedantic || warn_pointer_arith)
4271 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4272 size_exp = integer_one_node;
4274 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4276 if (pedantic || warn_pointer_arith)
4277 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4278 size_exp = integer_one_node;
4281 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4283 /* Needed to make OOPS V2R3 work. */
4285 if (TREE_CODE (intop) == INTEGER_CST
4286 && TREE_INT_CST_LOW (intop) == 0
4287 && TREE_INT_CST_HIGH (intop) == 0)
4290 /* If what we are about to multiply by the size of the elements
4291 contains a constant term, apply distributive law
4292 and multiply that constant term separately.
4293 This helps produce common subexpressions. */
4295 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4296 && ! TREE_CONSTANT (intop)
4297 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4298 && TREE_CONSTANT (size_exp))
4300 enum tree_code subcode = resultcode;
4301 if (TREE_CODE (intop) == MINUS_EXPR)
4302 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4303 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4304 intop = TREE_OPERAND (intop, 0);
4307 /* Convert the integer argument to a type the same size as sizetype
4308 so the multiply won't overflow spuriously. */
4310 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4311 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4313 /* Replace the integer argument with a suitable product by the object size.
4314 Do this multiplication as signed, then convert to the appropriate
4315 pointer type (actually unsigned integral). */
4317 intop = cp_convert (result_type,
4318 build_binary_op (MULT_EXPR, intop,
4319 cp_convert (TREE_TYPE (intop),
4322 /* Create the sum or difference. */
4324 result = build (resultcode, result_type, ptrop, intop);
4326 folded = fold (result);
4327 if (folded == result)
4328 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4332 /* Return a tree for the difference of pointers OP0 and OP1.
4333 The resulting tree has type int. */
4336 pointer_diff (op0, op1, ptrtype)
4337 register tree op0, op1;
4338 register tree ptrtype;
4340 register tree result, folded;
4341 tree restype = ptrdiff_type_node;
4342 tree target_type = TREE_TYPE (ptrtype);
4344 if (!complete_type_or_else (target_type, NULL_TREE))
4345 return error_mark_node;
4347 if (pedantic || warn_pointer_arith)
4349 if (TREE_CODE (target_type) == VOID_TYPE)
4350 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4351 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4352 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4353 if (TREE_CODE (target_type) == METHOD_TYPE)
4354 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4355 if (TREE_CODE (target_type) == OFFSET_TYPE)
4356 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4359 /* First do the subtraction as integers;
4360 then drop through to build the divide operator. */
4362 op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4363 cp_convert (restype, op1));
4365 /* This generates an error if op1 is a pointer to an incomplete type. */
4366 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4367 error ("arithmetic on pointer to an incomplete type");
4369 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4370 || TREE_CODE (target_type) == FUNCTION_TYPE
4371 || TREE_CODE (target_type) == METHOD_TYPE
4372 || TREE_CODE (target_type) == OFFSET_TYPE)
4374 : size_in_bytes (target_type));
4376 /* Do the division. */
4378 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4380 folded = fold (result);
4381 if (folded == result)
4382 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4386 /* Handle the case of taking the address of a COMPONENT_REF.
4387 Called by `build_unary_op'.
4389 ARG is the COMPONENT_REF whose address we want.
4390 ARGTYPE is the pointer type that this address should have. */
4393 build_component_addr (arg, argtype)
4396 tree field = TREE_OPERAND (arg, 1);
4397 tree basetype = decl_type_context (field);
4398 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4400 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4402 if (DECL_C_BIT_FIELD (field))
4404 cp_error ("attempt to take address of bit-field structure member `%D'",
4406 return error_mark_node;
4409 if (TREE_CODE (field) == FIELD_DECL
4410 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4412 /* Can't convert directly to ARGTYPE, since that
4413 may have the same pointer type as one of our
4415 rval = build1 (NOP_EXPR, argtype,
4416 convert_pointer_to (basetype, rval));
4417 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4420 /* This conversion is harmless. */
4421 rval = convert_force (argtype, rval, 0);
4423 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4425 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4426 size_int (BITS_PER_UNIT));
4427 int flag = TREE_CONSTANT (rval);
4428 offset = convert (sizetype, offset);
4429 rval = fold (build (PLUS_EXPR, argtype,
4430 rval, cp_convert (argtype, offset)));
4431 TREE_CONSTANT (rval) = flag;
4436 /* Construct and perhaps optimize a tree representation
4437 for a unary operation. CODE, a tree_code, specifies the operation
4438 and XARG is the operand. */
4441 build_x_unary_op (code, xarg)
4442 enum tree_code code;
4445 if (processing_template_decl)
4446 return build_min_nt (code, xarg, NULL_TREE);
4448 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4450 if (code == ADDR_EXPR
4451 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4452 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4453 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4454 || (TREE_CODE (xarg) == OFFSET_REF)))
4455 /* don't look for a function */;
4460 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4461 NULL_TREE, NULL_TREE);
4462 if (rval || code != ADDR_EXPR)
4466 if (code == ADDR_EXPR)
4468 if (TREE_CODE (xarg) == TARGET_EXPR)
4469 warning ("taking address of temporary");
4472 return build_unary_op (code, xarg, 0);
4475 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4478 condition_conversion (expr)
4482 if (processing_template_decl)
4484 t = cp_convert (boolean_type_node, expr);
4485 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4489 /* C++: Must handle pointers to members.
4491 Perhaps type instantiation should be extended to handle conversion
4492 from aggregates to types we don't yet know we want? (Or are those
4493 cases typically errors which should be reported?)
4495 NOCONVERT nonzero suppresses the default promotions
4496 (such as from short to int). */
4499 build_unary_op (code, xarg, noconvert)
4500 enum tree_code code;
4504 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4505 register tree arg = xarg;
4506 register tree argtype = 0;
4507 const char *errstring = NULL;
4510 if (arg == error_mark_node)
4511 return error_mark_node;
4516 /* This is used for unary plus, because a CONVERT_EXPR
4517 is enough to prevent anybody from looking inside for
4518 associativity, but won't generate any code. */
4519 if (!(arg = build_expr_type_conversion
4520 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4521 errstring = "wrong type argument to unary plus";
4525 arg = default_conversion (arg);
4526 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4527 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4532 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4533 errstring = "wrong type argument to unary minus";
4534 else if (!noconvert)
4535 arg = default_conversion (arg);
4539 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4543 arg = default_conversion (arg);
4545 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4547 errstring = "wrong type argument to bit-complement";
4548 else if (!noconvert)
4549 arg = default_conversion (arg);
4553 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4554 errstring = "wrong type argument to abs";
4555 else if (!noconvert)
4556 arg = default_conversion (arg);
4560 /* Conjugating a real value is a no-op, but allow it anyway. */
4561 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4562 errstring = "wrong type argument to conjugation";
4563 else if (!noconvert)
4564 arg = default_conversion (arg);
4567 case TRUTH_NOT_EXPR:
4568 arg = cp_convert (boolean_type_node, arg);
4569 val = invert_truthvalue (arg);
4570 if (arg != error_mark_node)
4572 errstring = "in argument to unary !";
4579 if (TREE_CODE (arg) == COMPLEX_CST)
4580 return TREE_REALPART (arg);
4581 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4582 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4587 if (TREE_CODE (arg) == COMPLEX_CST)
4588 return TREE_IMAGPART (arg);
4589 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4590 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4592 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4594 case PREINCREMENT_EXPR:
4595 case POSTINCREMENT_EXPR:
4596 case PREDECREMENT_EXPR:
4597 case POSTDECREMENT_EXPR:
4598 /* Handle complex lvalues (when permitted)
4599 by reduction to simpler cases. */
4601 val = unary_complex_lvalue (code, arg);
4605 /* Increment or decrement the real part of the value,
4606 and don't change the imaginary part. */
4607 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4611 arg = stabilize_reference (arg);
4612 real = build_unary_op (REALPART_EXPR, arg, 1);
4613 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4614 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4615 build_unary_op (code, real, 1), imag);
4618 /* Report invalid types. */
4620 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4623 if (code == PREINCREMENT_EXPR)
4624 errstring ="no pre-increment operator for type";
4625 else if (code == POSTINCREMENT_EXPR)
4626 errstring ="no post-increment operator for type";
4627 else if (code == PREDECREMENT_EXPR)
4628 errstring ="no pre-decrement operator for type";
4630 errstring ="no post-decrement operator for type";
4634 /* Report something read-only. */
4636 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4637 || TREE_READONLY (arg))
4638 readonly_error (arg, ((code == PREINCREMENT_EXPR
4639 || code == POSTINCREMENT_EXPR)
4640 ? "increment" : "decrement"),
4645 tree result_type = TREE_TYPE (arg);
4647 arg = get_unwidened (arg, 0);
4648 argtype = TREE_TYPE (arg);
4650 /* ARM $5.2.5 last annotation says this should be forbidden. */
4651 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4652 pedwarn ("ANSI C++ forbids %sing an enum",
4653 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4654 ? "increment" : "decrement");
4656 /* Compute the increment. */
4658 if (TREE_CODE (argtype) == POINTER_TYPE)
4660 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4661 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4662 cp_error ("cannot %s a pointer to incomplete type `%T'",
4663 ((code == PREINCREMENT_EXPR
4664 || code == POSTINCREMENT_EXPR)
4665 ? "increment" : "decrement"), TREE_TYPE (argtype));
4666 else if ((pedantic || warn_pointer_arith)
4667 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4668 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4669 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4670 ((code == PREINCREMENT_EXPR
4671 || code == POSTINCREMENT_EXPR)
4672 ? "increment" : "decrement"), argtype);
4673 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4676 inc = integer_one_node;
4678 inc = cp_convert (argtype, inc);
4680 /* Handle incrementing a cast-expression. */
4682 switch (TREE_CODE (arg))
4687 case FIX_TRUNC_EXPR:
4688 case FIX_FLOOR_EXPR:
4689 case FIX_ROUND_EXPR:
4692 tree incremented, modify, value, compound;
4693 if (! lvalue_p (arg) && pedantic)
4694 pedwarn ("cast to non-reference type used as lvalue");
4695 arg = stabilize_reference (arg);
4696 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4699 value = save_expr (arg);
4700 incremented = build (((code == PREINCREMENT_EXPR
4701 || code == POSTINCREMENT_EXPR)
4702 ? PLUS_EXPR : MINUS_EXPR),
4703 argtype, value, inc);
4704 TREE_SIDE_EFFECTS (incremented) = 1;
4706 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4707 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4709 /* Eliminate warning about unused result of + or -. */
4710 TREE_NO_UNUSED_WARNING (compound) = 1;
4718 /* Complain about anything else that is not a true lvalue. */
4719 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4720 || code == POSTINCREMENT_EXPR)
4721 ? "increment" : "decrement")))
4722 return error_mark_node;
4724 /* Forbid using -- on `bool'. */
4725 if (TREE_TYPE (arg) == boolean_type_node)
4727 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4729 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4730 return error_mark_node;
4733 /* This will only work if someone can convince Kenner to accept
4734 my patch to expand_increment. (jason) */
4735 val = build (code, TREE_TYPE (arg), arg, inc);
4737 if (code == POSTINCREMENT_EXPR)
4739 arg = stabilize_reference (arg);
4740 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4742 TREE_SIDE_EFFECTS (val) = 1;
4743 arg = save_expr (arg);
4744 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4745 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4748 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4753 val = build (code, TREE_TYPE (arg), arg, inc);
4755 TREE_SIDE_EFFECTS (val) = 1;
4756 return cp_convert (result_type, val);
4760 /* Note that this operation never does default_conversion
4761 regardless of NOCONVERT. */
4763 argtype = lvalue_type (arg);
4764 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4768 build_pointer_type (TREE_TYPE (argtype)), arg);
4769 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4772 else if (pedantic && DECL_MAIN_P (arg))
4774 pedwarn ("taking address of function `main'");
4776 /* Let &* cancel out to simplify resulting code. */
4777 if (TREE_CODE (arg) == INDIRECT_REF)
4779 /* We don't need to have `current_class_ptr' wrapped in a
4780 NON_LVALUE_EXPR node. */
4781 if (arg == current_class_ref)
4782 return current_class_ptr;
4784 arg = TREE_OPERAND (arg, 0);
4785 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4789 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4790 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4792 else if (lvalue_p (arg))
4793 /* Don't let this be an lvalue. */
4794 return non_lvalue (arg);
4798 /* For &x[y], return x+y */
4799 if (TREE_CODE (arg) == ARRAY_REF)
4801 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4802 return error_mark_node;
4803 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4804 TREE_OPERAND (arg, 1));
4807 /* Uninstantiated types are all functions. Taking the
4808 address of a function is a no-op, so just return the
4811 if (TREE_CODE (arg) == IDENTIFIER_NODE
4812 && IDENTIFIER_OPNAME_P (arg))
4814 my_friendly_abort (117);
4815 /* We don't know the type yet, so just work around the problem.
4816 We know that this will resolve to an lvalue. */
4817 return build1 (ADDR_EXPR, unknown_type_node, arg);
4820 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4821 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4823 /* They're trying to take the address of a unique non-static
4824 member function. This is ill-formed, but let's try to DTRT. */
4827 if (current_class_type
4828 && TREE_OPERAND (arg, 0) == current_class_ref)
4829 /* An expression like &memfn. */
4830 pedwarn ("taking the address of a non-static member function");
4832 pedwarn ("taking the address of a bound member function");
4834 base = TREE_TYPE (TREE_OPERAND (arg, 0));
4835 name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4837 cp_pedwarn (" to form a pointer to member function, say `&%T::%D'",
4839 arg = build_offset_ref (base, name);
4842 if (type_unknown_p (arg))
4843 return build1 (ADDR_EXPR, unknown_type_node, arg);
4845 /* Handle complex lvalues (when permitted)
4846 by reduction to simpler cases. */
4847 val = unary_complex_lvalue (code, arg);
4851 switch (TREE_CODE (arg))
4856 case FIX_TRUNC_EXPR:
4857 case FIX_FLOOR_EXPR:
4858 case FIX_ROUND_EXPR:
4860 if (! lvalue_p (arg) && pedantic)
4861 pedwarn ("taking the address of a cast to non-reference type");
4868 /* Allow the address of a constructor if all the elements
4870 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4871 && TREE_CONSTANT (arg))
4873 /* Anything not already handled and not a true memory reference
4875 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4876 && TREE_CODE (argtype) != METHOD_TYPE
4877 && !lvalue_or_else (arg, "unary `&'"))
4878 return error_mark_node;
4880 if (argtype != error_mark_node)
4881 argtype = build_pointer_type (argtype);
4883 if (mark_addressable (arg) == 0)
4884 return error_mark_node;
4889 if (TREE_CODE (arg) == COMPONENT_REF)
4890 addr = build_component_addr (arg, argtype);
4892 addr = build1 (ADDR_EXPR, argtype, arg);
4894 /* Address of a static or external variable or
4895 function counts as a constant */
4897 TREE_CONSTANT (addr) = 1;
4899 if (TREE_CODE (argtype) == POINTER_TYPE
4900 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4902 build_ptrmemfunc_type (argtype);
4903 addr = build_ptrmemfunc (argtype, addr, 0);
4916 argtype = TREE_TYPE (arg);
4917 return fold (build1 (code, argtype, arg));
4921 return error_mark_node;
4925 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4926 convert ARG with the same conversions in the same order
4927 and return the result. */
4930 convert_sequence (conversions, arg)
4934 switch (TREE_CODE (conversions))
4939 case FIX_TRUNC_EXPR:
4940 case FIX_FLOOR_EXPR:
4941 case FIX_ROUND_EXPR:
4943 return cp_convert (TREE_TYPE (conversions),
4944 convert_sequence (TREE_OPERAND (conversions, 0),
4953 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4954 for certain kinds of expressions which are not really lvalues
4955 but which we can accept as lvalues.
4957 If ARG is not a kind of expression we can handle, return zero. */
4960 unary_complex_lvalue (code, arg)
4961 enum tree_code code;
4964 /* Handle (a, b) used as an "lvalue". */
4965 if (TREE_CODE (arg) == COMPOUND_EXPR)
4967 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4968 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4969 TREE_OPERAND (arg, 0), real_result);
4972 /* Handle (a ? b : c) used as an "lvalue". */
4973 if (TREE_CODE (arg) == COND_EXPR
4974 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4975 return rationalize_conditional_expr (code, arg);
4977 if (TREE_CODE (arg) == MODIFY_EXPR
4978 || TREE_CODE (arg) == PREINCREMENT_EXPR
4979 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4980 return unary_complex_lvalue
4981 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4982 arg, TREE_OPERAND (arg, 0)));
4984 if (code != ADDR_EXPR)
4987 /* Handle (a = b) used as an "lvalue" for `&'. */
4988 if (TREE_CODE (arg) == MODIFY_EXPR
4989 || TREE_CODE (arg) == INIT_EXPR)
4991 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4992 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4993 TREE_NO_UNUSED_WARNING (arg) = 1;
4997 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4998 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4999 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
5001 /* The representation of something of type OFFSET_TYPE
5002 is really the representation of a pointer to it.
5003 Here give the representation its true type. */
5006 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
5008 if (TREE_CODE (arg) != OFFSET_REF)
5011 t = TREE_OPERAND (arg, 1);
5013 /* Check all this code for right semantics. */
5014 if (TREE_CODE (t) == FUNCTION_DECL)
5016 if (DECL_DESTRUCTOR_P (t))
5017 cp_error ("taking address of destructor");
5018 return build_unary_op (ADDR_EXPR, t, 0);
5020 if (TREE_CODE (t) == VAR_DECL)
5021 return build_unary_op (ADDR_EXPR, t, 0);
5026 if (TREE_OPERAND (arg, 0)
5027 && ! is_dummy_object (TREE_OPERAND (arg, 0))
5028 && TREE_CODE (t) != FIELD_DECL)
5030 cp_error ("taking address of bound pointer-to-member expression");
5031 return error_mark_node;
5034 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
5035 type = build_pointer_type (type);
5037 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5043 /* We permit compiler to make function calls returning
5044 objects of aggregate type look like lvalues. */
5048 if (TREE_CODE (targ) == SAVE_EXPR)
5049 targ = TREE_OPERAND (targ, 0);
5051 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
5053 if (TREE_CODE (arg) == SAVE_EXPR)
5056 targ = build_cplus_new (TREE_TYPE (arg), arg);
5057 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5060 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5061 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5062 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5065 /* Don't let anything else be handled specially. */
5069 /* Mark EXP saying that we need to be able to take the
5070 address of it; it should not be allocated in a register.
5071 Value is 1 if successful.
5073 C++: we do not allow `current_class_ptr' to be addressable. */
5076 mark_addressable (exp)
5079 register tree x = exp;
5081 if (TREE_ADDRESSABLE (x) == 1)
5085 switch (TREE_CODE (x))
5092 x = TREE_OPERAND (x, 0);
5096 if (x == current_class_ptr)
5098 if (! flag_this_is_variable)
5099 error ("address of `this' not available");
5100 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
5101 put_var_into_stack (x);
5105 if (TREE_STATIC (x) && TREE_READONLY (x)
5106 && DECL_RTL (x) != 0
5107 && ! DECL_IN_MEMORY_P (x))
5109 /* We thought this would make a good constant variable,
5110 but we were wrong. */
5111 push_obstacks_nochange ();
5112 end_temporary_allocation ();
5114 TREE_ASM_WRITTEN (x) = 0;
5116 rest_of_decl_compilation (x, 0,
5117 !DECL_FUNCTION_SCOPE_P (x),
5119 TREE_ADDRESSABLE (x) = 1;
5125 /* Caller should not be trying to mark initialized
5126 constant fields addressable. */
5127 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
5128 || DECL_IN_AGGR_P (x) == 0
5130 || DECL_EXTERNAL (x), 314);
5134 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5135 && !DECL_ARTIFICIAL (x) && extra_warnings)
5136 cp_warning ("address requested for `%D', which is declared `register'",
5138 put_var_into_stack (x);
5139 TREE_ADDRESSABLE (x) = 1;
5143 if (DECL_LANG_SPECIFIC (x) != 0)
5145 x = DECL_MAIN_VARIANT (x);
5146 /* We have to test both conditions here. The first may be
5147 non-zero in the case of processing a default function. The
5148 second may be non-zero in the case of a template function. */
5149 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
5152 TREE_ADDRESSABLE (x) = 1;
5154 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
5158 TREE_ADDRESSABLE (x) = 1;
5162 TREE_ADDRESSABLE (x) = 1;
5163 mark_addressable (TREE_OPERAND (x, 0));
5171 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5174 build_x_conditional_expr (ifexp, op1, op2)
5175 tree ifexp, op1, op2;
5177 if (processing_template_decl)
5178 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5180 return build_conditional_expr (ifexp, op1, op2);
5183 /* Handle overloading of the ',' operator when needed. Otherwise,
5184 this function just builds an expression list. */
5187 build_x_compound_expr (list)
5190 tree rest = TREE_CHAIN (list);
5193 if (processing_template_decl)
5194 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5196 if (rest == NULL_TREE)
5197 return build_compound_expr (list);
5199 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5200 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5202 return build_x_compound_expr (expr_tree_cons (NULL_TREE, result,
5203 TREE_CHAIN (rest)));
5205 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5207 /* the left-hand operand of a comma expression is like an expression
5208 statement: we should warn if it doesn't have any side-effects,
5209 unless it was explicitly cast to (void). */
5210 if ((extra_warnings || warn_unused)
5211 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5212 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5213 warning("left-hand operand of comma expression has no effect");
5215 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5216 else if (warn_unused)
5217 warn_if_unused_value (TREE_VALUE(list));
5220 return build_compound_expr
5221 (expr_tree_cons (NULL_TREE, TREE_VALUE (list),
5222 build_expr_list (NULL_TREE,
5223 build_x_compound_expr (rest))));
5226 /* Given a list of expressions, return a compound expression
5227 that performs them all and returns the value of the last of them. */
5230 build_compound_expr (list)
5236 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5237 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5239 if (TREE_CHAIN (list) == 0)
5241 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5242 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5243 if (TREE_CODE (list) == NOP_EXPR
5244 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5245 list = TREE_OPERAND (list, 0);
5247 /* Convert arrays to pointers. */
5248 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5249 return default_conversion (TREE_VALUE (list));
5251 return TREE_VALUE (list);
5254 first = TREE_VALUE (list);
5255 first = require_complete_type_in_void (first);
5256 if (first == error_mark_node)
5257 return error_mark_node;
5259 rest = build_compound_expr (TREE_CHAIN (list));
5260 if (rest == error_mark_node)
5261 return error_mark_node;
5263 /* When pedantic, a compound expression cannot be a constant expression. */
5264 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5267 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5268 break_out_cleanups (first), rest);
5272 build_static_cast (type, expr)
5278 if (type == error_mark_node || expr == error_mark_node)
5279 return error_mark_node;
5281 if (TREE_CODE (expr) == OFFSET_REF)
5282 expr = resolve_offset_ref (expr);
5284 if (processing_template_decl)
5286 tree t = build_min (STATIC_CAST_EXPR, copy_to_permanent (type),
5291 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5292 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5293 if (TREE_CODE (type) != REFERENCE_TYPE
5294 && TREE_CODE (expr) == NOP_EXPR
5295 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5296 expr = TREE_OPERAND (expr, 0);
5298 if (TREE_CODE (type) == VOID_TYPE)
5299 return build1 (CONVERT_EXPR, type, expr);
5301 if (TREE_CODE (type) == REFERENCE_TYPE)
5302 return (convert_from_reference
5303 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5304 LOOKUP_COMPLAIN, NULL_TREE)));
5306 if (IS_AGGR_TYPE (type))
5307 return build_cplus_new
5308 (type, (build_method_call
5309 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5310 TYPE_BINFO (type), LOOKUP_NORMAL)));
5312 expr = decay_conversion (expr);
5313 intype = TREE_TYPE (expr);
5315 /* FIXME handle casting to array type. */
5318 if (can_convert_arg (type, intype, expr))
5320 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5323 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5324 && at_least_as_qualified_p (TREE_TYPE (type),
5326 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5327 && ! TREE_VIA_VIRTUAL (binfo))
5330 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5332 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5333 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))))
5334 && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type)),
5335 TREE_TYPE (TREE_TYPE (intype)))
5336 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5337 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5338 && ! TREE_VIA_VIRTUAL (binfo))
5341 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5342 && TREE_CODE (type) != ARRAY_TYPE
5343 && TREE_CODE (type) != FUNCTION_TYPE
5344 && can_convert (intype, type))
5347 /* [expr.static.cast]
5349 The static_cast operator shall not be used to cast away
5351 if (ok && casts_away_constness (intype, type))
5353 cp_error ("static_cast from `%T' to `%T' casts away constness",
5355 return error_mark_node;
5359 return build_c_cast (type, expr);
5361 cp_error ("static_cast from `%T' to `%T'", intype, type);
5362 return error_mark_node;
5366 build_reinterpret_cast (type, expr)
5371 if (type == error_mark_node || expr == error_mark_node)
5372 return error_mark_node;
5374 if (TREE_CODE (expr) == OFFSET_REF)
5375 expr = resolve_offset_ref (expr);
5377 if (processing_template_decl)
5379 tree t = build_min (REINTERPRET_CAST_EXPR,
5380 copy_to_permanent (type), expr);
5384 if (TREE_CODE (type) != REFERENCE_TYPE)
5386 expr = decay_conversion (expr);
5388 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5389 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5390 if (TREE_CODE (expr) == NOP_EXPR
5391 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5392 expr = TREE_OPERAND (expr, 0);
5395 intype = TREE_TYPE (expr);
5397 if (TREE_CODE (type) == REFERENCE_TYPE)
5399 if (! real_lvalue_p (expr))
5401 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5402 return error_mark_node;
5404 expr = build_unary_op (ADDR_EXPR, expr, 0);
5405 if (expr != error_mark_node)
5406 expr = build_reinterpret_cast
5407 (build_pointer_type (TREE_TYPE (type)), expr);
5408 if (expr != error_mark_node)
5409 expr = build_indirect_ref (expr, 0);
5412 else if (same_type_p (TYPE_MAIN_VARIANT (intype),
5413 TYPE_MAIN_VARIANT (type)))
5414 return build_static_cast (type, expr);
5416 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5417 || TREE_CODE (intype) == ENUMERAL_TYPE))
5419 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5421 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5422 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5425 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5426 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5428 if (TREE_READONLY_DECL_P (expr))
5429 expr = decl_constant_value (expr);
5430 return fold (build1 (NOP_EXPR, type, expr));
5432 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5433 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5435 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5436 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5439 if (TREE_READONLY_DECL_P (expr))
5440 expr = decl_constant_value (expr);
5441 return fold (build1 (NOP_EXPR, type, expr));
5443 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5444 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5446 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5447 if (TREE_READONLY_DECL_P (expr))
5448 expr = decl_constant_value (expr);
5449 return fold (build1 (NOP_EXPR, type, expr));
5453 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5454 return error_mark_node;
5457 return cp_convert (type, expr);
5461 build_const_cast (type, expr)
5466 if (type == error_mark_node || expr == error_mark_node)
5467 return error_mark_node;
5469 if (TREE_CODE (expr) == OFFSET_REF)
5470 expr = resolve_offset_ref (expr);
5472 if (processing_template_decl)
5474 tree t = build_min (CONST_CAST_EXPR, copy_to_permanent (type),
5479 if (!POINTER_TYPE_P (type))
5481 cp_error ("`%T' is not a pointer, reference, or pointer-to-data-member type",
5483 cp_error ("as required by const_cast");
5485 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5487 cp_error ("`%T' is a pointer or reference to a function type",
5489 cp_error ("which is forbidden by const_cast");
5490 return error_mark_node;
5493 if (TREE_CODE (type) != REFERENCE_TYPE)
5495 expr = decay_conversion (expr);
5497 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5498 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5499 if (TREE_CODE (expr) == NOP_EXPR
5500 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5501 expr = TREE_OPERAND (expr, 0);
5504 intype = TREE_TYPE (expr);
5506 if (same_type_p (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type)))
5507 return build_static_cast (type, expr);
5508 else if (TREE_CODE (type) == REFERENCE_TYPE)
5510 if (! real_lvalue_p (expr))
5512 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5513 return error_mark_node;
5516 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5518 expr = build_unary_op (ADDR_EXPR, expr, 0);
5519 expr = build1 (NOP_EXPR, type, expr);
5520 return convert_from_reference (expr);
5523 else if (TREE_CODE (type) == POINTER_TYPE
5524 && TREE_CODE (intype) == POINTER_TYPE
5525 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5526 return cp_convert (type, expr);
5528 cp_error ("const_cast from `%T' to `%T'", intype, type);
5529 return error_mark_node;
5532 /* Build an expression representing a cast to type TYPE of expression EXPR.
5534 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5535 when doing the cast. */
5538 build_c_cast (type, expr)
5541 register tree value = expr;
5544 if (type == error_mark_node || expr == error_mark_node)
5545 return error_mark_node;
5547 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5548 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5549 if (TREE_CODE (type) != REFERENCE_TYPE
5550 && TREE_CODE (value) == NOP_EXPR
5551 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5552 value = TREE_OPERAND (value, 0);
5554 if (TREE_CODE (value) == OFFSET_REF)
5555 value = resolve_offset_ref (value);
5557 if (TREE_CODE (type) == ARRAY_TYPE)
5559 /* Allow casting from T1* to T2[] because Cfront allows it.
5560 NIHCL uses it. It is not valid ANSI C however, and hence, not
5562 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5565 pedwarn ("ANSI C++ forbids casting to an array type");
5566 type = build_pointer_type (TREE_TYPE (type));
5570 error ("ANSI C++ forbids casting to an array type");
5571 return error_mark_node;
5575 if (TREE_CODE (type) == FUNCTION_TYPE
5576 || TREE_CODE (type) == METHOD_TYPE)
5578 cp_error ("casting to function type `%T'", type);
5579 return error_mark_node;
5582 if (IS_SIGNATURE (type))
5584 error ("cast specifies signature type");
5585 return error_mark_node;
5588 if (processing_template_decl)
5590 tree t = build_min (CAST_EXPR, type,
5591 min_tree_cons (NULL_TREE, value, NULL_TREE));
5595 /* Convert functions and arrays to pointers and
5596 convert references to their expanded types,
5597 but don't convert any other types. If, however, we are
5598 casting to a class type, there's no reason to do this: the
5599 cast will only succeed if there is a converting constructor,
5600 and the default conversions will be done at that point. In
5601 fact, doing the default conversion here is actually harmful
5605 struct S { S(const A&); };
5607 since we don't want the array-to-pointer conversion done. */
5608 if (!IS_AGGR_TYPE (type))
5610 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5611 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5612 /* Don't do the default conversion on a ->* expression. */
5613 && ! (TREE_CODE (type) == POINTER_TYPE
5614 && bound_pmf_p (value)))
5615 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5616 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5617 value = default_conversion (value);
5619 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5620 /* However, even for class types, we still need to strip away
5621 the reference type, since the call to convert_force below
5622 does not expect the input expression to be of reference
5624 value = convert_from_reference (value);
5626 otype = TREE_TYPE (value);
5628 /* Optionally warn about potentially worrisome casts. */
5631 && TREE_CODE (type) == POINTER_TYPE
5632 && TREE_CODE (otype) == POINTER_TYPE
5633 && !at_least_as_qualified_p (TREE_TYPE (type),
5635 cp_warning ("cast discards qualifiers from pointer target type");
5637 /* Warn about possible alignment problems. */
5638 if (STRICT_ALIGNMENT && warn_cast_align
5639 && TREE_CODE (type) == POINTER_TYPE
5640 && TREE_CODE (otype) == POINTER_TYPE
5641 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5642 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5643 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5644 warning ("cast increases required alignment of target type");
5647 /* We should see about re-enabling these, they seem useful to
5649 if (TREE_CODE (type) == INTEGER_TYPE
5650 && TREE_CODE (otype) == POINTER_TYPE
5651 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5652 warning ("cast from pointer to integer of different size");
5654 if (TREE_CODE (type) == POINTER_TYPE
5655 && TREE_CODE (otype) == INTEGER_TYPE
5656 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5657 /* Don't warn about converting 0 to pointer,
5658 provided the 0 was explicit--not cast or made by folding. */
5659 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5660 warning ("cast to pointer from integer of different size");
5663 if (TREE_CODE (type) == VOID_TYPE)
5665 value = require_complete_type_in_void (value);
5666 if (value != error_mark_node)
5667 value = build1 (CONVERT_EXPR, void_type_node, value);
5669 else if (TREE_CODE (type) == REFERENCE_TYPE)
5670 value = (convert_from_reference
5671 (convert_to_reference (type, value, CONV_C_CAST,
5672 LOOKUP_COMPLAIN, NULL_TREE)));
5677 if (TREE_READONLY_DECL_P (value))
5678 value = decl_constant_value (value);
5681 value = convert_force (type, value, CONV_C_CAST);
5683 /* Ignore any integer overflow caused by the cast. */
5684 if (TREE_CODE (value) == INTEGER_CST)
5686 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5687 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5691 /* Always produce some operator for an explicit cast,
5692 so we can tell (for -pedantic) that the cast is no lvalue. */
5693 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5694 && real_lvalue_p (value))
5695 value = non_lvalue (value);
5700 /* Build an assignment expression of lvalue LHS from value RHS.
5701 MODIFYCODE is the code for a binary operator that we use
5702 to combine the old value of LHS with RHS to get the new value.
5703 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5705 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5708 build_modify_expr (lhs, modifycode, rhs)
5710 enum tree_code modifycode;
5713 register tree result;
5715 tree lhstype = TREE_TYPE (lhs);
5716 tree olhstype = lhstype;
5719 /* Avoid duplicate error messages from operands that had errors. */
5720 if (lhs == error_mark_node || rhs == error_mark_node)
5721 return error_mark_node;
5723 /* Types that aren't fully specified cannot be used in assignments. */
5724 lhs = require_complete_type (lhs);
5728 /* Handle assignment to signature pointers/refs. */
5730 if (TYPE_LANG_SPECIFIC (lhstype)
5731 && (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5733 return build_signature_pointer_constructor (lhs, rhs);
5736 /* Handle control structure constructs used as "lvalues". */
5738 switch (TREE_CODE (lhs))
5740 /* Handle --foo = 5; as these are valid constructs in C++ */
5741 case PREDECREMENT_EXPR:
5742 case PREINCREMENT_EXPR:
5743 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5744 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5745 stabilize_reference (TREE_OPERAND (lhs, 0)),
5746 TREE_OPERAND (lhs, 1));
5747 return build (COMPOUND_EXPR, lhstype,
5749 build_modify_expr (TREE_OPERAND (lhs, 0),
5752 /* Handle (a, b) used as an "lvalue". */
5754 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5756 if (newrhs == error_mark_node)
5757 return error_mark_node;
5758 return build (COMPOUND_EXPR, lhstype,
5759 TREE_OPERAND (lhs, 0), newrhs);
5762 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5763 if (newrhs == error_mark_node)
5764 return error_mark_node;
5765 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5767 /* Handle (a ? b : c) used as an "lvalue". */
5769 rhs = save_expr (rhs);
5771 /* Produce (a ? (b = rhs) : (c = rhs))
5772 except that the RHS goes through a save-expr
5773 so the code to compute it is only emitted once. */
5775 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5776 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5778 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5780 if (cond == error_mark_node)
5782 /* Make sure the code to compute the rhs comes out
5783 before the split. */
5784 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5785 /* Case to void to suppress warning
5786 from warn_if_unused_value. */
5787 cp_convert (void_type_node, rhs), cond);
5794 if (TREE_CODE (lhs) == OFFSET_REF)
5796 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5798 /* Static class member? */
5799 tree member = TREE_OPERAND (lhs, 1);
5800 if (TREE_CODE (member) == VAR_DECL)
5804 compiler_error ("invalid static class member");
5805 return error_mark_node;
5809 lhs = resolve_offset_ref (lhs);
5811 olhstype = lhstype = TREE_TYPE (lhs);
5814 if (lhs == error_mark_node)
5817 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5818 && modifycode != INIT_EXPR)
5820 lhs = convert_from_reference (lhs);
5821 olhstype = lhstype = TREE_TYPE (lhs);
5824 /* If a binary op has been requested, combine the old LHS value with the RHS
5825 producing the value we should actually store into the LHS. */
5827 if (modifycode == INIT_EXPR)
5829 if (! IS_AGGR_TYPE (lhstype))
5830 /* Do the default thing */;
5833 result = build_method_call (lhs, ctor_identifier,
5834 build_expr_list (NULL_TREE, rhs),
5835 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5836 if (result == NULL_TREE)
5837 return error_mark_node;
5841 else if (modifycode == NOP_EXPR)
5843 /* `operator=' is not an inheritable operator. */
5844 if (! IS_AGGR_TYPE (lhstype))
5845 /* Do the default thing */;
5848 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5849 lhs, rhs, make_node (NOP_EXPR));
5850 if (result == NULL_TREE)
5851 return error_mark_node;
5856 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5858 my_friendly_abort (978652);
5862 lhs = stabilize_reference (lhs);
5863 newrhs = build_binary_op (modifycode, lhs, rhs);
5864 if (newrhs == error_mark_node)
5866 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5867 TREE_TYPE (lhs), TREE_TYPE (rhs));
5868 return error_mark_node;
5872 /* Handle a cast used as an "lvalue".
5873 We have already performed any binary operator using the value as cast.
5874 Now convert the result to the cast type of the lhs,
5875 and then true type of the lhs and store it there;
5876 then convert result back to the cast type to be the value
5877 of the assignment. */
5879 switch (TREE_CODE (lhs))
5884 case FIX_TRUNC_EXPR:
5885 case FIX_FLOOR_EXPR:
5886 case FIX_ROUND_EXPR:
5888 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5889 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5890 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5891 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5892 newrhs = default_conversion (newrhs);
5894 tree inner_lhs = TREE_OPERAND (lhs, 0);
5897 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5898 otherwise the result is an rvalue. */
5899 if (! lvalue_p (lhs))
5900 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5902 result = build_modify_expr (inner_lhs, NOP_EXPR,
5903 cp_convert (TREE_TYPE (inner_lhs),
5904 cp_convert (lhstype, newrhs)));
5905 if (result == error_mark_node)
5907 return cp_convert (TREE_TYPE (lhs), result);
5914 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5915 Reject anything strange now. */
5917 if (!lvalue_or_else (lhs, "assignment"))
5918 return error_mark_node;
5920 GNU_xref_assign (lhs);
5922 /* Warn about storing in something that is `const'. */
5923 /* For C++, don't warn if this is initialization. */
5924 if (modifycode != INIT_EXPR
5925 /* For assignment to `const' signature pointer/reference fields,
5926 don't warn either, we already printed a better message before. */
5927 && ! (TREE_CODE (lhs) == COMPONENT_REF
5928 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5929 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5930 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5931 /* Functions are not modifiable, even though they are
5933 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5934 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5935 && C_TYPE_FIELDS_READONLY (lhstype))
5936 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5937 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5938 readonly_error (lhs, "assignment", 0);
5940 /* If storing into a structure or union member,
5941 it has probably been given type `int'.
5942 Compute the type that would go with
5943 the actual amount of storage the member occupies. */
5945 if (TREE_CODE (lhs) == COMPONENT_REF
5946 && (TREE_CODE (lhstype) == INTEGER_TYPE
5947 || TREE_CODE (lhstype) == REAL_TYPE
5948 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5950 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5952 /* If storing in a field that is in actuality a short or narrower
5953 than one, we must store in the field in its actual type. */
5955 if (lhstype != TREE_TYPE (lhs))
5957 lhs = copy_node (lhs);
5958 TREE_TYPE (lhs) = lhstype;
5962 /* check to see if there is an assignment to `this' */
5963 if (lhs == current_class_ptr)
5965 if (flag_this_is_variable > 0
5966 && DECL_NAME (current_function_decl) != NULL_TREE
5967 && (DECL_NAME (current_function_decl)
5968 != constructor_name (current_class_type)))
5969 warning ("assignment to `this' not in constructor or destructor");
5970 current_function_just_assigned_this = 1;
5973 if (modifycode != INIT_EXPR)
5975 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5976 modifycode = NOP_EXPR;
5977 /* Reference-bashing */
5978 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5980 tree tmp = convert_from_reference (lhs);
5981 lhstype = TREE_TYPE (tmp);
5982 if (TYPE_SIZE (lhstype) == 0)
5984 incomplete_type_error (lhs, lhstype);
5985 return error_mark_node;
5990 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5992 tree tmp = convert_from_reference (newrhs);
5993 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5995 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5996 return error_mark_node;
6002 if (TREE_SIDE_EFFECTS (lhs))
6003 lhs = stabilize_reference (lhs);
6004 if (TREE_SIDE_EFFECTS (newrhs))
6005 newrhs = stabilize_reference (newrhs);
6007 /* Convert new value to destination type. */
6009 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6013 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
6015 cp_error ("incompatible types in assignment of `%T' to `%T'",
6016 TREE_TYPE (rhs), lhstype);
6017 return error_mark_node;
6020 /* Allow array assignment in compiler-generated code. */
6021 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
6022 pedwarn ("ANSI C++ forbids assignment of arrays");
6024 /* Have to wrap this in RTL_EXPR for two cases:
6025 in base or member initialization and if we
6026 are a branch of a ?: operator. Since we
6027 can't easily know the latter, just do it always. */
6029 result = make_node (RTL_EXPR);
6031 TREE_TYPE (result) = void_type_node;
6032 do_pending_stack_adjust ();
6033 start_sequence_for_rtl_expr (result);
6035 /* As a matter of principle, `start_sequence' should do this. */
6038 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6039 ? 1 + (modifycode != INIT_EXPR): 0;
6040 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
6043 do_pending_stack_adjust ();
6045 TREE_SIDE_EFFECTS (result) = 1;
6046 RTL_EXPR_SEQUENCE (result) = get_insns ();
6047 RTL_EXPR_RTL (result) = const0_rtx;
6052 if (modifycode == INIT_EXPR)
6054 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
6055 "assignment", NULL_TREE, 0);
6056 if (lhs == DECL_RESULT (current_function_decl))
6058 if (DECL_INITIAL (lhs))
6059 warning ("return value from function receives multiple initializations");
6060 DECL_INITIAL (lhs) = newrhs;
6065 /* Avoid warnings on enum bit fields. */
6066 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
6067 && TREE_CODE (lhstype) == INTEGER_TYPE)
6069 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
6071 newrhs = convert_force (lhstype, newrhs, 0);
6074 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
6076 if (TREE_CODE (newrhs) == CALL_EXPR
6077 && TYPE_NEEDS_CONSTRUCTING (lhstype))
6078 newrhs = build_cplus_new (lhstype, newrhs);
6080 /* Can't initialize directly from a TARGET_EXPR, since that would
6081 cause the lhs to be constructed twice, and possibly result in
6082 accidental self-initialization. So we force the TARGET_EXPR to be
6083 expanded without a target. */
6084 if (TREE_CODE (newrhs) == TARGET_EXPR)
6085 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6086 TREE_OPERAND (newrhs, 0));
6089 if (newrhs == error_mark_node)
6090 return error_mark_node;
6092 if (TREE_CODE (newrhs) == COND_EXPR)
6095 tree cond = TREE_OPERAND (newrhs, 0);
6097 if (TREE_SIDE_EFFECTS (lhs))
6098 cond = build_compound_expr (tree_cons
6100 build_expr_list (NULL_TREE, cond)));
6102 /* Cannot have two identical lhs on this one tree (result) as preexpand
6103 calls will rip them out and fill in RTL for them, but when the
6104 rtl is generated, the calls will only be in the first side of the
6105 condition, not on both, or before the conditional jump! (mrs) */
6106 lhs1 = break_out_calls (lhs);
6109 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6110 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6111 lhstype, lhs, newrhs);
6114 tree result_type = TREE_TYPE (newrhs);
6115 /* We have to convert each arm to the proper type because the
6116 types may have been munged by constant folding. */
6118 = build (COND_EXPR, result_type, cond,
6119 build_modify_expr (lhs, modifycode,
6120 cp_convert (result_type,
6121 TREE_OPERAND (newrhs, 1))),
6122 build_modify_expr (lhs1, modifycode,
6123 cp_convert (result_type,
6124 TREE_OPERAND (newrhs, 2))));
6128 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6129 lhstype, lhs, newrhs);
6131 TREE_SIDE_EFFECTS (result) = 1;
6133 /* If we got the LHS in a different type for storing in,
6134 convert the result back to the nominal type of LHS
6135 so that the value we return always has the same type
6136 as the LHS argument. */
6138 if (olhstype == TREE_TYPE (result))
6140 /* Avoid warnings converting integral types back into enums
6141 for enum bit fields. */
6142 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6143 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6145 result = build (COMPOUND_EXPR, olhstype, result, olhs);
6146 TREE_NO_UNUSED_WARNING (result) = 1;
6149 return convert_for_assignment (olhstype, result, "assignment",
6154 build_x_modify_expr (lhs, modifycode, rhs)
6156 enum tree_code modifycode;
6159 if (processing_template_decl)
6160 return build_min_nt (MODOP_EXPR, lhs,
6161 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6163 if (modifycode != NOP_EXPR)
6165 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6166 make_node (modifycode));
6170 return build_modify_expr (lhs, modifycode, rhs);
6174 /* Get difference in deltas for different pointer to member function
6175 types. Return integer_zero_node, if FROM cannot be converted to a
6176 TO type. If FORCE is true, then allow reverse conversions as well.
6178 Note that the naming of FROM and TO is kind of backwards; the return
6179 value is what we add to a TO in order to get a FROM. They are named
6180 this way because we call this function to find out how to convert from
6181 a pointer to member of FROM to a pointer to member of TO. */
6184 get_delta_difference (from, to, force)
6188 tree delta = integer_zero_node;
6194 /* Should get_base_distance here, so we can check if any thing along the
6195 path is virtual, and we need to make sure we stay
6196 inside the real binfos when going through virtual bases.
6197 Maybe we should replace virtual bases with
6198 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6199 binfo = get_binfo (from, to, 1);
6200 if (binfo == error_mark_node)
6202 error (" in pointer to member function conversion");
6209 error_not_base_type (from, to);
6210 error (" in pointer to member conversion");
6213 binfo = get_binfo (to, from, 1);
6214 if (binfo == 0 || binfo == error_mark_node)
6216 if (binfo_from_vbase (binfo))
6218 binfo = binfo_member (BINFO_TYPE (binfo),
6219 CLASSTYPE_VBASECLASSES (from));
6220 cp_warning ("pointer to member cast to virtual base `%T'",
6221 BINFO_TYPE (binfo));
6222 warning (" will only work if you are very careful");
6224 delta = BINFO_OFFSET (binfo);
6225 delta = cp_convert (ptrdiff_type_node, delta);
6227 return build_binary_op (MINUS_EXPR,
6232 if (binfo_from_vbase (binfo))
6236 cp_warning ("pointer to member cast from virtual base `%T'",
6237 BINFO_TYPE (binfo));
6238 warning (" will only work if you are very careful");
6241 cp_error ("pointer to member conversion from virtual base `%T'",
6242 BINFO_TYPE (binfo));
6245 return BINFO_OFFSET (binfo);
6249 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6250 tree type, delta, idx, pfn, delta2;
6255 /* This is the old way we did it. We want to avoid calling
6256 digest_init, so that it can give an error if we use { } when
6257 initializing a pointer to member function. */
6261 u = build_nt (CONSTRUCTOR, NULL_TREE,
6262 expr_tree_cons (pfn_identifier, pfn, NULL_TREE));
6266 u = build_nt (CONSTRUCTOR, NULL_TREE,
6267 expr_tree_cons (delta2_identifier, delta2, NULL_TREE));
6270 u = build_nt (CONSTRUCTOR, NULL_TREE,
6271 expr_tree_cons (NULL_TREE, delta,
6272 expr_tree_cons (NULL_TREE, idx,
6273 expr_tree_cons (NULL_TREE, u, NULL_TREE))));
6275 return digest_init (type, u, (tree*)0);
6277 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6279 int allconstant, allsimple;
6281 delta_field = TYPE_FIELDS (type);
6282 idx_field = TREE_CHAIN (delta_field);
6283 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6284 subtype = TREE_TYPE (pfn_or_delta2_field);
6285 pfn_field = TYPE_FIELDS (subtype);
6286 delta2_field = TREE_CHAIN (pfn_field);
6290 allconstant = TREE_CONSTANT (pfn);
6291 allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6292 u = expr_tree_cons (pfn_field, pfn, NULL_TREE);
6296 delta2 = convert_and_check (delta_type_node, delta2);
6297 allconstant = TREE_CONSTANT (delta2);
6298 allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6299 u = expr_tree_cons (delta2_field, delta2, NULL_TREE);
6302 delta = convert_and_check (delta_type_node, delta);
6303 idx = convert_and_check (delta_type_node, idx);
6305 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6306 allsimple = allsimple
6307 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6308 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6310 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6311 u = expr_tree_cons (delta_field, delta,
6312 expr_tree_cons (idx_field, idx,
6313 expr_tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6314 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6315 TREE_CONSTANT (u) = allconstant;
6316 TREE_STATIC (u) = allconstant && allsimple;
6321 /* Build a constructor for a pointer to member function. It can be
6322 used to initialize global variables, local variable, or used
6323 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6326 If FORCE is non-zero, then force this conversion, even if
6327 we would rather not do it. Usually set when using an explicit
6330 Return error_mark_node, if something goes wrong. */
6333 build_ptrmemfunc (type, pfn, force)
6338 tree pfn_type = TREE_TYPE (pfn);
6339 tree to_type = build_ptrmemfunc_type (type);
6341 /* Handle multiple conversions of pointer to member functions. */
6342 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6344 tree idx = integer_zero_node;
6345 tree delta = integer_zero_node;
6346 tree delta2 = integer_zero_node;
6347 tree npfn = NULL_TREE;
6348 tree ndelta, ndelta2;
6352 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6353 cp_error ("conversion to `%T' from `%T'",
6356 if (TREE_CODE (pfn) == PTRMEM_CST)
6358 /* We could just build the resulting CONSTRUCTOR now, but we
6359 don't, relying on the general machinery below, together
6360 with constant-folding, to do the right thing. We don't
6361 want to return a PTRMEM_CST here, since a
6362 pointer-to-member constant is no longer a valid template
6363 argument once it is cast to any type, including its
6365 expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
6367 /* This constant points to a non-virtual function.
6368 NDELTA2 will be NULL, but it's value doesn't really
6369 matter since we won't use it anyhow. */
6370 ndelta2 = integer_zero_node;
6372 else if (same_type_p (to_type, pfn_type))
6373 /* We don't have to do any conversion. Note that we do this
6374 after checking for a PTRMEM_CST so that a PTRMEM_CST, cast
6375 to its own type, will not be considered a legal non-type
6376 template argument. */
6380 ndelta = cp_convert (ptrdiff_type_node,
6381 build_component_ref (pfn,
6384 ndelta2 = cp_convert (ptrdiff_type_node,
6385 DELTA2_FROM_PTRMEMFUNC (pfn));
6386 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6389 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6390 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6392 delta = build_binary_op (PLUS_EXPR, ndelta, n);
6393 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n);
6394 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6396 /* If it's a virtual function, this is what we want. */
6397 e2 = build_ptrmemfunc1 (to_type, delta, idx,
6400 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6401 npfn = build1 (NOP_EXPR, type, pfn);
6402 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6404 /* But if it's a non-virtual function, or NULL, we use this
6406 e3 = build_ptrmemfunc1 (to_type, delta,
6407 idx, npfn, NULL_TREE);
6408 return build_conditional_expr (e1, e2, e3);
6411 /* Handle null pointer to member function conversions. */
6412 if (integer_zerop (pfn))
6414 pfn = build_c_cast (type, integer_zero_node);
6415 return build_ptrmemfunc1 (to_type,
6416 integer_zero_node, integer_zero_node,
6420 if (type_unknown_p (pfn))
6421 return instantiate_type (type, pfn, 1);
6423 fn = TREE_OPERAND (pfn, 0);
6424 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6425 return make_ptrmem_cst (to_type, fn);
6428 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6432 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6439 tree type = TREE_TYPE (cst);
6440 tree fn = PTRMEM_CST_MEMBER (cst);
6441 tree ptr_class, fn_class;
6443 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6445 /* The class that the function belongs to. */
6446 fn_class = DECL_CLASS_CONTEXT (fn);
6448 /* The class that we're creating a pointer to member of. */
6449 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6451 /* First, calculate the adjustment to the function's class. */
6452 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6454 if (!DECL_VIRTUAL_P (fn))
6456 *idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6457 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6458 *delta2 = NULL_TREE;
6462 /* If we're dealing with a virtual function, we have to adjust 'this'
6463 again, to point to the base which provides the vtable entry for
6464 fn; the call will do the opposite adjustment. */
6465 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6466 tree binfo = binfo_or_else (orig_class, fn_class);
6467 *delta = size_binop (PLUS_EXPR, *delta, BINFO_OFFSET (binfo));
6469 /* Map everything down one to make room for the null PMF. */
6470 *idx = size_binop (PLUS_EXPR, DECL_VINDEX (fn), integer_one_node);
6473 /* Offset from an object of PTR_CLASS to the vptr for ORIG_CLASS. */
6474 *delta2 = size_binop (PLUS_EXPR, *delta,
6475 get_vfield_offset (TYPE_BINFO (orig_class)));
6479 /* Return an expression for DELTA2 from the pointer-to-member function
6483 delta2_from_ptrmemfunc (t)
6486 if (TREE_CODE (t) == PTRMEM_CST)
6493 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6498 return (build_component_ref
6499 (build_component_ref (t,
6500 pfn_or_delta2_identifier, NULL_TREE,
6502 delta2_identifier, NULL_TREE, 0));
6505 /* Return an expression for PFN from the pointer-to-member function
6509 pfn_from_ptrmemfunc (t)
6512 if (TREE_CODE (t) == PTRMEM_CST)
6519 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6524 return (build_component_ref
6525 (build_component_ref (t,
6526 pfn_or_delta2_identifier, NULL_TREE,
6528 pfn_identifier, NULL_TREE, 0));
6531 /* Convert value RHS to type TYPE as preparation for an assignment to
6532 an lvalue of type TYPE. ERRTYPE is a string to use in error
6533 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6534 are doing the conversion in order to pass the PARMNUMth argument of
6538 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6540 const char *errtype;
6544 register enum tree_code codel = TREE_CODE (type);
6545 register tree rhstype;
6546 register enum tree_code coder;
6548 if (codel == OFFSET_TYPE)
6549 my_friendly_abort (990505);
6551 if (TREE_CODE (rhs) == OFFSET_REF)
6552 rhs = resolve_offset_ref (rhs);
6554 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6555 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6556 rhs = TREE_OPERAND (rhs, 0);
6558 rhstype = TREE_TYPE (rhs);
6559 coder = TREE_CODE (rhstype);
6561 if (rhs == error_mark_node || rhstype == error_mark_node)
6562 return error_mark_node;
6563 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6564 return error_mark_node;
6566 /* Issue warnings about peculiar, but legal, uses of NULL. */
6567 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6568 cp_warning ("converting NULL to non-pointer type");
6570 /* The RHS of an assignment cannot have void type. */
6571 if (coder == VOID_TYPE)
6573 error ("void value not ignored as it ought to be");
6574 return error_mark_node;
6577 /* Simplify the RHS if possible. */
6578 if (TREE_CODE (rhs) == CONST_DECL)
6579 rhs = DECL_INITIAL (rhs);
6580 else if (TREE_READONLY_DECL_P (rhs))
6581 rhs = decl_constant_value (rhs);
6583 /* Warn about assigning a floating-point type to an integer type. */
6584 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6587 cp_warning ("`%T' used for argument %P of `%D'",
6588 rhstype, parmnum, fndecl);
6590 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6592 /* And warn about assigning a negative value to an unsigned
6594 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6596 if (TREE_CODE (rhs) == INTEGER_CST
6597 && TREE_NEGATED_INT (rhs))
6600 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6601 rhs, parmnum, fndecl);
6603 cp_warning ("%s of negative value `%E' to `%T'",
6604 errtype, rhs, type);
6606 overflow_warning (rhs);
6607 if (TREE_CONSTANT (rhs))
6613 The expression is implicitly converted (clause _conv_) to the
6614 cv-unqualified type of the left operand. */
6615 if (!can_convert_arg (type, rhstype, rhs))
6617 /* When -Wno-pmf-converions is use, we just silently allow
6618 conversions from pointers-to-members to plain pointers. If
6619 the conversion doesn't work, cp_convert will complain. */
6621 && TYPE_PTR_P (type)
6622 && TYPE_PTRMEMFUNC_P (rhstype))
6623 rhs = cp_convert (strip_top_quals (type), rhs);
6624 /* If the right-hand side has unknown type, then it is an
6625 overloaded function. Call instantiate_type to get error
6627 else if (rhstype == unknown_type_node)
6628 instantiate_type (type, rhs, 1);
6630 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6631 rhstype, type, parmnum, fndecl);
6633 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6635 return error_mark_node;
6637 return perform_implicit_conversion (strip_top_quals (type), rhs);
6640 /* Convert RHS to be of type TYPE.
6641 If EXP is non-zero, it is the target of the initialization.
6642 ERRTYPE is a string to use in error messages.
6644 Two major differences between the behavior of
6645 `convert_for_assignment' and `convert_for_initialization'
6646 are that references are bashed in the former, while
6647 copied in the latter, and aggregates are assigned in
6648 the former (operator=) while initialized in the
6651 If using constructor make sure no conversion operator exists, if one does
6652 exist, an ambiguity exists.
6654 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6657 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6658 tree exp, type, rhs;
6660 const char *errtype;
6664 register enum tree_code codel = TREE_CODE (type);
6665 register tree rhstype;
6666 register enum tree_code coder;
6668 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6669 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6670 if (TREE_CODE (rhs) == NOP_EXPR
6671 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6672 && codel != REFERENCE_TYPE)
6673 rhs = TREE_OPERAND (rhs, 0);
6675 if (rhs == error_mark_node
6676 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6677 return error_mark_node;
6679 if (TREE_CODE (rhs) == OFFSET_REF)
6681 rhs = resolve_offset_ref (rhs);
6682 if (rhs == error_mark_node)
6683 return error_mark_node;
6686 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6687 rhs = convert_from_reference (rhs);
6689 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6690 && TREE_CODE (type) != ARRAY_TYPE
6691 && (TREE_CODE (type) != REFERENCE_TYPE
6692 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6693 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6694 && (TREE_CODE (type) != REFERENCE_TYPE
6695 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6696 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6697 rhs = default_conversion (rhs);
6699 rhstype = TREE_TYPE (rhs);
6700 coder = TREE_CODE (rhstype);
6702 if (coder == ERROR_MARK)
6703 return error_mark_node;
6705 /* We accept references to incomplete types, so we can
6706 return here before checking if RHS is of complete type. */
6708 if (codel == REFERENCE_TYPE)
6710 /* This should eventually happen in convert_arguments. */
6711 extern int warningcount, errorcount;
6712 int savew = 0, savee = 0;
6715 savew = warningcount, savee = errorcount;
6716 rhs = initialize_reference (type, rhs);
6719 if (warningcount > savew)
6720 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6721 else if (errorcount > savee)
6722 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6728 exp = require_complete_type (exp);
6729 if (exp == error_mark_node)
6730 return error_mark_node;
6732 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6733 rhstype = TREE_TYPE (rhstype);
6735 type = complete_type (type);
6737 if (TYPE_LANG_SPECIFIC (type)
6738 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
6739 return build_signature_pointer_constructor (type, rhs);
6741 if (IS_AGGR_TYPE (type))
6742 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6744 if (type == TREE_TYPE (rhs))
6746 /* Issue warnings about peculiar, but legal, uses of NULL. We
6747 do this *before* the call to decl_constant_value so as to
6748 avoid duplicate warnings on code like `const int I = NULL;
6750 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6751 cp_warning ("converting NULL to non-pointer type");
6753 if (TREE_READONLY_DECL_P (rhs))
6754 rhs = decl_constant_value (rhs);
6759 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6762 /* Expand an ASM statement with operands, handling output operands
6763 that are not variables or INDIRECT_REFS by transforming such
6764 cases into cases that expand_asm_operands can handle.
6766 Arguments are same as for expand_asm_operands.
6768 We don't do default conversions on all inputs, because it can screw
6769 up operands that are expected to be in memory. */
6772 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6773 tree string, outputs, inputs, clobbers;
6778 int noutputs = list_length (outputs);
6780 /* o[I] is the place that output number I should be written. */
6781 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6784 /* Record the contents of OUTPUTS before it is modified. */
6785 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6786 o[i] = TREE_VALUE (tail);
6788 /* Generate the ASM_OPERANDS insn;
6789 store into the TREE_VALUEs of OUTPUTS some trees for
6790 where the values were actually stored. */
6791 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6793 /* Copy all the intermediate outputs into the specified outputs. */
6794 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6796 if (o[i] != TREE_VALUE (tail))
6798 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6799 const0_rtx, VOIDmode, EXPAND_NORMAL);
6802 /* Detect modification of read-only values.
6803 (Otherwise done by build_modify_expr.) */
6806 tree type = TREE_TYPE (o[i]);
6807 if (CP_TYPE_CONST_P (type)
6808 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6809 && C_TYPE_FIELDS_READONLY (type)))
6810 readonly_error (o[i], "modification by `asm'", 1);
6814 /* Those MODIFY_EXPRs could do autoincrements. */
6818 /* Expand a C `return' statement.
6819 RETVAL is the expression for what to return,
6820 or a null pointer for `return;' with no value.
6822 C++: upon seeing a `return', we must call destructors on all
6823 variables in scope which had constructors called on them.
6824 This means that if in a destructor, the base class destructors
6825 must be called before returning.
6827 The RETURN statement in C++ has initialization semantics. */
6830 c_expand_return (retval)
6833 extern tree dtor_label, ctor_label;
6834 tree result = DECL_RESULT (current_function_decl);
6835 tree valtype = TREE_TYPE (result);
6837 if (TREE_THIS_VOLATILE (current_function_decl))
6838 warning ("function declared `noreturn' has a `return' statement");
6840 if (retval == error_mark_node)
6842 current_function_returns_null = 1;
6846 if (processing_template_decl)
6848 add_tree (build_min_nt (RETURN_STMT, retval));
6855 error ("returning a value from a destructor");
6857 /* Can't just return from a destructor. */
6858 expand_goto (dtor_label);
6862 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6863 if ((DECL_NAME (current_function_decl) == ansi_opname[(int) NEW_EXPR]
6864 || DECL_NAME (current_function_decl) == ansi_opname[(int) VEC_NEW_EXPR])
6865 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6866 && null_ptr_cst_p (retval))
6867 cp_warning ("operator new should throw an exception, not return NULL");
6869 if (retval == NULL_TREE)
6871 /* A non-named return value does not count. */
6873 if (DECL_CONSTRUCTOR_P (current_function_decl))
6874 retval = current_class_ptr;
6875 else if (DECL_NAME (result) != NULL_TREE
6876 && TREE_CODE (valtype) != VOID_TYPE)
6880 current_function_returns_null = 1;
6882 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
6884 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
6886 pedwarn ("`return' with no value, in function returning non-void");
6887 /* Clear this, so finish_function won't say that we
6888 reach the end of a non-void function (which we don't,
6889 we gave a return!). */
6890 current_function_returns_null = 0;
6894 expand_null_return ();
6898 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6900 if (flag_this_is_variable)
6901 error ("return from a constructor: use `this = ...' instead");
6903 error ("returning a value from a constructor");
6904 retval = current_class_ptr;
6907 /* Effective C++ rule 15. See also start_function. */
6909 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
6910 && retval != current_class_ref)
6911 cp_warning ("`operator=' should return a reference to `*this'");
6913 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
6915 current_function_returns_null = 1;
6916 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6917 pedwarn ("`return' with a value, in function returning void");
6918 expand_return (retval);
6922 /* Now deal with possible C++ hair:
6923 (1) Compute the return value.
6924 (2) If there are aggregate values with destructors which
6925 must be cleaned up, clean them (taking care
6926 not to clobber the return value).
6927 (3) If an X(X&) constructor is defined, the return
6928 value must be returned via that. */
6930 if (retval == result
6931 || DECL_CONSTRUCTOR_P (current_function_decl))
6932 /* It's already done for us. */;
6933 else if (TREE_CODE (TREE_TYPE (retval)) == VOID_TYPE)
6935 pedwarn ("return of void value in function returning non-void");
6936 expand_expr_stmt (retval);
6941 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6943 /* First convert the value to the function's return type, then
6944 to the type of return value's location to handle the
6945 case that functype is thiner than the valtype. */
6947 retval = convert_for_initialization
6948 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6949 "return", NULL_TREE, 0);
6951 retval = convert (valtype, retval);
6953 if (retval == error_mark_node)
6955 /* Avoid warning about control reaching end of function. */
6956 expand_null_return ();
6960 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6961 else if (! current_function_returns_struct
6962 && TREE_CODE (retval) == TARGET_EXPR
6963 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6964 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6965 TREE_OPERAND (retval, 0));
6967 /* Add some useful error checking for C++. */
6968 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
6970 tree whats_returned;
6972 /* Sort through common things to see what it is
6973 we are returning. */
6974 whats_returned = retval;
6975 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6977 whats_returned = TREE_OPERAND (whats_returned, 1);
6978 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6979 whats_returned = TREE_OPERAND (whats_returned, 0);
6981 while (TREE_CODE (whats_returned) == CONVERT_EXPR
6982 || TREE_CODE (whats_returned) == NOP_EXPR)
6983 whats_returned = TREE_OPERAND (whats_returned, 0);
6984 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6986 whats_returned = TREE_OPERAND (whats_returned, 0);
6987 while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6988 || TREE_CODE (whats_returned) == TARGET_EXPR)
6990 /* Get the target. */
6991 whats_returned = TREE_OPERAND (whats_returned, 0);
6992 warning ("returning reference to temporary");
6996 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
6998 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
6999 warning ("reference to non-lvalue returned");
7000 else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
7001 && DECL_FUNCTION_SCOPE_P (whats_returned)
7002 && !(TREE_STATIC (whats_returned)
7003 || TREE_PUBLIC (whats_returned)))
7004 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
7007 else if (TREE_CODE (retval) == ADDR_EXPR)
7009 tree whats_returned = TREE_OPERAND (retval, 0);
7011 if (TREE_CODE (whats_returned) == VAR_DECL
7012 && DECL_NAME (whats_returned)
7013 && DECL_FUNCTION_SCOPE_P (whats_returned)
7014 && !(TREE_STATIC (whats_returned)
7015 || TREE_PUBLIC (whats_returned)))
7016 cp_warning_at ("address of local variable `%D' returned", whats_returned);
7020 if (retval != NULL_TREE
7021 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
7022 && ! in_control_zone_p ())
7023 current_function_return_value = retval;
7025 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7027 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
7028 expand_goto (ctor_label);
7031 if (retval && retval != result)
7033 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7034 TREE_SIDE_EFFECTS (result) = 1;
7037 expand_start_target_temps ();
7039 expand_return (result);
7041 expand_end_target_temps ();
7043 current_function_returns_value = 1;
7046 /* Start a C switch statement, testing expression EXP.
7047 Return EXP if it is valid, an error node otherwise. */
7050 c_expand_start_case (exp)
7055 exp = build_expr_type_conversion (WANT_INT | WANT_ENUM, exp, 1);
7056 if (exp == NULL_TREE)
7058 error ("switch quantity not an integer");
7059 exp = error_mark_node;
7061 if (exp == error_mark_node)
7062 return error_mark_node;
7064 exp = default_conversion (exp);
7065 type = TREE_TYPE (exp);
7066 idx = get_unwidened (exp, 0);
7067 /* We can't strip a conversion from a signed type to an unsigned,
7068 because if we did, int_fits_type_p would do the wrong thing
7069 when checking case values for being in range,
7070 and it's too hard to do the right thing. */
7071 if (TREE_UNSIGNED (TREE_TYPE (exp)) == TREE_UNSIGNED (TREE_TYPE (idx)))
7075 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7076 type, "switch statement");
7081 /* Returns non-zero if the pointer-type FROM can be converted to the
7082 pointer-type TO via a qualification conversion. If CONSTP is -1,
7083 then we return non-zero if the pointers are similar, and the
7084 cv-qualification signature of FROM is a proper subset of that of TO.
7086 If CONSTP is positive, then all outer pointers have been
7090 comp_ptr_ttypes_real (to, from, constp)
7094 int to_more_cv_qualified = 0;
7096 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7098 if (TREE_CODE (to) != TREE_CODE (from))
7101 if (TREE_CODE (from) == OFFSET_TYPE
7102 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7103 TYPE_OFFSET_BASETYPE (to)))
7106 /* Const and volatile mean something different for function types,
7107 so the usual checks are not appropriate. */
7108 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7110 if (!at_least_as_qualified_p (to, from))
7113 if (!at_least_as_qualified_p (from, to))
7118 ++to_more_cv_qualified;
7122 constp &= TYPE_READONLY (to);
7125 if (TREE_CODE (to) != POINTER_TYPE)
7127 same_type_p (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from))
7128 && (constp >= 0 || to_more_cv_qualified);
7132 /* When comparing, say, char ** to char const **, this function takes the
7133 'char *' and 'char const *'. Do not pass non-pointer types to this
7137 comp_ptr_ttypes (to, from)
7140 return comp_ptr_ttypes_real (to, from, 1);
7143 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7144 type or inheritance-related types, regardless of cv-quals. */
7147 ptr_reasonably_similar (to, from)
7150 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7152 if (TREE_CODE (to) != TREE_CODE (from))
7155 if (TREE_CODE (from) == OFFSET_TYPE
7156 && comptypes (TYPE_OFFSET_BASETYPE (to),
7157 TYPE_OFFSET_BASETYPE (from),
7158 COMPARE_BASE | COMPARE_RELAXED))
7161 if (TREE_CODE (to) != POINTER_TYPE)
7163 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
7164 COMPARE_BASE | COMPARE_RELAXED);
7168 /* Like comp_ptr_ttypes, for const_cast. */
7171 comp_ptr_ttypes_const (to, from)
7174 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7176 if (TREE_CODE (to) != TREE_CODE (from))
7179 if (TREE_CODE (from) == OFFSET_TYPE
7180 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7181 TYPE_OFFSET_BASETYPE (to)))
7184 if (TREE_CODE (to) != POINTER_TYPE)
7185 return same_type_p (TYPE_MAIN_VARIANT (to),
7186 TYPE_MAIN_VARIANT (from));
7190 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7193 comp_ptr_ttypes_reinterpret (to, from)
7198 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7200 if (TREE_CODE (from) == OFFSET_TYPE)
7201 from = TREE_TYPE (from);
7202 if (TREE_CODE (to) == OFFSET_TYPE)
7203 to = TREE_TYPE (to);
7205 /* Const and volatile mean something different for function types,
7206 so the usual checks are not appropriate. */
7207 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7208 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7210 if (!at_least_as_qualified_p (to, from))
7214 && !at_least_as_qualified_p (from, to))
7216 constp &= TYPE_READONLY (to);
7219 if (TREE_CODE (from) != POINTER_TYPE
7220 || TREE_CODE (to) != POINTER_TYPE)
7225 /* Returns the type-qualifier set corresponding to TYPE. */
7228 cp_type_quals (type)
7231 while (TREE_CODE (type) == ARRAY_TYPE)
7232 type = TREE_TYPE (type);
7234 return TYPE_QUALS (type);
7237 /* Returns non-zero if the TYPE contains a mutable member */
7240 cp_has_mutable_p (type)
7243 while (TREE_CODE (type) == ARRAY_TYPE)
7244 type = TREE_TYPE (type);
7246 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7249 /* Subroutine of casts_away_constness. Make T1 and T2 point at
7250 exemplar types such that casting T1 to T2 is casting away castness
7251 if and only if there is no implicit conversion from T1 to T2. */
7254 casts_away_constness_r (t1, t2)
7261 /* [expr.const.cast]
7263 For multi-level pointer to members and multi-level mixed pointers
7264 and pointers to members (conv.qual), the "member" aspect of a
7265 pointer to member level is ignored when determining if a const
7266 cv-qualifier has been cast away. */
7267 if (TYPE_PTRMEM_P (*t1))
7268 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
7269 if (TYPE_PTRMEM_P (*t2))
7270 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
7272 /* [expr.const.cast]
7274 For two pointer types:
7276 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
7277 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
7280 casting from X1 to X2 casts away constness if, for a non-pointer
7281 type T there does not exist an implicit conversion (clause
7284 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7288 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
7290 if (TREE_CODE (*t1) != POINTER_TYPE
7291 || TREE_CODE (*t2) != POINTER_TYPE)
7293 *t1 = cp_build_qualified_type (void_type_node,
7294 CP_TYPE_QUALS (*t1));
7295 *t2 = cp_build_qualified_type (void_type_node,
7296 CP_TYPE_QUALS (*t2));
7300 quals1 = CP_TYPE_QUALS (*t1);
7301 quals2 = CP_TYPE_QUALS (*t2);
7302 *t1 = TREE_TYPE (*t1);
7303 *t2 = TREE_TYPE (*t2);
7304 casts_away_constness_r (t1, t2);
7305 *t1 = build_pointer_type (*t1);
7306 *t2 = build_pointer_type (*t2);
7307 *t1 = cp_build_qualified_type (*t1, quals1);
7308 *t2 = cp_build_qualified_type (*t2, quals2);
7311 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
7315 casts_away_constness (t1, t2)
7319 if (TREE_CODE (t2) == REFERENCE_TYPE)
7321 /* [expr.const.cast]
7323 Casting from an lvalue of type T1 to an lvalue of type T2
7324 using a reference cast casts away constness if a cast from an
7325 rvalue of type "pointer to T1" to the type "pointer to T2"
7326 casts away constness. */
7327 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
7328 ? TREE_TYPE (t1) : t1);
7329 return casts_away_constness (build_pointer_type (t1),
7330 build_pointer_type (TREE_TYPE (t2)));
7333 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7334 /* [expr.const.cast]
7336 Casting from an rvalue of type "pointer to data member of X
7337 of type T1" to the type "pointer to data member of Y of type
7338 T2" casts away constness if a cast from an rvalue of type
7339 "poitner to T1" to the type "pointer to T2" casts away
7341 return casts_away_constness
7342 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7343 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7345 /* Casting away constness is only something that makes sense for
7346 pointer or reference types. */
7347 if (TREE_CODE (t1) != POINTER_TYPE
7348 || TREE_CODE (t2) != POINTER_TYPE)
7351 /* Top-level qualifiers don't matter. */
7352 t1 = TYPE_MAIN_VARIANT (t1);
7353 t2 = TYPE_MAIN_VARIANT (t2);
7354 casts_away_constness_r (&t1, &t2);
7355 if (!can_convert (t2, t1))