1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92, 93, 94, 1995 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 ();
44 extern tree shorten_compare ();
45 extern void binary_op_error ();
46 static tree pointer_int_sum ();
47 static tree pointer_diff ();
48 static tree convert_sequence ();
49 /* static */ tree unary_complex_lvalue ();
50 static tree get_delta_difference PROTO((tree, tree, int));
52 extern rtx original_result_rtx;
54 /* Return the target type of TYPE, which meas return T for:
55 T*, T&, T[], T (...), and otherwise, just T. */
61 if (TREE_CODE (type) == REFERENCE_TYPE)
62 type = TREE_TYPE (type);
63 while (TREE_CODE (type) == POINTER_TYPE
64 || TREE_CODE (type) == ARRAY_TYPE
65 || TREE_CODE (type) == FUNCTION_TYPE
66 || TREE_CODE (type) == METHOD_TYPE
67 || TREE_CODE (type) == OFFSET_TYPE)
68 type = TREE_TYPE (type);
72 /* Do `exp = require_complete_type (exp);' to make sure exp
73 does not have an incomplete type. (That includes void types.) */
76 require_complete_type (value)
79 tree type = TREE_TYPE (value);
81 /* First, detect a valid value with a complete type. */
82 if (TYPE_SIZE (type) != 0
83 && type != void_type_node
84 && ! (TYPE_LANG_SPECIFIC (type)
85 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
86 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
89 /* If we see X::Y, we build an OFFSET_TYPE which has
90 not been laid out. Try to avoid an error by interpreting
91 it as this->X::Y, if reasonable. */
92 if (TREE_CODE (value) == OFFSET_REF
94 && TREE_OPERAND (value, 0) == C_C_D)
96 tree base, member = TREE_OPERAND (value, 1);
97 tree basetype = TYPE_OFFSET_BASETYPE (type);
98 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
99 base = convert_pointer_to (basetype, current_class_decl);
100 value = build (COMPONENT_REF, TREE_TYPE (member),
101 build_indirect_ref (base, NULL_PTR), member);
102 return require_complete_type (value);
105 incomplete_type_error (value, type);
106 return error_mark_node;
109 /* Return truthvalue of whether type of EXP is instantiated. */
114 return (TREE_CODE (exp) == TREE_LIST
115 || TREE_TYPE (exp) == unknown_type_node
116 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
117 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
120 /* Return truthvalue of whether T is function (or pfn) type. */
125 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
126 || (TREE_CODE (t) == POINTER_TYPE
127 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
128 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
131 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
132 does not have an uninstantiated type.
133 TYPE is type to instantiate with, if uninstantiated. */
135 require_instantiated_type (type, exp, errval)
136 tree type, exp, errval;
138 if (TREE_TYPE (exp) == NULL_TREE)
140 error ("argument list may not have an initializer list");
144 if (TREE_TYPE (exp) == unknown_type_node
145 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
146 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node))
148 exp = instantiate_type (type, exp, 1);
149 if (TREE_TYPE (exp) == error_mark_node)
155 /* Return a variant of TYPE which has all the type qualifiers of LIKE
156 as well as those of TYPE. */
159 qualify_type (type, like)
162 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
163 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
164 /* @@ Must do member pointers here. */
165 return cp_build_type_variant (type, constflag, volflag);
168 /* Return the common type of two parameter lists.
169 We assume that comptypes has already been done and returned 1;
170 if that isn't so, this may crash.
172 As an optimization, free the space we allocate if the parameter
173 lists are already common. */
179 tree oldargs = p1, newargs, n;
182 char *first_obj = (char *) oballoc (0);
184 len = list_length (p1);
185 newargs = tree_last (p1);
187 if (newargs == void_list_node)
196 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
201 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
203 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
205 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
208 else if (! TREE_PURPOSE (p1))
210 if (TREE_PURPOSE (p2))
212 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
218 int cmp = simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2));
220 my_friendly_abort (111);
223 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
225 if (TREE_VALUE (p1) != TREE_VALUE (p2))
228 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
231 TREE_VALUE (n) = TREE_VALUE (p1);
242 /* Return the common type of two types.
243 We assume that comptypes has already been done and returned 1;
244 if that isn't so, this may crash.
246 This is the type for the result of most arithmetic operations
247 if the operands have the given two types.
249 We do not deal with enumeral types here because they have already been
250 converted to integer types. */
256 register enum tree_code code1;
257 register enum tree_code code2;
260 /* Save time if the two types are the same. */
262 if (t1 == t2) return t1;
264 /* If one type is nonsense, use the other. */
265 if (t1 == error_mark_node)
267 if (t2 == error_mark_node)
270 /* Merge the attributes */
272 { register tree a1, a2;
273 a1 = TYPE_ATTRIBUTES (t1);
274 a2 = TYPE_ATTRIBUTES (t2);
276 /* Either one unset? Take the set one. */
278 if (!(attributes = a1))
281 /* One that completely contains the other? Take it. */
283 else if (a2 && !attribute_list_contained (a1, a2))
284 if (attribute_list_contained (a2, a1))
288 /* Pick the longest list, and hang on the other
291 if (list_length (a1) < list_length (a2))
292 attributes = a2, a2 = a1;
294 for (; a2; a2 = TREE_CHAIN (a2))
295 if (!value_member (attributes, a2))
298 TREE_CHAIN (a1) = attributes;
304 /* Treat an enum type as the unsigned integer type of the same width. */
306 if (TREE_CODE (t1) == ENUMERAL_TYPE)
307 t1 = type_for_size (TYPE_PRECISION (t1), 1);
308 if (TREE_CODE (t2) == ENUMERAL_TYPE)
309 t2 = type_for_size (TYPE_PRECISION (t2), 1);
311 code1 = TREE_CODE (t1);
312 code2 = TREE_CODE (t2);
318 /* If only one is real, use it as the result. */
320 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
321 return build_type_attribute_variant (t1, attributes);
323 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
324 return build_type_attribute_variant (t2, attributes);
326 /* Both real or both integers; use the one with greater precision. */
328 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
329 return build_type_attribute_variant (t1, attributes);
330 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
331 return build_type_attribute_variant (t2, attributes);
333 /* Same precision. Prefer longs to ints even when same size. */
335 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
336 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
337 return build_type_attribute_variant (long_unsigned_type_node,
340 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
341 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
343 /* But preserve unsignedness from the other type,
344 since long cannot hold all the values of an unsigned int. */
345 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
346 t1 = long_unsigned_type_node;
348 t1 = long_integer_type_node;
349 return build_type_attribute_variant (t1, attributes);
352 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
353 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
354 return build_type_attribute_variant (long_double_type_node,
357 /* Otherwise prefer the unsigned one. */
359 if (TREE_UNSIGNED (t1))
360 return build_type_attribute_variant (t1, attributes);
362 return build_type_attribute_variant (t2, attributes);
366 /* For two pointers, do this recursively on the target type,
367 and combine the qualifiers of the two types' targets. */
368 /* This code was turned off; I don't know why.
369 But ANSI C++ specifies doing this with the qualifiers.
370 So I turned it on again. */
372 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
373 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
375 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
377 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
378 target = cp_build_type_variant (target, constp, volatilep);
379 if (code1 == POINTER_TYPE)
380 t1 = build_pointer_type (target);
382 t1 = build_reference_type (target);
383 return build_type_attribute_variant (t1, attributes);
387 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
388 return build_type_attribute_variant (t1, attributes);
391 t1 = build_reference_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
392 return build_type_attribute_variant (t1, attributes);
397 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
398 /* Save space: see if the result is identical to one of the args. */
399 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
400 return build_type_attribute_variant (t1, attributes);
401 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
402 return build_type_attribute_variant (t2, attributes);
403 /* Merge the element types, and have a size if either arg has one. */
404 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
405 return build_type_attribute_variant (t1, attributes);
409 /* Function types: prefer the one that specified arg types.
410 If both do, merge the arg types. Also merge the return types. */
412 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
413 tree p1 = TYPE_ARG_TYPES (t1);
414 tree p2 = TYPE_ARG_TYPES (t2);
417 /* Save space: see if the result is identical to one of the args. */
418 if (valtype == TREE_TYPE (t1) && ! p2)
419 return build_type_attribute_variant (t1, attributes);
420 if (valtype == TREE_TYPE (t2) && ! p1)
421 return build_type_attribute_variant (t2, attributes);
423 /* Simple way if one arg fails to specify argument types. */
424 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
426 rval = build_function_type (valtype, p2);
427 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
428 rval = build_exception_variant (NULL_TREE, rval, raises);
429 return build_type_attribute_variant (rval, attributes);
431 raises = TYPE_RAISES_EXCEPTIONS (t1);
432 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
434 rval = build_function_type (valtype, p1);
436 rval = build_exception_variant (NULL_TREE, rval, raises);
437 return build_type_attribute_variant (rval, attributes);
440 rval = build_function_type (valtype, commonparms (p1, p2));
441 rval = build_exception_variant (NULL_TREE, rval, raises);
442 return build_type_attribute_variant (rval, attributes);
447 my_friendly_assert (TYPE_MAIN_VARIANT (t1) == t1
448 && TYPE_MAIN_VARIANT (t2) == t2, 306);
450 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
451 return build_type_attribute_variant (t1, attributes);
452 else if (binfo_or_else (t2, t1))
453 return build_type_attribute_variant (t2, attributes);
455 compiler_error ("common_type called with uncommon aggregate types");
458 if (comptypes (TYPE_METHOD_BASETYPE (t1), TYPE_METHOD_BASETYPE (t2), 1)
459 && TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
461 /* Get this value the long way, since TYPE_METHOD_BASETYPE
462 is just the main variant of this. */
463 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
466 raises = TYPE_RAISES_EXCEPTIONS (t1);
468 /* If this was a member function type, get back to the
469 original type of type member function (i.e., without
470 the class instance variable up front. */
471 t1 = build_function_type (TREE_TYPE (t1), TREE_CHAIN (TYPE_ARG_TYPES (t1)));
472 t2 = build_function_type (TREE_TYPE (t2), TREE_CHAIN (TYPE_ARG_TYPES (t2)));
473 t3 = common_type (t1, t2);
474 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3), TYPE_ARG_TYPES (t3));
475 t1 = build_exception_variant (basetype, t3, raises);
478 compiler_error ("common_type called with uncommon method types");
480 return build_type_attribute_variant (t1, attributes);
483 if (TYPE_OFFSET_BASETYPE (t1) == TYPE_OFFSET_BASETYPE (t2)
484 && TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
486 tree basetype = TYPE_OFFSET_BASETYPE (t1);
487 t1 = build_offset_type (basetype,
488 common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
491 compiler_error ("common_type called with uncommon member types");
493 /* ... falls through ... */
496 return build_type_attribute_variant (t1, attributes);
500 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
502 compexcepttypes (t1, t2, strict)
506 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
510 comp_array_types (cmp, t1, t2, strict)
511 register int (*cmp)();
515 tree d1 = TYPE_DOMAIN (t1);
516 tree d2 = TYPE_DOMAIN (t2);
518 /* Target types must match incl. qualifiers. */
519 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
520 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), strict)))
523 /* Sizes must match unless one is missing or variable. */
524 if (d1 == 0 || d2 == 0 || d1 == d2
525 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
526 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
527 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
528 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
531 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
532 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
533 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
534 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
535 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
536 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
537 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
538 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
541 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
542 or various other operations. This is what ANSI C++ speaks of as
545 For C++: argument STRICT says we should be strict about this
548 2 : strict, except that if one type is a reference and
549 the other is not, compare the target type of the
550 reference to the type that's not a reference (ARM, p308).
551 This is used for checking for invalid overloading.
552 1 : strict (compared according to ANSI C)
553 This is used for checking whether two function decls match.
554 0 : <= (compared according to C++)
555 -1: <= or >= (relaxed)
557 Otherwise, pointers involving base classes and derived classes
558 can be mixed as valid: i.e. a pointer to a base class may be assigned
559 to a pointer to one of its derived classes, as per C++. A pointer to
560 a derived class may be passed as a parameter to a function expecting a
561 pointer to a base classes. These allowances do not commute. In this
562 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
563 be the derived class. */
565 comptypes (type1, type2, strict)
569 register tree t1 = type1;
570 register tree t2 = type2;
573 /* Suppress errors caused by previously reported errors */
578 /* This should never happen. */
579 my_friendly_assert (t1 != error_mark_node, 307);
581 if (t2 == error_mark_node)
586 /* Treat an enum type as the unsigned integer type of the same width. */
588 if (TREE_CODE (t1) == ENUMERAL_TYPE)
589 t1 = type_for_size (TYPE_PRECISION (t1), 1);
590 if (TREE_CODE (t2) == ENUMERAL_TYPE)
591 t2 = type_for_size (TYPE_PRECISION (t2), 1);
597 /* Different classes of types can't be compatible. */
599 if (TREE_CODE (t1) != TREE_CODE (t2))
602 && ((TREE_CODE (t1) == REFERENCE_TYPE)
603 ^ (TREE_CODE (t2) == REFERENCE_TYPE)))
605 if (TREE_CODE (t1) == REFERENCE_TYPE)
606 return comptypes (TREE_TYPE (t1), t2, 1);
607 return comptypes (t1, TREE_TYPE (t2), 1);
615 /* Qualifiers must match. */
617 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
619 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
622 /* Allow for two different type nodes which have essentially the same
623 definition. Note that we already checked for equality of the type
624 type qualifiers (just above). */
626 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
629 #ifdef COMP_TYPE_ATTRIBUTES
630 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
633 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
637 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
640 switch (TREE_CODE (t1))
649 val = (comptypes (TYPE_POINTER_TO (TYPE_OFFSET_BASETYPE (t1)),
650 TYPE_POINTER_TO (TYPE_OFFSET_BASETYPE (t2)), strict)
651 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
655 if (! compexcepttypes (t1, t2, strict))
658 /* This case is anti-symmetrical!
659 One can pass a base member (or member function)
660 to something expecting a derived member (or member function),
661 but not vice-versa! */
663 val = (comptypes (TYPE_POINTER_TO (TYPE_METHOD_BASETYPE (t2)),
664 TYPE_POINTER_TO (TYPE_METHOD_BASETYPE (t1)), strict)
665 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
666 && compparms (TREE_CHAIN (TYPE_ARG_TYPES (t1)),
667 TREE_CHAIN (TYPE_ARG_TYPES (t2)), strict));
681 if (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE)
685 rval = t1 == t2 || UNIQUELY_DERIVED_FROM_P (t1, t2);
694 val = UNIQUELY_DERIVED_FROM_P (t2, t1);
701 val = comptypes (t1, t2, strict);
705 if (! compexcepttypes (t1, t2, strict))
708 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
709 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
710 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2), strict));
714 /* Target types must match incl. qualifiers. */
715 val = comp_array_types (comptypes, t1, t2, strict);
718 case TEMPLATE_TYPE_PARM:
721 case UNINSTANTIATED_P_TYPE:
722 if (UPT_TEMPLATE (t1) != UPT_TEMPLATE (t2))
725 int i = TREE_VEC_LENGTH (UPT_PARMS (t1));
726 tree *p1 = &TREE_VEC_ELT (UPT_PARMS (t1), 0);
727 tree *p2 = &TREE_VEC_ELT (UPT_PARMS (t2), 0);
731 if (TREE_CODE_CLASS (TREE_CODE (p1[i])) == 't')
733 if (! comptypes (p1[i], p2[i], 1))
738 if (simple_cst_equal (p1[i], p2[i]) <= 0)
745 return attrval == 2 && val == 1 ? 2 : val;
748 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
749 ignoring their qualifiers.
751 NPTRS is the number of pointers we can strip off and keep cool.
752 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
753 but to not permit B** to convert to A**. */
756 comp_target_types (ttl, ttr, nptrs)
760 ttl = TYPE_MAIN_VARIANT (ttl);
761 ttr = TYPE_MAIN_VARIANT (ttr);
765 if (TREE_CODE (ttr) != TREE_CODE (ttl))
768 if (TREE_CODE (ttr) == POINTER_TYPE)
770 if (TREE_CODE (TREE_TYPE (ttl)) == POINTER_TYPE
771 || TREE_CODE (TREE_TYPE (ttl)) == ARRAY_TYPE)
772 return comp_ptr_ttypes (TREE_TYPE (ttl), TREE_TYPE (ttr));
774 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs - 1);
777 if (TREE_CODE (ttr) == REFERENCE_TYPE)
778 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
779 if (TREE_CODE (ttr) == ARRAY_TYPE)
780 return comp_array_types (comp_target_types, ttl, ttr, 0);
781 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
782 if (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
783 switch (comp_target_parms (TYPE_ARG_TYPES (ttl), TYPE_ARG_TYPES (ttr), 1))
792 my_friendly_abort (112);
798 else if (TREE_CODE (ttr) == OFFSET_TYPE)
800 /* Contravariance: we can assign a pointer to base member to a pointer
801 to derived member. Note difference from simple pointer case, where
802 we can pass a pointer to derived to a pointer to base. */
803 if (comptypes (TYPE_OFFSET_BASETYPE (ttr), TYPE_OFFSET_BASETYPE (ttl), 0))
804 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
805 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl), TYPE_OFFSET_BASETYPE (ttr), 0)
806 && comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
809 else if (IS_AGGR_TYPE (ttl))
813 if (comptypes (TYPE_POINTER_TO (ttl), TYPE_POINTER_TO (ttr), 0))
815 if (comptypes (TYPE_POINTER_TO (ttr), TYPE_POINTER_TO (ttl), 0))
823 /* If two types share a common base type, return that basetype.
824 If there is not a unique most-derived base type, this function
825 returns ERROR_MARK_NODE. */
827 common_base_type (tt1, tt2)
830 tree best = NULL_TREE, tmp;
833 /* If one is a baseclass of another, that's good enough. */
834 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
836 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
840 /* If they share a virtual baseclass, that's good enough. */
841 for (tmp = CLASSTYPE_VBASECLASSES (tt1); tmp; tmp = TREE_CHAIN (tmp))
843 if (binfo_member (BINFO_TYPE (tmp), CLASSTYPE_VBASECLASSES (tt2)))
844 return BINFO_TYPE (tmp);
848 /* Otherwise, try to find a unique baseclass of TT1
849 that is shared by TT2, and follow that down. */
850 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
852 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
853 tree trial = common_base_type (basetype, tt2);
856 if (trial == error_mark_node)
858 if (best == NULL_TREE)
860 else if (best != trial)
861 return error_mark_node;
866 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
868 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
869 tree trial = common_base_type (tt1, basetype);
872 if (trial == error_mark_node)
874 if (best == NULL_TREE)
876 else if (best != trial)
877 return error_mark_node;
883 /* Subroutines of `comptypes'. */
885 /* Return 1 if two parameter type lists PARMS1 and PARMS2
886 are equivalent in the sense that functions with those parameter types
887 can have equivalent types.
888 If either list is empty, we win.
889 Otherwise, the two lists must be equivalent, element by element.
891 C++: See comment above about TYPE1, TYPE2, STRICT.
892 If STRICT == 3, it means checking is strict, but do not compare
893 default parameter values. */
895 compparms (parms1, parms2, strict)
899 register tree t1 = parms1, t2 = parms2;
901 /* An unspecified parmlist matches any specified parmlist
902 whose argument types don't need default promotions. */
904 if (strict <= 0 && t1 == 0)
905 return self_promoting_args_p (t2);
906 if (strict < 0 && t2 == 0)
907 return self_promoting_args_p (t1);
911 if (t1 == 0 && t2 == 0)
913 /* If one parmlist is shorter than the other,
914 they fail to match, unless STRICT is <= 0. */
915 if (t1 == 0 || t2 == 0)
922 return t1 && TREE_PURPOSE (t1);
924 if (! comptypes (TREE_VALUE (t2), TREE_VALUE (t1), strict))
929 return t2 == void_list_node && TREE_PURPOSE (t1);
930 return TREE_PURPOSE (t1) || TREE_PURPOSE (t2);
933 /* Default parms are not part of the type of a function. */
934 if (strict != 3 && TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
936 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
938 my_friendly_abort (113);
944 t1 = TREE_CHAIN (t1);
945 t2 = TREE_CHAIN (t2);
949 /* This really wants return whether or not parameter type lists
950 would make their owning functions assignment compatible or not. */
952 comp_target_parms (parms1, parms2, strict)
956 register tree t1 = parms1, t2 = parms2;
957 int warn_contravariance = 0;
959 /* An unspecified parmlist matches any specified parmlist
960 whose argument types don't need default promotions.
961 @@@ see 13.3.3 for a counterexample... */
963 if (t1 == 0 && t2 != 0)
965 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
967 return self_promoting_args_p (t2);
970 return self_promoting_args_p (t1);
972 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
976 /* If one parmlist is shorter than the other,
977 they fail to match, unless STRICT is <= 0. */
978 if (t1 == 0 || t2 == 0)
983 return 1 + warn_contravariance;
984 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
986 p1 = TREE_VALUE (t1);
987 p2 = TREE_VALUE (t2);
991 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
992 || (TREE_CODE (p1) == REFERENCE_TYPE && TREE_CODE (p2) == REFERENCE_TYPE))
995 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
996 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
999 /* The following is wrong for contravariance,
1000 but many programs depend on it. */
1001 if (TREE_TYPE (p1) == void_type_node)
1003 if (TREE_TYPE (p2) == void_type_node)
1005 warn_contravariance = 1;
1008 if (IS_AGGR_TYPE (TREE_TYPE (p1)))
1010 if (comptypes (p2, p1, 0) == 0)
1012 if (comptypes (p1, p2, 0) != 0)
1013 warn_contravariance = 1;
1020 /* Note backwards order due to contravariance. */
1021 if (comp_target_types (p2, p1, 1) == 0)
1023 if (comp_target_types (p1, p2, 1))
1025 warn_contravariance = 1;
1031 /* What good do these cases do? */
1033 return p2 == void_type_node && TREE_PURPOSE (t1);
1034 return TREE_PURPOSE (t1) || TREE_PURPOSE (t2);
1037 /* Target types are compatible--just make sure that if
1038 we use parameter lists, that they are ok as well. */
1039 if (TREE_CODE (p1) == FUNCTION_TYPE || TREE_CODE (p1) == METHOD_TYPE)
1040 switch (comp_target_parms (TYPE_ARG_TYPES (p1),
1041 TYPE_ARG_TYPES (p2),
1049 warn_contravariance = 1;
1052 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1054 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1056 my_friendly_abort (114);
1061 return 1 + warn_contravariance;
1064 /* Return 1 if PARMS specifies a fixed number of parameters
1065 and none of their types is affected by default promotions. */
1068 self_promoting_args_p (parms)
1072 for (t = parms; t; t = TREE_CHAIN (t))
1074 register tree type = TREE_VALUE (t);
1076 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1079 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1085 if (C_PROMOTING_INTEGER_TYPE_P (type))
1091 /* Return an unsigned type the same as TYPE in other respects.
1093 C++: must make these work for type variants as well. */
1096 unsigned_type (type)
1099 tree type1 = TYPE_MAIN_VARIANT (type);
1100 if (type1 == signed_char_type_node || type1 == char_type_node)
1101 return unsigned_char_type_node;
1102 if (type1 == integer_type_node)
1103 return unsigned_type_node;
1104 if (type1 == short_integer_type_node)
1105 return short_unsigned_type_node;
1106 if (type1 == long_integer_type_node)
1107 return long_unsigned_type_node;
1108 if (type1 == long_long_integer_type_node)
1109 return long_long_unsigned_type_node;
1113 /* Return a signed type the same as TYPE in other respects. */
1119 tree type1 = TYPE_MAIN_VARIANT (type);
1120 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1121 return signed_char_type_node;
1122 if (type1 == unsigned_type_node)
1123 return integer_type_node;
1124 if (type1 == short_unsigned_type_node)
1125 return short_integer_type_node;
1126 if (type1 == long_unsigned_type_node)
1127 return long_integer_type_node;
1128 if (type1 == long_long_unsigned_type_node)
1129 return long_long_integer_type_node;
1133 /* Return a type the same as TYPE except unsigned or
1134 signed according to UNSIGNEDP. */
1137 signed_or_unsigned_type (unsignedp, type)
1141 if (! INTEGRAL_TYPE_P (type))
1143 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1144 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1145 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1146 return unsignedp ? unsigned_type_node : integer_type_node;
1147 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1148 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1149 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1150 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1151 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1152 return (unsignedp ? long_long_unsigned_type_node
1153 : long_long_integer_type_node);
1161 enum tree_code code = TREE_CODE (type);
1164 if (code == FUNCTION_TYPE)
1166 if (pedantic || warn_pointer_arith)
1167 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1168 return size_int (1);
1170 if (code == METHOD_TYPE)
1172 if (pedantic || warn_pointer_arith)
1173 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1174 return size_int (1);
1176 if (code == VOID_TYPE)
1178 if (pedantic || warn_pointer_arith)
1179 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1180 return size_int (1);
1182 if (code == ERROR_MARK)
1183 return size_int (1);
1185 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1186 referenced object.'' */
1187 if (code == REFERENCE_TYPE)
1188 type = TREE_TYPE (type);
1190 /* We couldn't find anything in the ARM or the draft standard that says,
1191 one way or the other, if doing sizeof on something that doesn't have
1192 an object associated with it is correct or incorrect. For example, if
1193 you declare `struct S { char str[16]; };', and in your program do
1194 a `sizeof (S::str)', should we flag that as an error or should we give
1195 the size of it? Since it seems like a reasonable thing to do, we'll go
1196 with giving the value. */
1197 if (code == OFFSET_TYPE)
1198 type = TREE_TYPE (type);
1200 /* @@ This also produces an error for a signature ref.
1201 In that case we should be able to do better. */
1202 if (IS_SIGNATURE (type))
1204 error ("`sizeof' applied to a signature type");
1205 return size_int (0);
1208 if (TYPE_SIZE (type) == 0)
1210 error ("`sizeof' applied to an incomplete type");
1211 return size_int (0);
1214 /* Convert in case a char is more than one unit. */
1215 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1216 size_int (TYPE_PRECISION (char_type_node)));
1217 /* size_binop does not put the constant in range, so do it now. */
1218 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1219 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1224 c_sizeof_nowarn (type)
1227 enum tree_code code = TREE_CODE (type);
1230 if (code == FUNCTION_TYPE
1231 || code == METHOD_TYPE
1232 || code == VOID_TYPE
1233 || code == ERROR_MARK)
1234 return size_int (1);
1235 if (code == REFERENCE_TYPE)
1236 type = TREE_TYPE (type);
1238 if (TYPE_SIZE (type) == 0)
1241 /* ??? Tiemann, why have any diagnostic here?
1242 There is none in the corresponding function for C. */
1243 warning ("sizeof applied to an incomplete type");
1245 return size_int (0);
1248 /* Convert in case a char is more than one unit. */
1249 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1250 size_int (TYPE_PRECISION (char_type_node)));
1251 force_fit_type (t, 0);
1255 /* Implement the __alignof keyword: Return the minimum required
1256 alignment of TYPE, measured in bytes. */
1262 enum tree_code code = TREE_CODE (type);
1265 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1266 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1268 if (code == VOID_TYPE || code == ERROR_MARK)
1269 return size_int (1);
1271 /* C++: this is really correct! */
1272 if (code == REFERENCE_TYPE)
1273 type = TREE_TYPE (type);
1275 /* @@ This also produces an error for a signature ref.
1276 In that case we should be able to do better. */
1277 if (IS_SIGNATURE (type))
1279 error ("`__alignof' applied to a signature type");
1280 return size_int (1);
1283 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1284 force_fit_type (t, 0);
1288 /* Perform default promotions for C data used in expressions.
1289 Arrays and functions are converted to pointers;
1290 enumeral types or short or char, to int.
1291 In addition, manifest constants symbols are replaced by their values.
1293 C++: this will automatically bash references to their target type. */
1296 default_conversion (exp)
1299 register tree type = TREE_TYPE (exp);
1300 register enum tree_code code = TREE_CODE (type);
1302 if (code == OFFSET_TYPE /* || TREE_CODE (exp) == OFFSET_REF */ )
1304 if (TREE_CODE (exp) == OFFSET_REF)
1305 return default_conversion (resolve_offset_ref (exp));
1307 type = TREE_TYPE (type);
1308 code = TREE_CODE (type);
1311 if (code == REFERENCE_TYPE)
1313 exp = convert_from_reference (exp);
1314 type = TREE_TYPE (exp);
1315 code = TREE_CODE (type);
1318 /* Constants can be used directly unless they're not loadable. */
1319 if (TREE_CODE (exp) == CONST_DECL)
1320 exp = DECL_INITIAL (exp);
1321 /* Replace a nonvolatile const static variable with its value. */
1322 else if (TREE_READONLY_DECL_P (exp) && DECL_MODE (exp) != BLKmode)
1324 exp = decl_constant_value (exp);
1325 type = TREE_TYPE (exp);
1328 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1329 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1331 if (INTEGRAL_CODE_P (code))
1333 tree t = type_promotes_to (type);
1335 return convert (t, exp);
1337 if (flag_traditional
1338 && TYPE_MAIN_VARIANT (type) == float_type_node)
1339 return convert (double_type_node, exp);
1340 if (code == VOID_TYPE)
1342 error ("void value not ignored as it ought to be");
1343 return error_mark_node;
1345 if (code == FUNCTION_TYPE)
1347 return build_unary_op (ADDR_EXPR, exp, 0);
1349 if (code == METHOD_TYPE)
1351 if (TREE_CODE (exp) == OFFSET_REF)
1353 my_friendly_assert (TREE_CODE (TREE_OPERAND (exp, 1)) == FUNCTION_DECL,
1355 return build_unary_op (ADDR_EXPR, TREE_OPERAND (exp, 1), 0);
1357 return build_unary_op (ADDR_EXPR, exp, 0);
1359 if (code == ARRAY_TYPE)
1364 int constp, volatilep;
1366 if (TREE_CODE (exp) == INDIRECT_REF)
1368 /* Stripping away the INDIRECT_REF is not the right
1369 thing to do for references... */
1370 tree inner = TREE_OPERAND (exp, 0);
1371 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1373 inner = build1 (CONVERT_EXPR,
1374 build_pointer_type (TREE_TYPE (TREE_TYPE (inner))),
1376 TREE_REFERENCE_EXPR (inner) = 1;
1378 return convert (TYPE_POINTER_TO (TREE_TYPE (type)), inner);
1381 if (TREE_CODE (exp) == COMPOUND_EXPR)
1383 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1384 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1385 TREE_OPERAND (exp, 0), op1);
1389 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1391 error ("invalid use of non-lvalue array");
1392 return error_mark_node;
1395 constp = volatilep = 0;
1396 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1397 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1399 constp = TREE_READONLY (exp);
1400 volatilep = TREE_THIS_VOLATILE (exp);
1403 restype = TREE_TYPE (type);
1404 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1405 || constp || volatilep)
1406 restype = cp_build_type_variant (restype,
1407 TYPE_READONLY (type) || constp,
1408 TYPE_VOLATILE (type) || volatilep);
1409 ptrtype = build_pointer_type (restype);
1411 if (TREE_CODE (exp) == VAR_DECL)
1413 /* ??? This is not really quite correct
1414 in that the type of the operand of ADDR_EXPR
1415 is not the target type of the type of the ADDR_EXPR itself.
1416 Question is, can this lossage be avoided? */
1417 adr = build1 (ADDR_EXPR, ptrtype, exp);
1418 if (mark_addressable (exp) == 0)
1419 return error_mark_node;
1420 TREE_CONSTANT (adr) = staticp (exp);
1421 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1424 /* This way is better for a COMPONENT_REF since it can
1425 simplify the offset for a component. */
1426 adr = build_unary_op (ADDR_EXPR, exp, 1);
1427 return convert (ptrtype, adr);
1433 build_object_ref (datum, basetype, field)
1434 tree datum, basetype, field;
1437 if (datum == error_mark_node)
1438 return error_mark_node;
1440 dtype = TREE_TYPE (datum);
1441 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1442 dtype = TREE_TYPE (dtype);
1443 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1445 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1446 basetype, field, dtype);
1447 return error_mark_node;
1449 else if (IS_SIGNATURE (IDENTIFIER_TYPE_VALUE (basetype)))
1451 warning ("signature name in scope resolution ignored");
1452 return build_component_ref (datum, field, NULL_TREE, 1);
1454 else if (is_aggr_typedef (basetype, 1))
1456 tree real_basetype = IDENTIFIER_TYPE_VALUE (basetype);
1457 tree binfo = binfo_or_else (real_basetype, TREE_TYPE (datum));
1459 return build_component_ref (build_scoped_ref (datum, basetype),
1462 return error_mark_node;
1465 /* Like `build_component_ref, but uses an already found field.
1466 Must compute access for C_C_D. Otherwise, ok. */
1468 build_component_ref_1 (datum, field, protect)
1472 register tree basetype = TREE_TYPE (datum);
1473 register enum tree_code code = TREE_CODE (basetype);
1476 if (code == REFERENCE_TYPE)
1478 datum = convert_from_reference (datum);
1479 basetype = TREE_TYPE (datum);
1480 code = TREE_CODE (basetype);
1483 if (! IS_AGGR_TYPE_CODE (code))
1485 if (code != ERROR_MARK)
1486 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1487 field, datum, basetype);
1488 return error_mark_node;
1491 if (TYPE_SIZE (basetype) == 0)
1493 incomplete_type_error (0, basetype);
1494 return error_mark_node;
1497 /* Look up component name in the structure type definition. */
1499 if (field == error_mark_node)
1500 my_friendly_abort (115);
1502 if (TREE_STATIC (field))
1507 enum access_type access
1508 = compute_access (TYPE_BINFO (current_class_type), field);
1510 if (access == access_private)
1512 cp_error ("field `%D' is private", field);
1513 return error_mark_node;
1515 else if (access == access_protected)
1517 cp_error ("field `%D' is protected", field);
1518 return error_mark_node;
1522 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1524 if (TREE_READONLY (datum) || TREE_READONLY (field))
1525 TREE_READONLY (ref) = 1;
1526 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1527 TREE_THIS_VOLATILE (ref) = 1;
1528 if (DECL_MUTABLE_P (field))
1529 TREE_READONLY (ref) = 0;
1534 /* Given a COND_EXPR in T, return it in a form that we can, for
1535 example, use as an lvalue. This code used to be in unary_complex_lvalue,
1536 but we needed it to deal with `a = (d == c) ? b : c' expressions, where
1537 we're dealing with aggregates. So, we now call this in unary_complex_lvalue,
1538 and in build_modify_expr. The case (in particular) that led to this was
1539 with CODE == ADDR_EXPR, since it's not an lvalue when we'd get it there. */
1541 rationalize_conditional_expr (code, t)
1542 enum tree_code code;
1546 build_conditional_expr (TREE_OPERAND (t, 0),
1547 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1548 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1552 build_component_ref (datum, component, basetype_path, protect)
1553 tree datum, component, basetype_path;
1556 register tree basetype = TREE_TYPE (datum);
1557 register enum tree_code code = TREE_CODE (basetype);
1558 register tree field = NULL;
1561 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it. */
1562 switch (TREE_CODE (datum))
1566 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1567 basetype_path, protect);
1568 return build (COMPOUND_EXPR, TREE_TYPE (value),
1569 TREE_OPERAND (datum, 0), value);
1572 return build_conditional_expr
1573 (TREE_OPERAND (datum, 0),
1574 build_component_ref (TREE_OPERAND (datum, 1), component,
1575 basetype_path, protect),
1576 build_component_ref (TREE_OPERAND (datum, 2), component,
1577 basetype_path, protect));
1580 if (code == REFERENCE_TYPE)
1583 /* TREE_REFERENCE_EXPRs are not converted by `convert_from_reference'.
1584 @@ Maybe that is not right. */
1585 if (TREE_REFERENCE_EXPR (datum))
1586 datum = build1 (INDIRECT_REF, TREE_TYPE (basetype), datum);
1589 datum = convert_from_reference (datum);
1590 basetype = TREE_TYPE (datum);
1591 code = TREE_CODE (basetype);
1594 /* First, see if there is a field or component with name COMPONENT. */
1595 if (TREE_CODE (component) == TREE_LIST)
1597 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
1598 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
1599 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
1602 if (TREE_CODE (component) == TYPE_EXPR)
1603 return build_component_type_expr (datum, component, NULL_TREE, protect);
1606 if (! IS_AGGR_TYPE_CODE (code))
1608 if (code != ERROR_MARK)
1609 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1610 component, datum, basetype);
1611 return error_mark_node;
1614 if (TYPE_SIZE (basetype) == 0)
1616 incomplete_type_error (0, basetype);
1617 return error_mark_node;
1620 if (TREE_CODE (component) == BIT_NOT_EXPR)
1622 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
1624 cp_error ("destructor specifier `%T::~%T' must have matching names",
1625 basetype, TREE_OPERAND (component, 0));
1626 return error_mark_node;
1628 if (! TYPE_HAS_DESTRUCTOR (basetype))
1630 cp_error ("type `%T' has no destructor", basetype);
1631 return error_mark_node;
1633 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
1636 /* Look up component name in the structure type definition. */
1637 if (CLASSTYPE_VFIELD (basetype)
1638 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
1639 /* Special-case this because if we use normal lookups in an ambiguous
1640 hierarchy, the compiler will abort (because vptr lookups are
1641 not supposed to be ambiguous. */
1642 field = CLASSTYPE_VFIELD (basetype);
1645 if (basetype_path == NULL_TREE)
1646 basetype_path = TYPE_BINFO (basetype);
1647 field = lookup_field (basetype_path, component,
1648 protect && ! VFIELD_NAME_P (component), 0);
1649 if (field == error_mark_node)
1650 return error_mark_node;
1652 if (field == NULL_TREE)
1654 /* Not found as a data field, look for it as a method. If found,
1655 then if this is the only possible one, return it, else
1656 report ambiguity error. */
1657 tree fndecls = lookup_fnfields (basetype_path, component, 1);
1658 if (fndecls == error_mark_node)
1659 return error_mark_node;
1662 if (TREE_CHAIN (fndecls) == NULL_TREE
1663 && DECL_CHAIN (TREE_VALUE (fndecls)) == NULL_TREE)
1665 enum access_type access;
1668 /* Unique, so use this one now. */
1669 basetype = TREE_PURPOSE (fndecls);
1670 fndecl = TREE_VALUE (fndecls);
1671 access = compute_access (TREE_PURPOSE (fndecls), fndecl);
1672 if (access == access_public)
1674 if (DECL_VINDEX (fndecl)
1675 && ! resolves_to_fixed_type_p (datum, 0))
1677 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1678 addr = convert_pointer_to (DECL_CONTEXT (fndecl), addr);
1679 datum = build_indirect_ref (addr, NULL_PTR);
1680 my_friendly_assert (datum != error_mark_node, 310);
1681 fndecl = build_vfn_ref (&addr, datum, DECL_VINDEX (fndecl));
1685 if (access == access_protected)
1686 cp_error ("member function `%D' is protected", fndecl);
1688 cp_error ("member function `%D' is private", fndecl);
1689 return error_mark_node;
1693 /* Just act like build_offset_ref, since the object does
1694 not matter unless we're actually calling the function. */
1695 tree t = build_tree_list (error_mark_node, fndecls);
1696 TREE_TYPE (t) = build_offset_type (basetype,
1703 if (component == ansi_opname[(int) TYPE_EXPR])
1704 cp_error ("`%#T' has no such type conversion operator", basetype);
1707 cp_error ("`%#T' has no member named `%D'", basetype, component);
1708 return error_mark_node;
1710 else if (TREE_TYPE (field) == error_mark_node)
1711 return error_mark_node;
1713 if (TREE_CODE (field) != FIELD_DECL)
1715 if (TREE_CODE (field) == TYPE_DECL)
1717 cp_error ("invalid use of type decl `%#D' as expression", field);
1718 return error_mark_node;
1720 if (DECL_RTL (field) != 0)
1721 assemble_external (field);
1722 TREE_USED (field) = 1;
1727 if (DECL_FIELD_CONTEXT (field) != basetype
1728 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
1730 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1731 if (integer_zerop (addr))
1733 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1734 return error_mark_node;
1736 addr = convert_pointer_to (DECL_FIELD_CONTEXT (field), addr);
1737 datum = build_indirect_ref (addr, NULL_PTR);
1738 my_friendly_assert (datum != error_mark_node, 311);
1740 ref = fold (build (COMPONENT_REF, TREE_TYPE (field),
1741 break_out_cleanups (datum), field));
1743 if (TREE_READONLY (datum) || TREE_READONLY (field))
1744 TREE_READONLY (ref) = 1;
1745 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1746 TREE_THIS_VOLATILE (ref) = 1;
1747 if (DECL_MUTABLE_P (field))
1748 TREE_READONLY (ref) = 0;
1753 /* Given an expression PTR for a pointer, return an expression
1754 for the value pointed to.
1755 ERRORSTRING is the name of the operator to appear in error messages.
1757 This function may need to overload OPERATOR_FNNAME.
1758 Must also handle REFERENCE_TYPEs for C++. */
1761 build_x_indirect_ref (ptr, errorstring)
1765 tree rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE, NULL_TREE);
1768 return build_indirect_ref (ptr, errorstring);
1772 build_indirect_ref (ptr, errorstring)
1776 register tree pointer = default_conversion (ptr);
1777 register tree type = TREE_TYPE (pointer);
1779 if (ptr == current_class_decl)
1782 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
1784 if (TREE_CODE (pointer) == ADDR_EXPR
1785 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1786 == TREE_TYPE (type)))
1787 return TREE_OPERAND (pointer, 0);
1790 tree t = TREE_TYPE (type);
1791 register tree ref = build1 (INDIRECT_REF,
1792 TYPE_MAIN_VARIANT (t), pointer);
1794 TREE_READONLY (ref) = TYPE_READONLY (t);
1795 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1796 TREE_SIDE_EFFECTS (ref)
1797 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1801 /* `pointer' won't be an error_mark_node if we were given a
1802 pointer to member, so it's cool to check for this here. */
1803 else if (TYPE_PTRMEMFUNC_P (type))
1804 error ("invalid use of `%s' on pointer to member function", errorstring);
1805 else if (TREE_CODE (type) == RECORD_TYPE
1806 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1807 error ("cannot dereference signature pointer/reference");
1808 else if (pointer != error_mark_node)
1811 error ("invalid type argument of `%s'", errorstring);
1813 error ("invalid type argument");
1815 return error_mark_node;
1818 /* This handles expressions of the form "a[i]", which denotes
1821 This is logically equivalent in C to *(a+i), but we may do it differently.
1822 If A is a variable or a member, we generate a primitive ARRAY_REF.
1823 This avoids forcing the array out of registers, and can work on
1824 arrays that are not lvalues (for example, members of structures returned
1827 If INDEX is of some user-defined type, it must be converted to
1828 integer type. Otherwise, to make a compatible PLUS_EXPR, it
1829 will inherit the type of the array, which will be some pointer type. */
1832 build_x_array_ref (array, index)
1835 tree rval = build_opfncall (ARRAY_REF, LOOKUP_NORMAL, array, index, NULL_TREE);
1838 return build_array_ref (array, index);
1842 build_array_ref (array, idx)
1849 error ("subscript missing in array reference");
1850 return error_mark_node;
1853 if (TREE_TYPE (array) == error_mark_node
1854 || TREE_TYPE (idx) == error_mark_node)
1855 return error_mark_node;
1857 itype = TREE_TYPE (idx);
1859 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1860 && TREE_CODE (array) != INDIRECT_REF)
1864 /* Subscripting with type char is likely to lose
1865 on a machine where chars are signed.
1866 So warn on any machine, but optionally.
1867 Don't warn for unsigned char since that type is safe.
1868 Don't warn for signed char because anyone who uses that
1869 must have done so deliberately. */
1870 if (warn_char_subscripts
1871 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
1872 warning ("array subscript has type `char'");
1874 /* Apply default promotions *after* noticing character types. */
1875 idx = default_conversion (idx);
1877 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
1879 error ("array subscript is not an integer");
1880 return error_mark_node;
1883 /* An array that is indexed by a non-constant
1884 cannot be stored in a register; we must be able to do
1885 address arithmetic on its address.
1886 Likewise an array of elements of variable size. */
1887 if (TREE_CODE (idx) != INTEGER_CST
1888 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1889 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1891 if (mark_addressable (array) == 0)
1892 return error_mark_node;
1894 /* An array that is indexed by a constant value which is not within
1895 the array bounds cannot be stored in a register either; because we
1896 would get a crash in store_bit_field/extract_bit_field when trying
1897 to access a non-existent part of the register. */
1898 if (TREE_CODE (idx) == INTEGER_CST
1899 && TYPE_VALUES (TREE_TYPE (array))
1900 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
1902 if (mark_addressable (array) == 0)
1903 return error_mark_node;
1906 if (pedantic && !lvalue_p (array))
1907 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
1909 /* Note in C++ it is valid to subscript a `register' array, since
1910 it is valid to take the address of something with that
1911 storage specification. */
1915 while (TREE_CODE (foo) == COMPONENT_REF)
1916 foo = TREE_OPERAND (foo, 0);
1917 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1918 warning ("subscripting array declared `register'");
1921 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1922 rval = build (ARRAY_REF, type, array, idx);
1923 /* Array ref is const/volatile if the array elements are
1924 or if the array is.. */
1925 TREE_READONLY (rval)
1926 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1927 | TREE_READONLY (array));
1928 TREE_SIDE_EFFECTS (rval)
1929 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1930 | TREE_SIDE_EFFECTS (array));
1931 TREE_THIS_VOLATILE (rval)
1932 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1933 /* This was added by rms on 16 Nov 91.
1934 It fixes vol struct foo *a; a->elts[1]
1935 in an inline function.
1936 Hope it doesn't break something else. */
1937 | TREE_THIS_VOLATILE (array));
1938 return require_complete_type (fold (rval));
1942 tree ar = default_conversion (array);
1943 tree ind = default_conversion (idx);
1945 /* Put the integer in IND to simplify error checking. */
1946 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1953 if (ar == error_mark_node)
1956 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
1958 error ("subscripted value is neither array nor pointer");
1959 return error_mark_node;
1961 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1963 error ("array subscript is not an integer");
1964 return error_mark_node;
1967 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar, ind, PLUS_EXPR),
1972 /* Build a function call to function FUNCTION with parameters PARAMS.
1973 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1974 TREE_VALUE of each node is a parameter-expression.
1975 FUNCTION's data type may be a function type or a pointer-to-function.
1977 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
1978 is the list of possible methods that FUNCTION could conceivably
1979 be. If the list of methods comes from a class, then it will be
1980 a list of lists (where each element is associated with the class
1981 that produced it), otherwise it will be a simple list (for
1982 functions overloaded in global scope).
1984 In the first case, TREE_VALUE (function) is the head of one of those
1985 lists, and TREE_PURPOSE is the name of the function.
1987 In the second case, TREE_PURPOSE (function) is the function's
1990 DECL is the class instance variable, usually CURRENT_CLASS_DECL. */
1993 * [eichin:19911015.1726EST] actually return a possibly incomplete
1997 build_x_function_call (function, params, decl)
1998 tree function, params, decl;
2003 if (function == error_mark_node)
2004 return error_mark_node;
2006 type = TREE_TYPE (function);
2007 is_method = ((TREE_CODE (function) == TREE_LIST
2008 && current_class_type != NULL_TREE
2009 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function)) == function)
2010 || TREE_CODE (function) == IDENTIFIER_NODE
2011 || TREE_CODE (type) == METHOD_TYPE
2012 || TYPE_PTRMEMFUNC_P (type));
2014 /* Handle methods, friends, and overloaded functions, respectively. */
2017 if (TREE_CODE (function) == FUNCTION_DECL)
2019 if (DECL_NAME (function))
2020 function = DECL_NAME (function);
2022 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2024 else if (TREE_CODE (function) == TREE_LIST)
2027 if (TREE_CODE (TREE_VALUE (function)) == TREE_LIST)
2028 function = TREE_PURPOSE (TREE_VALUE (function));
2030 function = TREE_PURPOSE (function);
2032 my_friendly_assert (TREE_CODE (TREE_VALUE (function)) == FUNCTION_DECL, 312);
2033 function = TREE_PURPOSE (function);
2036 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2038 if (TREE_CODE (function) == OFFSET_REF)
2040 if (TREE_OPERAND (function, 0))
2041 decl = TREE_OPERAND (function, 0);
2043 /* Call via a pointer to member function. */
2044 if (decl == NULL_TREE)
2046 error ("pointer to member function called, but not in class scope");
2047 return error_mark_node;
2049 /* What other type of POINTER_TYPE could this be? */
2050 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2051 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2052 && TREE_CODE (function) != OFFSET_REF)
2053 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE, function);
2057 /* this is an abbreviated method call.
2058 must go through here in case it is a virtual function.
2059 @@ Perhaps this could be optimized. */
2061 if (decl == NULL_TREE)
2063 if (current_class_type == NULL_TREE)
2065 error ("object missing in call to method `%s'",
2066 IDENTIFIER_POINTER (function));
2067 return error_mark_node;
2069 /* Yow: call from a static member function. */
2070 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (current_class_type),
2072 decl = build_indirect_ref (decl, NULL_PTR);
2075 return build_method_call (decl, function, params,
2076 NULL_TREE, LOOKUP_NORMAL);
2078 else if (TREE_CODE (function) == COMPONENT_REF
2079 && type == unknown_type_node)
2081 /* Should we undo what was done in build_component_ref? */
2082 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function, 1))) == TREE_VEC)
2083 /* Get the name that build_component_ref hid. */
2084 function = DECL_NAME (TREE_VALUE (TREE_OPERAND (function, 1)));
2086 function = TREE_PURPOSE (TREE_OPERAND (function, 1));
2087 return build_method_call (decl, function, params,
2088 NULL_TREE, LOOKUP_NORMAL);
2090 else if (TREE_CODE (function) == TREE_LIST)
2092 if (TREE_VALUE (function) == NULL_TREE)
2094 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2095 TREE_PURPOSE (function));
2096 return error_mark_node;
2100 tree val = TREE_VALUE (function);
2102 if (TREE_CODE (val) == TEMPLATE_DECL)
2103 return build_overload_call_maybe
2104 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0);
2105 else if (DECL_CHAIN (val) != NULL_TREE)
2106 return build_overload_call
2107 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0);
2109 my_friendly_abort (360);
2114 if (TREE_CODE (function) == OFFSET_REF)
2116 /* If the component is a data element (or a virtual function), we play
2117 games here to make things work. */
2120 if (TREE_OPERAND (function, 0))
2121 decl = TREE_OPERAND (function, 0);
2125 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2126 function = get_member_function_from_ptrfunc (&decl_addr,
2127 TREE_OPERAND (function, 1));
2128 params = tree_cons (NULL_TREE, decl_addr, params);
2129 return build_function_call (function, params);
2132 type = TREE_TYPE (function);
2133 if (type != error_mark_node)
2135 if (TREE_CODE (type) == REFERENCE_TYPE)
2136 type = TREE_TYPE (type);
2138 if (TYPE_LANG_SPECIFIC (type) && TYPE_OVERLOADS_CALL_EXPR (type))
2139 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2144 tree fntype = TREE_TYPE (function);
2147 /* Explicitly named method? */
2148 if (TREE_CODE (function) == FUNCTION_DECL)
2149 ctypeptr = TYPE_POINTER_TO (DECL_CLASS_CONTEXT (function));
2150 /* Expression with ptr-to-method type? It could either be a plain
2151 usage, or it might be a case where the ptr-to-method is being
2152 passed in as an argument. */
2153 else if (TYPE_PTRMEMFUNC_P (fntype))
2155 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2156 ctypeptr = TYPE_POINTER_TO (rec);
2158 /* Unexpected node type? */
2160 my_friendly_abort (116);
2161 if (decl == NULL_TREE)
2163 if (current_function_decl
2164 && DECL_STATIC_FUNCTION_P (current_function_decl))
2165 error ("invalid call to member function needing `this' in static member function scope");
2167 error ("pointer to member function called, but not in class scope");
2168 return error_mark_node;
2170 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2171 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2173 decl = build_unary_op (ADDR_EXPR, decl, 0);
2174 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2177 decl = build_c_cast (ctypeptr, decl, 0);
2178 params = tree_cons (NULL_TREE, decl, params);
2181 return build_function_call (function, params);
2184 /* Resolve a pointer to member function. INSTANCE is the object
2185 instance to use, if the member points to a virtual member. */
2188 get_member_function_from_ptrfunc (instance_ptrptr, function)
2189 tree *instance_ptrptr;
2192 if (TREE_CODE (function) == OFFSET_REF)
2194 function = TREE_OPERAND (function, 1);
2197 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2199 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2200 tree index = save_expr (build_component_ref (function,
2203 tree e1 = build (GT_EXPR, boolean_type_node, index,
2204 convert (delta_type_node, integer_zero_node));
2205 tree delta = convert (ptrdiff_type_node,
2206 build_component_ref (function, delta_identifier, 0, 0));
2207 tree delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2213 tree instance_ptr = *instance_ptrptr;
2215 if (TREE_SIDE_EFFECTS (instance_ptr))
2216 instance_ptr = save_expr (instance_ptr);
2218 /* convert down to the right base, before using the instance. */
2220 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)),
2222 if (instance == error_mark_node)
2225 vtbl = convert_pointer_to (ptr_type_node, instance);
2228 build_pointer_type (build_pointer_type (vtable_entry_type)),
2229 vtbl, convert (ptrdiff_type_node, delta2));
2230 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2231 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2233 integer_one_node, 1));
2234 if (! flag_vtable_thunks)
2236 aref = save_expr (aref);
2238 /* Save the intermediate result in a SAVE_EXPR so we don't have to
2239 compute each component of the virtual function pointer twice. */
2240 if (/* !building_cleanup && */ TREE_CODE (aref) == INDIRECT_REF)
2241 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
2243 delta = build_binary_op (PLUS_EXPR,
2244 build_conditional_expr (e1, build_component_ref (aref, delta_identifier, 0, 0), integer_zero_node),
2248 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2249 instance_ptr, delta);
2250 if (flag_vtable_thunks)
2253 e2 = build_component_ref (aref, pfn_identifier, 0, 0);
2255 e3 = PFN_FROM_PTRMEMFUNC (function);
2256 TREE_TYPE (e2) = TREE_TYPE (e3);
2257 function = build_conditional_expr (e1, e2, e3);
2259 /* Make sure this doesn't get evaluated first inside one of the
2260 branches of the COND_EXPR. */
2261 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2262 function = build (COMPOUND_EXPR, TREE_TYPE (function),
2263 instance_ptr, function);
2269 build_function_call_real (function, params, require_complete, flags)
2270 tree function, params;
2271 int require_complete, flags;
2273 register tree fntype, fndecl;
2274 register tree value_type;
2275 register tree coerced_params;
2276 tree name = NULL_TREE, assembler_name = NULL_TREE;
2279 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2280 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2281 if (TREE_CODE (function) == NOP_EXPR
2282 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2283 function = TREE_OPERAND (function, 0);
2285 if (TREE_CODE (function) == FUNCTION_DECL)
2287 name = DECL_NAME (function);
2288 assembler_name = DECL_ASSEMBLER_NAME (function);
2290 GNU_xref_call (current_function_decl,
2291 IDENTIFIER_POINTER (name ? name
2292 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function))));
2293 assemble_external (function);
2296 /* Convert anything with function type to a pointer-to-function. */
2299 && IDENTIFIER_LENGTH (name) == 4
2300 && ! strcmp (IDENTIFIER_POINTER (name), "main")
2301 && DECL_CONTEXT (function) == NULL_TREE)
2303 pedwarn ("ANSI C++ forbids calling `main' from within program");
2306 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2307 (because calling an inline function does not mean the function
2308 needs to be separately compiled). */
2310 if (DECL_INLINE (function))
2312 fntype = build_type_variant (TREE_TYPE (function),
2313 TREE_READONLY (function),
2314 TREE_THIS_VOLATILE (function));
2315 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
2319 assemble_external (function);
2320 TREE_USED (function) = 1;
2321 function = default_conversion (function);
2328 /* Convert anything with function type to a pointer-to-function. */
2329 if (function == error_mark_node)
2330 return error_mark_node;
2331 function = default_conversion (function);
2334 fntype = TREE_TYPE (function);
2336 if (TYPE_PTRMEMFUNC_P (fntype))
2338 tree instance_ptr = build_unary_op (ADDR_EXPR, C_C_D, 0);
2339 fntype = TYPE_PTRMEMFUNC_FN_TYPE (fntype);
2340 function = get_member_function_from_ptrfunc (&instance_ptr, function);
2343 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2344 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2346 if (!((TREE_CODE (fntype) == POINTER_TYPE
2347 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2350 error ("called object is not a function");
2351 return error_mark_node;
2354 /* fntype now gets the type of function pointed to. */
2355 fntype = TREE_TYPE (fntype);
2357 /* Convert the parameters to the types declared in the
2358 function prototype, or apply default promotions. */
2360 if (flags & LOOKUP_COMPLAIN)
2361 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2362 params, fndecl, LOOKUP_NORMAL);
2364 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2367 /* Check for errors in format strings. */
2369 if (warn_format && (name || assembler_name))
2370 check_function_format (name, assembler_name, coerced_params);
2372 /* Recognize certain built-in functions so we can make tree-codes
2373 other than CALL_EXPR. We do this when it enables fold-const.c
2374 to do something useful. */
2376 if (TREE_CODE (function) == ADDR_EXPR
2377 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2378 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2379 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
2384 if (coerced_params == 0)
2385 return integer_zero_node;
2386 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2390 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2392 register tree result =
2393 build (CALL_EXPR, value_type,
2394 function, coerced_params, NULL_TREE);
2396 TREE_SIDE_EFFECTS (result) = 1;
2398 if (! require_complete)
2399 return convert_from_reference (result);
2400 if (value_type == void_type_node)
2402 result = require_complete_type (result);
2403 return convert_from_reference (result);
2408 build_function_call (function, params)
2409 tree function, params;
2411 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2415 build_function_call_maybe (function, params)
2416 tree function, params;
2418 return build_function_call_real (function, params, 0, 0);
2422 /* Convert the actual parameter expressions in the list VALUES
2423 to the types in the list TYPELIST.
2424 If parmdecls is exhausted, or when an element has NULL as its type,
2425 perform the default conversions.
2427 RETURN_LOC is the location of the return value, if known, NULL_TREE
2428 otherwise. This is useful in the case where we can avoid creating
2429 a temporary variable in the case where we can initialize the return
2430 value directly. If we are not eliding constructors, then we set this
2431 to NULL_TREE to avoid this avoidance.
2433 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2435 This is also where warnings about wrong number of args are generated.
2437 Return a list of expressions for the parameters as converted.
2439 Both VALUES and the returned value are chains of TREE_LIST nodes
2440 with the elements of the list in the TREE_VALUE slots of those nodes.
2442 In C++, unspecified trailing parameters can be filled in with their
2443 default arguments, if such were specified. Do so here. */
2446 convert_arguments (return_loc, typelist, values, fndecl, flags)
2447 tree return_loc, typelist, values, fndecl;
2450 extern tree gc_protect_fndecl;
2451 register tree typetail, valtail;
2452 register tree result = NULL_TREE;
2456 if (! flag_elide_constructors)
2461 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2463 if (DECL_NAME (fndecl) == NULL_TREE
2464 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2465 called_thing = "constructor";
2467 called_thing = "member function";
2470 called_thing = "function";
2473 for (valtail = values, typetail = typelist;
2475 valtail = TREE_CHAIN (valtail), i++)
2477 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2478 register tree val = TREE_VALUE (valtail);
2480 if (val == error_mark_node)
2483 if (type == void_type_node)
2487 char *buf = (char *)alloca (40 + strlen (called_thing));
2488 sprintf (buf, "too many arguments to %s `%%s'", called_thing);
2489 error_with_decl (fndecl, buf);
2490 error ("at this point in file");
2493 error ("too many arguments to function");
2494 /* In case anybody wants to know if this argument
2497 TREE_TYPE (tree_last (result)) = error_mark_node;
2501 /* The tree type of the parameter being passed may not yet be
2502 known. In this case, its type is TYPE_UNKNOWN, and will
2503 be instantiated by the type given by TYPE. If TYPE
2504 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2505 if (type && type_unknown_p (val))
2506 val = require_instantiated_type (type, val, integer_zero_node);
2507 else if (type_unknown_p (val))
2509 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2510 if (TREE_CODE (val) == ADDR_EXPR)
2511 val = TREE_OPERAND (val, 0);
2512 if (TREE_CODE (val) == TREE_LIST
2513 && TREE_CHAIN (val) == NULL_TREE
2514 && TREE_TYPE (TREE_VALUE (val)) != NULL_TREE
2515 && (TREE_TYPE (val) == unknown_type_node
2516 || DECL_CHAIN (TREE_VALUE (val)) == NULL_TREE))
2517 /* Instantiates automatically. */
2518 val = TREE_VALUE (val);
2521 error ("insufficient type information in parameter list");
2522 val = integer_zero_node;
2525 else if (TREE_CODE (val) == OFFSET_REF
2526 && TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2528 /* This is unclean. Should be handled elsewhere. */
2529 val = build_unary_op (ADDR_EXPR, val, 0);
2531 else if (TREE_CODE (val) == OFFSET_REF)
2532 val = resolve_offset_ref (val);
2536 /* This code forces the assumption that if we have a ptr-to-func
2537 type in an arglist, that every routine that wants to check
2538 its validity has done so, and thus we need not do any
2539 more conversion. I don't remember why this is necessary. */
2540 else if (TREE_CODE (ttype) == FUNCTION_TYPE
2542 || TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
2543 || TREE_CODE (TREE_TYPE (type)) == VOID_TYPE))
2545 type = build_pointer_type (ttype);
2550 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2551 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2552 if (TREE_CODE (val) == NOP_EXPR
2553 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2554 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2555 val = TREE_OPERAND (val, 0);
2557 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2559 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2560 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2561 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2562 val = default_conversion (val);
2564 val = require_complete_type (val);
2567 if (val == error_mark_node)
2572 /* Formal parm type is specified by a function prototype. */
2575 if (TYPE_SIZE (type) == 0)
2577 error ("parameter type of called function is incomplete");
2582 #if 0 && defined (PROMOTE_PROTOTYPES)
2583 /* This breaks user-defined conversions. */
2584 /* Rather than truncating and then reextending,
2585 convert directly to int, if that's the type we will want. */
2586 if (! flag_traditional
2587 && (TREE_CODE (type) == INTEGER_TYPE
2588 || TREE_CODE (type) == ENUMERAL_TYPE)
2589 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2590 type = integer_type_node;
2592 parmval = convert_for_initialization (return_loc, type, val, flags,
2593 "argument passing", fndecl, i);
2594 #ifdef PROMOTE_PROTOTYPES
2595 if ((TREE_CODE (type) == INTEGER_TYPE
2596 || TREE_CODE (type) == ENUMERAL_TYPE)
2597 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2598 parmval = default_conversion (parmval);
2601 result = tree_cons (NULL_TREE, parmval, result);
2605 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2606 val = convert_from_reference (val);
2608 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2609 && (TYPE_PRECISION (TREE_TYPE (val))
2610 < TYPE_PRECISION (double_type_node)))
2611 /* Convert `float' to `double'. */
2612 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2613 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
2614 && (TYPE_HAS_INIT_REF (TREE_TYPE (val))
2615 || TYPE_HAS_ASSIGN_REF (TREE_TYPE (val))))
2617 cp_warning ("cannot pass objects of type `%T' through `...'",
2619 result = tree_cons (NULL_TREE, val, result);
2622 /* Convert `short' and `char' to full-size `int'. */
2623 result = tree_cons (NULL_TREE, default_conversion (val), result);
2627 /* There are certain functions for which we don't need
2628 to protect our arguments. GC_PROTECT_FNDECL is one. */
2629 && fndecl != gc_protect_fndecl
2630 && type_needs_gc_entry (TREE_TYPE (TREE_VALUE (result)))
2631 && ! value_safe_from_gc (NULL_TREE, TREE_VALUE (result)))
2632 /* This will build a temporary variable whose cleanup is
2633 to clear the obstack entry. */
2634 TREE_VALUE (result) = protect_value_from_gc (NULL_TREE,
2635 TREE_VALUE (result));
2638 typetail = TREE_CHAIN (typetail);
2641 if (typetail != 0 && typetail != void_list_node)
2643 /* See if there are default arguments that can be used */
2644 if (TREE_PURPOSE (typetail))
2646 for (; typetail != void_list_node; ++i)
2648 tree type = TREE_VALUE (typetail);
2649 tree val = TREE_PURPOSE (typetail);
2652 if (val == NULL_TREE)
2653 parmval = error_mark_node;
2654 else if (TREE_CODE (val) == CONSTRUCTOR)
2656 parmval = digest_init (type, val, (tree *)0);
2657 parmval = convert_for_initialization (return_loc, type, parmval, flags,
2658 "default constructor", fndecl, i);
2662 /* This could get clobbered by the following call. */
2663 if (TREE_HAS_CONSTRUCTOR (val))
2664 val = copy_node (val);
2666 parmval = convert_for_initialization (return_loc, type, val, flags,
2667 "default argument", fndecl, i);
2668 #ifdef PROMOTE_PROTOTYPES
2669 if ((TREE_CODE (type) == INTEGER_TYPE
2670 || TREE_CODE (type) == ENUMERAL_TYPE)
2671 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2672 parmval = default_conversion (parmval);
2677 && type_needs_gc_entry (TREE_TYPE (parmval))
2678 && ! value_safe_from_gc (NULL_TREE, parmval))
2679 parmval = protect_value_from_gc (NULL_TREE, parmval);
2681 result = tree_cons (0, parmval, result);
2682 typetail = TREE_CHAIN (typetail);
2683 /* ends with `...'. */
2684 if (typetail == NULL_TREE)
2692 char *buf = (char *)alloca (32 + strlen (called_thing));
2693 sprintf (buf, "too few arguments to %s `%%#D'", called_thing);
2694 cp_error_at (buf, fndecl);
2695 error ("at this point in file");
2698 error ("too few arguments to function");
2699 return error_mark_list;
2703 return nreverse (result);
2706 /* Build a binary-operation expression, after performing default
2707 conversions on the operands. CODE is the kind of expression to build. */
2710 build_x_binary_op (code, arg1, arg2)
2711 enum tree_code code;
2714 tree rval = build_opfncall (code, LOOKUP_SPECULATIVELY,
2715 arg1, arg2, NULL_TREE);
2717 return build_opfncall (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2718 if (code == MEMBER_REF)
2719 return build_m_component_ref (build_indirect_ref (arg1, NULL_PTR),
2721 return build_binary_op (code, arg1, arg2, 1);
2725 build_binary_op (code, arg1, arg2, convert_p)
2726 enum tree_code code;
2739 args[0] = args_save [0] = default_conversion (args[0]);
2740 args[1] = args_save [1] = default_conversion (args[1]);
2742 if (args[0] == error_mark_node || args[1] == error_mark_node)
2743 return error_mark_node;
2745 type0 = TREE_TYPE (args[0]);
2746 type1 = TREE_TYPE (args[1]);
2748 if (type_unknown_p (args[0]))
2750 args[0] = instantiate_type (type1, args[0], 1);
2751 args[0] = default_conversion (args[0]);
2753 else if (type_unknown_p (args[1]))
2755 args[1] = require_instantiated_type (type0, args[1],
2757 args[1] = default_conversion (args[1]);
2760 if (IS_AGGR_TYPE (type0) || IS_AGGR_TYPE (type1))
2762 /* Try to convert this to something reasonable. */
2763 if (! build_default_binary_type_conversion(code, &args[0], &args[1]))
2765 cp_error ("no match for `%O(%#T, %#T)'", code,
2766 TREE_TYPE (arg1), TREE_TYPE (arg2));
2767 return error_mark_node;
2771 if (args[0] == args_save[0])
2773 if (args[1] == args_save[1])
2776 return build_binary_op_nodefault (code, args[0], args[1], code);
2779 /* Build a binary-operation expression without default conversions.
2780 CODE is the kind of expression to build.
2781 This function differs from `build' in several ways:
2782 the data type of the result is computed and recorded in it,
2783 warnings are generated if arg data types are invalid,
2784 special handling for addition and subtraction of pointers is known,
2785 and some optimization is done (operations on narrow ints
2786 are done in the narrower type when that gives the same result).
2787 Constant folding is also done before the result is returned.
2789 ERROR_CODE is the code that determines what to say in error messages.
2790 It is usually, but not always, the same as CODE.
2792 Note that the operands will never have enumeral types
2793 because either they have just had the default conversions performed
2794 or they have both just been converted to some other type in which
2795 the arithmetic is to be done.
2797 C++: must do special pointer arithmetic when implementing
2798 multiple inheritance, and deal with pointer to member functions. */
2801 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
2802 enum tree_code code;
2803 tree orig_op0, orig_op1;
2804 enum tree_code error_code;
2807 register enum tree_code code0, code1;
2810 /* Expression code to give to the expression when it is built.
2811 Normally this is CODE, which is what the caller asked for,
2812 but in some special cases we change it. */
2813 register enum tree_code resultcode = code;
2815 /* Data type in which the computation is to be performed.
2816 In the simplest cases this is the common type of the arguments. */
2817 register tree result_type = NULL;
2819 /* Nonzero means operands have already been type-converted
2820 in whatever way is necessary.
2821 Zero means they need to be converted to RESULT_TYPE. */
2824 /* Nonzero means after finally constructing the expression
2825 give it this type. Otherwise, give it type RESULT_TYPE. */
2826 tree final_type = 0;
2828 /* Nonzero if this is an operation like MIN or MAX which can
2829 safely be computed in short if both args are promoted shorts.
2830 Also implies COMMON.
2831 -1 indicates a bitwise operation; this makes a difference
2832 in the exact conditions for when it is safe to do the operation
2833 in a narrower mode. */
2836 /* Nonzero if this is a comparison operation;
2837 if both args are promoted shorts, compare the original shorts.
2838 Also implies COMMON. */
2839 int short_compare = 0;
2841 /* Nonzero if this is a right-shift operation, which can be computed on the
2842 original short and then promoted if the operand is a promoted short. */
2843 int short_shift = 0;
2845 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2848 /* Apply default conversions. */
2849 op0 = default_conversion (orig_op0);
2850 op1 = default_conversion (orig_op1);
2852 type0 = TREE_TYPE (op0);
2853 type1 = TREE_TYPE (op1);
2855 /* The expression codes of the data types of the arguments tell us
2856 whether the arguments are integers, floating, pointers, etc. */
2857 code0 = TREE_CODE (type0);
2858 code1 = TREE_CODE (type1);
2860 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2861 STRIP_TYPE_NOPS (op0);
2862 STRIP_TYPE_NOPS (op1);
2864 /* If an error was already reported for one of the arguments,
2865 avoid reporting another error. */
2867 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2868 return error_mark_node;
2873 /* Handle the pointer + int case. */
2874 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2875 return pointer_int_sum (PLUS_EXPR, op0, op1);
2876 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2877 return pointer_int_sum (PLUS_EXPR, op1, op0);
2883 /* Subtraction of two similar pointers.
2884 We must subtract them as integers, then divide by object size. */
2885 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2886 && comp_target_types (type0, type1, 1))
2887 return pointer_diff (op0, op1);
2888 /* Handle pointer minus int. Just like pointer plus int. */
2889 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2890 return pointer_int_sum (MINUS_EXPR, op0, op1);
2899 case TRUNC_DIV_EXPR:
2901 case FLOOR_DIV_EXPR:
2902 case ROUND_DIV_EXPR:
2903 case EXACT_DIV_EXPR:
2904 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2905 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2907 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
2908 cp_warning ("division by zero in `%E / 0'", op0);
2909 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
2910 cp_warning ("division by zero in `%E / 0.'", op0);
2912 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2913 resultcode = RDIV_EXPR;
2915 /* When dividing two signed integers, we have to promote to int.
2916 unless we divide by a conatant != -1. Note that default
2917 conversion will have been performed on the operands at this
2918 point, so we have to dig out the original type to find out if
2920 shorten = ((TREE_CODE (op0) == NOP_EXPR
2921 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2922 || (TREE_CODE (op1) == INTEGER_CST
2923 && (TREE_INT_CST_LOW (op1) != -1
2924 || TREE_INT_CST_HIGH (op1) != -1)));
2930 case BIT_ANDTC_EXPR:
2933 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2935 /* If one operand is a constant, and the other is a short type
2936 that has been converted to an int,
2937 really do the work in the short type and then convert the
2938 result to int. If we are lucky, the constant will be 0 or 1
2939 in the short type, making the entire operation go away. */
2940 if (TREE_CODE (op0) == INTEGER_CST
2941 && TREE_CODE (op1) == NOP_EXPR
2942 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2943 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2945 final_type = result_type;
2946 op1 = TREE_OPERAND (op1, 0);
2947 result_type = TREE_TYPE (op1);
2949 if (TREE_CODE (op1) == INTEGER_CST
2950 && TREE_CODE (op0) == NOP_EXPR
2951 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2952 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2954 final_type = result_type;
2955 op0 = TREE_OPERAND (op0, 0);
2956 result_type = TREE_TYPE (op0);
2960 case TRUNC_MOD_EXPR:
2961 case FLOOR_MOD_EXPR:
2962 if (code1 == INTEGER_TYPE && integer_zerop (op1))
2963 cp_warning ("division by zero in `%E % 0'", op0);
2964 else if (code1 == REAL_TYPE && real_zerop (op1))
2965 cp_warning ("division by zero in `%E % 0.'", op0);
2967 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2969 /* Although it would be tempting to shorten always here, that loses
2970 on some targets, since the modulo instruction is undefined if the
2971 quotient can't be represented in the computation mode. We shorten
2972 only if unsigned or if dividing by something we know != -1. */
2973 shorten = ((TREE_CODE (op0) == NOP_EXPR
2974 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2975 || (TREE_CODE (op1) == INTEGER_CST
2976 && (TREE_INT_CST_LOW (op1) != -1
2977 || TREE_INT_CST_HIGH (op1) != -1)));
2982 case TRUTH_ANDIF_EXPR:
2983 case TRUTH_ORIF_EXPR:
2984 case TRUTH_AND_EXPR:
2986 result_type = boolean_type_node;
2989 /* Shift operations: result has same type as first operand;
2990 always convert second operand to int.
2991 Also set SHORT_SHIFT if shifting rightward. */
2994 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2996 result_type = type0;
2997 if (TREE_CODE (op1) == INTEGER_CST)
2999 if (tree_int_cst_lt (op1, integer_zero_node))
3000 warning ("right shift count is negative");
3003 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3005 if (TREE_INT_CST_HIGH (op1) != 0
3006 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3007 >= TYPE_PRECISION (type0)))
3008 warning ("right shift count >= width of type");
3011 /* Convert the shift-count to an integer, regardless of
3012 size of value being shifted. */
3013 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3014 op1 = convert (integer_type_node, op1);
3015 /* Avoid converting op1 to result_type later. */
3021 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3023 result_type = type0;
3024 if (TREE_CODE (op1) == INTEGER_CST)
3026 if (tree_int_cst_lt (op1, integer_zero_node))
3027 warning ("left shift count is negative");
3028 else if (TREE_INT_CST_HIGH (op1) != 0
3029 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3030 >= TYPE_PRECISION (type0)))
3031 warning ("left shift count >= width of type");
3033 /* Convert the shift-count to an integer, regardless of
3034 size of value being shifted. */
3035 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3036 op1 = convert (integer_type_node, op1);
3037 /* Avoid converting op1 to result_type later. */
3044 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3046 result_type = type0;
3047 if (TREE_CODE (op1) == INTEGER_CST)
3049 if (tree_int_cst_lt (op1, integer_zero_node))
3050 warning ("%s rotate count is negative",
3051 (code == LROTATE_EXPR) ? "left" : "right");
3052 else if (TREE_INT_CST_HIGH (op1) != 0
3053 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3054 >= TYPE_PRECISION (type0)))
3055 warning ("%s rotate count >= width of type",
3056 (code == LROTATE_EXPR) ? "left" : "right");
3058 /* Convert the shift-count to an integer, regardless of
3059 size of value being shifted. */
3060 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3061 op1 = convert (integer_type_node, op1);
3067 result_type = boolean_type_node;
3069 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3070 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3072 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3074 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3075 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3076 /* Anything compares with void *. void * compares with anything.
3077 Otherwise, the targets must be the same. */
3078 if (tt0 != tt1 && TREE_CODE (tt0) == RECORD_TYPE
3079 && TREE_CODE (tt1) == RECORD_TYPE)
3081 tree base = common_base_type (tt0, tt1);
3082 if (base == NULL_TREE)
3083 cp_pedwarn ("comparison of distinct object pointer types `%T' and `%T'", type0, type1);
3084 else if (base == error_mark_node)
3086 cp_error ("comparison of pointer types `%T' and `%T' requires conversion to ambiguous supertype", type0, type1);
3087 return error_mark_node;
3091 if (integer_zerop (op0))
3092 op0 = null_pointer_node;
3094 op0 = convert_pointer_to (base, op0);
3095 if (integer_zerop (op1))
3096 op1 = null_pointer_node;
3098 op1 = convert_pointer_to (base, op1);
3101 else if (comp_target_types (type0, type1, 1))
3103 else if (tt0 == void_type_node)
3105 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3106 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3107 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3108 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3109 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3111 else if (tt1 == void_type_node)
3113 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3114 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3115 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3117 else if ((TYPE_SIZE (tt0) != 0) != (TYPE_SIZE (tt1) != 0))
3118 cp_pedwarn ("comparison of %scomplete and %scomplete pointers `%T' and `%T'",
3119 TYPE_SIZE (tt0) == 0 ? "in" : "",
3120 TYPE_SIZE (tt1) == 0 ? "in" : "",
3123 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3126 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3127 && integer_zerop (op1))
3128 op1 = null_pointer_node;
3129 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3130 && integer_zerop (op0))
3131 op0 = null_pointer_node;
3132 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3134 error ("ANSI C++ forbids comparison between pointer and integer");
3135 op1 = convert (TREE_TYPE (op0), op1);
3137 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3139 error ("ANSI C++ forbids comparison between pointer and integer");
3140 op0 = convert (TREE_TYPE (op1), op0);
3142 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3143 && integer_zerop (op1))
3145 op0 = build_component_ref (op0, index_identifier, 0, 0);
3146 op1 = integer_zero_node;
3148 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3149 && integer_zerop (op0))
3151 op0 = build_component_ref (op1, index_identifier, 0, 0);
3152 op1 = integer_zero_node;
3154 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3155 && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3156 == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3158 /* The code we generate for the test is:
3160 (op0.index == op1.index
3161 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3162 || op0.pfn == op1.pfn)) */
3164 tree index0 = build_component_ref (op0, index_identifier, 0, 0);
3165 tree index1 = save_expr (build_component_ref (op1, index_identifier, 0, 0));
3166 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3167 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3168 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3169 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3171 tree integer_neg_one_node
3172 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3173 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3174 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3175 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3176 e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3177 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3178 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3179 if (code == EQ_EXPR)
3181 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3183 else if (TYPE_PTRMEMFUNC_P (type0)
3184 && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3186 tree index0 = build_component_ref (op0, index_identifier, 0, 0);
3188 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3189 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3190 tree delta21 = integer_zero_node;
3192 tree integer_neg_one_node
3193 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3194 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3195 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3197 /* Map everything down one to make room for the null pointer to member. */
3198 index1 = size_binop (PLUS_EXPR,
3199 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3201 op1 = integer_zero_node;
3202 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1)));
3203 delta21 = DECL_FIELD_BITPOS (delta21);
3204 delta21 = size_binop (FLOOR_DIV_EXPR, delta21, size_int (BITS_PER_UNIT));
3207 index1 = integer_neg_one_node;
3209 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0), op1);
3210 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3213 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3214 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3215 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3216 e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3217 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3218 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3219 if (code == EQ_EXPR)
3221 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3223 else if (TYPE_PTRMEMFUNC_P (type1)
3224 && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3226 return build_binary_op (code, op1, op0, 1);
3229 /* If args are not valid, clear out RESULT_TYPE
3230 to cause an error message later. */
3236 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3237 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3239 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3241 if (! comp_target_types (type0, type1, 1))
3242 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3244 else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
3245 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
3246 cp_pedwarn ("comparison of %scomplete and %scomplete pointers",
3247 TYPE_SIZE (TREE_TYPE (type0)) == 0 ? "in" : "",
3248 TYPE_SIZE (TREE_TYPE (type1)) == 0 ? "in" : "",
3251 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
3252 pedwarn ("ANSI C++ forbids ordered comparisons of pointers to functions");
3253 result_type = common_type (type0, type1);
3261 result_type = boolean_type_node;
3262 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3263 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3265 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3267 if (! comp_target_types (type0, type1, 1))
3268 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3270 else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
3271 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
3272 cp_pedwarn ("comparison of %scomplete and %scomplete pointers",
3273 TYPE_SIZE (TREE_TYPE (type0)) == 0 ? "in" : "",
3274 TYPE_SIZE (TREE_TYPE (type1)) == 0 ? "in" : "",
3277 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
3278 pedwarn ("ANSI C++ forbids ordered comparisons of pointers to functions");
3280 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3281 && integer_zerop (op1))
3282 op1 = null_pointer_node;
3283 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3284 && integer_zerop (op0))
3285 op0 = null_pointer_node;
3286 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3289 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3290 else if (! flag_traditional)
3291 warning ("comparison between pointer and integer");
3292 op1 = convert (TREE_TYPE (op0), op1);
3294 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3297 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3298 else if (! flag_traditional)
3299 warning ("comparison between pointer and integer");
3300 op0 = convert (TREE_TYPE (op1), op0);
3308 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3309 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3311 if (shorten || common || short_compare)
3312 result_type = common_type (type0, type1);
3314 /* For certain operations (which identify themselves by shorten != 0)
3315 if both args were extended from the same smaller type,
3316 do the arithmetic in that type and then extend.
3318 shorten !=0 and !=1 indicates a bitwise operation.
3319 For them, this optimization is safe only if
3320 both args are zero-extended or both are sign-extended.
3321 Otherwise, we might change the result.
3322 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3323 but calculated in (unsigned short) it would be (unsigned short)-1. */
3327 int unsigned0, unsigned1;
3328 tree arg0 = get_narrower (op0, &unsigned0);
3329 tree arg1 = get_narrower (op1, &unsigned1);
3330 /* UNS is 1 if the operation to be done is an unsigned one. */
3331 int uns = TREE_UNSIGNED (result_type);
3334 final_type = result_type;
3336 /* Handle the case that OP0 does not *contain* a conversion
3337 but it *requires* conversion to FINAL_TYPE. */
3339 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3340 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3341 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3342 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3344 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3346 /* For bitwise operations, signedness of nominal type
3347 does not matter. Consider only how operands were extended. */
3351 /* Note that in all three cases below we refrain from optimizing
3352 an unsigned operation on sign-extended args.
3353 That would not be valid. */
3355 /* Both args variable: if both extended in same way
3356 from same width, do it in that width.
3357 Do it unsigned if args were zero-extended. */
3358 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3359 < TYPE_PRECISION (result_type))
3360 && (TYPE_PRECISION (TREE_TYPE (arg1))
3361 == TYPE_PRECISION (TREE_TYPE (arg0)))
3362 && unsigned0 == unsigned1
3363 && (unsigned0 || !uns))
3365 = signed_or_unsigned_type (unsigned0,
3366 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3367 else if (TREE_CODE (arg0) == INTEGER_CST
3368 && (unsigned1 || !uns)
3369 && (TYPE_PRECISION (TREE_TYPE (arg1))
3370 < TYPE_PRECISION (result_type))
3371 && (type = signed_or_unsigned_type (unsigned1,
3373 int_fits_type_p (arg0, type)))
3375 else if (TREE_CODE (arg1) == INTEGER_CST
3376 && (unsigned0 || !uns)
3377 && (TYPE_PRECISION (TREE_TYPE (arg0))
3378 < TYPE_PRECISION (result_type))
3379 && (type = signed_or_unsigned_type (unsigned0,
3381 int_fits_type_p (arg1, type)))
3385 /* Shifts can be shortened if shifting right. */
3390 tree arg0 = get_narrower (op0, &unsigned_arg);
3392 final_type = result_type;
3394 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3395 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3397 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3398 /* We can shorten only if the shift count is less than the
3399 number of bits in the smaller type size. */
3400 && TREE_INT_CST_HIGH (op1) == 0
3401 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3402 /* If arg is sign-extended and then unsigned-shifted,
3403 we can simulate this with a signed shift in arg's type
3404 only if the extended result is at least twice as wide
3405 as the arg. Otherwise, the shift could use up all the
3406 ones made by sign-extension and bring in zeros.
3407 We can't optimize that case at all, but in most machines
3408 it never happens because available widths are 2**N. */
3409 && (!TREE_UNSIGNED (final_type)
3411 || ((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0))
3412 <= TYPE_PRECISION (result_type))))
3414 /* Do an unsigned shift if the operand was zero-extended. */
3416 = signed_or_unsigned_type (unsigned_arg,
3418 /* Convert value-to-be-shifted to that type. */
3419 if (TREE_TYPE (op0) != result_type)
3420 op0 = convert (result_type, op0);
3425 /* Comparison operations are shortened too but differently.
3426 They identify themselves by setting short_compare = 1. */
3430 /* Don't write &op0, etc., because that would prevent op0
3431 from being kept in a register.
3432 Instead, make copies of the our local variables and
3433 pass the copies by reference, then copy them back afterward. */
3434 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3435 enum tree_code xresultcode = resultcode;
3437 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3439 return convert (boolean_type_node, val);
3440 op0 = xop0, op1 = xop1, result_type = boolean_type_node;
3441 resultcode = xresultcode;
3444 if (short_compare && extra_warnings)
3446 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3447 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3449 tree comp_type = TREE_TYPE (op0);
3451 int unsignedp0, unsignedp1;
3452 tree primop0 = get_narrower (op0, &unsignedp0);
3453 tree primop1 = get_narrower (op1, &unsignedp1);
3455 /* Give warnings for comparisons between signed and unsigned
3456 quantities that may fail. Do not warn if the signed quantity
3457 is an unsuffixed integer literal (or some static constant
3458 expression involving such literals) and it is positive.
3459 Do not warn if the comparison is being done in a signed type,
3460 since the signed type will only be chosen if it can represent
3461 all the values of the unsigned type. */
3462 /* Do the checking based on the original operand trees, so that
3463 casts will be considered, but default promotions won't be. */
3464 if (TREE_UNSIGNED (comp_type)
3465 && ((op0_signed && (TREE_CODE (orig_op0) != INTEGER_CST
3466 || tree_int_cst_sgn (orig_op0) == -1))
3467 || (op1_signed && (TREE_CODE (orig_op1) != INTEGER_CST
3468 || tree_int_cst_sgn (orig_op1) == -1))))
3469 warning ("comparison between signed and unsigned");
3471 /* Warn if two unsigned values are being compared in a size
3472 larger than their original size, and one (and only one) is the
3473 result of a `~' operator. This comparison will always fail.
3475 Also warn if one operand is a constant, and the constant does not
3476 have all bits set that are set in the ~ operand when it is
3479 else if (TREE_CODE (primop0) == BIT_NOT_EXPR
3480 ^ TREE_CODE (primop1) == BIT_NOT_EXPR)
3482 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3483 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3484 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3485 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3487 if (TREE_CODE (primop0) == INTEGER_CST
3488 || TREE_CODE (primop1) == INTEGER_CST)
3491 HOST_WIDE_INT constant, mask;
3495 if (TREE_CODE (primop0) == INTEGER_CST)
3498 unsignedp = unsignedp1;
3499 constant = TREE_INT_CST_LOW (primop0);
3504 unsignedp = unsignedp0;
3505 constant = TREE_INT_CST_LOW (primop1);
3508 bits = TYPE_PRECISION (TREE_TYPE (primop));
3509 if (bits < TYPE_PRECISION (comp_type)
3510 && bits < HOST_BITS_PER_LONG && unsignedp)
3512 mask = (~ (HOST_WIDE_INT) 0) << bits;
3513 if ((mask & constant) != mask)
3514 warning ("comparison of promoted ~unsigned with constant");
3517 else if (unsignedp0 && unsignedp1
3518 && (TYPE_PRECISION (TREE_TYPE (primop0))
3519 < TYPE_PRECISION (comp_type))
3520 && (TYPE_PRECISION (TREE_TYPE (primop1))
3521 < TYPE_PRECISION (comp_type)))
3522 warning ("comparison of promoted ~unsigned with unsigned");
3527 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3528 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3529 Then the expression will be built.
3530 It will be given type FINAL_TYPE if that is nonzero;
3531 otherwise, it will be given type RESULT_TYPE. */
3535 binary_op_error (error_code);
3536 return error_mark_node;
3541 if (TREE_TYPE (op0) != result_type)
3542 op0 = convert (result_type, op0);
3543 if (TREE_TYPE (op1) != result_type)
3544 op1 = convert (result_type, op1);
3548 register tree result = build (resultcode, result_type, op0, op1);
3549 register tree folded;
3551 folded = fold (result);
3552 if (folded == result)
3553 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3554 if (final_type != 0)
3555 return convert (final_type, folded);
3560 /* Return a tree for the sum or difference (RESULTCODE says which)
3561 of pointer PTROP and integer INTOP. */
3564 pointer_int_sum (resultcode, ptrop, intop)
3565 enum tree_code resultcode;
3566 register tree ptrop, intop;
3570 register tree result;
3571 register tree folded = fold (intop);
3573 /* The result is a pointer of the same type that is being added. */
3575 register tree result_type = TREE_TYPE (ptrop);
3577 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3579 if (pedantic || warn_pointer_arith)
3580 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3581 size_exp = integer_one_node;
3583 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3585 if (pedantic || warn_pointer_arith)
3586 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3587 size_exp = integer_one_node;
3589 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3591 if (pedantic || warn_pointer_arith)
3592 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3593 size_exp = integer_one_node;
3595 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
3598 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3599 size_exp = integer_one_node;
3602 size_exp = size_in_bytes (TREE_TYPE (result_type));
3604 /* Needed to make OOPS V2R3 work. */
3606 if (TREE_CODE (intop) == INTEGER_CST
3607 && TREE_INT_CST_LOW (intop) == 0
3608 && TREE_INT_CST_HIGH (intop) == 0)
3611 /* If what we are about to multiply by the size of the elements
3612 contains a constant term, apply distributive law
3613 and multiply that constant term separately.
3614 This helps produce common subexpressions. */
3616 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3617 && ! TREE_CONSTANT (intop)
3618 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3619 && TREE_CONSTANT (size_exp))
3621 enum tree_code subcode = resultcode;
3622 if (TREE_CODE (intop) == MINUS_EXPR)
3623 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3624 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
3625 intop = TREE_OPERAND (intop, 0);
3628 /* Convert the integer argument to a type the same size as a pointer
3629 so the multiply won't overflow spuriously. */
3631 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
3632 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
3634 /* Replace the integer argument with a suitable product by the object size.
3635 Do this multiplication as signed, then convert to the appropriate
3636 pointer type (actually unsigned integral). */
3638 intop = convert (result_type,
3639 build_binary_op (MULT_EXPR, intop,
3640 convert (TREE_TYPE (intop), size_exp), 1));
3642 /* Create the sum or difference. */
3644 result = build (resultcode, result_type, ptrop, intop);
3646 folded = fold (result);
3647 if (folded == result)
3648 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
3652 /* Return a tree for the difference of pointers OP0 and OP1.
3653 The resulting tree has type int. */
3656 pointer_diff (op0, op1)
3657 register tree op0, op1;
3659 register tree result, folded;
3660 tree restype = ptrdiff_type_node;
3661 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3665 if (TREE_CODE (target_type) == VOID_TYPE)
3666 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3667 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3668 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3669 if (TREE_CODE (target_type) == METHOD_TYPE)
3670 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3671 if (TREE_CODE (target_type) == OFFSET_TYPE)
3672 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3675 /* First do the subtraction as integers;
3676 then drop through to build the divide operator. */
3678 op0 = build_binary_op (MINUS_EXPR,
3679 convert (restype, op0), convert (restype, op1), 1);
3681 /* This generates an error if op1 is a pointer to an incomplete type. */
3682 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
3683 error ("arithmetic on pointer to an incomplete type");
3685 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3686 || TREE_CODE (target_type) == FUNCTION_TYPE
3687 || TREE_CODE (target_type) == METHOD_TYPE
3688 || TREE_CODE (target_type) == OFFSET_TYPE)
3690 : size_in_bytes (target_type));
3692 /* Do the division. */
3694 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3696 folded = fold (result);
3697 if (folded == result)
3698 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3702 /* Handle the case of taking the address of a COMPONENT_REF.
3703 Called by `build_unary_op' and `build_up_reference'.
3705 ARG is the COMPONENT_REF whose address we want.
3706 ARGTYPE is the pointer type that this address should have.
3707 MSG is an error message to print if this COMPONENT_REF is not
3708 addressable (such as a bitfield). */
3711 build_component_addr (arg, argtype, msg)
3715 tree field = TREE_OPERAND (arg, 1);
3716 tree basetype = decl_type_context (field);
3717 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3719 if (DECL_BIT_FIELD (field))
3721 error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
3722 return error_mark_node;
3726 cp_warning ("address of `%T::%D' taken", basetype, field);
3728 if (TREE_CODE (field) == FIELD_DECL
3729 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
3731 /* Can't convert directly to ARGTYPE, since that
3732 may have the same pointer type as one of our
3734 rval = build1 (NOP_EXPR, argtype,
3735 convert_pointer_to (basetype, rval));
3736 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
3739 /* This conversion is harmless. */
3740 rval = convert_force (argtype, rval, 0);
3742 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3744 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3745 size_int (BITS_PER_UNIT));
3746 int flag = TREE_CONSTANT (rval);
3747 rval = fold (build (PLUS_EXPR, argtype,
3748 rval, convert (argtype, offset)));
3749 TREE_CONSTANT (rval) = flag;
3754 /* Construct and perhaps optimize a tree representation
3755 for a unary operation. CODE, a tree_code, specifies the operation
3756 and XARG is the operand. */
3759 build_x_unary_op (code, xarg)
3760 enum tree_code code;
3763 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3765 if (code == ADDR_EXPR
3766 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
3767 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
3768 || (TREE_CODE (xarg) == OFFSET_REF)))
3769 /* don't look for a function */;
3772 tree rval = build_opfncall (code, LOOKUP_SPECULATIVELY, xarg,
3773 NULL_TREE, NULL_TREE);
3775 return build_opfncall (code, LOOKUP_NORMAL, xarg,
3776 NULL_TREE, NULL_TREE);
3778 return build_unary_op (code, xarg, 0);
3781 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3784 condition_conversion (expr)
3787 tree t = convert (boolean_type_node, expr);
3788 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
3792 /* C++: Must handle pointers to members.
3794 Perhaps type instantiation should be extended to handle conversion
3795 from aggregates to types we don't yet know we want? (Or are those
3796 cases typically errors which should be reported?)
3798 NOCONVERT nonzero suppresses the default promotions
3799 (such as from short to int). */
3801 build_unary_op (code, xarg, noconvert)
3802 enum tree_code code;
3806 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3807 register tree arg = xarg;
3808 register tree argtype = 0;
3809 char *errstring = NULL;
3812 if (arg == error_mark_node)
3813 return error_mark_node;
3818 /* This is used for unary plus, because a CONVERT_EXPR
3819 is enough to prevent anybody from looking inside for
3820 associativity, but won't generate any code. */
3821 if (!(arg = build_expr_type_conversion
3822 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
3823 errstring = "wrong type argument to unary plus";
3827 arg = default_conversion (arg);
3828 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
3833 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
3834 errstring = "wrong type argument to unary minus";
3835 else if (!noconvert)
3836 arg = default_conversion (arg);
3840 if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM, arg, 1)))
3841 errstring = "wrong type argument to bit-complement";
3842 else if (!noconvert)
3843 arg = default_conversion (arg);
3847 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
3848 errstring = "wrong type argument to abs";
3849 else if (!noconvert)
3850 arg = default_conversion (arg);
3853 case TRUTH_NOT_EXPR:
3854 arg = convert (boolean_type_node, arg);
3855 val = invert_truthvalue (arg);
3856 if (arg != error_mark_node)
3858 errstring = "in argument to unary !";
3864 case PREINCREMENT_EXPR:
3865 case POSTINCREMENT_EXPR:
3866 case PREDECREMENT_EXPR:
3867 case POSTDECREMENT_EXPR:
3868 /* Handle complex lvalues (when permitted)
3869 by reduction to simpler cases. */
3871 val = unary_complex_lvalue (code, arg);
3875 /* Report invalid types. */
3877 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
3880 if (code == PREINCREMENT_EXPR)
3881 errstring ="no pre-increment operator for type";
3882 else if (code == POSTINCREMENT_EXPR)
3883 errstring ="no post-increment operator for type";
3884 else if (code == PREDECREMENT_EXPR)
3885 errstring ="no pre-decrement operator for type";
3887 errstring ="no post-decrement operator for type";
3891 /* Report something read-only. */
3893 if (TYPE_READONLY (TREE_TYPE (arg))
3894 || TREE_READONLY (arg))
3895 readonly_error (arg, ((code == PREINCREMENT_EXPR
3896 || code == POSTINCREMENT_EXPR)
3897 ? "increment" : "decrement"),
3902 tree result_type = TREE_TYPE (arg);
3904 arg = get_unwidened (arg, 0);
3905 argtype = TREE_TYPE (arg);
3907 /* ARM $5.2.5 last annotation says this should be forbidden. */
3908 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
3909 pedwarn ("ANSI C++ forbids %sing an enum",
3910 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3911 ? "increment" : "decrement");
3913 /* Compute the increment. */
3915 if (TREE_CODE (argtype) == POINTER_TYPE)
3917 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
3918 if (TYPE_SIZE (TREE_TYPE (argtype)) == 0)
3919 cp_error ("cannot %s a pointer to incomplete type `%T'",
3920 ((code == PREINCREMENT_EXPR
3921 || code == POSTINCREMENT_EXPR)
3922 ? "increment" : "decrement"), TREE_TYPE (argtype));
3923 else if (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
3924 || tmp == VOID_TYPE || tmp == OFFSET_TYPE)
3925 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
3926 ((code == PREINCREMENT_EXPR
3927 || code == POSTINCREMENT_EXPR)
3928 ? "increment" : "decrement"), argtype);
3929 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
3932 inc = integer_one_node;
3934 inc = convert (argtype, inc);
3936 /* Handle incrementing a cast-expression. */
3938 switch (TREE_CODE (arg))
3943 case FIX_TRUNC_EXPR:
3944 case FIX_FLOOR_EXPR:
3945 case FIX_ROUND_EXPR:
3948 tree incremented, modify, value;
3949 if (! lvalue_p (arg) && pedantic)
3950 pedwarn ("cast to non-reference type used as lvalue");
3951 arg = stabilize_reference (arg);
3952 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3955 value = save_expr (arg);
3956 incremented = build (((code == PREINCREMENT_EXPR
3957 || code == POSTINCREMENT_EXPR)
3958 ? PLUS_EXPR : MINUS_EXPR),
3959 argtype, value, inc);
3960 TREE_SIDE_EFFECTS (incremented) = 1;
3961 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3962 return build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3966 /* Complain about anything else that is not a true lvalue. */
3967 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3968 || code == POSTINCREMENT_EXPR)
3969 ? "increment" : "decrement")))
3970 return error_mark_node;
3972 /* Forbid using -- on `bool'. */
3973 if (TREE_TYPE (arg) == boolean_type_node)
3975 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
3977 cp_error ("invalid use of `--' on bool variable `%D'", arg);
3978 return error_mark_node;
3981 /* This will only work if someone can convince Kenner to accept
3982 my patch to expand_increment. (jason) */
3983 val = build (code, TREE_TYPE (arg), arg, inc);
3985 if (code == POSTINCREMENT_EXPR)
3987 arg = stabilize_reference (arg);
3988 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
3990 TREE_SIDE_EFFECTS (val) = 1;
3991 arg = save_expr (arg);
3992 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
3993 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
3996 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4001 val = build (code, TREE_TYPE (arg), arg, inc);
4003 TREE_SIDE_EFFECTS (val) = 1;
4004 return convert (result_type, val);
4008 /* Note that this operation never does default_conversion
4009 regardless of NOCONVERT. */
4011 argtype = TREE_TYPE (arg);
4012 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4014 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4015 TREE_REFERENCE_EXPR (arg) = 1;
4019 && TREE_CODE (arg) == FUNCTION_DECL
4021 && DECL_CONTEXT (arg) == NULL_TREE
4022 && IDENTIFIER_LENGTH (DECL_NAME (arg)) == 4
4023 && IDENTIFIER_POINTER (DECL_NAME (arg))[0] == 'm'
4024 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg)), "main"))
4026 pedwarn ("taking address of function `main'");
4028 /* Let &* cancel out to simplify resulting code. */
4029 if (TREE_CODE (arg) == INDIRECT_REF)
4031 /* We don't need to have `current_class_decl' wrapped in a
4032 NON_LVALUE_EXPR node. */
4034 return current_class_decl;
4036 /* Keep `default_conversion' from converting if
4037 ARG is of REFERENCE_TYPE. */
4038 arg = TREE_OPERAND (arg, 0);
4039 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4041 if (TREE_CODE (arg) == VAR_DECL && DECL_INITIAL (arg)
4042 && !TREE_SIDE_EFFECTS (DECL_INITIAL (arg)))
4043 arg = DECL_INITIAL (arg);
4044 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4045 TREE_REFERENCE_EXPR (arg) = 1;
4046 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4048 else if (lvalue_p (arg))
4049 /* Don't let this be an lvalue. */
4050 return non_lvalue (arg);
4054 /* For &x[y], return x+y */
4055 if (TREE_CODE (arg) == ARRAY_REF)
4057 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4058 return error_mark_node;
4059 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4060 TREE_OPERAND (arg, 1), 1);
4063 /* Uninstantiated types are all functions. Taking the
4064 address of a function is a no-op, so just return the
4067 if (TREE_CODE (arg) == IDENTIFIER_NODE
4068 && IDENTIFIER_OPNAME_P (arg))
4070 my_friendly_abort (117);
4071 /* We don't know the type yet, so just work around the problem.
4072 We know that this will resolve to an lvalue. */
4073 return build1 (ADDR_EXPR, unknown_type_node, arg);
4076 if (TREE_CODE (arg) == TREE_LIST)
4078 if (TREE_CODE (TREE_VALUE (arg)) == FUNCTION_DECL
4079 && DECL_CHAIN (TREE_VALUE (arg)) == NULL_TREE)
4080 /* Unique overloaded non-member function. */
4081 return build_unary_op (ADDR_EXPR, TREE_VALUE (arg), 0);
4082 if (TREE_CHAIN (arg) == NULL_TREE
4083 && TREE_CODE (TREE_VALUE (arg)) == TREE_LIST
4084 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg))) == NULL_TREE)
4085 /* Unique overloaded member function. */
4086 return build_unary_op (ADDR_EXPR, TREE_VALUE (TREE_VALUE (arg)),
4088 return build1 (ADDR_EXPR, unknown_type_node, arg);
4091 /* Handle complex lvalues (when permitted)
4092 by reduction to simpler cases. */
4093 val = unary_complex_lvalue (code, arg);
4097 switch (TREE_CODE (arg))
4102 case FIX_TRUNC_EXPR:
4103 case FIX_FLOOR_EXPR:
4104 case FIX_ROUND_EXPR:
4106 if (! lvalue_p (arg) && pedantic)
4107 pedwarn ("taking the address of a cast to non-reference type");
4110 /* Allow the address of a constructor if all the elements
4112 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
4114 /* Anything not already handled and not a true memory reference
4116 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4117 && TREE_CODE (argtype) != METHOD_TYPE
4118 && !lvalue_or_else (arg, "unary `&'"))
4119 return error_mark_node;
4121 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4122 /* If the lvalue is const or volatile,
4123 merge that into the type that the address will point to. */
4124 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
4125 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
4127 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4128 argtype = cp_build_type_variant (argtype,
4129 TREE_READONLY (arg),
4130 TREE_THIS_VOLATILE (arg));
4133 argtype = build_pointer_type (argtype);
4135 if (mark_addressable (arg) == 0)
4136 return error_mark_node;
4141 if (TREE_CODE (arg) == COMPONENT_REF)
4142 addr = build_component_addr (arg, argtype,
4143 "attempt to take address of bit-field structure member `%s'");
4145 addr = build1 (code, argtype, arg);
4147 /* Address of a static or external variable or
4148 function counts as a constant */
4150 TREE_CONSTANT (addr) = 1;
4158 argtype = TREE_TYPE (arg);
4159 return fold (build1 (code, argtype, arg));
4163 return error_mark_node;
4166 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4167 convert ARG with the same conversions in the same order
4168 and return the result. */
4171 convert_sequence (conversions, arg)
4175 switch (TREE_CODE (conversions))
4180 case FIX_TRUNC_EXPR:
4181 case FIX_FLOOR_EXPR:
4182 case FIX_ROUND_EXPR:
4184 return convert (TREE_TYPE (conversions),
4185 convert_sequence (TREE_OPERAND (conversions, 0),
4193 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4194 for certain kinds of expressions which are not really lvalues
4195 but which we can accept as lvalues.
4197 If ARG is not a kind of expression we can handle, return zero. */
4200 unary_complex_lvalue (code, arg)
4201 enum tree_code code;
4204 /* Handle (a, b) used as an "lvalue". */
4205 if (TREE_CODE (arg) == COMPOUND_EXPR)
4207 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4208 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4209 TREE_OPERAND (arg, 0), real_result);
4212 /* Handle (a ? b : c) used as an "lvalue". */
4213 if (TREE_CODE (arg) == COND_EXPR)
4214 return rationalize_conditional_expr (code, arg);
4216 if (TREE_CODE (arg) == MODIFY_EXPR
4217 || TREE_CODE (arg) == PREINCREMENT_EXPR
4218 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4219 return unary_complex_lvalue
4220 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4221 arg, TREE_OPERAND (arg, 0)));
4223 if (code != ADDR_EXPR)
4226 /* Handle (a = b) used as an "lvalue" for `&'. */
4227 if (TREE_CODE (arg) == MODIFY_EXPR
4228 || TREE_CODE (arg) == INIT_EXPR)
4230 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4231 return build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4234 if (TREE_CODE (arg) == WITH_CLEANUP_EXPR)
4236 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4237 real_result = build (WITH_CLEANUP_EXPR, TREE_TYPE (real_result),
4238 real_result, 0, TREE_OPERAND (arg, 2));
4242 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4243 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4244 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4246 /* The representation of something of type OFFSET_TYPE
4247 is really the representation of a pointer to it.
4248 Here give the representation its true type. */
4252 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4254 if (TREE_CODE (arg) != OFFSET_REF)
4257 t = TREE_OPERAND (arg, 1);
4259 if (TREE_CODE (t) == FUNCTION_DECL) /* Check all this code for right semantics. */
4260 return build_unary_op (ADDR_EXPR, t, 0);
4261 if (TREE_CODE (t) == VAR_DECL)
4262 return build_unary_op (ADDR_EXPR, t, 0);
4265 if (TREE_OPERAND (arg, 0)
4266 && (TREE_CODE (TREE_OPERAND (arg, 0)) != NOP_EXPR
4267 || TREE_OPERAND (TREE_OPERAND (arg, 0), 0) != error_mark_node))
4268 if (TREE_CODE (t) != FIELD_DECL)
4270 /* Don't know if this should return address to just
4271 _DECL, or actual address resolved in this expression. */
4272 sorry ("address of bound pointer-to-member expression");
4273 return error_mark_node;
4276 offset = get_delta_difference (DECL_FIELD_CONTEXT (t),
4277 TREE_TYPE (TREE_OPERAND (arg, 0)),
4279 offset = size_binop (PLUS_EXPR, offset,
4280 size_binop (EASY_DIV_EXPR,
4281 DECL_FIELD_BITPOS (t),
4282 size_int (BITS_PER_UNIT)));
4283 return convert (build_pointer_type (TREE_TYPE (arg)), offset);
4287 if (TREE_CODE (arg) == OFFSET_REF)
4289 tree left = TREE_OPERAND (arg, 0), left_addr;
4290 tree right_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 1), 0);
4293 if (current_class_decl)
4294 left_addr = current_class_decl;
4297 error ("no `this' for pointer to member");
4298 return error_mark_node;
4301 left_addr = build_unary_op (ADDR_EXPR, left, 0);
4303 return build (PLUS_EXPR, build_pointer_type (TREE_TYPE (arg)),
4304 build1 (NOP_EXPR, integer_type_node, left_addr),
4305 build1 (NOP_EXPR, integer_type_node, right_addr));
4308 /* We permit compiler to make function calls returning
4309 objects of aggregate type look like lvalues. */
4313 if (TREE_CODE (targ) == SAVE_EXPR)
4314 targ = TREE_OPERAND (targ, 0);
4316 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4318 if (TREE_CODE (arg) == SAVE_EXPR)
4321 targ = build_cplus_new (TREE_TYPE (arg), arg, 1);
4322 return build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (arg)), targ);
4325 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4326 return build (SAVE_EXPR, TYPE_POINTER_TO (TREE_TYPE (arg)),
4327 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4329 /* We shouldn't wrap WITH_CLEANUP_EXPRs inside of SAVE_EXPRs, but in case
4330 we do, here's how to handle it. */
4331 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == WITH_CLEANUP_EXPR)
4334 /* Not really a bug, but something to turn on when testing. */
4335 compiler_error ("WITH_CLEANUP_EXPR wrapped in SAVE_EXPR");
4337 return unary_complex_lvalue (ADDR_EXPR, targ);
4341 /* Don't let anything else be handled specially. */
4345 /* Mark EXP saying that we need to be able to take the
4346 address of it; it should not be allocated in a register.
4347 Value is 1 if successful.
4349 C++: we do not allow `current_class_decl' to be addressable. */
4352 mark_addressable (exp)
4355 register tree x = exp;
4357 if (TREE_ADDRESSABLE (x) == 1)
4361 switch (TREE_CODE (x))
4366 x = TREE_OPERAND (x, 0);
4370 if (x == current_class_decl)
4372 error ("address of `this' not available");
4373 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4374 put_var_into_stack (x);
4379 && TREE_READONLY (x)
4380 && DECL_RTL (x) != 0
4381 && ! decl_in_memory_p (x))
4383 /* We thought this would make a good constant variable,
4384 but we were wrong. */
4385 push_obstacks_nochange ();
4386 end_temporary_allocation ();
4388 TREE_ASM_WRITTEN (x) = 0;
4390 rest_of_decl_compilation (x, 0, IDENTIFIER_LOCAL_VALUE (x) == 0, 0);
4391 TREE_ADDRESSABLE (x) = 1;
4397 /* Caller should not be trying to mark initialized
4398 constant fields addressable. */
4399 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4400 || DECL_IN_AGGR_P (x) == 0
4402 || DECL_EXTERNAL (x), 314);
4406 /* For C++, we don't warn about taking the address of a register
4407 variable for CONST_DECLs; ARM p97 explicitly says it's okay. */
4408 put_var_into_stack (x);
4409 TREE_ADDRESSABLE (x) = 1;
4413 /* We have to test both conditions here. The first may
4414 be non-zero in the case of processing a default function.
4415 The second may be non-zero in the case of a template function. */
4416 x = DECL_MAIN_VARIANT (x);
4417 if ((DECL_INLINE (x) || DECL_PENDING_INLINE_INFO (x))
4418 && (DECL_CONTEXT (x) == NULL_TREE
4419 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (x))) != 't'
4420 || ! CLASSTYPE_INTERFACE_ONLY (DECL_CONTEXT (x))))
4422 mark_inline_for_output (x);
4423 if (x == current_function_decl)
4424 DECL_EXTERNAL (x) = 0;
4426 TREE_ADDRESSABLE (x) = 1;
4428 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4436 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4439 build_x_conditional_expr (ifexp, op1, op2)
4440 tree ifexp, op1, op2;
4442 tree rval = NULL_TREE;
4444 /* See comments in `build_x_binary_op'. */
4446 rval = build_opfncall (COND_EXPR, LOOKUP_SPECULATIVELY, ifexp, op1, op2);
4448 return build_opfncall (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4450 return build_conditional_expr (ifexp, op1, op2);
4454 build_conditional_expr (ifexp, op1, op2)
4455 tree ifexp, op1, op2;
4457 register tree type1;
4458 register tree type2;
4459 register enum tree_code code1;
4460 register enum tree_code code2;
4461 register tree result_type = NULL_TREE;
4462 tree orig_op1 = op1, orig_op2 = op2;
4464 /* If second operand is omitted, it is the same as the first one;
4465 make sure it is calculated only once. */
4469 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4470 ifexp = op1 = save_expr (ifexp);
4473 ifexp = truthvalue_conversion (ifexp);
4475 if (TREE_CODE (ifexp) == ERROR_MARK)
4476 return error_mark_node;
4478 op1 = require_instantiated_type (TREE_TYPE (op2), op1, error_mark_node);
4479 if (op1 == error_mark_node)
4480 return error_mark_node;
4481 op2 = require_instantiated_type (TREE_TYPE (op1), op2, error_mark_node);
4482 if (op2 == error_mark_node)
4483 return error_mark_node;
4485 /* C++: REFERENCE_TYPES must be dereferenced. */
4486 type1 = TREE_TYPE (op1);
4487 code1 = TREE_CODE (type1);
4488 type2 = TREE_TYPE (op2);
4489 code2 = TREE_CODE (type2);
4491 if (code1 == REFERENCE_TYPE)
4493 op1 = convert_from_reference (op1);
4494 type1 = TREE_TYPE (op1);
4495 code1 = TREE_CODE (type1);
4497 if (code2 == REFERENCE_TYPE)
4499 op2 = convert_from_reference (op2);
4500 type2 = TREE_TYPE (op2);
4501 code2 = TREE_CODE (type2);
4504 #if 1 /* Produces wrong result if within sizeof. Sorry. */
4505 /* Don't promote the operands separately if they promote
4506 the same way. Return the unpromoted type and let the combined
4507 value get promoted if necessary. */
4509 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
4510 && code2 != ARRAY_TYPE
4512 /* For C++, let the enumeral type come through. */
4513 && code2 != ENUMERAL_TYPE
4515 && code2 != FUNCTION_TYPE
4516 && code2 != METHOD_TYPE)
4520 if (TREE_CONSTANT (ifexp)
4521 && (TREE_CODE (ifexp) == INTEGER_CST
4522 || TREE_CODE (ifexp) == ADDR_EXPR))
4523 return (integer_zerop (ifexp) ? op2 : op1);
4525 if (TREE_CODE (op1) == CONST_DECL)
4526 op1 = DECL_INITIAL (op1);
4527 else if (TREE_READONLY_DECL_P (op1))
4528 op1 = decl_constant_value (op1);
4529 if (TREE_CODE (op2) == CONST_DECL)
4530 op2 = DECL_INITIAL (op2);
4531 else if (TREE_READONLY_DECL_P (op2))
4532 op2 = decl_constant_value (op2);
4534 type1 = cp_build_type_variant
4536 TREE_READONLY (op1) || TREE_READONLY (op2),
4537 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4538 /* ??? This is a kludge to deal with the fact that
4539 we don't sort out integers and enums properly, yet. */
4540 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
4541 if (TREE_TYPE (result) != type1)
4542 result = build1 (NOP_EXPR, type1, result);
4547 /* They don't match; promote them both and then try to reconcile them.
4548 But don't permit mismatching enum types. */
4549 if (code1 == ENUMERAL_TYPE)
4551 if (code2 == ENUMERAL_TYPE)
4553 message_2_types (error, "enumeral mismatch in conditional expression: `%s' vs `%s'", type1, type2);
4554 return error_mark_node;
4556 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2))
4557 warning ("enumeral and non-enumeral type in conditional expression");
4559 else if (extra_warnings
4560 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1))
4561 warning ("enumeral and non-enumeral type in conditional expression");
4563 if (code1 != VOID_TYPE)
4565 op1 = default_conversion (op1);
4566 type1 = TREE_TYPE (op1);
4567 code1 = TREE_CODE (type1);
4569 if (code2 != VOID_TYPE)
4571 op2 = default_conversion (op2);
4572 type2 = TREE_TYPE (op2);
4573 code2 = TREE_CODE (type2);
4576 /* Quickly detect the usual case where op1 and op2 have the same type
4578 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4581 result_type = type1;
4583 result_type = cp_build_type_variant
4585 TREE_READONLY (op1) || TREE_READONLY (op2),
4586 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4588 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
4589 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
4591 result_type = common_type (type1, type2);
4593 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4595 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
4596 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
4597 result_type = void_type_node;
4599 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4601 if (comp_target_types (type1, type2, 1))
4602 result_type = common_type (type1, type2);
4603 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
4604 && TREE_CODE (orig_op1) != NOP_EXPR)
4605 result_type = qualify_type (type2, type1);
4606 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
4607 && TREE_CODE (orig_op2) != NOP_EXPR)
4608 result_type = qualify_type (type1, type2);
4609 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
4611 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
4612 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4613 result_type = qualify_type (type1, type2);
4615 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
4617 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
4618 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4619 result_type = qualify_type (type2, type1);
4622 else if (comptypes (type2, type1, 0))
4623 result_type = type2;
4624 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
4625 && IS_AGGR_TYPE (TREE_TYPE (type2))
4626 && (result_type = common_base_type (TREE_TYPE (type1), TREE_TYPE (type2))))
4628 if (result_type == error_mark_node)
4630 cp_error ("common base type of types `%T' and `%T' is ambiguous",
4631 TREE_TYPE (type1), TREE_TYPE (type2));
4632 result_type = ptr_type_node;
4637 && result_type != TREE_TYPE (type1)
4638 && result_type != TREE_TYPE (type2))
4639 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
4640 type1, type2, result_type);
4642 result_type = TYPE_POINTER_TO (result_type);
4647 pedwarn ("pointer type mismatch in conditional expression");
4648 result_type = ptr_type_node;
4651 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4653 if (!integer_zerop (op2))
4654 pedwarn ("pointer/integer type mismatch in conditional expression");
4657 op2 = null_pointer_node;
4658 #if 0 /* Sez who? */
4659 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
4660 pedwarn ("ANSI C++ forbids conditional expr between 0 and function pointer");
4663 result_type = type1;
4665 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4667 if (!integer_zerop (op1))
4668 pedwarn ("pointer/integer type mismatch in conditional expression");
4671 op1 = null_pointer_node;
4672 #if 0 /* Sez who? */
4673 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
4674 pedwarn ("ANSI C++ forbids conditional expr between 0 and function pointer");
4677 result_type = type2;
4682 /* The match does not look good. If either is
4683 an aggregate value, try converting to a scalar type. */
4684 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
4686 message_2_types (error, "aggregate mismatch in conditional expression: `%s' vs `%s'", type1, type2);
4687 return error_mark_node;
4689 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
4691 tree tmp = build_type_conversion (CONVERT_EXPR, type2, op1, 0);
4692 if (tmp == NULL_TREE)
4694 cp_error ("aggregate type `%T' could not convert on lhs of `:'", type1);
4695 return error_mark_node;
4697 if (tmp == error_mark_node)
4698 error ("ambiguous pointer conversion");
4699 result_type = type2;
4702 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
4704 tree tmp = build_type_conversion (CONVERT_EXPR, type1, op2, 0);
4705 if (tmp == NULL_TREE)
4707 cp_error ("aggregate type `%T' could not convert on rhs of `:'", type2);
4708 return error_mark_node;
4710 if (tmp == error_mark_node)
4711 error ("ambiguous pointer conversion");
4712 result_type = type1;
4715 else if (flag_cond_mismatch)
4716 result_type = void_type_node;
4719 error ("type mismatch in conditional expression");
4720 return error_mark_node;
4724 if (result_type != TREE_TYPE (op1))
4725 op1 = convert_and_check (result_type, op1);
4726 if (result_type != TREE_TYPE (op2))
4727 op2 = convert_and_check (result_type, op2);
4730 /* XXX delete me, I've been here for years. */
4731 if (IS_AGGR_TYPE_CODE (code1))
4733 result_type = TREE_TYPE (op1);
4734 if (TREE_CONSTANT (ifexp))
4735 return (integer_zerop (ifexp) ? op2 : op1);
4737 if (TYPE_MODE (result_type) == BLKmode)
4739 register tree tempvar
4740 = build_decl (VAR_DECL, NULL_TREE, result_type);
4741 register tree xop1 = build_modify_expr (tempvar, NOP_EXPR, op1);
4742 register tree xop2 = build_modify_expr (tempvar, NOP_EXPR, op2);
4743 register tree result = fold (build (COND_EXPR, result_type,
4744 ifexp, xop1, xop2));
4746 layout_decl (tempvar, 0);
4747 /* No way to handle variable-sized objects here.
4748 I fear that the entire handling of BLKmode conditional exprs
4749 needs to be redone. */
4750 my_friendly_assert (TREE_CONSTANT (DECL_SIZE (tempvar)), 315);
4752 = assign_stack_local (DECL_MODE (tempvar),
4753 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
4754 + BITS_PER_UNIT - 1)
4758 TREE_SIDE_EFFECTS (result)
4759 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
4760 | TREE_SIDE_EFFECTS (op2);
4761 return build (COMPOUND_EXPR, result_type, result, tempvar);
4766 if (TREE_CONSTANT (ifexp))
4767 return integer_zerop (ifexp) ? op2 : op1;
4769 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
4772 /* Handle overloading of the ',' operator when needed. Otherwise,
4773 this function just builds an expression list. */
4775 build_x_compound_expr (list)
4778 tree rest = TREE_CHAIN (list);
4781 if (rest == NULL_TREE)
4782 return build_compound_expr (list);
4784 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
4785 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4787 return build_x_compound_expr (tree_cons (NULL_TREE, result, TREE_CHAIN (rest)));
4788 return build_compound_expr (tree_cons (NULL_TREE, TREE_VALUE (list),
4789 build_tree_list (NULL_TREE, build_x_compound_expr (rest))));
4792 /* Given a list of expressions, return a compound expression
4793 that performs them all and returns the value of the last of them. */
4796 build_compound_expr (list)
4801 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
4802 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
4804 if (TREE_CHAIN (list) == 0)
4806 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4807 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4808 if (TREE_CODE (list) == NOP_EXPR
4809 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
4810 list = TREE_OPERAND (list, 0);
4812 /* Convert arrays to pointers. */
4813 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
4814 return default_conversion (TREE_VALUE (list));
4816 return TREE_VALUE (list);
4819 rest = build_compound_expr (TREE_CHAIN (list));
4821 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
4823 /* the left-hand operand of a comma expression is like an expression
4824 statement: we should warn if it doesn't have any side-effects,
4825 unless it was explicitly cast to (void). */
4826 if ((extra_warnings || warn_unused)
4827 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
4828 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
4829 warning("left-hand operand of comma expression has no effect");
4831 /* When pedantic, a compound expression can be neither an lvalue
4832 nor an integer constant expression. */
4836 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4837 else if (warn_unused)
4838 warn_if_unused_value (TREE_VALUE(list));
4841 return build (COMPOUND_EXPR, TREE_TYPE (rest),
4842 break_out_cleanups (TREE_VALUE (list)), rest);
4852 return (TREE_CODE (t) == INTEGER_CST && integer_zerop (t));
4855 tree build_static_cast (type, expr)
4858 return build_c_cast (type, expr, 0);
4861 tree build_reinterpret_cast (type, expr)
4864 tree intype = TREE_TYPE (expr);
4866 if (TYPE_PTRMEMFUNC_P (type))
4867 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
4868 if (TYPE_PTRMEMFUNC_P (intype))
4869 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
4871 if (! POINTER_TYPE_P (type) && ! TREE_CODE (type) == INTEGER_TYPE)
4873 cp_error ("reinterpret_cast cannot convert to type `%T'", type);
4874 return error_mark_node;
4876 if (! POINTER_TYPE_P (intype) && ! TREE_CODE (intype) == INTEGER_TYPE)
4878 cp_error ("reinterpret_cast cannot convert from type `%T'", type);
4879 return error_mark_node;
4881 if (TREE_CODE (type) == INTEGER_TYPE && TREE_CODE (intype) != POINTER_TYPE)
4883 cp_error ("reinterpret_cast cannot convert non-pointer type `%T' to `%T'",
4885 return error_mark_node;
4887 if (TREE_CODE (intype) == INTEGER_TYPE && TREE_CODE (type) != POINTER_TYPE)
4889 cp_error ("reinterpret_cast cannot convert `%T' to non-pointer type `%T'",
4891 return error_mark_node;
4894 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (intype) == POINTER_TYPE)
4895 expr = convert (ptr_type_node, expr);
4897 return build_c_cast (type, expr, 0);
4900 tree build_const_cast (type, expr)
4903 tree intype = TREE_TYPE (expr);
4906 if (TYPE_PTRMEMFUNC_P (type))
4907 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
4908 if (TYPE_PTRMEMFUNC_P (intype))
4909 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
4911 if (! POINTER_TYPE_P (type))
4913 cp_error ("const_cast cannot convert to non-pointer type `%T'", type);
4914 return error_mark_node;
4916 if (TREE_CODE (type) == REFERENCE_TYPE && ! real_lvalue_p (expr))
4918 cp_error ("const_cast cannot convert rvalue to type `%T'", type);
4919 return error_mark_node;
4921 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (intype) != POINTER_TYPE)
4923 cp_error ("const_cast cannot convert non-pointer type `%T' to type `%T'",
4925 return error_mark_node;
4928 if (TREE_CODE (type) == REFERENCE_TYPE)
4930 t1 = TREE_TYPE (type);
4935 t1 = TREE_TYPE (type);
4936 t2 = TREE_TYPE (intype);
4938 for (; TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE;
4939 t1 = TREE_TYPE (t1), t2 = TREE_TYPE (t2))
4943 if (TREE_CODE (t1) == OFFSET_TYPE && TREE_CODE (t2) == OFFSET_TYPE)
4945 if (TYPE_OFFSET_BASETYPE (t1) != TYPE_OFFSET_BASETYPE (t2))
4947 cp_error ("const_cast cannot convert between pointers to members of different types `%T' and `%T'",
4948 TYPE_OFFSET_BASETYPE (t2), TYPE_OFFSET_BASETYPE (t1));
4949 return error_mark_node;
4951 t1 = TREE_TYPE (t1);
4952 t2 = TREE_TYPE (t2);
4955 if (TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4957 cp_error ("const_cast cannot convert unrelated type `%T' to `%T'",
4959 return error_mark_node;
4962 return build_c_cast (type, expr, 0);
4965 /* Build an expression representing a cast to type TYPE of expression EXPR.
4967 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
4968 when doing the cast. */
4971 build_c_cast (type, expr, allow_nonconverting)
4974 int allow_nonconverting;
4976 register tree value = expr;
4978 if (type == error_mark_node || expr == error_mark_node)
4979 return error_mark_node;
4981 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4982 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4983 if (TREE_CODE (type) != REFERENCE_TYPE
4984 && TREE_CODE (value) == NOP_EXPR
4985 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
4986 value = TREE_OPERAND (value, 0);
4988 if (TREE_TYPE (expr)
4989 && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
4990 && TREE_CODE (type) != OFFSET_TYPE)
4991 value = resolve_offset_ref (value);
4993 if (TREE_CODE (type) == ARRAY_TYPE)
4995 /* Allow casting from T1* to T2[] because Cfront allows it.
4996 NIHCL uses it. It is not valid ANSI C however, and hence, not
4998 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5001 pedwarn ("ANSI C++ forbids casting to an array type");
5002 type = build_pointer_type (TREE_TYPE (type));
5006 error ("ANSI C++ forbids casting to an array type");
5007 return error_mark_node;
5011 if (TREE_CODE (type) == FUNCTION_TYPE
5012 || TREE_CODE (type) == METHOD_TYPE)
5014 cp_error ("casting to function type `%T'", type);
5015 return error_mark_node;
5018 if (IS_SIGNATURE (type))
5020 error ("cast specifies signature type");
5021 return error_mark_node;
5024 /* If there's only one function in the overloaded space,
5026 if (TREE_CODE (value) == TREE_LIST
5027 && TREE_CHAIN (value) == NULL_TREE)
5028 value = TREE_VALUE (value);
5030 if (TREE_CODE (type) == VOID_TYPE)
5031 value = build1 (CONVERT_EXPR, type, value);
5032 else if (TREE_TYPE (value) == NULL_TREE
5033 || type_unknown_p (value))
5035 value = instantiate_type (type, value, 1);
5037 if (value == error_mark_node)
5038 return error_mark_node;
5045 /* Convert functions and arrays to pointers and
5046 convert references to their expanded types,
5047 but don't convert any other types. */
5048 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5049 || TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5050 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5051 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5052 value = default_conversion (value);
5053 otype = TREE_TYPE (value);
5055 /* Optionally warn about potentially worrisome casts. */
5058 && TREE_CODE (type) == POINTER_TYPE
5059 && TREE_CODE (otype) == POINTER_TYPE)
5061 /* For C++ we make these regular warnings, rather than
5062 softening them into pedwarns. */
5063 if (TYPE_VOLATILE (TREE_TYPE (otype))
5064 && ! TYPE_VOLATILE (TREE_TYPE (type)))
5065 warning ("cast discards `volatile' from pointer target type");
5066 if (TYPE_READONLY (TREE_TYPE (otype))
5067 && ! TYPE_READONLY (TREE_TYPE (type)))
5068 warning ("cast discards `const' from pointer target type");
5071 /* Warn about possible alignment problems. */
5072 if (STRICT_ALIGNMENT && warn_cast_align
5073 && TREE_CODE (type) == POINTER_TYPE
5074 && TREE_CODE (otype) == POINTER_TYPE
5075 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5076 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5077 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5078 warning ("cast increases required alignment of target type");
5081 if (TREE_CODE (type) == INTEGER_TYPE
5082 && TREE_CODE (otype) == POINTER_TYPE
5083 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5084 warning ("cast from pointer to integer of different size");
5086 if (TREE_CODE (type) == POINTER_TYPE
5087 && TREE_CODE (otype) == INTEGER_TYPE
5088 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5089 /* Don't warn about converting 0 to pointer,
5090 provided the 0 was explicit--not cast or made by folding. */
5091 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5092 warning ("cast to pointer from integer of different size");
5095 flag = allow_nonconverting ? CONV_NONCONVERTING : 0;
5097 if (TREE_CODE (type) == REFERENCE_TYPE)
5098 value = (convert_from_reference
5099 (convert_to_reference (type, value, CONV_OLD_CONVERT|flag,
5100 LOOKUP_COMPLAIN, NULL_TREE)));
5105 if (TREE_READONLY_DECL_P (value))
5106 value = decl_constant_value (value);
5109 value = convert_force (type, value, flag);
5111 /* Ignore any integer overflow caused by the cast. */
5112 if (TREE_CODE (value) == INTEGER_CST)
5114 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5115 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5120 /* Always produce some operator for an explicit cast,
5121 so we can tell (for -pedantic) that the cast is no lvalue.
5122 Also, pedantically, don't let (void *) (FOO *) 0 be a null
5123 pointer constant. */
5126 && TREE_CODE (value) == INTEGER_CST
5127 && TREE_CODE (expr) == INTEGER_CST
5128 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE))
5129 value = non_lvalue (value);
5135 /* Build an assignment expression of lvalue LHS from value RHS.
5137 In C++, if the left hand side of the assignment is a REFERENCE_TYPE,
5138 that reference becomes deferenced down to it base type. */
5140 /* Return a reference to the BASE_INDEX part of EXPR. TYPE is
5141 the type to which BASE_INDEX applies. */
5143 get_base_ref (type, base_index, expr)
5148 tree binfos = TYPE_BINFO_BASETYPES (type);
5149 tree base_binfo = TREE_VEC_ELT (binfos, base_index);
5152 if (TREE_CODE (expr) == ARRAY_REF
5153 || ! BINFO_OFFSET_ZEROP (base_binfo)
5154 || TREE_VIA_VIRTUAL (base_binfo)
5155 || TYPE_MODE (type) != TYPE_MODE (BINFO_TYPE (base_binfo)))
5157 tree addr = build_unary_op (ADDR_EXPR, expr, 0);
5158 ref = build_indirect_ref (convert_pointer_to (base_binfo, addr),
5163 ref = copy_node (expr);
5164 TREE_TYPE (ref) = BINFO_TYPE (base_binfo);
5169 /* Build an assignment expression of lvalue LHS from value RHS.
5170 MODIFYCODE is the code for a binary operator that we use
5171 to combine the old value of LHS with RHS to get the new value.
5172 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5174 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
5176 `build_modify_expr_1' implements recursive part of memberwise
5177 assignment operation. */
5179 build_modify_expr_1 (lhs, modifycode, rhs, basetype_path)
5181 enum tree_code modifycode;
5184 register tree result;
5186 tree lhstype = TREE_TYPE (lhs);
5187 tree olhstype = lhstype;
5189 /* Avoid duplicate error messages from operands that had errors. */
5190 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5191 return error_mark_node;
5193 /* If a binary op has been requested, combine the old LHS value with the RHS
5194 producing the value we should actually store into the LHS. */
5196 if (modifycode == INIT_EXPR)
5198 else if (modifycode == NOP_EXPR)
5200 /* must deal with overloading of `operator=' here. */
5201 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5202 lhstype = TREE_TYPE (lhstype);
5208 lhs = stabilize_reference (lhs);
5209 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5210 modifycode = NOP_EXPR;
5213 /* If storing into a structure or union member,
5214 it has probably been given type `int'.
5215 Compute the type that would go with
5216 the actual amount of storage the member occupies. */
5218 if (TREE_CODE (lhs) == COMPONENT_REF
5219 && (TREE_CODE (lhstype) == INTEGER_TYPE
5220 || TREE_CODE (lhstype) == REAL_TYPE
5221 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5222 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5224 /* C++: The semantics of C++ differ from those of C when an
5225 assignment of an aggregate is desired. Assignment in C++ is
5226 now defined as memberwise assignment of non-static members
5227 and base class objects. This rule applies recursively
5228 until a member of a built-in type is found.
5230 Also, we cannot do a bit-wise copy of aggregates which
5231 contain virtual function table pointers. Those
5232 pointer values must be preserved through the copy.
5233 However, this is handled in expand_expr, and not here.
5234 This is because much better code can be generated at
5235 that stage than this one. */
5236 if (TREE_CODE (lhstype) == RECORD_TYPE
5237 && TYPE_LANG_SPECIFIC (lhstype)
5238 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5243 /* Perform operation on object. */
5244 if (modifycode == INIT_EXPR && TYPE_HAS_INIT_REF (lhstype))
5246 result = build_method_call (lhs, constructor_name_full (lhstype),
5247 build_tree_list (NULL_TREE, rhs),
5248 basetype_path, LOOKUP_NORMAL);
5249 return build_indirect_ref (result, NULL_PTR);
5251 else if (modifycode == NOP_EXPR)
5253 /* `operator=' is not an inheritable operator; see 13.4.3. */
5254 if (TYPE_LANG_SPECIFIC (lhstype) && TYPE_HAS_ASSIGNMENT (lhstype))
5256 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5257 lhs, rhs, make_node (NOP_EXPR));
5258 if (result == NULL_TREE)
5259 return error_mark_node;
5264 if (TYPE_USES_VIRTUAL_BASECLASSES (lhstype)
5265 || (modifycode == NOP_EXPR && TYPE_GETS_ASSIGNMENT (lhstype))
5266 || (modifycode == INIT_EXPR && TYPE_GETS_INIT_REF (lhstype)))
5268 tree binfos = BINFO_BASETYPES (TYPE_BINFO (lhstype));
5271 if (binfos != NULL_TREE)
5272 /* Perform operation on each member, depth-first, left-right. */
5273 for (i = 0; i <= TREE_VEC_LENGTH (binfos)-1; i++)
5275 tree base_binfo = TREE_VEC_ELT (binfos, i);
5276 tree base_lhs, base_rhs;
5279 /* Assignments from virtual baseclasses handled elsewhere. */
5280 if (TREE_VIA_VIRTUAL (base_binfo))
5283 base_lhs = get_base_ref (lhstype, i, lhs);
5284 base_rhs = get_base_ref (lhstype, i, newrhs);
5286 BINFO_INHERITANCE_CHAIN (base_binfo) = basetype_path;
5288 = build_modify_expr_1 (base_lhs, modifycode, base_rhs,
5291 /* We either get back a compound stmt, or a simple one. */
5292 if (new_result && TREE_CODE (new_result) == TREE_LIST)
5293 new_result = build_compound_expr (new_result);
5294 result = tree_cons (NULL_TREE, new_result, result);
5297 for (elt = TYPE_FIELDS (lhstype); elt; elt = TREE_CHAIN (elt))
5299 tree vbases = NULL_TREE;
5300 tree elt_lhs, elt_rhs;
5302 if (TREE_CODE (elt) != FIELD_DECL)
5305 && (VFIELD_NAME_P (DECL_NAME (elt))
5306 || VBASE_NAME_P (DECL_NAME (elt))))
5309 if (TREE_READONLY (elt)
5310 || TREE_CODE (TREE_TYPE (elt)) == REFERENCE_TYPE)
5312 cp_error ("cannot generate default `%T::operator ='",
5314 if (TREE_CODE (TREE_TYPE (elt)) == REFERENCE_TYPE)
5315 cp_error_at ("because member `%#D' is a reference", elt);
5317 cp_error_at ("because member `%#D' is const", elt);
5319 return error_mark_node;
5322 if (IS_AGGR_TYPE (TREE_TYPE (elt))
5323 && TYPE_LANG_SPECIFIC (TREE_TYPE (elt)))
5324 vbases = CLASSTYPE_VBASECLASSES (TREE_TYPE (elt));
5326 elt_lhs = build (COMPONENT_REF, TREE_TYPE (elt), lhs, elt);
5327 elt_rhs = build (COMPONENT_REF, TREE_TYPE (elt), newrhs, elt);
5328 /* It is not always safe to go through `build_modify_expr_1'
5329 when performing element-wise copying. This is because
5330 an element may be of ARRAY_TYPE, which will not
5331 be properly copied as a naked element. */
5332 if (TREE_CODE (TREE_TYPE (elt)) == RECORD_TYPE
5333 && TYPE_LANG_SPECIFIC (TREE_TYPE (elt)))
5334 basetype_path = TYPE_BINFO (TREE_TYPE (elt));
5338 tree elt_lhs_addr = build_unary_op (ADDR_EXPR, elt_lhs, 0);
5339 tree elt_rhs_addr = build_unary_op (ADDR_EXPR, elt_rhs, 0);
5341 elt_lhs_addr = convert_pointer_to (vbases, elt_lhs_addr);
5342 elt_rhs_addr = convert_pointer_to (vbases, elt_rhs_addr);
5344 = tree_cons (NULL_TREE,
5346 (build_indirect_ref (elt_lhs_addr, NULL_PTR),
5348 build_indirect_ref (elt_rhs_addr, NULL_PTR),
5351 if (TREE_VALUE (result) == error_mark_node)
5352 return error_mark_node;
5353 vbases = TREE_CHAIN (vbases);
5355 elt_lhs = build_modify_expr_1 (elt_lhs, modifycode, elt_rhs,
5357 result = tree_cons (NULL_TREE, elt_lhs, result);
5361 return build_compound_expr (result);
5362 /* No fields to move. */
5363 return integer_zero_node;
5367 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5368 void_type_node, lhs, rhs);
5369 TREE_SIDE_EFFECTS (result) = 1;
5374 result = build_modify_expr (lhs, modifycode, newrhs);
5375 /* ARRAY_TYPEs cannot be converted to anything meaningful,
5376 and leaving it there screws up `build_compound_expr' when
5377 it tries to defaultly convert everything. */
5378 if (TREE_CODE (TREE_TYPE (result)) == ARRAY_TYPE)
5379 TREE_TYPE (result) = void_type_node;
5384 /* Taken from expr.c:
5385 Subroutine of expand_expr:
5386 record the non-copied parts (LIST) of an expr (LHS), and return a list
5387 which specifies the initial values of these parts. */
5390 init_noncopied_parts (lhs, list)
5397 for (tail = list; tail; tail = TREE_CHAIN (tail))
5398 if (TREE_CODE (TREE_VALUE (tail)) == TREE_LIST)
5399 parts = chainon (parts, init_noncopied_parts (lhs, TREE_VALUE (tail)));
5402 tree part = TREE_VALUE (tail);
5403 tree part_type = TREE_TYPE (part);
5404 tree to_be_initialized = build (COMPONENT_REF, part_type, lhs, part);
5405 parts = tree_cons (TREE_PURPOSE (tail), to_be_initialized, parts);
5410 /* Build an assignment expression of lvalue LHS from value RHS.
5411 MODIFYCODE is the code for a binary operator that we use
5412 to combine the old value of LHS with RHS to get the new value.
5413 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5415 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
5418 build_modify_expr (lhs, modifycode, rhs)
5420 enum tree_code modifycode;
5423 register tree result;
5425 tree lhstype = TREE_TYPE (lhs);
5426 tree olhstype = lhstype;
5429 /* Avoid duplicate error messages from operands that had errors. */
5430 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5431 return error_mark_node;
5433 /* Types that aren't fully specified cannot be used in assignments. */
5434 lhs = require_complete_type (lhs);
5436 /* Decide early if we are going to protect RHS from GC
5437 before assigning it to LHS. */
5438 if (type_needs_gc_entry (TREE_TYPE (rhs))
5439 && ! value_safe_from_gc (lhs, rhs))
5440 rhs = protect_value_from_gc (lhs, rhs);
5444 /* Handle assignment to signature pointers/refs. */
5446 if (TYPE_LANG_SPECIFIC (lhstype) &&
5447 (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5449 return build_signature_pointer_constructor (lhs, rhs);
5452 /* Handle control structure constructs used as "lvalues". */
5454 switch (TREE_CODE (lhs))
5456 /* Handle --foo = 5; as these are valid constructs in C++ */
5457 case PREDECREMENT_EXPR:
5458 case PREINCREMENT_EXPR:
5459 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5460 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5461 stabilize_reference (TREE_OPERAND (lhs, 0)));
5462 return build (COMPOUND_EXPR, lhstype,
5464 build_modify_expr (TREE_OPERAND (lhs, 0),
5467 /* Handle (a, b) used as an "lvalue". */
5469 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5471 if (TREE_CODE (newrhs) == ERROR_MARK)
5472 return error_mark_node;
5473 return build (COMPOUND_EXPR, lhstype,
5474 TREE_OPERAND (lhs, 0), newrhs);
5477 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5478 if (TREE_CODE (newrhs) == ERROR_MARK)
5479 return error_mark_node;
5480 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5482 /* Handle (a ? b : c) used as an "lvalue". */
5484 rhs = save_expr (rhs);
5486 /* Produce (a ? (b = rhs) : (c = rhs))
5487 except that the RHS goes through a save-expr
5488 so the code to compute it is only emitted once. */
5490 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5491 build_modify_expr (convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5493 build_modify_expr (convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5495 if (TREE_CODE (cond) == ERROR_MARK)
5497 /* Make sure the code to compute the rhs comes out
5498 before the split. */
5499 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5500 /* Case to void to suppress warning
5501 from warn_if_unused_value. */
5502 convert (void_type_node, rhs), cond);
5506 if (TREE_CODE (lhs) == OFFSET_REF)
5508 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5510 /* Static class member? */
5511 tree member = TREE_OPERAND (lhs, 1);
5512 if (TREE_CODE (member) == VAR_DECL)
5516 compiler_error ("invalid static class member");
5517 return error_mark_node;
5521 lhs = resolve_offset_ref (lhs);
5523 olhstype = lhstype = TREE_TYPE (lhs);
5526 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5527 && modifycode != INIT_EXPR)
5529 lhs = convert_from_reference (lhs);
5530 olhstype = lhstype = TREE_TYPE (lhs);
5533 /* If a binary op has been requested, combine the old LHS value with the RHS
5534 producing the value we should actually store into the LHS. */
5536 if (modifycode == INIT_EXPR)
5538 if (! IS_AGGR_TYPE (lhstype))
5539 /* Do the default thing */;
5540 else if (! TYPE_HAS_CONSTRUCTOR (lhstype))
5541 cp_error ("`%T' has no constructors", lhstype);
5542 else if (! TYPE_NEEDS_CONSTRUCTING (lhstype)
5543 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5544 /* Do the default thing */;
5547 result = build_method_call (lhs, constructor_name_full (lhstype),
5548 build_tree_list (NULL_TREE, rhs),
5549 NULL_TREE, LOOKUP_NORMAL);
5550 if (result == NULL_TREE)
5551 return error_mark_node;
5555 else if (modifycode == NOP_EXPR)
5558 /* `operator=' is not an inheritable operator. */
5559 if (! IS_AGGR_TYPE (lhstype))
5560 /* Do the default thing */;
5561 else if (! TYPE_HAS_ASSIGNMENT (lhstype))
5562 cp_error ("`%T' does not define operator=", lhstype);
5563 else if (! TYPE_HAS_REAL_ASSIGNMENT (lhstype)
5564 && ! TYPE_HAS_COMPLEX_ASSIGN_REF (lhstype)
5565 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5566 /* Do the default thing */;
5569 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5570 lhs, rhs, make_node (NOP_EXPR));
5571 if (result == NULL_TREE)
5572 return error_mark_node;
5576 /* Treat `operator=' as an inheritable operator. */
5577 if (TYPE_LANG_SPECIFIC (lhstype) && TYPE_GETS_ASSIGNMENT (lhstype))
5579 tree orig_lhstype = lhstype;
5580 while (! TYPE_HAS_ASSIGNMENT (lhstype))
5582 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (lhstype);
5583 tree basetype = NULL_TREE;
5584 for (i = 0; i < n_baseclasses; i++)
5585 if (TYPE_GETS_ASSIGNMENT (TYPE_BINFO_BASETYPE (lhstype, i)))
5587 if (basetype != NULL_TREE)
5589 message_2_types (error, "base classes `%s' and `%s' both have operator ='",
5591 TYPE_BINFO_BASETYPE (lhstype, i));
5592 return error_mark_node;
5594 basetype = TYPE_BINFO_BASETYPE (lhstype, i);
5598 if (orig_lhstype != lhstype)
5600 lhs = build_indirect_ref (convert_pointer_to (lhstype,
5601 build_unary_op (ADDR_EXPR, lhs, 0)), NULL_PTR);
5602 if (lhs == error_mark_node)
5604 cp_error ("conversion to private basetype `%T'", lhstype);
5605 return error_mark_node;
5608 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5609 lhs, rhs, make_node (NOP_EXPR));
5610 if (result == NULL_TREE)
5611 return error_mark_node;
5617 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5619 /* This case must convert to some sort of lvalue that
5620 can participate in an op= operation. */
5623 if (build_default_binary_type_conversion (modifycode, &lhs_tmp, &rhs_tmp))
5625 lhs = stabilize_reference (lhs_tmp);
5626 /* Forget is was ever anything else. */
5627 olhstype = lhstype = TREE_TYPE (lhs);
5628 newrhs = build_binary_op (modifycode, lhs, rhs_tmp, 1);
5632 cp_error ("no match for `%O(%#T, %#T)'", modifycode,
5633 TREE_TYPE (lhs), TREE_TYPE (rhs));
5634 return error_mark_node;
5639 lhs = stabilize_reference (lhs);
5640 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5643 /* Handle a cast used as an "lvalue".
5644 We have already performed any binary operator using the value as cast.
5645 Now convert the result to the cast type of the lhs,
5646 and then true type of the lhs and store it there;
5647 then convert result back to the cast type to be the value
5648 of the assignment. */
5650 switch (TREE_CODE (lhs))
5655 case FIX_TRUNC_EXPR:
5656 case FIX_FLOOR_EXPR:
5657 case FIX_ROUND_EXPR:
5659 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5660 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5661 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5662 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5663 newrhs = default_conversion (newrhs);
5665 tree inner_lhs = TREE_OPERAND (lhs, 0);
5667 if (! lvalue_p (lhs) && pedantic)
5668 pedwarn ("cast to non-reference type used as lvalue");
5670 result = build_modify_expr (inner_lhs, NOP_EXPR,
5671 convert (TREE_TYPE (inner_lhs),
5672 convert (lhstype, newrhs)));
5673 if (TREE_CODE (result) == ERROR_MARK)
5675 return convert (TREE_TYPE (lhs), result);
5679 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5680 Reject anything strange now. */
5682 if (!lvalue_or_else (lhs, "assignment"))
5683 return error_mark_node;
5685 GNU_xref_assign (lhs);
5687 /* Warn about storing in something that is `const'. */
5688 /* For C++, don't warn if this is initialization. */
5689 if (modifycode != INIT_EXPR
5690 /* For assignment to `const' signature pointer/reference fields,
5691 don't warn either, we already printed a better message before. */
5692 && ! (TREE_CODE (lhs) == COMPONENT_REF
5693 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5694 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5695 && (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
5696 || ((TREE_CODE (lhstype) == RECORD_TYPE
5697 || TREE_CODE (lhstype) == UNION_TYPE)
5698 && C_TYPE_FIELDS_READONLY (lhstype))
5699 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5700 && TYPE_READONLY (TREE_TYPE (lhstype)))))
5701 readonly_error (lhs, "assignment", 0);
5703 /* If storing into a structure or union member,
5704 it has probably been given type `int'.
5705 Compute the type that would go with
5706 the actual amount of storage the member occupies. */
5708 if (TREE_CODE (lhs) == COMPONENT_REF
5709 && (TREE_CODE (lhstype) == INTEGER_TYPE
5710 || TREE_CODE (lhstype) == REAL_TYPE
5711 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5713 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5715 /* If storing in a field that is in actuality a short or narrower
5716 than one, we must store in the field in its actual type. */
5718 if (lhstype != TREE_TYPE (lhs))
5720 lhs = copy_node (lhs);
5721 TREE_TYPE (lhs) = lhstype;
5725 /* check to see if there is an assignment to `this' */
5726 if (lhs == current_class_decl)
5728 if (flag_this_is_variable > 0
5729 && DECL_NAME (current_function_decl) != NULL_TREE
5730 && (DECL_NAME (current_function_decl)
5731 != constructor_name (current_class_type)))
5732 warning ("assignment to `this' not in constructor or destructor");
5733 current_function_just_assigned_this = 1;
5736 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5737 when the type of RHS is not yet known, i.e. its type
5738 is inherited from LHS. */
5739 rhs = require_instantiated_type (lhstype, newrhs, error_mark_node);
5740 if (rhs == error_mark_node)
5741 return error_mark_node;
5744 if (modifycode != INIT_EXPR)
5746 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5747 modifycode = NOP_EXPR;
5748 /* Reference-bashing */
5749 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5751 tree tmp = convert_from_reference (lhs);
5752 lhstype = TREE_TYPE (tmp);
5753 if (TYPE_SIZE (lhstype) == 0)
5755 incomplete_type_error (lhs, lhstype);
5756 return error_mark_node;
5761 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5763 tree tmp = convert_from_reference (newrhs);
5764 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5766 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5767 return error_mark_node;
5773 if (TREE_SIDE_EFFECTS (lhs))
5774 lhs = stabilize_reference (lhs);
5775 if (TREE_SIDE_EFFECTS (newrhs))
5776 newrhs = stabilize_reference (newrhs);
5779 /* This is now done by generating X(X&) and operator=(X&). */
5780 /* C++: The semantics of C++ differ from those of C when an
5781 assignment of an aggregate is desired. Assignment in C++ is
5782 now defined as memberwise assignment of non-static members
5783 and base class objects. This rule applies recursively
5784 until a member of a built-in type is found.
5786 Also, we cannot do a bit-wise copy of aggregates which
5787 contain virtual function table pointers. Those
5788 pointer values must be preserved through the copy.
5789 However, this is handled in expand_expr, and not here.
5790 This is because much better code can be generated at
5791 that stage than this one. */
5792 if (TREE_CODE (lhstype) == RECORD_TYPE
5793 && ! TYPE_PTRMEMFUNC_P (lhstype)
5794 && (TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))
5795 || (TREE_CODE (TREE_TYPE (newrhs)) == RECORD_TYPE
5796 && UNIQUELY_DERIVED_FROM_P (lhstype, TREE_TYPE (newrhs)))))
5798 tree vbases = CLASSTYPE_VBASECLASSES (lhstype);
5799 tree lhs_addr = build_unary_op (ADDR_EXPR, lhs, 0);
5802 /* Memberwise assignment would cause NEWRHS to be
5803 evaluated for every member that gets assigned.
5804 By wrapping side-effecting exprs in a SAVE_EXPR,
5805 NEWRHS will only be evaluated once. */
5806 if (IS_AGGR_TYPE (TREE_TYPE (newrhs))
5807 && TREE_SIDE_EFFECTS (newrhs)
5808 /* This are things we don't have to save. */
5809 && TREE_CODE (newrhs) != COND_EXPR
5810 && TREE_CODE (newrhs) != TARGET_EXPR
5811 && TREE_CODE (newrhs) != WITH_CLEANUP_EXPR)
5812 /* Call `break_out_cleanups' on NEWRHS in case there are cleanups.
5813 If NEWRHS is a CALL_EXPR that needs a cleanup, failure to do so
5814 will result in expand_expr expanding the call without knowing
5815 that it should run the cleanup. */
5816 newrhs = save_expr (break_out_cleanups (newrhs));
5818 if (TREE_CODE (newrhs) == COND_EXPR)
5819 rhs_addr = rationalize_conditional_expr (ADDR_EXPR, newrhs);
5821 rhs_addr = build_unary_op (ADDR_EXPR, newrhs, 0);
5823 result = tree_cons (NULL_TREE,
5824 convert (build_reference_type (lhstype), lhs),
5827 if (! comptypes (TREE_TYPE (lhs_addr), TREE_TYPE (rhs_addr), 1))
5828 rhs_addr = convert_pointer_to (TREE_TYPE (TREE_TYPE (lhs_addr)), rhs_addr);
5830 tree noncopied_parts = NULL_TREE;
5832 if (TYPE_NONCOPIED_PARTS (lhstype) != 0)
5833 noncopied_parts = init_noncopied_parts (lhs,
5834 TYPE_NONCOPIED_PARTS (lhstype));
5835 while (noncopied_parts != 0)
5837 result = tree_cons (NULL_TREE,
5838 build_modify_expr (convert (ptr_type_node, TREE_VALUE (noncopied_parts)),
5840 TREE_PURPOSE (noncopied_parts)),
5842 noncopied_parts = TREE_CHAIN (noncopied_parts);
5845 /* Once we have our hands on an address, we must change NEWRHS
5846 to work from there. Otherwise we can get multiple evaluations
5848 if (TREE_CODE (newrhs) != SAVE_EXPR)
5849 newrhs = build_indirect_ref (rhs_addr, NULL_PTR);
5853 tree elt_lhs = convert_pointer_to (vbases, lhs_addr);
5854 tree elt_rhs = convert_pointer_to (vbases, rhs_addr);
5856 = tree_cons (NULL_TREE,
5857 build_modify_expr_1 (build_indirect_ref (elt_lhs, NULL_PTR),
5859 build_indirect_ref (elt_rhs, NULL_PTR),
5860 TYPE_BINFO (lhstype)),
5862 if (TREE_VALUE (result) == error_mark_node)
5863 return error_mark_node;
5864 vbases = TREE_CHAIN (vbases);
5866 result = tree_cons (NULL_TREE,
5867 build_modify_expr_1 (lhs,
5870 TYPE_BINFO (lhstype)),
5872 return build_compound_expr (result);
5876 /* Convert new value to destination type. */
5878 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5882 /* Allow array assignment in compiler-generated code. */
5883 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5884 pedwarn ("ANSI C++ forbids assignment of arrays");
5886 /* Have to wrap this in RTL_EXPR for two cases:
5887 in base or member initialization and if we
5888 are a branch of a ?: operator. Since we
5889 can't easily know the latter, just do it always. */
5891 result = make_node (RTL_EXPR);
5893 TREE_TYPE (result) = void_type_node;
5894 do_pending_stack_adjust ();
5895 start_sequence_for_rtl_expr (result);
5897 /* As a matter of principle, `start_sequence' should do this. */
5900 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5901 ? 1 + (modifycode != INIT_EXPR): 0;
5902 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
5905 do_pending_stack_adjust ();
5907 TREE_SIDE_EFFECTS (result) = 1;
5908 RTL_EXPR_SEQUENCE (result) = get_insns ();
5909 RTL_EXPR_RTL (result) = const0_rtx;
5914 if (modifycode == INIT_EXPR)
5916 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5917 "assignment", NULL_TREE, 0);
5918 if (lhs == DECL_RESULT (current_function_decl))
5920 if (DECL_INITIAL (lhs))
5921 warning ("return value from function receives multiple initializations");
5922 DECL_INITIAL (lhs) = newrhs;
5928 if (IS_AGGR_TYPE (lhstype))
5930 if (result = build_opfncall (MODIFY_EXPR,
5931 LOOKUP_NORMAL, lhs, newrhs,
5932 make_node (NOP_EXPR)))
5936 /* Avoid warnings on enum bit fields. */
5937 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5938 && TREE_CODE (lhstype) == INTEGER_TYPE)
5940 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5942 newrhs = convert_force (lhstype, newrhs, 0);
5945 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5947 if (TREE_CODE (newrhs) == CALL_EXPR
5948 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5949 newrhs = build_cplus_new (lhstype, newrhs, 0);
5951 if (TREE_CODE (newrhs) == TARGET_EXPR)
5953 /* Can't initialize directly from a TARGET_EXPR, since that would
5954 cause the lhs to be constructed twice. So we force the
5955 TARGET_EXPR to be expanded. expand_expr should really do this
5958 tree xval = make_node (RTL_EXPR);
5961 do_pending_stack_adjust ();
5962 start_sequence_for_rtl_expr (xval);
5964 rtxval = expand_expr (newrhs, NULL, VOIDmode, 0);
5965 do_pending_stack_adjust ();
5966 TREE_SIDE_EFFECTS (xval) = 1;
5967 RTL_EXPR_SEQUENCE (xval) = get_insns ();
5969 RTL_EXPR_RTL (xval) = rtxval;
5970 TREE_TYPE (xval) = lhstype;
5975 if (TREE_CODE (newrhs) == ERROR_MARK)
5976 return error_mark_node;
5978 if (TREE_CODE (newrhs) == COND_EXPR)
5981 tree cond = TREE_OPERAND (newrhs, 0);
5983 if (TREE_SIDE_EFFECTS (lhs))
5984 cond = build_compound_expr (tree_cons
5986 build_tree_list (NULL_TREE, cond)));
5988 /* Cannot have two identical lhs on this one tree (result) as preexpand
5989 calls will rip them out and fill in RTL for them, but when the
5990 rtl is generated, the calls will only be in the first side of the
5991 condition, not on both, or before the conditional jump! (mrs) */
5992 lhs1 = break_out_calls (lhs);
5995 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5996 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5997 lhstype, lhs, newrhs);
6000 tree result_type = TREE_TYPE (newrhs);
6001 /* We have to convert each arm to the proper type because the
6002 types may have been munged by constant folding. */
6004 = build (COND_EXPR, result_type, cond,
6005 build_modify_expr (lhs, modifycode,
6006 convert (result_type,
6007 TREE_OPERAND (newrhs, 1))),
6008 build_modify_expr (lhs1, modifycode,
6009 convert (result_type,
6010 TREE_OPERAND (newrhs, 2))));
6013 else if (modifycode != INIT_EXPR && TREE_CODE (newrhs) == WITH_CLEANUP_EXPR)
6015 tree cleanup = TREE_OPERAND (newrhs, 2);
6018 /* Finish up by running cleanups and having the "value" of the lhs. */
6019 tree exprlist = tree_cons (NULL_TREE, cleanup,
6020 build_tree_list (NULL_TREE, lhs));
6021 newrhs = TREE_OPERAND (newrhs, 0);
6022 if (TREE_CODE (newrhs) == TARGET_EXPR)
6023 slot = TREE_OPERAND (newrhs, 0);
6024 else if (TREE_CODE (newrhs) == ADDR_EXPR)
6026 /* Bad but valid. */
6028 warning ("address taken of temporary object");
6031 my_friendly_abort (118);
6033 /* Copy the value computed in SLOT into LHS. */
6034 exprlist = tree_cons (NULL_TREE,
6035 build_modify_expr (lhs, modifycode, slot),
6037 /* Evaluate the expression that needs CLEANUP. This will
6038 compute the value into SLOT. */
6039 exprlist = tree_cons (NULL_TREE, newrhs, exprlist);
6040 result = convert (lhstype, build_compound_expr (exprlist));
6043 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6044 lhstype, lhs, newrhs);
6045 TREE_SIDE_EFFECTS (result) = 1;
6047 /* If we got the LHS in a different type for storing in,
6048 convert the result back to the nominal type of LHS
6049 so that the value we return always has the same type
6050 as the LHS argument. */
6052 if (olhstype == TREE_TYPE (result))
6054 /* Avoid warnings converting integral types back into enums
6055 for enum bit fields. */
6056 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6057 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6059 result = build (COMPOUND_EXPR, olhstype, result, olhs);
6060 TREE_NO_UNUSED_WARNING (result) = 1;
6063 return convert_for_assignment (olhstype, result, "assignment",
6068 /* Return 0 if EXP is not a valid lvalue in this language
6069 even though `lvalue_or_else' would accept it. */
6072 language_lvalue_valid (exp)
6078 /* Get differnce in deltas for different pointer to member function
6079 types. Return inetger_zero_node, if FROM cannot be converted to a
6080 TO type. If FORCE is true, then allow reverse conversions as well. */
6082 get_delta_difference (from, to, force)
6086 tree delta = integer_zero_node;
6092 /* Should get_base_distance here, so we can check if any thing along the
6093 path is virtual, and we need to make sure we stay
6094 inside the real binfos when going through virtual bases.
6095 Maybe we should replace virtual bases with
6096 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6097 binfo = get_binfo (from, to, 1);
6098 if (binfo == error_mark_node)
6100 error (" in pointer to member function conversion");
6107 error_not_base_type (from, to);
6108 error (" in pointer to member function conversion");
6111 binfo = get_binfo (to, from, 1);
6112 if (binfo == error_mark_node)
6114 error (" in pointer to member function conversion");
6119 error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from, to);
6122 if (TREE_VIA_VIRTUAL (binfo))
6124 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
6126 return build_binary_op (MINUS_EXPR,
6128 BINFO_OFFSET (binfo), 1);
6130 if (TREE_VIA_VIRTUAL (binfo))
6132 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
6134 return BINFO_OFFSET (binfo);
6137 /* Build a constructor for a pointer to member function. It can be
6138 used to initialize global variables, local variable, or used
6139 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6142 If FORCE is non-zero, then force this conversion, even if
6143 we would rather not do it. Usually set when using an explicit
6146 Return error_mark_node, if something goes wrong. */
6149 build_ptrmemfunc (type, pfn, force)
6153 tree index = integer_zero_node;
6154 tree delta = integer_zero_node;
6155 tree delta2 = integer_zero_node;
6160 /* Handle multiple conversions of pointer to member fucntions. */
6161 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6163 tree ndelta, ndelta2, nindex;
6164 /* Is is already the right type? */
6166 /* Sorry, can't do this, the backend is too stupid. */
6167 if (TYPE_METHOD_BASETYPE (TREE_TYPE (type))
6168 == TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))))
6170 if (type != TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
6172 npfn = build1 (NOP_EXPR, TYPE_GET_PTRMEMFUNC_TYPE (type), pfn);
6173 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6178 if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
6182 if (TREE_CODE (pfn) != CONSTRUCTOR)
6185 ndelta = convert (ptrdiff_type_node, build_component_ref (pfn, delta_identifier, 0, 0));
6186 ndelta2 = convert (ptrdiff_type_node, DELTA2_FROM_PTRMEMFUNC (pfn));
6187 index = build_component_ref (pfn, index_identifier, 0, 0);
6188 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))),
6189 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6191 delta = build_binary_op (PLUS_EXPR, delta, ndelta, 1);
6192 delta2 = build_binary_op (PLUS_EXPR, ndelta2, delta2, 1);
6193 e1 = fold (build (GT_EXPR, boolean_type_node, index, integer_zero_node));
6195 u = build_nt (CONSTRUCTOR, 0, tree_cons (delta2_identifier, delta2, NULL_TREE));
6196 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6197 tree_cons (NULL_TREE, index,
6198 tree_cons (NULL_TREE, u, NULL_TREE))));
6199 e2 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6201 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6202 npfn = build1 (NOP_EXPR, type, pfn);
6203 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6205 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, npfn, NULL_TREE));
6206 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6207 tree_cons (NULL_TREE, index,
6208 tree_cons (NULL_TREE, u, NULL_TREE))));
6209 e3 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6210 return build_conditional_expr (e1, e2, e3);
6213 ndelta = TREE_VALUE (CONSTRUCTOR_ELTS (pfn));
6214 nindex = TREE_VALUE (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn)));
6215 npfn = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn))));
6216 npfn = TREE_VALUE (CONSTRUCTOR_ELTS (npfn));
6217 if (integer_zerop (nindex))
6218 pfn = integer_zero_node;
6221 sorry ("value casting of varible nonnull pointer to member functions not supported");
6222 return error_mark_node;
6226 /* Handle null pointer to member function conversions. */
6227 if (integer_zerop (pfn))
6229 pfn = build_c_cast (type, integer_zero_node, 0);
6230 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, pfn, NULL_TREE));
6231 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, integer_zero_node,
6232 tree_cons (NULL_TREE, integer_zero_node,
6233 tree_cons (NULL_TREE, u, NULL_TREE))));
6234 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6237 if (TREE_CODE (pfn) == TREE_LIST
6238 || (TREE_CODE (pfn) == ADDR_EXPR
6239 && TREE_CODE (TREE_OPERAND (pfn, 0)) == TREE_LIST))
6241 pfn = instantiate_type (type, pfn, 1);
6242 if (pfn == error_mark_node)
6243 return error_mark_node;
6244 if (TREE_CODE (pfn) != ADDR_EXPR)
6245 pfn = build_unary_op (ADDR_EXPR, pfn, 0);
6248 /* Allow pointer to member conversions here. */
6249 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn))),
6250 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6252 delta2 = build_binary_op (PLUS_EXPR, delta2, delta, 1);
6254 if (TREE_CODE (TREE_OPERAND (pfn, 0)) != FUNCTION_DECL)
6255 warning ("assuming pointer to member function is non-virtual");
6257 if (TREE_CODE (TREE_OPERAND (pfn, 0)) == FUNCTION_DECL
6258 && DECL_VINDEX (TREE_OPERAND (pfn, 0)))
6260 /* Find the offset to the vfield pointer in the object. */
6261 vfield_offset = get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn, 0)),
6262 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn, 0)),
6264 vfield_offset = get_vfield_offset (vfield_offset);
6265 delta2 = size_binop (PLUS_EXPR, vfield_offset, delta2);
6267 /* Map everything down one to make room for the null pointer to member. */
6268 index = size_binop (PLUS_EXPR,
6269 DECL_VINDEX (TREE_OPERAND (pfn, 0)),
6271 u = build_nt (CONSTRUCTOR, 0, tree_cons (delta2_identifier, delta2, NULL_TREE));
6275 index = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6277 npfn = build1 (NOP_EXPR, type, pfn);
6278 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6280 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, npfn, NULL_TREE));
6283 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6284 tree_cons (NULL_TREE, index,
6285 tree_cons (NULL_TREE, u, NULL_TREE))));
6286 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6289 /* Convert value RHS to type TYPE as preparation for an assignment
6290 to an lvalue of type TYPE.
6291 The real work of conversion is done by `convert'.
6292 The purpose of this function is to generate error messages
6293 for assignments that are not allowed in C.
6294 ERRTYPE is a string to use in error messages:
6295 "assignment", "return", etc.
6297 C++: attempts to allow `convert' to find conversions involving
6298 implicit type conversion between aggregate and scalar types
6299 as per 8.5.6 of C++ manual. Does not randomly dereference
6300 pointers to aggregates! */
6303 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6309 register enum tree_code codel = TREE_CODE (type);
6310 register tree rhstype;
6311 register enum tree_code coder = TREE_CODE (TREE_TYPE (rhs));
6313 if (coder == UNKNOWN_TYPE)
6314 rhs = instantiate_type (type, rhs, 1);
6316 if (coder == ERROR_MARK)
6317 return error_mark_node;
6319 if (codel == OFFSET_TYPE)
6321 type = TREE_TYPE (type);
6322 codel = TREE_CODE (type);
6325 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6326 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6327 rhs = TREE_OPERAND (rhs, 0);
6329 if (rhs == error_mark_node)
6330 return error_mark_node;
6332 if (TREE_VALUE (rhs) == error_mark_node)
6333 return error_mark_node;
6335 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6337 rhs = resolve_offset_ref (rhs);
6338 if (rhs == error_mark_node)
6339 return error_mark_node;
6340 rhstype = TREE_TYPE (rhs);
6341 coder = TREE_CODE (rhstype);
6344 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6345 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6346 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6347 rhs = default_conversion (rhs);
6348 else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6349 rhs = convert_from_reference (rhs);
6351 rhstype = TREE_TYPE (rhs);
6352 coder = TREE_CODE (rhstype);
6354 /* This should no longer change types on us. */
6355 if (TREE_CODE (rhs) == CONST_DECL)
6356 rhs = DECL_INITIAL (rhs);
6357 else if (TREE_READONLY_DECL_P (rhs))
6358 rhs = decl_constant_value (rhs);
6360 if (type == rhstype)
6362 overflow_warning (rhs);
6366 if (coder == VOID_TYPE)
6368 error ("void value not ignored as it ought to be");
6369 return error_mark_node;
6371 /* Arithmetic types all interconvert. */
6372 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == BOOLEAN_TYPE)
6373 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == BOOLEAN_TYPE))
6375 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6376 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6379 cp_warning ("`%T' used for argument %P of `%D'",
6380 rhstype, parmnum, fndecl);
6382 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6384 /* And we should warn if assigning a negative value to
6385 an unsigned variable. */
6386 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6388 if (TREE_CODE (rhs) == INTEGER_CST
6389 && TREE_NEGATED_INT (rhs))
6392 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6393 rhs, parmnum, fndecl);
6395 cp_warning ("%s of negative value `%E' to `%T'",
6396 errtype, rhs, type);
6398 overflow_warning (rhs);
6399 if (TREE_CONSTANT (rhs))
6403 return convert_and_check (type, rhs);
6405 /* Conversions involving enums. */
6406 else if ((codel == ENUMERAL_TYPE
6407 && (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE))
6408 || (coder == ENUMERAL_TYPE
6409 && (INTEGRAL_CODE_P (codel) || codel == REAL_TYPE)))
6411 return cp_convert (type, rhs, CONV_IMPLICIT, 0);
6413 /* Conversions among pointers */
6414 else if (codel == POINTER_TYPE
6415 && (coder == POINTER_TYPE
6416 || (coder == RECORD_TYPE
6417 && (IS_SIGNATURE_POINTER (rhstype)
6418 || IS_SIGNATURE_REFERENCE (rhstype)))))
6420 register tree ttl = TREE_TYPE (type);
6424 if (coder == RECORD_TYPE)
6426 rhs = build_optr_ref (rhs);
6427 rhstype = TREE_TYPE (rhs);
6429 ttr = TREE_TYPE (rhstype);
6431 /* If both pointers are of aggregate type, then we
6432 can give better error messages, and save some work
6434 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6438 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6439 || type == class_star_type_node
6440 || rhstype == class_star_type_node)
6441 binfo = TYPE_BINFO (ttl);
6443 binfo = get_binfo (ttl, ttr, 1);
6445 if (binfo == error_mark_node)
6446 return error_mark_node;
6448 return error_not_base_type (ttl, ttr);
6450 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6453 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6454 rhstype, parmnum, fndecl);
6456 cp_pedwarn ("%s to `%T' from `%T' discards const",
6457 errtype, type, rhstype);
6459 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6462 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6463 rhstype, parmnum, fndecl);
6465 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6466 errtype, type, rhstype);
6470 /* Any non-function converts to a [const][volatile] void *
6471 and vice versa; otherwise, targets must be the same.
6472 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6473 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6474 || TYPE_MAIN_VARIANT (ttr) == void_type_node
6475 || (ctt = comp_target_types (type, rhstype, 1))
6476 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6477 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6479 /* ARM $4.8, commentary on p39. */
6480 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6481 && TREE_CODE (ttr) == OFFSET_TYPE)
6483 cp_error ("no standard conversion from `%T' to `void *'", ttr);
6484 return error_mark_node;
6488 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6491 if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6492 && TYPE_MAIN_VARIANT (ttr) == void_type_node
6493 && rhs != null_pointer_node)
6495 if (coder == RECORD_TYPE)
6496 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6499 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6502 /* Const and volatile mean something different for function types,
6503 so the usual warnings are not appropriate. */
6504 else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6505 || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6507 if (TREE_CODE (ttl) == OFFSET_TYPE
6508 && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6509 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6511 sorry ("%s between pointer to members converting across virtual baseclasses", errtype);
6512 return error_mark_node;
6514 else if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6517 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6518 rhstype, parmnum, fndecl);
6520 cp_pedwarn ("%s to `%T' from `%T' discards const",
6521 errtype, type, rhstype);
6523 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6526 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6527 rhstype, parmnum, fndecl);
6529 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6530 errtype, type, rhstype);
6532 else if (TREE_CODE (ttl) == TREE_CODE (ttr)
6533 && ! comp_target_types (type, rhstype, 1))
6536 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6537 rhstype, parmnum, fndecl);
6539 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6540 errtype, type, rhstype);
6544 else if (TREE_CODE (ttr) == OFFSET_TYPE
6545 && TREE_CODE (ttl) != OFFSET_TYPE)
6547 /* Normally, pointers to different type codes (other
6548 than void) are not compatible, but we perform
6549 some type instantiation if that resolves the
6550 ambiguity of (X Y::*) and (X *). */
6552 if (current_class_decl)
6554 if (TREE_CODE (rhs) == INTEGER_CST)
6556 rhs = build (PLUS_EXPR, build_pointer_type (TREE_TYPE (ttr)),
6557 current_class_decl, rhs);
6558 return convert_for_assignment (type, rhs,
6559 errtype, fndecl, parmnum);
6562 if (TREE_CODE (ttl) == METHOD_TYPE)
6563 error ("%s between pointer-to-method and pointer-to-member types",
6566 error ("%s between pointer and pointer-to-member types", errtype);
6567 return error_mark_node;
6571 int add_quals = 0, const_parity = 0, volatile_parity = 0;
6573 int unsigned_parity;
6576 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6577 for (; ; ttl = TREE_TYPE (ttl), ttr = TREE_TYPE (ttr))
6580 const_parity |= TYPE_READONLY (ttl) < TYPE_READONLY (ttr);
6581 volatile_parity |= TYPE_VOLATILE (ttl) < TYPE_VOLATILE (ttr);
6584 && (TYPE_READONLY (ttl) > TYPE_READONLY (ttr)
6585 || TYPE_VOLATILE (ttl) > TYPE_VOLATILE (ttr)))
6587 left_const &= TYPE_READONLY (ttl);
6589 if (TREE_CODE (ttl) != POINTER_TYPE
6590 || TREE_CODE (ttr) != POINTER_TYPE)
6593 unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6594 if (unsigned_parity)
6596 if (TREE_UNSIGNED (ttl))
6597 ttr = unsigned_type (ttr);
6599 ttl = unsigned_type (ttl);
6602 if (comp_target_types (ttl, ttr, nptrs) > 0)
6607 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6608 rhstype, parmnum, fndecl);
6610 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6611 errtype, type, rhstype);
6616 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6617 rhstype, parmnum, fndecl);
6619 cp_pedwarn ("%s to `%T' from `%T' discards const",
6620 errtype, type, rhstype);
6622 if (volatile_parity)
6625 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6626 rhstype, parmnum, fndecl);
6628 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6629 errtype, type, rhstype);
6631 if (unsigned_parity > 0)
6634 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6635 rhstype, parmnum, fndecl);
6637 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6638 errtype, type, rhstype);
6640 else if (unsigned_parity < 0)
6643 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6644 rhstype, parmnum, fndecl);
6646 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6647 errtype, type, rhstype);
6650 /* C++ is not so friendly about converting function and
6651 member function pointers as C. Emit warnings here. */
6652 if (TREE_CODE (ttl) == FUNCTION_TYPE
6653 || TREE_CODE (ttl) == METHOD_TYPE)
6654 if (! comptypes (ttl, ttr, 0))
6656 warning ("conflicting function types in %s:", errtype);
6657 cp_warning ("\t`%T' != `%T'", type, rhstype);
6660 else if (TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6662 /* When does this happen? */
6663 my_friendly_abort (119);
6664 /* Conversion of a pointer-to-member type to void *. */
6665 rhs = build_unary_op (ADDR_EXPR, rhs, 0);
6666 TREE_TYPE (rhs) = type;
6669 else if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6671 /* When does this happen? */
6672 my_friendly_abort (120);
6673 /* Conversion of a pointer-to-member type to void *. */
6674 rhs = build_unary_op (ADDR_EXPR, rhs, 0);
6675 TREE_TYPE (rhs) = type;
6681 cp_error ("passing `%T' as argument %P of `%D'",
6682 rhstype, parmnum, fndecl);
6684 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6685 return error_mark_node;
6688 return convert (type, rhs);
6690 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6692 /* An explicit constant 0 can convert to a pointer,
6693 but not a 0 that results from casting or folding. */
6694 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
6697 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6698 rhstype, parmnum, fndecl);
6700 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6701 errtype, type, rhstype);
6702 return convert (type, rhs);
6704 return null_pointer_node;
6706 else if (codel == INTEGER_TYPE
6707 && (coder == POINTER_TYPE
6708 || (coder == RECORD_TYPE
6709 && (IS_SIGNATURE_POINTER (rhstype)
6710 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6711 || IS_SIGNATURE_REFERENCE (rhstype)))))
6714 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6715 rhstype, parmnum, fndecl);
6717 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6718 errtype, type, rhstype);
6719 return convert (type, rhs);
6721 else if (codel == BOOLEAN_TYPE
6722 && (coder == POINTER_TYPE
6723 || (coder == RECORD_TYPE
6724 && (IS_SIGNATURE_POINTER (rhstype)
6725 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6726 || IS_SIGNATURE_REFERENCE (rhstype)))))
6727 return convert (type, rhs);
6730 else if (((coder == POINTER_TYPE && TREE_CODE (rhs) == ADDR_EXPR
6731 && TREE_CODE (rhstype) == POINTER_TYPE
6732 && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
6733 || integer_zerop (rhs)
6734 || TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6735 && TYPE_PTRMEMFUNC_P (type))
6737 /* compatible pointer to member functions. */
6738 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), rhs, 0);
6740 else if (codel == ERROR_MARK || coder == ERROR_MARK)
6741 return error_mark_node;
6743 /* This should no longer happen. References are initialized via
6744 `convert_for_initialization'. They should otherwise be
6745 bashed before coming here. */
6746 else if (codel == REFERENCE_TYPE)
6747 my_friendly_abort (317);
6748 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
6750 tree nrhs = build1 (NOP_EXPR, type, rhs);
6751 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6754 else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
6755 return convert (type, rhs);
6757 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6758 return error_mark_node;
6761 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6762 it is the target of the initialization.
6763 ERRTYPE is a string to use in error messages.
6765 Two major differences between the behavior of
6766 `convert_for_assignment' and `convert_for_initialization'
6767 are that references are bashed in the former, while
6768 copied in the latter, and aggregates are assigned in
6769 the former (operator=) while initialized in the
6772 If using constructor make sure no conversion operator exists, if one does
6773 exist, an ambiguity exists. */
6775 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6776 tree exp, type, rhs;
6782 register enum tree_code codel = TREE_CODE (type);
6783 register tree rhstype;
6784 register enum tree_code coder;
6786 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6787 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6788 if (TREE_CODE (rhs) == NOP_EXPR
6789 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6790 && codel != REFERENCE_TYPE)
6791 rhs = TREE_OPERAND (rhs, 0);
6793 if (rhs == error_mark_node
6794 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6795 return error_mark_node;
6797 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6799 rhs = resolve_offset_ref (rhs);
6800 if (rhs == error_mark_node)
6801 return error_mark_node;
6804 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6805 rhs = convert_from_reference (rhs);
6807 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6808 && TREE_CODE (type) != ARRAY_TYPE
6809 && (TREE_CODE (type) != REFERENCE_TYPE
6810 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6811 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6812 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6813 rhs = default_conversion (rhs);
6815 rhstype = TREE_TYPE (rhs);
6816 coder = TREE_CODE (rhstype);
6818 if (coder == UNKNOWN_TYPE)
6820 rhs = instantiate_type (type, rhs, 1);
6821 rhstype = TREE_TYPE (rhs);
6822 coder = TREE_CODE (rhstype);
6825 if (coder == ERROR_MARK)
6826 return error_mark_node;
6829 /* This is *not* the quick way out! It is the way to disaster. */
6830 if (type == rhstype)
6834 /* We accept references to incomplete types, so we can
6835 return here before checking if RHS is of complete type. */
6837 if (codel == REFERENCE_TYPE)
6839 /* This should eventually happen in convert_arguments. */
6840 extern int warningcount, errorcount;
6844 savew = warningcount, savee = errorcount;
6845 rhs = convert_to_reference (type, rhs, CONV_IMPLICIT, flags,
6846 exp ? exp : error_mark_node);
6849 if (warningcount > savew)
6850 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6851 else if (errorcount > savee)
6852 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6857 rhs = require_complete_type (rhs);
6858 if (rhs == error_mark_node)
6859 return error_mark_node;
6861 if (exp != 0) exp = require_complete_type (exp);
6862 if (exp == error_mark_node)
6863 return error_mark_node;
6865 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6866 rhstype = TREE_TYPE (rhstype);
6868 if (TYPE_LANG_SPECIFIC (type)
6869 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
6870 return build_signature_pointer_constructor (type, rhs);
6872 if (IS_AGGR_TYPE (type)
6873 && (TYPE_NEEDS_CONSTRUCTING (type) || TREE_HAS_CONSTRUCTOR (rhs)))
6875 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6877 /* This is sufficient to perform initialization. No need,
6878 apparently, to go through X(X&) to do first-cut
6879 initialization. Return through a TARGET_EXPR so that we get
6880 cleanups if it is used. */
6881 if (TREE_CODE (rhs) == CALL_EXPR)
6883 rhs = build_cplus_new (type, rhs, 0);
6886 /* Handle the case of default parameter initialization and
6887 initialization of static variables. */
6888 else if (TREE_CODE (rhs) == TARGET_EXPR)
6890 else if (TREE_CODE (rhs) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (rhs))
6892 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs, 0)) == CALL_EXPR, 318);
6895 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1)) == NULL_TREE, 316);
6896 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1))
6897 = build_unary_op (ADDR_EXPR, exp, 0);
6900 rhs = build_cplus_new (type, TREE_OPERAND (rhs, 0), 0);
6904 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)
6905 || (IS_AGGR_TYPE (rhstype) && UNIQUELY_DERIVED_FROM_P (type, rhstype)))
6907 if (TYPE_HAS_INIT_REF (type))
6909 tree init = build_method_call (exp, constructor_name_full (type),
6910 build_tree_list (NULL_TREE, rhs),
6911 TYPE_BINFO (type), LOOKUP_NORMAL);
6913 if (init == error_mark_node)
6914 return error_mark_node;
6918 exp = build_cplus_new (type, init, 0);
6922 return build (COMPOUND_EXPR, type, init, exp);
6925 /* ??? The following warnings are turned off because
6926 this is another place where the default X(X&) constructor
6928 if (TYPE_HAS_ASSIGNMENT (type))
6929 cp_warning ("bitwise copy: `%T' defines operator=", type);
6931 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6932 rhs = convert_from_reference (rhs);
6933 if (type != rhstype)
6935 tree nrhs = build1 (NOP_EXPR, type, rhs);
6936 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6942 return convert (type, rhs);
6945 if (type == TREE_TYPE (rhs))
6947 if (TREE_READONLY_DECL_P (rhs))
6948 rhs = decl_constant_value (rhs);
6952 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6955 /* Expand an ASM statement with operands, handling output operands
6956 that are not variables or INDIRECT_REFS by transforming such
6957 cases into cases that expand_asm_operands can handle.
6959 Arguments are same as for expand_asm_operands.
6961 We don't do default conversions on all inputs, because it can screw
6962 up operands that are expected to be in memory. */
6965 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6966 tree string, outputs, inputs, clobbers;
6971 int noutputs = list_length (outputs);
6973 /* o[I] is the place that output number I should be written. */
6974 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6977 /* Record the contents of OUTPUTS before it is modified. */
6978 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6979 o[i] = TREE_VALUE (tail);
6981 /* Generate the ASM_OPERANDS insn;
6982 store into the TREE_VALUEs of OUTPUTS some trees for
6983 where the values were actually stored. */
6984 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6986 /* Copy all the intermediate outputs into the specified outputs. */
6987 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6989 if (o[i] != TREE_VALUE (tail))
6991 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6992 const0_rtx, VOIDmode, 0);
6995 /* Detect modification of read-only values.
6996 (Otherwise done by build_modify_expr.) */
6999 tree type = TREE_TYPE (o[i]);
7000 if (TYPE_READONLY (type)
7001 || ((TREE_CODE (type) == RECORD_TYPE
7002 || TREE_CODE (type) == UNION_TYPE)
7003 && C_TYPE_FIELDS_READONLY (type)))
7004 readonly_error (o[i], "modification by `asm'", 1);
7008 /* Those MODIFY_EXPRs could do autoincrements. */
7012 /* Expand a C `return' statement.
7013 RETVAL is the expression for what to return,
7014 or a null pointer for `return;' with no value.
7016 C++: upon seeing a `return', we must call destructors on all
7017 variables in scope which had constructors called on them.
7018 This means that if in a destructor, the base class destructors
7019 must be called before returning.
7021 The RETURN statement in C++ has initialization semantics. */
7024 c_expand_return (retval)
7027 extern struct nesting *cond_stack, *loop_stack, *case_stack;
7028 extern tree dtor_label, ctor_label;
7029 tree result = DECL_RESULT (current_function_decl);
7030 tree valtype = TREE_TYPE (result);
7031 register int use_temp = 0;
7032 int returns_value = 1;
7034 if (TREE_THIS_VOLATILE (current_function_decl))
7035 warning ("function declared `noreturn' has a `return' statement");
7037 if (retval == error_mark_node)
7039 current_function_returns_null = 1;
7043 if (retval == NULL_TREE)
7045 /* A non-named return value does not count. */
7047 /* Can't just return from a destructor. */
7050 expand_goto (dtor_label);
7054 if (DECL_CONSTRUCTOR_P (current_function_decl))
7055 retval = current_class_decl;
7056 else if (DECL_NAME (result) != NULL_TREE
7057 && TREE_CODE (valtype) != VOID_TYPE)
7061 current_function_returns_null = 1;
7063 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
7065 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
7067 pedwarn ("`return' with no value, in function returning non-void");
7068 /* Clear this, so finish_function won't say that we
7069 reach the end of a non-void function (which we don't,
7070 we gave a return!). */
7071 current_function_returns_null = 0;
7075 expand_null_return ();
7079 else if (DECL_CONSTRUCTOR_P (current_function_decl)
7080 && retval != current_class_decl)
7082 error ("return from a constructor: use `this = ...' instead");
7083 retval = current_class_decl;
7086 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
7088 current_function_returns_null = 1;
7089 /* We do this here so we'll avoid a warning about how the function
7090 "may or may not return a value" in finish_function. */
7094 pedwarn ("`return' with a value, in function returning void");
7095 expand_return (retval);
7097 /* Add some useful error checking for C++. */
7098 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
7100 tree whats_returned;
7101 tree tmp_result = result;
7103 /* Don't initialize directly into a non-BLKmode retval, since that
7104 could lose when being inlined by another caller. (GCC can't
7105 read the function return register in an inline function when
7106 the return value is being ignored). */
7107 if (result && TYPE_MODE (TREE_TYPE (tmp_result)) != BLKmode)
7110 /* convert to reference now, so we can give error if we
7111 return an reference to a non-lvalue. */
7112 retval = convert_for_initialization (tmp_result, valtype, retval,
7113 LOOKUP_NORMAL, "return",
7116 /* Sort through common things to see what it is
7117 we are returning. */
7118 whats_returned = retval;
7119 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7121 whats_returned = TREE_OPERAND (whats_returned, 1);
7122 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7123 whats_returned = TREE_OPERAND (whats_returned, 0);
7125 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7127 whats_returned = TREE_OPERAND (whats_returned, 0);
7128 while (TREE_CODE (whats_returned) == NEW_EXPR
7129 || TREE_CODE (whats_returned) == TARGET_EXPR
7130 || TREE_CODE (whats_returned) == WITH_CLEANUP_EXPR)
7132 /* Get the target. */
7133 whats_returned = TREE_OPERAND (whats_returned, 0);
7134 warning ("returning reference to temporary");
7138 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
7140 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
7141 warning ("reference to non-lvalue returned");
7142 else if (! TREE_STATIC (whats_returned)
7143 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned)))
7144 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
7147 else if (TREE_CODE (retval) == ADDR_EXPR)
7149 tree whats_returned = TREE_OPERAND (retval, 0);
7151 if (TREE_CODE (whats_returned) == VAR_DECL
7152 && DECL_NAME (whats_returned)
7153 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7154 && !TREE_STATIC (whats_returned))
7155 cp_warning_at ("address of local variable `%D' returned", whats_returned);
7158 /* Now deal with possible C++ hair:
7159 (1) Compute the return value.
7160 (2) If there are aggregate values with destructors which
7161 must be cleaned up, clean them (taking care
7162 not to clobber the return value).
7163 (3) If an X(X&) constructor is defined, the return
7164 value must be returned via that. */
7166 if (retval == result
7167 /* Watch out for constructors, which "return" aggregates
7168 via initialization, but which otherwise "return" a pointer. */
7169 || DECL_CONSTRUCTOR_P (current_function_decl))
7171 /* This is just an error--it's already been reported. */
7172 if (TYPE_SIZE (valtype) == NULL_TREE)
7175 if (TYPE_MODE (valtype) != BLKmode
7176 && any_pending_cleanups (1))
7178 retval = get_temp_regvar (valtype, retval);
7179 use_temp = obey_regdecls;
7182 else if (IS_AGGR_TYPE (valtype) && TYPE_NEEDS_CONSTRUCTING (valtype))
7184 /* Throw away the cleanup that `build_functional_cast' gave us. */
7185 if (TREE_CODE (retval) == WITH_CLEANUP_EXPR
7186 && TREE_CODE (TREE_OPERAND (retval, 0)) == TARGET_EXPR)
7187 retval = TREE_OPERAND (retval, 0);
7188 expand_aggr_init (result, retval, 0, LOOKUP_ONLYCONVERTING);
7189 expand_cleanups_to (NULL_TREE);
7190 DECL_INITIAL (result) = NULL_TREE;
7195 if (TYPE_MODE (valtype) == VOIDmode)
7197 if (TYPE_MODE (TREE_TYPE (result)) != VOIDmode
7198 && warn_return_type)
7199 warning ("return of void value in function returning non-void");
7200 expand_expr_stmt (retval);
7204 else if (TYPE_MODE (valtype) != BLKmode
7205 && any_pending_cleanups (1))
7207 retval = get_temp_regvar (valtype, retval);
7208 expand_cleanups_to (NULL_TREE);
7209 use_temp = obey_regdecls;
7214 retval = convert_for_initialization (result, valtype, retval,
7216 "return", NULL_TREE, 0);
7217 DECL_INITIAL (result) = NULL_TREE;
7219 if (retval == error_mark_node)
7225 if (retval != NULL_TREE
7226 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
7227 && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
7228 current_function_return_value = retval;
7232 /* Everything's great--RETVAL is in RESULT. */
7233 if (original_result_rtx)
7235 store_expr (result, original_result_rtx, 0);
7236 expand_cleanups_to (NULL_TREE);
7238 else if (retval && retval != result)
7240 /* Clear this out so the later call to decl_function_context
7241 won't end up bombing on us. */
7242 if (DECL_CONTEXT (result) == error_mark_node)
7243 DECL_CONTEXT (result) = NULL_TREE;
7244 /* Here is where we finally get RETVAL into RESULT.
7245 `expand_return' does the magic of protecting
7246 RESULT from cleanups. */
7247 retval = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (result),
7249 /* This part _must_ come second, because expand_return looks for
7250 the INIT_EXPR as the toplevel node only. :-( */
7251 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7252 TREE_SIDE_EFFECTS (retval) = 1;
7253 expand_return (retval);
7256 expand_return (result);
7258 use_variable (DECL_RTL (result));
7259 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7260 expand_goto (ctor_label);
7262 expand_null_return ();
7266 /* We may still need to put RETVAL into RESULT. */
7267 result = DECL_RESULT (current_function_decl);
7268 if (original_result_rtx)
7270 /* Here we have a named return value that went
7271 into memory. We can compute RETVAL into that. */
7273 expand_assignment (result, retval, 0, 0);
7275 store_expr (result, original_result_rtx, 0);
7276 result = make_tree (TREE_TYPE (result), original_result_rtx);
7278 else if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7280 /* Here RETVAL is CURRENT_CLASS_DECL, so there's nothing to do. */
7281 expand_goto (ctor_label);
7285 /* Here is where we finally get RETVAL into RESULT.
7286 `expand_return' does the magic of protecting
7287 RESULT from cleanups. */
7288 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7289 TREE_SIDE_EFFECTS (result) = 1;
7290 expand_return (result);
7292 else if (TYPE_MODE (TREE_TYPE (result)) != VOIDmode)
7293 expand_return (result);
7296 current_function_returns_value = returns_value;
7298 /* These wind up after the BARRIER, which causes problems for
7299 expand_end_binding. What purpose were they supposed to serve? */
7300 if (original_result_rtx)
7301 use_variable (original_result_rtx);
7303 use_variable (DECL_RTL (DECL_RESULT (current_function_decl)));
7306 /* One way to clear out cleanups that EXPR might
7307 generate. Note that this code will really be
7308 dead code, but that is ok--cleanups that were
7309 needed were handled by the magic of `return'. */
7310 expand_cleanups_to (NULL_TREE);
7313 /* Start a C switch statement, testing expression EXP.
7314 Return EXP if it is valid, an error node otherwise. */
7317 c_expand_start_case (exp)
7321 register enum tree_code code;
7323 /* Convert from references, etc. */
7324 exp = default_conversion (exp);
7325 type = TREE_TYPE (exp);
7326 code = TREE_CODE (type);
7328 if (IS_AGGR_TYPE_CODE (code))
7329 exp = build_type_conversion (CONVERT_EXPR, integer_type_node, exp, 1);
7331 if (exp == NULL_TREE)
7333 error ("switch quantity not an integer");
7334 exp = error_mark_node;
7336 type = TREE_TYPE (exp);
7337 code = TREE_CODE (type);
7339 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
7341 error ("switch quantity not an integer");
7342 exp = error_mark_node;
7348 exp = default_conversion (exp);
7349 type = TREE_TYPE (exp);
7350 index = get_unwidened (exp, 0);
7351 /* We can't strip a conversion from a signed type to an unsigned,
7352 because if we did, int_fits_type_p would do the wrong thing
7353 when checking case values for being in range,
7354 and it's too hard to do the right thing. */
7355 if (TREE_UNSIGNED (TREE_TYPE (exp))
7356 == TREE_UNSIGNED (TREE_TYPE (index)))
7361 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7362 type, "switch statement");
7367 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7368 type have been const. */
7370 comp_ptr_ttypes_real (to, from, constp)
7374 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7376 if (TREE_CODE (to) != TREE_CODE (from))
7379 if (TYPE_READONLY (from) > TYPE_READONLY (to)
7380 || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7384 && (TYPE_READONLY (to) > TYPE_READONLY (from)
7385 || TYPE_VOLATILE (to) > TYPE_READONLY (from)))
7387 constp &= TYPE_READONLY (to);
7389 if (TREE_CODE (to) != POINTER_TYPE)
7390 return comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1);
7394 /* When comparing, say, char ** to char const **, this function takes the
7395 'char *' and 'char const *'. Do not pass non-pointer types to this
7398 comp_ptr_ttypes (to, from)
7401 return comp_ptr_ttypes_real (to, from, 1);