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 #ifdef PROMOTE_PROTOTYPES
3208 if ((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);
3216 if (parmval == error_mark_node)
3217 return error_mark_node;
3219 result = expr_tree_cons (NULL_TREE, parmval, result);
3223 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3224 val = convert_from_reference (val);
3226 result = expr_tree_cons (NULL_TREE,
3227 convert_arg_to_ellipsis (val),
3232 typetail = TREE_CHAIN (typetail);
3235 if (typetail != 0 && typetail != void_list_node)
3237 /* See if there are default arguments that can be used */
3238 if (TREE_PURPOSE (typetail))
3240 for (; typetail != void_list_node; ++i)
3243 = convert_default_arg (TREE_VALUE (typetail),
3244 TREE_PURPOSE (typetail),
3247 if (parmval == error_mark_node)
3248 return error_mark_node;
3250 result = expr_tree_cons (0, parmval, result);
3251 typetail = TREE_CHAIN (typetail);
3252 /* ends with `...'. */
3253 if (typetail == NULL_TREE)
3261 cp_error_at ("too few arguments to %s `%+#D'",
3262 called_thing, fndecl);
3263 error ("at this point in file");
3266 error ("too few arguments to function");
3267 return error_mark_list;
3271 return nreverse (result);
3274 /* Build a binary-operation expression, after performing default
3275 conversions on the operands. CODE is the kind of expression to build. */
3278 build_x_binary_op (code, arg1, arg2)
3279 enum tree_code code;
3282 if (processing_template_decl)
3283 return build_min_nt (code, arg1, arg2);
3285 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3289 build_binary_op (code, arg1, arg2)
3290 enum tree_code code;
3293 return build_binary_op_nodefault (code, arg1, arg2, code);
3296 /* Build a binary-operation expression without default conversions.
3297 CODE is the kind of expression to build.
3298 This function differs from `build' in several ways:
3299 the data type of the result is computed and recorded in it,
3300 warnings are generated if arg data types are invalid,
3301 special handling for addition and subtraction of pointers is known,
3302 and some optimization is done (operations on narrow ints
3303 are done in the narrower type when that gives the same result).
3304 Constant folding is also done before the result is returned.
3306 ERROR_CODE is the code that determines what to say in error messages.
3307 It is usually, but not always, the same as CODE.
3309 Note that the operands will never have enumeral types
3310 because either they have just had the default conversions performed
3311 or they have both just been converted to some other type in which
3312 the arithmetic is to be done.
3314 C++: must do special pointer arithmetic when implementing
3315 multiple inheritance, and deal with pointer to member functions. */
3318 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3319 enum tree_code code;
3320 tree orig_op0, orig_op1;
3321 enum tree_code error_code;
3324 register enum tree_code code0, code1;
3327 /* Expression code to give to the expression when it is built.
3328 Normally this is CODE, which is what the caller asked for,
3329 but in some special cases we change it. */
3330 register enum tree_code resultcode = code;
3332 /* Data type in which the computation is to be performed.
3333 In the simplest cases this is the common type of the arguments. */
3334 register tree result_type = NULL;
3336 /* Nonzero means operands have already been type-converted
3337 in whatever way is necessary.
3338 Zero means they need to be converted to RESULT_TYPE. */
3341 /* Nonzero means create the expression with this type, rather than
3343 tree build_type = 0;
3345 /* Nonzero means after finally constructing the expression
3346 convert it to this type. */
3347 tree final_type = 0;
3349 /* Nonzero if this is an operation like MIN or MAX which can
3350 safely be computed in short if both args are promoted shorts.
3351 Also implies COMMON.
3352 -1 indicates a bitwise operation; this makes a difference
3353 in the exact conditions for when it is safe to do the operation
3354 in a narrower mode. */
3357 /* Nonzero if this is a comparison operation;
3358 if both args are promoted shorts, compare the original shorts.
3359 Also implies COMMON. */
3360 int short_compare = 0;
3362 /* Nonzero if this is a right-shift operation, which can be computed on the
3363 original short and then promoted if the operand is a promoted short. */
3364 int short_shift = 0;
3366 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3369 /* Apply default conversions. */
3370 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3371 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3372 || code == TRUTH_XOR_EXPR)
3374 op0 = decay_conversion (orig_op0);
3375 op1 = decay_conversion (orig_op1);
3379 op0 = default_conversion (orig_op0);
3380 op1 = default_conversion (orig_op1);
3383 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3384 STRIP_TYPE_NOPS (op0);
3385 STRIP_TYPE_NOPS (op1);
3387 /* DTRT if one side is an overloaded function, but complain about it. */
3388 if (type_unknown_p (op0))
3390 tree t = instantiate_type (TREE_TYPE (op1), op0, 0);
3391 if (t != error_mark_node)
3393 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3398 if (type_unknown_p (op1))
3400 tree t = instantiate_type (TREE_TYPE (op0), op1, 0);
3401 if (t != error_mark_node)
3403 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3409 type0 = TREE_TYPE (op0);
3410 type1 = TREE_TYPE (op1);
3412 /* The expression codes of the data types of the arguments tell us
3413 whether the arguments are integers, floating, pointers, etc. */
3414 code0 = TREE_CODE (type0);
3415 code1 = TREE_CODE (type1);
3417 /* If an error was already reported for one of the arguments,
3418 avoid reporting another error. */
3420 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3421 return error_mark_node;
3426 /* Handle the pointer + int case. */
3427 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3428 return pointer_int_sum (PLUS_EXPR, op0, op1);
3429 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3430 return pointer_int_sum (PLUS_EXPR, op1, op0);
3436 /* Subtraction of two similar pointers.
3437 We must subtract them as integers, then divide by object size. */
3438 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3439 && comp_target_types (type0, type1, 1))
3440 return pointer_diff (op0, op1, common_type (type0, type1));
3441 /* Handle pointer minus int. Just like pointer plus int. */
3442 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3443 return pointer_int_sum (MINUS_EXPR, op0, op1);
3452 case TRUNC_DIV_EXPR:
3454 case FLOOR_DIV_EXPR:
3455 case ROUND_DIV_EXPR:
3456 case EXACT_DIV_EXPR:
3457 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3458 || code0 == COMPLEX_TYPE)
3459 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3460 || code1 == COMPLEX_TYPE))
3462 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3463 cp_warning ("division by zero in `%E / 0'", op0);
3464 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3465 cp_warning ("division by zero in `%E / 0.'", op0);
3467 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3468 resultcode = RDIV_EXPR;
3470 /* When dividing two signed integers, we have to promote to int.
3471 unless we divide by a constant != -1. Note that default
3472 conversion will have been performed on the operands at this
3473 point, so we have to dig out the original type to find out if
3475 shorten = ((TREE_CODE (op0) == NOP_EXPR
3476 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3477 || (TREE_CODE (op1) == INTEGER_CST
3478 && (TREE_INT_CST_LOW (op1) != -1
3479 || TREE_INT_CST_HIGH (op1) != -1)));
3485 case BIT_ANDTC_EXPR:
3488 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3490 /* If one operand is a constant, and the other is a short type
3491 that has been converted to an int,
3492 really do the work in the short type and then convert the
3493 result to int. If we are lucky, the constant will be 0 or 1
3494 in the short type, making the entire operation go away. */
3495 if (TREE_CODE (op0) == INTEGER_CST
3496 && TREE_CODE (op1) == NOP_EXPR
3497 && (TYPE_PRECISION (type1)
3498 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3499 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3501 final_type = result_type;
3502 op1 = TREE_OPERAND (op1, 0);
3503 result_type = TREE_TYPE (op1);
3505 if (TREE_CODE (op1) == INTEGER_CST
3506 && TREE_CODE (op0) == NOP_EXPR
3507 && (TYPE_PRECISION (type0)
3508 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3509 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3511 final_type = result_type;
3512 op0 = TREE_OPERAND (op0, 0);
3513 result_type = TREE_TYPE (op0);
3517 case TRUNC_MOD_EXPR:
3518 case FLOOR_MOD_EXPR:
3519 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3520 cp_warning ("division by zero in `%E %% 0'", op0);
3521 else if (code1 == REAL_TYPE && real_zerop (op1))
3522 cp_warning ("division by zero in `%E %% 0.'", op0);
3524 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3526 /* Although it would be tempting to shorten always here, that loses
3527 on some targets, since the modulo instruction is undefined if the
3528 quotient can't be represented in the computation mode. We shorten
3529 only if unsigned or if dividing by something we know != -1. */
3530 shorten = ((TREE_CODE (op0) == NOP_EXPR
3531 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3532 || (TREE_CODE (op1) == INTEGER_CST
3533 && (TREE_INT_CST_LOW (op1) != -1
3534 || TREE_INT_CST_HIGH (op1) != -1)));
3539 case TRUTH_ANDIF_EXPR:
3540 case TRUTH_ORIF_EXPR:
3541 case TRUTH_AND_EXPR:
3543 result_type = boolean_type_node;
3546 /* Shift operations: result has same type as first operand;
3547 always convert second operand to int.
3548 Also set SHORT_SHIFT if shifting rightward. */
3551 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3553 result_type = type0;
3554 if (TREE_CODE (op1) == INTEGER_CST)
3556 if (tree_int_cst_lt (op1, integer_zero_node))
3557 warning ("right shift count is negative");
3560 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3562 if (TREE_INT_CST_HIGH (op1) != 0
3563 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3564 >= TYPE_PRECISION (type0)))
3565 warning ("right shift count >= width of type");
3568 /* Convert the shift-count to an integer, regardless of
3569 size of value being shifted. */
3570 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3571 op1 = cp_convert (integer_type_node, op1);
3572 /* Avoid converting op1 to result_type later. */
3578 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3580 result_type = type0;
3581 if (TREE_CODE (op1) == INTEGER_CST)
3583 if (tree_int_cst_lt (op1, integer_zero_node))
3584 warning ("left shift count is negative");
3585 else if (TREE_INT_CST_HIGH (op1) != 0
3586 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3587 >= TYPE_PRECISION (type0)))
3588 warning ("left shift count >= width of type");
3590 /* Convert the shift-count to an integer, regardless of
3591 size of value being shifted. */
3592 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3593 op1 = cp_convert (integer_type_node, op1);
3594 /* Avoid converting op1 to result_type later. */
3601 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3603 result_type = type0;
3604 if (TREE_CODE (op1) == INTEGER_CST)
3606 if (tree_int_cst_lt (op1, integer_zero_node))
3607 warning ("%s rotate count is negative",
3608 (code == LROTATE_EXPR) ? "left" : "right");
3609 else if (TREE_INT_CST_HIGH (op1) != 0
3610 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3611 >= TYPE_PRECISION (type0)))
3612 warning ("%s rotate count >= width of type",
3613 (code == LROTATE_EXPR) ? "left" : "right");
3615 /* Convert the shift-count to an integer, regardless of
3616 size of value being shifted. */
3617 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3618 op1 = cp_convert (integer_type_node, op1);
3624 build_type = boolean_type_node;
3625 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3626 || code0 == COMPLEX_TYPE)
3627 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3628 || code1 == COMPLEX_TYPE))
3630 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3632 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3633 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3635 if (comp_target_types (type0, type1, 1))
3636 result_type = common_type (type0, type1);
3637 else if (tt0 == void_type_node)
3639 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3640 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3641 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3642 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3643 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3645 else if (tt1 == void_type_node)
3647 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3648 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3649 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3652 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3655 if (result_type == NULL_TREE)
3656 result_type = ptr_type_node;
3658 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3659 result_type = type0;
3660 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3661 result_type = type1;
3662 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3664 result_type = type0;
3665 error ("ANSI C++ forbids comparison between pointer and integer");
3667 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3669 result_type = type1;
3670 error ("ANSI C++ forbids comparison between pointer and integer");
3672 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3674 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3675 op1 = integer_zero_node;
3676 result_type = TREE_TYPE (op0);
3678 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3680 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3681 op1 = integer_zero_node;
3682 result_type = TREE_TYPE (op0);
3684 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3685 && same_type_p (type0, type1))
3687 /* The code we generate for the test is:
3689 (op0.index == op1.index
3690 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3691 || op0.pfn == op1.pfn)) */
3693 tree index0 = build_component_ref (op0, index_identifier,
3695 tree index1 = save_expr (build_component_ref (op1, index_identifier,
3697 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3698 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3699 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3700 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3702 tree integer_neg_one_node
3703 = build_binary_op (MINUS_EXPR, integer_zero_node,
3705 e1 = build_binary_op (EQ_EXPR, index0, index1);
3706 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3707 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3708 build_binary_op (EQ_EXPR, delta20, delta21));
3709 /* We can't use build_binary_op for this cmp because it would get
3710 confused by the ptr to method types and think we want pmfs. */
3711 e3 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3712 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3713 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3714 if (code == EQ_EXPR)
3716 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3718 else if (TYPE_PTRMEMFUNC_P (type0)
3719 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3721 tree index0 = build_component_ref (op0, index_identifier,
3724 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3725 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3726 tree delta21 = integer_zero_node;
3728 tree integer_neg_one_node
3729 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node);
3730 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3731 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3733 /* Map everything down one to make room for
3734 the null pointer to member. */
3735 index1 = size_binop (PLUS_EXPR,
3736 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3738 op1 = integer_zero_node;
3739 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3740 (TREE_TYPE (type1)));
3741 delta21 = DECL_FIELD_BITPOS (delta21);
3742 delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3743 size_int (BITS_PER_UNIT));
3744 delta21 = convert (sizetype, delta21);
3747 index1 = integer_neg_one_node;
3749 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3751 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3754 e1 = build_binary_op (EQ_EXPR, index0, index1);
3755 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3756 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3757 build_binary_op (EQ_EXPR, delta20, delta21));
3758 /* We can't use build_binary_op for this cmp because it would get
3759 confused by the ptr to method types and think we want pmfs. */
3760 e3 = build (EQ_EXPR, boolean_type_node, pfn0, op1);
3761 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3762 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3763 if (code == EQ_EXPR)
3765 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3767 else if (TYPE_PTRMEMFUNC_P (type1)
3768 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0))
3769 return build_binary_op (code, op1, op0);
3774 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3775 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3777 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3779 if (comp_target_types (type0, type1, 1))
3780 result_type = common_type (type0, type1);
3783 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3785 result_type = ptr_type_node;
3794 build_type = boolean_type_node;
3795 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3796 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3798 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3800 if (comp_target_types (type0, type1, 1))
3801 result_type = common_type (type0, type1);
3804 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3806 result_type = ptr_type_node;
3809 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3810 && integer_zerop (op1))
3811 result_type = type0;
3812 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3813 && integer_zerop (op0))
3814 result_type = type1;
3815 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3817 result_type = type0;
3818 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3820 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3822 result_type = type1;
3823 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3831 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3833 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3835 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3837 if (shorten || common || short_compare)
3838 result_type = common_type (type0, type1);
3840 /* For certain operations (which identify themselves by shorten != 0)
3841 if both args were extended from the same smaller type,
3842 do the arithmetic in that type and then extend.
3844 shorten !=0 and !=1 indicates a bitwise operation.
3845 For them, this optimization is safe only if
3846 both args are zero-extended or both are sign-extended.
3847 Otherwise, we might change the result.
3848 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3849 but calculated in (unsigned short) it would be (unsigned short)-1. */
3851 if (shorten && none_complex)
3853 int unsigned0, unsigned1;
3854 tree arg0 = get_narrower (op0, &unsigned0);
3855 tree arg1 = get_narrower (op1, &unsigned1);
3856 /* UNS is 1 if the operation to be done is an unsigned one. */
3857 int uns = TREE_UNSIGNED (result_type);
3860 final_type = result_type;
3862 /* Handle the case that OP0 does not *contain* a conversion
3863 but it *requires* conversion to FINAL_TYPE. */
3865 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3866 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3867 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3868 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3870 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3872 /* For bitwise operations, signedness of nominal type
3873 does not matter. Consider only how operands were extended. */
3877 /* Note that in all three cases below we refrain from optimizing
3878 an unsigned operation on sign-extended args.
3879 That would not be valid. */
3881 /* Both args variable: if both extended in same way
3882 from same width, do it in that width.
3883 Do it unsigned if args were zero-extended. */
3884 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3885 < TYPE_PRECISION (result_type))
3886 && (TYPE_PRECISION (TREE_TYPE (arg1))
3887 == TYPE_PRECISION (TREE_TYPE (arg0)))
3888 && unsigned0 == unsigned1
3889 && (unsigned0 || !uns))
3891 = signed_or_unsigned_type (unsigned0,
3892 common_type (TREE_TYPE (arg0),
3894 else if (TREE_CODE (arg0) == INTEGER_CST
3895 && (unsigned1 || !uns)
3896 && (TYPE_PRECISION (TREE_TYPE (arg1))
3897 < TYPE_PRECISION (result_type))
3898 && (type = signed_or_unsigned_type (unsigned1,
3900 int_fits_type_p (arg0, type)))
3902 else if (TREE_CODE (arg1) == INTEGER_CST
3903 && (unsigned0 || !uns)
3904 && (TYPE_PRECISION (TREE_TYPE (arg0))
3905 < TYPE_PRECISION (result_type))
3906 && (type = signed_or_unsigned_type (unsigned0,
3908 int_fits_type_p (arg1, type)))
3912 /* Shifts can be shortened if shifting right. */
3917 tree arg0 = get_narrower (op0, &unsigned_arg);
3919 final_type = result_type;
3921 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3922 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3924 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3925 /* We can shorten only if the shift count is less than the
3926 number of bits in the smaller type size. */
3927 && TREE_INT_CST_HIGH (op1) == 0
3928 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3929 /* If arg is sign-extended and then unsigned-shifted,
3930 we can simulate this with a signed shift in arg's type
3931 only if the extended result is at least twice as wide
3932 as the arg. Otherwise, the shift could use up all the
3933 ones made by sign-extension and bring in zeros.
3934 We can't optimize that case at all, but in most machines
3935 it never happens because available widths are 2**N. */
3936 && (!TREE_UNSIGNED (final_type)
3938 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3939 <= TYPE_PRECISION (result_type))))
3941 /* Do an unsigned shift if the operand was zero-extended. */
3943 = signed_or_unsigned_type (unsigned_arg,
3945 /* Convert value-to-be-shifted to that type. */
3946 if (TREE_TYPE (op0) != result_type)
3947 op0 = cp_convert (result_type, op0);
3952 /* Comparison operations are shortened too but differently.
3953 They identify themselves by setting short_compare = 1. */
3957 /* Don't write &op0, etc., because that would prevent op0
3958 from being kept in a register.
3959 Instead, make copies of the our local variables and
3960 pass the copies by reference, then copy them back afterward. */
3961 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3962 enum tree_code xresultcode = resultcode;
3964 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3966 return cp_convert (boolean_type_node, val);
3967 op0 = xop0, op1 = xop1;
3969 resultcode = xresultcode;
3972 if (short_compare && warn_sign_compare)
3974 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3975 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3977 int unsignedp0, unsignedp1;
3978 tree primop0 = get_narrower (op0, &unsignedp0);
3979 tree primop1 = get_narrower (op1, &unsignedp1);
3981 /* Check for comparison of different enum types. */
3982 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3983 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3984 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3985 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3987 cp_warning ("comparison between `%#T' and `%#T'",
3988 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3991 /* Give warnings for comparisons between signed and unsigned
3992 quantities that may fail. */
3993 /* Do the checking based on the original operand trees, so that
3994 casts will be considered, but default promotions won't be. */
3996 /* Do not warn if the comparison is being done in a signed type,
3997 since the signed type will only be chosen if it can represent
3998 all the values of the unsigned type. */
3999 if (! TREE_UNSIGNED (result_type))
4001 /* Do not warn if both operands are unsigned. */
4002 else if (op0_signed == op1_signed)
4004 /* Do not warn if the signed quantity is an unsuffixed
4005 integer literal (or some static constant expression
4006 involving such literals) and it is non-negative. */
4007 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
4008 && tree_int_cst_sgn (orig_op0) >= 0)
4009 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
4010 && tree_int_cst_sgn (orig_op1) >= 0))
4012 /* Do not warn if the comparison is an equality operation,
4013 the unsigned quantity is an integral constant and it does
4014 not use the most significant bit of result_type. */
4015 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
4016 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
4017 && int_fits_type_p (orig_op1,
4018 signed_type (result_type)))
4019 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
4020 && int_fits_type_p (orig_op0,
4021 signed_type (result_type)))))
4024 warning ("comparison between signed and unsigned");
4026 /* Warn if two unsigned values are being compared in a size
4027 larger than their original size, and one (and only one) is the
4028 result of a `~' operator. This comparison will always fail.
4030 Also warn if one operand is a constant, and the constant does not
4031 have all bits set that are set in the ~ operand when it is
4034 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
4035 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
4037 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
4038 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
4039 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
4040 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
4042 if (TREE_CODE (primop0) == INTEGER_CST
4043 || TREE_CODE (primop1) == INTEGER_CST)
4046 HOST_WIDE_INT constant, mask;
4050 if (TREE_CODE (primop0) == INTEGER_CST)
4053 unsignedp = unsignedp1;
4054 constant = TREE_INT_CST_LOW (primop0);
4059 unsignedp = unsignedp0;
4060 constant = TREE_INT_CST_LOW (primop1);
4063 bits = TYPE_PRECISION (TREE_TYPE (primop));
4064 if (bits < TYPE_PRECISION (result_type)
4065 && bits < HOST_BITS_PER_LONG && unsignedp)
4067 mask = (~ (HOST_WIDE_INT) 0) << bits;
4068 if ((mask & constant) != mask)
4069 warning ("comparison of promoted ~unsigned with constant");
4072 else if (unsignedp0 && unsignedp1
4073 && (TYPE_PRECISION (TREE_TYPE (primop0))
4074 < TYPE_PRECISION (result_type))
4075 && (TYPE_PRECISION (TREE_TYPE (primop1))
4076 < TYPE_PRECISION (result_type)))
4077 warning ("comparison of promoted ~unsigned with unsigned");
4082 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4083 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4084 Then the expression will be built.
4085 It will be given type FINAL_TYPE if that is nonzero;
4086 otherwise, it will be given type RESULT_TYPE. */
4090 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
4091 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
4092 return error_mark_node;
4095 /* Issue warnings about peculiar, but legal, uses of NULL. */
4096 if (/* It's reasonable to use pointer values as operands of &&
4097 and ||, so NULL is no exception. */
4098 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4099 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4100 (orig_op0 == null_node
4101 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4102 /* Or vice versa. */
4103 || (orig_op1 == null_node
4104 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4105 /* Or, both are NULL and the operation was not a comparison. */
4106 || (orig_op0 == null_node && orig_op1 == null_node
4107 && code != EQ_EXPR && code != NE_EXPR)))
4108 /* Some sort of arithmetic operation involving NULL was
4109 performed. Note that pointer-difference and pointer-addition
4110 have already been handled above, and so we don't end up here in
4112 cp_warning ("NULL used in arithmetic");
4116 if (TREE_TYPE (op0) != result_type)
4117 op0 = cp_convert (result_type, op0);
4118 if (TREE_TYPE (op1) != result_type)
4119 op1 = cp_convert (result_type, op1);
4121 if (op0 == error_mark_node || op1 == error_mark_node)
4122 return error_mark_node;
4125 if (build_type == NULL_TREE)
4126 build_type = result_type;
4129 register tree result = build (resultcode, build_type, op0, op1);
4130 register tree folded;
4132 folded = fold (result);
4133 if (folded == result)
4134 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4135 if (final_type != 0)
4136 return cp_convert (final_type, folded);
4141 /* Return a tree for the sum or difference (RESULTCODE says which)
4142 of pointer PTROP and integer INTOP. */
4145 pointer_int_sum (resultcode, ptrop, intop)
4146 enum tree_code resultcode;
4147 register tree ptrop, intop;
4151 register tree result;
4152 register tree folded = fold (intop);
4154 /* The result is a pointer of the same type that is being added. */
4156 register tree result_type = TREE_TYPE (ptrop);
4158 if (!complete_type_or_else (result_type, ptrop))
4159 return error_mark_node;
4161 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4163 if (pedantic || warn_pointer_arith)
4164 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
4165 size_exp = integer_one_node;
4167 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4169 if (pedantic || warn_pointer_arith)
4170 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
4171 size_exp = integer_one_node;
4173 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4175 if (pedantic || warn_pointer_arith)
4176 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4177 size_exp = integer_one_node;
4179 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4181 if (pedantic || warn_pointer_arith)
4182 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4183 size_exp = integer_one_node;
4186 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4188 /* Needed to make OOPS V2R3 work. */
4190 if (TREE_CODE (intop) == INTEGER_CST
4191 && TREE_INT_CST_LOW (intop) == 0
4192 && TREE_INT_CST_HIGH (intop) == 0)
4195 /* If what we are about to multiply by the size of the elements
4196 contains a constant term, apply distributive law
4197 and multiply that constant term separately.
4198 This helps produce common subexpressions. */
4200 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4201 && ! TREE_CONSTANT (intop)
4202 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4203 && TREE_CONSTANT (size_exp))
4205 enum tree_code subcode = resultcode;
4206 if (TREE_CODE (intop) == MINUS_EXPR)
4207 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4208 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4209 intop = TREE_OPERAND (intop, 0);
4212 /* Convert the integer argument to a type the same size as sizetype
4213 so the multiply won't overflow spuriously. */
4215 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4216 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4218 /* Replace the integer argument with a suitable product by the object size.
4219 Do this multiplication as signed, then convert to the appropriate
4220 pointer type (actually unsigned integral). */
4222 intop = cp_convert (result_type,
4223 build_binary_op (MULT_EXPR, intop,
4224 cp_convert (TREE_TYPE (intop),
4227 /* Create the sum or difference. */
4229 result = build (resultcode, result_type, ptrop, intop);
4231 folded = fold (result);
4232 if (folded == result)
4233 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4237 /* Return a tree for the difference of pointers OP0 and OP1.
4238 The resulting tree has type int. */
4241 pointer_diff (op0, op1, ptrtype)
4242 register tree op0, op1;
4243 register tree ptrtype;
4245 register tree result, folded;
4246 tree restype = ptrdiff_type_node;
4247 tree target_type = TREE_TYPE (ptrtype);
4249 if (!complete_type_or_else (target_type, NULL_TREE))
4250 return error_mark_node;
4252 if (pedantic || warn_pointer_arith)
4254 if (TREE_CODE (target_type) == VOID_TYPE)
4255 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4256 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4257 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4258 if (TREE_CODE (target_type) == METHOD_TYPE)
4259 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4260 if (TREE_CODE (target_type) == OFFSET_TYPE)
4261 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4264 /* First do the subtraction as integers;
4265 then drop through to build the divide operator. */
4267 op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4268 cp_convert (restype, op1));
4270 /* This generates an error if op1 is a pointer to an incomplete type. */
4271 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4272 error ("arithmetic on pointer to an incomplete type");
4274 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4275 || TREE_CODE (target_type) == FUNCTION_TYPE
4276 || TREE_CODE (target_type) == METHOD_TYPE
4277 || TREE_CODE (target_type) == OFFSET_TYPE)
4279 : size_in_bytes (target_type));
4281 /* Do the division. */
4283 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4285 folded = fold (result);
4286 if (folded == result)
4287 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4291 /* Handle the case of taking the address of a COMPONENT_REF.
4292 Called by `build_unary_op'.
4294 ARG is the COMPONENT_REF whose address we want.
4295 ARGTYPE is the pointer type that this address should have. */
4298 build_component_addr (arg, argtype)
4301 tree field = TREE_OPERAND (arg, 1);
4302 tree basetype = decl_type_context (field);
4303 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4305 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4307 if (DECL_C_BIT_FIELD (field))
4309 cp_error ("attempt to take address of bit-field structure member `%D'",
4311 return error_mark_node;
4314 if (TREE_CODE (field) == FIELD_DECL
4315 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4317 /* Can't convert directly to ARGTYPE, since that
4318 may have the same pointer type as one of our
4320 rval = build1 (NOP_EXPR, argtype,
4321 convert_pointer_to (basetype, rval));
4322 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4325 /* This conversion is harmless. */
4326 rval = convert_force (argtype, rval, 0);
4328 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4330 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4331 size_int (BITS_PER_UNIT));
4332 int flag = TREE_CONSTANT (rval);
4333 offset = convert (sizetype, offset);
4334 rval = fold (build (PLUS_EXPR, argtype,
4335 rval, cp_convert (argtype, offset)));
4336 TREE_CONSTANT (rval) = flag;
4341 /* Construct and perhaps optimize a tree representation
4342 for a unary operation. CODE, a tree_code, specifies the operation
4343 and XARG is the operand. */
4346 build_x_unary_op (code, xarg)
4347 enum tree_code code;
4350 if (processing_template_decl)
4351 return build_min_nt (code, xarg, NULL_TREE);
4353 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4355 if (code == ADDR_EXPR
4356 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4357 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4358 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4359 || (TREE_CODE (xarg) == OFFSET_REF)))
4360 /* don't look for a function */;
4365 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4366 NULL_TREE, NULL_TREE);
4367 if (rval || code != ADDR_EXPR)
4371 if (code == ADDR_EXPR)
4373 if (TREE_CODE (xarg) == TARGET_EXPR)
4374 warning ("taking address of temporary");
4377 return build_unary_op (code, xarg, 0);
4380 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4383 condition_conversion (expr)
4387 if (processing_template_decl)
4389 t = cp_convert (boolean_type_node, expr);
4390 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4394 /* C++: Must handle pointers to members.
4396 Perhaps type instantiation should be extended to handle conversion
4397 from aggregates to types we don't yet know we want? (Or are those
4398 cases typically errors which should be reported?)
4400 NOCONVERT nonzero suppresses the default promotions
4401 (such as from short to int). */
4404 build_unary_op (code, xarg, noconvert)
4405 enum tree_code code;
4409 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4410 register tree arg = xarg;
4411 register tree argtype = 0;
4412 const char *errstring = NULL;
4415 if (arg == error_mark_node)
4416 return error_mark_node;
4421 /* This is used for unary plus, because a CONVERT_EXPR
4422 is enough to prevent anybody from looking inside for
4423 associativity, but won't generate any code. */
4424 if (!(arg = build_expr_type_conversion
4425 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4426 errstring = "wrong type argument to unary plus";
4430 arg = default_conversion (arg);
4431 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4432 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4437 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4438 errstring = "wrong type argument to unary minus";
4439 else if (!noconvert)
4440 arg = default_conversion (arg);
4444 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4448 arg = default_conversion (arg);
4450 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4452 errstring = "wrong type argument to bit-complement";
4453 else if (!noconvert)
4454 arg = default_conversion (arg);
4458 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4459 errstring = "wrong type argument to abs";
4460 else if (!noconvert)
4461 arg = default_conversion (arg);
4465 /* Conjugating a real value is a no-op, but allow it anyway. */
4466 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4467 errstring = "wrong type argument to conjugation";
4468 else if (!noconvert)
4469 arg = default_conversion (arg);
4472 case TRUTH_NOT_EXPR:
4473 arg = cp_convert (boolean_type_node, arg);
4474 val = invert_truthvalue (arg);
4475 if (arg != error_mark_node)
4477 errstring = "in argument to unary !";
4484 if (TREE_CODE (arg) == COMPLEX_CST)
4485 return TREE_REALPART (arg);
4486 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4487 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4492 if (TREE_CODE (arg) == COMPLEX_CST)
4493 return TREE_IMAGPART (arg);
4494 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4495 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4497 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4499 case PREINCREMENT_EXPR:
4500 case POSTINCREMENT_EXPR:
4501 case PREDECREMENT_EXPR:
4502 case POSTDECREMENT_EXPR:
4503 /* Handle complex lvalues (when permitted)
4504 by reduction to simpler cases. */
4506 val = unary_complex_lvalue (code, arg);
4510 /* Increment or decrement the real part of the value,
4511 and don't change the imaginary part. */
4512 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4516 arg = stabilize_reference (arg);
4517 real = build_unary_op (REALPART_EXPR, arg, 1);
4518 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4519 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4520 build_unary_op (code, real, 1), imag);
4523 /* Report invalid types. */
4525 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4528 if (code == PREINCREMENT_EXPR)
4529 errstring ="no pre-increment operator for type";
4530 else if (code == POSTINCREMENT_EXPR)
4531 errstring ="no post-increment operator for type";
4532 else if (code == PREDECREMENT_EXPR)
4533 errstring ="no pre-decrement operator for type";
4535 errstring ="no post-decrement operator for type";
4539 /* Report something read-only. */
4541 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4542 || TREE_READONLY (arg))
4543 readonly_error (arg, ((code == PREINCREMENT_EXPR
4544 || code == POSTINCREMENT_EXPR)
4545 ? "increment" : "decrement"),
4550 tree result_type = TREE_TYPE (arg);
4552 arg = get_unwidened (arg, 0);
4553 argtype = TREE_TYPE (arg);
4555 /* ARM $5.2.5 last annotation says this should be forbidden. */
4556 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4557 pedwarn ("ANSI C++ forbids %sing an enum",
4558 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4559 ? "increment" : "decrement");
4561 /* Compute the increment. */
4563 if (TREE_CODE (argtype) == POINTER_TYPE)
4565 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4566 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4567 cp_error ("cannot %s a pointer to incomplete type `%T'",
4568 ((code == PREINCREMENT_EXPR
4569 || code == POSTINCREMENT_EXPR)
4570 ? "increment" : "decrement"), TREE_TYPE (argtype));
4571 else if ((pedantic || warn_pointer_arith)
4572 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4573 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4574 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4575 ((code == PREINCREMENT_EXPR
4576 || code == POSTINCREMENT_EXPR)
4577 ? "increment" : "decrement"), argtype);
4578 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4581 inc = integer_one_node;
4583 inc = cp_convert (argtype, inc);
4585 /* Handle incrementing a cast-expression. */
4587 switch (TREE_CODE (arg))
4592 case FIX_TRUNC_EXPR:
4593 case FIX_FLOOR_EXPR:
4594 case FIX_ROUND_EXPR:
4597 tree incremented, modify, value, compound;
4598 if (! lvalue_p (arg) && pedantic)
4599 pedwarn ("cast to non-reference type used as lvalue");
4600 arg = stabilize_reference (arg);
4601 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4604 value = save_expr (arg);
4605 incremented = build (((code == PREINCREMENT_EXPR
4606 || code == POSTINCREMENT_EXPR)
4607 ? PLUS_EXPR : MINUS_EXPR),
4608 argtype, value, inc);
4609 TREE_SIDE_EFFECTS (incremented) = 1;
4611 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4612 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4614 /* Eliminate warning about unused result of + or -. */
4615 TREE_NO_UNUSED_WARNING (compound) = 1;
4623 /* Complain about anything else that is not a true lvalue. */
4624 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4625 || code == POSTINCREMENT_EXPR)
4626 ? "increment" : "decrement")))
4627 return error_mark_node;
4629 /* Forbid using -- on `bool'. */
4630 if (TREE_TYPE (arg) == boolean_type_node)
4632 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4634 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4635 return error_mark_node;
4638 /* This will only work if someone can convince Kenner to accept
4639 my patch to expand_increment. (jason) */
4640 val = build (code, TREE_TYPE (arg), arg, inc);
4642 if (code == POSTINCREMENT_EXPR)
4644 arg = stabilize_reference (arg);
4645 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4647 TREE_SIDE_EFFECTS (val) = 1;
4648 arg = save_expr (arg);
4649 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4650 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4653 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4658 val = build (code, TREE_TYPE (arg), arg, inc);
4660 TREE_SIDE_EFFECTS (val) = 1;
4661 return cp_convert (result_type, val);
4665 /* Note that this operation never does default_conversion
4666 regardless of NOCONVERT. */
4668 argtype = lvalue_type (arg);
4669 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4673 build_pointer_type (TREE_TYPE (argtype)), arg);
4674 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4677 else if (pedantic && DECL_MAIN_P (arg))
4679 pedwarn ("taking address of function `main'");
4681 /* Let &* cancel out to simplify resulting code. */
4682 if (TREE_CODE (arg) == INDIRECT_REF)
4684 /* We don't need to have `current_class_ptr' wrapped in a
4685 NON_LVALUE_EXPR node. */
4686 if (arg == current_class_ref)
4687 return current_class_ptr;
4689 arg = TREE_OPERAND (arg, 0);
4690 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4694 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4695 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4697 else if (lvalue_p (arg))
4698 /* Don't let this be an lvalue. */
4699 return non_lvalue (arg);
4703 /* For &x[y], return x+y */
4704 if (TREE_CODE (arg) == ARRAY_REF)
4706 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4707 return error_mark_node;
4708 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4709 TREE_OPERAND (arg, 1));
4712 /* Uninstantiated types are all functions. Taking the
4713 address of a function is a no-op, so just return the
4716 if (TREE_CODE (arg) == IDENTIFIER_NODE
4717 && IDENTIFIER_OPNAME_P (arg))
4719 my_friendly_abort (117);
4720 /* We don't know the type yet, so just work around the problem.
4721 We know that this will resolve to an lvalue. */
4722 return build1 (ADDR_EXPR, unknown_type_node, arg);
4725 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4726 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4728 /* They're trying to take the address of a unique non-static
4729 member function. This is ill-formed, but let's try to DTRT. */
4732 if (current_class_type
4733 && TREE_OPERAND (arg, 0) == current_class_ref)
4734 /* An expression like &memfn. */
4735 pedwarn ("taking the address of a non-static member function");
4737 pedwarn ("taking the address of a bound member function");
4739 base = TREE_TYPE (TREE_OPERAND (arg, 0));
4740 name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4742 cp_pedwarn (" to form a pointer to member function, say `&%T::%D'",
4744 arg = build_offset_ref (base, name);
4747 if (type_unknown_p (arg))
4748 return build1 (ADDR_EXPR, unknown_type_node, arg);
4750 /* Handle complex lvalues (when permitted)
4751 by reduction to simpler cases. */
4752 val = unary_complex_lvalue (code, arg);
4756 switch (TREE_CODE (arg))
4761 case FIX_TRUNC_EXPR:
4762 case FIX_FLOOR_EXPR:
4763 case FIX_ROUND_EXPR:
4765 if (! lvalue_p (arg) && pedantic)
4766 pedwarn ("taking the address of a cast to non-reference type");
4773 /* Allow the address of a constructor if all the elements
4775 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4776 && TREE_CONSTANT (arg))
4778 /* Anything not already handled and not a true memory reference
4780 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4781 && TREE_CODE (argtype) != METHOD_TYPE
4782 && !lvalue_or_else (arg, "unary `&'"))
4783 return error_mark_node;
4785 if (argtype != error_mark_node)
4786 argtype = build_pointer_type (argtype);
4788 if (mark_addressable (arg) == 0)
4789 return error_mark_node;
4794 if (TREE_CODE (arg) == COMPONENT_REF)
4795 addr = build_component_addr (arg, argtype);
4797 addr = build1 (ADDR_EXPR, argtype, arg);
4799 /* Address of a static or external variable or
4800 function counts as a constant */
4802 TREE_CONSTANT (addr) = 1;
4804 if (TREE_CODE (argtype) == POINTER_TYPE
4805 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4807 build_ptrmemfunc_type (argtype);
4808 addr = build_ptrmemfunc (argtype, addr, 0);
4821 argtype = TREE_TYPE (arg);
4822 return fold (build1 (code, argtype, arg));
4826 return error_mark_node;
4830 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4831 convert ARG with the same conversions in the same order
4832 and return the result. */
4835 convert_sequence (conversions, arg)
4839 switch (TREE_CODE (conversions))
4844 case FIX_TRUNC_EXPR:
4845 case FIX_FLOOR_EXPR:
4846 case FIX_ROUND_EXPR:
4848 return cp_convert (TREE_TYPE (conversions),
4849 convert_sequence (TREE_OPERAND (conversions, 0),
4858 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4859 for certain kinds of expressions which are not really lvalues
4860 but which we can accept as lvalues.
4862 If ARG is not a kind of expression we can handle, return zero. */
4865 unary_complex_lvalue (code, arg)
4866 enum tree_code code;
4869 /* Handle (a, b) used as an "lvalue". */
4870 if (TREE_CODE (arg) == COMPOUND_EXPR)
4872 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4873 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4874 TREE_OPERAND (arg, 0), real_result);
4877 /* Handle (a ? b : c) used as an "lvalue". */
4878 if (TREE_CODE (arg) == COND_EXPR
4879 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4880 return rationalize_conditional_expr (code, arg);
4882 if (TREE_CODE (arg) == MODIFY_EXPR
4883 || TREE_CODE (arg) == PREINCREMENT_EXPR
4884 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4885 return unary_complex_lvalue
4886 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4887 arg, TREE_OPERAND (arg, 0)));
4889 if (code != ADDR_EXPR)
4892 /* Handle (a = b) used as an "lvalue" for `&'. */
4893 if (TREE_CODE (arg) == MODIFY_EXPR
4894 || TREE_CODE (arg) == INIT_EXPR)
4896 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4897 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4898 TREE_NO_UNUSED_WARNING (arg) = 1;
4902 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4903 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4904 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4906 /* The representation of something of type OFFSET_TYPE
4907 is really the representation of a pointer to it.
4908 Here give the representation its true type. */
4911 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4913 if (TREE_CODE (arg) != OFFSET_REF)
4916 t = TREE_OPERAND (arg, 1);
4918 /* Check all this code for right semantics. */
4919 if (TREE_CODE (t) == FUNCTION_DECL)
4921 if (DECL_DESTRUCTOR_P (t))
4922 cp_error ("taking address of destructor");
4923 return build_unary_op (ADDR_EXPR, t, 0);
4925 if (TREE_CODE (t) == VAR_DECL)
4926 return build_unary_op (ADDR_EXPR, t, 0);
4931 if (TREE_OPERAND (arg, 0)
4932 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4933 && TREE_CODE (t) != FIELD_DECL)
4935 cp_error ("taking address of bound pointer-to-member expression");
4936 return error_mark_node;
4939 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4940 type = build_pointer_type (type);
4942 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4948 /* We permit compiler to make function calls returning
4949 objects of aggregate type look like lvalues. */
4953 if (TREE_CODE (targ) == SAVE_EXPR)
4954 targ = TREE_OPERAND (targ, 0);
4956 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4958 if (TREE_CODE (arg) == SAVE_EXPR)
4961 targ = build_cplus_new (TREE_TYPE (arg), arg);
4962 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4965 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4966 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4967 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4970 /* Don't let anything else be handled specially. */
4974 /* Mark EXP saying that we need to be able to take the
4975 address of it; it should not be allocated in a register.
4976 Value is 1 if successful.
4978 C++: we do not allow `current_class_ptr' to be addressable. */
4981 mark_addressable (exp)
4984 register tree x = exp;
4986 if (TREE_ADDRESSABLE (x) == 1)
4990 switch (TREE_CODE (x))
4997 x = TREE_OPERAND (x, 0);
5001 if (x == current_class_ptr)
5003 if (! flag_this_is_variable)
5004 error ("address of `this' not available");
5005 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
5006 put_var_into_stack (x);
5010 if (TREE_STATIC (x) && TREE_READONLY (x)
5011 && DECL_RTL (x) != 0
5012 && ! DECL_IN_MEMORY_P (x))
5014 /* We thought this would make a good constant variable,
5015 but we were wrong. */
5016 push_obstacks_nochange ();
5017 end_temporary_allocation ();
5019 TREE_ASM_WRITTEN (x) = 0;
5021 rest_of_decl_compilation (x, 0,
5022 !DECL_FUNCTION_SCOPE_P (x),
5024 TREE_ADDRESSABLE (x) = 1;
5030 /* Caller should not be trying to mark initialized
5031 constant fields addressable. */
5032 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
5033 || DECL_IN_AGGR_P (x) == 0
5035 || DECL_EXTERNAL (x), 314);
5039 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5040 && !DECL_ARTIFICIAL (x) && extra_warnings)
5041 cp_warning ("address requested for `%D', which is declared `register'",
5043 put_var_into_stack (x);
5044 TREE_ADDRESSABLE (x) = 1;
5048 if (DECL_LANG_SPECIFIC (x) != 0)
5050 x = DECL_MAIN_VARIANT (x);
5051 /* We have to test both conditions here. The first may be
5052 non-zero in the case of processing a default function. The
5053 second may be non-zero in the case of a template function. */
5054 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
5057 TREE_ADDRESSABLE (x) = 1;
5059 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
5063 TREE_ADDRESSABLE (x) = 1;
5067 TREE_ADDRESSABLE (x) = 1;
5068 mark_addressable (TREE_OPERAND (x, 0));
5076 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5079 build_x_conditional_expr (ifexp, op1, op2)
5080 tree ifexp, op1, op2;
5082 if (processing_template_decl)
5083 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5085 return build_conditional_expr (ifexp, op1, op2);
5088 /* Handle overloading of the ',' operator when needed. Otherwise,
5089 this function just builds an expression list. */
5092 build_x_compound_expr (list)
5095 tree rest = TREE_CHAIN (list);
5098 if (processing_template_decl)
5099 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5101 if (rest == NULL_TREE)
5102 return build_compound_expr (list);
5104 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5105 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5107 return build_x_compound_expr (expr_tree_cons (NULL_TREE, result,
5108 TREE_CHAIN (rest)));
5110 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5112 /* the left-hand operand of a comma expression is like an expression
5113 statement: we should warn if it doesn't have any side-effects,
5114 unless it was explicitly cast to (void). */
5115 if ((extra_warnings || warn_unused)
5116 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5117 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5118 warning("left-hand operand of comma expression has no effect");
5120 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5121 else if (warn_unused)
5122 warn_if_unused_value (TREE_VALUE(list));
5125 return build_compound_expr
5126 (expr_tree_cons (NULL_TREE, TREE_VALUE (list),
5127 build_expr_list (NULL_TREE,
5128 build_x_compound_expr (rest))));
5131 /* Given a list of expressions, return a compound expression
5132 that performs them all and returns the value of the last of them. */
5135 build_compound_expr (list)
5141 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5142 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5144 if (TREE_CHAIN (list) == 0)
5146 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5147 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5148 if (TREE_CODE (list) == NOP_EXPR
5149 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5150 list = TREE_OPERAND (list, 0);
5152 /* Convert arrays to pointers. */
5153 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5154 return default_conversion (TREE_VALUE (list));
5156 return TREE_VALUE (list);
5159 first = TREE_VALUE (list);
5160 first = require_complete_type_in_void (first);
5161 if (first == error_mark_node)
5162 return error_mark_node;
5164 rest = build_compound_expr (TREE_CHAIN (list));
5165 if (rest == error_mark_node)
5166 return error_mark_node;
5168 /* When pedantic, a compound expression cannot be a constant expression. */
5169 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5172 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5173 break_out_cleanups (first), rest);
5177 build_static_cast (type, expr)
5183 if (type == error_mark_node || expr == error_mark_node)
5184 return error_mark_node;
5186 if (TREE_CODE (expr) == OFFSET_REF)
5187 expr = resolve_offset_ref (expr);
5189 if (processing_template_decl)
5191 tree t = build_min (STATIC_CAST_EXPR, copy_to_permanent (type),
5196 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5197 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5198 if (TREE_CODE (type) != REFERENCE_TYPE
5199 && TREE_CODE (expr) == NOP_EXPR
5200 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5201 expr = TREE_OPERAND (expr, 0);
5203 if (TREE_CODE (type) == VOID_TYPE)
5204 return build1 (CONVERT_EXPR, type, expr);
5206 if (TREE_CODE (type) == REFERENCE_TYPE)
5207 return (convert_from_reference
5208 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5209 LOOKUP_COMPLAIN, NULL_TREE)));
5211 if (IS_AGGR_TYPE (type))
5212 return build_cplus_new
5213 (type, (build_method_call
5214 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5215 TYPE_BINFO (type), LOOKUP_NORMAL)));
5217 expr = decay_conversion (expr);
5218 intype = TREE_TYPE (expr);
5220 /* FIXME handle casting to array type. */
5223 if (can_convert_arg (type, intype, expr))
5225 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5228 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5229 && at_least_as_qualified_p (TREE_TYPE (type),
5231 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5232 && ! TREE_VIA_VIRTUAL (binfo))
5235 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5237 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5238 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))))
5239 && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type)),
5240 TREE_TYPE (TREE_TYPE (intype)))
5241 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5242 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5243 && ! TREE_VIA_VIRTUAL (binfo))
5246 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5247 && TREE_CODE (type) != ARRAY_TYPE
5248 && TREE_CODE (type) != FUNCTION_TYPE
5249 && can_convert (intype, type))
5252 /* [expr.static.cast]
5254 The static_cast operator shall not be used to cast away
5256 if (ok && casts_away_constness (intype, type))
5258 cp_error ("static_cast from `%T' to `%T' casts away constness",
5260 return error_mark_node;
5264 return build_c_cast (type, expr);
5266 cp_error ("static_cast from `%T' to `%T'", intype, type);
5267 return error_mark_node;
5271 build_reinterpret_cast (type, expr)
5276 if (type == error_mark_node || expr == error_mark_node)
5277 return error_mark_node;
5279 if (TREE_CODE (expr) == OFFSET_REF)
5280 expr = resolve_offset_ref (expr);
5282 if (processing_template_decl)
5284 tree t = build_min (REINTERPRET_CAST_EXPR,
5285 copy_to_permanent (type), expr);
5289 if (TREE_CODE (type) != REFERENCE_TYPE)
5291 expr = decay_conversion (expr);
5293 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5294 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5295 if (TREE_CODE (expr) == NOP_EXPR
5296 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5297 expr = TREE_OPERAND (expr, 0);
5300 intype = TREE_TYPE (expr);
5302 if (TREE_CODE (type) == REFERENCE_TYPE)
5304 if (! real_lvalue_p (expr))
5306 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5307 return error_mark_node;
5309 expr = build_unary_op (ADDR_EXPR, expr, 0);
5310 if (expr != error_mark_node)
5311 expr = build_reinterpret_cast
5312 (build_pointer_type (TREE_TYPE (type)), expr);
5313 if (expr != error_mark_node)
5314 expr = build_indirect_ref (expr, 0);
5317 else if (same_type_p (TYPE_MAIN_VARIANT (intype),
5318 TYPE_MAIN_VARIANT (type)))
5319 return build_static_cast (type, expr);
5321 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5322 || TREE_CODE (intype) == ENUMERAL_TYPE))
5324 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5326 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5327 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5330 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5331 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5333 if (TREE_READONLY_DECL_P (expr))
5334 expr = decl_constant_value (expr);
5335 return fold (build1 (NOP_EXPR, type, expr));
5337 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5338 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5340 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5341 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5344 if (TREE_READONLY_DECL_P (expr))
5345 expr = decl_constant_value (expr);
5346 return fold (build1 (NOP_EXPR, type, expr));
5348 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5349 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5351 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5352 if (TREE_READONLY_DECL_P (expr))
5353 expr = decl_constant_value (expr);
5354 return fold (build1 (NOP_EXPR, type, expr));
5358 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5359 return error_mark_node;
5362 return cp_convert (type, expr);
5366 build_const_cast (type, expr)
5371 if (type == error_mark_node || expr == error_mark_node)
5372 return error_mark_node;
5374 if (TREE_CODE (expr) == OFFSET_REF)
5375 expr = resolve_offset_ref (expr);
5377 if (processing_template_decl)
5379 tree t = build_min (CONST_CAST_EXPR, copy_to_permanent (type),
5384 if (!POINTER_TYPE_P (type))
5386 cp_error ("`%T' is not a pointer, reference, or pointer-to-data-member type",
5388 cp_error ("as required by const_cast");
5390 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5392 cp_error ("`%T' is a pointer or reference to a function type",
5394 cp_error ("which is forbidden by const_cast");
5395 return error_mark_node;
5398 if (TREE_CODE (type) != REFERENCE_TYPE)
5400 expr = decay_conversion (expr);
5402 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5403 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5404 if (TREE_CODE (expr) == NOP_EXPR
5405 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5406 expr = TREE_OPERAND (expr, 0);
5409 intype = TREE_TYPE (expr);
5411 if (same_type_p (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type)))
5412 return build_static_cast (type, expr);
5413 else if (TREE_CODE (type) == REFERENCE_TYPE)
5415 if (! real_lvalue_p (expr))
5417 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5418 return error_mark_node;
5421 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5423 expr = build_unary_op (ADDR_EXPR, expr, 0);
5424 expr = build1 (NOP_EXPR, type, expr);
5425 return convert_from_reference (expr);
5428 else if (TREE_CODE (type) == POINTER_TYPE
5429 && TREE_CODE (intype) == POINTER_TYPE
5430 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5431 return cp_convert (type, expr);
5433 cp_error ("const_cast from `%T' to `%T'", intype, type);
5434 return error_mark_node;
5437 /* Build an expression representing a cast to type TYPE of expression EXPR.
5439 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5440 when doing the cast. */
5443 build_c_cast (type, expr)
5446 register tree value = expr;
5449 if (type == error_mark_node || expr == error_mark_node)
5450 return error_mark_node;
5452 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5453 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5454 if (TREE_CODE (type) != REFERENCE_TYPE
5455 && TREE_CODE (value) == NOP_EXPR
5456 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5457 value = TREE_OPERAND (value, 0);
5459 if (TREE_CODE (value) == OFFSET_REF)
5460 value = resolve_offset_ref (value);
5462 if (TREE_CODE (type) == ARRAY_TYPE)
5464 /* Allow casting from T1* to T2[] because Cfront allows it.
5465 NIHCL uses it. It is not valid ANSI C however, and hence, not
5467 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5470 pedwarn ("ANSI C++ forbids casting to an array type");
5471 type = build_pointer_type (TREE_TYPE (type));
5475 error ("ANSI C++ forbids casting to an array type");
5476 return error_mark_node;
5480 if (TREE_CODE (type) == FUNCTION_TYPE
5481 || TREE_CODE (type) == METHOD_TYPE)
5483 cp_error ("casting to function type `%T'", type);
5484 return error_mark_node;
5487 if (IS_SIGNATURE (type))
5489 error ("cast specifies signature type");
5490 return error_mark_node;
5493 if (processing_template_decl)
5495 tree t = build_min (CAST_EXPR, type,
5496 min_tree_cons (NULL_TREE, value, NULL_TREE));
5500 /* Convert functions and arrays to pointers and
5501 convert references to their expanded types,
5502 but don't convert any other types. If, however, we are
5503 casting to a class type, there's no reason to do this: the
5504 cast will only succeed if there is a converting constructor,
5505 and the default conversions will be done at that point. In
5506 fact, doing the default conversion here is actually harmful
5510 struct S { S(const A&); };
5512 since we don't want the array-to-pointer conversion done. */
5513 if (!IS_AGGR_TYPE (type))
5515 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5516 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5517 /* Don't do the default conversion on a ->* expression. */
5518 && ! (TREE_CODE (type) == POINTER_TYPE
5519 && bound_pmf_p (value)))
5520 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5521 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5522 value = default_conversion (value);
5524 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5525 /* However, even for class types, we still need to strip away
5526 the reference type, since the call to convert_force below
5527 does not expect the input expression to be of reference
5529 value = convert_from_reference (value);
5531 otype = TREE_TYPE (value);
5533 /* Optionally warn about potentially worrisome casts. */
5536 && TREE_CODE (type) == POINTER_TYPE
5537 && TREE_CODE (otype) == POINTER_TYPE
5538 && !at_least_as_qualified_p (TREE_TYPE (type),
5540 cp_warning ("cast discards qualifiers from pointer target type");
5542 /* Warn about possible alignment problems. */
5543 if (STRICT_ALIGNMENT && warn_cast_align
5544 && TREE_CODE (type) == POINTER_TYPE
5545 && TREE_CODE (otype) == POINTER_TYPE
5546 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5547 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5548 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5549 warning ("cast increases required alignment of target type");
5552 /* We should see about re-enabling these, they seem useful to
5554 if (TREE_CODE (type) == INTEGER_TYPE
5555 && TREE_CODE (otype) == POINTER_TYPE
5556 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5557 warning ("cast from pointer to integer of different size");
5559 if (TREE_CODE (type) == POINTER_TYPE
5560 && TREE_CODE (otype) == INTEGER_TYPE
5561 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5562 /* Don't warn about converting 0 to pointer,
5563 provided the 0 was explicit--not cast or made by folding. */
5564 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5565 warning ("cast to pointer from integer of different size");
5568 if (TREE_CODE (type) == VOID_TYPE)
5570 value = require_complete_type_in_void (value);
5571 if (value != error_mark_node)
5572 value = build1 (CONVERT_EXPR, void_type_node, value);
5574 else if (TREE_CODE (type) == REFERENCE_TYPE)
5575 value = (convert_from_reference
5576 (convert_to_reference (type, value, CONV_C_CAST,
5577 LOOKUP_COMPLAIN, NULL_TREE)));
5582 if (TREE_READONLY_DECL_P (value))
5583 value = decl_constant_value (value);
5586 value = convert_force (type, value, CONV_C_CAST);
5588 /* Ignore any integer overflow caused by the cast. */
5589 if (TREE_CODE (value) == INTEGER_CST)
5591 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5592 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5596 /* Always produce some operator for an explicit cast,
5597 so we can tell (for -pedantic) that the cast is no lvalue. */
5598 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5599 && real_lvalue_p (value))
5600 value = non_lvalue (value);
5605 /* Build an assignment expression of lvalue LHS from value RHS.
5606 MODIFYCODE is the code for a binary operator that we use
5607 to combine the old value of LHS with RHS to get the new value.
5608 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5610 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5613 build_modify_expr (lhs, modifycode, rhs)
5615 enum tree_code modifycode;
5618 register tree result;
5620 tree lhstype = TREE_TYPE (lhs);
5621 tree olhstype = lhstype;
5624 /* Avoid duplicate error messages from operands that had errors. */
5625 if (lhs == error_mark_node || rhs == error_mark_node)
5626 return error_mark_node;
5628 /* Types that aren't fully specified cannot be used in assignments. */
5629 lhs = require_complete_type (lhs);
5633 /* Handle assignment to signature pointers/refs. */
5635 if (TYPE_LANG_SPECIFIC (lhstype)
5636 && (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5638 return build_signature_pointer_constructor (lhs, rhs);
5641 /* Handle control structure constructs used as "lvalues". */
5643 switch (TREE_CODE (lhs))
5645 /* Handle --foo = 5; as these are valid constructs in C++ */
5646 case PREDECREMENT_EXPR:
5647 case PREINCREMENT_EXPR:
5648 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5649 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5650 stabilize_reference (TREE_OPERAND (lhs, 0)),
5651 TREE_OPERAND (lhs, 1));
5652 return build (COMPOUND_EXPR, lhstype,
5654 build_modify_expr (TREE_OPERAND (lhs, 0),
5657 /* Handle (a, b) used as an "lvalue". */
5659 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5661 if (newrhs == error_mark_node)
5662 return error_mark_node;
5663 return build (COMPOUND_EXPR, lhstype,
5664 TREE_OPERAND (lhs, 0), newrhs);
5667 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5668 if (newrhs == error_mark_node)
5669 return error_mark_node;
5670 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5672 /* Handle (a ? b : c) used as an "lvalue". */
5674 rhs = save_expr (rhs);
5676 /* Produce (a ? (b = rhs) : (c = rhs))
5677 except that the RHS goes through a save-expr
5678 so the code to compute it is only emitted once. */
5680 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5681 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5683 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5685 if (cond == error_mark_node)
5687 /* Make sure the code to compute the rhs comes out
5688 before the split. */
5689 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5690 /* Case to void to suppress warning
5691 from warn_if_unused_value. */
5692 cp_convert (void_type_node, rhs), cond);
5699 if (TREE_CODE (lhs) == OFFSET_REF)
5701 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5703 /* Static class member? */
5704 tree member = TREE_OPERAND (lhs, 1);
5705 if (TREE_CODE (member) == VAR_DECL)
5709 compiler_error ("invalid static class member");
5710 return error_mark_node;
5714 lhs = resolve_offset_ref (lhs);
5716 olhstype = lhstype = TREE_TYPE (lhs);
5719 if (lhs == error_mark_node)
5722 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5723 && modifycode != INIT_EXPR)
5725 lhs = convert_from_reference (lhs);
5726 olhstype = lhstype = TREE_TYPE (lhs);
5729 /* If a binary op has been requested, combine the old LHS value with the RHS
5730 producing the value we should actually store into the LHS. */
5732 if (modifycode == INIT_EXPR)
5734 if (! IS_AGGR_TYPE (lhstype))
5735 /* Do the default thing */;
5738 result = build_method_call (lhs, ctor_identifier,
5739 build_expr_list (NULL_TREE, rhs),
5740 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5741 if (result == NULL_TREE)
5742 return error_mark_node;
5746 else if (modifycode == NOP_EXPR)
5748 /* `operator=' is not an inheritable operator. */
5749 if (! IS_AGGR_TYPE (lhstype))
5750 /* Do the default thing */;
5753 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5754 lhs, rhs, make_node (NOP_EXPR));
5755 if (result == NULL_TREE)
5756 return error_mark_node;
5761 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5763 my_friendly_abort (978652);
5767 lhs = stabilize_reference (lhs);
5768 newrhs = build_binary_op (modifycode, lhs, rhs);
5769 if (newrhs == error_mark_node)
5771 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5772 TREE_TYPE (lhs), TREE_TYPE (rhs));
5773 return error_mark_node;
5777 /* Handle a cast used as an "lvalue".
5778 We have already performed any binary operator using the value as cast.
5779 Now convert the result to the cast type of the lhs,
5780 and then true type of the lhs and store it there;
5781 then convert result back to the cast type to be the value
5782 of the assignment. */
5784 switch (TREE_CODE (lhs))
5789 case FIX_TRUNC_EXPR:
5790 case FIX_FLOOR_EXPR:
5791 case FIX_ROUND_EXPR:
5793 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5794 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5795 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5796 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5797 newrhs = default_conversion (newrhs);
5799 tree inner_lhs = TREE_OPERAND (lhs, 0);
5802 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5803 otherwise the result is an rvalue. */
5804 if (! lvalue_p (lhs))
5805 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5807 result = build_modify_expr (inner_lhs, NOP_EXPR,
5808 cp_convert (TREE_TYPE (inner_lhs),
5809 cp_convert (lhstype, newrhs)));
5810 if (result == error_mark_node)
5812 return cp_convert (TREE_TYPE (lhs), result);
5819 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5820 Reject anything strange now. */
5822 if (!lvalue_or_else (lhs, "assignment"))
5823 return error_mark_node;
5825 GNU_xref_assign (lhs);
5827 /* Warn about storing in something that is `const'. */
5828 /* For C++, don't warn if this is initialization. */
5829 if (modifycode != INIT_EXPR
5830 /* For assignment to `const' signature pointer/reference fields,
5831 don't warn either, we already printed a better message before. */
5832 && ! (TREE_CODE (lhs) == COMPONENT_REF
5833 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5834 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5835 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5836 /* Functions are not modifiable, even though they are
5838 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5839 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5840 && C_TYPE_FIELDS_READONLY (lhstype))
5841 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5842 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5843 readonly_error (lhs, "assignment", 0);
5845 /* If storing into a structure or union member,
5846 it has probably been given type `int'.
5847 Compute the type that would go with
5848 the actual amount of storage the member occupies. */
5850 if (TREE_CODE (lhs) == COMPONENT_REF
5851 && (TREE_CODE (lhstype) == INTEGER_TYPE
5852 || TREE_CODE (lhstype) == REAL_TYPE
5853 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5855 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5857 /* If storing in a field that is in actuality a short or narrower
5858 than one, we must store in the field in its actual type. */
5860 if (lhstype != TREE_TYPE (lhs))
5862 lhs = copy_node (lhs);
5863 TREE_TYPE (lhs) = lhstype;
5867 /* check to see if there is an assignment to `this' */
5868 if (lhs == current_class_ptr)
5870 if (flag_this_is_variable > 0
5871 && DECL_NAME (current_function_decl) != NULL_TREE
5872 && (DECL_NAME (current_function_decl)
5873 != constructor_name (current_class_type)))
5874 warning ("assignment to `this' not in constructor or destructor");
5875 current_function_just_assigned_this = 1;
5878 if (modifycode != INIT_EXPR)
5880 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5881 modifycode = NOP_EXPR;
5882 /* Reference-bashing */
5883 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5885 tree tmp = convert_from_reference (lhs);
5886 lhstype = TREE_TYPE (tmp);
5887 if (TYPE_SIZE (lhstype) == 0)
5889 incomplete_type_error (lhs, lhstype);
5890 return error_mark_node;
5895 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5897 tree tmp = convert_from_reference (newrhs);
5898 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5900 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5901 return error_mark_node;
5907 if (TREE_SIDE_EFFECTS (lhs))
5908 lhs = stabilize_reference (lhs);
5909 if (TREE_SIDE_EFFECTS (newrhs))
5910 newrhs = stabilize_reference (newrhs);
5912 /* Convert new value to destination type. */
5914 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5918 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5920 cp_error ("incompatible types in assignment of `%T' to `%T'",
5921 TREE_TYPE (rhs), lhstype);
5922 return error_mark_node;
5925 /* Allow array assignment in compiler-generated code. */
5926 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5927 pedwarn ("ANSI C++ forbids assignment of arrays");
5929 /* Have to wrap this in RTL_EXPR for two cases:
5930 in base or member initialization and if we
5931 are a branch of a ?: operator. Since we
5932 can't easily know the latter, just do it always. */
5934 result = make_node (RTL_EXPR);
5936 TREE_TYPE (result) = void_type_node;
5937 do_pending_stack_adjust ();
5938 start_sequence_for_rtl_expr (result);
5940 /* As a matter of principle, `start_sequence' should do this. */
5943 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5944 ? 1 + (modifycode != INIT_EXPR): 0;
5945 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
5948 do_pending_stack_adjust ();
5950 TREE_SIDE_EFFECTS (result) = 1;
5951 RTL_EXPR_SEQUENCE (result) = get_insns ();
5952 RTL_EXPR_RTL (result) = const0_rtx;
5957 if (modifycode == INIT_EXPR)
5959 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5960 "assignment", NULL_TREE, 0);
5961 if (lhs == DECL_RESULT (current_function_decl))
5963 if (DECL_INITIAL (lhs))
5964 warning ("return value from function receives multiple initializations");
5965 DECL_INITIAL (lhs) = newrhs;
5970 /* Avoid warnings on enum bit fields. */
5971 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5972 && TREE_CODE (lhstype) == INTEGER_TYPE)
5974 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5976 newrhs = convert_force (lhstype, newrhs, 0);
5979 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5981 if (TREE_CODE (newrhs) == CALL_EXPR
5982 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5983 newrhs = build_cplus_new (lhstype, newrhs);
5985 /* Can't initialize directly from a TARGET_EXPR, since that would
5986 cause the lhs to be constructed twice, and possibly result in
5987 accidental self-initialization. So we force the TARGET_EXPR to be
5988 expanded without a target. */
5989 if (TREE_CODE (newrhs) == TARGET_EXPR)
5990 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5991 TREE_OPERAND (newrhs, 0));
5994 if (newrhs == error_mark_node)
5995 return error_mark_node;
5997 if (TREE_CODE (newrhs) == COND_EXPR)
6000 tree cond = TREE_OPERAND (newrhs, 0);
6002 if (TREE_SIDE_EFFECTS (lhs))
6003 cond = build_compound_expr (tree_cons
6005 build_expr_list (NULL_TREE, cond)));
6007 /* Cannot have two identical lhs on this one tree (result) as preexpand
6008 calls will rip them out and fill in RTL for them, but when the
6009 rtl is generated, the calls will only be in the first side of the
6010 condition, not on both, or before the conditional jump! (mrs) */
6011 lhs1 = break_out_calls (lhs);
6014 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6015 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6016 lhstype, lhs, newrhs);
6019 tree result_type = TREE_TYPE (newrhs);
6020 /* We have to convert each arm to the proper type because the
6021 types may have been munged by constant folding. */
6023 = build (COND_EXPR, result_type, cond,
6024 build_modify_expr (lhs, modifycode,
6025 cp_convert (result_type,
6026 TREE_OPERAND (newrhs, 1))),
6027 build_modify_expr (lhs1, modifycode,
6028 cp_convert (result_type,
6029 TREE_OPERAND (newrhs, 2))));
6033 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6034 lhstype, lhs, newrhs);
6036 TREE_SIDE_EFFECTS (result) = 1;
6038 /* If we got the LHS in a different type for storing in,
6039 convert the result back to the nominal type of LHS
6040 so that the value we return always has the same type
6041 as the LHS argument. */
6043 if (olhstype == TREE_TYPE (result))
6045 /* Avoid warnings converting integral types back into enums
6046 for enum bit fields. */
6047 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6048 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6050 result = build (COMPOUND_EXPR, olhstype, result, olhs);
6051 TREE_NO_UNUSED_WARNING (result) = 1;
6054 return convert_for_assignment (olhstype, result, "assignment",
6059 build_x_modify_expr (lhs, modifycode, rhs)
6061 enum tree_code modifycode;
6064 if (processing_template_decl)
6065 return build_min_nt (MODOP_EXPR, lhs,
6066 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6068 if (modifycode != NOP_EXPR)
6070 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6071 make_node (modifycode));
6075 return build_modify_expr (lhs, modifycode, rhs);
6079 /* Get difference in deltas for different pointer to member function
6080 types. Return integer_zero_node, if FROM cannot be converted to a
6081 TO type. If FORCE is true, then allow reverse conversions as well.
6083 Note that the naming of FROM and TO is kind of backwards; the return
6084 value is what we add to a TO in order to get a FROM. They are named
6085 this way because we call this function to find out how to convert from
6086 a pointer to member of FROM to a pointer to member of TO. */
6089 get_delta_difference (from, to, force)
6093 tree delta = integer_zero_node;
6099 /* Should get_base_distance here, so we can check if any thing along the
6100 path is virtual, and we need to make sure we stay
6101 inside the real binfos when going through virtual bases.
6102 Maybe we should replace virtual bases with
6103 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6104 binfo = get_binfo (from, to, 1);
6105 if (binfo == error_mark_node)
6107 error (" in pointer to member function conversion");
6114 error_not_base_type (from, to);
6115 error (" in pointer to member conversion");
6118 binfo = get_binfo (to, from, 1);
6119 if (binfo == 0 || binfo == error_mark_node)
6121 if (TREE_VIA_VIRTUAL (binfo))
6123 binfo = binfo_member (BINFO_TYPE (binfo),
6124 CLASSTYPE_VBASECLASSES (from));
6125 cp_warning ("pointer to member cast to virtual base `%T'",
6126 BINFO_TYPE (binfo));
6127 warning (" will only work if you are very careful");
6129 delta = BINFO_OFFSET (binfo);
6130 delta = cp_convert (ptrdiff_type_node, delta);
6132 return build_binary_op (MINUS_EXPR,
6137 if (TREE_VIA_VIRTUAL (binfo))
6141 cp_warning ("pointer to member cast from virtual base `%T'",
6142 BINFO_TYPE (binfo));
6143 warning (" will only work if you are very careful");
6146 cp_error ("pointer to member conversion from virtual base `%T'",
6147 BINFO_TYPE (binfo));
6150 return BINFO_OFFSET (binfo);
6154 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6155 tree type, delta, idx, pfn, delta2;
6160 /* This is the old way we did it. We want to avoid calling
6161 digest_init, so that it can give an error if we use { } when
6162 initializing a pointer to member function. */
6166 u = build_nt (CONSTRUCTOR, NULL_TREE,
6167 expr_tree_cons (pfn_identifier, pfn, NULL_TREE));
6171 u = build_nt (CONSTRUCTOR, NULL_TREE,
6172 expr_tree_cons (delta2_identifier, delta2, NULL_TREE));
6175 u = build_nt (CONSTRUCTOR, NULL_TREE,
6176 expr_tree_cons (NULL_TREE, delta,
6177 expr_tree_cons (NULL_TREE, idx,
6178 expr_tree_cons (NULL_TREE, u, NULL_TREE))));
6180 return digest_init (type, u, (tree*)0);
6182 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6184 int allconstant, allsimple;
6186 delta_field = TYPE_FIELDS (type);
6187 idx_field = TREE_CHAIN (delta_field);
6188 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6189 subtype = TREE_TYPE (pfn_or_delta2_field);
6190 pfn_field = TYPE_FIELDS (subtype);
6191 delta2_field = TREE_CHAIN (pfn_field);
6195 allconstant = TREE_CONSTANT (pfn);
6196 allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6197 u = expr_tree_cons (pfn_field, pfn, NULL_TREE);
6201 delta2 = convert_and_check (delta_type_node, delta2);
6202 allconstant = TREE_CONSTANT (delta2);
6203 allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6204 u = expr_tree_cons (delta2_field, delta2, NULL_TREE);
6207 delta = convert_and_check (delta_type_node, delta);
6208 idx = convert_and_check (delta_type_node, idx);
6210 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6211 allsimple = allsimple
6212 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6213 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6215 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6216 u = expr_tree_cons (delta_field, delta,
6217 expr_tree_cons (idx_field, idx,
6218 expr_tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6219 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6220 TREE_CONSTANT (u) = allconstant;
6221 TREE_STATIC (u) = allconstant && allsimple;
6226 /* Build a constructor for a pointer to member function. It can be
6227 used to initialize global variables, local variable, or used
6228 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6231 If FORCE is non-zero, then force this conversion, even if
6232 we would rather not do it. Usually set when using an explicit
6235 Return error_mark_node, if something goes wrong. */
6238 build_ptrmemfunc (type, pfn, force)
6243 tree pfn_type = TREE_TYPE (pfn);
6244 tree to_type = build_ptrmemfunc_type (type);
6246 /* Handle multiple conversions of pointer to member functions. */
6247 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6249 tree idx = integer_zero_node;
6250 tree delta = integer_zero_node;
6251 tree delta2 = integer_zero_node;
6252 tree npfn = NULL_TREE;
6253 tree ndelta, ndelta2;
6257 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6258 cp_error ("conversion to `%T' from `%T'",
6261 if (TREE_CODE (pfn) == PTRMEM_CST)
6263 /* We could just build the resulting CONSTRUCTOR now, but we
6264 don't, relying on the general machinery below, together
6265 with constant-folding, to do the right thing. We don't
6266 want to return a PTRMEM_CST here, since a
6267 pointer-to-member constant is no longer a valid template
6268 argument once it is cast to any type, including its
6270 expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
6272 /* This constant points to a non-virtual function.
6273 NDELTA2 will be NULL, but it's value doesn't really
6274 matter since we won't use it anyhow. */
6275 ndelta2 = integer_zero_node;
6277 else if (same_type_p (to_type, pfn_type))
6278 /* We don't have to do any conversion. Note that we do this
6279 after checking for a PTRMEM_CST so that a PTRMEM_CST, cast
6280 to its own type, will not be considered a legal non-type
6281 template argument. */
6285 ndelta = cp_convert (ptrdiff_type_node,
6286 build_component_ref (pfn,
6289 ndelta2 = cp_convert (ptrdiff_type_node,
6290 DELTA2_FROM_PTRMEMFUNC (pfn));
6291 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6294 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6295 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6297 delta = build_binary_op (PLUS_EXPR, ndelta, n);
6298 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n);
6299 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6301 /* If it's a virtual function, this is what we want. */
6302 e2 = build_ptrmemfunc1 (to_type, delta, idx,
6305 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6306 npfn = build1 (NOP_EXPR, type, pfn);
6307 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6309 /* But if it's a non-virtual function, or NULL, we use this
6311 e3 = build_ptrmemfunc1 (to_type, delta,
6312 idx, npfn, NULL_TREE);
6313 return build_conditional_expr (e1, e2, e3);
6316 /* Handle null pointer to member function conversions. */
6317 if (integer_zerop (pfn))
6319 pfn = build_c_cast (type, integer_zero_node);
6320 return build_ptrmemfunc1 (to_type,
6321 integer_zero_node, integer_zero_node,
6325 if (type_unknown_p (pfn))
6326 return instantiate_type (type, pfn, 1);
6328 fn = TREE_OPERAND (pfn, 0);
6329 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6330 return make_ptrmem_cst (to_type, fn);
6333 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6337 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6344 tree type = TREE_TYPE (cst);
6345 tree fn = PTRMEM_CST_MEMBER (cst);
6346 tree ptr_class, fn_class;
6348 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6350 /* The class that the function belongs to. */
6351 fn_class = DECL_CLASS_CONTEXT (fn);
6353 /* The class that we're creating a pointer to member of. */
6354 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6356 /* First, calculate the adjustment to the function's class. */
6357 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6359 if (!DECL_VIRTUAL_P (fn))
6361 *idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6362 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6363 *delta2 = NULL_TREE;
6367 /* If we're dealing with a virtual function, we have to adjust 'this'
6368 again, to point to the base which provides the vtable entry for
6369 fn; the call will do the opposite adjustment. */
6370 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6371 tree binfo = binfo_or_else (orig_class, fn_class);
6372 *delta = size_binop (PLUS_EXPR, *delta, BINFO_OFFSET (binfo));
6374 /* Map everything down one to make room for the null PMF. */
6375 *idx = size_binop (PLUS_EXPR, DECL_VINDEX (fn), integer_one_node);
6378 /* Offset from an object of PTR_CLASS to the vptr for ORIG_CLASS. */
6379 *delta2 = size_binop (PLUS_EXPR, *delta,
6380 get_vfield_offset (TYPE_BINFO (orig_class)));
6384 /* Return an expression for DELTA2 from the pointer-to-member function
6388 delta2_from_ptrmemfunc (t)
6391 if (TREE_CODE (t) == PTRMEM_CST)
6398 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6403 return (build_component_ref
6404 (build_component_ref (t,
6405 pfn_or_delta2_identifier, NULL_TREE,
6407 delta2_identifier, NULL_TREE, 0));
6410 /* Return an expression for PFN from the pointer-to-member function
6414 pfn_from_ptrmemfunc (t)
6417 if (TREE_CODE (t) == PTRMEM_CST)
6424 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6429 return (build_component_ref
6430 (build_component_ref (t,
6431 pfn_or_delta2_identifier, NULL_TREE,
6433 pfn_identifier, NULL_TREE, 0));
6436 /* Convert value RHS to type TYPE as preparation for an assignment to
6437 an lvalue of type TYPE. ERRTYPE is a string to use in error
6438 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6439 are doing the conversion in order to pass the PARMNUMth argument of
6443 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6445 const char *errtype;
6449 register enum tree_code codel = TREE_CODE (type);
6450 register tree rhstype;
6451 register enum tree_code coder;
6453 if (codel == OFFSET_TYPE)
6454 my_friendly_abort (990505);
6456 if (TREE_CODE (rhs) == OFFSET_REF)
6457 rhs = resolve_offset_ref (rhs);
6459 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6460 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6461 rhs = TREE_OPERAND (rhs, 0);
6463 rhstype = TREE_TYPE (rhs);
6464 coder = TREE_CODE (rhstype);
6466 if (rhs == error_mark_node || rhstype == error_mark_node)
6467 return error_mark_node;
6468 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6469 return error_mark_node;
6471 /* Issue warnings about peculiar, but legal, uses of NULL. */
6472 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6473 cp_warning ("converting NULL to non-pointer type");
6475 /* The RHS of an assignment cannot have void type. */
6476 if (coder == VOID_TYPE)
6478 error ("void value not ignored as it ought to be");
6479 return error_mark_node;
6482 /* Simplify the RHS if possible. */
6483 if (TREE_CODE (rhs) == CONST_DECL)
6484 rhs = DECL_INITIAL (rhs);
6485 else if (TREE_READONLY_DECL_P (rhs))
6486 rhs = decl_constant_value (rhs);
6488 /* Warn about assigning a floating-point type to an integer type. */
6489 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6492 cp_warning ("`%T' used for argument %P of `%D'",
6493 rhstype, parmnum, fndecl);
6495 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6497 /* And warn about assigning a negative value to an unsigned
6499 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6501 if (TREE_CODE (rhs) == INTEGER_CST
6502 && TREE_NEGATED_INT (rhs))
6505 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6506 rhs, parmnum, fndecl);
6508 cp_warning ("%s of negative value `%E' to `%T'",
6509 errtype, rhs, type);
6511 overflow_warning (rhs);
6512 if (TREE_CONSTANT (rhs))
6518 The expression is implicitly converted (clause _conv_) to the
6519 cv-unqualified type of the left operand. */
6520 if (!can_convert_arg (type, rhstype, rhs))
6522 /* When -Wno-pmf-converions is use, we just silently allow
6523 conversions from pointers-to-members to plain pointers. If
6524 the conversion doesn't work, cp_convert will complain. */
6526 && TYPE_PTR_P (type)
6527 && TYPE_PTRMEMFUNC_P (rhstype))
6528 rhs = cp_convert (strip_top_quals (type), rhs);
6529 /* If the right-hand side has unknown type, then it is an
6530 overloaded function. Call instantiate_type to get error
6532 else if (rhstype == unknown_type_node)
6533 instantiate_type (type, rhs, 1);
6535 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6536 rhstype, type, parmnum, fndecl);
6538 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6540 return error_mark_node;
6542 return perform_implicit_conversion (strip_top_quals (type), rhs);
6545 /* Convert RHS to be of type TYPE.
6546 If EXP is non-zero, it is the target of the initialization.
6547 ERRTYPE is a string to use in error messages.
6549 Two major differences between the behavior of
6550 `convert_for_assignment' and `convert_for_initialization'
6551 are that references are bashed in the former, while
6552 copied in the latter, and aggregates are assigned in
6553 the former (operator=) while initialized in the
6556 If using constructor make sure no conversion operator exists, if one does
6557 exist, an ambiguity exists.
6559 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6562 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6563 tree exp, type, rhs;
6565 const char *errtype;
6569 register enum tree_code codel = TREE_CODE (type);
6570 register tree rhstype;
6571 register enum tree_code coder;
6573 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6574 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6575 if (TREE_CODE (rhs) == NOP_EXPR
6576 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6577 && codel != REFERENCE_TYPE)
6578 rhs = TREE_OPERAND (rhs, 0);
6580 if (rhs == error_mark_node
6581 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6582 return error_mark_node;
6584 if (TREE_CODE (rhs) == OFFSET_REF)
6586 rhs = resolve_offset_ref (rhs);
6587 if (rhs == error_mark_node)
6588 return error_mark_node;
6591 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6592 rhs = convert_from_reference (rhs);
6594 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6595 && TREE_CODE (type) != ARRAY_TYPE
6596 && (TREE_CODE (type) != REFERENCE_TYPE
6597 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6598 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6599 && (TREE_CODE (type) != REFERENCE_TYPE
6600 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6601 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6602 rhs = default_conversion (rhs);
6604 rhstype = TREE_TYPE (rhs);
6605 coder = TREE_CODE (rhstype);
6607 if (coder == ERROR_MARK)
6608 return error_mark_node;
6610 /* We accept references to incomplete types, so we can
6611 return here before checking if RHS is of complete type. */
6613 if (codel == REFERENCE_TYPE)
6615 /* This should eventually happen in convert_arguments. */
6616 extern int warningcount, errorcount;
6617 int savew = 0, savee = 0;
6620 savew = warningcount, savee = errorcount;
6621 rhs = initialize_reference (type, rhs);
6624 if (warningcount > savew)
6625 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6626 else if (errorcount > savee)
6627 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6633 exp = require_complete_type (exp);
6634 if (exp == error_mark_node)
6635 return error_mark_node;
6637 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6638 rhstype = TREE_TYPE (rhstype);
6640 type = complete_type (type);
6642 if (TYPE_LANG_SPECIFIC (type)
6643 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
6644 return build_signature_pointer_constructor (type, rhs);
6646 if (IS_AGGR_TYPE (type))
6647 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6649 if (type == TREE_TYPE (rhs))
6651 /* Issue warnings about peculiar, but legal, uses of NULL. We
6652 do this *before* the call to decl_constant_value so as to
6653 avoid duplicate warnings on code like `const int I = NULL;
6655 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6656 cp_warning ("converting NULL to non-pointer type");
6658 if (TREE_READONLY_DECL_P (rhs))
6659 rhs = decl_constant_value (rhs);
6664 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6667 /* Expand an ASM statement with operands, handling output operands
6668 that are not variables or INDIRECT_REFS by transforming such
6669 cases into cases that expand_asm_operands can handle.
6671 Arguments are same as for expand_asm_operands.
6673 We don't do default conversions on all inputs, because it can screw
6674 up operands that are expected to be in memory. */
6677 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6678 tree string, outputs, inputs, clobbers;
6683 int noutputs = list_length (outputs);
6685 /* o[I] is the place that output number I should be written. */
6686 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6689 /* Record the contents of OUTPUTS before it is modified. */
6690 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6691 o[i] = TREE_VALUE (tail);
6693 /* Generate the ASM_OPERANDS insn;
6694 store into the TREE_VALUEs of OUTPUTS some trees for
6695 where the values were actually stored. */
6696 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6698 /* Copy all the intermediate outputs into the specified outputs. */
6699 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6701 if (o[i] != TREE_VALUE (tail))
6703 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6704 const0_rtx, VOIDmode, EXPAND_NORMAL);
6707 /* Detect modification of read-only values.
6708 (Otherwise done by build_modify_expr.) */
6711 tree type = TREE_TYPE (o[i]);
6712 if (CP_TYPE_CONST_P (type)
6713 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6714 && C_TYPE_FIELDS_READONLY (type)))
6715 readonly_error (o[i], "modification by `asm'", 1);
6719 /* Those MODIFY_EXPRs could do autoincrements. */
6723 /* Expand a C `return' statement.
6724 RETVAL is the expression for what to return,
6725 or a null pointer for `return;' with no value.
6727 C++: upon seeing a `return', we must call destructors on all
6728 variables in scope which had constructors called on them.
6729 This means that if in a destructor, the base class destructors
6730 must be called before returning.
6732 The RETURN statement in C++ has initialization semantics. */
6735 c_expand_return (retval)
6738 extern tree dtor_label, ctor_label;
6739 tree result = DECL_RESULT (current_function_decl);
6740 tree valtype = TREE_TYPE (result);
6742 if (TREE_THIS_VOLATILE (current_function_decl))
6743 warning ("function declared `noreturn' has a `return' statement");
6745 if (retval == error_mark_node)
6747 current_function_returns_null = 1;
6751 if (processing_template_decl)
6753 add_tree (build_min_nt (RETURN_STMT, retval));
6760 error ("returning a value from a destructor");
6762 /* Can't just return from a destructor. */
6763 expand_goto (dtor_label);
6767 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6768 if ((DECL_NAME (current_function_decl) == ansi_opname[(int) NEW_EXPR]
6769 || DECL_NAME (current_function_decl) == ansi_opname[(int) VEC_NEW_EXPR])
6770 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6771 && null_ptr_cst_p (retval))
6772 cp_warning ("operator new should throw an exception, not return NULL");
6774 if (retval == NULL_TREE)
6776 /* A non-named return value does not count. */
6778 if (DECL_CONSTRUCTOR_P (current_function_decl))
6779 retval = current_class_ptr;
6780 else if (DECL_NAME (result) != NULL_TREE
6781 && TREE_CODE (valtype) != VOID_TYPE)
6785 current_function_returns_null = 1;
6787 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
6789 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
6791 pedwarn ("`return' with no value, in function returning non-void");
6792 /* Clear this, so finish_function won't say that we
6793 reach the end of a non-void function (which we don't,
6794 we gave a return!). */
6795 current_function_returns_null = 0;
6799 expand_null_return ();
6803 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6805 if (flag_this_is_variable)
6806 error ("return from a constructor: use `this = ...' instead");
6808 error ("returning a value from a constructor");
6809 retval = current_class_ptr;
6812 /* Effective C++ rule 15. See also start_function. */
6814 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
6815 && retval != current_class_ref)
6816 cp_warning ("`operator=' should return a reference to `*this'");
6818 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
6820 current_function_returns_null = 1;
6821 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6822 pedwarn ("`return' with a value, in function returning void");
6823 expand_return (retval);
6827 /* Now deal with possible C++ hair:
6828 (1) Compute the return value.
6829 (2) If there are aggregate values with destructors which
6830 must be cleaned up, clean them (taking care
6831 not to clobber the return value).
6832 (3) If an X(X&) constructor is defined, the return
6833 value must be returned via that. */
6835 if (retval == result
6836 || DECL_CONSTRUCTOR_P (current_function_decl))
6837 /* It's already done for us. */;
6838 else if (TREE_CODE (TREE_TYPE (retval)) == VOID_TYPE)
6840 pedwarn ("return of void value in function returning non-void");
6841 expand_expr_stmt (retval);
6846 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6848 /* First convert the value to the function's return type, then
6849 to the type of return value's location to handle the
6850 case that functype is thiner than the valtype. */
6852 retval = convert_for_initialization
6853 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6854 "return", NULL_TREE, 0);
6856 retval = convert (valtype, retval);
6858 if (retval == error_mark_node)
6860 /* Avoid warning about control reaching end of function. */
6861 expand_null_return ();
6865 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6866 else if (! current_function_returns_struct
6867 && TREE_CODE (retval) == TARGET_EXPR
6868 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6869 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6870 TREE_OPERAND (retval, 0));
6872 /* Add some useful error checking for C++. */
6873 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
6875 tree whats_returned;
6877 /* Sort through common things to see what it is
6878 we are returning. */
6879 whats_returned = retval;
6880 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6882 whats_returned = TREE_OPERAND (whats_returned, 1);
6883 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6884 whats_returned = TREE_OPERAND (whats_returned, 0);
6886 while (TREE_CODE (whats_returned) == CONVERT_EXPR
6887 || TREE_CODE (whats_returned) == NOP_EXPR)
6888 whats_returned = TREE_OPERAND (whats_returned, 0);
6889 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6891 whats_returned = TREE_OPERAND (whats_returned, 0);
6892 while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6893 || TREE_CODE (whats_returned) == TARGET_EXPR)
6895 /* Get the target. */
6896 whats_returned = TREE_OPERAND (whats_returned, 0);
6897 warning ("returning reference to temporary");
6901 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
6903 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
6904 warning ("reference to non-lvalue returned");
6905 else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
6906 && DECL_FUNCTION_SCOPE_P (whats_returned)
6907 && !(TREE_STATIC (whats_returned)
6908 || TREE_PUBLIC (whats_returned)))
6909 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
6912 else if (TREE_CODE (retval) == ADDR_EXPR)
6914 tree whats_returned = TREE_OPERAND (retval, 0);
6916 if (TREE_CODE (whats_returned) == VAR_DECL
6917 && DECL_NAME (whats_returned)
6918 && DECL_FUNCTION_SCOPE_P (whats_returned)
6919 && !(TREE_STATIC (whats_returned)
6920 || TREE_PUBLIC (whats_returned)))
6921 cp_warning_at ("address of local variable `%D' returned", whats_returned);
6925 if (retval != NULL_TREE
6926 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
6927 && ! in_control_zone_p ())
6928 current_function_return_value = retval;
6930 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
6932 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
6933 expand_goto (ctor_label);
6936 if (retval && retval != result)
6938 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6939 TREE_SIDE_EFFECTS (result) = 1;
6942 expand_start_target_temps ();
6944 expand_return (result);
6946 expand_end_target_temps ();
6948 current_function_returns_value = 1;
6951 /* Start a C switch statement, testing expression EXP.
6952 Return EXP if it is valid, an error node otherwise. */
6955 c_expand_start_case (exp)
6960 exp = build_expr_type_conversion (WANT_INT | WANT_ENUM, exp, 1);
6961 if (exp == NULL_TREE)
6963 error ("switch quantity not an integer");
6964 exp = error_mark_node;
6966 if (exp == error_mark_node)
6967 return error_mark_node;
6969 exp = default_conversion (exp);
6970 type = TREE_TYPE (exp);
6971 idx = get_unwidened (exp, 0);
6972 /* We can't strip a conversion from a signed type to an unsigned,
6973 because if we did, int_fits_type_p would do the wrong thing
6974 when checking case values for being in range,
6975 and it's too hard to do the right thing. */
6976 if (TREE_UNSIGNED (TREE_TYPE (exp)) == TREE_UNSIGNED (TREE_TYPE (idx)))
6980 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
6981 type, "switch statement");
6986 /* Returns non-zero if the pointer-type FROM can be converted to the
6987 pointer-type TO via a qualification conversion. If CONSTP is -1,
6988 then we return non-zero if the pointers are similar, and the
6989 cv-qualification signature of FROM is a proper subset of that of TO.
6991 If CONSTP is positive, then all outer pointers have been
6995 comp_ptr_ttypes_real (to, from, constp)
6999 int to_more_cv_qualified = 0;
7001 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7003 if (TREE_CODE (to) != TREE_CODE (from))
7006 if (TREE_CODE (from) == OFFSET_TYPE
7007 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7008 TYPE_OFFSET_BASETYPE (to)))
7011 /* Const and volatile mean something different for function types,
7012 so the usual checks are not appropriate. */
7013 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7015 if (!at_least_as_qualified_p (to, from))
7018 if (!at_least_as_qualified_p (from, to))
7023 ++to_more_cv_qualified;
7027 constp &= TYPE_READONLY (to);
7030 if (TREE_CODE (to) != POINTER_TYPE)
7032 same_type_p (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from))
7033 && (constp >= 0 || to_more_cv_qualified);
7037 /* When comparing, say, char ** to char const **, this function takes the
7038 'char *' and 'char const *'. Do not pass non-pointer types to this
7042 comp_ptr_ttypes (to, from)
7045 return comp_ptr_ttypes_real (to, from, 1);
7048 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7049 type or inheritance-related types, regardless of cv-quals. */
7052 ptr_reasonably_similar (to, from)
7055 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7057 if (TREE_CODE (to) != TREE_CODE (from))
7060 if (TREE_CODE (from) == OFFSET_TYPE
7061 && comptypes (TYPE_OFFSET_BASETYPE (to),
7062 TYPE_OFFSET_BASETYPE (from),
7063 COMPARE_BASE | COMPARE_RELAXED))
7066 if (TREE_CODE (to) != POINTER_TYPE)
7068 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
7069 COMPARE_BASE | COMPARE_RELAXED);
7073 /* Like comp_ptr_ttypes, for const_cast. */
7076 comp_ptr_ttypes_const (to, from)
7079 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7081 if (TREE_CODE (to) != TREE_CODE (from))
7084 if (TREE_CODE (from) == OFFSET_TYPE
7085 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7086 TYPE_OFFSET_BASETYPE (to)))
7089 if (TREE_CODE (to) != POINTER_TYPE)
7090 return same_type_p (TYPE_MAIN_VARIANT (to),
7091 TYPE_MAIN_VARIANT (from));
7095 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7098 comp_ptr_ttypes_reinterpret (to, from)
7103 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7105 if (TREE_CODE (from) == OFFSET_TYPE)
7106 from = TREE_TYPE (from);
7107 if (TREE_CODE (to) == OFFSET_TYPE)
7108 to = TREE_TYPE (to);
7110 /* Const and volatile mean something different for function types,
7111 so the usual checks are not appropriate. */
7112 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7113 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7115 if (!at_least_as_qualified_p (to, from))
7119 && !at_least_as_qualified_p (from, to))
7121 constp &= TYPE_READONLY (to);
7124 if (TREE_CODE (from) != POINTER_TYPE
7125 || TREE_CODE (to) != POINTER_TYPE)
7130 /* Returns the type-qualifier set corresponding to TYPE. */
7133 cp_type_quals (type)
7136 while (TREE_CODE (type) == ARRAY_TYPE)
7137 type = TREE_TYPE (type);
7139 return TYPE_QUALS (type);
7142 /* Returns non-zero if the TYPE contains a mutable member */
7145 cp_has_mutable_p (type)
7148 while (TREE_CODE (type) == ARRAY_TYPE)
7149 type = TREE_TYPE (type);
7151 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7154 /* Subroutine of casts_away_constness. Make T1 and T2 point at
7155 exemplar types such that casting T1 to T2 is casting away castness
7156 if and only if there is no implicit conversion from T1 to T2. */
7159 casts_away_constness_r (t1, t2)
7166 /* [expr.const.cast]
7168 For multi-level pointer to members and multi-level mixed pointers
7169 and pointers to members (conv.qual), the "member" aspect of a
7170 pointer to member level is ignored when determining if a const
7171 cv-qualifier has been cast away. */
7172 if (TYPE_PTRMEM_P (*t1))
7173 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
7174 if (TYPE_PTRMEM_P (*t2))
7175 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
7177 /* [expr.const.cast]
7179 For two pointer types:
7181 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
7182 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
7185 casting from X1 to X2 casts away constness if, for a non-pointer
7186 type T there does not exist an implicit conversion (clause
7189 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7193 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
7195 if (TREE_CODE (*t1) != POINTER_TYPE
7196 || TREE_CODE (*t2) != POINTER_TYPE)
7198 *t1 = cp_build_qualified_type (void_type_node,
7199 CP_TYPE_QUALS (*t1));
7200 *t2 = cp_build_qualified_type (void_type_node,
7201 CP_TYPE_QUALS (*t2));
7205 quals1 = CP_TYPE_QUALS (*t1);
7206 quals2 = CP_TYPE_QUALS (*t2);
7207 *t1 = TREE_TYPE (*t1);
7208 *t2 = TREE_TYPE (*t2);
7209 casts_away_constness_r (t1, t2);
7210 *t1 = build_pointer_type (*t1);
7211 *t2 = build_pointer_type (*t2);
7212 *t1 = cp_build_qualified_type (*t1, quals1);
7213 *t2 = cp_build_qualified_type (*t2, quals2);
7216 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
7220 casts_away_constness (t1, t2)
7224 if (TREE_CODE (t2) == REFERENCE_TYPE)
7226 /* [expr.const.cast]
7228 Casting from an lvalue of type T1 to an lvalue of type T2
7229 using a reference cast casts away constness if a cast from an
7230 rvalue of type "pointer to T1" to the type "pointer to T2"
7231 casts away constness. */
7232 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
7233 ? TREE_TYPE (t1) : t1);
7234 return casts_away_constness (build_pointer_type (t1),
7235 build_pointer_type (TREE_TYPE (t2)));
7238 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7239 /* [expr.const.cast]
7241 Casting from an rvalue of type "pointer to data member of X
7242 of type T1" to the type "pointer to data member of Y of type
7243 T2" casts away constness if a cast from an rvalue of type
7244 "poitner to T1" to the type "pointer to T2" casts away
7246 return casts_away_constness
7247 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7248 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7250 /* Casting away constness is only something that makes sense for
7251 pointer or reference types. */
7252 if (TREE_CODE (t1) != POINTER_TYPE
7253 || TREE_CODE (t2) != POINTER_TYPE)
7256 /* Top-level qualifiers don't matter. */
7257 t1 = TYPE_MAIN_VARIANT (t1);
7258 t2 = TYPE_MAIN_VARIANT (t2);
7259 casts_away_constness_r (&t1, &t2);
7260 if (!can_convert (t2, t1))