1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92, 93, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization.
27 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
28 and to process initializations in declarations (since they work
29 like a strange sort of assignment). */
32 extern void warning ();
41 int mark_addressable ();
42 static tree convert_for_assignment ();
43 /* static */ tree convert_for_initialization ();
45 static int self_promoting_args_p ();
46 int comp_target_types ();
47 extern tree shorten_compare ();
48 extern void binary_op_error ();
49 static tree pointer_int_sum ();
50 static tree pointer_diff ();
51 static tree convert_sequence ();
52 /* static */ tree unary_complex_lvalue ();
53 static void pedantic_lvalue_warning ();
54 tree truthvalue_conversion ();
56 extern rtx original_result_rtx;
58 /* Return the target type of TYPE, which meas return T for:
59 T*, T&, T[], T (...), and otherwise, just T. */
65 if (TREE_CODE (type) == REFERENCE_TYPE)
66 type = TREE_TYPE (type);
67 while (TREE_CODE (type) == POINTER_TYPE
68 || TREE_CODE (type) == ARRAY_TYPE
69 || TREE_CODE (type) == FUNCTION_TYPE
70 || TREE_CODE (type) == METHOD_TYPE
71 || TREE_CODE (type) == OFFSET_TYPE)
72 type = TREE_TYPE (type);
76 /* Do `exp = require_complete_type (exp);' to make sure exp
77 does not have an incomplete type. (That includes void types.) */
80 require_complete_type (value)
83 tree type = TREE_TYPE (value);
85 /* First, detect a valid value with a complete type. */
86 if (TYPE_SIZE (type) != 0
87 && type != void_type_node
88 && ! (TYPE_LANG_SPECIFIC (type)
89 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
90 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
93 /* If we see X::Y, we build an OFFSET_TYPE which has
94 not been laid out. Try to avoid an error by interpreting
95 it as this->X::Y, if reasonable. */
96 if (TREE_CODE (value) == OFFSET_REF
98 && TREE_OPERAND (value, 0) == C_C_D)
100 tree base, member = TREE_OPERAND (value, 1);
101 tree basetype = TYPE_OFFSET_BASETYPE (type);
102 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
103 base = convert_pointer_to (basetype, current_class_decl);
104 value = build (COMPONENT_REF, TREE_TYPE (member),
105 build_indirect_ref (base, NULL_PTR), member);
106 return require_complete_type (value);
109 incomplete_type_error (value, type);
110 return error_mark_node;
113 /* Return truthvalue of whether type of EXP is instantiated. */
118 return (TREE_CODE (exp) == TREE_LIST
119 || TREE_TYPE (exp) == unknown_type_node
120 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
121 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
124 /* Return truthvalue of whether T is function (or pfn) type. */
129 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
130 || (TREE_CODE (t) == POINTER_TYPE
131 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
132 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
135 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
136 does not have an uninstantiated type.
137 TYPE is type to instantiate with, if uninstantiated. */
139 require_instantiated_type (type, exp, errval)
140 tree type, exp, errval;
142 if (TREE_TYPE (exp) == NULL_TREE)
144 error ("argument list may not have an initializer list");
148 if (TREE_TYPE (exp) == unknown_type_node
149 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
150 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node))
152 exp = instantiate_type (type, exp, 1);
153 if (TREE_TYPE (exp) == error_mark_node)
159 /* Return a variant of TYPE which has all the type qualifiers of LIKE
160 as well as those of TYPE. */
163 qualify_type (type, like)
166 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
167 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
168 /* @@ Must do member pointers here. */
169 return build_type_variant (type, constflag, volflag);
172 /* Return the common type of two parameter lists.
173 We assume that comptypes has already been done and returned 1;
174 if that isn't so, this may crash.
176 As an optimization, free the space we allocate if the parameter
177 lists are already common. */
183 tree oldargs = p1, newargs, n;
186 char *first_obj = (char *) oballoc (0);
188 len = list_length (p1);
189 newargs = tree_last (p1);
191 if (newargs == void_list_node)
200 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
205 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
207 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
209 /* We used to give a warning here that advised about a default
210 argument being given in the prototype but not in the function's
211 declaration. It's best not to bother. */
212 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
215 else if (! TREE_PURPOSE (p1))
217 if (TREE_PURPOSE (p2))
219 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
225 int cmp = simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2));
227 my_friendly_abort (111);
230 error ("redeclaration of default argument %d", i+1);
233 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
235 if (TREE_VALUE (p1) != TREE_VALUE (p2))
238 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
241 TREE_VALUE (n) = TREE_VALUE (p1);
252 /* Return the common type of two types.
253 We assume that comptypes has already been done and returned 1;
254 if that isn't so, this may crash.
256 This is the type for the result of most arithmetic operations
257 if the operands have the given two types.
259 We do not deal with enumeral types here because they have already been
260 converted to integer types. */
266 register enum tree_code code1;
267 register enum tree_code code2;
269 /* Save time if the two types are the same. */
271 if (t1 == t2) return t1;
273 /* If one type is nonsense, use the other. */
274 if (t1 == error_mark_node)
276 if (t2 == error_mark_node)
279 /* Treat an enum type as the unsigned integer type of the same width. */
281 if (TREE_CODE (t1) == ENUMERAL_TYPE)
282 t1 = type_for_size (TYPE_PRECISION (t1), 1);
283 if (TREE_CODE (t2) == ENUMERAL_TYPE)
284 t2 = type_for_size (TYPE_PRECISION (t2), 1);
286 code1 = TREE_CODE (t1);
287 code2 = TREE_CODE (t2);
293 /* If only one is real, use it as the result. */
295 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
298 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
301 /* Both real or both integers; use the one with greater precision. */
303 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
305 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
308 /* Same precision. Prefer longs to ints even when same size. */
310 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
311 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
312 return long_unsigned_type_node;
314 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
315 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
317 /* But preserve unsignedness from the other type,
318 since long cannot hold all the values of an unsigned int. */
319 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
320 return long_unsigned_type_node;
321 return long_integer_type_node;
324 /* Otherwise prefer the unsigned one. */
326 if (TREE_UNSIGNED (t1))
332 /* For two pointers, do this recursively on the target type,
333 and combine the qualifiers of the two types' targets. */
334 /* This code was turned off; I don't know why.
335 But ANSI C++ specifies doing this with the qualifiers.
336 So I turned it on again. */
338 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
339 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
341 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
343 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
344 target = build_type_variant (target, constp, volatilep);
345 if (code1 == POINTER_TYPE)
346 return build_pointer_type (target);
348 return build_reference_type (target);
352 return build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
355 return build_reference_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
360 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
361 /* Save space: see if the result is identical to one of the args. */
362 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
364 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
366 /* Merge the element types, and have a size if either arg has one. */
367 return build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
371 /* Function types: prefer the one that specified arg types.
372 If both do, merge the arg types. Also merge the return types. */
374 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
375 tree p1 = TYPE_ARG_TYPES (t1);
376 tree p2 = TYPE_ARG_TYPES (t2);
379 /* Save space: see if the result is identical to one of the args. */
380 if (valtype == TREE_TYPE (t1) && ! p2)
382 if (valtype == TREE_TYPE (t2) && ! p1)
385 /* Simple way if one arg fails to specify argument types. */
386 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
388 rval = build_function_type (valtype, p2);
389 if (raises = TYPE_RAISES_EXCEPTIONS (t2))
390 rval = build_exception_variant (NULL_TREE, rval, raises);
393 raises = TYPE_RAISES_EXCEPTIONS (t1);
394 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
396 rval = build_function_type (valtype, p1);
398 rval = build_exception_variant (NULL_TREE, rval, raises);
402 rval = build_function_type (valtype, commonparms (p1, p2));
403 return build_exception_variant (NULL_TREE, rval, raises);
408 my_friendly_assert (TYPE_MAIN_VARIANT (t1) == t1
409 && TYPE_MAIN_VARIANT (t2) == t2, 306);
411 if (binfo_or_else (t1, t2))
413 compiler_error ("common_type called with uncommon aggregate types");
417 if (TYPE_METHOD_BASETYPE (t1) == TYPE_METHOD_BASETYPE (t2)
418 && TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
420 /* Get this value the long way, since TYPE_METHOD_BASETYPE
421 is just the main variant of this. */
422 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
425 raises = TYPE_RAISES_EXCEPTIONS (t1);
427 /* If this was a member function type, get back to the
428 original type of type member function (i.e., without
429 the class instance variable up front. */
430 t1 = build_function_type (TREE_TYPE (t1), TREE_CHAIN (TYPE_ARG_TYPES (t1)));
431 t2 = build_function_type (TREE_TYPE (t2), TREE_CHAIN (TYPE_ARG_TYPES (t2)));
432 t3 = common_type (t1, t2);
433 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3), TYPE_ARG_TYPES (t3));
434 return build_exception_variant (basetype, t3, raises);
436 compiler_error ("common_type called with uncommon method types");
440 if (TYPE_OFFSET_BASETYPE (t1) == TYPE_OFFSET_BASETYPE (t2)
441 && TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
443 tree basetype = TYPE_OFFSET_BASETYPE (t1);
444 return build_offset_type (basetype,
445 common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
447 compiler_error ("common_type called with uncommon member types");
455 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
457 compexcepttypes (t1, t2, strict)
461 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
465 comp_array_types (cmp, t1, t2, strict)
466 register int (*cmp)();
470 tree d1 = TYPE_DOMAIN (t1);
471 tree d2 = TYPE_DOMAIN (t2);
473 /* Target types must match incl. qualifiers. */
474 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
475 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), strict)))
478 /* Sizes must match unless one is missing or variable. */
479 if (d1 == 0 || d2 == 0 || d1 == d2
480 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
481 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
482 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
483 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
486 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
487 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
488 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
489 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
490 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
491 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
492 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
493 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
496 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
497 or various other operations. This is what ANSI C++ speaks of as
500 For C++: argument STRICT says we should be strict about this
503 2 : strict, except that if one type is a reference and
504 the other is not, compare the target type of the
505 reference to the type that's not a reference (ARM, p308).
506 1 : strict (compared according to ANSI C)
507 0 : <= (compared according to C++)
508 -1: <= or >= (relaxed)
510 Otherwise, pointers involving base classes and derived classes
511 can be mixed as legal: i.e. a pointer to a base class may be assigned
512 to a pointer to one of its derived classes, as per C++. A pointer to
513 a derived class may be passed as a parameter to a function expecting a
514 pointer to a base classes. These allowances do not commute. In this
515 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
516 be the derived class. */
518 comptypes (type1, type2, strict)
522 register tree t1 = type1;
523 register tree t2 = type2;
525 /* Suppress errors caused by previously reported errors */
530 /* This should never happen. */
531 my_friendly_assert (t1 != error_mark_node, 307);
533 if (t2 == error_mark_node)
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);
549 /* Different classes of types can't be compatible. */
551 if (TREE_CODE (t1) != TREE_CODE (t2))
554 && ((TREE_CODE (t1) == REFERENCE_TYPE)
555 ^ (TREE_CODE (t2) == REFERENCE_TYPE)))
557 if (TREE_CODE (t1) == REFERENCE_TYPE)
558 return comptypes (TREE_TYPE (t1), t2, 1);
559 return comptypes (t1, TREE_TYPE (t2), 1);
567 /* Qualifiers must match. */
569 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
571 if (TREE_THIS_VOLATILE (t1) != TREE_THIS_VOLATILE (t2))
574 /* Allow for two different type nodes which have essentially the same
575 definition. Note that we already checked for equality of the type
576 type qualifiers (just above). */
578 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
581 switch (TREE_CODE (t1))
592 return (comptypes (TYPE_POINTER_TO (TYPE_OFFSET_BASETYPE (t1)),
593 TYPE_POINTER_TO (TYPE_OFFSET_BASETYPE (t2)), strict)
594 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
597 if (! compexcepttypes (t1, t2, strict))
600 /* This case is anti-symmetrical!
601 One can pass a base member (or member function)
602 to something expecting a derived member (or member function),
603 but not vice-versa! */
605 return (comptypes (TYPE_POINTER_TO (TYPE_METHOD_BASETYPE (t2)),
606 TYPE_POINTER_TO (TYPE_METHOD_BASETYPE (t1)), strict)
607 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
608 && compparms (TREE_CHAIN (TYPE_ARG_TYPES (t1)),
609 TREE_CHAIN (TYPE_ARG_TYPES(t2)), strict));
618 if (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE)
622 rval = t1 == t2 || UNIQUELY_DERIVED_FROM_P (t1, t2);
627 return UNIQUELY_DERIVED_FROM_P (t2, t1);
632 return comptypes (t1, t2, strict);
635 if (! compexcepttypes (t1, t2, strict))
638 return ((TREE_TYPE (t1) == TREE_TYPE (t2)
639 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
640 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2), strict));
643 /* Target types must match incl. qualifiers. */
644 return comp_array_types (comptypes, t1, t2, strict);
646 case TEMPLATE_TYPE_PARM:
652 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
653 ignoring their qualifiers.
655 NPTRS is the number of pointers we can strip off and keep cool.
656 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
657 but to not permit B** to convert to A**. */
660 comp_target_types (ttl, ttr, nptrs)
664 ttl = TYPE_MAIN_VARIANT (ttl);
665 ttr = TYPE_MAIN_VARIANT (ttr);
669 if (TREE_CODE (ttr) != TREE_CODE (ttl))
672 if (TREE_CODE (ttr) == POINTER_TYPE)
673 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs - 1);
675 if (TREE_CODE (ttr) == REFERENCE_TYPE)
676 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
677 if (TREE_CODE (ttr) == ARRAY_TYPE)
678 return comp_array_types (comp_target_types, ttl, ttr, 0);
679 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
680 if (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
681 switch (comp_target_parms (TYPE_ARG_TYPES (ttl), TYPE_ARG_TYPES (ttr), 0))
688 warning ("contravariance violation for method types ignored");
691 my_friendly_abort (112);
697 else if (TREE_CODE (ttr) == OFFSET_TYPE)
699 /* Contravariance: we can assign a pointer to base member to a pointer
700 to derived member. Note difference from simple pointer case, where
701 we can pass a pointer to derived to a pointer to base. */
702 if (comptypes (TYPE_OFFSET_BASETYPE (ttr), TYPE_OFFSET_BASETYPE (ttl), 0))
703 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
704 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl), TYPE_OFFSET_BASETYPE (ttr), 0)
705 && comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
707 warning ("contravariance violation for member types ignored");
711 else if (IS_AGGR_TYPE (ttl))
715 return comptypes (TYPE_POINTER_TO (ttl), TYPE_POINTER_TO (ttr), 0);
721 /* If two types share a common base type, return that basetype.
722 If there is not a unique most-derived base type, this function
723 returns ERROR_MARK_NODE. */
725 common_base_type (tt1, tt2)
728 tree best = NULL_TREE, tmp;
731 /* If one is a baseclass of another, that's good enough. */
732 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
734 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
737 /* If they share a virtual baseclass, that's good enough. */
738 for (tmp = CLASSTYPE_VBASECLASSES (tt1); tmp; tmp = TREE_CHAIN (tmp))
740 if (binfo_member (BINFO_TYPE (tmp), CLASSTYPE_VBASECLASSES (tt2)))
741 return BINFO_TYPE (tmp);
744 /* Otherwise, try to find a unique baseclass of TT1
745 that is shared by TT2, and follow that down. */
746 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
748 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
749 tree trial = common_base_type (basetype, tt2);
752 if (trial == error_mark_node)
754 if (best == NULL_TREE)
756 else if (best != trial)
757 return error_mark_node;
762 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
764 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
765 tree trial = common_base_type (tt1, basetype);
768 if (trial == error_mark_node)
770 if (best == NULL_TREE)
772 else if (best != trial)
773 return error_mark_node;
779 /* Subroutines of `comptypes'. */
781 /* Return 1 if two parameter type lists PARMS1 and PARMS2
782 are equivalent in the sense that functions with those parameter types
783 can have equivalent types.
784 If either list is empty, we win.
785 Otherwise, the two lists must be equivalent, element by element.
787 C++: See comment above about TYPE1, TYPE2, STRICT.
788 If STRICT == 3, it means checking is strict, but do not compare
789 default parameter values. */
791 compparms (parms1, parms2, strict)
795 register tree t1 = parms1, t2 = parms2;
797 /* An unspecified parmlist matches any specified parmlist
798 whose argument types don't need default promotions. */
801 return self_promoting_args_p (t2);
803 return self_promoting_args_p (t1);
807 if (t1 == 0 && t2 == 0)
809 /* If one parmlist is shorter than the other,
810 they fail to match, unless STRICT is <= 0. */
811 if (t1 == 0 || t2 == 0)
818 return t1 && TREE_PURPOSE (t1);
820 if (! comptypes (TREE_VALUE (t2), TREE_VALUE (t1), strict))
825 return t2 == void_list_node && TREE_PURPOSE (t1);
826 return TREE_PURPOSE (t1) || TREE_PURPOSE (t2);
828 if (strict != 3 && TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
830 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
832 my_friendly_abort (113);
837 t1 = TREE_CHAIN (t1);
838 t2 = TREE_CHAIN (t2);
842 /* This really wants return whether or not parameter type lists
843 would make their owning functions assignment compatible or not. */
845 comp_target_parms (parms1, parms2, strict)
849 register tree t1 = parms1, t2 = parms2;
850 int warn_contravariance = 0;
852 /* An unspecified parmlist matches any specified parmlist
853 whose argument types don't need default promotions.
854 @@@ see 13.3.3 for a counterexample... */
856 if (t1 == 0 && t2 != 0)
858 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
860 return self_promoting_args_p (t2);
863 return self_promoting_args_p (t1);
865 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
869 /* If one parmlist is shorter than the other,
870 they fail to match, unless STRICT is <= 0. */
871 if (t1 == 0 || t2 == 0)
876 return 1 + warn_contravariance;
877 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
879 p1 = TREE_VALUE (t1);
880 p2 = TREE_VALUE (t2);
883 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
884 || (TREE_CODE (p1) == REFERENCE_TYPE && TREE_CODE (p2) == REFERENCE_TYPE))
887 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
888 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
891 /* The following is wrong for contravariance,
892 but many programs depend on it. */
893 if (TREE_TYPE (p1) == void_type_node)
895 if (TREE_TYPE (p2) == void_type_node)
897 warn_contravariance = 1;
900 if (IS_AGGR_TYPE (TREE_TYPE (p1)))
902 if (comptypes (p2, p1, 0) == 0)
904 if (comptypes (p1, p2, 0) != 0)
905 warn_contravariance = 1;
912 /* Note backwards order due to contravariance. */
913 if (comp_target_types (p2, p1, 1) == 0)
915 if (comp_target_types (p1, p2, 1))
917 warn_contravariance = 1;
923 /* What good do these cases do? */
925 return p2 == void_type_node && TREE_PURPOSE (t1);
926 return TREE_PURPOSE (t1) || TREE_PURPOSE (t2);
929 /* Target types are compatible--just make sure that if
930 we use parameter lists, that they are ok as well. */
931 if (TREE_CODE (p1) == FUNCTION_TYPE || TREE_CODE (p1) == METHOD_TYPE)
932 switch (comp_target_parms (TYPE_ARG_TYPES (p1),
941 warn_contravariance = 1;
944 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
946 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
948 my_friendly_abort (114);
953 return 1 + warn_contravariance;
956 /* Return 1 if PARMS specifies a fixed number of parameters
957 and none of their types is affected by default promotions. */
960 self_promoting_args_p (parms)
964 for (t = parms; t; t = TREE_CHAIN (t))
966 register tree type = TREE_VALUE (t);
968 if (TREE_CHAIN (t) == 0 && type != void_type_node)
971 if (TYPE_MAIN_VARIANT (type) == float_type_node)
977 if (C_PROMOTING_INTEGER_TYPE_P (type))
983 /* Return an unsigned type the same as TYPE in other respects.
985 C++: must make these work for type variants as well. */
991 tree type1 = TYPE_MAIN_VARIANT (type);
992 if (type1 == signed_char_type_node || type1 == char_type_node)
993 return unsigned_char_type_node;
994 if (type1 == integer_type_node)
995 return unsigned_type_node;
996 if (type1 == short_integer_type_node)
997 return short_unsigned_type_node;
998 if (type1 == long_integer_type_node)
999 return long_unsigned_type_node;
1000 if (type1 == long_long_integer_type_node)
1001 return long_long_unsigned_type_node;
1005 /* Return a signed type the same as TYPE in other respects. */
1011 tree type1 = TYPE_MAIN_VARIANT (type);
1012 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1013 return signed_char_type_node;
1014 if (type1 == unsigned_type_node)
1015 return integer_type_node;
1016 if (type1 == short_unsigned_type_node)
1017 return short_integer_type_node;
1018 if (type1 == long_unsigned_type_node)
1019 return long_integer_type_node;
1020 if (type1 == long_long_unsigned_type_node)
1021 return long_long_integer_type_node;
1025 /* Return a type the same as TYPE except unsigned or
1026 signed according to UNSIGNEDP. */
1029 signed_or_unsigned_type (unsignedp, type)
1033 if (TREE_CODE (type) != INTEGER_TYPE)
1035 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1036 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1037 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1038 return unsignedp ? unsigned_type_node : integer_type_node;
1039 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1040 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1041 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1042 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1043 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1044 return (unsignedp ? long_long_unsigned_type_node
1045 : long_long_integer_type_node);
1053 enum tree_code code = TREE_CODE (type);
1056 if (code == FUNCTION_TYPE)
1058 if (pedantic || warn_pointer_arith)
1059 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1060 return size_int (1);
1062 if (code == METHOD_TYPE)
1064 if (pedantic || warn_pointer_arith)
1065 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1066 return size_int (1);
1068 if (code == VOID_TYPE)
1070 if (pedantic || warn_pointer_arith)
1071 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1072 return size_int (1);
1074 if (code == ERROR_MARK)
1075 return size_int (1);
1077 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1078 referenced object.'' */
1079 if (code == REFERENCE_TYPE)
1080 type = TREE_TYPE (type);
1082 /* We couldn't find anything in the ARM or the draft standard that says,
1083 one way or the other, if doing sizeof on something that doesn't have
1084 an object associated with it is correct or incorrect. For example, if
1085 you declare `struct S { char str[16]; };', and in your program do
1086 a `sizeof (S::str)', should we flag that as an error or should we give
1087 the size of it? Since it seems like a reasonable thing to do, we'll go
1088 with giving the value. */
1089 if (code == OFFSET_TYPE)
1090 type = TREE_TYPE (type);
1092 /* @@ This also produces an error for a signature ref.
1093 In that case we should be able to do better. */
1094 if (IS_SIGNATURE (type))
1096 error ("`sizeof' applied to a signature type");
1097 return size_int (0);
1100 if (TYPE_SIZE (type) == 0)
1102 error ("`sizeof' applied to an incomplete type");
1103 return size_int (0);
1106 /* Convert in case a char is more than one unit. */
1107 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1108 size_int (TYPE_PRECISION (char_type_node)));
1109 /* size_binop does not put the constant in range, so do it now. */
1110 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1111 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1116 c_sizeof_nowarn (type)
1119 enum tree_code code = TREE_CODE (type);
1122 if (code == FUNCTION_TYPE
1123 || code == METHOD_TYPE
1124 || code == VOID_TYPE
1125 || code == ERROR_MARK)
1126 return size_int (1);
1127 if (code == REFERENCE_TYPE)
1128 type = TREE_TYPE (type);
1130 if (TYPE_SIZE (type) == 0)
1132 /* ??? Tiemann, why have any diagnostic here?
1133 There is none in the corresponding function for C. */
1134 warning ("sizeof applied to an incomplete type");
1135 return size_int (0);
1138 /* Convert in case a char is more than one unit. */
1139 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1140 size_int (TYPE_PRECISION (char_type_node)));
1141 force_fit_type (t, 0);
1145 /* Implement the __alignof keyword: Return the minimum required
1146 alignment of TYPE, measured in bytes. */
1152 enum tree_code code = TREE_CODE (type);
1155 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1156 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1158 if (code == VOID_TYPE || code == ERROR_MARK)
1159 return size_int (1);
1161 /* C++: this is really correct! */
1162 if (code == REFERENCE_TYPE)
1163 type = TREE_TYPE (type);
1165 /* @@ This also produces an error for a signature ref.
1166 In that case we should be able to do better. */
1167 if (IS_SIGNATURE (type))
1169 error ("`__alignof' applied to a signature type");
1170 return size_int (1);
1173 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1174 force_fit_type (t, 0);
1178 /* Perform default promotions for C data used in expressions.
1179 Arrays and functions are converted to pointers;
1180 enumeral types or short or char, to int.
1181 In addition, manifest constants symbols are replaced by their values.
1183 C++: this will automatically bash references to their target type. */
1186 default_conversion (exp)
1189 register tree type = TREE_TYPE (exp);
1190 register enum tree_code code = TREE_CODE (type);
1192 if (code == OFFSET_TYPE /* || TREE_CODE (exp) == OFFSET_REF */ )
1194 if (TREE_CODE (exp) == OFFSET_REF)
1195 return default_conversion (resolve_offset_ref (exp));
1197 type = TREE_TYPE (type);
1198 code = TREE_CODE (type);
1201 if (code == REFERENCE_TYPE)
1203 exp = convert_from_reference (exp);
1204 type = TREE_TYPE (exp);
1205 code = TREE_CODE (type);
1208 /* Constants can be used directly unless they're not loadable. */
1209 if (TREE_CODE (exp) == CONST_DECL)
1210 exp = DECL_INITIAL (exp);
1211 /* Replace a nonvolatile const static variable with its value. */
1212 else if (TREE_READONLY_DECL_P (exp) && DECL_MODE (exp) != BLKmode)
1214 exp = decl_constant_value (exp);
1215 type = TREE_TYPE (exp);
1218 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1219 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1221 /* Normally convert enums to int,
1222 but convert wide enums to something wider. */
1223 if (code == ENUMERAL_TYPE)
1225 type = type_for_size (MAX (TYPE_PRECISION (type),
1226 TYPE_PRECISION (integer_type_node)),
1228 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
1229 && TREE_UNSIGNED (type)));
1230 return convert (type, exp);
1233 if (C_PROMOTING_INTEGER_TYPE_P (type))
1235 /* Traditionally, unsignedness is preserved in default promotions.
1236 Otherwise, retain unsignedness if really not getting bigger. */
1237 if (TREE_UNSIGNED (type)
1238 && (flag_traditional
1239 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1240 return convert (unsigned_type_node, exp);
1241 return convert (integer_type_node, exp);
1243 if (flag_traditional
1244 && TYPE_MAIN_VARIANT (type) == float_type_node)
1245 return convert (double_type_node, exp);
1246 if (code == VOID_TYPE)
1248 error ("void value not ignored as it ought to be");
1249 return error_mark_node;
1251 if (code == FUNCTION_TYPE)
1253 return build_unary_op (ADDR_EXPR, exp, 0);
1255 if (code == METHOD_TYPE)
1257 if (TREE_CODE (exp) == OFFSET_REF)
1259 my_friendly_assert (TREE_CODE (TREE_OPERAND (exp, 1)) == FUNCTION_DECL,
1261 return build_unary_op (ADDR_EXPR, TREE_OPERAND (exp, 1), 0);
1263 return build_unary_op (ADDR_EXPR, exp, 0);
1265 if (code == ARRAY_TYPE)
1270 int constp, volatilep;
1272 if (TREE_CODE (exp) == INDIRECT_REF)
1274 /* Stripping away the INDIRECT_REF is not the right
1275 thing to do for references... */
1276 tree inner = TREE_OPERAND (exp, 0);
1277 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1279 inner = build1 (CONVERT_EXPR,
1280 build_pointer_type (TREE_TYPE (TREE_TYPE (inner))),
1282 TREE_REFERENCE_EXPR (inner) = 1;
1284 return convert (TYPE_POINTER_TO (TREE_TYPE (type)), inner);
1287 if (TREE_CODE (exp) == COMPOUND_EXPR)
1289 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1290 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1291 TREE_OPERAND (exp, 0), op1);
1295 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1297 error ("invalid use of non-lvalue array");
1298 return error_mark_node;
1301 constp = volatilep = 0;
1302 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1303 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1305 constp = TREE_READONLY (exp);
1306 volatilep = TREE_THIS_VOLATILE (exp);
1309 restype = TREE_TYPE (type);
1310 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1311 || constp || volatilep)
1312 restype = build_type_variant (restype,
1313 TYPE_READONLY (type) || constp,
1314 TYPE_VOLATILE (type) || volatilep);
1315 ptrtype = build_pointer_type (restype);
1317 if (TREE_CODE (exp) == VAR_DECL)
1319 /* ??? This is not really quite correct
1320 in that the type of the operand of ADDR_EXPR
1321 is not the target type of the type of the ADDR_EXPR itself.
1322 Question is, can this lossage be avoided? */
1323 adr = build1 (ADDR_EXPR, ptrtype, exp);
1324 if (mark_addressable (exp) == 0)
1325 return error_mark_node;
1326 TREE_CONSTANT (adr) = staticp (exp);
1327 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1330 /* This way is better for a COMPONENT_REF since it can
1331 simplify the offset for a component. */
1332 adr = build_unary_op (ADDR_EXPR, exp, 1);
1333 return convert (ptrtype, adr);
1339 build_object_ref (datum, basetype, field)
1340 tree datum, basetype, field;
1342 if (datum == error_mark_node)
1343 return error_mark_node;
1344 else if (IS_SIGNATURE (IDENTIFIER_TYPE_VALUE (basetype)))
1346 warning ("signature name in scope resolution ignored");
1347 return build_component_ref (datum, field, NULL_TREE, 1);
1349 else if (is_aggr_typedef (basetype, 1))
1351 tree real_basetype = IDENTIFIER_TYPE_VALUE (basetype);
1352 if (binfo_or_else (real_basetype, TREE_TYPE (datum)))
1353 return build_component_ref (build_scoped_ref (datum, basetype),
1354 field, NULL_TREE, 1);
1356 return error_mark_node;
1359 /* Like `build_component_ref, but uses an already found field.
1360 Must compute access for C_C_D. Otherwise, ok. */
1362 build_component_ref_1 (datum, field, protect)
1366 register tree basetype = TREE_TYPE (datum);
1367 register enum tree_code code = TREE_CODE (basetype);
1370 if (code == REFERENCE_TYPE)
1372 datum = convert_from_reference (datum);
1373 basetype = TREE_TYPE (datum);
1374 code = TREE_CODE (basetype);
1377 if (! IS_AGGR_TYPE_CODE (code))
1379 if (code != ERROR_MARK)
1380 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1381 field, datum, basetype);
1382 return error_mark_node;
1385 if (TYPE_SIZE (basetype) == 0)
1387 incomplete_type_error (0, basetype);
1388 return error_mark_node;
1391 /* Look up component name in the structure type definition. */
1393 if (field == error_mark_node)
1394 my_friendly_abort (115);
1396 if (TREE_STATIC (field))
1401 enum access_type access
1402 = compute_access (TYPE_BINFO (current_class_type), field);
1404 if (access == access_private)
1406 cp_error ("field `%D' is private", field);
1407 return error_mark_node;
1409 else if (access == access_protected)
1411 cp_error ("field `%D' is protected", field);
1412 return error_mark_node;
1416 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1418 if (TREE_READONLY (datum) || TREE_READONLY (field))
1419 TREE_READONLY (ref) = 1;
1420 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1421 TREE_THIS_VOLATILE (ref) = 1;
1422 if (DECL_MUTABLE_P (field))
1423 TREE_READONLY (ref) = 0;
1428 /* Given a COND_EXPR in T, return it in a form that we can, for
1429 example, use as an lvalue. This code used to be in unary_complex_lvalue,
1430 but we needed it to deal with `a = (d == c) ? b : c' expressions, where
1431 we're dealing with aggregates. So, we now call this in unary_complex_lvalue,
1432 and in build_modify_expr. The case (in particular) that led to this was
1433 with CODE == ADDR_EXPR, since it's not an lvalue when we'd get it there. */
1435 rationalize_conditional_expr (code, t)
1436 enum tree_code code;
1440 build_conditional_expr (TREE_OPERAND (t, 0),
1441 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1442 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1446 build_component_ref (datum, component, basetype_path, protect)
1447 tree datum, component, basetype_path;
1450 register tree basetype = TREE_TYPE (datum);
1451 register enum tree_code code = TREE_CODE (basetype);
1452 register tree field = NULL;
1455 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1456 unless we are not to support things not strictly ANSI. */
1457 switch (TREE_CODE (datum))
1461 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1462 basetype_path, protect);
1463 return build (COMPOUND_EXPR, TREE_TYPE (value),
1464 TREE_OPERAND (datum, 0), value);
1467 return build_conditional_expr
1468 (TREE_OPERAND (datum, 0),
1469 build_component_ref (TREE_OPERAND (datum, 1), component,
1470 basetype_path, protect),
1471 build_component_ref (TREE_OPERAND (datum, 2), component,
1472 basetype_path, protect));
1475 if (code == REFERENCE_TYPE)
1478 /* TREE_REFERENCE_EXPRs are not converted by `convert_from_reference'.
1479 @@ Maybe that is not right. */
1480 if (TREE_REFERENCE_EXPR (datum))
1481 datum = build1 (INDIRECT_REF, TREE_TYPE (basetype), datum);
1484 datum = convert_from_reference (datum);
1485 basetype = TREE_TYPE (datum);
1486 code = TREE_CODE (basetype);
1489 /* First, see if there is a field or component with name COMPONENT. */
1490 if (TREE_CODE (component) == TREE_LIST)
1492 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
1493 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
1494 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
1496 if (TREE_CODE (component) == TYPE_EXPR)
1497 return build_component_type_expr (datum, component, NULL_TREE, protect);
1499 if (! IS_AGGR_TYPE_CODE (code))
1501 if (code != ERROR_MARK)
1502 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1503 component, datum, basetype);
1504 return error_mark_node;
1507 if (TYPE_SIZE (basetype) == 0)
1509 incomplete_type_error (0, basetype);
1510 return error_mark_node;
1513 if (TREE_CODE (component) == BIT_NOT_EXPR)
1515 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
1517 cp_error ("destructor specifier `%T::~%T' must have matching names",
1518 basetype, TREE_OPERAND (component, 0));
1519 return error_mark_node;
1521 if (! TYPE_HAS_DESTRUCTOR (basetype))
1523 cp_error ("type `%T' has no destructor", basetype);
1524 return error_mark_node;
1526 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
1529 /* Look up component name in the structure type definition. */
1530 if (CLASSTYPE_VFIELD (basetype)
1531 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
1532 /* Special-case this because if we use normal lookups in an ambiguous
1533 hierarchy, the compiler will abort (because vptr lookups are
1534 not supposed to be ambiguous. */
1535 field = CLASSTYPE_VFIELD (basetype);
1538 if (basetype_path == NULL_TREE)
1539 basetype_path = TYPE_BINFO (basetype);
1540 field = lookup_field (basetype_path, component,
1541 protect && ! VFIELD_NAME_P (component), 0);
1542 if (field == error_mark_node)
1543 return error_mark_node;
1545 if (field == NULL_TREE)
1547 /* Not found as a data field, look for it as a method. If found,
1548 then if this is the only possible one, return it, else
1549 report ambiguity error. */
1550 tree fndecls = lookup_fnfields (basetype_path, component, 1);
1551 if (fndecls == error_mark_node)
1552 return error_mark_node;
1555 if (TREE_CHAIN (fndecls) == NULL_TREE
1556 && DECL_CHAIN (TREE_VALUE (fndecls)) == NULL_TREE)
1558 enum access_type access;
1561 /* Unique, so use this one now. */
1562 basetype = TREE_PURPOSE (fndecls);
1563 fndecl = TREE_VALUE (fndecls);
1564 access = compute_access (TREE_PURPOSE (fndecls), fndecl);
1565 if (access == access_public)
1567 if (DECL_VINDEX (fndecl)
1568 && ! resolves_to_fixed_type_p (datum, 0))
1570 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1571 addr = convert_pointer_to (DECL_CONTEXT (fndecl), addr);
1572 datum = build_indirect_ref (addr, NULL_PTR);
1573 my_friendly_assert (datum != error_mark_node, 310);
1574 fndecl = build_vfn_ref (&addr, datum, DECL_VINDEX (fndecl));
1578 if (access == access_protected)
1579 cp_error ("member function `%D' is protected", fndecl);
1581 cp_error ("member function `%D' is private", fndecl);
1582 return error_mark_node;
1585 return build (COMPONENT_REF, unknown_type_node, datum, fndecls);
1588 if (component == ansi_opname[(int) TYPE_EXPR])
1589 cp_error ("`%#T' has no such type conversion operator", basetype);
1591 cp_error ("`%#T' has no member named `%D'", basetype, component);
1592 return error_mark_node;
1594 else if (TREE_TYPE (field) == error_mark_node)
1595 return error_mark_node;
1597 if (TREE_CODE (field) != FIELD_DECL)
1599 if (TREE_CODE (field) == TYPE_DECL)
1601 cp_error ("invalid use of type decl `%#D' as expression", field);
1602 return error_mark_node;
1604 if (DECL_RTL (field) != 0)
1605 assemble_external (field);
1606 TREE_USED (field) = 1;
1611 if (DECL_FIELD_CONTEXT (field) != basetype
1612 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
1614 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1615 if (integer_zerop (addr))
1617 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1618 return error_mark_node;
1620 addr = convert_pointer_to (DECL_FIELD_CONTEXT (field), addr);
1621 datum = build_indirect_ref (addr, NULL_PTR);
1622 my_friendly_assert (datum != error_mark_node, 311);
1624 ref = build (COMPONENT_REF, TREE_TYPE (field), break_out_cleanups (datum), field);
1626 if (TREE_READONLY (datum) || TREE_READONLY (field))
1627 TREE_READONLY (ref) = 1;
1628 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1629 TREE_THIS_VOLATILE (ref) = 1;
1630 if (DECL_MUTABLE_P (field))
1631 TREE_READONLY (ref) = 0;
1636 /* Given an expression PTR for a pointer, return an expression
1637 for the value pointed to.
1638 ERRORSTRING is the name of the operator to appear in error messages.
1640 This function may need to overload OPERATOR_FNNAME.
1641 Must also handle REFERENCE_TYPEs for C++. */
1644 build_x_indirect_ref (ptr, errorstring)
1648 tree rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE, NULL_TREE);
1651 return build_indirect_ref (ptr, errorstring);
1655 build_indirect_ref (ptr, errorstring)
1659 register tree pointer = default_conversion (ptr);
1660 register tree type = TREE_TYPE (pointer);
1662 if (ptr == current_class_decl)
1665 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
1667 if (TREE_CODE (pointer) == ADDR_EXPR
1668 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1669 == TREE_TYPE (type)))
1670 return TREE_OPERAND (pointer, 0);
1673 tree t = TREE_TYPE (type);
1674 register tree ref = build1 (INDIRECT_REF,
1675 TYPE_MAIN_VARIANT (t), pointer);
1677 TREE_READONLY (ref) = TYPE_READONLY (t);
1678 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1679 TREE_SIDE_EFFECTS (ref)
1680 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1684 /* `pointer' won't be an error_mark_node if we were given a
1685 pointer to member, so it's cool to check for this here. */
1686 else if (TYPE_PTRMEMFUNC_P (type))
1687 error ("invalid use of `%s' on pointer to member function", errorstring);
1688 else if (TREE_CODE (type) == RECORD_TYPE
1689 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1690 error ("cannot dereference signature pointer/reference");
1691 else if (pointer != error_mark_node)
1694 error ("invalid type argument of `%s'", errorstring);
1696 error ("invalid type argument");
1698 return error_mark_node;
1701 /* This handles expressions of the form "a[i]", which denotes
1704 This is logically equivalent in C to *(a+i), but we may do it differently.
1705 If A is a variable or a member, we generate a primitive ARRAY_REF.
1706 This avoids forcing the array out of registers, and can work on
1707 arrays that are not lvalues (for example, members of structures returned
1710 If INDEX is of some user-defined type, it must be converted to
1711 integer type. Otherwise, to make a compatible PLUS_EXPR, it
1712 will inherit the type of the array, which will be some pointer type. */
1715 build_x_array_ref (array, index)
1718 tree rval = build_opfncall (ARRAY_REF, LOOKUP_NORMAL, array, index, NULL_TREE);
1721 return build_array_ref (array, index);
1725 build_array_ref (array, idx)
1732 error ("subscript missing in array reference");
1733 return error_mark_node;
1736 if (TREE_TYPE (array) == error_mark_node
1737 || TREE_TYPE (idx) == error_mark_node)
1738 return error_mark_node;
1740 itype = TREE_TYPE (idx);
1741 /* We must check here for the reference, so we can do the possible
1742 conversions immediately afterwards. */
1743 if (TREE_CODE (itype) == REFERENCE_TYPE)
1745 idx = convert_from_reference (idx);
1746 itype = TREE_TYPE (idx);
1749 if (IS_AGGR_TYPE (itype))
1751 if (TYPE_HAS_INT_CONVERSION (itype))
1752 idx = build_type_conversion (CONVERT_EXPR,
1753 integer_type_node, idx, 1);
1756 error_with_aggr_type (itype,
1757 "type `%s' requires integer conversion for array indexing");
1758 return error_mark_node;
1762 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1763 && TREE_CODE (array) != INDIRECT_REF)
1767 /* Subscripting with type char is likely to lose
1768 on a machine where chars are signed.
1769 So warn on any machine, but optionally.
1770 Don't warn for unsigned char since that type is safe.
1771 Don't warn for signed char because anyone who uses that
1772 must have done so deliberately. */
1773 if (warn_char_subscripts
1774 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
1775 warning ("array subscript has type `char'");
1777 /* Apply default promotions *after* noticing character types. */
1778 idx = default_conversion (idx);
1780 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
1782 error ("array subscript is not an integer");
1783 return error_mark_node;
1786 /* An array that is indexed by a non-constant
1787 cannot be stored in a register; we must be able to do
1788 address arithmetic on its address.
1789 Likewise an array of elements of variable size. */
1790 if (TREE_CODE (idx) != INTEGER_CST
1791 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1792 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1794 if (mark_addressable (array) == 0)
1795 return error_mark_node;
1797 /* An array that is indexed by a constant value which is not within
1798 the array bounds cannot be stored in a register either; because we
1799 would get a crash in store_bit_field/extract_bit_field when trying
1800 to access a non-existent part of the register. */
1801 if (TREE_CODE (idx) == INTEGER_CST
1802 && TYPE_VALUES (TREE_TYPE (array))
1803 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
1805 if (mark_addressable (array) == 0)
1806 return error_mark_node;
1809 /* Note in C++ we don't bother warning about subscripting a
1810 `register' array, since it's legal in C++ to take the address
1811 of something with that storage specification. */
1812 if (pedantic && !lvalue_p (array))
1813 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
1818 while (TREE_CODE (foo) == COMPONENT_REF)
1819 foo = TREE_OPERAND (foo, 0);
1820 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1821 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
1824 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1825 rval = build (ARRAY_REF, type, array, idx);
1826 /* Array ref is const/volatile if the array elements are
1827 or if the array is.. */
1828 TREE_READONLY (rval)
1829 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1830 | TREE_READONLY (array));
1831 TREE_SIDE_EFFECTS (rval)
1832 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1833 | TREE_SIDE_EFFECTS (array));
1834 TREE_THIS_VOLATILE (rval)
1835 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1836 /* This was added by rms on 16 Nov 91.
1837 It fixes vol struct foo *a; a->elts[1]
1838 in an inline function.
1839 Hope it doesn't break something else. */
1840 | TREE_THIS_VOLATILE (array));
1841 return require_complete_type (fold (rval));
1845 tree ar = default_conversion (array);
1846 tree ind = default_conversion (idx);
1848 /* Put the integer in IND to simplify error checking. */
1849 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1856 if (ar == error_mark_node)
1859 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
1861 error ("subscripted value is neither array nor pointer");
1862 return error_mark_node;
1864 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1866 error ("array subscript is not an integer");
1867 return error_mark_node;
1870 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar, ind, PLUS_EXPR),
1875 /* Build a function call to function FUNCTION with parameters PARAMS.
1876 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1877 TREE_VALUE of each node is a parameter-expression.
1878 FUNCTION's data type may be a function type or a pointer-to-function.
1880 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
1881 is the list of possible methods that FUNCTION could conceivably
1882 be. If the list of methods comes from a class, then it will be
1883 a list of lists (where each element is associated with the class
1884 that produced it), otherwise it will be a simple list (for
1885 functions overloaded in global scope).
1887 In the first case, TREE_VALUE (function) is the head of one of those
1888 lists, and TREE_PURPOSE is the name of the function.
1890 In the second case, TREE_PURPOSE (function) is the function's
1893 DECL is the class instance variable, usually CURRENT_CLASS_DECL. */
1896 * [eichin:19911015.1726EST] actually return a possibly incomplete
1900 build_x_function_call (function, params, decl)
1901 tree function, params, decl;
1906 if (function == error_mark_node)
1907 return error_mark_node;
1909 type = TREE_TYPE (function);
1910 is_method = ((TREE_CODE (function) == TREE_LIST
1911 && current_class_type != NULL_TREE
1912 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function)) == function)
1913 || TREE_CODE (function) == IDENTIFIER_NODE
1914 || TREE_CODE (type) == METHOD_TYPE
1915 || TYPE_PTRMEMFUNC_P (type));
1917 /* Handle methods, friends, and overloaded functions, respectively. */
1920 if (TREE_CODE (function) == FUNCTION_DECL)
1922 if (DECL_NAME (function))
1923 function = DECL_NAME (function);
1925 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
1927 else if (TREE_CODE (function) == TREE_LIST)
1930 if (TREE_CODE (TREE_VALUE (function)) == TREE_LIST)
1931 function = TREE_PURPOSE (TREE_VALUE (function));
1933 function = TREE_PURPOSE (function);
1935 my_friendly_assert (TREE_CODE (TREE_VALUE (function)) == FUNCTION_DECL, 312);
1936 function = TREE_PURPOSE (function);
1939 else if (TREE_CODE (function) != IDENTIFIER_NODE)
1941 if (TREE_CODE (function) == OFFSET_REF)
1943 if (TREE_OPERAND (function, 0))
1944 decl = TREE_OPERAND (function, 0);
1946 /* Call via a pointer to member function. */
1947 if (decl == NULL_TREE)
1949 error ("pointer to member function called, but not in class scope");
1950 return error_mark_node;
1952 /* What other type of POINTER_TYPE could this be? */
1953 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
1954 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
1955 && TREE_CODE (function) != OFFSET_REF)
1956 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE, function);
1960 /* this is an abbreviated method call.
1961 must go through here in case it is a virtual function.
1962 @@ Perhaps this could be optimized. */
1964 if (decl == NULL_TREE)
1966 if (current_class_type == NULL_TREE)
1968 error ("object missing in call to method `%s'",
1969 IDENTIFIER_POINTER (function));
1970 return error_mark_node;
1972 /* Yow: call from a static member function. */
1973 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (current_class_type),
1975 decl = build_indirect_ref (decl, NULL_PTR);
1978 return build_method_call (decl, function, params,
1979 NULL_TREE, LOOKUP_NORMAL);
1981 else if (TREE_CODE (function) == COMPONENT_REF
1982 && type == unknown_type_node)
1984 /* Should we undo what was done in build_component_ref? */
1985 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function, 1))) == TREE_VEC)
1986 /* Get the name that build_component_ref hid. */
1987 function = DECL_NAME (TREE_VALUE (TREE_OPERAND (function, 1)));
1989 function = TREE_PURPOSE (TREE_OPERAND (function, 1));
1990 return build_method_call (decl, function, params,
1991 NULL_TREE, LOOKUP_NORMAL);
1993 else if (TREE_CODE (function) == TREE_LIST)
1995 if (TREE_VALUE (function) == NULL_TREE)
1997 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
1998 TREE_PURPOSE (function));
1999 return error_mark_node;
2003 tree id = TREE_PURPOSE (function);
2004 function = TREE_VALUE (function);
2006 if (TREE_CODE (function) == TEMPLATE_DECL)
2007 return build_overload_call_maybe
2008 (id, params, LOOKUP_COMPLAIN, (struct candidate *)0);
2009 else if (DECL_CHAIN (function) != NULL_TREE)
2010 return build_overload_call
2011 (id, params, LOOKUP_COMPLAIN, (struct candidate *)0);
2017 if (TREE_CODE (function) == OFFSET_REF)
2019 /* If the component is a data element (or a virtual function), we play
2020 games here to make things work. */
2023 if (TREE_OPERAND (function, 0))
2024 decl = TREE_OPERAND (function, 0);
2028 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2029 function = get_member_function_from_ptrfunc (&decl_addr, decl,
2030 TREE_OPERAND (function, 1));
2031 params = tree_cons (NULL_TREE, decl_addr, params);
2032 return build_function_call (function, params);
2035 type = TREE_TYPE (function);
2036 if (type != error_mark_node)
2038 if (TREE_CODE (type) == REFERENCE_TYPE)
2039 type = TREE_TYPE (type);
2041 if (TYPE_LANG_SPECIFIC (type) && TYPE_OVERLOADS_CALL_EXPR (type))
2042 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2047 tree fntype = TREE_TYPE (function);
2050 /* Explicitly named method? */
2051 if (TREE_CODE (function) == FUNCTION_DECL)
2052 ctypeptr = TYPE_POINTER_TO (DECL_CLASS_CONTEXT (function));
2053 /* Expression with ptr-to-method type? It could either be a plain
2054 usage, or it might be a case where the ptr-to-method is being
2055 passed in as an argument. */
2056 else if (TYPE_PTRMEMFUNC_P (fntype))
2058 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2059 ctypeptr = TYPE_POINTER_TO (rec);
2061 /* Unexpected node type? */
2063 my_friendly_abort (116);
2064 if (decl == NULL_TREE)
2066 if (current_function_decl
2067 && DECL_STATIC_FUNCTION_P (current_function_decl))
2068 error ("invalid call to member function needing `this' in static member function scope");
2070 error ("pointer to member function called, but not in class scope");
2071 return error_mark_node;
2073 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2074 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2076 decl = build_unary_op (ADDR_EXPR, decl, 0);
2077 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2080 decl = build_c_cast (ctypeptr, decl);
2081 params = tree_cons (NULL_TREE, decl, params);
2084 return build_function_call (function, params);
2087 /* Resolve a pointer to member function. INSTANCE is the object
2088 instance to use, if the member points to a virtual member. */
2091 get_member_function_from_ptrfunc (instance_ptrptr, instance, function)
2092 tree *instance_ptrptr;
2096 if (TREE_CODE (function) == OFFSET_REF)
2098 function = TREE_OPERAND (function, 1);
2101 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2103 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2104 tree index = save_expr (convert (integer_type_node,
2105 build_component_ref (function,
2108 tree e1 = build (GT_EXPR, integer_type_node, index, integer_zero_node);
2109 tree delta = build_component_ref (function, delta_identifier, 0, 0);
2110 tree delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2116 /* convert down to the right base, before using the instance. */
2117 instance = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)),
2118 build_unary_op (ADDR_EXPR, instance, 0));
2119 if (instance == error_mark_node)
2122 vtbl = convert_pointer_to (ptr_type_node, instance);
2123 vtbl = build (PLUS_EXPR,
2124 build_pointer_type (build_pointer_type (vtable_entry_type)),
2125 vtbl, convert (sizetype, delta2));
2126 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2127 aref = build_array_ref (vtbl, size_binop (MINUS_EXPR,
2130 aref = save_expr (aref);
2132 /* Save the intermediate result in a SAVE_EXPR so we don't have to
2133 compute each component of the virtual function pointer twice. */
2134 if (/* !building_cleanup && */ TREE_CODE (aref) == INDIRECT_REF)
2135 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
2137 delta = build (PLUS_EXPR, integer_type_node,
2138 build_conditional_expr (e1, build_component_ref (aref, delta_identifier, 0, 0), integer_zero_node),
2141 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (*instance_ptrptr),
2143 convert (integer_type_node, delta));
2144 e2 = build_component_ref (aref, pfn_identifier, 0, 0);
2146 e3 = PFN_FROM_PTRMEMFUNC (function);
2147 TREE_TYPE (e2) = TREE_TYPE (e3);
2148 function = build_conditional_expr (e1, e2, e3);
2154 build_function_call_real (function, params, require_complete, flags)
2155 tree function, params;
2156 int require_complete, flags;
2158 register tree fntype, fndecl;
2159 register tree value_type;
2160 register tree coerced_params;
2161 tree name = NULL_TREE, assembler_name = NULL_TREE;
2164 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2165 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2166 if (TREE_CODE (function) == NOP_EXPR
2167 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2168 function = TREE_OPERAND (function, 0);
2170 if (TREE_CODE (function) == FUNCTION_DECL)
2172 name = DECL_NAME (function);
2173 assembler_name = DECL_ASSEMBLER_NAME (function);
2175 GNU_xref_call (current_function_decl,
2176 IDENTIFIER_POINTER (name ? name
2177 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function))));
2178 assemble_external (function);
2181 /* Convert anything with function type to a pointer-to-function. */
2184 && IDENTIFIER_LENGTH (name) == 4
2185 && ! strcmp (IDENTIFIER_POINTER (name), "main")
2186 && DECL_CONTEXT (function) == NULL_TREE)
2188 pedwarn ("ANSI C++ forbids calling `main' from within program");
2191 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2192 (because calling an inline function does not mean the function
2193 needs to be separately compiled). */
2195 if (DECL_INLINE (function))
2197 fntype = build_type_variant (TREE_TYPE (function),
2198 TREE_READONLY (function),
2199 TREE_THIS_VOLATILE (function));
2200 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
2204 assemble_external (function);
2205 TREE_USED (function) = 1;
2206 function = default_conversion (function);
2213 /* Convert anything with function type to a pointer-to-function. */
2214 if (function == error_mark_node)
2215 return error_mark_node;
2216 function = default_conversion (function);
2219 fntype = TREE_TYPE (function);
2221 if (TYPE_PTRMEMFUNC_P (fntype))
2223 tree instance_ptr = build_unary_op (ADDR_EXPR, C_C_D, 0);
2224 fntype = TYPE_PTRMEMFUNC_FN_TYPE (fntype);
2225 function = get_member_function_from_ptrfunc (&instance_ptr, C_C_D, function);
2228 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2229 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2231 if (!((TREE_CODE (fntype) == POINTER_TYPE
2232 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2235 error ("called object is not a function");
2236 return error_mark_node;
2239 /* fntype now gets the type of function pointed to. */
2240 fntype = TREE_TYPE (fntype);
2242 /* Convert the parameters to the types declared in the
2243 function prototype, or apply default promotions. */
2245 if (flags & LOOKUP_COMPLAIN)
2246 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2247 params, fndecl, LOOKUP_NORMAL);
2249 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2252 /* Check for errors in format strings. */
2254 if (warn_format && (name || assembler_name))
2255 check_function_format (name, assembler_name, coerced_params);
2257 /* Recognize certain built-in functions so we can make tree-codes
2258 other than CALL_EXPR. We do this when it enables fold-const.c
2259 to do something useful. */
2261 if (TREE_CODE (function) == ADDR_EXPR
2262 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2263 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2264 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
2269 if (coerced_params == 0)
2270 return integer_zero_node;
2271 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2275 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2277 register tree result =
2278 build (CALL_EXPR, value_type,
2279 function, coerced_params, NULL_TREE);
2281 TREE_SIDE_EFFECTS (result) = 1;
2282 TREE_RAISES (result) |= !! TYPE_RAISES_EXCEPTIONS (fntype);
2283 if (! require_complete)
2285 if (value_type == void_type_node)
2287 return require_complete_type (result);
2292 build_function_call (function, params)
2293 tree function, params;
2295 return build_function_call_real (function, params, 1, 0);
2299 build_function_call_maybe (function, params)
2300 tree function, params;
2302 return build_function_call_real (function, params, 0, 0);
2306 /* Convert the actual parameter expressions in the list VALUES
2307 to the types in the list TYPELIST.
2308 If parmdecls is exhausted, or when an element has NULL as its type,
2309 perform the default conversions.
2311 RETURN_LOC is the location of the return value, if known, NULL_TREE
2312 otherwise. This is useful in the case where we can avoid creating
2313 a temporary variable in the case where we can initialize the return
2314 value directly. If we are not eliding constructors, then we set this
2315 to NULL_TREE to avoid this avoidance.
2317 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2319 This is also where warnings about wrong number of args are generated.
2321 Return a list of expressions for the parameters as converted.
2323 Both VALUES and the returned value are chains of TREE_LIST nodes
2324 with the elements of the list in the TREE_VALUE slots of those nodes.
2326 In C++, unspecified trailing parameters can be filled in with their
2327 default arguments, if such were specified. Do so here. */
2330 convert_arguments (return_loc, typelist, values, fndecl, flags)
2331 tree return_loc, typelist, values, fndecl;
2334 extern tree gc_protect_fndecl;
2335 register tree typetail, valtail;
2336 register tree result = NULL_TREE;
2338 int maybe_raises = 0;
2341 if (! flag_elide_constructors)
2346 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2348 if (DECL_NAME (fndecl) == NULL_TREE
2349 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2350 called_thing = "constructor";
2352 called_thing = "member function";
2357 called_thing = "function";
2361 for (valtail = values, typetail = typelist;
2363 valtail = TREE_CHAIN (valtail), i++)
2365 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2366 register tree val = TREE_VALUE (valtail);
2368 if (type == void_type_node)
2372 char *buf = (char *)alloca (40 + strlen (called_thing));
2373 sprintf (buf, "too many arguments to %s `%%s'", called_thing);
2374 error_with_decl (fndecl, buf);
2375 error ("at this point in file");
2378 error ("too many arguments to function");
2379 /* In case anybody wants to know if this argument
2382 TREE_TYPE (tree_last (result)) = error_mark_node;
2386 /* The tree type of the parameter being passed may not yet be
2387 known. In this case, its type is TYPE_UNKNOWN, and will
2388 be instantiated by the type given by TYPE. If TYPE
2389 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2390 if (type && type_unknown_p (val))
2391 val = require_instantiated_type (type, val, integer_zero_node);
2392 else if (type_unknown_p (val))
2394 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2395 if (TREE_CODE (val) == ADDR_EXPR)
2396 val = TREE_OPERAND (val, 0);
2397 if (TREE_CODE (val) == TREE_LIST
2398 && TREE_CHAIN (val) == NULL_TREE
2399 && TREE_TYPE (TREE_VALUE (val)) != NULL_TREE
2400 && (TREE_TYPE (val) == unknown_type_node
2401 || DECL_CHAIN (TREE_VALUE (val)) == NULL_TREE))
2402 /* Instantiates automatically. */
2403 val = TREE_VALUE (val);
2406 error ("insufficient type information in parameter list");
2407 val = integer_zero_node;
2410 else if (TREE_CODE (val) == OFFSET_REF
2411 && TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2413 /* This is unclean. Should be handled elsewhere. */
2414 val = build_unary_op (ADDR_EXPR, val, 0);
2416 else if (TREE_CODE (val) == OFFSET_REF)
2417 val = resolve_offset_ref (val);
2421 /* This code forces the assumption that if we have a ptr-to-func
2422 type in an arglist, that every routine that wants to check
2423 its validity has done so, and thus we need not do any
2424 more conversion. I don't remember why this is necessary. */
2425 else if (TREE_CODE (ttype) == FUNCTION_TYPE
2427 || TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
2428 || TREE_CODE (TREE_TYPE (type)) == VOID_TYPE))
2430 type = build_pointer_type (ttype);
2435 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2436 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2437 if (TREE_CODE (val) == NOP_EXPR
2438 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
2439 val = TREE_OPERAND (val, 0);
2441 if ((type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2442 && (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2443 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2444 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE))
2445 val = default_conversion (val);
2447 val = require_complete_type (val);
2449 if (val == error_mark_node)
2452 maybe_raises |= TREE_RAISES (val);
2456 /* Formal parm type is specified by a function prototype. */
2459 if (TYPE_SIZE (type) == 0)
2461 error ("parameter type of called function is incomplete");
2466 #ifdef PROMOTE_PROTOTYPES
2467 /* Rather than truncating and then reextending,
2468 convert directly to int, if that's the type we will want. */
2469 if (! flag_traditional
2470 && (TREE_CODE (type) == INTEGER_TYPE
2471 || TREE_CODE (type) == ENUMERAL_TYPE)
2472 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2473 type = integer_type_node;
2475 parmval = convert_for_initialization (return_loc, type, val, flags,
2476 "argument passing", fndecl, i);
2477 #ifdef PROMOTE_PROTOTYPES
2478 if ((TREE_CODE (type) == INTEGER_TYPE
2479 || TREE_CODE (type) == ENUMERAL_TYPE)
2480 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2481 parmval = default_conversion (parmval);
2484 result = tree_cons (NULL_TREE, parmval, result);
2488 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2489 val = convert_from_reference (val);
2491 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2492 && (TYPE_PRECISION (TREE_TYPE (val))
2493 < TYPE_PRECISION (double_type_node)))
2494 /* Convert `float' to `double'. */
2495 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2496 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
2497 && (TYPE_HAS_INIT_REF (TREE_TYPE (val))
2498 || TYPE_HAS_ASSIGN_REF (TREE_TYPE (val))))
2500 cp_warning ("cannot pass objects of type `%T' through `...'",
2502 result = tree_cons (NULL_TREE, val, result);
2505 /* Convert `short' and `char' to full-size `int'. */
2506 result = tree_cons (NULL_TREE, default_conversion (val), result);
2510 /* There are certain functions for which we don't need
2511 to protect our arguments. GC_PROTECT_FNDECL is one. */
2512 && fndecl != gc_protect_fndecl
2513 && type_needs_gc_entry (TREE_TYPE (TREE_VALUE (result)))
2514 && ! value_safe_from_gc (NULL_TREE, TREE_VALUE (result)))
2515 /* This will build a temporary variable whose cleanup is
2516 to clear the obstack entry. */
2517 TREE_VALUE (result) = protect_value_from_gc (NULL_TREE,
2518 TREE_VALUE (result));
2521 typetail = TREE_CHAIN (typetail);
2524 if (typetail != 0 && typetail != void_list_node)
2526 /* See if there are default arguments that can be used */
2527 if (TREE_PURPOSE (typetail))
2529 while (typetail != void_list_node)
2531 tree type = TREE_VALUE (typetail);
2532 tree val = TREE_PURPOSE (typetail);
2535 if (val == NULL_TREE)
2536 parmval = error_mark_node;
2537 else if (TREE_CODE (val) == CONSTRUCTOR)
2539 parmval = digest_init (type, val, (tree *)0);
2540 parmval = convert_for_initialization (return_loc, type, parmval, flags,
2541 "default constructor", fndecl, i);
2545 /* This could get clobbered by the following call. */
2546 if (TREE_HAS_CONSTRUCTOR (val))
2547 val = copy_node (val);
2549 parmval = convert_for_initialization (return_loc, type, val, flags,
2550 "default argument", fndecl, i);
2551 #ifdef PROMOTE_PROTOTYPES
2552 if ((TREE_CODE (type) == INTEGER_TYPE
2553 || TREE_CODE (type) == ENUMERAL_TYPE)
2554 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2555 parmval = default_conversion (parmval);
2558 maybe_raises |= TREE_RAISES (parmval);
2561 && type_needs_gc_entry (TREE_TYPE (parmval))
2562 && ! value_safe_from_gc (NULL_TREE, parmval))
2563 parmval = protect_value_from_gc (NULL_TREE, parmval);
2565 result = tree_cons (0, parmval, result);
2566 typetail = TREE_CHAIN (typetail);
2567 /* ends with `...'. */
2568 if (typetail == NULL_TREE)
2576 char *buf = (char *)alloca (32 + strlen (called_thing));
2577 sprintf (buf, "too few arguments to %s `%%#D'", called_thing);
2578 cp_error_at (buf, fndecl);
2579 error ("at this point in file");
2582 error ("too few arguments to function");
2583 return error_mark_list;
2587 TREE_RAISES (result) = maybe_raises;
2589 return nreverse (result);
2592 /* Build a binary-operation expression, after performing default
2593 conversions on the operands. CODE is the kind of expression to build. */
2596 build_x_binary_op (code, arg1, arg2)
2597 enum tree_code code;
2600 tree rval = build_opfncall (code, LOOKUP_SPECULATIVELY,
2601 arg1, arg2, NULL_TREE);
2603 return build_opfncall (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2604 if (code == MEMBER_REF)
2605 return build_m_component_ref (build_indirect_ref (arg1, NULL_PTR),
2607 return build_binary_op (code, arg1, arg2, 1);
2611 build_binary_op (code, arg1, arg2, convert_p)
2612 enum tree_code code;
2624 args[0] = default_conversion (args[0]);
2625 args[1] = default_conversion (args[1]);
2627 if (type_unknown_p (args[0]))
2629 args[0] = instantiate_type (TREE_TYPE (args[1]), args[0], 1);
2630 args[0] = default_conversion (args[0]);
2632 else if (type_unknown_p (args[1]))
2634 args[1] = require_instantiated_type (TREE_TYPE (args[0]),
2637 args[1] = default_conversion (args[1]);
2640 type1 = TREE_TYPE (args[0]);
2641 type2 = TREE_TYPE (args[1]);
2643 if (IS_AGGR_TYPE_2 (type1, type2) && ! TYPE_PTRMEMFUNC_P (type1))
2645 /* Try to convert this to something reasonable. */
2646 if (! build_default_binary_type_conversion(code, &args[0], &args[1]))
2647 return error_mark_node;
2649 else if ((IS_AGGR_TYPE (type1) && ! TYPE_PTRMEMFUNC_P (type1))
2650 || (IS_AGGR_TYPE (type2) && ! TYPE_PTRMEMFUNC_P (type2)))
2652 int convert_index = IS_AGGR_TYPE (type2);
2653 /* Avoid being tripped up by things like (ARG1 != 0). */
2656 types[0] = type1; types[1] = type2;
2657 try = build_type_conversion (code, types[convert_index ^ 1],
2658 args[convert_index], 1);
2661 && args[1] == integer_zero_node
2662 && (code == NE_EXPR || code == EQ_EXPR))
2663 try = build_type_conversion (code, ptr_type_node,
2664 args[convert_index], 1);
2667 cp_error ("no match for `%O(%#T, %#T)'", code,
2668 types[convert_index], types[convert_index ^ 1]);
2669 return error_mark_node;
2671 if (try == error_mark_node)
2672 error ("ambiguous pointer conversion");
2673 args[convert_index] = try;
2676 return build_binary_op_nodefault (code, args[0], args[1], code);
2679 /* Build a binary-operation expression without default conversions.
2680 CODE is the kind of expression to build.
2681 This function differs from `build' in several ways:
2682 the data type of the result is computed and recorded in it,
2683 warnings are generated if arg data types are invalid,
2684 special handling for addition and subtraction of pointers is known,
2685 and some optimization is done (operations on narrow ints
2686 are done in the narrower type when that gives the same result).
2687 Constant folding is also done before the result is returned.
2689 ERROR_CODE is the code that determines what to say in error messages.
2690 It is usually, but not always, the same as CODE.
2692 Note that the operands will never have enumeral types
2693 because either they have just had the default conversions performed
2694 or they have both just been converted to some other type in which
2695 the arithmetic is to be done.
2697 C++: must do special pointer arithmetic when implementing
2698 multiple inheritance, and deal with pointer to member functions. */
2701 build_binary_op_nodefault (code, op0, op1, error_code)
2702 enum tree_code code;
2704 enum tree_code error_code;
2706 tree type0 = TREE_TYPE (op0), type1 = TREE_TYPE (op1);
2708 /* The expression codes of the data types of the arguments tell us
2709 whether the arguments are integers, floating, pointers, etc. */
2710 register enum tree_code code0 = TREE_CODE (type0);
2711 register enum tree_code code1 = TREE_CODE (type1);
2713 /* Expression code to give to the expression when it is built.
2714 Normally this is CODE, which is what the caller asked for,
2715 but in some special cases we change it. */
2716 register enum tree_code resultcode = code;
2718 /* Data type in which the computation is to be performed.
2719 In the simplest cases this is the common type of the arguments. */
2720 register tree result_type = NULL;
2722 /* Nonzero means operands have already been type-converted
2723 in whatever way is necessary.
2724 Zero means they need to be converted to RESULT_TYPE. */
2727 /* Nonzero means after finally constructing the expression
2728 give it this type. Otherwise, give it type RESULT_TYPE. */
2729 tree final_type = 0;
2731 /* Nonzero if this is an operation like MIN or MAX which can
2732 safely be computed in short if both args are promoted shorts.
2733 Also implies COMMON.
2734 -1 indicates a bitwise operation; this makes a difference
2735 in the exact conditions for when it is safe to do the operation
2736 in a narrower mode. */
2739 /* Nonzero if this is a comparison operation;
2740 if both args are promoted shorts, compare the original shorts.
2741 Also implies COMMON. */
2742 int short_compare = 0;
2744 /* Nonzero if this is a right-shift operation, which can be computed on the
2745 original short and then promoted if the operand is a promoted short. */
2746 int short_shift = 0;
2748 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2751 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2752 STRIP_TYPE_NOPS (op0);
2753 STRIP_TYPE_NOPS (op1);
2755 /* If an error was already reported for one of the arguments,
2756 avoid reporting another error. */
2758 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2759 return error_mark_node;
2764 /* Handle the pointer + int case. */
2765 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2766 return pointer_int_sum (PLUS_EXPR, op0, op1);
2767 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2768 return pointer_int_sum (PLUS_EXPR, op1, op0);
2774 /* Subtraction of two similar pointers.
2775 We must subtract them as integers, then divide by object size. */
2776 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2777 && comp_target_types (type0, type1, 1))
2778 return pointer_diff (op0, op1);
2779 /* Handle pointer minus int. Just like pointer plus int. */
2780 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2781 return pointer_int_sum (MINUS_EXPR, op0, op1);
2790 case TRUNC_DIV_EXPR:
2792 case FLOOR_DIV_EXPR:
2793 case ROUND_DIV_EXPR:
2794 case EXACT_DIV_EXPR:
2795 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2796 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2798 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2799 resultcode = RDIV_EXPR;
2801 /* When dividing two signed integers, we have to promote to int.
2802 unless we divide by a conatant != -1. Note that default
2803 conversion will have been performed on the operands at this
2804 point, so we have to dig out the original type to find out if
2806 shorten = ((TREE_CODE (op0) == NOP_EXPR
2807 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2808 || (TREE_CODE (op1) == INTEGER_CST
2809 && (TREE_INT_CST_LOW (op1) != -1
2810 || TREE_INT_CST_HIGH (op1) != -1)));
2816 case BIT_ANDTC_EXPR:
2819 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2821 /* If one operand is a constant, and the other is a short type
2822 that has been converted to an int,
2823 really do the work in the short type and then convert the
2824 result to int. If we are lucky, the constant will be 0 or 1
2825 in the short type, making the entire operation go away. */
2826 if (TREE_CODE (op0) == INTEGER_CST
2827 && TREE_CODE (op1) == NOP_EXPR
2828 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2829 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2831 final_type = result_type;
2832 op1 = TREE_OPERAND (op1, 0);
2833 result_type = TREE_TYPE (op1);
2835 if (TREE_CODE (op1) == INTEGER_CST
2836 && TREE_CODE (op0) == NOP_EXPR
2837 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2838 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2840 final_type = result_type;
2841 op0 = TREE_OPERAND (op0, 0);
2842 result_type = TREE_TYPE (op0);
2846 case TRUNC_MOD_EXPR:
2847 case FLOOR_MOD_EXPR:
2848 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2850 /* Although it would be tempting to shorten always here, that loses
2851 on some targets, since the modulo instruction is undefined if the
2852 quotient can't be represented in the computation mode. We shorten
2853 only if unsigned or if dividing by something we know != -1. */
2854 shorten = ((TREE_CODE (op0) == NOP_EXPR
2855 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2856 || (TREE_CODE (op1) == INTEGER_CST
2857 && (TREE_INT_CST_LOW (op1) != -1
2858 || TREE_INT_CST_HIGH (op1) != -1)));
2863 case TRUTH_ANDIF_EXPR:
2864 case TRUTH_ORIF_EXPR:
2865 case TRUTH_AND_EXPR:
2867 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE || code0 == REAL_TYPE)
2868 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE || code1 == REAL_TYPE))
2870 /* Result of these operations is always an int,
2871 but that does not mean the operands should be
2872 converted to ints! */
2873 result_type = integer_type_node;
2874 op0 = truthvalue_conversion (op0);
2875 op1 = truthvalue_conversion (op1);
2880 /* Shift operations: result has same type as first operand;
2881 always convert second operand to int.
2882 Also set SHORT_SHIFT if shifting rightward. */
2885 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2887 result_type = type0;
2888 if (TREE_CODE (op1) == INTEGER_CST)
2890 if (tree_int_cst_lt (op1, integer_zero_node))
2891 warning ("right shift count is negative");
2894 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2896 if (TREE_INT_CST_HIGH (op1) != 0
2897 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2898 >= TYPE_PRECISION (type0)))
2899 warning ("right shift count >= width of type");
2902 /* Convert the shift-count to an integer, regardless of
2903 size of value being shifted. */
2904 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2905 op1 = convert (integer_type_node, op1);
2906 /* Avoid converting op1 to result_type later. */
2912 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2914 result_type = type0;
2915 if (TREE_CODE (op1) == INTEGER_CST)
2917 if (tree_int_cst_lt (op1, integer_zero_node))
2918 warning ("left shift count is negative");
2919 else if (TREE_INT_CST_HIGH (op1) != 0
2920 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2921 >= TYPE_PRECISION (type0)))
2922 warning ("left shift count >= width of type");
2924 /* Convert the shift-count to an integer, regardless of
2925 size of value being shifted. */
2926 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2927 op1 = convert (integer_type_node, op1);
2928 /* Avoid converting op1 to result_type later. */
2935 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2937 result_type = type0;
2938 if (TREE_CODE (op1) == INTEGER_CST)
2940 if (tree_int_cst_lt (op1, integer_zero_node))
2941 warning ("%s rotate count is negative",
2942 (code == LROTATE_EXPR) ? "left" : "right");
2943 else if (TREE_INT_CST_HIGH (op1) != 0
2944 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2945 >= TYPE_PRECISION (type0)))
2946 warning ("%s rotate count >= width of type",
2947 (code == LROTATE_EXPR) ? "left" : "right");
2949 /* Convert the shift-count to an integer, regardless of
2950 size of value being shifted. */
2951 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2952 op1 = convert (integer_type_node, op1);
2958 /* Result of comparison is always int,
2959 but don't convert the args to int! */
2960 result_type = integer_type_node;
2962 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2963 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2965 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2967 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
2968 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
2969 /* Anything compares with void *. void * compares with anything.
2970 Otherwise, the targets must be the same. */
2971 if (tt0 != tt1 && TREE_CODE (tt0) == RECORD_TYPE
2972 && TREE_CODE (tt1) == RECORD_TYPE)
2974 tree base = common_base_type (tt0, tt1);
2975 if (base == NULL_TREE)
2976 warning ("comparison of distinct object pointer types");
2977 else if (base == error_mark_node)
2979 message_2_types (error, "comparison of pointer types `%s*' and `%s*' requires conversion to ambiguous supertype", tt0, tt1);
2980 return error_mark_node;
2984 if (integer_zerop (op0))
2985 op0 = null_pointer_node;
2987 op0 = convert_pointer_to (base, op0);
2988 if (integer_zerop (op1))
2989 op1 = null_pointer_node;
2991 op1 = convert_pointer_to (base, op1);
2994 else if (comp_target_types (type0, type1, 1))
2996 else if (tt0 == void_type_node)
2998 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
2999 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3001 else if (tt1 == void_type_node)
3003 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
3004 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3006 else if ((TYPE_SIZE (tt0) != 0) != (TYPE_SIZE (tt1) != 0))
3007 pedwarn ("comparison of complete and incomplete pointers");
3009 pedwarn ("comparison of distinct pointer types lacks a cast");
3011 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3012 && integer_zerop (op1))
3013 op1 = null_pointer_node;
3014 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3015 && integer_zerop (op0))
3016 op0 = null_pointer_node;
3017 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3019 error ("ANSI C++ forbids comparison between pointer and integer");
3020 op1 = convert (TREE_TYPE (op0), op1);
3022 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3024 error ("ANSI C++ forbids comparison between pointer and integer");
3025 op0 = convert (TREE_TYPE (op1), op0);
3027 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3028 && integer_zerop (op1))
3030 op0 = build_component_ref (op0, index_identifier, 0, 0);
3031 op1 = integer_zero_node;
3033 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3034 && integer_zerop (op0))
3036 op0 = build_component_ref (op1, index_identifier, 0, 0);
3037 op1 = integer_zero_node;
3039 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3040 && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3041 == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3043 /* The code we generate for the test is:
3045 (op0.index == op1.index
3046 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3047 || op0.pfn == op1.pfn)) */
3049 tree index0 = build_component_ref (op0, index_identifier, 0, 0);
3050 tree index1 = save_expr (build_component_ref (op1, index_identifier, 0, 0));
3051 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3052 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3053 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3054 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3056 tree integer_neg_one_node
3057 = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
3058 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3059 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3060 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3061 e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3062 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3063 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3064 if (code == EQ_EXPR)
3066 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3068 else if (TYPE_PTRMEMFUNC_P (type0)
3069 && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3071 tree index0 = build_component_ref (op0, index_identifier, 0, 0);
3073 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3074 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3075 tree delta21 = integer_zero_node;
3077 tree integer_neg_one_node
3078 = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
3079 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3080 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3082 /* Map everything down one to make room for the null pointer to member. */
3083 index1 = size_binop (PLUS_EXPR,
3084 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3086 op1 = integer_zero_node;
3087 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1)));
3088 delta21 = DECL_FIELD_BITPOS (delta21);
3089 delta21 = size_binop (FLOOR_DIV_EXPR, delta21, size_int (BITS_PER_UNIT));
3092 index1 = integer_neg_one_node;
3094 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0), op1);
3095 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3098 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3099 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3100 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3101 e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3102 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3103 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3104 if (code == EQ_EXPR)
3106 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3108 else if (TYPE_PTRMEMFUNC_P (type1)
3109 && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3111 return build_binary_op (code, op1, op0, 1);
3114 /* If args are not valid, clear out RESULT_TYPE
3115 to cause an error message later. */
3121 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3122 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3124 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3126 if (! comp_target_types (type0, type1, 1))
3127 pedwarn ("comparison of distinct pointer types lacks a cast");
3128 else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
3129 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
3130 pedwarn ("comparison of complete and incomplete pointers");
3132 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
3133 pedwarn ("ANSI C++ forbids ordered comparisons of pointers to functions");
3134 result_type = common_type (type0, type1);
3142 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3143 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3145 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3147 if (! comp_target_types (type0, type1, 1))
3148 pedwarn ("comparison of distinct pointer types lacks a cast");
3149 else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
3150 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
3151 pedwarn ("comparison of complete and incomplete pointers");
3153 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
3154 pedwarn ("ANSI C++ forbids ordered comparisons of pointers to functions");
3155 result_type = integer_type_node;
3157 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3158 && integer_zerop (op1))
3160 result_type = integer_type_node;
3161 op1 = null_pointer_node;
3163 pedwarn ("ordered comparison of pointer with integer zero");
3165 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3166 && integer_zerop (op0))
3168 result_type = integer_type_node;
3169 op0 = null_pointer_node;
3171 pedwarn ("ANSI C++ forbids ordered comparison of pointer with integer zero");
3173 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3175 result_type = integer_type_node;
3177 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3178 else if (! flag_traditional)
3179 warning ("comparison between pointer and integer");
3180 op1 = convert (TREE_TYPE (op0), op1);
3182 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3184 result_type = integer_type_node;
3186 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3187 else if (! flag_traditional)
3188 warning ("comparison between pointer and integer");
3189 op0 = convert (TREE_TYPE (op1), op0);
3195 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3196 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3198 if (shorten || common || short_compare)
3199 result_type = common_type (type0, type1);
3201 /* For certain operations (which identify themselves by shorten != 0)
3202 if both args were extended from the same smaller type,
3203 do the arithmetic in that type and then extend.
3205 shorten !=0 and !=1 indicates a bitwise operation.
3206 For them, this optimization is safe only if
3207 both args are zero-extended or both are sign-extended.
3208 Otherwise, we might change the result.
3209 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3210 but calculated in (unsigned short) it would be (unsigned short)-1. */
3214 int unsigned0, unsigned1;
3215 tree arg0 = get_narrower (op0, &unsigned0);
3216 tree arg1 = get_narrower (op1, &unsigned1);
3217 /* UNS is 1 if the operation to be done is an unsigned one. */
3218 int uns = TREE_UNSIGNED (result_type);
3221 final_type = result_type;
3223 /* Handle the case that OP0 does not *contain* a conversion
3224 but it *requires* conversion to FINAL_TYPE. */
3226 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3227 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3228 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3229 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3231 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3233 /* For bitwise operations, signedness of nominal type
3234 does not matter. Consider only how operands were extended. */
3238 /* Note that in all three cases below we refrain from optimizing
3239 an unsigned operation on sign-extended args.
3240 That would not be valid. */
3242 /* Both args variable: if both extended in same way
3243 from same width, do it in that width.
3244 Do it unsigned if args were zero-extended. */
3245 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3246 < TYPE_PRECISION (result_type))
3247 && (TYPE_PRECISION (TREE_TYPE (arg1))
3248 == TYPE_PRECISION (TREE_TYPE (arg0)))
3249 && unsigned0 == unsigned1
3250 && (unsigned0 || !uns))
3252 = signed_or_unsigned_type (unsigned0,
3253 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3254 else if (TREE_CODE (arg0) == INTEGER_CST
3255 && (unsigned1 || !uns)
3256 && (TYPE_PRECISION (TREE_TYPE (arg1))
3257 < TYPE_PRECISION (result_type))
3258 && (type = signed_or_unsigned_type (unsigned1,
3260 int_fits_type_p (arg0, type)))
3262 else if (TREE_CODE (arg1) == INTEGER_CST
3263 && (unsigned0 || !uns)
3264 && (TYPE_PRECISION (TREE_TYPE (arg0))
3265 < TYPE_PRECISION (result_type))
3266 && (type = signed_or_unsigned_type (unsigned0,
3268 int_fits_type_p (arg1, type)))
3272 /* Shifts can be shortened if shifting right. */
3277 tree arg0 = get_narrower (op0, &unsigned_arg);
3279 final_type = result_type;
3281 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3282 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3284 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3285 /* If arg is sign-extended and then unsigned-shifted,
3286 we can simulate this with a signed shift in arg's type
3287 only if the extended result is at least twice as wide
3288 as the arg. Otherwise, the shift could use up all the
3289 ones made by sign-extension and bring in zeros.
3290 We can't optimize that case at all, but in most machines
3291 it never happens because available widths are 2**N. */
3292 && (!TREE_UNSIGNED (final_type)
3294 || ((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0))
3295 <= TYPE_PRECISION (result_type))))
3297 /* Do an unsigned shift if the operand was zero-extended. */
3299 = signed_or_unsigned_type (unsigned_arg,
3301 /* Convert value-to-be-shifted to that type. */
3302 if (TREE_TYPE (op0) != result_type)
3303 op0 = convert (result_type, op0);
3308 /* Comparison operations are shortened too but differently.
3309 They identify themselves by setting short_compare = 1. */
3313 /* Don't write &op0, etc., because that would prevent op0
3314 from being kept in a register.
3315 Instead, make copies of the our local variables and
3316 pass the copies by reference, then copy them back afterward. */
3317 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3318 enum tree_code xresultcode = resultcode;
3320 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3323 op0 = xop0, op1 = xop1, result_type = xresult_type;
3324 resultcode = xresultcode;
3327 if (short_compare && extra_warnings)
3329 int unsignedp0, unsignedp1;
3330 tree primop0 = get_narrower (op0, &unsignedp0);
3331 tree primop1 = get_narrower (op1, &unsignedp1);
3333 /* Warn if signed and unsigned are being compared in a size larger
3334 than their original size, as this will always fail. */
3336 if (unsignedp0 != unsignedp1
3337 && (TYPE_PRECISION (TREE_TYPE (primop0))
3338 < TYPE_PRECISION (result_type))
3339 && (TYPE_PRECISION (TREE_TYPE (primop1))
3340 < TYPE_PRECISION (result_type)))
3341 warning ("comparison between promoted unsigned and signed");
3343 /* Warn if two unsigned values are being compared in a size
3344 larger than their original size, and one (and only one) is the
3345 result of a `~' operator. This comparison will always fail.
3347 Also warn if one operand is a constant, and the constant does not
3348 have all bits set that are set in the ~ operand when it is
3351 else if (TREE_CODE (primop0) == BIT_NOT_EXPR
3352 ^ TREE_CODE (primop1) == BIT_NOT_EXPR)
3354 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3355 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3356 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3357 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3359 if (TREE_CODE (primop0) == INTEGER_CST
3360 || TREE_CODE (primop1) == INTEGER_CST)
3363 HOST_WIDE_INT constant, mask;
3367 if (TREE_CODE (primop0) == INTEGER_CST)
3370 unsignedp = unsignedp1;
3371 constant = TREE_INT_CST_LOW (primop0);
3376 unsignedp = unsignedp0;
3377 constant = TREE_INT_CST_LOW (primop1);
3380 bits = TYPE_PRECISION (TREE_TYPE (primop));
3381 if (bits < TYPE_PRECISION (result_type)
3382 && bits < HOST_BITS_PER_LONG && unsignedp)
3384 mask = (~ (HOST_WIDE_INT) 0) << bits;
3385 if ((mask & constant) != mask)
3386 warning ("comparison of promoted ~unsigned with constant");
3389 else if (unsignedp0 && unsignedp1
3390 && (TYPE_PRECISION (TREE_TYPE (primop0))
3391 < TYPE_PRECISION (result_type))
3392 && (TYPE_PRECISION (TREE_TYPE (primop1))
3393 < TYPE_PRECISION (result_type)))
3394 warning ("comparison of promoted ~unsigned with unsigned");
3399 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3400 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3401 Then the expression will be built.
3402 It will be given type FINAL_TYPE if that is nonzero;
3403 otherwise, it will be given type RESULT_TYPE. */
3407 binary_op_error (error_code);
3408 return error_mark_node;
3413 if (TREE_TYPE (op0) != result_type)
3414 op0 = convert (result_type, op0);
3415 if (TREE_TYPE (op1) != result_type)
3416 op1 = convert (result_type, op1);
3420 register tree result = build (resultcode, result_type, op0, op1);
3421 register tree folded;
3423 folded = fold (result);
3424 if (folded == result)
3425 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3426 if (final_type != 0)
3427 return convert (final_type, folded);
3432 /* Return a tree for the sum or difference (RESULTCODE says which)
3433 of pointer PTROP and integer INTOP. */
3436 pointer_int_sum (resultcode, ptrop, intop)
3437 enum tree_code resultcode;
3438 register tree ptrop, intop;
3442 register tree result;
3443 register tree folded = fold (intop);
3445 /* The result is a pointer of the same type that is being added. */
3447 register tree result_type = TREE_TYPE (ptrop);
3449 /* Needed to make OOPS V2R3 work. */
3451 if (TREE_CODE (intop) == INTEGER_CST
3452 && TREE_INT_CST_LOW (intop) == 0
3453 && TREE_INT_CST_HIGH (intop) == 0)
3456 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3458 if (pedantic || warn_pointer_arith)
3459 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3460 size_exp = integer_one_node;
3462 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3464 if (pedantic || warn_pointer_arith)
3465 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3466 size_exp = integer_one_node;
3468 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3470 if (pedantic || warn_pointer_arith)
3471 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3472 size_exp = integer_one_node;
3474 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
3477 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3478 size_exp = integer_one_node;
3481 size_exp = size_in_bytes (TREE_TYPE (result_type));
3483 /* If what we are about to multiply by the size of the elements
3484 contains a constant term, apply distributive law
3485 and multiply that constant term separately.
3486 This helps produce common subexpressions. */
3488 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3489 && ! TREE_CONSTANT (intop)
3490 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3491 && TREE_CONSTANT (size_exp))
3493 enum tree_code subcode = resultcode;
3494 if (TREE_CODE (intop) == MINUS_EXPR)
3495 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3496 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
3497 intop = TREE_OPERAND (intop, 0);
3500 /* Convert the integer argument to a type the same size as a pointer
3501 so the multiply won't overflow spuriously. */
3503 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
3504 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
3506 /* Replace the integer argument
3507 with a suitable product by the object size. */
3509 intop = build_binary_op (MULT_EXPR, intop, size_exp, 1);
3511 /* Create the sum or difference. */
3513 result = build (resultcode, result_type, ptrop, intop);
3515 folded = fold (result);
3516 if (folded == result)
3517 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
3521 /* Return a tree for the difference of pointers OP0 and OP1.
3522 The resulting tree has type int. */
3525 pointer_diff (op0, op1)
3526 register tree op0, op1;
3528 register tree result, folded;
3529 tree restype = ptrdiff_type_node;
3530 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3534 if (TREE_CODE (target_type) == VOID_TYPE)
3535 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3536 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3537 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3538 if (TREE_CODE (target_type) == METHOD_TYPE)
3539 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3540 if (TREE_CODE (target_type) == OFFSET_TYPE)
3541 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3544 /* First do the subtraction as integers;
3545 then drop through to build the divide operator. */
3547 op0 = build_binary_op (MINUS_EXPR,
3548 convert (restype, op0), convert (restype, op1), 1);
3550 /* This generates an error if op1 is a pointer to an incomplete type. */
3551 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
3552 error ("arithmetic on pointer to an incomplete type");
3554 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3555 || TREE_CODE (target_type) == FUNCTION_TYPE
3556 || TREE_CODE (target_type) == METHOD_TYPE
3557 || TREE_CODE (target_type) == OFFSET_TYPE)
3559 : size_in_bytes (target_type));
3561 /* Do the division. */
3563 result = build (EXACT_DIV_EXPR, restype, op0, op1);
3565 folded = fold (result);
3566 if (folded == result)
3567 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3571 /* Handle the case of taking the address of a COMPONENT_REF.
3572 Called by `build_unary_op' and `build_up_reference'.
3574 ARG is the COMPONENT_REF whose address we want.
3575 ARGTYPE is the pointer type that this address should have.
3576 MSG is an error message to print if this COMPONENT_REF is not
3577 addressable (such as a bitfield). */
3580 build_component_addr (arg, argtype, msg)
3584 tree field = TREE_OPERAND (arg, 1);
3585 tree basetype = decl_type_context (field);
3586 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3588 if (DECL_BIT_FIELD (field))
3590 error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
3591 return error_mark_node;
3595 cp_warning ("address of `%T::%D' taken", basetype, field);
3597 if (TREE_CODE (field) == FIELD_DECL
3598 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
3600 /* Can't convert directly to ARGTYPE, since that
3601 may have the same pointer type as one of our
3603 rval = build1 (NOP_EXPR, argtype,
3604 convert_pointer_to (basetype, rval));
3605 TREE_CONSTANT (rval) == TREE_CONSTANT (TREE_OPERAND (rval, 0));
3608 /* This conversion is harmless. */
3609 rval = convert (argtype, rval);
3611 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3613 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3614 size_int (BITS_PER_UNIT));
3615 int flag = TREE_CONSTANT (rval);
3616 rval = fold (build (PLUS_EXPR, argtype,
3617 rval, convert (argtype, offset)));
3618 TREE_CONSTANT (rval) = flag;
3623 /* Construct and perhaps optimize a tree representation
3624 for a unary operation. CODE, a tree_code, specifies the operation
3625 and XARG is the operand. */
3628 build_x_unary_op (code, xarg)
3629 enum tree_code code;
3632 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3634 if (code != ADDR_EXPR || TREE_CODE (TREE_TYPE (xarg)) != RECORD_TYPE
3635 || TYPE_SIZE (TREE_TYPE (xarg)))
3637 tree rval = build_opfncall (code, LOOKUP_SPECULATIVELY, xarg,
3638 NULL_TREE, NULL_TREE);
3640 return build_opfncall (code, LOOKUP_NORMAL, xarg,
3641 NULL_TREE, NULL_TREE);
3643 return build_unary_op (code, xarg, 0);
3646 /* C++: Must handle pointers to members.
3648 Perhaps type instantiation should be extended to handle conversion
3649 from aggregates to types we don't yet know we want? (Or are those
3650 cases typically errors which should be reported?)
3652 NOCONVERT nonzero suppresses the default promotions
3653 (such as from short to int). */
3655 build_unary_op (code, xarg, noconvert)
3656 enum tree_code code;
3660 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3661 register tree arg = xarg;
3662 register tree argtype = 0;
3663 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
3664 char *errstring = NULL;
3668 if (typecode == ERROR_MARK)
3669 return error_mark_node;
3671 if (typecode == REFERENCE_TYPE && code != ADDR_EXPR && ! noconvert)
3673 arg = convert_from_reference (arg);
3674 typecode = TREE_CODE (TREE_TYPE (arg));
3677 if (typecode == ENUMERAL_TYPE)
3678 typecode = INTEGER_TYPE;
3680 isaggrtype = IS_AGGR_TYPE_CODE (typecode);
3685 /* This is used for unary plus, because a CONVERT_EXPR
3686 is enough to prevent anybody from looking inside for
3687 associativity, but won't generate any code. */
3688 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3689 errstring = "wrong type argument to unary plus";
3690 else if (!noconvert)
3691 arg = default_conversion (arg);
3698 arg = default_conversion (arg);
3701 cp_error ("type conversion for type `%T' not allowed",
3703 return error_mark_node;
3705 typecode = TREE_CODE (TREE_TYPE (arg));
3709 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3710 errstring = "wrong type argument to unary minus";
3711 else if (!noconvert)
3712 arg = default_conversion (arg);
3719 arg = default_conversion (arg);
3722 cp_error ("type conversion for type `%T' not allowed",
3724 return error_mark_node;
3726 typecode = TREE_CODE (TREE_TYPE (arg));
3730 if (typecode != INTEGER_TYPE)
3731 errstring = "wrong type argument to bit-complement";
3732 else if (!noconvert)
3733 arg = default_conversion (arg);
3740 arg = default_conversion (arg);
3743 cp_error ("type conversion for type `%T' not allowed",
3745 return error_mark_node;
3747 typecode = TREE_CODE (TREE_TYPE (arg));
3751 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3752 errstring = "wrong type argument to abs";
3753 else if (!noconvert)
3754 arg = default_conversion (arg);
3757 case TRUTH_NOT_EXPR:
3760 arg = truthvalue_conversion (arg);
3761 typecode = TREE_CODE (TREE_TYPE (arg));
3764 if (typecode != INTEGER_TYPE
3765 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3766 /* These will convert to a pointer. */
3767 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
3769 errstring = "wrong type argument to unary exclamation mark";
3772 arg = truthvalue_conversion (arg);
3773 val = invert_truthvalue (arg);
3774 if (val) return val;
3780 case PREINCREMENT_EXPR:
3781 case POSTINCREMENT_EXPR:
3782 case PREDECREMENT_EXPR:
3783 case POSTDECREMENT_EXPR:
3784 /* Handle complex lvalues (when permitted)
3785 by reduction to simpler cases. */
3787 val = unary_complex_lvalue (code, arg);
3791 /* Report invalid types. */
3795 arg = default_conversion (arg);
3796 typecode = TREE_CODE (TREE_TYPE (arg));
3799 if (typecode != POINTER_TYPE
3800 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3802 if (code == PREINCREMENT_EXPR)
3803 errstring ="no pre-increment operator for type";
3804 else if (code == POSTINCREMENT_EXPR)
3805 errstring ="no post-increment operator for type";
3806 else if (code == PREDECREMENT_EXPR)
3807 errstring ="no pre-decrement operator for type";
3809 errstring ="no post-decrement operator for type";
3813 /* Report something read-only. */
3815 if (TYPE_READONLY (TREE_TYPE (arg))
3816 || TREE_READONLY (arg))
3817 readonly_error (arg, ((code == PREINCREMENT_EXPR
3818 || code == POSTINCREMENT_EXPR)
3819 ? "increment" : "decrement"),
3824 tree result_type = TREE_TYPE (arg);
3826 arg = get_unwidened (arg, 0);
3827 argtype = TREE_TYPE (arg);
3829 /* ARM $5.2.5 last annotation says this should be forbidden. */
3830 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
3831 pedwarn ("ANSI C++ forbids %sing an enum",
3832 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3833 ? "increment" : "decrement");
3835 /* Compute the increment. */
3837 if (typecode == POINTER_TYPE)
3839 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
3840 if (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
3841 || tmp == VOID_TYPE || tmp == OFFSET_TYPE)
3842 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
3843 ((code == PREINCREMENT_EXPR
3844 || code == POSTINCREMENT_EXPR)
3845 ? "increment" : "decrement"), argtype);
3846 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
3849 inc = integer_one_node;
3851 inc = convert (argtype, inc);
3853 /* Handle incrementing a cast-expression. */
3855 switch (TREE_CODE (arg))
3860 case FIX_TRUNC_EXPR:
3861 case FIX_FLOOR_EXPR:
3862 case FIX_ROUND_EXPR:
3865 tree incremented, modify, value;
3866 pedantic_lvalue_warning (CONVERT_EXPR);
3867 arg = stabilize_reference (arg);
3868 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3871 value = save_expr (arg);
3872 incremented = build (((code == PREINCREMENT_EXPR
3873 || code == POSTINCREMENT_EXPR)
3874 ? PLUS_EXPR : MINUS_EXPR),
3875 argtype, value, inc);
3876 TREE_SIDE_EFFECTS (incremented) = 1;
3877 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3878 return build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3882 if (TREE_CODE (arg) == OFFSET_REF)
3883 arg = resolve_offset_ref (arg);
3885 /* Complain about anything else that is not a true lvalue. */
3886 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3887 || code == POSTINCREMENT_EXPR)
3888 ? "increment" : "decrement")))
3889 return error_mark_node;
3891 val = build (code, TREE_TYPE (arg), arg, inc);
3892 TREE_SIDE_EFFECTS (val) = 1;
3893 return convert (result_type, val);
3897 /* Note that this operation never does default_conversion
3898 regardless of NOCONVERT. */
3900 if (TREE_REFERENCE_EXPR (arg))
3902 error ("references are not lvalues");
3903 return error_mark_node;
3905 else if (typecode == REFERENCE_TYPE)
3907 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
3908 TREE_REFERENCE_EXPR (arg) = 1;
3911 else if (TREE_CODE (arg) == FUNCTION_DECL
3913 && DECL_CONTEXT (arg) == NULL_TREE
3914 && IDENTIFIER_LENGTH (DECL_NAME (arg)) == 4
3915 && IDENTIFIER_POINTER (DECL_NAME (arg))[0] == 'm'
3916 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg)), "main"))
3919 error ("attempt to take address of function `main'");
3920 return error_mark_node;
3923 /* Let &* cancel out to simplify resulting code. */
3924 if (TREE_CODE (arg) == INDIRECT_REF)
3926 /* We don't need to have `current_class_decl' wrapped in a
3927 NON_LVALUE_EXPR node. */
3929 return current_class_decl;
3931 /* Keep `default_conversion' from converting if
3932 ARG is of REFERENCE_TYPE. */
3933 arg = TREE_OPERAND (arg, 0);
3934 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
3936 if (TREE_CODE (arg) == VAR_DECL && DECL_INITIAL (arg)
3937 && !TREE_SIDE_EFFECTS (DECL_INITIAL (arg)))
3938 arg = DECL_INITIAL (arg);
3939 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
3940 TREE_REFERENCE_EXPR (arg) = 1;
3941 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3943 else if (lvalue_p (arg))
3944 /* Don't let this be an lvalue. */
3945 return non_lvalue (arg);
3949 /* For &x[y], return x+y */
3950 if (TREE_CODE (arg) == ARRAY_REF)
3952 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3953 return error_mark_node;
3954 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3955 TREE_OPERAND (arg, 1), 1);
3958 /* For &(++foo), we are really taking the address of the variable
3959 being acted upon by the increment/decrement operator. ARM $5.3.1
3960 However, according to ARM $5.2.5, we don't allow postfix ++ and
3961 --, since the prefix operators return lvalues, but the postfix
3962 operators do not. */
3963 if (TREE_CODE (arg) == PREINCREMENT_EXPR
3964 || TREE_CODE (arg) == PREDECREMENT_EXPR)
3965 arg = TREE_OPERAND (arg, 0);
3967 /* Uninstantiated types are all functions. Taking the
3968 address of a function is a no-op, so just return the
3971 if (TREE_CODE (arg) == IDENTIFIER_NODE
3972 && IDENTIFIER_OPNAME_P (arg))
3974 my_friendly_abort (117);
3975 /* We don't know the type yet, so just work around the problem.
3976 We know that this will resolve to an lvalue. */
3977 return build1 (ADDR_EXPR, unknown_type_node, arg);
3980 if (TREE_CODE (arg) == TREE_LIST)
3982 /* Look at methods with only this name. */
3983 if (TREE_CODE (TREE_VALUE (arg)) == FUNCTION_DECL)
3985 tree targ = TREE_VALUE (arg);
3987 /* If this function is unique, or it is a unique
3988 constructor, we can take its address easily. */
3989 if (DECL_CHAIN (targ) == NULL_TREE
3990 || (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (targ))
3991 && DECL_CHAIN (DECL_CHAIN (targ)) == NULL_TREE))
3993 if (DECL_CHAIN (targ))
3994 targ = DECL_CHAIN (targ);
3995 if (DECL_CLASS_CONTEXT (targ))
3996 targ = build (OFFSET_REF, TREE_TYPE (targ), C_C_D, targ);
3998 val = unary_complex_lvalue (ADDR_EXPR, targ);
4003 /* This possible setting of TREE_CONSTANT is what makes it possible
4004 with an initializer list to emit the entire thing in the data
4005 section, rather than a run-time initialization. */
4006 arg = build1 (ADDR_EXPR, unknown_type_node, arg);
4008 TREE_CONSTANT (arg) = 1;
4011 if (TREE_CHAIN (arg) == NULL_TREE
4012 && TREE_CODE (TREE_VALUE (arg)) == TREE_LIST
4013 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg))) == NULL_TREE)
4015 /* Unique overloaded member function. */
4016 return build_unary_op (ADDR_EXPR, TREE_VALUE (TREE_VALUE (arg)), 0);
4018 return build1 (ADDR_EXPR, unknown_type_node, arg);
4021 /* Handle complex lvalues (when permitted)
4022 by reduction to simpler cases. */
4023 val = unary_complex_lvalue (code, arg);
4027 #if 0 /* Turned off because inconsistent;
4028 float f; *&(int)f = 3.4 stores in int format
4029 whereas (int)f = 3.4 stores in float format. */
4030 /* Address of a cast is just a cast of the address
4031 of the operand of the cast. */
4032 switch (TREE_CODE (arg))
4037 case FIX_TRUNC_EXPR:
4038 case FIX_FLOOR_EXPR:
4039 case FIX_ROUND_EXPR:
4042 pedwarn ("ANSI C++ forbids taking the address of a cast expression");
4043 return convert (build_pointer_type (TREE_TYPE (arg)),
4044 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0));
4048 /* Allow the address of a constructor if all the elements
4050 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
4052 /* Anything not already handled and not a true memory reference
4054 else if (typecode != FUNCTION_TYPE
4055 && typecode != METHOD_TYPE
4056 && !lvalue_or_else (arg, "unary `&'"))
4057 return error_mark_node;
4059 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4060 argtype = TREE_TYPE (arg);
4061 /* If the lvalue is const or volatile,
4062 merge that into the type that the address will point to. */
4063 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
4064 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
4066 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4067 argtype = build_type_variant (argtype,
4068 TREE_READONLY (arg),
4069 TREE_THIS_VOLATILE (arg));
4072 argtype = build_pointer_type (argtype);
4074 if (mark_addressable (arg) == 0)
4075 return error_mark_node;
4080 if (TREE_CODE (arg) == COMPONENT_REF)
4081 addr = build_component_addr (arg, argtype,
4082 "attempt to take address of bit-field structure member `%s'");
4084 addr = build1 (code, argtype, arg);
4086 /* Address of a static or external variable or
4087 function counts as a constant */
4089 TREE_CONSTANT (addr) = 1;
4097 argtype = TREE_TYPE (arg);
4098 return fold (build1 (code, argtype, arg));
4102 return error_mark_node;
4105 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4106 convert ARG with the same conversions in the same order
4107 and return the result. */
4110 convert_sequence (conversions, arg)
4114 switch (TREE_CODE (conversions))
4119 case FIX_TRUNC_EXPR:
4120 case FIX_FLOOR_EXPR:
4121 case FIX_ROUND_EXPR:
4123 return convert (TREE_TYPE (conversions),
4124 convert_sequence (TREE_OPERAND (conversions, 0),
4132 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4133 for certain kinds of expressions which are not really lvalues
4134 but which we can accept as lvalues.
4136 If ARG is not a kind of expression we can handle, return zero. */
4139 unary_complex_lvalue (code, arg)
4140 enum tree_code code;
4143 /* Handle (a, b) used as an "lvalue". */
4144 if (TREE_CODE (arg) == COMPOUND_EXPR)
4146 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4147 pedantic_lvalue_warning (COMPOUND_EXPR);
4148 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4149 TREE_OPERAND (arg, 0), real_result);
4152 /* Handle (a ? b : c) used as an "lvalue". */
4153 if (TREE_CODE (arg) == COND_EXPR)
4155 pedantic_lvalue_warning (COND_EXPR);
4156 return rationalize_conditional_expr (code, arg);
4159 if (code != ADDR_EXPR)
4162 /* Handle (a = b) used as an "lvalue" for `&'. */
4163 if (TREE_CODE (arg) == MODIFY_EXPR
4164 || TREE_CODE (arg) == INIT_EXPR)
4166 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4167 return build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4170 if (TREE_CODE (arg) == WITH_CLEANUP_EXPR)
4172 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4173 real_result = build (WITH_CLEANUP_EXPR, TREE_TYPE (real_result),
4174 real_result, 0, TREE_OPERAND (arg, 2));
4178 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4179 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4180 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4182 /* The representation of something of type OFFSET_TYPE
4183 is really the representation of a pointer to it.
4184 Here give the representation its true type. */
4188 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4190 if (TREE_CODE (arg) != OFFSET_REF)
4193 t = TREE_OPERAND (arg, 1);
4195 if (TREE_CODE (t) == FUNCTION_DECL) /* Check all this code for right semantics. */
4196 return build_unary_op (ADDR_EXPR, t, 0);
4197 if (TREE_CODE (t) == VAR_DECL)
4198 return build_unary_op (ADDR_EXPR, t, 0);
4201 /* Can't build a pointer to member if the member must
4202 go through virtual base classes. */
4203 if (virtual_member (DECL_FIELD_CONTEXT (t),
4204 CLASSTYPE_VBASECLASSES (TREE_TYPE (TREE_OPERAND (arg, 0)))))
4206 sorry ("pointer to member via virtual baseclass");
4207 return error_mark_node;
4210 if (TREE_OPERAND (arg, 0)
4211 && (TREE_CODE (TREE_OPERAND (arg, 0)) != NOP_EXPR
4212 || TREE_OPERAND (TREE_OPERAND (arg, 0), 0) != error_mark_node))
4214 /* Don't know if this should return address to just
4215 _DECL, or actual address resolved in this expression. */
4216 sorry ("address of bound pointer-to-member expression");
4217 return error_mark_node;
4220 return convert (build_pointer_type (TREE_TYPE (arg)),
4221 size_binop (EASY_DIV_EXPR,
4222 DECL_FIELD_BITPOS (t),
4223 size_int (BITS_PER_UNIT)));
4227 if (TREE_CODE (arg) == OFFSET_REF)
4229 tree left = TREE_OPERAND (arg, 0), left_addr;
4230 tree right_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 1), 0);
4233 if (current_class_decl)
4234 left_addr = current_class_decl;
4237 error ("no `this' for pointer to member");
4238 return error_mark_node;
4241 left_addr = build_unary_op (ADDR_EXPR, left, 0);
4243 return build (PLUS_EXPR, build_pointer_type (TREE_TYPE (arg)),
4244 build1 (NOP_EXPR, integer_type_node, left_addr),
4245 build1 (NOP_EXPR, integer_type_node, right_addr));
4248 /* We permit compiler to make function calls returning
4249 objects of aggregate type look like lvalues. */
4253 if (TREE_CODE (targ) == SAVE_EXPR)
4254 targ = TREE_OPERAND (targ, 0);
4256 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4258 if (TREE_CODE (arg) == SAVE_EXPR)
4261 targ = build_cplus_new (TREE_TYPE (arg), arg, 1);
4262 return build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (arg)), targ);
4265 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4266 return build (SAVE_EXPR, TYPE_POINTER_TO (TREE_TYPE (arg)),
4267 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4269 /* We shouldn't wrap WITH_CLEANUP_EXPRs inside of SAVE_EXPRs, but in case
4270 we do, here's how to handle it. */
4271 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == WITH_CLEANUP_EXPR)
4274 /* Not really a bug, but something to turn on when testing. */
4275 compiler_error ("WITH_CLEANUP_EXPR wrapped in SAVE_EXPR");
4277 return unary_complex_lvalue (ADDR_EXPR, targ);
4281 /* Don't let anything else be handled specially. */
4285 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
4286 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
4289 pedantic_lvalue_warning (code)
4290 enum tree_code code;
4293 pedwarn ("ANSI C++ forbids use of %s expressions as lvalues",
4294 code == COND_EXPR ? "conditional"
4295 : code == COMPOUND_EXPR ? "compound" : "cast");
4298 /* Mark EXP saying that we need to be able to take the
4299 address of it; it should not be allocated in a register.
4300 Value is 1 if successful.
4302 C++: we do not allow `current_class_decl' to be addressable. */
4305 mark_addressable (exp)
4308 register tree x = exp;
4310 if (TREE_ADDRESSABLE (x) == 1)
4314 switch (TREE_CODE (x))
4319 x = TREE_OPERAND (x, 0);
4323 if (x == current_class_decl)
4325 error ("address of `this' not available");
4326 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4327 put_var_into_stack (x);
4332 && TREE_READONLY (x)
4333 && DECL_RTL (x) != 0
4334 && ! decl_in_memory_p (x))
4336 /* We thought this would make a good constant variable,
4337 but we were wrong. */
4338 push_obstacks_nochange ();
4339 end_temporary_allocation ();
4341 TREE_ASM_WRITTEN (x) = 0;
4343 rest_of_decl_compilation (x, 0, IDENTIFIER_LOCAL_VALUE (x) == 0, 0);
4344 TREE_ADDRESSABLE (x) = 1;
4350 /* Caller should not be trying to mark initialized
4351 constant fields addressable. */
4352 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4353 || DECL_IN_AGGR_P (x) == 0
4355 || DECL_EXTERNAL (x), 314);
4359 /* For C++, we don't warn about taking the address of a register
4360 variable for CONST_DECLs; ARM p97 explicitly says it's okay. */
4361 put_var_into_stack (x);
4362 TREE_ADDRESSABLE (x) = 1;
4366 /* We have to test both conditions here. The first may
4367 be non-zero in the case of processing a default function.
4368 The second may be non-zero in the case of a template function. */
4369 x = DECL_MAIN_VARIANT (x);
4370 if ((DECL_INLINE (x) || DECL_PENDING_INLINE_INFO (x))
4371 && (DECL_CONTEXT (x) == NULL_TREE
4372 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (x))) != 't'
4373 || ! CLASSTYPE_INTERFACE_ONLY (DECL_CONTEXT (x))))
4375 mark_inline_for_output (x);
4376 if (x == current_function_decl)
4377 DECL_EXTERNAL (x) = 0;
4379 TREE_ADDRESSABLE (x) = 1;
4381 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4389 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4392 build_x_conditional_expr (ifexp, op1, op2)
4393 tree ifexp, op1, op2;
4395 tree rval = NULL_TREE;
4397 /* See comments in `build_x_binary_op'. */
4399 rval = build_opfncall (COND_EXPR, LOOKUP_SPECULATIVELY, ifexp, op1, op2);
4401 return build_opfncall (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4403 return build_conditional_expr (ifexp, op1, op2);
4407 build_conditional_expr (ifexp, op1, op2)
4408 tree ifexp, op1, op2;
4410 register tree type1;
4411 register tree type2;
4412 register enum tree_code code1;
4413 register enum tree_code code2;
4414 register tree result_type = NULL_TREE;
4415 tree orig_op1 = op1, orig_op2 = op2;
4417 /* If second operand is omitted, it is the same as the first one;
4418 make sure it is calculated only once. */
4422 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4423 ifexp = op1 = save_expr (ifexp);
4426 ifexp = truthvalue_conversion (default_conversion (ifexp));
4428 if (TREE_CODE (ifexp) == ERROR_MARK)
4429 return error_mark_node;
4431 op1 = require_instantiated_type (TREE_TYPE (op2), op1, error_mark_node);
4432 if (op1 == error_mark_node)
4433 return error_mark_node;
4434 op2 = require_instantiated_type (TREE_TYPE (op1), op2, error_mark_node);
4435 if (op2 == error_mark_node)
4436 return error_mark_node;
4438 /* C++: REFERENCE_TYPES must be dereferenced. */
4439 type1 = TREE_TYPE (op1);
4440 code1 = TREE_CODE (type1);
4441 type2 = TREE_TYPE (op2);
4442 code2 = TREE_CODE (type2);
4444 if (code1 == REFERENCE_TYPE)
4446 op1 = convert_from_reference (op1);
4447 type1 = TREE_TYPE (op1);
4448 code1 = TREE_CODE (type1);
4450 if (code2 == REFERENCE_TYPE)
4452 op2 = convert_from_reference (op2);
4453 type2 = TREE_TYPE (op2);
4454 code2 = TREE_CODE (type2);
4457 #if 1 /* Produces wrong result if within sizeof. Sorry. */
4458 /* Don't promote the operands separately if they promote
4459 the same way. Return the unpromoted type and let the combined
4460 value get promoted if necessary. */
4462 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
4463 && code2 != ARRAY_TYPE
4465 /* For C++, let the enumeral type come through. */
4466 && code2 != ENUMERAL_TYPE
4468 && code2 != FUNCTION_TYPE
4469 && code2 != METHOD_TYPE)
4473 if (TREE_CONSTANT (ifexp)
4474 && (TREE_CODE (ifexp) == INTEGER_CST
4475 || TREE_CODE (ifexp) == ADDR_EXPR))
4476 return (integer_zerop (ifexp) ? op2 : op1);
4478 if (TREE_CODE (op1) == CONST_DECL)
4479 op1 = DECL_INITIAL (op1);
4480 else if (TREE_READONLY_DECL_P (op1))
4481 op1 = decl_constant_value (op1);
4482 if (TREE_CODE (op2) == CONST_DECL)
4483 op2 = DECL_INITIAL (op2);
4484 else if (TREE_READONLY_DECL_P (op2))
4485 op2 = decl_constant_value (op2);
4487 type1 = build_type_variant
4489 TREE_READONLY (op1) || TREE_READONLY (op2),
4490 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4491 /* ??? This is a kludge to deal with the fact that
4492 we don't sort out integers and enums properly, yet. */
4493 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
4494 if (TREE_TYPE (result) != type1)
4495 result = build1 (NOP_EXPR, type1, result);
4500 /* They don't match; promote them both and then try to reconcile them.
4501 But don't permit mismatching enum types. */
4502 if (code1 == ENUMERAL_TYPE)
4504 if (code2 == ENUMERAL_TYPE)
4506 message_2_types (error, "enumeral mismatch in conditional expression: `%s' vs `%s'", type1, type2);
4507 return error_mark_node;
4509 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2))
4510 warning ("enumeral and non-enumeral type in conditional expression");
4512 else if (extra_warnings
4513 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1))
4514 warning ("enumeral and non-enumeral type in conditional expression");
4516 if (code1 != VOID_TYPE)
4518 op1 = default_conversion (op1);
4519 type1 = TREE_TYPE (op1);
4520 code1 = TREE_CODE (type1);
4522 if (code2 != VOID_TYPE)
4524 op2 = default_conversion (op2);
4525 type2 = TREE_TYPE (op2);
4526 code2 = TREE_CODE (type2);
4529 /* Quickly detect the usual case where op1 and op2 have the same type
4531 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4534 result_type = type1;
4536 result_type = build_type_variant
4538 TREE_READONLY (op1) || TREE_READONLY (op2),
4539 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4541 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
4542 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
4544 result_type = common_type (type1, type2);
4546 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4548 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
4549 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
4550 result_type = void_type_node;
4552 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4554 if (comp_target_types (type1, type2, 1))
4555 result_type = common_type (type1, type2);
4556 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
4557 && TREE_CODE (orig_op1) != NOP_EXPR)
4558 result_type = qualify_type (type2, type1);
4559 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
4560 && TREE_CODE (orig_op2) != NOP_EXPR)
4561 result_type = qualify_type (type1, type2);
4562 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
4564 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
4565 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4566 result_type = qualify_type (type1, type2);
4568 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
4570 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
4571 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4572 result_type = qualify_type (type2, type1);
4575 else if (comptypes (type2, type1, 0))
4576 result_type = type2;
4577 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
4578 && IS_AGGR_TYPE (TREE_TYPE (type2))
4579 && (result_type = common_base_type (TREE_TYPE (type1), TREE_TYPE (type2))))
4581 if (result_type == error_mark_node)
4583 message_2_types (error, "common base type of types `%s' and `%s' is ambiguous",
4584 TREE_TYPE (type1), TREE_TYPE (type2));
4585 result_type = ptr_type_node;
4587 else result_type = TYPE_POINTER_TO (result_type);
4591 pedwarn ("pointer type mismatch in conditional expression");
4592 result_type = ptr_type_node;
4595 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4597 if (!integer_zerop (op2))
4598 warning ("pointer/integer type mismatch in conditional expression");
4601 op2 = null_pointer_node;
4602 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
4603 pedwarn ("ANSI C++ forbids conditional expr between 0 and function pointer");
4605 result_type = type1;
4607 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4609 if (!integer_zerop (op1))
4610 warning ("pointer/integer type mismatch in conditional expression");
4613 op1 = null_pointer_node;
4614 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
4615 pedwarn ("ANSI C++ forbids conditional expr between 0 and function pointer");
4617 result_type = type2;
4618 op1 = null_pointer_node;
4623 /* The match does not look good. If either is
4624 an aggregate value, try converting to a scalar type. */
4625 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
4627 message_2_types (error, "aggregate mismatch in conditional expression: `%s' vs `%s'", type1, type2);
4628 return error_mark_node;
4630 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
4632 tree tmp = build_type_conversion (CONVERT_EXPR, type2, op1, 0);
4633 if (tmp == NULL_TREE)
4635 cp_error ("aggregate type `%T' could not convert on lhs of `:'", type1);
4636 return error_mark_node;
4638 if (tmp == error_mark_node)
4639 error ("ambiguous pointer conversion");
4640 result_type = type2;
4643 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
4645 tree tmp = build_type_conversion (CONVERT_EXPR, type1, op2, 0);
4646 if (tmp == NULL_TREE)
4648 cp_error ("aggregate type `%T' could not convert on rhs of `:'", type2);
4649 return error_mark_node;
4651 if (tmp == error_mark_node)
4652 error ("ambiguous pointer conversion");
4653 result_type = type1;
4656 else if (flag_cond_mismatch)
4657 result_type = void_type_node;
4660 error ("type mismatch in conditional expression");
4661 return error_mark_node;
4665 if (result_type != TREE_TYPE (op1))
4666 op1 = convert_and_check (result_type, op1);
4667 if (result_type != TREE_TYPE (op2))
4668 op2 = convert_and_check (result_type, op2);
4671 /* XXX delete me, I've been here for years. */
4672 if (IS_AGGR_TYPE_CODE (code1))
4674 result_type = TREE_TYPE (op1);
4675 if (TREE_CONSTANT (ifexp))
4676 return (integer_zerop (ifexp) ? op2 : op1);
4678 if (TYPE_MODE (result_type) == BLKmode)
4680 register tree tempvar
4681 = build_decl (VAR_DECL, NULL_TREE, result_type);
4682 register tree xop1 = build_modify_expr (tempvar, NOP_EXPR, op1);
4683 register tree xop2 = build_modify_expr (tempvar, NOP_EXPR, op2);
4684 register tree result = fold (build (COND_EXPR, result_type,
4685 ifexp, xop1, xop2));
4687 layout_decl (tempvar, 0);
4688 /* No way to handle variable-sized objects here.
4689 I fear that the entire handling of BLKmode conditional exprs
4690 needs to be redone. */
4691 my_friendly_assert (TREE_CONSTANT (DECL_SIZE (tempvar)), 315);
4693 = assign_stack_local (DECL_MODE (tempvar),
4694 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
4695 + BITS_PER_UNIT - 1)
4699 TREE_SIDE_EFFECTS (result)
4700 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
4701 | TREE_SIDE_EFFECTS (op2);
4702 return build (COMPOUND_EXPR, result_type, result, tempvar);
4707 if (TREE_CONSTANT (ifexp))
4708 return integer_zerop (ifexp) ? op2 : op1;
4710 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
4713 /* Handle overloading of the ',' operator when needed. Otherwise,
4714 this function just builds an expression list. */
4716 build_x_compound_expr (list)
4719 tree rest = TREE_CHAIN (list);
4722 if (rest == NULL_TREE)
4723 return build_compound_expr (list);
4725 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
4726 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4728 return build_x_compound_expr (tree_cons (NULL_TREE, result, TREE_CHAIN (rest)));
4729 return build_compound_expr (tree_cons (NULL_TREE, TREE_VALUE (list),
4730 build_tree_list (NULL_TREE, build_x_compound_expr (rest))));
4733 /* Given a list of expressions, return a compound expression
4734 that performs them all and returns the value of the last of them. */
4737 build_compound_expr (list)
4742 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
4743 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
4745 if (TREE_CHAIN (list) == 0)
4747 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4748 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4749 if (TREE_CODE (list) == NOP_EXPR
4750 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
4751 list = TREE_OPERAND (list, 0);
4753 /* Convert arrays to pointers. */
4754 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
4755 return default_conversion (TREE_VALUE (list));
4757 return TREE_VALUE (list);
4760 rest = build_compound_expr (TREE_CHAIN (list));
4762 /* When pedantic, a compound expression can be neither an lvalue
4763 nor an integer constant expression. */
4764 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
4767 return build (COMPOUND_EXPR, TREE_TYPE (rest),
4768 break_out_cleanups (TREE_VALUE (list)), rest);
4771 /* Build an expression representing a cast to type TYPE of expression EXPR. */
4774 build_c_cast (type, expr)
4778 register tree value = expr;
4780 if (type == error_mark_node || expr == error_mark_node)
4781 return error_mark_node;
4783 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4784 Strip such NOP_EXPRs, since VALUE is being used in non-lvalue context. */
4785 if (TREE_CODE (value) == NOP_EXPR
4786 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
4787 value = TREE_OPERAND (value, 0);
4789 if (TREE_TYPE (expr)
4790 && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
4791 && TREE_CODE (type) != OFFSET_TYPE)
4792 value = resolve_offset_ref (value);
4794 if (TREE_CODE (type) == ARRAY_TYPE)
4796 /* Allow casting from T1* to T2[] because Cfront allows it.
4797 NIHCL uses it. It is not valid ANSI C however, and hence, not
4799 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
4802 pedwarn ("ANSI C++ forbids casting to an array type");
4803 type = build_pointer_type (TREE_TYPE (type));
4807 error ("ANSI C++ forbids casting to an array type");
4808 return error_mark_node;
4812 if (IS_SIGNATURE (type))
4814 error ("cast specifies signature type");
4815 return error_mark_node;
4818 if (TREE_TYPE (value)
4819 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4821 /* For C++, we must copy the constness of TYPE into VALUE. */
4822 if (TREE_READONLY (value) != TYPE_READONLY (type))
4824 value = copy_node (value);
4825 TREE_READONLY (value) = TYPE_READONLY (type);
4829 if (TREE_CODE (type) == RECORD_TYPE
4830 || TREE_CODE (type) == UNION_TYPE)
4831 pedwarn ("ANSI C++ forbids casting nonscalar to the same type");
4836 /* If there's only one function in the overloaded space,
4838 if (TREE_CODE (value) == TREE_LIST
4839 && TREE_CHAIN (value) == NULL_TREE)
4840 value = TREE_VALUE (value);
4842 /* Make up for the fact that we do not always perform
4843 `default_conversion' anymore. */
4844 if (TREE_READONLY_DECL_P (value))
4845 value = decl_constant_value (value);
4847 if (TREE_TYPE (value) == NULL_TREE
4848 || type_unknown_p (value))
4850 value = instantiate_type (type, value, 1);
4852 if (value == error_mark_node)
4853 return error_mark_node;
4859 /* Convert functions and arrays to pointers and
4860 convert references to their expanded types,
4861 but don't convert any other types. */
4862 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
4863 || TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
4864 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
4865 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
4866 value = default_conversion (value);
4867 otype = TREE_TYPE (value);
4869 /* Optionally warn about potentially worrisome casts. */
4872 && TREE_CODE (type) == POINTER_TYPE
4873 && TREE_CODE (otype) == POINTER_TYPE)
4875 /* For C++ we make these regular warnings, rather than
4876 softening them into pedwarns. */
4877 if (TYPE_VOLATILE (TREE_TYPE (otype))
4878 && ! TYPE_VOLATILE (TREE_TYPE (type)))
4879 warning ("cast discards `volatile' from pointer target type");
4880 if (TYPE_READONLY (TREE_TYPE (otype))
4881 && ! TYPE_READONLY (TREE_TYPE (type)))
4882 warning ("cast discards `const' from pointer target type");
4885 /* Warn about possible alignment problems. */
4886 if (STRICT_ALIGNMENT && warn_cast_align
4887 && TREE_CODE (type) == POINTER_TYPE
4888 && TREE_CODE (otype) == POINTER_TYPE
4889 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4890 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4891 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4892 warning ("cast increases required alignment of target type");
4895 if (TREE_CODE (type) == INTEGER_TYPE
4896 && TREE_CODE (otype) == POINTER_TYPE
4897 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4898 warning ("cast from pointer to integer of different size");
4900 if (TREE_CODE (type) == POINTER_TYPE
4901 && TREE_CODE (otype) == INTEGER_TYPE
4902 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4903 /* Don't warn about converting 0 to pointer,
4904 provided the 0 was explicit--not cast or made by folding. */
4905 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
4906 warning ("cast to pointer from integer of different size");
4910 value = convert_force (type, value);
4912 /* Ignore any integer overflow caused by the cast. */
4913 if (TREE_CODE (value) == INTEGER_CST)
4915 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4916 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
4920 /* Always produce some operator for an explicit cast,
4921 so we can tell (for -pedantic) that the cast is no lvalue.
4922 Also, pedantically, don't let (void *) (FOO *) 0 be a null
4923 pointer constant. */
4926 && TREE_CODE (value) == INTEGER_CST
4927 && TREE_CODE (expr) == INTEGER_CST
4928 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE))
4930 tree nvalue = build1 (NOP_EXPR, type, value);
4931 TREE_CONSTANT (nvalue) = TREE_CONSTANT (value);
4938 /* Build an assignment expression of lvalue LHS from value RHS.
4940 In C++, if the left hand side of the assignment is a REFERENCE_TYPE,
4941 that reference becomes deferenced down to it base type. */
4943 /* Return a reference to the BASE_INDEX part of EXPR. TYPE is
4944 the type to which BASE_INDEX applies. */
4946 get_base_ref (type, base_index, expr)
4951 tree binfos = TYPE_BINFO_BASETYPES (type);
4952 tree base_binfo = TREE_VEC_ELT (binfos, base_index);
4955 if (TREE_CODE (expr) == ARRAY_REF
4956 || ! BINFO_OFFSET_ZEROP (base_binfo)
4957 || TREE_VIA_VIRTUAL (base_binfo)
4958 || TYPE_MODE (type) != TYPE_MODE (BINFO_TYPE (base_binfo)))
4960 tree addr = build_unary_op (ADDR_EXPR, expr, 0);
4961 ref = build_indirect_ref (convert_pointer_to (base_binfo, addr),
4966 ref = copy_node (expr);
4967 TREE_TYPE (ref) = BINFO_TYPE (base_binfo);
4973 /* Build an assignment expression of lvalue LHS from value RHS.
4974 MODIFYCODE is the code for a binary operator that we use
4975 to combine the old value of LHS with RHS to get the new value.
4976 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4978 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
4980 `build_modify_expr_1' implements recursive part of memberwise
4981 assignment operation. */
4983 build_modify_expr_1 (lhs, modifycode, rhs, basetype_path)
4985 enum tree_code modifycode;
4988 register tree result;
4990 tree lhstype = TREE_TYPE (lhs);
4991 tree olhstype = lhstype;
4993 /* Avoid duplicate error messages from operands that had errors. */
4994 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4995 return error_mark_node;
4997 /* If a binary op has been requested, combine the old LHS value with the RHS
4998 producing the value we should actually store into the LHS. */
5000 if (modifycode == INIT_EXPR)
5002 else if (modifycode == NOP_EXPR)
5004 /* must deal with overloading of `operator=' here. */
5005 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5006 lhstype = TREE_TYPE (lhstype);
5012 lhs = stabilize_reference (lhs);
5013 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5014 modifycode = NOP_EXPR;
5017 /* If storing into a structure or union member,
5018 it has probably been given type `int'.
5019 Compute the type that would go with
5020 the actual amount of storage the member occupies. */
5022 if (TREE_CODE (lhs) == COMPONENT_REF
5023 && (TREE_CODE (lhstype) == INTEGER_TYPE
5024 || TREE_CODE (lhstype) == REAL_TYPE
5025 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5026 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5028 /* C++: The semantics of C++ differ from those of C when an
5029 assignment of an aggregate is desired. Assignment in C++ is
5030 now defined as memberwise assignment of non-static members
5031 and base class objects. This rule applies recursively
5032 until a member of a built-in type is found.
5034 Also, we cannot do a bit-wise copy of aggregates which
5035 contain virtual function table pointers. Those
5036 pointer values must be preserved through the copy.
5037 However, this is handled in expand_expr, and not here.
5038 This is because much better code can be generated at
5039 that stage than this one. */
5040 if (TREE_CODE (lhstype) == RECORD_TYPE
5041 && TYPE_LANG_SPECIFIC (lhstype)
5042 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5047 /* Perform operation on object. */
5048 if (modifycode == INIT_EXPR && TYPE_HAS_INIT_REF (lhstype))
5050 result = build_method_call (lhs, constructor_name_full (lhstype),
5051 build_tree_list (NULL_TREE, rhs),
5052 basetype_path, LOOKUP_NORMAL);
5053 return build_indirect_ref (result, NULL_PTR);
5055 else if (modifycode == NOP_EXPR)
5057 /* `operator=' is not an inheritable operator; see 13.4.3. */
5058 if (TYPE_LANG_SPECIFIC (lhstype) && TYPE_HAS_ASSIGNMENT (lhstype))
5060 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5061 lhs, rhs, make_node (NOP_EXPR));
5062 if (result == NULL_TREE)
5063 return error_mark_node;
5068 if (TYPE_USES_VIRTUAL_BASECLASSES (lhstype)
5069 || (modifycode == NOP_EXPR && TYPE_GETS_ASSIGNMENT (lhstype))
5070 || (modifycode == INIT_EXPR && TYPE_GETS_INIT_REF (lhstype)))
5072 tree binfos = BINFO_BASETYPES (TYPE_BINFO (lhstype));
5075 if (binfos != NULL_TREE)
5076 /* Perform operation on each member, depth-first, left-right. */
5077 for (i = 0; i <= TREE_VEC_LENGTH (binfos)-1; i++)
5079 tree base_binfo = TREE_VEC_ELT (binfos, i);
5080 tree base_lhs, base_rhs;
5083 /* Assignments from virtual baseclasses handled elsewhere. */
5084 if (TREE_VIA_VIRTUAL (base_binfo))
5087 base_lhs = get_base_ref (lhstype, i, lhs);
5088 base_rhs = get_base_ref (lhstype, i, newrhs);
5090 BINFO_INHERITANCE_CHAIN (base_binfo) = basetype_path;
5092 = build_modify_expr_1 (base_lhs, modifycode, base_rhs,
5095 /* We either get back a compound stmt, or a simple one. */
5096 if (new_result && TREE_CODE (new_result) == TREE_LIST)
5097 new_result = build_compound_expr (new_result);
5098 result = tree_cons (NULL_TREE, new_result, result);
5101 for (elt = TYPE_FIELDS (lhstype); elt; elt = TREE_CHAIN (elt))
5103 tree vbases = NULL_TREE;
5104 tree elt_lhs, elt_rhs;
5106 if (TREE_CODE (elt) != FIELD_DECL)
5109 && (VFIELD_NAME_P (DECL_NAME (elt))
5110 || VBASE_NAME_P (DECL_NAME (elt))))
5113 if (TREE_READONLY (elt)
5114 || TREE_CODE (TREE_TYPE (elt)) == REFERENCE_TYPE)
5116 cp_error ("cannot generate default `%T::operator ='",
5118 if (TREE_CODE (TREE_TYPE (elt)) == REFERENCE_TYPE)
5119 cp_error_at ("because member `%#D' is a reference", elt);
5121 cp_error_at ("because member `%#D' is const", elt);
5123 return error_mark_node;
5126 if (IS_AGGR_TYPE (TREE_TYPE (elt))
5127 && TYPE_LANG_SPECIFIC (TREE_TYPE (elt)))
5128 vbases = CLASSTYPE_VBASECLASSES (TREE_TYPE (elt));
5130 elt_lhs = build (COMPONENT_REF, TREE_TYPE (elt), lhs, elt);
5131 elt_rhs = build (COMPONENT_REF, TREE_TYPE (elt), newrhs, elt);
5132 /* It is not always safe to go through `build_modify_expr_1'
5133 when performing element-wise copying. This is because
5134 an element may be of ARRAY_TYPE, which will not
5135 be properly copied as a naked element. */
5136 if (TREE_CODE (TREE_TYPE (elt)) == RECORD_TYPE
5137 && TYPE_LANG_SPECIFIC (TREE_TYPE (elt)))
5138 basetype_path = TYPE_BINFO (TREE_TYPE (elt));
5142 tree elt_lhs_addr = build_unary_op (ADDR_EXPR, elt_lhs, 0);
5143 tree elt_rhs_addr = build_unary_op (ADDR_EXPR, elt_rhs, 0);
5145 elt_lhs_addr = convert_pointer_to (vbases, elt_lhs_addr);
5146 elt_rhs_addr = convert_pointer_to (vbases, elt_rhs_addr);
5148 = tree_cons (NULL_TREE,
5150 (build_indirect_ref (elt_lhs_addr, NULL_PTR),
5152 build_indirect_ref (elt_rhs_addr, NULL_PTR),
5155 if (TREE_VALUE (result) == error_mark_node)
5156 return error_mark_node;
5157 vbases = TREE_CHAIN (vbases);
5159 elt_lhs = build_modify_expr_1 (elt_lhs, modifycode, elt_rhs,
5161 result = tree_cons (NULL_TREE, elt_lhs, result);
5165 return build_compound_expr (result);
5166 /* No fields to move. */
5167 return integer_zero_node;
5171 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5172 void_type_node, lhs, rhs);
5173 TREE_SIDE_EFFECTS (result) = 1;
5178 result = build_modify_expr (lhs, modifycode, newrhs);
5179 /* ARRAY_TYPEs cannot be converted to anything meaningful,
5180 and leaving it there screws up `build_compound_expr' when
5181 it tries to defaultly convert everything. */
5182 if (TREE_CODE (TREE_TYPE (result)) == ARRAY_TYPE)
5183 TREE_TYPE (result) = void_type_node;
5188 /* Taken from expr.c:
5189 Subroutine of expand_expr:
5190 record the non-copied parts (LIST) of an expr (LHS), and return a list
5191 which specifies the initial values of these parts. */
5194 init_noncopied_parts (lhs, list)
5201 for (tail = list; tail; tail = TREE_CHAIN (tail))
5202 if (TREE_CODE (TREE_VALUE (tail)) == TREE_LIST)
5203 parts = chainon (parts, init_noncopied_parts (lhs, TREE_VALUE (tail)));
5206 tree part = TREE_VALUE (tail);
5207 tree part_type = TREE_TYPE (part);
5208 tree to_be_initialized = build (COMPONENT_REF, part_type, lhs, part);
5209 parts = tree_cons (TREE_PURPOSE (tail), to_be_initialized, parts);
5214 /* Build an assignment expression of lvalue LHS from value RHS.
5215 MODIFYCODE is the code for a binary operator that we use
5216 to combine the old value of LHS with RHS to get the new value.
5217 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5219 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
5222 build_modify_expr (lhs, modifycode, rhs)
5224 enum tree_code modifycode;
5227 register tree result;
5229 tree lhstype = TREE_TYPE (lhs);
5230 tree olhstype = lhstype;
5232 /* Types that aren't fully specified cannot be used in assignments. */
5233 lhs = require_complete_type (lhs);
5235 /* Avoid duplicate error messages from operands that had errors. */
5236 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5237 return error_mark_node;
5239 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5240 Strip such NOP_EXPRs, since RHS is being used in non-lvalue context. */
5241 if (TREE_CODE (rhs) == NOP_EXPR
5242 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0)))
5243 rhs = TREE_OPERAND (rhs, 0);
5245 /* Decide early if we are going to protect RHS from GC
5246 before assigning it to LHS. */
5247 if (type_needs_gc_entry (TREE_TYPE (rhs))
5248 && ! value_safe_from_gc (lhs, rhs))
5249 rhs = protect_value_from_gc (lhs, rhs);
5253 /* Handle assignment to signature pointers/refs. */
5255 if (TYPE_LANG_SPECIFIC (lhstype) &&
5256 (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5258 return build_signature_pointer_constructor (lhs, rhs);
5261 /* Handle control structure constructs used as "lvalues". */
5263 switch (TREE_CODE (lhs))
5265 /* Handle --foo = 5; as these are valid constructs in C++ */
5266 case PREDECREMENT_EXPR:
5267 case PREINCREMENT_EXPR:
5268 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5269 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5270 stabilize_reference (TREE_OPERAND (lhs, 0)));
5271 return build (COMPOUND_EXPR, lhstype,
5273 build_modify_expr (TREE_OPERAND (lhs, 0),
5276 /* Handle (a, b) used as an "lvalue". */
5278 pedantic_lvalue_warning (COMPOUND_EXPR);
5279 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5281 if (TREE_CODE (newrhs) == ERROR_MARK)
5282 return error_mark_node;
5283 return build (COMPOUND_EXPR, lhstype,
5284 TREE_OPERAND (lhs, 0), newrhs);
5286 /* Handle (a ? b : c) used as an "lvalue". */
5288 pedantic_lvalue_warning (COND_EXPR);
5289 rhs = save_expr (rhs);
5291 /* Produce (a ? (b = rhs) : (c = rhs))
5292 except that the RHS goes through a save-expr
5293 so the code to compute it is only emitted once. */
5295 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5296 build_modify_expr (TREE_OPERAND (lhs, 1),
5298 build_modify_expr (TREE_OPERAND (lhs, 2),
5300 if (TREE_CODE (cond) == ERROR_MARK)
5302 /* Make sure the code to compute the rhs comes out
5303 before the split. */
5304 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5305 /* Case to void to suppress warning
5306 from warn_if_unused_value. */
5307 convert (void_type_node, rhs), cond);
5311 /* If a binary op has been requested, combine the old LHS value with the RHS
5312 producing the value we should actually store into the LHS. */
5314 if (modifycode == INIT_EXPR)
5316 if (TYPE_LANG_SPECIFIC (lhstype) && TYPE_HAS_CONSTRUCTOR (lhstype))
5318 result = build_method_call (lhs, constructor_name_full (lhstype),
5319 build_tree_list (NULL_TREE, rhs),
5320 NULL_TREE, LOOKUP_NORMAL);
5321 if (result == NULL_TREE)
5322 return error_mark_node;
5326 else if (modifycode == NOP_EXPR)
5328 /* must deal with overloading of `operator=' here. */
5329 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5330 lhstype = TREE_TYPE (lhstype);
5332 /* `operator=' is not an inheritable operator. */
5333 if (TYPE_LANG_SPECIFIC (lhstype) && TYPE_HAS_ASSIGNMENT (lhstype))
5335 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5336 lhs, rhs, make_node (NOP_EXPR));
5337 if (result == NULL_TREE)
5338 return error_mark_node;
5342 /* Treat `operator=' as an inheritable operator. */
5343 if (TYPE_LANG_SPECIFIC (lhstype) && TYPE_GETS_ASSIGNMENT (lhstype))
5345 tree orig_lhstype = lhstype;
5346 while (! TYPE_HAS_ASSIGNMENT (lhstype))
5348 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (lhstype);
5349 tree basetype = NULL_TREE;
5350 for (i = 0; i < n_baseclasses; i++)
5351 if (TYPE_GETS_ASSIGNMENT (TYPE_BINFO_BASETYPE (lhstype, i)))
5353 if (basetype != NULL_TREE)
5355 message_2_types (error, "base classes `%s' and `%s' both have operator ='",
5357 TYPE_BINFO_BASETYPE (lhstype, i));
5358 return error_mark_node;
5360 basetype = TYPE_BINFO_BASETYPE (lhstype, i);
5364 if (orig_lhstype != lhstype)
5366 lhs = build_indirect_ref (convert_pointer_to (lhstype,
5367 build_unary_op (ADDR_EXPR, lhs, 0)), NULL_PTR);
5368 if (lhs == error_mark_node)
5370 cp_error ("conversion to private basetype `%T'", lhstype);
5371 return error_mark_node;
5374 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5375 lhs, rhs, make_node (NOP_EXPR));
5376 if (result == NULL_TREE)
5377 return error_mark_node;
5383 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5385 /* This case must convert to some sort of lvalue that
5386 can participate in an op= operation. */
5389 if (build_default_binary_type_conversion (modifycode, &lhs_tmp, &rhs_tmp))
5391 lhs = stabilize_reference (lhs_tmp);
5392 /* Forget is was ever anything else. */
5393 olhstype = lhstype = TREE_TYPE (lhs);
5394 newrhs = build_binary_op (modifycode, lhs, rhs_tmp, 1);
5397 return error_mark_node;
5401 lhs = stabilize_reference (lhs);
5402 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5405 /* Handle a cast used as an "lvalue".
5406 We have already performed any binary operator using the value as cast.
5407 Now convert the result to the cast type of the lhs,
5408 and then true type of the lhs and store it there;
5409 then convert result back to the cast type to be the value
5410 of the assignment. */
5412 switch (TREE_CODE (lhs))
5417 case FIX_TRUNC_EXPR:
5418 case FIX_FLOOR_EXPR:
5419 case FIX_ROUND_EXPR:
5421 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5422 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5423 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5424 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5425 newrhs = default_conversion (newrhs);
5427 tree inner_lhs = TREE_OPERAND (lhs, 0);
5429 result = build_modify_expr (inner_lhs, NOP_EXPR,
5430 convert (TREE_TYPE (inner_lhs),
5431 convert (lhstype, newrhs)));
5432 if (TREE_CODE (result) == ERROR_MARK)
5434 return convert (TREE_TYPE (lhs), result);
5438 if (TREE_CODE (lhs) == OFFSET_REF)
5440 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5442 /* Static class member? */
5443 tree member = TREE_OPERAND (lhs, 1);
5444 if (TREE_CODE (member) == VAR_DECL)
5448 compiler_error ("invalid static class member");
5449 return error_mark_node;
5453 lhs = resolve_offset_ref (lhs);
5456 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5457 Reject anything strange now. */
5459 if (!lvalue_or_else (lhs, "assignment"))
5460 return error_mark_node;
5462 GNU_xref_assign (lhs);
5464 /* Warn about storing in something that is `const'. */
5465 /* For C++, don't warn if this is initialization. */
5466 if (modifycode != INIT_EXPR
5467 /* For assignment to `const' signature pointer/reference fields,
5468 don't warn either, we already printed a better message before. */
5469 && ! (TREE_CODE (lhs) == COMPONENT_REF
5470 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5471 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5472 && (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
5473 || ((TREE_CODE (lhstype) == RECORD_TYPE
5474 || TREE_CODE (lhstype) == UNION_TYPE)
5475 && C_TYPE_FIELDS_READONLY (lhstype))
5476 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5477 && TYPE_READONLY (TREE_TYPE (lhstype)))))
5478 readonly_error (lhs, "assignment", 0);
5480 /* If storing into a structure or union member,
5481 it has probably been given type `int'.
5482 Compute the type that would go with
5483 the actual amount of storage the member occupies. */
5485 if (TREE_CODE (lhs) == COMPONENT_REF
5486 && (TREE_CODE (lhstype) == INTEGER_TYPE
5487 || TREE_CODE (lhstype) == REAL_TYPE
5488 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5489 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5491 /* check to see if there is an assignment to `this' */
5492 if (lhs == current_class_decl)
5494 if (flag_this_is_variable > 0
5495 && DECL_NAME (current_function_decl) != NULL_TREE
5496 && current_class_name != DECL_NAME (current_function_decl))
5497 warning ("assignment to `this' not in constructor or destructor");
5498 current_function_just_assigned_this = 1;
5501 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5502 when the type of RHS is not yet known, i.e. its type
5503 is inherited from LHS. */
5504 rhs = require_instantiated_type (lhstype, newrhs, error_mark_node);
5505 if (rhs == error_mark_node)
5506 return error_mark_node;
5509 if (modifycode != INIT_EXPR)
5511 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5512 modifycode = NOP_EXPR;
5513 /* Reference-bashing */
5514 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5516 tree tmp = convert_from_reference (lhs);
5517 lhstype = TREE_TYPE (tmp);
5518 if (TYPE_SIZE (lhstype) == 0)
5520 incomplete_type_error (lhs, lhstype);
5521 return error_mark_node;
5526 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5528 tree tmp = convert_from_reference (newrhs);
5529 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5531 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5532 return error_mark_node;
5538 if (TREE_SIDE_EFFECTS (lhs))
5539 lhs = stabilize_reference (lhs);
5540 if (TREE_SIDE_EFFECTS (newrhs))
5541 newrhs = stabilize_reference (newrhs);
5543 /* C++: The semantics of C++ differ from those of C when an
5544 assignment of an aggregate is desired. Assignment in C++ is
5545 now defined as memberwise assignment of non-static members
5546 and base class objects. This rule applies recursively
5547 until a member of a built-in type is found.
5549 Also, we cannot do a bit-wise copy of aggregates which
5550 contain virtual function table pointers. Those
5551 pointer values must be preserved through the copy.
5552 However, this is handled in expand_expr, and not here.
5553 This is because much better code can be generated at
5554 that stage than this one. */
5555 if (TREE_CODE (lhstype) == RECORD_TYPE
5556 && ! TYPE_PTRMEMFUNC_P (lhstype)
5557 && (TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))
5558 || (TREE_CODE (TREE_TYPE (newrhs)) == RECORD_TYPE
5559 && UNIQUELY_DERIVED_FROM_P (lhstype, TREE_TYPE (newrhs)))))
5561 /* This was decided in finish_struct. */
5562 if (modifycode == INIT_EXPR)
5563 cp_error ("can't generate default copy constructor for `%T'", lhstype);
5565 cp_error ("can't generate default assignment operator for `%T'",
5568 /* This is now done by generating X(X&) and operator=(X&). */
5569 tree vbases = CLASSTYPE_VBASECLASSES (lhstype);
5570 tree lhs_addr = build_unary_op (ADDR_EXPR, lhs, 0);
5573 /* Memberwise assignment would cause NEWRHS to be
5574 evaluated for every member that gets assigned.
5575 By wrapping side-effecting exprs in a SAVE_EXPR,
5576 NEWRHS will only be evaluated once. */
5577 if (IS_AGGR_TYPE (TREE_TYPE (newrhs))
5578 && TREE_SIDE_EFFECTS (newrhs)
5579 /* This are things we don't have to save. */
5580 && TREE_CODE (newrhs) != COND_EXPR
5581 && TREE_CODE (newrhs) != TARGET_EXPR
5582 && TREE_CODE (newrhs) != WITH_CLEANUP_EXPR)
5583 /* Call `break_out_cleanups' on NEWRHS in case there are cleanups.
5584 If NEWRHS is a CALL_EXPR that needs a cleanup, failure to do so
5585 will result in expand_expr expanding the call without knowing
5586 that it should run the cleanup. */
5587 newrhs = save_expr (break_out_cleanups (newrhs));
5589 if (TREE_CODE (newrhs) == COND_EXPR)
5590 rhs_addr = rationalize_conditional_expr (ADDR_EXPR, newrhs);
5592 rhs_addr = build_unary_op (ADDR_EXPR, newrhs, 0);
5594 result = tree_cons (NULL_TREE,
5595 convert (build_reference_type (lhstype), lhs),
5598 if (! comptypes (TREE_TYPE (lhs_addr), TREE_TYPE (rhs_addr), 1))
5599 rhs_addr = convert_pointer_to (TREE_TYPE (TREE_TYPE (lhs_addr)), rhs_addr);
5601 tree noncopied_parts = NULL_TREE;
5603 if (TYPE_NONCOPIED_PARTS (lhstype) != 0)
5604 noncopied_parts = init_noncopied_parts (lhs,
5605 TYPE_NONCOPIED_PARTS (lhstype));
5606 while (noncopied_parts != 0)
5608 result = tree_cons (NULL_TREE,
5609 build_modify_expr (convert (ptr_type_node, TREE_VALUE (noncopied_parts)),
5611 TREE_PURPOSE (noncopied_parts)),
5613 noncopied_parts = TREE_CHAIN (noncopied_parts);
5616 /* Once we have our hands on an address, we must change NEWRHS
5617 to work from there. Otherwise we can get multiple evaluations
5619 if (TREE_CODE (newrhs) != SAVE_EXPR)
5620 newrhs = build_indirect_ref (rhs_addr, NULL_PTR);
5624 tree elt_lhs = convert_pointer_to (vbases, lhs_addr);
5625 tree elt_rhs = convert_pointer_to (vbases, rhs_addr);
5627 = tree_cons (NULL_TREE,
5628 build_modify_expr_1 (build_indirect_ref (elt_lhs, NULL_PTR),
5630 build_indirect_ref (elt_rhs, NULL_PTR),
5631 TYPE_BINFO (lhstype)),
5633 if (TREE_VALUE (result) == error_mark_node)
5634 return error_mark_node;
5635 vbases = TREE_CHAIN (vbases);
5637 result = tree_cons (NULL_TREE,
5638 build_modify_expr_1 (lhs,
5641 TYPE_BINFO (lhstype)),
5643 return build_compound_expr (result);
5647 /* If storing in a field that is in actuality a short or narrower than one,
5648 we must store in the field in its actual type. */
5650 if (lhstype != TREE_TYPE (lhs))
5652 lhs = copy_node (lhs);
5653 TREE_TYPE (lhs) = lhstype;
5656 /* Convert new value to destination type. */
5658 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5660 /* Have to wrap this in RTL_EXPR for two cases:
5661 in base or member initialization and if we
5662 are a branch of a ?: operator. Since we
5663 can't easily know the latter, just do it always. */
5665 result = make_node (RTL_EXPR);
5667 TREE_TYPE (result) = void_type_node;
5668 do_pending_stack_adjust ();
5669 start_sequence_for_rtl_expr (result);
5671 /* As a matter of principle, `start_sequence' should do this. */
5674 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
5675 1 + (modifycode != INIT_EXPR));
5677 do_pending_stack_adjust ();
5679 TREE_SIDE_EFFECTS (result) = 1;
5680 RTL_EXPR_SEQUENCE (result) = get_insns ();
5681 RTL_EXPR_RTL (result) = const0_rtx;
5686 if (modifycode == INIT_EXPR)
5688 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5689 "assignment", NULL_TREE, 0);
5690 if (lhs == DECL_RESULT (current_function_decl))
5692 if (DECL_INITIAL (lhs))
5693 warning ("return value from function receives multiple initializations");
5694 DECL_INITIAL (lhs) = newrhs;
5699 if (IS_AGGR_TYPE (lhstype))
5701 if (result = build_opfncall (MODIFY_EXPR,
5702 LOOKUP_NORMAL, lhs, newrhs,
5703 make_node (NOP_EXPR)))
5706 /* Avoid warnings on enum bit fields. */
5707 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5708 && TREE_CODE (lhstype) == INTEGER_TYPE)
5710 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5712 newrhs = convert_force (lhstype, newrhs);
5715 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5717 if (flag_elide_constructors == 0
5718 && TREE_CODE (newrhs) == CALL_EXPR
5719 && TREE_ADDRESSABLE (lhstype))
5721 /* Can't initialized directly from a CALL_EXPR, since
5722 we don't know about what doesn't alias what. */
5724 tree temp = get_temp_name (lhstype, 0);
5725 newrhs = build (COMPOUND_EXPR, lhstype,
5726 build_modify_expr (temp, INIT_EXPR, newrhs),
5731 if (TREE_CODE (newrhs) == ERROR_MARK)
5732 return error_mark_node;
5734 if (TREE_CODE (newrhs) == COND_EXPR)
5737 tree cond = TREE_OPERAND (newrhs, 0);
5739 if (TREE_SIDE_EFFECTS (lhs))
5740 cond = build_compound_expr (tree_cons
5742 build_tree_list (NULL_TREE, cond)));
5744 /* Cannot have two identical lhs on this one tree (result) as preexpand
5745 calls will rip them out and fill in RTL for them, but when the
5746 rtl is generated, the calls will only be in the first side of the
5747 condition, not on both, or before the conditional jump! (mrs) */
5748 lhs1 = break_out_calls (lhs);
5751 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5752 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5753 lhstype, lhs, newrhs);
5756 tree result_type = TREE_TYPE (newrhs);
5757 /* We have to convert each arm to the proper type because the
5758 types may have been munged by constant folding. */
5760 = build (COND_EXPR, result_type, cond,
5761 build_modify_expr (lhs, modifycode,
5762 convert (result_type,
5763 TREE_OPERAND (newrhs, 1))),
5764 build_modify_expr (lhs1, modifycode,
5765 convert (result_type,
5766 TREE_OPERAND (newrhs, 2))));
5769 else if (modifycode != INIT_EXPR && TREE_CODE (newrhs) == WITH_CLEANUP_EXPR)
5771 tree cleanup = TREE_OPERAND (newrhs, 2);
5774 /* Finish up by running cleanups and having the "value" of the lhs. */
5775 tree exprlist = tree_cons (NULL_TREE, cleanup,
5776 build_tree_list (NULL_TREE, lhs));
5777 newrhs = TREE_OPERAND (newrhs, 0);
5778 if (TREE_CODE (newrhs) == TARGET_EXPR)
5779 slot = TREE_OPERAND (newrhs, 0);
5780 else if (TREE_CODE (newrhs) == ADDR_EXPR)
5782 /* Bad but legal. */
5784 warning ("address taken of temporary object");
5787 my_friendly_abort (118);
5789 /* Copy the value computed in SLOT into LHS. */
5790 exprlist = tree_cons (NULL_TREE,
5791 build_modify_expr (lhs, modifycode, slot),
5793 /* Evaluate the expression that needs CLEANUP. This will
5794 compute the value into SLOT. */
5795 exprlist = tree_cons (NULL_TREE, newrhs, exprlist);
5796 result = convert (lhstype, build_compound_expr (exprlist));
5799 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5800 lhstype, lhs, newrhs);
5801 TREE_SIDE_EFFECTS (result) = 1;
5803 /* If we got the LHS in a different type for storing in,
5804 convert the result back to the nominal type of LHS
5805 so that the value we return always has the same type
5806 as the LHS argument. */
5808 if (olhstype == TREE_TYPE (result))
5810 /* Avoid warnings converting integral types back into enums
5811 for enum bit fields. */
5812 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5813 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5814 return convert_force (olhstype, result);
5815 return convert_for_assignment (olhstype, result, "assignment",
5820 /* Return 0 if EXP is not a valid lvalue in this language
5821 even though `lvalue_or_else' would accept it. */
5824 language_lvalue_valid (exp)
5830 /* Get differnce in deltas for different pointer to member function
5831 types. Return inetger_zero_node, if FROM cannot be converted to a
5832 TO type. If FORCE is true, then allow reverse conversions as well. */
5834 get_delta_difference (from, to, force)
5837 tree delta = integer_zero_node;
5843 binfo = get_binfo (from, to, 1);
5844 if (binfo == error_mark_node)
5846 error (" in pointer to member function conversion");
5853 error_not_base_type (from, to);
5854 error (" in pointer to member function conversion");
5857 binfo = get_binfo (to, from, 1);
5858 if (binfo == error_mark_node)
5860 error (" in pointer to member function conversion");
5865 error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from, to);
5868 if (TREE_VIA_VIRTUAL (binfo))
5870 warning ("pointer to member conversion from virtual base class will only work if your very careful");
5872 return fold (size_binop (MINUS_EXPR,
5874 BINFO_OFFSET (binfo)));
5876 if (TREE_VIA_VIRTUAL (binfo))
5878 warning ("pointer to member conversion from virtual base class will only work if your very careful");
5880 return BINFO_OFFSET (binfo);
5883 /* Build a constructor for a pointer to member function. It can be
5884 used to initialize global variables, local variable, or used
5885 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5888 If FORCE is non-zero, then force this conversion, even if
5889 we would rather not do it. Usually set when using an explicit
5892 Return error_mark_node, if something goes wrong. */
5895 build_ptrmemfunc (type, pfn, force)
5899 tree index = integer_zero_node;
5900 tree delta = integer_zero_node;
5901 tree delta2 = integer_zero_node;
5906 /* Handle multiple conversions of pointer to member fucntions. */
5907 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
5909 tree ndelta, ndelta2, nindex;
5910 /* Is is already the right type? */
5912 /* Sorry, can't do this, the backend is too stupid. */
5913 if (TYPE_METHOD_BASETYPE (TREE_TYPE (type))
5914 == TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))))
5916 if (type != TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
5918 npfn = build1 (NOP_EXPR, TYPE_GET_PTRMEMFUNC_TYPE (type), pfn);
5919 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
5924 if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
5928 if (TREE_CODE (pfn) != CONSTRUCTOR)
5931 ndelta = convert (sizetype, build_component_ref (pfn, delta_identifier, 0, 0));
5932 ndelta2 = convert (sizetype, DELTA2_FROM_PTRMEMFUNC (pfn));
5933 index = build_component_ref (pfn, index_identifier, 0, 0);
5934 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))),
5935 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
5937 delta = fold (size_binop (PLUS_EXPR, delta, ndelta));
5938 delta2 = fold (size_binop (PLUS_EXPR, ndelta2, delta2));
5939 e1 = fold (build (GT_EXPR, integer_type_node, index, integer_zero_node));
5941 u = build_nt (CONSTRUCTOR, 0, tree_cons (delta2_identifier, delta2, NULL_TREE));
5942 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
5943 tree_cons (NULL_TREE, index,
5944 tree_cons (NULL_TREE, u, NULL_TREE))));
5945 e2 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
5947 pfn = PFN_FROM_PTRMEMFUNC (pfn);
5948 npfn = build1 (NOP_EXPR, type, pfn);
5949 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
5951 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, npfn, NULL_TREE));
5952 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
5953 tree_cons (NULL_TREE, index,
5954 tree_cons (NULL_TREE, u, NULL_TREE))));
5955 e3 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
5956 return build_conditional_expr (e1, e2, e3);
5959 ndelta = TREE_VALUE (CONSTRUCTOR_ELTS (pfn));
5960 nindex = TREE_VALUE (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn)));
5961 npfn = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn))));
5962 npfn = TREE_VALUE (CONSTRUCTOR_ELTS (npfn));
5963 if (integer_zerop (nindex))
5964 pfn = integer_zero_node;
5967 sorry ("value casting of varible nonnull pointer to member functions not supported");
5968 return error_mark_node;
5972 /* Handle null pointer to member function conversions. */
5973 if (integer_zerop (pfn))
5975 pfn = build_c_cast (type, integer_zero_node);
5976 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, pfn, NULL_TREE));
5977 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, integer_zero_node,
5978 tree_cons (NULL_TREE, integer_zero_node,
5979 tree_cons (NULL_TREE, u, NULL_TREE))));
5980 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
5983 /* Allow pointer to member conversions here. */
5984 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn))),
5985 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
5987 delta2 = fold (size_binop (PLUS_EXPR, delta2, delta));
5989 if (TREE_CODE (TREE_OPERAND (pfn, 0)) != FUNCTION_DECL)
5990 warning ("assuming pointer to member function is non-virtual");
5992 if (TREE_CODE (TREE_OPERAND (pfn, 0)) == FUNCTION_DECL
5993 && DECL_VINDEX (TREE_OPERAND (pfn, 0)))
5995 /* Find the offset to the vfield pointer in the object. */
5996 vfield_offset = get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn, 0)),
5997 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn, 0)),
5999 vfield_offset = get_vfield_offset (vfield_offset);
6000 delta2 = size_binop (PLUS_EXPR, vfield_offset, delta2);
6002 /* Map everything down one to make room for the null pointer to member. */
6003 index = size_binop (PLUS_EXPR,
6004 DECL_VINDEX (TREE_OPERAND (pfn, 0)),
6006 u = build_nt (CONSTRUCTOR, 0, tree_cons (delta2_identifier, delta2, NULL_TREE));
6010 index = fold (size_binop (MINUS_EXPR, integer_zero_node, integer_one_node));
6012 npfn = build1 (NOP_EXPR, type, pfn);
6013 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6015 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, npfn, NULL_TREE));
6018 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6019 tree_cons (NULL_TREE, index,
6020 tree_cons (NULL_TREE, u, NULL_TREE))));
6021 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6024 /* Convert value RHS to type TYPE as preparation for an assignment
6025 to an lvalue of type TYPE.
6026 The real work of conversion is done by `convert'.
6027 The purpose of this function is to generate error messages
6028 for assignments that are not allowed in C.
6029 ERRTYPE is a string to use in error messages:
6030 "assignment", "return", etc.
6032 C++: attempts to allow `convert' to find conversions involving
6033 implicit type conversion between aggregate and scalar types
6034 as per 8.5.6 of C++ manual. Does not randomly dereference
6035 pointers to aggregates! */
6038 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6044 register enum tree_code codel = TREE_CODE (type);
6045 register tree rhstype;
6046 register enum tree_code coder = TREE_CODE (TREE_TYPE (rhs));
6048 if (coder == UNKNOWN_TYPE)
6049 rhs = instantiate_type (type, rhs, 1);
6051 if (coder == ERROR_MARK)
6052 return error_mark_node;
6054 if (codel == OFFSET_TYPE)
6056 type = TREE_TYPE (type);
6057 codel = TREE_CODE (type);
6060 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6061 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6062 rhs = TREE_OPERAND (rhs, 0);
6064 if (rhs == error_mark_node)
6065 return error_mark_node;
6067 if (TREE_VALUE (rhs) == error_mark_node)
6068 return error_mark_node;
6070 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6072 rhs = resolve_offset_ref (rhs);
6073 if (rhs == error_mark_node)
6074 return error_mark_node;
6075 rhstype = TREE_TYPE (rhs);
6076 coder = TREE_CODE (rhstype);
6079 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6080 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6081 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6082 rhs = default_conversion (rhs);
6083 else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6084 rhs = convert_from_reference (rhs);
6086 rhstype = TREE_TYPE (rhs);
6087 coder = TREE_CODE (rhstype);
6089 /* This should no longer change types on us. */
6090 if (TREE_CODE (rhs) == CONST_DECL)
6091 rhs = DECL_INITIAL (rhs);
6092 else if (TREE_READONLY_DECL_P (rhs))
6093 rhs = decl_constant_value (rhs);
6095 if (type == rhstype)
6097 overflow_warning (rhs);
6101 if (coder == VOID_TYPE)
6103 error ("void value not ignored as it ought to be");
6104 return error_mark_node;
6106 /* Arithmetic types all interconvert. */
6107 if ((codel == INTEGER_TYPE || codel == REAL_TYPE)
6108 && (coder == INTEGER_TYPE || coder == REAL_TYPE))
6110 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6111 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6114 cp_warning ("`%T' used for argument %P of `%D'",
6115 rhstype, parmnum, fndecl);
6117 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6119 /* And we should warn if assigning a negative value to
6120 an unsigned variable. */
6121 else if (TREE_UNSIGNED (type))
6123 if (TREE_CODE (rhs) == INTEGER_CST
6124 && TREE_NEGATED_INT (rhs))
6127 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6128 rhs, parmnum, fndecl);
6130 cp_warning ("%s of negative value `%E' to `%T'",
6131 errtype, rhs, type);
6133 overflow_warning (rhs);
6134 if (TREE_CONSTANT (rhs))
6138 return convert_and_check (type, rhs);
6140 /* Conversions involving enums. */
6141 else if ((codel == ENUMERAL_TYPE
6142 && (coder == ENUMERAL_TYPE || coder == INTEGER_TYPE || coder == REAL_TYPE))
6143 || (coder == ENUMERAL_TYPE
6144 && (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE || codel == REAL_TYPE)))
6146 return convert (type, rhs);
6148 /* Conversions among pointers */
6149 else if (codel == POINTER_TYPE
6150 && (coder == POINTER_TYPE
6151 || (coder == RECORD_TYPE
6152 && (IS_SIGNATURE_POINTER (rhstype)
6153 || IS_SIGNATURE_REFERENCE (rhstype)))))
6155 register tree ttl = TREE_TYPE (type);
6158 if (coder == RECORD_TYPE)
6160 rhs = build_optr_ref (rhs);
6161 rhstype = TREE_TYPE (rhs);
6163 ttr = TREE_TYPE (rhstype);
6165 /* If both pointers are of aggregate type, then we
6166 can give better error messages, and save some work
6168 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6172 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6173 || type == class_star_type_node
6174 || rhstype == class_star_type_node)
6175 binfo = TYPE_BINFO (ttl);
6177 binfo = get_binfo (ttl, ttr, 1);
6179 if (binfo == error_mark_node)
6180 return error_mark_node;
6182 return error_not_base_type (ttl, ttr);
6184 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6187 cp_warning ("passing `%T' as argument %P of `%D' discards const",
6188 rhstype, parmnum, fndecl);
6190 cp_warning ("%s to `%T' from `%T' discards const",
6191 errtype, type, rhstype);
6193 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6196 cp_warning ("passing `%T' as argument %P of `%D' discards volatile",
6197 rhstype, parmnum, fndecl);
6199 cp_warning ("%s to `%T' from `%T' discards volatile",
6200 errtype, type, rhstype);
6204 /* Any non-function converts to a [const][volatile] void *
6205 and vice versa; otherwise, targets must be the same.
6206 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6207 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6208 || TYPE_MAIN_VARIANT (ttr) == void_type_node
6209 || comp_target_types (type, rhstype, 1)
6210 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6211 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6213 /* ARM $4.8, commentary on p39. */
6214 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6215 && TREE_CODE (ttr) == OFFSET_TYPE)
6217 error ("no standard conversion from pointer to member to `void *'");
6218 return error_mark_node;
6221 if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6222 && TYPE_MAIN_VARIANT (ttr) == void_type_node
6223 && rhs != null_pointer_node)
6224 if (coder == RECORD_TYPE)
6225 pedwarn ("implicit conversion of signature pointer to type `%s'",
6226 type_as_string (type, 0));
6228 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6230 /* Const and volatile mean something different for function types,
6231 so the usual warnings are not appropriate. */
6232 else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6233 || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6235 if (TREE_CODE (ttl) == OFFSET_TYPE
6236 && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6237 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6239 sorry ("%s between pointer to members converting across virtual baseclasses", errtype);
6240 return error_mark_node;
6242 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6245 cp_warning ("passing `%T' as argument %P of `%D' discards const",
6246 rhstype, parmnum, fndecl);
6248 cp_warning ("%s to `%T' from `%T' discards const",
6249 errtype, type, rhstype);
6251 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6254 cp_warning ("passing `%T' as argument %P of `%D' discards volatile",
6255 rhstype, parmnum, fndecl);
6257 cp_warning ("%s to `%T' from `%T' discards volatile",
6258 errtype, type, rhstype);
6262 else if (TREE_CODE (ttr) == OFFSET_TYPE
6263 && TREE_CODE (ttl) != OFFSET_TYPE)
6265 /* Normally, pointers to different type codes (other
6266 than void) are not compatible, but we perform
6267 some type instantiation if that resolves the
6268 ambiguity of (X Y::*) and (X *). */
6270 if (current_class_decl)
6272 if (TREE_CODE (rhs) == INTEGER_CST)
6274 rhs = build (PLUS_EXPR, build_pointer_type (TREE_TYPE (ttr)),
6275 current_class_decl, rhs);
6276 return convert_for_assignment (type, rhs,
6277 errtype, fndecl, parmnum);
6280 if (TREE_CODE (ttl) == METHOD_TYPE)
6281 error ("%s between pointer-to-method and pointer-to-member types",
6284 error ("%s between pointer and pointer-to-member types", errtype);
6285 return error_mark_node;
6289 int const_parity = TYPE_READONLY (type) ^ TYPE_READONLY (rhstype);
6290 int volatile_parity = TYPE_VOLATILE (type) ^ TYPE_VOLATILE (rhstype);
6291 int unsigned_parity;
6294 while (TREE_CODE (ttl) == POINTER_TYPE
6295 && TREE_CODE (ttr) == POINTER_TYPE)
6298 const_parity |= TYPE_READONLY (ttl) ^ TYPE_READONLY (ttr);
6299 volatile_parity |= TYPE_VOLATILE (ttl) ^ TYPE_VOLATILE (ttr);
6300 ttl = TREE_TYPE (ttl);
6301 ttr = TREE_TYPE (ttr);
6303 unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6304 if (unsigned_parity)
6305 if (TREE_UNSIGNED (ttl))
6306 ttr = unsigned_type (ttr);
6308 ttl = unsigned_type (ttl);
6310 if (comp_target_types (ttl, ttr, nptrs))
6315 cp_warning ("passing `%T' as argument %P of `%D' discards const",
6316 rhstype, parmnum, fndecl);
6318 cp_warning ("%s to `%T' from `%T' discards const",
6319 errtype, type, rhstype);
6321 if (volatile_parity)
6324 cp_warning ("passing `%T' as argument %P of `%D' discards volatile",
6325 rhstype, parmnum, fndecl);
6327 cp_warning ("%s to `%T' from `%T' discards volatile",
6328 errtype, type, rhstype);
6330 if (unsigned_parity > 0)
6333 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6334 rhstype, parmnum, fndecl);
6336 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6337 errtype, type, rhstype);
6339 else if (unsigned_parity < 0)
6342 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6343 rhstype, parmnum, fndecl);
6345 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6346 errtype, type, rhstype);
6349 /* C++ is not so friendly about converting function and
6350 member function pointers as C. Emit warnings here. */
6351 if (TREE_CODE (ttl) == FUNCTION_TYPE
6352 || TREE_CODE (ttl) == METHOD_TYPE)
6353 if (! comptypes (ttl, ttr, 0))
6355 warning ("conflicting function types in %s:", errtype);
6356 cp_warning ("\t`%T' != `%T'", type, rhstype);
6359 else if (TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6361 /* When does this happen? */
6362 my_friendly_abort (119);
6363 /* Conversion of a pointer-to-member type to void *. */
6364 rhs = build_unary_op (ADDR_EXPR, rhs, 0);
6365 TREE_TYPE (rhs) = type;
6368 else if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6370 /* When does this happen? */
6371 my_friendly_abort (120);
6372 /* Conversion of a pointer-to-member type to void *. */
6373 rhs = build_unary_op (ADDR_EXPR, rhs, 0);
6374 TREE_TYPE (rhs) = type;
6380 cp_error ("passing `%T' as argument %P of `%D'",
6381 rhstype, parmnum, fndecl);
6383 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6384 return error_mark_node;
6387 return convert (type, rhs);
6389 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6391 /* An explicit constant 0 can convert to a pointer,
6392 but not a 0 that results from casting or folding. */
6393 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
6396 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6397 rhstype, parmnum, fndecl);
6399 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6400 errtype, type, rhstype);
6401 return convert (type, rhs);
6403 return null_pointer_node;
6405 else if (codel == INTEGER_TYPE
6406 && (coder == POINTER_TYPE
6407 || (coder == RECORD_TYPE
6408 && (IS_SIGNATURE_POINTER (rhstype)
6409 || IS_SIGNATURE_REFERENCE (rhstype)))))
6412 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6413 rhstype, parmnum, fndecl);
6415 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6416 errtype, type, rhstype);
6417 return convert (type, rhs);
6421 else if (((coder == POINTER_TYPE && TREE_CODE (rhs) == ADDR_EXPR
6422 && TREE_CODE (rhstype) == POINTER_TYPE
6423 && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
6424 || integer_zerop (rhs)
6425 || TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6426 && TYPE_PTRMEMFUNC_P (type))
6428 /* compatible pointer to member functions. */
6429 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), rhs, 0);
6431 else if (codel == ERROR_MARK || coder == ERROR_MARK)
6432 return error_mark_node;
6434 /* This should no longer happen. References are initialized via
6435 `convert_for_initialization'. They should otherwise be
6436 bashed before coming here. */
6437 else if (codel == REFERENCE_TYPE)
6438 /* Force an abort. */
6439 my_friendly_assert (codel != REFERENCE_TYPE, 317);
6440 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
6442 tree nrhs = build1 (NOP_EXPR, type, rhs);
6443 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6446 else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
6447 return convert (type, rhs);
6449 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6450 return error_mark_node;
6453 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6454 it is the target of the initialization.
6455 ERRTYPE is a string to use in error messages.
6457 Two major differences between the behavior of
6458 `convert_for_assignment' and `convert_for_initialization'
6459 are that references are bashed in the former, while
6460 copied in the latter, and aggregates are assigned in
6461 the former (operator=) while initialized in the
6464 If using constructor make sure no conversion operator exists, if one does
6465 exist, an ambiguity exists. */
6467 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6468 tree exp, type, rhs;
6474 register enum tree_code codel = TREE_CODE (type);
6475 register tree rhstype;
6476 register enum tree_code coder;
6478 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6479 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6480 if (TREE_CODE (rhs) == NOP_EXPR
6481 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0)))
6482 rhs = TREE_OPERAND (rhs, 0);
6484 if (rhs == error_mark_node
6485 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6486 return error_mark_node;
6488 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6490 rhs = resolve_offset_ref (rhs);
6491 if (rhs == error_mark_node)
6492 return error_mark_node;
6493 rhstype = TREE_TYPE (rhs);
6494 coder = TREE_CODE (rhstype);
6497 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6498 && TREE_CODE (type) != ARRAY_TYPE && TREE_CODE (type) != REFERENCE_TYPE)
6499 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6500 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6501 rhs = default_conversion (rhs);
6503 rhstype = TREE_TYPE (rhs);
6504 coder = TREE_CODE (rhstype);
6506 if (coder == UNKNOWN_TYPE)
6508 rhs = instantiate_type (type, rhs, 1);
6509 rhstype = TREE_TYPE (rhs);
6510 coder = TREE_CODE (rhstype);
6513 if (coder == ERROR_MARK)
6514 return error_mark_node;
6517 /* This is *not* the quick way out! It is the way to disaster. */
6518 if (type == rhstype)
6522 /* We accept references to incomplete types, so we can
6523 return here before checking if RHS is of complete type. */
6525 if (codel == REFERENCE_TYPE)
6526 return convert_to_reference ((exp ? exp : error_mark_node),
6527 type, rhs, fndecl, parmnum, errtype,
6530 rhs = require_complete_type (rhs);
6531 if (rhs == error_mark_node)
6532 return error_mark_node;
6534 if (exp != 0) exp = require_complete_type (exp);
6535 if (exp == error_mark_node)
6536 return error_mark_node;
6538 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6539 rhstype = TREE_TYPE (rhstype);
6541 if (TYPE_LANG_SPECIFIC (type)
6542 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
6543 return build_signature_pointer_constructor (type, rhs);
6545 if (IS_AGGR_TYPE (type) && TYPE_NEEDS_CONSTRUCTING (type))
6547 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6549 /* This is sufficient to perform initialization. No need,
6550 apparently, to go through X(X&) to do first-cut
6551 initialization. Return through a TARGET_EXPR so that we get
6552 cleanups if it is used. */
6553 if (TREE_CODE (rhs) == CALL_EXPR)
6555 rhs = build_cplus_new (type, rhs, 0);
6558 /* Handle the case of default parameter initialization and
6559 initialization of static variables. */
6560 else if (TREE_CODE (rhs) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (rhs))
6562 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs, 0)) == CALL_EXPR, 318);
6565 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1)) == NULL_TREE, 316);
6566 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1))
6567 = build_unary_op (ADDR_EXPR, exp, 0);
6570 rhs = build_cplus_new (type, TREE_OPERAND (rhs, 0), 0);
6574 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)
6575 || (IS_AGGR_TYPE (rhstype) && UNIQUELY_DERIVED_FROM_P (type, rhstype)))
6577 if (TYPE_HAS_INIT_REF (type))
6579 tree init = build_method_call (exp, constructor_name_full (type),
6580 build_tree_list (NULL_TREE, rhs),
6581 NULL_TREE, LOOKUP_NORMAL);
6583 if (init == error_mark_node)
6584 return error_mark_node;
6588 exp = build_cplus_new (type, init, 0);
6592 return build (COMPOUND_EXPR, type, init, exp);
6595 /* ??? The following warnings are turned off because
6596 this is another place where the default X(X&) constructor
6598 if (TYPE_HAS_ASSIGNMENT (type))
6599 cp_warning ("bitwise copy: `%T' defines operator=", type);
6601 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6602 rhs = convert_from_reference (rhs);
6603 if (type != rhstype)
6605 tree nrhs = build1 (NOP_EXPR, type, rhs);
6606 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6612 return convert (type, rhs);
6615 if (type == TREE_TYPE (rhs))
6617 if (TREE_READONLY_DECL_P (rhs))
6618 rhs = decl_constant_value (rhs);
6622 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6625 /* Expand an ASM statement with operands, handling output operands
6626 that are not variables or INDIRECT_REFS by transforming such
6627 cases into cases that expand_asm_operands can handle.
6629 Arguments are same as for expand_asm_operands.
6631 We don't do default conversions on all inputs, because it can screw
6632 up operands that are expected to be in memory. */
6635 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6636 tree string, outputs, inputs, clobbers;
6641 int noutputs = list_length (outputs);
6643 /* o[I] is the place that output number I should be written. */
6644 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6647 /* Record the contents of OUTPUTS before it is modified. */
6648 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6649 o[i] = TREE_VALUE (tail);
6651 /* Generate the ASM_OPERANDS insn;
6652 store into the TREE_VALUEs of OUTPUTS some trees for
6653 where the values were actually stored. */
6654 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6656 /* Copy all the intermediate outputs into the specified outputs. */
6657 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6659 if (o[i] != TREE_VALUE (tail))
6661 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6662 const0_rtx, VOIDmode, 0);
6665 /* Detect modification of read-only values.
6666 (Otherwise done by build_modify_expr.) */
6669 tree type = TREE_TYPE (o[i]);
6670 if (TYPE_READONLY (type)
6671 || ((TREE_CODE (type) == RECORD_TYPE
6672 || TREE_CODE (type) == UNION_TYPE)
6673 && C_TYPE_FIELDS_READONLY (type)))
6674 readonly_error (o[i], "modification by `asm'", 1);
6678 /* Those MODIFY_EXPRs could do autoincrements. */
6682 /* Expand a C `return' statement.
6683 RETVAL is the expression for what to return,
6684 or a null pointer for `return;' with no value.
6686 C++: upon seeing a `return', we must call destructors on all
6687 variables in scope which had constructors called on them.
6688 This means that if in a destructor, the base class destructors
6689 must be called before returning.
6691 The RETURN statement in C++ has initialization semantics. */
6694 c_expand_return (retval)
6697 extern struct nesting *cond_stack, *loop_stack, *case_stack;
6698 extern tree dtor_label, ctor_label;
6699 tree result = DECL_RESULT (current_function_decl);
6700 tree valtype = TREE_TYPE (result);
6701 register int use_temp = 0;
6702 int returns_value = 1;
6704 if (TREE_THIS_VOLATILE (current_function_decl))
6705 warning ("function declared `noreturn' has a `return' statement");
6707 if (retval == error_mark_node)
6709 current_function_returns_null = 1;
6713 if (retval == NULL_TREE)
6715 /* A non-named return value does not count. */
6717 /* Can't just return from a destructor. */
6720 expand_goto (dtor_label);
6724 if (DECL_CONSTRUCTOR_P (current_function_decl))
6725 retval = current_class_decl;
6726 else if (DECL_NAME (result) != NULL_TREE
6727 && TREE_CODE (valtype) != VOID_TYPE)
6731 current_function_returns_null = 1;
6733 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
6735 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
6737 pedwarn ("`return' with no value, in function returning non-void");
6738 /* Clear this, so finish_function won't say that we
6739 reach the end of a non-void function (which we don't,
6740 we gave a return!). */
6741 current_function_returns_null = 0;
6745 expand_null_return ();
6749 else if (DECL_CONSTRUCTOR_P (current_function_decl)
6750 && retval != current_class_decl)
6752 error ("return from a constructor: use `this = ...' instead");
6753 retval = current_class_decl;
6756 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
6758 current_function_returns_null = 1;
6759 /* We do this here so we'll avoid a warning about how the function
6760 "may or may not return a value" in finish_function. */
6763 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6764 pedwarn ("`return' with a value, in function returning void");
6765 expand_return (retval);
6767 /* Add some useful error checking for C++. */
6768 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
6770 tree whats_returned;
6771 tree tmp_result = result;
6773 /* Don't initialize directly into a non-BLKmode retval, since that
6774 could lose when being inlined by another caller. (GCC can't
6775 read the function return register in an inline function when
6776 the return value is being ignored). */
6777 if (result && TYPE_MODE (TREE_TYPE (tmp_result)) != BLKmode)
6780 /* convert to reference now, so we can give error if we
6781 return an reference to a non-lvalue. */
6782 retval = convert_for_initialization (tmp_result, valtype, retval,
6783 LOOKUP_NORMAL, "return",
6786 /* Sort through common things to see what it is
6787 we are returning. */
6788 whats_returned = retval;
6789 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6791 whats_returned = TREE_OPERAND (whats_returned, 1);
6792 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6793 whats_returned = TREE_OPERAND (whats_returned, 0);
6795 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6797 whats_returned = TREE_OPERAND (whats_returned, 0);
6798 while (TREE_CODE (whats_returned) == NEW_EXPR
6799 || TREE_CODE (whats_returned) == TARGET_EXPR
6800 || TREE_CODE (whats_returned) == WITH_CLEANUP_EXPR)
6801 /* Get the target. */
6802 whats_returned = TREE_OPERAND (whats_returned, 0);
6805 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
6807 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
6808 warning ("reference to non-lvalue returned");
6809 else if (! TREE_STATIC (whats_returned)
6810 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned)))
6811 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
6814 else if (TREE_CODE (retval) == ADDR_EXPR)
6816 tree whats_returned = TREE_OPERAND (retval, 0);
6818 if (TREE_CODE (whats_returned) == TREE_LIST)
6819 whats_returned = TREE_VALUE (whats_returned);
6821 if (DECL_NAME (whats_returned)
6822 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
6823 && !TREE_STATIC (whats_returned))
6824 cp_warning_at ("address of local variable `%D' returned", whats_returned);
6827 /* Now deal with possible C++ hair:
6828 (1) Compute the return value.
6829 (2) If there are aggregate values with destructors which
6830 must be cleaned up, clean them (taking care
6831 not to clobber the return value).
6832 (3) If an X(X&) constructor is defined, the return
6833 value must be returned via that. */
6835 if (retval == result
6836 /* Watch out for constructors, which "return" aggregates
6837 via initialization, but which otherwise "return" a pointer. */
6838 || DECL_CONSTRUCTOR_P (current_function_decl))
6840 /* This is just an error--it's already been reported. */
6841 if (TYPE_SIZE (valtype) == NULL_TREE)
6844 if (TYPE_MODE (valtype) != BLKmode
6845 && any_pending_cleanups (1))
6847 retval = get_temp_regvar (valtype, retval);
6848 use_temp = obey_regdecls;
6851 else if (IS_AGGR_TYPE (valtype) && TYPE_NEEDS_CONSTRUCTING (valtype))
6853 /* Throw away the cleanup that `build_functional_cast' gave us. */
6854 if (TREE_CODE (retval) == WITH_CLEANUP_EXPR
6855 && TREE_CODE (TREE_OPERAND (retval, 0)) == TARGET_EXPR)
6856 retval = TREE_OPERAND (retval, 0);
6857 expand_aggr_init (result, retval, 0);
6858 DECL_INITIAL (result) = NULL_TREE;
6863 if (TYPE_MODE (valtype) == VOIDmode)
6865 if (TYPE_MODE (TREE_TYPE (result)) != VOIDmode
6866 && warn_return_type)
6867 warning ("return of void value in function returning non-void");
6868 expand_expr_stmt (retval);
6872 else if (TYPE_MODE (valtype) != BLKmode
6873 && any_pending_cleanups (1))
6875 retval = get_temp_regvar (valtype, retval);
6876 use_temp = obey_regdecls;
6881 retval = convert_for_initialization (result, valtype, retval,
6883 "return", NULL_TREE, 0);
6884 DECL_INITIAL (result) = NULL_TREE;
6886 if (retval == error_mark_node)
6892 if (retval != NULL_TREE
6893 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
6894 && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
6895 current_function_return_value = retval;
6899 /* Everything's great--RETVAL is in RESULT. */
6900 if (original_result_rtx)
6901 store_expr (result, original_result_rtx, 0);
6902 else if (retval && retval != result)
6904 /* Clear this out so the later call to decl_function_context
6905 won't end up bombing on us. */
6906 if (DECL_CONTEXT (result) == error_mark_node)
6907 DECL_CONTEXT (result) = NULL_TREE;
6908 /* Here is where we finally get RETVAL into RESULT.
6909 `expand_return' does the magic of protecting
6910 RESULT from cleanups. */
6911 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6912 TREE_SIDE_EFFECTS (retval) = 1;
6913 expand_return (retval);
6916 expand_return (result);
6918 use_variable (DECL_RTL (result));
6919 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
6920 expand_goto (ctor_label);
6922 expand_null_return ();
6926 /* We may still need to put RETVAL into RESULT. */
6927 result = DECL_RESULT (current_function_decl);
6928 if (original_result_rtx)
6930 /* Here we have a named return value that went
6931 into memory. We can compute RETVAL into that. */
6933 expand_assignment (result, retval, 0, 0);
6935 store_expr (result, original_result_rtx, 0);
6936 result = make_tree (TREE_TYPE (result), original_result_rtx);
6938 else if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
6940 /* Here RETVAL is CURRENT_CLASS_DECL, so there's nothing to do. */
6941 expand_goto (ctor_label);
6945 /* Here is where we finally get RETVAL into RESULT.
6946 `expand_return' does the magic of protecting
6947 RESULT from cleanups. */
6948 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6949 TREE_SIDE_EFFECTS (result) = 1;
6950 expand_return (result);
6952 else if (TYPE_MODE (TREE_TYPE (result)) != VOIDmode)
6953 expand_return (result);
6956 current_function_returns_value = returns_value;
6957 if (original_result_rtx)
6958 use_variable (original_result_rtx);
6960 use_variable (DECL_RTL (DECL_RESULT (current_function_decl)));
6962 /* One way to clear out cleanups that EXPR might
6963 generate. Note that this code will really be
6964 dead code, but that is ok--cleanups that were
6965 needed were handled by the magic of `return'. */
6966 expand_cleanups_to (NULL_TREE);
6969 /* Start a C switch statement, testing expression EXP.
6970 Return EXP if it is valid, an error node otherwise. */
6973 c_expand_start_case (exp)
6977 register enum tree_code code;
6979 /* Convert from references, etc. */
6980 exp = default_conversion (exp);
6981 type = TREE_TYPE (exp);
6982 code = TREE_CODE (type);
6984 if (IS_AGGR_TYPE_CODE (code))
6985 exp = build_type_conversion (CONVERT_EXPR, integer_type_node, exp, 1);
6987 if (exp == NULL_TREE)
6989 error ("switch quantity not an integer");
6990 exp = error_mark_node;
6992 type = TREE_TYPE (exp);
6993 code = TREE_CODE (type);
6995 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6997 error ("switch quantity not an integer");
6998 exp = error_mark_node;
7004 exp = default_conversion (exp);
7005 type = TREE_TYPE (exp);
7006 index = get_unwidened (exp, 0);
7007 /* We can't strip a conversion from a signed type to an unsigned,
7008 because if we did, int_fits_type_p would do the wrong thing
7009 when checking case values for being in range,
7010 and it's too hard to do the right thing. */
7011 if (TREE_UNSIGNED (TREE_TYPE (exp))
7012 == TREE_UNSIGNED (TREE_TYPE (index)))
7016 expand_start_case (1, exp, type, "switch statement");