1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
42 static tree convert_for_assignment PROTO((tree, tree, const char *, tree,
44 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
45 static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
46 static int comp_target_parms PROTO((tree, tree, int));
47 static int comp_ptr_ttypes_real PROTO((tree, tree, int));
48 static int comp_ptr_ttypes_const PROTO((tree, tree));
49 static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
50 static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
52 static tree common_base_type PROTO((tree, tree));
54 static tree convert_sequence PROTO((tree, tree));
56 static tree lookup_anon_field PROTO((tree, tree));
57 static tree pointer_diff PROTO((tree, tree, tree));
58 static tree build_component_addr PROTO((tree, tree));
59 static tree qualify_type PROTO((tree, tree));
60 static tree get_delta_difference PROTO((tree, tree, int));
61 static int comp_cv_target_types PROTO((tree, tree, int));
62 static void casts_away_constness_r PROTO((tree *, tree *));
63 static int casts_away_constness PROTO ((tree, tree));
65 /* Return the target type of TYPE, which meas return T for:
66 T*, T&, T[], T (...), and otherwise, just T. */
72 if (TREE_CODE (type) == REFERENCE_TYPE)
73 type = TREE_TYPE (type);
74 while (TREE_CODE (type) == POINTER_TYPE
75 || TREE_CODE (type) == ARRAY_TYPE
76 || TREE_CODE (type) == FUNCTION_TYPE
77 || TREE_CODE (type) == METHOD_TYPE
78 || TREE_CODE (type) == OFFSET_TYPE)
79 type = TREE_TYPE (type);
83 /* Do `exp = require_complete_type (exp);' to make sure exp
84 does not have an incomplete type. (That includes void types.)
85 Returns the error_mark_node if the VALUE does not have
86 complete type when this function returns. */
89 require_complete_type (value)
94 if (processing_template_decl || value == error_mark_node)
97 if (TREE_CODE (value) == OVERLOAD)
98 type = unknown_type_node;
100 type = TREE_TYPE (value);
102 /* First, detect a valid value with a complete type. */
103 if (TYPE_SIZE (type) != 0
104 && TYPE_SIZE (type) != size_zero_node
105 && ! (TYPE_LANG_SPECIFIC (type)
106 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
107 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
110 /* If we see X::Y, we build an OFFSET_TYPE which has
111 not been laid out. Try to avoid an error by interpreting
112 it as this->X::Y, if reasonable. */
113 if (TREE_CODE (value) == OFFSET_REF
114 && current_class_ref != 0
115 && TREE_OPERAND (value, 0) == current_class_ref)
117 tree base, member = TREE_OPERAND (value, 1);
118 tree basetype = TYPE_OFFSET_BASETYPE (type);
119 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
120 base = convert_pointer_to (basetype, current_class_ptr);
121 value = build (COMPONENT_REF, TREE_TYPE (member),
122 build_indirect_ref (base, NULL_PTR), member);
123 return require_complete_type (value);
126 if (complete_type_or_else (type, value))
129 return error_mark_node;
132 /* Makes sure EXPR is a complete type when used in a void context, like a
133 whole expression, or lhs of a comma operator. Issue a diagnostic and
134 return error_mark_node on failure. This is a little tricky, because some
135 valid void types look stunningly similar to invalid void types. We err on
136 the side of caution */
139 require_complete_type_in_void (expr)
142 switch (TREE_CODE (expr))
148 op = TREE_OPERAND (expr,2);
149 op = require_complete_type_in_void (op);
150 TREE_OPERAND (expr,2) = op;
151 if (op == error_mark_node)
164 op = TREE_OPERAND (expr,1);
165 op = require_complete_type_in_void (op);
166 TREE_OPERAND (expr,1) = op;
167 if (op == error_mark_node)
176 case NON_LVALUE_EXPR:
181 op = TREE_OPERAND (expr,0);
182 op = require_complete_type_in_void (op);
183 TREE_OPERAND (expr,0) = op;
184 if (op == error_mark_node)
192 case CALL_EXPR: /* function call return can be ignored */
193 case RTL_EXPR: /* RTL nodes have no value */
194 case DELETE_EXPR: /* delete expressions have no type */
195 case VEC_DELETE_EXPR:
196 case INTEGER_CST: /* used for null pointer */
197 case EXIT_EXPR: /* have no return */
198 case LOOP_EXPR: /* have no return */
199 case BIND_EXPR: /* have no return */
200 case THROW_EXPR: /* have no return */
201 case MODIFY_EXPR: /* sometimes this has a void type, but that's ok */
202 case CONVERT_EXPR: /* sometimes has a void type */
207 tree op = TREE_OPERAND (expr,0);
209 /* Calling a function returning a reference has an implicit
210 dereference applied. We don't want to make that an error. */
211 if (TREE_CODE (op) == CALL_EXPR
212 && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
214 /* else fallthrough */
218 expr = require_complete_type (expr);
225 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
226 a template instantiation, do the instantiation. Returns TYPE,
227 whether or not it could be completed, unless something goes
228 horribly wrong, in which case the error_mark_node is returned. */
234 if (type == NULL_TREE)
235 /* Rather than crash, we return something sure to cause an error
237 return error_mark_node;
239 if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
241 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
243 tree t = complete_type (TREE_TYPE (type));
244 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
246 TYPE_NEEDS_CONSTRUCTING (type)
247 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
248 TYPE_NEEDS_DESTRUCTOR (type)
249 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
251 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
252 instantiate_class_template (TYPE_MAIN_VARIANT (type));
257 /* Like complete_type, but issue an error if the TYPE cannot be
258 completed. VALUE is used for informative diagnostics.
259 Returns NULL_TREE if the type cannot be made complete. */
262 complete_type_or_else (type, value)
266 type = complete_type (type);
267 if (type == error_mark_node)
268 /* We already issued an error. */
270 else if (!TYPE_SIZE (type) || TYPE_SIZE (type) == size_zero_node)
272 incomplete_type_error (value, type);
279 /* Return truthvalue of whether type of EXP is instantiated. */
285 return (TREE_CODE (exp) == OVERLOAD
286 || TREE_CODE (exp) == TREE_LIST
287 || TREE_TYPE (exp) == unknown_type_node
288 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
289 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
292 /* Return truthvalue of whether T is function (or pfn) type. */
298 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
299 || (TREE_CODE (t) == POINTER_TYPE
300 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
301 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
304 /* Return a variant of TYPE which has all the type qualifiers of LIKE
305 as well as those of TYPE. */
308 qualify_type (type, like)
311 /* @@ Must do member pointers here. */
312 return cp_build_qualified_type (type, (CP_TYPE_QUALS (type)
313 | CP_TYPE_QUALS (like)));
316 /* Return the common type of two parameter lists.
317 We assume that comptypes has already been done and returned 1;
318 if that isn't so, this may crash.
320 As an optimization, free the space we allocate if the parameter
321 lists are already common. */
327 tree oldargs = p1, newargs, n;
330 char *first_obj = (char *) oballoc (0);
332 len = list_length (p1);
333 newargs = tree_last (p1);
335 if (newargs == void_list_node)
344 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
349 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
351 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
353 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
356 else if (! TREE_PURPOSE (p1))
358 if (TREE_PURPOSE (p2))
360 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
366 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
368 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
370 if (TREE_VALUE (p1) != TREE_VALUE (p2))
373 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
376 TREE_VALUE (n) = TREE_VALUE (p1);
387 /* Given a type, perhaps copied for a typedef,
388 find the "original" version of it. */
393 while (TYPE_NAME (t) != NULL_TREE)
395 tree x = TYPE_NAME (t);
396 if (TREE_CODE (x) != TYPE_DECL)
398 x = DECL_ORIGINAL_TYPE (x);
406 /* T1 and T2 are arithmetic or enumeration types. Return the type
407 that will result from the "usual arithmetic converions" on T1 and
408 T2 as described in [expr]. */
411 type_after_usual_arithmetic_conversions (t1, t2)
415 enum tree_code code1 = TREE_CODE (t1);
416 enum tree_code code2 = TREE_CODE (t2);
419 /* FIXME: Attributes. */
420 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
421 || TREE_CODE (t1) == ENUMERAL_TYPE,
423 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
424 || TREE_CODE (t2) == ENUMERAL_TYPE,
427 /* In what follows, we slightly generalize the rules given in [expr]
428 so as to deal with `long long'. First, merge the attributes. */
429 attributes = merge_machine_type_attributes (t1, t2);
431 /* If only one is real, use it as the result. */
432 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
433 return build_type_attribute_variant (t1, attributes);
434 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
435 return build_type_attribute_variant (t2, attributes);
437 /* Perform the integral promotions. */
438 if (code1 != REAL_TYPE)
440 t1 = type_promotes_to (t1);
441 t2 = type_promotes_to (t2);
444 /* Both real or both integers; use the one with greater precision. */
445 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
446 return build_type_attribute_variant (t1, attributes);
447 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
448 return build_type_attribute_variant (t2, attributes);
450 if (code1 != REAL_TYPE)
452 /* If one is unsigned long long, then convert the other to unsigned
454 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
455 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
456 return build_type_attribute_variant (long_long_unsigned_type_node,
458 /* If one is a long long, and the other is an unsigned long, and
459 long long can represent all the values of an unsigned long, then
460 convert to a long long. Otherwise, convert to an unsigned long
461 long. Otherwise, if either operand is long long, convert the
464 Since we're here, we know the TYPE_PRECISION is the same;
465 therefore converting to long long cannot represent all the values
466 of an unsigned long, so we choose unsigned long long in that
468 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
469 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
471 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
472 ? long_long_unsigned_type_node
473 : long_long_integer_type_node);
474 return build_type_attribute_variant (t, attributes);
477 /* Go through the same procedure, but for longs. */
478 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
479 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
480 return build_type_attribute_variant (long_unsigned_type_node,
482 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
483 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
485 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
486 ? long_unsigned_type_node : long_integer_type_node);
487 return build_type_attribute_variant (t, attributes);
489 /* Otherwise prefer the unsigned one. */
490 if (TREE_UNSIGNED (t1))
491 return build_type_attribute_variant (t1, attributes);
493 return build_type_attribute_variant (t2, attributes);
497 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
498 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
499 return build_type_attribute_variant (long_double_type_node,
501 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
502 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
503 return build_type_attribute_variant (double_type_node,
506 return build_type_attribute_variant (float_type_node,
511 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
512 ARG1 and ARG2 are the values with those types. The LOCATION is a
513 string describing the current location, in case an error occurs. */
516 composite_pointer_type (t1, t2, arg1, arg2, location)
527 If one operand is a null pointer constant, the composite pointer
528 type is the type of the other operand. */
529 if (null_ptr_cst_p (arg1))
531 if (null_ptr_cst_p (arg2))
534 /* Deal with pointer-to-member functions in the same way as we deal
535 with pointers to functions. */
536 if (TYPE_PTRMEMFUNC_P (t1))
537 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
538 if (TYPE_PTRMEMFUNC_P (t2))
539 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
541 if (comp_target_types (t1, t2, 1))
542 result_type = common_type (t1, t2);
543 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t1)) == void_type_node)
545 if (pedantic && TREE_CODE (t2) == FUNCTION_TYPE)
546 pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
548 result_type = qualify_type (t1, t2);
550 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t2)) == void_type_node)
552 if (pedantic && TREE_CODE (t1) == FUNCTION_TYPE)
553 pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
555 result_type = qualify_type (t2, t1);
558 else if (same_or_base_type_p (t2, t1))
560 else if (IS_AGGR_TYPE (TREE_TYPE (t1))
561 && IS_AGGR_TYPE (TREE_TYPE (t2))
562 && (result_type = common_base_type (TREE_TYPE (t1),
565 if (result_type == error_mark_node)
567 cp_error ("common base type of types `%T' and `%T' is ambiguous",
568 TREE_TYPE (t1), TREE_TYPE (t2));
569 result_type = ptr_type_node;
574 && result_type != TREE_TYPE (t1)
575 && result_type != TREE_TYPE (t2))
576 cp_pedwarn ("`%T' and `%T' converted to `%T *' in %s",
577 t1, t2, result_type, location);
579 result_type = build_pointer_type (result_type);
584 cp_pedwarn ("pointer type mismatch in %s", location);
585 result_type = ptr_type_node;
591 /* Return the common type of two types.
592 We assume that comptypes has already been done and returned 1;
593 if that isn't so, this may crash.
595 This is the type for the result of most arithmetic operations
596 if the operands have the given two types.
598 We do not deal with enumeral types here because they have already been
599 converted to integer types. */
605 register enum tree_code code1;
606 register enum tree_code code2;
609 /* Save time if the two types are the same. */
612 t1 = original_type (t1);
613 t2 = original_type (t2);
617 /* If one type is nonsense, use the other. */
618 if (t1 == error_mark_node)
620 if (t2 == error_mark_node)
623 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
624 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
625 return type_after_usual_arithmetic_conversions (t1, t2);
627 /* Merge the attributes. */
628 attributes = merge_machine_type_attributes (t1, t2);
630 /* Treat an enum type as the unsigned integer type of the same width. */
632 if (TREE_CODE (t1) == ENUMERAL_TYPE)
633 t1 = type_for_size (TYPE_PRECISION (t1), 1);
634 if (TREE_CODE (t2) == ENUMERAL_TYPE)
635 t2 = type_for_size (TYPE_PRECISION (t2), 1);
637 if (TYPE_PTRMEMFUNC_P (t1))
638 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
639 if (TYPE_PTRMEMFUNC_P (t2))
640 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
642 code1 = TREE_CODE (t1);
643 code2 = TREE_CODE (t2);
645 /* If one type is complex, form the common type of the non-complex
646 components, then make that complex. Use T1 or T2 if it is the
648 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
650 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
651 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
652 tree subtype = common_type (subtype1, subtype2);
654 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
655 return build_type_attribute_variant (t1, attributes);
656 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
657 return build_type_attribute_variant (t2, attributes);
659 return build_type_attribute_variant (build_complex_type (subtype),
667 /* We should have called type_after_usual_arithmetic_conversions
669 my_friendly_abort (19990725);
674 /* For two pointers, do this recursively on the target type,
675 and combine the qualifiers of the two types' targets. */
676 /* This code was turned off; I don't know why.
677 But ANSI C++ specifies doing this with the qualifiers.
678 So I turned it on again. */
680 tree tt1 = TREE_TYPE (t1);
681 tree tt2 = TREE_TYPE (t2);
686 if (TREE_CODE (tt1) == OFFSET_TYPE)
688 b1 = TYPE_OFFSET_BASETYPE (tt1);
689 b2 = TYPE_OFFSET_BASETYPE (tt2);
690 tt1 = TREE_TYPE (tt1);
691 tt2 = TREE_TYPE (tt2);
696 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
697 tt1 = TYPE_MAIN_VARIANT (tt1);
698 tt2 = TYPE_MAIN_VARIANT (tt2);
704 compiler_error ("common_type called with uncommon member types");
707 else if (tt1 == void_type_node || tt2 == void_type_node)
708 target = void_type_node;
709 else if (tt1 == unknown_type_node)
711 else if (tt2 == unknown_type_node)
714 target = common_type (tt1, tt2);
716 target = cp_build_qualified_type (target, type_quals);
720 if (same_type_p (b1, b2)
721 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
722 target = build_offset_type (b2, target);
723 else if (binfo_or_else (b2, b1))
724 target = build_offset_type (b1, target);
727 if (code1 == POINTER_TYPE)
728 t1 = build_pointer_type (target);
730 t1 = build_reference_type (target);
731 t1 = build_type_attribute_variant (t1, attributes);
733 if (TREE_CODE (target) == METHOD_TYPE)
734 t1 = build_ptrmemfunc_type (t1);
741 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
742 /* Save space: see if the result is identical to one of the args. */
743 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
744 return build_type_attribute_variant (t1, attributes);
745 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
746 return build_type_attribute_variant (t2, attributes);
747 /* Merge the element types, and have a size if either arg has one. */
748 t1 = build_cplus_array_type
749 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
750 return build_type_attribute_variant (t1, attributes);
754 /* Function types: prefer the one that specified arg types.
755 If both do, merge the arg types. Also merge the return types. */
757 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
758 tree p1 = TYPE_ARG_TYPES (t1);
759 tree p2 = TYPE_ARG_TYPES (t2);
762 /* Save space: see if the result is identical to one of the args. */
763 if (valtype == TREE_TYPE (t1) && ! p2)
764 return build_type_attribute_variant (t1, attributes);
765 if (valtype == TREE_TYPE (t2) && ! p1)
766 return build_type_attribute_variant (t2, attributes);
768 /* Simple way if one arg fails to specify argument types. */
769 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
771 rval = build_function_type (valtype, p2);
772 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
773 rval = build_exception_variant (rval, raises);
774 return build_type_attribute_variant (rval, attributes);
776 raises = TYPE_RAISES_EXCEPTIONS (t1);
777 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
779 rval = build_function_type (valtype, p1);
781 rval = build_exception_variant (rval, raises);
782 return build_type_attribute_variant (rval, attributes);
785 rval = build_function_type (valtype, commonparms (p1, p2));
786 rval = build_exception_variant (rval, raises);
787 return build_type_attribute_variant (rval, attributes);
792 t1 = TYPE_MAIN_VARIANT (t1);
793 t2 = TYPE_MAIN_VARIANT (t2);
795 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
796 return build_type_attribute_variant (t1, attributes);
797 else if (binfo_or_else (t2, t1))
798 return build_type_attribute_variant (t2, attributes);
801 compiler_error ("common_type called with uncommon aggregate types");
802 return error_mark_node;
806 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
808 /* Get this value the long way, since TYPE_METHOD_BASETYPE
809 is just the main variant of this. */
813 tree b1 = TYPE_OFFSET_BASETYPE (t1);
814 tree b2 = TYPE_OFFSET_BASETYPE (t2);
816 if (same_type_p (b1, b2)
817 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
818 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
821 if (binfo_or_else (b2, b1) == NULL_TREE)
822 compiler_error ("common_type called with uncommon method types");
823 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
826 raises = TYPE_RAISES_EXCEPTIONS (t1);
828 /* If this was a member function type, get back to the
829 original type of type member function (i.e., without
830 the class instance variable up front. */
831 t1 = build_function_type (TREE_TYPE (t1),
832 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
833 t2 = build_function_type (TREE_TYPE (t2),
834 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
835 t3 = common_type (t1, t2);
836 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
837 TYPE_ARG_TYPES (t3));
838 t1 = build_exception_variant (t3, raises);
841 compiler_error ("common_type called with uncommon method types");
843 return build_type_attribute_variant (t1, attributes);
846 /* Pointers to members should now be handled by the POINTER_TYPE
848 my_friendly_abort (990325);
851 return build_type_attribute_variant (t1, attributes);
855 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
858 compexcepttypes (t1, t2)
861 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
864 /* Compare the array types T1 and T2, using CMP as the type comparison
865 function for the element types. STRICT is as for comptypes. */
868 comp_array_types (cmp, t1, t2, strict)
869 register int (*cmp) PROTO((tree, tree, int));
879 /* The type of the array elements must be the same. */
880 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
881 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
882 strict & ~COMPARE_REDECLARATION)))
885 d1 = TYPE_DOMAIN (t1);
886 d2 = TYPE_DOMAIN (t2);
891 /* If one of the arrays is dimensionless, and the other has a
892 dimension, they are of different types. However, it is legal to
900 declarations for an array object can specify
901 array types that differ by the presence or absence of a major
902 array bound (_dcl.array_). */
904 return strict & COMPARE_REDECLARATION;
906 /* Check that the dimensions are the same. */
907 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
909 && cp_tree_equal (TYPE_MAX_VALUE (d1),
910 TYPE_MAX_VALUE (d2)));
913 /* Return 1 if T1 and T2 are compatible types for assignment or
914 various other operations. STRICT is a bitwise-or of the COMPARE_*
918 comptypes (t1, t2, strict)
924 int orig_strict = strict;
926 /* The special exemption for redeclaring array types without an
927 array bound only applies at the top level:
932 is not legal, for example. */
933 strict &= ~COMPARE_REDECLARATION;
935 /* Suppress errors caused by previously reported errors */
939 /* This should never happen. */
940 my_friendly_assert (t1 != error_mark_node, 307);
942 if (t2 == error_mark_node)
945 if (strict & COMPARE_RELAXED)
947 /* Treat an enum type as the unsigned integer type of the same width. */
949 if (TREE_CODE (t1) == ENUMERAL_TYPE)
950 t1 = type_for_size (TYPE_PRECISION (t1), 1);
951 if (TREE_CODE (t2) == ENUMERAL_TYPE)
952 t2 = type_for_size (TYPE_PRECISION (t2), 1);
958 if (TYPE_PTRMEMFUNC_P (t1))
959 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
960 if (TYPE_PTRMEMFUNC_P (t2))
961 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
963 /* Different classes of types can't be compatible. */
964 if (TREE_CODE (t1) != TREE_CODE (t2))
967 /* Qualifiers must match. */
968 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
970 if (strict == COMPARE_STRICT
971 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
974 /* Allow for two different type nodes which have essentially the same
975 definition. Note that we already checked for equality of the type
976 qualifiers (just above). */
978 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
981 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
982 attribute is its own main variant (`val' will remain 0). */
983 #ifndef COMP_TYPE_ATTRIBUTES
984 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
987 if (strict & COMPARE_NO_ATTRIBUTES)
989 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
990 else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
993 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
996 switch (TREE_CODE (t1))
998 case TEMPLATE_TEMPLATE_PARM:
999 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1000 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1002 if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1)),
1003 DECL_TEMPLATE_PARMS (TYPE_NAME (t2))))
1005 if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1)
1006 && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2))
1008 /* Don't check inheritance. */
1009 strict = COMPARE_STRICT;
1014 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1015 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1016 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
1017 val = comp_template_args (TYPE_TI_ARGS (t1),
1020 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1022 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1027 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1028 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1029 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1033 if (! compexcepttypes (t1, t2))
1036 /* This case is anti-symmetrical!
1037 One can pass a base member (or member function)
1038 to something expecting a derived member (or member function),
1039 but not vice-versa! */
1041 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1042 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1046 case REFERENCE_TYPE:
1047 t1 = TREE_TYPE (t1);
1048 t2 = TREE_TYPE (t2);
1049 /* first, check whether the referred types match with the
1050 required level of strictness */
1051 val = comptypes (t1, t2, strict);
1054 if (TREE_CODE (t1) == RECORD_TYPE
1055 && TREE_CODE (t2) == RECORD_TYPE)
1060 if (! compexcepttypes (t1, t2))
1063 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1064 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1065 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1069 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1070 here since this is the one place where
1071 COMPARE_REDECLARATION should be used. */
1072 val = comp_array_types (comptypes, t1, t2, orig_strict);
1075 case TEMPLATE_TYPE_PARM:
1076 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1077 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1080 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
1082 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1085 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1090 return attrval == 2 && val == 1 ? 2 : val;
1093 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1094 only in the same direction as the target type. */
1097 comp_cv_target_types (ttl, ttr, nptrs)
1103 if (!at_least_as_qualified_p (ttl, ttr)
1104 && !at_least_as_qualified_p (ttr, ttl))
1105 /* The qualifications are incomparable. */
1108 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1109 return more_qualified_p (ttr, ttl) ? -1 : 1;
1111 t = comp_target_types (ttl, ttr, nptrs);
1112 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1113 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1119 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1120 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1121 converted to TTL. Return -1 means that TTL can be converted to TTR but
1124 NPTRS is the number of pointers we can strip off and keep cool.
1125 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1126 but to not permit B** to convert to A**.
1128 This should go away. Callers should use can_convert or something
1129 similar instead. (jason 17 Apr 1997) */
1132 comp_target_types (ttl, ttr, nptrs)
1136 ttl = TYPE_MAIN_VARIANT (ttl);
1137 ttr = TYPE_MAIN_VARIANT (ttr);
1138 if (same_type_p (ttl, ttr))
1141 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1144 if ((TREE_CODE (ttr) == POINTER_TYPE
1145 || TREE_CODE (ttr) == REFERENCE_TYPE)
1146 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1147 of the type pointed to. This is necessary for reference init
1148 semantics. We won't get here from a previous call with nptrs == 1;
1149 for multi-level pointers we end up in comp_ptr_ttypes. */
1152 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1154 ttl = TREE_TYPE (ttl);
1155 ttr = TREE_TYPE (ttr);
1159 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1160 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1162 else if (TREE_CODE (ttl) == VOID_TYPE
1163 && TREE_CODE (ttr) != FUNCTION_TYPE
1164 && TREE_CODE (ttr) != METHOD_TYPE
1165 && TREE_CODE (ttr) != OFFSET_TYPE)
1167 else if (TREE_CODE (ttr) == VOID_TYPE
1168 && TREE_CODE (ttl) != FUNCTION_TYPE
1169 && TREE_CODE (ttl) != METHOD_TYPE
1170 && TREE_CODE (ttl) != OFFSET_TYPE)
1172 else if (TREE_CODE (ttl) == POINTER_TYPE
1173 || TREE_CODE (ttl) == ARRAY_TYPE)
1175 if (comp_ptr_ttypes (ttl, ttr))
1177 else if (comp_ptr_ttypes (ttr, ttl))
1183 /* Const and volatile mean something different for function types,
1184 so the usual checks are not appropriate. */
1185 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1186 return comp_target_types (ttl, ttr, nptrs - 1);
1188 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1191 if (TREE_CODE (ttr) == ARRAY_TYPE)
1192 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1193 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1200 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1205 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1214 argsl = TYPE_ARG_TYPES (ttl);
1215 argsr = TYPE_ARG_TYPES (ttr);
1217 /* Compare 'this' here, not in comp_target_parms. */
1218 if (TREE_CODE (ttr) == METHOD_TYPE)
1220 tree tl = TYPE_METHOD_BASETYPE (ttl);
1221 tree tr = TYPE_METHOD_BASETYPE (ttr);
1223 if (!same_or_base_type_p (tr, tl))
1225 if (same_or_base_type_p (tl, tr))
1231 argsl = TREE_CHAIN (argsl);
1232 argsr = TREE_CHAIN (argsr);
1235 switch (comp_target_parms (argsl, argsr, 1))
1243 return saw_contra ? -1 : 1;
1246 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1250 /* Contravariance: we can assign a pointer to base member to a pointer
1251 to derived member. Note difference from simple pointer case, where
1252 we can pass a pointer to derived to a pointer to base. */
1253 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1254 TYPE_OFFSET_BASETYPE (ttl)))
1256 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1257 TYPE_OFFSET_BASETYPE (ttr)))
1267 ttl = TREE_TYPE (ttl);
1268 ttr = TREE_TYPE (ttr);
1270 if (TREE_CODE (ttl) == POINTER_TYPE
1271 || TREE_CODE (ttl) == ARRAY_TYPE)
1273 if (comp_ptr_ttypes (ttl, ttr))
1279 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1284 else if (IS_AGGR_TYPE (ttl))
1288 if (same_or_base_type_p (build_pointer_type (ttl),
1289 build_pointer_type (ttr)))
1291 if (same_or_base_type_p (build_pointer_type (ttr),
1292 build_pointer_type (ttl)))
1300 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1303 at_least_as_qualified_p (type1, type2)
1307 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1308 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1309 == CP_TYPE_QUALS (type2));
1312 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1315 more_qualified_p (type1, type2)
1319 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1320 && at_least_as_qualified_p (type1, type2));
1323 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1324 more cv-qualified that TYPE1, and 0 otherwise. */
1327 comp_cv_qualification (type1, type2)
1331 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1334 if (at_least_as_qualified_p (type1, type2))
1337 else if (at_least_as_qualified_p (type2, type1))
1343 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1344 subset of the cv-qualification signature of TYPE2, and the types
1345 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1348 comp_cv_qual_signature (type1, type2)
1352 if (comp_ptr_ttypes_real (type2, type1, -1))
1354 else if (comp_ptr_ttypes_real (type1, type2, -1))
1360 /* If two types share a common base type, return that basetype.
1361 If there is not a unique most-derived base type, this function
1362 returns ERROR_MARK_NODE. */
1365 common_base_type (tt1, tt2)
1368 tree best = NULL_TREE;
1371 /* If one is a baseclass of another, that's good enough. */
1372 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1374 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1377 /* Otherwise, try to find a unique baseclass of TT1
1378 that is shared by TT2, and follow that down. */
1379 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1381 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1382 tree trial = common_base_type (basetype, tt2);
1385 if (trial == error_mark_node)
1387 if (best == NULL_TREE)
1389 else if (best != trial)
1390 return error_mark_node;
1395 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1397 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1398 tree trial = common_base_type (tt1, basetype);
1401 if (trial == error_mark_node)
1403 if (best == NULL_TREE)
1405 else if (best != trial)
1406 return error_mark_node;
1412 /* Subroutines of `comptypes'. */
1414 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1415 equivalent in the sense that functions with those parameter types
1416 can have equivalent types. The two lists must be equivalent,
1419 C++: See comment above about TYPE1, TYPE2. */
1422 compparms (parms1, parms2)
1423 tree parms1, parms2;
1425 register tree t1 = parms1, t2 = parms2;
1427 /* An unspecified parmlist matches any specified parmlist
1428 whose argument types don't need default promotions. */
1432 if (t1 == 0 && t2 == 0)
1434 /* If one parmlist is shorter than the other,
1435 they fail to match. */
1436 if (t1 == 0 || t2 == 0)
1438 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1441 t1 = TREE_CHAIN (t1);
1442 t2 = TREE_CHAIN (t2);
1446 /* This really wants return whether or not parameter type lists
1447 would make their owning functions assignment compatible or not.
1449 The return value is like for comp_target_types.
1451 This should go away, possibly with the exception of the empty parmlist
1452 conversion; there are no conversions between function types in C++.
1453 (jason 17 Apr 1997) */
1456 comp_target_parms (parms1, parms2, strict)
1457 tree parms1, parms2;
1460 register tree t1 = parms1, t2 = parms2;
1461 int warn_contravariance = 0;
1463 /* In C, an unspecified parmlist matches any specified parmlist
1464 whose argument types don't need default promotions. This is not
1465 true for C++, but let's do it anyway for unfixed headers. */
1467 if (t1 == 0 && t2 != 0)
1469 if (! flag_strict_prototype && t2 == void_list_node)
1470 /* t1 might be the arglist of a function pointer in extern "C"
1471 declared to take (), which we fudged to (...). Don't make the
1472 user pay for our mistake. */;
1474 cp_pedwarn ("ANSI C++ prohibits conversion from `%#T' to `(...)'",
1476 return self_promoting_args_p (t2);
1479 return self_promoting_args_p (t1);
1481 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1485 /* If one parmlist is shorter than the other,
1486 they fail to match, unless STRICT is <= 0. */
1487 if (t1 == 0 || t2 == 0)
1492 return 1 + warn_contravariance;
1493 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1495 p1 = TREE_VALUE (t1);
1496 p2 = TREE_VALUE (t2);
1497 if (same_type_p (p1, p2))
1503 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1504 || (TREE_CODE (p1) == REFERENCE_TYPE
1505 && TREE_CODE (p2) == REFERENCE_TYPE))
1508 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1509 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1512 /* The following is wrong for contravariance,
1513 but many programs depend on it. */
1514 if (TREE_TYPE (p1) == void_type_node)
1516 if (TREE_TYPE (p2) == void_type_node)
1518 warn_contravariance = 1;
1521 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1522 && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (p1)),
1523 TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1526 /* Note backwards order due to contravariance. */
1527 if (comp_target_types (p2, p1, 1) <= 0)
1529 if (comp_target_types (p1, p2, 1) > 0)
1531 warn_contravariance = 1;
1538 return warn_contravariance ? -1 : 1;
1541 /* Return 1 if PARMS specifies a fixed number of parameters
1542 and none of their types is affected by default promotions. */
1545 self_promoting_args_p (parms)
1549 for (t = parms; t; t = TREE_CHAIN (t))
1551 register tree type = TREE_VALUE (t);
1553 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1559 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1562 if (C_PROMOTING_INTEGER_TYPE_P (type))
1568 /* Compute the value of the `sizeof' operator. */
1574 enum tree_code code = TREE_CODE (type);
1577 if (processing_template_decl)
1578 return build_min (SIZEOF_EXPR, sizetype, type);
1580 if (code == FUNCTION_TYPE)
1582 if (pedantic || warn_pointer_arith)
1583 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1584 return size_int (1);
1586 if (code == METHOD_TYPE)
1588 if (pedantic || warn_pointer_arith)
1589 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1590 return size_int (1);
1592 if (code == VOID_TYPE)
1594 if (pedantic || warn_pointer_arith)
1595 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1596 return size_int (1);
1598 if (code == ERROR_MARK)
1599 return size_int (1);
1601 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1602 referenced object.'' */
1603 if (code == REFERENCE_TYPE)
1604 type = TREE_TYPE (type);
1606 /* @@ This also produces an error for a signature ref.
1607 In that case we should be able to do better. */
1608 if (IS_SIGNATURE (type))
1610 error ("`sizeof' applied to a signature type");
1611 return size_int (0);
1613 else if (code == OFFSET_TYPE)
1615 cp_error ("`sizeof' applied to non-static member");
1616 return size_int (0);
1619 if (TYPE_SIZE (complete_type (type)) == 0)
1621 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1622 return size_int (0);
1625 /* Convert in case a char is more than one unit. */
1626 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1627 size_int (TYPE_PRECISION (char_type_node)));
1628 t = convert (sizetype, t);
1629 /* size_binop does not put the constant in range, so do it now. */
1630 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1631 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1639 if (processing_template_decl)
1640 return build_min (SIZEOF_EXPR, sizetype, e);
1642 if (TREE_CODE (e) == COMPONENT_REF
1643 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1644 error ("sizeof applied to a bit-field");
1645 /* ANSI says arrays and functions are converted inside comma.
1646 But we can't really convert them in build_compound_expr
1647 because that would break commas in lvalues.
1648 So do the conversion here if operand was a comma. */
1649 if (TREE_CODE (e) == COMPOUND_EXPR
1650 && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1651 || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1652 e = default_conversion (e);
1653 else if (is_overloaded_fn (e))
1655 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1656 return size_int (1);
1658 else if (type_unknown_p (e))
1660 incomplete_type_error (e, TREE_TYPE (e));
1661 return size_int (1);
1663 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1664 member unless you're in a non-static member of X. But, we used
1665 to support this usage, so we still permit it unless we're being
1667 else if (TREE_CODE (e) == OFFSET_REF)
1668 e = resolve_offset_ref (e);
1670 if (e == error_mark_node)
1673 return c_sizeof (TREE_TYPE (e));
1677 c_sizeof_nowarn (type)
1680 enum tree_code code = TREE_CODE (type);
1683 if (code == FUNCTION_TYPE
1684 || code == METHOD_TYPE
1685 || code == VOID_TYPE
1686 || code == ERROR_MARK)
1687 return size_int (1);
1688 if (code == REFERENCE_TYPE)
1689 type = TREE_TYPE (type);
1691 if (TYPE_SIZE (type) == 0)
1692 return size_int (0);
1694 /* Convert in case a char is more than one unit. */
1695 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1696 size_int (TYPE_PRECISION (char_type_node)));
1697 t = convert (sizetype, t);
1698 force_fit_type (t, 0);
1702 /* Implement the __alignof keyword: Return the minimum required
1703 alignment of TYPE, measured in bytes. */
1709 enum tree_code code = TREE_CODE (type);
1712 if (processing_template_decl)
1713 return build_min (ALIGNOF_EXPR, sizetype, type);
1715 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1716 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1718 if (code == VOID_TYPE || code == ERROR_MARK)
1719 return size_int (1);
1721 /* C++: this is really correct! */
1722 if (code == REFERENCE_TYPE)
1723 type = TREE_TYPE (type);
1725 /* @@ This also produces an error for a signature ref.
1726 In that case we should be able to do better. */
1727 if (IS_SIGNATURE (type))
1729 error ("`__alignof' applied to a signature type");
1730 return size_int (1);
1733 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1734 force_fit_type (t, 0);
1738 /* Perform the array-to-pointer and function-to-pointer conversions
1741 In addition, references are converted to rvalues and manifest
1742 constants are replaced by their values. */
1745 decay_conversion (exp)
1749 register enum tree_code code;
1751 if (TREE_CODE (exp) == OFFSET_REF)
1752 exp = resolve_offset_ref (exp);
1754 type = TREE_TYPE (exp);
1755 code = TREE_CODE (type);
1757 if (code == REFERENCE_TYPE)
1759 exp = convert_from_reference (exp);
1760 type = TREE_TYPE (exp);
1761 code = TREE_CODE (type);
1764 /* Constants can be used directly unless they're not loadable. */
1765 if (TREE_CODE (exp) == CONST_DECL)
1766 exp = DECL_INITIAL (exp);
1767 /* Replace a nonvolatile const static variable with its value. We
1768 don't do this for arrays, though; we want the address of the
1769 first element of the array, not the address of the first element
1770 of its initializing constant. We *do* replace variables that the
1771 user isn't really supposed to know about; this is a hack to deal
1772 with __PRETTY_FUNCTION__ and the like. */
1773 else if (TREE_READONLY_DECL_P (exp)
1774 && (code != ARRAY_TYPE
1775 || (TREE_CODE (exp) == VAR_DECL && DECL_IGNORED_P (exp))))
1777 exp = decl_constant_value (exp);
1778 type = TREE_TYPE (exp);
1781 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1782 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1784 if (code == VOID_TYPE)
1786 error ("void value not ignored as it ought to be");
1787 return error_mark_node;
1789 if (code == METHOD_TYPE)
1790 my_friendly_abort (990506);
1791 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1792 return build_unary_op (ADDR_EXPR, exp, 0);
1793 if (code == ARRAY_TYPE)
1798 if (TREE_CODE (exp) == INDIRECT_REF)
1800 /* Stripping away the INDIRECT_REF is not the right
1801 thing to do for references... */
1802 tree inner = TREE_OPERAND (exp, 0);
1803 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1805 inner = build1 (CONVERT_EXPR,
1806 build_pointer_type (TREE_TYPE
1807 (TREE_TYPE (inner))),
1809 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1811 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1814 if (TREE_CODE (exp) == COMPOUND_EXPR)
1816 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1817 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1818 TREE_OPERAND (exp, 0), op1);
1822 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1824 error ("invalid use of non-lvalue array");
1825 return error_mark_node;
1828 ptrtype = build_pointer_type (TREE_TYPE (type));
1830 if (TREE_CODE (exp) == VAR_DECL)
1832 /* ??? This is not really quite correct
1833 in that the type of the operand of ADDR_EXPR
1834 is not the target type of the type of the ADDR_EXPR itself.
1835 Question is, can this lossage be avoided? */
1836 adr = build1 (ADDR_EXPR, ptrtype, exp);
1837 if (mark_addressable (exp) == 0)
1838 return error_mark_node;
1839 TREE_CONSTANT (adr) = staticp (exp);
1840 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1843 /* This way is better for a COMPONENT_REF since it can
1844 simplify the offset for a component. */
1845 adr = build_unary_op (ADDR_EXPR, exp, 1);
1846 return cp_convert (ptrtype, adr);
1853 default_conversion (exp)
1857 enum tree_code code;
1859 exp = decay_conversion (exp);
1861 type = TREE_TYPE (exp);
1862 code = TREE_CODE (type);
1864 if (INTEGRAL_CODE_P (code))
1866 tree t = type_promotes_to (type);
1868 return cp_convert (t, exp);
1874 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1878 inline_conversion (exp)
1881 if (TREE_CODE (exp) == FUNCTION_DECL)
1882 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1887 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1888 decay_conversion to one. */
1891 string_conv_p (totype, exp, warn)
1897 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1900 t = TREE_TYPE (totype);
1901 if (!same_type_p (t, char_type_node)
1902 && !same_type_p (t, wchar_type_node))
1905 if (TREE_CODE (exp) == STRING_CST)
1907 /* Make sure that we don't try to convert between char and wchar_t. */
1908 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1913 /* Is this a string constant which has decayed to 'const char *'? */
1914 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1915 if (!same_type_p (TREE_TYPE (exp), t))
1918 if (TREE_CODE (exp) != ADDR_EXPR
1919 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1923 /* This warning is not very useful, as it complains about printf. */
1924 if (warn && warn_write_strings)
1925 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1931 build_object_ref (datum, basetype, field)
1932 tree datum, basetype, field;
1935 if (datum == error_mark_node)
1936 return error_mark_node;
1938 dtype = TREE_TYPE (datum);
1939 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1940 dtype = TREE_TYPE (dtype);
1941 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1943 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1944 basetype, field, dtype);
1945 return error_mark_node;
1947 else if (IS_SIGNATURE (basetype))
1949 warning ("signature name in scope resolution ignored");
1950 return build_component_ref (datum, field, NULL_TREE, 1);
1952 else if (is_aggr_type (basetype, 1))
1954 tree binfo = binfo_or_else (basetype, dtype);
1956 return build_x_component_ref (build_scoped_ref (datum, basetype),
1959 return error_mark_node;
1962 /* Like `build_component_ref, but uses an already found field, and converts
1963 from a reference. Must compute access for current_class_ref.
1967 build_component_ref_1 (datum, field, protect)
1971 return convert_from_reference
1972 (build_component_ref (datum, field, NULL_TREE, protect));
1975 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1976 can, for example, use as an lvalue. This code used to be in
1977 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1978 expressions, where we're dealing with aggregates. But now it's again only
1979 called from unary_complex_lvalue. The case (in particular) that led to
1980 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1984 rationalize_conditional_expr (code, t)
1985 enum tree_code code;
1988 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1989 the first operand is always the one to be used if both operands
1990 are equal, so we know what conditional expression this used to be. */
1991 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1994 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1995 ? LE_EXPR : GE_EXPR),
1996 TREE_OPERAND (t, 0),
1997 TREE_OPERAND (t, 1)),
1998 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1999 build_unary_op (code, TREE_OPERAND (t, 1), 0));
2003 build_conditional_expr (TREE_OPERAND (t, 0),
2004 build_unary_op (code, TREE_OPERAND (t, 1), 0),
2005 build_unary_op (code, TREE_OPERAND (t, 2), 0));
2008 /* Given the TYPE of an anonymous union field inside T, return the
2009 FIELD_DECL for the field. If not found return NULL_TREE. Because
2010 anonymous unions can nest, we must also search all anonymous unions
2011 that are directly reachable. */
2014 lookup_anon_field (t, type)
2019 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2021 if (TREE_STATIC (field))
2023 if (TREE_CODE (field) != FIELD_DECL)
2026 /* If we find it directly, return the field. */
2027 if (DECL_NAME (field) == NULL_TREE
2028 && type == TREE_TYPE (field))
2033 /* Otherwise, it could be nested, search harder. */
2034 if (DECL_NAME (field) == NULL_TREE
2035 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2037 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2045 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2046 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2047 that we are interested in, or it can be a FIELD_DECL. */
2050 build_component_ref (datum, component, basetype_path, protect)
2051 tree datum, component, basetype_path;
2054 register tree basetype;
2055 register enum tree_code code;
2056 register tree field = NULL;
2061 if (processing_template_decl)
2062 return build_min_nt (COMPONENT_REF, datum, component);
2064 if (datum == error_mark_node
2065 || TREE_TYPE (datum) == error_mark_node)
2066 return error_mark_node;
2068 /* BASETYPE holds the type of the class containing the COMPONENT. */
2069 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2071 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2073 switch (TREE_CODE (datum))
2077 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2078 basetype_path, protect);
2079 return build (COMPOUND_EXPR, TREE_TYPE (value),
2080 TREE_OPERAND (datum, 0), value);
2083 return build_conditional_expr
2084 (TREE_OPERAND (datum, 0),
2085 build_component_ref (TREE_OPERAND (datum, 1), component,
2086 basetype_path, protect),
2087 build_component_ref (TREE_OPERAND (datum, 2), component,
2088 basetype_path, protect));
2091 cp_error ("invalid use of %D", datum);
2092 datum = error_mark_node;
2099 code = TREE_CODE (basetype);
2101 if (code == REFERENCE_TYPE)
2103 datum = convert_from_reference (datum);
2104 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2105 code = TREE_CODE (basetype);
2107 if (TREE_CODE (datum) == OFFSET_REF)
2109 datum = resolve_offset_ref (datum);
2110 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2111 code = TREE_CODE (basetype);
2114 /* First, see if there is a field or component with name COMPONENT. */
2115 if (TREE_CODE (component) == TREE_LIST)
2117 /* I could not trigger this code. MvL */
2118 my_friendly_abort (980326);
2120 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2121 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2123 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2126 if (! IS_AGGR_TYPE_CODE (code))
2128 if (code != ERROR_MARK)
2129 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2130 component, datum, basetype);
2131 return error_mark_node;
2134 if (!complete_type_or_else (basetype, datum))
2135 return error_mark_node;
2137 if (TREE_CODE (component) == BIT_NOT_EXPR)
2139 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2141 cp_error ("destructor specifier `%T::~%T' must have matching names",
2142 basetype, TREE_OPERAND (component, 0));
2143 return error_mark_node;
2145 if (! TYPE_HAS_DESTRUCTOR (basetype))
2147 cp_error ("type `%T' has no destructor", basetype);
2148 return error_mark_node;
2150 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2153 /* Look up component name in the structure type definition. */
2154 if (CLASSTYPE_VFIELD (basetype)
2155 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
2156 /* Special-case this because if we use normal lookups in an ambiguous
2157 hierarchy, the compiler will abort (because vptr lookups are
2158 not supposed to be ambiguous. */
2159 field = CLASSTYPE_VFIELD (basetype);
2160 else if (TREE_CODE (component) == FIELD_DECL)
2162 else if (TREE_CODE (component) == TYPE_DECL)
2164 cp_error ("invalid use of type decl `%#D' as expression", component);
2165 return error_mark_node;
2167 else if (TREE_CODE (component) == TEMPLATE_DECL)
2169 cp_error ("invalid use of template `%#D' as expression", component);
2170 return error_mark_node;
2174 tree name = component;
2175 if (TREE_CODE (component) == VAR_DECL)
2176 name = DECL_NAME (component);
2177 if (basetype_path == NULL_TREE)
2178 basetype_path = TYPE_BINFO (basetype);
2179 field = lookup_field (basetype_path, name,
2180 protect && !VFIELD_NAME_P (name), 0);
2181 if (field == error_mark_node)
2182 return error_mark_node;
2184 if (field == NULL_TREE)
2186 /* Not found as a data field, look for it as a method. If found,
2187 then if this is the only possible one, return it, else
2188 report ambiguity error. */
2189 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2190 if (fndecls == error_mark_node)
2191 return error_mark_node;
2194 /* If the function is unique and static, we can resolve it
2195 now. Otherwise, we have to wait and see what context it is
2196 used in; a component_ref involving a non-static member
2197 function can only be used in a call (expr.ref). */
2199 if (TREE_CHAIN (fndecls) == NULL_TREE
2200 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2202 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2204 tree fndecl = TREE_VALUE (fndecls);
2205 enforce_access (TREE_PURPOSE (fndecls), fndecl);
2211 /* A unique non-static member function. Other parts
2212 of the compiler expect something with
2213 unknown_type_node to be really overloaded, so
2215 TREE_VALUE (fndecls)
2216 = scratch_ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2220 ref = build (COMPONENT_REF, unknown_type_node,
2221 datum, TREE_VALUE (fndecls));
2225 cp_error ("`%#T' has no member named `%D'", basetype, name);
2226 return error_mark_node;
2228 else if (TREE_TYPE (field) == error_mark_node)
2229 return error_mark_node;
2231 if (TREE_CODE (field) != FIELD_DECL)
2233 if (TREE_CODE (field) == TYPE_DECL)
2234 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2235 else if (DECL_RTL (field) != 0)
2238 TREE_USED (field) = 1;
2243 /* See if we have to do any conversions so that we pick up the field from the
2245 if (DECL_FIELD_CONTEXT (field) != basetype)
2247 tree context = DECL_FIELD_CONTEXT (field);
2248 tree base = context;
2249 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2250 && ANON_AGGR_TYPE_P (base))
2252 base = TYPE_CONTEXT (base);
2255 /* Handle base classes here... */
2256 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2258 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2259 if (integer_zerop (addr))
2261 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2262 return error_mark_node;
2264 if (VBASE_NAME_P (DECL_NAME (field)))
2266 /* It doesn't matter which vbase pointer we grab, just
2267 find one of them. */
2268 tree binfo = get_binfo (base,
2269 TREE_TYPE (TREE_TYPE (addr)), 0);
2270 addr = convert_pointer_to_real (binfo, addr);
2273 addr = convert_pointer_to (base, addr);
2274 datum = build_indirect_ref (addr, NULL_PTR);
2275 my_friendly_assert (datum != error_mark_node, 311);
2279 /* Handle things from anon unions here... */
2280 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2282 tree subfield = lookup_anon_field (basetype, context);
2283 tree subdatum = build_component_ref (datum, subfield,
2284 basetype_path, protect);
2285 return build_component_ref (subdatum, field, basetype_path, protect);
2289 /* Compute the type of the field, as described in [expr.ref]. */
2290 type_quals = TYPE_UNQUALIFIED;
2291 field_type = TREE_TYPE (field);
2292 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2293 /* The standard says that the type of the result should be the
2294 type referred to by the reference. But for now, at least, we
2295 do the conversion from reference type later. */
2299 type_quals = (CP_TYPE_QUALS (field_type)
2300 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2302 /* A field is const (volatile) if the enclosing object, or the
2303 field itself, is const (volatile). But, a mutable field is
2304 not const, even within a const object. */
2305 if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2306 type_quals &= ~TYPE_QUAL_CONST;
2307 if (!IS_SIGNATURE (field_type))
2308 field_type = cp_build_qualified_type (field_type, type_quals);
2311 ref = fold (build (COMPONENT_REF, field_type,
2312 break_out_cleanups (datum), field));
2314 /* Mark the expression const or volatile, as appropriate. Even
2315 though we've dealt with the type above, we still have to mark the
2316 expression itself. */
2317 if (type_quals & TYPE_QUAL_CONST)
2318 TREE_READONLY (ref) = 1;
2319 else if (type_quals & TYPE_QUAL_VOLATILE)
2320 TREE_THIS_VOLATILE (ref) = 1;
2325 /* Variant of build_component_ref for use in expressions, which should
2326 never have REFERENCE_TYPE. */
2329 build_x_component_ref (datum, component, basetype_path, protect)
2330 tree datum, component, basetype_path;
2333 tree t = build_component_ref (datum, component, basetype_path, protect);
2335 if (! processing_template_decl)
2336 t = convert_from_reference (t);
2341 /* Given an expression PTR for a pointer, return an expression
2342 for the value pointed to.
2343 ERRORSTRING is the name of the operator to appear in error messages.
2345 This function may need to overload OPERATOR_FNNAME.
2346 Must also handle REFERENCE_TYPEs for C++. */
2349 build_x_indirect_ref (ptr, errorstring)
2351 const char *errorstring;
2355 if (processing_template_decl)
2356 return build_min_nt (INDIRECT_REF, ptr);
2358 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2362 return build_indirect_ref (ptr, errorstring);
2366 build_indirect_ref (ptr, errorstring)
2368 const char *errorstring;
2370 register tree pointer, type;
2372 if (ptr == error_mark_node)
2373 return error_mark_node;
2375 if (ptr == current_class_ptr)
2376 return current_class_ref;
2378 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2379 ? ptr : default_conversion (ptr));
2380 type = TREE_TYPE (pointer);
2382 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2386 If the type of the expression is "pointer to T," the type
2387 of the result is "T."
2389 We must use the canonical variant because certain parts of
2390 the back end, like fold, do pointer comparisons between
2392 tree t = canonical_type_variant (TREE_TYPE (type));
2394 if (TREE_CODE (pointer) == ADDR_EXPR
2396 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2397 /* The POINTER was something like `&x'. We simplify `*&x' to
2399 return TREE_OPERAND (pointer, 0);
2402 tree ref = build1 (INDIRECT_REF, t, pointer);
2404 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2405 so that we get the proper error message if the result is used
2406 to assign to. Also, &* is supposed to be a no-op. */
2407 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2408 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2409 TREE_SIDE_EFFECTS (ref)
2410 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2415 /* `pointer' won't be an error_mark_node if we were given a
2416 pointer to member, so it's cool to check for this here. */
2417 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2418 error ("invalid use of `%s' on pointer to member", errorstring);
2419 else if (TREE_CODE (type) == RECORD_TYPE
2420 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2421 error ("cannot dereference signature pointer/reference");
2422 else if (pointer != error_mark_node)
2425 error ("invalid type argument of `%s'", errorstring);
2427 error ("invalid type argument");
2429 return error_mark_node;
2432 /* This handles expressions of the form "a[i]", which denotes
2435 This is logically equivalent in C to *(a+i), but we may do it differently.
2436 If A is a variable or a member, we generate a primitive ARRAY_REF.
2437 This avoids forcing the array out of registers, and can work on
2438 arrays that are not lvalues (for example, members of structures returned
2441 If INDEX is of some user-defined type, it must be converted to
2442 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2443 will inherit the type of the array, which will be some pointer type. */
2446 build_array_ref (array, idx)
2451 error ("subscript missing in array reference");
2452 return error_mark_node;
2455 if (TREE_TYPE (array) == error_mark_node
2456 || TREE_TYPE (idx) == error_mark_node)
2457 return error_mark_node;
2459 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2460 && TREE_CODE (array) != INDIRECT_REF)
2464 /* Subscripting with type char is likely to lose
2465 on a machine where chars are signed.
2466 So warn on any machine, but optionally.
2467 Don't warn for unsigned char since that type is safe.
2468 Don't warn for signed char because anyone who uses that
2469 must have done so deliberately. */
2470 if (warn_char_subscripts
2471 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2472 warning ("array subscript has type `char'");
2474 /* Apply default promotions *after* noticing character types. */
2475 idx = default_conversion (idx);
2477 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2479 error ("array subscript is not an integer");
2480 return error_mark_node;
2483 /* An array that is indexed by a non-constant
2484 cannot be stored in a register; we must be able to do
2485 address arithmetic on its address.
2486 Likewise an array of elements of variable size. */
2487 if (TREE_CODE (idx) != INTEGER_CST
2488 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2489 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2492 if (mark_addressable (array) == 0)
2493 return error_mark_node;
2495 /* An array that is indexed by a constant value which is not within
2496 the array bounds cannot be stored in a register either; because we
2497 would get a crash in store_bit_field/extract_bit_field when trying
2498 to access a non-existent part of the register. */
2499 if (TREE_CODE (idx) == INTEGER_CST
2500 && TYPE_VALUES (TREE_TYPE (array))
2501 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2503 if (mark_addressable (array) == 0)
2504 return error_mark_node;
2507 if (pedantic && !lvalue_p (array))
2508 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2510 /* Note in C++ it is valid to subscript a `register' array, since
2511 it is valid to take the address of something with that
2512 storage specification. */
2516 while (TREE_CODE (foo) == COMPONENT_REF)
2517 foo = TREE_OPERAND (foo, 0);
2518 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2519 warning ("subscripting array declared `register'");
2522 type = TREE_TYPE (TREE_TYPE (array));
2523 rval = build (ARRAY_REF, type, array, idx);
2524 /* Array ref is const/volatile if the array elements are
2525 or if the array is.. */
2526 TREE_READONLY (rval)
2527 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2528 TREE_SIDE_EFFECTS (rval)
2529 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2530 TREE_THIS_VOLATILE (rval)
2531 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2532 return require_complete_type (fold (rval));
2536 tree ar = default_conversion (array);
2537 tree ind = default_conversion (idx);
2539 /* Put the integer in IND to simplify error checking. */
2540 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2547 if (ar == error_mark_node)
2550 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2552 error ("subscripted value is neither array nor pointer");
2553 return error_mark_node;
2555 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2557 error ("array subscript is not an integer");
2558 return error_mark_node;
2561 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar,
2567 /* Build a function call to function FUNCTION with parameters PARAMS.
2568 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2569 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2570 not include any object pointer that may be required. FUNCTION's
2571 data type may be a function type or a pointer-to-function.
2573 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2574 is the list of possible methods that FUNCTION could conceivably
2575 be. If the list of methods comes from a class, then it will be
2576 a list of lists (where each element is associated with the class
2577 that produced it), otherwise it will be a simple list (for
2578 functions overloaded in global scope).
2580 In the first case, TREE_VALUE (function) is the head of one of those
2581 lists, and TREE_PURPOSE is the name of the function.
2583 In the second case, TREE_PURPOSE (function) is the function's
2586 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2588 When calling a TEMPLATE_DECL, we don't require a complete return
2592 build_x_function_call (function, params, decl)
2593 tree function, params, decl;
2596 tree template_id = NULL_TREE;
2599 if (function == error_mark_node)
2600 return error_mark_node;
2602 if (processing_template_decl)
2603 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2605 /* Save explicit template arguments if found */
2606 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2608 template_id = function;
2609 function = TREE_OPERAND (function, 0);
2612 type = TREE_TYPE (function);
2614 if (TREE_CODE (type) == OFFSET_TYPE
2615 && TREE_TYPE (type) == unknown_type_node
2616 && TREE_CODE (function) == TREE_LIST
2617 && TREE_CHAIN (function) == NULL_TREE)
2619 /* Undo (Foo:bar)()... */
2620 type = TYPE_OFFSET_BASETYPE (type);
2621 function = TREE_VALUE (function);
2622 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2623 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2624 function = TREE_VALUE (function);
2625 if (TREE_CODE (function) == OVERLOAD)
2626 function = OVL_FUNCTION (function);
2627 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2628 function = DECL_NAME (function);
2629 return build_method_call (decl, function, params,
2630 TYPE_BINFO (type), LOOKUP_NORMAL);
2633 if ((TREE_CODE (function) == FUNCTION_DECL
2634 && DECL_STATIC_FUNCTION_P (function))
2635 || (TREE_CODE (function) == TEMPLATE_DECL
2636 && DECL_STATIC_FUNCTION_P (DECL_RESULT (function))))
2637 return build_member_call(DECL_CONTEXT (function),
2639 ? template_id : DECL_NAME (function),
2642 is_method = ((TREE_CODE (function) == TREE_LIST
2643 && current_class_type != NULL_TREE
2644 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2646 || (TREE_CODE (function) == OVERLOAD
2647 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2648 || TREE_CODE (function) == IDENTIFIER_NODE
2649 || TREE_CODE (type) == METHOD_TYPE
2650 || TYPE_PTRMEMFUNC_P (type));
2652 /* A friend template. Make it look like a toplevel declaration. */
2653 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2654 function = scratch_ovl_cons (function, NULL_TREE);
2656 /* Handle methods, friends, and overloaded functions, respectively. */
2659 tree basetype = NULL_TREE;
2661 if (TREE_CODE (function) == OVERLOAD)
2662 function = OVL_CURRENT (function);
2664 if (TREE_CODE (function) == FUNCTION_DECL
2665 || DECL_FUNCTION_TEMPLATE_P (function))
2667 basetype = DECL_CLASS_CONTEXT (function);
2669 if (DECL_NAME (function))
2670 function = DECL_NAME (function);
2672 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2674 else if (TREE_CODE (function) == TREE_LIST)
2676 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2677 == FUNCTION_DECL, 312);
2678 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2679 function = TREE_PURPOSE (function);
2681 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2683 if (TREE_CODE (function) == OFFSET_REF)
2685 if (TREE_OPERAND (function, 0))
2686 decl = TREE_OPERAND (function, 0);
2688 /* Call via a pointer to member function. */
2689 if (decl == NULL_TREE)
2691 error ("pointer to member function called, but not in class scope");
2692 return error_mark_node;
2694 /* What other type of POINTER_TYPE could this be? */
2695 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2696 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2697 && TREE_CODE (function) != OFFSET_REF)
2698 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2703 /* this is an abbreviated method call.
2704 must go through here in case it is a virtual function.
2705 @@ Perhaps this could be optimized. */
2707 if (basetype && (! current_class_type
2708 || ! DERIVED_FROM_P (basetype, current_class_type)))
2709 return build_member_call (basetype, function, params);
2711 if (decl == NULL_TREE)
2713 if (current_class_type == NULL_TREE)
2715 cp_error ("object missing in call to method `%D'", function);
2716 return error_mark_node;
2718 /* Yow: call from a static member function. */
2719 decl = build_dummy_object (current_class_type);
2722 /* Put back explicit template arguments, if any. */
2724 function = template_id;
2725 return build_method_call (decl, function, params,
2726 NULL_TREE, LOOKUP_NORMAL);
2728 else if (TREE_CODE (function) == COMPONENT_REF
2729 && type == unknown_type_node)
2731 /* Undo what we did in build_component_ref. */
2732 decl = TREE_OPERAND (function, 0);
2733 function = TREE_OPERAND (function, 1);
2734 function = DECL_NAME (OVL_CURRENT (function));
2738 TREE_OPERAND (template_id, 0) = function;
2739 function = template_id;
2742 return build_method_call (decl, function, params,
2743 NULL_TREE, LOOKUP_NORMAL);
2745 else if (really_overloaded_fn (function))
2747 if (OVL_FUNCTION (function) == NULL_TREE)
2749 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2750 TREE_PURPOSE (function));
2751 return error_mark_node;
2755 /* Put back explicit template arguments, if any. */
2757 function = template_id;
2758 return build_new_function_call (function, params);
2762 /* Remove a potential OVERLOAD around it */
2763 function = OVL_CURRENT (function);
2766 if (TREE_CODE (function) == OFFSET_REF)
2768 /* If the component is a data element (or a virtual function), we play
2769 games here to make things work. */
2772 if (TREE_OPERAND (function, 0))
2773 decl = TREE_OPERAND (function, 0);
2775 decl = current_class_ref;
2777 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2779 /* Sigh. OFFSET_REFs are being used for too many things.
2780 They're being used both for -> and ->*, and we want to resolve
2781 the -> cases here, but leave the ->*. We could use
2782 resolve_offset_ref for those, too, but it would call
2783 get_member_function_from_ptrfunc and decl_addr wouldn't get
2784 updated properly. Nasty. */
2785 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2786 function = resolve_offset_ref (function);
2788 function = TREE_OPERAND (function, 1);
2790 function = get_member_function_from_ptrfunc (&decl_addr, function);
2791 params = expr_tree_cons (NULL_TREE, decl_addr, params);
2792 return build_function_call (function, params);
2795 type = TREE_TYPE (function);
2796 if (type != error_mark_node)
2798 if (TREE_CODE (type) == REFERENCE_TYPE)
2799 type = TREE_TYPE (type);
2801 if (IS_AGGR_TYPE (type))
2802 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2807 tree fntype = TREE_TYPE (function);
2808 tree ctypeptr = NULL_TREE;
2810 /* Explicitly named method? */
2811 if (TREE_CODE (function) == FUNCTION_DECL)
2812 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2813 /* Expression with ptr-to-method type? It could either be a plain
2814 usage, or it might be a case where the ptr-to-method is being
2815 passed in as an argument. */
2816 else if (TYPE_PTRMEMFUNC_P (fntype))
2818 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2819 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2820 ctypeptr = build_pointer_type (rec);
2822 /* Unexpected node type? */
2824 my_friendly_abort (116);
2825 if (decl == NULL_TREE)
2827 if (current_function_decl
2828 && DECL_STATIC_FUNCTION_P (current_function_decl))
2829 error ("invalid call to member function needing `this' in static member function scope");
2831 error ("pointer to member function called, but not in class scope");
2832 return error_mark_node;
2834 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2835 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2837 decl = build_unary_op (ADDR_EXPR, decl, 0);
2838 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2841 decl = build_c_cast (ctypeptr, decl);
2842 params = expr_tree_cons (NULL_TREE, decl, params);
2845 return build_function_call (function, params);
2848 /* Resolve a pointer to member function. INSTANCE is the object
2849 instance to use, if the member points to a virtual member. */
2852 get_member_function_from_ptrfunc (instance_ptrptr, function)
2853 tree *instance_ptrptr;
2856 if (TREE_CODE (function) == OFFSET_REF)
2858 function = TREE_OPERAND (function, 1);
2861 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2863 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2864 tree instance, basetype;
2866 tree instance_ptr = *instance_ptrptr;
2868 if (TREE_SIDE_EFFECTS (instance_ptr))
2869 instance_ptr = save_expr (instance_ptr);
2871 if (TREE_SIDE_EFFECTS (function))
2872 function = save_expr (function);
2874 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2875 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2877 delta = cp_convert (ptrdiff_type_node,
2878 build_component_ref (function, delta_identifier,
2880 e3 = PFN_FROM_PTRMEMFUNC (function);
2882 if (TYPE_SIZE (basetype) != NULL_TREE
2883 && ! TYPE_VIRTUAL_P (basetype))
2884 /* If basetype doesn't have virtual functions, don't emit code to
2885 handle that case. */
2889 /* Promoting idx before saving it improves performance on RISC
2890 targets. Without promoting, the first compare used
2891 load-with-sign-extend, while the second used normal load then
2892 shift to sign-extend. An optimizer flaw, perhaps, but it's
2893 easier to make this change. */
2894 idx = save_expr (default_conversion
2895 (build_component_ref (function,
2898 e1 = build_binary_op (GE_EXPR, idx, integer_zero_node);
2900 /* Convert down to the right base, before using the instance. */
2901 instance = convert_pointer_to_real (basetype, instance_ptr);
2902 if (instance == error_mark_node && instance_ptr != error_mark_node)
2905 vtbl = convert_pointer_to (ptr_type_node, instance);
2906 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2909 build_pointer_type (build_pointer_type (vtable_entry_type)),
2910 vtbl, cp_convert (ptrdiff_type_node, delta2));
2911 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2912 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2915 if (! flag_vtable_thunks)
2917 aref = save_expr (aref);
2919 delta = build_binary_op
2921 build_conditional_expr (e1,
2922 build_component_ref (aref,
2929 if (flag_vtable_thunks)
2932 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2933 TREE_TYPE (e2) = TREE_TYPE (e3);
2934 e1 = build_conditional_expr (e1, e2, e3);
2936 /* Make sure this doesn't get evaluated first inside one of the
2937 branches of the COND_EXPR. */
2938 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2939 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2943 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2944 instance_ptr, delta);
2946 if (instance_ptr == error_mark_node
2947 && TREE_CODE (e1) != ADDR_EXPR
2948 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2949 cp_error ("object missing in `%E'", function);
2957 build_function_call_real (function, params, require_complete, flags)
2958 tree function, params;
2959 int require_complete, flags;
2961 register tree fntype, fndecl;
2962 register tree value_type;
2963 register tree coerced_params;
2964 tree name = NULL_TREE, assembler_name = NULL_TREE;
2967 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2968 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2969 if (TREE_CODE (function) == NOP_EXPR
2970 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2971 function = TREE_OPERAND (function, 0);
2973 if (TREE_CODE (function) == FUNCTION_DECL)
2975 name = DECL_NAME (function);
2976 assembler_name = DECL_ASSEMBLER_NAME (function);
2978 GNU_xref_call (current_function_decl,
2979 IDENTIFIER_POINTER (name ? name
2980 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2982 mark_used (function);
2985 /* Convert anything with function type to a pointer-to-function. */
2986 if (pedantic && DECL_MAIN_P (function))
2987 pedwarn ("ANSI C++ forbids calling `main' from within program");
2989 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2990 (because calling an inline function does not mean the function
2991 needs to be separately compiled). */
2993 if (DECL_INLINE (function))
2994 function = inline_conversion (function);
2996 function = build_addr_func (function);
3002 function = build_addr_func (function);
3005 if (function == error_mark_node)
3006 return error_mark_node;
3008 fntype = TREE_TYPE (function);
3010 if (TYPE_PTRMEMFUNC_P (fntype))
3012 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3014 return error_mark_node;
3017 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3018 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3020 if (!((TREE_CODE (fntype) == POINTER_TYPE
3021 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3023 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3025 cp_error ("`%E' cannot be used as a function", function);
3026 return error_mark_node;
3029 /* fntype now gets the type of function pointed to. */
3030 fntype = TREE_TYPE (fntype);
3032 /* Convert the parameters to the types declared in the
3033 function prototype, or apply default promotions. */
3035 if (flags & LOOKUP_COMPLAIN)
3036 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3037 params, fndecl, LOOKUP_NORMAL);
3039 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3042 if (coerced_params == error_mark_node)
3044 if (flags & LOOKUP_SPECULATIVELY)
3047 return error_mark_node;
3050 /* Check for errors in format strings. */
3052 if (warn_format && (name || assembler_name))
3053 check_function_format (name, assembler_name, coerced_params);
3055 /* Recognize certain built-in functions so we can make tree-codes
3056 other than CALL_EXPR. We do this when it enables fold-const.c
3057 to do something useful. */
3059 if (TREE_CODE (function) == ADDR_EXPR
3060 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3061 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3062 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
3067 if (coerced_params == 0)
3068 return integer_zero_node;
3069 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3076 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3078 register tree result
3079 = build_call (function, value_type, coerced_params);
3081 if (require_complete)
3083 if (TREE_CODE (value_type) == VOID_TYPE)
3085 result = require_complete_type (result);
3087 if (IS_AGGR_TYPE (value_type))
3088 result = build_cplus_new (value_type, result);
3089 return convert_from_reference (result);
3094 build_function_call (function, params)
3095 tree function, params;
3097 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3100 /* Convert the actual parameter expressions in the list VALUES
3101 to the types in the list TYPELIST.
3102 If parmdecls is exhausted, or when an element has NULL as its type,
3103 perform the default conversions.
3105 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3107 This is also where warnings about wrong number of args are generated.
3109 Return a list of expressions for the parameters as converted.
3111 Both VALUES and the returned value are chains of TREE_LIST nodes
3112 with the elements of the list in the TREE_VALUE slots of those nodes.
3114 In C++, unspecified trailing parameters can be filled in with their
3115 default arguments, if such were specified. Do so here. */
3118 convert_arguments (typelist, values, fndecl, flags)
3119 tree typelist, values, fndecl;
3122 register tree typetail, valtail;
3123 register tree result = NULL_TREE;
3124 const char *called_thing = 0;
3127 /* Argument passing is always copy-initialization. */
3128 flags |= LOOKUP_ONLYCONVERTING;
3132 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3134 if (DECL_NAME (fndecl) == NULL_TREE
3135 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3136 called_thing = "constructor";
3138 called_thing = "member function";
3141 called_thing = "function";
3144 for (valtail = values, typetail = typelist;
3146 valtail = TREE_CHAIN (valtail), i++)
3148 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3149 register tree val = TREE_VALUE (valtail);
3151 if (val == error_mark_node)
3152 return error_mark_node;
3154 if (type == void_type_node)
3158 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3160 error ("at this point in file");
3163 error ("too many arguments to function");
3164 /* In case anybody wants to know if this argument
3167 TREE_TYPE (tree_last (result)) = error_mark_node;
3171 if (TREE_CODE (val) == OFFSET_REF)
3172 val = resolve_offset_ref (val);
3174 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3175 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3176 if (TREE_CODE (val) == NOP_EXPR
3177 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3178 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3179 val = TREE_OPERAND (val, 0);
3181 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3183 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3184 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3185 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3186 val = default_conversion (val);
3189 if (val == error_mark_node)
3190 return error_mark_node;
3194 /* Formal parm type is specified by a function prototype. */
3197 if (TYPE_SIZE (complete_type (type)) == 0)
3199 error ("parameter type of called function is incomplete");
3204 parmval = convert_for_initialization
3205 (NULL_TREE, type, val, flags,
3206 "argument passing", fndecl, i);
3207 if (PROMOTE_PROTOTYPES
3208 && (TREE_CODE (type) == INTEGER_TYPE
3209 || TREE_CODE (type) == ENUMERAL_TYPE)
3210 && (TYPE_PRECISION (type)
3211 < TYPE_PRECISION (integer_type_node)))
3212 parmval = default_conversion (parmval);
3215 if (parmval == error_mark_node)
3216 return error_mark_node;
3218 result = expr_tree_cons (NULL_TREE, parmval, result);
3222 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3223 val = convert_from_reference (val);
3225 result = expr_tree_cons (NULL_TREE,
3226 convert_arg_to_ellipsis (val),
3231 typetail = TREE_CHAIN (typetail);
3234 if (typetail != 0 && typetail != void_list_node)
3236 /* See if there are default arguments that can be used */
3237 if (TREE_PURPOSE (typetail))
3239 for (; typetail != void_list_node; ++i)
3242 = convert_default_arg (TREE_VALUE (typetail),
3243 TREE_PURPOSE (typetail),
3246 if (parmval == error_mark_node)
3247 return error_mark_node;
3249 result = expr_tree_cons (0, parmval, result);
3250 typetail = TREE_CHAIN (typetail);
3251 /* ends with `...'. */
3252 if (typetail == NULL_TREE)
3260 cp_error_at ("too few arguments to %s `%+#D'",
3261 called_thing, fndecl);
3262 error ("at this point in file");
3265 error ("too few arguments to function");
3266 return error_mark_list;
3270 return nreverse (result);
3273 /* Build a binary-operation expression, after performing default
3274 conversions on the operands. CODE is the kind of expression to build. */
3277 build_x_binary_op (code, arg1, arg2)
3278 enum tree_code code;
3281 if (processing_template_decl)
3282 return build_min_nt (code, arg1, arg2);
3284 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3288 build_binary_op (code, arg1, arg2)
3289 enum tree_code code;
3292 return build_binary_op_nodefault (code, arg1, arg2, code);
3295 /* Build a binary-operation expression without default conversions.
3296 CODE is the kind of expression to build.
3297 This function differs from `build' in several ways:
3298 the data type of the result is computed and recorded in it,
3299 warnings are generated if arg data types are invalid,
3300 special handling for addition and subtraction of pointers is known,
3301 and some optimization is done (operations on narrow ints
3302 are done in the narrower type when that gives the same result).
3303 Constant folding is also done before the result is returned.
3305 ERROR_CODE is the code that determines what to say in error messages.
3306 It is usually, but not always, the same as CODE.
3308 Note that the operands will never have enumeral types
3309 because either they have just had the default conversions performed
3310 or they have both just been converted to some other type in which
3311 the arithmetic is to be done.
3313 C++: must do special pointer arithmetic when implementing
3314 multiple inheritance, and deal with pointer to member functions. */
3317 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3318 enum tree_code code;
3319 tree orig_op0, orig_op1;
3320 enum tree_code error_code;
3323 register enum tree_code code0, code1;
3326 /* Expression code to give to the expression when it is built.
3327 Normally this is CODE, which is what the caller asked for,
3328 but in some special cases we change it. */
3329 register enum tree_code resultcode = code;
3331 /* Data type in which the computation is to be performed.
3332 In the simplest cases this is the common type of the arguments. */
3333 register tree result_type = NULL;
3335 /* Nonzero means operands have already been type-converted
3336 in whatever way is necessary.
3337 Zero means they need to be converted to RESULT_TYPE. */
3340 /* Nonzero means create the expression with this type, rather than
3342 tree build_type = 0;
3344 /* Nonzero means after finally constructing the expression
3345 convert it to this type. */
3346 tree final_type = 0;
3348 /* Nonzero if this is an operation like MIN or MAX which can
3349 safely be computed in short if both args are promoted shorts.
3350 Also implies COMMON.
3351 -1 indicates a bitwise operation; this makes a difference
3352 in the exact conditions for when it is safe to do the operation
3353 in a narrower mode. */
3356 /* Nonzero if this is a comparison operation;
3357 if both args are promoted shorts, compare the original shorts.
3358 Also implies COMMON. */
3359 int short_compare = 0;
3361 /* Nonzero if this is a right-shift operation, which can be computed on the
3362 original short and then promoted if the operand is a promoted short. */
3363 int short_shift = 0;
3365 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3368 /* Apply default conversions. */
3369 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3370 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3371 || code == TRUTH_XOR_EXPR)
3373 op0 = decay_conversion (orig_op0);
3374 op1 = decay_conversion (orig_op1);
3378 op0 = default_conversion (orig_op0);
3379 op1 = default_conversion (orig_op1);
3382 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3383 STRIP_TYPE_NOPS (op0);
3384 STRIP_TYPE_NOPS (op1);
3386 /* DTRT if one side is an overloaded function, but complain about it. */
3387 if (type_unknown_p (op0))
3389 tree t = instantiate_type (TREE_TYPE (op1), op0, 0);
3390 if (t != error_mark_node)
3392 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3397 if (type_unknown_p (op1))
3399 tree t = instantiate_type (TREE_TYPE (op0), op1, 0);
3400 if (t != error_mark_node)
3402 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3408 type0 = TREE_TYPE (op0);
3409 type1 = TREE_TYPE (op1);
3411 /* The expression codes of the data types of the arguments tell us
3412 whether the arguments are integers, floating, pointers, etc. */
3413 code0 = TREE_CODE (type0);
3414 code1 = TREE_CODE (type1);
3416 /* If an error was already reported for one of the arguments,
3417 avoid reporting another error. */
3419 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3420 return error_mark_node;
3425 /* Handle the pointer + int case. */
3426 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3427 return pointer_int_sum (PLUS_EXPR, op0, op1);
3428 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3429 return pointer_int_sum (PLUS_EXPR, op1, op0);
3435 /* Subtraction of two similar pointers.
3436 We must subtract them as integers, then divide by object size. */
3437 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3438 && comp_target_types (type0, type1, 1))
3439 return pointer_diff (op0, op1, common_type (type0, type1));
3440 /* Handle pointer minus int. Just like pointer plus int. */
3441 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3442 return pointer_int_sum (MINUS_EXPR, op0, op1);
3451 case TRUNC_DIV_EXPR:
3453 case FLOOR_DIV_EXPR:
3454 case ROUND_DIV_EXPR:
3455 case EXACT_DIV_EXPR:
3456 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3457 || code0 == COMPLEX_TYPE)
3458 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3459 || code1 == COMPLEX_TYPE))
3461 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3462 cp_warning ("division by zero in `%E / 0'", op0);
3463 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3464 cp_warning ("division by zero in `%E / 0.'", op0);
3466 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3467 resultcode = RDIV_EXPR;
3469 /* When dividing two signed integers, we have to promote to int.
3470 unless we divide by a constant != -1. Note that default
3471 conversion will have been performed on the operands at this
3472 point, so we have to dig out the original type to find out if
3474 shorten = ((TREE_CODE (op0) == NOP_EXPR
3475 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3476 || (TREE_CODE (op1) == INTEGER_CST
3477 && (TREE_INT_CST_LOW (op1) != -1
3478 || TREE_INT_CST_HIGH (op1) != -1)));
3484 case BIT_ANDTC_EXPR:
3487 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3489 /* If one operand is a constant, and the other is a short type
3490 that has been converted to an int,
3491 really do the work in the short type and then convert the
3492 result to int. If we are lucky, the constant will be 0 or 1
3493 in the short type, making the entire operation go away. */
3494 if (TREE_CODE (op0) == INTEGER_CST
3495 && TREE_CODE (op1) == NOP_EXPR
3496 && (TYPE_PRECISION (type1)
3497 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3498 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3500 final_type = result_type;
3501 op1 = TREE_OPERAND (op1, 0);
3502 result_type = TREE_TYPE (op1);
3504 if (TREE_CODE (op1) == INTEGER_CST
3505 && TREE_CODE (op0) == NOP_EXPR
3506 && (TYPE_PRECISION (type0)
3507 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3508 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3510 final_type = result_type;
3511 op0 = TREE_OPERAND (op0, 0);
3512 result_type = TREE_TYPE (op0);
3516 case TRUNC_MOD_EXPR:
3517 case FLOOR_MOD_EXPR:
3518 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3519 cp_warning ("division by zero in `%E %% 0'", op0);
3520 else if (code1 == REAL_TYPE && real_zerop (op1))
3521 cp_warning ("division by zero in `%E %% 0.'", op0);
3523 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3525 /* Although it would be tempting to shorten always here, that loses
3526 on some targets, since the modulo instruction is undefined if the
3527 quotient can't be represented in the computation mode. We shorten
3528 only if unsigned or if dividing by something we know != -1. */
3529 shorten = ((TREE_CODE (op0) == NOP_EXPR
3530 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3531 || (TREE_CODE (op1) == INTEGER_CST
3532 && (TREE_INT_CST_LOW (op1) != -1
3533 || TREE_INT_CST_HIGH (op1) != -1)));
3538 case TRUTH_ANDIF_EXPR:
3539 case TRUTH_ORIF_EXPR:
3540 case TRUTH_AND_EXPR:
3542 result_type = boolean_type_node;
3545 /* Shift operations: result has same type as first operand;
3546 always convert second operand to int.
3547 Also set SHORT_SHIFT if shifting rightward. */
3550 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3552 result_type = type0;
3553 if (TREE_CODE (op1) == INTEGER_CST)
3555 if (tree_int_cst_lt (op1, integer_zero_node))
3556 warning ("right shift count is negative");
3559 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3561 if (TREE_INT_CST_HIGH (op1) != 0
3562 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3563 >= TYPE_PRECISION (type0)))
3564 warning ("right shift count >= width of type");
3567 /* Convert the shift-count to an integer, regardless of
3568 size of value being shifted. */
3569 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3570 op1 = cp_convert (integer_type_node, op1);
3571 /* Avoid converting op1 to result_type later. */
3577 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3579 result_type = type0;
3580 if (TREE_CODE (op1) == INTEGER_CST)
3582 if (tree_int_cst_lt (op1, integer_zero_node))
3583 warning ("left shift count is negative");
3584 else if (TREE_INT_CST_HIGH (op1) != 0
3585 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3586 >= TYPE_PRECISION (type0)))
3587 warning ("left shift count >= width of type");
3589 /* Convert the shift-count to an integer, regardless of
3590 size of value being shifted. */
3591 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3592 op1 = cp_convert (integer_type_node, op1);
3593 /* Avoid converting op1 to result_type later. */
3600 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3602 result_type = type0;
3603 if (TREE_CODE (op1) == INTEGER_CST)
3605 if (tree_int_cst_lt (op1, integer_zero_node))
3606 warning ("%s rotate count is negative",
3607 (code == LROTATE_EXPR) ? "left" : "right");
3608 else if (TREE_INT_CST_HIGH (op1) != 0
3609 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3610 >= TYPE_PRECISION (type0)))
3611 warning ("%s rotate count >= width of type",
3612 (code == LROTATE_EXPR) ? "left" : "right");
3614 /* Convert the shift-count to an integer, regardless of
3615 size of value being shifted. */
3616 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3617 op1 = cp_convert (integer_type_node, op1);
3623 build_type = boolean_type_node;
3624 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3625 || code0 == COMPLEX_TYPE)
3626 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3627 || code1 == COMPLEX_TYPE))
3629 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3631 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3632 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3634 if (comp_target_types (type0, type1, 1))
3635 result_type = common_type (type0, type1);
3636 else if (tt0 == void_type_node)
3638 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3639 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3640 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3641 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3642 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3644 else if (tt1 == void_type_node)
3646 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3647 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3648 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3651 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3654 if (result_type == NULL_TREE)
3655 result_type = ptr_type_node;
3657 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3658 result_type = type0;
3659 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3660 result_type = type1;
3661 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3663 result_type = type0;
3664 error ("ANSI C++ forbids comparison between pointer and integer");
3666 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3668 result_type = type1;
3669 error ("ANSI C++ forbids comparison between pointer and integer");
3671 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3673 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3674 op1 = integer_zero_node;
3675 result_type = TREE_TYPE (op0);
3677 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3679 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3680 op1 = integer_zero_node;
3681 result_type = TREE_TYPE (op0);
3683 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3684 && same_type_p (type0, type1))
3686 /* The code we generate for the test is:
3688 (op0.index == op1.index
3689 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3690 || op0.pfn == op1.pfn)) */
3692 tree index0 = build_component_ref (op0, index_identifier,
3694 tree index1 = save_expr (build_component_ref (op1, index_identifier,
3696 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3697 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3698 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3699 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3701 tree integer_neg_one_node
3702 = build_binary_op (MINUS_EXPR, integer_zero_node,
3704 e1 = build_binary_op (EQ_EXPR, index0, index1);
3705 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3706 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3707 build_binary_op (EQ_EXPR, delta20, delta21));
3708 /* We can't use build_binary_op for this cmp because it would get
3709 confused by the ptr to method types and think we want pmfs. */
3710 e3 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3711 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3712 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3713 if (code == EQ_EXPR)
3715 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3717 else if (TYPE_PTRMEMFUNC_P (type0)
3718 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3720 tree index0 = build_component_ref (op0, index_identifier,
3723 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3724 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3725 tree delta21 = integer_zero_node;
3727 tree integer_neg_one_node
3728 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node);
3729 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3730 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3732 /* Map everything down one to make room for
3733 the null pointer to member. */
3734 index1 = size_binop (PLUS_EXPR,
3735 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3737 op1 = integer_zero_node;
3738 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3739 (TREE_TYPE (type1)));
3740 delta21 = DECL_FIELD_BITPOS (delta21);
3741 delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3742 size_int (BITS_PER_UNIT));
3743 delta21 = convert (sizetype, delta21);
3746 index1 = integer_neg_one_node;
3748 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3750 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3753 e1 = build_binary_op (EQ_EXPR, index0, index1);
3754 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3755 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3756 build_binary_op (EQ_EXPR, delta20, delta21));
3757 /* We can't use build_binary_op for this cmp because it would get
3758 confused by the ptr to method types and think we want pmfs. */
3759 e3 = build (EQ_EXPR, boolean_type_node, pfn0, op1);
3760 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3761 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3762 if (code == EQ_EXPR)
3764 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3766 else if (TYPE_PTRMEMFUNC_P (type1)
3767 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0))
3768 return build_binary_op (code, op1, op0);
3773 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3774 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3776 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3778 if (comp_target_types (type0, type1, 1))
3779 result_type = common_type (type0, type1);
3782 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3784 result_type = ptr_type_node;
3793 build_type = boolean_type_node;
3794 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3795 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3797 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3799 if (comp_target_types (type0, type1, 1))
3800 result_type = common_type (type0, type1);
3803 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3805 result_type = ptr_type_node;
3808 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3809 && integer_zerop (op1))
3810 result_type = type0;
3811 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3812 && integer_zerop (op0))
3813 result_type = type1;
3814 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3816 result_type = type0;
3817 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3819 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3821 result_type = type1;
3822 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3830 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3832 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3834 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3836 if (shorten || common || short_compare)
3837 result_type = common_type (type0, type1);
3839 /* For certain operations (which identify themselves by shorten != 0)
3840 if both args were extended from the same smaller type,
3841 do the arithmetic in that type and then extend.
3843 shorten !=0 and !=1 indicates a bitwise operation.
3844 For them, this optimization is safe only if
3845 both args are zero-extended or both are sign-extended.
3846 Otherwise, we might change the result.
3847 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3848 but calculated in (unsigned short) it would be (unsigned short)-1. */
3850 if (shorten && none_complex)
3852 int unsigned0, unsigned1;
3853 tree arg0 = get_narrower (op0, &unsigned0);
3854 tree arg1 = get_narrower (op1, &unsigned1);
3855 /* UNS is 1 if the operation to be done is an unsigned one. */
3856 int uns = TREE_UNSIGNED (result_type);
3859 final_type = result_type;
3861 /* Handle the case that OP0 does not *contain* a conversion
3862 but it *requires* conversion to FINAL_TYPE. */
3864 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3865 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3866 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3867 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3869 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3871 /* For bitwise operations, signedness of nominal type
3872 does not matter. Consider only how operands were extended. */
3876 /* Note that in all three cases below we refrain from optimizing
3877 an unsigned operation on sign-extended args.
3878 That would not be valid. */
3880 /* Both args variable: if both extended in same way
3881 from same width, do it in that width.
3882 Do it unsigned if args were zero-extended. */
3883 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3884 < TYPE_PRECISION (result_type))
3885 && (TYPE_PRECISION (TREE_TYPE (arg1))
3886 == TYPE_PRECISION (TREE_TYPE (arg0)))
3887 && unsigned0 == unsigned1
3888 && (unsigned0 || !uns))
3890 = signed_or_unsigned_type (unsigned0,
3891 common_type (TREE_TYPE (arg0),
3893 else if (TREE_CODE (arg0) == INTEGER_CST
3894 && (unsigned1 || !uns)
3895 && (TYPE_PRECISION (TREE_TYPE (arg1))
3896 < TYPE_PRECISION (result_type))
3897 && (type = signed_or_unsigned_type (unsigned1,
3899 int_fits_type_p (arg0, type)))
3901 else if (TREE_CODE (arg1) == INTEGER_CST
3902 && (unsigned0 || !uns)
3903 && (TYPE_PRECISION (TREE_TYPE (arg0))
3904 < TYPE_PRECISION (result_type))
3905 && (type = signed_or_unsigned_type (unsigned0,
3907 int_fits_type_p (arg1, type)))
3911 /* Shifts can be shortened if shifting right. */
3916 tree arg0 = get_narrower (op0, &unsigned_arg);
3918 final_type = result_type;
3920 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3921 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3923 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3924 /* We can shorten only if the shift count is less than the
3925 number of bits in the smaller type size. */
3926 && TREE_INT_CST_HIGH (op1) == 0
3927 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3928 /* If arg is sign-extended and then unsigned-shifted,
3929 we can simulate this with a signed shift in arg's type
3930 only if the extended result is at least twice as wide
3931 as the arg. Otherwise, the shift could use up all the
3932 ones made by sign-extension and bring in zeros.
3933 We can't optimize that case at all, but in most machines
3934 it never happens because available widths are 2**N. */
3935 && (!TREE_UNSIGNED (final_type)
3937 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3938 <= TYPE_PRECISION (result_type))))
3940 /* Do an unsigned shift if the operand was zero-extended. */
3942 = signed_or_unsigned_type (unsigned_arg,
3944 /* Convert value-to-be-shifted to that type. */
3945 if (TREE_TYPE (op0) != result_type)
3946 op0 = cp_convert (result_type, op0);
3951 /* Comparison operations are shortened too but differently.
3952 They identify themselves by setting short_compare = 1. */
3956 /* Don't write &op0, etc., because that would prevent op0
3957 from being kept in a register.
3958 Instead, make copies of the our local variables and
3959 pass the copies by reference, then copy them back afterward. */
3960 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3961 enum tree_code xresultcode = resultcode;
3963 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3965 return cp_convert (boolean_type_node, val);
3966 op0 = xop0, op1 = xop1;
3968 resultcode = xresultcode;
3971 if (short_compare && warn_sign_compare)
3973 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3974 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3976 int unsignedp0, unsignedp1;
3977 tree primop0 = get_narrower (op0, &unsignedp0);
3978 tree primop1 = get_narrower (op1, &unsignedp1);
3980 /* Check for comparison of different enum types. */
3981 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3982 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3983 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3984 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3986 cp_warning ("comparison between `%#T' and `%#T'",
3987 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3990 /* Give warnings for comparisons between signed and unsigned
3991 quantities that may fail. */
3992 /* Do the checking based on the original operand trees, so that
3993 casts will be considered, but default promotions won't be. */
3995 /* Do not warn if the comparison is being done in a signed type,
3996 since the signed type will only be chosen if it can represent
3997 all the values of the unsigned type. */
3998 if (! TREE_UNSIGNED (result_type))
4000 /* Do not warn if both operands are unsigned. */
4001 else if (op0_signed == op1_signed)
4003 /* Do not warn if the signed quantity is an unsuffixed
4004 integer literal (or some static constant expression
4005 involving such literals) and it is non-negative. */
4006 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
4007 && tree_int_cst_sgn (orig_op0) >= 0)
4008 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
4009 && tree_int_cst_sgn (orig_op1) >= 0))
4011 /* Do not warn if the comparison is an equality operation,
4012 the unsigned quantity is an integral constant and it does
4013 not use the most significant bit of result_type. */
4014 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
4015 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
4016 && int_fits_type_p (orig_op1,
4017 signed_type (result_type)))
4018 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
4019 && int_fits_type_p (orig_op0,
4020 signed_type (result_type)))))
4023 warning ("comparison between signed and unsigned");
4025 /* Warn if two unsigned values are being compared in a size
4026 larger than their original size, and one (and only one) is the
4027 result of a `~' operator. This comparison will always fail.
4029 Also warn if one operand is a constant, and the constant does not
4030 have all bits set that are set in the ~ operand when it is
4033 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
4034 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
4036 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
4037 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
4038 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
4039 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
4041 if (TREE_CODE (primop0) == INTEGER_CST
4042 || TREE_CODE (primop1) == INTEGER_CST)
4045 HOST_WIDE_INT constant, mask;
4049 if (TREE_CODE (primop0) == INTEGER_CST)
4052 unsignedp = unsignedp1;
4053 constant = TREE_INT_CST_LOW (primop0);
4058 unsignedp = unsignedp0;
4059 constant = TREE_INT_CST_LOW (primop1);
4062 bits = TYPE_PRECISION (TREE_TYPE (primop));
4063 if (bits < TYPE_PRECISION (result_type)
4064 && bits < HOST_BITS_PER_LONG && unsignedp)
4066 mask = (~ (HOST_WIDE_INT) 0) << bits;
4067 if ((mask & constant) != mask)
4068 warning ("comparison of promoted ~unsigned with constant");
4071 else if (unsignedp0 && unsignedp1
4072 && (TYPE_PRECISION (TREE_TYPE (primop0))
4073 < TYPE_PRECISION (result_type))
4074 && (TYPE_PRECISION (TREE_TYPE (primop1))
4075 < TYPE_PRECISION (result_type)))
4076 warning ("comparison of promoted ~unsigned with unsigned");
4081 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4082 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4083 Then the expression will be built.
4084 It will be given type FINAL_TYPE if that is nonzero;
4085 otherwise, it will be given type RESULT_TYPE. */
4089 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
4090 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
4091 return error_mark_node;
4094 /* Issue warnings about peculiar, but legal, uses of NULL. */
4095 if (/* It's reasonable to use pointer values as operands of &&
4096 and ||, so NULL is no exception. */
4097 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4098 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4099 (orig_op0 == null_node
4100 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4101 /* Or vice versa. */
4102 || (orig_op1 == null_node
4103 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4104 /* Or, both are NULL and the operation was not a comparison. */
4105 || (orig_op0 == null_node && orig_op1 == null_node
4106 && code != EQ_EXPR && code != NE_EXPR)))
4107 /* Some sort of arithmetic operation involving NULL was
4108 performed. Note that pointer-difference and pointer-addition
4109 have already been handled above, and so we don't end up here in
4111 cp_warning ("NULL used in arithmetic");
4115 if (TREE_TYPE (op0) != result_type)
4116 op0 = cp_convert (result_type, op0);
4117 if (TREE_TYPE (op1) != result_type)
4118 op1 = cp_convert (result_type, op1);
4120 if (op0 == error_mark_node || op1 == error_mark_node)
4121 return error_mark_node;
4124 if (build_type == NULL_TREE)
4125 build_type = result_type;
4128 register tree result = build (resultcode, build_type, op0, op1);
4129 register tree folded;
4131 folded = fold (result);
4132 if (folded == result)
4133 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4134 if (final_type != 0)
4135 return cp_convert (final_type, folded);
4140 /* Return a tree for the sum or difference (RESULTCODE says which)
4141 of pointer PTROP and integer INTOP. */
4144 pointer_int_sum (resultcode, ptrop, intop)
4145 enum tree_code resultcode;
4146 register tree ptrop, intop;
4150 register tree result;
4151 register tree folded = fold (intop);
4153 /* The result is a pointer of the same type that is being added. */
4155 register tree result_type = TREE_TYPE (ptrop);
4157 if (!complete_type_or_else (result_type, ptrop))
4158 return error_mark_node;
4160 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4162 if (pedantic || warn_pointer_arith)
4163 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
4164 size_exp = integer_one_node;
4166 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4168 if (pedantic || warn_pointer_arith)
4169 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
4170 size_exp = integer_one_node;
4172 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4174 if (pedantic || warn_pointer_arith)
4175 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4176 size_exp = integer_one_node;
4178 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4180 if (pedantic || warn_pointer_arith)
4181 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4182 size_exp = integer_one_node;
4185 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4187 /* Needed to make OOPS V2R3 work. */
4189 if (TREE_CODE (intop) == INTEGER_CST
4190 && TREE_INT_CST_LOW (intop) == 0
4191 && TREE_INT_CST_HIGH (intop) == 0)
4194 /* If what we are about to multiply by the size of the elements
4195 contains a constant term, apply distributive law
4196 and multiply that constant term separately.
4197 This helps produce common subexpressions. */
4199 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4200 && ! TREE_CONSTANT (intop)
4201 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4202 && TREE_CONSTANT (size_exp))
4204 enum tree_code subcode = resultcode;
4205 if (TREE_CODE (intop) == MINUS_EXPR)
4206 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4207 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4208 intop = TREE_OPERAND (intop, 0);
4211 /* Convert the integer argument to a type the same size as sizetype
4212 so the multiply won't overflow spuriously. */
4214 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4215 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4217 /* Replace the integer argument with a suitable product by the object size.
4218 Do this multiplication as signed, then convert to the appropriate
4219 pointer type (actually unsigned integral). */
4221 intop = cp_convert (result_type,
4222 build_binary_op (MULT_EXPR, intop,
4223 cp_convert (TREE_TYPE (intop),
4226 /* Create the sum or difference. */
4228 result = build (resultcode, result_type, ptrop, intop);
4230 folded = fold (result);
4231 if (folded == result)
4232 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4236 /* Return a tree for the difference of pointers OP0 and OP1.
4237 The resulting tree has type int. */
4240 pointer_diff (op0, op1, ptrtype)
4241 register tree op0, op1;
4242 register tree ptrtype;
4244 register tree result, folded;
4245 tree restype = ptrdiff_type_node;
4246 tree target_type = TREE_TYPE (ptrtype);
4248 if (!complete_type_or_else (target_type, NULL_TREE))
4249 return error_mark_node;
4251 if (pedantic || warn_pointer_arith)
4253 if (TREE_CODE (target_type) == VOID_TYPE)
4254 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4255 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4256 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4257 if (TREE_CODE (target_type) == METHOD_TYPE)
4258 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4259 if (TREE_CODE (target_type) == OFFSET_TYPE)
4260 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4263 /* First do the subtraction as integers;
4264 then drop through to build the divide operator. */
4266 op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4267 cp_convert (restype, op1));
4269 /* This generates an error if op1 is a pointer to an incomplete type. */
4270 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4271 error ("arithmetic on pointer to an incomplete type");
4273 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4274 || TREE_CODE (target_type) == FUNCTION_TYPE
4275 || TREE_CODE (target_type) == METHOD_TYPE
4276 || TREE_CODE (target_type) == OFFSET_TYPE)
4278 : size_in_bytes (target_type));
4280 /* Do the division. */
4282 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4284 folded = fold (result);
4285 if (folded == result)
4286 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4290 /* Handle the case of taking the address of a COMPONENT_REF.
4291 Called by `build_unary_op'.
4293 ARG is the COMPONENT_REF whose address we want.
4294 ARGTYPE is the pointer type that this address should have. */
4297 build_component_addr (arg, argtype)
4300 tree field = TREE_OPERAND (arg, 1);
4301 tree basetype = decl_type_context (field);
4302 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4304 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4306 if (DECL_C_BIT_FIELD (field))
4308 cp_error ("attempt to take address of bit-field structure member `%D'",
4310 return error_mark_node;
4313 if (TREE_CODE (field) == FIELD_DECL
4314 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4316 /* Can't convert directly to ARGTYPE, since that
4317 may have the same pointer type as one of our
4319 rval = build1 (NOP_EXPR, argtype,
4320 convert_pointer_to (basetype, rval));
4321 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4324 /* This conversion is harmless. */
4325 rval = convert_force (argtype, rval, 0);
4327 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4329 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4330 size_int (BITS_PER_UNIT));
4331 int flag = TREE_CONSTANT (rval);
4332 offset = convert (sizetype, offset);
4333 rval = fold (build (PLUS_EXPR, argtype,
4334 rval, cp_convert (argtype, offset)));
4335 TREE_CONSTANT (rval) = flag;
4340 /* Construct and perhaps optimize a tree representation
4341 for a unary operation. CODE, a tree_code, specifies the operation
4342 and XARG is the operand. */
4345 build_x_unary_op (code, xarg)
4346 enum tree_code code;
4349 if (processing_template_decl)
4350 return build_min_nt (code, xarg, NULL_TREE);
4352 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4354 if (code == ADDR_EXPR
4355 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4356 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4357 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4358 || (TREE_CODE (xarg) == OFFSET_REF)))
4359 /* don't look for a function */;
4364 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4365 NULL_TREE, NULL_TREE);
4366 if (rval || code != ADDR_EXPR)
4370 if (code == ADDR_EXPR)
4372 if (TREE_CODE (xarg) == TARGET_EXPR)
4373 warning ("taking address of temporary");
4376 return build_unary_op (code, xarg, 0);
4379 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4382 condition_conversion (expr)
4386 if (processing_template_decl)
4388 t = cp_convert (boolean_type_node, expr);
4389 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4393 /* C++: Must handle pointers to members.
4395 Perhaps type instantiation should be extended to handle conversion
4396 from aggregates to types we don't yet know we want? (Or are those
4397 cases typically errors which should be reported?)
4399 NOCONVERT nonzero suppresses the default promotions
4400 (such as from short to int). */
4403 build_unary_op (code, xarg, noconvert)
4404 enum tree_code code;
4408 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4409 register tree arg = xarg;
4410 register tree argtype = 0;
4411 const char *errstring = NULL;
4414 if (arg == error_mark_node)
4415 return error_mark_node;
4420 /* This is used for unary plus, because a CONVERT_EXPR
4421 is enough to prevent anybody from looking inside for
4422 associativity, but won't generate any code. */
4423 if (!(arg = build_expr_type_conversion
4424 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4425 errstring = "wrong type argument to unary plus";
4429 arg = default_conversion (arg);
4430 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4431 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4436 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4437 errstring = "wrong type argument to unary minus";
4438 else if (!noconvert)
4439 arg = default_conversion (arg);
4443 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4447 arg = default_conversion (arg);
4449 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4451 errstring = "wrong type argument to bit-complement";
4452 else if (!noconvert)
4453 arg = default_conversion (arg);
4457 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4458 errstring = "wrong type argument to abs";
4459 else if (!noconvert)
4460 arg = default_conversion (arg);
4464 /* Conjugating a real value is a no-op, but allow it anyway. */
4465 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4466 errstring = "wrong type argument to conjugation";
4467 else if (!noconvert)
4468 arg = default_conversion (arg);
4471 case TRUTH_NOT_EXPR:
4472 arg = cp_convert (boolean_type_node, arg);
4473 val = invert_truthvalue (arg);
4474 if (arg != error_mark_node)
4476 errstring = "in argument to unary !";
4483 if (TREE_CODE (arg) == COMPLEX_CST)
4484 return TREE_REALPART (arg);
4485 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4486 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4491 if (TREE_CODE (arg) == COMPLEX_CST)
4492 return TREE_IMAGPART (arg);
4493 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4494 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4496 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4498 case PREINCREMENT_EXPR:
4499 case POSTINCREMENT_EXPR:
4500 case PREDECREMENT_EXPR:
4501 case POSTDECREMENT_EXPR:
4502 /* Handle complex lvalues (when permitted)
4503 by reduction to simpler cases. */
4505 val = unary_complex_lvalue (code, arg);
4509 /* Increment or decrement the real part of the value,
4510 and don't change the imaginary part. */
4511 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4515 arg = stabilize_reference (arg);
4516 real = build_unary_op (REALPART_EXPR, arg, 1);
4517 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4518 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4519 build_unary_op (code, real, 1), imag);
4522 /* Report invalid types. */
4524 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4527 if (code == PREINCREMENT_EXPR)
4528 errstring ="no pre-increment operator for type";
4529 else if (code == POSTINCREMENT_EXPR)
4530 errstring ="no post-increment operator for type";
4531 else if (code == PREDECREMENT_EXPR)
4532 errstring ="no pre-decrement operator for type";
4534 errstring ="no post-decrement operator for type";
4538 /* Report something read-only. */
4540 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4541 || TREE_READONLY (arg))
4542 readonly_error (arg, ((code == PREINCREMENT_EXPR
4543 || code == POSTINCREMENT_EXPR)
4544 ? "increment" : "decrement"),
4549 tree result_type = TREE_TYPE (arg);
4551 arg = get_unwidened (arg, 0);
4552 argtype = TREE_TYPE (arg);
4554 /* ARM $5.2.5 last annotation says this should be forbidden. */
4555 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4556 pedwarn ("ANSI C++ forbids %sing an enum",
4557 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4558 ? "increment" : "decrement");
4560 /* Compute the increment. */
4562 if (TREE_CODE (argtype) == POINTER_TYPE)
4564 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4565 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4566 cp_error ("cannot %s a pointer to incomplete type `%T'",
4567 ((code == PREINCREMENT_EXPR
4568 || code == POSTINCREMENT_EXPR)
4569 ? "increment" : "decrement"), TREE_TYPE (argtype));
4570 else if ((pedantic || warn_pointer_arith)
4571 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4572 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4573 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4574 ((code == PREINCREMENT_EXPR
4575 || code == POSTINCREMENT_EXPR)
4576 ? "increment" : "decrement"), argtype);
4577 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4580 inc = integer_one_node;
4582 inc = cp_convert (argtype, inc);
4584 /* Handle incrementing a cast-expression. */
4586 switch (TREE_CODE (arg))
4591 case FIX_TRUNC_EXPR:
4592 case FIX_FLOOR_EXPR:
4593 case FIX_ROUND_EXPR:
4596 tree incremented, modify, value, compound;
4597 if (! lvalue_p (arg) && pedantic)
4598 pedwarn ("cast to non-reference type used as lvalue");
4599 arg = stabilize_reference (arg);
4600 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4603 value = save_expr (arg);
4604 incremented = build (((code == PREINCREMENT_EXPR
4605 || code == POSTINCREMENT_EXPR)
4606 ? PLUS_EXPR : MINUS_EXPR),
4607 argtype, value, inc);
4608 TREE_SIDE_EFFECTS (incremented) = 1;
4610 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4611 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4613 /* Eliminate warning about unused result of + or -. */
4614 TREE_NO_UNUSED_WARNING (compound) = 1;
4622 /* Complain about anything else that is not a true lvalue. */
4623 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4624 || code == POSTINCREMENT_EXPR)
4625 ? "increment" : "decrement")))
4626 return error_mark_node;
4628 /* Forbid using -- on `bool'. */
4629 if (TREE_TYPE (arg) == boolean_type_node)
4631 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4633 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4634 return error_mark_node;
4637 /* This will only work if someone can convince Kenner to accept
4638 my patch to expand_increment. (jason) */
4639 val = build (code, TREE_TYPE (arg), arg, inc);
4641 if (code == POSTINCREMENT_EXPR)
4643 arg = stabilize_reference (arg);
4644 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4646 TREE_SIDE_EFFECTS (val) = 1;
4647 arg = save_expr (arg);
4648 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4649 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4652 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4657 val = build (code, TREE_TYPE (arg), arg, inc);
4659 TREE_SIDE_EFFECTS (val) = 1;
4660 return cp_convert (result_type, val);
4664 /* Note that this operation never does default_conversion
4665 regardless of NOCONVERT. */
4667 argtype = lvalue_type (arg);
4668 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4672 build_pointer_type (TREE_TYPE (argtype)), arg);
4673 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4676 else if (pedantic && DECL_MAIN_P (arg))
4678 pedwarn ("taking address of function `main'");
4680 /* Let &* cancel out to simplify resulting code. */
4681 if (TREE_CODE (arg) == INDIRECT_REF)
4683 /* We don't need to have `current_class_ptr' wrapped in a
4684 NON_LVALUE_EXPR node. */
4685 if (arg == current_class_ref)
4686 return current_class_ptr;
4688 arg = TREE_OPERAND (arg, 0);
4689 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4693 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4694 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4696 else if (lvalue_p (arg))
4697 /* Don't let this be an lvalue. */
4698 return non_lvalue (arg);
4702 /* For &x[y], return x+y */
4703 if (TREE_CODE (arg) == ARRAY_REF)
4705 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4706 return error_mark_node;
4707 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4708 TREE_OPERAND (arg, 1));
4711 /* Uninstantiated types are all functions. Taking the
4712 address of a function is a no-op, so just return the
4715 if (TREE_CODE (arg) == IDENTIFIER_NODE
4716 && IDENTIFIER_OPNAME_P (arg))
4718 my_friendly_abort (117);
4719 /* We don't know the type yet, so just work around the problem.
4720 We know that this will resolve to an lvalue. */
4721 return build1 (ADDR_EXPR, unknown_type_node, arg);
4724 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4725 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4727 /* They're trying to take the address of a unique non-static
4728 member function. This is ill-formed, but let's try to DTRT. */
4731 if (current_class_type
4732 && TREE_OPERAND (arg, 0) == current_class_ref)
4733 /* An expression like &memfn. */
4734 pedwarn ("taking the address of a non-static member function");
4736 pedwarn ("taking the address of a bound member function");
4738 base = TREE_TYPE (TREE_OPERAND (arg, 0));
4739 name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4741 cp_pedwarn (" to form a pointer to member function, say `&%T::%D'",
4743 arg = build_offset_ref (base, name);
4746 if (type_unknown_p (arg))
4747 return build1 (ADDR_EXPR, unknown_type_node, arg);
4749 /* Handle complex lvalues (when permitted)
4750 by reduction to simpler cases. */
4751 val = unary_complex_lvalue (code, arg);
4755 switch (TREE_CODE (arg))
4760 case FIX_TRUNC_EXPR:
4761 case FIX_FLOOR_EXPR:
4762 case FIX_ROUND_EXPR:
4764 if (! lvalue_p (arg) && pedantic)
4765 pedwarn ("taking the address of a cast to non-reference type");
4772 /* Allow the address of a constructor if all the elements
4774 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4775 && TREE_CONSTANT (arg))
4777 /* Anything not already handled and not a true memory reference
4779 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4780 && TREE_CODE (argtype) != METHOD_TYPE
4781 && !lvalue_or_else (arg, "unary `&'"))
4782 return error_mark_node;
4784 if (argtype != error_mark_node)
4785 argtype = build_pointer_type (argtype);
4787 if (mark_addressable (arg) == 0)
4788 return error_mark_node;
4793 if (TREE_CODE (arg) == COMPONENT_REF)
4794 addr = build_component_addr (arg, argtype);
4796 addr = build1 (ADDR_EXPR, argtype, arg);
4798 /* Address of a static or external variable or
4799 function counts as a constant */
4801 TREE_CONSTANT (addr) = 1;
4803 if (TREE_CODE (argtype) == POINTER_TYPE
4804 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4806 build_ptrmemfunc_type (argtype);
4807 addr = build_ptrmemfunc (argtype, addr, 0);
4820 argtype = TREE_TYPE (arg);
4821 return fold (build1 (code, argtype, arg));
4825 return error_mark_node;
4829 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4830 convert ARG with the same conversions in the same order
4831 and return the result. */
4834 convert_sequence (conversions, arg)
4838 switch (TREE_CODE (conversions))
4843 case FIX_TRUNC_EXPR:
4844 case FIX_FLOOR_EXPR:
4845 case FIX_ROUND_EXPR:
4847 return cp_convert (TREE_TYPE (conversions),
4848 convert_sequence (TREE_OPERAND (conversions, 0),
4857 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4858 for certain kinds of expressions which are not really lvalues
4859 but which we can accept as lvalues.
4861 If ARG is not a kind of expression we can handle, return zero. */
4864 unary_complex_lvalue (code, arg)
4865 enum tree_code code;
4868 /* Handle (a, b) used as an "lvalue". */
4869 if (TREE_CODE (arg) == COMPOUND_EXPR)
4871 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4872 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4873 TREE_OPERAND (arg, 0), real_result);
4876 /* Handle (a ? b : c) used as an "lvalue". */
4877 if (TREE_CODE (arg) == COND_EXPR
4878 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4879 return rationalize_conditional_expr (code, arg);
4881 if (TREE_CODE (arg) == MODIFY_EXPR
4882 || TREE_CODE (arg) == PREINCREMENT_EXPR
4883 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4884 return unary_complex_lvalue
4885 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4886 arg, TREE_OPERAND (arg, 0)));
4888 if (code != ADDR_EXPR)
4891 /* Handle (a = b) used as an "lvalue" for `&'. */
4892 if (TREE_CODE (arg) == MODIFY_EXPR
4893 || TREE_CODE (arg) == INIT_EXPR)
4895 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4896 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4897 TREE_NO_UNUSED_WARNING (arg) = 1;
4901 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4902 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4903 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4905 /* The representation of something of type OFFSET_TYPE
4906 is really the representation of a pointer to it.
4907 Here give the representation its true type. */
4910 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4912 if (TREE_CODE (arg) != OFFSET_REF)
4915 t = TREE_OPERAND (arg, 1);
4917 /* Check all this code for right semantics. */
4918 if (TREE_CODE (t) == FUNCTION_DECL)
4920 if (DECL_DESTRUCTOR_P (t))
4921 cp_error ("taking address of destructor");
4922 return build_unary_op (ADDR_EXPR, t, 0);
4924 if (TREE_CODE (t) == VAR_DECL)
4925 return build_unary_op (ADDR_EXPR, t, 0);
4930 if (TREE_OPERAND (arg, 0)
4931 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4932 && TREE_CODE (t) != FIELD_DECL)
4934 cp_error ("taking address of bound pointer-to-member expression");
4935 return error_mark_node;
4938 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4939 type = build_pointer_type (type);
4941 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4947 /* We permit compiler to make function calls returning
4948 objects of aggregate type look like lvalues. */
4952 if (TREE_CODE (targ) == SAVE_EXPR)
4953 targ = TREE_OPERAND (targ, 0);
4955 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4957 if (TREE_CODE (arg) == SAVE_EXPR)
4960 targ = build_cplus_new (TREE_TYPE (arg), arg);
4961 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4964 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4965 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4966 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4969 /* Don't let anything else be handled specially. */
4973 /* Mark EXP saying that we need to be able to take the
4974 address of it; it should not be allocated in a register.
4975 Value is 1 if successful.
4977 C++: we do not allow `current_class_ptr' to be addressable. */
4980 mark_addressable (exp)
4983 register tree x = exp;
4985 if (TREE_ADDRESSABLE (x) == 1)
4989 switch (TREE_CODE (x))
4996 x = TREE_OPERAND (x, 0);
5000 if (x == current_class_ptr)
5002 if (! flag_this_is_variable)
5003 error ("address of `this' not available");
5004 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
5005 put_var_into_stack (x);
5009 if (TREE_STATIC (x) && TREE_READONLY (x)
5010 && DECL_RTL (x) != 0
5011 && ! DECL_IN_MEMORY_P (x))
5013 /* We thought this would make a good constant variable,
5014 but we were wrong. */
5015 push_obstacks_nochange ();
5016 end_temporary_allocation ();
5018 TREE_ASM_WRITTEN (x) = 0;
5020 rest_of_decl_compilation (x, 0,
5021 !DECL_FUNCTION_SCOPE_P (x),
5023 TREE_ADDRESSABLE (x) = 1;
5029 /* Caller should not be trying to mark initialized
5030 constant fields addressable. */
5031 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
5032 || DECL_IN_AGGR_P (x) == 0
5034 || DECL_EXTERNAL (x), 314);
5038 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5039 && !DECL_ARTIFICIAL (x) && extra_warnings)
5040 cp_warning ("address requested for `%D', which is declared `register'",
5042 put_var_into_stack (x);
5043 TREE_ADDRESSABLE (x) = 1;
5047 if (DECL_LANG_SPECIFIC (x) != 0)
5049 x = DECL_MAIN_VARIANT (x);
5050 /* We have to test both conditions here. The first may be
5051 non-zero in the case of processing a default function. The
5052 second may be non-zero in the case of a template function. */
5053 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
5056 TREE_ADDRESSABLE (x) = 1;
5058 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
5062 TREE_ADDRESSABLE (x) = 1;
5066 TREE_ADDRESSABLE (x) = 1;
5067 mark_addressable (TREE_OPERAND (x, 0));
5075 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5078 build_x_conditional_expr (ifexp, op1, op2)
5079 tree ifexp, op1, op2;
5081 if (processing_template_decl)
5082 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5084 return build_conditional_expr (ifexp, op1, op2);
5087 /* Handle overloading of the ',' operator when needed. Otherwise,
5088 this function just builds an expression list. */
5091 build_x_compound_expr (list)
5094 tree rest = TREE_CHAIN (list);
5097 if (processing_template_decl)
5098 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5100 if (rest == NULL_TREE)
5101 return build_compound_expr (list);
5103 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5104 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5106 return build_x_compound_expr (expr_tree_cons (NULL_TREE, result,
5107 TREE_CHAIN (rest)));
5109 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5111 /* the left-hand operand of a comma expression is like an expression
5112 statement: we should warn if it doesn't have any side-effects,
5113 unless it was explicitly cast to (void). */
5114 if ((extra_warnings || warn_unused)
5115 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5116 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5117 warning("left-hand operand of comma expression has no effect");
5119 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5120 else if (warn_unused)
5121 warn_if_unused_value (TREE_VALUE(list));
5124 return build_compound_expr
5125 (expr_tree_cons (NULL_TREE, TREE_VALUE (list),
5126 build_expr_list (NULL_TREE,
5127 build_x_compound_expr (rest))));
5130 /* Given a list of expressions, return a compound expression
5131 that performs them all and returns the value of the last of them. */
5134 build_compound_expr (list)
5140 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5141 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5143 if (TREE_CHAIN (list) == 0)
5145 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5146 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5147 if (TREE_CODE (list) == NOP_EXPR
5148 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5149 list = TREE_OPERAND (list, 0);
5151 /* Convert arrays to pointers. */
5152 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5153 return default_conversion (TREE_VALUE (list));
5155 return TREE_VALUE (list);
5158 first = TREE_VALUE (list);
5159 first = require_complete_type_in_void (first);
5160 if (first == error_mark_node)
5161 return error_mark_node;
5163 rest = build_compound_expr (TREE_CHAIN (list));
5164 if (rest == error_mark_node)
5165 return error_mark_node;
5167 /* When pedantic, a compound expression cannot be a constant expression. */
5168 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5171 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5172 break_out_cleanups (first), rest);
5176 build_static_cast (type, expr)
5182 if (type == error_mark_node || expr == error_mark_node)
5183 return error_mark_node;
5185 if (TREE_CODE (expr) == OFFSET_REF)
5186 expr = resolve_offset_ref (expr);
5188 if (processing_template_decl)
5190 tree t = build_min (STATIC_CAST_EXPR, copy_to_permanent (type),
5195 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5196 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5197 if (TREE_CODE (type) != REFERENCE_TYPE
5198 && TREE_CODE (expr) == NOP_EXPR
5199 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5200 expr = TREE_OPERAND (expr, 0);
5202 if (TREE_CODE (type) == VOID_TYPE)
5203 return build1 (CONVERT_EXPR, type, expr);
5205 if (TREE_CODE (type) == REFERENCE_TYPE)
5206 return (convert_from_reference
5207 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5208 LOOKUP_COMPLAIN, NULL_TREE)));
5210 if (IS_AGGR_TYPE (type))
5211 return build_cplus_new
5212 (type, (build_method_call
5213 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5214 TYPE_BINFO (type), LOOKUP_NORMAL)));
5216 expr = decay_conversion (expr);
5217 intype = TREE_TYPE (expr);
5219 /* FIXME handle casting to array type. */
5222 if (can_convert_arg (type, intype, expr))
5224 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5227 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5228 && at_least_as_qualified_p (TREE_TYPE (type),
5230 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5231 && ! TREE_VIA_VIRTUAL (binfo))
5234 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5236 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5237 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))))
5238 && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type)),
5239 TREE_TYPE (TREE_TYPE (intype)))
5240 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5241 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5242 && ! TREE_VIA_VIRTUAL (binfo))
5245 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5246 && TREE_CODE (type) != ARRAY_TYPE
5247 && TREE_CODE (type) != FUNCTION_TYPE
5248 && can_convert (intype, type))
5251 /* [expr.static.cast]
5253 The static_cast operator shall not be used to cast away
5255 if (ok && casts_away_constness (intype, type))
5257 cp_error ("static_cast from `%T' to `%T' casts away constness",
5259 return error_mark_node;
5263 return build_c_cast (type, expr);
5265 cp_error ("static_cast from `%T' to `%T'", intype, type);
5266 return error_mark_node;
5270 build_reinterpret_cast (type, expr)
5275 if (type == error_mark_node || expr == error_mark_node)
5276 return error_mark_node;
5278 if (TREE_CODE (expr) == OFFSET_REF)
5279 expr = resolve_offset_ref (expr);
5281 if (processing_template_decl)
5283 tree t = build_min (REINTERPRET_CAST_EXPR,
5284 copy_to_permanent (type), expr);
5288 if (TREE_CODE (type) != REFERENCE_TYPE)
5290 expr = decay_conversion (expr);
5292 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5293 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5294 if (TREE_CODE (expr) == NOP_EXPR
5295 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5296 expr = TREE_OPERAND (expr, 0);
5299 intype = TREE_TYPE (expr);
5301 if (TREE_CODE (type) == REFERENCE_TYPE)
5303 if (! real_lvalue_p (expr))
5305 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5306 return error_mark_node;
5308 expr = build_unary_op (ADDR_EXPR, expr, 0);
5309 if (expr != error_mark_node)
5310 expr = build_reinterpret_cast
5311 (build_pointer_type (TREE_TYPE (type)), expr);
5312 if (expr != error_mark_node)
5313 expr = build_indirect_ref (expr, 0);
5316 else if (same_type_p (TYPE_MAIN_VARIANT (intype),
5317 TYPE_MAIN_VARIANT (type)))
5318 return build_static_cast (type, expr);
5320 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5321 || TREE_CODE (intype) == ENUMERAL_TYPE))
5323 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5325 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5326 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5329 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5330 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5332 if (TREE_READONLY_DECL_P (expr))
5333 expr = decl_constant_value (expr);
5334 return fold (build1 (NOP_EXPR, type, expr));
5336 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5337 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5339 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5340 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5343 if (TREE_READONLY_DECL_P (expr))
5344 expr = decl_constant_value (expr);
5345 return fold (build1 (NOP_EXPR, type, expr));
5347 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5348 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5350 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5351 if (TREE_READONLY_DECL_P (expr))
5352 expr = decl_constant_value (expr);
5353 return fold (build1 (NOP_EXPR, type, expr));
5357 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5358 return error_mark_node;
5361 return cp_convert (type, expr);
5365 build_const_cast (type, expr)
5370 if (type == error_mark_node || expr == error_mark_node)
5371 return error_mark_node;
5373 if (TREE_CODE (expr) == OFFSET_REF)
5374 expr = resolve_offset_ref (expr);
5376 if (processing_template_decl)
5378 tree t = build_min (CONST_CAST_EXPR, copy_to_permanent (type),
5383 if (!POINTER_TYPE_P (type))
5385 cp_error ("`%T' is not a pointer, reference, or pointer-to-data-member type",
5387 cp_error ("as required by const_cast");
5389 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5391 cp_error ("`%T' is a pointer or reference to a function type",
5393 cp_error ("which is forbidden by const_cast");
5394 return error_mark_node;
5397 if (TREE_CODE (type) != REFERENCE_TYPE)
5399 expr = decay_conversion (expr);
5401 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5402 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5403 if (TREE_CODE (expr) == NOP_EXPR
5404 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5405 expr = TREE_OPERAND (expr, 0);
5408 intype = TREE_TYPE (expr);
5410 if (same_type_p (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type)))
5411 return build_static_cast (type, expr);
5412 else if (TREE_CODE (type) == REFERENCE_TYPE)
5414 if (! real_lvalue_p (expr))
5416 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5417 return error_mark_node;
5420 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5422 expr = build_unary_op (ADDR_EXPR, expr, 0);
5423 expr = build1 (NOP_EXPR, type, expr);
5424 return convert_from_reference (expr);
5427 else if (TREE_CODE (type) == POINTER_TYPE
5428 && TREE_CODE (intype) == POINTER_TYPE
5429 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5430 return cp_convert (type, expr);
5432 cp_error ("const_cast from `%T' to `%T'", intype, type);
5433 return error_mark_node;
5436 /* Build an expression representing a cast to type TYPE of expression EXPR.
5438 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5439 when doing the cast. */
5442 build_c_cast (type, expr)
5445 register tree value = expr;
5448 if (type == error_mark_node || expr == error_mark_node)
5449 return error_mark_node;
5451 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5452 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5453 if (TREE_CODE (type) != REFERENCE_TYPE
5454 && TREE_CODE (value) == NOP_EXPR
5455 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5456 value = TREE_OPERAND (value, 0);
5458 if (TREE_CODE (value) == OFFSET_REF)
5459 value = resolve_offset_ref (value);
5461 if (TREE_CODE (type) == ARRAY_TYPE)
5463 /* Allow casting from T1* to T2[] because Cfront allows it.
5464 NIHCL uses it. It is not valid ANSI C however, and hence, not
5466 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5469 pedwarn ("ANSI C++ forbids casting to an array type");
5470 type = build_pointer_type (TREE_TYPE (type));
5474 error ("ANSI C++ forbids casting to an array type");
5475 return error_mark_node;
5479 if (TREE_CODE (type) == FUNCTION_TYPE
5480 || TREE_CODE (type) == METHOD_TYPE)
5482 cp_error ("casting to function type `%T'", type);
5483 return error_mark_node;
5486 if (IS_SIGNATURE (type))
5488 error ("cast specifies signature type");
5489 return error_mark_node;
5492 if (processing_template_decl)
5494 tree t = build_min (CAST_EXPR, type,
5495 min_tree_cons (NULL_TREE, value, NULL_TREE));
5499 /* Convert functions and arrays to pointers and
5500 convert references to their expanded types,
5501 but don't convert any other types. If, however, we are
5502 casting to a class type, there's no reason to do this: the
5503 cast will only succeed if there is a converting constructor,
5504 and the default conversions will be done at that point. In
5505 fact, doing the default conversion here is actually harmful
5509 struct S { S(const A&); };
5511 since we don't want the array-to-pointer conversion done. */
5512 if (!IS_AGGR_TYPE (type))
5514 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5515 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5516 /* Don't do the default conversion on a ->* expression. */
5517 && ! (TREE_CODE (type) == POINTER_TYPE
5518 && bound_pmf_p (value)))
5519 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5520 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5521 value = default_conversion (value);
5523 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5524 /* However, even for class types, we still need to strip away
5525 the reference type, since the call to convert_force below
5526 does not expect the input expression to be of reference
5528 value = convert_from_reference (value);
5530 otype = TREE_TYPE (value);
5532 /* Optionally warn about potentially worrisome casts. */
5535 && TREE_CODE (type) == POINTER_TYPE
5536 && TREE_CODE (otype) == POINTER_TYPE
5537 && !at_least_as_qualified_p (TREE_TYPE (type),
5539 cp_warning ("cast discards qualifiers from pointer target type");
5541 /* Warn about possible alignment problems. */
5542 if (STRICT_ALIGNMENT && warn_cast_align
5543 && TREE_CODE (type) == POINTER_TYPE
5544 && TREE_CODE (otype) == POINTER_TYPE
5545 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5546 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5547 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5548 warning ("cast increases required alignment of target type");
5551 /* We should see about re-enabling these, they seem useful to
5553 if (TREE_CODE (type) == INTEGER_TYPE
5554 && TREE_CODE (otype) == POINTER_TYPE
5555 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5556 warning ("cast from pointer to integer of different size");
5558 if (TREE_CODE (type) == POINTER_TYPE
5559 && TREE_CODE (otype) == INTEGER_TYPE
5560 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5561 /* Don't warn about converting 0 to pointer,
5562 provided the 0 was explicit--not cast or made by folding. */
5563 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5564 warning ("cast to pointer from integer of different size");
5567 if (TREE_CODE (type) == VOID_TYPE)
5569 value = require_complete_type_in_void (value);
5570 if (value != error_mark_node)
5571 value = build1 (CONVERT_EXPR, void_type_node, value);
5573 else if (TREE_CODE (type) == REFERENCE_TYPE)
5574 value = (convert_from_reference
5575 (convert_to_reference (type, value, CONV_C_CAST,
5576 LOOKUP_COMPLAIN, NULL_TREE)));
5581 if (TREE_READONLY_DECL_P (value))
5582 value = decl_constant_value (value);
5585 value = convert_force (type, value, CONV_C_CAST);
5587 /* Ignore any integer overflow caused by the cast. */
5588 if (TREE_CODE (value) == INTEGER_CST)
5590 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5591 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5595 /* Always produce some operator for an explicit cast,
5596 so we can tell (for -pedantic) that the cast is no lvalue. */
5597 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5598 && real_lvalue_p (value))
5599 value = non_lvalue (value);
5604 /* Build an assignment expression of lvalue LHS from value RHS.
5605 MODIFYCODE is the code for a binary operator that we use
5606 to combine the old value of LHS with RHS to get the new value.
5607 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5609 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5612 build_modify_expr (lhs, modifycode, rhs)
5614 enum tree_code modifycode;
5617 register tree result;
5619 tree lhstype = TREE_TYPE (lhs);
5620 tree olhstype = lhstype;
5623 /* Avoid duplicate error messages from operands that had errors. */
5624 if (lhs == error_mark_node || rhs == error_mark_node)
5625 return error_mark_node;
5627 /* Types that aren't fully specified cannot be used in assignments. */
5628 lhs = require_complete_type (lhs);
5632 /* Handle assignment to signature pointers/refs. */
5634 if (TYPE_LANG_SPECIFIC (lhstype)
5635 && (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5637 return build_signature_pointer_constructor (lhs, rhs);
5640 /* Handle control structure constructs used as "lvalues". */
5642 switch (TREE_CODE (lhs))
5644 /* Handle --foo = 5; as these are valid constructs in C++ */
5645 case PREDECREMENT_EXPR:
5646 case PREINCREMENT_EXPR:
5647 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5648 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5649 stabilize_reference (TREE_OPERAND (lhs, 0)),
5650 TREE_OPERAND (lhs, 1));
5651 return build (COMPOUND_EXPR, lhstype,
5653 build_modify_expr (TREE_OPERAND (lhs, 0),
5656 /* Handle (a, b) used as an "lvalue". */
5658 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5660 if (newrhs == error_mark_node)
5661 return error_mark_node;
5662 return build (COMPOUND_EXPR, lhstype,
5663 TREE_OPERAND (lhs, 0), newrhs);
5666 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5667 if (newrhs == error_mark_node)
5668 return error_mark_node;
5669 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5671 /* Handle (a ? b : c) used as an "lvalue". */
5673 rhs = save_expr (rhs);
5675 /* Produce (a ? (b = rhs) : (c = rhs))
5676 except that the RHS goes through a save-expr
5677 so the code to compute it is only emitted once. */
5679 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5680 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5682 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5684 if (cond == error_mark_node)
5686 /* Make sure the code to compute the rhs comes out
5687 before the split. */
5688 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5689 /* Case to void to suppress warning
5690 from warn_if_unused_value. */
5691 cp_convert (void_type_node, rhs), cond);
5698 if (TREE_CODE (lhs) == OFFSET_REF)
5700 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5702 /* Static class member? */
5703 tree member = TREE_OPERAND (lhs, 1);
5704 if (TREE_CODE (member) == VAR_DECL)
5708 compiler_error ("invalid static class member");
5709 return error_mark_node;
5713 lhs = resolve_offset_ref (lhs);
5715 olhstype = lhstype = TREE_TYPE (lhs);
5718 if (lhs == error_mark_node)
5721 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5722 && modifycode != INIT_EXPR)
5724 lhs = convert_from_reference (lhs);
5725 olhstype = lhstype = TREE_TYPE (lhs);
5728 /* If a binary op has been requested, combine the old LHS value with the RHS
5729 producing the value we should actually store into the LHS. */
5731 if (modifycode == INIT_EXPR)
5733 if (! IS_AGGR_TYPE (lhstype))
5734 /* Do the default thing */;
5737 result = build_method_call (lhs, ctor_identifier,
5738 build_expr_list (NULL_TREE, rhs),
5739 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5740 if (result == NULL_TREE)
5741 return error_mark_node;
5745 else if (modifycode == NOP_EXPR)
5747 /* `operator=' is not an inheritable operator. */
5748 if (! IS_AGGR_TYPE (lhstype))
5749 /* Do the default thing */;
5752 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5753 lhs, rhs, make_node (NOP_EXPR));
5754 if (result == NULL_TREE)
5755 return error_mark_node;
5760 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5762 my_friendly_abort (978652);
5766 lhs = stabilize_reference (lhs);
5767 newrhs = build_binary_op (modifycode, lhs, rhs);
5768 if (newrhs == error_mark_node)
5770 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5771 TREE_TYPE (lhs), TREE_TYPE (rhs));
5772 return error_mark_node;
5776 /* Handle a cast used as an "lvalue".
5777 We have already performed any binary operator using the value as cast.
5778 Now convert the result to the cast type of the lhs,
5779 and then true type of the lhs and store it there;
5780 then convert result back to the cast type to be the value
5781 of the assignment. */
5783 switch (TREE_CODE (lhs))
5788 case FIX_TRUNC_EXPR:
5789 case FIX_FLOOR_EXPR:
5790 case FIX_ROUND_EXPR:
5792 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5793 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5794 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5795 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5796 newrhs = default_conversion (newrhs);
5798 tree inner_lhs = TREE_OPERAND (lhs, 0);
5801 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5802 otherwise the result is an rvalue. */
5803 if (! lvalue_p (lhs))
5804 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5806 result = build_modify_expr (inner_lhs, NOP_EXPR,
5807 cp_convert (TREE_TYPE (inner_lhs),
5808 cp_convert (lhstype, newrhs)));
5809 if (result == error_mark_node)
5811 return cp_convert (TREE_TYPE (lhs), result);
5818 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5819 Reject anything strange now. */
5821 if (!lvalue_or_else (lhs, "assignment"))
5822 return error_mark_node;
5824 GNU_xref_assign (lhs);
5826 /* Warn about storing in something that is `const'. */
5827 /* For C++, don't warn if this is initialization. */
5828 if (modifycode != INIT_EXPR
5829 /* For assignment to `const' signature pointer/reference fields,
5830 don't warn either, we already printed a better message before. */
5831 && ! (TREE_CODE (lhs) == COMPONENT_REF
5832 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5833 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5834 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5835 /* Functions are not modifiable, even though they are
5837 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5838 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5839 && C_TYPE_FIELDS_READONLY (lhstype))
5840 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5841 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5842 readonly_error (lhs, "assignment", 0);
5844 /* If storing into a structure or union member,
5845 it has probably been given type `int'.
5846 Compute the type that would go with
5847 the actual amount of storage the member occupies. */
5849 if (TREE_CODE (lhs) == COMPONENT_REF
5850 && (TREE_CODE (lhstype) == INTEGER_TYPE
5851 || TREE_CODE (lhstype) == REAL_TYPE
5852 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5854 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5856 /* If storing in a field that is in actuality a short or narrower
5857 than one, we must store in the field in its actual type. */
5859 if (lhstype != TREE_TYPE (lhs))
5861 lhs = copy_node (lhs);
5862 TREE_TYPE (lhs) = lhstype;
5866 /* check to see if there is an assignment to `this' */
5867 if (lhs == current_class_ptr)
5869 if (flag_this_is_variable > 0
5870 && DECL_NAME (current_function_decl) != NULL_TREE
5871 && (DECL_NAME (current_function_decl)
5872 != constructor_name (current_class_type)))
5873 warning ("assignment to `this' not in constructor or destructor");
5874 current_function_just_assigned_this = 1;
5877 if (modifycode != INIT_EXPR)
5879 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5880 modifycode = NOP_EXPR;
5881 /* Reference-bashing */
5882 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5884 tree tmp = convert_from_reference (lhs);
5885 lhstype = TREE_TYPE (tmp);
5886 if (TYPE_SIZE (lhstype) == 0)
5888 incomplete_type_error (lhs, lhstype);
5889 return error_mark_node;
5894 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5896 tree tmp = convert_from_reference (newrhs);
5897 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5899 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5900 return error_mark_node;
5906 if (TREE_SIDE_EFFECTS (lhs))
5907 lhs = stabilize_reference (lhs);
5908 if (TREE_SIDE_EFFECTS (newrhs))
5909 newrhs = stabilize_reference (newrhs);
5911 /* Convert new value to destination type. */
5913 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5917 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5919 cp_error ("incompatible types in assignment of `%T' to `%T'",
5920 TREE_TYPE (rhs), lhstype);
5921 return error_mark_node;
5924 /* Allow array assignment in compiler-generated code. */
5925 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5926 pedwarn ("ANSI C++ forbids assignment of arrays");
5928 /* Have to wrap this in RTL_EXPR for two cases:
5929 in base or member initialization and if we
5930 are a branch of a ?: operator. Since we
5931 can't easily know the latter, just do it always. */
5933 result = make_node (RTL_EXPR);
5935 TREE_TYPE (result) = void_type_node;
5936 do_pending_stack_adjust ();
5937 start_sequence_for_rtl_expr (result);
5939 /* As a matter of principle, `start_sequence' should do this. */
5942 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5943 ? 1 + (modifycode != INIT_EXPR): 0;
5944 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
5947 do_pending_stack_adjust ();
5949 TREE_SIDE_EFFECTS (result) = 1;
5950 RTL_EXPR_SEQUENCE (result) = get_insns ();
5951 RTL_EXPR_RTL (result) = const0_rtx;
5956 if (modifycode == INIT_EXPR)
5958 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5959 "assignment", NULL_TREE, 0);
5960 if (lhs == DECL_RESULT (current_function_decl))
5962 if (DECL_INITIAL (lhs))
5963 warning ("return value from function receives multiple initializations");
5964 DECL_INITIAL (lhs) = newrhs;
5969 /* Avoid warnings on enum bit fields. */
5970 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5971 && TREE_CODE (lhstype) == INTEGER_TYPE)
5973 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5975 newrhs = convert_force (lhstype, newrhs, 0);
5978 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5980 if (TREE_CODE (newrhs) == CALL_EXPR
5981 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5982 newrhs = build_cplus_new (lhstype, newrhs);
5984 /* Can't initialize directly from a TARGET_EXPR, since that would
5985 cause the lhs to be constructed twice, and possibly result in
5986 accidental self-initialization. So we force the TARGET_EXPR to be
5987 expanded without a target. */
5988 if (TREE_CODE (newrhs) == TARGET_EXPR)
5989 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5990 TREE_OPERAND (newrhs, 0));
5993 if (newrhs == error_mark_node)
5994 return error_mark_node;
5996 if (TREE_CODE (newrhs) == COND_EXPR)
5999 tree cond = TREE_OPERAND (newrhs, 0);
6001 if (TREE_SIDE_EFFECTS (lhs))
6002 cond = build_compound_expr (tree_cons
6004 build_expr_list (NULL_TREE, cond)));
6006 /* Cannot have two identical lhs on this one tree (result) as preexpand
6007 calls will rip them out and fill in RTL for them, but when the
6008 rtl is generated, the calls will only be in the first side of the
6009 condition, not on both, or before the conditional jump! (mrs) */
6010 lhs1 = break_out_calls (lhs);
6013 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6014 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6015 lhstype, lhs, newrhs);
6018 tree result_type = TREE_TYPE (newrhs);
6019 /* We have to convert each arm to the proper type because the
6020 types may have been munged by constant folding. */
6022 = build (COND_EXPR, result_type, cond,
6023 build_modify_expr (lhs, modifycode,
6024 cp_convert (result_type,
6025 TREE_OPERAND (newrhs, 1))),
6026 build_modify_expr (lhs1, modifycode,
6027 cp_convert (result_type,
6028 TREE_OPERAND (newrhs, 2))));
6032 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6033 lhstype, lhs, newrhs);
6035 TREE_SIDE_EFFECTS (result) = 1;
6037 /* If we got the LHS in a different type for storing in,
6038 convert the result back to the nominal type of LHS
6039 so that the value we return always has the same type
6040 as the LHS argument. */
6042 if (olhstype == TREE_TYPE (result))
6044 /* Avoid warnings converting integral types back into enums
6045 for enum bit fields. */
6046 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6047 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6049 result = build (COMPOUND_EXPR, olhstype, result, olhs);
6050 TREE_NO_UNUSED_WARNING (result) = 1;
6053 return convert_for_assignment (olhstype, result, "assignment",
6058 build_x_modify_expr (lhs, modifycode, rhs)
6060 enum tree_code modifycode;
6063 if (processing_template_decl)
6064 return build_min_nt (MODOP_EXPR, lhs,
6065 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6067 if (modifycode != NOP_EXPR)
6069 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6070 make_node (modifycode));
6074 return build_modify_expr (lhs, modifycode, rhs);
6078 /* Get difference in deltas for different pointer to member function
6079 types. Return integer_zero_node, if FROM cannot be converted to a
6080 TO type. If FORCE is true, then allow reverse conversions as well.
6082 Note that the naming of FROM and TO is kind of backwards; the return
6083 value is what we add to a TO in order to get a FROM. They are named
6084 this way because we call this function to find out how to convert from
6085 a pointer to member of FROM to a pointer to member of TO. */
6088 get_delta_difference (from, to, force)
6092 tree delta = integer_zero_node;
6098 /* Should get_base_distance here, so we can check if any thing along the
6099 path is virtual, and we need to make sure we stay
6100 inside the real binfos when going through virtual bases.
6101 Maybe we should replace virtual bases with
6102 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6103 binfo = get_binfo (from, to, 1);
6104 if (binfo == error_mark_node)
6106 error (" in pointer to member function conversion");
6113 error_not_base_type (from, to);
6114 error (" in pointer to member conversion");
6117 binfo = get_binfo (to, from, 1);
6118 if (binfo == 0 || binfo == error_mark_node)
6120 if (TREE_VIA_VIRTUAL (binfo))
6122 binfo = binfo_member (BINFO_TYPE (binfo),
6123 CLASSTYPE_VBASECLASSES (from));
6124 cp_warning ("pointer to member cast to virtual base `%T'",
6125 BINFO_TYPE (binfo));
6126 warning (" will only work if you are very careful");
6128 delta = BINFO_OFFSET (binfo);
6129 delta = cp_convert (ptrdiff_type_node, delta);
6131 return build_binary_op (MINUS_EXPR,
6136 if (TREE_VIA_VIRTUAL (binfo))
6140 cp_warning ("pointer to member cast from virtual base `%T'",
6141 BINFO_TYPE (binfo));
6142 warning (" will only work if you are very careful");
6145 cp_error ("pointer to member conversion from virtual base `%T'",
6146 BINFO_TYPE (binfo));
6149 return BINFO_OFFSET (binfo);
6153 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6154 tree type, delta, idx, pfn, delta2;
6159 /* This is the old way we did it. We want to avoid calling
6160 digest_init, so that it can give an error if we use { } when
6161 initializing a pointer to member function. */
6165 u = build_nt (CONSTRUCTOR, NULL_TREE,
6166 expr_tree_cons (pfn_identifier, pfn, NULL_TREE));
6170 u = build_nt (CONSTRUCTOR, NULL_TREE,
6171 expr_tree_cons (delta2_identifier, delta2, NULL_TREE));
6174 u = build_nt (CONSTRUCTOR, NULL_TREE,
6175 expr_tree_cons (NULL_TREE, delta,
6176 expr_tree_cons (NULL_TREE, idx,
6177 expr_tree_cons (NULL_TREE, u, NULL_TREE))));
6179 return digest_init (type, u, (tree*)0);
6181 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6183 int allconstant, allsimple;
6185 delta_field = TYPE_FIELDS (type);
6186 idx_field = TREE_CHAIN (delta_field);
6187 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6188 subtype = TREE_TYPE (pfn_or_delta2_field);
6189 pfn_field = TYPE_FIELDS (subtype);
6190 delta2_field = TREE_CHAIN (pfn_field);
6194 allconstant = TREE_CONSTANT (pfn);
6195 allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6196 u = expr_tree_cons (pfn_field, pfn, NULL_TREE);
6200 delta2 = convert_and_check (delta_type_node, delta2);
6201 allconstant = TREE_CONSTANT (delta2);
6202 allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6203 u = expr_tree_cons (delta2_field, delta2, NULL_TREE);
6206 delta = convert_and_check (delta_type_node, delta);
6207 idx = convert_and_check (delta_type_node, idx);
6209 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6210 allsimple = allsimple
6211 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6212 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6214 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6215 u = expr_tree_cons (delta_field, delta,
6216 expr_tree_cons (idx_field, idx,
6217 expr_tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6218 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6219 TREE_CONSTANT (u) = allconstant;
6220 TREE_STATIC (u) = allconstant && allsimple;
6225 /* Build a constructor for a pointer to member function. It can be
6226 used to initialize global variables, local variable, or used
6227 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6230 If FORCE is non-zero, then force this conversion, even if
6231 we would rather not do it. Usually set when using an explicit
6234 Return error_mark_node, if something goes wrong. */
6237 build_ptrmemfunc (type, pfn, force)
6242 tree pfn_type = TREE_TYPE (pfn);
6243 tree to_type = build_ptrmemfunc_type (type);
6245 /* Handle multiple conversions of pointer to member functions. */
6246 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6248 tree idx = integer_zero_node;
6249 tree delta = integer_zero_node;
6250 tree delta2 = integer_zero_node;
6251 tree npfn = NULL_TREE;
6252 tree ndelta, ndelta2;
6256 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6257 cp_error ("conversion to `%T' from `%T'",
6260 if (TREE_CODE (pfn) == PTRMEM_CST)
6262 /* We could just build the resulting CONSTRUCTOR now, but we
6263 don't, relying on the general machinery below, together
6264 with constant-folding, to do the right thing. We don't
6265 want to return a PTRMEM_CST here, since a
6266 pointer-to-member constant is no longer a valid template
6267 argument once it is cast to any type, including its
6269 expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
6271 /* This constant points to a non-virtual function.
6272 NDELTA2 will be NULL, but it's value doesn't really
6273 matter since we won't use it anyhow. */
6274 ndelta2 = integer_zero_node;
6276 else if (same_type_p (to_type, pfn_type))
6277 /* We don't have to do any conversion. Note that we do this
6278 after checking for a PTRMEM_CST so that a PTRMEM_CST, cast
6279 to its own type, will not be considered a legal non-type
6280 template argument. */
6284 ndelta = cp_convert (ptrdiff_type_node,
6285 build_component_ref (pfn,
6288 ndelta2 = cp_convert (ptrdiff_type_node,
6289 DELTA2_FROM_PTRMEMFUNC (pfn));
6290 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6293 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6294 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6296 delta = build_binary_op (PLUS_EXPR, ndelta, n);
6297 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n);
6298 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6300 /* If it's a virtual function, this is what we want. */
6301 e2 = build_ptrmemfunc1 (to_type, delta, idx,
6304 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6305 npfn = build1 (NOP_EXPR, type, pfn);
6306 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6308 /* But if it's a non-virtual function, or NULL, we use this
6310 e3 = build_ptrmemfunc1 (to_type, delta,
6311 idx, npfn, NULL_TREE);
6312 return build_conditional_expr (e1, e2, e3);
6315 /* Handle null pointer to member function conversions. */
6316 if (integer_zerop (pfn))
6318 pfn = build_c_cast (type, integer_zero_node);
6319 return build_ptrmemfunc1 (to_type,
6320 integer_zero_node, integer_zero_node,
6324 if (type_unknown_p (pfn))
6325 return instantiate_type (type, pfn, 1);
6327 fn = TREE_OPERAND (pfn, 0);
6328 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6329 return make_ptrmem_cst (to_type, fn);
6332 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6336 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6343 tree type = TREE_TYPE (cst);
6344 tree fn = PTRMEM_CST_MEMBER (cst);
6345 tree ptr_class, fn_class;
6347 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6349 /* The class that the function belongs to. */
6350 fn_class = DECL_CLASS_CONTEXT (fn);
6352 /* The class that we're creating a pointer to member of. */
6353 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6355 /* First, calculate the adjustment to the function's class. */
6356 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6358 if (!DECL_VIRTUAL_P (fn))
6360 *idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6361 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6362 *delta2 = NULL_TREE;
6366 /* If we're dealing with a virtual function, we have to adjust 'this'
6367 again, to point to the base which provides the vtable entry for
6368 fn; the call will do the opposite adjustment. */
6369 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6370 tree binfo = binfo_or_else (orig_class, fn_class);
6371 *delta = size_binop (PLUS_EXPR, *delta, BINFO_OFFSET (binfo));
6373 /* Map everything down one to make room for the null PMF. */
6374 *idx = size_binop (PLUS_EXPR, DECL_VINDEX (fn), integer_one_node);
6377 /* Offset from an object of PTR_CLASS to the vptr for ORIG_CLASS. */
6378 *delta2 = size_binop (PLUS_EXPR, *delta,
6379 get_vfield_offset (TYPE_BINFO (orig_class)));
6383 /* Return an expression for DELTA2 from the pointer-to-member function
6387 delta2_from_ptrmemfunc (t)
6390 if (TREE_CODE (t) == PTRMEM_CST)
6397 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6402 return (build_component_ref
6403 (build_component_ref (t,
6404 pfn_or_delta2_identifier, NULL_TREE,
6406 delta2_identifier, NULL_TREE, 0));
6409 /* Return an expression for PFN from the pointer-to-member function
6413 pfn_from_ptrmemfunc (t)
6416 if (TREE_CODE (t) == PTRMEM_CST)
6423 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6428 return (build_component_ref
6429 (build_component_ref (t,
6430 pfn_or_delta2_identifier, NULL_TREE,
6432 pfn_identifier, NULL_TREE, 0));
6435 /* Convert value RHS to type TYPE as preparation for an assignment to
6436 an lvalue of type TYPE. ERRTYPE is a string to use in error
6437 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6438 are doing the conversion in order to pass the PARMNUMth argument of
6442 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6444 const char *errtype;
6448 register enum tree_code codel = TREE_CODE (type);
6449 register tree rhstype;
6450 register enum tree_code coder;
6452 if (codel == OFFSET_TYPE)
6453 my_friendly_abort (990505);
6455 if (TREE_CODE (rhs) == OFFSET_REF)
6456 rhs = resolve_offset_ref (rhs);
6458 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6459 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6460 rhs = TREE_OPERAND (rhs, 0);
6462 rhstype = TREE_TYPE (rhs);
6463 coder = TREE_CODE (rhstype);
6465 if (rhs == error_mark_node || rhstype == error_mark_node)
6466 return error_mark_node;
6467 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6468 return error_mark_node;
6470 /* Issue warnings about peculiar, but legal, uses of NULL. */
6471 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6472 cp_warning ("converting NULL to non-pointer type");
6474 /* The RHS of an assignment cannot have void type. */
6475 if (coder == VOID_TYPE)
6477 error ("void value not ignored as it ought to be");
6478 return error_mark_node;
6481 /* Simplify the RHS if possible. */
6482 if (TREE_CODE (rhs) == CONST_DECL)
6483 rhs = DECL_INITIAL (rhs);
6484 else if (TREE_READONLY_DECL_P (rhs))
6485 rhs = decl_constant_value (rhs);
6487 /* Warn about assigning a floating-point type to an integer type. */
6488 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6491 cp_warning ("`%T' used for argument %P of `%D'",
6492 rhstype, parmnum, fndecl);
6494 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6496 /* And warn about assigning a negative value to an unsigned
6498 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6500 if (TREE_CODE (rhs) == INTEGER_CST
6501 && TREE_NEGATED_INT (rhs))
6504 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6505 rhs, parmnum, fndecl);
6507 cp_warning ("%s of negative value `%E' to `%T'",
6508 errtype, rhs, type);
6510 overflow_warning (rhs);
6511 if (TREE_CONSTANT (rhs))
6517 The expression is implicitly converted (clause _conv_) to the
6518 cv-unqualified type of the left operand. */
6519 if (!can_convert_arg (type, rhstype, rhs))
6521 /* When -Wno-pmf-converions is use, we just silently allow
6522 conversions from pointers-to-members to plain pointers. If
6523 the conversion doesn't work, cp_convert will complain. */
6525 && TYPE_PTR_P (type)
6526 && TYPE_PTRMEMFUNC_P (rhstype))
6527 rhs = cp_convert (strip_top_quals (type), rhs);
6528 /* If the right-hand side has unknown type, then it is an
6529 overloaded function. Call instantiate_type to get error
6531 else if (rhstype == unknown_type_node)
6532 instantiate_type (type, rhs, 1);
6534 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6535 rhstype, type, parmnum, fndecl);
6537 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6539 return error_mark_node;
6541 return perform_implicit_conversion (strip_top_quals (type), rhs);
6544 /* Convert RHS to be of type TYPE.
6545 If EXP is non-zero, it is the target of the initialization.
6546 ERRTYPE is a string to use in error messages.
6548 Two major differences between the behavior of
6549 `convert_for_assignment' and `convert_for_initialization'
6550 are that references are bashed in the former, while
6551 copied in the latter, and aggregates are assigned in
6552 the former (operator=) while initialized in the
6555 If using constructor make sure no conversion operator exists, if one does
6556 exist, an ambiguity exists.
6558 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6561 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6562 tree exp, type, rhs;
6564 const char *errtype;
6568 register enum tree_code codel = TREE_CODE (type);
6569 register tree rhstype;
6570 register enum tree_code coder;
6572 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6573 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6574 if (TREE_CODE (rhs) == NOP_EXPR
6575 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6576 && codel != REFERENCE_TYPE)
6577 rhs = TREE_OPERAND (rhs, 0);
6579 if (rhs == error_mark_node
6580 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6581 return error_mark_node;
6583 if (TREE_CODE (rhs) == OFFSET_REF)
6585 rhs = resolve_offset_ref (rhs);
6586 if (rhs == error_mark_node)
6587 return error_mark_node;
6590 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6591 rhs = convert_from_reference (rhs);
6593 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6594 && TREE_CODE (type) != ARRAY_TYPE
6595 && (TREE_CODE (type) != REFERENCE_TYPE
6596 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6597 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6598 && (TREE_CODE (type) != REFERENCE_TYPE
6599 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6600 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6601 rhs = default_conversion (rhs);
6603 rhstype = TREE_TYPE (rhs);
6604 coder = TREE_CODE (rhstype);
6606 if (coder == ERROR_MARK)
6607 return error_mark_node;
6609 /* We accept references to incomplete types, so we can
6610 return here before checking if RHS is of complete type. */
6612 if (codel == REFERENCE_TYPE)
6614 /* This should eventually happen in convert_arguments. */
6615 extern int warningcount, errorcount;
6616 int savew = 0, savee = 0;
6619 savew = warningcount, savee = errorcount;
6620 rhs = initialize_reference (type, rhs);
6623 if (warningcount > savew)
6624 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6625 else if (errorcount > savee)
6626 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6632 exp = require_complete_type (exp);
6633 if (exp == error_mark_node)
6634 return error_mark_node;
6636 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6637 rhstype = TREE_TYPE (rhstype);
6639 type = complete_type (type);
6641 if (TYPE_LANG_SPECIFIC (type)
6642 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
6643 return build_signature_pointer_constructor (type, rhs);
6645 if (IS_AGGR_TYPE (type))
6646 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6648 if (type == TREE_TYPE (rhs))
6650 /* Issue warnings about peculiar, but legal, uses of NULL. We
6651 do this *before* the call to decl_constant_value so as to
6652 avoid duplicate warnings on code like `const int I = NULL;
6654 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6655 cp_warning ("converting NULL to non-pointer type");
6657 if (TREE_READONLY_DECL_P (rhs))
6658 rhs = decl_constant_value (rhs);
6663 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6666 /* Expand an ASM statement with operands, handling output operands
6667 that are not variables or INDIRECT_REFS by transforming such
6668 cases into cases that expand_asm_operands can handle.
6670 Arguments are same as for expand_asm_operands.
6672 We don't do default conversions on all inputs, because it can screw
6673 up operands that are expected to be in memory. */
6676 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6677 tree string, outputs, inputs, clobbers;
6682 int noutputs = list_length (outputs);
6684 /* o[I] is the place that output number I should be written. */
6685 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6688 /* Record the contents of OUTPUTS before it is modified. */
6689 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6690 o[i] = TREE_VALUE (tail);
6692 /* Generate the ASM_OPERANDS insn;
6693 store into the TREE_VALUEs of OUTPUTS some trees for
6694 where the values were actually stored. */
6695 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6697 /* Copy all the intermediate outputs into the specified outputs. */
6698 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6700 if (o[i] != TREE_VALUE (tail))
6702 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6703 const0_rtx, VOIDmode, EXPAND_NORMAL);
6706 /* Detect modification of read-only values.
6707 (Otherwise done by build_modify_expr.) */
6710 tree type = TREE_TYPE (o[i]);
6711 if (CP_TYPE_CONST_P (type)
6712 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6713 && C_TYPE_FIELDS_READONLY (type)))
6714 readonly_error (o[i], "modification by `asm'", 1);
6718 /* Those MODIFY_EXPRs could do autoincrements. */
6722 /* Expand a C `return' statement.
6723 RETVAL is the expression for what to return,
6724 or a null pointer for `return;' with no value.
6726 C++: upon seeing a `return', we must call destructors on all
6727 variables in scope which had constructors called on them.
6728 This means that if in a destructor, the base class destructors
6729 must be called before returning.
6731 The RETURN statement in C++ has initialization semantics. */
6734 c_expand_return (retval)
6737 extern tree dtor_label, ctor_label;
6738 tree result = DECL_RESULT (current_function_decl);
6739 tree valtype = TREE_TYPE (result);
6741 if (TREE_THIS_VOLATILE (current_function_decl))
6742 warning ("function declared `noreturn' has a `return' statement");
6744 if (retval == error_mark_node)
6746 current_function_returns_null = 1;
6750 if (processing_template_decl)
6752 add_tree (build_min_nt (RETURN_STMT, retval));
6759 error ("returning a value from a destructor");
6761 /* Can't just return from a destructor. */
6762 expand_goto (dtor_label);
6766 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6767 if ((DECL_NAME (current_function_decl) == ansi_opname[(int) NEW_EXPR]
6768 || DECL_NAME (current_function_decl) == ansi_opname[(int) VEC_NEW_EXPR])
6769 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6770 && null_ptr_cst_p (retval))
6771 cp_warning ("operator new should throw an exception, not return NULL");
6773 if (retval == NULL_TREE)
6775 /* A non-named return value does not count. */
6777 if (DECL_CONSTRUCTOR_P (current_function_decl))
6778 retval = current_class_ptr;
6779 else if (DECL_NAME (result) != NULL_TREE
6780 && TREE_CODE (valtype) != VOID_TYPE)
6784 current_function_returns_null = 1;
6786 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
6788 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
6790 pedwarn ("`return' with no value, in function returning non-void");
6791 /* Clear this, so finish_function won't say that we
6792 reach the end of a non-void function (which we don't,
6793 we gave a return!). */
6794 current_function_returns_null = 0;
6798 expand_null_return ();
6802 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6804 if (flag_this_is_variable)
6805 error ("return from a constructor: use `this = ...' instead");
6807 error ("returning a value from a constructor");
6808 retval = current_class_ptr;
6811 /* Effective C++ rule 15. See also start_function. */
6813 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
6814 && retval != current_class_ref)
6815 cp_warning ("`operator=' should return a reference to `*this'");
6817 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
6819 current_function_returns_null = 1;
6820 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6821 pedwarn ("`return' with a value, in function returning void");
6822 expand_return (retval);
6826 /* Now deal with possible C++ hair:
6827 (1) Compute the return value.
6828 (2) If there are aggregate values with destructors which
6829 must be cleaned up, clean them (taking care
6830 not to clobber the return value).
6831 (3) If an X(X&) constructor is defined, the return
6832 value must be returned via that. */
6834 if (retval == result
6835 || DECL_CONSTRUCTOR_P (current_function_decl))
6836 /* It's already done for us. */;
6837 else if (TREE_CODE (TREE_TYPE (retval)) == VOID_TYPE)
6839 pedwarn ("return of void value in function returning non-void");
6840 expand_expr_stmt (retval);
6845 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6847 /* First convert the value to the function's return type, then
6848 to the type of return value's location to handle the
6849 case that functype is thiner than the valtype. */
6851 retval = convert_for_initialization
6852 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6853 "return", NULL_TREE, 0);
6855 retval = convert (valtype, retval);
6857 if (retval == error_mark_node)
6859 /* Avoid warning about control reaching end of function. */
6860 expand_null_return ();
6864 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6865 else if (! current_function_returns_struct
6866 && TREE_CODE (retval) == TARGET_EXPR
6867 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6868 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6869 TREE_OPERAND (retval, 0));
6871 /* Add some useful error checking for C++. */
6872 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
6874 tree whats_returned;
6876 /* Sort through common things to see what it is
6877 we are returning. */
6878 whats_returned = retval;
6879 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6881 whats_returned = TREE_OPERAND (whats_returned, 1);
6882 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6883 whats_returned = TREE_OPERAND (whats_returned, 0);
6885 while (TREE_CODE (whats_returned) == CONVERT_EXPR
6886 || TREE_CODE (whats_returned) == NOP_EXPR)
6887 whats_returned = TREE_OPERAND (whats_returned, 0);
6888 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6890 whats_returned = TREE_OPERAND (whats_returned, 0);
6891 while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6892 || TREE_CODE (whats_returned) == TARGET_EXPR)
6894 /* Get the target. */
6895 whats_returned = TREE_OPERAND (whats_returned, 0);
6896 warning ("returning reference to temporary");
6900 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
6902 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
6903 warning ("reference to non-lvalue returned");
6904 else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
6905 && DECL_FUNCTION_SCOPE_P (whats_returned)
6906 && !(TREE_STATIC (whats_returned)
6907 || TREE_PUBLIC (whats_returned)))
6908 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
6911 else if (TREE_CODE (retval) == ADDR_EXPR)
6913 tree whats_returned = TREE_OPERAND (retval, 0);
6915 if (TREE_CODE (whats_returned) == VAR_DECL
6916 && DECL_NAME (whats_returned)
6917 && DECL_FUNCTION_SCOPE_P (whats_returned)
6918 && !(TREE_STATIC (whats_returned)
6919 || TREE_PUBLIC (whats_returned)))
6920 cp_warning_at ("address of local variable `%D' returned", whats_returned);
6924 if (retval != NULL_TREE
6925 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
6926 && ! in_control_zone_p ())
6927 current_function_return_value = retval;
6929 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
6931 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
6932 expand_goto (ctor_label);
6935 if (retval && retval != result)
6937 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6938 TREE_SIDE_EFFECTS (result) = 1;
6941 expand_start_target_temps ();
6943 expand_return (result);
6945 expand_end_target_temps ();
6947 current_function_returns_value = 1;
6950 /* Start a C switch statement, testing expression EXP.
6951 Return EXP if it is valid, an error node otherwise. */
6954 c_expand_start_case (exp)
6959 exp = build_expr_type_conversion (WANT_INT | WANT_ENUM, exp, 1);
6960 if (exp == NULL_TREE)
6962 error ("switch quantity not an integer");
6963 exp = error_mark_node;
6965 if (exp == error_mark_node)
6966 return error_mark_node;
6968 exp = default_conversion (exp);
6969 type = TREE_TYPE (exp);
6970 idx = get_unwidened (exp, 0);
6971 /* We can't strip a conversion from a signed type to an unsigned,
6972 because if we did, int_fits_type_p would do the wrong thing
6973 when checking case values for being in range,
6974 and it's too hard to do the right thing. */
6975 if (TREE_UNSIGNED (TREE_TYPE (exp)) == TREE_UNSIGNED (TREE_TYPE (idx)))
6979 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
6980 type, "switch statement");
6985 /* Returns non-zero if the pointer-type FROM can be converted to the
6986 pointer-type TO via a qualification conversion. If CONSTP is -1,
6987 then we return non-zero if the pointers are similar, and the
6988 cv-qualification signature of FROM is a proper subset of that of TO.
6990 If CONSTP is positive, then all outer pointers have been
6994 comp_ptr_ttypes_real (to, from, constp)
6998 int to_more_cv_qualified = 0;
7000 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7002 if (TREE_CODE (to) != TREE_CODE (from))
7005 if (TREE_CODE (from) == OFFSET_TYPE
7006 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7007 TYPE_OFFSET_BASETYPE (to)))
7010 /* Const and volatile mean something different for function types,
7011 so the usual checks are not appropriate. */
7012 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7014 if (!at_least_as_qualified_p (to, from))
7017 if (!at_least_as_qualified_p (from, to))
7022 ++to_more_cv_qualified;
7026 constp &= TYPE_READONLY (to);
7029 if (TREE_CODE (to) != POINTER_TYPE)
7031 same_type_p (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from))
7032 && (constp >= 0 || to_more_cv_qualified);
7036 /* When comparing, say, char ** to char const **, this function takes the
7037 'char *' and 'char const *'. Do not pass non-pointer types to this
7041 comp_ptr_ttypes (to, from)
7044 return comp_ptr_ttypes_real (to, from, 1);
7047 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7048 type or inheritance-related types, regardless of cv-quals. */
7051 ptr_reasonably_similar (to, from)
7054 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7056 if (TREE_CODE (to) != TREE_CODE (from))
7059 if (TREE_CODE (from) == OFFSET_TYPE
7060 && comptypes (TYPE_OFFSET_BASETYPE (to),
7061 TYPE_OFFSET_BASETYPE (from),
7062 COMPARE_BASE | COMPARE_RELAXED))
7065 if (TREE_CODE (to) != POINTER_TYPE)
7067 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
7068 COMPARE_BASE | COMPARE_RELAXED);
7072 /* Like comp_ptr_ttypes, for const_cast. */
7075 comp_ptr_ttypes_const (to, from)
7078 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7080 if (TREE_CODE (to) != TREE_CODE (from))
7083 if (TREE_CODE (from) == OFFSET_TYPE
7084 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7085 TYPE_OFFSET_BASETYPE (to)))
7088 if (TREE_CODE (to) != POINTER_TYPE)
7089 return same_type_p (TYPE_MAIN_VARIANT (to),
7090 TYPE_MAIN_VARIANT (from));
7094 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7097 comp_ptr_ttypes_reinterpret (to, from)
7102 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7104 if (TREE_CODE (from) == OFFSET_TYPE)
7105 from = TREE_TYPE (from);
7106 if (TREE_CODE (to) == OFFSET_TYPE)
7107 to = TREE_TYPE (to);
7109 /* Const and volatile mean something different for function types,
7110 so the usual checks are not appropriate. */
7111 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7112 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7114 if (!at_least_as_qualified_p (to, from))
7118 && !at_least_as_qualified_p (from, to))
7120 constp &= TYPE_READONLY (to);
7123 if (TREE_CODE (from) != POINTER_TYPE
7124 || TREE_CODE (to) != POINTER_TYPE)
7129 /* Returns the type-qualifier set corresponding to TYPE. */
7132 cp_type_quals (type)
7135 while (TREE_CODE (type) == ARRAY_TYPE)
7136 type = TREE_TYPE (type);
7138 return TYPE_QUALS (type);
7141 /* Returns non-zero if the TYPE contains a mutable member */
7144 cp_has_mutable_p (type)
7147 while (TREE_CODE (type) == ARRAY_TYPE)
7148 type = TREE_TYPE (type);
7150 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7153 /* Subroutine of casts_away_constness. Make T1 and T2 point at
7154 exemplar types such that casting T1 to T2 is casting away castness
7155 if and only if there is no implicit conversion from T1 to T2. */
7158 casts_away_constness_r (t1, t2)
7165 /* [expr.const.cast]
7167 For multi-level pointer to members and multi-level mixed pointers
7168 and pointers to members (conv.qual), the "member" aspect of a
7169 pointer to member level is ignored when determining if a const
7170 cv-qualifier has been cast away. */
7171 if (TYPE_PTRMEM_P (*t1))
7172 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
7173 if (TYPE_PTRMEM_P (*t2))
7174 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
7176 /* [expr.const.cast]
7178 For two pointer types:
7180 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
7181 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
7184 casting from X1 to X2 casts away constness if, for a non-pointer
7185 type T there does not exist an implicit conversion (clause
7188 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7192 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
7194 if (TREE_CODE (*t1) != POINTER_TYPE
7195 || TREE_CODE (*t2) != POINTER_TYPE)
7197 *t1 = cp_build_qualified_type (void_type_node,
7198 CP_TYPE_QUALS (*t1));
7199 *t2 = cp_build_qualified_type (void_type_node,
7200 CP_TYPE_QUALS (*t2));
7204 quals1 = CP_TYPE_QUALS (*t1);
7205 quals2 = CP_TYPE_QUALS (*t2);
7206 *t1 = TREE_TYPE (*t1);
7207 *t2 = TREE_TYPE (*t2);
7208 casts_away_constness_r (t1, t2);
7209 *t1 = build_pointer_type (*t1);
7210 *t2 = build_pointer_type (*t2);
7211 *t1 = cp_build_qualified_type (*t1, quals1);
7212 *t2 = cp_build_qualified_type (*t2, quals2);
7215 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
7219 casts_away_constness (t1, t2)
7223 if (TREE_CODE (t2) == REFERENCE_TYPE)
7225 /* [expr.const.cast]
7227 Casting from an lvalue of type T1 to an lvalue of type T2
7228 using a reference cast casts away constness if a cast from an
7229 rvalue of type "pointer to T1" to the type "pointer to T2"
7230 casts away constness. */
7231 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
7232 ? TREE_TYPE (t1) : t1);
7233 return casts_away_constness (build_pointer_type (t1),
7234 build_pointer_type (TREE_TYPE (t2)));
7237 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7238 /* [expr.const.cast]
7240 Casting from an rvalue of type "pointer to data member of X
7241 of type T1" to the type "pointer to data member of Y of type
7242 T2" casts away constness if a cast from an rvalue of type
7243 "poitner to T1" to the type "pointer to T2" casts away
7245 return casts_away_constness
7246 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7247 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7249 /* Casting away constness is only something that makes sense for
7250 pointer or reference types. */
7251 if (TREE_CODE (t1) != POINTER_TYPE
7252 || TREE_CODE (t2) != POINTER_TYPE)
7255 /* Top-level qualifiers don't matter. */
7256 t1 = TYPE_MAIN_VARIANT (t1);
7257 t2 = TYPE_MAIN_VARIANT (t2);
7258 casts_away_constness_r (&t1, &t2);
7259 if (!can_convert (t2, t1))