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). */
44 static tree convert_for_assignment PROTO((tree, tree, const char *, tree,
46 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
47 static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
48 static int comp_target_parms PROTO((tree, tree, int));
49 static int comp_ptr_ttypes_real PROTO((tree, tree, int));
50 static int comp_ptr_ttypes_const PROTO((tree, tree));
51 static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
52 static int comp_except_types PROTO((tree, tree, int));
53 static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
55 static tree common_base_type PROTO((tree, tree));
57 static tree convert_sequence PROTO((tree, tree));
59 static tree lookup_anon_field PROTO((tree, tree));
60 static tree pointer_diff PROTO((tree, tree, tree));
61 static tree build_component_addr PROTO((tree, tree));
62 static tree qualify_type PROTO((tree, tree));
63 static tree get_delta_difference PROTO((tree, tree, int));
64 static int comp_cv_target_types PROTO((tree, tree, int));
65 static void casts_away_constness_r PROTO((tree *, tree *));
66 static int casts_away_constness PROTO ((tree, tree));
67 static void maybe_warn_about_returning_address_of_local PROTO ((tree));
69 /* Return the target type of TYPE, which means return T for:
70 T*, T&, T[], T (...), and otherwise, just T. */
76 if (TREE_CODE (type) == REFERENCE_TYPE)
77 type = TREE_TYPE (type);
78 while (TREE_CODE (type) == POINTER_TYPE
79 || TREE_CODE (type) == ARRAY_TYPE
80 || TREE_CODE (type) == FUNCTION_TYPE
81 || TREE_CODE (type) == METHOD_TYPE
82 || TREE_CODE (type) == OFFSET_TYPE)
83 type = TREE_TYPE (type);
87 /* Do `exp = require_complete_type (exp);' to make sure exp
88 does not have an incomplete type. (That includes void types.)
89 Returns the error_mark_node if the VALUE does not have
90 complete type when this function returns. */
93 require_complete_type (value)
98 if (processing_template_decl || value == error_mark_node)
101 if (TREE_CODE (value) == OVERLOAD)
102 type = unknown_type_node;
104 type = TREE_TYPE (value);
106 /* First, detect a valid value with a complete type. */
107 if (TYPE_SIZE (type) != 0
108 && TYPE_SIZE (type) != size_zero_node)
111 /* If we see X::Y, we build an OFFSET_TYPE which has
112 not been laid out. Try to avoid an error by interpreting
113 it as this->X::Y, if reasonable. */
114 if (TREE_CODE (value) == OFFSET_REF
115 && current_class_ref != 0
116 && TREE_OPERAND (value, 0) == current_class_ref)
118 tree base, member = TREE_OPERAND (value, 1);
119 tree basetype = TYPE_OFFSET_BASETYPE (type);
120 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
121 base = convert_pointer_to (basetype, current_class_ptr);
122 value = build (COMPONENT_REF, TREE_TYPE (member),
123 build_indirect_ref (base, NULL_PTR), member);
124 return require_complete_type (value);
127 if (complete_type_or_else (type, value))
130 return error_mark_node;
133 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
134 a template instantiation, do the instantiation. Returns TYPE,
135 whether or not it could be completed, unless something goes
136 horribly wrong, in which case the error_mark_node is returned. */
142 if (type == NULL_TREE)
143 /* Rather than crash, we return something sure to cause an error
145 return error_mark_node;
147 if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
149 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
151 tree t = complete_type (TREE_TYPE (type));
152 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
154 TYPE_NEEDS_CONSTRUCTING (type)
155 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
156 TYPE_NEEDS_DESTRUCTOR (type)
157 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
159 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
160 instantiate_class_template (TYPE_MAIN_VARIANT (type));
165 /* Like complete_type, but issue an error if the TYPE cannot be
166 completed. VALUE is used for informative diagnostics.
167 Returns NULL_TREE if the type cannot be made complete. */
170 complete_type_or_else (type, value)
174 type = complete_type (type);
175 if (type == error_mark_node)
176 /* We already issued an error. */
178 else if (!TYPE_SIZE (type) || TYPE_SIZE (type) == size_zero_node)
180 incomplete_type_error (value, type);
187 /* Return truthvalue of whether type of EXP is instantiated. */
193 return (TREE_CODE (exp) == OVERLOAD
194 || TREE_CODE (exp) == TREE_LIST
195 || TREE_TYPE (exp) == unknown_type_node
196 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
197 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
200 /* Return truthvalue of whether T is function (or pfn) type. */
206 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
207 || (TREE_CODE (t) == POINTER_TYPE
208 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
209 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
212 /* Return a variant of TYPE which has all the type qualifiers of LIKE
213 as well as those of TYPE. */
216 qualify_type (type, like)
219 /* @@ Must do member pointers here. */
220 return cp_build_qualified_type (type, (CP_TYPE_QUALS (type)
221 | CP_TYPE_QUALS (like)));
224 /* Return the common type of two parameter lists.
225 We assume that comptypes has already been done and returned 1;
226 if that isn't so, this may crash.
228 As an optimization, free the space we allocate if the parameter
229 lists are already common. */
235 tree oldargs = p1, newargs, n;
238 char *first_obj = (char *) oballoc (0);
240 len = list_length (p1);
241 newargs = tree_last (p1);
243 if (newargs == void_list_node)
252 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
257 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
259 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
261 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
264 else if (! TREE_PURPOSE (p1))
266 if (TREE_PURPOSE (p2))
268 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
274 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
276 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
278 if (TREE_VALUE (p1) != TREE_VALUE (p2))
281 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
284 TREE_VALUE (n) = TREE_VALUE (p1);
295 /* Given a type, perhaps copied for a typedef,
296 find the "original" version of it. */
301 while (TYPE_NAME (t) != NULL_TREE)
303 tree x = TYPE_NAME (t);
304 if (TREE_CODE (x) != TYPE_DECL)
306 x = DECL_ORIGINAL_TYPE (x);
314 /* T1 and T2 are arithmetic or enumeration types. Return the type
315 that will result from the "usual arithmetic converions" on T1 and
316 T2 as described in [expr]. */
319 type_after_usual_arithmetic_conversions (t1, t2)
323 enum tree_code code1 = TREE_CODE (t1);
324 enum tree_code code2 = TREE_CODE (t2);
327 /* FIXME: Attributes. */
328 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
329 || TREE_CODE (t1) == ENUMERAL_TYPE,
331 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
332 || TREE_CODE (t2) == ENUMERAL_TYPE,
335 /* In what follows, we slightly generalize the rules given in [expr]
336 so as to deal with `long long'. First, merge the attributes. */
337 attributes = merge_machine_type_attributes (t1, t2);
339 /* If only one is real, use it as the result. */
340 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
341 return build_type_attribute_variant (t1, attributes);
342 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
343 return build_type_attribute_variant (t2, attributes);
345 /* Perform the integral promotions. */
346 if (code1 != REAL_TYPE)
348 t1 = type_promotes_to (t1);
349 t2 = type_promotes_to (t2);
352 /* Both real or both integers; use the one with greater precision. */
353 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
354 return build_type_attribute_variant (t1, attributes);
355 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
356 return build_type_attribute_variant (t2, attributes);
358 if (code1 != REAL_TYPE)
360 /* If one is unsigned long long, then convert the other to unsigned
362 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
363 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
364 return build_type_attribute_variant (long_long_unsigned_type_node,
366 /* If one is a long long, and the other is an unsigned long, and
367 long long can represent all the values of an unsigned long, then
368 convert to a long long. Otherwise, convert to an unsigned long
369 long. Otherwise, if either operand is long long, convert the
372 Since we're here, we know the TYPE_PRECISION is the same;
373 therefore converting to long long cannot represent all the values
374 of an unsigned long, so we choose unsigned long long in that
376 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
377 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
379 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
380 ? long_long_unsigned_type_node
381 : long_long_integer_type_node);
382 return build_type_attribute_variant (t, attributes);
385 /* Go through the same procedure, but for longs. */
386 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
387 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
388 return build_type_attribute_variant (long_unsigned_type_node,
390 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
391 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
393 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
394 ? long_unsigned_type_node : long_integer_type_node);
395 return build_type_attribute_variant (t, attributes);
397 /* Otherwise prefer the unsigned one. */
398 if (TREE_UNSIGNED (t1))
399 return build_type_attribute_variant (t1, attributes);
401 return build_type_attribute_variant (t2, attributes);
405 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
406 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
407 return build_type_attribute_variant (long_double_type_node,
409 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
410 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
411 return build_type_attribute_variant (double_type_node,
414 return build_type_attribute_variant (float_type_node,
419 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
420 ARG1 and ARG2 are the values with those types. The LOCATION is a
421 string describing the current location, in case an error occurs. */
424 composite_pointer_type (t1, t2, arg1, arg2, location)
429 const char* location;
435 If one operand is a null pointer constant, the composite pointer
436 type is the type of the other operand. */
437 if (null_ptr_cst_p (arg1))
439 if (null_ptr_cst_p (arg2))
442 /* Deal with pointer-to-member functions in the same way as we deal
443 with pointers to functions. */
444 if (TYPE_PTRMEMFUNC_P (t1))
445 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
446 if (TYPE_PTRMEMFUNC_P (t2))
447 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
449 if (comp_target_types (t1, t2, 1))
450 result_type = common_type (t1, t2);
451 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t1)) == void_type_node)
453 if (pedantic && TREE_CODE (t2) == FUNCTION_TYPE)
454 pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
456 result_type = qualify_type (t1, t2);
458 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t2)) == void_type_node)
460 if (pedantic && TREE_CODE (t1) == FUNCTION_TYPE)
461 pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
463 result_type = qualify_type (t2, t1);
466 else if (same_or_base_type_p (t2, t1))
468 else if (IS_AGGR_TYPE (TREE_TYPE (t1))
469 && IS_AGGR_TYPE (TREE_TYPE (t2))
470 && (result_type = common_base_type (TREE_TYPE (t1),
473 if (result_type == error_mark_node)
475 cp_error ("common base type of types `%T' and `%T' is ambiguous",
476 TREE_TYPE (t1), TREE_TYPE (t2));
477 result_type = ptr_type_node;
482 && result_type != TREE_TYPE (t1)
483 && result_type != TREE_TYPE (t2))
484 cp_pedwarn ("`%T' and `%T' converted to `%T *' in %s",
485 t1, t2, result_type, location);
487 result_type = build_pointer_type (result_type);
492 cp_pedwarn ("pointer type mismatch in %s", location);
493 result_type = ptr_type_node;
499 /* Return the common type of two types.
500 We assume that comptypes has already been done and returned 1;
501 if that isn't so, this may crash.
503 This is the type for the result of most arithmetic operations
504 if the operands have the given two types.
506 We do not deal with enumeral types here because they have already been
507 converted to integer types. */
513 register enum tree_code code1;
514 register enum tree_code code2;
517 /* Save time if the two types are the same. */
520 t1 = original_type (t1);
521 t2 = original_type (t2);
525 /* If one type is nonsense, use the other. */
526 if (t1 == error_mark_node)
528 if (t2 == error_mark_node)
531 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
532 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
533 return type_after_usual_arithmetic_conversions (t1, t2);
535 /* Merge the attributes. */
536 attributes = merge_machine_type_attributes (t1, t2);
538 /* Treat an enum type as the unsigned integer type of the same width. */
540 if (TREE_CODE (t1) == ENUMERAL_TYPE)
541 t1 = type_for_size (TYPE_PRECISION (t1), 1);
542 if (TREE_CODE (t2) == ENUMERAL_TYPE)
543 t2 = type_for_size (TYPE_PRECISION (t2), 1);
545 if (TYPE_PTRMEMFUNC_P (t1))
546 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
547 if (TYPE_PTRMEMFUNC_P (t2))
548 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
550 code1 = TREE_CODE (t1);
551 code2 = TREE_CODE (t2);
553 /* If one type is complex, form the common type of the non-complex
554 components, then make that complex. Use T1 or T2 if it is the
556 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
558 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
559 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
560 tree subtype = common_type (subtype1, subtype2);
562 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
563 return build_type_attribute_variant (t1, attributes);
564 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
565 return build_type_attribute_variant (t2, attributes);
567 return build_type_attribute_variant (build_complex_type (subtype),
575 /* We should have called type_after_usual_arithmetic_conversions
577 my_friendly_abort (19990725);
582 /* For two pointers, do this recursively on the target type,
583 and combine the qualifiers of the two types' targets. */
584 /* This code was turned off; I don't know why.
585 But ANSI C++ specifies doing this with the qualifiers.
586 So I turned it on again. */
588 tree tt1 = TREE_TYPE (t1);
589 tree tt2 = TREE_TYPE (t2);
594 if (TREE_CODE (tt1) == OFFSET_TYPE)
596 b1 = TYPE_OFFSET_BASETYPE (tt1);
597 b2 = TYPE_OFFSET_BASETYPE (tt2);
598 tt1 = TREE_TYPE (tt1);
599 tt2 = TREE_TYPE (tt2);
604 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
605 tt1 = TYPE_MAIN_VARIANT (tt1);
606 tt2 = TYPE_MAIN_VARIANT (tt2);
610 else if (tt1 == void_type_node || tt2 == void_type_node)
611 target = void_type_node;
612 else if (tt1 == unknown_type_node)
614 else if (tt2 == unknown_type_node)
617 target = common_type (tt1, tt2);
619 target = cp_build_qualified_type (target, type_quals);
623 if (same_type_p (b1, b2)
624 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
625 target = build_offset_type (b2, target);
626 else if (binfo_or_else (b2, b1))
627 target = build_offset_type (b1, target);
630 if (code1 == POINTER_TYPE)
631 t1 = build_pointer_type (target);
633 t1 = build_reference_type (target);
634 t1 = build_type_attribute_variant (t1, attributes);
636 if (TREE_CODE (target) == METHOD_TYPE)
637 t1 = build_ptrmemfunc_type (t1);
644 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
645 /* Save space: see if the result is identical to one of the args. */
646 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
647 return build_type_attribute_variant (t1, attributes);
648 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
649 return build_type_attribute_variant (t2, attributes);
650 /* Merge the element types, and have a size if either arg has one. */
651 t1 = build_cplus_array_type
652 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
653 return build_type_attribute_variant (t1, attributes);
657 /* Function types: prefer the one that specified arg types.
658 If both do, merge the arg types. Also merge the return types. */
660 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
661 tree p1 = TYPE_ARG_TYPES (t1);
662 tree p2 = TYPE_ARG_TYPES (t2);
665 /* Save space: see if the result is identical to one of the args. */
666 if (valtype == TREE_TYPE (t1) && ! p2)
667 return build_type_attribute_variant (t1, attributes);
668 if (valtype == TREE_TYPE (t2) && ! p1)
669 return build_type_attribute_variant (t2, attributes);
671 /* Simple way if one arg fails to specify argument types. */
672 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
674 rval = build_function_type (valtype, p2);
675 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
676 rval = build_exception_variant (rval, raises);
677 return build_type_attribute_variant (rval, attributes);
679 raises = TYPE_RAISES_EXCEPTIONS (t1);
680 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
682 rval = build_function_type (valtype, p1);
684 rval = build_exception_variant (rval, raises);
685 return build_type_attribute_variant (rval, attributes);
688 rval = build_function_type (valtype, commonparms (p1, p2));
689 rval = build_exception_variant (rval, raises);
690 return build_type_attribute_variant (rval, attributes);
695 t1 = TYPE_MAIN_VARIANT (t1);
696 t2 = TYPE_MAIN_VARIANT (t2);
698 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
699 return build_type_attribute_variant (t1, attributes);
700 else if (binfo_or_else (t2, t1))
701 return build_type_attribute_variant (t2, attributes);
704 compiler_error ("common_type called with uncommon aggregate types");
705 return error_mark_node;
709 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
711 /* Get this value the long way, since TYPE_METHOD_BASETYPE
712 is just the main variant of this. */
716 tree b1 = TYPE_OFFSET_BASETYPE (t1);
717 tree b2 = TYPE_OFFSET_BASETYPE (t2);
719 if (same_type_p (b1, b2)
720 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
721 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
724 if (binfo_or_else (b2, b1) == NULL_TREE)
725 compiler_error ("common_type called with uncommon method types");
726 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
729 raises = TYPE_RAISES_EXCEPTIONS (t1);
731 /* If this was a member function type, get back to the
732 original type of type member function (i.e., without
733 the class instance variable up front. */
734 t1 = build_function_type (TREE_TYPE (t1),
735 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
736 t2 = build_function_type (TREE_TYPE (t2),
737 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
738 t3 = common_type (t1, t2);
739 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
740 TYPE_ARG_TYPES (t3));
741 t1 = build_exception_variant (t3, raises);
744 compiler_error ("common_type called with uncommon method types");
746 return build_type_attribute_variant (t1, attributes);
749 /* Pointers to members should now be handled by the POINTER_TYPE
751 my_friendly_abort (990325);
754 return build_type_attribute_variant (t1, attributes);
758 /* Compare two exception specifier types for exactness or subsetness, if
759 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
761 [except.spec] "If a class X ... objects of class X or any class publicly
762 and unambigously derrived from X. Similarly, if a pointer type Y * ...
763 exceptions of type Y * or that are pointers to any type publicly and
764 unambigously derrived from Y. Otherwise a function only allows exceptions
765 that have the same type ..."
766 This does not mention cv qualifiers and is different to what throw
767 [except.throw] and catch [except.catch] will do. They will ignore the
768 top level cv qualifiers, and allow qualifiers in the pointer to class
771 We implement the letter of the standard. */
774 comp_except_types (a, b, exact)
778 if (same_type_p (a, b))
782 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
785 if (TREE_CODE (a) == POINTER_TYPE
786 && TREE_CODE (b) == POINTER_TYPE)
790 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
794 if (TREE_CODE (a) != RECORD_TYPE
795 || TREE_CODE (b) != RECORD_TYPE)
798 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
804 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
805 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
806 otherwise it must be exact. Exception lists are unordered, but
807 we've already filtered out duplicates. Most lists will be in order,
808 we should try to make use of that. */
811 comp_except_specs (t1, t2, exact)
822 if (t1 == NULL_TREE) /* T1 is ... */
823 return t2 == NULL_TREE || !exact;
824 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
825 return t2 != NULL_TREE && !TREE_VALUE (t2);
826 if (t2 == NULL_TREE) /* T2 is ... */
828 if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
831 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
832 Count how many we find, to determine exactness. For exact matching and
833 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
835 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
837 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
839 tree a = TREE_VALUE (probe);
840 tree b = TREE_VALUE (t2);
842 if (comp_except_types (a, b, exact))
844 if (probe == base && exact)
845 base = TREE_CHAIN (probe);
850 if (probe == NULL_TREE)
853 return !exact || base == NULL_TREE || length == list_length (t1);
856 /* Compare the array types T1 and T2, using CMP as the type comparison
857 function for the element types. STRICT is as for comptypes. */
860 comp_array_types (cmp, t1, t2, strict)
861 register int (*cmp) PROTO((tree, tree, int));
871 /* The type of the array elements must be the same. */
872 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
873 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
874 strict & ~COMPARE_REDECLARATION)))
877 d1 = TYPE_DOMAIN (t1);
878 d2 = TYPE_DOMAIN (t2);
883 /* If one of the arrays is dimensionless, and the other has a
884 dimension, they are of different types. However, it is legal to
892 declarations for an array object can specify
893 array types that differ by the presence or absence of a major
894 array bound (_dcl.array_). */
896 return strict & COMPARE_REDECLARATION;
898 /* Check that the dimensions are the same. */
899 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
901 && cp_tree_equal (TYPE_MAX_VALUE (d1),
902 TYPE_MAX_VALUE (d2)));
905 /* Return 1 if T1 and T2 are compatible types for assignment or
906 various other operations. STRICT is a bitwise-or of the COMPARE_*
910 comptypes (t1, t2, strict)
916 int orig_strict = strict;
918 /* The special exemption for redeclaring array types without an
919 array bound only applies at the top level:
924 is not legal, for example. */
925 strict &= ~COMPARE_REDECLARATION;
927 /* Suppress errors caused by previously reported errors */
931 /* This should never happen. */
932 my_friendly_assert (t1 != error_mark_node, 307);
934 if (t2 == error_mark_node)
937 if (strict & COMPARE_RELAXED)
939 /* Treat an enum type as the unsigned integer type of the same width. */
941 if (TREE_CODE (t1) == ENUMERAL_TYPE)
942 t1 = type_for_size (TYPE_PRECISION (t1), 1);
943 if (TREE_CODE (t2) == ENUMERAL_TYPE)
944 t2 = type_for_size (TYPE_PRECISION (t2), 1);
950 if (TYPE_PTRMEMFUNC_P (t1))
951 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
952 if (TYPE_PTRMEMFUNC_P (t2))
953 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
955 /* Different classes of types can't be compatible. */
956 if (TREE_CODE (t1) != TREE_CODE (t2))
959 /* Qualifiers must match. */
960 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
962 if (strict == COMPARE_STRICT
963 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
966 /* Allow for two different type nodes which have essentially the same
967 definition. Note that we already checked for equality of the type
968 qualifiers (just above). */
970 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
973 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
974 attribute is its own main variant (`val' will remain 0). */
975 #ifndef COMP_TYPE_ATTRIBUTES
976 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
979 if (strict & COMPARE_NO_ATTRIBUTES)
981 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
982 else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
985 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
988 switch (TREE_CODE (t1))
990 case TEMPLATE_TEMPLATE_PARM:
991 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
992 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
994 if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1)),
995 DECL_TEMPLATE_PARMS (TYPE_NAME (t2))))
997 if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1)
998 && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2))
1000 /* Don't check inheritance. */
1001 strict = COMPARE_STRICT;
1006 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1007 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1008 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
1009 val = comp_template_args (TYPE_TI_ARGS (t1),
1012 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1014 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1019 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1020 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1021 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1025 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1026 TYPE_RAISES_EXCEPTIONS (t2), 1))
1029 /* This case is anti-symmetrical!
1030 One can pass a base member (or member function)
1031 to something expecting a derived member (or member function),
1032 but not vice-versa! */
1034 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1035 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1039 case REFERENCE_TYPE:
1040 t1 = TREE_TYPE (t1);
1041 t2 = TREE_TYPE (t2);
1042 /* first, check whether the referred types match with the
1043 required level of strictness */
1044 val = comptypes (t1, t2, strict);
1047 if (TREE_CODE (t1) == RECORD_TYPE
1048 && TREE_CODE (t2) == RECORD_TYPE)
1053 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1054 TYPE_RAISES_EXCEPTIONS (t2), 1))
1057 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1058 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1059 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1063 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1064 here since this is the one place where
1065 COMPARE_REDECLARATION should be used. */
1066 val = comp_array_types (comptypes, t1, t2, orig_strict);
1069 case TEMPLATE_TYPE_PARM:
1070 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1071 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1074 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
1076 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1079 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1084 return attrval == 2 && val == 1 ? 2 : val;
1087 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1088 only in the same direction as the target type. */
1091 comp_cv_target_types (ttl, ttr, nptrs)
1097 if (!at_least_as_qualified_p (ttl, ttr)
1098 && !at_least_as_qualified_p (ttr, ttl))
1099 /* The qualifications are incomparable. */
1102 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1103 return more_qualified_p (ttr, ttl) ? -1 : 1;
1105 t = comp_target_types (ttl, ttr, nptrs);
1106 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1107 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1113 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1114 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1115 converted to TTL. Return -1 means that TTL can be converted to TTR but
1118 NPTRS is the number of pointers we can strip off and keep cool.
1119 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1120 but to not permit B** to convert to A**.
1122 This should go away. Callers should use can_convert or something
1123 similar instead. (jason 17 Apr 1997) */
1126 comp_target_types (ttl, ttr, nptrs)
1130 ttl = TYPE_MAIN_VARIANT (ttl);
1131 ttr = TYPE_MAIN_VARIANT (ttr);
1132 if (same_type_p (ttl, ttr))
1135 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1138 if ((TREE_CODE (ttr) == POINTER_TYPE
1139 || TREE_CODE (ttr) == REFERENCE_TYPE)
1140 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1141 of the type pointed to. This is necessary for reference init
1142 semantics. We won't get here from a previous call with nptrs == 1;
1143 for multi-level pointers we end up in comp_ptr_ttypes. */
1146 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1148 ttl = TREE_TYPE (ttl);
1149 ttr = TREE_TYPE (ttr);
1153 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1154 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1156 else if (TREE_CODE (ttl) == VOID_TYPE
1157 && TREE_CODE (ttr) != FUNCTION_TYPE
1158 && TREE_CODE (ttr) != METHOD_TYPE
1159 && TREE_CODE (ttr) != OFFSET_TYPE)
1161 else if (TREE_CODE (ttr) == VOID_TYPE
1162 && TREE_CODE (ttl) != FUNCTION_TYPE
1163 && TREE_CODE (ttl) != METHOD_TYPE
1164 && TREE_CODE (ttl) != OFFSET_TYPE)
1166 else if (TREE_CODE (ttl) == POINTER_TYPE
1167 || TREE_CODE (ttl) == ARRAY_TYPE)
1169 if (comp_ptr_ttypes (ttl, ttr))
1171 else if (comp_ptr_ttypes (ttr, ttl))
1177 /* Const and volatile mean something different for function types,
1178 so the usual checks are not appropriate. */
1179 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1180 return comp_target_types (ttl, ttr, nptrs - 1);
1182 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1185 if (TREE_CODE (ttr) == ARRAY_TYPE)
1186 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1187 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1194 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1199 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1208 argsl = TYPE_ARG_TYPES (ttl);
1209 argsr = TYPE_ARG_TYPES (ttr);
1211 /* Compare 'this' here, not in comp_target_parms. */
1212 if (TREE_CODE (ttr) == METHOD_TYPE)
1214 tree tl = TYPE_METHOD_BASETYPE (ttl);
1215 tree tr = TYPE_METHOD_BASETYPE (ttr);
1217 if (!same_or_base_type_p (tr, tl))
1219 if (same_or_base_type_p (tl, tr))
1225 argsl = TREE_CHAIN (argsl);
1226 argsr = TREE_CHAIN (argsr);
1229 switch (comp_target_parms (argsl, argsr, 1))
1237 return saw_contra ? -1 : 1;
1240 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1244 /* Contravariance: we can assign a pointer to base member to a pointer
1245 to derived member. Note difference from simple pointer case, where
1246 we can pass a pointer to derived to a pointer to base. */
1247 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1248 TYPE_OFFSET_BASETYPE (ttl)))
1250 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1251 TYPE_OFFSET_BASETYPE (ttr)))
1261 ttl = TREE_TYPE (ttl);
1262 ttr = TREE_TYPE (ttr);
1264 if (TREE_CODE (ttl) == POINTER_TYPE
1265 || TREE_CODE (ttl) == ARRAY_TYPE)
1267 if (comp_ptr_ttypes (ttl, ttr))
1273 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1278 else if (IS_AGGR_TYPE (ttl))
1282 if (same_or_base_type_p (build_pointer_type (ttl),
1283 build_pointer_type (ttr)))
1285 if (same_or_base_type_p (build_pointer_type (ttr),
1286 build_pointer_type (ttl)))
1294 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1297 at_least_as_qualified_p (type1, type2)
1301 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1302 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1303 == CP_TYPE_QUALS (type2));
1306 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1309 more_qualified_p (type1, type2)
1313 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1314 && at_least_as_qualified_p (type1, type2));
1317 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1318 more cv-qualified that TYPE1, and 0 otherwise. */
1321 comp_cv_qualification (type1, type2)
1325 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1328 if (at_least_as_qualified_p (type1, type2))
1331 else if (at_least_as_qualified_p (type2, type1))
1337 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1338 subset of the cv-qualification signature of TYPE2, and the types
1339 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1342 comp_cv_qual_signature (type1, type2)
1346 if (comp_ptr_ttypes_real (type2, type1, -1))
1348 else if (comp_ptr_ttypes_real (type1, type2, -1))
1354 /* If two types share a common base type, return that basetype.
1355 If there is not a unique most-derived base type, this function
1356 returns ERROR_MARK_NODE. */
1359 common_base_type (tt1, tt2)
1362 tree best = NULL_TREE;
1365 /* If one is a baseclass of another, that's good enough. */
1366 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1368 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1371 /* Otherwise, try to find a unique baseclass of TT1
1372 that is shared by TT2, and follow that down. */
1373 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1375 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1376 tree trial = common_base_type (basetype, tt2);
1379 if (trial == error_mark_node)
1381 if (best == NULL_TREE)
1383 else if (best != trial)
1384 return error_mark_node;
1389 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1391 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1392 tree trial = common_base_type (tt1, basetype);
1395 if (trial == error_mark_node)
1397 if (best == NULL_TREE)
1399 else if (best != trial)
1400 return error_mark_node;
1406 /* Subroutines of `comptypes'. */
1408 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1409 equivalent in the sense that functions with those parameter types
1410 can have equivalent types. The two lists must be equivalent,
1413 C++: See comment above about TYPE1, TYPE2. */
1416 compparms (parms1, parms2)
1417 tree parms1, parms2;
1419 register tree t1 = parms1, t2 = parms2;
1421 /* An unspecified parmlist matches any specified parmlist
1422 whose argument types don't need default promotions. */
1426 if (t1 == 0 && t2 == 0)
1428 /* If one parmlist is shorter than the other,
1429 they fail to match. */
1430 if (t1 == 0 || t2 == 0)
1432 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1435 t1 = TREE_CHAIN (t1);
1436 t2 = TREE_CHAIN (t2);
1440 /* This really wants return whether or not parameter type lists
1441 would make their owning functions assignment compatible or not.
1443 The return value is like for comp_target_types.
1445 This should go away, possibly with the exception of the empty parmlist
1446 conversion; there are no conversions between function types in C++.
1447 (jason 17 Apr 1997) */
1450 comp_target_parms (parms1, parms2, strict)
1451 tree parms1, parms2;
1454 register tree t1 = parms1, t2 = parms2;
1455 int warn_contravariance = 0;
1457 /* In C, an unspecified parmlist matches any specified parmlist
1458 whose argument types don't need default promotions. This is not
1459 true for C++, but let's do it anyway for unfixed headers. */
1461 if (t1 == 0 && t2 != 0)
1463 if (! flag_strict_prototype && t2 == void_list_node)
1464 /* t1 might be the arglist of a function pointer in extern "C"
1465 declared to take (), which we fudged to (...). Don't make the
1466 user pay for our mistake. */;
1468 cp_pedwarn ("ANSI C++ prohibits conversion from `%#T' to `(...)'",
1470 return self_promoting_args_p (t2);
1473 return self_promoting_args_p (t1);
1475 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1479 /* If one parmlist is shorter than the other,
1480 they fail to match, unless STRICT is <= 0. */
1481 if (t1 == 0 || t2 == 0)
1486 return 1 + warn_contravariance;
1487 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1489 p1 = TREE_VALUE (t1);
1490 p2 = TREE_VALUE (t2);
1491 if (same_type_p (p1, p2))
1497 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1498 || (TREE_CODE (p1) == REFERENCE_TYPE
1499 && TREE_CODE (p2) == REFERENCE_TYPE))
1502 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1503 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1506 /* The following is wrong for contravariance,
1507 but many programs depend on it. */
1508 if (TREE_TYPE (p1) == void_type_node)
1510 if (TREE_TYPE (p2) == void_type_node)
1512 warn_contravariance = 1;
1515 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1516 && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (p1)),
1517 TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1520 /* Note backwards order due to contravariance. */
1521 if (comp_target_types (p2, p1, 1) <= 0)
1523 if (comp_target_types (p1, p2, 1) > 0)
1525 warn_contravariance = 1;
1532 return warn_contravariance ? -1 : 1;
1535 /* Compute the value of the `sizeof' operator. */
1541 enum tree_code code = TREE_CODE (type);
1544 if (processing_template_decl)
1545 return build_min (SIZEOF_EXPR, sizetype, type);
1547 if (code == FUNCTION_TYPE)
1549 if (pedantic || warn_pointer_arith)
1550 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1551 return size_int (1);
1553 if (code == METHOD_TYPE)
1555 if (pedantic || warn_pointer_arith)
1556 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1557 return size_int (1);
1559 if (code == VOID_TYPE)
1561 if (pedantic || warn_pointer_arith)
1562 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1563 return size_int (1);
1565 if (code == ERROR_MARK)
1566 return size_int (1);
1568 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1569 referenced object.'' */
1570 if (code == REFERENCE_TYPE)
1571 type = TREE_TYPE (type);
1573 if (code == OFFSET_TYPE)
1575 cp_error ("`sizeof' applied to non-static member");
1576 return size_int (0);
1579 if (TYPE_SIZE (complete_type (type)) == 0)
1581 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1582 return size_int (0);
1585 /* Convert in case a char is more than one unit. */
1586 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1587 size_int (TYPE_PRECISION (char_type_node)));
1588 t = convert (sizetype, t);
1589 /* size_binop does not put the constant in range, so do it now. */
1590 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1591 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1599 if (processing_template_decl)
1600 return build_min (SIZEOF_EXPR, sizetype, e);
1602 if (TREE_CODE (e) == COMPONENT_REF
1603 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1604 error ("sizeof applied to a bit-field");
1605 if (is_overloaded_fn (e))
1607 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1608 return size_int (1);
1610 else if (type_unknown_p (e))
1612 incomplete_type_error (e, TREE_TYPE (e));
1613 return size_int (1);
1615 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1616 member unless you're in a non-static member of X. So hand off to
1617 resolve_offset_ref. [expr.prim] */
1618 else if (TREE_CODE (e) == OFFSET_REF)
1619 e = resolve_offset_ref (e);
1621 if (e == error_mark_node)
1624 return c_sizeof (TREE_TYPE (e));
1628 c_sizeof_nowarn (type)
1631 enum tree_code code = TREE_CODE (type);
1634 if (code == FUNCTION_TYPE
1635 || code == METHOD_TYPE
1636 || code == VOID_TYPE
1637 || code == ERROR_MARK)
1638 return size_int (1);
1639 if (code == REFERENCE_TYPE)
1640 type = TREE_TYPE (type);
1642 if (TYPE_SIZE (type) == 0)
1643 return size_int (0);
1645 /* Convert in case a char is more than one unit. */
1646 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1647 size_int (TYPE_PRECISION (char_type_node)));
1648 t = convert (sizetype, t);
1649 force_fit_type (t, 0);
1653 /* Implement the __alignof keyword: Return the minimum required
1654 alignment of TYPE, measured in bytes. */
1660 enum tree_code code = TREE_CODE (type);
1663 if (processing_template_decl)
1664 return build_min (ALIGNOF_EXPR, sizetype, type);
1666 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1667 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1669 if (code == VOID_TYPE || code == ERROR_MARK)
1670 return size_int (1);
1672 /* C++: this is really correct! */
1673 if (code == REFERENCE_TYPE)
1674 type = TREE_TYPE (type);
1676 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1677 force_fit_type (t, 0);
1681 /* Perform the array-to-pointer and function-to-pointer conversions
1684 In addition, references are converted to rvalues and manifest
1685 constants are replaced by their values. */
1688 decay_conversion (exp)
1692 register enum tree_code code;
1694 if (TREE_CODE (exp) == OFFSET_REF)
1695 exp = resolve_offset_ref (exp);
1697 type = TREE_TYPE (exp);
1698 code = TREE_CODE (type);
1700 if (code == REFERENCE_TYPE)
1702 exp = convert_from_reference (exp);
1703 type = TREE_TYPE (exp);
1704 code = TREE_CODE (type);
1707 /* Constants can be used directly unless they're not loadable. */
1708 if (TREE_CODE (exp) == CONST_DECL)
1709 exp = DECL_INITIAL (exp);
1710 /* Replace a nonvolatile const static variable with its value. We
1711 don't do this for arrays, though; we want the address of the
1712 first element of the array, not the address of the first element
1713 of its initializing constant. We *do* replace variables that the
1714 user isn't really supposed to know about; this is a hack to deal
1715 with __PRETTY_FUNCTION__ and the like. */
1716 else if (TREE_READONLY_DECL_P (exp)
1717 && (code != ARRAY_TYPE
1718 || (TREE_CODE (exp) == VAR_DECL && DECL_IGNORED_P (exp))))
1720 exp = decl_constant_value (exp);
1721 type = TREE_TYPE (exp);
1724 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1725 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1727 if (code == VOID_TYPE)
1729 error ("void value not ignored as it ought to be");
1730 return error_mark_node;
1732 if (code == METHOD_TYPE)
1733 my_friendly_abort (990506);
1734 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1735 return build_unary_op (ADDR_EXPR, exp, 0);
1736 if (code == ARRAY_TYPE)
1741 if (TREE_CODE (exp) == INDIRECT_REF)
1743 /* Stripping away the INDIRECT_REF is not the right
1744 thing to do for references... */
1745 tree inner = TREE_OPERAND (exp, 0);
1746 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1748 inner = build1 (CONVERT_EXPR,
1749 build_pointer_type (TREE_TYPE
1750 (TREE_TYPE (inner))),
1752 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1754 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1757 if (TREE_CODE (exp) == COMPOUND_EXPR)
1759 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1760 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1761 TREE_OPERAND (exp, 0), op1);
1765 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1767 error ("invalid use of non-lvalue array");
1768 return error_mark_node;
1771 ptrtype = build_pointer_type (TREE_TYPE (type));
1773 if (TREE_CODE (exp) == VAR_DECL)
1775 /* ??? This is not really quite correct
1776 in that the type of the operand of ADDR_EXPR
1777 is not the target type of the type of the ADDR_EXPR itself.
1778 Question is, can this lossage be avoided? */
1779 adr = build1 (ADDR_EXPR, ptrtype, exp);
1780 if (mark_addressable (exp) == 0)
1781 return error_mark_node;
1782 TREE_CONSTANT (adr) = staticp (exp);
1783 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1786 /* This way is better for a COMPONENT_REF since it can
1787 simplify the offset for a component. */
1788 adr = build_unary_op (ADDR_EXPR, exp, 1);
1789 return cp_convert (ptrtype, adr);
1792 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1793 rvalues always have cv-unqualified types. */
1794 if (! CLASS_TYPE_P (type))
1795 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1801 default_conversion (exp)
1805 enum tree_code code;
1807 exp = decay_conversion (exp);
1809 type = TREE_TYPE (exp);
1810 code = TREE_CODE (type);
1812 if (INTEGRAL_CODE_P (code))
1814 tree t = type_promotes_to (type);
1816 return cp_convert (t, exp);
1822 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1826 inline_conversion (exp)
1829 if (TREE_CODE (exp) == FUNCTION_DECL)
1830 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1835 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1836 decay_conversion to one. */
1839 string_conv_p (totype, exp, warn)
1845 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1848 t = TREE_TYPE (totype);
1849 if (!same_type_p (t, char_type_node)
1850 && !same_type_p (t, wchar_type_node))
1853 if (TREE_CODE (exp) == STRING_CST)
1855 /* Make sure that we don't try to convert between char and wchar_t. */
1856 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1861 /* Is this a string constant which has decayed to 'const char *'? */
1862 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1863 if (!same_type_p (TREE_TYPE (exp), t))
1866 if (TREE_CODE (exp) != ADDR_EXPR
1867 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1871 /* This warning is not very useful, as it complains about printf. */
1872 if (warn && warn_write_strings)
1873 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1879 build_object_ref (datum, basetype, field)
1880 tree datum, basetype, field;
1883 if (datum == error_mark_node)
1884 return error_mark_node;
1886 dtype = TREE_TYPE (datum);
1887 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1888 dtype = TREE_TYPE (dtype);
1889 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1891 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1892 basetype, field, dtype);
1893 return error_mark_node;
1895 else if (is_aggr_type (basetype, 1))
1897 tree binfo = binfo_or_else (basetype, dtype);
1899 return build_x_component_ref (build_scoped_ref (datum, basetype),
1902 return error_mark_node;
1905 /* Like `build_component_ref, but uses an already found field, and converts
1906 from a reference. Must compute access for current_class_ref.
1910 build_component_ref_1 (datum, field, protect)
1914 return convert_from_reference
1915 (build_component_ref (datum, field, NULL_TREE, protect));
1918 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1919 can, for example, use as an lvalue. This code used to be in
1920 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1921 expressions, where we're dealing with aggregates. But now it's again only
1922 called from unary_complex_lvalue. The case (in particular) that led to
1923 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1927 rationalize_conditional_expr (code, t)
1928 enum tree_code code;
1931 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1932 the first operand is always the one to be used if both operands
1933 are equal, so we know what conditional expression this used to be. */
1934 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1937 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1938 ? LE_EXPR : GE_EXPR),
1939 TREE_OPERAND (t, 0),
1940 TREE_OPERAND (t, 1)),
1941 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1942 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1946 build_conditional_expr (TREE_OPERAND (t, 0),
1947 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1948 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1951 /* Given the TYPE of an anonymous union field inside T, return the
1952 FIELD_DECL for the field. If not found return NULL_TREE. Because
1953 anonymous unions can nest, we must also search all anonymous unions
1954 that are directly reachable. */
1957 lookup_anon_field (t, type)
1962 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1964 if (TREE_STATIC (field))
1966 if (TREE_CODE (field) != FIELD_DECL)
1969 /* If we find it directly, return the field. */
1970 if (DECL_NAME (field) == NULL_TREE
1971 && type == TREE_TYPE (field))
1976 /* Otherwise, it could be nested, search harder. */
1977 if (DECL_NAME (field) == NULL_TREE
1978 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1980 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1988 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1989 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1990 that we are interested in, or it can be a FIELD_DECL. */
1993 build_component_ref (datum, component, basetype_path, protect)
1994 tree datum, component, basetype_path;
1997 register tree basetype;
1998 register enum tree_code code;
1999 register tree field = NULL;
2004 if (processing_template_decl)
2005 return build_min_nt (COMPONENT_REF, datum, component);
2007 if (datum == error_mark_node
2008 || TREE_TYPE (datum) == error_mark_node)
2009 return error_mark_node;
2011 /* BASETYPE holds the type of the class containing the COMPONENT. */
2012 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2014 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2016 switch (TREE_CODE (datum))
2020 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2021 basetype_path, protect);
2022 return build (COMPOUND_EXPR, TREE_TYPE (value),
2023 TREE_OPERAND (datum, 0), value);
2026 return build_conditional_expr
2027 (TREE_OPERAND (datum, 0),
2028 build_component_ref (TREE_OPERAND (datum, 1), component,
2029 basetype_path, protect),
2030 build_component_ref (TREE_OPERAND (datum, 2), component,
2031 basetype_path, protect));
2034 cp_error ("invalid use of %D", datum);
2035 datum = error_mark_node;
2042 code = TREE_CODE (basetype);
2044 if (code == REFERENCE_TYPE)
2046 datum = convert_from_reference (datum);
2047 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2048 code = TREE_CODE (basetype);
2050 if (TREE_CODE (datum) == OFFSET_REF)
2052 datum = resolve_offset_ref (datum);
2053 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2054 code = TREE_CODE (basetype);
2057 /* First, see if there is a field or component with name COMPONENT. */
2058 if (TREE_CODE (component) == TREE_LIST)
2060 /* I could not trigger this code. MvL */
2061 my_friendly_abort (980326);
2063 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2064 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2066 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2069 if (! IS_AGGR_TYPE_CODE (code))
2071 if (code != ERROR_MARK)
2072 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2073 component, datum, basetype);
2074 return error_mark_node;
2077 if (!complete_type_or_else (basetype, datum))
2078 return error_mark_node;
2080 if (TREE_CODE (component) == BIT_NOT_EXPR)
2082 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2084 cp_error ("destructor specifier `%T::~%T' must have matching names",
2085 basetype, TREE_OPERAND (component, 0));
2086 return error_mark_node;
2088 if (! TYPE_HAS_DESTRUCTOR (basetype))
2090 cp_error ("type `%T' has no destructor", basetype);
2091 return error_mark_node;
2093 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2096 /* Look up component name in the structure type definition. */
2097 if (TYPE_VFIELD (basetype)
2098 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2099 /* Special-case this because if we use normal lookups in an ambiguous
2100 hierarchy, the compiler will abort (because vptr lookups are
2101 not supposed to be ambiguous. */
2102 field = TYPE_VFIELD (basetype);
2103 else if (TREE_CODE (component) == FIELD_DECL)
2105 else if (TREE_CODE (component) == TYPE_DECL)
2107 cp_error ("invalid use of type decl `%#D' as expression", component);
2108 return error_mark_node;
2110 else if (TREE_CODE (component) == TEMPLATE_DECL)
2112 cp_error ("invalid use of template `%#D' as expression", component);
2113 return error_mark_node;
2117 tree name = component;
2118 if (TREE_CODE (component) == VAR_DECL)
2119 name = DECL_NAME (component);
2120 if (TREE_CODE (component) == NAMESPACE_DECL)
2121 /* Source is in error, but produce a sensible diagnostic. */
2122 name = DECL_NAME (component);
2123 if (basetype_path == NULL_TREE)
2124 basetype_path = TYPE_BINFO (basetype);
2125 field = lookup_field (basetype_path, name,
2126 protect && !VFIELD_NAME_P (name), 0);
2127 if (field == error_mark_node)
2128 return error_mark_node;
2130 if (field == NULL_TREE)
2132 /* Not found as a data field, look for it as a method. If found,
2133 then if this is the only possible one, return it, else
2134 report ambiguity error. */
2135 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2136 if (fndecls == error_mark_node)
2137 return error_mark_node;
2140 /* If the function is unique and static, we can resolve it
2141 now. Otherwise, we have to wait and see what context it is
2142 used in; a component_ref involving a non-static member
2143 function can only be used in a call (expr.ref). */
2145 if (TREE_CHAIN (fndecls) == NULL_TREE
2146 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2148 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2150 tree fndecl = TREE_VALUE (fndecls);
2151 enforce_access (TREE_PURPOSE (fndecls), fndecl);
2157 /* A unique non-static member function. Other parts
2158 of the compiler expect something with
2159 unknown_type_node to be really overloaded, so
2161 TREE_VALUE (fndecls)
2162 = scratch_ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2166 ref = build (COMPONENT_REF, unknown_type_node,
2167 datum, TREE_VALUE (fndecls));
2171 cp_error ("`%#T' has no member named `%D'", basetype, name);
2172 return error_mark_node;
2174 else if (TREE_TYPE (field) == error_mark_node)
2175 return error_mark_node;
2177 if (TREE_CODE (field) != FIELD_DECL)
2179 if (TREE_CODE (field) == TYPE_DECL)
2180 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2181 else if (DECL_RTL (field) != 0)
2184 TREE_USED (field) = 1;
2189 /* See if we have to do any conversions so that we pick up the field from the
2191 if (DECL_FIELD_CONTEXT (field) != basetype)
2193 tree context = DECL_FIELD_CONTEXT (field);
2194 tree base = context;
2195 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2196 && ANON_AGGR_TYPE_P (base))
2198 base = TYPE_CONTEXT (base);
2201 /* Handle base classes here... */
2202 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2204 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2205 if (integer_zerop (addr))
2207 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2208 return error_mark_node;
2210 if (VBASE_NAME_P (DECL_NAME (field)))
2212 /* It doesn't matter which vbase pointer we grab, just
2213 find one of them. */
2214 tree binfo = get_binfo (base,
2215 TREE_TYPE (TREE_TYPE (addr)), 0);
2216 addr = convert_pointer_to_real (binfo, addr);
2219 addr = convert_pointer_to (base, addr);
2220 datum = build_indirect_ref (addr, NULL_PTR);
2221 my_friendly_assert (datum != error_mark_node, 311);
2225 /* Handle things from anon unions here... */
2226 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2228 tree subfield = lookup_anon_field (basetype, context);
2229 tree subdatum = build_component_ref (datum, subfield,
2230 basetype_path, protect);
2231 return build_component_ref (subdatum, field, basetype_path, protect);
2235 /* Compute the type of the field, as described in [expr.ref]. */
2236 type_quals = TYPE_UNQUALIFIED;
2237 field_type = TREE_TYPE (field);
2238 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2239 /* The standard says that the type of the result should be the
2240 type referred to by the reference. But for now, at least, we
2241 do the conversion from reference type later. */
2245 type_quals = (CP_TYPE_QUALS (field_type)
2246 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2248 /* A field is const (volatile) if the enclosing object, or the
2249 field itself, is const (volatile). But, a mutable field is
2250 not const, even within a const object. */
2251 if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2252 type_quals &= ~TYPE_QUAL_CONST;
2253 field_type = cp_build_qualified_type (field_type, type_quals);
2256 ref = fold (build (COMPONENT_REF, field_type,
2257 break_out_cleanups (datum), field));
2259 /* Mark the expression const or volatile, as appropriate. Even
2260 though we've dealt with the type above, we still have to mark the
2261 expression itself. */
2262 if (type_quals & TYPE_QUAL_CONST)
2263 TREE_READONLY (ref) = 1;
2264 else if (type_quals & TYPE_QUAL_VOLATILE)
2265 TREE_THIS_VOLATILE (ref) = 1;
2270 /* Variant of build_component_ref for use in expressions, which should
2271 never have REFERENCE_TYPE. */
2274 build_x_component_ref (datum, component, basetype_path, protect)
2275 tree datum, component, basetype_path;
2278 tree t = build_component_ref (datum, component, basetype_path, protect);
2280 if (! processing_template_decl)
2281 t = convert_from_reference (t);
2286 /* Given an expression PTR for a pointer, return an expression
2287 for the value pointed to.
2288 ERRORSTRING is the name of the operator to appear in error messages.
2290 This function may need to overload OPERATOR_FNNAME.
2291 Must also handle REFERENCE_TYPEs for C++. */
2294 build_x_indirect_ref (ptr, errorstring)
2296 const char *errorstring;
2300 if (processing_template_decl)
2301 return build_min_nt (INDIRECT_REF, ptr);
2303 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2307 return build_indirect_ref (ptr, errorstring);
2311 build_indirect_ref (ptr, errorstring)
2313 const char *errorstring;
2315 register tree pointer, type;
2317 if (ptr == error_mark_node)
2318 return error_mark_node;
2320 if (ptr == current_class_ptr)
2321 return current_class_ref;
2323 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2324 ? ptr : default_conversion (ptr));
2325 type = TREE_TYPE (pointer);
2327 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2331 If the type of the expression is "pointer to T," the type
2332 of the result is "T."
2334 We must use the canonical variant because certain parts of
2335 the back end, like fold, do pointer comparisons between
2337 tree t = canonical_type_variant (TREE_TYPE (type));
2339 if (same_type_p (TYPE_MAIN_VARIANT (t), void_type_node))
2341 /* A pointer to incomplete type (other than cv void) can be
2342 dereferenced [expr.unary.op]/1 */
2343 cp_error ("`%T' is not a pointer-to-object type", type);
2344 return error_mark_node;
2346 else if (TREE_CODE (pointer) == ADDR_EXPR
2348 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2349 /* The POINTER was something like `&x'. We simplify `*&x' to
2351 return TREE_OPERAND (pointer, 0);
2354 tree ref = build1 (INDIRECT_REF, t, pointer);
2356 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2357 so that we get the proper error message if the result is used
2358 to assign to. Also, &* is supposed to be a no-op. */
2359 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2360 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2361 TREE_SIDE_EFFECTS (ref)
2362 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2367 /* `pointer' won't be an error_mark_node if we were given a
2368 pointer to member, so it's cool to check for this here. */
2369 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2370 error ("invalid use of `%s' on pointer to member", errorstring);
2371 else if (pointer != error_mark_node)
2374 error ("invalid type argument of `%s'", errorstring);
2376 error ("invalid type argument");
2378 return error_mark_node;
2381 /* This handles expressions of the form "a[i]", which denotes
2384 This is logically equivalent in C to *(a+i), but we may do it differently.
2385 If A is a variable or a member, we generate a primitive ARRAY_REF.
2386 This avoids forcing the array out of registers, and can work on
2387 arrays that are not lvalues (for example, members of structures returned
2390 If INDEX is of some user-defined type, it must be converted to
2391 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2392 will inherit the type of the array, which will be some pointer type. */
2395 build_array_ref (array, idx)
2400 error ("subscript missing in array reference");
2401 return error_mark_node;
2404 if (TREE_TYPE (array) == error_mark_node
2405 || TREE_TYPE (idx) == error_mark_node)
2406 return error_mark_node;
2408 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2409 && TREE_CODE (array) != INDIRECT_REF)
2413 /* Subscripting with type char is likely to lose
2414 on a machine where chars are signed.
2415 So warn on any machine, but optionally.
2416 Don't warn for unsigned char since that type is safe.
2417 Don't warn for signed char because anyone who uses that
2418 must have done so deliberately. */
2419 if (warn_char_subscripts
2420 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2421 warning ("array subscript has type `char'");
2423 /* Apply default promotions *after* noticing character types. */
2424 idx = default_conversion (idx);
2426 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2428 error ("array subscript is not an integer");
2429 return error_mark_node;
2432 /* An array that is indexed by a non-constant
2433 cannot be stored in a register; we must be able to do
2434 address arithmetic on its address.
2435 Likewise an array of elements of variable size. */
2436 if (TREE_CODE (idx) != INTEGER_CST
2437 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2438 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2441 if (mark_addressable (array) == 0)
2442 return error_mark_node;
2444 /* An array that is indexed by a constant value which is not within
2445 the array bounds cannot be stored in a register either; because we
2446 would get a crash in store_bit_field/extract_bit_field when trying
2447 to access a non-existent part of the register. */
2448 if (TREE_CODE (idx) == INTEGER_CST
2449 && TYPE_VALUES (TREE_TYPE (array))
2450 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2452 if (mark_addressable (array) == 0)
2453 return error_mark_node;
2456 if (pedantic && !lvalue_p (array))
2457 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2459 /* Note in C++ it is valid to subscript a `register' array, since
2460 it is valid to take the address of something with that
2461 storage specification. */
2465 while (TREE_CODE (foo) == COMPONENT_REF)
2466 foo = TREE_OPERAND (foo, 0);
2467 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2468 warning ("subscripting array declared `register'");
2471 type = TREE_TYPE (TREE_TYPE (array));
2472 rval = build (ARRAY_REF, type, array, idx);
2473 /* Array ref is const/volatile if the array elements are
2474 or if the array is.. */
2475 TREE_READONLY (rval)
2476 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2477 TREE_SIDE_EFFECTS (rval)
2478 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2479 TREE_THIS_VOLATILE (rval)
2480 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2481 return require_complete_type (fold (rval));
2485 tree ar = default_conversion (array);
2486 tree ind = default_conversion (idx);
2488 /* Put the integer in IND to simplify error checking. */
2489 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2496 if (ar == error_mark_node)
2499 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2501 error ("subscripted value is neither array nor pointer");
2502 return error_mark_node;
2504 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2506 error ("array subscript is not an integer");
2507 return error_mark_node;
2510 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar,
2516 /* Build a function call to function FUNCTION with parameters PARAMS.
2517 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2518 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2519 not include any object pointer that may be required. FUNCTION's
2520 data type may be a function type or a pointer-to-function.
2522 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2523 is the list of possible methods that FUNCTION could conceivably
2524 be. If the list of methods comes from a class, then it will be
2525 a list of lists (where each element is associated with the class
2526 that produced it), otherwise it will be a simple list (for
2527 functions overloaded in global scope).
2529 In the first case, TREE_VALUE (function) is the head of one of those
2530 lists, and TREE_PURPOSE is the name of the function.
2532 In the second case, TREE_PURPOSE (function) is the function's
2535 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2537 When calling a TEMPLATE_DECL, we don't require a complete return
2541 build_x_function_call (function, params, decl)
2542 tree function, params, decl;
2545 tree template_id = NULL_TREE;
2548 if (function == error_mark_node)
2549 return error_mark_node;
2551 if (processing_template_decl)
2552 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2554 /* Save explicit template arguments if found */
2555 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2557 template_id = function;
2558 function = TREE_OPERAND (function, 0);
2561 type = TREE_TYPE (function);
2563 if (TREE_CODE (type) == OFFSET_TYPE
2564 && TREE_TYPE (type) == unknown_type_node
2565 && TREE_CODE (function) == TREE_LIST
2566 && TREE_CHAIN (function) == NULL_TREE)
2568 /* Undo (Foo:bar)()... */
2569 type = TYPE_OFFSET_BASETYPE (type);
2570 function = TREE_VALUE (function);
2571 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2572 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2573 function = TREE_VALUE (function);
2574 if (TREE_CODE (function) == OVERLOAD)
2575 function = OVL_FUNCTION (function);
2576 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2577 function = DECL_NAME (function);
2578 return build_method_call (decl, function, params,
2579 TYPE_BINFO (type), LOOKUP_NORMAL);
2582 if ((TREE_CODE (function) == FUNCTION_DECL
2583 && DECL_STATIC_FUNCTION_P (function))
2584 || (TREE_CODE (function) == TEMPLATE_DECL
2585 && DECL_STATIC_FUNCTION_P (DECL_RESULT (function))))
2586 return build_member_call(DECL_CONTEXT (function),
2588 ? template_id : DECL_NAME (function),
2591 is_method = ((TREE_CODE (function) == TREE_LIST
2592 && current_class_type != NULL_TREE
2593 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2595 || (TREE_CODE (function) == OVERLOAD
2596 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2597 || TREE_CODE (function) == IDENTIFIER_NODE
2598 || TREE_CODE (type) == METHOD_TYPE
2599 || TYPE_PTRMEMFUNC_P (type));
2601 /* A friend template. Make it look like a toplevel declaration. */
2602 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2603 function = scratch_ovl_cons (function, NULL_TREE);
2605 /* Handle methods, friends, and overloaded functions, respectively. */
2608 tree basetype = NULL_TREE;
2610 if (TREE_CODE (function) == OVERLOAD)
2611 function = OVL_CURRENT (function);
2613 if (TREE_CODE (function) == FUNCTION_DECL
2614 || DECL_FUNCTION_TEMPLATE_P (function))
2616 basetype = DECL_CLASS_CONTEXT (function);
2618 if (DECL_NAME (function))
2619 function = DECL_NAME (function);
2621 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2623 else if (TREE_CODE (function) == TREE_LIST)
2625 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2626 == FUNCTION_DECL, 312);
2627 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2628 function = TREE_PURPOSE (function);
2630 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2632 if (TREE_CODE (function) == OFFSET_REF)
2634 if (TREE_OPERAND (function, 0))
2635 decl = TREE_OPERAND (function, 0);
2637 /* Call via a pointer to member function. */
2638 if (decl == NULL_TREE)
2640 error ("pointer to member function called, but not in class scope");
2641 return error_mark_node;
2643 /* What other type of POINTER_TYPE could this be? */
2644 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2645 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2646 && TREE_CODE (function) != OFFSET_REF)
2647 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2652 /* this is an abbreviated method call.
2653 must go through here in case it is a virtual function.
2654 @@ Perhaps this could be optimized. */
2656 if (basetype && (! current_class_type
2657 || ! DERIVED_FROM_P (basetype, current_class_type)))
2658 return build_member_call (basetype, function, params);
2660 if (decl == NULL_TREE)
2662 if (current_class_type == NULL_TREE)
2664 cp_error ("object missing in call to method `%D'", function);
2665 return error_mark_node;
2667 /* Yow: call from a static member function. */
2668 decl = build_dummy_object (current_class_type);
2671 /* Put back explicit template arguments, if any. */
2673 function = template_id;
2674 return build_method_call (decl, function, params,
2675 NULL_TREE, LOOKUP_NORMAL);
2677 else if (TREE_CODE (function) == COMPONENT_REF
2678 && type == unknown_type_node)
2680 /* Undo what we did in build_component_ref. */
2681 decl = TREE_OPERAND (function, 0);
2682 function = TREE_OPERAND (function, 1);
2683 function = DECL_NAME (OVL_CURRENT (function));
2687 TREE_OPERAND (template_id, 0) = function;
2688 function = template_id;
2691 return build_method_call (decl, function, params,
2692 NULL_TREE, LOOKUP_NORMAL);
2694 else if (really_overloaded_fn (function))
2696 if (OVL_FUNCTION (function) == NULL_TREE)
2698 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2699 TREE_PURPOSE (function));
2700 return error_mark_node;
2704 /* Put back explicit template arguments, if any. */
2706 function = template_id;
2707 return build_new_function_call (function, params);
2711 /* Remove a potential OVERLOAD around it */
2712 function = OVL_CURRENT (function);
2715 if (TREE_CODE (function) == OFFSET_REF)
2717 /* If the component is a data element (or a virtual function), we play
2718 games here to make things work. */
2721 if (TREE_OPERAND (function, 0))
2722 decl = TREE_OPERAND (function, 0);
2724 decl = current_class_ref;
2726 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2728 /* Sigh. OFFSET_REFs are being used for too many things.
2729 They're being used both for -> and ->*, and we want to resolve
2730 the -> cases here, but leave the ->*. We could use
2731 resolve_offset_ref for those, too, but it would call
2732 get_member_function_from_ptrfunc and decl_addr wouldn't get
2733 updated properly. Nasty. */
2734 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2735 function = resolve_offset_ref (function);
2737 function = TREE_OPERAND (function, 1);
2739 function = get_member_function_from_ptrfunc (&decl_addr, function);
2740 params = tree_cons (NULL_TREE, decl_addr, params);
2741 return build_function_call (function, params);
2744 type = TREE_TYPE (function);
2745 if (type != error_mark_node)
2747 if (TREE_CODE (type) == REFERENCE_TYPE)
2748 type = TREE_TYPE (type);
2750 if (IS_AGGR_TYPE (type))
2751 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2756 tree fntype = TREE_TYPE (function);
2757 tree ctypeptr = NULL_TREE;
2759 /* Explicitly named method? */
2760 if (TREE_CODE (function) == FUNCTION_DECL)
2761 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2762 /* Expression with ptr-to-method type? It could either be a plain
2763 usage, or it might be a case where the ptr-to-method is being
2764 passed in as an argument. */
2765 else if (TYPE_PTRMEMFUNC_P (fntype))
2767 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2768 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2769 ctypeptr = build_pointer_type (rec);
2771 /* Unexpected node type? */
2773 my_friendly_abort (116);
2774 if (decl == NULL_TREE)
2776 if (current_function_decl
2777 && DECL_STATIC_FUNCTION_P (current_function_decl))
2778 error ("invalid call to member function needing `this' in static member function scope");
2780 error ("pointer to member function called, but not in class scope");
2781 return error_mark_node;
2783 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2784 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2786 decl = build_unary_op (ADDR_EXPR, decl, 0);
2787 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2790 decl = build_c_cast (ctypeptr, decl);
2791 params = tree_cons (NULL_TREE, decl, params);
2794 return build_function_call (function, params);
2797 /* Resolve a pointer to member function. INSTANCE is the object
2798 instance to use, if the member points to a virtual member. */
2801 get_member_function_from_ptrfunc (instance_ptrptr, function)
2802 tree *instance_ptrptr;
2805 if (TREE_CODE (function) == OFFSET_REF)
2807 function = TREE_OPERAND (function, 1);
2810 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2812 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2813 tree instance, basetype;
2815 tree instance_ptr = *instance_ptrptr;
2817 if (instance_ptr == error_mark_node
2818 && TREE_CODE (function) == PTRMEM_CST)
2820 /* Extracting the function address from a pmf is only
2821 allowed with -Wno-pmf-conversions. It only works for
2823 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2824 e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
2828 if (TREE_SIDE_EFFECTS (instance_ptr))
2829 instance_ptr = save_expr (instance_ptr);
2831 if (TREE_SIDE_EFFECTS (function))
2832 function = save_expr (function);
2834 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2835 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2837 delta = cp_convert (ptrdiff_type_node,
2838 build_component_ref (function, delta_identifier,
2840 e3 = PFN_FROM_PTRMEMFUNC (function);
2842 /* This used to avoid checking for virtual functions if basetype
2843 has no virtual functions, according to an earlier ANSI draft.
2844 With the final ISO C++ rules, such an optimization is
2845 incorrect: A pointer to a derived member can be static_cast
2846 to pointer-to-base-member, as long as the dynamic object
2847 later has the right member. */
2849 /* Promoting idx before saving it improves performance on RISC
2850 targets. Without promoting, the first compare used
2851 load-with-sign-extend, while the second used normal load then
2852 shift to sign-extend. An optimizer flaw, perhaps, but it's
2853 easier to make this change. */
2854 idx = save_expr (default_conversion
2855 (build_component_ref (function,
2858 e1 = build_binary_op (GE_EXPR, idx, integer_zero_node);
2860 /* Convert down to the right base, before using the instance. */
2861 instance = convert_pointer_to_real (basetype, instance_ptr);
2862 if (instance == error_mark_node && instance_ptr != error_mark_node)
2865 vtbl = convert_pointer_to (ptr_type_node, instance);
2866 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2869 build_pointer_type (build_pointer_type (vtable_entry_type)),
2870 vtbl, cp_convert (ptrdiff_type_node, delta2));
2871 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2872 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2875 if (! flag_vtable_thunks)
2877 aref = save_expr (aref);
2879 delta = build_binary_op
2881 build_conditional_expr (e1,
2882 build_component_ref (aref,
2889 if (flag_vtable_thunks)
2892 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2893 TREE_TYPE (e2) = TREE_TYPE (e3);
2894 e1 = build_conditional_expr (e1, e2, e3);
2896 /* Make sure this doesn't get evaluated first inside one of the
2897 branches of the COND_EXPR. */
2898 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2899 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2902 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2903 instance_ptr, delta);
2905 if (instance_ptr == error_mark_node
2906 && TREE_CODE (e1) != ADDR_EXPR
2907 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2908 cp_error ("object missing in `%E'", function);
2916 build_function_call_real (function, params, require_complete, flags)
2917 tree function, params;
2918 int require_complete, flags;
2920 register tree fntype, fndecl;
2921 register tree value_type;
2922 register tree coerced_params;
2923 tree name = NULL_TREE, assembler_name = NULL_TREE;
2926 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2927 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2928 if (TREE_CODE (function) == NOP_EXPR
2929 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2930 function = TREE_OPERAND (function, 0);
2932 if (TREE_CODE (function) == FUNCTION_DECL)
2934 name = DECL_NAME (function);
2935 assembler_name = DECL_ASSEMBLER_NAME (function);
2937 GNU_xref_call (current_function_decl,
2938 IDENTIFIER_POINTER (name ? name
2939 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2941 mark_used (function);
2944 /* Convert anything with function type to a pointer-to-function. */
2945 if (pedantic && DECL_MAIN_P (function))
2946 pedwarn ("ANSI C++ forbids calling `main' from within program");
2948 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2949 (because calling an inline function does not mean the function
2950 needs to be separately compiled). */
2952 if (DECL_INLINE (function))
2953 function = inline_conversion (function);
2955 function = build_addr_func (function);
2961 function = build_addr_func (function);
2964 if (function == error_mark_node)
2965 return error_mark_node;
2967 fntype = TREE_TYPE (function);
2969 if (TYPE_PTRMEMFUNC_P (fntype))
2971 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2973 return error_mark_node;
2976 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2977 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2979 if (!((TREE_CODE (fntype) == POINTER_TYPE
2980 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2982 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2984 cp_error ("`%E' cannot be used as a function", function);
2985 return error_mark_node;
2988 /* fntype now gets the type of function pointed to. */
2989 fntype = TREE_TYPE (fntype);
2991 /* Convert the parameters to the types declared in the
2992 function prototype, or apply default promotions. */
2994 if (flags & LOOKUP_COMPLAIN)
2995 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2996 params, fndecl, LOOKUP_NORMAL);
2998 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3001 if (coerced_params == error_mark_node)
3003 if (flags & LOOKUP_SPECULATIVELY)
3006 return error_mark_node;
3009 /* Check for errors in format strings. */
3011 if (warn_format && (name || assembler_name))
3012 check_function_format (name, assembler_name, coerced_params);
3014 /* Recognize certain built-in functions so we can make tree-codes
3015 other than CALL_EXPR. We do this when it enables fold-const.c
3016 to do something useful. */
3018 if (TREE_CODE (function) == ADDR_EXPR
3019 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3020 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3021 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
3026 if (coerced_params == 0)
3027 return integer_zero_node;
3028 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3035 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3037 register tree result
3038 = build_call (function, value_type, coerced_params);
3040 if (require_complete)
3042 if (TREE_CODE (value_type) == VOID_TYPE)
3044 result = require_complete_type (result);
3046 if (IS_AGGR_TYPE (value_type))
3047 result = build_cplus_new (value_type, result);
3048 return convert_from_reference (result);
3053 build_function_call (function, params)
3054 tree function, params;
3056 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3059 /* Convert the actual parameter expressions in the list VALUES
3060 to the types in the list TYPELIST.
3061 If parmdecls is exhausted, or when an element has NULL as its type,
3062 perform the default conversions.
3064 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3066 This is also where warnings about wrong number of args are generated.
3068 Return a list of expressions for the parameters as converted.
3070 Both VALUES and the returned value are chains of TREE_LIST nodes
3071 with the elements of the list in the TREE_VALUE slots of those nodes.
3073 In C++, unspecified trailing parameters can be filled in with their
3074 default arguments, if such were specified. Do so here. */
3077 convert_arguments (typelist, values, fndecl, flags)
3078 tree typelist, values, fndecl;
3081 register tree typetail, valtail;
3082 register tree result = NULL_TREE;
3083 const char *called_thing = 0;
3086 /* Argument passing is always copy-initialization. */
3087 flags |= LOOKUP_ONLYCONVERTING;
3091 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3093 if (DECL_NAME (fndecl) == NULL_TREE
3094 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3095 called_thing = "constructor";
3097 called_thing = "member function";
3100 called_thing = "function";
3103 for (valtail = values, typetail = typelist;
3105 valtail = TREE_CHAIN (valtail), i++)
3107 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3108 register tree val = TREE_VALUE (valtail);
3110 if (val == error_mark_node)
3111 return error_mark_node;
3113 if (type == void_type_node)
3117 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3119 error ("at this point in file");
3122 error ("too many arguments to function");
3123 /* In case anybody wants to know if this argument
3126 TREE_TYPE (tree_last (result)) = error_mark_node;
3130 if (TREE_CODE (val) == OFFSET_REF)
3131 val = resolve_offset_ref (val);
3133 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3134 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3135 if (TREE_CODE (val) == NOP_EXPR
3136 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3137 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3138 val = TREE_OPERAND (val, 0);
3140 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3142 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3143 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3144 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3145 val = default_conversion (val);
3148 if (val == error_mark_node)
3149 return error_mark_node;
3153 /* Formal parm type is specified by a function prototype. */
3156 if (TYPE_SIZE (complete_type (type)) == 0)
3158 error ("parameter type of called function is incomplete");
3163 parmval = convert_for_initialization
3164 (NULL_TREE, type, val, flags,
3165 "argument passing", fndecl, i);
3166 if (PROMOTE_PROTOTYPES
3167 && (TREE_CODE (type) == INTEGER_TYPE
3168 || TREE_CODE (type) == ENUMERAL_TYPE)
3169 && (TYPE_PRECISION (type)
3170 < TYPE_PRECISION (integer_type_node)))
3171 parmval = default_conversion (parmval);
3174 if (parmval == error_mark_node)
3175 return error_mark_node;
3177 result = tree_cons (NULL_TREE, parmval, result);
3181 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3182 val = convert_from_reference (val);
3184 result = tree_cons (NULL_TREE,
3185 convert_arg_to_ellipsis (val),
3190 typetail = TREE_CHAIN (typetail);
3193 if (typetail != 0 && typetail != void_list_node)
3195 /* See if there are default arguments that can be used */
3196 if (TREE_PURPOSE (typetail))
3198 for (; typetail != void_list_node; ++i)
3201 = convert_default_arg (TREE_VALUE (typetail),
3202 TREE_PURPOSE (typetail),
3205 if (parmval == error_mark_node)
3206 return error_mark_node;
3208 result = tree_cons (0, parmval, result);
3209 typetail = TREE_CHAIN (typetail);
3210 /* ends with `...'. */
3211 if (typetail == NULL_TREE)
3219 cp_error_at ("too few arguments to %s `%+#D'",
3220 called_thing, fndecl);
3221 error ("at this point in file");
3224 error ("too few arguments to function");
3225 return error_mark_list;
3229 return nreverse (result);
3232 /* Build a binary-operation expression, after performing default
3233 conversions on the operands. CODE is the kind of expression to build. */
3236 build_x_binary_op (code, arg1, arg2)
3237 enum tree_code code;
3240 if (processing_template_decl)
3241 return build_min_nt (code, arg1, arg2);
3243 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3247 build_binary_op (code, arg1, arg2)
3248 enum tree_code code;
3251 return build_binary_op_nodefault (code, arg1, arg2, code);
3254 /* Build a binary-operation expression without default conversions.
3255 CODE is the kind of expression to build.
3256 This function differs from `build' in several ways:
3257 the data type of the result is computed and recorded in it,
3258 warnings are generated if arg data types are invalid,
3259 special handling for addition and subtraction of pointers is known,
3260 and some optimization is done (operations on narrow ints
3261 are done in the narrower type when that gives the same result).
3262 Constant folding is also done before the result is returned.
3264 ERROR_CODE is the code that determines what to say in error messages.
3265 It is usually, but not always, the same as CODE.
3267 Note that the operands will never have enumeral types
3268 because either they have just had the default conversions performed
3269 or they have both just been converted to some other type in which
3270 the arithmetic is to be done.
3272 C++: must do special pointer arithmetic when implementing
3273 multiple inheritance, and deal with pointer to member functions. */
3276 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3277 enum tree_code code;
3278 tree orig_op0, orig_op1;
3279 enum tree_code error_code;
3282 register enum tree_code code0, code1;
3285 /* Expression code to give to the expression when it is built.
3286 Normally this is CODE, which is what the caller asked for,
3287 but in some special cases we change it. */
3288 register enum tree_code resultcode = code;
3290 /* Data type in which the computation is to be performed.
3291 In the simplest cases this is the common type of the arguments. */
3292 register tree result_type = NULL;
3294 /* Nonzero means operands have already been type-converted
3295 in whatever way is necessary.
3296 Zero means they need to be converted to RESULT_TYPE. */
3299 /* Nonzero means create the expression with this type, rather than
3301 tree build_type = 0;
3303 /* Nonzero means after finally constructing the expression
3304 convert it to this type. */
3305 tree final_type = 0;
3307 /* Nonzero if this is an operation like MIN or MAX which can
3308 safely be computed in short if both args are promoted shorts.
3309 Also implies COMMON.
3310 -1 indicates a bitwise operation; this makes a difference
3311 in the exact conditions for when it is safe to do the operation
3312 in a narrower mode. */
3315 /* Nonzero if this is a comparison operation;
3316 if both args are promoted shorts, compare the original shorts.
3317 Also implies COMMON. */
3318 int short_compare = 0;
3320 /* Nonzero if this is a right-shift operation, which can be computed on the
3321 original short and then promoted if the operand is a promoted short. */
3322 int short_shift = 0;
3324 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3327 /* Apply default conversions. */
3328 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3329 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3330 || code == TRUTH_XOR_EXPR)
3332 op0 = decay_conversion (orig_op0);
3333 op1 = decay_conversion (orig_op1);
3337 op0 = default_conversion (orig_op0);
3338 op1 = default_conversion (orig_op1);
3341 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3342 STRIP_TYPE_NOPS (op0);
3343 STRIP_TYPE_NOPS (op1);
3345 /* DTRT if one side is an overloaded function, but complain about it. */
3346 if (type_unknown_p (op0))
3348 tree t = instantiate_type (TREE_TYPE (op1), op0, 0);
3349 if (t != error_mark_node)
3351 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3356 if (type_unknown_p (op1))
3358 tree t = instantiate_type (TREE_TYPE (op0), op1, 0);
3359 if (t != error_mark_node)
3361 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3367 type0 = TREE_TYPE (op0);
3368 type1 = TREE_TYPE (op1);
3370 /* The expression codes of the data types of the arguments tell us
3371 whether the arguments are integers, floating, pointers, etc. */
3372 code0 = TREE_CODE (type0);
3373 code1 = TREE_CODE (type1);
3375 /* If an error was already reported for one of the arguments,
3376 avoid reporting another error. */
3378 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3379 return error_mark_node;
3384 /* Handle the pointer + int case. */
3385 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3386 return pointer_int_sum (PLUS_EXPR, op0, op1);
3387 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3388 return pointer_int_sum (PLUS_EXPR, op1, op0);
3394 /* Subtraction of two similar pointers.
3395 We must subtract them as integers, then divide by object size. */
3396 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3397 && comp_target_types (type0, type1, 1))
3398 return pointer_diff (op0, op1, common_type (type0, type1));
3399 /* Handle pointer minus int. Just like pointer plus int. */
3400 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3401 return pointer_int_sum (MINUS_EXPR, op0, op1);
3410 case TRUNC_DIV_EXPR:
3412 case FLOOR_DIV_EXPR:
3413 case ROUND_DIV_EXPR:
3414 case EXACT_DIV_EXPR:
3415 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3416 || code0 == COMPLEX_TYPE)
3417 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3418 || code1 == COMPLEX_TYPE))
3420 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3421 cp_warning ("division by zero in `%E / 0'", op0);
3422 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3423 cp_warning ("division by zero in `%E / 0.'", op0);
3425 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3426 resultcode = RDIV_EXPR;
3428 /* When dividing two signed integers, we have to promote to int.
3429 unless we divide by a constant != -1. Note that default
3430 conversion will have been performed on the operands at this
3431 point, so we have to dig out the original type to find out if
3433 shorten = ((TREE_CODE (op0) == NOP_EXPR
3434 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3435 || (TREE_CODE (op1) == INTEGER_CST
3436 && (TREE_INT_CST_LOW (op1) != -1
3437 || TREE_INT_CST_HIGH (op1) != -1)));
3443 case BIT_ANDTC_EXPR:
3446 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3448 /* If one operand is a constant, and the other is a short type
3449 that has been converted to an int,
3450 really do the work in the short type and then convert the
3451 result to int. If we are lucky, the constant will be 0 or 1
3452 in the short type, making the entire operation go away. */
3453 if (TREE_CODE (op0) == INTEGER_CST
3454 && TREE_CODE (op1) == NOP_EXPR
3455 && (TYPE_PRECISION (type1)
3456 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3457 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3459 final_type = result_type;
3460 op1 = TREE_OPERAND (op1, 0);
3461 result_type = TREE_TYPE (op1);
3463 if (TREE_CODE (op1) == INTEGER_CST
3464 && TREE_CODE (op0) == NOP_EXPR
3465 && (TYPE_PRECISION (type0)
3466 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3467 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3469 final_type = result_type;
3470 op0 = TREE_OPERAND (op0, 0);
3471 result_type = TREE_TYPE (op0);
3475 case TRUNC_MOD_EXPR:
3476 case FLOOR_MOD_EXPR:
3477 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3478 cp_warning ("division by zero in `%E %% 0'", op0);
3479 else if (code1 == REAL_TYPE && real_zerop (op1))
3480 cp_warning ("division by zero in `%E %% 0.'", op0);
3482 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3484 /* Although it would be tempting to shorten always here, that loses
3485 on some targets, since the modulo instruction is undefined if the
3486 quotient can't be represented in the computation mode. We shorten
3487 only if unsigned or if dividing by something we know != -1. */
3488 shorten = ((TREE_CODE (op0) == NOP_EXPR
3489 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3490 || (TREE_CODE (op1) == INTEGER_CST
3491 && (TREE_INT_CST_LOW (op1) != -1
3492 || TREE_INT_CST_HIGH (op1) != -1)));
3497 case TRUTH_ANDIF_EXPR:
3498 case TRUTH_ORIF_EXPR:
3499 case TRUTH_AND_EXPR:
3501 result_type = boolean_type_node;
3504 /* Shift operations: result has same type as first operand;
3505 always convert second operand to int.
3506 Also set SHORT_SHIFT if shifting rightward. */
3509 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3511 result_type = type0;
3512 if (TREE_CODE (op1) == INTEGER_CST)
3514 if (tree_int_cst_lt (op1, integer_zero_node))
3515 warning ("right shift count is negative");
3518 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3520 if (TREE_INT_CST_HIGH (op1) != 0
3521 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3522 >= TYPE_PRECISION (type0)))
3523 warning ("right shift count >= width of type");
3526 /* Convert the shift-count to an integer, regardless of
3527 size of value being shifted. */
3528 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3529 op1 = cp_convert (integer_type_node, op1);
3530 /* Avoid converting op1 to result_type later. */
3536 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3538 result_type = type0;
3539 if (TREE_CODE (op1) == INTEGER_CST)
3541 if (tree_int_cst_lt (op1, integer_zero_node))
3542 warning ("left shift count is negative");
3543 else if (TREE_INT_CST_HIGH (op1) != 0
3544 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3545 >= TYPE_PRECISION (type0)))
3546 warning ("left shift count >= width of type");
3548 /* Convert the shift-count to an integer, regardless of
3549 size of value being shifted. */
3550 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3551 op1 = cp_convert (integer_type_node, op1);
3552 /* Avoid converting op1 to result_type later. */
3559 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3561 result_type = type0;
3562 if (TREE_CODE (op1) == INTEGER_CST)
3564 if (tree_int_cst_lt (op1, integer_zero_node))
3565 warning ("%s rotate count is negative",
3566 (code == LROTATE_EXPR) ? "left" : "right");
3567 else if (TREE_INT_CST_HIGH (op1) != 0
3568 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3569 >= TYPE_PRECISION (type0)))
3570 warning ("%s rotate count >= width of type",
3571 (code == LROTATE_EXPR) ? "left" : "right");
3573 /* Convert the shift-count to an integer, regardless of
3574 size of value being shifted. */
3575 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3576 op1 = cp_convert (integer_type_node, op1);
3582 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3583 warning ("comparing floating point with == or != is unsafe");
3585 build_type = boolean_type_node;
3586 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3587 || code0 == COMPLEX_TYPE)
3588 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3589 || code1 == COMPLEX_TYPE))
3591 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3593 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3594 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3596 if (comp_target_types (type0, type1, 1))
3597 result_type = common_type (type0, type1);
3598 else if (tt0 == void_type_node)
3600 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3601 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3602 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3603 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3604 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3606 else if (tt1 == void_type_node)
3608 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3609 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3610 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3613 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3616 if (result_type == NULL_TREE)
3617 result_type = ptr_type_node;
3619 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3620 result_type = type0;
3621 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3622 result_type = type1;
3623 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3625 result_type = type0;
3626 error ("ANSI C++ forbids comparison between pointer and integer");
3628 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3630 result_type = type1;
3631 error ("ANSI C++ forbids comparison between pointer and integer");
3633 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3635 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3636 op1 = integer_zero_node;
3637 result_type = TREE_TYPE (op0);
3639 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3641 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3642 op1 = integer_zero_node;
3643 result_type = TREE_TYPE (op0);
3645 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3646 && same_type_p (type0, type1))
3648 /* The code we generate for the test is:
3650 (op0.index == op1.index
3651 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3652 || op0.pfn == op1.pfn)) */
3654 tree index0 = build_component_ref (op0, index_identifier,
3656 tree index1 = save_expr (build_component_ref (op1, index_identifier,
3658 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3659 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3660 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3661 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3663 tree integer_neg_one_node
3664 = build_binary_op (MINUS_EXPR, integer_zero_node,
3666 e1 = build_binary_op (EQ_EXPR, index0, index1);
3667 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3668 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3669 build_binary_op (EQ_EXPR, delta20, delta21));
3670 /* We can't use build_binary_op for this cmp because it would get
3671 confused by the ptr to method types and think we want pmfs. */
3672 e3 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3673 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3674 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3675 if (code == EQ_EXPR)
3677 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3679 else if (TYPE_PTRMEMFUNC_P (type0)
3680 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3682 tree index0 = build_component_ref (op0, index_identifier,
3685 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3686 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3687 tree delta21 = integer_zero_node;
3689 tree integer_neg_one_node
3690 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node);
3691 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3692 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3694 /* Map everything down one to make room for
3695 the null pointer to member. */
3696 index1 = size_binop (PLUS_EXPR,
3697 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3699 op1 = integer_zero_node;
3700 delta21 = TYPE_VFIELD (TYPE_METHOD_BASETYPE
3701 (TREE_TYPE (type1)));
3702 delta21 = DECL_FIELD_BITPOS (delta21);
3703 delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3704 size_int (BITS_PER_UNIT));
3705 delta21 = convert (sizetype, delta21);
3708 index1 = integer_neg_one_node;
3710 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3712 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3715 e1 = build_binary_op (EQ_EXPR, index0, index1);
3716 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3717 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3718 build_binary_op (EQ_EXPR, delta20, delta21));
3719 /* We can't use build_binary_op for this cmp because it would get
3720 confused by the ptr to method types and think we want pmfs. */
3721 e3 = build (EQ_EXPR, boolean_type_node, pfn0, op1);
3722 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3723 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3724 if (code == EQ_EXPR)
3726 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3728 else if (TYPE_PTRMEMFUNC_P (type1)
3729 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0))
3730 return build_binary_op (code, op1, op0);
3735 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3736 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3738 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3740 if (comp_target_types (type0, type1, 1))
3741 result_type = common_type (type0, type1);
3744 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3746 result_type = ptr_type_node;
3755 build_type = boolean_type_node;
3756 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3757 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3759 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3761 if (comp_target_types (type0, type1, 1))
3762 result_type = common_type (type0, type1);
3765 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3767 result_type = ptr_type_node;
3770 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3771 && integer_zerop (op1))
3772 result_type = type0;
3773 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3774 && integer_zerop (op0))
3775 result_type = type1;
3776 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3778 result_type = type0;
3779 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3781 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3783 result_type = type1;
3784 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3792 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3794 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3796 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3798 if (shorten || common || short_compare)
3799 result_type = common_type (type0, type1);
3801 /* For certain operations (which identify themselves by shorten != 0)
3802 if both args were extended from the same smaller type,
3803 do the arithmetic in that type and then extend.
3805 shorten !=0 and !=1 indicates a bitwise operation.
3806 For them, this optimization is safe only if
3807 both args are zero-extended or both are sign-extended.
3808 Otherwise, we might change the result.
3809 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3810 but calculated in (unsigned short) it would be (unsigned short)-1. */
3812 if (shorten && none_complex)
3814 int unsigned0, unsigned1;
3815 tree arg0 = get_narrower (op0, &unsigned0);
3816 tree arg1 = get_narrower (op1, &unsigned1);
3817 /* UNS is 1 if the operation to be done is an unsigned one. */
3818 int uns = TREE_UNSIGNED (result_type);
3821 final_type = result_type;
3823 /* Handle the case that OP0 does not *contain* a conversion
3824 but it *requires* conversion to FINAL_TYPE. */
3826 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3827 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3828 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3829 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3831 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3833 /* For bitwise operations, signedness of nominal type
3834 does not matter. Consider only how operands were extended. */
3838 /* Note that in all three cases below we refrain from optimizing
3839 an unsigned operation on sign-extended args.
3840 That would not be valid. */
3842 /* Both args variable: if both extended in same way
3843 from same width, do it in that width.
3844 Do it unsigned if args were zero-extended. */
3845 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3846 < TYPE_PRECISION (result_type))
3847 && (TYPE_PRECISION (TREE_TYPE (arg1))
3848 == TYPE_PRECISION (TREE_TYPE (arg0)))
3849 && unsigned0 == unsigned1
3850 && (unsigned0 || !uns))
3852 = signed_or_unsigned_type (unsigned0,
3853 common_type (TREE_TYPE (arg0),
3855 else if (TREE_CODE (arg0) == INTEGER_CST
3856 && (unsigned1 || !uns)
3857 && (TYPE_PRECISION (TREE_TYPE (arg1))
3858 < TYPE_PRECISION (result_type))
3859 && (type = signed_or_unsigned_type (unsigned1,
3861 int_fits_type_p (arg0, type)))
3863 else if (TREE_CODE (arg1) == INTEGER_CST
3864 && (unsigned0 || !uns)
3865 && (TYPE_PRECISION (TREE_TYPE (arg0))
3866 < TYPE_PRECISION (result_type))
3867 && (type = signed_or_unsigned_type (unsigned0,
3869 int_fits_type_p (arg1, type)))
3873 /* Shifts can be shortened if shifting right. */
3878 tree arg0 = get_narrower (op0, &unsigned_arg);
3880 final_type = result_type;
3882 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3883 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3885 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3886 /* We can shorten only if the shift count is less than the
3887 number of bits in the smaller type size. */
3888 && TREE_INT_CST_HIGH (op1) == 0
3889 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3890 /* If arg is sign-extended and then unsigned-shifted,
3891 we can simulate this with a signed shift in arg's type
3892 only if the extended result is at least twice as wide
3893 as the arg. Otherwise, the shift could use up all the
3894 ones made by sign-extension and bring in zeros.
3895 We can't optimize that case at all, but in most machines
3896 it never happens because available widths are 2**N. */
3897 && (!TREE_UNSIGNED (final_type)
3899 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3900 <= TYPE_PRECISION (result_type))))
3902 /* Do an unsigned shift if the operand was zero-extended. */
3904 = signed_or_unsigned_type (unsigned_arg,
3906 /* Convert value-to-be-shifted to that type. */
3907 if (TREE_TYPE (op0) != result_type)
3908 op0 = cp_convert (result_type, op0);
3913 /* Comparison operations are shortened too but differently.
3914 They identify themselves by setting short_compare = 1. */
3918 /* Don't write &op0, etc., because that would prevent op0
3919 from being kept in a register.
3920 Instead, make copies of the our local variables and
3921 pass the copies by reference, then copy them back afterward. */
3922 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3923 enum tree_code xresultcode = resultcode;
3925 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3927 return cp_convert (boolean_type_node, val);
3928 op0 = xop0, op1 = xop1;
3930 resultcode = xresultcode;
3933 if (short_compare && warn_sign_compare)
3935 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3936 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3938 int unsignedp0, unsignedp1;
3939 tree primop0 = get_narrower (op0, &unsignedp0);
3940 tree primop1 = get_narrower (op1, &unsignedp1);
3942 /* Check for comparison of different enum types. */
3943 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3944 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3945 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3946 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3948 cp_warning ("comparison between `%#T' and `%#T'",
3949 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3952 /* Give warnings for comparisons between signed and unsigned
3953 quantities that may fail. */
3954 /* Do the checking based on the original operand trees, so that
3955 casts will be considered, but default promotions won't be. */
3957 /* Do not warn if the comparison is being done in a signed type,
3958 since the signed type will only be chosen if it can represent
3959 all the values of the unsigned type. */
3960 if (! TREE_UNSIGNED (result_type))
3962 /* Do not warn if both operands are unsigned. */
3963 else if (op0_signed == op1_signed)
3965 /* Do not warn if the signed quantity is an unsuffixed
3966 integer literal (or some static constant expression
3967 involving such literals) and it is non-negative. */
3968 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3969 && tree_int_cst_sgn (orig_op0) >= 0)
3970 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3971 && tree_int_cst_sgn (orig_op1) >= 0))
3973 /* Do not warn if the comparison is an equality operation,
3974 the unsigned quantity is an integral constant and it does
3975 not use the most significant bit of result_type. */
3976 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3977 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3978 && int_fits_type_p (orig_op1,
3979 signed_type (result_type)))
3980 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3981 && int_fits_type_p (orig_op0,
3982 signed_type (result_type)))))
3985 warning ("comparison between signed and unsigned");
3987 /* Warn if two unsigned values are being compared in a size
3988 larger than their original size, and one (and only one) is the
3989 result of a `~' operator. This comparison will always fail.
3991 Also warn if one operand is a constant, and the constant does not
3992 have all bits set that are set in the ~ operand when it is
3995 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3996 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3998 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3999 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
4000 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
4001 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
4003 if (TREE_CODE (primop0) == INTEGER_CST
4004 || TREE_CODE (primop1) == INTEGER_CST)
4007 HOST_WIDE_INT constant, mask;
4011 if (TREE_CODE (primop0) == INTEGER_CST)
4014 unsignedp = unsignedp1;
4015 constant = TREE_INT_CST_LOW (primop0);
4020 unsignedp = unsignedp0;
4021 constant = TREE_INT_CST_LOW (primop1);
4024 bits = TYPE_PRECISION (TREE_TYPE (primop));
4025 if (bits < TYPE_PRECISION (result_type)
4026 && bits < HOST_BITS_PER_LONG && unsignedp)
4028 mask = (~ (HOST_WIDE_INT) 0) << bits;
4029 if ((mask & constant) != mask)
4030 warning ("comparison of promoted ~unsigned with constant");
4033 else if (unsignedp0 && unsignedp1
4034 && (TYPE_PRECISION (TREE_TYPE (primop0))
4035 < TYPE_PRECISION (result_type))
4036 && (TYPE_PRECISION (TREE_TYPE (primop1))
4037 < TYPE_PRECISION (result_type)))
4038 warning ("comparison of promoted ~unsigned with unsigned");
4043 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4044 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4045 Then the expression will be built.
4046 It will be given type FINAL_TYPE if that is nonzero;
4047 otherwise, it will be given type RESULT_TYPE. */
4051 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
4052 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
4053 return error_mark_node;
4056 /* Issue warnings about peculiar, but legal, uses of NULL. */
4057 if (/* It's reasonable to use pointer values as operands of &&
4058 and ||, so NULL is no exception. */
4059 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4060 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4061 (orig_op0 == null_node
4062 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4063 /* Or vice versa. */
4064 || (orig_op1 == null_node
4065 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4066 /* Or, both are NULL and the operation was not a comparison. */
4067 || (orig_op0 == null_node && orig_op1 == null_node
4068 && code != EQ_EXPR && code != NE_EXPR)))
4069 /* Some sort of arithmetic operation involving NULL was
4070 performed. Note that pointer-difference and pointer-addition
4071 have already been handled above, and so we don't end up here in
4073 cp_warning ("NULL used in arithmetic");
4077 if (TREE_TYPE (op0) != result_type)
4078 op0 = cp_convert (result_type, op0);
4079 if (TREE_TYPE (op1) != result_type)
4080 op1 = cp_convert (result_type, op1);
4082 if (op0 == error_mark_node || op1 == error_mark_node)
4083 return error_mark_node;
4086 if (build_type == NULL_TREE)
4087 build_type = result_type;
4090 register tree result = build (resultcode, build_type, op0, op1);
4091 register tree folded;
4093 folded = fold (result);
4094 if (folded == result)
4095 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4096 if (final_type != 0)
4097 return cp_convert (final_type, folded);
4102 /* Return a tree for the sum or difference (RESULTCODE says which)
4103 of pointer PTROP and integer INTOP. */
4106 pointer_int_sum (resultcode, ptrop, intop)
4107 enum tree_code resultcode;
4108 register tree ptrop, intop;
4112 register tree result;
4113 register tree folded = fold (intop);
4115 /* The result is a pointer of the same type that is being added. */
4117 register tree result_type = TREE_TYPE (ptrop);
4119 if (!complete_type_or_else (result_type, ptrop))
4120 return error_mark_node;
4122 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4124 if (pedantic || warn_pointer_arith)
4125 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
4126 size_exp = integer_one_node;
4128 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4130 if (pedantic || warn_pointer_arith)
4131 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
4132 size_exp = integer_one_node;
4134 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4136 if (pedantic || warn_pointer_arith)
4137 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4138 size_exp = integer_one_node;
4140 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4142 if (pedantic || warn_pointer_arith)
4143 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4144 size_exp = integer_one_node;
4147 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4149 /* Needed to make OOPS V2R3 work. */
4151 if (TREE_CODE (intop) == INTEGER_CST
4152 && TREE_INT_CST_LOW (intop) == 0
4153 && TREE_INT_CST_HIGH (intop) == 0)
4156 /* If what we are about to multiply by the size of the elements
4157 contains a constant term, apply distributive law
4158 and multiply that constant term separately.
4159 This helps produce common subexpressions. */
4161 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4162 && ! TREE_CONSTANT (intop)
4163 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4164 && TREE_CONSTANT (size_exp))
4166 enum tree_code subcode = resultcode;
4167 if (TREE_CODE (intop) == MINUS_EXPR)
4168 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4169 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4170 intop = TREE_OPERAND (intop, 0);
4173 /* Convert the integer argument to a type the same size as sizetype
4174 so the multiply won't overflow spuriously. */
4176 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4177 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4179 /* Replace the integer argument with a suitable product by the object size.
4180 Do this multiplication as signed, then convert to the appropriate
4181 pointer type (actually unsigned integral). */
4183 intop = cp_convert (result_type,
4184 build_binary_op (MULT_EXPR, intop,
4185 cp_convert (TREE_TYPE (intop),
4188 /* Create the sum or difference. */
4190 result = build (resultcode, result_type, ptrop, intop);
4192 folded = fold (result);
4193 if (folded == result)
4194 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4198 /* Return a tree for the difference of pointers OP0 and OP1.
4199 The resulting tree has type int. */
4202 pointer_diff (op0, op1, ptrtype)
4203 register tree op0, op1;
4204 register tree ptrtype;
4206 register tree result, folded;
4207 tree restype = ptrdiff_type_node;
4208 tree target_type = TREE_TYPE (ptrtype);
4210 if (!complete_type_or_else (target_type, NULL_TREE))
4211 return error_mark_node;
4213 if (pedantic || warn_pointer_arith)
4215 if (TREE_CODE (target_type) == VOID_TYPE)
4216 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4217 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4218 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4219 if (TREE_CODE (target_type) == METHOD_TYPE)
4220 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4221 if (TREE_CODE (target_type) == OFFSET_TYPE)
4222 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4225 /* First do the subtraction as integers;
4226 then drop through to build the divide operator. */
4228 op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4229 cp_convert (restype, op1));
4231 /* This generates an error if op1 is a pointer to an incomplete type. */
4232 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4233 error ("arithmetic on pointer to an incomplete type");
4235 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4236 || TREE_CODE (target_type) == FUNCTION_TYPE
4237 || TREE_CODE (target_type) == METHOD_TYPE
4238 || TREE_CODE (target_type) == OFFSET_TYPE)
4240 : size_in_bytes (target_type));
4242 /* Do the division. */
4244 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4246 folded = fold (result);
4247 if (folded == result)
4248 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4252 /* Handle the case of taking the address of a COMPONENT_REF.
4253 Called by `build_unary_op'.
4255 ARG is the COMPONENT_REF whose address we want.
4256 ARGTYPE is the pointer type that this address should have. */
4259 build_component_addr (arg, argtype)
4262 tree field = TREE_OPERAND (arg, 1);
4263 tree basetype = decl_type_context (field);
4264 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4266 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4268 if (DECL_C_BIT_FIELD (field))
4270 cp_error ("attempt to take address of bit-field structure member `%D'",
4272 return error_mark_node;
4275 if (TREE_CODE (field) == FIELD_DECL
4276 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4278 /* Can't convert directly to ARGTYPE, since that
4279 may have the same pointer type as one of our
4281 rval = build1 (NOP_EXPR, argtype,
4282 convert_pointer_to (basetype, rval));
4283 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4286 /* This conversion is harmless. */
4287 rval = convert_force (argtype, rval, 0);
4289 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4291 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4292 size_int (BITS_PER_UNIT));
4293 int flag = TREE_CONSTANT (rval);
4294 offset = convert (sizetype, offset);
4295 rval = fold (build (PLUS_EXPR, argtype,
4296 rval, cp_convert (argtype, offset)));
4297 TREE_CONSTANT (rval) = flag;
4302 /* Construct and perhaps optimize a tree representation
4303 for a unary operation. CODE, a tree_code, specifies the operation
4304 and XARG is the operand. */
4307 build_x_unary_op (code, xarg)
4308 enum tree_code code;
4311 if (processing_template_decl)
4312 return build_min_nt (code, xarg, NULL_TREE);
4314 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4316 if (code == ADDR_EXPR
4317 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4318 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4319 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4320 || (TREE_CODE (xarg) == OFFSET_REF)))
4321 /* don't look for a function */;
4326 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4327 NULL_TREE, NULL_TREE);
4328 if (rval || code != ADDR_EXPR)
4332 if (code == ADDR_EXPR)
4334 if (TREE_CODE (xarg) == TARGET_EXPR)
4335 warning ("taking address of temporary");
4338 return build_unary_op (code, xarg, 0);
4341 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4344 condition_conversion (expr)
4348 if (processing_template_decl)
4350 t = perform_implicit_conversion (boolean_type_node, expr);
4351 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4355 /* C++: Must handle pointers to members.
4357 Perhaps type instantiation should be extended to handle conversion
4358 from aggregates to types we don't yet know we want? (Or are those
4359 cases typically errors which should be reported?)
4361 NOCONVERT nonzero suppresses the default promotions
4362 (such as from short to int). */
4365 build_unary_op (code, xarg, noconvert)
4366 enum tree_code code;
4370 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4371 register tree arg = xarg;
4372 register tree argtype = 0;
4373 const char *errstring = NULL;
4376 if (arg == error_mark_node)
4377 return error_mark_node;
4382 /* This is used for unary plus, because a CONVERT_EXPR
4383 is enough to prevent anybody from looking inside for
4384 associativity, but won't generate any code. */
4385 if (!(arg = build_expr_type_conversion
4386 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4387 errstring = "wrong type argument to unary plus";
4391 arg = default_conversion (arg);
4392 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4393 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4398 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4399 errstring = "wrong type argument to unary minus";
4400 else if (!noconvert)
4401 arg = default_conversion (arg);
4405 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4409 arg = default_conversion (arg);
4411 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4413 errstring = "wrong type argument to bit-complement";
4414 else if (!noconvert)
4415 arg = default_conversion (arg);
4419 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4420 errstring = "wrong type argument to abs";
4421 else if (!noconvert)
4422 arg = default_conversion (arg);
4426 /* Conjugating a real value is a no-op, but allow it anyway. */
4427 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4428 errstring = "wrong type argument to conjugation";
4429 else if (!noconvert)
4430 arg = default_conversion (arg);
4433 case TRUTH_NOT_EXPR:
4434 arg = cp_convert (boolean_type_node, arg);
4435 val = invert_truthvalue (arg);
4436 if (arg != error_mark_node)
4438 errstring = "in argument to unary !";
4445 if (TREE_CODE (arg) == COMPLEX_CST)
4446 return TREE_REALPART (arg);
4447 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4448 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4453 if (TREE_CODE (arg) == COMPLEX_CST)
4454 return TREE_IMAGPART (arg);
4455 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4456 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4458 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4460 case PREINCREMENT_EXPR:
4461 case POSTINCREMENT_EXPR:
4462 case PREDECREMENT_EXPR:
4463 case POSTDECREMENT_EXPR:
4464 /* Handle complex lvalues (when permitted)
4465 by reduction to simpler cases. */
4467 val = unary_complex_lvalue (code, arg);
4471 /* Increment or decrement the real part of the value,
4472 and don't change the imaginary part. */
4473 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4477 arg = stabilize_reference (arg);
4478 real = build_unary_op (REALPART_EXPR, arg, 1);
4479 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4480 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4481 build_unary_op (code, real, 1), imag);
4484 /* Report invalid types. */
4486 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4489 if (code == PREINCREMENT_EXPR)
4490 errstring ="no pre-increment operator for type";
4491 else if (code == POSTINCREMENT_EXPR)
4492 errstring ="no post-increment operator for type";
4493 else if (code == PREDECREMENT_EXPR)
4494 errstring ="no pre-decrement operator for type";
4496 errstring ="no post-decrement operator for type";
4500 /* Report something read-only. */
4502 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4503 || TREE_READONLY (arg))
4504 readonly_error (arg, ((code == PREINCREMENT_EXPR
4505 || code == POSTINCREMENT_EXPR)
4506 ? "increment" : "decrement"),
4511 tree result_type = TREE_TYPE (arg);
4513 arg = get_unwidened (arg, 0);
4514 argtype = TREE_TYPE (arg);
4516 /* ARM $5.2.5 last annotation says this should be forbidden. */
4517 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4518 pedwarn ("ANSI C++ forbids %sing an enum",
4519 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4520 ? "increment" : "decrement");
4522 /* Compute the increment. */
4524 if (TREE_CODE (argtype) == POINTER_TYPE)
4526 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4527 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4528 cp_error ("cannot %s a pointer to incomplete type `%T'",
4529 ((code == PREINCREMENT_EXPR
4530 || code == POSTINCREMENT_EXPR)
4531 ? "increment" : "decrement"), TREE_TYPE (argtype));
4532 else if ((pedantic || warn_pointer_arith)
4533 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4534 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4535 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4536 ((code == PREINCREMENT_EXPR
4537 || code == POSTINCREMENT_EXPR)
4538 ? "increment" : "decrement"), argtype);
4539 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4542 inc = integer_one_node;
4544 inc = cp_convert (argtype, inc);
4546 /* Handle incrementing a cast-expression. */
4548 switch (TREE_CODE (arg))
4553 case FIX_TRUNC_EXPR:
4554 case FIX_FLOOR_EXPR:
4555 case FIX_ROUND_EXPR:
4558 tree incremented, modify, value, compound;
4559 if (! lvalue_p (arg) && pedantic)
4560 pedwarn ("cast to non-reference type used as lvalue");
4561 arg = stabilize_reference (arg);
4562 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4565 value = save_expr (arg);
4566 incremented = build (((code == PREINCREMENT_EXPR
4567 || code == POSTINCREMENT_EXPR)
4568 ? PLUS_EXPR : MINUS_EXPR),
4569 argtype, value, inc);
4571 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4572 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4574 /* Eliminate warning about unused result of + or -. */
4575 TREE_NO_UNUSED_WARNING (compound) = 1;
4583 /* Complain about anything else that is not a true lvalue. */
4584 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4585 || code == POSTINCREMENT_EXPR)
4586 ? "increment" : "decrement")))
4587 return error_mark_node;
4589 /* Forbid using -- on `bool'. */
4590 if (TREE_TYPE (arg) == boolean_type_node)
4592 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4594 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4595 return error_mark_node;
4598 /* This will only work if someone can convince Kenner to accept
4599 my patch to expand_increment. (jason) */
4600 val = build (code, TREE_TYPE (arg), arg, inc);
4602 if (code == POSTINCREMENT_EXPR)
4604 arg = stabilize_reference (arg);
4605 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4607 arg = save_expr (arg);
4608 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4609 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4612 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4617 val = build (code, TREE_TYPE (arg), arg, inc);
4619 TREE_SIDE_EFFECTS (val) = 1;
4620 return cp_convert (result_type, val);
4624 /* Note that this operation never does default_conversion
4625 regardless of NOCONVERT. */
4627 argtype = lvalue_type (arg);
4628 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4632 build_pointer_type (TREE_TYPE (argtype)), arg);
4633 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4636 else if (pedantic && DECL_MAIN_P (arg))
4638 pedwarn ("taking address of function `main'");
4640 /* Let &* cancel out to simplify resulting code. */
4641 if (TREE_CODE (arg) == INDIRECT_REF)
4643 /* We don't need to have `current_class_ptr' wrapped in a
4644 NON_LVALUE_EXPR node. */
4645 if (arg == current_class_ref)
4646 return current_class_ptr;
4648 arg = TREE_OPERAND (arg, 0);
4649 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4653 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4654 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4656 else if (lvalue_p (arg))
4657 /* Don't let this be an lvalue. */
4658 return non_lvalue (arg);
4662 /* For &x[y], return x+y */
4663 if (TREE_CODE (arg) == ARRAY_REF)
4665 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4666 return error_mark_node;
4667 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4668 TREE_OPERAND (arg, 1));
4671 /* Uninstantiated types are all functions. Taking the
4672 address of a function is a no-op, so just return the
4675 if (TREE_CODE (arg) == IDENTIFIER_NODE
4676 && IDENTIFIER_OPNAME_P (arg))
4678 my_friendly_abort (117);
4679 /* We don't know the type yet, so just work around the problem.
4680 We know that this will resolve to an lvalue. */
4681 return build1 (ADDR_EXPR, unknown_type_node, arg);
4684 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4685 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4687 /* They're trying to take the address of a unique non-static
4688 member function. This is ill-formed, but let's try to DTRT.
4689 Note: We only handle unique functions here because we don't
4690 want to complain if there's a static overload; non-unique
4691 cases will be handled by instantiate_type. But we need to
4692 handle this case here to allow casts on the resulting PMF. */
4694 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4695 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4697 if (! flag_ms_extensions)
4699 if (current_class_type
4700 && TREE_OPERAND (arg, 0) == current_class_ref)
4701 /* An expression like &memfn. */
4702 pedwarn ("taking the address of a non-static member function");
4704 pedwarn ("taking the address of a bound member function");
4707 (" to form a pointer to member function, say `&%T::%D'",
4711 arg = build_offset_ref (base, name);
4714 if (type_unknown_p (arg))
4715 return build1 (ADDR_EXPR, unknown_type_node, arg);
4717 /* Handle complex lvalues (when permitted)
4718 by reduction to simpler cases. */
4719 val = unary_complex_lvalue (code, arg);
4723 switch (TREE_CODE (arg))
4728 case FIX_TRUNC_EXPR:
4729 case FIX_FLOOR_EXPR:
4730 case FIX_ROUND_EXPR:
4732 if (! lvalue_p (arg) && pedantic)
4733 pedwarn ("taking the address of a cast to non-reference type");
4740 /* Allow the address of a constructor if all the elements
4742 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4743 && TREE_CONSTANT (arg))
4745 /* Anything not already handled and not a true memory reference
4747 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4748 && TREE_CODE (argtype) != METHOD_TYPE
4749 && !lvalue_or_else (arg, "unary `&'"))
4750 return error_mark_node;
4752 if (argtype != error_mark_node)
4753 argtype = build_pointer_type (argtype);
4755 if (mark_addressable (arg) == 0)
4756 return error_mark_node;
4761 if (TREE_CODE (arg) == COMPONENT_REF)
4762 addr = build_component_addr (arg, argtype);
4764 addr = build1 (ADDR_EXPR, argtype, arg);
4766 /* Address of a static or external variable or
4767 function counts as a constant */
4769 TREE_CONSTANT (addr) = 1;
4771 if (TREE_CODE (argtype) == POINTER_TYPE
4772 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4774 build_ptrmemfunc_type (argtype);
4775 addr = build_ptrmemfunc (argtype, addr, 0);
4788 argtype = TREE_TYPE (arg);
4789 return fold (build1 (code, argtype, arg));
4793 return error_mark_node;
4797 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4798 convert ARG with the same conversions in the same order
4799 and return the result. */
4802 convert_sequence (conversions, arg)
4806 switch (TREE_CODE (conversions))
4811 case FIX_TRUNC_EXPR:
4812 case FIX_FLOOR_EXPR:
4813 case FIX_ROUND_EXPR:
4815 return cp_convert (TREE_TYPE (conversions),
4816 convert_sequence (TREE_OPERAND (conversions, 0),
4825 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4826 for certain kinds of expressions which are not really lvalues
4827 but which we can accept as lvalues.
4829 If ARG is not a kind of expression we can handle, return zero. */
4832 unary_complex_lvalue (code, arg)
4833 enum tree_code code;
4836 /* Handle (a, b) used as an "lvalue". */
4837 if (TREE_CODE (arg) == COMPOUND_EXPR)
4839 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4840 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4841 TREE_OPERAND (arg, 0), real_result);
4844 /* Handle (a ? b : c) used as an "lvalue". */
4845 if (TREE_CODE (arg) == COND_EXPR
4846 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4847 return rationalize_conditional_expr (code, arg);
4849 if (TREE_CODE (arg) == MODIFY_EXPR
4850 || TREE_CODE (arg) == PREINCREMENT_EXPR
4851 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4852 return unary_complex_lvalue
4853 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4854 arg, TREE_OPERAND (arg, 0)));
4856 if (code != ADDR_EXPR)
4859 /* Handle (a = b) used as an "lvalue" for `&'. */
4860 if (TREE_CODE (arg) == MODIFY_EXPR
4861 || TREE_CODE (arg) == INIT_EXPR)
4863 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4864 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4865 TREE_NO_UNUSED_WARNING (arg) = 1;
4869 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4870 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4871 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4873 /* The representation of something of type OFFSET_TYPE
4874 is really the representation of a pointer to it.
4875 Here give the representation its true type. */
4878 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4880 if (TREE_CODE (arg) != OFFSET_REF)
4883 t = TREE_OPERAND (arg, 1);
4885 /* Check all this code for right semantics. */
4886 if (TREE_CODE (t) == FUNCTION_DECL)
4888 if (DECL_DESTRUCTOR_P (t))
4889 cp_error ("taking address of destructor");
4890 return build_unary_op (ADDR_EXPR, t, 0);
4892 if (TREE_CODE (t) == VAR_DECL)
4893 return build_unary_op (ADDR_EXPR, t, 0);
4898 if (TREE_OPERAND (arg, 0)
4899 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4900 && TREE_CODE (t) != FIELD_DECL)
4902 cp_error ("taking address of bound pointer-to-member expression");
4903 return error_mark_node;
4906 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4907 type = build_pointer_type (type);
4909 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4915 /* We permit compiler to make function calls returning
4916 objects of aggregate type look like lvalues. */
4920 if (TREE_CODE (targ) == SAVE_EXPR)
4921 targ = TREE_OPERAND (targ, 0);
4923 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4925 if (TREE_CODE (arg) == SAVE_EXPR)
4928 targ = build_cplus_new (TREE_TYPE (arg), arg);
4929 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4932 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4933 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4934 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4937 /* Don't let anything else be handled specially. */
4941 /* Mark EXP saying that we need to be able to take the
4942 address of it; it should not be allocated in a register.
4943 Value is 1 if successful.
4945 C++: we do not allow `current_class_ptr' to be addressable. */
4948 mark_addressable (exp)
4951 register tree x = exp;
4953 if (TREE_ADDRESSABLE (x) == 1)
4957 switch (TREE_CODE (x))
4964 x = TREE_OPERAND (x, 0);
4968 if (x == current_class_ptr)
4970 if (! flag_this_is_variable)
4971 error ("address of `this' not available");
4972 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4973 put_var_into_stack (x);
4977 if (TREE_STATIC (x) && TREE_READONLY (x)
4978 && DECL_RTL (x) != 0
4979 && ! DECL_IN_MEMORY_P (x))
4981 /* We thought this would make a good constant variable,
4982 but we were wrong. */
4983 push_permanent_obstack ();
4985 TREE_ASM_WRITTEN (x) = 0;
4987 rest_of_decl_compilation (x, 0,
4988 !DECL_FUNCTION_SCOPE_P (x),
4990 TREE_ADDRESSABLE (x) = 1;
4996 /* Caller should not be trying to mark initialized
4997 constant fields addressable. */
4998 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4999 || DECL_IN_AGGR_P (x) == 0
5001 || DECL_EXTERNAL (x), 314);
5005 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5006 && !DECL_ARTIFICIAL (x) && extra_warnings)
5007 cp_warning ("address requested for `%D', which is declared `register'",
5009 TREE_ADDRESSABLE (x) = 1;
5011 if (current_function && expanding_p)
5012 put_var_into_stack (x);
5016 if (DECL_LANG_SPECIFIC (x) != 0)
5018 x = DECL_MAIN_VARIANT (x);
5019 /* We have to test both conditions here. The first may be
5020 non-zero in the case of processing a default function. The
5021 second may be non-zero in the case of a template function. */
5022 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
5025 TREE_ADDRESSABLE (x) = 1;
5027 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
5031 TREE_ADDRESSABLE (x) = 1;
5035 TREE_ADDRESSABLE (x) = 1;
5036 mark_addressable (TREE_OPERAND (x, 0));
5044 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5047 build_x_conditional_expr (ifexp, op1, op2)
5048 tree ifexp, op1, op2;
5050 if (processing_template_decl)
5051 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5053 return build_conditional_expr (ifexp, op1, op2);
5056 /* Handle overloading of the ',' operator when needed. Otherwise,
5057 this function just builds an expression list. */
5060 build_x_compound_expr (list)
5063 tree rest = TREE_CHAIN (list);
5066 if (processing_template_decl)
5067 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5069 if (rest == NULL_TREE)
5070 return build_compound_expr (list);
5072 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5073 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5075 return build_x_compound_expr (tree_cons (NULL_TREE, result,
5076 TREE_CHAIN (rest)));
5078 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5080 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5081 /* the left-hand operand of a comma expression is like an expression
5082 statement: we should warn if it doesn't have any side-effects,
5083 unless it was explicitly cast to (void). */
5084 if ((extra_warnings || warn_unused)
5085 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5086 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5087 warning("left-hand operand of comma expression has no effect");
5089 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5090 else if (warn_unused)
5091 warn_if_unused_value (TREE_VALUE(list));
5094 return build_compound_expr
5095 (tree_cons (NULL_TREE, TREE_VALUE (list),
5096 build_expr_list (NULL_TREE,
5097 build_x_compound_expr (rest))));
5100 /* Given a list of expressions, return a compound expression
5101 that performs them all and returns the value of the last of them. */
5104 build_compound_expr (list)
5110 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5111 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5113 if (TREE_CHAIN (list) == 0)
5115 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5116 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5117 if (TREE_CODE (list) == NOP_EXPR
5118 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5119 list = TREE_OPERAND (list, 0);
5121 return TREE_VALUE (list);
5124 first = TREE_VALUE (list);
5125 first = convert_to_void (first, "lhs of comma");
5126 if (first == error_mark_node)
5127 return error_mark_node;
5129 rest = build_compound_expr (TREE_CHAIN (list));
5130 if (rest == error_mark_node)
5131 return error_mark_node;
5133 /* When pedantic, a compound expression cannot be a constant expression. */
5134 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5137 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5138 break_out_cleanups (first), rest);
5142 build_static_cast (type, expr)
5148 if (type == error_mark_node || expr == error_mark_node)
5149 return error_mark_node;
5151 if (TREE_CODE (expr) == OFFSET_REF)
5152 expr = resolve_offset_ref (expr);
5154 if (processing_template_decl)
5156 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5160 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5161 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5162 if (TREE_CODE (type) != REFERENCE_TYPE
5163 && TREE_CODE (expr) == NOP_EXPR
5164 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5165 expr = TREE_OPERAND (expr, 0);
5167 if (TREE_CODE (type) == VOID_TYPE)
5169 expr = convert_to_void (expr, /*implicit=*/NULL);
5173 if (TREE_CODE (type) == REFERENCE_TYPE)
5174 return (convert_from_reference
5175 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5176 LOOKUP_COMPLAIN, NULL_TREE)));
5178 if (IS_AGGR_TYPE (type))
5179 return build_cplus_new
5180 (type, (build_method_call
5181 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5182 TYPE_BINFO (type), LOOKUP_NORMAL)));
5184 expr = decay_conversion (expr);
5185 intype = TREE_TYPE (expr);
5187 /* FIXME handle casting to array type. */
5190 if (can_convert_arg (type, intype, expr))
5192 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5195 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5196 && at_least_as_qualified_p (TREE_TYPE (type),
5198 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5199 && ! TREE_VIA_VIRTUAL (binfo))
5202 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5204 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5205 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))))
5206 && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type)),
5207 TREE_TYPE (TREE_TYPE (intype)))
5208 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5209 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5210 && ! TREE_VIA_VIRTUAL (binfo))
5213 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5214 && TREE_CODE (type) != ARRAY_TYPE
5215 && TREE_CODE (type) != FUNCTION_TYPE
5216 && can_convert (intype, type))
5219 /* [expr.static.cast]
5221 The static_cast operator shall not be used to cast away
5223 if (ok && casts_away_constness (intype, type))
5225 cp_error ("static_cast from `%T' to `%T' casts away constness",
5227 return error_mark_node;
5231 return build_c_cast (type, expr);
5233 cp_error ("static_cast from `%T' to `%T'", intype, type);
5234 return error_mark_node;
5238 build_reinterpret_cast (type, expr)
5243 if (type == error_mark_node || expr == error_mark_node)
5244 return error_mark_node;
5246 if (TREE_CODE (expr) == OFFSET_REF)
5247 expr = resolve_offset_ref (expr);
5249 if (processing_template_decl)
5251 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5255 if (TREE_CODE (type) != REFERENCE_TYPE)
5257 expr = decay_conversion (expr);
5259 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5260 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5261 if (TREE_CODE (expr) == NOP_EXPR
5262 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5263 expr = TREE_OPERAND (expr, 0);
5266 intype = TREE_TYPE (expr);
5268 if (TREE_CODE (type) == REFERENCE_TYPE)
5270 if (! real_lvalue_p (expr))
5272 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5273 return error_mark_node;
5275 expr = build_unary_op (ADDR_EXPR, expr, 0);
5276 if (expr != error_mark_node)
5277 expr = build_reinterpret_cast
5278 (build_pointer_type (TREE_TYPE (type)), expr);
5279 if (expr != error_mark_node)
5280 expr = build_indirect_ref (expr, 0);
5283 else if (same_type_p (TYPE_MAIN_VARIANT (intype),
5284 TYPE_MAIN_VARIANT (type)))
5285 return build_static_cast (type, expr);
5287 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5288 || TREE_CODE (intype) == ENUMERAL_TYPE))
5290 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5292 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5293 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5296 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5297 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5299 if (TREE_READONLY_DECL_P (expr))
5300 expr = decl_constant_value (expr);
5301 return fold (build1 (NOP_EXPR, type, expr));
5303 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5304 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5306 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5307 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5310 if (TREE_READONLY_DECL_P (expr))
5311 expr = decl_constant_value (expr);
5312 return fold (build1 (NOP_EXPR, type, expr));
5314 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5315 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5317 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5318 if (TREE_READONLY_DECL_P (expr))
5319 expr = decl_constant_value (expr);
5320 return fold (build1 (NOP_EXPR, type, expr));
5324 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5325 return error_mark_node;
5328 return cp_convert (type, expr);
5332 build_const_cast (type, expr)
5337 if (type == error_mark_node || expr == error_mark_node)
5338 return error_mark_node;
5340 if (TREE_CODE (expr) == OFFSET_REF)
5341 expr = resolve_offset_ref (expr);
5343 if (processing_template_decl)
5345 tree t = build_min (CONST_CAST_EXPR, type, expr);
5349 if (!POINTER_TYPE_P (type))
5351 cp_error ("`%T' is not a pointer, reference, or pointer-to-data-member type",
5353 cp_error ("as required by const_cast");
5355 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5357 cp_error ("`%T' is a pointer or reference to a function type",
5359 cp_error ("which is forbidden by const_cast");
5360 return error_mark_node;
5363 if (TREE_CODE (type) != REFERENCE_TYPE)
5365 expr = decay_conversion (expr);
5367 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5368 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5369 if (TREE_CODE (expr) == NOP_EXPR
5370 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5371 expr = TREE_OPERAND (expr, 0);
5374 intype = TREE_TYPE (expr);
5376 if (same_type_p (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type)))
5377 return build_static_cast (type, expr);
5378 else if (TREE_CODE (type) == REFERENCE_TYPE)
5380 if (! real_lvalue_p (expr))
5382 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5383 return error_mark_node;
5386 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5388 expr = build_unary_op (ADDR_EXPR, expr, 0);
5389 expr = build1 (NOP_EXPR, type, expr);
5390 return convert_from_reference (expr);
5393 else if (TREE_CODE (type) == POINTER_TYPE
5394 && TREE_CODE (intype) == POINTER_TYPE
5395 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5396 return cp_convert (type, expr);
5398 cp_error ("const_cast from `%T' to `%T'", intype, type);
5399 return error_mark_node;
5402 /* Build an expression representing a cast to type TYPE of expression EXPR.
5404 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5405 when doing the cast. */
5408 build_c_cast (type, expr)
5411 register tree value = expr;
5414 if (type == error_mark_node || expr == error_mark_node)
5415 return error_mark_node;
5417 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5418 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5419 if (TREE_CODE (type) != REFERENCE_TYPE
5420 && TREE_CODE (value) == NOP_EXPR
5421 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5422 value = TREE_OPERAND (value, 0);
5424 if (TREE_CODE (value) == OFFSET_REF)
5425 value = resolve_offset_ref (value);
5427 if (TREE_CODE (type) == ARRAY_TYPE)
5429 /* Allow casting from T1* to T2[] because Cfront allows it.
5430 NIHCL uses it. It is not valid ANSI C however, and hence, not
5432 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5435 pedwarn ("ANSI C++ forbids casting to an array type");
5436 type = build_pointer_type (TREE_TYPE (type));
5440 error ("ANSI C++ forbids casting to an array type");
5441 return error_mark_node;
5445 if (TREE_CODE (type) == FUNCTION_TYPE
5446 || TREE_CODE (type) == METHOD_TYPE)
5448 cp_error ("casting to function type `%T'", type);
5449 return error_mark_node;
5452 if (processing_template_decl)
5454 tree t = build_min (CAST_EXPR, type,
5455 min_tree_cons (NULL_TREE, value, NULL_TREE));
5459 if (TREE_CODE (type) == VOID_TYPE)
5461 /* Conversion to void does not cause any of the normal function to
5462 * pointer, array to pointer and lvalue to rvalue decays. */
5464 value = convert_to_void (value, /*implicit=*/NULL);
5467 /* Convert functions and arrays to pointers and
5468 convert references to their expanded types,
5469 but don't convert any other types. If, however, we are
5470 casting to a class type, there's no reason to do this: the
5471 cast will only succeed if there is a converting constructor,
5472 and the default conversions will be done at that point. In
5473 fact, doing the default conversion here is actually harmful
5477 struct S { S(const A&); };
5479 since we don't want the array-to-pointer conversion done. */
5480 if (!IS_AGGR_TYPE (type))
5482 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5483 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5484 /* Don't do the default conversion on a ->* expression. */
5485 && ! (TREE_CODE (type) == POINTER_TYPE
5486 && bound_pmf_p (value)))
5487 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5488 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5489 value = default_conversion (value);
5491 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5492 /* However, even for class types, we still need to strip away
5493 the reference type, since the call to convert_force below
5494 does not expect the input expression to be of reference
5496 value = convert_from_reference (value);
5498 otype = TREE_TYPE (value);
5500 /* Optionally warn about potentially worrisome casts. */
5503 && TREE_CODE (type) == POINTER_TYPE
5504 && TREE_CODE (otype) == POINTER_TYPE
5505 && !at_least_as_qualified_p (TREE_TYPE (type),
5507 cp_warning ("cast discards qualifiers from pointer target type");
5509 /* Warn about possible alignment problems. */
5510 if (STRICT_ALIGNMENT && warn_cast_align
5511 && TREE_CODE (type) == POINTER_TYPE
5512 && TREE_CODE (otype) == POINTER_TYPE
5513 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5514 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5515 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5516 warning ("cast increases required alignment of target type");
5519 /* We should see about re-enabling these, they seem useful to
5521 if (TREE_CODE (type) == INTEGER_TYPE
5522 && TREE_CODE (otype) == POINTER_TYPE
5523 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5524 warning ("cast from pointer to integer of different size");
5526 if (TREE_CODE (type) == POINTER_TYPE
5527 && TREE_CODE (otype) == INTEGER_TYPE
5528 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5529 /* Don't warn about converting 0 to pointer,
5530 provided the 0 was explicit--not cast or made by folding. */
5531 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5532 warning ("cast to pointer from integer of different size");
5535 if (TREE_CODE (type) == REFERENCE_TYPE)
5536 value = (convert_from_reference
5537 (convert_to_reference (type, value, CONV_C_CAST,
5538 LOOKUP_COMPLAIN, NULL_TREE)));
5543 if (TREE_READONLY_DECL_P (value))
5544 value = decl_constant_value (value);
5547 value = convert_force (type, value, CONV_C_CAST);
5549 /* Ignore any integer overflow caused by the cast. */
5550 if (TREE_CODE (value) == INTEGER_CST)
5552 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5553 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5557 /* Always produce some operator for an explicit cast,
5558 so we can tell (for -pedantic) that the cast is no lvalue. */
5559 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5560 && real_lvalue_p (value))
5561 value = non_lvalue (value);
5566 /* Build an assignment expression of lvalue LHS from value RHS.
5567 MODIFYCODE is the code for a binary operator that we use
5568 to combine the old value of LHS with RHS to get the new value.
5569 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5571 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5574 build_modify_expr (lhs, modifycode, rhs)
5576 enum tree_code modifycode;
5579 register tree result;
5581 tree lhstype = TREE_TYPE (lhs);
5582 tree olhstype = lhstype;
5585 /* Avoid duplicate error messages from operands that had errors. */
5586 if (lhs == error_mark_node || rhs == error_mark_node)
5587 return error_mark_node;
5589 /* Types that aren't fully specified cannot be used in assignments. */
5590 lhs = require_complete_type (lhs);
5594 /* Handle control structure constructs used as "lvalues". */
5596 switch (TREE_CODE (lhs))
5598 /* Handle --foo = 5; as these are valid constructs in C++ */
5599 case PREDECREMENT_EXPR:
5600 case PREINCREMENT_EXPR:
5601 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5602 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5603 stabilize_reference (TREE_OPERAND (lhs, 0)),
5604 TREE_OPERAND (lhs, 1));
5605 return build (COMPOUND_EXPR, lhstype,
5607 build_modify_expr (TREE_OPERAND (lhs, 0),
5610 /* Handle (a, b) used as an "lvalue". */
5612 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5614 if (newrhs == error_mark_node)
5615 return error_mark_node;
5616 return build (COMPOUND_EXPR, lhstype,
5617 TREE_OPERAND (lhs, 0), newrhs);
5620 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5621 if (newrhs == error_mark_node)
5622 return error_mark_node;
5623 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5625 /* Handle (a ? b : c) used as an "lvalue". */
5627 rhs = save_expr (rhs);
5629 /* Produce (a ? (b = rhs) : (c = rhs))
5630 except that the RHS goes through a save-expr
5631 so the code to compute it is only emitted once. */
5634 /* Check this here to avoid odd errors when trying to convert
5635 a throw to the type of the COND_EXPR. */
5636 if (!lvalue_or_else (lhs, "assignment"))
5637 return error_mark_node;
5639 cond = build_conditional_expr
5640 (TREE_OPERAND (lhs, 0),
5641 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5642 TREE_OPERAND (lhs, 1)),
5644 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5645 TREE_OPERAND (lhs, 2)),
5648 if (cond == error_mark_node)
5650 /* Make sure the code to compute the rhs comes out
5651 before the split. */
5652 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5653 /* Case to void to suppress warning
5654 from warn_if_unused_value. */
5655 cp_convert (void_type_node, rhs), cond);
5662 if (TREE_CODE (lhs) == OFFSET_REF)
5664 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5666 /* Static class member? */
5667 tree member = TREE_OPERAND (lhs, 1);
5668 if (TREE_CODE (member) == VAR_DECL)
5672 compiler_error ("invalid static class member");
5673 return error_mark_node;
5677 lhs = resolve_offset_ref (lhs);
5679 olhstype = lhstype = TREE_TYPE (lhs);
5682 if (lhs == error_mark_node)
5685 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5686 && modifycode != INIT_EXPR)
5688 lhs = convert_from_reference (lhs);
5689 olhstype = lhstype = TREE_TYPE (lhs);
5692 /* If a binary op has been requested, combine the old LHS value with the RHS
5693 producing the value we should actually store into the LHS. */
5695 if (modifycode == INIT_EXPR)
5697 if (! IS_AGGR_TYPE (lhstype))
5698 /* Do the default thing */;
5701 result = build_method_call (lhs, ctor_identifier,
5702 build_expr_list (NULL_TREE, rhs),
5703 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5704 if (result == NULL_TREE)
5705 return error_mark_node;
5709 else if (modifycode == NOP_EXPR)
5711 /* `operator=' is not an inheritable operator. */
5712 if (! IS_AGGR_TYPE (lhstype))
5713 /* Do the default thing */;
5716 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5717 lhs, rhs, make_node (NOP_EXPR));
5718 if (result == NULL_TREE)
5719 return error_mark_node;
5724 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5726 my_friendly_abort (978652);
5730 lhs = stabilize_reference (lhs);
5731 newrhs = build_binary_op (modifycode, lhs, rhs);
5732 if (newrhs == error_mark_node)
5734 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5735 TREE_TYPE (lhs), TREE_TYPE (rhs));
5736 return error_mark_node;
5740 /* Handle a cast used as an "lvalue".
5741 We have already performed any binary operator using the value as cast.
5742 Now convert the result to the cast type of the lhs,
5743 and then true type of the lhs and store it there;
5744 then convert result back to the cast type to be the value
5745 of the assignment. */
5747 switch (TREE_CODE (lhs))
5752 case FIX_TRUNC_EXPR:
5753 case FIX_FLOOR_EXPR:
5754 case FIX_ROUND_EXPR:
5756 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5757 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5758 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5759 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5760 newrhs = default_conversion (newrhs);
5762 tree inner_lhs = TREE_OPERAND (lhs, 0);
5765 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5766 otherwise the result is an rvalue. */
5767 if (! lvalue_p (lhs))
5768 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5770 result = build_modify_expr (inner_lhs, NOP_EXPR,
5771 cp_convert (TREE_TYPE (inner_lhs),
5772 cp_convert (lhstype, newrhs)));
5773 if (result == error_mark_node)
5775 return cp_convert (TREE_TYPE (lhs), result);
5782 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5783 Reject anything strange now. */
5785 if (!lvalue_or_else (lhs, "assignment"))
5786 return error_mark_node;
5788 GNU_xref_assign (lhs);
5790 /* Warn about storing in something that is `const'. */
5791 /* For C++, don't warn if this is initialization. */
5792 if (modifycode != INIT_EXPR
5793 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5794 /* Functions are not modifiable, even though they are
5796 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5797 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5798 && C_TYPE_FIELDS_READONLY (lhstype))
5799 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5800 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5801 readonly_error (lhs, "assignment", 0);
5803 /* If storing into a structure or union member,
5804 it has probably been given type `int'.
5805 Compute the type that would go with
5806 the actual amount of storage the member occupies. */
5808 if (TREE_CODE (lhs) == COMPONENT_REF
5809 && (TREE_CODE (lhstype) == INTEGER_TYPE
5810 || TREE_CODE (lhstype) == REAL_TYPE
5811 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5813 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5815 /* If storing in a field that is in actuality a short or narrower
5816 than one, we must store in the field in its actual type. */
5818 if (lhstype != TREE_TYPE (lhs))
5820 lhs = copy_node (lhs);
5821 TREE_TYPE (lhs) = lhstype;
5825 if (modifycode != INIT_EXPR)
5827 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5828 modifycode = NOP_EXPR;
5829 /* Reference-bashing */
5830 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5832 tree tmp = convert_from_reference (lhs);
5833 lhstype = TREE_TYPE (tmp);
5834 if (TYPE_SIZE (lhstype) == 0)
5836 incomplete_type_error (lhs, lhstype);
5837 return error_mark_node;
5842 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5844 tree tmp = convert_from_reference (newrhs);
5845 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5847 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5848 return error_mark_node;
5854 if (TREE_SIDE_EFFECTS (lhs))
5855 lhs = stabilize_reference (lhs);
5856 if (TREE_SIDE_EFFECTS (newrhs))
5857 newrhs = stabilize_reference (newrhs);
5859 /* Convert new value to destination type. */
5861 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5865 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5867 cp_error ("incompatible types in assignment of `%T' to `%T'",
5868 TREE_TYPE (rhs), lhstype);
5869 return error_mark_node;
5872 /* Allow array assignment in compiler-generated code. */
5873 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5874 pedwarn ("ANSI C++ forbids assignment of arrays");
5876 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5877 ? 1 + (modifycode != INIT_EXPR): 0;
5878 return (build_vec_init
5879 (lhs, lhs, array_type_nelts (lhstype), newrhs,
5883 if (modifycode == INIT_EXPR)
5885 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5886 "assignment", NULL_TREE, 0);
5887 if (current_function_decl &&
5888 lhs == DECL_RESULT (current_function_decl))
5890 if (DECL_INITIAL (lhs))
5891 warning ("return value from function receives multiple initializations");
5892 DECL_INITIAL (lhs) = newrhs;
5897 /* Avoid warnings on enum bit fields. */
5898 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5899 && TREE_CODE (lhstype) == INTEGER_TYPE)
5901 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5903 newrhs = convert_force (lhstype, newrhs, 0);
5906 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5908 if (TREE_CODE (newrhs) == CALL_EXPR
5909 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5910 newrhs = build_cplus_new (lhstype, newrhs);
5912 /* Can't initialize directly from a TARGET_EXPR, since that would
5913 cause the lhs to be constructed twice, and possibly result in
5914 accidental self-initialization. So we force the TARGET_EXPR to be
5915 expanded without a target. */
5916 if (TREE_CODE (newrhs) == TARGET_EXPR)
5917 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5918 TREE_OPERAND (newrhs, 0));
5921 if (newrhs == error_mark_node)
5922 return error_mark_node;
5924 if (TREE_CODE (newrhs) == COND_EXPR)
5927 tree cond = TREE_OPERAND (newrhs, 0);
5929 if (TREE_SIDE_EFFECTS (lhs))
5930 cond = build_compound_expr (tree_cons
5932 build_expr_list (NULL_TREE, cond)));
5934 /* Cannot have two identical lhs on this one tree (result) as preexpand
5935 calls will rip them out and fill in RTL for them, but when the
5936 rtl is generated, the calls will only be in the first side of the
5937 condition, not on both, or before the conditional jump! (mrs) */
5938 lhs1 = break_out_calls (lhs);
5941 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5942 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5943 lhstype, lhs, newrhs);
5946 tree result_type = TREE_TYPE (newrhs);
5947 /* We have to convert each arm to the proper type because the
5948 types may have been munged by constant folding. */
5950 = build (COND_EXPR, result_type, cond,
5951 build_modify_expr (lhs, modifycode,
5952 cp_convert (result_type,
5953 TREE_OPERAND (newrhs, 1))),
5954 build_modify_expr (lhs1, modifycode,
5955 cp_convert (result_type,
5956 TREE_OPERAND (newrhs, 2))));
5960 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5961 lhstype, lhs, newrhs);
5963 TREE_SIDE_EFFECTS (result) = 1;
5965 /* If we got the LHS in a different type for storing in,
5966 convert the result back to the nominal type of LHS
5967 so that the value we return always has the same type
5968 as the LHS argument. */
5970 if (olhstype == TREE_TYPE (result))
5972 /* Avoid warnings converting integral types back into enums
5973 for enum bit fields. */
5974 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5975 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5977 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5978 TREE_NO_UNUSED_WARNING (result) = 1;
5981 return convert_for_assignment (olhstype, result, "assignment",
5986 build_x_modify_expr (lhs, modifycode, rhs)
5988 enum tree_code modifycode;
5991 if (processing_template_decl)
5992 return build_min_nt (MODOP_EXPR, lhs,
5993 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5995 if (modifycode != NOP_EXPR)
5997 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5998 make_node (modifycode));
6002 return build_modify_expr (lhs, modifycode, rhs);
6006 /* Get difference in deltas for different pointer to member function
6007 types. Return integer_zero_node, if FROM cannot be converted to a
6008 TO type. If FORCE is true, then allow reverse conversions as well.
6010 Note that the naming of FROM and TO is kind of backwards; the return
6011 value is what we add to a TO in order to get a FROM. They are named
6012 this way because we call this function to find out how to convert from
6013 a pointer to member of FROM to a pointer to member of TO. */
6016 get_delta_difference (from, to, force)
6020 tree delta = integer_zero_node;
6026 /* Should get_base_distance here, so we can check if any thing along the
6027 path is virtual, and we need to make sure we stay
6028 inside the real binfos when going through virtual bases.
6029 Maybe we should replace virtual bases with
6030 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6031 binfo = get_binfo (from, to, 1);
6032 if (binfo == error_mark_node)
6034 error (" in pointer to member function conversion");
6041 error_not_base_type (from, to);
6042 error (" in pointer to member conversion");
6045 binfo = get_binfo (to, from, 1);
6046 if (binfo == 0 || binfo == error_mark_node)
6048 if (binfo_from_vbase (binfo))
6050 binfo = binfo_member (BINFO_TYPE (binfo),
6051 CLASSTYPE_VBASECLASSES (from));
6052 cp_warning ("pointer to member cast to virtual base `%T'",
6053 BINFO_TYPE (binfo));
6054 warning (" will only work if you are very careful");
6056 delta = BINFO_OFFSET (binfo);
6057 delta = cp_convert (ptrdiff_type_node, delta);
6059 return build_binary_op (MINUS_EXPR,
6064 if (binfo_from_vbase (binfo))
6068 cp_warning ("pointer to member cast from virtual base `%T'",
6069 BINFO_TYPE (binfo));
6070 warning (" will only work if you are very careful");
6073 cp_error ("pointer to member conversion from virtual base `%T'",
6074 BINFO_TYPE (binfo));
6077 return BINFO_OFFSET (binfo);
6081 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6082 tree type, delta, idx, pfn, delta2;
6087 /* This is the old way we did it. We want to avoid calling
6088 digest_init, so that it can give an error if we use { } when
6089 initializing a pointer to member function. */
6093 u = build_nt (CONSTRUCTOR, NULL_TREE,
6094 tree_cons (pfn_identifier, pfn, NULL_TREE));
6098 u = build_nt (CONSTRUCTOR, NULL_TREE,
6099 tree_cons (delta2_identifier, delta2, NULL_TREE));
6102 u = build_nt (CONSTRUCTOR, NULL_TREE,
6103 tree_cons (NULL_TREE, delta,
6104 tree_cons (NULL_TREE, idx,
6105 tree_cons (NULL_TREE, u, NULL_TREE))));
6107 return digest_init (type, u, (tree*)0);
6109 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6111 int allconstant, allsimple;
6113 delta_field = TYPE_FIELDS (type);
6114 idx_field = TREE_CHAIN (delta_field);
6115 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6116 subtype = TREE_TYPE (pfn_or_delta2_field);
6117 pfn_field = TYPE_FIELDS (subtype);
6118 delta2_field = TREE_CHAIN (pfn_field);
6122 allconstant = TREE_CONSTANT (pfn);
6123 allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6124 u = tree_cons (pfn_field, pfn, NULL_TREE);
6128 delta2 = convert_and_check (delta_type_node, delta2);
6129 allconstant = TREE_CONSTANT (delta2);
6130 allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6131 u = tree_cons (delta2_field, delta2, NULL_TREE);
6134 delta = convert_and_check (delta_type_node, delta);
6135 idx = convert_and_check (delta_type_node, idx);
6137 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6138 allsimple = allsimple
6139 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6140 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6142 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6143 u = tree_cons (delta_field, delta,
6144 tree_cons (idx_field, idx,
6145 tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6146 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6147 TREE_CONSTANT (u) = allconstant;
6148 TREE_STATIC (u) = allconstant && allsimple;
6153 /* Build a constructor for a pointer to member function. It can be
6154 used to initialize global variables, local variable, or used
6155 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6158 If FORCE is non-zero, then force this conversion, even if
6159 we would rather not do it. Usually set when using an explicit
6162 Return error_mark_node, if something goes wrong. */
6165 build_ptrmemfunc (type, pfn, force)
6170 tree pfn_type = TREE_TYPE (pfn);
6171 tree to_type = build_ptrmemfunc_type (type);
6173 /* Handle multiple conversions of pointer to member functions. */
6174 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6176 tree idx = integer_zero_node;
6177 tree delta = integer_zero_node;
6178 tree delta2 = integer_zero_node;
6179 tree npfn = NULL_TREE;
6180 tree ndelta, ndelta2;
6184 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6185 cp_error ("conversion to `%T' from `%T'",
6188 if (TREE_CODE (pfn) == PTRMEM_CST)
6190 /* We could just build the resulting CONSTRUCTOR now, but we
6191 don't, relying on the general machinery below, together
6192 with constant-folding, to do the right thing. We don't
6193 want to return a PTRMEM_CST here, since a
6194 pointer-to-member constant is no longer a valid template
6195 argument once it is cast to any type, including its
6197 expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
6199 /* This constant points to a non-virtual function.
6200 NDELTA2 will be NULL, but it's value doesn't really
6201 matter since we won't use it anyhow. */
6202 ndelta2 = integer_zero_node;
6204 else if (same_type_p (to_type, pfn_type))
6205 /* We don't have to do any conversion. Note that we do this
6206 after checking for a PTRMEM_CST so that a PTRMEM_CST, cast
6207 to its own type, will not be considered a legal non-type
6208 template argument. */
6212 ndelta = cp_convert (ptrdiff_type_node,
6213 build_component_ref (pfn,
6216 ndelta2 = cp_convert (ptrdiff_type_node,
6217 DELTA2_FROM_PTRMEMFUNC (pfn));
6218 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6221 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6222 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6224 delta = build_binary_op (PLUS_EXPR, ndelta, n);
6225 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n);
6226 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6228 /* If it's a virtual function, this is what we want. */
6229 e2 = build_ptrmemfunc1 (to_type, delta, idx,
6232 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6233 npfn = build1 (NOP_EXPR, type, pfn);
6234 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6236 /* But if it's a non-virtual function, or NULL, we use this
6238 e3 = build_ptrmemfunc1 (to_type, delta,
6239 idx, npfn, NULL_TREE);
6240 return build_conditional_expr (e1, e2, e3);
6243 /* Handle null pointer to member function conversions. */
6244 if (integer_zerop (pfn))
6246 pfn = build_c_cast (type, integer_zero_node);
6247 return build_ptrmemfunc1 (to_type,
6248 integer_zero_node, integer_zero_node,
6252 if (type_unknown_p (pfn))
6253 return instantiate_type (type, pfn, 1);
6255 fn = TREE_OPERAND (pfn, 0);
6256 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6257 return make_ptrmem_cst (to_type, fn);
6260 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6264 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6271 tree type = TREE_TYPE (cst);
6272 tree fn = PTRMEM_CST_MEMBER (cst);
6273 tree ptr_class, fn_class;
6275 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6277 /* The class that the function belongs to. */
6278 fn_class = DECL_CLASS_CONTEXT (fn);
6280 /* The class that we're creating a pointer to member of. */
6281 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6283 /* First, calculate the adjustment to the function's class. */
6284 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6286 if (!DECL_VIRTUAL_P (fn))
6288 *idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6289 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6290 *delta2 = NULL_TREE;
6294 /* If we're dealing with a virtual function, we have to adjust 'this'
6295 again, to point to the base which provides the vtable entry for
6296 fn; the call will do the opposite adjustment. */
6297 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6298 tree binfo = binfo_or_else (orig_class, fn_class);
6299 *delta = size_binop (PLUS_EXPR, *delta, BINFO_OFFSET (binfo));
6301 /* Map everything down one to make room for the null PMF. */
6302 *idx = size_binop (PLUS_EXPR, DECL_VINDEX (fn), integer_one_node);
6305 /* Offset from an object of PTR_CLASS to the vptr for ORIG_CLASS. */
6306 *delta2 = size_binop (PLUS_EXPR, *delta,
6307 get_vfield_offset (TYPE_BINFO (orig_class)));
6311 /* Return an expression for DELTA2 from the pointer-to-member function
6315 delta2_from_ptrmemfunc (t)
6318 if (TREE_CODE (t) == PTRMEM_CST)
6325 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6330 return (build_component_ref
6331 (build_component_ref (t,
6332 pfn_or_delta2_identifier, NULL_TREE,
6334 delta2_identifier, NULL_TREE, 0));
6337 /* Return an expression for PFN from the pointer-to-member function
6341 pfn_from_ptrmemfunc (t)
6344 if (TREE_CODE (t) == PTRMEM_CST)
6351 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6356 return (build_component_ref
6357 (build_component_ref (t,
6358 pfn_or_delta2_identifier, NULL_TREE,
6360 pfn_identifier, NULL_TREE, 0));
6363 /* Convert value RHS to type TYPE as preparation for an assignment to
6364 an lvalue of type TYPE. ERRTYPE is a string to use in error
6365 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6366 are doing the conversion in order to pass the PARMNUMth argument of
6370 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6372 const char *errtype;
6376 register enum tree_code codel = TREE_CODE (type);
6377 register tree rhstype;
6378 register enum tree_code coder;
6380 if (codel == OFFSET_TYPE)
6381 my_friendly_abort (990505);
6383 if (TREE_CODE (rhs) == OFFSET_REF)
6384 rhs = resolve_offset_ref (rhs);
6386 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6387 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6388 rhs = TREE_OPERAND (rhs, 0);
6390 rhstype = TREE_TYPE (rhs);
6391 coder = TREE_CODE (rhstype);
6393 if (rhs == error_mark_node || rhstype == error_mark_node)
6394 return error_mark_node;
6395 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6396 return error_mark_node;
6398 /* Issue warnings about peculiar, but legal, uses of NULL. */
6399 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6400 cp_warning ("converting NULL to non-pointer type");
6402 /* The RHS of an assignment cannot have void type. */
6403 if (coder == VOID_TYPE)
6405 error ("void value not ignored as it ought to be");
6406 return error_mark_node;
6409 /* Simplify the RHS if possible. */
6410 if (TREE_CODE (rhs) == CONST_DECL)
6411 rhs = DECL_INITIAL (rhs);
6412 else if (TREE_READONLY_DECL_P (rhs))
6413 rhs = decl_constant_value (rhs);
6415 /* Warn about assigning a floating-point type to an integer type. */
6416 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6419 cp_warning ("`%T' used for argument %P of `%D'",
6420 rhstype, parmnum, fndecl);
6422 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6424 /* And warn about assigning a negative value to an unsigned
6426 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6428 if (TREE_CODE (rhs) == INTEGER_CST
6429 && TREE_NEGATED_INT (rhs))
6432 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6433 rhs, parmnum, fndecl);
6435 cp_warning ("%s of negative value `%E' to `%T'",
6436 errtype, rhs, type);
6438 overflow_warning (rhs);
6439 if (TREE_CONSTANT (rhs))
6445 The expression is implicitly converted (clause _conv_) to the
6446 cv-unqualified type of the left operand. */
6447 if (!can_convert_arg (type, rhstype, rhs))
6449 /* When -Wno-pmf-conversions is use, we just silently allow
6450 conversions from pointers-to-members to plain pointers. If
6451 the conversion doesn't work, cp_convert will complain. */
6453 && TYPE_PTR_P (type)
6454 && TYPE_PTRMEMFUNC_P (rhstype))
6455 rhs = cp_convert (strip_top_quals (type), rhs);
6458 /* If the right-hand side has unknown type, then it is an
6459 overloaded function. Call instantiate_type to get error
6461 if (rhstype == unknown_type_node)
6462 instantiate_type (type, rhs, 1);
6464 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6465 rhstype, type, parmnum, fndecl);
6467 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6469 return error_mark_node;
6472 return perform_implicit_conversion (strip_top_quals (type), rhs);
6475 /* Convert RHS to be of type TYPE.
6476 If EXP is non-zero, it is the target of the initialization.
6477 ERRTYPE is a string to use in error messages.
6479 Two major differences between the behavior of
6480 `convert_for_assignment' and `convert_for_initialization'
6481 are that references are bashed in the former, while
6482 copied in the latter, and aggregates are assigned in
6483 the former (operator=) while initialized in the
6486 If using constructor make sure no conversion operator exists, if one does
6487 exist, an ambiguity exists.
6489 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6492 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6493 tree exp, type, rhs;
6495 const char *errtype;
6499 register enum tree_code codel = TREE_CODE (type);
6500 register tree rhstype;
6501 register enum tree_code coder;
6503 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6504 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6505 if (TREE_CODE (rhs) == NOP_EXPR
6506 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6507 && codel != REFERENCE_TYPE)
6508 rhs = TREE_OPERAND (rhs, 0);
6510 if (rhs == error_mark_node
6511 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6512 return error_mark_node;
6514 if (TREE_CODE (rhs) == OFFSET_REF)
6516 rhs = resolve_offset_ref (rhs);
6517 if (rhs == error_mark_node)
6518 return error_mark_node;
6521 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6522 rhs = convert_from_reference (rhs);
6524 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6525 && TREE_CODE (type) != ARRAY_TYPE
6526 && (TREE_CODE (type) != REFERENCE_TYPE
6527 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6528 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6529 && (TREE_CODE (type) != REFERENCE_TYPE
6530 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6531 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6532 rhs = default_conversion (rhs);
6534 rhstype = TREE_TYPE (rhs);
6535 coder = TREE_CODE (rhstype);
6537 if (coder == ERROR_MARK)
6538 return error_mark_node;
6540 /* We accept references to incomplete types, so we can
6541 return here before checking if RHS is of complete type. */
6543 if (codel == REFERENCE_TYPE)
6545 /* This should eventually happen in convert_arguments. */
6546 extern int warningcount, errorcount;
6547 int savew = 0, savee = 0;
6550 savew = warningcount, savee = errorcount;
6551 rhs = initialize_reference (type, rhs);
6554 if (warningcount > savew)
6555 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6556 else if (errorcount > savee)
6557 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6563 exp = require_complete_type (exp);
6564 if (exp == error_mark_node)
6565 return error_mark_node;
6567 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6568 rhstype = TREE_TYPE (rhstype);
6570 type = complete_type (type);
6572 if (IS_AGGR_TYPE (type))
6573 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6575 if (type == TREE_TYPE (rhs))
6577 /* Issue warnings about peculiar, but legal, uses of NULL. We
6578 do this *before* the call to decl_constant_value so as to
6579 avoid duplicate warnings on code like `const int I = NULL;
6581 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6582 cp_warning ("converting NULL to non-pointer type");
6584 if (TREE_READONLY_DECL_P (rhs))
6585 rhs = decl_constant_value (rhs);
6590 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6593 /* Expand an ASM statement with operands, handling output operands
6594 that are not variables or INDIRECT_REFS by transforming such
6595 cases into cases that expand_asm_operands can handle.
6597 Arguments are same as for expand_asm_operands.
6599 We don't do default conversions on all inputs, because it can screw
6600 up operands that are expected to be in memory. */
6603 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6604 tree string, outputs, inputs, clobbers;
6609 int noutputs = list_length (outputs);
6611 /* o[I] is the place that output number I should be written. */
6612 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6615 /* Record the contents of OUTPUTS before it is modified. */
6616 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6617 o[i] = TREE_VALUE (tail);
6619 /* Generate the ASM_OPERANDS insn;
6620 store into the TREE_VALUEs of OUTPUTS some trees for
6621 where the values were actually stored. */
6622 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6624 /* Copy all the intermediate outputs into the specified outputs. */
6625 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6627 if (o[i] != TREE_VALUE (tail))
6629 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6630 const0_rtx, VOIDmode, EXPAND_NORMAL);
6633 /* Detect modification of read-only values.
6634 (Otherwise done by build_modify_expr.) */
6637 tree type = TREE_TYPE (o[i]);
6638 if (CP_TYPE_CONST_P (type)
6639 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6640 && C_TYPE_FIELDS_READONLY (type)))
6641 readonly_error (o[i], "modification by `asm'", 1);
6645 /* Those MODIFY_EXPRs could do autoincrements. */
6649 /* If RETVAL is the address of, or a reference to, a local variable or
6650 temporary give an appropraite warning. */
6653 maybe_warn_about_returning_address_of_local (retval)
6656 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6658 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6660 tree whats_returned;
6662 /* Sort through common things to see what it is
6663 we are returning. */
6664 whats_returned = retval;
6665 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6667 whats_returned = TREE_OPERAND (whats_returned, 1);
6668 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6669 whats_returned = TREE_OPERAND (whats_returned, 0);
6671 while (TREE_CODE (whats_returned) == CONVERT_EXPR
6672 || TREE_CODE (whats_returned) == NOP_EXPR)
6673 whats_returned = TREE_OPERAND (whats_returned, 0);
6674 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6676 whats_returned = TREE_OPERAND (whats_returned, 0);
6677 while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6678 || TREE_CODE (whats_returned) == TARGET_EXPR)
6680 /* Get the target. */
6681 whats_returned = TREE_OPERAND (whats_returned, 0);
6682 warning ("returning reference to temporary");
6686 if (TREE_CODE (whats_returned) == VAR_DECL
6687 && DECL_NAME (whats_returned))
6689 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
6690 warning ("reference to non-lvalue returned");
6691 else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
6692 && DECL_FUNCTION_SCOPE_P (whats_returned)
6693 && !(TREE_STATIC (whats_returned)
6694 || TREE_PUBLIC (whats_returned)))
6695 cp_warning_at ("reference to local variable `%D' returned",
6699 else if (TREE_CODE (retval) == ADDR_EXPR)
6701 tree whats_returned = TREE_OPERAND (retval, 0);
6703 if (TREE_CODE (whats_returned) == VAR_DECL
6704 && DECL_NAME (whats_returned)
6705 && DECL_FUNCTION_SCOPE_P (whats_returned)
6706 && !(TREE_STATIC (whats_returned)
6707 || TREE_PUBLIC (whats_returned)))
6708 cp_warning_at ("address of local variable `%D' returned",
6713 /* Check that returning RETVAL from the current function is legal.
6714 Return an expression explicitly showing all conversions required to
6715 change RETVAL into the function return type, and to assign it to
6716 the DECL_RESULT for the function. */
6719 check_return_expr (retval)
6723 /* The type actually returned by the function, after any
6726 int fn_returns_value_p;
6728 /* A `volatile' function is one that isn't supposed to return, ever.
6729 (This is a G++ extension, used to get better code for functions
6730 that call the `volatile' function.) */
6731 if (TREE_THIS_VOLATILE (current_function_decl))
6732 warning ("function declared `noreturn' has a `return' statement");
6734 /* Check for various simple errors. */
6735 if (retval == error_mark_node)
6737 /* If an error occurred, there's nothing to do. */
6738 current_function_returns_null = 1;
6739 return error_mark_node;
6741 else if (dtor_label)
6744 error ("returning a value from a destructor");
6747 else if (in_function_try_handler
6748 && DECL_CONSTRUCTOR_P (current_function_decl))
6750 /* If a return statement appears in a handler of the
6751 function-try-block of a constructor, the program is ill-formed. */
6752 error ("cannot return from a handler of a function-try-block of a constructor");
6753 return error_mark_node;
6755 else if (retval && DECL_CONSTRUCTOR_P (current_function_decl))
6756 /* You can't return a value from a constructor. */
6757 error ("returning a value from a constructor");
6759 /* Constructors actually always return `this', even though in C++
6760 you can't return a value from a constructor. */
6761 if (DECL_CONSTRUCTOR_P (current_function_decl))
6762 retval = current_class_ptr;
6764 /* When no explicit return-value is given in a function with a named
6765 return value, the named return value is used. */
6766 result = DECL_RESULT (current_function_decl);
6767 valtype = TREE_TYPE (result);
6768 my_friendly_assert (valtype != NULL_TREE, 19990924);
6769 fn_returns_value_p = !same_type_p (valtype, void_type_node);
6770 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6773 /* Check for a return statement with no return value in a function
6774 that's supposed to return a value. */
6775 if (!retval && fn_returns_value_p)
6777 pedwarn ("`return' with no value, in function returning non-void");
6778 /* Clear this, so finish_function won't say that we reach the
6779 end of a non-void function (which we don't, we gave a
6781 current_function_returns_null = 0;
6783 /* Check for a return statement with a value in a function that
6784 isn't supposed to return a value. */
6785 else if (retval && !fn_returns_value_p)
6787 if (same_type_p (TREE_TYPE (retval), void_type_node))
6788 /* You can return a `void' value from a function of `void'
6789 type. In that case, we have to evaluate the expression for
6790 its side-effects. */
6791 finish_expr_stmt (retval);
6793 pedwarn ("`return' with a value, in function returning void");
6795 current_function_returns_null = 1;
6797 /* There's really no value to return, after all. */
6801 /* Remember that this function can sometimes return without a
6803 current_function_returns_null = 1;
6805 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6806 if ((DECL_NAME (current_function_decl) == ansi_opname[(int) NEW_EXPR]
6807 || DECL_NAME (current_function_decl) == ansi_opname[(int) VEC_NEW_EXPR])
6808 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6809 && null_ptr_cst_p (retval))
6810 cp_warning ("operator new should throw an exception, not return NULL");
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 /* We don't need to do any conversions when there's nothing being
6823 /* Do any required conversions. */
6824 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6825 /* No conversions are required. */
6829 /* The type the function is declared to return. */
6830 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6832 /* First convert the value to the function's return type, then
6833 to the type of return value's location to handle the
6834 case that functype is thiner than the valtype. */
6835 retval = convert_for_initialization
6836 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6837 "return", NULL_TREE, 0);
6838 retval = convert (valtype, retval);
6840 /* If the conversion failed, treat this just like `return;'. */
6841 if (retval == error_mark_node)
6843 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6844 else if (! current_function_returns_struct
6845 && TREE_CODE (retval) == TARGET_EXPR
6846 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6847 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6848 TREE_OPERAND (retval, 0));
6850 maybe_warn_about_returning_address_of_local (retval);
6853 /* Actually copy the value returned into the appropriate location. */
6854 if (retval && retval != result)
6855 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6857 /* All done. Remember that this function did return a value. */
6858 current_function_returns_value = 1;
6862 /* Expand a C `return' statement.
6863 RETVAL is the expression for what to return,
6864 or a null pointer for `return;' with no value.
6866 C++: upon seeing a `return', we must call destructors on all
6867 variables in scope which had constructors called on them.
6868 This means that if in a destructor, the base class destructors
6869 must be called before returning.
6871 The RETURN statement in C++ has initialization semantics. */
6874 c_expand_return (retval)
6878 expand_null_return ();
6881 expand_start_target_temps ();
6882 expand_return (retval);
6883 expand_end_target_temps ();
6887 /* Start a C switch statement, testing expression EXP.
6888 Return EXP if it is valid, an error node otherwise. */
6891 c_expand_start_case (exp)
6896 exp = build_expr_type_conversion (WANT_INT | WANT_ENUM, exp, 1);
6897 if (exp == NULL_TREE)
6899 error ("switch quantity not an integer");
6900 exp = error_mark_node;
6902 if (exp == error_mark_node)
6903 return error_mark_node;
6905 exp = default_conversion (exp);
6906 type = TREE_TYPE (exp);
6907 idx = get_unwidened (exp, 0);
6908 /* We can't strip a conversion from a signed type to an unsigned,
6909 because if we did, int_fits_type_p would do the wrong thing
6910 when checking case values for being in range,
6911 and it's too hard to do the right thing. */
6912 if (TREE_UNSIGNED (TREE_TYPE (exp)) == TREE_UNSIGNED (TREE_TYPE (idx)))
6916 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
6917 type, "switch statement");
6922 /* Returns non-zero if the pointer-type FROM can be converted to the
6923 pointer-type TO via a qualification conversion. If CONSTP is -1,
6924 then we return non-zero if the pointers are similar, and the
6925 cv-qualification signature of FROM is a proper subset of that of TO.
6927 If CONSTP is positive, then all outer pointers have been
6931 comp_ptr_ttypes_real (to, from, constp)
6935 int to_more_cv_qualified = 0;
6937 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6939 if (TREE_CODE (to) != TREE_CODE (from))
6942 if (TREE_CODE (from) == OFFSET_TYPE
6943 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6944 TYPE_OFFSET_BASETYPE (to)))
6947 /* Const and volatile mean something different for function types,
6948 so the usual checks are not appropriate. */
6949 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6951 if (!at_least_as_qualified_p (to, from))
6954 if (!at_least_as_qualified_p (from, to))
6959 ++to_more_cv_qualified;
6963 constp &= TYPE_READONLY (to);
6966 if (TREE_CODE (to) != POINTER_TYPE)
6968 same_type_p (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from))
6969 && (constp >= 0 || to_more_cv_qualified);
6973 /* When comparing, say, char ** to char const **, this function takes the
6974 'char *' and 'char const *'. Do not pass non-pointer types to this
6978 comp_ptr_ttypes (to, from)
6981 return comp_ptr_ttypes_real (to, from, 1);
6984 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6985 type or inheritance-related types, regardless of cv-quals. */
6988 ptr_reasonably_similar (to, from)
6991 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6993 if (TREE_CODE (to) != TREE_CODE (from))
6996 if (TREE_CODE (from) == OFFSET_TYPE
6997 && comptypes (TYPE_OFFSET_BASETYPE (to),
6998 TYPE_OFFSET_BASETYPE (from),
6999 COMPARE_BASE | COMPARE_RELAXED))
7002 if (TREE_CODE (to) != POINTER_TYPE)
7004 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
7005 COMPARE_BASE | COMPARE_RELAXED);
7009 /* Like comp_ptr_ttypes, for const_cast. */
7012 comp_ptr_ttypes_const (to, from)
7015 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7017 if (TREE_CODE (to) != TREE_CODE (from))
7020 if (TREE_CODE (from) == OFFSET_TYPE
7021 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7022 TYPE_OFFSET_BASETYPE (to)))
7025 if (TREE_CODE (to) != POINTER_TYPE)
7026 return same_type_p (TYPE_MAIN_VARIANT (to),
7027 TYPE_MAIN_VARIANT (from));
7031 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7034 comp_ptr_ttypes_reinterpret (to, from)
7039 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7041 if (TREE_CODE (from) == OFFSET_TYPE)
7042 from = TREE_TYPE (from);
7043 if (TREE_CODE (to) == OFFSET_TYPE)
7044 to = TREE_TYPE (to);
7046 /* Const and volatile mean something different for function types,
7047 so the usual checks are not appropriate. */
7048 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7049 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7051 if (!at_least_as_qualified_p (to, from))
7055 && !at_least_as_qualified_p (from, to))
7057 constp &= TYPE_READONLY (to);
7060 if (TREE_CODE (from) != POINTER_TYPE
7061 || TREE_CODE (to) != POINTER_TYPE)
7066 /* Recursively examines the array elements of TYPE, until a non-array
7067 element type is found. */
7070 strip_array_types (type)
7073 while (TREE_CODE (type) == ARRAY_TYPE)
7074 type = TREE_TYPE (type);
7079 /* Returns the type-qualifier set corresponding to TYPE. */
7082 cp_type_quals (type)
7085 return TYPE_QUALS (strip_array_types (type));
7088 /* Returns non-zero if the TYPE contains a mutable member */
7091 cp_has_mutable_p (type)
7094 while (TREE_CODE (type) == ARRAY_TYPE)
7095 type = TREE_TYPE (type);
7097 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7100 /* Subroutine of casts_away_constness. Make T1 and T2 point at
7101 exemplar types such that casting T1 to T2 is casting away castness
7102 if and only if there is no implicit conversion from T1 to T2. */
7105 casts_away_constness_r (t1, t2)
7112 /* [expr.const.cast]
7114 For multi-level pointer to members and multi-level mixed pointers
7115 and pointers to members (conv.qual), the "member" aspect of a
7116 pointer to member level is ignored when determining if a const
7117 cv-qualifier has been cast away. */
7118 if (TYPE_PTRMEM_P (*t1))
7119 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
7120 if (TYPE_PTRMEM_P (*t2))
7121 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
7123 /* [expr.const.cast]
7125 For two pointer types:
7127 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
7128 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
7131 casting from X1 to X2 casts away constness if, for a non-pointer
7132 type T there does not exist an implicit conversion (clause
7135 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7139 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
7141 if (TREE_CODE (*t1) != POINTER_TYPE
7142 || TREE_CODE (*t2) != POINTER_TYPE)
7144 *t1 = cp_build_qualified_type (void_type_node,
7145 CP_TYPE_QUALS (*t1));
7146 *t2 = cp_build_qualified_type (void_type_node,
7147 CP_TYPE_QUALS (*t2));
7151 quals1 = CP_TYPE_QUALS (*t1);
7152 quals2 = CP_TYPE_QUALS (*t2);
7153 *t1 = TREE_TYPE (*t1);
7154 *t2 = TREE_TYPE (*t2);
7155 casts_away_constness_r (t1, t2);
7156 *t1 = build_pointer_type (*t1);
7157 *t2 = build_pointer_type (*t2);
7158 *t1 = cp_build_qualified_type (*t1, quals1);
7159 *t2 = cp_build_qualified_type (*t2, quals2);
7162 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
7166 casts_away_constness (t1, t2)
7170 if (TREE_CODE (t2) == REFERENCE_TYPE)
7172 /* [expr.const.cast]
7174 Casting from an lvalue of type T1 to an lvalue of type T2
7175 using a reference cast casts away constness if a cast from an
7176 rvalue of type "pointer to T1" to the type "pointer to T2"
7177 casts away constness. */
7178 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
7179 ? TREE_TYPE (t1) : t1);
7180 return casts_away_constness (build_pointer_type (t1),
7181 build_pointer_type (TREE_TYPE (t2)));
7184 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7185 /* [expr.const.cast]
7187 Casting from an rvalue of type "pointer to data member of X
7188 of type T1" to the type "pointer to data member of Y of type
7189 T2" casts away constness if a cast from an rvalue of type
7190 "poitner to T1" to the type "pointer to T2" casts away
7192 return casts_away_constness
7193 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7194 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7196 /* Casting away constness is only something that makes sense for
7197 pointer or reference types. */
7198 if (TREE_CODE (t1) != POINTER_TYPE
7199 || TREE_CODE (t2) != POINTER_TYPE)
7202 /* Top-level qualifiers don't matter. */
7203 t1 = TYPE_MAIN_VARIANT (t1);
7204 t2 = TYPE_MAIN_VARIANT (t2);
7205 casts_away_constness_r (&t1, &t2);
7206 if (!can_convert (t2, t1))